{"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":13851420,"sourceType":"competition"}],"dockerImageVersionId":31153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"data_path = Path(\"/kaggle/input/rsna-intracranial-aneurysm-detection\") print(\"Files and folders inside dataset:\") print(os.listdir(data_path))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:45:06.873607Z","iopub.execute_input":"2025-10-11T19:45:06.873895Z","iopub.status.idle":"2025-10-11T19:45:06.880059Z","shell.execute_reply.started":"2025-10-11T19:45:06.873874Z","shell.execute_reply":"2025-10-11T19:45:06.879036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ntrain = pd.read_csv(\"/kaggle/input/rsna-intracranial-aneurysm-detection/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:27:56.943494Z","iopub.execute_input":"2025-10-11T19:27:56.943725Z","iopub.status.idle":"2025-10-11T19:27:58.469254Z","shell.execute_reply.started":"2025-10-11T19:27:56.943706Z","shell.execute_reply":"2025-10-11T19:27:58.468202Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"First 5 rows of the dataset:\")\ndisplay(train.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:28:04.684412Z","iopub.execute_input":"2025-10-11T19:28:04.6848Z","iopub.status.idle":"2025-10-11T19:28:04.715385Z","shell.execute_reply.started":"2025-10-11T19:28:04.684767Z","shell.execute_reply":"2025-10-11T19:28:04.714522Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:28:44.911251Z","iopub.execute_input":"2025-10-11T19:28:44.911568Z","iopub.status.idle":"2025-10-11T19:28:44.922563Z","shell.execute_reply.started":"2025-10-11T19:28:44.911519Z","shell.execute_reply":"2025-10-11T19:28:44.921612Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:29:13.073309Z","iopub.execute_input":"2025-10-11T19:29:13.073637Z","iopub.status.idle":"2025-10-11T19:29:13.080065Z","shell.execute_reply.started":"2025-10-11T19:29:13.073612Z","shell.execute_reply":"2025-10-11T19:29:13.079087Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:29:46.166097Z","iopub.execute_input":"2025-10-11T19:29:46.166416Z","iopub.status.idle":"2025-10-11T19:29:46.276524Z","shell.execute_reply.started":"2025-10-11T19:29:46.166384Z","shell.execute_reply":"2025-10-11T19:29:46.27561Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train['PatientSex'].value_counts())\nprint(train['Modality'].value_counts())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:34:17.525473Z","iopub.execute_input":"2025-10-11T19:34:17.526509Z","iopub.status.idle":"2025-10-11T19:34:17.535248Z","shell.execute_reply.started":"2025-10-11T19:34:17.526474Z","shell.execute_reply":"2025-10-11T19:34:17.534306Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n\nplt.figure(figsize=(8,5))\nsns.histplot(train['PatientAge'], bins=30, kde=True)\nplt.title('Age Distribution of Patients')\nplt.xlabel('Age')\nplt.ylabel('Count')\nplt.show()\n\n\nplt.figure(figsize=(6,4))\nsns.countplot(data=train, x='PatientSex', hue='Aneurysm Present')\nplt.title('Aneurysm Presence by Gender')\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:35:02.219101Z","iopub.execute_input":"2025-10-11T19:35:02.219441Z","iopub.status.idle":"2025-10-11T19:35:03.794658Z","shell.execute_reply.started":"2025-10-11T19:35:02.219413Z","shell.execute_reply":"2025-10-11T19:35:03.793588Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Step 5: Correlation Heatmap among Artery Columns\nartery_cols = [\n    'Left Infraclinoid Internal Carotid Artery', 'Right Infraclinoid Internal Carotid Artery',\n    'Left Supraclinoid Internal Carotid Artery', 'Right Supraclinoid Internal Carotid Artery',\n    'Left Middle Cerebral Artery', 'Right Middle Cerebral Artery',\n    'Anterior Communicating Artery', 'Left Anterior Cerebral Artery', 'Right Anterior Cerebral Artery',\n    'Left Posterior Communicating Artery', 'Right Posterior Communicating Artery',\n    'Basilar Tip', 'Other Posterior Circulation'\n]\n\nplt.figure(figsize=(10,8))\nsns.heatmap(train[artery_cols].corr(), annot=True, cmap='coolwarm')\nplt.title('Correlation Between Artery Aneurysm Locations')\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:35:46.973364Z","iopub.execute_input":"2025-10-11T19:35:46.973879Z","iopub.status.idle":"2025-10-11T19:35:47.68338Z","shell.execute_reply.started":"2025-10-11T19:35:46.973853Z","shell.execute_reply":"2025-10-11T19:35:47.68243Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isnull().sum()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:38:02.444586Z","iopub.execute_input":"2025-10-11T19:38:02.444852Z","iopub.status.idle":"2025-10-11T19:38:02.453216Z","shell.execute_reply.started":"2025-10-11T19:38:02.444834Z","shell.execute_reply":"2025-10-11T19:38:02.452246Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ntrain.groupby('Aneurysm Present')['PatientAge'].describe()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:38:58.046221Z","iopub.execute_input":"2025-10-11T19:38:58.046529Z","iopub.status.idle":"2025-10-11T19:38:58.079455Z","shell.execute_reply.started":"2025-10-11T19:38:58.046501Z","shell.execute_reply":"2025-10-11T19:38:58.078357Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(8,6))\nsns.boxplot(x='Aneurysm Present', y='PatientAge', data=train, palette='Set2')\nplt.title('Age vs Aneurysm Presence')\nplt.xlabel('Aneurysm Present (0 = No, 1 = Yes)')\nplt.ylabel('Age')\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:39:26.437926Z","iopub.execute_input":"2025-10-11T19:39:26.438247Z","iopub.status.idle":"2025-10-11T19:39:26.608033Z","shell.execute_reply.started":"2025-10-11T19:39:26.438223Z","shell.execute_reply":"2025-10-11T19:39:26.607132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"gender_vs_aneurysm = pd.crosstab(train['PatientSex'], train['Aneurysm Present'], normalize='index') * 100\nprint(gender_vs_aneurysm)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:40:07.921678Z","iopub.execute_input":"2025-10-11T19:40:07.921952Z","iopub.status.idle":"2025-10-11T19:40:07.937385Z","shell.execute_reply.started":"2025-10-11T19:40:07.921933Z","shell.execute_reply":"2025-10-11T19:40:07.936462Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize=(6,4))\nsns.countplot(data=train, x='PatientSex', hue='Aneurysm Present', palette='Set2')\nplt.title('Gender vs Aneurysm Presence')\nplt.xlabel('Gender')\nplt.ylabel('Count')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:40:37.525733Z","iopub.execute_input":"2025-10-11T19:40:37.526004Z","iopub.status.idle":"2025-10-11T19:40:37.702854Z","shell.execute_reply.started":"2025-10-11T19:40:37.525987Z","shell.execute_reply":"2025-10-11T19:40:37.701824Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize=(8,6))\nsns.boxplot(data=train, x='PatientSex', y='PatientAge', hue='Aneurysm Present', palette='coolwarm')\nplt.title('Age vs Aneurysm Presence by Sex')\nplt.xlabel('Patient Sex')\nplt.ylabel('Age')\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:41:34.915777Z","iopub.execute_input":"2025-10-11T19:41:34.916076Z","iopub.status.idle":"2025-10-11T19:41:35.16637Z","shell.execute_reply.started":"2025-10-11T19:41:34.91605Z","shell.execute_reply":"2025-10-11T19:41:35.165605Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(10,6))\nsns.boxplot(data=train, x='Modality', y='PatientAge', hue='Aneurysm Present', palette='viridis')\nplt.title('Age vs Modality by Aneurysm Presence')\nplt.xlabel('Imaging Modality')\nplt.ylabel('Patient Age')\nplt.xticks(rotation=45)\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:42:00.445325Z","iopub.execute_input":"2025-10-11T19:42:00.445664Z","iopub.status.idle":"2025-10-11T19:42:00.798678Z","shell.execute_reply.started":"2025-10-11T19:42:00.445637Z","shell.execute_reply":"2025-10-11T19:42:00.797808Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"artery_cols = train.columns[4:-1]  # all artery-related columns\nartery_means = train.groupby('Aneurysm Present')[artery_cols].mean().T\n\nartery_means.plot(kind='bar', figsize=(12,6), color=['skyblue', 'salmon'])\nplt.title('Average Artery Involvement by Aneurysm Presence')\nplt.ylabel('Proportion of Cases')\nplt.xlabel('Artery')\nplt.legend(['No Aneurysm', 'Aneurysm Present'])\nplt.xticks(rotation=90)\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:42:30.110743Z","iopub.execute_input":"2025-10-11T19:42:30.111035Z","iopub.status.idle":"2025-10-11T19:42:30.505105Z","shell.execute_reply.started":"2025-10-11T19:42:30.111012Z","shell.execute_reply":"2025-10-11T19:42:30.504256Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Select only numeric columns\nnumeric_df = train.select_dtypes(include=['int64', 'float64'])\n\n# Compute correlation matrix\ncorr_matrix = numeric_df.corr()\n\n# Plot heatmap\nplt.figure(figsize=(12,8))\nsns.heatmap(corr_matrix, annot=False, cmap='coolwarm', linewidths=0.3)\nplt.title('Correlation Heatmap')\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:43:19.27602Z","iopub.execute_input":"2025-10-11T19:43:19.276352Z","iopub.status.idle":"2025-10-11T19:43:19.725574Z","shell.execute_reply.started":"2025-10-11T19:43:19.27633Z","shell.execute_reply":"2025-10-11T19:43:19.724718Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"corr_with_target = corr_matrix['Aneurysm Present'].sort_values(ascending=False)\nprint(corr_with_target)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:43:34.95376Z","iopub.execute_input":"2025-10-11T19:43:34.954045Z","iopub.status.idle":"2025-10-11T19:43:34.960839Z","shell.execute_reply.started":"2025-10-11T19:43:34.954027Z","shell.execute_reply":"2025-10-11T19:43:34.960015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nfrom pathlib import Path\n\nseries_path = Path(\"/kaggle/input/rsna-intracranial-aneurysm-detection/series\")\n\nprint(\"Sample folders/files inside 'series':\")\nprint(os.listdir(series_path)[:10])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:46:19.360459Z","iopub.execute_input":"2025-10-11T19:46:19.361033Z","iopub.status.idle":"2025-10-11T19:46:19.36825Z","shell.execute_reply.started":"2025-10-11T19:46:19.361009Z","shell.execute_reply":"2025-10-11T19:46:19.367513Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_series = series_path / os.listdir(series_path)[0]  # pick the first series\nprint(\"Inspecting folder:\", sample_series)\n\n# List first few files inside that folder\nprint(os.listdir(sample_series)[:10])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:46:44.986496Z","iopub.execute_input":"2025-10-11T19:46:44.986841Z","iopub.status.idle":"2025-10-11T19:46:45.013325Z","shell.execute_reply.started":"2025-10-11T19:46:44.986817Z","shell.execute_reply":"2025-10-11T19:46:45.012315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom\nimport matplotlib.pyplot as plt\n\n# Select one DICOM file from the sample folder\nsample_dicom_path = sample_series / os.listdir(sample_series)[0]\n\n# Read the DICOM file\ndicom = pydicom.dcmread(sample_dicom_path)\n\n# Print DICOM metadata\nprint(\"DICOM metadata:\")\nprint(dicom)\n\n# Display the image\nplt.imshow(dicom.pixel_array, cmap='gray')\nplt.title(\"Sample DICOM Image\")\nplt.axis(\"off\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:48:49.498325Z","iopub.execute_input":"2025-10-11T19:48:49.49867Z","iopub.status.idle":"2025-10-11T19:48:49.639732Z","shell.execute_reply.started":"2025-10-11T19:48:49.498643Z","shell.execute_reply":"2025-10-11T19:48:49.638789Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import random\n\n# Get 9 random dicom files from the same series\ndicom_files = os.listdir(sample_series)\nsample_files = random.sample(dicom_files, 9)\n\nfig, axes = plt.subplots(3, 3, figsize=(10, 10))\nfor i, f in enumerate(sample_files):\n    dicom_path = sample_series / f\n    img = pydicom.dcmread(dicom_path).pixel_array\n    ax = axes[i // 3, i % 3]\n    ax.imshow(img, cmap='gray')\n    ax.axis('off')\nplt.suptitle(\"Random MRI Slices from Same Patient\", fontsize=16)\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:50:20.320129Z","iopub.execute_input":"2025-10-11T19:50:20.320604Z","iopub.status.idle":"2025-10-11T19:50:20.961271Z","shell.execute_reply.started":"2025-10-11T19:50:20.320573Z","shell.execute_reply":"2025-10-11T19:50:20.960133Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\n\n# Pick one random file from the series\nrandom_file = random.choice(os.listdir(sample_series))\nfile_path = sample_series / random_file\n\n# Read image\ndicom_img = pydicom.dcmread(file_path)\nimg = dicom_img.pixel_array\n\n# Plot histogram of pixel intensities\nplt.figure(figsize=(8,5))\nplt.hist(img.ravel(), bins=100, color='gray')\nplt.title(\"Pixel Intensity Distribution in One MRI Slice\")\nplt.xlabel(\"Pixel Intensity\")\nplt.ylabel(\"Frequency\")\nplt.show()\n\n# Display the actual image\nplt.figure(figsize=(5,5))\nplt.imshow(img, cmap='gray')\nplt.axis('off')\nplt.title(\"Sample MRI Slice\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-11T19:51:35.794632Z","iopub.execute_input":"2025-10-11T19:51:35.794956Z","iopub.status.idle":"2025-10-11T19:51:36.479449Z","shell.execute_reply.started":"2025-10-11T19:51:35.794933Z","shell.execute_reply":"2025-10-11T19:51:36.478615Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom\nimport numpy as np\nimport os\nfrom pathlib import Path\n\n# Define your dataset path again\nseries_path = Path(\"/kaggle/input/rsna-intracranial-aneurysm-detection/series\")\n\n# Select the same sample series folder\nsample_series = series_path / os.listdir(series_path)[0]\n\n\n# Read and sort all DICOM slices by InstanceNumber\ndicom_files = []\nfor f in os.listdir(sample_series):\n    ds = pydicom.dcmread(sample_series / f)\n    dicom_files.append(ds)\ndicom_files = sorted(dicom_files, key=lambda d: getattr(d, \"InstanceNumber\", 0))\n\n# Stack into 3D volume\nvolume = np.stack([ds.pixel_array for ds in dicom_files]).astype(np.float32)\n\n# Normalize to [0, 1]\nvolume = (volume - np.min(volume)) / (np.max(volume) - np.min(volume))\n\nprint(\"Volume restored with shape:\", volume.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-12T07:39:37.656976Z","iopub.execute_input":"2025-10-12T07:39:37.657346Z","iopub.status.idle":"2025-10-12T07:39:40.797645Z","shell.execute_reply.started":"2025-10-12T07:39:37.657313Z","shell.execute_reply":"2025-10-12T07:39:40.796614Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(figsize=(8,5))\nplt.hist(volume.ravel(), bins=100, color='purple')\nplt.title(\"Pixel Intensity Distribution in Full 3D Volume (Normalized)\")\nplt.xlabel(\"Normalized Intensity\")\nplt.ylabel(\"Frequency\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-12T07:40:57.217601Z","iopub.execute_input":"2025-10-12T07:40:57.218733Z","iopub.status.idle":"2025-10-12T07:40:57.997259Z","shell.execute_reply.started":"2025-10-12T07:40:57.218686Z","shell.execute_reply":"2025-10-12T07:40:57.996101Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\n\nprint(\"Original volume shape:\", volume.shape)\n\n# Let's resize each slice to a smaller shape (for faster computation)\ntarget_size = (128, 128)  # (width, height)\nresized_slices = [cv2.resize(slice, target_size) for slice in volume]\n\nresized_volume = np.stack(resized_slices)\nprint(\"Resized volume shape:\", resized_volume.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-12T07:41:01.998753Z","iopub.execute_input":"2025-10-12T07:41:01.999068Z","iopub.status.idle":"2025-10-12T07:41:02.083981Z","shell.execute_reply.started":"2025-10-12T07:41:01.999044Z","shell.execute_reply":"2025-10-12T07:41:02.082842Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# load_preprocessed.py\nimport numpy as np\n\ndef load_train_data():\n    X = np.load('data/preprocessed/X_train.npy')\n    y = np.load('data/preprocessed/y_train.npy')\n    return X, y\n\ndef load_test_data():\n    X = np.load('data/preprocessed/X_test.npy')\n    y = np.load('data/preprocessed/y_test.npy')\n    return X, y\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-12T07:41:04.890251Z","iopub.execute_input":"2025-10-12T07:41:04.890571Z","iopub.status.idle":"2025-10-12T07:41:04.897532Z","shell.execute_reply.started":"2025-10-12T07:41:04.890543Z","shell.execute_reply":"2025-10-12T07:41:04.896069Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\n\nprint(\"Original volume shape:\", volume.shape)\n\n# Let's resize each slice to a smaller shape (for faster computation)\ntarget_size = (128, 128)  # (width, height)\nresized_slices = [cv2.resize(slice, target_size) for slice in volume]\n\nresized_volume = np.stack(resized_slices)\nprint(\"Resized volume shape:\", resized_volume.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-12T07:41:07.904909Z","iopub.execute_input":"2025-10-12T07:41:07.905273Z","iopub.status.idle":"2025-10-12T07:41:07.942671Z","shell.execute_reply.started":"2025-10-12T07:41:07.905244Z","shell.execute_reply":"2025-10-12T07:41:07.94166Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.figure(figsize=(12,4))\nfor i, idx in enumerate([20, 60, 100]):\n    plt.subplot(1, 3, i+1)\n    plt.imshow(resized_volume[idx], cmap='gray')\n    plt.title(f\"Slice {idx}\")\n    plt.axis('off')\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-12T07:42:07.575109Z","iopub.execute_input":"2025-10-12T07:42:07.575467Z","iopub.status.idle":"2025-10-12T07:42:07.885062Z","shell.execute_reply.started":"2025-10-12T07:42:07.575444Z","shell.execute_reply":"2025-10-12T07:42:07.883727Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Normalization\nnormalized_volume = (resized_volume - resized_volume.min()) / (resized_volume.max() - resized_volume.min())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-12T07:43:10.472965Z","iopub.execute_input":"2025-10-12T07:43:10.473337Z","iopub.status.idle":"2025-10-12T07:43:10.489217Z","shell.execute_reply.started":"2025-10-12T07:43:10.473311Z","shell.execute_reply":"2025-10-12T07:43:10.488139Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Standardization\nstandardized_volume = (resized_volume - np.mean(resized_volume)) / np.std(resized_volume)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-12T07:43:22.669982Z","iopub.execute_input":"2025-10-12T07:43:22.670328Z","iopub.status.idle":"2025-10-12T07:43:22.691919Z","shell.execute_reply.started":"2025-10-12T07:43:22.670296Z","shell.execute_reply":"2025-10-12T07:43:22.690861Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Pick some evenly spaced slice indices\nslice_indices = [20, 60, 100]\n\nplt.figure(figsize=(15, 8))\n\n# Show normalized volume slices\nfor i, idx in enumerate(slice_indices):\n    plt.subplot(2, len(slice_indices), i + 1)\n    plt.imshow(normalized_volume[idx], cmap='gray')\n    plt.title(f\"Normalized Slice {idx}\")\n    plt.axis('off')\n\n# Show standardized volume slices\nfor i, idx in enumerate(slice_indices):\n    plt.subplot(2, len(slice_indices), len(slice_indices) + i + 1)\n    plt.imshow(standardized_volume[idx], cmap='gray')\n    plt.title(f\"Standardized Slice {idx}\")\n    plt.axis('off')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-12T07:45:42.631446Z","iopub.execute_input":"2025-10-12T07:45:42.631796Z","iopub.status.idle":"2025-10-12T07:45:43.371175Z","shell.execute_reply.started":"2025-10-12T07:45:42.631773Z","shell.execute_reply":"2025-10-12T07:45:43.370062Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nfrom pathlib import Path\n\n# Create a folder to store preprocessed data\nsave_path = Path(\"preprocessed_data\")\nsave_path.mkdir(exist_ok=True)\n\n# Save normalized and standardized volumes\nnp.save(save_path / \"normalized_volume.npy\", normalized_volume)\nnp.save(save_path / \"standardized_volume.npy\", standardized_volume)\n\nprint(\"✅ Volumes saved successfully!\")\nprint(f\"Files saved in: {save_path.resolve()}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-12T07:46:52.900443Z","iopub.execute_input":"2025-10-12T07:46:52.900778Z","iopub.status.idle":"2025-10-12T07:46:52.925004Z","shell.execute_reply.started":"2025-10-12T07:46:52.900755Z","shell.execute_reply":"2025-10-12T07:46:52.923917Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom\nimport numpy as np\nimport cv2\nimport os\nfrom pathlib import Path\nimport matplotlib.pyplot as plt\n\n# Define the patient folder\npatient_folder = Path(\"/kaggle/input/rsna-intracranial-aneurysm-detection/series/1.2.826.0.1.3680043.8.498.96155132589464464482407972219172224468\")\n\n# Create a list of all DICOM file paths\ndicom_files = sorted([patient_folder / f for f in os.listdir(patient_folder) if f.endswith('.dcm')])\n\n# Initialize list to store processed images\nprocessed_slices = []\n\n# Loop through and process each DICOM slice\nfor f in dicom_files:\n    ds = pydicom.dcmread(f)\n    img = ds.pixel_array.astype(np.float32)\n    \n    # Normalize intensity to [0, 1]\n    img = (img - np.min(img)) / (np.max(img) - np.min(img))\n    \n    # Resize to 128x128 (you can change to 256x256 if you have more memory)\n    img_resized = cv2.resize(img, (128, 128))\n    \n    processed_slices.append(img_resized)\n\n# Convert to numpy array\nprocessed_slices = np.array(processed_slices)\n\nprint(\"Processed slices shape:\", processed_slices.shape)\nprint(\"Min pixel value:\", processed_slices.min())\nprint(\"Max pixel value:\", processed_slices.max())\n\n# Display sample preprocessed slices\nplt.figure(figsize=(10, 8))\nfor i, slice_idx in enumerate(np.linspace(0, len(processed_slices)-1, 9, dtype=int)):\n    plt.subplot(3, 3, i+1)\n    plt.imshow(processed_slices[slice_idx], cmap='gray')\n    plt.axis('off')\nplt.suptitle(\"Normalized and Resized MRI Slices\", fontsize=14)\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-12T08:00:56.57591Z","iopub.execute_input":"2025-10-12T08:00:56.576295Z","iopub.status.idle":"2025-10-12T08:00:58.855999Z","shell.execute_reply.started":"2025-10-12T08:00:56.576272Z","shell.execute_reply":"2025-10-12T08:00:58.855031Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pydicom\nimport cv2\nfrom pathlib import Path\nfrom tqdm import tqdm\n\n# Paths\nseries_path = Path(\"/kaggle/input/rsna-intracranial-aneurysm-detection/series\")\noutput_dir = Path(\"preprocessed_data\")\noutput_dir.mkdir(exist_ok=True)\n\n# Target size for resizing\ntarget_shape = (128, 128)\n\n# Function to load, resize, and normalize a DICOM series\ndef load_and_preprocess_series(folder_path):\n    dicom_files = sorted(\n        [pydicom.dcmread(folder_path / f) for f in os.listdir(folder_path) if f.endswith(\".dcm\")],\n        key=lambda x: int(x.InstanceNumber)\n    )\n\n    # Stack slices to form a 3D volume\n    volume = np.stack([ds.pixel_array for ds in dicom_files], axis=0)\n\n    # Resize each slice to 128x128\n    resized_volume = np.stack([cv2.resize(slice_, target_shape) for slice_ in volume], axis=0)\n\n    # Normalize (0–1)\n    normalized = (resized_volume - np.min(resized_volume)) / (np.max(resized_volume) - np.min(resized_volume) + 1e-8)\n\n    # Standardize (mean=0, std=1)\n    standardized = (normalized - np.mean(normalized)) / (np.std(normalized) + 1e-8)\n    return standardized\n\n# Loop through all patient folders and preprocess\nfor patient_folder in tqdm(os.listdir(series_path)):\n    folder_path = series_path / patient_folder\n    if folder_path.is_dir():\n        try:\n            volume = load_and_preprocess_series(folder_path)\n            np.save(output_dir / f\"{patient_folder}.npy\", volume)\n        except Exception as e:\n            print(f\"Skipping {patient_folder} due to error: {e}\")\n\nprint(\"All available series processed and saved in 'preprocessed_data/'\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-12T17:26:56.920598Z","iopub.execute_input":"2025-10-12T17:26:56.920978Z","execution_failed":"2025-10-12T20:18:26.798Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!du -sh preprocessed_data\n!ls preprocessed_data | wc -l\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-12T20:15:33.880986Z","iopub.execute_input":"2025-10-12T20:15:33.881255Z","iopub.status.idle":"2025-10-12T20:15:34.128135Z","shell.execute_reply.started":"2025-10-12T20:15:33.881228Z","shell.execute_reply":"2025-10-12T20:15:34.127147Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls -lh\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-12T20:17:24.695773Z","iopub.execute_input":"2025-10-12T20:17:24.696113Z","iopub.status.idle":"2025-10-12T20:17:24.816511Z","shell.execute_reply.started":"2025-10-12T20:17:24.696084Z","shell.execute_reply":"2025-10-12T20:17:24.815423Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!df -h\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-12T20:21:42.251895Z","iopub.execute_input":"2025-10-12T20:21:42.252242Z","iopub.status.idle":"2025-10-12T20:21:42.376371Z","shell.execute_reply.started":"2025-10-12T20:21:42.252215Z","shell.execute_reply":"2025-10-12T20:21:42.375461Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!df -i\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\noutput_dir = \"/kaggle/working/preprocessed_images\"\nos.makedirs(output_dir, exist_ok=True)\nprint(\"Output directory ready:\", output_dir)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-12T20:24:56.096521Z","iopub.execute_input":"2025-10-12T20:24:56.097317Z","iopub.status.idle":"2025-10-12T20:24:56.102757Z","shell.execute_reply.started":"2025-10-12T20:24:56.097285Z","shell.execute_reply":"2025-10-12T20:24:56.101998Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pydicom\nfrom tqdm import tqdm\nfrom pathlib import Path\nfrom scipy.ndimage import zoom\n\nseries_root = Path(\"/kaggle/input/rsna-intracranial-aneurysm-detection/series\")\noutput_dir = Path(\"/kaggle/working/preprocessed_images\")\noutput_dir.mkdir(exist_ok=True)\n\n# List all patient folders\nseries_list = sorted(os.listdir(series_root))\n\n# Detect already-processed files (if resuming)\ndone = [f.replace(\".npy\", \"\") for f in os.listdir(output_dir)]\nremaining = [s for s in series_list if s not in done]\n\nprint(f\"Already done: {len(done)} | Remaining: {len(remaining)}\")\n\n# Target shape (H, W, D)\nTARGET_SHAPE = (128, 128, 128)\n\ndef preprocess_volume(volume, target_shape=TARGET_SHAPE):\n    # Make sure it's 3D\n    if volume.ndim == 2:\n        volume = volume[np.newaxis, :, :]  # add depth axis\n    \n    h, w, d = volume.shape\n    zoom_factors = (target_shape[0]/h, target_shape[1]/w, target_shape[2]/d)\n    resized = zoom(volume, zoom_factors, order=1)  # linear interpolation\n\n    # Normalize to 0-1\n    resized = resized.astype(np.float32)\n    resized = (resized - resized.min()) / (resized.max() - resized.min() + 1e-8)\n    \n    # Standardize\n    resized = (resized - resized.mean()) / (resized.std() + 1e-8)\n    \n    return resized\n\nfor i, series_id in enumerate(tqdm(remaining)):\n    series_path = series_root / series_id\n    try:\n        # Read all DICOM slices\n        slices = []\n        for f in sorted(os.listdir(series_path)):\n            ds = pydicom.dcmread(series_path / f)\n            img = ds.pixel_array.astype(np.float32)\n            slices.append(img)\n\n        volume = np.stack(slices) if len(slices) > 1 else slices[0]\n\n        # Preprocess\n        standardized = preprocess_volume(volume, target_shape=TARGET_SHAPE)\n\n        # Save\n        np.save(output_dir / f\"{series_id}.npy\", standardized)\n\n       \n    except Exception as e:\n        print(f\"Skipping {series_id} due to error:\", e)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}