rs_annotation / app.py
treizh's picture
Upload app.py with huggingface_hub
cf03ace verified
raw
history blame
5.02 kB
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Ralstonia Annotation Tool"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Imports"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"from io import StringIO\n",
"\n",
"import pandas as pd\n",
"import panel as pn\n",
"\n",
"import scripts.tap_const as tc"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pn.extension(\"plotly\", \"tabulator\")\n",
"\n",
"template = pn.template.BootstrapTemplate(title=\"Ralstonia Annotation Tool\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constants"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"EXPERIMENT = \"72AC_PhD_2404\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Setup Paths\n",
"if tc.phenopsis.joinpath(EXPERIMENT).is_dir() is True:\n",
" pt_data = tc.data\n",
" pt_images = tc.phenopsis.joinpath(EXPERIMENT)\n",
" pt_rotations = tc.dataout.joinpath(\"rotation_angles\").joinpath(f\"{EXPERIMENT}\")\n",
"else:\n",
" here = Path(\".\").parent\n",
" pt_data = here.joinpath(\"data\")\n",
" pt_images = here.joinpath(\"images\").joinpath(EXPERIMENT)\n",
" pt_rotations = here.joinpath(\"rotation_angles\").joinpath(f\"{EXPERIMENT}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def to_dataframe(value):\n",
" if not value or not isinstance(value, bytes):\n",
" return pd.DataFrame()\n",
"\n",
" string_io = StringIO(value.decode(\"utf8\"))\n",
" return pd.read_csv(string_io, sep=\";\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Source Selection"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Download Template"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"template_download = pn.widgets.FileDownload(\n",
" file=pt_data.joinpath(f\"{EXPERIMENT}_raw.csv\"),\n",
" filename=f\"{EXPERIMENT}_raw.csv\",\n",
" button_type=\"success\",\n",
" label=\"Download template annotation file\",\n",
")\n",
"template_download"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Upload existing file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"file_input = pn.widgets.FileInput(accept=\".csv,.json\")\n",
"table = pn.widgets.Tabulator(\n",
" pn.bind(to_dataframe, file_input),\n",
" pagination=\"local\",\n",
" page_size=20,\n",
" sizing_mode=\"stretch_width\",\n",
")\n",
"layout = pn.Column(file_input, table)\n",
"layout"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Annotation Tools"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sl_plant = pn.widgets.Select(name=\"Plant\", options=[])\n",
"@pn.depends(table.param.value, watch=True)\n",
"def on_file_loaded(file):\n",
" sl_plant.options = list(table.value.plant.unique())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Render"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"template.sidebar.append(\n",
" pn.Card(pn.Column(template_download, file_input), title=\"File Manager\"),\n",
")\n",
"template.sidebar.append(sl_plant)\n",
"\n",
"template.main.append(table)\n",
"\n",
"template.servable()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "env",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}