leaspy.algo.base

This module defines the AlgorithmType, AlgorithmName and AbstractAlgo classes

Attributes

Classes

AlgorithmType

The type of the algorithms.

AlgorithmName

The available algorithms in Leaspy.

BaseAlgorithm

Base class containing common methods for all algorithm classes.

IterativeAlgorithm

Base class containing common methods for all algorithm classes.

Functions

get_algorithm_type(name)

Return the algorithm type.

get_algorithm_class(name)

Return the algorithm class.

algorithm_factory(settings)

Return the requested algorithm based on the provided settings.

Module Contents

ModelType
ReturnType
class AlgorithmType[source]

Bases: str, enum.Enum

The type of the algorithms.

FIT = 'fit'
PERSONALIZE = 'personalize'
SIMULATE = 'simulate'
class AlgorithmName[source]

Bases: str, enum.Enum

The available algorithms in Leaspy.

FIT_MCMC_SAEM = 'mcmc_saem'
FIT_LME = 'lme_fit'
PERSONALIZE_SCIPY_MINIMIZE = 'scipy_minimize'
PERSONALIZE_MEAN_POSTERIOR = 'mean_posterior'
PERSONALIZE_MODE_POSTERIOR = 'mode_posterior'
PERSONALIZE_CONSTANT = 'constant_prediction'
PERSONALIZE_LME = 'lme_personalize'
SIMULATE = 'simulate'
class BaseAlgorithm(settings)[source]

Bases: abc.ABC, Generic[ModelType, ReturnType]

Base class containing common methods for all algorithm classes.

Parameters:
settingsAlgorithmSettings

The specifications of the algorithm as a AlgorithmSettings instance.

Attributes:
nameAlgorithmName

Name of the algorithm.

familyAlgorithmType

Family of the algorithm.

deterministicbool

True, if and only if algorithm does not involve randomness. Setting a seed will have no effect on such algorithms.

algo_parametersdict

Contains the algorithm’s parameters. Those are controlled by the leaspy.algo.AlgorithmSettings.parameters class attribute.

seedint, optional

Seed used by numpy and torch.

Parameters:

settings (AlgorithmSettings)

name: AlgorithmName = None
family: AlgorithmType = None
deterministic: bool = False
seed
algo_parameters
output_manager = None
abstractmethod set_output_manager(output_settings)[source]
Parameters:

output_settings (OutputsSettings)

Return type:

None

run(model, dataset=None, **kwargs)[source]

Main method, run the algorithm.

Parameters:
modelBaseModel

The used model.

datasetDataset

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

Returns:
ReturnType:

Depends on algorithm class.

Parameters:
Return type:

ReturnType

See also

AbstractFitAlgo
AbstractPersonalizeAlgo
load_parameters(parameters)[source]

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

The keys in the input which does not belong to the algorithm’s parameters are ignored.

Parameters:
parametersdict

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

Parameters:

parameters (dict)

Examples

>>> from leaspy.algo import AlgorithmSettings, algorithm_factory, OutputsSettings
>>> my_algo = algorithm_factory(AlgorithmSettings("mcmc_saem"))
>>> my_algo.algo_parameters
{'progress_bar': True,
'n_iter': 10000,
'n_burn_in_iter': 9000,
'n_burn_in_iter_frac': 0.9,
'burn_in_step_power': 0.8,
'random_order_variables': True,
'sampler_ind': 'Gibbs',
'sampler_ind_params': {'acceptation_history_length': 25,
'mean_acceptation_rate_target_bounds': [0.2, 0.4],
'adaptive_std_factor': 0.1},
'sampler_pop': 'Gibbs',
'sampler_pop_params': {'random_order_dimension': True,
'acceptation_history_length': 25,
'mean_acceptation_rate_target_bounds': [0.2, 0.4],
'adaptive_std_factor': 0.1},
'annealing': {'do_annealing': False,
 'initial_temperature': 10,
 'n_plateau': 10,
 'n_iter': None,
 'n_iter_frac': 0.5}}
>>> parameters = {'n_iter': 5000, 'n_burn_in_iter': 4000}
>>> my_algo.load_parameters(parameters)
>>> my_algo.algo_parameters
{'progress_bar': True,
'n_iter': 5000,
'n_burn_in_iter': 4000,
'n_burn_in_iter_frac': 0.9,
'burn_in_step_power': 0.8,
'random_order_variables': True,
'sampler_ind': 'Gibbs',
'sampler_ind_params': {'acceptation_history_length': 25,
'mean_acceptation_rate_target_bounds': [0.2, 0.4],
'adaptive_std_factor': 0.1},
'sampler_pop': 'Gibbs',
'sampler_pop_params': {'random_order_dimension': True,
'acceptation_history_length': 25,
'mean_acceptation_rate_target_bounds': [0.2, 0.4],
'adaptive_std_factor': 0.1},
'annealing': {'do_annealing': False,
 'initial_temperature': 10,
 'n_plateau': 10,
 'n_iter': None,
 'n_iter_frac': 0.5}}
class IterativeAlgorithm(settings)[source]

Bases: BaseAlgorithm[ModelType, ReturnType]

Base class containing common methods for all algorithm classes.

Parameters:
settingsAlgorithmSettings

The specifications of the algorithm as a AlgorithmSettings instance.

Attributes:
nameAlgorithmName

Name of the algorithm.

familyAlgorithmType

Family of the algorithm.

deterministicbool

True, if and only if algorithm does not involve randomness. Setting a seed will have no effect on such algorithms.

algo_parametersdict

Contains the algorithm’s parameters. Those are controlled by the leaspy.algo.AlgorithmSettings.parameters class attribute.

seedint, optional

Seed used by numpy and torch.

Parameters:

settings (AlgorithmSettings)

current_iteration: int = 0
get_algorithm_type(name)[source]

Return the algorithm type.

Parameters:
namestr or AlgorithmName

The name of the algorithm.

Returns:
algorithm type: leaspy.algo.AlgorithmType
Parameters:

name (Union[str, AlgorithmName])

Return type:

AlgorithmType

get_algorithm_class(name)[source]

Return the algorithm class.

Parameters:
namestr or AlgorithmName

The name of the algorithm.

Returns:
algorithm class: BaseAlgorithm
Parameters:

name (Union[str, AlgorithmName])

Return type:

Type[BaseAlgorithm]

algorithm_factory(settings)[source]

Return the requested algorithm based on the provided settings.

Parameters:
settingsleaspy.algo.AlgorithmSettingss

The algorithm settings.

Returns:
algorithmchild class of BaseAlgorithm

The requested algorithm. If it exists, it will be compatible with algorithm family.

Parameters:

settings (AlgorithmSettings)

Return type:

BaseAlgorithm