{"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":"%matplotlib inline\n#這是juoyter notebook的magic word˙\n\nimport matplotlib\nimport matplotlib.pyplot as plt\nfrom IPython import display","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-29T19:41:23.669367Z","iopub.execute_input":"2021-06-29T19:41:23.669823Z","iopub.status.idle":"2021-06-29T19:41:23.682769Z","shell.execute_reply.started":"2021-06-29T19:41:23.669733Z","shell.execute_reply":"2021-06-29T19:41:23.681688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Covid 19醫療圖像檢測 資料預處理","metadata":{}},{"cell_type":"code","source":"import os\n#判斷是否在jupyter notebook上\ndef is_in_ipython():\n    \"Is the code running in the ipython environment (jupyter including)\"\n    program_name = os.path.basename(os.getenv('_', ''))\n\n    if ('jupyter-notebook' in program_name or # jupyter-notebook\n        'ipython'          in program_name or # ipython\n        'jupyter' in program_name or  # jupyter\n        'JPY_PARENT_PID'   in os.environ):    # ipython-notebook\n        return True\n    else:\n        return False\n\n\n#判斷是否在colab上\ndef is_in_colab():\n    if not is_in_ipython(): return False\n    try:\n        from google import colab\n        return True\n    except: return False\n\n#判斷是否在kaggke_kernal上\ndef is_in_kaggle_kernal():\n    if 'kaggle' in os.environ['PYTHONPATH']:\n        return True\n    else:\n        return False\n\nif is_in_colab():\n    from google.colab import drive\n    drive.mount('/content/gdrive')","metadata":{"execution":{"iopub.status.busy":"2021-06-29T19:41:23.687177Z","iopub.execute_input":"2021-06-29T19:41:23.687502Z","iopub.status.idle":"2021-06-29T19:41:23.696302Z","shell.execute_reply.started":"2021-06-29T19:41:23.687474Z","shell.execute_reply":"2021-06-29T19:41:23.695212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.environ['TRIDENT_BACKEND'] = 'pytorch'\n\nif is_in_kaggle_kernal():\n    os.environ['TRIDENT_HOME'] = './trident'\n    \nelif is_in_colab():\n    os.environ['TRIDENT_HOME'] = '/content/gdrive/My Drive/trident'\n\n#為確保安裝最新版 \n!pip uninstall tridentx -y\n!pip install tridentx --upgrade\n!pip install pydicom --upgrade\nimport json\nimport copy\nimport numpy as np\n#調用trident api\nimport trident as T\nfrom trident import *\nfrom trident.models import resnet,efficientnet\nimport random","metadata":{"execution":{"iopub.status.busy":"2021-06-29T19:41:23.697806Z","iopub.execute_input":"2021-06-29T19:41:23.698201Z","iopub.status.idle":"2021-06-29T19:41:47.018361Z","shell.execute_reply.started":"2021-06-29T19:41:23.698170Z","shell.execute_reply":"2021-06-29T19:41:47.017241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* ### DICOM Tag\n一份DICOM檔案中有許多的DICOM tag，用來詳細紀錄照片拍攝時的設備資訊、病人資訊等。Pydicom幫我們將DICOM tag轉換成python物件，讓我們更容易取得需要的資訊。下面程式利用pydicom讀取DICOM檔中的內容。pydicom將每一個tag條列如下面格式  \n  \n| DICOM tag(group,item) | DICOM tag 名稱 | DICOM tag 內容       |\n|-----------------------|----------------|----------------------|\n| (0010, 0020)          | Patient ID     | LO: 'LIDC-IDRI-0001' |","metadata":{}},{"cell_type":"markdown","source":"首先讀取一下看看全體圖像的數量","metadata":{}},{"cell_type":"code","source":"dicom_images=glob.glob('../input/siim-covid19-detection/train/*/*/*.dcm')\nprint(len(dicom_images))","metadata":{"execution":{"iopub.status.busy":"2021-06-29T19:41:47.020545Z","iopub.execute_input":"2021-06-29T19:41:47.021021Z","iopub.status.idle":"2021-06-29T19:42:13.412870Z","shell.execute_reply.started":"2021-06-29T19:41:47.020969Z","shell.execute_reply":"2021-06-29T19:42:13.411898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\n\nexample_case=random.choice(dicom_images)\nprint(example_case)\ndicoms=pydicom.read_file(example_case)\nprint(dicoms)","metadata":{"execution":{"iopub.status.busy":"2021-06-29T19:54:56.715991Z","iopub.execute_input":"2021-06-29T19:54:56.716347Z","iopub.status.idle":"2021-06-29T19:54:57.273655Z","shell.execute_reply.started":"2021-06-29T19:54:56.716317Z","shell.execute_reply":"2021-06-29T19:54:57.272644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"我們可以利用pydicom.read_file(路徑)來讀取dicom檔，而取出dicom圖像也很簡單，直接讀取pixel_array屬性即可","metadata":{}},{"cell_type":"code","source":"plt.rcParams[\"figure.figsize\"] = (10,10)\n\n#像素值並且儲存成numpy的格式\nimage = pydicom.read_file(example_case)\npixels=image.pixel_array\nplt.imshow(pixels, cmap=plt.cm.gray)\nplt.axis(\"off\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-06-29T19:42:14.278102Z","iopub.execute_input":"2021-06-29T19:42:14.278596Z","iopub.status.idle":"2021-06-29T19:42:15.297373Z","shell.execute_reply.started":"2021-06-29T19:42:14.278384Z","shell.execute_reply":"2021-06-29T19:42:15.296166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"接下來要示範的是如何將模糊的圖變得清晰銳利","metadata":{}},{"cell_type":"code","source":"from skimage.filters import unsharp_mask\nfrom skimage import exposure\n\nunsharp_image = unsharp_mask(pixels, radius=5, amount=2)\nequalized_image = exposure.equalize_hist(unsharp_image)\n\nplt.imshow(equalized_image, cmap=plt.cm.gray)\nplt.axis(\"off\")\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2021-06-29T19:42:15.298933Z","iopub.execute_input":"2021-06-29T19:42:15.299502Z","iopub.status.idle":"2021-06-29T19:42:17.179305Z","shell.execute_reply.started":"2021-06-29T19:42:15.299457Z","shell.execute_reply":"2021-06-29T19:42:17.178582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(pydicom.read_file(example_case).pixel_array.flatten(), bins=80, color='c')\nplt.xlabel(\"Hounsfield Units (HU)\")\nplt.ylabel(\"Frequency\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-29T19:42:17.180797Z","iopub.execute_input":"2021-06-29T19:42:17.181209Z","iopub.status.idle":"2021-06-29T19:42:17.602458Z","shell.execute_reply.started":"2021-06-29T19:42:17.181180Z","shell.execute_reply":"2021-06-29T19:42:17.601504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#這裡的id是圖片的資料夾第一層\nimport pandas as pd\ndf_train_study=pd.read_csv('../input/siim-covid19-detection/train_study_level.csv', index_col='id')\ndf_train_study = df_train_study.idxmax(axis=1).to_frame(name='case')\ndf_train_study.index = df_train_study.index.str.replace('_study', '')\ndf_train_study","metadata":{"execution":{"iopub.status.busy":"2021-06-29T19:42:17.603774Z","iopub.execute_input":"2021-06-29T19:42:17.604102Z","iopub.status.idle":"2021-06-29T19:42:17.669399Z","shell.execute_reply.started":"2021-06-29T19:42:17.604065Z","shell.execute_reply":"2021-06-29T19:42:17.668273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_image=pd.read_csv('../input/siim-covid19-detection/train_image_level.csv', index_col='id')\n#df_train_image = df_train_image.merge(df_train_study, left_on='StudyInstanceUID', right_index=True)\ndf_train_image","metadata":{"execution":{"iopub.status.busy":"2021-06-29T19:42:33.595542Z","iopub.execute_input":"2021-06-29T19:42:33.595909Z","iopub.status.idle":"2021-06-29T19:42:33.654784Z","shell.execute_reply.started":"2021-06-29T19:42:33.595857Z","shell.execute_reply":"2021-06-29T19:42:33.653726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_image = df_train_image.merge(df_train_study, left_on='StudyInstanceUID', right_index=True)\ndf_train_image","metadata":{"execution":{"iopub.status.busy":"2021-06-29T19:42:50.684810Z","iopub.execute_input":"2021-06-29T19:42:50.685208Z","iopub.status.idle":"2021-06-29T19:42:50.715165Z","shell.execute_reply.started":"2021-06-29T19:42:50.685176Z","shell.execute_reply":"2021-06-29T19:42:50.714367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"接下來是要解析Bounding Boxes","metadata":{}},{"cell_type":"code","source":"boxes = []\nfor i, row in df_train_image.iterrows():\n    value = row['boxes']\n    if isinstance(value, float): # nan\n        #print('x', end='')\n        continue\n    values = eval(value)\n    #print('.', end='')\n    for val in values:\n        box = {\n            'image': i,\n            'study': row['StudyInstanceUID'],\n            'case': row['case'],\n            'xmin': val['x'],\n            'ymin': val['y'],\n            'xmax': val['x'] + val['width'],\n            'ymax': val['y'] + val['height'],\n            'width': val['width'],\n            'height': val['height'],\n        }\n        boxes.append(box)\nboxes = pd.DataFrame(boxes)\nprint(len(boxes))\nboxes.head()\n","metadata":{"execution":{"iopub.status.busy":"2021-06-29T19:48:29.149371Z","iopub.execute_input":"2021-06-29T19:48:29.149776Z","iopub.status.idle":"2021-06-29T19:48:30.025738Z","shell.execute_reply.started":"2021-06-29T19:48:29.149740Z","shell.execute_reply":"2021-06-29T19:48:30.024767Z"},"trusted":true},"execution_count":null,"outputs":[]}]}