{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../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\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 1. RNSA Intracranial Hemorrhage Dataset Overview\n\n#### Intracranial Hemorrhage Types\n![img](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":"#### Dataset Overview\n- Train.csv: include the ID and Label:\n - ID is a combined string that includes the image filename and Hemorrhage type. \n - Label is a target column,  indicating the probability of whether that type of hemorrhage exists in the indicated image. \n   Format:\n   [Image Id]_[Sub-type_Name], as follows:\n   - Id,Label\n   - 1_epidural_hemorrhage,0\n   - 1_intraparenchymal_hemorrhage,0\n   - 1_intraventricular_hemorrhage,0\n   - 1_subarachnoid_hemorrhage,0.6\n   - 1_subdural_hemorrhage,0\n   - 1_any,0.9\n   \n    \n  - DICOM Images:\n   - DICOM is the standard for the communication and management of medical imaging information and related data.\n   - It can be exchanged between two entities that are capable of receiving image and patient data in DICOM format.  \n   - Images contain associated metadata. This will include PatientID, StudyInstanceUID, SeriesInstanceUID, and other features.\n   \n #### Data Files\n\n   - **stage_1_train.csv** - Contains Ids and target information.\n   - **stage_1_train_images.zip** and **stage_1_test_images.zip** - DICOM images -\n\n  \n"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 2. Train.csv Dataset EDA"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/rsna-intracranial-hemorrhage-detection/stage_1_train.csv')\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Training Dataset's shape:\", train_df.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df['Label'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Label Values Overview"},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(train_df.Label)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"####  Analysis the ID\n- Format:[Image Id]_[Sub-type_Name]"},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntrain_df['Sub_type'] = train_df['ID'].str.split(\"_\", n = 3, expand = True)[2]\ntrain_df['Img_file_name'] = train_df['ID'].str.rsplit(\"_\", n =1, expand = True)[0]\ntrain_df.head()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_type_summary = train_df.groupby('Sub_type').sum()\nsub_type_summary","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.set(rc={'figure.figsize':(8,8)})\nplot = sns.barplot(x=sub_type_summary.index, y= sub_type_summary.Label)\n\nplt.xticks(rotation=45)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig=plt.figure(figsize=(8, 8))\n\nsns.countplot(x=\"Sub_type\", hue=\"Label\", data=train_df, palette=\"deep\")\nplt.xticks(rotation=45)\nplt.title(\"Total Images by Subtype\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#train_df.to_csv('subtype_train.csv')\n#from IPython.display import FileLink, FileLinks\n# FileLink('subtype_train.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### DICOM Images Data EDA"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Train/Test Image Files Overview\n\ntrain_imgs = sorted(glob.glob(\"../input/rsna-intracranial-hemorrhage-detection/stage_1_train_images/*.dcm\"))\ntest_imgs = sorted(glob.glob(\"../input/rsna-intracranial-hemorrhage-detection/stage_1_test_images/*.dcm\"))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"total Train DICOM images: \", len(train_imgs))\nprint(\"Total Test DICOM images: \", len(test_imgs))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### DICOM Images Overview"},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_dicom_all=plt.figure(figsize=(15, 15))\nplt.title('DICOM Images Overview')\nplt.rcParams[\"axes.grid\"] = False\ncolumns = 4; rows = 4\nfor i in range(1, columns*rows +1):\n    dicom_img = pydicom.dcmread(train_imgs[i])\n    plot_dicom_all.add_subplot(rows, columns,i)\n    plt.imshow(dicom_img.pixel_array, cmap=plt.cm.bone)\n    plot_dicom_all.add_subplot\n    \n    \n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Get and Plot DICOM metadata"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Meta data structure\npydicom.dcmread(train_imgs[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_dicom_metadata(filename):\n    \"\"\"\n    show_dicom_metadata function is to get all important DICOM metadata, such as windowing parameters and other information and also plot it\n    input parameter:\n    filename: string, DICOM filename\n    \"\"\"\n\n    dataset = pydicom.dcmread(filename)\n    # Normal mode:\n    print()\n    print(\"Filename.........:\", filename)\n    print(\"Storage type.....:\", dataset['SOPInstanceUID'])\n    print(\"Patient id.......:\", dataset.PatientID)\n    print(\"Modality.........:\", dataset.Modality)\n    print()\n    print(\"Window Center.........:\", dataset.WindowCenter)\n    print('Window Width.........:',dataset.WindowWidth)\n    print('Rescale Intercept.........:',dataset.RescaleIntercept)\n    print('Rescale Slope.........:',dataset.RescaleSlope)\n\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\n    # use .get() if not sure the item exists, and want a default value if missing\n    print(\"Slice location...:\", dataset.get('SliceLocation', \"(missing)\"))\n\n    # plot the image using matplotlib\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_dicom_metadata(train_imgs[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def 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\ndef 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]\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_path = '../input/rsna-intracranial-hemorrhage-detection/'\nstage_1_train_images_path  = '../input/rsna-intracranial-hemorrhage-detection/stage_1_train_images/'\n\n\n\ndef display_dicom_image(df, sub_type, column_number,row_number):\n    \n    \"\"\"\n    display_dicom_image function shows the DICOM imgae from the training dataset dataframe.\n    df: data frame that includes the images and subtype information\n    sub_type: string, what sub_type want to show\n    column_number: int, how many images in a row\n    row_number: int, how many rows want to show\n\n    \"\"\"\n    # print(sub_type)\n    if sub_type not in ['any','epidural','intraparenchymal','intraventricular','subarachnoid','subdural']:\n        print('No this Type:',sub_type)\n        return   \n    \n    images = df[(df['Sub_type'] == sub_type) & (train_df['Label'] == 1)][:(column_number*row_number)].Img_file_name.values\n    \n    fig, axs = plt.subplots(row_number, column_number, figsize=(15,15))\n    \n    \n    for im in range(0, column_number*row_number):\n        # print(images[im])\n        # print(os.path.join(stage_1_train_images_path,images[im]+ '.dcm'))\n        data = pydicom.read_file(os.path.join(stage_1_train_images_path,images[im]+ '.dcm'))\n        \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 // column_number\n        j = im % column_number\n        axs[i,j].imshow(image_windowed, cmap=plt.cm.bone) \n        axs[i,j].axis('off')\n        \n       \n    plt.suptitle('Images of Hemorrhage Sub-type:' + sub_type )\n    plt.show()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Sub_type: Any"},{"metadata":{"trusted":true},"cell_type":"code","source":"display_dicom_image(train_df, 'any', 4,4)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Sub_Type: intraventricular"},{"metadata":{"trusted":true},"cell_type":"code","source":"display_dicom_image(train_df, 'intraventricular', 4,4)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Sub_Type: epidural"},{"metadata":{"trusted":true},"cell_type":"code","source":"display_dicom_image(train_df, 'epidural', 4,4)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Sub_Type: subarachnoid"},{"metadata":{"trusted":true},"cell_type":"code","source":"display_dicom_image(train_df, 'subarachnoid', 4,4)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Sub_Type:intraventricular"},{"metadata":{"trusted":true},"cell_type":"code","source":"display_dicom_image(train_df, 'intraventricular', 4,4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}