pydynamo.core package

Submodules

pydynamo.core.delays module

Functions implementing a delayed smooth effect in pydynamo.

class pydynamo.core.delays.Delay3(dt)

Bases: object

Delay function of the 3rd order. Returns a class that is callable as a function (see Call parameters) at a given step k.

Parameters

dtfloat

time step.

Call parameters

delay_timefloat

delay parameter. Higher delay increases smoothing.

val_kfloat

current value to delay

kint

current loop index.

ddt(value, delay_time)
initialise(init_value, delay_time)
pydynamo.core.delays.Dlinf3

alias of Delay3

class pydynamo.core.delays.Smooth(dt)

Bases: object

Delay information function of the 1st order for smoothing. Returns a class that is callable as a function (see Call parameters) at a given step k.

Parameters

dtfloat

time step.

Call parameters

delay_timefloat

delay parameter. Higher delay increases smoothing.

valfloat

current value to smooth

kint

current loop index.

ddt(value, delay_time)
initialise(init_value)

pydynamo.core.graph module

Functions of System which handles networks.

pydynamo.core.graph.assert_cst_acyclic(self)

Assert that the constant setting graph is acyclic.

pydynamo.core.graph.assert_init_acyclic(self)

Assert that the initialisation graph is acyclic, and print the cycle in case there is some.

pydynamo.core.graph.assert_update_acyclic(self)

Assert that the updating graph is acyclic, and print the cycle in case there is some.

pydynamo.core.graph.get_cst_graph(self)

Get the graph of influences for constants: an arrow from constant A to constant B if B needs A to be computed.

Returns

networkx.DiGraph

Graph of constant influences.

pydynamo.core.graph.get_influence_graph(self)

Get the graph of influences: an arrow from A to B if B needs A (at initialisation or updating step) to be computed.

Returns

networkx.DiGraph

Graph of influences.

pydynamo.core.graph.get_init_graph(self)

Get the graph of influences for variables at initialisation: an arrow from variable A to variable B if B needs A to be computed.

Returns

networkx.DiGraph

Graph of variable initialisation influences.

pydynamo.core.graph.get_update_graph(self)

Get the graph of influences for variables and their indices at updating step: an arrow from variable (A, i) to variable (B, k) if (B, k) needs (A, i) to be computed.

Returns

networkx.DiGraph

Graph of variable influences at updating step.

pydynamo.core.graph.get_update_graph_quotient(self)

Get the graph of influences for variables at updating step: an arrow from variable A to variable B if B needs A to be computed.

Returns

networkx.DiGraph

Graph of variable influences at updating step.

pydynamo.core.graph.set_order(self, gtype)

Returns the order to set constants, intitialize or update variables.

Parameters

gtypestr

Type of graph, either ‘cst’, ‘init’, or ‘update’.

pydynamo.core.parse_dynamo_functions module

Functions used to get appropriate parameters in case a pydnamo “special function” (smooth, tabhl …) is read in an equation.

pydynamo.core.parse_dynamo_functions.change_and_get_params(node, node_name)

Get information needed to call the special function in the node, and also change the node to include appropriate parameter names.

Parameters

nodeast.Module

Function call to handle.

node_namestr

Name of the node (variable or constant) which uses this function to be upated. This name is useful to determine the new name of the function (es: tabhl_io for the funciton tabhl and the updated variable io.

Returns

dict

All useful information to generate the special function. Depends on the type of the function.

pydynamo.core.parse_dynamo_functions.get_dynamo_fun_params(root, node_name)

Get information needed to execute the equation node, and also change node with appropriate parameters.

Parameters

nodeast.Module

Equation.

node_namestr

Name of the node (variable or constant) which uses this function to be upated. This name is useful to determine the new name of the function (es: tabhl_io for the funciton tabhl and the updated variable io.

Returns

(ast.Module, dict)

The modified node and all useful information to generate the equation.

pydynamo.core.parse_equations module

Functions used to read and parse equations written with a pydynamo syntax. There is functions to get the equation type (constant, update, initialisation) and the arguments (variables, constants, indices).

pydynamo.core.parse_equations.change_points_to_underscores(line, variables)

Change the points of indices k and j to underscores.

Parameters

linestr

Equation to change.

variablesiterable((str, str))

List of (variable name, index name) in the equation.

Returns

str

Equation changed.

pydynamo.core.parse_equations.get_pars_cst_eq(line)

Get parameters of an equation if it’s an constant equation.

Parameters

linestr

Equation.

Returns

(str, dict)

Tuple with the name of the constant, and a dictionnary containing the arguments and the new equation line.

pydynamo.core.parse_equations.get_pars_eq(root, eq_type)

Get parameters of an equation.

Parameters

rootast.Module

Equation.

eq_typestr

Equation type, one of ‘cst’, ‘init’, ‘update’.

Returns

(str, dict)

Variable or constant name, and parameters.

pydynamo.core.parse_equations.get_pars_init_eq(line)

Get parameters of an equation if it’s an initialisation equation.

Parameters

linestr

Equation.

Returns

(str, dict)

Tuple with the name of the initialized variable, and a dictionnary containing the arguments and the new equation line.

pydynamo.core.parse_equations.get_pars_update_eq(line)

Get parameters of an equation if it’s an update equation.

Parameters

linestr

Equation.

Returns

(str, dict)

Tuple with the name of the updated variable, and a dictionnary containing the arguments and the new equation line.

pydynamo.core.parse_equations.get_var_cst_fun(root)

Get every constants, variables and special functions contained in the equation.

Parameters

rootast.Module

Equation.

Returns

dict(str: set)

A dictionnary which contains the set of constants, variables and functions names.

{‘cst’: set(str), ‘var’: set((str, str)), ‘fun’: dict(str: {‘args’: list(str), ‘type’: str, ‘fun’: str})}

pydynamo.core.parse_equations.is_cst_eq(root)

Determines if a parsed line is an equation for a constant or not.

Parameters

rootast.Module

Equation.

pydynamo.core.parse_equations.is_eq(root)

Determines if a parsed line is an equation or not.

Parameters

rootast.Module

Equation.

Returns

pydynamo.core.parse_equations.is_eq_of_type(root, eq_type)

Determines if an equation is of a certain type (‘cst’, ‘init’, ‘update’).

Parameters

rootast.Module

Equation.

eq_typestr

Equation type, one of ‘cst’, ‘init’, ‘update’.

pydynamo.core.parse_equations.is_init_eq(root)

Determines if an equation for a variable is an initialisation equation or not.

Parameters

rootast.Module

Equation.

pydynamo.core.parse_equations.is_update_eq(root)

Determines if an equation for a variable is an update equation or not.

Parameters

rootast.Module

Equation.

pydynamo.core.parse_equations.is_variable_eq(root)

Determines if a parsed line is an equation for a variable or not.

Parameters

rootast.Module

Equation.

pydynamo.core.parse_equations.reformat_eq(root, variables)

Format a parsed equation to a string equation.

Parameters

rootast.Module

Equation.

variablesiterable((str, str))

List of (variable name, index name) in the equation.

Returns

str

Equation on string format.

pydynamo.core.parse_system module

Functions to parse an entire pydynamo code and generate a System object. Also defines every political changes.

pydynamo.core.parse_system.check_file(filename)

Check if every line in the file can be parsed by the ast module. If not, raise an error. Parameters ———- filename : str

Files in which every pydynamo equations are written.

pydynamo.core.parse_system.comment_from_equation(line)

Retrieve comment by removing ‘#’ and additional spaces.

pydynamo.core.parse_system.get_system_dicts(lines)

Parameters

linesiterable(str)

List of every equations.

Returns

(dict, dict, dict)

Nodes (variable, constants and functions), equations (constant, update and initialisation) and comments parsed in the equations list.

pydynamo.core.parse_system.list_from_file(filename, s=None, prepare=True)

Get a list of equatinos from a file with pydynamo equations. Parameters ———- filename : str

Files in which every pydynamo equations are written.

pydynamo.core.parse_system.list_from_function(fun, s=None, prepare=True)

Get a list of equations from a function which lists pydynamo equations.

Parameters

funfunction

Function in which every pydynamo equations are written.

Examples

Just write pydynamo equations inside a function, and create a System with it: >>> def custom_equations(): >>> pop.i = 100 >>> pop.k = pop.j /2 # Population >>> list_of_equations = list_from_function(custom_equations)

pydynamo.core.plot_system module

Functions used by a System instance to plot curves.

pydynamo.core.plot_system.plot(self, v_names=None, rescale=False, show_comments=True, filter_no=None, scales=None, colors=None, title='', linestyle='-', outside_legend_number=2, legend=True, **kwargs)

Plot the curves of the last simulation for indicated variables.

Parameters

v_namesiterable(str)

Variable to plot names. If None, all variables are plotted.

rescalebool

If yes, all curves are normalized between 0 and 1.

show_comments: bool

If yes, comments are shown in the legend.

filter_no: iterable(str)

Names of variables that won’t appear in the plot.

scales: dict(str, float)

Scales of variables. Variables are divided by their respective scales on the plot.

colors: dict(str, str)

Colors of each variable.

title: str

Title of the plot.

linestyle: str

Linestyle of the plot.

outside_legend_number: int

Number of lines from which legend is plotted outside the graph.

legend: bool

If yes, the legend is drawn.

**kwargs

Arguments for matplotlib.pyplot.plt

Returns

list(matplotlib.lines.Line2D)

List of plotted lines

pydynamo.core.plot_system.plot_compare(self, s2, v_names, scales=None, rescale=False, *args, **kwargs)

Show the variables of 2 different systems.

s2: System

Other system to compare whith.

v_names: iterable(str)

Names of variables or constant to plot.

scales: dict(str, float)

Scales of variables. Variables are divided by their respective scales on the plot.

rescale: bool

If yes, If yes, variables are normalized between 0 and 1.

*args

Argument list for the pydynamo.core.plot_system.plot function.

**kwargs

Arguments for the pydynamo.core.plot_system.plot function.

Returns

(list(matplotlib.lines.Line2D), list(matplotlib.lines.Line2D))

First and second compared lines.

pydynamo.core.plot_system.plot_non_linearity(self, var, **kwargs)

Plot the non linear functions with which the variable is computed.

Parameters

namestr

Variable name.

**kwargs

Arguments for matplotlib.pyplot.plot

Returns

matplotlib.lines.Line2D

Plotted line

pydynamo.core.plot_system.show_influence_graph(self, variables=None, depth=1, show_init_val=True, in_notebook=True, options=None, colors=None)

Show variables influence newtork with the Pyvis library.

Parameters

show_init_valbool

If True, show the initial value for a variable.

in_notebookbool

If True, network appears as a Widget in the notebook.

optionsdict

Pyvis options.

colorsdict

Colors of each variable and constant.

pydynamo.core.politics module

Functions to add new politics in a System object.

pydynamo.core.politics.new_cst_politic(self, cst, date, new_value)

Add a new equations to s for the constant cst, which becomes a variable changing from its former value to new_value after the date.

Parameters

sSystem

The system to implement the new politic.

cststr

Constant name.

datefloat

Date of politic application.

new_valuefloat

The new value that cst will take after the date date.

pydynamo.core.politics.new_politic(self, name, date, new_val)

Implements a new politic for some constant, table or variable from a certain date. PLEASE DO NOT ADD 2 NEW POLITICS ON THE SAME VARIABLE OR CONSTANT !

Parameters

name: str

Name of a constant, table or variable we want to change.

date: float

date from which the new value will be activated.

new_val: float, array or string

If name refers to a constant, a float with the new value. If name srefers to a table, an array of the sime size as the older one. If name refers to a variable, a string with the new value.

pydynamo.core.politics.new_table_politic(self, var, date, new_value)

Add a new equations to s for the table used by the variable var. The table changes from its former value to new_value after the date.

Parameters

sSystem

The system to implement the new politic.

varstr

Variable name.

datefloat

Date of politic application.

new_valuelist(float)

The new value that the table will take after the date date.

pydynamo.core.politics.new_var_politic(self, var, date, eq2)

Add a new equations to s for the variable var, which changes from its former value to new_value after the date.

Parameters

sSystem

The system to implement the new politic.

varstr

Variable name.

datefloat

Date of politic application.

new_valuestr

The new value, written with the pydynamo syntax, that the variable var will take after the date date.

pydynamo.core.psdsystem module

System class which creates a System from a Pysd model.

class pydynamo.core.psdsystem.PsdSystem(model)

Bases: System

System which initialises with a Pysd model.

equation(name)

Returns the reformatted equation of a variable or constant.

Parameters

nodestr

Name of the variable or constant to show the equation.

get_influence_graph()

Get the graph of influences: an arrow from A to B if B needs A (at initialisation or updating step) to be computed.

Returns

networkx.DiGraph

Graph of influences.

get_tabhl_args(name)

Get indications about a tabhl function.

Parameters

name: str

Name of the variable using a tabhl function.

Returns

np.array, np.array, str, str, str:

x, f(x), x label, y label, title

get_time()

Returns the time array.

Returns

np.array(float):

Array of system time.

get_var(name)

Get the variable array.

Parameters

name: str

Name of variable.

Returns

np.array(float):

Array of values of the variable for the last run.

get_var_names()

Return every variables names.

Returns

list(str)

List of variables names.

run()

Run PySD model.

pydynamo.core.specials module

Special functions that are used in pydynamo equations.

class pydynamo.core.specials.Interpol(x_low, x_high, x_incr, table)

Bases: object

Custom interpolate, because scipy’s one is slow and overkill

class pydynamo.core.specials.Sample(isam, time)

Bases: object

When called, returns a the last stored value unless some intervals is reached.

pydynamo.core.specials.clip(func2, func1, t, t_switch)

Logical function used as time switch to change parameter value.

Parameters

func2 : any

func1 : any

tfloat

current time value.

t_switchfloat

time threshold.

Returns

func2 if t>t_switch, else func1.

pydynamo.core.specials.step(hght, sttm, t)

If sttm is a variable (ndarray), th step time is determined by the method described in section 2.4.2. If hght is a variable, the step function has the effect of a gate function that “opens” the gate allowwing step to equal hght after time sttm. The step will equal 0 until the step time (and any time that time is less than sttm).

step = 0 if TIME < sttm step = hght otherwise

Parameters

hght: float

sttm: float:

tfloat

current time value.

pydynamo.core.system module

Define the System class, base class to simulate system dynamics from equations.

class pydynamo.core.system.System(code=None, prepare=True)

Bases: object

Base class for system dynamics.

A System stores 3 dictionnaries that contains:
  • nodes (constants, variables, and functions)

  • equations (constant values, updating equations and initialisation equations)

  • comments about nodes.

From this dictionnaries, it generates the updating pattern and run the simulation.

add_comments(comments)

Add comments to the System.

Parameters

commentsdict(str: str)

Each node name and its comment.

add_equations(new_code_lines)

Add new equations to the older ones. In case there is a conflict for a same variable or constant, the last equation only is remembered.

Parameters

new_code_lineslist(str)

Pydynamo equations to add.

assert_cst_acyclic()

Assert that the constant setting graph is acyclic.

assert_cst_defined()

Assert that every constant has an equation to be computed.

assert_init_acyclic()

Assert that the initialisation graph is acyclic, and print the cycle in case there is some.

assert_init_defined()

Assert that every variable has an initialisation or updated equation to be computed.

assert_update_acyclic()

Assert that the updating graph is acyclic, and print the cycle in case there is some.

assert_update_defined()

Assert that every variable has an updating equation to be computed.

change_functions_in_dict()

In case some functions are re-defined by user, modify dependencies in parameters.

clip(v2, v1, t, y)

Clip function. See specials.clip.

copy(prepare=True)

Returns a copy of the model, with

Parameters

preparebool

If yes, prepare the system to run.

definition(node)

Get the definition of a node.

Parameters

nodestr

Name of the variable or constant to get the definition.

equation(node)

Returns the reformatted equation of a variable or constant.

Parameters

nodestr

Name of the variable or constant to show the equation.

generate_all_vars(N)

Initialise an empty array for every variable.

Parameters

Nint

Size of simulation.

generate_var(var, N)

Initialise an empty array for a variable.

Parameters

varstr

Variable name.

Nint

Size of simulation.

get_all_variable_names()

Get the set of all variables.

Returns

set(str):

Set of names of all variables

get_at(var, t)

Returns the value of var at time t, or an interpolation if between rwo timestep values.

Parameters

varstr

Variable name.

tfloat

Time.

get_cst_graph()

Get the graph of influences for constants: an arrow from constant A to constant B if B needs A to be computed.

Returns

networkx.DiGraph

Graph of constant influences.

get_cst_val(cst, args)

Get the value of a constant according to its equation and arguments.

Parameters

cststr

Name of the constant.

argsdict(str, value)

Values of each arguments.

get_different_csts()

Returns every variable which value is different than in the equations and their, abnd its new value.

Returns

dict(str: (float, float))

Dictionnary with each name, and its corresponding old and new value.

get_in_nodes(node, with_definitions=False)

Returns the list of the nodes that this node needs to be computed.

Parameters

node: str

Name of the node

with_definitions: bool

If yes, returns a dictionnary with each node definition.

get_influence_graph()

Get the graph of influences: an arrow from A to B if B needs A (at initialisation or updating step) to be computed.

Returns

networkx.DiGraph

Graph of influences.

get_init_graph()

Get the graph of influences for variables at initialisation: an arrow from variable A to variable B if B needs A to be computed.

Returns

networkx.DiGraph

Graph of variable initialisation influences.

get_out_nodes(node, with_definitions=False)

Returns the list of the nodes using the node to be computed.

Parameters

node: str

Name of the node

with_definitions: bool

If yes, returns a dictionnary with each node definition.

get_tabhl_args(name)

Get indications about a tabhl function.

Parameters

name: str

Name of the variable using a tabhl function.

Returns

np.array, np.array, str, str, str:

x, f(x), x label, y label, title

get_time()

Returns the time array.

Returns

np.array(float):

Array of system time.

get_update_graph()

Get the graph of influences for variables and their indices at updating step: an arrow from variable (A, i) to variable (B, k) if (B, k) needs (A, i) to be computed.

Returns

networkx.DiGraph

Graph of variable influences at updating step.

get_var(name)

Get the variable array.

Parameters

name: str

Name of variable.

Returns

np.array(float):

Array of values of the variable for the last run.

get_var_names()

Return every variables names.

Returns

list(str)

List of variables names.

is_initialized(var)

Indicates if a var is initialized or not.

Parameters

varstr

Variable name.

is_table(name)

Indicates if a name is a table.

Parameters

namestr

Name of the node.

new_cst_politic(cst, date, new_value)

Add a new equations to s for the constant cst, which becomes a variable changing from its former value to new_value after the date.

Parameters

sSystem

The system to implement the new politic.

cststr

Constant name.

datefloat

Date of politic application.

new_valuefloat

The new value that cst will take after the date date.

new_politic(name, date, new_val)

Implements a new politic for some constant, table or variable from a certain date. PLEASE DO NOT ADD 2 NEW POLITICS ON THE SAME VARIABLE OR CONSTANT !

Parameters

name: str

Name of a constant, table or variable we want to change.

date: float

date from which the new value will be activated.

new_val: float, array or string

If name refers to a constant, a float with the new value. If name srefers to a table, an array of the sime size as the older one. If name refers to a variable, a string with the new value.

new_table_politic(var, date, new_value)

Add a new equations to s for the table used by the variable var. The table changes from its former value to new_value after the date.

Parameters

sSystem

The system to implement the new politic.

varstr

Variable name.

datefloat

Date of politic application.

new_valuelist(float)

The new value that the table will take after the date date.

new_var_politic(var, date, eq2)

Add a new equations to s for the variable var, which changes from its former value to new_value after the date.

Parameters

sSystem

The system to implement the new politic.

varstr

Variable name.

datefloat

Date of politic application.

new_valuestr

The new value, written with the pydynamo syntax, that the variable var will take after the date date.

plot(v_names=None, rescale=False, show_comments=True, filter_no=None, scales=None, colors=None, title='', linestyle='-', outside_legend_number=2, legend=True, **kwargs)

Plot the curves of the last simulation for indicated variables.

Parameters

v_namesiterable(str)

Variable to plot names. If None, all variables are plotted.

rescalebool

If yes, all curves are normalized between 0 and 1.

show_comments: bool

If yes, comments are shown in the legend.

filter_no: iterable(str)

Names of variables that won’t appear in the plot.

scales: dict(str, float)

Scales of variables. Variables are divided by their respective scales on the plot.

colors: dict(str, str)

Colors of each variable.

title: str

Title of the plot.

linestyle: str

Linestyle of the plot.

outside_legend_number: int

Number of lines from which legend is plotted outside the graph.

legend: bool

If yes, the legend is drawn.

**kwargs

Arguments for matplotlib.pyplot.plt

Returns

list(matplotlib.lines.Line2D)

List of plotted lines

plot_compare(s2, v_names, scales=None, rescale=False, *args, **kwargs)

Show the variables of 2 different systems.

s2: System

Other system to compare whith.

v_names: iterable(str)

Names of variables or constant to plot.

scales: dict(str, float)

Scales of variables. Variables are divided by their respective scales on the plot.

rescale: bool

If yes, If yes, variables are normalized between 0 and 1.

*args

Argument list for the pydynamo.core.plot_system.plot function.

**kwargs

Arguments for the pydynamo.core.plot_system.plot function.

Returns

(list(matplotlib.lines.Line2D), list(matplotlib.lines.Line2D))

First and second compared lines.

plot_non_linearity(var, **kwargs)

Plot the non linear functions with which the variable is computed.

Parameters

namestr

Variable name.

**kwargs

Arguments for matplotlib.pyplot.plot

Returns

matplotlib.lines.Line2D

Plotted line

prepare()

Assert that all equations are well defined, ant that the updating graph is acyclic. Also set all updating functions and constants.

raw_equation(node)

Returns the pydynamo raw equation of a variable or constant.

Parameters

nodestr

Name of the variable or constant to get the raw pydynamo equation.

reset_eqs(prepare=True)

Set all nodes, equations and comments.

run(N=None, dt=1)

After preparing and before running, an user can redefine some constants, or assign update or init functions, with the following syntax: >>> system.c = 1 # New value for constant c >>> # New function to update v1 >>> # using variable v1 at index j and constant c1: >>> def new_update(v2_j, c1):

“””Variable 2 documentation”””

>>>    return v2_j + c1
>>> system.update_v1 = new_update # New fct for updating v1
>>> s.run(10, 1) # Run with new parameters
set_all_csts()

Set every constant constant according to its equation and arguments ONLY IF the constant is not set yet.

set_all_funs()

For each equation, set the appropriate function to the System.

set_all_special_functions()

Generate every special dynamo functions that are stored. Constants (especially tables and delays) have to be initialized before.

set_cst(cst, args)

Set a constant according to its equation and arguments.

Parameters

cststr

Name of the constant.

argsdict(str, value)

Values of each arguments.

set_cst_fun_from_dict(node)

For each constant equation, set the appropriate function to the System.

set_fun(node, fun_name, args, line)

Set an updating, initialisation or constant setting function to the System. It evaluates a lambda function with the line equation inside.

Parameters

nodestr

Constant or variable name.

fun_namestr

Name of function

argsiterable(str)

Arguments of the function.

linestr

Formula with wich the node is computed (set, initialised orupdated).

set_init_fun_from_dict(node)

For each initialisation equation, set the appropriate function to the System.

set_order(gtype)

Returns the order to set constants, intitialize or update variables.

Parameters

gtypestr

Type of graph, either ‘cst’, ‘init’, or ‘update’.

set_special_fun(p)

Set the special function from its parameters.

pdict

Parameters of the function to be set.

set_update_fun_from_dict(node)

For each updating equation, set the appropriate function to the System.

set_update_loop()

Set the update loop list, stored as _update_loop. The list contains all information we need to update each variable, and is ordered in the topological order of the updating graph

show_influence_graph(variables=None, depth=1, show_init_val=True, in_notebook=True, options=None, colors=None)

Show variables influence newtork with the Pyvis library.

Parameters

show_init_valbool

If True, show the initial value for a variable.

in_notebookbool

If True, network appears as a Widget in the notebook.

optionsdict

Pyvis options.

colorsdict

Colors of each variable and constant.

step(hght, sttm, k)

Step function. See specials.step.

Module contents

Module pydynamo.core.