Skip to content

Commit 55b9106

Browse files
committed
switch to pytest from nosetests
1 parent 025f52d commit 55b9106

File tree

5 files changed

+302
-312
lines changed

5 files changed

+302
-312
lines changed

probscale/tests/test_formatters.py

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy
22

3-
import nose.tools as nt
3+
import pytest
44
import numpy.testing as nptest
55

66
from probscale import formatters
@@ -11,62 +11,59 @@ def teardown(self):
1111
pass
1212

1313
def test_baseline(self):
14-
nt.assert_equal(self.fmt._sig_figs(self.x, 3), self.known_3)
15-
nt.assert_equal(self.fmt._sig_figs(self.x, 4), self.known_4)
14+
assert self.fmt._sig_figs(self.x, 3) == self.known_3
15+
assert self.fmt._sig_figs(self.x, 4) == self.known_4
1616

1717
def test_string(self):
18-
nt.assert_equal(self.fmt._sig_figs('1.23', 3), '1.23')
18+
assert self.fmt._sig_figs('1.23', 3) == '1.23'
1919

2020
def test_na_inf(self):
21-
nt.assert_equal(self.fmt._sig_figs(numpy.nan, 3), 'NA')
22-
nt.assert_equal(self.fmt._sig_figs(numpy.inf, 3), 'NA')
21+
assert self.fmt._sig_figs(numpy.nan, 3) == 'NA'
22+
assert self.fmt._sig_figs(numpy.inf, 3) == 'NA'
2323

2424
def test_zero(self):
25-
nt.assert_equal(self.fmt._sig_figs(0, 3), '0')
25+
assert self.fmt._sig_figs(0, 3) == '0'
2626

2727
def test_trailing_zeros(self):
28-
nt.assert_equal(self.fmt._sig_figs(self.x, 8), self.known_8)
28+
assert self.fmt._sig_figs(self.x, 8) == self.known_8
2929

30-
@nptest.raises(ValueError)
3130
def test_sigFigs_zero_n(self):
32-
self.fmt._sig_figs(self.x, 0)
31+
with pytest.raises(ValueError):
32+
self.fmt._sig_figs(self.x, 0)
3333

34-
@nptest.raises(ValueError)
3534
def test_sigFigs_negative_n(self):
36-
self.fmt._sig_figs(self.x, -1)
35+
with pytest.raises(ValueError):
36+
self.fmt._sig_figs(self.x, -1)
3737

3838
def test_forceint(self):
39-
nt.assert_equal(
40-
self.fmt._sig_figs(self.x, 3, forceint=True),
41-
self.known_int
42-
)
39+
assert self.fmt._sig_figs(self.x, 3, forceint=True) == self.known_int
4340

4441

4542
class Mixin_Check_PctFormatter_sig_figs(Mixin_Check_Formatter_sig_figs):
4643
fmt = formatters.PctFormatter()
4744
def test__call__(self):
48-
nt.assert_equal(self.fmt(0.0301), '0.03')
49-
nt.assert_equal(self.fmt(0.2), '0.2')
50-
nt.assert_equal(self.fmt(0.1), '0.1')
51-
nt.assert_equal(self.fmt(10), '10')
52-
nt.assert_equal(self.fmt(5), '5')
53-
nt.assert_equal(self.fmt(50), '50')
54-
nt.assert_equal(self.fmt(99), '99')
55-
nt.assert_equal(self.fmt(99.1), '99.1')
56-
nt.assert_equal(self.fmt(99.99), '99.99')
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'
5754

5855

5956
class Mixin_Check_ProbFormatter_sig_figs(Mixin_Check_Formatter_sig_figs):
6057
fmt = formatters.ProbFormatter()
6158
def test__call__(self):
62-
nt.assert_equal(self.fmt(0.000301), '0.0003')
63-
nt.assert_equal(self.fmt(0.001), '0.001')
64-
nt.assert_equal(self.fmt(0.10), '0.10')
65-
nt.assert_equal(self.fmt(0.05), '0.05')
66-
nt.assert_equal(self.fmt(0.50), '0.50')
67-
nt.assert_equal(self.fmt(0.99), '0.99')
68-
nt.assert_equal(self.fmt(0.991), '0.991')
69-
nt.assert_equal(self.fmt(0.9999), '0.9999')
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'
7067

7168

7269
class Test_PctFormatter_sig_figs_gt1(Mixin_Check_PctFormatter_sig_figs):

probscale/tests/test_probscale.py

Lines changed: 65 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,98 @@
11
import sys
22
PYTHON27 = sys.version_info.major == 2
33

4-
import numpy as np
4+
import numpy
55
import matplotlib.pyplot as plt
66

77
try:
88
from scipy import stats
99
except:
1010
stats = None
1111

12-
from matplotlib.testing.decorators import image_comparison, cleanup
13-
import nose.tools as nt
14-
import numpy.testing as nptest
12+
import pytest
1513

1614
import probscale
1715
from probscale.probscale import _minimal_norm
1816

1917

20-
class Test__minimal_norm(object):
21-
def setup(self):
22-
self.mn = _minimal_norm()
23-
self.known__A = 0.1400122886866665
24-
25-
self.input = np.array([
26-
0.331, 0.742, 0.067, 0.826, 0.357, 0.089,
27-
0.754, 0.342, 0.762, 0.658, 0.239, 0.910,
28-
])
29-
30-
self.known_erf = np.array([
31-
0.36029027, 0.70598131, 0.07548843, 0.75724986,
32-
0.38635283, 0.10016122, 0.71371964, 0.37137355,
33-
0.71880142, 0.64791492, 0.26463458, 0.80188283,
34-
])
35-
36-
self.known_ppf = np.array([
37-
-0.43715354, 0.6495236 , -1.49851307, 0.93847570,
38-
-0.36648929, -1.34693863, 0.68713129, -0.40701088,
39-
0.71275076, 0.40701088, -0.70952297, 1.34075503,
40-
])
41-
42-
self.known_cdf = np.array([
43-
0.62967776, 0.77095633, 0.52670915, 0.79559795,
44-
0.63945410, 0.53545904, 0.77457539, 0.63382455,
45-
0.77697000, 0.74473093, 0.59444721, 0.81858875
46-
])
47-
48-
def test__A(self):
49-
nt.assert_true(hasattr(self.mn, '_A'))
50-
nt.assert_almost_equal(self.mn._A, self.known__A)
51-
52-
def test__approx_erf(self):
53-
nptest.assert_array_almost_equal(
54-
self.mn._approx_erf(self.input),
55-
self.known_erf,
56-
decimal=3
57-
)
58-
59-
def test__approx_inv_erf(self):
60-
nptest.assert_array_almost_equal(
61-
self.input,
62-
self.mn._approx_inv_erf(self.mn._approx_erf(self.input)),
63-
decimal=3
64-
)
65-
66-
def test_ppf(self):
67-
nptest.assert_array_almost_equal(
68-
self.mn.ppf(self.input),
69-
self.known_ppf,
70-
decimal=3
71-
)
72-
73-
def test_cdf(self):
74-
nptest.assert_array_almost_equal(
75-
self.mn.cdf(self.input),
76-
self.known_cdf,
77-
decimal=3
78-
)
79-
80-
81-
@image_comparison(baseline_images=['test_the_scale_default'], extensions=['png'])
82-
@nptest.dec.skipif(PYTHON27)
18+
@pytest.fixture
19+
def mn():
20+
return _minimal_norm()
21+
22+
23+
@pytest.fixture
24+
def mn_input():
25+
x = numpy.array([
26+
0.331, 0.742, 0.067, 0.826, 0.357, 0.089,
27+
0.754, 0.342, 0.762, 0.658, 0.239, 0.910,
28+
])
29+
return x
30+
31+
32+
def test_minimal_norm_A(mn):
33+
known__A = 0.1400122886866665
34+
assert abs(mn._A - known__A) < 0.0000001
35+
36+
37+
def test_minimal_norm__approx_erf(mn, mn_input):
38+
known_erf = numpy.array([
39+
0.36029027, 0.70598131, 0.07548843, 0.75724986,
40+
0.38635283, 0.10016122, 0.71371964, 0.37137355,
41+
0.71880142, 0.64791492, 0.26463458, 0.80188283,
42+
])
43+
44+
diff = mn._approx_erf(mn_input) - known_erf
45+
assert numpy.all(numpy.abs(diff) < 0.001)
46+
47+
48+
def test_minimal_norm__approx_inv_erf(mn, mn_input):
49+
diff = mn._approx_inv_erf(mn._approx_erf(mn_input)) - mn_input
50+
assert numpy.all(numpy.abs(diff) < 0.00001)
51+
52+
53+
def test_minimal_norm_ppf(mn, mn_input):
54+
known_ppf = numpy.array([
55+
-0.43715354, 0.6495236 , -1.49851307, 0.93847570,
56+
-0.36648929, -1.34693863, 0.68713129, -0.40701088,
57+
0.71275076, 0.40701088, -0.70952297, 1.34075503,
58+
])
59+
diff = mn.ppf(mn_input) - known_ppf
60+
assert numpy.all(numpy.abs(diff) < 0.001)
61+
62+
63+
def test_minimal_norm_cdf(mn, mn_input):
64+
known_cdf = numpy.array([
65+
0.62967776, 0.77095633, 0.52670915, 0.79559795,
66+
0.63945410, 0.53545904, 0.77457539, 0.63382455,
67+
0.77697000, 0.74473093, 0.59444721, 0.81858875
68+
])
69+
diff = mn.cdf(mn_input) - known_cdf
70+
assert numpy.all(numpy.abs(diff) < 0.001)
71+
72+
73+
@pytest.mark.mpl_image_compare(baseline_dir='baseline_images/test_probscale')
74+
@pytest.mark.skipif(PYTHON27, reason="legacy python")
8375
def test_the_scale_default():
8476
fig, ax = plt.subplots(figsize=(4, 8))
8577
ax.set_yscale('prob')
8678
ax.set_ylim(0.01, 99.99)
8779
fig.tight_layout()
80+
return fig
8881

8982

90-
@image_comparison(baseline_images=['test_the_scale_not_as_pct'], extensions=['png'])
83+
@pytest.mark.mpl_image_compare(baseline_dir='baseline_images/test_probscale')
9184
def test_the_scale_not_as_pct():
9285
fig, ax = plt.subplots(figsize=(4, 8))
9386
ax.set_yscale('prob', as_pct=False)
9487
ax.set_ylim(0.02, 0.98)
88+
return fig
9589

9690

97-
@image_comparison(baseline_images=['test_the_scale_beta'], extensions=['png'])
98-
@nptest.dec.skipif(stats is None)
91+
@pytest.mark.mpl_image_compare(baseline_dir='baseline_images/test_probscale')
92+
@pytest.mark.skipif(stats is None, reason="scipy not installed")
9993
def test_the_scale_beta():
10094
fig, ax = plt.subplots(figsize=(4, 8))
10195
ax.set_yscale('prob', as_pct=True, dist=stats.beta(3, 2))
10296
ax.set_ylim(1, 99)
10397
fig.tight_layout()
98+
return fig

probscale/tests/test_transforms.py

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
import numpy
2-
import matplotlib
3-
matplotlib.use('agg')
42

5-
import nose.tools as nt
3+
import pytest
64
import numpy.testing as nptest
75

86
from probscale.probscale import _minimal_norm
97
from probscale import transforms
108

119

1210
def test__mask_out_of_bounds():
13-
x = [-0.1, 0, 0.1, 0.5, 0.9, 1.0, 1.1]
1411
known = numpy.array([numpy.nan, numpy.nan, 0.1, 0.5, 0.9, numpy.nan, numpy.nan])
12+
x = [-0.1, 0, 0.1, 0.5, 0.9, 1.0, 1.1]
1513
result = transforms._mask_out_of_bounds(x)
16-
nptest.assert_array_almost_equal(result, known)
14+
nptest.assert_array_equal(known, result)
1715

1816

1917
def test__clip_out_of_bounds():
20-
x = [-0.1, 0, 0.1, 0.5, 0.9, 1.0, 1.1]
2118
known = numpy.array([0.0, 0.0, 0.1, 0.5, 0.9, 1.0, 1.0])
19+
x = [-0.1, 0, 0.1, 0.5, 0.9, 1.0, 1.1]
2220
result = transforms._clip_out_of_bounds(x)
23-
nptest.assert_array_almost_equal(result, known)
21+
diff = numpy.abs(result - known)
22+
assert numpy.all(diff < 0.0001)
2423

2524

2625
class Mixin_Transform(object):
@@ -30,38 +29,36 @@ class Mixin_Transform(object):
3029
known_has_inverse = True
3130

3231
def test_input_dims(self):
33-
nt.assert_true(hasattr(self.trans, 'input_dims'))
34-
nt.assert_equal(self.trans.input_dims, self.known_input_dims)
32+
assert hasattr(self.trans, 'input_dims')
33+
assert self.trans.input_dims == self.known_input_dims
3534

3635
def test_output_dims(self):
37-
nt.assert_true(hasattr(self.trans, 'output_dims'))
38-
nt.assert_equal(self.trans.output_dims, self.known_output_dims)
36+
assert hasattr(self.trans, 'output_dims')
37+
assert self.trans.output_dims == self.known_output_dims
3938

4039
def test_is_separable(self):
41-
nt.assert_true(hasattr(self.trans, 'is_separable'))
42-
nt.assert_equal(self.trans.is_separable, self.known_is_separable)
40+
assert hasattr(self.trans, 'is_separable')
41+
assert self.trans.is_separable == self.known_is_separable
4342

4443
def test_has_inverse(self):
45-
nt.assert_true(hasattr(self.trans, 'has_inverse'))
46-
nt.assert_equal(self.trans.has_inverse, self.known_has_inverse)
44+
assert hasattr(self.trans, 'has_inverse')
45+
assert self.trans.has_inverse == self.known_has_inverse
4746

4847
def test_dist(self):
49-
nt.assert_true(hasattr(self.trans, 'dist'))
50-
nt.assert_equal(self.trans.dist, _minimal_norm)
48+
assert hasattr(self.trans, 'dist')
49+
assert self.trans.dist == _minimal_norm
5150

5251
def test_transform_non_affine(self):
53-
nt.assert_true(hasattr(self.trans, 'transform_non_affine'))
54-
nptest.assert_almost_equal(self.trans.transform_non_affine([0.5]), self.known_tras_na)
52+
assert hasattr(self.trans, 'transform_non_affine')
53+
diff = numpy.abs(self.trans.transform_non_affine([0.5]) - self.known_tras_na)
54+
assert numpy.all(diff < 0.0001)
5555

5656
def test_inverted(self):
57-
nt.assert_true(hasattr(self.trans, 'inverted'))
57+
assert hasattr(self.trans, 'inverted')
5858

59-
@nt.raises(ValueError)
6059
def test_bad_non_pos(self):
61-
self._trans(_minimal_norm, nonpos='junk')
62-
63-
# def test_non_pos_default(self):
64-
# x = [-0.1, 0, 0.1, 0.5, 0.99, 1, 1.1]
60+
with pytest.raises(ValueError):
61+
self._trans(_minimal_norm, nonpos='junk')
6562

6663
def test_non_pos_clip(self):
6764
self._trans(_minimal_norm, nonpos='clip')
@@ -75,9 +72,9 @@ def setup(self):
7572

7673
def test_inverted(self):
7774
inv_trans = self.trans.inverted()
78-
nt.assert_equal(self.trans.dist, inv_trans.dist)
79-
nt.assert_equal(self.trans.factor, inv_trans.factor)
80-
nt.assert_equal(self.trans.nonpos, inv_trans.nonpos)
75+
assert self.trans.dist == inv_trans.dist
76+
assert self.trans.factor == inv_trans.factor
77+
assert self.trans.nonpos == inv_trans.nonpos
8178

8279

8380
class Test_QuantileTransform(Mixin_Transform):
@@ -88,6 +85,6 @@ def setup(self):
8885

8986
def test_inverted(self):
9087
inv_trans = self.trans.inverted()
91-
nt.assert_equal(self.trans.dist, inv_trans.dist)
92-
nt.assert_equal(self.trans.factor, inv_trans.factor)
93-
nt.assert_equal(self.trans.nonpos, inv_trans.nonpos)
88+
assert self.trans.dist == inv_trans.dist
89+
assert self.trans.factor == inv_trans.factor
90+
assert self.trans.nonpos == inv_trans.nonpos

0 commit comments

Comments
 (0)