grand-cypher icon indicating copy to clipboard operation
grand-cypher copied to clipboard

Possible bug with hints generation

Open davidmezzetti opened this issue 3 weeks ago • 6 comments

#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.

davidmezzetti avatar Nov 14 '25 11:11 davidmezzetti

hi @davidmezzetti , possible to share your graph setup? I'm not sure yet how the hints would affect isomorphic search in grandiso.

khoale88 avatar Nov 15 '25 16:11 khoale88

@khoale88

I was using txtai when I ran into this error but I've simplified this into a reproducible use case.

  1. Download this file to /tmp/graph
  2. Run pip install msgpack
  3. 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.

davidmezzetti avatar Nov 16 '25 00:11 davidmezzetti

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.

khoale88 avatar Nov 16 '25 16:11 khoale88

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 avatar Nov 16 '25 16:11 davidmezzetti

@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.

khoale88 avatar Nov 16 '25 17:11 khoale88

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.

davidmezzetti avatar Nov 17 '25 19:11 davidmezzetti