{"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":"# **Please, upvote if u find the code useful :D**","metadata":{}},{"cell_type":"code","source":"!pip install -U pylibjpeg[all]\n","metadata":{"execution":{"iopub.status.busy":"2022-09-16T11:52:36.091463Z","iopub.execute_input":"2022-09-16T11:52:36.092037Z","iopub.status.idle":"2022-09-16T11:52:52.729565Z","shell.execute_reply.started":"2022-09-16T11:52:36.091977Z","shell.execute_reply":"2022-09-16T11:52:52.728008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install cython","metadata":{"execution":{"iopub.status.busy":"2022-09-16T11:52:52.732582Z","iopub.execute_input":"2022-09-16T11:52:52.733095Z","iopub.status.idle":"2022-09-16T11:53:04.644248Z","shell.execute_reply.started":"2022-09-16T11:52:52.733049Z","shell.execute_reply":"2022-09-16T11:53:04.642566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install pylibjpeg pylibjpeg-libjpeg pydicom","metadata":{"execution":{"iopub.status.busy":"2022-09-16T11:53:04.646215Z","iopub.execute_input":"2022-09-16T11:53:04.646746Z","iopub.status.idle":"2022-09-16T11:53:16.413621Z","shell.execute_reply.started":"2022-09-16T11:53:04.646699Z","shell.execute_reply":"2022-09-16T11:53:16.41169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pillow","metadata":{"execution":{"iopub.status.busy":"2022-09-16T11:53:16.41695Z","iopub.execute_input":"2022-09-16T11:53:16.417756Z","iopub.status.idle":"2022-09-16T11:53:28.22687Z","shell.execute_reply.started":"2022-09-16T11:53:16.417706Z","shell.execute_reply":"2022-09-16T11:53:28.225521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -U python-gdcm","metadata":{"execution":{"iopub.status.busy":"2022-09-16T11:53:28.229153Z","iopub.execute_input":"2022-09-16T11:53:28.229632Z","iopub.status.idle":"2022-09-16T11:53:42.390211Z","shell.execute_reply.started":"2022-09-16T11:53:28.229575Z","shell.execute_reply":"2022-09-16T11:53:42.388745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import dill\n# dill.load_session('../input/rsna-2022-data-analysis/your_bk_dill.pkl')","metadata":{"execution":{"iopub.status.busy":"2022-09-16T11:53:42.392812Z","iopub.execute_input":"2022-09-16T11:53:42.393306Z","iopub.status.idle":"2022-09-16T11:53:56.655357Z","shell.execute_reply.started":"2022-09-16T11:53:42.39326Z","shell.execute_reply":"2022-09-16T11:53:56.653522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport numpy as np # linear algebra\n\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\n\nimport matplotlib.pyplot as plt\n\nimport seaborn as sns\n\nimport cv2\n\nimport os\n\nimport numpy as np\n\nfrom nibabel.testing import data_path\n\nfrom tensorflow.keras.applications import VGG16\n\nfrom tensorflow.keras.layers import Flatten\n\nfrom tensorflow.keras.layers import Dense\n\nfrom tensorflow.keras.layers import Input\n\nfrom tensorflow.keras.models import Model\n\nfrom tensorflow.keras.optimizers import Adam\n\nfrom tensorflow.keras.preprocessing.image import img_to_array\n\nfrom tensorflow.keras.preprocessing.image import load_img\n\nfrom sklearn.model_selection import train_test_split\n\n#import gdcm\n\nimport pydicom as dicom\n\nimport cv2\n\nimport pylibjpeg\n\nimport dill\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T19:33:03.525115Z","iopub.execute_input":"2022-09-15T19:33:03.526217Z","iopub.status.idle":"2022-09-15T19:33:13.993714Z","shell.execute_reply.started":"2022-09-15T19:33:03.526111Z","shell.execute_reply":"2022-09-15T19:33:13.991563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train.csv')\nimport csv","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.189307Z","iopub.status.idle":"2022-09-15T16:16:03.189756Z","shell.execute_reply.started":"2022-09-15T16:16:03.189564Z","shell.execute_reply":"2022-09-15T16:16:03.189583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.to_csv('data.csv')","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.190988Z","iopub.status.idle":"2022-09-15T16:16:03.192293Z","shell.execute_reply.started":"2022-09-15T16:16:03.191962Z","shell.execute_reply":"2022-09-15T16:16:03.191993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.193569Z","iopub.status.idle":"2022-09-15T16:16:03.194489Z","shell.execute_reply.started":"2022-09-15T16:16:03.194145Z","shell.execute_reply":"2022-09-15T16:16:03.194175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 6))\n\nsns.countplot(df_train['C1'])\nplt.title(\"Distribution of C1\", fontsize=20)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.196262Z","iopub.status.idle":"2022-09-15T16:16:03.196852Z","shell.execute_reply.started":"2022-09-15T16:16:03.196557Z","shell.execute_reply":"2022-09-15T16:16:03.196584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 6))\n\nsns.countplot(df_train['C2'])\nplt.title(\"Distribution of C2\", fontsize=20)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.198472Z","iopub.status.idle":"2022-09-15T16:16:03.199108Z","shell.execute_reply.started":"2022-09-15T16:16:03.1988Z","shell.execute_reply":"2022-09-15T16:16:03.198827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 6))\n\nsns.countplot(df_train['C3'])\nplt.title(\"Distribution of C3\", fontsize=20)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.200764Z","iopub.status.idle":"2022-09-15T16:16:03.201354Z","shell.execute_reply.started":"2022-09-15T16:16:03.201031Z","shell.execute_reply":"2022-09-15T16:16:03.201059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 6))\n\nsns.countplot(df_train['C4'])\nplt.title(\"Distribution of C4\", fontsize=20)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.20314Z","iopub.status.idle":"2022-09-15T16:16:03.203719Z","shell.execute_reply.started":"2022-09-15T16:16:03.203425Z","shell.execute_reply":"2022-09-15T16:16:03.203452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 6))\n\nsns.countplot(df_train['C5'])\nplt.title(\"Distribution of C5\", fontsize=20)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.205488Z","iopub.status.idle":"2022-09-15T16:16:03.206045Z","shell.execute_reply.started":"2022-09-15T16:16:03.205762Z","shell.execute_reply":"2022-09-15T16:16:03.20579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 6))\n\nsns.countplot(df_train['C6'])\nplt.title(\"Distribution of C6\", fontsize=20)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.207893Z","iopub.status.idle":"2022-09-15T16:16:03.208474Z","shell.execute_reply.started":"2022-09-15T16:16:03.208162Z","shell.execute_reply":"2022-09-15T16:16:03.208188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 6))\n\nsns.countplot(df_train['C7'])\nplt.title(\"Distribution of C7\", fontsize=20)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.209747Z","iopub.status.idle":"2022-09-15T16:16:03.210131Z","shell.execute_reply.started":"2022-09-15T16:16:03.209938Z","shell.execute_reply":"2022-09-15T16:16:03.209956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 6))\n\nsns.countplot(df_train['patient_overall'])\nplt.title(\"Distribution of patient overall\", fontsize=20)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.211286Z","iopub.status.idle":"2022-09-15T16:16:03.211724Z","shell.execute_reply.started":"2022-09-15T16:16:03.21152Z","shell.execute_reply":"2022-09-15T16:16:03.211539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ndirector = '../input/rsna-2022-cervical-spine-fracture-detection/train_images'\ncase = os.listdir(director)\ncase\ncase_day = []\nfile_name = []\nimage_matrix = []\nfor i in case:\n    directory_ = '../input/rsna-2022-cervical-spine-fracture-detection/train_images/' + i\n    day = os.listdir(directory_)\n    case_day.append(day)\n    image_matrix.append(i)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.212723Z","iopub.status.idle":"2022-09-15T16:16:03.21309Z","shell.execute_reply.started":"2022-09-15T16:16:03.212906Z","shell.execute_reply":"2022-09-15T16:16:03.212924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Access to the folders","metadata":{}},{"cell_type":"code","source":"train_bounding_boxes = df_train.copy()\ntrain_bounding_boxes = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train_bounding_boxes.csv')\ntrain_bounding_boxes.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.213937Z","iopub.status.idle":"2022-09-15T16:16:03.214302Z","shell.execute_reply.started":"2022-09-15T16:16:03.214115Z","shell.execute_reply":"2022-09-15T16:16:03.214131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# whiteblankimage = 255 * np.ones(shape=[512, 512, 3], dtype=np.uint8)\n\n\n# cv2.rectangle(whiteblankimage, pt1=(200,200), pt2=(300,300), color=(0,0,255), thickness=10)\n\n# plt.imshow(whiteblankimage)\n\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.215596Z","iopub.status.idle":"2022-09-15T16:16:03.21596Z","shell.execute_reply.started":"2022-09-15T16:16:03.215773Z","shell.execute_reply":"2022-09-15T16:16:03.21579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"x = []\ny = []\nfor i in range(len(train_bounding_boxes['x'])-1):\n    x.append(float(train_bounding_boxes['x'][i])+float(train_bounding_boxes['width'][i]))\n    y.append(float(train_bounding_boxes['y'][i])+float(train_bounding_boxes['height'][i]))","metadata":{"execution":{"iopub.status.busy":"2022-08-25T19:33:44.495747Z","iopub.execute_input":"2022-08-25T19:33:44.496253Z","iopub.status.idle":"2022-08-25T19:33:44.711555Z","shell.execute_reply.started":"2022-08-25T19:33:44.496201Z","shell.execute_reply":"2022-08-25T19:33:44.709946Z"}}},{"cell_type":"code","source":"x1 = []\ny1 = []\nx2 = []\ny2 = []\nfor i in range(len(train_bounding_boxes['x'])-1):\n    x1.append(float(train_bounding_boxes['x'][i])+float(train_bounding_boxes['width'][i]))\n    y1.append(float(train_bounding_boxes['y'][i])+float(train_bounding_boxes['height'][i]))\n    x2.append(float(train_bounding_boxes['x'][i])-float(train_bounding_boxes['width'][i]))\n    y2.append(float(train_bounding_boxes['y'][i])-float(train_bounding_boxes['height'][i]))","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.217063Z","iopub.status.idle":"2022-09-15T16:16:03.217733Z","shell.execute_reply.started":"2022-09-15T16:16:03.217493Z","shell.execute_reply":"2022-09-15T16:16:03.217517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from PIL import image\n\n# path = '../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001/1.dcm'\n\n# im = Image.open(path)\n\n# rgb_im = im.convert('RGB')\n\n# rgb_im.save('colors.jpg')\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.219129Z","iopub.status.idle":"2022-09-15T16:16:03.219585Z","shell.execute_reply.started":"2022-09-15T16:16:03.219324Z","shell.execute_reply":"2022-09-15T16:16:03.219363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport pydicom\nfrom pydicom.data import get_testdata_files\nfilename =('../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001/1.dcm')\nds = pydicom.dcmread(filename)\nplt.imshow(ds.pixel_array,cmap = plt.cm.gray)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.222007Z","iopub.status.idle":"2022-09-15T16:16:03.222635Z","shell.execute_reply.started":"2022-09-15T16:16:03.222305Z","shell.execute_reply":"2022-09-15T16:16:03.222332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we draw the square in the picture","metadata":{}},{"cell_type":"code","source":"# Python program to explain cv2.rectangle() method\n\t\n# importing cv2\nimport cv2\n\n# path\npath = ('../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10051/133.dcm')\nds = pydicom.dcmread(path)\n# Reading an image in grayscale mode\nimage = ds.pixel_array\n\n# Window name in which image is displayed\nwindow_name = 'Image'\n\n# Start coordinate, here (100, 50)\n# represents the top left corner of rectangle\nstart_point = (int(x1[0]), int(y1[0]))\n\n# Ending coordinate, here (125, 80)\n# represents the bottom right corner of rectangle\nend_point = (int(x2[0]), int(y2[0]))\n\n# Black color in BGR\ncolor = (0, 0, 0)\n\n# Line thickness of -1 px\n# Thickness of -1 will fill the entire shape\nthickness = -1\n\n# Using cv2.rectangle() method\n# Draw a rectangle of black color of thickness -1 px\nimage = cv2.rectangle(image, start_point, end_point, color, thickness)\n\n# Displaying the image\nplt.imshow(image,cmap = plt.cm.gray)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.224542Z","iopub.status.idle":"2022-09-15T16:16:03.225103Z","shell.execute_reply.started":"2022-09-15T16:16:03.224816Z","shell.execute_reply":"2022-09-15T16:16:03.224845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_filename = os.path.join('../input/rsna-2022-cervical-spine-fracture-detection/segmentations', '1.2.826.0.1.3680043.10633.nii')","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.229204Z","iopub.status.idle":"2022-09-15T16:16:03.229918Z","shell.execute_reply.started":"2022-09-15T16:16:03.229567Z","shell.execute_reply":"2022-09-15T16:16:03.229599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import nibabel as nib\nimg = nib.load(example_filename)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.231716Z","iopub.status.idle":"2022-09-15T16:16:03.232305Z","shell.execute_reply.started":"2022-09-15T16:16:03.232006Z","shell.execute_reply":"2022-09-15T16:16:03.232035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.234027Z","iopub.status.idle":"2022-09-15T16:16:03.234652Z","shell.execute_reply.started":"2022-09-15T16:16:03.234329Z","shell.execute_reply":"2022-09-15T16:16:03.234377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(img)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.236022Z","iopub.status.idle":"2022-09-15T16:16:03.236618Z","shell.execute_reply.started":"2022-09-15T16:16:03.236289Z","shell.execute_reply":"2022-09-15T16:16:03.236317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"header = img.header\nprint(header)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.239146Z","iopub.status.idle":"2022-09-15T16:16:03.239642Z","shell.execute_reply.started":"2022-09-15T16:16:03.239384Z","shell.execute_reply":"2022-09-15T16:16:03.239408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_bold = img.get_fdata()\n\n\n\nx_slice = data_bold[9, :]\ny_slice = data_bold[:, 19]\n\ndata_bold_t0 = [x_slice,y_slice]\n\nimport matplotlib.pyplot as plt\n\nfig, axes = plt.subplots(1, len(data_bold_t0))\nfor i, slice in enumerate(data_bold_t0):\n    axes[i].imshow(slice.T, cmap=\"gray\", origin=\"lower\")\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.241838Z","iopub.status.idle":"2022-09-15T16:16:03.242261Z","shell.execute_reply.started":"2022-09-15T16:16:03.242062Z","shell.execute_reply":"2022-09-15T16:16:03.242082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This are the necessary data we need to apply a softmax regression:","metadata":{}},{"cell_type":"code","source":"filenames = []\ndata = []\ntargets = []\na = np.array(img.dataobj)\ndata.append(a)\ntargets.append((x1[0], y1[0], x2[0], y2[0]))\nfilename = '133.dcm'\nfilenames.append(filename)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.243561Z","iopub.status.idle":"2022-09-15T16:16:03.243969Z","shell.execute_reply.started":"2022-09-15T16:16:03.243773Z","shell.execute_reply":"2022-09-15T16:16:03.243793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's calculate them for all the data","metadata":{}},{"cell_type":"code","source":"targets = []\nfolder = []\npath0='../input/rsna-2022-cervical-spine-fracture-detection/train_images/'\n\nfor i in os.listdir(path0):\n    nombre = os.listdir(path0 + i)\n    folder.append(nombre)\n\n    path = path0 + i\n    data = []\n    for img in os.listdir(path):\n        path2 = path0 + i + '/'+ img\n    \n    \n        ds = pydicom.dcmread(path2)\n        image = ds.pixel_array\n        data.append(image)\n#         pixel_array_numpy = ds.pixel_array\n#         image_format = '.jpg' # or '.png'\n#         image_path = path2.replace('.dcm', image_format)\n#         cv2.imwrite(path2+'.jpg', pixel_array_numpy)\n\ndill.dump_session('./your_bk_dill.pkl')\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.246359Z","iopub.status.idle":"2022-09-15T16:16:03.24675Z","shell.execute_reply.started":"2022-09-15T16:16:03.246564Z","shell.execute_reply":"2022-09-15T16:16:03.246583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#a = open('../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001/1.dcm', mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T16:16:03.247896Z","iopub.status.idle":"2022-09-15T16:16:03.248267Z","shell.execute_reply.started":"2022-09-15T16:16:03.248085Z","shell.execute_reply":"2022-09-15T16:16:03.248103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}