{"cells":[{"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 os\nimport gc\nimport glob\nimport pydicom\nfrom tqdm import tqdm\n\nimport multiprocessing\nfrom concurrent.futures import ThreadPoolExecutor\n\nBASE_PATH = '../input/rsna-str-pulmonary-embolism-detection'\nprint(os.listdir('../input/rsna-str-pulmonary-embolism-detection'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# train_df = pd.read_csv(os.path.join(BASE_PATH, 'train.csv'))\n# test_df = pd.read_csv(os.path.join(BASE_PATH, 'test.csv'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"LABEL_COLUMNS = ['negative_exam_for_pe', 'indeterminate', 'chronic_pe', 'acute_and_chronic_pe', 'central_pe', 'leftsided_pe', 'rightsided_pe', 'rv_lv_ratio_gte_1', 'rv_lv_ratio_lt_1']\nWEIGHTS       = [0.0736196319, 0.09202453988, 0.1042944785, 0.1042944785, 0.1877300613, 0.06257668712, 0.06257668712, 0.2346625767, 0.0782208589]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images = glob.glob(os.path.join(BASE_PATH, 'train/*/*/*.dcm'))\ntest_images = glob.glob(os.path.join(BASE_PATH, 'test/*/*/*.dcm'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"meta_columns = ['SOPInstanceUID', \n                'StudyInstanceUID', \n                'SeriesInstanceUID', \n                'InstanceNumber',\n                'SOPClassUID', \n                'SliceThickness', \n                'KVP', \n#                 'GantryDetectorTilt', \n                'TableHeight', \n#                 'RotationDirection', \n                'XRayTubeCurrent', \n                'Exposure', \n#                 'ConvolutionKernel', \n                'PatientPosition', \n                'ImagePositionPatient', \n#                 'ImageOrientationPatient', \n                'FrameOfReferenceUID', \n#                 'SamplesPerPixel', \n#                 'PhotometricInterpretation', \n#                 'Rows', \n#                 'Columns', \n#                 'PixelSpacing', \n#                 'BitsAllocated', \n#                 'BitsStored', \n#                 'HighBit', \n#                 'PixelRepresentation', \n                'WindowCenter', \n                'WindowWidth', \n                'RescaleIntercept', \n                'RescaleSlope']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Initialize dictionaries to collect the metadata\ncol_dict_train = {col: [] for col in meta_columns}\ncol_dict_test = {col: [] for col in meta_columns}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_first_of_dicom_field_as_int(x):\n    \"\"\"\n    https://www.kaggle.com/omission/eda-view-dicom-images-with-correct-windowing\n    \"\"\"\n    # get x[0] as in int is x is a 'pydicom.multival.MultiValue', otherwise get int(x)\n    if type(x) == pydicom.multival.MultiValue:\n        return int(x[0])\n    else:\n        return int(x)\n\n\ndef get_windowing(data):\n    \"\"\"\n    https://www.kaggle.com/omission/eda-view-dicom-images-with-correct-windowing\n    \"\"\"\n    dicom_fields = [data[('0028', '1050')].value,  # window center\n                    data[('0028', '1051')].value,  # window width\n                    data[('0028', '1052')].value,  # intercept\n                    data[('0028', '1053')].value]  # slope\n    return [get_first_of_dicom_field_as_int(x) for x in dicom_fields]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def process_train(img_path):\n    dicom_object = pydicom.dcmread(img_path)\n    for col in meta_columns: \n        window_center, window_width, intercept, slope = get_windowing(dicom_object)\n        if col == 'WindowWidth':\n            col_dict_train['WindowWidth'].append(window_width)\n        elif col == 'WindowCenter':\n            col_dict_train['WindowCenter'].append(window_center)\n        elif col == 'RescaleIntercept':\n            col_dict_train['RescaleIntercept'].append(intercept)\n        elif col == 'RescaleSlope':\n            col_dict_train['RescaleSlope'].append(slope)\n        else:\n            col_dict_train[col].append(str(getattr(dicom_object, col)))\n\ndef process_test(img_path):\n    dicom_object = pydicom.dcmread(img_path)\n    for col in meta_columns:\n        window_center, window_width, intercept, slope = get_windowing(dicom_object)\n        if col == 'WindowWidth':\n            col_dict_test['WindowWidth'].append(window_width)\n        elif col == 'WindowCenter':\n            col_dict_test['WindowCenter'].append(window_center)\n        elif col == 'RescaleIntercept':\n            col_dict_test['RescaleIntercept'].append(intercept)\n        elif col == 'RescaleSlope':\n            col_dict_test['RescaleSlope'].append(slope)\n        else:\n            col_dict_test[col].append(str(getattr(dicom_object, col)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\n# use multithreading to improve network or I/O bound tasks (such as read/write)\nwith ThreadPoolExecutor() as threads:\n    threads.map(process_train, train_images)\n\nmeta_df_train = pd.DataFrame(col_dict_train)\ndel col_dict_train\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\nwith ThreadPoolExecutor() as threads:\n    threads.map(process_test, test_images)\n\nmeta_df_test = pd.DataFrame(col_dict_test)\ndel col_dict_test\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"meta_df_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"meta_df_test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"meta_df_train.to_csv('train_meta.csv', index=False)\nmeta_df_test.to_csv('test_meta.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":4}