{"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 matplotlib.pyplot as plt \nimport os, glob, random, cv2, glob, pydicom\n","metadata":{"execution":{"iopub.status.busy":"2021-08-03T18:55:17.237755Z","iopub.execute_input":"2021-08-03T18:55:17.238096Z","iopub.status.idle":"2021-08-03T18:55:17.712238Z","shell.execute_reply.started":"2021-08-03T18:55:17.238064Z","shell.execute_reply":"2021-08-03T18:55:17.711217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndirectory = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train'\nvox = [\"FLAIR\",\"T1w\", \"T1wCE\", \"T2w\"]\n ","metadata":{"execution":{"iopub.status.busy":"2021-08-03T20:16:13.108659Z","iopub.execute_input":"2021-08-03T20:16:13.109012Z","iopub.status.idle":"2021-08-03T20:16:13.114608Z","shell.execute_reply.started":"2021-08-03T20:16:13.108979Z","shell.execute_reply":"2021-08-03T20:16:13.113458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Determining Planes","metadata":{}},{"cell_type":"code","source":"# Get a list of images in a series\ndef get_series_list(case, series_name):\n    series_list = []\n    for subdirs, dirs, files in os.walk(directory + '/' + case + \"/\" + series_name):\n        series_list = os.listdir(directory + '/' + case + '/' + series_name)        \n    return series_list","metadata":{"execution":{"iopub.status.busy":"2021-08-03T20:16:16.100703Z","iopub.execute_input":"2021-08-03T20:16:16.101094Z","iopub.status.idle":"2021-08-03T20:16:16.106378Z","shell.execute_reply.started":"2021-08-03T20:16:16.101063Z","shell.execute_reply":"2021-08-03T20:16:16.105062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Find the image in the middle of each series stack, returns a list of four images\ndef get_middle_images(case):\n    \n    middle_images = []\n    \n    # Iterate through each of the four series directories and get the files \n    for ser in vox:\n        series_files = get_series_list(case, ser)\n        series_df = pd.DataFrame(columns = ['image','instance_number'])\n\n        # Get the DICOM InstanceNumber tag to order the images since we can't rely on the filenames to be in order\n        for s in series_files:\n            img = pydicom.dcmread(directory + \"/\" + case + \"/\" + ser + \"/\" + s)\n            series_df.loc[len(series_df.index)] = [s, img[0x0020,0x0013].value] \n        series_df['instance_number'] = pd.to_numeric(series_df['instance_number'])\n\n        # Sort the image list by InstanceNumber\n        series_df = series_df.sort_values(by=['instance_number'])\n        \n        # Find the image in the middle of the list\n        middle_index = int(series_df.shape[0] / 2)\n        middle_image = series_df.iloc[middle_index]['image']\n\n        middle_images.append(ser + \"/\" + middle_image)\n\n    return middle_images","metadata":{"execution":{"iopub.status.busy":"2021-08-03T20:16:20.084385Z","iopub.execute_input":"2021-08-03T20:16:20.084711Z","iopub.status.idle":"2021-08-03T20:16:20.092173Z","shell.execute_reply.started":"2021-08-03T20:16:20.084680Z","shell.execute_reply":"2021-08-03T20:16:20.090924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert the Image Orientation Patient tag cosine values into a text string of the plane.\n# This represents the plane the image is 'closest to' .. it does not explain any obliqueness\ndef get_image_plane(loc):\n    row_x = round(loc[0])\n    row_y = round(loc[1])\n    row_z = round(loc[2])\n    col_x = round(loc[3])\n    col_y = round(loc[4])\n    col_z = round(loc[5])\n    if row_x == 1 and row_y == 0 and col_x == 0 and col_y == 0:\n        return \"Coronal\"\n    if row_x == 0 and row_y == 1 and col_x == 0 and col_y == 0:\n        return \"Sagittal\"\n    if row_x == 1 and row_y == 0 and col_x == 0 and col_y == 1:\n        return \"Axial\"\n    return \"Unknown\"","metadata":{"execution":{"iopub.status.busy":"2021-08-03T20:16:29.696025Z","iopub.execute_input":"2021-08-03T20:16:29.696347Z","iopub.status.idle":"2021-08-03T20:16:29.703337Z","shell.execute_reply.started":"2021-08-03T20:16:29.696318Z","shell.execute_reply":"2021-08-03T20:16:29.702086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display images and their plane\n#def plane_images(dataset,case):\n        \ncase1=[]\nflair,j_flair=[],[]  \nt1w,j_t1w=[],[]    \nt1wce,j_t1wce=[],[]   \nt2w,j_t2w=[],[]    \n#note = []\n\ndf ={}\n#dataset=\"train\"\n\ncases = os.listdir(directory)\nfor case in cases:\n      \n    for img in get_middle_images(case):\n        \n        baca=directory + \"/\" + case \n        image = pydicom.dcmread(baca+ \"/\" + img)\n        ser = img.split(\"/\")\n        image_orientation_patient = image[0x0020,0x0037]        \n        plane = get_image_plane(image_orientation_patient)\n\n        if ser[0]==\"FLAIR\":\n            flair.append(plane)   #+'('+str(len(os.listdir(baca+ \"/FLAIR\")))+')')\n            j_flair.append(len(os.listdir(baca+ \"/FLAIR\")))\n        if ser[0]==\"T1w\":\n            t1w.append(plane)   #+'('+str(len(os.listdir(baca+ \"/T1w\")))+')')\n            j_t1w.append(len(os.listdir(baca+ \"/T1w\")))\n        if ser[0]==\"T1wCE\":\n            t1wce.append(plane)   #+'('+str(len(os.listdir(baca+ \"/T1wCE\")))+')')\n            j_t1wce.append(len(os.listdir(baca+ \"/T1wCE\")))\n        if ser[0]==\"T2w\":\n            t2w.append(plane)   #+'('+str(len(os.listdir(baca+ \"/T2w\")))+')' )            \n            j_t2w.append(len(os.listdir(baca+ \"/T2w\")))\n                       \n    case1.append(case)   #+'('+str(len(cases))+')')\n    #note.append(img)\n    \n    df = pd.DataFrame.from_dict({        \n        \"case1\" : case1,\n        #\"0\" : jum_case1,\n        \n        \"flair\" : flair,\n        \"j_flair\" : j_flair,\n        \n        \"t1w\" : t1w,\n        \"j_t1w\" : j_t1w,\n        \n        \"t1wCE\" : t1wce,\n        \"j_t1wce\" : j_t1wce,\n        \n        \"t2w\" : t2w,\n        \"j_t2w\" : j_t2w\n        \n        #\"Note\" : note\n    })\n    #return df    \nprint(df)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T19:01:40.343369Z","iopub.execute_input":"2021-08-03T19:01:40.343695Z","iopub.status.idle":"2021-08-03T19:54:24.777792Z","shell.execute_reply.started":"2021-08-03T19:01:40.343665Z","shell.execute_reply":"2021-08-03T19:54:24.775928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df.to_excel(\"PreprocessDataMRI-1.xlsx\", sheet_name=\"data\", index=False)\n# df.to_csv(\"PreprocessDataMRI-1.csv\", index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_labels_dir = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv'\ndf_train_labels = pd.read_csv(df_train_labels_dir) \n\n#df_train_labels = df_train_labels.rename({'BraTS21ID': 'case1'}, axis=1)\ndf_train_labels['BraTS21ID'] = df_train_labels['BraTS21ID'].apply(lambda x: '{0:0>5}'.format(x))\nprint(len(df_train_labels))    \ndf_train_labels.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-03T20:16:57.793355Z","iopub.execute_input":"2021-08-03T20:16:57.793718Z","iopub.status.idle":"2021-08-03T20:16:57.815408Z","shell.execute_reply.started":"2021-08-03T20:16:57.793686Z","shell.execute_reply":"2021-08-03T20:16:57.814654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df_train=pd.read_csv('PreprocessDataMRI-0.csv')\ndf_train=df\n\ndf_train = df_train.rename({'case1':'BraTS21ID','flair': 'FLAIR','t1w':'T1w','t1wCE':'T1wCE','t2w':'T2w'}, axis=1)\ndf_train = df_train.rename({'j_flair': 'j_FLAIR','j_t1w':'j_T1w','j_t1wce':'j_T1wCE','j_t2w':'j_T2w'}, axis=1)\ndf_train['BraTS21ID'] = df_train['BraTS21ID'].apply(lambda x: '{0:0>5}'.format(x))\ndf_train['path_BraTS21ID']=directory+'/'+df_train['BraTS21ID'] \ndf_train2 = pd.concat([df_train, df_train_labels['MGMT_value']], axis=1)\ndf_train2[0:10]","metadata":{"execution":{"iopub.status.busy":"2021-08-03T20:17:12.903392Z","iopub.execute_input":"2021-08-03T20:17:12.903734Z","iopub.status.idle":"2021-08-03T20:17:12.934330Z","shell.execute_reply.started":"2021-08-03T20:17:12.903704Z","shell.execute_reply":"2021-08-03T20:17:12.933112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Adjust contrast and brightness","metadata":{"execution":{"iopub.status.busy":"2021-08-03T20:01:22.999580Z","iopub.execute_input":"2021-08-03T20:01:22.999913Z","iopub.status.idle":"2021-08-03T20:01:23.004235Z","shell.execute_reply.started":"2021-08-03T20:01:22.999882Z","shell.execute_reply":"2021-08-03T20:01:23.003086Z"}}},{"cell_type":"code","source":"# Make a simple linear VOI LUT from the raw (stored) pixel data\ndef make_lut(storedPixels, windowWidth, windowLevel, p_i):\n    \n    # Slope and Intercept set to 1 and 0 for MR. Get these from DICOM tags instead if using \n    # on a modality that requires them (CT, PT etc)\n    slope = 1.0\n    intercept = 0.0\n    minPixel = int(np.amin(storedPixels))\n    maxPixel = int(np.amax(storedPixels))\n\n    # Make an empty array for the LUT the size of the pixel 'width' in the raw pixel data\n    lut = [0] * (maxPixel + 1)\n    \n    # Invert pixels and windowLevel for MONOCHROME1. We invert the specified windowLevel so that \n    # increasing the level value makes the images brighter regardless of photometric intrepretation\n    invert = False\n    if p_i == \"MONOCHROME1\":\n        invert = True\n    else:\n        windowLevel = (maxPixel - minPixel) - windowLevel\n        \n    # Loop through the pixels and calculate each LUT value\n    for storedValue in range(minPixel, maxPixel):\n        modalityLutValue = storedValue * slope + intercept\n        voiLutValue = (((modalityLutValue - windowLevel) / windowWidth + 0.5) * 255.0)\n        clampedValue = min(max(voiLutValue, 0), 255)\n        if invert:\n            lut[storedValue] = round(255-clampedValue)\n        else:\n            lut[storedValue] = round(clampedValue)\n        \n    return lut","metadata":{"execution":{"iopub.status.busy":"2021-08-03T20:17:27.400714Z","iopub.execute_input":"2021-08-03T20:17:27.401086Z","iopub.status.idle":"2021-08-03T20:17:27.409496Z","shell.execute_reply.started":"2021-08-03T20:17:27.401055Z","shell.execute_reply":"2021-08-03T20:17:27.408387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Apply the LUT to a pixel array\ndef apply_lut(pixels_in, lut):\n    \n    pixels_in = pixels_in.flatten()\n    pixels_out = [0] * len(pixels_in)\n    \n    for i in range(0, len(pixels_in)):\n        pixel = pixels_in[i]\n        pixels_out[i] = int(lut[pixel])\n        \n    return pixels_out","metadata":{"execution":{"iopub.status.busy":"2021-08-03T20:17:34.221342Z","iopub.execute_input":"2021-08-03T20:17:34.221671Z","iopub.status.idle":"2021-08-03T20:17:34.226994Z","shell.execute_reply.started":"2021-08-03T20:17:34.221640Z","shell.execute_reply":"2021-08-03T20:17:34.225918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pasient=os.listdir(directory)[0]\n\n#vox = [\"FLAIR\",\"T1w\", \"T1wCE\", \"T2W\"]\ndef gambar(baris,kolom):  \n    t=directory+'/'+pasient+'/'+vox[kolom]\n    t_paths = sorted(os.listdir(t), \n            key=lambda x: int(x[:-4].split(\"-\")[-1]),)   \n    mid =len(os.listdir(directory+'/'+pasient+'/'+vox[kolom]))//2\n    t_paths_=t_paths[mid-((nbaris+2)//2):mid+((nbaris+2)//2)]  \n    t_paths_ = t_paths_[baris]\n    return t_paths_\n\nwindow_width = 300    # 1000,600,300,  1500,1000, 700\nwindow_level = 900     #  900,900,900,  2000,2000,2000\n\nnbaris=10\n\nfig,axs = plt.subplots(nbaris,4,figsize=(4*10,nbaris*10))\nfig.subplots_adjust(top=.96)\nfig.suptitle('pasient = '+pasient, fontsize=60, fontweight='bold')\n\nfor kolom in range (4):\n    for baris in range(nbaris):     \n        gam = pydicom.read_file(directory+'/'+pasient+'/'+vox[kolom]+'/'+gambar(baris,kolom))   #.pixel_array \n        \n        lut = make_lut(gam.pixel_array, window_width, window_level, gam.PhotometricInterpretation)\n        gam = np.reshape(apply_lut(gam.pixel_array, lut), (gam.pixel_array.shape[0],gam.pixel_array.shape[1]))\n        \n        plane=df_train2[df_train2['BraTS21ID'] == pasient][vox[kolom]].values[0]\n        \n        axs[baris, kolom].imshow(gam,cmap=\"viridis\")   #cmap='gray')   #cmap=\"viridis\") \n        axs[baris, kolom].set_title(vox[kolom]+'-'+plane+' ('+gambar(baris,kolom)+')',fontsize=26)","metadata":{"execution":{"iopub.status.busy":"2021-08-03T20:19:22.338791Z","iopub.execute_input":"2021-08-03T20:19:22.339159Z","iopub.status.idle":"2021-08-03T20:19:36.863072Z","shell.execute_reply.started":"2021-08-03T20:19:22.339128Z","shell.execute_reply":"2021-08-03T20:19:36.861952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pasient=os.listdir(directory)[1]\n\n\ndef gambar(baris,kolom):  \n    t=directory+'/'+pasient+'/'+vox[kolom]\n    t_paths = sorted(os.listdir(t), \n            key=lambda x: int(x[:-4].split(\"-\")[-1]),)   \n    mid =len(os.listdir(directory+'/'+pasient+'/'+vox[kolom]))//2\n    t_paths_=t_paths[mid-((nbaris+2)//2):mid+((nbaris+2)//2)]  \n    t_paths_ = t_paths_[baris]\n    return t_paths_\n\nnbaris=7\n\nfig,axs = plt.subplots(nbaris,4,figsize=(4*10,nbaris*10))\nfig.subplots_adjust(top=.96)\nfig.suptitle('pasient = '+pasient, fontsize=60, fontweight='bold')\n\nfor kolom in range (4):\n    for baris in range(nbaris):     \n        gam = pydicom.read_file(directory+'/'+pasient+'/'+vox[kolom]+'/'+gambar(baris,kolom)).pixel_array           \n        axs[baris, kolom].imshow(gam,cmap=\"viridis\") \n        axs[baris, kolom].set_title(vox[kolom]+' ('+gambar(baris,kolom)+')',fontsize=26)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-03T14:49:38.711324Z","iopub.execute_input":"2021-08-03T14:49:38.71156Z","iopub.status.idle":"2021-08-03T14:49:44.708372Z","shell.execute_reply.started":"2021-08-03T14:49:38.711538Z","shell.execute_reply":"2021-08-03T14:49:44.707376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}