Ignore:
Timestamp:
Nov 6, 2008, 3:32:23 PM (16 years ago)
Author:
rwilson
Message:

NumPy? conversion.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source_numpy_conversion/anuga/utilities/polygon.py

    r5889 r5902  
    1010#    #print 'Could not find scipy - using Numeric'
    1111
    12 from numpy import float, int, zeros, ones, array, concatenate, reshape, dot, allclose, newaxis, ascontiguousarray
     12##from numpy import float, int, zeros, ones, array, concatenate, reshape, dot, allclose, newaxis, ascontiguousarray
     13import numpy
    1314
    1415
     
    7677    # FIXME (Ole): Write this in C
    7778
    78     line0 = ensure_numeric(line0, float)
    79     line1 = ensure_numeric(line1, float)   
     79    line0 = ensure_numeric(line0, numpy.float)
     80    line1 = ensure_numeric(line1, numpy.float)   
    8081
    8182    x0 = line0[0,0]; y0 = line0[0,1]
     
    8990    u1 = (x2-x0)*(y1-y0) - (y2-y0)*(x1-x0)
    9091       
    91     if allclose(denom, 0.0):
     92    if numpy.allclose(denom, 0.0):
    9293        # Lines are parallel - check if they coincide on a shared a segment
    9394
    94         if allclose( [u0, u1], 0.0 ):
     95        if numpy.allclose( [u0, u1], 0.0 ):
    9596            # We now know that the lines if continued coincide
    9697            # The remaining check will establish if the finite lines share a segment
     
    120121            if line0_starts_on_line1 and line0_ends_on_line1:
    121122                # Shared segment is line0 fully included in line1
    122                 segment = array([[x0, y0], [x1, y1]])               
     123                segment = numpy.array([[x0, y0], [x1, y1]])               
    123124
    124125            if line1_starts_on_line0 and line1_ends_on_line0:
    125126                # Shared segment is line1 fully included in line0
    126                 segment = array([[x2, y2], [x3, y3]])
     127                segment = numpy.array([[x2, y2], [x3, y3]])
    127128           
    128129
     
    130131            if line0_starts_on_line1 and line1_ends_on_line0:
    131132                # Shared segment from line0 start to line 1 end
    132                 segment = array([[x0, y0], [x3, y3]])
     133                segment = numpy.array([[x0, y0], [x3, y3]])
    133134
    134135            if line1_starts_on_line0 and line0_ends_on_line1:
    135136                # Shared segment from line1 start to line 0 end
    136                 segment = array([[x2, y2], [x1, y1]])                               
     137                segment = numpy.array([[x2, y2], [x1, y1]])                               
    137138
    138139
     
    140141            if line0_starts_on_line1 and line1_starts_on_line0:
    141142                # Shared segment from line0 start to line 1 end
    142                 segment = array([[x0, y0], [x2, y2]])
     143                segment = numpy.array([[x0, y0], [x2, y2]])
    143144
    144145            if line0_ends_on_line1 and line1_ends_on_line0:
    145146                # Shared segment from line0 start to line 1 end
    146                 segment = array([[x3, y3], [x1, y1]])               
     147                segment = numpy.array([[x3, y3], [x1, y1]])               
    147148
    148149               
     
    161162
    162163        # Sanity check - can be removed to speed up if needed
    163         assert allclose(x, x2 + u1*(x3-x2))
    164         assert allclose(y, y2 + u1*(y3-y2))       
     164        assert numpy.allclose(x, x2 + u1*(x3-x2))
     165        assert numpy.allclose(y, y2 + u1*(y3-y2))       
    165166
    166167        # Check if point found lies within given line segments
     
    168169            # We have intersection
    169170
    170             return 1, array([x, y])
     171            return 1, numpy.array([x, y])
    171172        else:
    172173            # No intersection
     
    203204
    204205
    205     line0 = ensure_numeric(line0, float)
    206     line1 = ensure_numeric(line1, float)   
     206    line0 = ensure_numeric(line0, numpy.float)
     207    line1 = ensure_numeric(line1, numpy.float)   
    207208
    208209    status, value = _intersection(line0[0,0], line0[0,1],
     
    270271    if len(points.shape) == 1:
    271272        # Only one point was passed in. Convert to array of points
    272         points = reshape(points, (1,2))
     273        points = numpy.reshape(points, (1,2))
    273274
    274275    indices, count = separate_points_by_polygon(points, polygon,
     
    312313    #if verbose: print 'Checking input to outside_polygon'
    313314    try:
    314         points = ensure_numeric(points, float)
     315        points = ensure_numeric(points, numpy.float)
    315316    except NameError, e:
    316317        raise NameError, e
     
    320321
    321322    try:
    322         polygon = ensure_numeric(polygon, float)
     323        polygon = ensure_numeric(polygon, numpy.float)
    323324    except NameError, e:
    324325        raise NameError, e
     
    330331    if len(points.shape) == 1:
    331332        # Only one point was passed in. Convert to array of points
    332         points = reshape(points, (1,2))
     333        points = numpy.reshape(points, (1,2))
    333334
    334335    indices, count = separate_points_by_polygon(points, polygon,
     
    339340    if count == len(indices):
    340341        # No points are outside
    341         return array([])
     342        return numpy.array([])
    342343    else:
    343344        return indices[count:][::-1]  #return reversed
     
    354355    #if verbose: print 'Checking input to outside_polygon'
    355356    try:
    356         points = ensure_numeric(points, float)
     357        points = ensure_numeric(points, numpy.float)
    357358    except NameError, e:
    358359        raise NameError, e
     
    362363
    363364    try:
    364         polygon = ensure_numeric(polygon, float)
     365        polygon = ensure_numeric(polygon, numpy.float)
    365366    except NameError, e:
    366367        raise NameError, e
     
    371372    if len(points.shape) == 1:
    372373        # Only one point was passed in. Convert to array of points
    373         points = reshape(points, (1,2))
     374        points = numpy.reshape(points, (1,2))
    374375
    375376
     
    441442##    print 'Before: points=%s, flags=%s' % (type(points), str(points.flags))
    442443    try:
    443 ##        points = ascontiguousarray(ensure_numeric(points, float))
    444         points = ensure_numeric(points, float)
     444##        points = numpy.ascontiguousarray(ensure_numeric(points, numpy.float))
     445        points = ensure_numeric(points, numpy.float)
    445446    except NameError, e:
    446447        raise NameError, e
     
    452453    #if verbose: print 'Checking input to separate_points_by_polygon 2'
    453454    try:
    454 ##        polygon = ascontiguousarray(ensure_numeric(polygon, float))
    455         polygon = ensure_numeric(polygon, float)
     455##        polygon = numpy.ascontiguousarray(ensure_numeric(polygon, numpy.float))
     456        polygon = ensure_numeric(polygon, numpy.float)
    456457    except NameError, e:
    457458        raise NameError, e
     
    475476        # Only one point was passed in.
    476477        # Convert to array of points
    477         points = reshape(points, (1,2))
     478        points = numpy.reshape(points, (1,2))
    478479
    479480   
     
    494495
    495496
    496     indices = zeros( M, int )
     497    indices = numpy.zeros( M, numpy.int )
    497498
    498499    count = _separate_points_by_polygon(points, polygon, indices,
     
    621622
    622623    try:
    623         polygon = ensure_numeric(polygon, float)
     624        polygon = ensure_numeric(polygon, numpy.float)
    624625    except NameError, e:
    625626        raise NameError, e
     
    630631    x = polygon[:,0]
    631632    y = polygon[:,1]
    632     x = concatenate((x, [polygon[0,0]]), axis = 0)
    633     y = concatenate((y, [polygon[0,1]]), axis = 0)
     633    x = numpy.concatenate((x, [polygon[0,0]]), axis = 0)
     634    y = numpy.concatenate((y, [polygon[0,1]]), axis = 0)
    634635   
    635636    return x, y
     
    732733
    733734    def __call__(self, x, y):
    734         x = array(x).astype(float)
    735         y = array(y).astype(float)
     735        x = numpy.array(x).astype(numpy.float)
     736        y = numpy.array(y).astype(numpy.float)
    736737
    737738        assert len(x.shape) == 1 and len(y.shape) == 1
     
    740741        assert y.shape[0] == N
    741742
    742         points = ascontiguousarray(concatenate( (x[:,newaxis], y[:,newaxis]), axis=1 ))
     743        points = numpy.ascontiguousarray(numpy.concatenate( (x[:,numpy.newaxis], y[:,numpy.newaxis]), axis=1 ))
    743744       
    744745        if callable(self.default):
    745746            z = self.default(x, y)
    746747        else:
    747             z = ones(N, float) * self.default
     748            z = numpy.ones(N, numpy.float) * self.default
    748749
    749750        for polygon, value in self.regions:
     
    753754            if callable(value):
    754755                for i in indices:
    755                     xx = array([x[i]])
    756                     yy = array([y[i]])
     756                    xx = numpy.array([x[i]])
     757                    yy = numpy.array([y[i]])
    757758                    z[i] = value(xx, yy)[0]
    758759            else:
Note: See TracChangeset for help on using the changeset viewer.