{"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":"markdown","source":"Hello Fellas, this notebook is referred from best notebook so far for this particular competition https://www.kaggle.com/tanlikesmath/brain-tumor-radiogenomic-classification-eda-wip. I am no expert in this radiology field so I will avoid copy pasting amazing informative content written in referred notebook. Right now, I just tried to read dicom data using simple approach and plotting a 3D graph of it. I can say more such short but effective experiments are on the way in this notebook!","metadata":{}},{"cell_type":"markdown","source":"# Imports\n\nAt first, importing the required modules:","metadata":{}},{"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 pydicom\nimport glob\nfrom tqdm.notebook import tqdm\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport matplotlib.pyplot as plt\nfrom skimage import exposure\nimport cv2\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:22:37.151025Z","iopub.execute_input":"2021-07-14T08:22:37.151359Z","iopub.status.idle":"2021-07-14T08:22:37.157043Z","shell.execute_reply.started":"2021-07-14T08:22:37.151329Z","shell.execute_reply":"2021-07-14T08:22:37.155945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# A look at the provided data\n\nLet's check what data is available to us:","metadata":{}},{"cell_type":"code","source":"dataset_path = Path('../input/rsna-miccai-brain-tumor-radiogenomic-classification')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:22:37.158684Z","iopub.execute_input":"2021-07-14T08:22:37.159041Z","iopub.status.idle":"2021-07-14T08:22:37.168052Z","shell.execute_reply.started":"2021-07-14T08:22:37.159003Z","shell.execute_reply":"2021-07-14T08:22:37.167175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(dataset_path/'train_labels.csv')\nprint(f'There are {len(train_df)} patients in the dataset')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:22:37.169872Z","iopub.execute_input":"2021-07-14T08:22:37.170342Z","iopub.status.idle":"2021-07-14T08:22:37.186630Z","shell.execute_reply.started":"2021-07-14T08:22:37.170304Z","shell.execute_reply":"2021-07-14T08:22:37.185695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:22:37.188433Z","iopub.execute_input":"2021-07-14T08:22:37.189229Z","iopub.status.idle":"2021-07-14T08:22:37.199058Z","shell.execute_reply.started":"2021-07-14T08:22:37.189188Z","shell.execute_reply":"2021-07-14T08:22:37.198131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"You might wonder what are MGMT values, as they mentioned in overview-\nThe presence of a specific genetic sequence in the tumor known as MGMT promoter methylation has been shown to be a favorable prognostic factor and a strong predictor of responsiveness to chemotherapy.\n\nBraTS21ID-Its just an ID provided\n\nStructure of the data:\n\n* train_labels.csv - file containing the target `MGMT_value` for each of the 585 patients in the training data\n* sample_submission.csv - a sample submission file for us to predict `MGMT_value`.\n* train folder - comprises of MRI scans for 585 patients in DICOM format.\n* test folder - The hidden test dataset is >5x the size of the training set.","metadata":{}},{"cell_type":"code","source":"train_df['MGMT_value'].hist()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:22:37.200358Z","iopub.execute_input":"2021-07-14T08:22:37.200987Z","iopub.status.idle":"2021-07-14T08:22:37.356499Z","shell.execute_reply.started":"2021-07-14T08:22:37.200935Z","shell.execute_reply":"2021-07-14T08:22:37.355505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The distribution of the MGMT methylation status is fairly balanced.","metadata":{}},{"cell_type":"markdown","source":"# Dataset organization and MRI images\n\nThis competition dataset provides four types of MRI images:\n\n1. [Fluid Attenuated Inversion Recovery (FLAIR)](https://en.wikipedia.org/wiki/Fluid-attenuated_inversion_recovery)\n2. [T1-weighted pre-contrast (T1w)](https://radiopaedia.org/articles/t1-weighted-image?lang=us)\n3. [T1-weighted post-contrast (T1Gd)](https://radiopaedia.org/articles/t1-weighted-image?lang=us)\n4. [T2-weighted (T2)](https://radiopaedia.org/articles/t2-weighted-image?lang=us)\n","metadata":{}},{"cell_type":"markdown","source":"Picking 2 random scans as examples in order to visualize and 3D plot","metadata":{}},{"cell_type":"code","source":"def load_dicoms(dcm_path):\n    temp = []\n    files = os.listdir(dcm_path)\n    temp.append(files[0])\n    temp.append(files[1])\n    slices = [pydicom.dcmread(dcm_path + \"/\" + file) for file in temp]\n    slices.sort(key = lambda x: float(x.ImagePositionPatient[2]))\n\n    return slices\n\ndef plot_img(img, size=(7, 7), is_rgb=True, title=\"\", cmap='gray'):\n    plt.figure(figsize=size)\n    plt.imshow(img, cmap=cmap)\n    plt.suptitle(title)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:22:37.357884Z","iopub.execute_input":"2021-07-14T08:22:37.358257Z","iopub.status.idle":"2021-07-14T08:22:37.364869Z","shell.execute_reply.started":"2021-07-14T08:22:37.358216Z","shell.execute_reply":"2021-07-14T08:22:37.364025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scans = load_dicoms('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00688/FLAIR')\nprint(scans)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:22:37.366922Z","iopub.execute_input":"2021-07-14T08:22:37.367488Z","iopub.status.idle":"2021-07-14T08:22:37.387115Z","shell.execute_reply.started":"2021-07-14T08:22:37.367450Z","shell.execute_reply":"2021-07-14T08:22:37.386266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(scans[0].pixel_array, cmap=\"bone\")","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:22:37.388423Z","iopub.execute_input":"2021-07-14T08:22:37.388735Z","iopub.status.idle":"2021-07-14T08:22:37.565279Z","shell.execute_reply.started":"2021-07-14T08:22:37.388703Z","shell.execute_reply":"2021-07-14T08:22:37.564196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Rescaling few raw pixel values to H-Units","metadata":{}},{"cell_type":"code","source":"def transform_to_hu(slices):\n    images = np.stack([file.pixel_array for file in slices])\n    images = images.astype(np.int16)\n\n#     images = set_outside_scanner_to_air(images)\n    \n    # convert to HU\n    for n in range(len(slices)):\n        \n        intercept = slices[n].RescaleIntercept\n        slope = slices[n].RescaleSlope\n        \n        if slope != 1:\n            images[n] = slope * images[n].astype(np.float64)\n            images[n] = images[n].astype(np.int16)\n            \n        images[n] += np.int16(intercept)\n    \n    return np.array(images, dtype=np.int16)\n\nhu_scans = transform_to_hu(scans)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:22:37.566889Z","iopub.execute_input":"2021-07-14T08:22:37.567256Z","iopub.status.idle":"2021-07-14T08:22:37.575605Z","shell.execute_reply.started":"2021-07-14T08:22:37.567217Z","shell.execute_reply":"2021-07-14T08:22:37.574764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from mpl_toolkits.mplot3d.art3d import Poly3DCollection\nfrom skimage import measure \ndef plot_3d(image, threshold=700, color=\"navy\"):\n    \n    # Position the scan upright, \n    # so the head of the patient would be at the top facing the camera\n    p = image.transpose(2,1,0)\n    \n    verts, faces,_,_ = measure.marching_cubes_lewiner(p, threshold)\n\n    fig = plt.figure(figsize=(10, 10))\n    ax = fig.add_subplot(111, projection='3d')\n\n    # Fancy indexing: `verts[faces]` to generate a collection of triangles\n    mesh = Poly3DCollection(verts[faces], alpha=0.2)\n    mesh.set_facecolor(color)\n    ax.add_collection3d(mesh)\n\n    ax.set_xlim(0, p.shape[0])\n    ax.set_ylim(0, p.shape[1])\n    ax.set_zlim(0, p.shape[2])\n\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:22:37.576845Z","iopub.execute_input":"2021-07-14T08:22:37.577348Z","iopub.status.idle":"2021-07-14T08:22:37.585853Z","shell.execute_reply.started":"2021-07-14T08:22:37.577304Z","shell.execute_reply":"2021-07-14T08:22:37.584930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_3d(hu_scans)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:22:37.587198Z","iopub.execute_input":"2021-07-14T08:22:37.587783Z","iopub.status.idle":"2021-07-14T08:22:43.264837Z","shell.execute_reply.started":"2021-07-14T08:22:37.587739Z","shell.execute_reply":"2021-07-14T08:22:43.263924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Honestly, it doesn't make that much sense like when we 3D plot chest X-rays!","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}