delaynet.connectivities package#

Submodules#

delaynet.connectivities.continuous_ordinal_patterns module#

Continuous Ordinal Patterns (COP) connectivity metric.

delaynet.connectivities.continuous_ordinal_patterns.norm_window(ts: ndarray) ndarray[source]#

Normalise a window to values between -1 and 1.

delaynet.connectivities.continuous_ordinal_patterns.norm_windows(ts: ndarray, window_size: int) ndarray[source]#

Normalise sliding windows of a time series to values between -1 and 1.

Parameters:
Returns:

Normalised windows.

Return type:

numpy.ndarray

delaynet.connectivities.continuous_ordinal_patterns.pattern_distance(windows: ndarray, pattern: ndarray) ndarray[source]#

Compute the distance between the windows and a pattern.

Parameters:
Returns:

Distance between the windows and the pattern.

Return type:

numpy.ndarray

delaynet.connectivities.continuous_ordinal_patterns.pattern_transform(ts: ndarray, patterns: ndarray) ndarray[source]#

Transform time series using patterns.

Multiple time series can be transformed with multiple patterns at once. Patterns need to have the same length.

This function also accepts 1D time series and patterns. Wrapper for pattern_transform_2d().

Parameters:
  • ts (numpy.ndarray, shape=(n_ts, ts_len) or shape=(ts_len,)) – Time series.

  • patterns (numpy.ndarray, shape=(n_patterns, pattern_len) or shape=(pattern_len,)) – Patterns.

Returns:

Transformed time series.

Return type:

numpy.ndarray, shape=(n_ts, n_patterns, ts_len - pattern_len + 1) or if ts and/or patterns are 1D, the squeesed shape. For both 1D, the shape is (ts_len - pattern_len + 1).

delaynet.connectivities.continuous_ordinal_patterns.pattern_transform_2d(ts: ndarray, patterns: ndarray) ndarray[source]#

Transform time series using patterns.

Multiple time series can be transformed with multiple patterns at once. Patterns need to have the same length.

Use pattern_transform() for convenience if you only have one time series or one pattern to transform.

Parameters:
  • ts (numpy.ndarray, shape=(n_ts, ts_len)) – Time series.

  • patterns (numpy.ndarray, shape=(n_patterns, pattern_len)) – Patterns.

Returns:

Transformed time series.

Return type:

numpy.ndarray, shape=(n_ts, n_patterns, ts_len - pattern_len + 1)

delaynet.connectivities.continuous_ordinal_patterns.random_patterns(ts1, ts2, p_size=5, num_rnd_patterns=50, linear=True, lag_steps: int | list = None)[source]#

Continuous Ordinal Patterns (COP) connectivity metric [OMarinRodriguezAZ25, Zan23].

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

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

  • p_size (int) – Size of the ordinal pattern.

  • num_rnd_patterns (int) – Number of random patterns to consider.

  • linear (bool) – Start with the identity pattern.

  • lag_steps (int | list) – Time lags to consider. Can be a single integer or a list of integers. An integer will consider lags [1, …, lag_steps]. A list will consider the specified values as lags.

Returns:

Best p-value and corresponding lag.

Return type:

tuple[float, int]

delaynet.connectivities.granger module#

Granger Causality (GC) connectivity metric.

This module implements the Granger causality test, a renowned metric for assessing predictive causality between elements of a system. The Granger causality test operates on a simple and intuitive assumption: if element B is causing element A, then the past of B should contain information that aids in predicting the future of A. In other words, B has a role in shaping the future of A. [Die97, Gra69, KirchgassnerWH13]

Since its inception, the Granger causality test has found wide-ranging applications in various fields such as economics, engineering, sociology, biology, and neuroscience. It has also been adapted to cater to different situations and types of data. [Zan21]

Before using the Granger causality test, be sure to detrend the time series data. [BK84]

This module provides three implementations of the Granger causality test: a single-lag version, a multi-lag version, and a bidirectional multi-lag version.

delaynet.connectivities.granger.gt_multi_lag(ts1, ts2, lag_steps: int | list = None)[source]#

Granger Causality connectivity metric with variable time lag.

Testing for various time lags and selecting the one with the lowest p-value.

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

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

  • lag_steps (int | list) – Time lags to consider. Can be a single integer or a list of integers. An integer will consider lags [1, …, lag_steps]. A list will consider the specified values as lags.

Returns:

Best p-value and corresponding lag.

Return type:

tuple[float, int]

delaynet.connectivities.granger.gt_single_lag(ts1, ts2, lag_step)[source]#

Granger Causality (GC) connectivity metric with fixed time lag.

Testing causality of ts1 -> ts2 with a fixed time lag.

Parameters:
Returns:

Granger causality test p-value.

Return type:

float

delaynet.connectivities.gravity module#

Gravity connectivity metric.

delaynet.connectivities.gravity.grav(a, b)[source]#
delaynet.connectivities.gravity.gravity(ts1, ts2, lag_steps: int | list = None, n_tests: int = 20, rng: Generator | None = None)[source]#

Gravity connectivity (GC) metric.

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

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

  • lag_steps (int | list) – Time lags to consider. Can be a single integer or a list of integers. An integer will consider lags [1, …, lag_steps]. A list will consider the specified values as lags.

  • n_tests (int) – Number of iterations or resamples to perform within the hypothesis test.

  • rng (Generator | None) – Random number generator to resample from. If None, a default generator will be used.

Returns:

Best p-value and corresponding lag.

Return type:

tuple[float, int]

delaynet.connectivities.gravity.gravity_single(ts1, ts2, lag_step, n_tests: int = None, rng: Generator = None) float[source]#

Helper function for gravity connectivity metric.

This uses a permutation test to determine the p-value.

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

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

  • lag_step (int) – Time lag to consider.

  • n_tests (int) – Number of iterations or resamples to perform within the hypothesis test.

  • rng (Generator) – Random number generator to resample from.

Returns:

p-value of gravity connectivity.

Return type:

float

delaynet.connectivities.linear_correlation module#

Linear correlation (LC) connectivity metric.

delaynet.connectivities.linear_correlation.linear_correlation(ts1, ts2, lag_steps: int | list = None, **pr_kwargs)[source]#

Linear correlation (LC) connectivity metric.

LC measures the linear correlation between two time series over a specified number of time lags. The relevant value is the p-value of the Pearson correlation coefficient. This is returned by scipy.stats.pearsonr() as the second element of the returned tuple.

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

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

  • lag_steps (int | list) – Time lags to consider. Can be a single integer or a list of integers. An integer will consider lags [1, …, lag_steps]. A list will consider the specified values as lags.

  • pr_kwargs – Keyword arguments forwarded to scipy.stats.pearsonr().

Returns:

Best p-value and corresponding lag.

Return type:

tuple[float, int]

delaynet.connectivities.mutual_information module#

Mutual information (MI) connectivity metric.

delaynet.connectivities.mutual_information.mutual_information(ts1, ts2, approach: str = '', lag_steps: int | list = None, hypothesis_type: str = 'permutation_test', n_tests: int = 20, **mi_kwargs)[source]#

Mutual Information (MI) connectivity metric [ButhAZ25].

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

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

  • approach (str) – Approach to use. See infomeasure.mutual_information() for available approaches.

  • lag_steps (int | list) – Time lags to consider. Can be a single integer or a list of integers. An integer will consider lags [1, …, lag_steps]. A list will consider the specified values as lags.

  • hypothesis_type (str) – Type of hypothesis test to use. Either ‘permutation_test’ or ‘bootstrap’. Default is ‘permutation_test’.

  • n_tests (int) – Number of iterations or resamples to perform within the hypothesis test.

  • mi_kwargs – Additional keyword arguments for the mutual information estimator.

Returns:

Best p-value and corresponding lag.

Return type:

tuple[float, int]

Raises:

ValueError – If approach is not given.

delaynet.connectivities.rank_correlation module#

Rank correlation (RC) connectivity metric.

delaynet.connectivities.rank_correlation.rank_correlation(ts1, ts2, lag_steps: int | list = None, **sr_kwargs)[source]#

Rank correlation (RC) connectivity metric.

RC measures the spearman rank correlation coefficient between two time series over specified time lags. The interesting value is the p-value of the statistic, which is returned as the second element of the returned tuple in scipy.stats.spearmanr().

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

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

  • lag_steps (int | list) – Time lags to consider. Can be a single integer or a list of integers. An integer will consider lags [1, …, lag_steps]. A list will consider the specified values as lags.

  • sr_kwargs – Keyword arguments forwarded to scipy.stats.spearmanr().

Returns:

Best p-value and corresponding lag.

Return type:

tuple[float, int]

delaynet.connectivities.transfer_entropy module#

Transfer Entropy (TE) connectivity metric.

delaynet.connectivities.transfer_entropy.transfer_entropy(ts1, ts2, approach: str = '', lag_steps: int | list = None, hypothesis_type: str = 'permutation_test', n_tests: int = 20, **te_kwargs)[source]#

Transfer Entropy (TE) connectivity metric [ButhAZ25, Sch00].

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

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

  • approach (str) – Approach to use. See infomeasure.transfer_entropy() for available approaches.

  • lag_steps (int | list) – Time lags to consider. Can be a single integer or a list of integers. An integer will consider lags [1, …, lag_steps]. A list will consider the specified values as lags.

  • hypothesis_type (str) – Type of hypothesis test to use. Either ‘permutation_test’ or ‘bootstrap’. Default is ‘permutation_test’.

  • n_tests (int) – Number of iterations or resamples to perform within the hypothesis test.

  • te_kwargs – Additional keyword arguments for the transfer entropy estimator.

Returns:

Best p-value and corresponding lag.

Return type:

tuple[float, int]

Module contents#

Connectivities init, subpackage of delaynet.