Ejemplo n.º 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 _setup(self):
        # read GTCodes dataframe
        gtcodePath = getTestFilePath('sample_GTcodes.csv')
        self.GTCodes_df = read_csv(gtcodePath)
        self.GTCodes_df.index = self.GTCodes_df.loc[:, 'group']

        # read sample contours_df dataframe to test against
        contoursDfPath = getTestFilePath(
            os.path.join('annotations_and_masks', 'sample_contours_df.tsv'))
        self.CONTOURS_DF = read_csv(contoursDfPath, sep='\t', index_col=0)

        # read mask
        self.X_OFFSET = 59206
        self.Y_OFFSET = 33505
        self.MASKNAME = (
            'TCGA-A2-A0YE-01Z-00-DX1.8A2E3094-5755-42BC-969D-7F0A2ECA0F39_'
            'left-%d_top-%d_mag-BASE.png' % (self.X_OFFSET, self.Y_OFFSET))
        MASKPATH = getTestFilePath(
            os.path.join('annotations_and_masks', self.MASKNAME))
        self.MASK = imread(MASKPATH)
def test_prep(girderClient):  # noqa

    cfg.gc = girderClient
    cfg.URL = cfg.gc.urlBase.replace('api/v1/', '')

    # GT codes dict for parsing into label mask
    gtcodePath = getTestFilePath('sample_GTcodes.csv')
    cfg.GTCodes_dict = read_csv(gtcodePath)
    cfg.GTCodes_dict.index = cfg.GTCodes_dict.loc[:, 'group']
    cfg.GTCodes_dict = cfg.GTCodes_dict.to_dict(orient='index')

    # just a temp directory to save masks
    cfg.BASE_SAVEPATH = tempfile.mkdtemp()
    cfg.SAVEPATHS = {
        'contours': os.path.join(cfg.BASE_SAVEPATH, 'contours'),
        'rgb': os.path.join(cfg.BASE_SAVEPATH, 'rgbs'),
        'visualization': os.path.join(cfg.BASE_SAVEPATH, 'vis'),
    }
    for _, savepath in cfg.SAVEPATHS.items():
        os.mkdir(savepath)

    # where to save gallery
    cfg.combinedvis_savepath = os.path.join(cfg.BASE_SAVEPATH, 'combinedvis')
    os.mkdir(cfg.combinedvis_savepath)

    # get original item
    iteminfo = cfg.gc.get('/item',
                          parameters={'text': "TCGA-A2-A0YE-01Z-00-DX1"})[0]

    # create the folder to parse its contents into galleries
    folderinfo = cfg.gc.post('/folder',
                             data={
                                 'parentId': iteminfo['folderId'],
                                 'name': 'test'
                             })
    cfg.folderid = folderinfo['_id']

    # create girder folder to post resultant galleries
    post_folderinfo = cfg.gc.post('/folder',
                                  data={
                                      'parentId': iteminfo['folderId'],
                                      'name': 'test-post'
                                  })
    cfg.post_folderid = post_folderinfo['_id']

    # copy the item multiple times to create dummy database
    for i in range(2):
        _ = cfg.gc.post("/item/%s/copy" % iteminfo['_id'],
                        data={
                            'name': 'testSlide%dForRevGal' % i,
                            'copyAnnotations': True,
                            'folderId': cfg.folderid,
                        })