Configure save options and run at class __init__

This commit is contained in:
timofej 2023-08-20 02:10:32 +02:00
parent 899aa9990b
commit 8430d735e7

View File

@ -70,8 +70,11 @@ class Simulate1Layer:
def __init__(self, def __init__(self,
dim, dim,
eps, eps,
res=4, steps=100,
draw='pygame', draw='pygame',
res=4,
fps=30,
save=None,
path='/cloud/Public/_data/neuropercolation/test/', path='/cloud/Public/_data/neuropercolation/test/',
state_to_color_cb=None, state_to_color_cb=None,
*args, **kwargs): *args, **kwargs):
@ -94,9 +97,11 @@ class Simulate1Layer:
self.__rect = _Rect(left=0, top=30, width=dim*res, height=dim*res) 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.__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.__path = path self.__path = path
self.__state_list = [] self.__state_list = []
self.__activation_list = [] self.__activation_list = []
self.run(evolutions_per_second=fps, last_evolution_step=steps)
def run(self, def run(self,
evolutions_per_second=0, 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 :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
""" """
if save_states:
self._append_state() self._track()
else:
self._append_activation()
with contextlib.suppress(KeyboardInterrupt): with contextlib.suppress(KeyboardInterrupt):
while self.__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)
if save_states: self._track()
self._append_state()
else:
self._append_activation()
if self.__draw_engine is not None: if self.__draw_engine is not None:
time_ca_end = time.time() time_ca_end = time.time()
self._redraw_dirty_cells() self._redraw_dirty_cells()
@ -141,11 +141,15 @@ class Simulate1Layer:
self.__draw_engine._pygame.quit() self.__draw_engine._pygame.quit()
except: except:
print('Failed to quit pygame') print('Failed to quit pygame')
if save_states: self._save()
self._save_state_list()
self._save_activation_list()
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)] automaton_state = [[0 for n in range(self.__dimension)] for m in range(self.__dimension)]
activation = 0 activation = 0
for coord, cell in self._cellular_automaton._current_state.items(): 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) activation = round(1-2*activation/self.__dimension**2, 6)
self.__activation_list.append(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): 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)
def _save_activation_list(self): def _save_activation(self):
if not os.path.exists(self.__path): if not os.path.exists(self.__path):
os.makedirs(self.__path) os.makedirs(self.__path)
@ -230,8 +242,11 @@ class Simulate2Layers:
def __init__(self, def __init__(self,
dim, dim,
eps, eps,
res=4, steps=100,
draw='pygame', draw='pygame',
res=4,
fps=30,
save=None,
path='/cloud/Public/_data/neuropercolation/test/', path='/cloud/Public/_data/neuropercolation/test/',
state_to_color_cb=None, state_to_color_cb=None,
*args, **kwargs): *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.__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.__save = save
self.__path = path self.__path = path
self.__state_list = [] self.__state_list = []
self.__activation_list = [] self.__activation_list = []
self.run(evolutions_per_second=fps, last_evolution_step=steps)
def run(self, def run(self,
evolutions_per_second=0, 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 :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
""" """
if save_states: self._track()
self._append_state()
else:
self._append_activation()
with contextlib.suppress(KeyboardInterrupt): with contextlib.suppress(KeyboardInterrupt):
while self.__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)
if save_states: self._track()
self._append_state()
else:
self._append_activation()
if self.__draw_engine is not None: if self.__draw_engine is not None:
time_ca_end = time.time() time_ca_end = time.time()
self._redraw_dirty_cells() self._redraw_dirty_cells()
@ -304,11 +315,15 @@ class Simulate2Layers:
except: except:
print('Failed to quit pygame') print('Failed to quit pygame')
if save_states: self._save()
self._save_state_list()
self._save_activation_list()
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)] automaton_state = [[[0 for n in range(self.__dimension)] for m in range(self.__dimension)] for l in range(2)]
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():
@ -332,14 +347,22 @@ 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.__activation_list.append(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): 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)
def _save_activation_list(self): def _save_activation(self):
if not os.path.exists(self.__path): if not os.path.exists(self.__path):
os.makedirs(self.__path) os.makedirs(self.__path)
@ -398,8 +421,11 @@ class Simulate4Layers:
dim, dim,
eps, eps,
coupling=[], coupling=[],
res=4, steps=100,
draw='pygame', draw='pygame',
res=4,
fps=30,
save=None,
path='/cloud/Public/_data/neuropercolation/test/', path='/cloud/Public/_data/neuropercolation/test/',
state_to_color_cb=None, state_to_color_cb=None,
*args, **kwargs): *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.__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.__save = save
self.__path = path self.__path = path
self.__state_list = [] self.__state_list = []
self.__activation_list = [] self.__activation_list = []
self.run(evolutions_per_second=fps, last_evolution_step=steps)
def run(self, def run(self,
evolutions_per_second=0, evolutions_per_second=0,
@ -441,17 +469,11 @@ class Simulate4Layers:
Warning: is blocking until finished Warning: is blocking until finished
""" """
with contextlib.suppress(KeyboardInterrupt): with contextlib.suppress(KeyboardInterrupt):
if save_states: self._track()
self._append_state()
else:
self._append_activation()
while self.__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)
if save_states: self._track()
self._append_state()
else:
self._append_activation()
if self.__draw_engine is not None: if self.__draw_engine is not None:
time_ca_end = time.time() time_ca_end = time.time()
self._redraw_dirty_cells() self._redraw_dirty_cells()
@ -470,11 +492,16 @@ class Simulate4Layers:
self.__draw_engine._pygame.quit() self.__draw_engine._pygame.quit()
except: except:
print('Failed to quit pygame') 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)] 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]]
for coord, cell in self._cellular_automaton._current_state.items(): 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] activation = [[round(1-2*act/self.__dimension**2, 6) for act in layer] for layer in activation]
self.__activation_list.append(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): 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}_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)
def _save_activation_list(self): def _save_activation(self):
if not os.path.exists(self.__path): if not os.path.exists(self.__path):
os.makedirs(self.__path) os.makedirs(self.__path)