{"cells":[{"metadata":{},"cell_type":"markdown","source":"# How to embed images in dataframe"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nfrom functools import reduce\n\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport skimage.io","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls -lh ../input/prostate-cancer-grade-assessment","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"INPUT_DIR = \"../input/prostate-cancer-grade-assessment\"\nTRAIN_DIR = \"train_images\"\nMASK_DIR = \"train_label_masks\"","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train = pd.read_csv(f\"{INPUT_DIR}/train.csv\")\ntest = pd.read_csv(f\"{INPUT_DIR}/test.csv\")\nsbm = pd.read_csv(f\"{INPUT_DIR}/sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# See: https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html#Subclassing\n\nfrom IPython.display import HTML\nfrom pandas.io.formats.style import Styler\n\n\nTEMPLATE = \"\"\"\n{% extends \"html.tpl\" %}\n{% block table %}\n{{ super() }}\n<span>Shape: {{ shape|default(\"\") }}</span>\n{% endblock table %}\n\"\"\"\n\nwith open(\"with_shape.tpl\", \"w\") as f:\n    f.write(TEMPLATE)\n\nWithShape = Styler.from_custom_template(\".\", \"with_shape.tpl\")\n\ndef profile(df):\n    return HTML(WithShape(df.head()).render(shape=str(df.shape)))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"profile(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"profile(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"profile(sbm)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[\"gleason_score\"].value_counts().sort_index().to_frame()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Display images"},{"metadata":{"trusted":true},"cell_type":"code","source":"import base64\nfrom io import BytesIO\n\n\ndef encode_image(fig):\n    \"\"\"\n    See: https://stackoverflow.com/questions/48717794/matplotlib-embed-figures-in-auto-generated-html\n    \"\"\"\n    b = BytesIO()\n    fig.savefig(b, format=\"png\", bbox_inches=\"tight\", pad_inches=0.05)\n    return base64.b64encode(b.getvalue()).decode(\"utf-8\")\n\n    \ndef draw_image(img_path):\n    img = skimage.io.MultiImage(img_path)[-1]\n\n    fig, ax = plt.subplots()\n    ax.imshow(img)\n    ax.grid(False)\n    ax.set_xticks([])\n    ax.set_yticks([])\n    \n    for pos in [\"top\",\"bottom\", \"left\", \"right\"]:\n        ax.spines[pos].set_linewidth(0.5)\n    \n    return fig\n\n\ndef draw_mask(mask_path):\n    mask = skimage.io.MultiImage(mask_path)[-1]\n\n    colors = [\"black\", \"gray\", \"green\", \"yellow\", \"orange\", \"red\"]\n    cmap = matplotlib.colors.ListedColormap(colors)\n\n    fig, ax = plt.subplots()\n    ax.imshow(mask[:, :, 0], cmap=cmap, interpolation=\"nearest\", vmin=0, vmax=5)\n    ax.set_xticks([])\n    ax.set_yticks([])\n    \n    for pos in [\"top\",\"bottom\", \"left\", \"right\"]:\n        ax.spines[pos].set_linewidth(0.5)\n    \n    return fig\n\n\ndef merge_cols(df, cols):\n    return reduce(\n        lambda x, y: x + \"<br>\" + y,\n        [f\"{col}: \" + df[col].astype(str) for col in cols]\n    )\n\n\ndef show_images(df):\n    img_tag_tpl = '<img src=\"data:image/png;base64,{}\" style=\"dispaly: block; margin: 0 auto\">'\n\n    imgs = []\n    masks = []\n    for idx, row in df.iloc[:9].reset_index(drop=False).iterrows():\n        # Process image\n        img_path = os.path.join(INPUT_DIR, TRAIN_DIR, row[\"image_id\"] + \".tiff\")\n        img_fig = draw_image(img_path)\n        imgs.append(img_tag_tpl.format(encode_image(img_fig)))\n        plt.close(img_fig)\n\n        # Process mask\n        mask_path = os.path.join(INPUT_DIR, MASK_DIR, row[\"image_id\"] + \"_mask.tiff\")\n        \n        if os.path.exists(mask_path):\n            mask_fig = draw_mask(mask_path)\n            masks.append(img_tag_tpl.format(encode_image(mask_fig)))\n            plt.close(mask_fig)\n        else:\n            masks.append(\"\")\n\n    cols = [\"image_id\", \"data_provider\", \"isup_grade\", \"gleason_score\"]\n\n    return HTML(\n        df\n        .assign(\n            info=merge_cols(df, cols),\n            image=imgs,\n            mask=masks,\n        )\n        .drop(cols, axis=1)\n        .style\n        .set_properties(**{\n            \"background-color\": \"white\",\n            \"border\": \"1px solid black\",\n            \"text-align\": \"center\",\n        })\n        .hide_index()\n        .render()\n    )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_images(train[train[\"gleason_score\"] == \"5+5\"].sample(9, random_state=42))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_images(train[train[\"gleason_score\"] == \"5+4\"].sample(9, random_state=42))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_images(train[train[\"gleason_score\"] == \"5+3\"].sample(9, random_state=42))","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}