Skip to content

Commit 8a77aa4

Browse files
committed
v1->v2: Update priors
Extend PEtab v1->v2 conversion: * remove initializationPrior* * rename objectivePrior* * convert parameterScale* priors
1 parent 3c6b45b commit 8a77aa4

File tree

1 file changed

+72
-6
lines changed

1 file changed

+72
-6
lines changed

petab/v2/petab1to2.py

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,8 @@ def petab_files_1to2(yaml_config: Path | str, output_dir: Path | str):
9292

9393
# Update tables
9494

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-
)
95+
# parameter table
96+
parameter_df = v1v2_parameter_df(petab_problem.parameter_df.copy())
10197
file = yaml_config[v2.C.PARAMETER_FILE]
10298
v2.write_parameter_df(parameter_df, get_dest_path(file))
10399

@@ -393,3 +389,73 @@ def update_noise_dist(row):
393389
df.drop(columns=[v1.C.OBSERVABLE_TRANSFORMATION], inplace=True)
394390

395391
return df
392+
393+
394+
def v1v2_parameter_df(
395+
parameter_df: pd.DataFrame,
396+
) -> pd.DataFrame:
397+
"""Convert parameter table from petab v1 to v2.
398+
399+
Do all the necessary conversions to the parameter table that can
400+
be done with the parameter table alone.
401+
"""
402+
df = parameter_df.copy().reset_index()
403+
404+
# parameter.estimate: int -> bool
405+
df[v2.C.ESTIMATE] = df[v1.C.ESTIMATE].apply(
406+
lambda x: str(bool(int(x))).lower()
407+
)
408+
409+
def update_prior(row):
410+
"""Convert prior to v2 format."""
411+
prior_type = row.get(v1.C.OBJECTIVE_PRIOR_TYPE)
412+
if pd.isna(prior_type):
413+
prior_type = v1.C.UNIFORM
414+
415+
pscale = row.get(v1.C.PARAMETER_SCALE)
416+
if pd.isna(pscale):
417+
pscale = v1.C.LIN
418+
419+
if (
420+
pscale == v1.C.LIN
421+
or prior_type not in v1.C.PARAMETER_SCALE_PRIOR_TYPES
422+
):
423+
return prior_type
424+
425+
new_prior_type = (
426+
f"{pscale}-{prior_type.removeprefix('parameterScale').lower()}"
427+
)
428+
429+
if new_prior_type not in v2.C.PRIOR_DISTRIBUTIONS:
430+
raise NotImplementedError(
431+
f"PEtab v2 does not support prior type `{new_prior_type}' "
432+
f"required for parameter `{row.index}'."
433+
)
434+
435+
return new_prior_type
436+
437+
# update parameterScale*-priors
438+
if v1.C.OBJECTIVE_PRIOR_TYPE in df.columns:
439+
df[v1.C.OBJECTIVE_PRIOR_TYPE] = df.apply(update_prior, axis=1)
440+
441+
# rename objectivePrior* to prior*
442+
df.rename(
443+
columns={
444+
v1.C.OBJECTIVE_PRIOR_TYPE: v2.C.PRIOR_DISTRIBUTION,
445+
v1.C.OBJECTIVE_PRIOR_PARAMETERS: v2.C.PRIOR_PARAMETERS,
446+
},
447+
inplace=True,
448+
errors="ignore",
449+
)
450+
# some columns were dropped in PEtab v2
451+
df.drop(
452+
columns=[
453+
v1.C.INITIALIZATION_PRIOR_TYPE,
454+
v1.C.INITIALIZATION_PRIOR_PARAMETERS,
455+
v1.C.PARAMETER_SCALE,
456+
],
457+
inplace=True,
458+
errors="ignore",
459+
)
460+
461+
return df

0 commit comments

Comments
 (0)