Skip to content

Commit ac8ed07

Browse files
committed
add validation submodule
1 parent 61ce2e2 commit ac8ed07

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from matplotlib import pyplot
2+
3+
import nose.tools as nt
4+
from matplotlib.testing.decorators import cleanup
5+
6+
from probscale import validate
7+
8+
9+
class Test_axes_object(object):
10+
@nt.raises(ValueError)
11+
def test_bad_value(self):
12+
validate.axes_object('junk')
13+
14+
@cleanup
15+
def test_with_ax(self):
16+
fig, ax = pyplot.subplots()
17+
fig1, ax1 = validate.axes_object(ax)
18+
nt.assert_true(isinstance(ax1, pyplot.Axes))
19+
nt.assert_true(isinstance(fig1, pyplot.Figure))
20+
nt.assert_true(ax1 is ax)
21+
nt.assert_true(fig1 is fig)
22+
23+
@cleanup
24+
def test_with_None(self):
25+
fig1, ax1 = validate.axes_object(None)
26+
nt.assert_true(isinstance(ax1, pyplot.Axes))
27+
nt.assert_true(isinstance(fig1, pyplot.Figure))
28+
29+
30+
class Test_fit_argument(object):
31+
@nt.raises(ValueError)
32+
def test_bad_fitarg(self):
33+
validate.fit_argument('junk', 'fitprobs')
34+
35+
def test_x(self):
36+
nt.assert_equal('x', validate.fit_argument('x', 'fitprobs'))
37+
nt.assert_equal('x', validate.fit_argument('x', 'fitlogs'))
38+
39+
def test_y(self):
40+
nt.assert_equal('y', validate.fit_argument('y', 'fitprobs'))
41+
nt.assert_equal('y', validate.fit_argument('y', 'fitlogs'))
42+
43+
def test_both(self):
44+
nt.assert_equal('both', validate.fit_argument('both', 'fitprobs'))
45+
nt.assert_equal('both', validate.fit_argument('both', 'fitlogs'))
46+
47+
def test_None(self):
48+
nt.assert_true(validate.fit_argument(None, 'fitprobs') is None)
49+
nt.assert_true(validate.fit_argument(None, 'fitlogs') is None)
50+
51+
52+
class Test_axis_name(object):
53+
@nt.raises(ValueError)
54+
def test_bad_name(self):
55+
validate.axis_name('junk', 'axname')
56+
57+
def test_x(self):
58+
nt.assert_equal('x', validate.axis_name('x', 'axname'))
59+
60+
def test_y(self):
61+
nt.assert_equal('y', validate.axis_name('y', 'axname'))
62+
63+
64+
class Test_axis_type(object):
65+
@nt.raises(ValueError)
66+
def test_bad_value(self):
67+
validate.axis_type("JUNK")
68+
69+
def test_upper(self):
70+
nt.assert_equal('pp', validate.axis_type('PP'))
71+
nt.assert_equal('qq', validate.axis_type('QQ'))
72+
nt.assert_equal('prob', validate.axis_type('ProB'))

probscale/validate.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from matplotlib import pyplot
2+
3+
4+
def axes_object(ax):
5+
""" Checks if a value if an Axes. If None, a new one is created.
6+
7+
"""
8+
9+
if ax is None:
10+
fig, ax = pyplot.subplots()
11+
elif isinstance(ax, pyplot.Axes):
12+
fig = ax.figure
13+
else:
14+
msg = "`ax` must be a matplotlib Axes instance or None"
15+
raise ValueError(msg)
16+
17+
return fig, ax
18+
19+
20+
def axis_name(axname, argname):
21+
valid_args = ['x', 'y']
22+
if axname.lower() not in valid_args:
23+
msg = 'Invalid value for {} ({}). Must be on of {}.'
24+
raise ValueError(msg.format(argname, axname, valid_args))
25+
26+
return axname.lower()
27+
28+
29+
def fit_argument(arg, argname):
30+
valid_args = ['x', 'y', 'both', None]
31+
if arg not in valid_args:
32+
msg = 'Invalid value for {} ({}). Must be on of {}.'
33+
raise ValueError(msg.format(argname, arg, valid_args))
34+
35+
return arg
36+
37+
38+
def axis_type(axtype):
39+
if axtype.lower() not in ['pp', 'qq', 'prob']:
40+
raise ValueError("invalid axtype: {}".format(axtype))
41+
return axtype.lower()
42+
43+
44+

0 commit comments

Comments
 (0)