Skip to content

Commit 83a939d

Browse files
stephenworsleypp-mo
authored andcommitted
Fix equality for Ancillary Variables (#3536)
1 parent bc39449 commit 83a939d

File tree

2 files changed

+119
-9
lines changed

2 files changed

+119
-9
lines changed

lib/iris/cube.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ def _check_multi_dim_metadata(self, metadata, data_dims):
10441044

10451045
def _add_unique_aux_coord(self, coord, data_dims):
10461046
data_dims = self._check_multi_dim_metadata(coord, data_dims)
1047-
self._aux_coords_and_dims.append([coord, data_dims])
1047+
self._aux_coords_and_dims.append((coord, data_dims))
10481048

10491049
def add_aux_factory(self, aux_factory):
10501050
"""
@@ -1095,7 +1095,7 @@ def add_cell_measure(self, cell_measure, data_dims=None):
10951095
if self.cell_measures(cell_measure):
10961096
raise ValueError("Duplicate cell_measures are not permitted.")
10971097
data_dims = self._check_multi_dim_metadata(cell_measure, data_dims)
1098-
self._cell_measures_and_dims.append([cell_measure, data_dims])
1098+
self._cell_measures_and_dims.append((cell_measure, data_dims))
10991099
self._cell_measures_and_dims.sort(
11001100
key=lambda cm_dims: (cm_dims[0]._as_defn(), cm_dims[1])
11011101
)
@@ -1125,7 +1125,7 @@ def add_ancillary_variable(self, ancillary_variable, data_dims=None):
11251125
ancillary_variable, data_dims
11261126
)
11271127
self._ancillary_variables_and_dims.append(
1128-
[ancillary_variable, data_dims]
1128+
(ancillary_variable, data_dims)
11291129
)
11301130
self._ancillary_variables_and_dims.sort(
11311131
key=lambda av_dims: (av_dims[0]._as_defn(), av_dims[1])
@@ -1195,7 +1195,7 @@ def _add_unique_dim_coord(self, dim_coord, data_dim):
11951195
)
11961196
)
11971197

1198-
self._dim_coords_and_dims.append([dim_coord, int(data_dim)])
1198+
self._dim_coords_and_dims.append((dim_coord, int(data_dim)))
11991199

12001200
def remove_aux_factory(self, aux_factory):
12011201
"""Removes the given auxiliary coordinate factory from the cube."""
@@ -1263,7 +1263,7 @@ def remove_cell_measure(self, cell_measure):
12631263
cell_measure = self.cell_measure(cell_measure)
12641264

12651265
self._cell_measures_and_dims = [
1266-
[cell_measure_, dim]
1266+
(cell_measure_, dim)
12671267
for cell_measure_, dim in self._cell_measures_and_dims
12681268
if cell_measure_ is not cell_measure
12691269
]
@@ -1280,7 +1280,7 @@ def remove_ancillary_variable(self, ancillary_variable):
12801280
12811281
"""
12821282
self._ancillary_variables_and_dims = [
1283-
[ancillary_variable_, dim]
1283+
(ancillary_variable_, dim)
12841284
for ancillary_variable_, dim in self._ancillary_variables_and_dims
12851285
if ancillary_variable_ is not ancillary_variable
12861286
]
@@ -3566,6 +3566,16 @@ def __eq__(self, other):
35663566
or coord_comparison["non_equal_data_dimension"]
35673567
)
35683568

3569+
if result:
3570+
result = set(self._cell_measures_and_dims) == set(
3571+
other._cell_measures_and_dims
3572+
)
3573+
3574+
if result:
3575+
result = set(self._ancillary_variables_and_dims) == set(
3576+
other._ancillary_variables_and_dims
3577+
)
3578+
35693579
# Having checked everything else, check approximate data equality.
35703580
if result:
35713581
result = da.allclose(

lib/iris/tests/unit/cube/test_Cube.py

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@
2525
from iris.analysis import MEAN
2626
from iris.aux_factory import HybridHeightFactory
2727
from iris.cube import Cube
28-
from iris.coords import AuxCoord, DimCoord, CellMeasure, AncillaryVariable
28+
from iris.coords import (
29+
AuxCoord,
30+
DimCoord,
31+
CellMeasure,
32+
AncillaryVariable,
33+
CellMethod,
34+
)
2935
from iris.exceptions import (
3036
CoordinateNotFoundError,
3137
CellMeasureNotFoundError,
@@ -1911,13 +1917,13 @@ def test_remove_aux_coord(self):
19111917
def test_remove_cell_measure(self):
19121918
self.cube.remove_cell_measure(self.cube.cell_measure("area"))
19131919
self.assertEqual(
1914-
self.cube._cell_measures_and_dims, [[self.b_cell_measure, (0, 1)]]
1920+
self.cube._cell_measures_and_dims, [(self.b_cell_measure, (0, 1))]
19151921
)
19161922

19171923
def test_remove_cell_measure_by_name(self):
19181924
self.cube.remove_cell_measure("area")
19191925
self.assertEqual(
1920-
self.cube._cell_measures_and_dims, [[self.b_cell_measure, (0, 1)]]
1926+
self.cube._cell_measures_and_dims, [(self.b_cell_measure, (0, 1))]
19211927
)
19221928

19231929
def test_fail_remove_cell_measure_by_name(self):
@@ -2229,5 +2235,99 @@ def test_data_bool_not_eq(self):
22292235
self.assertFalse(cube1 == cube2)
22302236

22312237

2238+
class Test__eq__meta(tests.IrisTest):
2239+
def test_ancillary_fail(self):
2240+
cube1 = Cube([0, 1])
2241+
cube2 = Cube([0, 1])
2242+
avr = AncillaryVariable([2, 3], long_name="foo")
2243+
cube2.add_ancillary_variable(avr, 0)
2244+
self.assertFalse(cube1 == cube2)
2245+
2246+
def test_ancillary_reorder(self):
2247+
cube1 = Cube([0, 1])
2248+
cube2 = Cube([0, 1])
2249+
avr1 = AncillaryVariable([2, 3], long_name="foo")
2250+
avr2 = AncillaryVariable([4, 5], long_name="bar")
2251+
# Add the same ancillary variables to cube1 and cube2 in
2252+
# opposite orders.
2253+
cube1.add_ancillary_variable(avr1, 0)
2254+
cube1.add_ancillary_variable(avr2, 0)
2255+
cube2.add_ancillary_variable(avr2, 0)
2256+
cube2.add_ancillary_variable(avr1, 0)
2257+
self.assertTrue(cube1 == cube2)
2258+
2259+
def test_ancillary_diff_data(self):
2260+
cube1 = Cube([0, 1])
2261+
cube2 = Cube([0, 1])
2262+
avr1 = AncillaryVariable([2, 3], long_name="foo")
2263+
avr2 = AncillaryVariable([4, 5], long_name="foo")
2264+
cube1.add_ancillary_variable(avr1, 0)
2265+
cube2.add_ancillary_variable(avr2, 0)
2266+
self.assertFalse(cube1 == cube2)
2267+
2268+
def test_cell_measure_fail(self):
2269+
cube1 = Cube([0, 1])
2270+
cube2 = Cube([0, 1])
2271+
cms = CellMeasure([2, 3], measure="area", long_name="foo")
2272+
cube2.add_cell_measure(cms, 0)
2273+
self.assertFalse(cube1 == cube2)
2274+
2275+
def test_cell_measure_reorder(self):
2276+
cube1 = Cube([0, 1])
2277+
cube2 = Cube([0, 1])
2278+
cms1 = CellMeasure([2, 3], measure="area", long_name="foo")
2279+
cms2 = CellMeasure([4, 5], measure="area", long_name="bar")
2280+
# Add the same cell measure to cube1 and cube2 in
2281+
# opposite orders.
2282+
cube1.add_cell_measure(cms1, 0)
2283+
cube1.add_cell_measure(cms2, 0)
2284+
cube2.add_cell_measure(cms2, 0)
2285+
cube2.add_cell_measure(cms1, 0)
2286+
self.assertTrue(cube1 == cube2)
2287+
2288+
def test_cell_measure_diff_data(self):
2289+
cube1 = Cube([0, 1])
2290+
cube2 = Cube([0, 1])
2291+
cms1 = CellMeasure([2, 3], measure="area", long_name="foo")
2292+
cms2 = CellMeasure([4, 5], measure="area", long_name="foo")
2293+
cube1.add_cell_measure(cms1, 0)
2294+
cube2.add_cell_measure(cms2, 0)
2295+
self.assertFalse(cube1 == cube2)
2296+
2297+
def test_cell_method_fail(self):
2298+
cube1 = Cube([0, 1])
2299+
cube2 = Cube([0, 1])
2300+
cmth = CellMethod("mean", "time", "6hr")
2301+
cube2.add_cell_method(cmth)
2302+
self.assertFalse(cube1 == cube2)
2303+
2304+
# Unlike cell measures, cell methods are order sensitive.
2305+
def test_cell_method_reorder_fail(self):
2306+
cube1 = Cube([0, 1])
2307+
cube2 = Cube([0, 1])
2308+
cmth1 = CellMethod("mean", "time", "6hr")
2309+
cmth2 = CellMethod("mean", "time", "12hr")
2310+
# Add the same cell method to cube1 and cube2 in
2311+
# opposite orders.
2312+
cube1.add_cell_method(cmth1)
2313+
cube1.add_cell_method(cmth2)
2314+
cube2.add_cell_method(cmth2)
2315+
cube2.add_cell_method(cmth1)
2316+
self.assertFalse(cube1 == cube2)
2317+
2318+
def test_cell_method_correct_order(self):
2319+
cube1 = Cube([0, 1])
2320+
cube2 = Cube([0, 1])
2321+
cmth1 = CellMethod("mean", "time", "6hr")
2322+
cmth2 = CellMethod("mean", "time", "12hr")
2323+
# Add the same cell method to cube1 and cube2 in
2324+
# the same order.
2325+
cube1.add_cell_method(cmth1)
2326+
cube1.add_cell_method(cmth2)
2327+
cube2.add_cell_method(cmth1)
2328+
cube2.add_cell_method(cmth2)
2329+
self.assertTrue(cube1 == cube2)
2330+
2331+
22322332
if __name__ == "__main__":
22332333
tests.main()

0 commit comments

Comments
 (0)