source: anuga_core/source/anuga/fit_interpolate/test_fit.py @ 4150

Last change on this file since 4150 was 4144, checked in by duncan, 18 years ago

Changing the mesh generator and triangle interface to work with Numeric arrys, as input. Fixed to work on Linux

File size: 29.8 KB
Line 
1#!/usr/bin/env python
2
3#TEST
4import sys
5import unittest
6from math import sqrt
7import tempfile
8
9from Numeric import zeros, take, compress, Float, Int, dot, concatenate, \
10     ArrayType, allclose, array
11
12from fit import *
13from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh
14from anuga.utilities.sparse import Sparse, Sparse_CSR
15from anuga.coordinate_transforms.geo_reference import Geo_reference
16from anuga.utilities.numerical_tools import ensure_numeric
17from anuga.geospatial_data.geospatial_data import Geospatial_data
18
19def distance(x, y):
20    return sqrt( sum( (array(x)-array(y))**2 ))
21
22def linear_function(point):
23    point = array(point)
24    return point[:,0]+point[:,1]
25
26
27class 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\
2240.0, 2.0 , 2.\n\
2251.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
242    def test_fit_to_mesh(self):
243
244        a = [-1.0, 0.0]
245        b = [3.0, 4.0]
246        c = [4.0,1.0]
247        d = [-3.0, 2.0] #3
248        e = [-1.0,-2.0]
249        f = [1.0, -2.0] #5
250
251        vertices = [a, b, c, d,e,f]
252        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
253
254
255        fileName = tempfile.mktemp(".ddd")
256        file = open(fileName,"w")
257        file.write(" x,y, elevation \n\
258-2.0, 2.0, 0.\n\
259-1.0, 1.0, 0.\n\
2600.0, 2.0 , 2.\n\
2611.0, 1.0 , 2.\n\
262 2.0,  1.0 ,3. \n\
263 0.0,  0.0 , 0.\n\
264 1.0,  0.0 , 1.\n\
265 0.0,  -1.0, -1.\n\
266 -0.2, -0.5, -0.7\n\
267 -0.9, -1.5, -2.4\n\
268 0.5,  -1.9, -1.4\n\
269 3.0,  1.0 , 4.\n")
270        file.close()
271       
272        f = fit_to_mesh(vertices, triangles,fileName,
273                                alpha=0.0, max_read_lines=2)#, verbose=True)
274        answer = linear_function(vertices)
275        #print "f\n",f
276        #print "answer\n",answer
277        assert allclose(f, answer)
278       
279    def test_fit_to_mesh_2_atts(self):
280
281        a = [-1.0, 0.0]
282        b = [3.0, 4.0]
283        c = [4.0,1.0]
284        d = [-3.0, 2.0] #3
285        e = [-1.0,-2.0]
286        f = [1.0, -2.0] #5
287
288        vertices = [a, b, c, d,e,f]
289        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
290
291
292        fileName = tempfile.mktemp(".ddd")
293        file = open(fileName,"w")
294        # the 2nd att name is wacky so it's the first key off a hash table
295        file.write(" x,y, elevation, afriqction \n\
296-2.0, 2.0, 0., 0.\n\
297-1.0, 1.0, 0., 0.\n\
2980.0, 2.0 , 2., 20.\n\
2991.0, 1.0 , 2., 20.\n\
300 2.0,  1.0 ,3., 30. \n\
301 0.0,  0.0 , 0., 0.\n\
302 1.0,  0.0 , 1., 10.\n\
303 0.0,  -1.0, -1., -10.\n\
304 -0.2, -0.5, -0.7, -7.\n\
305 -0.9, -1.5, -2.4, -24. \n\
306 0.5,  -1.9, -1.4, -14. \n\
307 3.0,  1.0 , 4., 40. \n")
308        file.close()
309       
310        f = fit_to_mesh(vertices, triangles,fileName,
311                        alpha=0.0, 
312                        attribute_name='elevation', max_read_lines=2)
313        answer = linear_function(vertices)
314        #print "f\n",f
315        #print "answer\n",answer
316        assert allclose(f, answer)
317       
318    def test_fit_and_interpolation(self):
319
320        a = [0.0, 0.0]
321        b = [0.0, 2.0]
322        c = [2.0, 0.0]
323        d = [0.0, 4.0]
324        e = [2.0, 2.0]
325        f = [4.0, 0.0]
326
327        points = [a, b, c, d, e, f]
328        #bac, bce, ecf, dbe, daf, dae
329        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
330
331        #Get (enough) datapoints
332        data_points = [[ 0.66666667, 0.66666667],
333                       [ 1.33333333, 1.33333333],
334                       [ 2.66666667, 0.66666667],
335                       [ 0.66666667, 2.66666667],
336                       [ 0.0, 1.0],
337                       [ 0.0, 3.0],
338                       [ 1.0, 0.0],
339                       [ 1.0, 1.0],
340                       [ 1.0, 2.0],
341                       [ 1.0, 3.0],
342                       [ 2.0, 1.0],
343                       [ 3.0, 0.0],
344                       [ 3.0, 1.0]]
345
346        z = linear_function(data_points)
347        interp = Fit(points, triangles,
348                                alpha=0.0)
349
350        answer = linear_function(points)
351
352        f = interp.fit(data_points, z)
353
354        #print "f",f
355        #print "answer",answer
356        assert allclose(f, answer)
357
358       
359    def test_smoothing_and_interpolation(self):
360
361        a = [0.0, 0.0]
362        b = [0.0, 2.0]
363        c = [2.0, 0.0]
364        d = [0.0, 4.0]
365        e = [2.0, 2.0]
366        f = [4.0, 0.0]
367
368        points = [a, b, c, d, e, f]
369        #bac, bce, ecf, dbe, daf, dae
370        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
371
372        #Get (too few!) datapoints
373        data_points = [[ 0.66666667, 0.66666667],
374                       [ 1.33333333, 1.33333333],
375                       [ 2.66666667, 0.66666667],
376                       [ 0.66666667, 2.66666667]]
377
378        z = linear_function(data_points)
379        answer = linear_function(points)
380
381        #Make interpolator with too few data points and no smoothing
382        interp = Fit(points, triangles, alpha=0.0)
383        #Must raise an exception
384        try:
385            f = interp.fit(data_points,z)
386        except ToFewPointsError:
387            pass
388
389        #Now try with smoothing parameter
390        interp = Fit(points, triangles, alpha=1.0e-13)
391
392        f = interp.fit(data_points,z)
393        #f will be different from answer due to smoothing
394        assert allclose(f, answer,atol=5)
395
396
397    #Tests of smoothing matrix
398    def test_smoothing_matrix_one_triangle(self):
399        from Numeric import dot
400        a = [0.0, 0.0]
401        b = [0.0, 2.0]
402        c = [2.0,0.0]
403        points = [a, b, c]
404
405        vertices = [ [1,0,2] ]   #bac
406
407        interp = Fit(points, vertices)
408
409        assert allclose(interp.get_D(), [[1, -0.5, -0.5],
410                                   [-0.5, 0.5, 0],
411                                   [-0.5, 0, 0.5]])
412
413        #Define f(x,y) = x
414        f = array([0,0,2]) #Value at global vertex 2
415
416        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
417        #           int 1 dx dy = area = 2
418        assert dot(dot(f, interp.get_D()), f) == 2
419
420        #Define f(x,y) = y
421        f = array([0,2,0])  #Value at global vertex 1
422
423        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
424        #           int 1 dx dy = area = 2
425        assert dot(dot(f, interp.get_D()), f) == 2
426
427        #Define f(x,y) = x+y
428        f = array([0,2,2])  #Values at global vertex 1 and 2
429
430        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
431        #           int 2 dx dy = 2*area = 4
432        assert dot(dot(f, interp.get_D()), f) == 4
433
434
435    def test_smoothing_matrix_more_triangles(self):
436        from Numeric import dot
437
438        a = [0.0, 0.0]
439        b = [0.0, 2.0]
440        c = [2.0,0.0]
441        d = [0.0, 4.0]
442        e = [2.0, 2.0]
443        f = [4.0,0.0]
444
445        points = [a, b, c, d, e, f]
446        #bac, bce, ecf, dbe, daf, dae
447        vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]]
448
449        interp = Fit(points, vertices)
450
451
452        #assert allclose(interp.get_D(), [[1, -0.5, -0.5],
453        #                           [-0.5, 0.5, 0],
454        #                           [-0.5, 0, 0.5]])
455
456        #Define f(x,y) = x
457        f = array([0,0,2,0,2,4]) #f evaluated at points a-f
458
459        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
460        #           int 1 dx dy = total area = 8
461        assert dot(dot(f, interp.get_D()), f) == 8
462
463        #Define f(x,y) = y
464        f = array([0,2,0,4,2,0]) #f evaluated at points a-f
465
466        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
467        #           int 1 dx dy = area = 8
468        assert dot(dot(f, interp.get_D()), f) == 8
469
470        #Define f(x,y) = x+y
471        f = array([0,2,2,4,4,4])  #f evaluated at points a-f
472
473        #Check that int (df/dx)**2 + (df/dy)**2 dx dy =
474        #           int 2 dx dy = 2*area = 16
475        assert dot(dot(f, interp.get_D()), f) == 16
476
477
478
479    def test_fit_and_interpolation_with_different_origins(self):
480        """Fit a surface to one set of points. Then interpolate that surface
481        using another set of points.
482        This test tests situtaion where points and mesh belong to a different
483        coordinate system as defined by origin.
484        """
485
486        #Setup mesh used to represent fitted function
487        a = [0.0, 0.0]
488        b = [0.0, 2.0]
489        c = [2.0, 0.0]
490        d = [0.0, 4.0]
491        e = [2.0, 2.0]
492        f = [4.0, 0.0]
493
494        points = [a, b, c, d, e, f]
495        #bac, bce, ecf, dbe, daf, dae
496        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
497
498        #Datapoints to fit from
499        data_points1 = [[ 0.66666667, 0.66666667],
500                        [ 1.33333333, 1.33333333],
501                        [ 2.66666667, 0.66666667],
502                        [ 0.66666667, 2.66666667],
503                        [ 0.0, 1.0],
504                        [ 0.0, 3.0],
505                        [ 1.0, 0.0],
506                        [ 1.0, 1.0],
507                        [ 1.0, 2.0],
508                        [ 1.0, 3.0],
509                        [ 2.0, 1.0],
510                        [ 3.0, 0.0],
511                        [ 3.0, 1.0]]
512
513
514        #First check that things are OK when using same origin
515        mesh_origin = (56, 290000, 618000) #zone, easting, northing
516        data_origin = (56, 290000, 618000) #zone, easting, northing
517
518
519        #Fit surface to mesh
520        interp = Fit(points, triangles, 
521                               alpha=0.0,
522                               mesh_origin = mesh_origin)
523
524        data_geo_spatial = Geospatial_data(data_points1,
525                         geo_reference = Geo_reference(56, 290000, 618000))
526        z = linear_function(data_points1) #Example z-values
527        f = interp.fit(data_geo_spatial, z)   #Fitted values at vertices
528
529        #Shift datapoints according to new origins
530        for k in range(len(data_points1)):
531            data_points1[k][0] += mesh_origin[1] - data_origin[1]
532            data_points1[k][1] += mesh_origin[2] - data_origin[2]
533
534
535
536        #Fit surface to mesh
537        interp = Fit(points, triangles, 
538                               alpha=0.0) #,
539                               # mesh_origin = mesh_origin)
540
541        f1 = interp.fit(data_points1,z) #Fitted values at vertices (using same z as before)
542
543        assert allclose(f,f1), 'Fit should have been unaltered'
544
545
546    def test_smooth_attributes_to_mesh_function(self):
547        """ Testing 2 attributes smoothed to the mesh
548        """
549
550        a = [0.0, 0.0]
551        b = [0.0, 5.0]
552        c = [5.0, 0.0]
553        points = [a, b, c]
554        triangles = [ [1,0,2] ]   #bac
555
556        d1 = [1.0, 1.0]
557        d2 = [1.0, 3.0]
558        d3 = [3.0, 1.0]
559        z1 = [2, 4]
560        z2 = [4, 8]
561        z3 = [4, 8]
562        data_coords = [d1, d2, d3]
563        z = [z1, z2, z3]
564
565        f = fit_to_mesh(points, triangles, data_coords, z, alpha=0.0)
566        answer = [[0, 0], [5., 10.], [5., 10.]]
567
568        assert allclose(f, answer)
569
570    def test_fit_to_mesh_w_georef(self):
571        """Simple check that georef works at the fit_to_mesh level
572        """
573       
574        from anuga.coordinate_transforms.geo_reference import Geo_reference
575
576        #Mesh
577        vertex_coordinates = [[0.76, 0.76],
578                              [0.76, 5.76],
579                              [5.76, 0.76]]
580        triangles = [[0,2,1]]
581
582        mesh_geo = Geo_reference(56,-0.76,-0.76)
583        #print "mesh_geo.get_absolute(vertex_coordinates)", \
584         #     mesh_geo.get_absolute(vertex_coordinates)
585
586        #Data                       
587        data_points = [[ 201.0, 401.0],
588                       [ 201.0, 403.0],
589                       [ 203.0, 401.0]]
590
591        z = [2, 4, 4]
592       
593        data_geo = Geo_reference(56,-200,-400)
594
595        #print "data_geo.get_absolute(data_points)", \
596        #      data_geo.get_absolute(data_points)
597       
598        #Fit
599        zz = fit_to_mesh(vertex_coordinates, triangles, data_points, z,
600                         data_origin = data_geo.get_origin(),
601                         mesh_origin = mesh_geo.get_origin(),
602                         alpha = 0)
603        assert allclose( zz, [0,5,5] )
604
605
606    def test_fit_to_mesh_file(self):
607        from load_mesh.loadASCII import import_mesh_file, \
608             export_mesh_file
609        import tempfile
610        import os
611
612        # create a .tsh file, no user outline
613        mesh_dic = {}
614        mesh_dic['vertices'] = [[0.0, 0.0],
615                                          [0.0, 5.0],
616                                          [5.0, 0.0]]
617        mesh_dic['triangles'] =  [[0, 2, 1]]
618        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
619        mesh_dic['triangle_tags'] = ['']
620        mesh_dic['vertex_attributes'] = [[], [], []]
621        mesh_dic['vertiex_attribute_titles'] = []
622        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
623        mesh_dic['segment_tags'] = ['external',
624                                                  'external',
625                                                  'external']
626        mesh_file = tempfile.mktemp(".tsh")
627        export_mesh_file(mesh_file,mesh_dic)
628
629        # create an .xya file
630        point_file = tempfile.mktemp(".xya")
631        fd = open(point_file,'w')
632        fd.write("elevation, stage \n 1.0, 1.0,2.,4 \n 1.0, 3.0,4,8 \n 3.0,1.0,4.,8 \n")
633        fd.close()
634
635        mesh_output_file = tempfile.mktemp(".tsh") 
636        fit_to_mesh_file(mesh_file,
637                         point_file,
638                         mesh_output_file,
639                         alpha = 0.0)
640        # load in the .tsh file we just wrote
641        mesh_dic = import_mesh_file(mesh_output_file)
642        #print "mesh_dic",mesh_dic
643        ans =[[0.0, 0.0],
644              [5.0, 10.0],
645              [5.0,10.0]]
646        assert allclose(mesh_dic['vertex_attributes'],ans)
647
648        self.failUnless(mesh_dic['vertex_attribute_titles']  ==
649                        ['elevation','stage'],
650                        'test_fit_to_mesh_file failed')
651
652        #clean up
653        os.remove(mesh_file)
654        os.remove(point_file)
655        os.remove(mesh_output_file)
656
657
658    def test_fit_to_mesh_file3(self):
659        from load_mesh.loadASCII import import_mesh_file, \
660             export_mesh_file
661        import tempfile
662        import os
663
664        # create a .tsh file, no user outline
665        mesh_dic = {}
666        mesh_dic['vertices'] = [[0.76, 0.76],
667                                          [0.76, 5.76],
668                                          [5.76, 0.76]]
669        mesh_dic['triangles'] =  [[0, 2, 1]]
670        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
671        mesh_dic['triangle_tags'] = ['']
672        mesh_dic['vertex_attributes'] = [[], [], []]
673        mesh_dic['vertiex_attribute_titles'] = []
674        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
675        mesh_dic['segment_tags'] = ['external',
676                                                  'external',
677                                                  'external']
678        mesh_dic['geo_reference'] = Geo_reference(56,-0.76,-0.76)
679        mesh_file = tempfile.mktemp(".tsh")
680        export_mesh_file(mesh_file,mesh_dic)
681
682        # create an .xya file
683        point_file = tempfile.mktemp(".xya")
684        fd = open(point_file,'w')
685        fd.write("elevation, stage \n 1.0, 1.0,2.,4 \n 1.0, 3.0,4,8 \n 3.0,1.0,4.,8 \n")
686        fd.close()
687
688        mesh_output_file = tempfile.mktemp(".tsh")
689        fit_to_mesh_file(mesh_file,
690                         point_file,
691                         mesh_output_file,
692                         alpha = 0.0)
693        # load in the .tsh file we just wrote
694        mesh_dic = import_mesh_file(mesh_output_file)
695        #print "mesh_dic",mesh_dic
696        ans =[[0.0, 0.0],
697              [5.0, 10.0],
698              [5.0,10.0]]
699        assert allclose(mesh_dic['vertex_attributes'],ans)
700
701        self.failUnless(mesh_dic['vertex_attribute_titles']  ==
702                        ['elevation','stage'],
703                        'test_fit_to_mesh_file failed')
704
705        #clean up
706        os.remove(mesh_file)
707        os.remove(point_file)
708        os.remove(mesh_output_file)
709
710    def test_fit_to_mesh_file4(self):
711        from load_mesh.loadASCII import import_mesh_file, \
712             export_mesh_file
713        import tempfile
714        import os
715
716        # create a .tsh file, no user outline
717        mesh_dic = {}
718        mesh_dic['vertices'] = [[0.76, 0.76],
719                                [0.76, 5.76],
720                                [5.76, 0.76]]
721        mesh_dic['triangles'] =  [[0, 2, 1]]
722        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
723        mesh_dic['triangle_tags'] = ['']
724        mesh_dic['vertex_attributes'] = [[], [], []]
725        mesh_dic['vertiex_attribute_titles'] = []
726        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
727        mesh_dic['segment_tags'] = ['external',
728                                    'external',
729                                    'external']
730        mesh_dic['geo_reference'] = Geo_reference(56,-0.76,-0.76)
731        mesh_file = tempfile.mktemp(".tsh")
732        export_mesh_file(mesh_file,mesh_dic)
733
734        geo_ref = Geo_reference(56,-200,-400)
735        # create an .xya file
736        point_file = tempfile.mktemp(".xya")
737        fd = open(point_file,'w')
738        fd.write("elevation, stage \n 201.0, 401.0,2.,4 \n 201.0, 403.0,4,8 \n 203.0, 401.0,4.,8 \n")
739        geo_ref.write_ASCII(fd)
740        fd.close()
741
742        mesh_output_file = tempfile.mktemp(".tsh")
743        fit_to_mesh_file(mesh_file,
744                         point_file,
745                         mesh_output_file,
746                         alpha = 0.0)
747        # load in the .tsh file we just wrote
748        mesh_dic = import_mesh_file(mesh_output_file)
749        #print "mesh_dic",mesh_dic
750        ans =[[0.0, 0.0],
751              [5.0, 10.0],
752              [5.0, 10.0]]
753        assert allclose(mesh_dic['vertex_attributes'],ans)
754
755        self.failUnless(mesh_dic['vertex_attribute_titles']  ==
756                        ['elevation','stage'],
757                        'test_fit_to_mesh_file failed')
758
759        #clean up
760        os.remove(mesh_file)
761        os.remove(point_file)
762        os.remove(mesh_output_file)
763
764    def test_fit_to_mesh_fileII(self):
765        from load_mesh.loadASCII import import_mesh_file, \
766             export_mesh_file
767        import tempfile
768        import os
769
770        # create a .tsh file, no user outline
771        mesh_dic = {}
772        mesh_dic['vertices'] = [[0.0, 0.0],
773                                [0.0, 5.0],
774                                [5.0, 0.0]]
775        mesh_dic['triangles'] =  [[0, 2, 1]]
776        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
777        mesh_dic['triangle_tags'] = ['']
778        mesh_dic['vertex_attributes'] = [[1,2], [1,2], [1,2]]
779        mesh_dic['vertex_attribute_titles'] = ['density', 'temp']
780        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
781        mesh_dic['segment_tags'] = ['external',
782                                                  'external',
783                                                  'external']
784        mesh_file = tempfile.mktemp(".tsh")
785        export_mesh_file(mesh_file,mesh_dic)
786
787        # create an .xya file
788        point_file = tempfile.mktemp(".xya")
789        fd = open(point_file,'w')
790        fd.write("elevation, stage \n 1.0, 1.0,2.,4 \n 1.0, 3.0,4,8 \n 3.0,1.0,4.,8 \n")
791        fd.close()
792
793        mesh_output_file = "new_triangle.tsh"
794        fit_to_mesh_file(mesh_file,
795                         point_file,
796                         mesh_output_file,
797                         alpha = 0.0)
798        # load in the .tsh file we just wrote
799        mesh_dic = import_mesh_file(mesh_output_file)
800
801        assert allclose(mesh_dic['vertex_attributes'],
802                        [[1.0, 2.0,0.0, 0.0],
803                         [1.0, 2.0,5.0, 10.0],
804                         [1.0, 2.0,5.0,10.0]])
805
806        self.failUnless(mesh_dic['vertex_attribute_titles']  ==
807                        ['density', 'temp','elevation','stage'],
808                        'test_fit_to_mesh_file failed')
809
810        #clean up
811        os.remove(mesh_file)
812        os.remove(mesh_output_file)
813        os.remove(point_file)
814
815    def test_fit_to_mesh_file_errors(self):
816        from load_mesh.loadASCII import import_mesh_file, export_mesh_file
817        import tempfile
818        import os
819
820        # create a .tsh file, no user outline
821        mesh_dic = {}
822        mesh_dic['vertices'] = [[0.0, 0.0],[0.0, 5.0],[5.0, 0.0]]
823        mesh_dic['triangles'] =  [[0, 2, 1]]
824        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
825        mesh_dic['triangle_tags'] = ['']
826        mesh_dic['vertex_attributes'] = [[1,2], [1,2], [1,2]]
827        mesh_dic['vertex_attribute_titles'] = ['density', 'temp']
828        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
829        mesh_dic['segment_tags'] = ['external', 'external','external']
830        mesh_file = tempfile.mktemp(".tsh")
831        export_mesh_file(mesh_file,mesh_dic)
832
833        # create an .xya file
834        point_file = tempfile.mktemp(".xya")
835        fd = open(point_file,'w')
836        fd.write("elevation stage \n 1.0, 1.0,2.,4 \n 1.0, 3.0,4,8 \n 3.0,1.0,4.,8 \n")
837        fd.close()
838
839        mesh_output_file = "new_triangle.tsh"
840        try:
841            fit_to_mesh_file(mesh_file, point_file,
842                             mesh_output_file, display_errors = False)
843        except IOError:
844            pass
845        else:
846            #self.failUnless(0 ==1,  'Bad file did not raise error!')
847            raise 'Bad file did not raise error!'
848           
849        #clean up
850        os.remove(mesh_file)
851        os.remove(point_file)
852
853    def test_fit_to_mesh_file_errorsII(self):
854        from load_mesh.loadASCII import import_mesh_file, export_mesh_file
855        import tempfile
856        import os
857
858        # create a .tsh file, no user outline
859        mesh_file = tempfile.mktemp(".tsh")
860        fd = open(mesh_file,'w')
861        fd.write("unit testing a bad .tsh file \n")
862        fd.close()
863
864        # create an .xya file
865        point_file = tempfile.mktemp(".xya")
866        fd = open(point_file,'w')
867        fd.write("elevation, stage \n 1.0, 1.0,2.,4 \n 1.0, 3.0,4,8 \n 3.0,1.0,4.,8 \n")
868        fd.close()
869
870        mesh_output_file = "new_triangle.tsh"
871        try:
872            fit_to_mesh_file(mesh_file, point_file,
873                             mesh_output_file, display_errors = False)
874        except IOError:
875            pass
876        else:
877            raise 'Bad file did not raise error!'
878           
879        #clean up
880        os.remove(mesh_file)
881        os.remove(point_file)
882
883    def test_fit_to_mesh_file_errorsIII(self):
884        from load_mesh.loadASCII import import_mesh_file, export_mesh_file
885        import tempfile
886        import os
887
888        # create a .tsh file, no user outline
889        mesh_dic = {}
890        mesh_dic['vertices'] = [[0.0, 0.0],[0.0, 5.0],[5.0, 0.0]]
891        mesh_dic['triangles'] =  [[0, 2, 1]]
892        mesh_dic['segments'] = [[0, 1], [2, 0], [1, 2]]
893        mesh_dic['triangle_tags'] = ['']
894        mesh_dic['vertex_attributes'] = [[1,2], [1,2], [1,2]]
895        mesh_dic['vertex_attribute_titles'] = ['density', 'temp']
896        mesh_dic['triangle_neighbors'] = [[-1, -1, -1]]
897        mesh_dic['segment_tags'] = ['external', 'external','external']
898        mesh_file = tempfile.mktemp(".tsh")
899        export_mesh_file(mesh_file,mesh_dic)
900
901        # create an .xya file
902        point_file = tempfile.mktemp(".xya")
903        fd = open(point_file,'w')
904        fd.write("elevation, stage \n 1.0, 1.0,2.,4 \n 1.0, 3.0,4,8 \n 3.0,1.0,4.,8 \n")
905        fd.close()
906
907        #This a deliberately illegal filename to invoke the error.
908        mesh_output_file = ".../\z\z:ya.tsh"       
909
910        try:
911            fit_to_mesh_file(mesh_file, point_file,
912                             mesh_output_file, display_errors = False)
913        except IOError:
914            pass
915        else:
916            raise 'Bad file did not raise error!'
917       
918        #clean up
919        os.remove(mesh_file)
920        os.remove(point_file)
921
922
923    def Not_yet_test_smooth_att_to_mesh_with_excess_verts(self):
924
925        a = [0.0, 0.0]
926        b = [0.0, 5.0]
927        c = [5.0, 0.0]
928        d = [1.0, 1.0]
929        e = [18.0, 1000.0]
930       
931        points = [a, b, c, d, e]
932        triangles = [ [1,0,2] ]   #bac
933
934        d1 = [1.0, 1.0]
935        d2 = [1.0, 2.0]
936        d3 = [3.0,1.0]
937        d4 = [2.0,3.0]
938        d5 = [2.0,2.0]
939        d6 = [1.0,3.0]
940        data_coords = [d1, d2, d3, d4, d5, d6]
941        z = linear_function(data_coords)
942        #print "z",z
943
944        interp = Fit(points, triangles, alpha=0.0)
945       
946        try:
947            f = interp.fit(data_coords, z)
948        except VertsWithNoTrianglesError:
949            pass
950        else:
951            raise 'Verts with no triangles did not raise error!'
952       
953        #f = interp.fit(data_coords, z)
954        #answer = linear_function(points)
955
956        #  Removing the bad verts that we don't care about
957        #f = f[0:3]
958        #answer = answer[0:3]
959        #assert allclose(f, answer)
960
961#-------------------------------------------------------------
962if __name__ == "__main__":
963    suite = unittest.makeSuite(Test_Fit,'test')
964    #suite = unittest.makeSuite(Test_Fit,'test_smooth_att_to_mesh_with_excess_verts')
965    #suite = unittest.makeSuite(Test_Fit,'test_smooth_attributes_to_mesh_one_point')
966    runner = unittest.TextTestRunner(verbosity=1)
967    runner.run(suite)
968
969
970
971
972
Note: See TracBrowser for help on using the repository browser.