grand-cypher
grand-cypher copied to clipboard
Possible bug with hints generation
#90 encountered the following error during development.
AttributeError: 'Graph' object has no attribute 'pred'
Traceback:
File "python3.10/site-packages/grandcypher/__init__.py", line 1708, in run
return self._transformer._executors[0].returns()
File "python3.10/site-packages/grandcypher/__init__.py", line 897, in returns
results = self._lookup(
File "python3.10/site-packages/grandcypher/__init__.py", line 675, in _lookup
true_matches = self._get_true_matches()
File "python3.10/site-packages/grandcypher/__init__.py", line 1102, in _get_true_matches
for match in self._matches_iter(my_motif):
File "python3.10/site-packages/grandcypher/__init__.py", line 1190, in _matches_iter
for match in grandiso_finder:
File "python3.10/site-packages/grandiso/__init__.py", line 427, in find_motifs_iter
yield from walk(path)
File "python3.10/site-packages/grandiso/__init__.py", line 412, in walk
for candidate in get_next_backbone_candidates(
File "python3.10/site-packages/grandiso/__init__.py", line 260, in get_next_backbone_candidates
candidate_nodes_from_this_edge = host.pred[backbone[target]]
This fix worked around the problem but I'm unsure if that is the best approach. It appears somewhere along the way the new logic is incorrectly assuming the Graph is a DiGraph.
hi @davidmezzetti , possible to share your graph setup? I'm not sure yet how the hints would affect isomorphic search in grandiso.
@khoale88
I was using txtai when I ran into this error but I've simplified this into a reproducible use case.
- Download this file to /tmp/graph
- Run
pip install msgpack - Run the following code
from grandcypher import GrandCypher
import msgpack
import networkx as nx
# Load graph from file
graph = nx.Graph()
with open("/tmp/graph", "rb") as stream:
data = msgpack.unpack(stream)
graph.add_nodes_from(data["nodes"])
graph.add_edges_from(data["edges"])
GrandCypher(graph).run('MATCH P=({id: "Linux"})-[*1..4]->({id: "MacOS"})-[*1..4]->({id: "Microsoft Windows"}) RETURN P LIMIT 10')
You have to undo the fix I made locally in grand-cypher to run into the bug.
This is due to hinting
To double confirm, this code doesn't throw an error
gc = GrandCypher(graph)
gc._transformer._executors[0]._auto_node_jsondata_hints = False
gc.run(
"""MATCH P=(A{id: "Linux"})-[*1..4]->(B{id: "MacOS"})-[*1..4]->(C{id: "Microsoft Windows"})
RETURN P
LIMIT 10""",
)
The same code with hint will throw an error
graph.add_nodes_from(data["nodes"])
graph.add_edges_from(data["edges"])
gc = GrandCypher(graph)
gc._transformer._executors[0]._auto_node_jsondata_hints = False
gc.run(
"""MATCH P=(A{id: "Linux"})-[*1..4]->(B{id: "MacOS"})-[*1..4]->(C{id: "Microsoft Windows"})
RETURN P
LIMIT 10""",
hints=[{'C': 5937, 'B': 3553, 'A': 6818}]
)
I think this is because the hints for B and C causing grandiso to call of predecessor to find A.
We need more brain from @j6k4m8
P.S @davidmezzetti you can turn of the hints using gc._transformer._executors[0]._auto_node_jsondata_hints = False.
Interesting, thanks for taking a look!
I will say this behavior doesn't occur in previous versions, it's something new that was introduced with #82. That query used to work before that change.
@davidmezzetti the auto hint works like indexing. It narrows down search space for grandiso. I guess we can make a small improvement that is to turn off auto hint if graph is an instance of nx.Graph.
It might be still a workaround and we need @j6k4m8 to double check if he can make hints workable with Graph in grandiso, or we turn off auto hints for Graph permanently, or we can try to manipulate hints better for Graph.
I'll defer to whatever you guys feel is best on this. If the fix I had is the right move, that's good with me. If we need something more involved, just let me know how I can help.