{"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":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt \nimport os\nimport pydicom\nimport cv2 as cv\n\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-30T08:11:08.07528Z","iopub.execute_input":"2022-07-30T08:11:08.076389Z","iopub.status.idle":"2022-07-30T08:11:08.08199Z","shell.execute_reply.started":"2022-07-30T08:11:08.076347Z","shell.execute_reply":"2022-07-30T08:11:08.080593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading CSV files","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/train.csv\")\ndf_train","metadata":{"execution":{"iopub.status.busy":"2022-07-30T08:11:08.128123Z","iopub.execute_input":"2022-07-30T08:11:08.128599Z","iopub.status.idle":"2022-07-30T08:11:08.152733Z","shell.execute_reply.started":"2022-07-30T08:11:08.128548Z","shell.execute_reply":"2022-07-30T08:11:08.151772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"non_fracture = df_train[df_train.patient_overall==0]\nfracture = df_train[df_train.patient_overall==1]\n\nprint(len(non_fracture))\nprint(len(fracture))","metadata":{"execution":{"iopub.status.busy":"2022-07-30T08:11:08.173283Z","iopub.execute_input":"2022-07-30T08:11:08.174482Z","iopub.status.idle":"2022-07-30T08:11:08.183919Z","shell.execute_reply.started":"2022-07-30T08:11:08.17444Z","shell.execute_reply":"2022-07-30T08:11:08.182569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# count frequencies by column\nCs = ['C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7']\ndf_train[Cs].sum().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T08:11:08.218541Z","iopub.execute_input":"2022-07-30T08:11:08.219217Z","iopub.status.idle":"2022-07-30T08:11:08.4219Z","shell.execute_reply.started":"2022-07-30T08:11:08.219169Z","shell.execute_reply":"2022-07-30T08:11:08.420533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_annot = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/train_bounding_boxes.csv\")\ndf_annot","metadata":{"execution":{"iopub.status.busy":"2022-07-30T08:11:08.42501Z","iopub.execute_input":"2022-07-30T08:11:08.425556Z","iopub.status.idle":"2022-07-30T08:11:08.456353Z","shell.execute_reply.started":"2022-07-30T08:11:08.425506Z","shell.execute_reply":"2022-07-30T08:11:08.454914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_annot[\"StudyInstanceUID\"].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-07-30T08:11:08.458109Z","iopub.execute_input":"2022-07-30T08:11:08.458467Z","iopub.status.idle":"2022-07-30T08:11:08.470602Z","shell.execute_reply.started":"2022-07-30T08:11:08.458437Z","shell.execute_reply":"2022-07-30T08:11:08.469119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading DICOM images and visualize as axial, coronal, and saggital planes","metadata":{}},{"cell_type":"markdown","source":"> ","metadata":{}},{"cell_type":"code","source":"dir_train='../input/rsna-2022-cervical-spine-fracture-detection/train_images'\nfiles = os.listdir(dir_train)\nprint(len(files))","metadata":{"execution":{"iopub.status.busy":"2022-07-30T08:11:08.47267Z","iopub.execute_input":"2022-07-30T08:11:08.473054Z","iopub.status.idle":"2022-07-30T08:11:08.482228Z","shell.execute_reply.started":"2022-07-30T08:11:08.473021Z","shell.execute_reply":"2022-07-30T08:11:08.480656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check one DICOM image\nsample = df_annot.sample(1).iloc[0]\nprint(sample)\ndicom_path = os.path.join(dir_train, sample[\"StudyInstanceUID\"], f'{sample[\"slice_number\"]}.dcm')\nds = pydicom.dcmread(dicom_path)\nplt.imshow(ds.pixel_array, cmap='gray')\nplt.show()\n\nprint(ds)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T08:11:08.483945Z","iopub.execute_input":"2022-07-30T08:11:08.484484Z","iopub.status.idle":"2022-07-30T08:11:08.734555Z","shell.execute_reply.started":"2022-07-30T08:11:08.48443Z","shell.execute_reply":"2022-07-30T08:11:08.733688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#check all DICOM images for one patient\nimages = []\n\ndir_patient = os.path.join(dir_train, sample[\"StudyInstanceUID\"])\npatient_files = os.listdir(dir_patient)\n#print(patient_files)\n\nfor i in tqdm(range(len(patient_files))):\n    tmp_path = os.path.join(dir_patient, patient_files[i])\n    images.append(pydicom.dcmread(tmp_path))\n\nprint(\"file count: {}\".format(len(images)))\n\n# ensure they are in the correct order\nslices = sorted(images, key=lambda s: s.InstanceNumber)\n\n# for i in range(10):\n#     plt.subplot(2, 5, i+1)\n#     plt.imshow(slices[i].pixel_array,cmap='gray')\n    \n# pixel aspects, assuming all slices are the same\nps = slices[0].PixelSpacing\nss = slices[0].SliceThickness\nax_aspect = ps[1]/ps[0]\nsag_aspect = ps[1]/ss\ncor_aspect = ss/ps[0]\n\n# create 3D array\nimg_shape = list(slices[0].pixel_array.shape)\nimg_shape.append(len(slices))\nimg3d = np.zeros(img_shape)\n\n# fill 3D array with the images from the files\nfor i, s in enumerate(slices):\n    img2d = s.pixel_array\n    img3d[:, :, i] = img2d\n\nprint(img_shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T08:11:08.736683Z","iopub.execute_input":"2022-07-30T08:11:08.737758Z","iopub.status.idle":"2022-07-30T08:11:17.081639Z","shell.execute_reply.started":"2022-07-30T08:11:08.737719Z","shell.execute_reply":"2022-07-30T08:11:17.080423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ipywidgets import interact, IntSlider\n\ndef show_3d(x, y, z):\n    a1 = plt.subplot(2, 2, 1)\n    plt.imshow(img3d[:, :, z], cmap='gray')\n    a1.set_aspect(ax_aspect)\n    \n    a2 = plt.subplot(2, 2, 2)\n    plt.imshow(img3d[:, y, :], cmap='gray')\n    a2.set_aspect(sag_aspect)\n    \n    a3 = plt.subplot(2, 2, 3)\n    plt.imshow(img3d[x, :, :].T, cmap='gray')\n    a3.set_aspect(cor_aspect)\n    plt.show()\n    \ninteract(show_3d, \n         x =IntSlider(min=0, max=img_shape[0], step=5, value=int(img_shape[0] / 2)),\n         y =IntSlider(min=0, max=img_shape[1], step=5, value=int(img_shape[1] / 2)),\n         z =IntSlider(min=0, max=img_shape[2], step=5, value=int(img_shape[2] / 2))\n        )","metadata":{"execution":{"iopub.status.busy":"2022-07-30T08:11:17.083396Z","iopub.execute_input":"2022-07-30T08:11:17.083778Z","iopub.status.idle":"2022-07-30T08:11:17.893113Z","shell.execute_reply.started":"2022-07-30T08:11:17.083744Z","shell.execute_reply":"2022-07-30T08:11:17.891912Z"},"trusted":true},"execution_count":null,"outputs":[]}]}