episimlab.utils package

Submodules

episimlab.utils.datetime module

class episimlab.utils.datetime.IntPerDay

Bases: object

Provide an interval per day int_per_day

int_per_dayattr.Attribute

No description given

Variable properties:

  • type : variable

  • intent : out

  • global name : int_per_day

  • dimensions : ()

run_step(step_delta)
episimlab.utils.datetime.discrete_time_approx(rate, timestep)
Parameters
  • rate – daily rate

  • timestep – timesteps per day

Returns

rate rescaled by time step

episimlab.utils.datetime.dt64_to_day_of_week(dt64: numpy.datetime64) int

Convert a datetime to integer corresponding to day of the week. Monday == 0, Sunday == 6.

episimlab.utils.datetime.get_int_per_day(step_delta: numpy.timedelta64) float

Given a time delta, calculate the number of intervals per day.

episimlab.utils.plot module

episimlab.utils.plot.plotter(flavor='mpl', log_dir='./logs', log_stub=None, plotter_kwargs={})

TODO WORK IN PROGRESS

Decorates func with function that plots DataArray. This function returns a decorator, so use like:

@plotter() def my_func():

return xr.DataArray()

episimlab.utils.plot.visualize_compt_graph(gph: networkx.classes.graph.Graph, path: Optional[str] = None, mpl_backend: Optional[str] = None, pos=None, default_edge_color: str = 'black', default_node_color: str = 'black', node_size: int = 1000, font_size: int = 16, font_weight: str = 'bold', font_color: str = 'white', **kwargs)

Visualize compartment graph gph using matplotlib. Saves figure to a path if specified, e.g. ‘my_compt_graph.svg’.

episimlab.utils.plot.xr_plot(data_array, sel={}, isel={}, timeslice=slice(0, 100, None), sum_over=['risk_group', 'age_group'])

Uses DataArray.plot, which builds on mpl

episimlab.utils.profile module

episimlab.utils.profile.profiler(flavor='wall_clock', log_dir='./', log_stub=None, show_prof=False, cumulative=False)

Decorates func with Dask memory and thread profiling. This function returns a decorator, so use like:

@profiler() def my_func():

pass

episimlab.utils.rng module

episimlab.utils.rng.get_rng(seed: numbers.Number) numpy.random._generator.Generator

Generates a np.random.Generator instance from a seed.

episimlab.utils.tags module

episimlab.utils.tags.tag(tag_name)

Decorator factory that returns a tag_decorator, which adds string tag_name`s to the decorated object’s `TAGS attribute.

episimlab.utils.variable module

episimlab.utils.variable.any_negative(val, raise_err=False) bool

Checks if there are any negative values in the array. Accepts ndarray and DataArray types.

episimlab.utils.variable.clip_to_zero(val)

numpy.clip that accepts ndarray and DataArray types.

episimlab.utils.variable.coerce_to_da(proc, name: str, value, coords: Optional[dict] = None) xarray.core.dataarray.DataArray

Given a variable with name and value defined in process proc, retrieve the variable metadata and use it to coerce the value into an xarray.DataArray with the correct dimensions and coordinates. Returns value if variable is scalar (zero length dims attribute), DataArray otherwise.

episimlab.utils.variable.edge_weight_name(u, v) str

Attr name when looking for edge weight between nodes u and v.

episimlab.utils.variable.fix_coord_dtypes(da: xarray.core.dataarray.DataArray, max_len: Optional[int] = None) xarray.core.dataarray.DataArray

Changes coords with object dtype to unicode, e.g. <U5, where 5 would be max_len in this case. Workaround for missing object_codec for object array.

episimlab.utils.variable.get_var_dims(process, name: str) tuple

Given process-wrapped class process, retrieve the dims metadata attribute for variable with name.

episimlab.utils.variable.group_dict_by_var(d: dict) dict

Given a dictionary keyed by 2-length tuples, return a dictionary keyed only by the second element of the tuple.

episimlab.utils.variable.suffixed_dims(da: xarray.core.dataarray.DataArray, suffix: str, exclude: Optional[list] = None) dict

Generates a dictionary for suffixing DataArray or DataFrame dimensions. Compatible with methods such as DataFrame.rename and DataArray.rename.

episimlab.utils.variable.trim_data_to_coords(data: numpy.ndarray, coords: list) numpy.ndarray

Given a data array, trim the data to match the shape given by list of 2-length tuples coords, formatted like [(‘dim0’, range(10)), (‘dim0’, range(10)), …]. Be sure to check that order of dims in data is same as order of dims in coords.

episimlab.utils.variable.unsuffixed_dims(da: xarray.core.dataarray.DataArray, suffix: str) dict

Like suffixed_dims, but reverses the renaming transformation.

Module contents