/
calc_ali.py
161 lines (133 loc) · 6.06 KB
/
calc_ali.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# coding=utf-8
"""
This is a group of function to be used on TF data.
@author: mje
@email: mads [] cnru.dk
"""
from my_settings import (epochs_folder, tf_folder)
import numpy as np
import mne
import sys
import matplotlib.pyplot as plt
subject = sys.argv[1]
epochs = mne.read_epochs(
epochs_folder + "%s_trial_start-epo.fif" % subject, preload=False)
selection = mne.read_selection("Left-occipital")
selection = [f.replace(' ', '') for f in selection]
left_idx = mne.pick_types(
epochs.info,
meg="grad",
eeg=False,
eog=False,
stim=False,
exclude=[],
selection=selection)
selection = mne.read_selection("Right-occipital")
selection = [f.replace(' ', '') for f in selection]
right_idx = mne.pick_types(
epochs.info,
meg="grad",
eeg=False,
eog=False,
stim=False,
exclude=[],
selection=selection)
def calc_ALI(subject, show_plot=False):
"""Function calculates the alpha lateralization index (ALI).
The alpha lateralization index (ALI) is based on:
Huurne, N. ter, Onnink, M., Kan, C., Franke, B., Buitelaar, J.,
& Jensen, O. (2013).
Parameters
----------
subject : string
The name of the subject to calculate ALI for.
show_plot : bool
Whether to plot the data or not.
RETURNS
-------
ali_left : the ALI for the left cue
ali_right : the ALI for the right cue
"""
ctl_left = np.load(tf_folder + "%s_ctl_left-4-tfr.npy" % subject)
ctl_right = np.load(tf_folder + "%s_ctl_right-4-tfr.npy" % subject)
ent_left = np.load(tf_folder + "%s_ent_left-4-tfr.npy" % subject)
ent_right = np.load(tf_folder + "%s_ent_right-4-tfr.npy" % subject)
ALI_left_cue_ctl = ((ctl_left[:, left_idx, :, :].mean(axis=0).mean(
axis=0) - ctl_left[:, right_idx, :, :].mean(axis=0).mean(axis=0)) / (
ctl_left[:, left_idx, :, :].mean(axis=0).mean(axis=0) +
ctl_left[:, right_idx, :, :].mean(axis=0).mean(axis=0)))
ALI_right_cue_ctl = ((ctl_right[:, left_idx, :, :].mean(axis=0).mean(
axis=0) - ctl_right[:, right_idx, :, :].mean(axis=0).mean(axis=0)) / (
ctl_right[:, left_idx, :, :].mean(axis=0).mean(axis=0) +
ctl_right[:, right_idx, :, :].mean(axis=0).mean(axis=0)))
ALI_left_cue_ent = ((ent_left[:, left_idx, :, :].mean(axis=0).mean(
axis=0) - ent_left[:, right_idx, :, :].mean(axis=0).mean(axis=0)) / (
ent_left[:, left_idx, :, :].mean(axis=0).mean(axis=0) +
ent_left[:, right_idx, :, :].mean(axis=0).mean(axis=0)))
ALI_right_cue_ent = ((ent_right[:, left_idx, :, :].mean(axis=0).mean(
axis=0) - ent_right[:, right_idx, :, :].mean(axis=0).mean(axis=0)) / (
ent_right[:, left_idx, :, :].mean(axis=0).mean(axis=0) +
ent_right[:, right_idx, :, :].mean(axis=0).mean(axis=0)))
if show_plot:
times = epochs.times
plt.figure()
plt.plot(times, ALI_left_cue_ctl, 'r', label="ALI Left cue control")
plt.plot(times, ALI_left_cue_ent, 'b', label="ALI Left ent control")
plt.plot(times, ALI_right_cue_ctl, 'g', label="ALI Right cue control")
plt.plot(times, ALI_right_cue_ent, 'm', label="ALI Right ent control")
plt.legend()
plt.title("ALI curves for subject: %s" % subject)
plt.show()
return (ALI_left_cue_ctl.mean(axis=0), ALI_right_cue_ctl.mean(axis=0),
ALI_left_cue_ent.mean(axis=0), ALI_right_cue_ent.mean(axis=0))
def calc_ALI_itc(subject, show_plot=False):
"""Function calculates the alpha lateralization index (ALI).
The alpha lateralization index (ALI) is based on:
Huurne, N. ter, Onnink, M., Kan, C., Franke, B., Buitelaar, J.,
& Jensen, O. (2013).
Parameters
----------
subject : string
The name of the subject to calculate ALI for.
show_plot : bool
Whether to plot the data or not.
RETURNS
-------
ali_left : the ALI for the left cue
ali_right : the ALI for the right cue
"""
ctl_left = np.load(tf_folder + "%s_ctl_left-4-itc.npy" % subject)
ctl_right = np.load(tf_folder + "%s_ctl_right-4-itc.npy" % subject)
ent_left = np.load(tf_folder + "%s_ent_left-4-itc.npy" % subject)
ent_right = np.load(tf_folder + "%s_ent_right-4-itc.npy" % subject)
ALI_left_cue_ctl = ((ctl_left[left_idx, :, :].mean(axis=0).mean(
axis=0) - ctl_left[right_idx, :, :].mean(axis=0).mean(axis=0)) / (
ctl_left[left_idx, :, :].mean(axis=0).mean(axis=0) +
ctl_left[right_idx, :, :].mean(axis=0).mean(axis=0)))
ALI_right_cue_ctl = ((ctl_right[left_idx, :, :].mean(axis=0).mean(
axis=0) - ctl_right[right_idx, :, :].mean(axis=0).mean(axis=0)) / (
ctl_right[left_idx, :, :].mean(axis=0).mean(axis=0) +
ctl_right[right_idx, :, :].mean(axis=0).mean(axis=0)))
ALI_left_cue_ent = ((ent_left[left_idx, :, :].mean(axis=0).mean(
axis=0) - ent_left[right_idx, :, :].mean(axis=0).mean(axis=0)) / (
ent_left[left_idx, :, :].mean(axis=0).mean(axis=0) +
ent_left[right_idx, :, :].mean(axis=0).mean(axis=0)))
ALI_right_cue_ent = ((ent_right[left_idx, :, :].mean(axis=0).mean(
axis=0) - ent_right[right_idx, :, :].mean(axis=0).mean(axis=0)) / (
ent_right[left_idx, :, :].mean(axis=0).mean(axis=0) +
ent_right[right_idx, :, :].mean(axis=0).mean(axis=0)))
if show_plot:
times = epochs.times
plt.figure()
plt.plot(times, ALI_left_cue_ctl, 'r', label="ALI Left cue control")
plt.plot(times, ALI_left_cue_ent, 'b', label="ALI Left ent control")
plt.plot(times, ALI_right_cue_ctl, 'g', label="ALI Right cue control")
plt.plot(times, ALI_right_cue_ent, 'm', label="ALI Right ent control")
plt.legend()
plt.title("ALI curves for subject: %s" % subject)
plt.show()
return (ALI_left_cue_ctl, ALI_right_cue_ctl,
ALI_left_cue_ent, ALI_right_cue_ent)
ctl_left_ali, ctl_right_ali, ent_left_ali, ent_right_ali = calc_ALI_itc(subject)
data = np.vstack((ctl_left_ali, ctl_right_ali, ent_left_ali, ent_right_ali))
np.save(tf_folder + "%s_ali-itc_grad.npy" % subject, data)