From b889011d8ade01fff876a38b5ded31388f01ea93 Mon Sep 17 00:00:00 2001 From: Mike Angstadt Date: Thu, 14 Jun 2012 14:00:03 -0400 Subject: [PATCH 1/3] adding script copying and logging --- matlabScripts/mc_Logger.m | 75 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 matlabScripts/mc_Logger.m diff --git a/matlabScripts/mc_Logger.m b/matlabScripts/mc_Logger.m new file mode 100644 index 00000000..783bde9f --- /dev/null +++ b/matlabScripts/mc_Logger.m @@ -0,0 +1,75 @@ +function result = mc_Logger(cmd,argument) + result = 0; + global mcLog; + global mcRoot; + switch(lower(cmd)) + case 'setup' + %create copy of template script and create log file + [st i] = dbstack('-completenames'); + callingscript = st(end).file; + [p f e ans] = fileparts(callingscript); + rightnow = now; + scriptcopy = fullfile(argument,[f '_' datestr(rightnow,'yyyy-mm-dd_HHMMSSFFF') e]); + scriptlog = fullfile(argument,[f '_' datestr(rightnow,'yyyy-mm-dd_HHMMSSFFF') '.log']); + + headertxt = ''; + lines = 0; + if (exist(fullfile(mcRoot,'.local/mc_releasetag'))) + if (exist(fullfile(mcRoot,'.local/CurrentVersionSHA'))) + %prepend both release and SHA to template copy + headertxt = sprintf('''This script was run on %s using the Methods Core release and SHA listed below''',datestr(rightnow,'mm/dd/yyyy HH:MM:SS')); + shellcommand = sprintf('echo %s | cat - %s %s %s > %s',headertxt,fullfile(mcRoot,'.local/mc_releasetag'),fullfile(mcRoot,'.local/CurrentVersionSHA'),callingscript,scriptcopy); + lines = 3; + else + %prepend just release to template copy + headertxt = sprintf('''This script was run on %s using the Methods Core release listed below''',datestr(rightnow,'mm/dd/yyyy HH:MM:SS')); + shellcommand = sprintf('echo %s | cat - %s %s > %s',headertxt,fullfile(mcRoot,'.local/mc_releasetag'),callingscript,scriptcopy); + lines = 2; + end + else + if (exist(fullfile(mcRoot,'.local/CurrentVersionSHA'))) + %prepend just SHA to template copy + headertxt = sprintf('''This script was run on %s using the Methods Core SHA listed below''',datestr(rightnow,'mm/dd/yyyy HH:MM:SS')); + shellcommand = sprintf('echo %s | cat - %s %s > %s',headertxt,fullfile(mcRoot,'.local/CurrentVersionSHA'),callingscript,scriptcopy); + lines = 2; + else + %neither release nor SHA exist, just make template copy + headertxt = sprintf('''This script was run on %s''',datestr(rightnow,'mm/dd/yyyy HH:MM:SS')); + shellcommand = sprintf('echo %s | cat - %s > %s',headertxt,callingscript,scriptcopy); + lines = 1; + end + end + [status r] = system(shellcommand); + if (status ~= 0) + mc_Error(r); + end + shellcommand = sprintf('sed -i ''%s,%s s/^/%%/'' %s',num2str(1),num2str(lines),scriptcopy); + [status r] = system(shellcommand); + if (status ~= 0) + mc_Error(r); + end + + mcLog = scriptlog; + result = 1; + case 'log' + %log argument to log file + if (isempty(mcLog)) + %no log file defined + %just return so that scripts using mc_Error but not mcLog global + %can still function without errors + return; + end + if (exist('argument')~=1 || isempty(argument)) + %function called but we didn't get anything to log + return; + end + fid = fopen(mcLog,'a'); + if (fid == -1) + %could not open file for some reason + mc_Error('Could not open logfile %s for writing.',mcLog); + end + fprintf(fid,'%s\n',argument); + result = 1; + end + +return; From cbe1859180a0486eb8398c15a2ef2a981181d8b5 Mon Sep 17 00:00:00 2001 From: Mike Angstadt Date: Thu, 14 Jun 2012 14:34:08 -0400 Subject: [PATCH 2/3] initial finalized version of mc_Logger --- matlabScripts/mc_Error.m | 9 ++++--- matlabScripts/mc_Logger.m | 51 ++++++++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/matlabScripts/mc_Error.m b/matlabScripts/mc_Error.m index 9c436b82..6933719d 100644 --- a/matlabScripts/mc_Error.m +++ b/matlabScripts/mc_Error.m @@ -1,6 +1,6 @@ function mc_Error(errorString,varargin) % A utility function to make error handling and logging more compact -% A single call to mc_Error will log the error to the global mc_LogName, +% A single call to mc_Error will log the error to the global mcLog, % bring up an error dialog box with your error message, and actually throw % an exception with the Matlab error function. % @@ -37,8 +37,11 @@ function mc_Error(errorString,varargin) errormsg = errorString; end - %add future code to call mc_Logger with loglevel Error - + global mcLog + if (~isempty(mcLog)) + %call logging function + mc_Logger('log',errormsg,1); + end errordlg(errormsg); error(errormsg); diff --git a/matlabScripts/mc_Logger.m b/matlabScripts/mc_Logger.m index 783bde9f..de83261a 100644 --- a/matlabScripts/mc_Logger.m +++ b/matlabScripts/mc_Logger.m @@ -1,4 +1,30 @@ -function result = mc_Logger(cmd,argument) +function result = mc_Logger(cmd,argument,loglevel) +% A utility function to setup script copies and log messages to a log file +% FORMAT [result] = mc_Logger(cmd,cmdstring[,loglevel]); +% +% cmd this command tells mc_Logger what mode to run in. +% Current options are: +% 'setup' - create a timestamped copy of the +% original calling script (usually the +% _mc_template script) and create a log +% file in the given directory. In this +% mode cmdstring should contain the log +% directory and loglevel is not needed. +% 'log' - write cmdstring to the log file. +% +% cmdstring in 'setup' mode this should be the log file directory +% to use. In 'log' mode this should be the string that +% will be written to the log file. +% +% loglevel This optional argument should only be used in 'log' +% mode. If given, it sets the level of importance for the +% current cmdstring. Current possible values are: +% 1 - ERROR +% 2 - WARNING +% 3 - STATUS +% If not given, it defaults to 1 (ERROR). +% + result = 0; global mcLog; global mcRoot; @@ -9,8 +35,8 @@ callingscript = st(end).file; [p f e ans] = fileparts(callingscript); rightnow = now; - scriptcopy = fullfile(argument,[f '_' datestr(rightnow,'yyyy-mm-dd_HHMMSSFFF') e]); - scriptlog = fullfile(argument,[f '_' datestr(rightnow,'yyyy-mm-dd_HHMMSSFFF') '.log']); + scriptcopy = fullfile(cmdstring,[f '_' datestr(rightnow,'yyyy-mm-dd_HHMMSSFFF') e]); + scriptlog = fullfile(cmdstring,[f '_' datestr(rightnow,'yyyy-mm-dd_HHMMSSFFF') '.log']); headertxt = ''; lines = 0; @@ -52,14 +78,27 @@ mcLog = scriptlog; result = 1; case 'log' - %log argument to log file + if (~exist(loglevel,'var') || isempty(loglevel)) + loglevel = 1; + end + switch(loglevel) + case 1 + loglevelstr = 'ERROR: '; + case 2 + loglevelstr = 'WARNING: '; + case 3 + loglevelstr = 'STATUS: '; + otherwise + loglevelstr = 'UNKNOWN: '; + end + %log cmdstring to log file if (isempty(mcLog)) %no log file defined %just return so that scripts using mc_Error but not mcLog global %can still function without errors return; end - if (exist('argument')~=1 || isempty(argument)) + if (exist('cmdstring')~=1 || isempty(cmdstring)) %function called but we didn't get anything to log return; end @@ -68,7 +107,7 @@ %could not open file for some reason mc_Error('Could not open logfile %s for writing.',mcLog); end - fprintf(fid,'%s\n',argument); + fprintf(fid,'%s\t%s\n',loglevelstr,cmdstring); result = 1; end From 8e986d333e50006a74d9f10ef106e434a312bd86 Mon Sep 17 00:00:00 2001 From: Mike Angstadt Date: Thu, 14 Jun 2012 14:36:10 -0400 Subject: [PATCH 3/3] added logging to Preprocess_mc_template, FirstLevel_mc_template, and PreprocessFirstLevel_central scripts --- FirstLevel/FirstLevel_mc_template.m | 35 +++++++++++------------ FirstLevel/PreprocessFirstLevel_central.m | 14 +++++++-- FirstLevel/Preprocess_mc_template.m | 9 +++--- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/FirstLevel/FirstLevel_mc_template.m b/FirstLevel/FirstLevel_mc_template.m index 93dbafdc..2e42b69a 100644 --- a/FirstLevel/FirstLevel_mc_template.m +++ b/FirstLevel/FirstLevel_mc_template.m @@ -3,32 +3,30 @@ %%% These options are the same between Preprocessing and First level %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%% The path to SPM on your system -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -spmpath = '/net/dysthymia/spm8'; - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% The folder that contains your subject folders %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Exp = '/net/data4/MAS/'; +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% Path where your logfiles will be stored +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +LogTemplate = '[Exp]/Logs'; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% Path where your images are located -%% -%% Variables you can use in your template are: -%% Exp = path to your experiment directory -%% iSubject = index for subject -%% Subject = name of subject from SubjDir (using iSubject as index of row) -%% iRun = index of run (listed in Column 3 of SubjDir) -%% Run = name of run from RunDir (using iRun as index of row) -%% * = wildcard (can only be placed in final part of template) -%% Examples: -%% ImageTemplate = '[Exp]/Subjects/[Subject]/func/run_0[iRun]/'; -%% ImageTemplate = '[Exp]/Subjects/[Subject]/TASK/func/[Run]/' +%%% Path where your images are located +%%% +%%% Variables you can use in your template are: +%%% Exp = path to your experiment directory +%%% iSubject = index for subject +%%% Subject = name of subject from SubjDir (using iSubject as index of row) +%%% iRun = index of run (listed in Column 3 of SubjDir) +%%% Run = name of run from RunDir (using iRun as index of row) +%%% * = wildcard (can only be placed in final part of template) +%%% Examples: +%%% ImageTemplate = '[Exp]/Subjects/[Subject]/func/run_0[iRun]/'; +%%% ImageTemplate = '[Exp]/Subjects/[Subject]/TASK/func/[Run]/' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - ImageTemplate = '[Exp]/Subjects/[Subject]/TASK/func/[Run]zz/'; % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -389,6 +387,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% end +global mcRoot; %DEVSTART mcRoot = fullfile(fileparts(mfilename('fullpath')),'..'); %DEVSTOP diff --git a/FirstLevel/PreprocessFirstLevel_central.m b/FirstLevel/PreprocessFirstLevel_central.m index 1db4ddf9..23065a17 100644 --- a/FirstLevel/PreprocessFirstLevel_central.m +++ b/FirstLevel/PreprocessFirstLevel_central.m @@ -3,7 +3,17 @@ %%% Instead make a copy of PreprocessingFirstLevel_template.m %%% and edit that to match your data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -addpath /net/dysthymia/slab/users/sripada/repos/matlabScripts %%%% this is for generate_path_CSS + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%% Code to create logfile name +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +LogDirectory = mc_GenPath(struct('Template',LogTemplate,'mode','makedir')); +result = mc_Logger('setup',LogDirectory); +if (~result) + %error with setting up logging + mc_Error('There was an error creating your logfiles.\nDo you have permission to write to %s?',LogDirectory); +end + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% General calculations that apply to both Preprocessing and First Level %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -38,7 +48,7 @@ Pwra = [nop rep stp]; Pswra = [smp nop rep stp]; -addpath(spmpath); +%addpath(spmpath); addpath(pwd); spm('defaults','fmri'); global defaults diff --git a/FirstLevel/Preprocess_mc_template.m b/FirstLevel/Preprocess_mc_template.m index 0195c64c..95233ed4 100644 --- a/FirstLevel/Preprocess_mc_template.m +++ b/FirstLevel/Preprocess_mc_template.m @@ -4,14 +4,14 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%% The path to SPM on your system +%%% The folder that contains your subject folders %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -spmpath = '/net/dysthymia/spm8'; +Exp = '/net/data4/MAS/'; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%% The folder that contains your subject folders +%%% Path where your logfiles will be stored %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -Exp = '/net/data4/MAS/'; +LogTemplate = '[Exp]/Logs'; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -203,6 +203,7 @@ donormalize = 1; dosmooth = 1; +global mcRoot; %DEVSTART mcRoot = fullfile(fileparts(mfilename('fullpath')),'..'); %DEVSTOP