ml-kuleuven / deepstochlog Goto Github PK
View Code? Open in Web Editor NEWLicense: Apache License 2.0
License: Apache License 2.0
/root/miniconda3/lib/python3.8/site-packages/deepstochlog/_meta_program_suffix.pl I encountered this problem, can you help me solve it, thank you very much!
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).
If you are interested please let me know if there is anything else I can do to help.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.