{"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":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport pydicom\nimport glob\nimport os\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-11T05:41:27.492140Z","iopub.execute_input":"2024-07-11T05:41:27.492593Z","iopub.status.idle":"2024-07-11T05:41:28.227451Z","shell.execute_reply.started":"2024-07-11T05:41:27.492560Z","shell.execute_reply":"2024-07-11T05:41:28.226232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_path = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images'\n\nmeta_columns = ['study_id', 'series_id', 'instance',\n               'transfer_syntax', 'series_desc', \n               'slice_thickness', 'slices_spacing', 'pixel_spacing',\n               'patient_pos', 'image_pos', 'image_orient', 'slice_loc', \n               'samples_per_pixel', 'photo_interpr', 'pixel_interp',\n               'rows', 'columns',\n               'bits_allocated', 'bits_stored', 'high_bit',\n               'window_center', 'window_width']","metadata":{"execution":{"iopub.status.busy":"2024-07-11T05:41:28.229573Z","iopub.execute_input":"2024-07-11T05:41:28.230114Z","iopub.status.idle":"2024-07-11T05:41:28.236204Z","shell.execute_reply.started":"2024-07-11T05:41:28.230082Z","shell.execute_reply":"2024-07-11T05:41:28.235088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_meta(dcm):\n    \n    return (\n        dcm.StudyInstanceUID,\n        dcm.SeriesInstanceUID.split('.')[1],\n        dcm.InstanceNumber,\n\n        dcm.file_meta.TransferSyntaxUID,\n        dcm.SeriesDescription,\n        \n        dcm.SliceThickness,\n        dcm.SpacingBetweenSlices,\n        tuple(dcm.PixelSpacing),\n        \n        dcm.PatientPosition,\n        tuple(dcm.ImagePositionPatient),\n        tuple(dcm.ImageOrientationPatient),\n        dcm.SliceLocation,\n        \n        dcm.SamplesPerPixel,\n        dcm.PhotometricInterpretation, \n        dcm.PixelRepresentation,\n        \n        dcm.Rows,\n        dcm.Columns,\n        \n        dcm.BitsAllocated,\n        dcm.BitsStored,\n        dcm.HighBit,\n        \n        dcm.WindowCenter,\n        dcm.WindowWidth\n        \n    )","metadata":{"execution":{"iopub.status.busy":"2024-07-11T05:41:28.237690Z","iopub.execute_input":"2024-07-11T05:41:28.238066Z","iopub.status.idle":"2024-07-11T05:41:28.250649Z","shell.execute_reply.started":"2024-07-11T05:41:28.238039Z","shell.execute_reply":"2024-07-11T05:41:28.249462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dcms_paths = glob.glob(os.path.join(train_images_path, '*', '*', '*.dcm'))\nlen(dcms_paths)","metadata":{"execution":{"iopub.status.busy":"2024-07-11T05:41:28.252893Z","iopub.execute_input":"2024-07-11T05:41:28.253270Z","iopub.status.idle":"2024-07-11T05:42:32.188033Z","shell.execute_reply.started":"2024-07-11T05:41:28.253232Z","shell.execute_reply":"2024-07-11T05:42:32.186783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metas = []\n\nfor dcm_path in tqdm(dcms_paths):\n    \n    dcm = pydicom.dcmread(dcm_path)\n    metas.append(get_meta(dcm))","metadata":{"execution":{"iopub.status.busy":"2024-07-11T05:42:32.189787Z","iopub.execute_input":"2024-07-11T05:42:32.190239Z","iopub.status.idle":"2024-07-11T06:10:13.666238Z","shell.execute_reply.started":"2024-07-11T05:42:32.190200Z","shell.execute_reply":"2024-07-11T06:10:13.664795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metas = pd.DataFrame(metas, columns=meta_columns)\nmetas.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-11T06:10:13.668806Z","iopub.execute_input":"2024-07-11T06:10:13.669197Z","iopub.status.idle":"2024-07-11T06:10:15.887781Z","shell.execute_reply.started":"2024-07-11T06:10:13.669166Z","shell.execute_reply":"2024-07-11T06:10:15.886460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metas.to_csv('train_meta.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-11T06:15:00.373585Z","iopub.execute_input":"2024-07-11T06:15:00.374832Z","iopub.status.idle":"2024-07-11T06:15:07.055844Z","shell.execute_reply.started":"2024-07-11T06:15:00.374782Z","shell.execute_reply":"2024-07-11T06:15:07.054419Z"},"trusted":true},"execution_count":null,"outputs":[]}]}