Skip to content

Commit

Permalink
optuna example (#624)
Browse files Browse the repository at this point in the history
* optuna example

* use subprocess

* improve optuna example

* add jupyter-black

* rerun optuna

* add jupyter

* format notebooks
  • Loading branch information
PythonFZ authored May 26, 2023
1 parent 4822680 commit a266604
Show file tree
Hide file tree
Showing 15 changed files with 843 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ repos:
rev: 23.3.0
hooks:
- id: black
additional_dependencies: [".[jupyter]"]
types_or: [python, pyi, jupyter]
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
Expand Down
1 change: 1 addition & 0 deletions docs/source/_examples/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ The notebooks are organized in the following way:
09_lazy
OptionalDVC
jupyter
parameter_optimization
10 changes: 5 additions & 5 deletions examples/dataclasses_for_parameters.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,18 @@
" stop: int\n",
" step: int = 1\n",
"\n",
"\n",
"class ParameterConverter(znjson.ConverterBase):\n",
" instance = Parameter\n",
" representation = \"Parameter\"\n",
"\n",
" def encode(self, obj) -> str:\n",
" return dataclasses.asdict(obj)\n",
" \n",
"\n",
" def decode(self, value: str):\n",
" return Parameter(**value)\n",
"\n",
"\n",
"class ComputeRandomNumber(Node):\n",
" param: Parameter = zn.params()\n",
" number = zn.outs()\n",
Expand All @@ -148,9 +150,7 @@
" self.param = param\n",
"\n",
" def run(self):\n",
" self.number = random.randrange(\n",
" self.param.start, self.param.stop, self.param.step\n",
" )"
" self.number = random.randrange(self.param.start, self.param.stop, self.param.step)"
]
},
{
Expand Down Expand Up @@ -221,7 +221,7 @@
"parameter = Parameter(start=100, stop=200)\n",
"with Project() as proj:\n",
" ComputeRandomNumber(param=parameter)\n",
"proj.run(repro=True)\n"
"proj.run(repro=True)"
]
},
{
Expand Down
10 changes: 8 additions & 2 deletions examples/docs/02_Inheritance.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"source": [
"# Work in a temporary directory\n",
"from zntrack.utils import cwd_temp_dir\n",
"\n",
"temp_dir = cwd_temp_dir()\n",
"\n",
"!git init\n",
Expand Down Expand Up @@ -130,13 +131,16 @@
"source": [
"class AddNumber(NodeBase):\n",
" \"\"\"Shift input by an offset\"\"\"\n",
"\n",
" offset: float = zntrack.zn.params()\n",
"\n",
" def run(self):\n",
" self.output = self.inputs + self.offset\n",
"\n",
"\n",
"class MultiplyNumber(NodeBase):\n",
" \"\"\"Multiply input by a factor\"\"\"\n",
"\n",
" factor: float = zntrack.zn.params()\n",
"\n",
" def run(self):\n",
Expand Down Expand Up @@ -448,6 +452,7 @@
"source": [
"class DivideNumber(NodeBase):\n",
" \"\"\"Multiply input by a factor\"\"\"\n",
"\n",
" divider: float = zntrack.zn.params()\n",
"\n",
" def run(self):\n",
Expand All @@ -458,6 +463,7 @@
" a0: float = zntrack.zn.params()\n",
" a1: float = zntrack.zn.params()\n",
"\n",
"\n",
"class ManipulateNumber(zntrack.Node):\n",
" inputs: float = zntrack.zn.params()\n",
" output: float = zntrack.zn.outs()\n",
Expand Down Expand Up @@ -629,8 +635,8 @@
}
],
"source": [
"value_handler=DivideNumber(divider=3.0, inputs=None)\n",
"polynomial=Polynomial(a0=60.0, a1=10.0)\n",
"value_handler = DivideNumber(divider=3.0, inputs=None)\n",
"polynomial = Polynomial(a0=60.0, a1=10.0)\n",
"\n",
"with zntrack.Project() as project:\n",
" manipulate_number = ManipulateNumber(\n",
Expand Down
6 changes: 2 additions & 4 deletions examples/docs/03_dependencies.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,7 @@
"source": [
"random_number.load()\n",
"compute_power.load()\n",
"print(\n",
" f\"{random_number.number} ^ {compute_power.power} =\"\n",
" f\" {compute_power.number}\"\n",
")"
"print(f\"{random_number.number} ^ {compute_power.power} = {compute_power.number}\")"
]
},
{
Expand Down Expand Up @@ -301,6 +298,7 @@
"source": [
"# zntrack: break\n",
"\n",
"\n",
"class WriteToFile(zntrack.Node):\n",
" random_number: RandomNumber = zntrack.zn.deps()\n",
" file: Path = zntrack.dvc.outs(zntrack.nwd / \"random_number.txt\")\n",
Expand Down
3 changes: 2 additions & 1 deletion examples/docs/04_metrics_and_plots.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
"with zntrack.Project() as project:\n",
" node = MetricAndPlot()\n",
"\n",
"project.run() \n",
"project.run()\n",
"!git add .\n",
"!git commit -m \"First Run\""
]
Expand Down Expand Up @@ -623,6 +623,7 @@
" \"index\"\n",
" )\n",
"\n",
"\n",
"ZnTrackMetric().write_graph(run=True)"
]
},
Expand Down
5 changes: 2 additions & 3 deletions examples/docs/08_custom_zntrackoptions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@
" ):\n",
" atoms.append(row.toatoms())\n",
" # return the data so it can be saved in __dict__\n",
" return atoms\n",
" "
" return atoms"
]
},
{
Expand Down Expand Up @@ -295,7 +294,7 @@
"source": [
"node.load()\n",
"print(node.atoms)\n",
"# or \n",
"# or\n",
"AtomsClass.from_rev().atoms"
]
},
Expand Down
7 changes: 3 additions & 4 deletions examples/docs/09_lazy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,11 @@
"outputs": [],
"source": [
"class PrintOption(zntrack.zn.Output):\n",
"\n",
" def __init__(self):\n",
" super().__init__(dvc_option = \"outs\", use_repr=False)\n",
" super().__init__(dvc_option=\"outs\", use_repr=False)\n",
" # zntrack will try dvc --PrintOption outs.json\n",
" # we must tell it to use dvc --outs outs.json instead\n",
" \n",
"\n",
" def _get_value_from_file(self, instance) -> any:\n",
" print(f\"Loading data from files for {instance.name}\")\n",
" return super()._get_value_from_file(instance)"
Expand Down Expand Up @@ -1316,7 +1315,7 @@
}
],
"source": [
"# With lazy=False we get the results we expect. \n",
"# With lazy=False we get the results we expect.\n",
"# (Except for some random scenarios, where two random numbers are the same.)\n",
"print(f\"{random_number_1.number} != {random_number_2.number} != {random_number_3.number}\")\n",
"assert random_number_1.number != random_number_2.number\n",
Expand Down
Loading

0 comments on commit a266604

Please sign in to comment.