Utility#
utility
module#
- class mmodel.utility.EditMixin(*args, **kwargs)[source]#
Bases:
object
Mixin class that records the parameters that used in constructor.
For classes that allows the edit method to create a new instance, we keep track of the constructor parameter with the __new__ method. Some of the constructor parameters might not be saved as attributes or attributes of the same name, therefore the values are stored as well.
- property edit_dict#
Get the current attributes based on init_dict.
Some parameters are defined as properties to avoid the same reference. The method here updates the current values of the attributes.
- mmodel.utility.graph_topological_sort(graph)[source]#
Determine the topological order.
nx.topological_generations outputs a generator with each node list generation. However, it does not carry the node attributes. The method outputs a list of nodes for each generation.
- Returns:
topological order of the graph. Returns a list of nodes and their attributes.
- Return type:
list
- mmodel.utility.is_edge_attr_defined(graph, attr: str)[source]#
Check if all graph edges have the target attribute defined.
Raise an exception if the attribute is undefined.
- mmodel.utility.is_node_attr_defined(graph, attr: str)[source]#
Check if all graph nodes have the attribute defined.
- Parameters:
attr (str) – attribute string.
Raise an exception if the attribute is undefined.
- mmodel.utility.modelgraph_returns(graph)[source]#
Obtain the return parameter from the model graph.
The assumption is that all return parameter names are unique.
- Returns:
list of variable names based on note outputs
- Return type:
list
- mmodel.utility.modelgraph_signature(graph)[source]#
Obtain the signature from the model graph.
- Parameters:
graph (DiGraph) –
networkx.Digraph
object, with “signature” and “output” defined for nodes and “parameters” for edges. The args are a dictionary of inspected signatures.
- mmodel.utility.param_counter(graph, returns)[source]#
Count the number of times a parameter is used for graph execution.
Count all function signature parameters. For extra returns, add one to each count value.
- Parameters:
returns (list) – method returns (include extra returns)
- Returns:
dictionary with parameter_name: count pair
- Return type:
dict
- mmodel.utility.param_sorter(parameter)[source]#
Sorter for argument parameter.
The values in the tuple are compared in sequential order: 1. Order by parameter kind 2. Default parameter rank at the end of its kind 3. Alphabetical order
- Parameters:
parameter (inspect.Parameter) – parameter object
- Return type:
(bool, parameter.name, parameter.kind)
- mmodel.utility.parse_functype(func)[source]#
Parse function type.
For functions that are not a standard callable, returns the module name, such as
numpy.ufunc
.
- mmodel.utility.parse_parameters(parameters)[source]#
Parse a list of parameters to signatures
- Parameters:
parameters (list) – Parameters to parse. The element can either be a string as the parameter name or a tuple/list as (parameter, default).
- Returns:
parameter order and signature.
- mmodel.utility.replace_subgraph(graph, subgraph, subgraph_node)[source]#
Replace subgraph with a node.
Find all parent nodes, not in the subgraph, but child nodes in the subgraph. (All child nodes of subgraph nodes are in the subgraph). The edge attribute is passed down to the new edge. Here, a new graph is created by deep copying the original graph.
- Parameters:
model_graph (graph) – model_graph to modify.
subgraph (graph) – subgraph that is being replaced by a node subgraph is a view of the original graph.
subgraph_name (str) – name of the subgraph.
output (str) – output parameter name.