1
|
|
|
"""!
|
2
|
|
|
|
3
|
|
|
@brief Cluster analysis algorithm: BANG.
|
4
|
|
|
@details Implementation based on paper @cite inproceedings::bang::1.
|
5
|
|
|
|
6
|
|
|
@authors Andrei Novikov ([email protected])
|
7
|
|
|
@date 2014-2018
|
8
|
|
|
@copyright GNU Public License
|
9
|
|
|
|
10
|
|
|
@cond GNU_PUBLIC_LICENSE
|
11
|
|
|
PyClustering is free software: you can redistribute it and/or modify
|
12
|
|
|
it under the terms of the GNU General Public License as published by
|
13
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
14
|
|
|
(at your option) any later version.
|
15
|
|
|
|
16
|
|
|
PyClustering is distributed in the hope that it will be useful,
|
17
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19
|
|
|
GNU General Public License for more details.
|
20
|
|
|
|
21
|
|
|
You should have received a copy of the GNU General Public License
|
22
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
23
|
|
|
@endcond
|
24
|
|
|
|
25
|
|
|
"""
|
26
|
|
|
|
27
|
|
|
|
28
|
|
|
import matplotlib
|
|
|
|
|
29
|
|
|
import matplotlib.gridspec as gridspec;
|
|
|
|
|
30
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
31
|
|
|
import matplotlib.patches as patches
|
|
|
|
|
32
|
|
|
|
33
|
|
|
import itertools
|
34
|
|
|
|
35
|
|
|
from pyclustering.cluster import cluster_visualizer
|
36
|
|
|
from pyclustering.cluster.encoder import type_encoding
|
37
|
|
|
|
38
|
|
|
from pyclustering.utils import data_corners
|
39
|
|
|
from pyclustering.utils.color import color as color_list
|
40
|
|
|
|
41
|
|
|
|
42
|
|
|
|
43
|
|
|
class bang_visualizer:
|
44
|
|
|
"""!
|
45
|
|
|
@brief Visualizer of BANG algorithm's results.
|
46
|
|
|
@details BANG visualizer provides visualization services that are specific for BANG algorithm.
|
47
|
|
|
|
48
|
|
|
"""
|
49
|
|
|
|
50
|
|
|
|
51
|
|
|
__maximum_density_alpha = 0.6
|
52
|
|
|
|
53
|
|
|
|
54
|
|
|
@staticmethod
|
55
|
|
|
def show_blocks(directory):
|
56
|
|
|
"""!
|
57
|
|
|
@brief Show BANG-blocks (leafs only) in data space.
|
58
|
|
|
@details BANG-blocks represents grid that was used for clustering process.
|
59
|
|
|
|
60
|
|
|
@param[in] directory (bang_directory): Directory that was created by BANG algorithm during clustering process.
|
61
|
|
|
|
62
|
|
|
"""
|
63
|
|
|
|
64
|
|
|
dimension = len(directory.get_data()[0])
|
65
|
|
|
|
66
|
|
|
amount_canvases = 1
|
67
|
|
|
if dimension > 1:
|
68
|
|
|
amount_canvases = int(dimension * (dimension - 1) / 2)
|
69
|
|
|
|
70
|
|
|
figure = plt.figure();
|
71
|
|
|
grid_spec = gridspec.GridSpec(1, amount_canvases);
|
72
|
|
|
|
73
|
|
|
pairs = list(itertools.combinations(range(dimension), 2))
|
74
|
|
|
if len(pairs) == 0: pairs = [(0, 0)]
|
75
|
|
|
|
76
|
|
|
for index in range(amount_canvases):
|
77
|
|
|
ax = figure.add_subplot(grid_spec[index]);
|
78
|
|
|
bang_visualizer.__draw_blocks(ax, directory.get_leafs(), pairs[index])
|
79
|
|
|
bang_visualizer.__draw_two_dimension_data(ax, directory.get_data(), pairs[index])
|
80
|
|
|
|
81
|
|
|
plt.show()
|
82
|
|
|
|
83
|
|
|
|
84
|
|
|
@staticmethod
|
85
|
|
|
def show_dendrogram(dendrogram):
|
86
|
|
|
"""!
|
87
|
|
|
@brief Display dendrogram of BANG-blocks.
|
88
|
|
|
|
89
|
|
|
@param[in] dendrogram (list): List representation of dendrogram of BANG-blocks.
|
90
|
|
|
|
91
|
|
|
@see bang.get_dendrogram()
|
92
|
|
|
|
93
|
|
|
"""
|
94
|
|
|
plt.figure()
|
95
|
|
|
axis = plt.subplot(1, 1, 1)
|
96
|
|
|
|
97
|
|
|
current_position = 0
|
98
|
|
|
for index_cluster in range(len(dendrogram)):
|
99
|
|
|
densities = [ block.get_density() for block in dendrogram[index_cluster] ]
|
100
|
|
|
xrange = range(current_position, current_position + len(densities))
|
101
|
|
|
|
102
|
|
|
axis.bar(xrange, densities, 1.0, linewidth=0.0, color=color_list.get_color(index_cluster))
|
103
|
|
|
|
104
|
|
|
current_position += len(densities)
|
105
|
|
|
|
106
|
|
|
axis.set_ylabel("density")
|
107
|
|
|
axis.set_xlabel("block")
|
108
|
|
|
axis.xaxis.set_ticklabels([]);
|
109
|
|
|
|
110
|
|
|
plt.xlim([-0.5, current_position - 0.5])
|
111
|
|
|
plt.show()
|
112
|
|
|
|
113
|
|
|
|
114
|
|
|
@staticmethod
|
115
|
|
|
def show_clusters(data, clusters, noise=[]):
|
|
|
|
|
116
|
|
|
"""!
|
117
|
|
|
@brief Display K-Means clustering results.
|
118
|
|
|
|
119
|
|
|
@param[in] data (list): Dataset that was used for clustering.
|
120
|
|
|
@param[in] clusters (array_like): Clusters that were allocated by the algorithm.
|
121
|
|
|
@param[in] noise (array_like): Noise that were allocated by the algorithm.
|
122
|
|
|
|
123
|
|
|
"""
|
124
|
|
|
visualizer = cluster_visualizer()
|
125
|
|
|
visualizer.append_clusters(clusters, data)
|
126
|
|
|
visualizer.append_cluster(noise, data, marker='x')
|
127
|
|
|
visualizer.show()
|
128
|
|
|
|
129
|
|
|
|
130
|
|
|
@staticmethod
|
131
|
|
|
def __draw_two_dimension_data(ax, data, pair):
|
132
|
|
|
"""!
|
133
|
|
|
@brief Display data in two-dimensional canvas.
|
134
|
|
|
|
135
|
|
|
@param[in] ax (Axis): Canvas where data should be displayed.
|
136
|
|
|
@param[in] data (list): Data points that should be displayed.
|
137
|
|
|
@param[in] pair (tuple): Pair of dimension indexes.
|
138
|
|
|
|
139
|
|
|
"""
|
140
|
|
|
ax.set_xlabel("x%d" % pair[0])
|
141
|
|
|
ax.set_ylabel("x%d" % pair[1])
|
142
|
|
|
|
143
|
|
|
for point in data:
|
144
|
|
|
if len(data[0]) > 1:
|
145
|
|
|
ax.plot(point[pair[0]], point[pair[1]], color='red', marker='.')
|
146
|
|
|
else:
|
147
|
|
|
ax.plot(point[pair[0]], 0, color='red', marker='.')
|
148
|
|
|
ax.yaxis.set_ticklabels([]);
|
149
|
|
|
|
150
|
|
|
|
151
|
|
|
@staticmethod
|
152
|
|
|
def __draw_blocks(ax, blocks, pair):
|
153
|
|
|
"""!
|
154
|
|
|
@brief Display BANG-blocks on specified figure.
|
155
|
|
|
|
156
|
|
|
@param[in] ax (Axis): Axis where bang-blocks should be displayed.
|
157
|
|
|
@param[in] blocks (list): List of blocks that should be displyed.
|
158
|
|
|
@param[in] pair (tuple): Pair of coordinate index that should be displyed.
|
159
|
|
|
|
160
|
|
|
"""
|
161
|
|
|
ax.grid(False)
|
162
|
|
|
|
163
|
|
|
density_scale = blocks[-1].get_density()
|
164
|
|
|
for block in blocks:
|
165
|
|
|
bang_visualizer.__draw_block(ax, pair, block, density_scale)
|
166
|
|
|
|
167
|
|
|
|
168
|
|
|
@staticmethod
|
169
|
|
|
def __draw_block(ax, pair, block, density_scale):
|
170
|
|
|
"""!
|
171
|
|
|
@brief Display BANG-block on the specified ax.
|
172
|
|
|
|
173
|
|
|
@param[in] ax (Axis): Axis where block should be displayed.
|
174
|
|
|
@param[in] pair (tuple): Pair of coordinate index that should be displayed.
|
175
|
|
|
@param[in] block (bang_block): BANG-block that should be displayed.
|
176
|
|
|
@param[in] density_scale (double): Max density to display density of the block by appropriate tone.
|
177
|
|
|
|
178
|
|
|
"""
|
179
|
|
|
max_corner, min_corner = bang_visualizer.__get_rectangle_description(block, pair)
|
180
|
|
|
|
181
|
|
|
belong_cluster = block.get_cluster() is not None
|
182
|
|
|
|
183
|
|
|
density_scale = bang_visualizer.__maximum_density_alpha * block.get_density() / density_scale
|
184
|
|
|
|
185
|
|
|
face_color = matplotlib.colors.to_rgba('blue', alpha=density_scale)
|
186
|
|
|
edge_color = matplotlib.colors.to_rgba('black', alpha=1.0)
|
187
|
|
|
|
188
|
|
|
rect = patches.Rectangle(min_corner, max_corner[0] - min_corner[0], max_corner[1] - min_corner[1],
|
189
|
|
|
fill=belong_cluster,
|
190
|
|
|
facecolor=face_color,
|
191
|
|
|
edgecolor=edge_color,
|
192
|
|
|
linewidth=0.5)
|
193
|
|
|
ax.add_patch(rect)
|
194
|
|
|
|
195
|
|
|
|
196
|
|
|
@staticmethod
|
197
|
|
|
def __get_rectangle_description(block, pair):
|
198
|
|
|
"""!
|
199
|
|
|
@brief Create rectangle description for block in specific dimension.
|
200
|
|
|
|
201
|
|
|
@param[in] pair (tuple): Pair of coordinate index that should be displayed.
|
202
|
|
|
@param[in] block (bang_block): BANG-block that should be displayed
|
203
|
|
|
|
204
|
|
|
@return (tuple) Pair of corners that describes rectangle.
|
205
|
|
|
|
206
|
|
|
"""
|
207
|
|
|
max_corner, min_corner = block.get_spatial_block().get_corners()
|
208
|
|
|
|
209
|
|
|
max_corner = [max_corner[pair[0]], max_corner[pair[1]]]
|
210
|
|
|
min_corner = [min_corner[pair[0]], min_corner[pair[1]]]
|
211
|
|
|
|
212
|
|
|
if pair == (0, 0):
|
213
|
|
|
max_corner[1], min_corner[1] = 1.0, -1.0
|
214
|
|
|
|
215
|
|
|
return max_corner, min_corner
|
216
|
|
|
|
217
|
|
|
|
218
|
|
|
class bang_directory:
|
219
|
|
|
"""!
|
220
|
|
|
@brief BANG directory stores BANG-blocks that represents grid in data space.
|
221
|
|
|
@details The directory build BANG-blocks in binary tree manner. Leafs of the tree stored separately to provide
|
222
|
|
|
a direct access to the leafs that should be analysed. Leafs cache data-points.
|
223
|
|
|
|
224
|
|
|
"""
|
225
|
|
|
def __init__(self, data, levels, density_threshold=0.0):
|
226
|
|
|
"""!
|
227
|
|
|
@brief Create BANG directory - basically tree structure with direct access to leafs.
|
228
|
|
|
|
229
|
|
|
@param[in] data (list): Input data that is clustered.
|
230
|
|
|
@param[in] levels (uint): Height of the blocks tree.
|
231
|
|
|
@param[in] density_threshold (double): The lowest level of density when contained data by bang-block is
|
232
|
|
|
considered as a noise and there is no need to split it till the last level.
|
233
|
|
|
|
234
|
|
|
"""
|
235
|
|
|
self.__data = data
|
236
|
|
|
self.__levels = levels
|
237
|
|
|
self.__density_threshold = density_threshold
|
238
|
|
|
self.__leafs = []
|
239
|
|
|
self.__root = None
|
240
|
|
|
|
241
|
|
|
self.__create_directory()
|
242
|
|
|
|
243
|
|
|
|
244
|
|
|
def get_data(self):
|
245
|
|
|
"""!
|
246
|
|
|
@brief Return data that is stored in the directory.
|
247
|
|
|
|
248
|
|
|
@return (list) List of points that represents stored data.
|
249
|
|
|
|
250
|
|
|
"""
|
251
|
|
|
return self.__data
|
252
|
|
|
|
253
|
|
|
|
254
|
|
|
def get_leafs(self):
|
255
|
|
|
"""!
|
256
|
|
|
@brief Return leafs - the smallest blocks.
|
257
|
|
|
@details Some leafs can be bigger than others because splitting is not performed for blocks whose density is
|
258
|
|
|
less than threshold.
|
259
|
|
|
|
260
|
|
|
@return (list) List of blocks that are leafs of BANG directory.
|
261
|
|
|
|
262
|
|
|
"""
|
263
|
|
|
return self.__leafs
|
264
|
|
|
|
265
|
|
|
|
266
|
|
|
def __create_directory(self):
|
267
|
|
|
"""!
|
268
|
|
|
@brief Create BANG directory as a tree with separate storage for leafs.
|
269
|
|
|
|
270
|
|
|
"""
|
271
|
|
|
|
272
|
|
|
min_corner, max_corner = data_corners(self.__data)
|
273
|
|
|
data_block = spatial_block(max_corner, min_corner)
|
274
|
|
|
|
275
|
|
|
cache_require = (self.__levels == 1)
|
276
|
|
|
self.__root = bang_block(self.__data, 0, 0, data_block, cache_require)
|
277
|
|
|
|
278
|
|
|
if cache_require:
|
279
|
|
|
self.__leafs.append(self.__root)
|
280
|
|
|
else:
|
281
|
|
|
self.__build_directory_levels()
|
282
|
|
|
|
283
|
|
|
|
284
|
|
|
def __build_directory_levels(self):
|
285
|
|
|
"""!
|
286
|
|
|
@brief Build levels of direction if amount of level is greater than one.
|
287
|
|
|
|
288
|
|
|
"""
|
289
|
|
|
previous_level_blocks = [ self.__root ]
|
290
|
|
|
for level in range(1, self.__levels):
|
291
|
|
|
previous_level_blocks = self.__build_level(previous_level_blocks, level)
|
292
|
|
|
|
293
|
|
|
self.__leafs = sorted(self.__leafs, key=lambda block: block.get_density())
|
294
|
|
|
|
295
|
|
|
|
296
|
|
|
def __build_level(self, previous_level_blocks, level):
|
297
|
|
|
"""!
|
298
|
|
|
@brief Build new level of directory.
|
299
|
|
|
|
300
|
|
|
@param[in] previous_level_blocks (list): BANG-blocks on the previous level.
|
301
|
|
|
@param[in] level (uint): Level number that should be built.
|
302
|
|
|
|
303
|
|
|
@return (list) New block on the specified level.
|
304
|
|
|
|
305
|
|
|
"""
|
306
|
|
|
current_level_blocks = []
|
307
|
|
|
|
308
|
|
|
split_dimension = level % len(self.__data[0])
|
309
|
|
|
cache_require = (level == self.__levels - 1)
|
310
|
|
|
|
311
|
|
|
for block in previous_level_blocks:
|
312
|
|
|
self.__split_block(block, split_dimension, cache_require, current_level_blocks)
|
313
|
|
|
|
314
|
|
|
if cache_require:
|
315
|
|
|
self.__leafs += current_level_blocks
|
316
|
|
|
|
317
|
|
|
return current_level_blocks
|
318
|
|
|
|
319
|
|
|
|
320
|
|
|
def __split_block(self, block, split_dimension, cache_require, current_level_blocks):
|
321
|
|
|
"""!
|
322
|
|
|
@brief Split specific block in specified dimension.
|
323
|
|
|
@details Split is not performed for block whose density is lower than threshold value, such blocks are putted to
|
324
|
|
|
leafs.
|
325
|
|
|
|
326
|
|
|
@param[in] block (bang_block): BANG-block that should be split.
|
327
|
|
|
@param[in] split_dimension (uint): Dimension at which splitting should be performed.
|
328
|
|
|
@param[in] cache_require (bool): Defines when points in cache should be stored during density calculation.
|
329
|
|
|
@param[in|out] current_level_blocks (list): Block storage at the current level where new blocks should be added.
|
330
|
|
|
|
331
|
|
|
"""
|
332
|
|
|
if block.get_density() <= self.__density_threshold:
|
333
|
|
|
self.__leafs.append(block)
|
334
|
|
|
|
335
|
|
|
else:
|
336
|
|
|
left, right = block.split(split_dimension, cache_require)
|
337
|
|
|
current_level_blocks.append(left)
|
338
|
|
|
current_level_blocks.append(right)
|
339
|
|
|
|
340
|
|
|
|
341
|
|
|
|
342
|
|
|
class spatial_block:
|
343
|
|
|
"""!
|
344
|
|
|
@brief Geometrical description of BANG block in data space.
|
345
|
|
|
@details Provides services related to spatial function and used by bang_block
|
346
|
|
|
|
347
|
|
|
@see bang_block
|
348
|
|
|
|
349
|
|
|
"""
|
350
|
|
|
|
351
|
|
|
def __init__(self, max_corner, min_corner):
|
352
|
|
|
"""!
|
353
|
|
|
@brief Creates spatial block in data space.
|
354
|
|
|
|
355
|
|
|
@param[in] max_corner (array_like): Maximum corner coordinates of the block.
|
356
|
|
|
@param[in] min_corner (array_like): Minimal corner coordinates of the block.
|
357
|
|
|
|
358
|
|
|
"""
|
359
|
|
|
self.__max_corner = max_corner
|
360
|
|
|
self.__min_corner = min_corner
|
361
|
|
|
self.__volume = self.__calculate_volume()
|
362
|
|
|
|
363
|
|
|
|
364
|
|
|
def __str__(self):
|
365
|
|
|
"""!
|
366
|
|
|
@brief Returns string block description.
|
367
|
|
|
|
368
|
|
|
@return String representation of the block.
|
369
|
|
|
|
370
|
|
|
"""
|
371
|
|
|
return "(max: %s; min: %s)" % (self.__max_corner, self.__min_corner)
|
372
|
|
|
|
373
|
|
|
|
374
|
|
|
def __contains__(self, point):
|
375
|
|
|
"""!
|
376
|
|
|
@brief Point is considered as contained if it lies in block (belong to it).
|
377
|
|
|
|
378
|
|
|
@return (bool) True if point is in block, otherwise False.
|
379
|
|
|
|
380
|
|
|
"""
|
381
|
|
|
for i in range(len(point)):
|
382
|
|
|
if point[i] < self.__min_corner[i] or point[i] > self.__max_corner[i]:
|
383
|
|
|
return False
|
384
|
|
|
|
385
|
|
|
return True
|
386
|
|
|
|
387
|
|
|
|
388
|
|
|
def get_corners(self):
|
389
|
|
|
"""!
|
390
|
|
|
@brief Return spatial description of current block.
|
391
|
|
|
|
392
|
|
|
@return (tuple) Pair of maximum and minimum corners (max_corner, min_corner).
|
393
|
|
|
|
394
|
|
|
"""
|
395
|
|
|
return self.__max_corner, self.__min_corner
|
396
|
|
|
|
397
|
|
|
|
398
|
|
|
def get_volume(self):
|
399
|
|
|
"""!
|
400
|
|
|
@brief Returns volume of current block.
|
401
|
|
|
@details Volume block has uncommon mining here: for 1D is length of a line, for 2D is square of rectangle,
|
402
|
|
|
for 3D is volume of 3D figure, and for ND is volume of ND figure.
|
403
|
|
|
|
404
|
|
|
@return (double) Volume of current block.
|
405
|
|
|
|
406
|
|
|
"""
|
407
|
|
|
return self.__volume
|
408
|
|
|
|
409
|
|
|
|
410
|
|
|
def split(self, dimension):
|
411
|
|
|
"""!
|
412
|
|
|
@brief Split current block into two spatial blocks in specified dimension.
|
413
|
|
|
|
414
|
|
|
@param[in] dimension (uint): Dimension where current block should be split.
|
415
|
|
|
|
416
|
|
|
@return (tuple) Pair of new split blocks from current block.
|
417
|
|
|
|
418
|
|
|
"""
|
419
|
|
|
first_max_corner = self.__max_corner[:]
|
420
|
|
|
second_min_corner = self.__min_corner[:]
|
421
|
|
|
|
422
|
|
|
split_border = (self.__max_corner[dimension] + self.__min_corner[dimension]) / 2.0
|
423
|
|
|
|
424
|
|
|
first_max_corner[dimension] = split_border
|
425
|
|
|
second_min_corner[dimension] = split_border
|
426
|
|
|
|
427
|
|
|
return spatial_block(first_max_corner, self.__min_corner), spatial_block(self.__max_corner, second_min_corner)
|
428
|
|
|
|
429
|
|
|
|
430
|
|
|
def is_neighbor(self, block):
|
431
|
|
|
"""!
|
432
|
|
|
@brief Performs calculation to identify whether specified block is neighbor of current block.
|
433
|
|
|
|
434
|
|
|
@param[in] block (spatial_block): Another block that is check whether it is neighbor.
|
435
|
|
|
|
436
|
|
|
@return (bool) True is blocks are neighbors, False otherwise.
|
437
|
|
|
|
438
|
|
|
"""
|
439
|
|
|
if block is not self:
|
440
|
|
|
block_max_corner, _ = block.get_corners()
|
441
|
|
|
dimension = len(block_max_corner)
|
442
|
|
|
neighborhood_score = self.__calculate_neighborhood(block_max_corner)
|
443
|
|
|
|
444
|
|
|
if neighborhood_score == dimension:
|
445
|
|
|
return True
|
446
|
|
|
|
447
|
|
|
return False
|
448
|
|
|
|
449
|
|
|
|
450
|
|
|
def __calculate_neighborhood(self, block_max_corner):
|
451
|
|
|
"""!
|
452
|
|
|
@brief Calculates neighborhood score that defined whether blocks are neighbors.
|
453
|
|
|
|
454
|
|
|
@param[in] block_max_corner (list): Maximum coordinates of other block.
|
455
|
|
|
|
456
|
|
|
@return (uint) Neighborhood score.
|
457
|
|
|
|
458
|
|
|
"""
|
459
|
|
|
dimension = len(block_max_corner)
|
460
|
|
|
|
461
|
|
|
length_edges = [self.__max_corner[i] - self.__min_corner[i] for i in range(dimension)]
|
462
|
|
|
|
463
|
|
|
neighborhood_score = 0
|
464
|
|
|
for i in range(dimension):
|
465
|
|
|
diff = abs(block_max_corner[i] - self.__max_corner[i])
|
466
|
|
|
|
467
|
|
|
if diff <= length_edges[i] + length_edges[i] * 0.0001:
|
468
|
|
|
neighborhood_score += 1
|
469
|
|
|
|
470
|
|
|
return neighborhood_score
|
471
|
|
|
|
472
|
|
|
|
473
|
|
|
def __calculate_volume(self):
|
474
|
|
|
"""!
|
475
|
|
|
@brief Calculates volume of current spatial block.
|
476
|
|
|
|
477
|
|
|
@return (double) Volume of current spatial block.
|
478
|
|
|
|
479
|
|
|
"""
|
480
|
|
|
volume = self.__max_corner[0] - self.__min_corner[0]
|
481
|
|
|
for i in range(1, len(self.__max_corner)):
|
482
|
|
|
volume *= self.__max_corner[i] - self.__min_corner[i]
|
483
|
|
|
|
484
|
|
|
return volume
|
485
|
|
|
|
486
|
|
|
|
487
|
|
|
|
488
|
|
|
class bang_block:
|
489
|
|
|
"""!
|
490
|
|
|
@brief BANG-block that represent spatial region in data space.
|
491
|
|
|
|
492
|
|
|
"""
|
493
|
|
|
def __init__(self, data, region, level, space_block, cache_points=False):
|
494
|
|
|
"""!
|
495
|
|
|
@brief Create BANG-block.
|
496
|
|
|
|
497
|
|
|
@param[in] data (list): List of points that are processed.
|
498
|
|
|
@param[in] region (uint): Region number - unique value on a level.
|
499
|
|
|
@param[in] level (uint): Level number where block is created.
|
500
|
|
|
@param[in] space_block (spatial_block): Spatial block description in data space.
|
501
|
|
|
@param[in] cache_points (bool): if True then points are stored in memory (used for leaf blocks).
|
502
|
|
|
|
503
|
|
|
"""
|
504
|
|
|
self.__data = data
|
505
|
|
|
self.__region_number = region
|
506
|
|
|
self.__level = level
|
507
|
|
|
self.__spatial_block = space_block
|
508
|
|
|
self.__cache_points = cache_points
|
509
|
|
|
|
510
|
|
|
self.__cluster = None
|
511
|
|
|
self.__points = None
|
512
|
|
|
self.__density = self.__calculate_density()
|
513
|
|
|
|
514
|
|
|
|
515
|
|
|
def __str__(self):
|
516
|
|
|
"""!
|
517
|
|
|
@brief Returns string representation of BANG-block using region number and level where block is located.
|
518
|
|
|
|
519
|
|
|
"""
|
520
|
|
|
return "(" + str(self.__region_number) + ", " + str(self.__level) + ")"
|
521
|
|
|
|
522
|
|
|
|
523
|
|
|
def get_region(self):
|
524
|
|
|
"""!
|
525
|
|
|
@brief Returns region number of BANG-block.
|
526
|
|
|
@details Region number is unique on among region numbers on a directory level. Pair of region number and level
|
527
|
|
|
is unique for all directory.
|
528
|
|
|
|
529
|
|
|
@return (uint) Region number.
|
530
|
|
|
|
531
|
|
|
"""
|
532
|
|
|
return self.__region_number
|
533
|
|
|
|
534
|
|
|
|
535
|
|
|
def get_density(self):
|
536
|
|
|
"""!
|
537
|
|
|
@brief Returns density of the BANG-block.
|
538
|
|
|
|
539
|
|
|
@return (double) BANG-block density.
|
540
|
|
|
|
541
|
|
|
"""
|
542
|
|
|
return self.__density
|
543
|
|
|
|
544
|
|
|
|
545
|
|
|
def get_cluster(self):
|
546
|
|
|
"""!
|
547
|
|
|
@brief Return index of cluster to which the BANG-block belongs to.
|
548
|
|
|
@details Index of cluster may have None value if the block was not assigned to any cluster.
|
549
|
|
|
|
550
|
|
|
@return (uint) Index of cluster or None if the block does not belong to any cluster.
|
551
|
|
|
|
552
|
|
|
"""
|
553
|
|
|
return self.__cluster
|
554
|
|
|
|
555
|
|
|
|
556
|
|
|
def get_spatial_block(self):
|
557
|
|
|
"""!
|
558
|
|
|
@brief Return spatial block - BANG-block description in data space.
|
559
|
|
|
|
560
|
|
|
@return (spatial_block) Spatial block of the BANG-block.
|
561
|
|
|
|
562
|
|
|
"""
|
563
|
|
|
return self.__spatial_block
|
564
|
|
|
|
565
|
|
|
|
566
|
|
|
def get_points(self):
|
567
|
|
|
"""!
|
568
|
|
|
@brief Return points that covers by the BANG-block.
|
569
|
|
|
|
570
|
|
|
@return (list) List of point indexes that are covered by the block.
|
571
|
|
|
|
572
|
|
|
"""
|
573
|
|
|
if self.__points is None:
|
574
|
|
|
self.__cache_covered_data()
|
575
|
|
|
|
576
|
|
|
return self.__points
|
577
|
|
|
|
578
|
|
|
|
579
|
|
|
def set_cluster(self, index):
|
580
|
|
|
"""!
|
581
|
|
|
@brief Assign cluster to the BANG-block by index.
|
582
|
|
|
|
583
|
|
|
@param[in] index (uint): Index cluster that is assigned to BANG-block.
|
584
|
|
|
|
585
|
|
|
"""
|
586
|
|
|
self.__cluster = index
|
587
|
|
|
|
588
|
|
|
|
589
|
|
|
def is_neighbor(self, block):
|
590
|
|
|
"""!
|
591
|
|
|
@brief Performs calculation to check whether specified block is neighbor to the current.
|
592
|
|
|
|
593
|
|
|
@param[in] block (bang_block): Other BANG-block that should be checked for neighborhood.
|
594
|
|
|
|
595
|
|
|
@return (bool) True if blocks are neighbors, False if blocks are not neighbors.
|
596
|
|
|
|
597
|
|
|
"""
|
598
|
|
|
return self.get_spatial_block().is_neighbor(block.get_spatial_block())
|
599
|
|
|
|
600
|
|
|
|
601
|
|
|
def split(self, split_dimension, cache_points):
|
602
|
|
|
"""!
|
603
|
|
|
@brief Split BANG-block into two new blocks in specified dimension.
|
604
|
|
|
|
605
|
|
|
@param[in] split_dimension (uint): Dimension where block should be split.
|
606
|
|
|
@param[in] cache_points (bool): If True then covered points are cached. Used for leaf blocks.
|
607
|
|
|
|
608
|
|
|
@return (tuple) Pair of BANG-block that were formed from the current.
|
609
|
|
|
|
610
|
|
|
"""
|
611
|
|
|
left_region_number = self.__region_number
|
612
|
|
|
right_region_number = self.__region_number + 2 ** self.__level
|
613
|
|
|
|
614
|
|
|
first_spatial_block, second_spatial_block = self.__spatial_block.split(split_dimension)
|
615
|
|
|
|
616
|
|
|
left = bang_block(self.__data, left_region_number, self.__level + 1, first_spatial_block, cache_points)
|
617
|
|
|
right = bang_block(self.__data, right_region_number, self.__level + 1, second_spatial_block, cache_points)
|
618
|
|
|
|
619
|
|
|
return left, right
|
620
|
|
|
|
621
|
|
|
|
622
|
|
|
def __calculate_density(self):
|
623
|
|
|
"""!
|
624
|
|
|
@brief Calculates BANG-block density.
|
625
|
|
|
|
626
|
|
|
@return (double) BANG-block density.
|
627
|
|
|
|
628
|
|
|
"""
|
629
|
|
|
return self.__get_amount_points() / self.__spatial_block.get_volume()
|
630
|
|
|
|
631
|
|
|
|
632
|
|
|
def __get_amount_points(self):
|
633
|
|
|
"""!
|
634
|
|
|
@brief Count covered points by the BANG-block and if cache is enable then covered points are stored.
|
635
|
|
|
|
636
|
|
|
@return (uint) Amount of covered points.
|
637
|
|
|
|
638
|
|
|
"""
|
639
|
|
|
amount = 0
|
640
|
|
|
for index in range(len(self.__data)):
|
641
|
|
|
if self.__data[index] in self.__spatial_block:
|
642
|
|
|
self.__cache_point(index)
|
643
|
|
|
amount += 1
|
644
|
|
|
|
645
|
|
|
return amount
|
646
|
|
|
|
647
|
|
|
|
648
|
|
|
def __cache_covered_data(self):
|
649
|
|
|
"""!
|
650
|
|
|
@brief Cache covered data.
|
651
|
|
|
|
652
|
|
|
"""
|
653
|
|
|
self.__cache_points = True
|
654
|
|
|
self.__points = []
|
655
|
|
|
|
656
|
|
|
for index_point in range(len(self.__data)):
|
657
|
|
|
if self.__data[index_point] in self.__spatial_block:
|
658
|
|
|
self.__cache_point(index_point)
|
659
|
|
|
|
660
|
|
|
|
661
|
|
|
def __cache_point(self, index):
|
662
|
|
|
"""!
|
663
|
|
|
@brief Store index points.
|
664
|
|
|
|
665
|
|
|
@param[in] index (uint): Index point that should be stored.
|
666
|
|
|
|
667
|
|
|
"""
|
668
|
|
|
if self.__cache_points:
|
669
|
|
|
if self.__points is None:
|
670
|
|
|
self.__points = []
|
671
|
|
|
|
672
|
|
|
self.__points.append(index)
|
673
|
|
|
|
674
|
|
|
|
675
|
|
|
|
676
|
|
|
class bang:
|
677
|
|
|
"""!
|
678
|
|
|
@brief Class implements BANG grid based clustering algorithm.
|
679
|
|
|
@details BANG clustering algorithms uses a multidimensional grid structure to organize the value space surrounding
|
680
|
|
|
the pattern values. The patterns are grouped into blocks and clustered with respect to the blocks by
|
681
|
|
|
a topological neighbor search algorithm @cite inproceedings::bang::1.
|
682
|
|
|
|
683
|
|
|
Code example of BANG usage:
|
684
|
|
|
@code
|
685
|
|
|
from pyclustering.cluster.bang import bang, bang_visualizer
|
686
|
|
|
from pyclustering.utils import read_sample
|
687
|
|
|
from pyclustering.samples.definitions import FCPS_SAMPLES
|
688
|
|
|
|
689
|
|
|
# Read data three dimensional data.
|
690
|
|
|
data = read_sample(FCPS_SAMPLES.SAMPLE_CHAINLINK)
|
691
|
|
|
|
692
|
|
|
# Prepare algorithm's parameters.
|
693
|
|
|
levels = 11
|
694
|
|
|
|
695
|
|
|
# Create instance of BANG algorithm.
|
696
|
|
|
bang_instance = bang(data, levels)
|
697
|
|
|
bang_instance.process()
|
698
|
|
|
|
699
|
|
|
# Obtain clustering results.
|
700
|
|
|
clusters = bang_instance.get_clusters()
|
701
|
|
|
noise = bang_instance.get_noise()
|
702
|
|
|
directory = bang_instance.get_directory()
|
703
|
|
|
dendrogram = bang_instance.get_dendrogram()
|
704
|
|
|
|
705
|
|
|
# Visualize BANG clustering results.
|
706
|
|
|
bang_visualizer.show_blocks(directory)
|
707
|
|
|
bang_visualizer.show_dendrogram(dendrogram)
|
708
|
|
|
bang_visualizer.show_clusters(data, clusters, noise)
|
709
|
|
|
@endcode
|
710
|
|
|
|
711
|
|
|
There is visualization of BANG-clustering of three-dimensional data 'chainlink'. BANG-blocks that were formed during
|
712
|
|
|
processing are shown on following figure. The darkest color means highest density, blocks that does not cover points
|
713
|
|
|
are transparent:
|
714
|
|
|
@image html bang_blocks_chainlink.png "Fig. 1. BANG-blocks that cover input data."
|
715
|
|
|
|
716
|
|
|
Here is obtained dendrogram that can be used for further analysis to improve clustering results:
|
717
|
|
|
@image html bang_dendrogram_chainlink.png "Fig. 2. BANG dendrogram where the X-axis contains BANG-blocks, the Y-axis contains density."
|
718
|
|
|
|
719
|
|
|
BANG clustering result of 'chainlink' data:
|
720
|
|
|
@image html bang_clustering_chainlink.png "Fig. 3. BANG clustering result. Data: 'chainlink'."
|
721
|
|
|
|
722
|
|
|
"""
|
723
|
|
|
|
724
|
|
|
def __init__(self, data, levels, density_threshold=0.0, ccore=False):
|
725
|
|
|
"""!
|
726
|
|
|
@brief Create BANG clustering algorithm.
|
727
|
|
|
|
728
|
|
|
@param[in] data (list): Input data (list of points) that should be clustered.
|
729
|
|
|
@param[in] levels (uint): Amount of levels in tree (how many times block should be split).
|
730
|
|
|
@param[in] density_threshold (double): If block density is smaller than this value then contained data by this
|
731
|
|
|
block is considered as a noise.
|
732
|
|
|
@param[in] ccore (bool): Reserved positional argument - not used yet.
|
733
|
|
|
|
734
|
|
|
"""
|
735
|
|
|
self.__data = data
|
736
|
|
|
self.__levels = levels
|
737
|
|
|
self.__directory = None
|
738
|
|
|
self.__clusters = []
|
739
|
|
|
self.__noise = []
|
740
|
|
|
self.__cluster_blocks = []
|
741
|
|
|
self.__dendrogram = []
|
742
|
|
|
self.__density_threshold = density_threshold
|
743
|
|
|
self.__ccore = ccore
|
744
|
|
|
|
745
|
|
|
self.__validate_arguments()
|
746
|
|
|
|
747
|
|
|
|
748
|
|
|
def process(self):
|
749
|
|
|
"""!
|
750
|
|
|
@brief Performs clustering process in line with rules of BANG clustering algorithm.
|
751
|
|
|
|
752
|
|
|
@see get_clusters()
|
753
|
|
|
@see get_noise()
|
754
|
|
|
@see get_directory()
|
755
|
|
|
@see get_dendrogram()
|
756
|
|
|
|
757
|
|
|
"""
|
758
|
|
|
self.__directory = bang_directory(self.__data, self.__levels, self.__density_threshold)
|
759
|
|
|
self.__allocate_clusters()
|
760
|
|
|
|
761
|
|
|
|
762
|
|
|
def get_clusters(self):
|
763
|
|
|
"""!
|
764
|
|
|
@brief Returns allocated clusters.
|
765
|
|
|
|
766
|
|
|
@remark Allocated clusters are returned only after data processing (method process()). Otherwise empty list is returned.
|
767
|
|
|
|
768
|
|
|
@return (list) List of allocated clusters, each cluster contains indexes of objects in list of data.
|
769
|
|
|
|
770
|
|
|
@see process()
|
771
|
|
|
@see get_noise()
|
772
|
|
|
|
773
|
|
|
"""
|
774
|
|
|
return self.__clusters
|
775
|
|
|
|
776
|
|
|
|
777
|
|
|
def get_noise(self):
|
778
|
|
|
"""!
|
779
|
|
|
@brief Returns allocated noise.
|
780
|
|
|
|
781
|
|
|
@remark Allocated noise is returned only after data processing (method process()). Otherwise empty list is returned.
|
782
|
|
|
|
783
|
|
|
@return (list) List of indexes that are marked as a noise.
|
784
|
|
|
|
785
|
|
|
@see process()
|
786
|
|
|
@see get_clusters()
|
787
|
|
|
|
788
|
|
|
"""
|
789
|
|
|
return self.__noise
|
790
|
|
|
|
791
|
|
|
|
792
|
|
|
def get_directory(self):
|
793
|
|
|
"""!
|
794
|
|
|
@brief Returns grid directory that describes grid of the processed data.
|
795
|
|
|
|
796
|
|
|
@remark Grid directory is returned only after data processing (method process()). Otherwise None value is returned.
|
797
|
|
|
|
798
|
|
|
@return (bang_directory) BANG directory that describes grid of process data.
|
799
|
|
|
|
800
|
|
|
@see process()
|
801
|
|
|
|
802
|
|
|
"""
|
803
|
|
|
return self.__directory
|
804
|
|
|
|
805
|
|
|
|
806
|
|
|
def get_dendrogram(self):
|
807
|
|
|
"""!
|
808
|
|
|
@brief Returns dendrogram of clusters.
|
809
|
|
|
@details Dendrogram is created in following way: the density indices of all regions are calculated and sorted
|
810
|
|
|
in decreasing order for each cluster during clustering process.
|
811
|
|
|
|
812
|
|
|
@remark Dendrogram is returned only after data processing (method process()). Otherwise empty list is returned.
|
813
|
|
|
|
814
|
|
|
"""
|
815
|
|
|
return self.__dendrogram
|
816
|
|
|
|
817
|
|
|
|
818
|
|
|
def get_cluster_encoding(self):
|
819
|
|
|
"""!
|
820
|
|
|
@brief Returns clustering result representation type that indicate how clusters are encoded.
|
821
|
|
|
|
822
|
|
|
@return (type_encoding) Clustering result representation.
|
823
|
|
|
|
824
|
|
|
@see get_clusters()
|
825
|
|
|
|
826
|
|
|
"""
|
827
|
|
|
|
828
|
|
|
return type_encoding.CLUSTER_INDEX_LIST_SEPARATION
|
829
|
|
|
|
830
|
|
|
|
831
|
|
|
def __validate_arguments(self):
|
832
|
|
|
"""!
|
833
|
|
|
@brief Check input arguments of BANG algorithm and if one of them is not correct then appropriate exception
|
834
|
|
|
is thrown.
|
835
|
|
|
|
836
|
|
|
"""
|
837
|
|
|
if self.__levels <= 0:
|
838
|
|
|
raise ValueError("Incorrect amount of levels '%d'. Level value should be greater than 0." % self.__levels)
|
839
|
|
|
|
840
|
|
|
if len(self.__data) == 0:
|
841
|
|
|
raise ValueError("Empty input data. Data should contain at least one point.")
|
842
|
|
|
|
843
|
|
|
if self.__density_threshold < 0:
|
844
|
|
|
raise ValueError("Incorrect density threshold '%f'. Density threshold should not be negative." % self.__density_threshold)
|
845
|
|
|
|
846
|
|
|
|
847
|
|
|
def __allocate_clusters(self):
|
848
|
|
|
"""!
|
849
|
|
|
@brief Performs cluster allocation using leafs of tree in BANG directory (the smallest cells).
|
850
|
|
|
|
851
|
|
|
"""
|
852
|
|
|
leaf_blocks = self.__directory.get_leafs()
|
853
|
|
|
unhandled_block_indexes = set([i for i in range(len(leaf_blocks)) if leaf_blocks[i].get_density() > self.__density_threshold])
|
854
|
|
|
appropriate_block_indexes = set(unhandled_block_indexes)
|
|
|
|
|
855
|
|
|
|
856
|
|
|
current_block = self.__find_block_center(leaf_blocks, unhandled_block_indexes)
|
857
|
|
|
cluster_index = 0
|
858
|
|
|
|
859
|
|
|
while current_block is not None:
|
860
|
|
|
if current_block.get_density() <= self.__density_threshold:
|
861
|
|
|
break
|
862
|
|
|
|
863
|
|
|
self.__expand_cluster_block(current_block, cluster_index, leaf_blocks, unhandled_block_indexes)
|
864
|
|
|
|
865
|
|
|
current_block = self.__find_block_center(leaf_blocks, unhandled_block_indexes)
|
866
|
|
|
cluster_index += 1
|
867
|
|
|
|
868
|
|
|
self.__store_clustering_results(cluster_index, leaf_blocks)
|
869
|
|
|
|
870
|
|
|
|
871
|
|
|
def __expand_cluster_block(self, block, cluster_index, leaf_blocks, unhandled_block_indexes):
|
872
|
|
|
"""!
|
873
|
|
|
@brief Expand cluster from specific block that is considered as a central block.
|
874
|
|
|
|
875
|
|
|
@param[in] block (bang_block): Block that is considered as a central block for cluster.
|
876
|
|
|
@param[in] cluster_index (uint): Index of cluster that is assigned to blocks that forms new cluster.
|
877
|
|
|
@param[in] leaf_blocks (list): Leaf BANG-blocks that are considered during cluster formation.
|
878
|
|
|
@param[in] unhandled_block_indexes (set): Set of candidates (BANG block indexes) to become a cluster member. The
|
879
|
|
|
parameter helps to reduce traversing among BANG-block providing only restricted set of block that
|
880
|
|
|
should be considered.
|
881
|
|
|
|
882
|
|
|
"""
|
883
|
|
|
|
884
|
|
|
block.set_cluster(cluster_index)
|
885
|
|
|
self.__update_cluster_dendrogram(cluster_index, [block])
|
886
|
|
|
|
887
|
|
|
neighbors = self.__find_block_neighbors(block, leaf_blocks, unhandled_block_indexes)
|
888
|
|
|
self.__update_cluster_dendrogram(cluster_index, neighbors)
|
889
|
|
|
|
890
|
|
|
for neighbor in neighbors:
|
891
|
|
|
neighbor.set_cluster(cluster_index)
|
892
|
|
|
neighbor_neighbors = self.__find_block_neighbors(neighbor, leaf_blocks, unhandled_block_indexes)
|
893
|
|
|
self.__update_cluster_dendrogram(cluster_index, neighbor_neighbors)
|
894
|
|
|
|
895
|
|
|
neighbors += neighbor_neighbors
|
896
|
|
|
|
897
|
|
|
|
898
|
|
|
def __store_clustering_results(self, amount_clusters, leaf_blocks):
|
899
|
|
|
"""!
|
900
|
|
|
@brief Stores clustering results in a convenient way.
|
901
|
|
|
|
902
|
|
|
@param[in] amount_clusters (uint): Amount of cluster that was allocated during processing.
|
903
|
|
|
@param[in] leaf_blocks (list): Leaf BANG-blocks (the smallest cells).
|
904
|
|
|
|
905
|
|
|
"""
|
906
|
|
|
self.__clusters = [[] for _ in range(amount_clusters)]
|
907
|
|
|
for block in leaf_blocks:
|
908
|
|
|
index = block.get_cluster()
|
909
|
|
|
|
910
|
|
|
if index is not None:
|
911
|
|
|
self.__clusters[index] += block.get_points()
|
912
|
|
|
else:
|
913
|
|
|
self.__noise += block.get_points()
|
914
|
|
|
|
915
|
|
|
self.__clusters = [ list(set(cluster)) for cluster in self.__clusters ]
|
916
|
|
|
self.__noise = list(set(self.__noise))
|
917
|
|
|
|
918
|
|
|
|
919
|
|
|
def __find_block_center(self, level_blocks, unhandled_block_indexes):
|
920
|
|
|
"""!
|
921
|
|
|
@brief Search block that is cluster center for new cluster.
|
922
|
|
|
|
923
|
|
|
@return (bang_block) Central block for new cluster, if cluster is not found then None value is returned.
|
924
|
|
|
|
925
|
|
|
"""
|
926
|
|
|
for i in reversed(range(len(level_blocks))):
|
927
|
|
|
if level_blocks[i].get_density() <= self.__density_threshold:
|
928
|
|
|
return None
|
929
|
|
|
|
930
|
|
|
if level_blocks[i].get_cluster() is None:
|
931
|
|
|
unhandled_block_indexes.remove(i)
|
932
|
|
|
return level_blocks[i]
|
933
|
|
|
|
934
|
|
|
return None
|
935
|
|
|
|
936
|
|
|
|
937
|
|
|
def __find_block_neighbors(self, block, level_blocks, unhandled_block_indexes):
|
938
|
|
|
"""!
|
939
|
|
|
@brief Search block neighbors that are parts of new clusters (density is greater than threshold and that are
|
940
|
|
|
not cluster members yet), other neighbors are ignored.
|
941
|
|
|
|
942
|
|
|
@param[in] block (bang_block): BANG-block for which neighbors should be found (which can be part of cluster).
|
943
|
|
|
@param[in] level_blocks (list): BANG-blocks on specific level.
|
944
|
|
|
@param[in] unhandled_block_indexes (set): Blocks that have not been processed yet.
|
945
|
|
|
|
946
|
|
|
@return (list) Block neighbors that can become part of cluster.
|
947
|
|
|
|
948
|
|
|
"""
|
949
|
|
|
neighbors = []
|
950
|
|
|
|
951
|
|
|
handled_block_indexes = []
|
952
|
|
|
for unhandled_index in unhandled_block_indexes:
|
953
|
|
|
if block.is_neighbor(level_blocks[unhandled_index]):
|
954
|
|
|
handled_block_indexes.append(unhandled_index)
|
955
|
|
|
neighbors.append(level_blocks[unhandled_index])
|
956
|
|
|
|
957
|
|
|
# Maximum number of neighbors is eight
|
958
|
|
|
if len(neighbors) == 8:
|
959
|
|
|
break
|
960
|
|
|
|
961
|
|
|
for handled_index in handled_block_indexes:
|
962
|
|
|
unhandled_block_indexes.remove(handled_index)
|
963
|
|
|
|
964
|
|
|
return neighbors
|
965
|
|
|
|
966
|
|
|
|
967
|
|
|
def __update_cluster_dendrogram(self, index_cluster, blocks):
|
968
|
|
|
"""!
|
969
|
|
|
@brief Append clustered blocks to dendrogram.
|
970
|
|
|
|
971
|
|
|
@param[in] index_cluster (uint): Cluster index that was assigned to blocks.
|
972
|
|
|
@param[in] blocks (list): Blocks that were clustered.
|
973
|
|
|
|
974
|
|
|
"""
|
975
|
|
|
if len(self.__dendrogram) <= index_cluster:
|
976
|
|
|
self.__dendrogram.append([])
|
977
|
|
|
|
978
|
|
|
blocks = sorted(blocks, key=lambda block: block.get_density(), reverse=True)
|
979
|
|
|
self.__dendrogram[index_cluster] += blocks
|
980
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.