Skip to content

Commit

Permalink
Refactored examples
Browse files Browse the repository at this point in the history
  • Loading branch information
joernweissenborn committed Oct 16, 2021
1 parent 557959c commit a14fcdd
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 158 deletions.
2 changes: 1 addition & 1 deletion docs/source/notebooks/quickstart/quickstart.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"metadata": {},
"outputs": [],
"source": [
"from glotaran.examples.sequential import dataset\n",
"from glotaran.examples.sequential_spectral_decay import DATASET as dataset\n",
"\n",
"dataset"
]
Expand Down
1 change: 0 additions & 1 deletion glotaran/examples/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
from glotaran.examples import sequential
67 changes: 67 additions & 0 deletions glotaran/examples/parallel_spectral_decay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import numpy as np

from glotaran.analysis.simulation import simulate
from glotaran.io import load_model
from glotaran.io import load_parameters
from glotaran.project import Scheme
from glotaran.project.generators import generate_model_yml

SIMULATION_MODEL_YML = generate_model_yml(
"spectral-decay-parallel", **{"nr_compartments": 3, "irf": True}
)
SIMULATION_MODEL = load_model(SIMULATION_MODEL_YML, format_name="yml_str")

MODEL_YML = generate_model_yml("decay-parallel", **{"nr_compartments": 3, "irf": True})
MODEL = load_model(MODEL_YML, format_name="yml_str")

WANTED_PARAMETER_YML = """
rates:
- [species_1, 0.5]
- [species_2, 0.3]
- [species_3, 0.1]
irf:
- [center, 0.3]
- [width, 0.1]
shapes:
species_1:
- [amplitude, 30]
- [location, 620]
- [width, 40]
species_2:
- [amplitude, 20]
- [location, 630]
- [width, 20]
species_3:
- [amplitude, 60]
- [location, 650]
- [width, 60]
"""
WANTED_PARAMETER = load_parameters(WANTED_PARAMETER_YML, format_name="yml_str")

PARAMETER_YML = """
rates:
- [species_1, 0.5]
- [species_2, 0.3]
- [species_3, 0.1]
irf:
- [center, 0.3]
- [width, 0.1]
"""
PARAMETER = load_parameters(PARAMETER_YML, format_name="yml_str")

TIME_AXIS = np.arange(-1, 20, 0.01)
SPECTRAL_AXIS = np.arange(600, 700, 1.4)

DATASET = simulate(
SIMULATION_MODEL,
"dataset_1",
WANTED_PARAMETER,
{"time": TIME_AXIS, "spectral": SPECTRAL_AXIS},
noise=True,
noise_std_dev=1e-2,
)

SCHEME = Scheme(model=MODEL, parameters=PARAMETER, data={"dataset1": DATASET})
152 changes: 0 additions & 152 deletions glotaran/examples/sequential.py

This file was deleted.

67 changes: 67 additions & 0 deletions glotaran/examples/sequential_spectral_decay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import numpy as np

from glotaran.analysis.simulation import simulate
from glotaran.io import load_model
from glotaran.io import load_parameters
from glotaran.project import Scheme
from glotaran.project.generators import generate_model_yml

SIMULATION_MODEL_YML = generate_model_yml(
"spectral-decay-sequential", **{"nr_compartments": 3, "irf": True}
)
SIMULATION_MODEL = load_model(SIMULATION_MODEL_YML, format_name="yml_str")

MODEL_YML = generate_model_yml("decay-sequential", **{"nr_compartments": 3, "irf": True})
MODEL = load_model(MODEL_YML, format_name="yml_str")

WANTED_PARAMETER_YML = """
rates:
- [species_1, 0.5]
- [species_2, 0.3]
- [species_3, 0.1]
irf:
- [center, 0.3]
- [width, 0.1]
shapes:
species_1:
- [amplitude, 30]
- [location, 620]
- [width, 40]
species_2:
- [amplitude, 20]
- [location, 630]
- [width, 20]
species_3:
- [amplitude, 60]
- [location, 650]
- [width, 60]
"""
WANTED_PARAMETER = load_parameters(WANTED_PARAMETER_YML, format_name="yml_str")

PARAMETER_YML = """
rates:
- [species_1, 0.5]
- [species_2, 0.3]
- [species_3, 0.1]
irf:
- [center, 0.3]
- [width, 0.1]
"""
PARAMETER = load_parameters(PARAMETER_YML, format_name="yml_str")

TIME_AXIS = np.arange(-1, 20, 0.01)
SPECTRAL_AXIS = np.arange(600, 700, 1.4)

DATASET = simulate(
SIMULATION_MODEL,
"dataset_1",
WANTED_PARAMETER,
{"time": TIME_AXIS, "spectral": SPECTRAL_AXIS},
noise=True,
noise_std_dev=1e-2,
)

SCHEME = Scheme(model=MODEL, parameters=PARAMETER, data={"dataset1": DATASET})
6 changes: 4 additions & 2 deletions glotaran/examples/test/test_example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import xarray as xr

from glotaran.examples.sequential import dataset
from glotaran.examples.parallel_spectral_decay import DATASET as parallel_dataset
from glotaran.examples.sequential_spectral_decay import DATASET as sequential_dataset


def test_dataset():
assert isinstance(dataset, xr.Dataset)
assert isinstance(parallel_dataset, xr.Dataset)
assert isinstance(sequential_dataset, xr.Dataset)
3 changes: 3 additions & 0 deletions glotaran/project/generators/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
"""The glotaran generator package."""

from glotaran.project.generators.generator import generate_model
from glotaran.project.generators.generator import generate_model_yml
2 changes: 1 addition & 1 deletion glotaran/project/generators/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _generate_decay_model(
The generated model dictionary.
"""
compartments = [f"species_{i+1}" for i in range(nr_compartments)]
rates = [f"decay.species_{i+1}" for i in range(nr_compartments)]
rates = [f"rates.species_{i+1}" for i in range(nr_compartments)]
model = {
"megacomplex": {
f"megacomplex_{decay_type}_decay": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_generate_parallel_model(megacomplex_type: str, irf: bool, spectral: boo
assert megacomplex.type == f"decay-{megacomplex_type}"
assert megacomplex.compartments == expected_compartments
assert [r.full_label for r in megacomplex.rates] == [
f"decay.species_{i+1}" for i in range(nr_compartments)
f"rates.species_{i+1}" for i in range(nr_compartments)
]

assert "dataset_1" in model.dataset # type:ignore[attr-defined]
Expand Down

0 comments on commit a14fcdd

Please sign in to comment.