Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add graph hashing method to utils #296

Merged
merged 6 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ repos:
hooks:
- id: mypy
args: ["--install-types", "--non-interactive", "--ignore-missing-imports", "--follow-imports=skip", "--disable-error-code=import-untyped"]
additional_dependencies: [sqlalchemy2-stubs <= 0.0.2a20, SQLAlchemy <= 1.4]
additional_dependencies: [sqlalchemy2-stubs <= 0.0.2a38, SQLAlchemy < 1.5]
exclude: docs/conf.py
20 changes: 20 additions & 0 deletions buildingmotif/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import pyshacl # type: ignore
from rdflib import BNode, Graph, Literal, URIRef
from rdflib.compare import _TripleCanonicalizer
from rdflib.paths import ZeroOrOne
from rdflib.term import Node

Expand Down Expand Up @@ -690,3 +691,22 @@ def skolemize_shapes(g: Graph) -> Graph:
# apply the replacements
replace_nodes(g, replacements)
return g


def approximate_graph_hash(graph: Graph) -> int:
"""
Returns a cryptographic hash of the graph contents.
This uses the same method as rdflib's isomorphic function to generate a cryptographic hash of a given graph.
This can be used to calcualte graph isomorphism across time without needing to save the entire graph.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix spelling of "calcualte"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"This can be used to calcualte graph isomorphism across time without needing to save the entire graph."

I think really we mean "This method calculates a consistent hash of the canonicalized form of the graph. If the hashes of two graphs are equal, this means that the graphs are isomorphic. Generating the hashes (using this method) and caching them allows graph isomorphism to be determined without having to recalculate the canonical form of the graph, which can be expensive"


:param graph: graph to hash
:type graph: graph

:return: integer hash
:rtype: int
"""
# Copy graph to memory (improved performance if graph is backed by a DB store)
graph_prime = copy_graph(graph)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one question I have is if I should just create a new graph and add the db graph to it instead of calling copy graph, I think the former may be more performant and I'm not sure I need the blank node preservation in this usecase.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copying the graph is definitely slower than I want it to be. This is a property of the underlying graph store, so if you are adding a bunch of tuples and not doing some sort of copy-on-write thing then I'm not sure how much faster it could get

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the performance difference if you do this on a copy vs doing it on the original graph?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image These are the results I got with an in memory sqlite database. seems like your copy graph function is the fastest.

triple_canonicalizer = _TripleCanonicalizer(graph_prime)

return triple_canonicalizer.to_hash()
Loading
Loading