{"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":"raw","source":"# Mammography Breast Cancer Detection","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"!pip install -qU \"python-gdcm\" pydicom pylibjpeg \"opencv-python-headless\"","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-12-04T12:18:11.652719Z","iopub.execute_input":"2022-12-04T12:18:11.653212Z","iopub.status.idle":"2022-12-04T12:18:32.2405Z","shell.execute_reply.started":"2022-12-04T12:18:11.653115Z","shell.execute_reply":"2022-12-04T12:18:32.239421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\n\nimport os\nimport glob\nimport numpy as np\nimport pydicom\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom tqdm.auto import tqdm\n\nPATH_DATASET = \"/kaggle/input/rsna-breast-cancer-detection/train_images\"\nPATH_CONVERT = \"/kaggle/working/train_images\"","metadata":{"execution":{"iopub.status.busy":"2022-12-04T12:18:32.243566Z","iopub.execute_input":"2022-12-04T12:18:32.244059Z","iopub.status.idle":"2022-12-04T12:18:32.52356Z","shell.execute_reply.started":"2022-12-04T12:18:32.244008Z","shell.execute_reply":"2022-12-04T12:18:32.522402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls_image = glob.glob(os.path.join(PATH_DATASET, \"*\", \"*.dcm\"))\nprint(f\"found images: {len(ls_image)}\")","metadata":{"execution":{"iopub.status.busy":"2022-12-04T12:18:32.524673Z","iopub.execute_input":"2022-12-04T12:18:32.525029Z","iopub.status.idle":"2022-12-04T12:19:00.038508Z","shell.execute_reply.started":"2022-12-04T12:18:32.525001Z","shell.execute_reply":"2022-12-04T12:19:00.037259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load sample images","metadata":{}},{"cell_type":"code","source":"dicom = pydicom.dcmread(ls_image[0])\nprint(dicom)","metadata":{"execution":{"iopub.status.busy":"2022-12-04T12:19:00.040179Z","iopub.execute_input":"2022-12-04T12:19:00.041003Z","iopub.status.idle":"2022-12-04T12:19:00.168472Z","shell.execute_reply.started":"2022-12-04T12:19:00.040967Z","shell.execute_reply":"2022-12-04T12:19:00.167583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"if you want to see reason why to se windowing, see following discussion: https://www.kaggle.com/competitions/rsna-breast-cancer-detection/discussion/369815","metadata":{}},{"cell_type":"code","source":"import cv2\nfrom PIL import Image\nfrom pydicom.pixel_data_handlers import apply_windowing\n\nnb_cols, nb_spls = 5, 10\nnp.random.shuffle(ls_image)\nfig, axarr = plt.subplots(ncols=nb_cols, nrows=nb_spls // nb_cols,\n                          figsize=(4 * nb_cols, 5 * nb_spls / nb_cols))\nfor i, dicom_path in enumerate(ls_image[:nb_spls]):\n    dicom = pydicom.dcmread(dicom_path)\n    if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(dicom.pixel_array) - dicom.pixel_array\n    else:\n        data = dicom.pixel_array\n    img = apply_windowing(data, dicom)\n    print(img.shape, img.min(), img.max())\n    img = (img.astype(float) - img.min()) / (img.max() - img.min())\n    axarr[i // nb_cols, i % nb_cols].imshow(img, cmap=\"gray\")\n    axarr[i // nb_cols, i % nb_cols].set_axis_off()\nfig.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-12-04T12:19:00.170289Z","iopub.execute_input":"2022-12-04T12:19:00.171259Z","iopub.status.idle":"2022-12-04T12:19:36.704692Z","shell.execute_reply.started":"2022-12-04T12:19:00.171225Z","shell.execute_reply":"2022-12-04T12:19:36.703663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Convert images","metadata":{}},{"cell_type":"code","source":"def convert_dicom(dicom_path, output_dir, img_size: int = 1024):\n    dicom = pydicom.dcmread(dicom_path)\n    if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(dicom.pixel_array) - dicom.pixel_array\n    else:\n        data = dicom.pixel_array\n    img = apply_windowing(data, dicom)\n    img = (img.astype(float) - img.min()) / (img.max() - img.min())\n    img = Image.fromarray((img * 255).astype(np.uint8))\n\n    img_name, _ = os.path.splitext(os.path.basename(dicom_path))\n    img_dir = os.path.basename(os.path.dirname(dicom_path))\n    png_path = os.path.join(output_dir, img_dir, f\"{img_name}.png\")\n    os.makedirs(os.path.dirname(png_path), exist_ok=True)\n    # plt.imsave(png_path, (img * 255).astype(np.uint8))\n\n    img.thumbnail((img_size, img_size))\n    img.save(png_path)","metadata":{"execution":{"iopub.status.busy":"2022-12-04T12:19:36.706169Z","iopub.execute_input":"2022-12-04T12:19:36.706921Z","iopub.status.idle":"2022-12-04T12:19:36.717523Z","shell.execute_reply.started":"2022-12-04T12:19:36.706879Z","shell.execute_reply":"2022-12-04T12:19:36.716415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Process all images","metadata":{}},{"cell_type":"code","source":"from joblib import Parallel, delayed\n\n# ! rm -rf train_images\n# ! mkdir train_images\n\n_= Parallel(n_jobs=5)(\n    delayed(convert_dicom)(p_img, output_dir=PATH_CONVERT, img_size=720)\n    for p_img in tqdm(ls_image)\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-04T12:19:36.718645Z","iopub.execute_input":"2022-12-04T12:19:36.719586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Show some converted","metadata":{}},{"cell_type":"code","source":"ls_image = glob.glob(os.path.join(PATH_CONVERT, \"*\", \"*.png\"))\nnp.random.shuffle(ls_image)\n\nnb_spls = 6\n_, axarr = plt.subplots(ncols=2, nrows=nb_spls // 2, figsize=(10, 6 * nb_spls / 2))\nfor i, img_path in enumerate(ls_image[:nb_spls]):\n    img = plt.imread(img_path)\n    print(img.shape, img.min(), img.max())\n    axarr[i // 2, i % 2].imshow(img, cmap=\"gray\")\n    axarr[i // 2, i % 2].set_axis_off()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}