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