Skip to content

Commit

Permalink
Merge pull request #445 from Remi-Gau/remi-refactor_elapsed-time
Browse files Browse the repository at this point in the history
[REF] refactor elapsedTime and integrate it into the code base
  • Loading branch information
Remi-Gau authored Dec 22, 2021
2 parents 0c69ca0 + d3348fc commit ce7de0c
Show file tree
Hide file tree
Showing 28 changed files with 208 additions and 69 deletions.
82 changes: 49 additions & 33 deletions src/utils/elapsedTime.m
Original file line number Diff line number Diff line change
@@ -1,47 +1,63 @@
function [startTime, runTime] = elapsedTime(input, startTime, runTime, nbIteration)
%
% USAGE::
%
% [start, runTime] = elapsedTime(input, startTime, runTime, nbIteration)
%
%
% (C) Copyright 2021 CPP_SPM developers
function [startTime, runTime] = elapsedTime(opt, action, startTime, runTime, nbIteration)
%
% USAGE::
%
% [start, runTime] = elapsedTime(input, startTime, runTime, nbIteration)
%
%
% (C) Copyright 2021 CPP_SPM developers

if nargin < 3
runTime = [];
end
if nargin < 4
runTime = [];
end

switch input
switch action

case 'start'
case 'start'

startTime = tic;
startTime = tic;

case 'stop'
case 'stop'

fprintf('\n\n********* Done :) *********\n\n');
printDone(opt);

fprintf(' elapsed time is: ')
t=toc(startTime(end));
disp(datestr(datenum(0,0,0,0,0,t),'HH:MM:SS'));
t = toc(startTime(end));
msg = sprintf(' elapsed time: %s', formatDuration(t));
printToScreen(msg, opt);

runTime(end+1) = t;
ETA = mean(runTime) * (nbIteration - numel(runTime));
fprintf('\n ETA: ')
disp(datestr(datenum(0,0,0,0,0,ETA),'HH:MM:SS'));
runTime(end + 1) = t;
ETA = mean(runTime) * (nbIteration - numel(runTime));
msg = sprintf('\n ETA: %s', formatDuration(ETA));
printToScreen(msg, opt);

fprintf('\n***************************\n\n');
printHorizontalLine(opt, 27);

case 'globalStart'
case 'globalStart'

startTime = tic;
startTime = tic;

case 'globalStop'
case 'globalStop'

fprintf('\n\n********* Pipeline done :) *********\n\n');
fprintf(' global elapsed time is: ')
t=toc(startTime);
disp(datestr(datenum(0,0,0,0,0,t),'HH:MM:SS'));
fprintf('\n************************************\n\n');
printToScreen('\n\n********* Pipeline done :) *********\n', opt);

end
t = toc(opt.globalStart);
msg = sprintf(' global elapsed time: %s', formatDuration(t));
printToScreen(msg, opt);

printHorizontalLine(opt, 36);

end

end

function printDone(opt)
printToScreen('\n\n********* Done :) *********\n', opt);
end

function printHorizontalLine(opt, length)
printToScreen(['\n' repmat('*', 1, length) '\n\n'], opt);
end

function string = formatDuration(duration)
string = datestr(datenum(0, 0, 0, 0, 0, duration), 'HH:MM:SS');
end
2 changes: 1 addition & 1 deletion src/utils/printToScreen.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function printToScreen(msg, opt)
%
% (C) Copyright 2021 CPP_SPM developers

if nargin < 2 || opt.verbosity
if nargin < 2 || ~isfield(opt, 'verbosity') || opt.verbosity

fprintf(1, msg);

Expand Down
8 changes: 8 additions & 0 deletions src/workflows/bidsConcatBetaTmaps.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ function bidsConcatBetaTmaps(opt, deleteIndBeta, deleteIndTmaps)

RT = 0;

runTime = [];

for iSub = 1:numel(opt.subjects)

subjectStart = elapsedTime(opt, 'start');

subLabel = opt.subjects{iSub};

printProcessingSubject(iSub, subLabel, opt);
Expand Down Expand Up @@ -120,8 +124,12 @@ function bidsConcatBetaTmaps(opt, deleteIndBeta, deleteIndTmaps)

removeBetaImgTmaps(tMaps, deleteIndBeta, deleteIndTmaps, ffxDir);

[~, runTime] = elapsedTime(opt, 'stop', subjectStart, runTime, numel(opt.subjects));

end

cleanUpWorkflow(opt);

end

function removeBetaImgTmaps(tMaps, deleteIndBeta, deleteIndTmaps, ffxDir)
Expand Down
2 changes: 2 additions & 0 deletions src/workflows/bidsCopyInputFolder.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,6 @@ function bidsCopyInputFolder(opt, unzip)
printToScreen('\n\n', opt);
end

cleanUpWorkflow(opt);

end
3 changes: 3 additions & 0 deletions src/workflows/bidsCreateROI.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ function bidsCreateROI(opt)
end

end

end

cleanUpWorkflow(opt);

end
2 changes: 2 additions & 0 deletions src/workflows/bidsCreateVDM.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ function bidsCreateVDM(opt)

end

cleanUpWorkflow(opt);

bidsRename(opt);

end
8 changes: 8 additions & 0 deletions src/workflows/bidsFFX.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@
opt = createDefaultStatsModel(BIDS, opt);
end

runTime = [];

for iSub = 1:numel(opt.subjects)

subjectStart = elapsedTime(opt, 'start');

subLabel = opt.subjects{iSub};

printProcessingSubject(iSub, subLabel, opt);
Expand Down Expand Up @@ -88,8 +92,12 @@

end

[~, runTime] = elapsedTime(opt, 'stop', subjectStart, runTime, numel(opt.subjects));

end

cleanUpWorkflow(opt);

end

function checks(opt, action)
Expand Down
4 changes: 3 additions & 1 deletion src/workflows/bidsLesionAbnormalitiesDetection.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ function bidsLesionAbnormalitiesDetection(opt)
matlabbatch = {};
matlabbatch = setBatchLesionAbnormalitiesDetection(matlabbatch, opt, images);

saveAndRunWorkflow(matlabbatch, 'LesionAbnormalitiesDetection', opt, subLabel);
saveAndRunWorkflow(matlabbatch, 'LesionAbnormalitiesDetection', opt);

cleanUpWorkflow(opt);

end
2 changes: 2 additions & 0 deletions src/workflows/bidsLesionOverlapMap.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ function bidsLesionOverlapMap(opt)

end

cleanUpWorkflow(opt);

end
9 changes: 9 additions & 0 deletions src/workflows/bidsLesionSegmentation.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ function bidsLesionSegmentation(opt)

[BIDS, opt] = setUpWorkflow(opt, 'lesion segmentation');

runTime = [];

for iSub = 1:numel(opt.subjects)

subjectStart = elapsedTime(opt, 'start');

subLabel = opt.subjects{iSub};

printProcessingSubject(iSub, subLabel, opt);
Expand All @@ -32,9 +36,14 @@ function bidsLesionSegmentation(opt)

% copyFigures(BIDS, opt, subLabel);

[~, runTime] = elapsedTime(opt, 'stop', subjectStart, runTime, numel(opt.subjects));

end

cleanUpWorkflow(opt);

opt = setRenamingConfig(opt);

bidsRename(opt);

end
Expand Down
2 changes: 2 additions & 0 deletions src/workflows/bidsMidbrainReslice.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ function bidsMidbrainReslice(opt)

end

cleanUpWorkflow(opt);

bidsRename(opt);

end
2 changes: 2 additions & 0 deletions src/workflows/bidsRFX.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ function bidsRFX(action, opt)

end

cleanUpWorkflow(opt);

end

function checks(opt, action)
Expand Down
8 changes: 8 additions & 0 deletions src/workflows/bidsRealignReslice.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ function bidsRealignReslice(opt)

[BIDS, opt] = setUpWorkflow(opt, 'realign and reslice');

runTime = [];

for iSub = 1:numel(opt.subjects)

subjectStart = elapsedTime(opt, 'start');

subLabel = opt.subjects{iSub};

printProcessingSubject(iSub, subLabel, opt);
Expand All @@ -41,8 +45,12 @@ function bidsRealignReslice(opt)
copyFigures(BIDS, opt, subLabel);
end

[~, runTime] = elapsedTime(opt, 'stop', subjectStart, runTime, numel(opt.subjects));

end

cleanUpWorkflow(opt);

prefix = get_spm_prefix_list();
opt.query.prefix = prefix.realign;
bidsRename(opt);
Expand Down
8 changes: 8 additions & 0 deletions src/workflows/bidsRealignUnwarp.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ function bidsRealignUnwarp(opt)

[BIDS, opt] = setUpWorkflow(opt, 'realign and unwarp');

runTime = [];

for iSub = 1:numel(opt.subjects)

subjectStart = elapsedTime(opt, 'start');

subLabel = opt.subjects{iSub};

printProcessingSubject(iSub, subLabel, opt);
Expand All @@ -43,8 +47,12 @@ function bidsRealignUnwarp(opt)
copyFigures(BIDS, opt, subLabel);
end

[~, runTime] = elapsedTime(opt, 'stop', subjectStart, runTime, numel(opt.subjects));

end

cleanUpWorkflow(opt);

prefix = get_spm_prefix_list();
opt.query.prefix = prefix.unwarp;
bidsRename(opt);
Expand Down
2 changes: 2 additions & 0 deletions src/workflows/bidsRename.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ function bidsRename(opt)

end

cleanUpWorkflow(opt);

end
2 changes: 2 additions & 0 deletions src/workflows/bidsResliceTpmToFunc.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ function bidsResliceTpmToFunc(opt)

end

cleanUpWorkflow(opt);

opt = set_spm_2_bids_defaults(opt);
name_spec.entities.res = 'bold';
opt.spm_2_bids = opt.spm_2_bids.add_mapping('prefix', opt.spm_2_bids.realign, ...
Expand Down
2 changes: 2 additions & 0 deletions src/workflows/bidsResults.m
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@

end

cleanUpWorkflow(opt);

cd(currentDirectory);

end
Expand Down
8 changes: 8 additions & 0 deletions src/workflows/bidsRoiBasedGLM.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ function bidsRoiBasedGLM(opt)
opt = createDefaultStatsModel(BIDS, opt);
end

runTime = [];

for iSub = 1:numel(opt.subjects)

subjectStart = elapsedTime(opt, 'start');

subLabel = opt.subjects{iSub};

printProcessingSubject(iSub, subLabel, opt);
Expand Down Expand Up @@ -134,6 +138,10 @@ function bidsRoiBasedGLM(opt)

end

[~, runTime] = elapsedTime(opt, 'stop', subjectStart, runTime, numel(opt.subjects));

end

cleanUpWorkflow(opt);

end
2 changes: 2 additions & 0 deletions src/workflows/bidsRsHrf.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@

end

cleanUpWorkflow(opt);

end
8 changes: 8 additions & 0 deletions src/workflows/bidsSTC.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ function bidsSTC(opt)

[BIDS, opt] = setUpWorkflow(opt, 'slice timing correction');

runTime = [];

for iSub = 1:numel(opt.subjects)

subjectStart = elapsedTime(opt, 'start');

subLabel = opt.subjects{iSub};

printProcessingSubject(iSub, subLabel, opt);
Expand All @@ -44,8 +48,12 @@ function bidsSTC(opt)

saveAndRunWorkflow(matlabbatch, 'STC', opt, subLabel);

[~, runTime] = elapsedTime(opt, 'stop', subjectStart, runTime, numel(opt.subjects));

end

cleanUpWorkflow(opt);

prefix = get_spm_prefix_list;
opt.query.prefix = prefix.stc;
bidsRename(opt);
Expand Down
Loading

0 comments on commit ce7de0c

Please sign in to comment.