{"cells":[{"metadata":{},"cell_type":"markdown","source":"You can replace `submission_v1.csv` with your own public test preds "},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from fastai.vision.all import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datapath = Path(\"/kaggle/input/hubmap-kidney-segmentation/\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"sample_subdf = pd.read_csv(datapath/\"sample_submission.csv\")\nsubdf = pd.read_csv(\"/kaggle/input/hubmapsubmissions/submission_v1.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"subdf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"id2rle = dict(zip(subdf['id'], subdf['predicted']))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### rle utils"},{"metadata":{"trusted":true},"cell_type":"code","source":"def enc2mask(encs, shape):\n    \"shape : (w,h)\"\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for m,enc in enumerate(encs):\n        if isinstance(enc,np.float) and np.isnan(enc): continue\n        s = enc.split()\n        for i in range(len(s)//2):\n            start = int(s[2*i]) - 1\n            length = int(s[2*i+1])\n            img[start:start+length] = 1 + m\n    return img.reshape(shape).T\n\ndef rle_encode_less_memory(img):\n    pixels = img.T.flatten()\n    pixels[0] = 0\n    pixels[-1] = 0\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 2\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Filter out struct"},{"metadata":{"trusted":true},"cell_type":"code","source":"import rasterio, cv2, gc","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_struct_mask(dataset, structs, names=None):\n    \"Generate mask for anatomical struct including only names\"\n    struct_mask = np.zeros(dataset.shape)\n\n    for _,row in structs.iterrows():\n        if names is None:\n            coords = array(row['geometry']['coordinates'][0])\n            struct_mask = cv2.fillPoly(struct_mask, pts =[coords], color=255);\n        \n        elif row['properties']['classification']['name'] in names:   \n            coords = array(row['geometry']['coordinates'][0])\n            struct_mask = cv2.fillPoly(struct_mask, pts =[coords], color=255);\n\n    return struct_mask.astype(np.uint8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# def get_cortex_mask(dataset, structs):\n#     cortex_mask = np.zeros(dataset.shape)\n\n#     for _,row in structs.iterrows():\n#         if row['properties']['classification']['name'] == 'Cortex':   \n#             coords = array(row['geometry']['coordinates'][0])\n#             cortex_mask = cv2.fillPoly(cortex_mask, pts =[coords], color=255);\n\n#     return cortex_mask.astype(np.uint8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"public_testids = ['afa5e8098', 'b9a3865fc', 'c68fe75ea', 'b2dc8411c', '26dc41664']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testid = public_testids[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_id2rle = {}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for testid in progress_bar(public_testids):\n\n    structs = pd.read_json(datapath/f'test/{testid}-anatomical-structure.json')\n\n    dataset = rasterio.open(datapath/f'test/{testid}.tiff') \n\n\n#     cortex_mask = get_cortex_mask(dataset, structs)\n    cortex_mask = get_struct_mask(dataset, structs)\n\n    rle_mask = enc2mask([id2rle[testid]], dataset.shape[::-1])\n\n    # set non-cortex preds to 0\n    rle_mask[cortex_mask == 0] = 0\n\n    del cortex_mask, dataset\n    gc.collect()\n\n    new_rle = rle_encode_less_memory(rle_mask)\n    \n    new_id2rle[testid] = new_rle","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"subdf['predicted'] = subdf['id'].map(new_id2rle)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Submit"},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_subdf = sample_subdf[['id']].merge(subdf, on='id', how='left')\nsample_subdf['predicted'] = sample_subdf['predicted'].fillna('1 1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_subdf.to_csv(\"submission.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_subdf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}