{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n\n# Use the kagglehub client library to attach Kaggle resources like competitions, datasets, and models to your session\n# Learn more about kagglehub: https://github.com/Kaggle/kagglehub/blob/main/README.md\n\nimport kagglehub\n# kagglehub.dataset_download('<owner>/<dataset-slug>')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-09-20T06:42:26.349451Z","iopub.execute_input":"2026-09-20T06:42:26.350374Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\n\nprint(os.listdir(\"/kaggle/input\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T06:56:49.937479Z","iopub.execute_input":"2026-09-20T06:56:49.937784Z","iopub.status.idle":"2026-09-20T06:56:49.943488Z","shell.execute_reply.started":"2026-09-20T06:56:49.937758Z","shell.execute_reply":"2026-09-20T06:56:49.942618Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DATA_PATH = \"/kaggle/input/competitions/rsna-knee-abnormality-detection\"\n\nprint(os.listdir(DATA_PATH))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T06:56:14.13081Z","iopub.execute_input":"2026-09-20T06:56:14.131243Z","iopub.status.idle":"2026-09-20T06:56:14.136403Z","shell.execute_reply.started":"2026-09-20T06:56:14.13121Z","shell.execute_reply":"2026-09-20T06:56:14.135638Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom\nimport matplotlib.pyplot as plt\nfrom PIL import Image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T06:57:47.791114Z","iopub.execute_input":"2026-09-20T06:57:47.791479Z","iopub.status.idle":"2026-09-20T06:57:48.356814Z","shell.execute_reply.started":"2026-09-20T06:57:47.791452Z","shell.execute_reply":"2026-09-20T06:57:48.356076Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv(os.path.join(DATA_PATH, \"train.csv\"))\ntrain_series = pd.read_csv(os.path.join(DATA_PATH, \"train_series.csv\"))\n\ntest = pd.read_csv(os.path.join(DATA_PATH, \"test.csv\"))\ntest_series = pd.read_csv(os.path.join(DATA_PATH, \"test_series.csv\"))\n\nprint(\"Train shape:\", train.shape)\nprint(\"Train series shape:\", train_series.shape)\nprint(\"Test shape:\", test.shape)\nprint(\"Test series shape:\", test_series.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T06:58:30.635433Z","iopub.execute_input":"2026-09-20T06:58:30.636253Z","iopub.status.idle":"2026-09-20T06:58:30.940439Z","shell.execute_reply.started":"2026-09-20T06:58:30.63622Z","shell.execute_reply":"2026-09-20T06:58:30.939737Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"display(train.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T06:58:59.918812Z","iopub.execute_input":"2026-09-20T06:58:59.919568Z","iopub.status.idle":"2026-09-20T06:58:59.95502Z","shell.execute_reply.started":"2026-09-20T06:58:59.919535Z","shell.execute_reply":"2026-09-20T06:58:59.954252Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"display(train_series.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T06:59:22.356636Z","iopub.execute_input":"2026-09-20T06:59:22.357242Z","iopub.status.idle":"2026-09-20T06:59:22.369508Z","shell.execute_reply.started":"2026-09-20T06:59:22.357194Z","shell.execute_reply":"2026-09-20T06:59:22.368661Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Train columns:\")\nprint(train.columns.tolist())\n\nprint(\"\\nTrain series columns:\")\nprint(train_series.columns.tolist())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T06:59:41.749646Z","iopub.execute_input":"2026-09-20T06:59:41.749944Z","iopub.status.idle":"2026-09-20T06:59:41.755451Z","shell.execute_reply.started":"2026-09-20T06:59:41.749918Z","shell.execute_reply":"2026-09-20T06:59:41.754358Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_columns = [\n    \"ACL\",\n    \"MCL\",\n    \"Medial Meniscus\",\n    \"Lateral Meniscus\",\n    \"Medial OA\",\n    \"Lateral OA\",\n    \"PF OA\",\n    \"Effusion\",\n    \"Synovitis\",\n    \"Baker's\",\n    \"Contusion\",\n    \"Fracture\"\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T06:59:58.13787Z","iopub.execute_input":"2026-09-20T06:59:58.138184Z","iopub.status.idle":"2026-09-20T06:59:58.143438Z","shell.execute_reply.started":"2026-09-20T06:59:58.138158Z","shell.execute_reply":"2026-09-20T06:59:58.142505Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train[label_columns].head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:00:13.46277Z","iopub.execute_input":"2026-09-20T07:00:13.463624Z","iopub.status.idle":"2026-09-20T07:00:13.476657Z","shell.execute_reply.started":"2026-09-20T07:00:13.46359Z","shell.execute_reply":"2026-09-20T07:00:13.475831Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_counts = train[label_columns].sum()\n\nprint(label_counts)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:00:29.810202Z","iopub.execute_input":"2026-09-20T07:00:29.810562Z","iopub.status.idle":"2026-09-20T07:00:29.818644Z","shell.execute_reply.started":"2026-09-20T07:00:29.810534Z","shell.execute_reply":"2026-09-20T07:00:29.817855Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_percentages = train[label_columns].mean() * 100\n\nprint(label_percentages.round(2))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:00:48.871732Z","iopub.execute_input":"2026-09-20T07:00:48.87205Z","iopub.status.idle":"2026-09-20T07:00:48.880548Z","shell.execute_reply.started":"2026-09-20T07:00:48.87202Z","shell.execute_reply":"2026-09-20T07:00:48.87971Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train_series[\"Anatomical_Plane\"].value_counts())\nprint(train_series[\"Fluid_Sensitive\"].value_counts())\nprint(train_series[\"Fat_Suppression\"].value_counts())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:01:25.142882Z","iopub.execute_input":"2026-09-20T07:01:25.143665Z","iopub.status.idle":"2026-09-20T07:01:25.155893Z","shell.execute_reply.started":"2026-09-20T07:01:25.143628Z","shell.execute_reply":"2026-09-20T07:01:25.155002Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"display(\n    train_series[\n        [\n            \"Fluid_Sensitive\",\n            \"Fat_Suppression\",\n            \"Anatomical_Plane\"\n        ]\n    ].drop_duplicates()\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:01:52.099532Z","iopub.execute_input":"2026-09-20T07:01:52.100274Z","iopub.status.idle":"2026-09-20T07:01:52.119697Z","shell.execute_reply.started":"2026-09-20T07:01:52.100238Z","shell.execute_reply":"2026-09-20T07:01:52.118672Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"study_id = train_series[\"StudyInstanceUID\"].iloc[0]\n\nprint(\"Study ID:\")\nprint(study_id)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:02:23.526638Z","iopub.execute_input":"2026-09-20T07:02:23.526939Z","iopub.status.idle":"2026-09-20T07:02:23.532547Z","shell.execute_reply.started":"2026-09-20T07:02:23.526915Z","shell.execute_reply":"2026-09-20T07:02:23.531525Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"study_series = train_series[\n    train_series[\"StudyInstanceUID\"] == study_id\n]\n\ndisplay(study_series)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:02:38.884372Z","iopub.execute_input":"2026-09-20T07:02:38.884993Z","iopub.status.idle":"2026-09-20T07:02:38.897454Z","shell.execute_reply.started":"2026-09-20T07:02:38.884959Z","shell.execute_reply":"2026-09-20T07:02:38.896731Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"series_id = study_series[\"SeriesInstanceUID\"].iloc[0]\n\nprint(\"Series ID:\")\nprint(series_id)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:02:59.879909Z","iopub.execute_input":"2026-09-20T07:02:59.880704Z","iopub.status.idle":"2026-09-20T07:02:59.886057Z","shell.execute_reply.started":"2026-09-20T07:02:59.880668Z","shell.execute_reply":"2026-09-20T07:02:59.885132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"series_path = os.path.join(\n    DATA_PATH,\n    \"train_series\",\n    study_id,\n    series_id\n)\n\nprint(series_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:03:12.573953Z","iopub.execute_input":"2026-09-20T07:03:12.574251Z","iopub.status.idle":"2026-09-20T07:03:12.5794Z","shell.execute_reply.started":"2026-09-20T07:03:12.574226Z","shell.execute_reply":"2026-09-20T07:03:12.578364Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dcm_files = [\n    f for f in os.listdir(series_path)\n    if f.lower().endswith(\".dcm\")\n]\n\nprint(\"Number of DICOM slices:\", len(dcm_files))\nprint(\"First few files:\", dcm_files[:5])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:03:31.292488Z","iopub.execute_input":"2026-09-20T07:03:31.292761Z","iopub.status.idle":"2026-09-20T07:03:31.302211Z","shell.execute_reply.started":"2026-09-20T07:03:31.292737Z","shell.execute_reply":"2026-09-20T07:03:31.301333Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dcm_path = os.path.join(\n    series_path,\n    dcm_files[0]\n)\n\nds = pydicom.dcmread(dcm_path)\n\nprint(ds)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:03:54.788471Z","iopub.execute_input":"2026-09-20T07:03:54.789348Z","iopub.status.idle":"2026-09-20T07:03:54.812174Z","shell.execute_reply.started":"2026-09-20T07:03:54.789268Z","shell.execute_reply":"2026-09-20T07:03:54.811427Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image = ds.pixel_array.astype(np.float32)\n\nprint(\"Image shape:\", image.shape)\nprint(\"Minimum:\", image.min())\nprint(\"Maximum:\", image.max())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:04:14.502574Z","iopub.execute_input":"2026-09-20T07:04:14.502866Z","iopub.status.idle":"2026-09-20T07:04:14.510516Z","shell.execute_reply.started":"2026-09-20T07:04:14.502843Z","shell.execute_reply":"2026-09-20T07:04:14.509713Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(6, 6))\n\nplt.imshow(image, cmap=\"gray\")\n\nplt.title(\"Raw MRI Slice\")\nplt.axis(\"off\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:04:28.320012Z","iopub.execute_input":"2026-09-20T07:04:28.32039Z","iopub.status.idle":"2026-09-20T07:04:28.589028Z","shell.execute_reply.started":"2026-09-20T07:04:28.32036Z","shell.execute_reply":"2026-09-20T07:04:28.588349Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tags = [\n    \"PhotometricInterpretation\",\n    \"BitsAllocated\",\n    \"BitsStored\",\n    \"PixelRepresentation\",\n    \"RescaleSlope\",\n    \"RescaleIntercept\",\n    \"ImageOrientationPatient\",\n    \"ImagePositionPatient\",\n    \"SliceThickness\",\n    \"PixelSpacing\"\n]\n\nfor tag in tags:\n    if hasattr(ds, tag):\n        print(tag, \":\", getattr(ds, tag))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:04:45.519544Z","iopub.execute_input":"2026-09-20T07:04:45.520223Z","iopub.status.idle":"2026-09-20T07:04:45.526365Z","shell.execute_reply.started":"2026-09-20T07:04:45.52019Z","shell.execute_reply":"2026-09-20T07:04:45.525432Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Number of DICOM slices:\", len(dcm_files))\nprint(\"Image shape:\", image.shape)\nprint(\"Minimum:\", image.min())\nprint(\"Maximum:\", image.max())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:05:08.492076Z","iopub.execute_input":"2026-09-20T07:05:08.492921Z","iopub.status.idle":"2026-09-20T07:05:08.499973Z","shell.execute_reply.started":"2026-09-20T07:05:08.492885Z","shell.execute_reply":"2026-09-20T07:05:08.499152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"slices = []\n\nfor file_name in dcm_files:\n    file_path = os.path.join(series_path, file_name)\n    \n    ds = pydicom.dcmread(file_path)\n    \n    slices.append(ds)\n\nprint(\"Number of slices loaded:\", len(slices))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:05:53.598468Z","iopub.execute_input":"2026-09-20T07:05:53.599366Z","iopub.status.idle":"2026-09-20T07:05:54.111634Z","shell.execute_reply.started":"2026-09-20T07:05:53.599332Z","shell.execute_reply":"2026-09-20T07:05:54.110814Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(len(slices)):\n    ds = slices[i]\n    \n    if hasattr(ds, \"ImagePositionPatient\"):\n        print(i, ds.ImagePositionPatient)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:06:10.044066Z","iopub.execute_input":"2026-09-20T07:06:10.044484Z","iopub.status.idle":"2026-09-20T07:06:10.051075Z","shell.execute_reply.started":"2026-09-20T07:06:10.04445Z","shell.execute_reply":"2026-09-20T07:06:10.05034Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"first_ds = slices[0]\n\nprint(\"Image Orientation:\")\nprint(first_ds.ImageOrientationPatient)\n\nprint(\"\\nImage Position:\")\nprint(first_ds.ImagePositionPatient)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:06:49.229251Z","iopub.execute_input":"2026-09-20T07:06:49.229582Z","iopub.status.idle":"2026-09-20T07:06:49.235417Z","shell.execute_reply.started":"2026-09-20T07:06:49.229556Z","shell.execute_reply":"2026-09-20T07:06:49.234495Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"orientation = np.array(\n    first_ds.ImageOrientationPatient,\n    dtype=np.float32\n)\n\nrow_direction = orientation[:3]\ncolumn_direction = orientation[3:]\n\nslice_normal = np.cross(\n    row_direction,\n    column_direction\n)\n\nprint(\"Row direction:\", row_direction)\nprint(\"Column direction:\", column_direction)\nprint(\"Slice normal:\", slice_normal)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:07:15.752311Z","iopub.execute_input":"2026-09-20T07:07:15.752608Z","iopub.status.idle":"2026-09-20T07:07:15.760249Z","shell.execute_reply.started":"2026-09-20T07:07:15.752582Z","shell.execute_reply":"2026-09-20T07:07:15.759348Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"slice_information = []\n\nfor ds in slices:\n    \n    position = np.array(\n        ds.ImagePositionPatient,\n        dtype=np.float32\n    )\n    \n    location = np.dot(\n        position,\n        slice_normal\n    )\n    \n    slice_information.append(\n        (location, ds)\n    )\n\nprint(\"First few slice locations:\")\n\nfor item in slice_information[:5]:\n    print(item[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:07:30.7567Z","iopub.execute_input":"2026-09-20T07:07:30.757603Z","iopub.status.idle":"2026-09-20T07:07:30.764751Z","shell.execute_reply.started":"2026-09-20T07:07:30.757564Z","shell.execute_reply":"2026-09-20T07:07:30.763895Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"slice_information.sort(\n    key=lambda x: x[0]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:07:50.712972Z","iopub.execute_input":"2026-09-20T07:07:50.713675Z","iopub.status.idle":"2026-09-20T07:07:50.717993Z","shell.execute_reply.started":"2026-09-20T07:07:50.713642Z","shell.execute_reply":"2026-09-20T07:07:50.717255Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ordered_slices = []\n\nfor location, ds in slice_information:\n    ordered_slices.append(ds)\n\nprint(\"Ordered slices:\", len(ordered_slices))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:08:08.526161Z","iopub.execute_input":"2026-09-20T07:08:08.52685Z","iopub.status.idle":"2026-09-20T07:08:08.531721Z","shell.execute_reply.started":"2026-09-20T07:08:08.526817Z","shell.execute_reply":"2026-09-20T07:08:08.530759Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"volume = []\n\nfor ds in ordered_slices:\n    \n    image = ds.pixel_array.astype(np.float32)\n    \n    volume.append(image)\n\nvolume = np.stack(volume)\n\nprint(\"Volume shape:\", volume.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:08:25.608941Z","iopub.execute_input":"2026-09-20T07:08:25.609628Z","iopub.status.idle":"2026-09-20T07:08:25.671748Z","shell.execute_reply.started":"2026-09-20T07:08:25.609592Z","shell.execute_reply":"2026-09-20T07:08:25.670867Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 5, figsize=(20, 4))\n\nindices = [0, 5, 10, 15, 21]\n\nfor i in range(5):\n    \n    index = indices[i]\n    \n    axes[i].imshow(\n        volume[index],\n        cmap=\"gray\"\n    )\n    \n    axes[i].set_title(\n        f\"Slice {index}\"\n    )\n    \n    axes[i].axis(\"off\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:08:43.799923Z","iopub.execute_input":"2026-09-20T07:08:43.801012Z","iopub.status.idle":"2026-09-20T07:08:44.426Z","shell.execute_reply.started":"2026-09-20T07:08:43.80098Z","shell.execute_reply":"2026-09-20T07:08:44.425227Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_series(study_id, series_id):\n    \n    # Path to the series folder\n    series_path = os.path.join(\n        DATA_PATH,\n        \"train_series\",\n        study_id,\n        series_id\n    )\n    \n    # Get all DICOM files\n    dcm_files = []\n    \n    for file_name in os.listdir(series_path):\n        if file_name.lower().endswith(\".dcm\"):\n            dcm_files.append(file_name)\n    \n    # Read all DICOM files\n    slices = []\n    \n    for file_name in dcm_files:\n        \n        file_path = os.path.join(\n            series_path,\n            file_name\n        )\n        \n        ds = pydicom.dcmread(file_path)\n        slices.append(ds)\n    \n    # Calculate slice direction\n    first_ds = slices[0]\n    \n    orientation = np.array(\n        first_ds.ImageOrientationPatient,\n        dtype=np.float32\n    )\n    \n    row_direction = orientation[:3]\n    column_direction = orientation[3:]\n    \n    slice_normal = np.cross(\n        row_direction,\n        column_direction\n    )\n    \n    # Calculate position of each slice\n    slice_information = []\n    \n    for ds in slices:\n        \n        position = np.array(\n            ds.ImagePositionPatient,\n            dtype=np.float32\n        )\n        \n        location = np.dot(\n            position,\n            slice_normal\n        )\n        \n        slice_information.append(\n            (location, ds)\n        )\n    \n    # Sort slices\n    slice_information.sort(\n        key=lambda x: x[0]\n    )\n    \n    # Create ordered volume\n    volume = []\n    \n    for location, ds in slice_information:\n        \n        image = ds.pixel_array.astype(np.float32)\n        \n        volume.append(image)\n    \n    volume = np.stack(volume)\n    \n    return volume","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:10:07.160149Z","iopub.execute_input":"2026-09-20T07:10:07.160923Z","iopub.status.idle":"2026-09-20T07:10:07.17117Z","shell.execute_reply.started":"2026-09-20T07:10:07.160882Z","shell.execute_reply":"2026-09-20T07:10:07.170229Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"volume = load_series(\n    study_id,\n    series_id\n)\n\nprint(\"Volume shape:\", volume.shape)\nprint(\"Minimum:\", volume.min())\nprint(\"Maximum:\", volume.max())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:10:38.227243Z","iopub.execute_input":"2026-09-20T07:10:38.228157Z","iopub.status.idle":"2026-09-20T07:10:38.368701Z","shell.execute_reply.started":"2026-09-20T07:10:38.228116Z","shell.execute_reply":"2026-09-20T07:10:38.367854Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def normalize_image(image):\n    \n    low = np.percentile(image, 1)\n    high = np.percentile(image, 99)\n    \n    image = np.clip(\n        image,\n        low,\n        high\n    )\n    \n    image = (\n        image - low\n    ) / (\n        high - low + 1e-8\n    )\n    \n    return image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:11:00.390659Z","iopub.execute_input":"2026-09-20T07:11:00.391404Z","iopub.status.idle":"2026-09-20T07:11:00.397077Z","shell.execute_reply.started":"2026-09-20T07:11:00.391348Z","shell.execute_reply":"2026-09-20T07:11:00.395997Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"slice_image = volume[10]\n\nnormalized = normalize_image(\n    slice_image\n)\n\nprint(\"Before normalization:\")\nprint(\"Min:\", slice_image.min())\nprint(\"Max:\", slice_image.max())\n\nprint(\"\\nAfter normalization:\")\nprint(\"Min:\", normalized.min())\nprint(\"Max:\", normalized.max())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:11:16.419906Z","iopub.execute_input":"2026-09-20T07:11:16.420328Z","iopub.status.idle":"2026-09-20T07:11:16.434473Z","shell.execute_reply.started":"2026-09-20T07:11:16.420245Z","shell.execute_reply":"2026-09-20T07:11:16.43351Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(12, 5))\n\nplt.subplot(1, 2, 1)\n\nplt.imshow(\n    slice_image,\n    cmap=\"gray\"\n)\n\nplt.title(\"Original\")\nplt.axis(\"off\")\n\n\nplt.subplot(1, 2, 2)\n\nplt.imshow(\n    normalized,\n    cmap=\"gray\"\n)\n\nplt.title(\"Normalized\")\nplt.axis(\"off\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:11:38.064946Z","iopub.execute_input":"2026-09-20T07:11:38.065839Z","iopub.status.idle":"2026-09-20T07:11:38.360482Z","shell.execute_reply.started":"2026-09-20T07:11:38.065802Z","shell.execute_reply":"2026-09-20T07:11:38.359622Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def resize_image(image, size=224):\n    \n    image_pil = Image.fromarray(\n        (image * 255).astype(np.uint8)\n    )\n    \n    image_pil = image_pil.resize(\n        (size, size),\n        Image.Resampling.BILINEAR\n    )\n    \n    image_resized = np.array(\n        image_pil\n    ).astype(np.float32) / 255.0\n    \n    return image_resized","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:12:12.798328Z","iopub.execute_input":"2026-09-20T07:12:12.799227Z","iopub.status.idle":"2026-09-20T07:12:12.80419Z","shell.execute_reply.started":"2026-09-20T07:12:12.799191Z","shell.execute_reply":"2026-09-20T07:12:12.803297Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"resized = resize_image(\n    normalized\n)\n\nprint(\"Original shape:\", normalized.shape)\nprint(\"Resized shape:\", resized.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:12:27.530489Z","iopub.execute_input":"2026-09-20T07:12:27.530899Z","iopub.status.idle":"2026-09-20T07:12:27.538513Z","shell.execute_reply.started":"2026-09-20T07:12:27.530868Z","shell.execute_reply":"2026-09-20T07:12:27.537571Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_3ch = np.stack(\n    [resized, resized, resized],\n    axis=-1\n)\n\nprint(\"Shape:\", image_3ch.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:12:44.22357Z","iopub.execute_input":"2026-09-20T07:12:44.223862Z","iopub.status.idle":"2026-09-20T07:12:44.230247Z","shell.execute_reply.started":"2026-09-20T07:12:44.223837Z","shell.execute_reply":"2026-09-20T07:12:44.229232Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(6, 6))\n\nplt.imshow(image_3ch)\n\nplt.title(\"Preprocessed MRI Slice\")\n\nplt.axis(\"off\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-20T07:13:01.709956Z","iopub.execute_input":"2026-09-20T07:13:01.710743Z","iopub.status.idle":"2026-09-20T07:13:01.851108Z","shell.execute_reply.started":"2026-09-20T07:13:01.710708Z","shell.execute_reply":"2026-09-20T07:13:01.850366Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}