| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
from cluster import (KMeansClustering, ClusteringError) |
| 19 |
import unittest |
| 20 |
|
| 21 |
|
| 22 |
def compare_list(x, y): |
| 23 |
""" |
| 24 |
Compare lists by content. Ordering does not matter. |
| 25 |
Returns True if both lists contain the same items (and are of identical |
| 26 |
length) |
| 27 |
""" |
| 28 |
|
| 29 |
cmpx = [set(cluster) for cluster in x] |
| 30 |
cmpy = [set(cluster) for cluster in y] |
| 31 |
|
| 32 |
all_ok = True |
| 33 |
|
| 34 |
for cset in cmpx: |
| 35 |
all_ok &= cset in cmpy |
| 36 |
|
| 37 |
for cset in cmpy: |
| 38 |
all_ok &= cset in cmpx |
| 39 |
|
| 40 |
return all_ok |
| 41 |
|
| 42 |
|
| 43 |
class KClusterSmallListTestCase(unittest.TestCase): |
| 44 |
|
| 45 |
def testClusterLen1(self): |
| 46 |
"Testing that a search space of length 1 returns only one cluster" |
| 47 |
cl = KMeansClustering([876]) |
| 48 |
self.assertEqual([876], cl.getclusters(2)) |
| 49 |
self.assertEqual([876], cl.getclusters(5)) |
| 50 |
|
| 51 |
def testClusterLen0(self): |
| 52 |
"Testing if clustering an empty set, returns an empty set" |
| 53 |
cl = KMeansClustering([]) |
| 54 |
self.assertEqual([], cl.getclusters(2)) |
| 55 |
self.assertEqual([], cl.getclusters(7)) |
| 56 |
|
| 57 |
|
| 58 |
class KCluster2DTestCase(unittest.TestCase): |
| 59 |
|
| 60 |
def testClusterCount(self): |
| 61 |
"Test that asking for less than 2 clusters raises an error" |
| 62 |
cl = KMeansClustering([876, 123, 344, 676], |
| 63 |
distance=lambda x, y: abs(x - y)) |
| 64 |
self.assertRaises(ClusteringError, cl.getclusters, 0) |
| 65 |
self.assertRaises(ClusteringError, cl.getclusters, 1) |
| 66 |
|
| 67 |
def testNonsenseCluster(self): |
| 68 |
""" |
| 69 |
Test that asking for more clusters than data-items available raises an |
| 70 |
error |
| 71 |
""" |
| 72 |
cl = KMeansClustering([876, 123], distance=lambda x, y: abs(x - y)) |
| 73 |
self.assertRaises(ClusteringError, cl.getclusters, 5) |
| 74 |
|
| 75 |
def testUniformLength(self): |
| 76 |
""" |
| 77 |
Test if there is an item in the cluster that has a different |
| 78 |
cardinality |
| 79 |
""" |
| 80 |
data = [(1, 5), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6, 7), (7, 3), |
| 81 |
(8, 1), (8, 2), (8), (9, 2), (9, 3)] |
| 82 |
self.assertRaises(ValueError, KMeansClustering, data) |
| 83 |
|
| 84 |
def testPointDoubling(self): |
| 85 |
"test for bug #1604868" |
| 86 |
data = [(18, 13), (15, 12), (17, 12), (18, 12), (19, 12), (16, 11), |
| 87 |
(18, 11), (19, 10), (0, 0), (1, 4), (1, 2), (2, 3), (4, 1), |
| 88 |
(4, 3), (5, 2), (6, 1)] |
| 89 |
cl = KMeansClustering(data) |
| 90 |
clusters = cl.getclusters(2) |
| 91 |
expected = [[(18, 13), (15, 12), (17, 12), (18, 12), (19, 12), |
| 92 |
(16, 11), (18, 11), (19, 10)], |
| 93 |
[(0, 0), (1, 4), (1, 2), (2, 3), (4, 1), |
| 94 |
(5, 2), (6, 1), (4, 3)]] |
| 95 |
self.assertTrue(compare_list( |
| 96 |
clusters, |
| 97 |
expected), |
| 98 |
"Elements differ!\n%s\n%s" % (clusters, expected)) |
| 99 |
|
| 100 |
def testClustering(self): |
| 101 |
"Basic clustering test" |
| 102 |
data = [(8, 2), (7, 3), (2, 6), (3, 5), (3, 6), (1, 5), (8, 1), |
| 103 |
(3, 4), (8, 3), (9, 2), (2, 5), (9, 3)] |
| 104 |
cl = KMeansClustering(data) |
| 105 |
self.assertEqual( |
| 106 |
cl.getclusters(2), |
| 107 |
[[(8, 2), (8, 1), (8, 3), (7, 3), (9, 2), (9, 3)], |
| 108 |
[(3, 5), (1, 5), (3, 4), (2, 6), (2, 5), (3, 6)]]) |
| 109 |
|
| 110 |
def testUnmodifiedData(self): |
| 111 |
"Basic clustering test" |
| 112 |
data = [(8, 2), (7, 3), (2, 6), (3, 5), (3, 6), (1, 5), (8, 1), |
| 113 |
(3, 4), (8, 3), (9, 2), (2, 5), (9, 3)] |
| 114 |
cl = KMeansClustering(data) |
| 115 |
|
| 116 |
new_data = [] |
| 117 |
[new_data.extend(_) for _ in cl.getclusters(2)] |
| 118 |
self.assertEqual(sorted(new_data), sorted(data)) |
| 119 |
|
| 120 |
|
| 121 |
class KClusterSFBugs(unittest.TestCase): |
| 122 |
|
| 123 |
def testLostFunctionReference(self): |
| 124 |
"test for bug #1727558" |
| 125 |
cl = KMeansClustering([(1, 1), (20, 40), (20, 41)], |
| 126 |
lambda x, y: x + y) |
| 127 |
clusters = cl.getclusters(3) |
| 128 |
expected = [(1, 1), (20, 40), (20, 41)] |
| 129 |
self.assertTrue(compare_list( |
| 130 |
clusters, |
| 131 |
expected), |
| 132 |
"Elements differ!\n%s\n%s" % (clusters, expected)) |
| 133 |
|
| 134 |
def testMultidimArray(self): |
| 135 |
from random import random |
| 136 |
data = [] |
| 137 |
for _ in range(200): |
| 138 |
data.append([random(), random()]) |
| 139 |
cl = KMeansClustering(data, lambda p0, p1: ( |
| 140 |
p0[0] - p1[0]) ** 2 + (p0[1] - p1[1]) ** 2) |
| 141 |
cl.getclusters(10) |