def test_numeric_nd(self): # Contiguous only defined for 2d bounds. coord = AuxCoord(points=np.array([3, 6, 9]), bounds=np.array([[1, 2, 4, 5], [4, 5, 7, 8], [7, 8, 10, 11]])) with self.assertRaises(ValueError): coord.collapsed()
def test_numeric_nd_bounds_second(self): self.setupTestArrays((3, 4)) coord = AuxCoord(self.pts_real, bounds=self.bds_real) collapsed_coord = coord.collapsed(1) self.assertArrayEqual(collapsed_coord.points, np.array([15, 55, 95])) self.assertArrayEqual(collapsed_coord.bounds, np.array([[-2, 32], [38, 72], [78, 112]]))
def test_numeric_nd_bounds_all(self): self.setupTestArrays((3, 4)) coord = AuxCoord(self.pts_real, bounds=self.bds_real) collapsed_coord = coord.collapsed() self.assertArrayEqual(collapsed_coord.points, np.array([55])) self.assertArrayEqual(collapsed_coord.bounds, np.array([[-2, 112]]))
def test_serialize(self): # Collapse a string AuxCoord, causing it to be serialised. string = Pair(np.array(['two', 'four', 'six', 'eight']), np.array([['one', 'three'], ['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'], ['seven', 'eight', 'ten', 'eleven']])) def _serialize(data): return '|'.join(str(item) for item in data.flatten()) for units in ['unknown', 'no_unit']: 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)) 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_serialize(self): # Collapse a string AuxCoord, causing it to be serialised. string = Pair( np.array(['two', 'four', 'six', 'eight']), np.array([['one', 'three'], ['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'], ['seven', 'eight', 'ten', 'eleven']])) def _serialize(data): return '|'.join(str(item) for item in data.flatten()) for units in ['unknown', 'no_unit']: 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)) 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_numeric_nd_bounds_last(self): self.setupTestArrays((3, 4)) coord = AuxCoord(self.pts_real, bounds=self.bds_real) # ... and again with -ve dimension specification. collapsed_coord = coord.collapsed(-1) self.assertArrayEqual(collapsed_coord.points, np.array([15, 55, 95])) self.assertArrayEqual(collapsed_coord.bounds, np.array([[-2, 32], [38, 72], [78, 112]]))
def test_lazy_nd_bounds_first(self): self.setupTestArrays((3, 4)) coord = AuxCoord(self.pts_real, bounds=self.bds_lazy) collapsed_coord = coord.collapsed(0) self.assertArrayEqual(collapsed_coord.points, np.array([40, 50, 60, 70])) self.assertArrayEqual( collapsed_coord.bounds, np.array([[-2, 82], [8, 92], [18, 102], [28, 112]]))
def test_numeric_nd(self): coord = AuxCoord( points=np.array([[1, 2, 4, 5], [4, 5, 7, 8], [7, 8, 10, 11]])) collapsed_coord = coord.collapsed() self.assertArrayEqual(collapsed_coord.points, np.array([6])) self.assertArrayEqual(collapsed_coord.bounds, np.array([[1, 11]])) # Test partially collapsing one dimension... collapsed_coord = coord.collapsed(1) self.assertArrayEqual(collapsed_coord.points, np.array([3., 6., 9.])) self.assertArrayEqual(collapsed_coord.bounds, np.array([[1, 5], [4, 8], [7, 11]])) # ... and the other collapsed_coord = coord.collapsed(0) self.assertArrayEqual(collapsed_coord.points, np.array([4, 5, 7, 8])) self.assertArrayEqual(collapsed_coord.bounds, np.array([[1, 7], [2, 8], [4, 10], [5, 11]]))
def test_serialize(self): for units in ['unknown', 'no_unit']: for points, bounds in self.pairs: coord = mock.MagicMock(spec_set=AuxCoord, name='AuxCoord', points=points, bounds=bounds, units=units) # Now perform the collase operation with the mock coordinate. AuxCoord.collapsed(coord) # Examine the operational footprint in the mock. self.assertEqual(coord.copy.call_count, 1) args, kwargs = coord.copy.call_args self.assertEqual(args, ()) self.assertEqual(set(kwargs), set(['points', 'bounds'])) self.assertArrayEqual(kwargs['points'], self._serialize(points)) for index in np.ndindex(coord.bounds.shape[1:]): index_slice = (slice(None),) + tuple(index) self.assertArrayEqual(kwargs['bounds'][index_slice], self._serialize(bounds[index_slice]))
def test_lazy_nd_points_and_bounds(self): self.setupTestArrays((3, 4)) coord = AuxCoord(self.pts_lazy, bounds=self.bds_lazy) collapsed_coord = coord.collapsed() self.assertTrue(collapsed_coord.has_lazy_points()) self.assertTrue(collapsed_coord.has_lazy_bounds()) self.assertArrayEqual(collapsed_coord.points, da.array([55])) self.assertArrayEqual(collapsed_coord.bounds, da.array([[-2, 112]]))
def test_serialize(self): for units in ['unknown', 'no_unit']: for points, bounds in self.pairs: coord = mock.MagicMock(spec_set=AuxCoord, name='AuxCoord', points=points, bounds=bounds, units=units) # Now perform the collase operation with the mock coordinate. AuxCoord.collapsed(coord) # Examine the operational footprint in the mock. self.assertEqual(coord.copy.call_count, 1) args, kwargs = coord.copy.call_args self.assertEqual(args, ()) self.assertEqual(set(kwargs), set(['points', 'bounds'])) self.assertArrayEqual(kwargs['points'], self._serialize(points)) for index in np.ndindex(coord.bounds.shape[1:]): index_slice = (slice(None), ) + tuple(index) self.assertArrayEqual(kwargs['bounds'][index_slice], self._serialize(bounds[index_slice]))
def test_lazy_nd_points_and_bounds(self): import dask.array as da self.setupTestArrays((3, 4)) coord = AuxCoord(self.pts_lazy, bounds=self.bds_lazy) collapsed_coord = coord.collapsed() self.assertTrue(collapsed_coord.has_lazy_points()) self.assertTrue(collapsed_coord.has_lazy_bounds()) self.assertArrayEqual(collapsed_coord.points, da.array([55])) self.assertArrayEqual(collapsed_coord.bounds, da.array([[-2, 112]]))
def test_lazy_nd_bounds_all(self): self.setupTestArrays((3, 4)) coord = AuxCoord(self.pts_real, bounds=self.bds_lazy) collapsed_coord = coord.collapsed() # Note that the new points get recalculated from the lazy bounds # and so end up as lazy self.assertTrue(collapsed_coord.has_lazy_points()) self.assertTrue(collapsed_coord.has_lazy_bounds()) self.assertArrayEqual(collapsed_coord.points, np.array([55])) self.assertArrayEqual(collapsed_coord.bounds, da.array([[-2, 112]]))
def test_numeric_nd(self): coord = AuxCoord(points=np.array([[1, 2, 4, 5], [4, 5, 7, 8], [7, 8, 10, 11]])) collapsed_coord = coord.collapsed() self.assertArrayEqual(collapsed_coord.points, np.array([6])) self.assertArrayEqual(collapsed_coord.bounds, np.array([[1, 11]])) # Test partially collapsing one dimension... collapsed_coord = coord.collapsed(1) self.assertArrayEqual(collapsed_coord.points, np.array([3., 6., 9.])) self.assertArrayEqual(collapsed_coord.bounds, np.array([[1, 5], [4, 8], [7, 11]])) # ... and the other collapsed_coord = coord.collapsed(0) self.assertArrayEqual(collapsed_coord.points, np.array([4, 5, 7, 8])) self.assertArrayEqual(collapsed_coord.bounds, np.array([[1, 7], [2, 8], [4, 10], [5, 11]]))
def test_lazy_nd_bounds(self): import dask.array as da self.setupTestArrays((3, 4)) coord = AuxCoord(self.pts_real, bounds=self.bds_lazy) collapsed_coord = coord.collapsed() # Note that the new points get recalculated from the lazy bounds # and so end up as lazy self.assertTrue(collapsed_coord.has_lazy_points()) self.assertTrue(collapsed_coord.has_lazy_bounds()) self.assertArrayEqual(collapsed_coord.points, np.array([55])) self.assertArrayEqual(collapsed_coord.bounds, da.array([[-2, 112]]))
def test_serialize(self): # Collapse a string AuxCoord, causing it to be serialised. string = Pair( np.array(["two", "four", "six", "eight"]), np.array([ ["one", "three"], ["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"], ["seven", "eight", "ten", "eleven"], ]), ) def _serialize(data): return "|".join(str(item) for item in data.flatten()) for units in ["unknown", "no_unit"]: 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)) 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_lazy_points(self): # Lazy points should stay lazy after collapse. coord = AuxCoord(points=da.from_array(np.arange(5), chunks=5)) collapsed_coord = coord.collapsed() self.assertTrue(collapsed_coord.has_lazy_bounds()) self.assertTrue(collapsed_coord.has_lazy_points())