def test_focalmax(self): np.random.seed(0) a = np.random.rand(100, 100) self.assertTrue( np.allclose( rolling_window(a, 3).max(axis=(2, 3)), focal_statistics(a, window_size=3, func="max")[1:-1, 1:-1]))
def test_focalnanmax_reduce(self): np.random.seed(0) a = np.random.rand(5, 5) max = a.max() a[1, 1] = np.nan self.assertTrue( focal_statistics(a, window_size=5, func="max", reduce=True) == max)
def test_focalnanmin(self): np.random.seed(0) a = np.random.rand(5, 5) min = a.min() a[1, 1] = np.nan self.assertTrue( focal_statistics(a, window_size=5, func="min")[2, 2] == min)
def test_focalmean_reduce(self): np.random.seed(0) a = np.random.rand(100, 100) self.assertTrue( np.allclose( rolling_window(a, 5, reduce=True).mean(axis=(2, 3)), focal_statistics(a, window_size=5, func="mean", reduce=True)))
def test_focalstd_df_1(self): np.random.seed(0) a = np.random.rand(100, 100) self.assertTrue( np.allclose( rolling_window(a, 3).std(axis=(2, 3), ddof=1), focal_statistics(a, window_size=3, func="std", std_df=1)[1:-1, 1:-1]))
def test_errors(self): np.random.seed(0) a = np.random.rand(100, 100) with self.assertRaises(IndexError): focal_statistics(a, window_size=101, func="max") with self.assertRaises(ValueError): focal_statistics(a, window_size=3, fraction_accepted=1.1, func="max") with self.assertRaises(TypeError): focal_statistics(a, window_size="a", func="max") with self.assertRaises(ValueError): focal_statistics(np.random.rand(100, 100, 100), window_size=3, func="max") with self.assertRaises(ValueError): focal_statistics(a, window_size=9, reduce=True, func="max")
def test_focalmean_reduce_fraction_accepted_1(self): np.random.seed(0) a = np.random.rand(5, 5) # return value when all values are present self.assertTrue( np.allclose( focal_statistics(a, window_size=5, func="mean", reduce=True, fraction_accepted=1), np.mean(rolling_window(a, 5).mean(axis=(2, 3))))) a[3, 3] = np.nan # return nan when only one value is missing self.assertTrue( np.allclose(focal_statistics(a, window_size=5, func="mean", reduce=True, fraction_accepted=1), np.array([[np.nan]]), equal_nan=True))
def test_focalmean_reduce_fraction_accepted_0(self): np.random.seed(0) a = np.full((5, 5), np.nan) # return nan when nothing is present self.assertTrue( np.allclose(focal_statistics(a, window_size=5, func="mean", reduce=True, fraction_accepted=0), np.array([[np.nan]]), equal_nan=True)) # return value if only one value is present a[3, 3] = 10 self.assertTrue( np.allclose( focal_statistics(a, window_size=5, func="mean", reduce=True, fraction_accepted=0), np.nanmean(rolling_window(a, 5, reduce=True))))