maxperl - Meg Wiki
CbuMeg: maxperl

=Calling Maxfilter for multiple files using Perl =

Save it where you like (I called it preproc.pl), change the permission to executable (eg: chmod 740 preproc.pl) and run it from the directory where it sits by saying 'perl preproc.pl'. I am thinking of changing it, but this is a perfectly working one, too.

y.

#!/usr/bin/perl

# a wrapper around maxfilter to get badchannels first # and then get them to work in -bad option (needed due to MF broken functionality) # Yury Shtyrov, MRC Cognition and Brain Sciences Unit Mar/2008 # yury.shtyrov@mrc-cbu.cam.ac.uk

#setting the dir where the files are and a temp directory (create one manually first), on which #we will operate

$dir = "/imaging/yury/mpv/rawdata"; #replace with your data dir $tmp_dir = "$dir/temp"; # remember to creat

#setting the list of filenames for raw data files

@fifs = qw(filename1 filename2 filename3 ...); #filenames without .fif extension

#chaning to the working dir chdir "$dir";

#then, going through each session of an individual subj

  • foreach $fifs(@fifs) {

#getting the date and time in case we want to log it (for logging, use: perl preproc.pl > logfilename) $stamp = readpipe ("date"); print "\n$stamp";

#moving files to a temp place to process print "Moving $fifs.cnt to $tmp_dir/temp.fif\n"; rename ("$dir/$fifs.fif", "$tmp_dir/temp.fif") || die "cannot mv $fifs.fif to temp dir\n";

chdir "$tmp_dir"; #starting maxfilter with -autobad 20 to detect bad channels print "Starting maxfilter on $fifs.fif to detect bad channels. Please, pray continuously.\n"; $error = eval{system '/opt/neuromag/bin/util/i686-pc-linux-gnu/maxfilter -v -f temp.fif -o temp_sss.fif -autobad 20 -skip 20 1200 > temp_logfile'}; #trying to fetch the system call output, too. $error = $error / 256; print "Maxfilter finished with code $error - whatever it means\n";

#scavenging through the log-file for a list of bad channels $badch = readpipe ("cat temp_logfile | sed -n '/Static bad channels/p' | cut -f 5- -d ' ' | uniq"); #perhaps would be more elegant to do in perl rather than calling on cat|sed|cut|uniq. i'm lazy. chomp $badch; #remove trailing new line print "Bad channels identified: $badch \n";

#calling maxfilter with badchannels identified print "Starting maxfilter to actually process $fifs.fif. Keep praying.\n"; $error = eval{system "/opt/neuromag/bin/util/i686-pc-linux-gnu/maxfilter -v -f temp.fif -o temp_sst.fif -bad $badch -st 4 > temp_logfile2"}; $error = $error / 256; print "Maxfilter finished with code $error - see $fifs\_sst\_log.txt for details\n";

#moving the original fif and the real SSS-d file to where the stuff was before, removing the temp sss-file print "Moving the original fif,the maxfiltered file and the log to the data dir. Removing the temp stuff.\n"; rename ("$tmp_dir/temp.fif", "$dir/$fifs.fif") || die "cannot mv $fifs.fif back from the temp dir\n"; rename ("$tmp_dir/temp_logfile2", "$dir/$fifs\_sst_log.txt") || warn "cannot mv temp_logfile2 from the temp dir\n"; rename ("$tmp_dir/temp_logfile", "$dir/$fifs\_badch_log.txt") || warn "cannot mv temp_logfile from the temp dir\n"; rename ("$tmp_dir/temp_sst.fif", "$dir/$fifs\_sst.fif") || die "cannot mv $fifs\_sst.fif form the temp dir\n";

unlink "$tmp_dir/temp_sss.fif" || warn "cannot remove the temp sss file\n";

CbuMeg: maxperl (last edited 2008-03-11 15:52:14 by YuryShtyrov)