Example #1
0
    def readarray(self, size=None, offset=0, dtype=np.uint8, shape=None):
        """
        Similar to file.read(), but returns the contents of the underlying
        file as a numpy array (or mmap'd array if memmap=True) rather than a
        string.

        Usually it's best not to use the `size` argument with this method, but
        it's provided for compatibility.
        """

        if not hasattr(self.__file, 'read'):
            raise EOFError

        if not isinstance(dtype, np.dtype):
            dtype = np.dtype(dtype)

        if size and size % dtype.itemsize != 0:
            raise ValueError('size %d not a multiple of %s' % (size, dtype))

        if isinstance(shape, int):
            shape = (shape, )

        if size and shape:
            actualsize = sum(dim * dtype.itemsize for dim in shape)
            if actualsize < size:
                raise ValueError('size %d is too few bytes for a %s array of '
                                 '%s' % (size, shape, dtype))
            if actualsize < size:
                raise ValueError('size %d is too many bytes for a %s array of '
                                 '%s' % (size, shape, dtype))

        if size and not shape:
            shape = (size // dtype.itemsize, )

        if not (size or shape):
            warnings.warn(
                'No size or shape given to readarray(); assuming a '
                'shape of (1,)', AstropyUserWarning)
            shape = (1, )

        if self.memmap:
            return Memmap(self.__file,
                          offset=offset,
                          mode=MEMMAP_MODES[self.mode],
                          dtype=dtype,
                          shape=shape).view(np.ndarray)
        else:
            count = reduce(lambda x, y: x * y, shape)
            pos = self.__file.tell()
            self.__file.seek(offset)
            data = _array_from_file(self.__file, dtype, count, '')
            data.shape = shape
            self.__file.seek(pos)
            return data
Example #2
0
    def readarray(self, size=None, offset=0, dtype=np.uint8, shape=None):
        """
        Similar to file.read(), but returns the contents of the underlying
        file as a numpy array (or mmap'd array if memmap=True) rather than a
        string.

        Usually it's best not to use the `size` argument with this method, but
        it's provided for compatibility.
        """

        if not hasattr(self._file, 'read'):
            raise EOFError

        if not isinstance(dtype, np.dtype):
            dtype = np.dtype(dtype)

        if size and size % dtype.itemsize != 0:
            raise ValueError('size {} not a multiple of {}'.format(size, dtype))

        if isinstance(shape, int):
            shape = (shape,)

        if not (size or shape):
            warnings.warn('No size or shape given to readarray(); assuming a '
                          'shape of (1,)', AstropyUserWarning)
            shape = (1,)

        if size and not shape:
            shape = (size // dtype.itemsize,)

        if size and shape:
            actualsize = np.prod(shape) * dtype.itemsize

            if actualsize < size:
                raise ValueError('size {} is too few bytes for a {} array of '
                                 '{}'.format(size, shape, dtype))
            if actualsize < size:
                raise ValueError('size {} is too many bytes for a {} array of '
                                 '{}'.format(size, shape, dtype))

        filepos = self._file.tell()

        try:
            if self.memmap:
                if self._mmap is None:
                    # Instantiate Memmap array of the file offset at 0 (so we
                    # can return slices of it to offset anywhere else into the
                    # file)
                    memmap = Memmap(self._file, mode=MEMMAP_MODES[self.mode],
                                    dtype=np.uint8)

                    # Now we immediately discard the memmap array; we are
                    # really just using it as a factory function to instantiate
                    # the mmap object in a convenient way (may later do away
                    # with this usage)
                    self._mmap = memmap.base

                    # Prevent dorking with self._memmap._mmap by memmap.__del__
                    # in Numpy 1.6 (see
                    # https://github.com/numpy/numpy/commit/dcc355a0b179387eeba10c95baf2e1eb21d417c7)
                    memmap._mmap = None
                    del memmap

                return np.ndarray(shape=shape, dtype=dtype, offset=offset,
                                  buffer=self._mmap)
            else:
                count = reduce(operator.mul, shape)
                self._file.seek(offset)
                data = _array_from_file(self._file, dtype, count, '')
                data.shape = shape
                return data
        finally:
            # Make sure we leave the file in the position we found it; on
            # some platforms (e.g. Windows) mmaping a file handle can also
            # reset its file pointer
            self._file.seek(filepos)
Example #3
0
    def readarray(self, size=None, offset=0, dtype=np.uint8, shape=None):
        """
        Similar to file.read(), but returns the contents of the underlying
        file as a numpy array (or mmap'd array if memmap=True) rather than a
        string.

        Usually it's best not to use the `size` argument with this method, but
        it's provided for compatibility.
        """

        if not hasattr(self._file, 'read'):
            raise EOFError

        if not isinstance(dtype, np.dtype):
            dtype = np.dtype(dtype)

        if size and size % dtype.itemsize != 0:
            raise ValueError('size %d not a multiple of %s' % (size, dtype))

        if isinstance(shape, int):
            shape = (shape, )

        if not (size or shape):
            warnings.warn('No size or shape given to readarray(); assuming a '
                          'shape of (1,)')
            shape = (1, )

        if size and not shape:
            shape = (size // dtype.itemsize, )

        if size and shape:
            actualsize = np.prod(shape) * dtype.itemsize

            if actualsize < size:
                raise ValueError('size %d is too few bytes for a %s array of '
                                 '%s' % (size, shape, dtype))
            if actualsize < size:
                raise ValueError('size %d is too many bytes for a %s array of '
                                 '%s' % (size, shape, dtype))

        if self.memmap:
            if self._mmap is None:
                # Instantiate Memmap array of the file offset at 0
                # (so we can return slices of it to offset anywhere else into
                # the file)
                memmap = Memmap(self._file,
                                mode=MEMMAP_MODES[self.mode],
                                dtype=np.uint8)

                # Now we immediately discard the memmap array; we are really
                # just using it as a factory function to instantiate the mmap
                # object in a convenient way (may later do away with this
                # usage)
                self._mmap = memmap.base

                # Prevent dorking with self._memmap._mmap by memmap.__del__ in
                # Numpy 1.6 (see
                # https://github.com/numpy/numpy/commit/dcc355a0b179387eeba10c95baf2e1eb21d417c7)
                memmap._mmap = None
                del memmap

            return np.ndarray(shape=shape,
                              dtype=dtype,
                              offset=offset,
                              buffer=self._mmap)
        else:
            count = reduce(lambda x, y: x * y, shape)
            pos = self._file.tell()
            self._file.seek(offset)
            data = _array_from_file(self._file, dtype, count, '')
            data.shape = shape
            self._file.seek(pos)
            return data