{"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":13450071,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import polars as pl\nimport altair as alt\n\ndf = pl.read_csv(\n    '/kaggle/input/rsna-intracranial-aneurysm-detection/train.csv',\n    schema_overrides={\n        'PatientSex': pl.datatypes.Enum([\"Male\", \"Female\"]),\n        'Modality': pl.datatypes.Enum([\"MRA\", \"CTA\", \"MRI T2\", \"MRI T1post\"])\n    }).with_columns(total_aneurysm_locations=pl.sum_horizontal(pl.nth(range(4,17))))\naneurysms = df.filter(pl.col(\"Aneurysm Present\") == 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T05:19:19.855553Z","iopub.execute_input":"2025-08-21T05:19:19.855874Z","iopub.status.idle":"2025-08-21T05:19:19.874924Z","shell.execute_reply.started":"2025-08-21T05:19:19.855851Z","shell.execute_reply":"2025-08-21T05:19:19.874008Z"},"_kg_hide-input":true,"_kg_hide-output":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Intracranial aneurysms\n\n* [An aneurysm](https://www.nhs.uk/conditions/brain-aneurysm/) is a bulge in a blood vessel caused by a weakness in the blood vessel wall, usually at a point of branching\n* An aneusym may be *saccular* or *fusiform*\n    * intracranial aneurysms are most commonly saccular but [a small proportion (<10%)](https://jkns.or.kr/journal/view.php?doi=10.3340/jkns.2008.44.3.116) are fusiform\n* [Sizes](https://radiopaedia.org/articles/saccular-cerebral-aneurysm#nav_size):\n    * micro: `<300 micrometers` maximal dimension\n    * medium: `5-10 millimeters`\n    * large: `10-25 mm`\n    * giant: `>25 mm`\n\n<img\n    src=\"https://www.researchgate.net/profile/Grace-Carolan-Rees/publication/235363631/figure/fig2/AS:667111928123406@1536063286811/Representation-of-saccular-a-and-fusiform-b-aneurysms.png\"\n    alt=\"types of aneurysm\"\n    width=200 />\n    \n* [Predisposing factors](https://www.bafound.org/understanding-brain-aneurysms/causes-risk-factors/) relevant to the available dataset include:\n    * age (over 40)\n    * gender (female > male)","metadata":{"execution":{"iopub.status.busy":"2025-08-07T21:11:30.869869Z","iopub.execute_input":"2025-08-07T21:11:30.870569Z","iopub.status.idle":"2025-08-07T21:11:30.878618Z","shell.execute_reply.started":"2025-08-07T21:11:30.870543Z","shell.execute_reply":"2025-08-07T21:11:30.877416Z"}}},{"cell_type":"code","source":"full_dataset = df.select('PatientAge').to_series().plot.hist().properties(title=\"full dataset\")\naneurysms_only = aneurysms.select('PatientAge').to_series().plot.hist(\n    color=alt.value('orange')).properties(title=\"aneurysms only\")\n\nchart=(full_dataset|aneurysms_only).configure_title(fontSize=18, offset=15, orient='top', anchor='middle')\nchart.title=\"Age distribution comparison\"\nchart","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T05:20:32.274527Z","iopub.execute_input":"2025-08-21T05:20:32.275454Z","iopub.status.idle":"2025-08-21T05:20:32.337877Z","shell.execute_reply.started":"2025-08-21T05:20:32.27542Z","shell.execute_reply":"2025-08-21T05:20:32.336854Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"full_dataset_sex = df.group_by(\"PatientSex\").len()\nratio_full_dataset = (\n    full_dataset_sex.filter(pl.col(\"PatientSex\")==\"Female\").select(\"len\") /\n    full_dataset_sex.filter(pl.col(\"PatientSex\")==\"Male\").select(\"len\")\n).to_series()\naneurysms_only_sex = aneurysms.group_by(\"PatientSex\").len()\nratio_aneuysms = (\n    aneurysms_only_sex.filter(pl.col(\"PatientSex\")==\"Female\").select(\"len\") /\n    aneurysms_only_sex.filter(pl.col(\"PatientSex\")==\"Male\").select(\"len\")\n).to_series()\n\nprint(\"\"\"Sex balance\nFull dataset (F/M): {}\nAneurysms only (F/M): {}\"\"\".format(round(*ratio_full_dataset, 2), round(*ratio_aneuysms, 2)))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T05:20:34.674704Z","iopub.execute_input":"2025-08-21T05:20:34.674974Z","iopub.status.idle":"2025-08-21T05:20:34.685559Z","shell.execute_reply.started":"2025-08-21T05:20:34.674955Z","shell.execute_reply":"2025-08-21T05:20:34.684633Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"##  Relevance\n\n* The aneurysmal vessel represents a weak point with a risk of rupture. This results in bleeding which, in many cases, results in disability and/or death\n    * [bleeding](https://www.kaggle.com/code/smit2300/hemorrhage-medical-introduction) is usually into the subarachnoid space (but subdural in a minority of cases)\n \n<img\n    src=\"https://upload.wikimedia.org/wikipedia/commons/c/c1/SubarachnoidP.png\"\n    alt=\"subarachnoid haemorrhage\"\n    width=200 />","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"## Treatment\n\n* Preventative treatment is possible for aneurysms assessed as high risk for rupture\n* Usually [treatment](https://www.nhs.uk/conditions/brain-aneurysm/treatment/) is via minimally invasive *endovascular* coiling but sometimes surgery (*clipping*) is required depending on the anatomy\n\n<a href=\"https://jnis.bmj.com/content/neurintsurg/15/10/989/\"><img src=\"https://jnis.bmj.com/content/neurintsurg/15/10/989/F1.large.jpg\"\n     alt=\"endovascular repair\"\n     width=\"200\" /></a>","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"## Imaging\n\n### CT angiography\n\n  * Fast, widely available\n\n### MRI angiography\n\n  * More sensitive at detecting small aneurysms\n\n### Digital subtraction aneurysms\n\n  * gold standard investigation but invasive\n  * not included in the dataset","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"full_modality_counts = df.group_by('Modality').len().with_columns(\n    (pl.col('len')/len(df)*100).round(2).alias(\"percent\")\n).sort('percent', descending=True)\nfull_modality_counts_chart = alt.Chart(full_modality_counts).mark_arc().encode(\n    theta=\"len\",\n    color=\"Modality\",\n).properties(\n    title=alt.TitleParams(\n        text='Full dataset'\n    )\n)\naneurysm_only_modality_counts = aneurysms.group_by('Modality').len().with_columns(\n    (pl.col('len')/len(aneurysms)*100).round(2).alias(\"percent\")\n).sort('percent', descending=True)\naneurysm_only_modality_counts_chart = alt.Chart(aneurysm_only_modality_counts).mark_arc().encode(\n    theta=\"len\",\n    color=\"Modality\",\n).properties(\n    title=alt.TitleParams(\n        text='Aneurysms only')\n)\nchart = (full_modality_counts_chart| aneurysm_only_modality_counts_chart).properties(title=alt.TitleParams(\n        text='Dataset composition',\n        subtitle='by modality'))\nchart = chart.configure_title(fontSize=18, offset=5, orient='top', anchor='middle')\nchart","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T05:20:39.472141Z","iopub.execute_input":"2025-08-21T05:20:39.472497Z","iopub.status.idle":"2025-08-21T05:20:39.516305Z","shell.execute_reply.started":"2025-08-21T05:20:39.47247Z","shell.execute_reply":"2025-08-21T05:20:39.515452Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\n### [Quoted reference figures](https://radiopaedia.org/articles/saccular-cerebral-aneurysm#nav_location)\n* Anterior circulation: `~90%`\n    * ACA/ACoA complex: `30-40%`\n    * Supraclinoid ICA and ICA/PCoA junction: `~30%`\n    * MCA: `20-30%`\n* Posterior circulation: `~10%`\n\n* [15-30% of patients](https://books.google.co.uk/books?vid=ISBN9781848821330&redir_esc=y) have multiple aneurysms\n\n### Dataset composition\n\n*Analysis of dataset locations*","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"},"attachments":{}},{"cell_type":"code","source":"grouped_locations = aneurysms.with_columns(\n    multiple_locations=(pl.col(\"total_aneurysm_locations\")>1).cast(int),\n    any_anterior=pl.any_horizontal(\n        [\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',\n             'Right Middle Cerebral Artery',\n             'Anterior Communicating Artery',\n             'Left Anterior Cerebral Artery',\n             'Right Anterior Cerebral Artery',\n        ]\n    ).cast(int),\n    any_aca_acoa_complex=pl.any_horizontal(\n        [\n            'Anterior Communicating Artery',\n            'Left Anterior Cerebral Artery',\n            'Right Anterior Cerebral Artery',\n        ]\n                                          ).cast(int),\n    any_ica=pl.any_horizontal(\n        [\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        ]\n    ).cast(int),\n    any_mca=pl.any_horizontal(\n        [\n             'Left Middle Cerebral Artery',\n             'Right Middle Cerebral Artery',\n        ]\n    ).cast(int),\n    any_posterior=pl.any_horizontal(\n        [\n             'Left Posterior Communicating Artery',\n             'Right Posterior Communicating Artery',\n             'Basilar Tip',\n             'Other Posterior Circulation',\n        ]\n    ).cast(int)\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T05:23:36.127228Z","iopub.execute_input":"2025-08-21T05:23:36.127624Z","iopub.status.idle":"2025-08-21T05:23:36.138618Z","shell.execute_reply.started":"2025-08-21T05:23:36.127597Z","shell.execute_reply":"2025-08-21T05:23:36.137634Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Percentages\n\n* Broadly similar to reference ranges quoted on [Radiopaedia](https://radiopaedia.org/articles/saccular-cerebral-aneurysm#nav_location)","metadata":{"execution":{"iopub.status.busy":"2025-08-21T05:23:46.314275Z","iopub.execute_input":"2025-08-21T05:23:46.314595Z","iopub.status.idle":"2025-08-21T05:23:46.318443Z","shell.execute_reply.started":"2025-08-21T05:23:46.314543Z","shell.execute_reply":"2025-08-21T05:23:46.317465Z"}}},{"cell_type":"code","source":"grouped_locations.select(\n    ['multiple_locations', 'any_anterior', 'any_aca_acoa_complex', 'any_ica', 'any_mca', 'any_posterior']\n).sum() / len(grouped_locations)*100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-21T05:23:27.318273Z","iopub.execute_input":"2025-08-21T05:23:27.318973Z","iopub.status.idle":"2025-08-21T05:23:27.326177Z","shell.execute_reply.started":"2025-08-21T05:23:27.318944Z","shell.execute_reply":"2025-08-21T05:23:27.325373Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}