{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# RSNA 2022 - DICOM Data\n\nExtracts the DICOM data from the training dataset DICOM files and stores it into 'dicom_data.csv'.\n\nBased on https://www.kaggle.com/code/craigmthomas/rsna-2022-eda/notebook","metadata":{}},{"cell_type":"code","source":"!pip install -qU python-gdcm pydicom pylibjpeg","metadata":{"execution":{"iopub.status.busy":"2023-01-02T02:42:42.345822Z","iopub.execute_input":"2023-01-02T02:42:42.346184Z","iopub.status.idle":"2023-01-02T02:42:42.352561Z","shell.execute_reply.started":"2023-01-02T02:42:42.346155Z","shell.execute_reply":"2023-01-02T02:42:42.351313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom pydicom import dcmread","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-02T02:48:54.389437Z","iopub.execute_input":"2023-01-02T02:48:54.389829Z","iopub.status.idle":"2023-01-02T02:48:54.57378Z","shell.execute_reply.started":"2023-01-02T02:48:54.389799Z","shell.execute_reply":"2023-01-02T02:48:54.572365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_CSV = '/kaggle/input/rsna-breast-cancer-detection/train.csv'\ntrain = pd.read_csv(TRAIN_CSV)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-02T02:43:13.561644Z","iopub.execute_input":"2023-01-02T02:43:13.562056Z","iopub.status.idle":"2023-01-02T02:43:13.644119Z","shell.execute_reply.started":"2023-01-02T02:43:13.562023Z","shell.execute_reply":"2023-01-02T02:43:13.643024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATASET_PATH = \"../input/rsna-breast-cancer-detection\"\nDICOM_PATH_SPEC = \"{}/{}/{}/{}.dcm\"\nMONO = \"MONOCHROME1\"\nPHOTO_INT = \"PhotometricInterpretation\"\nDATASET_TYPE = \"train_images\"\nDO_NORMALIZE = True\nNORM_TYPE = \"histogram\"\nRESCALE_WIDTH = 256\nRESCALE_HEIGHT = 256\n\ndef get_dicom_meta_only(patient_id, img_id):\n    return dcmread(DICOM_PATH_SPEC.format(DATASET_PATH, DATASET_TYPE, patient_id, img_id), stop_before_pixels=True)\n\ndicom_data = dict()\nkeywords = set()\ndicom_df = train[[\"patient_id\", \"image_id\"]].copy()\n\nfor index, row in dicom_df.iterrows():\n    patient_id = row[\"patient_id\"]\n    image_id = row[\"image_id\"]\n    dicom = get_dicom_meta_only(patient_id, image_id)\n    if patient_id not in dicom_data:\n        dicom_data[patient_id] = dict()\n    if image_id not in dicom_data[patient_id]:\n        dicom_data[patient_id][image_id] = dict()\n    for feature in dicom.iterall():\n        dicom_data[patient_id][image_id][feature.keyword] = feature.value\n        keywords.add(feature.keyword)\n        \nfor keyword in keywords:\n    dicom_df[keyword] = dicom_df[[\"patient_id\", \"image_id\"]].apply(lambda x: np.nan if keyword not in dicom_data[x.patient_id][x.image_id] else dicom_data[x.patient_id][x.image_id][keyword], axis=1)\n\nprint(\": Keywords extracted from dicom files:\")\nfor keyword in keywords:\n    print(\"--> {}\".format(keyword))","metadata":{"execution":{"iopub.status.busy":"2023-01-02T02:48:59.762693Z","iopub.execute_input":"2023-01-02T02:48:59.763263Z","iopub.status.idle":"2023-01-02T03:08:31.153526Z","shell.execute_reply.started":"2023-01-02T02:48:59.763202Z","shell.execute_reply":"2023-01-02T03:08:31.152117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom_df.to_csv('dicom_data.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-02T03:10:11.034554Z","iopub.execute_input":"2023-01-02T03:10:11.035002Z","iopub.status.idle":"2023-01-02T03:10:12.255899Z","shell.execute_reply.started":"2023-01-02T03:10:11.034964Z","shell.execute_reply":"2023-01-02T03:10:12.254917Z"},"trusted":true},"execution_count":null,"outputs":[]}]}