{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":99552,"databundleVersionId":13747926,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Check Localizers\n\nThis notebooks takes a random aneurysm from the dataset and plots the marked location.\n\nI noticed inconsistencies in the localizers in my local version of the data. Before downloading another 200GB, I wanted to do check if the current version of the files is correct.\n\nMaybe someone finds it helpful.","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nimport pydicom\nfrom random import choice","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-09-16T09:07:42.679338Z","iopub.execute_input":"2025-09-16T09:07:42.680318Z","iopub.status.idle":"2025-09-16T09:07:49.393091Z","shell.execute_reply.started":"2025-09-16T09:07:42.679944Z","shell.execute_reply":"2025-09-16T09:07:49.390757Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_dir = '/kaggle/input/rsna-intracranial-aneurysm-detection'\nprint(os.listdir(data_dir))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-16T09:07:49.398531Z","iopub.execute_input":"2025-09-16T09:07:49.399152Z","iopub.status.idle":"2025-09-16T09:07:49.410349Z","shell.execute_reply.started":"2025-09-16T09:07:49.399102Z","shell.execute_reply":"2025-09-16T09:07:49.408597Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# load the csv files\ntrain_df = pd.read_csv(os.path.join(data_dir,'train.csv'))\nlocalizers_df = pd.read_csv(os.path.join(data_dir,'train_localizers.csv'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-16T09:07:49.412057Z","iopub.execute_input":"2025-09-16T09:07:49.412464Z","iopub.status.idle":"2025-09-16T09:07:49.547398Z","shell.execute_reply.started":"2025-09-16T09:07:49.412426Z","shell.execute_reply":"2025-09-16T09:07:49.546115Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# pick a random series where an aneurysm is present\npositive_cases = train_df[train_df['Aneurysm Present'] == 1]\nexample = choice(list(positive_cases.SeriesInstanceUID.unique()))\n# this example was shown to be wrong in another thread\nexample = '1.2.826.0.1.3680043.8.498.10843288560910004558081082597234683103'\nprint(example)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-16T09:07:49.548558Z","iopub.execute_input":"2025-09-16T09:07:49.548909Z","iopub.status.idle":"2025-09-16T09:07:49.577139Z","shell.execute_reply.started":"2025-09-16T09:07:49.548876Z","shell.execute_reply":"2025-09-16T09:07:49.575275Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# find corresponding location\nexample_location = localizers_df[localizers_df.SeriesInstanceUID == example].sample(1)\nexample_location.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-16T09:07:49.579479Z","iopub.execute_input":"2025-09-16T09:07:49.579923Z","iopub.status.idle":"2025-09-16T09:07:49.628673Z","shell.execute_reply.started":"2025-09-16T09:07:49.579875Z","shell.execute_reply":"2025-09-16T09:07:49.626673Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# extract image file and coordinates\nsop_instance = example_location.SOPInstanceUID.item()\ncoordinates = eval(example_location.coordinates.item())\nprint(coordinates)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-16T09:07:49.630418Z","iopub.execute_input":"2025-09-16T09:07:49.630847Z","iopub.status.idle":"2025-09-16T09:07:49.644333Z","shell.execute_reply.started":"2025-09-16T09:07:49.630782Z","shell.execute_reply":"2025-09-16T09:07:49.641904Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# load the dataset\nimage_file = os.path.join(data_dir,'series',example,sop_instance+'.dcm')\ndataset = pydicom.dcmread(image_file)\npixels = dataset.pixel_array\nif 'f' in coordinates.keys():\n    # multiframe dicom\n    pixels = pixels[coordinates['f']]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-16T09:07:49.647659Z","iopub.execute_input":"2025-09-16T09:07:49.64842Z","iopub.status.idle":"2025-09-16T09:07:49.748037Z","shell.execute_reply.started":"2025-09-16T09:07:49.64839Z","shell.execute_reply":"2025-09-16T09:07:49.744471Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot the slice and highlight the marked location\nax = plt.subplot(1,1,1)\nplt.pcolormesh(pixels,cmap = 'bone')\nhighlight_circle = plt.Circle((coordinates['x'], coordinates['y']), radius=20, color='red', fill=False, linewidth=1)\nax.add_patch(highlight_circle)\nplt.axis('square')\n_ = plt.axis('off')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-16T09:07:49.749544Z","iopub.execute_input":"2025-09-16T09:07:49.749878Z","iopub.status.idle":"2025-09-16T09:07:50.548364Z","shell.execute_reply.started":"2025-09-16T09:07:49.74984Z","shell.execute_reply":"2025-09-16T09:07:50.547232Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}