Skip to content

Commit f574370

Browse files
committed
Merge pull request #20 from phobson/update-recipe
update recipe and tester
2 parents 9c0fdba + 03f5581 commit f574370

File tree

9 files changed

+143
-116
lines changed

9 files changed

+143
-116
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ install:
3636

3737
- conda create --yes -n test python=$TRAVIS_PYTHON_VERSION
3838
- source activate test
39-
- conda install --yes numpy matplotlib coverage docopt requests pyyaml
39+
- conda install --yes --channel=conda-forge numpy matplotlib coverage docopt requests pyyaml
4040
- conda install --yes --channel=conda-forge pytest pytest-cov pytest-mpl
4141
- if [ ${COVERAGE} = true ]; then conda install scipy --yes; fi
4242
- pip install coveralls

check_probscale.py

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

5-
import pytest
6-
status = pytest.main(sys.argv[1:])
5+
import probscale
6+
status = probscale.test(*sys.argv[1:])
77
sys.exit(status)

conda.recipe/meta.yaml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
package:
22
name: mpl-probscale
3-
version: 0.1.2dev
3+
version: 0.2.0dev
44

55
source:
66
path: ../
77

88
build:
99
script: python setup.py install
10-
number: 2
10+
number: 1
1111

1212
requirements:
1313
build:
1414
- python
15+
- setuptools
1516
- numpy
1617
- matplotlib
17-
- nose
1818

1919
run:
2020
- python
2121
- numpy
2222
- matplotlib
23-
- nose
23+
- pytest
2424

2525
test:
2626
imports:
2727
- probscale
2828

2929
commands:
30-
- python -c "import matplotlib; matplotlib.use('agg'); import probscale; probscale.test()"
30+
- python -c "import sys; import matplotlib as mpl; mpl.use('agg'); import probscale as ps; sys.exit(ps.test('--mpl'))"
3131

3232
requires:
33-
- nose
33+
- pytest
34+
- pytest-mpl
3435
- scipy
3536

3637
about:

probscale/formatters.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ def _sig_figs(cls, x, n, expthresh=5, forceint=False):
3737
"""
3838

3939
# return a string value unaltered
40-
if isinstance(x, str) or x == 0.0:
41-
out = str(x)
40+
if isinstance(x, str):
41+
out = cls._sig_figs(float(x), n, expthresh=expthresh, forceint=forceint)
42+
43+
elif x == 0.0:
44+
out = '0'
4245

4346
# check on the number provided
44-
elif x is not None and not numpy.isinf(x) and not numpy.isnan(x):
47+
elif x is not None and numpy.isfinite(x):
4548

4649
# check on the _sig_figs
4750
if n < 1:

probscale/tests/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1-
from numpy.testing import Tester
2-
test = Tester().test
1+
from pkg_resources import resource_filename
2+
3+
import pytest
4+
5+
import probscale
6+
7+
def test(*args):
8+
options = [resource_filename('probscale', 'tests')]
9+
options.extend(list(args))
10+
return pytest.main(options)
Loading

probscale/tests/test_formatters.py

Lines changed: 49 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,94 +6,58 @@
66
from probscale import formatters
77

88

9-
class Mixin_Check_Formatter_sig_figs(object):
10-
def teardown(self):
11-
pass
9+
@pytest.mark.parametrize("fmtr", [formatters.PctFormatter, formatters.ProbFormatter])
10+
def test_base_class_of_formatter(fmtr):
11+
assert issubclass(fmtr, formatters._FormatterMixin)
1212

13-
def test_baseline(self):
14-
assert self.fmt._sig_figs(self.x, 3) == self.known_3
15-
assert self.fmt._sig_figs(self.x, 4) == self.known_4
1613

17-
def test_string(self):
18-
assert self.fmt._sig_figs('1.23', 3) == '1.23'
19-
20-
def test_na_inf(self):
21-
assert self.fmt._sig_figs(numpy.nan, 3) == 'NA'
22-
assert self.fmt._sig_figs(numpy.inf, 3) == 'NA'
23-
24-
def test_zero(self):
25-
assert self.fmt._sig_figs(0, 3) == '0'
26-
27-
def test_trailing_zeros(self):
28-
assert self.fmt._sig_figs(self.x, 8) == self.known_8
29-
30-
def test_sigFigs_zero_n(self):
31-
with pytest.raises(ValueError):
32-
self.fmt._sig_figs(self.x, 0)
33-
34-
def test_sigFigs_negative_n(self):
35-
with pytest.raises(ValueError):
36-
self.fmt._sig_figs(self.x, -1)
37-
38-
def test_forceint(self):
39-
assert self.fmt._sig_figs(self.x, 3, forceint=True) == self.known_int
40-
41-
42-
class Mixin_Check_PctFormatter_sig_figs(Mixin_Check_Formatter_sig_figs):
14+
@pytest.mark.parametrize(('pct', 'expected'), [
15+
(0.0301, '0.03'), (0.20, '0.2'), (0.100, '0.1'),
16+
(10.000, '10'), (5.00, '5'), (50.00, '50'),
17+
(99.000, '99'), (99.1, '99.1'), (99.99, '99.99'),
18+
])
19+
def test__call___PctFormatter(pct, expected):
4320
fmt = formatters.PctFormatter()
44-
def test__call__(self):
45-
assert self.fmt(0.0301) == '0.03'
46-
assert self.fmt(0.2) == '0.2'
47-
assert self.fmt(0.1) == '0.1'
48-
assert self.fmt(10) == '10'
49-
assert self.fmt(5) == '5'
50-
assert self.fmt(50) == '50'
51-
assert self.fmt(99) == '99'
52-
assert self.fmt(99.1) == '99.1'
53-
assert self.fmt(99.99) == '99.99'
21+
assert fmt(pct) == expected
5422

5523

56-
class Mixin_Check_ProbFormatter_sig_figs(Mixin_Check_Formatter_sig_figs):
24+
@pytest.mark.parametrize(('prob', 'expected'), [
25+
(0.000301, '0.0003'), (0.001000, '0.001'), (0.100000, '0.10'),
26+
(0.050000, '0.05'), (0.500000, '0.50'), (0.990000, '0.99'),
27+
(0.991000, '0.991'), (0.999900, '0.9999'),
28+
])
29+
def test__call___ProbFormmater(prob, expected):
5730
fmt = formatters.ProbFormatter()
58-
def test__call__(self):
59-
assert self.fmt(0.000301) == '0.0003'
60-
assert self.fmt(0.001) == '0.001'
61-
assert self.fmt(0.10) == '0.10'
62-
assert self.fmt(0.05) == '0.05'
63-
assert self.fmt(0.50) == '0.50'
64-
assert self.fmt(0.99) == '0.99'
65-
assert self.fmt(0.991) == '0.991'
66-
assert self.fmt(0.9999) == '0.9999'
67-
68-
69-
class Test_PctFormatter_sig_figs_gt1(Mixin_Check_PctFormatter_sig_figs):
70-
def setup(self):
71-
self.x = 1234.56
72-
self.known_3 = '1,230'
73-
self.known_4 = '1,235'
74-
self.known_8 = '1,234.5600'
75-
self.known_exp3 = '1.23e+08'
76-
self.known_int = '1,235'
77-
self.factor = 10**5
78-
79-
80-
class Test_PctFormatter_sig_figs_lt1(Mixin_Check_PctFormatter_sig_figs):
81-
def setup(self):
82-
self.x = 0.123456
83-
self.known_3 = '0.123'
84-
self.known_4 = '0.1235'
85-
self.known_8 = '0.12345600'
86-
self.known_exp3 = '1.23e-07'
87-
self.known_int = '0'
88-
self.factor = 10**-6
89-
90-
91-
class Test_ProbFormatter_sig_figs(Mixin_Check_ProbFormatter_sig_figs):
92-
def setup(self):
93-
self.x = 0.123456
94-
self.known_3 = '0.123'
95-
self.known_4 = '0.1235'
96-
self.known_8 = '0.12345600'
97-
self.known_exp3 = '1.23e-07'
98-
self.known_int = '0'
99-
self.factor = 10**-6
31+
assert fmt(prob) == expected
32+
33+
34+
@pytest.mark.parametrize(('value', 'N', 'expected', 'forceint'), [
35+
(1234.56, 3, '1,230', False),
36+
(1234.56, 4, '1,235', False),
37+
('1.23', 3, '1.23', False),
38+
(numpy.nan, 3, 'NA', False),
39+
(numpy.inf, 3, 'NA', False),
40+
(0, 3, '0', False),
41+
(1234.56, 8, '1,234.5600', False),
42+
(1.23456e8, 3, '1.23e+08', False),
43+
(1234.56, 3, '1,235', True),
44+
(0.123456, 3, '0.123', False),
45+
(0.123456, 4, '0.1235', False),
46+
('0.123456', 3, '0.123', False),
47+
(numpy.nan, 3, 'NA', False),
48+
(numpy.inf, 3, 'NA', False),
49+
(0, 3, '0', False),
50+
(0.123456, 8, '0.12345600', False),
51+
(1.23456e-7, 3, '1.23e-07', False),
52+
(0.123456, 3, '0', True),
53+
])
54+
def test__sig_figs(value, N, expected, forceint):
55+
fmt = formatters._FormatterMixin()
56+
assert fmt._sig_figs(value, N, forceint=forceint) == expected
57+
58+
59+
@pytest.mark.parametrize('N', [-1, 0, 0.5])
60+
def test__sig_figs_errors(N):
61+
fmt = formatters._FormatterMixin()
62+
with pytest.raises(ValueError):
63+
fmt._sig_figs(1.23, N)

probscale/tests/test_probscale.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ def test_minimal_norm_cdf(mn, mn_input):
7070
assert numpy.all(numpy.abs(diff) < 0.001)
7171

7272

73-
@pytest.mark.mpl_image_compare(baseline_dir='baseline_images/test_probscale')
73+
@pytest.mark.mpl_image_compare(
74+
baseline_dir='baseline_images/test_probscale',
75+
tolerance=15
76+
)
7477
@pytest.mark.skipif(PYTHON27, reason="legacy python")
7578
def test_the_scale_default():
7679
fig, ax = plt.subplots(figsize=(4, 8))
@@ -80,15 +83,21 @@ def test_the_scale_default():
8083
return fig
8184

8285

83-
@pytest.mark.mpl_image_compare(baseline_dir='baseline_images/test_probscale')
86+
@pytest.mark.mpl_image_compare(
87+
baseline_dir='baseline_images/test_probscale',
88+
tolerance=15
89+
)
8490
def test_the_scale_not_as_pct():
8591
fig, ax = plt.subplots(figsize=(4, 8))
8692
ax.set_yscale('prob', as_pct=False)
8793
ax.set_ylim(0.02, 0.98)
8894
return fig
8995

9096

91-
@pytest.mark.mpl_image_compare(baseline_dir='baseline_images/test_probscale')
97+
@pytest.mark.mpl_image_compare(
98+
baseline_dir='baseline_images/test_probscale',
99+
tolerance=13
100+
)
92101
@pytest.mark.skipif(stats is None, reason="scipy not installed")
93102
def test_the_scale_beta():
94103
fig, ax = plt.subplots(figsize=(4, 8))

0 commit comments

Comments
 (0)