Repository navigation
v1: validate and consistently interpret estimate values - #650
Conversation
`lint_problem` crashed with `ValueError` instead of reporting invalid `estimate` values (e.g., `"abc"`, empty cells) or non-numeric bounds, since the parameter table checks used `int()`/`float()` without catching errors. Also, `0.5` was accepted as `estimate`, and string `estimate` values were interpreted differently by lint (`"1"` estimated) and by the rest of `petab.v1` (`"1"` not estimated). Now, `estimate` values are parsed by a single function: accepted are integers and floats equal to 0 or 1 and the strings `"0"` and `"1"`. All code that decides whether a parameter is estimated uses `_is_estimated`, which does not modify the parameter table. Booleans are rejected by lint, but still interpreted as 0/1 elsewhere for backwards compatibility. Lint now reports invalid `estimate` values and non-numeric `lowerBound`, `upperBound`, and `nominalValue` values as `AssertionError` naming the affected parameters. Numeric strings such as `"0.1"` are accepted for these columns. Closes PEtab-dev#641. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #650 +/- ##
==========================================
+ Coverage 80.44% 81.20% +0.75%
==========================================
Files 75 75
Lines 8596 8793 +197
Branches 1568 1608 +40
==========================================
+ Hits 6915 7140 +225
+ Misses 1191 1172 -19
+ Partials 490 481 -9 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
PaulJonasJost
left a comment
There was a problem hiding this comment.
In general approved. If the to_numeric is possible, could vastly shorten the PR
| return list(parameter_df.index[_is_estimated(parameter_df)]) | ||
|
|
||
|
|
||
| def _parse_float(value) -> float: |
There was a problem hiding this comment.
As this seemed similar to #640, i asked claude briefly:
can most of these helpers be replaced with pd.to_numeric? With is_bool = values.map(lambda v: isinstance(v, bool | np.bool_)):
- _parse_floats → pd.to_numeric(values, errors="coerce").astype(float).mask(is_bool). The only difference is "1_0", which float() accepts as 10.
- _parse_estimate → the same, followed by .where(lambda x: x.isin([0, 1])).
- _is_estimated → pd.to_numeric(df[ESTIMATE], errors="coerce").eq(1).fillna(False).astype(bool).
The only behaviour change for estimate is that numeric strings such as "1.0", "+1", "01", " 1" and "1e0" count as 1, where the current code accepts only "0" and "1". Is that strictness intended? If so, a small string check in lint could keep it, while allow_bool, the nested parser and the fast paths could still go.
There was a problem hiding this comment.
I would prefer being even more strict, but that is difficult to get into the current v1 code. From those examples, I would only find it reasonable to accept " 1" if we ignore extra whitespace everywhere.
We could argue about some individual cases to accept or not, but overall, the goal was to apply the same rules for the linter and the other consumers and not to introduce different conversion/acceptance rules.
There was a problem hiding this comment.
fair point, but perhaps we can add for those a small correction and still mainly use the pd.to_numeric? not a strong opinion, but i see opportunity to keep the code base smaller 🙈
| ) | ||
| from .distributions import * | ||
| from .parameters import scale, unscale | ||
| from .parameters import _is_estimated, scale, unscale |
There was a problem hiding this comment.
Since we import it here, should we make it a public function?
There was a problem hiding this comment.
No, it's still (package)private.
| Numeric strings are accepted, see :func:`parameters._parse_float`. | ||
| """ | ||
| values = df[column] | ||
| present = values.notna() & ~values.isin([""]) |
There was a problem hiding this comment.
This together with the check in line 679 could produce a misleading error when only one value is "" correct? not sure this is intended.
There was a problem hiding this comment.
Old issue, but good catch. Fixed now.
| is_estimated = parameters._is_estimated(parameter_df) | ||
| lower_bounds = parameters._parse_floats(parameter_df[LOWER_BOUND]) | ||
| upper_bounds = parameters._parse_floats(parameter_df[UPPER_BOUND]) | ||
| for (par_id, row), estimated, lb, ub in zip( |
There was a problem hiding this comment.
Since the masks are already column-wise, could the loop be replaced by filtering to estimated rows and checking with masks?
There was a problem hiding this comment.
Makes sense. Changed it. Now all errors of one type share a single error message.
Report missing bounds of estimated parameters explicitly, instead of as `lowerBound` greater than `upperBound`. Check all parameters at once and report all affected parameters. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replace the per-value parsers by `pd.to_numeric`. `estimate` still only accepts the strings `"0"` and `"1"`. Numeric strings with underscores, such as `"1_0"`, are no longer accepted as bounds or nominal values. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Still needed for type annotations; removing it only went unnoticed on Python >= 3.14, where annotations are evaluated lazily. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lint_problemcrashed withValueErrorinstead of reporting invalidestimatevalues (e.g.,"abc", empty cells) or non-numeric bounds, as the parameter table checks usedint()/float()without catching errors. Also,estimate=0.5was accepted, and stringestimatevalues were interpreted differently by lint ("1"estimated) and by the rest ofpetab.v1("1"not estimated by, e.g.,get_optimization_parameters).Changes:
estimatevalues are parsed by a single function. Valid values are integers and floats equal to 0 or 1 (floats, because an empty cell makes pandas read the column as float) and the strings"0"and"1". Booleans, other strings such as"1.0"or"+1", and missing values are invalid.petab.v1(andpetab1to2) that decides whether a parameter is estimated uses the new_is_estimated, which does not modify the parameter table. Invalid values count as not estimated, except booleans, which are rejected by lint but still interpreted as 0/1 elsewhere for backwards compatibility.estimatevalues and non-numericlowerBound,upperBound, andnominalValuevalues asAssertionErrornaming the affected parameters. Numeric strings such as"0.1"are accepted for these columns. Non-numericnominalValues of estimated parameters were not checked before.check_parameter_boundscompares numeric values, so numeric-string bounds no longer raiseTypeError.check_parameter_boundsreports missing bounds of estimated parameters as such (previously "lowerBound greater than upperBound") and lists all affected parameters instead of only the first.Converting numeric-string bounds and nominal values in non-lint code (e.g.,
Problem.lb_scaled, prior handling, sampling) is left for a follow-up; that code already failed on string bounds before this change.Closes #641.
🤖 Generated with Claude Code