{"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":"<h1 style=\"border-style: outset;border-color: red;text-align: center;\">SIIM-FISABIO-RSNA COVID-19 Detection</h1>\n\n<img src=\"https://www.news-medical.net/image.axd?picture=2020%2F11%2Fshutterstock_1707538084_(1).jpg\" height=\"500\" width=\"500\" style=\"display: block;margin-left: auto;margin-right: auto;\"> \n\n<h2 style=\"font-weight:'bold'; color:blue; font-family:verdana; text-align: center;\"> Identify and localize COVID-19 abnormalities on chest radiographs </h2>\n\n<h2 style=\"text-align: center;border-style: double;text-align: center;border-color: red; \">About SIIM</h2>\n<img src=\"https://siim.org/resource/resmgr/SIIM_logo-600x315.png\" width=\"200\" style=\"display: block;margin-left: auto;margin-right: auto;\">\n<p> <b>Society for Imaging Informatics in Medicine</b> (<a href=\"https://siim.org/\">SIIM</a>) is the leading healthcare professional organization for those interested in the current and future use of informatics in medical imaging. The society's mission is to advance medical imaging informatics across the enterprise through education, research, and innovation in a multi-disciplinary community.</p>\n\n<h2 style=\"text-align: center;;border-style: double;text-align: center;;border-color: red;\"> What we need to do</h2>\n<img src=\"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQtew5pZk4BkEcy8v7TCXDaTAam9qj144eiy6K5ShjkeDNPVeZohAKIvdepF0HQxgcQzUU&usqp=CAU\" width=\"300\" height=\"300\" style=\"display: block;margin-left: auto;margin-right: auto;\">\n<p> <b>Identify and localize COVID-19 abnormalities on chest radiographs </b>. In particular, you'll categorize the radiographs as negative for pneumonia or typical, indeterminate, or atypical for COVID-19.</p>","metadata":{}},{"cell_type":"code","source":"!conda install -c conda-forge gdcm -y","metadata":{"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style = \"font-family:'Courier New';font-weight: bold;margin-top: 0px;margin-bottom: 1px;text-align: center;\">Import libraries</h1>","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom kaggle_datasets import KaggleDatasets\n\nimport numpy as np\nimport pandas as pd\nimport ast\n\nimport gdcm\n\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\n\nimport PIL\nimport cv2\nimport pydicom \nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport matplotlib.pyplot as plt\n\nimport os\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style = \"font-family:'Courier New';font-weight: bold;margin-top: 0px;margin-bottom: 1px;text-align: center;\">Checking TPU access</h2>","metadata":{}},{"cell_type":"code","source":"AUTO = tf.data.experimental.AUTOTUNE\n\ntry:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()  \n    print('Running on TPU ', tpu.master())\nexcept ValueError:\n    tpu = None\n\nif tpu:\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nelse:\n    strategy = tf.distribute.get_strategy()\n\nprint(\"REPLICAS: \", strategy.num_replicas_in_sync)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style = \"font-family:'Courier New';font-weight: bold;margin-top: 0px;margin-bottom: 1px;text-align: center;\">Loading Data</h1>","metadata":{}},{"cell_type":"code","source":"#GCS_DS_PATH = KaggleDatasets().get_gcs_path()\n\n#TRAIN_PATH = GCS_DS_PATH + \"/train\"\nTRAIN_PATH = \"../input/siim-covid19-detection/train\"\n#TEST_PATH = GCS_DS_PATH + \"/test\"\nTEST_PATH = \"../input/siim-covid19-detection/test\"\nTRAIN_FILES = tf.io.gfile.glob(TRAIN_PATH+\"/*/*/*.dcm\")\nTEST_FILES = tf.io.gfile.glob(TEST_PATH+\"/*/*/*.dcm\")\n\nclasses_dict = {\n    0 : \"Negative for Pneumonia\",\n    1  : \"Typical Appearance\",\n    2  : \"Indeterminate Appearance\",\n    3  : \"Atypical Appearance\"\n}\n\nHEIGHT,WIDTH = 512,512","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style = \"font-family:'Courier New';font-weight: bold;margin-top: 0px;margin-bottom: 1px;text-align: center;\">Preprocessing CSV files</h1>","metadata":{}},{"cell_type":"code","source":"#getting filepath from study_id or image_id\ndef get_path(file_id,main_path,id_type):\n    name = file_id.split(\"_\")[0]\n    if id_type == \"study\":\n        path = tf.io.gfile.glob(main_path+f\"/{name}/*/*.dcm\")[0]\n    else:\n        path = tf.io.gfile.glob(main_path+f\"/*/*/{name}.dcm\")[0]\n    return path","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Loading csv files\nimg_df = pd.read_csv(\"../input/siim-covid19-detection/train_image_level.csv\")\nstudy_df = pd.read_csv(\"../input/siim-covid19-detection/train_study_level.csv\")\n\n#converting into one-hot label\nstudy_df[\"one_hot\"] = study_df.apply(lambda x : np.array([x[\"Negative for Pneumonia\"],\n                                                        x[\"Typical Appearance\"],\n                                                        x[\"Indeterminate Appearance\"],\n                                                        x[\"Atypical Appearance\"]]),axis=1)\n\nstudy_df = study_df.drop([\"Negative for Pneumonia\",\"Typical Appearance\",\"Indeterminate Appearance\",\"Atypical Appearance\"],axis=1)\nstudy_df[\"label_id\"] = study_df[\"one_hot\"].map(lambda x : classes_dict[np.argmax(x)])\n\n\nstudy_df[\"filepath\"] = study_df[\"id\"].map(lambda x: get_path(x,TRAIN_PATH,\"study\"))\nstudy_df.rename(columns={\"id\":\"study_id\"},inplace=True)\nstudy_df[\"image_id\"] = study_df[\"filepath\"].map(lambda x : x.split(\"/\")[-1].split(\".\")[0]+\"_image\")\n\nimg_df.rename(columns={\"id\":\"image_id\"},inplace=True)\n\ntrain_df = pd.merge(study_df,img_df,on=\"image_id\")\ncols = [\"filepath\",\"study_id\",\"image_id\",\"StudyInstanceUID\",\"one_hot\",\"label_id\",\"label\",\"boxes\"]\ntrain_df = train_df[cols]\ntrain_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = list(train_df[\"label_id\"].value_counts().index)\ny = list(train_df[\"label_id\"].value_counts().values) \n\nplt.figure(figsize=(10,5))\nplt.bar(x,y)\nplt.xlabel(\"Label\")\nplt.ylabel(\"Count\")\nplt.title(\"Labels Distribution\")\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style = \"font-family:'Courier New';font-weight: bold;margin-top: 0px;margin-bottom: 1px;text-align: center;\">DICOM to Array</h2>","metadata":{}},{"cell_type":"code","source":"#Ref : https://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way\ndef dicom2arr(path, voi_lut = True, fix_monochrome = True):\n    dicom = pydicom.read_file(path)\n    \n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n               \n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n        \n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n        \n    return data","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2 style = \"font-family:'Courier New';font-weight: bold;margin-top: 0px;margin-bottom: 1px;text-align: center;\">Image Visualization</h2>","metadata":{}},{"cell_type":"code","source":"colors_dict = {\n    0 : \"blue\",\n    1 : \"orange\",\n    2 : 'green',\n    3 : \"red\"\n}\n\ndef displayImg(filepath_ls):\n    plt.figure(figsize=(18,18))\n    for i,filepath in enumerate(filepath_ls):\n        ax = plt.subplot(3,3,i+1)\n        arr = dicom2arr(filepath)\n        req = train_df[train_df[\"filepath\"]==filepath]\n        boxes = list(req[\"boxes\"].values)\n        \n        label = classes_dict[np.argmax(req[\"one_hot\"].values[0])]\n        color = colors_dict[np.argmax(req[\"one_hot\"].values[0])]\n        plt.imshow(arr,cmap=\"gray\")\n        plt.axis(\"off\")\n        plt.title(label)\n        \n        if not pd.isna(boxes[0]) :\n            for box in boxes:\n                box = ast.literal_eval(box)[0] \n                x,y,width,height = box[\"x\"], box[\"y\"], box[\"width\"], box[\"height\"]\n                rect = patches.Rectangle((x,y),width,height,edgecolor=color,linewidth=1, facecolor='none')\n                ax.add_patch(rect)\n                ax.text(x+15,y-50,label)\n    plt.show()\n\nrand = np.random.randint(0,4000,9)\nfilepath_ls = [list(train_df[\"filepath\"])[i] for i in rand]\ndisplayImg(filepath_ls)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}