Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog.d/+nominal-value-nan.fix.md
Original file line number Diff line number Diff line change
@@ -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))
36 changes: 18 additions & 18 deletions petab/v1/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions tests/v1/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading