-
Notifications
You must be signed in to change notification settings - Fork 0
/
OED_PCASL_HadT1adj_LOptimal.m
executable file
·159 lines (122 loc) · 6.09 KB
/
OED_PCASL_HadT1adj_LOptimal.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
function [bestPLD,bestTau,bestminVariance] = OED_PCASL_HadT1adj_LOptimal(varargin)
time = tic;
[param, BATDist, distWeight, scanTime, A, allSlice, nPLD, lims, slicedt] = parseInputs(varargin,nargin);
allSliceL = length(allSlice);
% Using what we know about the optimal design to limit the distribution we
% search over.
tauTry = round((lims.tauLB:lims.tauStep:lims.tauUB),5)';
tauTryL = length(tauTry);
PLDTry = round((lims.PLDLB:lims.PLDStep:lims.PLDUB),5)';
PLDTryL = length(PLDTry);
distL = length(BATDist);
%% Now minimise the variance over the distribution
bestminVariance = 1e99;
for numPLD = nPLD
disp(['numPLD = ' ns(numPLD)])
% Set the initial block length and PLD
tau = -1;
PLD = -1;
% Need to work out what the T1 adjusted block lengths are
param.tau = zeros(numPLD, 1, tauTryL);
for jj = 1:tauTryL
param.tau(:,1,jj) = bolus_fit(param, numPLD, tauTry(jj), 0, lims.tauStep);
end
param.tau(param.tau < lims.tauLB) = lims.tauLB; % Make sure no blocks are lower than 0.1
param.tau = repmat( param.tau , 1 , PLDTryL , 1 );
% For time-encoded data, there are effective PLDs
% The effective PLDs are ordered such that they correspond
% to the correct tag block
PLD_effectiveShift = cumsum(param.tau(end:-1:1,:,:)) - param.tau(end:-1:1,:,:); % Count up backwards
PLD_effectiveShift = PLD_effectiveShift(end:-1:1,:,:); % Order so the blocks are in order
PLDTry_curr = PLD_effectiveShift + repmat(PLDTry(:)', nPLD, 1, tauTryL);
clear PLD_effectiveShift
distWeightCurr = permute( repmat( distWeight', PLDTryL, 1, tauTryL ), [1,3,2] );
clear distWeight
continueFlag = true;
while continueFlag
oldTau = tau;
oldPLD = PLD; % Each time through all of the samples, save them
for ii = 1%1:numPLD
variance = zeros(PLDTryL, tauTryL, distL, allSliceL);
for kk = 1:allSliceL
slice = allSlice(kk);
param.t = param.tau + PLDTry_curr + ((slice-1)*slicedt);
[variance(:,:,:,kk)] = Hessian_LOptimal_analytical(param,A,scanTime,slice,slicedt);
end % End slice loop
% For comparison between nPLD, divide by the square of the number of
% images will be used for decoded: the result of signal
% averaging on the variance.
variance = variance / ((numPLD + 1)/2);
variance = variance * 6000 * 6000; % Change into (ml/100g/min)^2
% Take mean of generalised variance across the slices
varianceMean = mean(variance,4);
if any(varianceMean(:)==0)
warning('Some variances are zero. Setting to inf...')
varianceMean(varianceMean==0) = inf;
end
% Take mean of generalised variance across the BAT distribution
cost = distWeightCurr .* varianceMean;
clear varianceMean
cost(distWeightCurr==0) = 0; % To correct for 0*nan in distWeight .* CovCurr
cost(isnan(cost)) = inf; % To correct for 0*inf in distWeight .* CovCurr
costMean = mean( cost , 3 ); % Weighted mean
% Find the Tau and PLD that leads to the minimum generalised variance
[minVariance,jj] = min(costMean(:));
[row,col] = ind2sub(size(costMean),jj);
% Save the optimal PLD and LD
PLD = PLDTry(row);
tau = tauTry(col);
disp(['Tau = ' array2string(tau,'; ')])
disp(['PLD = ' array2string(PLD,'; ')])
end % End PLD loop
if sum(PLD~=oldPLD)==0 && (tau~=oldTau)==0
continueFlag = false;
end
end % End while
if (bestminVariance-minVariance)/bestminVariance > 1e-12
bestTau = param.tau(:,row,col);
bestPLD = PLD;
disp(['Best Tau = ' array2string(bestTau,'; ')])
disp(['Best PLD = ' array2string(bestPLD,'; ')])
bestminVariance = minVariance;
PLD_effectiveShift = cumsum(bestTau(end:-1:1)) - bestTau(end:-1:1); % Count up backwards
PLD_effectiveShift = PLD_effectiveShift(end:-1:1,:,:);
param.tau = bestTau;
param.PLD = PLD_effectiveShift + bestPLD;
param.t = param.tau + param.PLD;
disp(['numAv = ' ns(TRWeightingOrNAveFloor(param,scanTime,1,1,slicedt))])
end
toc(time)
end
%%
%%%% Function to set up inputs %%%%
function [param, BATDist, distWeight, scanTime, A, allSlice, nPLD, lims, slicedt] = parseInputs(varargins,nargins)
param = varargins{1};
BATDist = varargins{2}(:);
param.BAT = BATDist;
if nargins>2; distWeight = varargins{3}(:); else; distWeight = ones(size(BATDist)); end
if nargins>3; scanTime = varargins{4}; else; scanTime = 300; end
if nargins>4; A = varargins{5}; else; A = [1,1;1,1]; end
if nargins>5; allSlice = varargins{6}; else; allSlice = 1; end
if nargins>6; nPLD = varargins{7}; else; nPLD = 7; end
if nargins>7
lims = varargins{8};
%if(~isfield(lims,'maxIter'));lims.maxIter=50;end
if(~isfield(lims,'tauStep'));lims.tauStep=0.025;end
if(~isfield(lims,'PLDStep'));lims.PLDStep=0.025;end
if(~isfield(lims,'tauLB'));lims.tauLB=0.1;end
if(~isfield(lims,'tauUB'));lims.tauUB=4/nPLD(end);end
if(~isfield(lims,'PLDLB'));lims.PLDLB=0;end
if(~isfield(lims,'PLDUB'));lims.PLDUB=0.5;end % If 3s is longest BAT, we will see the peak signal with 3s PLD
else
lims = struct('PLDStep',0.025,'PLDLB',0,'PLDUB',BATDist(end)+0.025,...
'tauStep',0.025,'tauLB',0.1,'tauUB',4/nPLD(end));
end
if nargins>8; slicedt = varargins{9};
else
slicedt = 0.053125;
warning(['OED_CASL_PLD_NLLS_TRW_WDist_Floor_Unnormalised_acrossSlices.m: No slicedt specified. Using default slicedt = ' ns(slicedt)])
end
end
%%%%%%%%%%%%%%%%%%%%%%%%
end