{"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":"markdown","source":"# 🧾Table of Contents\n* [Load Data](#load)\n* [Features](#features)\n* [Target](#target)\n* [Target vs Features](#target_vs_features)\n* [Localizer Data](#local)\n* [Image File Example](#image)","metadata":{}},{"cell_type":"code","source":"# packages\n\n# standard\nimport numpy as np\nimport pandas as pd\nimport time\n\n# plots\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nimport seaborn as sns\n\n# image\nimport pydicom as dicom\n\n# other stuff\nimport warnings\nwarnings.filterwarnings('ignore')\n\nimport ast","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:42:45.007754Z","iopub.execute_input":"2025-07-29T16:42:45.008066Z","iopub.status.idle":"2025-07-29T16:42:45.693138Z","shell.execute_reply.started":"2025-07-29T16:42:45.008041Z","shell.execute_reply":"2025-07-29T16:42:45.692291Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls -l '/kaggle/input/rsna-intracranial-aneurysm-detection'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:32.60293Z","iopub.execute_input":"2025-07-29T16:29:32.603271Z","iopub.status.idle":"2025-07-29T16:29:32.736631Z","shell.execute_reply.started":"2025-07-29T16:29:32.603248Z","shell.execute_reply":"2025-07-29T16:29:32.735278Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls -l '/kaggle/input/rsna-intracranial-aneurysm-detection/kaggle_evaluation'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:32.739041Z","iopub.execute_input":"2025-07-29T16:29:32.739352Z","iopub.status.idle":"2025-07-29T16:29:32.87577Z","shell.execute_reply.started":"2025-07-29T16:29:32.739327Z","shell.execute_reply":"2025-07-29T16:29:32.87463Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls -l '/kaggle/input/rsna-intracranial-aneurysm-detection/segmentations'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:32.877534Z","iopub.execute_input":"2025-07-29T16:29:32.877981Z","iopub.status.idle":"2025-07-29T16:29:33.037094Z","shell.execute_reply.started":"2025-07-29T16:29:32.877935Z","shell.execute_reply":"2025-07-29T16:29:33.036012Z"},"_kg_hide-output":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls -l '/kaggle/input/rsna-intracranial-aneurysm-detection/series'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:33.038481Z","iopub.execute_input":"2025-07-29T16:29:33.038741Z","iopub.status.idle":"2025-07-29T16:29:33.79846Z","shell.execute_reply.started":"2025-07-29T16:29:33.038718Z","shell.execute_reply":"2025-07-29T16:29:33.797349Z"},"_kg_hide-output":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls -l '/kaggle/input/rsna-intracranial-aneurysm-detection/series/1.2.826.0.1.3680043.8.498.10004044428023505108375152878107656647'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:41:34.876709Z","iopub.execute_input":"2025-07-29T16:41:34.877096Z","iopub.status.idle":"2025-07-29T16:41:35.041583Z","shell.execute_reply.started":"2025-07-29T16:41:34.877068Z","shell.execute_reply":"2025-07-29T16:41:35.040556Z"},"_kg_hide-output":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# configs\npd.set_option('display.max_columns', None) # we want to display all columns in this notebook\npd.set_option('display.max_rows', 100) # increase number of displayed rows\npd.set_option('max_colwidth', None) # make full cells content visible\n\n# random seed\nmy_random_seed = 123\n\n# aesthetics\ndefault_color_1 = 'darkblue'\ndefault_color_2 = 'darkgreen'\ndefault_color_3 = 'darkred'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:33.801673Z","iopub.execute_input":"2025-07-29T16:29:33.801946Z","iopub.status.idle":"2025-07-29T16:29:33.807411Z","shell.execute_reply.started":"2025-07-29T16:29:33.801921Z","shell.execute_reply":"2025-07-29T16:29:33.806531Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='load'></a>\n# Load Data","metadata":{}},{"cell_type":"code","source":"t1 = time.time()\ndf_train = pd.read_csv('/kaggle/input/rsna-intracranial-aneurysm-detection/train.csv')\ndf_train_local = pd.read_csv('/kaggle/input/rsna-intracranial-aneurysm-detection/train_localizers.csv')\nt2 = time.time()\nprint('Elapsed time [s]:', np.round(t2-t1,4))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:33.808275Z","iopub.execute_input":"2025-07-29T16:29:33.808624Z","iopub.status.idle":"2025-07-29T16:29:33.850755Z","shell.execute_reply.started":"2025-07-29T16:29:33.808595Z","shell.execute_reply":"2025-07-29T16:29:33.849651Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# dimensions\ndf_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:33.851785Z","iopub.execute_input":"2025-07-29T16:29:33.852274Z","iopub.status.idle":"2025-07-29T16:29:33.859205Z","shell.execute_reply.started":"2025-07-29T16:29:33.852224Z","shell.execute_reply":"2025-07-29T16:29:33.85807Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# preview\ndf_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:33.860144Z","iopub.execute_input":"2025-07-29T16:29:33.860465Z","iopub.status.idle":"2025-07-29T16:29:33.883919Z","shell.execute_reply.started":"2025-07-29T16:29:33.860441Z","shell.execute_reply":"2025-07-29T16:29:33.883179Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# structure details\ndf_train.info(verbose=True, show_counts=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:33.884659Z","iopub.execute_input":"2025-07-29T16:29:33.884899Z","iopub.status.idle":"2025-07-29T16:29:33.908275Z","shell.execute_reply.started":"2025-07-29T16:29:33.884881Z","shell.execute_reply":"2025-07-29T16:29:33.907288Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# basic stats\ndf_train.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:33.909075Z","iopub.execute_input":"2025-07-29T16:29:33.909403Z","iopub.status.idle":"2025-07-29T16:29:33.956571Z","shell.execute_reply.started":"2025-07-29T16:29:33.909347Z","shell.execute_reply":"2025-07-29T16:29:33.955431Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:33.957556Z","iopub.execute_input":"2025-07-29T16:29:33.957922Z","iopub.status.idle":"2025-07-29T16:29:33.964485Z","shell.execute_reply.started":"2025-07-29T16:29:33.957891Z","shell.execute_reply":"2025-07-29T16:29:33.96346Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# define features and target\nfeatures_num = ['PatientAge']\n\nfeatures_cat = ['PatientSex', 'Modality',\n       'Left Infraclinoid Internal Carotid Artery',\n       'Right Infraclinoid Internal Carotid Artery',\n       'Left Supraclinoid Internal Carotid Artery',\n       'Right Supraclinoid Internal Carotid Artery',\n       'Left Middle Cerebral Artery', 'Right Middle Cerebral Artery',\n       'Anterior Communicating Artery', 'Left Anterior Cerebral Artery',\n       'Right Anterior Cerebral Artery', 'Left Posterior Communicating Artery',\n       'Right Posterior Communicating Artery', 'Basilar Tip',\n       'Other Posterior Circulation']\n\ntarget = 'Aneurysm Present'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:33.965351Z","iopub.execute_input":"2025-07-29T16:29:33.965677Z","iopub.status.idle":"2025-07-29T16:29:33.978683Z","shell.execute_reply.started":"2025-07-29T16:29:33.965648Z","shell.execute_reply":"2025-07-29T16:29:33.977724Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='features'></a>\n# Features","metadata":{}},{"cell_type":"code","source":"# plot histograms\nfor f in features_num:\n    plt.figure(figsize=(8,3))\n    df_train[f].plot(kind='hist', bins=25, color=default_color_1)\n    plt.title(f + ' - Train')\n    plt.grid()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:33.979689Z","iopub.execute_input":"2025-07-29T16:29:33.979926Z","iopub.status.idle":"2025-07-29T16:29:34.213557Z","shell.execute_reply.started":"2025-07-29T16:29:33.979908Z","shell.execute_reply":"2025-07-29T16:29:34.212738Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot categorical feature distributions\nfor f in features_cat:\n    plt.figure(figsize=(8,3))\n    df_train[f].value_counts().sort_index().plot(kind='bar', color=default_color_1)\n    plt.title(f + ' - Train')\n    plt.grid()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:34.215951Z","iopub.execute_input":"2025-07-29T16:29:34.216189Z","iopub.status.idle":"2025-07-29T16:29:36.437813Z","shell.execute_reply.started":"2025-07-29T16:29:34.216171Z","shell.execute_reply":"2025-07-29T16:29:36.437034Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='target'></a>\n# Target","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(4,4))\ndf_train[target].value_counts().sort_index().plot(kind='bar', color=default_color_3)\nplt.title(target)\nplt.grid()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:36.438838Z","iopub.execute_input":"2025-07-29T16:29:36.439069Z","iopub.status.idle":"2025-07-29T16:29:36.565563Z","shell.execute_reply.started":"2025-07-29T16:29:36.439048Z","shell.execute_reply":"2025-07-29T16:29:36.564557Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='target_vs_features'></a>\n# Target vs Features","metadata":{}},{"cell_type":"code","source":"# plot features distributions split by target\nfor f in features_num:\n    plt.figure(figsize=(4,3))\n    sns.violinplot(df_train, x=target, y=f,)\n    plt.title(f + ' by target')\n    plt.grid()\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:36.566538Z","iopub.execute_input":"2025-07-29T16:29:36.567293Z","iopub.status.idle":"2025-07-29T16:29:36.772129Z","shell.execute_reply.started":"2025-07-29T16:29:36.56726Z","shell.execute_reply":"2025-07-29T16:29:36.771023Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# impact of categorical features - normalized cross tables\nfor f in features_cat:\n    print('>>> Feature:', f)\n    ctab = pd.crosstab(df_train[target], df_train[f])\n    ctab_norm = ctab / ctab.sum()\n    plt.figure(figsize=(5,2))\n    g = sns.heatmap(ctab_norm, annot=True,\n                    fmt='.3f', linecolor='black',\n                    linewidths=0.5, cmap='Greens', \n                    vmin=0, vmax=+1)\n    plt.title(f + ' vs target - train')\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:34:48.758646Z","iopub.execute_input":"2025-07-29T16:34:48.758935Z","iopub.status.idle":"2025-07-29T16:34:51.03788Z","shell.execute_reply.started":"2025-07-29T16:34:48.758901Z","shell.execute_reply":"2025-07-29T16:34:51.037303Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='local'></a>\n# Localizer Data","metadata":{}},{"cell_type":"code","source":"# dimensions\ndf_train_local.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:39.145197Z","iopub.execute_input":"2025-07-29T16:29:39.145506Z","iopub.status.idle":"2025-07-29T16:29:39.151111Z","shell.execute_reply.started":"2025-07-29T16:29:39.145478Z","shell.execute_reply":"2025-07-29T16:29:39.150237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# preview\ndf_train_local.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:39.151968Z","iopub.execute_input":"2025-07-29T16:29:39.152295Z","iopub.status.idle":"2025-07-29T16:29:39.170454Z","shell.execute_reply.started":"2025-07-29T16:29:39.152267Z","shell.execute_reply":"2025-07-29T16:29:39.16932Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# locations\ndf_train_local.location.value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:39.171544Z","iopub.execute_input":"2025-07-29T16:29:39.171868Z","iopub.status.idle":"2025-07-29T16:29:39.192134Z","shell.execute_reply.started":"2025-07-29T16:29:39.171847Z","shell.execute_reply":"2025-07-29T16:29:39.191341Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# convert coordinates to dictionary\ndf_train_local.coordinates = df_train_local.coordinates.map(ast.literal_eval)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:39.193277Z","iopub.execute_input":"2025-07-29T16:29:39.193815Z","iopub.status.idle":"2025-07-29T16:29:39.245057Z","shell.execute_reply.started":"2025-07-29T16:29:39.193789Z","shell.execute_reply":"2025-07-29T16:29:39.244207Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# extract coordinates\ndef extract_x(my_dict):\n    return my_dict['x']\n\ndef extract_y(my_dict):\n    return my_dict['y']\n\ndf_train_local['x'] = df_train_local.coordinates.map(extract_x)\ndf_train_local['y'] = df_train_local.coordinates.map(extract_y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:29:39.246047Z","iopub.execute_input":"2025-07-29T16:29:39.246416Z","iopub.status.idle":"2025-07-29T16:29:39.254689Z","shell.execute_reply.started":"2025-07-29T16:29:39.246386Z","shell.execute_reply":"2025-07-29T16:29:39.253443Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot coordinates\nplt.figure(figsize=(9,9))\ng = sns.scatterplot(data=df_train_local, x='x', y='y', hue='location')\ng.legend(loc='center left', bbox_to_anchor=(1.1, 0.5), ncol=1)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:36:20.670266Z","iopub.execute_input":"2025-07-29T16:36:20.670901Z","iopub.status.idle":"2025-07-29T16:36:21.18853Z","shell.execute_reply.started":"2025-07-29T16:36:20.670876Z","shell.execute_reply":"2025-07-29T16:36:21.187443Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='image'></a>\n# Image File Example","metadata":{}},{"cell_type":"code","source":"base_path = '/kaggle/input/rsna-intracranial-aneurysm-detection/series/'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:45:31.597724Z","iopub.execute_input":"2025-07-29T16:45:31.598456Z","iopub.status.idle":"2025-07-29T16:45:31.602237Z","shell.execute_reply.started":"2025-07-29T16:45:31.598431Z","shell.execute_reply":"2025-07-29T16:45:31.60143Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"folder = '1.2.826.0.1.3680043.8.498.10004044428023505108375152878107656647/'\nfile = '1.2.826.0.1.3680043.8.498.10124807242473374136099471315028464450.dcm'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:45:32.334606Z","iopub.execute_input":"2025-07-29T16:45:32.334868Z","iopub.status.idle":"2025-07-29T16:45:32.339185Z","shell.execute_reply.started":"2025-07-29T16:45:32.334849Z","shell.execute_reply":"2025-07-29T16:45:32.338258Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"filename = base_path + folder + file","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:45:34.40177Z","iopub.execute_input":"2025-07-29T16:45:34.40205Z","iopub.status.idle":"2025-07-29T16:45:34.406628Z","shell.execute_reply.started":"2025-07-29T16:45:34.40203Z","shell.execute_reply":"2025-07-29T16:45:34.405309Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# load and display image\nds = dicom.dcmread(filename)\nplt.imshow(ds.pixel_array)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-29T16:45:35.962052Z","iopub.execute_input":"2025-07-29T16:45:35.962322Z","iopub.status.idle":"2025-07-29T16:45:36.236494Z","shell.execute_reply.started":"2025-07-29T16:45:35.962303Z","shell.execute_reply":"2025-07-29T16:45:36.235085Z"}},"outputs":[],"execution_count":null}]}