-
Notifications
You must be signed in to change notification settings - Fork 44
/
hmri_quiqi_check.m
139 lines (110 loc) · 3.98 KB
/
hmri_quiqi_check.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
function hmri_quiqi_check(job)
%==========================================================================
% PURPOSE: Plot Residuals of the estimated model with respect to the Motion
% Degradation Index to evaluate the efficiency of the weighting
%
%
% METHODS: Spatial variance of the residuals of each subject is plotted
% with respect to their corresponfing MDI value
%
%_______________________________________________________________________
% Antoine Lutti
% 2021.04.01
% Neuroimaging Research Laboratory, Lausanne University Hospital &
% University of Lausanne, Lausanne, Switzerland
% Nadege Corbin
% 2021.03.30
% Centre de Resonance Magnetique des Systemes Biologiques, Bordeaux, France
%==========================================================================
hmri_log(sprintf('\t--- Evaluate residuals and MDI relationship ---'));
%% ***********************************************%%
% Get Inputs
%*************************************************%%
% SPM.mat file
spm_mat_file = job.spm_mat_file;
load(spm_mat_file{1});
% Order of the polynomial fit
pow = job.power;
%% ***********************************************%%
% Get Residuals files
%*************************************************%%
[pn fn]=fileparts(spm_mat_file{1});
ResFiles=cellstr(spm_select('FPList',pn,'^Res_'));
if length(ResFiles{1})==0
error('No residual files could be found in the folder of the SPM.mat file. Please ensure to save the image residuals when estimating the model. ')
end
%% ***********************************************%%
% Get Mask
%*************************************************%%
fileMask=cellstr(spm_select('FPList',pn,'^mask.nii'));
if length(fileMask{1})==0
error('No mask could be found in the folder of the SPM.mat file')
end
Mask=spm_read_vols(spm_vol(fileMask{1}));
MaskIndx=find(Mask~=0);
%% ***********************************************%%
% Get MDI values
%*************************************************%%
if ~(isfield(SPM,'QUIQI_MDI'))
error('There is no QUIQI_MDI field in the SPM structure')
end
MDIVals=SPM.QUIQI_MDI;
%% ***********************************************%%
% Compute variance of the residuals
%*************************************************%%
ResidVar=zeros(size(MDIVals,1),1);
for Subjctr=1:size(MDIVals,1)% reads-in individual residual maps and estimates variance
tempRes=spm_read_vols(spm_vol(ResFiles{Subjctr}));
ResidVar(Subjctr)=var(tempRes(MaskIndx),'omitnan');
end
%% ***********************************************%%
% Fit of the residuals with respect to MDI
%*************************************************%%
% ResidVar=ResidVar*1e6;% for R2s maps in ms-1 only
[~,Rsq,yfit]=myPolyFit(MDIVals,ResidVar,pow,'Free');
plotResFit(ResidVar,yfit,Rsq,40,SPM.swd)
end
function [P,Rsq,yfit]=myPolyFit(X,Y,Powers,FitMethod)
% Polynomial fitting routine
% INPUTS:
% - X: independent variable
% - Y: dependent variable
% - Powers: order of the polynomial fit
% - FitMethod: allowed values: 1. 'NonNeg' - enforces positive
% polynomial coefficients. 2. 'Free' - allows positive and negative
% polynomial coefficients.
% OUTPUTS:
% - P: polynomial coefficients
% - Rsq: R-square of the fit
% - yfit: fitted dependent variable
%
Powers=linspace(0,Powers,Powers+1);
Xmat=[];
for ctr2=1:size(Powers,2)
if Powers(ctr2)==0
Xmat=cat(2,Xmat,ones(size(X,1),1));
else
Xmat=cat(2,Xmat,X.^Powers(ctr2));
end
end
if strcmp(FitMethod,'NonNeg')
P = lsqnonneg(Xmat,Y);
elseif strcmp(FitMethod,'Free')
P=pinv(Xmat'*Xmat)*Xmat'*Y;
end
yfit =Xmat * P;
SSresid = sum((Y - yfit).^2);
SStotal = (length(Y)-1) * var(Y);
Rsq = 1 - SSresid/SStotal;
end
function plotResFit(Y,yfit,Rsq,Nbins,SavePath)
% Plots residuals against the fit of the residuals with a polynominal
% function of the MDI
figure
hold on
plot(Y,yfit,'*')
plot(Y,Y,'r','Linewidth',2)%for reference
title(['R^2 = ' num2str(round(Rsq*1e2)/1e2)])
ylabel('Residual fit');xlabel('Residuals');
saveas(gcf, fullfile(SavePath,'ResFitvsRes'), 'fig');
end