Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/reports/copyGraphWindownOutput.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,36 @@
action = '';
end

figureDir = fullfile(opt.derivativesDir, ['sub-' subID], 'figures');
figureDir = fullfile(opt.derivativesDir, strcat('sub-', subID), 'figures');
if ~exist(figureDir, 'dir')
mkdir(figureDir);
end

for iFile = imgNb

% Using validationInputFile might be too agressive as it throws an error if
% it can't find a file. Let's use a work around and stick to warnings for now

% file = validationInputFile(pwd, sprintf('spm_.*%i.png', iFile));
file = spm_select('FPList', pwd, sprintf('^spm_.*%i.png$', iFile));

if ~isempty(file)
if isempty(file)

warning( ...
'copyGraphWindownOutput:noFile', ...
'No figure file to copy');

elseif size(file, 1) > 1

warning( ...
'copyGraphWindownOutput:tooManyFiles', ...
sprintf('\n %s\n %s\n %s', ...
'Too many figure files to copy.', ...
'Not sure what to do.', ...
'Will skip this step.'));
disp(file);

else

targetFile = sprintf( ...
'%s_%i_sub-%s_task-%s_%s.png', ...
Expand All @@ -55,6 +75,10 @@
file, ...
fullfile(figureDir, targetFile));

fprintf(1, '\n%s\nwas moved to\n%s\n', ...
file, ...
fullfile(figureDir, targetFile));

end

end
Expand Down
52 changes: 33 additions & 19 deletions src/utils/validationInputFile.m
Original file line number Diff line number Diff line change
@@ -1,45 +1,59 @@
% (C) Copyright 2019 CPP BIDS SPM-pipeline developers

function files = validationInputFile(dir, fileName, prefix)
function files = validationInputFile(dir, fileNamePattern, prefix)
%
% Short description of what the function does goes here.
% Looks for file name pattern in a given directory and returns all the files
% that match that pattern but throws an error if it cannot find any.
%
% A prefix can be added to the filename.
%
% This function is mostly used that a file exists so
% that an error is thrown early when building a SPM job rather than at run
% time.
%
% USAGE::
%
% [argout1, argout2] = templateFunction(argin1, [argin2 == default,] [argin3])
% files = validationInputFile(dir, fileName[, prefix])
%
% :param argin1: (dimension) obligatory argument. Lorem ipsum dolor sit amet,
% consectetur adipiscing elit. Ut congue nec est ac lacinia.
% :type argin1: type
% :param argin2: optional argument and its default value. And some of the
% options can be shown in litteral like ``this`` or ``that``.
% :type argin2: string
% :param argin3: (dimension) optional argument
% :param dir: Directory where the search will be conducted.
% :type dir: string
% :param fileName: file name pattern. Can be a regular expression except for
% the starting ``^`` and ending ``$``. For example:
% ``'sub-.*_ses-.*_task-.*_bold.nii'``.
% :type fileName: string
% :param prefix: prefix to be added to the filename pattern. This can also be
% a regular expression (ish). For example ,f looking for the files that
% start with ``c1`` or ``c2`` or ``c3``, the prefix can be
% ``c[123]``.
% :type prefix: string
%
% :returns: - :argout1: (type) (dimension)
% - :argout2: (type) (dimension)
% :returns:
%
% file = validationInputFile(dir, fileName, prefix)
% :files: (string array) returns the fullpath file list of all the
% files matching the required pattern.
%
%
% See also: ``spm_select``.
%
% Checks if files exist. A prefix can be added. The prefix allows for the
% use of regular expression.
%
% TPMs = validationInputFile(anatDataDir, anatImage, 'c[12]');
% Example:
% %
% % tissueProbaMaps = validationInputFile(anatDataDir, anatImage, 'c[12]');
%
% If the filet(s) exist(s), it returns a char array containing list of fullpath.
%

if nargin < 3
prefix = '';
end

files = spm_select('FPList', dir, ['^' prefix fileName '$']);
files = spm_select('FPList', dir, ['^' prefix fileNamePattern '$']);

if isempty(files)

errorStruct.identifier = 'validationInputFile:nonExistentFile';
errorStruct.message = sprintf( ...
'This file does not exist: %s', ...
fullfile(dir, [prefix fileName '[.gz]']));
fullfile(dir, [prefix fileNamePattern '[.gz]']));
error(errorStruct);

end
Expand Down
41 changes: 41 additions & 0 deletions tests/test_copyGraphWindownOutput.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

function test_copyGraphWindownOutputBasic()

delete('*.png');

pause(1);

opt.derivativesDir = pwd;
opt.taskName = 'testTask';
subID = '01';
Expand All @@ -30,6 +34,43 @@ function test_copyGraphWindownOutputBasic()
assertEqual(size(files, 1), 2);

pause(1);

if isOctave()
confirm_recursive_rmdir (true, 'local');
end
rmdir(fullfile(opt.derivativesDir, ['sub-' subID]), 's');

end

function test_copyGraphWindownOutputWarning()

delete('*.png');

pause(1);

opt.derivativesDir = pwd;
opt.taskName = 'testTask';
subID = '01';
action = 'testStep';

system('touch spm_002.png');
system('touch spm_test_002.png');

copyGraphWindownOutput(opt, subID, action, 2);

if ~isOctave()
assertWarning( ...
@()copyGraphWindownOutput(opt, subID, action, 2), ...
'copyGraphWindownOutput:tooManyFiles');

assertWarning( ...
@()copyGraphWindownOutput(opt, subID, action, 3), ...
'copyGraphWindownOutput:noFile');
end

if isOctave()
confirm_recursive_rmdir (true, 'local');
end
rmdir(fullfile(opt.derivativesDir, ['sub-' subID]), 's');

end