leaspy.models.constant_model module
- class ConstantModel(name: str, **kwargs)
Bases:
GenericModel
ConstantModel is a benchmark model that predicts constant values (no matter what the patient’s ages are).
These constant values depend on the algorithm setting and the patient’s values provided during calibration. It could predict:
last: last value seen during calibration (even if NaN),
last_known: last non NaN value seen during calibration*§,
max: maximum (=worst) value seen during calibration*§,
mean: average of values seen during calibration§.
\* <!> depending on features, the last_known / max value may correspond to different visits.§ <!> for a given feature, value will be NaN if and only if all values for this feature were NaN.- Parameters
- namestr
The model’s name
- **kwargs
Hyperparameters for the model. None supported for now.
See also
- Attributes
- namestr
The model’s name
- is_initializedbool
Always True (no true initialization needed for constant model)
- featureslist[str]
List of the model features. Unlike most models features will be determined at personalization only (because it does not needed any fit)
- dimensionint
Number of features (read-only)
- parametersdict
Model has no parameters: empty dictionary. The prediction_type parameter should be defined during personalization. Example:
>>> AlgorithmSettings('constant_prediction', prediction_type='last_known')
Methods
compute_individual_trajectory
(timepoints, ...)Compute scores values at the given time-point(s) given a subject's individual parameters.
get_hyperparameters
(*[, with_features, ...])Get all model hyperparameters
Check all model hyperparameters are ok
initialize
(dataset[, method])Initialize the model given a dataset and an initialization method.
load_hyperparameters
(hyperparameters, *[, ...])Load model hyperparameters from a dict
load_parameters
(parameters, *[, list_converter])Instantiate or update the model's parameters.
save
(path, **kwargs)Save Leaspy object as json model parameter file.
validate_compatibility_of_dataset
(dataset)Raise if the given dataset is not compatible with the current model.
- compute_individual_trajectory(timepoints, individual_parameters)
Compute scores values at the given time-point(s) given a subject’s individual parameters.
- Parameters
- timepointsscalar or array_like[scalar] (list, tuple,
numpy.ndarray
) Contains the age(s) of the subject.
- individual_parametersdict[str, Any]
Contains the individual parameters. Each individual parameter should be a scalar or array_like
- timepointsscalar or array_like[scalar] (list, tuple,
- Returns
torch.Tensor
Contains the subject’s scores computed at the given age(s) Shape of tensor is (1, n_tpts, n_features)
- get_hyperparameters(*, with_features=True, with_properties=True, default=None) Dict[str, Any]
Get all model hyperparameters
- Parameters
- with_features, with_propertiesbool (default True)
Whether to include features and respectively all _properties (i.e. _dynamic_ hyperparameters) in the returned dictionary
- defaultAny
Default value is something is an hyperparameter is missing (should not!)
- Returns
- dict { hyperparam_namestr -> hyperparam_valueAny }
- initialize(dataset: Dataset, method: str = None)
Initialize the model given a dataset and an initialization method.
After calling this method
is_initialized
should be True and model should be ready for use.- Parameters
- dataset
Dataset
The dataset we want to initialize from.
- methodstr, optional (default None)
A custom method to initialize the model
- dataset
- load_hyperparameters(hyperparameters: Dict[str, Any], *, with_defaults: bool = False) None
Load model hyperparameters from a dict
- Parameters
- hyperparametersdict[str, Any]
Contains the model’s hyperparameters
- with_defaultsbool (default False)
If true, it also resets hyperparameters that are part of the model but not included in hyperparameters to their default value.
- Raises
LeaspyModelInputError
if inconsistent hyperparameters
- load_parameters(parameters, *, list_converter=<built-in function array>) None
Instantiate or update the model’s parameters.
- Parameters
- parametersdict
Contains the model’s parameters.
- list_convertercallable
The function to convert list objects.
- save(path: str, **kwargs)
Save Leaspy object as json model parameter file.
Default save method: it can be overwritten in child class but should be generic…
- Parameters
- pathstr
Path to store the model’s parameters.
- **kwargs
Keyword arguments for json.dump method.
- validate_compatibility_of_dataset(dataset: Dataset)
Raise if the given dataset is not compatible with the current model.
- Parameters
- dataset
Dataset
The dataset we want to model.
- dataset
- Raises
LeaspyDataInputError
If and only if data is incompatible with model.