Changeset 6442


Ignore:
Timestamp:
Mar 4, 2009, 8:37:22 AM (16 years ago)
Author:
rwilson
Message:

Added tests for get_relative().

Location:
branches/numpy/anuga/coordinate_transforms
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/numpy/anuga/coordinate_transforms/geo_reference.py

    r6441 r6442  
    318318                msg = 'Single point must have two elements'
    319319                raise ShapeError, msg
    320                 #points = reshape(points, (1,2))
    321320
    322321        if not points.shape[1] == 2:
     
    348347        """
    349348
    350         is_list = False
    351         if type(points) == types.ListType:
    352             is_list = True
    353 
    354         points = ensure_numeric(points, num.float)
     349        # remember if we got a list
     350        is_list = isinstance(points, list)
     351
     352        # convert to a numeric array, force a copy
     353        points = ensure_numeric(copy.copy(points), num.float)
     354
     355        # sanity checks
    355356        if len(points.shape) == 1:
    356357            #One point has been passed
     
    358359                msg = 'Single point must have two elements'
    359360                raise ShapeError, msg
    360                 #points = reshape(points, (1,2))
    361361
    362362        if not points.shape[1] == 2:
  • branches/numpy/anuga/coordinate_transforms/test_geo_reference.py

    r6441 r6442  
    292292        msg = 'points=\n%s\nnew_points=\n%s' % (str(points), str(new_points))
    293293        for point, new_point in map(None, points, new_points):
    294             self.failUnless(point[0]+x == new_point[0], 'failed')
    295             self.failUnless(point[1]+y == new_point[1], 'failed')
     294            self.failUnless(point[0]+x == new_point[0], msg)
     295            self.failUnless(point[1]+y == new_point[1], msg)
    296296
    297297        # test with no supplied offsets
     
    336336        self.failUnless(type(new_points) == type(points), 'failed')
    337337        msg = ('Third call of .get_absolute() returned %s\nexpected %s'
     338               % (str(new_points), str(expected_new_points)))
     339        self.failUnless(num.alltrue(expected_new_points == new_points), msg)
     340
     341    def test_get_relative_list(self):
     342        # test with supplied offsets
     343        x = 7.0
     344        y = 3.0
     345       
     346        g = Geo_reference(56, x, y)
     347        points = [[3.0,34.0], [64.0,6.0]]
     348        new_points = g.get_relative(points)
     349
     350        self.failUnless(isinstance(new_points, list), 'failed')
     351        self.failUnless(type(new_points) == type(points), 'failed')
     352        for point, new_point in map(None, points, new_points):
     353            self.failUnless(point[0]-x == new_point[0], 'failed')
     354            self.failUnless(point[1]-y == new_point[1], 'failed')
     355
     356        # test with no supplied offsets
     357        g = Geo_reference()
     358        points = [[3.0,34.0], [64.0,6.0]]
     359        new_points = g.get_relative(points)
     360
     361        self.failUnless(isinstance(new_points, list), 'failed')
     362        self.failUnless(type(new_points) == type(points), 'failed')
     363        for point, new_point in map(None, points, new_points):
     364            self.failUnless(point[0] == new_point[0], 'failed')
     365            self.failUnless(point[1] == new_point[1], 'failed')
     366           
     367        # test that calling get_absolute twice does the right thing
     368        # first call
     369        dx = 10.0
     370        dy = 12.0
     371        g = Geo_reference(56, dx, dy)
     372        points = [[3.0,34.0], [64.0,6.0]]
     373        expected_new_points = [[3.0-dx,34.0-dy], [64.0-dx,6.0-dy]]
     374        new_points = g.get_relative(points)
     375
     376        self.failUnless(isinstance(new_points, list), 'failed')
     377        self.failUnless(type(new_points) == type(points), 'failed')
     378        self.failUnless(new_points == expected_new_points, 'failed')
     379
     380        # and repeat from 'new_points = g.get_absolute(points)' above
     381        # to see if second call with same input gives same results.
     382        new_points = g.get_relative(points)
     383
     384        self.failUnless(isinstance(new_points, list), 'failed')
     385        self.failUnless(type(new_points) == type(points), 'failed')
     386        self.failUnless(new_points == expected_new_points, 'failed')
     387
     388    def test_get_relative_array(self):
     389        '''Same test as test_get_relative_list(), but with numeric arrays.'''
     390
     391        # test with supplied offsets
     392        x = 7.0
     393        y = 3.0
     394       
     395        g = Geo_reference(56, x, y)
     396        points = num.array([[3.0,34.0], [64.0,6.0]])
     397        new_points = g.get_relative(points)
     398
     399        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
     400        self.failUnless(type(new_points) == type(points), 'failed')
     401        msg = 'points=\n%s\nnew_points=\n%s' % (str(points), str(new_points))
     402        for point, new_point in map(None, points, new_points):
     403            self.failUnless(point[0]-x == new_point[0], msg)
     404            self.failUnless(point[1]-y == new_point[1], msg)
     405
     406        # test with no supplied offsets
     407        g = Geo_reference()
     408        points = num.array([[3.0,34.0], [64.0,6.0]])
     409        new_points = g.get_relative(points)
     410
     411        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
     412        self.failUnless(type(new_points) == type(points), 'failed')
     413        self.failUnless(num.alltrue(points == new_points), 'failed')
     414
     415        # test that calling get_relative twice does the right thing
     416        # first call
     417        dx = 11.0
     418        dy = 13.0
     419        g = Geo_reference(56, dx, dy)
     420        points = num.array([[3.0,34.0], [64.0,6.0]])
     421        expected_new_points = num.array([[3.0-dx,34.0-dy], [64.0-dx,6.0-dy]])
     422        new_points = g.get_relative(points)
     423
     424        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
     425        self.failUnless(type(new_points) == type(points), 'failed')
     426        msg = ('First call of .get_relative() returned %s\nexpected %s'
     427               % (str(new_points), str(expected_new_points)))
     428        self.failUnless(num.alltrue(expected_new_points == new_points), msg)
     429
     430        # and repeat from 'new_points = g.get_relative(points)' above
     431        # to see if second call with same input gives same results.
     432        new_points = g.get_relative(points)
     433
     434        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
     435        self.failUnless(type(new_points) == type(points), 'failed')
     436        msg = ('Second call of .get_relative() returned\n%s\nexpected\n%s'
     437               % (str(new_points), str(expected_new_points)))
     438        self.failUnless(num.alltrue(expected_new_points == new_points), msg)
     439
     440        # and repeat again to see if *third* call with same input
     441        # gives same results.
     442        new_points = g.get_relative(points)
     443
     444        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
     445        self.failUnless(type(new_points) == type(points), 'failed')
     446        msg = ('Third call of .get_relative() returned %s\nexpected %s'
    338447               % (str(new_points), str(expected_new_points)))
    339448        self.failUnless(num.alltrue(expected_new_points == new_points), msg)
Note: See TracChangeset for help on using the changeset viewer.