Code Monkey home page Code Monkey logo

deepstochlog's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

deepstochlog's Issues

Tree Vizualization

I noticed that there is no way to visualize the query trees (at least that I could find) and it has helped me quite a bit in debugging to be able to look at them. This involves writing a method in TabledAndOrTrees which I have done. Please let me know whether this is something you want to add to the code.

def to_dot(self, term: Term):
    if term not in self._and_or_tree:
        raise RuntimeError("term {} is not in the tree".format(term))
    from deepstochlog.logic import Or, And, NNLeaf, StaticProbability, TermLeaf

    nodes, edges = [], []
    num_and_nodes = 0
    num_or_nodes = 0

    def recurse_term_node(term: Term, parent_hash):
        node = self._and_or_tree[term]
        node_hash = abs(hash(node) + parent_hash)
        if isinstance(node, Or):
            nonlocal num_or_nodes
            num_or_nodes += 1
            nodes.append(
                "{} [label={}]".format(
                    node_hash,
                    '<OR<BR/> <FONT POINT-SIZE="7"> {} </FONT>>'.format(str(term)),
                )
            )
        elif isinstance(node, And):
            nonlocal num_and_nodes
            num_and_nodes += 1
            nodes.append(
                "{} [label={}]".format(
                    node_hash,
                    '<AND<BR/> <FONT POINT-SIZE="7"> {} </FONT>>'.format(str(term)),
                )
            )

        elif isinstance(node, NNLeaf):
            nodes.append('{} [label="{}"]'.format(node_hash, str(node)))
            return

        for child in node.children:
            child_hash = abs(hash(child) + node_hash)
            if not isinstance(child, TermLeaf):
                edges.append((node_hash, child_hash))
            resolve_other_node(child, node_hash)

    def resolve_other_node(node, parent_hash):
        node_hash = abs(hash(node) + parent_hash)
        if isinstance(node, TermLeaf):
            edges.append(
                (
                    parent_hash,
                    abs(hash(self._and_or_tree[node.term]) + node_hash),
                )
            )
            recurse_term_node(node.term, node_hash)
            return
        elif isinstance(node, Or):
            nonlocal num_or_nodes
            num_or_nodes += 1
            nodes.append("{} [label={}]".format(node_hash, '"OR"'))
        elif isinstance(node, And):
            nonlocal num_and_nodes
            num_and_nodes += 1
            nodes.append("{} [label={}]".format(node_hash, '"AND"'))
        elif isinstance(node, StaticProbability):
            nodes.append("{} [label={}]".format(node_hash, node.probability))
            return
        elif isinstance(node, NNLeaf):
            nodes.append('{} [label="{}"]'.format(node_hash, str(node)))
            return
        else:
            raise RuntimeError("unexpected node type")

        for child in node.children:
            child_hash = abs(hash(child) + node_hash)
            if not isinstance(child, TermLeaf):
                edges.append((node_hash, child_hash))
            resolve_other_node(child, node_hash)

    recurse_term_node(term, 0)
    dot_string = (
        "Digraph {\n"
        + "\n".join(nodes)
        + "\n"
        + "\n".join(
            [
                "{} -> {}".format(source, destination)
                for source, destination in edges
            ]
        )
        + "\n}"
    )
    print(
        "run circuit with {} nodes and {} edges ({} and nodes, {} or nodes)".format(
            len(nodes), len(edges), num_and_nodes, num_or_nodes
        )
    )
    return dot_string, {
        "num_nodes": len(nodes),
        "num_edges": len(edges),
        "num_or_nodes": num_or_nodes,
        "num_and_nodes": num_and_nodes,
    }

It is possible it can be done much more compactly by somehow utilizing the visitors similarly to how the tree is actually traversed when computing queries. This produces something of this sort (truncated here for ease).
image

If you are interested please let me know if there is anything else I can do to help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.