1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | #TEST |
---|
4 | import sys |
---|
5 | import unittest |
---|
6 | from math import sqrt |
---|
7 | import tempfile |
---|
8 | import os |
---|
9 | from Numeric import zeros, take, compress, Float, Int, dot, concatenate, \ |
---|
10 | ArrayType, allclose, array |
---|
11 | |
---|
12 | from fit import * |
---|
13 | from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh |
---|
14 | from anuga.utilities.sparse import Sparse, Sparse_CSR |
---|
15 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
16 | from anuga.utilities.numerical_tools import ensure_numeric |
---|
17 | from anuga.geospatial_data.geospatial_data import Geospatial_data |
---|
18 | |
---|
19 | def distance(x, y): |
---|
20 | return sqrt( sum( (array(x)-array(y))**2 )) |
---|
21 | |
---|
22 | def linear_function(point): |
---|
23 | point = array(point) |
---|
24 | return point[:,0]+point[:,1] |
---|
25 | |
---|
26 | |
---|
27 | class Test_Fit(unittest.TestCase): |
---|
28 | |
---|
29 | def setUp(self): |
---|
30 | pass |
---|
31 | |
---|
32 | def tearDown(self): |
---|
33 | pass |
---|
34 | |
---|
35 | |
---|
36 | def test_smooth_attributes_to_mesh(self): |
---|
37 | a = [0.0, 0.0] |
---|
38 | b = [0.0, 5.0] |
---|
39 | c = [5.0, 0.0] |
---|
40 | points = [a, b, c] |
---|
41 | triangles = [ [1,0,2] ] #bac |
---|
42 | |
---|
43 | d1 = [1.0, 1.0] |
---|
44 | d2 = [1.0, 3.0] |
---|
45 | d3 = [3.0,1.0] |
---|
46 | z1 = 2 |
---|
47 | z2 = 4 |
---|
48 | z3 = 4 |
---|
49 | data_coords = [d1, d2, d3] |
---|
50 | |
---|
51 | z = [z1, z2, z3] |
---|
52 | fit = Fit(points, triangles, alpha=0) |
---|
53 | #print "interp.get_A()", interp.get_A() |
---|
54 | fit._build_matrix_AtA_Atz(ensure_numeric(data_coords), |
---|
55 | ensure_numeric(z)) |
---|
56 | #print "Atz - from fit", fit.Atz |
---|
57 | #print "AtA - from fit", fit.AtA.todense() |
---|
58 | #print "z",z |
---|
59 | |
---|
60 | assert allclose(fit.Atz, [2.8, 3.6, 3.6], atol=1e-7) |
---|
61 | |
---|
62 | f = fit.fit() |
---|
63 | |
---|
64 | answer = [0, 5., 5.] |
---|
65 | |
---|
66 | #print "f\n",f |
---|
67 | #print "answer\n",answer |
---|
68 | |
---|
69 | assert allclose(f, answer, atol=1e-7) |
---|
70 | |
---|
71 | def test_smooth_att_to_meshII(self): |
---|
72 | |
---|
73 | a = [0.0, 0.0] |
---|
74 | b = [0.0, 5.0] |
---|
75 | c = [5.0, 0.0] |
---|
76 | points = [a, b, c] |
---|
77 | triangles = [ [1,0,2] ] #bac |
---|
78 | |
---|
79 | d1 = [1.0, 1.0] |
---|
80 | d2 = [1.0, 2.0] |
---|
81 | d3 = [3.0,1.0] |
---|
82 | data_coords = [d1, d2, d3] |
---|
83 | z = linear_function(data_coords) |
---|
84 | #print "z",z |
---|
85 | |
---|
86 | interp = Fit(points, triangles, alpha=0.0) |
---|
87 | f = interp.fit(data_coords, z) |
---|
88 | answer = linear_function(points) |
---|
89 | #print "f\n",f |
---|
90 | #print "answer\n",answer |
---|
91 | |
---|
92 | assert allclose(f, answer) |
---|
93 | |
---|
94 | def test_smooth_attributes_to_meshIII(self): |
---|
95 | |
---|
96 | a = [-1.0, 0.0] |
---|
97 | b = [3.0, 4.0] |
---|
98 | c = [4.0,1.0] |
---|
99 | d = [-3.0, 2.0] #3 |
---|
100 | e = [-1.0,-2.0] |
---|
101 | f = [1.0, -2.0] #5 |
---|
102 | |
---|
103 | vertices = [a, b, c, d,e,f] |
---|
104 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
105 | |
---|
106 | point_coords = [[-2.0, 2.0], |
---|
107 | [-1.0, 1.0], |
---|
108 | [0.0,2.0], |
---|
109 | [1.0, 1.0], |
---|
110 | [2.0, 1.0], |
---|
111 | [0.0,0.0], |
---|
112 | [1.0, 0.0], |
---|
113 | [0.0, -1.0], |
---|
114 | [-0.2,-0.5], |
---|
115 | [-0.9, -1.5], |
---|
116 | [0.5, -1.9], |
---|
117 | [3.0,1.0]] |
---|
118 | |
---|
119 | z = linear_function(point_coords) |
---|
120 | interp = Fit(vertices, triangles, |
---|
121 | alpha=0.0) |
---|
122 | |
---|
123 | #print 'z',z |
---|
124 | f = interp.fit(point_coords,z) |
---|
125 | answer = linear_function(vertices) |
---|
126 | #print "f\n",f |
---|
127 | #print "answer\n",answer |
---|
128 | assert allclose(f, answer) |
---|
129 | |
---|
130 | |
---|
131 | def test_smooth_attributes_to_meshIV(self): |
---|
132 | """ Testing 2 attributes smoothed to the mesh |
---|
133 | """ |
---|
134 | |
---|
135 | a = [0.0, 0.0] |
---|
136 | b = [0.0, 5.0] |
---|
137 | c = [5.0, 0.0] |
---|
138 | points = [a, b, c] |
---|
139 | triangles = [ [1,0,2] ] #bac |
---|
140 | |
---|
141 | d1 = [1.0, 1.0] |
---|
142 | d2 = [1.0, 3.0] |
---|
143 | d3 = [3.0, 1.0] |
---|
144 | z1 = [2, 4] |
---|
145 | z2 = [4, 8] |
---|
146 | z3 = [4, 8] |
---|
147 | data_coords = [d1, d2, d3] |
---|
148 | |
---|
149 | z = [z1, z2, z3] |
---|
150 | fit = Fit(points, triangles, alpha=0) |
---|
151 | |
---|
152 | f = fit.fit(data_coords,z) |
---|
153 | answer = [[0,0], [5., 10.], [5., 10.]] |
---|
154 | assert allclose(f, answer) |
---|
155 | |
---|
156 | def test_smooth_attributes_to_mesh_build_fit_subset(self): |
---|
157 | |
---|
158 | a = [-1.0, 0.0] |
---|
159 | b = [3.0, 4.0] |
---|
160 | c = [4.0,1.0] |
---|
161 | d = [-3.0, 2.0] #3 |
---|
162 | e = [-1.0,-2.0] |
---|
163 | f = [1.0, -2.0] #5 |
---|
164 | |
---|
165 | vertices = [a, b, c, d,e,f] |
---|
166 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
167 | |
---|
168 | interp = Fit(vertices, triangles, |
---|
169 | alpha=0.0) |
---|
170 | |
---|
171 | point_coords = [[-2.0, 2.0], |
---|
172 | [-1.0, 1.0], |
---|
173 | [0.0,2.0], |
---|
174 | [1.0, 1.0], |
---|
175 | ] |
---|
176 | |
---|
177 | z = linear_function(point_coords) |
---|
178 | |
---|
179 | f = interp.build_fit_subset(point_coords,z) |
---|
180 | |
---|
181 | point_coords = [ |
---|
182 | [2.0, 1.0], |
---|
183 | [0.0,0.0], |
---|
184 | [1.0, 0.0], |
---|
185 | [0.0, -1.0], |
---|
186 | [-0.2,-0.5], |
---|
187 | [-0.9, -1.5], |
---|
188 | [0.5, -1.9], |
---|
189 | [3.0,1.0]] |
---|
190 | |
---|
191 | z = linear_function(point_coords) |
---|
192 | |
---|
193 | f = interp.build_fit_subset(point_coords,z) |
---|
194 | |
---|
195 | #print 'z',z |
---|
196 | f = interp.fit() |
---|
197 | answer = linear_function(vertices) |
---|
198 | #print "f\n",f |
---|
199 | #print "answer\n",answer |
---|
200 | assert allclose(f, answer) |
---|
201 | |
---|
202 | # test fit 2 mesh as well. |
---|
203 | def test_fit_file_blocking(self): |
---|
204 | |
---|
205 | a = [-1.0, 0.0] |
---|
206 | b = [3.0, 4.0] |
---|
207 | c = [4.0,1.0] |
---|
208 | d = [-3.0, 2.0] #3 |
---|
209 | e = [-1.0,-2.0] |
---|
210 | f = [1.0, -2.0] #5 |
---|
211 | |
---|
212 | vertices = [a, b, c, d,e,f] |
---|
213 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
214 | |
---|
215 | interp = Fit(vertices, triangles, |
---|
216 | alpha=0.0) |
---|
217 | |
---|
218 | |
---|
219 | fileName = tempfile.mktemp(".ddd") |
---|
220 | file = open(fileName,"w") |
---|
221 | file.write(" x,y, elevation \n\ |
---|
222 | -2.0, 2.0, 0.\n\ |
---|
223 | -1.0, 1.0, 0.\n\ |
---|
224 | 0.0, 2.0 , 2.\n\ |
---|
225 | 1.0, 1.0 , 2.\n\ |
---|
226 | 2.0, 1.0 ,3. \n\ |
---|
227 | 0.0, 0.0 , 0.\n\ |
---|
228 | 1.0, 0.0 , 1.\n\ |
---|
229 | 0.0, -1.0, -1.\n\ |
---|
230 | -0.2, -0.5, -0.7\n\ |
---|
231 | -0.9, -1.5, -2.4\n\ |
---|
232 | 0.5, -1.9, -1.4\n\ |
---|
233 | 3.0, 1.0 , 4.\n") |
---|
234 | file.close() |
---|
235 | |
---|
236 | f = interp.fit(fileName, max_read_lines=2) |
---|
237 | answer = linear_function(vertices) |
---|
238 | #print "f\n",f |
---|
239 | #print "answer\n",answer |
---|
240 | assert allclose(f, answer) |
---|
241 | os.remove(fileName) |
---|
242 | |
---|
243 | def test_fit_to_mesh(self): |
---|
244 | |
---|
245 | a = [-1.0, 0.0] |
---|
246 | b = [3.0, 4.0] |
---|
247 | c = [4.0,1.0] |
---|
248 | d = [-3.0, 2.0] #3 |
---|
249 | e = [-1.0,-2.0] |
---|
250 | f = [1.0, -2.0] #5 |
---|
251 | |
---|
252 | vertices = [a, b, c, d,e,f] |
---|
253 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
254 | |
---|
255 | |
---|
256 | fileName = tempfile.mktemp(".ddd") |
---|
257 | file = open(fileName,"w") |
---|
258 | file.write(" x,y, elevation \n\ |
---|
259 | -2.0, 2.0, 0.\n\ |
---|
260 | -1.0, 1.0, 0.\n\ |
---|
261 | 0.0, 2.0 , 2.\n\ |
---|
262 | 1.0, 1.0 , 2.\n\ |
---|
263 | 2.0, 1.0 ,3. \n\ |
---|
264 | 0.0, 0.0 , 0.\n\ |
---|
265 | 1.0, 0.0 , 1.\n\ |
---|
266 | 0.0, -1.0, -1.\n\ |
---|
267 | -0.2, -0.5, -0.7\n\ |
---|
268 | -0.9, -1.5, -2.4\n\ |
---|
269 | 0.5, -1.9, -1.4\n\ |
---|
270 | 3.0, 1.0 , 4.\n") |
---|
271 | file.close() |
---|
272 | |
---|
273 | f = fit_to_mesh(vertices, triangles,fileName, |
---|
274 | alpha=0.0, max_read_lines=2) |
---|
275 | #use_cache=True, verbose=True) |
---|
276 | answer = linear_function(vertices) |
---|
277 | #print "f\n",f |
---|
278 | #print "answer\n",answer |
---|
279 | assert allclose(f, answer) |
---|
280 | |
---|
281 | os.remove(fileName) |
---|
282 | |
---|
283 | def test_fit_to_mesh_pts(self): |
---|
284 | a = [-1.0, 0.0] |
---|
285 | b = [3.0, 4.0] |
---|
286 | c = [4.0,1.0] |
---|
287 | d = [-3.0, 2.0] #3 |
---|
288 | e = [-1.0,-2.0] |
---|
289 | f = [1.0, -2.0] #5 |
---|
290 | |
---|
291 | vertices = [a, b, c, d,e,f] |
---|
292 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
293 | |
---|
294 | |
---|
295 | fileName = tempfile.mktemp(".xya") |
---|
296 | file = open(fileName,"w") |
---|
297 | file.write(" elevation \n\ |
---|
298 | -2.0, 2.0, 0.\n\ |
---|
299 | -1.0, 1.0, 0.\n\ |
---|
300 | 0.0, 2.0 , 2.\n\ |
---|
301 | 1.0, 1.0 , 2.\n\ |
---|
302 | 2.0, 1.0 ,3. \n\ |
---|
303 | 0.0, 0.0 , 0.\n\ |
---|
304 | 1.0, 0.0 , 1.\n\ |
---|
305 | 0.0, -1.0, -1.\n\ |
---|
306 | -0.2, -0.5, -0.7\n\ |
---|
307 | -0.9, -1.5, -2.4\n\ |
---|
308 | 0.5, -1.9, -1.4\n\ |
---|
309 | 3.0, 1.0 , 4.\n") |
---|
310 | file.close() |
---|
311 | |
---|
312 | geo = Geospatial_data(fileName) |
---|
313 | fileName_pts = tempfile.mktemp(".pts") |
---|
314 | geo.export_points_file(fileName_pts) |
---|
315 | f = fit_to_mesh(vertices, triangles,fileName_pts, |
---|
316 | alpha=0.0, max_read_lines=2) |
---|
317 | #use_cache=True, verbose=True) |
---|
318 | answer = linear_function(vertices) |
---|
319 | #print "f\n",f |
---|
320 | #print "answer\n",answer |
---|
321 | assert allclose(f, answer) |
---|
322 | os.remove(fileName) |
---|
323 | os.remove(fileName_pts) |
---|
324 | |
---|
325 | def test_fit_to_mesh(self): |
---|
326 | |
---|
327 | a = [-1.0, 0.0] |
---|
328 | b = [3.0, 4.0] |
---|
329 | c = [4.0,1.0] |
---|
330 | d = [-3.0, 2.0] #3 |
---|
331 | e = [-1.0,-2.0] |
---|
332 | f = [1.0, -2.0] #5 |
---|
333 | |
---|
334 | vertices = [a, b, c, d,e,f] |
---|
335 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
336 | |
---|
337 | |
---|
338 | fileName = tempfile.mktemp(".ddd") |
---|
339 | file = open(fileName,"w") |
---|
340 | file.write(" x,y, elevation \n\ |
---|
341 | -2.0, 2.0, 0.\n\ |
---|
342 | -1.0, 1.0, 0.\n\ |
---|
343 | 0.0, 2.0 , 2.\n\ |
---|
344 | 1.0, 1.0 , 2.\n\ |
---|
345 | 2.0, 1.0 ,3. \n\ |
---|
346 | 0.0, 0.0 , 0.\n\ |
---|
347 | 1.0, 0.0 , 1.\n\ |
---|
348 | 0.0, -1.0, -1.\n\ |
---|
349 | -0.2, -0.5, -0.7\n\ |
---|
350 | -0.9, -1.5, -2.4\n\ |
---|
351 | 0.5, -1.9, -1.4\n\ |
---|
352 | 3.0, 1.0 , 4.\n") |
---|
353 | file.close() |
---|
354 | |
---|
355 | f = fit_to_mesh(vertices, triangles,fileName, |
---|
356 | alpha=0.0, max_read_lines=2) |
---|
357 | #use_cache=True, verbose=True) |
---|
358 | answer = linear_function(vertices) |
---|
359 | #print "f\n",f |
---|
360 | #print "answer\n",answer |
---|
361 | assert allclose(f, answer) |
---|
362 | |
---|
363 | os.remove(fileName) |
---|
364 | |
---|
365 | def test_fit_to_mesh_2_atts(self): |
---|
366 | |
---|
367 | a = [-1.0, 0.0] |
---|
368 | b = [3.0, 4.0] |
---|
369 | c = [4.0,1.0] |
---|
370 | d = [-3.0, 2.0] #3 |
---|
371 | e = [-1.0,-2.0] |
---|
372 | f = [1.0, -2.0] #5 |
---|
373 | |
---|
374 | vertices = [a, b, c, d,e,f] |
---|
375 | triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc |
---|
376 | |
---|
377 | |
---|
378 | fileName = tempfile.mktemp(".ddd") |
---|
379 | file = open(fileName,"w") |
---|
380 | # the 2nd att name is wacky so it's the first key off a hash table |
---|
381 | file.write(" x,y, elevation, afriqction \n\ |
---|
382 | -2.0, 2.0, 0., 0.\n\ |
---|
383 | -1.0, 1.0, 0., 0.\n\ |
---|
384 | 0.0, 2.0 , 2., 20.\n\ |
---|
385 | 1.0, 1.0 , 2., 20.\n\ |
---|
386 | 2.0, 1.0 ,3., 30. \n\ |
---|
387 | 0.0, 0.0 , 0., 0.\n\ |
---|
388 | 1.0, 0.0 , 1., 10.\n\ |
---|
389 | 0.0, -1.0, -1., -10.\n\ |
---|
390 | -0.2, -0.5, -0.7, -7.\n\ |
---|
391 | -0.9, -1.5, -2.4, -24. \n\ |
---|
392 | 0.5, -1.9, -1.4, -14. \n\ |
---|
393 | 3.0, 1.0 , 4., 40. \n") |
---|
394 | file.close() |
---|
395 | |
---|
396 | f = fit_to_mesh(vertices, triangles,fileName, |
---|
397 | alpha=0.0, |
---|
398 | attribute_name='elevation', max_read_lines=2) |
---|
399 | answer = linear_function(vertices) |
---|
400 | #print "f\n",f |
---|
401 | #print "answer\n",answer |
---|
402 | assert allclose(f, answer) |
---|
403 | os.remove(fileName) |
---|
404 | |
---|
405 | def test_fit_and_interpolation(self): |
---|
406 | |
---|
407 | a = [0.0, 0.0] |
---|
408 | b = [0.0, 2.0] |
---|
409 | c = [2.0, 0.0] |
---|
410 | d = [0.0, 4.0] |
---|
411 | e = [2.0, 2.0] |
---|
412 | f = [4.0, 0.0] |
---|
413 | |
---|
414 | points = [a, b, c, d, e, f] |
---|
415 | #bac, bce, ecf, dbe, daf, dae |
---|
416 | triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
417 | |
---|
418 | #Get (enough) datapoints |
---|
419 | data_points = [[ 0.66666667, 0.66666667], |
---|
420 | [ 1.33333333, 1.33333333], |
---|
421 | [ 2.66666667, 0.66666667], |
---|
422 | [ 0.66666667, 2.66666667], |
---|
423 | [ 0.0, 1.0], |
---|
424 | [ 0.0, 3.0], |
---|
425 | [ 1.0, 0.0], |
---|
426 | [ 1.0, 1.0], |
---|
427 | [ 1.0, 2.0], |
---|
428 | [ 1.0, 3.0], |
---|
429 | [ 2.0, 1.0], |
---|
430 | [ 3.0, 0.0], |
---|
431 | [ 3.0, 1.0]] |
---|
432 | |
---|
433 | z = linear_function(data_points) |
---|
434 | interp = Fit(points, triangles, |
---|
435 | alpha=0.0) |
---|
436 | |
---|
437 | answer = linear_function(points) |
---|
438 | |
---|
439 | f = interp.fit(data_points, z) |
---|
440 | |
---|
441 | #print "f",f |
---|
442 | #print "answer",answer |
---|
443 | assert allclose(f, answer) |
---|
444 | |
---|
445 | |
---|
446 | def test_smoothing_and_interpolation(self): |
---|
447 | |
---|
448 | a = [0.0, 0.0] |
---|
449 | b = [0.0, 2.0] |
---|
450 | c = [2.0, 0.0] |
---|
451 | d = [0.0, 4.0] |
---|
452 | e = [2.0, 2.0] |
---|
453 | f = [4.0, 0.0] |
---|
454 | |
---|
455 | points = [a, b, c, d, e, f] |
---|
456 | #bac, bce, ecf, dbe, daf, dae |
---|
457 | triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
458 | |
---|
459 | #Get (too few!) datapoints |
---|
460 | data_points = [[ 0.66666667, 0.66666667], |
---|
461 | [ 1.33333333, 1.33333333], |
---|
462 | [ 2.66666667, 0.66666667], |
---|
463 | [ 0.66666667, 2.66666667]] |
---|
464 | |
---|
465 | z = linear_function(data_points) |
---|
466 | answer = linear_function(points) |
---|
467 | |
---|
468 | #Make interpolator with too few data points and no smoothing |
---|
469 | interp = Fit(points, triangles, alpha=0.0) |
---|
470 | #Must raise an exception |
---|
471 | try: |
---|
472 | f = interp.fit(data_points,z) |
---|
473 | except ToFewPointsError: |
---|
474 | pass |
---|
475 | |
---|
476 | #Now try with smoothing parameter |
---|
477 | interp = Fit(points, triangles, alpha=1.0e-13) |
---|
478 | |
---|
479 | f = interp.fit(data_points,z) |
---|
480 | #f will be different from answer due to smoothing |
---|
481 | assert allclose(f, answer,atol=5) |
---|
482 | |
---|
483 | |
---|
484 | #Tests of smoothing matrix |
---|
485 | def test_smoothing_matrix_one_triangle(self): |
---|
486 | from Numeric import dot |
---|
487 | a = [0.0, 0.0] |
---|
488 | b = [0.0, 2.0] |
---|
489 | c = [2.0,0.0] |
---|
490 | points = [a, b, c] |
---|
491 | |
---|
492 | vertices = [ [1,0,2] ] #bac |
---|
493 | |
---|
494 | interp = Fit(points, vertices) |
---|
495 | |
---|
496 | assert allclose(interp.get_D(), [[1, -0.5, -0.5], |
---|
497 | [-0.5, 0.5, 0], |
---|
498 | [-0.5, 0, 0.5]]) |
---|
499 | |
---|
500 | #Define f(x,y) = x |
---|
501 | f = array([0,0,2]) #Value at global vertex 2 |
---|
502 | |
---|
503 | #Check that int (df/dx)**2 + (df/dy)**2 dx dy = |
---|
504 | # int 1 dx dy = area = 2 |
---|
505 | assert dot(dot(f, interp.get_D()), f) == 2 |
---|
506 | |
---|
507 | #Define f(x,y) = y |
---|
508 | f = array([0,2,0]) #Value at global vertex 1 |
---|
509 | |
---|
510 | #Check that int (df/dx)**2 + (df/dy)**2 dx dy = |
---|
511 | # int 1 dx dy = area = 2 |
---|
512 | assert dot(dot(f, interp.get_D()), f) == 2 |
---|
513 | |
---|
514 | #Define f(x,y) = x+y |
---|
515 | f = array([0,2,2]) #Values at global vertex 1 and 2 |
---|
516 | |
---|
517 | #Check that int (df/dx)**2 + (df/dy)**2 dx dy = |
---|
518 | # int 2 dx dy = 2*area = 4 |
---|
519 | assert dot(dot(f, interp.get_D()), f) == 4 |
---|
520 | |
---|
521 | |
---|
522 | def test_smoothing_matrix_more_triangles(self): |
---|
523 | from Numeric import dot |
---|
524 | |
---|
525 | a = [0.0, 0.0] |
---|
526 | b = [0.0, 2.0] |
---|
527 | c = [2.0,0.0] |
---|
528 | d = [0.0, 4.0] |
---|
529 | e = [2.0, 2.0] |
---|
530 | f = [4.0,0.0] |
---|
531 | |
---|
532 | points = [a, b, c, d, e, f] |
---|
533 | #bac, bce, ecf, dbe, daf, dae |
---|
534 | vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
535 | |
---|
536 | interp = Fit(points, vertices) |
---|
537 | |
---|
538 | |
---|
539 | #assert allclose(interp.get_D(), [[1, -0.5, -0.5], |
---|
540 | # [-0.5, 0.5, 0], |
---|
541 | # [-0.5, 0, 0.5]]) |
---|
542 | |
---|
543 | #Define f(x,y) = x |
---|
544 | f = array([0,0,2,0,2,4]) #f evaluated at points a-f |
---|
545 | |
---|
546 | #Check that int (df/dx)**2 + (df/dy)**2 dx dy = |
---|
547 | # int 1 dx dy = total area = 8 |
---|
548 | assert dot(dot(f, interp.get_D()), f) == 8 |
---|
549 | |
---|
550 | #Define f(x,y) = y |
---|
551 | f = array([0,2,0,4,2,0]) #f evaluated at points a-f |
---|
552 | |
---|
553 | #Check that int (df/dx)**2 + (df/dy)**2 dx dy = |
---|
554 | # int 1 dx dy = area = 8 |
---|
555 | assert dot(dot(f, interp.get_D()), f) == 8 |
---|
556 | |
---|
557 | #Define f(x,y) = x+y |
---|
558 | f = array([0,2,2,4,4,4]) #f evaluated at points a-f |
---|
559 | |
---|
560 | #Check that int (df/dx)**2 + (df/dy)**2 dx dy = |
---|
561 | # int 2 dx dy = 2*area = 16 |
---|
562 | assert dot(dot(f, interp.get_D()), f) == 16 |
---|
563 | |
---|
564 | |
---|
565 | |
---|
566 | def test_fit_and_interpolation_with_different_origins(self): |
---|
567 | """Fit a surface to one set of points. Then interpolate that surface |
---|
568 | using another set of points. |
---|
569 | This test tests situtaion where points and mesh belong to a different |
---|
570 | coordinate system as defined by origin. |
---|
571 | """ |
---|
572 | |
---|
573 | #Setup mesh used to represent fitted function |
---|
574 | a = [0.0, 0.0] |
---|
575 | b = [0.0, 2.0] |
---|
576 | c = [2.0, 0.0] |
---|
577 | d = [0.0, 4.0] |
---|
578 | e = [2.0, 2.0] |
---|
579 | f = [4.0, 0.0] |
---|
580 | |
---|
581 | points = [a, b, c, d, e, f] |
---|
582 | #bac, bce, ecf, dbe, daf, dae |
---|
583 | triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] |
---|
584 | |
---|
585 | #Datapoints to fit from |
---|
586 | data_points1 = [[ 0.66666667, 0.66666667], |
---|
587 | [ 1.33333333, 1.33333333], |
---|
588 | [ 2.66666667, 0.66666667], |
---|
589 | [ 0.66666667, 2.66666667], |
---|
590 | [ 0.0, 1.0], |
---|
591 | [ 0.0, 3.0], |
---|
592 | [ 1.0, 0.0], |
---|
593 | [ 1.0, 1.0], |
---|
594 | [ 1.0, 2.0], |
---|
595 | [ 1.0, 3.0], |
---|
596 | [ 2.0, 1.0], |
---|
597 | [ 3.0, 0.0], |
---|
598 | [ 3.0, 1.0]] |
---|
599 | |
---|
600 | |
---|
601 | #First check that things are OK when using same origin |
---|
602 | mesh_origin = (56, 290000, 618000) #zone, easting, northing |
---|
603 | data_origin = (56, 290000, 618000) #zone, easting, northing |
---|
604 | |
---|
605 | |
---|
606 | #Fit surface to mesh |
---|
607 | interp = Fit(points, triangles, |
---|
608 | alpha=0.0, |
---|
609 | mesh_origin = mesh_origin) |
---|
610 | |
---|
611 | data_geo_spatial = Geospatial_data(data_points1, |
---|
612 | geo_reference = Geo_reference(56, 290000, 618000)) |
---|
613 | z = linear_function(data_points1) #Example z-values |
---|
614 | f = interp.fit(data_geo_spatial, z) #Fitted values at vertices |
---|
615 | |
---|
616 | #Shift datapoints according to new origins |
---|
617 | for k in range(len(data_points1)): |
---|
618 | data_points1[k][0] += mesh_origin[1] - data_origin[1] |
---|
619 | data_points1[k][1] += mesh_origin[2] - data_origin[2] |
---|
620 | |
---|
621 | |
---|
622 | |
---|
623 | #Fit surface to mesh |
---|
624 | interp = Fit(points, triangles, |
---|
625 | alpha=0.0) #, |
---|
626 | # mesh_origin = mesh_origin) |
---|
627 | |
---|
628 | f1 = interp.fit(data_points1,z) #Fitted values at vertices (using same z as before) |
---|
629 | |
---|
630 | assert allclose(f,f1), 'Fit should have been unaltered' |
---|
631 | |
---|
632 | |
---|
633 | def test_smooth_attributes_to_mesh_function(self): |
---|
634 | """ Testing 2 attributes smoothed to the mesh |
---|
635 | """ |
---|
636 | |
---|
637 | a = [0.0, 0.0] |
---|
638 | b = [0.0, 5.0] |
---|
639 | c = [5.0, 0.0] |
---|
640 | points = [a, b, c] |
---|
641 | triangles = [ [1,0,2] ] #bac |
---|
642 | |
---|
643 | d1 = [1.0, 1.0] |
---|
644 | d2 = [1.0, 3.0] |
---|
645 | d3 = [3.0, 1.0] |
---|
646 | z1 = [2, 4] |
---|
647 | z2 = [4, 8] |
---|
648 | z3 = [4, 8] |
---|
649 | data_coords = [d1, d2, d3] |
---|
650 | z = [z1, z2, z3] |
---|
651 | |
---|
652 | f = fit_to_mesh(points, triangles, data_coords, z, alpha=0.0) |
---|
653 | answer = [[0, 0], [5., 10.], [5., 10.]] |
---|
654 | |
---|
655 | assert allclose(f, answer) |
---|
656 | |
---|
657 | def test_fit_to_mesh_w_georef(self): |
---|
658 | """Simple check that georef works at the fit_to_mesh level |
---|
659 | """ |
---|
660 | |
---|
661 | from anuga.coordinate_transforms.geo_reference import Geo_reference |
---|
662 | |
---|
663 | #Mesh |
---|
664 | vertex_coordinates = [[0.76, 0.76], |
---|
665 | [0.76, 5.76], |
---|
666 | [5.76, 0.76]] |
---|
667 | triangles = [[0,2,1]] |
---|
668 | |
---|
669 | mesh_geo = Geo_reference(56,-0.76,-0.76) |
---|
670 | #print "mesh_geo.get_absolute(vertex_coordinates)", \ |
---|
671 | # mesh_geo.get_absolute(vertex_coordinates) |
---|
672 | |
---|
673 | #Data |
---|
674 | data_points = [[ 201.0, 401.0], |
---|
675 | [ 201.0, 403.0], |
---|
676 | [ 203.0, 401.0]] |
---|
677 | |
---|
678 | z = [2, 4, 4] |
---|
679 | |
---|
680 | data_geo = Geo_reference(56,-200,-400) |
---|
681 | |
---|
682 | #print "data_geo.get_absolute(data_points)", \ |
---|
683 | # data_geo.get_absolute(data_points) |
---|
684 | |
---|
685 | #Fit |
---|
686 | zz = fit_to_mesh(vertex_coordinates, triangles, data_points, z, |
---|
687 | data_origin = data_geo.get_origin(), |
---|
688 | mesh_origin = mesh_geo.get_origin(), |
---|
689 | alpha = 0) |
---|
690 | assert allclose( zz, [0,5,5] ) |
---|
691 | |
---|
692 | |
---|
693 | def Not_yet_test_smooth_att_to_mesh_with_excess_verts(self): |
---|
694 | |
---|
695 | a = [0.0, 0.0] |
---|
696 | b = [0.0, 5.0] |
---|
697 | c = [5.0, 0.0] |
---|
698 | d = [1.0, 1.0] |
---|
699 | e = [18.0, 1000.0] |
---|
700 | |
---|
701 | points = [a, b, c, d, e] |
---|
702 | triangles = [ [1,0,2] ] #bac |
---|
703 | |
---|
704 | d1 = [1.0, 1.0] |
---|
705 | d2 = [1.0, 2.0] |
---|
706 | d3 = [3.0,1.0] |
---|
707 | d4 = [2.0,3.0] |
---|
708 | d5 = [2.0,2.0] |
---|
709 | d6 = [1.0,3.0] |
---|
710 | data_coords = [d1, d2, d3, d4, d5, d6] |
---|
711 | z = linear_function(data_coords) |
---|
712 | #print "z",z |
---|
713 | |
---|
714 | interp = Fit(points, triangles, alpha=0.0) |
---|
715 | |
---|
716 | try: |
---|
717 | f = interp.fit(data_coords, z) |
---|
718 | except VertsWithNoTrianglesError: |
---|
719 | pass |
---|
720 | else: |
---|
721 | raise 'Verts with no triangles did not raise error!' |
---|
722 | |
---|
723 | #f = interp.fit(data_coords, z) |
---|
724 | #answer = linear_function(points) |
---|
725 | |
---|
726 | # Removing the bad verts that we don't care about |
---|
727 | #f = f[0:3] |
---|
728 | #answer = answer[0:3] |
---|
729 | #assert allclose(f, answer) |
---|
730 | |
---|
731 | #------------------------------------------------------------- |
---|
732 | if __name__ == "__main__": |
---|
733 | suite = unittest.makeSuite(Test_Fit,'test') |
---|
734 | #suite = unittest.makeSuite(Test_Fit,'test_smooth_att_to_mesh_with_excess_verts') |
---|
735 | #suite = unittest.makeSuite(Test_Fit,'test_smooth_attributes_to_mesh_one_point') |
---|
736 | runner = unittest.TextTestRunner(verbosity=1) |
---|
737 | runner.run(suite) |
---|
738 | |
---|
739 | |
---|
740 | |
---|
741 | |
---|
742 | |
---|