Skip to content

Commit 4d0bcda

Browse files
committed
v2: Adapt models to changes in prior distributions
* priorDistribution/priorParameters instead of separate initializationPrior&objectivePrior * additional prior distributions Related to #374.
1 parent b43f5b8 commit 4d0bcda

File tree

3 files changed

+85
-89
lines changed

3 files changed

+85
-89
lines changed

petab/v2/C.py

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,10 @@
7979
NOMINAL_VALUE = "nominalValue"
8080
#: Estimate column in the parameter table
8181
ESTIMATE = "estimate"
82-
#: Initialization prior type column in the parameter table
83-
INITIALIZATION_PRIOR_TYPE = "initializationPriorType"
84-
#: Initialization prior parameters column in the parameter table
85-
INITIALIZATION_PRIOR_PARAMETERS = "initializationPriorParameters"
86-
#: Objective prior type column in the parameter table
87-
OBJECTIVE_PRIOR_TYPE = "objectivePriorType"
88-
#: Objective prior parameters column in the parameter table
89-
OBJECTIVE_PRIOR_PARAMETERS = "objectivePriorParameters"
82+
#: Prior distribution type column in the parameter table
83+
PRIOR_DISTRIBUTION = "priorDistribution"
84+
#: Prior parameters column in the parameter table
85+
PRIOR_PARAMETERS = "priorParameters"
9086

9187
#: Mandatory columns of parameter table
9288
PARAMETER_DF_REQUIRED_COLS = [
@@ -101,10 +97,8 @@
10197
PARAMETER_DF_OPTIONAL_COLS = [
10298
PARAMETER_NAME,
10399
NOMINAL_VALUE,
104-
INITIALIZATION_PRIOR_TYPE,
105-
INITIALIZATION_PRIOR_PARAMETERS,
106-
OBJECTIVE_PRIOR_TYPE,
107-
OBJECTIVE_PRIOR_PARAMETERS,
100+
PRIOR_DISTRIBUTION,
101+
PRIOR_PARAMETERS,
108102
]
109103

110104
#: Parameter table columns
@@ -193,35 +187,50 @@
193187

194188
# NOISE MODELS
195189

196-
#: Uniform distribution
197-
UNIFORM = "uniform"
198-
#: Uniform distribution on the parameter scale
199-
PARAMETER_SCALE_UNIFORM = "parameterScaleUniform"
200-
#: Normal distribution
201-
NORMAL = "normal"
202-
#: Normal distribution on the parameter scale
203-
PARAMETER_SCALE_NORMAL = "parameterScaleNormal"
190+
191+
#: Cauchy distribution.
192+
CAUCHY = "cauchy"
193+
#: Chi-squared distribution.
194+
# TODO: "chisquare" in PEtab and sbml-distrib, but usually "chi-squared"
195+
CHI_SQUARED = "chisquare"
196+
#: Exponential distribution.
197+
EXPONENTIAL = "exponential"
198+
#: Gamma distribution.
199+
GAMMA = "gamma"
204200
#: Laplace distribution
205201
LAPLACE = "laplace"
206-
#: Laplace distribution on the parameter scale
207-
PARAMETER_SCALE_LAPLACE = "parameterScaleLaplace"
208-
#: Log-normal distribution
209-
LOG_NORMAL = "logNormal"
202+
#: Log10-normal distribution.
203+
LOG10_NORMAL = "log10-normal"
210204
#: Log-Laplace distribution
211-
LOG_LAPLACE = "logLaplace"
205+
LOG_LAPLACE = "log-laplace"
206+
#: Log-normal distribution
207+
LOG_NORMAL = "log-normal"
208+
#: Log-uniform distribution.
209+
LOG_UNIFORM = "log-uniform"
210+
#: Normal distribution
211+
NORMAL = "normal"
212+
#: Rayleigh distribution.
213+
RAYLEIGH = "rayleigh"
214+
#: Uniform distribution
215+
UNIFORM = "uniform"
212216

213-
#: Supported prior types
214-
PRIOR_TYPES = [
215-
UNIFORM,
216-
NORMAL,
217+
#: Supported prior distribution types
218+
PRIOR_DISTRIBUTIONS = [
219+
CAUCHY,
220+
CHI_SQUARED,
221+
EXPONENTIAL,
222+
GAMMA,
217223
LAPLACE,
218-
LOG_NORMAL,
224+
LOG10_NORMAL,
219225
LOG_LAPLACE,
220-
PARAMETER_SCALE_UNIFORM,
221-
PARAMETER_SCALE_NORMAL,
222-
PARAMETER_SCALE_LAPLACE,
226+
LOG_NORMAL,
227+
LOG_UNIFORM,
228+
NORMAL,
229+
RAYLEIGH,
230+
UNIFORM,
223231
]
224232

233+
225234
#: Supported noise distributions
226235
NOISE_MODELS = [NORMAL, LAPLACE]
227236

petab/v2/core.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -124,35 +124,41 @@ class NoiseDistribution(str, Enum):
124124
LAPLACE = C.LAPLACE
125125

126126

127-
class PriorType(str, Enum):
127+
class PriorDistribution(str, Enum):
128128
"""Prior types.
129129
130130
Prior types as used in the PEtab parameters table.
131131
"""
132132

133-
#: Normal distribution.
134-
NORMAL = C.NORMAL
133+
#: Cauchy distribution.
134+
CAUCHY = C.CAUCHY
135+
#: Chi-squared distribution.
136+
CHI_SQUARED = C.CHI_SQUARED
137+
#: Exponential distribution.
138+
EXPONENTIAL = C.EXPONENTIAL
139+
#: Gamma distribution.
140+
GAMMA = C.GAMMA
135141
#: Laplace distribution.
136142
LAPLACE = C.LAPLACE
137-
#: Uniform distribution.
138-
UNIFORM = C.UNIFORM
139-
#: Log-normal distribution.
140-
LOG_NORMAL = C.LOG_NORMAL
143+
#: Log10-normal distribution.
144+
LOG10_NORMAL = C.LOG10_NORMAL
141145
#: Log-Laplace distribution
142146
LOG_LAPLACE = C.LOG_LAPLACE
143-
PARAMETER_SCALE_NORMAL = C.PARAMETER_SCALE_NORMAL
144-
PARAMETER_SCALE_LAPLACE = C.PARAMETER_SCALE_LAPLACE
145-
PARAMETER_SCALE_UNIFORM = C.PARAMETER_SCALE_UNIFORM
146-
147+
#: Log-normal distribution.
148+
LOG_NORMAL = C.LOG_NORMAL
149+
#: Log-uniform distribution.
150+
LOG_UNIFORM = C.LOG_UNIFORM
151+
#: Normal distribution.
152+
NORMAL = C.NORMAL
153+
#: Rayleigh distribution.
154+
RAYLEIGH = C.RAYLEIGH
155+
#: Uniform distribution.
156+
UNIFORM = C.UNIFORM
147157

148-
#: Objective prior types as used in the PEtab parameters table.
149-
ObjectivePriorType = PriorType
150-
#: Initialization prior types as used in the PEtab parameters table.
151-
InitializationPriorType = PriorType
152158

153-
assert set(C.PRIOR_TYPES) == {e.value for e in ObjectivePriorType}, (
154-
"ObjectivePriorType enum does not match C.PRIOR_TYPES: "
155-
f"{set(C.PRIOR_TYPES)} vs { {e.value for e in ObjectivePriorType} }"
159+
assert set(C.PRIOR_DISTRIBUTIONS) == {e.value for e in PriorDistribution}, (
160+
"PriorType enum does not match C.PRIOR_TYPES: "
161+
f"{set(C.PRIOR_DISTRIBUTIONS)} vs { {e.value for e in PriorDistribution} }"
156162
)
157163

158164

@@ -840,15 +846,12 @@ class Parameter(BaseModel):
840846
ub: float | None = Field(alias=C.UPPER_BOUND, default=None)
841847
#: Nominal value.
842848
nominal_value: float | None = Field(alias=C.NOMINAL_VALUE, default=None)
843-
#: Parameter scale.
844-
# TODO: keep or remove?
845-
scale: ParameterScale = Field(
846-
alias=C.PARAMETER_SCALE, default=ParameterScale.LIN
847-
)
848-
# TODO: change to bool in PEtab, or serialize as 0/1?
849-
# https://github.com/PEtab-dev/PEtab/discussions/610
850849
#: Is the parameter to be estimated?
851850
estimate: bool = Field(alias=C.ESTIMATE, default=True)
851+
#: Type of parameter prior distribution.
852+
prior_distribution: PriorDistribution | None = Field(
853+
alias=C.PRIOR_DISTRIBUTION, default=None
854+
)
852855

853856
# TODO priors
854857
# pydantic vs. petab.v1.priors.Prior

petab/v2/problem.py

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -813,14 +813,13 @@ def n_measurements(self) -> int:
813813
"""Number of measurements."""
814814
return len(self.measurement_table.measurements)
815815

816-
# TODO: update after implementing priors in `Parameter`
817816
@property
818817
def n_priors(self) -> int:
819818
"""Number of priors."""
820-
if OBJECTIVE_PRIOR_PARAMETERS not in self.parameter_df:
821-
return 0
822-
823-
return self.parameter_df[OBJECTIVE_PRIOR_PARAMETERS].notna().sum()
819+
return sum(
820+
p.prior_distribution is not None
821+
for p in self.parameter_table.parameters
822+
)
824823

825824
def validate(
826825
self, validation_tasks: list[ValidationTask] = None
@@ -944,10 +943,8 @@ def add_parameter(
944943
scale: str = None,
945944
lb: Number = None,
946945
ub: Number = None,
947-
init_prior_type: str = None,
948-
init_prior_pars: str | Sequence = None,
949-
obj_prior_type: str = None,
950-
obj_prior_pars: str | Sequence = None,
946+
prior_dist: str = None,
947+
prior_pars: str | Sequence = None,
951948
**kwargs,
952949
):
953950
"""Add a parameter to the problem.
@@ -959,11 +956,8 @@ def add_parameter(
959956
scale: The parameter scale
960957
lb: The lower bound of the parameter
961958
ub: The upper bound of the parameter
962-
init_prior_type: The type of the initialization prior distribution
963-
init_prior_pars: The parameters of the initialization prior
964-
distribution
965-
obj_prior_type: The type of the objective prior distribution
966-
obj_prior_pars: The parameters of the objective prior distribution
959+
prior_dist: The type of the prior distribution
960+
prior_pars: The parameters of the prior distribution
967961
kwargs: additional columns/values to add to the parameter table
968962
"""
969963
record = {
@@ -979,22 +973,12 @@ def add_parameter(
979973
record[LOWER_BOUND] = lb
980974
if ub is not None:
981975
record[UPPER_BOUND] = ub
982-
if init_prior_type is not None:
983-
record[INITIALIZATION_PRIOR_TYPE] = init_prior_type
984-
if init_prior_pars is not None:
985-
if not isinstance(init_prior_pars, str):
986-
init_prior_pars = PARAMETER_SEPARATOR.join(
987-
map(str, init_prior_pars)
988-
)
989-
record[INITIALIZATION_PRIOR_PARAMETERS] = init_prior_pars
990-
if obj_prior_type is not None:
991-
record[OBJECTIVE_PRIOR_TYPE] = obj_prior_type
992-
if obj_prior_pars is not None:
993-
if not isinstance(obj_prior_pars, str):
994-
obj_prior_pars = PARAMETER_SEPARATOR.join(
995-
map(str, obj_prior_pars)
996-
)
997-
record[OBJECTIVE_PRIOR_PARAMETERS] = obj_prior_pars
976+
if prior_dist is not None:
977+
record[PRIOR_DISTRIBUTION] = prior_dist
978+
if prior_pars is not None:
979+
if not isinstance(prior_pars, str):
980+
prior_pars = PARAMETER_SEPARATOR.join(map(str, prior_pars))
981+
record[PRIOR_PARAMETERS] = prior_pars
998982
record.update(kwargs)
999983

1000984
self.parameter_table += core.Parameter(**record)
@@ -1127,7 +1111,7 @@ def model_dump(self, **kwargs) -> dict[str, Any]:
11271111
'id': 'par',
11281112
'lb': 0.0,
11291113
'nominal_value': None,
1130-
'scale': <ParameterScale.LIN: 'lin'>,
1114+
'prior_distribution': None,
11311115
'ub': 1.0}]}
11321116
"""
11331117
res = {

0 commit comments

Comments
 (0)