1 | """Least squares smooting and interpolation. |
---|
2 | |
---|
3 | measure the speed of least squares. |
---|
4 | |
---|
5 | ________________________ |
---|
6 | General comments |
---|
7 | |
---|
8 | The max_points_per_cell does effect the time spent solving a |
---|
9 | problem. The best value to use is probably dependent on the number |
---|
10 | of triangles. Maybe develop a simple imperical algorithm, based on |
---|
11 | test results. |
---|
12 | |
---|
13 | Duncan Gray |
---|
14 | Geoscience Australia, 2004. |
---|
15 | """ |
---|
16 | |
---|
17 | |
---|
18 | import os |
---|
19 | import sys |
---|
20 | import time |
---|
21 | from random import seed, random |
---|
22 | |
---|
23 | from pyvolution.least_squares import Interpolation |
---|
24 | from fit_interpolate.interpolate import Interpolate |
---|
25 | from fit_interpolate.fit import Fit |
---|
26 | from pmesh.mesh import Mesh |
---|
27 | |
---|
28 | def mem_usage(): |
---|
29 | ''' |
---|
30 | returns the rss. |
---|
31 | |
---|
32 | RSS The total amount of physical memory used by the task, in kilo- |
---|
33 | bytes, is shown here. For ELF processes used library pages are |
---|
34 | counted here, for a.out processes not. |
---|
35 | |
---|
36 | Only works on nix systems. |
---|
37 | ''' |
---|
38 | import string |
---|
39 | p=os.popen('ps uwp %s'%os.getpid()) |
---|
40 | lines=p.readlines() |
---|
41 | #print "lines", lines |
---|
42 | status=p.close() |
---|
43 | if status or len(lines)!=2 or sys.platform == 'win32': |
---|
44 | return None |
---|
45 | return int(string.split(lines[1])[4]) |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | |
---|
50 | class BenchmarkLeastSquares: |
---|
51 | |
---|
52 | |
---|
53 | def __init__(self): |
---|
54 | pass |
---|
55 | |
---|
56 | def trial(self, |
---|
57 | num_of_points=20000, |
---|
58 | maxArea=1000, |
---|
59 | max_points_per_cell=4, |
---|
60 | is_fit=True, |
---|
61 | use_least_squares=True, |
---|
62 | save=False): |
---|
63 | ''' |
---|
64 | num_of_points |
---|
65 | ''' |
---|
66 | |
---|
67 | #print "num_of_points",num_of_points |
---|
68 | #print "maxArea",maxArea |
---|
69 | #print "max_points_per_cell", max_points_per_cell |
---|
70 | |
---|
71 | mesh_dict = self._build_regular_mesh_dict(maxArea=maxArea, |
---|
72 | save=save) |
---|
73 | points_dict = self._build_points_dict(num_of_points=num_of_points) |
---|
74 | |
---|
75 | #Initial time and memory |
---|
76 | t0 = time.time() |
---|
77 | #m0 = None on windows |
---|
78 | m0 = mem_usage() |
---|
79 | |
---|
80 | if use_least_squares is True: |
---|
81 | interp = Interpolation(mesh_dict['vertices'], |
---|
82 | mesh_dict['triangles'], |
---|
83 | points_dict['points'], |
---|
84 | expand_search=True, |
---|
85 | verbose = False, |
---|
86 | max_points_per_cell = max_points_per_cell) |
---|
87 | if is_fit is True: |
---|
88 | print "Fit in least squares" |
---|
89 | calc = interp.fit_points(points_dict['point_attributes']) |
---|
90 | |
---|
91 | else: |
---|
92 | # run an interploate problem. |
---|
93 | print "Interpolate!" |
---|
94 | calc = interp.interpolate(mesh_dict['vertex_attributes']) |
---|
95 | else: |
---|
96 | # need to change to fit_interpolate code |
---|
97 | interp = Fit(mesh_dict['vertices'], |
---|
98 | mesh_dict['triangles'], |
---|
99 | max_vertices_per_cell = max_points_per_cell) |
---|
100 | if is_fit is True: |
---|
101 | print "Fit in Fit" |
---|
102 | calc = interp.fit(points_dict['points'], |
---|
103 | points_dict['point_attributes']) |
---|
104 | pass |
---|
105 | else: |
---|
106 | # run an interploate problem. |
---|
107 | print "Interpolate!" |
---|
108 | |
---|
109 | interp = Interpolate(mesh_dict['vertices'], |
---|
110 | mesh_dict['triangles'], |
---|
111 | max_vertices_per_cell = max_points_per_cell) |
---|
112 | calc = interp.interpolate(mesh_dict['vertex_attributes'] |
---|
113 | ,points_dict['points'] |
---|
114 | ,start_blocking_len = 500000) |
---|
115 | |
---|
116 | time_taken_sec = (time.time()-t0) |
---|
117 | m1 = mem_usage() |
---|
118 | if m0 is None or m1 is None: |
---|
119 | memory_used = None |
---|
120 | else: |
---|
121 | memory_used = (m1 - m0) |
---|
122 | #print 'That took %.2f seconds' %time_taken_sec |
---|
123 | return time_taken_sec, memory_used, len(mesh_dict['triangles']) |
---|
124 | |
---|
125 | def _build_regular_mesh_dict(self, |
---|
126 | maxArea=1000, |
---|
127 | save=False): |
---|
128 | # make a mesh |
---|
129 | # pretty regular size, with some segments thrown in. |
---|
130 | m = Mesh() |
---|
131 | m.addUserVertex(0,0) |
---|
132 | m.addUserVertex(1.0,0) |
---|
133 | m.addUserVertex(0,1.0) |
---|
134 | m.addUserVertex(1.0,1.0) |
---|
135 | |
---|
136 | m.autoSegment(alpha = 100 ) |
---|
137 | |
---|
138 | dict = {} |
---|
139 | dict['points'] = [[.10,.10],[.90,.20]] |
---|
140 | dict['segments'] = [[0,1]] |
---|
141 | dict['segment_tags'] = ['wall1'] |
---|
142 | m.addVertsSegs(dict) |
---|
143 | |
---|
144 | dict = {} |
---|
145 | dict['points'] = [[.10,.90],[.40,.20]] |
---|
146 | dict['segments'] = [[0,1]] |
---|
147 | dict['segment_tags'] = ['wall2'] |
---|
148 | m.addVertsSegs(dict) |
---|
149 | |
---|
150 | dict = {} |
---|
151 | dict['points'] = [[.20,.90],[.60,.60]] |
---|
152 | dict['segments'] = [[0,1]] |
---|
153 | dict['segment_tags'] = ['wall3'] |
---|
154 | m.addVertsSegs(dict) |
---|
155 | |
---|
156 | dict = {} |
---|
157 | dict['points'] = [[.60,.20],[.90,.90]] |
---|
158 | dict['segments'] = [[0,1]] |
---|
159 | dict['segment_tags'] = ['wall4'] |
---|
160 | m.addVertsSegs(dict) |
---|
161 | |
---|
162 | m.generateMesh(mode = "Q", maxArea = maxArea) |
---|
163 | if save is True: |
---|
164 | m.export_mesh_file("aaaa.tsh") |
---|
165 | mesh_dict = m.Mesh2IOTriangulationDict() |
---|
166 | |
---|
167 | #Add vert attribute info to the mesh |
---|
168 | mesh_dict['vertex_attributes'] = [] |
---|
169 | # There has to be a better way of doing this.. |
---|
170 | for vertex in mesh_dict['vertices']: |
---|
171 | mesh_dict['vertex_attributes'].append([10.0]) |
---|
172 | |
---|
173 | return mesh_dict |
---|
174 | |
---|
175 | def _build_points_dict(self, num_of_points=20000): |
---|
176 | |
---|
177 | points_dict = {} |
---|
178 | points = [] |
---|
179 | point_atts = [] |
---|
180 | |
---|
181 | for point in range(num_of_points): |
---|
182 | points.append([random(), random()]) |
---|
183 | point_atts.append(10.0) |
---|
184 | |
---|
185 | points_dict['points'] = points |
---|
186 | points_dict['point_attributes'] = point_atts |
---|
187 | return points_dict |
---|
188 | |
---|
189 | |
---|
190 | #------------------------------------------------------------- |
---|
191 | if __name__ == "__main__": |
---|
192 | b = BenchmarkLeastSquares() |
---|
193 | b._build_regular_mesh_dict() |
---|
194 | b._build_points_dict() |
---|