Example #1
0
    def test_segmentation(self):
        """Check that segmentation works as expected from ported version."""
        classes = ["fc", "tc", "pc", "men"]
        expected_seg = np.load(
            os.path.join(
                util.UNITTEST_DATA_PATH,
                "datasets/oai/expected/test_001_V00-iwoai-2019-t6.npy"))

        scan = NiftiReader().load(
            os.path.join(util.UNITTEST_DATA_PATH,
                         "datasets/oai/test_001_V00.nii.gz"))

        tissue = FemoralCartilage()
        tissue.find_weights(
            os.path.join(os.path.dirname(__file__),
                         "../../weights/iwoai-2019-t6"))
        dims = scan.volume.shape
        input_shape = (dims[0], dims[1], 1)
        model = IWOAIOAIUnet2D(input_shape=input_shape,
                               weights_path=tissue.weights_file_path)
        masks = model.generate_mask(scan)
        K.clear_session()

        for i, tissue in enumerate(classes):
            assert np.all(masks[tissue].volume == expected_seg[..., i])
Example #2
0
 def test_segmentation(self):
     """Test if batch size makes a difference on the segmentation output"""
     scan = self.SCAN_TYPE(dicom_path=self.dicom_dirpath)
     tissue = FemoralCartilage()
     tissue.find_weights(SEGMENTATION_WEIGHTS_FOLDER)
     dims = scan.get_dimensions()
     input_shape = (dims[0], dims[1], 1)
     model = get_model(SEGMENTATION_MODEL,
                       input_shape=input_shape,
                       weights_path=tissue.weights_file_path)
     scan.segment(model, tissue)
Example #3
0
    def test_segmentation(self):
        """Test automatic segmentation
           Expected: NotImplementedError
        """
        scan = self.SCAN_TYPE(dicom_path=self.dicom_dirpath)
        tissue = FemoralCartilage()
        tissue.find_weights(SEGMENTATION_WEIGHTS_FOLDER)
        dims = scan.get_dimensions()
        input_shape = (dims[0], dims[1], 1)
        model = get_model(SEGMENTATION_MODEL,
                          input_shape=input_shape,
                          weights_path=tissue.weights_file_path)

        # automatic segmentation currently not implemented
        with self.assertRaises(NotImplementedError):
            scan.segment(model, tissue)
Example #4
0
    def test_segmentation_multiclass(self):
        """Test support for multiclass segmentation."""
        scan = self.SCAN_TYPE.from_dicom(self.dicom_dirpath,
                                         num_workers=util.num_workers())
        tissue = FemoralCartilage()
        tissue.find_weights(SEGMENTATION_WEIGHTS_FOLDER),
        dims = scan.get_dimensions()
        input_shape = (dims[0], dims[1], 1)
        model = get_model(SEGMENTATION_MODEL,
                          input_shape=input_shape,
                          weights_path=tissue.weights_file_path)
        scan.segment(model, tissue, use_rss=True)

        # This should call __del__ in KerasSegModel
        model = None
        K.clear_session()
Example #5
0
    def test_generate_t2_map(self):
        ys, _, _ = self._generate_mock_data()
        scan = QDess(ys)

        tissue = FemoralCartilage()
        t2 = scan.generate_t2_map(tissue)
        assert isinstance(t2, QuantitativeValue)
Example #6
0
    def test_t2_star_map(self):
        scan = self.SCAN_TYPE(dicom_path=self.dicom_dirpath)
        scan.interregister(target_path=QDESS_ECHO1_PATH, target_mask_path=TARGET_MASK_PATH)

        # run analysis with femoral cartilage, without mask in tissue, but mask as additional input.
        tissue = FemoralCartilage()
        map1 = scan.generate_t2_star_map(tissue, TARGET_MASK_PATH)
        assert map1 is not None, "map should not be None"

        # add mask to femoral cartilage and run
        nr = NiftiReader()
        tissue.set_mask(nr.load(TARGET_MASK_PATH))
        map2 = scan.generate_t2_star_map(tissue)
        assert map2 is not None, "map should not be None"

        # map1 and map2 should be identical
        assert (map1.volumetric_map.is_identical(map2.volumetric_map))
Example #7
0
    def test_segmentation(self):
        classes = ["fc", "tc", "pc", "men"]
        expected_seg = np.load(
            os.path.join(
                util.UNITTEST_DATA_PATH,
                "datasets/oai/expected/test_001_V00-iwoai-2019-t6-normalized.npy",
            ))

        scan = NiftiReader().load(
            os.path.join(util.UNITTEST_DATA_PATH,
                         "datasets/oai/test_001_V00.nii.gz"))

        tissue = FemoralCartilage()
        tissue.find_weights(
            os.path.join(os.path.dirname(__file__),
                         "../../weights/iwoai-2019-t6-normalized"))
        dims = scan.volume.shape
        input_shape = (dims[0], dims[1], 1)
        model = IWOAIOAIUnet2DNormalized(input_shape=input_shape,
                                         weights_path=tissue.weights_file_path)
        masks = model.generate_mask(scan)
        K.clear_session()

        for i, tissue in enumerate(classes):
            pred = masks[tissue].volume.astype(np.bool)
            gt = expected_seg[..., i].astype(np.bool)
            dice = 2 * np.sum(pred & gt) / np.sum(
                pred.astype(np.uint8) + gt.astype(np.uint8))
            # Zero-mean normalization of 32-bit vs 64-bit data results in slightly different
            # estimations of the mean and standard deviation.
            # However, when both volumes are compared pre-normalization
            # using np.all() both volumes are the same (see :meth:`test_h5_nifti_same`).
            # This slight difference in the image can affect network performance.
            # As a placeholder, we assert that the dice between the expected and
            # produced segmentations for each tissue must be greater than 99%
            #
            # Update: We found for this particular model and example pair, the segmentations
            # achieve a dice score of 1.0 for all tissues.
            # We enforce that the predicted mask must be equal to the expected mask.
            assert dice >= 0.99, "{}: {:0.6f}".format(tissue, dice)
            assert np.all(masks[tissue].volume == expected_seg[
                ..., i]), f"Segmentation not same for {tissue}"
Example #8
0
    def test_generate_t2_map(self):
        ys, _, _, _, _ = self._generate_mock_data()
        scan = Mapss(ys)

        mask = MedicalVolume(np.ones(ys[0].shape), np.eye(4))
        mask_path = os.path.join(self.data_dirpath, "test_t2_mask.nii.gz")
        NiftiWriter().save(mask, mask_path)

        tissue = FemoralCartilage()
        map1 = scan.generate_t2_map(tissue)
        assert map1 is not None

        tissue.set_mask(mask)
        map2 = scan.generate_t2_map(tissue, num_workers=util.num_workers())
        assert map2 is not None
        assert map1.volumetric_map.is_identical(map2.volumetric_map)

        map2 = scan.generate_t2_map(tissue, mask_path=mask, num_workers=util.num_workers())
        assert map2 is not None
        assert map1.volumetric_map.is_identical(map2.volumetric_map)
Example #9
0
    def test_quant_val_fitting(self):
        """Test quantitative fitting (T1-rho, T2)"""
        scan = self.SCAN_TYPE(dicom_path=self.dicom_dirpath)

        actions = [scan.generate_t1_rho_map, scan.generate_t2_map]
        for action in actions:
            tissue = FemoralCartilage()
            map1 = action(tissue, MANUAL_SEGMENTATION_MASK_PATH)
            assert map1 is not None, "%s: map1 should not be None" % str(
                action)

            nr = NiftiReader()
            tissue.set_mask(nr.load(MANUAL_SEGMENTATION_MASK_PATH))
            map2 = action(tissue)
            assert map2 is not None, "%s: map2 should not be None" % str(
                action)

            # map1 and map2 should be identical
            assert map1.volumetric_map.is_identical(
                map2.volumetric_map
            ), "%s: map1 and map2 should be identical" % str(action)
Example #10
0
    def test_t2_star_map(self):
        ys, _, _, _ = self._generate_mock_data()
        scan = Cones(ys)

        # No mask
        tissue = FemoralCartilage()
        map1 = scan.generate_t2_star_map(tissue,
                                         num_workers=util.num_workers())
        assert map1 is not None, "map should not be None"

        mask = MedicalVolume(np.ones(ys[0].shape), np.eye(4))

        # Use a mask
        tissue.set_mask(mask)
        map2 = scan.generate_t2_star_map(tissue,
                                         num_workers=util.num_workers())
        assert map2 is not None, "map should not be None"
        assert map1.volumetric_map.is_identical(map2.volumetric_map)

        # Use a mask as a path
        tissue = FemoralCartilage()
        mask_path = os.path.join(self.data_dirpath,
                                 "test_t2_star_map_mask.nii.gz")
        NiftiWriter().save(mask, mask_path)
        map2 = scan.generate_t2_star_map(tissue,
                                         num_workers=util.num_workers(),
                                         mask_path=mask_path)
        assert map2 is not None, "map should not be None"
        assert map1.volumetric_map.is_identical(map2.volumetric_map)