{"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":"## Extracting metadata from DICOM files using FAST.AI","metadata":{}},{"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)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-04T20:33:35.843255Z","iopub.execute_input":"2022-04-04T20:33:35.844252Z","iopub.status.idle":"2022-04-04T20:33:35.874266Z","shell.execute_reply.started":"2022-04-04T20:33:35.844049Z","shell.execute_reply":"2022-04-04T20:33:35.873167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pyarrow\n!pip install -U fastai\n!pip install pydicom kornia opencv-python scikit-image","metadata":{"execution":{"iopub.status.busy":"2022-04-04T20:44:34.217504Z","iopub.execute_input":"2022-04-04T20:44:34.217737Z","iopub.status.idle":"2022-04-04T20:45:19.168625Z","shell.execute_reply.started":"2022-04-04T20:44:34.21771Z","shell.execute_reply":"2022-04-04T20:45:19.167132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.basics import *\nfrom fastai.callback.all import *\nfrom fastai.vision.all import *\nfrom fastai.medical.imaging import *\n\nimport pydicom","metadata":{"execution":{"iopub.status.busy":"2022-04-04T20:45:41.457284Z","iopub.execute_input":"2022-04-04T20:45:41.457578Z","iopub.status.idle":"2022-04-04T20:45:45.476882Z","shell.execute_reply.started":"2022-04-04T20:45:41.457544Z","shell.execute_reply":"2022-04-04T20:45:45.475464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"items_train = get_dicom_files(\"../input/unifesp-x-ray-body-part-classifier/train/train\")\nitems_test = get_dicom_files(\"../input/unifesp-x-ray-body-part-classifier/test/test\")","metadata":{"execution":{"iopub.status.busy":"2022-04-04T20:46:31.647691Z","iopub.execute_input":"2022-04-04T20:46:31.648086Z","iopub.status.idle":"2022-04-04T20:46:47.883039Z","shell.execute_reply.started":"2022-04-04T20:46:31.648038Z","shell.execute_reply":"2022-04-04T20:46:47.881864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_idx = 1\nxray_sample = items_train[sample_idx].dcmread()\nxray_sample","metadata":{"execution":{"iopub.status.busy":"2022-04-04T20:55:57.068643Z","iopub.execute_input":"2022-04-04T20:55:57.068955Z","iopub.status.idle":"2022-04-04T20:55:57.117094Z","shell.execute_reply.started":"2022-04-04T20:55:57.06892Z","shell.execute_reply":"2022-04-04T20:55:57.116349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xray_sample.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-04T20:48:01.783431Z","iopub.execute_input":"2022-04-04T20:48:01.783729Z","iopub.status.idle":"2022-04-04T20:48:11.38953Z","shell.execute_reply.started":"2022-04-04T20:48:01.783698Z","shell.execute_reply":"2022-04-04T20:48:11.388837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom_train = pd.DataFrame.from_dicoms(items_train)\ndicom_train[:5]","metadata":{"execution":{"iopub.status.busy":"2022-04-04T21:04:24.502156Z","iopub.execute_input":"2022-04-04T21:04:24.502607Z","iopub.status.idle":"2022-04-04T21:04:44.78639Z","shell.execute_reply.started":"2022-04-04T21:04:24.502579Z","shell.execute_reply":"2022-04-04T21:04:44.785362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom_train.to_csv('dicom_metadata_train.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-04-04T21:06:20.920035Z","iopub.execute_input":"2022-04-04T21:06:20.920366Z","iopub.status.idle":"2022-04-04T21:06:20.932465Z","shell.execute_reply.started":"2022-04-04T21:06:20.920331Z","shell.execute_reply":"2022-04-04T21:06:20.930241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom_test = pd.DataFrame.from_dicoms(items_test)\ndicom_test[:5]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom_test.to_csv('dicom_metadata_test.csv', index = False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**You can access the dataset here:**\n\nhttps://www.kaggle.com/datasets/ibombonato/unifesp-xray-body-part-dicom-metadata-csv","metadata":{}}]}