-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfit_braino.m~
274 lines (234 loc) · 9.45 KB
/
fit_braino.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
runme_braino;
%%%%%%%
%I guess the first step is to set up a 2D basis set which has dimensions
%(n_pointsx4)xN_met
%i.e. Hadamard spectra are concatenated.
metabolites={'Cho','Cr','NAA', 'Lac', 'GABA'};
%n_metabolites=28;
%weights=ones(1,n_metabolites);
weights=[1 1 7 2 9 9 9 1 1 1 3 3 10 1 4 1 17 17 5 5 5 1 1 1 4 1 0.1 ];
%
load ../../Documents/MATLAB/HERCULES/SIMULATIONS.nosync/HERMES5_TE80_EP20_20170525/sim_IndividualMetabs.mat;
%metabolites={'Cho','Cr','NAA', 'Lac', 'GABA'};
%%%
metabolites={'Asc','Asp', 'Cr', 'GABA', 'GPC1','GPC2','GPC3','GSH1','GSH2','GSH3','Gln1','Gln2','Glu','H20','Ins','Lac','NAA1','NAA2','NAAG1','NAAG2','NAAG3','PCh1','PCh2','PCr1','PCr2','Scyllo','bHG'};
n_metabolites=27;
%weights=ones(1,n_metabolites);`
clear ANames
ANames=who('A_*');
%%%
BasisSet=zeros(length(ppm),n_metabolites,4);
eval(['ppm=' ANames{1} '.ppm;']);
%Restrict fit range
ppm(1)
ppm(end)
size(ppm)
%stophere
fit_range = ppm >= 1 & ppm <= 4.5;
ppm=ppm(fit_range);
BasisSet=zeros(sum(fit_range),n_metabolites,4);
for ii=1:n_metabolites
ANames{ii}
eval(['BasisSet(:,ii,1)=' ANames{ii} '.specs(fit_range);']);
eval(['BasisSet(:,ii,2)=B' ANames{ii}(2:end) '.specs(fit_range);']);
eval(['BasisSet(:,ii,3)=C' ANames{ii}(2:end) '.specs(fit_range);']);
eval(['BasisSet(:,ii,4)=D' ANames{ii}(2:end) '.specs(fit_range);']);
dummy=squeeze(BasisSet(:,ii,:))*hadamard(4);
BasisSet(:,ii,:)=dummy;
end
%BasisSet(:,7,:)=circshift(BasisSet(:,7,:),[0 -3 0]);
BasisSet=real(permute(BasisSet,[1,3,2]));
BasisSet=reshape(BasisSet,[length(BasisSet(:))/n_metabolites n_metabolites]);
%based on fit_iv.m, but lock the different parts of molecules together -
%e.g. NAAG1-3 GSH1-3
BasisSet2=BasisSet;
BasisSet2(:,5)=BasisSet2(:,5)+BasisSet2(:,6)+BasisSet2(:,7); %GPC
BasisSet2(:,6)=0;
BasisSet2(:,7)=0;
BasisSet2(:,8)=BasisSet2(:,8)+BasisSet2(:,9)+BasisSet2(:,10); %GSH
BasisSet2(:,9)=0;
BasisSet2(:,10)=0;
BasisSet2(:,11)=BasisSet2(:,11)+BasisSet2(:,12); %Gln
BasisSet2(:,12)=0;
BasisSet2(:,17)=BasisSet2(:,17)+BasisSet2(:,18); %NAA
BasisSet2(:,18)=0;
BasisSet2(:,19)=BasisSet2(:,19)+BasisSet2(:,20)+BasisSet2(:,21); %NAA
BasisSet2(:,20)=0;
BasisSet2(:,21)=0;
compressed=[1 1 1 1 1 0 0 1 0 0 1 0 1 1 1 1 1 0 1 0 0 1 1 1 1 1 1];
BasisSet3=zeros(length(ppm)*4,sum(compressed));
for ii=1:n_metabolites
jj=sum(compressed(1:ii));
if(BasisSet2(1,ii)) ~= 0
BasisSet3(:,jj)=BasisSet2(:,ii);
end
weights3(jj)=weights(ii);
end
%Shift PCho over (phantom is CholineChloride)
BasisSet3(:,15)=circshift(BasisSet3(:,15),[3 0]);
n_metabolites=sum(compressed);
weights3=weights3(1:n_metabolites);
% %Set up a fake spectrum
% %simulate noise of same size as the spectrum
% Noise=0.4*randn(length(ppm)*4,1);
% weights_sim=weights.*(1+0.1*randn(n_metabolites,1).');
%
% p1=0.05;
% p2=0.66;
% width=p1-p1*p2;
% widthG=p1*p2;
%
% lineshape = conv(LorentzianModel_herc([1 width 0 0 0],0.5:-0.01:-0.5).',GaussModel_herc([1 widthG 0 0 0],0.5:-0.01:-0.5).','same');
% lineshape =lineshape/sum(lineshape);
% baseline_c=[0 0.01 0.04 -0.2];
% baseline_m=[0.01 0.0 0.004 -0.03];
% baseline=[(ppm-3)*baseline_m(1)+baseline_c(1) (ppm-3)*baseline_m(2)+baseline_c(2) (ppm-3)*baseline_m(3)+baseline_c(3) (ppm-3)*baseline_m(4)+baseline_c(4)].';
% %Add additional basline curvature
% baseline=baseline+4*([8*sin(ppm*pi/3+1).'; 4*sin(ppm*pi/2.3+2).'; 4*sin(ppm*pi/8+2).'; -4*sin(ppm*pi/1.58+2).']);
% baseline=baseline';
% size(baseline')
% size(Noise)
% size(conv(sum(BasisSet.*repmat(weights_sim,[size(BasisSet,1) 1]),2),lineshape,'same'))
% Sim_Spectrum = conv(sum(BasisSet.*repmat(weights_sim,[size(BasisSet,1) 1]),2),lineshape,'same')+Noise+baseline';
% %Sim_Spectrum = sum(BasisSet.*repmat(weights_sim,[size(BasisSet,1) 1]),2)+Noise+baseline;
% Sim_Spectrum =real(Sim_Spectrum);
%NEXT STEP IS TO INTEGRATE MORE PARAMETERS
%StackPlot(Sim_Spectrum(end:-1:1,:),40)
%pause(10)
% hold on
% plot(lineshape)
% plot(Sim_Spectrum);
%
% pause(5)
%set(gca,'XDir','reverse');
Sim_Spectrum =real(M.spec.H4(end:-1:1,:));
%Sim_Spectrum =Sim_Spectrum(end:-1:1,:);
Sim_Spectrum=Sim_Spectrum(:);
%Work on a linear simulation
%Drop in code from GannetFit
size(Sim_Spectrum)
weights3
weights_init=[[1.5 1.5 10 2 0 2 4 10] zeros(1,8) 0.1 0.5 0];
lb = [zeros(1,n_metabolites) ones(1,8)*(-1000000) 0.01 0 -100]; %NP; our bounds are 0.03 less due to creatine shift
ub = [ones(1,n_metabolites)*20 ones(1,8)*1000000 1 1 100];
options = optimset('lsqcurvefit');
options = optimset(options,'Display','off','TolFun',1e-10,'Tolx',1e-10,'MaxIter',1e5);
nlinopts = statset('nlinfit');
nlinopts = statset(nlinopts, 'MaxIter', 1e5);
Fit_Spectrum2=Hercules_model(weights_init,BasisSet3);
Sim_Spectrum=Sim_Spectrum/max(Sim_Spectrum(:))*1E6;
StackPlot(Sim_Spectrum,40)
hold on
StackPlot(Fit_Spectrum2,40,'r')
% %StackPlot(Fit_Spectrum,1,'b')
hold off
%
plot(1:(sum(fit_range)*4),Sim_Spectrum,1:(sum(fit_range)*4),Fit_Spectrum2);
% pause(10)
%Fitting happens here
[Fitted_weights,resnorm,residg] = lsqcurvefit(@(xdummy,ydummy) Hercules_model(xdummy,ydummy), ...
weights_init, BasisSet3,Sim_Spectrum, ...
lb,ub,options);
% Sim_Spectrum = sum(BasisSet.*repmat(weights_sim,[size(BasisSet,1) 1]),2)+Noise;
%plot(1:2004,Sim_Spectrum,1:2004,sum(BasisSet.*repmat(Fitted_weights,[size(BasisSet,1) 1]),2));
% Sim_Spectrum=reshape(Sim_Spectrum,[,4]);
% %linear=linspace(-0.5,0.5,size(BasisSet,1)/4);
% %fit_baseline = [linear*Fitted_weights(10)+Fitted_weights(6) linear*Fitted_weights(11)+Fitted_weights(7) linear*Fitted_weights(12)+Fitted_weights(8) linear*Fitted_weights(13)+Fitted_weights(9)]
%
Fit_Spectrum=reshape(Hercules_model(Fitted_weights,BasisSet3),[length(ppm),4]);;
figure(7)
StackPlot(Sim_Spectrum,1);
hold on
StackPlot(Fit_Spectrum(:),1,'r');
hold off
%
title('Result after initial fit')
spline_vault=zeros([length(ppm),4,5]);
%Introduce iterative baseline spline
weights_init=Fitted_weights;
for ii=1:1
Sim_Spectrum=reshape(Sim_Spectrum,[length(ppm),4]);
Residuals=Sim_Spectrum-Fit_Spectrum;
%focus on fitting the first bit
%
spline1=fit(ppm',Residuals(1:length(ppm))','smoothingspline','SmoothingParam',0.5);
spline2=fit(ppm',Residuals((1:length(ppm))+length(ppm))','smoothingspline','SmoothingParam',0.7);
spline3=fit(ppm',Residuals((1:length(ppm))+length(ppm)*2)','smoothingspline','SmoothingParam',0.5);
spline4=fit(ppm',Residuals((1:length(ppm))+length(ppm)*3)','smoothingspline','SmoothingParam',0.5);
%
%
spline_vault(:,1,ii)=feval(spline1,ppm);
spline_vault(:,2,ii)=feval(spline2,ppm);
spline_vault(:,3,ii)=feval(spline3,ppm);
spline_vault(:,4,ii)=feval(spline4,ppm);
figure(8)
plot(ppm,spline_vault(:,2,ii),ppm,Residuals((1:length(ppm))+length(ppm)));
title(['Residual SD:' num2str(std(Residuals(:)))]);
descent(ii)=std(Residuals(:));
%pause(4)
% %plot(ppm,Residuals(1:501));
% %hold on
% %plot(ppm,feval(spline,ppm))
% %hold off
Sim_Spectrum=Sim_Spectrum-squeeze(spline_vault(:,:,ii));
Sim_Spectrum=Sim_Spectrum(:);
% size(Sim_Spectrum)
%loop through the fit again
if(ii>1)
weights_init=Fitted_weights(ii-1,:);
%weights_init(end)=0.5;
%weights_init(end-1)=0.1;
end
[Fitted_weights(ii,:),resnorm,residg] = lsqcurvefit(@(xdummy,ydummy) Hercules_model(xdummy,ydummy), ...
weights_init, BasisSet3,Sim_Spectrum, ...
lb,ub,options);
%
%
%
%
% %Fit_Spectrum2(1:501)=Fit_Spectrum2(1:501)+feval(spline,ppm)';
Fit_Spectrum=reshape(Hercules_model(Fitted_weights(ii,:),BasisSet3),[length(ppm),4]);;
end
Sim_Spectrum=reshape(Sim_Spectrum,[length(ppm),4]);
Sim_Spectrum=Sim_Spectrum+squeeze(sum(spline_vault,3));
%Fit_Spectrum2=reshape(Hercules_model(Fitted_weights,BasisSet3),[length(ppm),4])+squeeze(sum(spline_vault,3));
%ii=1;
Fit_Spectrum2=reshape(Hercules_model(Fitted_weights(ii,:),BasisSet3),[length(ppm),4])+squeeze(sum(spline_vault,3));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
figure(2)
Sim_Spectrum_display=Sim_Spectrum;
Fit_Spectrum_display=Fit_Spectrum2;
Sim_Spectrum_display(:,2:4)=Sim_Spectrum_display(:,2:4)*5;
Fit_Spectrum_display(:,2:4)=Fit_Spectrum_display(:,2:4)*5;
%Sim_Spectrum_display(Sim_Spectrum_display<-180)=-180;
%Fit_Spectrum_display(Fit_Spectrum_display<-180)=-180;
StackPlot(Sim_Spectrum_display,3000000)
hold on
StackPlot(Fit_Spectrum_display,3000000,'r')
spline_vault_display=squeeze(sum(spline_vault,3));
spline_vault_display(:,2:4)=spline_vault_display(:,2:4)*5;
StackPlot(spline_vault_display,3000000,'g')
hold off
text(400,220,'* 0.2')
resnorm
% orig_width=-200;
% BasisSet=zeros(length(ppm),4,length(weights));
% %simpleCho
% BasisSet(:,1,1)=LorentzianModel([0.8 orig_width 3.2 0 0],ppm);
% %simpleCr
% BasisSet(:,1,2)=LorentzianModel([1 orig_width 3.0 0 0],ppm);
% %simpleNAA
% BasisSet(:,1,3)=LorentzianModel([1.2 orig_width 2.0 0 0],ppm);
% BasisSet(:,3,3)=LorentzianModel([-1.2 orig_width 2.0 0 0],ppm);
% %simpleLac
% BasisSet(:,2,4)=LorentzianModel([0.2 orig_width 1.33 0 0],ppm)+LorentzianModel([0.2 orig_width 1.28 0 0],ppm);
% %simpleGABA
% BasisSet(:,1,5)=LorentzianModel([0.2 orig_width 3.02 0 0],ppm);
% BasisSet(:,3,5)=LorentzianModel([0.1 orig_width 3.02+0.06 0 0],ppm)+LorentzianModel([0.1 orig_width 3.02-0.06 0 0],ppm)+LorentzianModel([0.05 -200 3.02 0 0],ppm);
%
%
%
% BasisSet=reshape(BasisSet,[length(ppm)*4,length(weights)]);
%Model_Spectrum = sum(BasisSet.*repmat(weights,[size(BasisSet,1) 1]),2);
%plot(Model_Spectrum);