|
18 | 18 | from cluster import (HierarchicalClustering, KMeansClustering, ClusteringError) |
19 | 19 | from difflib import SequenceMatcher |
20 | 20 | import unittest |
| 21 | +try: |
| 22 | + import numpy |
| 23 | + NUMPY_AVAILABLE = True |
| 24 | +except: |
| 25 | + NUMPY_AVAILABLE = False |
21 | 26 |
|
22 | 27 |
|
23 | 28 | def compare_list(x, y): |
@@ -212,31 +217,26 @@ def testMultidimArray(self): |
212 | 217 | cl.getclusters(10) |
213 | 218 |
|
214 | 219 |
|
| 220 | +@unittest.skipUnless(NUMPY_AVAILABLE, |
| 221 | + 'numpy not available. Associated test will not be loaded!') |
215 | 222 | class NumpyTests(unittest.TestCase): |
216 | 223 |
|
217 | 224 | def testNumpyRandom(self): |
218 | | - from cluster import KMeansClustering |
219 | | - from numpy import random as rnd |
220 | | - data = rnd.rand(500, 2) |
| 225 | + data = numpy.random.rand(500, 2) |
221 | 226 | cl = KMeansClustering(data, lambda p0, p1: ( |
222 | 227 | p0[0] - p1[0]) ** 2 + (p0[1] - p1[1]) ** 2, numpy.array_equal) |
223 | 228 | cl.getclusters(10) |
224 | 229 |
|
225 | 230 |
|
226 | 231 | if __name__ == '__main__': |
227 | 232 | 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!" |
| 233 | + unittest.makeSuite(HClusterIntegerTestCase), |
| 234 | + unittest.makeSuite(HClusterSmallListTestCase), |
| 235 | + unittest.makeSuite(HClusterStringTestCase), |
| 236 | + unittest.makeSuite(KCluster2DTestCase), |
| 237 | + unittest.makeSuite(KClusterSFBugs)), |
| 238 | + unittest.makeSuite(KClusterSmallListTestCase), |
| 239 | + unittest.makeSuite(NumpyTests), |
| 240 | + ) |
241 | 241 |
|
242 | 242 | unittest.TextTestRunner(verbosity=2).run(suite) |
0 commit comments