Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[minor] Refactor storage #422

Merged
merged 10 commits into from
Aug 15, 2024
18 changes: 9 additions & 9 deletions notebooks/deepdive.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@
],
"source": [
"# Times two\n",
"l = Linear(x=10, run_after_init=True)\n",
"l = Linear(x=10, autorun=True)\n",
"(2*l).value # These nodes will try to run right away if everything upstream is ready"
]
},
Expand Down Expand Up @@ -902,7 +902,7 @@
}
],
"source": [
"a_list = Linear(x=[1,2,3,4], run_after_init=True)\n",
"a_list = Linear(x=[1,2,3,4], autorun=True)\n",
"a_list.outputs.x[:2].value # Explicitly on the output channel"
]
},
Expand Down Expand Up @@ -945,7 +945,7 @@
}
],
"source": [
"a_dict = Linear(x={\"a\": 1, \"b\": 2}, run_after_init=True)\n",
"a_dict = Linear(x={\"a\": 1, \"b\": 2}, autorun=True)\n",
"a_dict[\"a\"].value"
]
},
Expand All @@ -970,7 +970,7 @@
"class Foo:\n",
" bar = 42\n",
" \n",
"an_object = Linear(x=Foo(), run_after_init=True)\n",
"an_object = Linear(x=Foo(), autorun=True)\n",
"an_object.bar.value"
]
},
Expand Down Expand Up @@ -1085,7 +1085,7 @@
" x = (x, x + 3.14)\n",
" return x\n",
"\n",
"from_tuple = ReturnsTuple(x=7, run_after_init=True)\n",
"from_tuple = ReturnsTuple(x=7, autorun=True)\n",
"from_tuple.outputs.to_value_dict()"
]
},
Expand Down Expand Up @@ -5508,7 +5508,7 @@
"source": [
"## Saving and loading\n",
"\n",
"For python >= 3.11, graphs can be saved and loaded on request -- either by manually invoking the `.save()` method, or by setting the `save_after_run` attribute to `True` (on the object or at instantiation by kwarg). This creates a save file (currently using HDF5 as a format) in the parent-most node's working directory.\n",
"For python >= 3.11, graphs can be saved and loaded on request -- either by manually invoking the `.save_checkpoint()` method, or by setting the `checkpoint` attribute to `True` (on the object or at instantiation by kwarg). This creates a save file for the parent-most node.\n",
"\n",
"Subsequently instantiating a node with the same name in the same place will attempt to reload the saved graph automatically. \n",
"\n",
Expand Down Expand Up @@ -5537,7 +5537,7 @@
"wf.inp = wf.create.standard.UserInput(42)\n",
"wf.middle = 2 * wf.inp\n",
"wf.end = wf.middle - 42\n",
"wf.out = wf.create.standard.UserInput(wf.end, save_after_run=True)\n",
"wf.out = wf.create.standard.UserInput(wf.end, checkpoint=\"pickle\")\n",
"wf()\n",
"# wf.save() # Not needed, since `wf.out` saves after running"
]
Expand All @@ -5558,7 +5558,7 @@
"id": "f9238cf7-3050-4607-a542-010e3bb83f41",
"metadata": {},
"source": [
"You can force a newly instantiated node to ignore (and delete!) an existing save file by setting the `overwrite_save` kwarg to `True`.\n",
"You can force a newly instantiated node to ignore (and delete!) an existing save file by setting the `delete_existing_savefiles` kwarg to `True`.\n",
"\n",
"Finally, let's clean up our save to keep this demo director clean:"
]
Expand All @@ -5570,7 +5570,7 @@
"metadata": {},
"outputs": [],
"source": [
"reloaded.storage.delete()"
"reloaded.delete_storage()"
]
},
{
Expand Down
5 changes: 5 additions & 0 deletions pyiron_workflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@
inputs_to_list,
list_to_outputs,
)
from pyiron_workflow.storage import (
StorageInterface,
PickleStorage,
available_backends,
)
2 changes: 1 addition & 1 deletion pyiron_workflow/mixin/injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _node_injection(self, injection_class, *args, inject_self=True):
# Fall back on creating a new node in case parent is None or node nexists
node_args = (self, *args) if inject_self else args
return injection_class(
*node_args, parent=self.owner.parent, label=label, run_after_init=True
*node_args, parent=self.owner.parent, label=label, autorun=True
)

# We don't wrap __all__ the operators, because you might really want the string or
Expand Down
10 changes: 10 additions & 0 deletions pyiron_workflow/mixin/semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from __future__ import annotations

from abc import ABC
from pathlib import Path
from typing import Optional

from bidict import bidict
Expand Down Expand Up @@ -97,6 +98,15 @@ def semantic_root(self) -> Semantic:
"""The parent-most object in this semantic path; may be self."""
return self.parent.semantic_root if isinstance(self.parent, Semantic) else self

def as_path(self, root: Path | str | None = None) -> Path:
"""
The semantic path as a :class:`pathlib.Path`, with a filesystem :param:`root`
(default is the current working directory).
"""
return (Path.cwd() if root is None else Path(root)).joinpath(
*self.semantic_path.split(self.semantic_delimiter)
)

def __getstate__(self):
state = super().__getstate__()
state["_parent"] = None
Expand Down
Loading
Loading