{"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":"# 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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor 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-08-13T08:35:39.661149Z","iopub.execute_input":"2022-08-13T08:35:39.661657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q ../input/for-pydicom/pylibjpeg-1.4.0-py3-none-any.whl\n!pip install -q ../input/for-pydicom/python_gdcm-3.0.14-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl\n!pip install -q ../input/for-pydicom/pylibjpeg_libjpeg-1.3.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:56:28.022237Z","iopub.execute_input":"2022-08-13T08:56:28.022734Z","iopub.status.idle":"2022-08-13T08:57:04.307853Z","shell.execute_reply.started":"2022-08-13T08:56:28.022696Z","shell.execute_reply":"2022-08-13T08:57:04.306771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport itertools\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O \nimport matplotlib.pyplot as plt\n\nimport pydicom\nimport nibabel as nib\nfrom glob import glob\nfrom tqdm import tqdm\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:57:45.44327Z","iopub.execute_input":"2022-08-13T08:57:45.444049Z","iopub.status.idle":"2022-08-13T08:57:46.188501Z","shell.execute_reply.started":"2022-08-13T08:57:45.443985Z","shell.execute_reply":"2022-08-13T08:57:46.187064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_dicom(path, voi_lut = True, fix_monochrome = True):\n    dicom = pydicom.read_file(path)\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    return data, dicom\ndef read_nibabel(path):\n    return nib.load(path).get_fdata()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:58:24.842978Z","iopub.execute_input":"2022-08-13T08:58:24.845556Z","iopub.status.idle":"2022-08-13T08:58:24.852867Z","shell.execute_reply.started":"2022-08-13T08:58:24.845505Z","shell.execute_reply":"2022-08-13T08:58:24.852033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def multi_dim_plot(multi_dim_array, num_slices=64):\n    fig = plt.figure(figsize=(30, 30))\n    plt.title(f'Plotting first {num_slices} slices out of {multi_dim_array.shape}')\n    plt.yticks([])\n    plt.xticks([])\n    \n    for i in range(num_slices):\n        ax = fig.add_subplot(8, 8, i + 1)\n        plt.imshow(multi_dim_array[..., :num_slices][..., i])\n        plt.axis(\"off\")\n    plt.show()\n    \n    \ndef batch_plot(samples, cmap=None):\n    plt.figure(figsize=(30, 30))\n    \n    for i, sample in enumerate(samples):\n        ax = plt.subplot(8, 8, i + 1)\n        plt.axis('off')\n        plt.imshow(sample, cmap=cmap)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:58:33.329759Z","iopub.execute_input":"2022-08-13T08:58:33.330631Z","iopub.status.idle":"2022-08-13T08:58:33.339287Z","shell.execute_reply.started":"2022-08-13T08:58:33.330591Z","shell.execute_reply":"2022-08-13T08:58:33.338001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segmentation_masks = sorted(\n    glob(\n        '../input/rsna-2022-cervical-spine-fracture-detection/segmentations/*.nii'\n    )\n)\n\nlen(segmentation_masks)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:58:46.669165Z","iopub.execute_input":"2022-08-13T08:58:46.669591Z","iopub.status.idle":"2022-08-13T08:58:46.681082Z","shell.execute_reply.started":"2022-08-13T08:58:46.669557Z","shell.execute_reply":"2022-08-13T08:58:46.680216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, msk in enumerate(segmentation_masks[4:6]):\n    m = read_nibabel(msk)\n    multi_dim_plot(m)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:58:57.300037Z","iopub.execute_input":"2022-08-13T08:58:57.300591Z","iopub.status.idle":"2022-08-13T08:59:09.977786Z","shell.execute_reply.started":"2022-08-13T08:58:57.300554Z","shell.execute_reply":"2022-08-13T08:59:09.97649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train.csv')\ntrain_df.head(15)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:59:09.979496Z","iopub.execute_input":"2022-08-13T08:59:09.979862Z","iopub.status.idle":"2022-08-13T08:59:10.015168Z","shell.execute_reply.started":"2022-08-13T08:59:09.979828Z","shell.execute_reply":"2022-08-13T08:59:10.013447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:59:15.85333Z","iopub.execute_input":"2022-08-13T08:59:15.853777Z","iopub.status.idle":"2022-08-13T08:59:15.883041Z","shell.execute_reply.started":"2022-08-13T08:59:15.85374Z","shell.execute_reply":"2022-08-13T08:59:15.881657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/test.csv')\ntest_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:00:19.066144Z","iopub.execute_input":"2022-08-13T09:00:19.066632Z","iopub.status.idle":"2022-08-13T09:00:19.081758Z","shell.execute_reply.started":"2022-08-13T09:00:19.06659Z","shell.execute_reply":"2022-08-13T09:00:19.080567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom_files = glob(\n    \"../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10062/*.dcm\"\n)\n\nlen(dicom_files)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:00:23.125796Z","iopub.execute_input":"2022-08-13T09:00:23.12628Z","iopub.status.idle":"2022-08-13T09:00:23.135067Z","shell.execute_reply.started":"2022-08-13T09:00:23.126237Z","shell.execute_reply":"2022-08-13T09:00:23.134156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_dicom = dicom_files[0]\ntemp_read = read_dicom(sample_dicom)[1]\ntemp_read","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:00:30.31705Z","iopub.execute_input":"2022-08-13T09:00:30.318273Z","iopub.status.idle":"2022-08-13T09:00:30.347907Z","shell.execute_reply.started":"2022-08-13T09:00:30.318214Z","shell.execute_reply":"2022-08-13T09:00:30.346227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = [read_dicom(i)[0][None, ...] for i in dicom_files[:64]]\ntemp = np.concatenate(temp, axis=0)\ntemp.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:00:39.70536Z","iopub.execute_input":"2022-08-13T09:00:39.706495Z","iopub.status.idle":"2022-08-13T09:00:40.779098Z","shell.execute_reply.started":"2022-08-13T09:00:39.70643Z","shell.execute_reply":"2022-08-13T09:00:40.778272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_plot(temp, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:00:46.558741Z","iopub.execute_input":"2022-08-13T09:00:46.559459Z","iopub.status.idle":"2022-08-13T09:00:52.004226Z","shell.execute_reply.started":"2022-08-13T09:00:46.559417Z","shell.execute_reply":"2022-08-13T09:00:52.003274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = [read_dicom(i)[0][None, ...] for i in dicom_files[-64:]]\ntemp = np.concatenate(temp, axis=0)\ntemp.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:00:55.274713Z","iopub.execute_input":"2022-08-13T09:00:55.275296Z","iopub.status.idle":"2022-08-13T09:00:56.416573Z","shell.execute_reply.started":"2022-08-13T09:00:55.275245Z","shell.execute_reply":"2022-08-13T09:00:56.415252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_plot(temp, cmap='bone')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:01:02.39274Z","iopub.execute_input":"2022-08-13T09:01:02.393141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_bbox = pd.read_csv(\n    '../input/rsna-2022-cervical-spine-fracture-detection/train_bounding_boxes.csv'\n)\ndf_bbox.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_bbox.info()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df_bbox.StudyInstanceUID), df_bbox.StudyInstanceUID.nunique()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_bbox.iloc[0]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = '../input/rsna-2022-cervical-spine-fracture-detection/train_images/'\nlen(os.listdir(train_images)) - df_bbox.StudyInstanceUID.nunique()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_512 = []\ntemp_768 = []\n\n#[train/test]_images/[StudyInstanceUID]/[slice_number].dcm\nfor index, row in tqdm(df_bbox.iterrows(), total=len(df_bbox)):\n    with_bbox_dicom_path = os.path.join(\n            train_images, str(row['StudyInstanceUID']), str(row['slice_number']) + '.dcm'\n        )\n    with_bbox_read_dicom, _ = read_dicom(with_bbox_dicom_path)\n    \n    xmin = int(row['x'])\n    ymin = int(row['y'])\n    xmax = xmin + int(row['width'])\n    ymax = ymin + int(row['height'])\n    cv2.rectangle(with_bbox_read_dicom, (xmin, ymin), (xmax, ymax), (0,255,255), 3)\n    \n    if with_bbox_read_dicom.shape[0] == 512:\n        temp_512.append(with_bbox_read_dicom[None, ...])\n    else:\n        temp_768.append(with_bbox_read_dicom[None, ...])\n    \ntemp_512 = np.concatenate(temp_512, axis=0)\ntemp_768 = np.concatenate(temp_768, axis=0)\ntemp_512.shape, temp_768.shape","metadata":{"execution":{"iopub.status.idle":"2022-08-13T09:04:40.806364Z","shell.execute_reply.started":"2022-08-13T09:01:58.762263Z","shell.execute_reply":"2022-08-13T09:04:40.80509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_plot(temp_512[:64], cmap='jet')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:04:40.808388Z","iopub.execute_input":"2022-08-13T09:04:40.808843Z","iopub.status.idle":"2022-08-13T09:04:46.574448Z","shell.execute_reply.started":"2022-08-13T09:04:40.808809Z","shell.execute_reply":"2022-08-13T09:04:46.572606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_plot(temp_512[-64:], cmap='inferno')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:04:46.576381Z","iopub.execute_input":"2022-08-13T09:04:46.576828Z","iopub.status.idle":"2022-08-13T09:04:52.250245Z","shell.execute_reply.started":"2022-08-13T09:04:46.576788Z","shell.execute_reply":"2022-08-13T09:04:52.248358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_plot(temp_512[64:128], cmap='bone')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:04:52.25214Z","iopub.execute_input":"2022-08-13T09:04:52.252909Z","iopub.status.idle":"2022-08-13T09:04:58.238995Z","shell.execute_reply.started":"2022-08-13T09:04:52.252874Z","shell.execute_reply":"2022-08-13T09:04:58.237701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_plot(\n    np.apply_along_axis(\n        cv2.createCLAHE(clipLimit=4.0, tileGridSize=(8,8)).apply, 2, temp_512[128:192] # pick 64 slices\n    ), \n    cmap='gray'\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:04:58.240769Z","iopub.execute_input":"2022-08-13T09:04:58.241484Z","iopub.status.idle":"2022-08-13T09:05:06.168727Z","shell.execute_reply.started":"2022-08-13T09:04:58.24144Z","shell.execute_reply":"2022-08-13T09:05:06.167546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_plot(temp_768, cmap='bone')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:05:06.170271Z","iopub.execute_input":"2022-08-13T09:05:06.170667Z","iopub.status.idle":"2022-08-13T09:05:07.828501Z","shell.execute_reply.started":"2022-08-13T09:05:06.17063Z","shell.execute_reply":"2022-08-13T09:05:07.827273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"means = train_df.mean(numeric_only=True).to_dict()\ntest_df['fractured'] = test_df['prediction_type'].map(means)\ntest_df[['row_id','fractured']].to_csv('submission.csv', index=False, float_format='%.1g')\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:07:30.044998Z","iopub.execute_input":"2022-08-13T09:07:30.046456Z","iopub.status.idle":"2022-08-13T09:07:30.091212Z","shell.execute_reply.started":"2022-08-13T09:07:30.046389Z","shell.execute_reply":"2022-08-13T09:07:30.089917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}