File: somsc_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 (61 lines) | stat: -rwxr-xr-x 2,099 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
"""!

@brief Test templates for SOM-SC clustering module.

@authors Andrei Novikov (pyclustering@yandex.ru)
@date 2014-2020
@copyright BSD-3-Clause

"""


import numpy
import unittest

from pyclustering.cluster.somsc import somsc

from pyclustering.utils import read_sample

from random import random


class SyncnetTestTemplates(unittest.TestCase):
    def templateLengthProcessData(self, path_to_file, amount_clusters, expected_cluster_length, ccore):
        sample = read_sample(path_to_file)
        
        somsc_instance = somsc(sample, amount_clusters, 100, ccore)
        somsc_instance.process()
        
        clusters = somsc_instance.get_clusters()

        obtained_cluster_sizes = [len(cluster) for cluster in clusters]
        self.assertEqual(len(sample), sum(obtained_cluster_sizes))
        
        if expected_cluster_length is not None:
            obtained_cluster_sizes.sort()
            expected_cluster_length.sort()
            self.assertEqual(obtained_cluster_sizes,expected_cluster_length)


    def templateClusterAllocationOneDimensionData(self, ccore_flag):
        input_data = [[random()] for i in range(10)] + [[random() + 3] for i in range(10)] + \
                     [[random() + 5] for i in range(10)] + [[random() + 8] for i in range(10)]

        somsc_instance = somsc(input_data, 4, 100, ccore_flag)
        somsc_instance.process()
        clusters = somsc_instance.get_clusters()

        self.assertEqual(len(clusters), 4)
        for cluster in clusters:
            self.assertEqual(len(cluster), 10)


    def predict(self, path_to_file, amount_clusters, points, expected_closest_clusters, ccore):
        sample = read_sample(path_to_file)

        somsc_instance = somsc(sample, amount_clusters, 100, ccore)
        somsc_instance.process()

        closest_clusters = somsc_instance.predict(points)
        self.assertEqual(len(expected_closest_clusters), len(closest_clusters))
        self.assertTrue(numpy.array_equal(numpy.array(expected_closest_clusters), closest_clusters))