{"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":"gpu","dataSources":[{"sourceId":99552,"databundleVersionId":13694723,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# <div style=\"color:#18ad4c; font-family: 'Segoe UI'; text-align: center; border-top:5px solid #3e3ec7; padding-left:10px; background-color:#F8F9F9; padding:10px; border-radius:5px;font-weight: bold\">🧠 Intracranial Aneurysm</div>\n\n## What is a Brain Aneurysm?\n<iframe width=\"720\" height=\"405\" src=\"https://www.youtube.com/embed/-2PjRfZYoyI\" title=\"Treating OCD: What Is Exposure With Response Prevention?\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen></iframe>\n\n## 3 Treatments for Aneurysm\n<iframe width=\"720\" height=\"405\" src=\"https://www.youtube.com/embed/rRklkA70O1w\" title=\"Treating OCD: What Is Exposure With Response Prevention?\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen></iframe>","metadata":{}},{"cell_type":"markdown","source":"## Understanding Data and Files\n\n### **`.dcm` (DICOM Files)**  \n- **What it is**: The **global standard format for clinical medical images** (X-rays, CT, MRI, etc.).  \n- **Key features**:  \n  - Stores **both image data + rich metadata** (patient name, scan date, machine settings, radiation dose, etc.).  \n  - Designed for **hospital interoperability** (works across scanners, PACS systems, and clinics).  \n  - Typically **2D slices** (a full 3D scan = many `.dcm` files in a folder).  \n- **Why it exists**: Ensures images + critical clinical info travel together safely (e.g., a CT scan of your brain arrives at the radiologist with your ID and scan parameters).  \n- **Analogy**: Like a **PDF with embedded legal/technical documentation**—not just an image, but a *complete clinical record*.  \n\n---\n\n### **`.nii` (NIfTI Files)**  \n- **What it is**: A **research-focused format for 3D/4D medical image data** (common in AI/neuroscience).  \n- **Key features**:  \n  - Stores **only the raw 3D/4D image data** (e.g., brain MRI volume) + **minimal metadata** (pixel size, coordinate system).  \n  - **Single file** for an entire 3D scan (unlike DICOM’s folder of slices).  \n  - Optimized for **computation** (easy to load into Python/MATLAB for analysis).  \n- **Why it exists**: Simplifies data for **researchers** (e.g., training AI models on brain scans without clinical bureaucracy).  \n- **Analogy**: Like a **CSV for 3D images**—lean, efficient, and ready for number-crunching.  \n\n---\n\n### **Key Difference**  \n| Format | Purpose | Data Included | Typical User |  \n|--------|---------|---------------|--------------|  \n| **`.dcm` (DICOM)** | **Clinical care** | Image + patient/scan metadata | Hospitals, radiologists |  \n| **`.nii` (NIfTI)** | **Research/AI** | Raw 3D image data only | Scientists, ML engineers |  \n\n> 💡 **Simple rule**:  \n> - **Hospitals use `.dcm`** (for patient safety/compliance).  \n> - **Researchers convert `.dcm` → `.nii`** to strip clinical metadata and simplify analysis.  \n\nBoth are foundational to medical imaging—one for **patient care**, the other for **discovery**. 🏥🔬","metadata":{}},{"cell_type":"markdown","source":"# <div style=\"color:#18ad4c; font-family: 'Segoe UI'; text-align: center; border-top:5px solid #815ecc; padding-left:10px; background-color:#F8F9F9; padding:10px; border-radius:5px;font-weight: bold\">📚 Libraries / Packages</div>","metadata":{}},{"cell_type":"code","source":"from glob import glob\nimport pydicom as dicom #for dicom files\nimport nibabel as nib #for nii files\n\nimport os\nimport shutil\nimport gc\nfrom collections import defaultdict\nfrom typing import Tuple, List\n\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport polars as pl\nimport pydicom\nfrom scipy import ndimage\nfrom sklearn.preprocessing import StandardScaler\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.utils.data import Dataset, DataLoader\nimport torch.optim as optim\n\nimport kaggle_evaluation.rsna_inference_server\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-09-09T08:03:35.8299Z","iopub.execute_input":"2025-09-09T08:03:35.830115Z","iopub.status.idle":"2025-09-09T08:03:44.949641Z","shell.execute_reply.started":"2025-09-09T08:03:35.830097Z","shell.execute_reply":"2025-09-09T08:03:44.949083Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <div style=\"color:#18ad4c; font-family: 'Segoe UI'; text-align: center; border-top:5px solid #3e3ec7; padding-left:10px; background-color:#F8F9F9; padding:10px; border-radius:5px;font-weight: bold\">📖 Load Data</div>\n\n## Dicom File","metadata":{}},{"cell_type":"code","source":"train_images = glob(\"/kaggle/input/rsna-intracranial-aneurysm-detection/series/1.2.826.0.1.3680043.8.498.10004044428023505108375152878107656647/*\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-09T08:03:44.951295Z","iopub.execute_input":"2025-09-09T08:03:44.951658Z","iopub.status.idle":"2025-09-09T08:03:44.974393Z","shell.execute_reply.started":"2025-09-09T08:03:44.95164Z","shell.execute_reply":"2025-09-09T08:03:44.973716Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## NIfTI File","metadata":{}},{"cell_type":"code","source":"path = '/kaggle/input/rsna-intracranial-aneurysm-detection/segmentations/1.2.826.0.1.3680043.8.498.10035643165968342618460849823699311381.nii'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-09T08:03:44.975024Z","iopub.execute_input":"2025-09-09T08:03:44.975206Z","iopub.status.idle":"2025-09-09T08:03:44.989498Z","shell.execute_reply.started":"2025-09-09T08:03:44.97519Z","shell.execute_reply":"2025-09-09T08:03:44.988806Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## CSV Files","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/rsna-intracranial-aneurysm-detection/train.csv\")\nlabel_df = pd.read_csv(\"/kaggle/input/rsna-intracranial-aneurysm-detection/train_localizers.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-09T08:03:44.990253Z","iopub.execute_input":"2025-09-09T08:03:44.990511Z","iopub.status.idle":"2025-09-09T08:03:45.051012Z","shell.execute_reply.started":"2025-09-09T08:03:44.990494Z","shell.execute_reply":"2025-09-09T08:03:45.050439Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <div style=\"color:#18ad4c; font-family: 'Segoe UI'; text-align: center; border-top:5px solid #3e3ec7; padding-left:10px; background-color:#F8F9F9; padding:10px; border-radius:5px;font-weight: bold\">📊 Exploratory Data Analysis</div>","metadata":{}},{"cell_type":"markdown","source":"## View dicom files","metadata":{}},{"cell_type":"code","source":"plt.style.use('default')\nfig, axes = plt.subplots(4,4, figsize=(12,12))\ntrain_images\nfor i, ax in enumerate(axes.reshape(-1)):\n    img_path = train_images[i]\n    img = dicom.dcmread(img_path)  \n    ax.imshow(img.pixel_array)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-09T08:03:45.051718Z","iopub.execute_input":"2025-09-09T08:03:45.051939Z","iopub.status.idle":"2025-09-09T08:03:47.235068Z","shell.execute_reply.started":"2025-09-09T08:03:45.051922Z","shell.execute_reply":"2025-09-09T08:03:47.234095Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## View nii files","metadata":{}},{"cell_type":"code","source":"img = nib.load(path).get_fdata()\nimg.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-09T08:03:47.235753Z","iopub.execute_input":"2025-09-09T08:03:47.235982Z","iopub.status.idle":"2025-09-09T08:03:49.657225Z","shell.execute_reply.started":"2025-09-09T08:03:47.235965Z","shell.execute_reply":"2025-09-09T08:03:49.656602Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.style.use('default')\nfig, axes = plt.subplots(4,4, figsize=(12,12))\nfor i, ax in enumerate(axes.reshape(-1)):\n    ax.imshow(img[:,:,1 + i])\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-09T08:03:49.659238Z","iopub.execute_input":"2025-09-09T08:03:49.659458Z","iopub.status.idle":"2025-09-09T08:03:51.37848Z","shell.execute_reply.started":"2025-09-09T08:03:49.65944Z","shell.execute_reply":"2025-09-09T08:03:51.37779Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Exploring Data","metadata":{}},{"cell_type":"code","source":"# Check class imbalance\nprint(\"Aneurysm Present: 1 =\", train_df['Aneurysm Present'].mean()*100, \"%\")\n# Check modality distribution\nprint(train_df['Modality'].value_counts())\n# Check location-wise prevalence (critical for multi-label)\nlocations = [col for col in train_df.columns if 'Artery' in col or 'Communicating' in col]\nprint(train_df[locations].sum() / len(train_df))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-09T08:03:51.379345Z","iopub.execute_input":"2025-09-09T08:03:51.379632Z","iopub.status.idle":"2025-09-09T08:03:51.397181Z","shell.execute_reply.started":"2025-09-09T08:03:51.379609Z","shell.execute_reply":"2025-09-09T08:03:51.396442Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-09T08:03:51.398Z","iopub.execute_input":"2025-09-09T08:03:51.398281Z","iopub.status.idle":"2025-09-09T08:03:51.426995Z","shell.execute_reply.started":"2025-09-09T08:03:51.398254Z","shell.execute_reply":"2025-09-09T08:03:51.426335Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['PatientAge'].describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-09T08:03:51.427642Z","iopub.execute_input":"2025-09-09T08:03:51.427856Z","iopub.status.idle":"2025-09-09T08:03:51.436591Z","shell.execute_reply.started":"2025-09-09T08:03:51.42784Z","shell.execute_reply":"2025-09-09T08:03:51.435907Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['PatientAge'] = train_df['PatientAge'].astype(int)\nplt.figure(figsize=(10,6))\nsns.histplot(train_df['PatientAge'], bins=20, kde=False, color=sns.color_palette(\"rocket\")[4])  \nplt.xlabel('Patient Age')\nplt.ylabel('Count')\nplt.title('Distribution of Patient Age')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-09T08:03:51.43739Z","iopub.execute_input":"2025-09-09T08:03:51.437557Z","iopub.status.idle":"2025-09-09T08:03:51.64245Z","shell.execute_reply.started":"2025-09-09T08:03:51.43754Z","shell.execute_reply":"2025-09-09T08:03:51.641815Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Create cross-tabulation with proportions or counts\nctab = pd.crosstab(train_df['PatientSex'], train_df['Aneurysm Present'])\n\n# Plot grouped bar chart\nctab.plot(kind='bar', \n          color=sns.color_palette(\"pastel\"), \n          figsize=(8, 6), \n          width=0.8)\n\n# Labels and title\nplt.xlabel('Patient Sex')\nplt.ylabel('Count')\nplt.title('Aneurysm Presence by Patient Sex')\nplt.legend(title='Aneurysm Present', labels=['No', 'Yes'])\nplt.xticks(rotation=0)\n\n# Add value labels on bars (optional, improves readability)\nfor container in plt.gca().containers:\n    plt.bar_label(container, fmt='%d', padding=3)\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-09T08:03:51.643356Z","iopub.execute_input":"2025-09-09T08:03:51.643541Z","iopub.status.idle":"2025-09-09T08:03:52.076061Z","shell.execute_reply.started":"2025-09-09T08:03:51.643527Z","shell.execute_reply":"2025-09-09T08:03:52.075289Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot pie chart with counts shown on each slice\ntrain_df['Modality'].value_counts().plot(kind='pie', autopct='%d')\n\n# Optional: Improve layout and title\nplt.title('Distribution of Modality')\nplt.ylabel('')  # Hide the y-label (default is 'Modality' from pandas)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-09T08:03:52.076998Z","iopub.execute_input":"2025-09-09T08:03:52.077255Z","iopub.status.idle":"2025-09-09T08:03:52.188785Z","shell.execute_reply.started":"2025-09-09T08:03:52.077231Z","shell.execute_reply":"2025-09-09T08:03:52.188137Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-09T08:03:52.189473Z","iopub.execute_input":"2025-09-09T08:03:52.189662Z","iopub.status.idle":"2025-09-09T08:03:52.197916Z","shell.execute_reply.started":"2025-09-09T08:03:52.189647Z","shell.execute_reply":"2025-09-09T08:03:52.197303Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <div style=\"color:#18ad4c; font-family: 'Segoe UI'; text-align: center; border-top:5px solid #3e3ec7; padding-left:10px; background-color:#F8F9F9; padding:10px; border-radius:5px;font-weight: bold\">💎 Features in Data Files</div>","metadata":{}},{"cell_type":"markdown","source":"## Constants","metadata":{}},{"cell_type":"code","source":"ID_COL = 'SeriesInstanceUID'\n\nLABEL_COLS = [\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    'Left Posterior Communicating Artery',\n    'Right Posterior Communicating Artery',\n    'Basilar Tip',\n    'Other Posterior Circulation',\n    'Aneurysm Present',\n]\n\nDICOM_TAG_ALLOWLIST = [\n    'BitsAllocated', 'BitsStored', 'Columns', 'FrameOfReferenceUID', 'HighBit',\n    'ImageOrientationPatient', 'ImagePositionPatient', 'InstanceNumber', 'Modality',\n    'PatientID', 'PhotometricInterpretation', 'PixelRepresentation', 'PixelSpacing',\n    'PlanarConfiguration', 'RescaleIntercept', 'RescaleSlope', 'RescaleType', 'Rows',\n    'SOPClassUID', 'SOPInstanceUID', 'SamplesPerPixel', 'SliceThickness',\n    'SpacingBetweenSlices', 'StudyInstanceUID', 'TransferSyntaxUID',\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-09T08:03:52.1987Z","iopub.execute_input":"2025-09-09T08:03:52.198976Z","iopub.status.idle":"2025-09-09T08:03:52.212538Z","shell.execute_reply.started":"2025-09-09T08:03:52.198951Z","shell.execute_reply":"2025-09-09T08:03:52.211791Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Details:** \n- ID_COL identifies scan series.\n- LABEL_COLS contains all 14 target variables (13 artery locations + overall presence).\n- DICOM_TAG_ALLOWLIST Lists exactly which DICOM metadata tags are allowed (Competition restricts available metadata to mimic real-world clinical constraints).","metadata":{}},{"cell_type":"markdown","source":"# <div style=\"color:#18ad4c; font-family: 'Segoe UI'; text-align: center; border-top:5px solid #3e3ec7; padding-left:10px; background-color:#F8F9F9; padding:10px; border-radius:5px;font-weight: bold\">📚 Preprocess, Train, Predict Notebooks</div>\n\nFurther step by step solution is present in the following notebooks. \n1. [Preprocess Files](https://www.kaggle.com/code/taimour/preprocess-files-for-aneurysm-3)\n2. [MR Train Model](https://www.kaggle.com/code/taimour/mr-train-model-for-aneurysm-3)\n3. [CT Train Model](https://www.kaggle.com/code/taimour/ct-train-model-for-aneurysm-3)\n4. [Predict Intracranial Aneurysm](https://www.kaggle.com/code/taimour/predict-aneurysm-3)\n","metadata":{}}]}