{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":22990,"datasetId":1136396,"databundleVersionId":2048213},{"sourceType":"datasetVersion","sourceId":7455059,"datasetId":4339413,"databundleVersionId":7547009},{"sourceType":"kernelVersion","sourceId":103094444}],"dockerImageVersionId":30213,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# HuBMAP ⚕️ Segm: EDA 🔎 & exporting RLE masks","metadata":{}},{"cell_type":"code","source":"!conda install ../input/conda-pkg-pyvips-download-4-offline/conda-pkgs/*.tar.bz2 --offline","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-01-22T12:29:25.584296Z","iopub.execute_input":"2024-01-22T12:29:25.584814Z","iopub.status.idle":"2024-01-22T12:30:12.161847Z","shell.execute_reply.started":"2024-01-22T12:29:25.584712Z","shell.execute_reply":"2024-01-22T12:30:12.160303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Browse metadata","metadata":{}},{"cell_type":"code","source":"import os, glob\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport sys\n\nsys.path.append(\"/kaggle/input/rle-run-length-encoding-py-module/\")  # this will be path to this dataset\nDATASET_FOLDER = \"/kaggle/input/hubmap-kidney-segmentation\"\ndf_train = pd.read_csv(os.path.join(DATASET_FOLDER, \"train.csv\"))\ndisplay(df_train.head())","metadata":{"execution":{"iopub.status.busy":"2024-01-22T12:30:12.164247Z","iopub.execute_input":"2024-01-22T12:30:12.164807Z","iopub.status.idle":"2024-01-22T12:30:12.721957Z","shell.execute_reply.started":"2024-01-22T12:30:12.164763Z","shell.execute_reply":"2024-01-22T12:30:12.720567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Generate annotations 🗃️","metadata":{}},{"cell_type":"code","source":"from rle import rle_decode","metadata":{"execution":{"iopub.status.busy":"2024-01-22T12:30:12.723254Z","iopub.execute_input":"2024-01-22T12:30:12.723619Z","iopub.status.idle":"2024-01-22T12:30:12.741300Z","shell.execute_reply.started":"2024-01-22T12:30:12.723588Z","shell.execute_reply":"2024-01-22T12:30:12.739989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir train_images/\n!mkdir train_masks/","metadata":{"execution":{"iopub.status.busy":"2024-01-22T12:30:12.744694Z","iopub.execute_input":"2024-01-22T12:30:12.745565Z","iopub.status.idle":"2024-01-22T12:30:14.930359Z","shell.execute_reply.started":"2024-01-22T12:30:12.745508Z","shell.execute_reply":"2024-01-22T12:30:14.928989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pyvips\nfrom tqdm.auto import tqdm\n\nos.environ['VIPS_DISC_THRESHOLD'] = '12gb'\n# Image.MAX_IMAGE_PIXELS = 5_000_000_000\n\nfor _, row in tqdm(df_train.iterrows(), total=len(df_train)):\n    img_path = os.path.join(DATASET_FOLDER, \"train\", f\"{row['id']}.tiff\")\n    img = pyvips.Image.new_from_file(img_path)\n    img_path = os.path.join(\"train_images\", f\"{row['id']}.png\")\n    img.write_to_file(img_path)\n    # VIPS reads the buffer as if the data is saved column by column (column major)\n    # but numpy saves it in row major order.\n    mask = rle_decode(row['encoding'], img_shape=(img.width, img.height)).T\n    seg_path = os.path.join(\"train_masks\", f\"{row['id']}.png\")\n    # Image.fromarray(mask).save(seg_path)\n    seg = pyvips.Image.new_from_array(mask)\n    assert (img.width, img.height) == (seg.width, seg.height)\n    seg.write_to_file(seg_path)\n    print(seg.width, seg.height)\n    del img, mask, seg","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-01-22T12:30:14.932247Z","iopub.execute_input":"2024-01-22T12:30:14.932752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -lh train_images/\n!ls -lh train_masks/","metadata":{"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Show some images 🖼️","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom skimage import color\n\nfig, axes = plt.subplots(nrows=3, ncols=2, figsize=(9, 9))\nfor i, (_, row) in enumerate(df_train.sample(3).iterrows()):\n    img_path = os.path.join(\"train_images\", f\"{row['id']}.png\")\n    img = pyvips.Image.thumbnail(img_path, 1000).numpy()\n    seg_path = os.path.join(\"train_masks\", f\"{row['id']}.png\")\n    mask = pyvips.Image.thumbnail(seg_path, 1000).numpy()\n    print(img.shape, mask.shape)\n    axes[i, 0].imshow(img)\n    axes[i, 0].set_axis_off()\n    axes[i, 1].imshow(color.label2rgb(mask, img, bg_label=0, bg_color=(1.,1.,1.), alpha=0.25))\n    axes[i, 1].set_axis_off()\n    del img, mask\nfig.tight_layout()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}