Ignore:
Timestamp:
Nov 5, 2008, 4:19:05 PM (16 years ago)
Author:
rwilson
Message:

Converted to full numpy.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/utilities/test_polygon.py

    r5403 r5891  
    33
    44import unittest
    5 from Numeric import zeros, array, allclose
     5import numpy
    66from math import sqrt, pi
    77from anuga.utilities.numerical_tools import ensure_numeric
     
    4343        p2 = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]]
    4444
    45         f = Polygon_function( [(p1, 1.0)] )
     45        f = Polygon_function( [(p1, 1.0)], verbose=False )
    4646        z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1
    47         assert allclose(z, [1,1,0,0])
     47        assert numpy.allclose(z, [1,1,0,0])
    4848
    4949
    5050        f = Polygon_function( [(p2, 2.0)] )
    5151        z = f([5, 5, 27, 35], [5, 9, 8, -5]) # First and last inside p2
    52         assert allclose(z, [2,0,0,2])
     52        assert numpy.allclose(z, [2,0,0,2])
    5353
    5454
     
    5656        f = Polygon_function( [(p1, 1.0), (p2, 2.0)] )
    5757        z = f([5, 5, 27, 35], [5, 9, 8, -5])
    58         assert allclose(z, [2,1,0,2])
     58        assert numpy.allclose(z, [2,1,0,2])
     59
     60
     61    def test_polygon_function_constants_tuple(self):
     62        polygon = [[0,0], [10,0], [10,10], [0,10]]
     63        points = ((5, 5), (5, 9), (27, 8), (35, -5))
     64
     65        (indices, count) = separate_points_by_polygon(points, polygon, verbose=False)
    5966
    6067    def test_polygon_function_csvfile(self):
     
    7279        z = f([430000,480000], [490000,7720000]) # first outside, second inside
    7380       
    74         assert allclose(z, [0,10])
     81        assert numpy.allclose(z, [0,10])
    7582
    7683    def test_polygon_function_georef(self):
     
    9097        z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1
    9198
    92         assert allclose(z, [1,1,0,0])
     99        assert numpy.allclose(z, [1,1,0,0])
    93100
    94101
    95102        f = Polygon_function( [(p2, 2.0)], geo_reference=geo)
    96103        z = f([5, 5, 27, 35], [5, 9, 8, -5]) #First and last inside p2
    97         assert allclose(z, [2,0,0,2])
     104        assert numpy.allclose(z, [2,0,0,2])
    98105
    99106
     
    101108        f = Polygon_function( [(p1, 1.0), (p2, 2.0)], geo_reference=geo)
    102109        z = f([5, 5, 27, 35], [5, 9, 8, -5])
    103         assert allclose(z, [2,1,0,2])
     110        assert numpy.allclose(z, [2,1,0,2])
    104111
    105112
     
    107114        f = Polygon_function( [(p1, 1.0), (p2, 2.0)])
    108115        z = f([5, 5, 27, 35], [5, 9, 8, -5])
    109         assert not allclose(z, [2,1,0,2])       
     116        assert not numpy.allclose(z, [2,1,0,2])       
    110117
    111118
     
    120127        f = Polygon_function( [(p1, test_function)] )
    121128        z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1
    122         assert allclose(z, [10,14,0,0])
     129        assert numpy.allclose(z, [10,14,0,0])
    123130
    124131        # Combined
    125132        f = Polygon_function( [(p1, test_function), (p2, 2.0)] )
    126133        z = f([5, 5, 27, 35], [5, 9, 8, -5])
    127         assert allclose(z, [2,14,0,2])
     134        assert numpy.allclose(z, [2,14,0,2])
    128135
    129136
     
    131138        f = Polygon_function( [(p1, test_function), (p2, 2.0)], default = 3.14)
    132139        z = f([5, 5, 27, 35], [5, 9, 8, -5])
    133         assert allclose(z, [2,14,3.14,2])
     140        assert numpy.allclose(z, [2,14,3.14,2])
    134141
    135142
     
    138145                              default = test_function)
    139146        z = f([5, 5, 27, 35], [5, 9, 8, -5])
    140         assert allclose(z, [2,14,35,2])
     147        assert numpy.allclose(z, [2,14,35,2])
    141148
    142149
     
    210217        res = inside_polygon(points, polygon)
    211218        assert len(res) == 2
    212         assert allclose(res, [0,1])
     219        assert numpy.allclose(res, [0,1])     ## alltrue?
    213220
    214221
     
    305312        res = inside_polygon( points, polygon, verbose=False )
    306313
    307         assert allclose( res, [0,1,2] )
     314        assert numpy.allclose( res, [0,1,2] )
    308315
    309316    def test_outside_polygon(self):
     
    317324       
    318325        indices = outside_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U )
    319         assert allclose( indices, [1] )
     326        assert numpy.allclose( indices, [1] )
    320327       
    321328        # One more test of vector formulation returning indices
     
    324331        res = outside_polygon( points, polygon )
    325332
    326         assert allclose( res, [3, 4] )
     333        assert numpy.allclose( res, [3, 4] )
    327334
    328335
     
    332339        res = outside_polygon( points, polygon )
    333340
    334         assert allclose( res, [0, 4, 5] )       
     341        assert numpy.allclose( res, [0, 4, 5] )       
    335342     
    336343    def test_outside_polygon2(self):
     
    355362        #print indices, count
    356363        assert count == 0 #None inside
    357         assert allclose(indices, [3,2,1,0])
     364        assert numpy.allclose(indices, [3,2,1,0])
    358365
    359366        indices = outside_polygon(points, U, closed = True)
    360         assert allclose(indices, [0,1,2,3])
     367        assert numpy.allclose(indices, [0,1,2,3])
    361368
    362369        indices = inside_polygon(points, U, closed = True)
    363         assert allclose(indices, [])               
     370        assert numpy.allclose(indices, [])               
    364371
    365372
     
    375382        indices, count = separate_points_by_polygon(points, U)
    376383        assert count == 3 #All inside
    377         assert allclose(indices, [0,1,2])
     384        assert numpy.allclose(indices, [0,1,2])
    378385
    379386        indices = outside_polygon(points, U, closed = True)
    380         assert allclose(indices, [])
     387        assert numpy.allclose(indices, [])
    381388
    382389        indices = inside_polygon(points, U, closed = True)
    383         assert allclose(indices, [0,1,2])
     390        assert numpy.allclose(indices, [0,1,2])
    384391       
    385392
     
    388395
    389396        indices, count = separate_points_by_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U )
    390         assert allclose( indices, [0,2,1] )
     397        assert numpy.allclose( indices, [0,2,1] )
    391398        assert count == 2
    392399       
     
    396403        res, count = separate_points_by_polygon( points, polygon )
    397404
    398         assert allclose( res, [0,1,2,4,3] )
     405        assert numpy.allclose( res, [0,1,2,4,3] )
    399406        assert count == 3
    400407
     
    404411        res, count = separate_points_by_polygon( points, polygon )
    405412
    406         assert allclose( res, [1,2,3,5,4,0] )       
     413        assert numpy.allclose( res, [1,2,3,5,4,0] )       
    407414        assert count == 3
    408415       
     
    588595        status, value = intersection(line0, line1)
    589596        assert status == 1
    590         assert allclose(value, [0.0, 0.0])
     597        assert numpy.allclose(value, [0.0, 0.0])
    591598
    592599    def test_intersection2(self):
     
    596603        status, value = intersection(line0, line1)
    597604        assert status == 1
    598         assert allclose(value, [12.0, 6.0])
     605        assert numpy.allclose(value, [12.0, 6.0])
    599606
    600607        # Swap direction of one line
     
    603610        status, value = intersection(line0, line1)
    604611        assert status == 1
    605         assert allclose(value, [12.0, 6.0])
     612        assert numpy.allclose(value, [12.0, 6.0])
    606613
    607614        # Swap order of lines
    608615        status, value = intersection(line1, line0)
    609616        assert status == 1
    610         assert allclose(value, [12.0, 6.0])       
     617        assert numpy.allclose(value, [12.0, 6.0])       
    611618       
    612619    def test_intersection3(self):
     
    616623        status, value = intersection(line0, line1)
    617624        assert status == 1
    618         assert allclose(value, [14.068965517, 7.0344827586])
     625        assert numpy.allclose(value, [14.068965517, 7.0344827586])
    619626
    620627        # Swap direction of one line
     
    623630        status, value = intersection(line0, line1)
    624631        assert status == 1
    625         assert allclose(value, [14.068965517, 7.0344827586])       
     632        assert numpy.allclose(value, [14.068965517, 7.0344827586])       
    626633
    627634        # Swap order of lines
    628635        status, value = intersection(line1, line0)
    629636        assert status == 1       
    630         assert allclose(value, [14.068965517, 7.0344827586])       
     637        assert numpy.allclose(value, [14.068965517, 7.0344827586])       
    631638
    632639
     
    641648        status, value = intersection(line0, line1)
    642649        assert status == 1
    643         assert allclose(value, [1.0, 1.0])
     650        assert numpy.allclose(value, [1.0, 1.0])
    644651
    645652
     
    649656        status, value = intersection(line0, line1)
    650657        assert status == 1
    651         assert allclose(value, [1.0, 1.0])       
     658        assert numpy.allclose(value, [1.0, 1.0])       
    652659       
    653660
     
    679686                                                          common_end_point[1])
    680687            msg += ' gave %s, \nbut when reversed we got %s' %(p1, p2)
    681             assert allclose(p1, p2), msg
     688            assert numpy.allclose(p1, p2), msg
    682689
    683690            # Swap order of lines
     
    685692            assert status == 1                       
    686693            msg = 'Order of lines gave different results'
    687             assert allclose(p1, p3), msg
     694            assert numpy.allclose(p1, p3), msg
    688695           
    689696
     
    725732        status, value = intersection(line0, line1)
    726733        assert status == 2
    727         assert allclose(value, [[0,0], [3,0]])
     734        assert numpy.allclose(value, [[0,0], [3,0]])
    728735
    729736        # Overlap 2
     
    733740        status, value = intersection(line0, line1)
    734741        assert status == 2
    735         assert allclose(value, [[-3, 0], [5,0]])       
     742        assert numpy.allclose(value, [[-3, 0], [5,0]])       
    736743
    737744        # Inclusion 1
     
    741748        status, value = intersection(line0, line1)
    742749        assert status == 2       
    743         assert allclose(value, line1)
     750        assert numpy.allclose(value, line1)
    744751
    745752        # Inclusion 2
     
    749756        status, value = intersection(line0, line1)
    750757        assert status == 2       
    751         assert allclose(value, line0)                                       
     758        assert numpy.allclose(value, line0)                                       
    752759
    753760
     
    768775        status, value = intersection(line0, line1)
    769776        assert status == 2               
    770         assert allclose(value, [[1, 7], [7, 19]])
     777        assert numpy.allclose(value, [[1, 7], [7, 19]])
    771778
    772779        status, value = intersection(line1, line0)
    773780        assert status == 2
    774         assert allclose(value, [[1, 7], [7, 19]])
     781        assert numpy.allclose(value, [[1, 7], [7, 19]])
    775782
    776783        # Swap direction
     
    779786        status, value = intersection(line0, line1)
    780787        assert status == 2
    781         assert allclose(value, [[7, 19], [1, 7]])
     788        assert numpy.allclose(value, [[7, 19], [1, 7]])
    782789
    783790        line0 = [[0,5], [7,19]]
     
    785792        status, value = intersection(line0, line1)
    786793        assert status == 2
    787         assert allclose(value, [[1, 7], [7, 19]])       
     794        assert numpy.allclose(value, [[1, 7], [7, 19]])       
    788795       
    789796
     
    793800        status, value = intersection(line0, line1)
    794801        assert status == 2                       
    795         assert allclose(value, [[1,7], [7, 19]])               
     802        assert numpy.allclose(value, [[1,7], [7, 19]])               
    796803
    797804        line0 = [[0,5], [10,25]]
     
    799806        status, value = intersection(line0, line1)
    800807        assert status == 2                       
    801         assert allclose(value, [[1,7], [7, 19]])
     808        assert numpy.allclose(value, [[1,7], [7, 19]])
    802809
    803810
     
    806813        status, value = intersection(line0, line1)
    807814        assert status == 2                       
    808         assert allclose(value, [[7, 19], [1, 7]])                       
     815        assert numpy.allclose(value, [[7, 19], [1, 7]])                       
    809816       
    810817       
     
    843850        res = inside_polygon(points, polygon)
    844851        assert len(res) == 2
    845         assert allclose(res, [0,1])
     852        assert numpy.allclose(res, [0,1])
    846853
    847854    def test_polygon_area(self):
     
    990997if __name__ == "__main__":
    991998    suite = unittest.makeSuite(Test_Polygon,'test')
    992     #suite = unittest.makeSuite(Test_Polygon,'test_inside_polygon_geo_ref')
     999##    suite = unittest.makeSuite(Test_Polygon,'test_polygon_function_constantsX')
    9931000    runner = unittest.TextTestRunner()
    9941001    runner.run(suite)
Note: See TracChangeset for help on using the changeset viewer.