dynpy.graphdynamics - Module for implementing dynamical systems on graphs¶
Module which implements dynamical systems on graph
- class dynpy.graphdynamics.RandomWalkerBase(graph)[source]¶
Bases:
DiscreteStateVectorDynamicalSystem
,StochasticDynamicalSystem
- iterate()[source]¶
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
- 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_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 --------------------------------------------------------------------------------
- property var_name_ndxs¶
A mapping from variables names to their indexes
- var_names¶
The names of the variables in the dynamical system
- class dynpy.graphdynamics.RandomWalkerEnsemble(graph, discrete_time=True, issparse=True)[source]¶
Bases:
MarkovChain
This intializes a stochastic dynamical system representing a random walker on a graph.dynpy.sample_nets.karateclub_net
- Parameters:
- graphnumpy array
Matrix representing the adjacency or weighted connectivity of the underlying graph
- discrete_timebool, optional
Whether walker should follow discrete (default) or continuous time dynamics. Only discrete time dynamics are supported for individual walkers, though a distribution of walkers created using the
dynpy.dynsys.MarkovChain
supports both.- issparsebool, optional
Whether to use a sparse or dense transition matrix.
- discrete_time = True¶
Whether the dynamical system obeys discrete- or continuous-time dynamics
- classmethod from_deterministic_system(base_sys, issparse=True)¶
Alternative constructor creates a a Markov Chain from the transitions of an underlying deterministic system. It maintains properties of the underlying system, such as the sparsity of the state transition matrix, and whether the system is discrete or continuous-time. The underlying system must be an instance of
dynpy.dynsys.DeterministicDynamicalSystem
anddynpy.dynsys.DiscreteStateDynamicalSystem
.For example, for a Boolean network:
>>> import dynpy >>> yeast = dynpy.sample_nets.budding_yeast_bn >>> bn = dynpy.bn.BooleanNetwork(rules=yeast) >>> bnEnsemble = dynpy.markov.MarkovChain.from_deterministic_system(bn, issparse=True) >>> init = bnEnsemble.get_uniform_distribution() >>> trajectory = bnEnsemble.get_trajectory(init, max_time=80)
If we wish to project the state of the Markov chain back onto the activations of the variables in the underlying system, we can use the ndx2state_mx matrix. For example:
>>> import dynpy >>> import numpy as np >>> yeast = dynpy.sample_nets.budding_yeast_bn >>> bn = dynpy.bn.BooleanNetwork(rules=yeast) >>> bn_ensemble = dynpy.markov.MarkovChain.from_deterministic_system(bn, issparse=True) >>> init = bn_ensemble.get_uniform_distribution() >>> final_state = bn_ensemble.iterate(init, max_time=80) >>> print(np.ravel(final_state.dot(bn.get_ndx2state_mx()))) [0. 0.05664062 0.07373047 0.07373047 0.92236328 0. 0. 0. 0.91503906 0. 0. ]
- Parameters:
- base_sysobject
Dynamical system over whose states the Markov chain will be defined
- issparsebool, optional
Whether transition matrix should be in sparse or dense matrix format
- get_equilibrium_distribution()¶
Get equilibrium state (i.e. the stable, equilibrium distribution) for this dynamical system. Uses eigen-decomposition.
- Returns:
- numpy array or scipy.sparse matrix
Equilibrium distribution
- 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
- get_uniform_distribution()¶
Return uniform starting distribution over all system states.
- 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
- property num_states¶
- num_vars = None¶
The number of variables in the dynamical system
- project(keep_vars, initial_dist=None)¶
Create a Markov chain by projecting an existing Markov chain over a multivariate dynamical system onto a subset of those variables.
For example:
>>> import dynpy >>> r = [ ... ['x1', ['x1','x2'], lambda x1,x2: (x1 and x2) ], ... ['x2', ['x1','x2'], lambda x1,x2: (x1 or x2) ], ... ] >>> bn = dynpy.bn.BooleanNetwork(rules=r, mode='FUNCS') >>> bnensemble = dynpy.markov.MarkovChain.from_deterministic_system(bn, issparse=False) >>> proj = dynpy.markov.MarkovChain.project(bnensemble, [0]) >>> print(proj.transition_matrix) [[1. 0. ] [0.5 0.5]]
- Parameters:
- keep_varslist
List of variables to keep
- initial_distoptional
Marginalize using this distribution for starting conditions
- transition_matrix = None¶
Transition matrix for linear system.
- property var_name_ndxs¶
A mapping from variables names to their indexes
- var_names¶
The names of the variables in the dynamical system