{"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":{"execution":{"iopub.status.busy":"2021-07-06T23:36:20.378237Z","iopub.execute_input":"2021-07-06T23:36:20.378865Z","iopub.status.idle":"2021-07-06T23:36:48.267202Z","shell.execute_reply.started":"2021-07-06T23:36:20.378767Z","shell.execute_reply":"2021-07-06T23:36:48.266342Z"},"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-07-06T23:36:56.244338Z","iopub.execute_input":"2021-07-06T23:36:56.244694Z","iopub.status.idle":"2021-07-06T23:36:56.25256Z","shell.execute_reply.started":"2021-07-06T23:36:56.244652Z","shell.execute_reply":"2021-07-06T23:36:56.251516Z"},"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    dicom = pydicom.read_file(path)\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n    # MONOCHROME1 MONOCHROME2 RGB PALETTE COLOR \n\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.max(data) - data\n    \n    # image array에서 최솟값이 0이되도록 빼주고 가장 큰 값으로 다 나눠서 0-1사이의 값으로 변환후, 255를 곱해준다\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-06T23:36:57.957515Z","iopub.execute_input":"2021-07-06T23:36:57.957835Z","iopub.status.idle":"2021-07-06T23:36:58.076419Z","shell.execute_reply.started":"2021-07-06T23:36:57.957805Z","shell.execute_reply":"2021-07-06T23:36:58.075356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize(array, size, keep_ratio = False, resample=Image.LANCZOS):\n    im = Image.fromarray(array)\n    if keep_ratio:\n        # creates nice thumbnails of all JPEG images in the current directory preserving aspect ratios with 128x128 max resolution.\n        im.thumbnail((size, size), resample)\n    else:\n        im = im.resize((size, size), resample)\n    return im\n           ","metadata":{"execution":{"iopub.status.busy":"2021-07-06T23:36:59.726666Z","iopub.execute_input":"2021-07-06T23:36:59.726992Z","iopub.status.idle":"2021-07-06T23:36:59.732843Z","shell.execute_reply.started":"2021-07-06T23:36:59.726962Z","shell.execute_reply":"2021-07-06T23:36:59.731562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/siim-covid19-detection/train_image_level.csv\")\ntrain","metadata":{"execution":{"iopub.status.busy":"2021-07-06T23:37:01.783723Z","iopub.execute_input":"2021-07-06T23:37:01.784069Z","iopub.status.idle":"2021-07-06T23:37:01.833846Z","shell.execute_reply.started":"2021-07-06T23:37:01.784035Z","shell.execute_reply":"2021-07-06T23:37:01.832893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"../input/siim-covid19-detection/train/00086460a852/9e8302230c91/65761e66de9f.dcm\"\ndicom = pydicom.read_file(path)\nprint(dicom.pixel_array.shape)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-06T23:37:07.559971Z","iopub.execute_input":"2021-07-06T23:37:07.560359Z","iopub.status.idle":"2021-07-06T23:37:08.115266Z","shell.execute_reply.started":"2021-07-06T23:37:07.560325Z","shell.execute_reply":"2021-07-06T23:37:08.114091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"split = 'train'\nsave_dir = f'/kaggle/tmp/{split}/'\n\nos.makedirs(save_dir, exist_ok=True)\nsave_dir = f'/kaggle/tmp/{split}/image/'\nos.makedirs(save_dir, exist_ok=True)\n\nId = []\ndim0 = []\ndim1 = []\n\nfor dirname, _, filenames in tqdm(os.walk(f'../input/siim-covid19-detection/{split}')):\n\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        \n        dim0.append(xray.shape[0])\n        dim1.append(xray.shape[1])\n        Id.append(file.split(\".\")[0])\n             \n        im = resize(xray, size=512)\n    \n        im.save(os.path.join(save_dir, file.replace('.dcm', '_image.png')))","metadata":{"execution":{"iopub.status.busy":"2021-07-06T23:37:13.142538Z","iopub.execute_input":"2021-07-06T23:37:13.142889Z","iopub.status.idle":"2021-07-07T00:31:25.020689Z","shell.execute_reply.started":"2021-07-06T23:37:13.142856Z","shell.execute_reply":"2021-07-07T00:31:25.017633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tar -zcf image.tar.gz -C \"/kaggle/tmp/train/image/\" .","metadata":{"execution":{"iopub.status.busy":"2021-07-07T00:50:03.181509Z","iopub.execute_input":"2021-07-07T00:50:03.181930Z","iopub.status.idle":"2021-07-07T00:50:35.464860Z","shell.execute_reply.started":"2021-07-07T00:50:03.181884Z","shell.execute_reply":"2021-07-07T00:50:35.463455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a href=\"./image.tar.gz\"> Download File_image </a>","metadata":{}},{"cell_type":"code","source":"df_meta = pd.DataFrame({\"id\":Id, \"dim0\":dim0, \"dim1\":dim1})\ndf_meta['split'] = \"train\"\ndf_meta","metadata":{"execution":{"iopub.status.busy":"2021-07-07T00:39:08.043735Z","iopub.execute_input":"2021-07-07T00:39:08.044095Z","iopub.status.idle":"2021-07-07T00:39:08.080574Z","shell.execute_reply.started":"2021-07-07T00:39:08.044057Z","shell.execute_reply":"2021-07-07T00:39:08.079433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_meta.to_csv('metadf.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-07T04:03:54.290739Z","iopub.execute_input":"2021-07-07T04:03:54.291093Z","iopub.status.idle":"2021-07-07T04:03:54.353806Z","shell.execute_reply.started":"2021-07-07T04:03:54.291056Z","shell.execute_reply":"2021-07-07T04:03:54.352665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a href=\"./metadf.csv\"> Download File_image </a>","metadata":{}},{"cell_type":"code","source":"save_dir = f\"/kaggle/tmp/{split}/study/\"\nos.makedirs(save_dir, exist_ok = True)\n\n\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))","metadata":{"execution":{"iopub.status.busy":"2021-07-07T00:50:53.100336Z","iopub.execute_input":"2021-07-07T00:50:53.100732Z","iopub.status.idle":"2021-07-07T01:48:16.837299Z","shell.execute_reply.started":"2021-07-07T00:50:53.100676Z","shell.execute_reply":"2021-07-07T01:48:16.835786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tar -zcf study.tar.gz -C \"/kaggle/tmp/train/study/\" .","metadata":{"execution":{"iopub.status.busy":"2021-07-07T04:03:31.815664Z","iopub.execute_input":"2021-07-07T04:03:31.816132Z","iopub.status.idle":"2021-07-07T04:03:32.585196Z","shell.execute_reply.started":"2021-07-07T04:03:31.816056Z","shell.execute_reply":"2021-07-07T04:03:32.584020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n<a href=\"./study.tar.gz\"> Download File_study </a>","metadata":{}}]}