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,
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,6 +131,7 @@ 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:
@ -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,6 +302,7 @@ 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:
@ -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,6 +333,7 @@ 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
@ -341,23 +348,27 @@ 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:
@ -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,6 +499,7 @@ 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)]
@ -503,6 +517,7 @@ 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]]
@ -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: