-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclureal.py
executable file
·573 lines (484 loc) · 18.3 KB
/
clureal.py
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
"""
======================
CluReAL algorithm v2.0
FIV, Nov 2020
======================
"""
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
from scipy.spatial import distance_matrix
from scipy.spatial import distance
from KDEpy import FFTKDE
from scipy.signal import find_peaks
from itertools import combinations, permutations
from sklearn.cluster import MiniBatchKMeans
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
import math
import statistics
# ********* CluReAL parameters ***********
MIN_REL_DENSITY = -0.8
MIN_CARDINALITY_R = 0.005
OUTLIER_SENS = 0.75
REP = 0
PRUN_LEVEL = 0 #0:normal, 1:high class-overlap expected, 2: very-high class-overlap expected
# ***** Symbolic Keys parameters *********
SIMILAR_DENSITY_TH = 3
RADII_RATIO_TH = 2
# ****************************************
class ClusterContext: # --- Cluster Context class ---
def __init__(self,k,m):
self.k = k # number of clusters (int)
self.centroids = np.zeros([k,m]) # matrix (kxm) with robust cluster centers
self.mass = np.zeros(k) # cluster mass or cardinality (k-size array)
self.mnDa = np.zeros(k) # cluster mean intra distance (k-size array)
self.mdDa = np.zeros(k) # cluster median intra distance (k-size array)
self.sdDa = np.zeros(k) # cluster std-dev intra distance (k-size array)
self.De = np.zeros([k,k]) # cluster inter distance matrix (k x k matrix)
self.outliers = 0 # number of outliers / total data points (float)
class RefinementContext: # --- Cluster Refinement Context class ---
def __init__(self,k):
self.mm = np.zeros(k) # multimodality flags for each cluster (k-size array)
self.kdens = np.zeros(k) # cluster relative densities (k-size array)
self.Odens = np.zeros(k) # global/overall density (float)
self.kinship = np.zeros([k,k]) # cluster kinship indices (k x k matrix): 4-unrelated, 3-friends, 2-relatives, 1-parent and child, 0-itself
class GValidity: # --- GValidity class ---
def __init__(self,k):
self.Gstr = 0 # strict global index (float)
self.Grex = 0 # relaxed global index (float)
self.Gmin = 0 # minimum global index (float)
self.oi_st = np.zeros(k) # individual strict indices (array of floats)
self.oi_rx = np.zeros(k) # individual relaxed indices (array of floats)
self.oi_mn = np.zeros(k) # individual min indices (array of floats)
self.extR = np.zeros(k) # extended radii (array of floats)
self.strR = np.zeros(k) # strict radii (array of floats)
self.volR = np.zeros(k) # times that the extended radious is in the core radious (array of floats)
def sample_size (N, s, e):
z=1.96
num = N * pow(z,2) * pow(s,2)
den = (N-1) * pow(e,2) + pow(z,2) * pow(s,2)
n = int(math.floor(num/den))
return n
def coreset_extractor(X, y, x=5, qv=0.3, k=None, q=None, chunksize=None):
[m, n] = X.shape
if k is None:
Xt = StandardScaler().fit_transform(X)
pca = PCA(n_components=2)
Xp = pca.fit_transform(Xt)
sigma = np.std(Xp)
if sigma<1:
sigma=1
error = 0.1*np.std(Xp);
k = sample_size( m, sigma, error )
if chunksize is None:
chunksize = m
index = np.random.permutation(m)
O = X[index[0:k]]
ind = index[0:k]
P = np.zeros(k)
for i in range(0,m,chunksize):
dist = distance.cdist(X[i:(i+chunksize)], O)
dist_sorted = np.argsort(dist, axis=1)
closest = dist_sorted[:,0:x].flatten()
P += np.count_nonzero (closest[:,np.newaxis] == np.arange(k), 0)
if q is None:
q = np.quantile(P, qv)
O = O[P>=q]
ind = ind[P>=q]
return O,y[ind],ind
def moving_average(x, w):
return np.convolve(x, np.ones(w), 'valid') / w
def create_circle(x,y,r,f,c,v):
circle= plt.Circle((x,y), radius = r, fill=f, ec='k', fc=c, visible=v)
return circle
def create_rectangle(x,y,w,h,f,c,v):
rectangle = plt.Rectangle((x,y), w, h, fill=f, ec='k', fc=c, visible=v)
return rectangle
def add_shape(patch):
ax=plt.gca()
ax.add_patch(patch)
plt.axis('scaled')
def draw_symbol(cc, gv, rc):
# inputs
# cc: cluster context (ClusterContext)
# gv: goi validity indices (GValidty)
# rc: cluster refinement context (RefinementContext)
k, outliers = cc.k, cc.outliers
Gstr, Grex, Gmin, volr = gv.Gstr, gv.Grex, gv.Gmin, gv.volR
mm,kinship,kdens = rc.mm, rc.kinship, rc.kdens
child = np.where(kinship == 1)
densdiff = np.absolute(np.nanmax(kdens)-np.nanmin(kdens))/np.absolute(np.minimum( np.nanmax(kdens),np.nanmin(kdens) ))
volr = np.nanmean(volr)
x_ec, x_ec2, x_cc, x_cc2, x_ccup, x_ech = 0, 0, 0, 0, 0, 0
y_ec, y_cc = 0, 0
v_ec, v_ec2, v_cc, v_cc2, v_ccup, v_ech, v_r1, v_l1, f_ec2, v_eov = False, False, False, False, False, False, False, False, False, False
v_ol, v_om, v_oh = False, False, False
c_ec2 = 'k'
if (sum(mm)>0):
y_cc, v_ccup = -0.08, True
else:
y_cc = 0
if len(child[0])>0:
v_ech = True
if Gmin < 0 and k>2 and Gstr>=0 and Grex>=1:
v_eov = True
if k==1:
x_ec, y_ec, v_ec, v_cc = 0, 0, True, True
elif (k==2 and len(child[0])>0):
x_ec, y_ec, v_ec, v_cc = 0, 0, True, True
else:
if densdiff>=SIMILAR_DENSITY_TH:
c_ec2, f_ec2 = 'lightgrey', True
if Gstr>=1:
x_ec, x_cc, x_ccup, x_ech, v_ec, v_cc = -0.5, -0.5, -0.5, -0.7, True, True
x_ec2, x_cc2, v_ec2, v_cc2 = 0.5, 0.5, True, True
elif Gstr>0:
if Grex>1:
x_ec, x_cc, x_ccup, x_ech, v_ec, v_cc = -0.4, -0.4, -0.4, -0.6, True, True
x_ec2, x_cc2, v_ec2, v_cc2 = 0.4, 0.4, True, True
else:
x_ec, x_cc, x_ccup, x_ech, v_ec, v_cc = -0.3, -0.3, -0.3, -0.5, True, True
x_ec2, x_cc2, v_ec2, v_cc2 = 0.3, 0.3, True, True
else:
if Grex>1:
x_ec, x_cc, x_ccup, x_ech, v_ec, v_cc = -0.15, -0.15, -0.15, -0.35, True, True
x_ec2, x_cc2, v_ec2, v_cc2 = 0.15, 0.15, True, True
elif Grex>0:
v_r1 = True
x_cc, x_ccup, x_ech, v_cc = -0.2, -0.2, -0.2, True
x_cc2, v_cc2 = 0.2, True
else:
v_l1, v_r1 = True, True
v_ccup, v_cc, v_cc2, v_ech = False, False, False, False
if volr<RADII_RATIO_TH:
v_cc2 = False
if np.sum(mm)==0:
v_cc = False
if outliers>0:
v_ol = True
if outliers>0.05:
v_om = True
if outliers>0.20:
v_oh = True
r1 = create_rectangle(-0.6,-0.4,1.2,0.8,True,'lightgrey',v_r1)
ec2 = create_circle(x_ec2,0,0.4,f_ec2,c_ec2,v_ec2)
ec = create_circle(x_ec,y_ec,0.4,False,'k',v_ec)
ecov = create_circle(x_ec-0.30,0.25,0.12,False,'k',v_eov)
ech = create_circle(x_ech,-0.20,0.08,False,'k',v_ech)
cc = create_circle(x_cc,y_cc,0.03,True,'k',v_cc)
cc2 = create_circle(x_cc2,0,0.03,True,'k',v_cc2)
ccup = create_circle(x_ccup,0.08,0.03,True,'k',v_ccup)
o1 = create_circle(-0.15,-0.45,0.02,True,'k',v_om)
o2 = create_circle(-0.3,-0.50,0.02,True,'k',v_oh)
o3 = create_circle(0.15,-0.45,0.02,True,'k',v_om)
o4 = create_circle(0.3,-0.50,0.02,True,'k',v_oh)
o5 = create_circle(0,-0.50,0.02,True,'k',v_ol)
if v_l1:
r1.set_hatch('\\')
add_shape(r1)
add_shape(ec2)
add_shape(ec)
add_shape(ech)
add_shape(ecov)
add_shape(cc)
add_shape(cc2)
add_shape(ccup)
add_shape(o1),add_shape(o2),add_shape(o3),add_shape(o4),add_shape(o5)
ax=plt.gca()
ax.set_xlim(-1, 1)
ax.set_ylim(-0.6, 0.6)
s = str(k)
plt.text(0, 0.4, s, fontsize=10, ha='center')
plt.axis('off')
def dig_multimodal(X,y,mm):
k,c = max(y)+1, -1
n_clusters = 2
alg = MiniBatchKMeans(n_clusters=n_clusters, random_state=10)
for i in range(k):
if mm[i]:
Xi = np.array(X[y==i])
yi = alg.fit_predict(Xi)
d = max(yi)
yi[yi>0] = k+c+d
yi[yi==0] = i
y[y==i]=yi
c = c+d
return y
def rebuilt_labels(y):
# inputs
# y: array with cluster labels (-1 for outliers)
# outputs
# y_new: refined array with cluster labels (-1 for outliers)
y_rem = np.unique(y)
outs = np.where(y_rem == -1)
a = 0
if len(outs[0])>0:
a=1
y_new = np.copy(y)
for i in range(0,y_rem.shape[0]-a):
y_new[y==y_rem[i+a]]=i
return y_new
def graph_ref(X,y,kinship,prun_level):
kinship[kinship==5]=0
G = nx.from_numpy_matrix(kinship)
kin = []
for tup in list(G.edges):
kin.append(5-kinship[tup[0],tup[1]])
pos = nx.spring_layout(G)
for edge in list(G.edges):
if G[edge[0]][edge[1]]["weight"] >= 4-prun_level:
G.remove_edge(edge[0], edge[1])
elif G[edge[0]][edge[1]]["weight"] == 3-prun_level:
if multimodality(np.vstack((X[y==edge[0]],X[y==edge[1]]))):
G.remove_edge(edge[0], edge[1])
lsubG = list(nx.connected_components(G))
if len(lsubG)==1:
for edge in list(G.edges):
if G[edge[0]][edge[1]]["weight"] >= 2:
G.remove_edge(edge[0], edge[1])
lsubG = list(nx.connected_components(G))
ynew = np.zeros(len(y), dtype=int)
ynew[y==-1]=-1
nc = 0
for subG in lsubG:
for lab in subG:
ynew[y==lab] = nc
nc = nc+1
return ynew
def reassign_outliers(X,y,out_sens,centroids,extR):
if out_sens==0:
mem_th = np.ones(len(extR))*np.inf
else:
mem_th = np.divide(extR,out_sens)
Xi = X[y==-1]
dm = distance.cdist(Xi,centroids)
yout = np.argmin(dm, axis=1)
dm_min = np.min(dm, axis=1)
ths_ext = mem_th[yout]
yout[dm_min>ths_ext]=-1
ynew = y
ynew[y==-1]=yout
return ynew
def refine(X,y,cc,gv,rc,rep = REP, min_rdens = MIN_REL_DENSITY, min_mass = MIN_CARDINALITY_R, out_sens = OUTLIER_SENS, prun_level = PRUN_LEVEL):
# inputs
# X: dataset (nxm), matrix of n vectors with m dimensions
# y: array with cluster labels (-1 for outliers)
# cc: cluster context (ClusterContext)
# gv: goi validity indices (GValidty)
# rc: cluster refinement context (RefinementContext)
# rep: number of repetitions
# outputs
# y: (refined?) array with cluster labels (-1 for outliers)
n, m = X.shape
for j in range(0,rep+1):
if sum(rc.mm):
y = dig_multimodal(X,y,rc.mm)
cc = cluster_context(X,y)
gv = gval(cc)
rc = refinement_context(X,y,cc,gv)
ch_flag = False
for i in range(0,cc.k):
if cc.outliers < 0.5 and (rc.kdens[i] <= min_rdens or cc.mass[i] < sum(cc.mass) * min_mass):
y[y==i] = -1 # or reassign them
ch_flag = True
if ch_flag:
y = rebuilt_labels(y)
cc = cluster_context(X,y)
gv = gval(cc)
rc = refinement_context(X,y,cc,gv)
y = graph_ref(X,y,rc.kinship, prun_level)
cc = cluster_context(X,y)
gv = gval(cc)
rc = refinement_context(X,y,cc,gv)
if sum(y==-1):
y = reassign_outliers(X,y,out_sens,cc.centroids,gv.strR)
cc = cluster_context(X,y)
if j<rep:
gv = gval(cc)
rc = refinement_context(X,y,cc,gv)
return y,cc
def cluster_kinship(k,De,erad,srad):
# inputs
# k: number of clusters
# De: cluster inter distance matrix (k x k matrix)
# erad: extended radii (k-size array)
# srad: strict radii (k-size array)
# outputs
# kinship: cluster kinship indices (k x k matrix): 5-unrelated, 4-acquitances, 3-close-friends, 2-relatives, 1-parent and child, 0-itself.
kinship = np.zeros(shape=(k,k))
comb = combinations(np.arange(k), 2)
for (i,j) in list(comb):
if erad[i] + erad[j] <= De[i,j]:
kinship[i,j],kinship[j,i] = 5,5
else:
if (erad[i] < De[i,j] and erad[j] < De[i,j]): #friends
if ((De[i,j] - srad[i] < erad[j]) or (De[i,j] - srad[j] < erad[i])): #close friends
kinship[i,j],kinship[j,i] = 3,3
else:
kinship[i,j],kinship[j,i] = 4,4 # acquaintance
elif ((erad[i] + De[i,j] < erad[j]) or (erad[j] + De[i,j] < erad[i])): #parent and child
kinship[i,j],kinship[j,i] = 1,1
else: #relatives
kinship[i,j],kinship[j,i] = 2,2
return kinship
def get_centroids(X,y,k):
# inputs
# X: dataset (nxm), matrix of n vectors with m dimensions
# y: array with cluster labels (-1 for outliers)
# k: number of clusters
# outputs
# centroids: matrix (kxm) with robust cluster centers
m = X.shape[1]
centroids = np.zeros(shape=(k,m))
for i in range(0,k):
Xi = np.array(X[y==i])
cXi = np.nanmedian(Xi, axis=0)
centroids[i] = cXi
return centroids
def multimodality(Xi):
# inputs
# Xi: cluster data (nxm), matrix of n vectors with m dimensions
# outputs
# mm: multimodality flag (scalar: 0 or 1)
n, m = Xi.shape
mm, bwf = 0,10
points = int(50*(np.log10(n)+1))
for i in range(0,m):
feat = Xi[:,i].reshape(-1,1)
bw=(max(feat)-min(feat))/bwf
if bw > 0:
try:
x, y = FFTKDE(bw='silverman').fit(feat).evaluate(points)
except:
x, y = FFTKDE(bw=bwf).fit(feat).evaluate(points)
peaks, _ = find_peaks(y, prominence=0.5)
if len(peaks) > 1:
mm = 1
return mm
def multimodal_clusters(X,y,k):
# inputs
# X: dataset (nxm), matrix of n vectors with m dimensions
# y: array with cluster labels (-1 for outliers)
# k: number of clusters
# outputs
# mm: multimodality flags for each cluster (kx1-array)
mm = np.zeros(shape=(k,1))
for i in range(0,k):
Xi = np.array(X[y==i])
if Xi.shape[0] > 0:
mm[i] = multimodality(Xi)
return mm
def rdensity(X, y, k):
# inputs
# X: dataset (nxm), matrix of n vectors with m dimensions
# y: array with cluster labels (-1 for outliers)
# k: number of clusters
# outputs
# Odens: global/overall density (scalar)
# kdens: cluster relative densities (kx1-array)
Ocentroid = np.nanmedian(X, axis=0)
dXtoO = distance.cdist(X,[Ocentroid])
Odens = 1/((np.nanmean(dXtoO) + 2*np.nanstd(dXtoO)) / X.shape[0])
kdens = np.zeros(shape=(k,1))
for i in range(0,k):
Xi = np.array(X[y==i])
cXi = np.nanmedian(Xi, axis=0)
intradXi = distance.cdist(Xi,[cXi])
medinXi = np.nanmedian(intradXi)
if medinXi == 0:
medinXi = 1
icard = np.sum(y==i)
kdens[i] = -1 + (icard/medinXi)/Odens
return kdens, Odens
def refinement_context(X,y,cc,gv):
# inputs
# X: dataset (nxm), matrix of n vectors with m dimensions
# y: array with cluster labels (-1 for outliers)
# cc: cluster context (ClusterContext)
# gv: goi validity indices (GValidty)
# outputs
# rc: cluster refinement context (RefinementContext)
rc = RefinementContext(cc.k)
rc.kdens, rc.Odens = rdensity(X,y,cc.k)
rc.kinship = cluster_kinship(cc.k,cc.De,gv.extR,gv.strR)
rc.mm = multimodal_clusters(X,y,cc.k)
return rc
def other_validations(X,y,verbose = False):
# inputs
# X: dataset (nxm), matrix of n vectors with m dimensions
# y: array with cluster labels (-1 for outliers)
# verbose: (bool) "True" stands for verbose mode
# outputs
# S: Silhouette index of the whole dataset (float)
# CH: Calinski Harabasz index of the whole dataset (float)
# DB: Davies Bouldin of the whole dataset (float)
from sklearn import metrics
X=X[y!=-1,:]
y=y[y!=-1]
S,CH,DB = np.nan, np.nan, np.nan
if len(y):
k = max(y)
if k>0:
S = metrics.silhouette_score(X, y, metric='euclidean')
CH = metrics.calinski_harabasz_score(X, y)
DB = metrics.davies_bouldin_score(X, y)
if verbose:
print("- Validity index > Silhouette:", S)
print("- Validity index > Calinski Harabasz:", CH)
print("- Validity index > Davies Bouldin:", DB)
return S,CH,DB
def gval(cc, verbose = False):
# inputs
# cc: cluster context (ClusterContext)
# verbose: (bool) "True" stands for verbose mode
# outputs
# gv: goi validity indices (GValidty)
k = cc.k
gv = GValidity(k)
radm, radm2, = np.zeros(shape=(k,1)), np.zeros(shape=(k,1))
oist, oirx = np.ones(shape=(k,k))*np.inf, np.ones(shape=(k,k))*np.inf
gv.extR = cc.mnDa + 2*cc.sdDa
gv.strR = cc.mdDa
gv.volR = np.divide(gv.extR, gv.strR, out=np.ones_like(gv.extR), where=gv.strR!=0)
radm = np.multiply(gv.extR, cc.mass)
radm2 = np.multiply(gv.strR, cc.mass)
per = permutations(np.arange(cc.k), 2)
for (i,j) in list(per):
oist[i][j] = cc.De[i][j] - gv.extR[i] - (cc.mnDa[j] +2*cc.sdDa[j])
oirx[i][j] = cc.De[i][j] - cc.mdDa[i] - cc.mdDa[j]
gv.oi_st = np.amin(oist, axis=0)
gv.oi_rx = np.amin(oirx, axis=0)
gv.oi_mn = np.divide(gv.oi_st, gv.extR, out=gv.oi_st, where=gv.extR!=0)
gv.Gstr = np.sum(np.multiply(gv.oi_st, cc.mass)) / np.sum(radm)
gv.Grex = np.sum(np.multiply(gv.oi_rx, cc.mass)) / np.sum(radm2)
if len(gv.oi_mn):
gv.Gmin = np.nanmin(gv.oi_mn)
if verbose:
print("- Validity index > GOI > Grex:", gv.Grex)
print("- Validity index > GOI > Gstr:", gv.Gstr)
print("- Validity index > GOI > Gmin:", gv.Gmin)
return gv
def cluster_context(X,y):
# inputs
# X: dataset (nxm), matrix of n vectors with m dimensions
# y: array with cluster labels (-1 for outliers)
# outputs
# ClusterContext: cluster context
k = max(y)+1
cc = ClusterContext(k,len(X[0,:]))
cXi = np.zeros(shape=(k,X.shape[1]))
for i in range(0,k):
Xi = np.array(X[y==i])
cc.mass[i] = Xi.shape[0]
cX = np.nanmedian(Xi, axis=0)
cXi[i] = cX
dm = distance.cdist(Xi,[cX])
cc.mnDa[i] = np.nanmean(dm)
cc.mdDa[i] = np.nanmedian(dm)
cc.sdDa[i] = np.nanstd(dm)
cc.De = distance_matrix(cXi,cXi)
cc.centroids = cXi
cc.outliers = sum(y==-1)/sum(cc.mass)
return cc