def test_dtypes(self): # Index coords with all combinations of real+lazy points+bounds, and # either an int or floating dtype. # Check that dtypes remain the same in all cases, taking the dtypes # directly from the core points and bounds (as we have no masking). for (main_coord, points_type_name, bounds_type_name) in \ coords_all_dtypes_and_lazynesses(self, AuxCoord): sub_coord = main_coord[:2, 1] coord_dtype = main_coord.dtype msg = ('Indexing main_coord of dtype {} ' 'with {} points and {} bounds ' 'changed dtype of {} to {}.') sub_points = sub_coord.core_points() self.assertEqual( sub_points.dtype, coord_dtype, msg.format(coord_dtype, points_type_name, bounds_type_name, 'points', sub_points.dtype)) if bounds_type_name is not 'no': sub_bounds = sub_coord.core_bounds() main_bounds_dtype = main_coord.bounds_dtype self.assertEqual( sub_bounds.dtype, main_bounds_dtype, msg.format(main_bounds_dtype, points_type_name, bounds_type_name, 'bounds', sub_bounds.dtype))
def test_realdata_copies(self): # Copy coords with all combinations of real+lazy points+bounds. # In all cases, check that any real arrays are copies, not views. for (main_coord, points_lazyness, bounds_lazyness) in \ coords_all_dtypes_and_lazynesses(self, AuxCoord): copied_coord = main_coord.copy() msg = ('Copied coord with {} points and {} bounds ' 'does not have its own separate {} array.') if points_lazyness == 'real': main_points = main_coord.core_points() copied_points = copied_coord.core_points() self.assertEqualRealArraysAndDtypes(main_points, copied_points) self.assertArraysDoNotShareData( main_points, copied_points, msg.format(points_lazyness, bounds_lazyness, 'points')) if bounds_lazyness == 'real': main_bounds = main_coord.core_bounds() copied_bounds = copied_coord.core_bounds() self.assertEqualRealArraysAndDtypes(main_bounds, copied_bounds) self.assertArraysDoNotShareData( main_bounds, copied_bounds, msg.format(points_lazyness, bounds_lazyness, 'bounds'))
def test_real_data_copies(self): # Index coords with all combinations of real+lazy points+bounds. # In all cases, check that any real arrays are copied by the indexing. for (main_coord, points_lazyness, bounds_lazyness) in \ coords_all_dtypes_and_lazynesses(self, AuxCoord): sub_coord = main_coord[:2, 1] msg = ('Indexed coord with {} points and {} bounds ' 'does not have its own separate {} array.') if points_lazyness == 'real': main_points = main_coord.core_points() sub_points = sub_coord.core_points() sub_main_points = main_points[:2, 1] self.assertEqualRealArraysAndDtypes(sub_points, sub_main_points) self.assertArraysDoNotShareData( sub_points, sub_main_points, msg.format(points_lazyness, bounds_lazyness, 'points')) if bounds_lazyness == 'real': main_bounds = main_coord.core_bounds() sub_bounds = sub_coord.core_bounds() sub_main_bounds = main_bounds[:2, 1] self.assertEqualRealArraysAndDtypes(sub_bounds, sub_main_bounds) self.assertArraysDoNotShareData( sub_bounds, sub_main_bounds, msg.format(points_lazyness, bounds_lazyness, 'bounds'))
def test_lazyness(self): # Copy coords with all combinations of real+lazy points+bounds, and # either an int or floating dtype. # Check that lazy data stays lazy and real stays real, in all cases. for (main_coord, points_lazyness, bounds_lazyness) in \ coords_all_dtypes_and_lazynesses(self, AuxCoord): coord_dtype = main_coord.dtype copied_coord = main_coord.copy() msg = ('Copying main_coord of dtype {} ' 'with {} points and {} bounds ' 'changed lazyness of {} from {!r} to {!r}.') copied_pts_lazyness = lazyness_string(copied_coord.core_points()) self.assertEqual(copied_pts_lazyness, points_lazyness, msg.format(coord_dtype, points_lazyness, bounds_lazyness, 'points', points_lazyness, copied_pts_lazyness)) if bounds_lazyness != 'no': copied_bds_lazy = lazyness_string(copied_coord.core_bounds()) self.assertEqual(copied_bds_lazy, bounds_lazyness, msg.format(coord_dtype, points_lazyness, bounds_lazyness, 'bounds', bounds_lazyness, copied_bds_lazy))
def test_lazyness_and_dtype_combinations(self): for (coord, points_type_name, bounds_type_name) in \ coords_all_dtypes_and_lazynesses(self, DimCoord): pts = coord.core_points() bds = coord.core_bounds() # Check properties of points. if (points_type_name == 'real' and coord.dtype == self.pts_real.dtype): # Points array should be identical to the reference one. self.assertArraysShareData( pts, self.pts_real, 'Points are not the same data as the provided array.') else: # the original points array was cast to a test dtype. check_pts = self.pts_real.astype(coord.dtype) self.assertEqualRealArraysAndDtypes(pts, check_pts) # Check properties of bounds. if bounds_type_name != 'no': if (bounds_type_name == 'real' and coord.bounds_dtype == self.bds_real.dtype): # Bounds array should be the reference data. self.assertArraysShareData( bds, self.bds_real, 'Bounds are not the same data as the provided array.') else: # the original bounds array was cast to a test dtype. check_bds = self.bds_real.astype(coord.bounds_dtype) self.assertEqualRealArraysAndDtypes(bds, check_bds)
def test_lazyness_and_dtype_combinations(self): for ( coord, points_type_name, bounds_type_name, ) in coords_all_dtypes_and_lazynesses(self, DimCoord): pts = coord.core_points() bds = coord.core_bounds() # Check properties of points. # Points array should not be identical to the reference one. self.assertArraysDoNotShareData( pts, self.pts_real, "Points are the same data as the provided array.", ) # the original points array was cast to a test dtype. check_pts = self.pts_real.astype(coord.dtype) self.assertEqualRealArraysAndDtypes(pts, check_pts) # Check properties of bounds. if bounds_type_name != "no": # Bounds array should not be the reference data. self.assertArraysDoNotShareData( bds, self.bds_real, "Bounds are the same data as the provided array.", ) # the original bounds array was cast to a test dtype. check_bds = self.bds_real.astype(coord.bounds_dtype) self.assertEqualRealArraysAndDtypes(bds, check_bds)
def test_lazyness(self): # Index coords with all combinations of real+lazy points+bounds, and # either an int or floating dtype. # Check that lazy data stays lazy and real stays real, in all cases. for (main_coord, points_type_name, bounds_type_name) in \ coords_all_dtypes_and_lazynesses(self, DimCoord): # N.B. 'points_type_name' and 'bounds_type_name' in the iteration # are the original types (lazy/real/none) of the points+bounds, # but the DimCoord itself only ever has real data. if points_type_name == 'lazy': points_type_name = 'real' if bounds_type_name == 'lazy': bounds_type_name = 'real' sub_coord = main_coord[:2] msg = ('Indexing coord of dtype {} ' 'with {} points and {} bounds ' 'changed "lazyness" of {} from {!r} to {!r}.') coord_dtype = main_coord.dtype sub_points_lazyness = lazyness_string(sub_coord.core_points()) self.assertEqual( sub_points_lazyness, points_type_name, msg.format(coord_dtype, points_type_name, bounds_type_name, 'points', points_type_name, sub_points_lazyness)) if bounds_type_name is not 'no': sub_bounds_lazy = lazyness_string(sub_coord.core_bounds()) self.assertEqual( sub_bounds_lazy, bounds_type_name, msg.format(coord_dtype, points_type_name, bounds_type_name, 'bounds', bounds_type_name, sub_bounds_lazy))
def test_lazyness(self): # Copy coords with all combinations of real+lazy points+bounds, and # either an int or floating dtype. # Check that lazy data stays lazy and real stays real, in all cases. for (main_coord, points_lazyness, bounds_lazyness) in \ coords_all_dtypes_and_lazynesses(self, AuxCoord): coord_dtype = main_coord.dtype copied_coord = main_coord.copy() msg = ('Copying main_coord of dtype {} ' 'with {} points and {} bounds ' 'changed lazyness of {} from {!r} to {!r}.') copied_pts_lazyness = lazyness_string(copied_coord.core_points()) self.assertEqual( copied_pts_lazyness, points_lazyness, msg.format(coord_dtype, points_lazyness, bounds_lazyness, 'points', points_lazyness, copied_pts_lazyness)) if bounds_lazyness != 'no': copied_bds_lazy = lazyness_string(copied_coord.core_bounds()) self.assertEqual( copied_bds_lazy, bounds_lazyness, msg.format(coord_dtype, points_lazyness, bounds_lazyness, 'bounds', bounds_lazyness, copied_bds_lazy))
def test_lazyness(self): # Index coords with all combinations of real+lazy points+bounds, and # either an int or floating dtype. # Check that lazy data stays lazy and real stays real, in all cases. for (main_coord, points_type_name, bounds_type_name) in \ coords_all_dtypes_and_lazynesses(self, AuxCoord): sub_coord = main_coord[:2, 1] msg = ('Indexing coord of dtype {} ' 'with {} points and {} bounds ' 'changed laziness of {} from {!r} to {!r}.') coord_dtype = main_coord.dtype sub_points_lazyness = lazyness_string(sub_coord.core_points()) self.assertEqual( sub_points_lazyness, points_type_name, msg.format(coord_dtype, points_type_name, bounds_type_name, 'points', points_type_name, sub_points_lazyness)) if bounds_type_name is not 'no': sub_bounds_lazy = lazyness_string(sub_coord.core_bounds()) self.assertEqual( sub_bounds_lazy, bounds_type_name, msg.format(coord_dtype, points_type_name, bounds_type_name, 'bounds', bounds_type_name, sub_bounds_lazy))
def test_lazyness_and_dtype_combinations(self): for (coord, points_type_name, bounds_type_name) in \ coords_all_dtypes_and_lazynesses(self, AuxCoord): pts = coord.core_points() bds = coord.core_bounds() # Check properties of points. if points_type_name == 'real': # Real points. if coord.dtype == self.pts_real.dtype: self.assertArraysShareData( pts, self.pts_real, 'Points are not the same data as the provided array.') self.assertIsNot( pts, self.pts_real, 'Points array is the same instance as the provided ' 'array.') else: # the original points were cast to a test dtype. check_pts = self.pts_real.astype(coord.dtype) self.assertEqualRealArraysAndDtypes(pts, check_pts) else: # Lazy points : the core data may be promoted to float. check_pts = self.pts_lazy.astype(pts.dtype) self.assertEqualLazyArraysAndDtypes(pts, check_pts) # The realisation type should be correct, though. target_dtype = coord.dtype self.assertEqual(coord.points.dtype, target_dtype) # Check properties of bounds. if bounds_type_name == 'real': # Real bounds. if coord.bounds_dtype == self.bds_real.dtype: self.assertArraysShareData( bds, self.bds_real, 'Bounds are not the same data as the provided array.') self.assertIsNot( pts, self.pts_real, 'Bounds array is the same instance as the provided ' 'array.') else: # the original bounds were cast to a test dtype. check_bds = self.bds_real.astype(coord.bounds_dtype) self.assertEqualRealArraysAndDtypes(bds, check_bds) elif bounds_type_name == 'lazy': # Lazy points : the core data may be promoted to float. check_bds = self.bds_lazy.astype(bds.dtype) self.assertEqualLazyArraysAndDtypes(bds, check_bds) # The realisation type should be correct, though. target_dtype = coord.bounds_dtype self.assertEqual(coord.bounds.dtype, target_dtype)
def test_lazyness(self): # Index coords with all combinations of real+lazy points+bounds, and # either an int or floating dtype. # Check that lazy data stays lazy and real stays real, in all cases. for ( main_coord, points_type_name, bounds_type_name, ) in coords_all_dtypes_and_lazynesses(self, AuxCoord): sub_coord = main_coord[:2, 1] msg = ( "Indexing coord of dtype {} " "with {} points and {} bounds " "changed laziness of {} from {!r} to {!r}." ) coord_dtype = main_coord.dtype sub_points_lazyness = lazyness_string(sub_coord.core_points()) self.assertEqual( sub_points_lazyness, points_type_name, msg.format( coord_dtype, points_type_name, bounds_type_name, "points", points_type_name, sub_points_lazyness, ), ) if bounds_type_name != "no": sub_bounds_lazy = lazyness_string(sub_coord.core_bounds()) self.assertEqual( sub_bounds_lazy, bounds_type_name, msg.format( coord_dtype, points_type_name, bounds_type_name, "bounds", bounds_type_name, sub_bounds_lazy, ), )
def test_realdata_readonly(self): # Copy coords with all combinations of real+lazy points+bounds. # In all cases, check that data arrays are read-only. for (main_coord, points_type_name, bounds_type_name) in \ coords_all_dtypes_and_lazynesses(self, DimCoord): copied_coord = main_coord.copy() copied_points = copied_coord.core_points() expected_error_msg = 'output array is read-only' with self.assertRaisesRegexp(ValueError, expected_error_msg): copied_points[:1] += 33 if bounds_type_name != 'no': copied_bounds = copied_coord.core_bounds() with self.assertRaisesRegexp(ValueError, expected_error_msg): copied_bounds[:1] += 33
def test_dtypes(self): # Index coords with all combinations of real+lazy points+bounds, and # either an int or floating dtype. # Check that dtypes remain the same in all cases, taking the dtypes # directly from the core points and bounds (as we have no masking). for ( main_coord, points_type_name, bounds_type_name, ) in coords_all_dtypes_and_lazynesses(self, DimCoord): sub_coord = main_coord[:2] coord_dtype = main_coord.dtype msg = ("Indexing main_coord of dtype {} " "with {} points and {} bounds " "changed dtype of {} to {}.") sub_points = sub_coord.core_points() self.assertEqual( sub_points.dtype, coord_dtype, msg.format( coord_dtype, points_type_name, bounds_type_name, "points", sub_points.dtype, ), ) if bounds_type_name != "no": sub_bounds = sub_coord.core_bounds() main_bounds_dtype = main_coord.bounds_dtype self.assertEqual( sub_bounds.dtype, main_bounds_dtype, msg.format( main_bounds_dtype, points_type_name, bounds_type_name, "bounds", sub_bounds.dtype, ), )