mowgli.utils#

mowgli.utils.compute_ground_cost(features, cost: str, eps: float, force_recompute: bool, cost_path: str, dtype: dtype, device: device) Tensor#

Compute the ground cost (not lazily!)

Parameters:
  • features (array-like) – A array with the features to compute the cost on.

  • cost (str) – The function to compute the cost. Scipy distances are allowed.

  • force_recompute (bool) – Recompute even is there is already a cost matrix saved at the provided path.

  • cost_path (str) – Where to look for or where to save the cost.

  • dtype (torch.dtype) – The dtype for the output.

  • device (torch.device) – The device for the ouput.

Returns:

The ground cost

Return type:

torch.Tensor

mowgli.utils.early_stop(history: List, tol: float, nonincreasing: bool = False) bool#

Based on a history and a tolerance, whether to stop early or not.

Parameters:
  • history (List) – The loss history.

  • tol (float) – The tolerance before early stopping.

  • nonincreasing (bool, optional) – When False, throws an error if the loss goes up. Defaults to False.

Raises:

ValueError – When the loss goes up.

Returns:

Whether to stop early.

Return type:

bool

mowgli.utils.entropy(X: Tensor, min_one: bool = False, rescale: bool = False) Tensor#

Entropy function, \(E(X) = \langle X, \log X - 1 angle\).

Args:
X (torch.Tensor):

The parameter to compute the entropy of.

min_one (bool, optional):

Whether to inclue the \(-1\) in the formula. Defaults to False.

rescale (bool, optional):

Rescale so that the value is between 0 and 1 (when min_one=False).

Returns:

torch.Tensor: The entropy of X.

mowgli.utils.entropy_dual_loss(Y: Tensor) Tensor#

Compute the Legendre dual of the entropy.

Parameters:

Y (torch.Tensor) – The input parameter.

Returns:

The loss.

Return type:

torch.Tensor

mowgli.utils.normalize_tensor(X: Tensor) Tensor#

Normalize a tensor along columns

Parameters:

X (torch.Tensor) – The tensor to normalize.

Returns:

The normalized tensor.

Return type:

torch.Tensor

mowgli.utils.ot_dual_loss(A: dict, G: dict, K: dict, eps: float, mod_weights: Tensor, dim=(0, 1)) Tensor#

Compute the Legendre dual of the entropic OT loss.

Parameters:
  • A (dict) – The input data.

  • G (dict) – The dual variable.

  • K (dict) – The kernel.

  • eps (float) – The entropic regularization.

  • mod_weights (torch.Tensor) – The weights per cell and modality.

  • dim (tuple, optional) – How to sum the loss. Defaults to (0, 1).

Returns:

The loss

Return type:

torch.Tensor

mowgli.utils.reference_dataset(X, dtype: dtype, device: device, keep_idx: Iterable) Tensor#

Select features, transpose dataset and convert to Tensor.

Parameters:
  • X (array-like) – The input data

  • dtype (torch.dtype) – The dtype to create

  • device (torch.device) – The device to create on

  • keep_idx (Iterable) – The variables to keep.

Returns:

The reference dataset A.

Return type:

torch.Tensor