@@ -445,6 +445,7 @@ def create_complete_graph(
445445 url_template = "" ,
446446 limit : Optional [int ] = None ,
447447 skip_auto_items : bool = False ,
448+ vis_relations : bool = False ,
448449 ) -> nx .DiGraph :
449450 """
450451 :param url_template: template to insert links based on uris
@@ -466,8 +467,14 @@ def create_complete_graph(
466467 # this is the case for some statements which are subject of a qualifier relation
467468 assert item_uri in p .ds .statement_uri_map
468469 continue
469- if not isinstance ( item , p . Item ) or item .short_key in ["I000" ]:
470+ if item .short_key in ["I000" ]:
470471 continue
472+ if vis_relations :
473+ if (not isinstance (item , p .Item ) and not isinstance (item , p .Relation )):
474+ continue # what possible type could item have to get here?
475+ else :
476+ if not isinstance (item , p .Item ):
477+ continue
471478 if skip_auto_items and "Ia" in item .short_key :
472479 continue
473480 # count only items
@@ -643,7 +650,7 @@ def svg_replace(self, raw_svg_data: str, REPLACEMENTS: dict) -> str:
643650 return svg_data1
644651
645652
646- def visualize_entity (self , uri : str , url_template = "" , write_tmp_files : Union [bool , str ] = False , radius = 1 , graph = None ) -> str :
653+ def visualize_entity (self , uri : str , url_template = "" , write_tmp_files : Union [bool , str ] = False , radius = 1 , graph = None , vis_relations = False ) -> str :
647654 """
648655
649656 :param uri: entity uri (like "irk:/my/module#I0123")
@@ -653,7 +660,7 @@ def visualize_entity(self, uri: str, url_template="", write_tmp_files: Union[boo
653660 :return: svg_data as string
654661 """
655662 if graph is None :
656- big_G = self .create_complete_graph (url_template )
663+ big_G = self .create_complete_graph (url_template , vis_relations = vis_relations )
657664 else :
658665 big_G = graph
659666 try :
@@ -696,7 +703,7 @@ def visualize_entity(self, uri: str, url_template="", write_tmp_files: Union[boo
696703 # for interactive graph, we need hyperlinks. these mess up the svg with <> inside attributes -> remove
697704 svg_data1 = re .sub (r'(?<=xlink:title=").+?(?=" target=)' , "" , svg_data1 )
698705
699- if 1 :
706+ if 0 :
700707 # add legend
701708 svg_data1 = self .add_legend (svg_data1 , relation_color_map , list_of_relations )
702709
@@ -778,7 +785,7 @@ def get_label(self, entity):
778785 return res
779786
780787
781- def visualize_all_entities (self , url_template = "" , write_tmp_files : Union [bool , str ] = False , skip_auto_items : bool = False ) -> str :
788+ def visualize_all_entities (self , url_template = "" , write_tmp_files : Union [bool , str ] = False , skip_auto_items : bool = False , vis_relations = False ) -> str :
782789 """visualize all entities loaded in datastore. output svg graph.
783790
784791 Args:
@@ -789,7 +796,7 @@ def visualize_all_entities(self, url_template="", write_tmp_files: Union[bool, s
789796 Returns:
790797 str: svg graph
791798 """
792- G = self .create_complete_graph (url_template , skip_auto_items = skip_auto_items )
799+ G = self .create_complete_graph (url_template , skip_auto_items = skip_auto_items , vis_relations = vis_relations )
793800
794801 print (f"Visualizing { len (G .nodes )} nodes and { len (G .edges )} edges." )
795802 ecm = self .build_edge_color_map (G )
@@ -883,10 +890,10 @@ def render_label(self, label: str):
883890
884891 return res .format (** REPLACEMENTS )
885892
886- def create_interactive_graph (self , url_template = "" , output_dir = "graph_site" , radius = 1 , skip_auto_items = True , skip_existing = False ):
893+ def create_interactive_graph (self , url_template = "" , output_dir = "graph_site" , radius = 1 , skip_auto_items = True , skip_existing = False , vis_relations = False ):
887894 os .makedirs (output_dir , exist_ok = True )
888895
889- G = self .create_complete_graph (url_template , skip_auto_items = skip_auto_items )
896+ G = self .create_complete_graph (url_template , skip_auto_items = skip_auto_items , vis_relations = vis_relations )
890897 print (f"Visualizing { len (G .nodes )} nodes and { len (G .edges )} edges." )
891898
892899 for node in G .nodes :
@@ -914,9 +921,17 @@ def create_interactive_graph(self, url_template="", output_dir="graph_site", rad
914921 # to trick graphviz to render rect and then replace href and title to create tooltip
915922 image_map = re .sub (r'(?<=shape="rect")(.+?)(href=")(.+?)(" title=")(.+?)(?=")' , lambda mo : mo .group (1 )+ mo .group (2 )+ "" + mo .group (4 )+ mo .group (3 ), image_map )
916923
917- desc = p .ds .items [node .uri ].R2 .value if p .ds .items [node .uri ].R2 else ""
924+ if node_name .startswith ("I" ):
925+ item = p .ds .items [node .uri ]
926+ else :
927+ item = p .ds .relations [node .uri ]
928+
929+ desc = item .R2 .value if item .R2 else ""
930+ # add usage hint, sometimes more expressive than R2
931+ desc += "<br>" + " " .join (item .R18 ) if item .R18 else ""
932+
918933 context = {
919- "title" : node_name + " " + p . ds . items [ node . uri ] .R1 .value ,
934+ "title" : node_name + " " + item .R1 .value ,
920935 "img_source" : f"{ node_name } .svg" ,
921936 "map" : image_map ,
922937 "desc" : desc
@@ -927,7 +942,7 @@ def create_interactive_graph(self, url_template="", output_dir="graph_site", rad
927942
928943 # Index page
929944 dot_path = os .path .join (output_dir , "index.dot" )
930- self .visualize_all_entities (write_tmp_files = dot_path , skip_auto_items = skip_auto_items )
945+ self .visualize_all_entities (write_tmp_files = dot_path , skip_auto_items = skip_auto_items , vis_relations = vis_relations )
931946
932947 # create map
933948 cmapx_path = os .path .join (output_dir , f"index.map" )
@@ -961,6 +976,6 @@ def create_interactive_graph(self, url_template="", output_dir="graph_site", rad
961976
962977if __name__ == "__main__" :
963978 # visualize_all_entities(write_tmp_files=True, skip_auto_items=True)
964- vm .create_interactive_graph ()
979+ vm .create_interactive_graph (vis_relations = True )
965980 # nl = p.irkloader.load_mod_from_path("output.py", "nl", "nonlinear")
966981 # visualize_entity("irk:/builtins#I31", write_tmp_files=True, radius=1)
0 commit comments