{"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":"# HuBMAP create Mask from Polygon","metadata":{}},{"cell_type":"code","source":"!pip install imantics --quiet","metadata":{"execution":{"iopub.status.busy":"2023-06-18T05:31:02.444933Z","iopub.execute_input":"2023-06-18T05:31:02.445358Z","iopub.status.idle":"2023-06-18T05:31:15.490734Z","shell.execute_reply.started":"2023-06-18T05:31:02.445329Z","shell.execute_reply":"2023-06-18T05:31:15.489400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport json\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport imantics\nfrom skimage.transform import resize","metadata":{"execution":{"iopub.status.busy":"2023-06-18T05:31:15.493866Z","iopub.execute_input":"2023-06-18T05:31:15.494405Z","iopub.status.idle":"2023-06-18T05:31:15.501776Z","shell.execute_reply.started":"2023-06-18T05:31:15.494356Z","shell.execute_reply":"2023-06-18T05:31:15.500586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_jsonl(file_path):\n    data = []\n    with open(file_path, 'r') as file:\n        for line in file:\n            json_data = json.loads(line)\n            data.append(json_data)\n    return data","metadata":{"execution":{"iopub.status.busy":"2023-06-18T05:31:15.503408Z","iopub.execute_input":"2023-06-18T05:31:15.503892Z","iopub.status.idle":"2023-06-18T05:31:15.519121Z","shell.execute_reply.started":"2023-06-18T05:31:15.503853Z","shell.execute_reply":"2023-06-18T05:31:15.517997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"title=pd.read_csv('/kaggle/input/hubmap-hacking-the-human-vasculature/tile_meta.csv')\ndisplay(title)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T05:31:15.522202Z","iopub.execute_input":"2023-06-18T05:31:15.522572Z","iopub.status.idle":"2023-06-18T05:31:15.559252Z","shell.execute_reply.started":"2023-06-18T05:31:15.522544Z","shell.execute_reply":"2023-06-18T05:31:15.558410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wsi=pd.read_csv('/kaggle/input/hubmap-hacking-the-human-vasculature/wsi_meta.csv')\ndisplay(wsi)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T05:31:15.560404Z","iopub.execute_input":"2023-06-18T05:31:15.561186Z","iopub.status.idle":"2023-06-18T05:31:15.578344Z","shell.execute_reply.started":"2023-06-18T05:31:15.561155Z","shell.execute_reply":"2023-06-18T05:31:15.577314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub=pd.read_csv('/kaggle/input/hubmap-hacking-the-human-vasculature/sample_submission.csv')\ndisplay(sample_sub)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T05:31:15.579608Z","iopub.execute_input":"2023-06-18T05:31:15.579915Z","iopub.status.idle":"2023-06-18T05:31:15.593811Z","shell.execute_reply.started":"2023-06-18T05:31:15.579889Z","shell.execute_reply":"2023-06-18T05:31:15.592744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# photo image","metadata":{}},{"cell_type":"code","source":"image_path = '/kaggle/input/hubmap-hacking-the-human-vasculature/train/0006ff2aa7cd.tif'\nimage = mpimg.imread(image_path)\nplt.imshow(image)\nplt.axis('off')\nplt.show()\nprint(image.shape)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T05:31:15.595031Z","iopub.execute_input":"2023-06-18T05:31:15.595374Z","iopub.status.idle":"2023-06-18T05:31:15.853011Z","shell.execute_reply.started":"2023-06-18T05:31:15.595346Z","shell.execute_reply":"2023-06-18T05:31:15.851323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# polygon visualize","metadata":{}},{"cell_type":"code","source":"file_path = '/kaggle/input/hubmap-hacking-the-human-vasculature/polygons.jsonl'\njsonl_data = read_jsonl(file_path)\ndata = pd.DataFrame(jsonl_data)\ndisplay(data)\nanno0=data.iloc[0,1]\nprint(len(anno0))\n#print(anno0[0])","metadata":{"execution":{"iopub.status.busy":"2023-06-18T05:31:15.854569Z","iopub.execute_input":"2023-06-18T05:31:15.855221Z","iopub.status.idle":"2023-06-18T05:31:19.625321Z","shell.execute_reply.started":"2023-06-18T05:31:15.855172Z","shell.execute_reply":"2023-06-18T05:31:19.624272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cell_colors = [\n    (255, 0, 0),    # Red\n    (0, 255, 0),    # Green\n    (0, 0, 255),    # Blue\n    (255, 255, 0),  # Yellow\n    (255, 0, 255),  # Magenta\n    (0, 255, 255),  # Cyan\n    (128, 0, 0),    # Maroon\n    (0, 128, 0),    # Green (dark)\n    (0, 0, 128),    # Navy\n]","metadata":{"execution":{"iopub.status.busy":"2023-06-18T05:31:19.626559Z","iopub.execute_input":"2023-06-18T05:31:19.626893Z","iopub.status.idle":"2023-06-18T05:31:19.632885Z","shell.execute_reply.started":"2023-06-18T05:31:19.626865Z","shell.execute_reply":"2023-06-18T05:31:19.632083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bgw = np.ones(image.shape)*225 #,dtype=np.uint8\nbgw2 = np.copy(bgw)\n\nimage2=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)\nimage2 = cv2.cvtColor(image2, cv2.COLOR_GRAY2RGB)\n\ncell_positions = anno0","metadata":{"execution":{"iopub.status.busy":"2023-06-18T05:31:19.635579Z","iopub.execute_input":"2023-06-18T05:31:19.636299Z","iopub.status.idle":"2023-06-18T05:31:19.657150Z","shell.execute_reply.started":"2023-06-18T05:31:19.636270Z","shell.execute_reply":"2023-06-18T05:31:19.656165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, cell_position in enumerate(cell_positions):\n    \n    ptype=cell_position['type']\n    coordinates=cell_position['coordinates']\n    \n    if ptype=='blood_vessel':\n        color = cell_colors[0] #red\n    elif ptype=='glomerulus':\n        color = cell_colors[1] #green\n    else:\n        color = cell_colors[2] #blue\n        print(ptype)\n    for coordinate in coordinates:\n        for xy in coordinate:\n            x,y=xy[0],xy[1]\n            bgw2[y,x] = color\n            image2[y,x] = color\n            \nplt.imshow(bgw2)\nplt.axis('off')\nplt.show()   \n\nplt.imshow(image2)\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-18T05:31:19.661133Z","iopub.execute_input":"2023-06-18T05:31:19.661522Z","iopub.status.idle":"2023-06-18T05:31:20.059275Z","shell.execute_reply.started":"2023-06-18T05:31:19.661489Z","shell.execute_reply":"2023-06-18T05:31:20.057513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# create mask from polygon data","metadata":{}},{"cell_type":"code","source":"image_size = 512\ninput_image_size = (512,512)\n\nmasks = np.zeros((len(cell_positions),image_size,image_size,1), dtype=bool)\ntypes=[]\nfor i, cell_position in enumerate(cell_positions):\n    segmentation = cell_position[\"coordinates\"]\n    types += [cell_position[\"type\"]]\n    cur_mask = imantics.Polygons(segmentation).mask(*input_image_size).array\n    cur_mask = np.expand_dims(resize(cur_mask, (image_size, image_size), mode='constant', preserve_range=True), 2)\n    masks[i] = masks[i] | cur_mask","metadata":{"execution":{"iopub.status.busy":"2023-06-18T05:31:20.061631Z","iopub.execute_input":"2023-06-18T05:31:20.062651Z","iopub.status.idle":"2023-06-18T05:31:20.120192Z","shell.execute_reply.started":"2023-06-18T05:31:20.062601Z","shell.execute_reply":"2023-06-18T05:31:20.118953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,mask in enumerate(masks):\n    print(types[i],mask.shape) \n    plt.imshow(mask)\n    plt.axis('off')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-18T05:37:02.039138Z","iopub.execute_input":"2023-06-18T05:37:02.039585Z","iopub.status.idle":"2023-06-18T05:37:03.283235Z","shell.execute_reply.started":"2023-06-18T05:37:02.039550Z","shell.execute_reply":"2023-06-18T05:37:03.281784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks2=np.sum(masks,axis=0)\nprint(masks2.shape)\nplt.imshow(masks2)\nplt.axis('off')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}