def __eq__(self, other, rtol=1.0e-5, atol=1.0e-8): """Override '==' to allow comparison with other raster objecs Input other: Raster instance to compare to rtol, atol: Relative and absolute tolerance. See numpy.allclose for details """ # Check type if not isinstance(other, Raster): msg = ('Raster instance cannot be compared to %s' ' as its type is %s ' % (str(other), type(other))) raise TypeError(msg) # Check projection if self.projection != other.projection: return False # Check geotransform if self.get_geotransform() != other.get_geotransform(): return False # Check data if not nanallclose(self.get_data(), other.get_data(), rtol=rtol, atol=atol): return False # Check keywords if self.keywords != other.keywords: return False # Raster layers are identical up to the specified tolerance return True
def test_linear_interpolation_nan_array(self): """Interpolation library works (linear mode) with grid points being NaN """ # Define pixel centers along each direction x = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] y = [4.0, 5.0, 7.0, 9.0, 11.0, 13.0] # Define ny by nx array with corresponding values A = numpy.zeros((len(x), len(y))) # Define values for each x, y pair as a linear function for i in range(len(x)): for j in range(len(y)): A[i, j] = linear_function(x[i], y[j]) A[2, 3] = numpy.nan # (x=2.0, y=9.0): NaN # Then test that interpolated points can contain NaN xis = numpy.linspace(x[0], x[-1], 12) etas = numpy.linspace(y[0], y[-1], 10) points = combine_coordinates(xis, etas) vals = interpolate2d(x, y, A, points, mode='linear') refs = linear_function(points[:, 0], points[:, 1]) # Set reference result with expected NaNs and compare for i, (xi, eta) in enumerate(points): if (1.0 < xi <= 3.0) and (7.0 < eta <= 11.0): refs[i] = numpy.nan assert nanallclose(vals, refs, rtol=1e-12, atol=1e-12)
def __eq__(self, other, rtol=1.0e-5, atol=1.0e-8): """Override '==' to allow comparison with other raster objecs Input other: Raster instance to compare to rtol, atol: Relative and absolute tolerance. See numpy.allclose for details """ # Check type if not isinstance(other, Raster): msg = ('Raster instance cannot be compared to %s' ' as its type is %s ' % (str(other), type(other))) raise TypeError(msg) # Check projection if self.projection != other.projection: return False # Check geotransform if self.get_geotransform() != other.get_geotransform(): return False # Check data if not nanallclose( self.get_data(), other.get_data(), rtol=rtol, atol=atol): return False # Check keywords if self.keywords != other.keywords: return False # Raster layers are identical up to the specified tolerance return True
def test_linear_interpolation_nan_points(self): """Interpolation library works with interpolation points being NaN This is was the reason for bug reported in: https://github.com/AIFDR/riab/issues/155 """ # Define pixel centers along each direction x = [1.0, 2.0, 4.0] y = [5.0, 9.0] # Define ny by nx array with corresponding values A = numpy.zeros((len(x), len(y))) # Define values for each x, y pair as a linear function for i in range(len(x)): for j in range(len(y)): A[i, j] = linear_function(x[i], y[j]) # Then test that interpolated points can contain NaN xis = numpy.linspace(x[0], x[-1], 10) etas = numpy.linspace(y[0], y[-1], 10) xis[6:7] = numpy.nan etas[3] = numpy.nan points = combine_coordinates(xis, etas) vals = interpolate2d(x, y, A, points, mode='linear') refs = linear_function(points[:, 0], points[:, 1]) assert nanallclose(vals, refs, rtol=1e-12, atol=1e-12)
def test_interpolation_random_array_and_nan(self): """Interpolation library (constant and linear) works with NaN """ # Define pixel centers along each direction x = numpy.arange(20) * 1.0 y = numpy.arange(25) * 1.0 # Define ny by nx array with corresponding values A = numpy.zeros((len(x), len(y))) # Define arbitrary values for each x, y pair numpy.random.seed(17) A = numpy.random.random((len(x), len(y))) * 10 # Create islands of NaN A[5, 13] = numpy.nan A[6, 14] = A[6, 18] = numpy.nan A[7, 14:18] = numpy.nan A[8, 13:18] = numpy.nan A[9, 12:19] = numpy.nan A[10, 14:17] = numpy.nan A[11, 15] = numpy.nan A[15, 5:6] = numpy.nan # Creat interpolation points xis = numpy.linspace(x[0], x[-1], 39) # Hit all mid points etas = numpy.linspace(y[0], y[-1], 73) # Hit thirds points = combine_coordinates(xis, etas) for mode in ['linear', 'constant']: vals = interpolate2d(x, y, A, points, mode=mode) # Calculate reference result with expected NaNs and compare i = j = 0 for k, (xi, eta) in enumerate(points): # Find indices of nearest higher value in x and y i = numpy.searchsorted(x, xi) j = numpy.searchsorted(y, eta) if i > 0 and j > 0: # Get four neigbours A00 = A[i - 1, j - 1] A01 = A[i - 1, j] A10 = A[i, j - 1] A11 = A[i, j] if numpy.allclose(xi, x[i]): alpha = 1.0 else: alpha = 0.5 if numpy.allclose(eta, y[j]): beta = 1.0 else: beta = eta - y[j - 1] if mode == 'linear': if numpy.any(numpy.isnan([A00, A01, A10, A11])): ref = numpy.nan else: ref = (A00 * (1 - alpha) * (1 - beta) + A01 * (1 - alpha) * beta + A10 * alpha * (1 - beta) + A11 * alpha * beta) elif mode == 'constant': assert alpha >= 0.5 # Only case in this test if beta < 0.5: ref = A10 else: ref = A11 else: msg = 'Unknown mode: %s' % mode raise Exception(msg) #print i, j, xi, eta, alpha, beta, vals[k], ref assert nanallclose(vals[k], ref, rtol=1e-12, atol=1e-12)