From 4a96db257fb49ca8eda7bff79fba335771bf66af Mon Sep 17 00:00:00 2001 From: s-weigand Date: Fri, 20 May 2022 15:25:37 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Added=20missing=20parameters=20chec?= =?UTF-8?q?k=20to=20model=20validation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- glotaran/model/model.py | 7 ++++ glotaran/model/test/test_model.py | 53 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/glotaran/model/model.py b/glotaran/model/model.py index 3fb9339f6..93733fa55 100644 --- a/glotaran/model/model.py +++ b/glotaran/model/model.py @@ -388,6 +388,13 @@ def problem_list(self, parameters: ParameterGroup | None = None) -> list[str]: for item in items.values(): problems += item.validate(self, parameters=parameters) + if parameters is not None and len(parameters.missing_parameter_value_labels) != 0: + label_prefix = "\n - " + problems.append( + f"Parameter definition is missing values for the labels:" + f"{label_prefix}{label_prefix.join(parameters.missing_parameter_value_labels)}" + ) + return problems def validate(self, parameters: ParameterGroup = None, raise_exception: bool = False) -> str: diff --git a/glotaran/model/test/test_model.py b/glotaran/model/test/test_model.py index b1970cc2b..3eb5266b6 100644 --- a/glotaran/model/test/test_model.py +++ b/glotaran/model/test/test_model.py @@ -10,6 +10,7 @@ import xarray as xr from IPython.core.formatters import format_display_data +from glotaran.io import load_parameters from glotaran.model import DatasetModel from glotaran.model import Megacomplex from glotaran.model import megacomplex @@ -337,6 +338,58 @@ def test_model_validity(test_model: Model, model_error: Model, parameter: Parame assert len(model_error.problem_list(parameter)) == 10 +def test_model_validate_missing_parameters(): + """Show list of missing parameters as a problem.""" + + model_dict = { + "default_megacomplex": "decay-sequential", + "megacomplex": { + "megacomplex_sequential_decay": { + "type": "decay-sequential", + "compartments": ["species_1", "species_2", "species_3", "species_4"], + "rates": [ + "b.missing_value_1", + "b.missing_value_2", + "b.2", + "kinetic.j.missing_value_3", + ], + "dimension": "time", + } + }, + "dataset": { + "dataset_1": { + "group": "default", + "megacomplex": ["megacomplex_sequential_decay"], + } + }, + } + model = Model.from_dict(model_dict) + parameters = load_parameters( + dedent( + """\ + b: + - ["missing_value_1",] + - ["missing_value_2"] + - ["2", 0.75] + kinetic: + j: + - ["missing_value_3"] + """ + ), + format_name="yml_str", + ) + expected = dedent( + """\ + Your model has 1 problems: + + * Parameter definition is missing values for the labels: + - b.missing_value_1 + - b.missing_value_2 + - kinetic.j.missing_value_3""" + ) + assert model.validate(parameters) == expected + + def test_items(test_model: Model): assert "m1" in test_model.megacomplex