{"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 style=\"text-align: center\"> First Impression about this RSNA Intracranial Hemorrhage\n</div>\n<img src=\"https://images.idgesg.net/images/article/2017/09/data-science-certification10-100734865-large.jpg\">\n\n   Bleeding, also called hemorrhage, is the name used to describe blood loss. It can refer to blood loss inside the body, called internal bleeding, or to blood loss outside of the body, called external bleeding.\n\nIn this we are working with 4 types and another any\n\n- 1_epidural\n- intraparenchymal\n- intraventricular\n- subarachnoid\n- subdural\n- any\n","metadata":{}},{"cell_type":"markdown","source":"<a id=\"top\"></a> <br>\n## Notebook  Content\n\n1. [What is an intracranial hemorrhage](#1)\n1. [Import](#2)\n1. [Load Data](#3)\n1. [Check images](#4)\n1. [Visualization](#5)\n1. [Working newTable ](#6)\n1. [Visualization of hemorrhage epidural](#7)\n1. [Visualization of hemorrhage Intraparenchymal](#8)\n1. [Visualization of hemorrhage Intraparenchymal](#9)\n1. [Visualization of hemorrhage Subarachnoid](#10)\n1. [Visualization of hemorrhage subdural](#11)\n\n","metadata":{}},{"cell_type":"markdown","source":"**<a id=\"1\"></a> <br>**\n## 1- What is an intracranial hemorrhage","metadata":{}},{"cell_type":"markdown","source":"<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F603584%2F56162e47358efd77010336a373beb0d2%2Fsubtypes-of-hemorrhage.png?generation=1568657910458946&alt=media\">","metadata":{}},{"cell_type":"markdown","source":"Intracranial hemorrhage (ICH) refers to acute bleeding inside your skull or brain. It’s a life-threatening emergency. You should go to the emergency room right away or call 911 if you think you or someone you know is experiencing ICH.","metadata":{}},{"cell_type":"markdown","source":"### What are the types of ICH?\n\nThere are four types of ICH:\n\n- epidural hematoma\n- subdural hematoma\n- subarachnoid hemorrhage\n- intracerebral hemorrhage","metadata":{}},{"cell_type":"markdown","source":"### Epidural hematoma\n\nA hematoma is a collection of blood, in a clot or ball, outside of a blood vessel. An epidural hematoma occurs when blood accumulates between your skull and the outermost covering of your brain.\n\nIt typically follows a head injury, and usually with a skull fracture. High-pressure bleeding is a prominent feature. If you have an epidural hematoma, you may briefly lose consciousness and then regain consciousness.","metadata":{}},{"cell_type":"markdown","source":"### Subdural hematoma\n\nA subdural hematoma is a collection of blood on the surface of your brain.\n\nIt’s typically the result of your head moving rapidly forward and stopping, such as in a car accident. However, it could also suggest abuse in children. This is the same type of movement a child experiences when being shaken.\n\nA subdural hematoma is more common than other ICHs in older people and people with history of heavy alcohol use.","metadata":{}},{"cell_type":"markdown","source":"### Intracerebral hemorrhage\nIntracerebral hemorrhage is when there’s bleeding inside of your brain. This is the most common type of ICH that occurs with a stroke. It’s not usually the result of injury.\n\nA prominent warning sign is the sudden onset of neurological deficit. This is a problem with your brain’s functioning. The symptoms progress over minutes to hours. They include:\n\n- headache\n- difficulty speaking\n- nausea\n- vomiting\n- decreased consciousness\n- weakness in one part of the body\n- elevated blood pressure","metadata":{}},{"cell_type":"markdown","source":"**<a id=\"2\"></a> <br>**\n## 2- Import","metadata":{}},{"cell_type":"code","source":"import glob, pylab, pandas as pd\nimport pydicom, numpy as np\nfrom os import listdir\nfrom os.path import isfile, join\nimport matplotlib.pylab as plt\nimport os\nimport seaborn as sns","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2023-10-31T19:37:52.316591Z","iopub.execute_input":"2023-10-31T19:37:52.317304Z","iopub.status.idle":"2023-10-31T19:37:53.33412Z","shell.execute_reply.started":"2023-10-31T19:37:52.317213Z","shell.execute_reply":"2023-10-31T19:37:53.332925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import layers\nfrom keras.applications import DenseNet121\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import Callback, ModelCheckpoint\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.optimizers import Adam\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-10-31T19:38:36.459226Z","iopub.execute_input":"2023-10-31T19:38:36.460341Z","iopub.status.idle":"2023-10-31T19:38:39.026873Z","shell.execute_reply.started":"2023-10-31T19:38:36.460252Z","shell.execute_reply":"2023-10-31T19:38:39.025387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH=\"../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection\"\n!ls ../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection","metadata":{"execution":{"iopub.status.busy":"2023-10-31T19:40:05.79717Z","iopub.execute_input":"2023-10-31T19:40:05.797659Z","iopub.status.idle":"2023-10-31T19:40:06.825704Z","shell.execute_reply.started":"2023-10-31T19:40:05.797579Z","shell.execute_reply":"2023-10-31T19:40:06.824268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"3\"></a> <br>\n## 3- Load Data\n\n**stage_1_train.csv** - the training set. Contains Ids and target information.\n**stage_1_sample_submission.csv** - a sample submission file in the correct format. Contains Ids for the test set.\n\n\nStage 1 Images - **stage_1_train_images.zip** and **stage_1_test_images.zip**\n\n- images for the current stage. Filenames are also patient names.\n\n","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(join(PATH,'stage_2_train.csv'))","metadata":{"execution":{"iopub.status.busy":"2023-10-31T19:41:21.087261Z","iopub.execute_input":"2023-10-31T19:41:21.088032Z","iopub.status.idle":"2023-10-31T19:41:26.924893Z","shell.execute_reply.started":"2023-10-31T19:41:21.087968Z","shell.execute_reply":"2023-10-31T19:41:26.923904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-10-31T19:41:35.082123Z","iopub.execute_input":"2023-10-31T19:41:35.082871Z","iopub.status.idle":"2023-10-31T19:41:35.109592Z","shell.execute_reply.started":"2023-10-31T19:41:35.082803Z","shell.execute_reply":"2023-10-31T19:41:35.108454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-31T19:41:41.792325Z","iopub.execute_input":"2023-10-31T19:41:41.792808Z","iopub.status.idle":"2023-10-31T19:41:41.799834Z","shell.execute_reply.started":"2023-10-31T19:41:41.792731Z","shell.execute_reply":"2023-10-31T19:41:41.798683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"newtable = train.copy()","metadata":{"execution":{"iopub.status.busy":"2023-10-31T19:41:59.570463Z","iopub.execute_input":"2023-10-31T19:41:59.571311Z","iopub.status.idle":"2023-10-31T19:41:59.749873Z","shell.execute_reply.started":"2023-10-31T19:41:59.571241Z","shell.execute_reply":"2023-10-31T19:41:59.74852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.Label.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-10-31T19:42:09.539862Z","iopub.execute_input":"2023-10-31T19:42:09.540302Z","iopub.status.idle":"2023-10-31T19:42:09.56243Z","shell.execute_reply.started":"2023-10-31T19:42:09.540241Z","shell.execute_reply":"2023-10-31T19:42:09.561065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Images Example\ntrain_images_dir = '../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_train/'\n","metadata":{"execution":{"iopub.status.busy":"2023-10-31T19:42:20.634648Z","iopub.execute_input":"2023-10-31T19:42:20.63513Z","iopub.status.idle":"2023-10-31T19:42:20.640424Z","shell.execute_reply.started":"2023-10-31T19:42:20.635049Z","shell.execute_reply":"2023-10-31T19:42:20.638896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = [f for f in tqdm(listdir(train_images_dir)) if isfile(join(train_images_dir, f))]","metadata":{"execution":{"iopub.status.busy":"2023-10-31T19:44:15.408886Z","iopub.execute_input":"2023-10-31T19:44:15.409352Z","iopub.status.idle":"2023-10-31T20:26:34.441682Z","shell.execute_reply.started":"2023-10-31T19:44:15.409279Z","shell.execute_reply":"2023-10-31T20:26:34.439358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images_dir = '../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/stage_2_test/'\ntest_images = [f for f in tqdm(listdir(test_images_dir)) if isfile(join(test_images_dir, f))]","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:26:49.645136Z","iopub.execute_input":"2023-10-31T20:26:49.645779Z","iopub.status.idle":"2023-10-31T20:34:19.76377Z","shell.execute_reply.started":"2023-10-31T20:26:49.645651Z","shell.execute_reply":"2023-10-31T20:34:19.762574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number of train images:', len(train_images))\nprint('Number of test images:', len(test_images))","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:49:23.720402Z","iopub.execute_input":"2023-10-31T20:49:23.720921Z","iopub.status.idle":"2023-10-31T20:49:23.728107Z","shell.execute_reply.started":"2023-10-31T20:49:23.720841Z","shell.execute_reply":"2023-10-31T20:49:23.726775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4\"></a> <br>\n## 4- Check images","metadata":{}},{"cell_type":"markdown","source":"## Overview of DICOM files and medical images\nMedical images are stored in a special format known as DICOM files (*.dcm). They contain a combination of header metadata as well as underlying raw image arrays for pixel data. In Python, one popular library to access and manipulate DICOM files is the pydicom module. To use the pydicom library, first find the DICOM file for a given patientId by simply looking for the matching file in the stage_1_train_images/ folder, and the use the pydicom.read_file() method to load the data:","metadata":{}},{"cell_type":"code","source":"fig=plt.figure(figsize=(15, 10))\ncolumns = 5; rows = 4\nfor i in range(1, columns*rows +1):\n    ds = pydicom.dcmread(train_images_dir + train_images[i])\n    fig.add_subplot(rows, columns, i)\n    plt.imshow(ds.pixel_array, cmap=plt.cm.bone)\n    fig.add_subplot","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:49:23.734058Z","iopub.execute_input":"2023-10-31T20:49:23.734554Z","iopub.status.idle":"2023-10-31T20:49:27.185129Z","shell.execute_reply.started":"2023-10-31T20:49:23.734452Z","shell.execute_reply":"2023-10-31T20:49:27.184164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\nprint(ds) # this is file type of image","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:49:27.187191Z","iopub.execute_input":"2023-10-31T20:49:27.187774Z","iopub.status.idle":"2023-10-31T20:49:27.194618Z","shell.execute_reply.started":"2023-10-31T20:49:27.187719Z","shell.execute_reply":"2023-10-31T20:49:27.193813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = ds.pixel_array\nprint(type(im))\nprint(im.dtype)\nprint(im.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:49:27.196165Z","iopub.execute_input":"2023-10-31T20:49:27.196727Z","iopub.status.idle":"2023-10-31T20:49:27.20889Z","shell.execute_reply.started":"2023-10-31T20:49:27.196674Z","shell.execute_reply":"2023-10-31T20:49:27.208001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"5\"></a> <br>\n## 5- Visualization of data","metadata":{}},{"cell_type":"code","source":"pylab.imshow(im, cmap=pylab.cm.gist_gray)\npylab.axis('on')","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:49:27.210278Z","iopub.execute_input":"2023-10-31T20:49:27.21073Z","iopub.status.idle":"2023-10-31T20:49:27.428952Z","shell.execute_reply.started":"2023-10-31T20:49:27.21068Z","shell.execute_reply":"2023-10-31T20:49:27.427776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.Label.value_counts())\nsns.countplot(x='Label', data=train)","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:49:27.433509Z","iopub.execute_input":"2023-10-31T20:49:27.434181Z","iopub.status.idle":"2023-10-31T20:49:28.22638Z","shell.execute_reply.started":"2023-10-31T20:49:27.434085Z","shell.execute_reply":"2023-10-31T20:49:28.224997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"6\"></a> <br>\n## 6- Working newTable ","metadata":{}},{"cell_type":"code","source":"train['Sub_type'] = train['ID'].str.split(\"_\", n = 3, expand = True)[2]\ntrain['PatientID'] = train['ID'].str.split(\"_\", n = 3, expand = True)[1]\n","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:49:28.230922Z","iopub.execute_input":"2023-10-31T20:49:28.231416Z","iopub.status.idle":"2023-10-31T20:50:17.604898Z","shell.execute_reply.started":"2023-10-31T20:49:28.231343Z","shell.execute_reply":"2023-10-31T20:50:17.603967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:50:17.607782Z","iopub.execute_input":"2023-10-31T20:50:17.608659Z","iopub.status.idle":"2023-10-31T20:50:17.622728Z","shell.execute_reply.started":"2023-10-31T20:50:17.608188Z","shell.execute_reply":"2023-10-31T20:50:17.621782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(20, 8))\n\nsns.countplot(x=\"Sub_type\", hue=\"Label\", data=train)\n\nplt.title(\"Total Images by Subtype\")","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:50:17.624013Z","iopub.execute_input":"2023-10-31T20:50:17.624498Z","iopub.status.idle":"2023-10-31T20:50:23.11996Z","shell.execute_reply.started":"2023-10-31T20:50:17.62444Z","shell.execute_reply":"2023-10-31T20:50:23.11867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=\"Label\", hue=\"Sub_type\", data=train)","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:50:23.121817Z","iopub.execute_input":"2023-10-31T20:50:23.122258Z","iopub.status.idle":"2023-10-31T20:50:27.318283Z","shell.execute_reply.started":"2023-10-31T20:50:23.12218Z","shell.execute_reply":"2023-10-31T20:50:27.317138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subtype_counts = train.groupby(\"Sub_type\").Label.value_counts().unstack()\nsubtype_counts = subtype_counts.loc[:, 1] / train.groupby(\"Sub_type\").size() * 100\nsubtype_counts","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:50:27.319885Z","iopub.execute_input":"2023-10-31T20:50:27.320261Z","iopub.status.idle":"2023-10-31T20:50:32.530595Z","shell.execute_reply.started":"2023-10-31T20:50:27.320197Z","shell.execute_reply":"2023-10-31T20:50:32.529754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf=train.copy()\ntraindf[['ID', 'Image', 'Diagnosis']] = traindf['ID'].str.split('_', expand=True)\ntraindf = traindf[['Image', 'Diagnosis', 'Label']]\ntraindf.drop_duplicates(inplace=True)\ntraindf = traindf.pivot(index='Image', columns='Diagnosis', values='Label').reset_index()\ntraindf['Image'] = 'ID_' + traindf['Image']\ntraindf.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:50:32.532061Z","iopub.execute_input":"2023-10-31T20:50:32.532684Z","iopub.status.idle":"2023-10-31T20:51:21.846754Z","shell.execute_reply.started":"2023-10-31T20:50:32.532614Z","shell.execute_reply":"2023-10-31T20:51:21.845693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Look at the bad cases\nLet's count images in which there are multiple hemorrhages detected.","metadata":{}},{"cell_type":"code","source":"for n in range(6):\n    many = traindf[traindf[['epidural', 'intraparenchymal', 'intraventricular', 'subarachnoid', 'subdural']].sum(1) == n].copy()\n    print('Number of hemorrhages: {}, amount of such images: {}, fraction: {:.3f}%'.format(n, len(many), 100 * len(many) / len(traindf)))","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:51:21.848587Z","iopub.execute_input":"2023-10-31T20:51:21.848983Z","iopub.status.idle":"2023-10-31T20:51:22.168382Z","shell.execute_reply.started":"2023-10-31T20:51:21.848917Z","shell.execute_reply":"2023-10-31T20:51:22.16704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"multi_target_count = train.groupby(\"ID\").Label.sum()\nfig, ax = plt.subplots(1,1,figsize=(20,5))\nsns.barplot(x=subtype_counts.index, y=subtype_counts.values, ax=ax, palette=\"Set2\")\nplt.xticks(rotation=45); \nax.set_title(\"How much binary imbalance do we have?\")\nax.set_ylabel(\"% of positive occurences (1)\");\nplt.show() ","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:51:22.17101Z","iopub.execute_input":"2023-10-31T20:51:22.171563Z","iopub.status.idle":"2023-10-31T20:51:43.250821Z","shell.execute_reply.started":"2023-10-31T20:51:22.171465Z","shell.execute_reply":"2023-10-31T20:51:43.249844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Hounsfield units\n\n- Hounsfield units are a measurement to describe radiodensity.\n  \n- Different tissues have different HUs\n  \n- Our eye can only detect ~6% change in greyscale (16 shades of grey)\n  \n- Given 2000 HU of one image (-1000 to 1000), this means that 1 greyscale covers 8 HUs.\n  \n- Consequently there can happen a change of 120 HUs unit our eye is able to detect an intensity change in the image.\n  \n- The example of a hemorrhage in the brain shows relevant HUs in the range of 8-70. We won't be able to see important changes in the intensi y to detect the hemorrhage.\n  \n- This is the reason why we have to focus 256 shades of grey into a small range/window of HU units. (WINDOW)\n  \n- The level means where this window is centered.","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(2,1,figsize=(20,10))\nfor file in train_images[0:10]:\n    dataset = pydicom.dcmread(train_images_dir + file)\n    image = dataset.pixel_array.flatten()\n    rescaled_image = image * dataset.RescaleSlope + dataset.RescaleIntercept\n    sns.distplot(image.flatten(), ax=ax[0]);\n    sns.distplot(rescaled_image.flatten(), ax=ax[1])\nax[0].set_title(\"Raw pixel array distributions for 10 examples\")\nax[1].set_title(\"HU unit distributions for 10 examples\");","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:51:43.252455Z","iopub.execute_input":"2023-10-31T20:51:43.252807Z","iopub.status.idle":"2023-10-31T20:51:47.621112Z","shell.execute_reply.started":"2023-10-31T20:51:43.252747Z","shell.execute_reply":"2023-10-31T20:51:47.619981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Insights\n- We observe cases with -1000 and -2000\n- Nonetheless the mode is located at 0 for all example raw pixel value arrays. This is likely to correspond to air. Consequently our raw pixel values are not given as HU unit.\n- But we can transform the image to HU units by scaling with the slope and intercept.\n- Then we can see that mode is located at -1000 (HU of air).","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(4,10,figsize=(20,12))\n\nfor n in range(10):\n    dataset = pydicom.dcmread(train_images_dir + train_images[n])\n    image = dataset.pixel_array\n    rescaled_image = image * dataset.RescaleSlope + dataset.RescaleIntercept\n    mask2000 = np.where((rescaled_image <= -1500) & (rescaled_image > -2500), 1, 0)\n    mask3000 = np.where(rescaled_image <= -2500, 1, 0)\n    ax[0,n].imshow(rescaled_image)\n    rescaled_image[rescaled_image < -1024] = -1024\n    ax[1,n].imshow(mask2000)\n    ax[2,n].imshow(mask3000)\n    ax[3,n].imshow(rescaled_image)\n    ax[0,n].grid(False)\n    ax[1,n].grid(False)\n    ax[2,n].grid(False)\n    ax[3,n].grid(False)\nax[0,0].set_title(\"Rescaled image\")\nax[1,0].set_title(\"Mask -2000\")\nax[2,0].set_title(\"Mask -3000\");\nax[3,0].set_title(\"Background to air\");","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:51:47.62274Z","iopub.execute_input":"2023-10-31T20:51:47.623076Z","iopub.status.idle":"2023-10-31T20:51:53.019767Z","shell.execute_reply.started":"2023-10-31T20:51:47.623017Z","shell.execute_reply":"2023-10-31T20:51:53.018881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can see that both cases (-2000 and -3000) correspond to the outside region ot cylindrical CT scanners. Consequently we can set these values to -1000 (air in HU) without worries.","metadata":{}},{"cell_type":"markdown","source":"## Pixel Spacing\nWhen browsing through the dicom files, we can see that a value called pixel spacing changes as well!","metadata":{}},{"cell_type":"code","source":"pixelspacing_w = []\npixelspacing_h = []\nspacing_filenames = []\nfor file in train_images[0:1000]:\n    dataset = pydicom.dcmread(train_images_dir + file)\n    spacing = dataset.PixelSpacing\n    pixelspacing_w.append(spacing[0])\n    pixelspacing_h.append(spacing[1])\n    spacing_filenames.append(file)\n\nfig, ax = plt.subplots(1,2,figsize=(20,5))\nsns.distplot(pixelspacing_w, ax=ax[0], color=\"Limegreen\", kde=False)\nax[0].set_title(\"Pixel spacing width \\n distribution\")\nax[0].set_ylabel(\"Frequency\")\nsns.distplot(pixelspacing_h, ax=ax[1], color=\"Mediumseagreen\", kde=False)\nax[1].set_title(\"Pixel spacing height \\n distribution\");\nax[1].set_ylabel(\"Frequency\");","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:51:53.021173Z","iopub.execute_input":"2023-10-31T20:51:53.021655Z","iopub.status.idle":"2023-10-31T20:52:08.930762Z","shell.execute_reply.started":"2023-10-31T20:51:53.021583Z","shell.execute_reply":"2023-10-31T20:52:08.929416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"min_file = spacing_filenames[np.argmin(pixelspacing_w)]\nmax_file = spacing_filenames[np.argmax(pixelspacing_w)]","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:52:08.93266Z","iopub.execute_input":"2023-10-31T20:52:08.933118Z","iopub.status.idle":"2023-10-31T20:52:08.939389Z","shell.execute_reply.started":"2023-10-31T20:52:08.933014Z","shell.execute_reply":"2023-10-31T20:52:08.938213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rescale_pixelarray(dataset):\n    image = dataset.pixel_array\n    rescaled_image = image * dataset.RescaleSlope + dataset.RescaleIntercept\n    rescaled_image[rescaled_image < -1024] = -1024\n    return rescaled_image","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:52:08.941181Z","iopub.execute_input":"2023-10-31T20:52:08.941559Z","iopub.status.idle":"2023-10-31T20:52:08.954809Z","shell.execute_reply.started":"2023-10-31T20:52:08.941493Z","shell.execute_reply":"2023-10-31T20:52:08.953737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2,figsize=(20,10))\n\ndataset_min = pydicom.dcmread(train_images_dir + min_file)\nimage_min = rescale_pixelarray(dataset_min)\n\ndataset_max = pydicom.dcmread(train_images_dir + max_file)\nimage_max = rescale_pixelarray(dataset_max)\n\nax[0].imshow(image_min, cmap=\"Spectral\")\nax[0].set_title(\"Pixel spacing w: \" + str(np.min(pixelspacing_w)))\nax[1].imshow(image_max, cmap=\"Spectral\");\nax[1].set_title(\"Pixel spacing w: \" + str(np.max(pixelspacing_w)))\nax[0].grid(False)\nax[1].grid(False)","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:52:08.956459Z","iopub.execute_input":"2023-10-31T20:52:08.956985Z","iopub.status.idle":"2023-10-31T20:52:09.757982Z","shell.execute_reply.started":"2023-10-31T20:52:08.956929Z","shell.execute_reply":"2023-10-31T20:52:09.75652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Reading in the [docs](https://dicom.innolitics.com/ciods/ct-image/image-plane/00280030):\n\n> All pixel spacing related Attributes are encoded as the physical distance between the centers of each two-dimensional pixel, specified by two numeric values.The first value is the row spacing in mm, that is the spacing between the centers of adjacent rows, or vertical spacing.The second value is the column spacing in mm, that is the spacing between the centers of adjacent columns, or horizontal spacing.\n\nConsequently it's related to the physical distance. Let's understand it given our extreme examples. The pixel spacing yields the mm of physical distance of one pixel. Hence we can compute the overall distance covered by one image width or height:","metadata":{}},{"cell_type":"code","source":"np.min(pixelspacing_w) * 512","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:52:09.759762Z","iopub.execute_input":"2023-10-31T20:52:09.760179Z","iopub.status.idle":"2023-10-31T20:52:09.768381Z","shell.execute_reply.started":"2023-10-31T20:52:09.760107Z","shell.execute_reply":"2023-10-31T20:52:09.767343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This is the true length in mm covered by our image. In my current case (can change as listdir choses file order at random) it's 195 mm meaning 19.5 cm.","metadata":{}},{"cell_type":"code","source":"np.max(pixelspacing_w) * 512","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:52:09.770333Z","iopub.execute_input":"2023-10-31T20:52:09.770712Z","iopub.status.idle":"2023-10-31T20:52:09.785025Z","shell.execute_reply.started":"2023-10-31T20:52:09.77065Z","shell.execute_reply":"2023-10-31T20:52:09.784021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"7\"></a> <br>\n## 7- Visualization of hemorrhage epidural","metadata":{}},{"cell_type":"code","source":"\ndef window_image(img, window_center,window_width, intercept, slope):\n\n    img = (img*slope +intercept)\n    img_min = window_center - window_width//2\n    img_max = window_center + window_width//2\n    img[img<img_min] = img_min\n    img[img>img_max] = img_max\n    return img \n    ","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:52:09.786957Z","iopub.execute_input":"2023-10-31T20:52:09.787361Z","iopub.status.idle":"2023-10-31T20:52:09.796206Z","shell.execute_reply.started":"2023-10-31T20:52:09.787298Z","shell.execute_reply":"2023-10-31T20:52:09.795068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_first_of_dicom_field_as_int(x):\n    #get x[0] as in int is x is a 'pydicom.multival.MultiValue', otherwise get int(x)\n    if type(x) == pydicom.multival.MultiValue:\n        return int(x[0])\n    else:\n        return int(x)\n\ndef get_windowing(data):\n    dicom_fields = [data[('0028','1050')].value, #window center\n                    data[('0028','1051')].value, #window width\n                    data[('0028','1052')].value, #intercept\n                    data[('0028','1053')].value] #slope\n    return [get_first_of_dicom_field_as_int(x) for x in dicom_fields]","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:52:09.798021Z","iopub.execute_input":"2023-10-31T20:52:09.798484Z","iopub.status.idle":"2023-10-31T20:52:09.810705Z","shell.execute_reply.started":"2023-10-31T20:52:09.798392Z","shell.execute_reply":"2023-10-31T20:52:09.809762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_dir\n\ndef view_images(images, title = '', aug = None):\n    width = 5\n    height = 2\n    fig, axs = plt.subplots(height, width, figsize=(15,5))\n    \n    for im in range(0, height * width):\n        ''''\n        image = pydicom.read_file(os.path.join(train_images_dir,'ID_'+images[im]+ '.dcm')).pixel_array\n        i = im // width\n        j = im % width\n        axs[i,j].imshow(image, cmap=plt.cm.bone) \n        axs[i,j].axis('off')'''''\n        \n        data = pydicom.read_file(os.path.join(train_images_dir,'ID_'+images[im]+ '.dcm'))\n        image = data.pixel_array\n        window_center , window_width, intercept, slope = get_windowing(data)\n        image_windowed = window_image(image, window_center, window_width, intercept, slope)\n\n\n        i = im // width\n        j = im % width\n        axs[i,j].imshow(image_windowed, cmap=plt.cm.bone) \n        axs[i,j].axis('off')\n        \n        \n    plt.suptitle(title)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:52:09.812595Z","iopub.execute_input":"2023-10-31T20:52:09.813012Z","iopub.status.idle":"2023-10-31T20:52:09.829571Z","shell.execute_reply.started":"2023-10-31T20:52:09.812881Z","shell.execute_reply":"2023-10-31T20:52:09.82824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"view_images(train[(train['Sub_type'] == 'epidural') & (train['Label'] == 1)][:10].PatientID.values, title = 'Images of hemorrhage epidural')","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:52:09.831711Z","iopub.execute_input":"2023-10-31T20:52:09.832251Z","iopub.status.idle":"2023-10-31T20:52:11.373339Z","shell.execute_reply.started":"2023-10-31T20:52:09.832164Z","shell.execute_reply":"2023-10-31T20:52:11.372162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"8\"></a> <br>\n## 8- Visualization of hemorrhage intraparenchymal","metadata":{}},{"cell_type":"code","source":"view_images(train[(train['Sub_type'] == 'intraparenchymal') & (train['Label'] == 1)][:20].PatientID.values, title = 'Images of hemorrhage intraparenchymal')","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:52:11.375053Z","iopub.execute_input":"2023-10-31T20:52:11.375462Z","iopub.status.idle":"2023-10-31T20:52:12.850021Z","shell.execute_reply.started":"2023-10-31T20:52:11.375387Z","shell.execute_reply":"2023-10-31T20:52:12.848542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"9\"></a> <br>\n## 9- Visualization of hemorrhage intraventricular","metadata":{}},{"cell_type":"code","source":"view_images(train[(train['Sub_type'] == 'intraventricular') & (train['Label'] == 1)][:20].PatientID.values, title = 'Images of hemorrhage intraventricular')","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:52:12.851947Z","iopub.execute_input":"2023-10-31T20:52:12.85254Z","iopub.status.idle":"2023-10-31T20:52:14.297291Z","shell.execute_reply.started":"2023-10-31T20:52:12.852448Z","shell.execute_reply":"2023-10-31T20:52:14.295945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"10\"></a> <br>\n## 10- Visualization of hemorrhage subarachnoid","metadata":{}},{"cell_type":"code","source":"view_images(train[(train['Sub_type'] == 'subarachnoid') & (train['Label'] == 1)][:20].PatientID.values, title = 'Images of hemorrhage subarachnoid')","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:52:14.298912Z","iopub.execute_input":"2023-10-31T20:52:14.29927Z","iopub.status.idle":"2023-10-31T20:52:15.74365Z","shell.execute_reply.started":"2023-10-31T20:52:14.299211Z","shell.execute_reply":"2023-10-31T20:52:15.742206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"11\"></a> <br>\n## 11- Visualization of hemorrhage subdural","metadata":{}},{"cell_type":"code","source":"view_images(train[(train['Sub_type'] == 'subdural') & (train['Label'] == 1)][:20].PatientID.values, title = 'Images of hemorrhage subdural')","metadata":{"execution":{"iopub.status.busy":"2023-10-31T20:52:15.745943Z","iopub.execute_input":"2023-10-31T20:52:15.74645Z","iopub.status.idle":"2023-10-31T20:52:17.333987Z","shell.execute_reply.started":"2023-10-31T20:52:15.746365Z","shell.execute_reply":"2023-10-31T20:52:17.33272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"###### [Go to top](#top)","metadata":{}}]}