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 | Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou |
---|
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 pmesh.mesh import Mesh |
---|
25 | |
---|
26 | def mem_usage(): |
---|
27 | ''' |
---|
28 | returns the rss. |
---|
29 | |
---|
30 | RSS The total amount of physical memory used by the task, in kilo- |
---|
31 | bytes, is shown here. For ELF processes used library pages are |
---|
32 | counted here, for a.out processes not. |
---|
33 | |
---|
34 | Only works on nix systems. |
---|
35 | ''' |
---|
36 | import string |
---|
37 | p=os.popen('ps uwp %s'%os.getpid()) |
---|
38 | lines=p.readlines() |
---|
39 | #print "lines", lines |
---|
40 | status=p.close() |
---|
41 | if status or len(lines)!=2 or sys.platform == 'win32': |
---|
42 | return None |
---|
43 | return int(string.split(lines[1])[4]) |
---|
44 | |
---|
45 | |
---|
46 | |
---|
47 | |
---|
48 | class BenchmarkLeastSquares: |
---|
49 | |
---|
50 | |
---|
51 | def __init__(self): |
---|
52 | pass |
---|
53 | |
---|
54 | def trial(self, |
---|
55 | num_of_points=20000, |
---|
56 | maxArea=1000, |
---|
57 | max_points_per_cell=4, |
---|
58 | save=False): |
---|
59 | #print "num_of_points",num_of_points |
---|
60 | #print "maxArea",maxArea |
---|
61 | #print "max_points_per_cell", max_points_per_cell |
---|
62 | |
---|
63 | # make a mesh |
---|
64 | # pretty regular size, with some segments thrown in. |
---|
65 | m = Mesh() |
---|
66 | m.addUserVertex(0,0) |
---|
67 | m.addUserVertex(100,0) |
---|
68 | m.addUserVertex(0,100) |
---|
69 | m.addUserVertex(100,100) |
---|
70 | |
---|
71 | m.autoSegment(alpha = 100 ) |
---|
72 | |
---|
73 | dict = {} |
---|
74 | dict['points'] = [[10,10],[90,20]] |
---|
75 | dict['segments'] = [[0,1]] |
---|
76 | dict['segment_tags'] = ['wall1'] |
---|
77 | m.addVertsSegs(dict) |
---|
78 | |
---|
79 | dict = {} |
---|
80 | dict['points'] = [[10,90],[40,20]] |
---|
81 | dict['segments'] = [[0,1]] |
---|
82 | dict['segment_tags'] = ['wall2'] |
---|
83 | m.addVertsSegs(dict) |
---|
84 | |
---|
85 | dict = {} |
---|
86 | dict['points'] = [[20,90],[60,60]] |
---|
87 | dict['segments'] = [[0,1]] |
---|
88 | dict['segment_tags'] = ['wall3'] |
---|
89 | m.addVertsSegs(dict) |
---|
90 | |
---|
91 | dict = {} |
---|
92 | dict['points'] = [[60,20],[90,90]] |
---|
93 | dict['segments'] = [[0,1]] |
---|
94 | dict['segment_tags'] = ['wall4'] |
---|
95 | m.addVertsSegs(dict) |
---|
96 | |
---|
97 | m.generateMesh(mode = "Q", maxArea = maxArea) |
---|
98 | if save is True: |
---|
99 | m.export_mesh_file("aaaa.tsh") |
---|
100 | mesh_dict = m.Mesh2IOTriangulationDict() |
---|
101 | |
---|
102 | points = [] |
---|
103 | point_atts = [] |
---|
104 | for point in range(num_of_points): |
---|
105 | points.append([random()*100, random()*100]) |
---|
106 | point_atts.append(10.0) |
---|
107 | |
---|
108 | #Initial time and memory |
---|
109 | t0 = time.time() |
---|
110 | #m0 = None on windows |
---|
111 | m0 = mem_usage() |
---|
112 | |
---|
113 | interp = Interpolation(mesh_dict['vertices'], |
---|
114 | mesh_dict['triangles'], points, |
---|
115 | alpha=0.2, expand_search=True, |
---|
116 | verbose = False, |
---|
117 | max_points_per_cell = 4) |
---|
118 | calc = interp.fit_points(point_atts ) |
---|
119 | #print "interp.expanded_quad_searches", interp.expanded_quad_searches |
---|
120 | #print "calc", calc |
---|
121 | time_taken_sec = (time.time()-t0) |
---|
122 | m1 = mem_usage() |
---|
123 | if m0 is None or m1 is None: |
---|
124 | memory_used = None |
---|
125 | else: |
---|
126 | memory_used = (m1 - m0) |
---|
127 | #print 'That took %.2f seconds' %time_taken_sec |
---|
128 | return time_taken_sec, memory_used |
---|