Ejemplo n.º 1
0
def check_deterministic_inclusion(
    g1 :Automaton,
    g2 :Automaton,
    expected :int,
    show_g1 :bool = True,
    show_g2 :bool = True
):
    obtained = deterministic_inclusion(g1, g2)

    if in_ipynb():
        from pybgl.graphviz import graph_to_html
        from pybgl.html     import html
        l = list()
        if show_g1:
            l += ["<b>A</b>",  TEMPLATE_HTML % graph_to_html(g1)]
        if show_g2:
            l += ["<b>A'</b>", TEMPLATE_HTML % graph_to_html(g2)]
        result = "A c A'" if obtained == 1 else \
                 "A = A'" if obtained == 0 else \
                 "A' c A" if obtained == -1 else \
                 "A ! A'" if obtained is None else \
                 "??????"
        l.append(result)
        html("<br/>".join(l))

    assert obtained == expected, "obtained = %s expected = %s" % (obtained, expected)
Ejemplo n.º 2
0
def test_graph_to_html_with_weird_chars():
    from pybgl.automaton import Automaton, add_edge
    g = Automaton(2)
    add_edge(0, 1, WEIRD_CHARS, g)
    graph_to_html(g)
    if in_ipynb():
        ipynb_display_graph(g)
Ejemplo n.º 3
0
def _test_revuz_minimize(g: IncidenceAutomaton, e_expected: set):
    before_html = graph_to_html(g) if in_ipynb() else None
    revuz_minimize(g)
    if in_ipynb():
        after_html = graph_to_html(g)
        html(beside(before_html, after_html, "Minimization", "Before",
                    "After"))
    check_graph(g, e_expected)
Ejemplo n.º 4
0
def test_rpn_deque_ast():
    tokenized = tokenizer_re("(a?b)*?c+d")
    ast = Ast()
    output = RpnDequeAst(map_operators=MAP_OPERATORS_RE, ast=ast)
    ret = shunting_yard_postfix(tokenized, MAP_OPERATORS_RE, output=output)
    assert num_vertices(ast) == 11
    assert num_edges(ast) == 10
    [root] = ret
    assert root == 10
    from pybgl.graphviz import graph_to_html
    graph_to_html(ast)
Ejemplo n.º 5
0
def test_trie_trie():
    t1 = make_t1()
    t2 = make_t2()
    t1.insert(t2)
    if in_ipynb(): html(graph_to_html(t1))
    assert num_vertices(t1) == 26
    assert num_vertices(t2) == 12
Ejemplo n.º 6
0
def test_graph_to_html_with_html_sequences():
    from collections import defaultdict
    from pybgl.property_map import make_assoc_property_map

    g = DirectedGraph(2)
    (e, _) = add_edge(0, 1, g)
    pmap_vlabel = make_assoc_property_map(defaultdict(str))
    pmap_elabel = make_assoc_property_map(defaultdict(str))
    gdp = GraphDp(g, dpv={"label": pmap_vlabel}, dpe={"label": pmap_elabel})

    for label in [
            "<b>foo</b>",
            "<foo>",
            "<",
            ">",
            "<b>foo</b><bar>",
            "<bar><b>foo</b>",
            "<font color='red'><b>foo</b></font>",
            # NB: foo.png must exists + graphviz imposes <img/> not <img>
            #"<table><tr><td><img src='foo.png'/></td></tr></table>",
    ]:
        print(f"{label} --> {graphviz_escape_html(label)}")
        pmap_vlabel[0] = pmap_vlabel[1] = pmap_elabel[e] = label
        shtml = graph_to_html(gdp)
        if in_ipynb():
            html(shtml)
            ipynb_display_graph(gdp)
Ejemplo n.º 7
0
def test_max_suffix_tree_g():
    t = make_suffix_trie("ananas")
    if in_ipynb(): html(graph_to_html(t))
    assert num_vertices(t) == 16
    for q in vertices(t):
        assert is_final(q, t)
    assert not is_final(BOTTOM, t)
Ejemplo n.º 8
0
def test_deterministic_union(show_g1: bool = True,
                             show_g2: bool = True,
                             show_g12: bool = True):
    g1 = make_dafsa1()
    g2 = make_dafsa2()
    g12 = deterministic_union(g1, g2)

    if in_ipynb():
        from pybgl.graphviz import graph_to_html
        from pybgl.html import html
        l = list()
        if show_g1: l += ["<b>A</b>", graph_to_html(g1)]
        if show_g2: l += ["<b>A'</b>", graph_to_html(g2)]
        if show_g12: l += ["<b>A &#x222a; A'</b><br/>", graph_to_html(g12)]
        html("<br/>".join(l))
    assert num_vertices(g12) == 12
    assert num_edges(g12) == 11
Ejemplo n.º 9
0
def test_graph_graphviz():
    for G in [DirectedGraph, UndirectedGraph]:
        g = G(3)
        (e1, _) = add_edge(0, 1, g)
        (e2, _) = add_edge(0, 1, g)
        (e3, _) = add_edge(0, 2, g)
        (e4, _) = add_edge(0, 2, g)
        (e5, _) = add_edge(1, 2, g)
        svg = graph_to_html(g)
Ejemplo n.º 10
0
def test_matching_tries():
    both = {"an", "banana"}
    only1 = {"ananas", "x", "y", "z"}
    only2 = {"bananas", "bank", "t"}
    corpus1 = both | only1
    corpus2 = both | only2

    t1 = Trie()
    for w in corpus1:
        t1.insert(w)

    t2 = Trie()
    for w in corpus2:
        t2.insert(w)

    if in_ipynb():
        html(graph_to_html(t1))
        html(graph_to_html(t2))

    l = trie_matching(t1, t2)
    assert l[1] == len(only1)
    assert l[2] == len(only2)
    assert l[3] == len(both)
Ejemplo n.º 11
0
def strong_components_to_html(g, pmap_color, pmap_component) -> str:
    """
    Args:
        g: A DirectedGraph.
        pmap_component: A ReadPropertyMap, mapping a vertex with its strongly
            connected component.
    """
    return graph_to_html(
        g,
        dpv={
            "color" : make_func_property_map(lambda u: pmap_color[pmap_component[u]])
        },
        dpe={
            "color" : make_func_property_map(lambda e: edge_color(e, g, pmap_component, pmap_color)),
            "style" : make_func_property_map(lambda e: (
                "solid" if edge_color(e, g, pmap_component, pmap_color, None) else "dashed"
            )),
        }
    )
Ejemplo n.º 12
0
def test_revuz_height():
    g = make_incidence_node_automaton([(0, 1), (0, 2), (1, 2), (1, 3), (2, 4),
                                       (3, 4)],
                                      make_assoc_property_map(
                                          defaultdict(lambda: None, {
                                              1: "a",
                                              2: "b",
                                              3: "a",
                                              4: "c"
                                          })))

    map_vheight = defaultdict()
    pmap_vheight = make_assoc_property_map(map_vheight)
    pmap_vlabel = make_func_property_map(lambda u: "%s<br/>height: %s" %
                                         (u, pmap_vheight[u]))
    max_height = revuz_height(g, pmap_vheight)

    if in_ipynb():
        html(graph_to_html(g))

    assert map_vheight == {0: 3, 1: 2, 2: 1, 3: 1, 4: 0}
    assert max_height == 3, f"Expected max_height = 3, got {max_height}"
Ejemplo n.º 13
0
def test_trie_digital_sequence():
    t2 = make_t2()
    if in_ipynb(): html(graph_to_html(t2))
    assert num_vertices(t2) == 12
Ejemplo n.º 14
0
def test_nfa_graphviz():
    nfa = make_nfa1()
    svg = graph_to_html(nfa)
Ejemplo n.º 15
0
def test_incidence_node_automaton_graphviz():
    g = make_g2()
    svg = graph_to_html(g)
Ejemplo n.º 16
0
def test_trie_string():
    t1 = make_t1()
    if in_ipynb(): html(graph_to_html(t1))
    assert num_vertices(t1) == 17
Ejemplo n.º 17
0
def test_bk_tree_graphviz():
    from pybgl.graphviz import graph_to_html
    s = graph_to_html(TREE)
    assert isinstance(s, str)
Ejemplo n.º 18
0
def test_incidence_automaton_graphviz():
    svg = graph_to_html(G1)
Ejemplo n.º 19
0
def test_automaton_graphviz():
    svg = graph_to_html(G1)
    assert isinstance(svg, str)
Ejemplo n.º 20
0
def test_graph_to_html_with_pmaps():
    # Configure theme

    GraphvizStyle.set_fg_color("grey")
    GraphvizStyle.set_bg_color("transparent")
    display_graph = ipynb_display_graph

    from pybgl.graph_dp import GraphDp
    from pybgl.property_map import make_func_property_map

    def vertex_filter(u):
        return u < 5

    def edge_filter(e, g, vertex_filter):
        return vertex_filter(source(e, g)) and vertex_filter(target(e, g))

    for G in [DirectedGraph, UndirectedGraph]:
        html(str(G))
        g = make_graph(G)

        # Graph configuration display
        dv = {"color": "purple"}
        de = {"color": "red"}
        dpv = {
            "fontcolor":
            make_func_property_map(lambda e: "cyan" if e % 2 else "orange")
        }
        dpe = {
            "fontcolor":
            make_func_property_map(lambda e: "blue"
                                   if source(e, g) % 2 else "red"),
            "label":
            make_func_property_map(
                lambda e: f"({source(e, g)}, {target(e, g)})")
        }

        # Choose view
        # Omit vs (resp. es) to iterate over all vertices (resp. edges)
        vs = [u for u in vertices(g) if vertex_filter(u)]
        es = [e for e in edges(g) if edge_filter(e, g, vertex_filter)]

        # Method1: call helper (ipynb_display_graph, graph_to_html)
        shtml = graph_to_html(g, dpv=dpv, dpe=dpe, dv=dv, de=de, vs=vs, es=es)
        assert isinstance(shtml, str)
        if in_ipynb():
            ipynb_display_graph(g,
                                dpv=dpv,
                                dpe=dpe,
                                dv=dv,
                                de=de,
                                vs=vs,
                                es=es)

        # Method2: use GraphDp. This offers the opportunity to export the
        # displayed graph to other export formats.
        gdp = GraphDp(g, dpv=dpv, dpe=dpe, dv=dv, de=de)

        # These two commands have the same outcome
        shtml = graph_to_html(gdp, vs=vs, es=es)
        assert isinstance(shtml, str)
        shtml = graph_to_html(gdp,
                              dpv=dpv,
                              dpe=dpe,
                              dv=dv,
                              de=de,
                              vs=vs,
                              es=es)
        assert isinstance(shtml, str)

        if in_ipynb():
            # These two commands have the same outcome
            ipynb_display_graph(gdp, vs=vs, es=es)
            ipynb_display_graph(gdp,
                                dpv=dpv,
                                dpe=dpe,
                                dv=dv,
                                de=de,
                                vs=vs,
                                es=es)
Ejemplo n.º 21
0
def _test_graph_copy_small(threshold :int = 50):
    g = DirectedGraph(5)
    (e01, _) = add_edge(0, 1, g)
    (e02, _) = add_edge(0, 2, g)
    (e04, _) = add_edge(0, 4, g)
    (e12, _) = add_edge(1, 2, g)
    (e23, _) = add_edge(2, 3, g)
    (e24, _) = add_edge(2, 4, g)
    (e40, _) = add_edge(4, 0, g)
    (e44, _) = add_edge(4, 4, g)
    map_eweight = defaultdict(
        lambda: None,
        {
            e01 : 83,
            e02 : 3,
            e04 : 78,
            e12 : 92,
            e23 : 7,
            e24 : 18,
            e40 : 51,
            e44 : 84,
        }
    )
    pmap_eweight = make_assoc_property_map(map_eweight)
    pmap_erelevant = make_func_property_map(lambda e: pmap_eweight[e] >= threshold)

    g_dup = DirectedGraph(0)

    # Edge duplicate
    map_eweight_dup = defaultdict()
    pmap_eweight_dup = make_assoc_property_map(map_eweight_dup)
    def callback_dup_edge(e, g, e_dup, g_dup):
        pmap_eweight_dup[e_dup] = pmap_eweight[e]

    # Vertex mapping
    map_vertices = defaultdict()
    pmap_vertices = make_assoc_property_map(map_vertices)
    map_edges = defaultdict()
    pmap_edges = make_assoc_property_map(map_edges)

    graph_copy(
        0, g, g_dup,
        pmap_erelevant    = pmap_erelevant,
        pmap_vertices     = pmap_vertices,
        pmap_edges        = pmap_edges,
        callback_dup_edge = callback_dup_edge
    )

    if in_ipynb():
        ori_html = graph_to_html(
            g,
            dpv = {
                "label" : make_func_property_map(lambda u: "%s<br/>(becomes %s)" % (u, pmap_vertices[u]))
            },
            dpe = {
                "color" : make_func_property_map(lambda e : "darkgreen" if pmap_erelevant[e] else "lightgrey"),
                "style" : make_func_property_map(lambda e : "solid"     if pmap_erelevant[e] else "dashed"),
                "label" : pmap_eweight,
            }
        )
        dup_html = graph_to_html(
            g_dup,
            dpe = {
                "label" : pmap_eweight_dup,
            }
        )
        html(
            """
            <table>
                <tr>
                    <th>Original</th>
                    <th>Extracted</th>
                </tr><tr>
                    <td>%s</td>
                    <td>%s</td>
                </tr>
            </table>
            """ % (ori_html, dup_html)
        )

    if threshold == 50:
        expected_num_edges = 5
        assert map_vertices == {
            0 : 0,
            1 : 1,
            2 : 2,
            4 : 3
        }
        for e, e_dup in map_edges.items():
            u = source(e, g)
            v = target(e, g)
            u_dup = source(e_dup, g_dup)
            v_dup = target(e_dup, g_dup)
            assert u_dup == pmap_vertices[u], "u_dup = %s ; pmap_vertices[%s] = %s" % (u_dup, u, pmap_vertices[u])
            assert v_dup == pmap_vertices[v], "v_dup = %s ; pmap_vertices[%s] = %s" % (v_dup, v, pmap_vertices[v])
            assert pmap_eweight[e] == pmap_eweight_dup[e_dup]
    elif threshold < min(w for w in map_eweight.values()):
        expected_num_edges = num_edges(g)
    elif threshold > max(w for w in map_eweight.values()):
        expected_num_edges = 0

    assert expected_num_edges == num_edges(g_dup), \
        """
        Invalid edge number:
          Expected: %s
          Obtained: %s
        """ % (expected_num_edges, num_edges(g_dup))
Ejemplo n.º 22
0
def test_node_automaton_graphviz():
    g = make_g2()
    svg = graph_to_html(g)
    assert isinstance(svg, str)
Ejemplo n.º 23
0
def test_graph_to_html():
    g = DirectedGraph(2)
    (e, _) = add_edge(0, 1, g)
    shtml = graph_to_html(g)
    if in_ipynb():
        ipynb_display_graph(g)