Skip to content

Commit

Permalink
✨ Added missing parameters check to model validation
Browse files Browse the repository at this point in the history
  • Loading branch information
s-weigand committed May 20, 2022
1 parent aa17bf0 commit 4a96db2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
7 changes: 7 additions & 0 deletions glotaran/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
53 changes: 53 additions & 0 deletions glotaran/model/test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4a96db2

Please sign in to comment.