{"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":"## Introduction\nAt the end of 2019, the coronavirus disease aroused in “Wuhan”, the city of china. Since January 2020, It has been extensively increased all around the world. On 30 January 2020, the WHO announced public health emergency internationally because of COVID-19. The disease was named coronavirus disease (COVID-19) by WHO in February 2020. The epidemic COVID-19 disease is caused by a virus called \"Severe Acute Respiratory Syndrome\" coronavirus-2 (SARS-CoV-2). The coronavirus is a family of several viruses that causes disease like \"Severe Acute Respiratory Syndrome\" (SARS-CoV) and \"Middle East Respiratory Syndrome\". Nowadays, countries like the USA, India, Brazil, and Russia struggling with the COVID-19 as it is the cause of the increase in the death rate. It can be spread to humans as well as other species of animals such as cats, cattle, bats, and camels. It is can be deadly for people with a weakened immune system. Physicians and specialists of infectious diseases around the world are starving hard to discover a treatment for the disease. The diseases can be spreading by touching and sitting or standing close to infected person. So, the COVID-19 infected person should to be identified and isolated before he might infect others. The earlier stage symptoms of COVID-19 include “cough”, “fatigue”, “fever”, and “myalgia”. The more intense situation of disease includes the heart damage, the respiratory problem, and internal infections. Some other abnormal states are observed in Covid-19 patients Chest CT and X-ray images. In most cases, the doctors use the patient’s X-ray and CT scan images of the patient’s chest for earlier stage diagnose of COVID-19. Chest radiography is the most widely used method of pneumonia diagnosis worldwide. It is a faster and cheaper clinical method of diagnosis. It is preferred over computed tomography (CT) and MRI because of lower radiation dose, easy availability, and cost. So, X-ray is the first choice of the radiologist for chest pathology detection and it is also applied for confirmation of COVID-19 patients. Therefore, this study focus on the use of X-ray images for COVID-19 detection in patients.","metadata":{}},{"cell_type":"markdown","source":"## Import libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport pydicom as dicom\nimport cv2\nimport ast\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-05-27T09:30:57.915323Z","iopub.execute_input":"2021-05-27T09:30:57.915732Z","iopub.status.idle":"2021-05-27T09:30:57.997387Z","shell.execute_reply.started":"2021-05-27T09:30:57.915655Z","shell.execute_reply":"2021-05-27T09:30:57.996544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load data","metadata":{}},{"cell_type":"code","source":"train_image = pd.read_csv('../input/siim-covid19-detection/train_image_level.csv')\ntrain_study = pd.read_csv('../input/siim-covid19-detection/train_study_level.csv')\nsample_submission = pd.read_csv('../input/siim-covid19-detection/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-27T09:30:58.334726Z","iopub.execute_input":"2021-05-27T09:30:58.335240Z","iopub.status.idle":"2021-05-27T09:30:58.382103Z","shell.execute_reply.started":"2021-05-27T09:30:58.335205Z","shell.execute_reply":"2021-05-27T09:30:58.381131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T09:30:58.551651Z","iopub.execute_input":"2021-05-27T09:30:58.552017Z","iopub.status.idle":"2021-05-27T09:30:58.567283Z","shell.execute_reply.started":"2021-05-27T09:30:58.551973Z","shell.execute_reply":"2021-05-27T09:30:58.566141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_image)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T09:30:58.785415Z","iopub.execute_input":"2021-05-27T09:30:58.785718Z","iopub.status.idle":"2021-05-27T09:30:58.790963Z","shell.execute_reply.started":"2021-05-27T09:30:58.785693Z","shell.execute_reply":"2021-05-27T09:30:58.790024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_study.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T09:30:59.000145Z","iopub.execute_input":"2021-05-27T09:30:59.000546Z","iopub.status.idle":"2021-05-27T09:30:59.011010Z","shell.execute_reply.started":"2021-05-27T09:30:59.000515Z","shell.execute_reply":"2021-05-27T09:30:59.009840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_study)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T09:30:59.260275Z","iopub.execute_input":"2021-05-27T09:30:59.260614Z","iopub.status.idle":"2021-05-27T09:30:59.267081Z","shell.execute_reply.started":"2021-05-27T09:30:59.260582Z","shell.execute_reply":"2021-05-27T09:30:59.265877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T09:30:59.514873Z","iopub.execute_input":"2021-05-27T09:30:59.515207Z","iopub.status.idle":"2021-05-27T09:30:59.523557Z","shell.execute_reply.started":"2021-05-27T09:30:59.515158Z","shell.execute_reply":"2021-05-27T09:30:59.522583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(sample_submission)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T09:30:59.969354Z","iopub.execute_input":"2021-05-27T09:30:59.969700Z","iopub.status.idle":"2021-05-27T09:30:59.975282Z","shell.execute_reply.started":"2021-05-27T09:30:59.969661Z","shell.execute_reply":"2021-05-27T09:30:59.974340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Merging train study in train","metadata":{}},{"cell_type":"code","source":"train_study['StudyInstanceUID'] = train_study['id'].apply(lambda x: x.replace('_study', ''))\ndel train_study['id']\ntrain_image = train_image.merge(train_study, on='StudyInstanceUID')\ntrain_image.head()\ndef bar_plot(train_df, variable):\n    var = train_df[variable]\n    varValue = var.value_counts()\n    \n    # visualize\n    plt.figure(figsize = (12,3))\n    plt.bar(varValue.index, varValue)\n    plt.xticks(varValue.index, varValue.index.values)\n    plt.ylabel(\"Frequency\")\n    plt.title(variable)\n    plt.show()\n    print(\"{}: \\n {}\".format(variable,varValue))\n    \ntrain_image['target'] = 'Negative for Pneumonia'\ntrain_image.loc[train_image['Typical Appearance']==1, 'target'] = 'Typical Appearance'\ntrain_image.loc[train_image['Indeterminate Appearance']==1, 'target'] = 'Indeterminate Appearance'\ntrain_image.loc[train_image['Atypical Appearance']==1, 'target'] = 'Atypical Appearance'\nbar_plot(train_image, 'target') ","metadata":{"execution":{"iopub.status.busy":"2021-05-27T09:31:01.099832Z","iopub.execute_input":"2021-05-27T09:31:01.100398Z","iopub.status.idle":"2021-05-27T09:31:01.251643Z","shell.execute_reply.started":"2021-05-27T09:31:01.100353Z","shell.execute_reply":"2021-05-27T09:31:01.250643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image[\"target\"].value_counts().head(7).plot(kind = 'pie', autopct='%1.1f%%', figsize=(8, 8)).legend()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T09:31:06.077581Z","iopub.execute_input":"2021-05-27T09:31:06.077927Z","iopub.status.idle":"2021-05-27T09:31:06.269632Z","shell.execute_reply.started":"2021-05-27T09:31:06.077899Z","shell.execute_reply":"2021-05-27T09:31:06.268657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image.loc[0]","metadata":{"execution":{"iopub.status.busy":"2021-05-27T09:31:11.247740Z","iopub.execute_input":"2021-05-27T09:31:11.248110Z","iopub.status.idle":"2021-05-27T09:31:11.258392Z","shell.execute_reply.started":"2021-05-27T09:31:11.248077Z","shell.execute_reply":"2021-05-27T09:31:11.257195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating a function to get the image","metadata":{}},{"cell_type":"code","source":"def extraction(i):\n    path_train = '../input/siim-covid19-detection/' + 'train/' + train_image.loc[i, 'StudyInstanceUID']\n    last_folder_in_path = os.listdir(path_train)[0]\n    path_train = path_train + '/{}/'.format(last_folder_in_path)\n    img_id = train_image.loc[i, 'id'].replace('_image','.dcm')\n    print(img_id)\n    data_file = dicom.dcmread(path_train+img_id)\n    img = data_file.pixel_array\n    return img","metadata":{"execution":{"iopub.status.busy":"2021-05-27T09:31:12.518785Z","iopub.execute_input":"2021-05-27T09:31:12.519138Z","iopub.status.idle":"2021-05-27T09:31:12.524269Z","shell.execute_reply.started":"2021-05-27T09:31:12.519110Z","shell.execute_reply":"2021-05-27T09:31:12.523115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image1=extraction(0)\nimage1","metadata":{"execution":{"iopub.status.busy":"2021-05-27T09:31:13.463844Z","iopub.execute_input":"2021-05-27T09:31:13.464204Z","iopub.status.idle":"2021-05-27T09:31:13.506130Z","shell.execute_reply.started":"2021-05-27T09:31:13.464156Z","shell.execute_reply":"2021-05-27T09:31:13.505186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating bounding boxes","metadata":{}},{"cell_type":"code","source":"boxes = ast.literal_eval(train_image.loc[0, 'boxes'])\nfig, ax = plt.subplots(1,1, figsize=(8,4))\nfor box in boxes:\n    p = matplotlib.patches.Rectangle((box['x'], box['y']),\n                                      box['width'], box['height'],\n                                      ec='r', fc='none', lw=1.5)\n    ax.add_patch(p)\nax.imshow(image1,cmap='gray')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T09:31:16.407328Z","iopub.execute_input":"2021-05-27T09:31:16.407711Z","iopub.status.idle":"2021-05-27T09:31:17.705308Z","shell.execute_reply.started":"2021-05-27T09:31:16.407679Z","shell.execute_reply":"2021-05-27T09:31:17.704279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(3,3, figsize=(20,16))\nfig.subplots_adjust(hspace=.1, wspace=.1)\naxes = axes.ravel()\n\nfor row in range(9):\n    img = extraction(row)\n    # if (nan == nan)\n    # False\n    if (train_image.loc[row,'boxes'] == train_image.loc[row,'boxes']):\n        boxes = ast.literal_eval(train_image.loc[row,'boxes'])\n        for box in boxes:\n            p = matplotlib.patches.Rectangle((box['x'], box['y']),\n                                              box['width'], box['height'],\n                                              ec='r', fc='none', lw=2.\n                                            )\n            axes[row].add_patch(p)\n    \n    axes[row].imshow(img, cmap='gray')\n    axes[row].set_title(train_image.loc[row, 'label'].split(' ')[0])\n    axes[row].set_xticklabels([])\n    axes[row].set_yticklabels([])","metadata":{"execution":{"iopub.status.busy":"2021-05-27T09:32:55.626448Z","iopub.execute_input":"2021-05-27T09:32:55.626822Z","iopub.status.idle":"2021-05-27T09:33:07.229766Z","shell.execute_reply.started":"2021-05-27T09:32:55.626778Z","shell.execute_reply":"2021-05-27T09:33:07.227396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Future work: Model training","metadata":{}},{"cell_type":"markdown","source":"## Consider upvoting if you like my work.","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}