From 28e0ecb4a373fd4ea6bf8dcc31314aba3c837a72 Mon Sep 17 00:00:00 2001 From: timofej Date: Wed, 23 Aug 2023 16:11:44 +0200 Subject: [PATCH] Fix abort through quit event and path formatting --- neuropercolation/display.py | 63 ++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/neuropercolation/display.py b/neuropercolation/display.py index c2e3c31..253ac9f 100644 --- a/neuropercolation/display.py +++ b/neuropercolation/display.py @@ -73,7 +73,7 @@ class Simulate1Layer: steps=100, draw='pygame', res=4, - fps=30, + fps=0, save=None, path='/cloud/Public/_data/neuropercolation/test/', state_to_color_cb=None, @@ -98,9 +98,10 @@ class Simulate1Layer: 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.__path = path if path[-1]=='/' else path+'/' self.__state_list = [] self.__activation_list = [] + self.__active = True self.run(evolutions_per_second=fps, last_evolution_step=steps) def run(self, @@ -117,7 +118,7 @@ class Simulate1Layer: self._track() with contextlib.suppress(KeyboardInterrupt): - while self.__draw_engine.is_active() and self._not_at_the_end(last_evolution_step): + while self.__active() and self._not_at_the_end(last_evolution_step): time_ca_start = time.time() self._cellular_automaton.evolve(evolutions_per_draw) self._track() @@ -130,7 +131,8 @@ class Simulate1Layer: evolution_step=self._cellular_automaton.evolution_step, runlendig=len(str(last_evolution_step))) self._sleep_to_keep_rate(time.time() - time_ca_start, evolutions_per_second) - + self.__active = self.__draw_engine.is_active() + if self.__draw_engine is not None: try: self.__draw_engine._pygame.quit() @@ -240,7 +242,7 @@ class Simulate2Layers: steps=100, draw='pygame', res=4, - fps=30, + fps=0, save=None, path='/cloud/Public/_data/neuropercolation/test/', state_to_color_cb=None, @@ -267,9 +269,11 @@ class Simulate2Layers: 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.__path = path if path[-1]=='/' else path+'/' self.__state_list = [] self.__activation_list = [] + self.__channel_list = [] + self.__active = True self.run(evolutions_per_second=fps, last_evolution_step=steps) def run(self, @@ -285,7 +289,7 @@ class Simulate2Layers: """ self._track() with contextlib.suppress(KeyboardInterrupt): - while self.__draw_engine.is_active() and self._not_at_the_end(last_evolution_step): + while self.__active and self._not_at_the_end(last_evolution_step): time_ca_start = time.time() self._cellular_automaton.evolve(evolutions_per_draw) self._track() @@ -298,7 +302,8 @@ class Simulate2Layers: evolution_step=self._cellular_automaton.evolution_step, runlendig=len(str(last_evolution_step))) self._sleep_to_keep_rate(time.time() - time_ca_start, evolutions_per_second) - + self.__active = self.__draw_engine.is_active() + if self.__draw_engine is not None: try: self.__draw_engine._pygame.quit() @@ -312,6 +317,7 @@ class Simulate2Layers: self._append_all() elif self.__save == 'simple': self._append_activation() + self.__channel_list.append(self._cellular_automaton.count_channels()) def _append_all(self): automaton_state = [[[0 for n in range(self.__dimension)] for m in range(self.__dimension)] for l in range(2)] @@ -327,7 +333,8 @@ class Simulate2Layers: activation = [round(1-2*act/self.__dimension**2, 6) for act in activation] self.__state_list.append(automaton_state) self.__activation_list.append(activation) - + self.__channel_list.append(self._cellular_automaton.count_channels()) + def _append_activation(self): activation = [0]*2 for coord, cell in self._cellular_automaton._current_state.items(): @@ -341,24 +348,28 @@ class Simulate2Layers: if self.__save == 'all': self._save_all() elif self.__save == 'simple': - self._save_activation() + self._save_properties() 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) + 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}_channels.txt", 'w', encoding='utf-8') as f: + json.dump(self.__channel_list, f, indent=1) - def _save_activation(self): + def _save_properties(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}_channels.txt", 'w', encoding='utf-8') as f: + json.dump(self.__channel_list, f, indent=1) + def _sleep_to_keep_rate(self, time_taken, evolutions_per_second): # pragma: no cover if evolutions_per_second > 0: rest_time = 1.0 / evolutions_per_second - time_taken @@ -414,7 +425,7 @@ class Simulate4Layers: steps=100, draw='pygame', res=4, - fps=30, + fps=0, save=None, path='/cloud/Public/_data/neuropercolation/test/', state_to_color_cb=None, @@ -441,9 +452,11 @@ class Simulate4Layers: 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.__path = path if path[-1]=='/' else path+'/' self.__state_list = [] self.__activation_list = [] + self.__channel_list = [] + self.__active = True self.run(evolutions_per_second=fps, last_evolution_step=steps) def run(self, @@ -459,7 +472,7 @@ class Simulate4Layers: """ with contextlib.suppress(KeyboardInterrupt): self._track() - while self.__draw_engine.is_active() and self._not_at_the_end(last_evolution_step): + while self.__active and self._not_at_the_end(last_evolution_step): time_ca_start = time.time() self._cellular_automaton.evolve(evolutions_per_draw) self._track() @@ -472,7 +485,7 @@ class Simulate4Layers: evolution_step=self._cellular_automaton.evolution_step, runlendig=len(str(last_evolution_step))) self._sleep_to_keep_rate(time.time() - time_ca_start, evolutions_per_second) - + self.__active = self.__draw_engine.is_active() if self.__draw_engine is not None: try: self.__draw_engine._pygame.quit() @@ -486,7 +499,8 @@ class Simulate4Layers: self._append_all() elif self.__save == 'simple': self._append_activation() - + self.__channel_list.append(self._cellular_automaton.count_channels()) + 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]] @@ -503,7 +517,8 @@ class Simulate4Layers: activation = [[round(1-2*act/self.__dimension**2, 6) for act in layer] for layer in activation] self.__state_list.append(automaton_state) self.__activation_list.append(activation) - + self.__channel_list.append(self._cellular_automaton.count_channels()) + def _append_activation(self): activation = [[0,0],[0,0]] for coord, cell in self._cellular_automaton._current_state.items(): @@ -517,7 +532,7 @@ class Simulate4Layers: if self.__save == 'all': self._save_all() elif self.__save == 'simple': - self._save_activation() + self._save_properties() def _save_all(self): if not os.path.exists(self.__path): @@ -527,13 +542,17 @@ class Simulate4Layers: 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) + with open(self.__path+f"eps={round(self.__epsilon,3):.3f}_channels.txt", 'w', encoding='utf-8') as f: + json.dump(self.__channel_list, f, indent=1) - def _save_activation(self): + def _save_properties(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}_channels.txt", 'w', encoding='utf-8') as f: + json.dump(self.__channel_list, f, indent=1) def _sleep_to_keep_rate(self, time_taken, evolutions_per_second): # pragma: no cover if evolutions_per_second > 0: