From ce54e47756b3e9a0a5eb35d182608dba6d361a79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Wei=C3=9Fenborn?= Date: Sat, 16 Oct 2021 15:06:15 +0200 Subject: [PATCH] Changed result tests to use examples --- .../io/folder/test/test_folder_plugin.py | 18 +++++----- .../builtin/io/yml/test/test_save_result.py | 35 +++++++++++-------- .../test/test_parameter_parameter_group.py | 12 +++---- .../modules/test/test_project_result.py | 16 +++++---- glotaran/examples/parallel_spectral_decay.py | 2 +- .../examples/sequential_spectral_decay.py | 2 +- glotaran/project/test/test_result.py | 27 ++------------ 7 files changed, 51 insertions(+), 61 deletions(-) diff --git a/glotaran/builtin/io/folder/test/test_folder_plugin.py b/glotaran/builtin/io/folder/test/test_folder_plugin.py index 710281d17..8aab0893b 100644 --- a/glotaran/builtin/io/folder/test/test_folder_plugin.py +++ b/glotaran/builtin/io/folder/test/test_folder_plugin.py @@ -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")) @@ -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() diff --git a/glotaran/builtin/io/yml/test/test_save_result.py b/glotaran/builtin/io/yml/test/test_save_result.py index a6d3e653b..65c2adeb2 100644 --- a/glotaran/builtin/io/yml/test/test_save_result.py +++ b/glotaran/builtin/io/yml/test/test_save_result.py @@ -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( @@ -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 """ ) @@ -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() diff --git a/glotaran/deprecation/modules/test/test_parameter_parameter_group.py b/glotaran/deprecation/modules/test/test_parameter_parameter_group.py index e13777338..eaba2dd76 100644 --- a/glotaran/deprecation/modules/test/test_parameter_parameter_group.py +++ b/glotaran/deprecation/modules/test/test_parameter_parameter_group.py @@ -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 """ diff --git a/glotaran/deprecation/modules/test/test_project_result.py b/glotaran/deprecation/modules/test/test_project_result.py index 0e1e34d9c..1f6e5188d 100644 --- a/glotaran/deprecation/modules/test/test_project_result.py +++ b/glotaran/deprecation/modules/test/test_project_result.py @@ -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 diff --git a/glotaran/examples/parallel_spectral_decay.py b/glotaran/examples/parallel_spectral_decay.py index 5e7e145c5..9e6a17124 100644 --- a/glotaran/examples/parallel_spectral_decay.py +++ b/glotaran/examples/parallel_spectral_decay.py @@ -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}) diff --git a/glotaran/examples/sequential_spectral_decay.py b/glotaran/examples/sequential_spectral_decay.py index 1a0fbf27f..7c771eb97 100644 --- a/glotaran/examples/sequential_spectral_decay.py +++ b/glotaran/examples/sequential_spectral_decay.py @@ -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}) diff --git a/glotaran/project/test/test_result.py b/glotaran/project/test/test_result.py index 60c27fd37..ccd9ad6a5 100644 --- a/glotaran/project/test/test_result.py +++ b/glotaran/project/test/test_result.py @@ -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):