Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 5 additions & 4 deletions petab/v2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
ConfigDict,
Field,
ValidationInfo,
field_serializer,
field_validator,
model_validator,
)
Expand Down Expand Up @@ -875,10 +876,6 @@ def _validate_estimate_before(cls, v):
if isinstance(v, bool):
return v

# FIXME: grace period for 0/1 values until the test suite was updated
if v in [0, 1, "0", "1"]:
return bool(int(v))

# TODO: clarify whether extra whitespace is allowed
if isinstance(v, str):
v = v.strip().lower()
Expand All @@ -891,6 +888,10 @@ def _validate_estimate_before(cls, v):
f"Invalid value for estimate: {v}. Must be `true` or `false`."
)

@field_serializer("estimate")
def serialize_dt(self, estimate: bool, _info):
return str(estimate).lower()

@field_validator("lb", "ub", "nominal_value")
@classmethod
def _convert_nan_to_none(cls, v):
Expand Down
14 changes: 11 additions & 3 deletions petab/v2/petab1to2.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,20 @@ def petab_files_1to2(yaml_config: Path | str, output_dir: Path | str):
new_yaml_config = v2.ProblemConfig(**new_yaml_config)

# Update tables
# condition tables, observable tables, SBML files, parameter table:
# no changes - just copy

# parameter table:
# * parameter.estimate: int -> bool
parameter_df = petab_problem.parameter_df.copy()
parameter_df[v1.C.ESTIMATE] = parameter_df[v1.C.ESTIMATE].apply(
lambda x: str(bool(int(x))).lower()
)
file = yaml_config[v2.C.PARAMETER_FILE]
_copy_file(get_src_path(file), Path(get_dest_path(file)))
v2.write_parameter_df(parameter_df, get_dest_path(file))

# sub-problems
for problem_config in new_yaml_config.problems:
# copy files that don't need conversion
# (models, observables, visualizations)
for file in chain(
problem_config.observable_files,
(model.location for model in problem_config.model_files.values()),
Expand Down
2 changes: 1 addition & 1 deletion petab/v2/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ def add_observable(
def add_parameter(
self,
id_: str,
estimate: bool | str | int = True,
estimate: bool | str = True,
nominal_value: Number | None = None,
scale: str = None,
lb: Number = None,
Expand Down
4 changes: 2 additions & 2 deletions tests/v2/test_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ def test_modify_problem():
check_dtype=False,
)

problem.add_parameter("parameter1", 1, 0, lb=1, ub=2)
problem.add_parameter("parameter1", True, 0, lb=1, ub=2)
problem.add_parameter("parameter2", False, 2)

exp_parameter_df = pd.DataFrame(
data={
PARAMETER_ID: ["parameter1", "parameter2"],
ESTIMATE: [1, 0],
ESTIMATE: ["true", "false"],
NOMINAL_VALUE: [0.0, 2.0],
LOWER_BOUND: [1.0, np.nan],
UPPER_BOUND: [2.0, np.nan],
Expand Down