{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Cervical Spine Fracture Detection : RSNA 2022","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\n\n# NII Image :\nfrom nibabel.testing import data_path\nimport nibabel as nib\n\n# DCM Image :\nimport pydicom\nimport pydicom.data\nfrom pydicom.data import get_testdata_files","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-19T16:05:16.281404Z","iopub.execute_input":"2022-09-19T16:05:16.282541Z","iopub.status.idle":"2022-09-19T16:05:16.288592Z","shell.execute_reply.started":"2022-09-19T16:05:16.282489Z","shell.execute_reply":"2022-09-19T16:05:16.287476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## I. Position of the problem","metadata":{}},{"cell_type":"markdown","source":"RSNA (Radiological Society of North America) propose an artificial intelligence challenge : the goal is to detect and localize cervical spine, which is the most common type of cervical fracture. \n\nto proceed we need to develop machine learning models that match with radiologist's role. The model should be able to make a diagnosis on the seven vertebrae that compromise the cervical spine.","metadata":{}},{"cell_type":"markdown","source":"![image_vertebra](https://www.researchgate.net/profile/Steve-Barker-2/publication/300416768/figure/fig2/AS:608334231117826@1522049591803/Anatomical-representation-of-a-human-neck-illustrating-its-four-major-kinematic-units.png)\n\n**Anatomical representation of a human neck illustrating its four major kinematic units - \"Design of a biologically inspired humanoid neck\" - Steve Barker, Luis A. Fuente, Khaled Hayatleh, Nigel Crook (2015)**","metadata":{}},{"cell_type":"markdown","source":"We need to predict a probability for a fracture at each of the seven cervical vertebrae designated as C1, C2, C3, C4, C5, C6 and C7. There is also an any label, *patient_overall* , which indicates that a fracture of ANY kind described before exists in the examination. \n\nFor each exam Id, the model submit a set of predicted probabilities (a separate row for each cervical level subtype). We then take the log loss for each predicted probability versus its true label.\n\nTo measure the performance of the model, we use the binary weighted log loss function. Considering a label **j** on exam **i**, this loss is specified as :\n\n\\begin{align}\nL_{ij} = - w_{j} [ y_{ij} * \\log(p_{ij}) + (1 - y_{ij}) * \\log(1 - p_{ij}) ]\n\\end{align}\n\nAt the end, final loss is obtained by the averaging the $L_{ij}$ among all rows.","metadata":{}},{"cell_type":"code","source":"df_sub =  pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/sample_submission.csv\")\ndf_sub","metadata":{"execution":{"iopub.status.busy":"2022-09-19T15:57:54.481149Z","iopub.execute_input":"2022-09-19T15:57:54.481437Z","iopub.status.idle":"2022-09-19T15:57:54.51338Z","shell.execute_reply.started":"2022-09-19T15:57:54.48141Z","shell.execute_reply":"2022-09-19T15:57:54.512183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## II - Look and understand the data","metadata":{}},{"cell_type":"markdown","source":"The training dataset is composed of 9 rows : \n- **StudyInstanceUID** : Identification number of the patient, unique for each patient. It will allow us to associate each scanner image to its corresponding diagnosis.\n- **patient_overall** : Dummy variable that corresponds to the global diagnosis of the patient (i.e equal to 1 if at least one cervical is fractured, 0 otherwise)\n- **C1-C7** : Dummy variables that corresponds to the diagnosis of each cervical (i.e equal to 1 if the associated cervicak is fractured, 0 otherwise)","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/train.csv\")\ndf_train.sample(5)","metadata":{"execution":{"iopub.status.busy":"2022-09-19T15:57:54.515109Z","iopub.execute_input":"2022-09-19T15:57:54.515582Z","iopub.status.idle":"2022-09-19T15:57:54.539598Z","shell.execute_reply.started":"2022-09-19T15:57:54.515505Z","shell.execute_reply":"2022-09-19T15:57:54.538449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For each **StudyInstanceUID**, we dispose of two distincts elements : \n- one **segmentation** which is an N-D array containing the image data coupled with a (4,4) matrix mapping array coordinates to coordinates in some RAS+ world coordinate space (Coordinate systems and affines) in the *.nii* format\n- one **image** of the scanner in the *.dcm* format","metadata":{}},{"cell_type":"code","source":"seg_paths = []\nimg_paths = []\nUIDs = list(df_train[\"StudyInstanceUID\"])\nfor uid in tqdm(UIDs):\n    seg_path = \"../input/rsna-2022-cervical-spine-fracture-detection/segmentations/\" + str(uid) + \".nii\"\n    img_path = \"../input/rsna-2022-cervical-spine-fracture-detection/train_images/\" + str(uid)\n    seg_paths.append(seg_path)\n    img_paths.append(img_path)\n\ndf_train[\"seg_path\"] = seg_paths\ndf_train[\"img_path\"] = img_paths\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-19T15:57:54.541795Z","iopub.execute_input":"2022-09-19T15:57:54.542149Z","iopub.status.idle":"2022-09-19T15:57:54.580569Z","shell.execute_reply.started":"2022-09-19T15:57:54.542118Z","shell.execute_reply":"2022-09-19T15:57:54.579443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"## View a DCM Image\n","metadata":{}},{"cell_type":"code","source":"base = \"../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.11988\"\npass_dicom = \"25.dcm\"  # file name is 1-12.dcm\n  \n# enter DICOM image name for pattern\n# result is a list of 1 element\nfilename = pydicom.data.data_manager.get_files(base, pass_dicom)[0]\n  \nds = pydicom.dcmread(filename)\n  \nplt.imshow(ds.pixel_array, cmap=plt.cm.bone)  # set the color map to bone\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-19T17:49:43.027022Z","iopub.execute_input":"2022-09-19T17:49:43.028324Z","iopub.status.idle":"2022-09-19T17:49:43.250486Z","shell.execute_reply.started":"2022-09-19T17:49:43.028278Z","shell.execute_reply":"2022-09-19T17:49:43.249376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## View a NII Image\n","metadata":{}},{"cell_type":"code","source":"img = nib.load('../input/rsna-2022-cervical-spine-fracture-detection/segmentations/1.2.826.0.1.3680043.11988.nii').get_fdata()\nprint(img.shape)\ntest = img[:,:,45]\nplt.imshow(test)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-19T17:19:12.4486Z","iopub.execute_input":"2022-09-19T17:19:12.449614Z","iopub.status.idle":"2022-09-19T17:19:12.791459Z","shell.execute_reply.started":"2022-09-19T17:19:12.44957Z","shell.execute_reply":"2022-09-19T17:19:12.790333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create Class Dataset","metadata":{}},{"cell_type":"code","source":"from torch.utils.data import Dataset\n\nclass Dataset(Dataset):\n    \n    def __init__(self, df):\n        \n        super().__init__()\n        \n        self.seg_paths = df[\"seg_path\"]\n        \n        self.img_paths = df[\"img_path\"]\n        \n    def __getitem__(self, index):\n        \n        seg_path = self.seg_paths.loc[index]\n        print(seg_path)\n        #nii_img = nib.load(seg_path).get_fdata()\n        \n        img_path = self.img_paths.loc[index]\n        \n        return seg_path, img_path\n    \n    def __len__(self):\n        \n        return len(self.seg_paths)","metadata":{"execution":{"iopub.status.busy":"2022-09-19T15:57:57.047469Z","iopub.execute_input":"2022-09-19T15:57:57.048678Z","iopub.status.idle":"2022-09-19T15:57:58.827203Z","shell.execute_reply.started":"2022-09-19T15:57:57.048643Z","shell.execute_reply":"2022-09-19T15:57:58.82512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = Dataset(df_train)\nprint(\"Dataset len : \", dataset.__len__())\nprint(\"NII path : \", dataset.__getitem__(0)[0])\nprint(\"DCM path : \", dataset.__getitem__(0)[1])","metadata":{"execution":{"iopub.status.busy":"2022-09-19T15:57:58.828962Z","iopub.execute_input":"2022-09-19T15:57:58.829698Z","iopub.status.idle":"2022-09-19T15:57:58.839475Z","shell.execute_reply.started":"2022-09-19T15:57:58.829653Z","shell.execute_reply":"2022-09-19T15:57:58.837716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}