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

BUG: Tolerant crs comparison #2062

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 28 additions & 2 deletions lib/iris/coord_systems.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@
import cartopy.crs as ccrs


def _dict_nearly_equal(dict1, dict2):
# Allow coordinate system comparison (floating point numbers with
# tolerance).
for key, value in dict1.iteritems():
if key not in dict2:
result = False
break
if hasattr(dict2[key], '__dict__') and hasattr(value, '__dict__'):
result = _dict_nearly_equal(dict2[key].__dict__,
value.__dict__)
elif hasattr(dict2[key], '__dict__') != hasattr(value, '__dict__'):
result = False
else:
# We intentionally do not use a global tolerance for coordinate
# system comparison.
try:
result = np.allclose(dict2[key], value, 1e-5)
except (TypeError, ValueError):
result = dict2[key] == value
if not result:
break
return result


class CoordSystem(six.with_metaclass(ABCMeta, object)):
"""
Abstract base class for coordinate systems.
Expand All @@ -40,8 +64,10 @@ class CoordSystem(six.with_metaclass(ABCMeta, object)):
grid_mapping_name = None

def __eq__(self, other):
return (self.__class__ == other.__class__ and
self.__dict__ == other.__dict__)
res = False
if other is not None:
res = _dict_nearly_equal(self.__dict__, other.__dict__)
return res

def __ne__(self, other):
# Must supply __ne__, Python does not defer to __eq__ for
Expand Down
79 changes: 79 additions & 0 deletions lib/iris/tests/unit/coord_systems/test_GeogCS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# (C) British Crown Copyright 2016, Met Office
#
# This file is part of Iris.
#
# Iris is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Iris is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Iris. If not, see <http://www.gnu.org/licenses/>.
"""Unit tests for the :class:`iris.coord_systems.GeogCS` class."""

from __future__ import (absolute_import, division, print_function)
from six.moves import (filter, input, map, range, zip) # noqa

# Import iris.tests first so that some things can be initialised before
# importing anything else.
import iris.tests as tests

import numpy as np

from iris.coord_systems import GeogCS


class Test___eq__(tests.IrisTest):
def test_derived_quantities_eq(self):
# Ensure that derived quantities are equal.
crs1 = GeogCS(semi_major_axis=6377563.396, semi_minor_axis=6356256.909)
crs2 = GeogCS(semi_major_axis=6377563.396,
inverse_flattening=299.324)
self.assertEqual(crs1, crs2)

def test_32bit_derived_quantities_eq(self):
# Ensure that 32-bit coerced quantities don't fail equality.
crs1 = GeogCS(semi_major_axis=6377563.396, semi_minor_axis=6356256.909)
crs2 = GeogCS(semi_major_axis=np.float32(6377563.396),
inverse_flattening=np.float32(299.324))
self.assertEqual(crs1, crs2)

def test_derived_quantities_neq(self):
# Ensure that derived quantities are not equal.
crs1 = GeogCS(semi_major_axis=6377563.396, semi_minor_axis=6356256.909)
crs2 = GeogCS(semi_major_axis=6377563.396,
inverse_flattening=299.33)
self.assertFalse(crs1 == crs2)

def test_straight_eq(self):
# No derived quantities.
crs1 = GeogCS(6377560)
crs2 = GeogCS(6377570)
self.assertEqual(crs1, crs2)

def test_straight_neq(self):
# No derived quantities.
crs1 = GeogCS(6377500)
crs2 = GeogCS(6377600)
self.assertFalse(crs1 == crs2)

def test_straight_32bit_eq(self):
# No derived quantities.
crs1 = GeogCS(6377560)
crs2 = GeogCS(np.float32(6377570))
self.assertEqual(crs1, crs2)

def test_straight_32bit_neq(self):
# No derived quantities.
crs1 = GeogCS(6377500)
crs2 = GeogCS(np.float32(6377600))
self.assertFalse(crs1 == crs2)


if __name__ == '__main__':
tests.main()