/
ft_checkdata.m
1914 lines (1720 loc) · 67.5 KB
/
ft_checkdata.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function [data] = ft_checkdata(data, varargin)
% FT_CHECKDATA checks the input data of the main FieldTrip functions, e.g. whether the
% type of data structure corresponds with the required data. If necessary and possible,
% this function will adjust the data structure to the input requirements (e.g. change
% dimord, average over trials, convert inside from index into logical).
%
% If the input data does NOT correspond to the requirements, this function will give a
% warning message and if applicable point the user to external documentation (link to
% website).
%
% Use as
% [data] = ft_checkdata(data, ...)
%
% Optional input arguments should be specified as key-value pairs and can include
% feedback = 'yes' or 'no'
% datatype = raw, freq, timelock, comp, spike, source, mesh, dip, volume, segmentation, parcellation
% dimord = any combination of time, freq, chan, refchan, rpt, subj, chancmb, rpttap, pos
% senstype = ctf151, ctf275, ctf151_planar, ctf275_planar, neuromag122, neuromag306, bti148, bti248, bti248_planar, magnetometer, electrode
% fsample = sampling frequency to use to go from SPIKE to RAW representation
% ismeg = 'yes' or 'no', requires the data to have a grad structure
% iseeg = 'yes' or 'no', requires the data to have an elec structure
% isnirs = 'yes' or 'no', requires the data to have an opto structure
% hasunit = 'yes' or 'no'
% hascoordsys = 'yes' or 'no'
% haschantype = 'yes' or 'no'
% haschanunit = 'yes' or 'no'
% hassampleinfo = 'yes', 'no', or 'ifmakessense' (applies to raw and timelock data)
% hascumtapcnt = 'yes' or 'no' (only applies to freq data)
% hasdim = 'yes' or 'no'
% hasdof = 'yes' or 'no'
% hasbrain = 'yes' or 'no' (only applies to segmentation)
% insidestyle = logical, index, can also be empty
% cmbstyle = sparse, sparsewithpow, full, fullfast, fourier (applies to covariance and cross-spectral density)
% segmentationstyle = indexed, probabilistic (only applies to segmentation)
% parcellationstyle = indexed, probabilistic (only applies to parcellation)
% trialinfostyle = matrix, table or empty
%
% For some options you can specify multiple values, e.g.
% [data] = ft_checkdata(data, 'senstype', {'ctf151', 'ctf275'}), e.g. in megrealign
% [data] = ft_checkdata(data, 'datatype', {'timelock', 'freq'}), e.g. in sourceanalysis
%
% See also FT_DATATYPE_XXX for each of the respective data types.
% Copyright (C) 2007-2021, Robert Oostenveld
% Copyright (C) 2010-2012, Martin Vinck
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% for the documentation and details.
%
% FieldTrip 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 3 of the License, or
% (at your option) any later version.
%
% FieldTrip 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 FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id$
% in case of an error this function could use dbstack for more detailed
% user feedback
%
% this function should replace/encapsulate
% fixdimord
% fixinside
% fixprecision
% fixvolume
% fixpos
% data2raw
% raw2data
% grid2transform
% transform2grid
% fourier2crsspctrm
% freq2cumtapcnt
% sensortype
% time2offset
% offset2time
% fixsens -> this is kept a separate function because it should also be
% called from other modules
%
% other potential uses for this function:
% time -> offset in freqanalysis
% average over trials
% csd as matrix
% FIXME the following is difficult, if not impossible, to support without knowing the parameter
% FIXME it is presently (dec 2014) not being used anywhere in FT, so can be removed
% hastrials = 'yes' or 'no'
% check whether people are using deprecated options
sel = find(strcmp(varargin(1:2:end), 'hastrialdef'));
if ~isempty(sel)
ft_warning('the option ''hastrialdef'' is deprecated, please use ''hassampleinfo'' instead');
varargin{2*sel-1} = 'hassampleinfo';
end
sel = find(strcmp(varargin(1:2:end), 'inside'));
if ~isempty(sel)
ft_warning('the option ''inside'' is deprecated, please use ''insidestyle'' instead');
varargin{2*sel-1} = 'insidestyle';
end
sel = find(strcmp(varargin(1:2:end), 'cmbrepresentation'));
if ~isempty(sel)
ft_warning('the option ''cmbrepresentation'' is deprecated, please use ''cmbstyle'' instead');
varargin{2*sel-1} = 'cmbstyle';
end
% get the optional input arguments
feedback = ft_getopt(varargin, 'feedback', 'no');
dtype = ft_getopt(varargin, 'datatype'); % should not conflict with the ft_datatype function
dimord = ft_getopt(varargin, 'dimord');
stype = ft_getopt(varargin, 'senstype'); % senstype is a function name which should not be masked
ismeg = ft_getopt(varargin, 'ismeg');
iseeg = ft_getopt(varargin, 'iseeg');
isnirs = ft_getopt(varargin, 'isnirs');
hastrials = ft_getopt(varargin, 'hastrials');
hasunit = ft_getopt(varargin, 'hasunit', 'no');
hascoordsys = ft_getopt(varargin, 'hascoordsys', 'no');
haschantype = ft_getopt(varargin, 'haschantype', 'no');
haschanunit = ft_getopt(varargin, 'haschanunit', 'no');
hassampleinfo = ft_getopt(varargin, 'hassampleinfo', 'ifmakessense');
hasdim = ft_getopt(varargin, 'hasdim');
hascumtapcnt = ft_getopt(varargin, 'hascumtapcnt');
hasdof = ft_getopt(varargin, 'hasdof');
hasbrain = ft_getopt(varargin, 'hasbrain');
cmbstyle = ft_getopt(varargin, 'cmbstyle'); % sparse, sparsewithpow, full, fullfast, fourier
channelcmb = ft_getopt(varargin, 'channelcmb');
insidestyle = ft_getopt(varargin, 'insidestyle'); % logical, index
segmentationstyle = ft_getopt(varargin, 'segmentationstyle'); % this will be passed on to the corresponding ft_datatype_xxx function
parcellationstyle = ft_getopt(varargin, 'parcellationstyle'); % this will be passed on to the corresponding ft_datatype_xxx function
trialinfostyle = ft_getopt(varargin, 'trialinfostyle');
fsample = ft_getopt(varargin, 'fsample');
% determine the type of input data
israw = ft_datatype(data, 'raw');
isfreq = ft_datatype(data, 'freq');
istimelock = ft_datatype(data, 'timelock');
iscomp = ft_datatype(data, 'comp');
isspike = ft_datatype(data, 'spike');
isvolume = ft_datatype(data, 'volume');
issegmentation = ft_datatype(data, 'segmentation');
isparcellation = ft_datatype(data, 'parcellation');
issource = ft_datatype(data, 'source');
isdip = ft_datatype(data, 'dip');
ismvar = ft_datatype(data, 'mvar');
isfreqmvar = ft_datatype(data, 'freqmvar');
ischan = ft_datatype(data, 'chan');
ismesh = ft_datatype(data, 'mesh');
% FIXME use the istrue function on ismeg and hasxxx options
if ~isequal(feedback, 'no') % can be 'yes' or 'text'
if iscomp
% it can be comp and raw/timelock/freq at the same time, therefore this has to go first
nchan = size(data.topo,1);
ncomp = size(data.topo,2);
ft_info('the input is component data with %d components and %d original channels\n', ncomp, nchan);
end % if iscomp
if ismesh
% it can be comp and source at the same time, therefore this has to go first
data = fixpos(data);
npos = 0;
ntri = 0;
nhex = 0;
ntet = 0;
% the data can contain multiple surfaces
for i=1:numel(data)
npos = npos+size(data.pos,1);
if isfield(data, 'tri'), ntri = ntri+size(data.tri,1); end
if isfield(data, 'hex'), nhex = nhex+size(data.hex,1); end
if isfield(data, 'tet'), ntet = ntet+size(data.tet,1); end
end
if isfield(data,'tri')
ft_info('the input is mesh data with %d vertices and %d triangles\n', npos, ntri);
elseif isfield(data,'hex')
ft_info('the input is mesh data with %d vertices and %d hexahedrons\n', npos, nhex);
elseif isfield(data,'tet')
ft_info('the input is mesh data with %d vertices and %d tetrahedrons\n', npos, ntet);
else
ft_info('the input is mesh data with %d vertices', npos);
end
end % if ismesh
if israw
nchan = length(data.label);
ntrial = length(data.trial);
ft_info('the input is raw data with %d channels and %d trials\n', nchan, ntrial);
elseif istimelock
nchan = length(data.label);
ntime = length(data.time);
ft_info('the input is timelock data with %d channels and %d timebins\n', nchan, ntime);
elseif isfreq
if isfield(data, 'label')
nchan = length(data.label);
nfreq = length(data.freq);
if isfield(data, 'time'), ntime = num2str(length(data.time)); else ntime = 'no'; end
ft_info('the input is freq data with %d channels, %d frequencybins and %s timebins\n', nchan, nfreq, ntime);
elseif isfield(data, 'labelcmb')
nchan = length(data.labelcmb);
nfreq = length(data.freq);
if isfield(data, 'time'), ntime = num2str(length(data.time)); else ntime = 'no'; end
ft_info('the input is freq data with %d channel combinations, %d frequencybins and %s timebins\n', nchan, nfreq, ntime);
else
ft_error('cannot infer freq dimensions');
end
elseif isspike
nchan = length(data.label);
ft_info('the input is spike data with %d channels\n', nchan);
elseif isvolume
if issegmentation
ft_info('the input is segmented volume data with dimensions [%d %d %d]\n', data.dim(1), data.dim(2), data.dim(3));
print_voxelinfo(data)
print_segmentationinfo(data)
else
ft_info('the input is volume data with dimensions [%d %d %d]\n', data.dim(1), data.dim(2), data.dim(3));
print_voxelinfo(data)
end
elseif issource
data = fixpos(data); % ensure that positions are in pos, not in pnt
nsource = size(data.pos, 1);
if isparcellation
subtype = 'parcellated source';
else
subtype = 'source';
end
if isfield(data, 'dim')
ft_info('the input is %s data with %d brainordinates on a [%d %d %d] grid\n', subtype, nsource, data.dim(1), data.dim(2), data.dim(3));
else
ft_info('the input is %s data with %d brainordinates\n', subtype, nsource);
end
clear subtype
elseif isdip
ft_info('the input is dipole data\n');
elseif ismvar
ft_info('the input is mvar data\n');
elseif isfreqmvar
ft_info('the input is freqmvar data\n');
elseif ischan
nchan = length(data.label);
if isfield(data, 'brainordinate')
ft_info('the input is parcellated data with %d parcels\n', nchan);
else
ft_info('the input is chan data with %d channels\n', nchan);
end
end % if israw etc.
end % give feedback
if issource && isvolume
% it should be either one or the other: the choice here is to represent it as volume description since that is simpler to handle
% the conversion is done by removing the grid positions
data = rmfield(data, 'pos');
issource = false;
end
if isfield(data, 'trialinfo')
if strcmp(trialinfostyle, 'table')
if ismatrix(data.trialinfo)
data.trialinfo = array2table(data.trialinfo);
end
elseif strcmp(trialinfostyle, 'matrix')
if istable(data.trialinfo)
data.trialinfo = table2array(data.trialinfo);
end
else
% no conversion is needed
end
end
% the ft_datatype_XXX functions ensures the consistency of the XXX datatype
% and provides a detailed description of the dataformat and its history
if iscomp % this should go before israw/istimelock/isfreq
data = ft_datatype_comp(data, 'hassampleinfo', hassampleinfo);
elseif israw
data = ft_datatype_raw(data, 'hassampleinfo', hassampleinfo);
elseif istimelock
data = ft_datatype_timelock(data, 'hassampleinfo', hassampleinfo);
elseif isfreq
data = ft_datatype_freq(data);
elseif isspike
data = ft_datatype_spike(data);
elseif issegmentation % this should go before isvolume
data = ft_datatype_segmentation(data, 'segmentationstyle', segmentationstyle, 'hasbrain', hasbrain);
elseif isvolume
data = ft_datatype_volume(data);
elseif isparcellation % this should go before issource
data = ft_datatype_parcellation(data, 'parcellationstyle', parcellationstyle);
elseif issource
data = ft_datatype_source(data);
elseif isdip
data = ft_datatype_dip(data);
elseif ismvar || isfreqmvar
data = ft_datatype_mvar(data);
end
if ~isempty(dtype)
if ~isa(dtype, 'cell')
dtype = {dtype};
end
okflag = 0;
for i=1:length(dtype)
% check that the data matches with one or more of the required ft_datatypes
switch dtype{i}
case 'raw+comp'
okflag = okflag + (israw & iscomp);
case 'freq+comp'
okflag = okflag + (isfreq & iscomp);
case 'timelock+comp'
okflag = okflag + (istimelock & iscomp);
case 'source+mesh'
okflag = okflag + (issource & ismesh);
case 'raw'
okflag = okflag + (israw & ~iscomp);
case 'freq'
okflag = okflag + (isfreq & ~iscomp);
case 'timelock'
okflag = okflag + (istimelock & ~iscomp);
case 'comp'
okflag = okflag + (iscomp && ~(israw || istimelock || isfreq));
case 'spike'
okflag = okflag + isspike;
case 'volume'
okflag = okflag + isvolume;
case 'source'
okflag = okflag + issource;
case 'dip'
okflag = okflag + isdip;
case 'mvar'
okflag = okflag + ismvar;
case 'freqmvar'
okflag = okflag + isfreqmvar;
case 'chan'
okflag = okflag + ischan;
case 'segmentation'
okflag = okflag + issegmentation;
case 'parcellation'
okflag = okflag + isparcellation;
case 'mesh'
okflag = okflag + ismesh;
end % switch dtype
end % for dtype
% try to convert the data if needed
for iCell = 1:length(dtype)
if okflag
% the requested datatype is specified in descending order of
% preference (if there is a preference at all), so don't bother
% checking the rest of the requested data types if we already
% succeeded in converting
break;
end
if isequal(dtype(iCell), {'parcellation'}) && issegmentation
data = volume2source(data); % segmentation=volume, parcellation=source
data = ft_datatype_parcellation(data);
issegmentation = 0;
isvolume = 0;
isparcellation = 1;
issource = 1;
okflag = 1;
elseif isequal(dtype(iCell), {'segmentation'}) && isparcellation
data = source2volume(data); % segmentation=volume, parcellation=source
data = ft_datatype_segmentation(data);
isparcellation = 0;
issource = 0;
issegmentation = 1;
isvolume = 1;
okflag = 1;
elseif isequal(dtype(iCell), {'source'}) && isvolume
data = volume2source(data);
data = ft_datatype_source(data);
isvolume = 0;
issource = 1;
okflag = 1;
elseif isequal(dtype(iCell), {'volume'}) && (ischan || istimelock || isfreq)
if isfield(data, 'brainordinate')
data = parcellated2source(data);
data = ft_datatype_volume(data);
else
ft_error('cannot convert channel-level data to volumetric representation');
end
ischan = 0; istimelock = 0; isfreq = 0;
isvolume = 1;
okflag = 1;
elseif (isequal(dtype(iCell), {'source'}) || isequal(dtype(iCell), {'source+mesh'})) && (ischan || istimelock || isfreq)
if isfield(data, 'brainordinate')
data = parcellated2source(data);
data = ft_datatype_source(data);
else
data = chan2source(data);
data = ft_datatype_source(data);
end % converting channel data
ischan = 0; istimelock = 0; isfreq = 0;
issource = 1;
okflag = 1;
elseif isequal(dtype(iCell), {'volume'}) && issource
data = source2volume(data);
data = ft_datatype_volume(data);
isvolume = 1;
issource = 0;
okflag = 1;
elseif isequal(dtype(iCell), {'raw'}) && issource
data = source2raw(data);
data = ft_datatype_raw(data, 'hassampleinfo', hassampleinfo);
issource = 0;
israw = 1;
okflag = 1;
elseif isequal(dtype(iCell), {'raw'}) && istimelock
if iscomp
data = removefields(data, {'topo', 'topolabel', 'topodimord', 'unmixing', 'unmixingdimord'}); % these fields are not desired
iscomp = 0;
end
data = timelock2raw(data);
data = ft_datatype_raw(data, 'hassampleinfo', hassampleinfo);
istimelock = 0;
israw = 1;
okflag = 1;
elseif isequal(dtype(iCell), {'raw+comp'}) && istimelock && iscomp
data = timelock2raw(data);
data = ft_datatype_raw(data, 'hassampleinfo', hassampleinfo);
istimelock = 0;
iscomp = 1;
israw = 1;
okflag = 1;
elseif isequal(dtype(iCell), {'timelock+comp'}) && israw && iscomp
data = raw2timelock(data);
data = ft_datatype_timelock(data, 'hassampleinfo', hassampleinfo);
istimelock = 1;
iscomp = 1;
israw = 0;
okflag = 1;
elseif isequal(dtype(iCell), {'comp'}) && israw && iscomp
data = keepfields(data, {'label', 'topo', 'topolabel', 'unmixing', 'elec', 'grad', 'cfg'}); % these are the only relevant fields
data = ft_datatype_comp(data, 'hassampleinfo', hassampleinfo);
israw = 0;
iscomp = 1;
okflag = 1;
elseif isequal(dtype(iCell), {'comp'}) && istimelock && iscomp
data = keepfields(data, {'label', 'topo', 'topolabel', 'unmixing', 'elec', 'grad', 'cfg'}); % these are the only relevant fields
data = ft_datatype_comp(data, 'hassampleinfo', hassampleinfo);
istimelock = 0;
iscomp = 1;
okflag = 1;
elseif isequal(dtype(iCell), {'comp'}) && isfreq && iscomp
data = keepfields(data, {'label', 'topo', 'topolabel', 'unmixing', 'elec', 'grad', 'cfg'}); % these are the only relevant fields
data = ft_datatype_comp(data, 'hassampleinfo', 'no'); % freq data does not have sampleinfo
isfreq = 0;
iscomp = 1;
okflag = 1;
elseif isequal(dtype(iCell), {'raw'}) && israw
if iscomp
data = removefields(data, {'topo', 'topolabel', 'topodimord', 'unmixing', 'unmixingdimord'}); % these fields are not desired
iscomp = 0;
end
data = ft_datatype_raw(data, 'hassampleinfo', hassampleinfo);
okflag = 1;
elseif isequal(dtype(iCell), {'timelock'}) && istimelock
if iscomp
data = removefields(data, {'topo', 'topolabel', 'topodimord', 'unmixing', 'unmixingdimord'}); % these fields are not desired
iscomp = 0;
end
data = ft_datatype_timelock(data, 'hassampleinfo', hassampleinfo);
okflag = 1;
elseif isequal(dtype(iCell), {'freq'}) && isfreq
if iscomp
data = removefields(data, {'topo', 'topolabel', 'topodimord', 'unmixing', 'unmixingdimord'}); % these fields are not desired
iscomp = 0;
end
data = ft_datatype_freq(data);
okflag = 1;
elseif isequal(dtype(iCell), {'timelock'}) && israw
if iscomp
data = removefields(data, {'topo', 'topolabel', 'topodimord', 'unmixing', 'unmixingdimord'}); % these fields are not desired
iscomp = 0;
end
data = raw2timelock(data);
data = ft_datatype_timelock(data, 'hassampleinfo', hassampleinfo);
israw = 0;
istimelock = 1;
okflag = 1;
elseif isequal(dtype(iCell), {'raw'}) && isfreq
if iscomp
data = removefields(data, {'topo', 'topolabel', 'topodimord', 'unmixing', 'unmixingdimord'}); % these fields are not desired
iscomp = 0;
end
data = freq2raw(data);
data = ft_datatype_raw(data, 'hassampleinfo', hassampleinfo);
isfreq = 0;
israw = 1;
okflag = 1;
elseif isequal(dtype(iCell), {'raw'}) && ischan
data = chan2timelock(data);
data = timelock2raw(data);
data = ft_datatype_raw(data, 'hassampleinfo', hassampleinfo);
ischan = 0;
israw = 1;
okflag = 1;
elseif isequal(dtype(iCell), {'timelock'}) && ischan
data = chan2timelock(data);
data = ft_datatype_timelock(data, 'hassampleinfo', hassampleinfo);
ischan = 0;
istimelock = 1;
okflag = 1;
elseif isequal(dtype(iCell), {'freq'}) && ischan
data = chan2freq(data);
data = ft_datatype_freq(data);
ischan = 0;
isfreq = 1;
okflag = 1;
elseif isequal(dtype(iCell), {'spike'}) && israw
data = raw2spike(data);
data = ft_datatype_spike(data);
israw = 0;
isspike = 1;
okflag = 1;
elseif isequal(dtype(iCell), {'raw'}) && isspike
data = spike2raw(data,fsample);
data = ft_datatype_raw(data, 'hassampleinfo', hassampleinfo);
isspike = 0;
israw = 1;
okflag = 1;
end
end % for iCell
if ~okflag
% construct an error message
typestr = printor(dtype, true);
helpfun = cell(size(dtype));
for i=1:numel(dtype)
helpfun{i} = sprintf('ft_datatype_%s', dtype{i});
end
helpfun = helpfun(cellfun(@exist, helpfun)>0);
if ~isempty(helpfun)
helpstr = printor(helpfun);
ft_error('This function requires %s data as input, see %s.', typestr, helpstr);
else
ft_error('This function requires %s data as input.', typestr);
end
end % if okflag
end
if ~isempty(dimord)
if ~isa(dimord, 'cell')
dimord = {dimord};
end
if isfield(data, 'dimord')
okflag = any(strcmp(data.dimord, dimord));
else
okflag = 0;
end
if ~okflag
% construct an error message
ft_error('This function requires data with a dimord of %s.', printor(dimord, true));
end % if okflag
end
if ~isempty(stype)
if ~isa(stype, 'cell')
stype = {stype};
end
if isfield(data, 'grad') || isfield(data, 'elec') || isfield(data, 'opto')
if any(strcmp(ft_senstype(data), stype))
okflag = 1;
elseif any(cellfun(@ft_senstype, repmat({data}, size(stype)), stype))
% this is required to detect more general types, such as "meg" or "ctf" rather than "ctf275"
okflag = 1;
else
okflag = 0;
end
else
% the data does not contain a sensor array
okflag = 0;
end
if ~okflag
% construct an error message
ft_error('This function requires data with an %s sensor array.', printor(stype, true));
end % if okflag
end
if ~isempty(ismeg)
if isequal(ismeg, 'yes')
okflag = isfield(data, 'grad');
elseif isequal(ismeg, 'no')
okflag = ~isfield(data, 'grad');
end
if ~okflag && isequal(ismeg, 'yes')
ft_error('This function requires MEG data with a ''grad'' field');
elseif ~okflag && isequal(ismeg, 'no')
ft_error('This function should not be given MEG data with a ''grad'' field');
end % if okflag
end
if ~isempty(iseeg)
if isequal(iseeg, 'yes')
okflag = isfield(data, 'elec');
elseif isequal(iseeg, 'no')
okflag = ~isfield(data, 'elec');
end
if ~okflag && isequal(iseeg, 'yes')
ft_error('This function requires EEG data with an ''elec'' field');
elseif ~okflag && isequal(iseeg, 'no')
ft_error('This function should not be given EEG data with an ''elec'' field');
end % if okflag
end
if ~isempty(isnirs)
if isequal(isnirs, 'yes')
okflag = isfield(data, 'opto');
elseif isequal(isnirs, 'no')
okflag = ~isfield(data, 'opto');
end
if ~okflag && isequal(isnirs, 'yes')
ft_error('This function requires NIRS data with an ''opto'' field');
elseif ~okflag && isequal(isnirs, 'no')
ft_error('This function should not be given NIRS data with an ''opto'' field');
end % if okflag
end
if ~isempty(insidestyle)
if strcmp(insidestyle, 'index')
ft_warning('the indexed representation of inside/outside source locations is deprecated');
end
% TODO absorb the fixinside function into this code
data = fixinside(data, insidestyle);
okflag = isfield(data, 'inside');
if ~okflag
% construct an error message
ft_error('This function requires data with an ''inside'' field.');
end % if okflag
end
if istrue(hasunit) && ~isfield(data, 'unit')
% calling convert_units with only the input data adds the units without converting
data = ft_determine_units(data);
end % if hasunit
if istrue(hascoordsys) && ~isfield(data, 'coordsys')
data = ft_determine_coordsys(data);
end % if hascoordsys
if istrue(haschantype) && ~isfield(data, 'chantype')
data.chantype = ft_chantype(data);
end % if haschantype
if istrue(haschanunit) && ~isfield(data, 'chanunit')
data.chanunit = ft_chanunit(data);
end % if haschanunit
if isequal(hastrials, 'yes')
hasrpt = isfield(data, 'trial');
if ~hasrpt && isfield(data, 'dimord')
% instead look in the dimord for rpt or subj
hasrpt = ~isempty(strfind(data.dimord, 'rpt')) || ...
~isempty(strfind(data.dimord, 'rpttap')) || ...
~isempty(strfind(data.dimord, 'subj'));
end
if ~hasrpt
ft_error('This function requires data with a ''trial'' field');
end % if hasrpt
elseif isequal(hastrials, 'no') && istimelock
if ~isfield(data, 'avg') && (isfield(data, 'trial') || isfield(data, 'individual'))
% average on the fly
tmpcfg = [];
tmpcfg.keeptrials = 'no';
data = ft_timelockanalysis(tmpcfg, data);
end
end
if strcmp(hasdim, 'yes') && ~isfield(data, 'dim')
data.dim = pos2dim(data.pos);
elseif strcmp(hasdim, 'no') && isfield(data, 'dim')
data = rmfield(data, 'dim');
end % if hasdim
if strcmp(hascumtapcnt, 'yes') && ~isfield(data, 'cumtapcnt')
ft_error('This function requires data with a ''cumtapcnt'' field');
elseif strcmp(hascumtapcnt, 'no') && isfield(data, 'cumtapcnt')
data = rmfield(data, 'cumtapcnt');
end % if hascumtapcnt
if strcmp(hasdof, 'yes') && ~isfield(data, 'dof')
ft_error('This function requires data with a ''dof'' field');
elseif strcmp(hasdof, 'no') && isfield(data, 'dof')
data = rmfield(data, 'dof');
end % if hasdof
if ~isempty(cmbstyle)
if istimelock
data = fixcov(data, cmbstyle);
elseif isfreq
data = fixcsd(data, cmbstyle, channelcmb);
elseif isfreqmvar
data = fixcsd(data, cmbstyle, channelcmb);
else
ft_error('this function requires data with a covariance, coherence or cross-spectrum');
end
end % cmbstyle
if isfield(data, 'grad')
% ensure that the gradiometer structure is up to date
data.grad = ft_datatype_sens(data.grad);
end
if isfield(data, 'elec')
% ensure that the electrode structure is up to date
data.elec = ft_datatype_sens(data.elec);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% represent the covariance matrix in a particular manner
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [data] = fixcov(data, desired)
if any(isfield(data, {'cov', 'corr'}))
if ~isfield(data, 'labelcmb')
current = 'full';
else
current = 'sparse';
end
else
ft_error('Could not determine the current representation of the covariance matrix');
end
if isequal(current, desired)
% nothing to do
elseif strcmp(current, 'full') && strcmp(desired, 'sparse')
% FIXME should be implemented
ft_error('not yet implemented');
elseif strcmp(current, 'sparse') && strcmp(desired, 'full')
% FIXME should be implemented
ft_error('not yet implemented');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% represent the cross-spectral density matrix in a particular manner
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [data] = fixcsd(data, desired, channelcmb)
% FIXCSD converts univariate frequency domain data (fourierspctrm) into a bivariate
% representation (crsspctrm), or changes the representation of bivariate frequency
% domain data (sparse/full/sparsewithpow, sparsewithpow only works for crsspctrm or
% fourierspctrm)
% Copyright (C) 2010, Jan-Mathijs Schoffelen, Robert Oostenveld
if isfield(data, 'crsspctrm') && isfield(data, 'powspctrm')
current = 'sparsewithpow';
elseif isfield(data, 'powspctrm')
current = 'sparsewithpow';
elseif isfield(data, 'fourierspctrm') && ~isfield(data, 'labelcmb')
current = 'fourier';
elseif ~isfield(data, 'labelcmb')
current = 'full';
elseif isfield(data, 'labelcmb')
current = 'sparse';
else
ft_error('Could not determine the current representation of the %s matrix', param);
end
% first go from univariate fourier to the required bivariate representation
if isequal(current, desired)
% nothing to do
elseif strcmp(current, 'fourier') && strcmp(desired, 'sparsewithpow')
if startsWith(data.dimord, 'rpttap')
nrpt = size(data.cumtapcnt,1);
else
nrpt = 1;
end
if contains(data.dimord, 'freq'), nfrq = length(data.freq); else nfrq = 1; end
if contains(data.dimord, 'time'), ntim = length(data.time); else ntim = 1; end
fastflag = all(data.cumtapcnt(:)==data.cumtapcnt(1));
flag = nrpt==1; % needed to truncate the singleton dimension upfront
% create auto-spectra
nchan = length(data.label);
if fastflag
% all trials have the same amount of tapers
powspctrm = zeros(nrpt,nchan,nfrq,ntim);
ntap = data.cumtapcnt(1);
for p = 1:ntap
powspctrm = powspctrm + abs(data.fourierspctrm(p:ntap:end,:,:,:,:)).^2;
end
powspctrm = powspctrm./ntap;
else
% different amount of tapers
powspctrm = zeros(nrpt,nchan,nfrq,ntim) + zeros(nrpt,nchan,nfrq,ntim)*1i;
sumtapcnt = [0;cumsum(data.cumtapcnt(:))];
for p = 1:nrpt
indx = (sumtapcnt(p)+1):sumtapcnt(p+1);
tmpdat = data.fourierspctrm(indx,:,:,:);
powspctrm(p,:,:,:) = (sum(tmpdat.*conj(tmpdat),1))./data.cumtapcnt(p);
end
end
% create cross-spectra
if ~isempty(channelcmb)
ncmb = size(channelcmb,1);
cmbindx = zeros(ncmb,2);
labelcmb = cell(ncmb,2);
for k = 1:ncmb
ch1 = find(strcmp(data.label, channelcmb(k,1)));
ch2 = find(strcmp(data.label, channelcmb(k,2)));
if ~isempty(ch1) && ~isempty(ch2)
cmbindx(k,:) = [ch1 ch2];
labelcmb(k,:) = data.label([ch1 ch2])';
end
end
crsspctrm = zeros(nrpt,ncmb,nfrq,ntim) + 1i.*zeros(nrpt,ncmb,nfrq,ntim);
if fastflag
for p = 1:ntap
tmpdat1 = data.fourierspctrm(p:ntap:end,cmbindx(:,1),:,:,:);
tmpdat2 = data.fourierspctrm(p:ntap:end,cmbindx(:,2),:,:,:);
crsspctrm = crsspctrm + tmpdat1.*conj(tmpdat2);
end
crsspctrm = crsspctrm./ntap;
else
for p = 1:nrpt
indx = (sumtapcnt(p)+1):sumtapcnt(p+1);
tmpdat1 = data.fourierspctrm(indx,cmbindx(:,1),:,:);
tmpdat2 = data.fourierspctrm(indx,cmbindx(:,2),:,:);
crsspctrm(p,:,:,:) = (sum(tmpdat1.*conj(tmpdat2),1))./data.cumtapcnt(p);
end
end
data.crsspctrm = crsspctrm;
data.labelcmb = labelcmb;
end
data.powspctrm = powspctrm;
data = rmfield(data, 'fourierspctrm');
if ntim>1
data.dimord = 'chan_freq_time';
else
data.dimord = 'chan_freq';
end
if nrpt>1
data.dimord = ['rpt_' data.dimord];
end
if flag
siz = size(data.powspctrm);
data.powspctrm = reshape(data.powspctrm, [siz(2:end) 1]);
if isfield(data, 'crsspctrm')
siz = size(data.crsspctrm);
data.crsspctrm = reshape(data.crsspctrm, [siz(2:end) 1]);
end
end
elseif strcmp(current, 'fourier') && strcmp(desired, 'sparse')
if isempty(channelcmb), ft_error('no channel combinations are specified'); end
if startsWith(data.dimord, 'rpttap')
nrpt = size(data.cumtapcnt,1);
else
nrpt = 1;
end
if contains(data.dimord, 'freq'), nfrq = length(data.freq); else nfrq = 1; end
if contains(data.dimord, 'time'), ntim = length(data.time); else ntim = 1; end
flag = nrpt==1; % flag needed to squeeze first dimension if singleton
ncmb = size(channelcmb,1);
cmbindx = zeros(ncmb,2);
labelcmb = cell(ncmb,2);
for k = 1:ncmb
ch1 = find(strcmp(data.label, channelcmb(k,1)));
ch2 = find(strcmp(data.label, channelcmb(k,2)));
if ~isempty(ch1) && ~isempty(ch2)
cmbindx(k,:) = [ch1 ch2];
labelcmb(k,:) = data.label([ch1 ch2])';
end
end
sumtapcnt = [0;cumsum(data.cumtapcnt(:))];
fastflag = all(data.cumtapcnt(:)==data.cumtapcnt(1));
if fastflag && nrpt>1
ntap = data.cumtapcnt(1);
% compute running sum across tapers
siz = [size(data.fourierspctrm) 1];
for p = 1:ntap
indx = p:ntap:nrpt*ntap;
if p==1
tmpc = zeros(numel(indx), size(cmbindx,1), siz(3), siz(4)) + ...
1i.*zeros(numel(indx), size(cmbindx,1), siz(3), siz(4));
end
for k = 1:size(cmbindx,1)
tmpc(:,k,:,:) = data.fourierspctrm(indx,cmbindx(k,1),:,:).* ...
conj(data.fourierspctrm(indx,cmbindx(k,2),:,:));
end
if p==1
crsspctrm = tmpc;
else
crsspctrm = tmpc + crsspctrm;
end
end
crsspctrm = crsspctrm./ntap;
else
crsspctrm = zeros(nrpt, ncmb, nfrq, ntim);
for p = 1:nrpt
indx = (sumtapcnt(p)+1):sumtapcnt(p+1);
tmpdat1 = data.fourierspctrm(indx,cmbindx(:,1),:,:);
tmpdat2 = data.fourierspctrm(indx,cmbindx(:,2),:,:);
crsspctrm(p,:,:,:) = (sum(tmpdat1.*conj(tmpdat2),1))./data.cumtapcnt(p);
end
end
data.crsspctrm = crsspctrm;
data.labelcmb = labelcmb;
data = rmfield(data, 'fourierspctrm');
data = rmfield(data, 'label');
if ntim>1
data.dimord = 'chancmb_freq_time';
else
data.dimord = 'chancmb_freq';
end
if nrpt>1
data.dimord = ['rpt_' data.dimord];
end
if flag
if isfield(data,'powspctrm')
% deal with the singleton 'rpt', i.e. remove it
siz = size(data.powspctrm);
data.powspctrm = reshape(data.powspctrm, [siz(2:end) 1]);
end
if isfield(data,'crsspctrm')
% this conditional statement is needed in case there's a single channel
siz = size(data.crsspctrm);
data.crsspctrm = reshape(data.crsspctrm, [siz(2:end) 1]);
end
end
elseif strcmp(current, 'fourier') && strcmp(desired, 'full')
% this is how it is currently and the desired functionality of prepare_freq_matrices
if startsWith(data.dimord, 'rpttap')
nrpt = size(data.cumtapcnt, 1);
flag = 0;
else
nrpt = 1;
flag = 1;
end
if contains(data.dimord, 'freq'), nfrq = length(data.freq); else nfrq = 1; end
if contains(data.dimord, 'time'), ntim = length(data.time); else ntim = 1; end
if any(data.cumtapcnt(1,:) ~= data.cumtapcnt(1,1)), ft_error('this only works when all frequencies have the same number of tapers'); end
nchan = length(data.label);
crsspctrm = zeros(nrpt,nchan,nchan,nfrq,ntim);
sumtapcnt = [0;cumsum(data.cumtapcnt(:,1))];
for k = 1:ntim
for m = 1:nfrq
for p = 1:nrpt
% FIXME speed this up in the case that all trials have equal number of tapers
indx = (sumtapcnt(p)+1):sumtapcnt(p+1);
tmpdat = transpose(data.fourierspctrm(indx,:,m,k));
crsspctrm(p,:,:,m,k) = (tmpdat*tmpdat')./data.cumtapcnt(p);
clear tmpdat;
end
end
end
data.crsspctrm = crsspctrm;
data = rmfield(data, 'fourierspctrm');
if ntim>1
data.dimord = 'chan_chan_freq_time';
else
data.dimord = 'chan_chan_freq';
end
if nrpt>1
data.dimord = ['rpt_' data.dimord];
end
% remove first singleton dimension
if flag || nrpt==1, siz = size(data.crsspctrm); data.crsspctrm = reshape(data.crsspctrm, siz(2:end)); end
elseif strcmp(current, 'fourier') && strcmp(desired, 'fullfast')
nrpt = size(data.fourierspctrm, 1);
nchn = numel(data.label);
nfrq = length(data.freq);
if contains(data.dimord, 'time'), ntim = length(data.time); else ntim = 1; end
data.fourierspctrm = reshape(data.fourierspctrm, [nrpt nchn nfrq*ntim]);
%data.fourierspctrm(~isfinite(data.fourierspctrm)) = 0;
crsspctrm = complex(zeros(nchn,nchn,nfrq*ntim));