Skip to content

Commit

Permalink
Update export_fig to v3.15; some cosmetic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
markmikkelsen committed Jul 30, 2021
1 parent cd83a43 commit 522fabb
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 77 deletions.
21 changes: 8 additions & 13 deletions CoRegStandAlone/CoReg.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

% Coregistration of MRS voxel volumes to imaging datasets, based on headers.

MRS_struct.version.coreg = '201204';
MRS_struct.version.coreg = '210701';

warning('off'); % temporarily suppress warning messages

% First check if SPM12 is installed and on the search path
spmversion = fileparts(which('spm'));
if isempty(spmversion)
spm_version = fileparts(which('spm'));
if isempty(spm_version)
msg = 'SPM not found! Please install SPM12 and make sure it is in your search path.';
msg = hyperlink('https://www.fil.ion.ucl.ac.uk/spm/software/spm12', 'SPM12', msg);
error(msg);
elseif strcmpi(spmversion(end-3:end),'spm8')
elseif strcmpi(spm_version(end-3:end),'spm8')
msg = ['SPM8 detected. Gannet no longer supports SPM8. ' ...
'Please install SPM12 and make sure it is in your search path.'];
msg = hyperlink('https://www.fil.ion.ucl.ac.uk/spm/software/spm12', 'SPM12', msg);
error(msg);
end

if MRS_struct.ii ~= length(struc)
error('The number of nifti files does not match the number of MRS files processed by CoRegStandAlone.');
error('The number of NIfTI files does not match the number of MRS files processed by CoRegStandAlone.');
end

numscans = numel(MRS_struct.metabfile);
Expand Down Expand Up @@ -151,9 +151,7 @@
colormap('gray');
img = MRS_struct.mask.(vox{kk}).img{ii}(:);
caxis([0 mean(img(img > 0.01)) + 3*std(img(img > 0.01))]);
axis equal;
axis tight;
axis off;
axis equal tight off;
text(10, size(MRS_struct.mask.(vox{kk}).img{ii},1)/2, 'L', 'Color', [1 1 1], 'FontSize', 20);
text(size(MRS_struct.mask.(vox{kk}).img{ii},2) - 20, size(MRS_struct.mask.(vox{kk}).img{ii},1)/2, 'R', 'Color', [1 1 1], 'FontSize', 20);
set(ha,'pos',[0 0.15 1 1]);
Expand All @@ -165,15 +163,13 @@
axes('Position', [0.825, 0.05, 0.125, 0.125]);
imshow(I);
text(0.9, 0, MRS_struct.version.Gannet, 'Units', 'normalized', 'FontName', 'Arial', 'FontSize', 14, 'FontWeight', 'bold', 'HorizontalAlignment', 'left');
axis off;
axis square;
axis off square;

% Gannet documentation
axes('Position', [(1-0.9)/2, 0.025, 0.9, 0.15]);
str = 'For complete documentation, please visit: https://markmikkelsen.github.io/Gannet-docs';
text(0.5, 0, str, 'FontName', 'Arial', 'FontSize', 11, 'HorizontalAlignment', 'center');
axis off;
axis square;
axis off square;

% For Philips .data
if strcmpi(MRS_struct.p.vendor,'Philips_data')
Expand Down Expand Up @@ -207,7 +203,6 @@
end
saveas(gcf, pdfname);


end

end
Expand Down
2 changes: 1 addition & 1 deletion CoRegStandAlone/CoRegStandAlone.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

MRS_struct.version.Gannet = '3.2.0';
MRS_struct.version.load = '210624';
MRS_struct.version.load = '210730';
MRS_struct.ii = 0;
if size(metabfile,2) == 1
metabfile = metabfile';
Expand Down
64 changes: 29 additions & 35 deletions CoRegStandAlone/Seg.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@
% This is useful if only the tissue segmentation information is supposed to
% be obtained.

MRS_struct.version.segment = '201204';
MRS_struct.version.segment = '210331';
vox = MRS_struct.p.vox(1);

warning('off'); % temporarily suppress warning messages

% First check if SPM12 is installed and on the search path
spmversion = fileparts(which('spm'));
if isempty(spmversion)
spm_version = fileparts(which('spm'));
if isempty(spm_version)
msg = 'SPM not found! Please install SPM12 and make sure it is in your search path.';
msg = hyperlink('https://www.fil.ion.ucl.ac.uk/spm/software/spm12', 'SPM12', msg);
error(msg);
elseif strcmpi(spmversion(end-3:end),'spm8')
elseif strcmpi(spm_version(end-3:end),'spm8')
msg = ['SPM8 detected. Gannet no longer supports SPM8. ' ...
'Please install SPM12 and make sure it is in your search path.'];
msg = hyperlink('https://www.fil.ion.ucl.ac.uk/spm/software/spm12', 'SPM12', msg);
error(msg);
end

kk = 1;
setupSPM = 1;
setup_spm = 1;

for ii = 1:length(MRS_struct.metabfile)

% 1 - Take nifti from GannetCoRegister and segment it in SPM
% 1. Take NIfTI from GannetCoRegister and segment it in SPM

[T1dir, T1name, T1ext] = fileparts(MRS_struct.mask.(vox{kk}).T1image{ii});
struc = MRS_struct.mask.(vox{kk}).T1image{ii};
Expand All @@ -49,16 +49,16 @@
filesExist(jj) = exist(tmp{jj}, 'file'); %#ok<AGROW>
end
if ~all(filesExist)
if setupSPM
% Set up SPM for batch processing (do it once and only when needed)
if setup_spm
% Set up SPM for batch processing (do it once per batch)
spm('defaults','fmri');
spm_jobman('initcfg');
setupSPM = 0;
setup_spm = 0;
end
CallSPM12segmentation(struc);
end

% 2 - Determine GM, WM and CSF fractions for each voxel
% 2. Calculate QC metrics and GM, WM, and CSF fractions for each voxel

if strcmp(T1dir,'')
T1dir = '.';
Expand All @@ -73,40 +73,40 @@
WMvol = spm_vol(WM);
CSFvol = spm_vol(CSF);
airvol = spm_vol(air);

% Segmentation quality metrics (Chua et al. JMRI, 2009; Ganzetti et
% al. Front. Neuroinform., 2016; Esteban et al. PLOS One, 2017)
T1 = spm_vol(struc);
T1_tmp = T1.private.dat(:,:,:);

WMvol_tmp = WMvol.private.dat(:,:,:);
WMvol_tmp(WMvol_tmp < 0.9) = NaN;
WMvol_thresh = WMvol_tmp .* T1_tmp;
WMvol_thresh = WMvol_thresh(:);

GMvol_tmp = GMvol.private.dat(:,:,:);
GMvol_tmp(GMvol_tmp < 0.9) = NaN;
GMvol_thresh = GMvol_tmp .* T1_tmp;
GMvol_thresh = GMvol_thresh(:);

airvol_tmp = airvol.private.dat(:,:,:);
airvol_tmp(airvol_tmp < 0.9) = NaN;
airvol_thresh = airvol_tmp .* T1_tmp;
airvol_thresh = airvol_thresh(:);

MRS_struct.out.tissue.CV_WM(ii) = std(WMvol_thresh,'omitnan') / mean(WMvol_thresh,'omitnan');
MRS_struct.out.tissue.CV_GM(ii) = std(GMvol_thresh,'omitnan') / mean(GMvol_thresh,'omitnan');
MRS_struct.out.tissue.CJV(ii) = (std(WMvol_thresh,'omitnan') + std(GMvol_thresh,'omitnan')) ...
/ abs(mean(WMvol_thresh,'omitnan') - mean(GMvol_thresh,'omitnan'));
MRS_struct.out.tissue.CNR(ii) = abs(mean(WMvol_thresh,'omitnan') - mean(GMvol_thresh,'omitnan')) / ...
sqrt(var(airvol_thresh,'omitnan') + var(WMvol_thresh,'omitnan') + var(GMvol_thresh,'omitnan'));
MRS_struct.out.tissue.CV_WM(ii) = std(WMvol_thresh, 'omitnan') / mean(WMvol_thresh, 'omitnan');
MRS_struct.out.tissue.CV_GM(ii) = std(GMvol_thresh, 'omitnan') / mean(GMvol_thresh, 'omitnan');
MRS_struct.out.tissue.CJV(ii) = (std(WMvol_thresh, 'omitnan') + std(GMvol_thresh, 'omitnan')) ...
/ abs(mean(WMvol_thresh, 'omitnan') - mean(GMvol_thresh, 'omitnan'));
MRS_struct.out.tissue.CNR(ii) = abs(mean(WMvol_thresh, 'omitnan') - mean(GMvol_thresh, 'omitnan')) / ...
sqrt(var(airvol_thresh, 'omitnan') + var(WMvol_thresh, 'omitnan') + var(GMvol_thresh, 'omitnan'));

T1_tmp = T1_tmp(:);
n_vox = numel(T1_tmp);
efc_max = n_vox * (1/sqrt(n_vox)) * log(1/sqrt(n_vox));
b_max = sqrt(sum(T1_tmp.^2));
MRS_struct.out.tissue.EFC(ii) = (1/efc_max) .* sum((T1_tmp ./ b_max) .* log((T1_tmp + eps) ./ b_max));

% Loop over voxels if PRIAM
for kk = 1:length(vox)

Expand All @@ -115,7 +115,7 @@

% GM
O_GMvox.fname = fullfile(a, [b '_GM' c]);
O_GMvox.descrip = 'GMmasked_MRS_Voxel_Mask';
O_GMvox.descrip = 'MRS_voxel_mask_GM';
O_GMvox.dim = voxmaskvol.dim;
O_GMvox.dt = voxmaskvol.dt;
O_GMvox.mat = voxmaskvol.mat;
Expand All @@ -124,7 +124,7 @@

% WM
O_WMvox.fname = fullfile(a, [b '_WM' c]);
O_WMvox.descrip = 'WMmasked_MRS_Voxel_Mask';
O_WMvox.descrip = 'MRS_voxel_mask_WM';
O_WMvox.dim = voxmaskvol.dim;
O_WMvox.dt = voxmaskvol.dt;
O_WMvox.mat = voxmaskvol.mat;
Expand All @@ -133,14 +133,14 @@

% CSF
O_CSFvox.fname = fullfile(a, [b '_CSF' c]);
O_CSFvox.descrip = 'CSFmasked_MRS_Voxel_Mask';
O_CSFvox.descrip = 'MRS_voxel_mask_CSF';
O_CSFvox.dim = voxmaskvol.dim;
O_CSFvox.dt = voxmaskvol.dt;
O_CSFvox.mat = voxmaskvol.mat;
CSF_voxmask_vol = CSFvol.private.dat(:,:,:) .* voxmaskvol.private.dat(:,:,:);
O_CSFvox = spm_write_vol(O_CSFvox, CSF_voxmask_vol);

% 3 - Calculate a CSF-corrected i.u. value and output it to the structure
% 3. Calculate a CSF-corrected i.u. value and output it to the structure

GMsum = sum(sum(sum(O_GMvox.private.dat(:,:,:))));
WMsum = sum(sum(sum(O_WMvox.private.dat(:,:,:))));
Expand All @@ -154,7 +154,7 @@
MRS_struct.out.(vox{kk}).tissue.fWM(ii) = fWM;
MRS_struct.out.(vox{kk}).tissue.fCSF(ii) = fCSF;

% 4 - Build output
% 4. Build output

if ishandle(104)
clf(104);
Expand Down Expand Up @@ -296,8 +296,6 @@

warning('on'); % turn warnings back on

end


function img_montage = PlotSegmentedVoxels(struc, voxoff, voxmaskvol, O_GMvox, O_WMvox, O_CSFvox)

Expand Down Expand Up @@ -368,16 +366,12 @@

ha = subplot(2,3,1:3);
imagesc(img_montage);
axis equal;
axis tight;
axis off;
axis equal tight off;
text(floor(size(mask_t,2)/2), 20, 'Voxel', 'Color', [1 1 1], 'FontSize', 20, 'HorizontalAlignment', 'center');
text(floor(size(mask_t,2)) + floor(size(mask_t,2)/2), 20, 'GM', 'Color', [1 1 1], 'FontSize', 20, 'HorizontalAlignment', 'center');
text(2*floor(size(mask_t,2)) + floor(size(mask_t,2)/2), 20, 'WM', 'Color', [1 1 1], 'FontSize', 20, 'HorizontalAlignment', 'center');
text(3*floor(size(mask_t,2)) + floor(size(mask_t,2)/2), 20, 'CSF', 'Color', [1 1 1], 'FontSize', 20, 'HorizontalAlignment', 'center');
set(ha,'pos',[0 0.17 1 1]);

end
set(ha, 'pos', [0 0.17 1 1]);



2 changes: 1 addition & 1 deletion GannetCoRegister.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
end

if MRS_struct.ii ~= length(struc)
error('The number of nifti files does not match the number of MRS files processed by GannetLoad.');
error('The number of NIfTI files does not match the number of MRS files processed by GannetLoad.');
end

run_count = 0;
Expand Down
2 changes: 1 addition & 1 deletion GannetMask_Philips.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
lr_ang = MRS_struct.p.voxang(ii,1);
cc_ang = MRS_struct.p.voxang(ii,3);

% We need to flip ap and lr axes to match NIFTI convention
% We need to flip ap and lr axes to match NIfTI convention
ap_off = -ap_off;
lr_off = -lr_off;
ap_ang = -ap_ang;
Expand Down
4 changes: 2 additions & 2 deletions GannetMask_SiemensRDA.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
ii = 1;
end

% Parse RDA filename and establish nifti voxelmask filename
% Parse RDA filename and establish NIfTI voxelmask filename
[path, name] = fileparts(fname);
fidoutmask = fullfile(path,[name '_mask.nii']);
fid = fopen(fname);
Expand Down Expand Up @@ -168,7 +168,7 @@
halfpixshift(3) = -halfpixshift(3);
XYZ = XYZ + repmat(halfpixshift, [1 size(XYZ,2)]);

% We need to flip ap and lr axes to match NIFTI convention
% We need to flip ap and lr axes to match NIfTI convention
VoxOffs(1) = -VoxOffs(1);
VoxOffs(2) = -VoxOffs(2);

Expand Down
2 changes: 1 addition & 1 deletion GannetMask_SiemensTWIX.m
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
halfpixshift(3) = -halfpixshift(3);
XYZ = XYZ + repmat(halfpixshift, [1 size(XYZ,2)]);

% We need to flip ap and lr axes to match NIFTI convention
% We need to flip ap and lr axes to match NIfTI convention
VoxOffs(1) = -VoxOffs(1);
VoxOffs(2) = -VoxOffs(2);

Expand Down
26 changes: 13 additions & 13 deletions GannetSegment.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

for ii = 1:MRS_struct.p.numScans

% 1. Take nifti from GannetCoRegister and segment it in SPM
% 1. Take NIfTI from GannetCoRegister and segment it in SPM

[T1dir, T1name, T1ext] = fileparts(MRS_struct.mask.(vox{kk}).T1image{ii});
struc = MRS_struct.mask.(vox{kk}).T1image{ii};
Expand Down Expand Up @@ -211,19 +211,19 @@
text_pos = 1;

if strcmp(MRS_struct.p.vendor,'Siemens_rda')
[~,filesExist,tmp3] = fileparts(MRS_struct.metabfile{1,ii*2-1});
[~,tmp2,tmp3] = fileparts(MRS_struct.metabfile{1,ii*2-1});
else
[~,filesExist,tmp3] = fileparts(MRS_struct.metabfile{1,ii});
[~,tmp2,tmp3] = fileparts(MRS_struct.metabfile{1,ii});
end
fname = [filesExist tmp3];
fname = [tmp2 tmp3];
if length(fname) > 30
fname = [fname(1:12) '...' fname(end-11:end)];
end
text(0.5, text_pos-0.12, 'Filename: ', 'FontName', 'Arial', 'HorizontalAlignment','right', 'VerticalAlignment', 'top', 'FontSize', 13);
text(0.5, text_pos-0.12, [' ' fname], 'FontName', 'Arial', 'VerticalAlignment', 'top', 'FontSize', 13, 'Interpreter', 'none');

[~,filesExist,tmp3] = fileparts(MRS_struct.mask.(vox{kk}).T1image{ii});
T1image = [filesExist tmp3];
[~,tmp2,tmp3] = fileparts(MRS_struct.mask.(vox{kk}).T1image{ii});
T1image = [tmp2 tmp3];
if length(T1image) > 30
T1image = [T1image(1:12) '...' T1image(end-11:end)];
end
Expand All @@ -247,16 +247,16 @@
switch target{jj}
case 'GABA'
tmp1 = 'GABA+/Water (CSF-corrected): ';
filesExist = sprintf(' %.2f i.u.', MRS_struct.out.(vox{kk}).GABA.ConcIU_CSFcorr(ii));
tmp2 = sprintf(' %.2f i.u.', MRS_struct.out.(vox{kk}).GABA.ConcIU_CSFcorr(ii));
case 'Lac'
tmp1 = 'Lac+MM/Water (CSF-corrected): ';
filesExist = sprintf(' %.2f i.u.', MRS_struct.out.(vox{kk}).Lac.ConcIU_CSFcorr(ii));
tmp2 = sprintf(' %.2f i.u.', MRS_struct.out.(vox{kk}).Lac.ConcIU_CSFcorr(ii));
case {'Glx','GSH','EtOH'}
tmp1 = [target{jj} '/Water (CSF-corrected): '];
filesExist = sprintf(' %.2f i.u.', MRS_struct.out.(vox{kk}).(target{jj}).ConcIU_CSFcorr(ii));
tmp2 = sprintf(' %.2f i.u.', MRS_struct.out.(vox{kk}).(target{jj}).ConcIU_CSFcorr(ii));
end
text(0.5, text_pos, tmp1, 'FontName', 'Arial', 'HorizontalAlignment','right', 'VerticalAlignment', 'top', 'FontSize', 13);
text(0.5, text_pos, filesExist, 'FontName', 'Arial', 'VerticalAlignment', 'top', 'FontSize', 13);
text(0.5, text_pos, tmp2, 'FontName', 'Arial', 'VerticalAlignment', 'top', 'FontSize', 13);
end
end

Expand Down Expand Up @@ -286,11 +286,11 @@
MRS_struct.mask.(vox{kk}).img_montage{ii} = PlotSegmentedVoxels(struc, voxoff, voxmaskvol, O_GMvox, O_WMvox, O_CSFvox);

if strcmp(MRS_struct.p.vendor, 'Siemens_rda')
[~,tmp,filesExist] = fileparts(MRS_struct.metabfile{1,ii*2-1});
[~,tmp,tmp2] = fileparts(MRS_struct.metabfile{1,ii*2-1});
else
[~,tmp,filesExist] = fileparts(MRS_struct.metabfile{1,ii});
[~,tmp,tmp2] = fileparts(MRS_struct.metabfile{1,ii});
end
fname = [tmp filesExist];
fname = [tmp tmp2];
if length(fname) > 30
fname = [fname(1:12) '...' fname(end-11:end)];
end
Expand Down
2 changes: 1 addition & 1 deletion GannetVersion.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
str = char(str(:)');
expression = '(?<field>MRS_struct.version.Gannet = )''(?<version>.*?)''';
out = regexp(str, expression, 'names');
fprintf('\n %s\n\n', out.version);
fprintf('\nYour version of Gannet is %s\n\n', out.version);
Loading

0 comments on commit 522fabb

Please sign in to comment.