Add json dump for automaton states
This commit is contained in:
parent
d35beaea81
commit
ad73f60e6b
@ -16,6 +16,9 @@ limitations under the License.
|
|||||||
|
|
||||||
# pylint: disable=all
|
# pylint: disable=all
|
||||||
|
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
import pbjson
|
||||||
import time
|
import time
|
||||||
import operator
|
import operator
|
||||||
import collections
|
import collections
|
||||||
@ -69,6 +72,7 @@ class Simulate2Layers:
|
|||||||
eps,
|
eps,
|
||||||
res=4,
|
res=4,
|
||||||
draw='pygame',
|
draw='pygame',
|
||||||
|
path='/cloud/Public/_data/neuropercolation/test/',
|
||||||
state_to_color_cb=None,
|
state_to_color_cb=None,
|
||||||
*args, **kwargs):
|
*args, **kwargs):
|
||||||
"""
|
"""
|
||||||
@ -83,13 +87,17 @@ class Simulate2Layers:
|
|||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self._cellular_automaton = Neuropercolation(dim,eps)
|
self._cellular_automaton = Neuropercolation(dim,eps)
|
||||||
self.__cell_size = [res,res]
|
self.__cell_size = [res,res]
|
||||||
|
self.__dimension = dim
|
||||||
|
self.__epsilon = eps
|
||||||
self.__gridside = dim*res
|
self.__gridside = dim*res
|
||||||
self.__samegap = 10
|
self.__samegap = 10
|
||||||
self.__othergap = 20
|
self.__othergap = 20
|
||||||
self.__rect = _Rect(left=0, top=30, width=dim*res, height=2*dim*res+self.__samegap)
|
self.__rect = _Rect(left=0, top=30, width=dim*res, height=2*dim*res+self.__samegap)
|
||||||
self.__draw_engine = PygameEngine((self.__rect.width,self.__rect.height+self.__rect.top)) if draw == 'pygame' else draw
|
self.__draw_engine = PygameEngine((self.__rect.width,self.__rect.height+self.__rect.top)) if draw == 'pygame' else draw
|
||||||
self.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb
|
self.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb
|
||||||
|
self.__path = path
|
||||||
|
self.__state_list = []
|
||||||
|
|
||||||
def run(self,
|
def run(self,
|
||||||
evolutions_per_second=0,
|
evolutions_per_second=0,
|
||||||
evolutions_per_draw=1,
|
evolutions_per_draw=1,
|
||||||
@ -101,10 +109,12 @@ class Simulate2Layers:
|
|||||||
:param last_evolution_step: 0 = infinite | > 0 evolution step at which this method will stop
|
:param last_evolution_step: 0 = infinite | > 0 evolution step at which this method will stop
|
||||||
Warning: is blocking until finished
|
Warning: is blocking until finished
|
||||||
"""
|
"""
|
||||||
|
self._append_state()
|
||||||
with contextlib.suppress(KeyboardInterrupt):
|
with contextlib.suppress(KeyboardInterrupt):
|
||||||
while self._is_not_user_terminated() and self._not_at_the_end(last_evolution_step):
|
while self._is_not_user_terminated() and self._not_at_the_end(last_evolution_step):
|
||||||
time_ca_start = time.time()
|
time_ca_start = time.time()
|
||||||
self._cellular_automaton.evolve(evolutions_per_draw)
|
self._cellular_automaton.evolve(evolutions_per_draw)
|
||||||
|
self._append_state()
|
||||||
time_ca_end = time.time()
|
time_ca_end = time.time()
|
||||||
self._redraw_dirty_cells()
|
self._redraw_dirty_cells()
|
||||||
time_ds_end = time.time()
|
time_ds_end = time.time()
|
||||||
@ -117,7 +127,26 @@ class Simulate2Layers:
|
|||||||
self.__draw_engine._pygame.quit()
|
self.__draw_engine._pygame.quit()
|
||||||
except:
|
except:
|
||||||
print('Failed to quit pygame')
|
print('Failed to quit pygame')
|
||||||
|
self._save_state_list()
|
||||||
|
|
||||||
|
def _append_state(self):
|
||||||
|
automaton_state = [[[0 for n in range(self.__dimension)] for m in range(self.__dimension)] for l in range(2)]
|
||||||
|
for coord, cell in self._cellular_automaton._current_state.items():
|
||||||
|
x,y,l = coord
|
||||||
|
automaton_state[l][y][x] = int(cell.state[0])
|
||||||
|
|
||||||
|
automaton_state = [[''.join(str(bits) for bits in rows) for rows in layers] for layers in automaton_state]
|
||||||
|
automaton_state = ['.'.join(str(rows) for rows in layers) for layers in automaton_state]
|
||||||
|
automaton_state = '-'.join(str(layers) for layers in automaton_state)
|
||||||
|
self.__state_list.append(automaton_state)
|
||||||
|
|
||||||
|
def _save_state_list(self):
|
||||||
|
if not os.path.exists(self.__path):
|
||||||
|
os.makedirs(self.__path)
|
||||||
|
|
||||||
|
with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_states.txt", 'w', encoding='utf-8') as f:
|
||||||
|
json.dump(self.__state_list, f, indent=1)
|
||||||
|
|
||||||
def _sleep_to_keep_rate(self, time_taken, evolutions_per_second): # pragma: no cover
|
def _sleep_to_keep_rate(self, time_taken, evolutions_per_second): # pragma: no cover
|
||||||
if evolutions_per_second > 0:
|
if evolutions_per_second > 0:
|
||||||
rest_time = 1.0 / evolutions_per_second - time_taken
|
rest_time = 1.0 / evolutions_per_second - time_taken
|
||||||
@ -175,6 +204,7 @@ class Simulate4Layers:
|
|||||||
coupling=[],
|
coupling=[],
|
||||||
res=4,
|
res=4,
|
||||||
draw='pygame',
|
draw='pygame',
|
||||||
|
path='/cloud/Public/_data/neuropercolation/test/',
|
||||||
state_to_color_cb=None,
|
state_to_color_cb=None,
|
||||||
*args, **kwargs):
|
*args, **kwargs):
|
||||||
"""
|
"""
|
||||||
@ -189,13 +219,17 @@ class Simulate4Layers:
|
|||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self._cellular_automaton = NeuropercolationCoupled(dim,eps,coupling)
|
self._cellular_automaton = NeuropercolationCoupled(dim,eps,coupling)
|
||||||
self.__cell_size = [res,res]
|
self.__cell_size = [res,res]
|
||||||
|
self.__dimension = dim
|
||||||
|
self.__epsilon = eps
|
||||||
self.__gridside = dim*res
|
self.__gridside = dim*res
|
||||||
self.__samegap = 10
|
self.__samegap = 10
|
||||||
self.__othergap = 20
|
self.__othergap = 20
|
||||||
self.__rect = _Rect(left=0, top=30, width=2*dim*res+self.__othergap, height=2*dim*res+self.__samegap)
|
self.__rect = _Rect(left=0, top=30, width=2*dim*res+self.__othergap, height=2*dim*res+self.__samegap)
|
||||||
self.__draw_engine = PygameEngine((self.__rect.width,self.__rect.height+self.__rect.top)) if draw == 'pygame' else draw
|
self.__draw_engine = PygameEngine((self.__rect.width,self.__rect.height+self.__rect.top)) if draw == 'pygame' else draw
|
||||||
self.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb
|
self.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb
|
||||||
|
self.__path = path
|
||||||
|
self.__state_list = []
|
||||||
|
|
||||||
def run(self,
|
def run(self,
|
||||||
evolutions_per_second=0,
|
evolutions_per_second=0,
|
||||||
evolutions_per_draw=1,
|
evolutions_per_draw=1,
|
||||||
@ -208,9 +242,11 @@ class Simulate4Layers:
|
|||||||
Warning: is blocking until finished
|
Warning: is blocking until finished
|
||||||
"""
|
"""
|
||||||
with contextlib.suppress(KeyboardInterrupt):
|
with contextlib.suppress(KeyboardInterrupt):
|
||||||
|
self._append_state()
|
||||||
while self._is_not_user_terminated() and self._not_at_the_end(last_evolution_step):
|
while self._is_not_user_terminated() and self._not_at_the_end(last_evolution_step):
|
||||||
time_ca_start = time.time()
|
time_ca_start = time.time()
|
||||||
self._cellular_automaton.evolve(evolutions_per_draw)
|
self._cellular_automaton.evolve(evolutions_per_draw)
|
||||||
|
self._append_state()
|
||||||
time_ca_end = time.time()
|
time_ca_end = time.time()
|
||||||
self._redraw_dirty_cells()
|
self._redraw_dirty_cells()
|
||||||
time_ds_end = time.time()
|
time_ds_end = time.time()
|
||||||
@ -223,7 +259,27 @@ class Simulate4Layers:
|
|||||||
self.__draw_engine._pygame.quit()
|
self.__draw_engine._pygame.quit()
|
||||||
except:
|
except:
|
||||||
print('Failed to quit pygame')
|
print('Failed to quit pygame')
|
||||||
|
self._save_state_list()
|
||||||
|
|
||||||
|
def _append_state(self):
|
||||||
|
automaton_state = [[[[0 for n in range(self.__dimension)] for m in range(self.__dimension)] for l in range(2)] for k in range(2)]
|
||||||
|
for coord, cell in self._cellular_automaton._current_state.items():
|
||||||
|
x,y,l,o = coord
|
||||||
|
automaton_state[o][l][y][x] = int(cell.state[0])
|
||||||
|
|
||||||
|
automaton_state = [[[''.join(str(bits) for bits in rows) for rows in layers] for layers in oscillators] for oscillators in automaton_state]
|
||||||
|
automaton_state = [['.'.join(str(rows) for rows in layers) for layers in oscillators] for oscillators in automaton_state]
|
||||||
|
automaton_state = ['-'.join(str(layers) for layers in oscillators) for oscillators in automaton_state]
|
||||||
|
automaton_state = '='.join(str(oscillators) for oscillators in automaton_state)
|
||||||
|
self.__state_list.append(automaton_state)
|
||||||
|
|
||||||
|
def _save_state_list(self):
|
||||||
|
if not os.path.exists(self.__path):
|
||||||
|
os.makedirs(self.__path)
|
||||||
|
|
||||||
|
with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_states.txt", 'w', encoding='utf-8') as f:
|
||||||
|
json.dump(self.__state_list, f, indent=1)
|
||||||
|
|
||||||
def _sleep_to_keep_rate(self, time_taken, evolutions_per_second): # pragma: no cover
|
def _sleep_to_keep_rate(self, time_taken, evolutions_per_second): # pragma: no cover
|
||||||
if evolutions_per_second > 0:
|
if evolutions_per_second > 0:
|
||||||
rest_time = 1.0 / evolutions_per_second - time_taken
|
rest_time = 1.0 / evolutions_per_second - time_taken
|
||||||
|
Loading…
Reference in New Issue
Block a user