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
|
"""!
@brief Test templates for BANG algorithm.
@authors Andrei Novikov (pyclustering@yandex.ru)
@date 2014-2020
@copyright BSD-3-Clause
"""
# Generate images without having a window appear.
import matplotlib
matplotlib.use('Agg')
from pyclustering.tests.assertion import assertion
from pyclustering.cluster.bang import bang, bang_visualizer, bang_animator
from pyclustering.utils import read_sample
class bang_test_template:
@staticmethod
def clustering(path, levels, density_threshold, expected_clusters, expected_noise, ccore, **kwargs):
sample = read_sample(path)
amount_threshold = kwargs.get('amount_threshold', 0)
bang_instance = bang(sample, levels, ccore,
density_threshold=density_threshold,
amount_threshold=amount_threshold)
bang_instance.process()
clusters = bang_instance.get_clusters()
noise = bang_instance.get_noise()
directory = bang_instance.get_directory()
dendrogram = bang_instance.get_dendrogram()
assertion.eq(len(clusters), len(dendrogram))
obtained_length = len(noise)
obtained_cluster_length = []
for cluster in clusters:
obtained_length += len(cluster)
obtained_cluster_length.append(len(cluster))
obtained_cluster_length.sort()
assertion.eq(len(sample), obtained_length)
assertion.eq(expected_noise, len(noise))
if expected_clusters is not None:
assertion.eq(len(expected_clusters), len(clusters))
assertion.eq(expected_clusters, obtained_cluster_length)
leafs = directory.get_leafs()
covered_points = set()
for leaf in leafs:
points = leaf.get_points()
for index_point in points:
covered_points.add(index_point)
assertion.eq(len(sample), len(covered_points))
return bang_instance
@staticmethod
def visualize(path, levels, threshold, ccore, **kwargs):
sample = read_sample(path)
bang_instance = bang(sample, levels, ccore, density_threshold=threshold)
bang_instance.process()
directory = bang_instance.get_directory()
dendrogram = bang_instance.get_dendrogram()
bang_visualizer.show_blocks(directory)
bang_visualizer.show_dendrogram(dendrogram)
@staticmethod
def animate(path, levels, threshold, ccore, **kwargs):
sample = read_sample(path)
bang_instance = bang(sample, levels, ccore, density_threshold=threshold)
bang_instance.process()
directory = bang_instance.get_directory()
clusters = bang_instance.get_clusters()
noise = bang_instance.get_noise()
animator = bang_animator(directory, clusters)
animator.animate()
@staticmethod
def exception(type, sample_storage, levels, threshold, ccore):
try:
sample = sample_storage
if isinstance(sample_storage, str):
sample = read_sample(sample_storage)
bang_instance = bang(sample, levels, ccore, density_threshold=threshold)
bang_instance.process()
except type:
return
except Exception as ex:
raise AssertionError("Expected: '%s', Actual: '%s'" % (type, type(ex).__name__))
raise AssertionError("Expected: '%s', Actual: 'None'" % type)
|