Skip to content

Commit

Permalink
Made dimension_values unique argument supported throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Jan 25, 2016
1 parent 1352f60 commit ae9a808
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 28 deletions.
9 changes: 3 additions & 6 deletions holoviews/core/data.py
Expand Up @@ -27,7 +27,7 @@
from .ndmapping import NdMapping, item_check, sorted_context
from .spaces import HoloMap
from . import util
from .util import wrap_tuple, basestring
from .util import wrap_tuple, basestring, unique_array


class Columns(Element):
Expand Down Expand Up @@ -343,11 +343,8 @@ def dimension_values(self, dim, unique=False):
"""
dim = self.get_dimension(dim).name
dim_vals = self.interface.values(self, dim)
if unique and pd:
return pd.unique(dim_vals)
elif unique:
_, uniq_inds = np.unique(dim_vals, return_index=True)
return dim_vals[np.sort(uniq_inds)]
if unique:
return unique_array(dim_vals)
else:
return dim_vals

Expand Down
2 changes: 1 addition & 1 deletion holoviews/core/dimension.py
Expand Up @@ -736,7 +736,7 @@ def select(self, selection_specs=None, **kwargs):
return selection


def dimension_values(self, dimension):
def dimension_values(self, dimension, unique=False):
"""
Returns the values along the specified dimension. This method
must be implemented for all Dimensioned type.
Expand Down
10 changes: 6 additions & 4 deletions holoviews/core/element.py
Expand Up @@ -10,7 +10,8 @@
from .overlay import Overlayable, NdOverlay, CompositeOverlay
from .spaces import HoloMap, GridSpace
from .tree import AttrTree
from .util import dimension_sort, get_param_values, dimension_sanitizer
from .util import (dimension_sort, get_param_values, dimension_sanitizer,
unique_array)


class Element(ViewableElement, Composable, Overlayable):
Expand Down Expand Up @@ -481,14 +482,15 @@ def aggregate(self, dimensions, function, **kwargs):
return self.clone(rows, kdims=grouped.kdims)


def dimension_values(self, dim):
def dimension_values(self, dim, unique=False):
dim = self.get_dimension(dim)
value_dims = self.dimensions('value', label=True)
if dim.name in value_dims:
index = value_dims.index(dim.name)
return np.array([v[index] for v in self.data.values()])
vals = np.array([v[index] for v in self.data.values()])
return unique_array(vals) if unique else vals
else:
return NdMapping.dimension_values(self, dim.name)
return NdMapping.dimension_values(self, dim.name, unique)


def values(self):
Expand Down
17 changes: 8 additions & 9 deletions holoviews/core/layout.py
Expand Up @@ -15,7 +15,8 @@
from .dimension import Dimension, Dimensioned, ViewableElement
from .ndmapping import OrderedDict, NdMapping, UniformNdMapping
from .tree import AttrTree
from .util import int_to_roman, sanitize_identifier, group_sanitizer, label_sanitizer
from .util import (int_to_roman, sanitize_identifier, group_sanitizer,
label_sanitizer, unique_array)
from . import traversal


Expand Down Expand Up @@ -125,12 +126,9 @@ def get(self, key, default=None):
return self.data[key] if key in self.data else default


def dimension_values(self, dimension):
def dimension_values(self, dimension, unique=False):
dimension = self.get_dimension(dimension).name
if dimension in self.kdims:
return self.layout_order[:len(self.data)]
else:
return self.main.dimension_values(dimension)
return self.main.dimension_values(dimension, unique)


def __getitem__(self, key):
Expand Down Expand Up @@ -433,16 +431,17 @@ def clone(self, *args, **overrides):
return clone


def dimension_values(self, dimension):
def dimension_values(self, dimension, unique=False):
"Returns the values along the specified dimension."
dimension = self.get_dimension(dimension).name
all_dims = self.traverse(lambda x: [d.name for d in x.dimensions()])
if dimension in chain.from_iterable(all_dims):
values = [el.dimension_values(dimension) for el in self
if dimension in el.dimensions(label=True)]
return np.concatenate(values)
vals = np.concatenate(values)
return unique_array(vals) if unique else vals
else:
return super(Layout, self).dimension_values(dimension)
return super(Layout, self).dimension_values(dimension, unique)


def cols(self, ncols):
Expand Down
7 changes: 4 additions & 3 deletions holoviews/core/ndmapping.py
Expand Up @@ -343,17 +343,18 @@ def drop_dimension(self, dimensions):
kdims=dims)


def dimension_values(self, dimension):
def dimension_values(self, dimension, unique=False):
"Returns the values along the specified dimension."
dimension = self.get_dimension(dimension).name
if dimension in self.kdims:
return np.array([k[self.get_dimension_index(dimension)] for k in self.data.keys()])
if dimension in self.dimensions(label=True):
values = [el.dimension_values(dimension) for el in self
if dimension in el.dimensions()]
return np.concatenate(values)
vals = np.concatenate(values)
return unique_array(vals) if unique else vals
else:
return super(MultiDimensionalMapping, self).dimension_values(dimension)
return super(MultiDimensionalMapping, self).dimension_values(dimension, unique)


def reindex(self, kdims=[], force=False):
Expand Down
10 changes: 6 additions & 4 deletions holoviews/core/overlay.py
Expand Up @@ -65,18 +65,20 @@ def hist(self, index=0, adjoin=True, dimension=None, **kwargs):
return layout


def dimension_values(self, dimension):
def dimension_values(self, dimension, unique=False):
values = []
found = False
for el in self:
if dimension in el.dimensions(label=True):
values.append(el.dimension_values(dimension))
found = True
if not found:
return super(CompositeOverlay, self).dimension_values(dimension)
return super(CompositeOverlay, self).dimension_values(dimension, unique)
values = [v for v in values if v is not None and len(v)]
return np.concatenate(values) if len(values) else np.array()

if not values:
return np.array()
vals = np.concatenate(values)
return unique_array(vals) if unique else vals


class Overlay(Layout, CompositeOverlay):
Expand Down
2 changes: 1 addition & 1 deletion holoviews/core/spaces.py
Expand Up @@ -261,7 +261,7 @@ def sample(self, samples=[], bounds=None, **sample_values):
raise NotImplementedError("Regular sampling not implemented "
"for high-dimensional Views.")

samples = list(util.unique_iterator(self.last.closest(linsamples)))
samples = util.unique_array(self.last.closest(linsamples))

sampled = self.clone([(k, view.sample(samples, **sample_values))
for k, view in self.data.items()])
Expand Down
13 changes: 13 additions & 0 deletions holoviews/core/util.py
Expand Up @@ -470,6 +470,17 @@ def unique_iterator(seq):
yield item


def unique_array(arr):
"""
Returns an array of unique values in the input order
"""
if pd:
return pd.unique(arr)
else:
_, uniq_inds = np.unique(arr, return_index=True)
return arr[np.sort(uniq_inds)]


def match_spec(element, specification):
"""
Matches the group.label specification of the supplied
Expand Down Expand Up @@ -829,3 +840,5 @@ def groupby_python(self_or_cls, ndmapping, dimensions, container_type,
else [((), (v,))]), **kwargs))
for k, v in iterative_select(ndmapping, dim_names, selects)]
return container_type(groups, kdims=dimensions)


0 comments on commit ae9a808

Please sign in to comment.