delaynet.utils package#

Submodules#

delaynet.utils.bind_args module#

Function to bind args for the decorators of the connectivity and detrend functions.

delaynet.utils.bind_args.bind_args(func: callable, args: list, kwargs: dict) BoundArguments[source]#

Bind the arguments to the parameters of the function.

This will automatically raise a TypeError if a required argument is missing or an unknown argument is passed.

If kwargs have a key check_kwargs with value False, the kwargs are not checked for availability. This is useful if you want to pass unused keyword.

Parameters:
  • func (callable) – The function to bind the arguments to.

  • args (list) – The positional arguments to bind.

  • kwargs (dict) – The keyword arguments to bind.

Returns:

The bound arguments.

Return type:

BoundArguments

Raises:
  • TypeError – If a required argument is missing.

  • TypeError – If an unknown kwarg is passed.

delaynet.utils.dict_lookup module#

Dict lookup function, used for metrics and detrending methods.

delaynet.utils.dict_lookup.dict_lookup(lookup: dict) dict[source]#

Dict lookup function.

Parameters:

lookup (dict) – The lookup dict.

Returns:

The inverted lookup dict.

Return type:

dict

Raises:

TypeError – If the lookup is not a dict.

Example:
>>> dict_lookup({"a": 1, "b": 2, "c": 1})
{1: ["a", "c"], 2: ["b"]}
>>> dict_lookup({"a": 1, "b": 2, "c": 1, "d": 2})
{1: ["a", "c"], 2: ["b", "d"]}
>>> dict_lookup({})
{}
>>> dict_lookup(123)
Traceback (most recent call last):
    ...
TypeError: Expected dict, got <class 'int'>.

delaynet.utils.lag_steps module#

Utility functions to handle lag steps.

class delaynet.utils.lag_steps.Connectivity(*args, **kwargs)[source]#

Bases: Protocol[P]

A protocol for connectivity metrics.

Connectivity metrics are rigidly typed in their first three parameters: the time series and one lag step. The rest are optional keyword arguments.

delaynet.utils.lag_steps.assure_lag_list(lag_steps: int | list[int]) list[int][source]#

Ensure that lag_steps is a list of lags.

If lag_steps is an integer, it will be converted to a list containing integers from 1 to lag_steps. If lag_steps is already a list, it will be checked to ensure that all elements are integers.

Parameters:

lag_steps (int | list[int]) – An integer >= 1 or a list of integers.

Returns:

A list of integers

Type:

list[int]

Raises:

ValueError – If lag_steps is not an integer >= 1 or a list of integers.

delaynet.utils.lag_steps.find_optimal_lag(metric_func: Connectivity, ts1, ts2, lag_steps: list, op=<built-in function min>, **kwargs)[source]#

Find the optimal value and lag for a given metric function.

The optimal value and lag are determined by applying a given operation to a list of values obtained by applying metric_func for each lag in lag_steps. The operation can be min, max, or any other operation that takes a list of values and returns a single value. If metric_func returns a p-value, the operator should be the minimum (default optional parameter).

Parameters:
  • metric_func (Connectivity) – Function to compute the metric for a given lag step. Accepts time series ts1 and ts2, a lag step lag, and any additional keyword arguments.

  • ts1 (numpy.ndarray) – First time series.

  • ts2 (numpy.ndarray) – Second time series.

  • lag_steps (list) – Time lags to consider. Needs to be a list of integers.

  • op (Callable) – Operator to find the optimal lag step (e.g., default min() or max()).

  • kwargs – Additional keyword arguments to pass to the metric function.

Returns:

Optimal metric value and corresponding lag step.

Return type:

tuple[float, int]

delaynet.utils.logging module#

Logging configuration for delaynet.

Module contents#

Utility functions for delaynet.