1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
|
"""Unit tests for table.
"""
import json
import os
import pathlib
import pickle
from collections import defaultdict
from tempfile import TemporaryDirectory
from unittest import TestCase, skipIf
import numpy
import pytest
from numpy import arange
from numpy.testing import assert_equal
from cogent3 import load_table, make_table, open_
from cogent3.format.table import (
formatted_array,
get_continuation_tables_headers,
is_html_markup,
)
from cogent3.parse.table import FilteringParser
from cogent3.util.misc import get_object_provenance
from cogent3.util.table import (
Table,
cast_str_to_array,
cast_str_to_numeric,
cast_to_array,
)
try:
from pandas import DataFrame
except ImportError:
DataFrame = None
TEST_ROOT = pathlib.Path(__file__).parent.parent
class TrapOutput:
def __call__(self, data, *args, **kwargs):
self.data, _, _ = data._get_repr_()
self.output = repr(data)
class TableTests(TestCase):
"""Tests of individual functions in table"""
# test table 1
t1_header = ["chrom", "stableid", "length"]
t1_rows = [
["X", "ENSG00000005893", 1353],
["A", "ENSG00000019485", 1827],
["A", "ENSG00000019102", 999],
["X", "ENSG00000012174", 1599],
["X", "ENSG00000010671", 1977],
["A", "ENSG00000019186", 1554],
["A", "ENSG00000019144", 4185],
["X", "ENSG00000008056", 2307],
["A", "ENSG00000018408", 1383],
["A", "ENSG00000019169", 1698],
]
# test table 2
t2_header = ["id", "foo", "bar"]
t2_rows = [
[1, "abc", 11],
[2, "bca", 22],
[3, "cab", 33],
[4, "abc", 44],
[5, "bca", 55],
]
# test table 3
t3_header = ["id", "foo", "bar"]
t3_rows = [[6, "abc", 66], [7, "bca", 77]]
# test table 4
t4_header = ["id", "foo", "bar"]
t4_rows = [[8, "abc", 88], [9, "bca", 99]]
# test table 5
t5_header = ["a", "b", "c", "d"]
t5_rows = [[1, 1, 1, 1], [2, 0, 1, 1], [1, 3, 2, 2]]
# test table 6
t6_header = ["id", "foo", "bar"]
t6_rows = [["60", " | ", "666"], ["70", "bca", "777"]]
# test table 7
t7_header = ["chrom", "gene", "stat"]
t7_rows = [
["X", "ENSG00000005893", 0.1353],
["A", "ENSG00000019485", 1827.558],
["A", "ENSG00000019102", 999.125],
]
t8_header = ["edge.name", "edge.parent", "length", "x", "y", "z"]
t8_rows = [
["Human", "edge.0", 4.0, 1.0, 3.0, 6.0],
["NineBande", "root", 4.0, 1.0, 3.0, 6.0],
]
def test_empty(self):
"""create table with no data"""
for data in [None, [], {}, ()]:
t = Table(header=["col 1", "col 2"], data=data)
self.assertEqual(len(t), 0)
self.assertEqual(t.shape, (0, 2), f"failed with {data}")
def test_input_containers(self):
"""should not fail on defaultdict"""
raw = dict(a=[1, 2, 3], b=[3, 4, 6])
data = defaultdict(list)
data.update(raw)
t = Table(data=data)
self.assertEqual(t.shape, (3, 2))
def test_keys_are_str(self):
"""all column headers converted to str"""
t = Table(header=["col 1", 2], data=[[0, 1]])
self.assertEqual(t.header, ("col 1", "2"))
def test_no_index_name(self):
"""assigning None has no effect"""
t = Table(header=self.t5_header, data=self.t5_rows)
t.index_name = None
self.assertIs(t.index_name, None)
def test_index_name(self):
"""correctly assigns"""
t = Table(header=self.t3_header, data=self.t3_rows, index_name="foo")
self.assertEqual(t.index_name, "foo")
# fails if not an existing column
with self.assertRaises(ValueError):
t.index_name = "missing"
data = t.columns.to_dict()
# correctly handled when provided on construction
with self.assertRaises(ValueError):
t = Table(data=data, index_name="missing")
t = Table(data=data, index_name="foo")
self.assertEqual(t.index_name, "foo")
# correctly reset when assigned None
t.index_name = None
self.assertEqual(t.index_name, None)
self.assertEqual(t.columns.index_name, None)
self.assertEqual(t._template, None)
# ... prior to providing columns
t = Table(index_name="foo")
for c, v in data.items():
t.columns[c] = v
self.assertEqual(t.index_name, "foo")
t = Table(index_name="missing")
for c, v in data.items():
t.columns[c] = v
with self.assertRaises(ValueError):
t.index_name
def test_table_data_int_keys(self):
"""correctly construct table from dict with int's as keys"""
head = ["", 0, 1]
data = {0: [2, 2], 1: [2, 2], "": [0, 1]}
t = Table(head, data=data)
assert_equal(t.array.tolist(), [[0, 2, 2], [1, 2, 2]])
def test_table_with_empty_string_index(self):
"""handle an index of empty string"""
d = {
"": ["Chimpanzee", "Galago", "Gorilla"],
"Chimpanzee": [0.0, 0.19, 0.005],
"Galago": [0.19, 0.0, 0.19],
}
table = make_table(data=d, index_name="")
val = table["Galago", "Chimpanzee"]
self.assertEqual(val, 0.19)
def test_column_repr_str(self):
"""repr and str of Columns"""
t = Table(header=list("abcdefg"), data=[[0, 1.1, 2, 3, 4, 5.5, 6.0]])
r = repr(t.columns)
s = str(t.columns)
self.assertIsInstance(r, str)
self.assertEqual(r, s)
def test_slicing_columns(self):
"""works using names, ints, bool array"""
t = Table(header=self.t5_header, data=self.t5_rows)
n = t.columns["b":"d"]
assert_equal(n, [[1, 0, 3], [1, 1, 2]])
n = t.columns[1:3]
assert_equal(n, [[1, 0, 3], [1, 1, 2]])
indices = numpy.array([False, True, True, False])
n = t.columns[indices]
assert_equal(n, [[1, 0, 3], [1, 1, 2]])
def test_indexing_columns(self):
"""works using names or ints"""
t = Table(header=self.t5_header, data=self.t5_rows)
n = t.columns["b"]
assert_equal(n, [1, 0, 3])
n = t.columns[1]
assert_equal(n, [1, 0, 3])
def test_indexing_rows(self):
"""works using names or ints"""
t = Table(header=self.t7_header, data=self.t7_rows, index_name="gene")
got = t["ENSG00000019485", "chrom"]
self.assertEqual(got, "A")
def test_immutability_cells(self):
"""table cells are immutable"""
t = Table(header=self.t7_header, data=self.t7_rows, index_name="gene")
with self.assertRaises(TypeError):
t["ENSG00000019485", "chrom"] = "D"
# even via column instance
with self.assertRaises(ValueError):
t.columns["chrom"]["ENSG00000019485"] = "D"
def test_slicing_table(self):
"""works using column names, ints, bool array"""
t = Table(header=self.t5_header, data=self.t5_rows)
n = t[:, "b":"d"]
self.assertEqual(n.columns.order, ("b", "c"))
assert_equal(n.array, [[1, 1], [0, 1], [3, 2]])
# using numpy bool array
rows = numpy.array([True, False, True])
n = t[rows]
self.assertEqual(n.shape, (2, 4))
assert_equal(n.columns["a"], [1, 1])
rows = numpy.array([True, False, True])
columns = numpy.array([True, False, False, True])
n = t[rows, columns]
assert_equal(n.header, numpy.array(t.header)[columns])
self.assertEqual(n.shape, (2, 2))
# column formatting copied on slice
t = Table(header=self.t5_header, data=self.t5_rows)
t.format_column("c", "%.2e")
n = t[:, 1:]
self.assertEqual(n._column_templates, t._column_templates)
def test_slicing_using_numpy_indexing(self):
"""support numpy advanced indexing"""
t = Table(header=self.t5_header, data=self.t5_rows)
indices = t.columns["b"] != 0
got = t[indices]
expect = t.array[[0, 2], :]
assert_equal(got.array, expect)
got = t[indices, [True, False, True, True]]
expect = expect[:, [0, 2, 3]]
assert_equal(got.array, expect)
# using numpy arrays for rows and columns
got_np = t[indices, numpy.array([True, False, True, True])]
assert_equal(got_np.array, got.array)
def test_slicing_with_index(self):
"""different slice types work when index_name defined"""
# slicing by int works with index_name too
t = Table(header=self.t8_header, data=self.t8_rows, index_name="edge.name")
got = t[[1]]
self.assertEqual(got.columns["edge.name"], "NineBande")
self.assertEqual(got.shape, (1, t.shape[1]))
for v, dtype in [(1, None), (1, object), ("NineBande", "U")]:
got = t[numpy.array([v], dtype=dtype)]
self.assertEqual(got.columns["edge.name"], "NineBande")
self.assertEqual(got.shape, (1, t.shape[1]))
# works if, for some reason, the index_name column has floats
t = Table(header=self.t7_header, data=self.t7_rows, index_name="stat")
got = t[[1827.5580]]
self.assertEqual(got.shape, (1, t.shape[1]))
got = t[numpy.array([1827.5580])]
self.assertEqual(got.shape, (1, t.shape[1]))
def test_specifying_space(self):
"""controls spacing in simple format"""
space = " "
t4 = Table(header=self.t1_header, data=self.t1_rows)
orig = len(str(t4).splitlines()[1])
t4 = Table(header=self.t1_header, data=self.t1_rows, space=space)
got1 = len(str(t4).splitlines()[1])
self.assertTrue(got1 > orig)
# repr is same
got2 = len(repr(t4).splitlines()[1])
self.assertEqual(got1, got2)
def test_construct_from_dict2d(self):
"""construction from a 2D dict"""
data = {
"edge.parent": {
"NineBande": "root",
"edge.1": "root",
"DogFaced": "root",
"Human": "edge.0",
"edge.0": "edge.1",
"Mouse": "edge.1",
"HowlerMon": "edge.0",
},
"x": {
"NineBande": 1.0,
"edge.1": 1.0,
"DogFaced": 1.0,
"Human": 1.0,
"edge.0": 1.0,
"Mouse": 1.0,
"HowlerMon": 1.0,
},
}
t = Table(data=data)
self.assertEqual(t.shape, (len(data["x"]), len(data)))
def test_wrapping_tables_index_1row(self):
"""correctly wraps table to <= maximum width"""
index = "A/C"
h = ["A/C", "A/G", "A/T", "C/A"]
rows = [[0.0425, 0.1424, 0.0226, 0.0391]]
t = Table(header=h, data=rows, max_width=30, index_name=index)
wrapped = str(t)
# index_name column occurs twice for these conditions
for c in h:
expect = 2 if c == index else 1
self.assertEqual(wrapped.count(c), expect)
def test_wrapping_tables_index_multirow(self):
"""correctly wraps table to <= maximum width"""
# multi-row table
d2D = {
"edge.parent": {
"NineBande": "root",
"Human": "edge.0",
},
"x": {
"NineBande": 1.0,
"Human": 1.0,
},
"length": {
"NineBande": 4.0,
"Human": 4.0,
},
"y": {
"NineBande": 3.0,
"Human": 3.0,
},
"z": {
"NineBande": 6.0,
"Human": 6.0,
},
"edge.name": {
"Human": "Human",
"NineBande": "NineBande",
},
}
row_order = list(d2D["edge.name"])
t = Table(
["edge.name", "edge.parent", "length", "x", "y", "z"],
d2D,
row_order=row_order,
space=8,
max_width=50,
index_name="edge.name",
title="My title",
legend="legend: this is a nonsense example.",
)
wrapped = str(t)
for line in wrapped.splitlines():
len(line)
def test_wrapping_tables(self):
"""correctly wraps table to <= maximum width"""
h = ["A/C", "A/G", "A/T", "C/A"]
rows = [[0.0425, 0.1424, 0.0226, 0.0391]]
t = Table(header=h, data=rows, max_width=30)
wrapped = str(t)
# index_name column occurs twice for these conditions
for c in h:
self.assertEqual(wrapped.count(c), 1)
# multi-row table
data = {
"edge.parent": {
"NineBande": "root",
"edge.1": "root",
},
"x": {
"NineBande": 1.0,
"edge.1": 1.0,
},
"length": {
"NineBande": 4.0,
"edge.1": 4.0,
},
"y": {
"NineBande": 3.0,
"edge.1": 3.0,
},
"z": {
"NineBande": 6.0,
"edge.1": 6.0,
},
}
t = Table(data=data, max_width=30)
wrapped = str(t)
for c in data:
self.assertEqual(wrapped.count(c), 1)
def test_format_array(self):
"""correctly format array data"""
f = (2.53, 12.426, 9.9, 7.382e-08)
# with default format_spec
g, l, w = formatted_array(numpy.array(f), "LR", precision=2)
self.assertTrue(l.endswith("LR"))
for e in g:
v = e.split(".")
self.assertEqual(len(v[-1]), 2, v)
# handles bool
g, l, w = formatted_array(numpy.array([True, False, True]), "LR", precision=2)
self.assertEqual(g[0].strip(), "True")
# title is always right aligned
_, l, _ = formatted_array(numpy.array(f), "LR", format_spec=">.1f")
self.assertTrue(l.endswith("LR"))
_, l, _ = formatted_array(numpy.array(f), "LR", format_spec="<.1f")
self.assertTrue(l.startswith("LR"))
# using format_spec with right alignment character
g, l, w = formatted_array(numpy.array(f), " blah", format_spec=">.1f")
for e in g:
# padded with spaces
self.assertTrue(e.startswith(" "), e)
self.assertFalse(e.endswith(" "), e)
# using format_spec with left alignment character
g, l, w = formatted_array(numpy.array(f), " blah", format_spec="<.1f")
for e in g:
# padded with spaces
self.assertTrue(e.endswith(" "), e)
self.assertFalse(e.startswith(" "), e)
# using format_spec with center alignment character
g, l, w = formatted_array(numpy.array(f), " blah", format_spec="^.1f")
for e in g:
# padded with spaces
self.assertTrue(e.endswith(" "), e)
self.assertTrue(e.startswith(" "), e)
g, _, _ = formatted_array(numpy.array(f), "blah", format_spec=".4f")
for e in g:
v = e.split(".")
self.assertEqual(len(v[-1]), 4, v)
# cope with C-style format strings
g, _, _ = formatted_array(numpy.array(f), "blah", format_spec="%.4f")
for e in g:
v = e.split(".")
self.assertEqual(len(v[-1]), 4, v)
# handle a formatter function
def formatcol(value):
if isinstance(value, float):
val = f"{value:.2f}"
else:
val = str(value)
return val
o = [3, "abc", 3.456789]
g, _, _ = formatted_array(
numpy.array(o, dtype="O"), "blah", format_spec=formatcol
)
self.assertEqual(g[0], " 3", g[0])
self.assertEqual(g[1], " abc", g[1])
self.assertEqual(g[2], "3.46", g)
# don't pad
g, l, w = formatted_array(numpy.array(f), " blah", format_spec="<.1f")
g, l, w = formatted_array(
numpy.array(f), " blah", format_spec="<.1f", pad=False
)
self.assertEqual(l, "blah")
for v in g:
self.assertTrue(" " not in v)
# use the align argument, 'c'
g, l, w = formatted_array(
numpy.array(f), " blah ", precision=1, pad=True, align="c"
)
for v in g:
self.assertTrue(v.startswith(" ") and v.endswith(" "))
# use the align argument, 'l'
g, l, w = formatted_array(
numpy.array(f), " blah ", precision=1, pad=True, align="l"
)
for v in g:
self.assertTrue(not v.startswith(" ") and v.endswith(" "))
# use the align argument, 'r'
col_title = " blah "
g, l, w = formatted_array(
numpy.array(f), col_title, precision=1, pad=True, align="r"
)
for v in g:
self.assertTrue(v.startswith(" ") and not v.endswith(" "))
self.assertEqual(w, len(col_title))
# raises error if align invalid value
with self.assertRaises(ValueError):
formatted_array(
numpy.array(f), " blah ", precision=1, pad=True, align="blah"
)
def test_get_continuation_tables_headers(self):
"""correctly identify the columns for subtables"""
cols_widths = [("", 10), ("b", 5), ("c", 3), ("d", 14), ("e", 15)]
got = get_continuation_tables_headers(cols_widths)
# no subtables, returns list of lists
expect = [[c for c, _ in cols_widths]]
self.assertEqual(got, expect)
# fails if any column has a width < max_width
with self.assertRaises(ValueError):
get_continuation_tables_headers(cols_widths, max_width=5)
# or if the sum of the index_name width and column is > max_width
with self.assertRaises(ValueError):
get_continuation_tables_headers(cols_widths, index_name="", max_width=24)
got = get_continuation_tables_headers(cols_widths, max_width=25)
expect = [["", "b", "c"], ["d"], ["e"]]
self.assertEqual(got, expect)
# with an index_name column
got = get_continuation_tables_headers(cols_widths, index_name="", max_width=27)
expect = [["", "b", "c"], ["", "d"], ["", "e"]]
self.assertEqual(got, expect)
cols_widths = [("a", 10), ("b", 5), ("c", 3), ("d", 14), ("e", 15)]
got = get_continuation_tables_headers(cols_widths, index_name="a", max_width=27)
expect = [["a", "b", "c"], ["a", "d"], ["a", "e"]]
self.assertEqual(got, expect)
# space has an affect
got = get_continuation_tables_headers(cols_widths, max_width=25, space=4)
expect = [["a", "b"], ["c", "d"], ["e"]]
self.assertEqual(got, expect)
def test_cast_to_array(self):
"""correctly cast to numpy array"""
b = (True, False, True)
a = cast_to_array(b)
self.assertTrue("bool" in a.dtype.name)
self.assertEqual(a.tolist(), list(b))
s = (
"NP_003077_hs_mm_rn_dna",
"NP_004893_hs_mm_rn_dna",
"NP_005079_hs_mm_rn_dna",
"NP_005500_hs_mm_rn_dna",
"NP_055852_hs_mm_rn_dna",
)
a = cast_to_array(s)
self.assertTrue("str" in a.dtype.name)
self.assertEqual(a.tolist(), list(s))
f = (2.53, 12.426, 9.9, 7.382e-08)
a = cast_to_array(f)
self.assertTrue("float" in a.dtype.name, a.dtype.name)
self.assertEqual(a.tolist(), list(f))
d = [3, 4, 5]
a = cast_to_array(d)
self.assertTrue("int" in a.dtype.name, a.dtype.name)
self.assertEqual(a.tolist(), list(d))
o = [3, "abc", 3.4]
a = cast_to_array(o)
self.assertTrue("object" in a.dtype.name, a.dtype.name)
self.assertEqual(a.tolist(), list(o))
def test_make_table(self):
"""makes a table"""
data = {
"edge.parent": {
"NineBande": "root",
"edge.1": "root",
"DogFaced": "root",
"Human": "edge.0",
},
"x": {
"NineBande": 1.0,
"edge.1": 1.0,
"DogFaced": 1.0,
"Human": 1.0,
},
"length": {
"NineBande": 4.0,
"edge.1": 4.0,
"DogFaced": 4.0,
"Human": 4.0,
},
"y": {
"NineBande": 3.0,
"edge.1": 3.0,
"DogFaced": 3.0,
"Human": 3.0,
},
"z": {
"NineBande": 6.0,
"edge.1": 6.0,
"DogFaced": 6.0,
"Human": 6.0,
},
"edge.names": {
"NineBande": "NineBande",
"edge.1": "edge.1",
"DogFaced": "DogFaced",
"Human": "Human",
},
}
t = make_table(data=data)
self.assertEqual(t.shape, (4, 6))
# if index_name column not specified
with self.assertRaises(IndexError):
_ = t["Human", "edge.parent"]
# use an index_name
t = make_table(data=data, index_name="edge.names")
# index_name col is the first one, and the data can be indexed
self.assertEqual(t.columns.order[0], "edge.names")
self.assertEqual(t["Human", "edge.parent"], "edge.0")
# providing path raises TypeError
with self.assertRaises(TypeError):
make_table("some_path.tsv")
with self.assertRaises(TypeError):
make_table(header="some_path.tsv")
with self.assertRaises(TypeError):
make_table(data="some_path.tsv")
def test_modify_title_legend(self):
"""reflected in persistent attrs"""
rows = (
("NP_003077_hs_mm_rn_dna", "Con", 2.5386013224378985),
("NP_004893_hs_mm_rn_dna", "Con", 0.12135142635634111e06),
)
t = Table(["Gene", "Type", "LR"], rows)
t.title = "a new one"
self.assertEqual(t._get_persistent_attrs()["title"], "a new one")
t.legend = "a new 2"
self.assertEqual(t._get_persistent_attrs()["legend"], "a new 2")
def test_dunder_repr_eq_str(self):
"""dunder str and repr methods should produce same"""
rows = (
("NP_003077_hs_mm_rn_dna", "Con", 2.5386013224378985),
("NP_004893_hs_mm_rn_dna", "Con", 0.12135142635634111e06),
)
t = Table(["Gene", "Type", "LR"], rows)
t.format_column("LR", "%.4e")
s = str(t)
r = repr(t)
self.assertTrue(r.startswith(s))
@skipIf(DataFrame is None, "pandas not installed")
def test_make_table_from_dataframe(self):
"""makes a table from a pandas data frame"""
df = DataFrame(data=[[0, 1], [3, 7]], columns=["a", "b"])
t = make_table(data_frame=df)
assert_equal(t.columns["a"], [0, 3])
assert_equal(t.columns["b"], [1, 7])
with self.assertRaises(TypeError):
make_table(data_frame="abcde")
def test_appended(self):
"""test the table appended method"""
t2 = Table(header=self.t2_header, data=self.t2_rows)
t3 = Table(header=self.t3_header, data=self.t3_rows)
t4 = Table(header=self.t4_header, data=self.t4_rows)
append_0 = t2.appended("foo2", [], title="self")
self.assertEqual(append_0.shape[0], t2.shape[0])
# test the title feature
self.assertEqual(append_0.title, "self")
append_1 = t2.appended("foo2", [t3])
self.assertEqual(append_1.shape[0], t2.shape[0] + t3.shape[0])
# test the new_column feature
self.assertEqual(append_1.shape[1], 4)
self.assertEqual(append_1.header[0], "foo2")
append_2 = t2.appended("foo2", [t3, t4])
self.assertEqual(append_2.shape[0], t2.shape[0] + t3.shape[0] + t4.shape[0])
append_3 = t2.appended("", [t3, t4])
self.assertEqual(append_3.shape[0], t2.shape[0] + t3.shape[0] + t4.shape[0])
self.assertEqual(append_3.shape[1], t2.shape[1] + 1)
def test_appended_mixed_dtypes(self):
"""handles table columns with different dtypes"""
t1 = Table(header=["a", "b"], data=dict(a=[1], b=["s"]))
t2 = Table(header=["a", "b"], data=dict(a=[1.2], b=[4]))
appended = t1.appended(None, t2)
self.assertTrue("float" in appended.columns["a"].dtype.name)
self.assertTrue("object" in appended.columns["b"].dtype.name)
def test_count(self):
"""test the table count method"""
t1 = Table(header=self.t1_header, data=self.t1_rows)
self.assertEqual(t1.count('chrom == "X"'), 4)
self.assertEqual(t1.count('stableid.endswith("6")'), 2)
self.assertEqual(t1.count("length % 2 == 0"), 2)
self.assertEqual(t1.count('chrom == "Y"'), 0)
self.assertEqual(t1.count('length % 2 == 0 and chrom == "A"'), 2)
self.assertEqual(t1.count('length % 2 == 0 or chrom == "X"'), 6)
t2 = Table(header=self.t2_header, data=self.t2_rows)
self.assertEqual(t2.count('foo == "abc"'), 2)
self.assertEqual(t2.count('foo == "cab"'), 1)
self.assertEqual(t2.count("bar % 2 == 0"), 2)
self.assertEqual(t2.count("id == 0"), 0)
def test_count_empty(self):
"""empty table count method returns 0"""
t1 = Table(header=self.t1_header)
self.assertEqual(t1.count('chrom == "X"'), 0)
self.assertEqual(t1.count(lambda x: x == "X", columns="chrom"), 0)
def test_count_unique(self):
"""correctly computes unique values"""
data = {
"Project_Code": [
"Ovary-AdenoCA",
"Liver-HCC",
"Panc-AdenoCA",
"Panc-AdenoCA",
],
"Donor_ID": ["DO46416", "DO45049", "DO51493", "DO32860"],
"Variant_Classification": ["IGR", "Intron", "Intron", "Intron"],
}
table = make_table(data=data)
co = table.count_unique(["Project_Code", "Variant_Classification"])
self.assertEqual(co[("Panc-AdenoCA", "Intron")], 2)
self.assertEqual(co[("Liver-HCC", "IGR")], 0)
co = table.count_unique("Variant_Classification")
self.assertEqual(co["Intron"], 3)
self.assertEqual(co["IGR"], 1)
def test_distinct_values(self):
"""test the table distinct_values method"""
t1 = Table(header=self.t1_header, data=self.t1_rows)
self.assertEqual(len(t1.distinct_values("chrom")), 2)
self.assertEqual(len(t1.distinct_values("stableid")), 10)
self.assertEqual(len(t1.distinct_values("length")), 10)
t2 = Table(header=self.t2_header, data=self.t2_rows)
self.assertEqual(len(t2.distinct_values("id")), 5)
self.assertEqual(len(t2.distinct_values("foo")), 3)
self.assertEqual(len(t2.distinct_values("bar")), 5)
d = t2.distinct_values("foo")
self.assertEqual(d, {"cab", "bca", "abc"})
def test_filtered(self):
"""test the table filtered method"""
t1 = Table(header=self.t1_header, data=self.t1_rows)
self.assertEqual(t1.filtered('chrom == "X"').shape[0], 4)
self.assertEqual(t1.filtered('stableid.endswith("6")').shape[0], 2)
self.assertEqual(t1.filtered("length % 2 == 0").shape[0], 2)
self.assertEqual(t1.filtered('chrom == "Y"').shape[0], 0)
self.assertEqual(t1.filtered('length % 2 == 0 and chrom == "A"').shape[0], 2)
self.assertEqual(t1.filtered('length % 2 == 0 or chrom == "X"').shape[0], 6)
t2 = Table(header=self.t2_header, data=self.t2_rows)
self.assertEqual(t2.filtered('foo == "abc"').shape[0], 2)
self.assertEqual(t2.filtered('foo == "cab"').shape[0], 1)
self.assertEqual(t2.filtered("bar % 2 == 0").shape[0], 2)
self.assertEqual(t2.filtered("id == 0").shape[0], 0)
def test_filtered_empty(self):
"""test the table filtered method"""
t1 = Table(header=self.t1_header)
self.assertEqual(t1.shape[0], 0)
got = t1.filtered('chrom == "X"')
self.assertEqual(got.shape[0], 0)
got = t1.filtered(lambda x: x == "X", columns="chrom")
self.assertEqual(got.shape[0], 0)
def test_filtered_by_column(self):
"""test the table filtered_by_column method"""
t1 = Table(header=self.t1_header, data=self.t1_rows)
t2 = Table(header=self.t2_header, data=self.t2_rows)
def is_numeric(values):
try:
sum(values)
except TypeError:
return False
return True
self.assertEqual(t1.filtered_by_column(is_numeric).shape[1], 1)
self.assertEqual(t2.filtered_by_column(is_numeric).shape[1], 2)
def test_get_columns(self):
"""test the table get_columns method"""
t1 = Table(header=self.t1_header, data=self.t1_rows)
self.assertEqual(t1.get_columns("chrom").shape[0], t1.shape[0])
self.assertEqual(t1.get_columns("chrom").shape[1], 1)
self.assertEqual(t1.get_columns(["chrom", "length"]).shape[0], t1.shape[0])
self.assertEqual(t1.get_columns(["chrom", "length"]).shape[1], 2)
# if index_name, includes that in return
t1 = Table(header=self.t1_header, data=self.t1_rows, index_name="stableid")
r = t1.get_columns(["length"])
self.assertEqual(r.header, ("stableid", "length"))
# if index_name, unless excluded
r = t1.get_columns(["length"], with_index=False)
self.assertIs(r.index_name, None)
def test_joined(self):
"""test the table joined method"""
t2 = Table(header=self.t2_header, data=self.t2_rows)
t3 = Table(header=self.t3_header, data=self.t3_rows)
# inner join with defaults
got = t2.joined(t3)
self.assertEqual(got.shape[0], 0)
# inner join test
self.assertEqual(
t2.joined(t3, columns_self="foo", columns_other="foo").shape[0], 4
)
# merged 'foo' column, so (6-1) columns in join
self.assertEqual(
t2.joined(t3, columns_self="foo", columns_other="foo").shape[1], 5
)
# non-inner join test (cartesian product of rows)
got = t2.joined(t3, inner_join=False)
self.assertEqual(got.shape[0], t2.shape[0] * t3.shape[0])
self.assertEqual(
t2.joined(t3, inner_join=False).shape[1], t2.shape[1] + t3.shape[1]
)
got = t2.cross_join(t3)
self.assertEqual(got.shape[0], t2.shape[0] * t3.shape[0])
self.assertEqual(
t2.joined(t3, inner_join=False).shape[1], t2.shape[1] + t3.shape[1]
)
def test_joined_diff_indexing(self):
"""join handles different indexing"""
a = Table(
header=["index", "col2", "col3"],
data=[[1, 2, 3], [2, 3, 1], [2, 6, 5]],
title="A",
)
b = Table(
header=["index", "col2", "col3"],
data=[[1, 2, 3], [2, 2, 1], [3, 6, 3]],
title="B",
)
# index by int
j1 = a.joined(b, [0, 2])
# index by column names
j2 = a.joined(b, ["index", "col3"])
self.assertEqual(j1.header, j2.header)
self.assertEqual(str(j1), str(j2))
def test_normalized(self):
"""test the table normalized method"""
t5 = Table(header=self.t5_header, data=self.t5_rows)
self.assertEqual(
t5.normalized().to_list(t5.header),
[
[0.25, 0.25, 0.25, 0.25],
[0.5, 0.0, 0.25, 0.25],
[0.125, 0.375, 0.25, 0.25],
],
)
self.assertEqual(
t5.normalized(by_row=False).to_list(t5.header),
[[0.25, 0.25, 0.25, 0.25], [0.5, 0.0, 0.25, 0.25], [0.25, 0.75, 0.5, 0.5]],
)
def test_sorted(self):
"""test the table sorted method"""
t1 = Table(header=self.t1_header, data=self.t1_rows)
got = t1.sorted("length")
self.assertEqual(
got.to_list("length"),
[999, 1353, 1383, 1554, 1599, 1698, 1827, 1977, 2307, 4185],
)
t5 = Table(header=self.t5_header, data=self.t5_rows)
self.assertEqual(t5.sorted("b").to_list("b"), [0, 1, 3])
self.assertEqual(t5.sorted().to_list("a"), [1, 1, 2])
self.assertEqual(t5.sorted(reverse="a").to_list("a"), [2, 1, 1])
table = Table(
data={
"chrom": ("X", "A", "A", "X", "X", "A", "A", "X", "A", "A"),
"stableid": (
"ENSG00000005893",
"ENSG00000019485",
"ENSG00000019102",
"ENSG00000012174",
"ENSG00000010671",
"ENSG00000019186",
"ENSG00000019144",
"ENSG00000008056",
"ENSG00000018408",
"ENSG00000019169",
),
"length": [1353, 1827, 999, 1599, 1977, 1554, 4185, 2307, 1383, 1698],
}
)
table = table.sorted(columns=["chrom", "stableid"])
last_index = len(table) - 1
self.assertEqual(table[0, "stableid"], "ENSG00000018408")
self.assertEqual(table[last_index, "stableid"], "ENSG00000012174")
table = table.sorted(reverse="stableid")
self.assertEqual(table[0, "stableid"], "ENSG00000019485")
self.assertEqual(table[last_index, "stableid"], "ENSG00000005893")
table = table.sorted(reverse="chrom", columns="length")
self.assertEqual(table[0, "stableid"], "ENSG00000019102")
self.assertEqual(table[last_index, "stableid"], "ENSG00000019144")
# providing reversed argument name raises TypeError
with self.assertRaises(TypeError):
table.sorted(reversed="chrom")
def test_summed(self):
"""test the table summed method"""
t5 = Table(header=self.t5_header, data=self.t5_rows)
self.assertEqual(t5.summed(), [4, 4, 4, 4])
self.assertEqual(t5.summed(col_sum=False), [4, 4, 8])
t2 = Table(header=self.t2_header, data=self.t2_rows)
self.assertEqual(t2.summed(indices=2), 165)
mix = Table(header=["A", "B"], data=[[0, ""], [1, 2], [3, 4]])
self.assertEqual(mix.summed("B", strict=False), 6)
self.assertEqual(mix.summed(0, col_sum=False, strict=False), 0)
self.assertEqual(mix.summed(1, col_sum=False), 3)
self.assertEqual(mix.summed(strict=False), [4, 6])
self.assertEqual(mix.summed(col_sum=False, strict=False), [0, 3, 7])
with self.assertRaises(TypeError):
_ = mix.summed(strict=True)
def test_to_list(self):
"""test the table to_list method"""
t3 = Table(header=self.t3_header, data=self.t3_rows)
self.assertEqual(t3.to_list("id"), [6, 7])
self.assertEqual(t3.to_list("foo"), ["abc", "bca"])
def test_to_list_column_order(self):
"""column order of input reflected in result"""
t3 = Table(header=self.t3_header, data=self.t3_rows)
rev_order = ["id", "foo", "bar"]
rev_order.reverse()
result = t3.to_list(rev_order)
self.assertEqual(result[0], list(reversed(self.t3_rows[0][:])))
def test_to_dict(self):
"""cast to 2D dict"""
t = Table(header=self.t7_header, data=self.t7_rows, digits=1)
def test_transposed(self):
"""test the table transposed method"""
t1 = Table(header=self.t1_header, data=self.t1_rows)
# note the transpose has one less row
got = t1.transposed("", select_as_header="stableid")
self.assertEqual(got.shape[0], t1.shape[1] - 1)
# note the transpose has an extra column
self.assertEqual(got.shape[1], t1.shape[0] + 1)
# specifying a column without unique values not supported
with self.assertRaises(ValueError):
_ = t1.transposed("", select_as_header="chrom")
def test_transposed_numeric(self):
"""table transposed with numeric header converted to str's"""
t = Table(header=self.t2_header, data=self.t2_rows)
# note the transpose has one less row
got = t.transposed("", select_as_header="bar")
self.assertEqual(got.shape[0], t.shape[1] - 1)
# note the transpose has an extra column
self.assertEqual(got.shape[1], t.shape[0] + 1)
self.assertEqual(got.header, ("", "11", "22", "33", "44", "55"))
r = str(got) # this should not fail!
def test_transposed_forgets_index(self):
"""transposed table defaults to no row index_name"""
data = {
"": [0, 1, 2, 3, 4, 5, 6],
"T": [2, 10, 1, 6, 1, 5, 0],
"C": [0, 0, 0, 0, 0, 0, 1],
"A": [8, 0, 9, 4, 9, 4, 4],
"G": [0, 0, 0, 0, 0, 1, 5],
}
t = Table(header=["", "T", "C", "A", "G"], data=data, index_name="")
tr = t.transposed("Base", select_as_header="")
self.assertEqual(tr.index_name, None)
# but you can set a new one
tr = t.transposed("Base", select_as_header="", index_name="Base")
self.assertEqual(tr.index_name, "Base")
self.assertEqual(tr["G", "5"], 1)
def test_del_column(self):
"""correctly removes the column"""
t = Table(header=self.t5_header, data=self.t5_rows)
columns = list(t.columns)
expect = tuple(columns[1:])
del t.columns[columns[0]]
self.assertEqual(t.columns.order, expect)
def test_take_columns(self):
"""correctly takes columns"""
t = Table(header=self.t4_header, data=self.t4_rows)
columns = list(t.columns)
expect = tuple(columns[1:])
n = t.columns.take_columns(expect)
self.assertEqual(n.order, expect)
n = t.columns.take_columns(columns[0])
self.assertEqual(n.order, (columns[0],))
n = t.columns.take_columns(1)
self.assertEqual(n.order, (columns[1],))
def test_with_new_column(self):
"""test the table with_new_column method"""
t5 = Table(header=self.t5_header, data=self.t5_rows)
t5_row_sum = t5.with_new_column("sum", sum, t5.header)
self.assertEqual(t5_row_sum.get_columns("sum").to_list(), [4, 4, 8])
# now using a string expression
t8 = Table(header=self.t8_header, data=self.t8_rows, index_name="edge.name")
n = t8.with_new_column("YZ", callback="y+z")
assert_equal(n.columns["YZ"], [9.0, 9.0])
# if the new column alreayb exists, the new table has the newest column
n2 = t8.with_new_column("YZ", callback="y*z")
assert_equal(n2.columns["YZ"], [18.0, 18.0])
self.assertNotEqual(id(n), id(n2))
# bu the column arrays that have not changed should be equal
for c in n.columns:
if c == "YZ":
self.assertNotEqual(id(n.columns[c]), id(n2.columns[c]))
else:
self.assertEqual(id(n.columns[c]), id(n2.columns[c]))
def test_with_new_header(self):
"""test the table with_new_header method"""
t2 = Table(header=self.t2_header, data=self.t2_rows)
t2 = t2.with_new_header("id", "no")
self.assertEqual(t2.header[0], "no")
t2 = t2.with_new_header("foo", "moo")
self.assertEqual(t2.header[1], "moo")
t2 = t2.with_new_header("moo", "foo")
self.assertEqual(t2.header[1], "foo")
def test_formatted_mutable(self):
"""returns a mutable object"""
# required by formatting functions
t = Table(self.t1_header, self.t1_rows)
fmt = t._formatted()
fmt[0][0] = "24"
self.assertEqual(fmt[0][0], "24")
def test_formatted_precision(self):
"""applies numerical precision"""
# required by formatting functions
t = Table(self.t7_header, self.t7_rows, digits=1)
fmt = t._formatted()
# last column should have single place after decimal
for l in fmt[1:]:
decimal = l[-1].strip().split(".")[-1]
self.assertEqual(len(decimal), 1, l[-1])
def test_str_empty(self):
"""empty table returns empty str"""
table = make_table()
self.assertEqual(str(table), "")
def test_repr_empty(self):
"""empty table returns empty str"""
table = make_table()
got = repr(table)
self.assertEqual(got, "0 rows x 0 columns")
def test_str_zero_rows(self):
"""table with no rows returns column heads"""
table = make_table(header=["a"])
self.assertEqual(str(table), "=\na\n-\n-")
def test_str_object_col(self):
"""str works when a column has complex object"""
# data has tuples in an array
data = dict(
key=numpy.array([("a", "c"), ("b", "c"), ("a", "d")], dtype="O"),
count=[1, 3, 2],
)
t = Table(data=data)
got = str(t)
self.assertEqual(len(got.splitlines()), 7)
def test_str_md_format(self):
"""str() produces markdown table"""
md_table = make_table(
header=["a", "b"], data=[["val1", "val2"], ["has | symbol", "val4"]]
)
md = md_table.to_string(format="md")
self.assertTrue(r"has \| symbol" in md)
def test_str_tex_format(self):
"""str() produces latex tabular table"""
tex_table = make_table(
header=["a", "b"], data=[["val1", "val2"], ["val3", "val4"]]
)
tex = tex_table.to_string(format="tex", justify="cr")
self.assertEqual(
tex_table.to_string(format="tex", justify="cr"),
tex_table.to_latex(justify="cr"),
)
self.assertEqual(tex.splitlines()[2], r"\begin{tabular}{ c r }")
self.assertFalse("caption" in tex)
# with a title
tex_table = make_table(
header=["a", "b"],
data=[["val1", "val2"], ["val3", "val4"]],
title="a title",
)
tex = tex_table.to_string(format="tex")
tex = tex.splitlines()
self.assertEqual(tex[-2], r"\caption{a title}")
tex = tex_table.to_string(format="tex", label="tab:first")
tex = tex.splitlines()
self.assertEqual(tex[-3], r"\caption{a title}")
self.assertEqual(tex[-2], r"\label{tab:first}")
# with a legend, no title
tex_table = make_table(
header=["a", "b"],
data=[["val1", "val2"], ["val3", "val4"]],
legend="a legend",
)
tex = tex_table.to_string(format="tex")
tex = tex.splitlines()
# because it's treated as a title by default
self.assertEqual(tex[-2], r"\caption{a legend}")
# unless you say not to
tex = tex_table.to_string(format="tex", concat_title_legend=False)
tex = tex.splitlines()
self.assertEqual(tex[-2], r"\caption*{a legend}")
tex_table = make_table(
header=["a", "b"],
data=[["val1", "val2"], ["val3", "val4"]],
title="a title.",
legend="a legend",
)
tex = tex_table.to_string(format="tex")
tex = tex.splitlines()
self.assertEqual(tex[-2], r"\caption{a title. a legend}")
tex = tex_table.to_string(format="tex", concat_title_legend=False)
tex = tex.splitlines()
self.assertEqual(tex[2], r"\caption{a title.}")
self.assertEqual(tex[-2], r"\caption*{a legend}")
tex = tex_table.to_string(
format="tex", concat_title_legend=False, label="table"
)
tex = tex.splitlines()
self.assertEqual(tex[2], r"\caption{a title.}")
self.assertEqual(tex[3], r"\label{table}")
def test_to_html(self):
"""generates html table within c3table div"""
# with no index_name, or title, or legend
import re
t = Table(header=self.t8_header, data=self.t8_rows)
got = t.to_html()
# make sure tags are matched
for tag in ("div", "style", "table", "thead"):
self.assertEqual(len(re.findall(f"<[/]*{tag}.*>", got)), 2)
self.assertEqual(len(re.findall(f"<[/]*tr>", got)), 4)
# 2 columns should be left aligned, 4 right aligned
# adding 1 for the CSS style definition
self.assertEqual(got.count("c3col_left"), 4 + 1)
self.assertEqual(got.count("c3col_right"), 8 + 1)
self.assertEqual(got.count("cell_title"), 1) # CSS defn only
num_spans = got.count("span")
num_caption = got.count("caption")
t = Table(header=self.t8_header, data=self.t8_rows, title="a title")
got = t.to_html()
self.assertEqual(got.count("cell_title"), 2)
# number of spans increases by 2 to enclose the title
self.assertEqual(got.count("span"), num_spans + 2)
self.assertEqual(got.count("caption"), num_caption + 2)
# no <br> element
self.assertNotIn("<br>", got)
t = Table(header=self.t8_header, data=self.t8_rows, legend="a legend")
got = t.to_html()
self.assertEqual(got.count("cell_title"), 1)
# cell_legend not actually defined in CSS yet
self.assertEqual(got.count("cell_legend"), 1)
# number of spans increases by 2 to enclose the title
self.assertEqual(got.count("span"), num_spans + 2)
self.assertEqual(got.count("caption"), num_caption + 2)
# no <br> element
self.assertNotIn("<br>", got)
t = Table(
header=self.t8_header, data=self.t8_rows, title="a title", legend="a legend"
)
got = t.to_html()
self.assertEqual(got.count("cell_title"), 2)
# cell_legend not actually defined in CSS yet
self.assertEqual(got.count("cell_legend"), 1)
self.assertEqual(got.count("caption"), num_caption + 2)
# has <br> element
self.assertIn("<br>", got)
def test_invalid_format(self):
"""should raise value error"""
t = make_table(self.t2_header, data=self.t2_rows)
with self.assertRaises(ValueError):
t.format = "blah"
def test_phylip(self):
"""generates phylip format"""
rows = [
["a", "", 0.088337278874079342, 0.18848582712597683, 0.44084000179091454],
["c", 0.088337278874079342, "", 0.088337278874079342, 0.44083999937417828],
["b", 0.18848582712597683, 0.088337278874079342, "", 0.44084000179090932],
["e", 0.44084000179091454, 0.44083999937417828, 0.44084000179090932, ""],
]
header = ["seq1/2", "a", "c", "b", "e"]
dist = Table(header=header, data=rows, index_name="seq1/2")
r = dist.to_string(format="phylip")
r = r.splitlines()
self.assertEqual(r[0].strip(), "4")
for line in r[1:]:
line = line.split()
self.assertTrue(line[0] in dist.header)
self.assertTrue(line[-1][-1].isdigit())
line = r[1].split()
self.assertEqual(line[1], "0.0000", line)
def test_pickle_unpickle(self):
"""roundtrip via pickling"""
data = {
"edge.parent": {
"NineBande": "root",
"edge.1": "root",
},
"x": {
"NineBande": 1.0,
"edge.1": 1.0,
},
"length": {
"NineBande": 4.0,
"edge.1": 4.0,
},
"y": {
"NineBande": 3.0,
"edge.1": 3.0,
},
"z": {
"NineBande": 6.0,
"edge.1": 6.0,
},
"edge.name": {
"NineBande": "NineBande",
"edge.1": "edge.1",
},
}
t = Table(
data=data,
max_width=50,
index_name="edge.name",
title="My title",
legend="blah",
)
# via string
s = pickle.dumps(t)
r = pickle.loads(s)
self.assertEqual(str(t), str(r))
# via file
with TemporaryDirectory(".") as dirname:
path = pathlib.Path(dirname) / "table.pickle"
t.write(str(path))
r = load_table(path)
self.assertEqual(str(t), str(r))
def test_load_mixed(self):
"""load data with mixed data type columns"""
t = Table(
header=["abcd", "data", "float"],
data=[[str([1, 2, 3, 4, 5]), "0", 1.1], ["x", 5.0, 2.1], ["y", "", 3.1]],
)
with TemporaryDirectory(".") as dirname:
path = pathlib.Path(dirname) / "table.tsv"
t.write(str(path))
r = load_table(path)
self.assertEqual(str(t), str(r))
self.assertTrue("float", r.columns["float"].dtype.name)
def test_load_mixed_static(self):
"""load data, mixed data type columns remain as string"""
t = make_table(header=["A", "B"], data=[[1, 1], ["a", 2]])
with TemporaryDirectory(".") as dirname:
path = pathlib.Path(dirname) / "table.txt"
t.write(str(path), sep="\t")
# if static types, then mixed columns become strings
r = load_table(path, sep="\t", static_column_types=True)
self.assertTrue("str" in r.columns["A"].dtype.name)
def test_load_mixed_row_lengths(self):
"""skip_inconsistent skips rows that have different length to header"""
h = list("ABCDE")
r = [list("12345"), list("000"), list("12345")]
text = "\n".join(["\t".join(l) for l in [h] + r])
with TemporaryDirectory(".") as dirname:
path = pathlib.Path(dirname) / "table.tsv"
with open(path, "w") as out:
out.write(text)
r = load_table(path, skip_inconsistent=True)
self.assertEqual(r.shape, (2, 5))
self.assertEqual(r.header, tuple(h))
self.assertEqual(r.array.tolist(), [list(range(1, 6))] * 2)
# loading without skip_inconsistent raise ValueError
with self.assertRaises(ValueError):
r = load_table(path, skip_inconsistent=False)
def test_write_to_json(self):
"""tests writing to json file"""
t = load_table("data/sample.tsv")
with TemporaryDirectory(".") as dirname:
path = pathlib.Path(dirname) / "table.json"
t.write(path)
with open_(path) as fn:
got = json.loads(fn.read())
self.assertEqual(got["type"], get_object_provenance(Table))
data = got["data"]
self.assertEqual(tuple(data["order"]), t.header)
self.assertEqual(
t.shape,
(
len(tuple(data["columns"].items())[0][1]["values"]),
len(data["columns"]),
),
)
self.assertEqual(
t.array.T.tolist(),
[v["values"] for v in data["columns"].values()],
)
def test_write_compressed(self):
"""tests writing to compressed format"""
t = load_table("data/sample.tsv")
with open("data/sample.tsv") as infile:
expect = infile.read()
with TemporaryDirectory(".") as dirname:
path = pathlib.Path(dirname) / "table.txt"
# using the compressed option
t.write(path, sep="\t", compress=True)
with open_(f"{path}.gz") as infile:
got = infile.read()
self.assertEqual(got, expect)
# specifying via a suffix
t.write(f"{path}.gz", sep="\t")
with open_(f"{path}.gz") as infile:
got = infile.read()
self.assertEqual(got, expect)
def test_load_table_from_json(self):
"""tests loading a Table object from json file"""
with TemporaryDirectory(dir=".") as dirname:
json_path = os.path.join(dirname, "table.json")
t = load_table("data/sample.tsv")
t.write(json_path)
got = load_table(json_path)
self.assertEqual(got.shape, t.shape)
self.assertEqual(got.header, t.header)
assert_equal(got.array, t.array)
def test_load_table_invalid_type(self):
"""raises TypeError if filename invalid type"""
with self.assertRaises(TypeError):
load_table({"a": [0, 1]})
def test_make_table_white_space_in_column(self):
"""strips white space from column headers"""
# matching header and data keys
t = make_table(header=[" a"], data={" a": [0, 2]}, sep="\t")
self.assertEqual(t.columns["a"].tolist(), [0, 2])
self.assertIsInstance(t.to_string(), str)
# data key has a space
t = make_table(data={" a": [0, 2]}, sep="\t")
self.assertEqual(t.columns["a"].tolist(), [0, 2])
self.assertIsInstance(t.to_string(), str)
def test_load_table_filename_case(self):
"""load_table insensitive to file name case"""
with TemporaryDirectory(".") as dirname:
dirname = pathlib.Path(dirname)
with open(dirname / "temp.CSV", "w") as outfile:
outfile.write("a,b,c\n0,2,abc\n1,3,efg")
table = load_table(dirname / "temp.CSV")
data = table.columns.to_dict()
self.assertEqual(data, dict(a=[0, 1], b=[2, 3], c=["abc", "efg"]))
def test_load_table_limit(self):
"""limit argument to function works"""
t = load_table("data/sample.tsv", limit=2)
self.assertEqual(t.shape[0], 2)
def test_load_table_returns_static_columns(self):
"""for static data, load_table gives same dtypes for static_columns_type=True/False"""
t = load_table("data/sample.tsv", sep="\t", static_column_types=False)
is_false = {t.columns[c].dtype.name for c in t.columns}
t = load_table("data/sample.tsv", sep="\t", static_column_types=True)
is_true = {t.columns[c].dtype.name for c in t.columns}
self.assertEqual(is_true, is_false)
def test_formats(self):
"""exercising the different supported formats"""
last = ""
for format, startwith in (
("md", "|"),
("rst", "+"),
("latex", r"\begin"),
("markdown", "|"),
("csv", "id,"),
("tsv", "id\t"),
("simple", "="),
):
t3 = Table(
header=self.t3_header,
data=self.t3_rows,
format=format,
title="A title",
legend="A legend",
)
got = str(t3).splitlines()
query_line = 1 if format == "simple" else 0
got = got[query_line]
self.assertIsInstance(got, str)
self.assertNotEqual(got, last)
self.assertTrue(got.startswith(startwith), f"{format}: {got[:10]}")
last = got
def test_grid_table_format(self):
"""test the table grid_table_format method"""
from cogent3.format.table import grid_table_format
formatted_grid = grid_table_format(
self.t6_header, self.t6_rows, title="Test", legend="Units"
)
self.assertEqual(len(formatted_grid.split("\n")), len(self.t6_rows) * 2 + 7)
formatted_grid = grid_table_format(
self.t6_header,
self.t6_rows,
title="Really Long Title",
legend="Extra Long Legend",
)
self.assertEqual(len(formatted_grid.split("\n")), len(self.t6_rows) * 2 + 7 + 2)
def test_to_markdown(self):
"""Exercising the table markdown method"""
table = make_table(self.t6_header, self.t6_rows, format="md")
markdown_table = table.to_markdown(justify="crl")
markdown_list = markdown_table.split("\n")
self.assertEqual(markdown_list[2].count(r"|"), 5)
# the pipe symbol should have been escaped
self.assertEqual(markdown_list[2].count(r"\|"), 1)
with self.assertRaises(ValueError):
_ = table.to_markdown(justify="cr1")
def test_to_csv(self):
"""successfully create csv formatted string"""
table = Table(
header=self.t3_header,
data=self.t3_rows,
title="A title",
legend="A legend",
)
sv = table.to_csv()
expect = ["id,foo,bar", "6,abc,66", "7,bca,77"]
self.assertEqual(sv.splitlines(), expect)
sv = table.to_csv(with_title=True)
self.assertEqual(sv.splitlines(), ["A title"] + expect)
sv = table.to_csv(with_legend=True)
self.assertEqual(sv.splitlines(), expect + ["A legend"])
sv = table.to_csv(with_title=True, with_legend=True)
self.assertEqual(sv.splitlines(), ["A title"] + expect + ["A legend"])
def test_to_tsv(self):
"""successfully create csv formatted string"""
table = Table(
header=self.t3_header,
data=self.t3_rows,
title="A title",
legend="A legend",
)
sv = table.to_tsv()
expect = ["id\tfoo\tbar", "6\tabc\t66", "7\tbca\t77"]
self.assertEqual(sv.splitlines(), expect)
sv = table.to_tsv(with_title=True)
self.assertEqual(sv.splitlines(), ["A title"] + expect)
sv = table.to_tsv(with_legend=True)
self.assertEqual(sv.splitlines(), expect + ["A legend"])
sv = table.to_tsv(with_title=True, with_legend=True)
self.assertEqual(sv.splitlines(), ["A title"] + expect + ["A legend"])
def test_to_delim(self):
"""successfully create separated format with arbitrary character"""
table = Table(
header=self.t3_header,
data=self.t3_rows,
)
sv = table.to_string(sep=";")
expect = ["id;foo;bar", "6;abc;66", "7;bca;77"]
self.assertEqual(sv.splitlines(), expect)
def test_to_rst_grid(self):
"""generates a rst grid table"""
table = Table(header=["a", "b"], data=[[1, 2]], title="A title")
got = table.to_rst(csv_table=False).splitlines()
self.assertTrue(table.title in got[1])
self.assertEqual(set(got[0]), {"-", "+"})
self.assertEqual(set(got[4]), {"=", "+"})
def test_to_rst_csv(self):
"""generates a rst csv-table"""
table = Table(
header=["a", "b"],
data=[[1, 2]],
title="A title",
legend="A legend",
)
got = table.to_rst(csv_table=True)
self.assertEqual(
got.splitlines(),
[
".. csv-table:: A title A legend",
' :header: "a", "b"',
"",
" 1, 2",
],
)
# try without a title/legend
table = Table(header=["a", "b"], data=[[1, 2]])
got = table.to_rst(csv_table=True)
self.assertEqual(
got.splitlines(),
[
".. csv-table::",
' :header: "a", "b"',
"",
" 1, 2",
],
)
def test_get_repr_(self):
"""handles single column case"""
t = make_table(self.t2_header, data=self.t2_rows)
t = t[:, 0]
# the next line was previously failing
t._get_repr_()
table = Table(header=["a", "b"], data=[[1, 2]])
table, _, unset_columns = table._get_repr_()
self.assertEqual(table.shape, (1, 2))
self.assertIsNone(unset_columns)
table = make_table(header=["a", "b"])
table.columns["a"] = ["a"]
table, _, unset_columns = table._get_repr_()
self.assertEqual(table.shape, (1, 1))
self.assertIn("b", unset_columns)
def test_repr_html_(self):
"""should produce html"""
# no index_name
t = Table(header=self.t8_header, data=self.t8_rows)
_ = t._repr_html_()
# with an index_name
t = Table(header=self.t8_header, data=self.t8_rows, index_name="edge.name")
got = t._repr_html_()
# and the index_name column should contain "index_name" css class
self.assertEqual(
got.count("index"), t.shape[0] + 1
) # add 1 for CSS style sheet
# data has tuples in an array
data = dict(
key=numpy.array([("a", "c"), ("b", "c"), ("a", "d")], dtype="O"),
count=[1, 3, 2],
)
t = Table(data=data)
_ = t._repr_html_()
# some columns without data
table = make_table(header=["a", "b"])
table.columns["a"] = ["a"]
_ = t._repr_html_()
# single column with a single value should not fail
table = make_table(data={"kappa": [3.2]}, title="a title")
_ = table._repr_html_()
# set head and tail, introduces ellipsis row class
table = make_table(data={"A": list("abcdefghijk"), "B": list(range(11))})
table.set_repr_policy(head=8, tail=1)
got = table._repr_html_().splitlines()
num_rows = 0
for l in got:
if "<tr>" in l:
num_rows += 1
if "ellipsis" in l:
break
self.assertEqual(num_rows, 9)
def test_array(self):
"""should produce array"""
# data has tuples in an array
data = dict(
key=numpy.array([("a", "c"), ("b", "c"), ("a", "d")], dtype="O"),
count=[1, 3, 2],
)
expect = [list(v) for v in zip(data["key"][:], data["count"])]
t = Table(data=data)
arr = t.array
assert_equal(arr.tolist(), expect)
def test_separator_format(self):
"""testing separator_format with title and legend, and contents that match the separator"""
from cogent3.format.table import separator_format
with self.assertRaises(RuntimeError):
_ = separator_format(self.t6_header, self.t6_rows)
separated_table = separator_format(
self.t6_header, self.t6_rows, sep=" | ", title="Test", legend="Units"
)
self.assertEqual(len(separated_table.split("\n")), len(self.t6_rows) + 3)
def test_separator_format_writer(self):
"""exercising separator_format_writer"""
from cogent3.format.table import separator_formatter
t3 = Table(header=self.t3_header, data=self.t3_rows)
comma_sep = t3.to_string(sep=",").splitlines()
writer = separator_formatter(sep=" | ")
formatted = [
f for f in writer([l.split(",") for l in comma_sep], has_header=True)
]
expected_format = ["id | foo | bar", "6 | abc | 66", "7 | bca | 77"]
self.assertEqual(formatted, expected_format)
def test_set_repr_policy(self):
"""exercising setting repr policy"""
t = Table(header=self.t2_header, data=self.t2_rows)
t.set_repr_policy(random=2)
r = repr(t)
self.assertIsInstance(r, str)
r, _, _ = t._get_repr_()
self.assertEqual(r.shape[0], 2)
t.set_repr_policy(head=1)
r, _, _ = t._get_repr_()
self.assertEqual(r.shape[0], 1)
t.set_repr_policy(tail=3)
r, _, _ = t._get_repr_()
self.assertEqual(r.shape[0], 3)
t.set_repr_policy(show_shape=False)
r = repr(t)
self.assertFalse(f"\n{t.shape[0]:,} rows x {t.shape[1]:,} columns" in r)
r = t._repr_html_()
self.assertFalse(f"\n{t.shape[0]:,} rows x {t.shape[1]:,} columns" in r)
def test_head(self):
"""returns the head of the table!"""
from cogent3.util import table
display = table.display
head = TrapOutput()
table.display = head
t = Table(header=self.t1_header, data=self.t1_rows)
t.head(nrows=3)
self.assertEqual(head.data.shape[0], 3)
self.assertEqual(len(head.output.splitlines()), 9)
self.assertEqual(head.data.to_list(), self.t1_rows[:3])
# tests when number of rows < default
t = make_table(data=dict(a=["a"], b=["b"]))
t.head()
self.assertEqual(head.data.shape[0], 1)
self.assertEqual(len(head.output.splitlines()), 7)
self.assertEqual(head.data.to_list(), [["a", "b"]])
table.display = display
def test_tail(self):
"""returns the tail of the table!"""
from cogent3.util import table
display = table.display
tail = TrapOutput()
table.display = tail
t = Table(header=self.t1_header, data=self.t1_rows)
t.tail(nrows=3)
self.assertEqual(tail.data.shape[0], 3)
self.assertEqual(len(tail.output.splitlines()), 9)
self.assertEqual(
[int(v) for v in tail.data[:, -1].to_list()],
[r[-1] for r in self.t1_rows[-3:]],
)
# tests when number of rows < default
t = make_table(data=dict(a=["a"], b=["b"]))
t.tail()
self.assertEqual(tail.data.shape[0], 1)
self.assertEqual(len(tail.output.splitlines()), 7)
self.assertEqual(tail.data.to_list(), [["a", "b"]])
table.display = display
@skipIf(DataFrame is None, "pandas not installed")
def test_to_pandas(self):
"""produces a dataframe"""
t = Table(header=self.t1_header, data=self.t1_rows)
df = t.to_pandas()
self.assertIsInstance(df, DataFrame)
data = df.to_numpy()
self.assertEqual(data.tolist(), self.t1_rows)
def test_load_table(self):
"""exercising load table"""
path = os.path.dirname(os.path.dirname(__file__))
path = os.path.join(path, "data/sample.tsv")
table = load_table(path)
self.assertEqual(table.shape, (10, 3))
def test_cast_str_to_numerical(self):
"""correctly converts a series of strings to numeric values"""
d = arange(4, step=0.1)
r = cast_str_to_numeric(d)
assert_equal(d, r)
with numpy.testing.suppress_warnings() as sup:
# we know that converting to real loses imaginary
sup.filter(numpy.ComplexWarning)
for d_type in [numpy.int64, numpy.complex128, numpy.float64]:
d = d.astype(d_type)
r = cast_str_to_numeric(d)
self.assertIsInstance(r[0], type(d[0]))
d = d.astype(str)
r = cast_str_to_numeric(d)
self.assertIsInstance(r[0], numpy.float64)
d = numpy.array(d, dtype="U")
r = cast_str_to_numeric(d)
self.assertIsInstance(r[0], numpy.float64)
d = numpy.array(d, dtype="S")
r = cast_str_to_numeric(d)
self.assertIsInstance(r[0], numpy.float64)
def test_cast_str_to_array(self):
"""handle processing string series"""
d = [".123|.345", "123"]
r = cast_str_to_array(d, static_type=True)
self.assertTrue("str" in r.dtype.name)
r = cast_str_to_array(d, static_type=False)
self.assertEqual(r.dtype.name, "object")
d = [".123|.345", "123", "()"]
r = cast_str_to_array(d, static_type=False)
self.assertEqual(r[-1], ())
def test_filtering_parser(self):
"""filters rows"""
expect = []
for r in self.t1_rows:
row = [str(e) for e in r]
expect.append(row)
t = make_table(self.t1_header, data=self.t1_rows)
lines = t.to_csv().splitlines()
# no limit set
reader = FilteringParser(
row_condition=lambda x: x[0] == "A", with_header=True, sep=","
)
got = [line for line in reader(lines)]
self.assertEqual(got[0], self.t1_header)
self.assertEqual(got[1:], [r for r in expect if r[0] == "A"])
# limit set
reader = FilteringParser(
lambda x: x[0] == "A", with_header=True, sep=",", limit=2
)
got = [line for line in reader(lines)]
self.assertEqual(got[0], self.t1_header)
self.assertEqual(got[1:], [r for r in expect if r[0] == "A"][:2])
# negate
reader = FilteringParser(
lambda x: x[0] == "A", negate=True, with_header=True, sep=","
)
got = [line for line in reader(lines)]
self.assertEqual(got[0], self.t1_header)
self.assertEqual(got[1:], [r for r in expect if r[0] == "X"])
# parser works with load_table
path = TEST_ROOT / "data" / "sample.tsv"
reader = FilteringParser(lambda x: x[0] == "A", with_header=True, sep="\t")
table = load_table(path, reader=reader)
self.assertEqual(list(table.header), self.t1_header)
self.assertEqual(table.array.tolist(), [r for r in self.t1_rows if r[0] == "A"])
# parser works if called on path as Path
reader = FilteringParser(lambda x: x[0] == "A", with_header=True, sep="\t")
got = [r for r in reader(path)]
self.assertEqual(len(got), 7)
# parser works if called on path as str
got = [r for r in reader(str(path))]
self.assertEqual(len(got), 7)
# parser works with no conditions
reader = FilteringParser(with_header=True, sep="\t")
t = load_table(path, reader=reader)
self.assertEqual(t.shape, (10, 3))
def test_filtering_parser_filter_columns(self):
"""filters columns"""
path = TEST_ROOT / "data" / "sample.tsv"
# specified by int
reader = FilteringParser(columns=0, with_header=True, sep="\t")
got = load_table(path, reader=reader)
self.assertEqual(got.shape, (10, 1))
# specified by str
reader = FilteringParser(columns="length", with_header=True, sep="\t")
got = load_table(path, reader=reader)
self.assertEqual(got.shape, (10, 1))
# specified by index_name
reader = FilteringParser(columns=[0, 2], with_header=True, sep="\t")
got = load_table(path, reader=reader)
self.assertEqual(got.shape, (10, 2))
# specified by name
reader = FilteringParser(
columns=["chrom", "length"], with_header=True, sep="\t"
)
got2 = load_table(path, reader=reader)
self.assertEqual(got2.shape, (10, 2))
assert_equal(got.array, got2.array)
# raises value error if column name doesn't exist
reader = FilteringParser(columns=["blah", "length"], with_header=True, sep="\t")
with self.assertRaises(ValueError):
_ = load_table(path, reader=reader)
# raises IndexError if column index_name doesn't exist
reader = FilteringParser(columns=[0, 10], with_header=True, sep="\t")
with self.assertRaises(IndexError):
_ = load_table(path, reader=reader)
# raises ValueError if names given and with_header is False
with self.assertRaises(ValueError):
_ = FilteringParser(columns=["blah"], with_header=False)
def test_set_column_format(self):
"""fails if invalid format spec provided"""
data = {
"Gene": [
"NP_003077_hs_mm_rn_dna",
"NP_004893_hs_mm_rn_dna",
"NP_005079_hs_mm_rn_dna",
"NP_005500_hs_mm_rn_dna",
"NP_055852_hs_mm_rn_dna",
],
"Type": ["Con", "Con", "Con", "Con", "Con"],
"LR": [
2.5386013224378985,
121351.42635634111,
9516594.978886133,
7.382703020266491e-08,
10933217.708952725,
],
}
t = make_table(data=data)
with self.assertRaises(ValueError):
t.format_column("LR", ".4e")
def test_table_format(self):
"""table formating function doesn't fail with different input values"""
from cogent3.format.table import formatted_cells
data = [[230, "acdef", None], [6, "cc", 1.9876]]
head = ["one", "two", "three"]
_ = formatted_cells(data, header=head)
data = [[230, "acdef", 1.3], [6, "cc", numpy.array([1.9876, 2.34])]]
_ = formatted_cells(data, header=head)
def test_to_categorical(self):
"""correctly construct contingency table"""
data = {"Ts": [31, 58], "Tv": [36, 138], "": ["syn", "nsyn"]}
table = make_table(header=["", "Ts", "Tv"], data=data)
with self.assertRaises(ValueError):
# did not set an index_name
table.to_categorical(columns=["Ts", "Tv"])
got = table.to_categorical(columns=["Ts", "Tv"], index_name="")
assert_equal(got.observed, table[:, 1:].array)
got = table.to_categorical(["Ts"])
mean = got.observed.array.mean()
expected = numpy.array([[mean], [mean]])
assert_equal(got.expected, expected)
# works if index_name included
got = table.to_categorical(columns=["Ts", "Tv", ""])
assert_equal(got.observed, table[:, 1:].array)
# works if no columns specified
got = table.to_categorical()
assert_equal(got.observed, table[:, 1:].array)
data = {
"": numpy.array(["syn", "nsyn"], dtype=object),
"Ts": numpy.array([31, 58], dtype=object),
"Tv": numpy.array([36, 138], dtype=object),
}
table = make_table(header=["", "Ts", "Tv"], data=data, index_name="")
with self.assertRaises(TypeError):
table.to_categorical(columns=["Ts", "Tv"])
def test_is_html_markup(self):
"""format function confirms correctly specified html"""
self.assertTrue(is_html_markup("<table>blah</table>"))
self.assertTrue(is_html_markup("<table>blah<table>blah</table></table>"))
self.assertTrue(is_html_markup("<i>blah</i><sub>blah</sub>"))
self.assertTrue(is_html_markup("<i>blah</i>\n<sub>blah</sub>"))
self.assertFalse(is_html_markup("<table>blah</tabl>"))
self.assertFalse(is_html_markup("<table>"))
self.assertFalse(is_html_markup("blah < blah"))
self.assertFalse(is_html_markup("blah > blah"))
@pytest.mark.parametrize("data", ([[0, 1, 2], [0, 1]], [[0, 1, 2], [0, 1, 2, 3]]))
def test_mixed_row_lengths(data):
"""should fail"""
with pytest.raises(ValueError):
_ = Table(header=["a", "b", "c"], data=data)
@pytest.fixture
def t2():
header = ["id", "foo", "bar"]
rows = [
[1, "abc", 11],
[2, "bca", 22],
[3, "cab", 33],
[4, "abc", 44],
[5, "bca", 55],
]
return Table(header=header, data=rows, index_name="id")
@pytest.fixture
def t3():
header = ["id", "foo2", "bar2"]
rows = [[6, "abc", 66], [7, "bca", 77]]
return Table(header=header, data=rows, index_name="id")
@pytest.fixture
def t4():
header = ["id", "foo2", "bar2"]
rows = [[6, "abc", 66], [7, "bca", 77]]
return Table(header=header, data=rows, index_name="id")
def test_inner_join_col_naming(t2, t3):
"""test the table joined method"""
# inner join with defaults
got = t2.inner_join(t3)
expect = list(t2.header) + [f"right_{c}" for c in t3.header[1:]]
assert list(got.header) == expect
# inner join with no col_prefix
got = t2.inner_join(t3, col_prefix="")
expect = list(t2.header) + list(t3.header[1:])
assert list(got.header) == expect
def test_outer_join_col_naming(t2, t3):
"""test the table joined method"""
# cross join with a new col prefix
col_prefix = "cp_"
got = t2.cross_join(t3, col_prefix=col_prefix)
expect = list(t2.header) + [f"{col_prefix}{c}" for c in t3.header]
assert list(got.header) == expect
assert got.shape[0] == t2.shape[0] * t3.shape[0]
|