def test_max(self): output = gridpp.neighbourhood(values, 1, gridpp.Max) self.assertEqual(output[2][2], 18) output = gridpp.neighbourhood(values, 1, gridpp.Max) self.assertEqual(output[0][4], 9) output = gridpp.neighbourhood(values, 100, gridpp.Max) self.assertTrue((np.array(output) == 24).all())
def test_min(self): output = gridpp.neighbourhood(values, 1, gridpp.Min) self.assertEqual(output[2][2], 6) output = gridpp.neighbourhood(values, 1, gridpp.Min) self.assertEqual(output[0][4], 3) output = gridpp.neighbourhood(values, 100, gridpp.Min) self.assertTrue((np.array(output) == 0).all())
def test_mean(self): output = gridpp.neighbourhood(values, 1, gridpp.Mean) self.assertEqual(output[2][2], 12.5) self.assertAlmostEqual(output[0][4], 5.3333, 4) output = gridpp.neighbourhood(values, 100, gridpp.Mean) self.assertTrue((np.abs(np.array(output) - 12.086956) < 0.0001).all()) output = np.array(gridpp.neighbourhood(values, 0, gridpp.Mean)).flatten() I = np.where(np.isnan(output) == 0)[0] self.assertTrue((np.isnan(output) == np.isnan(values.flatten())).all()) self.assertTrue((output[I] == values.flatten()[I]).all())
def test_invalid_arguments(self): """Check that exception is thrown for invalid arguments""" field = np.ones([5, 5]) halfwidth = -1 stats = [gridpp.Mean, gridpp.Min, gridpp.Max, gridpp.Median] for stat in stats: for func in [gridpp.neighbourhood, gridpp.neighbourhood_brute_force]: with self.assertRaises(ValueError) as e: gridpp.neighbourhood(field, halfwidth, stat) # User should use the _quantile function for func in [gridpp.neighbourhood, gridpp.neighbourhood_brute_force]: with self.assertRaises(Exception) as e: gridpp.neighbourhood(field, 1, gridpp.Quantile)
def test_overflow(self): """ Check that mean is not affected by overflow """ N = int(1e3) values = np.array(np.arange(1, N)**3) values = np.expand_dims(values, 1) output = gridpp.neighbourhood(values, 0, gridpp.Mean) np.testing.assert_array_almost_equal(np.zeros(values.shape), output / values - 1, 6)
def test_missing(self): empty = np.zeros([5, 5]) empty[0:3, 0:3] = np.nan for statistic in [ gridpp.Mean, gridpp.Min, gridpp.Max, gridpp.Median, gridpp.Std, gridpp.Variance ]: output = gridpp.neighbourhood(empty, 1, statistic) self.assertTrue(np.isnan(np.array(output)[0:2, 0:2]).all())
def test_empty(self): for statistic in [ gridpp.Mean, gridpp.Min, gridpp.Max, gridpp.Median, gridpp.Std, gridpp.Variance ]: output = gridpp.neighbourhood([[]], 1, statistic) self.assertEqual(len(output.shape), 2) self.assertEqual(output.shape[0], 0) self.assertEqual(output.shape[1], 0) for quantile in np.arange(0.1, 0.9, 0.1): for num_thresholds in [1, 2]: thresholds = gridpp.get_neighbourhood_thresholds( values, num_thresholds) output = gridpp.neighbourhood_quantile_fast([[]], 0.9, 1, thresholds) self.assertEqual(len(output.shape), 2) self.assertEqual(output.shape[0], 0) self.assertEqual(output.shape[1], 0)
def test_memory_leak_input(self): """Checks if there is a memory leak when passing vectors """ N = 1000 last = None min = None max = None for i in range(5): vec = np.random.rand(N * N) vec2 = np.random.rand(N, N) output2 = gridpp.neighbourhood(vec2, 7, gridpp.Mean) output = gridpp.dewpoint(vec, vec) curr = memory_usage() if min is None or max is None: min = curr max = curr if curr < min: min = curr if curr > max: max = curr last = curr self.assertTrue(max / min < 2)
def test_mean(self): input = (np.random.rand(1000, 1000) > 0.5).astype(float) thresholds = [1, 5, 10] output = gridpp.neighbourhood(values, 7, gridpp.Mean)