Skip to content

Commit 0d82eca

Browse files
committed
Merge pull request #5 from phobson/y-oriented-tests
tests for when Y is the prob axis
2 parents b83fc04 + 206af54 commit 0d82eca

11 files changed

+112
-24
lines changed

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ install:
3434
- conda create --yes -n test python=$TRAVIS_PYTHON_VERSION
3535
- source activate test
3636
#- conda config --add channels phobson
37-
- conda install --yes numpy nose scipy
38-
- pip install matplotlib
39-
- conda install --yes coverage docopt requests pyyaml
37+
- conda install --yes numpy nose scipy matplotlib=1.4 coverage docopt requests pyyaml
4038
- pip install coveralls
4139
- pip install .
4240

probscale/probscale.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,25 @@ def _sig_figs(cls, x, n, expthresh=5, forceint=False):
8888
8989
"""
9090

91+
# return a string value unaltered
92+
if isinstance(x, str) or x == 0.0:
93+
out = str(x)
94+
9195
# check on the number provided
92-
if x is not None and not np.isinf(x) and not np.isnan(x):
96+
elif x is not None and not np.isinf(x) and not np.isnan(x):
9397

9498
# check on the _sig_figs
9599
if n < 1:
96100
raise ValueError("number of sig figs (n) must be greater than zero")
97101

98-
# return a string value unaltered
99-
if isinstance(x, str):
100-
out = x
101-
102102
elif forceint:
103103
out = '{:,.0f}'.format(x)
104104

105105
# logic to do all of the rounding
106-
elif x != 0.0:
106+
else:
107107
order = np.floor(np.log10(np.abs(x)))
108108

109-
if -1.0 * expthresh <= order <= expthresh:
109+
if (-1.0 * expthresh <= order <= expthresh):
110110
decimal_places = int(n - 1 - order)
111111

112112
if decimal_places <= 0:
@@ -121,9 +121,6 @@ def _sig_figs(cls, x, n, expthresh=5, forceint=False):
121121
fmt = '{0:.%de}' % decimal_places
122122
out = fmt.format(x)
123123

124-
else:
125-
out = str(round(x, n))
126-
127124
# with NAs and INFs, just return 'NA'
128125
else:
129126
out = 'NA'

probscale/tests/test_probscale/test_probscale.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ def test_baseline(self):
8181
nt.assert_equal(self.fmt._sig_figs(self.x, 3), self.known_3)
8282
nt.assert_equal(self.fmt._sig_figs(self.x, 4), self.known_4)
8383

84+
def test_string(self):
85+
nt.assert_equal(self.fmt._sig_figs('1.23', 3), '1.23')
86+
87+
def test_na_inf(self):
88+
nt.assert_equal(self.fmt._sig_figs(np.nan, 3), 'NA')
89+
nt.assert_equal(self.fmt._sig_figs(np.inf, 3), 'NA')
90+
91+
def test_zero(self):
92+
nt.assert_equal(self.fmt._sig_figs(0, 3), '0')
93+
8494
def test_trailing_zeros(self):
8595
nt.assert_equal(self.fmt._sig_figs(self.x, 8), self.known_8)
8696

probscale/tests/test_probscale/test_viz.py

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import matplotlib
1+
import matplotlib
22
matplotlib.use('agg')
33

44
import numpy as np
@@ -24,12 +24,12 @@ def setup_plot_data():
2424
return data
2525

2626

27-
@cleanup
2827
class Test__check_ax_obj(object):
2928
@nt.raises(ValueError)
3029
def test_bad_value(self):
3130
viz._check_ax_obj('junk')
3231

32+
@cleanup
3333
def test_with_ax(self):
3434
fig, ax = plt.subplots()
3535
fig1, ax1 = viz._check_ax_obj(ax)
@@ -38,15 +38,16 @@ def test_with_ax(self):
3838
nt.assert_true(ax1 is ax)
3939
nt.assert_true(fig1 is fig)
4040

41+
@cleanup
4142
def test_with_None(self):
4243
fig1, ax1 = viz._check_ax_obj(None)
4344
nt.assert_true(isinstance(ax1, plt.Axes))
4445
nt.assert_true(isinstance(fig1, plt.Figure))
4546

4647

47-
class Test__check_fig_arg(object):
48+
class Test__check_fit_arg(object):
4849
@nt.raises(ValueError)
49-
def test_bad_value(self):
50+
def test_bad_fitarg(self):
5051
viz._check_fit_arg('junk', 'fitprobs')
5152

5253
def test_x(self):
@@ -67,8 +68,8 @@ def test_None(self):
6768

6869

6970
class Test__check_ax_name(object):
70-
@nt.raises(ValueError)
71-
def test_bad_value(self):
71+
@nt.raises
72+
def test_bad_name(self):
7273
viz._check_fit_arg('junk', 'axname')
7374

7475
def test_x(self):
@@ -270,6 +271,17 @@ def test_linlog(self):
270271
)
271272

272273

274+
class Test__check_ax_type(object):
275+
@nt.raises(ValueError)
276+
def test_bad_value(self):
277+
viz._check_ax_type("JUNK")
278+
279+
def test_upper(self):
280+
nt.assert_equal('pp', viz._check_ax_type('PP'))
281+
nt.assert_equal('qq', viz._check_ax_type('QQ'))
282+
nt.assert_equal('prob', viz._check_ax_type('ProB'))
283+
284+
273285
@image_comparison(baseline_images=['test_probplot_prob'], extensions=['png'])
274286
def test_probplot_prob():
275287
fig, ax = plt.subplots()
@@ -319,3 +331,65 @@ def test_probplot_pp_bestfit():
319331
fig = viz.probplot(data, ax=ax, axtype='pp', otherscale='linear',
320332
xlabel='test x', bestfit=True, ylabel='test y',
321333
scatter_kws=scatter_kws, line_kws=line_kws)
334+
335+
336+
@image_comparison(baseline_images=['test_probplot_prob_probax_y'], extensions=['png'])
337+
def test_probplot_prob_probax_y():
338+
fig, ax = plt.subplots()
339+
data = setup_plot_data()
340+
fig = viz.probplot(data, ax=ax, xlabel='Test xlabel', otherscale='log', probax='y')
341+
nt.assert_true(isinstance(fig, plt.Figure))
342+
343+
344+
@image_comparison(baseline_images=['test_probplot_qq_probax_y'], extensions=['png'])
345+
def test_probplot_qq_probax_y():
346+
fig, ax = plt.subplots()
347+
data = setup_plot_data()
348+
fig = viz.probplot(data, ax=ax, axtype='qq', ylabel='Test label', probax='y',
349+
otherscale='log', scatter_kws=dict(color='r'))
350+
351+
352+
@image_comparison(baseline_images=['test_probplot_pp_probax_y'], extensions=['png'])
353+
def test_probplot_pp_probax_y():
354+
fig, ax = plt.subplots()
355+
data = setup_plot_data()
356+
scatter_kws = dict(color='b', linestyle='--', markeredgecolor='g', markerfacecolor='none')
357+
fig = viz.probplot(data, ax=ax, axtype='pp', otherscale='linear', probax='y',
358+
xlabel='test x', ylabel='test y', scatter_kws=scatter_kws)
359+
360+
361+
@image_comparison(baseline_images=['test_probplot_prob_bestfit_probax_y'], extensions=['png'])
362+
def test_probplot_prob_bestfit_probax_y():
363+
fig, ax = plt.subplots()
364+
data = setup_plot_data()
365+
fig = viz.probplot(data, ax=ax, xlabel='Test xlabel', bestfit=True, otherscale='log', probax='y')
366+
nt.assert_true(isinstance(fig, plt.Figure))
367+
368+
369+
@image_comparison(baseline_images=['test_probplot_qq_bestfit_probax_y'], extensions=['png'])
370+
def test_probplot_qq_bestfit_probax_y():
371+
fig, ax = plt.subplots()
372+
data = setup_plot_data()
373+
fig = viz.probplot(data, ax=ax, axtype='qq', bestfit=True, ylabel='Test label', otherscale='log', probax='y')
374+
375+
376+
@image_comparison(baseline_images=['test_probplot_pp_bestfit_probax_y'], extensions=['png'])
377+
def test_probplot_pp_bestfit_probax_y():
378+
fig, ax = plt.subplots()
379+
data = setup_plot_data()
380+
scatter_kws = {'marker': 's', 'color': 'red'}
381+
line_kws = {'linestyle': '--', 'linewidth': 3}
382+
fig = viz.probplot(data, ax=ax, axtype='pp', otherscale='linear', probax='y',
383+
xlabel='test x', bestfit=True, ylabel='test y',
384+
scatter_kws=scatter_kws, line_kws=line_kws)
385+
386+
387+
@cleanup
388+
def test_probplot_test_results():
389+
fig, ax = plt.subplots()
390+
data = setup_plot_data()
391+
fig, results = viz.probplot(data, return_results=True)
392+
393+
nt.assert_true(isinstance(results, dict))
394+
known_keys = sorted(['q', 'x', 'y', 'xhat', 'yhat', 'res'])
395+
nt.assert_list_equal(sorted(list(results.keys())), known_keys)

0 commit comments

Comments
 (0)