-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
zn.Meta
for metadata which is not tracked by DVC (#404)
* add `zn.Meta` for metadata which is not tracked by DVC * Merge branch 'main' into 294-... * add 'zntrack.meta.Text' ZnTrackOption * fix merge conflicts * improve test
- Loading branch information
Showing
7 changed files
with
171 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import pathlib | ||
import subprocess | ||
|
||
import pytest | ||
import yaml | ||
|
||
import zntrack | ||
|
||
|
||
class NodeWithMeta(zntrack.Node): | ||
author = zntrack.meta.Text("Fabian") | ||
title = zntrack.meta.Text("Test Node") | ||
|
||
|
||
def test_NodeWithMeta(proj_path): | ||
NodeWithMeta().write_graph() | ||
|
||
node_w_meta = NodeWithMeta.load() | ||
assert node_w_meta.author == "Fabian" | ||
|
||
dvc_yaml = yaml.safe_load(pathlib.Path("dvc.yaml").read_text()) | ||
assert dvc_yaml["stages"]["NodeWithMeta"]["meta"] == { | ||
"author": "Fabian", | ||
"title": "Test Node", | ||
} | ||
|
||
|
||
class CombinedNodeWithMeta(zntrack.Node): | ||
input: str = zntrack.zn.params("Hello ") | ||
output: str = zntrack.zn.outs() | ||
author: str = zntrack.meta.Text() | ||
|
||
def run(self): | ||
self.output = self.input + self.author | ||
|
||
|
||
def test_CombinedNodeWithMeta(proj_path): | ||
with pytest.raises(TypeError): | ||
# should raise an error because author is missing as kwarg | ||
_ = CombinedNodeWithMeta() | ||
|
||
CombinedNodeWithMeta(author="World").write_graph(run=True) | ||
assert CombinedNodeWithMeta.load().output == "Hello World" | ||
assert CombinedNodeWithMeta.load().author == "World" | ||
|
||
CombinedNodeWithMeta(author="there").write_graph(run=True) | ||
# changing the 'meta.Text' should not trigger running the model again | ||
assert CombinedNodeWithMeta.load().output == "Hello World" | ||
assert CombinedNodeWithMeta.load().author == "there" | ||
|
||
subprocess.check_call(["dvc", "repro", "-f"]) | ||
# Forcing rerun should use the updated meta keyword. | ||
assert CombinedNodeWithMeta.load().output == "Hello there" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""ZnTrack Node meta data __init__ | ||
Collection of Nodes that allow the storage of data inside a Node that is neither a true | ||
parameter but also not an output. This can be e.g. information about the user, some | ||
description but also e.g. different paths to binaries that should be used. | ||
In principle, it is advisable to add the binary as a direct dependency but often | ||
this is not feasible and can be circumvented by using 'meta'. | ||
""" | ||
import pathlib | ||
|
||
from zntrack import utils | ||
from zntrack.core.zntrackoption import ZnTrackOption | ||
|
||
|
||
class Text(ZnTrackOption): | ||
"""ZnTrack Text based meta descriptor | ||
This ZnTrackOption allows the storage of plain text data in the 'dvc.yaml' meta key. | ||
""" | ||
|
||
zn_type = utils.ZnTypes.META | ||
file = utils.Files.dvc | ||
dvc_option = utils.DVCOptions.PARAMS | ||
|
||
def get_filename(self, instance) -> pathlib.Path: | ||
return | ||
|
||
def mkdir(self, instance): | ||
return | ||
|
||
def get_data_from_files(self, instance): | ||
try: | ||
file_content = utils.file_io.read_file(self.file) | ||
except FileNotFoundError as err: | ||
raise self._get_loading_errors(instance) from err | ||
|
||
try: | ||
values = file_content["stages"][instance.node_name]["meta"][self.name] | ||
except KeyError as err: | ||
raise self._get_loading_errors(instance) from err | ||
return values | ||
|
||
def save(self, instance): | ||
if instance.__dict__.get(self.name) is utils.LazyOption: | ||
# do not save anything if __get__/__set__ was never used | ||
return | ||
utils.file_io.update_meta( | ||
file=utils.Files.dvc, | ||
node_name=instance.node_name, | ||
data={self.name: getattr(instance, self.name)}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.9
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.8
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.10
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.8
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.10
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.9
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.9
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.10
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.8
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.9
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.8
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.10
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.8
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.9
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.10
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.10
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.8
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.9
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.10
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.8
14d18c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report for Python 3.9