How to use the cogent3.util.table.Table function in cogent3

To help you get started, we’ve selected a few cogent3 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cogent3 / cogent3 / tests / test_util / test_table.py View on Github external
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="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))
github cogent3 / cogent3 / tests / test_util / test_table.py View on Github external
def test_repr_html_(self):
        """should produce html"""
        # no index
        t = Table(header=self.t8_header, data=self.t8_rows)
        _ = t._repr_html_()
        # with an index
        t = Table(header=self.t8_header, data=self.t8_rows, index="edge.name")
        _ = t._repr_html_()

        # 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_()
github cogent3 / cogent3 / tests / test_util / test_table.py View on Github external
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"})
github cogent3 / cogent3 / tests / test_util / test_table.py View on Github external
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"))
github cogent3 / cogent3 / tests / test_util / test_table.py View on Github external
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)
github cogent3 / cogent3 / tests / test_util / test_table.py View on Github external
def test_tolist(self):
        """test the table tolist method"""
        t3 = Table(header=self.t3_header, data=self.t3_rows)
        self.assertEqual(t3.tolist("id"), [6, 7])
        self.assertEqual(t3.tolist("foo"), ["abc", "bca"])
github cogent3 / cogent3 / tests / test_util / test_table.py View on Github external
def test_repr_html_(self):
        """should produce html"""
        # no index
        t = Table(header=self.t8_header, data=self.t8_rows)
        _ = t._repr_html_()
        # with an index
        t = Table(header=self.t8_header, data=self.t8_rows, index="edge.name")
        _ = t._repr_html_()

        # 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_()
github cogent3 / cogent3 / src / cogent3 / app / result.py View on Github external
def _get_repr_data_(self):
        rows = []
        attrs = ["lnL", "nfp", "DLC", "unique_Q"]
        for key, member in self.items():
            member.deserialised_values()  # making sure we're fully reloaded
            if key == self._name_of_null:
                status_name = ["null", repr(key)]
            else:
                status_name = ["alt", repr(key)]
            row = status_name + [getattr(member, a) for a in attrs]
            rows.append(row)

        table = Table(header=["hypothesis", "key"] + attrs, data=rows, title=self.name)
        table = table.sorted(columns="nfp")
        stats = [[self.LR, self.df, self.pvalue]]
        stats = Table(header=["LR", "df", "pvalue"], data=stats, title="Statistics")
        return stats, table
github cogent3 / cogent3 / src / cogent3 / evolve / likelihood_function.py View on Github external
d = d[part]
                    except KeyError:
                        d = "NA"
                    else:
                        row_used = True
                    row[param] = d
                if row_used:
                    row.update(dict(list(zip(table_dims, scope))))
                    row = [row[k] for k in heading_names]
                    list_table.append(row)
            if table_dims:
                title = ["", "%s params" % " ".join(table_dims)][with_titles]
            else:
                title = ["", "global params"][with_titles]
            row_ids = None
            stat_table = table.Table(
                heading_names,
                list_table,
                max_width=80,
                index=row_ids,
                title=title,
                **self._format,
            )
            if group[table_dims] == [mprob_name]:
                # if stat_table.shape
                # if mprobs, we use the motifs as header
                motifs = list(sorted(set(stat_table.tolist("motif"))))
                if stat_table.shape[1] == 2:
                    motif_prob = dict(stat_table.tolist())
                    heading_names = motifs
                    list_table = [motif_prob[m] for m in motifs]
                    list_table = [list_table]
github cogent3 / cogent3 / src / cogent3 / evolve / fast_distance.py View on Github external
from cogent3.util.table import Table

    header = [r"Seq1 \ Seq2"] + names
    rows = zeros((len(names), len(names)), dtype="O")
    for i in range(len(names) - 1):
        n1 = names[i]
        for j in range(i + 1, len(names)):
            n2 = names[j]
            val = stats[(n1, n2)]
            rows[i, j] = val
            rows[j, i] = val
    rows = rows.tolist()
    for i in range(len(names)):
        rows[i].insert(0, names[i])

    table = Table(header=header, rows=rows, row_ids=True, missing_data="*", **kwargs)
    return table