我们从Python开源项目中,提取了以下44个代码示例,用于说明如何使用numpy.min_scalar_type()。
def create(predict_fn, word_representations, batch_size, window_size, vocabulary_size, result_callback): assert result_callback is not None instance_dtype = np.min_scalar_type(vocabulary_size - 1) logging.info('Instance elements will be stored using %s.', instance_dtype) if result_callback.should_average_input(): batcher = EmbeddingMapper( predict_fn, word_representations, result_callback) else: batcher = WordBatcher( predict_fn, batch_size, window_size, instance_dtype, result_callback) return batcher
def test_usigned_shortshort(self): dt = np.min_scalar_type(2**8-1) wanted = np.dtype('uint8') assert_equal(wanted, dt)
def test_usigned_short(self): dt = np.min_scalar_type(2**16-1) wanted = np.dtype('uint16') assert_equal(wanted, dt)
def test_usigned_int(self): dt = np.min_scalar_type(2**32-1) wanted = np.dtype('uint32') assert_equal(wanted, dt)
def test_usigned_longlong(self): dt = np.min_scalar_type(2**63-1) wanted = np.dtype('uint64') assert_equal(wanted, dt)
def test_object(self): dt = np.min_scalar_type(2**64) wanted = np.dtype('O') assert_equal(wanted, dt)
def _linear_loc(coords, shape, signed=False): n = reduce(operator.mul, shape, 1) if signed: n = -n dtype = np.min_scalar_type(n) out = np.zeros(coords.shape[1], dtype=dtype) tmp = np.zeros(coords.shape[1], dtype=dtype) strides = 1 for i, d in enumerate(shape[::-1]): # out += self.coords[-(i + 1), :].astype(dtype) * strides np.multiply(coords[-(i + 1), :], strides, out=tmp, dtype=dtype) np.add(tmp, out, out=out) strides *= d return out
def reshape(self, shape): if self.shape == shape: return self if any(d == -1 for d in shape): extra = int(np.prod(self.shape) / np.prod([d for d in shape if d != -1])) shape = tuple([d if d != -1 else extra for d in shape]) if self.shape == shape: return self if self._cache is not None: for sh, value in self._cache['reshape']: if sh == shape: return value # TODO: this np.prod(self.shape) enforces a 2**64 limit to array size linear_loc = self.linear_loc() coords = np.empty((len(shape), self.nnz), dtype=np.min_scalar_type(max(shape))) strides = 1 for i, d in enumerate(shape[::-1]): coords[-(i + 1), :] = (linear_loc // strides) % d strides *= d result = COO(coords, self.data, shape, has_duplicates=self.has_duplicates, sorted=self.sorted, cache=self._cache is not None) if self._cache is not None: self._cache['reshape'].append((shape, result)) return result
def rasterize_pctcover_geom(geom, shape, affine, scale=None, all_touched=False): """ Parameters ---------- geom: GeoJSON geometry shape: desired shape affine: desired transform scale: scale at which to generate percent cover estimate Returns ------- ndarray: float32 """ if scale is None: scale = 10 min_dtype = min_scalar_type(scale ** 2) new_affine = Affine(affine[0]/scale, 0, affine[2], 0, affine[4]/scale, affine[5]) new_shape = (shape[0] * scale, shape[1] * scale) rv_array = rasterize_geom(geom, new_shape, new_affine, all_touched=all_touched) rv_array = rebin_sum(rv_array, shape, min_dtype) return rv_array.astype('float32') / (scale**2)
def deposit(self, frequencies): # TODO: adjust signature with UniversalData frequencies = np.array(frequencies, dtype=self.composition_type) # freqencies must not exceed 4,294,967,295 maxtype = np.min_scalar_type(np.max(frequencies)) row = frequencies.astype(maxtype) # TODO: support sparse features self._frequencies.append(row)
def render(self): # Convert data to QImage for display. profile = debug.Profiler() if self.image is None or self.image.size == 0: return if isinstance(self.lut, collections.Callable): lut = self.lut(self.image) else: lut = self.lut if self.autoDownsample: # reduce dimensions of image based on screen resolution o = self.mapToDevice(QtCore.QPointF(0,0)) x = self.mapToDevice(QtCore.QPointF(1,0)) y = self.mapToDevice(QtCore.QPointF(0,1)) w = Point(x-o).length() h = Point(y-o).length() if w == 0 or h == 0: self.qimage = None return xds = max(1, int(1.0 / w)) yds = max(1, int(1.0 / h)) axes = [1, 0] if self.axisOrder == 'row-major' else [0, 1] image = fn.downsample(self.image, xds, axis=axes[0]) image = fn.downsample(image, yds, axis=axes[1]) self._lastDownsample = (xds, yds) else: image = self.image # if the image data is a small int, then we can combine levels + lut # into a single lut for better performance levels = self.levels if levels is not None and levels.ndim == 1 and image.dtype in (np.ubyte, np.uint16): if self._effectiveLut is None: eflsize = 2**(image.itemsize*8) ind = np.arange(eflsize) minlev, maxlev = levels levdiff = maxlev - minlev levdiff = 1 if levdiff == 0 else levdiff # don't allow division by 0 if lut is None: efflut = fn.rescaleData(ind, scale=255./levdiff, offset=minlev, dtype=np.ubyte) else: lutdtype = np.min_scalar_type(lut.shape[0]-1) efflut = fn.rescaleData(ind, scale=(lut.shape[0]-1)/levdiff, offset=minlev, dtype=lutdtype, clip=(0, lut.shape[0]-1)) efflut = lut[efflut] self._effectiveLut = efflut lut = self._effectiveLut levels = None # Assume images are in column-major order for backward compatibility # (most images are in row-major order) if self.axisOrder == 'col-major': image = image.transpose((1, 0, 2)[:image.ndim]) argb, alpha = fn.makeARGB(image, lut=lut, levels=levels) self.qimage = fn.makeQImage(argb, alpha, transpose=False)
def __init__(self, coords, data=None, shape=None, has_duplicates=True, sorted=False, cache=False): self._cache = None if cache: self.enable_caching() if data is None: # {(i, j, k): x, (i, j, k): y, ...} if isinstance(coords, dict): coords = list(coords.items()) has_duplicates = False if isinstance(coords, np.ndarray): result = COO.from_numpy(coords) self.coords = result.coords self.data = result.data self.has_duplicates = result.has_duplicates self.sorted = result.sorted self.shape = result.shape return # [] if not coords: data = [] coords = [] # [((i, j, k), value), (i, j, k), value), ...] elif isinstance(coords[0][0], Iterable): if coords: assert len(coords[0]) == 2 data = [x[1] for x in coords] coords = [x[0] for x in coords] coords = np.asarray(coords).T # (data, (row, col, slab, ...)) else: data = coords[0] coords = np.stack(coords[1], axis=0) self.data = np.asarray(data) self.coords = np.asarray(coords) if self.coords.ndim == 1: self.coords = self.coords[None, :] if shape and not np.prod(self.coords.shape): self.coords = np.zeros((len(shape), 0), dtype=np.uint64) if shape is None: if self.coords.nbytes: shape = tuple((self.coords.max(axis=1) + 1).tolist()) else: shape = () self.shape = tuple(shape) if self.shape: dtype = np.min_scalar_type(max(self.shape)) else: dtype = np.int_ self.coords = self.coords.astype(dtype) assert not self.shape or len(data) == self.coords.shape[1] self.has_duplicates = has_duplicates self.sorted = sorted
def _get_expanded_coords_data(coords, data, params, broadcast_shape): """ Expand coordinates/data to broadcast_shape. Does most of the heavy lifting for broadcast_to. Produces sorted output for sorted inputs. Parameters ---------- coords : np.ndarray The coordinates to expand. data : np.ndarray The data corresponding to the coordinates. params : list The broadcast parameters. broadcast_shape : tuple[int] The shape to broadcast to. Returns ------- expanded_coords : np.ndarray List of 1-D arrays. Each item in the list has one dimension of coordinates. expanded_data : np.ndarray The data corresponding to expanded_coords. """ first_dim = -1 expand_shapes = [] for d, p, l in zip(range(len(broadcast_shape)), params, broadcast_shape): if p and first_dim == -1: expand_shapes.append(coords.shape[1]) first_dim = d if not p: expand_shapes.append(l) all_idx = COO._cartesian_product(*(np.arange(d, dtype=np.min_scalar_type(d - 1)) for d in expand_shapes)) dt = np.result_type(*(np.min_scalar_type(l - 1) for l in broadcast_shape)) false_dim = 0 dim = 0 expanded_coords = np.empty((len(broadcast_shape), all_idx.shape[1]), dtype=dt) expanded_data = data[all_idx[first_dim]] for d, p, l in zip(range(len(broadcast_shape)), params, broadcast_shape): if p: expanded_coords[d] = coords[dim, all_idx[first_dim]] else: expanded_coords[d] = all_idx[false_dim + (d > first_dim)] false_dim += 1 if p is not None: dim += 1 return np.asarray(expanded_coords), np.asarray(expanded_data) # (c) senderle # Taken from https://stackoverflow.com/a/11146645/774273 # License: https://creativecommons.org/licenses/by-sa/3.0/