pymob.utils package#

Submodules#

pymob.utils.bayesian module#

pymob.utils.bayesian.cluster_chains(posterior)#

pymob.utils.config module#

pymob.utils.config.catch_patterns(expression_str)#
pymob.utils.config.dX_dt2X(expr: str)#
pymob.utils.config.get_return_arguments(func)#
pymob.utils.config.lambdify_expression(expression_str)#
pymob.utils.config.lookup(val, *indexable_objects)#
pymob.utils.config.lookup_args(args, *objects_to_search)#
pymob.utils.config.lookup_from(val: Any, collection: Iterable[Mapping]) Any#
pymob.utils.config.read_config(config_file)#
pymob.utils.config.simulation_io_adapter(input_config_file, input_events_file, output_path)#

A simple adapter to adapt the configuration file to be usable with the case-study design for the example of experiment based simulation

pymob.utils.errors module#

exception pymob.utils.errors.PymobError(message)#

Bases: Exception

Exception raised for custom error scenarios.

message -- explanation of the error
pymob.utils.errors.errormsg(msg)#
pymob.utils.errors.get_version(module: module) str#
pymob.utils.errors.import_optional_dependency(name: str, extra: str = '', *, errors: Literal['raise', 'warn', 'ignore'] = 'raise') module | None#

This is based on pandas.compat._optional.py from 03.04.2024 pandas-dev/pandas The function originally also supports comparison against the version number of the installed package.

Import an optional dependency.

By default, if a dependency is missing an ImportError with a nice message will be raised. If a dependency is present, but too old, we raise.

Parameters:
  • name (str) – The module name.

  • extra (str) – Additional text to include in the ImportError message.

  • errors (str {'raise', 'warn', 'ignore'}) –

    What to do when a dependency is not found or its version is too old.

    • raise : Raise an ImportError

    • warn : Only applicable when a module’s version is to old. Warns that the version is too old and returns None

    • ignore: If the module is not installed, return None, otherwise, return the module, even if the version is too old. It’s expected that users validate the version locally when using errors="ignore" (see. io/html.py)

  • min_version (str, default None) – Specify a minimum version that is different from the global pandas minimum version required.

Returns:

maybe_module – The imported module, when found and the version is correct. None is returned when the package is not found and errors is False, or when the package’s version is too old and errors is 'warn' or 'ignore'.

Return type:

Optional[ModuleType]

pymob.utils.help module#

pymob.utils.math_helpers module#

pymob.utils.math_helpers.round_to_nearest_multiple_of_y(x, y)#
pymob.utils.math_helpers.take_mean(list)#

pymob.utils.misc module#

class pymob.utils.misc.Date2Delta(origin)#

Bases: object

pymob.utils.misc.benchmark(func)#
pymob.utils.misc.dayTicks(x, pos)#
pymob.utils.misc.expand_grid(data_dict)#

Create a dataframe from every combination of given values.

pymob.utils.misc.get_grouped_unique_val(df, variable, groupby='id', extra_dim=None, fill_extra=nan)#
pymob.utils.misc.get_host(localhosts)#
pymob.utils.misc.get_limits_from_array(theta)#
pymob.utils.misc.hourTicks(x, pos)#
pymob.utils.misc.invert_list_tuple(data)#

this small helper inverts a list of tuples to a list of lists like [(a,b),(a,b),(a,b)] –> [[a,a,a],[b,b,b]]

pymob.utils.misc.match_columns(features, dataset)#
pymob.utils.misc.pivot_xarray(ds, name, dimname, sep='_')#
pymob.utils.misc.pop_key(d, ex)#

pop (remove) a given key from a dictionary

pymob.utils.misc.repeat_func(func, n, **kwargs)#

repeat a generic function and store the output in a numpy array func: function to be called n: number of repetitions of the function call **kwargs: keyword arguments passed to func

returns a 1D numpy array of the iterated results

pymob.utils.misc.replace_na(df, column, default_value)#
pymob.utils.misc.round_decimals_down(number: float, decimals: int = 2)#

Returns a value rounded up to a specific number of decimal places.

pymob.utils.misc.to_xarray_dataset(rawdata)#

pymob.utils.plot_helpers module#

pymob.utils.plot_helpers.combine_legends(axes)#
pymob.utils.plot_helpers.plot_hist(x, name='', bins=10, ax=None, hdi=False, decorate=True)#
pymob.utils.plot_helpers.plot_loghist(x, name='', bins=10, ax=None, hdi=False, decorate=True, color='tab:blue', alpha=1, legend=None, orientation='vertical', **hist_kwargs)#

pymob.utils.store_file module#

pymob.utils.store_file.case_study_output(case_study, scenario, pkg_dir='case_studies')#
pymob.utils.store_file.create_fname_date_ver(dirname='./', fname='file', fextension='.txt')#
pymob.utils.store_file.deserialize(dct, convert_time=True)#
pymob.utils.store_file.go_to_case_studies(case_study_dir='case_studies', stop_inside_case_studies=True)#

A convenience method to find the case studies directory from a directory within the case study. And change the working directory respectively

pymob.utils.store_file.import_package(package_path)#

this script handles the import of a case study without the typical __init__.py file. It iterates through all .py files in the root directory of the case study (typically: sim, mod, stats, plot, data, prior) and imports them with () package path to package from project dir (e.g. “model/core/daphnia”)

pymob.utils.store_file.is_number(s)#
pymob.utils.store_file.list_converter(x)#
pymob.utils.store_file.opt(a, b, c)#

tests arguments for their state and returns with priority a, b and then c

pymob.utils.store_file.parse_config_section(section, method: str = 'strint')#

method can be one of ‘strint’ (mix of string and integers) or “list” if lists are also in the section

pymob.utils.store_file.prepare_casestudy(case_study: tuple[str, str], config_file: str = 'settings.cfg', pkg_dir='case_studies') ConfigParser#

Loads the configuration file of the case study by providing the name of the case study directory, the name of the scenario directory, and the name of the configuration file. Also adds the case_study directory to sys.path so that all modules in the root of the case_study directory can be imported (e.g. import sim, import mod, …)

By default the function assumes that you have the following directory structure:

```kotlin project_directory (working directory)

├─ case_studies │ ├─ case_study_1 │ │ ├─ scenarios │ │ │ ├─ scenario_1 │ │ │ │ └─ settings.cfg │ │ │ ├─ scenario_2 │ │ │ │ └─ settings.cfg │ │ └─ results │ ├─ sim.py │ ├─ mod.py │ ├─ data.py │ ├─ plot.py │ └─ … └─ other_directory

```

Parameters:
  • case_study (tuple[str,str]) – A tuple of the case_study direcory and the scenario directory

  • config_file (str, optional) – The name of the configuration file contained in the scenario directory. Default is ‘settings.cfg’

  • pkg_dir (str, optional) – The name of the folder, where all case studies are located. Can also be a relative or an absolute file path. By default “case_studies”

Returns:

The configuration file of the case-study-scenario combination.

Return type:

ConfigParser

Example

This example demonstrates how to use the prepare_casestudy function:

```python from my_module import prepare_casestudy

# Example usage of prepare_casestudy function config = prepare_casestudy((“lotka_volterra_case_study”, “test_scenario”), “settings.cfg”)

# Now you can access configuration settings print(config.get(“case-study”, “name”)) print(config.get(“case-study”, “scenario”))

# And use the config file to initialize a Simulation object from sim import Simulation sim = Simulation(config) ```

pymob.utils.store_file.prepare_casestudy_sbi(case_study)#
pymob.utils.store_file.prepare_sbi(sbi_config_file)#
pymob.utils.store_file.prepare_scenario(case_study, scenario, input_files=[])#
pymob.utils.store_file.read_config(config_file)#
pymob.utils.store_file.read_settings()#
pymob.utils.store_file.reroute_output_to_base(output)#
pymob.utils.store_file.scenario_file(file, case_study, scenario, pkg_dir='case_studies')#
pymob.utils.store_file.sequential_filename_iterator(path, sep='_')#

file storing function that appends a running number to the filename, depending on the number of files in the directory. Note that this does only work if files are created sequentially. It will probably produce errors if the files are produced in parallel.

pymob.utils.store_file.serialize(ds, convert_time=True)#

function serializes a dataset. By default converts a time coordinate from datetime64[ns] to float[ns]

pymob.utils.store_file.store_sbi_simulations(path, theta, x, simname=None)#
pymob.utils.store_file.unixify_path(path)#
pymob.utils.store_file.unnest(d, flat, parent_key='', sep='.')#

Module contents#