{"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":13190393,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# The main thing to point out is that while only single slice is indicated as labeled\n# multiple slices contain aneurysm. This can introduce label noise if too many/not enough slices are given\n# positive label for each series. The number of slices with visible aneurism is different between series.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-29T14:46:47.068898Z","iopub.execute_input":"2025-07-29T14:46:47.069356Z","iopub.status.idle":"2025-07-29T14:46:48.501609Z","shell.execute_reply.started":"2025-07-29T14:46:47.06928Z","shell.execute_reply":"2025-07-29T14:46:48.500674Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import polars as pl\nimport glob\nimport os \nimport pydicom\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nimport json","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tr = pl.read_csv(\"../input/rsna-intracranial-aneurysm-detection/train.csv\")\ntrl = pl.read_csv(\"../input/rsna-intracranial-aneurysm-detection/train_localizers.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T14:46:48.50324Z","iopub.execute_input":"2025-07-29T14:46:48.503622Z","iopub.status.idle":"2025-07-29T14:46:48.852585Z","shell.execute_reply.started":"2025-07-29T14:46:48.50359Z","shell.execute_reply":"2025-07-29T14:46:48.851643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_around_label(row_number, num_around, trl=trl):\n    row = trl[row_number]\n    a,b,c,d = row[:,:4].transpose()[:,0].to_list()\n    \n    folders = glob.glob(\"../input/rsna-intracranial-aneurysm-detection/series/*\")\n    \n    for i in folders:\n        if a in i:\n            break\n    files = sorted(glob.glob(os.path.join(i, \"*\")))\n    i_n = list()\n    for e, f in enumerate(files):\n        ex = pydicom.dcmread(f)\n        i_n.append([ex.InstanceNumber,f])\n    \n    i_n = sorted(i_n)\n    \n    for e, f in enumerate(i_n):\n        if b in f[1]:\n            break\n    \n    c = json.loads(c.replace(\"'\",'\"'))\n    \n\n    for i in range(e-num_around,e+num_around+1):\n        if i ==e:\n            print(\"labeled slice\")\n        ex = pydicom.dcmread(i_n[i][1])\n        fig, ax = plt.subplots()\n        if len(ex.pixel_array.shape) > 2:\n            continue\n        ax.imshow(ex.pixel_array)\n        rect = patches.Rectangle((c[\"x\"]-10, c[\"y\"]-10), 20, 20, linewidth=1, edgecolor='r', facecolor='none')\n        ax.add_patch(rect)\n        plt.show()\n    \n   ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T15:05:05.278674Z","iopub.execute_input":"2025-07-29T15:05:05.279013Z","iopub.status.idle":"2025-07-29T15:05:05.289463Z","shell.execute_reply.started":"2025-07-29T15:05:05.278988Z","shell.execute_reply":"2025-07-29T15:05:05.288145Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# as can be seen the aneurysm is visible on several nearby slices. \nplot_around_label(55,4)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T15:05:10.870217Z","iopub.execute_input":"2025-07-29T15:05:10.870591Z","iopub.status.idle":"2025-07-29T15:05:13.139526Z","shell.execute_reply.started":"2025-07-29T15:05:10.870565Z","shell.execute_reply":"2025-07-29T15:05:13.138517Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# some nearby slices do not contain the aneurysm, and its not always clear which ones do. \nplot_around_label(8,4)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T15:05:20.553919Z","iopub.execute_input":"2025-07-29T15:05:20.554358Z","iopub.status.idle":"2025-07-29T15:05:22.94901Z","shell.execute_reply.started":"2025-07-29T15:05:20.554321Z","shell.execute_reply":"2025-07-29T15:05:22.947937Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}