Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
dweindl committed Dec 11, 2024
1 parent c01f2fb commit d3b4e7f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
62 changes: 31 additions & 31 deletions petab/v1/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(
if trunc == (-np.inf, np.inf):
trunc = None

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

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

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

def sample(self, shape=None) -> np.ndarray:
def sample(self, shape=None) -> np.ndarray | float:
"""Sample from the distribution.
:param shape: The shape of the sample.
Expand All @@ -123,16 +123,16 @@ def sample(self, shape=None) -> np.ndarray:
return sample

@abc.abstractmethod
def _sample(self, shape=None) -> np.ndarray:
"""Sample from the underlying distribution, accounting for truncation.
def _sample(self, shape=None) -> np.ndarray | float:
"""Sample from the underlying distribution.
:param shape: The shape of the sample.
:return: A sample from the underlying distribution,
before applying, e.g., the log transformation.
before applying, e.g., the log transformation or truncation.
"""
...

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

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

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

def _cdf_transformed_untruncated(self, x):
def _cdf_transformed_untruncated(self, x) -> np.ndarray | float:
"""Cumulative distribution function of the transformed, but untruncated
distribution at x.
Expand All @@ -187,7 +187,7 @@ def _cdf_transformed_untruncated(self, x):
"""
return self._cdf_untransformed_untruncated(self._log(x))

def _cdf_untransformed_untruncated(self, x):
def _cdf_untransformed_untruncated(self, x) -> np.ndarray | float:
"""Cumulative distribution function of the underlying
(untransformed, untruncated) distribution at x.
Expand All @@ -196,7 +196,7 @@ def _cdf_untransformed_untruncated(self, x):
"""
raise NotImplementedError

def _ppf_untransformed_untruncated(self, q):
def _ppf_untransformed_untruncated(self, q) -> np.ndarray | float:
"""Percent point function of the underlying
(untransformed, untruncated) distribution at q.
Expand All @@ -205,7 +205,7 @@ def _ppf_untransformed_untruncated(self, q):
"""
raise NotImplementedError

def _ppf_transformed_untruncated(self, q):
def _ppf_transformed_untruncated(self, q) -> np.ndarray | float:
"""Percent point function of the transformed, but untruncated
distribution at q.
Expand All @@ -214,7 +214,7 @@ def _ppf_transformed_untruncated(self, q):
"""
return self._exp(self._ppf_untransformed_untruncated(q))

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

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

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

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

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

@property
def loc(self):
def loc(self) -> float:
"""The location parameter of the underlying distribution."""
return self._loc

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

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

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

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

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

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


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

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

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

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

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

@property
def loc(self):
def loc(self) -> float:
"""The location parameter of the underlying distribution."""
return self._loc

@property
def scale(self):
def scale(self) -> float:
"""The scale parameter of the underlying distribution."""
return self._scale
20 changes: 10 additions & 10 deletions petab/v1/priors.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,19 @@ def __repr__(self):
)

@property
def type(self):
def type(self) -> str:
return self._type

@property
def parameters(self):
def parameters(self) -> tuple:
return self._parameters

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

@property
def transformation(self):
def transformation(self) -> str:
return self._transformation

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

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

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

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

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

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

return self.distribution.pdf(x) * coeff

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

0 comments on commit d3b4e7f

Please sign in to comment.