# Compute directionality of connectivity with multivariate Granger causality#

This example demonstrates how Granger causality based on state-space models [1] can be used to compute directed connectivity between sensors in a multivariate manner. Furthermore, the use of time-reversal for improving the robustness of directed connectivity estimates to noise in the data is discussed [2].

# Author: Thomas S. Binns <t.s.binns@outlook.com>
# sphinx_gallery_thumbnail_number = 3

import mne
import numpy as np
from matplotlib import pyplot as plt
from mne.datasets.fieldtrip_cmc import data_path

from mne_connectivity import spectral_connectivity_epochs


## Background#

Multivariate forms of signal analysis allow you to simultaneously consider the activity of multiple signals. In the case of connectivity, the interaction between multiple sensors can be analysed at once, producing a single connectivity spectrum. This approach brings not only practical benefits (e.g. easier interpretability of results from the dimensionality reduction), but can also offer methodological improvements (e.g. enhanced signal-to-noise ratio and reduced bias).

Additionally, it can be of interest to examine the directionality of connectivity between signals, providing additional clarity to how information flows in a system. One such directed measure of connectivity is Granger causality (GC). A signal, $$\boldsymbol{x}$$, is said to Granger-cause another signal, $$\boldsymbol{y}$$, if information from the past of $$\boldsymbol{x}$$ improves the prediction of the present of $$\boldsymbol{y}$$ over the case where only information from the past of $$\boldsymbol{y}$$ is used. Note: GC does not make any assertions about the true causality between signals.

The degree to which $$\boldsymbol{x}$$ and $$\boldsymbol{y}$$ can be used to predict one another in a linear model can be quantified using vector autoregressive (VAR) models. Considering the simpler case of time domain connectivity, the VAR models are as follows:

$$y_t = \sum_{k=1}^{K} a_k y_{t-k} + \xi_t^y$$ , $$Var(\xi_t^y) := \Sigma_y$$ ,

and $$\boldsymbol{z}_t = \sum_{k=1}^K \boldsymbol{A}_k \boldsymbol{z}_{t-k} + \boldsymbol{\epsilon}_t$$ , $$\boldsymbol{\Sigma} := \langle \boldsymbol{\epsilon}_t \boldsymbol{\epsilon}_t^T \rangle = \begin{bmatrix} \Sigma_{xx} & \Sigma_{xy} \\ \Sigma_{yx} & \Sigma_{yy} \end{bmatrix}$$ ,

representing the reduced and full VAR models, respectively, where: $$K$$ is the order of the VAR model, determining the number of lags, $$k$$, used; $$\boldsymbol{Z} := \begin{bmatrix} \boldsymbol{x} \\ \boldsymbol{y} \end{bmatrix}$$; $$\boldsymbol{A}$$ is a matrix of coefficients explaining the contribution of past entries of $$\boldsymbol{Z}$$ to its current value; and $$\xi$$ and $$\boldsymbol{\epsilon}$$ are the residuals of the VAR models. In this way, the information of the signals at time $$t$$ can be represented as a weighted form of the information from the previous timepoints, plus some residual information not encoded in the signals’ past. In practice, VAR model parameters are computed from an autocovariance sequence generated from the time-series data using the Yule-Walker equations [3].

The residuals, or errors, represent how much information about the present state of the signals is not explained by their past. We can therefore estimate how much $$\boldsymbol{x}$$ Granger-causes $$\boldsymbol{y}$$ by comparing the variance of the residuals of the reduced VAR model ($$\Sigma_y$$; i.e. how much the present of $$\boldsymbol{y}$$ is not explained by its own past) and of the full VAR model ($$\Sigma_{yy}$$; i.e. how much the present of $$\boldsymbol{y}$$ is not explained by both its own past and that of $$\boldsymbol{x}$$):

$$F_{x \rightarrow y} = ln \Large{(\frac{\Sigma_y}{\Sigma_{yy}})}$$ ,

where $$F$$ is the Granger score. For example, if $$\boldsymbol{x}$$ contains no information about $$\boldsymbol{y}$$, the residuals of the reduced and full VAR models will be identical, and $$F_{x \rightarrow y}$$ will naturally be 0, indicating that information from $$\boldsymbol{x}$$ does not flow to $$\boldsymbol{y}$$. In contrast, if $$\boldsymbol{x}$$ does help to predict $$\boldsymbol{y}$$, the residual of the full model will be smaller than that of the reduced model. $$\Large{\frac{\Sigma_y} {\Sigma_{yy}}}$$ will therefore be greater than 1, leading to a Granger score > 0. Granger scores are bound between $$[0, \infty)$$.

These same principles apply to spectral GC, which provides information about the directionality of connectivity for individual frequencies. For spectral GC, the autocovariance sequence is generated from an inverse Fourier transform applied to the cross-spectral density of the signals. Additionally, a spectral transfer function is used to translate information from the VAR models back into the frequency domain before computing the final Granger scores.

Barnett and Seth (2015) [1] have defined a multivariate form of spectral GC based on state-space models, enabling the estimation of information flow between whole sets of signals simultaneously:

$$F_{A \rightarrow B}(f) = \Re ln \Large{(\frac{ det(\boldsymbol{S}_{BB}(f))}{det(\boldsymbol{S}_{BB}(f) - \boldsymbol{H}_{BA}(f) \boldsymbol{\Sigma}_{AA \lvert B} \boldsymbol{H}_{BA}^*(f))})}$$ ,

where: $$A$$ and $$B$$ are the seeds and targets, respectively; $$f$$ is a given frequency; $$\boldsymbol{H}$$ is the spectral transfer function; $$\boldsymbol{\Sigma}$$ is the innovations form residuals’ covariance matrix of the state-space model; $$\boldsymbol{S}$$ is $$\boldsymbol{\Sigma}$$ transformed by $$\boldsymbol{H}$$; and $$\boldsymbol{\Sigma}_{IJ \lvert K} := \boldsymbol{\Sigma}_{IJ} - \boldsymbol{\Sigma}_{IK} \boldsymbol{\Sigma}_{KK}^{-1} \boldsymbol{\Sigma}_{KJ}$$, representing a partial covariance matrix. The same principles apply as before: a numerator greater than the denominator means that information from the seed signals aids the prediction of activity in the target signals, leading to a Granger score > 0.

There are several benefits to a state-space approach for computing GC: compared to traditional autoregressive-based approaches, the use of state-space models offers reduced statistical bias and increased statistical power; furthermore, the dimensionality reduction offered by the multivariate nature of the approach can aid in the interpretability and subsequent analysis of the results.

To demonstrate the use of GC for estimating directed connectivity, we start by loading some example MEG data and dividing it into two-second-long epochs.

raw = mne.io.read_raw_ctf(data_path() / "SubjectCMC.ds")
raw.pick("mag")
raw.notch_filter(50)
raw.resample(100)


  0%|                                               | 0.00/329M [00:00<?, ?B/s]
0%|                                       | 561k/329M [00:00<01:12, 4.52MB/s]
0%|▏                                     | 1.33M/329M [00:00<00:53, 6.14MB/s]
1%|▎                                     | 2.38M/329M [00:00<00:40, 8.00MB/s]
1%|▎                                     | 3.20M/329M [00:00<00:40, 8.08MB/s]
1%|▍                                     | 4.21M/329M [00:00<00:37, 8.62MB/s]
2%|▌                                     | 5.25M/329M [00:00<00:36, 8.89MB/s]
2%|▋                                     | 6.30M/329M [00:00<00:35, 9.06MB/s]
2%|▊                                     | 7.21M/329M [00:00<00:36, 8.93MB/s]
2%|▉                                     | 8.10M/329M [00:00<00:38, 8.30MB/s]
3%|█                                     | 8.94M/329M [00:01<00:48, 6.60MB/s]
3%|█                                     | 9.70M/329M [00:01<00:48, 6.55MB/s]
3%|█▏                                    | 10.5M/329M [00:01<00:47, 6.70MB/s]
4%|█▎                                    | 11.5M/329M [00:01<00:42, 7.40MB/s]
4%|█▍                                    | 12.6M/329M [00:01<00:39, 8.03MB/s]
4%|█▌                                    | 13.6M/329M [00:01<00:36, 8.57MB/s]
4%|█▋                                    | 14.5M/329M [00:01<00:37, 8.31MB/s]
5%|█▊                                    | 15.5M/329M [00:01<00:38, 8.25MB/s]
5%|█▉                                    | 16.3M/329M [00:02<00:40, 7.80MB/s]
5%|█▉                                    | 17.1M/329M [00:02<00:42, 7.42MB/s]
5%|██                                    | 17.8M/329M [00:02<00:42, 7.40MB/s]
6%|██▏                                   | 18.8M/329M [00:02<00:38, 8.00MB/s]
6%|██▎                                   | 19.6M/329M [00:02<00:39, 7.76MB/s]
6%|██▍                                   | 20.7M/329M [00:02<00:38, 7.95MB/s]
7%|██▍                                   | 21.5M/329M [00:02<00:40, 7.66MB/s]
7%|██▌                                   | 22.5M/329M [00:02<00:39, 7.80MB/s]
7%|██▋                                   | 23.3M/329M [00:02<00:39, 7.78MB/s]
7%|██▊                                   | 24.3M/329M [00:03<00:37, 8.13MB/s]
8%|██▉                                   | 25.4M/329M [00:03<00:35, 8.48MB/s]
8%|███                                   | 26.4M/329M [00:03<00:34, 8.66MB/s]
8%|███▏                                  | 27.5M/329M [00:03<00:33, 9.00MB/s]
9%|███▎                                  | 28.4M/329M [00:03<00:33, 8.91MB/s]
9%|███▍                                  | 29.3M/329M [00:03<00:34, 8.63MB/s]
9%|███▍                                  | 30.1M/329M [00:03<00:42, 7.03MB/s]
9%|███▌                                  | 30.9M/329M [00:03<00:46, 6.40MB/s]
10%|███▋                                  | 31.9M/329M [00:04<00:43, 6.83MB/s]
10%|███▊                                  | 32.6M/329M [00:04<00:45, 6.46MB/s]
10%|███▊                                  | 33.3M/329M [00:04<00:45, 6.49MB/s]
10%|███▉                                  | 34.3M/329M [00:04<00:43, 6.78MB/s]
11%|████                                  | 35.0M/329M [00:04<00:43, 6.71MB/s]
11%|████                                  | 35.6M/329M [00:04<00:44, 6.62MB/s]
11%|████▏                                 | 36.4M/329M [00:04<00:44, 6.57MB/s]
11%|████▎                                 | 37.4M/329M [00:04<00:39, 7.36MB/s]
12%|████▍                                 | 38.4M/329M [00:05<00:36, 7.96MB/s]
12%|████▌                                 | 39.2M/329M [00:05<00:36, 7.86MB/s]
12%|████▌                                 | 40.0M/329M [00:05<00:39, 7.36MB/s]
12%|████▋                                 | 40.8M/329M [00:05<00:40, 7.11MB/s]
13%|████▊                                 | 41.5M/329M [00:05<00:43, 6.63MB/s]
13%|████▊                                 | 42.2M/329M [00:05<00:43, 6.58MB/s]
13%|████▉                                 | 42.9M/329M [00:05<00:43, 6.52MB/s]
13%|█████                                 | 43.7M/329M [00:05<00:42, 6.75MB/s]
14%|█████▏                                | 44.5M/329M [00:05<00:42, 6.76MB/s]
14%|█████▎                                | 45.5M/329M [00:06<00:37, 7.52MB/s]
14%|█████▎                                | 46.3M/329M [00:06<00:39, 7.17MB/s]
14%|█████▍                                | 47.1M/329M [00:06<00:39, 7.09MB/s]
15%|█████▌                                | 47.9M/329M [00:06<00:38, 7.24MB/s]
15%|█████▌                                | 48.7M/329M [00:06<00:38, 7.35MB/s]
15%|█████▊                                | 50.0M/329M [00:06<00:34, 8.17MB/s]
15%|█████▉                                | 51.0M/329M [00:06<00:32, 8.64MB/s]
16%|█████▉                                | 51.9M/329M [00:06<00:32, 8.65MB/s]
16%|██████                                | 52.8M/329M [00:06<00:32, 8.64MB/s]
16%|██████▏                               | 53.9M/329M [00:07<00:29, 9.31MB/s]
17%|██████▎                               | 55.0M/329M [00:07<00:28, 9.62MB/s]
17%|██████▍                               | 56.2M/329M [00:07<00:27, 9.83MB/s]
17%|██████▌                               | 57.3M/329M [00:07<00:27, 9.84MB/s]
18%|██████▋                               | 58.3M/329M [00:07<00:27, 9.99MB/s]
18%|██████▉                               | 59.6M/329M [00:07<00:28, 9.62MB/s]
18%|███████                               | 60.7M/329M [00:07<00:27, 9.71MB/s]
19%|███████                               | 61.7M/329M [00:07<00:28, 9.24MB/s]
19%|███████▏                              | 62.6M/329M [00:07<00:30, 8.88MB/s]
19%|███████▎                              | 63.7M/329M [00:08<00:28, 9.44MB/s]
20%|███████▍                              | 64.6M/329M [00:08<00:29, 9.04MB/s]
20%|███████▌                              | 65.6M/329M [00:08<00:30, 8.75MB/s]
20%|███████▋                              | 67.0M/329M [00:08<00:28, 9.29MB/s]
21%|███████▊                              | 68.0M/329M [00:08<00:27, 9.63MB/s]
21%|███████▉                              | 69.0M/329M [00:08<00:28, 9.29MB/s]
21%|████████                              | 70.1M/329M [00:08<00:27, 9.57MB/s]
22%|████████▏                             | 71.1M/329M [00:08<00:27, 9.51MB/s]
22%|████████▎                             | 72.2M/329M [00:08<00:26, 9.66MB/s]
22%|████████▍                             | 73.2M/329M [00:09<00:26, 9.77MB/s]
23%|████████▌                             | 74.3M/329M [00:09<00:26, 9.53MB/s]
23%|████████▋                             | 75.2M/329M [00:09<00:27, 9.30MB/s]
23%|████████▊                             | 76.2M/329M [00:09<00:27, 9.15MB/s]
23%|████████▉                             | 77.2M/329M [00:09<00:26, 9.42MB/s]
24%|█████████                             | 78.5M/329M [00:09<00:25, 9.71MB/s]
24%|█████████▏                            | 79.4M/329M [00:09<00:26, 9.32MB/s]
24%|█████████▎                            | 80.4M/329M [00:09<00:27, 8.98MB/s]
25%|█████████▍                            | 81.3M/329M [00:09<00:29, 8.36MB/s]
25%|█████████▌                            | 82.4M/329M [00:10<00:27, 8.90MB/s]
25%|█████████▋                            | 83.4M/329M [00:10<00:26, 9.17MB/s]
26%|█████████▊                            | 84.7M/329M [00:10<00:24, 10.2MB/s]
26%|█████████▉                            | 85.9M/329M [00:10<00:23, 10.5MB/s]
26%|██████████                            | 86.9M/329M [00:10<00:22, 10.5MB/s]
27%|██████████▏                           | 88.0M/329M [00:10<00:22, 10.5MB/s]
27%|██████████▎                           | 89.1M/329M [00:10<00:22, 10.8MB/s]
27%|██████████▍                           | 90.2M/329M [00:10<00:23, 10.4MB/s]
28%|██████████▌                           | 91.3M/329M [00:10<00:24, 9.68MB/s]
28%|██████████▋                           | 92.3M/329M [00:11<00:24, 9.52MB/s]
28%|██████████▊                           | 93.4M/329M [00:11<00:24, 9.62MB/s]
29%|██████████▉                           | 94.4M/329M [00:11<00:24, 9.77MB/s]
29%|███████████                           | 95.4M/329M [00:11<00:24, 9.60MB/s]
29%|███████████                           | 96.4M/329M [00:11<00:25, 9.19MB/s]
30%|███████████▏                          | 97.4M/329M [00:11<00:24, 9.49MB/s]
30%|███████████▎                          | 98.3M/329M [00:11<00:25, 9.07MB/s]
30%|███████████▍                          | 99.2M/329M [00:11<00:26, 8.71MB/s]
30%|███████████▊                           | 100M/329M [00:11<00:27, 8.35MB/s]
31%|███████████▉                           | 101M/329M [00:12<00:27, 8.25MB/s]
31%|████████████                           | 102M/329M [00:12<00:26, 8.60MB/s]
31%|████████████▏                          | 103M/329M [00:12<00:30, 7.38MB/s]
31%|████████████▎                          | 104M/329M [00:12<00:34, 6.50MB/s]
32%|████████████▎                          | 104M/329M [00:12<00:33, 6.68MB/s]
32%|████████████▍                          | 105M/329M [00:12<00:33, 6.77MB/s]
32%|████████████▌                          | 106M/329M [00:12<00:28, 7.71MB/s]
32%|████████████▋                          | 107M/329M [00:12<00:29, 7.45MB/s]
33%|████████████▊                          | 108M/329M [00:13<00:28, 7.81MB/s]
33%|████████████▉                          | 109M/329M [00:13<00:26, 8.31MB/s]
33%|█████████████                          | 110M/329M [00:13<00:27, 8.07MB/s]
34%|█████████████                          | 111M/329M [00:13<00:27, 8.06MB/s]
34%|█████████████▏                         | 112M/329M [00:13<00:24, 8.82MB/s]
34%|█████████████▎                         | 113M/329M [00:13<00:25, 8.44MB/s]
34%|█████████████▍                         | 113M/329M [00:13<00:27, 7.84MB/s]
35%|█████████████▌                         | 114M/329M [00:13<00:32, 6.61MB/s]
35%|█████████████▌                         | 115M/329M [00:14<00:37, 5.67MB/s]
35%|█████████████▋                         | 116M/329M [00:14<00:39, 5.46MB/s]
35%|█████████████▊                         | 116M/329M [00:14<00:38, 5.51MB/s]
36%|█████████████▉                         | 117M/329M [00:14<00:37, 5.58MB/s]
36%|█████████████▉                         | 118M/329M [00:14<00:35, 6.00MB/s]
36%|██████████████                         | 119M/329M [00:14<00:35, 5.99MB/s]
36%|██████████████▏                        | 119M/329M [00:14<00:33, 6.25MB/s]
37%|██████████████▎                        | 121M/329M [00:14<00:30, 6.77MB/s]
37%|██████████████▎                        | 121M/329M [00:15<00:30, 6.74MB/s]
37%|██████████████▍                        | 122M/329M [00:15<00:31, 6.67MB/s]
37%|██████████████▌                        | 123M/329M [00:15<00:29, 6.97MB/s]
38%|██████████████▋                        | 124M/329M [00:15<00:26, 7.71MB/s]
38%|██████████████▊                        | 125M/329M [00:15<00:25, 8.11MB/s]
38%|██████████████▉                        | 126M/329M [00:15<00:22, 9.09MB/s]
39%|███████████████                        | 127M/329M [00:15<00:21, 9.24MB/s]
39%|███████████████▏                       | 128M/329M [00:15<00:21, 9.22MB/s]
39%|███████████████▎                       | 129M/329M [00:15<00:22, 8.89MB/s]
40%|███████████████▍                       | 130M/329M [00:16<00:25, 7.90MB/s]
40%|███████████████▌                       | 131M/329M [00:16<00:29, 6.77MB/s]
40%|███████████████▌                       | 132M/329M [00:16<00:29, 6.80MB/s]
40%|███████████████▋                       | 132M/329M [00:16<00:30, 6.46MB/s]
40%|███████████████▊                       | 133M/329M [00:16<00:34, 5.72MB/s]
41%|███████████████▊                       | 134M/329M [00:16<00:33, 5.92MB/s]
41%|███████████████▉                       | 134M/329M [00:16<00:34, 5.61MB/s]
41%|███████████████▉                       | 135M/329M [00:17<00:37, 5.18MB/s]
41%|████████████████                       | 136M/329M [00:17<00:38, 4.97MB/s]
41%|████████████████▏                      | 136M/329M [00:17<00:37, 5.10MB/s]
42%|████████████████▏                      | 137M/329M [00:17<00:34, 5.51MB/s]
42%|████████████████▎                      | 138M/329M [00:17<00:33, 5.76MB/s]
42%|████████████████▍                      | 139M/329M [00:17<00:27, 6.93MB/s]
42%|████████████████▌                      | 140M/329M [00:17<00:24, 7.58MB/s]
43%|████████████████▋                      | 141M/329M [00:17<00:23, 7.89MB/s]
43%|████████████████▊                      | 141M/329M [00:17<00:24, 7.71MB/s]
43%|████████████████▉                      | 143M/329M [00:18<00:23, 8.03MB/s]
44%|█████████████████                      | 144M/329M [00:18<00:21, 8.62MB/s]
44%|█████████████████                      | 144M/329M [00:18<00:23, 7.72MB/s]
44%|█████████████████▏                     | 145M/329M [00:18<00:23, 7.91MB/s]
44%|█████████████████▎                     | 146M/329M [00:18<00:23, 7.82MB/s]
45%|█████████████████▍                     | 147M/329M [00:18<00:25, 7.25MB/s]
45%|█████████████████▍                     | 148M/329M [00:18<00:25, 7.19MB/s]
45%|█████████████████▌                     | 148M/329M [00:18<00:27, 6.67MB/s]
45%|█████████████████▋                     | 149M/329M [00:18<00:27, 6.43MB/s]
46%|█████████████████▊                     | 150M/329M [00:19<00:26, 6.71MB/s]
46%|█████████████████▊                     | 151M/329M [00:19<00:27, 6.48MB/s]
46%|█████████████████▉                     | 151M/329M [00:19<00:30, 5.79MB/s]
46%|██████████████████                     | 152M/329M [00:19<00:31, 5.69MB/s]
46%|██████████████████                     | 153M/329M [00:19<00:28, 6.11MB/s]
47%|██████████████████▏                    | 154M/329M [00:19<00:24, 7.15MB/s]
47%|██████████████████▎                    | 155M/329M [00:19<00:23, 7.27MB/s]
47%|██████████████████▍                    | 156M/329M [00:19<00:21, 8.04MB/s]
48%|██████████████████▌                    | 157M/329M [00:20<00:21, 8.06MB/s]
48%|██████████████████▋                    | 157M/329M [00:20<00:23, 7.24MB/s]
48%|██████████████████▋                    | 158M/329M [00:20<00:24, 6.99MB/s]
48%|██████████████████▊                    | 159M/329M [00:20<00:25, 6.78MB/s]
49%|██████████████████▉                    | 160M/329M [00:20<00:24, 6.85MB/s]
49%|███████████████████                    | 161M/329M [00:20<00:22, 7.38MB/s]
49%|███████████████████▏                   | 162M/329M [00:20<00:22, 7.48MB/s]
49%|███████████████████▏                   | 162M/329M [00:20<00:22, 7.54MB/s]
50%|███████████████████▎                   | 163M/329M [00:20<00:21, 7.88MB/s]
50%|███████████████████▍                   | 164M/329M [00:21<00:20, 8.19MB/s]
50%|███████████████████▌                   | 166M/329M [00:21<00:19, 8.52MB/s]
51%|███████████████████▋                   | 167M/329M [00:21<00:18, 8.63MB/s]
51%|███████████████████▊                   | 168M/329M [00:21<00:19, 8.36MB/s]
51%|███████████████████▉                   | 168M/329M [00:21<00:19, 8.13MB/s]
51%|████████████████████                   | 169M/329M [00:21<00:19, 8.19MB/s]
52%|████████████████████▏                  | 170M/329M [00:21<00:21, 7.45MB/s]
52%|████████████████████▎                  | 171M/329M [00:21<00:21, 7.43MB/s]
52%|████████████████████▍                  | 172M/329M [00:22<00:20, 7.56MB/s]
52%|████████████████████▍                  | 173M/329M [00:22<00:21, 7.21MB/s]
53%|████████████████████▌                  | 174M/329M [00:22<00:21, 7.32MB/s]
53%|████████████████████▋                  | 174M/329M [00:22<00:20, 7.45MB/s]
53%|████████████████████▊                  | 175M/329M [00:22<00:19, 8.07MB/s]
54%|████████████████████▉                  | 176M/329M [00:22<00:19, 7.92MB/s]
54%|████████████████████▉                  | 177M/329M [00:22<00:19, 7.91MB/s]
54%|█████████████████████                  | 178M/329M [00:22<00:22, 6.88MB/s]
54%|█████████████████████▏                 | 179M/329M [00:22<00:20, 7.32MB/s]
55%|█████████████████████▎                 | 179M/329M [00:23<00:20, 7.30MB/s]
55%|█████████████████████▎                 | 180M/329M [00:23<00:19, 7.52MB/s]
55%|█████████████████████▍                 | 181M/329M [00:23<00:21, 6.92MB/s]
55%|█████████████████████▌                 | 182M/329M [00:23<00:22, 6.43MB/s]
55%|█████████████████████▌                 | 182M/329M [00:23<00:25, 5.84MB/s]
56%|█████████████████████▋                 | 183M/329M [00:23<00:25, 5.78MB/s]
56%|█████████████████████▋                 | 184M/329M [00:23<00:30, 4.81MB/s]
56%|█████████████████████▊                 | 184M/329M [00:23<00:30, 4.81MB/s]
56%|█████████████████████▉                 | 185M/329M [00:24<00:26, 5.45MB/s]
56%|█████████████████████▉                 | 185M/329M [00:24<00:28, 5.10MB/s]
56%|██████████████████████                 | 186M/329M [00:24<00:28, 5.02MB/s]
57%|██████████████████████                 | 186M/329M [00:24<00:29, 4.76MB/s]
57%|██████████████████████▏                | 187M/329M [00:24<00:27, 5.25MB/s]
57%|██████████████████████▎                | 188M/329M [00:24<00:24, 5.82MB/s]
57%|██████████████████████▎                | 189M/329M [00:24<00:22, 6.15MB/s]
58%|██████████████████████▍                | 190M/329M [00:24<00:19, 7.14MB/s]
58%|██████████████████████▌                | 191M/329M [00:24<00:17, 8.01MB/s]
58%|██████████████████████▋                | 192M/329M [00:25<00:16, 8.27MB/s]
59%|██████████████████████▊                | 193M/329M [00:25<00:17, 7.94MB/s]
59%|██████████████████████▉                | 194M/329M [00:25<00:16, 8.14MB/s]
59%|███████████████████████                | 195M/329M [00:25<00:16, 8.33MB/s]
59%|███████████████████████▏               | 196M/329M [00:25<00:15, 8.78MB/s]
60%|███████████████████████▎               | 197M/329M [00:25<00:14, 9.05MB/s]
60%|███████████████████████▍               | 198M/329M [00:25<00:15, 8.55MB/s]
60%|███████████████████████▌               | 199M/329M [00:25<00:15, 8.51MB/s]
61%|███████████████████████▋               | 199M/329M [00:25<00:15, 8.30MB/s]
61%|███████████████████████▋               | 200M/329M [00:26<00:16, 7.81MB/s]
61%|███████████████████████▊               | 201M/329M [00:26<00:17, 7.53MB/s]
61%|███████████████████████▉               | 202M/329M [00:26<00:15, 8.30MB/s]
62%|████████████████████████               | 203M/329M [00:26<00:17, 7.20MB/s]
62%|████████████████████████▏              | 204M/329M [00:26<00:17, 7.26MB/s]
62%|████████████████████████▎              | 205M/329M [00:26<00:16, 7.67MB/s]
62%|████████████████████████▎              | 206M/329M [00:26<00:16, 7.61MB/s]
63%|████████████████████████▍              | 206M/329M [00:26<00:15, 7.88MB/s]
63%|████████████████████████▌              | 207M/329M [00:27<00:16, 7.30MB/s]
63%|████████████████████████▋              | 208M/329M [00:27<00:15, 7.71MB/s]
63%|████████████████████████▊              | 209M/329M [00:27<00:14, 8.11MB/s]
64%|████████████████████████▊              | 210M/329M [00:27<00:14, 7.96MB/s]
64%|████████████████████████▉              | 211M/329M [00:27<00:14, 8.14MB/s]
64%|█████████████████████████              | 212M/329M [00:27<00:16, 7.06MB/s]
65%|█████████████████████████▏             | 213M/329M [00:27<00:17, 6.74MB/s]
65%|█████████████████████████▎             | 213M/329M [00:27<00:16, 7.21MB/s]
65%|█████████████████████████▍             | 214M/329M [00:27<00:14, 7.81MB/s]
65%|█████████████████████████▌             | 215M/329M [00:28<00:13, 8.44MB/s]
66%|█████████████████████████▋             | 216M/329M [00:28<00:12, 8.70MB/s]
66%|█████████████████████████▊             | 218M/329M [00:28<00:11, 9.48MB/s]
66%|█████████████████████████▉             | 219M/329M [00:28<00:11, 9.31MB/s]
67%|██████████████████████████             | 220M/329M [00:28<00:11, 9.19MB/s]
67%|██████████████████████████▏            | 221M/329M [00:28<00:11, 9.53MB/s]
67%|██████████████████████████▎            | 222M/329M [00:28<00:11, 9.37MB/s]
68%|██████████████████████████▍            | 223M/329M [00:28<00:11, 9.65MB/s]
68%|██████████████████████████▌            | 224M/329M [00:28<00:10, 10.1MB/s]
68%|██████████████████████████▋            | 225M/329M [00:29<00:10, 9.89MB/s]
69%|██████████████████████████▊            | 226M/329M [00:29<00:10, 9.94MB/s]
69%|██████████████████████████▉            | 227M/329M [00:29<00:10, 10.1MB/s]
69%|███████████████████████████            | 228M/329M [00:29<00:09, 10.1MB/s]
70%|███████████████████████████▏           | 230M/329M [00:29<00:09, 10.1MB/s]
70%|███████████████████████████▎           | 231M/329M [00:29<00:09, 9.87MB/s]
70%|███████████████████████████▍           | 232M/329M [00:29<00:10, 9.52MB/s]
71%|███████████████████████████▌           | 233M/329M [00:29<00:10, 9.14MB/s]
71%|███████████████████████████▋           | 233M/329M [00:29<00:10, 8.99MB/s]
71%|███████████████████████████▊           | 235M/329M [00:30<00:10, 9.42MB/s]
72%|███████████████████████████▉           | 236M/329M [00:30<00:10, 9.21MB/s]
72%|████████████████████████████           | 236M/329M [00:30<00:10, 8.56MB/s]
72%|████████████████████████████           | 237M/329M [00:30<00:11, 8.26MB/s]
72%|████████████████████████████▏          | 238M/329M [00:30<00:11, 8.00MB/s]
73%|████████████████████████████▎          | 239M/329M [00:30<00:10, 8.43MB/s]
73%|████████████████████████████▍          | 240M/329M [00:30<00:11, 8.08MB/s]
73%|████████████████████████████▌          | 241M/329M [00:30<00:10, 8.45MB/s]
74%|████████████████████████████▋          | 242M/329M [00:30<00:09, 8.94MB/s]
74%|████████████████████████████▊          | 243M/329M [00:31<00:09, 8.93MB/s]
74%|████████████████████████████▉          | 244M/329M [00:31<00:09, 9.20MB/s]
74%|█████████████████████████████          | 245M/329M [00:31<00:09, 9.02MB/s]
75%|█████████████████████████████▏         | 246M/329M [00:31<00:09, 8.95MB/s]
75%|█████████████████████████████▎         | 247M/329M [00:31<00:09, 8.51MB/s]
75%|█████████████████████████████▎         | 248M/329M [00:31<00:09, 8.20MB/s]
76%|█████████████████████████████▍         | 249M/329M [00:31<00:09, 8.36MB/s]
76%|█████████████████████████████▌         | 250M/329M [00:31<00:09, 8.52MB/s]
76%|█████████████████████████████▋         | 251M/329M [00:31<00:08, 8.80MB/s]
77%|█████████████████████████████▊         | 252M/329M [00:32<00:08, 8.87MB/s]
77%|█████████████████████████████▉         | 253M/329M [00:32<00:08, 9.04MB/s]
77%|██████████████████████████████         | 254M/329M [00:32<00:08, 8.74MB/s]
77%|██████████████████████████████▏        | 255M/329M [00:32<00:09, 8.16MB/s]
78%|██████████████████████████████▎        | 256M/329M [00:32<00:09, 7.96MB/s]
78%|██████████████████████████████▍        | 256M/329M [00:32<00:11, 6.51MB/s]
78%|██████████████████████████████▍        | 257M/329M [00:32<00:11, 6.33MB/s]
78%|██████████████████████████████▌        | 258M/329M [00:33<00:12, 5.64MB/s]
79%|██████████████████████████████▋        | 259M/329M [00:33<00:11, 5.89MB/s]
79%|██████████████████████████████▊        | 260M/329M [00:33<00:10, 6.54MB/s]
79%|██████████████████████████████▉        | 261M/329M [00:33<00:10, 6.83MB/s]
79%|██████████████████████████████▉        | 261M/329M [00:33<00:09, 6.97MB/s]
80%|███████████████████████████████        | 262M/329M [00:33<00:10, 6.33MB/s]
80%|███████████████████████████████▏       | 263M/329M [00:33<00:11, 5.86MB/s]
80%|███████████████████████████████▏       | 263M/329M [00:33<00:11, 5.88MB/s]
80%|███████████████████████████████▎       | 264M/329M [00:33<00:11, 5.89MB/s]
80%|███████████████████████████████▎       | 265M/329M [00:34<00:10, 5.91MB/s]
81%|███████████████████████████████▍       | 265M/329M [00:34<00:10, 6.10MB/s]
81%|███████████████████████████████▌       | 266M/329M [00:34<00:09, 6.58MB/s]
81%|███████████████████████████████▌       | 267M/329M [00:34<00:09, 6.89MB/s]
81%|███████████████████████████████▋       | 268M/329M [00:34<00:08, 6.95MB/s]
82%|███████████████████████████████▊       | 269M/329M [00:34<00:08, 7.46MB/s]
82%|███████████████████████████████▉       | 270M/329M [00:34<00:07, 7.66MB/s]
82%|████████████████████████████████       | 271M/329M [00:34<00:07, 7.70MB/s]
83%|████████████████████████████████▏      | 272M/329M [00:34<00:07, 8.07MB/s]
83%|████████████████████████████████▎      | 273M/329M [00:35<00:06, 9.37MB/s]
83%|████████████████████████████████▍      | 274M/329M [00:35<00:05, 9.34MB/s]
83%|████████████████████████████████▌      | 275M/329M [00:35<00:05, 9.34MB/s]
84%|████████████████████████████████▋      | 276M/329M [00:35<00:05, 9.82MB/s]
84%|████████████████████████████████▊      | 277M/329M [00:35<00:04, 10.6MB/s]
85%|████████████████████████████████▉      | 278M/329M [00:35<00:05, 9.75MB/s]
85%|█████████████████████████████████      | 279M/329M [00:35<00:05, 9.25MB/s]
85%|█████████████████████████████████▏     | 280M/329M [00:35<00:05, 8.47MB/s]
85%|█████████████████████████████████▎     | 281M/329M [00:35<00:05, 8.67MB/s]
86%|█████████████████████████████████▍     | 282M/329M [00:36<00:05, 8.69MB/s]
86%|█████████████████████████████████▌     | 283M/329M [00:36<00:05, 8.77MB/s]
86%|█████████████████████████████████▋     | 284M/329M [00:36<00:04, 9.12MB/s]
87%|█████████████████████████████████▊     | 285M/329M [00:36<00:04, 8.93MB/s]
87%|█████████████████████████████████▉     | 286M/329M [00:36<00:05, 8.48MB/s]
87%|██████████████████████████████████     | 287M/329M [00:36<00:05, 7.39MB/s]
87%|██████████████████████████████████     | 288M/329M [00:36<00:05, 7.19MB/s]
88%|██████████████████████████████████▏    | 289M/329M [00:36<00:05, 7.03MB/s]
88%|██████████████████████████████████▎    | 290M/329M [00:37<00:05, 7.86MB/s]
88%|██████████████████████████████████▍    | 290M/329M [00:37<00:05, 7.72MB/s]
89%|██████████████████████████████████▌    | 292M/329M [00:37<00:04, 8.00MB/s]
89%|██████████████████████████████████▋    | 293M/329M [00:37<00:04, 8.27MB/s]
89%|██████████████████████████████████▊    | 293M/329M [00:37<00:04, 7.60MB/s]
89%|██████████████████████████████████▊    | 294M/329M [00:37<00:04, 8.11MB/s]
90%|██████████████████████████████████▉    | 295M/329M [00:37<00:03, 8.61MB/s]
90%|███████████████████████████████████    | 296M/329M [00:37<00:03, 8.68MB/s]
90%|███████████████████████████████████▏   | 297M/329M [00:37<00:04, 7.64MB/s]
91%|███████████████████████████████████▎   | 298M/329M [00:38<00:04, 6.87MB/s]
91%|███████████████████████████████████▍   | 299M/329M [00:38<00:04, 7.28MB/s]
91%|███████████████████████████████████▌   | 300M/329M [00:38<00:04, 7.24MB/s]
91%|███████████████████████████████████▌   | 301M/329M [00:38<00:04, 6.97MB/s]
92%|███████████████████████████████████▋   | 302M/329M [00:38<00:03, 7.71MB/s]
92%|███████████████████████████████████▊   | 302M/329M [00:38<00:03, 7.66MB/s]
92%|███████████████████████████████████▉   | 303M/329M [00:38<00:03, 8.02MB/s]
92%|████████████████████████████████████   | 304M/329M [00:38<00:03, 8.03MB/s]
93%|████████████████████████████████████▏  | 305M/329M [00:38<00:03, 7.93MB/s]
93%|████████████████████████████████████▏  | 306M/329M [00:39<00:02, 8.06MB/s]
93%|████████████████████████████████████▎  | 307M/329M [00:39<00:02, 8.66MB/s]
93%|████████████████████████████████████▍  | 308M/329M [00:39<00:02, 8.19MB/s]
94%|████████████████████████████████████▌  | 309M/329M [00:39<00:02, 8.59MB/s]
94%|████████████████████████████████████▋  | 310M/329M [00:39<00:02, 8.61MB/s]
94%|████████████████████████████████████▊  | 311M/329M [00:39<00:02, 8.72MB/s]
95%|████████████████████████████████████▉  | 312M/329M [00:39<00:02, 8.76MB/s]
95%|█████████████████████████████████████  | 313M/329M [00:39<00:01, 8.89MB/s]
95%|█████████████████████████████████████▏ | 314M/329M [00:39<00:01, 9.50MB/s]
96%|█████████████████████████████████████▎ | 315M/329M [00:40<00:01, 9.65MB/s]
96%|█████████████████████████████████████▍ | 316M/329M [00:40<00:01, 9.16MB/s]
96%|█████████████████████████████████████▌ | 317M/329M [00:40<00:01, 9.13MB/s]
97%|█████████████████████████████████████▋ | 318M/329M [00:40<00:01, 9.82MB/s]
97%|█████████████████████████████████████▊ | 319M/329M [00:40<00:01, 9.53MB/s]
97%|█████████████████████████████████████▉ | 320M/329M [00:40<00:00, 9.46MB/s]
98%|██████████████████████████████████████ | 321M/329M [00:40<00:00, 9.73MB/s]
98%|██████████████████████████████████████▏| 322M/329M [00:40<00:00, 9.42MB/s]
98%|██████████████████████████████████████▎| 323M/329M [00:41<00:00, 8.57MB/s]
98%|██████████████████████████████████████▍| 324M/329M [00:41<00:00, 8.26MB/s]
99%|██████████████████████████████████████▌| 325M/329M [00:41<00:00, 8.62MB/s]
99%|██████████████████████████████████████▌| 326M/329M [00:41<00:00, 7.94MB/s]
99%|██████████████████████████████████████▋| 327M/329M [00:41<00:00, 8.47MB/s]
100%|██████████████████████████████████████▊| 328M/329M [00:41<00:00, 8.10MB/s]
100%|██████████████████████████████████████▉| 329M/329M [00:41<00:00, 8.21MB/s]
0%|                                               | 0.00/329M [00:00<?, ?B/s]
100%|████████████████████████████████████████| 329M/329M [00:00<00:00, 753GB/s]
ds directory : /home/circleci/mne_data/MNE-fieldtrip_cmc-data/SubjectCMC.ds
Separate EEG position data file not present.
Quaternion matching (desired vs. transformed):
0.33   78.32    0.00 mm <->    0.33   78.32    0.00 mm (orig :  -71.62   40.46 -256.48 mm) diff =    0.000 mm
-0.33  -78.32   -0.00 mm <->   -0.33  -78.32   -0.00 mm (orig :   39.27  -70.16 -258.60 mm) diff =    0.000 mm
114.65    0.00   -0.00 mm <->  114.65    0.00   -0.00 mm (orig :   64.35   66.64 -262.01 mm) diff =    0.000 mm
Coordinate transformations established.
Polhemus data for 3 HPI coils added
Device coordinate locations for 3 HPI coils added
Picked positions of 4 EEG channels from channel info
4 EEG locations added to Polhemus data.
Measurement info composed.
Finding samples for /home/circleci/mne_data/MNE-fieldtrip_cmc-data/SubjectCMC.ds/SubjectCMC.meg4:
System clock channel is available, checking which samples are valid.
75 x 12000 = 911610 samples from 191 chs
390 samples omitted at the end
Removing 5 compensators from info because not all compensation channels were picked.
Reading 0 ... 72000  =      0.000 ...    60.000 secs...
Filtering raw data in 1 contiguous segment
Setting up band-stop filter from 49 - 51 Hz

FIR filter parameters
---------------------
Designing a one-pass, zero-phase, non-causal bandstop filter:
- Windowed time-domain design (firwin) method
- Hamming window with 0.0194 passband ripple and 53 dB stopband attenuation
- Lower passband edge: 49.38
- Lower transition bandwidth: 0.50 Hz (-6 dB cutoff frequency: 49.12 Hz)
- Upper passband edge: 50.62 Hz
- Upper transition bandwidth: 0.50 Hz (-6 dB cutoff frequency: 50.88 Hz)
- Filter length: 7921 samples (6.601 s)

[Parallel(n_jobs=1)]: Done  17 tasks      | elapsed:    0.1s
[Parallel(n_jobs=1)]: Done  71 tasks      | elapsed:    0.3s
30 matching events found
No baseline correction applied
0 projection items activated
Using data from preloaded Raw for 30 events and 200 original time points ...


We will focus on connectivity between sensors over the parietal and occipital cortices, with 20 parietal sensors designated as group A, and 22 occipital sensors designated as group B.

# parietal sensors
signals_a = [
idx
for idx, ch_info in enumerate(epochs.info["chs"])
if ch_info["ch_name"][2] == "P"
]
# occipital sensors
signals_b = [
idx
for idx, ch_info in enumerate(epochs.info["chs"])
if ch_info["ch_name"][2] == "O"
]

indices_ab = (np.array([signals_a]), np.array([signals_b]))  # A => B
indices_ba = (np.array([signals_b]), np.array([signals_a]))  # B => A

# compute Granger causality
gc_ab = spectral_connectivity_epochs(
epochs,
method=["gc"],
indices=indices_ab,
fmin=5,
fmax=30,
rank=(np.array([5]), np.array([5])),
gc_n_lags=20,
)  # A => B
gc_ba = spectral_connectivity_epochs(
epochs,
method=["gc"],
indices=indices_ba,
fmin=5,
fmax=30,
rank=(np.array([5]), np.array([5])),
gc_n_lags=20,
)  # B => A
freqs = gc_ab.freqs

Adding metadata with 3 columns
Connectivity computation...
computing connectivity for 1 connections
using t=0.000s..1.990s for estimation (200 points)
frequencies: 5.0Hz..30.0Hz (51 points)
Using multitaper spectrum estimation with 7 DPSS windows
the following metrics will be computed: GC
computing cross-spectral density for epoch 1
computing cross-spectral density for epoch 2
computing cross-spectral density for epoch 3
computing cross-spectral density for epoch 4
computing cross-spectral density for epoch 5
computing cross-spectral density for epoch 6
computing cross-spectral density for epoch 7
computing cross-spectral density for epoch 8
computing cross-spectral density for epoch 9
computing cross-spectral density for epoch 10
computing cross-spectral density for epoch 11
computing cross-spectral density for epoch 12
computing cross-spectral density for epoch 13
computing cross-spectral density for epoch 14
computing cross-spectral density for epoch 15
computing cross-spectral density for epoch 16
computing cross-spectral density for epoch 17
computing cross-spectral density for epoch 18
computing cross-spectral density for epoch 19
computing cross-spectral density for epoch 20
computing cross-spectral density for epoch 21
computing cross-spectral density for epoch 22
computing cross-spectral density for epoch 23
computing cross-spectral density for epoch 24
computing cross-spectral density for epoch 25
computing cross-spectral density for epoch 26
computing cross-spectral density for epoch 27
computing cross-spectral density for epoch 28
computing cross-spectral density for epoch 29
computing cross-spectral density for epoch 30
Computing GC for connection 1 of 1

0%|          | frequency blocks : 0/51 [00:00<?,       ?it/s]
22%|██▏       | frequency blocks : 11/51 [00:00<00:00,  676.14it/s]
43%|████▎     | frequency blocks : 22/51 [00:00<00:00,  671.07it/s]
61%|██████    | frequency blocks : 31/51 [00:00<00:00,  550.99it/s]
73%|███████▎  | frequency blocks : 37/51 [00:00<00:00,  497.22it/s]
90%|█████████ | frequency blocks : 46/51 [00:00<00:00,  503.89it/s]
100%|██████████| frequency blocks : 51/51 [00:00<00:00,  516.56it/s]
[Connectivity computation done]
Replacing existing metadata with 3 columns
Connectivity computation...
computing connectivity for 1 connections
using t=0.000s..1.990s for estimation (200 points)
frequencies: 5.0Hz..30.0Hz (51 points)
Using multitaper spectrum estimation with 7 DPSS windows
the following metrics will be computed: GC
computing cross-spectral density for epoch 1
computing cross-spectral density for epoch 2
computing cross-spectral density for epoch 3
computing cross-spectral density for epoch 4
computing cross-spectral density for epoch 5
computing cross-spectral density for epoch 6
computing cross-spectral density for epoch 7
computing cross-spectral density for epoch 8
computing cross-spectral density for epoch 9
computing cross-spectral density for epoch 10
computing cross-spectral density for epoch 11
computing cross-spectral density for epoch 12
computing cross-spectral density for epoch 13
computing cross-spectral density for epoch 14
computing cross-spectral density for epoch 15
computing cross-spectral density for epoch 16
computing cross-spectral density for epoch 17
computing cross-spectral density for epoch 18
computing cross-spectral density for epoch 19
computing cross-spectral density for epoch 20
computing cross-spectral density for epoch 21
computing cross-spectral density for epoch 22
computing cross-spectral density for epoch 23
computing cross-spectral density for epoch 24
computing cross-spectral density for epoch 25
computing cross-spectral density for epoch 26
computing cross-spectral density for epoch 27
computing cross-spectral density for epoch 28
computing cross-spectral density for epoch 29
computing cross-spectral density for epoch 30
Computing GC for connection 1 of 1

0%|          | frequency blocks : 0/51 [00:00<?,       ?it/s]
20%|█▉        | frequency blocks : 10/51 [00:00<00:00,  584.59it/s]
41%|████      | frequency blocks : 21/51 [00:00<00:00,  493.68it/s]
61%|██████    | frequency blocks : 31/51 [00:00<00:00,  491.34it/s]
86%|████████▋ | frequency blocks : 44/51 [00:00<00:00,  558.07it/s]
100%|██████████| frequency blocks : 51/51 [00:00<00:00,  557.89it/s]
[Connectivity computation done]


Plotting the results, we see that there is a flow of information from our parietal sensors (group A) to our occipital sensors (group B) with a noticeable peak at ~8 Hz, and smaller peaks at 18 and 26 Hz.

fig, axis = plt.subplots(1, 1)
axis.plot(freqs, gc_ab.get_data()[0], linewidth=2)
axis.set_xlabel("Frequency (Hz)")
axis.set_ylabel("Connectivity (A.U.)")
fig.suptitle("GC: [A => B]")

Text(0.5, 0.98, 'GC: [A => B]')


## Drivers and receivers: analysing the net direction of information flow#

Although analysing connectivity in a given direction can be of interest, there may exist a bidirectional relationship between signals. In such cases, identifying the signals that dominate information flow (the drivers) may be desired. For this, we can simply subtract the Granger scores in the opposite direction, giving us the net GC score:

$$F_{A \rightarrow B}^{net} := F_{A \rightarrow B} - F_{B \rightarrow A}$$.

Doing so, we see that the flow of information across the spectrum remains dominant from parietal to occipital sensors (indicated by the positive-valued Granger scores), with similar peaks around 10, 18, and 26 Hz.

net_gc = gc_ab.get_data() - gc_ba.get_data()  # [A => B] - [B => A]

fig, axis = plt.subplots(1, 1)
axis.plot((freqs[0], freqs[-1]), (0, 0), linewidth=2, linestyle="--", color="k")
axis.plot(freqs, net_gc[0], linewidth=2)
axis.set_xlabel("Frequency (Hz)")
axis.set_ylabel("Connectivity (A.U.)")
fig.suptitle("Net GC: [A => B] - [B => A]")

Text(0.5, 0.98, 'Net GC: [A => B] - [B => A]')


## Improving the robustness of connectivity estimates with time-reversal#

One limitation of GC methods is the risk of connectivity estimates being contaminated with noise. For instance, consider the case where, due to volume conduction, multiple sensors detect activity from the same source. Naturally, information recorded at these sensors mutually help to predict the activity of one another, leading to spurious estimates of directed connectivity which one may incorrectly attribute to information flow between different brain regions. On the other hand, even if there is no source mixing, the presence of correlated noise between sensors can similarly bias directed connectivity estimates.

To address this issue, Haufe et al. (2013) [4] propose contrasting causality scores obtained on the original time-series to those obtained on the reversed time-series. The idea behind this approach is as follows: if temporal order is crucial in distinguishing a driver from a recipient, then reversing the temporal order should reduce, if not flip, an estimate of directed connectivity. In practice, time-reversal is implemented as a transposition of the autocovariance sequence used to compute GC [5].

Several studies have shown that that such an approach can reduce the degree of false-positive connectivity estimates (even performing favourably against other methods such as the phase slope index) [6] and retain the ability to correctly identify the net direction of information flow akin to net GC [2][4]. This approach is termed time-reversed GC (TRGC):

$$\tilde{D}_{A \rightarrow B}^{net} := F_{A \rightarrow B}^{net} - F_{\tilde{A} \rightarrow \tilde{B}}^{net}$$ ,

where $$\sim$$ represents time-reversal, and:

$$F_{\tilde{A} \rightarrow \tilde{B}}^{net} := F_{\tilde{A} \rightarrow \tilde{B}} - F_{\tilde{B} \rightarrow \tilde{A}}$$.

GC on time-reversed signals can be computed simply with method=['gc_tr'], which will perform the time-reversal of the signals for the end-user. Note that time-reversed results should only be interpreted in the context of net results, i.e. with $$\tilde{D}_{A \rightarrow B}^{net}$$. In the example below, notice how the outputs are not used directly, but rather used to produce net scores of the time-reversed signals. The net scores of the time-reversed signals can then be subtracted from the net scores of the original signals to produce the final TRGC scores.

# compute GC on time-reversed signals
gc_tr_ab = spectral_connectivity_epochs(
epochs,
method=["gc_tr"],
indices=indices_ab,
fmin=5,
fmax=30,
rank=(np.array([5]), np.array([5])),
gc_n_lags=20,
)  # TR[A => B]
gc_tr_ba = spectral_connectivity_epochs(
epochs,
method=["gc_tr"],
indices=indices_ba,
fmin=5,
fmax=30,
rank=(np.array([5]), np.array([5])),
gc_n_lags=20,
)  # TR[B => A]

# compute net GC on time-reversed signals (TR[A => B] - TR[B => A])
net_gc_tr = gc_tr_ab.get_data() - gc_tr_ba.get_data()

# compute TRGC
trgc = net_gc - net_gc_tr

Replacing existing metadata with 3 columns
Connectivity computation...
computing connectivity for 1 connections
using t=0.000s..1.990s for estimation (200 points)
frequencies: 5.0Hz..30.0Hz (51 points)
Using multitaper spectrum estimation with 7 DPSS windows
the following metrics will be computed: GC time-reversed
computing cross-spectral density for epoch 1
computing cross-spectral density for epoch 2
computing cross-spectral density for epoch 3
computing cross-spectral density for epoch 4
computing cross-spectral density for epoch 5
computing cross-spectral density for epoch 6
computing cross-spectral density for epoch 7
computing cross-spectral density for epoch 8
computing cross-spectral density for epoch 9
computing cross-spectral density for epoch 10
computing cross-spectral density for epoch 11
computing cross-spectral density for epoch 12
computing cross-spectral density for epoch 13
computing cross-spectral density for epoch 14
computing cross-spectral density for epoch 15
computing cross-spectral density for epoch 16
computing cross-spectral density for epoch 17
computing cross-spectral density for epoch 18
computing cross-spectral density for epoch 19
computing cross-spectral density for epoch 20
computing cross-spectral density for epoch 21
computing cross-spectral density for epoch 22
computing cross-spectral density for epoch 23
computing cross-spectral density for epoch 24
computing cross-spectral density for epoch 25
computing cross-spectral density for epoch 26
computing cross-spectral density for epoch 27
computing cross-spectral density for epoch 28
computing cross-spectral density for epoch 29
computing cross-spectral density for epoch 30
Computing GC time-reversed for connection 1 of 1

0%|          | frequency blocks : 0/51 [00:00<?,       ?it/s]
29%|██▉       | frequency blocks : 15/51 [00:00<00:00,  903.24it/s]
57%|█████▋    | frequency blocks : 29/51 [00:00<00:00,  881.76it/s]
63%|██████▎   | frequency blocks : 32/51 [00:00<00:00,  634.71it/s]
84%|████████▍ | frequency blocks : 43/51 [00:00<00:00,  641.88it/s]
100%|██████████| frequency blocks : 51/51 [00:00<00:00,  671.70it/s]
[Connectivity computation done]
Replacing existing metadata with 3 columns
Connectivity computation...
computing connectivity for 1 connections
using t=0.000s..1.990s for estimation (200 points)
frequencies: 5.0Hz..30.0Hz (51 points)
Using multitaper spectrum estimation with 7 DPSS windows
the following metrics will be computed: GC time-reversed
computing cross-spectral density for epoch 1
computing cross-spectral density for epoch 2
computing cross-spectral density for epoch 3
computing cross-spectral density for epoch 4
computing cross-spectral density for epoch 5
computing cross-spectral density for epoch 6
computing cross-spectral density for epoch 7
computing cross-spectral density for epoch 8
computing cross-spectral density for epoch 9
computing cross-spectral density for epoch 10
computing cross-spectral density for epoch 11
computing cross-spectral density for epoch 12
computing cross-spectral density for epoch 13
computing cross-spectral density for epoch 14
computing cross-spectral density for epoch 15
computing cross-spectral density for epoch 16
computing cross-spectral density for epoch 17
computing cross-spectral density for epoch 18
computing cross-spectral density for epoch 19
computing cross-spectral density for epoch 20
computing cross-spectral density for epoch 21
computing cross-spectral density for epoch 22
computing cross-spectral density for epoch 23
computing cross-spectral density for epoch 24
computing cross-spectral density for epoch 25
computing cross-spectral density for epoch 26
computing cross-spectral density for epoch 27
computing cross-spectral density for epoch 28
computing cross-spectral density for epoch 29
computing cross-spectral density for epoch 30
Computing GC time-reversed for connection 1 of 1

0%|          | frequency blocks : 0/51 [00:00<?,       ?it/s]
20%|█▉        | frequency blocks : 10/51 [00:00<00:00,  623.65it/s]
39%|███▉      | frequency blocks : 20/51 [00:00<00:00,  615.48it/s]
51%|█████     | frequency blocks : 26/51 [00:00<00:00,  512.10it/s]
71%|███████   | frequency blocks : 36/51 [00:00<00:00,  536.79it/s]
98%|█████████▊| frequency blocks : 50/51 [00:00<00:00,  603.65it/s]
100%|██████████| frequency blocks : 51/51 [00:00<00:00,  599.85it/s]
[Connectivity computation done]


Plotting the TRGC results reveals a very different picture compared to net GC. For one, there is now a dominance of information flow ~6 Hz from occipital to parietal sensors (indicated by the negative-valued Granger scores). Additionally, the peak ~10 Hz is less dominant in the spectrum, with parietal to occipital information flow between 13-20 Hz being much more prominent. The stark difference between net GC and TRGC results indicates that the net GC spectrum was contaminated by spurious connectivity resulting from source mixing or correlated noise in the recordings. Altogether, the use of TRGC instead of net GC is generally advised.

fig, axis = plt.subplots(1, 1)
axis.plot((freqs[0], freqs[-1]), (0, 0), linewidth=2, linestyle="--", color="k")
axis.plot(freqs, trgc[0], linewidth=2)
axis.set_xlabel("Frequency (Hz)")
axis.set_ylabel("Connectivity (A.U.)")
fig.suptitle("TRGC: net[A => B] - net time-reversed[A => B]")

Text(0.5, 0.98, 'TRGC: net[A => B] - net time-reversed[A => B]')


## Controlling spectral smoothing with the number of lags#

One important parameter when computing GC is the number of lags used when computing the VAR model. A lower number of lags reduces the computational cost, but in the context of spectral GC, leads to a smoothing of Granger scores across frequencies. The number of lags can be specified using the gc_n_lags parameter. The default value is 40, however there is no correct number of lags to use when computing GC. Instead, you have to use your own best judgement of whether or not your Granger scores look overly smooth.

Below is a comparison of Granger scores computed with a different number of lags. In the above examples we used 20 lags, which we will compare to Granger scores computed with 60 lags. As you can see, the spectra of Granger scores computed with 60 lags is noticeably less smooth, but it does share the same overall pattern.

gc_ab_60 = spectral_connectivity_epochs(
epochs,
method=["gc"],
indices=indices_ab,
fmin=5,
fmax=30,
rank=(np.array([5]), np.array([5])),
gc_n_lags=60,
)  # A => B

fig, axis = plt.subplots(1, 1)
axis.plot(freqs, gc_ab.get_data()[0], linewidth=2, label="20 lags")
axis.plot(freqs, gc_ab_60.get_data()[0], linewidth=2, label="60 lags")
axis.set_xlabel("Frequency (Hz)")
axis.set_ylabel("Connectivity (A.U.)")
axis.legend()
fig.suptitle("GC: [A => B]")

Replacing existing metadata with 3 columns
Connectivity computation...
computing connectivity for 1 connections
using t=0.000s..1.990s for estimation (200 points)
frequencies: 5.0Hz..30.0Hz (51 points)
Using multitaper spectrum estimation with 7 DPSS windows
the following metrics will be computed: GC
computing cross-spectral density for epoch 1
computing cross-spectral density for epoch 2
computing cross-spectral density for epoch 3
computing cross-spectral density for epoch 4
computing cross-spectral density for epoch 5
computing cross-spectral density for epoch 6
computing cross-spectral density for epoch 7
computing cross-spectral density for epoch 8
computing cross-spectral density for epoch 9
computing cross-spectral density for epoch 10
computing cross-spectral density for epoch 11
computing cross-spectral density for epoch 12
computing cross-spectral density for epoch 13
computing cross-spectral density for epoch 14
computing cross-spectral density for epoch 15
computing cross-spectral density for epoch 16
computing cross-spectral density for epoch 17
computing cross-spectral density for epoch 18
computing cross-spectral density for epoch 19
computing cross-spectral density for epoch 20
computing cross-spectral density for epoch 21
computing cross-spectral density for epoch 22
computing cross-spectral density for epoch 23
computing cross-spectral density for epoch 24
computing cross-spectral density for epoch 25
computing cross-spectral density for epoch 26
computing cross-spectral density for epoch 27
computing cross-spectral density for epoch 28
computing cross-spectral density for epoch 29
computing cross-spectral density for epoch 30
Computing GC for connection 1 of 1

0%|          | frequency blocks : 0/51 [00:00<?,       ?it/s]
2%|▏         | frequency blocks : 1/51 [00:00<00:00,   53.12it/s]
6%|▌         | frequency blocks : 3/51 [00:00<00:00,   68.12it/s]
10%|▉         | frequency blocks : 5/51 [00:00<00:00,   76.57it/s]
14%|█▎        | frequency blocks : 7/51 [00:00<00:00,   80.78it/s]
18%|█▊        | frequency blocks : 9/51 [00:00<00:00,   69.58it/s]
22%|██▏       | frequency blocks : 11/51 [00:00<00:00,   71.45it/s]
25%|██▌       | frequency blocks : 13/51 [00:00<00:00,   74.07it/s]
29%|██▉       | frequency blocks : 15/51 [00:00<00:00,   75.88it/s]
31%|███▏      | frequency blocks : 16/51 [00:00<00:00,   73.57it/s]
35%|███▌      | frequency blocks : 18/51 [00:00<00:00,   73.75it/s]
39%|███▉      | frequency blocks : 20/51 [00:00<00:00,   75.32it/s]
43%|████▎     | frequency blocks : 22/51 [00:00<00:00,   78.11it/s]
47%|████▋     | frequency blocks : 24/51 [00:00<00:00,   80.32it/s]
49%|████▉     | frequency blocks : 25/51 [00:00<00:00,   77.14it/s]
53%|█████▎    | frequency blocks : 27/51 [00:00<00:00,   78.95it/s]
57%|█████▋    | frequency blocks : 29/51 [00:00<00:00,   81.17it/s]
61%|██████    | frequency blocks : 31/51 [00:00<00:00,   83.16it/s]
65%|██████▍   | frequency blocks : 33/51 [00:00<00:00,   85.06it/s]
67%|██████▋   | frequency blocks : 34/51 [00:00<00:00,   80.80it/s]
71%|███████   | frequency blocks : 36/51 [00:00<00:00,   82.64it/s]
75%|███████▍  | frequency blocks : 38/51 [00:00<00:00,   84.36it/s]
78%|███████▊  | frequency blocks : 40/51 [00:00<00:00,   85.96it/s]
82%|████████▏ | frequency blocks : 42/51 [00:00<00:00,   87.57it/s]
84%|████████▍ | frequency blocks : 43/51 [00:00<00:00,   84.08it/s]
88%|████████▊ | frequency blocks : 45/51 [00:00<00:00,   85.22it/s]
92%|█████████▏| frequency blocks : 47/51 [00:00<00:00,   86.59it/s]
96%|█████████▌| frequency blocks : 49/51 [00:00<00:00,   87.89it/s]
100%|██████████| frequency blocks : 51/51 [00:00<00:00,   88.22it/s]
100%|██████████| frequency blocks : 51/51 [00:00<00:00,   84.76it/s]
[Connectivity computation done]

Text(0.5, 0.98, 'GC: [A => B]')


## Handling high-dimensional data#

An important issue to consider when computing multivariate GC is that the data GC is computed on should not be rank deficient (i.e. must have full rank). More specifically, the autocovariance matrix must not be singular or close to singular.

In the case that your data is not full rank and rank is left as None, an automatic rank computation is performed and an appropriate degree of dimensionality reduction will be enforced. The rank of the data is determined by computing the singular values of the data and finding those within a factor of $$1e^{-6}$$ relative to the largest singular value.

Whilst unlikely, there may be scenarios in which this threshold is too lenient. In these cases, you should inspect the singular values of your data to identify an appropriate degree of dimensionality reduction to perform, which you can then specify manually using the rank argument. The code below shows one possible approach for finding an appropriate rank of close-to-singular data with a more conservative threshold.

# gets the singular values of the data
s = np.linalg.svd(raw.get_data(), compute_uv=False)
# finds how many singular values are 'close' to the largest singular value
rank = np.count_nonzero(s >= s[0] * 1e-4)  # 1e-4 is the 'closeness' criteria


Nonethless, even in situations where you specify an appropriate rank, it is not guaranteed that the subsequently-computed autocovariance sequence will retain this non-singularity (this can depend on, e.g. the number of lags). Hence, you may also encounter situations where you have to specify a rank less than that of your data to ensure that the autocovariance sequence is non-singular.

In the above examples, notice how a rank of 5 was given, despite there being 20 channels in the seeds and targets. Attempting to compute GC on the original data would not succeed, given that the resulting autocovariance sequence is singular, as the example below shows.

try:
spectral_connectivity_epochs(
epochs,
method=["gc"],
indices=indices_ab,
fmin=5,
fmax=30,
rank=None,
gc_n_lags=20,
verbose=False,
)  # A => B
print("Success!")
except RuntimeError as error:
print("\nCaught the following error:\n" + repr(error))

Caught the following error:
RuntimeError('the autocovariance matrix is singular; check if your data is rank deficient and specify an appropriate rank argument <= the rank of the seeds and targets')


Rigorous checks are implemented to identify any such instances which would otherwise cause the GC computation to produce erroneous results. You can therefore be confident as an end-user that these cases will be caught.

Finally, when comparing GC scores across recordings, it is highly recommended to estimate connectivity from the same number of channels (or equally from the same degree of rank subspace projection) to avoid biases in connectivity estimates. Bias can be avoided by specifying a consistent rank subspace to project to using the rank argument, standardising your connectivity estimates regardless of changes in e.g. the number of channels across recordings. Note that this does not refer to the number of seeds and targets within a connection being identical, rather to the number of seeds and targets across connections.

## References#

Total running time of the script: (0 minutes 57.924 seconds)

Gallery generated by Sphinx-Gallery