diff --git a/holoviews/core/data/iris.py b/holoviews/core/data/iris.py index 84e2ccffd4..c114d621a9 100644 --- a/holoviews/core/data/iris.py +++ b/holoviews/core/data/iris.py @@ -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) @@ -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): """ @@ -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) @@ -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] diff --git a/tests/testdataset.py b/tests/testdataset.py index 54c5aa305a..4b8e3b268e 100644 --- a/tests/testdataset.py +++ b/tests/testdataset.py @@ -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): @@ -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 + diff --git a/tests/testirisinterface.py b/tests/testirisinterface.py index 0e0b35e8c3..16315a6227 100644 --- a/tests/testirisinterface.py +++ b/tests/testirisinterface.py @@ -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]) @@ -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): @@ -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):