예제 #1
0
파일: clip.py 프로젝트: josetak/pyfor
def ray_trace(x, y, poly):
    """
    Determines for some set of x and y coordinates, which of those coordinates is within `poly`. Ray trace is \
    generally called as an internal function, see :func:`.poly_clip`

    :param x: A 1D numpy array of x coordinates.
    :param y: A 1D numpy array of y coordinates.
    :param poly: The coordinates of a polygon as a numpy array (i.e. from geo_json['coordinates']
    :return: A 1D boolean numpy array, true values are those points that are within `poly`.
    """
    @vectorize([bool_(float64, float64)])
    def ray(x, y):
        # where xy is a coordinate
        n = len(poly)
        inside = False
        p2x = 0.0
        p2y = 0.0
        xints = 0.0
        p1x, p1y = poly[0]
        for i in range(n + 1):
            p2x, p2y = poly[i % n]
            if y > min(p1y, p2y):
                if y <= max(p1y, p2y):
                    if x <= max(p1x, p2x):
                        if p1y != p2y:
                            xints = (y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x
                        if p1x == p2x or x <= xints:
                            inside = not inside
            p1x, p1y = p2x, p2y
        return inside

    return ray(x, y)
예제 #2
0
파일: clip.py 프로젝트: jtpils/laxpy
def ray_trace(x, y, poly):
    """
    A numba implementation of the ray tracing algorithm.
    :param x: A 1D numpy array of x coordinates.
    :param y: A 1D numpy array of y coordinates.
    :param poly: A shapely polygon.
    :return:
    """

    poly = np.array(poly.exterior.coords)

    @vectorize([bool_(float64, float64)])
    def ray(x, y):
        # where xy is a coordinate
        n = len(poly)
        inside = False
        p2x = 0.0
        p2y = 0.0
        xints = 0.0
        p1x, p1y = poly[0]
        for i in range(n + 1):
            p2x, p2y = poly[i % n]
            if y > min(p1y, p2y):
                if y <= max(p1y, p2y):
                    if x <= max(p1x, p2x):
                        if p1y != p2y:
                            xints = (y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x
                        if p1x == p2x or x <= xints:
                            inside = not inside
            p1x, p1y = p2x, p2y
        return inside

    return (ray(x, y))
    def test_type_inference(self):
        global vector_add
        vector_add = vectorize([
            bool_(double, int_),
            double(double, double),
            float_(double, float_),
        ])(add)

        cfunc = jit(func)

        self.assertEqual(cfunc(np.dtype(np.float64), np.dtype('i')), int8[:])
        self.assertEqual(cfunc(np.dtype(np.float64), np.dtype(np.float64)),
                         double[:])
        self.assertEqual(cfunc(np.dtype(np.float64), np.dtype(np.float32)),
                         float_[:])
    def test_type_inference(self):
        """This is testing numpy ufunc dispatch machinery"""
        global vector_add
        vector_add = vectorize([
            bool_(double, int_),
            double(double, double),
            float_(double, float_),
        ])(add)

        cfunc = jit(func)

        def numba_type_equal(a, b):
            self.assertEqual(a.dtype, b.dtype)
            self.assertEqual(a.ndim, b.ndim)

        numba_type_equal(cfunc(np.dtype(np.float64), np.dtype("i")), bool_[:])
        numba_type_equal(cfunc(np.dtype(np.float64), np.dtype(np.float64)),
                         double[:])
        # This is because the double(double, double) matches first
        numba_type_equal(cfunc(np.dtype(np.float64), np.dtype(np.float32)),
                         double[:])
    def test_type_inference(self):
        """This is testing numpy ufunc dispatch machinery
        """
        global vector_add
        vector_add = vectorize([
            bool_(double, int_),
            double(double, double),
            float_(double, float_),
        ])(add)

        cfunc = jit(func)

        def numba_type_equal(a, b):
            self.assertEqual(a.dtype, b.dtype)
            self.assertEqual(a.ndim, b.ndim)

        numba_type_equal(cfunc(np.dtype(np.float64), np.dtype('i')), bool_[:])
        numba_type_equal(cfunc(np.dtype(np.float64), np.dtype(np.float64)),
                         double[:])
        # This is because the double(double, double) matches first
        numba_type_equal(cfunc(np.dtype(np.float64), np.dtype(np.float32)),
                         double[:])
예제 #6
0
파일: funcs.py 프로젝트: jawjay/numbagg
import numpy as np
from numba import bool_, float32, float64, int32, int64

from .decorators import ndreduce


@ndreduce([bool_(int32), bool_(int64), bool_(float32), bool_(float64)])
def allnan(a):
    f = True
    for ai in a.flat:
        if not np.isnan(ai):
            f = False
            break
    return f


@ndreduce([bool_(int32), bool_(int64), bool_(float32), bool_(float64)])
def anynan(a):
    f = False
    for ai in a.flat:
        if np.isnan(ai):
            f = True
            break
    return f


@ndreduce([int64(int32), int64(int64), int64(float32), int64(float64)])
def count(a):
    non_missing = 0
    for ai in a.flat:
        if not np.isnan(ai):
예제 #7
0
from numba import double, int64, int8, bool_
from numba.decorators import jit
from app.tool import deepthroat as dt


@jit(bool_(int64, int64, double[:], double[:]), nopython=True)
def contraction_share(v1, v2, high, low):
    """Calculate does v2 shares min 1 pip with volatility contraction

    Parameters
    ----------
    candle v1: int
    candle v2: int
    candle high: double[:]
    candle low: double[:]

    Returns
    -------
    True if share False if doesn't
    """
    for i in range(v1, v2, -1):
        if high[v2] < low[i] and low[v2] > high[i]:
            return False
    else:
        return True


@jit(int64(int64, int64[:], int8[:]), nopython=True)
def prior_bull_wrb(start, dir, wrb):
    for i in range(start - 1, 0, -1):
        if wrb[i] == 1 and dir[i] == 1:
예제 #8
0
파일: sieve.py 프로젝트: pombreda/python-1
import numba
from math import sqrt

@numba.jit(numba.bool_(numba.int64), nopython=True)
def is_prime(n):
    d = int(sqrt(n))
    while d > 1:
        if n % d == 0:
            return False
        else:
            d = d-1
    return True
예제 #9
0
            q_trans_tr[i] *= f_trans_tr[j]
        # Inverts about 1, to take the _infection_ probability
        q_trans_tr[i] = 1. - q_trans_tr[i]


# ---------------
# Convergence (consecutive states close to each other) and numerical error checking

# # Convergence check - numpy version
# def check_states_are_close(p_state, p_next, tol):
#     return np.amax(np.abs((p_next - p_state))) < tol


# Convergence check - numba version
@nb.njit(
    nb.bool_(nb_ncount_t, nb_int_t, nb_p_t[:, :], nb_p_t[:, :], nb_float_t))
def check_states_are_close(num_nodes, num_states, p_state, p_next, tol):
    """
    Check if all markov state probabilities from the current and the (already calculated) next step are all close
    up to a given tolerance tol. The operation is executed for all nodes and all states. Any entry that exceeds the
    tolerance immediately implies a 'False' return.
    """
    for i_s in range(num_states):
        for i in range(num_nodes):
            if abs(p_next[i_s, i] - p_state[i_s, i]) >= tol:
                return False

    return True


@nb.njit(nb.void(nb_ncount_t, nb_p_t[:, :]))