Skip to content

Commit ce36442

Browse files
committed
Added missing maxfilter scripts
1 parent a5523b3 commit ce36442

File tree

2 files changed

+329
-0
lines changed

2 files changed

+329
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
function badstr = bad_channels_from_mf(raw_file, raw_file_out, badfile, out_dir, finstr, Nbuf, OverWrite, ParType, do_eval)
2+
% get bad channels for one fiff-file from maxfilter using -autobad
3+
% raw_file: fiff-file for which bad channels needed
4+
% raw_file_out: name of output fiff-file from maxfilter
5+
% badfile: file that records bad channel information
6+
% out_dir: output directory
7+
% finstr: text string with maxfilter parameters
8+
% Nbuf: number of buffers from fiff-file
9+
% OverWrite: 0/1, whether output files to be overwritten
10+
% ParType: Parallelisation option
11+
% do_eval: 0/1, whether commands to be executed, or only to be displayed at command line
12+
% Olaf Hauk, Sep 14 (adapated from RH script)
13+
14+
outfile = fullfile(out_dir,sprintf('%s_bad',raw_file_out));
15+
16+
if ~exist(badfile) | OverWrite
17+
18+
% Write out movements too...
19+
posfile = fullfile(out_dir,sprintf('%s_headpos.txt',raw_file_out));
20+
21+
finstr = [finstr ' -hp ' posfile];
22+
23+
finstr = [finstr ' -v | tee ' outfile '.log'];
24+
25+
fprintf(1, '\n\nMF command for bad channel detection: \n%s\n\n', finstr);
26+
27+
if do_eval
28+
if ParType==1
29+
spmd; eval(finstr);
30+
end
31+
else
32+
eval(finstr);
33+
end
34+
end
35+
36+
% Pull out bad channels from logfile:
37+
cat_bad_cmd = sprintf('!cat %s.log | sed -n -e ''/Detected/p'' -e ''/Static/p'' | cut -f 5- -d '' '' >> %s',outfile,badfile);
38+
fprintf(1, '\n\nNow attempting:\n%s\n\n', cat_bad_cmd);
39+
fp = fopen(badfile,'w'); fprintf(fp,' '); fclose(fp);
40+
if do_eval
41+
eval(cat_bad_cmd);
42+
end
43+
end
44+
45+
46+
fprintf(1, '\nDetermining bad channels\n');
47+
tmp=dlmread(badfile,' '); % Nbuf = size(tmp,1);
48+
tmp=reshape(tmp,1,prod(size(tmp)));
49+
tmp=tmp(tmp>0); % Omit zeros (padded by dlmread):
50+
51+
% Get frequencies (number of buffers in which chan was bad):
52+
[frq,allbad] = hist(tmp,unique(tmp));
53+
54+
% Mark bad based on threshold (currently ~5% of buffers (assuming 500 buffers)):
55+
badchans = allbad(frq>0.05*Nbuf);
56+
if isempty(badchans)
57+
badstr = '';
58+
else
59+
badstr = sprintf(' -bad %s',num2str(badchans))
60+
end
61+
62+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
2+
function maxfilter_one_subject(cstr, datinfo, s, ParType)
3+
% run Maxfilter on sessions within sub-directory for one subject
4+
% cstr: structure with maxfilter commands
5+
% datinfo: data information (files, directories etc.)
6+
% s: subject index to process here
7+
% ParType: Parallelisation option
8+
% Olaf Hauk, Sep 14 (adapted from RH script)
9+
10+
% for ease of use, get info from datinfo structure
11+
data_in_dir = datinfo.data_in_dir;
12+
data_out_dir = datinfo.data_out_dir;
13+
sub_dirs = datinfo.sub_dirs;
14+
nr_subs = datinfo.nr_subs;
15+
bad_chans = datinfo.bad_chans;
16+
OverWrite = datinfo.OverWrite;
17+
movfile = datinfo.movfile;
18+
Nbuf = datinfo.Nbuf;
19+
20+
raw_files_in = datinfo.raw_files_in;
21+
raw_files_out = datinfo.raw_files_out;
22+
nr_raw_files = length(raw_files_in);
23+
24+
if ~isempty(datinfo.mvcomp_fail) % if individual movecomp failures specified
25+
mvcomp_fail = datinfo.mvcomp_fail{s};
26+
else % if no movecomp failures specified
27+
mvcomp_fail = zeros(1,length(raw_files_in));
28+
end;
29+
30+
finstr = []; % initialise final Maxfilter command string
31+
32+
% Output directory
33+
out_dir = fullfile(data_out_dir, sub_dirs{s}),
34+
35+
if datinfo.do_eval % delete/create file for movement parameters
36+
if exist(movfile)
37+
delete(movfile)
38+
end
39+
eval(sprintf('!touch %s',movfile));
40+
end
41+
42+
log_filename = [num2str(s) '_' datinfo.logfilestem];
43+
log_fid = fopen( log_filename, 'w' );
44+
45+
fprintf(log_fid, '%s\n', sub_dirs{s});
46+
47+
for r = 1:nr_raw_files
48+
49+
raw_file = fullfile(data_in_dir,sub_dirs{s},sprintf('/%s.fif',raw_files_in{r})); % Get raw FIF file
50+
51+
fprintf(log_fid, '\n%s\n', raw_file);
52+
53+
raw_stem = raw_file(1:(end-4));
54+
55+
badstr = cstr.badstr; % will be altered during loop
56+
57+
% determine sphere origin
58+
% Fit sphere (since better than MaxFilter does)
59+
if isempty(cstr.headori)
60+
if r == 1 % fit sphere doesn't change with run!
61+
incEEG = 0;
62+
% if exist(fullfile(out_dir,'fittmp.txt')); delete(fullfile(out_dir,'fittmp.txt')); end
63+
% if exist(fullfile(out_dir,sprintf('run_%02d_hpi.txt',raw_files_out{r}))); delete(fullfile(out_dir,sprintf('run_%02d_hpi.txt',raw_files_out{r}))); end
64+
% [orig,rad,fit] = meg_fit_sphere(raw_file,out_dir,sprintf('%s_hpi.txt',raw_files_out{r}),incEEG);
65+
% delete(fullfile(out_dir,'fittmp.txt'));
66+
if exist(fullfile(out_dir,sprintf('run_%02d_hpi.txt',raw_files_out{r}))); delete(fullfile(out_dir,sprintf('run_%02d_hpi.txt',raw_files_out{r}))); end
67+
cwd = pwd;
68+
[orig,rad,fit] = meg_fit_sphere(raw_file,out_dir,sprintf('%s_hpi.txt',raw_files_out{r}),incEEG);
69+
cd( cwd ); % back to original working directory
70+
end
71+
cstr.origstr = sprintf(' -origin %d %d %d -frame head',orig(1),orig(2),orig(3));
72+
else,
73+
% use pre-defined origin
74+
cstr.origstr = sprintf(' -origin %d %d %d -frame head',cstr.headori(1),cstr.headori(2),cstr.headori(3))
75+
end;
76+
77+
fprintf(1, '\nHead origin: %s\n', cstr.origstr);
78+
79+
80+
%% 1. Bad channel detection (this email says important if doing tSSS later https://www.jiscmail.ac.uk/cgi-bin/webadmin?A2=NEUROMEG;d3f363f3.1205)
81+
82+
outfile = fullfile(out_dir,sprintf('%s_bad',raw_files_out{r}));
83+
badfile = sprintf('%s.txt',outfile);
84+
bad_filename = [outfile '.fif'];
85+
Dout = dir( bad_filename );
86+
% if output file exists, get date
87+
if isempty( Dout )
88+
bad_datenum = 0;
89+
else
90+
bad_datenum = datenum( Dout.date )
91+
end
92+
93+
cstr.filestr = sprintf(' -f %s -o %s.fif',raw_file,outfile);
94+
95+
finstr_bad = [cstr.maxfstr cstr.filestr cstr.origstr cstr.basestr cstr.badstr cstr.compstr_bad];
96+
97+
% get bad channels from maxfilter
98+
bad_str_tmp = maxfilter_get_bad_channels_from_fiff(raw_file, raw_files_out{r}, badfile, out_dir, finstr_bad, Nbuf, OverWrite, ParType, datinfo.do_eval);
99+
100+
101+
102+
% Take note whether output files were successfully created or not
103+
Dout = dir( bad_filename );
104+
if isempty( Dout )
105+
bad_datenum2 = 0;
106+
else
107+
bad_datenum2 = datenum( Dout.date )
108+
end
109+
if (bad_datenum2 > bad_datenum),
110+
fprintf(log_fid, 'Created: %s\n', bad_filename);
111+
fprintf(log_fid, '%d %s\n', Dout.bytes, Dout.date);
112+
else
113+
fprintf(log_fid, 'NOT created: %s\n', bad_filename);
114+
end
115+
116+
% delete current fiff-file if desired
117+
if datinfo.del_bads && exist(bad_filename, 'file')
118+
delete( bad_filename );
119+
end
120+
121+
% add manually specified bad channels
122+
if ~isempty(bad_chans)
123+
if isempty(bad_str_tmp) && ~isempty(bad_chans{s}), % if -bad wasn't added during bad channel detection
124+
bad_str_tmp = ' -bad ';
125+
end
126+
badstr = [badstr bad_str_tmp];
127+
for bb = 1:length(bad_chans{s}),
128+
badstr = [badstr ' ' bad_chans{s}{bb} ' '];
129+
end
130+
end
131+
fprintf(1, 'Bad channels: %s\n\n', badstr);
132+
133+
%% 2. tSSS (incl. align within subject if multiple runs)
134+
135+
outfile = fullfile(out_dir,sprintf('%s', raw_files_out{r}));
136+
137+
% trans to first file in list
138+
if r == 1
139+
transfstfile = [outfile '.fif'];
140+
cstr.transtr = '';
141+
else
142+
cstr.transtr = sprintf(' -trans %s ', transfstfile);
143+
end
144+
145+
% get date of existing file
146+
Dout = dir( [outfile '.fif'] );
147+
if isempty( Dout )
148+
out_datenum = 0;
149+
else
150+
out_datenum = datenum( Dout.date )
151+
end
152+
153+
if ~exist(sprintf('%s.fif',outfile)) | OverWrite
154+
155+
cstr.filestr = sprintf(' -f %s -o %s.fif', raw_file,outfile);
156+
157+
% check if movement compensation shall be applied
158+
if mvcomp_fail, % if not
159+
finstr = [cstr.maxfstr cstr.filestr cstr.basestr badstr cstr.tSSSstr cstr.origstr cstr.transtr cstr.dsstr sprintf(' -v | tee %s.log',outfile)]
160+
fprintf(1, 'No movement compensation applied for %s\n', outfile);
161+
fprintf(log_fid, 'No movement compensation requested for %s\n', outfile);
162+
else, % if yes
163+
finstr = [cstr.maxfstr cstr.filestr cstr.basestr badstr cstr.tSSSstr cstr.compstr cstr.origstr cstr.transtr sprintf(' -v | tee %s.log',outfile)]
164+
end;
165+
166+
fprintf(1, '\nFinal Maxfilter command:\n%s\n\n', finstr);
167+
if r == 1
168+
fprintf(log_fid, '%s\n\n', finstr);
169+
end;
170+
171+
if datinfo.do_eval % only execute if desired
172+
if ParType==1
173+
spmd; eval(finstr); end
174+
else
175+
eval(finstr);
176+
end
177+
178+
eval(sprintf('!echo ''Trans 1st...'' >> %s', movfile));
179+
eval(sprintf('!cat %s.log | sed -n ''/Position change/p'' | cut -f 7- -d '' '' >> %s', outfile,movfile));
180+
end
181+
end
182+
183+
% Take note whether output files were successfully created or not
184+
% try again without movecomp first
185+
Dout = dir( [outfile '.fif'] );
186+
if isempty( Dout )
187+
out_datenum2 = 0;
188+
else
189+
out_datenum2 = datenum( Dout.date )
190+
end
191+
192+
if datinfo.do_eval % only execute if desired
193+
if (out_datenum2 == out_datenum) && OverWrite, % if date of output file has not changed when it should have...
194+
finstr = [cstr.maxfstr cstr.filestr cstr.basestr badstr cstr.tSSSstr cstr.origstr cstr.transtr cstr.dsstr sprintf(' -v | tee %s.log',outfile)]
195+
fprintf(1, 'Trying again without movement compensation for %s\n', outfile);
196+
fprintf(log_fid, 'NOT created at first attempt: %s\n', [outfile '.fif']);
197+
if ParType==1
198+
spmd; eval(finstr); end;
199+
else
200+
eval(finstr);
201+
end
202+
203+
Dout = dir( [outfile '.fif'] );
204+
if isempty( Dout )
205+
out_datenum2 = 0;
206+
else
207+
out_datenum2 = datenum( Dout.date )
208+
end
209+
if out_datenum2 > out_datenum,
210+
fprintf(log_fid, 'Created (2nd attempt): %s\n', [outfile '.fif']);
211+
fprintf(log_fid, '%d %s\n', Dout.bytes, Dout.date);
212+
else
213+
fprintf(log_fid, 'NOT created (2nd attempt): %s\n', [outfile '.fif']);
214+
end
215+
elseif (out_datenum2 > out_datenum) && (out_datenum>0)
216+
fprintf(log_fid, 'Created and overwritten: %s\n', [outfile '.fif']);
217+
elseif (out_datenum2 > out_datenum) && (out_datenum==0)
218+
fprintf(log_fid, 'Created new: %s\n', [outfile '.fif']);
219+
elseif (out_datenum > 0) && ~OverWrite,
220+
fprintf(log_fid, 'Existing not overwritten: %s\n', [outfile '.fif']);
221+
elseif (out_datenum == 0) && (out_datenum2==0),
222+
fprintf(log_fid, 'Nothing created for: %s\n', [outfile '.fif']);
223+
else
224+
fprintf(log_fid, 'Not sure what do to with: %s\n', [outfile '.fif']);
225+
end
226+
227+
end % if datinfo...
228+
229+
% if downsampling specified
230+
if ~isempty(cstr.dsstr)
231+
outfile_ds = [outfile '_ds'];
232+
cstr.filestr = sprintf(' -f %s.fif -o %s.fif', outfile, outfile_ds);
233+
234+
Dout = dir( [outfile_ds '.fif'] );
235+
if isempty( Dout )
236+
out_datenum = 0;
237+
else
238+
out_datenum = datenum( Dout.date )
239+
end
240+
241+
finstr = [cstr.maxfstr cstr.filestr cstr.dsstr sprintf(' -nosss -force -v | tee %s.log',outfile_ds)];
242+
if datinfo.do_eval
243+
fprintf(1, '\nDownsampling to: %s\n', outfile_ds);
244+
eval(finstr);
245+
else
246+
fprintf(1, '\nDownsampling command:\n %s\n', finstr);
247+
end
248+
249+
Dout = dir( [outfile_ds '.fif'] );
250+
if isempty( Dout )
251+
out_datenum2 = 0;
252+
else
253+
out_datenum2 = datenum( Dout.date )
254+
end
255+
if out_datenum2 > out_datenum,
256+
fprintf(log_fid, 'Successfully downsampled: %s\n', [outfile_ds '.fif']);
257+
fprintf(log_fid, '%d %s\n', Dout.bytes, Dout.date);
258+
else
259+
fprintf(log_fid, 'NOT downsampled: %s\n', [outfile_ds '.fif']);
260+
end
261+
end
262+
263+
end % for r...
264+
265+
fprintf(log_fid, '\n\nI''ve done everything I could!');
266+
267+
fclose( log_fid );

0 commit comments

Comments
 (0)