[1935] | 1 | """Least squares smooting and interpolation. |
---|
| 2 | |
---|
| 3 | measure the speed of least squares. |
---|
[1969] | 4 | |
---|
| 5 | ________________________ |
---|
| 6 | General comments |
---|
[1935] | 7 | |
---|
[1969] | 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 | |
---|
[2184] | 13 | Duncan Gray |
---|
[1935] | 14 | Geoscience Australia, 2004. |
---|
| 15 | """ |
---|
| 16 | |
---|
| 17 | |
---|
[2004] | 18 | import os |
---|
| 19 | import sys |
---|
[1943] | 20 | import time |
---|
[2004] | 21 | from random import seed, random |
---|
[1935] | 22 | |
---|
[1943] | 23 | from pyvolution.least_squares import Interpolation |
---|
[2563] | 24 | from fit_interpolate.interpolate import Interpolate |
---|
[2926] | 25 | from fit_interpolate.fit import Fit |
---|
[2004] | 26 | from pmesh.mesh import Mesh |
---|
[1935] | 27 | |
---|
[1974] | 28 | def mem_usage(): |
---|
[1972] | 29 | ''' |
---|
| 30 | returns the rss. |
---|
| 31 | |
---|
[1974] | 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 | |
---|
[1972] | 36 | Only works on nix systems. |
---|
| 37 | ''' |
---|
[1969] | 38 | import string |
---|
[1974] | 39 | p=os.popen('ps uwp %s'%os.getpid()) |
---|
[1969] | 40 | lines=p.readlines() |
---|
[1972] | 41 | #print "lines", lines |
---|
[1969] | 42 | status=p.close() |
---|
[1974] | 43 | if status or len(lines)!=2 or sys.platform == 'win32': |
---|
[1969] | 44 | return None |
---|
| 45 | return int(string.split(lines[1])[4]) |
---|
[1935] | 46 | |
---|
[1969] | 47 | |
---|
| 48 | |
---|
| 49 | |
---|
[2004] | 50 | class BenchmarkLeastSquares: |
---|
[1935] | 51 | |
---|
| 52 | |
---|
[2004] | 53 | def __init__(self): |
---|
[1935] | 54 | pass |
---|
| 55 | |
---|
[2004] | 56 | def trial(self, |
---|
| 57 | num_of_points=20000, |
---|
| 58 | maxArea=1000, |
---|
| 59 | max_points_per_cell=4, |
---|
[2184] | 60 | is_fit=True, |
---|
[2563] | 61 | use_least_squares=True, |
---|
[2004] | 62 | save=False): |
---|
[2184] | 63 | ''' |
---|
| 64 | num_of_points |
---|
| 65 | ''' |
---|
| 66 | |
---|
[2004] | 67 | #print "num_of_points",num_of_points |
---|
| 68 | #print "maxArea",maxArea |
---|
| 69 | #print "max_points_per_cell", max_points_per_cell |
---|
[1935] | 70 | |
---|
[2563] | 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: |
---|
[2926] | 88 | print "Fit in least squares" |
---|
[2563] | 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 |
---|
[2926] | 97 | interp = Fit(mesh_dict['vertices'], |
---|
[2563] | 98 | mesh_dict['triangles'], |
---|
| 99 | max_vertices_per_cell = max_points_per_cell) |
---|
| 100 | if is_fit is True: |
---|
[2926] | 101 | print "Fit in Fit" |
---|
| 102 | calc = interp.fit(points_dict['points'], |
---|
| 103 | points_dict['point_attributes']) |
---|
[2563] | 104 | pass |
---|
| 105 | else: |
---|
| 106 | # run an interploate problem. |
---|
| 107 | print "Interpolate!" |
---|
[2926] | 108 | |
---|
| 109 | interp = Interpolate(mesh_dict['vertices'], |
---|
| 110 | mesh_dict['triangles'], |
---|
| 111 | max_vertices_per_cell = max_points_per_cell) |
---|
[2563] | 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 |
---|
[2004] | 129 | # pretty regular size, with some segments thrown in. |
---|
[1969] | 130 | m = Mesh() |
---|
| 131 | m.addUserVertex(0,0) |
---|
[2184] | 132 | m.addUserVertex(1.0,0) |
---|
| 133 | m.addUserVertex(0,1.0) |
---|
| 134 | m.addUserVertex(1.0,1.0) |
---|
[1969] | 135 | |
---|
| 136 | m.autoSegment(alpha = 100 ) |
---|
| 137 | |
---|
| 138 | dict = {} |
---|
[2184] | 139 | dict['points'] = [[.10,.10],[.90,.20]] |
---|
[1969] | 140 | dict['segments'] = [[0,1]] |
---|
| 141 | dict['segment_tags'] = ['wall1'] |
---|
| 142 | m.addVertsSegs(dict) |
---|
| 143 | |
---|
| 144 | dict = {} |
---|
[2184] | 145 | dict['points'] = [[.10,.90],[.40,.20]] |
---|
[1969] | 146 | dict['segments'] = [[0,1]] |
---|
| 147 | dict['segment_tags'] = ['wall2'] |
---|
| 148 | m.addVertsSegs(dict) |
---|
| 149 | |
---|
| 150 | dict = {} |
---|
[2184] | 151 | dict['points'] = [[.20,.90],[.60,.60]] |
---|
[1969] | 152 | dict['segments'] = [[0,1]] |
---|
| 153 | dict['segment_tags'] = ['wall3'] |
---|
| 154 | m.addVertsSegs(dict) |
---|
| 155 | |
---|
| 156 | dict = {} |
---|
[2184] | 157 | dict['points'] = [[.60,.20],[.90,.90]] |
---|
[1969] | 158 | dict['segments'] = [[0,1]] |
---|
| 159 | dict['segment_tags'] = ['wall4'] |
---|
| 160 | m.addVertsSegs(dict) |
---|
[2004] | 161 | |
---|
| 162 | m.generateMesh(mode = "Q", maxArea = maxArea) |
---|
| 163 | if save is True: |
---|
| 164 | m.export_mesh_file("aaaa.tsh") |
---|
[1969] | 165 | mesh_dict = m.Mesh2IOTriangulationDict() |
---|
[2563] | 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 = {} |
---|
[1969] | 178 | points = [] |
---|
| 179 | point_atts = [] |
---|
[2184] | 180 | |
---|
[1969] | 181 | for point in range(num_of_points): |
---|
[2573] | 182 | points.append([random(), random()]) |
---|
[1969] | 183 | point_atts.append(10.0) |
---|
[2184] | 184 | |
---|
[2563] | 185 | points_dict['points'] = points |
---|
| 186 | points_dict['point_attributes'] = point_atts |
---|
| 187 | return points_dict |
---|
[1969] | 188 | |
---|
[2184] | 189 | |
---|
[2563] | 190 | #------------------------------------------------------------- |
---|
| 191 | if __name__ == "__main__": |
---|
| 192 | b = BenchmarkLeastSquares() |
---|
| 193 | b._build_regular_mesh_dict() |
---|
| 194 | b._build_points_dict() |
---|