Skip to content

Commit 773ee54

Browse files
committed
add function to return filename of given contrast image
1 parent bbdf3dc commit 773ee54

File tree

6 files changed

+158
-5
lines changed

6 files changed

+158
-5
lines changed
Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,40 @@
1-
function printAvailableContrasts(SPM, opt)
1+
function printAvailableContrasts(varargin)
2+
%
3+
% USAGE::
4+
%
5+
% printAvailableContrasts(SPM, opt)
6+
%
7+
% :param SPM: fullpath to SPM.mat file or content of SPM.mat file
8+
% :type SPM: structure or path
9+
%
10+
% :param opt: Options chosen.
11+
% :type opt: structure
12+
%
13+
% See also: returnContrastImageFile, getContrastNb
214
%
315

416
% (C) Copyright 2019 bidspm developers
17+
18+
defaultOpt = struct('verbosity', 2);
19+
20+
isStructOrFile = @(x) isstruct(x) || exist(x, 'file') == 2;
21+
22+
args = inputParser;
23+
24+
addRequired(args, 'SPM', isStructOrFile);
25+
addOptional(args, 'opt', defaultOpt, @isstruct);
26+
27+
parse(args, varargin{:});
28+
29+
SPM = args.Results.SPM;
30+
if ~isstruct(SPM)
31+
load(SPM, 'SPM');
32+
end
33+
34+
opt = args.Results.opt;
35+
536
printToScreen('List of contrast in this SPM file\n', opt);
637
printToScreen(createUnorderedList({SPM.xCon.name}), opt);
738
printToScreen('\n', opt);
39+
840
end

src/messages/printToScreen.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ function printToScreen(varargin)
99

1010
args = inputParser;
1111

12-
default_opt = struct('verbosity', 2);
13-
default_format = 'blue';
12+
defaultOpt = struct('verbosity', 2);
13+
defaultFormat = 'blue';
1414

1515
addRequired(args, 'msg', @ischar);
16-
addOptional(args, 'opt', default_opt, @isstruct);
17-
addParameter(args, 'format', default_format, @ischar);
16+
addOptional(args, 'opt', defaultOpt, @isstruct);
17+
addParameter(args, 'format', defaultFormat, @ischar);
1818

1919
parse(args, varargin{:});
2020

src/stats/utils/getContrastNb.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010
% contrastNb = getContrastNb(result, opt, SPM)
1111
%
1212
%
13+
% :param SPM: content of SPM.mat file
14+
% :type SPM: structure or path
15+
%
16+
% :param result: structure with at least a ``name`` field
17+
% with a chat with the name of the contrast of interest
18+
% :type result: struct
19+
%
20+
% :param opt: Options chosen.
21+
% :type opt: structure
22+
%
23+
%
24+
% See also: printAvailableContrasts, getContrastNb
1325

1426
% (C) Copyright 2019 bidspm developers
1527

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
function conImageFile = returnContrastImageFile(varargin)
2+
%
3+
% Return the contrast image file for the contrast name the user asked
4+
%
5+
% The search is regex based and any string (like 'foo') will be by default
6+
% regexified (into '^foo$').
7+
%
8+
% USAGE::
9+
%
10+
% conImageFile = returnContrastImageFile(SPM, name, opt)
11+
%
12+
% :param SPM: fullpath to SPM.mat file or content of SPM.mat file
13+
% :type SPM: structure or path
14+
%
15+
% :param name: name of the contrast of interest
16+
% :type name: char
17+
%
18+
% :param opt: Options chosen.
19+
% :type opt: structure
20+
%
21+
% See also: printAvailableContrasts, getContrastNb
22+
%
23+
24+
% (C) Copyright 2022 bidspm developers
25+
26+
defaultOpt = struct('verbosity', 2);
27+
28+
isStructOrFile = @(x) isstruct(x) || exist(x, 'file') == 2;
29+
30+
args = inputParser;
31+
32+
addRequired(args, 'SPM', isStructOrFile);
33+
addRequired(args, 'name', @ischar);
34+
addOptional(args, 'opt', defaultOpt, @istruct);
35+
36+
parse(args, varargin{:});
37+
38+
SPM = args.Results.SPM;
39+
if ~isstruct(SPM)
40+
SPM = load(SPM, 'SPM');
41+
end
42+
43+
result.name = args.Results.name;
44+
opt = args.Results.opt;
45+
46+
contrastNb = getContrastNb(result, opt, SPM);
47+
48+
conImageFile = fullfile(SPM.swd, sprintf('con_%04.0f.nii', contrastNb));
49+
50+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function test_suite = test_printAvailableContrasts %#ok<*STOUT>
2+
3+
% (C) Copyright 2022 bidspm developers
4+
try % assignment of 'localfunctions' is necessary in Matlab >= 2016
5+
test_functions = localfunctions(); %#ok<*NASGU>
6+
catch % no problem; early Matlab versions can use initTestSuite fine
7+
end
8+
initTestSuite;
9+
end
10+
11+
function test_printAvailableContrasts_basic()
12+
13+
opt = setTestCfg();
14+
15+
SPM.xCon(1, 1).name = 'foo';
16+
SPM.xCon(2, 1).name = 'bar';
17+
SPM.xCon(3, 1).name = 'foobar';
18+
19+
printAvailableContrasts(SPM, opt);
20+
21+
save(fullfile(pwd, 'SPM.mat'), 'SPM');
22+
23+
printAvailableContrasts(fullfile(pwd, 'SPM.mat'), opt);
24+
25+
printAvailableContrasts(SPM);
26+
27+
delete(fullfile(pwd, 'SPM.mat'));
28+
29+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function test_suite = test_returnContrastImageFile %#ok<*STOUT>
2+
3+
% (C) Copyright 2022 bidspm developers
4+
try % assignment of 'localfunctions' is necessary in Matlab >= 2016
5+
test_functions = localfunctions(); %#ok<*NASGU>
6+
catch % no problem; early Matlab versions can use initTestSuite fine
7+
end
8+
initTestSuite;
9+
end
10+
11+
function test_returnContrastImageFile_basic()
12+
13+
%% GIVEN
14+
15+
opt = setTestCfg();
16+
17+
SPM.swd = pwd;
18+
SPM.xCon(1, 1).name = 'foo';
19+
SPM.xCon(2, 1).name = 'bar';
20+
SPM.xCon(3, 1).name = 'foobar';
21+
22+
conImageFile = returnContrastImageFile(SPM, 'bar');
23+
assertEqual(conImageFile, fullfile(pwd, 'con_0002.nii'));
24+
25+
save(fullfile(pwd, 'SPM.mat'), 'SPM');
26+
conImageFile = returnContrastImageFile(SPM, 'foobar');
27+
assertEqual(conImageFile, fullfile(pwd, 'con_0003.nii'));
28+
delete(fullfile(pwd, 'SPM.mat'));
29+
30+
end

0 commit comments

Comments
 (0)