| T O P I C R E V I E W |
| E.Mansfield |
Posted - Jun 12 2009 : 09:31:58 AM Hello, I'm trying to write a script for qsub to run a MATLAB m-file. Does anyone have a sample script I can look at?
Thanks, Eric |
| 12 L A T E S T R E P L I E S (Newest First) |
| petty |
Posted - Jun 15 2009 : 4:39:43 PM FYI: this would be the correct syntax for the replacement (note the location of the single quotes) ... perl -pi -e 's|/home/esm5/experiment/CogMCI.01/|'$EXPERIMENT'|g' PPLinuxSUBJ.m |
| josh.bizzell |
Posted - Jun 15 2009 : 4:39:22 PM Eric,
Here is an example cluster script I wrote to run Matlab via the execution nodes (if you use this script, make sure to change the email address in the "#$ -M" line and remember to ONLY change lines between "# -- BEGIN USER SCRIPT -- " and "# -- END USER SCRIPT -- #").
Start script (script is called "lp_filter_tsv.sh"):
#!/bin/sh
# This is a BIAC template script for jobs on the cluster
# You have to provide the Experiment on command line
# when you submit the job the cluster.
#
# > qsub -v EXPERIMENT=Dummy.01 script.sh args
#
# There are 2 USER sections
# 1. USER DIRECTIVE: If you want mail notifications when
# your job is completed or fails you need to set the
# correct email address.
#
# 2. USER SCRIPT: Add the user script in this section.
# Within this section you can access your experiment
# folder using $EXPERIMENT. All paths are relative to this variable
# eg: $EXPERIMENT/Data $EXPERIMENT/Analysis
# By default all terminal output is routed to the " Analysis "
# folder under the Experiment directory i.e. $EXPERIMENT/Analysis
# To change this path, set the OUTDIR variable in this section
# to another location under your experiment folder
# eg: OUTDIR=$EXPERIMENT/Analysis/GridOut
# By default on successful completion the job will return 0
# If you need to set another return code, set the RETURNCODE
# variable in this section. To avoid conflict with system return
# codes, set a RETURNCODE higher than 100.
# eg: RETURNCODE=110
# Arguments to the USER SCRIPT are accessible in the usual fashion
# eg: $1 $2 $3
# The remaining sections are setup related and don't require
# modifications for most scripts. They are critical for access
# to your data
# --- BEGIN GLOBAL DIRECTIVE --
#$ -S /bin/sh
#$ -o $HOME/$JOB_NAME.$JOB_ID.out
#$ -e $HOME/$JOB_NAME.$JOB_ID.out
#$ -m ea
# -- END GLOBAL DIRECTIVE --
# -- BEGIN PRE-USER --
#Name of experiment whose data you want to access
EXPERIMENT=${EXPERIMENT:?"Experiment not provided"}
source /etc/biac_sge.sh
EXPERIMENT=`biacmount $EXPERIMENT`
EXPERIMENT=${EXPERIMENT:?"Returned NULL Experiment"}
if [ $EXPERIMENT = "ERROR" ]
then
exit 32
else
#Timestamp
echo "----JOB [$JOB_NAME.$JOB_ID] START [`date`] on HOST [$HOSTNAME]----"
# -- END PRE-USER --
# **********************************************************
# -- BEGIN USER DIRECTIVE --
# Send notifications to the following address
#$ -M Yourname@biac.duke.edu
# -- END USER DIRECTIVE --
# -- BEGIN USER SCRIPT --
# User script goes here
# Initialize input variables
SUBJ=$1
RUN=$2
CUTOFF=$3
echo "Cutoff frequency is " & $CUTOFF
# Initialize other variables
INFILE=$EXPERIMENT/Data/FSLOutput/PRESTATS/$SUBJ/$RUN.feat/filtered_func_data.nii.gz
OUTDIR=$EXPERIMENT/Analysis/FunctionalConnectivity/RestingStateConnectivity/FilteredData/$SUBJ
OUTFILE=$OUTDIR/${RUN}_filtered
TEMPFILE=$OUTDIR/${RUN}_temp.img
SCRIPTDIR=$EXPERIMENT/Scripts/FunctionalConnectivity
BIACROOT=/usr/local/packages/MATLAB/BIAC
# Make output directory if it doesn't exist
mkdir -p $OUTDIR
# Change into directory where template exists
cd $SCRIPTDIR
# Loop through template script replacing keywords
for i in 'lp_filter_script_template.m'; do
sed -e 's@INPUTFILE@'$INFILE'@g' \
-e 's@OUTPUTFILE@'$TEMPFILE'@g' \
-e 's@SCRIPTDIR@'$SCRIPTDIR'@g' \
-e 's@BIACROOT@'$BIACROOT'@g' \
-e 's@CUTOFF_FREQ@'$CUTOFF'@g' <$i> $OUTDIR/lp_filter_${RUN}.m
done
# Change to output directory and run matlab on input script
cd $OUTDIR
/usr/local/matlab2009a/bin/matlab -nodisplay < lp_filter_${RUN}.m
# Convert output data to .nii.gz and delete temp file
bxh2analyze --niigz -s $OUTDIR/${RUN}_temp.bxh $OUTFILE
rm -f $OUTDIR/${RUN}_temp.*
# -- END USER SCRIPT -- #
# **********************************************************
# -- BEGIN POST-USER --
echo "----JOB [$JOB_NAME.$JOB_ID] STOP [`date`]----"
OUTDIR=${OUTDIR:-$EXPERIMENT/Analysis}
mv $HOME/$JOB_NAME.$JOB_ID.out $OUTDIR/$JOB_NAME.$JOB_ID.out
RETURNCODE=${RETURNCODE:-0}
exit $RETURNCODE
fi
# -- END POST USER--
The way it works is that is sets a bunch of variables, and then it loops through a template Matlab file (lp_filter_script_template.m) replacing keywords (the words in the template file in all caps) with those variables and writes out the keyword replaced file. Then it runs Matlab on that new Matlab file.
Here is the template Matlab file:
% Add necessary paths
BIACroot = 'BIACROOT';
startm=fullfile(BIACroot,'startup.m');
if exist(startm,'file')
run(startm);
else
warning(sprintf(['Unable to locate central BIAC startup.m file\n (%s).\n' ...
' Connect to network or set BIACMATLABROOT environment variable.\n'],startm));
end
clear startm BIACroot
addpath('SCRIPTDIR');
% Load the images
tic
t1 = readmr('INPUTFILE');
% What is the cutoff frequency
cutoff_freq = CUTOFF_FREQ;
% Run the filter
data_lowpass_int16 = lp_filter(t1,cutoff_freq);
% Save the data
outfile = 'OUTPUTFILE';
writemr(data_lowpass_int16,outfile,{'Raw','int16'});
toc
To run the script from the head node, you'd need to do something like:
qsub -v EXPERIMENT=YourExperiment.01 lp_filter_tsv.sh 20090615_99999 run01 0.08
Hope this helps, Josh |
| petty |
Posted - Jun 15 2009 : 4:27:05 PM no. it needs to be the executed value of $EXPERIMENT ... but there is no way of knowing that before you start.
that replacement above, would need to happen in your shell script before the execution of matlab, so that the path would be set inside your matlab script. |
| E.Mansfield |
Posted - Jun 15 2009 : 4:21:02 PM I'm slightly confused. So MATLAB will recognize the ${EXPERIMENT} when SPM needs to run the PPLinuxSUBJ.m files? |
| petty |
Posted - Jun 15 2009 : 3:37:59 PM you could also do a search/replace on your PPLinux.m files (something like this):
perl -pi -e "s| /home/esm5/experiment/CogMCI.01/ | ${EXPERIMENT} |g" PPLinuxSUBJ.m |
| petty |
Posted - Jun 15 2009 : 3:31:39 PM OK, i looked through your scripts that i can find and there are a couple things.
to run the matlab script through the non-interactive nodes you need to have matlab -nodisplay < ${EXPERIMENT}/Analysis/Conn/ConnStartup.m
and within that script you cannot reference "/home/esm5/experiments/" because that doesn't exist. everytime you submit a job, your experiment that you call on submit gets mounted with a random set of letters/numbers ... you need to have your submit script add the proper path to your files.
you need to do a replace inside that ConnStartup.m with $EXPERIMENT/then/other/dirs everytime you run the script ... thats what i was doing in that perl code above. |
| E.Mansfield |
Posted - Jun 15 2009 : 2:36:34 PM MATLAB logged the following error when I ran it without the GUI and through the command window. This error did NOT occur when I ran the batch file'20090416_35934/PPLinux35934.m' through the SPM interface and the process ran to completion.
ConnStartup.m
jobs =
'20090416_35934/PPLinux35934.m'
----------------------------------------------------------------------- Running job #1 ----------------------------------------------------------------------- Running 'Named Directory Selector' Done 'Named Directory Selector' Running 'Change Directory' Change Directory: New working directory
/home/esm5/experiments/CogMCI.01/Analysis/Conn/20090416_35934/
Done 'Change Directory' Done
{??? Attempt to reference field of non-structure array. } diary off
|
| josh.bizzell |
Posted - Jun 15 2009 : 10:53:02 AM Eric,
Is anything being returned in the log file that might be helpful?
-Josh |
| E.Mansfield |
Posted - Jun 15 2009 : 10:38:03 AM I'm using a qsub script to start MATLAB and run SPM8 in batch mode. My SPM batch file first changes to the subject's directory and then begins to realign+reslice followed by coregistration. Unforunately when I submit my batch file with qsub, it only changes the directory and then stops. When I ran the same batch file in interactive mode, it ran to completion. So, I need a way to make qsub run my batch file to completion. Thanks, Eric |
| petty |
Posted - Jun 12 2009 : 6:06:48 PM nope, if you have write permissions to the folder you are in it should work.
what are you using to write files?
if its a biac tool, did you run the biac startup script within your matlab script (run /usr/local/packages/MATLAB/BIAC/startup.m)? |
| E.Mansfield |
Posted - Jun 12 2009 : 4:31:48 PM Thank you Chris. I was able to launch matlab and begin running my script. However, it didn't run the part where it writes files. Do I need to do something to grant exectutive permission to matlab?
Thanks, Eric |
| petty |
Posted - Jun 12 2009 : 10:02:37 AM hey eric, you need to set up your matlab script just like any normal matlab, then have your qsub script go to the directory and run the matlab script:
TESTDIR=${EXPERIMENT}/Analysis/mlab
cd $TESTDIR
matlab -nodisplay < test2009a.m
Another approach that i use is have my matlab script set up, inside of my shell script that i am running, then execute that.
For instance, i use perl and do something like this:
#runs recon and constructs sensitivity map
my $TDIM = 64;
my $OUTNAME = "test";
#make the the temporary path
if (!-d $TESTDIR) {
mkpath($TESTDIR, 0);
}
chdir($TESTDIR) or die "can't change directory";
#run matlab to construct the sensitivity map
$cmd = <<MLAB;
run '$BIACTDIR/startup.m';
x = 64;
y = 64;
z = 34;
t = $TDIM;
randImg = rand([x,y,z,t]);
writemr(createmrstruct(randImg),'step1/$OUTNAME.bxh',{'BXH','image','','step1/$OUTNAME.img'});
MLAB
open MLAB, "| matlab -nodisplay";
print MLAB $cmd."\n";
print MLAB "quit force\n";
close MLAB;
the above sets up the script, inside perl, then opens matlab and prints everything to matlab to be run ... then quits once the process is finished. however, i still make a qsub script that goes to the directory that contains my perl script, and executes it. |