-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpectralEvents_Struct_Extraction.m
executable file
·227 lines (159 loc) · 8.5 KB
/
SpectralEvents_Struct_Extraction.m
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
%% Extract our data from the large struct
addpath(('/Projects/7TBrainMech/scripts/eeg/toolbox/SpectralEvents-master/'))
addpath((genpath('Functions/spectral_event_functions/')));
addpath(hera('/Projects/7TBrainMech/scripts/fieldtrip-20191127'))
ft_defaults
% values to initalize
task = 'Resting_State'; % which task do you want to run. CHANGE ACCORDING TO MGS, REST, SNR, etc. *****
epoch = 'Delay'; % which epoch do you want to run, if REST there is no epoch
band = 'Gamma';
seconds = '3_4';
if task == 'MGS' && epoch == 'Delay'
savefolder = [outpath band '/' epoch seconds '/'];
specEv_struct = load([savefolder band '_specEV_DLPFC_Delay_' seconds '.mat']);
specEv_struct = specEv_struct.specEv_struct;
elseif task == 'MGS' && epoch == 'Fix'
savefolder = [outpath band '/' epoch ];
specEv_struct = load([savefolder band '_specEV_DLPFC_Fix.mat']);
specEv_struct = specEv_struct.specEv_struct;
elseif task == 'Resting_State'
savefolder = [outpath band '/' task '/'];
specEv_struct = load([savefolder band '_specEV_DLPFC_RS.mat']);
specEv_struct = specEv_struct.specEv_struct;
end
EventNumber = cell(1, length(specEv_struct));
EventDuration = cell(1, length(specEv_struct));
Power = cell(1, length(specEv_struct));
EventMaxPower = cell(1, length(specEv_struct));
EventMaxFreq = cell(1, length(specEv_struct));
for i = 1:length(specEv_struct)
subject = specEv_struct(i);
if isempty(subject.TrialSummary)
continue
end
EventNumber{1,i} = subject.TrialSummary.TrialSummary.eventnumber;
EventDuration{1,i} = subject.TrialSummary.TrialSummary.meaneventduration;
Power{1,i} = subject.TrialSummary.TrialSummary.meaneventpower;
EventMaxPower{1,i} = subject.Events.Events.maximapower;
EventMaxFreq{1,i} = subject.Events.Events.maximafreq;
end
if task == 'MGS' && epoch == 'Delay'
savefolder = [outpath band '/' epoch seconds '/'];
save([savefolder band '_EventNumber' seconds '.mat'], 'EventNumber')
save([savefolder band '_EventDuration' seconds '.mat'], 'EventDuration')
save([savefolder band '_Power' seconds '.mat'], 'Power')
save([savefolder band '_EventMaxPower' seconds '.mat'], 'EventMaxPower')
save([savefolder band '_EventMaxFreq' seconds '.mat'], 'EventMaxFreq')
elseif task == 'MGS' && epoch == 'Fix'
savefolder = [outpath band '/' epoch ];
save([savefolder band '_EventNumber' epoch '.mat'], 'EventNumber')
save([savefolder band '_EventDuration' epoch '.mat'], 'EventDuration')
save([savefolder band '_Power' epoch '.mat'], 'Power')
save([savefolder band '_EventMaxPower' epoch '.mat'], 'EventMaxPower')
save([savefolder band '_EventMaxFreq' epoch '.mat'], 'EventMaxFreq')
elseif task == 'Resting_State'
savefolder = [outpath band '/' task '/'];
save([savefolder band '_EventNumberRS.mat'], 'EventNumber')
save([savefolder band '_EventDurationRS.mat'], 'EventDuration')
save([savefolder band '_PowerRS.mat'], 'Power')
save([savefolder band '_EventMaxPowerRS.mat'], 'EventMaxPower')
save([savefolder band '_EventMaxFreqRS.mat'], 'EventMaxFreq')
end
for i = 1:length(specEv_struct)
AvgPower_PerSubject(1,i) = mean(Power{1,i});
AvgEventNumber_PerSubject(1,i) = mean(EventNumber{1,i});
AvgEventDuration_PerSubject(1,i) = mean(EventDuration{1,i});
AvgEventMaxPower_PerSubject(1,i) = mean(EventMaxPower{1,i});
AvgEventMaxFreq_PerSubject(1,i) = mean(EventMaxFreq{1,i});
SDPower_PerSubject(1,i) = std(Power{1,i});
end
if task == 'MGS' && epoch == 'Delay'
savefolder = [outpath band '/' epoch seconds '/'];
save([savefolder band '_AvgEventNumber' seconds '.mat'], 'AvgEventNumber_PerSubject')
save([savefolder band '_AvgEventDuration' seconds '.mat'], 'AvgEventDuration_PerSubject')
save([savefolder band '_AvgPower' seconds '.mat'], 'AvgPower_PerSubject')
save([savefolder band '_AvgEventMaxPower' seconds '.mat'], 'AvgEventMaxPower_PerSubject')
save([savefolder band '_AvgEventMaxFreq' seconds '.mat'], 'AvgEventMaxFreq_PerSubject')
elseif task == 'MGS' && epoch == 'Fix'
savefolder = [outpath band '/' epoch ];
save([savefolder band '_AvgEventNumber' epoch '.mat'], 'AvgEventNumber_PerSubject')
save([savefolder band '_AvgEventDuration' epoch '.mat'], 'AvgEventDuration_PerSubject')
save([savefolder band '_AvgPower' epoch '.mat'], 'AvgPower_PerSubject')
save([savefolder band '_AvgEventMaxPower' epoch '.mat'], 'AvgEventMaxPower_PerSubject')
save([savefolder band '_AvgEventMaxFreq' epoch '.mat'], 'AvgEventMaxFreq_PerSubject')
elseif task == 'Resting_State'
savefolder = [outpath band '/' task '/'];
save([savefolder band '_AvgEventNumberRS.mat'], 'AvgEventNumber_PerSubject')
save([savefolder band '_AvgEventDurationRS.mat'], 'AvgEventDuration_PerSubject')
save([savefolder band '_AvgPowerRS.mat'], 'AvgPower_PerSubject')
save([savefolder band '_AvgEventMaxPowerRS.mat'], 'AvgEventMaxPower_PerSubject')
save([savefolder band '_AvgEventMaxFreqRS.mat'], 'AvgEventMaxFreq_PerSubject')
end
largeT = table('Size', [0, 5], 'VariableTypes', {'string', 'double', 'double','double','double'});
largeT.Properties.VariableNames = {'Subject', 'Trial' ,[band '_Trial_Power'],[band '_Event_Number'], [band '_Event_Duration']};
%Extract info trial by trial
for i = 1:length(specEv_struct) %Subjects
clear Trial_power
clear Trial_eventDuration
clear Trial_eventNumber
T = table('Size', [length(Power{1,i}), 5], 'VariableTypes', {'string', 'double', 'double','double','double'});
T.Properties.VariableNames = {'Subject', 'Trial', 'Band',[band '_Trial_Power'], [band '_Event_Number'], [band '_Event_Duration']};
if isempty(Power{1,i}) || isempty(EventNumber{1,i}) || isempty(EventDuration{1,i})
continue
end
Subject_power = Power{1,i};
Subject_channel_eventNumber = EventNumber{1,i};
Subject_channel_eventDuration= EventDuration{1,i};
if isempty(Power{1,i}) || isempty(EventNumber{1,i}) || isempty(EventDuration{1,i})
continue
end
T.Trial_Power = Subject_power;
T.Event_Number = Subject_channel_eventNumber;
T.Event_Duration = Subject_channel_eventDuration;
T.Trial = [1:length(T.Trial_Power)]';
T.Subject(1:length(T.Trial_Power)) = idvalues(i,:);
T.Band(1:length(T.Trial_Power)) = band;
largeT = [largeT; T];
end
if task == 'MGS' && epoch == 'Delay'
savefolder = [outpath band '/' epoch seconds '/'];
writetable(largeT, [savefolder band '_subs_' seconds '.csv']);
elseif task == 'MGS' && epoch == 'Fix'
savefolder = [outpath band '/' epoch ];
writetable(largeT, [savefolder band '_subs_Fix.csv']);
elseif task == 'Resting_State'
savefolder = [outpath band '/' task '/'];
writetable(largeT, [savefolder band '_subs_RS.csv']);
end
%% peak frequency and power
largeT = table('Size', [0, 4], 'VariableTypes', {'string', 'double', 'double','double'});
largeT.Properties.VariableNames = {'Subject', 'Trial', 'Band', [band 'Peak_Frequency'], [band 'Peak_Power']};
% %Extract info trial by trial
for i = 1:length(specEv_struct) %Subjects
T = table('Size', [length(EventMaxFreq{1,i}), 4], 'VariableTypes', {'string', 'double', 'double','double'});
T.Properties.VariableNames = {'Subject', 'Trial', [band 'Peak_Frequency'], [band 'Peak_Power']};
if isempty(EventMaxFreq{1,i}) || isempty(EventMaxPower{1,i})
continue
end
Subject_peakFrequency = EventMaxFreq{1,i};
Subject_peakPower = EventMaxPower{1,i};
if isempty( EventMaxFreq(1,i)) || isempty(EventMaxPower(1,i))
continue
end
T.Peak_Frequency = Subject_peakFrequency;
T.Peak_Power = Subject_peakPower;
T.Trial = [1:length(T.Peak_Frequency)]';
T.Subject(1:length(T.Peak_Frequency)) = Subjects(i);
T.Band(1:length(T.Peak_Frequency)) = band;
largeT = [largeT; T];
end
if task == 'MGS' && epoch == 'Delay'
savefolder = [outpath band '/' epoch seconds '/'];
writetable(largeT, [savefolder band '_subs_PeakFreq_Power' seconds '.csv']);
elseif task == 'MGS' && epoch == 'Fix'
savefolder = [outpath band '/' epoch ];
writetable(largeT, [savefolder band '_subs_PeakFreq_Power_Fix.csv']);
elseif task == 'Resting_State'
savefolder = [outpath band '/' task '/'];
writetable(largeT, [savefolder band '_subs_PeakFreq_Power_RS.csv']);
end