Skip to content

Commit

Permalink
fix: fixing datetime bursh + ds gen
Browse files Browse the repository at this point in the history
  • Loading branch information
kirangadhave committed Oct 22, 2023
1 parent da3ddbe commit 202496d
Show file tree
Hide file tree
Showing 11 changed files with 22,361 additions and 142,136 deletions.
17,336 changes: 43 additions & 17,293 deletions examples/notebooks/avalanche/avalance.ipynb

Large diffs are not rendered by default.

146,675 changes: 21,920 additions & 124,755 deletions examples/notebooks/avalanche/avalanche_task.ipynb

Large diffs are not rendered by default.

378 changes: 313 additions & 65 deletions examples/test_ext_widget.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "persist_ext",
"version": "0.1.0",
"version": "1.5.0-rc0",
"description": "PersIst is a JupyterLab extension to enable persistent interactive visualizations in JupyterLab notebooks.",
"keywords": [
"jupyter",
Expand Down
4 changes: 2 additions & 2 deletions persist_ext/internals/utils/dt_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def create_equal_query_for_timeunit(column_name, unix_ts, timeunits):
if len(q) > 0:
q += " & "

q += f"@pd.to_datetime({column_name}).dt.{unit} == @pd.to_datetime({unix_ts}, unit='ms').{unit}"
q += f"@pd.to_datetime(`{column_name}`).dt.{unit} == @pd.to_datetime({unix_ts}, unit='ms').{unit}"
return f"({q})"


Expand All @@ -79,5 +79,5 @@ def create_range_query_for_timeunit(column_name, unix_ts, timeunits):
q += " & "
lower = min(unix_ts)
upper = max(unix_ts)
q += f"@pd.to_datetime({lower}, unit='ms').{unit} <= @pd.to_datetime({column_name}).dt.{unit} <= @pd.to_datetime({upper}, unit='ms').{unit}"
q += f"@pd.to_datetime({lower}, unit='ms').{unit} <= @pd.to_datetime(`{column_name}`).dt.{unit} <= @pd.to_datetime({upper}, unit='ms').{unit}"
return f"({q})"
49 changes: 40 additions & 9 deletions persist_ext/internals/vis/barchart.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def barchart(
data,
x,
y,
encoding=["x"],
orientation="vertical",
color=None,
interaction=True,
selection_type="interval",
Expand All @@ -32,7 +32,7 @@ def barchart(
Returns:
altair chart object
"""
chart = base_altair_plot(data, height=height, width=width)
chart, data = base_altair_plot(data, height=height, width=width)

chart = chart.mark_bar()

Expand All @@ -44,20 +44,51 @@ def barchart(
if not interaction:
return chart

barchart_non_agg_axis = "x"

if orientation == "horizontal":
barchart_non_agg_axis = "y"

encodings = [barchart_non_agg_axis]

x_encode = chart.encoding.x.to_dict()
y_encode = chart.encoding.y.to_dict()

is_binned = False
is_time_unit = False

if barchart_non_agg_axis == "x":
is_binned = "bin" in x_encode
is_time_unit = "timeUnit" in x_encode
elif barchart_non_agg_axis == "y":
is_time_unit = "timeUnit" in y_encode
is_binned = "bin" in y_encode

is_binned_or_time_unit = is_binned or is_time_unit

selection = None
if is_binned and selection_type == "point":
print("Point selections for binned axis not implemented")

if selection_type == "point":
selection = alt.selection_point(name="selector", encodings=encoding)
selection = alt.selection_point(name="selector", encodings=encodings)
else:
selection = alt.selection_interval(name="selector", encodings=encoding)

chart = chart.add_params(selection)
selection = alt.selection_interval(
name="selector", encodings=encodings, views=["base_chart"]
)

if color:
chart = chart.encode(color=alt.condition(selection, color, alt.value("gray")))
if is_binned_or_time_unit:
filtered_layer = chart.transform_filter(selection)
chart.name = "base_chart"
chart = chart.encode(color=alt.value("gray"), opacity=alt.value(0.3))
chart = chart.add_params(selection)
chart = chart + filtered_layer
else:
chart = chart.add_params(selection)
chart = chart.encode(
color=alt.condition(selection, alt.value("steelblue"), alt.value("gray"))
)

return OutputWithTrrackWidget(body_widget=VegaLiteChartWidget(chart), data=data)
return OutputWithTrrackWidget(
body_widget=VegaLiteChartWidget(chart, data=data), data=data
)
2 changes: 0 additions & 2 deletions persist_ext/internals/widgets/base/body_widget_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ def _apply_interactions(self, interactions, *copied_var_tuple):
# Update the cache with interaction id in thread
def __update(id, vars_to_copy):
self._cached_apply_record[id] = vars_to_copy
print("Fin", id)

print("Start", id)
thread = Thread(
target=__update, args=(id, self._to_cache(*copied_var_tuple))
)
Expand Down
33 changes: 29 additions & 4 deletions persist_ext/internals/widgets/vegalite_chart/selection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy

import pandas as pd
import traitlets
from altair import Undefined

Expand Down Expand Up @@ -93,13 +94,19 @@ def query(self, direction="in"):
if len(q) > 0:
q += " & "

if not isinstance(value, int) or not isinstance(value, float):
_v = value
dt = pd.to_datetime(_v)
value = dt.timestamp() * 1000

if has_timeunit_parts(col):
timeunit_str = extract_timeunit_parts(col)
col = strip_timeunit_parts(col)
timeunits = get_time_unit_parts(timeunit_str)

q += create_range_query_for_timeunit(col, value, timeunits)
elif len(value) == 2:
q += f"{min(value)} <= {col} <= {max(value)}"
q += f"{min(value)} <= `{col}` <= {max(value)}"
else:
raise ValueError(f"Unhandled selection shape: {val}")
# if len(q) > 0:
Expand All @@ -117,26 +124,44 @@ def query(self, direction="in"):
if len(sub_q) > 0:
sub_q += " & "
timeunit_str = None

if not isinstance(value, int) and not isinstance(value, float):
print("?", value)
_v = value
dt = pd.to_datetime(_v)
value = dt.timestamp() * 1000

if has_timeunit_parts(col):
timeunit_str = extract_timeunit_parts(col)
col = strip_timeunit_parts(col)
timeunits = get_time_unit_parts(timeunit_str)
sub_q += create_equal_query_for_timeunit(col, value, timeunits)
else:
sub_q += f"{col} == {repr(value)}"
sub_q += f"`{col}` == {value}"
q += f"({sub_q})"

# print(q)
print(q)
return f"~({q})" if direction == "out" else q


def extract_point_value(value):
if not value:
return None

value = copy.deepcopy(value)

if "vlPoint" in value:
if "or" in value["vlPoint"]:
return value["vlPoint"]["or"]
val = [*value["vlPoint"]["or"]]

for i, _ in enumerate(val):
for k, v in _.items():
if has_timeunit_parts(k):
if not isinstance(v, int) and not isinstance(v, float):
_[k] = pd.to_datetime(v).timestamp() * 1000
val[i] = _

return val

return None

Expand Down
13 changes: 11 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ dependencies = [
"anywidget",
"lzstring",
"traittypes==0.2.1",
"pyarrow"
"pyarrow",
"scikit-learn",
"paretoset",
"vega-datasets"
]
dynamic = ["version", "description", "authors", "urls", "keywords"]

Expand All @@ -43,13 +46,19 @@ source = "nodejs"
fields = ["description", "authors", "urls"]

[tool.hatch.build.targets.sdist]
artifacts = ["persist_ext/labextension"]
artifacts = ["persist_ext/labextension", "persist_ext/static"]
exclude = [".github", "binder"]

[tool.hatch.build.targets.wheel.shared-data]
"persist_ext/labextension" = "share/jupyter/labextensions/persist_ext"
"install.json" = "share/jupyter/labextensions/persist_ext/install.json"

[tool.hatch.build.targets.wheel.force-include]
"persist_ext/static" = "persist_ext/static"

[tool.hatch.build]
artifacts=["persist_ext/static", "/static", "static"]

[tool.hatch.build.hooks.version]
path = "persist_ext/_version.py"

Expand Down
3 changes: 2 additions & 1 deletion src/utils/stripImmutableClone.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ImmutableObject } from '@hookstate/core';
import { parseStringify } from './jsonHelpers';

export type NoImmutable<T> = T extends ImmutableObject<infer R>
? R
Expand All @@ -7,7 +8,7 @@ export type NoImmutable<T> = T extends ImmutableObject<infer R>
: never;

export function stripImmutableClone<T>(ob: T): NoImmutable<T> {
return structuredClone(ob) as any;
return parseStringify(ob) as any;
}

export function stripImmutableCloneJSON<T>(ob: T): NoImmutable<T> {
Expand Down
2 changes: 0 additions & 2 deletions src/widgets/header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ function Header({ cell }: Props) {
const [hasSelections] = useModelState('df_has_selections');
const [dfBeingGenerated] = useModelState<string | null>('df_being_generated');

console.log({ dfBeingGenerated });

return (
<Group
sx={{
Expand Down

0 comments on commit 202496d

Please sign in to comment.