leaspy.models.utilities¶
Functions¶
|
Convert a scalar or array_like into an, at least 2D, dtype tensor. |
|
|
|
Nice serialization of floats, torch tensors (or numpy arrays). |
|
Convert a scalar or array_like into an, at least 2D, dtype tensor. |
|
|
|
Nice serialization of floats, torch tensors (or numpy arrays). |
Convert input tensor to list. |
|
|
Check that variance is strictly positive and return its square root, otherwise fail with a convergence error. |
|
|
|
|
|
|
|
Compute linear regression slopes and their standard deviations for each feature. |
|
Linear Regression on each feature to get intercept & slopes |
Returns means and standard deviations for the features of the given dataset values. |
|
Returns mu / sigma of given dataset times. |
|
|
Get the log of the velocities, clamping them to min if negative. |
|
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_dim
int 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:
Examples
>>> tensorize_2D([1, 2], 0) == tensor([[1, 2]]) >>> tensorize_2D([1, 2], -1) == tensor([[1], [2])
- serialize_tensor(v, *, indent='', sub_indent='')[source]¶
Nice serialization of floats, torch tensors (or numpy arrays).
- 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_dim
int 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:
Examples
>>> tensorize_2D([1, 2], 0) == tensor([[1, 2]]) >>> tensorize_2D([1, 2], -1) == tensor([[1], [2])
- serialize_tensor(v, *, indent='', sub_indent='')[source]¶
Nice serialization of floats, torch tensors (or numpy arrays).
- tensor_to_list(x)[source]¶
Convert input tensor to list.
- Parameters:
- x
listortorch.Tensor Input tensor or list to be converted.
- x
- Returns:
listList converted from tensor input, or original list if input was not a tensor.
- Raises:
NotImplementedErrorIf the input is a WeightedTensor, as this functionality is not yet implemented.
- Parameters:
- Return type:
- 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:
- variance
torch.Tensor The variance we would like to convert to a std-dev.
- varname
str The name of the variable.
- tol
float, optional The lower bound on variance, under which the converge error is raised. Default=1e-5.
- variance
- Returns:
- obj:
torch.Tensor The standard deviation from the variance.
- Raises:
LeaspyConvergenceErrorIf the variance is less than the specified tolerance, indicating a convergence issue.
- Parameters:
- Return type:
- compute_ind_param_std_from_suff_stats_mixture(state, ip_values, ip_sqr_values, *, ip_name, dim, **kws)[source]¶
- compute_patient_slopes_distribution(df, *, max_inds=None)[source]¶
Compute linear regression slopes and their standard deviations for each feature.
- Parameters:
- df
pd.DataFrame DataFrame containing individual scores.
- max_inds
int, optional Restrict computation to first max_inds individuals. Default=”None”
- df
- 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:
- compute_linear_regression_subjects(df, *, max_inds=None)[source]¶
Linear Regression on each feature to get intercept & slopes
- Parameters:
- df
pd.DataFrame Contains the individual scores.
- max_inds
int, optional Restrict computation to first max_inds individuals. Default=”None”
- df
- Returns:
- obj:
Dict`[:obj:`str,
pd.DataFrame]: Dictionary with : - keys : feature names - values : DataFrame with :index : Individual IDs
columns : ‘intercept’, ‘slope’
- Parameters:
- Return type:
- compute_patient_values_distribution(df)[source]¶
Returns means and standard deviations for the features of the given dataset values.
- Parameters:
- df
pd.DataFrame Contains the individual scores.
- df
- 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:
- compute_patient_time_distribution(df)[source]¶
Returns mu / sigma of given dataset times.
- Parameters:
- df
pd.DataFrame Contains the individual scores.
- df
- 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:
- get_log_velocities(velocities, features, *, min=0.01)[source]¶
Get the log of the velocities, clamping them to min if negative.
- Parameters:
- velocities
torch.Tensor The velocities to be clamped and logged.
- features
List`[:obj:`str] The names of the features corresponding to the velocities.
- min
float, optional The minimum value to clamp the velocities to. Default=1e-2
- velocities
- Returns:
torch.TensorThe log of the clamped velocities.
- Raises:
WarningIf some negative velocities are provided.
- Parameters:
- Return type:
- 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:
- t
torch.FloatTensor The tensor to be rounded.
- tol
float, optional The tolerance factor controlling rounding precision (higher = finer rounding). Default=1 << 16 (65536). This corresponds to rounding to ~ 10**-4.8.
- t
- Returns:
torch.FloatTensorThe rounded tensor with the same shape as input t.
- Parameters:
t (torch.FloatTensor)
tol (float)
- Return type:
torch.FloatTensor