-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #333 from nanglo123/add_dkg_viz
Add visualization for DKG relations
- Loading branch information
Showing
4 changed files
with
317 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import networkx as nx | ||
|
||
|
||
def draw_relations(records, fname, is_full=False): | ||
"""Draw a graph of some DKG records queried from the /relations endpoint.""" | ||
|
||
graph = nx.DiGraph() | ||
graph.graph["rankdir"] = "LR" | ||
|
||
for relation in records: | ||
if is_full: | ||
subject_curie = relation['subject']['id'] | ||
subject_name = relation['subject']['name'] | ||
object_curie = relation['object']['id'] | ||
object_name = relation['object']['name'] | ||
predicate_name = relation['predicate'].get('type') | ||
predicate_curie = relation['predicate']['pred'] | ||
|
||
subject_node = f"{subject_name} ({subject_curie})" | ||
predicate_edge = f"{predicate_name} ({predicate_curie})" \ | ||
if predicate_name else predicate_curie | ||
object_node = f"{object_name} ({object_curie})" | ||
|
||
graph.add_edge(subject_node, object_node, label=predicate_edge, | ||
color="red", weight=2) | ||
else: | ||
graph.add_edge(relation['subject'], relation['object'], | ||
label=relation['predicate'], | ||
color="red", weight=2) | ||
agraph = nx.nx_agraph.to_agraph(graph) | ||
agraph.draw(path=fname, prog="dot", format="png") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters