{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n#import os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#   for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-11T04:27:56.993661Z","iopub.execute_input":"2021-07-11T04:27:56.994017Z","iopub.status.idle":"2021-07-11T04:27:56.998736Z","shell.execute_reply.started":"2021-07-11T04:27:56.993984Z","shell.execute_reply":"2021-07-11T04:27:56.997667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport os\nimport glob\nimport csv\nimport numpy as np\nimport pandas as pd\nimport cv2\nimport pydicom\nimport matplotlib.pyplot as plt\nfrom skimage.filters import unsharp_mask, meijering, sato, scharr, hessian\nfrom skimage.exposure import exposure, equalize_hist, equalize_adapthist","metadata":{"execution":{"iopub.status.busy":"2021-07-11T04:37:44.094573Z","iopub.execute_input":"2021-07-11T04:37:44.094929Z","iopub.status.idle":"2021-07-11T04:37:44.102056Z","shell.execute_reply.started":"2021-07-11T04:37:44.094898Z","shell.execute_reply":"2021-07-11T04:37:44.100930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This notebook attempts to prepare the chest scan dataset to be used to train a deep learning algorithims. There are 3 main steps in this notebook. This work is inpired by many notebooks in competetion. \n1. Enhance the images. \n2. Convert the images to png. \n3. prepare the CSV for training. ","metadata":{}},{"cell_type":"code","source":"#Step 1: Editing the images\n# Apply filters. \ndef apply_filter(f, img):\n    if f == 'equalize_hist':\n        img = equalize_hist(img, nbins=256, mask=None)\n        \n    if f == 'equalize_adapthist': \n        img = equalize_adapthist(img, kernel_size=None, clip_limit=0.01, nbins=256)\n        \n    if f == 'unsharp_mask':\n        img = unsharp_mask(img, radius=5, amount=2)\n        \n    if f == 'meijering':\n        img = meijering(img, sigmas=range(1, 10, 2), alpha=None, black_ridges=True, mode='reflect', cval=0)\n        \n    if f == 'sato':\n        img = sato(img, sigmas=range(1, 10, 2), black_ridges=True, mode='reflect', cval=0)\n        \n    if f == 'scharr':\n        img = scharr(img, mask=None, axis=None, mode='reflect', cval=0.0)\n        \n    if f == 'hessian':\n        img = hessian(img, sigmas=range(1, 10, 2), scale_range=None, scale_step=None, alpha=0.5, beta=0.5, gamma=15, black_ridges=True, mode='reflect', cval=0)\n    \n    if f == 'threshold_isodata':\n        img = threshold_isodata(image=img, nbins=256, return_all=False, hist=None)\n    \n    return img\n","metadata":{"execution":{"iopub.status.busy":"2021-07-11T04:36:58.941605Z","iopub.execute_input":"2021-07-11T04:36:58.942111Z","iopub.status.idle":"2021-07-11T04:36:58.951457Z","shell.execute_reply.started":"2021-07-11T04:36:58.942076Z","shell.execute_reply":"2021-07-11T04:36:58.950392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_path = glob.glob(\"/kaggle/input/siim-covid19-detection/train/**/*.dcm\", recursive=True)[:10]\nprint(images_path)\n\n","metadata":{"execution":{"iopub.status.busy":"2021-07-11T04:42:03.104122Z","iopub.execute_input":"2021-07-11T04:42:03.104459Z","iopub.status.idle":"2021-07-11T04:42:13.596816Z","shell.execute_reply.started":"2021-07-11T04:42:03.104430Z","shell.execute_reply":"2021-07-11T04:42:13.595683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print a sample image\nimg = pydicom.dcmread(images_path[0])\nplt.imshow(img.pixel_array,cmap=plt.cm.gray)","metadata":{"execution":{"iopub.status.busy":"2021-07-11T04:44:42.599137Z","iopub.execute_input":"2021-07-11T04:44:42.599654Z","iopub.status.idle":"2021-07-11T04:44:43.791417Z","shell.execute_reply.started":"2021-07-11T04:44:42.599609Z","shell.execute_reply":"2021-07-11T04:44:43.790401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Step 2: Convert the images to png format","metadata":{"execution":{"iopub.status.busy":"2021-07-11T04:33:09.834250Z","iopub.execute_input":"2021-07-11T04:33:09.834721Z","iopub.status.idle":"2021-07-11T04:33:09.837772Z","shell.execute_reply.started":"2021-07-11T04:33:09.834687Z","shell.execute_reply":"2021-07-11T04:33:09.837023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Step 3: preparing the csv labels. ","metadata":{"execution":{"iopub.status.busy":"2021-07-11T04:33:27.750274Z","iopub.execute_input":"2021-07-11T04:33:27.750769Z","iopub.status.idle":"2021-07-11T04:33:27.753891Z","shell.execute_reply.started":"2021-07-11T04:33:27.750736Z","shell.execute_reply":"2021-07-11T04:33:27.753090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}