def test_2d_mwm_small(self): values = [[9, 2], [5, 5], [2, 9]] weights = [[3, 0], [5, 0], [8, 0]] np.testing.assert_array_equal( py_mwm_nd(values, weights, 3), moving_weighted_median(values, weights, (3, 3)), ) values = [ [8.0, 6.0, 1.0, 5.0], [6.0, 6.0, 8.0, 10.0], [8.0, 4.0, 4.0, 7.0], [10.0, 1.0, 7.0, 8.0], ] weights = [ [8.0, 6.0, 1.0, 5.0], [6.0, 6.0, 8.0, 10.0], [8.0, 4.0, 4.0, 7.0], [10.0, 1.0, 7.0, 8.0], ] np.testing.assert_array_equal( py_mwm_nd(values, weights, 3), moving_weighted_median(values, weights, (3, 3)), ) values = [[9, 4], [2, 5]] weights = [[4, 8], [4, 5]] np.testing.assert_array_equal( py_mwm_nd(values, weights, 3), moving_weighted_median(values, weights, (3, 3)), )
def test_2d_mwm_big(self): N = 100 M = 100 N_w = 5 values = np.random.random_sample((N, M)) weights = np.random.random_sample((N, M)) t0 = time.time() moving_weighted_median(values, weights, (N_w, N_w)) t_cython = time.time() - t0 print("2D moving {}x{} weighted median with {}x{} elements took {}s". format(N_w, N_w, N, N, t_cython))
def test_2d_mwm(self): values = np.random.rand(14, 8) weights = np.random.rand(14, 8) np.testing.assert_array_equal( py_mwm_nd(values, weights, 3), moving_weighted_median(values, weights, (3, 3)), )
def test_2d_mwm_int(self): values = np.asarray(np.random.randint(0, 10, (14, 8)), np.float64) weights = np.asarray(np.random.randint(0, 10, (14, 8)), np.float64) np.testing.assert_array_equal( py_mwm_nd(values, weights, (3, 3)), moving_weighted_median(values, weights, (3, 3)), )
def medfilt(x, mask, size, *args): """Apply a moving median filter to masked data. The application is done by iterative filling to overcome the fact we don't have an actual implementation of a nanmedian. Parameters ---------- x : np.ndarray Data to filter. mask : np.ndarray Mask of data to filter out. size : tuple Size of the window in each dimension. Returns ------- y : np.ndarray The masked data. Data within the mask is undefined. """ if np.iscomplexobj(x): return medfilt(x.real, mask, size) + 1.0j * medfilt(x.imag, mask, size) # Copy and do initial masking x = np.ascontiguousarray(x.astype(np.float64)) w = np.ascontiguousarray((~mask).astype(np.float64)) return weighted_median.moving_weighted_median(x, w, size, *args)
def test_small_zero_weight(self): window = (3, 3) zero_shape = (2, 2) shape = (10, 10) data = np.ones(shape, dtype=np.float64) weight = np.ones_like(data) weight[1:zero_shape[0] + 1, 1:zero_shape[1] + 1] = 0.0 np.testing.assert_array_equal( data, moving_weighted_median(data, weight, window))
def test_1d_mwm_big(self): N = 100 values = np.random.random_sample(N) weights = np.random.random_sample(N) t0 = time.time() res_py = py_mwm_1d(values, weights, 5) t_py = time.time() - t0 t0 = time.time() res_cython = moving_weighted_median(values, weights, 5) t_cython = time.time() - t0 np.testing.assert_array_equal(res_py, res_cython) print( "1D moving weighted median with {} elements took {}s / {}s".format( N, t_py, t_cython))
def test_med_zero_weight(self): window = (3, ) zero_shape = (3, ) shape = (10, ) data = np.ones(shape, dtype=np.float64) weight = np.ones_like(data) weight[1:zero_shape[0] + 1] = 0.0 result = data.copy() result[2] = np.nan np.testing.assert_array_equal( result, moving_weighted_median(data, weight, window))
def test_2d_mwm_small_large_window(self): # Try a window that is much larger than the input array. # This has caused crashes in older versions values = [ [8.0, 6.0, 1.0, 5.0], [6.0, 6.0, 8.0, 10.0], [8.0, 4.0, 4.0, 7.0], [10.0, 1.0, 7.0, 8.0], ] weights = [ [8.0, 6.0, 1.0, 5.0], [6.0, 6.0, 8.0, 10.0], [8.0, 4.0, 4.0, 7.0], [10.0, 1.0, 7.0, 8.0], ] py_res = py_mwm_nd(values, weights, 11) cy_res = moving_weighted_median(values, weights, (11, 11)) np.testing.assert_array_equal(py_res, cy_res) # The window is so large all values should be equal, double check that np.testing.assert_array_equal(cy_res, cy_res[0, 0])
def test_zero_weights(self): values = [1, 1, 1] weights = [0, 0, 0] np.testing.assert_array_equal([0, 0, 0], moving_weighted_median( values, weights, 1))
def test_1d_mwm(self): values = [0.1, 0, 7.7, 0, 9.999, 42, 0, 1, 9, 1] weights = [0, 7.5, 0.33, 23.23, 0, 4, 7, 8, 9, 0] np.testing.assert_array_equal( py_mwm_1d(values, weights, 5), moving_weighted_median(values, weights, 5))
def test_1d_mwm_int(self): values = [0, 0, 7, 0, 9, 42, 0, 1, 9, 1] weights = [0, 7, 1, 23, 0, 4, 7, 8, 9, 0] np.testing.assert_array_almost_equal( py_mwm_1d(values, weights, 5), moving_weighted_median(values, weights, 5))