{"cells":[{"metadata":{},"cell_type":"markdown","source":"## Since Dataset Is quite large:\n### We will convert images from dicom to png and resize them.\n**We will be using joblib parallel and delayed functions to speed up the process**"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport random\nimport cv2\nimport glob2\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nfrom joblib import Parallel, delayed\nfrom tqdm import tqdm_notebook as tqdm\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport matplotlib.image as immg\nfrom pathlib import Path\nimport os\nimport gc\nimport cv2\nfrom tqdm.notebook import tqdm\nimport zipfile\nimport io\nfrom sklearn.decomposition import PCA\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tr_img_dir = Path('../input/vinbigdata-chest-xray-abnormalities-detection/train')\nts_img_dir = Path('../input/vinbigdata-chest-xray-abnormalities-detection/test')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv('../input/vinbigdata-chest-xray-abnormalities-detection/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"files = [str(x)+'.dicom' for x in df.image_id.tolist()]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## SAMPLE Image"},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_szs(x):\n    fl = pydicom.read_file(x)\n    return fl.Rows,fl.Columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#szs = Parallel(n_jobs=-1,verbose=1)(delayed(get_szs)(str(ts_img_dir)+'/'+i) for i in random.sample(files,50))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"IMAGE_SIZE = (512,512)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Thanks to https://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way\n\nFor Explaining and giving function for correct method for conversion."},{"metadata":{"trusted":true},"cell_type":"code","source":"def read_xray(path, voi_lut = True, fix_monochrome = True):\n    dicom = pydicom.read_file(path)\n    \n    # VOI LUT (if available by DICOM device) is used to transform raw DICOM data to \"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    img = cv2.resize(data, IMAGE_SIZE)\n    \n    return img","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## We will use joblib **parallel & delayed** to read and process dicom images in batches of 50. "},{"metadata":{"trusted":true},"cell_type":"code","source":"x_tot,x2_tot = [],[]\nbatch = 50\ndim0 = []\ndim1 = []\nfor idx in tqdm(range(0,len(files),batch)):\n    names = [str(ts_img_dir)+'/'+x for x in files[idx:idx+batch]]\n    out = Parallel(n_jobs=-1)(delayed(get_szs)(i) for i in names)\n    for s in range(len(out)):\n        d0,d1 = out[s][0],out[s][1]\n        dim0.append(d0)\n        dim1.append(d1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['dim0'] = dim0\ndf['dim1'] = dim1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.to_csv('vinbigdata_test_meta.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}