source: inundation/fit_interpolate/benchmark_least_squares.py @ 2378

Last change on this file since 2378 was 2184, checked in by duncan, 19 years ago

writing results to file

File size: 4.1 KB
RevLine 
[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]18import os
19import sys
[1943]20import time
[2004]21from random import seed, random
[1935]22
[1943]23from pyvolution.least_squares import Interpolation
[2004]24from pmesh.mesh import Mesh
[1935]25
[1974]26def mem_usage():
[1972]27    '''
28    returns the rss.
29
[1974]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           
[1972]34    Only works on nix systems.
35    '''
[1969]36    import string
[1974]37    p=os.popen('ps uwp %s'%os.getpid()) 
[1969]38    lines=p.readlines()
[1972]39    #print "lines", lines
[1969]40    status=p.close() 
[1974]41    if status or len(lines)!=2 or sys.platform == 'win32': 
[1969]42        return None 
43    return int(string.split(lines[1])[4]) 
[1935]44
[1969]45
46
47
[2004]48class BenchmarkLeastSquares:
[1935]49
50
[2004]51    def __init__(self):
[1935]52        pass
53
[2004]54    def trial(self,
55              num_of_points=20000,
56              maxArea=1000,
57              max_points_per_cell=4,
[2184]58              is_fit=True,
[2004]59              save=False):
[2184]60        '''
61        num_of_points
62        '''
63       
[2004]64        #print "num_of_points",num_of_points
65        #print "maxArea",maxArea
66        #print "max_points_per_cell", max_points_per_cell
[1935]67
[2004]68        # make a mesh
69        # pretty regular size, with some segments thrown in.
[1969]70        m = Mesh()
71        m.addUserVertex(0,0)
[2184]72        m.addUserVertex(1.0,0)
73        m.addUserVertex(0,1.0)
74        m.addUserVertex(1.0,1.0)
[1969]75       
76        m.autoSegment(alpha = 100 )
77       
78        dict = {}
[2184]79        dict['points'] = [[.10,.10],[.90,.20]]
[1969]80        dict['segments'] = [[0,1]] 
81        dict['segment_tags'] = ['wall1']   
82        m.addVertsSegs(dict)
83   
84        dict = {}
[2184]85        dict['points'] = [[.10,.90],[.40,.20]]
[1969]86        dict['segments'] = [[0,1]] 
87        dict['segment_tags'] = ['wall2']   
88        m.addVertsSegs(dict)
89       
90        dict = {}
[2184]91        dict['points'] = [[.20,.90],[.60,.60]]
[1969]92        dict['segments'] = [[0,1]] 
93        dict['segment_tags'] = ['wall3'] 
94        m.addVertsSegs(dict)
95       
96        dict = {}
[2184]97        dict['points'] = [[.60,.20],[.90,.90]]
[1969]98        dict['segments'] = [[0,1]] 
99        dict['segment_tags'] = ['wall4']   
100        m.addVertsSegs(dict)
[2004]101
102        m.generateMesh(mode = "Q", maxArea = maxArea)       
103        if save is True:
104            m.export_mesh_file("aaaa.tsh")
[1969]105        mesh_dict =  m.Mesh2IOTriangulationDict()
[2184]106        #print "mesh_dict",mesh_dict
[1969]107        points = []
108        point_atts = []
[2184]109        vertex_atts = []
110
111             
112           
[1969]113        for point in range(num_of_points):
114            points.append([random()*100, random()*100])
115            point_atts.append(10.0)
[2184]116
117        # There has to be a better way of doing this..
118        for vertex in mesh_dict['vertices']:
119            vertex_atts.append(10.0)
[2004]120           
121        #Initial time and memory
122        t0 = time.time()
123        #m0 = None on windows
124        m0 = mem_usage()
[1969]125
126        interp = Interpolation(mesh_dict['vertices'],
127                               mesh_dict['triangles'], points,
128                               alpha=0.2, expand_search=True,
[2004]129                               verbose = False,
130                               max_points_per_cell = 4)
[2184]131        if is_fit is True:
132            calc = interp.fit_points(point_atts)
133
134        else:
135            # run an interploate problem.
136            print "Interpolate!"
137            calc = interp.interpolate(vertex_atts)
138           
[2004]139        time_taken_sec = (time.time()-t0)
140        m1 = mem_usage()
141        if m0 is None or m1 is None:
142            memory_used = None
143        else:
144            memory_used = (m1 - m0)
145        #print 'That took %.2f seconds' %time_taken_sec
[2184]146        return time_taken_sec, memory_used, len(mesh_dict['triangles'])
Note: See TracBrowser for help on using the repository browser.