@@ -202,15 +202,41 @@ def testLostFunctionReference(self):
202202 expected ),
203203 "Elements differ!\n %s\n %s" % (clusters , expected ))
204204
205+ def testMultidimArray (self ):
206+ from random import random
207+ data = []
208+ for _ in range (200 ):
209+ data .append ([random (), random ()])
210+ cl = KMeansClustering (data , lambda p0 , p1 : (
211+ p0 [0 ] - p1 [0 ]) ** 2 + (p0 [1 ] - p1 [1 ]) ** 2 )
212+ cl .getclusters (10 )
213+
214+
215+ class NumpyTests (unittest .TestCase ):
216+
217+ def testNumpyRandom (self ):
218+ from cluster import KMeansClustering
219+ from numpy import random as rnd
220+ data = rnd .rand (500 , 2 )
221+ cl = KMeansClustering (data , lambda p0 , p1 : (
222+ p0 [0 ] - p1 [0 ]) ** 2 + (p0 [1 ] - p1 [1 ]) ** 2 , numpy .array_equal )
223+ cl .getclusters (10 )
224+
205225
206226if __name__ == '__main__' :
207- unittest .TextTestRunner (verbosity = 2 ).run (
208- unittest .TestSuite ((
209- unittest .makeSuite (HClusterSmallListTestCase ),
210- unittest .makeSuite (HClusterIntegerTestCase ),
211- unittest .makeSuite (HClusterStringTestCase ),
212- unittest .makeSuite (KClusterSmallListTestCase ),
213- unittest .makeSuite (KCluster2DTestCase ),
214- unittest .makeSuite (KClusterSFBugs ),
215- ))
216- )
227+ suite = unittest .TestSuite ((
228+ unittest .makeSuite (HClusterSmallListTestCase ),
229+ unittest .makeSuite (HClusterIntegerTestCase ),
230+ unittest .makeSuite (HClusterStringTestCase ),
231+ unittest .makeSuite (KClusterSmallListTestCase ),
232+ unittest .makeSuite (KCluster2DTestCase ),
233+ unittest .makeSuite (KClusterSFBugs )))
234+
235+ try :
236+ import numpy # NOQA
237+ tests = unittest .makeSuite (NumpyTests )
238+ suite .addTests (tests )
239+ except ImportError :
240+ print "numpy not available. Associated test will not be loaded!"
241+
242+ unittest .TextTestRunner (verbosity = 2 ).run (suite )
0 commit comments