{"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":"# Intro\nWelcome to the [VinBigData Chest X-ray Abnormalities Detection](https://www.kaggle.com/c/vinbigdata-chest-xray-abnormalities-detection/data) compedition.\n\n![](https://storage.googleapis.com/kaggle-competitions/kaggle/24800/logos/header.png)\n\nWe consider 14 critical radiographic findings as listed below (click for further informations):\n\n0 - [Aortic enlargement](https://en.wikipedia.org/wiki/Aortic_aneurysm) <br>\n1 - [Atelectasis](https://en.wikipedia.org/wiki/Atelectasis) <br>\n2 - [Calcification](https://en.wikipedia.org/wiki/Calcification) <br>\n3 - [Cardiomegaly](https://en.wikipedia.org/wiki/Cardiomegaly) <br>\n4 - [Consolidation](https://en.wikipedia.org/wiki/Pulmonary_consolidation) <br>\n5 - [ILD](https://en.wikipedia.org/wiki/Interstitial_lung_disease) <br>\n6 - [Infiltration](https://en.wikipedia.org/wiki/Infiltration_(medical)) <br>\n7 - [Lung Opacity](https://en.wikipedia.org/wiki/Ground-glass_opacity) <br>\n8 - [Nodule/Mass](https://en.wikipedia.org/wiki/Lung_nodule) <br>\n9 - Other lesion <br>\n10 - [Pleural effusion](https://en.wikipedia.org/wiki/Pleural_effusion) <br>\n11 - [Pleural thickening](https://en.wikipedia.org/wiki/Pleural_thickening) <br>\n12 - [Pneumothorax](https://en.wikipedia.org/wiki/Pneumothorax) <br>\n13 - [Pulmonary fibrosis](https://en.wikipedia.org/wiki/Pulmonary_fibrosis#:~:text=Pulmonary%20fibrosis%20is%20a%20condition,%2C%20pneumothorax%2C%20and%20lung%20cancer.)\n\n<span style=\"color: royalblue;\">Please vote the notebook up if it helps you. Feel free to leave a comment above the notebook. Thank you. </span>","metadata":{}},{"cell_type":"markdown","source":"# Libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport pydicom as dicom\nimport cv2\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2021-05-24T12:21:21.662635Z","iopub.execute_input":"2021-05-24T12:21:21.662941Z","iopub.status.idle":"2021-05-24T12:21:22.077658Z","shell.execute_reply.started":"2021-05-24T12:21:21.662912Z","shell.execute_reply":"2021-05-24T12:21:22.076829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Path","metadata":{}},{"cell_type":"code","source":"path = '/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/'\nos.listdir(path)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T12:21:31.169513Z","iopub.execute_input":"2021-05-24T12:21:31.169953Z","iopub.status.idle":"2021-05-24T12:21:31.186529Z","shell.execute_reply.started":"2021-05-24T12:21:31.169914Z","shell.execute_reply":"2021-05-24T12:21:31.185609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(path+'train.csv')\nsamp_subm = pd.read_csv(path+'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-24T12:21:38.425761Z","iopub.execute_input":"2021-05-24T12:21:38.426145Z","iopub.status.idle":"2021-05-24T12:21:38.580859Z","shell.execute_reply.started":"2021-05-24T12:21:38.426112Z","shell.execute_reply":"2021-05-24T12:21:38.580099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Overview","metadata":{}},{"cell_type":"code","source":"print('Number train samples:', len(train_data.index))\nprint('Number test samples:', len(samp_subm.index))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-05-24T12:21:50.132796Z","iopub.execute_input":"2021-05-24T12:21:50.13312Z","iopub.status.idle":"2021-05-24T12:21:50.138938Z","shell.execute_reply.started":"2021-05-24T12:21:50.133087Z","shell.execute_reply":"2021-05-24T12:21:50.13801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(12, 4))\nx = train_data['class_name'].value_counts().keys()\ny = train_data['class_name'].value_counts().values\nax.bar(x, y)\nax.set_xticklabels(x, rotation=90)\nax.set_title('Distribution of the labels')\nplt.grid()\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-05-24T12:21:58.90844Z","iopub.execute_input":"2021-05-24T12:21:58.90878Z","iopub.status.idle":"2021-05-24T12:21:59.171154Z","shell.execute_reply.started":"2021-05-24T12:21:58.908749Z","shell.execute_reply":"2021-05-24T12:21:59.170245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As we can see the dataset is inbalanced.","metadata":{}},{"cell_type":"markdown","source":"# Read dicom Files","metadata":{}},{"cell_type":"code","source":"idnum = 2\nimage_id = train_data.loc[idnum, 'image_id']\ndata_file = dicom.dcmread(path+'train/'+image_id+'.dicom')\nimg = data_file.pixel_array","metadata":{"execution":{"iopub.status.busy":"2021-05-24T12:22:18.889907Z","iopub.execute_input":"2021-05-24T12:22:18.890252Z","iopub.status.idle":"2021-05-24T12:22:20.759516Z","shell.execute_reply.started":"2021-05-24T12:22:18.890213Z","shell.execute_reply":"2021-05-24T12:22:20.758711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Print meta data of the image:","metadata":{}},{"cell_type":"code","source":"print(data_file)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-05-24T12:22:34.011558Z","iopub.execute_input":"2021-05-24T12:22:34.011898Z","iopub.status.idle":"2021-05-24T12:22:34.017887Z","shell.execute_reply.started":"2021-05-24T12:22:34.011867Z","shell.execute_reply":"2021-05-24T12:22:34.016698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Image shape:', img.shape)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-05-24T12:22:40.869262Z","iopub.execute_input":"2021-05-24T12:22:40.86962Z","iopub.status.idle":"2021-05-24T12:22:40.874547Z","shell.execute_reply.started":"2021-05-24T12:22:40.869588Z","shell.execute_reply":"2021-05-24T12:22:40.87333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bbox = [train_data.loc[idnum, 'x_min'],\n        train_data.loc[idnum, 'y_min'],\n        train_data.loc[idnum, 'x_max'],\n        train_data.loc[idnum, 'y_max']]\nfig, ax = plt.subplots(1, 1, figsize=(20, 4))\nax.imshow(img, cmap='gray')\np = matplotlib.patches.Rectangle((bbox[0], bbox[1]),\n                                 bbox[2]-bbox[0],\n                                 bbox[3]-bbox[1],\n                                 ec='r', fc='none', lw=2.)\nax.add_patch(p)\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-05-24T12:22:49.087672Z","iopub.execute_input":"2021-05-24T12:22:49.088012Z","iopub.status.idle":"2021-05-24T12:22:49.485083Z","shell.execute_reply.started":"2021-05-24T12:22:49.087982Z","shell.execute_reply":"2021-05-24T12:22:49.484147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Show Examples\nPlot 3 images of every class with the bounding boxes:","metadata":{}},{"cell_type":"code","source":"def plot_example(idx_list):\n    fig, axs = plt.subplots(1, 3, figsize=(15, 10))\n    fig.subplots_adjust(hspace = .1, wspace=.1)\n    axs = axs.ravel()\n    for i in range(3):\n        image_id = train_data.loc[idx_list[i], 'image_id']\n        data_file = dicom.dcmread(path+'train/'+image_id+'.dicom')\n        img = data_file.pixel_array\n        axs[i].imshow(img, cmap='gray')\n        axs[i].set_title(train_data.loc[idx_list[i], 'class_name'])\n        axs[i].set_xticklabels([])\n        axs[i].set_yticklabels([])\n        if train_data.loc[idx_list[i], 'class_name'] != 'No finding':\n            bbox = [train_data.loc[idx_list[i], 'x_min'],\n                    train_data.loc[idx_list[i], 'y_min'],\n                    train_data.loc[idx_list[i], 'x_max'],\n                    train_data.loc[idx_list[i], 'y_max']]\n            p = matplotlib.patches.Rectangle((bbox[0], bbox[1]),\n                                             bbox[2]-bbox[0],\n                                             bbox[3]-bbox[1],\n                                             ec='r', fc='none', lw=2.)\n            axs[i].add_patch(p)\n            \nfor num in range(15):\n    idx_list = train_data[train_data['class_id']==num][0:3].index.values\n    plot_example(idx_list)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-05-24T12:23:26.957702Z","iopub.execute_input":"2021-05-24T12:23:26.958068Z","iopub.status.idle":"2021-05-24T12:24:33.452234Z","shell.execute_reply.started":"2021-05-24T12:23:26.958035Z","shell.execute_reply":"2021-05-24T12:24:33.451198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Write Output\n","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0"}},{"cell_type":"code","source":"samp_subm.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-05-24T12:25:11.858311Z","iopub.execute_input":"2021-05-24T12:25:11.858681Z","iopub.status.idle":"2021-05-24T12:25:11.875987Z","shell.execute_reply.started":"2021-05-24T12:25:11.85865Z","shell.execute_reply":"2021-05-24T12:25:11.875177Z"},"trusted":true},"execution_count":null,"outputs":[]}]}