{"cells":[{"metadata":{"_uuid":"17191c226f30f1734b6089da4a9f52b5c3e9af83"},"cell_type":"markdown","source":"DICOM data contains not only the image pixel data but also the data format (meta data), shooting condition and patient information.\nIn this notebook, I extracted age and sex information from the given data.\n\nI'm not sure the extracted data is useful or not for what we want to predict..."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport sys, time, os, json\n\nimport matplotlib.pyplot as plt\nfrom  tqdm import tqdm\n\nimport pydicom\n\n\nclass Configs():\n    def __init__(self):\n        self.data_root_dir = '../input/'\n        self.test_image_dir = os.path.join(self.data_root_dir, 'stage_1_test_images/')\n        self.train_image_dir = os.path.join(self.data_root_dir, 'stage_1_train_images/')\n        self.file_ext = '.dcm'\n\nC = Configs()\n\nprint('\\n'.join(os.listdir(\"../input\")))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"markdown","source":"# load and join data"},{"metadata":{"trusted":true,"_uuid":"9ef6333887c04d9bb05f5f2bf388eeec8a62f998"},"cell_type":"code","source":"labels = pd.read_csv(C.data_root_dir+'stage_1_train_labels.csv', \n                     dtype={'patientId':str, \n                            'x':np.float, \n                            'y': np.float, \n                            'width': np.float, \n                            'height': np.float, \n                            'target': np.int})\n\ndetails = pd.read_csv(C.data_root_dir+'stage_1_detailed_class_info.csv', dtype={'patientId': str, 'class': str})\n# rename column name to avoid a trouble.\n# column name 'class' could cause the trouble when use query()\ndetails.columns = ['patientId', 'details']\nwhole_label_info = pd.concat([labels, details.drop('patientId', axis=1)], axis=1)\n\n# quick-check\nwhole_label_info['details'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6dde54014e117242e54f973b39f397649584757d"},"cell_type":"markdown","source":"# extract patient data\nNote: PatientBirthDate and PatientOrientation was empty."},{"metadata":{"trusted":true,"_uuid":"ea2edb154b63c0332d3cb8e43287586f3d36f16d"},"cell_type":"code","source":"patientId = whole_label_info['patientId'].drop_duplicates().values\npatient_info = pd.DataFrame()\nfor p in tqdm(patientId):\n    ds = pydicom.dcmread(C.train_image_dir + p + C.file_ext)\n    tmp_info = pd.DataFrame({\n        'patientId': [ds.PatientID],\n        'age': [ds.PatientAge],\n        'sex': [ds.PatientSex]\n    })\n    patient_info = patient_info.append(tmp_info)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"94a3a665ba45840079c66ee2b9b7f08fe3e17091"},"cell_type":"code","source":"whole_label_info = whole_label_info.merge(patient_info, on='patientId', how='left')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9d1b7058c5dbbd973f8e12c5b6b160036a29a791"},"cell_type":"markdown","source":"# let's see the extracted information"},{"metadata":{"trusted":true,"_uuid":"00fc72925080e10e4351dfd3df9669bbcccc29be"},"cell_type":"code","source":"stat_age = patient_info['age'].value_counts(dropna=False).reset_index().rename({'index': 'age', 'age': 'count'}, axis=1)\nstat_age['age'] = stat_age['age'].astype(int)\nstat_age.sort_values('age')\n\nprint('range of age: ', stat_age['age'].min(), stat_age['age'].max())\n\nplt.bar(stat_age['age'], stat_age['count'])\nplt.ylabel('count')\nplt.xlabel('age')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"01e0a4695df8ceaa53a4336de2a435312ea9d02b"},"cell_type":"markdown","source":"# check gender"},{"metadata":{"trusted":true,"_uuid":"809db17bb1788f63f7797ec34e5f7c8e6b921971"},"cell_type":"code","source":"stat_sex = patient_info['sex'].value_counts(dropna=False).reset_index().rename({'index': 'sex', 'sex': 'count'}, axis=1)\nplt.bar(stat_sex['sex'], stat_sex['count'])\nplt.ylabel('count')\nplt.xlabel('gender')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"aea2d8ccfd255d3acf339082d8dbc412c81e100c"},"cell_type":"markdown","source":"to be continued..."}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}