leaspy.models.utilities

Functions

tensorize_2D(x, unsqueeze_dim[, dtype])

Convert a scalar or array_like into an, at least 2D, dtype tensor.

val_to_tensor(val[, shape])

serialize_tensor(v, *[, indent, sub_indent])

Nice serialization of floats, torch tensors (or numpy arrays).

is_array_like(v)

tensorize_2D(x, unsqueeze_dim[, dtype])

Convert a scalar or array_like into an, at least 2D, dtype tensor.

val_to_tensor(val[, shape])

serialize_tensor(v, *[, indent, sub_indent])

Nice serialization of floats, torch tensors (or numpy arrays).

is_array_like(v)

tensor_to_list(x)

Convert input tensor to list.

compute_std_from_variance(variance, varname[, tol])

Check that variance is strictly positive and return its square root, otherwise fail with a convergence error.

compute_ind_param_mean_from_suff_stats_mixture(state, ...)

compute_ind_param_std_from_suff_stats_mixture(state, ...)

compute_ind_param_std_from_suff_stats_mixture_burn_in(...)

compute_probs_from_state(state)

compute_patient_slopes_distribution(df, *[, max_inds])

Compute linear regression slopes and their standard deviations for each feature.

compute_linear_regression_subjects(df, *[, max_inds])

Linear Regression on each feature to get intercept & slopes

compute_patient_values_distribution(df)

Returns means and standard deviations for the features of the given dataset values.

compute_patient_time_distribution(df)

Returns mu / sigma of given dataset times.

get_log_velocities(velocities, features, *[, min])

Get the log of the velocities, clamping them to min if negative.

torch_round(t, *[, tol])

Multiplies the tensor by tol, applies standard rounding, then scales back.

Module Contents

tensorize_2D(x, unsqueeze_dim, dtype=torch.float32)[source]

Convert a scalar or array_like into an, at least 2D, dtype tensor.

Parameters:
xscalar or array_like

Element to be tensorized.

unsqueeze_dimint

Dimension to be unsqueezed (0 or -1). Meaningful for 1D array-like only (for scalar or vector of length 1 it has no matter).

Returns:
torch.Tensor, at least 2D
Parameters:

unsqueeze_dim (int)

Return type:

Tensor

Examples

>>> tensorize_2D([1, 2], 0) == tensor([[1, 2]])
>>> tensorize_2D([1, 2], -1) == tensor([[1], [2])
val_to_tensor(val, shape=None)[source]
Parameters:

shape (Optional[tuple])

serialize_tensor(v, *, indent='', sub_indent='')[source]

Nice serialization of floats, torch tensors (or numpy arrays).

Parameters:
  • indent (str)

  • sub_indent (str)

Return type:

str

is_array_like(v)[source]
Parameters:

v (Any)

Return type:

bool

tensorize_2D(x, unsqueeze_dim, dtype=torch.float32)[source]

Convert a scalar or array_like into an, at least 2D, dtype tensor.

Parameters:
xscalar or array_like

Element to be tensorized.

unsqueeze_dimint

Dimension to be unsqueezed (0 or -1). Meaningful for 1D array-like only (for scalar or vector of length 1 it has no matter).

Returns:
torch.Tensor, at least 2D
Parameters:

unsqueeze_dim (int)

Return type:

Tensor

Examples

>>> tensorize_2D([1, 2], 0) == tensor([[1, 2]])
>>> tensorize_2D([1, 2], -1) == tensor([[1], [2])
val_to_tensor(val, shape=None)[source]
Parameters:

shape (Optional[tuple])

serialize_tensor(v, *, indent='', sub_indent='')[source]

Nice serialization of floats, torch tensors (or numpy arrays).

Parameters:
  • indent (str)

  • sub_indent (str)

Return type:

str

is_array_like(v)[source]
Parameters:

v (Any)

Return type:

bool

tensor_to_list(x)[source]

Convert input tensor to list.

Parameters:
xlist or torch.Tensor

Input tensor or list to be converted.

Returns:
list

List converted from tensor input, or original list if input was not a tensor.

Raises:
NotImplementedError

If the input is a WeightedTensor, as this functionality is not yet implemented.

Parameters:

x (Union[list, Tensor])

Return type:

list

compute_std_from_variance(variance, varname, tol=1e-05)[source]

Check that variance is strictly positive and return its square root, otherwise fail with a convergence error. If variance is multivariate check that all components are strictly positive. TODO? a full Bayesian setting with good priors on all variables should prevent such convergence issues.

Parameters:
variancetorch.Tensor

The variance we would like to convert to a std-dev.

varnamestr

The name of the variable.

tolfloat, optional

The lower bound on variance, under which the converge error is raised. Default=1e-5.

Returns:
obj:

torch.Tensor The standard deviation from the variance.

Raises:
LeaspyConvergenceError

If the variance is less than the specified tolerance, indicating a convergence issue.

Parameters:
Return type:

Tensor

compute_ind_param_mean_from_suff_stats_mixture(state, *, ip_name)[source]
Parameters:
Return type:

Tensor

compute_ind_param_std_from_suff_stats_mixture(state, ip_values, ip_sqr_values, *, ip_name, dim, **kws)[source]
Parameters:
compute_ind_param_std_from_suff_stats_mixture_burn_in(state, *, ip_name)[source]
Parameters:
Return type:

Tensor

compute_probs_from_state(state)[source]
Parameters:

state (Dict[str, Tensor])

Return type:

Tensor

compute_patient_slopes_distribution(df, *, max_inds=None)[source]

Compute linear regression slopes and their standard deviations for each feature.

Parameters:
dfpd.DataFrame

DataFrame containing individual scores.

max_indsint, optional

Restrict computation to first max_inds individuals. Default=”None”

Returns:
Tuple`[:obj:`torch.Tensor, torch.Tensor]:

Tuple with : - [0] : torch.Tensor of shape (n_features,) - Regression slopes - [1] : torch.Tensor of shape (n_features,) - Standard deviation of the slopes

Parameters:
Return type:

Tuple[Tensor, Tensor]

compute_linear_regression_subjects(df, *, max_inds=None)[source]

Linear Regression on each feature to get intercept & slopes

Parameters:
dfpd.DataFrame

Contains the individual scores.

max_indsint, optional

Restrict computation to first max_inds individuals. Default=”None”

Returns:
obj:

Dict`[:obj:`str, pd.DataFrame]: Dictionary with : - keys : feature names - values : DataFrame with :

  • index : Individual IDs

  • columns : ‘intercept’, ‘slope’

Parameters:
Return type:

Dict[str, DataFrame]

compute_patient_values_distribution(df)[source]

Returns means and standard deviations for the features of the given dataset values.

Parameters:
dfpd.DataFrame

Contains the individual scores.

Returns:
obj:

Tuple[torch.Tensor, torch.Tensor]: Tuple with: - [0] : torch.Tensor of shape (n_features,) - Means of the features - [1] : torch.Tensor of shape (n_features,) - Standard deviations of the features

Parameters:

df (DataFrame)

Return type:

Tuple[Tensor, Tensor]

compute_patient_time_distribution(df)[source]

Returns mu / sigma of given dataset times.

Parameters:
dfpd.DataFrame

Contains the individual scores.

Returns:
Tuple`[:obj:`torch.Tensor, torch.Tensor]:

Tuple with: - [0] : torch.Tensor - Mean of the times - [1] : torch.Tensor - Standard deviation of the times

Parameters:

df (DataFrame)

Return type:

Tuple[Tensor, Tensor]

get_log_velocities(velocities, features, *, min=0.01)[source]

Get the log of the velocities, clamping them to min if negative.

Parameters:
velocitiestorch.Tensor

The velocities to be clamped and logged.

featuresList`[:obj:`str]

The names of the features corresponding to the velocities.

minfloat, optional

The minimum value to clamp the velocities to. Default=1e-2

Returns:
torch.Tensor

The log of the clamped velocities.

Raises:
Warning

If some negative velocities are provided.

Parameters:
Return type:

Tensor

torch_round(t, *, tol=1 << 16)[source]

Multiplies the tensor by tol, applies standard rounding, then scales back. This effectively rounds values to the nearest multiple of 1.0 / tol.

Parameters:
ttorch.FloatTensor

The tensor to be rounded.

tolfloat, optional

The tolerance factor controlling rounding precision (higher = finer rounding). Default=1 << 16 (65536). This corresponds to rounding to ~ 10**-4.8.

Returns:
torch.FloatTensor

The rounded tensor with the same shape as input t.

Parameters:
  • t (torch.FloatTensor)

  • tol (float)

Return type:

torch.FloatTensor