{"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":"!cp ../input/gdcm-conda-install/gdcm.tar .\n!tar -xvzf gdcm.tar\n!conda install --offline ./gdcm/gdcm-2.8.9-py37h71b2a6d_0.tar.bz2\nimport gdcm\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nfrom pathlib import Path\nimport glob\nfrom PIL import Image\nfrom joblib import Parallel, delayed\nfrom tqdm import tqdm\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport tensorflow as tf\nimport tensorflow_io as tfio","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-02T10:23:10.230121Z","iopub.execute_input":"2022-12-02T10:23:10.230785Z","iopub.status.idle":"2022-12-02T10:23:10.240833Z","shell.execute_reply.started":"2022-12-02T10:23:10.230736Z","shell.execute_reply":"2022-12-02T10:23:10.238307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fp = '../input/rsna-breast-cancer-detection/train_images/10006/462822612.dcm'","metadata":{"execution":{"iopub.status.busy":"2022-12-02T10:22:28.514886Z","iopub.execute_input":"2022-12-02T10:22:28.516519Z","iopub.status.idle":"2022-12-02T10:22:28.522586Z","shell.execute_reply.started":"2022-12-02T10:22:28.516465Z","shell.execute_reply":"2022-12-02T10:22:28.521109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# pydicom read pixel array","metadata":{}},{"cell_type":"code","source":"%%time\npydicom_img = pydicom.dcmread(fp).pixel_array\npydicom_img.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-02T10:56:34.309447Z","iopub.execute_input":"2022-12-02T10:56:34.309999Z","iopub.status.idle":"2022-12-02T10:56:36.548408Z","shell.execute_reply.started":"2022-12-02T10:56:34.309958Z","shell.execute_reply":"2022-12-02T10:56:36.54655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# tfio read dicom","metadata":{}},{"cell_type":"code","source":"%%time\nimage_bytes = tf.io.read_file(fp)\ntfio_image = 65535 - tfio.image.decode_dicom_image(image_bytes, scale='auto', dtype=tf.uint16).numpy().squeeze()\ntfio_image.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-02T10:56:36.553533Z","iopub.execute_input":"2022-12-02T10:56:36.55402Z","iopub.status.idle":"2022-12-02T10:56:38.254897Z","shell.execute_reply.started":"2022-12-02T10:56:36.553983Z","shell.execute_reply":"2022-12-02T10:56:38.253645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# tfio read scaled to 255 (lossy on error) dicom","metadata":{}},{"cell_type":"code","source":"%%time\nimage_bytes = tf.io.read_file(fp)\ntfio_lossy_image = 255 - tfio.image.decode_dicom_image(image_bytes, scale='auto', on_error='lossy', dtype=tf.uint8).numpy().squeeze()\ntfio_lossy_image.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-02T10:56:38.256751Z","iopub.execute_input":"2022-12-02T10:56:38.257109Z","iopub.status.idle":"2022-12-02T10:56:39.884547Z","shell.execute_reply.started":"2022-12-02T10:56:38.257078Z","shell.execute_reply":"2022-12-02T10:56:39.883404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# maximum values","metadata":{}},{"cell_type":"code","source":"print (pydicom_img.max())\nprint (tfio_image.max())\nprint (tfio_lossy_image.max())","metadata":{"execution":{"iopub.status.busy":"2022-12-02T10:56:39.886728Z","iopub.execute_input":"2022-12-02T10:56:39.887077Z","iopub.status.idle":"2022-12-02T10:56:39.958626Z","shell.execute_reply.started":"2022-12-02T10:56:39.887046Z","shell.execute_reply":"2022-12-02T10:56:39.95709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nfig, axes = plt.subplots(1,3, figsize=(10,10))\naxes[0].imshow(pydicom_img, cmap='gray')\naxes[0].set_title('image')\naxes[1].imshow(tfio_image, cmap='gray')\naxes[1].set_title('tfio image');\naxes[2].imshow(tfio_lossy_image, cmap='gray')\naxes[2].set_title('tfio image scaled');","metadata":{"execution":{"iopub.status.busy":"2022-12-02T10:56:42.018779Z","iopub.execute_input":"2022-12-02T10:56:42.019214Z","iopub.status.idle":"2022-12-02T10:56:47.158209Z","shell.execute_reply.started":"2022-12-02T10:56:42.01918Z","shell.execute_reply":"2022-12-02T10:56:47.156671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pydicom Read and Crop","metadata":{}},{"cell_type":"code","source":"%%time\npydicom_img = pydicom.dcmread(fp).pixel_array\npydicom_img = pydicom_img[:,pydicom_img.std(axis=0) > 60]\npydicom_img = pydicom_img[pydicom_img.std(axis=1) > 60, :]\npydicom_img.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-02T10:57:56.072238Z","iopub.execute_input":"2022-12-02T10:57:56.072739Z","iopub.status.idle":"2022-12-02T10:57:58.302994Z","shell.execute_reply.started":"2022-12-02T10:57:56.072702Z","shell.execute_reply":"2022-12-02T10:57:58.301776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# TFIO Read and Crop","metadata":{}},{"cell_type":"code","source":"%%time\nimage_bytes = tf.io.read_file(fp)\ntfio_image = 65535 - tfio.image.decode_dicom_image(image_bytes, scale='auto', dtype=tf.uint16).numpy().squeeze()\ntfio_image = tfio_image[:,tfio_image.std(axis=0) > 60]\ntfio_image = tfio_image[tfio_image.std(axis=1) > 60, :]\ntfio_image.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-02T10:58:54.612731Z","iopub.execute_input":"2022-12-02T10:58:54.613274Z","iopub.status.idle":"2022-12-02T10:58:56.455316Z","shell.execute_reply.started":"2022-12-02T10:58:54.613218Z","shell.execute_reply":"2022-12-02T10:58:56.454082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# TFIO Scaled Read and Crop","metadata":{}},{"cell_type":"code","source":"%%time\nimage_bytes = tf.io.read_file(fp)\ntfio_lossy_image = 255 - tfio.image.decode_dicom_image(image_bytes, scale='auto', on_error='lossy', dtype=tf.uint8).numpy().squeeze()\ntfio_lossy_image = tfio_lossy_image[:,tfio_lossy_image.std(axis=0) > 0.5]\ntfio_lossy_image = tfio_lossy_image[tfio_lossy_image.std(axis=1) > 0.5, :]\ntfio_lossy_image.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-02T10:58:56.457365Z","iopub.execute_input":"2022-12-02T10:58:56.4579Z","iopub.status.idle":"2022-12-02T10:58:58.306963Z","shell.execute_reply.started":"2022-12-02T10:58:56.457864Z","shell.execute_reply":"2022-12-02T10:58:58.305113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nfig, axes = plt.subplots(1,3, figsize=(10,10))\naxes[0].imshow(pydicom_img, cmap='gray')\naxes[0].set_title('image')\naxes[1].imshow(tfio_image, cmap='gray')\naxes[1].set_title('tfio image');\naxes[2].imshow(tfio_lossy_image, cmap='gray')\naxes[2].set_title('tfio lossy image');","metadata":{"execution":{"iopub.status.busy":"2022-12-02T10:58:58.309234Z","iopub.execute_input":"2022-12-02T10:58:58.309914Z","iopub.status.idle":"2022-12-02T10:59:00.726142Z","shell.execute_reply.started":"2022-12-02T10:58:58.309854Z","shell.execute_reply":"2022-12-02T10:59:00.724392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}