Skip to content

Commit 2faf8ed

Browse files
bzahseberg
authored andcommitted
TST: Add parametrize for interpolation methods
1 parent 98cf811 commit 2faf8ed

File tree

1 file changed

+43
-91
lines changed

1 file changed

+43
-91
lines changed

numpy/lib/tests/test_function_base.py

Lines changed: 43 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,98 +2914,50 @@ def test_linear_nan_1D(self, dtype):
29142914
np.testing.assert_equal(res, np.NAN)
29152915
np.testing.assert_equal(res.dtype, arr.dtype)
29162916

2917-
TYPE_CODES = np.typecodes["AllInteger"] + np.typecodes["AllFloat"] + "O"
2918-
2919-
@pytest.mark.parametrize("dtype", TYPE_CODES)
2920-
def test_linear_inverted_cdf(self, dtype):
2921-
# METHOD 1 of H&F
2922-
arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype)
2923-
res = np.percentile(
2924-
arr,
2925-
40.0,
2926-
interpolation="inverted_cdf")
2927-
np.testing.assert_almost_equal(res, 20, 15)
2928-
2929-
@pytest.mark.parametrize("dtype", TYPE_CODES)
2930-
def test_linear_averaged_inverted_cdf(self, dtype):
2931-
# METHOD 2 of H&F
2932-
arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype)
2933-
res = np.percentile(
2934-
arr,
2935-
40.0,
2936-
interpolation="averaged_inverted_cdf")
2937-
np.testing.assert_almost_equal(res, 27.5, 15)
2938-
2939-
@pytest.mark.parametrize("dtype", TYPE_CODES)
2940-
def test_linear_closest_observation(self, dtype):
2941-
# METHOD 3 of H&F
2942-
arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype)
2943-
res = np.percentile(
2944-
arr,
2945-
40.0,
2946-
interpolation="closest_observation")
2947-
np.testing.assert_almost_equal(res, 20, 15)
2948-
2949-
@pytest.mark.parametrize("dtype", TYPE_CODES)
2950-
def test_linear_interpolated_inverted_cdf(self, dtype):
2951-
# METHOD 4 of H&F
2952-
arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype)
2953-
res = np.percentile(
2954-
arr,
2955-
40.0,
2956-
interpolation="interpolated_inverted_cdf")
2957-
np.testing.assert_almost_equal(res, 20, 15)
2958-
2959-
@pytest.mark.parametrize("dtype", TYPE_CODES)
2960-
def test_linear_hazen(self, dtype):
2961-
# METHOD 5 of H&F
2962-
arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype)
2963-
res = np.percentile(
2964-
arr,
2965-
40.0,
2966-
interpolation="hazen")
2967-
np.testing.assert_almost_equal(res, 27.5, 15)
2968-
2969-
@pytest.mark.parametrize("dtype", TYPE_CODES)
2970-
def test_linear_weibull(self, dtype):
2971-
# METHOD 6 of H&F
2972-
arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype)
2973-
res = np.percentile(
2974-
arr,
2975-
40.0,
2976-
interpolation="weibull")
2977-
np.testing.assert_almost_equal(res, 26, 15)
2978-
2979-
@pytest.mark.parametrize("dtype", TYPE_CODES)
2980-
def test_linear_linear(self, dtype):
2981-
# METHOD 7 of H&F
2982-
# Test defaults
2983-
assert_equal(np.percentile(range(10), 50), 4.5)
2984-
# explicit interpolation_method (the default)
2985-
res = np.percentile([15.0, 20.0, 35.0, 40.0, 50.0],
2986-
40,
2987-
interpolation="linear")
2988-
np.testing.assert_almost_equal(res, 29, 15)
2989-
2990-
@pytest.mark.parametrize("dtype", TYPE_CODES)
2991-
def test_linear_median_unbiased(self, dtype):
2992-
# METHOD 8 of H&F
2993-
arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype)
2994-
res = np.percentile(
2995-
arr,
2996-
40.0,
2997-
interpolation="median_unbiased")
2998-
np.testing.assert_almost_equal(res, 27, 14)
2917+
H_F_TYPE_CODES = [(int_type, np.float64)
2918+
for int_type in np.typecodes["AllInteger"]
2919+
] + [(np.float16, np.float64),
2920+
(np.float32, np.float64),
2921+
(np.float64, np.float64),
2922+
(np.float128, np.float128),
2923+
(np.complex64, np.complex128),
2924+
(np.complex128, np.complex128),
2925+
(np.complex256, np.complex256),
2926+
(np.dtype("O"), np.float64)]
2927+
2928+
@pytest.mark.parametrize(["input_dtype", "expected_dtype"], H_F_TYPE_CODES)
2929+
@pytest.mark.parametrize(["interpolation", "expected"],
2930+
[("inverted_cdf", 20),
2931+
("averaged_inverted_cdf", 27.5),
2932+
("closest_observation", 20),
2933+
("interpolated_inverted_cdf", 20),
2934+
("hazen", 27.5),
2935+
("weibull", 26),
2936+
("linear", 29),
2937+
("median_unbiased", 27),
2938+
("normal_unbiased", 27.125),
2939+
])
2940+
def test_linear_interpolation(self,
2941+
interpolation,
2942+
expected,
2943+
input_dtype,
2944+
expected_dtype):
2945+
arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=input_dtype)
2946+
actual = np.percentile(arr, 40.0, interpolation=interpolation)
2947+
2948+
np.testing.assert_almost_equal(actual, expected, 14)
2949+
2950+
if interpolation in ["inverted_cdf", "closest_observation"]:
2951+
if input_dtype == "O":
2952+
np.testing.assert_equal(np.asarray(actual).dtype, np.float64)
2953+
else:
2954+
np.testing.assert_equal(np.asarray(actual).dtype,
2955+
np.dtype(input_dtype))
2956+
else:
2957+
np.testing.assert_equal(np.asarray(actual).dtype,
2958+
np.dtype(expected_dtype))
29992959

3000-
@pytest.mark.parametrize("dtype", TYPE_CODES)
3001-
def test_linear_normal_unbiased(self, dtype):
3002-
# METHOD 9 of H&F
3003-
arr = np.asarray([15.0, 20.0, 35.0, 40.0, 50.0], dtype=dtype)
3004-
res = np.percentile(
3005-
arr,
3006-
40.0,
3007-
interpolation="normal_unbiased")
3008-
np.testing.assert_almost_equal(res, 27.125, 15)
2960+
TYPE_CODES = np.typecodes["AllInteger"] + np.typecodes["AllFloat"] + "O"
30092961

30102962
@pytest.mark.parametrize("dtype", TYPE_CODES)
30112963
def test_lower_higher(self, dtype):

0 commit comments

Comments
 (0)