{"cells":[{"metadata":{},"cell_type":"markdown","source":"# About this kernel\n\nThis kernel generates the metadata of the files (i.e., the information from the DICOM files), so you can perform analysis by directly using this kernel as an input.\n\nNote: \"SOP Instance UID\" matches the file names."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport pydicom\nfrom tqdm import tqdm\n\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"BASE_PATH = '/kaggle/input/rsna-intracranial-hemorrhage-detection/'\nTRAIN_DIR = 'stage_1_train_images/'\nTEST_DIR = 'stage_1_test_images/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def generate_df(base, files):\n    train_di = {}\n\n    for filename in tqdm(files):\n        path = base + filename\n        dcm = pydicom.dcmread(path)\n        all_keywords = dcm.dir()\n        ignored = ['Rows', 'Columns', 'PixelData']\n\n        for name in all_keywords:\n            if name in ignored:\n                continue\n\n            if name not in train_di:\n                train_di[name] = []\n\n            train_di[name].append(dcm[name].value)\n\n    df = pd.DataFrame(train_di)\n    \n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_files = os.listdir(BASE_PATH + TRAIN_DIR)\ntrain_df = generate_df(BASE_PATH + TRAIN_DIR, train_files)\n\ntest_files = os.listdir(BASE_PATH + TEST_DIR)\ntest_df = generate_df(BASE_PATH + TEST_DIR, test_files)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.to_csv('train_metadata.csv')\ntest_df.to_csv('test_metadata.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}