Example #1
0
    def test_polygon_merger_tiled_masks(self, girderClient):  # noqa
        """Test Polygon_merger.run()."""
        # read GTCodes dataframe
        gtcodePath = getTestFilePath('sample_GTcodes.csv')
        GTCodes_df = read_csv(gtcodePath)
        GTCodes_df.index = GTCodes_df.loc[:, 'group']

        # This is where masks for adjacent rois are saved
        mask_loadpath = getTestFilePath(
            os.path.join('annotations_and_masks', 'polygon_merger_roi_masks'))
        maskpaths = [
            os.path.join(mask_loadpath, j) for j in os.listdir(mask_loadpath)
            if j.endswith('.png')
        ]

        # init and run polygon merger on masks grid
        pm = Polygon_merger(maskpaths=maskpaths,
                            GTCodes_df=GTCodes_df,
                            discard_nonenclosed_background=True,
                            verbose=1)
        contours_df = pm.run()

        # make sure it is what we expect
        assert contours_df.shape == (13, 13)
        assert set(contours_df.loc[:, 'group']) == {
            'roi', 'mostly_tumor', 'mostly_stroma',
            'mostly_lymphocytic_infiltrate'
        }

        # deleting existing annotations in target slide (if any)
        sampleSlideItem = girderClient.resourceLookup(
            '/user/admin/Public/TCGA-A2-A0YE-01Z-00-DX1.8A2E3094-5755-42BC-969D-7F0A2ECA0F39.svs'
        )  # noqa
        sampleSlideId = str(sampleSlideItem['_id'])
        delete_annotations_in_slide(girderClient, sampleSlideId)

        # get list of annotation documents
        annotation_docs = get_annotation_documents_from_contours(
            contours_df.copy(),
            separate_docs_by_group=True,
            docnamePrefix='test',
            verbose=False,
            monitorPrefix=sampleSlideId + ": annotation docs")

        # post annotations to slide -- make sure it posts without errors
        for annotation_doc in annotation_docs:
            resp = girderClient.post("/annotation?itemId=" + sampleSlideId,
                                     json=annotation_doc)
            assert 'annotation' in resp.keys()
    def test_get_annotation_documents_from_contours(self,
                                                    girderClient):  # noqa
        """Test get_contours_from_bin_mask()."""
        self._setup()
        sampleSlideItem = girderClient.resourceLookup(
            '/user/admin/Public/TCGA-A2-A0YE-01Z-00-DX1.8A2E3094-5755-42BC-969D-7F0A2ECA0F39.svs'
        )  # noqa
        sampleSlideId = str(sampleSlideItem['_id'])
        # get list of annotation documents
        annprops = {
            'X_OFFSET': self.X_OFFSET,
            'Y_OFFSET': self.Y_OFFSET,
            'opacity': 0.2,
            'lineWidth': 4.0,
        }
        annotation_docs = get_annotation_documents_from_contours(
            self.CONTOURS_DF.copy(),
            separate_docs_by_group=True,
            annots_per_doc=10,
            docnamePrefix='test',
            annprops=annprops,
            verbose=False,
            monitorPrefix=self.MASKNAME[:12] + ": annotation docs")

        # make sure its what we expect
        assert len(annotation_docs) == 8
        assert {j['name']
                for j in annotation_docs} == {
                    'test_blood_vessel-0', 'test_exclude-0',
                    'test_mostly_lymphocytic_infiltrate-0',
                    'test_mostly_stroma-0', 'test_mostly_tumor-0',
                    'test_mostly_tumor-1', 'test_normal_acinus_or_duct-0',
                    'test_roi-0'
                }

        # deleting existing annotations in target slide (if any)
        delete_annotations_in_slide(girderClient, sampleSlideId)

        # post annotations to slide -- make sure it posts without errors
        resp = girderClient.post("/annotation?itemId=" + sampleSlideId,
                                 json=annotation_docs[0])
        assert 'annotation' in resp.keys()