/[thuban]/trunk/thuban/test/test_classgen.py
ViewVC logotype

Annotation of /trunk/thuban/test/test_classgen.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1450 - (hide annotations)
Fri Jul 18 10:18:56 2003 UTC (21 years, 7 months ago) by bh
File MIME type: text/x-python
File size: 11871 byte(s)
(TestFixedRamp.test): Extend test to check
the non-fixed values as well. The old test would have accepted
a fixed ramp that only returnes the fixed properties

1 bh 1110 # Copyright (c) 2002, 2003 by Intevation GmbH
2 jonathan 890 # Authors:
3     # Bernhard Herzog <[email protected]>
4     #
5     # This program is free software under the GPL (>=v2)
6     # Read the file COPYING coming with Thuban for details.
7    
8     """
9     Test the Menu
10     """
11    
12     __version__ = "$Revision$"
13    
14     import unittest
15    
16     import support
17     support.initthuban()
18    
19 jonathan 1166 from Thuban.Model.classgen import \
20     generate_singletons, \
21     generate_uniform_distribution, \
22     generate_quantiles, \
23     calculate_quantiles, \
24 jonathan 1436 GreyRamp, CustomRamp, FixedRamp
25 jonathan 890 from Thuban.Model.range import Range
26 jonathan 1362 from Thuban.Model.color import Color
27 jonathan 890
28 bh 1379 from Thuban.Model.classification import ClassGroupRange, ClassGroupProperties
29 jonathan 1166
30 jonathan 890 class ClassGenTest(unittest.TestCase):
31    
32 jonathan 1166 def doClassRangeTest(self, clazz, ranges):
33     self.assertEquals(clazz.GetNumGroups(), len(ranges))
34 jonathan 1362 for i in range(clazz.GetNumGroups()):
35 jonathan 1166 group = clazz.GetGroup(i)
36     r1 = str(Range(ranges[i]))
37     r2 = group.GetRange()
38     self.assertEquals(r1, r2)
39 jonathan 890
40 jonathan 1362 self.doBoundsTest(clazz)
41    
42 jonathan 1166 def doClassSingleTest(self, clazz, _list):
43     self.assertEquals(clazz.GetNumGroups(), len(_list))
44 jonathan 1362 for i in range(clazz.GetNumGroups()):
45 jonathan 1166 group = clazz.GetGroup(i)
46     self.assertEquals(group.GetValue(), _list[i])
47 jonathan 1362
48     self.doBoundsTest(clazz)
49    
50     def doBoundsTest(self, clazz, ramp = GreyRamp):
51    
52     #
53     # check that the properties are right (i.e. the first group
54     # is all white, the last is all black). This assumes that
55     # the GreyRamp, unless another is provided.
56     #
57     if clazz.GetNumGroups() >= 1:
58     groupF = clazz.GetGroup(0)
59     first = ramp.GetProperties(0)
60     self.assertEquals(groupF.GetProperties(), first)
61    
62     if clazz.GetNumGroups() >= 2:
63     groupL = clazz.GetGroup(clazz.GetNumGroups() - 1)
64     last = ramp.GetProperties(1)
65     self.assertEquals(groupL.GetProperties(), last)
66 jonathan 1166
67     def test_generate_singletons(self):
68     """Test generate_singletons"""
69    
70 jonathan 890 eq = self.assertEquals
71 jonathan 1166 gs = generate_singletons
72 jonathan 1345 ramp = GreyRamp
73 jonathan 890
74 jonathan 1166 _list = [1, 2, 3, 4]
75 jonathan 1345 cl = gs(_list, ramp)
76 jonathan 1166 self.doClassSingleTest(cl, _list)
77    
78     _list = range(0, 100)
79 jonathan 1345 cl = gs(_list, ramp)
80 jonathan 1166 self.doClassSingleTest(cl, _list)
81    
82     _list = range(-100, 100)
83 jonathan 1345 cl = gs(_list, ramp)
84 jonathan 1166 self.doClassSingleTest(cl, _list)
85    
86     _list = ['a', 'b', 'c', 'd']
87 jonathan 1345 cl = gs(_list, ramp)
88 jonathan 1166 self.doClassSingleTest(cl, _list)
89    
90     _list = []
91 jonathan 1345 cl = gs(_list, ramp)
92 jonathan 1166 self.doClassSingleTest(cl, _list)
93    
94     _list = [1]
95 jonathan 1345 cl = gs(_list, ramp)
96 jonathan 1166 self.doClassSingleTest(cl, _list)
97    
98    
99     def test_generate_uniform_distribution(self):
100     """Test generate_uniform_distribution"""
101    
102     eq = self.assertEquals
103     gud = generate_uniform_distribution
104 jonathan 1345 ramp = GreyRamp
105 jonathan 1166
106     cl = gud(0, 99, 10, ramp, True)
107     self.doClassRangeTest(cl, ("[0;10[", "[10;20[", "[20;30[", "[30;40[",
108     "[40;50[", "[50;60[", "[60;70[", "[70;80[",
109     "[80;90[", "[90;99]"))
110    
111     cl = gud(0, 99, 10, ramp, False)
112     self.doClassRangeTest(cl, ("[0;9.9[", "[9.9;19.8[", "[19.8;29.7[",
113     "[29.7;39.6[", "[39.6;49.5[", "[49.5;59.4[",
114     "[59.4;69.3[", "[69.3;79.2[", "[79.2;89.1[",
115     "[89.1;99.0]"))
116    
117     cl = gud(1, 2, 2, ramp, False)
118     self.doClassRangeTest(cl, ("[1;1.5[", "[1.5;2]"))
119    
120     cl = gud(1, 2, 2, ramp, True)
121     self.doClassRangeTest(cl, ("[1;2[", "[2;2]"))
122    
123    
124     def test_generate_quantiles(self):
125     """Test generate_quantiles"""
126    
127     eq = self.assertEquals
128    
129 jonathan 890 #
130 jonathan 1166 # Test calculate_quantiles
131 jonathan 890 #
132    
133 jonathan 1166 gq = generate_quantiles
134 jonathan 890
135 jonathan 1345 ramp = GreyRamp
136 jonathan 1166
137     adj, cl = gq([1, 2, 3, 4], [.25, .5, .75, 1.0], ramp, Range("[1;4]"))
138     self.failIf(adj)
139     self.doClassRangeTest(cl, ("[1;1]", "]1;2]", "]2;3]", "]3;4]"))
140    
141     adj, cl = gq(range(0, 100), [.25, .5, .75, 1.0], ramp, Range("[0;100["))
142     self.failIf(adj)
143     self.doClassRangeTest(cl, ("[0;24]", "]24;49]", "]49;74]", "]74;100["))
144    
145     adj, cl = gq(range(0, 100), [.33, .66, 1.0], ramp, Range("[0;100]"))
146     self.failIf(adj)
147     self.doClassRangeTest(cl, ("[0;32]", "]32;65]", "]65;100]"))
148    
149     # negative input
150     adj,cl = gq(range(-100,100), [.33, .66, 1.0], ramp, Range("[-100;100]"))
151     self.failIf(adj)
152     self.doClassRangeTest(cl, ("[-100;-35]", "]-35;31]", "]31;100]"))
153    
154     # unequal percentiles
155     adj,cl = gq(range(0, 100), [.25, .66, .8, 1.0], ramp, Range("[0;100]"))
156     self.failIf(adj)
157     self.doClassRangeTest(cl, ("[0;24]", "]24;65]", "]65;79]", "]79;100]"))
158    
159     # input all the same
160     adj,cl = gq([1, 1, 1, 1], [.25, .5, .75, 1.0], ramp, Range("[1;4]"))
161     self.failUnless(adj)
162     self.doClassRangeTest(cl, ("[1;4]",))
163    
164     # empty input
165     adj,cl = gq([], [.25, .5, .75, 1.0], ramp, Range("[1;4]"))
166     self.failUnless(adj)
167     self.doClassRangeTest(cl, ())
168    
169     # empty range
170     adj,cl = gq([1, 2, 3, 4], [.25, .5, .75, 1.0], ramp, Range("]0;1["))
171     self.failUnless(adj)
172     self.doClassRangeTest(cl, ())
173    
174     # empty percentiles
175     self.assertRaises(ValueError, gq,
176     [1, 2, 3, 4], [], ramp, Range("]0;1["))
177    
178     # single percentile
179     self.assertRaises(ValueError, gq,
180     [1, 2, 3, 4], [.5], ramp, Range("[0;4]"))
181    
182     # more percentiles than input
183     adj,cl = gq([1], [.5, 1.0], ramp, Range("[0;4]"))
184     self.failUnless(adj)
185     self.doClassRangeTest(cl, ("[0;4]",))
186    
187     adj,cl = gq([1], [.1, .2, .3, .4, .5, .6, .7, .8, .9, 1.0], ramp, Range("[0;4]"))
188     self.failUnless(adj)
189     self.doClassRangeTest(cl, ("[0;4]",))
190    
191     # range smaller than the input
192     adj,cl = gq([1, 2, 3, 4], [.5, 1.0], ramp, Range("[2;3]"))
193     self.failIf(adj)
194     self.doClassRangeTest(cl, ("[2;2]","]2;3]"))
195    
196     # range outside the input
197     adj,cl = gq([5, 6, 7, 8], [.5, 1.0], ramp, Range("[2;3]"))
198     self.failUnless(adj)
199     self.doClassRangeTest(cl, ())
200    
201     adj,cl = gq([1, 1, 1, 1, 1, 1], [.25, .5, .75, 1.0], ramp, Range("[1;4]"))
202     self.failUnless(adj)
203     self.doClassRangeTest(cl, ("[1;4]",))
204    
205     adj,cl = gq([1, 1, 1, 1, 1, 2, 3], [.25, .5, .75, 1.0], ramp, Range("[1;4]"))
206     self.failUnless(adj)
207     self.doClassRangeTest(cl, ("[1;1]", "]1;2]", "]2;4]"))
208    
209     # adjusted quantiles
210     adj,cl = gq([1, 1, 1, 1, 1,
211     2, 2, 2, 2, 2,
212     3, 3, 3, 3, 3,
213     4, 4, 4, 4, 4,
214     5, 5, 5, 5, 5],
215     [.12, .24, .36, .50, .62, .76, .88, 1.0], ramp, Range("[1;5]"))
216    
217     self.failUnless(adj)
218     self.doClassRangeTest(cl, ("[1;1]", "]1;2]", "]2;3]", "]3;4]", "]4;5]"))
219    
220     def test_calculate_quantiles(self):
221     """Test calculate_quantiles"""
222    
223     eq = self.assertEquals
224    
225     #
226     # Test calculate_quantiles
227     #
228    
229     cq = calculate_quantiles
230    
231 jonathan 890 result = cq([1, 2, 3, 4], [.25, .5, .75, 1.0], Range("[1;4]"))
232 jonathan 896 eq(result, (0, 0, 3, [(0, .25), (1, .5), (2, .75), (3, 1.0)]))
233 jonathan 890
234     result = cq(range(0, 100), [.25, .5, .75, 1.0], Range("[0;100]"))
235 jonathan 896 eq(result, (0, 0, 99, [(24, .25), (49, .5), (74, .75), (99, 1.0)]))
236 jonathan 890
237     result = cq(range(0, 100), [.33, .66, 1.0], Range("[0;100]"))
238 jonathan 896 eq(result, (0, 0, 99, [(32, .33), (65, .66), (99, 1.0)]))
239 jonathan 890
240     # negative input
241     result = cq(range(-100, 100), [.33, .66, 1.0], Range("[-100;100]"))
242 jonathan 896 eq(result, (0, 0, 199, [(65, .33), (131, .66), (199, 1.0)]))
243 jonathan 890
244     # unequal percentiles
245     result = cq(range(0, 100), [.25, .66, .8, 1.0], Range("[0;100]"))
246 jonathan 896 eq(result, (0, 0, 99, [(24, .25), (65, .66), (79, .8), (99, 1.0)]))
247 jonathan 890
248     # input all the same
249     result = cq([1, 1, 1, 1], [.25, .5, .75, 1.0], Range("[1;4]"))
250 jonathan 896 eq(result, (1, 0, 3, [(3, 1.0)]))
251 jonathan 890
252     # empty input
253     result = cq([], [.25, .5, .75, 1.0], Range("[1;4]"))
254 jonathan 896 eq(result, None)
255 jonathan 890
256     # empty range
257     result = cq([1, 2, 3, 4], [.25, .5, .75, 1.0], Range("]0;1["))
258 jonathan 896 eq(result, None)
259 jonathan 890
260     # empty percentiles
261 jonathan 1166 self.assertRaises(ValueError, cq, [1, 2, 3, 4], [], Range("]0;1["))
262 jonathan 890
263     # single percentile
264 jonathan 1166 self.assertRaises(ValueError, cq, [1, 2, 3, 4], [.5], Range("[0;4]"))
265 jonathan 890
266 jonathan 1166 # percentile doesn't cover range
267     self.assertRaises(ValueError, cq, [1, 2, 3, 4], [.5,.8], Range("[0;4]"))
268    
269 jonathan 890 # more percentiles than input
270     result = cq([1], [.5, 1.0], Range("[0;4]"))
271 jonathan 896 eq(result, (1, 0, 0, [(0, 1.0)]))
272 jonathan 890
273     result = cq([1], [.1, .2, .3, .4, .5, .6, .7, .8, .9, 1.0], Range("[0;4]"))
274 jonathan 896 eq(result, (1, 0, 0, [(0, 1.0)]))
275 jonathan 890
276     # range smaller than the input
277     result = cq([1, 2, 3, 4], [.5, 1.0], Range("[2;3]"))
278 jonathan 896 eq(result, (0, 1, 2, [(1, .5), (2, 1.0)]))
279 jonathan 890
280     # range outside the input
281     result = cq([5, 6, 7, 8], [.5, 1.0], Range("[2;3]"))
282 jonathan 896 eq(result, None)
283 jonathan 890
284     result = cq([1, 1, 1, 1, 1, 1], [.25, .5, .75, 1.0], Range("[1;4]"))
285 jonathan 896 eq(result, (1, 0, 5, [(5, 1.0)]))
286 jonathan 890
287     result = cq([1, 1, 1, 1, 1, 2, 3], [.25, .5, .75, 1.0], Range("[1;4]"))
288 jonathan 896 eq(result, (1, 0, 6,
289     [(4, 0.7142857142857143), # the algorithm generated
290     (5, 0.8571428571428571), # these values, but they are
291     (6, 1.0)])) # right.
292 jonathan 890
293     # adjusted quantiles
294     result = cq([1, 1, 1, 1, 1,
295     2, 2, 2, 2, 2,
296     3, 3, 3, 3, 3,
297     4, 4, 4, 4, 4,
298     5, 5, 5, 5, 5],
299     [.12, .24, .36, .50, .62, .76, .88, 1.0], Range("[1;5]"))
300 jonathan 896 eq(result, (1, 0, 24, [(4, .2), (9, .4), (14, .6), (19, .8), (24, 1.0)]))
301 jonathan 890
302 bh 1379
303     class TestCustomRamp(unittest.TestCase):
304    
305     def test_color_interpolation(self):
306     """Test CustomRamp color interpolation"""
307     start = ClassGroupProperties()
308     start.SetFill(Color(1, 1, 1))
309     start.SetLineColor(Color(0, 0, 0))
310    
311     end = ClassGroupProperties()
312     end.SetFill(Color(1, 0, 0))
313     end.SetLineColor(Color(0, 1, 0))
314    
315     ramp = CustomRamp(start, end)
316     half = ramp.GetProperties(0.5)
317     self.assertEquals(half.GetFill(), Color(1, 0.5, 0.5))
318     self.assertEquals(half.GetLineColor(), Color(0, 0.5, 0))
319    
320 bh 1450
321 jonathan 1436 class TestFixedRamp(unittest.TestCase):
322 bh 1379
323 jonathan 1436 def test(self):
324 bh 1450 """Test FixedRamp"""
325 jonathan 1436 eq = self.assertEquals
326    
327     for lineColor, lineWidth, fillColor in \
328     [(None, None, None), (Color(1, 1, 1), None, None),
329     (None, 4, None), (None, None, Color(0, 1, 0)),
330     (Color(1, 1, 1), 4, None), (Color(1, 1, 1), None, Color(0, 1, 0)),
331     (None, 4, Color(0, 1, 0)), (Color(1, 1, 1), 4, Color(0, 1, 0))]:
332    
333     framp = FixedRamp(GreyRamp, (lineColor, lineWidth, fillColor))
334    
335     for i in [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]:
336     props = framp.GetProperties(i)
337 bh 1450 grey = Color(1 - i, 1 - i, 1 - i)
338     if lineColor is not None:
339     eq(props.GetLineColor(), lineColor)
340     else:
341     eq(props.GetLineColor(), grey)
342     if lineWidth is not None:
343     eq(props.GetLineWidth(), lineWidth)
344     if fillColor is not None:
345     eq(props.GetFill(), fillColor)
346     else:
347     eq(props.GetFill(), grey)
348 jonathan 1436
349 jonathan 890 if __name__ == "__main__":
350     unittest.main()
351    

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26