Beispiel #1
0
 def test_missing_module(self):
     with self.assertRaises(ImportError) as raises:
         get_cython_function_address("fakemodule", "fakefunction")
     # The quotes are not there in Python 2
     msg = "No module named '?fakemodule'?"
     match = re.match(msg, str(raises.exception))
     self.assertIsNotNone(match)
Beispiel #2
0
 def test_missing_module(self):
     with self.assertRaises(ImportError) as raises:
         addr = get_cython_function_address("fakemodule", "fakefunction")
     # The quotes are not there in Python 2
     msg = "No module named '?fakemodule'?"
     match = re.match(msg, str(raises.exception))
     self.assertIsNotNone(match)
Beispiel #3
0
 def test_getting_function(self):
     addr = get_cython_function_address("scipy.special.cython_special",
                                        "j0")
     functype = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double)
     _j0 = functype(addr)
     j0 = jit(nopython=True)(lambda x: _j0(x))
     self.assertEqual(j0(0), 1)
 def test_missing_function(self):
     with self.assertRaises(ValueError) as raises:
         addr = get_cython_function_address("scipy.special.cython_special", "foo")
     msg = (
         "No function 'foo' found in __pyx_capi__ of 'scipy.special.cython_special'"
     )
     self.assertEqual(msg, str(raises.exception))
Beispiel #5
0
 def cy_unboxer(self):
     '''
     Return the address of the cython generated C(++) function which
     provides the actual DAAL C++ pointer for our result/model Python object.
     '''
     addr = get_cython_function_address("_daal4py", 'unbox_'+self.spec.c_name)
     return addr
Beispiel #6
0
def _make_betainc():
    addr = get_cython_function_address('scipy.special.cython_special',
                                       'betainc')
    betainc_type = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double,
                                    ctypes.c_double, ctypes.c_double)
    func = betainc_type(addr)
    return func
Beispiel #7
0
def get_special_function_pointer(ctypes_signature, function_name):
    try:
        address = get_cython_function_address(
            'scipy.special.cython_special',
            function_name,
        )
    except ValueError:
        return None
    return ctypes.CFUNCTYPE(*ctypes_signature)(address)
Beispiel #8
0
 def get_chunked_array_xarray_value(element_type, element_type_name):
     addr = get_cython_function_address(
         self.override_module_name,
         "ChunkedArray_" + element_type_name + "Array_Value",
     )
     ChunkedArray_xArray_Value = ctypes.CFUNCTYPE(
         element_type, ctypes.c_voidp, ctypes.c_uint64,
         ctypes.c_uint64)(addr)
     return ChunkedArray_xArray_Value
Beispiel #9
0
def get_cython_function_address_with_defaults(full_function_name, default_module_name, default_function_name):
    module_name = None
    function_name = None
    if full_function_name:
        i = full_function_name.rfind(".")
        if i >= 0:
            module_name = full_function_name[:i]
            function_name = full_function_name[i + 1 :]
        else:
            function_name = full_function_name
    return get_cython_function_address(module_name or default_module_name, function_name or default_function_name)
Beispiel #10
0
def generate_signatures_dicts(signature_to_pointer):
    name_to_numba_signatures = collections.defaultdict(list)
    name_and_types_to_pointer = {}
    for mangled_name, *signature in signature_to_pointer.keys():
        name = de_mangle_function_name(mangled_name)
        name_to_numba_signatures[name].append(tuple(signature[1:]))

        key = (name, ) + tuple(signature[1:])

        address = (get_cython_function_address('scipy.special.cython_special',
                                               mangled_name))
        ctypes_signature = [NUMBA_TO_CTYPES[t] for t in signature]
        ctypes_cast = (ctypes.CFUNCTYPE(*ctypes_signature))
        name_and_types_to_pointer[key] = ctypes_cast(address)

    name_to_numba_signatures = {
        name: tuple(signatures)
        for name, signatures in name_to_numba_signatures.items()
    }
    return name_to_numba_signatures, name_and_types_to_pointer
            thresh = beta - alpha
            if thresh < -1:
                matrix[n_t, n_r] += scale * 0.5 * erfcx(-thresh) * np.exp(
                    -beta * beta)
            else:
                matrix[n_t, n_r] += (scale * 0.5 * (1 + erf(thresh)) *
                                     np.exp(alpha * (alpha - 2 * beta)))
            if backsweep:
                x1 = np.exp(-r_n * (t_n - center + backsweep_period))
                x2 = np.exp(-r_n * ((backsweep_period / 2) - (t_n - center)))
                x3 = np.exp(-r_n * backsweep_period)
                matrix[n_t, n_r] += scale * (x1 + x2) / (1 - x3)


import ctypes  # noqa: E402

# This is a work around to use scipy.special function with numba
from numba.extending import get_cython_function_address  # noqa: E402

_dble = ctypes.c_double

functype = ctypes.CFUNCTYPE(_dble, _dble)

erf_addr = get_cython_function_address("scipy.special.cython_special",
                                       "__pyx_fuse_1erf")
erfcx_addr = get_cython_function_address("scipy.special.cython_special",
                                         "__pyx_fuse_1erfcx")

erf = functype(erf_addr)
erfcx = functype(erfcx_addr)
Beispiel #12
0
from numba.extending import get_cython_function_address

__ffi = cffi.FFI()

__uplo_U = np.array([ord('U')], dtype=np.int8)
__uplo_L = np.array([ord('L')], dtype=np.int8)
__trans_N = np.array([ord('N')], dtype=np.int8)
__trans_T = np.array([ord('T')], dtype=np.int8)
__trans_C = np.array([ord('C')], dtype=np.int8)
__diag_U = np.array([ord('U')], dtype=np.int8)
__diag_N = np.array([ord('N')], dtype=np.int8)
__inc_1 = np.ones(1, dtype=np.int32)

__dtrsv = __ffi.cast(
    "void (*) (char*, char*, char*, int*, double*, int*, double*, int*)",
    get_cython_function_address("scipy.linalg.cython_blas", "dtrsv"))
__dposv = __ffi.cast(
    "void (*) (char*, int*, int*, double*, int*, double*, int*, int*)",
    get_cython_function_address("scipy.linalg.cython_lapack", "dposv"))


@n.njit(n.void(n.boolean, n.boolean, n.double[:, ::1], n.double[::1]),
        nogil=True)
def _dtrsv(lower, trans, a, x):
    inc1 = __ffi.from_buffer(__inc_1)

    # dtrsv uses Fortran-layout arrays. Because we use C-layout arrays, we will
    # invert the meaning of 'lower' and 'trans', and the function will work fine.
    # We also need to swap index orders
    uplo = __uplo_U if lower else __uplo_L
    tspec = __trans_N if trans else __trans_T
Beispiel #13
0
import ctypes
from math import erf
import numba as nb
from numba import njit
from numba.extending import get_cython_function_address
import numpy as np
import scipy.stats as stats
from .statsutils import chi_test, kruskal, kendalltau

# Import erfinv from scipy special into numba
addr = get_cython_function_address("scipy.special.cython_special", "erfinv")
functype = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double)
erfinv_fn = functype(addr)


@njit
def resample_strat(x, y, n_classes):
    """
    Resampling (bootstrapping) of x stratified according to y.

    Parameters
    ----------
    x: numpy array
        Typically a matrix nxm with n samples and m variables.
    y: numpy array (1D)
        The class variable we are stratifying against.
    n_classes: int
        The number of classes in y.

    Returns
    -------
Beispiel #14
0
  <capsule object "double (double, double, int __pyx_skip_dispatch)" at 0x7f7148cbf480>)]
```
We get a list of tuples consisting of the function name (__pyx_fuse_...) and the type signature (<capsule object ....).
Clearly ```__pyx_fuse_0kve``` and ```__pyx_fuse_1kve``` correspond to the function ```scipy.special.kve``` which is not what we want.
```__pyx_fuse_0kv``` takes complex values as input and output parameter, also not what we want.
So, ```__pyx_fuse_1kv``` seems to be the right one, acting on doubles!

[1] https://numba.pydata.org/numba-doc/dev/extending/high-level.html#importing-cython-functions
[2] https://www.bountysource.com/issues/78681198-questions-about-__pyx_capi__-use-stability
"""

import ctypes
from numba.extending import get_cython_function_address


addr = get_cython_function_address("scipy.special.cython_special", "__pyx_fuse_1kv")
functype = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double, ctypes.c_double)
kv = functype(addr)

addr = get_cython_function_address("scipy.special.cython_special", "__pyx_fuse_1gamma")
functype = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double)
gamma = functype(addr)


# small test to make sure we have the correct thing
if __name__ == '__main__':
    import numpy as np
    import scipy.special as ss 

    for x in [0.2, 0.5, 1, 1.5, 2]:
        np.testing.assert_allclose(ss.gamma(x), gamma(x))
Beispiel #15
0
# import numpy as np
# from scipy.linalg.cython_lapack cimport dpotrf, dpotrs

# --- wrappers to BLAS/LAPACK functions ----------------------------------------

# necessary ctypes
_PTR = ctypes.POINTER
_float = ctypes.c_float
_double = ctypes.c_double
_int = ctypes.c_int
_float_p = _PTR(_float)
_double_p = _PTR(_double)
_int_p = _PTR(_int)

# dpotrf (cho_factor for doubles)
addr = get_cython_function_address('scipy.linalg.cython_lapack', 'dpotrf')
functype = ctypes.CFUNCTYPE(
    None,
    _int_p,  # UPLO
    _int_p,  # N
    _double_p,  # U
    _int_p,  # LDA
    _int_p,  # INFO
)
_dpotrf = functype(addr)
# cho_factor: float
addr = get_cython_function_address('scipy.linalg.cython_lapack', 'spotrf')
functype = ctypes.CFUNCTYPE(
    None,
    _int_p,  # UPLO
    _int_p,  # N
Beispiel #16
0
                     [2 * (bd + ac), 2 * (cd - ab), aa + dd - bb - cc]])


@jit(nopython=True)
def rotate_by_theta(n, theta):
    """rotates vector n by theta in random direction"""
    axis = np.random.randn(3)
    axis -= numba_dot(axis, n) * n / numba_norm(n) ** 2
    axis /= numba_norm(axis)
    return numba_dot(rotation_matrix(axis, theta), n)


# numba optimized Voigt function using numba wrapper around scipy.wofz (Faddeeva function)
# see https://github.com/numba/numba/issues/3086
# python setup.py build_ext --inplace
_PTR = ctypes.POINTER
_dble = ctypes.c_double
_ptr_dble = _PTR(_dble)

addr = get_cython_function_address("special", "wofz")
functype = ctypes.CFUNCTYPE(None, _dble, _dble, _ptr_dble, _ptr_dble)
wofz_fn = functype(addr)


@jit(nopython=True)
def numba_Voigt(a, x):
    out_real = np.empty(1, dtype=np.float64)
    out_imag = np.empty(1, dtype=np.float64)
    wofz_fn(x, a, out_real.ctypes, out_imag.ctypes)   
    return out_real[0]
Beispiel #17
0
import ctypes
from math import gamma, erfc
import numba as nb
from numba import njit, vectorize, uint64
from numba.extending import get_cython_function_address
import numpy as np


addr = get_cython_function_address("scipy.special.cython_special", "chdtrc")
functype = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double, ctypes.c_double)
chdtrc_fn = functype(addr)


@vectorize('float64(float64, float64)')
def vec_chdtrc(x, y):
    return chdtrc_fn(x, y)

@njit
def chdtrc(x, y):
    return vec_chdtrc(x, y)

###############################
##### Independence Tests ######
###############################


@njit
def kruskal(x, y):
    alldata = np.concatenate((x, y))
    ranked = rankdata(alldata, use_missing=False)
    ties = tiecorrect(ranked[ranked>1])
Beispiel #18
0
import matplotlib.pyplot as plt
import nlopt
import numpy as np
from numba import njit
from numba import vectorize
from numba.extending import get_cython_function_address
from scipy import special
from tables import ComplexCol
from tables import Float64Col
from tables import IsDescription
from tables import open_file
from tqdm import tqdm

from SiPANN import scee

addr = get_cython_function_address("scipy.special.cython_special", "binom")
functype = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double, ctypes.c_double)
binom_fn = functype(addr)

##################################################
###              HELPER FUNCTIONS              ###
###  used to help quickly define gap functions ###
##################################################


@njit
def bernstein_quick(n, j, t):
    """Quickly computes the jth bernstein polynomial for the basis of n+1
    polynomials.

    Parameters
Beispiel #19
0
    def __init__(self,
                 orig_typ,
                 addr_func,
                 addr_func_name=None,
                 override_module_name=None):
        # HACK: No super call

        class Type(numba.types.Type):
            def __init__(self, chunk_type):
                super().__init__(name=orig_typ.__name__ + "[" +
                                 chunk_type.__name__ + "]")
                self.chunk_type = chunk_type

            @property
            def key(self):
                return (self.name, self.chunk_type)

        typs = {k: Type(c) for k, c in _array_type_map.items()}

        @typeof_impl.register(orig_typ)
        def typeof_(val, c):
            _ = c
            return typs[val.type]

        _ = typeof_

        self._build_model(Type)

        self.Type = Type
        self.types = typs
        self.type_name = orig_typ.__name__
        self.module_name = orig_typ.__module__
        self.override_module_name = override_module_name or self.module_name
        self.orig_type = orig_typ

        self.addr_func = self._build_unbox_by_call(addr_func, addr_func_name)

        addr = get_cython_function_address(self.override_module_name,
                                           "ChunkedArray_length")
        ChunkedArray_length = ctypes.CFUNCTYPE(ctypes.c_uint64,
                                               ctypes.c_voidp)(addr)

        @overload(len)
        def overload_len(v):
            if isinstance(v, self.Type):

                def impl_(v):
                    return ChunkedArray_length(v.ptr)

                return impl_
            return None

        _ = overload_len

        addr = get_cython_function_address(self.override_module_name,
                                           "ChunkedArray_num_chunks")
        ChunkedArray_num_chunks = ctypes.CFUNCTYPE(ctypes.c_uint64,
                                                   ctypes.c_voidp)(addr)

        addr = get_cython_function_address(self.override_module_name,
                                           "ChunkedArray_Array_chunk_length")
        ChunkedArray_Array_chunk_length = ctypes.CFUNCTYPE(
            ctypes.c_uint64, ctypes.c_voidp, ctypes.c_uint64)(addr)

        @overload_method(self.Type, "convert_index")
        def overload_convert_index(v, i):
            if isinstance(v, self.Type):
                if isinstance(i, numba.types.UniTuple) and i.types == (
                        numba.types.uint64,
                        numba.types.uint64,
                ):

                    def impl_pair_index(v, i):
                        _ = v
                        return i

                    return impl_pair_index

                def impl_single_index(v, i):
                    chunk = 0
                    while True:  # contains break
                        chunk_len = ChunkedArray_Array_chunk_length(
                            v.ptr, chunk)
                        if i < chunk_len:
                            break
                        i -= chunk_len
                        chunk += 1
                    return (chunk, i)

                return impl_single_index
            return None

        _ = overload_convert_index

        addr = get_cython_function_address(self.override_module_name,
                                           "ChunkedArray_Array_is_valid")
        ChunkedArray_is_valid = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_voidp,
                                                 ctypes.c_uint64,
                                                 ctypes.c_uint64)(addr)

        @overload_method(self.Type, "is_valid")
        def overload_is_valid(v, ind):
            _ = v
            _ = ind

            def impl_(v, ind):
                c, i = v.convert_index(ind)
                return ChunkedArray_is_valid(v.ptr, c, i)

            return impl_

        _ = overload_is_valid

        @overload_method(self.Type, "is_null")
        def overload_is_null(v, ind):
            _ = v
            _ = ind

            def impl_(v, ind):
                c, i = v.convert_index(ind)
                return not ChunkedArray_is_valid(v.ptr, c, i)

            return impl_

        _ = overload_is_null

        @overload_method(self.Type, "indicies")
        def overload_indicies(v):
            _ = v

            def impl_(v):
                for c in range(ChunkedArray_num_chunks(v.ptr)):
                    for i in range(ChunkedArray_Array_chunk_length(v.ptr, c)):
                        if v.is_valid((c, i)):
                            yield (c, i)

            return impl_

        _ = overload_indicies

        def get_chunked_array_xarray_value(element_type, element_type_name):
            addr = get_cython_function_address(
                self.override_module_name,
                "ChunkedArray_" + element_type_name + "Array_Value",
            )
            ChunkedArray_xArray_Value = ctypes.CFUNCTYPE(
                element_type, ctypes.c_voidp, ctypes.c_uint64,
                ctypes.c_uint64)(addr)
            return ChunkedArray_xArray_Value

        ChunkedArray_xArray_Value_map = {
            t: get_chunked_array_xarray_value(
                _arrow_ctypes_map[_type_array_map[t.chunk_type]],
                t.chunk_type.__name__[:-5],
            )
            for t in typs.values()
        }

        @overload(operator.getitem)
        def overload_getitem(v, i):
            if isinstance(v, self.Type):
                ChunkedArray_xArray_Value = ChunkedArray_xArray_Value_map[v]
                if isinstance(i, numba.types.UniTuple) and i.types == (
                        numba.types.uint64,
                        numba.types.uint64,
                ):

                    def impl_pair_index(v, i):
                        return ChunkedArray_xArray_Value(v.ptr, i[0], i[1])

                    return impl_pair_index

                def impl_single_index(v, i):
                    chunk = 0
                    while True:  # contains break
                        chunk_len = ChunkedArray_Array_chunk_length(
                            v.ptr, chunk)
                        if i < chunk_len:
                            break
                        i -= chunk_len
                        chunk += 1
                    return ChunkedArray_xArray_Value(v.ptr, chunk, i)

                return impl_single_index
            return None

        _ = overload_getitem

        @overload_method(self.Type, "values")
        def overload_values(v):
            ChunkedArray_xArray_Value = ChunkedArray_xArray_Value_map[v]

            def impl_(v):
                for c in range(ChunkedArray_num_chunks(v.ptr)):
                    for i in range(ChunkedArray_Array_chunk_length(v.ptr, c)):
                        if v.is_valid((c, i)):
                            yield ChunkedArray_xArray_Value(v.ptr, c, i)

            return impl_

        _ = overload_values
Beispiel #20
0
import numba
from numba.extending import get_cython_function_address
import ctypes
from numpy import ascontiguousarray, linspace, interp, zeros, diff, double, sqrt, arange

_PTR = ctypes.POINTER
_dble = ctypes.c_double
_ptr_dble = _PTR(_dble)

addr = get_cython_function_address("optimum_reparam_N", "reparm_dp")
reparm_dp_functype = ctypes.CFUNCTYPE(_ptr_dble, _ptr_dble)
reparm_dp_numba = reparm_dp_functype(addr)


@numba.jit()
def grad(f, binsize):
    n = f.shape[0]
    g = zeros(n)
    h = binsize * arange(1, n + 1)
    g[0] = (f[0] - f[0]) / (h[1] - h[0])
    g[-1] = (f[-1] - f[(-2)]) / (h[-1] - h[-2])

    h = h[2:-1] - h[0:-3]
    g[1:-2] = (f[2:-1] - f[0:-3]) / h[0]

    return g


@numba.njit()
def efda_distance(q1, q2):
    if (q1 == q2).all():
Beispiel #21
0
                       n_points=500,
                       n_cond=2):
    """
    Simulates a diffusion process given a parameter vector.
    """

    x = np.zeros((n_points, n_cond), dtype=np.float32)
    simulate_diffusion_parallel(x, drifts, sv, zr, szr, a, ndt, sndt, alpha)
    return x


# Get a pointer to the C function diffusion.c
if __name__ != '__main__':

    # Add to path
    sys.path.append(
        sys.path.insert(
            0,
            os.path.join(
                os.path.dirname(os.path.dirname(os.path.realpath(__file__))),
                'simulators')))

    addr_diffusion = get_cython_function_address("diffusion",
                                                 "diffusion_trial")
    functype = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double,
                                ctypes.c_double, ctypes.c_double,
                                ctypes.c_double, ctypes.c_double,
                                ctypes.c_double, ctypes.c_double,
                                ctypes.c_double, ctypes.c_double, ctypes.c_int)

    diffusion_trial = functype(addr_diffusion)
Beispiel #22
0
    
    phi_neg_choice = norm.cdf(- sqrt_x_k.reshape(1, degrees, 1) 
                                - u_dif.reshape(n_obs, 1, n_choices - 1)) 
    phi_pos_choice = norm.cdf(sqrt_x_k.reshape(1, degrees, 1) 
                                - u_dif.reshape(n_obs, 1, n_choices -1))
    
    choice_probs_interm = phi_neg_choice.prod(axis=2) + phi_pos_choice.prod(axis=2)
    choice_prob_obs = (1/(2*np.sqrt(np.pi)))*np.dot(choice_probs_interm, fraction)
    
    return choice_prob_obs



# FROM HERE ON THIS CODE IS MADE BY JANOS!!
    
cdf_addr = get_cython_function_address("scipy.special.cython_special", '__pyx_fuse_1ndtr')
functype = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double)
normcdf = functype(cdf_addr)

ppf_addr = get_cython_function_address("scipy.special.cython_special", 'ndtri')
normppf = functype(ppf_addr)

log_cdf_addr = get_cython_function_address("scipy.special.cython_special", "__pyx_fuse_1log_ndtr")
log_normcdf = functype(log_cdf_addr)

def get_seed_points(alphas, seeds):
    """Seed points for the generation of r-sequences.
    
    The r-sequences are seeded with a real number between 0 and 1.
    This seed is used to calculate a first point. All other points
    can be calculated recursively from the first point. We call
        # necessary when discrete variables are define on integers
        x = np.asarray(x, dtype=float)
        from pyapprox.cython.orthonormal_polynomials_1d import \
            evaluate_orthonormal_polynomial_deriv_1d_pyx
        return evaluate_orthonormal_polynomial_deriv_1d_pyx(
            x, nmax, ab, deriv_order)
    except:
        print('evaluate_orthonormal_polynomial_deriv_1d_pyx extension failed')
    return __evaluate_orthonormal_polynomial_deriv_1d


_PTR = ctypes.POINTER
_dble = ctypes.c_double
_ptr_dble = _PTR(_dble)

addr = get_cython_function_address("scipy.special.cython_special", "gammaln")
functype = ctypes.CFUNCTYPE(_dble, _dble)
gammaln_float64 = functype(addr)


@njit
def numba_gammaln(x):
    return gammaln_float64(x)


@jit(nopython=True)
def __evaluate_orthonormal_polynomial_1d(x, nmax, ab):
    r""" 
    Evaluate univariate orthonormal polynomials using their
    three-term recurrence coefficients.
Beispiel #24
0
import numba.extending as nbe
import ctypes
import numpy as np
from numba import njit

dpotrf_addr = nbe.get_cython_function_address(
    'scipy.linalg.cython_lapack', 'dpotrf')  # cholesky decomposition
dtrsv_addr = nbe.get_cython_function_address('scipy.linalg.cython_blas',
                                             'dtrsv')  # solve Ax=b

dpotrf_cfunc = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p,
                                ctypes.c_void_p, ctypes.c_void_p,
                                ctypes.c_void_p)

dtrsv_cfunc = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p,
                               ctypes.c_void_p, ctypes.c_void_p,
                               ctypes.c_void_p, ctypes.c_void_p,
                               ctypes.c_void_p, ctypes.c_void_p)

dpotrf_fun = dpotrf_cfunc(dpotrf_addr)
dtrsv_fun = dtrsv_cfunc(dtrsv_addr)


@njit
def lapack_solve_triangular(Lalloc, balloc, N):
    #solves the linalg equation A*x=L*L**T*x=b, where L is the lower cholesky decomposed matrix of A

    side_a = np.empty(1, dtype=np.int32)
    side_a[0] = 76  # L
    t_a = np.empty(1, dtype=np.int32)
    t_a[0] = 78  # N - solve L*x=b and not L**T*x=b
Beispiel #25
0
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import numpy as np
import ctypes
from numba import jit, cfunc, vectorize
from numba.extending import get_cython_function_address
from scipy.integrate import quad
from scipy.interpolate import interp1d
"""
Constants and utility functions.
"""

# Set up the gamma function. It needs to be defined this way since
# get_cython_function_address doesn't work with gamma, likely because gamma
# involves complex numbers.
addr_gs = get_cython_function_address("scipy.special.cython_special",
                                      "gammasgn")
functype_gs = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double)
gammasgn_cs = functype_gs(addr_gs)

addr_gln = get_cython_function_address("scipy.special.cython_special",
                                       "gammaln")
functype_gln = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double)
gammaln_cs = functype_gln(addr_gln)

## Required to handle case where first argument to incomplete gamma function is 0.
# Can fix by finding the mangled function name in:
# >>> from scipy.special.cython_special import __pyx_capi__
# >>> __pyx_capi__
# addr_expi = get_cython_function_address("scipy.special.cython_special", "expi")
# functype_expi = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double)
# expi_cs = functype_expi(addr_expi)
Beispiel #26
0
 def test_getting_function(self):
     addr = get_cython_function_address("scipy.special.cython_special", "j0")
     functype = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double)
     _j0 = functype(addr)
     j0 = jit(nopython=True)(lambda x: _j0(x))
     self.assertEqual(j0(0), 1)
Beispiel #27
0
        'ellipkinc': [(
            float64,
            float64,
        )],
        'ellipeinc': [(
            float64,
            float64,
        )],
        'erf': [(float64, )],
    }

    name_and_types_to_pointer = {
        ('expi', float64):
        ctypes.CFUNCTYPE(ctypes.c_double,
                         ctypes.c_double)(get_cython_function_address(
                             'scipy.special.cython_special',
                             '__pyx_fuse_1expi')),
        ('ellipe', float64):
        ctypes.CFUNCTYPE(ctypes.c_double,
                         ctypes.c_double)(get_cython_function_address(
                             'scipy.special.cython_special', 'ellipe')),
        ('iv', float64, float64):
        ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double,
                         ctypes.c_double)(get_cython_function_address(
                             'scipy.special.cython_special',
                             '__pyx_fuse_1iv')),
        ('gamma', float64):
        ctypes.CFUNCTYPE(ctypes.c_double,
                         ctypes.c_double)(get_cython_function_address(
                             'scipy.special.cython_special',
                             '__pyx_fuse_1gamma')),
Beispiel #28
0
 def test_missing_function(self):
     with self.assertRaises(ValueError) as raises:
         addr = get_cython_function_address("scipy.special.cython_special", "foo")
     msg = "No function 'foo' found in __pyx_capi__ of 'scipy.special.cython_special'"
     self.assertEqual(msg, str(raises.exception))
from numba.extending import get_cython_function_address
from numba import cfunc, jit
from numba.types import float64, CPointer, intc
from scipy import LowLevelCallable
from scipy.integrate import quad
from scipy.optimize import brentq

from dm_params import fx, sv
from utilities import e0, b0, D0, delta, kpc_to_cm, speed_of_light, D
from utilities import rho_critical, GeV_to_m_sun
from utilities import e_high_excess

# Obtained by finding the mangled function name in:
# >>> from scipy.special.cython_special import __pyx_capi__
# >>> __pyx_capi__
addr_hyp = get_cython_function_address("scipy.special.cython_special",
                                       "__pyx_fuse_1hyp2f1")
functype_hyp = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double,
                                ctypes.c_double, ctypes.c_double,
                                ctypes.c_double)
hyp2f1_cs = functype_hyp(addr_hyp)


@np.vectorize
def rho(dist, rs, rhos, gamma):
    return rhos * (rs / np.abs(dist))**gamma * (1. + np.abs(dist) / rs)**(
        gamma - 3.)


@np.vectorize
def mass(rs, rhos, gamma):
    # Find virial mass numerically. The "magic number" of 10000 should
Beispiel #30
0
    def __init__(
        self,
        orig_typ,
        addr_func,
        element_type,
        addr_func_name=None,
        override_module_name=None,
    ):
        super().__init__(orig_typ, addr_func, addr_func_name,
                         override_module_name)

        assert self.type_name.endswith("Array")
        element_type_name = self.type_name[:-5]

        addr = get_cython_function_address(self.override_module_name,
                                           "Array_length")
        Array_length = ctypes.CFUNCTYPE(ctypes.c_uint64, ctypes.c_voidp)(addr)

        @overload(len)
        def overload_len(v):
            if isinstance(v, self.Type):

                def impl_(v):
                    return Array_length(v.ptr)

                return impl_
            return None

        _ = overload_len

        addr = get_cython_function_address(self.override_module_name,
                                           "Array_is_valid")
        Array_is_valid = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_voidp,
                                          ctypes.c_uint64)(addr)

        @overload_method(self.Type, "is_valid")
        def overload_is_valid(v, i):
            _ = v
            _ = i

            def impl_(v, i):
                return Array_is_valid(v.ptr, i)

            return impl_

        _ = overload_is_valid

        @overload_method(self.Type, "is_null")
        def overload_is_null(v, i):
            _ = v
            _ = i

            def impl_(v, i):
                return not Array_is_valid(v.ptr, i)

            return impl_

        _ = overload_is_null

        @overload_method(self.Type, "indicies")
        def overload_indicies(v):
            _ = v

            def impl_(v):
                for i in range(Array_length(v.ptr)):
                    if v.is_valid(i):
                        yield i

            return impl_

        _ = overload_indicies

        addr = get_cython_function_address(
            self.override_module_name,
            "Array_" + element_type_name + "Array_Value")
        Array_xArray_Value = ctypes.CFUNCTYPE(element_type, ctypes.c_voidp,
                                              ctypes.c_uint64)(addr)

        @overload(operator.getitem)
        def overload_getitem(v, i):
            _ = i
            if isinstance(v, self.Type):

                def impl_(v, i):
                    return Array_xArray_Value(v.ptr, i)

                return impl_
            return None

        _ = overload_getitem

        @overload_method(self.Type, "values")
        def overload_values(v):
            _ = v

            def impl_(v):
                for i in range(Array_length(v.ptr)):
                    if v.is_valid(i):
                        yield Array_xArray_Value(v.ptr, i)

            return impl_

        _ = overload_values
Beispiel #31
0
    out = (cos(2*alp)- 1/(1+x)) / (kap - beta * (1+x) * sin(2*alp))    
    
    # Add SC term
    # out += -1 / (  (gamma**2-1)*(1+x)*(kap - beta*(1+x)*sin(2*alp))  )    
        
    return out




# Include special functions for Numba
#
# Tip from: https://github.com/numba/numba/issues/3086
# and http://numba.pydata.org/numba-doc/latest/extending/high-level.html
#
addr1 = get_cython_function_address('scipy.special.cython_special', 'ellipkinc')
addr2 = get_cython_function_address('scipy.special.cython_special', 'ellipeinc')
functype = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double, ctypes.c_double)
my_ellipkinc = functype(addr1)
my_ellipeinc = functype(addr2)




@vectorize([float64(float64, float64, float64)])
def psi_x(z, x, gamma):
    """
    2D longitudinal potential ( "psi_phi" term excluded ) 
    
    Numba vectorized    
Beispiel #32
0
import ctypes
import numba
from numba.extending import get_cython_function_address
import numpy as np

import pyximport

pyximport.install()
import wofz_cython

_PTR = ctypes.POINTER
_dble = ctypes.c_double
_ptr_dble = _PTR(_dble)

addr = get_cython_function_address("wofz_cython", "wofz")
functype = ctypes.CFUNCTYPE(None, _dble, _dble, _ptr_dble, _ptr_dble)
wofz_fn = functype(addr)


@numba.njit
def wofz(z):
    w_real = np.empty(1, dtype=np.float64)
    w_imag = np.empty(1, dtype=np.float64)
    wofz_fn(np.real(z), np.imag(z), w_real.ctypes, w_imag.ctypes)
    return np.complex(w_real[0] + 1j * w_imag[0])
Beispiel #33
0
        A += np.sum(_A_ij(sample1[i], sample2))
    return A / (N1 * N2)


@jit("float32(float32[:], float32[:])", nopython=True, parallel=True)
def KS_statistic(sample1, sample2):
    sample_both = np.concatenate((sample1, sample2))
    cdf1 = np.searchsorted(sample1, sample_both, 'right') / len(sample1)
    cdf2 = np.searchsorted(sample2, sample_both, 'right') / len(sample2)
    ks = np.max(np.abs(cdf1 - cdf2))
    return ks


## Standard normal distribution

_addr_ndtri = get_cython_function_address("scipy.special.cython_special",
                                          "ndtri")
_functype_ndtri = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double)
normal_ppf = _functype_ndtri(_addr_ndtri)

_addr_ndtr = get_cython_function_address("scipy.special.cython_special",
                                         "__pyx_fuse_0ndtr")
_functype_ndtr = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double)
normal_cdf = _functype_ndtr(_addr_ndtr)

## Quantile-Quantile plot


def calculate_QQplot(data1, data2, a=0):
    def _sample_quantiles(data):
        probplot = gofplots.ProbPlot(np.array(data, dtype=float), a=a)
        return probplot.sample_quantiles