-
Notifications
You must be signed in to change notification settings - Fork 9
/
std_readpacfile.m
386 lines (350 loc) · 16.5 KB
/
std_readpacfile.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
% std_readfile() - Read data file containing STUDY measures.
%
% Usage:
% >> [data param range1 range2] = std_readfile(filename, 'key', val);
%
% Inputs:
% filename - [string] read specific file, for instance 's1.daterp'
% containing ERP data for dataset "s1.set". It is also
% possible to provide only the "base" file name "s1" and
% function will load the appropriate file based on the
% selected input measure (measure input).
%
% Optional inputs:
% 'channels' - [cell or integer] channel labels - for instance
% { 'cz' 'pz' } of channels to load from the data file.
% 'components' - [integer] component index in the selected EEG dataset
% for which to read the data
% 'timelimits' - [min max] ERSP/ERP time (latency in ms) range of interest
% 'freqlimits' - [min max] ERSP/Spectrum frequency range (in Hz) of interest
% 'measure' - ['erp'|'spec'|'ersp'|'itc'|'timef'|'erspbase'|'erspboot'
% 'itcboot'|'erpim'] data measure to read. If a full file name
% is provided, the data measure is selected automatically.
% 'getparamsonly' - ['on'|'off'] only read file parameters not data.
% 'trialselect' - Cell of cells defining the field and the values
% of the field in the trialinfo structure to be used to pull out the trials.
% i.e. if values are string: {'field1',{val1_fromfield1 val2_fromfield1}, 'field2'...}
% if values are numeric: {'field1',[val1 val2], 'field2'...}
% 'designvar' - Structure of independent variables with fields 'label'
% and 'value'. Each independent variable should have these
% two fields so the function can use these values to pull
% out the trials. If empty, all the trials are read.
% 'singletrials' - { 'on','off' } Extract single trials. Default 'off'
% 'getparamonly' - { 'on','off'} Get only parameters. Default 'off'
% 'concatenate' - { 'on','off'} In case of ERP images this function
% set 'singletrials' to 'on'. Default 'off'
% 'dataindices' - obsolete input
%
% Outputs:
% data - the multi-channel or multi-component data. The size of this
% output depends on the number of dimension (for ERSP or ERP
% for instance). The last dimension is channels or components.
% params - structure containing parameters saved with the data file.
% range1 - time points (ERP, ERSP) or frequency points (spectrum)
% range2 - frequency points (ERSP, ITCs)
% events - Event readed from the data structure
%
% Examples:
% % the examples below read all data channels for the selected files
% [ersp params times freqs] = std_readfiles('s1.datersp');
% [erp params times] = std_readfiles('s1.daterp');
% [erp params times] = std_readfiles('s1.daterp', timerange', [-100 500]);
% [erp params times] = std_readfiles('s1.daterp', timerange', [-100 500],'trialselect','load',[1 2],'type', {'Y'});
%
% Authors: Arnaud Delorme, SCCN, INC, UCSD, May 2010
% Copyright (C) Arnaud Delorme, SCCN, INC, UCSD, 2010, [email protected]
%
% This file is part of EEGLAB, see http://www.eeglab.org
% for the documentation and details.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice,
% this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
% THE POSSIBILITY OF SUCH DAMAGE.
% dimensions
% time x freqs x channel_comps x subjects_trials
function [measureData, parameters, measureRange1, measureRange2, measureRange3, events] = std_readpacfile(fileBaseName, varargin);
if nargin < 1
help std_readpacfile;
return;
end
if iscell(fileBaseName)
% this is when the data of a subject is split accross sessions
% then we need to call std_readpacfile several times and combine
% the results
% ------------------------------------------------------------
if length(fileBaseName) > 1
for iFile = 1:length(fileBaseName)
[measureDataTmp{iFile}, parameters, measureRange1, measureRange2, measureRange3, eventsTmp{iFile}] = std_readpacfile(fileBaseName{iFile}, varargin{:});
end
% combine arrays
measureData = measureDataTmp{1};
events = eventsTmp{1};
for iData = 2:length(measureDataTmp)
for iCell = 1:length(measureDataTmp{iData}(:))
if ~isempty(measureDataTmp{iData}{iCell})
if ndims(measureDataTmp{iData}{iCell}) == 2, measureData{iCell} = [ measureData{iCell} measureDataTmp{iData}{iCell} ];
elseif ndims(measureDataTmp{iData}{iCell}) == 2, measureData{iCell}(:, :, end+1:end+size(measureDataTmp{iData}{iCell},3)) = measureDataTmp{iData}{iCell};
end
end
end
end
return
else
fileBaseName = fileBaseName{1};
end
end
opt = finputcheck(varargin, { 'components' 'integer' [] [];
'getparamonly' 'string' { 'on','off' } 'off';
'trialselect' 'cell' {} {};
'trialinfo' 'struct' {} struct([]);
'designvar' 'struct' [] struct([]);
'singletrials' 'string' { 'on','off' } 'off';
'concatenate' 'string' { 'on','off' } 'off'; % ERPimage only
'channels' 'cell' [] {};
'cache' 'struct' [] struct([]);
'function' { 'function_handle' 'integer' } [] [];
'measure' 'string' {'datpac' 'icapac'} 'datpac';
'timelimits' 'real' [] [];
'freqlimits1' 'real' [] [];
'freqlimits2' 'real' [] [];
'dataindices' 'integer' [] [] }, 'std_readpacfile');
if ischar(opt), error(opt); end
if strcmpi(opt.concatenate, 'on'), opt.singletrials = 'on'; end
% get file extension
% ------------------
if ~isempty(opt.channels) || (~isempty(opt.dataindices) && opt.dataindices(1) < 0) , dataType = 'chan';
else dataType = 'comp';
end
[~, ~, currentFileExt] = fileparts(fileBaseName);
if length(currentFileExt) > 3 && (strcmpi(currentFileExt(2:4), 'dat') || strcmpi(currentFileExt(2:4), 'ica'))
opt.measure = currentFileExt(5:end);
if strcmpi(currentFileExt(2:4), 'dat'), dataType = 'chan';
else dataType = 'comp';
end
fileExt = '';
else
if strcmpi(dataType, 'chan'), fileExt = [ '.dat' opt.measure ];
else fileExt = [ '.ica' opt.measure ];
end
end
% get fields to read
% ------------------
v6Flag = testv6([ fileBaseName fileExt ]);
if v6Flag || strcmpi(opt.measure, 'topo')
if ~isempty(opt.channels)
fileData = load('-mat', [ fileBaseName fileExt ], 'labels');
end
else
fileData = matfile([ fileBaseName fileExt ]);
end
% get channel or component indices
% --------------------------------
if ~isempty(opt.channels) && isnumeric(opt.channels)
opt.dataindices = opt.channels;
elseif ~isempty(opt.channels)
chan.chanlocs = struct('labels', fileData.labels);
opt.dataindices = std_chaninds(chan, opt.channels);
elseif ~isempty(opt.components)
opt.dataindices = opt.components;
else opt.dataindices = abs(opt.dataindices);
end
if v6Flag
for iChan = 1:length(opt.dataindices)
chanList{iChan} = [ dataType int2str(opt.dataindices(iChan)) fastif(strcmpi(opt.measure, 'topo'), '_grid', '') ];
end
warning('off', 'MATLAB:load:variableNotFound');
if ~isempty(opt.dataindices)
fileData = load('-mat', [ fileBaseName fileExt ], chanList{:}, 'trialinfo', 'times', 'freqs1','freqs2','parameters', 'events', 'chanlocsforinterp', 'chanall');
else
fileData = load('-mat', [ fileBaseName fileExt ], 'trialinfo', 'times', 'freqs1','freqs2', 'parameters', 'events', 'chanlocsforinterp', 'chanall');
end
warning('on', 'MATLAB:load:variableNotFound');
end
% scan datasets
% -------------
if strcmpi(opt.getparamonly, 'on'), opt.dataindices = 1; end
measureRange1 = [];
measureRange2 = [];
measureRange3 = [];
measureData = [];
parameters = [];
events = {};
% get output for parameters and measure ranges
% --------------------------------------------
fileFields = fieldnames(fileData);
if ~isempty( strmatch('parameters', fileFields) )
parameters = removedup(fileData.parameters);
for index = 1:length(parameters), if iscell(parameters{index}), parameters{index} = { parameters{index} }; end; end
parameters = struct(parameters{:});
end
if ~isempty(strmatch('times', fileFields, 'exact')), measureRange1 = fileData.times; end
if ~isempty(strmatch('freqs1', fileFields, 'exact')), measureRange2 = fileData.freqs1; end
if ~isempty(strmatch('freqs2', fileFields, 'exact')), measureRange3 = fileData.freqs2; end
% if the function is only called to get parameters
% ------------------------------------------------
% if strcmpi(opt.measure, 'spec'), measureRange1 = measureRange2; opt.timelimits = opt.freqlimits; end
% Time
if ~isempty(opt.timelimits)
[measureRange1, indBegin1, indEnd1 ] = indicesselect(measureRange1, opt.timelimits);
else
indBegin1 = 1;
indEnd1 = length(measureRange1);
end
% Freqs 1
if ~isempty(opt.freqlimits1)
[measureRange2, indBegin2, indEnd2 ] = indicesselect(measureRange2, opt.freqlimits1);
else
indBegin2 = 1;
indEnd2 = length(measureRange2);
end
% Freqs 2
if ~isempty(opt.freqlimits1)
[measureRange3, indBegin3, indEnd3 ] = indicesselect(measureRange3, opt.freqlimits2);
else
indBegin3 = 1;
indEnd3 = length(measureRange3);
end
if strcmpi(opt.getparamonly, 'on')
return;
end
options = { opt.dataindices, opt.function, dataType, indBegin1, indEnd1, indBegin2, indEnd2, indBegin3, indEnd3 };
if isempty(opt.designvar)
[ measureData, events ] = getfiledata(fileData, NaN, options{:}); % read all data
measureData = { measureData };
events = { events };
else
[ measureData, events ] = globalgetfiledata(fileData, opt.designvar, options, {});
end
% remove duplicates in the list of parameters
% -------------------------------------------
function cella = removedup(cella)
[tmp, indices] = unique_bc(cella(1:2:end));
if length(tmp) ~= length(cella)/2
%fprintf('Warning: duplicate ''key'', ''val'' parameter(s), keeping the last one(s)\n');
end
cella = cella(sort(union(indices*2-1, indices*2)));
% find indices for selection of measure
% -------------------------------------
function [measureRange, indBegin, indEnd] = indicesselect(measureRange, measureLimits);
indBegin = 1;
indEnd = length(measureRange);
if ~isempty(measureRange) && ~isempty(measureLimits) && (measureLimits(1) > measureRange(1) || measureLimits(end) < measureRange(end))
indBegin = min(find(measureRange >= measureLimits(1)));
indEnd = max(find(measureRange <= measureLimits(end)));
measureRange = measureRange(indBegin:indEnd);
end
% recursive function to load data
% -------------------------------
function [ measureData, eventVals ] = globalgetfiledata(fileData, designvar, options, trialselect);
if length(designvar) == 0
[ measureData, eventVals ] = getfiledata(fileData, trialselect, options{:});
measureData = { measureData };
eventVals = { eventVals };
else
% scan independent variable values
if isfield(designvar(1), 'vartype') && strcmpi('continuous', designvar(1).vartype)
if ~ischar(designvar(1).value), designvar(1).value = ''; end
trialselect = { trialselect{:} designvar(1).label designvar(1).value };
[ tmpMeasureData tmpEvents ] = globalgetfiledata(fileData, designvar(2:end), options, trialselect);
measureData(1,:,:,:) = reshape(tmpMeasureData, [ 1 size(tmpMeasureData) ]);
eventVals( 1,:,:,:) = reshape(tmpEvents , [ 1 size(tmpEvents ) ]);
else
for iField = 1:length(designvar(1).value)
trialselect = { trialselect{:} designvar(1).label designvar(1).value{iField} };
[ tmpMeasureData, tmpEvents ] = globalgetfiledata(fileData, designvar(2:end), options, trialselect);
measureData(iField,:,:,:) = reshape(tmpMeasureData, [ 1 size(tmpMeasureData) ]);
eventVals( iField,:,:,:) = reshape(tmpEvents , [ 1 size(tmpEvents ) ]);
end
end
end
% load data from structure or file
% --------------------------------
function [ fieldData, events ] = getfiledata(fileData, trialselect, chan, func, dataType, indBegin1, indEnd1, indBegin2, indEnd2, indBegin3, indEnd3)
persistent tmpcache;
persistent hashcode;
if length(chan) > 1
% error('This function can only read one channel at a time');
end
% get trial indices
fieldData = [];
events = [];
subTrials = [];
trials = [];
if ~isempty(trialselect)
if isnumeric(trialselect) && isnan(trialselect(1))
trials = [1:length(fileData.trialinfo)]; % read all trials if NaN
else
[trials, events] = std_gettrialsind(fileData.trialinfo, trialselect{:});
if length(unique(diff(trials))) > 1
temptrials = [trials(1):trials(end)];
subTrials = trials-trials(1)+1;
trials = temptrials;
end
end
end
for index = 1:length(chan)
allfields = fieldnames(fileData);
% topoFlag = ~isempty(findstr(allfields{1}, '_grid'));
fieldToRead = [ dataType int2str(chan(index)) ];
% find trials
if isempty(trials)
return;
% trials = size(fileData.(fieldToRead), ndims(fileData.(fieldToRead))); % not sure what this does
end
% load data
warning('off', 'MATLAB:MatFile:OlderFormat');
if ndims(fileData.(fieldToRead)) == 3 % no single trials
tmpFieldData = fileData.(fieldToRead)(indBegin2:indEnd2,indBegin3:indEnd3,indBegin1:indEnd1);
% if ~isempty(subTrials), tmpFieldData = tmpFieldData(:, subTrials); end
else tmpFieldData = fileData.(fieldToRead)(indBegin2:indEnd2,indBegin3:indEnd3,indBegin1:indEnd1,trials); % frequencies first here (was freq x time x trials)
if ~isempty(subTrials), tmpFieldData = tmpFieldData(:, :, :, subTrials); end
end
if isfield(fileData, 'events') && ~isempty(fileData.events)
events = fileData.events(trials);
if ~isempty(subTrials), events = events(subTrials); end
end
warning('on', 'MATLAB:MatFile:OlderFormat');
% average single trials if necessary
if ~isempty(func)
tmpFieldData = func(tmpFieldData);
end
% store data
if index == 1 && length(chan) == 1
fieldData = tmpFieldData;
else
if index == 1
fieldData = zeros([ size(tmpFieldData) length(chan) ]);
end
if ndims(tmpFieldData) == 2
fieldData(:,:,index) = tmpFieldData;
elseif ndims(tmpFieldData) == 4
fieldData(:,:,:,:,index) = tmpFieldData;
end
end
end
% see if a file is v6 of v7.3
function v6 = testv6(x)
fid=fopen(x);
txt=char(fread(fid,[1,140],'*char'));
tmp = fclose(fid);
txt=[txt,char(0)];
txt=txt(1:find(txt==0,1,'first')-1);
if ~isempty(strfind(txt, 'MATLAB 5.0')), v6 = true; else v6 = false; end