How to use assertAlmostEqual method in autotest

Best Python code snippet using autotest_python

test_Cluster.py

Source:test_Cluster.py Github

copy

Full Screen

...26 from Bio.Cluster import mean, median27 elif TestCluster.module == 'Pycluster':28 from Pycluster import mean, median29 data = numpy.array([34.3, 3, 2])30 self.assertAlmostEqual(mean(data), 13.1, places=3)31 self.assertAlmostEqual(median(data), 3.0, places=3)32 data = [5, 10, 15, 20]33 self.assertAlmostEqual(mean(data), 12.5, places=3)34 self.assertAlmostEqual(median(data), 12.5, places=3)35 data = [1, 2, 3, 5, 7, 11, 13, 17]36 self.assertAlmostEqual(mean(data), 7.375, places=3)37 self.assertAlmostEqual(median(data), 6.0, places=3)38 data = [100, 19, 3, 1.5, 1.4, 1, 1, 1]39 self.assertAlmostEqual(mean(data), 15.988, places=3)40 self.assertAlmostEqual(median(data), 1.45, places=3)41 def test_matrix_parse(self):42 if TestCluster.module == 'Bio.Cluster':43 from Bio.Cluster import treecluster44 elif TestCluster.module == 'Pycluster':45 from Pycluster import treecluster46 # Normal matrix, no errors47 data1 = numpy.array([[1.1, 1.2],48 [1.4, 1.3],49 [1.1, 1.5],50 [2.0, 1.5],51 [1.7, 1.9],52 [1.7, 1.9],53 [5.7, 5.9],54 [5.7, 5.9],55 [3.1, 3.3],56 [5.4, 5.3],57 [5.1, 5.5],58 [5.0, 5.5],59 [5.1, 5.2]])60 # Another normal matrix, no errors; written as a list61 data2 = [[1.1, 2.2, 3.3, 4.4, 5.5],62 [3.1, 3.2, 1.3, 2.4, 1.5],63 [4.1, 2.2, 0.3, 5.4, 0.5],64 [12.1, 2.0, 0.0, 5.0, 0.0]]65 # Ragged matrix66 data3 = [[91.1, 92.2, 93.3, 94.4, 95.5],67 [93.1, 93.2, 91.3, 92.4],68 [94.1, 92.2, 90.3],69 [12.1, 92.0, 90.0, 95.0, 90.0]]70 # Matrix with bad cells71 data4 = [[7.1, 7.2, 7.3, 7.4, 7.5],72 [7.1, 7.2, 7.3, 7.4, 'snoopy'],73 [7.1, 7.2, 7.3, None, None]]74 # Matrix with a bad row75 data5 = [[23.1, 23.2, 23.3, 23.4, 23.5],76 None,77 [23.1, 23.0, 23.0, 23.0, 23.0]]78 # Various references that don't point to matrices at all79 data6 = "snoopy"80 data7 = {'a': [[2.3, 1.2], [3.3, 5.6]]}81 data8 = []82 data9 = [None]83 try:84 treecluster(data1)85 except Exception: # TODO - Which exceptions?86 self.fail("treecluster failed to accept matrix data1")87 try:88 treecluster(data2)89 except Exception: # TODO - Which exceptions?90 self.fail("treecluster failed to accept matrix data2")91 self.assertRaises(TypeError, lambda: treecluster(data3))92 self.assertRaises(TypeError, lambda: treecluster(data4))93 self.assertRaises(TypeError, lambda: treecluster(data5))94 self.assertRaises(TypeError, lambda: treecluster(data6))95 self.assertRaises(TypeError, lambda: treecluster(data7))96 self.assertRaises(TypeError, lambda: treecluster(data8))97 self.assertRaises(TypeError, lambda: treecluster(data9))98 def test_kcluster(self):99 if TestCluster.module == 'Bio.Cluster':100 from Bio.Cluster import kcluster101 elif TestCluster.module == 'Pycluster':102 from Pycluster import kcluster103 nclusters = 3104 # First data set105 weight = numpy.array([1, 1, 1, 1, 1])106 data = numpy.array([[1.1, 2.2, 3.3, 4.4, 5.5],107 [3.1, 3.2, 1.3, 2.4, 1.5],108 [4.1, 2.2, 0.3, 5.4, 0.5],109 [12.1, 2.0, 0.0, 5.0, 0.0]])110 mask = numpy.array([[1, 1, 1, 1, 1],111 [1, 1, 1, 1, 1],112 [1, 1, 1, 1, 1],113 [1, 1, 1, 1, 1]], int)114 # TODO - Use a context manager here once we drop Python 2.6115 # Method should be one letter:116 self.assertRaises(ValueError, kcluster, data,117 **{"nclusters": nclusters, "mask": mask,118 "weight": weight, "transpose": 0, "npass": 100,119 "method": "any", "dist": "e"})120 # Distance should be one letter:121 self.assertRaises(ValueError, kcluster, data,122 **{"nclusters": nclusters, "mask": mask,123 "weight": weight, "transpose": 0, "npass": 100,124 "method": "a", "dist": "euclidean"})125 clusterid, error, nfound = kcluster(data, nclusters=nclusters,126 mask=mask, weight=weight,127 transpose=0, npass=100,128 method='a', dist='e')129 self.assertEqual(len(clusterid), len(data))130 correct = [0, 1, 1, 2]131 mapping = [clusterid[correct.index(i)] for i in range(nclusters)]132 for i in range(len(clusterid)):133 self.assertEqual(clusterid[i], mapping[correct[i]])134 # Second data set135 weight = numpy.array([1, 1])136 data = numpy.array([[1.1, 1.2],137 [1.4, 1.3],138 [1.1, 1.5],139 [2.0, 1.5],140 [1.7, 1.9],141 [1.7, 1.9],142 [5.7, 5.9],143 [5.7, 5.9],144 [3.1, 3.3],145 [5.4, 5.3],146 [5.1, 5.5],147 [5.0, 5.5],148 [5.1, 5.2]])149 mask = numpy.array([[1, 1],150 [1, 1],151 [1, 1],152 [1, 1],153 [1, 1],154 [1, 1],155 [1, 1],156 [1, 1],157 [1, 1],158 [1, 1],159 [1, 1],160 [1, 1],161 [1, 1]], int)162 # TODO - Use a context manager here once we drop Python 2.6163 # Method should be one letter:164 self.assertRaises(ValueError, kcluster, data,165 **{"nclusters": 3, "mask": mask,166 "weight": weight, "transpose": 0, "npass": 100,167 "method": "any", "dist": "e"})168 # Distance should be one letter:169 self.assertRaises(ValueError, kcluster, data,170 **{"nclusters": 3, "mask": mask,171 "weight": weight, "transpose": 0, "npass": 100,172 "method": "a", "dist": "euclidean"})173 clusterid, error, nfound = kcluster(data, nclusters=3, mask=mask,174 weight=weight, transpose=0,175 npass=100, method='a', dist='e')176 self.assertEqual(len(clusterid), len(data))177 correct = [0, 0, 0, 0, 0, 0, 1, 1, 2, 1, 1, 1, 1]178 mapping = [clusterid[correct.index(i)] for i in range(nclusters)]179 for i in range(len(clusterid)):180 self.assertEqual(clusterid[i], mapping[correct[i]])181 def test_clusterdistance(self):182 if TestCluster.module == 'Bio.Cluster':183 from Bio.Cluster import clusterdistance184 elif TestCluster.module == 'Pycluster':185 from Pycluster import clusterdistance186 # First data set187 weight = numpy.array([1, 1, 1, 1, 1])188 data = numpy.array([[1.1, 2.2, 3.3, 4.4, 5.5],189 [3.1, 3.2, 1.3, 2.4, 1.5],190 [4.1, 2.2, 0.3, 5.4, 0.5],191 [12.1, 2.0, 0.0, 5.0, 0.0]])192 mask = numpy.array([[1, 1, 1, 1, 1],193 [1, 1, 1, 1, 1],194 [1, 1, 1, 1, 1],195 [1, 1, 1, 1, 1]], int)196 # Cluster assignments197 c1 = [0]198 c2 = [1, 2]199 c3 = [3]200 # TODO - Use a context manager here once we drop Python 2.6201 # Method should be one letter:202 self.assertRaises(ValueError, clusterdistance, data,203 **{"mask": mask, "weight": weight,204 "index1": c1, "index2": c2, "transpose": 0,205 "method": "any", "dist": "e"})206 # Distance should be one letter:207 self.assertRaises(ValueError, clusterdistance, data,208 **{"mask": mask, "weight": weight,209 "index1": c1, "index2": c2, "transpose": 0,210 "method": "a", "dist": "euclidean"})211 distance = clusterdistance(data, mask=mask, weight=weight,212 index1=c1, index2=c2, dist='e',213 method='a', transpose=0)214 self.assertAlmostEqual(distance, 6.650, places=3)215 distance = clusterdistance(data, mask=mask, weight=weight,216 index1=c1, index2=c3, dist='e',217 method='a', transpose=0)218 self.assertAlmostEqual(distance, 32.508, places=3)219 distance = clusterdistance(data, mask=mask, weight=weight,220 index1=c2, index2=c3, dist='e',221 method='a', transpose=0)222 self.assertAlmostEqual(distance, 15.118, places=3)223 # Second data set224 weight = numpy.array([1, 1])225 data = numpy.array([[1.1, 1.2],226 [1.4, 1.3],227 [1.1, 1.5],228 [2.0, 1.5],229 [1.7, 1.9],230 [1.7, 1.9],231 [5.7, 5.9],232 [5.7, 5.9],233 [3.1, 3.3],234 [5.4, 5.3],235 [5.1, 5.5],236 [5.0, 5.5],237 [5.1, 5.2]])238 mask = numpy.array([[1, 1],239 [1, 1],240 [1, 1],241 [1, 1],242 [1, 1],243 [1, 1],244 [1, 1],245 [1, 1],246 [1, 1],247 [1, 1],248 [1, 1],249 [1, 1],250 [1, 1]], int)251 # Cluster assignments252 c1 = [0, 1, 2, 3]253 c2 = [4, 5, 6, 7]254 c3 = [8]255 # TODO - Use a context manager here once we drop Python 2.6256 # Method should be one letter:257 self.assertRaises(ValueError, clusterdistance, data,258 **{"mask": mask, "weight": weight,259 "index1": c1, "index2": c2,260 "method": "any", "dist": "e",261 "transpose": 0})262 # Distance should be one letter:263 self.assertRaises(ValueError, clusterdistance, data,264 **{"mask": mask, "weight": weight,265 "index1": c1, "index2": c2,266 "method": "a", "dist": "euclidena",267 "transpose": 0})268 distance = clusterdistance(data, mask=mask, weight=weight,269 index1=c1, index2=c2, dist='e',270 method='a', transpose=0)271 self.assertAlmostEqual(distance, 5.833, places=3)272 distance = clusterdistance(data, mask=mask, weight=weight,273 index1=c1, index2=c3, dist='e',274 method='a', transpose=0)275 self.assertAlmostEqual(distance, 3.298, places=3)276 distance = clusterdistance(data, mask=mask, weight=weight,277 index1=c2, index2=c3, dist='e',278 method='a', transpose=0)279 self.assertAlmostEqual(distance, 0.360, places=3)280 def test_treecluster(self):281 if TestCluster.module == 'Bio.Cluster':282 from Bio.Cluster import treecluster283 elif TestCluster.module == 'Pycluster':284 from Pycluster import treecluster285 # First data set286 weight1 = [1, 1, 1, 1, 1]287 data1 = numpy.array([[1.1, 2.2, 3.3, 4.4, 5.5],288 [3.1, 3.2, 1.3, 2.4, 1.5],289 [4.1, 2.2, 0.3, 5.4, 0.5],290 [12.1, 2.0, 0.0, 5.0, 0.0]])291 mask1 = numpy.array([[1, 1, 1, 1, 1],292 [1, 1, 1, 1, 1],293 [1, 1, 1, 1, 1],294 [1, 1, 1, 1, 1]], int)295 # TODO - Use a context manager here once we drop Python 2.6296 # Method should be one letter:297 self.assertRaises(ValueError, treecluster,298 **{"data": data1, "mask": mask1, "weight": weight1,299 "transpose": 0, "method": "any", "dist": "e"})300 # Distance should be one letter:301 self.assertRaises(ValueError, treecluster,302 **{"data": data1, "mask": mask1, "weight": weight1,303 "transpose": 0, "method": "any", "dist": "euclidean"})304 # test first data set305 # Pairwise average-linkage clustering"306 tree = treecluster(data=data1, mask=mask1, weight=weight1,307 transpose=0, method='a', dist='e')308 self.assertEqual(len(tree), len(data1) - 1)309 self.assertEqual(tree[0].left, 2)310 self.assertEqual(tree[0].right, 1)311 self.assertAlmostEqual(tree[0].distance, 2.600, places=3)312 self.assertEqual(tree[1].left, -1)313 self.assertEqual(tree[1].right, 0)314 self.assertAlmostEqual(tree[1].distance, 7.300, places=3)315 self.assertEqual(tree[2].left, 3)316 self.assertEqual(tree[2].right, -2)317 self.assertAlmostEqual(tree[2].distance, 21.348, places=3)318 # Pairwise single-linkage clustering319 tree = treecluster(data=data1, mask=mask1, weight=weight1,320 transpose=0, method='s', dist='e')321 self.assertEqual(len(tree), len(data1) - 1)322 self.assertEqual(tree[0].left, 1)323 self.assertEqual(tree[0].right, 2)324 self.assertAlmostEqual(tree[0].distance, 2.600, places=3)325 self.assertEqual(tree[1].left, 0)326 self.assertEqual(tree[1].right, -1)327 self.assertAlmostEqual(tree[1].distance, 5.800, places=3)328 self.assertEqual(tree[2].left, -2)329 self.assertEqual(tree[2].right, 3)330 self.assertAlmostEqual(tree[2].distance, 12.908, places=3)331 # Pairwise centroid-linkage clustering332 tree = treecluster(data=data1, mask=mask1, weight=weight1,333 transpose=0, method='c', dist='e')334 self.assertEqual(len(tree), len(data1) - 1)335 self.assertEqual(tree[0].left, 1)336 self.assertEqual(tree[0].right, 2)337 self.assertAlmostEqual(tree[0].distance, 2.600, places=3)338 self.assertEqual(tree[1].left, 0)339 self.assertEqual(tree[1].right, -1)340 self.assertAlmostEqual(tree[1].distance, 6.650, places=3)341 self.assertEqual(tree[2].left, -2)342 self.assertEqual(tree[2].right, 3)343 self.assertAlmostEqual(tree[2].distance, 19.437, places=3)344 # Pairwise maximum-linkage clustering345 tree = treecluster(data=data1, mask=mask1, weight=weight1,346 transpose=0, method='m', dist='e')347 self.assertEqual(len(tree), len(data1) - 1)348 self.assertEqual(tree[0].left, 2)349 self.assertEqual(tree[0].right, 1)350 self.assertAlmostEqual(tree[0].distance, 2.600, places=3)351 self.assertEqual(tree[1].left, -1)352 self.assertEqual(tree[1].right, 0)353 self.assertAlmostEqual(tree[1].distance, 8.800, places=3)354 self.assertEqual(tree[2].left, 3)355 self.assertEqual(tree[2].right, -2)356 self.assertAlmostEqual(tree[2].distance, 32.508, places=3)357 # Second data set358 weight2 = [1, 1]359 data2 = numpy.array([[0.8223, 0.9295],360 [1.4365, 1.3223],361 [1.1623, 1.5364],362 [2.1826, 1.1934],363 [1.7763, 1.9352],364 [1.7215, 1.9912],365 [2.1812, 5.9935],366 [5.3290, 5.9452],367 [3.1491, 3.3454],368 [5.1923, 5.3156],369 [4.7735, 5.4012],370 [5.1297, 5.5645],371 [5.3934, 5.1823]])372 mask2 = numpy.array([[1, 1],373 [1, 1],374 [1, 1],375 [1, 1],376 [1, 1],377 [1, 1],378 [1, 1],379 [1, 1],380 [1, 1],381 [1, 1],382 [1, 1],383 [1, 1],384 [1, 1]], int)385 # Test second data set386 # Pairwise average-linkage clustering387 tree = treecluster(data=data2, mask=mask2, weight=weight2,388 transpose=0, method='a', dist='e')389 self.assertEqual(len(tree), len(data2) - 1)390 self.assertEqual(tree[0].left, 5)391 self.assertEqual(tree[0].right, 4)392 self.assertAlmostEqual(tree[0].distance, 0.003, places=3)393 self.assertEqual(tree[1].left, 9)394 self.assertEqual(tree[1].right, 12)395 self.assertAlmostEqual(tree[1].distance, 0.029, places=3)396 self.assertEqual(tree[2].left, 2)397 self.assertEqual(tree[2].right, 1)398 self.assertAlmostEqual(tree[2].distance, 0.061, places=3)399 self.assertEqual(tree[3].left, 11)400 self.assertEqual(tree[3].right, -2)401 self.assertAlmostEqual(tree[3].distance, 0.070, places=3)402 self.assertEqual(tree[4].left, -4)403 self.assertEqual(tree[4].right, 10)404 self.assertAlmostEqual(tree[4].distance, 0.128, places=3)405 self.assertEqual(tree[5].left, 7)406 self.assertEqual(tree[5].right, -5)407 self.assertAlmostEqual(tree[5].distance, 0.224, places=3)408 self.assertEqual(tree[6].left, -3)409 self.assertEqual(tree[6].right, 0)410 self.assertAlmostEqual(tree[6].distance, 0.254, places=3)411 self.assertEqual(tree[7].left, -1)412 self.assertEqual(tree[7].right, 3)413 self.assertAlmostEqual(tree[7].distance, 0.391, places=3)414 self.assertEqual(tree[8].left, -8)415 self.assertEqual(tree[8].right, -7)416 self.assertAlmostEqual(tree[8].distance, 0.532, places=3)417 self.assertEqual(tree[9].left, 8)418 self.assertEqual(tree[9].right, -9)419 self.assertAlmostEqual(tree[9].distance, 3.234, places=3)420 self.assertEqual(tree[10].left, -6)421 self.assertEqual(tree[10].right, 6)422 self.assertAlmostEqual(tree[10].distance, 4.636, places=3)423 self.assertEqual(tree[11].left, -11)424 self.assertEqual(tree[11].right, -10)425 self.assertAlmostEqual(tree[11].distance, 12.741, places=3)426 # Pairwise single-linkage clustering427 tree = treecluster(data=data2, mask=mask2, weight=weight2,428 transpose=0, method='s', dist='e')429 self.assertEqual(len(tree), len(data2) - 1)430 self.assertEqual(tree[0].left, 4)431 self.assertEqual(tree[0].right, 5)432 self.assertAlmostEqual(tree[0].distance, 0.003, places=3)433 self.assertEqual(tree[1].left, 9)434 self.assertEqual(tree[1].right, 12)435 self.assertAlmostEqual(tree[1].distance, 0.029, places=3)436 self.assertEqual(tree[2].left, 11)437 self.assertEqual(tree[2].right, -2)438 self.assertAlmostEqual(tree[2].distance, 0.033, places=3)439 self.assertEqual(tree[3].left, 1)440 self.assertEqual(tree[3].right, 2)441 self.assertAlmostEqual(tree[3].distance, 0.061, places=3)442 self.assertEqual(tree[4].left, 10)443 self.assertEqual(tree[4].right, -3)444 self.assertAlmostEqual(tree[4].distance, 0.077, places=3)445 self.assertEqual(tree[5].left, 7)446 self.assertEqual(tree[5].right, -5)447 self.assertAlmostEqual(tree[5].distance, 0.092, places=3)448 self.assertEqual(tree[6].left, 0)449 self.assertEqual(tree[6].right, -4)450 self.assertAlmostEqual(tree[6].distance, 0.242, places=3)451 self.assertEqual(tree[7].left, -7)452 self.assertEqual(tree[7].right, -1)453 self.assertAlmostEqual(tree[7].distance, 0.246, places=3)454 self.assertEqual(tree[8].left, 3)455 self.assertEqual(tree[8].right, -8)456 self.assertAlmostEqual(tree[8].distance, 0.287, places=3)457 self.assertEqual(tree[9].left, -9)458 self.assertEqual(tree[9].right, 8)459 self.assertAlmostEqual(tree[9].distance, 1.936, places=3)460 self.assertEqual(tree[10].left, -10)461 self.assertEqual(tree[10].right, -6)462 self.assertAlmostEqual(tree[10].distance, 3.432, places=3)463 self.assertEqual(tree[11].left, 6)464 self.assertEqual(tree[11].right, -11)465 self.assertAlmostEqual(tree[11].distance, 3.535, places=3)466 # Pairwise centroid-linkage clustering467 tree = treecluster(data=data2, mask=mask2, weight=weight2,468 transpose=0, method='c', dist='e')469 self.assertEqual(len(tree), len(data2) - 1)470 self.assertEqual(tree[0].left, 4)471 self.assertEqual(tree[0].right, 5)472 self.assertAlmostEqual(tree[0].distance, 0.003, places=3)473 self.assertEqual(tree[1].left, 12)474 self.assertEqual(tree[1].right, 9)475 self.assertAlmostEqual(tree[1].distance, 0.029, places=3)476 self.assertEqual(tree[2].left, 1)477 self.assertEqual(tree[2].right, 2)478 self.assertAlmostEqual(tree[2].distance, 0.061, places=3)479 self.assertEqual(tree[3].left, -2)480 self.assertEqual(tree[3].right, 11)481 self.assertAlmostEqual(tree[3].distance, 0.063, places=3)482 self.assertEqual(tree[4].left, 10)483 self.assertEqual(tree[4].right, -4)484 self.assertAlmostEqual(tree[4].distance, 0.109, places=3)485 self.assertEqual(tree[5].left, -5)486 self.assertEqual(tree[5].right, 7)487 self.assertAlmostEqual(tree[5].distance, 0.189, places=3)488 self.assertEqual(tree[6].left, 0)489 self.assertEqual(tree[6].right, -3)490 self.assertAlmostEqual(tree[6].distance, 0.239, places=3)491 self.assertEqual(tree[7].left, 3)492 self.assertEqual(tree[7].right, -1)493 self.assertAlmostEqual(tree[7].distance, 0.390, places=3)494 self.assertEqual(tree[8].left, -7)495 self.assertEqual(tree[8].right, -8)496 self.assertAlmostEqual(tree[8].distance, 0.382, places=3)497 self.assertEqual(tree[9].left, -9)498 self.assertEqual(tree[9].right, 8)499 self.assertAlmostEqual(tree[9].distance, 3.063, places=3)500 self.assertEqual(tree[10].left, 6)501 self.assertEqual(tree[10].right, -6)502 self.assertAlmostEqual(tree[10].distance, 4.578, places=3)503 self.assertEqual(tree[11].left, -10)504 self.assertEqual(tree[11].right, -11)505 self.assertAlmostEqual(tree[11].distance, 11.536, places=3)506 # Pairwise maximum-linkage clustering507 tree = treecluster(data=data2, mask=mask2, weight=weight2,508 transpose=0, method='m', dist='e')509 self.assertEqual(len(tree), len(data2) - 1)510 self.assertEqual(tree[0].left, 5)511 self.assertEqual(tree[0].right, 4)512 self.assertAlmostEqual(tree[0].distance, 0.003, places=3)513 self.assertEqual(tree[1].left, 9)514 self.assertEqual(tree[1].right, 12)515 self.assertAlmostEqual(tree[1].distance, 0.029, places=3)516 self.assertEqual(tree[2].left, 2)517 self.assertEqual(tree[2].right, 1)518 self.assertAlmostEqual(tree[2].distance, 0.061, places=3)519 self.assertEqual(tree[3].left, 11)520 self.assertEqual(tree[3].right, 10)521 self.assertAlmostEqual(tree[3].distance, 0.077, places=3)522 self.assertEqual(tree[4].left, -2)523 self.assertEqual(tree[4].right, -4)524 self.assertAlmostEqual(tree[4].distance, 0.216, places=3)525 self.assertEqual(tree[5].left, -3)526 self.assertEqual(tree[5].right, 0)527 self.assertAlmostEqual(tree[5].distance, 0.266, places=3)528 self.assertEqual(tree[6].left, -5)529 self.assertEqual(tree[6].right, 7)530 self.assertAlmostEqual(tree[6].distance, 0.302, places=3)531 self.assertEqual(tree[7].left, -1)532 self.assertEqual(tree[7].right, 3)533 self.assertAlmostEqual(tree[7].distance, 0.425, places=3)534 self.assertEqual(tree[8].left, -8)535 self.assertEqual(tree[8].right, -6)536 self.assertAlmostEqual(tree[8].distance, 0.968, places=3)537 self.assertEqual(tree[9].left, 8)538 self.assertEqual(tree[9].right, 6)539 self.assertAlmostEqual(tree[9].distance, 3.975, places=3)540 self.assertEqual(tree[10].left, -10)541 self.assertEqual(tree[10].right, -7)542 self.assertAlmostEqual(tree[10].distance, 5.755, places=3)543 self.assertEqual(tree[11].left, -11)544 self.assertEqual(tree[11].right, -9)545 self.assertAlmostEqual(tree[11].distance, 22.734, places=3)546 def test_somcluster(self):547 if TestCluster.module == 'Bio.Cluster':548 from Bio.Cluster import somcluster549 elif TestCluster.module == 'Pycluster':550 from Pycluster import somcluster551 # First data set552 weight = [1, 1, 1, 1, 1]553 data = numpy.array([[1.1, 2.2, 3.3, 4.4, 5.5],554 [3.1, 3.2, 1.3, 2.4, 1.5],555 [4.1, 2.2, 0.3, 5.4, 0.5],556 [12.1, 2.0, 0.0, 5.0, 0.0]])557 mask = numpy.array([[1, 1, 1, 1, 1],558 [1, 1, 1, 1, 1],559 [1, 1, 1, 1, 1],560 [1, 1, 1, 1, 1]], int)561 # TODO - Use a context manager here once we drop Python 2.6562 # Distance should be one letter:563 self.assertRaises(ValueError, somcluster,564 **{"data": data, "mask": mask, "weight": weight,565 "transpose": 0, "nxgrid": 10, "nygrid": 10,566 "inittau": 0.02, "niter": 100, "dist": "euclidean"})567 clusterid, celldata = somcluster(data=data, mask=mask, weight=weight,568 transpose=0, nxgrid=10, nygrid=10,569 inittau=0.02, niter=100, dist='e')570 self.assertEqual(len(clusterid), len(data))571 self.assertEqual(len(clusterid[0]), 2)572 # Second data set573 weight = [1, 1]574 data = numpy.array([[1.1, 1.2],575 [1.4, 1.3],576 [1.1, 1.5],577 [2.0, 1.5],578 [1.7, 1.9],579 [1.7, 1.9],580 [5.7, 5.9],581 [5.7, 5.9],582 [3.1, 3.3],583 [5.4, 5.3],584 [5.1, 5.5],585 [5.0, 5.5],586 [5.1, 5.2]])587 mask = numpy.array([[1, 1],588 [1, 1],589 [1, 1],590 [1, 1],591 [1, 1],592 [1, 1],593 [1, 1],594 [1, 1],595 [1, 1],596 [1, 1],597 [1, 1],598 [1, 1],599 [1, 1]], int)600 clusterid, celldata = somcluster(data=data, mask=mask, weight=weight,601 transpose=0, nxgrid=10, nygrid=10,602 inittau=0.02, niter=100, dist='e')603 self.assertEqual(len(clusterid), len(data))604 self.assertEqual(len(clusterid[0]), 2)605 def test_distancematrix_kmedoids(self):606 if TestCluster.module == 'Bio.Cluster':607 from Bio.Cluster import distancematrix, kmedoids608 elif TestCluster.module == 'Pycluster':609 from Pycluster import distancematrix, kmedoids610 data = numpy.array([[2.2, 3.3, 4.4],611 [2.1, 1.4, 5.6],612 [7.8, 9.0, 1.2],613 [4.5, 2.3, 1.5],614 [4.2, 2.4, 1.9],615 [3.6, 3.1, 9.3],616 [2.3, 1.2, 3.9],617 [4.2, 9.6, 9.3],618 [1.7, 8.9, 1.1]])619 mask = numpy.array([[1, 1, 1],620 [1, 1, 1],621 [0, 1, 1],622 [1, 1, 1],623 [1, 1, 1],624 [0, 1, 0],625 [1, 1, 1],626 [1, 0, 1],627 [1, 1, 1]], int)628 weight = numpy.array([2.0, 1.0, 0.5])629 matrix = distancematrix(data, mask=mask, weight=weight)630 self.assertAlmostEqual(matrix[1][0], 1.243, places=3)631 self.assertAlmostEqual(matrix[2][0], 25.073, places=3)632 self.assertAlmostEqual(matrix[2][1], 44.960, places=3)633 self.assertAlmostEqual(matrix[3][0], 4.510, places=3)634 self.assertAlmostEqual(matrix[3][1], 5.924, places=3)635 self.assertAlmostEqual(matrix[3][2], 29.957, places=3)636 self.assertAlmostEqual(matrix[4][0], 3.410, places=3)637 self.assertAlmostEqual(matrix[4][1], 4.761, places=3)638 self.assertAlmostEqual(matrix[4][2], 29.203, places=3)639 self.assertAlmostEqual(matrix[4][3], 0.077, places=3)640 self.assertAlmostEqual(matrix[5][0], 0.040, places=3)641 self.assertAlmostEqual(matrix[5][1], 2.890, places=3)642 self.assertAlmostEqual(matrix[5][2], 34.810, places=3)643 self.assertAlmostEqual(matrix[5][3], 0.640, places=3)644 self.assertAlmostEqual(matrix[5][4], 0.490, places=3)645 self.assertAlmostEqual(matrix[6][0], 1.301, places=3)646 self.assertAlmostEqual(matrix[6][1], 0.447, places=3)647 self.assertAlmostEqual(matrix[6][2], 42.990, places=3)648 self.assertAlmostEqual(matrix[6][3], 3.934, places=3)649 self.assertAlmostEqual(matrix[6][4], 3.046, places=3)650 self.assertAlmostEqual(matrix[6][5], 3.610, places=3)651 self.assertAlmostEqual(matrix[7][0], 8.002, places=3)652 self.assertAlmostEqual(matrix[7][1], 6.266, places=3)653 self.assertAlmostEqual(matrix[7][2], 65.610, places=3)654 self.assertAlmostEqual(matrix[7][3], 12.240, places=3)655 self.assertAlmostEqual(matrix[7][4], 10.952, places=3)656 self.assertAlmostEqual(matrix[7][5], 0.000, places=3)657 self.assertAlmostEqual(matrix[7][6], 8.720, places=3)658 self.assertAlmostEqual(matrix[8][0], 10.659, places=3)659 self.assertAlmostEqual(matrix[8][1], 19.056, places=3)660 self.assertAlmostEqual(matrix[8][2], 0.010, places=3)661 self.assertAlmostEqual(matrix[8][3], 16.949, places=3)662 self.assertAlmostEqual(matrix[8][4], 15.734, places=3)663 self.assertAlmostEqual(matrix[8][5], 33.640, places=3)664 self.assertAlmostEqual(matrix[8][6], 18.266, places=3)665 self.assertAlmostEqual(matrix[8][7], 18.448, places=3)666 clusterid, error, nfound = kmedoids(matrix, npass=1000)667 self.assertEqual(clusterid[0], 5)668 self.assertEqual(clusterid[1], 5)669 self.assertEqual(clusterid[2], 2)670 self.assertEqual(clusterid[3], 5)671 self.assertEqual(clusterid[4], 5)672 self.assertEqual(clusterid[5], 5)673 self.assertEqual(clusterid[6], 5)674 self.assertEqual(clusterid[7], 5)675 self.assertEqual(clusterid[8], 2)676 self.assertAlmostEqual(error, 7.680, places=3)677 def test_pca(self):678 if TestCluster.module == 'Bio.Cluster':679 from Bio.Cluster import pca680 elif TestCluster.module == 'Pycluster':681 from Pycluster import pca682 data = numpy.array([[3.1, 1.2],683 [1.4, 1.3],684 [1.1, 1.5],685 [2.0, 1.5],686 [1.7, 1.9],687 [1.7, 1.9],688 [5.7, 5.9],689 [5.7, 5.9],690 [3.1, 3.3],691 [5.4, 5.3],692 [5.1, 5.5],693 [5.0, 5.5],694 [5.1, 5.2],695 ])696 mean, coordinates, pc, eigenvalues = pca(data)697 self.assertAlmostEqual(mean[0], 3.5461538461538464)698 self.assertAlmostEqual(mean[1], 3.5307692307692311)699 self.assertAlmostEqual(coordinates[0, 0], 2.0323189722653883)700 self.assertAlmostEqual(coordinates[0, 1], 1.2252420399694917)701 self.assertAlmostEqual(coordinates[1, 0], 3.0936985166252251)702 self.assertAlmostEqual(coordinates[1, 1], -0.10647619705157851)703 self.assertAlmostEqual(coordinates[2, 0], 3.1453186907749426)704 self.assertAlmostEqual(coordinates[2, 1], -0.46331699855941139)705 self.assertAlmostEqual(coordinates[3, 0], 2.5440202962223761)706 self.assertAlmostEqual(coordinates[3, 1], 0.20633980959571077)707 self.assertAlmostEqual(coordinates[4, 0], 2.4468278463376221)708 self.assertAlmostEqual(coordinates[4, 1], -0.28412285736824866)709 self.assertAlmostEqual(coordinates[5, 0], 2.4468278463376221)710 self.assertAlmostEqual(coordinates[5, 1], -0.28412285736824866)711 self.assertAlmostEqual(coordinates[6, 0], -3.2018619434743254)712 self.assertAlmostEqual(coordinates[6, 1], 0.019692314198662915)713 self.assertAlmostEqual(coordinates[7, 0], -3.2018619434743254)714 self.assertAlmostEqual(coordinates[7, 1], 0.019692314198662915)715 self.assertAlmostEqual(coordinates[8, 0], 0.46978641990344067)716 self.assertAlmostEqual(coordinates[8, 1], -0.17778754731982949)717 self.assertAlmostEqual(coordinates[9, 0], -2.5549912731867215)718 self.assertAlmostEqual(coordinates[9, 1], 0.19733897451533403)719 self.assertAlmostEqual(coordinates[10, 0], -2.5033710990370044)720 self.assertAlmostEqual(coordinates[10, 1], -0.15950182699250004)721 self.assertAlmostEqual(coordinates[11, 0], -2.4365601663089413)722 self.assertAlmostEqual(coordinates[11, 1], -0.23390813900973562)723 self.assertAlmostEqual(coordinates[12, 0], -2.2801521629852974)724 self.assertAlmostEqual(coordinates[12, 1], 0.0409309711916888)725 self.assertAlmostEqual(pc[0, 0], -0.66810932728062988)726 self.assertAlmostEqual(pc[0, 1], -0.74406312017235743)727 self.assertAlmostEqual(pc[1, 0], 0.74406312017235743)728 self.assertAlmostEqual(pc[1, 1], -0.66810932728062988)729 self.assertAlmostEqual(eigenvalues[0], 9.3110471246032844)730 self.assertAlmostEqual(eigenvalues[1], 1.4437456297481428)731 data = numpy.array([[2.3, 4.5, 1.2, 6.7, 5.3, 7.1],732 [1.3, 6.5, 2.2, 5.7, 6.2, 9.1],733 [3.2, 7.2, 3.2, 7.4, 7.3, 8.9],734 [4.2, 5.2, 9.2, 4.4, 6.3, 7.2]])735 mean, coordinates, pc, eigenvalues = pca(data)736 self.assertAlmostEqual(mean[0], 2.7500)737 self.assertAlmostEqual(mean[1], 5.8500)738 self.assertAlmostEqual(mean[2], 3.9500)739 self.assertAlmostEqual(mean[3], 6.0500)740 self.assertAlmostEqual(mean[4], 6.2750)741 self.assertAlmostEqual(mean[5], 8.0750)742 self.assertAlmostEqual(coordinates[0, 0], 2.6460846688406905)743 self.assertAlmostEqual(coordinates[0, 1], -2.1421701432732418)744 self.assertAlmostEqual(coordinates[0, 2], -0.56620932754145858)745 self.assertAlmostEqual(coordinates[0, 3], 0.0)746 self.assertAlmostEqual(coordinates[1, 0], 2.0644120899917544)747 self.assertAlmostEqual(coordinates[1, 1], 0.55542108669180323)748 self.assertAlmostEqual(coordinates[1, 2], 1.4818772348457117)749 self.assertAlmostEqual(coordinates[1, 3], 0.0)750 self.assertAlmostEqual(coordinates[2, 0], 1.0686641862092987)751 self.assertAlmostEqual(coordinates[2, 1], 1.9994412069101073)752 self.assertAlmostEqual(coordinates[2, 2], -1.000720598980291)753 self.assertAlmostEqual(coordinates[2, 3], 0.0)754 self.assertAlmostEqual(coordinates[3, 0], -5.77916094504174)755 self.assertAlmostEqual(coordinates[3, 1], -0.41269215032867046)756 self.assertAlmostEqual(coordinates[3, 2], 0.085052691676038017)757 self.assertAlmostEqual(coordinates[3, 3], 0.0)758 self.assertAlmostEqual(pc[0, 0], -0.26379660005997291)759 self.assertAlmostEqual(pc[0, 1], 0.064814972617134495)760 self.assertAlmostEqual(pc[0, 2], -0.91763310094893846)761 self.assertAlmostEqual(pc[0, 3], 0.26145408875373249)762 self.assertAlmostEqual(pc[1, 0], 0.05073770520434398)763 self.assertAlmostEqual(pc[1, 1], 0.68616983388698793)764 self.assertAlmostEqual(pc[1, 2], 0.13819106187213354)765 self.assertAlmostEqual(pc[1, 3], 0.19782544121828985)766 self.assertAlmostEqual(pc[2, 0], -0.63000893660095947)767 self.assertAlmostEqual(pc[2, 1], 0.091155993862151397)768 self.assertAlmostEqual(pc[2, 2], 0.045630391256086845)769 self.assertAlmostEqual(pc[2, 3], -0.67456694780914772)770 # As the last eigenvalue is zero, the corresponding eigenvector is771 # strongly affected by roundoff error, and is not being tested here.772 # For PCA, this doesn't matter since all data have a zero coefficient773 # along this eigenvector.774 self.assertAlmostEqual(eigenvalues[0], 6.7678878332578778)775 self.assertAlmostEqual(eigenvalues[1], 3.0108911400291856)776 self.assertAlmostEqual(eigenvalues[2], 1.8775592718563467)777 self.assertAlmostEqual(eigenvalues[3], 0.0)778if __name__ == "__main__":779 TestCluster.module = 'Bio.Cluster'780 runner = unittest.TextTestRunner(verbosity=2)...

Full Screen

Full Screen

test_complex.py

Source:test_complex.py Github

copy

Full Screen

...8)9from random import random10# These tests ensure that complex math does the right thing11class ComplexTest(unittest.TestCase):12 def assertAlmostEqual(self, a, b):13 if isinstance(a, complex):14 if isinstance(b, complex):15 unittest.TestCase.assertAlmostEqual(self, a.real, b.real)16 unittest.TestCase.assertAlmostEqual(self, a.imag, b.imag)17 else:18 unittest.TestCase.assertAlmostEqual(self, a.real, b)19 unittest.TestCase.assertAlmostEqual(self, a.imag, 0.)20 else:21 if isinstance(b, complex):22 unittest.TestCase.assertAlmostEqual(self, a, b.real)23 unittest.TestCase.assertAlmostEqual(self, 0., b.imag)24 else:25 unittest.TestCase.assertAlmostEqual(self, a, b)26 def assertCloseAbs(self, x, y, eps=1e-9):27 """Return true iff floats x and y "are close\""""28 # put the one with larger magnitude second29 if abs(x) > abs(y):30 x, y = y, x31 if y == 0:32 return abs(x) < eps33 if x == 0:34 return abs(y) < eps35 # check that relative difference < eps36 self.assert_(abs((x-y)/y) < eps)37 def assertClose(self, x, y, eps=1e-9):38 """Return true iff complexes x and y "are close\""""39 self.assertCloseAbs(x.real, y.real, eps)40 self.assertCloseAbs(x.imag, y.imag, eps)41 def assertIs(self, a, b):42 self.assert_(a is b)43 def check_div(self, x, y):44 """Compute complex z=x*y, and check that z/x==y and z/y==x."""45 z = x * y46 if x != 0:47 q = z / x48 self.assertClose(q, y)49 q = z.__div__(x)50 self.assertClose(q, y)51 q = z.__truediv__(x)52 self.assertClose(q, y)53 if y != 0:54 q = z / y55 self.assertClose(q, x)56 q = z.__div__(y)57 self.assertClose(q, x)58 q = z.__truediv__(y)59 self.assertClose(q, x)60 def test_div(self):61 simple_real = [float(i) for i in xrange(-5, 6)]62 simple_complex = [complex(x, y) for x in simple_real for y in simple_real]63 for x in simple_complex:64 for y in simple_complex:65 self.check_div(x, y)66 # A naive complex division algorithm (such as in 2.0) is very prone to67 # nonsense errors for these (overflows and underflows).68 self.check_div(complex(1e200, 1e200), 1+0j)69 self.check_div(complex(1e-200, 1e-200), 1+0j)70 # Just for fun.71 for i in xrange(100):72 self.check_div(complex(random(), random()),73 complex(random(), random()))74 self.assertRaises(ZeroDivisionError, complex.__div__, 1+1j, 0+0j)75 # FIXME: The following currently crashes on Alpha76 # self.assertRaises(OverflowError, pow, 1e200+1j, 1e200+1j)77 def test_truediv(self):78 self.assertAlmostEqual(complex.__truediv__(2+0j, 1+1j), 1-1j)79 self.assertRaises(ZeroDivisionError, complex.__truediv__, 1+1j, 0+0j)80 def test_floordiv(self):81 self.assertAlmostEqual(complex.__floordiv__(3+0j, 1.5+0j), 2)82 self.assertRaises(ZeroDivisionError, complex.__floordiv__, 3+0j, 0+0j)83 def test_coerce(self):84 self.assertRaises(OverflowError, complex.__coerce__, 1+1j, 1L<<10000)85 def test_richcompare(self):86 self.assertRaises(OverflowError, complex.__eq__, 1+1j, 1L<<10000)87 self.assertEqual(complex.__lt__(1+1j, None), NotImplemented)88 self.assertIs(complex.__eq__(1+1j, 1+1j), True)89 self.assertIs(complex.__eq__(1+1j, 2+2j), False)90 self.assertIs(complex.__ne__(1+1j, 1+1j), False)91 self.assertIs(complex.__ne__(1+1j, 2+2j), True)92 self.assertRaises(TypeError, complex.__lt__, 1+1j, 2+2j)93 self.assertRaises(TypeError, complex.__le__, 1+1j, 2+2j)94 self.assertRaises(TypeError, complex.__gt__, 1+1j, 2+2j)95 self.assertRaises(TypeError, complex.__ge__, 1+1j, 2+2j)96 def test_mod(self):97 self.assertRaises(ZeroDivisionError, (1+1j).__mod__, 0+0j)98 a = 3.33+4.43j99 try:100 a % 0101 except ZeroDivisionError:102 pass103 else:104 self.fail("modulo parama can't be 0")105 def test_divmod(self):106 self.assertRaises(ZeroDivisionError, divmod, 1+1j, 0+0j)107 def test_pow(self):108 self.assertAlmostEqual(pow(1+1j, 0+0j), 1.0)109 self.assertAlmostEqual(pow(0+0j, 2+0j), 0.0)110 self.assertRaises(ZeroDivisionError, pow, 0+0j, 1j)111 self.assertAlmostEqual(pow(1j, -1), 1/1j)112 self.assertAlmostEqual(pow(1j, 200), 1)113 self.assertRaises(ValueError, pow, 1+1j, 1+1j, 1+1j)114 a = 3.33+4.43j115 self.assertEqual(a ** 0j, 1)116 self.assertEqual(a ** 0.+0.j, 1)117 self.assertEqual(3j ** 0j, 1)118 self.assertEqual(3j ** 0, 1)119 try:120 0j ** a121 except ZeroDivisionError:122 pass123 else:124 self.fail("should fail 0.0 to negative or complex power")125 try:126 0j ** (3-2j)127 except ZeroDivisionError:128 pass129 else:130 self.fail("should fail 0.0 to negative or complex power")131 # The following is used to exercise certain code paths132 self.assertEqual(a ** 105, a ** 105)133 self.assertEqual(a ** -105, a ** -105)134 self.assertEqual(a ** -30, a ** -30)135 self.assertEqual(0.0j ** 0, 1)136 b = 5.1+2.3j137 self.assertRaises(ValueError, pow, a, b, 0)138 def test_boolcontext(self):139 for i in xrange(100):140 self.assert_(complex(random() + 1e-6, random() + 1e-6))141 self.assert_(not complex(0.0, 0.0))142 def test_conjugate(self):143 self.assertClose(complex(5.3, 9.8).conjugate(), 5.3-9.8j)144 def test_constructor(self):145 class OS:146 def __init__(self, value): self.value = value147 def __complex__(self): return self.value148 class NS(object):149 def __init__(self, value): self.value = value150 def __complex__(self): return self.value151 self.assertEqual(complex(OS(1+10j)), 1+10j)152 self.assertEqual(complex(NS(1+10j)), 1+10j)153 self.assertRaises(TypeError, complex, OS(None))154 self.assertRaises(TypeError, complex, NS(None))155 self.assertAlmostEqual(complex("1+10j"), 1+10j)156 self.assertAlmostEqual(complex(10), 10+0j)157 self.assertAlmostEqual(complex(10.0), 10+0j)158 self.assertAlmostEqual(complex(10L), 10+0j)159 self.assertAlmostEqual(complex(10+0j), 10+0j)160 self.assertAlmostEqual(complex(1,10), 1+10j)161 self.assertAlmostEqual(complex(1,10L), 1+10j)162 self.assertAlmostEqual(complex(1,10.0), 1+10j)163 self.assertAlmostEqual(complex(1L,10), 1+10j)164 self.assertAlmostEqual(complex(1L,10L), 1+10j)165 self.assertAlmostEqual(complex(1L,10.0), 1+10j)166 self.assertAlmostEqual(complex(1.0,10), 1+10j)167 self.assertAlmostEqual(complex(1.0,10L), 1+10j)168 self.assertAlmostEqual(complex(1.0,10.0), 1+10j)169 self.assertAlmostEqual(complex(3.14+0j), 3.14+0j)170 self.assertAlmostEqual(complex(3.14), 3.14+0j)171 self.assertAlmostEqual(complex(314), 314.0+0j)172 self.assertAlmostEqual(complex(314L), 314.0+0j)173 self.assertAlmostEqual(complex(3.14+0j, 0j), 3.14+0j)174 self.assertAlmostEqual(complex(3.14, 0.0), 3.14+0j)175 self.assertAlmostEqual(complex(314, 0), 314.0+0j)176 self.assertAlmostEqual(complex(314L, 0L), 314.0+0j)177 self.assertAlmostEqual(complex(0j, 3.14j), -3.14+0j)178 self.assertAlmostEqual(complex(0.0, 3.14j), -3.14+0j)179 self.assertAlmostEqual(complex(0j, 3.14), 3.14j)180 self.assertAlmostEqual(complex(0.0, 3.14), 3.14j)181 self.assertAlmostEqual(complex("1"), 1+0j)182 self.assertAlmostEqual(complex("1j"), 1j)183 self.assertAlmostEqual(complex(), 0)184 self.assertAlmostEqual(complex("-1"), -1)185 self.assertAlmostEqual(complex("+1"), +1)186 class complex2(complex): pass187 self.assertAlmostEqual(complex(complex2(1+1j)), 1+1j)188 self.assertAlmostEqual(complex(real=17, imag=23), 17+23j)189 self.assertAlmostEqual(complex(real=17+23j), 17+23j)190 self.assertAlmostEqual(complex(real=17+23j, imag=23), 17+46j)191 self.assertAlmostEqual(complex(real=1+2j, imag=3+4j), -3+5j)192 c = 3.14 + 1j193 self.assert_(complex(c) is c)194 del c195 self.assertRaises(TypeError, complex, "1", "1")196 self.assertRaises(TypeError, complex, 1, "1")197 self.assertEqual(complex(" 3.14+J "), 3.14+1j)198 if test_support.have_unicode:199 self.assertEqual(complex(unicode(" 3.14+J ")), 3.14+1j)200 # SF bug 543840: complex(string) accepts strings with \0201 # Fixed in 2.3.202 self.assertRaises(ValueError, complex, '1+1j\0j')203 self.assertRaises(TypeError, int, 5+3j)204 self.assertRaises(TypeError, long, 5+3j)205 self.assertRaises(TypeError, float, 5+3j)206 self.assertRaises(ValueError, complex, "")207 self.assertRaises(TypeError, complex, None)208 self.assertRaises(ValueError, complex, "\0")209 self.assertRaises(TypeError, complex, "1", "2")210 self.assertRaises(TypeError, complex, "1", 42)211 self.assertRaises(TypeError, complex, 1, "2")212 self.assertRaises(ValueError, complex, "1+")213 self.assertRaises(ValueError, complex, "1+1j+1j")214 self.assertRaises(ValueError, complex, "--")215 if test_support.have_unicode:216 self.assertRaises(ValueError, complex, unicode("1"*500))217 self.assertRaises(ValueError, complex, unicode("x"))218 class EvilExc(Exception):219 pass220 class evilcomplex:221 def __complex__(self):222 raise EvilExc223 self.assertRaises(EvilExc, complex, evilcomplex())224 class float2:225 def __init__(self, value):226 self.value = value227 def __float__(self):228 return self.value229 self.assertAlmostEqual(complex(float2(42.)), 42)230 self.assertAlmostEqual(complex(real=float2(17.), imag=float2(23.)), 17+23j)231 self.assertRaises(TypeError, complex, float2(None))232 class complex0(complex):233 """Test usage of __complex__() when inheriting from 'complex'"""234 def __complex__(self):235 return 42j236 class complex1(complex):237 """Test usage of __complex__() with a __new__() method"""238 def __new__(self, value=0j):239 return complex.__new__(self, 2*value)240 def __complex__(self):241 return self242 class complex2(complex):243 """Make sure that __complex__() calls fail if anything other than a244 complex is returned"""245 def __complex__(self):246 return None247 self.assertAlmostEqual(complex(complex0(1j)), 42j)248 self.assertAlmostEqual(complex(complex1(1j)), 2j)249 self.assertRaises(TypeError, complex, complex2(1j))250 def test_hash(self):251 for x in xrange(-30, 30):252 self.assertEqual(hash(x), hash(complex(x, 0)))253 x /= 3.0 # now check against floating point254 self.assertEqual(hash(x), hash(complex(x, 0.)))255 def test_abs(self):256 nums = [complex(x/3., y/7.) for x in xrange(-9,9) for y in xrange(-9,9)]257 for num in nums:258 self.assertAlmostEqual((num.real**2 + num.imag**2) ** 0.5, abs(num))259 def test_repr(self):260 self.assertEqual(repr(1+6j), '(1+6j)')261 self.assertEqual(repr(1-6j), '(1-6j)')262 self.assertNotEqual(repr(-(1+0j)), '(-1+-0j)')263 def test_neg(self):264 self.assertEqual(-(1+6j), -1-6j)265 def test_file(self):266 a = 3.33+4.43j267 b = 5.1+2.3j268 fo = None269 try:270 fo = open(test_support.TESTFN, "wb")271 print >>fo, a, b272 fo.close()...

Full Screen

Full Screen

adsorbate_thermo_test.py

Source:adsorbate_thermo_test.py Github

copy

Full Screen

...21 """22 Check values of Q_trans, S_trans, Cp_trans, dH_trans23 """24 Q_trans, S_trans, Cp_trans, dH_trans = self.adsorbate_thermo.get_translation_thermo()25 self.assertAlmostEqual(Q_trans[0], 121.638576209145)26 self.assertAlmostEqual(Q_trans[45], 301.9035599354932)27 self.assertAlmostEqual(Q_trans[-2], 811.8757895562588)28 self.assertAlmostEqual(S_trans[0], 56.54717436538365)29 self.assertAlmostEqual(S_trans[45], 64.10547399343213)30 self.assertAlmostEqual(S_trans[-2], 72.33048004244537)31 self.assertAlmostEqual(Cp_trans[0], 8.314472)32 self.assertAlmostEqual(Cp_trans[45], 8.314472)33 self.assertAlmostEqual(Cp_trans[-2], 8.314472)34 self.assertAlmostEqual(dH_trans[0], 2478.9598268)35 self.assertAlmostEqual(dH_trans[45], 6152.70928)36 self.assertAlmostEqual(dH_trans[-2], 16545.79928)37 def test_get_vibration_thermo(self):38 """39 Check values of Q_vib, S_vib, dH_vib, Cv_vib40 """41 Q_vib, S_vib, dH_vib, Cv_vib = self.adsorbate_thermo.get_vibrational_thermo()42 self.assertAlmostEqual(Q_vib[0], 11.034963254025913)43 self.assertAlmostEqual(Q_vib[45], 130.32233489549847)44 self.assertAlmostEqual(Q_vib[-2], 6018.728683338621)45 self.assertAlmostEqual(S_vib[0], 38.07225504419116)46 self.assertAlmostEqual(S_vib[45], 67.49815326008806)47 self.assertAlmostEqual(S_vib[-2], 110.22104390942083)48 self.assertAlmostEqual(dH_vib[0], 5399.089968555581)49 self.assertAlmostEqual(dH_vib[45], 19984.872272678833)50 self.assertAlmostEqual(dH_vib[-2], 75347.8861223393)51 self.assertAlmostEqual(Cv_vib[0], 28.074698286767678)52 self.assertAlmostEqual(Cv_vib[45], 36.77745028228141)53 self.assertAlmostEqual(Cv_vib[-2], 49.93125232130692)54 def test_get_thermo(self):55 """56 Check the NASA polynomials57 """58 nasa = self.adsorbate_thermo.get_thermo()59 a_low = [1253.2, 18.899, -0.0390199, 3.98049e-05, -1.52948e-08, -24879.5, 20.1649]60 a_high = [4092.47, 1.56959, 0.000643606, -5.4255e-07, 9.64746e-11, -397031, -12477.9]61 np.testing.assert_array_almost_equal(nasa.polynomials[0].coeffs, a_low, 1)62 np.testing.assert_array_almost_equal(nasa.polynomials[1].coeffs, a_high, 1)63if __name__ == '__main__':...

Full Screen

Full Screen

varasto_test.py

Source:varasto_test.py Github

copy

Full Screen

...7 self.ylijaama = Varasto(10, 30)8 9 def test_konstruktori_luo_tyhjan_varaston(self):10 # https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertAlmostEqual11 self.assertAlmostEqual(self.varasto.saldo, 0)12 def test_uudella_varastolla_oikea_tilavuus(self):13 self.assertAlmostEqual(self.varasto.tilavuus, 10)14 def test_lisays_lisaa_saldoa(self):15 self.varasto.lisaa_varastoon(8)16 self.assertAlmostEqual(self.varasto.saldo, 8)17 18 def test_lisayksessa_vain_positiviisia_arvoja(self):19 self.varasto.lisaa_varastoon(-10)20 self.assertAlmostEqual(self.varasto.saldo, 0)21 22 def test_lisayksessa_ei_ylijaama(self):23 self.varasto.lisaa_varastoon(500)24 self.assertAlmostEqual(self.varasto.saldo, 10)25 def test_lisays_lisaa_pienentaa_vapaata_tilaa(self):26 self.varasto.lisaa_varastoon(8)27 # vapaata tilaa pitäisi vielä olla tilavuus-lisättävä määrä eli 228 self.assertAlmostEqual(self.varasto.paljonko_mahtuu(), 2)29 def test_ottaminen_palauttaa_oikean_maaran(self):30 self.varasto.lisaa_varastoon(8)31 saatu_maara = self.varasto.ota_varastosta(2)32 self.assertAlmostEqual(saatu_maara, 2)33 def test_ottaminen_lisaa_tilaa(self):34 self.varasto.lisaa_varastoon(8)35 self.varasto.ota_varastosta(2)36 # varastossa pitäisi olla tilaa 10 - 8 + 2 eli 437 self.assertAlmostEqual(self.varasto.paljonko_mahtuu(), 4)38 39 def test_ottaminen_vain_positiivisilla_arvoilla(self):40 self.varasto.lisaa_varastoon(5)41 saatu_maara = self.varasto.ota_varastosta(-5)42 self.assertAlmostEqual(saatu_maara, 0)43 44 def test_varaston_tyhjentaminen_palauttaa_oikean_maaran_saldon(self):45 self.varasto.lisaa_varastoon(8)46 saatu_maara = self.varasto.ota_varastosta(10)47 self.assertAlmostEqual(saatu_maara, 8)48 self.assertAlmostEqual(self.varasto.saldo, 0)49 50 def test_konstruktori_hyvaksyy_vain_positiivisen_tilavuuden(self):51 self.assertAlmostEqual(self.neg_arvot.tilavuus, 0)52 53 def test_konstruktori_hyvaksyy_vain_positiivisen_alku_saldon(self):54 self.assertAlmostEqual(self.neg_arvot.saldo, 0)55 56 def test_konstruktori_laskee_ylijaamatilanteen_oikein(self):57 self.assertAlmostEqual(self.ylijaama.saldo, 10)58 def test_tulostus_oikein(self):...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful