[docs]classRSNACervicalSpineFracture(SubjectsDataset):"""RSNA 2022 Cervical Spine Fracture Detection dataset. This is a helper class for the dataset used in the `RSNA 2022 Cervical Spine Fracture Detection`_ hosted on `kaggle <https://www.kaggle.com/>`_. The dataset must be downloaded before instantiating this class. .. _RSNA 2022 Cervical Spine Fracture Detection: https://www.kaggle.com/competitions/rsna-2022-cervical-spine-fracture-detection/overview/evaluation """# noqa: B950UID='StudyInstanceUID'def__init__(self,root_dir:TypePath,add_segmentations:bool=False,add_bounding_boxes:bool=False,**kwargs,):self.root_dir=normalize_path(root_dir)subjects=self._get_subjects(add_segmentations,add_bounding_boxes,)super().__init__(subjects,**kwargs)@staticmethoddef_get_image_dirs_dict(images_dir:Path)->Dict[str,Path]:dirs_dict={}fordicom_dirinsorted(images_dir.iterdir()):dirs_dict[dicom_dir.name]=dicom_dirreturndirs_dict@staticmethoddef_get_segs_paths_dict(segs_dir:Path)->Dict[str,Path]:paths_dict={}forimage_pathinsorted(segs_dir.iterdir()):key=image_path.name.replace('.gz','').replace('.nii','')paths_dict[key]=image_pathreturnpaths_dictdef_get_subjects(self,add_segmentations:bool,add_bounding_boxes:bool,)->List[Subject]:subjects=[]pd=get_pandas()fromtqdm.autoimporttqdmsplit_name='train'images_dirname=f'{split_name}_images'images_dir=self.root_dir/images_dirnameimage_dirs_dict=self._get_image_dirs_dict(images_dir)segmentations_dir=self.root_dir/'segmentations'seg_paths_dict=self._get_segs_paths_dict(segmentations_dir)bboxes_path=self.root_dir/'train_bounding_boxes.csv'bounding_boxes_df=pd.read_csv(bboxes_path)grouped_boxes=bounding_boxes_df.groupby(self.UID)df=pd.read_csv(self.root_dir/f'{split_name}.csv')for_,rowintqdm(list(df.iterrows())):uid=row[self.UID]image_dir=image_dirs_dict[uid]seg_path=Noneifadd_segmentations:seg_path=seg_paths_dict.get(uid,None)boxes=[]ifadd_bounding_boxes:try:boxes_df=grouped_boxes.get_group(uid)boxes=[dict(row)for_,rowinboxes_df.iterrows()]exceptKeyError:passsubject=self._get_subject(dict(row),image_dir,seg_path,boxes,)subjects.append(subject)returnsubjects@staticmethoddef_filter_list(iterable:List[Path],target:str):def_filter(path:Path):ifpath.is_dir():returntarget==path.nameelse:name=path.name.replace('.gz','').replace('.nii','')returntarget==namefound=list(filter(_filter,iterable))iffound:assertlen(found)==1result=found[0]else:result=Nonereturnresultdef_get_subject(self,csv_row_dict:Dict[str,Union[str,int]],image_dir:Path,seg_path:Optional[Path],boxes:TypeBoxes,)->Subject:subject_dict:Dict[str,Any]={}subject_dict.update(csv_row_dict)subject_dict['ct']=ScalarImage(image_dir)ifseg_pathisnotNone:subject_dict['seg']=LabelMap(seg_path)ifboxes:subject_dict['boxes']=boxesreturnSubject(**subject_dict)
defget_pandas()->ModuleType:try:importpandasreturnpandasexceptImportErrorase:message=('Pandas is required for this operation.'' Install pandas with "pip install pandas" and try again')raiseImportError(message)frome