Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization for heatmap aggregation with pandas #1174

Merged
merged 3 commits into from Mar 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion holoviews/core/data/__init__.py
Expand Up @@ -518,7 +518,9 @@ def get_dimension_type(self, dim):

def dframe(self, dimensions=None):
"""
Returns the data in the form of a DataFrame.
Returns the data in the form of a DataFrame. Supplying a list
of dimensions filters the dataframe. If the data is already
a DataFrame a copy is returned.
"""
if dimensions:
dimensions = [self.get_dimension(d, strict=True).name for d in dimensions]
Expand Down
12 changes: 12 additions & 0 deletions holoviews/core/data/pandas.py
Expand Up @@ -222,6 +222,18 @@ def add_dimension(cls, columns, dimension, dim_pos, values, vdim):
return data


@classmethod
def as_dframe(cls, dataset):
"""
Returns the data of a Dataset as a dataframe avoiding copying
if it already a dataframe type.
"""
if issubclass(dataset.interface, PandasInterface):
return dataset.data
else:
return dataset.dframe()


@classmethod
def dframe(cls, columns, dimensions):
if dimensions:
Expand Down
18 changes: 15 additions & 3 deletions holoviews/element/util.py
Expand Up @@ -5,8 +5,14 @@

from ..core import Dataset, OrderedDict
from ..core.operation import ElementOperation
from ..core.util import (pd, is_nan, sort_topologically,
cartesian_product, is_cyclic, one_to_one)
from ..core.util import (is_nan, sort_topologically, one_to_one,
cartesian_product, is_cyclic)

try:
import pandas as pd
from ..core.data import PandasInterface
except:
pd = None

try:
import dask
Expand Down Expand Up @@ -134,7 +140,13 @@ def _aggregate_dataset(self, obj, xcoords, ycoords):
dtype = 'dataframe' if pd else 'dictionary'
dense_data = Dataset(data, kdims=obj.kdims, vdims=obj.vdims, datatype=[dtype])
concat_data = obj.interface.concatenate([dense_data, obj], datatype=[dtype])
agg = concat_data.reindex([xdim, ydim], vdims).aggregate([xdim, ydim], reduce_fn)
reindexed = concat_data.reindex([xdim, ydim], vdims)
if pd:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use reindexed.interface.dframe(dimensions=None, copy=False) instead of exposing the copy keyword argument at the element level? For copy=False to work, you are already assuming a dataframe type interface is being used...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the other thing you could do is complain if copy=False is passed to the dframe method of any interface that isn't based on dataframes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For copy=False to work, you are already assuming a dataframe type interface is being used...

Because then I need conditional branches for the "is already dataframe" and "convert to dataframe" paths again. I guess I agree copy is confusing because you might assume you can mutate the dataframe and have an effect on the original element if you don't make a copy, when the real point of it is to avoid making pointless copies.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be any harm with the dataframe interfaces just avoiding pointless copies automatically? Then it doesn't have to be something the user needs to ever think about...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my usage of dframe I often create it and then assign to it so that would be a bit of pain.

df = PandasInterface.as_dframe(reindexed)
df = df.groupby([xdim, ydim], sort=False).first().reset_index()
agg = reindexed.clone(df)
else:
agg = reindexed.aggregate([xdim, ydim], reduce_fn)

# Convert data to a gridded dataset
grid_data = {xdim: xcoords, ydim: ycoords}
Expand Down
11 changes: 3 additions & 8 deletions holoviews/operation/datashader.py
Expand Up @@ -20,23 +20,19 @@

from ..core import (ElementOperation, Element, Dimension, NdOverlay,
Overlay, CompositeOverlay, Dataset)
from ..core.data import ArrayInterface, PandasInterface, DaskInterface
from ..core.data import PandasInterface, DaskInterface
from ..core.util import get_param_values, basestring
from ..element import GridImage, Image, Path, Curve, Contours, RGB
from ..streams import RangeXY

DF_INTERFACES = [PandasInterface, DaskInterface]

@dispatch(Element)
def discover(dataset):
"""
Allows datashader to correctly discover the dtypes of the data
in a holoviews Element.
"""
if dataset.interface in DF_INTERFACES:
return dsdiscover(dataset.data)
else:
return dsdiscover(dataset.dframe())
return dsdiscover(PandasInterface.as_dframe(element))


@bypixel.pipeline.register(Element)
Expand Down Expand Up @@ -135,7 +131,6 @@ def get_agg_data(cls, obj, category=None):
kdims = obj.kdims
vdims = obj.vdims
x, y = obj.dimensions(label=True)[:2]
is_df = lambda x: isinstance(x, Dataset) and x.interface in DF_INTERFACES
if isinstance(obj, Path):
glyph = 'line'
for p in obj.data:
Expand All @@ -146,7 +141,7 @@ def get_agg_data(cls, obj, category=None):
elif isinstance(obj, CompositeOverlay):
for key, el in obj.data.items():
x, y, element, glyph = cls.get_agg_data(el)
df = element.data if is_df(element) else element.dframe()
df = PandasInterface.as_dframe(element)
if isinstance(obj, NdOverlay):
df = df.assign(**dict(zip(obj.dimensions('key', True), key)))
paths.append(df)
Expand Down