示例#1
0
def _euclidean_distance_map(a: ArrayLike, b: ArrayLike,
                            out: ArrayLike) -> None:  # pragma: no cover.
    """Helper function for the map step of euclidean distance which runs on
    the device (GPU) itself.

    Parameters
    ----------
    a
        [array-like, shape: (1, n)]
    b
        [array-like, shape: (1, n)]
    out
        [array-like, shape: (1)]
        The output array for returning the result.

    Returns
    -------
    An ndarray, which contains the squared sum of the corresponding elements
    of the given pair of vectors.
    """
    square_sum = types.float32(0)

    zero = types.uint32(0)
    a_shape_0 = types.uint32(a.shape[types.uint32(0)])
    i = types.uint32(0)

    while i < a_shape_0:
        if a[i] >= zero and b[i] >= zero:
            square_sum += (a[i] - b[i])**types.uint32(2)
        i = types.uint32(i + types.uint32(1))
    out[0] = square_sum
示例#2
0
def _correlation(x: ArrayLike, y: ArrayLike,
                 out: ArrayLike) -> None:  # pragma: no cover.
    # Note: assigning variable and only saving the final value in the
    # array made this significantly faster.

    # aggressively making all variables explicitly typed
    # makes it more performant by a factor of ~2-3x
    v0 = types.float32(0)
    v1 = types.float32(0)
    v2 = types.float32(0)
    v3 = types.float32(0)
    v4 = types.float32(0)
    v5 = types.float32(0)

    m = types.uint32(x.shape[types.uint32(0)])
    i = types.uint32(0)

    zero = types.uint32(0)

    while i < m:
        if x[i] >= zero and y[i] >= zero:
            v0 += x[i]
            v1 += y[i]
            v2 += x[i] * x[i]
            v3 += y[i] * y[i]
            v4 += x[i] * y[i]
            v5 += 1
        i = types.uint32(i + types.uint32(1))

    out[0] = v0
    out[1] = v1
    out[2] = v2
    out[3] = v3
    out[4] = v4
    out[5] = v5
示例#3
0
import math
import warnings

from numba.targets.imputils import Registry
from numba import types
from numba.itanium_mangler import mangle
from .hsaimpl import _declare_function

registry = Registry()
lower = registry.lower

# -----------------------------------------------------------------------------

_unary_b_f = types.int32(types.float32)
_unary_b_d = types.int32(types.float64)
_unary_f_f = types.float32(types.float32)
_unary_d_d = types.float64(types.float64)
_binary_f_ff = types.float32(types.float32, types.float32)
_binary_d_dd = types.float64(types.float64, types.float64)

function_descriptors = {
    'isnan': (_unary_b_f, _unary_b_d),
    'isinf': (_unary_b_f, _unary_b_d),

    'ceil': (_unary_f_f, _unary_d_d),
    'floor': (_unary_f_f, _unary_d_d),

    'fabs': (_unary_f_f, _unary_d_d),

    'sqrt': (_unary_f_f, _unary_d_d),
    'exp': (_unary_f_f, _unary_d_d),
示例#4
0
import math
import warnings

from numba.targets.imputils import implement, Registry
from numba import types
from numba.itanium_mangler import mangle
from .hsaimpl import _declare_function

registry = Registry()
register = registry.register

# -----------------------------------------------------------------------------

_unary_b_f = types.int32(types.float32)
_unary_b_d = types.int32(types.float64)
_unary_f_f = types.float32(types.float32)
_unary_d_d = types.float64(types.float64)
_binary_f_ff = types.float32(types.float32, types.float32)
_binary_d_dd = types.float64(types.float64, types.float64)

function_descriptors = {
    'isnan': (_unary_b_f, _unary_b_d),
    'isinf': (_unary_b_f, _unary_b_d),
    'ceil': (_unary_f_f, _unary_d_d),
    'floor': (_unary_f_f, _unary_d_d),
    'fabs': (_unary_f_f, _unary_d_d),
    'sqrt': (_unary_f_f, _unary_d_d),
    'exp': (_unary_f_f, _unary_d_d),
    'expm1': (_unary_f_f, _unary_d_d),
    'log': (_unary_f_f, _unary_d_d),
    'log10': (_unary_f_f, _unary_d_d),
示例#5
0
        cycle_edges_mask, cost_inside_cycle):

    if not cycle_vertices_mask[internal_vertex]:
        cost_inside_cycle -= vertex_costs[internal_vertex]

    cycle_vertices_mask[internal_vertex] = True

    if tree_edges_mask[first_internal_edge_index]:
        cycle_edges_mask[first_internal_edge_index] = True
        return cost_inside_cycle, second_internal_edge_index, internal_vertex
    else:
        cycle_edges_mask[second_internal_edge_index] = True
        return cost_inside_cycle, first_internal_edge_index, non_tree_edge_primary_vertex


@jit(float32(planar_graph_nb_type, int32[:], boolean[:], boolean[:], int32[:],
             int32),
     nopython=True)
def _add_tree_path_to_cycle_masks_and_return_its_cost(
        graph, parent_edge_indices, cycle_vertices_mask, cycle_edges_mask,
        next_edge_indices_in_path_to_cycle, internal_vertex):

    tree_path_cost = 0.0

    current_vertex = internal_vertex

    while not cycle_vertices_mask[current_vertex]:

        cycle_vertices_mask[current_vertex] = True
        tree_path_cost += graph.vertex_costs[current_vertex]

        if next_edge_indices_in_path_to_cycle[current_vertex] == -1:
示例#6
0
def __subset_hydroTable_to_forecast(hydroTable,forecast,subset_hucs=None):

    if isinstance(hydroTable,str):
        hydroTable = pd.read_csv(
                                 hydroTable,
                                 dtype={'HUC':str,'feature_id':str,
                                         'HydroID':str,'stage':float,
                                         'discharge_cms':float,'LakeID' : int}
                                )
        hydroTable.set_index(['HUC','feature_id','HydroID'],inplace=True)
    elif isinstance(hydroTable,pd.DataFrame):
        pass #consider checking for correct dtypes, indices, and columns
    else:
        raise TypeError("Pass path to hydro-table csv or Pandas DataFrame")

    if isinstance(forecast,str):
        forecast = pd.read_csv(
                               forecast,
                               dtype={'feature_id' : str , 'discharge' : float}
                              )
        forecast.set_index('feature_id',inplace=True)
    elif isinstance(forecast,pd.DataFrame):
        pass # consider checking for dtypes, indices, and columns
    else:
        raise TypeError("Pass path to forecast file csv or Pandas DataFrame")


    # susbset hucs if passed
    if subset_hucs is not None:
        if isinstance(subset_hucs,list):
            if len(subset_hucs) == 1:
                try:
                    subset_hucs = open(subset_hucs[0]).read().split('\n')
                except FileNotFoundError:
                    pass
        elif isinstance(subset_hucs,str):
                try:
                    subset_hucs = open(subset_hucs).read().split('\n')
                except FileNotFoundError:
                    subset_hucs = [subset_hucs]

        # subsets HUCS
        subset_hucs_orig = subset_hucs.copy() ; subset_hucs = []
        for huc in np.unique(hydroTable.index.get_level_values('HUC')):
            for sh in subset_hucs_orig:
                if huc.startswith(sh):
                    subset_hucs += [huc]

        hydroTable = hydroTable[np.in1d(hydroTable.index.get_level_values('HUC'), subset_hucs)]

    # join tables
    hydroTable = hydroTable.join(forecast,on=['feature_id'],how='inner')

    # initialize dictionary
    catchmentStagesDict = typed.Dict.empty(types.int32,types.float64)

    # interpolate stages
    for hid,sub_table in hydroTable.groupby(level='HydroID'):

        interpolated_stage = np.interp(sub_table.loc[:,'discharge'].unique(),sub_table.loc[:,'discharge_cms'],sub_table.loc[:,'stage'])

        # add this interpolated stage to catchment stages dict
        h = round(interpolated_stage[0],4)

        hid = types.int32(hid) ; h = types.float32(h)
        catchmentStagesDict[hid] = h

    # huc set
    hucSet = [str(i) for i in hydroTable.index.get_level_values('HUC').unique().to_list()]

    return(catchmentStagesDict,hucSet)
示例#7
0
    -------
    No test is performed here for efficiency: distribution must contain non-
    negative values that sum to one.
    """
    # Notes
    U = uniform(0.0, 1.0)
    cumsum = 0.0
    size = distribution.size
    for j in range(size):
        cumsum += distribution[j]
        if U <= cumsum:
            return j
    return size - 1


@njit(float32(float32, float32))
def log_sum_2_exp(a, b):
    """Computation of log( (e^a + e^b) / 2) in an overflow-proof way

    Parameters
    ----------
    a : `float32`
        First number

    b : `float32`
        Second number

    Returns
    -------
    output : `float32`
        Value of log( (e^a + e^b) / 2) for the given a and b
示例#8
0
# Authors: Stephane Gaiffas <*****@*****.**>
# License: GPL 3.0

from math import log
from random import expovariate

from numba import njit
from numba.types import float32, boolean, uint32, uint8, void, Tuple

from .tree import TreeClassifier
from .utils import log_sum_2_exp

# TODO: write all the docstrings


@njit(float32(TreeClassifier.class_type.instance_type, uint32, uint32))
def node_score(tree, node, idx_class):
    """Computes the score of the node

    Parameters
    ----------
    tree : `TreeClassifier`
        The tree containing the node

    node : `uint32`
        The index of the node in the tree

    idx_class : `uint32`
        Class index for which we want the score

    Returns
示例#9
0
# Authors: Stephane Gaiffas <*****@*****.**>
# License: BSD 3 clause
import numpy as np
from numpy import sin, sign, pi, abs, sqrt
import matplotlib.pyplot as plt
from numba import vectorize
from numba.types import float32, float64

# Examples of signals originally made by David L. Donoho.


@vectorize([float32(float32), float64(float64)], nopython=True)
def heavisine(x):
    """Computes the "heavisine" signal.

    Parameters
    ----------
    x : `numpy.array`, shape=(n_samples,)
        Inputs values

    Returns
    -------
    output : `numpy.array`, shape=(n_samples,)
        The value of the signal at given inputs

    Notes
    -----
    Inputs are supposed to belong to [0, 1] and must have dtype `float32` or `float64`

    """
    return 4 * sin(4 * pi * x) - sign(x - 0.3) - sign(0.72 - x)
示例#10
0
##################################
# Useful collections of signatures
##################################
_unary_bool = [nt.boolean(nt.boolean)]
_unary_int = [
    nt.uint8(nt.uint8),
    nt.int8(nt.int8),
    nt.uint16(nt.uint16),
    nt.int16(nt.int16),
    nt.uint32(nt.uint32),
    nt.int32(nt.int32),
    nt.uint64(nt.uint64),
    nt.int64(nt.int64)
]
_unary_float = [nt.float32(nt.float32), nt.float64(nt.float64)]
_unary_all = _unary_bool + _unary_int + _unary_float

_binary_bool = [nt.boolean(nt.boolean, nt.boolean)]
_binary_int = [
    nt.uint8(nt.uint8, nt.uint8),
    nt.int8(nt.int8, nt.int8),
    nt.uint16(nt.uint16, nt.uint16),
    nt.int16(nt.int16, nt.int16),
    nt.uint32(nt.uint32, nt.uint32),
    nt.int32(nt.int32, nt.int32),
    nt.uint64(nt.uint64, nt.uint64),
    nt.int64(nt.int64, nt.int64)
]
_binary_float = [
    nt.float32(nt.float32, nt.float32),