-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial_modifier.html
More file actions
654 lines (496 loc) · 37.3 KB
/
tutorial_modifier.html
File metadata and controls
654 lines (496 loc) · 37.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
<!DOCTYPE html>
<html lang="en" data-content_root="./" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Use modifiers and shortcuts — mmodel 0.9.0 documentation</title>
<script data-cfasync="false">
document.documentElement.dataset.mode = localStorage.getItem("mode") || "";
document.documentElement.dataset.theme = localStorage.getItem("theme") || "";
</script>
<!--
this give us a css class that will be invisible only if js is disabled
-->
<noscript>
<style>
.pst-js-only { display: none !important; }
</style>
</noscript>
<!-- Loaded before other Sphinx assets -->
<link href="_static/styles/theme.css?digest=8878045cc6db502f8baf" rel="stylesheet" />
<link href="_static/styles/pydata-sphinx-theme.css?digest=8878045cc6db502f8baf" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=8f2a1f02" />
<link rel="stylesheet" type="text/css" href="_static/styles/sphinx-book-theme.css?v=a3416100" />
<!-- So that users can add custom icons -->
<script src="_static/scripts/fontawesome.js?digest=8878045cc6db502f8baf"></script>
<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="_static/scripts/bootstrap.js?digest=8878045cc6db502f8baf" />
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=8878045cc6db502f8baf" />
<script src="_static/documentation_options.js?v=2a76c96f"></script>
<script src="_static/doctools.js?v=9bcbadda"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="_static/scripts/sphinx-book-theme.js?v=887ef09a"></script>
<script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
<script>window.MathJax = {"tex": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "processEscapes": true}, "options": {"ignoreHtmlClass": "tex2jax_ignore|mathjax_ignore|document", "processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script>DOCUMENTATION_OPTIONS.pagename = 'tutorial_modifier';</script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Create and modify a subgraph" href="tutorial_subgraph.html" />
<link rel="prev" title="Create a model group" href="tutorial_group.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
<meta name="docsearch:version" content="" />
</head>
<body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="">
<div id="pst-skip-link" class="skip-link d-print-none"><a href="#main-content">Skip to main content</a></div>
<div id="pst-scroll-pixel-helper"></div>
<button type="button" class="btn rounded-pill" id="pst-back-to-top">
<i class="fa-solid fa-arrow-up"></i>Back to top</button>
<dialog id="pst-search-dialog">
<form class="bd-search d-flex align-items-center"
action="search.html"
method="get">
<i class="fa-solid fa-magnifying-glass"></i>
<input type="search"
class="form-control"
name="q"
placeholder="Search..."
aria-label="Search..."
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"/>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
</form>
</dialog>
<div class="pst-async-banner-revealer d-none">
<aside id="bd-header-version-warning" class="d-none d-print-none" aria-label="Version warning"></aside>
</div>
<header class="bd-header navbar navbar-expand-lg bd-navbar d-print-none">
</header>
<div class="bd-container">
<div class="bd-container__inner bd-page-width">
<dialog id="pst-primary-sidebar-modal"></dialog>
<div id="pst-primary-sidebar" class="bd-sidebar-primary bd-sidebar">
<div class="sidebar-header-items sidebar-primary__section">
</div>
<div class="sidebar-primary-items__start sidebar-primary__section">
<div class="sidebar-primary-item">
<a class="navbar-brand logo" href="index.html">
<p class="title logo__title">mmodel 0.9.0 documentation</p>
</a></div>
<div class="sidebar-primary-item">
<button class="btn search-button-field search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</button></div>
<div class="sidebar-primary-item"><nav class="bd-links bd-docs-nav" aria-label="Main">
<div class="bd-toc-item navbar-nav active">
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="overview.html">Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">Changelog</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Tutorial</span></p>
<ul class="current nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="tutorial_node.html">Define a node</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial_graph.html">Build a model graph</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial_model.html">Create a model</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial_group.html">Create a model group</a></li>
<li class="toctree-l1 current active"><a class="current reference internal" href="#">Use modifiers and shortcuts</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorial_subgraph.html">Create and modify a subgraph</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">API Guideline</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="api_handler.html">Handler API</a></li>
<li class="toctree-l1"><a class="reference internal" href="api_modifier_shortcut.html">Modifier and shortcut API</a></li>
<li class="toctree-l1"><a class="reference internal" href="api_metadata.html">Metadata API</a></li>
</ul>
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Reference</span></p>
<ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="ref_node.html">Node</a></li>
<li class="toctree-l1"><a class="reference internal" href="ref_graph.html">Graph</a></li>
<li class="toctree-l1"><a class="reference internal" href="ref_model.html">Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="ref_handler.html">Handler</a></li>
<li class="toctree-l1"><a class="reference internal" href="ref_modifier.html">Modifier</a></li>
<li class="toctree-l1"><a class="reference internal" href="ref_shortcut.html">Shortcut</a></li>
<li class="toctree-l1"><a class="reference internal" href="ref_filter.html">Filter</a></li>
<li class="toctree-l1"><a class="reference internal" href="ref_signature.html">Signature</a></li>
<li class="toctree-l1"><a class="reference internal" href="ref_metadata.html">Metadata</a></li>
<li class="toctree-l1"><a class="reference internal" href="ref_visualizer.html">Visualizer</a></li>
<li class="toctree-l1"><a class="reference internal" href="ref_utility.html">Utility</a></li>
</ul>
</div>
</nav></div>
</div>
<div class="sidebar-primary-items__end sidebar-primary__section">
<div class="sidebar-primary-item">
<div id="ethical-ad-placement"
class="flat"
data-ea-publisher="readthedocs"
data-ea-type="readthedocs-sidebar"
data-ea-manual="true">
</div></div>
</div>
</div>
<main id="main-content" class="bd-main" role="main">
<div class="sbt-scroll-pixel-helper"></div>
<div class="bd-content">
<div class="bd-article-container">
<div class="bd-header-article d-print-none">
<div class="header-article-items header-article__inner">
<div class="header-article-items__start">
<div class="header-article-item"><button class="sidebar-toggle primary-toggle btn btn-sm" title="Toggle primary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="fa-solid fa-bars"></span>
</button></div>
</div>
<div class="header-article-items__end">
<div class="header-article-item">
<div class="article-header-buttons">
<div class="dropdown dropdown-download-buttons">
<button class="btn dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false" aria-label="Download this page">
<i class="fas fa-download"></i>
</button>
<ul class="dropdown-menu">
<li><a href="_sources/tutorial_modifier.rst" target="_blank"
class="btn btn-sm btn-download-source-button dropdown-item"
title="Download source file"
data-bs-placement="left" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-file"></i>
</span>
<span class="btn__text-container">.rst</span>
</a>
</li>
<li>
<button onclick="window.print()"
class="btn btn-sm btn-download-pdf-button dropdown-item"
title="Print to PDF"
data-bs-placement="left" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-file-pdf"></i>
</span>
<span class="btn__text-container">.pdf</span>
</button>
</li>
</ul>
</div>
<button onclick="toggleFullScreen()"
class="btn btn-sm btn-fullscreen-button"
title="Fullscreen mode"
data-bs-placement="bottom" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-expand"></i>
</span>
</button>
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i>
</button>
<button class="btn btn-sm pst-navbar-icon search-button search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass fa-lg"></i>
</button>
<button class="sidebar-toggle secondary-toggle btn btn-sm" title="Toggle secondary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="fa-solid fa-list"></span>
</button>
</div></div>
</div>
</div>
</div>
<div id="jb-print-docs-body" class="onlyprint">
<h1>Use modifiers and shortcuts</h1>
<!-- Table of contents -->
<div id="print-main-content">
<div id="jb-print-toc">
<div>
<h2> Contents </h2>
</div>
<nav aria-label="Page">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#available-modifiers-and-shortcuts">Available modifiers and shortcuts</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#apply-a-modifier-to-a-node-or-model">Apply a modifier to a node or model</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#shortcut-usage">Shortcut usage</a><ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#loop-shortcut">loop shortcut</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#print-shortcut">print shortcut</a></li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#modifier-and-shortcut-chaining">Modifier and Shortcut chaining</a></li>
</ul>
</nav>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article">
<section id="use-modifiers-and-shortcuts">
<h1>Use modifiers and shortcuts<a class="headerlink" href="#use-modifiers-and-shortcuts" title="Link to this heading">#</a></h1>
<p>Traditionally, to modify a component of a pre-defined model, one has to rewrite
the code.
The approach is error-prone and not scalable. <em>mmodel</em> uses DAG to create
modular models that allow easy modification of nodes and models post-definition
using modifiers. A modifier is a decorator that can modify the function of a node
or the model itself. To go a step further, <em>mmodel</em> also provides shortcuts,
which are designed to apply directly to a model.
The shortcuts apply modifiers to the model or nodes in the model.</p>
<section id="available-modifiers-and-shortcuts">
<h2>Available modifiers and shortcuts<a class="headerlink" href="#available-modifiers-and-shortcuts" title="Link to this heading">#</a></h2>
<div class="pst-scrollable-table-container"><table class="table" id="id1">
<caption><span class="caption-text">Available modifiers</span><a class="headerlink" href="#id1" title="Link to this table">#</a></caption>
<colgroup>
<col style="width: 9.1%" />
<col style="width: 9.1%" />
<col style="width: 81.8%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Modifier</p></th>
<th class="head"><p>Module</p></th>
<th class="head"><p>Description</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><a class="reference internal" href="ref_modifier.html#mmodel.modifier.loop_input" title="mmodel.modifier.loop_input"><code class="xref py py-mod docutils literal notranslate"><span class="pre">loop_input</span></code></a></p></td>
<td><p><a class="reference internal" href="ref_modifier.html#module-mmodel.modifier" title="mmodel.modifier"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mmodel.modifier</span></code></a></p></td>
<td><p>Modify a function to iterate one given parameter.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="ref_modifier.html#mmodel.modifier.zip_loop_inputs" title="mmodel.modifier.zip_loop_inputs"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zip_loop_inputs</span></code></a></p></td>
<td><p><a class="reference internal" href="ref_modifier.html#module-mmodel.modifier" title="mmodel.modifier"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mmodel.modifier</span></code></a></p></td>
<td><p>Modify a function to iterate the parameters pairwise.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="ref_modifier.html#mmodel.modifier.profile_time" title="mmodel.modifier.profile_time"><code class="xref py py-mod docutils literal notranslate"><span class="pre">profile_time</span></code></a></p></td>
<td><p><a class="reference internal" href="ref_modifier.html#module-mmodel.modifier" title="mmodel.modifier"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mmodel.modifier</span></code></a></p></td>
<td><p>Profile the execution time of a function.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="ref_modifier.html#mmodel.modifier.print_inputs" title="mmodel.modifier.print_inputs"><code class="xref py py-mod docutils literal notranslate"><span class="pre">print_inputs</span></code></a></p></td>
<td><p><a class="reference internal" href="ref_modifier.html#module-mmodel.modifier" title="mmodel.modifier"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mmodel.modifier</span></code></a></p></td>
<td><p>Print the inputs of the function with units.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="ref_modifier.html#mmodel.modifier.print_output" title="mmodel.modifier.print_output"><code class="xref py py-mod docutils literal notranslate"><span class="pre">print_output</span></code></a></p></td>
<td><p><a class="reference internal" href="ref_modifier.html#module-mmodel.modifier" title="mmodel.modifier"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mmodel.modifier</span></code></a></p></td>
<td><p>Print the outputs of the function with units.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="ref_shortcut.html#mmodel.shortcut.loop_shortcut" title="mmodel.shortcut.loop_shortcut"><code class="xref py py-mod docutils literal notranslate"><span class="pre">loop_shortcut</span></code></a></p></td>
<td><p><a class="reference internal" href="ref_shortcut.html#module-mmodel.shortcut" title="mmodel.shortcut"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mmodel.shortcut</span></code></a></p></td>
<td><p>Loop over a parameter during the experiment execution.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="ref_shortcut.html#mmodel.shortcut.print_shortcut" title="mmodel.shortcut.print_shortcut"><code class="xref py py-mod docutils literal notranslate"><span class="pre">print_shortcut</span></code></a></p></td>
<td><p><a class="reference internal" href="ref_shortcut.html#module-mmodel.shortcut" title="mmodel.shortcut"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mmodel.shortcut</span></code></a></p></td>
<td><p>Apply <code class="docutils literal notranslate"><span class="pre">print_inputs</span></code> and <code class="docutils literal notranslate"><span class="pre">print_output</span></code> shortcuts to individual nodes
that print out intermediate variable values during node execution.</p></td>
</tr>
</tbody>
</table>
</div>
</section>
<section id="apply-a-modifier-to-a-node-or-model">
<h2>Apply a modifier to a node or model<a class="headerlink" href="#apply-a-modifier-to-a-node-or-model" title="Link to this heading">#</a></h2>
<p>For example, to add a loop modifier to the node “add”:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">mmodel</span><span class="w"> </span><span class="kn">import</span> <span class="n">Graph</span><span class="p">,</span> <span class="n">Node</span>
<span class="n">G</span> <span class="o">=</span> <span class="n">Graph</span><span class="p">()</span>
<span class="k">def</span><span class="w"> </span><span class="nf">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""The sum of a and b."""</span>
<span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
<span class="k">def</span><span class="w"> </span><span class="nf">squared</span><span class="p">(</span><span class="n">c</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""The squared value of c."""</span>
<span class="k">return</span> <span class="n">c</span><span class="o">**</span><span class="mi">2</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">mmodel.modifier</span><span class="w"> </span><span class="kn">import</span> <span class="n">loop_input</span>
<span class="n">G</span><span class="o">.</span><span class="n">add_edge</span><span class="p">(</span><span class="s2">"add"</span><span class="p">,</span> <span class="s2">"squared"</span><span class="p">)</span>
<span class="c1"># set object without modifiers</span>
<span class="n">G</span><span class="o">.</span><span class="n">add_node_object</span><span class="p">(</span><span class="n">Node</span><span class="p">(</span><span class="s2">"squared"</span><span class="p">,</span> <span class="n">squared</span><span class="p">,</span> <span class="s2">"d"</span><span class="p">))</span>
<span class="c1"># set object with modifier</span>
<span class="n">G</span><span class="o">.</span><span class="n">add_node_object</span><span class="p">(</span><span class="n">Node</span><span class="p">(</span><span class="s2">"add"</span><span class="p">,</span> <span class="n">add</span><span class="p">,</span> <span class="s2">"c"</span><span class="p">,</span> <span class="n">modifiers</span><span class="o">=</span><span class="p">[</span><span class="n">loop_input</span><span class="p">(</span><span class="n">parameter</span><span class="o">=</span><span class="s1">'b'</span><span class="p">)]))</span>
<span class="c1"># post modification</span>
<span class="c1"># a new copy of the graph is created</span>
<span class="n">H</span> <span class="o">=</span> <span class="n">G</span><span class="o">.</span><span class="n">edit_node</span><span class="p">(</span><span class="s2">"add"</span><span class="p">,</span> <span class="n">modifiers</span><span class="o">=</span><span class="p">[</span><span class="n">loop_input</span><span class="p">(</span><span class="s1">'b'</span><span class="p">)])</span>
</pre></div>
</div>
<p>Similarly, use the <code class="docutils literal notranslate"><span class="pre">modifiers</span></code> argument to define model modifiers.</p>
</section>
<section id="shortcut-usage">
<h2>Shortcut usage<a class="headerlink" href="#shortcut-usage" title="Link to this heading">#</a></h2>
<section id="loop-shortcut">
<h3>loop shortcut<a class="headerlink" href="#loop-shortcut" title="Link to this heading">#</a></h3>
<p>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 model is created with the subgraph as a
single node, and the node function is a 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.</p>
<p>For example, a graph of <span class="math notranslate nohighlight">\(G=\{V=\{A, B, C\}, E=\{(A, B), (B, C)\}\}\)</span>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">A</span> <span class="o">-></span> <span class="n">B</span> <span class="o">-></span> <span class="n">C</span>
<span class="n">A</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="n">B</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="p">)</span>
<span class="n">C</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
</pre></div>
</div>
<p>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 the loop for c is created, the
graph:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">A</span> <span class="o">-></span> <span class="n">BC</span>
<span class="n">A</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="n">BC</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
</pre></div>
</div>
<p>As a result, the subsequent loop definition only recognizes the subgraph
node BC and loops the node instead.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>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.</p>
</div>
</section>
<section id="print-shortcut">
<h3>print shortcut<a class="headerlink" href="#print-shortcut" title="Link to this heading">#</a></h3>
<p>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.</p>
<p>For example, a graph of <span class="math notranslate nohighlight">\(G=\{V=\{A, B, C\}, E=\{(A, B), (B, C)\}\}\)</span>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">A</span> <span class="o">-></span> <span class="n">B</span> <span class="o">-></span> <span class="n">C</span>
<span class="k">def</span><span class="w"> </span><span class="nf">A</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
<span class="k">return</span> <span class="n">c</span>
<span class="k">def</span><span class="w"> </span><span class="nf">B</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
<span class="n">e</span> <span class="o">=</span> <span class="n">c</span> <span class="o">+</span> <span class="n">d</span>
<span class="k">return</span> <span class="n">e</span>
<span class="k">def</span><span class="w"> </span><span class="nf">C</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">f</span><span class="p">):</span>
<span class="n">g</span> <span class="o">=</span> <span class="n">e</span> <span class="o">+</span> <span class="n">f</span>
<span class="k">return</span> <span class="n">g</span>
</pre></div>
</div>
<p>And the model is <code class="docutils literal notranslate"><span class="pre">M</span> <span class="pre">=</span> <span class="pre">Model(graph=G,</span> <span class="pre">...)</span></code>. To output the input value a,
intermediate values of c and e:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># M = Model(...)</span>
<span class="o">>>></span> <span class="n">print_shortcut</span><span class="p">(</span><span class="n">M</span><span class="p">,</span> <span class="p">[</span><span class="s1">'a=</span><span class="si">{a:.2f}</span><span class="s1">'</span><span class="p">,</span> <span class="s1">'c=</span><span class="si">{c:.2f}</span><span class="s1">'</span><span class="p">,</span> <span class="s1">'e=</span><span class="si">{e:.2f}</span><span class="s1">'</span><span class="p">])</span>
<span class="o">>>></span> <span class="n">M</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">d</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">f</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
<span class="n">a</span><span class="o">=</span><span class="mf">1.00</span>
<span class="n">c</span><span class="o">=</span><span class="mf">3.00</span>
<span class="n">e</span><span class="o">=</span><span class="mf">7.00</span>
</pre></div>
</div>
<p>The shortcut works by applying a <code class="docutils literal notranslate"><span class="pre">print_inputs</span></code> modifier and
<code class="docutils literal notranslate"><span class="pre">print_output</span></code> modifier to node A, and a <code class="docutils literal notranslate"><span class="pre">print_output</span></code> modifier to
node B.</p>
<p>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 “ | “:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># M = Model(...)</span>
<span class="o">>>></span> <span class="n">M</span> <span class="o">=</span> <span class="n">print_shortcut</span><span class="p">(</span><span class="n">M</span><span class="p">,</span> <span class="p">[</span><span class="s1">'a=</span><span class="si">{a:.2f}</span><span class="s1">'</span><span class="p">,</span> <span class="s1">'c=</span><span class="si">{c:.2f}</span><span class="s1">'</span><span class="p">,</span> <span class="s1">'e=</span><span class="si">{e:.2f}</span><span class="s1">'</span><span class="p">],</span> <span class="n">end</span><span class="o">=</span><span class="s1">' | '</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">M</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">d</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">f</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
<span class="n">a</span><span class="o">=</span><span class="mf">1.00</span> <span class="o">|</span> <span class="n">c</span><span class="o">=</span><span class="mf">3.00</span> <span class="o">|</span> <span class="n">e</span><span class="o">=</span><span class="mf">7.00</span> <span class="o">|</span>
</pre></div>
</div>
<p>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:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">M</span> <span class="o">=</span> <span class="n">print_shortcut</span><span class="p">(</span><span class="n">print_shortcut</span><span class="p">(</span><span class="n">M</span><span class="p">,</span> <span class="p">[</span><span class="s1">'a=</span><span class="si">{a:.2f}</span><span class="s1">'</span><span class="p">,</span> <span class="s1">'c=</span><span class="si">{c:.2f}</span><span class="s1">'</span><span class="p">],</span> <span class="n">end</span><span class="o">=</span><span class="s1">' | '</span><span class="p">),</span> <span class="p">[</span><span class="s1">'e=</span><span class="si">{e:.2f}</span><span class="s1">'</span><span class="p">])</span>
</pre></div>
</div>
<p>The output for a looped model:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># M = Model(...)</span>
<span class="o">>>></span> <span class="n">M_loop</span> <span class="o">=</span> <span class="n">loop_shortcut</span><span class="p">(</span><span class="n">M</span><span class="p">,</span> <span class="s1">'a'</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">M_loop</span><span class="p">(</span><span class="n">a_loop</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="n">b</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">d</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">f</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
<span class="n">a</span><span class="o">=</span><span class="mf">1.00</span> <span class="o">|</span> <span class="n">c</span><span class="o">=</span><span class="mf">3.00</span> <span class="o">|</span> <span class="n">e</span><span class="o">=</span><span class="mf">7.00</span>
<span class="n">a</span><span class="o">=</span><span class="mf">2.00</span> <span class="o">|</span> <span class="n">c</span><span class="o">=</span><span class="mf">4.00</span> <span class="o">|</span> <span class="n">e</span><span class="o">=</span><span class="mf">8.00</span>
</pre></div>
</div>
<p>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 <code class="docutils literal notranslate"><span class="pre">print_shortcut</span></code> as well.</p>
</section>
</section>
<section id="modifier-and-shortcut-chaining">
<h2>Modifier and Shortcut chaining<a class="headerlink" href="#modifier-and-shortcut-chaining" title="Link to this heading">#</a></h2>
<p>Because the modifiers are decorators, they can be chained. The modifiers in the
list are applied in the order of the <code class="docutils literal notranslate"><span class="pre">modifiers</span></code> argument. The output of a
shortcut is a model; therefore, we can apply multiple shortcuts to a single model.</p>
<p>See <a class="reference internal" href="api_modifier_shortcut.html"><span class="doc">modifier and shortcut API reference</span></a> for
creating custom modifiers and shortcuts.
See <a class="reference internal" href="ref_modifier.html"><span class="doc">modifier reference</span></a> and <a class="reference internal" href="ref_shortcut.html"><span class="doc">shortcut reference</span></a>
for all available modifiers.</p>
</section>
</section>
</article>
<footer class="prev-next-footer d-print-none">
<div class="prev-next-area">
<a class="left-prev"
href="tutorial_group.html"
title="previous page">
<i class="fa-solid fa-angle-left"></i>
<div class="prev-next-info">
<p class="prev-next-subtitle">previous</p>
<p class="prev-next-title">Create a model group</p>
</div>
</a>
<a class="right-next"
href="tutorial_subgraph.html"
title="next page">
<div class="prev-next-info">
<p class="prev-next-subtitle">next</p>
<p class="prev-next-title">Create and modify a subgraph</p>
</div>
<i class="fa-solid fa-angle-right"></i>
</a>
</div>
</footer>
</div>
<dialog id="pst-secondary-sidebar-modal"></dialog>
<div id="pst-secondary-sidebar" class="bd-sidebar-secondary bd-toc"><div class="sidebar-secondary-items sidebar-secondary__inner">
<div class="sidebar-secondary-item">
<div class="page-toc tocsection onthispage">
<i class="fa-solid fa-list"></i> Contents
</div>
<nav class="bd-toc-nav page-toc">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#available-modifiers-and-shortcuts">Available modifiers and shortcuts</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#apply-a-modifier-to-a-node-or-model">Apply a modifier to a node or model</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#shortcut-usage">Shortcut usage</a><ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#loop-shortcut">loop shortcut</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#print-shortcut">print shortcut</a></li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#modifier-and-shortcut-chaining">Modifier and Shortcut chaining</a></li>
</ul>
</nav></div>
</div></div>
</div>
<footer class="bd-footer-content">
<div class="bd-footer-content__inner container">
<div class="footer-item">
<p class="component-author">
By Peter Sun
</p>
</div>
<div class="footer-item">
</div>
<div class="footer-item">
</div>
<div class="footer-item">
</div>
</div>
</footer>
</main>
</div>
</div>
<!-- Scripts loaded after <body> so the DOM is not blocked -->
<script defer src="_static/scripts/bootstrap.js?digest=8878045cc6db502f8baf"></script>
<script defer src="_static/scripts/pydata-sphinx-theme.js?digest=8878045cc6db502f8baf"></script>
<footer class="bd-footer">
</footer>
</body>
</html>