leaspy.models.base¶
Classes¶
A structured summary of a Leaspy model. |
|
Possible initialization methods for Leaspy models. |
|
This is the public interface for Leaspy models. |
|
Base model class from which all |
Module Contents¶
- class Summary[source]¶
A structured summary of a Leaspy model.
Provides programmatic access to model metadata, parameters, and training statistics. Calling model.summary() prints the summary; storing it in a variable allows access to individual attributes.
Use s.help() to see all available attributes and usage examples.
Examples
>>> model.summary() # Prints the formatted summary >>> s = model.summary() # Store to access attributes >>> s.algorithm # 'mcmc_saem' >>> s.seed # 42 >>> s.n_subjects # 150 >>> s.get_param('tau_std') # tensor([10.5]) >>> s.help() # Show all available attributes
- property sources: list[str] | None¶
List of source names (e.g., [‘s0’, ‘s1’]) or None if not applicable.
- property clusters: list[str] | None¶
List of cluster names (e.g., [‘c0’, ‘c1’]) or None if not applicable.
- property converged: bool | None¶
Whether the training algorithm converged.
- Return type:
Optional[bool]
- property n_subjects: int | None¶
Number of subjects in the training dataset.
- Return type:
Optional[int]
- property n_visits: int | None¶
Total number of visits in the training dataset.
- Return type:
Optional[int]
- property n_observations: int | None¶
Total number of observations in the training dataset.
- Return type:
Optional[int]
- get_param(name)[source]¶
Get a parameter value by name, searching across all categories.
- Parameters:
- namestr
The parameter name (e.g., ‘betas_mean’, ‘tau_std’, ‘noise_std’).
- Returns:
- The parameter value (typically a torch.Tensor), or None if not found.
- Parameters:
name (str)
- Return type:
Optional[Any]
- class InitializationMethod[source]¶
-
Possible initialization methods for Leaspy models.
- DEFAULT = 'default'¶
- RANDOM = 'random'¶
- class ModelInterface[source]¶
Bases:
abc.ABCThis is the public interface for Leaspy models. It defines the methods and properties that all models should implement. It is not meant to be instantiated directly, but rather to be inherited by concrete model classes.
- property is_initialized: bool¶
- Abstractmethod:
- Return type:
True if the model is initialized, False otherwise.
- property features: list[FeatureType]¶
- Abstractmethod:
- Return type:
List of model features (None if not initialization).
- Raises:
- NotImplementedError
- Return type:
- property parameters: DictParamsTorch¶
- Abstractmethod:
- Return type:
Dictionary of values for model parameters.
- Returns:
- Raises:
- NotImplementedError
- Return type:
- property hyperparameters: DictParamsTorch¶
- Abstractmethod:
- Return type:
Dictionary of values for model hyperparameters.
- Returns:
- Raises:
- NotImplementedError
- Return type:
- abstractmethod fit(data=None, algorithm=None, algorithm_settings=None, algorithm_settings_path=None, **kwargs)[source]¶
Estimate the model’s parameters \(\theta\) for a given dataset and a given algorithm.
These model’s parameters correspond to the fixed-effects of the mixed-effects model.
There are three ways to provide parameters to the fitting algorithm:
By providing an instance of
AlgorithmSettingsBy providing a path to a serialized
AlgorithmSettingsBy providing the algorithm name and parameters directly
If settings are provided in multiple ways, the order above will prevail.
- Parameters:
- data
pd.DataFrameorDataorDataset, optional Contains the information of the individuals, in particular the time-points \((t_{i,j})\) and the observations \((y_{i,j})\).
- algorithm
strorAlgorithmName,optional The name of the algorithm to use.
Note
Use this if you want to provide algorithm settings through kwargs.
- algorithm_settings
AlgorithmSettings, optional The algorithm settings to use.
Note
Use this if you want to customize algorithm settings through the
AlgorithmSettingsclass. If provided, the fit will rely on these settings.- algorithm_settings_path
strorPath, optional The path to the algorithm settings file.
Note
If provided, the settings from the file will be used instead of the settings provided through kwarsg.
- **kwargs
dict Contains the algorithm’s settings.
- data
- Raises:
- NotImplementedError
- Parameters:
data (Optional[Union[DataFrame, leaspy.io.data.dataset.Data, Dataset]])
algorithm (Optional[Union[str, AlgorithmName]])
algorithm_settings (Optional[AlgorithmSettings])
Examples
Fit a logistic model on a longitudinal dataset, display the group parameters
>>> from leaspy.models import LogisticModel >>> from leaspy.datasets import load_dataset >>> putamen_df = load_dataset("parkinson-putamen") >>> model = LogisticModel(name="test-model-logistic") >>> model.fit(putamen_df, "mcmc_saem", seed=0, print_periodicity=50) >>> print(model) === MODEL === betas_mean : [] log_g_mean : [-0.8394] log_v0_mean : [-3.7930] noise_std : 0.021183 tau_mean : [64.6920] tau_std : [10.0864] xi_std : [0.5232]
- abstractmethod personalize(data=None, algorithm=None, algorithm_settings=None, algorithm_settings_path=None, **kwargs)[source]¶
Estimate individual parameters for each ID of a given dataset.
These individual parameters correspond to the random-effects \((z_{i,j})\) of the mixed-effects model.
- Parameters:
- data
pd.DataFrameorDataorDataset, optional Contains the information of the individuals, in particular the time-points \((t_{i,j})\) and the observations \((y_{i,j})\).
- algorithm
strorAlgorithmName, optional The name of the algorithm to use.
- algorithm_settings
AlgorithmSettings, optional The algorithm settings to use.
Note
Use this if you want to customize algorithm settings through the
AlgorithmSettingsclass. If provided, the fit will rely on these settings.- algorithm_settings_path
strorPath, optional The path to the algorithm settings file.
Note
If provided, the settings from the file will be used instead of the settings provided.
- **kwargs
dict Contains the algorithm’s settings.
- data
- Returns:
- ips
IndividualParameters Individual parameters computed.
- ips
- Raises:
- NotImplementedError
- Parameters:
data (Optional[Union[DataFrame, leaspy.io.data.dataset.Data, Dataset]])
algorithm (Optional[Union[str, AlgorithmName]])
algorithm_settings (Optional[AlgorithmSettings])
- Return type:
Examples
Compute the individual parameters for a given longitudinal dataset and calibrated model, then display the histogram of the log-acceleration:
>>> from leaspy.datasets import load_model, load_dataset >>> model = load_model("parkinson-putamen") >>> putamen_df = load_dataset("parkinson-putamen") >>> individual_parameters = model.personalize(putamen_df, "scipy_minimize", seed=0)
- abstractmethod estimate(timepoints, individual_parameters, *, to_dataframe=None)[source]¶
Return the model values for individuals characterized by their individual parameters \(z_i\) at time-points \((t_{i,j})_j\).
- Parameters:
- timepoints
pd.MultiIndexordict[IDType,list[float] ] Contains, for each individual, the time-points to estimate. It can be a unique time-point or a list of time-points.
- individual_parameters
IndividualParameters Corresponds to the individual parameters of individuals.
- to_dataframe
bool, optional Whether to output a dataframe of estimations? If None: default is to be True if and only if timepoints is a pandas.MultiIndex
- timepoints
- Returns:
- individual_trajectory
pd.MultiIndexordict[IDType,list[float]] Key: patient indices. Value:
numpy.ndarrayof the estimated value, in the shape (number of timepoints, number of features)
- individual_trajectory
- Raises:
- NotImplementedError
- Parameters:
timepoints (Union[MultiIndex, dict[IDType, list[float]]])
individual_parameters (IndividualParameters)
to_dataframe (Optional[bool])
- Return type:
Examples
Given the individual parameters of two subjects, estimate the features of the first at 70, 74 and 80 years old and at 71 and 72 years old for the second.
>>> from leaspy.datasets import load_model, load_individual_parameters, load_dataset >>> model = load_model("parkinson-putamen") >>> individual_parameters = load_individual_parameters("parkinson-putamen") >>> df_train = load_dataset("parkinson-putamen-train_and_test").xs("train", level="SPLIT") >>> timepoints = {'GS-001': (70, 74, 80), 'GS-002': (71, 72)} >>> estimations = model.estimate(timepoints, individual_parameters)
- abstractmethod simulate(individual_parameters, data=None, **kwargs)[source]¶
- Run the simulation pipeline using a leaspy model.
This method simulates longitudinal data using the given leaspy model. It performs the following steps: - Retrieves individual parameters (IP) from fixed effects of the model. - Loads the specified Leaspy model. - Generates visit ages (timepoints) for each individual (based on specifications
in visits_type).
Simulates observations at those visit ages.
Packages the result into a Result object, including simulated data, individual parameters, and the model’s noise standard deviation.
- Parameters:
- Returns:
- simulated_data
Result Contains the generated individual parameters & the corresponding generated scores. Returns empty Result if any required input is None.
- simulated_data
- Raises:
- NotImplementedError
- Parameters:
individual_parameters (IndividualParameters)
data (Optional[Union[DataFrame, leaspy.io.data.dataset.Data, Dataset]])
- class BaseModel(name, **kwargs)[source]¶
Bases:
ModelInterfaceBase model class from which all
Leaspymodels should inherit.It implements the
ModelInterface.- Parameters:
name (str)
- dataset_info¶
- training_info¶
- initialization_method: InitializationMethod¶
- property features: list[FeatureType] | None¶
List of model features (None if not initialization).
- Returns:
- :
list[FeatureType], optional The features of the model, or None if not initialized.
- :
- Return type:
Optional[list[FeatureType]]
- initialize(dataset=None)[source]¶
Initialize the model given a
Datasetand an initialization method.After calling this method
is_initializedshould beTrueand model should be ready for use.
- save(path, **kwargs)[source]¶
Save model as json model parameter file.
- Parameters:
- Raises:
LeaspyModelInputErrorIf the model is not initialized.
- Parameters:
- Return type:
None
- to_dict(**kwargs)[source]¶
Export model as a dictionary ready for export.
- Returns:
KwargsTypeThe model instance serialized as a dictionary.
- Return type:
- classmethod load(path_to_model_settings)[source]¶
Load a model from a json model parameter file.
- Parameters:
- path_to_model_settings
strorPath The path to the model’s parameters file.
- path_to_model_settings
- Returns:
BaseModelAn instance of the model loaded from the file.
- Raises:
LeaspyModelInputErrorIf the model settings file is not found or cannot be read.
- Parameters:
- abstractmethod load_parameters(parameters)[source]¶
Load model parameters from a dictionary.
- Parameters:
- parameters
KwargsType The parameters to load into the model.
- parameters
- Raises:
- NotImplementedError
- Parameters:
parameters (KwargsType)
- Return type:
None
- fit(data=None, algorithm=None, algorithm_settings=None, algorithm_settings_path=None, **kwargs)[source]¶
Estimate the model’s parameters for a given dataset and a given algorithm. These model’s parameters correspond to the fixed-effects of the mixed-effects model.
- Parameters:
- data
pd.DataFrameorDataorDataset, optional Contains the information of the individuals, in particular the time-points \((t_{i,j})\) and the observations \((y_{i,j})\).
- algorithm
strorAlgorithmName, optional The name of the algorithm to use. Use this if you want to provide algorithm settings through kwargs.
- algorithm_settings
AlgorithmSettings, optional The algorithm settings to use. Use this if you want to customize algorithm settings through the
AlgorithmSettingsclass. If provided, the fit will rely on these settings.- algorithm_settings_path
strorPath, optional The path to the algorithm settings file. If provided, the settings from the file will be used instead of the settings provided.
- **kwargs
dict Contains the algorithm’s settings.
- data
- Parameters:
data (Optional[Union[DataFrame, leaspy.io.data.dataset.Data, Dataset]])
algorithm (Optional[Union[str, AlgorithmName]])
algorithm_settings (Optional[AlgorithmSettings])
- info()[source]¶
Print detailed information about the model configuration and training dataset.
- Return type:
None
- summary()[source]¶
Generate a structured summary of the model.
When called directly (e.g., model.summary()), prints a formatted summary. When stored in a variable, provides programmatic access to model attributes.
- Returns:
SummaryA structured summary object.
- Raises:
LeaspyModelInputErrorIf the model is not initialized or has no parameters.
- Return type:
Examples
>>> model.summary() # Prints the summary >>> s = model.summary() # Store to access attributes >>> s.nll # Get specific value >>> s.help() # Show available attributes
- personalize(data=None, algorithm=None, algorithm_settings=None, algorithm_settings_path=None, **kwargs)[source]¶
Estimate individual parameters for each ID of a given dataset. These individual parameters correspond to the random-effects \((z_{i,j})\) of the mixed-effects model.
- Parameters:
- data
pd.DataFrameorDataorDataset, optional Contains the information of the individuals, in particular the time-points \((t_{i,j})\) and the observations \((y_{i,j})\).
- algorithm
strorAlgorithmName, optional The name of the algorithm to use.
- algorithm_settings
AlgorithmSettings, optional The algorithm settings to use. Use this if you want to customize algorithm settings through the
AlgorithmSettingsclass. If provided, the fit will rely on these settings.- algorithm_settings_path
strorPath, optional The path to the algorithm settings file. If provided, the settings from the file will be used instead of the settings provided.
- **kwargs
dict Contains the algorithm’s settings.
- data
- Returns:
IndividualParametersIndividual parameters computed.
- Parameters:
data (Optional[Union[DataFrame, leaspy.io.data.dataset.Data, Dataset]])
algorithm (Optional[Union[str, AlgorithmName]])
algorithm_settings (Optional[AlgorithmSettings])
- Return type:
- estimate(timepoints, individual_parameters, *, to_dataframe=None)[source]¶
Return the model values for individuals characterized by their individual parameters \(z_i\) at time-points \((t_{i,j})_j\).
- Parameters:
- timepoints
pd.MultiIndexordict[IDType,list[float]] Contains, for each individual, the time-points to estimate. It can be a unique time-point or a list of time-points.
- individual_parameters
IndividualParameters Corresponds to the individual parameters of individuals.
- to_dataframe
bool, optional Whether to output a dataframe of estimations? If None: default is to be True if and only if timepoints is a pandas.MultiIndex
- timepoints
- Returns:
- individual_trajectory
pd.DataFrameordict[IDType,np.ndarray] Key: patient indices. Value:
numpy.ndarrayof the estimated value, in the shape (number of timepoints, number of features)
- individual_trajectory
- Parameters:
timepoints (Union[MultiIndex, dict[IDType, list[float]]])
individual_parameters (IndividualParameters)
to_dataframe (Optional[bool])
- Return type:
- abstractmethod compute_individual_trajectory(timepoints, individual_parameters)[source]¶
Compute the model values for an individual characterized by their individual parameters at given time-points. Parameters ———- timepoints :
list[float]The time-points at which to compute the model values.
- individual_parameters
IndividualParameters The individual parameters of the individual.
- Returns:
torch.TensorThe computed model values for the individual at the given time-points.
- Raises:
- NotImplementedError
If the method is not implemented in the subclass.
- Parameters:
individual_parameters (IndividualParameters)
- Return type:
- individual_parameters
- simulate(algorithm=None, algorithm_settings=None, algorithm_settings_path=None, **kwargs)[source]¶
Run the simulation pipeline using a leaspy model.
This method simulates longitudinal data using the given leaspy model. It performs the following steps: - Retrieves individual parameters (IP) from fixed effects of the model. - Loads the specified Leaspy model. - Generates visit ages (timepoints) for each individual (based on specifications in visits_type) - Simulates observations at those visit ages. - Packages the result into a Result object, including simulated data, individual parameters, and the model’s noise standard deviation.
- Parameters:
- algorithm
strorAlgorithmName, optional The name of the algorithm to use. Use this if you want to provide algorithm settings through kwargs.
- algorithm_settings
AlgorithmSettings, optional The algorithm settings to use. Use this if you want to customize algorithm settings through the
AlgorithmSettingsclass. If provided, the fit will rely on these settings.- algorithm_settings_path
strorPath, optional The path to the algorithm settings file. If provided, the settings from the file will be used instead of the settings provided.
- **kwargs
dict Contains the algorithm’s settings.
- algorithm
- Returns:
- simulated_data
Result Contains the generated individual parameters & the corresponding generated scores. Returns empty Result if any required input is None.
- simulated_data
- Parameters:
algorithm (Optional[Union[str, AlgorithmName]])
algorithm_settings (Optional[AlgorithmSettings])
Notes
To generate a new subject, first we estimate the joined distribution of the individual parameters and the reparametrized baseline ages. Then, we randomly pick a new point from this distribution, which define the individual parameters & baseline age of our new subjects. Then, we generate the timepoints following the baseline age. Then, from the model and the generated timepoints and individual parameters, we compute the corresponding values estimations. Then, we add some noise to these estimations, which is the same noise-model as the one from your model by default. But, you may customize it by setting the noise keyword.
Examples
Use a calibrated model & individual parameters to simulate new subjects similar to the ones you have:
>>> from leaspy.models import LogisticModel >>> from leaspy.io.data import Data >>> from leaspy.datasets import load_dataset, load_leaspy_instance, load_individual_parameters >>> putamen_df = load_dataset("parkinson-putamen-train_and_test") >>> data = Data.from_dataframe(putamen_df.xs('train', level='SPLIT')) >>> leaspy_logistic = load_leaspy_instance("parkinson-putamen-train") >>> visits_params = {'patient_number':200, 'visit_type': "random", 'first_visit_mean' : 0., 'first_visit_std' : 0.4, 'time_follow_up_mean' : 11, 'time_follow_up_std' : 0.5, 'distance_visit_mean' : 2/12, 'distance_visit_std' : 0.75/12, 'min_spacing_between_visits': 1/365 } >>> simulated_data = model.simulate( algorithm="simulate", features=["MDS1_total", "MDS2_total", "MDS3_off_total", 'SCOPA_total','MOCA_total','REM_total','PUTAMEN_R','PUTAMEN_L','CAUDATE_R','CAUDATE_L'],visit_parameters= visits_params )