Skip to content

Commit 5727881

Browse files
committed
add interactive graph api
1 parent 54188c1 commit 5727881

1 file changed

Lines changed: 18 additions & 22 deletions

File tree

src/pyirk/visualization.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from rdflib import Literal
99
import subprocess
1010
import re
11+
import time
1112

1213
import networkx as nx
1314
import nxv # for graphviz visualization of networkx graphs
@@ -632,15 +633,14 @@ def visualize_entity(uri: str, url_template="", write_tmp_files: Union[bool, str
632633
:return: svg_data as string
633634
"""
634635

635-
636636
big_G = create_complete_graph(url_template)
637637
try:
638638
node_of_interest = big_G._items[uri]
639639
except KeyError:
640640
msg = f"URI '{uri}' could not be found in the complete knowledge graph"
641641
raise p.InvalidURIError(msg)
642642

643-
small_G = nx.ego_graph(big_G, node_of_interest, radius, undirected=True)
643+
small_G = nx.ego_graph(big_G, node_of_interest, radius, undirected=True) #! perfomance of this operation sucks
644644
raw_dot_data = render_graph_to_dot(small_G)
645645

646646
dot_data0 = raw_dot_data
@@ -760,11 +760,11 @@ def save_data_to_file(mode, dot_data, svg_data):
760760
svg_fpath = "./tmp.svg"
761761
dot_fpath = "./tmp_dot.txt"
762762

763-
with open(dot_fpath, "w") as txtfile:
763+
with open(dot_fpath, "wt", encoding="utf-8") as txtfile:
764764
txtfile.write(dot_data)
765765
print("File written:", os.path.abspath(dot_fpath))
766766

767-
with open(svg_fpath, "w") as txtfile:
767+
with open(svg_fpath, "wt", encoding="utf-8") as txtfile:
768768
txtfile.write(svg_data)
769769
print("File written:", os.path.abspath(svg_fpath))
770770

@@ -776,32 +776,31 @@ def render_label(label: str):
776776

777777
return res.format(**REPLACEMENTS)
778778

779-
def create_interactive_graph(url_template="", output_dir="graph_site", radius=1):
779+
def create_interactive_graph(url_template="", output_dir="graph_site", radius=1, skip_auto_items=True, skip_existing=False):
780780
os.makedirs(output_dir, exist_ok=True)
781781

782782
G = create_complete_graph(url_template)
783783
print(f"Visualizing {len(G.nodes)} nodes and {len(G.edges)} edges.")
784784

785785
for node in G.nodes:
786786
node_name = node.short_key
787+
if skip_auto_items and "Ia" in node_name:
788+
continue
787789
print(node_name)
788790
dot_path = os.path.join(output_dir, f"{node_name}.dot")
791+
if skip_existing and os.path.isfile(dot_path):
792+
continue
789793
visualize_entity(node.uri, write_tmp_files=dot_path, radius=radius)
790794

791-
# tmp = "tmp_dot.txt"
792-
# shutil.move(tmp, dot_path)
793-
# img_path = os.path.join(output_dir, f"{node_name}.svg")
794-
# shutil.move("tmp.svg", img_path)
795-
796-
# create png and map
795+
# create map
797796
cmapx_path = os.path.join(output_dir, f"{node_name}.map")
798797
res2 = subprocess.run(["dot", "-Tcmapx", "-o", cmapx_path, dot_path])
799798
assert res2.returncode == 0, f"{res2.stderr}"
800799

801-
with open(cmapx_path, "r") as f:
800+
with open(cmapx_path, "r", encoding="utf-8") as f:
802801
image_map = f.read()
803802

804-
with open(os.path.join(output_dir, f"{node_name}.html"), "w") as f:
803+
with open(os.path.join(output_dir, f"{node_name}.html"), "w", encoding="utf-8") as f:
805804
f.write(f"""<!DOCTYPE html>
806805
<html>
807806
<head><title>Node {node_name}</title></head>
@@ -817,20 +816,16 @@ def create_interactive_graph(url_template="", output_dir="graph_site", radius=1)
817816
# Index page
818817
dot_path = os.path.join(output_dir, "index.dot")
819818
visualize_all_entities(write_tmp_files=dot_path)
820-
# tmp = "tmp_dot.txt"
821-
# shutil.move(tmp, dot_path)
822-
# img_path = os.path.join(output_dir, f"index.svg")
823-
# shutil.move("tmp.svg", img_path)
824819

825-
# create png and map
820+
# create map
826821
cmapx_path = os.path.join(output_dir, f"index.map")
827822
res2 = subprocess.run(["dot", "-Tcmapx", "-o", cmapx_path, dot_path])
828823
assert res2.returncode == 0, f"{res2.stderr}"
829824

830-
with open(cmapx_path, "r") as f:
825+
with open(cmapx_path, "r", encoding="utf-8") as f:
831826
image_map = f.read()
832827

833-
with open(os.path.join(output_dir, f"index.html"), "w") as f:
828+
with open(os.path.join(output_dir, f"index.html"), "w", encoding="utf-8") as f:
834829
f.write(f"""<!DOCTYPE html>
835830
<html>
836831
<head><title>Overview</title></head>
@@ -844,5 +839,6 @@ def create_interactive_graph(url_template="", output_dir="graph_site", radius=1)
844839

845840
if __name__ == "__main__":
846841
# visualize_all_entities(write_tmp_files=True)
847-
create_interactive_graph()
848-
# visualize_entity("irk:/builtins#I31", write_tmp_files=True, radius=0)
842+
# create_interactive_graph()
843+
# nl = p.irkloader.load_mod_from_path("output.py", "nl", "nonlinear")
844+
visualize_entity("irk:/builtins#I31", write_tmp_files=True, radius=1)

0 commit comments

Comments
 (0)