Skip to content

Commit

Permalink
adding a touch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
xtianpoli committed Jul 1, 2021
1 parent a814023 commit 283fe75
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 48 deletions.
50 changes: 12 additions & 38 deletions notebooks/VegaHeatmap.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,10 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "downtown-toronto",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0620ef3a615c47b89511634a5bab0624",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(VegaWidget(), IntProgress(value=0, description='progress')))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"import numpy as np\n",
"import time\n",
Expand Down Expand Up @@ -69,7 +54,8 @@
" # widget.update('data', remove='datum.x>=0')\n",
" #print(\"displayed\", widget._displayed)\n",
" widget._displayed = True\n",
" widget.update_dataframe('data', source, remove=\"true\")\n",
" widget.update_dataframe('data', source, remove=\"true\", \n",
" touch_mode=True, touch=True)\n",
"\n",
"await progressive_loop(widget)"
]
Expand All @@ -84,25 +70,12 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"id": "393a3fc4",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2c3a03a90b554107bb871868609e1e02",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(VegaWidget(), IntProgress(value=0, description='progress'), Text(value='', description='Mean ti…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"import time\n",
"import numpy as np\n",
Expand Down Expand Up @@ -158,7 +131,8 @@
" value = hist\n",
" widget._displayed = True\n",
" start = time.time()\n",
" widget.update_array2d('data', value, ['x', 'y', 'z'], remove=\"true\")\n",
" widget.update_array2d('data', value, ['x', 'y', 'z'], remove=\"true\", \n",
" touch_mode=True, touch=True)\n",
" times.append(time.time() - start)\n",
" mean.value = f\"{np.mean(times)*1000:.3f}ms\"\n",
" std.value = f\"{np.std(times)*1000:.3f}ms\"\n",
Expand Down Expand Up @@ -191,7 +165,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
"version": "3.9.5"
}
},
"nbformat": 4,
Expand Down
3 changes: 2 additions & 1 deletion vega/dataframes/numpy_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ def __init__(self, source, *args, **kw):
assert source is None or isinstance(
source, dict
) # TODO: check values are ndarrays
source = {k:v.copy() for (k, v) in source.items()}
super().__init__(source, *args, **kw)
if not self._touch_mode:
self._source = {k:v.copy() for (k, v) in self._source.items()}

@property
def columns(self):
Expand Down
19 changes: 18 additions & 1 deletion vega/dataframes/source_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@


class SourceAdapter(metaclass=ABCMeta):
def __init__(self, source, columns=None, compression=None):
def __init__(self, source, columns=None, compression=None, touch_mode=False):
self._source = source
self._columns = None
self._compression = compression
self._touched = False
self._touch_mode = touch_mode

@property
def is_touched(self):
ret = self._touched
self._touched = False
return ret

def touch(self):
assert self._touch_mode
self._touched = True

def _equals_no_touch_mode(self, other):
if self._touch_mode or other._touch_mode:
return True
return self.equals(other)

@abstractproperty
def columns(self):
Expand Down
7 changes: 4 additions & 3 deletions vega/dataframes/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ def set(self, obj, value):
old_value = obj._trait_values.get(self.name, self.default_value)
obj._trait_values[self.name] = new_value
if (
(old_value is None and new_value is not None)
or (old_value is Undefined and new_value is not Undefined)
or not old_value.equals(new_value)
(old_value is None and new_value is not None)
or (old_value is Undefined and new_value is not Undefined)
or (old_value.is_touched or new_value.is_touched)
or not old_value._equals_no_touch_mode(new_value)
):
obj._notify_trait(self.name, old_value, new_value)

Expand Down
16 changes: 11 additions & 5 deletions vega/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ def update(self, key, remove=None, insert=None):
else:
self._pending_updates.append(update)

def update_dataframe(self, key, df, remove=None):
def update_dataframe(self, key, df, remove=None,
touch_mode=False, touch=False):
"""Update the chart data with a DataFrame.
Updates are only reflected on the client, i.e., after re-displaying
Expand All @@ -177,17 +178,20 @@ def update_dataframe(self, key, df, remove=None):
property ``t < 5``.
"""
if isinstance(df, pd.DataFrame):
self._df = PandasAdapter(df)
self._df = PandasAdapter(df, touch_mode=touch_mode)
else:
assert isinstance(df, SourceAdapter)
self._df = df
if touch:
self._df.touch()
update = dict(key=key)
if remove is not None:
update['remove'] = remove
update['insert'] = "@dataframe"
self.send(dict(type="update", updates=[update]))

def update_array2d(self, key, arr, columns, remove=None):
def update_array2d(self, key, arr, columns, remove=None,
touch_mode=False, touch=False):
"""Update the chart data with a 2d array and their column names.
Updates are only reflected on the client, i.e., after re-displaying
Expand Down Expand Up @@ -217,10 +221,12 @@ def update_array2d(self, key, arr, columns, remove=None):
"""
if isinstance(arr, np.ndarray):
fancy_col = ','.join(columns)
self._df = NumpyAdapter({fancy_col: arr})
self._df = NumpyAdapter({fancy_col: arr}, touch_mode=touch_mode)
else:
assert isinstance(arr, SourceAdapter)
self._df = arr
self._df = arr
if touch:
self._df.touch()
update = dict(key=key)
if remove is not None:
update['remove'] = remove
Expand Down

0 comments on commit 283fe75

Please sign in to comment.