{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Ralstonia Annotation Tool" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n" ] } ], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "from io import StringIO\n", "import json\n", "from datetime import datetime as dt\n", "\n", "import pandas as pd\n", "import panel as pn\n", "\n", "import scripts.tap_const as tc\n", "import scripts.tap_image as ti" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "application/javascript": "(function(root) {\n function now() {\n return new Date();\n }\n\n var force = true;\n var py_version = '3.4.3'.replace('rc', '-rc.').replace('.dev', '-dev.');\n var reloading = false;\n var Bokeh = root.Bokeh;\n\n if (typeof (root._bokeh_timeout) === \"undefined\" || force) {\n root._bokeh_timeout = Date.now() + 5000;\n root._bokeh_failed_load = false;\n }\n\n function run_callbacks() {\n try {\n root._bokeh_onload_callbacks.forEach(function(callback) {\n if (callback != null)\n callback();\n });\n } finally {\n delete root._bokeh_onload_callbacks;\n }\n console.debug(\"Bokeh: all callbacks have finished\");\n }\n\n function load_libs(css_urls, js_urls, js_modules, js_exports, callback) {\n if (css_urls == null) css_urls = [];\n if (js_urls == null) js_urls = [];\n if (js_modules == null) js_modules = [];\n if (js_exports == null) js_exports = {};\n\n root._bokeh_onload_callbacks.push(callback);\n\n if (root._bokeh_is_loading > 0) {\n console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n return null;\n }\n if (js_urls.length === 0 && js_modules.length === 0 && Object.keys(js_exports).length === 0) {\n run_callbacks();\n return null;\n }\n if (!reloading) {\n console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n }\n\n function on_load() {\n root._bokeh_is_loading--;\n if (root._bokeh_is_loading === 0) {\n console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n run_callbacks()\n }\n }\n window._bokeh_on_load = on_load\n\n function on_error() {\n console.error(\"failed to load \" + url);\n }\n\n var skip = [];\n if (window.requirejs) {\n window.requirejs.config({'packages': {}, 'paths': {'tabulator': 'https://cdn.jsdelivr.net/npm/tabulator-tables@5.5.0/dist/js/tabulator.min', 'moment': 'https://cdn.jsdelivr.net/npm/luxon/build/global/luxon.min'}, 'shim': {}});\n require([\"tabulator\"], function(Tabulator) {\n\twindow.Tabulator = Tabulator\n\ton_load()\n })\n require([\"moment\"], function(moment) {\n\twindow.moment = moment\n\ton_load()\n })\n root._bokeh_is_loading = css_urls.length + 2;\n } else {\n root._bokeh_is_loading = css_urls.length + js_urls.length + js_modules.length + Object.keys(js_exports).length;\n }\n\n var existing_stylesheets = []\n var links = document.getElementsByTagName('link')\n for (var i = 0; i < links.length; i++) {\n var link = links[i]\n if (link.href != null) {\n\texisting_stylesheets.push(link.href)\n }\n }\n for (var i = 0; i < css_urls.length; i++) {\n var url = css_urls[i];\n if (existing_stylesheets.indexOf(url) !== -1) {\n\ton_load()\n\tcontinue;\n }\n const element = document.createElement(\"link\");\n element.onload = on_load;\n element.onerror = on_error;\n element.rel = \"stylesheet\";\n element.type = \"text/css\";\n element.href = url;\n console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n document.body.appendChild(element);\n } if (((window.Tabulator !== undefined) && (!(window.Tabulator instanceof HTMLElement))) || window.requirejs) {\n var urls = ['https://cdn.holoviz.org/panel/1.4.5/dist/bundled/datatabulator/tabulator-tables@5.5.0/dist/js/tabulator.min.js'];\n for (var i = 0; i < urls.length; i++) {\n skip.push(urls[i])\n }\n } if (((window.moment !== undefined) && (!(window.moment instanceof HTMLElement))) || window.requirejs) {\n var urls = ['https://cdn.holoviz.org/panel/1.4.5/dist/bundled/datatabulator/luxon/build/global/luxon.min.js'];\n for (var i = 0; i < urls.length; i++) {\n skip.push(urls[i])\n }\n } var existing_scripts = []\n var scripts = document.getElementsByTagName('script')\n for (var i = 0; i < scripts.length; i++) {\n var script = scripts[i]\n if (script.src != null) {\n\texisting_scripts.push(script.src)\n }\n }\n for (var i = 0; i < js_urls.length; i++) {\n var url = js_urls[i];\n if (skip.indexOf(url) !== -1 || existing_scripts.indexOf(url) !== -1) {\n\tif (!window.requirejs) {\n\t on_load();\n\t}\n\tcontinue;\n }\n var element = document.createElement('script');\n element.onload = on_load;\n element.onerror = on_error;\n element.async = false;\n element.src = url;\n console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n document.head.appendChild(element);\n }\n for (var i = 0; i < js_modules.length; i++) {\n var url = js_modules[i];\n if (skip.indexOf(url) !== -1 || existing_scripts.indexOf(url) !== -1) {\n\tif (!window.requirejs) {\n\t on_load();\n\t}\n\tcontinue;\n }\n var element = document.createElement('script');\n element.onload = on_load;\n element.onerror = on_error;\n element.async = false;\n element.src = url;\n element.type = \"module\";\n console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n document.head.appendChild(element);\n }\n for (const name in js_exports) {\n var url = js_exports[name];\n if (skip.indexOf(url) >= 0 || root[name] != null) {\n\tif (!window.requirejs) {\n\t on_load();\n\t}\n\tcontinue;\n }\n var element = document.createElement('script');\n element.onerror = on_error;\n element.async = false;\n element.type = \"module\";\n console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n element.textContent = `\n import ${name} from \"${url}\"\n window.${name} = ${name}\n window._bokeh_on_load()\n `\n document.head.appendChild(element);\n }\n if (!js_urls.length && !js_modules.length) {\n on_load()\n }\n };\n\n function inject_raw_css(css) {\n const element = document.createElement(\"style\");\n element.appendChild(document.createTextNode(css));\n document.body.appendChild(element);\n }\n\n var js_urls = [\"https://cdn.holoviz.org/panel/1.4.5/dist/bundled/datatabulator/tabulator-tables@5.5.0/dist/js/tabulator.min.js\", \"https://cdn.holoviz.org/panel/1.4.5/dist/bundled/datatabulator/luxon/build/global/luxon.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-3.4.3.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-3.4.3.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-3.4.3.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-3.4.3.min.js\", \"https://cdn.holoviz.org/panel/1.4.5/dist/panel.min.js\"];\n var js_modules = [];\n var js_exports = {};\n var css_urls = [\"https://cdn.holoviz.org/panel/1.4.5/dist/bundled/datatabulator/tabulator-tables@5.5.0/dist/css/tabulator_simple.min.css?v=1.4.5\"];\n var inline_js = [ function(Bokeh) {\n Bokeh.set_log_level(\"info\");\n },\nfunction(Bokeh) {} // ensure no trailing comma for IE\n ];\n\n function run_inline_js() {\n if ((root.Bokeh !== undefined) || (force === true)) {\n for (var i = 0; i < inline_js.length; i++) {\n\ttry {\n inline_js[i].call(root, root.Bokeh);\n\t} catch(e) {\n\t if (!reloading) {\n\t throw e;\n\t }\n\t}\n }\n // Cache old bokeh versions\n if (Bokeh != undefined && !reloading) {\n\tvar NewBokeh = root.Bokeh;\n\tif (Bokeh.versions === undefined) {\n\t Bokeh.versions = new Map();\n\t}\n\tif (NewBokeh.version !== Bokeh.version) {\n\t Bokeh.versions.set(NewBokeh.version, NewBokeh)\n\t}\n\troot.Bokeh = Bokeh;\n }} else if (Date.now() < root._bokeh_timeout) {\n setTimeout(run_inline_js, 100);\n } else if (!root._bokeh_failed_load) {\n console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n root._bokeh_failed_load = true;\n }\n root._bokeh_is_initializing = false\n }\n\n function load_or_wait() {\n // Implement a backoff loop that tries to ensure we do not load multiple\n // versions of Bokeh and its dependencies at the same time.\n // In recent versions we use the root._bokeh_is_initializing flag\n // to determine whether there is an ongoing attempt to initialize\n // bokeh, however for backward compatibility we also try to ensure\n // that we do not start loading a newer (Panel>=1.0 and Bokeh>3) version\n // before older versions are fully initialized.\n if (root._bokeh_is_initializing && Date.now() > root._bokeh_timeout) {\n root._bokeh_is_initializing = false;\n root._bokeh_onload_callbacks = undefined;\n console.log(\"Bokeh: BokehJS was loaded multiple times but one version failed to initialize.\");\n load_or_wait();\n } else if (root._bokeh_is_initializing || (typeof root._bokeh_is_initializing === \"undefined\" && root._bokeh_onload_callbacks !== undefined)) {\n setTimeout(load_or_wait, 100);\n } else {\n root._bokeh_is_initializing = true\n root._bokeh_onload_callbacks = []\n var bokeh_loaded = Bokeh != null && (Bokeh.version === py_version || (Bokeh.versions !== undefined && Bokeh.versions.has(py_version)));\n if (!reloading && !bokeh_loaded) {\n\troot.Bokeh = undefined;\n }\n load_libs(css_urls, js_urls, js_modules, js_exports, function() {\n\tconsole.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n\trun_inline_js();\n });\n }\n }\n // Give older versions of the autoload script a head-start to ensure\n // they initialize before we start loading newer version.\n setTimeout(load_or_wait, 100)\n}(window));", "application/vnd.holoviews_load.v0+json": "" }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": "\nif ((window.PyViz === undefined) || (window.PyViz instanceof HTMLElement)) {\n window.PyViz = {comms: {}, comm_status:{}, kernels:{}, receivers: {}, plot_index: []}\n}\n\n\n function JupyterCommManager() {\n }\n\n JupyterCommManager.prototype.register_target = function(plot_id, comm_id, msg_handler) {\n if (window.comm_manager || ((window.Jupyter !== undefined) && (Jupyter.notebook.kernel != null))) {\n var comm_manager = window.comm_manager || Jupyter.notebook.kernel.comm_manager;\n comm_manager.register_target(comm_id, function(comm) {\n comm.on_msg(msg_handler);\n });\n } else if ((plot_id in window.PyViz.kernels) && (window.PyViz.kernels[plot_id])) {\n window.PyViz.kernels[plot_id].registerCommTarget(comm_id, function(comm) {\n comm.onMsg = msg_handler;\n });\n } else if (typeof google != 'undefined' && google.colab.kernel != null) {\n google.colab.kernel.comms.registerTarget(comm_id, (comm) => {\n var messages = comm.messages[Symbol.asyncIterator]();\n function processIteratorResult(result) {\n var message = result.value;\n console.log(message)\n var content = {data: message.data, comm_id};\n var buffers = []\n for (var buffer of message.buffers || []) {\n buffers.push(new DataView(buffer))\n }\n var metadata = message.metadata || {};\n var msg = {content, buffers, metadata}\n msg_handler(msg);\n return messages.next().then(processIteratorResult);\n }\n return messages.next().then(processIteratorResult);\n })\n }\n }\n\n JupyterCommManager.prototype.get_client_comm = function(plot_id, comm_id, msg_handler) {\n if (comm_id in window.PyViz.comms) {\n return window.PyViz.comms[comm_id];\n } else if (window.comm_manager || ((window.Jupyter !== undefined) && (Jupyter.notebook.kernel != null))) {\n var comm_manager = window.comm_manager || Jupyter.notebook.kernel.comm_manager;\n var comm = comm_manager.new_comm(comm_id, {}, {}, {}, comm_id);\n if (msg_handler) {\n comm.on_msg(msg_handler);\n }\n } else if ((plot_id in window.PyViz.kernels) && (window.PyViz.kernels[plot_id])) {\n var comm = window.PyViz.kernels[plot_id].connectToComm(comm_id);\n comm.open();\n if (msg_handler) {\n comm.onMsg = msg_handler;\n }\n } else if (typeof google != 'undefined' && google.colab.kernel != null) {\n var comm_promise = google.colab.kernel.comms.open(comm_id)\n comm_promise.then((comm) => {\n window.PyViz.comms[comm_id] = comm;\n if (msg_handler) {\n var messages = comm.messages[Symbol.asyncIterator]();\n function processIteratorResult(result) {\n var message = result.value;\n var content = {data: message.data};\n var metadata = message.metadata || {comm_id};\n var msg = {content, metadata}\n msg_handler(msg);\n return messages.next().then(processIteratorResult);\n }\n return messages.next().then(processIteratorResult);\n }\n }) \n var sendClosure = (data, metadata, buffers, disposeOnDone) => {\n return comm_promise.then((comm) => {\n comm.send(data, metadata, buffers, disposeOnDone);\n });\n };\n var comm = {\n send: sendClosure\n };\n }\n window.PyViz.comms[comm_id] = comm;\n return comm;\n }\n window.PyViz.comm_manager = new JupyterCommManager();\n \n\n\nvar JS_MIME_TYPE = 'application/javascript';\nvar HTML_MIME_TYPE = 'text/html';\nvar EXEC_MIME_TYPE = 'application/vnd.holoviews_exec.v0+json';\nvar CLASS_NAME = 'output';\n\n/**\n * Render data to the DOM node\n */\nfunction render(props, node) {\n var div = document.createElement(\"div\");\n var script = document.createElement(\"script\");\n node.appendChild(div);\n node.appendChild(script);\n}\n\n/**\n * Handle when a new output is added\n */\nfunction handle_add_output(event, handle) {\n var output_area = handle.output_area;\n var output = handle.output;\n if ((output.data == undefined) || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {\n return\n }\n var id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n var toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n if (id !== undefined) {\n var nchildren = toinsert.length;\n var html_node = toinsert[nchildren-1].children[0];\n html_node.innerHTML = output.data[HTML_MIME_TYPE];\n var scripts = [];\n var nodelist = html_node.querySelectorAll(\"script\");\n for (var i in nodelist) {\n if (nodelist.hasOwnProperty(i)) {\n scripts.push(nodelist[i])\n }\n }\n\n scripts.forEach( function (oldScript) {\n var newScript = document.createElement(\"script\");\n var attrs = [];\n var nodemap = oldScript.attributes;\n for (var j in nodemap) {\n if (nodemap.hasOwnProperty(j)) {\n attrs.push(nodemap[j])\n }\n }\n attrs.forEach(function(attr) { newScript.setAttribute(attr.name, attr.value) });\n newScript.appendChild(document.createTextNode(oldScript.innerHTML));\n oldScript.parentNode.replaceChild(newScript, oldScript);\n });\n if (JS_MIME_TYPE in output.data) {\n toinsert[nchildren-1].children[1].textContent = output.data[JS_MIME_TYPE];\n }\n output_area._hv_plot_id = id;\n if ((window.Bokeh !== undefined) && (id in Bokeh.index)) {\n window.PyViz.plot_index[id] = Bokeh.index[id];\n } else {\n window.PyViz.plot_index[id] = null;\n }\n } else if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n var bk_div = document.createElement(\"div\");\n bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n var script_attrs = bk_div.children[0].attributes;\n for (var i = 0; i < script_attrs.length; i++) {\n toinsert[toinsert.length - 1].childNodes[1].setAttribute(script_attrs[i].name, script_attrs[i].value);\n }\n // store reference to server id on output_area\n output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n }\n}\n\n/**\n * Handle when an output is cleared or removed\n */\nfunction handle_clear_output(event, handle) {\n var id = handle.cell.output_area._hv_plot_id;\n var server_id = handle.cell.output_area._bokeh_server_id;\n if (((id === undefined) || !(id in PyViz.plot_index)) && (server_id !== undefined)) { return; }\n var comm = window.PyViz.comm_manager.get_client_comm(\"hv-extension-comm\", \"hv-extension-comm\", function () {});\n if (server_id !== null) {\n comm.send({event_type: 'server_delete', 'id': server_id});\n return;\n } else if (comm !== null) {\n comm.send({event_type: 'delete', 'id': id});\n }\n delete PyViz.plot_index[id];\n if ((window.Bokeh !== undefined) & (id in window.Bokeh.index)) {\n var doc = window.Bokeh.index[id].model.document\n doc.clear();\n const i = window.Bokeh.documents.indexOf(doc);\n if (i > -1) {\n window.Bokeh.documents.splice(i, 1);\n }\n }\n}\n\n/**\n * Handle kernel restart event\n */\nfunction handle_kernel_cleanup(event, handle) {\n delete PyViz.comms[\"hv-extension-comm\"];\n window.PyViz.plot_index = {}\n}\n\n/**\n * Handle update_display_data messages\n */\nfunction handle_update_output(event, handle) {\n handle_clear_output(event, {cell: {output_area: handle.output_area}})\n handle_add_output(event, handle)\n}\n\nfunction register_renderer(events, OutputArea) {\n function append_mime(data, metadata, element) {\n // create a DOM node to render to\n var toinsert = this.create_output_subarea(\n metadata,\n CLASS_NAME,\n EXEC_MIME_TYPE\n );\n this.keyboard_manager.register_events(toinsert);\n // Render to node\n var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n render(props, toinsert[0]);\n element.append(toinsert);\n return toinsert\n }\n\n events.on('output_added.OutputArea', handle_add_output);\n events.on('output_updated.OutputArea', handle_update_output);\n events.on('clear_output.CodeCell', handle_clear_output);\n events.on('delete.Cell', handle_clear_output);\n events.on('kernel_ready.Kernel', handle_kernel_cleanup);\n\n OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n safe: true,\n index: 0\n });\n}\n\nif (window.Jupyter !== undefined) {\n try {\n var events = require('base/js/events');\n var OutputArea = require('notebook/js/outputarea').OutputArea;\n if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n register_renderer(events, OutputArea);\n }\n } catch(err) {\n }\n}\n", "application/vnd.holoviews_load.v0+json": "" }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.holoviews_exec.v0+json": "", "text/html": [ "
\n", "
\n", "
\n", "" ] }, "metadata": { "application/vnd.holoviews_exec.v0+json": { "id": "a47cdb5e-d733-4f9b-bd1b-f250f1a7dd25" } }, "output_type": "display_data" } ], "source": [ "pn.extension(\"tabulator\")\n", "\n", "template = pn.template.BootstrapTemplate(title=\"Ralstonia Annotation Tool\")" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "pd.set_option(\"display.max_colwidth\", 500)\n", "pd.set_option(\"display.max_columns\", 500)\n", "pd.set_option(\"display.width\", 1000)\n", "pd.set_option(\"display.max_rows\", 20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Constants" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "EXPERIMENT = \"72AC_PhD_2404\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "INDEX_KEY = \"date_time\"" ] }, { "cell_type": "code", "execution_count": 20, "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": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "treatments = {\n", " \"Ralstonia\": \"RS\",\n", " \"Control\": \"CT\",\n", " \"Hydric Stress\": \"HS\",\n", " \"DC3000\": \"DC\",\n", "}" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
experimentplantcameradate_timeanglewavelengthheightjob_idfilepathrobotdatetimehourplant_idPositionPlanteplaquepositiontreatmentxytimestampfile_name
072AC_PhD_240472AC73_CL_RS_XXcam_scan_phenopsis2024-07-27 10:06:090SW7550227372AC_PhD_2404#20240727120609#72AC73_CL_RS_XX#LIN#0#SW755#2273#0.tifphenopsis2024-07-2710:06:09.6010001273P3_A131RS1312024-07-27 10:06:09.601000+00:0072AC_PhD_2404#20240727120609#72AC73_CL_RS_XX#LIN#0#SW755#2273#0.jpg
172AC_PhD_240472AC73_CL_RS_XXcam_scan_phenopsis2024-07-27 13:06:100SW7550227472AC_PhD_2404#20240727150610#72AC73_CL_RS_XX#LIN#0#SW755#2274#0.tifphenopsis2024-07-2713:06:10.8110001573P3_A131RS1312024-07-27 13:06:10.811000+00:0072AC_PhD_2404#20240727150610#72AC73_CL_RS_XX#LIN#0#SW755#2274#0.jpg
272AC_PhD_240472AC73_CL_RS_XXcam_scan_phenopsis2024-07-28 10:06:110SW7550227772AC_PhD_2404#20240728120610#72AC73_CL_RS_XX#LIN#0#SW755#2277#0.tifphenopsis2024-07-2810:06:11.1330001273P3_A131RS1312024-07-28 10:06:11.133000+00:0072AC_PhD_2404#20240728120610#72AC73_CL_RS_XX#LIN#0#SW755#2277#0.jpg
372AC_PhD_240472AC73_CL_RS_XXcam_scan_phenopsis2024-07-28 13:06:120SW7550227872AC_PhD_2404#20240728150612#72AC73_CL_RS_XX#LIN#0#SW755#2278#0.tifphenopsis2024-07-2813:06:12.6780001573P3_A131RS1312024-07-28 13:06:12.678000+00:0072AC_PhD_2404#20240728150612#72AC73_CL_RS_XX#LIN#0#SW755#2278#0.jpg
472AC_PhD_240472AC73_CL_RS_XXcam_scan_phenopsis2024-07-29 10:06:100SW7550228172AC_PhD_2404#20240729120609#72AC73_CL_RS_XX#LIN#0#SW755#2281#0.tifphenopsis2024-07-2910:06:10.2040001273P3_A131RS1312024-07-29 10:06:10.204000+00:0072AC_PhD_2404#20240729120609#72AC73_CL_RS_XX#LIN#0#SW755#2281#0.jpg
.....................................................................
604372AC_PhD_240472AC468_CL_DC_XXcam_scan_phenopsis2024-08-05 10:37:340SW7550231072AC_PhD_2404#20240805123734#72AC468_CL_DC_XX#LIN#0#SW755#2310#0.tifphenopsis2024-08-0510:37:34.72100012468P13_A361336DC36122024-08-05 10:37:34.721000+00:0072AC_PhD_2404#20240805123734#72AC468_CL_DC_XX#LIN#0#SW755#2310#0.jpg
604472AC_PhD_240472AC468_CL_DC_XXcam_scan_phenopsis2024-08-05 13:37:350SW7550231172AC_PhD_2404#20240805153734#72AC468_CL_DC_XX#LIN#0#SW755#2311#0.tifphenopsis2024-08-0513:37:35.10000015468P13_A361336DC36122024-08-05 13:37:35.100000+00:0072AC_PhD_2404#20240805153734#72AC468_CL_DC_XX#LIN#0#SW755#2311#0.jpg
604572AC_PhD_240472AC468_CL_DC_XXcam_scan_phenopsis2024-08-06 10:37:300SW7550231472AC_PhD_2404#20240806123730#72AC468_CL_DC_XX#LIN#0#SW755#2314#0.tifphenopsis2024-08-0610:37:30.91800012468P13_A361336DC36122024-08-06 10:37:30.918000+00:0072AC_PhD_2404#20240806123730#72AC468_CL_DC_XX#LIN#0#SW755#2314#0.jpg
604672AC_PhD_240472AC468_CL_DC_XXcam_scan_phenopsis2024-08-06 13:37:350SW7550231572AC_PhD_2404#20240806153734#72AC468_CL_DC_XX#LIN#0#SW755#2315#0.tifphenopsis2024-08-0613:37:35.62900015468P13_A361336DC36122024-08-06 13:37:35.629000+00:0072AC_PhD_2404#20240806153734#72AC468_CL_DC_XX#LIN#0#SW755#2315#0.jpg
604772AC_PhD_240472AC468_CL_DC_XXcam_scan_phenopsis2024-08-07 10:37:360SW7550231872AC_PhD_2404#20240807123735#72AC468_CL_DC_XX#LIN#0#SW755#2318#0.tifphenopsis2024-08-0710:37:36.14600012468P13_A361336DC36122024-08-07 10:37:36.146000+00:0072AC_PhD_2404#20240807123735#72AC468_CL_DC_XX#LIN#0#SW755#2318#0.jpg
\n", "

6048 rows Ă— 22 columns

\n", "
" ], "text/plain": [ " experiment plant camera date_time angle wavelength height job_id filepath robot date time hour plant_id PositionPlante plaque position treatment x y timestamp file_name\n", "0 72AC_PhD_2404 72AC73_CL_RS_XX cam_scan_phenopsis 2024-07-27 10:06:09 0 SW755 0 2273 72AC_PhD_2404#20240727120609#72AC73_CL_RS_XX#LIN#0#SW755#2273#0.tif phenopsis 2024-07-27 10:06:09.601000 12 73 P3_A1 3 1 RS 13 1 2024-07-27 10:06:09.601000+00:00 72AC_PhD_2404#20240727120609#72AC73_CL_RS_XX#LIN#0#SW755#2273#0.jpg\n", "1 72AC_PhD_2404 72AC73_CL_RS_XX cam_scan_phenopsis 2024-07-27 13:06:10 0 SW755 0 2274 72AC_PhD_2404#20240727150610#72AC73_CL_RS_XX#LIN#0#SW755#2274#0.tif phenopsis 2024-07-27 13:06:10.811000 15 73 P3_A1 3 1 RS 13 1 2024-07-27 13:06:10.811000+00:00 72AC_PhD_2404#20240727150610#72AC73_CL_RS_XX#LIN#0#SW755#2274#0.jpg\n", "2 72AC_PhD_2404 72AC73_CL_RS_XX cam_scan_phenopsis 2024-07-28 10:06:11 0 SW755 0 2277 72AC_PhD_2404#20240728120610#72AC73_CL_RS_XX#LIN#0#SW755#2277#0.tif phenopsis 2024-07-28 10:06:11.133000 12 73 P3_A1 3 1 RS 13 1 2024-07-28 10:06:11.133000+00:00 72AC_PhD_2404#20240728120610#72AC73_CL_RS_XX#LIN#0#SW755#2277#0.jpg\n", "3 72AC_PhD_2404 72AC73_CL_RS_XX cam_scan_phenopsis 2024-07-28 13:06:12 0 SW755 0 2278 72AC_PhD_2404#20240728150612#72AC73_CL_RS_XX#LIN#0#SW755#2278#0.tif phenopsis 2024-07-28 13:06:12.678000 15 73 P3_A1 3 1 RS 13 1 2024-07-28 13:06:12.678000+00:00 72AC_PhD_2404#20240728150612#72AC73_CL_RS_XX#LIN#0#SW755#2278#0.jpg\n", "4 72AC_PhD_2404 72AC73_CL_RS_XX cam_scan_phenopsis 2024-07-29 10:06:10 0 SW755 0 2281 72AC_PhD_2404#20240729120609#72AC73_CL_RS_XX#LIN#0#SW755#2281#0.tif phenopsis 2024-07-29 10:06:10.204000 12 73 P3_A1 3 1 RS 13 1 2024-07-29 10:06:10.204000+00:00 72AC_PhD_2404#20240729120609#72AC73_CL_RS_XX#LIN#0#SW755#2281#0.jpg\n", "... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... .. .. ... ...\n", "6043 72AC_PhD_2404 72AC468_CL_DC_XX cam_scan_phenopsis 2024-08-05 10:37:34 0 SW755 0 2310 72AC_PhD_2404#20240805123734#72AC468_CL_DC_XX#LIN#0#SW755#2310#0.tif phenopsis 2024-08-05 10:37:34.721000 12 468 P13_A36 13 36 DC 36 12 2024-08-05 10:37:34.721000+00:00 72AC_PhD_2404#20240805123734#72AC468_CL_DC_XX#LIN#0#SW755#2310#0.jpg\n", "6044 72AC_PhD_2404 72AC468_CL_DC_XX cam_scan_phenopsis 2024-08-05 13:37:35 0 SW755 0 2311 72AC_PhD_2404#20240805153734#72AC468_CL_DC_XX#LIN#0#SW755#2311#0.tif phenopsis 2024-08-05 13:37:35.100000 15 468 P13_A36 13 36 DC 36 12 2024-08-05 13:37:35.100000+00:00 72AC_PhD_2404#20240805153734#72AC468_CL_DC_XX#LIN#0#SW755#2311#0.jpg\n", "6045 72AC_PhD_2404 72AC468_CL_DC_XX cam_scan_phenopsis 2024-08-06 10:37:30 0 SW755 0 2314 72AC_PhD_2404#20240806123730#72AC468_CL_DC_XX#LIN#0#SW755#2314#0.tif phenopsis 2024-08-06 10:37:30.918000 12 468 P13_A36 13 36 DC 36 12 2024-08-06 10:37:30.918000+00:00 72AC_PhD_2404#20240806123730#72AC468_CL_DC_XX#LIN#0#SW755#2314#0.jpg\n", "6046 72AC_PhD_2404 72AC468_CL_DC_XX cam_scan_phenopsis 2024-08-06 13:37:35 0 SW755 0 2315 72AC_PhD_2404#20240806153734#72AC468_CL_DC_XX#LIN#0#SW755#2315#0.tif phenopsis 2024-08-06 13:37:35.629000 15 468 P13_A36 13 36 DC 36 12 2024-08-06 13:37:35.629000+00:00 72AC_PhD_2404#20240806153734#72AC468_CL_DC_XX#LIN#0#SW755#2315#0.jpg\n", "6047 72AC_PhD_2404 72AC468_CL_DC_XX cam_scan_phenopsis 2024-08-07 10:37:36 0 SW755 0 2318 72AC_PhD_2404#20240807123735#72AC468_CL_DC_XX#LIN#0#SW755#2318#0.tif phenopsis 2024-08-07 10:37:36.146000 12 468 P13_A36 13 36 DC 36 12 2024-08-07 10:37:36.146000+00:00 72AC_PhD_2404#20240807123735#72AC468_CL_DC_XX#LIN#0#SW755#2318#0.jpg\n", "\n", "[6048 rows x 22 columns]" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# pd.read_csv(pt_data.joinpath(f\"{EXPERIMENT}_raw.csv\"), sep=\";\").assign(date_time=lambda x: x.date_time.str.split(\".\", expand=True)[0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## User Interface" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Widgets" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "dwn_template = 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", " sizing_mode=\"stretch_width\",\n", ")\n", "\n", "file_input = pn.widgets.FileInput(accept=\".csv,.json\", sizing_mode=\"stretch_width\")\n", "table = pn.widgets.Tabulator(\n", " value=pd.DataFrame(),\n", " pagination=\"local\",\n", " page_size=20,\n", " sizing_mode=\"stretch_width\",\n", ")\n", "\n", "\n", "sl_treatment = pn.widgets.Select(\n", " name=\"Treatment\", options=list(treatments.keys()), value=\"Ralstonia\"\n", ")\n", "sl_plant = pn.widgets.Select(name=\"Plant\", options=[])\n", "ck_show_finished = pn.widgets.Checkbox(name=\"Show completed plants\", value=False)\n", "\n", "im_current = pn.pane.Image(max_width=800, max_height=800, sizing_mode=\"stretch_width\")\n", "discrete_slider = pn.widgets.DiscreteSlider(\n", " name=\"Discrete Player\", options=[0], value=0\n", ")\n", "bt_previous = pn.widgets.ButtonIcon(icon=\"caret-left\", size=\"4em\", toggle_duration=500)\n", "bt_next = pn.widgets.ButtonIcon(icon=\"caret-right\", size=\"4em\", toggle_duration=500)\n", "ii_disease_index = pn.widgets.IntInput(\n", " name=\"Disease Index\",\n", " start=0,\n", " end=4,\n", " step=1,\n", " value=0,\n", " max_width=80,\n", " sizing_mode=\"stretch_width\",\n", ")\n", "\n", "dwn_annotations = pn.widgets.FileDownload(\n", " file=pt_data.joinpath(f\"{EXPERIMENT}_raw.csv\"),\n", " filename=f\"{EXPERIMENT}_raw.csv\",\n", " label=\"Download\",\n", " name=\"Download Annotations\",\n", " sizing_mode=\"stretch_width\",\n", " icon=\"file-download\",\n", " button_type=\"primary\",\n", ")\n", "pg_completion = pn.indicators.Progress(name=\"Annotation progress\", value=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Callbacks" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "updating: bool = False\n", "\n", "\n", "@pn.depends(file_input.param.value, watch=True)\n", "def on_file_loaded(value):\n", " if not value or not isinstance(value, bytes):\n", " return pd.DataFrame()\n", "\n", " string_io = StringIO(value.decode(\"utf8\"))\n", " ret = pd.read_csv(string_io, sep=\";\").assign(\n", " file_name=lambda x: x.filepath.str.replace(\".tif\", \".jpg\"),\n", " date_time=lambda x: x.date_time.str.split(\".\", expand=True)[0],\n", " )\n", " if \"di\" not in ret:\n", " ret[\"di\"] = 0\n", " if \"done\" not in ret:\n", " ret[\"done\"] = False\n", "\n", " table.value = ret\n", "\n", "\n", "@pn.depends(\n", " table.param.value,\n", " ck_show_finished.param.value,\n", " sl_treatment.param.value,\n", " watch=True,\n", ")\n", "def on_table_changed(file: str, show_done: bool, treatment):\n", " df = table.value\n", " if \"done\" in df and \"treatment\" in df:\n", " df = df[df.treatment == treatments[treatment]]\n", " sl_plant.options = list(\n", " df.plant.unique()\n", " if show_done is True\n", " else df[df.done == False].plant.unique()\n", " )\n", "\n", "\n", "@pn.cache(max_items=10, policy=\"LRU\")\n", "def get_plant_data(df, plant_name) -> pd.DataFrame:\n", " file_names, rotations = [], []\n", " for k, v in json.load(\n", " open(pt_rotations.joinpath(sl_plant.value).with_suffix(\".json\"), \"r\")\n", " ).items():\n", " file_names.append(k + \".jpg\")\n", " rotations.append(v)\n", "\n", " return df[df.plant == plant_name].merge(\n", " pd.DataFrame(data={\"file_name\": file_names, \"rotation\": rotations}),\n", " on=\"file_name\",\n", " how=\"left\",\n", " )\n", "\n", "\n", "def update_image():\n", " global updating\n", " updating = True\n", " try:\n", " row = (\n", " get_plant_data(df=table.value, plant_name=sl_plant.value)\n", " .set_index(INDEX_KEY)\n", " .loc[discrete_slider.value]\n", " )\n", " if pt_images.joinpath(row.file_name).is_file() is True:\n", " im_current.object = ti.to_pil(\n", " ti.rotate_image(\n", " image=ti.load_image(file_name=row.file_name, file_path=pt_images),\n", " angle=row.rotation,\n", " )\n", " )\n", " ii_disease_index.value = row.di\n", " else:\n", " im_current.object = None\n", " finally:\n", " updating = False\n", "\n", "\n", "@pn.depends(sl_plant.param.value, watch=True)\n", "def on_plant_changed(plant_name):\n", " discrete_slider.options = get_plant_data(df=table.value, plant_name=plant_name)[\n", " INDEX_KEY\n", " ].to_list()\n", " discrete_slider.value = discrete_slider.options[0]\n", " update_image()\n", "\n", "\n", "@pn.depends(discrete_slider.param.value, watch=True)\n", "def on_index_changed(index):\n", " update_image()\n", "\n", "\n", "def do_previous(event):\n", " discrete_slider.value = discrete_slider.options[\n", " max(discrete_slider.options.index(discrete_slider.value) - 1, 0)\n", " ]\n", "\n", "\n", "def do_next(event):\n", " discrete_slider.value = discrete_slider.options[\n", " min(\n", " discrete_slider.options.index(discrete_slider.value) + 1,\n", " len(discrete_slider.options) - 1,\n", " )\n", " ]\n", "\n", "\n", "bt_previous.on_click(do_previous)\n", "bt_next.on_click(do_next)\n", "\n", "\n", "@pn.depends(ii_disease_index.param.value, watch=True)\n", "def on_di_changed(di):\n", " if updating is True:\n", " return\n", " table.value.loc[\n", " (table.value.plant == sl_plant.value)\n", " & (table.value[INDEX_KEY] >= discrete_slider.value),\n", " \"di\",\n", " ] = di\n", " table.value.loc[(table.value.plant == sl_plant.value), \"done\"] = True\n", "\n", " s_buf = StringIO()\n", " table.value.to_csv(s_buf, sep=\";\")\n", " s_buf.seek(0)\n", " dwn_annotations.filename = f\"{EXPERIMENT}_{dt.now().strftime('%Y%d%m_%H%M%S')}.csv\"\n", " dwn_annotations.file = s_buf\n", " dwn_annotations.visible = True\n", "\n", " pg_completion.max = len(table.value.plant.unique())\n", " pg_completion.value = len(table.value[table.value.done == True].plant.unique())" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3, 4, 5, 6]\n", "a.index(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Build Components" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "sidebar = pn.Column(\n", " pn.Card(\n", " pn.Column(\n", " pn.pane.Markdown(\"**Step 1:** Doawload the annaotation template file.\"),\n", " pn.pane.Alert(\n", " \"\"\"Only download the template the first time, changes are stored on your computer only.\"\"\",\n", " alert_type=\"danger\",\n", " ),\n", " dwn_template,\n", " pn.pane.Markdown(\"**Step 2:** Uplaod the downloaded template file.\"),\n", " file_input,\n", " pn.pane.Markdown(\n", " \"\"\"**Step 3:** Annotate images. \n", " Uppon finishing or before closing the app us the download button next to the disease index selector to download the annotations.\"\"\"\n", " ),\n", " pn.pane.Alert(\n", " \"If you want to resume an existing annotation session, upload your latest download.\",\n", " alert_type=\"info\",\n", " ),\n", " ),\n", " title=\"File Manager\",\n", " ),\n", " pn.WidgetBox(\n", " \"#### Plant selection\",\n", " sl_treatment,\n", " sl_plant,\n", " ck_show_finished,\n", " sizing_mode=\"stretch_width\",\n", " ),\n", " pn.WidgetBox(\n", " \"#### Annotation progress\", pg_completion, sizing_mode=\"stretch_width\"\n", " ),\n", ")\n", "main = pn.Column(\n", " im_current,\n", " pn.Row(bt_previous, discrete_slider, bt_next, ii_disease_index, dwn_annotations),\n", " max_width=800,\n", " max_height=800,\n", " sizing_mode=\"stretch_width\",\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Test" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "aaae447d85264800b41a5618d8e78171", "version_major": 2, "version_minor": 0 }, "text/plain": [ "BokehModel(combine_events=True, render_bundle={'docs_json': {'fbfa3e7d-2cea-465d-9844-cecbf0c4a154': {'version…" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# sidebar.width = 330\n", "pn.Row(sidebar, main)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Render" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "template.sidebar.append(sidebar)\n", "template.main.append(main)\n", "template.servable()" ] } ], "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 }