Skip to content

Commit 8614c02

Browse files
authored
v2: Parameter.estimate -> bool (#368)
* Update serialization * Update upconversion * Update tests
1 parent 7ab7cd8 commit 8614c02

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

petab/v2/core.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
ConfigDict,
2020
Field,
2121
ValidationInfo,
22+
field_serializer,
2223
field_validator,
2324
model_validator,
2425
)
@@ -875,10 +876,6 @@ def _validate_estimate_before(cls, v):
875876
if isinstance(v, bool):
876877
return v
877878

878-
# FIXME: grace period for 0/1 values until the test suite was updated
879-
if v in [0, 1, "0", "1"]:
880-
return bool(int(v))
881-
882879
# TODO: clarify whether extra whitespace is allowed
883880
if isinstance(v, str):
884881
v = v.strip().lower()
@@ -891,6 +888,10 @@ def _validate_estimate_before(cls, v):
891888
f"Invalid value for estimate: {v}. Must be `true` or `false`."
892889
)
893890

891+
@field_serializer("estimate")
892+
def _serialize_estimate(self, estimate: bool, _info):
893+
return str(estimate).lower()
894+
894895
@field_validator("lb", "ub", "nominal_value")
895896
@classmethod
896897
def _convert_nan_to_none(cls, v):

petab/v2/petab1to2.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,20 @@ def petab_files_1to2(yaml_config: Path | str, output_dir: Path | str):
9191
new_yaml_config = v2.ProblemConfig(**new_yaml_config)
9292

9393
# Update tables
94-
# condition tables, observable tables, SBML files, parameter table:
95-
# no changes - just copy
94+
95+
# parameter table:
96+
# * parameter.estimate: int -> bool
97+
parameter_df = petab_problem.parameter_df.copy()
98+
parameter_df[v1.C.ESTIMATE] = parameter_df[v1.C.ESTIMATE].apply(
99+
lambda x: str(bool(int(x))).lower()
100+
)
96101
file = yaml_config[v2.C.PARAMETER_FILE]
97-
_copy_file(get_src_path(file), Path(get_dest_path(file)))
102+
v2.write_parameter_df(parameter_df, get_dest_path(file))
98103

104+
# sub-problems
99105
for problem_config in new_yaml_config.problems:
106+
# copy files that don't need conversion
107+
# (models, observables, visualizations)
100108
for file in chain(
101109
problem_config.observable_files,
102110
(model.location for model in problem_config.model_files.values()),

petab/v2/problem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ def add_observable(
939939
def add_parameter(
940940
self,
941941
id_: str,
942-
estimate: bool | str | int = True,
942+
estimate: bool | str = True,
943943
nominal_value: Number | None = None,
944944
scale: str = None,
945945
lb: Number = None,

tests/v2/test_problem.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,13 @@ def test_modify_problem():
140140
check_dtype=False,
141141
)
142142

143-
problem.add_parameter("parameter1", 1, 0, lb=1, ub=2)
143+
problem.add_parameter("parameter1", True, 0, lb=1, ub=2)
144144
problem.add_parameter("parameter2", False, 2)
145145

146146
exp_parameter_df = pd.DataFrame(
147147
data={
148148
PARAMETER_ID: ["parameter1", "parameter2"],
149-
ESTIMATE: [1, 0],
149+
ESTIMATE: ["true", "false"],
150150
NOMINAL_VALUE: [0.0, 2.0],
151151
LOWER_BOUND: [1.0, np.nan],
152152
UPPER_BOUND: [2.0, np.nan],

0 commit comments

Comments
 (0)