A generalized n-dimensional cellular automata engine, using napari for visualisation.
automata.mp4
pip install napari-cellular-automata
Open napari
, then use the Initialize World
widget to create a new layer with the automaton, of your choice. Use Populate World
to randomize the initial state as you prefer. Then use the Run Automaton
widget to run the simulation. You can select multiple states to plot some stats as well.
You can also use this as a library:
from napari-cellular-automata.engine import populate_world_with_state, step_world, get_state_descriptions
from napari-cellular-automata.automata import AUTOMATA
import numpy as np
pyro = AUTOMATA['Pyroclastic']
world = np.zeros((100, 100, 100), dtype=np.uint8)
populate_world_with_state(world, 9, blob_number=1)
states = get_state_descriptions(pyro)
while True:
uniq, counts = np.unique(world, return_counts=True)
for state, count in zip(uniq, counts):
print(f'{states[state]}: {count}')
world = step_world(world, pyro, wrap=False, edge_value=10)
In the generalized approach, rules are defined by transition dictionaries that encode how each state changes into a different state, plus some extra info such as color and state name for visualisation. This implementation is based on a series of videos by tsoding. Original typescript source code: https://github.com/tsoding/autocell.
For example, Conway's Game of Life can be defined as follows:
[
{ # state 0 (dead)
'transitions': {
# if this cell has 3 neighbours of state 1 (alive), become state 1
# -1 means "any number" (only relevant with more than 2 states)
(-1, 3): 1,
},
# if none of the rules above apply, become/remain the "default" state 0 (dead)
'default': 0,
# visualisation parameters
'color': 'transparent',
'name': 'dead',
},
{ # state 1 (alive)
'transitions': {
# if this cell has 2 or 3 neighbours of state 1 (alive), remain state 1
(-1, 2): 1,
(-1, 3): 1,
},
# if none of the rules above apply, become the "default" state 0 (dead)
'default': 0,
'color': 'green',
'name': 'alive',
},
]
A second, more limited (but smaller and faster) engine is based on the Survival-Birth rules; neighbour types do not matter, just their number. Other than dead and alive, any number of decay states can be added, which simply "delay" the time of death of a cell. The Game of Life
is defined as:
{
# number of non-dead neighbours necessary for survival
'survival': [2, 3],
# number of non-dead neighbours necessary for birth
'birth': [3],
# number of total states (including alive, dead, and decay)
'states': 2,
}
or, in short form: '2-3/3/2'
.
You can add a custom automaton by simply adding to the AUTOMATA
dictionary:
from napari-cellular-automata.engine import rules_from_survival_birth
from napari-cellular-automata.automata import AUTOMATA
AUTOMATA['My rules'] = {
'ndim': 2,
'mode': 'survival-birth',
'rules': rules_from_survival_birth('2-3,5/3/4'),
}
This napari plugin was generated with Cookiecutter using @napari's cookiecutter-napari-plugin template.
You can install napari-cellular-automata
via pip:
pip install napari-cellular-automata
To install latest development version :
pip install git+https://github.com/brisvag/napari-cellular-automata.git
Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.
Distributed under the terms of the GNU GPL v3.0 license, "napari-cellular-automata" is free and open source software
If you encounter any problems, please file an issue along with a detailed description.