From f24bc8ac6fca399a0ecb8f41316475a296b226eb Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Mon, 6 Jul 2015 21:57:00 -0400 Subject: [PATCH 1/3] py3k: Use Unicode in coord collapse. --- lib/iris/coords.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/iris/coords.py b/lib/iris/coords.py index a1bab3888d..8ee43cb6f6 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -968,10 +968,10 @@ def collapsed(self, dims_to_collapse=None): for index in np.ndindex(shape): index_slice = (slice(None),) + tuple(index) bounds.append(serialize(self.bounds[index_slice])) - dtype = np.dtype('S{}'.format(max(map(len, bounds)))) + dtype = np.dtype('U{}'.format(max(map(len, bounds)))) bounds = np.array(bounds, dtype=dtype).reshape((1,) + shape) points = serialize(self.points) - dtype = np.dtype('S{}'.format(len(points))) + dtype = np.dtype('U{}'.format(len(points))) # Create the new collapsed coordinate. coord = self.copy(points=np.array(points, dtype=dtype), bounds=bounds) From 01502f0d5c99e20e4a6961bba5be539495306a16 Mon Sep 17 00:00:00 2001 From: Patrick Peglar Date: Fri, 6 Nov 2015 17:24:16 +0000 Subject: [PATCH 2/3] py3k: Use Unicode in coord collapse : Modified = unicode only in Python 3. --- lib/iris/coords.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/iris/coords.py b/lib/iris/coords.py index 8ee43cb6f6..ed6b271cff 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -968,10 +968,11 @@ def collapsed(self, dims_to_collapse=None): for index in np.ndindex(shape): index_slice = (slice(None),) + tuple(index) bounds.append(serialize(self.bounds[index_slice])) - dtype = np.dtype('U{}'.format(max(map(len, bounds)))) + string_type_fmt = 'S{}' if six.PY2 else 'U{}' + dtype = np.dtype(string_type_fmt.format(max(map(len, bounds)))) bounds = np.array(bounds, dtype=dtype).reshape((1,) + shape) points = serialize(self.points) - dtype = np.dtype('U{}'.format(len(points))) + dtype = np.dtype(string_type_fmt.format(len(points))) # Create the new collapsed coordinate. coord = self.copy(points=np.array(points, dtype=dtype), bounds=bounds) From 501f3e3015d4bd01eaf78af8d37d0721633541c0 Mon Sep 17 00:00:00 2001 From: Patrick Peglar Date: Sun, 8 Nov 2015 11:33:16 +0000 Subject: [PATCH 3/3] Review fix: bug in string coord collapse. --- lib/iris/coords.py | 2 +- lib/iris/tests/unit/coords/test_Coord.py | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/iris/coords.py b/lib/iris/coords.py index ed6b271cff..f2d5c95c57 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -962,13 +962,13 @@ def collapsed(self, dims_to_collapse=None): # bounds as strings. serialize = lambda x: '|'.join([str(i) for i in x.flatten()]) bounds = None + string_type_fmt = 'S{}' if six.PY2 else 'U{}' if self.bounds is not None: shape = self.bounds.shape[1:] bounds = [] for index in np.ndindex(shape): index_slice = (slice(None),) + tuple(index) bounds.append(serialize(self.bounds[index_slice])) - string_type_fmt = 'S{}' if six.PY2 else 'U{}' dtype = np.dtype(string_type_fmt.format(max(map(len, bounds)))) bounds = np.array(bounds, dtype=dtype).reshape((1,) + shape) points = serialize(self.points) diff --git a/lib/iris/tests/unit/coords/test_Coord.py b/lib/iris/tests/unit/coords/test_Coord.py index 85aa642294..20dc298406 100644 --- a/lib/iris/tests/unit/coords/test_Coord.py +++ b/lib/iris/tests/unit/coords/test_Coord.py @@ -205,6 +205,8 @@ def test_serialize(self): ['three', 'five'], ['five', 'seven'], ['seven', 'nine']])) + string_nobounds = Pair(np.array(['ecks', 'why', 'zed']), + None) string_multi = Pair(np.array(['three', 'six', 'nine']), np.array([['one', 'two', 'four', 'five'], ['four', 'five', 'seven', 'eight'], @@ -214,15 +216,17 @@ def _serialize(data): return '|'.join(str(item) for item in data.flatten()) for units in ['unknown', 'no_unit']: - for points, bounds in [string, string_multi]: + for points, bounds in [string, string_nobounds, string_multi]: coord = AuxCoord(points=points, bounds=bounds, units=units) collapsed_coord = coord.collapsed() self.assertArrayEqual(collapsed_coord.points, _serialize(points)) - for index in np.ndindex(bounds.shape[1:]): - index_slice = (slice(None),) + tuple(index) - self.assertArrayEqual(collapsed_coord.bounds[index_slice], - _serialize(bounds[index_slice])) + if bounds is not None: + for index in np.ndindex(bounds.shape[1:]): + index_slice = (slice(None),) + tuple(index) + self.assertArrayEqual( + collapsed_coord.bounds[index_slice], + _serialize(bounds[index_slice])) def test_dim_1d(self): # Numeric coords should not be serialised.