{"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","_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-28T10:44:27.250895Z","iopub.execute_input":"2022-09-28T10:44:27.251295Z"},"_kg_hide-input":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os \nimport pathlib\nimport glob \nfrom tqdm import tqdm \n\nimport pandas as pd\nimport numpy as np\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport pydicom","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:42:43.131375Z","iopub.execute_input":"2022-09-29T05:42:43.131779Z","iopub.status.idle":"2022-09-29T05:42:48.384652Z","shell.execute_reply.started":"2022-09-29T05:42:43.131745Z","shell.execute_reply":"2022-09-29T05:42:48.383509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = \"../input/rsna-2022-cervical-spine-fracture-detection/\"","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:42:48.386652Z","iopub.execute_input":"2022-09-29T05:42:48.387339Z","iopub.status.idle":"2022-09-29T05:42:48.392487Z","shell.execute_reply.started":"2022-09-29T05:42:48.387275Z","shell.execute_reply":"2022-09-29T05:42:48.391214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(DATA_DIR)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:42:48.393856Z","iopub.execute_input":"2022-09-29T05:42:48.394257Z","iopub.status.idle":"2022-09-29T05:42:48.411315Z","shell.execute_reply.started":"2022-09-29T05:42:48.394227Z","shell.execute_reply":"2022-09-29T05:42:48.410442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(DATA_DIR + \"train.csv\")\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:42:48.413421Z","iopub.execute_input":"2022-09-29T05:42:48.413758Z","iopub.status.idle":"2022-09-29T05:42:48.434877Z","shell.execute_reply.started":"2022-09-29T05:42:48.413729Z","shell.execute_reply":"2022-09-29T05:42:48.433752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.StudyInstanceUID","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:42:50.705191Z","iopub.execute_input":"2022-09-29T05:42:50.705823Z","iopub.status.idle":"2022-09-29T05:42:50.713736Z","shell.execute_reply.started":"2022-09-29T05:42:50.705784Z","shell.execute_reply":"2022-09-29T05:42:50.712871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = os.listdir(DATA_DIR + \"train_images\")[301:401]\nprint(Y[0])\n#print(\"Hi\")","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:10:27.937117Z","iopub.execute_input":"2022-09-29T07:10:27.93808Z","iopub.status.idle":"2022-09-29T07:10:27.947432Z","shell.execute_reply.started":"2022-09-29T07:10:27.938043Z","shell.execute_reply":"2022-09-29T07:10:27.946273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(DATA_DIR + \"train_images\")[301:401])","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:10:15.272523Z","iopub.execute_input":"2022-09-29T07:10:15.272971Z","iopub.status.idle":"2022-09-29T07:10:15.281264Z","shell.execute_reply.started":"2022-09-29T07:10:15.27294Z","shell.execute_reply":"2022-09-29T07:10:15.280368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(DATA_DIR + \"train_images/1.2.826.0.1.3680043.6200\"))","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:14:06.688369Z","iopub.execute_input":"2022-09-29T07:14:06.68931Z","iopub.status.idle":"2022-09-29T07:14:06.736092Z","shell.execute_reply.started":"2022-09-29T07:14:06.689256Z","shell.execute_reply":"2022-09-29T07:14:06.734769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\nwith open(\"../input/rsna-2022-cervical-spine-fracture-detection/train.csv\",newline='') as f:\n    ereader = csv.DictReader(f)\n    print(\"Fractured Only\")\n    print(\"                         P C1 C2 C3 C4 C5 C6 C7\")\n    print(\"===============\")\n    for row in ereader:\n            #n=100\n            Y = row['StudyInstanceUID']\n            while(Y!=B):\n                X = row['patient_overall']\n                if(X!='0'):    #1-fractured, 0-Nonfractured\n                    print(row['StudyInstanceUID'],row['patient_overall'],row['C1'],row['C2'],row['C3'],row['C4'],row['C5'],row['C6'],row['C7'])\n                ","metadata":{"execution":{"iopub.status.busy":"2022-09-29T06:35:42.143554Z","iopub.status.idle":"2022-09-29T06:35:42.143953Z","shell.execute_reply.started":"2022-09-29T06:35:42.143772Z","shell.execute_reply":"2022-09-29T06:35:42.14379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len('print(row['StudyInstanceUID'],row['patient_overall'],row['C1'],row['C2'],row['C3'],row['C4'],row['C5'],row['C6'],row['C7'])')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\nwith open(\"../input/rsna-2022-cervical-spine-fracture-detection/train.csv\",newline='') as f:\n    ereader = csv.DictReader(f)\n    print(\"NON-Fractured Only\")\n    print(\"                          PC1C2C3C4C5C6C7\")\n    print(\"===============\")\n    for row in ereader:\n            Y = row['StudyInstanceUID']\n            if Y=='1.2.826.0.1.3680043.27079':\n                X = row['patient_overall']\n                if(X!='1'):\n                    print(row['StudyInstanceUID'],row['patient_overall'],row['C1'],row['C2'],row['C3'],row['C4'],row['C5'],row['C6'],row['C7'])","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:28:08.771907Z","iopub.execute_input":"2022-09-29T07:28:08.773052Z","iopub.status.idle":"2022-09-29T07:28:08.793506Z","shell.execute_reply.started":"2022-09-29T07:28:08.773013Z","shell.execute_reply":"2022-09-29T07:28:08.792583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_instance = \"1.2.826.0.1.3680043.1016\"\n# list the first 5 frames in a select study instance\nos.listdir(DATA_DIR + f\"train_images/{study_instance}\")[:5]","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:50:03.387427Z","iopub.execute_input":"2022-09-28T07:50:03.387935Z","iopub.status.idle":"2022-09-28T07:50:03.398758Z","shell.execute_reply.started":"2022-09-28T07:50:03.387899Z","shell.execute_reply":"2022-09-28T07:50:03.397556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#NEW_DIR = \"../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.1016\"\n#os.listdir(DATA_DIR + f\"train_images/{study_instance}\")[:5]\nfrom glob import glob\nimport matplotlib.pylab as plt\n\nimport pydicom as dicom","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:21:00.724189Z","iopub.execute_input":"2022-09-29T07:21:00.72522Z","iopub.status.idle":"2022-09-29T07:21:00.732551Z","shell.execute_reply.started":"2022-09-29T07:21:00.725177Z","shell.execute_reply":"2022-09-29T07:21:00.731057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = glob(\"../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10136/*\")","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:21:06.918969Z","iopub.execute_input":"2022-09-29T07:21:06.919366Z","iopub.status.idle":"2022-09-29T07:21:06.927835Z","shell.execute_reply.started":"2022-09-29T07:21:06.919334Z","shell.execute_reply":"2022-09-29T07:21:06.926644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use('default')\nfig, axes = plt.subplots(4,4, figsize=(12,12))\ntrain_images\nfor i, ax in enumerate(axes.reshape(-1)):\n    img_path = train_images[i]     \n    img = dicom.dcmread(img_path)  \n    ax.imshow(img.pixel_array)     \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:21:16.756112Z","iopub.execute_input":"2022-09-29T07:21:16.756551Z","iopub.status.idle":"2022-09-29T07:21:19.419465Z","shell.execute_reply.started":"2022-09-29T07:21:16.756514Z","shell.execute_reply":"2022-09-29T07:21:19.418078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom as dicom\npath = \"../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.1016/120.dcm\"\nX = dicom.dcmread(path)\nprint(X)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:23:04.286245Z","iopub.execute_input":"2022-09-29T07:23:04.286684Z","iopub.status.idle":"2022-09-29T07:23:04.309517Z","shell.execute_reply.started":"2022-09-29T07:23:04.28665Z","shell.execute_reply":"2022-09-29T07:23:04.308327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:23:07.564698Z","iopub.execute_input":"2022-09-29T07:23:07.565622Z","iopub.status.idle":"2022-09-29T07:23:07.697793Z","shell.execute_reply.started":"2022-09-29T07:23:07.565572Z","shell.execute_reply":"2022-09-29T07:23:07.69675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\npath = os.listdir('../input/rsna-2022-cervical-spine-fracture-detection/train_images/')\nclasses = {'1.2.826.0.1.3680043.1016':0, '1.2.826.0.1.3680043.10136':1}\n","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:23:09.168465Z","iopub.execute_input":"2022-09-29T07:23:09.169225Z","iopub.status.idle":"2022-09-29T07:23:09.175052Z","shell.execute_reply.started":"2022-09-29T07:23:09.169185Z","shell.execute_reply":"2022-09-29T07:23:09.174179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Creating a folder for store png files**","metadata":{}},{"cell_type":"code","source":"dirName = './fractured'\nos.makedirs(dirName)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:23:12.054829Z","iopub.execute_input":"2022-09-29T07:23:12.055277Z","iopub.status.idle":"2022-09-29T07:23:12.060965Z","shell.execute_reply.started":"2022-09-29T07:23:12.055238Z","shell.execute_reply":"2022-09-29T07:23:12.059776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.listdir('./Non-fractured')","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:23:13.437909Z","iopub.execute_input":"2022-09-29T07:23:13.438696Z","iopub.status.idle":"2022-09-29T07:23:13.476984Z","shell.execute_reply.started":"2022-09-29T07:23:13.438658Z","shell.execute_reply":"2022-09-29T07:23:13.475104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\ncv_img = []\nfor img in glob.glob(\"./pop/*.png\"):\n    n= cv2.imread(img)\n    cv_img.append(n)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:23:18.704222Z","iopub.execute_input":"2022-09-29T07:23:18.705017Z","iopub.status.idle":"2022-09-29T07:23:18.710022Z","shell.execute_reply.started":"2022-09-29T07:23:18.704979Z","shell.execute_reply":"2022-09-29T07:23:18.709065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\ndirName = './Non-fractured'\nos.makedirs(dirName)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:23:31.129205Z","iopub.execute_input":"2022-09-29T07:23:31.12964Z","iopub.status.idle":"2022-09-29T07:23:31.134974Z","shell.execute_reply.started":"2022-09-29T07:23:31.129608Z","shell.execute_reply":"2022-09-29T07:23:31.134063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('./cop')[:5]","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:23:36.056765Z","iopub.execute_input":"2022-09-29T07:23:36.057175Z","iopub.status.idle":"2022-09-29T07:23:36.081036Z","shell.execute_reply.started":"2022-09-29T07:23:36.057144Z","shell.execute_reply":"2022-09-29T07:23:36.079594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**DCM to JPEG**","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pydicom\nfrom PIL import Image\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:34:31.256141Z","iopub.execute_input":"2022-09-29T07:34:31.256578Z","iopub.status.idle":"2022-09-29T07:34:31.262105Z","shell.execute_reply.started":"2022-09-29T07:34:31.256544Z","shell.execute_reply":"2022-09-29T07:34:31.260691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_names(path):\n    names = []\n    for root, dirnames, filenames in os.walk(path):\n        for filename in filenames:\n            _, ext = os.path.splitext(filename)\n            if ext in ['.dcm']:\n                names.append(filename)\n    return names    \n#print(get_names('../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.1016'))\n\n\ndef convert_dcm_jpg(name):\n    im = pydicom.dcmread('../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.27079/'+name)\n    im = im.pixel_array.astype(float)\n\n    rescaled_image = (np.maximum(im,0)/im.max())*255\n    final_image = np.uint8(rescaled_image)\n    final_image = Image.fromarray(final_image)\n    \n    return final_image\nnames = get_names('../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.27079/')\nfor name in names:\n    image = convert_dcm_jpg(name)\n    \n    image.save(name+'.jpeg')\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:34:37.430707Z","iopub.execute_input":"2022-09-29T07:34:37.431108Z","iopub.status.idle":"2022-09-29T07:34:37.481166Z","shell.execute_reply.started":"2022-09-29T07:34:37.431076Z","shell.execute_reply":"2022-09-29T07:34:37.479526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nip = './fractured'\nlen(os.listdir(nip))","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:29:39.084873Z","iopub.execute_input":"2022-09-29T07:29:39.085269Z","iopub.status.idle":"2022-09-29T07:29:39.093384Z","shell.execute_reply.started":"2022-09-29T07:29:39.085237Z","shell.execute_reply":"2022-09-29T07:29:39.092209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_names(path):\n    names = []\n    for root, dirnames, filenames in os.walk(path):\n        for filename in filenames:\n            _, ext = os.path.splitext(filename)\n            if ext in ['.dcm']:\n                names.append(filename)\n    return names    \n#print(get_names('../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.1016'))\n\n\ndef convert_dcm_jpg(name):\n    im = pydicom.dcmread('../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.27079/'+name)\n    im = im.pixel_array.astype(float)\n\n    rescaled_image = (np.maximum(im,0)/im.max())*255\n    final_image = np.uint8(rescaled_image)\n    final_image = Image.fromarray(final_image)\n    \n    return final_image\nnames = get_names('../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.27079/')\nfor name in names:\n    image = convert_dcm_jpg(name)\n    \n    image.save(name+'.jpeg')\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:30:33.219684Z","iopub.execute_input":"2022-09-29T07:30:33.220498Z","iopub.status.idle":"2022-09-29T07:30:33.269706Z","shell.execute_reply.started":"2022-09-29T07:30:33.220458Z","shell.execute_reply":"2022-09-29T07:30:33.268008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\n\nsource_folder = r\".//\"\ndestination_folder = r\"./Non-fractured//\"\n\n# fetch all files\nfor file_name in os.listdir(source_folder):\n    # construct full file path\n    source = source_folder + file_name\n    destination = destination_folder + file_name\n    # move only files\n    if os.path.isfile(source):\n        shutil.move(source, destination)\n        #print('Moved:', file_name)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\n\nsource_folder = r\".//\"\ndestination_folder = r\"./fractured//\"\n\n# fetch all files\nfor file_name in os.listdir(source_folder):\n    # construct full file path\n    source = source_folder + file_name\n    destination = destination_folder + file_name\n    # move only files\n    if os.path.isfile(source):\n        shutil.move(source, destination)\n        #print('Moved:', file_name)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:25:17.39123Z","iopub.execute_input":"2022-09-29T07:25:17.391684Z","iopub.status.idle":"2022-09-29T07:25:17.412016Z","shell.execute_reply.started":"2022-09-29T07:25:17.39165Z","shell.execute_reply":"2022-09-29T07:25:17.410758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Starting Model**","metadata":{}},{"cell_type":"code","source":"import pathlib\ndata_dir = './'\ndata_dir = pathlib.Path(data_dir)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:35:28.614031Z","iopub.execute_input":"2022-09-29T07:35:28.614466Z","iopub.status.idle":"2022-09-29T07:35:28.619892Z","shell.execute_reply.started":"2022-09-29T07:35:28.614433Z","shell.execute_reply":"2022-09-29T07:35:28.61877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_count = len(list(data_dir.glob('*/*.jpeg')))\nprint(image_count)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:35:29.967345Z","iopub.execute_input":"2022-09-29T07:35:29.96843Z","iopub.status.idle":"2022-09-29T07:35:29.976923Z","shell.execute_reply.started":"2022-09-29T07:35:29.96838Z","shell.execute_reply":"2022-09-29T07:35:29.975561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import PIL\n\nroses = list(data_dir.glob('fractured/*'))\nPIL.Image.open(str(roses[3]))","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:35:31.28345Z","iopub.execute_input":"2022-09-29T07:35:31.283842Z","iopub.status.idle":"2022-09-29T07:35:31.324827Z","shell.execute_reply.started":"2022-09-29T07:35:31.283812Z","shell.execute_reply":"2022-09-29T07:35:31.323718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roses = list(data_dir.glob('Non-fractured/*'))\nPIL.Image.open(str(roses[3]))","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:35:38.369243Z","iopub.execute_input":"2022-09-29T07:35:38.369668Z","iopub.status.idle":"2022-09-29T07:35:38.39457Z","shell.execute_reply.started":"2022-09-29T07:35:38.369634Z","shell.execute_reply":"2022-09-29T07:35:38.393074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Creating Dataset**","metadata":{}},{"cell_type":"code","source":"batch_size = 32\nimg_height = 180\nimg_width = 180","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:35:43.629559Z","iopub.execute_input":"2022-09-29T07:35:43.629975Z","iopub.status.idle":"2022-09-29T07:35:43.635362Z","shell.execute_reply.started":"2022-09-29T07:35:43.629942Z","shell.execute_reply":"2022-09-29T07:35:43.634138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = tf.keras.utils.image_dataset_from_directory(\n  data_dir,\n  validation_split=0.2,\n  subset=\"training\",\n  seed=123,\n  image_size=(img_height, img_width),\n  batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:35:45.329777Z","iopub.execute_input":"2022-09-29T07:35:45.330164Z","iopub.status.idle":"2022-09-29T07:35:46.678101Z","shell.execute_reply.started":"2022-09-29T07:35:45.330135Z","shell.execute_reply":"2022-09-29T07:35:46.675695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_ds = tf.keras.utils.image_dataset_from_directory(\n  data_dir,\n  validation_split=0.2,\n  subset=\"validation\",\n  seed=123,\n  image_size=(img_height, img_width),\n  batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:35:49.507021Z","iopub.execute_input":"2022-09-29T07:35:49.508312Z","iopub.status.idle":"2022-09-29T07:35:49.632463Z","shell.execute_reply.started":"2022-09-29T07:35:49.508245Z","shell.execute_reply":"2022-09-29T07:35:49.631427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = train_ds.class_names\nprint(class_names)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:35:51.533425Z","iopub.execute_input":"2022-09-29T07:35:51.53382Z","iopub.status.idle":"2022-09-29T07:35:51.539502Z","shell.execute_reply.started":"2022-09-29T07:35:51.533789Z","shell.execute_reply":"2022-09-29T07:35:51.538366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.rmdir('./.virtual_documents')","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:35:52.932338Z","iopub.execute_input":"2022-09-29T07:35:52.933088Z","iopub.status.idle":"2022-09-29T07:35:52.937306Z","shell.execute_reply.started":"2022-09-29T07:35:52.93305Z","shell.execute_reply":"2022-09-29T07:35:52.936405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('./')","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:35:54.761689Z","iopub.execute_input":"2022-09-29T07:35:54.762109Z","iopub.status.idle":"2022-09-29T07:35:54.770015Z","shell.execute_reply.started":"2022-09-29T07:35:54.762073Z","shell.execute_reply":"2022-09-29T07:35:54.768821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.figure(figsize=(10, 10))\nfor images, labels in train_ds.take(1):\n    for i in range(9):\n        ax = plt.subplot(3, 3, i + 1)\n        plt.imshow(images[i].numpy().astype(\"uint8\"))\n        plt.title(class_names[labels[i]])\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-09-29T07:38:01.85939Z","iopub.execute_input":"2022-09-29T07:38:01.85977Z","iopub.status.idle":"2022-09-29T07:38:02.603496Z","shell.execute_reply.started":"2022-09-29T07:38:01.859739Z","shell.execute_reply":"2022-09-29T07:38:02.602329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Work Assigned**","metadata":{}},{"cell_type":"code","source":"!pip install -qU \"python-gdcm\" pydicom pylibjpeg \"opencv-python-headless\"","metadata":{"execution":{"iopub.status.busy":"2022-09-28T11:52:31.589866Z","iopub.execute_input":"2022-09-28T11:52:31.59032Z","iopub.status.idle":"2022-09-28T11:52:56.312346Z","shell.execute_reply.started":"2022-09-28T11:52:31.590287Z","shell.execute_reply":"2022-09-28T11:52:56.310716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport os\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nfrom plotly.offline import init_notebook_mode, iplot, plot\nfrom tqdm.notebook import tqdm\nfrom pathlib import Path\nfrom collections import Counter\n\ntqdm.pandas()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T11:52:56.315689Z","iopub.execute_input":"2022-09-28T11:52:56.316072Z","iopub.status.idle":"2022-09-28T11:52:57.87267Z","shell.execute_reply.started":"2022-09-28T11:52:56.316038Z","shell.execute_reply":"2022-09-28T11:52:57.871464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nimport nibabel as nib\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut","metadata":{"execution":{"iopub.status.busy":"2022-09-28T11:52:57.874266Z","iopub.execute_input":"2022-09-28T11:52:57.874633Z","iopub.status.idle":"2022-09-28T11:52:57.978763Z","shell.execute_reply.started":"2022-09-28T11:52:57.874591Z","shell.execute_reply":"2022-09-28T11:52:57.977333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\n\npaths = {\n    'train_df': Path('../input/rsna-2022-cervical-spine-fracture-detection/train.csv'),\n    'train_bbox': Path('../input/rsna-2022-cervical-spine-fracture-detection/train_bounding_boxes.csv'),\n    'train_images': Path('../input/rsna-2022-cervical-spine-fracture-detection/train_images'),\n    'train_nifti_segments': Path('../input/rsna-2022-cervical-spine-fracture-detection/segmentations'),\n    'test_df': Path('../input/rsna-2022-cervical-spine-fracture-detection/test.csv'),\n    'test_images': Path('../input/rsna-2022-cervical-spine-fracture-detection/test_images')\n}","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:15:24.348727Z","iopub.execute_input":"2022-09-29T05:15:24.349597Z","iopub.status.idle":"2022-09-29T05:15:24.35641Z","shell.execute_reply.started":"2022-09-29T05:15:24.34955Z","shell.execute_reply":"2022-09-29T05:15:24.35494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(paths['train_df'])\ntest_df = pd.read_csv(paths['test_df'])","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:15:24.775126Z","iopub.execute_input":"2022-09-29T05:15:24.775636Z","iopub.status.idle":"2022-09-29T05:15:24.795653Z","shell.execute_reply.started":"2022-09-29T05:15:24.775598Z","shell.execute_reply":"2022-09-29T05:15:24.794563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.drop(index=301, inplace=True)\ntrain_df.reset_index(drop=True, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:15:25.372208Z","iopub.execute_input":"2022-09-29T05:15:25.373457Z","iopub.status.idle":"2022-09-29T05:15:25.379904Z","shell.execute_reply.started":"2022-09-29T05:15:25.373406Z","shell.execute_reply":"2022-09-29T05:15:25.378965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:16:12.408837Z","iopub.execute_input":"2022-09-29T05:16:12.409269Z","iopub.status.idle":"2022-09-29T05:16:12.42341Z","shell.execute_reply.started":"2022-09-29T05:16:12.409233Z","shell.execute_reply":"2022-09-29T05:16:12.422209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = pydicom.dcmread('../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.32046/257.dcm')\nplt.imshow(ds.pixel_array, cmap=plt.cm.bone)  # set the color map to bone\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T11:52:58.060504Z","iopub.execute_input":"2022-09-28T11:52:58.060975Z","iopub.status.idle":"2022-09-28T11:52:58.351634Z","shell.execute_reply.started":"2022-09-28T11:52:58.060924Z","shell.execute_reply":"2022-09-28T11:52:58.350304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.drop(index=1135, inplace=True)\ntrain_df.reset_index(drop=True, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T11:52:58.353238Z","iopub.execute_input":"2022-09-28T11:52:58.353578Z","iopub.status.idle":"2022-09-28T11:52:58.359995Z","shell.execute_reply.started":"2022-09-28T11:52:58.353549Z","shell.execute_reply":"2022-09-28T11:52:58.358579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['total_fractures'] = train_df.loc[:,[f\"C{i}\" for i in range(1,8)]].sum(axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T11:52:58.36416Z","iopub.execute_input":"2022-09-28T11:52:58.364609Z","iopub.status.idle":"2022-09-28T11:52:58.374379Z","shell.execute_reply.started":"2022-09-28T11:52:58.364573Z","shell.execute_reply":"2022-09-28T11:52:58.373092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(100)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T11:52:58.376268Z","iopub.execute_input":"2022-09-28T11:52:58.37672Z","iopub.status.idle":"2022-09-28T11:52:58.40277Z","shell.execute_reply.started":"2022-09-28T11:52:58.376684Z","shell.execute_reply":"2022-09-28T11:52:58.401398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_df)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T11:52:58.406839Z","iopub.execute_input":"2022-09-28T11:52:58.407243Z","iopub.status.idle":"2022-09-28T11:52:58.415181Z","shell.execute_reply.started":"2022-09-28T11:52:58.40721Z","shell.execute_reply":"2022-09-28T11:52:58.414273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['segment_path'] = train_df['StudyInstanceUID'].map(lambda x: paths['train_images']/x)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T11:52:58.416621Z","iopub.execute_input":"2022-09-28T11:52:58.417138Z","iopub.status.idle":"2022-09-28T11:52:58.438774Z","shell.execute_reply.started":"2022-09-28T11:52:58.417102Z","shell.execute_reply":"2022-09-28T11:52:58.437545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T11:52:58.440239Z","iopub.execute_input":"2022-09-28T11:52:58.440613Z","iopub.status.idle":"2022-09-28T11:52:58.461671Z","shell.execute_reply.started":"2022-09-28T11:52:58.440581Z","shell.execute_reply":"2022-09-28T11:52:58.460334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def num_slices(path):\n    slices = list(path.glob('*'))\n    return len(slices)\n\ntrain_df['num_slices'] = train_df['segment_path'].progress_map(num_slices)\ntrain_df['num_slices'] = train_df['num_slices'].astype('int')","metadata":{"execution":{"iopub.status.busy":"2022-09-28T11:52:58.463024Z","iopub.execute_input":"2022-09-28T11:52:58.463452Z","iopub.status.idle":"2022-09-28T11:54:03.91333Z","shell.execute_reply.started":"2022-09-28T11:52:58.463418Z","shell.execute_reply":"2022-09-28T11:54:03.911868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T11:54:03.9152Z","iopub.execute_input":"2022-09-28T11:54:03.91595Z","iopub.status.idle":"2022-09-28T11:54:03.931798Z","shell.execute_reply.started":"2022-09-28T11:54:03.915913Z","shell.execute_reply":"2022-09-28T11:54:03.930521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_dcm_file = list(train_df['segment_path'][123].glob('*'))[130]\nprint(random_dcm_file)\nrandom_dcm_file = pydicom.dcmread(random_dcm_file)\nprint(random_dcm_file)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T11:54:03.93334Z","iopub.execute_input":"2022-09-28T11:54:03.933858Z","iopub.status.idle":"2022-09-28T11:54:03.967966Z","shell.execute_reply.started":"2022-09-28T11:54:03.933813Z","shell.execute_reply":"2022-09-28T11:54:03.966802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# getting metadata with .get(key)\nprint(\"Instance Number:\",random_dcm_file.get('InstanceNumber'))\nprint(\"Rows x Columns:\", random_dcm_file.get(\"Rows\"), random_dcm_file.get(\"Columns\"))\nprint(\"Image Position (Patient):\", random_dcm_file.get(\"ImagePositionPatient\"))","metadata":{"execution":{"iopub.status.busy":"2022-09-28T11:54:03.969706Z","iopub.execute_input":"2022-09-28T11:54:03.97016Z","iopub.status.idle":"2022-09-28T11:54:03.978942Z","shell.execute_reply.started":"2022-09-28T11:54:03.970123Z","shell.execute_reply":"2022-09-28T11:54:03.977253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dcm_image = apply_voi_lut(random_dcm_file.pixel_array, random_dcm_file)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T11:54:03.980988Z","iopub.execute_input":"2022-09-28T11:54:03.981366Z","iopub.status.idle":"2022-09-28T11:54:03.998432Z","shell.execute_reply.started":"2022-09-28T11:54:03.981334Z","shell.execute_reply":"2022-09-28T11:54:03.996697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(dcm_image, cmap='bone')","metadata":{"execution":{"iopub.status.busy":"2022-09-28T11:54:04.001445Z","iopub.execute_input":"2022-09-28T11:54:04.002091Z","iopub.status.idle":"2022-09-28T11:54:04.255008Z","shell.execute_reply.started":"2022-09-28T11:54:04.002048Z","shell.execute_reply":"2022-09-28T11:54:04.253459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_nii_file = list(paths['train_nifti_segments'].glob('*'))[4]\nprint(random_nii_file)\n\ndef open_nii_file(path):\n    f = nib.load(path)\n    segmentations = f.get_fdata()[:, ::-1, ::-1].transpose(2, 1, 0)\n    return segmentations\n\nnii_segments = open_nii_file(random_nii_file)\nprint(nii_segments.shape, \"=> (num_slices, height, width)\")\nplt.imshow(nii_segments[123,:,:],cmap='bone')","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:18:47.356022Z","iopub.execute_input":"2022-09-28T07:18:47.356428Z","iopub.status.idle":"2022-09-28T07:18:48.868967Z","shell.execute_reply.started":"2022-09-28T07:18:47.356396Z","shell.execute_reply":"2022-09-28T07:18:48.867493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#np.unique(nii_segments[slice_number])\nprint(\"[background, *, vertebra]\")\nprint(\"here 4. denotes that the slice is a part of C4 vertebra\")\nnp.unique(nii_segments[100])","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:18:51.032124Z","iopub.execute_input":"2022-09-28T07:18:51.034094Z","iopub.status.idle":"2022-09-28T07:18:51.057028Z","shell.execute_reply.started":"2022-09-28T07:18:51.034034Z","shell.execute_reply":"2022-09-28T07:18:51.055489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f\"We only have {len(list(paths['train_nifti_segments'].glob('*')))} nii files / {len(train_df)} studies\"","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:19:13.633739Z","iopub.execute_input":"2022-09-28T07:19:13.634216Z","iopub.status.idle":"2022-09-28T07:19:13.645053Z","shell.execute_reply.started":"2022-09-28T07:19:13.634181Z","shell.execute_reply":"2022-09-28T07:19:13.643718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_nii_segment_path(uid):\n    base_path = paths['train_nifti_segments']\n    # path if exists else None\n    path = base_path/(uid+'.nii')\n    if path.exists():\n        return path\n    return None\n\ntrain_df['nii_segments_path'] = train_df['StudyInstanceUID'].map(add_nii_segment_path)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:19:16.635672Z","iopub.execute_input":"2022-09-28T07:19:16.636484Z","iopub.status.idle":"2022-09-28T07:19:18.182417Z","shell.execute_reply.started":"2022-09-28T07:19:16.636445Z","shell.execute_reply":"2022-09-28T07:19:18.181214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_dcm_images(path):\n    paths = list(path.glob('*'))\n    paths.sort(key=lambda x:int(x.stem)) # sort based on slice index which is the filename: index.dcm\n    data = [pydicom.dcmread(f) for f in paths]\n    images = [apply_voi_lut(dcm.pixel_array, dcm) for dcm in data]\n    return images","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:19:47.448485Z","iopub.execute_input":"2022-09-28T07:19:47.448902Z","iopub.status.idle":"2022-09-28T07:19:47.456862Z","shell.execute_reply.started":"2022-09-28T07:19:47.448867Z","shell.execute_reply":"2022-09-28T07:19:47.455145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_nii_segments(path):\n    f = nib.load(path)\n    segmentations = f.get_fdata()[:, ::-1, ::-1].transpose(2, 1, 0)\n    return segmentations","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:19:50.704161Z","iopub.execute_input":"2022-09-28T07:19:50.704945Z","iopub.status.idle":"2022-09-28T07:19:50.710003Z","shell.execute_reply.started":"2022-09-28T07:19:50.704904Z","shell.execute_reply":"2022-09-28T07:19:50.70915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_idx = 99\ndcm_images = get_dcm_images(train_df['segment_path'][sample_idx])\nnii_segments = get_nii_segments(train_df['nii_segments_path'][sample_idx])\nprint((len(dcm_images), *dcm_images[0].shape), nii_segments.shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:20:32.195262Z","iopub.execute_input":"2022-09-28T07:20:32.195748Z","iopub.status.idle":"2022-09-28T07:20:41.4789Z","shell.execute_reply.started":"2022-09-28T07:20:32.195701Z","shell.execute_reply":"2022-09-28T07:20:41.476687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_slice(idx, dcm_images=dcm_images, nii_segments=nii_segments):\n    fig, (ax1, ax2) = plt.subplots(1, 2)\n    ax1.axis('off'); ax2.axis('off')\n    fig.suptitle(f'Slice {idx}')\n    ax1.imshow(dcm_images[idx], cmap='bone')\n    ax2.imshow(nii_segments[idx,:,:], cmap='bone')\n\nfor i in range(123,128):\n    plot_slice(i)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:20:41.481198Z","iopub.execute_input":"2022-09-28T07:20:41.481586Z","iopub.status.idle":"2022-09-28T07:20:42.745402Z","shell.execute_reply.started":"2022-09-28T07:20:41.481553Z","shell.execute_reply":"2022-09-28T07:20:42.742939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"uids_with_segments = train_df[train_df['nii_segments_path'].notnull()]\nuids_with_segments.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:20:42.748509Z","iopub.execute_input":"2022-09-28T07:20:42.749475Z","iopub.status.idle":"2022-09-28T07:20:42.78448Z","shell.execute_reply.started":"2022-09-28T07:20:42.749429Z","shell.execute_reply":"2022-09-28T07:20:42.782293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def check_reverse_required(path):\n    paths = list(path.glob('*'))\n    paths.sort(key=lambda x:int(x.stem))\n    z_first = pydicom.dcmread(paths[0]).get(\"ImagePositionPatient\")[-1]\n    z_last = pydicom.dcmread(paths[-1]).get(\"ImagePositionPatient\")[-1]\n    if z_last < z_first:\n        return False\n    return True","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:20:42.787048Z","iopub.execute_input":"2022-09-28T07:20:42.787414Z","iopub.status.idle":"2022-09-28T07:20:42.798602Z","shell.execute_reply.started":"2022-09-28T07:20:42.787384Z","shell.execute_reply":"2022-09-28T07:20:42.796195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checks = train_df['segment_path'].progress_map(check_reverse_required)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:20:43.042271Z","iopub.execute_input":"2022-09-28T07:20:43.042896Z","iopub.status.idle":"2022-09-28T07:22:31.984731Z","shell.execute_reply.started":"2022-09-28T07:20:43.042853Z","shell.execute_reply":"2022-09-28T07:22:31.983338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['reverse_required'] = checks","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:22:31.9867Z","iopub.execute_input":"2022-09-28T07:22:31.987088Z","iopub.status.idle":"2022-09-28T07:22:31.993922Z","shell.execute_reply.started":"2022-09-28T07:22:31.987054Z","shell.execute_reply":"2022-09-28T07:22:31.992798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Counter(checks)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T07:22:36.401696Z","iopub.execute_input":"2022-09-28T07:22:36.402132Z","iopub.status.idle":"2022-09-28T07:22:36.411351Z","shell.execute_reply.started":"2022-09-28T07:22:36.402097Z","shell.execute_reply":"2022-09-28T07:22:36.410001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices_where_reverse_required = [i for i,req in (checks.reset_index()).values if req is True]\nprint(indices_where_reverse_required)","metadata":{"execution":{"iopub.status.busy":"2022-09-28T09:51:23.474992Z","iopub.execute_input":"2022-09-28T09:51:23.475367Z","iopub.status.idle":"2022-09-28T09:51:23.493875Z","shell.execute_reply.started":"2022-09-28T09:51:23.475335Z","shell.execute_reply":"2022-09-28T09:51:23.492119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df.loc[0])\ndcm_images = get_dcm_images(train_df['segment_path'][0]) # 0 fractures\ndcm_np = np.array(dcm_images)\nif train_df['reverse_required'][0] == True:\n    dcm_np = dcm_np[::-1]\nsaggital_view = dcm_np[:,:,256]\nplt.imshow(saggital_view, cmap='bone')","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:37:43.949044Z","iopub.execute_input":"2022-09-29T05:37:43.949475Z","iopub.status.idle":"2022-09-29T05:37:43.971008Z","shell.execute_reply.started":"2022-09-29T05:37:43.949441Z","shell.execute_reply":"2022-09-29T05:37:43.969526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor uid, path, rev in tqdm(train_df.loc[:,['StudyInstanceUID','segment_path','reverse_required']].values):\n    dcm_images = get_dcm_images(path)\n    dcm_np = np.array(dcm_images)\n    if rev == True:\n        dcm_np = dcm_np[::-1]\n    saggital_view = dcm_np[:,:,256]\n    plt.imshow(saggital_view,cmap='bone')\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T09:51:13.011446Z","iopub.execute_input":"2022-09-28T09:51:13.011988Z","iopub.status.idle":"2022-09-28T09:51:13.040618Z","shell.execute_reply.started":"2022-09-28T09:51:13.011935Z","shell.execute_reply":"2022-09-28T09:51:13.038871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****SAMPLE****","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom matplotlib import animation, rc; rc('animation', html='jshtml')\nfrom matplotlib.colors import ListedColormap, LinearSegmentedColormap\nimport matplotlib.patches as patches\nfrom IPython.display import display_html\nimport seaborn as sns\nimport re\nimport os\nimport cv2\nimport gc\nfrom glob import glob\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nfrom pathlib import Path","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:19:01.702004Z","iopub.execute_input":"2022-09-29T05:19:01.702427Z","iopub.status.idle":"2022-09-29T05:19:01.71308Z","shell.execute_reply.started":"2022-09-29T05:19:01.702394Z","shell.execute_reply":"2022-09-29T05:19:01.711848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\n\nwith open('../input/rsna-2022-cervical-spine-fracture-detection/train.csv') as inp, open('temp.csv', 'w') as out:\n    reader = csv.reader(inp)\n    writer = csv.writer(out, delimiter=',')\n    #No need to use `insert(), `append()` simply use `+` to concatenate two lists.\n    writer.writerow(['ID'] + next(reader))\n    #Iterate over enumerate object of reader and pass the starting index as 1.\n    writer.writerows([i] + row for i, row in enumerate(reader, 1))","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:19:02.731878Z","iopub.execute_input":"2022-09-29T05:19:02.732295Z","iopub.status.idle":"2022-09-29T05:19:02.744216Z","shell.execute_reply.started":"2022-09-29T05:19:02.732248Z","shell.execute_reply":"2022-09-29T05:19:02.743092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths = {\n    'temp_df': Path('./temp.csv'),\n}","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:19:10.023926Z","iopub.execute_input":"2022-09-29T05:19:10.024632Z","iopub.status.idle":"2022-09-29T05:19:10.029678Z","shell.execute_reply.started":"2022-09-29T05:19:10.024595Z","shell.execute_reply":"2022-09-29T05:19:10.028425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_df = pd.read_csv(paths['temp_df'])","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:19:11.608362Z","iopub.execute_input":"2022-09-29T05:19:11.608783Z","iopub.status.idle":"2022-09-29T05:19:11.619031Z","shell.execute_reply.started":"2022-09-29T05:19:11.608748Z","shell.execute_reply":"2022-09-29T05:19:11.617786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![](http://)![](http://)","metadata":{}},{"cell_type":"code","source":"temp_df = pd.read_csv(\"./temp.csv\")\ntemp_df[\"total_fractures\"] = temp_df.iloc[:, 3:].sum(axis=1)\ndt = pd.melt(temp_df, \n             id_vars=['StudyInstanceUID', 'patient_overall','total_fractures'],\n             var_name=\"Vertebra\",\n             value_name=\"Flag\")","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:19:13.828875Z","iopub.execute_input":"2022-09-29T05:19:13.829273Z","iopub.status.idle":"2022-09-29T05:19:13.845032Z","shell.execute_reply.started":"2022-09-29T05:19:13.82924Z","shell.execute_reply":"2022-09-29T05:19:13.844031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_df[temp_df['StudyInstanceUID'] == \"1.2.826.0.1.3680043.6200\"]","metadata":{"execution":{"iopub.status.busy":"2022-09-29T05:34:09.752339Z","iopub.execute_input":"2022-09-29T05:34:09.75274Z","iopub.status.idle":"2022-09-29T05:34:09.767485Z","shell.execute_reply.started":"2022-09-29T05:34:09.752708Z","shell.execute_reply":"2022-09-29T05:34:09.766192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = \"../input/rsna-2022-cervical-spine-fracture-detection/\"\nstudy_instance = '1.2.826.0.1.3680043.18197' \n\nos.listdir(DATA_DIR + f\"train_images/{study_instance}\")[:5]","metadata":{"execution":{"iopub.status.busy":"2022-09-28T11:12:28.878063Z","iopub.execute_input":"2022-09-28T11:12:28.878488Z","iopub.status.idle":"2022-09-28T11:12:28.886339Z","shell.execute_reply.started":"2022-09-28T11:12:28.878435Z","shell.execute_reply":"2022-09-28T11:12:28.884833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}