-
Notifications
You must be signed in to change notification settings - Fork 7
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
Changes from 4 commits
cf9b9cb
a10e5bc
ac1bda3
67cb019
3b9158c
9c9d436
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
triple_canonicalizer = _TripleCanonicalizer(graph_prime) | ||
|
||
return triple_canonicalizer.to_hash() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix spelling of "calcualte"