def get_pair_matches(alpha_inds, beta_conjuncts, beta_inds, conds): n_vars = len(conds.vars) pair_matches = List( [List.empty_list(i8_i8_arr_tuple) for _ in range(n_vars)]) print(beta_inds) # print(beta_inds) for i in range(n_vars): pair_matches_i = pair_matches[i] for j in range(n_vars): if (beta_inds[i, j] != -1): terms_ij = beta_conjuncts[beta_inds[i, j]] print(len(terms_ij)) if (len(terms_ij) > 1): # When two or more beta nodes act on the same pair of variables # we need to find the intersection of the pairs they form # Note: this could probably be rethought to be more performant # but it is probably a rare enough case that it doesn't matter pair_set = None for term in terms_ij: pairs = filter_beta(term, alpha_inds[i], alpha_inds[j]) _pair_set = Dict.empty(i8_x2, u1) if (pair_set is None): for pair in pairs: _pair_set[(pair[0], pair[1])] = u1(1) else: for pair in pairs: t = (pair[0], pair[1]) if (t in pair_set): _pair_set[t] = u1(1) pair_set = _pair_set pairs = np.empty((len(pair_set), 2), dtype=np.int64) for k, (a, b) in enumerate(pair_set.keys()): pairs[k][0] = a pairs[k][1] = b else: # The more common case: there is just one beta term for this # pair of variables term = terms_ij[0] pairs = filter_beta(term, alpha_inds[i], alpha_inds[j]) print(pairs) pair_matches_i.append((j, pairs)) return pair_matches
def fit_tree(tree, config, iterative=False): ''' Refits the tree from its DataStats ''' cache_nodes = False # print("Z") context_stack, node_dict, nodes = \ build_root(tree) # print("A") while (len(context_stack) > 0): # print("AZ") c = context_stack.pop() update_nominal_impurities(tree, c, iterative) # print("BZ") # print(c.impurities[:,0],c.start,c.end) best_split = np.argmin(c.impurities[:, 0]) for split in [best_split]: # print("S", split) inds_l, inds_r, y_counts_l, y_counts_r, imp_tot, imp_l, imp_r, val = \ extract_nominal_split_info(tree, c, split) # print("S1", split) # if(impurity_decrease[split] <= 0.0): if (c.impurity - imp_tot <= 0): c.node.ttype = TTYPE_LEAF else: # print("S2", split) ptr = _pointer_from_struct(c) locs = (c, best_split, val, iterative, node_dict, context_stack, cache_nodes) node_l = new_node(locs, tree, inds_l, y_counts_l, imp_l, 0) node_r = new_node(locs, tree, inds_r, y_counts_r, imp_r, 1) split_data = SplitData(u1(False), i4(split), i4(val), i4(node_l), i4(node_r)) #np.array([split, val, node_l, node_r, -1],dtype=np.int32) c.node.split_data.append(split_data) c.node.op_enum = OP_EQ # print("B") if (not iterative): SplitterContext_dtor(c) # print("C") # _decref_pointer(ptr) return 0
def biInterpolation(distorted, i, j): i = u2(i) j = u2(j) Q11 = distorted[j, i] Q12 = distorted[j, i + 1] Q21 = distorted[j + 1, i] Q22 = distorted[j + 1, i + 1] return u1( Q11 * (i + 1 - i) * (j + 1 - j) + Q12 * (i - i) * (j + 1 - j) + Q21 * (i + 1 - i) * (j - j) + Q22 * (i - i) * (j - j) )
def _init_signature(cls): assert hasattr( cls, 'signature'), "Operator %r missing signature." % cls.__name__ out_type, arg_types = parse_signature(cls.signature) out_type = TYPE_ALIASES.get(out_type, out_type) arg_types = [TYPE_ALIASES.get(x, x) for x in arg_types] # print(arg_types) cls.out_type = out_type cls.arg_types = arg_types resolved_out_type = REGISTERED_TYPES[out_type] resolved_arg_types = [REGISTERED_TYPES[x] for x in arg_types] cls.signature = "{}({})".format(out_type, ",".join(arg_types)) cls.signature_obj = resolved_out_type( *resolved_arg_types ) #"{}({})".format(out_type,",".join(arg_types)) cls.cond_signature = "u1({})".format(",".join(arg_types)) cls.cond_signature_obj = u1(*resolved_arg_types) u_types, u_inds = np.unique(arg_types, return_inverse=True) cls.u_arg_types = u_types cls.u_arg_inds = u_inds if (isinstance(cls.commutes, bool)): if (cls.commutes == True): cls.commutes = [ np.where(i == u_inds)[0].tolist() for i in range(len(u_types)) ] else: cls.commutes = [] else: assert (isinstance(cls.commutes, Iterable)) right_commutes = Dict.empty(i8, i8[::1]) for i in range(len(cls.commutes)): commuting_set = cls.commutes[i] # print(commuting_set) for j in range(len(commuting_set) - 1, 0, -1): right_commutes[commuting_set[j]] = np.array(commuting_set[0:j], dtype=np.int64) for k in commuting_set[0:j]: assert u_inds[k] == u_inds[commuting_set[j]], \ "invalid 'commutes' argument, argument %s and %s have different types \ %s and %s" % (j, k, u_types[u_inds[j]], u_types[u_inds[k]]) cls.right_commutes = right_commutes
# b[index] = msg_bytearray[index] # index = index + 1 return b def isSignalSignedType(signal): if signal._valuetype == '+': return False else: return True @njit(numba.i8(numba.u8,numba.u1)) def twosComplement(number, signalsize): return (number - (1 << signalsize)) @njit(numba.u1(numba.u1)) def getArrayIdxFromStartBit(n): return (0 if (n+1)%8 == 0 else 8-((n+1)%8)) + (n/8)*8 def getFactorIsIntegerFromSignal(signal): return True if float(signal._factor).is_integer() else False def getOffsetIsIntegerFromSignal(signal): return True if float(signal._offset).is_integer() else False # @jit def getSignalNumber(barray_unpacked, barray, start_bit, signalsize, isByteorderIntel, isValuetypeiSigned, factor, offset): # just guard againts unhandled (yet) intel bytecode # motorola only for now if 1 == isByteorderIntel: raise UserWarning
akd[h] = elems @njit(nogil=True, fastmath=True) def akd_get(akd, _arr, h=None): '''Gets an i4 from a dictionary keyed by an array _arr''' arr = _arr.view(np.uint8) if (h is None): h = hasharray(arr) if (h in akd): for elem in akd[h]: if (len(elem[0]) == len(arr) and (elem[0] == arr).all()): return elem[1] return -1 TTYPE_NODE = u1(1) TTYPE_LEAF = u1(2) @njit(cache=True) def next_split_chain(c, is_right, is_cont, split, val): l = len(c.split_chain) split_chain = np.empty(l + 1, dtype=np.uint64) split_chain[:l] = c.split_chain split_chain[l] = (is_cont << 63) | (is_right << 62) | (split << 32) | val return split_chain @njit(cache=True) def new_node(locs, tree, sample_inds, y_counts, impurity, is_right): c, best_split, best_val, iterative, node_dict, context_stack, cache_nodes = locs
#### TreeNode #### treenode_fields = [ ('ttype',u1), ('index',i4), ('op_enum', u1), ('split_data',ListType(SplitDataType)), ('counts', u4[:]) ] TreeNode, TreeNodeType = define_structref("TreeNode",treenode_fields,define_constructor=False) OP_NOP = u1(0) OP_GE = u1(1) OP_LT = u1(2) OP_ISNAN = u1(3) OP_EQ = u1(4) # i4_arr = i4[:] @njit(cache=True) def TreeNode_ctor(ttype, index, counts): st = new(TreeNodeType) st.ttype = ttype st.index = index st.op_enum = OP_NOP st.split_data = List.empty_list(SplitDataType) st.counts = counts
import itertools import random import sys import time from time import strftime, localtime import numpy as np from numba import vectorize, njit, prange, jit, u1 @jit(u1(u1[:], u1[:])) def hamming_distance(array1, array2): """ Calculates the hamming distance between two numpy arrays of uint8s """ if (array1.shape != array1.shape): raise ValueError("Shape of array1 & array2 does not match") distance = 0 for i in range(array1.shape[0]): if (array1[i] != array2[i]): distance += 1 return distance @jit def gen_max_hamming_set(N): """ Generates maximal hamming distance permutation set N - the number of permutations in the returned set """ # There a 9! permutations, factorial function avoided for numba NUM_PERMUTATIONS = 362880
#!/usr/bin/env python import rospy from foobar.msg import Canbus as CanBusMsg from numba import jit , njit import numba import numpy as np canbus_msg = CanBusMsg() @njit(numba.u1(numba.u1)) def getArrayIdxFromStartBit(n): return (0 if (n+1)%8 == 0 else 8-((n+1)%8)) + (n/8)*8 # @njit(numba.u8(numba.u1[:],numba.u1,numba.u1,numba.u8)) def setBigEndiNumberToNpArr(blist, idx, size, number): # print number # print [number & (1 << (size - i - 1)) for i in range (0,size)] for i in range (0,size): if (number & (1 << (size - i - 1))): blist[idx+i] = 1 else: blist[idx+i] = 0 def startRosNode(): rospy.init_node('can_test_msg_node', anonymous=True) # pub_can_msg = rospy.Publisher('radar_packet/can0/send', CanBusMsg, queue_size=10) pub_can_msg = rospy.Publisher('radar_packet/can0/recv', CanBusMsg, queue_size=300) pub_can_msg2 = rospy.Publisher('radar_packet/can1/recv', CanBusMsg, queue_size=300) pub_can_msg3 = rospy.Publisher('radar_packet/can2/recv', CanBusMsg, queue_size=300)