-
Notifications
You must be signed in to change notification settings - Fork 44
/
hmri_quiqi_build.m
153 lines (131 loc) · 4.89 KB
/
hmri_quiqi_build.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
function hmri_quiqi_build(job)
%==========================================================================
%
% PURPOSE: Compute dictionaries of covariance matrices from the motion
% degradation index.The dictionary will be used by spm_reml.m or
% spm_reml_sc.m to account for heteroscedasticity of the data.
% See " reference to the paper " for more details
%
%
% METHODS: SPM.mat created when designing the model is modified to add in
% SPM.xVi.Vi the dictionary of covariance matrices.
%_______________________________________________________________________
% 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--- Build dictionary of covariance matrices based on the MDI ---'));
%% ***********************************************%%
% Get Inputs
%*************************************************%%
% SPM.mat file
spm_mat_file = job.spm_mat_file;
load(spm_mat_file{1});
% get MDI values
MDItype = job.MDItype;
if isfield(MDItype,'MDIjsonfile')
ListFile=MDItype.MDIjsonfile.filename;
if length(ListFile)~= size (SPM.xX.X,1)
error('The number of json file is different from the number of lines in the design matrix')
end
MDIsource= MDItype.MDIjsonfile.MDIsource;
for f=1:length(ListFile)
QAdata=spm_jsonread(ListFile{f});
col=0;
if (MDIsource.PDw)
if isfield(QAdata.SDR2s,'PDw')
col=col+1;
MDIvalues(f,col)=QAdata.SDR2s.PDw;
else
error('There is no MDI value from PDw data')
end
end
if (MDIsource.T1w)
if isfield(QAdata.SDR2s,'T1w')
col=col+1;
MDIvalues(f,col)=QAdata.SDR2s.T1w;
else
error('There is no MDI value from T1w data')
end
end
if (MDIsource.MTw)
if isfield(QAdata.SDR2s,'MTw')
col=col+1;
MDIvalues(f,col)=QAdata.SDR2s.MTw;
else
error('There is no MDI value from MTw data')
end
end
end
else
MDIvalues = job.MDItype.MDImatrix.MDIvalues;
if size(MDIvalues,1)~= size (SPM.xX.X,1)
error('The number of lines in the MDI matrix is different from the number of lines in the design matrix')
end
end
% vector of powers of MDI
lambda = job.lambda;
%% ***********************************************%%
% create vectors of MDI to the power of lambda
% ************************************************%%
MatCovDict=[];
for indMDI= 1:size(MDIvalues,2)
for indLam=1:size(lambda,2)
MatCovDict=cat(2,MatCovDict,MDIvalues(:,indMDI).^lambda(indLam));
end
end
%% ***********************************************%%
% Check if a dictionary is already present.
% One will exist if a group comparison has been specified
%*************************************************%%
if isfield(SPM.xVi,'Vi')
% check if it is a group comparison
nVi=size(SPM.xVi.Vi,2);
for v=1:nVi
len(v)=length(find(diag(SPM.xVi.Vi{v})~=0));
end
if length(unique(len))>1 % This is a group comparison with different sizes for both groups
[Uni inda indc]=unique(len);
GroupIndx={};
for ctr=1:length(unique(len))
GroupIndx{ctr}=find(diag(SPM.xVi.Vi{inda(ctr)})~=0);
end
else
if unique(len)==size(SPM.xVi.Vi{1},1)/2 % This is a comparison of two groups with the same size
GroupIndx{1}=find(diag(SPM.xVi.Vi{1})~=0);
GroupIndx{2}=find(diag(SPM.xVi.Vi{end})~=0);
else % This is not a group comparison
GroupIndx{1}=find(diag(SPM.xVi.V)~=0);
end
end
else % this is not a group comparison
GroupIndx{1}=find(diag(SPM.xVi.V)~=0);
end
SPM=rmfield(SPM,'xVi');
%% ***********************************************%%
% Create dictionary of covariance matrices and
% store it in SPM.xVi.Vi
%*************************************************%%
ind=0;
for indGroup=1:size(GroupIndx,2)% separate basis function for each power of the MDI AND group
for indMat=1:size(MatCovDict,2)
ind=ind+1;
DiagTerms=zeros(length(MDIvalues),1);
DiagTerms(GroupIndx{indGroup},1)=MatCovDict(GroupIndx{indGroup},indMat);
SPM.xVi.Vi(ind)={sparse(diag(DiagTerms))};
end
end
%% ***********************************************%%
% Add MDI vaalues to the SPM structure
%*************************************************%%
SPM.QUIQI_MDI=MDIvalues;
%% ***********************************************%%
% Save the changes in SPM.mat
%*************************************************%%
[pn fn]= fileparts(spm_mat_file{1});
save(fullfile(pn,'SPM.mat'), 'SPM')
end