leaspy.algo.fit

Submodules

Classes

FitAlgorithm

Abstract class containing common method for all fit algorithm classes.

FitOutputManager

Class used by AbstractAlgo (and its child classes) to display & save plots and statistics during algorithm execution.

LMEFitAlgorithm

Fitting algorithm associated to LMEModel.

TensorMcmcSaemAlgorithm

Main algorithm for MCMC-SAEM.

Package Contents

class FitAlgorithm(settings)[source]

Bases: leaspy.algo.base.IterativeAlgorithm[leaspy.algo.base.ModelType, leaspy.algo.base.ReturnType]

Abstract class containing common method for all fit algorithm classes.

The algorithm is proven to converge if the sequence burn_in_step is positive, with an infinite sum \(\sum_k \epsilon_k = +\infty\) and a finite sum of the squares \(\sum_k \epsilon_k^2 < \infty\) (see following paper).

Construction of Bayesian Deformable Models via a Stochastic Approximation Algorithm: A Convergence Study

Parameters:
settingsAlgorithmSettings

The specifications of the algorithm as a AlgorithmSettings instance.

Attributes:
algorithm_devicestr

Valid torch.device

current_iterationint, default 0

The number of the current iteration. The first iteration will be 1 and the last one n_iter.

sufficient_statisticsdict [str, torch.Tensor] or None

Sufficient statistics of the previous step. It is None during all the burn-in phase.

output_managerFitOutputManager

Optional output manager of the algorithm

Inherited attributes

From AbstractAlgo

Parameters:

settings (AlgorithmSettings)

See also

leaspy.api.Leaspy.fit()
family
logs
sufficient_statistics: DictParamsTorch | None = None
set_output_manager(output_settings)[source]

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 …)

Parameters:

output_settings (OutputsSettings)

Return type:

None

Examples

>>> from leaspy.algo import AlgorithmSettings, algorithm_factory, OutputsSettings
>>> algo_settings = AlgorithmSettings("mcmc_saem")
>>> my_algo = algorithm_factory(algo_settings)
>>> settings = {
    'path': 'brouillons',
    'print_periodicity': 50,
    'plot_periodicity': 100,
    'save_periodicity': 50
}
>>> my_algo.set_output_manager(OutputsSettings(settings))
class FitOutputManager(outputs)[source]

Class used by AbstractAlgo (and its child classes) to display & save plots and statistics during algorithm execution.

Parameters:
outputsOutputsSettings

Initialize the FitOutputManager class attributes, like the logs paths, the console print periodicity and so forth.

Attributes:
path_outputstr

Path of the folder containing all the outputs

path_plotstr

Path of the subfolder of path_output containing the logs plots

path_plot_convergence_model_parametersstr

Path of the first plot of the convergence of the model’s parameters (in the subfolder path_plot)

path_plot_patientsstr

Path of the subfolder of path_plot containing the plot of the reconstruction of the patients’ longitudinal trajectory by the model

nb_of_patients_to_plotint

Number of patients for whom the reconstructions will be plotted.

path_save_model_parameters_convergencestr

Path of the subfolder of path_output containing the progression of the model’s parameters convergence

periodicity_plotint (default 100)

Set the frequency of the display of the plots

periodicity_printint

Set the frequency of the display of the statistics

periodicity_saveint

Set the frequency of the saves of the model’s parameters

periodicity_plot_patientsint

Set the frequency of the saves of the patients’ reconstructions

plot_sourcewisebool

If True, plots will be generated for each source separately.

periodicity_print
periodicity_save
periodicity_plot
nb_of_patients_to_plot
periodicity_plot_patients
plot_sourcewise
time
iteration(algo, model, data)[source]

Call methods to save state of the running computation, display statistics & plots if the current iteration is a multiple of periodicity_print, periodicity_plot or periodicity_save

Parameters:
algoFitAlgo

A fitting algorithm.

modelMcmcSaemCompatibleModel

The model used by the computation.

dataDataset

The data used by the computation

Parameters:
Return type:

None

print_time()[source]

Prints the duration since the last periodic point.

print_model_statistics(model)[source]

Prints model’s statistics.

Parameters:
modelMcmcSaemCompatibleModel

The model used by the computation

Parameters:

model (McmcSaemCompatibleModel)

print_algo_statistics(algo)[source]

Prints algorithm’s statistics

Parameters:
algoFitAlgo

A fitting algorithm.

Parameters:

algo (FitAlgorithm)

save_model_parameters_convergence(iteration, model)[source]

Saves the current state of the model’s parameters

Parameters:
iterationint

The current iteration.

modelMcmcSaemCompatibleModel

The model used by the computation

Parameters:
Return type:

None

save_plot_convergence_model_parameters(model)[source]

Saves figures of the model parameters’ convergence in multiple pages of a PDF.

Parameters:
modelMcmcSaemCompatibleModel

The model used by the computation

Parameters:

model (McmcSaemCompatibleModel)

save_plot_patient_reconstructions(iteration, model, data)[source]

Saves figures of real longitudinal values and their reconstructions computed by the model for maximum 5 patients during each iteration.

Parameters:
iterationint

The current iteration

modelMcmcSaemCompatibleModel

The model used by the computation

dataDataset

The dataset used by the computation

Parameters:
Return type:

None

class LMEFitAlgorithm(settings)[source]

Bases: leaspy.algo.fit.base.FitAlgorithm[leaspy.models.LMEModel, tuple]

Fitting algorithm associated to LMEModel.

Parameters:
settingsAlgorithmSettings
Parameters:

settings (AlgorithmSettings)

name: AlgorithmName
force_independent_random_effects
sm_fit_parameters
set_output_manager(output_settings)[source]

Not implemented.

Parameters:

output_settings (OutputsSettings)

Return type:

None

class TensorMcmcSaemAlgorithm(settings)[source]

Bases: leaspy.algo.algo_with_device.AlgorithmWithDeviceMixin, leaspy.algo.algo_with_annealing.AlgorithmWithAnnealingMixin, leaspy.algo.algo_with_samplers.AlgorithmWithSamplersMixin, leaspy.algo.fit.base.FitAlgorithm[leaspy.models.McmcSaemCompatibleModel, leaspy.variables.state.State]

Main algorithm for MCMC-SAEM.

Parameters:
settingsAlgorithmSettings

MCMC fit algorithm settings

Attributes:
samplersdict [str, AbstractSampler ]

Dictionary of samplers per each variable

random_order_variablesbool (default True)

This attribute controls whether we randomize the order of variables at each iteration. Article gives a reason on why we should activate this flag.

temperaturefloat
temperature_invfloat

Temperature and its inverse are modified during algorithm if annealing is used

Parameters:

settings (AlgorithmSettings)

See also

leaspy.samplers
name: AlgorithmName
log_current_iteration(state)[source]
Parameters:

state (State)

is_current_iteration_in_last_n()[source]

Return True if current iteration is within the last n realizations defined in logging settings.

should_current_iteration_be_saved()[source]

Return True if current iteration should be saved based on log saving periodicity.