PyRates.pyrates.ir¶
pyrates.ir module (intermediate representation)¶
This module contains classes relevant for the intermediate representation (IR) in PyRates. All variations of PyRates frontends should produce valid instances of IR classes defined in this module and all backends should accept only these instances as input. Thus the intermediate representation is assumed to be consistent across different frontends and backends.
pyrates.ir.abc module¶
-
class
pyrates.ir.abc.
AbstractBaseIR
(label: str, template: str = None)[source]¶ Bases:
object
Abstract base class for intermediate representation classes
-
__getitem__
(key: str)[source]¶ Custom implementation of __getitem__ that dissolves strings of form “key1/key2/key3” into lookups of form self[key1][key2][key3].
Parameters: key – Returns: Return type: item
-
classmethod
from_file
(filename: str, filetype: str = 'pickle', error_on_instance_check: bool = True)[source]¶ Load an IR instance from file. The function verifies that the loaded object is indeed an instance of the class this function was called from.
Parameters: - filename – Path to file (relative or absolute)
- filetype – Indicate which file type to expect
- error_on_instance_check – Toggle whether or not to raise an error if the instance check fails.
Returns: Return type: Any
-
getitem_from_iterator
(key: str, key_iter: Iterator[str])[source]¶ Invoked by __getitem__ or [] slicing. Needs to be implemented in subclass.
-
label
¶
-
template
¶
-
to_file
(filename: str, filetype: str = 'pickle', template_name: Optional[str] = None) → None[source]¶ Save an IR object to file. The filetype ‘pickle’ save the object as is to a file, whereas the ‘yaml’ option creates a template from the IR object. In the latter case you need to define a template_name that is used in the YAML file.
Parameters: - filename – Path to file (absolute or relative).
- filetype – Chooses which loader to use to load the file. Allowed types: pickle, yaml
- template_name – This is required for the ‘yaml’ format, in order to define the name of the newly created template.
Returns: Return type: None
-
pyrates.ir.circuit module¶
-
class
pyrates.ir.circuit.
CircuitIR
(label: str = 'circuit', nodes: Dict[str, pyrates.ir.node.NodeIR] = None, edges: list = None, template: str = None, step_size_adaptation: bool = False, step_size: float = None, verbose: bool = True, backend: str = None, scalar_shape: tuple = None, **kwargs)[source]¶ Bases:
pyrates.ir.abc.AbstractBaseIR
Custom graph data structure that represents a backend of nodes and edges with associated equations and variables.
-
edges
¶ Shortcut to self.graph.edges. See documentation of networkx.MultiDiGraph.edges.
-
get_var
(var: str, get_key: bool = False) → Union[str, pyrates.backend.computegraph.ComputeVar][source]¶ Extracts variable from the backend (i.e. the ComputeGraph instance).
Parameters: - var – Name of the variable.
- get_key – If true, the backend variable name will be returned
Returns: Either the backend variable or its name.
Return type: Union[str, ComputeVar]
-
getitem_from_iterator
(key: str, key_iter: Iterator[str])[source]¶ Invoked by __getitem__ or [] slicing. Needs to be implemented in subclass.
-
graph
¶
-
label
¶
-
nodes
¶ Shortcut to self.graph.nodes. See documentation of networkx.MultiDiGraph.nodes.
-
run
(simulation_time: float, outputs: Optional[dict] = None, sampling_step_size: Optional[float] = None, solver: str = 'euler', **kwargs) → Union[dict, Tuple[dict, float]][source]¶ Simulate the backend behavior over time via a tensorflow session.
Parameters: - simulation_time – Simulation time in seconds.
- outputs – Output variables that will be returned. Each key is the desired name of an output variable and each value is a string that specifies a variable in the graph in the same format as used for the input definition: ‘node_name/op_name/var_name’.
- sampling_step_size – Time in seconds between sampling points of the output variables.
- solver – Numerical solving scheme to use for differential equations. Currently supported ODE solving schemes: - ‘euler’ for the explicit Euler method - ‘scipy’ for integration via the scipy.integrate.solve_ivp method.
- kwargs – Keyword arguments that are passed on to the chosen solver.
Returns: First entry of the tuple contains the output variables in a pandas dataframe, the second contains the simulation time in seconds. If profiling was not chosen during call of the function, only the dataframe will be returned.
Return type: Union[DataFrame, Tuple[DataFrame, float]]
-
-
class
pyrates.ir.circuit.
NetworkGraph
(label: str = 'circuit', nodes: Dict[str, pyrates.ir.node.NodeIR] = None, edges: list = None, template: str = None, step_size: float = 0.001, step_size_adaptation: bool = True, verbose: bool = True, **kwargs)[source]¶ Bases:
pyrates.ir.abc.AbstractBaseIR
View on the entire network as a graph. Translates edge operations and attributes into a form that allows to parse the network graph into a final compute graph.
-
__getitem__
(key: str)[source]¶ Custom implementation of __getitem__ that dissolves strings of form “key1/key2/key3” into lookups of form self[key1][key2][key3].
Parameters: key – Returns: Return type: item
-
add_edge
(source: str, target: str, edge_ir: pyrates.ir.edge.EdgeIR = None, weight: float = 1.0, delay: float = None, spread: float = None, **data)[source]¶ Parameters: - source –
- target –
- edge_ir –
- weight –
- delay –
- spread –
- data – If no template is given, data is assumed to conform to the format that is needed to add an edge. I.e., data needs to contain fields for weight, delay, edge_ir, source_var, target_var.
-
edges
¶ Shortcut to self.graph.edges. See documentation of networkx.MultiDiGraph.edges.
-
getitem_from_iterator
(key: str, key_iter: Iterator[str])[source]¶ Invoked by __getitem__ or [] slicing. Needs to be implemented in subclass.
-
nodes
¶ Shortcut to self.graph.nodes. See documentation of networkx.MultiDiGraph.nodes.
-
pyrates.ir.node module¶
-
class
pyrates.ir.node.
NodeIR
(label: str, operators: pyrates.ir.operator_graph.OperatorGraph, values: dict = None, template: str = None)[source]¶ Bases:
pyrates.ir.abc.AbstractBaseIR
-
getitem_from_iterator
(key: str, key_iter: Iterator[str])[source]¶ Alias for self.op_graph.getitem_from_iterator
-
op_graph
¶
-
operators
¶
-
values
¶
-
-
class
pyrates.ir.node.
VectorizedNodeIR
(label: str, operators: pyrates.ir.operator_graph.OperatorGraph, values: dict = None, template: str = None)[source]¶ Bases:
pyrates.ir.abc.AbstractBaseIR
Alternate version of NodeIR that takes a full NodeIR as input and creates a vectorized form of it.
-
__len__
()[source]¶ Returns size of this vector node as recorded in self._length.
Returns: Return type: self._length
-
add_op
(op_key: str, inputs: dict, output: str, equations: list, variables: dict)[source]¶ Wrapper for internal op_graph.add_operator that adds any values to node-level values dictionary for quick access
Parameters: - op_key – Name of operator to be added
- inputs – dictionary definining input variables of the operator
- output – string defining name of single output variable
- equations – list of equations (strings)
- variables – dictionary describing variables
-
add_op_edge
(source_op_key: str, target_op_key: str, **attr)[source]¶ Alias to self.op_graph.add_edge
Parameters: - source_op_key –
- target_op_key –
- attr –
-
extend
(node: pyrates.ir.node.NodeIR)[source]¶ Extend variables vectors by values from one additional node.
Parameters: node – A node whose values are used to extend the vector dimension of this vectorized node.
-
getitem_from_iterator
(key: str, key_iter: Iterator[str])[source]¶ Alias for self.op_graph.getitem_from_iterator
-
length
¶
-
op_graph
¶
-
operators
¶
-
pyrates.ir.edge module¶
-
class
pyrates.ir.edge.
EdgeIR
(label: str, operators: pyrates.ir.operator_graph.OperatorGraph = None, values: dict = None, template: str = None)[source]¶ Bases:
pyrates.ir.node.VectorizedNodeIR
-
get_inputs
()[source]¶ Find all input variables of operators that need to be mapped to source variables in a source node.
-
inputs
¶ Detect input variables of edge. This also references the operator the variable belongs to.
Note: As of 0.9.0 multiple source/input variables are allowed per edge.
-
length
¶
-
n_inputs
¶ Computes number of input variables that need to be informed from outside the edge (meaning from some node).
-
op_graph
¶
-
output
¶ Detect output variable of edge, assuming only one output variable exists.
-
pyrates.ir.operator module¶
-
class
pyrates.ir.operator.
OperatorIR
(equations: List[str], variables: List[tuple], inputs: List[str], output: str, template: str = None)[source]¶ Bases:
pyrates.ir.abc.AbstractBaseIR
This implementation of the Operator IR is aimed to be hashable and immutable. Following Python standards, we assume that users are consenting adults. Objects are thus not actually immutable, just slightly protected. This might change in the future.
-
equations
¶
-
getitem_from_iterator
(key: str, key_iter: Iterator[str])[source]¶ Checks if a variable named by key exists in an equations. :param key: :param key_iter:
Returns: Return type: key
-
inputs
¶
-
output
¶
-
variables
¶
-
-
class
pyrates.ir.operator.
ProtectedVariableDict
(variables: List[tuple])[source]¶ Bases:
object
Hashable dictionary-like object as container for variable definitions. It does not support item assignment after creation, but is strictly speaking not immutable. There may also be faster implementations, but this works for now.
pyrates.ir.operator_graph module¶
-
class
pyrates.ir.operator_graph.
OperatorGraph
(operators: Dict[str, pyrates.ir.operator.OperatorIR] = None, template: str = '')[source]¶ Bases:
networkx.classes.digraph.DiGraph
Intermediate representation for nodes and edges.
-
getitem_from_iterator
(key: str, key_iter: Iterator[str])[source]¶ Helper function for Python magic __getitem__. Accepts an iterator that yields string keys. If key_iter contains one key, an operator will be (looked for and) returned. If it instead contains two keys, properties of a variable that belong to an operator is returned.
Parameters: - key –
- key_iter –
Returns: operator or variable properties
Return type: item
-
-
class
pyrates.ir.operator_graph.
VectorizedOperatorGraph
(op_graph: pyrates.ir.operator_graph.OperatorGraph = None, values: dict = None)[source]¶ Bases:
networkx.classes.digraph.DiGraph
Alternate version of OperatorGraph that is produced during vectorization. Contents of this version are not particularly protected and the instance is not cached.
-
append_values
(value_dict: dict)[source]¶ Append value along vector dimension of operators.
Parameters: value_dict –
-
getitem_from_iterator
(key: str, key_iter: Iterator[str])[source]¶ Helper function for Python magic __getitem__. Accepts an iterator that yields string keys. If key_iter contains one key, an operator will be (looked for and) returned. If it instead contains two keys, properties of a variable that belong to an operator is returned.
Parameters: - key –
- key_iter –
Returns: operator or variable properties
Return type: item
-
operators
¶ Alias for self.nodes
-