Skip to content

Commit

Permalink
Add debug and talk
Browse files Browse the repository at this point in the history
  • Loading branch information
saulshanabrook committed Oct 14, 2020
1 parent e4d04cd commit 75b4c5e
Show file tree
Hide file tree
Showing 9 changed files with 3,716 additions and 128 deletions.
3,772 changes: 3,654 additions & 118 deletions examples/charting-example.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ibis_vega_transform/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .altair_renderer import altair_renderer
from .compiler import compiler_target_function
from .core import apply
from .globals import _expr_map, set_fallback
from .globals import _expr_map, set_fallback, enable_debug, disable_debug
from .query import query_target_func


Expand Down
6 changes: 4 additions & 2 deletions ibis_vega_transform/altair_data_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
get_active_span,
get_fallback,
set_active_span,
debug,
)
from .tracer import tracer

Expand All @@ -33,8 +34,9 @@ def altair_data_transformer(data):
if not get_active_span():
set_active_span(tracer.start_span("altair", tags={tags.SERVICE: "kernel"}))

get_active_span().log_kv({"sql:initial": expr.compile()})

sql = expr.compile()
get_active_span().log_kv({"sql:initial": sql})
debug("sql:initial", {"sql": sql})
h = str(hash(expr))
name = f"{DATA_NAME_PREFIX}{h}"
_expr_map[h] = expr
Expand Down
2 changes: 2 additions & 0 deletions ibis_vega_transform/altair_monkeypatch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ibis_vega_transform.globals import reset_debug
import warnings

import altair
Expand All @@ -21,6 +22,7 @@ def updated_chart_init(self, data=None, *args, **kwargs):
those types and set the `ibis` attribute to the original ibis expression.
"""
if data is not None and isinstance(data, ibis.Expr):
reset_debug()
expr = data
data = empty_dataframe(expr)
data.ibis = expr
Expand Down
3 changes: 2 additions & 1 deletion ibis_vega_transform/altair_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import opentracing
import typing

from .globals import get_active_span, get_fallback, set_active_span
from .globals import get_active_span, get_fallback, set_active_span, debug
from .tracer import tracer

__all__ = ["altair_renderer"]
Expand All @@ -24,6 +24,7 @@ def altair_renderer(spec):
injected_span: typing.Dict = {}
tracer.inject(active_span, opentracing.Format.TEXT_MAP, injected_span)
active_span.log_kv({"vega-lite:initial": spec})
debug("vega-lite:initial", spec)
active_span.finish()
set_active_span(None)

Expand Down
4 changes: 3 additions & 1 deletion ibis_vega_transform/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import opentracing

from .globals import DATA_NAME_PREFIX, _expr_map
from .globals import DATA_NAME_PREFIX, _expr_map, debug
from .tracer import tracer

__all__ = ["compiler_target_function"]
Expand Down Expand Up @@ -44,11 +44,13 @@ def compiler_target_function(comm, msg):

with tracer.start_span("compile-vega", references=root_ref) as span:
span.log_kv({"vega-spec:initial": spec})
debug("vega-spec:initial", spec)

try:
with tracer.start_span("transform-vega", child_of=span) as transform_span:
updated_spec = _transform(spec, root_span)
transform_span.log_kv({"vega-spec:transformed": updated_spec})
span.log_kv({"vega-spec:transformed": updated_spec})

comm.send(updated_spec)
except ValueError as e:
Expand Down
36 changes: 36 additions & 0 deletions ibis_vega_transform/globals.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import typing
from IPython.core.display import JSON
import ibis
from numpy.lib.function_base import disp
import opentracing
import IPython.display

__all__ = [
"_expr_map",
Expand All @@ -9,8 +12,13 @@
"set_fallback",
"get_active_span",
"set_active_span",
"enable_debug",
"disable_debug",
"reset_debug",
"debug",
]


_expr_map: typing.Dict[str, ibis.Expr] = {}

DATA_NAME_PREFIX = "ibis:"
Expand Down Expand Up @@ -40,3 +48,31 @@ def set_active_span(scan: typing.Optional[opentracing.Span]) -> None:

def get_active_span() -> typing.Optional[opentracing.Span]:
return active_span


DEBUG = False

DISPLAY = None
JSON_DISPLAY = None


def reset_debug():
global JSON_DISPLAY, DISPLAY
JSON_DISPLAY = IPython.display.JSON({}, root="ibis-vega-transform")
DISPLAY = IPython.display.display(JSON_DISPLAY, display_id=True)


def enable_debug():
global DEBUG
DEBUG = True


def disable_debug():
global DEBUG
DEBUG = False


def debug(key: str, value):
if DEBUG:
JSON_DISPLAY.data[key] = value
DISPLAY.update(JSON_DISPLAY)
16 changes: 12 additions & 4 deletions ibis_vega_transform/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import opentracing

from .core import apply
from .globals import _expr_map
from .globals import _expr_map, debug
from .tracer import tracer

__all__ = ["query_target_func"]
Expand Down Expand Up @@ -84,7 +84,12 @@ def execute_query(parameters: dict):
if name not in _expr_map:
raise ValueError(f"{name} is not an expression known to us!")
expr = _expr_map[name]
scope.span.log_kv({"sql:initial": expr.compile()})
sql = expr.compile()
scope.span.log_kv({"sql:initial": sql})
debug(
"query:initial",
{"transforms": transforms, "parameters": parameters, "sql": sql},
)
if transforms:
# Replace all string instances of data references with value in schema
for k, v in parameters.items():
Expand All @@ -106,12 +111,15 @@ def execute_query(parameters: dict):
f"Failed to convert {transforms} with error message message '{e}'"
)
with tracer.start_span("ibis:execute") as execute_span:
execute_span.log_kv({"sql": expr.compile()})
sql = expr.compile()
execute_span.log_kv({"sql": sql})
if ENABLE_MULTIPROCESSING:
data = execute_new_client(expr)
else:
data = expr.execute()
return altair.to_values(data)["values"]
values = altair.to_values(data)["values"]
debug("query:result", {"transforms": transforms, "sql": sql, "values": values})
return values


def _patch_vegaexpr(expr: str, name: str, value: str) -> str:
Expand Down
3 changes: 2 additions & 1 deletion src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ export class IbisVegaRenderer extends Widget implements IRenderMime.IRenderer {
const res = await vegaEmbed(this.node, vSpec, {
actions: true,
defaultStyle: true,
mode: 'vega'
mode: 'vega',

});
this._view = res.view;

Expand Down

0 comments on commit 75b4c5e

Please sign in to comment.