我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.uint32()。
def _read_ctr_ticks( task_handle, high_tick, low_tick, num_samps_per_chan, timeout, interleaved=FillMode.GROUP_BY_CHANNEL): samps_per_chan_read = ctypes.c_int() cfunc = lib_importer.windll.DAQmxReadCtrTicks if cfunc.argtypes is None: with cfunc.arglock: if cfunc.argtypes is None: cfunc.argtypes = [ lib_importer.task_handle, ctypes.c_int, ctypes.c_double, ctypes.c_int, wrapped_ndpointer(dtype=numpy.uint32, flags=('C', 'W')), wrapped_ndpointer(dtype=numpy.uint32, flags=('C', 'W')), ctypes.c_uint, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(c_bool32)] error_code = cfunc( task_handle, num_samps_per_chan, timeout, interleaved.value, high_tick, low_tick, numpy.prod(high_tick.shape), ctypes.byref(samps_per_chan_read), None) check_for_error(error_code) return samps_per_chan_read.value
def run(im): im_disp = im.copy() window_name = "Draw line here." cv2.namedWindow(window_name,cv2.WINDOW_AUTOSIZE) cv2.moveWindow(window_name, 910, 0) print " Drag across the screen to set lines.\n Do it twice" print " After drawing the lines press 'r' to resume\n" l1 = np.empty((2, 2), np.uint32) l2 = np.empty((2, 2), np.uint32) list = [l1,l2] mouse_down = False def callback(event, x, y, flags, param): global trigger, mouse_down if trigger<2: if event == cv2.EVENT_LBUTTONDOWN: mouse_down = True list[trigger][0] = (x, y) if event == cv2.EVENT_LBUTTONUP and mouse_down: mouse_down = False list[trigger][1] = (x,y) cv2.line(im_disp, (list[trigger][0][0], list[trigger][0][1]), (list[trigger][1][0], list[trigger][1][1]), (255, 0, 0), 2) trigger += 1 else: pass cv2.setMouseCallback(window_name, callback) while True: cv2.imshow(window_name,im_disp) key = cv2.waitKey(10) & 0xFF if key == ord('r'): # Press key `q` to quit the program return list exit()
def __init__(self, image, samplefac=10, colors=256): # Check Numpy if np is None: raise RuntimeError("Need Numpy for the NeuQuant algorithm.") # Check image if image.size[0] * image.size[1] < NeuQuant.MAXPRIME: raise IOError("Image is too small") if image.mode != "RGBA": raise IOError("Image mode should be RGBA.") # Initialize self.setconstants(samplefac, colors) self.pixels = np.fromstring(image.tostring(), np.uint32) self.setUpArrays() self.learn() self.fix() self.inxbuild()
def laplace_gpu(y_gpu, mode='valid'): shape = np.array(y_gpu.shape).astype(np.uint32) dtype = y_gpu.dtype block_size = (16,16,1) grid_size = (int(np.ceil(float(shape[1])/block_size[0])), int(np.ceil(float(shape[0])/block_size[1]))) shared_size = int((2+block_size[0])*(2+block_size[1])*dtype.itemsize) preproc = _generate_preproc(dtype, shape) mod = SourceModule(preproc + kernel_code, keep=True) if mode == 'valid': laplace_fun_gpu = mod.get_function("laplace_valid") laplace_gpu = cua.empty((y_gpu.shape[0]-2, y_gpu.shape[1]-2), y_gpu.dtype) if mode == 'same': laplace_fun_gpu = mod.get_function("laplace_same") laplace_gpu = cua.empty((y_gpu.shape[0], y_gpu.shape[1]), y_gpu.dtype) laplace_fun_gpu(laplace_gpu.gpudata, y_gpu.gpudata, block=block_size, grid=grid_size, shared=shared_size) return laplace_gpu
def remove_artifacts(self, image): """ Remove the connected components that are not within the parameters Operates in place :param image: sudoku's thresholded image w/o grid :return: None """ labeled, features = label(image, structure=CROSS) lbls = np.arange(1, features + 1) areas = extract_feature(image, labeled, lbls, np.sum, np.uint32, 0) sides = extract_feature(image, labeled, lbls, min_side, np.float32, 0, True) diags = extract_feature(image, labeled, lbls, diagonal, np.float32, 0, True) for index in lbls: area = areas[index - 1] / 255 side = sides[index - 1] diag = diags[index - 1] if side < 5 or side > 20 \ or diag < 15 or diag > 25 \ or area < 40: image[labeled == index] = 0 return None
def _write_binary_u_32( task_handle, write_array, num_samps_per_chan, auto_start, timeout, data_layout=FillMode.GROUP_BY_CHANNEL): samps_per_chan_written = ctypes.c_int() cfunc = lib_importer.windll.DAQmxWriteBinaryU32 if cfunc.argtypes is None: with cfunc.arglock: if cfunc.argtypes is None: cfunc.argtypes = [ lib_importer.task_handle, ctypes.c_int, c_bool32, ctypes.c_double, ctypes.c_int, wrapped_ndpointer(dtype=numpy.uint32, flags=('C', 'W')), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(c_bool32)] error_code = cfunc( task_handle, num_samps_per_chan, auto_start, timeout, data_layout.value, write_array, ctypes.byref(samps_per_chan_written), None) check_for_error(error_code) return samps_per_chan_written.value
def _write_digital_u_32( task_handle, write_array, num_samps_per_chan, auto_start, timeout, data_layout=FillMode.GROUP_BY_CHANNEL): samps_per_chan_written = ctypes.c_int() cfunc = lib_importer.windll.DAQmxWriteDigitalU32 if cfunc.argtypes is None: with cfunc.arglock: if cfunc.argtypes is None: cfunc.argtypes = [ lib_importer.task_handle, ctypes.c_int, c_bool32, ctypes.c_double, ctypes.c_int, wrapped_ndpointer(dtype=numpy.uint32, flags=('C', 'W')), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(c_bool32)] error_code = cfunc( task_handle, num_samps_per_chan, auto_start, timeout, data_layout.value, write_array, ctypes.byref(samps_per_chan_written), None) check_for_error(error_code) return samps_per_chan_written.value
def _read_binary_u_32( task_handle, read_array, num_samps_per_chan, timeout, fill_mode=FillMode.GROUP_BY_CHANNEL): samps_per_chan_read = ctypes.c_int() cfunc = lib_importer.windll.DAQmxReadBinaryU32 if cfunc.argtypes is None: with cfunc.arglock: if cfunc.argtypes is None: cfunc.argtypes = [ lib_importer.task_handle, ctypes.c_int, ctypes.c_double, ctypes.c_int, wrapped_ndpointer(dtype=numpy.uint32, flags=('C', 'W')), ctypes.c_uint, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(c_bool32)] error_code = cfunc( task_handle, num_samps_per_chan, timeout, fill_mode.value, read_array, numpy.prod(read_array.shape), ctypes.byref(samps_per_chan_read), None) check_for_error(error_code) return samps_per_chan_read.value
def _read_digital_u_32( task_handle, read_array, num_samps_per_chan, timeout, fill_mode=FillMode.GROUP_BY_CHANNEL): samps_per_chan_read = ctypes.c_int() cfunc = lib_importer.windll.DAQmxReadDigitalU32 if cfunc.argtypes is None: with cfunc.arglock: if cfunc.argtypes is None: cfunc.argtypes = [ lib_importer.task_handle, ctypes.c_int, ctypes.c_double, ctypes.c_int, wrapped_ndpointer(dtype=numpy.uint32, flags=('C', 'W')), ctypes.c_uint, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(c_bool32)] error_code = cfunc( task_handle, num_samps_per_chan, timeout, fill_mode.value, read_array, numpy.prod(read_array.shape), ctypes.byref(samps_per_chan_read), None) check_for_error(error_code) return samps_per_chan_read.value
def _read_counter_u_32(task_handle, read_array, num_samps_per_chan, timeout): samps_per_chan_read = ctypes.c_int() cfunc = lib_importer.windll.DAQmxReadCounterU32 if cfunc.argtypes is None: with cfunc.arglock: if cfunc.argtypes is None: cfunc.argtypes = [ lib_importer.task_handle, ctypes.c_int, ctypes.c_double, wrapped_ndpointer(dtype=numpy.uint32, flags=('C', 'W')), ctypes.c_uint, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(c_bool32)] error_code = cfunc( task_handle, num_samps_per_chan, timeout, read_array, numpy.prod(read_array.shape), ctypes.byref(samps_per_chan_read), None) check_for_error(error_code) return samps_per_chan_read.value
def _read_counter_u_32_ex( task_handle, read_array, num_samps_per_chan, timeout, fill_mode=FillMode.GROUP_BY_CHANNEL): samps_per_chan_read = ctypes.c_int() cfunc = lib_importer.windll.DAQmxReadCounterU32Ex if cfunc.argtypes is None: with cfunc.arglock: if cfunc.argtypes is None: cfunc.argtypes = [ lib_importer.task_handle, ctypes.c_int, ctypes.c_double, ctypes.c_int, wrapped_ndpointer(dtype=numpy.uint32, flags=('C', 'W')), ctypes.c_uint, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(c_bool32)] error_code = cfunc( task_handle, num_samps_per_chan, timeout, fill_mode.value, read_array, numpy.prod(read_array.shape), ctypes.byref(samps_per_chan_read), None) check_for_error(error_code) return samps_per_chan_read.value
def _get_dtype_maps(): """ Get dictionaries to map numpy data types to ITK types and the other way around. """ # Define pairs tmp = [ (np.float32, 'MET_FLOAT'), (np.float64, 'MET_DOUBLE'), (np.uint8, 'MET_UCHAR'), (np.int8, 'MET_CHAR'), (np.uint16, 'MET_USHORT'), (np.int16, 'MET_SHORT'), (np.uint32, 'MET_UINT'), (np.int32, 'MET_INT'), (np.uint64, 'MET_ULONG'), (np.int64, 'MET_LONG') ] # Create dictionaries map1, map2 = {}, {} for np_type, itk_type in tmp: map1[np_type.__name__] = itk_type map2[itk_type] = np_type.__name__ # Done return map1, map2
def report_barcodes_h5(self, filename): data = self.report('barcodes') if self.barcode_whitelist: bc_sequences = cr_utils.format_barcode_seqs(self.barcode_whitelist, self.gem_groups) elif self.barcode_summary is not None: bc_sequences = self.barcode_summary else: # Get all observed bc sequences bc_sequences = sorted(list(reduce(lambda x, y: x.union(y.keys()), data.values(), set()))) # Build the columns for the table bc_table_cols = {cr_constants.H5_BC_SEQUENCE_COL: bc_sequences} for metric_name, metric_data in data.iteritems(): counts = np.array([metric_data.get(bc, 0) for bc in bc_sequences], dtype=np.uint32) bc_table_cols[metric_name] = counts cr_utils.write_h5(filename, bc_table_cols)
def load(shape,vertex_array): destination = vertex_array[gx.VA_PTNMTXIDX.name] vertex_index = 0 matrix_table = numpy.zeros(10,numpy.uint32) for batch in shape.batches: source = numpy.concatenate([primitive.vertices[gx.VA_PTNMTXIDX.name] for primitive in batch.primitives]) source //= 3 for i,index in enumerate(batch.matrix_table): if index == 0xFFFF: continue matrix_table[i] = index length = sum(len(primitive.vertices) for primitive in batch.primitives) numpy.take(matrix_table,source,0,destination[vertex_index:vertex_index + length]) vertex_index += length glEnableVertexAttribArray(MATRIX_INDEX_ATTRIBUTE_LOCATION) vertex_type = vertex_array.dtype stride = vertex_type.itemsize offset = vertex_type.fields[gx.VA_PTNMTXIDX.name][1] glVertexAttribIPointer(MATRIX_INDEX_ATTRIBUTE_LOCATION,1,GL_UNSIGNED_INT,stride,GLvoidp(offset))
def __init__(self, *args, **kwds): import numpy self.dst_types = [numpy.uint8, numpy.uint16, numpy.uint32] try: self.dst_types.append(numpy.uint64) except AttributeError: pass pygame.display.init() try: unittest.TestCase.__init__(self, *args, **kwds) self.sources = [self._make_src_surface(8), self._make_src_surface(16), self._make_src_surface(16, srcalpha=True), self._make_src_surface(24), self._make_src_surface(32), self._make_src_surface(32, srcalpha=True)] finally: pygame.display.quit()
def _check_valid_data(self, data): """Checks that the incoming data is a 2 x #elements ndarray of ints. Parameters ---------- data : :obj:`numpy.ndarray` The data to verify. Raises ------ ValueError If the data is not of the correct shape or type. """ if data.dtype.type != np.int8 and data.dtype.type != np.int16 \ and data.dtype.type != np.int32 and data.dtype.type != np.int64 \ and data.dtype.type != np.uint8 and data.dtype.type != np.uint16 \ and data.dtype.type != np.uint32 and data.dtype.type != np.uint64: raise ValueError('Must initialize image coords with a numpy int ndarray') if data.shape[0] != 2: raise ValueError('Illegal data array passed to image coords. Must have 2 coordinates') if len(data.shape) > 2: raise ValueError('Illegal data array passed to point cloud. Must have 1 or 2 dimensions')
def to_best_type(array): '''Convert array to lowest possible bitrate. ''' ui8 = np.iinfo(np.uint8) ui8 = ui8.max ui16 = np.iinfo(np.uint16) ui16 = ui16.max ui32 = np.iinfo(np.uint32) ui32 = ui32.max ui64 = np.iinfo(np.uint64) ui64 = ui64.max if array.max() <= ui64: new_type = np.uint64 if array.max() <= ui32: new_type = np.uint32 if array.max() <= ui16: new_type = np.uint16 if array.max() <= ui8: new_type = np.uint8 return array.astype(new_type)
def read_uncompressed_patch(pcpatch_wkb, schema): ''' Patch binary structure uncompressed: byte: endianness (1 = NDR, 0 = XDR) uint32: pcid (key to POINTCLOUD_SCHEMAS) uint32: 0 = no compression uint32: npoints pointdata[]: interpret relative to pcid ''' patchbin = unhexlify(pcpatch_wkb) npoints = unpack("I", patchbin[9:13])[0] dt = schema_dtype(schema) patch = np.fromstring(patchbin[13:], dtype=dt) # debug # print(patch[:10]) return patch, npoints
def __init__(self, queue, grid_size, program_buffer, block_sizes): self.queue = queue self.grid_size = grid_size self.program_buffer = program_buffer self.block_sizes = block_sizes self.integral_one = 0 self.integral_x = 0 self.integral_y = 0 self.integral_z = 0 self.integral_xx = 0 self.integral_yy = 0 self.integral_zz = 0 self.integral_xy = 0 self.integral_xz = 0 self.integral_yz = 0 self.index_sums = cl_util.Buffer(queue, numpy.uint32, 10, pyopencl.mem_flags.READ_WRITE) self.counter = cl_util.Buffer(queue, numpy.uint32, 1, pyopencl.mem_flags.READ_WRITE) self.list = cl_util.Buffer(queue, cl_util.Buffer.quad_dtype(numpy.uint8), grid_size * grid_size * grid_size, pyopencl.mem_flags.WRITE_ONLY) self.box_corner = None self.level = None
def generate_trainig_data(self, num_points): ''' Generate training dataset. Produce random (integer) sequences X, and corresponding expected output sequences Y = generate_output_sequence(X). Return xy_data, y_data (both of type uint32) xy_data = numpy array of shape [num_points, in_seq_len + out_seq_len], with each point being X + Y y_data = numpy array of shape [num_points, out_seq_len] ''' x_data = np.random.randint(0, self.in_max_int, size=(num_points, self.in_seq_len)) # shape [num_points, in_seq_len] x_data = x_data.astype(np.uint32) # ensure integer type y_data = [ self.sequence_pattern.generate_output_sequence(x) for x in x_data ] y_data = np.array(y_data) xy_data = np.append(x_data, y_data, axis=1) # shape [num_points, 2*seq_len] return xy_data, y_data
def test_int(self): for st, ut, s in [(np.int8, np.uint8, 8), (np.int16, np.uint16, 16), (np.int32, np.uint32, 32), (np.int64, np.uint64, 64)]: for i in range(1, s): assert_equal(hash(st(-2**i)), hash(-2**i), err_msg="%r: -2**%d" % (st, i)) assert_equal(hash(st(2**(i - 1))), hash(2**(i - 1)), err_msg="%r: 2**%d" % (st, i - 1)) assert_equal(hash(st(2**i - 1)), hash(2**i - 1), err_msg="%r: 2**%d - 1" % (st, i)) i = max(i - 1, 1) assert_equal(hash(ut(2**(i - 1))), hash(2**(i - 1)), err_msg="%r: 2**%d" % (ut, i - 1)) assert_equal(hash(ut(2**i - 1)), hash(2**i - 1), err_msg="%r: 2**%d - 1" % (ut, i))
def test_prod(self): ba = [1, 2, 10, 11, 6, 5, 4] ba2 = [[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]] for ctype in [np.int16, np.uint16, np.int32, np.uint32, np.float32, np.float64, np.complex64, np.complex128]: a = np.array(ba, ctype) a2 = np.array(ba2, ctype) if ctype in ['1', 'b']: self.assertRaises(ArithmeticError, a.prod) self.assertRaises(ArithmeticError, a2.prod, axis=1) else: assert_equal(a.prod(axis=0), 26400) assert_array_equal(a2.prod(axis=0), np.array([50, 36, 84, 180], ctype)) assert_array_equal(a2.prod(axis=-1), np.array([24, 1890, 600], ctype))
def test_dtype_mix(self): c = np.array([False, True, False, False, False, False, True, False, False, False, True, False]) a = np.uint32(1) b = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.], dtype=np.float64) r = np.array([5., 1., 3., 2., -1., -4., 1., -10., 10., 1., 1., 3.], dtype=np.float64) assert_equal(np.where(c, a, b), r) a = a.astype(np.float32) b = b.astype(np.int64) assert_equal(np.where(c, a, b), r) # non bool mask c = c.astype(np.int) c[c != 0] = 34242324 assert_equal(np.where(c, a, b), r) # invert tmpmask = c != 0 c[c == 0] = 41247212 c[tmpmask] = 0 assert_equal(np.where(c, b, a), r)
def test_setdiff1d(self): a = np.array([6, 5, 4, 7, 1, 2, 7, 4]) b = np.array([2, 4, 3, 3, 2, 1, 5]) ec = np.array([6, 7]) c = setdiff1d(a, b) assert_array_equal(c, ec) a = np.arange(21) b = np.arange(19) ec = np.array([19, 20]) c = setdiff1d(a, b) assert_array_equal(c, ec) assert_array_equal([], setdiff1d([], [])) a = np.array((), np.uint32) assert_equal(setdiff1d(a, []).dtype, np.uint32)
def test_basic(self): ba = [1, 2, 10, 11, 6, 5, 4] ba2 = [[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]] for ctype in [np.int8, np.uint8, np.int16, np.uint16, np.int32, np.uint32, np.float32, np.float64, np.complex64, np.complex128]: a = np.array(ba, ctype) a2 = np.array(ba2, ctype) tgt = np.array([1, 3, 13, 24, 30, 35, 39], ctype) assert_array_equal(np.cumsum(a, axis=0), tgt) tgt = np.array( [[1, 2, 3, 4], [6, 8, 10, 13], [16, 11, 14, 18]], ctype) assert_array_equal(np.cumsum(a2, axis=0), tgt) tgt = np.array( [[1, 3, 6, 10], [5, 11, 18, 27], [10, 13, 17, 22]], ctype) assert_array_equal(np.cumsum(a2, axis=1), tgt)
def test_basic(self): ba = [1, 2, 10, 11, 6, 5, 4] ba2 = [[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]] for ctype in [np.int16, np.uint16, np.int32, np.uint32, np.float32, np.float64, np.complex64, np.complex128]: a = np.array(ba, ctype) a2 = np.array(ba2, ctype) if ctype in ['1', 'b']: self.assertRaises(ArithmeticError, np.prod, a) self.assertRaises(ArithmeticError, np.prod, a2, 1) else: assert_equal(a.prod(axis=0), 26400) assert_array_equal(a2.prod(axis=0), np.array([50, 36, 84, 180], ctype)) assert_array_equal(a2.prod(axis=-1), np.array([24, 1890, 600], ctype))
def test_basic(self): ba = [1, 2, 10, 11, 6, 5, 4] ba2 = [[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]] for ctype in [np.int16, np.uint16, np.int32, np.uint32, np.float32, np.float64, np.complex64, np.complex128]: a = np.array(ba, ctype) a2 = np.array(ba2, ctype) if ctype in ['1', 'b']: self.assertRaises(ArithmeticError, np.cumprod, a) self.assertRaises(ArithmeticError, np.cumprod, a2, 1) self.assertRaises(ArithmeticError, np.cumprod, a) else: assert_array_equal(np.cumprod(a, axis=-1), np.array([1, 2, 20, 220, 1320, 6600, 26400], ctype)) assert_array_equal(np.cumprod(a2, axis=0), np.array([[1, 2, 3, 4], [5, 12, 21, 36], [50, 36, 84, 180]], ctype)) assert_array_equal(np.cumprod(a2, axis=-1), np.array([[1, 2, 6, 24], [5, 30, 210, 1890], [10, 30, 120, 600]], ctype))
def torus_faces(x, y): faces = np.empty((x * y, 4), dtype=np.uint32) tmp = np.arange(0, x * y) faces[:, 0] = tmp faces[:, 1] = np.roll(tmp, -y) tmp += 1 tmp.shape = (x, y) tmp[:, y - 1] -= y tmp.shape = -1 faces[:, 3] = tmp faces[:, 2] = np.roll(tmp, -y) faces.shape = -1 l_total = np.empty(x * y, dtype=np.uint32) l_total[:] = 4 l_start = np.arange(0, (x * y) * 4, 4, dtype=np.uint32) return SvPolygon(l_start, l_total, faces)
def gen_file(fname, nbytes): nelems = int(nbytes / 4) data = np.random.randint(0, 2 ** 32, nelems, dtype=np.uint32) # zero out 10% so there is something to compress zeros = np.random.randint(0, nelems, int(nelems / 10.0)) data[zeros] = 0 data = data.tostring() # write 1GB max at a time - the gzip # module doesn't like writing >= 4GB # in one go. chunksize = 1073741824 while len(data) > 0: chunk = data[:chunksize] data = data[chunksize:] with gzip.open(fname, 'ab') as outf: outf.write(chunk)
def get_risk_free_rate(self, start_date, end_date): mongo_dates = self._yield_curve['dates'].find({}, {"_id":0}).sort('date', pymongo.ASCENDING) _dates = np.array([np.uint32(d['date']) for d in mongo_dates]) tenor = risk_free_helper.get_tenor_for(start_date, end_date) tenor = tenor[-1] + tenor[:-1] mongo_data = self._yield_curve[tenor].find({}, {"_id":0}) _table = np.array([d['data'] for d in mongo_data]) d = start_date.year * 10000 + start_date.month * 100 + start_date.day pos = _dates.searchsorted(d) if pos > 0 and (pos == len(_dates) or _dates[pos] != d): pos -= 1 while pos >= 0 and np.isnan(_table[pos]): # data is missing ... pos -= 1 return _table[pos]
def available_data_range(self, frequency): """ ?????????????? :param str frequency: ?????`1d` ?????, `1m` ?????? :return: (earliest, latest) """ if frequency == '1d': mongo_data = self._day_bars[self.INSTRUMENT_TYPE_MAP['INDX']]['000001.XSHG'].find({}, {"_id":0}).sort('date', pymongo.ASCENDING) mongo_data = list(mongo_data) s, e = np.uint32(mongo_data[0]['date']), np.uint32(mongo_data[-1]['date']) return convert_int_to_date(s).date(), convert_int_to_date(e).date() if frequency == '1m': raise NotImplementedError
def laplace_stack_gpu(y_gpu, mode='valid'): """ This funtion computes the Laplacian of each slice of a stack of images """ shape = np.array(y_gpu.shape).astype(np.uint32) dtype = y_gpu.dtype block_size = (6,int(np.floor(512./6./float(shape[0]))),int(shape[0])) grid_size = (int(np.ceil(float(shape[1])/block_size[0])), int(np.ceil(float(shape[0])/block_size[1]))) shared_size = int((2+block_size[0])*(2+block_size[1])*(2+block_size[2]) *dtype.itemsize) preproc = _generate_preproc(dtype, (shape[1],shape[2])) mod = SourceModule(preproc + kernel_code, keep=True) laplace_fun_gpu = mod.get_function("laplace_stack_same") laplace_gpu = cua.empty((y_gpu.shape[0], y_gpu.shape[1], y_gpu.shape[2]), y_gpu.dtype) laplace_fun_gpu(laplace_gpu.gpudata, y_gpu.gpudata, block=block_size, grid=grid_size, shared=shared_size) return laplace_gpu
def laplace3d_gpu(y_gpu): shape = np.array(y_gpu.shape).astype(np.uint32) dtype = y_gpu.dtype block_size = (6,int(np.floor(512./6./float(shape[0]))),int(shape[0])) grid_size = (int(np.ceil(float(shape[1])/block_size[0])), int(np.ceil(float(shape[0])/block_size[1]))) shared_size = int((2+block_size[0])*(2+block_size[1])*(2+block_size[2]) *dtype.itemsize) preproc = _generate_preproc(dtype, (shape[1],shape[2])) mod = SourceModule(preproc + kernel_code, keep=True) laplace_fun_gpu = mod.get_function("laplace3d_same") laplace_gpu = cua.empty((y_gpu.shape[0], y_gpu.shape[1], y_gpu.shape[2]), y_gpu.dtype) laplace_fun_gpu(laplace_gpu.gpudata, y_gpu.gpudata, block=block_size, grid=grid_size, shared=shared_size) return laplace_gpu
def modify_sparse23_gpu(y_gpu, beta): shape = np.array(y_gpu.shape).astype(np.uint32) gpu_shape = np.array([np.prod(shape),np.prod(shape)]) gpu_shape = np.uint32(np.ceil(np.sqrt(gpu_shape))) dtype = y_gpu.dtype block_size = (16,16,1) grid_size = (int(np.ceil(float(gpu_shape[1])/block_size[0])), int(np.ceil(float(gpu_shape[0])/block_size[1]))) preproc = _generate_preproc(dtype, np.array(grid_size) * np.array(block_size)[0:1]) mod = SourceModule(preproc + kernel_code, keep=True) modify_alpha23_fun = mod.get_function("modify_alpha23") modify_alpha23_fun(y_gpu.gpudata, np.float32(beta), np.uint32(np.prod(shape)), block=block_size, grid=grid_size)
def modify_sparse_gpu(y_gpu, beta, alpha=2/3): shape = np.array(y_gpu.shape).astype(np.uint32) gpu_shape = np.array([np.prod(shape),np.prod(shape)]) gpu_shape = np.uint32(np.ceil(np.sqrt(gpu_shape))) dtype = y_gpu.dtype block_size = (16,16,1) grid_size = (int(np.ceil(float(gpu_shape[1])/block_size[0])), int(np.ceil(float(gpu_shape[0])/block_size[1]))) preproc = _generate_preproc(dtype, np.array(grid_size) * np.array(block_size)[0:1]) mod = SourceModule(preproc + kernel_code, keep=True) modify_alpha_fun = mod.get_function("modify_alpha") modify_alpha_fun(y_gpu.gpudata, np.float32(beta), np.float32(alpha), np.uint32(np.prod(shape)), block=block_size, grid=grid_size)
def project_on_basis_gpu(fs_gpu, basis_gpu): basis_length = basis_gpu.shape[0] shape = np.array(fs_gpu.shape).astype(np.uint32) dtype = fs_gpu.dtype block_size = (16,16,1) grid_size = (1,int(np.ceil(float(basis_length)/block_size[1]))) weights_gpu = cua.empty(basis_length, dtype=dtype) preproc = _generate_preproc(dtype, shape) preproc += '#define BLOCK_SIZE %d\n' % (block_size[0]*block_size[1]) mod = SourceModule(preproc + projection_code, keep=True) projection_fun = mod.get_function("projection") projection_fun(weights_gpu.gpudata, fs_gpu.gpudata, basis_gpu.gpudata, np.uint32(basis_length), block=block_size, grid=grid_size)
def hsv2rgb(hsv): hsv = np.array(hsv) input_shape = hsv.shape hsv = hsv.reshape(-1, 3) h, s, v = hsv[:, 0] / 255, hsv[:, 1] / 255, hsv[:, 2] i = np.uint32(h * 6.0) # pylint: disable=no-member f = (h * 6.0) - i p = v * (1.0 - s) q = v * (1.0 - s * f) t = v * (1.0 - s * (1.0 - f)) i = i % 6 rgb = np.zeros_like(hsv, np.uint8) v, t, p, q = v.reshape(-1, 1), t.reshape(-1, 1), p.reshape(-1, 1), q.reshape(-1, 1) # This could probably be much faster if replaced with np.choose rgb[i == 0] = np.hstack([v, t, p])[i == 0] rgb[i == 1] = np.hstack([q, v, p])[i == 1] rgb[i == 2] = np.hstack([p, v, t])[i == 2] rgb[i == 3] = np.hstack([p, q, v])[i == 3] rgb[i == 4] = np.hstack([t, p, v])[i == 4] rgb[i == 5] = np.hstack([v, p, q])[i == 5] rgb[s == 0.0] = np.hstack([v, v, v])[s == 0.0] return rgb.reshape(input_shape)
def table(self): """Return a 2D array of UInt32 representing the sequence""" table = np.zeros((len(self.sequence_items), 6), dtype=np.uint32) for ct, entry in enumerate(self.sequence_items): control_word = SequenceControlWord(\ init_marker_sequence = 1 if ct==0 else 0, \ end_marker_sequence = 1 if ct==(len(self.sequence_items) - 1) else 0, \ marker_enable = 1 if isinstance(entry, WaveformEntry) else 0, \ data_cmd_sel = 1 if isinstance(entry, IdleEntry) else 0 \ ) table[ct,0] = control_word.packed table[ct,1] = self.sequence_loop_ct if ct==0 else 0 if isinstance(entry, WaveformEntry): table[ct, 2] = entry.loop_ct table[ct, 3] = entry.segment_id table[ct, 5] = 0xffffffff elif isinstance(entry, IdleEntry): table[ct, 3] = entry.dac_level() table[ct, 4] = entry.length else: raise TypeError("Unhandled sequence entry type") return table
def generate_ds2_data(max_length, str_w, nout, nbands, batch_size, num_iter): frame_stride = 0.01 # seconds, hard-coded value in make_aeon_dataloaders max_utt_len = ((int(max_length / frame_stride) - 1) // str_w) + 1 max_lbl_len = (max_utt_len - 1) // 2 train_set, eval_set = make_fake_dataloader(nbands, max_lbl_len, max_utt_len, nout, batch_size, num_iter) inputs = train_set.make_placeholders() if "audio_length" not in inputs: inputs["audio_length"] = ng.placeholder([ax.N], dtype=np.uint32) if "trans_length" not in inputs: inputs["trans_length"] = ng.placeholder([ax.N], dtype=np.uint32) return inputs, train_set, eval_set
def _reads(self, addr, length): if length > 65535: length = 65535 self.logger.warning("Maximum read-length is %d", length) header = b'r' + bytes(bytearray([0, length & 0xFF, (length >> 8) & 0xFF, addr & 0xFF, (addr >> 8) & 0xFF, (addr >> 16) & 0xFF, (addr >> 24) & 0xFF])) self.socket.send(header) data = self.socket.recv(length * 4 + 8) while (len(data) < length * 4 + 8): data += self.socket.recv(length * 4 - len(data) + 8) if data[:8] == header: # check for in-sync transmission return np.frombuffer(data[8:], dtype=np.uint32) else: # error handling self.logger.error("Wrong control sequence from server: %s", data[:8]) self.emptybuffer() return None
def _writes(self, addr, values): values = values[:65535 - 2] length = len(values) header = b'w' + bytes(bytearray([0, length & 0xFF, (length >> 8) & 0xFF, addr & 0xFF, (addr >> 8) & 0xFF, (addr >> 16) & 0xFF, (addr >> 24) & 0xFF])) # send header+body self.socket.send(header + np.array(values, dtype=np.uint32).tobytes()) if self.socket.recv(8) == header: # check for in-sync transmission return True # indicate successful write else: # error handling self.logger.error("Error: wrong control sequence from server") self.emptybuffer() return None
def numpy2bifrost(dtype): if dtype == np.int8: return _bf.BF_DTYPE_I8 elif dtype == np.int16: return _bf.BF_DTYPE_I16 elif dtype == np.int32: return _bf.BF_DTYPE_I32 elif dtype == np.uint8: return _bf.BF_DTYPE_U8 elif dtype == np.uint16: return _bf.BF_DTYPE_U16 elif dtype == np.uint32: return _bf.BF_DTYPE_U32 elif dtype == np.float16: return _bf.BF_DTYPE_F16 elif dtype == np.float32: return _bf.BF_DTYPE_F32 elif dtype == np.float64: return _bf.BF_DTYPE_F64 elif dtype == np.float128: return _bf.BF_DTYPE_F128 elif dtype == ci8: return _bf.BF_DTYPE_CI8 elif dtype == ci16: return _bf.BF_DTYPE_CI16 elif dtype == ci32: return _bf.BF_DTYPE_CI32 elif dtype == cf16: return _bf.BF_DTYPE_CF16 elif dtype == np.complex64: return _bf.BF_DTYPE_CF32 elif dtype == np.complex128: return _bf.BF_DTYPE_CF64 elif dtype == np.complex256: return _bf.BF_DTYPE_CF128 else: raise ValueError("Unsupported dtype: " + str(dtype))
def numpy2string(dtype): if dtype == np.int8: return 'i8' elif dtype == np.int16: return 'i16' elif dtype == np.int32: return 'i32' elif dtype == np.int64: return 'i64' elif dtype == np.uint8: return 'u8' elif dtype == np.uint16: return 'u16' elif dtype == np.uint32: return 'u32' elif dtype == np.uint64: return 'u64' elif dtype == np.float16: return 'f16' elif dtype == np.float32: return 'f32' elif dtype == np.float64: return 'f64' elif dtype == np.float128: return 'f128' elif dtype == np.complex64: return 'cf32' elif dtype == np.complex128: return 'cf64' elif dtype == np.complex256: return 'cf128' else: raise TypeError("Unsupported dtype: " + str(dtype))
def train(net_shapes, net_params, optimizer, utility, pool): # pass seed instead whole noise matrix to parallel will save your time noise_seed = np.random.randint(0, 2 ** 32 - 1, size=N_KID, dtype=np.uint32).repeat(2) # mirrored sampling # distribute training in parallel jobs = [pool.apply_async(get_reward, (net_shapes, net_params, env, CONFIG['ep_max_step'], CONFIG['continuous_a'], [noise_seed[k_id], k_id], )) for k_id in range(N_KID*2)] rewards = np.array([j.get() for j in jobs]) kids_rank = np.argsort(rewards)[::-1] # rank kid id by reward cumulative_update = np.zeros_like(net_params) # initialize update values for ui, k_id in enumerate(kids_rank): np.random.seed(noise_seed[k_id]) # reconstruct noise using seed cumulative_update += utility[ui] * sign(k_id) * np.random.randn(net_params.size) gradients = optimizer.get_gradients(cumulative_update/(2*N_KID*SIGMA)) return net_params + gradients, rewards