Skip to content

Commit

Permalink
ran black v20 (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyod committed Aug 31, 2020
1 parent 966d13c commit 0f90bd8
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 21 deletions.
2 changes: 1 addition & 1 deletion KDEpy/FFTKDE.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class FFTKDE(BaseKDE):
London ; New York: Chapman and Hall/CRC, 1995. Pages 182-192.
- Statsmodels implementation, at
``statsmodels.nonparametric.kde.KDEUnivariate``.
"""

def __init__(self, kernel="gaussian", bw=1, norm=2):
Expand Down
5 changes: 4 additions & 1 deletion KDEpy/binning.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,10 @@ def linbin_Ndim_python(data, grid_points, weights=None):
# Compute integer part and fractional part for every x_i
# Compute relation to previous grid point, and next grid point
int_frac = (
((int(coordinate), 1 - (coordinate % 1)), (int(coordinate) + 1, (coordinate % 1)),)
(
(int(coordinate), 1 - (coordinate % 1)),
(int(coordinate) + 1, (coordinate % 1)),
)
for coordinate in observation
)

Expand Down
53 changes: 47 additions & 6 deletions KDEpy/tests/test_NaiveKDE.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,41 @@ def test_additivity_with_weights(data, split_index):
@pytest.mark.parametrize(
"kernel, bw, n, expected_result",
[
("box", 0.1, 5, np.array([2.101278e-19, 3.469447e-18, 1.924501e00, 0.000000e00, 9.622504e-01]),),
("box", 0.2, 5, np.array([3.854941e-18, 2.929755e-17, 9.622504e-01, 0.000000e00, 4.811252e-01]),),
(
"box",
0.1,
5,
np.array([2.101278e-19, 3.469447e-18, 1.924501e00, 0.000000e00, 9.622504e-01]),
),
(
"box",
0.2,
5,
np.array([3.854941e-18, 2.929755e-17, 9.622504e-01, 0.000000e00, 4.811252e-01]),
),
("box", 0.6, 3, np.array([0.1603751, 0.4811252, 0.4811252])),
("tri", 0.6, 3, np.array([0.1298519, 0.5098009, 0.3865535])),
("epa", 0.1, 6, np.array([0.000000e00, 7.285839e-17, 2.251871e-01, 1.119926e00, 0.000000e00, 1.118034e00,]),),
("biweight", 2, 5, np.array([0.1524078, 0.1655184, 0.1729870, 0.1743973, 0.1696706]),),
(
"epa",
0.1,
6,
np.array(
[
0.000000e00,
7.285839e-17,
2.251871e-01,
1.119926e00,
0.000000e00,
1.118034e00,
]
),
),
(
"biweight",
2,
5,
np.array([0.1524078, 0.1655184, 0.1729870, 0.1743973, 0.1696706]),
),
],
)
def test_against_R_density(kernel, bw, n, expected_result):
Expand All @@ -95,7 +124,19 @@ def test_against_R_density(kernel, bw, n, expected_result):
"bw, n, expected_result",
[
(1, 3, np.array([0.17127129, 0.34595518, 0.30233275])),
(0.1, 5, np.array([2.56493684e-22, 4.97598466e-06, 2.13637668e00, 4.56012216e-04, 1.32980760e00,]),),
(
0.1,
5,
np.array(
[
2.56493684e-22,
4.97598466e-06,
2.13637668e00,
4.56012216e-04,
1.32980760e00,
]
),
),
(0.01, 3, np.array([0.0, 13.29807601, 13.29807601])),
],
)
Expand Down Expand Up @@ -124,7 +165,7 @@ def test_constant_values_silverman():
Test that a data set with constant values does not fail when using silverman's rule.
Tests with "almost" constant values should also get a bw assigned automatically,
although silverman's rule technically does not do this.
https://github.com/tommyod/KDEpy/issues/9
"""

Expand Down
51 changes: 46 additions & 5 deletions KDEpy/tests/test_TreeKDE.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,41 @@ def test_additivity_with_weights(data, split_index):
@pytest.mark.parametrize(
"kernel, bw, n, expected_result",
[
("box", 0.1, 5, np.array([2.101278e-19, 3.469447e-18, 1.924501e00, 0.000000e00, 9.622504e-01]),),
("box", 0.2, 5, np.array([3.854941e-18, 2.929755e-17, 9.622504e-01, 0.000000e00, 4.811252e-01]),),
(
"box",
0.1,
5,
np.array([2.101278e-19, 3.469447e-18, 1.924501e00, 0.000000e00, 9.622504e-01]),
),
(
"box",
0.2,
5,
np.array([3.854941e-18, 2.929755e-17, 9.622504e-01, 0.000000e00, 4.811252e-01]),
),
("box", 0.6, 3, np.array([0.1603751, 0.4811252, 0.4811252])),
("tri", 0.6, 3, np.array([0.1298519, 0.5098009, 0.3865535])),
("epa", 0.1, 6, np.array([0.000000e00, 7.285839e-17, 2.251871e-01, 1.119926e00, 0.000000e00, 1.118034e00,]),),
("biweight", 2, 5, np.array([0.1524078, 0.1655184, 0.1729870, 0.1743973, 0.1696706]),),
(
"epa",
0.1,
6,
np.array(
[
0.000000e00,
7.285839e-17,
2.251871e-01,
1.119926e00,
0.000000e00,
1.118034e00,
]
),
),
(
"biweight",
2,
5,
np.array([0.1524078, 0.1655184, 0.1729870, 0.1743973, 0.1696706]),
),
],
)
def test_against_R_density(kernel, bw, n, expected_result):
Expand All @@ -92,7 +121,19 @@ def test_against_R_density(kernel, bw, n, expected_result):
"bw, n, expected_result",
[
(1, 3, np.array([0.17127129, 0.34595518, 0.30233275])),
(0.1, 5, np.array([2.56493684e-22, 4.97598466e-06, 2.13637668e00, 4.56012216e-04, 1.32980760e00,]),),
(
0.1,
5,
np.array(
[
2.56493684e-22,
4.97598466e-06,
2.13637668e00,
4.56012216e-04,
1.32980760e00,
]
),
),
(0.01, 3, np.array([0.0, 13.29807601, 13.29807601])),
],
)
Expand Down
10 changes: 8 additions & 2 deletions KDEpy/tests/test_binning.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ class TestBinningFunctions:
@pytest.mark.parametrize(
"data, func",
itertools.product(
[[1, 2, 3, 4, 5, 6], [0.04, 0.54, 0.33, 0.85, 0.16], [-4.12, 0.98, -4.3, -1.85], [0, 0, 1],],
[
[1, 2, 3, 4, 5, 6],
[0.04, 0.54, 0.33, 0.85, 0.16],
[-4.12, 0.98, -4.3, -1.85],
[0, 0, 1],
],
[linear_binning, linbin_Ndim_python],
),
)
Expand Down Expand Up @@ -105,7 +110,8 @@ def test_binning_simple_examples(self, data, weights, ans):
assert np.allclose(y, ans)

@pytest.mark.parametrize(
"dims, use_weights, eq_grid", itertools.product([1, 2, 3, 4], [True, False], [True, False]),
"dims, use_weights, eq_grid",
itertools.product([1, 2, 3, 4], [True, False], [True, False]),
)
def test_cython_binning(self, dims, use_weights, eq_grid):
"""
Expand Down
14 changes: 12 additions & 2 deletions KDEpy/tests/test_kernel_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@

class TestKernelHelperFunctions:
@pytest.mark.parametrize(
"dim, expected", [(0, 1.25331), (1, 1), (2, 1.25331), (3, 2), (4, 3.75994), (5, 8), (6, 18.7997),],
"dim, expected",
[
(0, 1.25331),
(1, 1),
(2, 1.25331),
(3, 2),
(4, 3.75994),
(5, 8),
(6, 18.7997),
],
)
def test_gauss_integral(self, dim, expected):
"""
Expand Down Expand Up @@ -49,7 +58,8 @@ def test_integral_unity(self, fname, function):
assert np.isclose(integral, 1)

@pytest.mark.parametrize(
"p, kernel_name", itertools.product([0.5, 1, 1.5, 2, 13, np.inf], ["triweight", "gaussian"]),
"p, kernel_name",
itertools.product([0.5, 1, 1.5, 2, 13, np.inf], ["triweight", "gaussian"]),
)
def test_integral_unity_2D_many_p_norms(self, p, kernel_name):
"""
Expand Down
14 changes: 10 additions & 4 deletions KDEpy/tests/test_sorted_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TestGridSorted:
def test_regression_issue_grids(self):
"""
This test is based on Issue 15, raised by @blasern.
https://github.com/tommyod/KDEpy/issues/15
"""

Expand Down Expand Up @@ -42,7 +42,7 @@ def test_regression_issue_grids(self):
def test_regression_issue_code(self):
"""
This test is based on Issue 15, raised by @blasern. Tests the full code.
https://github.com/tommyod/KDEpy/issues/15
"""
# imports
Expand Down Expand Up @@ -71,7 +71,10 @@ def test_on_good_grids(self):
grid = np.array([[0], [0], [0], [1], [1], [1], [2], [2], [2]], dtype=float)
assert grid_is_sorted(grid)

grid = np.array([[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]], dtype=float,)
grid = np.array(
[[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]],
dtype=float,
)
assert grid_is_sorted(grid)

grid = np.array([[1, 1], [2, 2], [3, 3]], dtype=float)
Expand Down Expand Up @@ -300,7 +303,10 @@ def test_on_bad_grids(self):
grid = np.array([[0], [0], [2], [1], [1], [1], [2], [2], [2]], dtype=float)
assert not grid_is_sorted(grid)

grid = np.array([[0, 0], [0, 1], [0, 2], [1, 0], [1, 2], [1, 1], [2, 0], [2, 1], [2, 2]], dtype=float,)
grid = np.array(
[[0, 0], [0, 1], [0, 2], [1, 0], [1, 2], [1, 1], [2, 0], [2, 1], [2, 2]],
dtype=float,
)
assert not grid_is_sorted(grid)

grid = np.array([[1, 1], [3, 3], [2, 2]], dtype=float)
Expand Down

0 comments on commit 0f90bd8

Please sign in to comment.