|
1 |
| -#import ipdb |
2 | 1 | import textwrap
|
3 | 2 | import pandas as pd
|
4 |
| -import io, base64 |
| 3 | +import io |
| 4 | +import base64 |
5 | 5 |
|
6 | 6 | from . import fstriplestore
|
7 | 7 | from . import obj_tracking
|
8 | 8 | from . import wb_stack
|
9 | 9 |
|
10 | 10 | random_id = 0
|
| 11 | + |
| 12 | + |
11 | 13 | def dump_obj_state(obj):
|
12 | 14 | caller_stack_entry = wb_stack.wb_stack.get_parent_of_current_entry()
|
13 |
| - stack_entry = wb_stack.wb_stack.get_top() |
| 15 | + stack_entry = wb_stack.wb_stack.get_top() |
14 | 16 | t_obj, obj_found = obj_tracking.tracking_store.get_tracking_obj(obj)
|
15 | 17 |
|
16 | 18 | global random_id
|
17 |
| - obj_state_uri = f"<ObjState#{random_id}>"; random_id += 1 |
18 |
| - fstriplestore.triple_store.dump_triple(obj_state_uri, "rdf:type", "<ObjState>") |
| 19 | + obj_state_uri = f"<ObjState#{random_id}>" |
| 20 | + random_id += 1 |
| 21 | + fstriplestore.triple_store.dump_triple( |
| 22 | + obj_state_uri, "rdf:type", "<ObjState>" |
| 23 | + ) |
19 | 24 | fstriplestore.triple_store.dump_triple(obj_state_uri, "<obj>", t_obj.uri)
|
20 |
| - fstriplestore.triple_store.dump_triple(obj_state_uri, "<part-of>", caller_stack_entry.uri) |
21 |
| - fstriplestore.triple_store.dump_triple(obj_state_uri, "<version>", f'"{t_obj.last_version_num}"') |
| 25 | + fstriplestore.triple_store.dump_triple( |
| 26 | + obj_state_uri, "<part-of>", caller_stack_entry.uri |
| 27 | + ) |
| 28 | + fstriplestore.triple_store.dump_triple( |
| 29 | + obj_state_uri, "<version>", f'"{t_obj.last_version_num}"' |
| 30 | + ) |
22 | 31 | t_obj.last_obj_state_uri = obj_state_uri
|
23 | 32 | t_obj.last_version_num += 1
|
24 | 33 |
|
25 |
| - dump_obj_state_cc(obj_state_uri, obj, output_type = 'head') |
| 34 | + dump_obj_state_cc(obj_state_uri, obj, output_type="head") |
26 | 35 |
|
27 | 36 | return t_obj
|
28 |
| - |
29 |
| -def dump_obj_state_cc(obj_state_uri, obj, output_type = 'head'): |
| 37 | + |
| 38 | + |
| 39 | +def dump_obj_state_cc(obj_state_uri, obj, output_type="head"): |
30 | 40 | if isinstance(obj, pd.DataFrame):
|
31 | 41 | dump_DataFrame_obj_state_cc(obj_state_uri, obj, output_type)
|
32 | 42 | elif isinstance(obj, pd.Series):
|
33 | 43 | dump_Series_obj_state_cc(obj_state_uri, obj, output_type)
|
34 | 44 | else:
|
35 | 45 | raise Exception(f"unknown obj type at {obj_state_uri}")
|
36 | 46 |
|
| 47 | + |
37 | 48 | def dump_DataFrame_obj_state_cc(obj_state_uri, df, output_type):
|
38 | 49 | ts = fstriplestore.triple_store
|
39 | 50 | global random_id
|
40 |
| - obj_state_cc_uri = f"<ObjStateCC#{random_id}>"; random_id += 1 |
| 51 | + obj_state_cc_uri = f"<ObjStateCC#{random_id}>" |
| 52 | + random_id += 1 |
41 | 53 |
|
42 | 54 | ts.dump_triple(obj_state_cc_uri, "rdf:type", "<ObjStateCC>")
|
43 | 55 | ts.dump_triple(obj_state_cc_uri, "<obj-state>", obj_state_uri)
|
44 | 56 |
|
45 |
| - if output_type == 'head': |
| 57 | + if output_type == "head": |
46 | 58 | ts.dump_triple(obj_state_cc_uri, "rdf:type", "<CCGlance>")
|
47 | 59 | ts.dump_triple(obj_state_cc_uri, "<shape>", f'"{df.shape}"')
|
48 |
| - df_head_html = df.head(10).applymap(lambda x: textwrap.shorten(str(x), 50)).to_html().replace("<", "<").replace(">", ">").replace('"', """).replace("\n", " ") |
| 60 | + df_head_html = ( |
| 61 | + df.head(10) |
| 62 | + .applymap(lambda x: textwrap.shorten(str(x), 50)) |
| 63 | + .to_html() |
| 64 | + .replace("<", "<") |
| 65 | + .replace(">", ">") |
| 66 | + .replace('"', """) |
| 67 | + .replace("\n", " ") |
| 68 | + ) |
49 | 69 | ts.dump_triple(obj_state_cc_uri, "<df-head>", '"' + df_head_html + '"')
|
50 |
| - elif output_type == 'plot': |
| 70 | + elif output_type == "plot": |
51 | 71 | ts.dump_triple(obj_state_cc_uri, "rdf:type", "<CCBasicPlot>")
|
52 | 72 | ts.dump_triple(obj_state_cc_uri, "<shape>", f'"{df.shape}"')
|
53 | 73 | out_fd = io.BytesIO()
|
54 | 74 | fig = df.plot().get_figure()
|
55 | 75 | fig.savefig(out_fd)
|
56 |
| - #ipdb.set_trace() |
57 |
| - im_s = base64.b64encode(out_fd.getvalue()).decode('ascii') |
58 |
| - ts.dump_triple(obj_state_cc_uri, '<plot-im>', '"' + im_s + '"') |
| 76 | + # ipdb.set_trace() |
| 77 | + im_s = base64.b64encode(out_fd.getvalue()).decode("ascii") |
| 78 | + ts.dump_triple(obj_state_cc_uri, "<plot-im>", '"' + im_s + '"') |
59 | 79 | else:
|
60 | 80 | raise Exception(f"unknown output_type: {output_type}")
|
61 | 81 |
|
62 | 82 |
|
63 | 83 | def dump_Series_obj_state_cc(obj_state_uri, s, output_type):
|
64 | 84 | ts = fstriplestore.triple_store
|
65 | 85 | global random_id
|
66 |
| - obj_state_cc_uri = f"<ObjStateCC#{random_id}>"; random_id += 1 |
| 86 | + obj_state_cc_uri = f"<ObjStateCC#{random_id}>" |
| 87 | + random_id += 1 |
67 | 88 |
|
68 | 89 | ts.dump_triple(obj_state_cc_uri, "rdf:type", "<ObjStateCC>")
|
69 | 90 | ts.dump_triple(obj_state_cc_uri, "<obj-state>", obj_state_uri)
|
70 | 91 |
|
71 |
| - if output_type == 'head': |
| 92 | + if output_type == "head": |
72 | 93 | ts.dump_triple(obj_state_cc_uri, "rdf:type", "<CCGlance>")
|
73 | 94 | ts.dump_triple(obj_state_cc_uri, "<shape>", f"{len(s)}")
|
74 | 95 | ts.dump_triple(obj_state_cc_uri, "<df-head>", '"NONE"')
|
75 |
| - elif output_type == 'plot': |
76 |
| - ts.dump_triple(obj_state_cc_uri, "rdf:type", "<CCBasicPlot>") |
| 96 | + elif output_type == "plot": |
| 97 | + ts.dump_triple(obj_state_cc_uri, "rdf:type", "<CCBasicPlot>") |
77 | 98 | ts.dump_triple(obj_state_cc_uri, "<shape>", f"{len(s)}")
|
78 | 99 | out_fd = io.BytesIO()
|
79 | 100 | fig = s.plot().get_figure()
|
80 | 101 | fig.savefig(out_fd)
|
81 |
| - #ipdb.set_trace() |
82 |
| - im_s = base64.b64encode(out_fd.getvalue()).decode('ascii') |
83 |
| - ts.dump_triple(obj_state_cc_uri, '<plot-im>', '"' + im_s + '"') |
| 102 | + # ipdb.set_trace() |
| 103 | + im_s = base64.b64encode(out_fd.getvalue()).decode("ascii") |
| 104 | + ts.dump_triple(obj_state_cc_uri, "<plot-im>", '"' + im_s + '"') |
84 | 105 | else:
|
85 | 106 | raise Exception(f"unknown output_type: {output_type}")
|
86 |
| - |
|
0 commit comments