Skip to content

Commit 091ac55

Browse files
Merge pull request AustinRochford#12 from AustinRochford/_-private-functions
Prefix private functions with _
2 parents 7d4ca2c + 684396d commit 091ac55

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

pycebox/ice.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pandas as pd
99

1010

11-
def get_grid_points(x, num_grid_points):
11+
def _get_grid_points(x, num_grid_points):
1212
if num_grid_points is None:
1313
return x.unique()
1414
else:
@@ -18,7 +18,7 @@ def get_grid_points(x, num_grid_points):
1818
return x.quantile(np.linspace(0, 1, num_grid_points)).unique()
1919

2020

21-
def get_point_x_ilocs(grid_index, data_index):
21+
def _get_point_x_ilocs(grid_index, data_index):
2222
data_level = 'data_{}'.format(grid_index.name)
2323

2424
return (np.abs(np.subtract
@@ -27,7 +27,7 @@ def get_point_x_ilocs(grid_index, data_index):
2727
.argmin(axis=0))
2828

2929

30-
def get_quantiles(x):
30+
def _get_quantiles(x):
3131
return np.greater.outer(x, x).sum(axis=1) / x.size
3232

3333

@@ -41,8 +41,8 @@ def ice(data, column, predict, num_grid_points=None):
4141
If num_grid_points is None, column varies over its unique values in data.
4242
If num_grid_points is not None, column varies over num_grid_pts values, evenly spaced at its quantiles in data.
4343
"""
44-
x_s = get_grid_points(data[column], num_grid_points)
45-
ice_data, orig_column = to_ice_data(data, column, x_s)
44+
x_s = _get_grid_points(data[column], num_grid_points)
45+
ice_data, orig_column = _to_ice_data(data, column, x_s)
4646
ice_data['ice_y'] = predict(ice_data.values)
4747
ice_data['data_{}'.format(column)] = orig_column
4848

@@ -90,7 +90,7 @@ def ice_plot(ice_data, frac_to_plot=1.,
9090
ice_data = ice_data.sort_index()
9191

9292
if centered:
93-
quantiles = get_quantiles(ice_data.index)
93+
quantiles = __get_quantiles(ice_data.index)
9494
centered_quantile_iloc = np.abs(quantiles - centered_quantile).argmin()
9595
ice_data = ice_data - ice_data.iloc[centered_quantile_iloc]
9696

@@ -103,12 +103,12 @@ def ice_plot(ice_data, frac_to_plot=1.,
103103

104104

105105
if x_quantile:
106-
x = get_quantiles(ice_data.index)
106+
x = __get_quantiles(ice_data.index)
107107
else:
108108
x = ice_data.index
109109

110110
if plot_points:
111-
point_x_ilocs = get_point_x_ilocs(plot_ice_data.index, plot_ice_data.columns)
111+
point_x_ilocs = _get_point_x_ilocs(plot_ice_data.index, plot_ice_data.columns)
112112
point_x = x[point_x_ilocs]
113113
point_y = plot_ice_data.values[point_x_ilocs, np.arange(point_x_ilocs.size)]
114114

@@ -151,7 +151,7 @@ def pdp(ice_data):
151151
return ice_data.mean(axis=1)
152152

153153

154-
def to_ice_data(data, column, x_s):
154+
def _to_ice_data(data, column, x_s):
155155
"""
156156
Create the DataFrame necessary for ICE calculations
157157
"""

test/test.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,39 @@ def compare_with_NaN(x, y):
1818

1919

2020
# tests
21-
def test_get_grid_points_num_grid_points():
21+
def test__get_grid_points_num_grid_points():
2222
x = pd.Series(np.array([0, 0, 1, 2, 3, 4, 5, 6, 7]))
2323
expected_grid_points = np.array([0, 1, 3, 5, 7])
2424

25-
assert (expected_grid_points == ice.get_grid_points(x, 5)).all()
25+
assert (expected_grid_points == ice._get_grid_points(x, 5)).all()
2626

2727

28-
def test_get_grid_points_num_grid_points_too_many():
28+
def test__get_grid_points_num_grid_points_too_many():
2929
x = pd.Series(np.array([0, 0, 1]))
3030
expected_grid_points = np.array([0, 0.5, 1])
3131

32-
assert (expected_grid_points == ice.get_grid_points(x, 5)).all()
32+
assert (expected_grid_points == ice._get_grid_points(x, 5)).all()
3333

3434

3535
@given(st.lists(st.floats()))
36-
def test_get_grid_points_num_grid_points_None(l):
36+
def test__get_grid_points_num_grid_points_None(l):
3737
x = pd.Series(l)
3838

39-
assert compare_with_NaN(x.unique(), ice.get_grid_points(x, None)).all()
39+
assert compare_with_NaN(x.unique(), ice._get_grid_points(x, None)).all()
4040

4141

4242
@given((st.tuples(st.integers(min_value=2, max_value=10))
4343
.flatmap(lambda size: arrays(np.float64, size))),
4444
(st.tuples(st.integers(min_value=2, max_value=10))
4545
.flatmap(lambda size: arrays(np.float64, size))))
46-
def test_get_point_x_ilocs(grid, data):
46+
def test__get_point_x_ilocs(grid, data):
4747
assume(np.isfinite(grid).all())
4848
assume(np.isfinite(data).all())
4949

5050
grid_index = pd.Float64Index(grid, name='x')
5151
data_index = pd.Float64Index(data, name='data_x')
5252

53-
point_x_ilocs = ice.get_point_x_ilocs(grid_index, data_index)
53+
point_x_ilocs = ice._get_point_x_ilocs(grid_index, data_index)
5454

5555
diffs = np.subtract.outer(grid, data)
5656

@@ -118,14 +118,14 @@ def test_pdp(args):
118118
assert compare_with_NaN(pdp, pdp_expected).all()
119119

120120

121-
def test_to_ice_data():
121+
def test__to_ice_data():
122122
X = np.array([[1, 2, 3],
123123
[4, 5, 6],
124124
[7, 8, 9]])
125125
data = pd.DataFrame(X, columns=['x1', 'x2', 'x3'])
126126
x_s = np.array([10, 11])
127127

128-
ice_data, orig_column = ice.to_ice_data(data, 'x3', x_s)
128+
ice_data, orig_column = ice._to_ice_data(data, 'x3', x_s)
129129
ice_data_expected = pd.DataFrame(np.array([[1, 2, 10],
130130
[1, 2, 11],
131131
[4, 5, 10],
@@ -144,12 +144,12 @@ def test_to_ice_data():
144144
.flatmap(lambda size: arrays(np.float64, (1,) + size))),
145145
(st.tuples(st.integers(min_value=1, max_value=10))
146146
.flatmap(lambda shape: arrays(np.float64, shape))))
147-
def test_to_ice_data_one_sample(X, x_s):
147+
def test__to_ice_data_one_sample(X, x_s):
148148
n_cols = X.shape[1]
149149
columns = ['x{}'.format(i) for i in range(n_cols)]
150150
data = pd.DataFrame(X, columns=list(columns))
151151

152-
ice_data, orig_column = ice.to_ice_data(data, 'x1', x_s)
152+
ice_data, orig_column = ice._to_ice_data(data, 'x1', x_s)
153153

154154
ice_data_expected_values = np.repeat(X, x_s.size, axis=0)
155155
ice_data_expected_values[:, 1] = x_s
@@ -165,14 +165,14 @@ def test_to_ice_data_one_sample(X, x_s):
165165
st.integers(min_value=2, max_value=10))
166166
.flatmap(lambda shape: arrays(np.float64, shape))),
167167
st.floats())
168-
def test_to_ice_data_one_test_point(l, x_s):
168+
def test__to_ice_data_one_test_point(l, x_s):
169169
X = np.array(l)
170170
n_cols = X.shape[1]
171171
columns = ['x{}'.format(i) for i in range(n_cols)]
172172
data = pd.DataFrame(X, columns=columns)
173173
x_s = np.array(x_s)
174174

175-
ice_data, orig_column = ice.to_ice_data(data, 'x0', x_s)
175+
ice_data, orig_column = ice._to_ice_data(data, 'x0', x_s)
176176

177177
ice_data_expected_values = X.copy()
178178
ice_data_expected_values[:, 0] = x_s

0 commit comments

Comments
 (0)