-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQTI.m
217 lines (195 loc) · 8.79 KB
/
QTI.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
classdef QTI < LogLinear
% Authors: Ben Jeurissen (ben.jeurissen@uantwerpen.be), Jan Morez (jan.morez@uantwerpen.be)
%
% Basic usage:
%
% model = QTI(grad);
% y = Volumes.mask(y,mask);
% x = model.solve(y);
% m = model.metrics(x);
%
% with
%
% grad: n_w × 5 (gradient direction + b-value + b-delta, preferrably expressed in ms/um^2)
% y: n_x × n_y × n_z × n_w (weighted image series)
% mask : n_x × n_y × n_z (boolean processing mask)
% x: n_x × n_y × n_z × 28 (model parameters)
% m: struct with scalar metrics
%
%
% Copyright (c) 2020 University of Antwerp
%
% Permission is hereby granted, free of charge, to any non-commercial
% entity ('Recipient') obtaining a copy of this software and associated
% documentation files (the 'Software'), to the Software solely for
% non-commercial research, including the rights to use, copy and modify
% the Software, subject to the following conditions:
%
% 1. The above copyright notice and this permission notice shall be
% included by the Recipient in all copies or substantial portions of the
% Software.
%
% 2. The Software shall not be distributed to any third parties
% without written approval of the authors.
%
% 3. The Software is provided 'as is', without warranty of any kind,
% express or implied, including but not limited to the warranties of
% merchantability, fitness for a particular purpose and noninfringement.
% In no event shall the authors or copyright holders be liable for any
% claim, damages or other liability, whether in an action of contract,
% tort or otherwise, arising from, out of or in connection with the
% Software or the use or other dealings in the Software.
%
% 4. The Software may only be used for non-commercial research and may
% not be used for clinical care.
%
% 5. Prior to publication of research involving the Software, the
% Recipient shall inform the Authors listed above.
%
methods (Access = public, Static = false)
function obj = QTI(grad, varargin)
fprintf(1, 'Setting up QTI model...\n');
% parse QTI specifc options
p = inputParser;
p.KeepUnmatched = true;
p.addOptional('constr', [0 0 1 1 1]);
p.addOptional('constr_dirs', 100);
p.addOptional('rank_fix', 'eq_constr', @(input) strcmp(input,'eq_constr') | strcmp(input,'ste_offset'));
p.parse(varargin{:});
% set up problem matrix
grad = double(grad);
grad(:, 1:3) = bsxfun(@rdivide, grad(:, 1:3), sqrt(sum(grad(:, 1:3).^2, 2))); grad(isnan(grad)) = 0;
A = [ones([size(grad, 1) 1], class(grad)) -Tensor.tpars_to_1x6(grad(:,4), grad(:,5), grad(:,1:3)) 0.5*Tensor.t_1x6_to_1x21(Tensor.tpars_to_1x6(grad(:,4), grad(:,5), grad(:,1:3)))];
Aeq = []; beq = [];
if rank(A) < 28
if rank(A) == 23
switch p.Results.rank_fix
case 'eq_constr'
disp('WARNING: Using equality constraints to deal with rank deficiency of A in exp(A*x). Expected when fitting to LTE+STE only data.')
Aeq = zeros(5,28);
Aeq(1,12) = 1;
Aeq(2,13) = 1;
Aeq(3,14) = 1;
Aeq(4,15) = 1;
Aeq(5,16) = 1;
beq = zeros(5,1);
case 'ste_offset'
disp('WARNING: Adding small offset to b_delta of STE samples to increase rank of A in exp(A*x). Expected when fitting to LTE+STE only data.')
my_eps = 1e-4;
b_delta = grad(:,5); b_delta(b_delta >= 0 & b_delta < my_eps) = my_eps; b_delta(b_delta <= 0 & b_delta > -my_eps) = -my_eps;
A = [ones([size(grad, 1) 1], class(grad)) -Tensor.tpars_to_1x6(grad(:,4), grad(:,5), grad(:,1:3)) 0.5*Tensor.t_1x6_to_1x21(Tensor.tpars_to_1x6(grad(:,4), b_delta, grad(:,1:3)))];
if rank(A) < 28
error('A in exp(A*x) is not full rank. Make sure your acquisiton contains multiple b-values and b-tensor shapes.');
end
otherwise
end
else
error('A in exp(A*x) is not full rank. Make sure your acquisiton contains multiple b-values and b-tensor shapes.');
end
end
% set up constraint matrix
constr = p.Results.constr;
n = p.Results.constr_dirs;
Aneq = [];
if exist('constr', 'var') && any(constr)
dirs = Directions.get(n);
c1 = Tensor.tpars_to_1x6(ones(n,1), ones(n,1), dirs); % linear
c2 = Tensor.t_1x6_to_1x21(c1);
E_bulk = Tensor.iso_1x21();
if constr(1)
disp('Constraining to non-negative diffusivity')
Aneq = [Aneq; -[zeros(n, 1) c1 zeros(n, 21)]];
end
if constr(2)
disp('Constraining to non-negative total kurtosis')
Aneq = [Aneq; -[zeros(n, 7) c2]];
end
if constr(3)
disp('Constraining to non-negative isotropic kurtosis')
Aneq = [Aneq; -[zeros(1, 7) E_bulk]];
end
if constr(4)
disp('Constraining to non-negative anisotropic kurtosis')
Aneq = [Aneq; -[zeros(n, 7) c2-E_bulk]];
end
if constr(5)
disp('Constraining to monotonic signal decay')
Aneq = [Aneq; [zeros(n, 1) -c1 max(grad(:, 4))*c2]];
end
end
% set up constraint vector
bneq = [];
if size(Aneq, 1) > 0
bneq = zeros(size(Aneq, 1), 1);
end
% set up generic y = exp(A*x) problem
obj = obj@LogLinear(A,Aneq,bneq,Aeq,beq,varargin{:});
end
end
methods (Access = public, Static = true)
function metrics = metrics(x)
if ndims(x) ~= 2; [x, mask] = Volumes.vec(x); end %#ok<ISMAT>
fprintf(1, 'Calculating QTI metrics ...\n');
dt_1x6 = x(2:7,:)';
B0 = exp(x(1,:));
FA = Tensor.fa(dt_1x6);
MD = Tensor.md(dt_1x6);
L = Tensor.eigval(dt_1x6);
AD = real(L(:,1));
RD = mean(real(L(:,2:3)),2);
dt2_1x21 = Tensor.t_1x6_to_1x21(dt_1x6);
ct_1x21 = x(8:28,:)';
[E_bulk, E_shear, E_iso] = Tensor.iso_1x21();
V_MD2 = Tensor.inner(dt2_1x21, E_bulk);
V_shear2 = Tensor.inner(dt2_1x21, E_shear);
V_iso2 = Tensor.inner(dt2_1x21, E_iso);
V_MD = Tensor.inner(ct_1x21 , E_bulk);
V_shear = Tensor.inner(ct_1x21 , E_shear);
V_iso = Tensor.inner(ct_1x21 , E_iso);
V_MD1 = V_MD + V_MD2;
V_shear1 = V_shear + V_shear2;
V_iso1 = V_iso + V_iso2;
C_MD = V_MD ./ V_MD1;
C_mu = 1.5 * V_shear1 ./ V_iso1;
C_M = 1.5 * V_shear2 ./ V_iso2;
C_c = C_M ./ C_mu;
MKi = 3 * V_MD ./ V_MD2;
MKa = (6/5) * V_shear1 ./ V_MD2;
MKad = (6/5) * V_shear ./ V_MD2;
MKt = MKi + MKa;
MK = MKad + MKi;
MKd = MKa - MKad;
uFA = sqrt(C_mu);
S_I = sqrt(V_MD.*(V_MD > 0));
S_A = sqrt(V_shear1.*(V_shear1 > 0));
metrics.b0 = B0;
metrics.fa = FA';
metrics.md = MD';
metrics.ad = AD';
metrics.rd = RD';
metrics.v_md2 = V_MD2';
metrics.v_shear2 = V_shear2';
metrics.v_iso2 = V_iso2';
metrics.v_md = V_MD';
metrics.v_shear = V_shear';
metrics.v_iso = V_iso';
metrics.v_md1 = V_MD1';
metrics.v_shear1 = V_shear1';
metrics.v_iso1 = V_iso1';
metrics.c_md = C_MD';
metrics.c_mu = C_mu';
metrics.c_m = C_M';
metrics.c_c = C_c';
metrics.mki = MKi';
metrics.mka = MKa';
metrics.mkad = MKad';
metrics.mkt = MKt';
metrics.mk = MK';
metrics.mkd = MKd';
metrics.ufa = uFA';
metrics.s_i = S_I';
metrics.s_a = S_A';
if exist('mask','var'); metrics = Volumes.unvec_struct(metrics,mask); end
end
end
end