{"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":"## Importing Library","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"from ipywidgets import interact, interactive, IntSlider, ToggleButtons\nimport numpy as np\nimport pandas as pd\nimport random, re, math\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2021-09-16T04:10:26.013215Z","iopub.execute_input":"2021-09-16T04:10:26.01363Z","iopub.status.idle":"2021-09-16T04:10:26.947687Z","shell.execute_reply.started":"2021-09-16T04:10:26.013535Z","shell.execute_reply":"2021-09-16T04:10:26.946421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Function and pathes for loading data","metadata":{}},{"cell_type":"code","source":"DATA_DIR = \"/kaggle/input/covid-segmentation/\"\nrad=DATA_DIR + \"images_radiopedia.npy\", DATA_DIR + \"masks_radiopedia.npy\"\nmed=DATA_DIR + \"images_medseg.npy\", DATA_DIR + \"masks_medseg.npy\"\ntest=DATA_DIR + \"test_images_medseg.npy\"\n\ndef load_case(image_file, mask_file=None):\n    image =np.load(image_file).astype(np.float32)\n    if not mask_file is None:\n        mask =np.load(mask_file).astype(np.int8)\n        return image, mask\n    return image","metadata":{"execution":{"iopub.status.busy":"2021-09-16T04:10:49.592804Z","iopub.execute_input":"2021-09-16T04:10:49.593129Z","iopub.status.idle":"2021-09-16T04:10:49.601266Z","shell.execute_reply.started":"2021-09-16T04:10:49.593097Z","shell.execute_reply":"2021-09-16T04:10:49.60057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualization functions","metadata":{}},{"cell_type":"markdown","source":"### Let's look at some images and their masks using widget interact ","metadata":{}},{"cell_type":"code","source":"def explore_image(slices):\n    plt.figure(figsize=(10, 10))\n    plt.imshow(load_case(med[0],med[1])[0][slices, :, :, 0], cmap='gray');\n    plt.imshow(load_case(med[0],med[1])[1][slices, :, :, 0].squeeze(), alpha=0.3, cmap='Reds')\n    plt.imshow(load_case(med[0],med[1])[1][slices, :, :, 1].squeeze(), alpha=0.2, cmap='Blues')\n    plt.title('Explore slices of Images_medseg \\n Blue color is Consolidations \\n Red color is Ground Glass  ', fontsize=20)\n    plt.axis('off')\n    return slices\n\n\ninteract(explore_image, slices=(0, load_case(med[0],med[1])[0].shape[0] - 1));","metadata":{"execution":{"iopub.status.busy":"2021-09-16T04:11:03.996227Z","iopub.execute_input":"2021-09-16T04:11:03.997032Z","iopub.status.idle":"2021-09-16T04:11:11.158985Z","shell.execute_reply.started":"2021-09-16T04:11:03.996988Z","shell.execute_reply":"2021-09-16T04:11:11.157435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize(batch_1,batch_2,pred=False):\n   \n    classes_dict = {'Ground_Glass': 0.,\n                    'Consolidations': 1.,\n                    'Lungs_Other': 2.,\n                    'Background': 3. }\n                                   \n    \n    \n    fix, ax = plt.subplots(2, 5, figsize=(60, 40))\n    \n    for i in range(2):\n        \n        \n        ax[i,0].set_xticks([]) \n        ax[i,0].set_yticks([])\n        if i==0:\n            ax[i,0].imshow(batch_1[0][:,:,0], cmap='Greys')\n            ax[i,0].set_ylabel(\"Ground Truth \", fontsize=55) if pred==True else ax[i,0].set_ylabel(\"From batch_1 \", fontsize=55)\n            for j in range(4):\n                mask_f = batch_1[1][:,:,j]\n                img_label = list(classes_dict.keys())[j]\n\n                ax[i, j + 1].imshow(mask_f, vmin=0, vmax=1,cmap='cividis')\n                ax[i, j + 1].set_xticks([]) \n                ax[i, j + 1].set_yticks([]) \n                ax[i][j + 1].set_title(f\"{img_label} \", fontsize=55)\n \n        else:\n            ax[i,0].imshow(batch_2[0][:,:,0], cmap='Greys')\n            ax[i,0].set_ylabel(\"Prediction\", fontsize=55) if pred==True else ax[i,0].set_ylabel(\"From batch_2 \", fontsize=55)\n            for j in range(4):\n                mask_f = batch_2[1][:,:,j]\n                img_label = list(classes_dict.keys())[j]\n\n                ax[i, j + 1].imshow(mask_f, vmin=0, vmax=1,cmap='cividis')\n                ax[i, j + 1].set_xticks([]) \n                ax[i, j + 1].set_yticks([]) \n \n                    \n\n    plt.tight_layout()\n    plt.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2021-09-16T04:11:15.921305Z","iopub.execute_input":"2021-09-16T04:11:15.921834Z","iopub.status.idle":"2021-09-16T04:11:15.939123Z","shell.execute_reply.started":"2021-09-16T04:11:15.921792Z","shell.execute_reply":"2021-09-16T04:11:15.93786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"maxval = 99\ni = np.random.randint(0, maxval)\nvisualize(batch_1=(load_case(med[0])[i], load_case(med[1])[i]),batch_2=(load_case(rad[0])[i], load_case(rad[1])[i]),pred=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-16T04:11:17.28893Z","iopub.execute_input":"2021-09-16T04:11:17.289224Z","iopub.status.idle":"2021-09-16T04:11:51.44757Z","shell.execute_reply.started":"2021-09-16T04:11:17.289187Z","shell.execute_reply":"2021-09-16T04:11:51.445793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def count_classes(image,mask):\n    ground_glass=[]\n    consolidations=[]\n    other=[]\n    background=[]\n    \n    for i in range(image.shape[0]):\n        if mask[i,:,:,0].sum()!=0:\n            ground_glass.append(image[i])\n        if mask[i,:,:,1].sum()!=0:\n            consolidations.append(image[i])\n        if mask[i,:,:,2].sum()!=0:\n            other.append(image[i])\n        if mask[i,:,:,3].sum()!=0:\n            background.append(image[i])\n            \n    x=['ground_glass','consolidations','other','background']\n    y=[len(ground_glass),len(consolidations),len(other),len(background)]\n    plt.figure(figsize=(12,5))\n    plt.title('The number of each category segmentaion in images', fontsize=20)\n    sns.set_context('paper',font_scale=2)\n    sns.barplot(x=x,y=y,palette='coolwarm')\n    \n    print(f'The number of images that has: \\n Ground_Glass Segmentation:{y[0]} \\\n    \\n Consolidations Segmentation:{y[1]} \\n Lung_Others Segmentation:{y[2]} \\n Background Segmentation:{y[3]}')\n        \n                \n    return y","metadata":{"execution":{"iopub.status.busy":"2021-09-16T04:15:42.896177Z","iopub.execute_input":"2021-09-16T04:15:42.896496Z","iopub.status.idle":"2021-09-16T04:15:43.117859Z","shell.execute_reply.started":"2021-09-16T04:15:42.896465Z","shell.execute_reply":"2021-09-16T04:15:43.116545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=count_classes(load_case(med[0],med[1])[0],load_case(med[0],med[1])[1])","metadata":{"execution":{"iopub.status.busy":"2021-09-16T04:15:43.666043Z","iopub.execute_input":"2021-09-16T04:15:43.666345Z","iopub.status.idle":"2021-09-16T04:15:44.429078Z","shell.execute_reply.started":"2021-09-16T04:15:43.666316Z","shell.execute_reply":"2021-09-16T04:15:44.428089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"v=count_classes(load_case(rad[0],rad[1])[0],load_case(rad[0],rad[1])[1])","metadata":{"execution":{"iopub.status.busy":"2021-09-16T04:16:13.743493Z","iopub.execute_input":"2021-09-16T04:16:13.744644Z","iopub.status.idle":"2021-09-16T04:16:20.914599Z","shell.execute_reply.started":"2021-09-16T04:16:13.744579Z","shell.execute_reply":"2021-09-16T04:16:20.913628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}