1 | """Least squares smooting and interpolation. |
---|
2 | |
---|
3 | measure the speed of least squares. |
---|
4 | |
---|
5 | Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou |
---|
6 | Geoscience Australia, 2004. |
---|
7 | """ |
---|
8 | |
---|
9 | |
---|
10 | import unittest |
---|
11 | from math import sqrt |
---|
12 | |
---|
13 | |
---|
14 | from least_squares import * |
---|
15 | from Numeric import allclose, array, transpose |
---|
16 | |
---|
17 | from coordinate_transforms.geo_reference import Geo_reference |
---|
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_Least_Squares(unittest.TestCase): |
---|
28 | |
---|
29 | def setUp(self): |
---|
30 | pass |
---|
31 | |
---|
32 | def tearDown(self): |
---|
33 | pass |
---|
34 | |
---|
35 | def test_expand_search2(self): |
---|
36 | from random import seed, random |
---|
37 | from pmesh.mesh import Mesh |
---|
38 | |
---|
39 | seed(2) |
---|
40 | m = Mesh() |
---|
41 | verts_per_sector = 100 #100 |
---|
42 | num_of_points = 300 |
---|
43 | sec_side = 9.0 |
---|
44 | |
---|
45 | vert_atts = [] |
---|
46 | for vert in range(verts_per_sector): |
---|
47 | m.addUserVertex(random()*sec_side, random()*sec_side) |
---|
48 | vert_atts.append(random()) |
---|
49 | for vert in range(verts_per_sector): |
---|
50 | m.addUserVertex(90.0+random()*sec_side, 90.0+random()*sec_side) |
---|
51 | vert_atts.append(random()) |
---|
52 | for vert in range(verts_per_sector): |
---|
53 | m.addUserVertex(random()*sec_side, 90.0+random()*sec_side) |
---|
54 | vert_atts.append(random()) |
---|
55 | for vert in range(verts_per_sector): |
---|
56 | m.addUserVertex(90.0+random()*sec_side, random()*sec_side) |
---|
57 | vert_atts.append(random()) |
---|
58 | #for vert in range(verts_per_sector): |
---|
59 | # m.addUserVertex(40.0+random()*sec_side, 40.0+random()*sec_side) |
---|
60 | # vert_atts.append(random()) |
---|
61 | |
---|
62 | m.autoSegment(alpha = 100 ) |
---|
63 | m.generateMesh(mode = "Q") |
---|
64 | m.export_mesh_file("aaaa.tsh") |
---|
65 | mesh_dict = m.Mesh2IOTriangulationDict() |
---|
66 | |
---|
67 | points = [] |
---|
68 | point_atts = [] |
---|
69 | for point in range(num_of_points): |
---|
70 | points.append([random()*100, random()*100]) |
---|
71 | point_atts.append(10.0) |
---|
72 | m.autoSegment(alpha = 100 ) |
---|
73 | |
---|
74 | #print 'points', points |
---|
75 | t0 = time.time() |
---|
76 | interp = Interpolation(mesh_dict['vertices'], |
---|
77 | mesh_dict['triangles'], points, |
---|
78 | alpha=0.2, expand_search=False, #True, |
---|
79 | verbose = False, |
---|
80 | max_points_per_cell = 4) |
---|
81 | calc = interp.fit_points(point_atts ) |
---|
82 | #print "calc", calc |
---|
83 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
84 | |
---|
85 | #------------------------------------------------------------- |
---|
86 | if __name__ == "__main__": |
---|
87 | suite = unittest.makeSuite(Test_Least_Squares,'test') |
---|
88 | runner = unittest.TextTestRunner(verbosity=1) |
---|
89 | runner.run(suite) |
---|