1 |
# Copyright (c) 2002 by Intevation GmbH |
# Copyright (c) 2002, 2003 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# |
# |
16 |
import support |
import support |
17 |
support.initthuban() |
support.initthuban() |
18 |
|
|
19 |
from Thuban.Model.classgen import ClassGenerator |
from Thuban.Model.classgen import CalculateQuantiles |
20 |
from Thuban.Model.range import Range |
from Thuban.Model.range import Range |
21 |
|
|
22 |
class ClassGenTest(unittest.TestCase): |
class ClassGenTest(unittest.TestCase): |
26 |
|
|
27 |
eq = self.assertEquals |
eq = self.assertEquals |
28 |
|
|
|
cg = ClassGenerator() |
|
|
|
|
29 |
# |
# |
30 |
# Test CalculateQuantiles |
# Test CalculateQuantiles |
31 |
# |
# |
32 |
|
|
33 |
cq = cg.CalculateQuantiles |
cq = CalculateQuantiles |
34 |
|
|
35 |
result = cq([1, 2, 3, 4], [.25, .5, .75, 1.0], Range("[1;4]")) |
result = cq([1, 2, 3, 4], [.25, .5, .75, 1.0], Range("[1;4]")) |
36 |
eq(result, (0, [(0, .25), (1, .5), (2, .75), (3, 1.0)])) |
eq(result, (0, 0, 3, [(0, .25), (1, .5), (2, .75), (3, 1.0)])) |
37 |
|
|
38 |
result = cq(range(0, 100), [.25, .5, .75, 1.0], Range("[0;100]")) |
result = cq(range(0, 100), [.25, .5, .75, 1.0], Range("[0;100]")) |
39 |
eq(result, (0, [(24, .25), (49, .5), (74, .75), (99, 1.0)])) |
eq(result, (0, 0, 99, [(24, .25), (49, .5), (74, .75), (99, 1.0)])) |
40 |
|
|
41 |
result = cq(range(0, 100), [.33, .66, 1.0], Range("[0;100]")) |
result = cq(range(0, 100), [.33, .66, 1.0], Range("[0;100]")) |
42 |
eq(result, (0, [(32, .33), (65, .66), (99, 1.0)])) |
eq(result, (0, 0, 99, [(32, .33), (65, .66), (99, 1.0)])) |
43 |
|
|
44 |
# negative input |
# negative input |
45 |
result = cq(range(-100, 100), [.33, .66, 1.0], Range("[-100;100]")) |
result = cq(range(-100, 100), [.33, .66, 1.0], Range("[-100;100]")) |
46 |
eq(result, (0, [(65, .33), (131, .66), (199, 1.0)])) |
eq(result, (0, 0, 199, [(65, .33), (131, .66), (199, 1.0)])) |
47 |
|
|
48 |
# unequal percentiles |
# unequal percentiles |
49 |
result = cq(range(0, 100), [.25, .66, .8, 1.0], Range("[0;100]")) |
result = cq(range(0, 100), [.25, .66, .8, 1.0], Range("[0;100]")) |
50 |
eq(result, (0, [(24, .25), (65, .66), (79, .8), (99, 1.0)])) |
eq(result, (0, 0, 99, [(24, .25), (65, .66), (79, .8), (99, 1.0)])) |
51 |
|
|
52 |
# input all the same |
# input all the same |
53 |
result = cq([1, 1, 1, 1], [.25, .5, .75, 1.0], Range("[1;4]")) |
result = cq([1, 1, 1, 1], [.25, .5, .75, 1.0], Range("[1;4]")) |
54 |
eq(result, (0, [(3, 1.0)])) |
eq(result, (1, 0, 3, [(3, 1.0)])) |
55 |
|
|
56 |
# empty input |
# empty input |
57 |
result = cq([], [.25, .5, .75, 1.0], Range("[1;4]")) |
result = cq([], [.25, .5, .75, 1.0], Range("[1;4]")) |
58 |
eq(result, (0, [])) |
eq(result, None) |
59 |
|
|
60 |
# empty range |
# empty range |
61 |
result = cq([1, 2, 3, 4], [.25, .5, .75, 1.0], Range("]0;1[")) |
result = cq([1, 2, 3, 4], [.25, .5, .75, 1.0], Range("]0;1[")) |
62 |
eq(result, (0, [])) |
eq(result, None) |
63 |
|
|
64 |
# empty percentiles |
# empty percentiles |
65 |
result = cq([1, 2, 3, 4], [], Range("]0;1[")) |
result = cq([1, 2, 3, 4], [], Range("]0;1[")) |
66 |
eq(result, (0, [])) |
eq(result, None) |
67 |
|
|
68 |
# single percentile |
# single percentile |
69 |
result = cq([1, 2, 3, 4], [.5], Range("[0;4]")) |
result = cq([1, 2, 3, 4], [.5], Range("[0;4]")) |
70 |
eq(result, (0, [(1, .5)])) |
eq(result, (0, 0, 3, [(1, .5)])) |
71 |
|
|
72 |
# more percentiles than input |
# more percentiles than input |
73 |
result = cq([1], [.5, 1.0], Range("[0;4]")) |
result = cq([1], [.5, 1.0], Range("[0;4]")) |
74 |
eq(result, (1, [(0, 1.0)])) |
eq(result, (1, 0, 0, [(0, 1.0)])) |
75 |
|
|
76 |
result = cq([1], [.1, .2, .3, .4, .5, .6, .7, .8, .9, 1.0], Range("[0;4]")) |
result = cq([1], [.1, .2, .3, .4, .5, .6, .7, .8, .9, 1.0], Range("[0;4]")) |
77 |
eq(result, (1, [(0, 1.0)])) |
eq(result, (1, 0, 0, [(0, 1.0)])) |
78 |
|
|
79 |
# range smaller than the input |
# range smaller than the input |
80 |
result = cq([1, 2, 3, 4], [.5, 1.0], Range("[2;3]")) |
result = cq([1, 2, 3, 4], [.5, 1.0], Range("[2;3]")) |
81 |
eq(result, (0, [(1, .5), (2, 1.0)])) |
eq(result, (0, 1, 2, [(1, .5), (2, 1.0)])) |
82 |
|
|
83 |
# range outside the input |
# range outside the input |
84 |
result = cq([5, 6, 7, 8], [.5, 1.0], Range("[2;3]")) |
result = cq([5, 6, 7, 8], [.5, 1.0], Range("[2;3]")) |
85 |
eq(result, (0, [])) |
eq(result, None) |
86 |
|
|
87 |
result = cq([1, 1, 1, 1, 1, 1], [.25, .5, .75, 1.0], Range("[1;4]")) |
result = cq([1, 1, 1, 1, 1, 1], [.25, .5, .75, 1.0], Range("[1;4]")) |
88 |
eq(result, (1, [(5, 1.0)])) |
eq(result, (1, 0, 5, [(5, 1.0)])) |
89 |
|
|
90 |
result = cq([1, 1, 1, 1, 1, 2, 3], [.25, .5, .75, 1.0], Range("[1;4]")) |
result = cq([1, 1, 1, 1, 1, 2, 3], [.25, .5, .75, 1.0], Range("[1;4]")) |
91 |
eq(result, (1, [(4, 0.7142857142857143), # the algorithm generated |
eq(result, (1, 0, 6, |
92 |
(5, 0.8571428571428571), # these values, but they are |
[(4, 0.7142857142857143), # the algorithm generated |
93 |
(6, 1.0)])) # right. |
(5, 0.8571428571428571), # these values, but they are |
94 |
|
(6, 1.0)])) # right. |
95 |
|
|
96 |
# adjusted quantiles |
# adjusted quantiles |
97 |
result = cq([1, 1, 1, 1, 1, |
result = cq([1, 1, 1, 1, 1, |
100 |
4, 4, 4, 4, 4, |
4, 4, 4, 4, 4, |
101 |
5, 5, 5, 5, 5], |
5, 5, 5, 5, 5], |
102 |
[.12, .24, .36, .50, .62, .76, .88, 1.0], Range("[1;5]")) |
[.12, .24, .36, .50, .62, .76, .88, 1.0], Range("[1;5]")) |
103 |
eq(result, (1, [(4, .2), (9, .4), (14, .6), (19, .8), (24, 1.0)])) |
eq(result, (1, 0, 24, [(4, .2), (9, .4), (14, .6), (19, .8), (24, 1.0)])) |
104 |
|
|
105 |
if __name__ == "__main__": |
if __name__ == "__main__": |
106 |
unittest.main() |
unittest.main() |