Utility#
utility module#
- class mmodel.utility.EditMixin(*args, **kwargs)[source]#
Bases:
objectMixin class that records the parameters used in the constructor.
For classes that allow the edit method to create a new instance, we keep track of the constructor parameters with the __new__ method. Some of the constructor parameters might not be saved as attributes or as 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.check_model_returns(graph, returns)[source]#
Check if the user-defined returns are valid.
The function is used by the model, where only returns with elements are passed.
- Parameters:
returns (list) – returns to check
- Return type:
list
- 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. This function outputs a list of nodes with their attributes.
- Returns:
topological order of the graph. Returns a list of (node, attributes) tuples.
- 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.is_node_output_unique(graph)[source]#
Check if all node output attributes are unique.
Here we allow None values to be duplicated. The algorithm simply starts a new list for comparison. Raises an exception if the node attribute values are not unique.
- 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.DiGraphobject 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 parameters.
The values in the tuple are compared in sequential order: 1. Order by parameter kind 2. Default parameters rank at the end of their kind 3. Alphabetical order
- Parameters:
parameter (inspect.Parameter) – parameter object
- Return type:
tuple(int, bool, str)
- 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.