{"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":"# Problem","metadata":{}},{"cell_type":"markdown","source":"DICOM files contain valuable metadata to classify examples and evaluate your model.\n\nThis notebook explains how to extract these metadata and convert them into a DataFrame.\n\nFeel free to share your approach to this problem if you find your solution easier or more efficient :)","metadata":{}},{"cell_type":"markdown","source":"# Solution","metadata":{}},{"cell_type":"code","source":"import pathlib\nimport itertools","metadata":{"execution":{"iopub.status.busy":"2021-06-05T05:43:45.100363Z","iopub.execute_input":"2021-06-05T05:43:45.100912Z","iopub.status.idle":"2021-06-05T05:43:45.106030Z","shell.execute_reply.started":"2021-06-05T05:43:45.100860Z","shell.execute_reply":"2021-06-05T05:43:45.104642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2021-06-05T05:43:45.108638Z","iopub.execute_input":"2021-06-05T05:43:45.109419Z","iopub.status.idle":"2021-06-05T05:43:45.121132Z","shell.execute_reply.started":"2021-06-05T05:43:45.109370Z","shell.execute_reply":"2021-06-05T05:43:45.120019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root = pathlib.Path('/kaggle/input/siim-covid19-detection')","metadata":{"execution":{"iopub.status.busy":"2021-06-05T05:43:45.122485Z","iopub.execute_input":"2021-06-05T05:43:45.122938Z","iopub.status.idle":"2021-06-05T05:43:45.135003Z","shell.execute_reply.started":"2021-06-05T05:43:45.122892Z","shell.execute_reply":"2021-06-05T05:43:45.133930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata = []\nfor file in root.glob('**/*.dcm'):\n    print('.', end='')\n    # read the file, but ignore pixel values\n    dcm = pydicom.dcmread(file, stop_before_pixels=True)\n    # iterall will magically convert RawElement from DataElement\n    elements = itertools.chain(dcm.iterall(), dcm.file_meta.iterall())\n    for elem in elements:\n        meta = {\n            'VM': elem.VM,\n            'VR': elem.VR,\n            'tag': elem.tag,\n            'name': elem.name,\n            'keyword': elem.keyword,\n            # SQ values are redundant with other elements\n            'data': elem.value if elem.VR != 'SQ' else None,\n            'value': elem.repval,\n            'filename': file.name,\n        }\n        metadata.append(meta)\nmetadata = pd.DataFrame(metadata)\nprint('\\nDONE:', len(metadata))\nmetadata.tail(50)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-05T05:43:45.137031Z","iopub.execute_input":"2021-06-05T05:43:45.137341Z","iopub.status.idle":"2021-06-05T05:44:34.599744Z","shell.execute_reply.started":"2021-06-05T05:43:45.137312Z","shell.execute_reply":"2021-06-05T05:44:34.598796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display the last dcm for comparison\ndcm","metadata":{"execution":{"iopub.status.busy":"2021-06-05T05:44:34.601655Z","iopub.execute_input":"2021-06-05T05:44:34.601945Z","iopub.status.idle":"2021-06-05T05:44:34.608874Z","shell.execute_reply.started":"2021-06-05T05:44:34.601916Z","shell.execute_reply":"2021-06-05T05:44:34.607818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display the metadata schema\nmetadata.info()","metadata":{"execution":{"iopub.status.busy":"2021-06-05T05:44:34.610377Z","iopub.execute_input":"2021-06-05T05:44:34.610685Z","iopub.status.idle":"2021-06-05T05:44:34.869952Z","shell.execute_reply.started":"2021-06-05T05:44:34.610654Z","shell.execute_reply":"2021-06-05T05:44:34.869045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Good luck with the competition :)\n\n**If you found this notebook useful, please consider leaving an upvote!** ","metadata":{}}]}