本文整理汇总了Python中numpy.getbuffer方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.getbuffer方法的具体用法?Python numpy.getbuffer怎么用?Python numpy.getbuffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.getbuffer方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: numpy_buffer
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import getbuffer [as 别名]
def numpy_buffer(ndarray):
"""Creates a buffer from c_contiguous numpy ndarray."""
# Credits to: https://git.io/fjC5g
if isinstance(ndarray, (pd.Series, pd.DataFrame)):
ndarray = ndarray.values
if ndarray.flags.c_contiguous:
obj_c_contiguous = ndarray
elif ndarray.flags.f_contiguous:
obj_c_contiguous = ndarray.T
else:
obj_c_contiguous = ndarray.flatten()
obj_c_contiguous = obj_c_contiguous.view(np.uint8)
if hasattr(np, "getbuffer"):
return np.getbuffer(obj_c_contiguous)
else:
return memoryview(obj_c_contiguous)
示例2: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import getbuffer [as 别名]
def __init__(self, hash_name='md5', coerce_mmap=False):
"""
Parameters
----------
hash_name: string
The hash algorithm to be used
coerce_mmap: boolean
Make no difference between np.memmap and np.ndarray
objects.
"""
self.coerce_mmap = coerce_mmap
Hasher.__init__(self, hash_name=hash_name)
# delayed import of numpy, to avoid tight coupling
import numpy as np
self.np = np
if hasattr(np, 'getbuffer'):
self._getbuffer = np.getbuffer
else:
self._getbuffer = memoryview
示例3: hash_from_code
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import getbuffer [as 别名]
def hash_from_code(msg):
try:
return hashlib.md5(msg).hexdigest()
except TypeError:
assert isinstance(msg, numpy.ndarray)
return hashlib.md5(numpy.getbuffer(msg)).hexdigest()
示例4: setUp
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import getbuffer [as 别名]
def setUp(self):
MNIST_IMAGE_MAGIC = 2051
MNIST_LABEL_MAGIC = 2049
numpy.random.seed(9 + 5 + 2015)
self.train_features_mock = numpy.random.randint(
0, 256, (10, 1, 28, 28)).astype('uint8')
self.train_targets_mock = numpy.random.randint(
0, 10, (10, 1)).astype('uint8')
self.test_features_mock = numpy.random.randint(
0, 256, (10, 1, 28, 28)).astype('uint8')
self.test_targets_mock = numpy.random.randint(
0, 10, (10, 1)).astype('uint8')
self.tempdir = tempfile.mkdtemp()
self.train_images_path = os.path.join(
self.tempdir, 'train-images-idx3-ubyte.gz')
self.train_labels_path = os.path.join(
self.tempdir, 'train-labels-idx1-ubyte.gz')
self.test_images_path = os.path.join(
self.tempdir, 't10k-images-idx3-ubyte.gz')
self.test_labels_path = os.path.join(
self.tempdir, 't10k-labels-idx1-ubyte.gz')
self.wrong_images_path = os.path.join(self.tempdir, 'wrong_images.gz')
self.wrong_labels_path = os.path.join(self.tempdir, 'wrong_labels.gz')
with gzip.open(self.train_images_path, 'wb') as f:
f.write(struct.pack('>iiii', *(MNIST_IMAGE_MAGIC, 10, 28, 28)))
f.write(getbuffer(self.train_features_mock.flatten()))
with gzip.open(self.train_labels_path, 'wb') as f:
f.write(struct.pack('>ii', *(MNIST_LABEL_MAGIC, 10)))
f.write(getbuffer(self.train_targets_mock.flatten()))
with gzip.open(self.test_images_path, 'wb') as f:
f.write(struct.pack('>iiii', *(MNIST_IMAGE_MAGIC, 10, 28, 28)))
f.write(getbuffer(self.test_features_mock.flatten()))
with gzip.open(self.test_labels_path, 'wb') as f:
f.write(struct.pack('>ii', *(MNIST_LABEL_MAGIC, 10)))
f.write(getbuffer(self.test_targets_mock.flatten()))
with gzip.open(self.wrong_images_path, 'wb') as f:
f.write(struct.pack('>iiii', *(2000, 10, 28, 28)))
with gzip.open(self.wrong_labels_path, 'wb') as f:
f.write(struct.pack('>ii', *(2000, 10)))
示例5: callback
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import getbuffer [as 别名]
def callback(in_data, frame_count, time_info, status):
global lock, debug, stack, channels, prefix, current_channel, current_value
with lock:
begsample = 0
endsample = min(frame_count, stack.shape[0])
dat = stack[begsample:endsample]
# add zero-padding if required
pad = np.zeros((frame_count - endsample, channels), dtype=np.float32)
dat = np.concatenate((dat, pad), axis=0)
# remove the current samples from the stack
stack = stack[endsample:]
if stack.shape[0] == 0 and current_channel != None:
# send a trigger to indicate that the sample finished playing
patch.setvalue("%s.%s" % (finished, current_channel), current_value)
current_channel = None
current_value = 0
try:
# this is for Python 2
buf = np.getbuffer(dat)
except:
# this is for Python 3
buf = dat.tobytes()
return buf, pyaudio.paContinue
示例6: array_to_blob
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import getbuffer [as 别名]
def array_to_blob(array):
if IS_PYTHON3:
return array.tostring()
else:
return np.getbuffer(array)
示例7: filePlayerCallback
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import getbuffer [as 别名]
def filePlayerCallback(self, in_data, numFrames, time_info, status):
startTime = tm.time()
if self.sampleIndex+numFrames >= self.numFrames:
self.sampleIndex = 0
inputBuffer = self.samples[self.sampleIndex*self.bytesPerFrameAllChannels:(self.sampleIndex+numFrames)*self.bytesPerFrameAllChannels]
inputIntArray = np.frombuffer(inputBuffer, dtype='<i2')
self.inputFrames[:] = pcm2float(inputIntArray).reshape(-1, self.numChannels).T
self.sampleIndex += numFrames
#logging.info('AudioStreamProcessor: setting processFramesEvent')
self.processFramesDoneEvent.clear()
self.processFramesEvent.set()
#logging.info('AudioStreamProcessor: waiting for processFramesDoneEvent')
self.processFramesDoneEvent.wait()
#logging.info('AudioStreamProcessor: done waiting for processFramesDoneEvent')
outputIntArray = float2pcm(self.outputFrames.T.flatten())
try:
outputBuffer = np.getbuffer(outputIntArray)
except:
outputBuffer = outputIntArray.tobytes()
self.processingTimes.append(tm.time() - startTime)
return outputBuffer, self.paContinue
示例8: callback
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import getbuffer [as 别名]
def callback(in_data, frame_count, time_info, status):
global stack, window, firstsample, stretch, inputrate, outputrate, outputblock, prevoutput, b, a, zi
now = time.time()
duration = now - prevoutput
prevoutput = now
if outputblock > 5 and duration > 0:
old = outputrate
new = frame_count / duration
if old/new > 0.1 or old/new < 10:
outputrate = (1 - lrate) * old + lrate * new
# estimate the required stretch between input and output rate
old = stretch
new = outputrate / inputrate
stretch = (1 - lrate) * old + lrate * new
# linearly interpolate the selection of samples, i.e. stretch or compress the time axis when needed
begsample = firstsample
endsample = round(firstsample + frame_count / stretch)
selection = np.linspace(begsample, endsample, frame_count).astype(np.int32)
# remember where to continue the next time
firstsample = (endsample + 1) % window
with lock:
lenstack = len(stack)
if endsample > (window - 1) and lenstack>1:
# the selection passes the boundary, concatenate the first two blocks
dat = np.append(stack[0], stack[1], axis=0)
elif lenstack>0:
# the selection can be made in the first block
dat = stack[0]
# select the samples that will be written to the audio card
try:
dat = dat[selection]
except:
dat = np.zeros((frame_count,1), dtype=float)
if endsample > window:
# it is time to remove data from the stack
with lock:
stack = stack[1:] # remove the first block
try:
# this is for Python 2
buf = np.getbuffer(dat)
except:
# this is for Python 3
buf = dat.tobytes()
outputblock += 1
return buf, pyaudio.paContinue