This repository has been archived by the owner on Jan 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
calibrate3D_g.m
303 lines (265 loc) · 9.76 KB
/
calibrate3D_g.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
% Copyright (c)2017 Ries Lab, European Molecular Biology Laboratory,
% Heidelberg.
%
% This file is part of GPUmleFit_LM Fitter.
%
% GPUmleFit_LM Fitter is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% GPUmleFit_LM Fitter is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with GPUmleFit_LM Fitter. If not, see <http://www.gnu.org/licenses/>.
%
%
% Additional permission under GNU GPL version 3 section 7
%
% If you modify this Program, or any covered work, by
% linking or combining it with libraries required for interaction
% with analysis programs such as Igor Pro or Matlab,
% the licensors of this Program grant you additional permission
% to convey the resulting work.
%%
function [SXY,beadpos,parameters]=calibrate3D_g(p)
% p.filelist
% p.outputfile
% p.dz
% p.modality
% p.zcorr
% p.ROIxy
% p.ROIz
% p.smoothxy
% p.smoothz
% p.gaussrange
% p.filter;
% p.zcorrframes
% p.gaussroi
% p.fov =[x1 y1 x2 y2]
% p.mindistance
% p.emgain
% p.xrange
% p.yrange
% p.positions (get beads only here)
% p.smap (called from SMAP: extended functionality)
% isglobalfit
% Tfile
if ~isfield(p,'smap')&& (~p.isglobalfit)
p.smap=false;
% imageRoi=zeros(2,1);
else
% imageRoi=p.imageRoi; %position of image on Chip. This is important, if the calibration file used a different ROI than the actual measurement
end
if ~isfield(p,'xrange')
p.xrange=[-inf inf]; p.yrange=[-inf inf];
end
if ~isfield(p,'emgain')
p.emgain=0;
end
if ~isfield(p,'smoothxy')
p.smoothxy=0;
end
if ~isfield(p,'isglobalfit')
p.isglobalfit=0;
end
if ~isfield(p,'transformation')
p.transformation=[];
end
if ~isfield(p,'filechannel')
p.filechannel=1;
end
%get bead positions
p.status.String='Load files and segment beads';drawnow
if ~isfield(p,'tabgroup')
f=figure('Name','Bead calibration');
p.tabgroup=uitabgroup(f);
calibrationfigure=f;
else
calibrationfigure=p.tabgroup.Parent;
end
%get beads from images
% if isfield(p,'isglobalfit')&&p.isglobalfit
[beads,p]=images2beads_globalfit(p);
% else
% [beads,p]=images2beads_so(p);
% end
imageRoi=p.roi{1};
%get positions of beads
for k=length(beads):-1:1
beadposx(k)=beads(k).pos(1);
beadposy(k)=beads(k).pos(2);
end
%if only to take beads in a certain range, remove others
if isfield(p,'fov')&&~isempty(p.fov)
indbad=beadposx<p.fov(1)| beadposx>p.fov(3)|beadposy<p.fov(2)|beadposy>p.fov(4);
beads=beads(~indbad);
end
if isempty(beads)
warndlg('Could not find and segment any bead. ROI size too large?')
p.status.String='error: could not find and segment any bead...';
return
end
p.midpoint=round(size(beads(1).stack.image,3)/2); %reference for beads
p.ploton=false;
if contains(p.modality,'astig') %|| contains(p.modality,'2D') %XXXX %needs to be fixed and extended to global
%determine sx,sy
t=tic;
p.status.String=['Gaussian fit of beads to get spatial parameters '];drawnow
for k=1:length(beads)
stackh=single(beads(k).stack.image);
s=size(stackh);
d=round((s(1)-p.gaussroi)/2);
stack=stackh(d+1:end-d,d+1:end-d,:);
%fit bead bead stacks with Gaussian model
if contains(p.modality,'astig')
P=mleFit_LM(stack,4,100,1,0,1);
beads(k).loc.PSFxpix=P(:,5);
beads(k).loc.PSFypix=P(:,6);
beads(k).loc.phot=P(:,3);
beads(k).f0=stackas2z_so(beads(k).loc.PSFxpix,beads(k).loc.PSFypix,beads(k).loc.frames,beads(k).loc.phot,p);
else
P=mleFit_LM(stack,2,100,1,0,1);
beads(k).loc.PSFxpix=P(:,5);
beads(k).loc.PSFypix=P(:,5);
beads(k).loc.phot=P(:,3);
beads(k).f0=stackas2z2D_so(beads(k).loc.PSFxpix,beads(k).loc.frames,beads(k).loc.phot,p);
end
beads(k).loc.bg=P(:,4);
%determine true position of the beads as the position, where PSFx==PSFy
ind=find(beads(k).loc.frames<=beads(k).f0,1,'last');
if isnan(beads(k).f0)||isempty(ind)
ind=1;
end
beads(k).psfx0=beads(k).loc.PSFxpix(ind);
beads(k).psfy0=beads(k).loc.PSFypix(ind);
if toc(t)>1
p.status.String=['Gaussian fit of beads to get spatial paramters: ' num2str(k) ' of ' num2str(length(beads))];
drawnow
t=tic;
end
end
%remove beads for which no position could be found
badind=isnan([beads(:).f0]);
beads(badind)=[];
else
f0g=p.midpoint;
for k=1:length(beads)
beads(k).f0=f0g;
end
end
%get positions of beads
for k=length(beads):-1:1
beadposxs(k)=beads(k).pos(1);
beadposys(k)=beads(k).pos(2);
beadfilenumber(k)=beads(k).filenumber;
end
%spatially dependent calibration
tgmain=p.tabgroup;
for X=1:length(p.xrange)-1
for Y=1:length(p.yrange)-1
if length(p.xrange)>2||length(p.yrange)>2
ht=uitab(tgmain,'Title',['X' num2str(X) 'Y' num2str(Y)]);
p.tabgroup=uitabgroup(ht);
end
indgood=beadposxs< p.xrange(X+1) & beadposxs>p.xrange(X) & beadposys<p.yrange(Y+1) & beadposys>p.yrange(Y);
beadsh=beads(indgood);
for k=1:max(beadfilenumber)
indfile=(beadfilenumber==k)&indgood;
p.fileax(k).NextPlot='add';
scatter(p.fileax(k),beadposxs(indfile),beadposys(indfile),60,[1 1 1])
scatter(p.fileax(k),beadposxs(indfile),beadposys(indfile),50)
end
if isempty(beadsh)
disp(['no beads found in part' num2str(p.xrange(X:X+1)) ', ' num2str(p.yrange(Y:Y+1))])
continue
end
if contains(p.modality,'astig')
%get calibration for Gaussian fit
p.status.String='get spline approximation';drawnow
p.ax_z=axes(uitab(p.tabgroup,'Title','sx(z), sy(z)'));
[spline_curves,indgoodc,curves]=getspline_so(beadsh,p);
gausscal.spline_curves=spline_curves;
drawnow
else
indgoodc=true(size(beadsh));
gausscal=[];
p.ax_z=[];
end
% get cspline calibration
p.status.String='get cspline calibration';drawnow
[csplinecal,indgoods,beadpos{X,Y}]=getstackcal_g(beadsh(indgoodc),p);
for f=1:max(beadpos{X,Y}.filenumber(:))
indfile=(beadpos{X,Y}.filenumber==f);
p.fileax(f).NextPlot='add';
plot(p.fileax(f),beadpos{X,Y}.xim(indfile),beadpos{X,Y}.yim(indfile),'m+')
end
icf=find(indgoodc);
icfs=icf(indgoods);
for k=1:length(csplinecal.cspline.coeff)
cspline.coeff{k}=single(csplinecal.cspline.coeff{k});
end
cspline.dz=csplinecal.cspline.dz;
cspline.z0=csplinecal.cspline.z0;
cspline.x0=csplinecal.cspline.x0;
cspline.global.isglobal=p.isglobalfit;
cspline.mirror=csplinecal.cspline.mirror;
cspline.global.transformation=p.transformation;
if p.isglobalfit
cspline.global.coeffrawref=single(csplinecal.cspline.coeffrawref);
cspline.global.coeffrawtar=single(csplinecal.cspline.coeffrawtar);
cspline.normf=csplinecal.cspline.normf;
end
if contains(p.modality,'astig')
photbead=10^5; %corr PSF normalized to 1. As MLE is used, this screws up statistics totally. Thus assign bright signal to bead.
stackb=csplinecal.PSF;
stackb=(stackb)*photbead;
mp=ceil(size(stackb,1)/2);dx=floor(p.gaussroi/2);
stack=single(stackb(mp-dx:mp+dx,mp-dx:mp+dx,:));
P=mleFit_LM(stack,4,200,1,0,1);
ch.sx=double(P(:,5));
ch.sy=double(P(:,6));
f0m=median([beadsh(icfs).f0]);
ch.z=double(((1:size(stack,3))'-f0m)*p.dz);
p.ax_sxsy=axes(uitab(p.tabgroup,'Title','Gauss cal'));
p.ax_z.NextPlot='add';
p.status.String='get Gauss model calibration';drawnow
gausscalh=getgausscal_so(ch,p);
legend(p.ax_z,'bad bead data','good bead data','spline fit sx','spline fit sy','average PSF','average PSF','Gauss zfit','Gauss zfit')
gausscal=copyfields(gausscal,gausscalh);
gauss_zfit=single(gausscal.fitzpar);
gauss_sx2_sy2=gausscal.Sx2_Sy2;
else
gausscal=[];
gauss_sx2_sy2=[];
gauss_zfit=[];
p.ax_sxsy=[];
end
cspline_all=csplinecal;
% cspline_all.bspline=[];
% cspline_all.PSF=[];
% cspline_all.PSFsmooth=[];
cspline_all=[];
PSF=csplinecal.PSF;
SXY(X,Y)=struct('gausscal',gausscal,'cspline_all',cspline_all,'gauss_sx2_sy2',gauss_sx2_sy2,'gauss_zfit',gauss_zfit,...
'cspline',cspline,'Xrangeall',p.xrange+imageRoi(1),'Yrangeall',p.yrange+imageRoi(2),'Xrange',p.xrange([X X+1])+imageRoi(1),...
'Yrange',p.yrange([Y Y+1])+imageRoi(2),'posind',[X,Y],'EMon',p.emgain,'PSF',{PSF});
end
end
parameters=myrmfield(p,{'tabgroup','status','ax_z','ax_sxsy','fileax'});
p.status.String='save calibration';drawnow
if ~isempty(p.outputfile)
if p.smap
parameters.smappos.P=[];
save(p.outputfile,'SXY','parameters');
else
save(p.outputfile,'gausscal','cspline_all','gauss_sx2_sy2','gauss_zfit','cspline','parameters');
end
filefig=strrep(p.outputfile,'.mat','.fig');
savefig(calibrationfigure,filefig,'compact');
end
p.status.String='Calibration done';drawnow
end