dynpy.bn - Boolean network module

Module implementing Boolean Network classes

class dynpy.bn.BooleanNetwork(rules, mode=None, convert_to_truthtable=True)[source]

Bases: DiscreteStateVectorDynamicalSystem, DeterministicDynamicalSystem

A class for Boolean Network dynamical systems.

The network is specified using the rules parameter, which is a list with one element for each Boolean variable. Each of these elements is itself a list, containing 3 parts:

  • The name of the current variable

  • The names of the variables that have inputs to the current variable

  • The dynamical update rule for this variable

The dynamical update rules can be passed in in two ways. If the mode parameter is equal to ‘TRUTHTABLES’ (the default), then it should be a truth table, represented as a list of 0s and 1s. The first element of this list corresponds to the desired output when all the inputs are on (i.e., are all 1s), while the last element of this list corresopnds to the desired output when all the inputs are off. For example:

>>> #      output when         1 1 0 0
>>> #        inputs are:       1 0 1 0
>>> r = [ ['x1', ['x1','x2'], [1,0,0,0]],
...       ['x2', ['x1','x2'], [1,1,1,0]] ]
>>> import dynpy
>>> bn1 = dynpy.bn.BooleanNetwork(rules = r)

The other way to pass in the dynamical update rules is to set the mode parameter to ‘FUNCS’, and specify the update rule of each variable as a Python function that takes in the inputs as arguments:

>>> r = [ ['x1', ['x1','x2'], lambda x1,x2: (x1 and x2) ],
...       ['x2', ['x1','x2'], lambda x1,x2: (x1 or  x2) ] ]
>>> import dynpy
>>> bn1 = dynpy.bn.BooleanNetwork(rules=r, mode='FUNCS')
Parameters:
ruleslist

The definition of the Boolean network, as described above

mode{‘TRUTHTABLES’,’FUNCS’}, optional

Specifies how the update functions are defined (as truthtables or as Python functions). By default tries to guess

convert_to_truthtablebool, optional

If update functions are passed in as Python, whether to convert them to truthtables. This has initial performance penalty but then leads to faster iterations.

rules = None

The provided definition of the Boolean network

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_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
--------------------------------------------------------------------------------
states()[source]

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

get_structural_graph()[source]

Get graph of strutural connectivity

Returns:
numpy array

Adjacency matrix representing which variables have inputs from which other variables