{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import joblib\nimport PIL\nfrom glob import glob\nimport pydicom\nimport numpy as np\nimport pandas as pd\nimport os\nimport cv2\nimport json\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom collections import Counter\nfrom PIL import Image\nimport math\nimport seaborn as sns\nfrom collections import defaultdict\nfrom pathlib import Path\nimport cv2\nfrom tqdm import tqdm\nimport re\nimport logging as l\nfrom glob import glob\nimport argparse\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_first_of_dicom_field_as_int(x):\n    if type(x) == pydicom.multival.MultiValue:\n        return int(x[0])\n    return int(x)\n\ndef get_id(img_dicom):\n    return str(img_dicom.SOPInstanceUID)\n\ndef get_metadata_from_dicom(img_dicom):\n    metadata = {\n        \"window_center\": img_dicom.WindowCenter,\n        \"window_width\": img_dicom.WindowWidth,\n        \"intercept\": img_dicom.RescaleIntercept,\n        \"slope\": img_dicom.RescaleSlope,\n    }\n    return {k: get_first_of_dicom_field_as_int(v) for k, v in metadata.items()}\n\ndef window_image(img, window_center, window_width, intercept, slope):\n    img = img * slope + intercept\n    img_min = window_center - window_width // 2\n    img_max = window_center + window_width // 2\n    img[img < img_min] = img_min\n    img[img > img_max] = img_max\n    return img \n\ndef resize(img, new_w, new_h):\n    img = PIL.Image.fromarray(img.astype(np.int8), mode=\"L\")\n    return img.resize((new_w, new_h), resample=PIL.Image.BICUBIC)\n\ndef save_img(img_pil, subfolder, name):\n    img_pil.save(subfolder+name+'.png')\n\ndef normalize_minmax(img):\n    mi, ma = img.min(), img.max()\n    return (img - mi) / (ma - mi)\n\ndef prepare_image(img_path):\n    img_dicom = pydicom.read_file(img_path)\n    img_id = get_id(img_dicom)\n    metadata = get_metadata_from_dicom(img_dicom)\n    img = window_image(img_dicom.pixel_array, **metadata)\n    img = normalize_minmax(img) * 255\n    img = PIL.Image.fromarray(img.astype(np.int8), mode=\"L\")\n    return img_id, img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/rsna-str-pulmonary-embolism-detection/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_path = Path('../input/rsna-str-pulmonary-embolism-detection/train')\nimg_path = (base_path/train_df['StudyInstanceUID'][4]/train_df['SeriesInstanceUID'][4]/(train_df['SOPInstanceUID'][4]+'.dcm'))\nimg_data = prepare_image(img_path)\nplt.imshow(img_data[1])\n\nprint(np.asarray(img_data[1]).min(), np.asarray(img_data[1]).max())","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}