{"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":"import os\nimport pandas as pd\nfrom pathlib import Path\n\nDATA = Path('/kaggle/input/competitions/rsna-knee-abnormality-detection')\n\nprint(os.listdir(DATA))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-08-13T09:21:38.339872Z","iopub.execute_input":"2026-08-13T09:21:38.340228Z","iopub.status.idle":"2026-08-13T09:21:38.7581Z","shell.execute_reply.started":"2026-08-13T09:21:38.340201Z","shell.execute_reply":"2026-08-13T09:21:38.756937Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ntrain_df = pd.read_csv(DATA / 'train.csv')\n\nprint(train_df.head())\nprint(train_df.columns)\nprint(train_df.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-13T09:21:38.759872Z","iopub.execute_input":"2026-08-13T09:21:38.760357Z","iopub.status.idle":"2026-08-13T09:21:38.973095Z","shell.execute_reply.started":"2026-08-13T09:21:38.760328Z","shell.execute_reply":"2026-08-13T09:21:38.971807Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install -q pydicom","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-13T09:21:38.974366Z","iopub.execute_input":"2026-08-13T09:21:38.975081Z","iopub.status.idle":"2026-08-13T09:21:45.810314Z","shell.execute_reply.started":"2026-08-13T09:21:38.975046Z","shell.execute_reply":"2026-08-13T09:21:45.808863Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom\nimport matplotlib.pyplot as plt\n\n# get ONE DICOM file quickly\nsample_file = next(DATA.rglob('*.dcm'))\n\nprint('Sample file:')\nprint(sample_file)\n\n# read the image\ndcm = pydicom.dcmread(sample_file)\nimage = dcm.pixel_array\n\nprint('Image shape:', image.shape)\n\n# display\nplt.figure(figsize=(6,6))\nplt.imshow(image, cmap='gray')\nplt.title(sample_file.name)\nplt.axis('off')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-13T09:21:45.813234Z","iopub.execute_input":"2026-08-13T09:21:45.813537Z","iopub.status.idle":"2026-08-13T09:21:47.313406Z","shell.execute_reply.started":"2026-08-13T09:21:45.813503Z","shell.execute_reply":"2026-08-13T09:21:47.312124Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"series_df = pd.read_csv(DATA / 'train_series.csv')\nseries_df.head(100)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-13T09:21:47.314899Z","iopub.execute_input":"2026-08-13T09:21:47.315262Z","iopub.status.idle":"2026-08-13T09:21:47.439554Z","shell.execute_reply.started":"2026-08-13T09:21:47.315233Z","shell.execute_reply":"2026-08-13T09:21:47.43763Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(series_df.columns)\nprint(series_df.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-13T09:21:47.44107Z","iopub.execute_input":"2026-08-13T09:21:47.441953Z","iopub.status.idle":"2026-08-13T09:21:47.450593Z","shell.execute_reply.started":"2026-08-13T09:21:47.441902Z","shell.execute_reply":"2026-08-13T09:21:47.448041Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"merged_df = train_df.merge(series_df, on='StudyInstanceUID')\n\nprint('Merged shape:', merged_df.shape)\nmerged_df[['StudyInstanceUID', 'SeriesInstanceUID', 'Anatomical_Plane']].head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-13T09:21:47.45219Z","iopub.execute_input":"2026-08-13T09:21:47.452914Z","iopub.status.idle":"2026-08-13T09:21:47.522276Z","shell.execute_reply.started":"2026-08-13T09:21:47.452878Z","shell.execute_reply":"2026-08-13T09:21:47.521197Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"study_id = merged_df.iloc[0]['StudyInstanceUID']\nprint('Study ID:')\nprint(study_id)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-13T09:21:47.523741Z","iopub.execute_input":"2026-08-13T09:21:47.524262Z","iopub.status.idle":"2026-08-13T09:21:47.530965Z","shell.execute_reply.started":"2026-08-13T09:21:47.524217Z","shell.execute_reply":"2026-08-13T09:21:47.529395Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from pathlib import Path\n\nstudy_path = DATA / 'train_series' / study_id\n\nprint('Study path exists:', study_path.exists())\nprint(study_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-13T09:22:26.541992Z","iopub.execute_input":"2026-08-13T09:22:26.542784Z","iopub.status.idle":"2026-08-13T09:22:26.549296Z","shell.execute_reply.started":"2026-08-13T09:22:26.542694Z","shell.execute_reply":"2026-08-13T09:22:26.548187Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"series_folders = list(study_path.iterdir())\n\nprint('Number of MRI series:', len(series_folders))\n\nfor folder in series_folders[:5]:\n    print(folder.name)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-13T09:23:25.240772Z","iopub.execute_input":"2026-08-13T09:23:25.241125Z","iopub.status.idle":"2026-08-13T09:23:25.253526Z","shell.execute_reply.started":"2026-08-13T09:23:25.24109Z","shell.execute_reply":"2026-08-13T09:23:25.251649Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom\nimport matplotlib.pyplot as plt\n\n# choose the first MRI series\nseries_path = series_folders[0]\n\n# get one DICOM image from that series\ndcm_file = next(series_path.glob('*.dcm'))\n\nprint('DICOM file:', dcm_file.name)\n\n# read the image\ndcm = pydicom.dcmread(dcm_file)\nimage = dcm.pixel_array\n\nprint('Image shape:', image.shape)\n\n# display the MRI slice\nplt.figure(figsize=(6,6))\nplt.imshow(image, cmap='gray')\nplt.title(f'Knee MRI Slice\\\\n{dcm_file.name}')\nplt.axis('off')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-13T09:24:56.582133Z","iopub.execute_input":"2026-08-13T09:24:56.583238Z","iopub.status.idle":"2026-08-13T09:24:56.856996Z","shell.execute_reply.started":"2026-08-13T09:24:56.583201Z","shell.execute_reply":"2026-08-13T09:24:56.856028Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# count how many slices are in this series\ncount = sum(1 for _ in series_path.glob('*.dcm'))\nprint('This MRI series contains', count, 'slices')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-08-13T09:46:23.031037Z","iopub.execute_input":"2026-08-13T09:46:23.031435Z","iopub.status.idle":"2026-08-13T09:46:23.038285Z","shell.execute_reply.started":"2026-08-13T09:46:23.031393Z","shell.execute_reply":"2026-08-13T09:46:23.037212Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}