pymob.sim package#

Submodules#

pymob.sim.config module#

pymob.sim.base module#

pymob.sim.base.enlist_attr(ds: Dataset | DataArray, attr: str)#

Transforms a string representation of a metadata attribute of an xarray dataset or datarray to a list

pymob.sim.base.stack_variables(ds: ~xarray.core.dataset.Dataset, variables: ~typing.List[str], new_coordinates: ~typing.List[str], new_dim: str, pattern: ~typing.Callable = <function <lambda>>)#

Combine data variables and coordinates into a new variable of a higher dimension.

Parameters:
  • ds (xr.Dataset) – The input xarray Dataset.

  • variables (List[str]) – List of variable bases to stack.

  • new_coordinates (List[str]) – List of new coordinates for the higher dimension. Note that this must be the same for all variables you want to stack.

  • new_dim (str) – The name of the new dimension.

  • pattern (Callable, optional) – A function to generate names for new variables based on the variable and coordinate names.

Returns:

The modified xarray Dataset with stacked variables.

Return type:

xr.Dataset

Examples

A use case is the following. Consider a dataset that has the variables

cext_A cext_B cext_C my_other_var_1 my_other_var_2

And you want to combine the variables with the same base cext_ into a new variable that has the base as a dimension.

>>> import xarray as xr
>>> from typing import List, Callable
>>> from pymob.sim.base import stack_variables
>>>
>>> # Example usage:
>>> an_xarray_dataset = xr.Dataset({
...     'cext_A': ([], 1.0),
...     'cext_B': ([], 2.0),
...     'cext_C': ([], 3.0),
...     'my_other_var_1': ([], 4.0),
...     'my_other_var_2': ([], 5.0),
... })
>>>
>>> result_dataset = stack_variables(
...     ds=an_xarray_dataset,
...     variables=["cext"],
...     new_coordinates=["A", "B", "C"],
...     new_dim="letters",
... )
>>>
>>> result_dataset
<xarray.Dataset>
Dimensions:         (letters: 3)
Coordinates:
  * letters         (letters) <U1 'A' 'B' 'C'
Data variables:
    my_other_var_1  float64 4.0
    my_other_var_2  float64 5.0
    cext            (letters) float64 1.0 2.0 3.0
pymob.sim.base.unlist_attrs(ds: Dataset | DataArray)#

Transforms lists of variables to a comma separated string to work around errors when storing the dataset or dataarray to disk

pymob.sim.evaluator module#

class pymob.sim.evaluator.Evaluator(model: Callable, solver: type | Callable, dimensions: Sequence[str], dimension_sizes: Dict[str, int], parameter_dims: Dict[str, Tuple[str, ...]], n_ode_states: int, var_dim_mapper: Dict, data_structure: Dict, data_structure_and_dimensionality: Dict, coordinates: Dict[str, ndarray[Any, dtype[ScalarType]]], coordinates_input_vars: Dict[str, Dict[str, Dict[str, ndarray[Any, dtype[ScalarType]]]]], dims_input_vars: Dict[str, Dict[str, Tuple[str, ...]]], coordinates_indices: Dict, data_variables: Sequence[str], stochastic: bool, batch_dimension: str, indices: Dict = {}, post_processing: Callable | None = None, solver_options: Dict = {}, **kwargs)#

Bases: object

The Evaluator is an instance to evaluate a model. It’s purpose is primarily to create objects that can be spawned and evaluated in parallel and can individually track the results of a simulation or a parameter inference process. If needed the evaluations can be tracked and results can later be collected.

Seed may not be set as a property, because this should be something passed through

property allowed_model_signature_arguments#
property dimensionality#
get_call_signature()#
property parameters: frozendict#
result: Dataset#
property results#
spawn()#
pymob.sim.evaluator.create_dataset_from_dict(Y: dict, data_structure, coordinates, var_dim_mapper)#
pymob.sim.evaluator.create_dataset_from_numpy(Y, Y_names, coordinates)#

pymob.sim.solvetools module#

Module contents#