{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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"},"jupytext":{"cell_metadata_filter":"-all","main_language":"python","notebook_metadata_filter":"-all"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":99552,"databundleVersionId":13190393,"sourceType":"competition"},{"sourceId":12704901,"sourceType":"datasetVersion","datasetId":8029612},{"sourceId":12725632,"sourceType":"datasetVersion","datasetId":8043418}],"dockerImageVersionId":31089,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nimport pydicom\nimport matplotlib.pyplot as plt\nimport cv2\nimport random  \nfrom skimage.transform import resize\nfrom pathlib import Path","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:21:03.535235Z","iopub.execute_input":"2025-08-11T02:21:03.535571Z","iopub.status.idle":"2025-08-11T02:21:06.829963Z","shell.execute_reply.started":"2025-08-11T02:21:03.535538Z","shell.execute_reply":"2025-08-11T02:21:06.829008Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/rsna-intracranial-aneurysm-detection/train.csv')\ntrain_loc=pd.read_csv('/kaggle/input/rsna-intracranial-aneurysm-detection/train_localizers.csv')\ndisplay(train[0:3])\ndisplay(train_loc[0:3])\nprint(len(train_loc))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:21:06.832118Z","iopub.execute_input":"2025-08-11T02:21:06.832776Z","iopub.status.idle":"2025-08-11T02:21:06.925381Z","shell.execute_reply.started":"2025-08-11T02:21:06.832749Z","shell.execute_reply":"2025-08-11T02:21:06.924486Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_loc[train_loc['SeriesInstanceUID']==train.iloc[4404,0]]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:21:06.926618Z","iopub.execute_input":"2025-08-11T02:21:06.927631Z","iopub.status.idle":"2025-08-11T02:21:06.944697Z","shell.execute_reply.started":"2025-08-11T02:21:06.9276Z","shell.execute_reply":"2025-08-11T02:21:06.943403Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import ast\ncoord_str = train_loc.iloc[2285, 2]\ncoord = ast.literal_eval(coord_str) \nprint(coord)\n\ndcm_path=f\"/kaggle/input/rsna-intracranial-aneurysm-detection/series/{train_loc.iloc[2285,0]}/{train_loc.iloc[2285,1]}.dcm\"\nprint(dcm_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:21:06.945981Z","iopub.execute_input":"2025-08-11T02:21:06.946377Z","iopub.status.idle":"2025-08-11T02:21:06.961347Z","shell.execute_reply.started":"2025-08-11T02:21:06.946338Z","shell.execute_reply":"2025-08-11T02:21:06.960442Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Count the occurrences of each location\nlocation_counts = train_loc['location'].value_counts()\n\n# Plot the distribution\nplt.figure(figsize=(10, 6))\nlocation_counts.plot(kind='bar')\nplt.title('Distribution of Intracranial Aneurysm Locations')\nplt.xlabel('Location')\nplt.ylabel('Count')\nplt.xticks(rotation=90)\nplt.tight_layout()\nplt.show()\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:21:06.962227Z","iopub.execute_input":"2025-08-11T02:21:06.962628Z","iopub.status.idle":"2025-08-11T02:21:07.438632Z","shell.execute_reply.started":"2025-08-11T02:21:06.962605Z","shell.execute_reply":"2025-08-11T02:21:07.437229Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['Modality'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:21:07.439882Z","iopub.execute_input":"2025-08-11T02:21:07.440299Z","iopub.status.idle":"2025-08-11T02:21:07.452284Z","shell.execute_reply.started":"2025-08-11T02:21:07.440265Z","shell.execute_reply":"2025-08-11T02:21:07.450996Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:21:07.456431Z","iopub.execute_input":"2025-08-11T02:21:07.456893Z","iopub.status.idle":"2025-08-11T02:21:07.481699Z","shell.execute_reply.started":"2025-08-11T02:21:07.456844Z","shell.execute_reply":"2025-08-11T02:21:07.480724Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\nnumerical_cols = train.select_dtypes(include=np.number).columns.tolist()\n\nplt.figure(figsize=(16,8))\nsns.heatmap(train[numerical_cols].corr(), annot=True, cmap='summer')\nplt.title(\"Correlation Between Numerical Features\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:21:07.482623Z","iopub.execute_input":"2025-08-11T02:21:07.482868Z","iopub.status.idle":"2025-08-11T02:21:09.323069Z","shell.execute_reply.started":"2025-08-11T02:21:07.482845Z","shell.execute_reply":"2025-08-11T02:21:09.321701Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.histplot(train[train['Aneurysm Present'] == 0])\nsns.histplot(train[train['Aneurysm Present'] == 1], color='red')\nplt.title(\"Presence of IA Distribution\");","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:21:09.32427Z","iopub.execute_input":"2025-08-11T02:21:09.324769Z","iopub.status.idle":"2025-08-11T02:21:11.13207Z","shell.execute_reply.started":"2025-08-11T02:21:09.324743Z","shell.execute_reply":"2025-08-11T02:21:11.130862Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mod_counts = train[\"Modality\"].value_counts().head(13)#Try different values of head\nsns.set(style=\"white\")\nplt.figure(figsize=(8, 6))\n#x=loc_counts.index, y=loc_counts.values\nax = sns.barplot(x=mod_counts.index, y=mod_counts.values, color=sns.color_palette(\"Greens\", n_colors=5)[3])\nplt.title(\"Medical Imaging Series Modalities\", fontsize=16)\nplt.xlabel(\"Intracranial Aneurysm\", fontsize=12)\nplt.ylabel(\"Frequency\", fontsize=12)\nplt.xticks(rotation=45, fontsize=11)\nplt.yticks(fontsize=11)\nsns.despine()\n\n#+2 is good if chart is vertical. +20 worked for horizontal\nfor i, v in enumerate(mod_counts.values):\n    ax.text(i, v + 20, str(v), ha='center', va='bottom', fontsize=10)\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:21:11.133263Z","iopub.execute_input":"2025-08-11T02:21:11.133851Z","iopub.status.idle":"2025-08-11T02:21:11.385307Z","shell.execute_reply.started":"2025-08-11T02:21:11.133826Z","shell.execute_reply":"2025-08-11T02:21:11.38393Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"DCM IMAGES","metadata":{}},{"cell_type":"code","source":"import glob\ntrain_images = glob.glob((f\"/kaggle/input/rsna-intracranial-aneurysm-detection/series/**/*.dcm\"))\nprint(\"Total number of images: \", len(train_images))\n\ntrain_images = pd.Series(train_images)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:21:11.386283Z","iopub.execute_input":"2025-08-11T02:21:11.386526Z","iopub.status.idle":"2025-08-11T02:23:08.660598Z","shell.execute_reply.started":"2025-08-11T02:21:11.386504Z","shell.execute_reply":"2025-08-11T02:23:08.659355Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig=plt.figure(figsize=(20, 15))\ncolumns = 4; rows = 4\nfor i in range(1, columns*rows +1):\n    ds = pydicom.dcmread(train_images[i])#Original was dcmread(train_images_dir + train_images[i])\n    fig.add_subplot(rows, columns, i)\n    plt.imshow(ds.pixel_array, cmap=plt.cm.bone)\n    fig.add_subplot","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:23:08.661976Z","iopub.execute_input":"2025-08-11T02:23:08.662402Z","iopub.status.idle":"2025-08-11T02:23:12.612478Z","shell.execute_reply.started":"2025-08-11T02:23:08.662366Z","shell.execute_reply":"2025-08-11T02:23:12.610852Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"File type of image","metadata":{}},{"cell_type":"code","source":"print(ds)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:23:12.613749Z","iopub.execute_input":"2025-08-11T02:23:12.614077Z","iopub.status.idle":"2025-08-11T02:23:12.625083Z","shell.execute_reply.started":"2025-08-11T02:23:12.614053Z","shell.execute_reply":"2025-08-11T02:23:12.624011Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"im = ds.pixel_array\nprint(type(im))\nprint(im.dtype)\nprint(im.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:23:29.824734Z","iopub.execute_input":"2025-08-11T02:23:29.825096Z","iopub.status.idle":"2025-08-11T02:23:29.831755Z","shell.execute_reply.started":"2025-08-11T02:23:29.825072Z","shell.execute_reply":"2025-08-11T02:23:29.830695Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"lets plot nii slices","metadata":{}},{"cell_type":"code","source":"import glob\nmrscans = glob.glob ('../input/rsna-intracranial-aneurysm-detection/segmentations/*/*')\n\n\n# num of segmentation mask\nlen(mrscans)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:23:38.709903Z","iopub.execute_input":"2025-08-11T02:23:38.710336Z","iopub.status.idle":"2025-08-11T02:23:38.850598Z","shell.execute_reply.started":"2025-08-11T02:23:38.710308Z","shell.execute_reply":"2025-08-11T02:23:38.849463Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ndef read_nibabel(path):\n    return nib.load(path).get_fdata()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:23:51.734507Z","iopub.execute_input":"2025-08-11T02:23:51.73518Z","iopub.status.idle":"2025-08-11T02:23:51.740079Z","shell.execute_reply.started":"2025-08-11T02:23:51.735156Z","shell.execute_reply":"2025-08-11T02:23:51.739033Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def multi_dim_plot(multi_dim_array, id, num_slices=49): \n    fig = plt.figure(figsize=(30, 30))\n    plt.title(\n        f'Plotting first {num_slices} slices of {id}', \n        fontdict = {'fontsize' : 20}\n    )\n    plt.yticks([])\n    plt.xticks([])\n    \n    xy = int(np.sqrt(num_slices))\n    for i in range(num_slices):\n        ax = fig.add_subplot(xy, xy, i + 1)\n        plt.imshow(multi_dim_array[..., i])\n        plt.axis(\"off\")\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:23:56.469727Z","iopub.execute_input":"2025-08-11T02:23:56.470112Z","iopub.status.idle":"2025-08-11T02:23:56.478056Z","shell.execute_reply.started":"2025-08-11T02:23:56.470087Z","shell.execute_reply":"2025-08-11T02:23:56.476436Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from random import sample\nimport nibabel as nib\n\nfor msk in sample(mrscans, 2):\n    m = read_nibabel(msk)\n    multi_dim_plot(m, id=msk.split('/')[-1])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:24:06.371013Z","iopub.execute_input":"2025-08-11T02:24:06.371334Z","iopub.status.idle":"2025-08-11T02:24:23.559299Z","shell.execute_reply.started":"2025-08-11T02:24:06.371312Z","shell.execute_reply":"2025-08-11T02:24:23.558255Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def multi_dim_plot(multi_dim_array, id, num_slices=25): #Original was 64\n    fig = plt.figure(figsize=(30, 30))\n    plt.title(\n        f'Plotting first {num_slices} slices of {id}', \n        fontdict = {'fontsize' : 20}\n    )\n    plt.yticks([])\n    plt.xticks([])\n    \n    xy = int(np.sqrt(num_slices))\n    for i in range(num_slices):\n        ax = fig.add_subplot(xy, xy, i + 1)\n        plt.imshow(multi_dim_array[..., :num_slices][..., i])\n        plt.axis(\"off\")\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:24:33.285332Z","iopub.execute_input":"2025-08-11T02:24:33.285738Z","iopub.status.idle":"2025-08-11T02:24:33.293313Z","shell.execute_reply.started":"2025-08-11T02:24:33.285711Z","shell.execute_reply":"2025-08-11T02:24:33.292055Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from random import sample\nimport nibabel as nib\n\nfor msk in sample(mrscans, 2):\n    m = read_nibabel(msk)\n    multi_dim_plot(m, id=msk.split('/')[-1])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:24:38.139506Z","iopub.execute_input":"2025-08-11T02:24:38.14066Z","iopub.status.idle":"2025-08-11T02:24:48.381204Z","shell.execute_reply.started":"2025-08-11T02:24:38.140616Z","shell.execute_reply":"2025-08-11T02:24:48.380008Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dcm_path=f\"/kaggle/input/rsna-intracranial-aneurysm-detection/series/{train_loc.iloc[i,0]}/{train_loc.iloc[i,1]}.dcm\"\nds = pydicom.dcmread(dcm_path)\nsite=train_loc.iloc[i,3]\nimage = ds.pixel_array\ncoord = coord_str = train_loc.iloc[i, 2]\ncoord = ast.literal_eval(coord_str) \nplt.imshow(image, cmap='gray')\nplt.scatter(coord['x'], coord['y'], color='yellow', alpha=0.1, s=500)  \nplt.title(f\"{site} with IA\")\nplt.axis('off')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-11T02:25:08.935115Z","iopub.execute_input":"2025-08-11T02:25:08.935489Z","iopub.status.idle":"2025-08-11T02:25:09.177188Z","shell.execute_reply.started":"2025-08-11T02:25:08.935462Z","shell.execute_reply":"2025-08-11T02:25:09.176008Z"}},"outputs":[],"execution_count":null}]}