diff --git a/changelog.d/+nominal-value-nan.fix.md b/changelog.d/+nominal-value-nan.fix.md new file mode 100644 index 00000000..105426ae --- /dev/null +++ b/changelog.d/+nominal-value-nan.fix.md @@ -0,0 +1,5 @@ +PEtab v1 validation: `check_parameter_df`, and thus `lint_problem`, now +reports an error for non-estimated parameters with a missing (empty) +`nominalValue`. +As `petab1to2` validates its input, it now rejects such problems +([#640](https://github.com/PEtab-dev/libpetab-python/pull/640)) diff --git a/petab/v1/lint.py b/petab/v1/lint.py index 9d142af8..9290d77e 100644 --- a/petab/v1/lint.py +++ b/petab/v1/lint.py @@ -249,17 +249,15 @@ def check_parameter_df( df[column_name].values, column_name ) - # nominal value is generally optional, but required if any for any - # parameter estimate != 1 - non_estimated_par_ids = list( - df.index[ - (df[ESTIMATE] != 1) - | ( - pd.api.types.is_string_dtype(df[ESTIMATE]) - and df[ESTIMATE] != "1" - ) - ] - ) + assert_parameter_id_is_string(df) + assert_parameter_scale_is_valid(df) + assert_parameter_bounds_are_numeric(df) + assert_parameter_estimate_is_boolean(df) + assert_unique_parameter_ids(df) + + # nominal value is generally optional, but required for parameters + # with estimate == 0 + non_estimated_par_ids = list(df.index[df[ESTIMATE].apply(int) != 1]) if non_estimated_par_ids: if NOMINAL_VALUE not in df: raise AssertionError( @@ -269,18 +267,20 @@ def check_parameter_df( f"but column {NOMINAL_VALUE} is missing." ) try: - df.loc[non_estimated_par_ids, NOMINAL_VALUE].apply(float) - except ValueError as e: + nominal_values = df.loc[ + non_estimated_par_ids, NOMINAL_VALUE + ].apply(lambda x: np.nan if pd.isna(x) else float(x)) + except (ValueError, TypeError) as e: raise AssertionError( f"Expected numeric values for `{NOMINAL_VALUE}` in parameter " "table for all non-estimated parameters." ) from e + if missing := list(nominal_values.index[nominal_values.isna()]): + raise AssertionError( + f"`{NOMINAL_VALUE}` is required for parameters with " + f"`{ESTIMATE}=0`, but is missing for {missing}." + ) - assert_parameter_id_is_string(df) - assert_parameter_scale_is_valid(df) - assert_parameter_bounds_are_numeric(df) - assert_parameter_estimate_is_boolean(df) - assert_unique_parameter_ids(df) check_parameter_bounds(df) assert_parameter_prior_type_is_valid(df) assert_parameter_prior_parameters_are_valid(df) diff --git a/tests/v1/test_lint.py b/tests/v1/test_lint.py index e62b5505..9bd0351b 100644 --- a/tests/v1/test_lint.py +++ b/tests/v1/test_lint.py @@ -574,6 +574,27 @@ def test_check_parameter_df(): parameter_df[NOMINAL_VALUE] = parameter_df[NOMINAL_VALUE].astype("object") lint.check_parameter_df(df=parameter_df) + # NOMINAL_VALUE missing for estimated parameter is fine + parameter_df.loc["par1", NOMINAL_VALUE] = nan + lint.check_parameter_df(df=parameter_df) + + # NOMINAL_VALUE missing for non-estimated parameter + for missing_value in (nan, None, pd.NA): + parameter_df.loc["par2", NOMINAL_VALUE] = missing_value + with pytest.raises(AssertionError, match=r"missing for \['par2'\]"): + lint.check_parameter_df(df=parameter_df) + # ... with float column, as from `get_parameter_df` + parameter_df.loc["par2", NOMINAL_VALUE] = nan + float_df = parameter_df.astype({NOMINAL_VALUE: float}) + with pytest.raises(AssertionError, match=r"missing for \['par2'\]"): + lint.check_parameter_df(df=float_df) + # ... with string ESTIMATE column + str_df = parameter_df.astype({ESTIMATE: str}) + with pytest.raises(AssertionError, match=r"missing for \['par2'\]"): + lint.check_parameter_df(df=str_df) + str_df.loc["par2", NOMINAL_VALUE] = 1.0 + lint.check_parameter_df(df=str_df) + # NOMINAL_VALUE empty, for non-estimated parameter parameter_df.loc["par2", NOMINAL_VALUE] = "" with pytest.raises(AssertionError):