|
27 | 27 | import collections.abc
|
28 | 28 | import dataclasses
|
29 | 29 | import datetime
|
30 |
| -import itertools |
31 | 30 | import json
|
32 | 31 | import numbers
|
33 | 32 | import warnings
|
@@ -657,41 +656,10 @@ def __str__(self):
|
657 | 656 | return util.unicode_table(rows, header=headers, row_separator=False)
|
658 | 657 |
|
659 | 658 | def _repr_html_(self):
|
660 |
| - """ |
661 |
| - Called by jupyter notebooks to render tables |
662 |
| - """ |
663 | 659 | headers, rows = self._text_header_and_rows(
|
664 | 660 | limit=tskit._print_options["max_lines"]
|
665 | 661 | )
|
666 |
| - headers = "".join(f"<th>{header}</th>" for header in headers) |
667 |
| - rows = ( |
668 |
| - f'<td style="text-align: center;" colspan="{len(headers)}"><em>{row[11:]}' |
669 |
| - f" rows skipped (tskit.set_print_options)</em></td>" |
670 |
| - if "__skipped__" in row |
671 |
| - else "".join(f"<td>{cell}</td>" for cell in row) |
672 |
| - for row in rows |
673 |
| - ) |
674 |
| - rows = "".join(f"<tr>{row}</tr>\n" for row in rows) |
675 |
| - return f""" |
676 |
| - <div> |
677 |
| - <style scoped=""> |
678 |
| - .tskit-table tbody tr th:only-of-type {{vertical-align: middle;}} |
679 |
| - .tskit-table tbody tr th {{vertical-align: top;}} |
680 |
| - .tskit-table tbody td {{text-align: right;padding: 0.5em 0.5em;}} |
681 |
| - .tskit-table tbody th {{padding: 0.5em 0.5em;}} |
682 |
| - </style> |
683 |
| - <table border="1" class="tskit-table"> |
684 |
| - <thead> |
685 |
| - <tr> |
686 |
| - {headers} |
687 |
| - </tr> |
688 |
| - </thead> |
689 |
| - <tbody> |
690 |
| - {rows} |
691 |
| - </tbody> |
692 |
| - </table> |
693 |
| - </div> |
694 |
| - """ |
| 662 | + return util.html_table(rows, header=headers) |
695 | 663 |
|
696 | 664 | def _columns_all_integer(self, *colnames):
|
697 | 665 | # For displaying floating point values without loads of decimal places
|
@@ -852,15 +820,8 @@ def __init__(self, max_rows_increment=0, ll_table=None):
|
852 | 820 | def _text_header_and_rows(self, limit=None):
|
853 | 821 | headers = ("id", "flags", "location", "parents", "metadata")
|
854 | 822 | rows = []
|
855 |
| - if limit is None or self.num_rows <= limit: |
856 |
| - indexes = range(self.num_rows) |
857 |
| - else: |
858 |
| - indexes = itertools.chain( |
859 |
| - range(limit // 2), |
860 |
| - [-1], |
861 |
| - range(self.num_rows - (limit - (limit // 2)), self.num_rows), |
862 |
| - ) |
863 |
| - for j in indexes: |
| 823 | + row_indexes = util.truncate_rows(self.num_rows, limit) |
| 824 | + for j in row_indexes: |
864 | 825 | if j == -1:
|
865 | 826 | rows.append(f"__skipped__{self.num_rows-limit}")
|
866 | 827 | else:
|
@@ -1105,16 +1066,9 @@ def __init__(self, max_rows_increment=0, ll_table=None):
|
1105 | 1066 | def _text_header_and_rows(self, limit=None):
|
1106 | 1067 | headers = ("id", "flags", "population", "individual", "time", "metadata")
|
1107 | 1068 | rows = []
|
1108 |
| - if limit is None or self.num_rows <= limit: |
1109 |
| - indexes = range(self.num_rows) |
1110 |
| - else: |
1111 |
| - indexes = itertools.chain( |
1112 |
| - range(limit // 2), |
1113 |
| - [-1], |
1114 |
| - range(self.num_rows - (limit - (limit // 2)), self.num_rows), |
1115 |
| - ) |
| 1069 | + row_indexes = util.truncate_rows(self.num_rows, limit) |
1116 | 1070 | decimal_places_times = 0 if self._columns_all_integer("time") else 8
|
1117 |
| - for j in indexes: |
| 1071 | + for j in row_indexes: |
1118 | 1072 | row = self[j]
|
1119 | 1073 | if j == -1:
|
1120 | 1074 | rows.append(f"__skipped__{self.num_rows-limit}")
|
@@ -1306,16 +1260,9 @@ def __init__(self, max_rows_increment=0, ll_table=None):
|
1306 | 1260 | def _text_header_and_rows(self, limit=None):
|
1307 | 1261 | headers = ("id", "left", "right", "parent", "child", "metadata")
|
1308 | 1262 | rows = []
|
1309 |
| - if limit is None or self.num_rows <= limit: |
1310 |
| - indexes = range(self.num_rows) |
1311 |
| - else: |
1312 |
| - indexes = itertools.chain( |
1313 |
| - range(limit // 2), |
1314 |
| - [-1], |
1315 |
| - range(self.num_rows - (limit - (limit // 2)), self.num_rows), |
1316 |
| - ) |
| 1263 | + row_indexes = util.truncate_rows(self.num_rows, limit) |
1317 | 1264 | decimal_places = 0 if self._columns_all_integer("left", "right") else 8
|
1318 |
| - for j in indexes: |
| 1265 | + for j in row_indexes: |
1319 | 1266 | if j == -1:
|
1320 | 1267 | rows.append(f"__skipped__{self.num_rows-limit}")
|
1321 | 1268 | else:
|
@@ -1528,17 +1475,10 @@ def __init__(self, max_rows_increment=0, ll_table=None):
|
1528 | 1475 | def _text_header_and_rows(self, limit=None):
|
1529 | 1476 | headers = ("id", "left", "right", "node", "source", "dest", "time", "metadata")
|
1530 | 1477 | rows = []
|
1531 |
| - if limit is None or self.num_rows <= limit: |
1532 |
| - indexes = range(self.num_rows) |
1533 |
| - else: |
1534 |
| - indexes = itertools.chain( |
1535 |
| - range(limit // 2), |
1536 |
| - [-1], |
1537 |
| - range(self.num_rows - (limit - (limit // 2)), self.num_rows), |
1538 |
| - ) |
| 1478 | + row_indexes = util.truncate_rows(self.num_rows, limit) |
1539 | 1479 | decimal_places_coords = 0 if self._columns_all_integer("left", "right") else 8
|
1540 | 1480 | decimal_places_times = 0 if self._columns_all_integer("time") else 8
|
1541 |
| - for j in indexes: |
| 1481 | + for j in row_indexes: |
1542 | 1482 | if j == -1:
|
1543 | 1483 | rows.append(f"__skipped__{self.num_rows-limit}")
|
1544 | 1484 | else:
|
@@ -1748,16 +1688,9 @@ def __init__(self, max_rows_increment=0, ll_table=None):
|
1748 | 1688 | def _text_header_and_rows(self, limit=None):
|
1749 | 1689 | headers = ("id", "position", "ancestral_state", "metadata")
|
1750 | 1690 | rows = []
|
1751 |
| - if limit is None or self.num_rows <= limit: |
1752 |
| - indexes = range(self.num_rows) |
1753 |
| - else: |
1754 |
| - indexes = itertools.chain( |
1755 |
| - range(limit // 2), |
1756 |
| - [-1], |
1757 |
| - range(self.num_rows - (limit - (limit // 2)), self.num_rows), |
1758 |
| - ) |
| 1691 | + row_indexes = util.truncate_rows(self.num_rows, limit) |
1759 | 1692 | decimal_places = 0 if self._columns_all_integer("position") else 8
|
1760 |
| - for j in indexes: |
| 1693 | + for j in row_indexes: |
1761 | 1694 | if j == -1:
|
1762 | 1695 | rows.append(f"__skipped__{self.num_rows-limit}")
|
1763 | 1696 | else:
|
@@ -1971,17 +1904,10 @@ def __init__(self, max_rows_increment=0, ll_table=None):
|
1971 | 1904 | def _text_header_and_rows(self, limit=None):
|
1972 | 1905 | headers = ("id", "site", "node", "time", "derived_state", "parent", "metadata")
|
1973 | 1906 | rows = []
|
1974 |
| - if limit is None or self.num_rows <= limit: |
1975 |
| - indexes = range(self.num_rows) |
1976 |
| - else: |
1977 |
| - indexes = itertools.chain( |
1978 |
| - range(limit // 2), |
1979 |
| - [-1], |
1980 |
| - range(self.num_rows - (limit - (limit // 2)), self.num_rows), |
1981 |
| - ) |
| 1907 | + row_indexes = util.truncate_rows(self.num_rows, limit) |
1982 | 1908 | # Currently mutations do not have discretised times: this for consistency
|
1983 | 1909 | decimal_places_times = 0 if self._columns_all_integer("time") else 8
|
1984 |
| - for j in indexes: |
| 1910 | + for j in row_indexes: |
1985 | 1911 | if j == -1:
|
1986 | 1912 | rows.append(f"__skipped__{self.num_rows-limit}")
|
1987 | 1913 | else:
|
@@ -2232,15 +2158,8 @@ def add_row(self, metadata=None):
|
2232 | 2158 | def _text_header_and_rows(self, limit=None):
|
2233 | 2159 | headers = ("id", "metadata")
|
2234 | 2160 | rows = []
|
2235 |
| - if limit is None or self.num_rows <= limit: |
2236 |
| - indexes = range(self.num_rows) |
2237 |
| - else: |
2238 |
| - indexes = itertools.chain( |
2239 |
| - range(limit // 2), |
2240 |
| - [-1], |
2241 |
| - range(self.num_rows - (limit - (limit // 2)), self.num_rows), |
2242 |
| - ) |
2243 |
| - for j in indexes: |
| 2161 | + row_indexes = util.truncate_rows(self.num_rows, limit) |
| 2162 | + for j in row_indexes: |
2244 | 2163 | if j == -1:
|
2245 | 2164 | rows.append(f"__skipped__{self.num_rows-limit}")
|
2246 | 2165 | else:
|
@@ -2490,15 +2409,8 @@ def append_columns(
|
2490 | 2409 | def _text_header_and_rows(self, limit=None):
|
2491 | 2410 | headers = ("id", "timestamp", "record")
|
2492 | 2411 | rows = []
|
2493 |
| - if limit is None or self.num_rows <= limit: |
2494 |
| - indexes = range(self.num_rows) |
2495 |
| - else: |
2496 |
| - indexes = itertools.chain( |
2497 |
| - range(limit // 2), |
2498 |
| - [-1], |
2499 |
| - range(self.num_rows - (limit - (limit // 2)), self.num_rows), |
2500 |
| - ) |
2501 |
| - for j in indexes: |
| 2412 | + row_indexes = util.truncate_rows(self.num_rows, limit) |
| 2413 | + for j in row_indexes: |
2502 | 2414 | if j == -1:
|
2503 | 2415 | rows.append(f"__skipped__{self.num_rows-limit}")
|
2504 | 2416 | else:
|
|
0 commit comments