Skip to content

Commit d3b4e7f

Browse files
committed
review
1 parent c01f2fb commit d3b4e7f

File tree

2 files changed

+41
-41
lines changed

2 files changed

+41
-41
lines changed

petab/v1/distributions.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def __init__(
4141
if trunc == (-np.inf, np.inf):
4242
trunc = None
4343

44-
if trunc is not None and trunc[0] > trunc[1]:
44+
if trunc is not None and trunc[0] >= trunc[1]:
4545
raise ValueError(
4646
"The lower truncation limit must be smaller "
4747
"than the upper truncation limit."
@@ -83,7 +83,7 @@ def trunc_high(self) -> float:
8383
return self._trunc[1] if self._trunc else np.inf
8484

8585
def _exp(self, x: np.ndarray | float) -> np.ndarray | float:
86-
"""Exponentiate / undo the log transformation according.
86+
"""Exponentiate / undo the log transformation if applicable.
8787
8888
Exponentiate if a log transformation is applied to the distribution.
8989
Otherwise, return the input.
@@ -96,7 +96,7 @@ def _exp(self, x: np.ndarray | float) -> np.ndarray | float:
9696
return self._logbase**x
9797

9898
def _log(self, x: np.ndarray | float) -> np.ndarray | float:
99-
"""Apply the log transformation.
99+
"""Apply the log transformation if enabled.
100100
101101
Compute the log of x with the specified base if a log transformation
102102
is applied to the distribution. Otherwise, return the input.
@@ -108,7 +108,7 @@ def _log(self, x: np.ndarray | float) -> np.ndarray | float:
108108
return x
109109
return np.log(x) / np.log(self._logbase)
110110

111-
def sample(self, shape=None) -> np.ndarray:
111+
def sample(self, shape=None) -> np.ndarray | float:
112112
"""Sample from the distribution.
113113
114114
:param shape: The shape of the sample.
@@ -123,16 +123,16 @@ def sample(self, shape=None) -> np.ndarray:
123123
return sample
124124

125125
@abc.abstractmethod
126-
def _sample(self, shape=None) -> np.ndarray:
127-
"""Sample from the underlying distribution, accounting for truncation.
126+
def _sample(self, shape=None) -> np.ndarray | float:
127+
"""Sample from the underlying distribution.
128128
129129
:param shape: The shape of the sample.
130130
:return: A sample from the underlying distribution,
131-
before applying, e.g., the log transformation.
131+
before applying, e.g., the log transformation or truncation.
132132
"""
133133
...
134134

135-
def pdf(self, x):
135+
def pdf(self, x) -> np.ndarray | float:
136136
"""Probability density function at x.
137137
138138
:param x: The value at which to evaluate the PDF.
@@ -150,7 +150,7 @@ def pdf(self, x):
150150
)
151151

152152
@abc.abstractmethod
153-
def _pdf(self, x):
153+
def _pdf(self, x) -> np.ndarray | float:
154154
"""Probability density function of the underlying distribution at x.
155155
156156
:param x: The value at which to evaluate the PDF.
@@ -166,7 +166,7 @@ def logbase(self) -> bool | float:
166166
"""
167167
return self._logbase
168168

169-
def cdf(self, x):
169+
def cdf(self, x) -> np.ndarray | float:
170170
"""Cumulative distribution function at x.
171171
172172
:param x: The value at which to evaluate the CDF.
@@ -178,7 +178,7 @@ def cdf(self, x):
178178
self._cdf_transformed_untruncated(x) - self._cd_low
179179
) * self._truncation_normalizer
180180

181-
def _cdf_transformed_untruncated(self, x):
181+
def _cdf_transformed_untruncated(self, x) -> np.ndarray | float:
182182
"""Cumulative distribution function of the transformed, but untruncated
183183
distribution at x.
184184
@@ -187,7 +187,7 @@ def _cdf_transformed_untruncated(self, x):
187187
"""
188188
return self._cdf_untransformed_untruncated(self._log(x))
189189

190-
def _cdf_untransformed_untruncated(self, x):
190+
def _cdf_untransformed_untruncated(self, x) -> np.ndarray | float:
191191
"""Cumulative distribution function of the underlying
192192
(untransformed, untruncated) distribution at x.
193193
@@ -196,7 +196,7 @@ def _cdf_untransformed_untruncated(self, x):
196196
"""
197197
raise NotImplementedError
198198

199-
def _ppf_untransformed_untruncated(self, q):
199+
def _ppf_untransformed_untruncated(self, q) -> np.ndarray | float:
200200
"""Percent point function of the underlying
201201
(untransformed, untruncated) distribution at q.
202202
@@ -205,7 +205,7 @@ def _ppf_untransformed_untruncated(self, q):
205205
"""
206206
raise NotImplementedError
207207

208-
def _ppf_transformed_untruncated(self, q):
208+
def _ppf_transformed_untruncated(self, q) -> np.ndarray | float:
209209
"""Percent point function of the transformed, but untruncated
210210
distribution at q.
211211
@@ -214,7 +214,7 @@ def _ppf_transformed_untruncated(self, q):
214214
"""
215215
return self._exp(self._ppf_untransformed_untruncated(q))
216216

217-
def _inverse_transform_sample(self, shape):
217+
def _inverse_transform_sample(self, shape) -> np.ndarray | float:
218218
"""Generate an inverse transform sample from the transformed and
219219
truncated distribution.
220220
@@ -260,25 +260,25 @@ def __repr__(self):
260260
log = f", log={self._logbase}" if self._logbase else ""
261261
return f"Normal(loc={self._loc}, scale={self._scale}{trunc}{log})"
262262

263-
def _sample(self, shape=None):
263+
def _sample(self, shape=None) -> np.ndarray | float:
264264
return np.random.normal(loc=self._loc, scale=self._scale, size=shape)
265265

266-
def _pdf(self, x):
266+
def _pdf(self, x) -> np.ndarray | float:
267267
return norm.pdf(x, loc=self._loc, scale=self._scale)
268268

269-
def _cdf_untransformed_untruncated(self, x):
269+
def _cdf_untransformed_untruncated(self, x) -> np.ndarray | float:
270270
return norm.cdf(x, loc=self._loc, scale=self._scale)
271271

272-
def _ppf_untransformed_untruncated(self, q):
272+
def _ppf_untransformed_untruncated(self, q) -> np.ndarray | float:
273273
return norm.ppf(q, loc=self._loc, scale=self._scale)
274274

275275
@property
276-
def loc(self):
276+
def loc(self) -> float:
277277
"""The location parameter of the underlying distribution."""
278278
return self._loc
279279

280280
@property
281-
def scale(self):
281+
def scale(self) -> float:
282282
"""The scale parameter of the underlying distribution."""
283283
return self._scale
284284

@@ -311,16 +311,16 @@ def __repr__(self):
311311
log = f", log={self._logbase}" if self._logbase else ""
312312
return f"Uniform(low={self._low}, high={self._high}{log})"
313313

314-
def _sample(self, shape=None):
314+
def _sample(self, shape=None) -> np.ndarray | float:
315315
return np.random.uniform(low=self._low, high=self._high, size=shape)
316316

317-
def _pdf(self, x):
317+
def _pdf(self, x) -> np.ndarray | float:
318318
return uniform.pdf(x, loc=self._low, scale=self._high - self._low)
319319

320-
def _cdf_untransformed_untruncated(self, x):
320+
def _cdf_untransformed_untruncated(self, x) -> np.ndarray | float:
321321
return uniform.cdf(x, loc=self._low, scale=self._high - self._low)
322322

323-
def _ppf_untransformed_untruncated(self, q):
323+
def _ppf_untransformed_untruncated(self, q) -> np.ndarray | float:
324324
return uniform.ppf(q, loc=self._low, scale=self._high - self._low)
325325

326326

@@ -357,24 +357,24 @@ def __repr__(self):
357357
log = f", log={self._logbase}" if self._logbase else ""
358358
return f"Laplace(loc={self._loc}, scale={self._scale}{trunc}{log})"
359359

360-
def _sample(self, shape=None):
360+
def _sample(self, shape=None) -> np.ndarray | float:
361361
return np.random.laplace(loc=self._loc, scale=self._scale, size=shape)
362362

363-
def _pdf(self, x):
363+
def _pdf(self, x) -> np.ndarray | float:
364364
return laplace.pdf(x, loc=self._loc, scale=self._scale)
365365

366-
def _cdf_untransformed_untruncated(self, x):
366+
def _cdf_untransformed_untruncated(self, x) -> np.ndarray | float:
367367
return laplace.cdf(x, loc=self._loc, scale=self._scale)
368368

369-
def _ppf_untransformed_untruncated(self, q):
369+
def _ppf_untransformed_untruncated(self, q) -> np.ndarray | float:
370370
return laplace.ppf(q, loc=self._loc, scale=self._scale)
371371

372372
@property
373-
def loc(self):
373+
def loc(self) -> float:
374374
"""The location parameter of the underlying distribution."""
375375
return self._loc
376376

377377
@property
378-
def scale(self):
378+
def scale(self) -> float:
379379
"""The scale parameter of the underlying distribution."""
380380
return self._scale

petab/v1/priors.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,19 @@ def __repr__(self):
157157
)
158158

159159
@property
160-
def type(self):
160+
def type(self) -> str:
161161
return self._type
162162

163163
@property
164-
def parameters(self):
164+
def parameters(self) -> tuple:
165165
return self._parameters
166166

167167
@property
168-
def bounds(self):
168+
def bounds(self) -> tuple[float, float] | None:
169169
return self._bounds
170170

171171
@property
172-
def transformation(self):
172+
def transformation(self) -> str:
173173
return self._transformation
174174

175175
def sample(self, shape=None) -> np.ndarray:
@@ -183,11 +183,11 @@ def sample(self, shape=None) -> np.ndarray:
183183

184184
def _scale_sample(self, sample):
185185
"""Scale the sample to the parameter space"""
186-
# we also need to scale paramterScale* distributions, because
186+
# we also need to scale parameterScale* distributions, because
187187
# internally, they are handled as (unscaled) log-distributions
188188
return scale(sample, self.transformation)
189189

190-
def _clip_to_bounds(self, x):
190+
def _clip_to_bounds(self, x) -> np.ndarray | float:
191191
"""Clip `x` values to bounds.
192192
193193
:param x: The values to clip. Assumed to be on the parameter scale.
@@ -201,16 +201,16 @@ def _clip_to_bounds(self, x):
201201
)
202202

203203
@property
204-
def lb_scaled(self):
204+
def lb_scaled(self) -> float:
205205
"""The lower bound on the parameter scale."""
206206
return scale(self.bounds[0], self.transformation)
207207

208208
@property
209-
def ub_scaled(self):
209+
def ub_scaled(self) -> float:
210210
"""The upper bound on the parameter scale."""
211211
return scale(self.bounds[1], self.transformation)
212212

213-
def pdf(self, x):
213+
def pdf(self, x) -> np.ndarray | float:
214214
"""Probability density function at x.
215215
216216
:param x: The value at which to evaluate the PDF.
@@ -232,7 +232,7 @@ def pdf(self, x):
232232

233233
return self.distribution.pdf(x) * coeff
234234

235-
def neglogprior(self, x):
235+
def neglogprior(self, x) -> np.ndarray | float:
236236
"""Negative log-prior at x.
237237
238238
:param x: The value at which to evaluate the negative log-prior.

0 commit comments

Comments
 (0)