py_champ.utility package

Submodules

py_champ.utility.util module

class py_champ.utility.util.BaseSchedulerByTypeFiltered(model: Model)

Bases: BaseScheduler

A scheduler that overrides the step method to allow for filtering of agents by .agt_type.

Example:

>>> scheduler = BaseSchedulerByTypeFiltered(model)
>>> scheduler.step(agt_type="Behavior")
step(agt_type=None) None

Execute the step of all the agents, one at a time.

do_each(method, agent_keys=None, shuffle=False, agt_type=None)
py_champ.utility.util.get_nested_attr(obj, attr_str)

A patch to collect a nested attribute using MESA’s datacollector.

This function is used to get a nested attribute from an object. For example, if we have an object with an attribute “a” that is a dictionary with a key “b”, we can get the value of “b” by calling get_nested_attr(obj, “a.b”). This is useful when collecting data from agents in a model.

Parameters:
  • obj (object) – An object.

  • attr_str (str) – A string of nested attributes separated by a period.

Returns:

The nested attribute.

Return type:

object

py_champ.utility.util.get_agt_attr(attr_str)

Get a nested attribute from an agent object.

This replaces, e.g., lambda a: getattr(a, “satisfaction”, None) We have to do this to return None if the attribute is not exist in the given agent type. def func(agent):

return getattr(agent, attr, None).

Parameters:

attr_str (str) – A string of nested attributes separated by a period.

Returns:

A function that returns the nested attribute.

Return type:

function

py_champ.utility.util.dict_to_string(dictionary, prefix='', indentor='  ', level=2)

Ture a dictionary into a printable string.

Parameters:
  • dictionary (dict) – A dictionary.

  • prefix (str, optional) – Prefix, by default “”.

  • indentor (str, optional) – Indentor, by default ” “.

  • level (int, optional) – Level of indentation, by default 2.

Returns:

A printable string.

Return type:

str

class py_champ.utility.util.TimeRecorder

Bases: object

A class for recording time.

get_elapsed_time(event=None, strf=True)

Get elapsed time since the start of the recorder.

Parameters:
  • event (str, optional) – Record event, by default None.

  • strf (bool, optional) – Convert seconds to string format, by default True.

Returns:

  • float or str

  • Elapsed time or string format of the elapsed time.

static sec2str(secs, fmt='%H:%M:%S')

Convert seconds to string format.

Parameters:
  • secs (int) – Seconds.

  • fmt (str, optional) – Format, by default “%H:%M:%S”.

Returns:

A string.

Return type:

str

class py_champ.utility.util.Indicator

Bases: object

A class for calculating indicators.

static remove_na(x_obv, y_sim)

Remove nan in x_obv and y_sim.

This function makes sure there is no nan involves in the indicator calculation. If nan is detected, data points will be remove from x_obv and y_sim simultaneously.

Parameters:
  • x_obv (array) – Observation data.

  • y_sim (array) – Simulation data.

Returns:

Updated (x_obv, y_sim)

Return type:

tuple

static cal_indicator_df(x_obv, y_sim, index_name='value', indicators_list=None, r_na=True)

Calculate indicators and return as a DataFrame.

Parameters:
  • x_obv (array) – Observation data.

  • y_sim (array) – Simulation data.

  • index_name (str, optional) – Index name, by default “value”.

  • indicators_list (list, optional) – List of indicators, by default None.

  • r_na (bool, optional) – Remove nan, by default True.

Returns:

A DataFrame of indicators.

Return type:

DataFrame

static get_r(x_obv, y_sim, r_na=True)

Correlation.

Parameters:
  • x_obv (array) – Observation data.

  • y_sim (array) – Simulation data.

  • r_na (bool, optional) – Remove nan, by default True

Returns:

r coefficient.

Return type:

float

static get_r2(x_obv, y_sim, r_na=True)

Coefficient of determination.

Parameters:
  • x_obv (array) – Observation data.

  • y_sim (array) – Simulation data.

  • r_na (bool, optional) – Remove nan, by default True

Returns:

r2 coefficient.

Return type:

float

static get_rmse(x_obv, y_sim, r_na=False)

Root mean square error.

Parameters:
  • x_obv (array) – Observation data.

  • y_sim (array) – Simulation data.

  • r_na (bool, optional) – Remove nan, by default True

Returns:

Root mean square error.

Return type:

float

static get_nse(x_obv, y_sim, r_na=False)

Nash-Sutcliffe efficiency.

Parameters:
  • x_obv (array) – Observation data.

  • y_sim (array) – Simulation data.

  • r_na (bool, optional) – Remove nan, by default True

Returns:

Nash-Sutcliffe efficiency.

Return type:

float

static get_inse(x_obv, y_sim, r_na=False)

Inverse Nash-Sutcliffe efficiency.

Parameters:
  • x_obv (array) – Observation data.

  • y_sim (array) – Simulation data.

  • r_na (bool, optional) – Remove nan, by default True

Returns:

Inverse Nash-Sutcliffe efficiency.

Return type:

float

static get_cp(x_obv, y_sim, r_na=False)

Correlation of persistence.

Parameters:
  • x_obv (array) – Observation data.

  • y_sim (array) – Simulation data.

  • r_na (bool, optional) – Remove nan, by default True

Returns:

Correlation of persistence.

Return type:

float

static get_rsr(x_obv, y_sim, r_na=False)

RMSE-observations standard deviation ratio.

Parameters:
  • x_obv (array) – Observation data.

  • y_sim (array) – Simulation data.

  • r_na (bool, optional) – Remove nan, by default True

Returns:

RMSE-observations standard deviation ratio.

Return type:

float

static get_kge(x_obv, y_sim, r_na=True)

Kling-Gupta efficiency.

Parameters:
  • x_obv (array) – Observation data.

  • y_sim (array) – Simulation data.

  • r_na (bool, optional) – Remove nan, by default True

Returns:

Kling-Gupta efficiency.

Return type:

float

static get_ikge(x_obv, y_sim, r_na=True)

Inverse Kling-Gupta efficiency.

Parameters:
  • x_obv (array) – Observation data.

  • y_sim (array) – Simulation data.

  • r_na (bool, optional) – Remove nan, by default True

Returns:

Inverse Kling-Gupta efficiency.

Return type:

float

Module contents

Provides utility functions and classes for the PyCHAMP package.