Skip to content

Commit 4962781

Browse files
committed
added CC rdf hierachy definitions, fixed CC output for supported cases
1 parent 46cdf1a commit 4962781

File tree

4 files changed

+196
-27
lines changed

4 files changed

+196
-27
lines changed

examples/notebooks/conditional-join-w-cmp.ipynb

Lines changed: 96 additions & 8 deletions
Large diffs are not rendered by default.

pyjviz/fstriplestore.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ def dump_prefixes__(self):
2525
<MethodCall> rdfs:subClassOf <WithBlock> .
2626
<NestedCall> rdf:type rdfs:Class .
2727
<NestedCall> rdfs:subClassOf <WithBlock> .
28+
29+
<Obj> rdf:type rdfs:Class .
30+
<ObjState> rdf:type rdfs:Class .
31+
<ObjStateCC> rdf:type rdfs:Class .
32+
33+
<CC> rdf:type rdfs:Class .
34+
<CCGlance> rdf:type rdfs:Class .
35+
<CCGlance> rdfs:subClassOf <CC> .
36+
<CCBasicPlot> rdf:type rdfs:Class .
37+
<CCBasicPlot> rdfs:subClassOf <CC> .
2838
"""), file = self.out_fd)
2939

3040
def dump_triple(self, subj, pred, obj):

pyjviz/obj_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def dump_DataFrame_obj_state_cc(obj_state_uri, df, output_type):
4949
ts.dump_triple(obj_state_cc_uri, "<df-head>", '"' + df_head_html + '"')
5050
elif output_type == 'plot':
5151
ts.dump_triple(obj_state_cc_uri, "rdf:type", "<CCBasicPlot>")
52-
ts.dump_triple(obj_state_cc_uri, "<df-shape>", f'"{df.shape}"')
52+
ts.dump_triple(obj_state_cc_uri, "<shape>", f'"{df.shape}"')
5353
out_fd = io.BytesIO()
5454
fig = df.plot().get_figure()
5555
fig.savefig(out_fd)
@@ -73,7 +73,7 @@ def dump_Series_obj_state_cc(obj_state_uri, s, output_type):
7373
ts.dump_triple(obj_state_cc_uri, "<shape>", f"{len(s)}")
7474
elif output_type == 'plot':
7575
ts.dump_triple(obj_state_cc_uri, "rdf:type", "<CCBasicPlot>")
76-
ts.dump_triple(obj_state_cc_uri, "<s-size>", f"{len(s)}")
76+
ts.dump_triple(obj_state_cc_uri, "<shape>", f"{len(s)}")
7777
out_fd = io.BytesIO()
7878
fig = s.plot().get_figure()
7979
fig.savefig(out_fd)

pyjviz/viz.py

Lines changed: 88 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,40 @@
2020
def uri_to_dot_id(uri):
2121
return str(hash(uri)).replace("-", "d")
2222

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("&lt;", "<").replace("&gt;", ">").replace("&quot;", "'").replace("&#10;", "\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+
2357
def dump_subgraph(g, cc_uri, out_fd):
2458
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})]
2559
for subgraph, subgraph_label in subgraphs:
@@ -40,29 +74,66 @@ def dump_subgraph(g, cc_uri, out_fd):
4074
"""
4175
for obj_state, version, obj_type, obj_uudi in g.query(rq, base = fstriplestore.base_uri, initBindings = {'sg': subgraph}):
4276
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.
4781
}
4882
"""
4983

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)
6287

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}
6492

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)
65132

133+
del shape
134+
del href
135+
del node_bgcolor
136+
66137
rq = """
67138
select ?method_call_obj ?method_name ?method_display ?method_count ?method_stack_depth ?method_stack_trace {
68139
?method_call_obj rdf:type <MethodCall>;

0 commit comments

Comments
 (0)