Skip to content

Commit

Permalink
Bugfix: Running CAT12 and SimNIBS with files without sform/qform
Browse files Browse the repository at this point in the history
  • Loading branch information
ftadel committed Feb 22, 2023
1 parent a270ac0 commit 672f30b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
43 changes: 43 additions & 0 deletions toolbox/anatomy/mri_add_world.m
@@ -0,0 +1,43 @@
function sMri = mri_add_world(MriFile, sMri)
% MRI_ADD_WORLD: Add a default "world" transformation to a MRI.

% @=============================================================================
% This function is part of the Brainstorm software:
% https://neuroimage.usc.edu/brainstorm
%
% Copyright (c) University of Southern California & McGill University
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPLv3
% license can be found at http://www.gnu.org/copyleft/gpl.html.
%
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE
% UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
% WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
% LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
%
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Francois Tadel, 2023

% If MRI is not loaded yet
if (nargin < 2) || isempty(sMri)
sMri = in_mri_bst(MriFile);
end

% A vox2ras matrix must be present in the MRI for running CAT12
if ~isfield(sMri, 'InitTransf') || isempty(sMri.InitTransf) || ~any(strcmpi(sMri.InitTransf(:,1), 'vox2ras'))
disp('BST> Adding default world transformation to MRI...');
% Add vox2ras
sMri.InitTransf = {'vox2ras', ...
[1, 0, 0, -size(sMri.Cube,1) / 2 .* sMri.Voxsize(1); ...
0, 1, 0, -size(sMri.Cube,2) / 2 .* sMri.Voxsize(2); ...
0, 0, 1, -size(sMri.Cube,3) / 2 .* sMri.Voxsize(3); ...
0, 0, 0, 1]};
% Remove existing NIFTI header because it would cause incompatibilities
sMri.Header = [];
% Save modification
bst_save(file_fullpath(MriFile), sMri, 'v7');
end

2 changes: 2 additions & 0 deletions toolbox/io/import_anatomy_cat_2020.m
Expand Up @@ -262,6 +262,8 @@
return;
end
end
% Load file again, to get the new fiducials
sMri = in_mri_bst(BstT1File);


%% ===== MNI NORMALIZATION =====
Expand Down
9 changes: 9 additions & 0 deletions toolbox/process/functions/process_fem_mesh.m
Expand Up @@ -260,6 +260,8 @@
errMsg = 'Invalid neck MNI Z coordinate (must be negative or zero).';
return;
end
% A vox2ras matrix must be present in the MRI
sMriT1 = mri_add_world(T1File, sMriT1);
end

% ===== LOAD/CUT T2 =====
Expand All @@ -279,6 +281,8 @@
sMriT2 = sMriT2tmp;
end
end
% A vox2ras matrix must be present in the MRI
sMriT2 = mri_add_world(T2File, sMriT2);
end
FemFile = [];

Expand Down Expand Up @@ -750,12 +754,17 @@
% Create temporary folder for segmentation files
simnibsDir = bst_fullfile(bst_get('BrainstormTmpDir'), simCmd);
mkdir(simnibsDir);
% Remove previous nifti header, as it can cause issues with SimNIBS (if sform is defined and not qform)
sMriT1.Header = [];
% Save T1 MRI in .nii format
subjid = strrep(sSubject.Name, '@', '');
T1Nii = bst_fullfile(simnibsDir, [subjid 'T1.nii']);
out_mri_nii(sMriT1, T1Nii);
% Save T2 MRI in .nii format
if ~isempty(T2File)
% Remove previous nifti header, as it can cause issues with SimNIBS (if sform is defined and not qform)
sMriT2.Header = [];
% Save file
T2Nii = bst_fullfile(simnibsDir, [subjid 'T2.nii']);
out_mri_nii(sMriT2, T2Nii);
else
Expand Down
4 changes: 3 additions & 1 deletion toolbox/process/functions/process_segment_cat12.m
Expand Up @@ -23,7 +23,7 @@
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Francois Tadel, 2019-2021
% Authors: Francois Tadel, 2019-2023

eval(macro_method);
end
Expand Down Expand Up @@ -242,6 +242,8 @@
'Missing fiducials: the surfaces cannot be aligned with the MRI.'];
end
end
% A vox2ras matrix must be present in the MRI for running CAT12
sMri = mri_add_world(T1FileBst, sMri);

% ===== SAVE MRI AS NII =====
bst_progress('text', 'Saving temporary files...');
Expand Down

0 comments on commit 672f30b

Please sign in to comment.