{"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":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nfrom pathlib import Path\nimport cv2\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-23T03:55:11.386203Z","iopub.execute_input":"2021-06-23T03:55:11.386557Z","iopub.status.idle":"2021-06-23T03:55:11.391525Z","shell.execute_reply.started":"2021-06-23T03:55:11.386525Z","shell.execute_reply":"2021-06-23T03:55:11.390507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_PATH = '../input/siim-covid19-detection/'\ntrain_study = pd.read_csv(BASE_PATH + 'train_study_level.csv')\ntrain_study.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-23T03:51:37.290743Z","iopub.status.idle":"2021-06-23T03:51:37.291320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image = pd.read_csv(BASE_PATH + 'train_image_level.csv')\ntrain_image.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-23T03:51:37.292253Z","iopub.status.idle":"2021-06-23T03:51:37.292922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image['split_label'] = train_image.label.apply(lambda x: [x.split()[offs:offs+6] for offs in range(0, len(x.split()), 6)])\ntrain_image.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-23T03:51:37.293767Z","iopub.status.idle":"2021-06-23T03:51:37.294338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dicom2array(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    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    return data\n\ndef plot_img(img, size=(7, 7), is_rgb=True, title=\"\", cmap='gray'):\n    plt.figure(figsize=size)\n    plt.imshow(img, cmap=cmap)\n    plt.suptitle(title)\n    plt.show()\n\ndef plot_imgs(imgs, cols=4, size=7, is_rgb=True, title=\"\", cmap='rainbow', img_size=(500,500)):\n    rows = len(imgs)//cols + 1\n    fig = plt.figure(figsize=(cols*size, rows*size))\n    for i, img in enumerate(imgs):\n        if img_size is not None:\n            img = cv2.resize(img, img_size)\n        fig.add_subplot(rows, cols, i+1)\n        plt.imshow(img, cmap=cmap)\n    plt.suptitle(title)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-23T03:51:37.295262Z","iopub.status.idle":"2021-06-23T03:51:37.295879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\nfrom fastai.medical.imaging import *\ndataset_path = Path('../input/siim-covid19-detection')\ndef image_path(row):\n    study_path = dataset_path/'train'/row.StudyInstanceUID\n    for i in get_dicom_files(study_path):\n        if row.id.split('_')[0] == i.stem: return i \n\ntrain_image['image_path'] = train_image.apply(image_path, axis=1)\ntrain_image.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-23T03:56:48.029077Z","iopub.execute_input":"2021-06-23T03:56:48.029451Z","iopub.status.idle":"2021-06-23T03:57:15.882338Z","shell.execute_reply.started":"2021-06-23T03:56:48.029418Z","shell.execute_reply":"2021-06-23T03:57:15.881328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs = []\nimage_paths = train_image['image_path'].values\ncolor_mean=(104,11,12)\n# map label_id to specify color\nthickness = 5\nscale = 5\n\n\nfor i in range(8):\n    image_path = random.choice(image_paths)\n    img = dicom2array(path=image_path)\n    img = cv2.resize(img, None, fx=1/scale, fy=1/scale)\n    \n\n    img = np.stack([img, img, img], axis=-1)\n    for i in train_image.loc[train_image['image_path'] == image_path].split_label.values[0]:\n        if i[0] == 'opacity':\n            img = cv2.rectangle(img,\n                                (int(float(i[2])/scale), int(float(i[3])/scale)),\n                                (int(float(i[4])/scale), int(float(i[5])/scale)),\n                                [255,0,0], thickness)\n    \n    img = cv2.resize(img, (500,500))\n    \n    img = img - color_mean\n    imgs.append(img)\n    \nplot_imgs(imgs, cmap=\"rainbow\")\n","metadata":{"execution":{"iopub.status.busy":"2021-06-23T03:59:21.683426Z","iopub.execute_input":"2021-06-23T03:59:21.683794Z","iopub.status.idle":"2021-06-23T03:59:26.834946Z","shell.execute_reply.started":"2021-06-23T03:59:21.683764Z","shell.execute_reply":"2021-06-23T03:59:26.833835Z"},"trusted":true},"execution_count":null,"outputs":[]}]}