def check_for_pattern_noise(data, snr_threshold, pixel_threshold): """ Test for pattern noise in an image Parameters ---------- data : numpy array Image data to test for pattern noise snr_threshold : float Threshold for the Signal to Noise ratio in the power spectrum to be considered bad pixel_threshold : int Number of pixels that have to be above the S/N threshold for an image to be considered bad Returns ------- is_bad : bool Returns true if the image has pattern noise """ power = np.median(np.abs(np.fft.rfft2(data)), axis=0) snr = (power - np.median(power)) / median_absolute_deviation(power) # Throw away the first several elements of the snr because they are usually high # It is not clear exactly how many you should throw away, 15 seems to work snr = snr[15:] return (snr > snr_threshold).sum() >= pixel_threshold
def test_mad_axis_none_mask_none(): for i in range(25): size = np.random.randint(1, 10000) mean = np.random.uniform(-1000, 1000) sigma = np.random.uniform(0, 1000) a = np.random.normal(mean, sigma, size) expected = np.median(np.abs(a.astype(np.float32) - np.median(a.astype(np.float32)))) actual = stats.median_absolute_deviation(a) np.testing.assert_allclose(actual, np.float32(expected), atol=1e-4)
def test_mad_axis_none_mask_none(set_random_seed): for i in range(25): size = np.random.randint(1, 10000) mean = np.random.uniform(-1000, 1000) sigma = np.random.uniform(0, 1000) a = np.random.normal(mean, sigma, size) expected = np.median(np.abs(a.astype(np.float32) - np.median(a.astype(np.float32)))) actual = stats.median_absolute_deviation(a) np.testing.assert_allclose(actual, np.float32(expected), atol=1e-4)
def test_mad_2d_axis_1_mask_none(): for i in range(5): size1 = np.random.randint(1, 300) size2 = np.random.randint(1, 300) mean = np.random.uniform(-1000, 1000) sigma = np.random.uniform(0, 1000) a = np.random.normal(mean, sigma, size=(size1, size2)) expected = np.median(np.abs(a.astype(np.float32).T - np.median(a.astype(np.float32), axis=1)).T, axis=1) actual = stats.median_absolute_deviation(a, axis=1) np.testing.assert_allclose(actual, expected.astype(np.float32), atol=1e-4)
def test_mad_2d_axis_1_mask_none(set_random_seed): for i in range(5): size1 = np.random.randint(1, 300) size2 = np.random.randint(5, 300) mean = np.random.uniform(-1000, 1000) sigma = np.random.uniform(0, 1000) a = np.random.normal(mean, sigma, size=(size1, size2)) expected = np.median(np.abs(a.astype(np.float32).T - np.median(a.astype(np.float32), axis=1)).T, axis=1) actual = stats.median_absolute_deviation(a, axis=1) np.testing.assert_allclose(actual, expected.astype(np.float32), atol=1e-4)
def test_mad_3d_axis_0_mask_none(set_random_seed): for i in range(5): size1 = np.random.randint(5, 50) size2 = np.random.randint(1, 50) size3 = np.random.randint(1, 50) mean = np.random.uniform(-1000, 1000) sigma = np.random.uniform(0, 1000) a = np.random.normal(mean, sigma, size=(size1, size2, size3)) expected = np.median(np.abs(a.astype(np.float32) - np.median(a.astype(np.float32), axis=0)), axis=0) actual = stats.median_absolute_deviation(a, axis=0) np.testing.assert_allclose(actual, expected.astype(np.float32), atol=1e-4)
def test_mad_axis_none_mask(): for i in range(25): size = np.random.randint(1, 10000) mean = np.random.uniform(-1000, 1000) sigma = np.random.uniform(0, 1000) a = np.random.normal(mean, sigma, size) value_to_mask = np.random.uniform(0, 0.8) mask = np.random.uniform(0, 1.0, size) < value_to_mask a_masked = ma.array(a, mask=mask, dtype=np.float32) expected = ma.median(ma.array(np.abs(a_masked - ma.median(a_masked)), dtype=np.float32, mask=mask)) actual = stats.median_absolute_deviation(a, mask=mask) np.testing.assert_allclose(actual, np.float32(expected), atol=1e-4)
def test_mad_axis_none_mask(set_random_seed): for i in range(25): size = np.random.randint(1, 10000) mean = np.random.uniform(-1000, 1000) sigma = np.random.uniform(0, 1000) a = np.random.normal(mean, sigma, size) value_to_mask = np.random.uniform(0, 0.8) mask = np.random.uniform(0, 1.0, size) < value_to_mask a_masked = ma.array(a, mask=mask, dtype=np.float32) expected = ma.median(ma.array(np.abs(a_masked - ma.median(a_masked)), dtype=np.float32, mask=mask)) actual = stats.median_absolute_deviation(a, mask=mask) np.testing.assert_allclose(actual, np.float32(expected), atol=1e-4)
def test_mad_2d_axis_1_mask(): for i in range(5): size1 = np.random.randint(1, 300) size2 = np.random.randint(1, 300) mean = np.random.uniform(-1000, 1000) sigma = np.random.uniform(0, 1000) a = np.random.normal(mean, sigma, size=(size1, size2)) value_to_mask = np.random.uniform(0, 0.8) mask = np.random.uniform(0, 1.0, size=(size1, size2)) < value_to_mask a_masked = ma.array(a, mask=mask, dtype=np.float32) expected = ma.median(ma.array(np.abs(a.T - ma.median(a_masked, axis=1)).T, dtype=np.float32, mask=mask), axis=1) actual = stats.median_absolute_deviation(a, mask=mask, axis=1) np.testing.assert_allclose(actual, np.float32(expected), atol=1e-4)
def test_mad_2d_axis_1_mask(set_random_seed): for i in range(5): size1 = np.random.randint(1, 300) size2 = np.random.randint(5, 300) mean = np.random.uniform(-1000, 1000) sigma = np.random.uniform(0, 1000) a = np.random.normal(mean, sigma, size=(size1, size2)) value_to_mask = np.random.uniform(0, 0.8) mask = np.random.uniform(0, 1.0, size=(size1, size2)) < value_to_mask a_masked = ma.array(a, mask=mask, dtype=np.float32) expected = ma.median(ma.array(np.abs(a.T - ma.median(a_masked, axis=1)).T, dtype=np.float32, mask=mask), axis=1) actual = stats.median_absolute_deviation(a, mask=mask, axis=1) np.testing.assert_allclose(actual, np.float32(expected), atol=1e-4)
def test_mad_3d_axis_2_mask(): for i in range(5): size1 = np.random.randint(1, 50) size2 = np.random.randint(1, 50) size3 = np.random.randint(1, 50) mean = np.random.uniform(-1000, 1000) sigma = np.random.uniform(0, 1000) a = np.random.normal(mean, sigma, size=(size1, size2, size3)) value_to_mask = np.random.uniform(0, 0.8) mask = np.random.uniform(0, 1.0, size=(size1, size2, size3)) < value_to_mask a_masked = ma.array(a, mask=mask, dtype=np.float32) expected = ma.median(ma.array(np.abs(a - np.expand_dims(ma.median(a_masked, axis=2), axis=2)), dtype=np.float32, mask=mask), axis=2) actual = stats.median_absolute_deviation(a, mask=mask, axis=2) np.testing.assert_allclose(actual, expected.astype(np.float32), atol=1e-9)
def test_mad_3d_axis_2_mask(set_random_seed): for i in range(5): size1 = np.random.randint(1, 50) size2 = np.random.randint(1, 50) size3 = np.random.randint(5, 50) mean = np.random.uniform(-1000, 1000) sigma = np.random.uniform(0, 1000) a = np.random.normal(mean, sigma, size=(size1, size2, size3)) value_to_mask = np.random.uniform(0, 0.8) mask = np.random.uniform(0, 1.0, size=(size1, size2, size3)) < value_to_mask a_masked = ma.array(a, mask=mask, dtype=np.float32) expected = ma.median(ma.array(np.abs(a - np.expand_dims(ma.median(a_masked, axis=2), axis=2)), dtype=np.float32, mask=mask), axis=2) actual = stats.median_absolute_deviation(a, mask=mask, axis=2) np.testing.assert_allclose(actual, expected.astype(np.float32), atol=1e-9)
def test_mad_3d_axis_2_mask_none(set_random_seed): for i in range(5): size1 = np.random.randint(10, 50) size2 = np.random.randint(10, 50) size3 = np.random.randint(10, 50) mean = np.random.uniform(-1000, 1000) sigma = np.random.uniform(0, 1000) a = np.random.normal(mean, sigma, size=(size1, size2, size3)) b = a.copy() expected = np.median(np.abs(a.astype(np.float32) - np.median(a.astype(np.float32), axis=2).reshape(size1, size2, 1)), axis=2) actual = stats.median_absolute_deviation(b, axis=2) np.testing.assert_allclose(actual, expected, rtol=1e-5)
def test_mad_3d_axis_0_mask_none(): for i in range(5): size1 = np.random.randint(1, 50) size2 = np.random.randint(1, 50) size3 = np.random.randint(1, 50) mean = np.random.uniform(-1000, 1000) sigma = np.random.uniform(0, 1000) a = np.random.normal(mean, sigma, size=(size1, size2, size3)) expected = np.median(np.abs( a.astype(np.float32) - np.median(a.astype(np.float32), axis=0)), axis=0) actual = stats.median_absolute_deviation(a, axis=0) np.testing.assert_allclose(actual, expected.astype(np.float32), atol=1e-4)