Utilities#

Module with utilities for the mixture model package.

lymixture.utils.binom_pmf(k: ndarray, n: int, p: float) ndarray[source]#

Compute binomial PMF fast.

lymixture.utils.late_binomial(support: ndarray, p: float = 0.5) ndarray[source]#

Parametrized binomial distribution.

lymixture.utils.map_to_simplex(from_real: ndarray | list[float]) ndarray[source]#

Map from real numbers to simplex.

The result has one entry more than values. The method creates a simplex by adding a dimension which is fixed to zero Then the values are run through a softmax function to normalize them.

>>> real = [4, 3.5]
>>> map_to_simplex(real)
array([0.01127223, 0.61544283, 0.37328494])
lymixture.utils.map_to_real(from_simplex: ndarray | list[float]) ndarray[source]#

Map from simplex to real numbers.

>>> simplex = [0.01127223, 0.61544283, 0.37328494]
>>> np.allclose(map_to_real(simplex), [4, 3.5])
True
lymixture.utils.normalize(values: ndarray, axis: int, **isclose_kwargs: float | bool) ndarray[source]#

Normalize values to sum to 1 along axis.

Beyond normalizing, this function also sets values that are close to zero to the exact value of zero. For this, it passes all extra keyword arguments to numpy’s isclose function.

>>> normalize(np.array([0.1, 0.2, 0.7]), axis=0)    
array([0.1, 0.2, 0.7])
>>> normalize(np.array([1e-20, 0.3, 0.7]), axis=0)  
array([0. , 0.3, 0.7])
>>> normalize(np.array([1e-20, 0.3, 0.3]), axis=0)  
array([0. , 0.5, 0.5])
lymixture.utils.log_normalize(log_values: ndarray, axis: int) ndarray[source]#

Log-normalize log_values to sum to 1 along axis.

>>> log_norm = log_normalize(np.log([0.1, 0.2, 0.7]), axis=0)
>>> np.exp(np.logaddexp.reduce(log_norm, axis=0))   
np.float64(1.0)
lymixture.utils.harden(values: ndarray, axis: int) ndarray[source]#

Harden values to become a one-hot-encoding along the given axis.

>>> values = np.array(
...     [[0.1, 0.2, 0.7],
...      [0.3, 0.4, 0.3]]
... )
>>> harden(values, axis=1)   
array([[0, 0, 1],
       [0, 1, 0]])
>>> arr = np.array([[[0.84, 0.64, 0.3 , 0.23],
...                  [0.18, 0.31, 0.23, 0.54],
...                  [0.08, 0.05, 0.72, 0.09]],
...                 [[0.33, 0.43, 0.28, 0.54],
...                  [0.26, 0.48, 0.8 , 0.01],
...                  [0.45, 0.09, 0.64, 0.11]]])
>>> harden(arr, axis=2)      
array([[[1, 0, 0, 0],
        [0, 0, 0, 1],
        [0, 0, 1, 0]],
       [[0, 0, 0, 1],
        [0, 0, 1, 0],
        [0, 0, 1, 0]]])
>>> harden(np.array([0.1, 0.2, 0.3, 0.1]), axis=0)
array([0, 0, 1, 0])
lymixture.utils.join_with_resps(patient_data: DataFrame, num_components: int, resps: ndarray | None = None) DataFrame[source]#

Join patient data with empty responsibilities (and reset index).

lymixture.utils.one_slice(idx: int) slice[source]#

Return a slice that selects only one element at the given index.

Helpful if one wants to select one element, but return it as a list.

>>> l = [1, 2, 3, 4, 5]
>>> l[one_slice(2)]
[3]