-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
271ae52
commit d25ead3
Showing
11 changed files
with
336 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Example project | ||
|
||
README normally contains something. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
tests/test-data/example-project/notebooks/011_make-draws.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# --- | ||
# jupyter: | ||
# jupytext: | ||
# text_representation: | ||
# extension: .py | ||
# format_name: percent | ||
# format_version: '1.3' | ||
# jupytext_version: 1.16.1 | ||
# kernelspec: | ||
# display_name: Python 3 (ipykernel) | ||
# language: python | ||
# name: python3 | ||
# --- | ||
|
||
# %% [markdown] | ||
# # Make draws | ||
# | ||
# Make draws of data | ||
|
||
# %% [markdown] | ||
# ## Imports | ||
|
||
# %% | ||
import numpy as np | ||
from local.config import converter_yaml, load_config_from_file | ||
|
||
from pydoit_nb.config_handling import get_config_for_step_id | ||
|
||
# %% [markdown] | ||
# ## Define the notebook-based step this notebook belongs to | ||
|
||
# %% | ||
step: str = "make_draws" | ||
|
||
# %% [markdown] | ||
# ## Parameters | ||
|
||
# %% editable=true slideshow={"slide_type": ""} tags=["parameters"] | ||
config_file: str = "dev-config-absolute.yaml" # config file | ||
step_config_id: str = "only" # config ID to select for this branch | ||
|
||
# %% [markdown] | ||
# ## Load config | ||
|
||
# %% | ||
config = load_config_from_file(config_file) | ||
config_step = get_config_for_step_id(config=config, step=step, step_config_id=step_config_id) | ||
config_set_seed = get_config_for_step_id(config=config, step="set_seed", step_config_id="only") | ||
|
||
# %% [markdown] | ||
# ## Action | ||
|
||
# %% | ||
N_DRAWS: int = 25 | ||
|
||
# %% | ||
with open(config_set_seed.file_seed) as fh: | ||
seed = int(fh.read()) | ||
|
||
seed | ||
|
||
# %% [markdown] | ||
# ### Make draws | ||
|
||
# %% | ||
generator = np.random.Generator(np.random.PCG64(seed)) | ||
|
||
# %% | ||
cov = np.array([[0.9, 0.1], [0.1, 0.3]]) | ||
draws = generator.multivariate_normal( | ||
mean=np.zeros_like(np.diag(cov)), | ||
cov=cov, | ||
size=N_DRAWS, | ||
) | ||
|
||
# %% | ||
with open(config_step.file_draws, "w") as fh: | ||
fh.write(converter_yaml.dumps(draws)) | ||
|
||
config_step.file_draws |
68 changes: 68 additions & 0 deletions
68
tests/test-data/example-project/notebooks/012_scale-draws.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# --- | ||
# jupyter: | ||
# jupytext: | ||
# text_representation: | ||
# extension: .py | ||
# format_name: percent | ||
# format_version: '1.3' | ||
# jupytext_version: 1.16.1 | ||
# kernelspec: | ||
# display_name: Python 3 (ipykernel) | ||
# language: python | ||
# name: python3 | ||
# --- | ||
|
||
# %% [markdown] | ||
# # Scale draws | ||
# | ||
# Scale the draws | ||
|
||
# %% [markdown] | ||
# ## Imports | ||
|
||
# %% | ||
import numpy as np | ||
import numpy.typing as nptype | ||
from local.config import converter_yaml, load_config_from_file | ||
|
||
from pydoit_nb.config_handling import get_config_for_step_id | ||
|
||
# %% [markdown] | ||
# ## Define the notebook-based step this notebook belongs to | ||
|
||
# %% | ||
step: str = "make_draws" | ||
|
||
# %% [markdown] | ||
# ## Parameters | ||
|
||
# %% editable=true slideshow={"slide_type": ""} tags=["parameters"] | ||
config_file: str = "dev-config-absolute.yaml" # config file | ||
step_config_id: str = "only" # config ID to select for this branch | ||
|
||
# %% [markdown] | ||
# ## Load config | ||
|
||
# %% | ||
config = load_config_from_file(config_file) | ||
config_step = get_config_for_step_id(config=config, step=step, step_config_id=step_config_id) | ||
|
||
# %% [markdown] | ||
# ## Action | ||
|
||
# %% [markdown] | ||
# ### Scale draws | ||
|
||
# %% | ||
with open(config_step.file_draws) as fh: | ||
draws = converter_yaml.loads(fh.read(), nptype.NDArray[np.float64]) | ||
|
||
# %% | ||
scaled = draws * config_step.factor | ||
|
||
|
||
# %% | ||
with open(config_step.file_draws_scaled, "w") as fh: | ||
fh.write(converter_yaml.dumps(scaled)) | ||
|
||
config_step.file_draws_scaled |
68 changes: 68 additions & 0 deletions
68
tests/test-data/example-project/notebooks/020_retrieve-data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# --- | ||
# jupyter: | ||
# jupytext: | ||
# text_representation: | ||
# extension: .py | ||
# format_name: percent | ||
# format_version: '1.3' | ||
# jupytext_version: 1.16.1 | ||
# kernelspec: | ||
# display_name: Python 3 (ipykernel) | ||
# language: python | ||
# name: python3 | ||
# --- | ||
|
||
# %% [markdown] | ||
# # Retrieve data | ||
# | ||
# Retrieve data from source | ||
|
||
# %% [markdown] | ||
# ## Imports | ||
|
||
# %% | ||
from local.config import load_config_from_file | ||
|
||
from pydoit_nb.config_handling import get_config_for_step_id | ||
|
||
# %% [markdown] | ||
# ## Define the notebook-based step this notebook belongs to | ||
|
||
# %% | ||
step: str = "retrieve_data" | ||
|
||
# %% [markdown] | ||
# ## Parameters | ||
|
||
# %% editable=true slideshow={"slide_type": ""} tags=["parameters"] | ||
config_file: str = "dev-config-absolute.yaml" # config file | ||
step_config_id: str = "only" # config ID to select for this branch | ||
|
||
# %% [markdown] | ||
# ## Load config | ||
|
||
# %% | ||
config = load_config_from_file(config_file) | ||
config_step = get_config_for_step_id(config=config, step=step, step_config_id=step_config_id) | ||
|
||
# %% [markdown] | ||
# ## Action | ||
|
||
# %% [markdown] | ||
# ### Retrieve raw data | ||
|
||
# %% | ||
config_step.file_raw_data.parent.mkdir(exist_ok=True, parents=True) | ||
with open(config_step.file_raw_data, "w") as fh: | ||
fh.write(config_step.source) | ||
|
||
# %% [markdown] | ||
# ### Clean data | ||
|
||
# %% | ||
retrieved = f"Retrieved from {config_step.source}" | ||
|
||
# %% | ||
config_step.file_clean_data.parent.mkdir(exist_ok=True, parents=True) | ||
with open(config_step.file_clean_data, "w") as fh: | ||
fh.write(retrieved) |
93 changes: 93 additions & 0 deletions
93
tests/test-data/example-project/notebooks/110_make-plots.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# --- | ||
# jupyter: | ||
# jupytext: | ||
# text_representation: | ||
# extension: .py | ||
# format_name: percent | ||
# format_version: '1.3' | ||
# jupytext_version: 1.16.1 | ||
# kernelspec: | ||
# display_name: Python 3 (ipykernel) | ||
# language: python | ||
# name: python3 | ||
# --- | ||
|
||
# %% [markdown] | ||
# # Plot | ||
# | ||
# Plot the data | ||
|
||
# %% [markdown] | ||
# ## Imports | ||
|
||
# %% | ||
from pathlib import Path | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import numpy.typing as nptype | ||
from local.config import converter_yaml, load_config_from_file | ||
|
||
from pydoit_nb.config_handling import get_config_for_step_id | ||
|
||
# %% [markdown] | ||
# ## Define the notebook-based step this notebook belongs to | ||
|
||
# %% | ||
step: str = "plot" | ||
|
||
# %% [markdown] | ||
# ## Parameters | ||
|
||
# %% editable=true slideshow={"slide_type": ""} tags=["parameters"] | ||
config_file: str = "dev-config-absolute.yaml" # config file | ||
step_config_id: str = "only" # config ID to select for this branch | ||
|
||
# %% [markdown] | ||
# ## Load config | ||
|
||
# %% | ||
config = load_config_from_file(config_file) | ||
config_step = get_config_for_step_id(config=config, step=step, step_config_id=step_config_id) | ||
|
||
# %% [markdown] | ||
# ## Action | ||
|
||
# %% [markdown] | ||
# ### Load draws | ||
|
||
|
||
# %% | ||
def load_file_to_np(fin: Path) -> nptype.NDArray[np.float64]: | ||
with open(fin) as fh: | ||
read = converter_yaml.loads(fh.read(), nptype.NDArray[np.float64]) | ||
|
||
return read | ||
|
||
|
||
loaded = {} | ||
for md in config.make_draws: | ||
loaded[md.step_config_id] = {} | ||
loaded[md.step_config_id]["draws"] = load_file_to_np(md.file_draws) | ||
loaded[md.step_config_id]["scaled"] = load_file_to_np(md.file_draws_scaled) | ||
|
||
# %% [markdown] | ||
# ### Plot | ||
|
||
# %% | ||
for idx, axis in ((0, "x"), (1, "y")): | ||
for k, v in loaded.items(): | ||
for sk in ["draws", "scaled"]: | ||
plt.hist(v[sk][:, idx], label=f"{k} {sk} {axis}", alpha=0.4) | ||
|
||
plt.legend() | ||
plt.show() | ||
|
||
# %% | ||
for k, v in loaded.items(): | ||
for sk in ["draws", "scaled"]: | ||
plt.scatter(v[sk][:, 0], v[sk][:, 1], label=f"{k} {sk}", alpha=0.4, color=config_step.colour) | ||
|
||
plt.legend() | ||
config_step.file_plot.parent.mkdir(exist_ok=True, parents=True) | ||
plt.savefig(config_step.file_plot) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters