{"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":"markdown","source":"# Implementation Example to fix PixelRepresentation == 1\n\nThe following will showcase two scenarios:\n1. Addressing the issue of visually unusual pixel arrays in DICOM files.\n2. Illustrate that the function does not affect DICOM files that appear visually normal.\n","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport pydicom\nimport os\nimport numpy as np\nfrom matplotlib import pyplot as plt\n\ndef standardize_pixel_array(dcm: pydicom.dataset.FileDataset) -> np.ndarray:\n    # Correct DICOM pixel_array if PixelRepresentation == 1.\n    pixel_array = dcm.pixel_array\n    if dcm.PixelRepresentation == 1:\n        bit_shift = dcm.BitsAllocated - dcm.BitsStored\n        dtype = pixel_array.dtype \n        pixel_array = (pixel_array << bit_shift).astype(dtype) >>  bit_shift\n    return pixel_array\n\ntrain_dicom_tags = pd.read_parquet('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_dicom_tags.parquet', engine='pyarrow')","metadata":{"execution":{"iopub.status.busy":"2023-08-02T15:02:53.97139Z","iopub.execute_input":"2023-08-02T15:02:53.971786Z","iopub.status.idle":"2023-08-02T15:03:01.334715Z","shell.execute_reply.started":"2023-08-02T15:02:53.971755Z","shell.execute_reply":"2023-08-02T15:03:01.333715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Example Case of Unusual DICOM Pixel Array\n\nExample Case of Unusual DICOM Pixel Array\npatient_id = 5425\n\nExample image is found in index 917637 of pixel_representation pandas DataFrame.\n\nYou will find that the Original image appears very unusual, but fixed after implementing the function.","metadata":{}},{"cell_type":"code","source":"# Grab the path of the image in index 917637\nsample_image = train_dicom_tags.loc[917637]['path']\n\n# Open the DICOM file using pydicom.\ndcm = pydicom.read_file(os.path.join('/kaggle/input/rsna-2023-abdominal-trauma-detection',sample_image))\n\noriginal_pixel_array = dcm.pixel_array\nfixed_pixel_array = standardize_pixel_array(dcm)\n\n# To demonstrate that Pixel Repsentation is indeed 1\nprint(f'{dcm.SOPInstanceUID}: Pixel Representation = {dcm.PixelRepresentation}')\n# To demonstrate whether the pixel array has been changed after passing through the function.\nprint(f'Two images are equal: {np.array_equal(original_pixel_array,fixed_pixel_array)}')\n\n# plot both images\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(original_pixel_array, cmap='gray')\nax1.set_title('Original')\nax1.axis('off')\nax2.imshow(fixed_pixel_array, cmap='gray')\nax2.set_title('Fixed')\nax2.axis('off')\nfig.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2023-08-02T15:03:07.190523Z","iopub.execute_input":"2023-08-02T15:03:07.19154Z","iopub.status.idle":"2023-08-02T15:03:07.668541Z","shell.execute_reply.started":"2023-08-02T15:03:07.191496Z","shell.execute_reply":"2023-08-02T15:03:07.667568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Example Case of Normal DICOM Pixel Array\npatient_id = 19\n\nExample image is found in index 1477490 of pixel_representation pandas DataFrame.\n\nYou will find that even after the DICOM pixel array has undergone processing through the function, its content array unaltered if the DICOM image appears normal.\n","metadata":{}},{"cell_type":"code","source":"# Grab the path of the image in index 1477490\nsample_image = train_dicom_tags.loc[1477490]['path']\n\n# Open the DICOM file using pydicom.\ndcm = pydicom.read_file(os.path.join('/kaggle/input/rsna-2023-abdominal-trauma-detection',sample_image))\n\noriginal_pixel_array = dcm.pixel_array\nfixed_pixel_array = standardize_pixel_array(dcm)\n\n# To demonstrate that Pixel Repsentation is indeed 1\nprint(f'{dcm.SOPInstanceUID}: Pixel Representation = {dcm.PixelRepresentation}')\n# To demonstrate whether the pixel array has been changed after passing through the function.\nprint(f'Two images are equal: {np.array_equal(original_pixel_array,fixed_pixel_array)}')\n\n# plot both images\nfig, (ax1, ax2) = plt.subplots(1, 2)\nax1.imshow(original_pixel_array, cmap='gray')\nax1.set_title('Original')\nax1.axis('off')\nax2.imshow(fixed_pixel_array, cmap='gray')\nax2.set_title('Fixed')\nax2.axis('off')\nfig.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2023-08-02T15:03:10.231168Z","iopub.execute_input":"2023-08-02T15:03:10.231608Z","iopub.status.idle":"2023-08-02T15:03:10.823874Z","shell.execute_reply.started":"2023-08-02T15:03:10.23157Z","shell.execute_reply":"2023-08-02T15:03:10.822621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}