Utils

This module provides utils that can be used in processes.

class miscore.utils.WeibullDistribution(scale=None, shape=None, mean=None)[source]

Weibull distribution. Can be used as follows.

>>> wd = WeibullDistribution(mean=1, shape=2)
>>> wd.mean
1.0
>>> wd.variance
0.27323954473516254
>>> wd(0.2)
1.431502705169328

See https://en.wikipedia.org/wiki/Weibull_distribution for further details.

Parameters:
Raises:
  • ValueError – WeibullDistribution should be initialized with one of the following combinations: ‘scale’ and ‘shape’; ‘mean’ and ‘shape’.

  • AssertionError – Scale should be positive.

  • AssertionError – Shape should be positive.

property mean: float

Returns the mean of the distribution.

Returns:

The mean of the distribution

property variance: float

Returns the variance of the distribution.

Returns:

The variance of the distribution

__call__(r)[source]

Draws a value from the distribution using the quantile (inverse cumulative distribution) function.

Parameters:

r (float) – A (random) number in [0, 1].

Return type:

float

Returns:

The value drawn from the distribution

Raises:

ValueError – Raised when r is not in [0, 1]”

class miscore.utils.PiecewiseLinear(data, cumulative=False, weigh=False)[source]

A piecewise linear function. Can be used as follows:

>>> data = [(0, 3.0), (0.2, 4.0), (1.0, 5.5)]
>>> pl = PiecewiseLinear(data)
>>> pl(0.1)
3.5
>>> pl.array([0.1, 0.5, 1.0])
array([3.5   , 4.5625, 5.5   ])

Or alternatively, with a pandas Series:

>>> np_data = np.array(data, dtype=np.float64)
>>> pd_data = pd.Series(np_data[:, 0], index=np_data[:, 1])
>>> pl = PiecewiseLinear(pd_data)
>>> pl(0.1)
3.5

See https://en.wikipedia.org/wiki/Piecewise_linear_function for further details.

Parameters:
  • data (Sequence[Tuple[SupportsFloat, SupportsFloat]] | Series) – The data to construct the piecewise linear function with. Should be a sequence of sequences, or a pandas Series object, with the x values as Index. The latter sequences should each have two elements: an x-value and a y-value. Thus, an Nx2 matrix is valid input.

  • cumulative (bool) – Whether or not the y-values should be converted to their cumulative sums. Defaults to False.

  • weigh (bool) – Whether or not the y-values should be weighted with the first differences in x-values. Defaults to False.

Raises:
Data

Type hinting for the input data that can be used to create a PiecewiseLinear object.

alias of Sequence[Tuple[SupportsFloat, SupportsFloat]] | Series

__call__(x)[source]

Interpolate a single value.

Parameters:

x (float) – The value to interpolate.

Return type:

float

Returns:

The value obtained by interpolation.

array(x)[source]

Interpolate an array.

Parameters:

x (Sequence[float]) – The array to interpolate.

Return type:

ndarray

Returns:

The values obtained by interpolation.

reverse()[source]

Returns the reversed version of the PiecewiseLinear.

Returns:

The reversed version of the PiecewiseLinear.

inverse_integral(h)[source]

Inverse_integral solves for t in \(\int^t_0 F(s) ds = h\) with F(s) a PiecewiseLinear.

Parameters:

h (float) – Area under the curve.

Returns:

Returns upper bound of the integral.

Raises:
  • AssertionError – Inverse integral is not possible for this PiecewiseLinear. Either the first data point is not defined for x=0 or it has negative y values.

  • AssertionError – h must be equal to or greater than 0.

class miscore.utils.PiecewiseConstant(data, cumulative=False)[source]

A piecewise constant function. Can be used as follows:

>>> data = [(0, 3.0), (0.2, 4.0), (1.0, 5.5)]
>>> pc = PiecewiseConstant(data)
>>> pc(0.3)
4.0
>>> pc(-1)
3.0
>>> pc(2)
5.5

Or alternatively, with a pandas Series:

>>> np_data = np.array(data, dtype=np.float64)
>>> pd_data = pd.Series(np_data[:, 0], index=np_data[:, 1])
>>> pc = PiecewiseConstant(pd_data)
>>> pc(0.3)
4.0

See https://en.wikipedia.org/wiki/Step_function for further details.

Parameters:
  • data (Sequence[Tuple[SupportsFloat, SupportsFloat]] | Series) – The data to construct the piecewise constant function with. Should be a sequence of sequences, or a pandas Series object, with the x values as Index The latter sequences should each have two elements: an x-value and a y-value. Thus, an Nx2 matrix is valid input.

  • cumulative (bool) – Whether or not the y-values should be converted to their cumulative sums. Defaults to False.

Raises:
Data

Type hinting for the input data that can be used to create a PiecewiseConstant object.

alias of Sequence[Tuple[SupportsFloat, SupportsFloat]] | Series

__call__(x)[source]

Evaluate a single value.

Parameters:

x (float) – The value to evaluate.

Return type:

float

Returns:

The obtained value.

class miscore.utils.CategoricalDist(data)[source]

A categorical distribution. Can be used as follows:

>>> data = [(3, "a"), (0.5, None), (1, 3)]
>>> cd = CategoricalDist(data)
>>> # The sum of the weights is 3 + 0.5 + 1 = 4.5
>>> # 0.1 is smaller than 3 / 4.5, and therefore:
>>> cd(0.1)
'a'
>>> # 0.7 is in between 3/4.5 and (3+0.5)/4.5 and therefore:
>>> cd(0.7)
>>> # 1 is larger than (3+0.5)/4.5 and therefore:
>>> cd(1)
3

Or alternatively, with a pandas Series:

>>> idx = [x[1] for x in data]
>>> vals = [x[0] for x in data]
>>> pd_data = pd.Series(vals, index=idx)
>>> cd = CategoricalDist(pd_data)
>>> cd(0.1)
'a'

See https://en.wikipedia.org/wiki/Categorical_distribution for further details.

Parameters:

data (Sequence[Tuple[SupportsFloat, Any]] | Series) – The data to construct the categorical distribution with. Should be a sequence of sequences or a pandas Series object, with the weights as Index The latter sequences should each have two elements: a weight and a value.

Raises:
Data

Type hinting for the input data that can be used to create a CategoricalDist object.

alias of Sequence[Tuple[SupportsFloat, Any]] | Series

__call__(x)[source]

An initialized categorical distribution can be called with a value in the (closed) interval [0, 1]. The corresponding value will be returned.

Parameters:

x (float) – Value in [0, 1].

Returns:

The corresponding value.

class miscore.utils.InverseGeneralisedLogisticFunction(k, q, b, v, a=0.0, c=1.0)[source]

The inverse function of the generalised logistic function. Can be used as follows:

>>> func = InverseGeneralisedLogisticFunction(k=20, q=100, b=0.05, v=0.15)
>>> func(np.array([0.1]))
array([88.22693142])
>>> func(np.array([0.5]))
array([98.15153043])

See also: https://en.wikipedia.org/wiki/Generalised_logistic_function.

Parameters:
  • a (float) – The lower asymptote, defaults to 0.

  • k (float) – The upper asymptote.

  • b (float) – The growth rate.

  • v (float) – Affects near which asymptote maximum growth occurs.

  • q (float) – Is related to the value of Y(0).

  • c (float) – The upper asymptote is a + (k-a)/(c^{1/v}), defaults to 1.

Raises:
__call__(x)[source]

Evaluate multiple values with the inverse generalised logistic function.

Parameters:

x (ndarray) – The values to evaluate.

Return type:

ndarray

Returns:

The obtained values.