def contract(self): """Reshapes contraction coefficients into (nprim, ncont) array.""" x = 0 rect = np.empty((int64(self.nprim), int64(self.ncont))) for i in range(self.nprim): for j in range(self.ncont): rect[i, j] = self._coef[x] x += 1 return rect
def _meantsub_jit(data): """ Calculate mean in time (ignoring zeros) and subtract in place Could ultimately parallelize by computing only on subset of data. """ nint, nbl, nchan, npol = data.shape for i in range(nbl): for j in range(nchan): for k in range(npol): ss = complex64(0) weight = int64(0) for l in range(nint): ss += data[l, i, j, k] if data[l, i, j, k] != 0j: weight += 1 if weight > 0: mean = ss/weight else: mean = complex64(0) if mean: for l in range(nint): if data[l, i, j, k] != 0j: data[l, i, j, k] -= mean
def test_compute_signature(): s = symbol('s', 'int64') t = symbol('t', 'float32') d = symbol('d', 'datetime') assert compute_signature(s + t) == float64(int64, float32) assert (compute_signature(d.truncate(days=1)) == datetime64('D')(datetime64('us'))) assert compute_signature(d.day + 1) == int64(datetime64('us'))
def _make_findrfc(cmp1, cmp2): @jit(int64(int64[:], float64[:], float64), nopython=True) def findrfc2(t, y, h): # cmp1, cmp2 = (a_le_b, a_lt_b) if method==0 else (a_lt_b, a_le_b) n = len(y) j, t0, z0 = 0, 0, 0 y0 = y[t0] # The rainflow filter for ti in range(1, n): fpi = y0 + h fmi = y0 - h yi = y[ti] if z0 == 0: if cmp1(yi, fmi): z1 = -1 elif cmp1(fpi, yi): z1 = +1 else: z1 = 0 t1, y1 = (t0, y0) if z1 == 0 else (ti, yi) else: if (((z0 == +1) and cmp1(yi, fmi)) or ((z0 == -1) and cmp2(yi, fpi))): z1 = -1 elif (((z0 == +1) and cmp2(fmi, yi)) or ((z0 == -1) and cmp1(fpi, yi))): z1 = +1 else: raise ValueError # warnings.warn('Something wrong, i={}'.format(tim1)) # Update y1 if z1 != z0: t1, y1 = ti, yi elif z1 == -1: t1, y1 = (t0, y0) if y0 < yi else (ti, yi) elif z1 == +1: t1, y1 = (t0, y0) if y0 > yi else (ti, yi) # Update y if y0 is a turning point if abs(z0 - z1) == 2: j += 1 t[j] = t0 # Update t0, y0, z0 t0, y0, z0 = t1, y1, z1 # end # Update y if last y0 is greater than (or equal) threshold if cmp2(h, abs(y0 - y[t[j]])): j += 1 t[j] = t0 return j + 1 return findrfc2
def _square(vals): nbas = int64(np.round(np.roots(np.array([1, 1, -2 * vals.shape[0]]))[1])) square = np.empty((nbas, nbas), dtype=np.float64) cnt = 0 for i in range(nbas): for j in range(i + 1): square[i, j] = vals[cnt] square[j, i] = vals[cnt] cnt += 1 return square
def xoroshiro128p_uniform_float64(states, index): '''Return a float64 in range [0.0, 1.0) and advance ``states[index]``. :type states: 1D array, dtype=xoroshiro128p_dtype :param states: array of RNG states :type index: int64 :param index: offset in states to update :rtype: float64 ''' index = int64(index) return uint64_to_unit_float64(xoroshiro128p_next(states, index))
def _tri_indices(vals): nel = vals.shape[0] nbas = int64(np.round(np.roots(np.array([1, 1, -2 * vals.shape[0]]))[1])) chi0 = np.empty(nel, dtype=np.int64) chi1 = np.empty(nel, dtype=np.int64) cnt = 0 for i in range(nbas): for j in range(i + 1): chi0[cnt] = i chi1[cnt] = j cnt += 1 return chi0, chi1
def _dedisperseresample_jit(data, delay, dt, result): nint, nbl, nchan, npol = data.shape nintout = int64(len(result)) for j in range(nbl): for l in range(npol): for k in range(nchan): for i in range(nintout): weight = int64(0) for r in range(dt): iprime = int64(i*dt + delay[k] + r) val = data[iprime, j, k, l] result[i, j, k, l] += val if val != 0j: weight += 1 if weight > 0: result[i, j, k, l] = result[i, j, k, l]/weight else: result[i, j, k, l] = weight return result
def _resample_gu(data, dt): b""" Multicore resampling via numpy broadcasting. Requires that data be in nint axisto be last, so input visibility array must have view from "np.swapaxis(data, 0, 3)". *modifies original memory space* (unlike _resample_jit) """ if dt > 1: for i in range(data.shape[0]//dt): iprime = int64(i*dt) data[i] = data[iprime] for r in range(1, dt): data[i] += data[iprime+r] data[i] = data[i]/dt
def calc_ppr(indptr, indices, out_degree, alpha, eps): r"""Calculate the personalized PageRank vector for all nodes using a variant of the Andersen algorithm (see Andersen et al. :Local Graph Partitioning using PageRank Vectors.) Args: indptr (np.ndarray): Index pointer for the sparse matrix (CSR-format). indices (np.ndarray): Indices of the sparse matrix entries (CSR-format). out_degree (np.ndarray): Out-degree of each node. alpha (float): Alpha of the PageRank to calculate. eps (float): Threshold for PPR calculation stopping criterion (:obj:`edge_weight >= eps * out_degree`). :rtype: (:class:`List[List[int]]`, :class:`List[List[float]]`) """ alpha_eps = alpha * eps js = [[0]] * len(out_degree) vals = [[0.]] * len(out_degree) for inode_uint in numba.prange(len(out_degree)): inode = numba.int64(inode_uint) p = {inode: 0.0} r = {} r[inode] = alpha q = [inode] while len(q) > 0: unode = q.pop() res = r[unode] if unode in r else 0 if unode in p: p[unode] += res else: p[unode] = res r[unode] = 0 for vnode in indices[indptr[unode]:indptr[unode + 1]]: _val = (1 - alpha) * res / out_degree[unode] if vnode in r: r[vnode] += _val else: r[vnode] = _val res_vnode = r[vnode] if vnode in r else 0 if res_vnode >= alpha_eps * out_degree[vnode]: if vnode not in q: q.append(vnode) js[inode] = list(p.keys()) vals[inode] = list(p.values()) return js, vals
def _meantsub_gu(data): """ Subtract time mean while ignoring zeros. Vectorizes over time axis. Assumes time axis is last so use np.swapaxis(0,3) when passing visibility array in """ ss = complex64(0) weight = int64(0) for i in range(data.shape[0]): ss += data[i] if data[i] != 0j: weight += 1 mean = ss/weight for i in range(data.shape[0]): data[i] -= mean
def test_apply_function_in_function(self): def foo(f, f_inner): return f(f_inner) @cfunc('int64(float64)') def f_inner(i): return int64(i * 3) @cfunc(int64(types.FunctionType(f_inner._sig))) def f(f_inner): return f_inner(123.4) self.assertEqual( jit(nopython=True)(foo)(f, f_inner), foo(f._pyfunc, f_inner._pyfunc))
def get_ancestor_graph(self, n): edges = np.zeros_like(self.edges) edges_visited = np.zeros(len(self.edges), dtype=int64) n_visited = int64(0) ix = int64(0) dep_derivatives = np.zeros(len(self.edges), dtype=int64) ix, n_visited, deriv_dep = walk_parents(self.parent_edges, self.edges, n, edges, ix, edges_visited, n_visited, self.node_types, 0, dep_derivatives) edges = edges[:ix, :] edges[:ix, 3] = 1 nodes = np.zeros(2 * len(edges), dtype=int64) for i, e in enumerate(edges): nodes[2 * i] = e[0] nodes[2 * i + 1] = e[1] nodes = nodes[:i * 2 + 1 + 1] nodes = np.unique(nodes) return nodes, edges, dep_derivatives[:deriv_dep]
def _grid_visibilities_jit(data, u, v, w, npixx, npixy, uvres, grids): b""" Grid visibilities into rounded uv coordinates using jit on single core. Rounding not working here, so minor differences with original and guvectorized versions. """ nint, nbl, nchan, npol = data.shape # rounding not available in numba # ubl = np.round(us/uvres, 0).astype(np.int32) # vbl = np.round(vs/uvres, 0).astype(np.int32) for j in range(nbl): for k in range(nchan): ubl = int64(u[j, k]/uvres) vbl = int64(v[j, k]/uvres) if (np.abs(ubl < npixx//2)) and (np.abs(vbl < npixy//2)): umod = int64(np.mod(ubl, npixx)) vmod = int64(np.mod(vbl, npixy)) for i in range(nint): for l in range(npol): grids[i, umod, vmod] += data[i, j, k, l] return grids
def depth_first_search(node, path, children): children_of_node = children[node, :] for i in range(children_of_node[0]): c = children_of_node[i + 1] if len(path) > 0: if index(path, c) >= 0: return c, np.append(path, c) path_ = np.append(path, c) cyclic_dependency, cyclic_path = depth_first_search(c, path_, children) if cyclic_dependency >= 0: return cyclic_dependency, cyclic_path return int64(-1), np.zeros((0, ), dtype=int64)
def _meantsub_gu(data): b""" Subtract time mean while ignoring zeros. Vectorizes over time axis. Assumes time axis is last so use np.swapaxis(0,3) when passing visibility array in **CURRENTLY NOT WORKING WITH FLAGS** """ ss = complex64(0) weight = int64(0) for i in range(data.shape[0]): ss += data[i] if data[i] != complex64(0): weight += 1 mean = ss/weight for i in range(data.shape[0]): data[i] -= mean
def remove_old_data(self): """ Remove old data """ d_current = self.Simulated_current_time if self.Simulating else datetime.fromtimestamp( time()) moments = np.zeros(len(self.EventsDatabase), dtype=np.int64) for i, e in enumerate(self.EventsDatabase): moments[i] = e.Moment.timestamp() min_moment = int64( (d_current - timedelta(weeks=self.Keeping_Weeks)).timestamp()) out = np.zeros(len(moments), dtype=np.bool) AbnormalEventAnalyzer.remove_old_data_speedup(moments, min_moment, out) res = [] for i, evnt in enumerate(self.EventsDatabase): if out[i]: res.append(evnt) self.EventsDatabase = res
def generate_lasso_mask(image, selectedData): """ Generates a polygon mask using the given lasso coordinates :param selectedData: The raw coordinates selected from the data :return: The polygon mask generated from the given coordinate """ height = image.size[1] y_coords = selectedData["lassoPoints"]["y"] y_coords_corrected = [height - coord for coord in y_coords] # coordinates_tuple = list(zip(selectedData["lassoPoints"]["x"], y_coords_corrected)) # mask = Image.new("L", image.size) # draw = ImageDraw.Draw(mask) # draw.polygon(coordinates_tuple, fill=255) coordinates_tuple = list( zip(selectedData["lassoPoints"]["x"], y_coords_corrected)) pts = np.array(coordinates_tuple) # (1) Crop the bounding rect print(coordinates_tuple) @vectorize([int64(float64)]) def redondear(x): return x pts = redondear(pts) rect = cv2.boundingRect(pts) x, y, w, h = rect bite = np.array(image) croped = bite[y:y + h, x:x + w].copy() # (2) make mask pts = pts - pts.min(axis=0) mask = np.zeros(croped.shape[:2], np.uint8) cv2.drawContours(np.array(mask), [pts], -1, (255, 255, 255), -1, cv2.LINE_AA) # (3) do bit-op dst = cv2.bitwise_and(croped, croped, mask=mask) mask = Image.new("L", image.size) draw = ImageDraw.Draw(mask) draw.polygon(coordinates_tuple, fill=255) return mask
def test_ns_choose(self): """Functions are passed in via namespace scoping and called conditionally. """ def a(i): return i + 1 def b(i): return i + 2 def mkfoo(a_, b_): def foo(choose_left): if choose_left: r = a_(1) else: r = b_(2) return r return foo sig = int64(int64) for decor in [ mk_cfunc_func(sig), njit_func, mk_njit_with_sig_func(sig), mk_wap_func(sig), mk_ctypes_func(sig) ][:-1]: for jit_opts in [dict(nopython=True), dict(forceobj=True)]: jit_ = jit(**jit_opts) with self.subTest(decor=decor.__name__): a_ = decor(a) b_ = decor(b) self.assertEqual( jit_(mkfoo(a_, b_))(True), mkfoo(a, b)(True)) self.assertEqual( jit_(mkfoo(a_, b_))(False), mkfoo(a, b)(False)) self.assertNotEqual( jit_(mkfoo(a_, b_))(True), mkfoo(a, b)(False))
def xoroshiro128p_next(states, index): '''Return the next random uint64 and advance the RNG in states[index]. :type states: 1D array, dtype=xoroshiro128p_dtype :param states: array of RNG states :type index: int64 :param index: offset in states to update :rtype: uint64 ''' index = int64(index) s0 = states[index]['s0'] s1 = states[index]['s1'] result = s0 + s1 s1 ^= s0 states[index]['s0'] = uint64(rotl(s0, uint32(55))) ^ s1 ^ (s1 << uint32(14)) states[index]['s1'] = uint64(rotl(s1, uint32(36))) return result
def xoroshiro128p_normal_float64(states, index): '''Return a normally distributed float32 and advance ``states[index]``. The return value is drawn from a Gaussian of mean=0 and sigma=1 using the Box-Muller transform. This advances the RNG sequence by two steps. :type states: 1D array, dtype=xoroshiro128p_dtype :param states: array of RNG states :type index: int64 :param index: offset in states to update :rtype: float64 ''' index = int64(index) u1 = xoroshiro128p_uniform_float32(states, index) u2 = xoroshiro128p_uniform_float32(states, index) z0 = math.sqrt(-float64(2.0) * math.log(u1)) * math.cos(TWO_PI_FLOAT64 * u2) # discarding second normal value # z1 = math.sqrt(-float64(2.0) * math.log(u1)) * math.sin(TWO_PI_FLOAT64 * u2) return z0
def setpoint(x, y): def getcolor(col): if col * self.speed > self.nColors - 1: while (col * self.speed > 3071) and (self.speed > 3): self.speed -= 1 return self.color_list[self.nColors - 1] return self.color_list[int32(col * self.speed)] def coord1(x, y): # convert 2d -> 1d coord return x + y * self.sw def setscreen(coord, color): self.screen[coord] = color if x >= 0 < self.sw and y >= 0 < self.sh: coord = int64(coord1(x, y)) ic = self.icon[coord] setscreen(coord, getcolor(ic)) ic += 1 if ic > 12288: ic = 8192 self.icon[coord] = ic
def xoroshiro128p_jump(states, index): '''Advance the RNG in ``states[index]`` by 2**64 steps. :type states: 1D array, dtype=xoroshiro128p_dtype :param states: array of RNG states :type index: int64 :param index: offset in states to update ''' index = int64(index) s0 = uint64(0) s1 = uint64(0) for i in range(2): for b in range(64): if XOROSHIRO128P_JUMP[i] & (uint64(1) << uint32(b)): s0 ^= states[index]['s0'] s1 ^= states[index]['s1'] xoroshiro128p_next(states, index) states[index]['s0'] = s0 states[index]['s1'] = s1
def test_in__(self): """Function is passed in as an argument. """ def a(i): return i + 1 def foo(f): return 0 sig = int64(int64) for decor in [ mk_cfunc_func(sig), njit_func, mk_njit_with_sig_func(sig), mk_ctypes_func(sig), mk_wap_func(sig) ]: for jit_opts in [dict(nopython=True), dict(forceobj=True)]: jit_ = jit(**jit_opts) with self.subTest(decor=decor.__name__, jit=jit_opts): a_ = decor(a) self.assertEqual(jit_(foo)(a_), foo(a))
def test_in_call_out(self): """Function is passed in as an argument, called, and returned. """ def a(i): return i + 1 def foo(f): f(123) return f sig = int64(int64) for decor in [mk_cfunc_func(sig), njit_func, mk_njit_with_sig_func(sig), mk_wap_func(sig)]: for jit_opts in [dict(nopython=True), dict(forceobj=True)]: jit_ = jit(**jit_opts) with self.subTest(decor=decor.__name__): a_ = decor(a) r1 = jit_(foo)(a_).pyfunc r2 = foo(a) self.assertEqual(r1, r2)
def test_ns_out(self): """Function is passed in via namespace scoping and returned. """ def a(i): return i + 1 def mkfoo(a_): def foo(): return a_ return foo sig = int64(int64) for decor in [mk_cfunc_func(sig), njit_func, mk_njit_with_sig_func(sig), mk_wap_func(sig), mk_ctypes_func(sig)][:-1]: for jit_opts in [dict(nopython=True), dict(forceobj=True)]: jit_ = jit(**jit_opts) with self.subTest(decor=decor.__name__): a_ = decor(a) self.assertEqual(jit_(mkfoo(a_))().pyfunc, mkfoo(a)())
def init_xoroshiro128p_state(states, index, seed): '''Use SplitMix64 to generate an xoroshiro128p state from 64-bit seed. This ensures that manually set small seeds don't result in a predictable initial sequence from the random number generator. :type states: 1D array, dtype=xoroshiro128p_dtype :param states: array of RNG states :type index: uint64 :param index: offset in states to update :type seed: int64 :param seed: seed value to use when initializing state ''' index = int64(index) seed = uint64(seed) z = seed + uint64(0x9E3779B97F4A7C15) z = (z ^ (z >> uint32(30))) * uint64(0xBF58476D1CE4E5B9) z = (z ^ (z >> uint32(27))) * uint64(0x94D049BB133111EB) z = z ^ (z >> uint32(31)) states[index]['s0'] = z states[index]['s1'] = z
def test_in_choose_func_value(self): """Functions are passed in as arguments, selected conditionally and called. """ def a(i): return i + 1 def b(i): return i + 2 def foo(a, b, choose_left): if choose_left: f = a else: f = b return f(1) sig = int64(int64) for decor in [ mk_cfunc_func(sig), mk_wap_func(sig), njit_func, mk_njit_with_sig_func(sig), mk_ctypes_func(sig) ][:-1]: for jit_opts in [dict(nopython=True), dict(forceobj=True)]: jit_ = jit(**jit_opts) with self.subTest(decor=decor.__name__): a_ = decor(a) b_ = decor(b) self.assertEqual(jit_(foo)(a_, b_, True), foo(a, b, True)) self.assertEqual( jit_(foo)(a_, b_, False), foo(a, b, False)) self.assertNotEqual( jit_(foo)(a_, b_, True), foo(a, b, False))
def _cssub0_jit(data, dataavg): """ Use scipy calculate 4-pt mean as input to spline estimate. zeroed data is treated as flagged """ nint, nbl, nchan, npol = data.shape piece = nint // 4 for i in range(nbl): for j in range(nchan): for k in range(npol): # mean in each piece for pp in range(4): ss = complex64(0) weight = int64(0) for l in range(pp * piece, (pp + 1) * piece): ss += data[l, i, j, k] if data[l, i, j, k] != 0j: weight += 1 if weight > 0: dataavg[pp, i, j, k] = ss / weight else: dataavg[pp, i, j, k] = complex64(0) # TODO: instead use nearest?
def xoroshiro128p_jump(states, index): '''Advance the RNG in ``states[index]`` by 2**64 steps. :type states: 1D array, dtype=xoroshiro128p_dtype :param states: array of RNG states :type index: int64 :param index: offset in states to update ''' index = int64(index) jump = (uint64(0xbeac0467eba5facb), uint64(0xd86b048b86aa9922)) s0 = uint64(0) s1 = uint64(0) for i in range(2): for b in range(64): if jump[i] & (uint64(1) << uint32(b)): s0 ^= states[index]['s0'] s1 ^= states[index]['s1'] xoroshiro128p_next(states, index) states[index]['s0'] = s0 states[index]['s1'] = s1
def resample(data, dt, parallel=False): """ Resample (integrate) by factor dt and return new data structure wraps _resample to add logging. Can set mode to "single" or "multi" to use different functions. """ if not np.any(data): return np.array([]) len0 = data.shape[0] logger.info('Resampling data of length {0} by a factor of {1}' .format(len0, dt)) nint, nbl, nchan, npol = data.shape newsh = (int64(nint//dt), nbl, nchan, npol) if parallel: data = data.copy() _ = _resample_gu(np.swapaxes(data, 0, 3), dt) return data[:len0//dt] else: result = np.zeros(shape=newsh, dtype=data.dtype) _resample_jit(np.require(data, requirements='W'), dt, result) return result
def xoroshiro128p_jump(states, index): """Advance the RNG in ``states[index]`` by 2**64 steps. :type states: 1D array, dtype=xoroshiro128p_dtype :param states: array of RNG states :type index: int64 :param index: offset in states to update """ index = int64(index) jump = (uint64(0xBEAC0467EBA5FACB), uint64(0xD86B048B86AA9922)) s0 = uint64(0) s1 = uint64(0) for i in range(2): for b in range(64): if jump[i] & (uint64(1) << uint32(b)): s0 ^= states[index]["s0"] s1 ^= states[index]["s1"] xoroshiro128p_next(states, index) states[index]["s0"] = s0 states[index]["s1"] = s1
def test(x1, c1): #x1=x1[:2] #for i in x1: i = 0 e1 = x1[i] e2 = x1[i + 1] x2 = n1[e1] y2 = n1[e2] #return x2,y2 #a=func(x2,y2) temp1, temp2, temp3, temp4, temp5, temp6 = 0, 0, 0, 0, 0, 0 for i in range(0, 7): if x2[i] == 1 and y2[i] == 1: temp1 = temp1 + 1 elif x2[i] == 1 and y2[i] == 0: temp2 = temp2 + 1 elif x2[i] == 0 and y2[i] == 1: temp3 = temp3 + 1 for i in range(8, 82): if x2[i] == 1 and y2[i] == 1: temp4 = temp4 + 1 elif x2[i] == 1 and y2[i] == 0: temp5 = temp5 + 1 elif x2[i] == 0 and y2[i] == 1: temp6 = temp6 + 1 c1[0] = int64(temp1) c1[1] = int64(temp2) c1[2] = int64(temp3) c1[3] = int64(temp4) c1[4] = int64(temp5) c1[5] = int64(temp6) a = [temp1, temp2, temp3, temp4, temp5, temp6] for i in range(c1.shape[0]): # for j in range(0,5): c1[i] = a[i] return a
"""Projection on exp. dual cone, and cache.""" minuspi = exp_pri_Pi(-z, cache) return np.copy(z) + minuspi @nb.jit(nb.float64[:](nb.float64[:], nb.float64[:], nb.float64[:])) def exp_dua_D(z, x, cache): """Derivative of projection on exp. dual cone.""" # res = exp_pri_D(z, x, cache) return np.copy(x) + exp_pri_D(-z, -x, cache) exp_dua_cone = cone(exp_dua_Pi, exp_dua_D, exp_dua_D) @nb.jit(nb.int64(nb.int64)) def sizevec2sizemat(n): m = int(np.sqrt(2 * n)) if not n == (m * (m + 1) / 2.): raise DimensionError return m @nb.jit(nb.int64(nb.int64)) def sizemat2sizevec(m): return int((m * (m + 1) / 2.)) @nb.jit(nb.float64[:](nb.float64[:, :])) def mat2vec(Z): """Upper tri row stacked, off diagonal multiplied by sqrt(2)."""
xy + \\text{trunc}\\left(\\frac{\\left(\\left|x - y\\right| - 1\\right)^{2}}{4}\\right) Args: x (array): First value array y (array): Second value array Returns: p (array): Pairing function result Note: This function has a vectorized version that is imported as :func:`~exa.algorithms.indexing.unordered_pairing`; use that function when working with array data. .. _pairing function: http://www.mattdipasquale.com/blog/2014/03/09/unique-unordered-pairing-function/ ''' return np.int64(x * y + np.trunc((np.abs(x - y) - 1)**2 / 4)) if global_config['pkg_numba']: from numba import jit, vectorize, int32, int64, float32, float64 arange1 = jit(nopython=True, cache=True)(arange1) arange2 = jit(nopython=True, cache=True)(arange2) indexes_sc1 = jit(nopython=True, cache=True)(indexes_sc1) indexes_sc2 = jit(nopython=True, cache=True)(indexes_sc2) unordered_pairing = vectorize([int32(int32, int32), int64(int64, int64), float32(float32, float32), float64(float64, float64)], nopython=True)(unordered_pairing)
Args: x1 (array): First component of vector 1 y1 (array): Second component of vector 1 z1 (array): Third component of vector 1 x2 (array): First component of vector 2 y2 (array): Second component of vector 2 z2 (array): Third component of vector 2 Returns: r2 (array): Element-wise squared distance (see definition) .. math:: r2 = (x1 - x2)^{2} + (y1 - y2)^{2} + (z1 - z2)^{2} ''' return (x1 - x2)**2 + (y1 - y2)**2 + (z1 - z2)**2 if global_config['pkg_numba']: from numba import vectorize, float64, float32, int64, int32 vmag3 = vectorize([int32(int32, int32, int32), int64(int64, int64, int64), float32(float32, float32, float32), float64(float64, float64, float64)])(vmag3) vdist3 = vectorize([int32(int32, int32, int32, int32, int32, int32), int64(int64, int64, int64, int64, int64, int64), float32(float32, float32, float32, float32, float32, float32), float64(float64, float64, float64, float64, float64, float64)])(vdist3)
if n <= 2: return 1 return rfib_python(n - 2) + rfib_python(n - 1) # ---- PYTHON NUMBA ----------------------------- import numba @numba.jit def rfib_numba_lazy(n): if n <= 2: return 1 return rfib_numba_lazy(n - 2) + rfib_numba_lazy(n - 1) @numba.jit(numba.int64(numba.int64)) def rfib_numba(n): if n <= 2: return 1 return rfib_numba(n - 2) + rfib_numba(n - 1) RTESTS = ( 'rfib_rust', 'rfib_c', 'rfib_numba', 'rfib_python', ) N = 30 ARGS = [N]
def test_impala_to_numba_pass_through(self): @udf(int64(FunctionContext, IntVal)) def fn(context, x): return x
from slumba import create_function, sqlite_udf from numba import int64, float64, optional @sqlite_udf(float64(float64)) def add_one(x): return x + 1.0 @sqlite_udf(optional(float64)(float64)) def add_one_optional(x): return x + 1.0 if x is not None else None @sqlite_udf(int64(int64, float64)) def add_each_other(x, y): return x + int(y) @pytest.fixture def con(): con = sqlite3.connect(':memory:') con.execute(""" CREATE TABLE t ( id INTEGER PRIMARY KEY, key VARCHAR(1), value DOUBLE PRECISION ) """) con.execute("""
def lssd_compute(r, theta, vrad, mask, sweep_startidx, sweep_endidx): """ Compute core for llsd, uses numpy only functions and numba jit. Parameters: =========== r: array volume radar range array (2D) (m) theta: array volume radar azimuth angle array (2D) (radians) vrad: array volume radial velocity array (2D) (m/s) mask: logical array volume mask for valid radial velocities (2D) sweep_startidx: numba int64 array index of starting rays for tilts sweep_endidx: numba int64 array index of ending rays for tilts Returns: ======== azi_shear: azimuthal shear calculated via the linear least squares derivitives method """ #set the constants definining the LLSD grid in the azimuthal and radial directions azi_saxis = 2000 #m #notes: total azimuthal size = 2*azi_saxis rng_saxis = 1 #idx away from i #notes: total range size = 2*rng_saxis #init az_shear for the volume azi_shear = np.zeros(vrad.shape) #begin looping over grid for k in np.arange(len(sweep_startidx)): #subset volume into tilt r_tilt = r[sweep_startidx[k]:sweep_endidx[k]+1] theta_tilt = theta[sweep_startidx[k]:sweep_endidx[k]+1] vrad_tilt = vrad[sweep_startidx[k]:sweep_endidx[k]+1] mask_tilt = mask[sweep_startidx[k]:sweep_endidx[k]+1] #convert from cylindinrical to cartesian coords x = r_tilt * np.cos(theta_tilt) y = r_tilt * np.sin(theta_tilt) #get size and init az_shear_tilt sz = vrad_tilt.shape azi_shear_tilt = np.zeros(sz) for i in np.arange(0, sz[0]): for j in np.arange(0 + rng_saxis, sz[1] - rng_saxis): #skip if j is invalid if mask_tilt[i, j]: continue #defining the amount of index offsets for azimuthal direction arc_len_idx_offset = int64([(azi_saxis//((2*r_tilt[i, j]*np.pi)/360))])[0] #arc length as a fraction or circ #limit the offset to 100 if arc_len_idx_offset > 100: arc_len_idx_offset = 100 #define the indices for the LLSd grid and deal with wrapping lower_arc_idx = i - arc_len_idx_offset upper_arc_idx = i + arc_len_idx_offset if lower_arc_idx < 0: lower_arc_idx = lower_arc_idx + sz[0] if upper_arc_idx > sz[0]-1: upper_arc_idx = upper_arc_idx - sz[0] if upper_arc_idx < lower_arc_idx: ii_range = np.concatenate((np.arange(lower_arc_idx, sz[0], 1), np.arange(0, upper_arc_idx+1 ,1)), axis=0) else: ii_range = np.arange(lower_arc_idx, upper_arc_idx+1) #define jj range jj_range = np.arange(j-rng_saxis+1, j+rng_saxis) #perform calculations according to Miller et al., (2013) topsum = 0 botsum = 0 masked = False for ii in ii_range: for jj in jj_range: dtheta = (theta_tilt[ii, jj] - theta_tilt[i, j]) #ensure the angle difference doesnt wrap onto another tilt if (abs(dtheta) > np.pi) and (dtheta > 0): dtheta = ((theta_tilt[ii, jj]-2*np.pi) - theta_tilt[i, j]) elif (abs(dtheta) > np.pi) and (dtheta < 0): dtheta=(theta_tilt[ii, jj]) - (theta_tilt[i, j]-2*np.pi) topsum = topsum + (r_tilt[ii, jj]*dtheta) * vrad_tilt[ii, jj] botsum = botsum + (r_tilt[ii, jj]*dtheta)**2 if mask_tilt[ii, jj]: masked = True if masked: #exclude regions which contain any masked pixels pass elif botsum == 0: #exclude areas where there is only one point in each grid pass else: azi_shear_tilt[i, j] = topsum/botsum #insert az shear tilt into volume array azi_shear[sweep_startidx[k]:sweep_endidx[k]+1] = azi_shear_tilt return azi_shear
import unittest from numba import void, int32, uint32, jit, int64 @jit(void(uint32[:], uint32, uint32)) def prng(X, A, C): for i in range(X.shape[0]): for j in range(100): v = (A * X[i] + C) X[i] = v & 0xffffffff @jit(uint32()) def unsigned_literal(): return abs(0xFFFFFFFF) @jit(int64()) def unsigned_literal_64(): return 0x100000000 @jit(int64(int32)) def constant_int_add(a): return 0xffffffff + a class Test(unittest.TestCase): def test_prng(self): N = 100 A = 1664525 C = 1013904223 X0 = np.arange(N, dtype=np.uint32) X1 = X0.copy() prng.py_func(X0, A, C)
variance += (new-old)*(new-newavg+old-oldavg)/(N-1) stddev = sqrt(variance) def get_norm(mu, sd, sample): return norm(mu, sd).logpdf(sample) @vectorize([float64(boolean, float64, float64)]) def ufunc_where(condition, x, y): if condition: return x else: return y @vectorize([int64(float64, float64)], nopython=True) def get_onset_index(onset, dt): onsets = int(onset / dt) return onsets @jit(nopython=True) def slice_theta_array(theta_array, index_arrays, n_vals): i, ix = 0, 0 param_array = np.empty(index_arrays.T.shape) for nlvls in n_vals: arr_ix = index_arrays[i] param_array[:, i] = theta_array[ix:ix+nlvls][arr_ix] i += 1 ix += nlvls return param_array.T
return d1 - d0 @numba.autojit(nopython=True) def datetime_add_timedelta(d, t): return d + t @numba.autojit(nopython=True) def datetime_subtract_timedelta(d, t): return d - t # JNB: vectorize doesn't work for struct-like types right now #@vectorize([numba.datetime(units='D')(numba.datetime(units='D'))]) def ufunc_inc_day(a): return a + numpy.timedelta64(1, 'D') @numba.jit(numba.int64(numba.string_), nopython=True) def cast_datetime_to_int(datetime_str): x = numpy.datetime64(datetime_str) return x @numba.autojit(nopython=True) def datetime_array_index(datetimes, index): return datetimes[index] @numba.jit(numba.datetime(units='M')(numba.datetime(units='M')[:], numba.int_), nopython=True) def datetime_array_index2(datetimes, index): return datetimes[index] @numba.autojit(nopython=True) def timedelta_array_index(timedeltas, index):
def argcast_inner(a, b): if b: # Here `a` is unified to int64 (from int32 originally) a = int64(0) return a
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: return i return 0 @jit(int64(int64, int64[:], int8[:]), nopython=True) def prior_bear_wrb(start, dir, wrb): for i in range(start - 1, 0, -1): if wrb[i] == 1 and dir[i] == -1: return i return 0
l = [(r+i, c+j) for i in [-1, 0, 1] for j in [-1, 0, 1] if 0 <= r + i < n_row if 0 <= c + j < n_col if not (j == 0 and i == 0)] return l """ #### METHODS TO SPEED UP EXECUTION #### binomial = np.random.binomial shuffle = np.random.shuffle random_choice = random.choice """ @jit(int64(), nopython=True) # NOTE: declaring the return type is not required def divide_symmetrically_q(): SYMMETRIC_DIVISION_PROB = 0.3 #3/10.0 verdict = binomial(1, SYMMETRIC_DIVISION_PROB) return verdict @jit(int64(), nopython=True) def divide_q(): DIVISION_PROB = 0.0416 #1 / 24.0 verdict = binomial(1, DIVISION_PROB) return verdict
# # Copyright (c) 2016-2021 Deephaven Data Labs and Patent Pending # import unittest from numba import vectorize, int64 from deephaven import TableTools import bootstrap @vectorize([int64(int64, int64)]) def vectorized_func(x, y): return x % 3 + y class TestClass(unittest.TestCase): def test_part_of_expr(self): with self.assertRaises(Exception): t = TableTools.emptyTable(10).view( "I=ii", "J=(ii * 2)").update("K = 2 * vectorized_func(I, J)") def test_cast(self): t = TableTools.emptyTable(10).view( "I=ii", "J=(ii * 2)").update("K = (float)vectorized_func(I, J)") html_output = TableTools.html(t) self.assertIn("<td>9</td>", html_output) def test_column(self): t = TableTools.emptyTable(10).view( "I=ii", "J=(ii * 2)").update("K = vectorized_func(I, J)") html_output = TableTools.html(t) self.assertIn("<td>9</td>", html_output)
################################################################################ # Created by Oscar Martinez # # [email protected] # # Apache License # # Version 2.0, January 2004 # # # # Extended by: Stella Psomadaki # # Source: Stackoverflow http://bit.ly/1YrXQdj # ################################################################################ from numba import jit, int32, int64 ############################################################################### ###################### Morton conversion in 2D ###################### ############################################################################### @jit(int64(int32)) def Expand2D(n): """ Encodes the 64 bit morton code for a 31 bit number in the 2D space using a divide and conquer approach for separating the bits. 1 bit is not used because the integers are not unsigned Args: n (int): a 2D dimension Returns: int: 64 bit morton code in 2D Raises: Exception: ERROR: Morton code is valid only for positive numbers """
class AlgorithmicStepMethods: @staticmethod @numba.njit([ float64(float64[:], int64[:], int64), int64(int64[:], int64[:], int64) ], **conf.JIT_FLAGS) def amax(row, idx, length): result = np.amax(row[idx[:length]]) return result @staticmethod @numba.njit([ float64(float64[:], int64[:], int64), int64(int64[:], int64[:], int64) ], **conf.JIT_FLAGS) def amin(row, idx, length): result = np.amin(row[idx[:length]]) return result @staticmethod # @numba.njit(**conf.JIT_FLAGS) # Note: in Numba 0.51 "np.dot() only supported on float and complex arrays" def cell_id_body(cell_id, cell_origin, strides): cell_id[:] = np.dot(strides, cell_origin) @staticmethod def cell_id(cell_id, cell_origin, strides): return AlgorithmicStepMethods.cell_id_body(cell_id.data, cell_origin.data, strides.data) @staticmethod @numba.njit(void(float64[:], float64[:], int64[:], int64[:], int64), **conf.JIT_FLAGS) def distance_pair(data_out, data_in, is_first_in_pair, idx, length): # note: silently assumes that data_out is not permuted (i.e. not part of state) for i in prange(length - 1): data_out[i] = np.abs(data_in[idx[i]] - data_in[idx[i + 1]]) if is_first_in_pair[i] else 0 @staticmethod @numba.njit(void(int64[:], int64[:], int64[:], int64[:], int64), **conf.JIT_FLAGS) def find_pairs_body(cell_start, is_first_in_pair, cell_id, idx, length): for i in prange(length - 1): is_first_in_pair[i] = (cell_id[idx[i]] == cell_id[idx[i + 1]] and (i - cell_start[cell_id[idx[i]]]) % 2 == 0) @staticmethod def find_pairs(cell_start, is_first_in_pair, cell_id, idx, length): return AlgorithmicStepMethods.find_pairs_body(cell_start.data, is_first_in_pair.data, cell_id.data, idx.data, length) @staticmethod @numba.njit(void(float64[:], int64[:], int64[:], int64[:], int64), **conf.JIT_FLAGS) def max_pair_body(data_out, data_in, is_first_in_pair, idx, length): # note: silently assumes that data_out is not permuted (i.e. not part of state) for i in prange(length - 1): data_out[i] = max(data_in[idx[i]], data_in[idx[i + 1]]) if is_first_in_pair[i] else 0 @staticmethod def max_pair(data_out, data_in, is_first_in_pair, idx, length): return AlgorithmicStepMethods.max_pair_body(data_out.data, data_in.data, is_first_in_pair.data, idx.data, length) @staticmethod @numba.njit(void(float64[:], float64[:], int64[:], int64[:], int64), **conf.JIT_FLAGS) def sort_pair_body(data_out, data_in, is_first_in_pair, idx, length): data_out[:] = 0 for i in prange(length - 1): if is_first_in_pair[i]: if data_in[idx[i]] < data_in[idx[i + 1]]: data_out[i], data_out[i + 1] = data_in[idx[i + 1]], data_in[idx[i]] else: data_out[i], data_out[i + 1] = data_in[idx[i]], data_in[idx[i + 1]] @staticmethod def sort_pair(data_out, data_in, is_first_in_pair, idx, length): return AlgorithmicStepMethods.sort_pair_body(data_out.data, data_in.data, is_first_in_pair.data, idx.data, length) @staticmethod @numba.njit(void(float64[:], float64[:], int64[:], int64[:], int64), **conf.JIT_FLAGS) def sum_pair_body(data_out, data_in, is_first_in_pair, idx, length): # note: silently assumes that data_out is not permuted (i.e. not part of state) for i in prange(length - 1): data_out[i] = (data_in[idx[i]] + data_in[idx[i + 1]]) if is_first_in_pair[i] else 0 @staticmethod def sum_pair(data_out, data_in, is_first_in_pair, idx, length): return AlgorithmicStepMethods.sum_pair_body(data_out.data, data_in.data, is_first_in_pair.data, idx.data, length)
def test_impala_to_numba_conv(self): @udf(int64(FunctionContext, IntVal)) def fn(context, x): return x + 1
err += dx x += sx else: err = dy // 2 for i in range(dy): pairs[i, 0] = x pairs[i, 1] = y err -= dx if err < 0: x += sx err += dy y += sy return pairs @jit(int64(int32, int32), nopython=True) def rc(row: types.int32, col: types.int32) -> types.int64: return (row << 32) + col @jit(nopython=True) def _get_rc(a: Dict, row: types.int64, col: types.int64, d: types.float64 = np.inf) -> types.float64: kk = rc(row, col) if kk in a: return a.get(kk) else: return d
''' Created on 6. okt. 2016 @author: pab ''' from __future__ import absolute_import, division from numba import jit, float64, int64, int32, int8, void import numpy as np @jit(int64(int64[:], int8[:])) def _findcross(ind, y): ix, dcross, start, v = 0, 0, 0, 0 n = len(y) if y[0] < v: dcross = -1 # first is a up-crossing elif y[0] > v: dcross = 1 # first is a down-crossing elif y[0] == v: # Find out what type of crossing we have next time.. for i in range(1, n): start = i if y[i] < v: ind[ix] = i - 1 # first crossing is a down crossing ix += 1 dcross = -1 # The next crossing is a up-crossing break elif y[i] > v: ind[ix] = i - 1 # first crossing is a up-crossing ix += 1 dcross = 1 # The next crossing is a down-crossing