-
Notifications
You must be signed in to change notification settings - Fork 9
/
std_readpacdata.m
304 lines (282 loc) · 13.1 KB
/
std_readpacdata.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
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
302
303
304
% std_reapacddata() - load pac measures for data channels or
% for all components of a specified cluster.
% Called by plotting functions
% Usage:
% >> [STUDY, datavals, times] = ...
% std_readpacdata(STUDY, ALLEEG, varargin);
% Inputs:
% STUDY - studyset structure containing some or all files in ALLEEG
% ALLEEG - vector of loaded EEG datasets
%
% Optional inputs:
% 'design' - [integer] read files from a specific STUDY design. Default
% is empty (use current design in STUDY.currentdesign). Use
% NaN to create a design with with all the data.
% 'channels' - [cell] list of channels to import {default: none}
% 'clusters' - [integer] list of clusters to import {[]|default: all but
% the parent cluster (1) and any 'NotClust' clusters}
% 'singletrials' - ['on'|'off'] load single trials spectral data (if
% available). Default is 'off'.
% 'subject' - [string] select a specific subject {default:all}
% 'datatype' - ['MIPAC'|'PAC'| select measure to load Default is 'PAC'.
% 'component' - [integer] select a specific component in a cluster.
% This is the index of the component in the cluster not the
% component number {default:all}
% 'timerange' - [min max] time range {default: whole measure range}
%
% Output:
% STUDY - updated studyset structure
% pacvals - [cell array] erp data (the cell array size is
% condition x groups)
% times - [float array] array of time values
%
% Example:
% [pac times] = std_readpacdata(STUDY, ALLEEG, 'channels', { ALLEEG(1).chanlocs(1).labels });
%
% Author: Ramon Martinez-Cancino, SCCN, UCSD
% Arnaud Delorme, SCCN, UCSD
% Copyright (C) 2020 Ramon Martinez-Cancino, INC, SCCN
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
function [STUDY, datavals, xvals, yvals, zvals, events, params] = std_readpacdata(STUDY, ALLEEG, varargin)
if nargin < 2
help std_readdata;
return;
end
STUDY = pop_erspparams(STUDY, 'default');
[opt moreopts] = finputcheck( varargin, { ...
'design' 'integer' [] STUDY.currentdesign;
'channels' 'cell' [] {};
'clusters' 'integer' [] [];
'timerange' 'real' [] [];
'freqrange1' 'real' [] [];
'freqrange2' 'real' [] [];
'datatype' 'string' { 'pac' 'mipac'} 'pac';
'singletrials' 'string' { 'on','off' } 'off';
'componentpol' 'string' { 'on','off' } 'on';
'component' 'integer' [] [];
'subject' 'string' [] '' }, ...
'std_readdata', 'ignore');
if ischar(opt), error(opt); end
dtype = opt.datatype; % data type
% get the file extension
% ----------------------
tmpDataType = opt.datatype;
if ~isempty(opt.channels), fileExt = '.datpac';
else fileExt = '.icapac';
end
% list of subjects
% ----------------
allSubjects = { STUDY.datasetinfo.subject };
uniqueSubjects = unique(allSubjects);
STUDY.subject = uniqueSubjects;
if ischar(opt.subject) && ~isempty(opt.subject), subjectList = {opt.subject}; else subjectList = opt.subject; end
if isempty(subjectList)
if isnan(opt.design), subjectList = STUDY.subject;
else subjectList = STUDY.design(opt.design).cases.value;
end
end
% options
% -------
opts = {};
if ~isempty(opt.timerange), opts = [opts(:)' {'timelimits'}, {opt.timerange} ]; end
if ~isempty(opt.freqrange1), opts = [opts(:)' {'freqlimits1'}, {opt.freqrange1} ]; end
if ~isempty(opt.freqrange2), opts = [opts(:)' {'freqlimits1'}, {opt.freqrange2} ]; end
opts = [ opts(:)' {'singletrials'} {opt.singletrials} ];
fprintf('Reading subjects'' data or looking up measure values in EEGLAB cache\n');
% get all sessions (same code as std_readdat)
% -------------------------------------------
allSessions = { STUDY.datasetinfo.session };
allSessions(cellfun(@isempty, allSessions)) = { 1 };
allSessions = cellfun(@num2str, allSessions, 'uniformoutput', false);
uniqueSessions = unique(allSessions);
for iSubj = 1:length(subjectList)
fprintf('.');
% check cache
bigstruct = [];
if ~isempty(opt.channels), bigstruct.channel = opt.channels;
else bigstruct.cluster = opt.clusters; % there can only be one cluster
end
bigstruct.datatype = opt.datatype;
bigstruct.singletrials = opt.singletrials;
bigstruct.subject = subjectList{iSubj};
bigstruct.component = opt.component;
bigstruct.options = opts;
if isnan(opt.design)
bigstruct.design.variable = struct([]);
else bigstruct.design.variable = STUDY.design(opt.design).variable;
end
% find component indices
% ----------------------
if ~isempty(opt.clusters)
datasetInds = strmatch(subjectList{iSubj}, { STUDY.datasetinfo.subject }, 'exact');
compList = [];
polList = [];
if isempty(opt.component)
for iDat = datasetInds(:)'
indSet = find(STUDY.cluster(opt.clusters).sets(1,:) == iDat); % each column contain info about the same subject so we many only consider the first row
if ~isempty(indSet)
compList = [ compList STUDY.cluster(opt.clusters).comps(indSet) ];
if strcmpi(dtype, 'erp') && strcmpi(opt.componentpol, 'on')
polList = [ polList componentPol(indSet) ];
end
end
end
else
if ~isempty(intersect(datasetInds, STUDY.cluster(opt.clusters).sets(:,opt.component)))
compList = [ compList STUDY.cluster(opt.clusters).comps(opt.component) ];
if strcmpi(dtype, 'erp') && strcmpi(opt.componentpol, 'on')
polList = [ polList componentPol(opt.component) ];
end
end
end
end
% read all channels/components at once
% hashcode = gethashcode(std_serialize(bigstruct));
% [STUDY.cache, tmpstruct] = eeg_cache(STUDY.cache, hashcode);
tmpstruct = []; % RAMONcheck cache management for PAC
if ~isempty(tmpstruct)
dataTmp{iSubj} = tmpstruct{1};
xvals = tmpstruct{2};
yvals = tmpstruct{3};
zvals = tmpstruct{4};
eventsTmp{iSubj} = tmpstruct{5};
params = tmpstruct{6};
else
datInds = find(strncmp( subjectList{iSubj}, allSubjects, max(cellfun(@length, allSubjects))));
fileName = getfilename({STUDY.datasetinfo(datInds).filepath}, STUDY.datasetinfo(datInds(1)).subject, { STUDY.datasetinfo(datInds).session }, fileExt, length(uniqueSessions) == 1);
if ~isempty(opt.channels)
[dataTmp{iSubj}, params, xvals, yvals, zvals, eventsTmp{iSubj} ] = std_readpacfile( fileName, 'designvar', struct(bigstruct.design.variable), opts{:}, 'channels', opt.channels);
else
[dataTmp{iSubj}, params, xvals, yvals, zvals, eventsTmp{iSubj} ] = std_readpacfile( fileName, 'designvar', struct(bigstruct.design.variable), opts{:}, 'components', compList);
end
% Data manipulation removed here(--)
% STUDY.cache = eeg_cache(STUDY.cache, hashcode, { dataTmp{iSubj} xvals yvals zvals eventsTmp{iSubj} params });
end
end
fprintf('\n');
% if single trials, swap the last 2 dim (put channels before trials) %
% RAMON: Check when more than one channel
if strcmpi(opt.singletrials, 'on') && length(opt.channels) > 1
% if ndims(dataTmp{1}{1}) == 4
% for iCase = 1:length(dataTmp)
% for iItem = 1:length(dataTmp{1}(:))
% dataTmp{iCase}{iItem} = permute(dataTmp{iCase}{iItem}, [1 3 2]);
% end
% end
% else
if ndims(dataTmp{1}{1}) == 5
for iCase = 1:length(dataTmp)
for iItem = 1:length(dataTmp{1}(:))
dataTmp{iCase}{iItem} = permute(dataTmp{iCase}{iItem}, [1 2 3 5 4]);
end
end
end
% end
end
% store data for all subjects
if strcmp(opt.datatype, 'MIPAC')
if length(opt.channels) > 1
dim = 5; % 6
else
dim = 4;
end
else
if length(opt.channels) > 1
dim = 5;
else
dim = 4; % ok for single channels no mipac
end
end
events = {};
if ~isempty(opt.clusters)
% Split ICA components from the same subjects need to be made
% as if coming from different subjects. E.g. 3 componets 2 S01 1S02 and
% 2 cond -> will yield dataTmp2 = {2×1 cell} {2×1 cell} {2×1 cell}
dataTmp2 = {};
realDim = dim;
if strcmpi(opt.singletrials, 'on'), realDim = realDim+1; end
for iDat1 = 1:length(dataTmp)
compNumbers = cellfun(@(x)size(x, realDim), dataTmp{iDat1});
if length(unique(compNumbers)) > 1
error('Cannot handle conditions with different number of components');
end
for iComps = 1:compNumbers(1)
dataTmp2{end+1} = [];
for iDat2 = 1:length(dataTmp{iDat1}(:))
% check dimensions of components
if strcmpi(opt.singletrials, 'on') && strcmpi(tmpDataType, 'MIPAC'), dataTmp2{end}{iDat2} = dataTmp{iDat1}{iDat2}(:,:,:,:,iComps);
else dataTmp2{end}{iDat2} = dataTmp{iDat1}{iDat2}(:,:,:,iComps);
end
end
dataTmp2{end} = reshape(dataTmp2{end}, size(dataTmp{iDat1}));
end
end
dataTmp = dataTmp2;
end
datavals = reorganizedata(dataTmp, dim); % here will pile up thee trials from all subjects in condition groups.
% reorganize data
% ---------------
function datavals = reorganizedata(dataTmp, dim)
datavals = cell(size(dataTmp{1}));
% copy data
for iItem=1:length(dataTmp{1}(:)')
numItems = sum(cellfun(@(x)size(x{iItem},dim)*(size(x{iItem},1) > 1), dataTmp)); % the size > 1 allows to detect empty array which have a non-null last dim
ind = find(~cellfun(@(x)isempty(x{iItem}), dataTmp));
if ~isempty(ind)
ind = ind(1);
switch dim
case 2, datavals{iItem} = zeros([ size(dataTmp{ind}{iItem},1) numItems], 'single');
case 3, datavals{iItem} = zeros([ size(dataTmp{ind}{iItem},1) size(dataTmp{ind}{iItem},2) numItems], 'single');
case 4, datavals{iItem} = zeros([ size(dataTmp{ind}{iItem},1) size(dataTmp{ind}{iItem},2) size(dataTmp{ind}{iItem},3) numItems], 'single');
case 5, datavals{iItem} = zeros([ size(dataTmp{ind}{iItem},1) size(dataTmp{ind}{iItem},2) size(dataTmp{ind}{iItem},3) size(dataTmp{ind}{iItem},4) numItems], 'single');
case 6, datavals{iItem} = zeros([ size(dataTmp{ind}{iItem},1) size(dataTmp{ind}{iItem},2) size(dataTmp{ind}{iItem},3) size(dataTmp{ind}{iItem},4) size(dataTmp{ind}{iItem},5) numItems], 'single');
end
end
end
for iItem=1:length(dataTmp{1}(:)')
count = 1;
for iCase = 1:length(dataTmp)
if ~isempty(dataTmp{iCase}{iItem})
numItems = size(dataTmp{iCase}{iItem},dim) * (size(dataTmp{iCase}{iItem},1) > 1); % the size > 1 allows to detect empty array which have a non-null last dim
switch dim
case 2, datavals{iItem}(:,count:count+numItems-1) = dataTmp{iCase}{iItem};
case 3, datavals{iItem}(:,:,count:count+numItems-1) = dataTmp{iCase}{iItem};
case 4, datavals{iItem}(:,:,:,count:count+numItems-1) = dataTmp{iCase}{iItem};
case 5, datavals{iItem}(:,:,:,:,count:count+numItems-1) = dataTmp{iCase}{iItem};
case 6, datavals{iItem}(:,:,:,:,:,count:count+numItems-1) = dataTmp{iCase}{iItem};
end
count = count+numItems;
end
end
end
% get file base name: filepath and sess are cell array (in case 2 files per subject)
% ----------------------------------------------------------------------------------
function filebase = getfilename(filepath, subj, sess, fileSuffix, onlyOneSession)
if onlyOneSession
filebase = fullfile(filepath{1}, [ subj fileSuffix ] );
else
if isempty(sess)
sess = { '1' };
end
for iSess = 1:length(sess)
if isnumeric(sess{iSess})
sesStr = [ '0' num2str(sess{iSess}) ];
else
sesStr = [ '0' sess{iSess} ];
end
filebase{iSess} = fullfile(filepath{iSess}, [ subj '_ses-' sesStr(end-1:end) fileSuffix ] );
end
end