Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/pip/sphinx-4.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ authored May 2, 2022
2 parents 2b882e6 + bbb2691 commit a26e830
Show file tree
Hide file tree
Showing 18 changed files with 506 additions and 70 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
[![Documentation](https://readthedocs.org/projects/zntrack/badge/?version=latest)](https://zntrack.readthedocs.io/en/latest/?badge=latest)
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/zincware/ZnTrack/HEAD)
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.6472851.svg)](https://doi.org/10.5281/zenodo.6472851)
[![ZnTrack](https://img.shields.io/badge/Powered%20by-ZnTrack-%23007CB0)](https://zntrack.readthedocs.io/en/latest/)




Expand Down
106 changes: 80 additions & 26 deletions examples/docs/05_metadata.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"cell_type": "markdown",
"id": "7be10adc-8677-4a69-87f6-489b10d7a989",
"metadata": {
"tags": []
"tags": [],
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"# Metadata collection with ZnTrack\n",
Expand All @@ -18,7 +21,11 @@
"cell_type": "code",
"execution_count": 1,
"id": "f0676d7e-7803-488c-9891-d500d55aa47f",
"metadata": {},
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"from zntrack import Node, zn, config\n",
Expand All @@ -34,7 +41,10 @@
"id": "355f3eb2-b42b-43d6-aa18-29dfa579b742",
"metadata": {
"nbsphinx": "hidden",
"tags": []
"tags": [],
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
Expand All @@ -47,13 +57,17 @@
"cell_type": "code",
"execution_count": 3,
"id": "c2ea08fe-7113-421e-b6b3-71b4e048c724",
"metadata": {},
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initialized empty Git repository in /tmp/tmpiprbxls8/.git/\r\n",
"Initialized empty Git repository in /tmp/tmp4us1bcfh/.git/\r\n",
"Initialized DVC repository.\r\n",
"\r\n",
"You can now commit the changes to git.\r\n",
Expand Down Expand Up @@ -84,7 +98,11 @@
"cell_type": "code",
"execution_count": 4,
"id": "bf980834-b0d5-4be4-ac26-d653736645f6",
"metadata": {},
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"class SleepNode(Node):\n",
Expand All @@ -107,15 +125,19 @@
"cell_type": "code",
"execution_count": 5,
"id": "e4e13211-1462-44d4-939a-9a96a057969e",
"metadata": {},
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2022-02-22 13:49:02,925 (WARNING): Jupyter support is an experimental feature! Please save your notebook before running this command!\n",
"2022-04-29 15:25:27,153 (WARNING): Jupyter support is an experimental feature! Please save your notebook before running this command!\n",
"Submit issues to https://github.com/zincware/ZnTrack.\n",
"2022-02-22 13:49:06,330 (WARNING): Running DVC command: 'dvc run -n SleepNode ...'\n"
"2022-04-29 15:25:31,612 (WARNING): Running DVC command: 'dvc run -n SleepNode ...'\n"
]
}
],
Expand All @@ -127,14 +149,18 @@
"cell_type": "code",
"execution_count": 6,
"id": "d87965c0-b9c9-40cc-9b63-dc9047c42915",
"metadata": {},
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" \rore\u001B[39m>Path metadata.run:timeit metadata.sleep_1s:timeit\r\n",
"nodes/SleepNode/metrics_no_cache.json 3.0023 1.00115\r\n",
"nodes/SleepNode/metrics_no_cache.json 3.00312 1.00053\r\n",
"\u001B[0m"
]
}
Expand All @@ -146,7 +172,11 @@
{
"cell_type": "markdown",
"id": "8d5fdb4c-ae21-47d9-8c2f-e1c89d48a1da",
"metadata": {},
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"We can also time a sinlge function multiple times, using the following example:"
]
Expand All @@ -155,16 +185,21 @@
"cell_type": "code",
"execution_count": 7,
"id": "a689306e-4b24-44c7-a933-2482483090f9",
"metadata": {},
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"class SleepNodeMulti(Node):\n",
" metadata = zn.metadata()\n",
"\n",
" @TimeIt\n",
" def run(self):\n",
" self.sleep(1)\n",
" self.sleep(2)\n",
" self.sleep(1.0)\n",
" self.sleep(0.8)\n",
" self.sleep(1.2)\n",
"\n",
" @TimeIt\n",
" def sleep(self, time):\n",
Expand All @@ -175,13 +210,17 @@
"cell_type": "code",
"execution_count": 8,
"id": "cbc74582-c997-4050-8076-199ef2f63df6",
"metadata": {},
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2022-02-22 13:49:19,112 (WARNING): Running DVC command: 'dvc run -n SleepNodeMulti ...'\n"
"2022-04-29 15:25:43,644 (WARNING): Running DVC command: 'dvc run -n SleepNodeMulti ...'\n"
]
}
],
Expand All @@ -193,15 +232,19 @@
"cell_type": "code",
"execution_count": 9,
"id": "f8336ac2-bb26-45c7-9094-262f4de57126",
"metadata": {},
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" \rore\u001B[39m>Path metadata.run:timeit metadata.sleep:timeit metadata.sleep_1:timeit metadata.sleep_1s:timeit\r\n",
"nodes/SleepNode/metrics_no_cache.json 3.0023 - - 1.00115\r\n",
"nodes/SleepNodeMulti/metrics_no_cache.json 3.00354 1.00103 2.00188 -\r\n",
" \rore\u001B[39m>Path metadata.run:timeit metadata.sleep:timeit.mean metadata.sleep:timeit.std metadata.sleep_1s:timeit\r\n",
"nodes/SleepNode/metrics_no_cache.json 3.00312 - - 1.00053\r\n",
"nodes/SleepNodeMulti/metrics_no_cache.json 3.00455 1.00099 0.20019 -\r\n",
"\u001B[0m"
]
}
Expand All @@ -213,20 +256,28 @@
{
"cell_type": "markdown",
"id": "aca25029-74ec-457c-9759-8b80ec06bce9",
"metadata": {},
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"One can also access the metrics directly within Python. This is possible, because they are just another `zn.metrics` which is automatically added when using one of the given metadata decorators."
"One can also access the metrics directly within Python. This is possible, because they are just another `zn.metrics` which is automatically added when using one of the given metadata decorators. This also gives access to the individual run times which are hidden when using `dvc metrics show`."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "6b6d4e62-f2db-4550-b89c-9012c3387cbf",
"metadata": {},
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"text/plain": "{'sleep:timeit': 1.0010313987731934,\n 'sleep_1:timeit': 2.0018763542175293,\n 'run:timeit': 3.00354266166687}"
"text/plain": "{'sleep:timeit': {'values': [1.0008997917175293,\n 0.8008444309234619,\n 1.2012319564819336],\n 'mean': 1.0009920597076416,\n 'std': 0.20019377872637664},\n 'run:timeit': 3.0045523643493652}"
},
"execution_count": 10,
"metadata": {},
Expand All @@ -243,7 +294,10 @@
"id": "35ed65a1-9590-43f8-8aa9-244fd187f823",
"metadata": {
"nbsphinx": "hidden",
"tags": []
"tags": [],
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
Expand Down
32 changes: 32 additions & 0 deletions tests/integration_tests/test_node_to_node_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,35 @@ def test_stacked_name_getdeps(proj_path, steps):

for step in range(steps):
assert AddOne[f"add_{step}"].output == step + 1


class ParameterNodeWithHash(Node):
param1 = zn.params()
param2 = zn.params()

_hash = zn.Hash()

def run(self):
pass


class ParamDeps(Node):
deps: ParameterNodeWithHash = zn.deps(ParameterNodeWithHash)
outs = zn.outs()

def run(self):
self.outs = self.deps.param1 + self.deps.param2


def test_ParameterNodeWithHash(proj_path):
ParameterNodeWithHash(param1=2, param2=40).write_graph()
ParamDeps().write_graph()

subprocess.check_call(["dvc", "repro"])
assert ParamDeps.load().outs == 42

# Change parameters and thereby check if the dependencies are executed
ParameterNodeWithHash(param1=10, param2=7).save()
subprocess.check_call(["dvc", "dag"])
subprocess.check_call(["dvc", "repro"])
assert ParamDeps.load().outs == 17
46 changes: 46 additions & 0 deletions tests/integration_tests/test_single_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,49 @@ def test_load_named_nodes(proj_path):
# this will run load with name=Node01, lazy=True/False
assert ExampleNode01[{"name": "Node01", "lazy": True}].outputs == 42
assert ExampleNode01[{"name": "Node01", "lazy": False}].outputs == 42


class NodeCustomFileName(Node):
output_std = zn.outs()
output_custom = zn.outs(filename="custom_data")

def run(self):
self.output_std = "Hello World"
self.output_custom = "Lorem Ipsum"


def test_NodeCustomFileName(proj_path):
NodeCustomFileName().write_graph(run=True)

assert NodeCustomFileName.load().output_std == "Hello World"
assert NodeCustomFileName.load().output_custom == "Lorem Ipsum"

output_std = pathlib.Path("nodes", "NodeCustomFileName", "outs.json")
output_custom = pathlib.Path("nodes", "NodeCustomFileName", "custom_data.json")

assert output_std.exists()
assert output_custom.exists()
#
assert json.loads(output_std.read_text())["output_std"] == "Hello World"
assert json.loads(output_custom.read_text())["output_custom"] == "Lorem Ipsum"


def test_collect(proj_path):
ExampleNode01(inputs="Hello World").write_graph(run=True)
assert ExampleNode01.load().zntrack.collect(zn.params) == {"inputs": "Hello World"}
assert ExampleNode01.load().zntrack.collect(zn.outs) == {"outputs": "Hello World"}

SingleNodeNoInit(param1=25, param2=42).write_graph(run=True)
assert SingleNodeNoInit.load().zntrack.collect(zn.params) == {
"param1": 25,
"param2": 42,
}
assert SingleNodeNoInit.load().zntrack.collect(zn.outs) == {"result": 67}

ExampleNode01(inputs={"Hello": "World"}, name="TestNode").write_graph(run=True)
assert ExampleNode01["TestNode"].zntrack.collect(zn.params) == {
"inputs": {"Hello": "World"}
}

with pytest.raises(ValueError):
ExampleNode01["TestNode"].zntrack.collect((zn.params, zn.outs))
27 changes: 26 additions & 1 deletion tests/integration_tests/test_timeit.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,36 @@ def run(self):
time.sleep(self.sleep_for)


class SleepClassLoop(Node):
sleep_for = zn.params(0.1)
timeit_metrics = zn.metadata()
iterations = zn.params(30)

def run(self):
for _ in range(30):
self.sleep()

@TimeIt
def sleep(self):
time.sleep(self.sleep_for)


def test_timeit_no_metadata_err(proj_path):
with pytest.raises(DescriptorMissing):
SleepClassNoMetadata().run_and_save()


def test_timeit(proj_path):
SleepClass().run_and_save()
assert SleepClass.load().timeit_metrics["run:timeit"] - 1.0 < 0.1
assert pytest.approx(SleepClass.load().timeit_metrics["run:timeit"], 0.1) == 1.0


def test_timeit_loop(proj_path):
sleep_class = SleepClassLoop()
sleep_class.run_and_save()
assert (
pytest.approx(SleepClassLoop.load().timeit_metrics["sleep:timeit"]["mean"], 0.01)
== 0.1
)
assert SleepClassLoop.load().timeit_metrics["sleep:timeit"]["std"] < 1e-3
assert len(SleepClassLoop.load().timeit_metrics["sleep:timeit"]["values"]) == 30
Loading

0 comments on commit a26e830

Please sign in to comment.