Exemple #1
0
 def operator_truediv(size):
     a = Array(size, 'int32')
     b = Array(size, 'int32')
     for i in range(size):
         a[i] = nb_types.int32(i+10)
         b[i] = nb_types.int32(i+3)
     return operator.truediv(a, b)
Exemple #2
0
 def operator_and_bw(size):
     a = Array(size, 'int32')
     b = Array(size, 'int32')
     for i in range(size):
         a[i] = nb_types.int32(i)
         b[i] = nb_types.int32(size-i-1)
     return operator.and_(a, b)
Exemple #3
0
 def operator_mod(size):
     a = Array(size, 'int32')
     b = Array(size, 'int32')
     for i in range(size):
         a[i] = nb_types.int32(i * 123)
         b[i] = nb_types.int32(7)
     return operator.mod(a, b)
Exemple #4
0
 def operator_rshift(size):
     a = Array(size, 'int32')
     b = Array(size, 'int32')
     for i in range(size):
         a[i] = nb_types.int32(i)
         b[i] = nb_types.int32(size-i-1)
     return operator.rshift(a, b)
Exemple #5
0
 def operator_pow(size):
     a = Array(size, 'int32')
     b = Array(size, 'int32')
     for i in range(size):
         a[i] = nb_types.int32(i+1)
         b[i] = nb_types.int32(size-i)
     return operator.pow(a, b)
Exemple #6
0
 def operator_iadd(size):
     a = Array(size, 'int32')
     b = Array(size, 'int32')
     for i in range(size):
         a[i] = nb_types.int32(i)
         b[i] = nb_types.int32(1)
     operator.iadd(a, b)
     return a
Exemple #7
0
 def operator_ifloordiv(size):
     a = Array(size, 'int32')
     b = Array(size, 'int32')
     for i in range(size):
         a[i] = nb_types.int32(i+10)
         b[i] = nb_types.int32(i+3)
     operator.ifloordiv(a, b)
     return a
Exemple #8
0
 def operator_ixor(size):
     a = Array(size, 'int32')
     b = Array(size, 'int32')
     for i in range(size):
         a[i] = nb_types.int32(i)
         b[i] = nb_types.int32(size-i-1)
     operator.ixor(a, b)
     return a
Exemple #9
0
def nearest_grd_indice(x, y, x0, y0, xstep, ystep):
    """
    Get nearest grid index from a position.

    :param x: longitude
    :param y: latitude
    :param float x0: first grid longitude
    :param float y0: first grid latitude
    :param float xstep: step between two longitude
    :param float ystep: step between two latitude
    """
    return (
        numba_types.int32(round(((x - x0[0]) % 360.0) / xstep)),
        numba_types.int32(round((y - y0[0]) / ystep)),
    )
Exemple #10
0
def get_refcount(typingctx, obj):
    """Get the current refcount of an object.

    FIXME: only handles the first object
    """
    def codegen(context, builder, signature, args):
        [obj] = args
        [ty] = signature.args
        # A sequence of (type, meminfo)
        meminfos = []
        if context.enable_nrt:
            tmp_mis = context.nrt.get_meminfos(builder, ty, obj)
            meminfos.extend(tmp_mis)
        refcounts = []
        if meminfos:
            for ty, mi in meminfos:
                miptr = builder.bitcast(mi, _meminfo_struct_type.as_pointer())
                refctptr = cgutils.gep_inbounds(builder, miptr, 0, 0)
                refct = builder.load(refctptr)
                refct_32bit = builder.trunc(refct, ir.IntType(32))
                refcounts.append(refct_32bit)
        return refcounts[0]

    sig = types.int32(obj)
    return sig, codegen
Exemple #11
0
        def non_void_func(typingctx, a):
            sig = types.int32(types.int32)

            def codegen(context, builder, signature, args):
                pass  # oops, should be returning a value here, raise exception

            return sig, codegen
Exemple #12
0
def _runlength_encode_mask_to_ranges(mask):
    """
    Helper function for runlength_encode_mask_to_ranges(), above.

    Args:
        mask:
            binary volume, 3D

    Returns:
        Flattened runlength encoding, as a numba typed.List:

            [Z,Y,X1,X2,Z,Y,X1,X2,Z,Y,X1,X2,...]

        Must be converted to np.array and un-flattened by the caller.
        Uses DVID conventions for the ranges, i.e. X1,X2 is inclusive,
        not one-past-the-end.
    """
    runs = List.empty_list(item_type=int32)

    Z, Y, X = mask.shape
    for z in np.arange(Z, dtype=np.int32):
        for y in np.arange(Y, dtype=np.int32):
            x0 = x1 = int32(0)
            in_run = False
            for x in np.arange(X, dtype=np.int32):
                if not in_run and mask[z,y,x]:
                    x0 = x
                    in_run = True
                elif in_run and not mask[z,y,x]:
                    x1 = np.int32(x-1)
                    in_run = False
                    runs.extend([z, y, x0, x1])

            if in_run:
                x1 = np.int32(X-1)
                runs.extend([z, y, x0, x1])

    return runs
Exemple #13
0
 def operator_abs(size):
     a = Array(size, 'int32')
     for i in range(size):
         a[i] = nb_types.int32(-i)
     return abs(a)
Exemple #14
0
 def operator_not_in(size, v):
     a = Array(size, 'int32')
     for i in range(size):
         a[i] = nb_types.int32(i)
     return v not in a
Exemple #15
0
 def operator_neg(size):
     a = Array(size, 'int32')
     for i in range(size):
         a[i] = nb_types.int32(i)
     return operator.neg(a)
Exemple #16
0
 def operator_ne_array(size, v):
     a = Array(size, 'int32')
     for i in range(size):
         a[i] = nb_types.int32(i)
     return a != a
Exemple #17
0

GrB_UnaryOp = OpContainer()
GrB_BinaryOp = OpContainer()

##################################
# 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),
Exemple #18
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)
Exemple #19
0
from __future__ import print_function, absolute_import, division
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),
Exemple #20
0
                if no_vertices_overlapped_yet:
                    no_vertices_overlapped_yet = False
                else:
                    cycle_vertices_mask[current_vertex] = False
                    next_edge_indices_in_path_to_cycle[
                        current_vertex] = current_edge_index

            else:
                cycle_vertices_mask[current_vertex] = True

            current_edge_index = parent_edge_indices[current_vertex]

    return cycle_vertices_mask, cycle_edges_mask, next_edge_indices_in_path_to_cycle


@jit(int32(int32, planar_graph_nb_type, boolean[:]), nopython=True)
def _get_cycle_vertex(edge_index, graph, cycle_vertices_mask):

    edge_vertex1 = graph.edges.vertex1[edge_index]
    edge_vertex2 = graph.edges.vertex2[edge_index]

    if cycle_vertices_mask[edge_vertex1]:
        return edge_vertex1

    return edge_vertex2


@jit(void(planar_graph_nb_type, boolean[:], float32[:], int32[:], int32, int32,
          boolean[:], boolean[:], boolean),
     nopython=True)
def iterate_tree_adjacency_costs_on_tree_cycle_side(
Exemple #21
0
def float_to_int(x):
    return types.int32(x)
Exemple #22
0

@jit(int32[:](int32[:], int32), nopython=True)
def _mark_separation_when_components_less_than_two_thirds(
        connected_component_indices, max_component_index):

    separation = utils.repeat_int(separation_class.SECOND_PART,
                                  len(connected_component_indices))

    separation[connected_component_indices ==
               max_component_index] = separation_class.FIRST_PART

    return separation


@jit(int32(float32[:]), nopython=True)
def _find_level_one(bfs_level_costs):

    level_one = 0
    cost_up_to_level_one = bfs_level_costs[level_one]

    while cost_up_to_level_one <= 1 / 2:
        level_one += 1
        cost_up_to_level_one += bfs_level_costs[level_one]

    return level_one


@jit(int32(int32[:], int32, int32), nopython=True)
def _find_level_zero(bfs_level_sizes, level_one,
                     vertices_count_up_to_level_one):
Exemple #23
0
def float_to_int(x):
    return types.int32(x)
Exemple #24
0
    inds = hpat.distributed_api.gatherv(indices)
    if rank == 0:
        all_indices = inds
    else:
        all_indices = np.empty(n, indices.dtype)
    hpat.distributed_api.bcast(all_indices)

    start = hpat.distributed_api.get_start(n, n_pes, rank)
    end = hpat.distributed_api.get_end(n, n_pes, rank)
    return all_indices[start:end]

@intrinsic
def tuple_to_ptr(typingctx, tuple_tp=None):
    def codegen(context, builder, sig, args):
        ptr = cgutils.alloca_once(builder, args[0].type)
        builder.store(args[0], ptr)
        return builder.bitcast(ptr, lir.IntType(8).as_pointer())
    return signature(types.voidptr, tuple_tp), codegen

_h5read_filter = types.ExternalFunction("hpat_h5_read_filter",
    types.int32(h5dataset_or_group_type, types.int32, types.voidptr,
    types.voidptr, types.intp, types.voidptr, types.int32, types.voidptr, types.int32))

@numba.njit
def h5read_filter(dset_id, ndim, starts, counts, is_parallel, out_arr, read_indices):
    starts_ptr = tuple_to_ptr(starts)
    counts_ptr = tuple_to_ptr(counts)
    type_enum = hpat.distributed_api.get_type_enum(out_arr)
    return _h5read_filter(dset_id, ndim, starts_ptr, counts_ptr, is_parallel,
                   out_arr.ctypes, type_enum, read_indices.ctypes, len(read_indices))
Exemple #25
0
def detect_local_minima_(grid, general_mask, pixel_mask,
                         maximum_local_extremum, sign):
    """
    Take an array and detect the troughs using the local maximum filter.
    Returns a boolean mask of the troughs (i.e., 1 when
    the pixel's value is the neighborhood maximum, 0 otherwise)
    http://stackoverflow.com/questions/3684484/peak-detection-in-a-2d-array/3689710#3689710
    """
    nb_x, nb_y = grid.shape
    # init with fake value because numba need to type data in list
    xs, ys = [0], [0]
    xs.pop(0)
    ys.pop(0)
    g = empty((3, 3))
    for i in range(1, nb_x - 1):
        for j in range(1, nb_y - 1):
            # Copy footprint
            for i_ in range(-1, 2):
                for j_ in range(-1, 2):
                    if general_mask[i_ + i, j_ + j]:
                        g[i_ + 1, j_ + 1] = 2e10
                    else:
                        g[i_ + 1, j_ + 1] = grid[i_ + i, j_ + j] * sign
            # if center equal to min
            # TODO if center and neigboor have same value we have problem, i don't know how
            if g.min() == (grid[i, j] * sign) and pixel_mask[i, j]:
                xs.append(i)
                ys.append(j)
    nb_extrema = len(xs)

    # If several extrema we try to separate them
    if nb_extrema > maximum_local_extremum:
        # Group
        nb_group = 1
        gr = zeros(nb_extrema, dtype=numba_types.int16)
        for i0 in range(nb_extrema - 1):
            for i1 in range(i0 + 1, nb_extrema):
                if (abs(xs[i0] - xs[i1]) + abs(ys[i0] - ys[i1])) == 1:
                    if gr[i0] == 0 and gr[i1] == 0:
                        # Nobody was link with a known group
                        gr[i0] = nb_group
                        gr[i1] = nb_group
                        nb_group += 1
                    elif gr[i0] == 0 and gr[i1] != 0:
                        # i1 is link not i0
                        gr[i0] = gr[i1]
                    elif gr[i1] == 0 and gr[i0] != 0:
                        # i0 is link not i1
                        gr[i1] = gr[i0]
                    else:
                        # there already linked in two different group
                        # we replace group from i0 with group from i1
                        gr[gr == gr[i0]] = gr[i1]

        m = gr != 0
        grs = unique(gr[m])
        # all non grouped extremum
        # Numba work around
        xs_new, ys_new = [0], [0]
        xs_new.pop(0), ys_new.pop(0)
        for i in range(nb_extrema):
            if m[i]:
                continue
            xs_new.append(xs[i])
            ys_new.append(ys[i])
        for gr_ in grs:
            nb = 0
            x_mean = 0
            y_mean = 0
            # Choose barycentre of group
            for i in range(nb_extrema):
                if gr_ == gr[i]:
                    x_mean += xs[i]
                    y_mean += ys[i]
                    nb += 1
            x_mean /= nb
            y_mean /= nb

            xs_new.append(numba_types.int32(round(x_mean)))
            ys_new.append(numba_types.int32(round(y_mean)))
        return xs_new, ys_new
    return xs, ys
Exemple #26
0
from __future__ import print_function, absolute_import, division
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),
Exemple #27
0
    if n_burn == 0:
        cdk_copy = np.copy(cdk)
        ckn_copy = np.copy(ckn)
        to_store = Sample(cdk_copy, ckn_copy)
        samples.append(to_store)

    # store the last Z
    for (d, pos) in Z:
        Z[(d, pos)] = Z_mat[d, pos]

    all_lls = np.array(all_lls)
    return all_lls, samples


@jit(int32(int32, int32, int32, int32[:, :], int32[:], int32, int32, int32,
           float64[:], float64[:], float64, float64, float64[:], float64[:],
           float64, int32[:, :], int32[:], int32[:, :], int32[:]),
     nopython=True)
def _nb_get_new_index(d, n, k, cdk, cd, N, K, previous_K, alpha, beta, N_beta,
                      K_alpha, post, cumsum, random_number, ckn, ck,
                      previous_ckn, previous_ck):

    temp_ckn = ckn[:, n]
    temp_previous_ckn = previous_ckn[:, n]
    temp_cdk = cdk[d, :]

    # remove from model
    cdk[d, k] -= 1
    cd[d] -= 1
    ckn[k, n] -= 1
    ck[k] -= 1
Exemple #28
0
 def non_void_func(typingctx, a):
     sig = types.int32(types.int32)
     def codegen(context, builder, signature, args):
         pass # oops, should be returning a value here, raise exception
     return sig, codegen
Exemple #29
0
        cdk_copy = np.copy(cdk)
        ckn_copy = np.copy(ckn)
        to_store = Sample(cdk_copy, ckn_copy)
        samples.append(to_store)                                      

    # store the last Z
    for (d, pos) in Z:
        Z[(d, pos)] = Z_mat[d, pos]
            
    all_lls = np.array(all_lls)            
    return all_lls, samples

@jit(int32(
           int32, int32, int32, int32[:, :], int32[:], 
           int32, int32, int32, float64[:], float64[:],
           float64, float64,
           float64[:], float64[:], float64,
           int32[:, :], int32[:], int32[:, :], int32[:]
), nopython=True)
def _nb_get_new_index(d, n, k, cdk, cd, 
                      N, K, previous_K, alpha, beta, 
                      N_beta, K_alpha,                      
                      post, cumsum, random_number, 
                      ckn, ck, previous_ckn, previous_ck):

    temp_ckn = ckn[:, n]
    temp_previous_ckn = previous_ckn[:, n]
    temp_cdk = cdk[d, :]

    # remove from model
    cdk[d, k] -= 1