{"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\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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## PACKAGES"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport pydicom as dcm\nimport cv2\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(\"../input/rsna-str-pulmonary-embolism-detection/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"DataFrame Shape: \", train_df.shape)\n\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"def window_image(img, window_center,window_width, intercept, slope, rescale=True):\n    img = (img*slope +intercept) #for translation adjustments given in the dicom file. \n    img_min = window_center - window_width//2 #minimum HU level\n    img_max = window_center + window_width//2 #maximum HU level\n    img[img<img_min] = img_min #set img_min for all HU levels less than minimum HU level\n    img[img>img_max] = img_max #set img_max for all HU levels higher than maximum HU level\n    if rescale: \n        img = (img - img_min) / (img_max - img_min)*255.0 \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) == dcm.multival.MultiValue: return int(x[0])\n    else: 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]","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"def view_images(files, width , height, title = '', aug = None, windowing = True, rescale= False):\n    fig, axs = plt.subplots(height, width, figsize=(15,15))\n    \n    for im in range(0, height * width):\n        data = dcm.dcmread(files[im])\n        image = data.pixel_array\n        window_center , window_width, intercept, slope = get_windowing(data)\n        if windowing:\n            output = window_image(image, window_center, window_width, intercept, slope, rescale = rescale)\n        else:\n            output = image\n        i = im // width\n        j = im % width\n        if width == 1 and height == 1:\n            axs.imshow(output, cmap=plt.cm.gray) \n            axs.axis('off')\n        else:\n            axs[i, j].imshow(output, cmap=plt.cm.gray) \n            axs[i, j].axis('off')\n    plt.suptitle(title)\n    plt.tight_layout()\n    plt.show()\n    \n\nroot_path = \"../input/rsna-str-pulmonary-embolism-detection/train/\"\nexample_files = [root_path + \"0003b3d648eb/d2b2960c2bbf/00ac73cfc372.dcm\",\n                 root_path + \"0003b3d648eb/d2b2960c2bbf/03d7693b0405.dcm\",\n                root_path + \"0003b3d648eb/d2b2960c2bbf/055eabedd904.dcm\", \n                root_path + \"0003b3d648eb/d2b2960c2bbf/084e7b3d3d6c.dcm\"]\nview_images(example_files,2, 2, 'Images with Windowing')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def sample(train_df, row, nosamples):\n    t = train_df.values\n    t = t[:, 3:]\n    filterd_smaple = train_df.iloc[np.where(list(map(lambda x : all(x), t[:, :] == t[row, :])))]\n    samples = filterd_smaple.groupby(['StudyInstanceUID', 'SeriesInstanceUID']).first().reset_index().values\n    np.random.shuffle(samples)\n    samples = samples[np.random.randint(0, samples.shape[0], (nosamples, 1))]\n    return np.squeeze(samples)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"samples = sample(train_df, 0, 10)\nsamples = list(map(lambda x : root_path + \"/\".join(x) +\".dcm\", samples[:, :3]))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"```\npe_present_on_image         0 \nnegative_exam_for_pe        0\nqa_motion                   0\nqa_contrast                 0\nflow_artifact               0\nrv_lv_ratio_gte_1           0\nrv_lv_ratio_lt_1            1\nleftsided_pe                1 \nchronic_pe                  0\ntrue_filling_defect_not_pe  0\nrightsided_pe               1\nacute_and_chronic_pe        0\ncentral_pe                  0\nacute_pe                    1\nindeterminate               0\n```"},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"view_images(samples[:4],2, 2, 'Images with Windowing')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for col in train_df.columns[3:]:\n    plt.bar(['0','1'], train_df[col].value_counts().values)\n    plt.title(col)\n    plt.xlabel('Class')\n    plt.ylabel('Count')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Feature Exploration"},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.applications.vgg16 import preprocess_input\nimport matplotlib.animation as animation\nfrom IPython.display import HTML","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"feature_extractor = tf.keras.applications.VGG16(include_top = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"input_layer = feature_extractor.input\n\noutput_layer = [out.output for out in feature_extractor.layers[1:]]\nfeatur_extraction_model = tf.keras.Model(inputs = input_layer, outputs = output_layer)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def decode_dcm(file, rescale):\n    data = dcm.dcmread(file)\n    image = data.pixel_array\n    window_center , window_width, intercept, slope = get_windowing(data)\n    output = window_image(image, window_center, window_width, intercept, slope, rescale = rescale)\n    return output","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"file = example_files[0]\nimage = decode_dcm(file, True)\nimage = np.repeat(image[..., np.newaxis], 3, -1)\nimage = np.expand_dims(image, axis = 0)\npreprocessed_image = preprocess_input(image)\n\nprint(\"Image Shape              : \", image.shape)\nprint(\"PreProcessed Image Shape : \", preprocessed_image.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%time\npredictions = featur_extraction_model.predict(image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def layer_features(predictions, layer_idx):\n    fig = plt.figure(figsize = (8, 8))\n    img_list = []\n    for pred in range(predictions[layer_idx].shape[-1]):\n        img = plt.imshow(predictions[layer_idx][0, :, :, pred], cmap='gray', animated=True)\n        img_list.append([img])\n\n    ani = animation.ArtistAnimation(fig, img_list, interval=predictions[layer_idx].shape[-1] * 10, blit=True,\n                                    repeat_delay=1000)\n    return ani\n\nani = layer_features(predictions, 0)\nHTML(ani.to_html5_video())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ani = layer_features(predictions, 1)\nHTML(ani.to_html5_video())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ani = layer_features(predictions, 2)\nHTML(ani.to_html5_video())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ani = layer_features(predictions, 3)\nHTML(ani.to_html5_video())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"file = example_files[0]\nimage = decode_dcm(file, True)\nimage = cv2.resize(image, (224, 224))\nimage = np.repeat(image[..., np.newaxis], 3, -1)\nimage = np.expand_dims(image, axis = 0)\npreprocessed_image = preprocess_input(image)\n\npredictions2 = featur_extraction_model.predict(image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f, ax = plt.subplots(2, 2, figsize=(15, 15))\nax[0, 0].imshow(predictions2[0][0, :, :, 2], cmap = 'gray')\nax[0,0].set_title('Feature of Image with resize')\nax[0, 1].imshow(predictions[0][0, :, :, 2], cmap = 'gray')\nax[0, 1].set_title('Feature of Image without resize')\nax[1, 0].imshow(image[0, :, :, 0], cmap = 'gray')\nax[1, 0].set_title('Original Image with resize')\nax[1, 1].imshow(decode_dcm(file, True), cmap = 'gray')\nax[1, 1].set_title('Original Image Without resize')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f, ax = plt.subplots(2, 2, figsize=(15, 15))\nax[0, 0].imshow(predictions2[5][0, :, :, 2], cmap = 'gray')\nax[0,0].set_title('Feature of Image with resize at layer_5')\nax[0, 1].imshow(predictions[5][0, :, :, 2], cmap = 'gray')\nax[0, 1].set_title('Feature of Image without resize at layer_5')\nax[1, 0].imshow(predictions[6][0, :, :, 2], cmap = 'gray')\nax[1, 0].set_title('Feature of Image without resize at layer_6')\nax[1, 1].imshow(predictions[7][0, :, :, 2], cmap = 'gray')\nax[1, 1].set_title('Feature of Image without resize at layer_7')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f, ax = plt.subplots(2, 2, figsize=(15, 15))\nax[0, 0].imshow(predictions2[5][0, :, :, 2], cmap = 'gray')\nax[0,0].set_title('Feature of Image with resize at layer_5')\nax[0, 1].imshow(predictions[8][0, :, :, 2], cmap = 'gray')\nax[0, 1].set_title('Feature of Image without resize at layer_8')\nax[1, 0].imshow(predictions[9][0, :, :, 2], cmap = 'gray')\nax[1, 0].set_title('Feature of Image without resize at layer_9')\nax[1, 1].imshow(predictions[10][0, :, :, 2], cmap = 'gray')\nax[1, 1].set_title('Feature of Image without resize at layer_10')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.groupby(['StudyInstanceUID', 'SeriesInstanceUID']).count().reset_index().head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.loc[(train_df.StudyInstanceUID == '6897fa9de148') & (train_df.SeriesInstanceUID == '2bfbb7fd2e8b')][train_df.columns[3:]].sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.loc[(train_df.StudyInstanceUID == '0003b3d648eb') & (train_df.SeriesInstanceUID == 'd2b2960c2bbf')][train_df.columns[3:]].sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.loc[(train_df.StudyInstanceUID == '000f7f114264')][train_df.columns[3:]].sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"negative_exam_for_pe_0 = train_df.loc[(train_df.StudyInstanceUID == '6897fa9de148') & (train_df.SeriesInstanceUID == '2bfbb7fd2e8b')][train_df.columns[:3]].values\nnegative_exam_for_pe_1 = train_df.loc[(train_df.StudyInstanceUID == '0003b3d648eb') & (train_df.SeriesInstanceUID == 'd2b2960c2bbf')][train_df.columns[:3]].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"negative_exam_for_pe_0_sample = os.path.join(root_path, \"/\".join(negative_exam_for_pe_0[5]) + \".dcm\")\nnegative_exam_for_pe_1_sample = os.path.join(root_path, \"/\".join(negative_exam_for_pe_1[10]) + \".dcm\")\nnegative_exam_for_pe_1_sample1 = os.path.join(root_path, \"/\".join(negative_exam_for_pe_1[100]) + \".dcm\")\nnegative_exam_for_pe_1_sample2 = os.path.join(root_path, \"/\".join(negative_exam_for_pe_1[121]) + \".dcm\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<h3><strong>Lable for following figure</strong></h3>\n\n```\nfigure 1 ---> negative exam for pe = 0 (leftside pe, rightside pe, rv_lv_ratio_lt_1)\nfigure 2 ---> negative exam for pe = 1\nfigure 3 ---> negative exam for pe = 1\nfigure 4 ---> negative exam for pe = 1\n```"},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"view_images([negative_exam_for_pe_0_sample, negative_exam_for_pe_1_sample, negative_exam_for_pe_1_sample1, negative_exam_for_pe_1_sample2], 2, 2, 'Images with Windowing')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(len(predictions)):\n    print(f\"layer_{i} Shape ---> \", predictions[i].shape)","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":4}