{"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":"<div align='center'><font size=\"5\" color='#353B47'>RSNA 2022 Cervical Spine Fracture Detection</font></div>\n<div align='center'><font size=\"4\" color=\"#353B47\">Identify cervical fractures from scans</font></div>\n<br>\n<hr>","metadata":{}},{"cell_type":"markdown","source":"The goal of this competition is to identify fractures in CT scans of the cervical spine (neck) at both the level of a single vertebrae and the entire patient. Quickly detecting and determining the location of any vertebral fractures is essential to prevent neurologic deterioration and paralysis after trauma\n\nThis competition uses a hidden test. When your submitted notebook is scored the actual test data (including a full length sample submission) will be made available to your notebook","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\n\nfrom matplotlib.patches import Rectangle\nimport numpy as np\nimport pandas as pd\nimport os\nimport re\nimport random\nimport matplotlib.pyplot as plt\nimport plotly\nimport plotly.graph_objects as go\nfrom plotly.subplots import make_subplots\nimport plotly.express as px\nfrom pydicom import dcmread\nfrom tqdm import tqdm\nimport multiprocessing as mp\nimport seaborn as sns\nimport datetime","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-01T21:34:49.309104Z","iopub.execute_input":"2022-08-01T21:34:49.310175Z","iopub.status.idle":"2022-08-01T21:34:51.511354Z","shell.execute_reply.started":"2022-08-01T21:34:49.310131Z","shell.execute_reply":"2022-08-01T21:34:51.509879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Path to dataframe and image folders\nPATH = \"../input/rsna-2022-cervical-spine-fracture-detection\"\n\n# Import trainset\ntrain_dataframe = pd.read_csv(os.path.join(PATH, 'train.csv'))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T21:35:24.133389Z","iopub.execute_input":"2022-08-01T21:35:24.133824Z","iopub.status.idle":"2022-08-01T21:35:24.150885Z","shell.execute_reply.started":"2022-08-01T21:35:24.133786Z","shell.execute_reply":"2022-08-01T21:35:24.149686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataframe","metadata":{"execution":{"iopub.status.busy":"2022-08-01T21:35:24.387911Z","iopub.execute_input":"2022-08-01T21:35:24.38875Z","iopub.status.idle":"2022-08-01T21:35:24.409409Z","shell.execute_reply.started":"2022-08-01T21:35:24.3887Z","shell.execute_reply":"2022-08-01T21:35:24.408007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataframe.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:13:26.919859Z","iopub.execute_input":"2022-08-01T18:13:26.920692Z","iopub.status.idle":"2022-08-01T18:13:26.941435Z","shell.execute_reply.started":"2022-08-01T18:13:26.920638Z","shell.execute_reply":"2022-08-01T18:13:26.93961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataframe.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:13:26.946315Z","iopub.execute_input":"2022-08-01T18:13:26.946777Z","iopub.status.idle":"2022-08-01T18:13:26.964535Z","shell.execute_reply.started":"2022-08-01T18:13:26.94674Z","shell.execute_reply":"2022-08-01T18:13:26.963177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataframe.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:13:26.96692Z","iopub.execute_input":"2022-08-01T18:13:26.967775Z","iopub.status.idle":"2022-08-01T18:13:26.980743Z","shell.execute_reply.started":"2022-08-01T18:13:26.967722Z","shell.execute_reply":"2022-08-01T18:13:26.979301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Scan and fracture counts per patient\ntrain_dataframe['nb_scans'] = train_dataframe['StudyInstanceUID'].apply(lambda x: len(os.listdir(f'../input/rsna-2022-cervical-spine-fracture-detection/train_images/{x}')))\ntrain_dataframe['nb_fractures'] = train_dataframe[['C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7']].sum(axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:13:26.982941Z","iopub.execute_input":"2022-08-01T18:13:26.983472Z","iopub.status.idle":"2022-08-01T18:13:34.196339Z","shell.execute_reply.started":"2022-08-01T18:13:26.983423Z","shell.execute_reply":"2022-08-01T18:13:34.195267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.box(train_dataframe, y=\"nb_scans\", points=\"all\", title='Nb scans per patient', color_discrete_sequence=[\"goldenrod\"])\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-01T18:13:34.197855Z","iopub.execute_input":"2022-08-01T18:13:34.19898Z","iopub.status.idle":"2022-08-01T18:13:34.270623Z","shell.execute_reply.started":"2022-08-01T18:13:34.198939Z","shell.execute_reply":"2022-08-01T18:13:34.26919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict_c = {}\nfor i in range(1,7):\n    dict_c[f'C{i}'] = train_dataframe.loc[train_dataframe[f'C{i}'] == 1].shape[0]\n    \nx = list(dict_c.keys())\ny = list(dict_c.values())\n\nmax_index = np.argmax(list(dict_c.values()))\ncolors = ['lightblue',] * 7\ncolors[max_index] = 'goldenrod'\n\nfig = go.Figure(\n    data=[\n        go.Bar(\n            x=x,\n            y=y,\n            marker_color=colors\n        )\n    ]\n)\n\nfig.update_layout(\n    title_text='Fractured vertebrae location counts',\n)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-01T18:13:34.272561Z","iopub.execute_input":"2022-08-01T18:13:34.273069Z","iopub.status.idle":"2022-08-01T18:13:34.302391Z","shell.execute_reply.started":"2022-08-01T18:13:34.273017Z","shell.execute_reply":"2022-08-01T18:13:34.301138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colors = ['lightblue',] * 6\ncolors[0] = 'goldenrod'\n\ndict_nb_fractures = dict(train_dataframe.loc[train_dataframe['nb_fractures'] != 0, 'nb_fractures'].value_counts())\n\nx = list(dict_nb_fractures.keys())\ny = list(dict_nb_fractures.values())\n\nfig = go.Figure(\n    data=[\n        go.Bar(\n            x=x,\n            y=y,\n            marker_color=colors\n        )\n    ]\n)\n\nfig.update_layout(\n    title_text='Fracture counts distribution',\n)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-01T18:13:34.304Z","iopub.execute_input":"2022-08-01T18:13:34.304413Z","iopub.status.idle":"2022-08-01T18:13:34.329897Z","shell.execute_reply.started":"2022-08-01T18:13:34.304376Z","shell.execute_reply":"2022-08-01T18:13:34.328616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Scan counts per nb fractures\ny0 = train_dataframe.loc[train_dataframe[\"nb_fractures\"]==1, 'nb_scans']\ny1 = train_dataframe.loc[train_dataframe[\"nb_fractures\"]==2, 'nb_scans']\ny2 = train_dataframe.loc[train_dataframe[\"nb_fractures\"]==3, 'nb_scans']\ny3 = train_dataframe.loc[train_dataframe[\"nb_fractures\"]==4, 'nb_scans']\ny4 = train_dataframe.loc[train_dataframe[\"nb_fractures\"]==5, 'nb_scans']\ny5 = train_dataframe.loc[train_dataframe[\"nb_fractures\"]==6, 'nb_scans']\n\nfig = go.Figure()\nfig.add_trace(go.Box(y=y0, name=\"1\", marker_color='lightblue'))\nfig.add_trace(go.Box(y=y1, name=\"2\", marker_color='lightblue'))\nfig.add_trace(go.Box(y=y2, name=\"3\", marker_color='goldenrod'))\nfig.add_trace(go.Box(y=y3, name=\"4\", marker_color='lightblue'))\nfig.add_trace(go.Box(y=y2, name=\"5\", marker_color='goldenrod'))\nfig.add_trace(go.Box(y=y3, name=\"6\", marker_color='lightblue'))\n\nfig.update_layout(\n    title_text='Scan counts per nb fractures',\n    showlegend=False\n)\nfig.update_xaxes(title = 'At least')\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-01T18:13:34.331388Z","iopub.execute_input":"2022-08-01T18:13:34.332238Z","iopub.status.idle":"2022-08-01T18:13:34.369513Z","shell.execute_reply.started":"2022-08-01T18:13:34.332173Z","shell.execute_reply":"2022-08-01T18:13:34.368294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Slightly higher median number of scans for those who have 3 or 5 fractures","metadata":{}},{"cell_type":"markdown","source":"# Read DICOM files","metadata":{}},{"cell_type":"markdown","source":"### What is a DICOM file?\n\nA DICOM file is an image saved in the Digital Imaging and Communications in Medicine (DICOM) format. It contains an image from a medical scan, such as an ultrasound or MRI. DICOM files may also include identification data for patients to link the image to a specific individual.","metadata":{}},{"cell_type":"code","source":"ds = dcmread(os.path.join(PATH, 'train_images', '1.2.826.0.1.3680043.10001/1.dcm'))\nds","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:13:34.371142Z","iopub.execute_input":"2022-08-01T18:13:34.372387Z","iopub.status.idle":"2022-08-01T18:13:34.387765Z","shell.execute_reply.started":"2022-08-01T18:13:34.372337Z","shell.execute_reply":"2022-08-01T18:13:34.386828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Extracting metadata from DICOM files","metadata":{}},{"cell_type":"code","source":"def extract_file(file_path):\n    '''\n    :param file_path: path of DICOM file to extract metadata from\n    :return: a dictionary of extracted metadata\n    '''\n    \n    ds = dcmread(file_path)\n    image_id = file_path.split(sep=\"/\")[-2]\n\n    observation_dict = {}\n    observation_dict['image_id'] = image_id\n    \n    file_meta_keys = list(ds.file_meta._dict.keys())\n    remaining_meta_keys = list(ds._dict.keys())\n    \n    for key in file_meta_keys:\n        observation_dict[str(key)] = str(ds.file_meta[key].value)\n        \n    # Not taking into account pixel value\n    for key in remaining_meta_keys:\n        if key != (0x7fe0, 0x0010):\n            observation_dict[str(key)] = str(ds[key].value)\n        \n    return observation_dict","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:13:34.389204Z","iopub.execute_input":"2022-08-01T18:13:34.389796Z","iopub.status.idle":"2022-08-01T18:13:34.398693Z","shell.execute_reply.started":"2022-08-01T18:13:34.389761Z","shell.execute_reply":"2022-08-01T18:13:34.397478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# An example\nextract_file('../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001/1.dcm')","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:13:34.402822Z","iopub.execute_input":"2022-08-01T18:13:34.403471Z","iopub.status.idle":"2022-08-01T18:13:34.422085Z","shell.execute_reply.started":"2022-08-01T18:13:34.403432Z","shell.execute_reply":"2022-08-01T18:13:34.421052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mapper_dict = {\n    'image_id':'image_id',\n               \n    '(0002, 0001)':\"File Meta Information Version\",\n    '(0002, 0002)':\"Media Storage SOP Class UID\",\n    '(0002, 0003)':\"Media Storage SOP Instance UID\",\n    '(0002, 0010)':\"Transfer Syntax UID\",\n    '(0002, 0012)':\"Implementation Class UID\",\n    '(0002, 0013)':\"Implementation Version Name\",\n\n    '(0008, 0018)':\"SOPInstanceUID\",\n    '(0008, 0023)':\"Date of Creation\",\n    '(0008, 0033)':\"Time of Creation\",\n\n    '(0010, 0010)':\"Patient Name\",\n    '(0010, 0020)':\"Patient ID\",\n    \n    '(0018, 0050)':\"Slice Thickness\",\n    \n    '(0020, 000d)':\"Study Instance UID\",\n    '(0020, 000e)':\"Series Instance UID\",\n    '(0020, 0013)':\"Instance Number\",\n    '(0020, 0032)':\"Image Position (Patient)\",\n    '(0020, 0037)':\"Image Orientation (Patient)\",\n    \n    '(0028, 0002)':\"Samples per Pixel\",\n    '(0028, 0004)':\"Photometric Interpretation\",\n    '(0028, 0010)':\"Rows\",\n    '(0028, 0011)':\"Columns\",\n    '(0028, 0030)':\"Pixel Spacing\",\n    '(0028, 0100)':\"Bits Allocated\",\n    '(0028, 0101)':\"Bits Stored\",\n    '(0028, 0102)':\"High Bit\",\n    '(0028, 0103)':\"Pixel Representation\",\n    '(0028, 1050)':\"Window Center\",\n    '(0028, 1051)':\"Window Width\",\n    '(0028, 1052)':\"Rescale Intercept\",\n    '(0028, 1053)':\"Rescale Slope\",\n}","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:13:34.42385Z","iopub.execute_input":"2022-08-01T18:13:34.424934Z","iopub.status.idle":"2022-08-01T18:13:34.434729Z","shell.execute_reply.started":"2022-08-01T18:13:34.424885Z","shell.execute_reply":"2022-08-01T18:13:34.433452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def meta_information_one_folder(folder):\n    '''\n    :param folder: path of folder containing all DICOM files to extract metadata from\n    :return: a dataframe with extracted metadata, one row matches one scan\n    '''\n    \n    folder_filenames = os.listdir(os.path.join(PATH, folder))\n    one_obs = extract_file(os.path.join(PATH, folder, folder_filenames[0]))\n    metadata = pd.DataFrame(columns = one_obs.keys())\n    \n    print(f'Extracting metadata from folder {folder}')\n    for filename in tqdm(folder_filenames):\n        one_obs = extract_file(os.path.join(PATH, folder, filename))\n        metadata = metadata.append(one_obs, ignore_index=True)\n        \n    metadata.columns = metadata.columns.map(mapper_dict)\n    metadata.to_csv(f\"dicom_metadata.csv\", index=False)\n    \n    return metadata","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:13:34.436932Z","iopub.execute_input":"2022-08-01T18:13:34.437701Z","iopub.status.idle":"2022-08-01T18:13:34.453953Z","shell.execute_reply.started":"2022-08-01T18:13:34.43766Z","shell.execute_reply":"2022-08-01T18:13:34.452482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata = meta_information_one_folder('train_images/1.2.826.0.1.3680043.10001')","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:13:34.455733Z","iopub.execute_input":"2022-08-01T18:13:34.456417Z","iopub.status.idle":"2022-08-01T18:13:37.321973Z","shell.execute_reply.started":"2022-08-01T18:13:34.456371Z","shell.execute_reply":"2022-08-01T18:13:37.320563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for column in metadata.columns:\n    if len(set(metadata[column])) != 1:\n        print(column)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:13:37.323771Z","iopub.execute_input":"2022-08-01T18:13:37.324126Z","iopub.status.idle":"2022-08-01T18:13:37.335404Z","shell.execute_reply.started":"2022-08-01T18:13:37.324094Z","shell.execute_reply":"2022-08-01T18:13:37.334059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Among all scans, only these columns have several values for a given patient","metadata":{}},{"cell_type":"markdown","source":"# Preprocessing metadata","metadata":{}},{"cell_type":"code","source":"metadata['Date of Creation'] = metadata['Time of Creation'].apply(lambda x: datetime.datetime.fromtimestamp(eval(x)).strftime('%Y-%m-%d %H:%M:%S'))\nmetadata[['Media Storage SOP Instance UID', 'SOPInstanceUID', 'Time of Creation', 'Date of Creation', 'Instance Number', 'Image Position (Patient)']].head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:13:37.33752Z","iopub.execute_input":"2022-08-01T18:13:37.338013Z","iopub.status.idle":"2022-08-01T18:13:37.369406Z","shell.execute_reply.started":"2022-08-01T18:13:37.337968Z","shell.execute_reply":"2022-08-01T18:13:37.367115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Some observations","metadata":{}},{"cell_type":"code","source":"def get_random_files_from_patient(path):\n    '''\n    :param path: path of folder containing more than 9 DICOM files\n    :return: a list of 9 DICOM filenames\n    '''\n    \n    return random.sample(os.listdir(path), 9)\n\n\ndef rescale_image(dicom_file):\n    '''\n    :param dicom_file: DICOM file\n    :return: image and rescaled image\n    '''\n    \n    image = dicom_file.pixel_array.flatten()\n    rescaled_image = image * dicom_file.RescaleSlope + dicom_file.RescaleIntercept\n    \n    return image, rescaled_image\n\n\ndef display_images(files_list, graph_indexes = np.arange(9)):\n    '''\n    :param files_list: list of DICOM files (output from get_random_files_from_patient)\n    :param graph_indexes: indexes of the files to display\n    :return: plots images, pixel distributions and rescaled pixel distributions\n    '''\n    \n    # define subplot\n    fig, axs = plt.subplots(3,3, figsize=(20,12))\n    for idx, file in enumerate(files_list):\n        \n        full_path = os.path.join('../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001', file)\n        ds = dcmread(full_path)\n\n        axs[idx//3, 0].imshow(ds.pixel_array, cmap=plt.get_cmap('gray'))   \n        axs[idx//3, 0].axis(\"off\")\n        \n        image, rescaled_image = rescale_image(ds)\n        \n        sns.distplot(image.flatten(), ax=axs[idx//3, 1]);\n        sns.distplot(rescaled_image.flatten(), ax=axs[idx//3, 2])\n        axs[idx//3, 1].set_title(\"Raw pixel array distributions\")\n        axs[idx//3, 2].set_title(\"HU unit distributions\");    \n        \n        \n    # the bottom of the subplots of the figure\n    plt.subplots_adjust(bottom = 0.001)\n    plt.subplots_adjust(top = 0.99)\n    \n    # show the figure\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:13:37.371401Z","iopub.execute_input":"2022-08-01T18:13:37.372617Z","iopub.status.idle":"2022-08-01T18:13:37.392795Z","shell.execute_reply.started":"2022-08-01T18:13:37.372563Z","shell.execute_reply":"2022-08-01T18:13:37.39124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files_to_display = get_random_files_from_patient('../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001')\ndisplay_images(files_to_display)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:13:37.394422Z","iopub.execute_input":"2022-08-01T18:13:37.395638Z","iopub.status.idle":"2022-08-01T18:13:58.577443Z","shell.execute_reply.started":"2022-08-01T18:13:37.395575Z","shell.execute_reply":"2022-08-01T18:13:58.576008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Bounding Boxes","metadata":{}},{"cell_type":"code","source":"bb_train = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train_bounding_boxes.csv')\nbb_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T21:34:53.914183Z","iopub.execute_input":"2022-08-01T21:34:53.915411Z","iopub.status.idle":"2022-08-01T21:34:53.977813Z","shell.execute_reply.started":"2022-08-01T21:34:53.915362Z","shell.execute_reply":"2022-08-01T21:34:53.976971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bb_train.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bb_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T18:18:47.846358Z","iopub.execute_input":"2022-08-01T18:18:47.846795Z","iopub.status.idle":"2022-08-01T18:18:47.860562Z","shell.execute_reply.started":"2022-08-01T18:18:47.84676Z","shell.execute_reply":"2022-08-01T18:18:47.859297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Counting total number of files for annotated patients\n# siuid_list = train_dataframe.loc[train_dataframe['patient_overall'] == 1, 'StudyInstanceUID'].values\n# cnt = 0\n# for siuid in tqdm(siuid_list):\n#     cnt+=len(os.listdir(f'../input/rsna-2022-cervical-spine-fracture-detection/train_images/{siuid}'))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T21:46:20.180141Z","iopub.execute_input":"2022-08-01T21:46:20.18144Z","iopub.status.idle":"2022-08-01T22:24:55.373993Z","shell.execute_reply.started":"2022-08-01T21:46:20.181396Z","shell.execute_reply":"2022-08-01T22:24:55.37274Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(cnt)\n# 320919","metadata":{"execution":{"iopub.status.busy":"2022-08-01T22:24:55.376523Z","iopub.execute_input":"2022-08-01T22:24:55.377313Z","iopub.status.idle":"2022-08-01T22:24:55.382965Z","shell.execute_reply.started":"2022-08-01T22:24:55.377264Z","shell.execute_reply":"2022-08-01T22:24:55.381932Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# getting proportion of annotated files\n7217/320919","metadata":{"execution":{"iopub.status.busy":"2022-08-01T22:24:55.384558Z","iopub.execute_input":"2022-08-01T22:24:55.385823Z","iopub.status.idle":"2022-08-01T22:24:55.398893Z","shell.execute_reply.started":"2022-08-01T22:24:55.385772Z","shell.execute_reply":"2022-08-01T22:24:55.397511Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import glob\n# missing_annot = []\n# spines = {f\"C{i}\": [] for i in range(1, 8)}\n\n# for _, row in tqdm(train_dataframe.iterrows()):\n#     study_id = row[\"StudyInstanceUID\"]\n#     ls_imgs = glob.glob(os.path.join('../input/rsna-2022-cervical-spine-fracture-detection/train_images', study_id, \"*.dcm\"))\n#     annot_slices = bb_train[bb_train[\"StudyInstanceUID\"] == study_id][\"slice_number\"].values.tolist()\n#     if not annot_slices:\n#         if row[\"patient_overall\"] == 1:\n#             missing_annot.append(study_id)\n#         continue\n        \n#     nb = float(len(ls_imgs))\n#     idx = [i / nb for i in annot_slices]\n#     for c in spines:\n#         if row[c] == 1:\n#             spines[c] += idx","metadata":{"execution":{"iopub.status.busy":"2022-08-01T21:39:38.466042Z","iopub.execute_input":"2022-08-01T21:39:38.466474Z","iopub.status.idle":"2022-08-01T21:40:58.711982Z","shell.execute_reply.started":"2022-08-01T21:39:38.466435Z","shell.execute_reply":"2022-08-01T21:40:58.710211Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Segmentations","metadata":{}},{"cell_type":"code","source":"# import nibabel as nib\n# img = nib.load('../input/rsna-2022-cervical-spine-fracture-detection/segmentations/1.2.826.0.1.3680043.10633.nii')\n# data = img.get_data()\n# plt.imshow(data, cmap=plt.get_cmap('gray'))   ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T19:15:07.981121Z","iopub.execute_input":"2022-08-01T19:15:07.98155Z","iopub.status.idle":"2022-08-01T19:15:09.950077Z","shell.execute_reply.started":"2022-08-01T19:15:07.981517Z","shell.execute_reply":"2022-08-01T19:15:09.948603Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## WIP...","metadata":{}},{"cell_type":"markdown","source":"# References\n\n#### DICOM files\n* <a href=\"https://pydicom.github.io/pydicom/stable/auto_examples/input_output/plot_read_dicom.html\">Deal with .dicom files</a>\n* <a href=\"https://dicom.innolitics.com/ciods/rt-plan/patient-study/00101020\">Matching DICOM metadata</a>\n\n#### Notebook\n* <a href=\"https://www.kaggle.com/code/allunia/rsna-csf-cervical-spine-fracture-eda\">Rescaling DICOM files</a>","metadata":{}},{"cell_type":"markdown","source":"<hr>\n<div align='justify'><font color=\"#353B47\" size=\"4\">Thank you for taking the time to read this notebook. I hope that I was able to answer your questions or your curiosity and that it was quite understandable. <u>any constructive comments are welcome</u>. They help me progress and motivate me to share better quality content. I am above all a passionate person who tries to advance my knowledge but also that of others. If you liked it, feel free to <u>upvote and share my work.</u> </font></div>\n<br>\n<div align='center'><font color=\"#353B47\" size=\"3\">Thank you and may passion guide you.</font></div>","metadata":{}}]}