pymob.sim package#
Submodules#
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.evaluators 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", ... ) >>> >>> print(result_dataset) <xarray.Dataset> Dimensions: (letters: 3) Coordinates: * letters (letters) <U1 'A' 'B' 'C' Data variables: my_other_var_1 (float64) float64 4.0 my_other_var_2 (float64) 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: Callable, parameters: Dict, dimensions: List, n_ode_states: int, var_dim_mapper: List, data_structure: Dict, coordinates: Dict, data_variables: List, stochastic: bool, indices: Dict | None = {}, post_processing: Callable | None = None, **kwargs)#
Bases:
objectThe 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()#
- result: Dataset#
- property results#
- pymob.sim.evaluator.create_dataset_from_dict(Y: dict, data_structure, coordinates)#
- pymob.sim.evaluator.create_dataset_from_numpy(Y, Y_names, coordinates)#
pymob.sim.solvetools module#
- class pymob.sim.solvetools.SolverBase#
Bases:
objectThe idea of creating a solver as a class is that it is easier to pass on important arguments of the simulation relevant to the Solver. Therefore a solver can access all attributes of an Evaluator
- static solve()#
- pymob.sim.solvetools.create_interpolation(x_in: Dataset, y: str, x: str = 'time', factor: float = 0.0001, interpolation: Literal['fill-forward', 'linear'] = 'fill-forward') Dataset#
Make the interpolation safe by adding a coordinate just before each x-value (except the first vaue). The distance between the new and the next point are calculated as a fraction of the previous distance between neighboring points. The corresponding y-values are first set to NaN and then interpolated based on the interpolation method.
- Parameters:
x_in (xr.Dataset) – The input dataset which contains a coordinate (x) and a data variable (y)
x (str, optional) – The name of the x coordinate, by default “time”
factor (float, optional) – The distance between the newly added points and the following existing points on the x-scale, by default 1e-4
interpolation (Literal["fill-forward", "linear"], optional) – The interpolation method. In addition to ‘fill-forward’ and ‘linear’, any method give in xr.interpolate_na can be chosen, by default “fill-forward”
- Returns:
The interpolated dataset
- Return type:
xr.Dataset
- pymob.sim.solvetools.mappar(func, parameters, exclude=[])#