Shortcut#
The shortcuts are used to modify the Experiment/Model object directly. The mrfmsim package provides the following shortcuts:
loop_shortcut
: loop over a parameter during the experiment execution. The shortcut finds the optimal location in the graph to create the subgraph and create the loop.print_shortcut
: automatically applyprint_inputs
andprint_output
shortcuts to individual nodes that print out intermediate variable values during node execution.
Loop Shortcut#
The loop shortcut works by locating the first dependency of the parameter in the graph. It then creates a subgraph that contains all the nodes that depend on the parameter. A new experiment is created with the subgraph as a single node, and the node function is an experiment model. When multiple parameters need to be looped, the user needs to inspect the order of the parameter appearance in the graph to achieve an optimal result.
For example, a graph of \(G=\{V=\{A, B, C\}, E=\{(A, B), (B, C)\}\}\):
A -> B -> C
A(a, b)
B(c, d)
C(e, f)
The optimal way to loop c and e is to define the loop of parameter e in node C first and then define the loop of parameter c in node B second. If the order given is reversed, both parameters c and e are looped at node B level. The reason for the behavior is that when loop c is created, the graph:
A -> BC
A(a, b)
BC(c, d, e, f)
As a result, the subsequent loop definition only recognizes the subgraph node BC and loop the node instead.
Note
For a two-loop system, the optimal order can always be resolved. However, looping more than three parameters, the optimal order may not be resolved. Therefore, the design decision is made for the user to define the loop order.
Print Shortcut#
The print shortcut aims to print out intermediate values during node execution to check the execution process. The shortcut is helpful for slow algorithms and looped models. We also do not want the algorithm to create unnecessary subgraphs. Therefore, the final design of the shortcut applies modifiers to individual nodes instead of the entire model. The design is flexible and works if the underlying graph structure is changed. The user decides the string format and output style to maintain flexibility. For the shortcut’s unachievable output style, the users are encouraged to add modifiers to the nodes directly.
For example a graph of \(G=\{V=\{A, B, C\}, E=\{(A, B), (B, C)\}\}\):
A -> B -> C
def A(a, b):
c = a + b
return c
def B(c, d):
e = c + d
return e
def C(e, f):
g = e + f
return g
And the model is M = Model(graph=G, ...)
. To output the input value a,
intermediate value of c and e:
>>> print_shortcut(M, ['a={a:.2f}', 'c={c:.2f}', 'e={e:.2f}'])
>>> M(a=1, b=2, d=4, f=10)
a=1.00
c=3.00
e=7.00
The shortcut works by applying a print_inputs
modifier and
print_output
modifier to node A, and a print_output
modifier to
node B.
The shortcuts can modify the print keyword arguments. For example, the print function’s default “end” parameter is “n”. To change the “end” parameter to “ | “:
>>> M = print_shortcut(M, ['a={a:.2f}', 'c={c:.2f}', 'e={e:.2f}'], end=' | ')
>>> M(a=1, b=2, d=4, f=10)
a=1.00 | c=3.00 | e=7.00 |
However, in a loop, the output stays in a single line, but we want to create a linebreak for each loop. The user can apply a modifier that has a unique “end” parameter to create the line break or use a second shortcut:
M = print_shorcut(print_shortcut(M, ['a={a:.2f}', 'c={c:.2f}'], end=' | '), ['e={e:.2f}'])
The output for a looped model:
>>> M_loop = loop_shortcut(M, 'a')
>>> M_loop(a_loop=[1, 2], b=2, d=4, f=10)
a=1.00 | c=3.00 | e=7.00
a=2.00 | c=4.00 | e=8.00
The decision to have a uniform print parameter argument is to simplify the
user interface and the underlying algorithm. The shortcut is used to apply
the print-related shortcuts quickly. Users are encouraged to create
additional nodes for monitoring the execution process, which can benefit from
the print_shortcut
as well.
shortcut
module#
Shortcuts
The shortcut should work for both the Model and the Experiment classes.
- mrfmsim.shortcut.loop_shortcut(model, parameter: str, name=None)[source]#
Shortcut to add a loop to a subgraph.
The parameter needs to be in the graph signature. Otherwise, an exception is raised. For parameters that are not from the graph (generated by modifiers), use regular loops or change modifiers.
- Parameters:
model – executable model
parameter (str) – loop parameter
name (str) – name of the new model, defaults to old model name.
- Returns:
a new model with looped parameter
- mrfmsim.shortcut.print_shortcut(model, stdout_format_list, **pargs)[source]#
Shortcut to add print modifiers to nodes.
- Parameters:
model – mrfmsim experiment
stdout_format_list (list) – list of format strings each format string should only contain one variable.
If the variable is not in the inputs or outputs of the nodes, the variable is ignored. The printout prioritizes inputs in a node. If a parameter is a node input and output, the first occurrence is used (when it is the output of the node). The decision to make all output the same setting (pargs for print()) is to simplify the implementation. If the user wants to have customized printout behavior, use the regular modifier for each node.