{"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,"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Sumeet Sagar https://www.kaggle.com/code/ssagar012/siim-covid-19-novice-notebook-eda-box-detection\n\n# Installing dcm library to read Dicom Images\n!pip install python-gdcm\nprint(\"Installation Complete\")\n!pip install tensorflow-io\nprint(\" TF - io Installed Successfully\")","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-10-20T22:31:41.656397Z","iopub.execute_input":"2022-10-20T22:31:41.656869Z","iopub.status.idle":"2022-10-20T22:32:05.617439Z","shell.execute_reply.started":"2022-10-20T22:31:41.656833Z","shell.execute_reply":"2022-10-20T22:32:05.616338Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Sumeet Sagar https://www.kaggle.com/code/ssagar012/siim-covid-19-novice-notebook-eda-box-detection\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport ast #helps to process trees of the Python abstract syntax grammar.\nimport pydicom # for working with DICOM files such as medical images, reports, and radiotherapy objects.\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport PIL # Python Imaging Library\nfrom PIL import Image, ImageDraw, ImageFont #Python Imaging Library\nimport tensorflow as tf\n\nimport tensorflow_hub as hub\nimport wandb # experiment tracking, dataset versioning, and model management\nimport seaborn as sns\nimport tqdm # visualise progress\nimport cv2 #convert dicom to png","metadata":{"execution":{"iopub.status.busy":"2022-10-20T22:32:23.007307Z","iopub.execute_input":"2022-10-20T22:32:23.007985Z","iopub.status.idle":"2022-10-20T22:32:23.044046Z","shell.execute_reply.started":"2022-10-20T22:32:23.007883Z","shell.execute_reply":"2022-10-20T22:32:23.042139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Sumeet Sagar https://www.kaggle.com/code/ssagar012/siim-covid-19-novice-notebook-eda-box-detection\n\n# Importing the training files names\nt_image_fnames = []\npath = \"/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train_images/\"\nimport os\nlen(os.listdir(path))\nfor root, dirs, filenames in os.walk(path):\n    for fname in filenames:\n        t_image_fnames.append(os.path.join(root,fname))\n\ntrain_image_level = pd.read_csv(\"/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train_bounding_boxes.csv\")\ntrain_study_level = pd.read_csv(\"/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train.csv\")    \nlen(t_image_fnames)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T22:32:28.439273Z","iopub.execute_input":"2022-10-20T22:32:28.440374Z","iopub.status.idle":"2022-10-20T22:32:32.854183Z","shell.execute_reply.started":"2022-10-20T22:32:28.440328Z","shell.execute_reply":"2022-10-20T22:32:32.852425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Sumeet Sagar https://www.kaggle.com/code/ssagar012/siim-covid-19-novice-notebook-eda-box-detection\n\n# Crosschecking that the number of image file paths is same as the number of image IDs\nif len(train_image_level.StudyInstanceUID) == len(t_image_fnames):\n    print(\"length is almost the same\")\n    \nelse:\n    print(\"holy moly\")\n    \ntrain_image_level.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-20T22:32:34.724228Z","iopub.execute_input":"2022-10-20T22:32:34.724643Z","iopub.status.idle":"2022-10-20T22:32:34.74329Z","shell.execute_reply.started":"2022-10-20T22:32:34.724593Z","shell.execute_reply":"2022-10-20T22:32:34.742012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"There are \",train_image_level.StudyInstanceUID.duplicated().sum(),\" Images that refer to duplicated study IDs\")","metadata":{"execution":{"iopub.status.busy":"2022-10-20T22:32:42.020655Z","iopub.execute_input":"2022-10-20T22:32:42.021079Z","iopub.status.idle":"2022-10-20T22:32:42.031527Z","shell.execute_reply.started":"2022-10-20T22:32:42.021045Z","shell.execute_reply":"2022-10-20T22:32:42.029817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = t_image_fnames\ny = train_image_level[\"StudyInstanceUID\"]","metadata":{"execution":{"iopub.status.busy":"2022-10-20T22:35:23.843223Z","iopub.execute_input":"2022-10-20T22:35:23.84422Z","iopub.status.idle":"2022-10-20T22:35:23.851287Z","shell.execute_reply.started":"2022-10-20T22:35:23.844063Z","shell.execute_reply":"2022-10-20T22:35:23.849852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Sumeet Sagar https://www.kaggle.com/code/ssagar012/siim-covid-19-novice-notebook-eda-box-detection\n\nimport matplotlib.pyplot as plt\n\nimport pydicom\n%matplotlib inline\nplt.figure(figsize = (10,8))\nimage = pydicom.dcmread(X[21])\nplt.imshow(image.pixel_array,cmap=plt.cm.bone);","metadata":{"execution":{"iopub.status.busy":"2022-10-20T22:35:51.32725Z","iopub.execute_input":"2022-10-20T22:35:51.329076Z","iopub.status.idle":"2022-10-20T22:35:51.754558Z","shell.execute_reply.started":"2022-10-20T22:35:51.329005Z","shell.execute_reply":"2022-10-20T22:35:51.753295Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Sumeet Sagar https://www.kaggle.com/code/ssagar012/siim-covid-19-novice-notebook-eda-box-detection\n\n## Function to Display 25 Images\ndef show_25_images(images):\n    \"\"\"\n    Displays a plot of 25 images and their labes for training images\n    \"\"\"\n    \n    # setup the figure\n    plt.figure(figsize = (10,10))\n    \n    # loop through 25 files to display 25 images\n    for i in range(25):\n        # Create subplots ( 5 rows , 5 columns)\n        ax = plt.subplot(5,5,i+1)\n        # display an image\n        image = pydicom.dcmread(images[i])\n        plt.imshow(image.pixel_array,cmap = plt.cm.bone)\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-10-20T22:36:23.946569Z","iopub.execute_input":"2022-10-20T22:36:23.94783Z","iopub.status.idle":"2022-10-20T22:36:23.956321Z","shell.execute_reply.started":"2022-10-20T22:36:23.947772Z","shell.execute_reply":"2022-10-20T22:36:23.955005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_25_images(X[20:])","metadata":{"execution":{"iopub.status.busy":"2022-10-20T22:36:37.798972Z","iopub.execute_input":"2022-10-20T22:36:37.799382Z","iopub.status.idle":"2022-10-20T22:36:40.948502Z","shell.execute_reply.started":"2022-10-20T22:36:37.79935Z","shell.execute_reply":"2022-10-20T22:36:40.947085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Sumeet Sagar https://www.kaggle.com/code/ssagar012/siim-covid-19-novice-notebook-eda-box-detection\n\nimport ast\nboxes = ast.literal_eval(train_image_level.loc[3,'slice_number'])\nboxes","metadata":{"execution":{"iopub.status.busy":"2022-10-20T22:39:52.949303Z","iopub.execute_input":"2022-10-20T22:39:52.950451Z","iopub.status.idle":"2022-10-20T22:39:52.985199Z","shell.execute_reply.started":"2022-10-20T22:39:52.950403Z","shell.execute_reply":"2022-10-20T22:39:52.983258Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Where we read slice_number  it was supposed to be boxes column. And didn't work.","metadata":{}},{"cell_type":"code","source":"#Code by Sumeet Sagar https://www.kaggle.com/code/ssagar012/siim-covid-19-novice-notebook-eda-box-detection\n\ndef display_image_and_box(image):\n    \"\"\"\n    Takes image number as input\n    \"\"\"\n    import matplotlib\n    fig, axs = plt.subplots(3,3,figsize = (20,16))\n    fig.subplots_adjust(hspace = .1 , wspace = .1)\n    axs = axs.ravel()\n    row = image\n    i=0\n    for row in range(row,row+9):\n        study = train_image_level.loc[row, 'StudyInstanceUID']\n        dt_file =pydicom.dcmread( X[row])\n        img = dt_file.pixel_array\n        if(train_image_level.loc[row,'slice_number']!= train_image_level.loc[row,'slice_number']) == False:\n            boxes = ast.literal_eval(train_image_level.loc[row,'slice_number'])\n        \n            for box in boxes:\n                p = matplotlib.patches.Rectangle((box['x'],box['y']), box['width'], box['height'],\n                                           ec = 'r',fc = 'none', lw = 2.)\n                axs[i].add_patch(p)\n        axs[i].imshow(img,cmap = plt.cm.bone)\n        axs[i].set_title(train_image_level.loc[row,'slice_number'].split(' ')[0])\n        axs[i].set_xticklabels([])\n        axs[i].set_yticklabels([])\n        i+=1","metadata":{"execution":{"iopub.status.busy":"2022-10-20T22:42:47.344259Z","iopub.execute_input":"2022-10-20T22:42:47.344977Z","iopub.status.idle":"2022-10-20T22:42:47.355896Z","shell.execute_reply.started":"2022-10-20T22:42:47.344936Z","shell.execute_reply":"2022-10-20T22:42:47.354574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#display_image_and_box(19)","metadata":{"execution":{"iopub.status.busy":"2022-10-20T22:43:33.627386Z","iopub.execute_input":"2022-10-20T22:43:33.628878Z","iopub.status.idle":"2022-10-20T22:43:33.634201Z","shell.execute_reply.started":"2022-10-20T22:43:33.62882Z","shell.execute_reply":"2022-10-20T22:43:33.632826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Acknowledgements:\n\nSumeet Sagar https://www.kaggle.com/code/ssagar012/siim-covid-19-novice-notebook-eda-box-detection","metadata":{}}]}