{"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 numpy as np\nimport nibabel as nib\nimport glob\nimport pandas as pd\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.utils import normalize\nimport matplotlib.pyplot as plt\nfrom sklearn.preprocessing import MinMaxScaler\nimport os\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport cv2\nscaler = MinMaxScaler()\n\n\nimport tensorflow as tf","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-26T13:34:05.810211Z","iopub.execute_input":"2023-03-26T13:34:05.811154Z","iopub.status.idle":"2023-03-26T13:34:05.817896Z","shell.execute_reply.started":"2023-03-26T13:34:05.811105Z","shell.execute_reply":"2023-03-26T13:34:05.816790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install protobuf --no-index --find-links=file:///kaggle/input/focal-loss-package/storage_dir/protobuf-3.19.6-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl --force-reinstall","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install focal_loss --no-index --find-links=file:///kaggle/input/focal-loss-package/storage_dir/focal_loss-0.0.7-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2023-03-26T14:30:18.863165Z","iopub.execute_input":"2023-03-26T14:30:18.864346Z","iopub.status.idle":"2023-03-26T14:30:28.816344Z","shell.execute_reply.started":"2023-03-26T14:30:18.864286Z","shell.execute_reply":"2023-03-26T14:30:28.815011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_dir = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/'\ntest_predictions = pd.read_csv(root_dir+'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-03-26T13:02:34.812787Z","iopub.execute_input":"2023-03-26T13:02:34.813717Z","iopub.status.idle":"2023-03-26T13:02:34.821671Z","shell.execute_reply.started":"2023-03-26T13:02:34.813668Z","shell.execute_reply":"2023-03-26T13:02:34.820668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions.tail()","metadata":{"execution":{"iopub.status.busy":"2023-03-26T13:02:42.116004Z","iopub.execute_input":"2023-03-26T13:02:42.116686Z","iopub.status.idle":"2023-03-26T13:02:42.126594Z","shell.execute_reply.started":"2023-03-26T13:02:42.116647Z","shell.execute_reply":"2023-03-26T13:02:42.125552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_folder_list = sorted(glob.glob(root_dir + 'test/*'))\n","metadata":{"execution":{"iopub.status.busy":"2023-03-26T12:54:53.814021Z","iopub.execute_input":"2023-03-26T12:54:53.814399Z","iopub.status.idle":"2023-03-26T12:54:53.822038Z","shell.execute_reply.started":"2023-03-26T12:54:53.814364Z","shell.execute_reply":"2023-03-26T12:54:53.820894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_folder_list[2]","metadata":{"execution":{"iopub.status.busy":"2023-03-26T12:54:59.750431Z","iopub.execute_input":"2023-03-26T12:54:59.751365Z","iopub.status.idle":"2023-03-26T12:54:59.758612Z","shell.execute_reply.started":"2023-03-26T12:54:59.751314Z","shell.execute_reply":"2023-03-26T12:54:59.757459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_dcm(path, voi_lut = True, fix_monochrome = True):\n    dicom = pydicom.read_file(path)\n    \n    # VOI LUT (if available by DICOM device) is used to 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               \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        \n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    \n    data=cv2.resize(data, dsize=(240, 240), interpolation=cv2.INTER_LINEAR)\n        \n    return data","metadata":{"execution":{"iopub.status.busy":"2023-03-26T13:17:05.494856Z","iopub.execute_input":"2023-03-26T13:17:05.495318Z","iopub.status.idle":"2023-03-26T13:17:05.508361Z","shell.execute_reply.started":"2023-03-26T13:17:05.495276Z","shell.execute_reply":"2023-03-26T13:17:05.507094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_3D_volume(images_list):\n    \n    volume=[]\n    for image in images_list:\n        npy_image=read_dcm(image)\n        volume.append(npy_image)\n    \n    volume_npy=np.array(volume)\n    return volume_npy","metadata":{"execution":{"iopub.status.busy":"2023-03-26T13:24:18.242310Z","iopub.execute_input":"2023-03-26T13:24:18.242685Z","iopub.status.idle":"2023-03-26T13:24:18.249985Z","shell.execute_reply.started":"2023-03-26T13:24:18.242652Z","shell.execute_reply":"2023-03-26T13:24:18.247083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"volume=create_3D_volume(sorted(glob.glob(test_folder_list[30]+'/FLAIR/*')))\nvolume=normalize(volume, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-03-26T13:46:54.803657Z","iopub.execute_input":"2023-03-26T13:46:54.804310Z","iopub.status.idle":"2023-03-26T13:46:55.045637Z","shell.execute_reply.started":"2023-03-26T13:46:54.804272Z","shell.execute_reply":"2023-03-26T13:46:55.044405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (4,4))\nplt.imshow(volume[30,:,:], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-03-26T13:46:56.402639Z","iopub.execute_input":"2023-03-26T13:46:56.403240Z","iopub.status.idle":"2023-03-26T13:46:56.621153Z","shell.execute_reply.started":"2023-03-26T13:46:56.403187Z","shell.execute_reply":"2023-03-26T13:46:56.620167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.shape(volume)","metadata":{"execution":{"iopub.status.busy":"2023-03-26T13:46:59.625476Z","iopub.execute_input":"2023-03-26T13:46:59.626538Z","iopub.status.idle":"2023-03-26T13:46:59.633703Z","shell.execute_reply.started":"2023-03-26T13:46:59.626487Z","shell.execute_reply":"2023-03-26T13:46:59.632464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-03-26T14:30:46.521936Z","iopub.execute_input":"2023-03-26T14:30:46.523628Z","iopub.status.idle":"2023-03-26T14:30:46.532443Z","shell.execute_reply.started":"2023-03-26T14:30:46.523563Z","shell.execute_reply":"2023-03-26T14:30:46.531133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mask_percentage(mask):\n    num_zeros = (mask == 0).sum()\n    num_not_zero = (mask != 0).sum()\n\n    percent=num_not_zero/(num_zeros+num_not_zero)\n    return percent","metadata":{"execution":{"iopub.status.busy":"2023-03-26T13:38:45.501496Z","iopub.execute_input":"2023-03-26T13:38:45.501866Z","iopub.status.idle":"2023-03-26T13:38:45.507272Z","shell.execute_reply.started":"2023-03-26T13:38:45.501833Z","shell.execute_reply":"2023-03-26T13:38:45.506213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def segmentation_from_3D(volume):\n    #imported the saved UNet model\n    UNetModel=tf.keras.models.load_model('/kaggle/input/resnet-unet-custom-deepan' + '/UNet_fold1.hdf5')\n    \n    number_of_slices=np.shape(volume)[0]\n    start=int(0.0*number_of_slices)\n    end=int(1*number_of_slices-1)\n    print(start)\n    print(end)\n    mask_pred=UNetModel.predict(volume[start:end+1, :, :])\n    mask_pred_argmax=np.argmax(mask_pred, axis=3)\n    \n    slice=0\n    best_slice_index=-1\n    best_slice_mask_percent=0.0\n    \n    #Finding the best slice mask\n    while(slice<(end-start+1)): #iterating over slices the mask_ROI\n        mask_slice=mask_pred_argmax[slice,:, :]\n        image_slice=volume[slice,:, :]\n\n        mask_slice=np.array(mask_slice)\n        image_slice=np.array(image_slice)\n\n        if(mask_percentage(mask_slice)>best_slice_mask_percent):\n            best_slice_index=slice\n            best_slice_mask_percent= mask_percentage(mask_slice)\n\n\n        slice=slice+1\n\n    #we have the best slice of the predicted volume as best_slice_index\n    if(best_slice_index==start):\n        img_1=volume[start+best_slice_index, :, :]\n    else:\n        img_1=volume[start+best_slice_index-1, :, :]\n    \n    img_2=volume[start+best_slice_index,:, :]\n    \n    if(best_slice_index==end):\n        img_3=volume[start+best_slice_index, :,:]\n    else:\n        img_3=volume[start+best_slice_index+1, :,:]\n\n    if(mask_percentage(mask_pred_argmax[best_slice_index,:, :])>0.005):\n        mask_2=mask_pred_argmax[best_slice_index,:, :]  #the actual best slice mask\n    else:\n        mask_2=mask_pred_argmax[int((start-end+1)/2),:, :] \n\n    rect= cv2.boundingRect(mask_2.astype(np.uint8)) \n    #cropping the images based on the binary mask area\n    cropped_img_1 = img_1[rect[1]:(rect[1]+rect[3]), rect[0]:(rect[0]+rect[2])]\n    cropped_img_2 = img_2[rect[1]:(rect[1]+rect[3]), rect[0]:(rect[0]+rect[2])]\n    cropped_img_3= img_3[rect[1]:(rect[1]+rect[3]), rect[0]:(rect[0]+rect[2])]\n\n    #stacking the images on top of one another\n    cropped_img=np.stack([cropped_img_1, cropped_img_2, cropped_img_3], axis=2)\n    \n    return cv2.resize(cropped_img, dsize=(100, 100), interpolation=cv2.INTER_LINEAR)","metadata":{"execution":{"iopub.status.busy":"2023-03-26T14:51:07.872174Z","iopub.execute_input":"2023-03-26T14:51:07.872927Z","iopub.status.idle":"2023-03-26T14:51:07.887401Z","shell.execute_reply.started":"2023-03-26T14:51:07.872887Z","shell.execute_reply":"2023-03-26T14:51:07.886285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stack_array=[]\n\nfor test_data_dir in test_folder_list:\n    test_images_list=sorted(glob.glob(test_data_dir+'/FLAIR/*'))\n    print(test_data_dir)\n    #Create 3D volume from all DCM images\n    volume=create_3D_volume(test_images_list)\n    volume=normalize(volume, axis=1)\n    #pass volume to segmentation pipeline\n    stack=segmentation_from_3D(volume)\n    stack_array.append(stack)\n    \n    \n    \nstack_array=np.array(stack_array)","metadata":{"execution":{"iopub.status.busy":"2023-03-26T14:51:08.329450Z","iopub.execute_input":"2023-03-26T14:51:08.329822Z","iopub.status.idle":"2023-03-26T14:53:44.775600Z","shell.execute_reply.started":"2023-03-26T14:51:08.329791Z","shell.execute_reply":"2023-03-26T14:53:44.774480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.shape(stack_array))","metadata":{"execution":{"iopub.status.busy":"2023-03-26T14:54:42.384134Z","iopub.execute_input":"2023-03-26T14:54:42.384874Z","iopub.status.idle":"2023-03-26T14:54:42.390415Z","shell.execute_reply.started":"2023-03-26T14:54:42.384838Z","shell.execute_reply":"2023-03-26T14:54:42.389290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from focal_loss import BinaryFocalLoss\ndef classification_predictions(stack_array):\n    \n    class_model_1=tf.keras.models.load_model('/kaggle/input/resnet-unet-custom-deepan' + '/EfficientNetB7_combined.hdf5', custom_objects={'custom_loss': BinaryFocalLoss(gamma=2)} )\n    class_model_2=tf.keras.models.load_model('/kaggle/input/resnet-unet-custom-deepan' + '/ResNet18_fold1.hdf5', custom_objects={'custom_loss': BinaryFocalLoss(gamma=2)} )\n    y_pred_1=class_model_1.predict(stack_array)\n    y_pred_2=class_model_2.predict(stack_array)\n    \n    #y_pred=(y_pred_1+y_pred_2+y_pred_3+y_pred_4+y_pred_5)/5\n    y_pred=(y_pred_1 + y_pred_2)/2\n    return y_pred","metadata":{"execution":{"iopub.status.busy":"2023-03-26T14:55:26.490308Z","iopub.execute_input":"2023-03-26T14:55:26.490685Z","iopub.status.idle":"2023-03-26T14:55:26.496915Z","shell.execute_reply.started":"2023-03-26T14:55:26.490653Z","shell.execute_reply":"2023-03-26T14:55:26.495895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=classification_predictions(stack_array)","metadata":{"execution":{"iopub.status.busy":"2023-03-26T14:55:42.641642Z","iopub.execute_input":"2023-03-26T14:55:42.642013Z","iopub.status.idle":"2023-03-26T14:55:45.903664Z","shell.execute_reply.started":"2023-03-26T14:55:42.641980Z","shell.execute_reply":"2023-03-26T14:55:45.902617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"execution":{"iopub.status.busy":"2023-03-26T14:55:50.873351Z","iopub.execute_input":"2023-03-26T14:55:50.874452Z","iopub.status.idle":"2023-03-26T14:55:50.886033Z","shell.execute_reply.started":"2023-03-26T14:55:50.874402Z","shell.execute_reply":"2023-03-26T14:55:50.884932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds=list(y_pred[:,1])","metadata":{"execution":{"iopub.status.busy":"2023-03-26T14:56:32.809969Z","iopub.execute_input":"2023-03-26T14:56:32.810364Z","iopub.status.idle":"2023-03-26T14:56:32.815912Z","shell.execute_reply.started":"2023-03-26T14:56:32.810331Z","shell.execute_reply":"2023-03-26T14:56:32.814716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds","metadata":{"execution":{"iopub.status.busy":"2023-03-26T14:56:37.047339Z","iopub.execute_input":"2023-03-26T14:56:37.048669Z","iopub.status.idle":"2023-03-26T14:56:37.057586Z","shell.execute_reply.started":"2023-03-26T14:56:37.048621Z","shell.execute_reply":"2023-03-26T14:56:37.056378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions['MGMT_value'] = preds\ntest_predictions.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-26T14:58:28.165055Z","iopub.execute_input":"2023-03-26T14:58:28.166031Z","iopub.status.idle":"2023-03-26T14:58:28.180312Z","shell.execute_reply.started":"2023-03-26T14:58:28.165983Z","shell.execute_reply":"2023-03-26T14:58:28.179057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}