{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.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":71549,"databundleVersionId":8561470,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\n\n# Define the file path\nfile_path = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_series_descriptions.csv'\n\n# Load the CSV file into a DataFrame\ndf = pd.read_csv(file_path)\n\n# Display the first few rows of the DataFrame\nprint(df.head())","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-29T07:12:30.542287Z","iopub.execute_input":"2024-09-29T07:12:30.543038Z","iopub.status.idle":"2024-09-29T07:12:31.761691Z","shell.execute_reply.started":"2024-09-29T07:12:30.543000Z","shell.execute_reply":"2024-09-29T07:12:31.759946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sag = df[df['series_description'] == 'Sagittal T2/STIR']","metadata":{"execution":{"iopub.status.busy":"2024-09-27T19:35:58.379316Z","iopub.execute_input":"2024-09-27T19:35:58.379755Z","iopub.status.idle":"2024-09-27T19:35:58.390362Z","shell.execute_reply.started":"2024-09-27T19:35:58.379723Z","shell.execute_reply":"2024-09-27T19:35:58.388103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sag = sag[600:900]","metadata":{"execution":{"iopub.status.busy":"2024-09-27T19:36:01.489117Z","iopub.execute_input":"2024-09-27T19:36:01.489534Z","iopub.status.idle":"2024-09-27T19:36:01.506047Z","shell.execute_reply.started":"2024-09-27T19:36:01.489494Z","shell.execute_reply":"2024-09-27T19:36:01.504350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sag['path'] = sag.apply(lambda row: f\"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images/{row['study_id']}/{row['series_id']}\", axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-09-27T19:36:07.585561Z","iopub.execute_input":"2024-09-27T19:36:07.586114Z","iopub.status.idle":"2024-09-27T19:36:07.636141Z","shell.execute_reply.started":"2024-09-27T19:36:07.586061Z","shell.execute_reply":"2024-09-27T19:36:07.634654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pydicom\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom tqdm import tqdm\nimport gc\n\n# Global count to keep track of image filenames\nglobal_count = 9074\n\n# Function to save DICOM images with sliding window labels 'n-1', 'n', 'n+1'\ndef save_dicom_images(sliding_window, global_count, save_dir):\n    labels = ['n-1', 'n', 'n+1']\n    for idx, (instance_number, dcm_data) in enumerate(sliding_window):\n        # File name format: '{global_count}_{label}'\n        save_path = os.path.join(save_dir, f'{global_count}_{labels[idx]}.png')\n        \n        # Save the DICOM slice as an image (using matplotlib)\n        plt.imshow(dcm_data.pixel_array, cmap='gray')\n        plt.axis('off')  # Turn off axis\n        plt.savefig(save_path, bbox_inches='tight', pad_inches=0)\n        plt.close()\n\n\n# Create output directory to save images (all images will be saved here)\noutput_dir = \"all_patient_images\"\nos.makedirs(output_dir, exist_ok=True)\n\n# Iterate over each patient\nfor idx, row in tqdm(sag.iterrows(), total=sag.shape[0], desc=\"Processing Patients\"):\n    gc.collect()\n    dcm_dir = row['path']  # Get the directory path from the 'path' column\n\n    # List all DICOM files in the directory\n    dcm_files = [f for f in os.listdir(dcm_dir) if f.endswith('.dcm')]\n    \n    # If there are DICOM files, count them and process the sliding window\n    if dcm_files:\n        # Load all DICOM files and check their Series Instance UID and Instance Number\n        dicom_data_list = []\n        for file in dcm_files:\n            file_path = os.path.join(dcm_dir, file)\n            dcm_data = pydicom.dcmread(file_path)\n            instance_number = dcm_data.InstanceNumber\n            dicom_data_list.append((instance_number, dcm_data))\n        \n        # Sort by Instance Number\n        dicom_data_list.sort(key=lambda x: x[0])\n\n        # Update the 'slice_count' column with the number of slices for this series\n        sag.at[idx, 'slice_count'] = len(dicom_data_list)\n\n        # Apply sliding window of size 3\n        for i in range(1, len(dicom_data_list) - 1):\n            sliding_window = dicom_data_list[i - 1:i + 2]  # Get slices n-1, n, n+1\n\n            # Save images in the sliding window using labels 'n-1', 'n', 'n+1'\n            save_dicom_images(sliding_window, global_count, output_dir)\n\n            # Increment the global count\n            global_count += 1\n        del dicom_data_list\n    else:\n        # If no DICOM files, set slice_count to 0\n        sag.at[idx, 'slice_count'] = 0\n    del dcm_files\n\n# Print the updated DataFrame\nprint(sag)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-27T19:36:13.004955Z","iopub.execute_input":"2024-09-27T19:36:13.005381Z","iopub.status.idle":"2024-09-27T19:37:36.384441Z","shell.execute_reply.started":"2024-09-27T19:36:13.005349Z","shell.execute_reply":"2024-09-27T19:37:36.383226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n\n# Define the directory to zip and the output zip file path\ndirectory_to_zip = '/kaggle/working/all_patient_images'\noutput_zip_file = '/kaggle/working/all_patient_images.zip'\n\n# Create a zip file from the directory\nshutil.make_archive(output_zip_file[:-4], 'zip', directory_to_zip)\n\nprint(f\"Zipped {directory_to_zip} into {output_zip_file}\")","metadata":{"execution":{"iopub.status.busy":"2024-09-27T19:27:23.692776Z","iopub.execute_input":"2024-09-27T19:27:23.693319Z","iopub.status.idle":"2024-09-27T19:27:26.310819Z","shell.execute_reply.started":"2024-09-27T19:27:23.693278Z","shell.execute_reply":"2024-09-27T19:27:26.309478Z"},"trusted":true},"execution_count":null,"outputs":[]}]}