/[thuban]/branches/WIP-pyshapelib-bramz/test/test_classgen.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/test/test_classgen.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 890 by jonathan, Fri May 9 18:08:31 2003 UTC revision 1166 by jonathan, Thu Jun 12 12:42:24 2003 UTC
# Line 1  Line 1 
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  #  #
# Line 16  import unittest Line 16  import unittest
16  import support  import support
17  support.initthuban()  support.initthuban()
18    
19  from Thuban.Model.classgen import ClassGenerator  from Thuban.Model.classgen import \
20        generate_singletons, \
21        generate_uniform_distribution, \
22        generate_quantiles, \
23        calculate_quantiles, \
24        GreyRamp
25  from Thuban.Model.range import Range  from Thuban.Model.range import Range
26    
27    from Thuban.Model.classification import ClassGroupRange
28    
29  class ClassGenTest(unittest.TestCase):  class ClassGenTest(unittest.TestCase):
30    
31      def test(self):      def doClassRangeTest(self, clazz, ranges):
32          """Test ClassGenerator methods"""          self.assertEquals(clazz.GetNumGroups(), len(ranges))
33            for i in range(len(ranges)):
34                group = clazz.GetGroup(i)
35                r1 = str(Range(ranges[i]))
36                r2 = group.GetRange()
37                self.assertEquals(r1, r2)
38    
39        def doClassSingleTest(self, clazz, _list):
40            self.assertEquals(clazz.GetNumGroups(), len(_list))
41            for i in range(len(_list)):
42                group = clazz.GetGroup(i)
43                self.assertEquals(group.GetValue(), _list[i])
44                
45        def test_generate_singletons(self):
46            """Test generate_singletons"""
47    
48          eq = self.assertEquals          eq = self.assertEquals
49            gs = generate_singletons
50            ramp = GreyRamp()
51    
52            _list = [1, 2, 3, 4]
53            cl = gs(_list, len(_list), ramp)
54            self.doClassSingleTest(cl, _list)
55    
56            _list = range(0, 100)
57            cl = gs(_list, len(_list), ramp)
58            self.doClassSingleTest(cl, _list)
59    
60            _list = range(-100, 100)
61            cl = gs(_list, len(_list), ramp)
62            self.doClassSingleTest(cl, _list)
63    
64            _list = ['a', 'b', 'c', 'd']
65            cl = gs(_list, len(_list), ramp)
66            self.doClassSingleTest(cl, _list)
67    
68            _list = []
69            cl = gs(_list, len(_list), ramp)
70            self.doClassSingleTest(cl, _list)
71    
72            _list = [1]
73            cl = gs(_list, len(_list), ramp)
74            self.doClassSingleTest(cl, _list)
75            
76    
77        def test_generate_uniform_distribution(self):
78            """Test generate_uniform_distribution"""
79    
80            eq = self.assertEquals
81            gud = generate_uniform_distribution
82            ramp = GreyRamp()
83    
84            cl = gud(0, 99, 10, ramp, True)
85            self.doClassRangeTest(cl, ("[0;10[", "[10;20[", "[20;30[", "[30;40[",
86                                  "[40;50[", "[50;60[", "[60;70[", "[70;80[",
87                                  "[80;90[", "[90;99]"))
88    
89            cl = gud(0, 99, 10, ramp, False)
90            self.doClassRangeTest(cl, ("[0;9.9[", "[9.9;19.8[", "[19.8;29.7[",
91                                  "[29.7;39.6[", "[39.6;49.5[", "[49.5;59.4[",
92                                  "[59.4;69.3[", "[69.3;79.2[", "[79.2;89.1[",
93                                  "[89.1;99.0]"))
94    
95          cg = ClassGenerator()          cl = gud(1, 2, 2, ramp, False)
96            self.doClassRangeTest(cl, ("[1;1.5[", "[1.5;2]"))
97    
98            cl = gud(1, 2, 2, ramp, True)
99            self.doClassRangeTest(cl, ("[1;2[", "[2;2]"))
100    
101    
102        def test_generate_quantiles(self):
103            """Test generate_quantiles"""
104    
105            eq = self.assertEquals
106    
107          #          #
108          # Test CalculateQuantiles          # Test calculate_quantiles
109          #          #
110    
111          cq = cg.CalculateQuantiles          gq = generate_quantiles
112    
113            ramp = GreyRamp()
114    
115            adj, cl = gq([1, 2, 3, 4], [.25, .5, .75, 1.0], ramp, Range("[1;4]"))
116            self.failIf(adj)
117            self.doClassRangeTest(cl, ("[1;1]", "]1;2]", "]2;3]", "]3;4]"))
118    
119            adj, cl = gq(range(0, 100), [.25, .5, .75, 1.0], ramp, Range("[0;100["))
120            self.failIf(adj)
121            self.doClassRangeTest(cl, ("[0;24]", "]24;49]", "]49;74]", "]74;100["))
122    
123            adj, cl = gq(range(0, 100), [.33, .66, 1.0], ramp, Range("[0;100]"))
124            self.failIf(adj)
125            self.doClassRangeTest(cl, ("[0;32]", "]32;65]", "]65;100]"))
126    
127            # negative input
128            adj,cl = gq(range(-100,100), [.33, .66, 1.0], ramp, Range("[-100;100]"))
129            self.failIf(adj)
130            self.doClassRangeTest(cl, ("[-100;-35]", "]-35;31]", "]31;100]"))
131    
132            # unequal percentiles
133            adj,cl = gq(range(0, 100), [.25, .66, .8, 1.0], ramp, Range("[0;100]"))
134            self.failIf(adj)
135            self.doClassRangeTest(cl, ("[0;24]", "]24;65]", "]65;79]", "]79;100]"))
136    
137            # input all the same
138            adj,cl = gq([1, 1, 1, 1], [.25, .5, .75, 1.0], ramp, Range("[1;4]"))
139            self.failUnless(adj)
140            self.doClassRangeTest(cl, ("[1;4]",))
141    
142            # empty input
143            adj,cl = gq([], [.25, .5, .75, 1.0], ramp, Range("[1;4]"))
144            self.failUnless(adj)
145            self.doClassRangeTest(cl, ())
146    
147            # empty range
148            adj,cl = gq([1, 2, 3, 4], [.25, .5, .75, 1.0], ramp, Range("]0;1["))
149            self.failUnless(adj)
150            self.doClassRangeTest(cl, ())
151    
152            # empty percentiles
153            self.assertRaises(ValueError, gq,
154                              [1, 2, 3, 4], [], ramp, Range("]0;1["))
155    
156            # single percentile
157            self.assertRaises(ValueError, gq,
158                              [1, 2, 3, 4], [.5], ramp, Range("[0;4]"))
159    
160            # more percentiles than input
161            adj,cl = gq([1], [.5, 1.0], ramp, Range("[0;4]"))
162            self.failUnless(adj)
163            self.doClassRangeTest(cl, ("[0;4]",))
164    
165            adj,cl = gq([1], [.1, .2, .3, .4, .5, .6, .7, .8, .9, 1.0], ramp, Range("[0;4]"))
166            self.failUnless(adj)
167            self.doClassRangeTest(cl, ("[0;4]",))
168    
169            # range smaller than the input
170            adj,cl = gq([1, 2, 3, 4], [.5, 1.0], ramp, Range("[2;3]"))
171            self.failIf(adj)
172            self.doClassRangeTest(cl, ("[2;2]","]2;3]"))
173    
174            # range outside the input
175            adj,cl = gq([5, 6, 7, 8], [.5, 1.0], ramp, Range("[2;3]"))
176            self.failUnless(adj)
177            self.doClassRangeTest(cl, ())
178    
179            adj,cl = gq([1, 1, 1, 1, 1, 1], [.25, .5, .75, 1.0], ramp, Range("[1;4]"))
180            self.failUnless(adj)
181            self.doClassRangeTest(cl, ("[1;4]",))
182    
183            adj,cl = gq([1, 1, 1, 1, 1, 2, 3], [.25, .5, .75, 1.0], ramp, Range("[1;4]"))
184            self.failUnless(adj)
185            self.doClassRangeTest(cl, ("[1;1]", "]1;2]", "]2;4]"))
186    
187            # adjusted quantiles
188            adj,cl = gq([1, 1, 1, 1, 1,
189                         2, 2, 2, 2, 2,
190                         3, 3, 3, 3, 3,
191                         4, 4, 4, 4, 4,
192                         5, 5, 5, 5, 5],
193                        [.12, .24, .36, .50, .62, .76, .88, 1.0], ramp, Range("[1;5]"))
194            
195            self.failUnless(adj)
196            self.doClassRangeTest(cl, ("[1;1]", "]1;2]", "]2;3]", "]3;4]", "]4;5]"))
197            
198        def test_calculate_quantiles(self):
199            """Test calculate_quantiles"""
200    
201            eq = self.assertEquals
202    
203            #
204            # Test calculate_quantiles
205            #
206    
207            cq = calculate_quantiles
208    
209          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]"))
210          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)]))
211    
212          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]"))
213          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)]))
214    
215          result = cq(range(0, 100), [.33, .66, 1.0], Range("[0;100]"))          result = cq(range(0, 100), [.33, .66, 1.0], Range("[0;100]"))
216          eq(result, (0, [(32, .33), (65, .66), (99, 1.0)]))          eq(result, (0, 0, 99, [(32, .33), (65, .66), (99, 1.0)]))
217    
218          # negative input          # negative input
219          result = cq(range(-100, 100), [.33, .66, 1.0], Range("[-100;100]"))          result = cq(range(-100, 100), [.33, .66, 1.0], Range("[-100;100]"))
220          eq(result, (0, [(65, .33), (131, .66), (199, 1.0)]))          eq(result, (0, 0, 199, [(65, .33), (131, .66), (199, 1.0)]))
221    
222          # unequal percentiles          # unequal percentiles
223          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]"))
224          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)]))
225    
226          # input all the same          # input all the same
227          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]"))
228          eq(result, (0, [(3, 1.0)]))          eq(result, (1, 0, 3, [(3, 1.0)]))
229    
230          # empty input          # empty input
231          result = cq([], [.25, .5, .75, 1.0], Range("[1;4]"))          result = cq([], [.25, .5, .75, 1.0], Range("[1;4]"))
232          eq(result, (0, []))          eq(result, None)
233    
234          # empty range          # empty range
235          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["))
236          eq(result, (0, []))          eq(result, None)
237    
238          # empty percentiles          # empty percentiles
239          result = cq([1, 2, 3, 4], [], Range("]0;1["))          self.assertRaises(ValueError, cq, [1, 2, 3, 4], [], Range("]0;1["))
         eq(result, (0, []))  
240    
241          # single percentile          # single percentile
242          result = cq([1, 2, 3, 4], [.5], Range("[0;4]"))          self.assertRaises(ValueError, cq, [1, 2, 3, 4], [.5], Range("[0;4]"))
243          eq(result, (0, [(1, .5)]))  
244            # percentile doesn't cover range
245            self.assertRaises(ValueError, cq, [1, 2, 3, 4], [.5,.8], Range("[0;4]"))
246    
247          # more percentiles than input          # more percentiles than input
248          result = cq([1], [.5, 1.0], Range("[0;4]"))          result = cq([1], [.5, 1.0], Range("[0;4]"))
249          eq(result, (1, [(0, 1.0)]))          eq(result, (1, 0, 0, [(0, 1.0)]))
250    
251          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]"))
252          eq(result, (1, [(0, 1.0)]))          eq(result, (1, 0, 0, [(0, 1.0)]))
253    
254          # range smaller than the input          # range smaller than the input
255          result = cq([1, 2, 3, 4], [.5, 1.0], Range("[2;3]"))          result = cq([1, 2, 3, 4], [.5, 1.0], Range("[2;3]"))
256          eq(result, (0, [(1, .5), (2, 1.0)]))          eq(result, (0, 1, 2, [(1, .5), (2, 1.0)]))
257    
258          # range outside the input          # range outside the input
259          result = cq([5, 6, 7, 8], [.5, 1.0], Range("[2;3]"))          result = cq([5, 6, 7, 8], [.5, 1.0], Range("[2;3]"))
260          eq(result, (0, []))          eq(result, None)
261    
262          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]"))
263          eq(result, (1, [(5, 1.0)]))          eq(result, (1, 0, 5, [(5, 1.0)]))
264    
265          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]"))
266          eq(result, (1, [(4, 0.7142857142857143), # the algorithm generated          eq(result, (1, 0, 6,
267                          (5, 0.8571428571428571), # these values, but they are                 [(4, 0.7142857142857143), # the algorithm generated
268                          (6, 1.0)]))              # right.                  (5, 0.8571428571428571), # these values, but they are
269                    (6, 1.0)]))              # right.
270    
271          # adjusted quantiles          # adjusted quantiles
272          result = cq([1, 1, 1, 1, 1,          result = cq([1, 1, 1, 1, 1,
# Line 101  class ClassGenTest(unittest.TestCase): Line 275  class ClassGenTest(unittest.TestCase):
275                       4, 4, 4, 4, 4,                       4, 4, 4, 4, 4,
276                       5, 5, 5, 5, 5],                       5, 5, 5, 5, 5],
277                      [.12, .24, .36, .50, .62, .76, .88, 1.0], Range("[1;5]"))                      [.12, .24, .36, .50, .62, .76, .88, 1.0], Range("[1;5]"))
278          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)]))
279    
280  if __name__ == "__main__":  if __name__ == "__main__":
281      unittest.main()      unittest.main()

Legend:
Removed from v.890  
changed lines
  Added in v.1166

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26