Skip to content

Commit

Permalink
Merge pull request #39 from AutoResearch/feat/support-state-logic
Browse files Browse the repository at this point in the history
feat: support state logic
  • Loading branch information
hollandjg authored Aug 25, 2023
2 parents 3831eb9 + 8da8488 commit 184021d
Show file tree
Hide file tree
Showing 37 changed files with 270 additions and 3,140 deletions.
1 change: 0 additions & 1 deletion examples/cylc-conda/.gitignore

This file was deleted.

98 changes: 0 additions & 98 deletions examples/cylc-conda/README.md

This file was deleted.

10 changes: 0 additions & 10 deletions examples/cylc-conda/environment.yml

This file was deleted.

45 changes: 0 additions & 45 deletions examples/cylc-conda/flow.cylc

This file was deleted.

52 changes: 0 additions & 52 deletions examples/cylc-conda/lib/python/controller_setup.py

This file was deleted.

8 changes: 0 additions & 8 deletions examples/cylc-conda/lib/python/dump_initial_controller.py

This file was deleted.

44 changes: 12 additions & 32 deletions examples/cylc-pip/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,16 @@ This example requires:
- familiarity with and a working installation of `cylc` (e.g. by going through the
[tutorial](https://cylc.github.io/cylc-doc/latest/html/tutorial/index.html))
- `virtualenv`
- `python3.10` (so you can run `virtualenv venv -p python3.10`)
- `python3.8` (so you can run `virtualenv venv -p python3.8`)

A new environment will be created during the setup phase of the `cylc` workflow run.

## Setup

To initialize the workflow, we define a file in the`lib/python` directory
[(a cylc convention)](https://cylc.github.io/cylc-doc/stable/html/user-guide/writing-workflows/configuration.html#workflow-configuration-directories) with the code for the experiment:
[`lib/python/controller_setup.py`](./lib/python/controller_setup.py)

The first step in the workflow will be to:
- load the controller from the file
- save its state to a `.dill` file in the share directory.

This is done with the file [`lib/python/dump_initial_controller.py`](./lib/python/dump_initial_controller.py).
[`lib/python/components.py`](./lib/python/controller_setup.py), including all the required functions. These
functions will be called in turn by the `autora.workflow.__main__` script.

The [`flow.cylc`](./flow.cylc) file defines the workflow.

Expand All @@ -43,7 +38,7 @@ cylc install .

We tell cylc to play the workflow:
```shell
cylc play "with-cylc-pip"
cylc play "cylc-pip"
```

(As a shortcut for "validate, install and play", use `cylc vip .`)
Expand All @@ -55,39 +50,24 @@ cylc gui

... or the text user interface (TUI):
```shell
cylc tui "with-cylc-pip"
cylc tui "cylc-pip"
```

## Results

We can load and interrogate the resulting object as follows:
We can load and interrogate the results as follows:

```python
import os
import dill
import numpy as np
from matplotlib import pyplot as plt

from controller_setup import experiment_runner as ground_truth, noise_std

def plot_results(controller_):
last_model = controller_.state.filter_by(kind={"MODEL"}).history[-1].data

x = np.linspace(-10, 10, 100).reshape((-1, 1))

plt.plot(x, last_model.predict(x), label="model")

plt.plot(x, ground_truth(x, noise_std_=0.), label="ground_truth", c="orange")
plt.fill_between(x.flatten(), ground_truth(x, noise_std_=0.).flatten() + noise_std, ground_truth(x, noise_std_=0.).flatten() - noise_std,
alpha=0.3, color="orange")
from autora.state import State

for i, observation in enumerate(controller_.state.filter_by(kind={"OBSERVATION"}).history):
xi, yi = observation.data[:,0], observation.data[:,1]
plt.scatter(xi, yi, label=f"observation {i=}")
plt.legend()
def show_results(s: State):
print(s)

with open(os.path.expanduser("~/cylc-run/with-cylc-pip/runN/share/controller.dill"),"rb") as file:
controller_result = dill.load(file)
with open(os.path.expanduser("~/cylc-run/cylc-pip/runN/share/controller.dill"),"rb") as file:
state = dill.load(file)

plot_results(controller_result)
show_results(state)
```
47 changes: 27 additions & 20 deletions examples/cylc-pip/flow.cylc
Original file line number Diff line number Diff line change
@@ -1,50 +1,57 @@
[scheduling]
cycling mode = integer
initial cycle point = 1
final cycle point = 3
initial cycle point = 0
final cycle point = 5
[[graph]]
R1 = """
setup_python => initialize_controller => experimentalist
R1/0 = """
setup_python => initial_state
"""
P1 = """
experimentalist => experiment_runner => theorist
theorist[-P1] => experimentalist
R1/1 = """
initial_state[^] => experimentalist => experiment_runner => theorist
"""
2/P1 = """
theorist[-P1] => experimentalist => experiment_runner => theorist
"""

[runtime]
[[setup_python]]
script = """
virtualenv "$CYLC_WORKFLOW_SHARE_DIR/env" -p python3.10
virtualenv "$CYLC_WORKFLOW_SHARE_DIR/env" -p python3.8
source "$CYLC_WORKFLOW_SHARE_DIR/env/bin/activate"
pip install --upgrade pip
pip install -r "$CYLC_WORKFLOW_RUN_DIR/requirements.txt"
"""
[[initialize_controller]]
[[initial_state]]
script = """
$CYLC_WORKFLOW_SHARE_DIR/env/bin/python \
-m dump_initial_controller \
"$CYLC_WORKFLOW_SHARE_DIR/controller.dill"
-m autora.workflow \
components.initial_state \
--out-path "$CYLC_WORKFLOW_SHARE_DIR/$CYLC_TASK_CYCLE_POINT/result.dill" \
--debug
"""
[[experimentalist]]
script = """
$CYLC_WORKFLOW_SHARE_DIR/env/bin/python -m autora.workflow \
"$CYLC_WORKFLOW_SHARE_DIR/controller.dill" \
"$CYLC_WORKFLOW_SHARE_DIR/controller.dill" \
experimentalist
components.experimentalist \
--in-path "$CYLC_WORKFLOW_SHARE_DIR/$((CYLC_TASK_CYCLE_POINT - 1))/result.dill" \
--out-path "$CYLC_WORKFLOW_SHARE_DIR/$CYLC_TASK_CYCLE_POINT/conditions.dill" \
--debug
"""
[[experiment_runner]]
script = """
$CYLC_WORKFLOW_SHARE_DIR/env/bin/python -m autora.workflow \
"$CYLC_WORKFLOW_SHARE_DIR/controller.dill" \
"$CYLC_WORKFLOW_SHARE_DIR/controller.dill" \
experiment_runner
components.experiment_runner \
--in-path "$CYLC_WORKFLOW_SHARE_DIR/$CYLC_TASK_CYCLE_POINT/conditions.dill" \
--out-path "$CYLC_WORKFLOW_SHARE_DIR/$CYLC_TASK_CYCLE_POINT/data.dill" \
--debug
"""
[[theorist]]
script = """
$CYLC_WORKFLOW_SHARE_DIR/env/bin/python -m autora.workflow \
"$CYLC_WORKFLOW_SHARE_DIR/controller.dill" \
"$CYLC_WORKFLOW_SHARE_DIR/controller.dill" \
theorist
components.theorist \
--in-path "$CYLC_WORKFLOW_SHARE_DIR/$CYLC_TASK_CYCLE_POINT/data.dill" \
--out-path "$CYLC_WORKFLOW_SHARE_DIR/$CYLC_TASK_CYCLE_POINT/result.dill" \
--debug
"""


Loading

0 comments on commit 184021d

Please sign in to comment.