Exemplo n.º 1
0
    def test_all_data(self):
        folder = 'testing_data'
        all_files = [
            join(folder, f) for f in listdir(folder) if isfile(join(folder, f))
        ]

        for f in all_files:
            if f.endswith(('nii.gz')):
                loaded_shape = image_loader.load_image_from_file(
                    f).get_data().shape
                print(loaded_shape)
                self.assertGreaterEqual(5, len(loaded_shape))
            else:
                with self.assertRaisesRegexp(ValueError, ''):
                    image_loader.load_image_from_file(f)
Exemplo n.º 2
0
    def shape(self):
        """
        This function read image shape info from the headers
        The lengths in the fifth dim of multiple images are summed
        as a multi-mod representation.
        The fourth dim corresponding to different time sequences
        is ignored.

        :return: a tuple of integers as image shape
        """
        if self._original_shape is None:
            try:
                self._original_shape = tuple(
                    load_image_from_file(_file, _loader).header['dim'][1:6]
                    for _file, _loader in zip(self.file_path, self.loader))
            except (IOError, KeyError, AttributeError, IndexError):
                tf.logging.fatal('unknown image shape from header %s',
                                 self.file_path)
                raise ValueError
            try:
                non_modality_shapes = set([
                    tuple(shape[:4].tolist()) for shape in self._original_shape
                ])
                assert len(non_modality_shapes) == 1
            except (TypeError, IndexError, AssertionError):
                tf.logging.fatal(
                    "could not combining multimodal images: "
                    "shapes not consistent %s -- %s", self.file_path,
                    self._original_shape)
                raise ValueError
            n_modalities = \
                np.sum([int(shape[4]) for shape in self._original_shape])
            self._original_shape = non_modality_shapes.pop() + (n_modalities, )
        return self._original_shape
Exemplo n.º 3
0
 def get_data(self):
     if len(self._file_path) > 1:
         # 2D image from multiple files
         raise NotImplementedError
     image_obj = load_image_from_file(self.file_path[0], self.loader[0])
     image_data = image_obj.get_data()
     image_data = misc.expand_to_5d(image_data)
     return image_data
Exemplo n.º 4
0
def infer_ndims_from_file(file_path):
    # todo: loader specified by the user is not used for ndims infer.
    image_header = load_image_from_file(file_path).header
    try:
        return int(image_header['dim'][0])
    except TypeError:
        pass
    try:
        return int(len(image_header.get_data_shape()))
    except (TypeError, AttributeError):
        pass

    tf.logging.fatal('unsupported file header in: {}'.format(file_path))
    raise IOError('could not get ndims from file header, please '
                  'consider convert image files to NifTI format.')
Exemplo n.º 5
0
    def dtype(self):
        """
        data type property of the input images.

        :return: a tuple of input image data types
            ``len(self.dtype) == len(self.file_path)``
        """
        if not self._dtype:
            try:
                self._dtype = tuple(
                    load_image_from_file(_file,
                                         _loader).header.get_data_dtype()
                    for _file, _loader in zip(self.file_path, self.loader))
            except (IOError, TypeError, AttributeError):
                tf.logging.warning('could not decide image data type')
                self._dtype = (np.dtype(np.float32), ) * len(self.file_path)
        return self._dtype
Exemplo n.º 6
0
    def _load_header(self):
        """
        read original header for pixdim and affine info

        :return:
        """
        self._original_pixdim = []
        self._original_affine = []
        for file_i, loader_i in zip(self.file_path, self.loader):
            _obj = load_image_from_file(file_i, loader_i)
            try:
                misc.correct_image_if_necessary(_obj)
                self._original_pixdim.append(_obj.header.get_zooms()[:3])
                self._original_affine.append(_obj.affine)
            except (TypeError, IndexError, AttributeError):
                tf.logging.fatal('could not read header from %s', file_i)
                raise ValueError
Exemplo n.º 7
0
    def _load_single_5d(self, idx=0):
        if len(self._file_path) > 1:
            # 5D image from multiple 4d files
            raise NotImplementedError
        # assuming len(self._file_path) == 1
        image_obj = load_image_from_file(self.file_path[idx], self.loader[idx])
        image_data = image_obj.get_data()
        image_data = misc.expand_to_5d(image_data)
        assert image_data.shape[3] == 1, "time sequences not supported"
        if self.original_axcodes[idx] and self.output_axcodes[idx]:
            output_image = []
            for t_pt in range(image_data.shape[3]):
                mod_list = []
                for mod in range(image_data.shape[4]):
                    spatial_slice = image_data[..., t_pt:t_pt + 1, mod:mod + 1]
                    spatial_slice = misc.do_reorientation(
                        spatial_slice, self.original_axcodes[idx],
                        self.output_axcodes[idx])
                    mod_list.append(spatial_slice)
                output_image.append(np.concatenate(mod_list, axis=4))
            image_data = np.concatenate(output_image, axis=3)

        if self.original_pixdim[idx] and self.output_pixdim[idx]:
            assert len(self._original_pixdim[idx]) == \
                   len(self.output_pixdim[idx]), \
                "wrong pixdim format original {} output {}".format(
                    self._original_pixdim[idx], self.output_pixdim[idx])
            # verbose: warning when interpolate_order>1 for integers
            output_image = []
            for t_pt in range(image_data.shape[3]):
                mod_list = []
                for mod in range(image_data.shape[4]):
                    spatial_slice = image_data[..., t_pt:t_pt + 1, mod:mod + 1]
                    spatial_slice = misc.do_resampling(
                        spatial_slice, self.original_pixdim[idx],
                        self.output_pixdim[idx], self.interp_order[idx])
                    mod_list.append(spatial_slice)
                output_image.append(np.concatenate(mod_list, axis=4))
            image_data = np.concatenate(output_image, axis=3)
        return image_data
Exemplo n.º 8
0
    def get_data(self):
        if len(self._file_path) > 1:
            # 3D image from multiple 2d files
            mod_list = []
            for mod in range(len(self.file_path)):
                mod_2d = SpatialImage2D(
                    file_path=(self.file_path[mod], ),
                    name=(self.name[mod], ),
                    interp_order=(self.interp_order[mod], ),
                    output_pixdim=(self.output_pixdim[mod], ),
                    output_axcodes=(self.output_axcodes[mod], ),
                    loader=(self.loader[mod], ))
                mod_data_5d = mod_2d.get_data()
                mod_list.append(mod_data_5d)
            try:
                image_data = np.concatenate(mod_list, axis=4)
            except ValueError:
                tf.logging.fatal(
                    "multi-modal data shapes not consistent -- trying to "
                    "concat {}.".format([mod.shape for mod in mod_list]))
                raise
            return image_data
        # assuming len(self._file_path) == 1
        image_obj = load_image_from_file(self.file_path[0], self.loader[0])
        image_data = image_obj.get_data()
        image_data = misc.expand_to_5d(image_data)
        if self.original_axcodes[0] and self.output_axcodes[0]:
            image_data = misc.do_reorientation(image_data,
                                               self.original_axcodes[0],
                                               self.output_axcodes[0])

        if self.original_pixdim[0] and self.output_pixdim[0]:
            # verbose: warning when interpolate_order>1 for integers
            image_data = misc.do_resampling(image_data,
                                            self.original_pixdim[0],
                                            self.output_pixdim[0],
                                            self.interp_order[0])
        return image_data
Exemplo n.º 9
0
 def load_2d_image(self, loader=None):
     data = image_loader.load_image_from_file(CASE_LOGO_2D,
                                              loader=loader).get_data()
     self.assertAllClose(data.shape, (400, 677, 1, 1, 4))
Exemplo n.º 10
0
 def test_nibabel_3d(self):
     data = image_loader.load_image_from_file(CASE_NIBABEL_3D).get_data()
     self.assertAllClose(data.shape, (256, 168, 256))