[1]:
import zntrack
[2]:
zntrack.config.nb_name = "06_named_nodes.ipynb"
[4]:
!git init
!dvc init
Initialized empty Git repository in /tmp/tmpeeo_o0hl/.git/
Initialized DVC repository.

You can now commit the changes to git.

+---------------------------------------------------------------------+
|                                                                     |
|        DVC has enabled anonymous aggregate usage analytics.         |
|     Read the analytics documentation (and how to opt-out) here:     |
|             <https://dvc.org/doc/user-guide/analytics>              |
|                                                                     |
+---------------------------------------------------------------------+

What's next?
------------
- Check out the documentation: <https://dvc.org/doc>
- Get help and share ideas: <https://dvc.org/chat>
- Star us on GitHub: <https://github.com/iterative/dvc>

Named Nodes#

Named Nodes allow us to use the same Node multiple times in a single graph at e.g. different steps. Therefore, we can pass a name argument to the __init__ of our Node.

Notice that this is one of only very few scenarios where we want to pass an argument directly to the __init__

[5]:
class HelloWorld(zntrack.Node):
    inputs = zntrack.zn.params()
    outputs = zntrack.zn.outs()

    def run(self):
        self.outputs = self.inputs
[6]:
with zntrack.Project() as project:
    hw1 = HelloWorld(inputs=3)
    hw2 = HelloWorld(inputs=17, name="Test01")
    hw3 = HelloWorld(inputs=42, name="Test02")
project.run()
Running DVC command: 'stage add --name HelloWorld --force ...'
Creating 'dvc.yaml'
Adding stage 'HelloWorld' in 'dvc.yaml'

To track the changes with git, run:

        git add dvc.yaml nodes/HelloWorld/.gitignore

To enable auto staging, run:

        dvc config core.autostage true
Jupyter support is an experimental feature! Please save your notebook before running this command!
Submit issues to https://github.com/zincware/ZnTrack.
[NbConvertApp] Converting notebook 06_named_nodes.ipynb to script
/data/fzills/miniconda3/envs/zntrack/lib/python3.10/site-packages/nbformat/__init__.py:93: MissingIDFieldWarning: Code cell is missing an id field, this will become a hard error in future nbformat versions. You may want to use `normalize()` on your notebooks before validations (available since nbformat 5.1.4). Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.
  validate(nb)
[NbConvertApp] Writing 1821 bytes to 06_named_nodes.py
Running DVC command: 'stage add --name Test01 --force ...'
Adding stage 'Test01' in 'dvc.yaml'

To track the changes with git, run:

        git add nodes/Test01/.gitignore dvc.yaml

To enable auto staging, run:

        dvc config core.autostage true
[NbConvertApp] Converting notebook 06_named_nodes.ipynb to script
/data/fzills/miniconda3/envs/zntrack/lib/python3.10/site-packages/nbformat/__init__.py:93: MissingIDFieldWarning: Code cell is missing an id field, this will become a hard error in future nbformat versions. You may want to use `normalize()` on your notebooks before validations (available since nbformat 5.1.4). Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.
  validate(nb)
[NbConvertApp] Writing 1821 bytes to 06_named_nodes.py
Running DVC command: 'stage add --name Test02 --force ...'
Adding stage 'Test02' in 'dvc.yaml'

To track the changes with git, run:

        git add dvc.yaml nodes/Test02/.gitignore

To enable auto staging, run:

        dvc config core.autostage true
[NbConvertApp] Converting notebook 06_named_nodes.ipynb to script
/data/fzills/miniconda3/envs/zntrack/lib/python3.10/site-packages/nbformat/__init__.py:93: MissingIDFieldWarning: Code cell is missing an id field, this will become a hard error in future nbformat versions. You may want to use `normalize()` on your notebooks before validations (available since nbformat 5.1.4). Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.
  validate(nb)
Running stage 'Test02':
> zntrack run src.HelloWorld.HelloWorld --name Test02
[NbConvertApp] Writing 1821 bytes to 06_named_nodes.py
Generating lock file 'dvc.lock'
Updating lock file 'dvc.lock'

Running stage 'Test01':
> zntrack run src.HelloWorld.HelloWorld --name Test01
Updating lock file 'dvc.lock'

Running stage 'HelloWorld':
> zntrack run src.HelloWorld.HelloWorld --name HelloWorld
Updating lock file 'dvc.lock'

To track the changes with git, run:

        git add dvc.lock

To enable auto staging, run:

        dvc config core.autostage true
Use `dvc push` to send your updates to remote storage.
[7]:
!dvc dag
+------------+
| HelloWorld |
+------------+
+--------+
| Test01 |
+--------+
+--------+
| Test02 |
+--------+

We can now also build a Node that depends on multiple of the same Nodes

[8]:
class FindMaximum(zntrack.Node):
    deps = zntrack.zn.deps()
    maximum = zntrack.zn.outs()

    def run(self):
        self.maximum = 0
        for node in self.deps:
            if node.outputs > self.maximum:
                self.maximum = node.outputs
                print(f"New maximum found {node.outputs}.")
[9]:
with project:
    max_node = FindMaximum(deps=[hw1, hw2, hw3])
project.run()
Running DVC command: 'stage add --name HelloWorld --force ...'
Modifying stage 'HelloWorld' in 'dvc.yaml'

To track the changes with git, run:

        git add dvc.yaml

To enable auto staging, run:

        dvc config core.autostage true
[NbConvertApp] Converting notebook 06_named_nodes.ipynb to script
/data/fzills/miniconda3/envs/zntrack/lib/python3.10/site-packages/nbformat/__init__.py:93: MissingIDFieldWarning: Code cell is missing an id field, this will become a hard error in future nbformat versions. You may want to use `normalize()` on your notebooks before validations (available since nbformat 5.1.4). Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.
  validate(nb)
[NbConvertApp] Writing 1821 bytes to 06_named_nodes.py
Running DVC command: 'stage add --name Test01 --force ...'
Modifying stage 'Test01' in 'dvc.yaml'

To track the changes with git, run:

        git add dvc.yaml

To enable auto staging, run:

        dvc config core.autostage true
[NbConvertApp] Converting notebook 06_named_nodes.ipynb to script
/data/fzills/miniconda3/envs/zntrack/lib/python3.10/site-packages/nbformat/__init__.py:93: MissingIDFieldWarning: Code cell is missing an id field, this will become a hard error in future nbformat versions. You may want to use `normalize()` on your notebooks before validations (available since nbformat 5.1.4). Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.
  validate(nb)
[NbConvertApp] Writing 1821 bytes to 06_named_nodes.py
Running DVC command: 'stage add --name Test02 --force ...'
Modifying stage 'Test02' in 'dvc.yaml'

To track the changes with git, run:

        git add dvc.yaml

To enable auto staging, run:

        dvc config core.autostage true
[NbConvertApp] Converting notebook 06_named_nodes.ipynb to script
/data/fzills/miniconda3/envs/zntrack/lib/python3.10/site-packages/nbformat/__init__.py:93: MissingIDFieldWarning: Code cell is missing an id field, this will become a hard error in future nbformat versions. You may want to use `normalize()` on your notebooks before validations (available since nbformat 5.1.4). Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.
  validate(nb)
[NbConvertApp] Writing 1821 bytes to 06_named_nodes.py
Running DVC command: 'stage add --name FindMaximum --force ...'
Adding stage 'FindMaximum' in 'dvc.yaml'

To track the changes with git, run:

        git add dvc.yaml nodes/FindMaximum/.gitignore

To enable auto staging, run:

        dvc config core.autostage true
[NbConvertApp] Converting notebook 06_named_nodes.ipynb to script
/data/fzills/miniconda3/envs/zntrack/lib/python3.10/site-packages/nbformat/__init__.py:93: MissingIDFieldWarning: Code cell is missing an id field, this will become a hard error in future nbformat versions. You may want to use `normalize()` on your notebooks before validations (available since nbformat 5.1.4). Previous versions of nbformat are fixing this issue transparently, and will stop doing so in the future.
  validate(nb)
Stage 'HelloWorld' didn't change, skipping
Stage 'Test01' didn't change, skipping
Stage 'Test02' didn't change, skipping
Running stage 'FindMaximum':
> zntrack run src.FindMaximum.FindMaximum --name FindMaximum
[NbConvertApp] Writing 1821 bytes to 06_named_nodes.py
New maximum found 3.
New maximum found 17.
New maximum found 42.
Updating lock file 'dvc.lock'

To track the changes with git, run:

        git add dvc.lock

To enable auto staging, run:

        dvc config core.autostage true
Use `dvc push` to send your updates to remote storage.
[10]:
!dvc dag
+------------+          +--------+          +--------+
| HelloWorld |          | Test01 |          | Test02 |
+------------+**        +--------+       ***+--------+
                ***          *        ***
                   ****     *     ****
                       **   *   **
                    +-------------+
                    | FindMaximum |
                    +-------------+

Using this combined Node we can e.g. find the maximum of the generated values.

[11]:
max_node.load()
print(max_node.maximum)
42
[12]:
# Running it manually to highlight the print statements
FindMaximum.from_rev().run()
New maximum found 3.
New maximum found 17.
New maximum found 42.