Skip to content

Commit

Permalink
🧪 Added failing test to reproduce save_result issue with relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
s-weigand committed Dec 18, 2022
1 parent ad96e6f commit e5f083b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 55 deletions.
107 changes: 67 additions & 40 deletions glotaran/builtin/io/yml/test/test_save_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from glotaran.optimization.optimize import optimize
from glotaran.project.result import Result
from glotaran.testing.simulated_data.sequential_spectral_decay import SCHEME
from glotaran.utils.io import chdir_context


@pytest.fixture(scope="session")
Expand All @@ -23,12 +24,10 @@ def dummy_result():
yield optimize(scheme, raise_exception=True)


def test_save_result_yml(
tmp_path: Path,
dummy_result: Result,
):
@pytest.mark.parametrize("path_is_absolute", (True, False))
def test_save_result_yml(tmp_path: Path, dummy_result: Result, path_is_absolute: bool):
"""Check all files exist."""
expected = dedent(
expected_result = dedent(
f"""\
number_of_function_evaluations: 1
success: true
Expand All @@ -49,49 +48,77 @@ def test_save_result_yml(
dataset_1: dataset_1.nc
"""
)
expected_scheme = dedent(
"""\
model: model.yml
parameters: initial_parameters.csv
data:
dataset_1: dataset_1.nc
clp_link_tolerance: 0.0
clp_link_method: nearest
maximum_number_function_evaluations: 1
add_svd: true
ftol: 1e-08
gtol: 1e-08
xtol: 1e-08
optimization_method: TrustRegionReflection
result_path: null
"""
)
if path_is_absolute is True:
result_dir = tmp_path / "testresult"
else:
result_dir = Path("testresult")

result_dir = tmp_path / "testresult"
result_path = result_dir / "result.yml"
save_result(result_path=result_path, result=dummy_result)
with chdir_context("." if path_is_absolute is True else tmp_path):
save_result(result_path=result_path, result=dummy_result)

assert dummy_result.source_path == result_path.as_posix()
assert dummy_result.source_path == result_path.as_posix()

assert (result_dir / "result.md").exists()
assert (result_dir / "scheme.yml").exists()
assert result_path.exists()
assert (result_dir / "initial_parameters.csv").exists()
assert (result_dir / "optimized_parameters.csv").exists()
assert (result_dir / "optimization_history.csv").exists()
assert (result_dir / "dataset_1.nc").exists()
assert (result_dir / "result.md").exists()
assert (result_dir / "scheme.yml").exists()
assert (result_dir / "scheme.yml").read_text() == expected_scheme
assert result_path.exists()
assert (result_dir / "initial_parameters.csv").exists()
assert (result_dir / "optimized_parameters.csv").exists()
assert (result_dir / "optimization_history.csv").exists()
assert (result_dir / "dataset_1.nc").exists()

# We can't check equality due to numerical fluctuations
got = result_path.read_text()
print(got)
assert expected in got
# We can't check equality due to numerical fluctuations
got = result_path.read_text()
print(got)
assert expected_result in got


def test_save_result_yml_roundtrip(tmp_path: Path, dummy_result: Result):
@pytest.mark.parametrize("path_is_absolute", (True, False))
def test_save_result_yml_roundtrip(tmp_path: Path, dummy_result: Result, path_is_absolute: bool):
"""Save and reloaded Result should be the same."""
result_dir = tmp_path / "testresult"
if path_is_absolute is True:
result_dir = tmp_path / "testresult"
else:
result_dir = Path("testresult")
result_path = result_dir / "result.yml"
save_result(result_path=result_path, result=dummy_result)
result_round_tripped = load_result(result_path)

assert dummy_result.source_path == result_path.as_posix()
assert result_round_tripped.source_path == result_path.as_posix()
with chdir_context("." if path_is_absolute is True else tmp_path):
save_result(result_path=result_path, result=dummy_result)
result_round_tripped = load_result(result_path)

assert_frame_equal(
dummy_result.initial_parameters.to_dataframe(),
result_round_tripped.initial_parameters.to_dataframe(),
)
assert_frame_equal(
dummy_result.optimized_parameters.to_dataframe(),
result_round_tripped.optimized_parameters.to_dataframe(),
)
assert_frame_equal(
dummy_result.parameter_history.to_dataframe(),
result_round_tripped.parameter_history.to_dataframe(),
)
assert_frame_equal(
dummy_result.optimization_history.data, result_round_tripped.optimization_history.data
)
assert dummy_result.source_path == result_path.as_posix()
assert result_round_tripped.source_path == result_path.as_posix()

assert_frame_equal(
dummy_result.initial_parameters.to_dataframe(),
result_round_tripped.initial_parameters.to_dataframe(),
)
assert_frame_equal(
dummy_result.optimized_parameters.to_dataframe(),
result_round_tripped.optimized_parameters.to_dataframe(),
)
assert_frame_equal(
dummy_result.parameter_history.to_dataframe(),
result_round_tripped.parameter_history.to_dataframe(),
)
assert_frame_equal(
dummy_result.optimization_history.data, result_round_tripped.optimization_history.data
)
34 changes: 19 additions & 15 deletions glotaran/builtin/io/yml/test/test_save_scheme.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from pathlib import Path

import pytest
import xarray as xr

from glotaran.io import load_scheme
Expand All @@ -13,10 +14,7 @@
from glotaran.testing.simulated_data.sequential_spectral_decay import DATASET
from glotaran.testing.simulated_data.sequential_spectral_decay import MODEL
from glotaran.testing.simulated_data.sequential_spectral_decay import PARAMETERS

if TYPE_CHECKING:
from pathlib import Path

from glotaran.utils.io import chdir_context

want = """\
model: m.yml
Expand All @@ -35,7 +33,8 @@
"""


def test_save_scheme(tmp_path: Path):
@pytest.mark.parametrize("path_is_absolute", (True, False))
def test_save_scheme(tmp_path: Path, path_is_absolute: bool):
save_model(MODEL, tmp_path / "m.yml")
save_parameters(PARAMETERS, tmp_path / "p.csv")
save_dataset(DATASET, tmp_path / "d.nc")
Expand All @@ -44,12 +43,17 @@ def test_save_scheme(tmp_path: Path):
PARAMETERS,
{"dataset_1": DATASET},
)
scheme_path = tmp_path / "testscheme.yml"
save_scheme(file_name=scheme_path, format_name="yml", scheme=scheme)

assert scheme_path.is_file()
assert scheme_path.read_text() == want
loaded = load_scheme(scheme_path)
print(loaded.model.validate(loaded.parameters))
assert loaded.model.valid(loaded.parameters)
assert isinstance(scheme.data["dataset_1"], xr.Dataset)
if path_is_absolute is True:
scheme_path = tmp_path / "testscheme.yml"
else:
scheme_path = Path("testscheme.yml")

with chdir_context("." if path_is_absolute is True else tmp_path):
save_scheme(file_name=scheme_path, format_name="yml", scheme=scheme)

assert scheme_path.is_file()
assert scheme_path.read_text() == want
loaded = load_scheme(scheme_path)
print(loaded.model.validate(loaded.parameters))
assert loaded.model.valid(loaded.parameters)
assert isinstance(scheme.data["dataset_1"], xr.Dataset)

0 comments on commit e5f083b

Please sign in to comment.