Example #1
0
    def get_raw_data(self, index):

        from boost.python import streambuf
        from scitbx.array_family import flex

        # is this image a type we can read?
        assert self._data_type in ["h", "f", "B", "l", "b", "H", "I", "d"]

        with FormatGatanDM4.open_file(self._image_file, "rb") as f:
            f.seek(self._data_offset)

            skip_bytes = index * self._image_num_elements * self._data_size
            f.seek(skip_bytes, whence=1)

            if self._data_type == "f":
                from dxtbx import read_float32

                raw_data = read_float32(streambuf(f), self._image_num_elements)
            elif self._data_type == "B":
                from dxtbx import read_uint8

                raw_data = read_uint8(streambuf(f), self._image_num_elements)
            elif self._data_type == "h":
                from dxtbx import read_int16

                raw_data = read_int16(streambuf(f), self._image_num_elements)
            elif self._data_type == "H":
                from dxtbx import read_uint16

                raw_data = read_uint16(streambuf(f), self._image_num_elements)
            elif self._data_type == "l":
                from dxtbx import read_int32

                raw_data = read_int32(streambuf(f), self._image_num_elements)
            elif self._data_type == "I":
                from dxtbx import read_uint32

                raw_data = read_uint32(streambuf(f), self._image_num_elements)

            # no C++ reader for remaining types (should be unusual anyway)
            else:
                vals = struct.unpack(
                    self._byteord + self._data_type * self._image_num_elements,
                    f.read(self._data_size * self._image_num_elements),
                )
                if self._data_type == "d":
                    raw_data = flex.double(vals)
                if self._data_type in ["b", "?"]:
                    raw_data = flex.int(vals)

        raw_data.reshape(flex.grid(self._image_size[1], self._image_size[0]))
        return raw_data
    def get_raw_data(self, index):

        from boost.python import streambuf
        from scitbx.array_family import flex

        # is this image a type we can read?
        assert self._data_type in ['h', 'f', 'B', 'l', 'b', 'H', 'I', 'd']

        with FormatGatanDM4.open_file(self._image_file, 'rb') as f:
            f.seek(self._data_offset)

            skip_bytes = index * self._image_num_elements * self._data_size
            f.seek(skip_bytes, whence=1)

            if self._data_type == 'f':
                from dxtbx import read_float32
                raw_data = read_float32(streambuf(f), self._image_num_elements)
            elif self._data_type == 'B':
                from dxtbx import read_uint8
                raw_data = read_uint8(streambuf(f), self._image_num_elements)
            elif self._data_type == 'h':
                from dxtbx import read_int16
                raw_data = read_int16(streambuf(f), self._image_num_elements)
            elif self._data_type == 'H':
                from dxtbx import read_uint16
                raw_data = read_uint16(streambuf(f), self._image_num_elements)
            elif self._data_type == 'l':
                from dxtbx import read_int32
                raw_data = read_int32(streambuf(f), self._image_num_elements)
            elif self._data_type == 'I':
                from dxtbx import read_uint32
                raw_data = read_uint32(streambuf(f), self._image_num_elements)

            # no C++ reader for remaining types (should be unusual anyway)
            else:
                vals = struct.unpack(
                    self._byteord + self._data_type * self._image_num_elements,
                    f.read(self._data_size * self._image_num_elements))
                if self._data_type == 'd':
                    raw_data = flex.double(vals)
                if self._data_type in ['b', '?']:
                    raw_data = flex.int(vals)

        raw_data.reshape(flex.grid(self._image_size[1], self._image_size[0]))
        return raw_data
Example #3
0
    def get_raw_data(self):
        """Get the pixel intensities (i.e. read the image and return as a
        flex array of integers.)"""

        # It is better to catch FORMAT 86 here and fail with a sensible error msg
        # as soon as something is attempted with the image data rather than in
        # the understand method. Otherwise the user gets FormatBruker reading the
        # image improperly but without failing
        if self.header_dict["FORMAT"] != "100":
            raise Sorry(
                "Only FORMAT 100 images from the Photon II are currently "
                "supported")

        f = self.open_file(self._image_file, "rb")
        header_size = int(self.header_dict["HDRBLKS"]) * 512
        f.read(header_size)

        if is_big_endian():
            read_2b = read_uint16_bs
            read_4b = read_uint32_bs
        else:
            read_2b = read_uint16
            read_4b = read_uint32

        # NPIXELB stores the number of bytes/pixel for the data and the underflow
        # table. We expect 1 byte for underflows and either 2 or 1 byte per pixel
        # for the data
        npixelb = [int(e) for e in self.header_dict["NPIXELB"].split()]
        assert npixelb[1] == 1

        if npixelb[0] == 1:
            read_data = read_uint8
        elif npixelb[0] == 2:
            read_data = read_2b
        else:
            raise IncorrectFormatError(
                "{0} bytes per pixel is not supported".format(npixelb[0]))

        nrows = int(self.header_dict["NROWS"].split()[0])
        ncols = int(self.header_dict["NCOLS"].split()[0])

        raw_data = read_data(streambuf(f), nrows * ncols)

        image_size = (nrows, ncols)
        raw_data.reshape(flex.grid(*image_size))

        (num_underflows, num_2b_overflows, num_4b_overflows) = [
            int(e) for e in self.header_dict["NOVERFL"].split()
        ]

        # read underflows
        if num_underflows > 0:
            # stored values are padded to a multiple of 16 bytes
            nbytes = num_underflows + 15 & ~(15)
            underflow_vals = read_uint8(streambuf(f), nbytes)[:num_underflows]
        else:
            underflow_vals = None

        # handle 2 byte overflows
        if num_2b_overflows > 0:
            # stored values are padded to a multiple of 16 bytes
            nbytes = num_2b_overflows * 2 + 15 & ~(15)
            overflow_vals = read_2b(streambuf(f),
                                    nbytes // 2)[:num_2b_overflows]
            overflow = flex.int(nrows * ncols, 0)
            sel = (raw_data == 255).as_1d()
            overflow.set_selected(sel, overflow_vals - 255)
            overflow.reshape(flex.grid(*image_size))
            raw_data += overflow

        # handle 4 byte overflows
        if num_4b_overflows > 0:
            # stored values are padded to a multiple of 16 bytes
            nbytes = num_4b_overflows * 4 + 15 & ~(15)
            overflow_vals = read_4b(streambuf(f),
                                    nbytes // 4)[:num_4b_overflows]
            overflow = flex.int(nrows * ncols, 0)
            sel = (raw_data == 65535).as_1d()
            overflow.set_selected(sel, overflow_vals - 65535)
            overflow.reshape(flex.grid(*image_size))
            raw_data += overflow

        # handle underflows
        if underflow_vals is not None:
            sel = (raw_data == 0).as_1d()
            underflow = flex.int(nrows * ncols, 0)
            underflow.set_selected(sel, underflow_vals)
            underflow.reshape(flex.grid(*image_size))
            raw_data += underflow

        # handle baseline. num_underflows == -1 means no baseline subtraction. See
        # https://github.com/cctbx/cctbx_project/files/1262952/BISFrameFileFormats.zip
        if num_underflows != -1:
            num_exposures = [int(e) for e in self.header_dict["NEXP"].split()]
            baseline = num_exposures[2]
            raw_data += baseline

        return raw_data
Example #4
0
    def get_raw_data(self):
        '''Get the pixel intensities (i.e. read the image and return as a
    flex array of integers.)'''

        from boost.python import streambuf
        from dxtbx import read_uint8, read_uint16, read_uint16_bs, read_uint32, read_uint32_bs
        from dxtbx import is_big_endian
        from scitbx.array_family import flex
        f = self.open_file(self._image_file, 'rb')
        header_size = int(self.header_dict['HDRBLKS']) * 512
        f.read(header_size)

        if is_big_endian():
            read_2b = read_uint16_bs
            read_4b = read_uint32_bs
        else:
            read_2b = read_uint16
            read_4b = read_uint32

        # NPIXELB stores the number of bytes/pixel for the data and the underflow
        # table. We expect 1 byte for underflows and either 2 or 1 byte per pixel
        # for the data
        npixelb = [int(e) for e in self.header_dict['NPIXELB'].split()]
        assert npixelb[1] == 1

        if npixelb[0] == 1:
            read_data = read_uint8
        elif npixelb[0] == 2:
            read_data = read_2b
        else:
            from dxtbx import IncorrectFormatError
            raise IncorrectFormatError(
                "{0} bytes per pixel is not supported".format(npixelb[0]))

        nrows = int(self.header_dict['NROWS'].split()[0])
        ncols = int(self.header_dict['NCOLS'].split()[0])

        raw_data = read_data(streambuf(f), nrows * ncols)

        image_size = (nrows, ncols)
        raw_data.reshape(flex.grid(*image_size))

        (num_underflows, num_2b_overflows, num_4b_overflows) = [
            int(e) for e in self.header_dict['NOVERFL'].split()
        ]

        # read underflows
        if num_underflows > 0:
            # stored values are padded to a multiple of 16 bytes
            nbytes = num_underflows + 15 & ~(15)
            underflow_vals = read_uint8(streambuf(f), nbytes)[:num_underflows]
        else:
            underflow_vals = None

        # handle 2 byte overflows
        if num_2b_overflows > 0:
            # stored values are padded to a multiple of 16 bytes
            nbytes = num_2b_overflows * 2 + 15 & ~(15)
            overflow_vals = read_2b(streambuf(f),
                                    nbytes // 2)[:num_2b_overflows]
            overflow = flex.int(nrows * ncols, 0)
            sel = (raw_data == 255).as_1d()
            overflow.set_selected(sel, overflow_vals - 255)
            overflow.reshape(flex.grid(*image_size))
            raw_data += overflow

        # handle 4 byte overflows
        if num_4b_overflows > 0:
            # stored values are padded to a multiple of 16 bytes
            nbytes = num_4b_overflows * 4 + 15 & ~(15)
            overflow_vals = read_4b(streambuf(f),
                                    nbytes // 4)[:num_4b_overflows]
            overflow = flex.int(nrows * ncols, 0)
            sel = (raw_data == 65535).as_1d()
            overflow.set_selected(sel, overflow_vals - 65535)
            overflow.reshape(flex.grid(*image_size))
            raw_data += overflow

        # handle underflows
        if underflow_vals is not None:
            sel = (raw_data == 0).as_1d()
            underflow = flex.int(nrows * ncols, 0)
            underflow.set_selected(sel, underflow_vals)
            underflow.reshape(flex.grid(*image_size))
            raw_data += underflow

        # handle baseline. num_underflows == -1 means no baseline subtraction. See
        # https://github.com/cctbx/cctbx_project/files/1262952/BISFrameFileFormats.zip
        if num_underflows != -1:
            num_exposures = [int(e) for e in self.header_dict['NEXP'].split()]
            baseline = num_exposures[2]
            raw_data += baseline

        return raw_data