{"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":"import gc\n\nimport glob\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport pydicom\nimport tqdm\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2023-03-25T20:03:57.447081Z","iopub.execute_input":"2023-03-25T20:03:57.447568Z","iopub.status.idle":"2023-03-25T20:03:57.627884Z","shell.execute_reply.started":"2023-03-25T20:03:57.447464Z","shell.execute_reply":"2023-03-25T20:03:57.625922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load Dicom images\nfile = '/kaggle/input/rsna-breast-cancer-detection/train_images/10006/1459541791.dcm'\ndicom = pydicom.dcmread(file)","metadata":{"execution":{"iopub.status.busy":"2023-03-25T20:03:57.634473Z","iopub.execute_input":"2023-03-25T20:03:57.636677Z","iopub.status.idle":"2023-03-25T20:03:57.764358Z","shell.execute_reply.started":"2023-03-25T20:03:57.636598Z","shell.execute_reply":"2023-03-25T20:03:57.763157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom","metadata":{"execution":{"iopub.status.busy":"2023-03-25T20:03:57.766264Z","iopub.execute_input":"2023-03-25T20:03:57.766892Z","iopub.status.idle":"2023-03-25T20:03:57.776088Z","shell.execute_reply.started":"2023-03-25T20:03:57.766858Z","shell.execute_reply":"2023-03-25T20:03:57.775292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom.pixel_array.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-25T20:03:57.778383Z","iopub.execute_input":"2023-03-25T20:03:57.778955Z","iopub.status.idle":"2023-03-25T20:03:59.487566Z","shell.execute_reply.started":"2023-03-25T20:03:57.778908Z","shell.execute_reply":"2023-03-25T20:03:59.486279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(2, 2, figsize=(12, 12))\naxes = axes.flatten()\n\noriginal_image = dicom.pixel_array\nvoi_lut_image = apply_voi_lut(dicom.pixel_array, dicom)\n\n# Scale images\noriginal_image = (\n    (original_image - original_image.min()) / (original_image.max() - original_image.min())\n)\noriginal_image = 1 - original_image\n\nvoi_lut_image = (\n    (voi_lut_image - voi_lut_image.min()) / (voi_lut_image.max() - voi_lut_image.min())\n)\nvoi_lut_image = 1 - voi_lut_image\n\nax1 = axes[0]\nax1.imshow(original_image, cmap='jet')\n\nax2 = axes[1]\nax2.imshow(voi_lut_image, cmap='jet')\n\nax3 = axes[2]\nax3.hist(original_image.flatten())\n\nax4 = axes[3]\nax4.hist(voi_lut_image.flatten())","metadata":{"execution":{"iopub.status.busy":"2023-03-25T20:03:59.489099Z","iopub.execute_input":"2023-03-25T20:03:59.489543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from multiprocessing import Pool, cpu_count\nfrom functools import partial\n\n# One other thing we would like to do is just understand the different sizes of the images\nfiles = glob.glob('/kaggle/input/rsna-breast-cancer-detection/train_images/*/*')\nfiles = np.random.choice(files, 15000, replace=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_image_shape(shape_dict, file):\n    dicom = pydicom.dcmread(file)\n    key = (dicom.Columns, dicom.Rows)\n    \n    if shape_dict.get(key) is not None:\n        shape_dict[key] += 1\n        \n    else:\n        shape_dict[key] = 0\n        \n    del dicom, key\n    gc.collect()\n\nimage_shapes = {}\nfunction = partial(get_image_shape, image_shapes)\nwith Pool(cpu_count()) as p:\n    for x in tqdm.tqdm(p.imap(function, files)):\n        pass","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_shapes","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}