20
20
def uri_to_dot_id (uri ):
21
21
return str (hash (uri )).replace ("-" , "d" )
22
22
23
+ def make_table_popup_href (head_html ):
24
+ if head_html is None :
25
+ return ""
26
+
27
+ href = ""
28
+ if fstriplestore .triple_store .output_dir :
29
+ temp_dir = os .path .join (fstriplestore .triple_store .output_dir , "tmp" )
30
+ with tempfile .NamedTemporaryFile (dir = temp_dir , suffix = '.html' , delete = False ) as temp_fp :
31
+ popup_size = (800 , 200 )
32
+ temp_fp .write (head_html .toPython ().replace ("<" , "<" ).replace (">" , ">" ).replace (""" , "'" ).replace (" " , "\n " ).encode ('ascii' ))
33
+ href = f"""href="javascript:
34
+ /* alert(location.pathname.match(/.*\//) + '\n ' + '{ temp_fp .name } ' + '\n ' + '{ fstriplestore .triple_store .output_dir } '); */
35
+ {{ window.open(location.pathname.match(/.*\//) + 'tmp/' + '{ os .path .basename (temp_fp .name )} ', '_blank', 'width={ popup_size [0 ]} ,height={ popup_size [1 ]} '); }}
36
+ "
37
+ """
38
+
39
+ return href
40
+
41
+ def make_image_popup_href (image_b64 ):
42
+ href = ""
43
+ if fstriplestore .triple_store .output_dir :
44
+ temp_dir = os .path .join (fstriplestore .triple_store .output_dir , "tmp" )
45
+ with tempfile .NamedTemporaryFile (dir = temp_dir , suffix = '.html' , delete = False ) as temp_fp :
46
+ popup_size = (900 , 500 )
47
+ temp_fp .write (("<img src='data:image/png;base64," + image_b64 .toPython () + "'></img>" ).encode ('ascii' ))
48
+ href = f"""href="javascript:
49
+ /* alert(location.pathname.match(/.*\//) + '\n ' + '{ temp_fp .name } ' + '\n ' + '{ fstriplestore .triple_store .output_dir } '); */
50
+ {{ window.open(location.pathname.match(/.*\//) + 'tmp/' + '{ os .path .basename (temp_fp .name )} ', '_blank', 'width={ popup_size [0 ]} ,height={ popup_size [1 ]} '); }}
51
+ "
52
+ """
53
+
54
+ return href
55
+
56
+
23
57
def dump_subgraph (g , cc_uri , out_fd ):
24
58
subgraphs = [r for r in g .query ("select ?pp ?pl { ?pp rdf:type <CodeBlock>; rdf:label ?pl; <part-of> ?sg }" , base = fstriplestore .base_uri , initBindings = {'sg' : cc_uri })]
25
59
for subgraph , subgraph_label in subgraphs :
@@ -40,29 +74,66 @@ def dump_subgraph(g, cc_uri, out_fd):
40
74
"""
41
75
for obj_state , version , obj_type , obj_uudi in g .query (rq , base = fstriplestore .base_uri , initBindings = {'sg' : subgraph }):
42
76
obj_state_cc_rq = """
43
- select ?shape {
44
- ?obj_state_cc <obj-state> ?obj_state.
45
- ?obj_state_cc rdf:type <CCGlance> ;
46
- <shape > ?shape
77
+ select ?obj_state ?cc_type {
78
+ ?obj_state_cc rdf:type/rdfs:subClassOf+ <CC>;
79
+ rdf:type ?cc_type ;
80
+ <obj-state > ?obj_state.
47
81
}
48
82
"""
49
83
50
- node_bgcolor = "#88000022"
51
- for shape in g .query (obj_state_cc_rq , base = fstriplestore .base_uri , initBindings = {'obj_state' : obj_state }):
52
- #ipdb.set_trace()
53
- shape = shape [0 ]
54
- print (f"""
55
- node_{ uri_to_dot_id (obj_state )} [
56
- color="{ node_bgcolor } "
57
- shape = rect
58
- label = <<table border="0" cellborder="0" cellspacing="0" cellpadding="4">
59
- <tr> <td> <b>{ obj_state .split ('/' )[- 1 ]} </b><br/>{ obj_type } { shape .toPython ()} { version } </td> </tr>
60
- </table>>
61
- ];
84
+ ccs = pd .DataFrame (columns = ['obj_state_cc' , 'cc_type' ])
85
+ for obj_state_cc , cc_type in g .query (obj_state_cc_rq , base = fstriplestore .base_uri , initBindings = {'obj_state' : obj_state }):
86
+ ccs = pd .concat ([ccs , pd .DataFrame ([[obj_state_cc , cc_type ]], columns = ccs .columns )], axis = 0 , ignore_index = True )
62
87
63
- """ , file = out_fd )
88
+ cc_basic_plot_uri = rdflib .URIRef ('CCBasicPlot' , base = fstriplestore .base_uri )
89
+ cc_glance_uri = rdflib .URIRef ('CCGlance' , base = fstriplestore .base_uri )
90
+ target_cc_type = cc_basic_plot_uri if (ccs .cc_type == cc_basic_plot_uri ).any () else cc_glance_uri
91
+ bindings = {'obj_state' : obj_state , 'target_cc_type' : target_cc_type }
64
92
93
+ if target_cc_type == cc_glance_uri :
94
+ if obj_type .toPython () == "DataFrame" :
95
+ glance_rq = """
96
+ select ?shape ?head {
97
+ ?obj_state_cc <obj-state> ?obj_state; rdf:type ?target_cc_type; <shape> ?shape; <df-head> ?head .
98
+ }
99
+ """
100
+ for shape , head in g .query (glance_rq , base = fstriplestore .base_uri , initBindings = bindings ):
101
+ shape = shape
102
+ node_bgcolor = "#88000022"
103
+ href = make_table_popup_href (head )
104
+ elif obj_type .toPython () == "Series" :
105
+ glance_rq = "select ?shape { ?obj_state_cc <obj-state> ?obj_state; rdf:type ?target_cc_type; <shape> ?shape }"
106
+ for shape in g .query (glance_rq , base = fstriplestore .base_uri , initBindings = bindings ):
107
+ shape = shape [0 ]
108
+ node_bgcolor = "#88000022"
109
+ href = make_table_popup_href (None )
110
+ else :
111
+ raise Exception (f"unknown obj_type { obj_type } " )
112
+ elif target_cc_type == cc_basic_plot_uri :
113
+ basic_plot_rq = "select ?shape ?plot_im { ?obj_state_cc <obj-state> ?obj_state; rdf:type ?target_cc_type; <shape> ?shape; <plot-im> ?plot_im }"
114
+ #ipdb.set_trace()
115
+ for shape , plot_im in g .query (basic_plot_rq , base = fstriplestore .base_uri , initBindings = bindings ):
116
+ shape = shape
117
+ node_bgcolor = "#44056022"
118
+ href = make_image_popup_href (plot_im )
119
+ else :
120
+ raise Exception (f"unknown cc_type { target_cc_type } " )
121
+
122
+ print (f"""
123
+ node_{ uri_to_dot_id (obj_state )} [
124
+ color="{ node_bgcolor } "
125
+ shape = rect
126
+ label = <<table border="0" cellborder="0" cellspacing="0" cellpadding="4">
127
+ <tr> <td> <b>{ obj_state .split ('/' )[- 1 ]} </b><br/>{ obj_type } { shape .toPython ()} { version } </td> </tr>
128
+ </table>>
129
+ { href }
130
+ ];
131
+ """ , file = out_fd )
65
132
133
+ del shape
134
+ del href
135
+ del node_bgcolor
136
+
66
137
rq = """
67
138
select ?method_call_obj ?method_name ?method_display ?method_count ?method_stack_depth ?method_stack_trace {
68
139
?method_call_obj rdf:type <MethodCall>;
0 commit comments