{"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":"!conda install '/kaggle/input/sixth/libjpeg-turbo-2.1.0-h7f98852_0.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/sixth/libgcc-ng-9.3.0-h2828fa1_19.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/sixth/gdcm-2.8.9-py37h500ead1_1.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/sixth/conda-4.10.1-py37h89c1867_0.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/sixth/certifi-2020.12.5-py37h89c1867_1.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/sixth/openssl-1.1.1k-h7f98852_0.tar.bz2' -c conda-forge -y","metadata":{"execution":{"iopub.status.busy":"2021-07-21T01:34:32.436367Z","iopub.execute_input":"2021-07-21T01:34:32.436798Z","iopub.status.idle":"2021-07-21T01:35:54.725548Z","shell.execute_reply.started":"2021-07-21T01:34:32.436701Z","shell.execute_reply":"2021-07-21T01:35:54.724088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Makimg Test PNG files","metadata":{}},{"cell_type":"code","source":"import os\n\nfrom PIL import Image\nimport pandas as pd\nfrom tqdm.auto import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-21T01:35:54.727407Z","iopub.execute_input":"2021-07-21T01:35:54.727740Z","iopub.status.idle":"2021-07-21T01:35:54.925024Z","shell.execute_reply.started":"2021-07-21T01:35:54.727704Z","shell.execute_reply":"2021-07-21T01:35:54.923872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/siim-covid19-detection/sample_submission.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2021-07-21T01:35:54.927419Z","iopub.execute_input":"2021-07-21T01:35:54.927867Z","iopub.status.idle":"2021-07-21T01:35:54.976458Z","shell.execute_reply.started":"2021-07-21T01:35:54.927821Z","shell.execute_reply":"2021-07-21T01:35:54.975485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\ndef read_xray(path, voi_lut = True, fix_monochrome = True):\n    # Original from: https://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way\n    dicom = pydicom.read_file(path)\n    \n    # VOI LUT (if available by DICOM device) is used to transform raw DICOM data to \n    # \"human-friendly\" view\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n               \n    # depending on this value, X-ray may look inverted - fix that:\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":{"execution":{"iopub.status.busy":"2021-07-21T01:35:54.977742Z","iopub.execute_input":"2021-07-21T01:35:54.978023Z","iopub.status.idle":"2021-07-21T01:35:55.313489Z","shell.execute_reply.started":"2021-07-21T01:35:54.977997Z","shell.execute_reply":"2021-07-21T01:35:55.312575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize(array, size, keep_ratio=False, resample=Image.LANCZOS):\n    # Original from: https://www.kaggle.com/xhlulu/vinbigdata-process-and-resize-to-image\n    im = Image.fromarray(array)\n    \n    if keep_ratio:\n        im.thumbnail((size, size), resample)\n    else:\n        im = im.resize((size, size), resample)\n    \n    return im","metadata":{"execution":{"iopub.status.busy":"2021-07-21T01:35:55.314709Z","iopub.execute_input":"2021-07-21T01:35:55.315308Z","iopub.status.idle":"2021-07-21T01:35:55.322122Z","shell.execute_reply.started":"2021-07-21T01:35:55.315265Z","shell.execute_reply":"2021-07-21T01:35:55.321097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"split = 'test'\n\nsave_dir = f'/kaggle/tmp/{split}/'\nos.makedirs(save_dir, exist_ok=True)\n\nsave_dir = f'/kaggle/tmp/{split}/study/'\nos.makedirs(save_dir, exist_ok=True)\n\nfor dirname, _, filenames in tqdm(os.walk(f\"../input/siim-covid19-detection/{split}\")):\n    for file in filenames:\n        xray = read_xray(os.path.join(dirname, file))\n        im = resize(xray, size=600)\n        study = dirname.split('/')[-2]+'_study.png'\n        im.save(os.path.join(save_dir, study))\n        ","metadata":{"execution":{"iopub.status.busy":"2021-07-21T01:36:39.356110Z","iopub.execute_input":"2021-07-21T01:36:39.356480Z","iopub.status.idle":"2021-07-21T01:47:09.605275Z","shell.execute_reply.started":"2021-07-21T01:36:39.356448Z","shell.execute_reply":"2021-07-21T01:47:09.603472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = []\ndim0 = []\ndim1 = []\nsplits = []\nsave_dir = f'/kaggle/tmp/{split}/image/'\nos.makedirs(save_dir, exist_ok=True)\n\nfor dirname, _, filenames in tqdm(os.walk(f'../input/siim-covid19-detection/{split}')):\n    for file in filenames:\n        # set keep_ratio=True to have original aspect ratio\n        xray = read_xray(os.path.join(dirname, file))\n        im = resize(xray, size=512)  \n        im.save(os.path.join(save_dir, file.replace('.dcm', '_image.png')))\n        image_id.append(file.replace('.dcm', ''))\n        dim0.append(xray.shape[0])\n        dim1.append(xray.shape[1])\n        splits.append(split)\nmeta = pd.DataFrame.from_dict({'image_id': image_id, 'dim0': dim0, 'dim1': dim1, 'split': splits})","metadata":{"execution":{"iopub.status.busy":"2021-07-21T01:47:09.609214Z","iopub.execute_input":"2021-07-21T01:47:09.609610Z","iopub.status.idle":"2021-07-21T01:56:43.834762Z","shell.execute_reply.started":"2021-07-21T01:47:09.609571Z","shell.execute_reply":"2021-07-21T01:56:43.833549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tar -zcf image.tar.gz -C \"/kaggle/tmp/test/image/\" .\n!tar -zcf study.tar.gz -C \"/kaggle/tmp/test/study/\" .\nmeta.to_csv('metadf_test.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T01:59:39.089913Z","iopub.execute_input":"2021-07-21T01:59:39.090349Z","iopub.status.idle":"2021-07-21T01:59:54.993214Z","shell.execute_reply.started":"2021-07-21T01:59:39.090312Z","shell.execute_reply":"2021-07-21T01:59:54.992116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}