Skip to content

Commit

Permalink
Merge pull request #709 from ioam/iris_constructor
Browse files Browse the repository at this point in the history
Iris interface handles standard constructors enabling unit tests
  • Loading branch information
jlstevens committed Jun 9, 2016
2 parents cd8045f + a3a9f59 commit 16d97f4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
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)
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):
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

0 comments on commit 16d97f4

Please sign in to comment.