leaspy.io.outputs.individual_parameters¶
Classes¶
Data container for individual parameters, contains IDs, timepoints and observations values. |
Module Contents¶
- class IndividualParameters[source]¶
Data container for individual parameters, contains IDs, timepoints and observations values. Output of the
leaspy.algo.personalizemethod, contains the random effects.There are used as output of the personalization algorithms and as input/output of the simulation algorithm, to provide an initial distribution of individual parameters.
- Attributes:
- _indices
list List of the patient indices
- _individual_parameters
dict Individual indices (key) with their corresponding individual parameters {parameter name: parameter value}
- _parameters_shape
dict Shape of each individual parameter
- _default_saving_type
str Default extension for saving when none is provided
- _indices
- VALID_IO_EXTENSIONS = ['csv', 'json']¶
- add_individual_parameters(index, individual_parameters)[source]¶
Add the individual parameter of an individual to the IndividualParameters object
- Parameters:
- index:class:
IDType Index of the individual
- individual_parameters
DictParams Individual parameters of the individual
- index:class:
- Raises:
LeaspyIndividualParamsInputErrorIf the index is not a string or has already been added
Or if the individual parameters is not a dict.
Or if individual parameters are not self-consistent.
- Parameters:
index (IDType)
individual_parameters (DictParams)
Examples
Add two individual with tau, xi and sources parameters
>>> ip = IndividualParameters() >>> ip.add_individual_parameters('index-1', {"xi": 0.1, "tau": 70, "sources": [0.1, -0.3]}) >>> ip.add_individual_parameters('index-2', {"xi": 0.2, "tau": 73, "sources": [-0.4, -0.1]})
- items()[source]¶
Get the items of the individual parameters dictionary.
- Returns:
- ItemsView
A view object displaying a list of tuples (individual ID, parameters dict)
Examples
>>> ip = IndividualParameters() >>> ip.add_individual_parameters('index-1', {"xi": 0.1, "tau": 70, "sources": [0.1, -0.3]}) >>> list(ip.items()) ['index-1', {"xi": 0.1, "tau": 70, "sources": [0.1, -0.3]}]
- subset(indices, *, copy=True)[source]¶
Returns IndividualParameters object with a subset of the initial individuals
- Parameters:
- indices
Iterable`[:class:`~leaspy.utils.typing.IDType] List of strings that corresponds to the indices of the individuals to return
- copy
bool, optional (default True) Should we copy underlying parameters or not?
- indices
- Returns:
IndividualParametersAn instance of the IndividualParameters object with the selected list of individuals
- Raises:
LeaspyIndividualParamsInputErrorRaise an error if one of the index is not in the IndividualParameters
- Parameters:
Examples
>>> ip = IndividualParameters() >>> ip.add_individual_parameters('index-1', {"xi": 0.1, "tau": 70, "sources": [0.1, -0.3]}) >>> ip.add_individual_parameters('index-2', {"xi": 0.2, "tau": 73, "sources": [-0.4, -0.1]}) >>> ip.add_individual_parameters('index-3', {"xi": 0.3, "tau": 58, "sources": [-0.6, 0.2]}) >>> ip_sub = ip.subset(['index-1', 'index-3'])
- get_aggregate(parameter, function)[source]¶
Returns the result of aggregation by function of parameter values across all patients
- Parameters:
- parameter
ParamType Name of the parameter
- function
Callable A function operating on iterables and supporting axis keyword, and outputing an iterable supporting the tolist method.
- parameter
- Returns:
listResulting value of the parameter
- Raises:
LeaspyIndividualParamsInputErrorIf individual parameters are empty,
or if the parameter is not in the IndividualParameters.
- Parameters:
parameter (ParamType)
function (Callable)
- Return type:
Examples
>>> ip = IndividualParameters.load("path/to/individual_parameters") >>> tau_median = ip.get_aggregate("tau", np.median)
- get_mean(parameter)[source]¶
Returns the mean value of a parameter across all patients
- Parameters:
- parameter
ParamType Name of the parameter
- parameter
- Returns:
listMean value of the parameter
- Raises:
LeaspyIndividualParamsInputErrorIf individual parameters are empty,
or if the parameter is not in the IndividualParameters.
- Parameters:
parameter (ParamType)
Examples
>>> ip = IndividualParameters.load("path/to/individual_parameters") >>> tau_mean = ip.get_mean("tau")
- get_std(parameter)[source]¶
Returns the standard deviation of a parameter across all patients
- Parameters:
- parameter
ParamType Name of the parameter
- parameter
- Returns:
listStandard-deviation value of the parameter
- Raises:
LeaspyIndividualParamsInputErrorIf individual parameters are empty,
or if the parameter is not in the IndividualParameters.
- Parameters:
parameter (ParamType)
Examples
>>> ip = IndividualParameters.load("path/to/individual_parameters") >>> tau_std = ip.get_std("tau")
- to_dataframe()[source]¶
Returns the dataframe of individual parameters
- Returns:
pandas.DataFrameEach row corresponds to one individual. The index corresponds to the individual index (‘ID’). The columns are the names of the parameters.
- Return type:
Examples
Convert the individual parameters object into a dataframe
>>> ip = IndividualParameters.load("path/to/individual_parameters") >>> ip_df = ip.to_dataframe()
- static from_dataframe(df)[source]¶
Static method that returns an IndividualParameters object from the dataframe
- Parameters:
- df
pandas.DataFrame Dataframe of the individual parameters. Each row must correspond to one individual. The index corresponds to the individual index. The columns are the names of the parameters.
- df
- Returns:
IndividualParametersAn instance of IndividualParameters initialized from the DataFrame.
- Parameters:
df (DataFrame)
Examples
>>> import pandas as pd >>> data = { >>> 'tau': [70, 73], >>> 'xi': [0.1, 0.2], >>> 'sources_0': [0.1, -0.4], >>> 'sources_1': [-0.3, -0.1] >>> } >>> df = pd.DataFrame(data, index=['id1', 'id2']) >>> ip = IndividualParameters.from_dataframe(df)
- static from_pytorch(indices, dict_pytorch)[source]¶
Static method that returns an IndividualParameters object from the indices and pytorch dictionary
- Parameters:
- indices
list`[:class:`~leaspy.utils.typing.IDType] List of the patients indices
- dict_pytorch
DictParmasTorch Dictionary of the individual parameters
- indices
- Returns:
IndividualParametersAn instance of IndividualParameters initialized from the pytorch dictionary.
- Raises:
- Parameters:
dict_pytorch (DictParamsTorch)
Examples
>>> indices = ['index-1', 'index-2', 'index-3'] >>> ip_pytorch = { >>> "xi": torch.tensor([[0.1], [0.2], [0.3]], dtype=torch.float32), >>> "tau": torch.tensor([[70], [73], [58.]], dtype=torch.float32), >>> "sources": torch.tensor([[0.1, -0.3], [-0.4, 0.1], [-0.6, 0.2]], dtype=torch.float32) >>> } >>> ip_pytorch = IndividualParameters.from_pytorch(indices, ip_pytorch)
- to_pytorch()[source]¶
Returns the indices and pytorch dictionary of individual parameters
- Returns:
- indices:
list`[:class:`~leaspy.utils.typing.IDType] List of patient indices
- pytorch_dict:
DictParamsTorch Dictionary of the individual parameters {parameter name: pytorch tensor of values across individuals}
- indices:
- Return type:
Examples
Convert the individual parameters object into a dataframe
>>> ip = IndividualParameters.load("path/to/individual_parameters") >>> indices, ip_pytorch = ip.to_pytorch()
- save(path, **kwargs)[source]¶
Saves the individual parameters (json or csv) at the path location
TODO? save leaspy version as well for retro/future-compatibility issues?
- Parameters:
- path
str Path and file name of the individual parameters. The extension can be json or csv. If no extension, default extension (csv) is used
- **kwargs
Additional keyword arguments to pass to either: *
pandas.DataFrame.to_csv()*json.dump()depending on saving format requested
- path
- Raises:
LeaspyIndividualParamsInputErrorIf extension not supported for saving
If individual parameters are empty
- Parameters:
path (str)
Warning
Emits a warning if no file extension is provided and the default extension is used.
Examples
>>> ip.save("params.csv", index=False) >>> ip.save("params.json", indent=4)
- classmethod load(path)[source]¶
Static method that loads the individual parameters (json or csv) existing at the path location
- Parameters:
- path
str Path and file name of the individual parameters.
- path
- Returns:
IndividualParametersIndividual parameters object load from the file
- Raises:
LeaspyIndividualParamsInputErrorIf the provided extension is not csv or not json.
- Parameters:
path (str)
Examples
>>> ip = IndividualParameters.load('/path/to/individual_parameters_1.json') >>> ip2 = IndividualParameters.load('/path/to/individual_parameters_2.csv')