-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathorganize_normalizeToTrialOne.m
More file actions
302 lines (223 loc) · 17.1 KB
/
organize_normalizeToTrialOne.m
File metadata and controls
302 lines (223 loc) · 17.1 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
% normalizes the power to the first trial
function norm = organize_normalizeToTrialOne(dataOrganized, handles)
% PT (12 March 2013), some repetition on this, put inside functions
% for faster debugging / developing
if nargin == 0
handles = init_defaultSettings();
load(fullfile(handles.path.debugMatFiles, 'tempToNormalize.mat'))
else
if handles.saveTempDebugMATs == 1
save(fullfile(handles.path.debugMatFiles, 'tempToNormalize.mat'))
end
end
% get data fields stored to the bins
% e.g. (fieldsIn{fieldsInd})trum, (fieldsIn{fieldsInd})tralDensity, "dummy"
try
fieldsIn = fieldnames(dataOrganized{1}.subject{1}.bins{1}.trial{1}.period{1}.ch);
catch err
err
dataOrganized{1}.subject{1}.bins{1}.trial{1}.period{1}
dataOrganized{1}.subject{1}.bins{1}.trial{1}.period{1}.ch
dataOrganized{1}.subject{1}.bins{1}.trial{1}.period{1}.ch.powerSpectrum
end
%% INSPECT the INPUT DATA
% dataOrganized
% dataOrganized
% dataOrganized.subject{1}
% dataOrganized.subject{1}.bins{1}
% dataOrganized.subject{1}.bins{1}.trial{1}
% dataOrganized.subject{1}.bins{1}.trial{1}.period{1}
% dataOrganized.subject{1}.bins{1}.trial{1}.period{1}.ch
if handles.showDebugMessages == 1
disp('NORMALIZE: (1st subject, 1st condition, bin 1, trial 1, period 1')
dataOrganized{1}.subject{1}.bins{1}.trial{1}.period{1}.ch.(fieldsIn{fieldsInd})
end
% now here you have for example 148 data points corresponding to the
% "epochs" or time windows, so power spectrum as a function of time for
% the given frequency range
% A = dataOrganized{1}.subject{1}.bins{1}.trial{1}.period{1}.ch.(fieldsIn{1}).aver
% assign the input to the output
% now the only change to the input in this functions happens to the
% mean power spectra (.aver) and the standard deviation (.SD)
norm = dataOrganized;
%% NORMALIZE THE DATA
% In the previous implementation (data was normalized to the first
% trial, RUN_BASIC_EEG.m)
% So go again through the data, note now referring to the first {1}
% cell element of each structure, this should be correct if the
% previous code works as planned. So if the code starts crashing here,
% it is a good way to debug the previous code as well. As the data
% lengths should be the same
for i = 1 : length(dataOrganized) % no of conditions, i.e. 3 with WHITE / RED / DARK
norm{i} = organize_normParforLoop(i, norm{i}, fieldsIn, handles);
end
% actually the parfor jams the MATLAB, so just your ordinary
% FOR here
%% SUBFUNCTION FOR PAR LOOP
function norm = organize_normParforLoop(i, norm, fieldsIn, handles)
if i == 1; disp(' '); end
disp([' normalizing data for condition = ', handles.colorConditionCell{i}])
for fieldsInd = 1 : length(fieldsIn)
for ij = 1 : length(norm.subject) % number of different subjects tested
for j = 1 : length(norm.subject{1}.bins) % so as many frequency bins there were
for k = 1 : length(norm.subject{1}.bins{1}.trial) % number of trials
for l = 1 : length(norm.subject{1}.bins{1}.trial{1}.period) % number of different periods
%% check if there is some channels saved at all
% (when some period is not for example done for
% the subject, the .ch could be empty)
try
containsChannels = ~isempty(norm.subject{ij}.bins{j}.trial{k}.period{l});
catch err
err
[i fieldsInd ij j k l] % debug indices
end
if containsChannels == 1
nrOfChannelsIn = length(norm.subject{ij}.bins{j}.trial{k}.period{l}.ch);
else
warning(['No channels for subject index=', num2str(ij), ', trial = ', num2str(k), ', period = ', num2str(l)])
nrOfChannelsIn = NaN;
end
%% GET THE NORMALIZATION VALUE
%% for first trial (k == 1)
if k == 1
% for the "defined" bands such as alpha,
% beta, etc. we know from which channels
% average the response from, thus the size
% of the .ch is 1, whereas for "generic
% bins" (see initDefaultSettings.m) there
% are more channels (for example Oz, Pz,
% etc.), thus we need a condition to take
% care of that
if nrOfChannelsIn == 1 % the "defined bands"
% (fieldsIn{fieldsInd}) here already is a mean
% of Pz and Oz locations, depends on
% the definitions on
% init_defaultSettings.m
% mean
averVector = norm.subject{ij}.bins{j}.trial{k}.period{l}.ch.(fieldsIn{fieldsInd}).aver;
normValueAver = nanmean(averVector);
% median
medianVector = norm.subject{ij}.bins{j}.trial{k}.period{l}.ch.(fieldsIn{fieldsInd}).medianValue;
normValueMedian = nanmean(medianVector);
% assign to structure
normFactorAver = normValueAver;
normFactorMedian = normValueMedian;
% disp(l)
% a1 = norm.subject{ij}.bins{j}.trial{k}.period{l}.ch.(fieldsIn{fieldsInd})
% get the mean of the vector
% containing the data points (different
% time points), of the first trial
% so that mean of all the data points of first trial
% should be 1 (the previous code by
% R. Hamner)
elseif nrOfChannelsIn > 1 % many channels
% nrOfDataPoints = length(norm.subject{ij}.bins{j}.trial{k}.period{l}.ch{1}.(fieldsIn{fieldsInd}).aver);
% go through the channels
for ki = 1 : nrOfChannelsIn
% mean
averVector = norm.subject{ij}.bins{j}.trial{k}.period{l}.ch{ki}.(fieldsIn{fieldsInd}).aver;
normValueAver = nanmean(averVector);
% median
medianVector = norm.subject{ij}.bins{j}.trial{k}.period{l}.ch{ki}.(fieldsIn{fieldsInd}).medianValue;
normValueMedian = nanmean(medianVector);
% assign to structure
normFactorAverCh{ki} = normValueAver;
normFactorMedianCh{ki} = normValueMedian;
end
else
norm.subject{ij}.bins{j}.trial{k}.period{l}.normFactor = NaN;
warning('no norm factor')
end
% DEBUG
if handles.showDebugMessages == 1 && l == 1 && j == 1 && k == 3 && nrOfChannelsIn == 1
% for first period (l), and for first band (j)
disp(['Trial = ', num2str(k-2), ', NormAver: ', num2str(normValueAver), ...
', Mean = ', num2str(nanmean(norm.subject{ij}.bins{j}.trial{k-2}.period{l}.ch.(fieldsIn{fieldsInd}).aver))])
disp(['Trial = ', num2str(k-2), ', NormAver: ', num2str(normValueAver), ...
', Mean = ', num2str(nanmean(norm.subject{ij}.bins{j}.trial{k-1}.period{l}.ch.(fieldsIn{fieldsInd}).aver))])
disp(['Trial = ', num2str(k-2), ', NormAver: ', num2str(normValueAver), ...
', Mean = ', num2str(nanmean(norm.subject{ij}.bins{j}.trial{k}.period{l}.ch.(fieldsIn{fieldsInd}).aver))])
end
%% for trial 2 and 3, etc.
elseif k > 1
% save the normFactor for all the
% different bins using the value
% computed for "k == 1"
indexForNormValue = 1; % first trial
if nrOfChannelsIn == 1 % the "defined bands"
% a3 = norm.subject{ij}.bins{j}.trial{indexForNormValue}.period{l}.ch.(fieldsIn{fieldsInd})
%normValueAver = norm.subject{ij}.bins{j}.trial{indexForNormValue}.period{l}.ch.(fieldsIn{fieldsInd}).normFactorAver;
%normValueMedian = norm.subject{ij}.bins{j}.trial{indexForNormValue}.period{l}.ch.(fieldsIn{fieldsInd}).normFactorMedian;
%norm.subject{ij}.bins{j}.trial{k}.period{l}.ch.(fieldsIn{fieldsInd}).normFactorAver = normValueAver;
%norm.subject{ij}.bins{j}.trial{k}.period{l}.ch.(fieldsIn{fieldsInd}).normFactorMedian = normValueMedian;
elseif nrOfChannelsIn > 1 % many channels (like
% go through the channels
for ki = 1 : nrOfChannelsIn
%normValueAver = norm.subject{ij}.bins{j}.trial{indexForNormValue}.period{l}.ch{ki}.(fieldsIn{fieldsInd}).normFactorAver{ki};
%normValueMedian = norm.subject{ij}.bins{j}.trial{indexForNormValue}.period{l}.ch{ki}.(fieldsIn{fieldsInd}).normFactorMedian{ki};
%norm.subject{ij}.bins{j}.trial{k}.period{l}.ch{ki}.(fieldsIn{fieldsInd}).normFactorAver{ki} = normValueAver;
%norm.subject{ij}.bins{j}.trial{k}.period{l}.ch{ki}.(fieldsIn{fieldsInd}).normFactorMedian{ki} = normValueMedian;
end
end
% DEBUG
if handles.showDebugMessages == 1 && l == 1 && j == 1 && k == 3 && nrOfChannelsIn == 1
% for first period (l), and for first band (j)
disp(['Trial = ', num2str(k-2), ', NormAver: ', num2str(normValueAver), ...
', Mean = ', num2str(nanmean(norm.subject{ij}.bins{j}.trial{k-2}.period{l}.ch.(fieldsIn{fieldsInd}).aver))])
disp(['Trial = ', num2str(k-2), ', NormAver: ', num2str(normValueAver), ...
', Mean = ', num2str(nanmean(norm.subject{ij}.bins{j}.trial{k-1}.period{l}.ch.(fieldsIn{fieldsInd}).aver))])
disp(['Trial = ', num2str(k-2), ', NormAver: ', num2str(normValueAver), ...
', Mean = ', num2str(nanmean(norm.subject{ij}.bins{j}.trial{k}.period{l}.ch.(fieldsIn{fieldsInd}).aver))])
end
end
%% NORMALIZE THE DATA THEN USING THE VALUE COMPUTED ABOVE
if nrOfChannelsIn == 1 % the "defined bands"
% normFactorAver = norm.subject{ij}.bins{j}.trial{k}.period{l}.ch.(fieldsIn{fieldsInd}).normFactorAver;
% normFactorMedian = norm.subject{ij}.bins{j}.trial{k}.period{l}.ch.(fieldsIn{fieldsInd}).normFactorMedian;
% use subfunction
norm.subject{ij}.bins{j}.trial{k}.period{l}.ch ...
= organize_normalizeLowLevel(norm.subject{ij}.bins{j}.trial{k}.period{l}.ch, normFactorAver, normFactorMedian, fieldsIn{fieldsInd});
elseif nrOfChannelsIn > 1
% go through the channels
for ki = 1 : nrOfChannelsIn
% normFactorAver = norm.subject{ij}.bins{j}.trial{k}.period{l}.ch{ki}.(fieldsIn{fieldsInd}).normFactorAver{ki};
% normFactorMedian = norm.subject{ij}.bins{j}.trial{k}.period{l}.ch{ki}.(fieldsIn{fieldsInd}).normFactorMedian{ki};
% use subfunction
norm.subject{ij}.bins{j}.trial{k}.period{l}.ch{ki} ...
= organize_normalizeLowLevel(norm.subject{ij}.bins{j}.trial{k}.period{l}.ch{ki}, normFactorAver, normFactorMedian, fieldsIn{fieldsInd});
end
else
norm.subject{ij}.bins{j}.trial{k}.period{l}.ch.(fieldsIn{fieldsInd}).aver = NaN;
norm.subject{ij}.bins{j}.trial{k}.period{l}.ch.(fieldsIn{fieldsInd}).SD = NaN;
norm.subject{ij}.bins{j}.trial{k}.period{l}.ch.(fieldsIn{fieldsInd}).medianValue = NaN;
norm.subject{ij}.bins{j}.trial{k}.period{l}.ch.(fieldsIn{fieldsInd}).SD = NaN;
norm.subject{ij}.bins{j}.trial{k}.period{l}.ch.(fieldsIn{fieldsInd}).SDmedian = NaN;
norm.subject{ij}.bins{j}.trial{k}.period{l}.ch.(fieldsIn{fieldsInd}).N = Nan;
norm.subject{ij}.bins{j}.trial{k}.period{l}.ch.(fieldsIn{fieldsInd}).normFactorAver = NaN;
norm.subject{ij}.bins{j}.trial{k}.period{l}.ch.(fieldsIn{fieldsInd}).normFactorMedian = Nan;
warning('no norm factor')
end
end % l
end % k
end % j
end % ij
end % fieldInd
%% Subfunctions for normalization
function structureOut = organize_normalizeLowLevel(structureIn, normFactorAver, normFactorMedian, fieldIn)
% use more human-readable variable
% names for input data
averPower_in = structureIn.(fieldIn).aver;
medianPower_in = structureIn.(fieldIn).medianValue;
sdPower_in = structureIn.(fieldIn).SD;
structureOut = structureIn;
% normalize
averPower_out = averPower_in / normFactorAver;
medianPower_out = medianPower_in / normFactorMedian;
sdPower_out = sdPower_in / normFactorAver;
sdPower_outMedian = sdPower_in / normFactorMedian;
% and now assign to the output cell/structure "monster"
structureOut.(fieldIn).aver = averPower_out;
structureOut.(fieldIn).medianValue = medianPower_out;
structureOut.(fieldIn).SD = sdPower_out;
structureOut.(fieldIn).SDmedian = sdPower_outMedian;
structureOut.(fieldIn).N = structureIn.(fieldIn).N;