{"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":"code","source":"! pip install pylibjpeg -q\n! pip install python-gdcm -q\n! pip install pylibjpeg-libjpeg -q","metadata":{"execution":{"iopub.status.busy":"2022-10-31T01:22:07.013204Z","iopub.execute_input":"2022-10-31T01:22:07.013734Z","iopub.status.idle":"2022-10-31T01:22:40.458308Z","shell.execute_reply.started":"2022-10-31T01:22:07.013692Z","shell.execute_reply":"2022-10-31T01:22:40.455977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport pandas as pd\nimport numpy as np \nimport scipy as sc \nimport pydicom as dicom\nimport torch\nimport torchvision as tv\nfrom tqdm.notebook import tqdm\nimport wandb\nimport tensorflow as tf\n\nfrom pydicom import dcmread\nimport pylibjpeg\nimport cv2\nimport matplotlib.pyplot as plt \nimport random\nfrom random import randint\n\n# Packages\nimport nibabel as nb\nimport os\nimport sys\nimport math\nfrom pathlib import Path\nimport warnings\nwarnings.simplefilter(\"ignore\")\n\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-31T01:23:56.935392Z","iopub.execute_input":"2022-10-31T01:23:56.935899Z","iopub.status.idle":"2022-10-31T01:23:56.945102Z","shell.execute_reply.started":"2022-10-31T01:23:56.935859Z","shell.execute_reply":"2022-10-31T01:23:56.943651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root = Path('../input/rsna-2022-cervical-spine-fracture-detection')\ntrain_folder = root/'train_images'\ntest_folder = root/'test_images'\n\nseg_folder = root/'segmentations'\nsegmentations = list(seg_folder.iterdir())\nseg_ids = [o.stem for o in segmentations]\n\ntrain_df = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/train.csv\")\ntest_df = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/test.csv\")\nbbdf = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train_bounding_boxes.csv')\n\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-31T01:22:40.886308Z","iopub.execute_input":"2022-10-31T01:22:40.88688Z","iopub.status.idle":"2022-10-31T01:22:41.004204Z","shell.execute_reply.started":"2022-10-31T01:22:40.886835Z","shell.execute_reply":"2022-10-31T01:22:41.002637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-31T01:22:41.00707Z","iopub.execute_input":"2022-10-31T01:22:41.007441Z","iopub.status.idle":"2022-10-31T01:22:41.022082Z","shell.execute_reply.started":"2022-10-31T01:22:41.007406Z","shell.execute_reply":"2022-10-31T01:22:41.020913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom(path):\n    img=dicom.dcmread(path)\n    img.PhotometricInterpretation = 'YBR_FULL'\n    data = img.pixel_array    \n    data = data - np.min(data)\n    if np.max(data) != 0:\n        data = data / np.max(data)\n    data=(data * 255).astype(np.uint8)\n    return cv2.cvtColor(data, cv2.COLOR_GRAY2RGB), img\n\nrand= randint(0, 10)\nprint(f'1.2.826.0.1.3680043.10001/{rand}.dcm')\nim, meta = load_dicom(f'../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.11170/{rand}.dcm')\n\nplt.figure(figsize = (10,10))\nplt.imshow(im)\nplt.title('Train regular image')","metadata":{"execution":{"iopub.status.busy":"2022-10-31T01:22:41.024022Z","iopub.execute_input":"2022-10-31T01:22:41.024634Z","iopub.status.idle":"2022-10-31T01:22:41.451565Z","shell.execute_reply.started":"2022-10-31T01:22:41.024593Z","shell.execute_reply":"2022-10-31T01:22:41.450522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"randomlist = random.sample(range(1, 125), 25)\ncount = 0\n\nplt.figure(figsize=(15,15))\n\nfor sel in randomlist:\n    print(f'1.2.826.0.1.3680043.10001/{sel}.dcm')\n    im, meta = load_dicom(f'../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.11170/{sel}.dcm')\n    plt.subplot(5, 5, count+1)\n    plt.xticks([])\n    plt.imshow(im, cmap='gray')\n    plt.xlabel(f'{sel}.dcm')\n    count+=1","metadata":{"execution":{"iopub.status.busy":"2022-10-31T01:35:41.118228Z","iopub.execute_input":"2022-10-31T01:35:41.118728Z","iopub.status.idle":"2022-10-31T01:35:45.038835Z","shell.execute_reply.started":"2022-10-31T01:35:41.118681Z","shell.execute_reply":"2022-10-31T01:35:45.03686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/rsna-2022-cervical-spine-fracture-detection/segmentations/1.2.826.0.1.3680043.12833.nii'\nimg = nb.load(path)\nprint(img)\n\n# Convert to numpy array\nseg = img.get_fdata()[:, ::-1, ::-1].transpose(2, 1, 0)\nseg.shape\n\n# Plot images\ntest = seg[:,:,200]\nplt.imshow(test)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-31T01:22:41.502251Z","iopub.status.idle":"2022-10-31T01:22:41.50272Z","shell.execute_reply.started":"2022-10-31T01:22:41.502491Z","shell.execute_reply":"2022-10-31T01:22:41.502514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-31T01:22:41.505499Z","iopub.status.idle":"2022-10-31T01:22:41.50643Z","shell.execute_reply.started":"2022-10-31T01:22:41.506196Z","shell.execute_reply":"2022-10-31T01:22:41.506219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[train_df.columns[2:]].sum().plot.bar(rot=0, color='DarkTurquoise')\nplt.title(\"Fracturas por sección C\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-31T01:22:41.507561Z","iopub.status.idle":"2022-10-31T01:22:41.508816Z","shell.execute_reply.started":"2022-10-31T01:22:41.508436Z","shell.execute_reply":"2022-10-31T01:22:41.508483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.patient_overall.value_counts().sort_values().plot(kind = 'barh', color='brown')\nplt.title(\"Patient overall fractures\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-31T01:22:41.510931Z","iopub.status.idle":"2022-10-31T01:22:41.511483Z","shell.execute_reply.started":"2022-10-31T01:22:41.511238Z","shell.execute_reply":"2022-10-31T01:22:41.511258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}