{"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 gdcm -c conda-forge -y","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-06-06T17:18:55.782378Z","iopub.execute_input":"2021-06-06T17:18:55.782988Z","iopub.status.idle":"2021-06-06T17:19:44.481408Z","shell.execute_reply.started":"2021-06-06T17:18:55.782879Z","shell.execute_reply":"2021-06-06T17:19:44.480553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-06-06T17:19:44.482948Z","iopub.execute_input":"2021-06-06T17:19:44.483197Z","iopub.status.idle":"2021-06-06T17:19:44.492611Z","shell.execute_reply.started":"2021-06-06T17:19:44.48317Z","shell.execute_reply":"2021-06-06T17:19:44.49152Z"},"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-06-06T17:19:44.494197Z","iopub.execute_input":"2021-06-06T17:19:44.494485Z","iopub.status.idle":"2021-06-06T17:19:44.747402Z","shell.execute_reply.started":"2021-06-06T17:19:44.494457Z","shell.execute_reply":"2021-06-06T17:19:44.746522Z"},"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-06-06T17:19:44.748711Z","iopub.execute_input":"2021-06-06T17:19:44.74896Z","iopub.status.idle":"2021-06-06T17:19:44.75312Z","shell.execute_reply.started":"2021-06-06T17:19:44.748933Z","shell.execute_reply":"2021-06-06T17:19:44.752371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = []\ndim0 = []\ndim1 = []\nsplits = []\nIMG_SIZE = 512\nIMG_format = 'png'\n\nfor split in ['test', 'train']:\n    save_dir = f'/kaggle/tmp/{split}/'\n\n    os.makedirs(save_dir, exist_ok=True)\n    \n    for 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=IMG_SIZE)  \n            im.save(os.path.join(save_dir, file.replace('dcm', IMG_format)))\n\n            image_id.append(file.replace('.dcm', ''))\n            dim0.append(xray.shape[0])\n            dim1.append(xray.shape[1])\n            splits.append(split)","metadata":{"execution":{"iopub.status.busy":"2021-06-06T17:19:45.035132Z","iopub.execute_input":"2021-06-06T17:19:45.03535Z","iopub.status.idle":"2021-06-06T17:22:52.985549Z","shell.execute_reply.started":"2021-06-06T17:19:45.035328Z","shell.execute_reply":"2021-06-06T17:22:52.983615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n!tar -zcf train.tar.gz -C \"/kaggle/tmp/train/\" .\n!tar -zcf test.tar.gz -C \"/kaggle/tmp/test/\" .","metadata":{"execution":{"iopub.status.busy":"2021-06-06T17:22:52.987151Z","iopub.status.idle":"2021-06-06T17:22:52.987779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame.from_dict({'image_id': image_id, 'dim0': dim0, 'dim1': dim1, 'split': splits})\ndf.to_csv('meta.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-06T17:22:52.988585Z","iopub.status.idle":"2021-06-06T17:22:52.989076Z"},"trusted":true},"execution_count":null,"outputs":[]}]}