{"cells":[{"metadata":{},"cell_type":"markdown","source":"# RSNA Intracranial Hemorrhage Detection "},{"metadata":{},"cell_type":"markdown","source":"<b>Competition Overview</b><br/><br/>\nIntracranial hemorrhage, bleeding that occurs inside the cranium, is a serious health problem requiring rapid and often intensive medical treatment. For example, intracranial hemorrhages account for approximately 10% of strokes in the U.S., where stroke is the fifth-leading cause of death. Identifying the location and type of any hemorrhage present is a critical step in treating the patient.\n\nDiagnosis requires an urgent procedure. When a patient shows acute neurological symptoms such as severe headache or loss of consciousness, highly trained specialists review medical images of the patient’s cranium to look for the presence, location and type of hemorrhage. The process is complicated and often time consuming."},{"metadata":{},"cell_type":"markdown","source":"<b>What am i predicting?</b><br/><br/>\nIn this competition our goal is to predict intracranial hemorrhage and its subtypes. Given an image the we need to predict probablity of each subtype. This indicates its a multilabel classification problem."},{"metadata":{},"cell_type":"markdown","source":"<b>Intracranial Hemorrhage</b>"},{"metadata":{},"cell_type":"markdown","source":"<img src=\"https://www.technology.org/texorgwp/wp-content/uploads/2019/09/Intracerebral_hemorrhage.jpg\"  style=\"height:30%;width:50%\"/>"},{"metadata":{},"cell_type":"markdown","source":"An intracranial hemorrhage is a type of bleeding that occurs inside the skull. Symptoms include sudden tingling, weakness, numbness, paralysis, severe headache, difficulty with swallowing or vision, loss of balance or coordination, difficulty understanding, speaking , reading, or writing, and a change in level of consciousness or alertness, marked by stupor, lethargy, sleepiness, or coma. Any type of bleeding inside the skull or brain is a medical emergency. It is important to get the person to a hospital emergency room immediately to determine the cause of the bleeding and begin medical treatment."},{"metadata":{},"cell_type":"markdown","source":"<b>Types</b>\n<br/>\n1. Epidural\n2. Intraparenchymal    \n3. Intraventricular\n4. Subarachnoid \n5. Subdural\n6. Any"},{"metadata":{},"cell_type":"markdown","source":"### 1. Epidural"},{"metadata":{},"cell_type":"markdown","source":"<img src=\"https://upload.wikimedia.org/wikipedia/commons/f/f9/EpiduralHematoma.jpg\" style='width:20%;height:20%'/>"},{"metadata":{},"cell_type":"markdown","source":"A 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.\n\nSource: https://www.healthline.com/health/extradural-hemorrhage#type"},{"metadata":{},"cell_type":"markdown","source":"### 2. Intraparenchymal"},{"metadata":{},"cell_type":"markdown","source":"<img src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/e/ed/Mri041a2.jpg/220px-Mri041a2.jpg\" />"},{"metadata":{},"cell_type":"markdown","source":"Intraparenchymal hemorrhage (IPH) is one form of intracerebral bleeding in which there is bleeding within brain parenchyma. The other form is intraventricular hemorrhage (IVH). Intraparenchymal hemorrhage accounts for approx. 8-13% of all strokes and results from a wide spectrum of disorders."},{"metadata":{},"cell_type":"markdown","source":"### 3. Intraventricular"},{"metadata":{},"cell_type":"markdown","source":"<img src='https://prod-images.static.radiopaedia.org/images/12380293/17abfd31d98cfe599e44fe3053db9e_thumb.jpg' />"},{"metadata":{},"cell_type":"markdown","source":"Intraventricular hemorrhage (IVH), also known as intraventricular bleeding, is a bleeding into the brain's ventricular system, where the cerebrospinal fluid is produced and circulates through towards the subarachnoid space. It can result from physical trauma or from hemorrhaging in stroke."},{"metadata":{},"cell_type":"markdown","source":"### 4. Subarachnoid"},{"metadata":{},"cell_type":"markdown","source":"<img src=\"https://www.nejm.org/na101/home/literatum/publisher/mms/journals/content/nejm/2017/nejm_2017.377.issue-3/nejmcp1605827/20180122/images/img_medium/nejmcp1605827_f2.jpeg\" style=\"width:25%;height:25%\"/>"},{"metadata":{},"cell_type":"markdown","source":"Subarachnoid hemorrhage (SAH) is a life-threatening type of stroke caused by bleeding into the space surrounding the brain. SAH can be caused by a ruptured aneurysm, AVM, or head injury. One-third of patients will survive with good recovery; one-third will survive with a disability; and one-third will die. Treatment focuses on stopping the bleeding, restoring normal blood flow, and preventing vasospasm."},{"metadata":{},"cell_type":"markdown","source":"### 5. Subdural"},{"metadata":{},"cell_type":"markdown","source":"<img src=\"https://cdn.britannica.com/51/193251-050-E254065D/CT-scan-brain-hematoma.jpg\" style=\"width:25%;height:25%\"/>"},{"metadata":{},"cell_type":"markdown","source":"A subdural hemorrhage (or hematoma) is a type of bleeding that often occurs outside the brain as a result of a severe head injury. It takes place when blood vessels burst between the brain and the leather-like membrane that wraps around the brain (the dura mater)."},{"metadata":{},"cell_type":"markdown","source":"<b>Note</b>\nA nice video explaining all the types can be found [here](https://www.youtube.com/watch?v=Kb_wzb7-rvE). "},{"metadata":{},"cell_type":"markdown","source":"### Basic EDA"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport pydicom\nimport cv2\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"input_folder = '../input/rsna-intracranial-hemorrhage-detection/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path_train_img = input_folder + 'stage_1_train_images/'\npath_test_img = input_folder + 'stage_1_test_images/'","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Loading Data"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(input_folder + 'stage_1_train.csv')\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# extract subtype\ntrain_df['sub_type'] = train_df['ID'].apply(lambda x: x.split('_')[-1])\n# extract filename\ntrain_df['file_name'] = train_df['ID'].apply(lambda x: '_'.join(x.split('_')[:2]) + '.dcm')\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Number of train images availabe:\", len(os.listdir(path_train_img)))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Epidural"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df[train_df['sub_type'] == 'epidural']['Label'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(x='Label', data=train_df[train_df['sub_type'] == 'epidural'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Comments**<br/><br/>\nFor epidural sub type we have 6,71,501 images labeled as 0 and 2,761 labelled as 1."},{"metadata":{},"cell_type":"markdown","source":"### Intraparenchymal"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df[train_df['sub_type'] == 'intraparenchymal']['Label'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(x='Label', data=train_df[train_df['sub_type'] == 'intraparenchymal'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"**Comments**<br/><br/>\nFor intraparenchymal sub type we have 6,41,698 images labeled as 0 and 32,564 labelled as 1."},{"metadata":{},"cell_type":"markdown","source":"### Intraventricular"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df[train_df['sub_type'] == 'intraventricular']['Label'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(x='Label', data=train_df[train_df['sub_type'] == 'intraventricular'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Comments**<br/><br/>\nFor intraparenchymal sub type we have 6,50,496 images labeled as 0 and 23,766 labelled as 1."},{"metadata":{},"cell_type":"markdown","source":"### Subarachnoid"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df[train_df['sub_type'] == 'subarachnoid']['Label'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(x='Label', data=train_df[train_df['sub_type'] == 'subarachnoid'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Comments**<br/><br/>\nFor subarachnoid sub type we have 6,42,140 images labeled as 0 and 32,122 labelled as 1."},{"metadata":{},"cell_type":"markdown","source":"### Subdural"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df[train_df['sub_type'] == 'subdural']['Label'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(x='Label', data=train_df[train_df['sub_type'] == 'subdural'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Comments**<br/><br/>\nFor Subdural sub type we have 6,31,766 images labeled as 0 and 42,496 labelled as 1."},{"metadata":{},"cell_type":"markdown","source":"### Any"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df[train_df['sub_type'] == 'any']['Label'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(x='Label', data=train_df[train_df['sub_type'] == 'any'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Comments**<br/><br/>\nFor any sub type we have 5,77,159 images labeled as 0 and 97,103 labelled as 1."},{"metadata":{},"cell_type":"markdown","source":"### Final Dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_final_df = pd.pivot_table(train_df.drop(columns='ID'), index=\"file_name\", \\\n                                columns=\"sub_type\", values=\"Label\")\ntrain_final_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(16, 6))\n\ngraph = sns.countplot(x=\"sub_type\", hue=\"Label\", data=(train_df))\ngraph.set_xticklabels(graph.get_xticklabels(),rotation=90)\nplt.show()","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}