Compare topographies

This example shows how to compare EEG topographies, based on the method described by McCarthy & Wood 1.

# sphinx_gallery_thumbnail_number = 4
from eelbrain import *

Simulated data

Generate a simulated dataset (as in the Cluster-based permutation t-test example)

dss = []
for subject in range(10):
    # generate data for one subject
    ds = datasets.simulate_erp(seed=subject)
    # average across trials to get condition means
    ds_agg = ds.aggregate('cloze_cat')
    # add the subject name as variable
    ds_agg[:, 'subject'] = f'S{subject:02}'
    dss.append(ds_agg)

ds = combine(dss)
# make subject a random factor (to treat it as random effect for ANOVA)
ds['subject'].random = True
# Re-reference the EEG data (i.e., subtract the mean of the two mastoid channels):
ds['eeg'] -= ds['eeg'].mean(sensor=['M1', 'M2'])
print(ds.head())

Out:

n    cloze     cloze_cat   n_chars   subject
--------------------------------------------
40   0.88051   high        4.125     S00
40   0.17241   low         3.825     S00
40   0.89466   high        4         S01
40   0.13778   low         4.575     S01
40   0.90215   high        4.275     S02
40   0.12206   low         3.9       S02
40   0.88503   high        4         S03
40   0.14273   low         4.175     S03
40   0.90499   high        4.1       S04
40   0.15732   low         3.5       S04
--------------------------------------------
NDVars: eeg

The simulated data in the two conditions:

p = plot.TopoArray('eeg', 'cloze_cat', ds=ds, axh=2, axw=10, t=[0.120, 0.200, 0.280])
high, low

Test between conditions

Test whether the 120 ms topography differs between the two cloze conditions. The dataset already includes one row per cell (i.e., per cloze condition and subject). Consequently, we can just index the topography at the desired time point:

topography = ds['eeg'].sub(time=0.120)
# normalize the data in accordance with McCarth & Wood (1985)
topography = normalize_in_cells(topography, 'sensor', 'cloze_cat', ds)
# "melt" the topography NDVar to turn the sensor dimension into a Factor
ds_topography = table.melt_ndvar(topography, 'sensor', ds=ds)
# Note EEG is a single column, and the last column indicates the sensor
ds_topography.head()
n cloze cloze_cat n_chars subject eeg sensor
40 0.88051 high 4.125 S00 -0.33651 Fp1
40 0.17241 low 3.825 S00 -0.87354 Fp1
40 0.89466 high 4 S01 -0.95766 Fp1
40 0.13778 low 4.575 S01 -1.5892 Fp1
40 0.90215 high 4.275 S02 -1.4114 Fp1
40 0.12206 low 3.9 S02 -1.6793 Fp1
40 0.88503 high 4 S03 -1.1447 Fp1
40 0.14273 low 4.175 S03 -1.7142 Fp1
40 0.90499 high 4.1 S04 -2.0633 Fp1
40 0.15732 low 3.5 S04 -1.6171 Fp1


ANOVA to test whether the effect of cloze_cat differs between sensors:

test.ANOVA('eeg', 'cloze_cat * sensor * subject', ds=ds_topography)

Out:

<ANOVA: eeg ~ cloze_cat + sensor + cloze_cat x sensor + subject + cloze_cat x subject + sensor x subject + cloze_cat x sensor x subject
                            SS     df      MS   MS(denom)   df(denom)           F        p
  ----------------------------------------------------------------------------------------
  cloze_cat               0.00      1    0.00        6.00           9
  sensor               1291.70     64   20.18        0.13         576   155.80***   < .001
  cloze_cat x sensor      8.30     64    0.13        0.12         576     1.09        .307
  ----------------------------------------------------------------------------------------
  Total                1575.44   1299
>

The non-significant interaction suggests that the effect of cloze_cat does not differ between sensors, i.e., the topographies do not differ, which is consistent with being generated by the same underlying neural sources.

Test two time points

Since we’re not interested in condition here, we first average across conditions, i.e., with the goal of having one row per subject:

ds_average = ds.aggregate('subject', drop_bad=True)
print(ds_average)

Out:

n    cloze     n_chars   subject
--------------------------------
40   0.52646   3.975     S00
40   0.51622   4.2875    S01
40   0.51211   4.0875    S02
40   0.51388   4.0875    S03
40   0.53115   3.8       S04
40   0.52163   4.0375    S05
40   0.53789   4.2       S06
40   0.52491   4.1125    S07
40   0.52464   4.15      S08
40   0.52559   3.8875    S09
--------------------------------
NDVars: eeg

In order to compare two time points, we need to construct a new dataset with time point as Factor:

dss = []
for time in [0.120, 0.280]:
    ds_time = ds_average['subject',]  # A new dataset with the 'subject' variable only
    ds_time['eeg'] = ds_average['eeg'].sub(time=time)
    ds_time[:, 'time'] = f'{time*1000:.0f} ms'
    dss.append(ds_time)
ds_times = combine(dss)
ds_times.summary()
Key Type Values
subject Factor S00:2, S01:2, S02:2, S03:2, S04:2, S05:2, S06:2, S07:2, S08:2, S09:2 (random)
eeg NDVar 65 sensor; -3.86687e-06 - 4.15211e-06
time Factor 120 ms:10, 280 ms:10
None: 20 cases


Then, normalize the data in accordance with McCarth & Wood (1985)

topography = normalize_in_cells('eeg', 'sensor', 'time', ds=ds_times)
# "melt" the topography NDVar to turn the sensor dimension into a Factor
ds_topography = table.melt_ndvar(topography, 'sensor', ds=ds_times)
# Note EEG is a single column, and the last column indicates the sensor
ds_topography.head()
subject time eeg sensor
S00 120 ms -0.60024 Fp1
S01 120 ms -1.2696 Fp1
S02 120 ms -1.547 Fp1
S03 120 ms -1.4269 Fp1
S04 120 ms -1.8517 Fp1
S05 120 ms -1.43 Fp1
S06 120 ms -1.2525 Fp1
S07 120 ms -1.3213 Fp1
S08 120 ms -1.4612 Fp1
S09 120 ms -0.99601 Fp1


Plot the topographies before and after normalization:

p = plot.Topomap('eeg', 'time', ds=ds_times, ncol=2, title="Original")
p = plot.Topomap(topography, 'time', ds=ds_times, ncol=2, title="Normalized")
  • Original, 120 ms, 280 ms
  • Normalized, 120 ms, 280 ms

Compre the topographies with the ANOVA – test whether the effect of time differs between sensors:

test.ANOVA('eeg', 'time * sensor * subject', ds=ds_topography)

Out:

<ANOVA: eeg ~ time + sensor + time x sensor + subject + time x subject + sensor x subject + time x sensor x subject
                       SS     df      MS   MS(denom)   df(denom)          F        p
  ----------------------------------------------------------------------------------
  time               0.00      1    0.00       21.57           9
  sensor          1264.90     64   19.76        0.31         576   63.24***   < .001
  time x sensor     35.10     64    0.55        0.31         576    1.78***   < .001
  ----------------------------------------------------------------------------------
  Total           2064.85   1299
>

Visualize the difference

res = testnd.TTestRelated(topography, 'time', match='subject', ds=ds_times)
p = plot.Topomap(res, ncol=3, title="Normalized topography differences")
Normalized topography differences, 120 ms, 280 ms, (120 ms) - (280 ms)

References

1

McCarthy, G., & Wood, C. C. (1985). Scalp Distributions of Event-Related Potentials—An Ambiguity Associated with Analysis of Variance Models. Electroencephalography and Clinical Neurophysiology, 61, S226–S227. 10.1016/0013-4694(85)90858-2

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

Gallery generated by Sphinx-Gallery