How to use the cogent3.util.union_dict.UnionDict 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_app / test_io.py View on Github external
def test_write_json_with_info(self):
        """correctly writes an object with info attribute from json"""
        # create a mock object that pretends like it's been derived from
        # something
        from cogent3.util.union_dict import UnionDict

        with TemporaryDirectory(dir=".") as dirname:
            outdir = join(dirname, "delme")
            mock = Mock()
            mock.to_rich_dict = DNA.to_rich_dict
            mock.info = UnionDict(source=join("blah", "delme.json"))
            writer = io_app.write_json(outdir, create=True)
            _ = writer(mock)
            reader = io_app.load_json()
            got = reader(join(outdir, "delme.json"))
            self.assertEqual(got, DNA)

        # now with a zipped archive
        with TemporaryDirectory(dir=".") as dirname:
            outdir = join(dirname, "delme.zip")
            mock = Mock()
            mock.to_rich_dict = DNA.to_rich_dict
            mock.info = UnionDict(source=join("blah", "delme.json"))
            writer = io_app.write_json(outdir, create=True)
            identifier = writer(mock)
            reader = io_app.load_json()
            got = reader(writer.data_store[0])
github cogent3 / cogent3 / tests / test_util / test_union_dict.py View on Github external
def test_union(self):
        """correctly adjust a prob vector so all values > minval"""
        d = UnionDict({"a": 1, "b": 2, "c": 3, "d": {"e": 5, "f": 6}})
        e = UnionDict({"b": 0, "d": {"f": 0, "g": 7}})
        d |= e
        self.assertEqual(d.a, 1)
        self.assertEqual(d.b, 0)
        self.assertEqual(d.d.e, 5)
        self.assertEqual(d.d.f, 0)
        self.assertEqual(d.d.g, 7)
github cogent3 / cogent3 / tests / test_util / test_union_dict.py View on Github external
def test_union_with_empty_sub_dict(self):
        """unioning with a dict that has an empty sub-dict"""
        d = UnionDict({"title": {}})
        e = UnionDict({"title": {"text": "Alignment Position"}})
        f = UnionDict(e.copy())
        e |= d
        self.assertEqual(e, f)
github cogent3 / cogent3 / src / cogent3 / draw / dendrogram.py View on Github external
with plotly line style to applied to these edges
        legendgroup : str or None
            if str, a legend will be presented
        tip2 : str
            if provided, and edges is a str, passes edges (as tip1) and kwargs to get_edge_names
        kwargs
            keyword arguments passed onto get_edge_names
        """
        if tip2:
            assert type(edges) == str, "cannot use a series of edges and tip2"
            edges = self.get_edge_names(edges, tip2, **kwargs)

        if type(edges) == str:
            edges = [edges]
        edges = frozenset(edges)
        style = UnionDict(width=self._line_width, color=self._line_color)
        style.update(line)
        self._edge_sets[edges] = UnionDict(legendgroup=legendgroup, line=style)
        mapping = {e: edges for e in edges}
        self._edge_mapping.update(mapping)
        if legendgroup:
            self.layout["showlegend"] = True

        # need to trigger recreation of figure
        self._traces = []
github cogent3 / cogent3 / src / cogent3 / draw / drawable.py View on Github external
def as_trace(self, name=None):
        """returns component for plotly display"""
        name = name or self.name
        data = UnionDict(
            type="scatter",
            x=self.x,
            y=self.y,
            mode=self._mode,
            fill="toself",
            fillcolor=self.fillcolor,
            line=dict(color=self.fillcolor),
            text=self.text,
            name=name,
            legendgroup=self._legendgroup,
            showlegend=self._showlegend,
            hoverinfo="text",
        )
        return data
github cogent3 / cogent3 / src / cogent3 / draw / dendrogram.py View on Github external
if str, a legend will be presented
        tip2 : str
            if provided, and edges is a str, passes edges (as tip1) and kwargs to get_edge_names
        kwargs
            keyword arguments passed onto get_edge_names
        """
        if tip2:
            assert type(edges) == str, "cannot use a series of edges and tip2"
            edges = self.get_edge_names(edges, tip2, **kwargs)

        if type(edges) == str:
            edges = [edges]
        edges = frozenset(edges)
        style = UnionDict(width=self._line_width, color=self._line_color)
        style.update(line)
        self._edge_sets[edges] = UnionDict(legendgroup=legendgroup, line=style)
        mapping = {e: edges for e in edges}
        self._edge_mapping.update(mapping)
        if legendgroup:
            self.layout["showlegend"] = True

        # need to trigger recreation of figure
        self._traces = []
github cogent3 / cogent3 / src / cogent3 / draw / letter.py View on Github external
def as_shape(self):
        """returns component for plotly display"""
        self._scale(x=self.scale_x, y=self.scale_y)
        self._shift(x=self.x, y=self.y)
        self._rotate(degrees=self.degrees)

        path = ",".join(
            [" ".join([str(sub_seg) for sub_seg in seg]) for seg in self.path]
        )
        data = UnionDict(
            type="path",
            xref=self.xref,
            yref=self.yref,
            path=path,
            line={"width": 0},
            fillcolor=self.fillcolor,
        )
        return data
github cogent3 / cogent3 / src / cogent3 / draw / simplex.py View on Github external
def _get_vertex_label_trace(self):
        trace = UnionDict(
            type="scatter3d",
            # Draw the vertex labels
            x=self.vertices[:, 0],
            y=self.vertices[:, 1],
            z=self.vertices[:, 2],
            marker=UnionDict(size=4, color="#1f77b4", colorscale="Viridis"),
            text=self.vertex_labels,
            textfont=UnionDict(size=16, family="sans serif"),
            mode="markers+text",
            hoverinfo="skip",
            showlegend=False,
            name="labels",
        )
        return trace
github cogent3 / cogent3 / src / cogent3 / draw / dendrogram.py View on Github external
if self.show_support:
                support = edge.support_text_coord(
                    self.support_xshift,
                    self.support_yshift,
                    threshold=self.support_threshold,
                )
                if support is not None:
                    support |= UnionDict(xref="x", yref="y", font=self.tip_font)
                    support_text.append(support)

        traces = []
        for key in grouped:
            group = grouped[key]
            style = self._edge_sets.get(
                key,
                UnionDict(
                    line=UnionDict(
                        width=self._line_width,
                        color=self._line_color,
                        shape="spline",
                        smoothing=1.3,
                    )
                ),
            )
            trace = UnionDict(type="scatter", x=group["x"], y=group["y"], mode="lines")
            trace |= style
            if "legendgroup" not in style:
                trace["showlegend"] = False
            else:
                trace["name"] = style["legendgroup"]
            traces.append(trace)
github cogent3 / cogent3 / src / cogent3 / util / table.py View on Github external
def _get_persistent_attrs(self):
        attrs = UnionDict(self._persistent_attrs.copy())
        return attrs