diff --git a/neuropercolation/automaton.py b/neuropercolation/automaton.py index fa19304..c684d7f 100644 --- a/neuropercolation/automaton.py +++ b/neuropercolation/automaton.py @@ -84,8 +84,9 @@ class CellularAutomatonCreator(abc.ABC): class Neurolattice(CellularAutomatonCreator, abc.ABC): """ Cellular automaton with the evolution rules of conways game of life """ - def __init__(self, dim, eps, *args, **kwargs): + def __init__(self, dim, eps, init, *args, **kwargs): super().__init__(dimension=[dim, dim], + init=init, neighborhood=VonNeumannNeighborhood(EdgeRule.FIRST_AND_LAST_CELL_OF_DIMENSION_ARE_NEIGHBORS)) self._evolution_step = 0 self.epsilon = eps diff --git a/neuropercolation/display.py b/neuropercolation/display.py index 0dfac1a..c1965ab 100644 --- a/neuropercolation/display.py +++ b/neuropercolation/display.py @@ -24,6 +24,7 @@ import operator import collections import contextlib from typing import Sequence +import math as m #just for last_sync from . import Neurolattice, Neuropercolation, NeuropercolationCoupled @@ -102,7 +103,6 @@ class Simulate1Layer: 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, @@ -119,7 +119,7 @@ class Simulate1Layer: self._track() 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() self._cellular_automaton.evolve(evolutions_per_draw) self._track() @@ -275,7 +275,6 @@ class Simulate2Layers: 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,9 +458,9 @@ class Simulate4Layers: 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, evolutions_per_second=0, evolutions_per_draw=1, @@ -475,28 +474,34 @@ class Simulate4Layers: """ with contextlib.suppress(KeyboardInterrupt): 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) - self._track() + try: + 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() + if self.__draw_engine is not None: + time_ca_end = time.time() + self._redraw_dirty_cells() + time_ds_end = time.time() + self.print_process_info(evolve_duration=(time_ca_end - time_ca_start), + draw_duration=(time_ds_end - time_ca_end), + 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() + self._save() + + finally: if self.__draw_engine is not None: - time_ca_end = time.time() - self._redraw_dirty_cells() - time_ds_end = time.time() - self.print_process_info(evolve_duration=(time_ca_end - time_ca_start), - draw_duration=(time_ds_end - time_ca_end), - 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() - except: - print('Failed to quit pygame') - - self._save() + try: + self.__draw_engine._pygame.quit() + except: + print('Failed to quit pygame') + + + def _track(self): if self.__save == 'all': self._append_all() @@ -601,6 +606,6 @@ class Simulate4Layers: def __draw_cell_surface(self, surface_pos, cell_color): return self.__draw_engine.fill_surface_with_color((surface_pos, self.__cell_size), cell_color) - def print_process_info(self, evolve_duration, draw_duration, evolution_step, runlendig): + def print_process_info(self, evolve_duration, draw_duration, evolution_step, runlendig, extra=''): self.__draw_engine.fill_surface_with_color(((0, 0), (self.__rect.width, 30))) - self.__draw_engine.write_text((0, 5), f'Step: {evolution_step:>{runlendig}} FPS: {int(1/(evolve_duration+draw_duration))}') + self.__draw_engine.write_text((0, 5), f'Step: {evolution_step:>{runlendig}} FPS: {int(1/(evolve_duration+draw_duration))} {extra}')