Python numpy.getbuffer() Examples

The following are 16 code examples for showing how to use numpy.getbuffer(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: heamy   Author: rushter   File: cache.py    License: MIT License 6 votes vote down vote up
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) 
Example 2
Project: estimators   Author: fridiculous   File: hashing.py    License: MIT License 6 votes vote down vote up
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 
Example 3
Project: mlens   Author: flennerhag   File: hashing.py    License: MIT License 6 votes vote down vote up
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 
Example 4
Project: django-estimators   Author: fridiculous   File: hashing.py    License: MIT License 6 votes vote down vote up
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 
Example 5
Project: Splunking-Crime   Author: nccgroup   File: hashing.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
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 
Example 6
Project: abu   Author: bbfamily   File: hashing.py    License: GNU General Public License v3.0 6 votes vote down vote up
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 
Example 7
Project: SqueezeMeta   Author: jtamames   File: hashing.py    License: GNU General Public License v3.0 6 votes vote down vote up
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 
Example 8
Project: twitter-stock-recommendation   Author: alvarobartt   File: hashing.py    License: MIT License 6 votes vote down vote up
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 
Example 9
Project: D-VAE   Author: muhanzhang   File: utils.py    License: MIT License 5 votes vote down vote up
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() 
Example 10
Project: attention-lvcsr   Author: rizar   File: utils.py    License: MIT License 5 votes vote down vote up
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() 
Example 11
Project: attention-lvcsr   Author: rizar   File: test_converters.py    License: MIT License 5 votes vote down vote up
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))) 
Example 12
Project: fuel   Author: mila-iqia   File: test_converters.py    License: MIT License 5 votes vote down vote up
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))) 
Example 13
Project: eegsynth   Author: eegsynth   File: sampler.py    License: GNU General Public License v3.0 5 votes vote down vote up
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 
Example 14
Project: hfnet   Author: ethz-asl   File: db_handling.py    License: MIT License 5 votes vote down vote up
def array_to_blob(array):
    if IS_PYTHON3:
        return array.tostring()
    else:
        return np.getbuffer(array) 
Example 15
Project: gcc-nmf   Author: seanwood   File: audioProcessor.py    License: MIT License 5 votes vote down vote up
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 
Example 16
Project: eegsynth   Author: eegsynth   File: outputaudio.py    License: GNU General Public License v3.0 4 votes vote down vote up
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