@@ -73,27 +73,104 @@ def setUp(self):
7373 self .__data = [791 , 956 , 676 , 124 , 564 , 84 , 24 , 365 , 594 , 940 , 398 ,
7474 971 , 131 , 365 , 542 , 336 , 518 , 835 , 134 , 391 ]
7575
76- def testCluster (self ):
76+ def testSingleLinkage (self ):
7777 "Basic Hierarchical Clustering test with integers"
7878 cl = HierarchicalClustering (self .__data , lambda x , y : abs (x - y ))
79- cl .cluster ()
79+ result = cl .getlevel (40 )
80+
81+ # sort the values to make the tests less prone to algorithm changes
82+ result = sorted ([sorted (_ ) for _ in result ])
8083 self .assertEqual ([
8184 [24 ],
8285 [84 , 124 , 131 , 134 ],
8386 [336 , 365 , 365 , 391 , 398 ],
87+ [518 , 542 , 564 , 594 ],
8488 [676 ],
85- [594 , 518 , 542 , 564 ],
89+ [791 ],
90+ [835 ],
8691 [940 , 956 , 971 ],
92+ ], result )
93+
94+ def testCompleteLinkage (self ):
95+ "Basic Hierarchical Clustering test with integers"
96+ cl = HierarchicalClustering (self .__data ,
97+ lambda x , y : abs (x - y ),
98+ linkage = 'complete' )
99+ result = cl .getlevel (40 )
100+
101+ # sort the values to make the tests less prone to algorithm changes
102+ result = sorted ([sorted (_ ) for _ in result ])
103+
104+ expected = [
105+ [24 ],
106+ [84 ],
107+ [124 , 131 , 134 ],
108+ [336 , 365 , 365 ],
109+ [391 , 398 ],
110+ [518 ],
111+ [542 , 564 ],
112+ [594 ],
113+ [676 ],
87114 [791 ],
88115 [835 ],
89- ], cl .getlevel (40 ))
116+ [940 , 956 , 971 ],
117+ ]
118+ self .assertEqual (result , expected )
119+
120+ def testUCLUS (self ):
121+ "Basic Hierarchical Clustering test with integers"
122+ cl = HierarchicalClustering (self .__data ,
123+ lambda x , y : abs (x - y ),
124+ linkage = 'uclus' )
125+ expected = [
126+ [24 ],
127+ [84 ],
128+ [124 , 131 , 134 ],
129+ [336 , 365 , 365 , 391 , 398 ],
130+ [518 , 542 , 564 ],
131+ [594 ],
132+ [676 ],
133+ [791 ],
134+ [835 ],
135+ [940 , 956 , 971 ],
136+ ]
137+ result = sorted ([sorted (_ ) for _ in cl .getlevel (40 )])
138+ self .assertEqual (result , expected )
139+
140+ def testAverageLinkage (self ):
141+ cl = HierarchicalClustering (self .__data ,
142+ lambda x , y : abs (x - y ),
143+ linkage = 'average' )
144+ # TODO: The current test-data does not really trigger a difference
145+ # between UCLUS and "average" linkage.
146+ expected = [
147+ [24 ],
148+ [84 ],
149+ [124 , 131 , 134 ],
150+ [336 , 365 , 365 , 391 , 398 ],
151+ [518 , 542 , 564 ],
152+ [594 ],
153+ [676 ],
154+ [791 ],
155+ [835 ],
156+ [940 , 956 , 971 ],
157+ ]
158+ result = sorted ([sorted (_ ) for _ in cl .getlevel (40 )])
159+ self .assertEqual (result , expected )
90160
91161 def testUnmodifiedData (self ):
92162 cl = HierarchicalClustering (self .__data , lambda x , y : abs (x - y ))
93163 new_data = []
94164 [new_data .extend (_ ) for _ in cl .getlevel (40 )]
95165 self .assertEqual (sorted (new_data ), sorted (self .__data ))
96166
167+ def testMultiprocessing (self ):
168+ cl = HierarchicalClustering (self .__data , lambda x , y : abs (x - y ),
169+ num_processes = 4 )
170+ new_data = []
171+ [new_data .extend (_ ) for _ in cl .getlevel (40 )]
172+ self .assertEqual (sorted (new_data ), sorted (self .__data ))
173+
97174
98175class HClusterStringTestCase (unittest .TestCase ):
99176
0 commit comments