{"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":"# import libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport torch \nimport torch.nn as nn\nimport os\nimport glob\nimport pydicom\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nfrom pydicom import dcmread\nimport ast\n","metadata":{"execution":{"iopub.status.busy":"2021-06-17T20:00:26.774911Z","iopub.execute_input":"2021-06-17T20:00:26.775503Z","iopub.status.idle":"2021-06-17T20:00:28.164612Z","shell.execute_reply.started":"2021-06-17T20:00:26.775405Z","shell.execute_reply":"2021-06-17T20:00:28.163638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data\n\n**Files**\n* train_study_level.csv - the train study-level metadata, with one row for each study, including correct labels.\n* train_image_level.csv - the train image-level metadata, with one row for each image, including both correct labels and any bounding boxes in a dictionary format. Some images in both test and train have multiple bounding boxes.\n* sample_submission.csv - a sample submission file containing all image- and study-level IDs.\n\n**train_study_level.csv**\n* id - unique study identifier\n* Negative for Pneumonia - 1 if the study is negative for pneumonia, 0 otherwise\n* Typical Appearance - 1 if the study has this appearance, 0 otherwise\n* Indeterminate Appearance  - 1 if the study has this appearance, 0 otherwise\n* Atypical Appearance  - 1 if the study has this appearance, 0 otherwise\n\n**train_image_level.csv**\n* id - unique image identifier\n* boxes - bounding boxes in easily-readable dictionary format\n* label - the correct prediction label for the provided bounding boxes","metadata":{}},{"cell_type":"markdown","source":"# Path","metadata":{}},{"cell_type":"code","source":"DIR_PATH = \"../input/siim-covid19-detection\"\ntrain_imglvl_path = f\"{DIR_PATH}/train_image_level.csv\"\ntrain_stdylvl_path = f\"{DIR_PATH}/train_study_level.csv\"\ntrain_path = f\"{DIR_PATH}/train\"\n\n#loading csv file using pandas \n\ntrain_df = pd.read_csv(train_imglvl_path)\n#train_df.sample(5)","metadata":{"execution":{"iopub.status.busy":"2021-06-17T20:00:28.166319Z","iopub.execute_input":"2021-06-17T20:00:28.166741Z","iopub.status.idle":"2021-06-17T20:00:28.215813Z","shell.execute_reply.started":"2021-06-17T20:00:28.166695Z","shell.execute_reply":"2021-06-17T20:00:28.214872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_df['id'].unique().shape\n#Nan_df = train_df.groupby(\"id\")[\"boxes\"].agg(lambda s: (s == \"NaN\").sum()).reset_index().rename({\"class_id\" : \"Nan_values\"}, axis = 1)","metadata":{"execution":{"iopub.status.busy":"2021-06-17T20:00:28.217516Z","iopub.execute_input":"2021-06-17T20:00:28.21784Z","iopub.status.idle":"2021-06-17T20:00:28.221296Z","shell.execute_reply.started":"2021-06-17T20:00:28.217803Z","shell.execute_reply":"2021-06-17T20:00:28.220394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Drop Nan Values and convert boxes from str to list**","metadata":{}},{"cell_type":"code","source":"train_new = train_df.dropna(axis = 0, inplace = False).reset_index(drop = True)\ntrain_new['boxes'] = train_new.boxes.apply(ast.literal_eval)  # converting into list ","metadata":{"execution":{"iopub.status.busy":"2021-06-17T20:00:28.223249Z","iopub.execute_input":"2021-06-17T20:00:28.223938Z","iopub.status.idle":"2021-06-17T20:00:28.436878Z","shell.execute_reply.started":"2021-06-17T20:00:28.223893Z","shell.execute_reply":"2021-06-17T20:00:28.435788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Get random sample from DataFrame**","metadata":{}},{"cell_type":"code","source":"sample_df = train_new.sample(5).reset_index(drop = True)","metadata":{"execution":{"iopub.status.busy":"2021-06-17T20:00:28.438422Z","iopub.execute_input":"2021-06-17T20:00:28.43886Z","iopub.status.idle":"2021-06-17T20:00:28.445738Z","shell.execute_reply.started":"2021-06-17T20:00:28.438815Z","shell.execute_reply":"2021-06-17T20:00:28.445031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df","metadata":{"execution":{"iopub.status.busy":"2021-06-17T20:00:28.446781Z","iopub.execute_input":"2021-06-17T20:00:28.44722Z","iopub.status.idle":"2021-06-17T20:00:28.48298Z","shell.execute_reply.started":"2021-06-17T20:00:28.447189Z","shell.execute_reply":"2021-06-17T20:00:28.48201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, rows in sample_df.iterrows():\n    # get dir \n    dir = os.listdir(train_path + \"/\" + rows[\"StudyInstanceUID\"])\n    #print(rows['id'])\n    #print(train_path + \"/\" + rows[\"StudyInstanceUID\"] + \"/\"+ dir[0] + \"/\" + rows[\"id\"][:-6] + \".dcm\")\n    #continue\n    dicom = pydicom.dcmread(train_path + \"/\" + rows[\"StudyInstanceUID\"] + \"/\"+ dir[0] + \"/\" + rows[\"id\"][:-6] + \".dcm\")\n    img = dicom.pixel_array\n    \n    boxs = rows['boxes']\n    #print(boxs)\n    fig, a = plt.subplots(1,1)\n    fig.set_size_inches(10,10)\n    a.imshow(img, cmap = 'gray')\n    \n    \n    for box in boxs:\n        x, y, width, height = int(box['x']), int(box['y']), int(box['width']), int(box['height'])\n        #print(x, y, width, height)\n        rect = patches.Rectangle((x, y),\n                                 width, height,\n                                 linewidth = 2,\n                                 edgecolor = 'r',\n                                 facecolor = 'none')\n        a.add_patch(rect)\n        \n    plt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2021-06-17T20:00:28.484473Z","iopub.execute_input":"2021-06-17T20:00:28.484924Z","iopub.status.idle":"2021-06-17T20:00:38.569453Z","shell.execute_reply.started":"2021-06-17T20:00:28.484874Z","shell.execute_reply":"2021-06-17T20:00:38.568454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ***Thanks for your patience***\n","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}