{"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 pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\n\nimport os\nfrom tqdm.notebook import tqdm\nimport SimpleITK as sitk","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-24T03:36:22.737669Z","iopub.execute_input":"2021-08-24T03:36:22.738070Z","iopub.status.idle":"2021-08-24T03:36:23.455304Z","shell.execute_reply.started":"2021-08-24T03:36:22.737984Z","shell.execute_reply":"2021-08-24T03:36:23.454453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_DIR = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/'\nOUT_FOLDER = 'train'\nMRI_TYPES = ['T1w', 'T1wCE','T2w', 'FLAIR']\nEXT = 'jpg'\n\nDEBUG = False","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:36:23.456670Z","iopub.execute_input":"2021-08-24T03:36:23.457135Z","iopub.status.idle":"2021-08-24T03:36:23.461264Z","shell.execute_reply.started":"2021-08-24T03:36:23.457090Z","shell.execute_reply":"2021-08-24T03:36:23.460302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reader = sitk.ImageSeriesReader()\nreader.LoadPrivateTagsOn()","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:36:23.462968Z","iopub.execute_input":"2021-08-24T03:36:23.463500Z","iopub.status.idle":"2021-08-24T03:36:23.480789Z","shell.execute_reply.started":"2021-08-24T03:36:23.463462Z","shell.execute_reply":"2021-08-24T03:36:23.479671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:36:23.482443Z","iopub.execute_input":"2021-08-24T03:36:23.482847Z","iopub.status.idle":"2021-08-24T03:36:23.499093Z","shell.execute_reply.started":"2021-08-24T03:36:23.482811Z","shell.execute_reply":"2021-08-24T03:36:23.498159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:36:23.500326Z","iopub.execute_input":"2021-08-24T03:36:23.500597Z","iopub.status.idle":"2021-08-24T03:36:23.870847Z","shell.execute_reply.started":"2021-08-24T03:36:23.500570Z","shell.execute_reply":"2021-08-24T03:36:23.869872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dicom = pydicom.dcmread('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/FLAIR/Image-1.dcm')","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:36:23.872070Z","iopub.execute_input":"2021-08-24T03:36:23.872339Z","iopub.status.idle":"2021-08-24T03:36:23.877670Z","shell.execute_reply.started":"2021-08-24T03:36:23.872312Z","shell.execute_reply":"2021-08-24T03:36:23.876533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_meta_info_v2(dicom):\n    ret_dict = {dicom.get(k).name.replace(' ',''): dicom.get(k).value for k in dicom.keys() if dicom.get(k).name != 'Pixel Data'}\n    ret_dict['timestamp'] = dicom.timestamp\n    return ret_dict","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:36:23.879016Z","iopub.execute_input":"2021-08-24T03:36:23.879311Z","iopub.status.idle":"2021-08-24T03:36:23.890511Z","shell.execute_reply.started":"2021-08-24T03:36:23.879281Z","shell.execute_reply":"2021-08-24T03:36:23.889310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# meta = get_meta_info_v2(dicom)\n# meta2 = meta.copy()\n# meta2['abc'] = 111","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:36:23.893232Z","iopub.execute_input":"2021-08-24T03:36:23.893615Z","iopub.status.idle":"2021-08-24T03:36:23.902836Z","shell.execute_reply.started":"2021-08-24T03:36:23.893582Z","shell.execute_reply":"2021-08-24T03:36:23.901639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pd.DataFrame([meta, meta2])","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:36:23.904588Z","iopub.execute_input":"2021-08-24T03:36:23.905093Z","iopub.status.idle":"2021-08-24T03:36:23.914934Z","shell.execute_reply.started":"2021-08-24T03:36:23.905033Z","shell.execute_reply":"2021-08-24T03:36:23.913789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_dicom(path, voi_lut=True, fix_monochrome=True):\n    dicom = pydicom.dcmread(path)\n    # VOI LUT (if available by DICOM device) is used to\n    # transform raw DICOM data to \"human-friendly\" view\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    \n    # read other meta\n    meta = get_meta_info_v2(dicom)\n    \n    return data, meta","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:36:23.916135Z","iopub.execute_input":"2021-08-24T03:36:23.916673Z","iopub.status.idle":"2021-08-24T03:36:23.929528Z","shell.execute_reply.started":"2021-08-24T03:36:23.916620Z","shell.execute_reply":"2021-08-24T03:36:23.928072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resample(image, ref_image):\n    resampler = sitk.ResampleImageFilter()\n    resampler.SetReferenceImage(ref_image)\n    resampler.SetInterpolator(sitk.sitkLinear)\n    \n    resampler.SetTransform(sitk.AffineTransform(image.GetDimension()))\n\n    resampler.SetOutputSpacing(ref_image.GetSpacing())\n\n    resampler.SetSize(ref_image.GetSize())\n\n    resampler.SetOutputDirection(ref_image.GetDirection())\n\n    resampler.SetOutputOrigin(ref_image.GetOrigin())\n\n    resampler.SetDefaultPixelValue(image.GetPixelIDValue())\n\n    resamped_image = resampler.Execute(image)\n    \n    return resamped_image\n\ndef normalize(data):\n    return (data - np.min(data)) / (np.max(data) - np.min(data))","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:36:23.931608Z","iopub.execute_input":"2021-08-24T03:36:23.931949Z","iopub.status.idle":"2021-08-24T03:36:23.942944Z","shell.execute_reply.started":"2021-08-24T03:36:23.931919Z","shell.execute_reply":"2021-08-24T03:36:23.941657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/'\npatient_id = 688\nseries_path = f'{train_path}/{patient_id:5d}/T1w'\nprint(series_path)\nfilenamesDICOM = reader.GetGDCMSeriesFileNames(f'{train_path}/{patient_id:05d}/T1wCE')\nreader.SetFileNames(filenamesDICOM)\nref_voxels = reader.Execute()","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:36:23.944159Z","iopub.execute_input":"2021-08-24T03:36:23.944439Z","iopub.status.idle":"2021-08-24T03:36:26.271125Z","shell.execute_reply.started":"2021-08-24T03:36:23.944412Z","shell.execute_reply":"2021-08-24T03:36:26.270066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"writer = sitk.ImageFileWriter()","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:36:26.275980Z","iopub.execute_input":"2021-08-24T03:36:26.276371Z","iopub.status.idle":"2021-08-24T03:36:26.282880Z","shell.execute_reply.started":"2021-08-24T03:36:26.276331Z","shell.execute_reply":"2021-08-24T03:36:26.281796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_ids = []\nimage_names = []\nmri_types = []\nmetas = []\n\nmri_type_mapping = {\n    'T1w':'t1',\n    'T1wCE':'t1ce',\n    'T2w':'t2',\n    'FLAIR':'flair'\n}\n\niterations = tqdm(os.listdir(TRAIN_DIR)) if not DEBUG else tqdm(os.listdir(TRAIN_DIR)[:3])\n\nfor patient_id in iterations:\n    patient_dir = os.path.join(TRAIN_DIR, patient_id) \n    for mri_type in MRI_TYPES:\n        type_dir = os.path.join(patient_dir, mri_type)\n        out_dir = os.path.join(OUT_FOLDER, patient_id, mri_type)\n#         os.makedirs(out_dir, exist_ok=True)\n            # read dicom series at once\n        try:\n            filenamesDICOM = reader.GetGDCMSeriesFileNames(type_dir)\n            reader.SetFileNames(filenamesDICOM)\n            voxels = reader.Execute()\n            resampled_voxels = resample(voxels, ref_voxels)\n#             resampled_voxels_arr = sitk.GetArrayViewFromImage(resampled_voxels)\n#             transposed_resampled_voxels_arr = resampled_voxels_arr.T\n#             processed_voxels = sitk.GetImageFromArray(transposed_resampled_voxels_arr)\n            \n            processed_voxels = resampled_voxels\n            outputImageFileName = os.path.join('BraTS2021_Training_Data', f'BraTS2021_{patient_id}', \n                                               f'BraTS2021_{patient_id}_{mri_type_mapping[mri_type]}.nii.gz')\n            os.makedirs(os.path.dirname(outputImageFileName), exist_ok=True)\n            writer.SetFileName(outputImageFileName)\n            writer.Execute(resampled_voxels)\n\n        except Exception as ex:\n            print(ex)\n#         break\n#     break","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:39:12.283326Z","iopub.execute_input":"2021-08-24T03:39:12.283883Z","iopub.status.idle":"2021-08-24T03:39:33.207506Z","shell.execute_reply.started":"2021-08-24T03:39:12.283846Z","shell.execute_reply":"2021-08-24T03:39:33.206810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"outputImageFileName","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:39:33.208804Z","iopub.execute_input":"2021-08-24T03:39:33.209086Z","iopub.status.idle":"2021-08-24T03:39:33.215035Z","shell.execute_reply.started":"2021-08-24T03:39:33.209057Z","shell.execute_reply":"2021-08-24T03:39:33.214016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls BraTS2021_Training_Data/BraTS2021_00688/","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:39:33.216777Z","iopub.execute_input":"2021-08-24T03:39:33.217062Z","iopub.status.idle":"2021-08-24T03:39:33.997926Z","shell.execute_reply.started":"2021-08-24T03:39:33.217033Z","shell.execute_reply":"2021-08-24T03:39:33.996836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef visualize_3_planes(voxels):\n    plt.figure(figsize=(9,3))\n    plt.subplot(1,3,1)\n    plt.imshow(voxels[voxels.shape[0]//2])\n    plt.subplot(1,3,2)\n    plt.imshow(voxels[:, voxels.shape[1]//2, :])\n    plt.subplot(1,3,3)\n    plt.imshow(voxels[:,:,voxels.shape[2]//2])","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:39:34.000233Z","iopub.execute_input":"2021-08-24T03:39:34.000698Z","iopub.status.idle":"2021-08-24T03:39:34.007563Z","shell.execute_reply.started":"2021-08-24T03:39:34.000645Z","shell.execute_reply":"2021-08-24T03:39:34.006709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_arr = sitk.GetArrayViewFromImage(processed_voxels)","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:39:34.009147Z","iopub.execute_input":"2021-08-24T03:39:34.009905Z","iopub.status.idle":"2021-08-24T03:39:34.019033Z","shell.execute_reply.started":"2021-08-24T03:39:34.009858Z","shell.execute_reply":"2021-08-24T03:39:34.017936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_arr.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:39:34.020510Z","iopub.execute_input":"2021-08-24T03:39:34.021210Z","iopub.status.idle":"2021-08-24T03:39:34.032966Z","shell.execute_reply.started":"2021-08-24T03:39:34.021165Z","shell.execute_reply":"2021-08-24T03:39:34.031758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_3_planes(final_arr)","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:39:34.034323Z","iopub.execute_input":"2021-08-24T03:39:34.034607Z","iopub.status.idle":"2021-08-24T03:39:34.435597Z","shell.execute_reply.started":"2021-08-24T03:39:34.034579Z","shell.execute_reply":"2021-08-24T03:39:34.434453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# visualize_3_planes(resampled_voxels_arr)","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:39:52.507731Z","iopub.execute_input":"2021-08-24T03:39:52.508133Z","iopub.status.idle":"2021-08-24T03:39:52.513166Z","shell.execute_reply.started":"2021-08-24T03:39:52.508097Z","shell.execute_reply":"2021-08-24T03:39:52.511501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r BraTS2021_Training_Data.zip BraTS2021_Training_Data/ >> log.txt","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:39:53.572648Z","iopub.execute_input":"2021-08-24T03:39:53.573171Z","iopub.status.idle":"2021-08-24T03:39:55.731503Z","shell.execute_reply.started":"2021-08-24T03:39:53.573123Z","shell.execute_reply":"2021-08-24T03:39:55.730066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf BraTS2021_Training_Data","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:39:55.733969Z","iopub.execute_input":"2021-08-24T03:39:55.734288Z","iopub.status.idle":"2021-08-24T03:39:56.533389Z","shell.execute_reply.started":"2021-08-24T03:39:55.734252Z","shell.execute_reply":"2021-08-24T03:39:56.532095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # notes:\n# - Align voxel with a ref voxel\n# - Visualize to compare with data from task 1 (2 plane plots)","metadata":{"execution":{"iopub.status.busy":"2021-08-24T03:37:00.551659Z","iopub.status.idle":"2021-08-24T03:37:00.552147Z"},"trusted":true},"execution_count":null,"outputs":[]}]}