{"cells":[{"metadata":{},"cell_type":"markdown","source":"https://www.kaggle.com/c/rsna-intracranial-hemorrhage-detection/overview/hemorrhage-types\n\n# Hemorrhage Types\n\nHemorrhage in the head (intracranial hemorrhage) is a relatively common condition that has many causes ranging from trauma, stroke, aneurysm, vascular malformations, high blood pressure, illicit drugs and blood clotting disorders. The neurologic consequences also vary extensively depending upon the size, type of hemorrhage and location ranging from headache to death. The role of the Radiologist is to detect the hemorrhage, characterize the hemorrhage subtype, its size and to determine if the hemorrhage might be jeopardizing critical areas of the brain that might require immediate surgery.\n\nWhile all acute (i.e. new) hemorrhages appear dense (i.e. white) on computed tomography (CT), the primary imaging features that help Radiologists determine the subtype of hemorrhage are the location, shape and proximity to other structures (see table).\n\nIntraparenchymal hemorrhage is blood that is located completely within the brain itself; intraventricular or subarachnoid hemorrhage is blood that has leaked into the spaces of the brain that normally contain cerebrospinal fluid (the ventricles or subarachnoid cisterns). Extra-axial hemorrhages are blood that collects in the tissue coverings that surround the brain (e.g. subdural or epidural subtypes). ee figure.) Patients may exhibit more than one type of cerebral hemorrhage, which c may appear on the same image. While small hemorrhages are less morbid than large hemorrhages typically, even a small hemorrhage can lead to death because it is an indicator of another type of serious abnormality (e.g. cerebral aneurysm). \n\n![Types](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F603584%2F56162e47358efd77010336a373beb0d2%2Fsubtypes-of-hemorrhage.png?generation=1568657910458946&alt=media)"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport warnings\nwarnings.filterwarnings('ignore')\n\nprint(os.listdir(\"../input/rsna-intracranial-hemorrhage-detection\"))\nimport glob\n\nimport pydicom\n\nfrom matplotlib import cm\nfrom matplotlib import pyplot as plt\n\nfrom keras.models import Model\nfrom keras.layers import Input\nfrom keras.layers.convolutional import Conv2D, Conv2DTranspose\nfrom keras.layers.pooling import MaxPooling2D\nfrom keras.layers.merge import concatenate\nfrom keras import backend as K\n\nimport tensorflow as tf\n\nfrom tqdm import tqdm_notebook\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# What is DICOM?\n\nDicom is a format that has metadata, as well as Pixeldata attached to it. Below I extract some basic info with an image. You will know about the gender and age of the patient, as well as info how the image is sampled and generated. Quite useful to programatically read. Here's the Wikipedia article for it.\nFrom - https://www.kaggle.com/jesperdramsch/intro-chest-xray-dicom-viz-u-nets-full-data"},{"metadata":{"trusted":true},"cell_type":"code","source":"path = \"../input/rsna-intracranial-hemorrhage-detection\"\ndataset = pydicom.dcmread(\"../input/rsna-intracranial-hemorrhage-detection/stage_1_train_images/ID_231d901c1.dcm\")\nprint(\"Finding the details that can be fetched from a dcm file\")\nprint(\"\")\nprint(dataset)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**We dont have information on patient's age, sex, etc. which could have been some helpful features.**"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# https://www.kaggle.com/jesperdramsch/intro-chest-xray-dicom-viz-u-nets-full-data\n\ndef show_dcm_info(dataset):\n    print(\"Storage type.....:\", dataset.SOPInstanceUID)\n    print()\n\n    print(\"Photometric.........:\", dataset.PhotometricInterpretation)\n    print(\"Patient id..........:\", dataset.PatientID)\n    print(\"Modality............:\", dataset.Modality)\n    print(\"Image Position......:\", dataset.ImagePositionPatient)\n    print(\"Image Orient........:\", dataset.ImageOrientationPatient)\n    \n    if 'PixelData' in dataset:\n        rows = int(dataset.Rows)\n        cols = int(dataset.Columns)\n        print(\"Image size.......: {rows:d} x {cols:d}, {size:d} bytes\".format(\n            rows=rows, cols=cols, size=len(dataset.PixelData)))\n        if 'PixelSpacing' in dataset:\n            print(\"Pixel spacing....:\", dataset.PixelSpacing)\n\ndef plot_pixel_array(dataset, figsize=(10,10)):\n    plt.figure(figsize=figsize)\n    plt.imshow(dataset.pixel_array, cmap=plt.cm.bone)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_dcm_info(dataset)\nplot_pixel_array(dataset)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"start = 5   # Starting index of images\nnum_img = 10 # Total number of images to show\n\nfig, ax = plt.subplots(nrows=1, ncols=num_img, sharey=True, figsize=(num_img*10,10))\nfor q, file_path in enumerate(glob.glob('../input/rsna-intracranial-hemorrhage-detection/stage_1_train_images/*.dcm')[start:start+num_img]):\n    dataset = pydicom.dcmread(file_path)\n    #show_dcm_info(dataset)\n    ax[q].imshow(dataset.pixel_array, cmap=plt.cm.bone)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(path + '/stage_1_train.csv')\ntest_df = pd.read_csv(path + '/stage_1_sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Train -', len(train_df))\nprint('Test -', len(test_df))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head(12)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Patients go through six tests **(ID_63eb1e259_epidural, ID_63eb1e259_intraparenchymal, ID_63eb1e259_intraventricular, ID_63eb1e259_subarachnoid, ID_63eb1e259_subdural, ID_63eb1e259_any)** and the results are tabulated as boolean values."},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df[['PID','Test']] = train_df.ID.str.rsplit(\"_\", n=1, expand=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"epidural = train_df[train_df.Test == 'epidural']\nintraparenchymal = train_df[train_df.Test == 'intraparenchymal']\nintraventricular = train_df[train_df.Test == 'intraventricular']\nsubarachnoid = train_df[train_df.Test == 'subarachnoid']\nsubdural = train_df[train_df.Test == 'subdural']\nanyy = train_df[train_df.Test == 'any']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('EPIDURAL')\ndisplay(epidural['Label'].value_counts())\nprint('INTRAPARENCHYMAL')\ndisplay(intraparenchymal['Label'].value_counts())\nprint('INTRAVENTRICULAR')\ndisplay(intraventricular['Label'].value_counts())\nprint('SUBARACHNOID')\ndisplay(subarachnoid['Label'].value_counts())\nprint('SUBDURAL')\ndisplay(subdural['Label'].value_counts())\nprint(\"ANY\")\ndisplay(anyy['Label'].value_counts())","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}