{"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":23823,"databundleVersionId":1920183,"sourceType":"competition"},{"sourceId":1154184,"sourceType":"datasetVersion","datasetId":652413},{"sourceId":1885540,"sourceType":"datasetVersion","datasetId":1123128},{"sourceId":1888569,"sourceType":"datasetVersion","datasetId":1125071},{"sourceId":1888577,"sourceType":"datasetVersion","datasetId":1125092},{"sourceId":1966767,"sourceType":"datasetVersion","datasetId":1174392},{"sourceId":2124928,"sourceType":"datasetVersion","datasetId":1275092}],"dockerImageVersionId":30066,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nimport typing as t\nimport time\n\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-25T13:22:50.38592Z","iopub.execute_input":"2021-06-25T13:22:50.3864Z","iopub.status.idle":"2021-06-25T13:22:50.393147Z","shell.execute_reply.started":"2021-06-25T13:22:50.386304Z","shell.execute_reply":"2021-06-25T13:22:50.391874Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# https://www.kaggle.com/rdizzl3/hpa-segmentation-masks-no-internet/notebook\n\n# https://github.com/CellProfiling/HPA-Cell-Segmentation\n\ntry:\n    import hpacellseg.cellsegmentator as cellsegmentator\n    from hpacellseg.utils import label_cell\nexcept:\n    !pip install -q \"../input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl\"\n    !pip install -q \"../input/hpapytorchzoozip/pytorch_zoo-master\"\n    !pip install -q \"../input/hpacellsegmentatormaster/HPA-Cell-Segmentation-master\"\n    import hpacellseg.cellsegmentator as cellsegmentator\n    from hpacellseg.utils import label_cell, label_nuclei\n    \nNUC_MODEL = '../input/hpacellsegmentatormodelweights/dpn_unet_nuclei_v1.pth'\nCELL_MODEL = '../input/hpacellsegmentatormodelweights/dpn_unet_cell_3ch_v1.pth'","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:22:50.40028Z","iopub.execute_input":"2021-06-25T13:22:50.400653Z","iopub.status.idle":"2021-06-25T13:23:20.110758Z","shell.execute_reply.started":"2021-06-25T13:22:50.400622Z","shell.execute_reply":"2021-06-25T13:23:20.109428Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def build_image_names(image_id: str) -> list:\n    # red = microtubule\n    mt = f'../input/hpa-single-cell-image-classification/train/{image_id}_red.png'\n    \n    # blue = nuclei\n    nu = f'../input/hpa-single-cell-image-classification/train/{image_id}_blue.png'\n    \n    # yellow = endoplasmic reticulum\n    er = f'../input/hpa-single-cell-image-classification/train/{image_id}_yellow.png'\n    \n    # green = target protein\n    tp = f'../input/hpa-single-cell-image-classification/train/{image_id}_green.png'\n    \n    return [mt], [er], [nu], [tp], [[mt], [er], [nu]]","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:23:20.113629Z","iopub.execute_input":"2021-06-25T13:23:20.114154Z","iopub.status.idle":"2021-06-25T13:23:20.122171Z","shell.execute_reply.started":"2021-06-25T13:23:20.114105Z","shell.execute_reply":"2021-06-25T13:23:20.120434Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"segmentator = cellsegmentator.CellSegmentator(\n    NUC_MODEL,\n    CELL_MODEL,\n    scale_factor=0.25,\n    device='cuda',\n    padding=False,\n    multi_channel_model=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:23:20.123848Z","iopub.execute_input":"2021-06-25T13:23:20.124395Z","iopub.status.idle":"2021-06-25T13:23:32.163359Z","shell.execute_reply.started":"2021-06-25T13:23:20.124356Z","shell.execute_reply":"2021-06-25T13:23:32.161573Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"start_time = time.time()\n\nnames = build_image_names('002679c2-bbb6-11e8-b2ba-ac1f6b6435d0')\ncell_segmentations = segmentator.pred_cells(names[-1])\nnuc_segmentations = segmentator.pred_nuclei(names[2])\n\ni = 0\nnuclei_mask, cell_mask = label_cell(nuc_segmentations[i], cell_segmentations[i])\n\nprint(time.time()-start_time)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:23:32.165511Z","iopub.execute_input":"2021-06-25T13:23:32.165941Z","iopub.status.idle":"2021-06-25T13:23:51.129043Z","shell.execute_reply.started":"2021-06-25T13:23:32.165903Z","shell.execute_reply":"2021-06-25T13:23:51.12778Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nplt.figure(figsize=(20,10))\nmt = plt.imread(names[0][0])\ner = plt.imread(names[1][0])\nnu = plt.imread(names[2][0])\ntp = plt.imread(names[3][0])\nmask = cell_mask\n\nimg = np.dstack((mt, er, nu))\nplt.axis('off')\nplt.imshow(img)\n#plt.imshow(mask, alpha=0.4)\nplt.savefig('org_rgb.png')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:27:09.123827Z","iopub.execute_input":"2021-06-25T13:27:09.124246Z","iopub.status.idle":"2021-06-25T13:27:11.066274Z","shell.execute_reply.started":"2021-06-25T13:27:09.12421Z","shell.execute_reply":"2021-06-25T13:27:11.06516Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.axis('off')\nplt.imshow(mask)\nplt.savefig('seg_mask_total.png')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:23:53.800388Z","iopub.execute_input":"2021-06-25T13:23:53.800815Z","iopub.status.idle":"2021-06-25T13:23:54.400566Z","shell.execute_reply.started":"2021-06-25T13:23:53.800767Z","shell.execute_reply":"2021-06-25T13:23:54.399137Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, ax = plt.subplots(2, 2, figsize=(14, 14))\n\nax[0][0].imshow(nu)\nax[0][0].title.set_text('nucleus')\nax[0][1].imshow(mt)\nax[0][1].title.set_text('microtubules')\nax[1][0].imshow(er)\nax[1][0].title.set_text('endoplasmic reticulum')\nax[1][1].imshow(tp)\nax[1][1].title.set_text('target protein')\n\nfor i in [0, 1]:\n    for j in [0, 1]:\n        ax[i][j].axes.xaxis.set_visible(False)\n        ax[i][j].axes.yaxis.set_visible(False)\n        \nplt.subplots_adjust(wspace=0.05, hspace=0.05)\nplt.savefig('4_ch_seperately.png')","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:23:54.402578Z","iopub.execute_input":"2021-06-25T13:23:54.403103Z","iopub.status.idle":"2021-06-25T13:23:57.089093Z","shell.execute_reply.started":"2021-06-25T13:23:54.403055Z","shell.execute_reply":"2021-06-25T13:23:57.08815Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.axis('off')\n\nmask_individual_cell = cell_mask == 7\nplt.imshow(mask_individual_cell)\nplt.title('cell 7 mask')\n\nplt.savefig('mask_cell_7.png')\nmask_individual_cell[0][400]","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:23:57.090911Z","iopub.execute_input":"2021-06-25T13:23:57.091273Z","iopub.status.idle":"2021-06-25T13:23:57.657725Z","shell.execute_reply.started":"2021-06-25T13:23:57.091237Z","shell.execute_reply":"2021-06-25T13:23:57.656818Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"first_x = cell_mask.shape[1]\nfirst_y = cell_mask.shape[0]\nlast_x = 0\nlast_y = 0\ncount = 0\n\nfor i in range(cell_mask.shape[1]):\n    for j in range(cell_mask.shape[0]):\n        if mask_individual_cell[i][j] == True:\n            count += 1\n            if first_x > j:\n                first_x = j\n            if last_x < j:\n                last_x = j\n            if first_y > i:\n                first_y = i\n            if last_y < i:\n                last_y = i\n\nfirst_x -= min(10, first_x)\nfirst_y -= min(10, first_y)\nlast_x += min(10, cell_mask.shape[1] - last_x)\nlast_y += min(10, cell_mask.shape[0] - last_y)\n\nprint(first_x, last_x, first_y, last_y)\n\nplt.axis('off')\nplt.imshow(mask_individual_cell[first_y: last_y, first_x: last_x])\nplt.savefig('isolated_mask_cell_7.png')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:23:57.658764Z","iopub.execute_input":"2021-06-25T13:23:57.65902Z","iopub.status.idle":"2021-06-25T13:24:07.458027Z","shell.execute_reply.started":"2021-06-25T13:23:57.658995Z","shell.execute_reply":"2021-06-25T13:24:07.456972Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.axis('off')\nplt.imshow(img[first_y: last_y, first_x: last_x])\nplt.savefig('cell_7_with_neighbors.png')","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:24:07.459534Z","iopub.execute_input":"2021-06-25T13:24:07.459941Z","iopub.status.idle":"2021-06-25T13:24:07.639154Z","shell.execute_reply.started":"2021-06-25T13:24:07.459906Z","shell.execute_reply":"2021-06-25T13:24:07.637913Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub_img = mask_individual_cell[first_y: last_y, first_x: last_x]\nempty_img = np.zeros_like(sub_img)\nfinal_img_mt = np.where(mask_individual_cell[first_y: last_y, first_x: last_x] == False, np.zeros_like(sub_img), mt[first_y: last_y, first_x: last_x])\nfinal_img_er = np.where(mask_individual_cell[first_y: last_y, first_x: last_x] == False, np.zeros_like(sub_img), er[first_y: last_y, first_x: last_x])\nfinal_img_nu = np.where(mask_individual_cell[first_y: last_y, first_x: last_x] == False, np.zeros_like(sub_img), nu[first_y: last_y, first_x: last_x])\nfinal_img_tp = np.where(mask_individual_cell[first_y: last_y, first_x: last_x] == False, np.zeros_like(sub_img), tp[first_y: last_y, first_x: last_x])\n\nsub_img = np.dstack((final_img_mt, final_img_er, final_img_nu))\nplt.axis('off')\nplt.imshow(sub_img)\nplt.savefig('cell_7_isolated.png')","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:24:07.640753Z","iopub.execute_input":"2021-06-25T13:24:07.64116Z","iopub.status.idle":"2021-06-25T13:24:07.796667Z","shell.execute_reply.started":"2021-06-25T13:24:07.641118Z","shell.execute_reply":"2021-06-25T13:24:07.795499Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.axis('off')\nplt.imshow(final_img_mt)\nplt.savefig('cell_7_mt_iso.png')\n\nplt.imshow(final_img_er)\nplt.savefig('cell_7_er_iso.png')\n\nplt.imshow(final_img_nu)\nplt.savefig('cell_7_nu_iso.png')\n\nplt.imshow(final_img_tp)\nplt.savefig('cell_7_tp_iso.png')","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:24:07.798176Z","iopub.execute_input":"2021-06-25T13:24:07.798491Z","iopub.status.idle":"2021-06-25T13:24:08.22123Z","shell.execute_reply.started":"2021-06-25T13:24:07.798462Z","shell.execute_reply":"2021-06-25T13:24:08.219633Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, ax = plt.subplots(2, 2, figsize=(10, 10))\n\nax[0][0].imshow(final_img_nu)\nax[0][0].title.set_text('nucleus')\nax[0][1].imshow(final_img_mt)\nax[0][1].title.set_text('microtubules')\nax[1][0].imshow(final_img_er)\nax[1][0].title.set_text('endoplasmic reticulum')\nax[1][1].imshow(final_img_tp)\nax[1][1].title.set_text('target protein')\n\nfor i in [0, 1]:\n    for j in [0, 1]:\n        ax[i][j].axes.xaxis.set_visible(False)\n        ax[i][j].axes.yaxis.set_visible(False)\n        \nplt.subplots_adjust(wspace=0.01, hspace=0.1)\nplt.savefig('cell_7_final_result_seperately.png')","metadata":{"execution":{"iopub.status.busy":"2021-06-25T13:24:43.886119Z","iopub.execute_input":"2021-06-25T13:24:43.886482Z","iopub.status.idle":"2021-06-25T13:24:44.414685Z","shell.execute_reply.started":"2021-06-25T13:24:43.886449Z","shell.execute_reply":"2021-06-25T13:24:44.413607Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.axis('off')\nfig, ax = plt.subplots(1, 3, figsize=(14, 6))\n\nax[0].imshow(img)\nax[0].title.set_text('original image without\\ntarget protein channel')\nax[1].imshow(mask)\nax[1].title.set_text('segmentation mask')\nax[2].imshow(mask_individual_cell)\nax[2].title.set_text('segmentation mask cell 7')\n\nfor j in [0, 1, 2]:\n    ax[j].axes.xaxis.set_visible(False)\n    ax[j].axes.yaxis.set_visible(False)\n    \nplt.subplots_adjust(wspace=0.1, hspace=0.15)\nplt.savefig('seg fig upper.png')\nplt.show()\n\n#===============================================\n    \nfig, ax = plt.subplots(1, 3, figsize=(14, 6))\n\nax[0].imshow(mask_individual_cell[first_y: last_y, first_x: last_x])\nax[0].title.set_text('cut out mask for cell 7')\nax[1].imshow(img[first_y: last_y, first_x: last_x])\nax[1].title.set_text('original image cut out around cell 7')\nax[2].imshow(sub_img)\nax[2].title.set_text('image of cell 7 only')\n\nfor j in [0, 1, 2]:\n    ax[j].axes.xaxis.set_visible(False)\n    ax[j].axes.yaxis.set_visible(False)\n    \nplt.subplots_adjust(wspace=0.1, hspace=0.15)\nplt.savefig('seg fig lower.png')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:09:37.469339Z","iopub.execute_input":"2021-06-25T14:09:37.469741Z","iopub.status.idle":"2021-06-25T14:09:40.639857Z","shell.execute_reply.started":"2021-06-25T14:09:37.46969Z","shell.execute_reply":"2021-06-25T14:09:40.638745Z"},"trusted":true},"outputs":[],"execution_count":null}]}