{"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":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport pydicom","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-01T22:01:23.216656Z","iopub.execute_input":"2021-06-01T22:01:23.216971Z","iopub.status.idle":"2021-06-01T22:01:23.221130Z","shell.execute_reply.started":"2021-06-01T22:01:23.216940Z","shell.execute_reply":"2021-06-01T22:01:23.219985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**This notebook looks for VOI LUT Sequences, VOI LUT Functions and Transfer Syntaxes in the SIIM-Covid19 train dataset.**\n\n- It took about 20 min to run on GPU","metadata":{}},{"cell_type":"code","source":"# Load the data\nbase_path = \"/kaggle/input/siim-covid19-detection/\"\nstudies_df = pd.read_csv(os.path.join(base_path,\"train_study_level.csv\"))\nimages_df = pd.read_csv(os.path.join(base_path,\"train_image_level.csv\"))\n\n# Strip the extra text from the study and image IDs\nstudies_df['id'] = studies_df['id'].map(lambda x: x.rstrip('_study'))\nimages_df['id'] = images_df['id'].map(lambda x: x.rstrip('_image'))\n\n# Merge the dfs together\ndata_df = pd.merge(images_df, studies_df, how='inner', left_on='StudyInstanceUID', right_on='id')\ndata_df.drop(['id_y'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-01T22:01:23.318560Z","iopub.execute_input":"2021-06-01T22:01:23.319202Z","iopub.status.idle":"2021-06-01T22:01:23.444543Z","shell.execute_reply.started":"2021-06-01T22:01:23.319098Z","shell.execute_reply":"2021-06-01T22:01:23.442489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This function finds the first image in a StudyInstanceUID directory and returns its path\ndef get_image_by_study_id(study_id):\n    study_path = base_path + \"train/\" + study_id + \"/\"\n    for subdir, dirs, files in os.walk(study_path):\n        for file in files:     \n            image = os.path.join(subdir, file)\n            if os.path.isfile(image):\n                return image\n    return \"none\"","metadata":{"execution":{"iopub.status.busy":"2021-06-01T22:01:23.447071Z","iopub.execute_input":"2021-06-01T22:01:23.447636Z","iopub.status.idle":"2021-06-01T22:01:23.453370Z","shell.execute_reply.started":"2021-06-01T22:01:23.447597Z","shell.execute_reply":"2021-06-01T22:01:23.452320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loop through the images and check how many images are not Explicit VR Little Endian transfer syntax.\n# Also count images with VOI LUT Sequence tags present\n\ncount = 0\nimages_with_voi_lut = 0\nimages_with_voi_lut_function = 0\nimages_not_explicit_le = 0\ndeflated_syntaxes = []\nvoi_lut_functions = []\n\nfor index, row in data_df.iterrows():\n    img_file = get_image_by_study_id(row['StudyInstanceUID'])\n    img = pydicom.dcmread(img_file)\n    \n    # Check for a LUT sqequence tag\n    if (0x0028,0x3010) in img:\n        images_with_voi_lut += 1\n    \n    # Check for a LUT Function tag\n    if (0x0028,0x1056) in img:\n        images_with_voi_lut_function += 1\n        \n        if img(0x0028,0x1056) not in voi_lut_functions:\n            voi_lut_functions.append(img(0x0028,0x1056))\n            \n    # Check the transfer syntax\n    if img.file_meta.TransferSyntaxUID != \"1.2.840.10008.1.2.1\":\n        images_not_explicit_le += 1\n        \n        if img.file_meta.TransferSyntaxUID not in deflated_syntaxes:\n            deflated_syntaxes.append(img.file_meta.TransferSyntaxUID)\n        \n    count += 1\n    \nprint(\"Done checking \" + str(count) + \" images\")\nprint(\"Found \" + str(images_with_voi_lut) + \" images with VOI LUT\")\nprint(\"Found \" + str(images_with_voi_lut_function) + \" images with VOI LUT Function\")\nprint(\"VOI LUT Functions:\")\nprint(voi_lut_functions)\nprint(\"Found \" + str(images_not_explicit_le) + \" images that are not Explicit VR LE\")\nprint(\"Non Explicit VR LE Transfer Syntaxes\")\nprint(deflated_syntaxes)","metadata":{"execution":{"iopub.status.busy":"2021-06-01T22:01:23.454893Z","iopub.execute_input":"2021-06-01T22:01:23.455562Z","iopub.status.idle":"2021-06-01T22:14:51.323871Z","shell.execute_reply.started":"2021-06-01T22:01:23.455525Z","shell.execute_reply":"2021-06-01T22:14:51.323113Z"},"trusted":true},"execution_count":null,"outputs":[]}]}