-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_optics.m
186 lines (164 loc) · 6.95 KB
/
cluster_optics.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
function [ SetOfClusters, RD, CD, order ] = cluster_optics(points, minpts, epsilon)
% This function computes a set of clusters based on the algorithm introduced in Figure 19 of
% Ankerst, Mihael, et al. "OPTICS: ordering points to identify the clustering structure."
% ACM Sigmod Record. Vol. 28. No. 2. ACM, 1999.
% Written by Alex Kendall
% University of Cambridge
% 18 Feb 2015
% http://mi.eng.cam.ac.uk/~agk34/
% This software is licensed under GPLv3, see included glpv3.txt.
% Input:
% points - input points to cluster where each point is a separate row and the columns are data dimensions
% minpts - the minimum points required to form a cluster
% epsilon - a percentage threshold to make a cluster
% Output:
% SetOfClusters - a struct containing each cluster's start and end index
% RD - each point's reachability distance
% CD - each point's core distance
% order - the order of points in the reachability graph
% Dependencies:
% This function requires optics.m from Michal Daszykowski's implementation of calculating the reachability distance for all points.
% For more details, refer to http://chemometria.us.edu.pl/index.php?goto=downloads
disp('Calculating reachability for all points.');
tic;
[RD,CD,order]=optics(points,minpts);
toc;
disp('Computing clusters.');
tic;
mib = 0;
i = 1;
SetOfSteepDownAreas = struct();
SetOfClusters = struct();
while i < size(points,1)-1
mib = max([mib, RD(order(i))]);
if RD(order(i))*(1-epsilon) >= RD(order(i+1))
% update mib values and filter down areas
for k=2:size(SetOfSteepDownAreas,2)
SetOfSteepDownAreas(k).mib = max(RD(order((SetOfSteepDownAreas(k).end+1):i)));
end
k=2;
while k<=size(SetOfSteepDownAreas,2)
if RD(order(SetOfSteepDownAreas(k).start))*(1-epsilon) < mib
if k==size(SetOfSteepDownAreas,2)
SetOfSteepDownAreas = SetOfSteepDownAreas(1:k-1);
else
SetOfSteepDownAreas = SetOfSteepDownAreas([1:k-1, k+1:size(SetOfSteepDownAreas,2)]);
end
else
k = k+1;
end
end
newD = size(SetOfSteepDownAreas,2)+1;
SetOfSteepDownAreas(newD).start = i;
SetOfSteepDownAreas(newD).mib = 0;
% find end of downward area
while i < size(points,1)-1
if RD(order(i))*(1-epsilon) >= RD(order(i+1))
i = i+1;
else
j = i;
while j < size(points,1)-1
if or(j-i>minpts, RD(order(j)) < RD(order(j+1)))
% if the downward area that isn't steep is longer than minpts, or no longer downward
j=-1;
break;
elseif RD(order(j))*(1-epsilon) >= RD(order(j+1))
% if it is a steepdownward area
break;
else
j = j+1;
end
end
if or(j == -1, j == size(points,1)-1)
% end of downward area
break;
else
i = j;
end
end
end
SetOfSteepDownAreas(newD).end = i-1;
mib = RD(order(i));
elseif RD(order(i)) <= RD(order(i+1))*(1-epsilon)
% Up area
upAreaStart = i;
% update mib values and filter down areas
for k=2:size(SetOfSteepDownAreas,2)
SetOfSteepDownAreas(k).mib = max(RD(order(SetOfSteepDownAreas(k).end:i)));
end
k=2;
while k<=size(SetOfSteepDownAreas,2)
if RD(order(SetOfSteepDownAreas(k).start))*(1-epsilon) < mib
if k==size(SetOfSteepDownAreas,2)
SetOfSteepDownAreas = SetOfSteepDownAreas(1:k-1);
else
SetOfSteepDownAreas = SetOfSteepDownAreas([1:k-1, k+1:size(SetOfSteepDownAreas,2)]);
end
else
k = k+1;
end
end
% find end of upward area
while i < size(points,1)-1
if RD(order(i)) <= RD(order(i+1))*(1-epsilon)
i = i+1;
else
j = i;
while j < size(points,1)-1
if or(j-i>minpts, RD(order(j)) > RD(order(j+1)))
% if the upward area that isn't steep is longer than minpts, or no longer upward
j=-1;
break;
elseif RD(order(j)) <= RD(order(j+1))*(1-epsilon)
% if it is a steepdownward area
break;
else
j = j+1;
end
end
if or(j == -1, j== size(points,1)-1)
% end of downward area
break;
else
i = j;
end
end
end
mib = RD(order(i));
for k=2:size(SetOfSteepDownAreas,2)
if RD(order(i))*(1-epsilon) > SetOfSteepDownAreas(k).mib
if and(RD(order(SetOfSteepDownAreas(k).start)) >= RD(upAreaStart) , RD(order(SetOfSteepDownAreas(k).end)) <= RD(order(i)))
if abs(RD(order(SetOfSteepDownAreas(k).start))-RD(order(i))) <= epsilon*max(RD(order(SetOfSteepDownAreas(k).start)),RD(order(i)))
% condition a
clusterStart = SetOfSteepDownAreas(k).start;
clusterEnd = i;
elseif RD(order(SetOfSteepDownAreas(k).start))*(1-epsilon) > RD(order(i))
% condition b
tmp = abs(RD(SetOfSteepDownAreas(k).start:SetOfSteepDownAreas(k).end)-RD(order(i)));
[~, clusterStart] = min(tmp); %index of closest value
clusterStart = clusterStart+SetOfSteepDownAreas(k).start-1;
clusterEnd = i;
elseif RD(order(SetOfSteepDownAreas(k).start)) < RD(order(i))*(1-epsilon)
% condition c
clusterStart = SetOfSteepDownAreas(k).start;
tmp = abs(RD(upAreaStart:i)-RD(order(SetOfSteepDownAreas(k).start)));
[~, clusterEnd] = min(tmp); %index of closest value
clusterEnd = clusterEnd+upAreaStart;
else
error('ERROR\n');
end
if abs(clusterEnd - clusterStart) >= minpts
newD = size(SetOfClusters,2)+1;
SetOfClusters(newD).start = clusterStart;
SetOfClusters(newD).end = clusterEnd;
end
end
end
end
else
i = i+1;
end
end
SetOfClusters = SetOfClusters(2:size(SetOfClusters,2));
toc;
end