def test_invalid_curve(self): """Check for exception on invalid curve""" with self.assertRaises(Exception) as e: gridpp.apply_curve([0, 1], [1, 2, 3], [1, 2], gridpp.OneToOne, gridpp.OneToOne) with self.assertRaises(Exception) as e: gridpp.apply_curve([0, 1], [1, 2], [1, 2, 3], gridpp.OneToOne, gridpp.OneToOne)
def test_all_extrapolation_policies(self): """Check that all policies work""" for policy in [ gridpp.OneToOne, gridpp.Zero, gridpp.NearestSlope, gridpp.MeanSlope, gridpp.Unchanged ]: curve_fcst = np.random.rand(3, 2, 4) curve_ref = np.random.rand(3, 2, 4) field = np.random.rand(3, 2) gridpp.apply_curve(field, curve_ref, curve_fcst, policy, policy)
def test_train(self): refs = [[1, 2, 3], [1, 1, 1]] fcst = [2, 3, 4] for ref in refs: new_fcst = gridpp.apply_curve(fcst, ref, fcst, gridpp.OneToOne, gridpp.OneToOne) np.testing.assert_array_equal(ref, new_fcst)
def test_negative(self): ref = [1, 0, -1] fcst = [2, 3, 4] curve_ref, curve_fcst = gridpp.quantile_mapping_curve(ref, fcst) np.testing.assert_array_equal(curve_fcst, fcst) np.testing.assert_array_equal(curve_ref, np.sort(ref)) new_fcst = gridpp.apply_curve(fcst, curve_ref, curve_fcst, gridpp.OneToOne, gridpp.OneToOne) np.testing.assert_array_equal(np.sort(ref), new_fcst)
def test_edge(self): """Check values on edge of curve""" x = [1, 2, 3] y = [2, 3, 4] policies = [ gridpp.OneToOne, gridpp.Zero, gridpp.MeanSlope, gridpp.NearestSlope ] for val in [1, 3]: for policy in policies: output = gridpp.apply_curve([val], y, x, policy, policy) np.testing.assert_array_equal(output, [val + 1])
def test_extrapolation(self): """Check values outside curve""" x = [1, 2, 3] y = [2, 5, 6] policies = [ gridpp.OneToOne, gridpp.Zero, gridpp.MeanSlope, gridpp.NearestSlope ] np.testing.assert_array_equal( gridpp.apply_curve([0, 4], y, x, gridpp.OneToOne, gridpp.OneToOne), [1, 7]) np.testing.assert_array_equal( gridpp.apply_curve([0, 4], y, x, gridpp.Zero, gridpp.Zero), [2, 6]) np.testing.assert_array_equal( gridpp.apply_curve([0, 4], y, x, gridpp.MeanSlope, gridpp.MeanSlope), [0, 8]) np.testing.assert_array_equal( gridpp.apply_curve([0, 4], y, x, gridpp.NearestSlope, gridpp.NearestSlope), [-1, 7]) np.testing.assert_array_equal( gridpp.apply_curve([0, 4], y, x, gridpp.Unchanged, gridpp.Unchanged), [0, 4])
def test_empty_curve(self): """Check for exception on empty curve""" with self.assertRaises(Exception) as e: gridpp.apply_curve([0, 1], [[], []], gridpp.OneToOne, gridpp.OneToOne) with self.assertRaises(Exception) as e: gridpp.apply_curve([0, 1], [[1, 2], []], gridpp.OneToOne, gridpp.OneToOne) with self.assertRaises(Exception) as e: gridpp.apply_curve([0, 1], [[], [1, 2]], gridpp.OneToOne, gridpp.OneToOne)
def test_3d(self): curve_fcst = np.random.rand(3, 2, 4) curve_ref = np.random.rand(3, 2, 4) field = np.random.rand(3, 2) gridpp.apply_curve(field, curve_ref, curve_fcst, gridpp.OneToOne, gridpp.OneToOne)
def test_empty_fcst(self): """Check for empty result on empty input""" q = gridpp.apply_curve([], [1, 2], [1, 2], gridpp.OneToOne, gridpp.OneToOne) np.testing.assert_array_equal(q, [])