{"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-11-28T06:05:35.723507Z","iopub.execute_input":"2024-11-28T06:05:35.724010Z","iopub.status.idle":"2024-11-28T06:05:37.328488Z","shell.execute_reply.started":"2024-11-28T06:05:35.723978Z","shell.execute_reply":"2024-11-28T06:05:37.326876Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"unique_study_ids = set()\n\n# Loop through the DataFrame\nfor study_id in df['study_id']:\n    unique_study_ids.add(study_id)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T06:11:52.115889Z","iopub.execute_input":"2024-11-28T06:11:52.116238Z","iopub.status.idle":"2024-11-28T06:11:52.123652Z","shell.execute_reply.started":"2024-11-28T06:11:52.116215Z","shell.execute_reply":"2024-11-28T06:11:52.122328Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(4003253 in unique_study_ids)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T06:12:30.258275Z","iopub.execute_input":"2024-11-28T06:12:30.258809Z","iopub.status.idle":"2024-11-28T06:12:30.267344Z","shell.execute_reply.started":"2024-11-28T06:12:30.258745Z","shell.execute_reply":"2024-11-28T06:12:30.266064Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"N = 100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T06:47:21.195383Z","iopub.execute_input":"2024-11-28T06:47:21.196918Z","iopub.status.idle":"2024-11-28T06:47:21.202364Z","shell.execute_reply.started":"2024-11-28T06:47:21.196868Z","shell.execute_reply":"2024-11-28T06:47:21.201206Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom\nimport numpy as np\n\ndef dcm_to_array(dcm_path):\n    # Load the DICOM file\n    dcm_data = pydicom.dcmread(dcm_path)\n    \n    # Extract pixel data and convert it to a NumPy array\n    numpy_array = dcm_data.pixel_array\n    \n    return numpy_array","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T06:38:53.780528Z","iopub.execute_input":"2024-11-28T06:38:53.781581Z","iopub.status.idle":"2024-11-28T06:38:54.001987Z","shell.execute_reply.started":"2024-11-28T06:38:53.781538Z","shell.execute_reply":"2024-11-28T06:38:54.000640Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def f(t1,t2,m,n):\n    return int(((m/n)*t1) + ((1-(m/n))*t2))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T06:52:29.715233Z","iopub.execute_input":"2024-11-28T06:52:29.715632Z","iopub.status.idle":"2024-11-28T06:52:29.721011Z","shell.execute_reply.started":"2024-11-28T06:52:29.715581Z","shell.execute_reply":"2024-11-28T06:52:29.719874Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from PIL import Image\ndef create_n_channel(patient,img1,img2):\n    folder_path = f'/kaggle/working/hyper/{patient}/'\n    os.makedirs(folder_path, exist_ok = True)\n    for m in range(N):\n        x = np.vectorize(f)(t1_img, t2_img,m,N)\n        x = x.astype(np.uint8)\n        image = Image.fromarray(x)\n        image.save(folder_path + str(m) + '.png')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T07:08:06.508162Z","iopub.execute_input":"2024-11-28T07:08:06.508526Z","iopub.status.idle":"2024-11-28T07:08:06.514697Z","shell.execute_reply.started":"2024-11-28T07:08:06.508499Z","shell.execute_reply":"2024-11-28T07:08:06.513513Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\ncount = 0\nfor patient in unique_study_ids:\n    curr_patient = df[df['study_id'] == patient]\n    if len(curr_patient)!=3:\n        continue\n    t1_series_id = str(int(curr_patient[curr_patient['series_description'] == 'Sagittal T1']['series_id']))\n    t2_series_id = str(int(curr_patient[curr_patient['series_description'] == 'Sagittal T2/STIR']['series_id']))\n    #print(t1_series_id)\n    #print(t2_series_id)\n    t1_path = f'/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images/{patient}/{t1_series_id}/'\n    num_t1 = len(os.listdir(t1_path))\n    t2_path = f'/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images/{patient}/{t2_series_id}/'\n    num_t2 = len(os.listdir(t2_path))\n    if num_t1!=num_t2:\n        continue\n    t1_img_path = t1_path + f'{num_t1//2}.dcm'\n    t1_img = dcm_to_array(t1_img_path)\n    #print(t1_img.shape, end = '')\n    t2_img_path = t2_path + f'{num_t1//2}.dcm'\n    t2_img = dcm_to_array(t2_img_path)\n    #print(t2_img.shape)\n    if t1_img.shape != t2_img.shape:\n        continue\n    create_n_channel(patient,t1_img,t2_img)\n    count+=1\n    break\nprint(count)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T07:08:17.196040Z","iopub.execute_input":"2024-11-28T07:08:17.196398Z","iopub.status.idle":"2024-11-28T07:08:30.892926Z","shell.execute_reply.started":"2024-11-28T07:08:17.196374Z","shell.execute_reply":"2024-11-28T07:08:30.891517Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import shutil\n\n# Path to the folder to be zipped\nfolder_path = \"/kaggle/working/hyper\"\n\n# Path for the output zip file\noutput_zip_path = \"/kaggle/working/output.zip\"\n\n# Zip the folder\nshutil.make_archive(output_zip_path.replace('.zip', ''), 'zip', folder_path)\n\nprint(f\"Folder zipped successfully at {output_zip_path}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-28T07:12:11.843298Z","iopub.execute_input":"2024-11-28T07:12:11.844658Z","iopub.status.idle":"2024-11-28T07:12:12.485288Z","shell.execute_reply.started":"2024-11-28T07:12:11.844606Z","shell.execute_reply":"2024-11-28T07:12:12.484009Z"}},"outputs":[],"execution_count":null}]}