-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFIZZSCAN.ts
365 lines (333 loc) · 9.97 KB
/
FIZZSCAN.ts
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
/**
* DBSCAN - Density based clustering
*
* @author Lukasz Krawczyk <[email protected]>
* @copyright MIT
*/
/**
* FIZZSCAN class construcotr
* @constructor
*
* @param {Array} dataset
* @param {number} epsilon
* @param {number} minPts
* @param {function} distanceFunction
* @returns {FIZZSCAN}
*/
class FIZZSCAN {
dataset: Array<Array<any>>;
epsilon: number;
minPts: number;
distance: (p: Array<number>, q: Array<number>) => number;
forceIn: boolean;
clusters: Array<Array<number>>;
clusterCentroids: Array<Pair>;
noise: Array<number>;
noiseAssigned: Array<Array<number>>;
_visited: Array<number>;
_assigned: Array<number>;
_datasetLength: number;
constructor(dataset: Array<Array<number>>, epsilon: number, minPts: number, forceIn: boolean, distanceFunction?: (p: any, q: any) => number) {
/** @type {Array} */
this.dataset = dataset;
/** @type {number} */
this.epsilon = epsilon;
/** @type {number} */
this.minPts = minPts;
/** @type {function} */
if (!distanceFunction) {
distanceFunction = this._euclideanDistance;
}
this.distance = distanceFunction;
/** @type {boolean} */
this.forceIn = forceIn;
/** @type {Array} */
this.clusters = [];
/** @type {Array<Array<number>>} */
this.clusterCentroids = [];
/** @type {Array} */
this.noise = [];
/** @type {Array} */
this.noiseAssigned = [];
// temporary variables used during computation
/** @type {Array} */
this._visited = [];
/** @type {Array} */
this._assigned = [];
/** @type {number} */
this._datasetLength = dataset.length;
this.run(dataset, epsilon, minPts, forceIn, distanceFunction);
}
/******************************************************************************/
// public functions
/**
* Start clustering
*
* @param {Array} dataset
* @param {number} epsilon
* @param {number} minPts
* @param {function} distanceFunction
* @param {boolean} distanceFunction
* @returns {undefined}
* @access public
*/
run(dataset: Array<Array<number>>, epsilon: number, minPts: number, forceIn: boolean, distanceFunction?: (p: Array<number>, q: Array<number>) => number): Array<any> {
this._init(dataset, epsilon, minPts, forceIn, distanceFunction);
for (var pointId = 0; pointId < this._datasetLength; pointId++) {
// if point is not visited, check if it forms a cluster
if (this._visited[pointId] !== 1) {
this._visited[pointId] = 1;
// if closest neighborhood is too small to form a cluster, mark as noise
var neighbors: number[] = this._regionQuery(pointId);
if (neighbors.length < this.minPts) {
this.noise.push(pointId);
} else {
// create new cluster and add point
var clusterId = this.clusters.length;
this.clusters.push([]);
this._addToCluster(pointId, clusterId);
this._expandCluster(clusterId, neighbors);
}
}
}
//Declusters extremely small clusters in large datasets into noise.
if (this.dataset.length > 1000) {
var t = this.minPts * 2;
for (var clusterId = 0; clusterId < this.clusters.length; clusterId++) {
var tempCluster = this.clusters[clusterId];
if (tempCluster.length < t) {
for (var pointId = 0; pointId < tempCluster.length; pointId++) {
this.noise.push(tempCluster[pointId]);
}
this.clusters[clusterId] = [];
}
}
this.clusters = this.clusters.filter((e) => e.length > 0);
}
//Forms centroids of each generated cluster
for (var i = 0; i < this.clusters.length; i++) {
this.clusterCentroids.push(this._centroid(this.clusters[i]));
}
let reverseClusterLookup: Array<number> = [];
for (let i = 0; i < this._datasetLength; i++){
reverseClusterLookup.push(0);
}
for (let clusterID in this.clusters) {
for (let point of this.clusters[clusterID]) {
reverseClusterLookup[point] = Number(clusterID);
}
}
let tempStorage: number[][] = [];
for (let noisePointID of this.noise) {
let nearestNeighbor: number = this._nearestAssignedNeighbor(this.clusters.flat(), noisePointID);
tempStorage.push([noisePointID, Number(reverseClusterLookup[nearestNeighbor])]);
}
//Optionally forces noise points into clusters by grouping them into the nearest already-clustered point.
if (this.forceIn) {
this.noiseAssigned = [];
for (let i: number = 0; i < tempStorage.length; i++) {
let point: number[] = tempStorage[i];
this.noiseAssigned.push([point[0], point[1]]);
this._addToCluster(point[0], point[1]);
reverseClusterLookup[point[0]] = point[1];
}
this.noise = [];
}
else {
this.noiseAssigned = [];
for (let i: number = 0; i < tempStorage.length; i++) {
let point: number[] = tempStorage[i];
this.noiseAssigned.push([point[0], point[1]]);
reverseClusterLookup[point[0]] = point[1];
}
}
return this.clusters;
}
/******************************************************************************/
// protected functions
/**
* Set object properties
*
* @param {Array} dataset
* @param {number} epsilon
* @param {number} minPts
* @param {function} distance
* @param {boolean} forceIn
* @returns {undefined}
* @access protected
*/
_init(dataset: Array<Array<number>>, epsilon: number, minPts: number, forceIn: boolean, distance?: (p: any, q: any) => number): void {
if (dataset) {
if (!(dataset instanceof Array)) {
throw Error('Dataset must be of type array, ' +
typeof dataset + ' given');
}
this.dataset = dataset;
this.clusters = [];
this.noise = [];
this._datasetLength = dataset.length;
this._visited = new Array(this._datasetLength);
this._assigned = new Array(this._datasetLength);
}
if (epsilon) {
this.epsilon = epsilon;
}
if (minPts) {
this.minPts = minPts;
}
if (distance) {
this.distance = distance;
}
if (forceIn) {
this.forceIn = forceIn;
}
}
/**
* Expand cluster to closest points of given neighborhood
*
* @param {number} clusterId
* @param {Array} neighbors
* @returns {undefined}
* @access protected
*/
_expandCluster(clusterId: number, neighbors: number[]): void {
/**
* It's very important to calculate length of neighbors array each time,
* as the number of elements changes over time
*/
for (var i = 0; i < neighbors.length; i++) {
var pointId2 = neighbors[i];
if (this._visited[pointId2] !== 1) {
this._visited[pointId2] = 1;
var neighbors2 = this._regionQuery(pointId2);
if (neighbors2.length >= this.minPts) {
neighbors = this._mergeArrays(neighbors, neighbors2);
}
}
// add to cluster
if (this._assigned[pointId2] !== 1) {
this._addToCluster(pointId2, clusterId);
if (this.noise.indexOf(pointId2) > -1) {
this.noise.splice(this.noise.indexOf(pointId2), 1);
}
}
}
}
/**
* Add new point to cluster
*
* @param {number} pointId
* @param {number} clusterId
*/
_addToCluster(pointId: number, clusterId: number): void {
this.clusters[clusterId].push(pointId);
this._assigned[pointId] = 1;
}
/**
* Find all neighbors around given point
*
* @param {number} pointId,
* @param {number} epsilon
* @returns {Array}
* @access protected
*/
_regionQuery(pointId: number): number[] {
let neighbors: number[] = [];
let nnId: number = 0;
let nnDist: number = 0;
for (var id = 0; id < this._datasetLength; id++) {
var dist = this.distance(this.dataset[pointId], this.dataset[id]);
if (dist < this.epsilon) {
neighbors.push(id);
}
if (nnDist == 0) {
nnDist = dist;
nnId = id;
}
else if (nnDist > dist) {
nnDist = dist;
nnId = id;
}
}
return neighbors;
}
/******************************************************************************/
// helpers
/**
* @param {Array} a
* @param {Array} b
* @returns {Array}
* @access protected
*/
_mergeArrays(a: any[], b: any[]) {
var len: number = b.length;
for (var i = 0; i < len; i++) {
var P: any = b[i];
if (a.indexOf(P) < 0) {
a.push(P);
}
}
return a;
}
/**
* Calculate euclidean distance in multidimensional space
*
* @param {Array} p
* @param {Array} q
* @returns {number}
* @access protected
*/
_euclideanDistance(p: number[], q: number[]): number {
var sum: number = 0;
var i: number = Math.min(p.length, q.length);
while (i--) {
sum += (p[i] - q[i]) * (p[i] - q[i]);
}
return Math.sqrt(sum);
}
/**
* Calculate centroid of a group of points
*
* @param {Array} c
* @returns {Array}
* @access protected
*/
_centroid(c: Array<number>): Pair {
let sumX: number = 0;
let sumY: number = 0;
const n: number = c.length;
for (let i of c) {
sumX += this.dataset[i][0];
sumY += this.dataset[i][1];
}
return [sumX / n, sumY / n];
}
/**
* Given a list of clustered points and an outlier, returns the closest clustered point.
*
* @param {Array} datasetIds
* @param {number} pointId
* @returns {number}
* @access protected
*/
_nearestAssignedNeighbor(datasetIds: number[], pointId: number): number {
var nearest: number[] = [0, 0];
for (var id of datasetIds) {
if (nearest[1] == 0) {
nearest = [id, this._euclideanDistance(this.dataset[pointId], this.dataset[id])];
}
let distance = this._euclideanDistance(this.dataset[pointId], this.dataset[id]);
if (nearest[1] > distance) {
nearest = [id, distance];
}
}
return nearest[0];
}
};
type Pair = [number, number]
/*
if (typeof module !== 'undefined' && module.exports) {
module.exports = FIZZSCAN;
}
*/
export { FIZZSCAN };