leaspy.models.utilities ======================= .. py:module:: leaspy.models.utilities Functions --------- .. autoapisummary:: leaspy.models.utilities.tensorize_2D leaspy.models.utilities.val_to_tensor leaspy.models.utilities.serialize_tensor leaspy.models.utilities.is_array_like leaspy.models.utilities.tensorize_2D leaspy.models.utilities.val_to_tensor leaspy.models.utilities.serialize_tensor leaspy.models.utilities.is_array_like leaspy.models.utilities.tensor_to_list leaspy.models.utilities.compute_std_from_variance leaspy.models.utilities.compute_ind_param_mean_from_suff_stats_mixture leaspy.models.utilities.compute_ind_param_std_from_suff_stats_mixture leaspy.models.utilities.compute_ind_param_std_from_suff_stats_mixture_burn_in leaspy.models.utilities.compute_probs_from_state leaspy.models.utilities.compute_patient_slopes_distribution leaspy.models.utilities.compute_linear_regression_subjects leaspy.models.utilities.compute_patient_values_distribution leaspy.models.utilities.compute_patient_time_distribution leaspy.models.utilities.get_log_velocities leaspy.models.utilities.torch_round Module Contents --------------- .. py:function:: tensorize_2D(x, unsqueeze_dim, dtype=torch.float32) Convert a scalar or array_like into an, at least 2D, dtype tensor. :Parameters: **x** : scalar or array_like Element to be tensorized. **unsqueeze_dim** : :obj:`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: :class:`torch.Tensor`, at least 2D .. .. rubric:: Examples >>> tensorize_2D([1, 2], 0) == tensor([[1, 2]]) >>> tensorize_2D([1, 2], -1) == tensor([[1], [2]) .. !! processed by numpydoc !! .. py:function:: val_to_tensor(val, shape = None) .. py:function:: serialize_tensor(v, *, indent = '', sub_indent = '') Nice serialization of floats, torch tensors (or numpy arrays). .. !! processed by numpydoc !! .. py:function:: is_array_like(v) .. py:function:: tensorize_2D(x, unsqueeze_dim, dtype=torch.float32) Convert a scalar or array_like into an, at least 2D, dtype tensor. :Parameters: **x** : scalar or array_like Element to be tensorized. **unsqueeze_dim** : :obj:`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: :class:`torch.Tensor`, at least 2D .. .. rubric:: Examples >>> tensorize_2D([1, 2], 0) == tensor([[1, 2]]) >>> tensorize_2D([1, 2], -1) == tensor([[1], [2]) .. !! processed by numpydoc !! .. py:function:: val_to_tensor(val, shape = None) .. py:function:: serialize_tensor(v, *, indent = '', sub_indent = '') Nice serialization of floats, torch tensors (or numpy arrays). .. !! processed by numpydoc !! .. py:function:: is_array_like(v) .. py:function:: tensor_to_list(x) Convert input tensor to list. :Parameters: **x** : :obj:`list` or :obj:`torch.Tensor` Input tensor or list to be converted. :Returns: :obj:`list` List converted from tensor input, or original list if input was not a tensor. :Raises: :exc:`NotImplementedError` If the input is a `WeightedTensor`, as this functionality is not yet implemented. .. !! processed by numpydoc !! .. py:function:: compute_std_from_variance(variance, varname, tol = 1e-05) 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** : :obj:`torch.Tensor` The variance we would like to convert to a std-dev. **varname** : :obj:`str` The name of the variable. **tol** : :obj:`float`, 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: :exc:`.LeaspyConvergenceError` If the variance is less than the specified tolerance, indicating a convergence issue. .. !! processed by numpydoc !! .. py:function:: compute_ind_param_mean_from_suff_stats_mixture(state, *, ip_name) .. py:function:: compute_ind_param_std_from_suff_stats_mixture(state, ip_values, ip_sqr_values, *, ip_name, dim, **kws) .. py:function:: compute_ind_param_std_from_suff_stats_mixture_burn_in(state, *, ip_name) .. py:function:: compute_probs_from_state(state) .. py:function:: compute_patient_slopes_distribution(df, *, max_inds = None) Compute linear regression slopes and their standard deviations for each feature. :Parameters: **df** : :obj:`pd.DataFrame` DataFrame containing individual scores. **max_inds** : :obj:`int`, optional Restrict computation to first `max_inds` individuals. Default="None" :Returns: :obj:`Tuple`[:obj:`torch.Tensor`, :obj:`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 .. !! processed by numpydoc !! .. py:function:: compute_linear_regression_subjects(df, *, max_inds = None) Linear Regression on each feature to get intercept & slopes :Parameters: **df** : :obj:`pd.DataFrame` Contains the individual scores. **max_inds** : :obj:`int`, optional Restrict computation to first `max_inds` individuals. Default="None" :Returns: :obj: `Dict`[:obj:`str`, :obj:`pd.DataFrame`]: Dictionary with : - keys : feature names - values : DataFrame with : - index : Individual IDs - columns : 'intercept', 'slope' .. !! processed by numpydoc !! .. py:function:: compute_patient_values_distribution(df) Returns means and standard deviations for the features of the given dataset values. :Parameters: **df** : :obj:`pd.DataFrame` Contains the individual scores. :Returns: :obj: Tuple[:obj:`torch.Tensor`, :obj:`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 .. !! processed by numpydoc !! .. py:function:: compute_patient_time_distribution(df) Returns mu / sigma of given dataset times. :Parameters: **df** : :obj:`pd.DataFrame` Contains the individual scores. :Returns: :obj:`Tuple`[:obj:`torch.Tensor`, :obj:`torch.Tensor`]: Tuple with: - [0] : torch.Tensor - Mean of the times - [1] : torch.Tensor - Standard deviation of the times .. !! processed by numpydoc !! .. py:function:: get_log_velocities(velocities, features, *, min = 0.01) Get the log of the velocities, clamping them to `min` if negative. :Parameters: **velocities** : :obj:`torch.Tensor` The velocities to be clamped and logged. **features** : :obj:`List`[:obj:`str`] The names of the features corresponding to the velocities. **min** : :obj:`float`, optional The minimum value to clamp the velocities to. Default=1e-2 :Returns: :obj:`torch.Tensor` The log of the clamped velocities. :Raises: :obj:`Warning` If some negative velocities are provided. .. !! processed by numpydoc !! .. py:function:: torch_round(t, *, tol = 1 << 16) 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** : :obj:`torch.FloatTensor` The tensor to be rounded. **tol** : :obj:`float`, optional The tolerance factor controlling rounding precision (higher = finer rounding). Default=1 << 16 (65536). This corresponds to rounding to ~ 10**-4.8. :Returns: :obj:`torch.FloatTensor` The rounded tensor with the same shape as input `t`. .. !! processed by numpydoc !!