-
Notifications
You must be signed in to change notification settings - Fork 2
/
spherical_cluster.py
453 lines (359 loc) · 13.6 KB
/
spherical_cluster.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
# @Author: Joey Teng
# @Email: [email protected]
# @Filename: spherical_cluster.py
# @Last modified by: Joey Teng
# @Last modified time: 25-Mar-2018
"""Obtain clusters and calculate meta-features.
Args:
dataset_filename (string): path to the dataset
Predefined types:
Point (dict): {'coordinate': (float, ...), 'label': int}
Dataset (list): list of dict objects:
[Point, ...]
Vertex (tuple): Point['coordinate']
Vertices (list): [Vertex, ...]
Output files:
dataset_filename.output.json: calculated meta-features.
dataset_filename.clusters.json: calculated clusters.
dataset_filename.log: log file
"""
import argparse
import collections
import json
import logging
import logging.handlers
import math
import multiprocessing.pool
import os
import numpy
import meta_features
INFINITESIMAL = 1e-323
PROCESS_COUNT = int(os.cpu_count() / 2)
def initialize_logger(
name='LOG',
filename=None,
level=logging.DEBUG,
filemode='a'):
"""Initialize a logger in module logging.
Args:
name (string, optional): Name of logger. Defaults to None.
filename (string, optional): Defaults to None.
The path of log file
By default, logger will stream to the standard output
level (logging level, optional): Defaults to logging.INFO
filemode (string, optional): Defaults to 'a'.
'w' or 'a', overwrite or append
Returns:
logger: [description]
"""
log_format = '%(asctime)s %(levelname)s\n' + \
' %(filename)s:%(lineno)s: %(name)s: %(message)s'
if filename is None:
handler = logging.StreamHandler()
else:
handler = logging.handlers.RotatingFileHandler(
filename=filename, mode=filemode)
handler.setFormatter(logging.Formatter(log_format))
logger = logging.getLogger(name)
logger.addHandler(handler)
logger.setLevel(level)
return logger, handler
def load_dataset(filename):
"""Load data from a csv file.
Args:
filename (string): path of input file.
CSV format
[coordinate, ...] + [label]
Returns:
Dataset: dataset
"""
return [(
lambda point: {
'coordinate': tuple(map(float, point[:-1])),
'label': int(point[-1])})
(string.strip().rstrip().split(','))
for string in open(filename, 'r').read()
.strip().rstrip().split('\n')]
def initialize_cluster(coordinates):
"""Construct a cluster instance with given coordiante.
A factory function
Args:
coordinates (list): The coordinates that needed to be included.
[Vertex, ...]
Returns:
dict: a cluster initialized with given coordinates
[{
'centroid' (Vertex): centroid of the sphere,
'radius' (float): radius of the sphere,
'points' (list): Instances in the cluster
i.e. distance <= radius
[Vertex, ...],
'size' (int): Number of instances covered by the sphere
len(['points']),
'volume' (float): volume of the sphere
}]
"""
points = coordinates
_points = list(map(numpy.array, coordinates))
centroid = sum(_points) / len(_points)
radius = max(
map(lambda x, y=centroid: numpy.linalg.norm((x - y)), _points))
return {
'centroid': tuple(centroid),
'radius': radius,
'points': points,
'size': len(points),
'log-volume': calculate_log_volume(len(centroid), radius)
}
def calculate_distance(lhs, rhs):
"""Calculate the euclidean distance between 2 points.
Args:
lhs, rhs (Vertex): Coordinates of 2 points
Returns:
float: Euclidean distance between them
"""
return numpy.linalg.norm((numpy.array(lhs) - numpy.array(rhs)))
def calculate_log_volume(dimension, radius):
"""Calculate the log-volume of a sphere with given dimension and radius.
Args:
dimension (int): dimension of the space
radius (float): radius of the sphere
Returns:
float: the log-volume of the sphere
radius is set as REL_TOL (1e-09)
"""
if (math.isclose(radius, 0)):
radius = INFINITESIMAL
try:
log_volume = ((dimension / 2.0) * math.log(math.pi) + dimension *
math.log(radius) - math.lgamma(dimension / 2.0 + 1))
except ValueError as message:
raise ValueError("".join([
"{0}\n".format(message),
"(({0} / 2.0) * ln(pi) + ({0} * ln({1})".format(dimension, radius),
" - ln(gamma({0} / 2.0 + 1)))".format(dimension)]))
if math.isnan(log_volume):
raise ValueError(
"Volume is NaN: pi ^ " +
"({0} / 2.0) / gamma({0} / 2.0 + 1) * {1} ^ {0}".format(
dimension, radius))
return log_volume
def float_less_or_equal(lhs, rhs, **kwargs):
"""Determine float A is less than or equal to B using numpy.isclose().
Use numpy.isclose() to determine if A and B are equal
with default tolerance.
Args:
lhs, rhs (float): values that need to be compared
kwargs: kwargs for numpy.isclose()
Returns:
bool: result of comparison.
"""
return numpy.isclose(lhs, rhs, **kwargs) or (lhs < rhs)
def check_inside_cluster(cluster, point):
"""Check if point is inside the cluster.
Args:
cluster (dict): cluster to be checked
{
'centroid' (Vertex): centroid of the cluster,
'radius' (float): radius of the cluster
}
point (Vertex): point to be checked
Returns:
bool: if the point is encompassed by the boundary
"""
return float_less_or_equal(
calculate_distance(cluster['centroid'], point), cluster['radius'])
def check_homogeneity(cluster, label, clusters):
"""Check homogeneity of the cluster with given clusters.
A homogeneous cluster will not overlap with any other cluster which has
different label, but may overlap with cluster that has the same label.
Which means, there should be no region with ambiguity in
categorisation process.
Args:
cluster (dict): Cluster that need to be checked
{
'centroid' (Vertex): centroid of the cluster,
'radius' (float): radius of the cluster
}
label (): label of the cluster
clusters (dict): list of clusters with labels as keys.
{
label: [cluster, ...]
}
Returns:
bool: if cluster is homogeneous
"""
for _label, _clusters in clusters.items():
if _label == label:
continue
for _cluster in _clusters:
if float_less_or_equal(
calculate_distance(
cluster['centroid'], _cluster['centroid']),
(cluster['radius'] + _cluster['radius'])):
return False
return True
def clustering(dataset, logger):
"""Calculate all spherical clusters.
All spheres will be pure(only contains data points with same label)
Args:
dataset (list): All the instances in the space with label
list of dict objects:
[Point, ...]
logger (logger): logger for logging
Returns:
dict: Clusters obtained separated by labels
label: clusters (list of dict objects)
[{
'centroid' (Vertex): centroid of the sphere,
'radius' (float): radius of the sphere,
'points' (list) : Instances in the cluster
[Vertex, ...],
'size' (int): Number of instances covered by the sphere
len(['points']),
'volume': The volume of the sphere
float(optional)
}, ...]
"""
logger.info('Sorting datasets...')
dataset.sort(key=lambda x: x['coordinate'])
logger.info('Initialise clusters...')
clusters = collections.defaultdict(list)
for instance in dataset:
clusters[instance['label']].append(
initialize_cluster((instance['coordinate'], )))
logger.info('Merging clusters...')
logger_count = 0
for label, homo_clusters in clusters.items():
index = 0
while index < len(homo_clusters):
current = homo_clusters[index]
merging_index = -1
distance = float('inf')
for j_index, cluster in enumerate(homo_clusters[index + 1:]):
new_distance = calculate_distance(
current['centroid'], cluster['centroid'])
if new_distance < distance:
merging_index = j_index + index + 1
distance = new_distance
if merging_index == -1:
index += 1
continue
cluster = initialize_cluster(
current['points'] + homo_clusters[merging_index]['points'])
if (check_homogeneity(cluster, label, clusters)):
homo_clusters[merging_index], homo_clusters[-1] =\
homo_clusters[-1], homo_clusters[merging_index]
homo_clusters.pop()
current = cluster
homo_clusters[index] = current
else:
index += 1
logger_count += 1
logger.info('{0}/{1} categories completed'.format(
logger_count, len(clusters.keys())))
return clusters
def main(args):
"""
Start main function here.
Dispatching all the tasks to process.
"""
log_file = args.log
logger, handler = initialize_logger("Parent", log_file)
logger.info('Start: Version 2.1.1')
logger.debug('Logger initialized')
logger.debug('argparse: %r', args)
logger.removeHandler(handler)
_args = []
for dataset_filename in args.paths:
clusters_filename = dataset_filename + ".clusters.json"
output_filename = dataset_filename + ".output.json"
_args.append(tuple([
dataset_filename,
clusters_filename,
output_filename,
log_file]))
pool = multiprocessing.pool.Pool(PROCESS_COUNT)
list(pool.map(task_processing, _args))
pool.close()
pool.join()
def task_processing(args): # Take note here!!!
"""Unwrap the args tuple to adapt a function with multiple args to map."""
def worker(
dataset_filename,
clusters_filename,
output_filename,
log_file):
"""Link the submodules to process the data."""
logger, handler = initialize_logger(dataset_filename, log_file)
logger.debug('Logger initialized')
logger.debug('Loading dataset')
dataset = load_dataset(dataset_filename)
logger.info('Dataset loaded')
logger.info('Trying to load clusters from %s', clusters_filename)
clusters = None
try:
clusters = json.load(open(clusters_filename, 'r'))
except FileNotFoundError:
logger.warning('Clusters data file not found')
except json.decoder.JSONDecodeError:
logger.warning('File broken. Not Json Decodable')
if not clusters:
logger.debug('Clustering data points')
clusters = clustering(dataset, logger)
logger.debug(
'Dumping clusters data into json file: %s', clusters_filename)
json.dump(clusters, open(clusters_filename, 'w'))
logger.info('Data points clustered')
logger.debug('Calculating meta-feature indicators')
features = meta_features.meta_features(clusters)
logger.debug(
'Dumping meta-feature indicators into json file: %s',
clusters_filename)
json.dump(features, open(output_filename, 'w'))
logger.info('Meta-feature indicators calculated')
logger.info('Complete')
logger.removeHandler(handler)
return worker(*args)
def traverse(paths):
"""Traverse to collect all the data files."""
print("Starting Traverse Through", flush=True)
files = []
while paths:
path = paths[0]
paths = paths[1:]
for file in os.listdir(path):
if (file.find('.json') == -1
and file.find('.log') == -1
and file.find('.DS_Store') == -1
and file.find('.png') == -1
and file.find('.html') == -1):
files.append('{0}/{1}'.format(path, file))
elif os.path.isdir('{0}/{1}'.format(path, file)):
paths.append('{0}/{1}'.format(path, file))
print("Traverse Completed.", flush=True)
return files
def parse_args():
"""Parse all necessary args."""
parser = argparse.ArgumentParser(
description="Obtain clusters and calculate meta-features")
parser.add_argument('-r', action='store', nargs='+',
default=[], metavar='Directory',
help='Recursively processing all files in the folder')
parser.add_argument('-i', action='store', nargs='+',
default=[], metavar='File',
help='Files that need to be processed')
parser.add_argument('--log', action='store', type=str,
default='spherical_cluster.log', metavar='Log file',
help='Path to the log file')
args = parser.parse_args()
paths = []
if (args.r):
paths = traverse(args.r)
paths.extend(args.i)
paths.sort()
args.paths = paths
return args
if __name__ == '__main__':
args = parse_args()
main(args)