-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetermine_Blinking_Distribution5.m
306 lines (183 loc) · 7.48 KB
/
Determine_Blinking_Distribution5.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
300
301
302
303
%This is a function that determines P_{blink} from the supporting material
%and defines the bins used within the likelihood calculation.
function [bins, D_Counts3, Total_No_Blink, Resolution, X_overall, M_mat]= Determine_Blinking_Distribution5(LocalizationsFinal, Frame_Information, Pre_A, Resolution)
X_overall=[];
%First we are going to go through and find the max of distance between locs
%in a image. We are also going to go through and find the min distance in
%an image
D_maxf=0;
D_maxm=Inf;
for i=1:length(LocalizationsFinal)
% if length(LocalizationsFinal{i})<9000
i/length(LocalizationsFinal);
D = (pdist(LocalizationsFinal{i}));
D_max=max(D);
if D_max>D_maxf
D_maxf=D_max;
end
if D_max<D_maxm
D_maxm=D_max;
end
%end
end
%Set the bins used in the likelihood calculation, this will be the same for
%every image.
bins=[0:Resolution:D_maxf, Inf];
Total_Blink=[];
Total_No_Blink=[];
disp('Working on step 1')
for i=1:length(LocalizationsFinal)
%if length(LocalizationsFinal{i})<9000
%disp(['Percent Done=',num2str(i/length(LocalizationsFinal)),' with STEP 1'])
%Here we will gather the distance distrabutions from as many cells as
%posible. This will allow us to define the joint probability
%distribution more accuratly.
Z2 = pdist([[1:length(Frame_Information{i})]'*0,Frame_Information{i}(:)]);
D = (pdist(LocalizationsFinal{i}));
D_Blink=D(Z2<Pre_A);
D_No_Blink=D([(Z2>Pre_A).*(Pre_A*5>Z2)]==1);
%Here we define the distrubions for each of the images, we then compair
%them later.
D_Counts=histcounts(D_Blink,bins,'Normalization','prob');
Total_Blink=[Total_Blink; D_Counts];
D_Counts2=histcounts(D_No_Blink,bins,'Normalization','prob');
Total_No_Blink=[Total_No_Blink; D_Counts2];
%end
end
%%
%Here we go through and scale the probablity distributions after 10 times
%the resolution of the experiment.
D_Counts=mean(Total_Blink,1);%histcounts(Total_Blink,bins,'Normalization','prob');
D_Counts2=mean(Total_No_Blink,1);%histcounts(Total_No_Blink,bins,'Normalization','prob');
%kk=find(D_Counts<D_Counts2,1);
D_Scale=sum(D_Counts(10:end))/sum(D_Counts2(10:end));
D_Counts3=D_Counts-D_Counts2*D_Scale;
D_Counts3=(D_Counts3)/sum(D_Counts3);
%Finally we clean up the distributions a little bit so that the logic is
%consistent with how the distribution should behave
good=1;
ins=0;
for i=4:length(D_Counts3)-1
if D_Counts3(i)>0 && D_Counts3(i+1)<D_Counts3(i) && good==1
else
if ins==0
ins=i;
end
good=0;
D_Counts3(i)=0;
end
end
D_Counts3(D_Counts3<0)=0;
D_Counts3=(D_Counts3)/sum(D_Counts3);
D_Counts3=(D_Counts3)/sum(D_Counts3);
%Anything greater than 8 times the resolution of the experiment is
%considered noise and will be eliminated from the probability distribution.
%We then warn the user if this is the case as they may want to consider a
%lower resolution.
if sum(D_Counts3(8:end)>0)>1
disp('Warning: Eliminating Noise for higher bins')
D_Counts3(8:end)=0;
D_Counts3=(D_Counts3)/sum(D_Counts3);
end
Distribution_for_Blink2=D_Counts3;
%Here we do the fitting to determine how much of each distribution makes up
%the pairwise distance distributions at each frame difference. This is
%equation 3 of the Supporting Material.
Dscale_store={};
for oinw=1:length(LocalizationsFinal)
Dscale_store{oinw}=[];
end
disp('Still Working on step 1, wait for me')
parfor i=1:length(LocalizationsFinal)
%if length(LocalizationsFinal{i})<9000
disp('Still Working on step 1, wait for me')
%disp(['Percent Done=',num2str(i/length(LocalizationsFinal)),' with STEP 1'])
%Here we will gather the distance distrabutions from as many cells as
%posible. This will allow us to define the joint probability
%distribution more accuratly.
Z2 = pdist([[1:length(Frame_Information{i})]'*0,Frame_Information{i}(:)]);
D = (pdist(LocalizationsFinal{i}));
D_No_Blink=D([(Z2>Pre_A)]==1);
D_No_Blink=D([(Z2>Pre_A).*(Pre_A*5>Z2)]==1);
True_Distribuiton2=histcounts(D_No_Blink,bins,'Normalization','prob');
for w=1:Pre_A
D_Blink=D(Z2==w);
Temp_Distribution=((histcounts(D_Blink,bins,'Normalization','prob')));
y=Temp_Distribution;
t=[True_Distribuiton2; Distribution_for_Blink2];
F=@(x,xdata) x.*xdata(1,:)+(1-x).*xdata(2,:);
opts = optimset('Display','off');
x0=1;
[x,~,~,~,~] = lsqcurvefit(F,x0,t,y,[],[],opts);
D_Scale=x;
Dscale_store{i}(w)=D_Scale;
end
%end
end
Dscale_store2=[];
for oinw=1:length(LocalizationsFinal)
Dscale_store2(oinw,:)=Dscale_store{oinw};
end
if length(LocalizationsFinal)>1
X_overall=mean(Dscale_store2);
Dscale_store=mean(Dscale_store2);
else
X_overall=(Dscale_store2);
Dscale_store=(Dscale_store2);
end
%Here we are going to determine the matrix M, it is better to do it over
%all of the images as there is less error if you have a lower number of
%localizations in one image.
Dscale_store(Dscale_store>1)=1;
Dscale_store(Dscale_store<0)=.0000001;
Dscale_store(end)=1;
Deviation_in_Probabilityt={};
for oinw=1:length(LocalizationsFinal)
Deviation_in_Probabilityt{oinw}=[];
end
disp('Still going, Working on step 1')
parfor i=1:length(LocalizationsFinal)
disp('Still going, Working on step 1')
%disp(['Percent Done=',num2str(i/length(LocalizationsFinal)),' with STEP 1'])
%Here we will gather the distance distrabutions from as many cells as
%posible. This will allow us to define the joint probability
%distribution more accuratly.
Z2 = pdist([[1:length(Frame_Information{i})]'*0,Frame_Information{i}(:)]);
D = (pdist(LocalizationsFinal{i}));
D_No_Blink=D([(Z2>Pre_A)]==1);
D_No_Blink=D([(Z2>Pre_A).*(Pre_A*5>Z2)]==1);
True_Distribuiton=histcounts(D_No_Blink,bins,'Normalization','prob');
for w=1:Pre_A
%D_Blink=D(Z2==w);
% Temp_Distribution=((histcounts(D_Blink,bins,'Normalization','prob')));
D_Scale=Dscale_store(w);
Temp_Distribution2=Distribution_for_Blink2*(1-D_Scale)+True_Distribuiton*(D_Scale);
%See the equation in determining the probability section of the paper.
Combined=(Temp_Distribution2-D_Scale*True_Distribuiton)./(Temp_Distribution2);
Deviation_in_Probabilityt{i}(w,:)=Combined;
end
end
%Here we are going to go through and calculate M
if length(LocalizationsFinal)>1
M_mat=[];
for w=1:Pre_A
M_mat_t=[];
for i=1:length(Deviation_in_Probabilityt)
M_mat_t(i,:)=Deviation_in_Probabilityt{i}(w,:);
end
M_mat(w,:)=mean(M_mat_t);
end
else
M_mat=Deviation_in_Probabilityt{1};
end
%%
%We dont use this, but just in case you want to mess areound
M_mat2=[];
for w=1:Pre_A
M_mat_t=[];
for i=1:length(Deviation_in_Probabilityt)
M_mat_t(i,:)=Deviation_in_Probabilityt{i}(w,:);
end
M_mat2(w,:)=median(M_mat_t);
end
hey=1;