Skip to content

Commit 07254b7

Browse files
authored
[FIX] Fix the way the voxel size is computed (#1053)
* change the way the voxel size is conputed * check voxel size before running preproc * use tmp dir
1 parent c514d1f commit 07254b7

File tree

4 files changed

+88
-17
lines changed

4 files changed

+88
-17
lines changed

bidspm.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ function preprocess(args)
278278
'dummyScans', opt.dummy_scans, ...
279279
'force', false);
280280
end
281-
281+
bidsCheckVoxelSize(opt);
282282
if opt.useFieldmaps && ~opt.anatOnly
283283
bidsCreateVDM(opt);
284284
end

src/utils/getFuncVoxelDims.m

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,9 @@
2828
return
2929
else
3030
hdr = spm_vol(fullfile(subFuncDataDir, fileName));
31-
voxDim = diag(hdr(1).mat);
32-
% Voxel dimensions are not pure integers before reslicing, therefore
33-
% round the dimensions of the functional files to the 1st decimal point
34-
voxDim = abs(voxDim(1:3)');
35-
voxDim = round(voxDim * 10) / 10;
36-
% Add it to opt.funcVoxelDims to have the same value for
37-
% all subjects and sessions
31+
M = hdr(1).mat;
32+
voxDim = sqrt(sum(M(1:3, 1:3).^2));
33+
% Add it to opt.funcVoxelDims to have the same value for all sessions
3834
opt.funcVoxelDims = voxDim;
3935
end
4036

src/workflows/bidsCheckVoxelSize.m

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
function bidsCheckVoxelSize(opt)
2+
%
3+
% Check that all file to preprocess have the same voxel size
4+
%
5+
% USAGE::
6+
%
7+
% status = bidsCheckVoxelSize(opt)
8+
%
9+
% :param opt: Options chosen for the analysis.
10+
% See also: ``checkOptions()`` and ``loadAndCheckOptions()``.
11+
% :type opt: structure
12+
%
13+
14+
% (C) Copyright 2023 bidspm developers
15+
16+
% If no normalisation is to be done,
17+
% checking that all files have the same res is not as important
18+
if ~ismember('IXI549Space', opt.space)
19+
return
20+
end
21+
22+
if ~isempty(opt.funcVoxelDims)
23+
return
24+
end
25+
26+
opt.pipeline.type = 'preproc';
27+
28+
[BIDS, opt] = getData(opt, opt.dir.preproc);
29+
30+
allFiles = {};
31+
allVoxDim = [];
32+
33+
for iSub = 1:numel(opt.subjects)
34+
35+
subLabel = opt.subjects{iSub};
36+
37+
for iTask = 1:numel(opt.taskName)
38+
39+
opt.query.task = opt.taskName{iTask};
40+
41+
filter = fileFilterForBold(opt, subLabel, 'stc');
42+
43+
files = bids.query(BIDS, 'data', filter);
44+
45+
for iFile = 1:numel(files)
46+
allFiles{end + 1, 1} = files{iFile}; %#ok<*AGROW>
47+
[subFuncDataDir, boldFilename, ext] = spm_fileparts(files{iFile});
48+
voxDim = getFuncVoxelDims(opt, subFuncDataDir, [boldFilename, ext]);
49+
allVoxDim(end + 1, :) = voxDim;
50+
end
51+
52+
end
53+
54+
end
55+
56+
differentFromFirstImageRes = (allVoxDim - allVoxDim(1, :)) > 10^-3;
57+
if any(differentFromFirstImageRes(:))
58+
msg = sprintf('%s\n', 'Not all input files have the same resolution.');
59+
for i = 1:numel(allFiles)
60+
msg = [msg, sprintf('\n%s: %0.3f, %0.3f, %0.3f', ...
61+
strrep(allFiles{i}, fullfile(BIDS.pth, filesep), ''), ...
62+
allVoxDim(i, 1), ...
63+
allVoxDim(i, 2), ...
64+
allVoxDim(i, 3))];
65+
end
66+
msg = [msg, sprintf('\n\n%s\n', ...
67+
['Use the "opt.funcVoxelDims" to force the voxel dimensions', ...
68+
'to have the same resolution after normalisation.'])];
69+
logger('ERROR', msg, ...
70+
'options', opt, ...
71+
'filename', mfilename(), ...
72+
'id', 'differentVoxelSize');
73+
end
74+
75+
end

tests/tests_batches/test_saveMatlabBatch.m

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,43 @@ function test_saveMatlabBatch_basic()
1313
subLabel = '01';
1414
opt = setOptions('dummy', subLabel);
1515
opt.dryRun = false;
16-
opt.dir.jobs = pwd;
16+
tmpDir = tempdir;
17+
spm_mkdir(tmpDir);
18+
opt.dir.jobs = tmpDir;
1719

1820
matlabbatch = struct('test', 1);
1921
saveMatlabBatch(matlabbatch, 'test', opt, subLabel);
2022

21-
expectedOutput = fullfile(pwd, ['sub-' subLabel], ...
23+
expectedOutput = fullfile(tmpDir, ['sub-' subLabel], ...
2224
['batch_test_' datestr(now, 'yyyy-mm-ddTHH-MM') '.mat']);
2325

2426
assertEqual(exist(expectedOutput, 'file'), 0);
2527
assertEqual(exist(spm_file(expectedOutput, 'ext', '.json'), 'file'), 2);
2628

27-
expectedOutput = fullfile(pwd, ['sub-' subLabel], ...
29+
expectedOutput = fullfile(tmpDir, ['sub-' subLabel], ...
2830
['batch_test_' datestr(now, 'yyyy_mm_ddTHH_MM') '.m']);
2931

3032
assertEqual(exist(expectedOutput, 'file'), 2);
3133

32-
cleanUp(fullfile(pwd, ['sub-' subLabel]));
33-
3434
end
3535

3636
function test_saveMatlabBatch_group()
3737

3838
subLabel = '01';
3939
opt = setOptions('dummy', subLabel);
4040
opt.dryRun = false;
41-
opt.dir.jobs = pwd;
41+
tmpDir = tempdir;
42+
spm_mkdir(tmpDir);
43+
opt.dir.jobs = tmpDir;
4244

4345
matlabbatch = struct('test', 1);
4446

45-
expectedOutput = fullfile(pwd, 'group', ...
47+
expectedOutput = fullfile(tmpDir, 'group', ...
4648
['batch_groupTest_' datestr(now, 'yyyy-mm-ddTHH-MM') '.mat']);
4749

4850
saveMatlabBatch(matlabbatch, 'groupTest', opt);
4951

5052
assertEqual(exist(expectedOutput, 'file'), 0);
5153
assertEqual(exist(spm_file(strrep(expectedOutput, '-', '_'), 'ext', '.m'), 'file'), 2);
5254

53-
cleanUp(fullfile(pwd, 'group'));
54-
5555
end

0 commit comments

Comments
 (0)