def decode(self, buf, out=None): buf = ensure_contiguous_ndarray(buf) if out is not None: out = ensure_contiguous_ndarray(out) tiled = jpeg_decode(buf) return ndarray_copy(tiled, out)
def test_ensure_contiguous_ndarray_writeable(): # check that the writeability of the underlying buffer is preserved for writeable in [False, True]: a = np.arange(100) a.setflags(write=writeable) m = ensure_contiguous_ndarray(a) assert m.flags.writeable == writeable m = ensure_contiguous_ndarray(memoryview(a)) assert m.flags.writeable == writeable
def test_ensure_contiguous_ndarray_shares_memory(): typed_bufs = [ ('u', 1, b'adsdasdas'), ('u', 1, bytes(20)), ('i', 8, np.arange(100, dtype=np.int64)), ('f', 8, np.linspace(0, 1, 100, dtype=np.float64)), ('i', 4, array.array('i', b'qwertyuiqwertyui')), ('u', 4, array.array('I', b'qwertyuiqwertyui')), ('f', 4, array.array('f', b'qwertyuiqwertyui')), ('f', 8, array.array('d', b'qwertyuiqwertyui')), ('i', 1, array.array('b', b'qwertyuiqwertyui')), ('u', 1, array.array('B', b'qwertyuiqwertyui')), ('u', 1, mmap.mmap(-1, 10)) ] for expected_kind, expected_itemsize, buf in typed_bufs: a = ensure_contiguous_ndarray(buf) assert isinstance(a, np.ndarray) assert expected_kind == a.dtype.kind if isinstance(buf, array.array): assert buf.itemsize == a.dtype.itemsize else: assert expected_itemsize == a.dtype.itemsize if PY2: # pragma: py3 no cover assert np.shares_memory(a, np.getbuffer(buf)) else: # pragma: py2 no cover assert np.shares_memory(a, memoryview(buf))
def encode(self, buf): # normalise input buf = ensure_contiguous_ndarray(buf) # do compression return _bz2.compress(buf, self.level)
def decode(self, buf, out=None): # normalise inputs buf = ensure_contiguous_ndarray(buf) if out is not None: out = ensure_contiguous_ndarray(out) # N.B., bz2 cannot handle ndarray directly because of truth testing issues buf = memoryview(buf) # do decompression dec = _bz2.decompress(buf) # handle destination - Python standard library bz2 module does not # support direct decompression into buffer, so we have to copy into # out if given return ndarray_copy(dec, out)
def decode(self, buf, out=None): """The method to decode a jpeg image into a raw format. Parameters ---------- buf : contiguous_ndarray The jpeg image to be decoded into raw format. out : contiguous_ndarray, optional Another location to write the raw image to. Returns ------- ndarray The image in raw format """ buf = ensure_contiguous_ndarray(buf) if out is not None: out = ensure_contiguous_ndarray(out) tiled = jpeg_decode(buf) return ndarray_copy(tiled, out)
def decode(self, buf, out=None): buf = ensure_contiguous_ndarray(buf) fn = tempfile.mktemp(suffix="grib2") buf.tofile(fn) # do decode ds = cfgrib.open_file(fn) data = ds.variables[self.var].data if hasattr(data, "build_array"): data = data.build_array() if out is not None: return ndarray_copy(data, out) else: return data
def test_ensure_contiguous_ndarray_invalid_inputs(): # object array not allowed a = np.array([u'Xin chào thế giới'], dtype=object) for e in [a, memoryview(a)]: with pytest.raises(TypeError): ensure_contiguous_ndarray(e) # non-contiguous arrays not allowed with pytest.raises(ValueError): ensure_contiguous_ndarray(np.arange(100)[::2]) # unicode array.array not allowed a = array.array('u', u'qwertyuiqwertyui') with pytest.raises(TypeError): ensure_contiguous_ndarray(a) if PY2: # pragma: py3 no cover # char array.array not allowed a = array.array('c', b'qwertyuiqwertyui') with pytest.raises(TypeError): ensure_contiguous_ndarray(a)
def test_ensure_contiguous_ndarray_max_buffer_size(): for max_buffer_size in [4, 64, 1024]: ensure_contiguous_ndarray( np.zeros(max_buffer_size - 1, dtype=np.int8), max_buffer_size) ensure_contiguous_ndarray( np.zeros(max_buffer_size, dtype=np.int8), max_buffer_size) buffers = [ bytes(b"x" * (max_buffer_size + 1)), np.zeros(max_buffer_size + 1, dtype=np.int8), np.zeros(max_buffer_size + 2, dtype=np.int8), np.zeros(max_buffer_size, dtype=np.int16), np.zeros(max_buffer_size, dtype=np.int32)] for buf in buffers: with pytest.raises(ValueError): ensure_contiguous_ndarray(buf, max_buffer_size=max_buffer_size)
def ensure_str(s): if not isinstance(s, str): s = ensure_contiguous_ndarray(s) s = codecs.decode(s, 'ascii') return s
def ensure_text_type(s): if not isinstance(s, text_type): s = ensure_contiguous_ndarray(s) s = codecs.decode(s, 'ascii') return s