Exemple #1
0
 def create_from_tilesummaries(self, 
                               tilesummaries:List[tile_extraction.tiles.TileSummary], 
                               patient_id_getter:Callable, 
                               case_id_getter:Callable, 
                               slide_id_getter:Callable, 
                               labels_getter:Callable):
     """
     Inits self.patients from tilesummaries. 
     On how to create tilesummaries: 
         https://github.com/FAU-DLM/wsi_processing_pipeline/blob/master/tile_extraction/example.ipynb
         
     Arguments:
         tilesummaries:
         patient_id_getter: method that maps from the tilesummary's whole-slide image path to a patient id
         case_id_getter: method that maps from the tilesummary's whole-slide image path to a case id
         slide_id_getter: method that maps from the tilesummary's whole-slide image path to a slide id
         labels_getter: method that maps from the tilesummary's whole-slide image path and a RegionOfInterest
                         to a list ob labels
     """
     
     # empty the lists, in case this function is called multiple times
     self.__patients = []
     __rois = []
     #key: patient_id; value: Patient object
     patient_id_to_patient = {}
     for tilesummary in tqdm(tilesummaries):
         
         # skip tilesummaries with 0 tiles
         if(len(tilesummary.top_tiles()) == 0):
             continue
         
         
         ###
         # patient
         ###
         current_patient = None
         patient_id = patient_id_getter(tilesummary.wsi_path)
         if(patient_id not in patient_id_to_patient.keys()):
             current_patient = Patient(patient_id=patient_id, patient_manager=self)
             patient_id_to_patient[patient_id] = current_patient
             self.add_patient(current_patient)
         else:
             current_patient = patient_id_to_patient[patient_id]
         
         ###
         # case
         ###
         case_id = case_id_getter(tilesummary.wsi_path)
         current_case = None
         for case in current_patient.get_cases():
             if(case.case_id == case_id):
                 current_case = case
                 break;
         if(current_case == None):
             current_case = Case(case_id=case_id, patient=current_patient)
             current_patient.add_case(current_case)
         
         ###
         # whole-slide
         ###
         slide_id = slide_id_getter(tilesummary.wsi_path)
         current_slide = WholeSlideImage(slide_id=slide_id, case=current_case, path=tilesummary.wsi_path)
         current_case.add_whole_slide_image(current_slide)
         
         ###
         # regions of interest
         ###
         rois = tilesummary.rois
         
         assert (rois != None and len(rois) > 0)
         for roi in rois:
             
             __rois.append(roi)
             
             roi.reset_tiles() #reset tiles in case this function is called multiple times
             current_slide.add_region_of_interest(roi)
             roi.whole_slide_image = current_slide
             roi.labels = labels_getter(tilesummary.wsi_path, roi)
             for tile in tilesummary.top_tiles():               
                 if(tile.roi.roi_id == roi.roi_id):
                     tile.set_removed_flag(value=False) # this flag might still be True from a previous call
                     roi.add_tile(tile)
                     tile.labels = roi.labels
                     
     #__rois = self.get_rois(dataset_type=shared.enums.DatasetType.all)
     roi_ids = [r.roi_id for r in __rois]
     if(len(roi_ids) != len(set(roi_ids))):
         raise ValueError('The rois do not have unique ids.')
Exemple #2
0
 def create_from_preextracted_tiles(self, 
                                    tile_paths:List[pathlib.Path],
                                     patient_id_getter:Callable, 
                                     case_id_getter:Callable, 
                                     slide_id_getter:Callable, 
                                     labels_getter:Callable):
     """
     Inits self.patients from paths to already extracted tiles.
         
     Arguments:
         paths: paths to the tiles
         patient_id_getter: method that maps from the tile image path to a patient id
         case_id_getter: method that maps from the tile image path to a case id
         slide_id_getter: method that maps from the tile image path to a slide id
         labels_getter: function that maps from a tile_path to the tile's labels
     """
     
     # empty the list, in case this function is called multiple times
     self.patients = []
     
     #key: patient_id; value: Patient object
     patient_id_to_patient = {}
     for tile_path in tqdm(tile_paths):
         ###
         # patient
         ###
         current_patient = None
         patient_id = patient_id_getter(tile_path)
         if(patient_id not in patient_id_to_patient.keys()):
             current_patient = Patient(patient_id=patient_id, patient_manager=self)
             patient_id_to_patient[patient_id] = current_patient
             self.add_patient(current_patient)
         else:
             current_patient = patient_id_to_patient[patient_id]
         
         ###
         # case
         ###
         case_id = case_id_getter(tile_path)
         current_case = None
         for case in current_patient.get_cases():
             if(case.case_id == case_id):
                 current_case = case
                 break;
         if(current_case == None):
             current_case = Case(case_id=case_id, patient=current_patient)
             current_patient.add_case(current_case)
         
         ###
         # whole-slide
         ###
         slide_id = slide_id_getter(tile_path)
         current_slide = None
         for slide in current_case.get_whole_slide_images():
             if(slide.slide_id == slide_id):
                 current_slide = slide
                 break;
         if(current_slide == None):
             current_slide = WholeSlideImage(slide_id=slide_id, case=current_case)
             current_case.add_whole_slide_image(current_slide)
         
         ###
         # region of interest; just one dummy roi per wsi
         ###
         current_roi = None
         if(len(current_slide.get_regions_of_interest()) == 0):
             current_roi = roi.RegionOfInterestDummy(slide_id, current_slide)
             current_slide.add_region_of_interest(current_roi)
         else:
             current_roi = current_slide.get_regions_of_interest()[0]
         tile = Tile(roi=current_roi, tile_path=tile_path, labels=labels_getter(tile_path))
         current_roi.add_tile(tile)