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

Iris interface handles standard constructors enabling unit tests #709

Merged
merged 4 commits into from
Jun 9, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions holoviews/core/data/iris.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,18 @@ def init(cls, eltype, data, kdims, vdims):
kdim_names = [kd.name for kd in eltype.kdims]

if not isinstance(data, iris.cube.Cube):
ndims = len(kdim_names)
kdims = [kd if isinstance(kd, Dimension) else Dimension(kd)
for kd in kdims]
vdim = vdims[0].name if isinstance(vdims[0], Dimension) else vdims[0]
if isinstance(data, tuple):
coords = [iris.coords.DimCoord(vals, long_name=kd)
for kd, vals in zip(kdim_names, data)]
value_array = data[-1]
vdim = vdims[0].name if isinstance(vdims[0], Dimension) else vdims[0]
data = {d: vals for d, vals in zip(kdim_names + [vdim], data)}
elif isinstance(data, dict):
vdim = vdims[0].name if isinstance(vdims[0], Dimension) else vdims[0]
coords = [iris.coords.DimCoord(vals, long_name=kd)
for kd, vals in data.items() if kd in kdims]
value_array = data[vdim]
coords = [(iris.coords.DimCoord(data[kd.name], long_name=kd.name,
units=kd.unit), ndims-n-1)
for n, kd in enumerate(kdims)]
try:
data = iris.cube.Cube(value_array, long_name=vdim,
dim_coords_and_dims=coords)
Expand Down Expand Up @@ -222,6 +224,25 @@ def aggregate(cls, columns, kdims, function, **kwargs):
raise NotImplementedError


@classmethod
def sample(cls, dataset, samples=[]):
"""
Sampling currently not implemented.
"""
raise NotImplementedError


@classmethod
def add_dimension(cls, columns, dimension, dim_pos, values, vdim):
"""
Adding value dimensions not currently supported by iris interface.
Adding key dimensions not possible on dense interfaces.
"""
if not vdim:
raise Exception("Cannot add key dimension to a dense representation.")
raise NotImplementedError


@classmethod
def select_to_constraint(cls, selection):
"""
Expand All @@ -232,7 +253,7 @@ def select_to_constraint(cls, selection):
if isinstance(constraint, slice):
constraint = (constraint.start, constraint.stop)
if isinstance(constraint, tuple):
constraint = iris.util.between(*constraint)
constraint = iris.util.between(*constraint, rh_inclusive=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, this must be related to the bounds issue I noticed in the tests (see below).

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, so far we've used non-inclusive upper bounds in holoviews, so this just makes it consistent. Might be worth having the discussion which is more appropriate, since iris and xarray both seem to use inclusive bounds.

constraint_kwargs[dim] = constraint
return iris.Constraint(**constraint_kwargs)

Expand All @@ -244,8 +265,9 @@ def select(cls, dataset, selection_mask=None, **selection):
"""
constraint = cls.select_to_constraint(selection)
pre_dim_coords = [c.name() for c in dataset.data.dim_coords]
indexed = cls.indexed(dataset, selection)
extracted = dataset.data.extract(constraint)
if not extracted.dim_coords:
if indexed and not extracted.dim_coords:
return extracted.data.item()
post_dim_coords = [c.name() for c in extracted.dim_coords]
dropped = [c for c in pre_dim_coords if c not in post_dim_coords]
Expand Down
35 changes: 34 additions & 1 deletion tests/testdataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def test_dataset_double_zip_init(self):

class GridDatasetTest(HomogeneousColumnTypes, ComparisonTestCase):
"""
Test of the NdDataset interface (mostly for backwards compatibility)
Test of the Grid array interface
"""

def setUp(self):
Expand Down Expand Up @@ -480,3 +480,36 @@ def test_dataset_sort_vdim_hm(self):
def test_dataset_groupby(self):
self.assertEqual(self.dataset_hm.groupby('x').keys(), list(self.xs))



class IrisDatasetTest(GridDatasetTest):
"""
Tests for Iris interface
"""

def setUp(self):
import iris
self.restore_datatype = Dataset.datatype
Dataset.datatype = ['cube']
self.data_instance_type = iris.cube.Cube
self.init_data()

# Disabled tests for NotImplemented methods
def test_dataset_add_dimensions_values_hm(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume you are doing this to disable some of the inherited tests. A docstring explaining why these tests are not enable would be good...

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, sounds good. They should eventually be remove once those methods are implemented.

pass

def test_dataset_sort_vdim_hm(self):
pass

def test_dataset_1D_reduce_hm(self):
pass

def test_dataset_2D_reduce_hm(self):
pass

def test_dataset_2D_aggregate_partial_hm(self):
pass

def test_dataset_sample_hm(self):
pass

7 changes: 4 additions & 3 deletions tests/testirisinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class TestCube(ComparisonTestCase):

def setUp(self):
self.cube = lat_lon_cube()
self.epsilon = 0.01

def test_dim_to_coord(self):
dim = coord_to_dimension(self.cube.coords()[0])
Expand Down Expand Up @@ -70,7 +71,7 @@ def test_select_index(self):

def test_select_slice(self):
cube = Dataset(self.cube)
self.assertEqual(cube.select(longitude=(0, 1)).data.data,
self.assertEqual(cube.select(longitude=(0, 1+self.epsilon)).data.data,
np.array([[1, 2], [5, 6], [9, 10]], dtype=np.int32))

def test_select_set(self):
Expand All @@ -84,8 +85,8 @@ def test_select_multi_index(self):

def test_select_multi_slice1(self):
cube = Dataset(self.cube)
self.assertEqual(cube.select(longitude=(0, 1),
latitude=(0, 1)).data.data,
self.assertEqual(cube.select(longitude=(0, 1+self.epsilon),
latitude=(0, 1+self.epsilon)).data.data,
np.array([[5, 6], [9, 10]], dtype=np.int32))

def test_select_multi_slice2(self):
Expand Down