{"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 pandas as pd\nimport os\nimport pydicom\nimport glob\nimport seaborn as sn\nfrom fastai.vision.all import *\nfrom fastai.medical.imaging import *\nfrom tqdm.notebook import tqdm\nimport matplotlib.pyplot as plt\nfrom skimage import exposure\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport cv2\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:04.602554Z","iopub.execute_input":"2021-07-14T07:00:04.602959Z","iopub.status.idle":"2021-07-14T07:00:04.610921Z","shell.execute_reply.started":"2021-07-14T07:00:04.602923Z","shell.execute_reply":"2021-07-14T07:00:04.609278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Check files and folders","metadata":{}},{"cell_type":"code","source":"train_dataset_path = Path('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:04.612980Z","iopub.execute_input":"2021-07-14T07:00:04.613593Z","iopub.status.idle":"2021-07-14T07:00:04.628075Z","shell.execute_reply.started":"2021-07-14T07:00:04.613545Z","shell.execute_reply":"2021-07-14T07:00:04.627115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset_path.ls()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:04.630410Z","iopub.execute_input":"2021-07-14T07:00:04.631048Z","iopub.status.idle":"2021-07-14T07:00:04.652118Z","shell.execute_reply.started":"2021-07-14T07:00:04.631000Z","shell.execute_reply":"2021-07-14T07:00:04.651382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dicom2array(path, voi_lut=True, fix_monochrome=True):\n    dicom = pydicom.read_file(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    return data\n        \n    \ndef plot_img(img, size=(10, 10), is_rgb=True, title=\"\", cmap='gray'):\n    plt.figure(figsize=size)\n    plt.imshow(img, cmap=cmap)\n    plt.suptitle(title)\n    plt.show()\n\n\ndef plot_imgs(imgs, cols=4, size=7, is_rgb=True, title=\"\", cmap='gray', img_size=(500,500)):\n    rows = len(imgs)//cols + 1\n    fig = plt.figure(figsize=(cols*size, rows*size))\n    for i, img in enumerate(imgs):\n        if img_size is not None:\n            img = cv2.resize(img, img_size)\n        fig.add_subplot(rows, cols, i+1)\n        plt.imshow(img, cmap=cmap)\n    plt.suptitle(title)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:04.653343Z","iopub.execute_input":"2021-07-14T07:00:04.653640Z","iopub.status.idle":"2021-07-14T07:00:04.663295Z","shell.execute_reply.started":"2021-07-14T07:00:04.653607Z","shell.execute_reply":"2021-07-14T07:00:04.662376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom_paths = [i.ls()[10] for i in (train_dataset_path/'00045').ls()]\nimgs = [dicom2array(path) for path in dicom_paths]\nplot_imgs(imgs)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:04.686222Z","iopub.execute_input":"2021-07-14T07:00:04.686614Z","iopub.status.idle":"2021-07-14T07:00:05.270252Z","shell.execute_reply.started":"2021-07-14T07:00:04.686580Z","shell.execute_reply":"2021-07-14T07:00:05.269303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom_paths","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:05.271763Z","iopub.execute_input":"2021-07-14T07:00:05.272045Z","iopub.status.idle":"2021-07-14T07:00:05.278500Z","shell.execute_reply.started":"2021-07-14T07:00:05.272017Z","shell.execute_reply":"2021-07-14T07:00:05.277451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_path = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/'\ntrain_path = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train'\ntest_path = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/test'\n\nflair_dir = 'FLAIR'\nt1w_dir = 'T1w'\nt1wce_dir = 'T1wCE'\nt2w_dir = 'T2w'\n\nos.listdir(dataset_path)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:05.280337Z","iopub.execute_input":"2021-07-14T07:00:05.280641Z","iopub.status.idle":"2021-07-14T07:00:05.291330Z","shell.execute_reply.started":"2021-07-14T07:00:05.280611Z","shell.execute_reply":"2021-07-14T07:00:05.290314Z"},"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\")\nprint(f'There are {len(train_labels)} patients in this dataset')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:05.293087Z","iopub.execute_input":"2021-07-14T07:00:05.293355Z","iopub.status.idle":"2021-07-14T07:00:05.303935Z","shell.execute_reply.started":"2021-07-14T07:00:05.293329Z","shell.execute_reply":"2021-07-14T07:00:05.302765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:05.305102Z","iopub.execute_input":"2021-07-14T07:00:05.305561Z","iopub.status.idle":"2021-07-14T07:00:05.315913Z","shell.execute_reply.started":"2021-07-14T07:00:05.305531Z","shell.execute_reply":"2021-07-14T07:00:05.314633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.groupby('MGMT_value').count()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:05.317105Z","iopub.execute_input":"2021-07-14T07:00:05.317674Z","iopub.status.idle":"2021-07-14T07:00:05.329019Z","shell.execute_reply.started":"2021-07-14T07:00:05.317627Z","shell.execute_reply":"2021-07-14T07:00:05.328123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsn.countplot(train_labels.MGMT_value)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:05.330215Z","iopub.execute_input":"2021-07-14T07:00:05.330805Z","iopub.status.idle":"2021-07-14T07:00:05.450040Z","shell.execute_reply.started":"2021-07-14T07:00:05.330762Z","shell.execute_reply":"2021-07-14T07:00:05.448788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getFullId(id):\n    return str(id).zfill(5)\n    \ndef getFlairPath(id):\n    flair_path = os.path.join(train_path, getFullId(id), flair_dir)\n    return flair_path if os.path.isdir(flair_path) else False\n\ndef getT1wPath(id):\n    t1w_path = os.path.join(train_path, getFullId(id), t1w_dir)\n    return t1w_path if os.path.isdir(t1w_path) else False\n\ndef getT1wcePath(id):\n    t1wce_path = os.path.join(train_path, getFullId(id), t1wce_dir)\n    return t1wce_path if os.path.isdir(t1wce_path) else False\n\ndef getT2wPath(id):\n    t2w_path = os.path.join(train_path, getFullId(id), t2w_dir)\n    return t2w_path if os.path.isdir(t2w_path) else False","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:05.452579Z","iopub.execute_input":"2021-07-14T07:00:05.452880Z","iopub.status.idle":"2021-07-14T07:00:05.459883Z","shell.execute_reply.started":"2021-07-14T07:00:05.452849Z","shell.execute_reply":"2021-07-14T07:00:05.458722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def countFlairFiles(id):\n    path = getFlairPath(id)\n    return len([file for file in os.listdir(path)])\n\ndef countT1wFiles(id):\n    path = getT1wPath(id)\n    return len([file for file in os.listdir(path)])\n\ndef countT1wceFiles(id):\n    path = getT1wcePath(id)\n    return len([file for file in os.listdir(path)])\n\ndef countT2wFiles(id):\n    path = getT2wPath(id)\n    return len([file for file in os.listdir(path)])\n\ntrain_labels['FLAIR'] = train_labels['BraTS21ID'].apply(lambda x: countFlairFiles(x))\ntrain_labels['T1w'] = train_labels['BraTS21ID'].apply(lambda x: countT1wFiles(x))\ntrain_labels['T1wCE'] = train_labels['BraTS21ID'].apply(lambda x: countT1wceFiles(x))\ntrain_labels['T2w'] = train_labels['BraTS21ID'].apply(lambda x: countT2wFiles(x))","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:05.461520Z","iopub.execute_input":"2021-07-14T07:00:05.461834Z","iopub.status.idle":"2021-07-14T07:00:07.304174Z","shell.execute_reply.started":"2021-07-14T07:00:05.461804Z","shell.execute_reply":"2021-07-14T07:00:07.303036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sn.boxplot(x=\"variable\", y=\"value\", data=pd.melt(train_labels[['FLAIR', 'T1w', 'T1wCE', 'T2w']]))\nplt.title('Number of images files by structural multi-parametric MRI')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:07.305640Z","iopub.execute_input":"2021-07-14T07:00:07.306022Z","iopub.status.idle":"2021-07-14T07:00:07.471094Z","shell.execute_reply.started":"2021-07-14T07:00:07.305980Z","shell.execute_reply":"2021-07-14T07:00:07.469868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get3ScaledImage(path):\n\n    dicom = pydicom.read_file(path)\n    img = dicom.pixel_array\n\n    r, c = img.shape\n    img_conv = np.empty((c, r, 3), dtype=img.dtype)\n    img_conv[:,:,2] = img_conv[:,:,1] = img_conv[:,:,0] = img\n\n    ## Step 1. Convert to float to avoid overflow or underflow losses.\n    img_2d = img_conv.astype(float)\n\n    ## Step 2. Rescaling grey scale between 0-255\n    img_2d_scaled = (np.maximum(img_2d,0) / img_2d.max()) * 255.0\n\n    ## Step 3. Convert to uint\n    img_2d_scaled = np.uint8(img_2d_scaled)\n    img_2d_scaled.reshape([img_2d_scaled.shape[0], img_2d_scaled.shape[1], 3])\n    \n    return img_2d_scaled, (c, r)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:07.472383Z","iopub.execute_input":"2021-07-14T07:00:07.472731Z","iopub.status.idle":"2021-07-14T07:00:07.479391Z","shell.execute_reply.started":"2021-07-14T07:00:07.472695Z","shell.execute_reply":"2021-07-14T07:00:07.478282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id = 0\n\nnb = countFlairFiles(id)\npath = getFlairPath(id)\nframes =[]\n\nfor i in range(nb):\n    file_name = 'Image-' + str(i+1) + '.dcm'\n    img_path = os.path.join(path, file_name)\n    img_2d_scaled, size = get3ScaledImage(img_path)\n    frames.append(img_2d_scaled)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:07.480994Z","iopub.execute_input":"2021-07-14T07:00:07.481382Z","iopub.status.idle":"2021-07-14T07:00:10.409772Z","shell.execute_reply.started":"2021-07-14T07:00:07.481341Z","shell.execute_reply":"2021-07-14T07:00:10.408777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_2d_scaled.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:10.411360Z","iopub.execute_input":"2021-07-14T07:00:10.411807Z","iopub.status.idle":"2021-07-14T07:00:10.418849Z","shell.execute_reply.started":"2021-07-14T07:00:10.411753Z","shell.execute_reply":"2021-07-14T07:00:10.417671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_2d_scaled","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:10.420338Z","iopub.execute_input":"2021-07-14T07:00:10.420675Z","iopub.status.idle":"2021-07-14T07:00:10.432760Z","shell.execute_reply.started":"2021-07-14T07:00:10.420644Z","shell.execute_reply":"2021-07-14T07:00:10.431874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:10.434354Z","iopub.execute_input":"2021-07-14T07:00:10.434804Z","iopub.status.idle":"2021-07-14T07:00:10.449709Z","shell.execute_reply.started":"2021-07-14T07:00:10.434761Z","shell.execute_reply":"2021-07-14T07:00:10.448589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = train_labels.MGMT_value ","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:10.451072Z","iopub.execute_input":"2021-07-14T07:00:10.451334Z","iopub.status.idle":"2021-07-14T07:00:10.460420Z","shell.execute_reply.started":"2021-07-14T07:00:10.451307Z","shell.execute_reply":"2021-07-14T07:00:10.459362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:10.461792Z","iopub.execute_input":"2021-07-14T07:00:10.462079Z","iopub.status.idle":"2021-07-14T07:00:10.474592Z","shell.execute_reply.started":"2021-07-14T07:00:10.462052Z","shell.execute_reply":"2021-07-14T07:00:10.473512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_labels.drop(['BraTS21ID', 'MGMT_value'], axis='columns')\ny = label","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:10.475840Z","iopub.execute_input":"2021-07-14T07:00:10.476109Z","iopub.status.idle":"2021-07-14T07:00:10.483916Z","shell.execute_reply.started":"2021-07-14T07:00:10.476083Z","shell.execute_reply":"2021-07-14T07:00:10.483011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape, y.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:10.485590Z","iopub.execute_input":"2021-07-14T07:00:10.486124Z","iopub.status.idle":"2021-07-14T07:00:10.497052Z","shell.execute_reply.started":"2021-07-14T07:00:10.486079Z","shell.execute_reply":"2021-07-14T07:00:10.496164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nmodel = RandomForestClassifier()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:10.498194Z","iopub.execute_input":"2021-07-14T07:00:10.498475Z","iopub.status.idle":"2021-07-14T07:00:10.506381Z","shell.execute_reply.started":"2021-07-14T07:00:10.498433Z","shell.execute_reply":"2021-07-14T07:00:10.505536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X, y)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:00:10.507578Z","iopub.execute_input":"2021-07-14T07:00:10.508054Z","iopub.status.idle":"2021-07-14T07:00:10.700850Z","shell.execute_reply.started":"2021-07-14T07:00:10.508007Z","shell.execute_reply":"2021-07-14T07:00:10.699783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Thanks for reading this far. If you have any suggestions for further tips to add, feel free to comment below.","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}