Changeset 2781


Ignore:
Timestamp:
Apr 28, 2006, 4:54:26 PM (19 years ago)
Author:
duncan
Message:

investigating ticket #8

Location:
inundation/fit_interpolate
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/fit_interpolate/spike_least_squares.py

    r2753 r2781  
    384384                 vertex_coordinates,
    385385                 triangles,
     386                 z,
    386387                 point_coordinates = None,
    387388                 alpha = None,
     
    419420          point coordinates and vertex coordinates are assumed to be
    420421          relative to their respective origins.
     422         
     423          z: Single 1d vector or array of data at the point_coordinates.
    421424
    422425        """
     
    461464            if verbose: print 'Building interpolation matrix'
    462465            self.build_interpolation_matrix_A(point_coordinates,
     466                                              z,
    463467                                              verbose = verbose,
    464468                                              expand_search = expand_search,
     
    489493            if verbose: print 'Building interpolation matrix'
    490494            self.build_interpolation_matrix_A(point_coordinates,
     495                                              z,
    491496                                              verbose = verbose,
    492497                                              data_origin = data_origin,
     
    516521
    517522    def build_interpolation_matrix_A(self, point_coordinates,
     523                                     z,
    518524                                     verbose = False, expand_search = True,
    519525                                     max_points_per_cell=30,
     
    537543        from utilities.polygon import inside_polygon
    538544       
     545        #Convert input to Numeric arrays
     546        z = ensure_numeric(z, Float)
     547           
    539548        #Convert input to Numeric arrays just in case.
    540549        point_coordinates = ensure_numeric(point_coordinates, Float)
     
    543552        m = self.mesh.coordinates.shape[0] #Nbr of basis functions (1/vertex)
    544553        n = point_coordinates.shape[0]     #Nbr of data points
    545 
     554        if len(z.shape) > 1:
     555            att_num = z.shape[1]
     556        else:
     557            att_num = 0
     558           
     559        assert z.shape[0] == point_coordinates.shape[0]
     560        if verbose: print 'Number of attributes: %d' %att_num
    546561        if verbose: print 'Number of datapoints: %d' %n
    547562        if verbose: print 'Number of basis functions: %d' %m
     
    549564        self.A = Sparse(n,m)
    550565        self.AtA = Sparse(m,m)
     566        self.Atz = zeros((m,att_num), Float)
    551567
    552568        #Build quad tree of vertices
     
    603619                for j in js:
    604620                    self.A[i,j] = sigmas[j]
     621                    #self.Atz[n-i-1] +=  sigmas[j]*z[n-j-1]
     622                    #print "i",i
     623                    #print "j",j
     624                    #print "self.Atz",self.Atz
     625                    #print "z",z
     626                    #a=self.Atz[j]
     627                    #b=z[i]
     628                    self.Atz[j] +=  sigmas[j]*z[i]
     629                    #print "m-i-1",m-i-1
     630                    #print "n-j-1",n-j-1
     631                    #print "self.A[i,j]",self.A[i,j]
     632                    #print "z[n-j-1]",z[n-j-1]
     633                    #print "z[i]",z[i]
     634                    #print "",
     635                    #print "self.Atz",self.Atz                   
    605636                    for k in js:
    606637                        if interp_only == False:
     
    688719
    689720        This is the brute force which is too slow for large problems,
    690         but could be used for testing
     721but could be used for testing
    691722        """
    692723
     
    873904        #FIXME (DSG-DsG): could Sparse_CSR be used here?  Use this format
    874905        # after a matrix is built, before calcs.
     906        Atz = self.Atz
     907        #print "self.get_A()", self.get_A()
     908        #print "self.Atz",self.Atz
     909        #print 'z', z
    875910        Atz = self.A.trans_mult(z)
     911        #print "self.A.trans_mult(z)",self.A.trans_mult(z)
    876912
    877913
  • inundation/fit_interpolate/spike_test_least_squares.py

    r2752 r2781  
    77
    88
    9 from least_squares import *
     9from spike_least_squares import *
    1010from Numeric import allclose, array, transpose
     11from Numeric import zeros, take, compress, array, Float, Int, dot, transpose, concatenate, ArrayType
     12from utilities.sparse import Sparse, Sparse_CSR
    1113
    1214from coordinate_transforms.geo_reference import Geo_reference
     
    4345        data_coords = [d1, d2, d3]
    4446
    45         interp = Interpolation(points, triangles, data_coords, alpha=0)
    4647        z = [z1, z2, z3]
    47 
    48         A = interp.get_A()
    49         print "A",A
     48        interp = Interpolation(points, triangles, z,data_coords, alpha=0)
     49        #print "interp.get_A()", interp.get_A()
     50        A = interp.A
     51        #print "A",A
     52        #print "z",z
    5053        Atz = A.trans_mult(z)
    51         print "Atz",Atz
     54        #print "Atz",Atz
    5255       
    5356        f = interp.fit(z)
    5457        answer = [0, 5., 5.]
    5558
    56         print "f\n",f
    57         print "answer\n",answer
     59        #print "f\n",f
     60        #print "answer\n",answer
    5861
    5962        assert allclose(f, answer, atol=1e-7)
    6063
    61 
     64       
     65    def test_smooth_attributes_to_mesh_one_point(self):
     66        a = [0.0, 0.0]
     67        b = [0.0, 5.0]
     68        c = [5.0, 0.0]
     69        points = [a, b, c]
     70        triangles = [ [1,0,2] ]   #bac
     71
     72        d1 = [1.0, 1.0]
     73        d2 = [1.0, 3.0]
     74        d3 = [3.0,1.0]
     75        z1 = 2
     76        z2 = 4
     77        z3 = 4
     78        data_coords = [d1]
     79
     80        z = [z1]
     81        interp = Interpolation(points, triangles, z,data_coords, alpha=0)
     82        #print "interp.get_A()", interp.get_A()
     83        A = interp.A
     84        #print "A",A
     85        #print "z",z
     86        Atz_actual = A.trans_mult(z)
     87        #Atz = interp.Atz.todense()
     88
     89       
     90        #print "Atz",Atz
     91        #print "Atz_actual",Atz_actual
     92       
     93
     94        #assert allclose(Atz_actual, Atz, atol=1e-7)
     95
     96    def ytest_chewin_the_fat(self):
     97       
     98        A = Sparse(2,2)
     99        A[0,0] = 1
     100        A[1,0] = 1
     101        A[0,1] = 0
     102        A[1,1] = 1
     103        z = [1,1]
     104        m = 2
     105        n = 2
     106        Atz = zeros((n), Float)
     107        r = A.trans_mult(z)
     108        for i in range(m):
     109            for j in range(n):
     110                Atz[i] += A[m-i-1,n-j-1]*z[i]
     111        print "A.trans_mult(z)",A.trans_mult(z)
     112        print "Atz",Atz
     113        print "A*z", A*z
     114       
     115
     116        #print "answer\n",answer
     117
     118        assert allclose(f, answer, atol=1e-7)
     119
     120
     121    def test_smooth_att_to_meshII(self):
     122
     123        a = [0.0, 0.0]
     124        b = [0.0, 5.0]
     125        c = [5.0, 0.0]
     126        points = [a, b, c]
     127        triangles = [ [1,0,2] ]   #bac
     128
     129        d1 = [1.0, 1.0]
     130        d2 = [1.0, 2.0]
     131        d3 = [3.0,1.0]
     132        data_coords = [d1, d2, d3]
     133        z = linear_function(data_coords)
     134        #print "z",z
     135
     136        interp = Interpolation(points, triangles, z,
     137                               data_coords, alpha=0.0)
     138        f = interp.fit(z)
     139        answer = linear_function(points)
     140        #print "f\n",f
     141        #print "answer\n",answer
     142
     143        assert allclose(f, answer)
     144
     145    def test_smooth_attributes_to_meshIII(self):
     146
     147        a = [-1.0, 0.0]
     148        b = [3.0, 4.0]
     149        c = [4.0,1.0]
     150        d = [-3.0, 2.0] #3
     151        e = [-1.0,-2.0]
     152        f = [1.0, -2.0] #5
     153
     154        vertices = [a, b, c, d,e,f]
     155        triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc
     156
     157        point_coords = [[-2.0, 2.0],
     158                        [-1.0, 1.0],
     159                        [0.0,2.0],
     160                        [1.0, 1.0],
     161                        [2.0, 1.0],
     162                        [0.0,0.0],
     163                        [1.0, 0.0],
     164                        [0.0, -1.0],
     165                        [-0.2,-0.5],
     166                        [-0.9, -1.5],
     167                        [0.5, -1.9],
     168                        [3.0,1.0]]
     169
     170        z = linear_function(point_coords)
     171        interp = Interpolation(vertices, triangles, z,
     172                               point_coords, alpha=0.0)
     173
     174        #print 'z',z
     175        f = interp.fit(z)
     176        answer = linear_function(vertices)
     177        #print "f\n",f
     178        #print "answer\n",answer
     179        assert allclose(f, answer)
     180
     181    def test_smooth_attributes_to_meshIV(self):
     182        """ Testing 2 attributes smoothed to the mesh
     183        """
     184
     185        a = [0.0, 0.0]
     186        b = [0.0, 5.0]
     187        c = [5.0, 0.0]
     188        points = [a, b, c]
     189        triangles = [ [1,0,2] ]   #bac
     190
     191        d1 = [1.0, 1.0]
     192        d2 = [1.0, 3.0]
     193        d3 = [3.0, 1.0]
     194        z1 = [2, 4]
     195        z2 = [4, 8]
     196        z3 = [4, 8]
     197        data_coords = [d1, d2, d3]
     198
     199        z = [z1, z2, z3]
     200        interp = Interpolation(points, triangles, z,
     201                               data_coords, alpha=0.0)
     202        f =  interp.fit_points(z)
     203        answer = [[0,0], [5., 10.], [5., 10.]]
     204        assert allclose(f, answer)
     205
     206
     207    def test_fit_and_interpolation(self):
     208        from mesh import Mesh
     209
     210        a = [0.0, 0.0]
     211        b = [0.0, 2.0]
     212        c = [2.0, 0.0]
     213        d = [0.0, 4.0]
     214        e = [2.0, 2.0]
     215        f = [4.0, 0.0]
     216
     217        points = [a, b, c, d, e, f]
     218        #bac, bce, ecf, dbe, daf, dae
     219        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
     220
     221        #Get (enough) datapoints
     222        data_points = [[ 0.66666667, 0.66666667],
     223                       [ 1.33333333, 1.33333333],
     224                       [ 2.66666667, 0.66666667],
     225                       [ 0.66666667, 2.66666667],
     226                       [ 0.0, 1.0],
     227                       [ 0.0, 3.0],
     228                       [ 1.0, 0.0],
     229                       [ 1.0, 1.0],
     230                       [ 1.0, 2.0],
     231                       [ 1.0, 3.0],
     232                       [ 2.0, 1.0],
     233                       [ 3.0, 0.0],
     234                       [ 3.0, 1.0]]
     235
     236        z = linear_function(data_points)
     237        interp = Interpolation(points, triangles, z,
     238                               data_points, alpha=0.0)
     239
     240        answer = linear_function(points)
     241
     242        f = interp.fit(z)
     243
     244        #print "f",f
     245        #print "answer",answer
     246        assert allclose(f, answer)
     247
     248       
     249    def test_smoothing_and_interpolation(self):
     250
     251        a = [0.0, 0.0]
     252        b = [0.0, 2.0]
     253        c = [2.0, 0.0]
     254        d = [0.0, 4.0]
     255        e = [2.0, 2.0]
     256        f = [4.0, 0.0]
     257
     258        points = [a, b, c, d, e, f]
     259        #bac, bce, ecf, dbe, daf, dae
     260        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
     261
     262        #Get (too few!) datapoints
     263        data_points = [[ 0.66666667, 0.66666667],
     264                       [ 1.33333333, 1.33333333],
     265                       [ 2.66666667, 0.66666667],
     266                       [ 0.66666667, 2.66666667]]
     267
     268        z = linear_function(data_points)
     269        answer = linear_function(points)
     270
     271        #Make interpolator with too few data points and no smoothing
     272        interp = Interpolation(points, triangles, z,
     273                               data_points, alpha=0.0)
     274        #Must raise an exception
     275        try:
     276            f = interp.fit(z)
     277        except:
     278            pass
     279
     280        #Now try with smoothing parameter
     281        interp = Interpolation(points, triangles, z,
     282                               data_points, alpha=1.0e-13)
     283
     284        f = interp.fit(z)
     285        #f will be different from answer due to smoothing
     286        assert allclose(f, answer,atol=5)
     287
     288        #Map back
     289        #z1 = interp.interpolate(f)
     290        #assert allclose(z, z1)
     291       
    62292#-------------------------------------------------------------
    63293if __name__ == "__main__":
    64294    suite = unittest.makeSuite(Test_Least_Squares,'test')
     295    #suite = unittest.makeSuite(Test_Least_Squares,'test_smoothing_and_interpolation')
     296    #suite = unittest.makeSuite(Test_Least_Squares,'test_smooth_attributes_to_mesh_one_point')
    65297    runner = unittest.TextTestRunner(verbosity=1)
    66298    runner.run(suite)
Note: See TracChangeset for help on using the changeset viewer.