-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSRcluster.m
More file actions
2206 lines (1976 loc) · 75.5 KB
/
Copy pathSRcluster.m
File metadata and controls
2206 lines (1976 loc) · 75.5 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
classdef SRcluster < handle
% SRcluster class written by Michael Wester and Keith Lidke (12/5/2016)
% <wester@math.unm.edu>
% The New Mexico Center for the Spatiotemporal Modeling of Cell Signaling
% University of New Mexico Health Sciences Center
% Albuquerque, New Mexico, USA 87131
% Copyright (c) 2015-2016 by Michael J. Wester and Keith A. Lidke
%
% Example main program:
%
% pixel2nm = 16000/150;
%
% load(...);
%
% Sigma_Reg = std(SRtest.DriftCorrect_XYShift) .* pixel2nm; % nm
% if isnan(Sigma_Reg)
% Sigma_Reg = [10, 10];
% end
%
% X = double(SRtest.Results.X) .* pixel2nm; % nm
% Y = double(SRtest.Results.Y) .* pixel2nm; % nm
% X_STD = double(SRtest.Results.X_STD) .* pixel2nm; % nm
% Y_STD = double(SRtest.Results.Y_STD) .* pixel2nm; % nm
%
% SRc = SRcluster();
% %SRc.PvalueStatistics = true;
% %SRc.PlotFigures = false;
% % clusterSR can work in nD.
% [xy_SR, sigma_SR, combined] = ...
% SRc.clusterSR([X, Y], [X_STD, Y_STD], Sigma_Reg);
% % Functions below assume 2D data.
% cutoffFigs = SRc.cutoffPlots();
% SRcollapseFig = SRc.plotSRcollapse();
% SRclusterFig = SRc.plotSRclusters();
% [results, analysisFigs] = SRc.analyzeSRclusters();
% =============================================================================
properties
% =============================================================================
Method = 'hierarchal_singlelabel'; % H-SET collapse method
%Method = 'keith_HC';
%Method = 'trivial';
Algorithm = 'hierarchalSimple'; minPts = 1; % default clustering setup
%Algorithm = 'DBSCAN_Daszykowski'; minPts = 3;
Dim = 2; % allowed values are 2 (2D) and 3 (3D)
PlotFigures = true; % plot various cluster related figures
Printing = true; % print various cluster statistics
ProduceLegend = true; % produce legends for some cluster figures
% If true, produce various Pvalue plots at the expense of slowing down the
% collapsing algorithm
PvalueStatistics = false;
Timing = true; % produce timings for clustering
Verbose = false; % print cluster sizes as found
% range of cutoff distances (nm) for analyses with cutoff as a parameter
Cutoff = 10 : 10 : 5000;
E = 30; % cutoff distance (nm)
LoS = 0.01; % level of significance
A_ROI = 1; % area of the ROI (nm^2)
ShrinkFactor = 0; % boundary shrink factor (0 = convex hull)
% experimental collapse parameters
MaxLD = inf; % maximum linkage distance allowed
MaxLN = inf; % maximum leaf nodes allowed
% reject properties
Histograms = false; % reject histograms
DetailedStatistics = false; % reject detailed statistics
% Voronoi properties
Alpha = 2; % Ratio of local density / overall density for a
% point's Voronoi region to be considered sufficiently
% dense for clustering purposes.
Valgorithm = 2; % Voronoi algorithm to apply:
% 1 [0] calculations consider Voronoi regions only
% 2 [1] calculations consider Voronoi regions and
% their adjacent neighbors
% 3 [1M] consider the median density of each cell
% and its neighbors
Plotting = false; % Produce Voronoi plots.
PtIDs = false; % Label the points in the plots.
% =============================================================================
end
properties(SetAccess = protected)
% =============================================================================
Sigma_Reg; % sigma registration
XY_orig; % input (x, y)
Sigma_orig; % input sigma
C_orig; % initial clusters at cutoff E
XY; % collapsed (x, y)
Sigma; % collapsed sigma
C; % clusters at cutoff E of collapsed data
Nodes_combined; % multiple nodes collapsed into single nodes
PDfig = []; % Pvalue vs (linkage distance, # leaf nodes) figures
PD = []; % Pvalue vs (linkage distance, # leaf nodes) data
% =============================================================================
end % properties(SetAccess = protected)
methods
% =============================================================================
function [XY, sigma, combined] = clusterSR(obj, XY_orig, sigma_orig, Sigma_Reg)
% Combine multiple clustered points into single localizations when appropriate
% via a top-down descent through a hierarchal dendrogram relationship between
% points.
% n is the original number of points and m is the dimension.
% n' is the final number of points after combinations have occurred.
%
% Inputs:
% XY_orig n x m matrix of coordinates
% sigma_orig n x m matrix of position uncertainties (1 standard deviation)
% Sigma_Reg 1 x m array of registration error (1 standard deviation)
%
% Outputs:
% XY n' x m final coordinate matrix
% sigma n' x m final position uncertainty matrix
% combined cell array of indices of combined points per cluster
obj.XY_orig = XY_orig;
obj.Sigma_orig = sigma_orig;
obj.Sigma_Reg = Sigma_Reg;
% Find clusters in the data.
if obj.Timing
tic
end
switch obj.Method
case 'trivial'
% For testing purposes only.
obj.XY = XY_orig;
obj.Sigma = sigma_orig;
case 'hierarchal_singlelabel'
% Collapse multiple emitters into single emitters.
if obj.PvalueStatistics
[obj.XY, obj.Sigma, obj.Nodes_combined] = ...
obj.hierarchalSingleLabelP(XY_orig, sigma_orig, ...
Sigma_Reg, obj.LoS);
else
[obj.XY, obj.Sigma, obj.Nodes_combined] = ...
SRcluster.hierarchalSingleLabel(XY_orig, sigma_orig, ...
Sigma_Reg, obj.LoS);
end
case 'keith_HC'
% Collapse multiple emitters into single emitters.
[obj.XY, obj.Sigma, obj.Nodes_combined] = ...
SRcluster.keith_HC(XY_orig, sigma_orig, Sigma_Reg, obj.LoS);
otherwise
error('Unknown method: %s\n', obj.Method);
end
if obj.Timing
toc
end
XY = obj.XY;
sigma = obj.Sigma;
combined = obj.Nodes_combined;
end
% =============================================================================
function set_XY_sigma(obj, Sigma_Reg, XY_orig, sigma_orig, XY, sigma, combined)
obj.Sigma_Reg = Sigma_Reg;
obj.XY_orig = XY_orig;
obj.Sigma_orig = sigma_orig;
obj.XY = XY;
obj.Sigma = sigma;
obj.Nodes_combined = combined;
end
% =============================================================================
function [cutoffFigs, M] = cutoffPlots(obj)
% Produce a series of plots by varying the cutoff distance in a hierarchal
% clustering algorithm.
if size(obj.XY, 1) <= 1
warning('cutoffPlots: 0 or 1 points provided!');
return
end
Z = linkage(obj.XY, 'single');
n_cutoffs = length(obj.Cutoff);
N = zeros(1, n_cutoffs); % # of clusters
M = zeros(1, n_cutoffs); % # of clusters with > 2 objects
A = zeros(1, n_cutoffs); % mean area of clusters with > 2 objects
Oa = zeros(1, n_cutoffs); % mean # of objects per cluster
Om = zeros(1, n_cutoffs); % mean # of objects per cluster with > 2 objects
for i = 1 : n_cutoffs
T = cluster(Z, 'Cutoff', obj.Cutoff(i), 'Criterion', 'distance', ...
'Depth', 2);
nC = max(T);
% Find the indices for each cluster.
C = [];
for j = 1 : nC
c = find(T == j);
C{j} = c';
end
centers = zeros(2, nC);
for j = 1 : nC
centers(:, j) = mean(obj.XY(C{j}, :), 1);
end
% # of clusters
N(i) = nC;
% Nm: # of clusters with multiple objects (> 2)
% Am: area of clusters with multiple objects (> 2)
% Oa: # of objects per cluster
% Om: # of objects per cluster with multiple objects (> 2)
Nm = 0;
Am = 0;
for j = 1 : nC
C_j = C{j};
nC_j = length(C_j);
Oa(i) = Oa(i) + nC_j;
if nC_j > 2
Nm = Nm + 1;
Om(i) = Om(i) + nC_j;
%[~, area] = convhull(obj.XY(C_j, 1), obj.XY(C_j, 2));
[~, area] = boundary(obj.XY(C_j, 1), obj.XY(C_j, 2), ...
obj.ShrinkFactor);
Am = Am + area;
end
end
M(i) = Nm;
A(i) = Am / Nm;
Oa(i) = Oa(i) / nC;
Om(i) = Om(i) / Nm;
% mean nearest neighbor center-to-center distances
min_c2c_dists = min(squareform(pdist(centers')) + 1.0e+10 * eye(nC));
if isempty(min_c2c_dists) % 1 cluster
D(i) = 0;
else % > 1 cluster
D(i) = mean(min_c2c_dists);
end
end
n_figs = 0;
% # of clusters vs. cutoff
n_figs = n_figs + 1;
if obj.PlotFigures
cutoffFigs(n_figs) = figure();
else
cutoffFigs(n_figs) = figure('Visible', 'off');
end
hold on
plot(obj.Cutoff, N, 'k-', 'LineWidth', 3);
plot(obj.Cutoff, M, 'm--', 'LineWidth', 3);
xlabel('cutoff (nm)');
ylabel('# of clusters');
legend('total', '# objs > 2', 'Location', 'NorthEast');
hold off
% # of objects per clusters vs. cutoff
n_figs = n_figs + 1;
if obj.PlotFigures
cutoffFigs(n_figs) = figure();
else
cutoffFigs(n_figs) = figure('Visible', 'off');
end
hold on
plot(obj.Cutoff, Oa, 'k-', 'LineWidth', 3);
plot(obj.Cutoff, Om, 'm--', 'LineWidth', 3);
xlabel('cutoff (nm)');
ylabel('# of objects per cluster');
legend('all', '# objs > 2', 'Location', 'SouthEast');
hold off
% mean cluster area vs. cutoff
n_figs = n_figs + 1;
if obj.PlotFigures
cutoffFigs(n_figs) = figure();
else
cutoffFigs(n_figs) = figure('Visible', 'off');
end
hold on
plot(obj.Cutoff, A, 'k-', 'LineWidth', 3);
xlabel('cutoff (nm)');
ylabel('mean cluster area (nm^2)');
hold off
% mean nearest neighbor center-to-center distances vs. cutoff
n_figs = n_figs + 1;
if obj.PlotFigures
cutoffFigs(n_figs) = figure();
else
cutoffFigs(n_figs) = figure('Visible', 'off');
end
hold on
plot(obj.Cutoff, D, 'k-', 'LineWidth', 3);
xlabel('cutoff (nm)');
ylabel('mean nearest neighbor center-to-center distances (nm)');
hold off
end
% =============================================================================
function E = chooseCutoff(obj)
% Choose a cutoff value from the peak of the curve of the number of clusters
% with multiple objects vs. cutoff distance. If more than one cutoff value is
% at the peak, choose the largest.
if size(obj.XY, 1) <= 1
warning('chooseCutoff: 0 or 1 points provided!');
return
end
Z = linkage(obj.XY, 'single');
n_cutoffs = length(obj.Cutoff);
M = zeros(1, n_cutoffs); % # of clusters with > 2 objects
for i = 1 : n_cutoffs
T = cluster(Z, 'Cutoff', obj.Cutoff(i), 'Criterion', 'distance', ...
'Depth', 2);
nC = max(T);
% Find the indices for each cluster.
C = [];
for j = 1 : nC
c = find(T == j);
C{j} = c';
end
% Nm: # of clusters with multiple objects (> 2)
Nm = 0;
for j = 1 : nC
C_j = C{j};
nC_j = length(C_j);
if nC_j > 2
Nm = Nm + 1;
end
end
M(i) = Nm;
end
%[Mmax, k] = max(M);
Mmax = max(M);
k = find(M == Mmax);
E = obj.Cutoff(k);
E = max(E);
fprintf('\ncutoff = %.0f nm (%d multiple object clusters)\n', E, Mmax);
if length(k) > 1
fprintf('--- chosen from cutoffs =');
fprintf(' %.0f', obj.Cutoff(k));
fprintf('\n');
end
end
% =============================================================================
function SRcollapseFig = plotSRcollapse(obj, xy_region)
% Plot original and collapsed data, using convex hull outlines to indicate
% which original observations were collapsed into a single localization.
if obj.PlotFigures
SRcollapseFig = figure();
else
SRcollapseFig = figure('Visible', 'off');
end
hold on
% Fake points used to make the legend.
plot(-1e+10, -1e+10, 'c.', 'MarkerSize', 10);
plot(-1e+10, -1e+10, 'b.', 'MarkerSize', 10);
plot(-1e+10, -1e+10, 'k-');
% Plot original data.
plot(obj.XY_orig(:, 1), obj.XY_orig(:, 2), 'c.', 'MarkerSize', 15);
% Plot processed data.
plot(obj.XY(:, 1), obj.XY(:, 2), 'b.', 'MarkerSize', 10);
% Plot convex hull of combined nodes.
n_eliminated = 0;
n_collapses = length(obj.Nodes_combined);
for i = 1 : n_collapses
i_collapsed = obj.Nodes_combined{i};
n_collapsed = length(i_collapsed);
n_eliminated = n_eliminated + n_collapsed - 1;
if n_collapsed == 2
plot(obj.XY_orig(i_collapsed, 1), obj.XY_orig(i_collapsed, 2), 'k-');
else
%k = convhull(obj.XY_orig(i_collapsed, 1), ...
% obj.XY_orig(i_collapsed, 2));
k = boundary(obj.XY_orig(i_collapsed, 1), ...
obj.XY_orig(i_collapsed, 2), obj.ShrinkFactor);
k = i_collapsed(k);
plot(obj.XY_orig(k, 1), obj.XY_orig(k, 2), 'k-');
end
end
if exist('xy_region', 'var')
if exist('xy_region', 'var')
plot(xy_region(:, 1), xy_region(:, 2), 'k-', 'LineWidth', 3);
end
xmin = min([obj.XY_orig(:, 1); xy_region(:, 1)]);
xmax = max([obj.XY_orig(:, 1); xy_region(:, 1)]);
ymin = min([obj.XY_orig(:, 2); xy_region(:, 2)]);
ymax = max([obj.XY_orig(:, 2); xy_region(:, 2)]);
else
xmin = min(obj.XY_orig(:, 1));
xmax = max(obj.XY_orig(:, 1));
ymin = min(obj.XY_orig(:, 2));
ymax = max(obj.XY_orig(:, 2));
end
dx = xmax - xmin;
dy = ymax - ymin;
axis([xmin - 0.05*dx, xmax + 0.05*dx, ymin - 0.05*dy, ymax + 0.05*dy]);
xlabel('x (nm)');
ylabel('y (nm)');
title(sprintf('collapses = %d, eliminated = %d (P > %.3f)', ...
n_collapses, n_eliminated, obj.LoS));
legend('original objects', 'collapsed objects', 'collapse boundaries', ...
'Location', 'Best');
hold off
end
% =============================================================================
function SRcollapseFig = plotSRcollapse3(obj, xy_region)
% Plot original and collapsed data, using convex hull outlines to indicate
% which original observations were collapsed into a single localization.
if obj.PlotFigures
SRcollapseFig = figure();
else
SRcollapseFig = figure('Visible', 'off');
end
hold on
% Fake points used to make the legend.
plot3(-1e+10, -1e+10, -1e+10, 'c.', 'MarkerSize', 10);
plot3(-1e+10, -1e+10, -1e+10, 'b.', 'MarkerSize', 10);
plot3(-1e+10, -1e+10, -1e+10, 'k-');
% Plot original data.
plot3(obj.XY_orig(:, 1), obj.XY_orig(:, 2), obj.XY_orig(:, 3), ...
'c.', 'MarkerSize', 15);
% Plot processed data.
plot3(obj.XY(:, 1), obj.XY(:, 2), obj.XY(:, 3), 'b.', 'MarkerSize', 10);
% Plot convex hull of combined nodes.
n_eliminated = 0;
n_collapses = length(obj.Nodes_combined);
for i = 1 : n_collapses
i_collapsed = obj.Nodes_combined{i};
n_collapsed = length(i_collapsed);
n_eliminated = n_eliminated + n_collapsed - 1;
if n_collapsed == 2
plot3(obj.XY_orig(i_collapsed, 1), obj.XY_orig(i_collapsed, 2), ...
obj.XY_orig(i_collapsed, 3), 'k-');
else
%k = convhull(obj.XY_orig(i_collapsed, 1), ...
% obj.XY_orig(i_collapsed, 2));
k = boundary(obj.XY_orig(i_collapsed, 1), ...
obj.XY_orig(i_collapsed, 2), ...
obj.XY_orig(i_collapsed, 3), obj.ShrinkFactor);
k = i_collapsed(k);
plot3(obj.XY_orig(k, 1), obj.XY_orig(k, 2), obj.XY_orig(k, 3), 'k-');
end
end
if exist('xy_region', 'var')
if exist('xy_region', 'var')
plot3(xy_region(:, 1), xy_region(:, 2), xy_region(:, 3), ...
'k-', 'LineWidth', 3);
end
xmin = min([obj.XY_orig(:, 1); xy_region(:, 1)]);
xmax = max([obj.XY_orig(:, 1); xy_region(:, 1)]);
ymin = min([obj.XY_orig(:, 2); xy_region(:, 2)]);
ymax = max([obj.XY_orig(:, 2); xy_region(:, 2)]);
zmin = min([obj.XY_orig(:, 3); xy_region(:, 3)]);
zmax = max([obj.XY_orig(:, 3); xy_region(:, 3)]);
else
xmin = min(obj.XY_orig(:, 1));
xmax = max(obj.XY_orig(:, 1));
ymin = min(obj.XY_orig(:, 2));
ymax = max(obj.XY_orig(:, 2));
zmin = min(obj.XY_orig(:, 3));
zmax = max(obj.XY_orig(:, 3));
end
dx = xmax - xmin;
dy = ymax - ymin;
dz = zmax - zmin;
axis([xmin - 0.05*dx, xmax + 0.05*dx, ymin - 0.05*dy, ymax + 0.05*dy, ...
zmin - 0.05*dz, zmax + 0.05*dz]);
xlabel('x (nm)');
ylabel('y (nm)');
zlabel('z (nm)');
title(sprintf('collapses = %d, eliminated = %d (P > %.3f)', ...
n_collapses, n_eliminated, obj.LoS));
legend('original objects', 'collapsed objects', 'collapse boundaries', ...
'Location', 'Best');
hold off
end
% =============================================================================
function SRclusterFig = plotSRclusters(obj)
% Plot original observations, collapsed localizations, clusters of collapsed
% points that pass the single label test so really should also be collapsed
% (pass 2 of the algorithm), and clusters of collapsed points that are
% considered to be true clusters.
switch obj.Algorithm
case 'hierarchalSimple'
obj.C = SRcluster.hierarchalSimple(obj.XY, obj.E);
nC = length(obj.C);
otherwise
c = Clustering();
if strcmp(obj.Algorithm, 'Voronoi')
c.Alpha = obj.Alpha;
c.Valgorithm = obj.Valgorithm;
c.Plotting = obj.Plotting;
c.PtIDs = obj.PtIDs;
end
[nC, obj.C, ~, ptsI] = ...
c.cluster(obj.Algorithm, obj.XY, obj.E, obj.minPts);
for i = 1 : length(ptsI)
obj.C{nC + i} = ptsI(i);
end
nC = nC + length(ptsI);
end
collapsed = false;
clustered = false;
% Check each cluster in turn to see if it is likely that it really
% represents a single label.
if obj.PlotFigures
SRclusterFig = figure();
else
SRclusterFig = figure('Visible', 'off');
end
hold on
% Fake points used to make the legend.
plot(-1e+10, -1e+10, 'c.', 'MarkerSize', 10);
plot(-1e+10, -1e+10, 'b.', 'MarkerSize', 10);
plot(-1e+10, -1e+10, 'g-', 'MarkerSize', 10);
plot(-1e+10, -1e+10, 'r-', 'MarkerSize', 10);
% Plot original data.
plot(obj.XY_orig(:, 1), obj.XY_orig(:, 2), 'c.', 'MarkerSize', 10);
% Look for clusters.
xmin = 1.0e+10;
ymin = 1.0e+10;
xmax = -1.0e+10;
ymax = -1.0e+10;
for i = 1 : nC
C_i = obj.C{i};
xmin = min(xmin, min(obj.XY(C_i, 1)));
ymin = min(ymin, min(obj.XY(C_i, 2)));
xmax = max(xmax, max(obj.XY(C_i, 1)));
ymax = max(ymax, max(obj.XY(C_i, 2)));
nC_i = length(C_i);
[Pvalue, XY_wm, sigma_wm] = ...
SRcluster.singleLabelTest(obj.XY(C_i, :), obj.Sigma(C_i, :), ...
obj.Sigma_Reg);
if obj.Verbose
fprintf('[%3d] Cluster of %3d: Pvalue = %f', i, nC_i, Pvalue);
end
% A cluster of 1, so already a single label.
if nC_i == 1
if obj.Verbose
fprintf(' *');
end
% Collapsed to a single label.
elseif Pvalue > obj.LoS
collapsed = true;
if obj.Verbose
fprintf(' *');
end
% Draw outline.
if nC_i == 2
plot(obj.XY(C_i, 1), obj.XY(C_i, 2), 'r-', 'LineWidth', 3);
elseif nC_i > 2
%k = convhull(obj.XY(C_i, 1), obj.XY(C_i, 2));
k = boundary(obj.XY(C_i, 1), obj.XY(C_i, 2), obj.ShrinkFactor);
plot(obj.XY(C_i(k), 1), obj.XY(C_i(k), 2), 'r-', 'LineWidth', 3);
end
% Cluster containing multiple labels.
else
clustered = true;
% Draw outline.
if nC_i == 2
plot(obj.XY(C_i, 1), obj.XY(C_i, 2), 'g-', 'LineWidth', 3);
elseif nC_i > 2
%k = convhull(obj.XY(C_i, 1), obj.XY(C_i, 2));
k = boundary(obj.XY(C_i, 1), obj.XY(C_i, 2), obj.ShrinkFactor);
plot(obj.XY(C_i(k), 1), obj.XY(C_i(k), 2), 'g-', 'LineWidth', 3);
end
end
if obj.Verbose
fprintf('\n');
end
end
% Plot processed data.
plot(obj.XY(:, 1), obj.XY(:, 2), 'b.', 'MarkerSize', 10);
dx = xmax - xmin;
dy = ymax - ymin;
axis([xmin - 0.05*dx, xmax + 0.05*dx, ymin - 0.05*dy, ymax + 0.05*dy]);
xlabel('x (nm)');
ylabel('y (nm)');
title(sprintf('(P > %.3f, cutoff = %.0f)', obj.LoS, obj.E));
if collapsed
legend('original objects', 'collapsed objects', ...
'multiple label cluster', 'single label cluster', ...
'Location', 'Best');
elseif clustered
legend('original objects', 'collapsed objects', ...
'multiple label cluster', 'Location', 'Best');
else
legend('original objects', 'collapsed objects', 'Location', 'Best');
end
hold off
end
% -----------------------------------------------------------------------------
function SRclusterFig = plotSRclusters3(obj)
% Plot original observations, collapsed localizations, clusters of collapsed
% points that pass the single label test so really should also be collapsed
% (pass 2 of the algorithm), and clusters of collapsed points that are
% considered to be true clusters.
switch obj.Algorithm
case 'hierarchalSimple'
obj.C = SRcluster.hierarchalSimple(obj.XY, obj.E);
nC = length(obj.C);
otherwise
c = Clustering();
if strcmp(obj.Algorithm, 'Voronoi')
c.Alpha = obj.Alpha;
c.Valgorithm = obj.Valgorithm;
c.Plotting = obj.Plotting;
c.PtIDs = obj.PtIDs;
end
[nC, obj.C, ~, ptsI] = ...
c.cluster(obj.Algorithm, obj.XY, obj.E, obj.minPts);
for i = 1 : length(ptsI)
obj.C{nC + i} = ptsI(i);
end
nC = nC + length(ptsI);
end
collapsed = false;
clustered = false;
% Check each cluster in turn to see if it is likely that it really
% represents a single label.
if obj.PlotFigures
SRclusterFig = figure();
else
SRclusterFig = figure('Visible', 'off');
end
hold on
% Fake points used to make the legend.
plot3(-1e+10, -1e+10, -1e+10, 'c.', 'MarkerSize', 10);
plot3(-1e+10, -1e+10, -1e+10, 'b.', 'MarkerSize', 10);
plot3(-1e+10, -1e+10, -1e+10, 'g-', 'MarkerSize', 10);
plot3(-1e+10, -1e+10, -1e+10, 'r-', 'MarkerSize', 10);
% Plot original data.
plot3(obj.XY_orig(:, 1), obj.XY_orig(:, 2), obj.XY_orig(:, 3), ...
'c.', 'MarkerSize', 10);
% Look for clusters.
xmin = 1.0e+10;
ymin = 1.0e+10;
zmin = 1.0e+10;
xmax = -1.0e+10;
ymax = -1.0e+10;
zmax = -1.0e+10;
for i = 1 : nC
C_i = obj.C{i};
xmin = min(xmin, min(obj.XY(C_i, 1)));
ymin = min(ymin, min(obj.XY(C_i, 2)));
zmin = min(zmin, min(obj.XY(C_i, 3)));
xmax = max(xmax, max(obj.XY(C_i, 1)));
ymax = max(ymax, max(obj.XY(C_i, 2)));
zmax = max(zmax, max(obj.XY(C_i, 3)));
nC_i = length(C_i);
[Pvalue, XY_wm, sigma_wm] = ...
SRcluster.singleLabelTest(obj.XY(C_i, :), obj.Sigma(C_i, :), ...
obj.Sigma_Reg);
if obj.Verbose
fprintf('[%3d] Cluster of %3d: Pvalue = %f', i, nC_i, Pvalue);
end
% A cluster of 1, so already a single label.
if nC_i == 1
if obj.Verbose
fprintf(' *');
end
% Collapsed to a single label.
elseif Pvalue > obj.LoS
collapsed = true;
if obj.Verbose
fprintf(' *');
end
% Draw outline.
if nC_i == 2
plot3(obj.XY(C_i, 1), obj.XY(C_i, 2), obj.XY(C_i, 3), ...
'r-', 'LineWidth', 3);
elseif nC_i > 2
%k = convhull(obj.XY(C_i, 1), obj.XY(C_i, 2));
k = boundary(obj.XY(C_i, 1), obj.XY(C_i, 2), obj.XY(C_i, 3), ...
obj.ShrinkFactor);
plot3(obj.XY(C_i(k), 1), obj.XY(C_i(k), 2), obj.XY(C_i(k), 3), ...
'r-', 'LineWidth', 3);
end
% Cluster containing multiple labels.
else
clustered = true;
% Draw outline.
if nC_i == 2
plot3(obj.XY(C_i, 1), obj.XY(C_i, 2), obj.XY(C_i, 3), ...
'g-', 'LineWidth', 3);
elseif nC_i > 2
%k = convhull(obj.XY(C_i, 1), obj.XY(C_i, 2));
k = boundary(obj.XY(C_i, 1), obj.XY(C_i, 2), obj.XY(C_i, 3), ...
obj.ShrinkFactor);
plot3(obj.XY(C_i(k), 1), obj.XY(C_i(k), 2), obj.XY(C_i(k), 3), ...
'g-', 'LineWidth', 3);
end
end
if obj.Verbose
fprintf('\n');
end
end
% Plot processed data.
plot3(obj.XY(:, 1), obj.XY(:, 2), obj.XY(:, 3), 'b.', 'MarkerSize', 10);
dx = xmax - xmin;
dy = ymax - ymin;
dz = zmax - zmin;
axis([xmin - 0.05*dx, xmax + 0.05*dx, ymin - 0.05*dy, ymax + 0.05*dy, ...
zmin - 0.05*dz, zmax + 0.05*dz]);
xlabel('x (nm)');
ylabel('y (nm)');
zlabel('z (nm)');
title(sprintf('(P > %.3f, cutoff = %.0f)', obj.LoS, obj.E));
if collapsed
legend('original objects', 'collapsed objects', ...
'multiple label cluster', 'single label cluster', ...
'Location', 'Best');
elseif clustered
legend('original objects', 'collapsed objects', ...
'multiple label cluster', 'Location', 'Best');
else
legend('original objects', 'collapsed objects', 'Location', 'Best');
end
hold off
end
% =============================================================================
function [results, analysisFigs] = analyzeSRclusters(obj)
% Compute statistics for original observations and collapsed localizations.
% Pass 2 of the algorithm (collapsing any clusters detected after the initial
% collapse that pass the single label test) is performed with the results given
% in the final analysis.
%
% numclust_orig(1,2,3) number of singles, doubles, multiples in original
% data (observations)
% singles_per_total_clusters_orig
% fraction of singles as clusters
% numobjs_orig(1,2,3) number of objects in single, double, multiple
% clusters
% numobjs_per_multiple_cluster_orig
% average number of objects per multiple (>= 3)
% cluster
% n_collapses_pass1 number of clusters that were collapsed by the
% hierarchal top-down algorithm (pass 1)
% numobjs_collapsed_pass1 number of objects in each cluster before it
% was collapsed _including_ singlets
% n_objs_collapsed_pass1 sum(numobjs_collapsed_pass1(1 : n_collapses))
% NOTE: Therefore, the number of objects that were eliminated =
% n_objs_collapsed_pass1 - n_collapses_pass1
% n_objs_per_cluster_orig number of objects per original cluster
% n_objs_per_cluster number of objects per collapsed cluster
% n_collapses_pass2 number of clusters collapsed in pass 2
% n_objs_collapsed_pass2 total number of objects in the clusters before
% they were collapsed
% NOTE: Thus, the number of objects eliminated =
% n_objs_collapsed_pass2 - n_collapsed_pass2
% numclust(1,2,3) number of singles, doubles, multiples in the
% collapsed data (localizations)
% singles_per_total_clusters fraction of singles as clusters
% numdensity(1,2,3) numclust(1,2,3) / ROI area
% numobjs(1,2,3) number of objects in single, double, multiple
% clusters
% numobjs_per_multiple_cluster
% number of objects per multiple (>= 3) cluster
% cluster_numobjs number of objects per cluster for all clusters
% cluster_areas area per cluster for all clusters
% cluster_radii equivalent radii per cluster for all clusters
% cluster_perimeters perimeter per cluster for all clusters
% cluster_compactness 4 pi area / perimeter^2 per cluster for all
% clusters
% area_per_multiple_cluster average area per multiple (>= 3) cluster
% radius_per_2orMore_cluster average radius per double and multiple cluster
% numobjs_per_area number of objects in each multiple cluster
% divided by its area
% centers centers of each final cluster
% min_c2c_dists nearest neighbor center-to-center distances
% between all cluster centers
% min_c2c_dist(1,2,3,4) minimum nearest neighbor center-to-center
% distances between centers of clusters of
% (1) singles, (2) doubles, (3) multiples, (4) all
% singlets indices of the original objects forming each
% singlet
% multiples indices of the original objects forming each
% multiple
% singlets_always fraction of singlets that were always singlets
switch obj.Algorithm
case 'hierarchalSimple'
obj.C_orig = SRcluster.hierarchalSimple(obj.XY_orig, obj.E);
obj.C = SRcluster.hierarchalSimple(obj.XY, obj.E);
nC_orig = length(obj.C_orig);
nC = length(obj.C);
% Find the center of each final cluster.
centers = zeros(obj.Dim, nC);
for i = 1 : nC
centers(:, i) = mean(obj.XY(obj.C{i}, :), 1);
end
otherwise
c = Clustering();
if strcmp(obj.Algorithm, 'Voronoi')
c.Alpha = obj.Alpha;
c.Valgorithm = obj.Valgorithm;
c.Plotting = obj.Plotting;
c.PtIDs = obj.PtIDs;
end
[nC, obj.C, centers, ptsI] = ...
c.cluster(obj.Algorithm, obj.XY, obj.E, obj.minPts);
if strcmp(obj.Algorithm, 'DBSCAN_Daszykowski_noE')
obj.E = c.E;
end
[nC_orig, obj.C_orig, ~, ptsI_orig] = ...
c.cluster(obj.Algorithm, obj.XY_orig, obj.E, obj.minPts);
for i = 1 : length(ptsI_orig)
obj.C_orig{nC_orig + i} = ptsI_orig(i);
end
nC_orig = nC_orig + length(ptsI_orig);
for i = 1 : length(ptsI)
obj.C{nC + i} = ptsI(i);
centers(:, nC + i) = obj.XY(ptsI(i), :);
end
nC = nC + length(ptsI);
end
% Compute the number of objects in each cluster.
nCi_orig = arrayfun(@(x) length(cell2mat(x)), obj.C_orig);
nCi = arrayfun(@(x) length(cell2mat(x)), obj.C);
% (1) single, (2) double, (>2) multiple:
%
% Number of clusters containing 1/2/>2 objects.
numclust_orig = zeros(1, 3);
numclust = zeros(1, 3);
% Total number of objects in clusters containing 1/2/>2 objects.
numobjs_orig = zeros(1, 3);
numobjs = zeros(1, 3);
% Number of objects per cluster.
n_objs = [];
% Area per cluster.
areas = [];
% Equivalent radii per cluster.
radii = [];
% Perimeter per cluster.
perims = [];
% Compactness per cluster.
compactness = [];
% Total area of multiple clusters.
area = 0;
% Total equivalent radii of double and multiple clusters.
radius = 0;
% Minimum center-to-center distances between objects for clusters containing
% 1/2/>2/all objects.
min_c2c_dist = zeros(1, 4);
% Number of objects in a cluster per area of the cluster for each multiple
% cluster.
numobjs_per_area = [];
% --------------------------------------------------------------------------
% Analyze the original clusters.
for i = 1 : nC_orig
nC_i = length(obj.C_orig{i});
if nC_i == 1
numclust_orig(1) = numclust_orig(1) + 1;
numobjs_orig(1) = numobjs_orig(1) + 1;
elseif nC_i == 2
numclust_orig(2) = numclust_orig(2) + 1;
numobjs_orig(2) = numobjs_orig(2) + 2;
else
numclust_orig(3) = numclust_orig(3) + 1;
numobjs_orig(3) = numobjs_orig(3) + nC_i;
end
end
% --------------------------------------------------------------------------
if obj.Printing
fprintf('\n');
fprintf('Original Analysis:\n\n');
end
results.numclust_orig = numclust_orig;
if obj.Printing
fprintf('# of clusters = %d\n', nC_orig);
fprintf('# of singles = %d\n', numclust_orig(1));
fprintf('# of doubles = %d\n', numclust_orig(2));
fprintf('# of multiples = %d\n', numclust_orig(3));
end
results.singles_per_total_clusters_orig = numclust_orig(1) / nC_orig;
if obj.Printing
fprintf('singles / total clusters = %.3f\n', ...
results.singles_per_total_clusters_orig);
end
results.numobjs_orig = numobjs_orig;
if numclust_orig(3) ~= 0
results.numobjs_per_multiple_cluster_orig = ...
numobjs_orig(3) / numclust_orig(3);
else
results.numobjs_per_multiple_cluster_orig = [];
end
if obj.Printing
fprintf('# objects (TOTAL) = %d\n', sum(numobjs_orig));
fprintf('# objects (singles) = %d\n', numobjs_orig(1));
fprintf('# objects (doubles) = %d\n', numobjs_orig(2));
fprintf('# objects (multiples) = %d\n', numobjs_orig(3));
fprintf('# objects / multiple cluster = %.3f\n', ...
results.numobjs_per_multiple_cluster_orig);
end
% --------------------------------------------------------------------------
% Analyze the combined nodes (collapse analysis).
%
% n_collapses is the number of clusters that were collapsed by the
% hierarchal top-down algorithm (pass 1)
% numobjs_collapsed is the number of objects in each cluster before it was
% collapsed _including_ singlets
% Therefore, the number of objects that were eliminated =
% sum(numobjs_collapsed(1 : n_collapses)) - n_collapses.
[analysisFigs{1}, n_collapses, numobjs_collapsed] = ...
obj.collapseHistogram(numobjs_orig);
results.n_collapses_pass1 = n_collapses;
results.numobjs_collapsed_pass1 = numobjs_collapsed;
results.n_objs_collapsed_pass1 = sum(numobjs_collapsed);
% --------------------------------------------------------------------------
singlets = [];
multiples = [];
n_singlets = 0;
n_multiples = 0;
% Find the singlets that were originally singlets.
sing = setdiff(1 : size(obj.XY_orig, 1), [obj.Nodes_combined{:}]);
n_combined = length(obj.Nodes_combined);
n_collapsed = 0;
n_objs_collapsed = 0;
% Check each final cluster in turn to see if it is likely that it really
% represents a single label and the analyze the results.
%
% n_collapsed is the number of clusters collapsed in pass 2
% n_objs_collapsed is the total number of objects in the clusters before
% they were collapsed
% Thus, the number of objects eliminated = n_objs_collapsed - n_collapsed.
% singlets are the indices of the original objects forming each
% singlet
% multiples are the indices of the original objects forming each
% multiple
% singlets_always fraction of singlets that were always singlets
for i = 1 : nC
C_i = obj.C{i};