Browse Source

tukey box plot tests

box plot tests updated according to the new return format of
_box_points() method
pull/226/head
Jan Richter 10 years ago
parent
commit
81e50cd834
  1. 4
      pygal/graph/box.py
  2. 63
      pygal/test/test_box.py

4
pygal/graph/box.py

@ -212,8 +212,8 @@ class Box(Graph):
else: # seq has an odd length
return seq[n // 2]
# sort the copy in case the originals must stay in original order
outliers = []
# sort the copy in case the originals must stay in original order
s = sorted([x for x in values if x is not None])
n = len(s)
if not n:
@ -252,8 +252,6 @@ class Box(Graph):
q0 = s[b0]
q4 = s[b4-1]
outliers = s[:b0] + s[b4:]
#print "Q: [%s,%s,%s,%s,%s] O: %s" \
# % (q0, q1, q2, q3, q4, outliers)
else:
q0 = q1 - 1.5 * iqr

63
pygal/test/test_box.py

@ -22,7 +22,7 @@ from pygal import Box as ghostedBox
def test_quartiles():
a = [-2.0, 3.0, 4.0, 5.0, 8.0] # odd test data
q0, q1, q2, q3, q4 = Box._box_points(a)
(min_s, q0, q1, q2, q3, q4, max_s), outliers = Box._box_points(a)
assert q1 == 7.0 / 4.0
assert q2 == 4.0
@ -31,17 +31,17 @@ def test_quartiles():
assert q4 == 23 / 4.0 + 6.0 # q3 + 1.5 * iqr
b = [1.0, 4.0, 6.0, 8.0] # even test data
q0, q1, q2, q3, q4 = Box._box_points(b)
(min_s, q0, q1, q2, q3, q4, max_s), outliers = Box._box_points(b)
assert q2 == 5.0
c = [2.0, None, 4.0, 6.0, None] # odd with None elements
q0, q1, q2, q3, q4 = Box._box_points(c)
(min_s, q0, q1, q2, q3, q4, max_s), outliers = Box._box_points(c)
assert q2 == 4.0
d = [4]
q0, q1, q2, q3, q4 = Box._box_points(d)
(min_s, q0, q1, q2, q3, q4, max_s), outliers = Box._box_points(d)
assert q0 == 4
assert q1 == 4
@ -49,9 +49,11 @@ def test_quartiles():
assert q3 == 4
assert q4 == 4
def test_quartiles_min_extremes():
a = [-2.0, 3.0, 4.0, 5.0, 8.0] # odd test data
q0, q1, q2, q3, q4 = Box._box_points(a, mode='extremes')
(min_s, q0, q1, q2, q3, q4, max_s), outliers = Box._box_points(
a, mode='extremes')
assert q1 == 7.0 / 4.0
assert q2 == 4.0
@ -60,17 +62,20 @@ def test_quartiles_min_extremes():
assert q4 == 8.0 # max
b = [1.0, 4.0, 6.0, 8.0] # even test data
q0, q1, q2, q3, q4 = Box._box_points(b, mode='extremes')
(min_s, q0, q1, q2, q3, q4, max_s), outliers = Box._box_points(
b, mode='extremes')
assert q2 == 5.0
c = [2.0, None, 4.0, 6.0, None] # odd with None elements
q0, q1, q2, q3, q4 = Box._box_points(c, mode='extremes')
(min_s, q0, q1, q2, q3, q4, max_s), outliers = Box._box_points(
c, mode='extremes')
assert q2 == 4.0
d = [4]
q0, q1, q2, q3, q4 = Box._box_points(d, mode='extremes')
(min_s, q0, q1, q2, q3, q4, max_s), outliers = Box._box_points(
d, mode='extremes')
assert q0 == 4
assert q1 == 4
@ -79,6 +84,48 @@ def test_quartiles_min_extremes():
assert q4 == 4
def test_quartiles_tukey():
a = [] # empty data
(min_s, q0, q1, q2, q3, q4, max_s), outliers = Box._box_points(
a, mode='tukey')
assert min_s == q0 == q1 == q2 == q3 == q4 == 0
assert outliers == []
# https://en.wikipedia.org/wiki/Quartile example 1
b = [6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49]
(min_s, q0, q1, q2, q3, q4, max_s), outliers = Box._box_points(
b, mode='tukey')
assert min_s == q0 == 6
assert q1 == 20.25
assert q2 == 40
assert q3 == 42.75
assert max_s == q4 == 49
assert outliers == []
# previous test with added outlier 75
c = [6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49, 75]
(min_s, q0, q1, q2, q3, q4, max_s), outliers = Box._box_points(
c, mode='tukey')
assert min_s == q0 == 6
assert q1 == 25.5
assert q2 == (40 + 41) / 2.0
assert q3 == 45
assert max_s == 75
assert outliers == [75]
# one more outlier, -30
c = [6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49, 75, 77]
(min_s, q0, q1, q2, q3, q4, max_s), outliers = Box._box_points(
c, mode='tukey')
assert min_s == q0 == 6
assert q1 == 30.75
assert q2 == 41
assert q3 == 47.5
assert max_s == 77
assert 75 in outliers
assert 77 in outliers
def test_simple_box():
box = ghostedBox()
box.add('test1', [-1, 2, 3, 3.1, 3.2, 4, 5])

Loading…
Cancel
Save