-
Notifications
You must be signed in to change notification settings - Fork 0
/
OED_PCASL_Hadfreelunch_fixed_LOptimal.m
executable file
·148 lines (113 loc) · 5.76 KB
/
OED_PCASL_Hadfreelunch_fixed_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
function [bestPLD,bestTau,bestminVariance] = OED_PCASL_Hadfreelunch_fixed_LOptimal(varargin)
time = tic;
% nPLD is the same as param.num_enc for time-encoded protocols
[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)'; % The duration of the 2nd encoded LD
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; % Currently set up for fixed block duration
PLD = -1;
param.tau = permute(repmat(tauTry, 1, numPLD, PLDTryL), [2,3,1]); % Dimensions nPLD x PLDTryL x tauTryL
param.tau(1,:,:) = 1.8; % White Paper tag duration (Should make this a user defined parameter)
param.PLD = repmat(PLDTry(:)', 1, 1, tauTryL);
[~,param.PLD] = generateTETimings(param.tau , param.PLD, numPLD, 'standard');
distWeight = permute( repmat( distWeight', PLDTryL, 1, tauTryL ), [1,3,2] );
continueFlag = true;
while continueFlag
oldTau = tau;
oldPLD = PLD; % Each time through all of the samples, save them
for ii = 1 % Don't need to loop through timepoints for time-encoded
variance = zeros(PLDTryL, tauTryL, distL, allSliceL);
for kk = 1:allSliceL
slice = allSlice(kk);
param.t = param.tau + param.PLD + ((slice-1)*slicedt);
[variance(:,:,:,kk)] = Hessian_LOptimal_analytical(param,A,scanTime,slice,slicedt);
end % End slice loop
% For comparison between nPLD, divide by 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 = distWeight .* varianceMean; % Weight by the ATT distribution
cost(distWeight==0) = 0; % To correct for 0*nan in distWeight .* varianceMean
cost(isnan(cost)) = inf; % To correct for 0*inf in distWeight .* varianceMean
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 the PLDs and LDs stay constant, then exit
if sum(PLD~=oldPLD)==0 && (tau~=oldTau)==0
continueFlag = false;
end
end % End while
%if (bestminEffLS-minEffLS)/bestminEffLS > 1e-12, update
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;
param.tau = bestTau;
[~,param.PLD] = generateTETimings(param.tau , bestPLD, numPLD, 'standard');
param.t = param.tau + param.PLD;
[numAv,TotalTR] = TRWeightingOrNAveFloor(param,scanTime,1,1,slicedt);
disp(['numAv = ' ns(numAv)])
disp(['Scan time = ' ns(numAv*TotalTR)])
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,0;0,0]; 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=1.8;end
if(~isfield(lims,'PLDLB'));lims.PLDLB=0;end
if(~isfield(lims,'PLDUB'));lims.PLDUB=1;end
else
lims = struct('PLDStep',0.025,'PLDLB',0,'PLDUB',1,...
'tauStep',0.025,'tauLB',0.1,'tauUB',1.8);
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