{"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 pylibjpeg-libjpeg pydicom","metadata":{"execution":{"iopub.status.busy":"2022-09-05T18:56:09.282123Z","iopub.execute_input":"2022-09-05T18:56:09.283177Z","iopub.status.idle":"2022-09-05T18:56:24.502084Z","shell.execute_reply.started":"2022-09-05T18:56:09.283068Z","shell.execute_reply":"2022-09-05T18:56:24.500628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import 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\n\nfrom pydicom import dcmread\nimport pylibjpeg\nimport cv2\nimport matplotlib.pyplot as plt \nfrom random import randint\n\n\n# Packages\nimport nibabel as nb\nimport os\nimport sys\nfrom pathlib import Path\nimport warnings\nwarnings.simplefilter(\"ignore\")\n","metadata":{"execution":{"iopub.status.busy":"2022-09-05T18:56:24.505615Z","iopub.execute_input":"2022-09-05T18:56:24.506175Z","iopub.status.idle":"2022-09-05T18:56:28.895521Z","shell.execute_reply.started":"2022-09-05T18:56:24.506118Z","shell.execute_reply":"2022-09-05T18:56:28.894156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train ","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-09-05T18:56:28.897403Z","iopub.execute_input":"2022-09-05T18:56:28.899187Z","iopub.status.idle":"2022-09-05T18:56:28.920844Z","shell.execute_reply.started":"2022-09-05T18:56:28.899138Z","shell.execute_reply":"2022-09-05T18:56:28.919502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-09-05T18:56:28.924849Z","iopub.execute_input":"2022-09-05T18:56:28.925312Z","iopub.status.idle":"2022-09-05T18:56:28.953333Z","shell.execute_reply.started":"2022-09-05T18:56:28.925269Z","shell.execute_reply":"2022-09-05T18:56:28.95205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-09-05T18:56:28.955087Z","iopub.execute_input":"2022-09-05T18:56:28.956113Z","iopub.status.idle":"2022-09-05T18:56:28.986012Z","shell.execute_reply.started":"2022-09-05T18:56:28.956065Z","shell.execute_reply":"2022-09-05T18:56:28.984991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- patient_overall = 0 if all others equal 0 meeans no fracturation ","metadata":{}},{"cell_type":"markdown","source":"- Considering all the predicted columns [C1-C7] as a one hot encoding and covert them to label encoding, all in one column","metadata":{}},{"cell_type":"code","source":"\n\ntrain['predicted'] = train['C1'].map(str) + ',' +  train['C2'].map(str) + ','+ train['C3'].map(str) + ','+ train['C4'].map(str) + \\\n','+ train['C5'].map(str) + ','+ train['C6'].map(str) + ','+ train['C7'].map(str) \n\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-05T18:56:28.9876Z","iopub.execute_input":"2022-09-05T18:56:28.988254Z","iopub.status.idle":"2022-09-05T18:56:29.013655Z","shell.execute_reply.started":"2022-09-05T18:56:28.988213Z","shell.execute_reply":"2022-09-05T18:56:29.012597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_overall_count = train.groupby('patient_overall')['StudyInstanceUID'].nunique()\nplt.figure(figsize = (7,4))\npatient_overall_count.plot(kind= 'bar', rot=0)","metadata":{"execution":{"iopub.status.busy":"2022-09-05T18:56:29.01511Z","iopub.execute_input":"2022-09-05T18:56:29.015425Z","iopub.status.idle":"2022-09-05T18:56:29.280988Z","shell.execute_reply.started":"2022-09-05T18:56:29.015397Z","shell.execute_reply":"2022-09-05T18:56:29.279457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- As can be seen we have 70 unique possible classes than we can work on ","metadata":{}},{"cell_type":"markdown","source":"# Test","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-09-05T18:56:29.286105Z","iopub.execute_input":"2022-09-05T18:56:29.286765Z","iopub.status.idle":"2022-09-05T18:56:29.298496Z","shell.execute_reply.started":"2022-09-05T18:56:29.286731Z","shell.execute_reply":"2022-09-05T18:56:29.297187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-09-05T18:56:29.300645Z","iopub.execute_input":"2022-09-05T18:56:29.301104Z","iopub.status.idle":"2022-09-05T18:56:29.312261Z","shell.execute_reply.started":"2022-09-05T18:56:29.30107Z","shell.execute_reply":"2022-09-05T18:56:29.31091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# train_bounding_boxes","metadata":{}},{"cell_type":"code","source":"train_bounding_boxes = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/train_bounding_boxes.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-09-05T18:56:29.316607Z","iopub.execute_input":"2022-09-05T18:56:29.317516Z","iopub.status.idle":"2022-09-05T18:56:29.350251Z","shell.execute_reply.started":"2022-09-05T18:56:29.317478Z","shell.execute_reply":"2022-09-05T18:56:29.349023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_bounding_boxes.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-09-05T18:56:29.35157Z","iopub.execute_input":"2022-09-05T18:56:29.352001Z","iopub.status.idle":"2022-09-05T18:56:29.368915Z","shell.execute_reply.started":"2022-09-05T18:56:29.351961Z","shell.execute_reply":"2022-09-05T18:56:29.367986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sample_submission:","metadata":{}},{"cell_type":"code","source":"sample_submission= pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-09-05T18:56:29.370097Z","iopub.execute_input":"2022-09-05T18:56:29.370922Z","iopub.status.idle":"2022-09-05T18:56:29.387084Z","shell.execute_reply.started":"2022-09-05T18:56:29.370886Z","shell.execute_reply":"2022-09-05T18:56:29.385713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission","metadata":{"execution":{"iopub.status.busy":"2022-09-05T18:56:29.388886Z","iopub.execute_input":"2022-09-05T18:56:29.389721Z","iopub.status.idle":"2022-09-05T18:56:29.400185Z","shell.execute_reply.started":"2022-09-05T18:56:29.38968Z","shell.execute_reply":"2022-09-05T18:56:29.399013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Img Dataset: ---------------------------------------------------------------","metadata":{}},{"cell_type":"markdown","source":"# Import packages:","metadata":{}},{"cell_type":"markdown","source":"## Train_images:","metadata":{}},{"cell_type":"code","source":"\ndef load_dicom(path):\n    \"\"\"\n    This supports loading both regular and compressed JPEG images. \n    See the first sell with `pip install` commands for the necessary dependencies\n    \"\"\"\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.10001/{rand}.dcm')\nplt.figure(figsize = (10,10\n                ))\nplt.imshow(im)\nplt.title('Train regular image')","metadata":{"execution":{"iopub.status.busy":"2022-09-05T18:56:40.771137Z","iopub.execute_input":"2022-09-05T18:56:40.771536Z","iopub.status.idle":"2022-09-05T18:56:41.253499Z","shell.execute_reply.started":"2022-09-05T18:56:40.771497Z","shell.execute_reply":"2022-09-05T18:56:41.25211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Segmentations:","metadata":{}},{"cell_type":"code","source":"\npath = '../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","metadata":{"execution":{"iopub.status.busy":"2022-09-05T18:56:57.985885Z","iopub.execute_input":"2022-09-05T18:56:57.987542Z","iopub.status.idle":"2022-09-05T18:56:59.722496Z","shell.execute_reply.started":"2022-09-05T18:56:57.987478Z","shell.execute_reply":"2022-09-05T18:56:59.721091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot images\ntest = seg[:,:,200]\nplt.imshow(test)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-05T18:57:04.929509Z","iopub.execute_input":"2022-09-05T18:57:04.930061Z","iopub.status.idle":"2022-09-05T18:57:05.16924Z","shell.execute_reply.started":"2022-09-05T18:57:04.930014Z","shell.execute_reply":"2022-09-05T18:57:05.167614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Hint\n- We can visualize the segmentations just by the code provided [here](https://www.vincentkoppelmans.com/neuroscience/quick-visualization-of-nifti-images/)","metadata":{}},{"cell_type":"code","source":"# Quick display of a Nifti image\n\n# Disable Toolbar for plots\nplt.rcParams['toolbar'] = 'None'\n\n# Environment and file names\nhome = str(Path.home())\niFile = '../input/rsna-2022-cervical-spine-fracture-detection/segmentations/1.2.826.0.1.3680043.10633.nii'\noFile=(str(os.path.basename(iFile).replace('.nii.gz','.png').replace('.nii','.png')))\n\n# Set rounding\nnp.set_printoptions(formatter={'float': lambda x: \"{0:0.2f}\".format(x)})\n\n\n\n### IMPORT DATA ###\n# Load data\nimage=nb.load(iFile)\n\n# 3D data\nif image.header['dim'][0]==3:\n    data=image.get_data()\n    # 4D data\nelif  image.header['dim'][0]==4:\n    data=image.get_data()[:,:,:,0]\n\n# Header\nheader=image.header\n\n# Set NAN to 0\ndata[np.isnan(data)] = 0\n\n\n\n### PREPARE SOME PARAMETERS ###\n\n# Spacing for Aspect Ratio\nsX=header['pixdim'][1]\nsY=header['pixdim'][2]\nsZ=header['pixdim'][3]\n\n# Size per slice\nlX = data.shape[0]\nlY = data.shape[1]\nlZ = data.shape[2]\n\n# Middle slice number\nmX = int(lX/2)\nmY = int(lY/2)\nmZ = int(lZ/2)\n\n# True middle point\ntmX = lX/2.0\ntmY = lY/2.0\ntmZ = lZ/2.0\n\n\n\n### ORIENTATION ###\nqfX = image.get_qform()[0,0]\nsfX = image.get_sform()[0,0]\n\nif qfX < 0 and (sfX == 0 or sfX < 0):\n    oL = 'R'\n    oR = 'L'\nelif qfX > 0 and (sfX == 0 or sfX > 0):\n    oL = 'L'\n    oR = 'R'\nif sfX < 0 and (qfX == 0 or qfX < 0):\n    oL = 'R'\n    oR = 'L'\nelif sfX > 0 and (qfX == 0 or qfX > 0):\n    oL = 'L'\n    oR = 'R'\n\n\n\n### PLOTTING ###\n\n# Plot main window\nfig = plt.figure(\n    facecolor='black',\n    figsize=(5,4),\n    dpi=200\n)\n\n# Black background\nplt.style.use('dark_background')\n\n# Set title\nfig.canvas.set_window_title(oFile.replace('.png',''))\n\n\n# Coronal\nax1=fig.add_subplot(2,2,1)\nimgplot = plt.imshow(\n    np.rot90(data[:,mY,:]),\n    aspect=sZ/sX,\n)\nimgplot.set_cmap('gray')\n\nax1.hlines(tmZ, 0, lX, colors='red', linestyles='dotted', linewidth=.5)\nax1.vlines(tmX, 0, lZ, colors='red', linestyles='dotted', linewidth=.5)\n\nplt.axis('off')\n\n\n# Sagittal\nax2=fig.add_subplot(2,2,2)\nimgplot = plt.imshow(\n    np.rot90(data[mX,:,:]),\n    aspect=sZ/sY,\n)\nimgplot.set_cmap('gray')\n\nax2.hlines(tmZ, 0, lY, colors='red', linestyles='dotted', linewidth=.5)\nax2.vlines(tmY, 0, lZ, colors='red', linestyles='dotted', linewidth=.5)\n\nplt.axis('off')\n\n\n# Axial\nax3=fig.add_subplot(2,2,3)\nimgplot = plt.imshow(\n    np.rot90(data[:,:,mZ]),\n    aspect=sY/sX\n)\nimgplot.set_cmap('gray')\n\nax3.hlines(tmY, 0, lX, colors='red', linestyles='dotted', linewidth=.5)\nax3.vlines(tmX, 0, lY, colors='red', linestyles='dotted', linewidth=.5)\n\nplt.axis('off')\n\nplt.text(-10, mY+5, oL, fontsize=9, color='red') # Label on left side\n\n\n# Textual information\n# sform code\nsform=np.round(image.get_sform(),decimals=2)\nsform_txt=str(sform).replace('[',' ').replace(']',' ').replace(' ','   ').replace('   -','  -')\n\n# qform code\nqform=np.round(image.get_qform(),decimals=2)\nqform_txt=str(qform).replace('[',' ').replace(']',' ').replace(' ','   ').replace('   -','  -')\n\n# Dimensions\ndims=str(data.shape).replace(', ',' x ').replace('(','').replace(')','')\ndim=(\"Dimensions: \"+dims)\n\n# Spacing\nspacing=(\"Spacing: \"\n         +str(np.round(sX, decimals=2))\n         +\" x \"\n         +str(np.round(sY, decimals=2))\n         +\" x \"\n         +str(np.round(sZ, decimals=2))\n         +\" mm\"\n)\n\n# Data type\ntype=image.header.get_data_dtype()\ntype_str=(\"Data type: \"+str(type))\n\n# Volumes\nvolumes=(\"Volumes: \"+str(image.header['dim'][4]))\n\n# Range\nmin=np.round(np.amin(data), decimals=2)\nmax=np.round(np.amax(data), decimals=2)\nrange=(\"Range: \"+str(min)+\" - \"+str(max))\n\ntext=(\n    dim+\"\\n\"\n    +spacing+\"\\n\"\n    +volumes+\"\\n\"\n    +type_str+\"\\n\"\n    +range+\"\\n\\n\"\n    +\"sform code:\\n\"\n    +sform_txt+\"\\n\"\n    +\"\\nqform code:\\n\"\n    +qform_txt\n)\n\n# Plot text subplot\nax4=fig.add_subplot(2,2,4)\nplt.text(\n    0.15,\n    0.95,\n    text,\n    horizontalalignment='left',\n    verticalalignment='top',\n    size=6,\n    color='white',\n)\nplt.axis('off')\n\n# Adjust whitespace\nplt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)\n\n# Display\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-05T18:57:05.818673Z","iopub.execute_input":"2022-09-05T18:57:05.81917Z","iopub.status.idle":"2022-09-05T18:57:09.647317Z","shell.execute_reply.started":"2022-09-05T18:57:05.819129Z","shell.execute_reply":"2022-09-05T18:57:09.6461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}