{"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 -c conda-forge gdcm -y\n!tar -xvf ../input/gdcm-conda-install/gdcm.tar\n!conda install ../working/gdcm/conda-4.8.4-py37hc8dfbb8_2.tar.bz2\n!conda install ../working/gdcm/gdcm-2.8.9-py37h71b2a6d_0.tar.bz2\n!conda install ../working/gdcm/libjpeg-turbo-2.0.3-h516909a_1.tar.bz2","metadata":{"execution":{"iopub.status.busy":"2021-05-20T03:40:40.946508Z","iopub.execute_input":"2021-05-20T03:40:40.946965Z","iopub.status.idle":"2021-05-20T03:41:32.527144Z","shell.execute_reply.started":"2021-05-20T03:40:40.946859Z","shell.execute_reply":"2021-05-20T03:41:32.526266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import display_html\n# def restartkernel() :\ndisplay_html(\"<script>Jupyter.notebook.kernel.restart()</script>\")\n# restartkernel()\n\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T03:41:32.531986Z","iopub.execute_input":"2021-05-20T03:41:32.532315Z","iopub.status.idle":"2021-05-20T03:41:32.538220Z","shell.execute_reply.started":"2021-05-20T03:41:32.532286Z","shell.execute_reply":"2021-05-20T03:41:32.537277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nfrom tqdm import tqdm\nfrom glob import glob\nimport os\nimport matplotlib.pyplot as plt\nimport gdcm\nimport pydicom as dicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport pydicom\nimport cv2\nfrom PIL import Image","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-05-20T04:16:13.559578Z","iopub.execute_input":"2021-05-20T04:16:13.559953Z","iopub.status.idle":"2021-05-20T04:16:13.566813Z","shell.execute_reply.started":"2021-05-20T04:16:13.559918Z","shell.execute_reply":"2021-05-20T04:16:13.565232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = \"../input/siim-covid19-detection\"\nIM_SIZE = 512","metadata":{"execution":{"iopub.status.busy":"2021-05-20T03:42:05.784777Z","iopub.execute_input":"2021-05-20T03:42:05.785112Z","iopub.status.idle":"2021-05-20T03:42:05.788923Z","shell.execute_reply.started":"2021-05-20T03:42:05.785082Z","shell.execute_reply":"2021-05-20T03:42:05.788185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_study_level = pd.read_csv(os.path.join(data_dir, \"train_study_level.csv\"))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T03:42:08.553477Z","iopub.execute_input":"2021-05-20T03:42:08.553961Z","iopub.status.idle":"2021-05-20T03:42:08.578473Z","shell.execute_reply.started":"2021-05-20T03:42:08.553930Z","shell.execute_reply":"2021-05-20T03:42:08.577375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_study_level.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T03:42:11.351841Z","iopub.execute_input":"2021-05-20T03:42:11.352202Z","iopub.status.idle":"2021-05-20T03:42:11.385439Z","shell.execute_reply.started":"2021-05-20T03:42:11.352162Z","shell.execute_reply":"2021-05-20T03:42:11.384153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dicom data","metadata":{}},{"cell_type":"code","source":"train_df = train_study_level","metadata":{"execution":{"iopub.status.busy":"2021-05-20T03:42:13.420961Z","iopub.execute_input":"2021-05-20T03:42:13.421471Z","iopub.status.idle":"2021-05-20T03:42:13.426197Z","shell.execute_reply.started":"2021-05-20T03:42:13.421422Z","shell.execute_reply":"2021-05-20T03:42:13.425432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_paths = []\n\nfor sid in tqdm(train_df['id']):\n    path = glob(os.path.join(data_dir, 'train' , sid.split(\"_\")[0] + \"/*/*\" ))[0]\n    training_paths.append(path)\n\ntrain_df['path'] = training_paths","metadata":{"execution":{"iopub.status.busy":"2021-05-20T03:42:15.176514Z","iopub.execute_input":"2021-05-20T03:42:15.176890Z","iopub.status.idle":"2021-05-20T03:42:35.549095Z","shell.execute_reply.started":"2021-05-20T03:42:15.176855Z","shell.execute_reply":"2021-05-20T03:42:35.548166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dicom2array(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\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\n\n\nimages = []\nfor filename in tqdm(train_df.path):\n    im = dicom2array(filename)\n    images.append(cv2.resize(im, (IM_SIZE, IM_SIZE)))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T03:42:38.950623Z","iopub.execute_input":"2021-05-20T03:42:38.950985Z","iopub.status.idle":"2021-05-20T04:15:27.078056Z","shell.execute_reply.started":"2021-05-20T03:42:38.950954Z","shell.execute_reply":"2021-05-20T04:15:27.076219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(\"train_images\", exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T04:15:49.188504Z","iopub.execute_input":"2021-05-20T04:15:49.189017Z","iopub.status.idle":"2021-05-20T04:15:49.206514Z","shell.execute_reply.started":"2021-05-20T04:15:49.188984Z","shell.execute_reply":"2021-05-20T04:15:49.205121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, idx in tqdm(enumerate(train_df.id)):\n    Image.fromarray(images[i]).save(os.path.join(\"train_images\", str(idx) + \".jpg\"))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T04:16:19.680720Z","iopub.execute_input":"2021-05-20T04:16:19.681111Z","iopub.status.idle":"2021-05-20T04:16:50.735531Z","shell.execute_reply.started":"2021-05-20T04:16:19.681080Z","shell.execute_reply":"2021-05-20T04:16:50.734692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tar -czf train_images.tar.gz train_images\n!du -h train_images.tar.gz","metadata":{"execution":{"iopub.status.busy":"2021-05-20T04:17:08.556799Z","iopub.execute_input":"2021-05-20T04:17:08.557370Z","iopub.status.idle":"2021-05-20T04:17:15.646194Z","shell.execute_reply.started":"2021-05-20T04:17:08.557315Z","shell.execute_reply":"2021-05-20T04:17:15.645097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\nFileLink(r'train_images.tar.gz')","metadata":{"execution":{"iopub.status.busy":"2021-05-20T04:17:46.430027Z","iopub.execute_input":"2021-05-20T04:17:46.430433Z","iopub.status.idle":"2021-05-20T04:17:46.437239Z","shell.execute_reply.started":"2021-05-20T04:17:46.430396Z","shell.execute_reply":"2021-05-20T04:17:46.436218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}