leaspy.algo.personalize.abstract_personalize_algo module

class AbstractPersonalizeAlgo(settings: AlgorithmSettings)

Bases: AbstractAlgo

Abstract class for personalize algorithm. Estimation of individual parameters of a given Data file with a frozen model (already estimated, or loaded from known parameters).

Parameters:
settingsAlgorithmSettings

Settings of the algorithm.

Attributes:
namestr

Algorithm’s name.

seedint, optional

Algorithm’s seed (default None).

algo_parametersdict

Algorithm’s parameters.

Methods

load_parameters(parameters)

Update the algorithm's parameters by the ones in the given dictionary.

run(model, *args[, return_loss])

Main method, run the algorithm.

run_impl(model, dataset)

Main personalize function, wraps the abstract _get_individual_parameters() method.

set_output_manager(output_settings)

Set a FitOutputManager object for the run of the algorithm

deterministic: bool = False
family: str = 'personalize'
load_parameters(parameters: dict)

Update the algorithm’s parameters by the ones in the given dictionary. The keys in the io which does not belong to the algorithm’s parameters keys are ignored.

Parameters:
parametersdict

Contains the pairs (key, value) of the wanted parameters

Examples

>>> settings = leaspy.io.settings.algorithm_settings.AlgorithmSettings("mcmc_saem")
>>> my_algo = leaspy.algo.fit.tensor_mcmcsaem.TensorMCMCSAEM(settings)
>>> my_algo.algo_parameters
{'n_iter': 10000,
 'n_burn_in_iter': 9000,
 'eps': 0.001,
 'L': 10,
 'sampler_ind': 'Gibbs',
 'sampler_pop': 'Gibbs',
 'annealing': {'do_annealing': False,
  'initial_temperature': 10,
  'n_plateau': 10,
  'n_iter': 200}}
>>> parameters = {'n_iter': 5000, 'n_burn_in_iter': 4000}
>>> my_algo.load_parameters(parameters)
>>> my_algo.algo_parameters
{'n_iter': 5000,
 'n_burn_in_iter': 4000,
 'eps': 0.001,
 'L': 10,
 'sampler_ind': 'Gibbs',
 'sampler_pop': 'Gibbs',
 'annealing': {'do_annealing': False,
  'initial_temperature': 10,
  'n_plateau': 10,
  'n_iter': 200}}
name: str = None
output_manager: FitOutputManager | None
run(model: AbstractModel, *args, return_loss: bool = False, **extra_kwargs) Any

Main method, run the algorithm.

TODO fix proper abstract class method: input depends on algorithm… (esp. simulate != from others…)

Parameters:
modelAbstractModel

The used model.

datasetDataset

Contains all the subjects’ observations with corresponding timepoints, in torch format to speed up computations.

return_lossbool (default False), keyword only

Should the algorithm return main output and optional loss output as a 2-tuple?

Returns:
Depends on algorithm class: TODO change?
run_impl(model: AbstractModel, dataset: Dataset) Tuple[IndividualParameters, Tensor]

Main personalize function, wraps the abstract _get_individual_parameters() method.

Parameters:
modelAbstractModel

A subclass object of leaspy AbstractModel.

datasetDataset

Dataset object build with leaspy class objects Data, algo & model

Returns:
individual_parametersIndividualParameters

Contains individual parameters.

noise_stdfloat or torch.FloatTensor

The estimated noise (is a tensor if model.noise_model is 'gaussian_diagonal')

= \frac{1}{n_{visits} \times n_{dim}} \sqrt{\sum_{i, j \in [1, n_{visits}] \times [1, n_{dim}]} \varepsilon_{i,j}}

where \varepsilon_{i,j} = \left( f(\theta, (z_{i,j}), (t_{i,j})) - (y_{i,j}) \right)^2 , where \theta are the model’s fixed effect, (z_{i,j}) the model’s random effects, (t_{i,j}) the time-points and f the model’s estimator.

set_output_manager(output_settings: OutputsSettings) None

Set a FitOutputManager object for the run of the algorithm

Parameters:
output_settingsOutputsSettings

Contains the logs settings for the computation run (console print periodicity, plot periodicity …)

Examples

>>> from leaspy import AlgorithmSettings
>>> from leaspy.io.settings.outputs_settings import OutputsSettings
>>> from leaspy.algo.fit.tensor_mcmcsaem import TensorMCMCSAEM
>>> algo_settings = AlgorithmSettings("mcmc_saem")
>>> my_algo = TensorMCMCSAEM(algo_settings)
>>> settings = {'path': 'brouillons',
                'console_print_periodicity': 50,
                'plot_periodicity': 100,
                'save_periodicity': 50
                }
>>> my_algo.set_output_manager(OutputsSettings(settings))