Skip to content

Commit

Permalink
Changed result tests to use examples
Browse files Browse the repository at this point in the history
  • Loading branch information
joernweissenborn committed Dec 3, 2021
1 parent 6cd8a35 commit ce54e47
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 61 deletions.
18 changes: 10 additions & 8 deletions glotaran/builtin/io/folder/test/test_folder_plugin.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING
from typing import Literal

import pytest

from glotaran.io import save_result
from glotaran.project.test.test_result import dummy_result # noqa: F401
from glotaran.analysis.optimize import optimize
from glotaran.examples.sequential_spectral_decay import SCHEME
from glotaran.project.result import Result

if TYPE_CHECKING:
from typing import Literal

from glotaran.project.result import Result
@pytest.fixture(scope="session")
def dummy_result():
"""Dummy result for testing."""
print(SCHEME.data["dataset_1"])
yield optimize(SCHEME, raise_exception=True)


@pytest.mark.parametrize("format_name", ("folder", "legacy"))
Expand All @@ -34,9 +38,7 @@ def test_save_result_folder(
"initial_parameters.csv",
"optimized_parameters.csv",
"parameter_history.csv",
"dataset1.nc",
"dataset2.nc",
"dataset3.nc",
"dataset_1.nc",
]
for wanted in wanted_files:
assert (result_dir / wanted).exists()
Expand Down
35 changes: 21 additions & 14 deletions glotaran/builtin/io/yml/test/test_save_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

from pathlib import Path
from textwrap import dedent
from typing import TYPE_CHECKING
import pytest
from dataclasses import replace

from glotaran import __version__
from glotaran.io import save_result
from glotaran.project.test.test_result import dummy_result # noqa: F401
from glotaran.analysis.optimize import optimize
from glotaran.examples.sequential_spectral_decay import SCHEME
from glotaran.project.result import Result

if TYPE_CHECKING:

from glotaran.project.result import Result
@pytest.fixture(scope="session")
def dummy_result():
"""Dummy result for testing."""
scheme = replace(SCHEME, maximum_number_function_evaluations=1)
print(scheme.data["dataset_1"])
yield optimize(scheme, raise_exception=True)


def test_save_result_yml(
Expand All @@ -19,22 +26,23 @@ def test_save_result_yml(
):
"""Check all files exist."""
expected = dedent(
f"""\
"""\
number_of_function_evaluations: 1
success: true
termination_reason: The maximum number of function evaluations is exceeded.
glotaran_version: {__version__}
glotaran_version: 0.5.0
free_parameter_labels:
- '1'
- '2'
- rates.species_1
- rates.species_2
- rates.species_3
- irf.center
- irf.width
scheme: scheme.yml
initial_parameters: initial_parameters.csv
optimized_parameters: optimized_parameters.csv
parameter_history: parameter_history.csv
data:
dataset1: dataset1.nc
dataset2: dataset2.nc
dataset3: dataset3.nc
dataset_1: dataset_1.nc
"""
)

Expand All @@ -46,8 +54,7 @@ def test_save_result_yml(
assert (result_dir / "result.yml").exists()
assert (result_dir / "initial_parameters.csv").exists()
assert (result_dir / "optimized_parameters.csv").exists()
assert (result_dir / "dataset1.nc").exists()
assert (result_dir / "dataset2.nc").exists()
assert (result_dir / "dataset3.nc").exists()
assert (result_dir / "dataset_1.nc").exists()

# We can't check equality due to numerical fluctuations
assert expected in (result_dir / "result.yml").read_text()
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@
from textwrap import dedent

from glotaran.deprecation.modules.test import deprecation_warning_on_call_test_helper
from glotaran.examples.sequential import parameter
from glotaran.examples.sequential_spectral_decay import PARAMETER


def test_parameter_group_to_csv_no_stderr(tmp_path: Path):
"""``ParameterGroup.to_csv`` raises deprecation warning and saves file."""
parameter_path = tmp_path / "test_parameter.csv"
deprecation_warning_on_call_test_helper(
parameter.to_csv, args=[parameter_path.as_posix()], raise_exception=True
PARAMETER.to_csv, args=[parameter_path.as_posix()], raise_exception=True
)
expected = dedent(
"""\
label,value,expression,minimum,maximum,non-negative,vary,standard-error
j.1,1.0,None,-inf,inf,False,False,None
j.0,0.0,None,-inf,inf,False,False,None
kinetic.1,0.5,None,-inf,inf,False,True,None
kinetic.2,0.3,None,-inf,inf,False,True,None
kinetic.3,0.1,None,-inf,inf,False,True,None
rates.species_1,0.5,None,-inf,inf,False,True,None
rates.species_2,0.3,None,-inf,inf,False,True,None
rates.species_3,0.1,None,-inf,inf,False,True,None
irf.center,0.3,None,-inf,inf,False,True,None
irf.width,0.1,None,-inf,inf,False,True,None
"""
Expand Down
16 changes: 10 additions & 6 deletions glotaran/deprecation/modules/test/test_project_result.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
"""Test deprecated functionality in 'glotaran.project.result'."""
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from glotaran.analysis.optimize import optimize
from glotaran.deprecation.modules.test import deprecation_warning_on_call_test_helper
from glotaran.project.test.test_result import dummy_result # noqa: F401
from glotaran.examples.sequential_spectral_decay import SCHEME
from glotaran.project.result import Result

if TYPE_CHECKING:

from glotaran.project.result import Result
@pytest.fixture(scope="session")
def dummy_result():
"""Dummy result for testing."""
print(SCHEME.data["dataset_1"])
yield optimize(SCHEME, raise_exception=True)


def test_Result_get_dataset_method(dummy_result: Result): # noqa: F811
"""Result.get_dataset(dataset_label) gives correct dataset."""

_, result = deprecation_warning_on_call_test_helper(
dummy_result.get_dataset, args=["dataset1"], raise_exception=True
dummy_result.get_dataset, args=["dataset_1"], raise_exception=True
)

assert result == dummy_result.data["dataset1"]
assert result == dummy_result.data["dataset_1"]


def test_Result_get_dataset_method_error(dummy_result: Result): # noqa: F811
Expand Down
2 changes: 1 addition & 1 deletion glotaran/examples/parallel_spectral_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@
noise_std_dev=1e-2,
)

SCHEME = Scheme(model=MODEL, parameters=PARAMETER, data={"dataset1": DATASET})
SCHEME = Scheme(model=MODEL, parameters=PARAMETER, data={"dataset_1": DATASET})
2 changes: 1 addition & 1 deletion glotaran/examples/sequential_spectral_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@
noise_std_dev=1e-2,
)

SCHEME = Scheme(model=MODEL, parameters=PARAMETER, data={"dataset1": DATASET})
SCHEME = Scheme(model=MODEL, parameters=PARAMETER, data={"dataset_1": DATASET})
27 changes: 3 additions & 24 deletions glotaran/project/test/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,15 @@
from IPython.core.formatters import format_display_data

from glotaran.analysis.optimize import optimize
from glotaran.analysis.simulation import simulate
from glotaran.analysis.test.models import ThreeDatasetDecay as suite
from glotaran.project import Scheme
from glotaran.examples.sequential_spectral_decay import SCHEME
from glotaran.project.result import Result


@pytest.fixture(scope="session")
def dummy_result():
"""Dummy result for testing."""

wanted_parameters = suite.wanted_parameters
data = {}
for i in range(3):
global_axis = getattr(suite, "global_axis" if i == 0 else f"global_axis{i+1}")
model_axis = getattr(suite, "model_axis" if i == 0 else f"model_axis{i+1}")

data[f"dataset{i+1}"] = simulate(
suite.sim_model,
f"dataset{i+1}",
wanted_parameters,
{"global": global_axis, "model": model_axis},
)
scheme = Scheme(
model=suite.model,
parameters=suite.initial_parameters,
data=data,
maximum_number_function_evaluations=1,
)

yield optimize(scheme)
print(SCHEME.data["dataset_1"])
yield optimize(SCHEME, raise_exception=True)


def test_result_ipython_rendering(dummy_result: Result):
Expand Down

0 comments on commit ce54e47

Please sign in to comment.