Fix abort through quit event and path formatting

This commit is contained in:
timofej 2023-08-23 16:11:44 +02:00
parent cd0a17385f
commit 28e0ecb4a3

View File

@ -73,7 +73,7 @@ class Simulate1Layer:
steps=100, steps=100,
draw='pygame', draw='pygame',
res=4, res=4,
fps=30, fps=0,
save=None, save=None,
path='/cloud/Public/_data/neuropercolation/test/', path='/cloud/Public/_data/neuropercolation/test/',
state_to_color_cb=None, 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.__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.__save = save self.__save = save
self.__path = path self.__path = path if path[-1]=='/' else path+'/'
self.__state_list = [] self.__state_list = []
self.__activation_list = [] self.__activation_list = []
self.__active = True
self.run(evolutions_per_second=fps, last_evolution_step=steps) self.run(evolutions_per_second=fps, last_evolution_step=steps)
def run(self, def run(self,
@ -117,7 +118,7 @@ class Simulate1Layer:
self._track() self._track()
with contextlib.suppress(KeyboardInterrupt): 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() time_ca_start = time.time()
self._cellular_automaton.evolve(evolutions_per_draw) self._cellular_automaton.evolve(evolutions_per_draw)
self._track() self._track()
@ -130,7 +131,8 @@ class Simulate1Layer:
evolution_step=self._cellular_automaton.evolution_step, evolution_step=self._cellular_automaton.evolution_step,
runlendig=len(str(last_evolution_step))) runlendig=len(str(last_evolution_step)))
self._sleep_to_keep_rate(time.time() - time_ca_start, evolutions_per_second) 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: if self.__draw_engine is not None:
try: try:
self.__draw_engine._pygame.quit() self.__draw_engine._pygame.quit()
@ -240,7 +242,7 @@ class Simulate2Layers:
steps=100, steps=100,
draw='pygame', draw='pygame',
res=4, res=4,
fps=30, fps=0,
save=None, save=None,
path='/cloud/Public/_data/neuropercolation/test/', path='/cloud/Public/_data/neuropercolation/test/',
state_to_color_cb=None, 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.__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.__save = save self.__save = save
self.__path = path self.__path = path if path[-1]=='/' else path+'/'
self.__state_list = [] self.__state_list = []
self.__activation_list = [] self.__activation_list = []
self.__channel_list = []
self.__active = True
self.run(evolutions_per_second=fps, last_evolution_step=steps) self.run(evolutions_per_second=fps, last_evolution_step=steps)
def run(self, def run(self,
@ -285,7 +289,7 @@ class Simulate2Layers:
""" """
self._track() self._track()
with contextlib.suppress(KeyboardInterrupt): 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() time_ca_start = time.time()
self._cellular_automaton.evolve(evolutions_per_draw) self._cellular_automaton.evolve(evolutions_per_draw)
self._track() self._track()
@ -298,7 +302,8 @@ class Simulate2Layers:
evolution_step=self._cellular_automaton.evolution_step, evolution_step=self._cellular_automaton.evolution_step,
runlendig=len(str(last_evolution_step))) runlendig=len(str(last_evolution_step)))
self._sleep_to_keep_rate(time.time() - time_ca_start, evolutions_per_second) 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: if self.__draw_engine is not None:
try: try:
self.__draw_engine._pygame.quit() self.__draw_engine._pygame.quit()
@ -312,6 +317,7 @@ class Simulate2Layers:
self._append_all() self._append_all()
elif self.__save == 'simple': elif self.__save == 'simple':
self._append_activation() self._append_activation()
self.__channel_list.append(self._cellular_automaton.count_channels())
def _append_all(self): def _append_all(self):
automaton_state = [[[0 for n in range(self.__dimension)] for m in range(self.__dimension)] for l in range(2)] 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] activation = [round(1-2*act/self.__dimension**2, 6) for act in activation]
self.__state_list.append(automaton_state) self.__state_list.append(automaton_state)
self.__activation_list.append(activation) self.__activation_list.append(activation)
self.__channel_list.append(self._cellular_automaton.count_channels())
def _append_activation(self): def _append_activation(self):
activation = [0]*2 activation = [0]*2
for coord, cell in self._cellular_automaton._current_state.items(): for coord, cell in self._cellular_automaton._current_state.items():
@ -341,24 +348,28 @@ class Simulate2Layers:
if self.__save == 'all': if self.__save == 'all':
self._save_all() self._save_all()
elif self.__save == 'simple': elif self.__save == 'simple':
self._save_activation() self._save_properties()
def _save_all(self): def _save_all(self):
if not os.path.exists(self.__path): if not os.path.exists(self.__path):
os.makedirs(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: 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) 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): if not os.path.exists(self.__path):
os.makedirs(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: 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) 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 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
@ -414,7 +425,7 @@ class Simulate4Layers:
steps=100, steps=100,
draw='pygame', draw='pygame',
res=4, res=4,
fps=30, fps=0,
save=None, save=None,
path='/cloud/Public/_data/neuropercolation/test/', path='/cloud/Public/_data/neuropercolation/test/',
state_to_color_cb=None, 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.__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.__save = save self.__save = save
self.__path = path self.__path = path if path[-1]=='/' else path+'/'
self.__state_list = [] self.__state_list = []
self.__activation_list = [] self.__activation_list = []
self.__channel_list = []
self.__active = True
self.run(evolutions_per_second=fps, last_evolution_step=steps) self.run(evolutions_per_second=fps, last_evolution_step=steps)
def run(self, def run(self,
@ -459,7 +472,7 @@ class Simulate4Layers:
""" """
with contextlib.suppress(KeyboardInterrupt): with contextlib.suppress(KeyboardInterrupt):
self._track() 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() time_ca_start = time.time()
self._cellular_automaton.evolve(evolutions_per_draw) self._cellular_automaton.evolve(evolutions_per_draw)
self._track() self._track()
@ -472,7 +485,7 @@ class Simulate4Layers:
evolution_step=self._cellular_automaton.evolution_step, evolution_step=self._cellular_automaton.evolution_step,
runlendig=len(str(last_evolution_step))) runlendig=len(str(last_evolution_step)))
self._sleep_to_keep_rate(time.time() - time_ca_start, evolutions_per_second) 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: if self.__draw_engine is not None:
try: try:
self.__draw_engine._pygame.quit() self.__draw_engine._pygame.quit()
@ -486,7 +499,8 @@ class Simulate4Layers:
self._append_all() self._append_all()
elif self.__save == 'simple': elif self.__save == 'simple':
self._append_activation() self._append_activation()
self.__channel_list.append(self._cellular_automaton.count_channels())
def _append_all(self): 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)] 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]] 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] activation = [[round(1-2*act/self.__dimension**2, 6) for act in layer] for layer in activation]
self.__state_list.append(automaton_state) self.__state_list.append(automaton_state)
self.__activation_list.append(activation) self.__activation_list.append(activation)
self.__channel_list.append(self._cellular_automaton.count_channels())
def _append_activation(self): def _append_activation(self):
activation = [[0,0],[0,0]] activation = [[0,0],[0,0]]
for coord, cell in self._cellular_automaton._current_state.items(): for coord, cell in self._cellular_automaton._current_state.items():
@ -517,7 +532,7 @@ class Simulate4Layers:
if self.__save == 'all': if self.__save == 'all':
self._save_all() self._save_all()
elif self.__save == 'simple': elif self.__save == 'simple':
self._save_activation() self._save_properties()
def _save_all(self): def _save_all(self):
if not os.path.exists(self.__path): if not os.path.exists(self.__path):
@ -527,13 +542,17 @@ class Simulate4Layers:
json.dump(self.__state_list, f, indent=1) 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: 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) 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): if not os.path.exists(self.__path):
os.makedirs(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: 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) 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 def _sleep_to_keep_rate(self, time_taken, evolutions_per_second): # pragma: no cover
if evolutions_per_second > 0: if evolutions_per_second > 0: