Skip to content

Commit

Permalink
Made dimension lookups more strict
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Jan 26, 2017
1 parent 076b460 commit 2770e78
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 47 deletions.
22 changes: 13 additions & 9 deletions holoviews/core/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def __call__(self, new_type, kdims=None, vdims=None, groupby=None,
groupby = [groupby]

selected = self._element.reindex(groupby+kdims, vdims)
params = {'kdims': [selected.get_dimension(kd) for kd in kdims],
'vdims': [selected.get_dimension(vd) for vd in vdims],
params = {'kdims': [selected.get_dimension(kd, strict=True) for kd in kdims],
'vdims': [selected.get_dimension(vd, strict=True) for vd in vdims],
'label': selected.label}
if selected.group != selected.params()['group'].default:
params['group'] = selected.group
Expand Down Expand Up @@ -215,7 +215,9 @@ def range(self, dim, data_range=True):
object.
"""
dim = self.get_dimension(dim)
if None not in dim.range:
if dimension is None:
return (None, None)
elif None not in dim.range:
return dim.range
elif dim in self.dimensions() and data_range:
if len(self):
Expand Down Expand Up @@ -291,12 +293,12 @@ def reindex(self, kdims=None, vdims=None):
if kdims is None:
key_dims = [d for d in self.kdims if not vdims or d not in vdims]
else:
key_dims = [self.get_dimension(k) for k in kdims]
key_dims = [self.get_dimension(k, strict=True) for k in kdims]

if vdims is None:
val_dims = [d for d in self.vdims if not kdims or d not in kdims]
else:
val_dims = [self.get_dimension(v) for v in vdims]
val_dims = [self.get_dimension(v, strict=True) for v in vdims]

data = self.interface.reindex(self, key_dims, val_dims)
return self.clone(data, kdims=key_dims, vdims=val_dims)
Expand Down Expand Up @@ -378,7 +380,7 @@ def aggregate(self, dimensions=None, function=None, spreadfn=None, **kwargs):
raise ValueError("The aggregate method requires a function to be specified")
if dimensions is None: dimensions = self.kdims
elif not isinstance(dimensions, list): dimensions = [dimensions]
kdims = [self.get_dimension(d) for d in dimensions]
kdims = [self.get_dimension(d, strict=True) for d in dimensions]
aggregated = self.interface.aggregate(self, kdims, function, **kwargs)
aggregated = self.interface.unpack_scalar(self, aggregated)

Expand Down Expand Up @@ -508,13 +510,15 @@ def dframe(self, dimensions=None):
Returns the data in the form of a DataFrame.
"""
if dimensions:
dimensions = [self.get_dimension(d).name for d in dimensions]
dimensions = [self.get_dimension(d, strict=True).name for d in dimensions]
return self.interface.dframe(self, dimensions)


def columns(self, dimensions=None):
if dimensions is None: dimensions = self.dimensions()
dimensions = [self.get_dimension(d) for d in dimensions]
if dimensions is None:
dimensions = self.dimensions()
else:
dimensions = [self.get_dimension(d, strict=True) for d in dimensions]
return {d.name: self.dimension_values(d) for d in dimensions}


Expand Down
2 changes: 1 addition & 1 deletion holoviews/core/data/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def groupby(cls, dataset, dimensions, container_type, group_type, **kwargs):
data = dataset.data

# Get dimension objects, labels, indexes and data
dimensions = [dataset.get_dimension(d) for d in dimensions]
dimensions = [dataset.get_dimension(d, strict=True) for d in dimensions]
dim_idxs = [dataset.get_dimension_index(d) for d in dimensions]
ndims = len(dimensions)
kdims = [kdim for kdim in dataset.kdims
Expand Down
4 changes: 2 additions & 2 deletions holoviews/core/data/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DictInterface(Interface):

@classmethod
def dimension_type(cls, dataset, dim):
name = dataset.get_dimension(dim).name
name = dataset.get_dimension(dim, strict=True).name
return dataset.data[name].dtype.type

@classmethod
Expand Down Expand Up @@ -235,7 +235,7 @@ def sample(cls, dataset, samples=[]):

@classmethod
def aggregate(cls, dataset, kdims, function, **kwargs):
kdims = [dataset.get_dimension(d).name for d in kdims]
kdims = [dataset.get_dimension(d, strict=True).name for d in kdims]
vdims = dataset.dimensions('value', label='name')
groups = cls.groupby(dataset, kdims, list, OrderedDict)
aggregated = OrderedDict([(k, []) for k in kdims+vdims])
Expand Down
6 changes: 3 additions & 3 deletions holoviews/core/data/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def coords(cls, dataset, dim, ordered=False, expanded=False):
coordinates are in ascending order and expanded creates
ND-array matching the dimensionality of the dataset.
"""
dim = dataset.get_dimension(dim)
dim = dataset.get_dimension(dim, strict=True)
if expanded:
return util.expand_grid_coords(dataset, dim)
data = dataset.data[dim.name]
Expand Down Expand Up @@ -162,7 +162,7 @@ def canonicalize(cls, dataset, data, coord_dims=None):

@classmethod
def values(cls, dataset, dim, expanded=True, flat=True):
dim = dataset.get_dimension(dim)
dim = dataset.get_dimension(dim, strict=True)
if dim in dataset.vdims:
data = dataset.data.get(dim.name)
data = cls.canonicalize(dataset, data)
Expand All @@ -177,7 +177,7 @@ def values(cls, dataset, dim, expanded=True, flat=True):
@classmethod
def groupby(cls, dataset, dim_names, container_type, group_type, **kwargs):
# Get dimensions information
dimensions = [dataset.get_dimension(d) for d in dim_names]
dimensions = [dataset.get_dimension(d, strict=True) for d in dim_names]
kdims = [kdim for kdim in dataset.kdims if kdim not in dimensions]

# Update the kwargs appropriately for Element group types
Expand Down
10 changes: 5 additions & 5 deletions holoviews/core/data/iris.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def validate(cls, dataset):

@classmethod
def coords(cls, dataset, dim, ordered=False, expanded=False):
dim = dataset.get_dimension(dim)
dim = dataset.get_dimension(dim, strict=True)
if expanded:
return util.expand_grid_coords(dataset, dim.name)
data = dataset.data.coords(dim.name)[0].points
Expand All @@ -135,7 +135,7 @@ def values(cls, dataset, dim, expanded=True, flat=True):
"""
Returns an array of the values along the supplied dimension.
"""
dim = dataset.get_dimension(dim)
dim = dataset.get_dimension(dim, strict=True)
if dim in dataset.vdims:
coord_names = [c.name() for c in dataset.data.dim_coords]
data = dataset.data.copy().data
Expand Down Expand Up @@ -174,7 +174,7 @@ def groupby(cls, dataset, dims, container_type=HoloMap, group_type=None, **kwarg
break up a high-dimensional dataset into smaller viewable chunks.
"""
if not isinstance(dims, list): dims = [dims]
dims = [dataset.get_dimension(d) for d in dims]
dims = [dataset.get_dimension(d, strict=True) for d in dims]
constraints = [d.name for d in dims]
slice_dims = [d for d in dataset.kdims if d not in dims]

Expand All @@ -199,7 +199,7 @@ def range(cls, dataset, dimension):
"""
Computes the range along a particular dimension.
"""
dim = dataset.get_dimension(dimension)
dim = dataset.get_dimension(dimension, strict=True)
values = dataset.dimension_values(dim.name, False)
return (np.nanmin(values), np.nanmax(values))

Expand Down Expand Up @@ -273,7 +273,7 @@ def select_to_constraint(cls, dataset, selection):
constraint = (constraint.start, constraint.stop)
if isinstance(constraint, tuple):
constraint = iris.util.between(*constraint, rh_inclusive=False)
dim = dataset.get_dimension(dim)
dim = dataset.get_dimension(dim, strict=True)
constraint_kwargs[dim.name] = constraint
return iris.Constraint(**constraint_kwargs)

Expand Down
13 changes: 7 additions & 6 deletions holoviews/core/data/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PandasInterface(Interface):

@classmethod
def dimension_type(cls, columns, dim):
name = columns.get_dimension(dim).name
name = columns.get_dimension(dim, strict=True).name
idx = list(columns.data.columns).index(name)
return columns.data.dtypes[idx].type

Expand Down Expand Up @@ -91,7 +91,7 @@ def validate(cls, dataset):

@classmethod
def range(cls, columns, dimension):
column = columns.data[columns.get_dimension(dimension).name]
column = columns.data[columns.get_dimension(dimension, strict=True).name]
if column.dtype.kind == 'O':
if (not isinstance(columns.data, pd.DataFrame) or
LooseVersion(pd.__version__) < '0.17.0'):
Expand All @@ -111,7 +111,7 @@ def concat(cls, columns_objs):

@classmethod
def groupby(cls, columns, dimensions, container_type, group_type, **kwargs):
index_dims = [columns.get_dimension(d) for d in dimensions]
index_dims = [columns.get_dimension(d, strict=True) for d in dimensions]
element_dims = [kdim for kdim in columns.kdims
if kdim not in index_dims]

Expand Down Expand Up @@ -172,7 +172,7 @@ def redim(cls, dataset, dimensions):
@classmethod
def sort(cls, columns, by=[]):
import pandas as pd
cols = [columns.get_dimension(d).name for d in by]
cols = [columns.get_dimension(d, strict=True).name for d in by]

if (not isinstance(columns.data, pd.DataFrame) or
LooseVersion(pd.__version__) < '0.17.0'):
Expand All @@ -194,7 +194,7 @@ def select(cls, columns, selection_mask=None, **selection):

@classmethod
def values(cls, columns, dim, expanded=True, flat=True):
dim = columns.get_dimension(dim)
dim = columns.get_dimension(dim, strict=True)
data = columns.data[dim.name]
if not expanded:
return data.unique()
Expand Down Expand Up @@ -225,7 +225,8 @@ def add_dimension(cls, columns, dimension, dim_pos, values, vdim):
@classmethod
def dframe(cls, columns, dimensions):
if dimensions:
dimensions = [columns.get_dimension(d).name for d in dimensions]
dimensions = [columns.get_dimension(d, strict=True).name
for d in dimensions]
return columns.reindex(dimensions).data.copy()
else:
return columns.data.copy()
Expand Down
19 changes: 10 additions & 9 deletions holoviews/core/data/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class XArrayInterface(GridInterface):

@classmethod
def dimension_type(cls, dataset, dim):
name = dataset.get_dimension(dim).name
name = dataset.get_dimension(dim, strict=True).name
return dataset.data[name].dtype.type


@classmethod
def dtype(cls, dataset, dim):
name = dataset.get_dimension(dim).name
name = dataset.get_dimension(dim, strict=True).name
return dataset.data[name].dtype


Expand Down Expand Up @@ -82,7 +82,7 @@ def init(cls, eltype, data, kdims, vdims):

@classmethod
def range(cls, dataset, dimension):
dim = dataset.get_dimension(dimension).name
dim = dataset.get_dimension(dimension, strict=True).name
if dim in dataset.data:
data = dataset.data[dim]
dmin, dmax = data.min().data, data.max().data
Expand All @@ -95,7 +95,7 @@ def range(cls, dataset, dimension):

@classmethod
def groupby(cls, dataset, dimensions, container_type, group_type, **kwargs):
index_dims = [dataset.get_dimension(d) for d in dimensions]
index_dims = [dataset.get_dimension(d, strict=True) for d in dimensions]
element_dims = [kdim for kdim in dataset.kdims
if kdim not in index_dims]

Expand Down Expand Up @@ -128,7 +128,7 @@ def groupby(cls, dataset, dimensions, container_type, group_type, **kwargs):

@classmethod
def coords(cls, dataset, dim, ordered=False, expanded=False):
dim = dataset.get_dimension(dim).name
dim = dataset.get_dimension(dim, strict=True).name
if expanded:
return util.expand_grid_coords(dataset, dim)
data = np.atleast_1d(dataset.data[dim].data)
Expand All @@ -139,7 +139,7 @@ def coords(cls, dataset, dim, ordered=False, expanded=False):

@classmethod
def values(cls, dataset, dim, expanded=True, flat=True):
dim = dataset.get_dimension(dim)
dim = dataset.get_dimension(dim, strict=True)
data = dataset.data[dim.name].data
if dim in dataset.vdims:
coord_dims = dataset.data[dim.name].dims
Expand All @@ -160,7 +160,7 @@ def aggregate(cls, dataset, dimensions, function, **kwargs):
elif not dimensions:
return dataset.data.apply(function)
else:
dim = dataset.get_dimension(dimensions[0])
dim = dataset.get_dimension(dimensions[0], strict=True)
return dataset.data.groupby(dim.name).apply(function)


Expand Down Expand Up @@ -200,7 +200,7 @@ def sort(cls, dataset, by=[]):
def select(cls, dataset, selection_mask=None, **selection):
validated = {}
for k, v in selection.items():
dim = dataset.get_dimension(k).name
dim = dataset.get_dimension(k, strict=True).name
if isinstance(v, slice):
v = (v.start, v.stop)
if isinstance(v, set):
Expand Down Expand Up @@ -233,7 +233,8 @@ def length(cls, dataset):

@classmethod
def dframe(cls, dataset, dimensions):
dimensions = [dataset.get_dimension(d).name for d in dimensions]
dimensions = [dataset.get_dimension(d, strict=True).name
for d in dimensions]
if dimensions:
return dataset.reindex(columns=dimensions)
else:
Expand Down
8 changes: 4 additions & 4 deletions holoviews/core/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def _reduce_map(self, dimensions, function, reduce_map):
reduce_map = [(d, function) for d in dimensions]
elif not reduce_map:
reduce_map = [(d, function) for d in self.kdims]
reduced = [(self.get_dimension(d).name, fn)
reduced = [(self.get_dimension(d, strict=True).name, fn)
for d, fn in reduce_map]
grouped = [(fn, [dim for dim, _ in grp]) for fn, grp in groupby(reduced, lambda x: x[1])]
return grouped[0]
Expand Down Expand Up @@ -175,7 +175,7 @@ def mapping(self, kdims=None, vdims=None, **kwargs):

def array(self, dimensions=[]):
if dimensions:
dims = [self.get_dimension(d) for d in dimensions]
dims = [self.get_dimension(d, strict=True) for d in dimensions]
else:
dims = [d for d in self.kdims + self.vdims if d != 'Index']
columns, types = [], []
Expand Down Expand Up @@ -319,8 +319,8 @@ def reindex(self, kdims=None, vdims=None, force=False):
elif kdims is None:
kdims = [d for d in self.dimensions() if d not in vdims]
if 'Index' not in kdims: kdims = ['Index'] + kdims
key_dims = [self.get_dimension(k) for k in kdims]
val_dims = [self.get_dimension(v) for v in vdims]
key_dims = [self.get_dimension(k, strict=True) for k in kdims]
val_dims = [self.get_dimension(v, strict=True) for v in vdims]

kidxs = [(i, k in self.kdims, self.get_dimension_index(k))
for i, k in enumerate(kdims)]
Expand Down
2 changes: 1 addition & 1 deletion holoviews/core/ndmapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def groupby(self, dimensions, container_type=None, group_type=None, **kwargs):
return self
container_type = container_type if container_type else type(self)
group_type = group_type if group_type else type(self)
dimensions = [self.get_dimension(d) for d in dimensions]
dimensions = [self.get_dimension(d, strict=True) for d in dimensions]
sort = not self._sorted
with item_check(False):
return util.ndmapping_groupby(self, dimensions, container_type,
Expand Down
8 changes: 5 additions & 3 deletions holoviews/core/spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,9 +999,11 @@ def layout(self, dimensions=None, **kwargs):
def overlay(self, dimensions=None, **kwargs):
if dimensions is None:
dimensions = self.kdims
if not isinstance(dimensions, (list, tuple)):
dimensions = [dimensions]
dimensions = [self.get_dimension(d) for d in dimensions]
else:
if not isinstance(dimensions, (list, tuple)):
dimensions = [dimensions]
dimensions = [self.get_dimension(d, strict=True)
for d in dimensions]
dims = [d for d in self.kdims if d not in dimensions]
return self.groupby(dims, group_type=NdOverlay)

Expand Down
2 changes: 1 addition & 1 deletion holoviews/element/chart3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, data, extents=None, **params):


def range(self, dim, data_range=True):
dim_idx = dim if isinstance(dim, int) else self.get_dimension_index(dim)
dim_idx = self.get_dimension_index(dim)
if dim_idx in [0, 1]:
l, b, r, t = self.bounds.lbrt()
if dim_idx == 0:
Expand Down
6 changes: 3 additions & 3 deletions holoviews/element/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,12 @@ def _coord2matrix(self, coord):
for i in [1, 0])


def range(self, dimension):
def range(self, dimension, data_range=True):
idx = self.get_dimension_index(dimension)
if idx in [0, 1]:
if data_range and idx in [0, 1]:
data = self.data[idx]
return np.min(data), np.max(data)
elif idx == 2:
elif data_range and idx == 2:
data = self.data[idx]
return np.nanmin(data), np.nanmax(data)
super(QuadMesh, self).range(dimension)
Expand Down

0 comments on commit 2770e78

Please sign in to comment.