Changeset 2666


Ignore:
Timestamp:
Apr 6, 2006, 11:07:20 AM (18 years ago)
Author:
ole
Message:

Introduced acceptable_overhoot limit in least_squares and tested it

Location:
inundation/pyvolution
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/pyvolution/least_squares.py

    r2665 r2666  
    3838
    3939
    40 
    41 acceptable_fit_overshoot = 1.001 #Must be larger than 1
    4240
    4341DEFAULT_ALPHA = 0.001
     
    159157                alpha = DEFAULT_ALPHA,
    160158                verbose = False,
     159                acceptable_overshoot = 1,
    161160                expand_search = False,
    162161                data_origin = None,
     
    181180
    182181          alpha: Smoothing parameter.
     182
     183          acceptable overshoot: controls the allowed factor by which fitted values
     184          may exceed the value of input data. The lower limit is defined
     185          as min(z) - acceptable_overshoot*delta z and upper limit
     186          as max(z) + acceptable_overshoot*delta z
     187         
    183188
    184189          point_attributes: Vector or array of data at the point_coordinates.
     
    252257        zeta = Zeta[:,i]
    253258        z = Z[:,i]               
    254         zc = Zc[:,i]       
     259        zc = Zc[:,i]
     260
     261        max_zc = max(zc)
     262        min_zc = min(zc)
     263        delta_zc = max_zc-min_zc
     264        upper_limit = max_zc + delta_zc*acceptable_overshoot
     265        lower_limit = min_zc - delta_zc*acceptable_overshoot       
    255266       
    256         if max(zeta) > max(zc)*acceptable_fit_overshoot or\
    257            min(zeta) < min(zc)/acceptable_fit_overshoot:
    258             msg = 'Least sqares produced values outside the range of the input '
    259             msg += 'data by a factor greater than %.2f. ' %acceptable_fit_overshoot
    260             msg += 'z in [%f, %f], zeta in [%f, %f].\n' %(min(zc), max(zc),
     267
     268        if max(zeta) > upper_limit or min(zeta) < lower_limit:
     269            msg = 'Least sqares produced values outside the allowed '
     270            msg += 'range [%f, %f].\n' %(lower_limit, upper_limit)
     271            msg += 'z in [%f, %f], zeta in [%f, %f].\n' %(min_zc, max_zc,
    261272                                                          min(zeta), max(zeta))
    262 
    263             offending_vertices = (zeta > max(zc)*acceptable_fit_overshoot) or\
    264                                  (zeta < min(zc)/acceptable_fit_overshoot)
    265 
     273            msg += 'If greater range is needed, increase the value of '
     274            msg += 'acceptable_fit_overshoot (currently %f).\n' %(acceptable_overshoot)
     275
     276
     277            offending_vertices = (zeta > upper_limit or zeta < lower_limit)
    266278            Xi_c = compress(offending_vertices, Xi)
    267279            Eta_c = compress(offending_vertices, Eta)
  • inundation/pyvolution/test_least_squares.py

    r2665 r2666  
    18561856        #Fit surface to mesh
    18571857        z = linear_function(data_points1) #Example z-values
    1858         v = fit_to_mesh(points, triangles, data_points1, z, alpha=0.0,
    1859                         precrop=True, verbose=False)
     1858        v = fit_to_mesh(points, triangles, data_points1, z, alpha=0.0,
     1859                        precrop=True, verbose=False)
     1860
    18601861        assert allclose(linear_function(points), v)
     1862
     1863
     1864       
     1865    def test_acceptable_overshoot(self):
     1866        """Fit a surface to one set of points. Then interpolate that surface
     1867        using another set of points.
     1868        Check that exceedance in fitted values are caught.
     1869        """
     1870        from mesh import Mesh
     1871
     1872
     1873        #Setup mesh used to represent fitted function
     1874        a = [0.0, 0.0]
     1875        b = [0.0, 2.0]
     1876        c = [2.0, 0.0]
     1877        d = [0.0, 4.0]
     1878        e = [2.0, 2.0]
     1879        f = [4.0, 0.0]
     1880
     1881        points = [a, b, c, d, e, f]
     1882        #bac, bce, ecf, dbe, daf, dae
     1883        triangles = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
     1884
     1885        #Datapoints to fit from
     1886        data_points1 = [[ 0.66666667, 0.66666667],
     1887                        [ 1.33333333, 1.33333333],
     1888                        [ 2.66666667, 0.66666667],
     1889                        [ 0.66666667, 2.66666667],
     1890                        [ 0.0, 1.0],
     1891                        [ 0.0, 3.0],
     1892                        [ 1.0, 0.0],
     1893                        [ 1.0, 1.0],
     1894                        [ 15, -17],   #Outside mesh
     1895                        [ 1.0, 2.0],
     1896                        [ 1.0, 3.0],
     1897                        [ 2.0, 1.0],
     1898                        [ 3.0, 0.0],
     1899                        [ 3.0, 1.0]]
     1900
     1901        #Fit surface to mesh
     1902        z = linear_function(data_points1) #Example z-values
     1903
     1904        try:
     1905            v = fit_to_mesh(points, triangles, data_points1, z, alpha=0.0,
     1906                            acceptable_overshoot = 0.2,
     1907                            precrop=True, verbose=False)
     1908        except FittingError, e:
     1909            pass
     1910        else:
     1911            raise 'Should have raised exception'
     1912           
     1913
     1914        #assert allclose(linear_function(points), v)
     1915       
    18611916
    18621917       
     
    18641919if __name__ == "__main__":
    18651920    #suite = unittest.makeSuite(Test_Least_Squares,'test_smooth_attributes_to_mesh_function')
    1866     suite = unittest.makeSuite(Test_Least_Squares,'test_fit_using_fit_to_mesh')
     1921    suite = unittest.makeSuite(Test_Least_Squares,'test')
    18671922
    18681923    #suite = unittest.makeSuite(Test_Least_Squares,'test_fit_to_msh_netcdf_fileII')
Note: See TracChangeset for help on using the changeset viewer.