-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpectralEvents_Struct_Extraction_IndividualChannels.m
executable file
·186 lines (124 loc) · 6.37 KB
/
SpectralEvents_Struct_Extraction_IndividualChannels.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
%% Extract our data from the large struct
addpath(genpath('Functions/spectral_event_functions/'));
addpath(genpath(hera('/Projects/7TBrainMech/scripts/eeg/toolbox/SpectralEvents-master/')))
addpath(hera('/Projects/7TBrainMech/scripts/fieldtrip-20191213'))
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';
region = 'selectiveFrontal';
% In path
inpath = hera('Projects/7TBrainMech/scripts/eeg/Shane/preprocessed_data');
% Outpath
outpath = hera('Projects/7TBrainMech/scripts/eeg/Shane/Results/Power_Analysis/Spectral_events_analysis/');
if task == 'MGS' && epoch == 'Delay'
savefolder = [outpath band '/' epoch seconds '/'];
specEv_struct = load([savefolder band '_specEV_' region '_Delay_' seconds '.mat']);
specEv_struct = specEv_struct.specEv_struct;
elseif task == 'MGS' && epoch == 'Fix'
savefolder = [outpath band '/' epoch ];
specEv_struct = load([savefolder band '_specEV_' region '_Fix.mat']);
specEv_struct = specEv_struct.specEv_struct;
elseif task == 'Resting_State'
savefolder = [outpath band '/' task '/'];
specEv_struct = load([savefolder band '_specEV_' region '_RS.mat']);
specEv_struct = specEv_struct.specEv_struct;
end
largeT = table('Size', [0, 6], 'VariableTypes', {'string', 'double','double', 'double','double','double'});
largeT.Properties.VariableNames = {'Subject','Channel', 'Trial','Band',[band 'Trial_Power'],[band 'Event_Number'],[band 'Event_Duration']};
for j = 1:length(specEV_struct_allChannels)
specEv_struct = specEV_struct_allChannels{1,j};
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
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
%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}), 6], 'VariableTypes', {'string', 'double','double', 'double','double','double'});
T.Properties.VariableNames = {'Subject','Channel', '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)) = SubjectsRan(:,i);
T.Channel = j+zeros(size(Subject_power,1),1);
T.Band(1:length(T.Trial_Power)) = band;
largeT = [largeT; T];
end
end
if task == 'MGS' && epoch == 'Delay'
savefolder = [outpath band '/' epoch seconds '/'];
writetable(largeT, [savefolder band '_allChannels_TrialLevel_Delay' seconds '.csv']);
elseif task == 'MGS' && epoch == 'Fix'
savefolder = [outpath band '/' epoch ];
writetable(largeT, [savefolder band '_allChannels_TrialLevel_Fix.csv']);
elseif task == 'Resting_State'
savefolder = [outpath band '/' task '/'];
writetable(largeT, [savefolder band '_allChannels_TrialLevel_RS.csv']);
end
%% peak frequency and power
largeT = table('Size', [0, 6], 'VariableTypes', {'string', 'double','double', 'double','double','double'});
largeT.Properties.VariableNames = {'Subject','Channel', 'Trial', 'Band',[band 'Trial_Power'],[band 'Event_Number'],[band 'Event_Duration']};
% 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', [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.Band(1:length(T.Peak_Frequency)) = band;
T.Subject(1:length(T.Peak_Frequency)) = Subjects(i);
largeT = [largeT; T];
end
if task == 'MGS' && epoch == 'Delay'
savefolder = [outpath band '/' epoch seconds '/'];
writetable(largeT, [savefolder band '_allChannels_TrialLevel_PeakFreq_Delay' seconds '.csv']);
elseif task == 'MGS' && epoch == 'Fix'
savefolder = [outpath band '/' epoch ];
writetable(largeT, [savefolder band '_allChannels_TrialLevel_PeakFreq_Fix.csv']);
elseif task == 'Resting_State'
savefolder = [outpath band '/' task '/'];
writetable(largeT, [savefolder band '_allChannels_TrialLevel_PeakFreq_RS.csv']);
end