{"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":"!pip install -qU python-gdcm pydicom pylibjpeg dicomsdl","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-12-25T22:11:00.145762Z","iopub.execute_input":"2022-12-25T22:11:00.14644Z","iopub.status.idle":"2022-12-25T22:11:11.511607Z","shell.execute_reply.started":"2022-12-25T22:11:00.146403Z","shell.execute_reply":"2022-12-25T22:11:11.510374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport pydicom\nimport dicomsdl\nimport cv2\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-12-25T22:11:11.513918Z","iopub.execute_input":"2022-12-25T22:11:11.514287Z","iopub.status.idle":"2022-12-25T22:11:11.550773Z","shell.execute_reply.started":"2022-12-25T22:11:11.514252Z","shell.execute_reply":"2022-12-25T22:11:11.549553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_longest_edge_to(img, longest_edge_size = 512):\n    h, w = img.shape\n    if h > w:\n        new_h = longest_edge_size\n        new_w = int(w * new_h / h)\n    else:\n        new_w = longest_edge_size\n        new_h = int(h * new_w / w)\n    return cv2.resize(img, dsize=(new_w, new_h), interpolation=cv2.INTER_LINEAR)\n\nnorm = lambda img: ((img - img.min()) / (img.max() - img.min()) * 255).astype(np.uint8)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-12-25T22:11:11.553544Z","iopub.execute_input":"2022-12-25T22:11:11.5539Z","iopub.status.idle":"2022-12-25T22:11:11.561818Z","shell.execute_reply.started":"2022-12-25T22:11:11.553864Z","shell.execute_reply":"2022-12-25T22:11:11.560635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p_ids = [27770, 45629, 822, 27744, 32362, 38739]\ndcm_dir = '/kaggle/input/rsna-breast-cancer-detection/train_images'\n\ndf = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv').query('patient_id in @p_ids')\n\ndf.head()","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-12-25T22:11:11.564584Z","iopub.execute_input":"2022-12-25T22:11:11.565043Z","iopub.status.idle":"2022-12-25T22:11:11.659382Z","shell.execute_reply.started":"2022-12-25T22:11:11.565Z","shell.execute_reply":"2022-12-25T22:11:11.658415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, r in df.iterrows():\n    dcm_fn = f'{dcm_dir}/{r.patient_id}/{r.image_id}.dcm'\n    dcm_mt = pydicom.dcmread(dcm_fn)\n    \n    pyimg = dcm_mt.pixel_array\n    pyimg = norm(pyimg)\n    \n    diimg = dicomsdl.open(dcm_fn).pixelData()\n    diimg = norm(diimg)\n             \n    if dcm_mt.PhotometricInterpretation == 'MONOCHROME1':\n        pyimg = 1 - pyimg\n        diimg = 1 - diimg\n        \n    pyimg = resize_longest_edge_to(pyimg)\n    diimg = resize_longest_edge_to(diimg)\n        \n    fig, ax = plt.subplots(1, 2, figsize = (20, 10))\n    \n    fig.suptitle(f'p_id: {r.patient_id}, i_id: {r.image_id}, cancer: {r.cancer}')\n    \n    ax[0].set_title('pydicom')\n    ax[0].imshow(pyimg)\n    \n    ax[1].set_title('dicomsdl')\n    ax[1].imshow(diimg)\n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-25T22:12:41.644708Z","iopub.execute_input":"2022-12-25T22:12:41.645093Z"},"trusted":true},"execution_count":null,"outputs":[]}]}