leaspy.models.utils.initialization.model_initialization module

compute_linregress_subjects(df: DataFrame, *, max_inds: int | None = None) Dict[str, DataFrame]

Linear Regression on each feature to get intercept & slopes

Parameters:
dfpd.DataFrame

Contains the individual scores (with nans).

max_indsint, optional (default None)

Restrict computation to first max_inds individuals.

Returns:
dict[feat_name: str, regress_params_per_subj: pandas.DataFrame]
compute_patient_slopes_distribution(df: DataFrame, *, max_inds: int | None = None)

Linear Regression on each feature to get slopes

Parameters:
dfpd.DataFrame

Contains the individual scores (with nans).

max_indsint, optional (default None)

Restrict computation to first max_inds individuals.

Returns:
slopes_mutorch.Tensor [n_features,]
slopes_sigmatorch.Tensor [n_features,]
compute_patient_time_distribution(df: DataFrame)

Returns mu / sigma of given dataset times.

Parameters:
dfpd.DataFrame

Contains the individual scores (with nans).

Returns:
meantorch.Tensor scalar
sigmatorch.Tensor scalar
compute_patient_values_distribution(df: DataFrame)

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

Parameters:
dfpd.DataFrame

Contains the individual scores (with nans).

Returns:
meanstorch.Tensor [n_features,]

One mean per feature.

stdtorch.Tensor [n_features,]

One standard deviation per feature.

get_lme_results(df: DataFrame, n_jobs=-1, *, with_random_slope_age=True, **lme_fit_kwargs)

Fit a LME on univariate (per feature) time-series (feature vs. patients’ ages with varying intercept & slope)

Parameters:
dfpd.DataFrame

Contains all the data (with nans)

n_jobsint

Number of jobs in parallel when multiple features to init Not used for now, buggy

with_random_slope_agebool (default True)

Has LME model a random slope per age (otherwise only a random intercept).

**lme_fit_kwargs

Kwargs passed to ‘lme_fit’ (such as force_independent_random_effects, default True)

Returns:
dict

{param: str -> param_values_for_ft: torch.Tensor(nb_fts, *shape_param)}

get_log_velocities(velocities: Tensor, features: List[str], *, min: float = 0.01) Tensor

Warn if some negative velocities are provided, clamp them to min and return their log.

initialize_deltas_ordinal(model, df: DataFrame, parameters: dict) None

Updates in-place initial values for the ordinal deltas parameters and initializes ordinal noise_model attributes.

Parameters:
modelAbstractModel

The model to initialize.

dfpd.DataFrame

Contains the individual scores (with nans).

parametersdict[str, torch.Tensor]

The parameters coming from the standard initialization of the logistic model

Returns:
parametersdict[str, torch.Tensor]

The updated parameters initialization, with new parameter deltas

initialize_linear(model, df: DataFrame, method)

Initialize the linear model’s group parameters.

Parameters:
modelAbstractModel

The model to initialize.

dfpd.DataFrame

Contains the individual scores (with nans).

methodstr

not used for now

Returns:
parametersdict [str, torch.Tensor]

Contains the initialized model’s group parameters. The parameters’ keys are ‘g’, ‘v0’, ‘betas’, ‘tau_mean’, ‘tau_std’, ‘xi_mean’, ‘xi_std’, ‘sources_mean’, ‘sources_std’.

initialize_logistic(model, df: DataFrame, method)

Initialize the logistic model’s group parameters.

Parameters:
modelAbstractModel

The model to initialize.

dfpd.DataFrame

Contains the individual scores (with nans).

methodstr
Must be one of:
  • 'default': initialize at mean.

  • 'random': initialize with a gaussian realization with same mean and variance.

Returns:
parametersdict [str, torch.Tensor]

Contains the initialized model’s group parameters. The parameters’ keys are ‘g’, ‘v0’, ‘betas’, ‘tau_mean’, ‘tau_std’, ‘xi_mean’, ‘xi_std’, ‘sources_mean’, ‘sources_std’.

Raises:
LeaspyInputError

If method is not handled

initialize_logistic_parallel(model, df, method)

Initialize the logistic parallel model’s group parameters.

Parameters:
modelAbstractModel

The model to initialize.

dfpd.DataFrame

Contains the individual scores (with nans).

methodstr
Must be one of:
  • 'default': initialize at mean.

  • 'random': initialize with a gaussian realization with same mean and variance.

Returns:
parametersdict [str, torch.Tensor]

Contains the initialized model’s group parameters. The parameters’ keys are ‘g’, ‘tau_mean’, ‘tau_std’, ‘xi_mean’, ‘xi_std’, ‘sources_mean’, ‘sources_std’, ‘deltas’ and ‘betas’.

Raises:
LeaspyInputError

If method is not handled

initialize_parameters(model, dataset, method='default') tuple

Initialize the model’s group parameters given its name & the scores of all subjects.

Under-the-hood it calls an initialization function dedicated for the model:

It is automatically called during Leaspy.fit().

Parameters:
modelAbstractModel

The model to initialize.

datasetDataset

Contains the individual scores.

methodstr
Must be one of:
  • 'default': initialize at mean.

  • 'random': initialize with a gaussian realization with same mean and variance.

Returns:
parametersdict [str, torch.Tensor]

Contains the initialized model’s group parameters.

Raises:
LeaspyInputError

If no initialization method is known for model type / method

linregress_against_time(s: Series) Dict[str, float]

Return intercept & slope of a linear regression of series values against time (present in series index).

lme_init(model, df: DataFrame, fact_std=1.0, **kwargs)

Initialize the model’s group parameters.

Parameters:
modelAbstractModel

The model to initialize (must be an univariate or multivariate linear or logistic manifold model).

dfpd.DataFrame

Contains the individual scores (with nans).

fact_stdfloat

Multiplicative factor to apply on std-dev (tau, xi, noise) found naively with LME

**kwargs

Additional kwargs passed to get_lme_results()

Returns:
parametersdict [str, torch.Tensor]

Contains the initialized model’s group parameters.

Raises:
LeaspyInputError

If model is not supported for this initialization