From d1a96374d2795ab53532d6d6a7eaf3be0ea1c7f9 Mon Sep 17 00:00:00 2001 From: Sebastian Weigand Date: Sat, 2 Apr 2022 22:10:01 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20ParameterGroup.get=20degrades=20?= =?UTF-8?q?full=5Flabel=20of=20nested=20Parameters=20with=20nesting=20over?= =?UTF-8?q?=202=20(#1043)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ๐Ÿงช Added failing test for issue 933 * ๐Ÿฉน Fixed issue in ParameterGroup.get with degraded full_label * ๐Ÿงช Added unitest for ParameterGroup.get and label nesting of 3 * ๐Ÿšง๐Ÿ“š Added change to changelog --- changelog.md | 1 + glotaran/parameter/parameter_group.py | 8 +++++--- glotaran/parameter/test/test_parameter_group.py | 2 ++ glotaran/project/test/test_result.py | 17 +++++++++++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index 75d5488f2..76252527a 100644 --- a/changelog.md +++ b/changelog.md @@ -22,6 +22,7 @@ ### ๐Ÿฉน Bug fixes - ๐Ÿฉน Fix Crash in optimization_group_calculator_linked when using guidance spectra (#950) +- ๐Ÿฉน ParameterGroup.get degrades full_label of nested Parameters with nesting over 2 (#1043) ### ๐Ÿ“š Documentation diff --git a/glotaran/parameter/parameter_group.py b/glotaran/parameter/parameter_group.py index 306b18fd8..9b91fda74 100644 --- a/glotaran/parameter/parameter_group.py +++ b/glotaran/parameter/parameter_group.py @@ -434,9 +434,9 @@ def get(self, label: str) -> Parameter: # type:ignore[override] Raised if no parameter with the given label exists. """ # sometimes the spec parser delivers the labels as int - label = str(label) # sourcery skip + full_label = str(label) # sourcery skip - path = label.split(".") + path = full_label.split(".") label = path.pop() # TODO: audit this code @@ -447,7 +447,9 @@ def get(self, label: str) -> Parameter: # type:ignore[override] except KeyError: raise ParameterNotFoundException(path, label) try: - return group._parameters[label] + parameter = group._parameters[label] + parameter.full_label = full_label + return parameter except KeyError: raise ParameterNotFoundException(path, label) diff --git a/glotaran/parameter/test/test_parameter_group.py b/glotaran/parameter/test/test_parameter_group.py index cd0bad686..db99bbf57 100644 --- a/glotaran/parameter/test/test_parameter_group.py +++ b/glotaran/parameter/test/test_parameter_group.py @@ -91,6 +91,8 @@ def test_parameter_group_from_dict_nested(): assert [p.label for _, p in group.all()] == [f"{i}" for i in range(1, 4)] assert [p.value for _, p in group.all()] == list(range(7, 10)) + assert params.get("kinetic.j.1").full_label == "kinetic.j.1" + def test_parameter_group_to_array(): params = """ diff --git a/glotaran/project/test/test_result.py b/glotaran/project/test/test_result.py index 3172feff1..47e8420e1 100644 --- a/glotaran/project/test/test_result.py +++ b/glotaran/project/test/test_result.py @@ -12,7 +12,11 @@ from glotaran.io import SAVING_OPTIONS_MINIMAL from glotaran.io import SavingOptions from glotaran.project.result import Result +from glotaran.project.scheme import Scheme +from glotaran.testing.simulated_data.sequential_spectral_decay import DATASET from glotaran.testing.simulated_data.sequential_spectral_decay import SCHEME +from glotaran.testing.simulated_data.sequential_spectral_decay import SIMULATION_MODEL +from glotaran.testing.simulated_data.shared_decay import SIMULATION_PARAMETERS from glotaran.testing.simulated_data.shared_decay import SPECTRAL_AXIS @@ -37,6 +41,19 @@ def test_result_ipython_rendering(dummy_result: Result): assert rendered_markdown_return["text/markdown"].startswith("| Optimization Result") +def test_result_markdown_nested_parameters(): + """Test not crash of Result.markdown() for nested parameters + + See https://github.com/glotaran/pyglotaran/issues/933 + """ + scheme = Scheme( + model=SIMULATION_MODEL, parameters=SIMULATION_PARAMETERS, data={"dataset_1": DATASET} + ) + result = optimize(scheme, raise_exception=True) + + assert "shapes.species_1.amplitude" in result.markdown() + + def test_get_scheme(dummy_result: Result): scheme = dummy_result.get_scheme() assert "residual" not in dummy_result.scheme.data["dataset_1"]