-
Notifications
You must be signed in to change notification settings - Fork 1
/
seldata.m
executable file
·157 lines (138 loc) · 4.56 KB
/
seldata.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
function seldata(info, opt, subj)
%SELDATA get data from recordings and put them in subject directory
% it recreates the "info.nick" folder for each subject
%
% INFO
% .recs: path of /data1/projects/PROJ/recordings/REC/subjects/
% .mod: modality, MOD in /data1/projects/PROJ/subjects/SUBJ/MOD/NICK/
%
% .data: name of projects/PROJ/subjects/
% .nick: NICK in /data1/projects/PROJ/subjects/SUBJ/MOD/NICK/
% .log: name of the file and directory to save log
%
% .sens.file: file with EEG sensors. It can be sfp or mat. It's included
% in data struct. If empty, it does not read the sensors.
%
% CFG.OPT
% .rcnd: specific name of the condition of interest in the raw recording folder
%
% .trialfun: name of the trialfun used to read the data, see below.
% The function should be in NICK_private/
% .trialopt: options to pass to trialfun_XXX
% .selchan: channels to read. It can be a vector or a cell of
% strings with the elec names on file (Micromed elec
% names are ' 1' ' 2' ' 3')
% .label: if not empty, labels of electrodes to rename (same
% length as cfg.opt.selchan)
%
% IN
% raw data in any format Fieldtrip can read in the recording folder:
% /data1/projects/PROJ/recordings/REC/subjects/SUBJ/MOD/RAW/
%
% OUT
% data in FieldTrip format in /data1/projects/PROJ/subjects/SUBJ/MOD/NICK/
%
% You need to write your own function to read the data and the events.
% Call the function something like "trialfun_XXX" and use as:
% [trl, event] = trialfun_XXX(opt)
% where trl is 1x3 vector (as in ft_definetrial) and event is the structure
% which can be used later on in redef.m to prepare the actual trials.
% It's better if you prepare only one big trial. gclean will clean the
% whole trial and prefers continuous data. You can create smaller trials
% later, during redef.m
%
% Part of EVENTBASED preprocessing
% see also SELDATA, GCLEAN, REDEF
%---------------------------%
%-start log
output = sprintf('%s (%04d) began at %s on %s\n', ...
mfilename, subj, datestr(now, 'HH:MM:SS'), datestr(now, 'dd-mmm-yy'));
tic_t = tic;
%---------------------------%
%---------------------------%
%-dir and files
rdir = sprintf('%s%04d/%s/%s/', info.recs, subj, info.mod, 'raw'); % recording raw
ddir = sprintf('%s%04d/%s/%s/', info.data, subj, info.mod, info.nick); % data dir
if isdir(ddir); rmdir(ddir, 's'); end
mkdir(ddir)
%-----------------%
%-read sensors (can be sfp or mat)
hassens = false;
if isfield(info.sens, 'file') && ~isempty(info.sens.file)
hassens = true;
sens = ft_read_sens(info.sens.file);
sens.label = upper(sens.label); % <- EGI labels are uppercase, but the elec file is lowercase
end
%-----------------%
prepr_name = '_A'; % preprocessing name to append
%---------------------------%
%---------------------------%
%-find raw data
allfile = dir([rdir '*' opt.rcnd '*']);
for i = 1:numel(allfile)
dataset = [rdir allfile(i).name];
%-----------------%
%-definetrials
if isfield(opt, 'trialcfg')
cfg = opt.trialcfg;
else
cfg = [];
end
cfg.trialfun = opt.trialfun;
cfg.dataset = dataset;
cfg = ft_definetrial(cfg);
%--------%
%-ignore files with no events
if all(cfg.trl(1,1:3) == [0 0 0])
continue
end
%--------%
%-----------------%
%-----------------%
%-preprocessing
cfg.feedback = 'off';
if iscell(opt.selchan)
cfg.channel = opt.selchan{subj};
else
cfg.channel = opt.selchan;
end
cfg.continuous = 'yes'; % necessary for MEG data over trials
data = ft_preprocessing(cfg);
event = ft_findcfg(data.cfg, 'event');
if ischar(event) && ...
strcmp(event, 'empty - this was cleared by checkconfig')
event = ft_read_event(event);
end
%-----------------%
%-----------------%
%-fix channels
if ~isempty(opt.label)
data.label = opt.label;
end
if hassens
data.elec = sens;
end
%-----------------%
%-----------------%
%-save data
[~, filename] = fileparts(allfile(i).name);
savename = [info.nick '_' filename prepr_name]; % <-- add nick name
save([ddir savename], 'data', 'event');
clear data
%-----------------%
end
%---------------------------%
%---------------------------%
%-end log
toc_t = toc(tic_t);
outtmp = sprintf('%s (%04d) ended at %s on %s after %s\n\n', ...
mfilename, subj, datestr(now, 'HH:MM:SS'), datestr(now, 'dd-mmm-yy'), ...
datestr( datenum(0, 0, 0, 0, 0, toc_t), 'HH:MM:SS'));
output = [output outtmp];
%-----------------%
fprintf(output)
fid = fopen([info.log '.txt'], 'a');
fwrite(fid, output);
fclose(fid);
%-----------------%
%---------------------------%