1515# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1616#
1717
18- from cluster import (HierarchicalClustering , KMeansClustering , ClusteringError )
18+ """
19+ Tests for hierarchical clustering.
20+
21+ .. note::
22+
23+ Even though the results are lists, the order of items in the resulting
24+ clusters is non-deterministic. This should be taken into consideration when
25+ writing "expected" values!
26+ """
27+
28+ from sys import hexversion
1929from difflib import SequenceMatcher
2030import unittest
31+
2132try :
2233 import numpy
2334 NUMPY_AVAILABLE = True
2435except :
2536 NUMPY_AVAILABLE = False
2637
38+ from cluster import (HierarchicalClustering , KMeansClustering , ClusteringError )
39+
2740
2841def compare_list (x , y ):
2942 """
@@ -51,13 +64,20 @@ class HClusterSmallListTestCase(unittest.TestCase):
5164 Test for Bug #1516204
5265 """
5366
67+ def __init__ (self , * args , ** kwargs ):
68+ super (HClusterSmallListTestCase , self ).__init__ (* args , ** kwargs )
69+ if hexversion < 0x030000f0 :
70+ self .assertCItemsEqual = self .assertItemsEqual
71+ else :
72+ self .assertCItemsEqual = self .assertCountEqual
73+
5474 def testClusterLen1 (self ):
5575 """
5676 Testing if hierarchical clustering a set of length 1 returns a set of
5777 length 1
5878 """
5979 cl = HierarchicalClustering ([876 ], lambda x , y : abs (x - y ))
60- self .assertEqual ([876 ], cl .getlevel (40 ))
80+ self .assertCItemsEqual ([876 ], cl .getlevel (40 ))
6181
6282 def testClusterLen0 (self ):
6383 """
@@ -69,6 +89,13 @@ def testClusterLen0(self):
6989
7090class HClusterIntegerTestCase (unittest .TestCase ):
7191
92+ def __init__ (self , * args , ** kwargs ):
93+ super (HClusterIntegerTestCase , self ).__init__ (* args , ** kwargs )
94+ if hexversion < 0x030000f0 :
95+ self .assertCItemsEqual = self .assertItemsEqual
96+ else :
97+ self .assertCItemsEqual = self .assertCountEqual
98+
7299 def setUp (self ):
73100 self .__data = [791 , 956 , 676 , 124 , 564 , 84 , 24 , 365 , 594 , 940 , 398 ,
74101 971 , 131 , 365 , 542 , 336 , 518 , 835 , 134 , 391 ]
@@ -79,15 +106,15 @@ def testSingleLinkage(self):
79106 result = cl .getlevel (40 )
80107
81108 # sort the values to make the tests less prone to algorithm changes
82- result = sorted ( [sorted (_ ) for _ in result ])
83- self .assertEqual ([
109+ result = [sorted (_ ) for _ in result ]
110+ self .assertCItemsEqual ([
84111 [24 ],
85- [84 , 124 , 131 , 134 ],
86112 [336 , 365 , 365 , 391 , 398 ],
87113 [518 , 542 , 564 , 594 ],
88114 [676 ],
89115 [791 ],
90116 [835 ],
117+ [84 , 124 , 131 , 134 ],
91118 [940 , 956 , 971 ],
92119 ], result )
93120
0 commit comments