leaspy.io.outputs.individual_parameters

Classes

IndividualParameters

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.personalize method, 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:
_indiceslist

List of the patient indices

_individual_parametersdict

Individual indices (key) with their corresponding individual parameters {parameter name: parameter value}

_parameters_shapedict

Shape of each individual parameter

_default_saving_typestr

Default extension for saving when none is provided

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_parametersDictParams

Individual parameters of the individual

Raises:
LeaspyIndividualParamsInputError
  • If 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:

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:
indicesIterable`[:class:`~leaspy.utils.typing.IDType]

List of strings that corresponds to the indices of the individuals to return

copybool, optional (default True)

Should we copy underlying parameters or not?

Returns:
IndividualParameters

An instance of the IndividualParameters object with the selected list of individuals

Raises:
LeaspyIndividualParamsInputError

Raise 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:
parameterParamType

Name of the parameter

functionCallable

A function operating on iterables and supporting axis keyword, and outputing an iterable supporting the tolist method.

Returns:
list

Resulting value of the parameter

Raises:
LeaspyIndividualParamsInputError
  • If individual parameters are empty,

  • or if the parameter is not in the IndividualParameters.

Parameters:
Return type:

list

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:
parameterParamType

Name of the parameter

Returns:
list

Mean value of the parameter

Raises:
LeaspyIndividualParamsInputError
  • If 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:
parameterParamType

Name of the parameter

Returns:
list

Standard-deviation value of the parameter

Raises:
LeaspyIndividualParamsInputError
  • If 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.DataFrame

Each row corresponds to one individual. The index corresponds to the individual index (‘ID’). The columns are the names of the parameters.

Return type:

DataFrame

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:
dfpandas.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.

Returns:
IndividualParameters

An 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:
indiceslist`[:class:`~leaspy.utils.typing.IDType]

List of the patients indices

dict_pytorchDictParmasTorch

Dictionary of the individual parameters

Returns:
IndividualParameters

An instance of IndividualParameters initialized from the pytorch dictionary.

Raises:
LeaspyIndividualParamsInputError
Parameters:

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}

Return type:

tuple[list[IDType], DictParamsTorch]

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:
pathstr

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

Raises:
LeaspyIndividualParamsInputError
  • If 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:
pathstr

Path and file name of the individual parameters.

Returns:
IndividualParameters

Individual parameters object load from the file

Raises:
LeaspyIndividualParamsInputError

If 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')