Top Level Utilities#

General utility functions for the lyscripts package.

lyscripts.utils.binom_pmf(support: list[int] | ndarray, p: float = 0.5)[source]#

Binomial PMF that is much faster than the one from scipy.

lyscripts.utils.get_dict_depth(nested: dict) int[source]#

Get the depth of a nested dictionary.

>>> get_dict_depth({"a": {"b": 1}})
2
>>> varying_depth = {"a": {"b": 1}, "c": {"d": {"e": 2}}}
>>> get_dict_depth(varying_depth)
3
lyscripts.utils.delete_private_keys(nested: dict) dict[source]#

Delete private keys from a nested dictionary.

A ‘private’ key is a key whose name starts with an underscore. For example:

>>> delete_private_keys({"patient": {"__doc__": "some patient info", "age": 61}})
{'patient': {'age': 61}}
>>> delete_private_keys({"patient": {"age": 61}})
{'patient': {'age': 61}}
lyscripts.utils.flatten(nested: dict, prev_key: tuple = (), max_depth: int | None = None) dict[source]#

Flatten nested dict by creating key tuples for each value at max_depth.

>>> nested = {"tumor": {"1": {"t_stage": 1, "size": 12.3}}}
>>> flatten(nested)
{('tumor', '1', 't_stage'): 1, ('tumor', '1', 'size'): 12.3}
>>> mapping = {"patient": {"#": {"age": {"func": int, "columns": ["age"]}}}}
>>> flatten(mapping, max_depth=3)
{('patient', '#', 'age'): {'func': <class 'int'>, 'columns': ['age']}}

Note that flattening an already flat dictionary will yield some weird results.

lyscripts.utils.unflatten(flat: dict) dict[source]#

Take a flat dictionary with tuples of keys and create nested dict from it.

>>> flat = {('tumor', '1', 't_stage'): 1, ('tumor', '1', 'size'): 12.3}
>>> unflatten(flat)
{'tumor': {'1': {'t_stage': 1, 'size': 12.3}}}
>>> mapping = {('patient', '#', 'age'): {'func': int, 'columns': ['age']}}
>>> unflatten(mapping)
{'patient': {'#': {'age': {'func': <class 'int'>, 'columns': ['age']}}}}
lyscripts.utils.get_modalities_subset(defined_modalities: dict[str, list[float]], selection: list[str]) dict[str, list[float]][source]#

Of the defined_modalities return only those mentioned in the selection.

>>> modalities = {"CT": [0.76, 0.81], "MRI": [0.63, 0.86]}
>>> get_modalities_subset(modalities, ["CT"])
{'CT': [0.76, 0.81]}
lyscripts.utils.load_patient_data(file_path: Path, **read_csv_kwargs: dict) DataFrame[source]#

Load patient data from a CSV file stored at file.

lyscripts.utils.load_yaml_params(file_path: Path) dict[source]#

Load parameters from a YAML file.

lyscripts.utils.load_model_samples(file_path: Path, name: str = 'mcmc', flat: bool = True, discard: int = 0, thin: int = 1) ndarray[source]#

Load MCMC samples stored in HDF5 file at file_path under a key name.

lyscripts.utils.get_hdf5_backend(file_path: Path, dataset: str = 'mcmc', nwalkers: int | None = None, ndim: int | None = None, reset: bool = False) HDFBackend[source]#

Open an HDF5 file at file_path and return a backend.