-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
test_distributions.py
341 lines (267 loc) · 11.8 KB
/
test_distributions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
import pytest
import nose.tools as nt
import numpy.testing as npt
from distutils.version import LooseVersion
from .. import distributions as dist
_no_statsmodels = not dist._has_statsmodels
if not _no_statsmodels:
import statsmodels
import statsmodels.nonparametric as smnp
_old_statsmodels = LooseVersion(statsmodels.__version__) < "0.11"
else:
_old_statsmodels = False
class TestDistPlot(object):
rs = np.random.RandomState(0)
x = rs.randn(100)
def test_hist_bins(self):
try:
fd_edges = np.histogram_bin_edges(self.x, "fd")
except AttributeError:
pytest.skip("Requires numpy >= 1.15")
ax = dist.distplot(self.x)
for edge, bar in zip(fd_edges, ax.patches):
assert pytest.approx(edge) == bar.get_x()
plt.close(ax.figure)
n = 25
n_edges = np.histogram_bin_edges(self.x, n)
ax = dist.distplot(self.x, bins=n)
for edge, bar in zip(n_edges, ax.patches):
assert pytest.approx(edge) == bar.get_x()
def test_elements(self):
n = 10
ax = dist.distplot(self.x, bins=n,
hist=True, kde=False, rug=False, fit=None)
assert len(ax.patches) == 10
assert len(ax.lines) == 0
assert len(ax.collections) == 0
plt.close(ax.figure)
ax = dist.distplot(self.x,
hist=False, kde=True, rug=False, fit=None)
assert len(ax.patches) == 0
assert len(ax.lines) == 1
assert len(ax.collections) == 0
plt.close(ax.figure)
ax = dist.distplot(self.x,
hist=False, kde=False, rug=True, fit=None)
assert len(ax.patches) == 0
assert len(ax.lines) == 0
assert len(ax.collections) == 1
plt.close(ax.figure)
ax = dist.distplot(self.x,
hist=False, kde=False, rug=False, fit=stats.norm)
assert len(ax.patches) == 0
assert len(ax.lines) == 1
assert len(ax.collections) == 0
def test_distplot_with_nans(self):
f, (ax1, ax2) = plt.subplots(2)
x_null = np.append(self.x, [np.nan])
dist.distplot(self.x, ax=ax1)
dist.distplot(x_null, ax=ax2)
line1 = ax1.lines[0]
line2 = ax2.lines[0]
assert np.array_equal(line1.get_xydata(), line2.get_xydata())
for bar1, bar2 in zip(ax1.patches, ax2.patches):
assert bar1.get_xy() == bar2.get_xy()
assert bar1.get_height() == bar2.get_height()
class TestKDE(object):
rs = np.random.RandomState(0)
x = rs.randn(50)
y = rs.randn(50)
kernel = "gau"
bw = "scott"
gridsize = 128
clip = (-np.inf, np.inf)
cut = 3
def test_scipy_univariate_kde(self):
"""Test the univariate KDE estimation with scipy."""
grid, y = dist._scipy_univariate_kde(self.x, self.bw, self.gridsize,
self.cut, self.clip)
nt.assert_equal(len(grid), self.gridsize)
nt.assert_equal(len(y), self.gridsize)
for bw in ["silverman", .2]:
dist._scipy_univariate_kde(self.x, bw, self.gridsize,
self.cut, self.clip)
@pytest.mark.skipif(_no_statsmodels, reason="no statsmodels")
def test_statsmodels_univariate_kde(self):
"""Test the univariate KDE estimation with statsmodels."""
grid, y = dist._statsmodels_univariate_kde(self.x, self.kernel,
self.bw, self.gridsize,
self.cut, self.clip)
nt.assert_equal(len(grid), self.gridsize)
nt.assert_equal(len(y), self.gridsize)
for bw in ["silverman", .2]:
dist._statsmodels_univariate_kde(self.x, self.kernel, bw,
self.gridsize, self.cut,
self.clip)
def test_scipy_bivariate_kde(self):
"""Test the bivariate KDE estimation with scipy."""
clip = [self.clip, self.clip]
x, y, z = dist._scipy_bivariate_kde(self.x, self.y, self.bw,
self.gridsize, self.cut, clip)
nt.assert_equal(x.shape, (self.gridsize, self.gridsize))
nt.assert_equal(y.shape, (self.gridsize, self.gridsize))
nt.assert_equal(len(z), self.gridsize)
# Test a specific bandwidth
clip = [self.clip, self.clip]
x, y, z = dist._scipy_bivariate_kde(self.x, self.y, 1,
self.gridsize, self.cut, clip)
# Test that we get an error with an invalid bandwidth
with nt.assert_raises(ValueError):
dist._scipy_bivariate_kde(self.x, self.y, (1, 2),
self.gridsize, self.cut, clip)
@pytest.mark.skipif(_no_statsmodels, reason="no statsmodels")
def test_statsmodels_bivariate_kde(self):
"""Test the bivariate KDE estimation with statsmodels."""
clip = [self.clip, self.clip]
x, y, z = dist._statsmodels_bivariate_kde(self.x, self.y, self.bw,
self.gridsize,
self.cut, clip)
nt.assert_equal(x.shape, (self.gridsize, self.gridsize))
nt.assert_equal(y.shape, (self.gridsize, self.gridsize))
nt.assert_equal(len(z), self.gridsize)
@pytest.mark.skipif(_no_statsmodels, reason="no statsmodels")
def test_statsmodels_kde_cumulative(self):
"""Test computation of cumulative KDE."""
grid, y = dist._statsmodels_univariate_kde(self.x, self.kernel,
self.bw, self.gridsize,
self.cut, self.clip,
cumulative=True)
nt.assert_equal(len(grid), self.gridsize)
nt.assert_equal(len(y), self.gridsize)
# make sure y is monotonically increasing
npt.assert_((np.diff(y) > 0).all())
def test_kde_cummulative_2d(self):
"""Check error if args indicate bivariate KDE and cumulative."""
with npt.assert_raises(TypeError):
dist.kdeplot(self.x, data2=self.y, cumulative=True)
def test_kde_singular(self):
with pytest.warns(UserWarning):
ax = dist.kdeplot(np.ones(10))
line = ax.lines[0]
assert not line.get_xydata().size
with pytest.warns(UserWarning):
ax = dist.kdeplot(np.ones(10) * np.nan)
line = ax.lines[1]
assert not line.get_xydata().size
@pytest.mark.skipif(_no_statsmodels or _old_statsmodels,
reason="no statsmodels or statsmodels without issue")
def test_statsmodels_zero_bandwidth(self):
"""Test handling of 0 bandwidth data in statsmodels."""
x = np.zeros(100)
x[0] = 1
try:
smnp.kde.bandwidths.select_bandwidth(x, "scott", "gau")
except RuntimeError:
# Only execute the actual test in the except clause, this should
# keep the test from failing in the future if statsmodels changes
# it's behavior to avoid raising the error itself.
# Track at https://github.com/statsmodels/statsmodels/issues/5419
with pytest.warns(UserWarning):
ax = dist.kdeplot(x)
line = ax.lines[0]
assert not line.get_xydata().size
@pytest.mark.parametrize("cumulative", [True, False])
def test_kdeplot_with_nans(self, cumulative):
if cumulative and _no_statsmodels:
pytest.skip("no statsmodels")
x_missing = np.append(self.x, [np.nan, np.nan])
f, ax = plt.subplots()
dist.kdeplot(self.x, cumulative=cumulative)
dist.kdeplot(x_missing, cumulative=cumulative)
line1, line2 = ax.lines
assert np.array_equal(line1.get_xydata(), line2.get_xydata())
def test_bivariate_kde_series(self):
df = pd.DataFrame({'x': self.x, 'y': self.y})
ax_series = dist.kdeplot(df.x, df.y)
ax_values = dist.kdeplot(df.x.values, df.y.values)
nt.assert_equal(len(ax_series.collections),
len(ax_values.collections))
nt.assert_equal(ax_series.collections[0].get_paths(),
ax_values.collections[0].get_paths())
def test_bivariate_kde_colorbar(self):
f, ax = plt.subplots()
dist.kdeplot(self.x, self.y,
cbar=True, cbar_kws=dict(label="density"),
ax=ax)
nt.assert_equal(len(f.axes), 2)
nt.assert_equal(f.axes[1].get_ylabel(), "density")
def test_legend(self):
f, ax = plt.subplots()
dist.kdeplot(self.x, self.y, label="test1")
line = ax.lines[-1]
assert line.get_label() == "test1"
f, ax = plt.subplots()
dist.kdeplot(self.x, self.y, shade=True, label="test2")
fill = ax.collections[-1]
assert fill.get_label() == "test2"
def test_contour_color(self):
rgb = (.1, .5, .7)
f, ax = plt.subplots()
dist.kdeplot(self.x, self.y, color=rgb)
contour = ax.collections[-1]
assert np.array_equal(contour.get_color()[0, :3], rgb)
low = ax.collections[0].get_color().mean()
high = ax.collections[-1].get_color().mean()
assert low < high
f, ax = plt.subplots()
dist.kdeplot(self.x, self.y, shade=True, color=rgb)
contour = ax.collections[-1]
low = ax.collections[0].get_facecolor().mean()
high = ax.collections[-1].get_facecolor().mean()
assert low > high
f, ax = plt.subplots()
dist.kdeplot(self.x, self.y, shade=True, colors=[rgb])
for level in ax.collections:
level_rgb = tuple(level.get_facecolor().squeeze()[:3])
assert level_rgb == rgb
class TestRugPlot(object):
@pytest.fixture
def list_data(self):
return np.random.randn(20).tolist()
@pytest.fixture
def array_data(self):
return np.random.randn(20)
@pytest.fixture
def series_data(self):
return pd.Series(np.random.randn(20))
def test_rugplot(self, list_data, array_data, series_data):
h = .1
for data in [list_data, array_data, series_data]:
f, ax = plt.subplots()
dist.rugplot(data, h)
rug, = ax.collections
segments = np.array(rug.get_segments())
assert len(segments) == len(data)
assert np.array_equal(segments[:, 0, 0], data)
assert np.array_equal(segments[:, 1, 0], data)
assert np.array_equal(segments[:, 0, 1], np.zeros_like(data))
assert np.array_equal(segments[:, 1, 1], np.ones_like(data) * h)
plt.close(f)
f, ax = plt.subplots()
dist.rugplot(data, h, axis="y")
rug, = ax.collections
segments = np.array(rug.get_segments())
assert len(segments) == len(data)
assert np.array_equal(segments[:, 0, 1], data)
assert np.array_equal(segments[:, 1, 1], data)
assert np.array_equal(segments[:, 0, 0], np.zeros_like(data))
assert np.array_equal(segments[:, 1, 0], np.ones_like(data) * h)
plt.close(f)
f, ax = plt.subplots()
dist.rugplot(data, axis="y")
dist.rugplot(data, vertical=True)
c1, c2 = ax.collections
assert np.array_equal(c1.get_segments(), c2.get_segments())
plt.close(f)
f, ax = plt.subplots()
dist.rugplot(data)
dist.rugplot(data, lw=2)
dist.rugplot(data, linewidth=3, alpha=.5)
for c, lw in zip(ax.collections, [1, 2, 3]):
assert np.squeeze(c.get_linewidth()).item() == lw
assert c.get_alpha() == .5
plt.close(f)