{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":36363,"databundleVersionId":4050810}],"dockerImageVersionId":31286,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T12:00:05.204312Z","iopub.execute_input":"2026-03-03T12:00:05.204722Z","iopub.status.idle":"2026-03-03T12:00:05.213015Z","shell.execute_reply.started":"2026-03-03T12:00:05.204693Z","shell.execute_reply":"2026-03-03T12:00:05.211856Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DATA_PATH = \"/kaggle/input/competitions/rsna-2022-cervical-spine-fracture-detection\"\nprint(DATA_PATH)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T12:00:05.214985Z","iopub.execute_input":"2026-03-03T12:00:05.215408Z","iopub.status.idle":"2026-03-03T12:00:05.231061Z","shell.execute_reply.started":"2026-03-03T12:00:05.215323Z","shell.execute_reply":"2026-03-03T12:00:05.230213Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir(DATA_PATH)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T12:00:05.232552Z","iopub.execute_input":"2026-03-03T12:00:05.23289Z","iopub.status.idle":"2026-03-03T12:00:05.252116Z","shell.execute_reply.started":"2026-03-03T12:00:05.232856Z","shell.execute_reply":"2026-03-03T12:00:05.251402Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ntrain_df = pd.read_csv(f\"{DATA_PATH}/train.csv\")\ntrain_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T12:00:05.25376Z","iopub.execute_input":"2026-03-03T12:00:05.253994Z","iopub.status.idle":"2026-03-03T12:00:05.639665Z","shell.execute_reply.started":"2026-03-03T12:00:05.253972Z","shell.execute_reply":"2026-03-03T12:00:05.63888Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"study_id = train_df.iloc[0][\"StudyInstanceUID\"]\nstudy_id","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T12:00:05.64067Z","iopub.execute_input":"2026-03-03T12:00:05.640975Z","iopub.status.idle":"2026-03-03T12:00:05.646698Z","shell.execute_reply.started":"2026-03-03T12:00:05.640943Z","shell.execute_reply":"2026-03-03T12:00:05.645932Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Cell 0: Install required libraries with specific versions\n!pip uninstall -y pydicom pylibjpeg pylibjpeg-libjpeg pylibjpeg-openjpeg gdcm\n!pip install pydicom==2.4.4\n!pip install pylibjpeg==2.0.0\n!pip install pylibjpeg-libjpeg==2.1.0\n!pip install pylibjpeg-openjpeg==2.0.0\n!pip install gdcm\n\n# Force restart the kernel (this is important!)\nimport IPython\nIPython.Application.instance().kernel.do_shutdown(True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T12:00:45.587074Z","iopub.execute_input":"2026-03-03T12:00:45.588014Z","iopub.status.idle":"2026-03-03T12:01:10.28768Z","shell.execute_reply.started":"2026-03-03T12:00:45.587979Z","shell.execute_reply":"2026-03-03T12:01:10.286722Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install pylibjpeg pylibjpeg-libjpeg pylibjpeg-openjpeg gdcm","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T12:00:28.578725Z","iopub.execute_input":"2026-03-03T12:00:28.579098Z","iopub.status.idle":"2026-03-03T12:00:32.885196Z","shell.execute_reply.started":"2026-03-03T12:00:28.579055Z","shell.execute_reply":"2026-03-03T12:00:32.884042Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom\nimport numpy as np\nimport os\n\n# Install pylibjpeg if not already installed\ntry:\n    import pylibjpeg\nexcept ImportError:\n    !pip install pylibjpeg pylibjpeg-libjpeg pylibjpeg-openjpeg\n\ndicom_dir = f\"{DATA_PATH}/train_images/{study_id}\"\n\n# Get all DICOM files and sort them numerically\nfiles = sorted([f for f in os.listdir(dicom_dir) if f.endswith('.dcm')], \n               key=lambda x: int(x.split(\".\")[0]))\n\nprint(f\"Found {len(files)} DICOM files\")\n\nslices = []\nfor i, f in enumerate(files):\n    try:\n        filepath = os.path.join(dicom_dir, f)\n        ds = pydicom.dcmread(filepath)\n        \n        # Check if pixel_array is accessible\n        if hasattr(ds, 'pixel_array'):\n            slices.append(ds.pixel_array)\n        else:\n            print(f\"Warning: File {f} has no pixel_array\")\n            \n    except Exception as e:\n        print(f\"Error reading {f}: {str(e)}\")\n        continue\n    \n    # Print progress for large volumes\n    if (i + 1) % 50 == 0:\n        print(f\"Loaded {i + 1} slices\")\n\nif len(slices) > 0:\n    volume = np.stack(slices)\n    print(f\"Volume shape: {volume.shape}\")\n    print(f\"Data type: {volume.dtype}\")\n    print(f\"Value range: {volume.min()} to {volume.max()}\")\nelse:\n    print(\"No slices could be loaded\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T12:00:32.886956Z","iopub.execute_input":"2026-03-03T12:00:32.887369Z","iopub.status.idle":"2026-03-03T12:00:38.889402Z","shell.execute_reply.started":"2026-03-03T12:00:32.887299Z","shell.execute_reply":"2026-03-03T12:00:38.888381Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}