{"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":"#Imports for analysing data\nimport numpy as np \nimport pandas as pd \nimport os\nimport glob\n\n\n#imports for displaying images\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as mpatches\nimport PIL\nfrom ipywidgets import interactive, interact, ToggleButtons,IntSlider\n\n#imports for building the model\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras.preprocessing.image import ImageDataGenerator\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:47:54.349639Z","iopub.execute_input":"2022-08-13T11:47:54.350268Z","iopub.status.idle":"2022-08-13T11:47:59.813520Z","shell.execute_reply.started":"2022-08-13T11:47:54.350174Z","shell.execute_reply":"2022-08-13T11:47:59.812553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"../input/uw-madison-gi-tract-image-segmentation/train.csv\")\ntrain_data\n","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:48:03.843807Z","iopub.execute_input":"2022-08-13T11:48:03.844534Z","iopub.status.idle":"2022-08-13T11:48:04.384345Z","shell.execute_reply.started":"2022-08-13T11:48:03.844499Z","shell.execute_reply":"2022-08-13T11:48:04.383617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_train = train_data\nfinal_train['ext'] = train_data['id']+'.png'\n\nfinal_train = pd.DataFrame(final_train)\nfinal_train","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:48:10.880461Z","iopub.execute_input":"2022-08-13T11:48:10.880743Z","iopub.status.idle":"2022-08-13T11:48:10.913499Z","shell.execute_reply.started":"2022-08-13T11:48:10.880706Z","shell.execute_reply":"2022-08-13T11:48:10.912665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating dataframe with large bowel segmentation values and corresponding id\n\nlb_df = train_data[(train_data['class']=='large_bowel')]\nlb_df = pd.DataFrame(lb_df)\nlb_df.rename(columns={'segmentation': 'lb_seg'}, inplace =True)\nlb_df = lb_df[['id','lb_seg']]\n\n\n#Creating dataframe with small bowel segmentation values and corresponding id\n\nsb_df = train_data[(train_data['class']=='small_bowel')]\nsb_df = pd.DataFrame(sb_df)\nsb_df.rename(columns={'segmentation': 'sb_seg'}, inplace =True)\nsb_df = sb_df[['id','sb_seg']]\n\n#Creating dataframe with stomach segmentation values and corresponding id\n\nstm_df = train_data[(train_data['class']=='stomach')]\nstm_df = pd.DataFrame(stm_df)\nstm_df.rename(columns={'segmentation': 'stm_seg'}, inplace =True)\nstm_df = stm_df[['id','stm_seg']]","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:48:22.747172Z","iopub.execute_input":"2022-08-13T11:48:22.747441Z","iopub.status.idle":"2022-08-13T11:48:22.812403Z","shell.execute_reply.started":"2022-08-13T11:48:22.747412Z","shell.execute_reply":"2022-08-13T11:48:22.811679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Merging the 3 created dataframes\n\nnew_train_df = lb_df\npd.DataFrame(new_train_df)\nnew_train_df = new_train_df.merge(sb_df, on='id')\nnew_train_df = new_train_df.merge(stm_df, on='id')\nnew_train_df\n","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:48:25.629448Z","iopub.execute_input":"2022-08-13T11:48:25.630050Z","iopub.status.idle":"2022-08-13T11:48:25.693296Z","shell.execute_reply.started":"2022-08-13T11:48:25.630014Z","shell.execute_reply":"2022-08-13T11:48:25.692586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_paths = glob.glob(\"../input/uw-madison-gi-tract-image-segmentation/train/*/*/*/*\")\n\ntemp_df = train_paths\ntemp_df = pd.DataFrame(temp_df,columns=['Path'])\ntemp_df['Path'][0]\n","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:48:28.798299Z","iopub.execute_input":"2022-08-13T11:48:28.798568Z","iopub.status.idle":"2022-08-13T11:48:43.044945Z","shell.execute_reply.started":"2022-08-13T11:48:28.798536Z","shell.execute_reply":"2022-08-13T11:48:43.044238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id_dict ={'id': temp_df['Path'].apply(lambda x: (x.split('/')[-3]) +'_slice_'+ (x.split('_')[-5])),\n          'Ht': temp_df['Path'].apply(lambda x: x.split('_')[-4]),'Wt': temp_df['Path'].apply(lambda x: x.split('_')[-3]),\n         'case': temp_df['Path'].apply(lambda x: x.split('/')[-4].replace('case','')),\n         'day': temp_df['Path'].apply(lambda x: (x.split('_')[-6]).split('/')[-3].replace('day','')),\n         'slice': temp_df['Path'].apply(lambda x: x.split('_')[-5]) }\ntemp_df['id'] = id_dict['id']\ntemp_df['Ht'] = id_dict['Ht']\ntemp_df['Wt'] = id_dict['Wt']\ntemp_df['case'] = id_dict['case']\ntemp_df['day'] = id_dict['day']\ntemp_df['slice'] = id_dict['slice']\ntemp_df[['id','case','day','slice','Ht','Wt','Path']]\ntrain_df = new_train_df.merge(temp_df, on='id')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:48:43.047933Z","iopub.execute_input":"2022-08-13T11:48:43.048626Z","iopub.status.idle":"2022-08-13T11:48:43.275431Z","shell.execute_reply.started":"2022-08-13T11:48:43.048588Z","shell.execute_reply":"2022-08-13T11:48:43.274674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.DataFrame(train_df)\n\n#Removing unreliable image datas\ndrop_list=[]\nfor i in range(len(train_df)):\n    if((train_df['case'][i]=='7' and train_df['day'][i]=='0') or (train_df['case'][i]=='81' and train_df['day'][i]=='30') ):\n        drop_list.append(i)\ntrain_df = train_df.drop(index= drop_list)\ntrain_df = train_df.reset_index(drop=True)\n\ntrain_df['lb_flag']= train_df[\"lb_seg\"].apply(lambda x: not pd.isna(x))\ntrain_df['sb_flag']= train_df[\"sb_seg\"].apply(lambda x: not pd.isna(x))\ntrain_df['stm_flag']= train_df[\"stm_seg\"].apply(lambda x: not pd.isna(x))\ntrain_df[\"n_segs\"] = train_df[\"lb_flag\"].astype(int)+train_df[\"sb_flag\"].astype(int)+train_df[\"stm_flag\"].astype(int)\ntrain_df = train_df[['id','case','day','slice','n_segs','lb_seg','lb_flag','sb_seg','sb_flag','stm_seg','stm_flag','Ht','Wt','Path']]\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:48:48.788164Z","iopub.execute_input":"2022-08-13T11:48:48.788425Z","iopub.status.idle":"2022-08-13T11:48:49.453938Z","shell.execute_reply.started":"2022-08-13T11:48:48.788397Z","shell.execute_reply":"2022-08-13T11:48:49.453232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Displaying Original Images**","metadata":{}},{"cell_type":"code","source":"def png_to_array(x):\n\n    img = PIL.Image.open(x).resize((220,220), PIL.Image.ANTIALIAS)\n    img_data = img.getdata()\n    arr = np.array(img_data).reshape(220,220)\n    arr = arr/255\n    return arr","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:48:58.232558Z","iopub.execute_input":"2022-08-13T11:48:58.232856Z","iopub.status.idle":"2022-08-13T11:48:58.237932Z","shell.execute_reply.started":"2022-08-13T11:48:58.232822Z","shell.execute_reply":"2022-08-13T11:48:58.236902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,15))    \nfor i in range(6):\n    j =np.random.randint(1,len(train_df['Path']))\n    plt.subplot(3,3,i+1)\n    plt.axis('Off')\n    plt.title(train_df['id'][j])\n    plt.imshow(png_to_array(train_df['Path'][j]),cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:49:00.400523Z","iopub.execute_input":"2022-08-13T11:49:00.400820Z","iopub.status.idle":"2022-08-13T11:49:01.023189Z","shell.execute_reply.started":"2022-08-13T11:49:00.400769Z","shell.execute_reply":"2022-08-13T11:49:01.022189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def interactive_path(case_no='123',day_no='20'):\n    display_paths=[]\n    display_id=[]\n    for i in range(len(train_df['id'])):\n        if (train_df['case'][i]==case_no and train_df['day'][i]==day_no):\n            display_paths.append(png_to_array(train_df['Path'][i]))\n            display_id.append(train_df['id'][i])\n    \n    display_arr=np.asarray(display_paths)\n    \n    def explore_3dimage(layer):\n        plt.figure(figsize=(10, 5))\n        plt.imshow(display_arr[layer,:, :], cmap='gray');\n        plt.title(display_id[layer])\n        plt.axis('off')\n        return layer\n\n    interact(explore_3dimage,layer=(0,display_arr.shape[0]-1))","metadata":{"execution":{"iopub.status.busy":"2022-08-13T12:23:41.693819Z","iopub.execute_input":"2022-08-13T12:23:41.694472Z","iopub.status.idle":"2022-08-13T12:23:41.701860Z","shell.execute_reply.started":"2022-08-13T12:23:41.694437Z","shell.execute_reply":"2022-08-13T12:23:41.700831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#case = input(\"Enter case number\")\n#day = input(\"Enter day\")\ninteractive_path()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T12:23:44.017356Z","iopub.execute_input":"2022-08-13T12:23:44.017633Z","iopub.status.idle":"2022-08-13T12:23:45.739263Z","shell.execute_reply.started":"2022-08-13T12:23:44.017600Z","shell.execute_reply":"2022-08-13T12:23:45.738597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Displaying Image With Mask**","metadata":{}},{"cell_type":"code","source":"def mask_maker(seg, ht, wt):\n    #seg --> segment coordinates \n    #ht --> height of corresponding image\n    #wt --> width of corresponding image\n    \n    seg_arr = np.asarray(seg.split(), dtype='int')\n    start_point = seg_arr[0::2] -1\n    length_point = seg_arr[1::2]\n\n    end_point = start_point + length_point\n\n    case_mask = np.zeros(int(ht)*int(wt), dtype=np.uint8)\n\n    for start, end in zip(start_point, end_point):\n            case_mask[start:end] = 1\n\n    case_mask = case_mask.reshape(int(wt), int(ht))\n    case_mask = case_mask\n    return case_mask\n        ","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:49:47.185528Z","iopub.execute_input":"2022-08-13T11:49:47.185862Z","iopub.status.idle":"2022-08-13T11:49:47.192124Z","shell.execute_reply.started":"2022-08-13T11:49:47.185826Z","shell.execute_reply":"2022-08-13T11:49:47.191377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def img_reader(file, ht, wt):\n    \n    #Reads the original image and reshapes it accordingly to fit the mask\n    \n    c_img = PIL.Image.open(file)\n    c_img_data = c_img.getdata()\n    \n    if (ht==wt):\n        c_arr = np.array(c_img_data).reshape(int(ht), int(wt))\n    else:\n         c_arr = np.array(c_img_data).reshape(int(wt), int(ht))\n  \n    return c_arr","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:49:51.578351Z","iopub.execute_input":"2022-08-13T11:49:51.578721Z","iopub.status.idle":"2022-08-13T11:49:51.586308Z","shell.execute_reply.started":"2022-08-13T11:49:51.578681Z","shell.execute_reply":"2022-08-13T11:49:51.585371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Dataframe containing id's that have all 3 segmentations available\n\nall_img_df = train_df[['id','case','day','lb_seg','sb_seg','stm_seg','Ht','Wt','Path']]\nall_img_df = pd.DataFrame(all_img_df.dropna()).reset_index(drop=True)\nall_img_df","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:49:56.056056Z","iopub.execute_input":"2022-08-13T11:49:56.056974Z","iopub.status.idle":"2022-08-13T11:49:56.106985Z","shell.execute_reply.started":"2022-08-13T11:49:56.056929Z","shell.execute_reply":"2022-08-13T11:49:56.106177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mask_trimer(seg,file,ht,wt,n):\n    mask = mask_maker(seg[n],ht[n],wt[n])\n    mask_color = mask.astype(np.float64)\n    mask_color[np.where(mask_color==0)]=np.nan\n    img = img_reader(file[n],ht[n],wt[n])\n    return mask_color,img\n","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:50:00.106227Z","iopub.execute_input":"2022-08-13T11:50:00.106520Z","iopub.status.idle":"2022-08-13T11:50:00.112418Z","shell.execute_reply.started":"2022-08-13T11:50:00.106489Z","shell.execute_reply":"2022-08-13T11:50:00.111383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Visualising each organ's mask individually\n\ndef visualiser_1():\n    fig = plt.figure()\n    plt.figure(figsize=(20,15)) \n    \n    a = 1\n    for i in range(3):\n        k = np.random.randint(0,len(all_img_df['id']))\n        print('Row',i+1,'ID=', all_img_df['id'][k],'\\n\\n')\n          \n        \n        for j in range(3):\n            plt.subplot(3,3,a)\n            plt.axis('Off') \n            \n            \n            if (j==0):#Large bowel\n                mask_color,img = mask_trimer(all_img_df['lb_seg'],all_img_df['Path'],all_img_df['Ht'],all_img_df['Wt'],k)\n                a+=1\n                plt.title(\"Large Bowel\")\n                plt.imshow(mask_color, interpolation='nearest',cmap='viridis',vmin=0,vmax=1)\n                plt.imshow(img,cmap='gray', alpha =0.7)    \n            \n            elif (j==1):#Small bowel\n                mask_color,img = mask_trimer(all_img_df['sb_seg'],all_img_df['Path'],all_img_df['Ht'],all_img_df['Wt'],k)\n                a+=1\n                plt.title(\"Small Bowel\")\n                plt.imshow(mask_color, interpolation='nearest', cmap ='rainbow', vmin=0,vmax=1)\n                plt.imshow(img,cmap='gray', alpha =0.8)\n            \n            elif(j==2):#Stomach\n                mask_color,img = mask_trimer(all_img_df['stm_seg'],all_img_df['Path'],all_img_df['Ht'],all_img_df['Wt'],k)\n                a+=1\n                plt.title(\"Stomach\")\n                plt.imshow(mask_color, interpolation='nearest', cmap ='rainbow_r',vmin=0,vmax=1)\n                plt.imshow(img,cmap='gray', alpha =0.8)\n       \n\n        \n\n   \n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:50:07.495588Z","iopub.execute_input":"2022-08-13T11:50:07.495885Z","iopub.status.idle":"2022-08-13T11:50:07.506013Z","shell.execute_reply.started":"2022-08-13T11:50:07.495854Z","shell.execute_reply":"2022-08-13T11:50:07.505147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Each row represents 'one' case ID\\n\\n\")\nvisualiser_1()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:50:10.332910Z","iopub.execute_input":"2022-08-13T11:50:10.333372Z","iopub.status.idle":"2022-08-13T11:50:11.157339Z","shell.execute_reply.started":"2022-08-13T11:50:10.333338Z","shell.execute_reply":"2022-08-13T11:50:11.156470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef visualiser_2():\n    fig = plt.figure()\n    plt.figure(figsize=(20,15)) \n    a = 1\n    \n    labels = ['lb','sb','stm']\n    for i in range(3):\n        k = np.random.randint(0,len(all_img_df['id']))   \n        orig_img = img_reader(all_img_df['Path'][k],all_img_df['Ht'][k],all_img_df['Wt'][k])\n        plt.subplot(1,3,i+1)\n        plt.imshow(orig_img,cmap='gray', alpha =1) \n        \n        for j in range(3):\n            plt.subplot(1,3,a)\n            plt.axis('Off') \n            \n            if(j==0):#printing Large bowel seg mask\n                mask_color,img = mask_trimer(all_img_df['lb_seg'],all_img_df['Path'],all_img_df['Ht'],all_img_df['Wt'],k)\n                plt.imshow(mask_color, interpolation='nearest',cmap='viridis',vmin=0,vmax=1)\n                plt.title(all_img_df['id'][k])\n               \n                \n            elif(j==1):#printing Small bowel seg mask\n                mask_color,img = mask_trimer(all_img_df['sb_seg'],all_img_df['Path'],all_img_df['Ht'],all_img_df['Wt'],k)\n                plt.imshow(mask_color, interpolation='nearest', cmap = 'rainbow', vmin=0,vmax=1)\n                \n            elif(j==2):#printing Stomach seg mask\n                mask_color,img = mask_trimer(all_img_df['stm_seg'],all_img_df['Path'],all_img_df['Ht'],all_img_df['Wt'],k)\n                red_patch = mpatches.Patch(color='red', label='small bowel')\n                yel_patch = mpatches.Patch(color='yellow', label='large bowel')\n                pur_patch = mpatches.Patch(color='purple', label='stomach')\n                plt.legend(handles=[yel_patch,red_patch,pur_patch])\n                plt.imshow(mask_color, interpolation='nearest', cmap = 'rainbow_r',vmin=0,vmax=1)\n               \n        a+=1\n","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:50:20.273012Z","iopub.execute_input":"2022-08-13T11:50:20.273525Z","iopub.status.idle":"2022-08-13T11:50:20.286071Z","shell.execute_reply.started":"2022-08-13T11:50:20.273488Z","shell.execute_reply":"2022-08-13T11:50:20.285269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualiser_2()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:50:22.394144Z","iopub.execute_input":"2022-08-13T11:50:22.394436Z","iopub.status.idle":"2022-08-13T11:50:23.081174Z","shell.execute_reply.started":"2022-08-13T11:50:22.394405Z","shell.execute_reply":"2022-08-13T11:50:23.079724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Region-wise segmentation interactive view**","metadata":{}},{"cell_type":"code","source":"def days(case_no):\n    day_list =[]\n    for i in range(len(all_img_df['id'])):\n        if(all_img_df['case'][i]== case_no):\n            day_list.append(all_img_df['day'][i])\n    k = np.random.randint(0,len(day_list))\n    return day_list[k]","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:50:29.546542Z","iopub.execute_input":"2022-08-13T11:50:29.546839Z","iopub.status.idle":"2022-08-13T11:50:29.551813Z","shell.execute_reply.started":"2022-08-13T11:50:29.546804Z","shell.execute_reply":"2022-08-13T11:50:29.551029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"case_list = list(all_img_df['case'].unique())\n\ndef interactive_path(region):\n    k = np.random.randint(0,len(case_list))\n    case = case_list[k]\n    day = days(case)\n    \n    if(region=='All'):\n        \n        mask_lb=[]\n        mask_sb=[]\n        mask_stm=[]\n        img_l=[]\n        id_l=[]\n        for i in range(len(all_img_df['id'])):\n            if (all_img_df['case'][i]==case and all_img_df['day'][i]==day):\n                mask_color_lb,img = mask_trimer(all_img_df['lb_seg'],all_img_df['Path'],all_img_df['Ht'],all_img_df['Wt'],i)\n                mask_lb.append(mask_color_lb)\n                img_l.append(img)\n                \n                mask_color_sb,img = mask_trimer(all_img_df['sb_seg'],all_img_df['Path'],all_img_df['Ht'],all_img_df['Wt'],i)\n                mask_sb.append(mask_color_sb)\n                \n                  \n                mask_color_stm,img = mask_trimer(all_img_df['stm_seg'],all_img_df['Path'],all_img_df['Ht'],all_img_df['Wt'],i)\n                mask_stm.append(mask_color_stm)\n                id_l.append(all_img_df['id'][i])\n                \n        img_arr = np.asarray(img_l)\n        mask_lb_arr = np.asarray(mask_lb)\n        mask_sb_arr = np.asarray(mask_sb)\n        mask_stm_arr = np.asarray(mask_stm)\n        \n    elif(region=='Large Bowel') :\n      \n        mask_l=[]\n        img_l=[]\n        id_l=[]\n        for i in range(len(all_img_df['id'])):\n            if (all_img_df['case'][i]==case and all_img_df['day'][i]==day):\n                mask_color,img = mask_trimer(all_img_df['lb_seg'],all_img_df['Path'],all_img_df['Ht'],all_img_df['Wt'],i)\n                mask_l.append(mask_color)\n                img_l.append(img)\n                id_l.append(all_img_df['id'][i])\n        \n        mask_arr = np.asarray(mask_l)\n        img_arr = np.asarray(img_l)\n        color = 'viridis'\n        \n    elif(region=='Small Bowel') :\n       \n        mask_l=[]\n        img_l=[]\n        id_l=[]\n        for i in range(len(all_img_df['id'])):\n            if (all_img_df['case'][i]==case and all_img_df['day'][i]==day):\n                mask_color,img = mask_trimer(all_img_df['sb_seg'],all_img_df['Path'],all_img_df['Ht'],all_img_df['Wt'],i)\n                mask_l.append(mask_color)\n                img_l.append(img)\n                id_l.append(all_img_df['id'][i])\n        \n        mask_arr = np.asarray(mask_l)\n        img_arr = np.asarray(img_l)\n        color = 'rainbow'\n\n    elif(region=='Stomach') :\n       \n        mask_l=[]\n        img_l=[]\n        id_l=[]\n        for i in range(len(all_img_df['id'])):\n            if (all_img_df['case'][i]==case and all_img_df['day'][i]==day):\n                mask_color,img = mask_trimer(all_img_df['stm_seg'],all_img_df['Path'],all_img_df['Ht'],all_img_df['Wt'],i)\n                mask_l.append(mask_color)\n                img_l.append(img)\n                id_l.append(all_img_df['id'][i])\n        \n        mask_arr = np.asarray(mask_l)\n        img_arr = np.asarray(img_l)\n        color = 'rainbow_r'\n\n        \n        \n    def interactive_plot(slices):\n        for j in range((img_arr.shape[0])):\n            \n            if (region=='All'):\n                plt.subplot(1,1,1) \n                plt.imshow(img_arr[slices,:,:], cmap='gray', alpha=1)\n                plt.imshow(mask_lb_arr[slices,:,:], interpolation='nearest',cmap='viridis',vmin=0,vmax=1)\n                plt.imshow(mask_sb_arr[slices,:,:], interpolation='nearest',cmap='rainbow',vmin=0,vmax=1)\n                plt.imshow(mask_stm_arr[slices,:,:], interpolation='nearest',cmap='rainbow_r',vmin=0,vmax=1)\n                plt.title(id_l[slices]) \n                \n            else:\n                plt.subplot(1,1,1) \n                plt.imshow(img_arr[slices,:,:], cmap='gray', alpha=1)\n                plt.imshow(mask_arr[slices,:,:], interpolation='nearest',cmap=color,vmin=0,vmax=1)\n                plt.title(id_l[slices]) \n                \n        y_patch = mpatches.Patch(color='yellow',label = 'Large Bowel')\n        r_patch = mpatches.Patch(color='red', label = 'Small Bowel')\n        p_patch = mpatches.Patch(color='purple', label = 'Stomach')\n        plt.legend(handles=[y_patch,r_patch,p_patch], bbox_to_anchor=(1.6, 1.05))\n        \n    interact(interactive_plot,slices = (0,img_arr.shape[0]-1))","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:50:37.152499Z","iopub.execute_input":"2022-08-13T11:50:37.152792Z","iopub.status.idle":"2022-08-13T11:50:37.176563Z","shell.execute_reply.started":"2022-08-13T11:50:37.152739Z","shell.execute_reply":"2022-08-13T11:50:37.175748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"select_region = ToggleButtons(\n    options = ['All','Large Bowel','Small Bowel','Stomach'],\n    description = \"Pick region:\",\n    button_style = 'info',\n)\ninteractive(interactive_path,region=select_region)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T11:50:39.892763Z","iopub.execute_input":"2022-08-13T11:50:39.893404Z","iopub.status.idle":"2022-08-13T11:50:41.054115Z","shell.execute_reply.started":"2022-08-13T11:50:39.893365Z","shell.execute_reply":"2022-08-13T11:50:41.053360Z"},"trusted":true},"execution_count":null,"outputs":[]}]}