Beispiel #1
0
 def __init__(self,
              preprocessing: Optional[Union[ImagePreproParams,
                                            Dict]] = None):
     if preprocessing:
         if isinstance(preprocessing, ImagePreproParams):
             self._preprocessing = preprocessing
         elif isinstance(preprocessing, dict):
             self._preprocessing = ImagePreproParams(**preprocessing)
     else:
         self._preprocessing = ImagePreproParams()
Beispiel #2
0
def test_sitk_backend(
    disk_im_gry,
    disk_im_mch_notile,
    disk_im_rgb,
):

    im_gry = sitk_backend(str(disk_im_gry),
                          preprocessing=ImagePreproParams(image_type="FL"))
    # im_mch = sitk_backend(str(disk_im_mch_notile), preprocessing={"image_type":"FL"})
    im_rgb = sitk_backend(str(disk_im_rgb),
                          preprocessing=ImagePreproParams(image_type="BF"))

    assert im_gry.GetSize() == (2048, 2048)
    # assert im_mch.GetSize() == (2048,2048,3)
    assert im_rgb.GetSize() == (2048, 2048)
Beispiel #3
0
def test_czi_read_mc_std_preprocess():
    image_fp = os.path.join(PRIVATE_DIR, "czi_4ch_16bit.czi")
    preprocessing = ImagePreproParams()
    ri = reg_image_loader(image_fp, 1, preprocessing=preprocessing)
    ri.read_reg_image()
    assert ri.reg_image.GetNumberOfComponentsPerPixel() == 1
    assert ri.reg_image.GetPixelID() == 1
Beispiel #4
0
    def check_cache_preprocessing(self, output_dir: Union[str, Path],
                                  image_tag: str):
        """

        Parameters
        ----------
        output_dir: path
            Where cached data is on disk
        image_tag:
            Tag of the image modality

        Returns
        -------
        prepro_flag: bool
            Whether a preprocessed version of the image exists in the cache.
        """
        (
            out_image_fp,
            out_params_fp,
            out_mask_fp,
            _,
            _,
        ) = self._get_all_cache_data_fps(output_dir, image_tag)

        if out_image_fp.exists() and out_params_fp.exists():
            cached_preprocessing = ImagePreproParams.parse_file(out_params_fp)
            return self.preprocessing == cached_preprocessing
        else:
            return False
Beispiel #5
0
    def preprocess_reg_image_intensity(
            self, image: sitk.Image,
            preprocessing: ImagePreproParams) -> sitk.Image:
        """
        Preprocess image intensity data to single channel image.

        Parameters
        ----------
        image: sitk.Image
            reg_image to be preprocessed
        preprocessing: ImagePreproParams
            Parameters of the preprocessing

        Returns
        -------
        image: sitk.Image
            Preprocessed single-channel image
        """

        if preprocessing.image_type.value == "FL":
            preprocessing.invert_intensity = False
        elif preprocessing.image_type.value == "BF":
            preprocessing.max_int_proj = False
            preprocessing.contrast_enhance = False
            if self.is_rgb:
                preprocessing.invert_intensity = True

        if preprocessing.max_int_proj:
            image = sitk_max_int_proj(image)

        if preprocessing.contrast_enhance:
            image = contrast_enhance(image)

        if preprocessing.invert_intensity:
            image = sitk_inv_int(image)

        if preprocessing.custom_processing:
            for k, v in preprocessing.custom_processing.items():
                print(f"performing preprocessing: {k}")
                image = v(image)

        image.SetSpacing((self.image_res, self.image_res))

        return image
Beispiel #6
0
def test_wsireg2d_add_modality_w_np(data_out_dir, im_mch_np):
    wsi_reg = WsiReg2D(gen_project_name_str(), str(data_out_dir))
    wsi_reg.add_modality(
        "test_mod",
        im_mch_np,
        0.65,
        channel_names=["test"],
        channel_colors=["red"],
    )
    assert np.array_equal(
        wsi_reg.modalities.get("test_mod").get("image_filepath"), im_mch_np)
    assert wsi_reg.modalities.get("test_mod").get("image_res") == 0.65
    assert wsi_reg.modalities.get("test_mod").get("channel_names") == ["test"]
    assert wsi_reg.modalities.get("test_mod").get("channel_colors") == ["red"]
    assert (wsi_reg.modalities.get("test_mod").get("preprocessing") ==
            ImagePreproParams())
    assert wsi_reg.modalities.get("test_mod").get("mask") == None
    assert wsi_reg.n_modalities == 1
Beispiel #7
0
    def load_from_cache(self, output_dir: Union[str, Path], image_tag: str):
        """
        Read in preprocessed data from the cache folder.
        Parameters
        ----------
        output_dir: path
            Where cached data is on disk
        image_tag:
            Tag of the image modality

        Returns
        -------
        from_cache_flag: bool
            Whether data was read from cache
        """
        (
            image_fp,
            params_fp,
            mask_fp,
            init_tform_fp,
            osize_tform_fp,
        ) = self._get_all_cache_data_fps(output_dir, image_tag)

        read_from_cache = self.check_cache_preprocessing(output_dir, image_tag)

        if read_from_cache:
            self._reg_image = sitk.ReadImage(str(image_fp))
            self._preprocessing = ImagePreproParams(
                **json.load(open(params_fp, "r")))
            self.pre_reg_transforms = json.load(open(init_tform_fp, "r"))
            if osize_tform_fp.exists():
                self.original_size_transform = json.load(
                    open(osize_tform_fp, "r"))

            if mask_fp.exists():
                self._mask = sitk.ReadImage(str(mask_fp))
            return True
        else:
            return False
Beispiel #8
0
def test_tifffile_dask_backend(
    disk_im_gry_pyr,
    disk_im_mch_pyr,
    disk_im_rgb_pyr,
    disk_im_gry,
    disk_im_mch,
    disk_im_rgb,
):
    im_gry_pyr = tifffile_dask_backend(
        disk_im_gry_pyr,
        largest_series=0,
        preprocessing=ImagePreproParams(image_type="FL"),
    )
    im_mch_pyr = tifffile_dask_backend(
        disk_im_mch_pyr,
        largest_series=0,
        preprocessing=ImagePreproParams(image_type="FL"),
    )
    im_rgb_pyr = tifffile_dask_backend(
        disk_im_rgb_pyr,
        largest_series=0,
        preprocessing=ImagePreproParams(image_type="BF"),
    )
    im_gry = tifffile_dask_backend(
        disk_im_gry,
        largest_series=0,
        preprocessing=ImagePreproParams(image_type="FL"),
    )
    im_mch = tifffile_dask_backend(
        disk_im_mch,
        largest_series=0,
        preprocessing=ImagePreproParams(image_type="FL"),
    )
    im_rgb = tifffile_dask_backend(
        disk_im_rgb,
        largest_series=0,
        preprocessing=ImagePreproParams(image_type="BF"),
    )

    assert im_gry_pyr.GetSize() == (2048, 2048)
    assert im_mch_pyr.GetSize() == (2048, 2048, 3)
    assert im_rgb_pyr.GetSize() == (2048, 2048)
    assert im_gry.GetSize() == (2048, 2048)
    assert im_mch.GetSize() == (2048, 2048, 3)
    assert im_rgb.GetSize() == (2048, 2048)
Beispiel #9
0
def test_czi_read_rgb_bf_preprocess_default():
    image_fp = os.path.join(PRIVATE_DIR, "czi_rgb.czi")
    preprocessing = ImagePreproParams()
    ri = reg_image_loader(image_fp, 1, preprocessing=preprocessing)
    ri.read_reg_image()
    assert ri.reg_image.GetNumberOfComponentsPerPixel() == 1
Beispiel #10
0
    def preprocess_reg_image_spatial(
        self,
        image: sitk.Image,
        preprocessing: ImagePreproParams,
        imported_transforms=None,
    ) -> Tuple[sitk.Image, List[Dict]]:
        """
        Spatial preprocessing of the reg_image.

        Parameters
        ----------
        image: sitk.Image
            reg_image to be preprocessed
        preprocessing: ImagePreproParams
            Spatial preprocessing parameters
        imported_transforms:
            Not implemented yet..

        Returns
        -------
        image: sitk.Image
            Spatially preprcessed image ready for registration
        transforms: list of transforms
            List of pre-initial transformations
        """

        transforms = []
        original_size = image.GetSize()

        if preprocessing.downsampling > 1:
            print("performing downsampling by factor: {}".format(
                preprocessing.downsampling))
            image.SetSpacing((self.image_res, self.image_res))
            image = sitk.Shrink(
                image,
                (preprocessing.downsampling, preprocessing.downsampling),
            )

            if self._mask is not None:
                self._mask.SetSpacing((self.image_res, self.image_res))
                self._mask = sitk.Shrink(
                    self._mask,
                    (
                        preprocessing.downsampling,
                        preprocessing.downsampling,
                    ),
                )

            image_res = image.GetSpacing()[0]
        else:
            image_res = self.image_res

        if float(preprocessing.rot_cc) != 0.0:
            print(f"rotating counter-clockwise {preprocessing.rot_cc}")
            rot_tform = gen_rigid_tform_rot(image, image_res,
                                            preprocessing.rot_cc)
            (
                composite_transform,
                _,
                final_tform,
            ) = prepare_wsireg_transform_data({"initial": [rot_tform]})

            image = transform_plane(image, final_tform, composite_transform)

            if self._mask is not None:
                self._mask.SetSpacing((image_res, image_res))
                self._mask = transform_plane(self._mask, final_tform,
                                             composite_transform)
            transforms.append(rot_tform)

        if preprocessing.flip:
            print(f"flipping image {preprocessing.flip.value}")

            flip_tform = gen_aff_tform_flip(image, image_res,
                                            preprocessing.flip.value)

            (
                composite_transform,
                _,
                final_tform,
            ) = prepare_wsireg_transform_data({"initial": [flip_tform]})

            image = transform_plane(image, final_tform, composite_transform)

            if self._mask is not None:
                self._mask.SetSpacing((image_res, image_res))
                self._mask = transform_plane(self._mask, final_tform,
                                             composite_transform)

            transforms.append(flip_tform)

        if self._mask and preprocessing.crop_to_mask_bbox:
            print("computing mask bounding box")
            if preprocessing.mask_bbox is None:
                mask_bbox = compute_mask_to_bbox(self._mask)
                preprocessing.mask_bbox = mask_bbox

        if preprocessing.mask_bbox:

            print("cropping to mask")
            translation_transform = gen_rigid_translation(
                image,
                image_res,
                preprocessing.mask_bbox.X,
                preprocessing.mask_bbox.Y,
                preprocessing.mask_bbox.WIDTH,
                preprocessing.mask_bbox.HEIGHT,
            )

            (
                composite_transform,
                _,
                final_tform,
            ) = prepare_wsireg_transform_data(
                {"initial": [translation_transform]})

            image = transform_plane(image, final_tform, composite_transform)

            self.original_size_transform = gen_rig_to_original(
                original_size, deepcopy(translation_transform))

            if self._mask is not None:
                self._mask.SetSpacing((image_res, image_res))
                self._mask = transform_plane(self._mask, final_tform,
                                             composite_transform)
            transforms.append(translation_transform)

        return image, transforms
Beispiel #11
0
def test_read_preprocess_array():
    da_arr = ensure_dask_array(np.zeros((128, 128, 3), dtype=np.uint8))
    mc_arr = ensure_dask_array(np.zeros((3, 128, 128), dtype=np.uint8))
    gr_arr = ensure_dask_array(np.zeros((128, 128), dtype=np.uint8))

    std_rgb = read_preprocess_array(
        da_arr,
        preprocessing=ImagePreproParams(image_type="BF"),
        force_rgb=None,
    )
    nonstd_rgb = read_preprocess_array(
        mc_arr,
        preprocessing=ImagePreproParams(image_type="BF"),
        force_rgb=True,
    )

    all_ch_mc = read_preprocess_array(
        mc_arr, preprocessing=ImagePreproParams(image_type="FL"))

    ch0_ch_mc = read_preprocess_array(
        mc_arr, preprocessing=ImagePreproParams(ch_indices=[0]))
    ch01_ch_mc = read_preprocess_array(
        mc_arr, preprocessing=ImagePreproParams(ch_indices=[0, 1]))
    ch12_ch_mc = read_preprocess_array(
        mc_arr, preprocessing=ImagePreproParams(ch_indices=[1, 2]))
    ch02_ch_mc = read_preprocess_array(
        mc_arr, preprocessing=ImagePreproParams(ch_indices=[0, 2]))
    ch1_ch_mc = read_preprocess_array(
        mc_arr, preprocessing=ImagePreproParams(ch_indices=[1]))
    ch2_ch_mc = read_preprocess_array(
        mc_arr, preprocessing=ImagePreproParams(ch_indices=[2]))

    std_gr = read_preprocess_array(gr_arr, ImagePreproParams(image_type="FL"))
    chsel0_gr = read_preprocess_array(
        gr_arr, preprocessing=ImagePreproParams(ch_indices=[0]))
    chsel01_gr = read_preprocess_array(
        gr_arr, preprocessing=ImagePreproParams(ch_indices=[0, 1]))

    assert std_rgb.GetNumberOfComponentsPerPixel() == 1
    assert nonstd_rgb.GetNumberOfComponentsPerPixel() == 1
    assert all_ch_mc.GetDepth() == 3
    assert ch0_ch_mc.GetDepth() == 0
    assert ch01_ch_mc.GetDepth() == 2
    assert ch12_ch_mc.GetDepth() == 2
    assert ch02_ch_mc.GetDepth() == 2
    assert ch1_ch_mc.GetDepth() == 0
    assert ch2_ch_mc.GetDepth() == 0
    assert std_gr.GetDepth() == 0
    assert chsel0_gr.GetDepth() == 0
    assert chsel01_gr.GetDepth() == 0
    assert std_rgb.GetSize() == (128, 128)
    assert nonstd_rgb.GetSize() == (128, 128)
    assert all_ch_mc.GetSize() == (128, 128, 3)
    assert ch0_ch_mc.GetSize() == (128, 128)
    assert ch01_ch_mc.GetSize() == (128, 128, 2)
    assert ch12_ch_mc.GetSize() == (128, 128, 2)
    assert ch02_ch_mc.GetSize() == (128, 128, 2)
    assert ch1_ch_mc.GetSize() == (128, 128)
    assert ch2_ch_mc.GetSize() == (128, 128)
    assert std_gr.GetSize() == (128, 128)
    assert chsel0_gr.GetSize() == (128, 128)
    assert chsel01_gr.GetSize() == (128, 128)