File: clique_templates.py

package info (click to toggle)
python-pyclustering 0.10.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 11,128 kB
  • sloc: cpp: 38,888; python: 24,311; sh: 384; makefile: 105
file content (90 lines) | stat: -rwxr-xr-x 2,780 bytes parent folder | download | duplicates (2)
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
"""!

@brief Test templates for CLIQUE 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.clique import clique, clique_visualizer

from pyclustering.utils import read_sample


class clique_test_template:
    @staticmethod
    def clustering(path, intervals, density_threshold, expected_clusters, expected_noise, ccore_enabled, **kwargs):
        sample = read_sample(path)
        dimension = len(sample[0])

        clique_instance = clique(sample, intervals, density_threshold, ccore=ccore_enabled)
        clique_instance.process()

        clusters = clique_instance.get_clusters()
        noise = clique_instance.get_noise()
        cells = clique_instance.get_cells()

        assertion.eq(len(cells), pow(intervals, dimension))

        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)

        covered_points = set()
        for cell in cells:
            points = cell.points
            for index_point in points:
                covered_points.add(index_point)

        assertion.eq(len(sample), len(covered_points))
        return clique_instance


    @staticmethod
    def visualize(path, levels, threshold, ccore_enabled, **kwargs):
        sample = read_sample(path)

        clique_instance = clique(sample, levels, threshold, ccore=ccore_enabled)
        clique_instance.process()

        cells = clique_instance.get_cells()

        clique_visualizer.show_grid(cells, sample)


    @staticmethod
    def exception(type, sample_storage, levels, threshold, ccore_enabled):
        try:
            sample = sample_storage
            if isinstance(sample_storage, str):
                sample = read_sample(sample_storage)

            bang_instance = clique(sample, levels, threshold, ccore=ccore_enabled)
            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)