dynpy.ca - Cellular automata module

Module implementing Cellular automaton dynamical systems.

class dynpy.ca.CellularAutomaton(num_vars, num_neighbors, rule, mode='RULENUMBER', dim=1)[source]

Bases: BooleanNetwork

Cellular automaton object. Constructs an underlying dynpy.bn.BooleanNetwork on a lattice and with a homogenous update function. Implements periodic boundary conditions.

For example:

>>> from dynpy.ca import CellularAutomaton
>>> import numpy as np
>>> ca = CellularAutomaton(num_vars=50, num_neighbors=1, rule=110)
>>> init_state = np.zeros(ca.num_vars, dtype='uint8')
>>> init_state[int(ca.num_vars/2)] = 1
>>> for line in ca.get_trajectory(init_state, 10):
...   print("".join('#' if e == 1 else '-' for e in line))
-------------------------#------------------------
------------------------##------------------------
-----------------------###------------------------
----------------------##-#------------------------
---------------------#####------------------------
--------------------##---#------------------------
-------------------###--##------------------------
------------------##-#-###------------------------
-----------------#######-#------------------------
----------------##-----###------------------------
Parameters:
num_varsint or list

The number of cells in the automaton (i.e. the size of the automaton). If dim > 1, then this must be list indicating the number of variables in each dimension.

num_neighborsint

Number of neighbors in each direction that the update rule depends on.

ruleint or list

If mode is ‘RULENUMBER’, then this should be the update rule, specified as a number representing the truth table of each node. If mode is ‘TRUTHTABLE’, this should be a list specifying the truthtable.

mode{‘RULENUMBER’,’TRUTHTABLE’,’FUNC’} (default ‘RULENUMBER’)

How the rules parameter should be interpreted.

dimint (default 1)

Dimensionality of cellular automata lattice.

discrete_time = True

Whether the dynamical system obeys discrete- or continuous-time dynamics

get_attractor_basins(sort=False, start_state_iter=None)

Computes the attractors and basins of the current discrete-state dynamical system.

Parameters:
sortbool, optional

Whether to sort attractors and basin states (slower).

start_state_iteriterator, optional

Iterator to indicate which start-states to start from. If not specified, tries all states.

Returns:
basin_attslist of lists

A list of the the attractor states for each basin (basin order is from largest basin to smallest).

basin_stateslist of lists

A list of all the states in each basin (basin order is from largest basin to smallest).

get_ndx2state_map()
get_ndx2state_mx()
get_state2ndx_map()
get_structural_graph()

Get graph of strutural connectivity

Returns:
numpy array

Adjacency matrix representing which variables have inputs from which other variables

get_trajectory(start_state, max_time, num_points=None, logscale=False)

This method get a trajectory of a dynamical system starting from a particular starting state.

Parameters:
start_stateobject

Which state to start from

max_timefloat

Until which point to run the dynamical system (number of iterations for discrete-time systems or time limit for continuous-time systems)

num_pointsint, optional

How many timepoints to sample the trajectory at. This determines how big each ‘step size’ is. By default, equal to int(max_time)

logscalebool, optional

Whether to sample the timepoints on a logscale or not (default)

Returns:
trajectory: numpy array

Array of states corresponding to trajectory

iterate(start_state, max_time)

This method runs the dynamical system for max_time starting from start_state and returns the result. In fact, this method is set at run-time by the constructor to either _iterateDiscrete or _iterateContinuous depending on whether the dynamical system object is initialized with discrete_time=True or discrete_time=False. Thus, sub-classes should override _iterateDiscrete and _iterateContinuous instead of this method. See also dynpy.dynsys.DynamicalSystem.iterateOneStep()

Parameters:
start_statenumpy array or scipy.sparse matrix

Which state to start from

max_timefloat

Until which point to run the dynamical system (number of iterations for discrete-time systems or time limit for continuous-time systems)

Returns:
numpy array or scipy.sparse matrix

End state

iterate_1step(start_state)

This method runs a discrete-time dynamical system for 1 timestep. At run-time, the construct either repoints this method to _iterateOneStep for discrete-time systems, or removes it for continuous time systems.

Parameters:
start_statenumpy array or scipy.sparse matrix

Which state to start from

Returns:
numpy array or scipy.sparse matrix

Iterated state

num_vars = None

The number of variables in the dynamical system

print_attractor_basins()

Prints the attractors and basin of the dynamical system

>>> import dynpy
>>> rules = [ ['a',['a','b'],[1,1,1,0]],['b',['a','b'],[1,0,0,0]]]
>>> bn = dynpy.bn.BooleanNetwork(rules=rules)
>>> bn.print_attractor_basins()
* BASIN 1 : 2 States
ATTRACTORS:
      a      b
      1      0
--------------------------------------------------------------------------------
* BASIN 2 : 1 States
ATTRACTORS:
      a      b
      0      0
--------------------------------------------------------------------------------
* BASIN 3 : 1 States
ATTRACTORS:
      a      b
      1      1
--------------------------------------------------------------------------------
rules = None

The provided definition of the Boolean network

states()

Returns list of all possible states occupied by system.

property var_name_ndxs

A mapping from variables names to their indexes

var_names

The names of the variables in the dynamical system