diff --git a/neuropercolation/display.py b/neuropercolation/display.py index 6558317..518cb47 100644 --- a/neuropercolation/display.py +++ b/neuropercolation/display.py @@ -70,8 +70,11 @@ class Simulate1Layer: def __init__(self, dim, eps, - res=4, + steps=100, draw='pygame', + res=4, + fps=30, + save=None, path='/cloud/Public/_data/neuropercolation/test/', state_to_color_cb=None, *args, **kwargs): @@ -94,9 +97,11 @@ class Simulate1Layer: self.__rect = _Rect(left=0, top=30, width=dim*res, height=dim*res) 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.__save = save self.__path = path self.__state_list = [] self.__activation_list = [] + self.run(evolutions_per_second=fps, last_evolution_step=steps) def run(self, evolutions_per_second=0, @@ -110,18 +115,13 @@ class Simulate1Layer: :param last_evolution_step: 0 = infinite | > 0 evolution step at which this method will stop Warning: is blocking until finished """ - if save_states: - self._append_state() - else: - self._append_activation() + + self._track() with contextlib.suppress(KeyboardInterrupt): while self.__active and self._not_at_the_end(last_evolution_step): time_ca_start = time.time() self._cellular_automaton.evolve(evolutions_per_draw) - if save_states: - self._append_state() - else: - self._append_activation() + self._track() if self.__draw_engine is not None: time_ca_end = time.time() self._redraw_dirty_cells() @@ -141,11 +141,15 @@ class Simulate1Layer: self.__draw_engine._pygame.quit() except: print('Failed to quit pygame') - if save_states: - self._save_state_list() - self._save_activation_list() - - def _append_state(self): + self._save() + + def _track(self): + if self.__save == 'all': + self._append_all() + elif self.__save == 'simple': + self._append_activation() + + def _append_all(self): automaton_state = [[0 for n in range(self.__dimension)] for m in range(self.__dimension)] activation = 0 for coord, cell in self._cellular_automaton._current_state.items(): @@ -169,14 +173,22 @@ class Simulate1Layer: activation = round(1-2*activation/self.__dimension**2, 6) self.__activation_list.append(activation) - def _save_state_list(self): + def _save(self): + if self.__save == 'all': + self._save_all() + elif self.__save == 'simple': + self._save_activation() + + def _save_all(self): if not os.path.exists(self.__path): os.makedirs(self.__path) + with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_activation.txt", 'w', encoding='utf-8') as f: + json.dump(self.__activation_list, f, indent=1) 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 _save_activation_list(self): + def _save_activation(self): if not os.path.exists(self.__path): os.makedirs(self.__path) @@ -230,8 +242,11 @@ class Simulate2Layers: def __init__(self, dim, eps, - res=4, + steps=100, draw='pygame', + res=4, + fps=30, + save=None, path='/cloud/Public/_data/neuropercolation/test/', state_to_color_cb=None, *args, **kwargs): @@ -256,9 +271,11 @@ class Simulate2Layers: 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.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb + self.__save = save self.__path = path self.__state_list = [] self.__activation_list = [] + self.run(evolutions_per_second=fps, last_evolution_step=steps) def run(self, evolutions_per_second=0, @@ -272,18 +289,12 @@ class Simulate2Layers: :param last_evolution_step: 0 = infinite | > 0 evolution step at which this method will stop Warning: is blocking until finished """ - if save_states: - self._append_state() - else: - self._append_activation() + self._track() with contextlib.suppress(KeyboardInterrupt): while self.__active and self._not_at_the_end(last_evolution_step): time_ca_start = time.time() self._cellular_automaton.evolve(evolutions_per_draw) - if save_states: - self._append_state() - else: - self._append_activation() + self._track() if self.__draw_engine is not None: time_ca_end = time.time() self._redraw_dirty_cells() @@ -304,11 +315,15 @@ class Simulate2Layers: except: print('Failed to quit pygame') - if save_states: - self._save_state_list() - self._save_activation_list() + self._save() - def _append_state(self): + def _track(self): + if self.__save == 'all': + self._append_all() + elif self.__save == 'simple': + self._append_activation() + + def _append_all(self): automaton_state = [[[0 for n in range(self.__dimension)] for m in range(self.__dimension)] for l in range(2)] activation = [0]*2 for coord, cell in self._cellular_automaton._current_state.items(): @@ -331,15 +346,23 @@ class Simulate2Layers: activation = [round(1-2*act/self.__dimension**2, 6) for act in activation] self.__activation_list.append(activation) + + def _save(self): + if self.__save == 'all': + self._save_all() + elif self.__save == 'simple': + self._save_activation() - def _save_state_list(self): + def _save_all(self): if not os.path.exists(self.__path): os.makedirs(self.__path) + with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_activation.txt", 'w', encoding='utf-8') as f: + json.dump(self.__activation_list, f, indent=1) 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 _save_activation_list(self): + def _save_activation(self): if not os.path.exists(self.__path): os.makedirs(self.__path) @@ -398,8 +421,11 @@ class Simulate4Layers: dim, eps, coupling=[], - res=4, + steps=100, draw='pygame', + res=4, + fps=30, + save=None, path='/cloud/Public/_data/neuropercolation/test/', state_to_color_cb=None, *args, **kwargs): @@ -424,9 +450,11 @@ class Simulate4Layers: 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.__state_to_color = self._get_cell_color if state_to_color_cb is None else state_to_color_cb + self.__save = save self.__path = path self.__state_list = [] self.__activation_list = [] + self.run(evolutions_per_second=fps, last_evolution_step=steps) def run(self, evolutions_per_second=0, @@ -441,17 +469,11 @@ class Simulate4Layers: Warning: is blocking until finished """ with contextlib.suppress(KeyboardInterrupt): - if save_states: - self._append_state() - else: - self._append_activation() + self._track() while self.__active and self._not_at_the_end(last_evolution_step): time_ca_start = time.time() self._cellular_automaton.evolve(evolutions_per_draw) - if save_states: - self._append_state() - else: - self._append_activation() + self._track() if self.__draw_engine is not None: time_ca_end = time.time() self._redraw_dirty_cells() @@ -470,11 +492,16 @@ class Simulate4Layers: self.__draw_engine._pygame.quit() except: print('Failed to quit pygame') - if save_states: - self._save_state_list() - self._save_activation_list() - def _append_state(self): + self._save() + + def _track(self): + if self.__save == 'all': + self._append_all() + elif self.__save == 'simple': + self._append_activation() + + def _append_all(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)] activation = [[0,0],[0,0]] for coord, cell in self._cellular_automaton._current_state.items(): @@ -500,14 +527,22 @@ class Simulate4Layers: activation = [[round(1-2*act/self.__dimension**2, 6) for act in layer] for layer in activation] self.__activation_list.append(activation) - def _save_state_list(self): + def _save(self): + if self.__save == 'all': + self._save_all() + elif self.__save == 'simple': + self._save_activation() + + def _save_all(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) + with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_activation.txt", 'w', encoding='utf-8') as f: + json.dump(self.__activation_list, f, indent=1) - def _save_activation_list(self): + def _save_activation(self): if not os.path.exists(self.__path): os.makedirs(self.__path)