{"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":"<font size = '5'> **Data visualization and extraction of middle images from all four scan types for classification.** </font>\n\nNotes:\n\nExclude the data from subjects [00109, 00123, 00790]\n","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport pydicom as dicom\nimport cv2\nimport ast\nfrom glob import glob\nimport warnings\nfrom collections import Counter\nimport seaborn as sns\nfrom scipy import ndimage, misc\nimport pydicom\nwarnings.filterwarnings('ignore')\n\nimport re\nimport math\n\nimport tensorflow as tf\n\nimport imageio\nimport ipywidgets as widgets\nfrom PIL import Image","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-10T23:21:25.272156Z","iopub.execute_input":"2021-08-10T23:21:25.272555Z","iopub.status.idle":"2021-08-10T23:21:25.279361Z","shell.execute_reply.started":"2021-08-10T23:21:25.272502Z","shell.execute_reply":"2021-08-10T23:21:25.27814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#get train path\npath = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/'\n\n#create train path\ntrain_path = os.path.join(path, 'train')\n\n#list all the files in train path\nsubject_ids = os.listdir(train_path)\n\n#get names of scan types\nscan_types = os.listdir(train_path + '/' + str(subject_ids[0]))\n\n#print types of scans\nprint(scan_types, '\\n')\n\n#get train labels\n#check the train labels csv file\ntrain_labels = pd.read_csv(path + 'train_labels.csv')\n\ntrain_labels.head(5)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-10T23:21:26.481841Z","iopub.execute_input":"2021-08-10T23:21:26.482232Z","iopub.status.idle":"2021-08-10T23:21:26.502721Z","shell.execute_reply.started":"2021-08-10T23:21:26.482199Z","shell.execute_reply":"2021-08-10T23:21:26.501961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#get subject id's and get rid of those three subjects\nsubject_rid = ['00109', '00123', '00790']\n\nfor subject_id in subject_rid:\n    \n    if subject_id in subject_ids:\n        subject_ids.remove(subject_id)\n\nnum_of_sub = len(subject_ids)\n        \n#number of subjects\nprint('Number of subjects after elimination: {}'.format(num_of_sub))","metadata":{"execution":{"iopub.status.busy":"2021-08-10T23:21:27.421373Z","iopub.execute_input":"2021-08-10T23:21:27.421768Z","iopub.status.idle":"2021-08-10T23:21:27.427935Z","shell.execute_reply.started":"2021-08-10T23:21:27.421735Z","shell.execute_reply":"2021-08-10T23:21:27.426908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#this function takes the file list inside FLAIR of each subject and returns the number for each file image. (ex. 'Image-4.dcm' returns 4). This helps to get\n# the image scans in the middle \n\ndef file_nums(file_list):\n    \n    nums = []\n    image_str = []\n    \n    for f in file_list:\n        \n        name = f.replace('Image-', '')\n        name = name.replace('.dcm', '')\n        nums.append(int(name))\n    \n    nums = np.sort(nums)\n    \n    for num in nums:\n        \n        name = 'Image-' + str(num) + '.dcm'\n        image_str.append(name)\n    \n    return nums, image_str\n\n#a function to just pad images to 512 by 512. If the image is small this function will paste the image in a black 512 by 512 image\n\ndef pad_image(image):\n        \n    #check if the image is bigger than 512 x 512    \n    if (image.shape[0] > 512) or (image.shape[1] > 512):\n        \n        #if true resize the image to 512 x 512\n        image = tf.image.resize(image, (512, 512))\n        \n    #get shape of the image\n    rows, cols, _ = image.shape   \n    \n    #padding image size\n    final_im = np.zeros([512, 512, 1])\n    \n    #paste image\n    paste_row = int((512 - rows) / 2)\n    paste_col = int((512 - cols) / 2)\n    \n    final_im[paste_row:paste_row + rows, paste_col:paste_col + cols, :] = image\n    \n    return final_im","metadata":{"execution":{"iopub.status.busy":"2021-08-10T23:21:28.115755Z","iopub.execute_input":"2021-08-10T23:21:28.116077Z","iopub.status.idle":"2021-08-10T23:21:28.124293Z","shell.execute_reply.started":"2021-08-10T23:21:28.116048Z","shell.execute_reply":"2021-08-10T23:21:28.123259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<font size = '3'> **Interactive slides for Flair images** </font>","metadata":{}},{"cell_type":"code","source":"#check the image slices from one subject\n\n#flair scans from subject 0\nsub_path = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/FLAIR'\n\n#list all the files inside\nimage_list = os.listdir(sub_path)\nnums, file_names = file_nums(image_list)\n\ndef show_flair(Slide):\n\n    dcom = pydicom.read_file(sub_path + '/' + file_names[Slide])\n    im = dcom.pixel_array\n    \n    plt.figure(figsize = (6, 6))\n    plt.title('Flair')\n    plt.imshow(im, cmap = 'gray')\n    plt.xticks([])\n    plt.yticks([])\n    plt.show()\n    \nwidgets.interact(show_flair, Slide = (0, len(nums) - 1 , 1))\n\n#append images to save as gif\nims = []\n\n# #save as a gif\n# for file in file_names:\n    \n#     dcom = pydicom.read_file(sub_path + '/' + file)\n#     im = dcom.pixel_array\n#     #im = imageio.imread(im)\n#     ims.append(im.astype('uint8'))\n\n# imgs = [Image.fromarray(img) for img in ims]    \n# imgs[0].save(\"flair.gif\", save_all=True, append_images=imgs[1:], duration=50, loop=0)\n    ","metadata":{"execution":{"iopub.status.busy":"2021-08-10T23:21:30.736649Z","iopub.execute_input":"2021-08-10T23:21:30.736977Z","iopub.status.idle":"2021-08-10T23:21:32.630861Z","shell.execute_reply.started":"2021-08-10T23:21:30.736948Z","shell.execute_reply":"2021-08-10T23:21:32.629801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<font size = '3'> **Interactive images for T1W type** </font>","metadata":{}},{"cell_type":"code","source":"#check the image slices from one subject\n\n#flair scans from subject 0\nsub_path = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1w'\n\n#list all the files inside\nimage_list = os.listdir(sub_path)\nnums, file_names = file_nums(image_list)\n\ndef show_t1w(Slide):\n\n    dcom = pydicom.read_file(sub_path + '/' + file_names[Slide])\n    im = dcom.pixel_array\n    \n    plt.figure(figsize = (6, 6))\n    plt.title('T1W')\n    plt.imshow(im, cmap = 'gray')\n    plt.xticks([])\n    plt.yticks([])\n    plt.show()\n    \nwidgets.interact(show_t1w, Slide = (0, len(nums) - 1 , 1))\n\n#save as a gif\nfor file in file_names:\n    \n    dcom = pydicom.read_file(sub_path + '/' + file)\n    im = dcom.pixel_array\n    #im = imageio.imread(im)\n    ims.append(im.astype('uint8'))\n\nimgs = [Image.fromarray(img) for img in ims]    \nimgs[0].save(\"t1w.gif\", save_all=True, append_images=imgs[1:], duration=150, loop=0)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T23:21:32.635957Z","iopub.execute_input":"2021-08-10T23:21:32.636304Z","iopub.status.idle":"2021-08-10T23:21:33.897489Z","shell.execute_reply.started":"2021-08-10T23:21:32.636268Z","shell.execute_reply":"2021-08-10T23:21:33.896345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<font size = '3'> **Interactive for T2W type** </font>","metadata":{}},{"cell_type":"code","source":"#check the image slices from one subject\n\n#flair scans from subject 0\nsub_path = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T2w'\n\n#list all the files inside\nimage_list = os.listdir(sub_path)\nnums, file_names = file_nums(image_list)\n\ndef show_T2W(Slide):\n\n    dcom = pydicom.read_file(sub_path + '/' + file_names[Slide])\n    im = dcom.pixel_array\n    \n    plt.figure(figsize = (6, 6))\n    plt.title('T2W')\n    plt.imshow(im, cmap = 'gray')\n    plt.xticks([])\n    plt.yticks([])\n    plt.show()\n    \nwidgets.interact(show_T2W, Slide = (0, len(nums) - 1 , 1))","metadata":{"execution":{"iopub.status.busy":"2021-08-10T23:21:34.555803Z","iopub.execute_input":"2021-08-10T23:21:34.556115Z","iopub.status.idle":"2021-08-10T23:21:34.681501Z","shell.execute_reply.started":"2021-08-10T23:21:34.556087Z","shell.execute_reply":"2021-08-10T23:21:34.680496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<font size = '3'> **Interactive T1wCE images** </font>","metadata":{}},{"cell_type":"code","source":"#check the image slices from one subject\n\n#flair scans from subject 0\nsub_path = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1wCE'\n\n#list all the files inside\nimage_list = os.listdir(sub_path)\nnums, file_names = file_nums(image_list)\n\ndef show_T1wCE(Slide):\n\n    dcom = pydicom.read_file(sub_path + '/' + file_names[Slide])\n    im = dcom.pixel_array\n    \n    plt.figure(figsize = (6, 6))\n    plt.title('T1wCE')\n    plt.imshow(im, cmap = 'gray')\n    plt.xticks([])\n    plt.yticks([])\n    plt.show()\n    \nwidgets.interact(show_T1wCE, Slide = (0, len(nums) - 1 , 1))","metadata":{"execution":{"iopub.status.busy":"2021-08-10T23:21:37.003155Z","iopub.execute_input":"2021-08-10T23:21:37.003523Z","iopub.status.idle":"2021-08-10T23:21:37.137359Z","shell.execute_reply.started":"2021-08-10T23:21:37.003488Z","shell.execute_reply":"2021-08-10T23:21:37.136288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\ntick = time.time()\n\n#create an empty list for the image data and the labels\nX_flair = np.zeros([num_of_sub, 512, 512, 1]) #image length and the number of pictures\nX_t1w = np.zeros([num_of_sub, 512, 512, 1])\nX_t2w = np.zeros([num_of_sub, 512, 512, 1])\nX_t1wce = np.zeros([num_of_sub, 512, 512, 1])\nY = np.zeros(num_of_sub)\n\n#iterate through subject number and label in train_labels dataframe\nfor i in range(len(scan_types)):\n\n    for k, s in enumerate(subject_ids):\n\n        #get the directory for the subject\n        directory = train_path + '/' +  s + '/' + scan_types[i]\n\n        #get the file_list\n        file_list = os.listdir(directory)\n        file_number,_ = file_nums(file_list)\n        \n        #get the middle image \n        ind = len(file_number)// 2\n        sub_id = file_number[ind]\n\n        file_name = 'Image-' + str(sub_id) + '.dcm'\n        \n        #create directory\n        file_dir = directory + '/' + file_name\n        \n        #read dicom\n        dicom = pydicom.read_file(file_dir)\n        \n        #get the numpy array\n        image = dicom.pixel_array\n        \n        #reshape the image to 3 dims\n        image = image.reshape(image.shape[0], image.shape[1], 1)\n     \n        #check the size and pad the image to 512 and 512\n        if image.shape != (512, 512, 1):\n            \n            #pad image\n            image = pad_image(image)\n\n        #normalize the image\n        image_norm = (image - np.mean(image)) / np.std(image)\n        \n        if scan_types[i] == 'FLAIR':\n            \n            X_flair[k, :, :, :] = image_norm\n            Y[k] = train_labels['MGMT_value'][train_labels['BraTS21ID'] == int(s)].values[0]\n        \n        elif scan_types[i] == 'T2w':\n            \n            X_t2w[k, :, :, :] = image_norm\n        \n        elif scan_types[i] == 'T1w':\n            \n            X_t1w[k, :, :, :] = image_norm\n        \n        elif scan_types[i] == 'T1wCE':\n            \n            X_t1wce[k, :, :, :] = image_norm\n\ntock = time.time()\n\nprint(tock - tick)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T23:21:37.910362Z","iopub.execute_input":"2021-08-10T23:21:37.910873Z","iopub.status.idle":"2021-08-10T23:21:51.654389Z","shell.execute_reply.started":"2021-08-10T23:21:37.910837Z","shell.execute_reply":"2021-08-10T23:21:51.652907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot some of the pictures to see\nrand_inds = np.random.choice(np.arange(len(X_flair)), 10)\n\nfig, ax = plt.subplots(len(rand_inds), 4, figsize = (10, 20))\n\nfor i, r in enumerate(rand_inds):\n    \n    ax[i, 0].imshow(X_flair[r], cmap = 'gray')\n    ax[i, 0].set_xticks([])\n    ax[i, 0].set_yticks([])\n    ax[i, 0].set_title('Flair - {}'.format(Y[r]))\n    \n    ax[i, 1].imshow(X_t1wce[r], cmap = 'gray')\n    ax[i, 1].set_xticks([])\n    ax[i, 1].set_yticks([])\n    ax[i, 1].set_title('T1WCE - {}'.format(Y[r]))\n    \n    ax[i, 2].imshow(X_t1w[r], cmap = 'gray')\n    ax[i, 2].set_xticks([])\n    ax[i, 2].set_yticks([])\n    ax[i, 2].set_title('T1W - {}'.format(Y[r]))\n    \n    ax[i, 3].imshow(X_t2w[r], cmap = 'gray')\n    ax[i, 3].set_xticks([])\n    ax[i, 3].set_yticks([])\n    ax[i, 3].set_title('T2W - {}'.format(Y[r]))","metadata":{"execution":{"iopub.status.busy":"2021-08-10T23:21:51.655823Z","iopub.execute_input":"2021-08-10T23:21:51.656155Z","iopub.status.idle":"2021-08-10T23:21:54.574361Z","shell.execute_reply.started":"2021-08-10T23:21:51.656119Z","shell.execute_reply":"2021-08-10T23:21:54.573209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#save numpy files\nnp.save('x_flair', X_flair)\nnp.save('x_t1w', X_t1w)\nnp.save('x_t2w', X_t2w)\nnp.save('x_t1wce', X_t1wce)\nnp.save('y', Y)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T23:23:43.491295Z","iopub.execute_input":"2021-08-10T23:23:43.491639Z","iopub.status.idle":"2021-08-10T23:24:25.920016Z","shell.execute_reply.started":"2021-08-10T23:23:43.491607Z","shell.execute_reply":"2021-08-10T23:24:25.919142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}