Skip to content

Commit

Permalink
update tests, use zntrack.config
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Jan 24, 2025
1 parent 17c9f24 commit 2fc8509
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 34 deletions.
1 change: 1 addition & 0 deletions tests/files/dvc_config/user_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ stages:
- nodes/MyNode/metric.json
- nodes/MyNode/node-meta.json:
cache: true
- nodes/MyNode/results.json
20 changes: 6 additions & 14 deletions tests/files/test_user_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,18 @@
CWD = pathlib.Path(__file__).parent.resolve()


# class MyNode(zntrack.Node):
# """Some Node."""

# metric: dict = zntrack.metrics()

# def run(self) -> None:
# self.metric = {"a": 1, "b": 2}


def test_node(proj_path):
assert zntrack.Config.ALWAYS_CACHE is False
zntrack.Config.ALWAYS_CACHE = True
assert zntrack.Config.ALWAYS_CACHE is True
assert zntrack.config.ALWAYS_CACHE is False
zntrack.config.ALWAYS_CACHE = True
assert zntrack.config.ALWAYS_CACHE is True

# We define the node here, because the config has to be set
# bevore calling zntrack.metrics()
class MyNode(zntrack.Node):
"""Some Node."""

metric: dict = zntrack.metrics()
metrics_path: pathlib.Path = zntrack.metrics_path(zntrack.nwd / "results.json")

def run(self) -> None:
self.metric = {"a": 1, "b": 2}
Expand All @@ -40,8 +32,8 @@ def run(self) -> None:

proj.build()

zntrack.Config.ALWAYS_CACHE = False # reset to default value
assert zntrack.Config.ALWAYS_CACHE is False
zntrack.config.ALWAYS_CACHE = False # reset to default value
assert zntrack.config.ALWAYS_CACHE is False

assert json.loads(
(CWD / "zntrack_config" / "user_config.json").read_text()
Expand Down
4 changes: 4 additions & 0 deletions tests/files/zntrack_config/user_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"nwd": {
"_type": "pathlib.Path",
"value": "nodes/MyNode"
},
"metrics_path": {
"_type": "pathlib.Path",
"value": "$nwd$/results.json"
}
}
}
4 changes: 2 additions & 2 deletions zntrack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from zntrack.add import add
from zntrack.apply import apply
from zntrack.config import Config
from zntrack.fields import (
deps,
deps_path,
Expand All @@ -22,6 +21,7 @@
from zntrack.node import Node
from zntrack.project import Project
from zntrack.utils import nwd
from zntrack import config

__all__ = [
"params",
Expand All @@ -41,7 +41,7 @@
"apply",
"add",
"field",
"Config",
"config",
]

logger = logging.getLogger(__name__)
Expand Down
17 changes: 5 additions & 12 deletions zntrack/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@
EXP_INFO_PATH = pathlib.Path(".exp_info.yaml")


class Config:
"""Configuration class for ZnTrack.

Attributes
----------
ALWAYS_CACHE : bool
For "node-meta.json" and "dvc stage add ... --metrics-no-cache" the default is using
git tracked files. Setting this to True will override the default behavior to always
use the DVC cache. If you have a DVC cache setup, this might be desirable, to avoid
a mixture between DVC cache and git tracked files.

"""

ALWAYS_CACHE: bool = False
# For "node-meta.json" and "dvc stage add ... --metrics-no-cache" the default is using
# git tracked files. Setting this to True will override the default behavior to always
# use the DVC cache. If you have a DVC cache setup, this might be desirable, to avoid
# a mixture between DVC cache and git tracked files.
ALWAYS_CACHE: bool = False


# Use sentinel object for zntrack specific configurations. Use
Expand Down
5 changes: 3 additions & 2 deletions zntrack/fields/outs_and_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import znjson

from zntrack.config import NOT_AVAILABLE, ZnTrackOptionEnum, Config
from zntrack.config import NOT_AVAILABLE, ZnTrackOptionEnum
from zntrack import config
from zntrack.fields.base import field
from zntrack.node import Node

Expand Down Expand Up @@ -47,7 +48,7 @@ def outs(*, cache: bool = True, independent: bool = False, **kwargs):

def metrics(*, cache: bool|None = None, independent: bool = False, **kwargs):
if cache is None:
cache = Config.ALWAYS_CACHE
cache = config.ALWAYS_CACHE
return field(
default=NOT_AVAILABLE,
cache=cache,
Expand Down
6 changes: 4 additions & 2 deletions zntrack/fields/x_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
ZNTRACK_LAZY_VALUE,
ZNTRACK_OPTION,
ZnTrackOptionEnum,
Config,
)
from zntrack import config

# if t.TYPE_CHECKING:
from zntrack.node import Node
Expand Down Expand Up @@ -89,10 +89,12 @@ def plots_path(
def metrics_path(
default=dataclasses.MISSING,
*,
cache: bool = Config.ALWAYS_CACHE,
cache: bool|None = None,
independent: bool = False,
**kwargs,
):
if cache is None:
cache = config.ALWAYS_CACHE
kwargs["metadata"] = kwargs.get("metadata", {})
kwargs["metadata"][ZNTRACK_OPTION] = ZnTrackOptionEnum.METRICS_PATH
kwargs["metadata"][ZNTRACK_CACHE] = cache
Expand Down
4 changes: 2 additions & 2 deletions zntrack/plugins/dvc_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
ZNTRACK_OPTION,
ZNTRACK_OPTION_PLOTS_CONFIG,
ZnTrackOptionEnum,
Config,
)
from zntrack import config

# if t.TYPE_CHECKING:
from zntrack.node import Node
Expand Down Expand Up @@ -132,7 +132,7 @@ def convert_to_dvc_yaml(self) -> dict | object:
"metrics": [
{
(self.node.nwd / "node-meta.json").as_posix(): {
"cache": Config.ALWAYS_CACHE
"cache": config.ALWAYS_CACHE
}
}
],
Expand Down

0 comments on commit 2fc8509

Please sign in to comment.