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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1528 - (show annotations)
Wed Jul 30 15:43:41 2003 UTC (21 years, 7 months ago) by jonathan
Original Path: trunk/thuban/test/test_classgen.py
File MIME type: text/x-python
File size: 11878 byte(s)
Use renamed Ramp instances.

1 # Copyright (c) 2002, 2003 by Intevation GmbH
2 # 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 from Thuban.Model.classgen import \
20 generate_singletons, \
21 generate_uniform_distribution, \
22 generate_quantiles, \
23 calculate_quantiles, \
24 grey_ramp, CustomRamp, FixedRamp
25 from Thuban.Model.range import Range
26 from Thuban.Model.color import Color
27
28 from Thuban.Model.classification import ClassGroupRange, ClassGroupProperties
29
30 class ClassGenTest(unittest.TestCase):
31
32 def doClassRangeTest(self, clazz, ranges):
33 self.assertEquals(clazz.GetNumGroups(), len(ranges))
34 for i in range(clazz.GetNumGroups()):
35 group = clazz.GetGroup(i)
36 r1 = str(Range(ranges[i]))
37 r2 = group.GetRange()
38 self.assertEquals(r1, r2)
39
40 self.doBoundsTest(clazz)
41
42 def doClassSingleTest(self, clazz, _list):
43 self.assertEquals(clazz.GetNumGroups(), len(_list))
44 for i in range(clazz.GetNumGroups()):
45 group = clazz.GetGroup(i)
46 self.assertEquals(group.GetValue(), _list[i])
47
48 self.doBoundsTest(clazz)
49
50 def doBoundsTest(self, clazz, ramp = grey_ramp):
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 grey_ramp, 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
67 def test_generate_singletons(self):
68 """Test generate_singletons"""
69
70 eq = self.assertEquals
71 gs = generate_singletons
72 ramp = grey_ramp
73
74 _list = [1, 2, 3, 4]
75 cl = gs(_list, ramp)
76 self.doClassSingleTest(cl, _list)
77
78 _list = range(0, 100)
79 cl = gs(_list, ramp)
80 self.doClassSingleTest(cl, _list)
81
82 _list = range(-100, 100)
83 cl = gs(_list, ramp)
84 self.doClassSingleTest(cl, _list)
85
86 _list = ['a', 'b', 'c', 'd']
87 cl = gs(_list, ramp)
88 self.doClassSingleTest(cl, _list)
89
90 _list = []
91 cl = gs(_list, ramp)
92 self.doClassSingleTest(cl, _list)
93
94 _list = [1]
95 cl = gs(_list, ramp)
96 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 ramp = grey_ramp
105
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 #
130 # Test calculate_quantiles
131 #
132
133 gq = generate_quantiles
134
135 ramp = grey_ramp
136
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 result = cq([1, 2, 3, 4], [.25, .5, .75, 1.0], Range("[1;4]"))
232 eq(result, (0, 0, 3, [(0, .25), (1, .5), (2, .75), (3, 1.0)]))
233
234 result = cq(range(0, 100), [.25, .5, .75, 1.0], Range("[0;100]"))
235 eq(result, (0, 0, 99, [(24, .25), (49, .5), (74, .75), (99, 1.0)]))
236
237 result = cq(range(0, 100), [.33, .66, 1.0], Range("[0;100]"))
238 eq(result, (0, 0, 99, [(32, .33), (65, .66), (99, 1.0)]))
239
240 # negative input
241 result = cq(range(-100, 100), [.33, .66, 1.0], Range("[-100;100]"))
242 eq(result, (0, 0, 199, [(65, .33), (131, .66), (199, 1.0)]))
243
244 # unequal percentiles
245 result = cq(range(0, 100), [.25, .66, .8, 1.0], Range("[0;100]"))
246 eq(result, (0, 0, 99, [(24, .25), (65, .66), (79, .8), (99, 1.0)]))
247
248 # input all the same
249 result = cq([1, 1, 1, 1], [.25, .5, .75, 1.0], Range("[1;4]"))
250 eq(result, (1, 0, 3, [(3, 1.0)]))
251
252 # empty input
253 result = cq([], [.25, .5, .75, 1.0], Range("[1;4]"))
254 eq(result, None)
255
256 # empty range
257 result = cq([1, 2, 3, 4], [.25, .5, .75, 1.0], Range("]0;1["))
258 eq(result, None)
259
260 # empty percentiles
261 self.assertRaises(ValueError, cq, [1, 2, 3, 4], [], Range("]0;1["))
262
263 # single percentile
264 self.assertRaises(ValueError, cq, [1, 2, 3, 4], [.5], Range("[0;4]"))
265
266 # percentile doesn't cover range
267 self.assertRaises(ValueError, cq, [1, 2, 3, 4], [.5,.8], Range("[0;4]"))
268
269 # more percentiles than input
270 result = cq([1], [.5, 1.0], Range("[0;4]"))
271 eq(result, (1, 0, 0, [(0, 1.0)]))
272
273 result = cq([1], [.1, .2, .3, .4, .5, .6, .7, .8, .9, 1.0], Range("[0;4]"))
274 eq(result, (1, 0, 0, [(0, 1.0)]))
275
276 # range smaller than the input
277 result = cq([1, 2, 3, 4], [.5, 1.0], Range("[2;3]"))
278 eq(result, (0, 1, 2, [(1, .5), (2, 1.0)]))
279
280 # range outside the input
281 result = cq([5, 6, 7, 8], [.5, 1.0], Range("[2;3]"))
282 eq(result, None)
283
284 result = cq([1, 1, 1, 1, 1, 1], [.25, .5, .75, 1.0], Range("[1;4]"))
285 eq(result, (1, 0, 5, [(5, 1.0)]))
286
287 result = cq([1, 1, 1, 1, 1, 2, 3], [.25, .5, .75, 1.0], Range("[1;4]"))
288 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
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 eq(result, (1, 0, 24, [(4, .2), (9, .4), (14, .6), (19, .8), (24, 1.0)]))
301
302
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
321 class TestFixedRamp(unittest.TestCase):
322
323 def test(self):
324 """Test FixedRamp"""
325 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(grey_ramp, (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 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
349 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