{"cells":[{"metadata":{},"cell_type":"markdown","source":"Let's create a CSV with all the metadata of the DICOM images so we can do a better analysis of that data.\n\n# Import"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import glob\n\nimport pandas as pd\n\nimport pydicom\n\nimport tqdm","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Read loop"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Paths\ninput_path = \"../input/rsna-intracranial-hemorrhage-detection\"\ntrain_imgs = glob.glob(f\"{input_path}/stage_1_train_images/*\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"def save_value(img_data, name, value):\n    if type(value) == pydicom.multival.MultiValue:\n        for i, j in enumerate(value):\n            save_value(img_data, f\"{name}_{i}\", j)\n    else:\n        if type(value) == pydicom.uid.UID:\n            value = str(value)\n        elif type(value) == pydicom.valuerep.DSfloat:\n            value = float(value)\n        img_data[name] = value\n\ndef get_data_dict(img):\n    img_data = {}\n    for i in img.iterall():\n        if i.name == \"Pixel Data\":\n            continue\n        name = i.name.replace(\" \", \"_\").replace(\"(\", \"\").replace(\")\", \"\").lower()\n        save_value(img_data, name, i.value)\n    return img_data\n\ndef get_list_data(imgs):\n    list_data = []\n    for i in tqdm.tqdm(imgs):\n        img = pydicom.read_file(i)\n        img_data = get_data_dict(img)\n        list_data.append(img_data)\n    return list_data\n\ndef get_df_data(imgs):\n    list_data = get_list_data(imgs)\n    return pd.DataFrame(list_data)\n\ndf_imgs = get_df_data(train_imgs)\ndf_imgs.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Save output"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_imgs.to_csv(\"df_dicom_metadata.csv\", index=False)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}