diff --git a/01.train_DecisionTree_PlanetaryComputer.ipynb b/01.train_DecisionTree_PlanetaryComputer.ipynb new file mode 100644 index 0000000..39292f0 --- /dev/null +++ b/01.train_DecisionTree_PlanetaryComputer.ipynb @@ -0,0 +1,2184 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "b1dab7f7", + "metadata": {}, + "source": [ + "# 🌍 Decision Tree Land Classification - Planetary Computer\n", + "\n", + "## 📌 Notebook này chạy trên:\n", + "- ✅ **Server ODC/JupyterHub** với Dask Gateway\n", + "- ✅ Load dữ liệu từ **Microsoft Planetary Computer** (không cần ODC database)\n", + "\n", + "## 🎯 Nguồn dữ liệu:\n", + "**Microsoft Planetary Computer STAC API**\n", + "- Sentinel-2 L2A (optical)\n", + "- Sentinel-1 RTC (SAR)\n", + "\n", + "## 🚀 Infrastructure:\n", + "- **Dask Gateway**: Adaptive scaling (1-10 workers)\n", + "- **Datacube**: Initialized nhưng không dùng để load data\n", + "- **S3 Access**: Configured với requester_pays\n", + "\n", + "## 🔄 Workflow:\n", + "1. Load data từ Planetary Computer (STAC API)\n", + "2. Preprocessing (cloud mask, NDVI, resampling)\n", + "3. Train Decision Tree model\n", + "4. Evaluate & save model\n", + "\n", + "---\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "df9820b8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": [ + "(function(root) {\n", + " function now() {\n", + " return new Date();\n", + " }\n", + "\n", + " const force = true;\n", + " const py_version = '3.6.0'.replace('rc', '-rc.').replace('.dev', '-dev.');\n", + " const reloading = false;\n", + " const Bokeh = root.Bokeh;\n", + "\n", + " // Set a timeout for this load but only if we are not already initializing\n", + " if (typeof (root._bokeh_timeout) === \"undefined\" || (force || !root._bokeh_is_initializing)) {\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", + " // Don't load bokeh if it is still initializing\n", + " console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n", + " return null;\n", + " } else if (js_urls.length === 0 && js_modules.length === 0 && Object.keys(js_exports).length === 0) {\n", + " // There is nothing to load\n", + " run_callbacks();\n", + " return null;\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(e) {\n", + " const src_el = e.srcElement\n", + " console.error(\"failed to load \" + (src_el.href || src_el.src));\n", + " }\n", + "\n", + " const skip = [];\n", + " if (window.requirejs) {\n", + " window.requirejs.config({'packages': {}, 'paths': {}, 'shim': {}});\n", + " root._bokeh_is_loading = css_urls.length + 0;\n", + " } else {\n", + " root._bokeh_is_loading = css_urls.length + js_urls.length + js_modules.length + Object.keys(js_exports).length;\n", + " }\n", + "\n", + " const existing_stylesheets = []\n", + " const links = document.getElementsByTagName('link')\n", + " for (let i = 0; i < links.length; i++) {\n", + " const link = links[i]\n", + " if (link.href != null) {\n", + " existing_stylesheets.push(link.href)\n", + " }\n", + " }\n", + " for (let i = 0; i < css_urls.length; i++) {\n", + " const url = css_urls[i];\n", + " const escaped = encodeURI(url)\n", + " if (existing_stylesheets.indexOf(escaped) !== -1) {\n", + " on_load()\n", + " continue;\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", + " } var existing_scripts = []\n", + " const scripts = document.getElementsByTagName('script')\n", + " for (let i = 0; i < scripts.length; i++) {\n", + " var script = scripts[i]\n", + " if (script.src != null) {\n", + " existing_scripts.push(script.src)\n", + " }\n", + " }\n", + " for (let i = 0; i < js_urls.length; i++) {\n", + " const url = js_urls[i];\n", + " const escaped = encodeURI(url)\n", + " if (skip.indexOf(escaped) !== -1 || existing_scripts.indexOf(escaped) !== -1) {\n", + " if (!window.requirejs) {\n", + " on_load();\n", + " }\n", + " continue;\n", + " }\n", + " const 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 (let i = 0; i < js_modules.length; i++) {\n", + " const url = js_modules[i];\n", + " const escaped = encodeURI(url)\n", + " if (skip.indexOf(escaped) !== -1 || existing_scripts.indexOf(escaped) !== -1) {\n", + " if (!window.requirejs) {\n", + " on_load();\n", + " }\n", + " continue;\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", + " const url = js_exports[name];\n", + " const escaped = encodeURI(url)\n", + " if (skip.indexOf(escaped) >= 0 || root[name] != null) {\n", + " if (!window.requirejs) {\n", + " on_load();\n", + " }\n", + " continue;\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", + " const js_urls = [\"https://cdn.holoviz.org/panel/1.5.3/dist/bundled/reactiveesm/es-module-shims@^1.10.0/dist/es-module-shims.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-3.6.0.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-3.6.0.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-3.6.0.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-3.6.0.min.js\", \"https://cdn.holoviz.org/panel/1.5.3/dist/panel.min.js\"];\n", + " const js_modules = [];\n", + " const js_exports = {};\n", + " const css_urls = [];\n", + " const inline_js = [ function(Bokeh) {\n", + " Bokeh.set_log_level(\"info\");\n", + " },\n", + "function(Bokeh) {} // ensure no trailing comma for IE\n", + " ];\n", + "\n", + " function run_inline_js() {\n", + " if ((root.Bokeh !== undefined) || (force === true)) {\n", + " for (let i = 0; i < inline_js.length; i++) {\n", + " try {\n", + " inline_js[i].call(root, root.Bokeh);\n", + " } catch(e) {\n", + " if (!reloading) {\n", + " throw e;\n", + " }\n", + " }\n", + " }\n", + " // Cache old bokeh versions\n", + " if (Bokeh != undefined && !reloading) {\n", + " var NewBokeh = root.Bokeh;\n", + " if (Bokeh.versions === undefined) {\n", + " Bokeh.versions = new Map();\n", + " }\n", + " if (NewBokeh.version !== Bokeh.version) {\n", + " Bokeh.versions.set(NewBokeh.version, NewBokeh)\n", + " }\n", + " root.Bokeh = Bokeh;\n", + " }\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", + " // If the timeout and bokeh was not successfully loaded we reset\n", + " // everything and try loading again\n", + " root._bokeh_timeout = Date.now() + 5000;\n", + " root._bokeh_is_initializing = false;\n", + " root._bokeh_onload_callbacks = undefined;\n", + " root._bokeh_is_loading = 0\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", + " const bokeh_loaded = root.Bokeh != null && (root.Bokeh.version === py_version || (root.Bokeh.versions !== undefined && root.Bokeh.versions.has(py_version)));\n", + " if (!reloading && !bokeh_loaded) {\n", + " if (root.Bokeh) {\n", + " root.Bokeh = undefined;\n", + " }\n", + " console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n", + " }\n", + " load_libs(css_urls, js_urls, js_modules, js_exports, function() {\n", + " console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n", + " run_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": "(function(root) {\n function now() {\n return new Date();\n }\n\n const force = true;\n const py_version = '3.6.0'.replace('rc', '-rc.').replace('.dev', '-dev.');\n const reloading = false;\n const Bokeh = root.Bokeh;\n\n // Set a timeout for this load but only if we are not already initializing\n if (typeof (root._bokeh_timeout) === \"undefined\" || (force || !root._bokeh_is_initializing)) {\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 // Don't load bokeh if it is still initializing\n console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n return null;\n } else if (js_urls.length === 0 && js_modules.length === 0 && Object.keys(js_exports).length === 0) {\n // There is nothing to load\n run_callbacks();\n return null;\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(e) {\n const src_el = e.srcElement\n console.error(\"failed to load \" + (src_el.href || src_el.src));\n }\n\n const skip = [];\n if (window.requirejs) {\n window.requirejs.config({'packages': {}, 'paths': {}, 'shim': {}});\n root._bokeh_is_loading = css_urls.length + 0;\n } else {\n root._bokeh_is_loading = css_urls.length + js_urls.length + js_modules.length + Object.keys(js_exports).length;\n }\n\n const existing_stylesheets = []\n const links = document.getElementsByTagName('link')\n for (let i = 0; i < links.length; i++) {\n const link = links[i]\n if (link.href != null) {\n existing_stylesheets.push(link.href)\n }\n }\n for (let i = 0; i < css_urls.length; i++) {\n const url = css_urls[i];\n const escaped = encodeURI(url)\n if (existing_stylesheets.indexOf(escaped) !== -1) {\n on_load()\n continue;\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 } var existing_scripts = []\n const scripts = document.getElementsByTagName('script')\n for (let i = 0; i < scripts.length; i++) {\n var script = scripts[i]\n if (script.src != null) {\n existing_scripts.push(script.src)\n }\n }\n for (let i = 0; i < js_urls.length; i++) {\n const url = js_urls[i];\n const escaped = encodeURI(url)\n if (skip.indexOf(escaped) !== -1 || existing_scripts.indexOf(escaped) !== -1) {\n if (!window.requirejs) {\n on_load();\n }\n continue;\n }\n const 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 (let i = 0; i < js_modules.length; i++) {\n const url = js_modules[i];\n const escaped = encodeURI(url)\n if (skip.indexOf(escaped) !== -1 || existing_scripts.indexOf(escaped) !== -1) {\n if (!window.requirejs) {\n on_load();\n }\n continue;\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 const url = js_exports[name];\n const escaped = encodeURI(url)\n if (skip.indexOf(escaped) >= 0 || root[name] != null) {\n if (!window.requirejs) {\n on_load();\n }\n continue;\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 const js_urls = [\"https://cdn.holoviz.org/panel/1.5.3/dist/bundled/reactiveesm/es-module-shims@^1.10.0/dist/es-module-shims.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-3.6.0.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-3.6.0.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-3.6.0.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-3.6.0.min.js\", \"https://cdn.holoviz.org/panel/1.5.3/dist/panel.min.js\"];\n const js_modules = [];\n const js_exports = {};\n const css_urls = [];\n const 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 (let i = 0; i < inline_js.length; i++) {\n try {\n inline_js[i].call(root, root.Bokeh);\n } catch(e) {\n if (!reloading) {\n throw e;\n }\n }\n }\n // Cache old bokeh versions\n if (Bokeh != undefined && !reloading) {\n var NewBokeh = root.Bokeh;\n if (Bokeh.versions === undefined) {\n Bokeh.versions = new Map();\n }\n if (NewBokeh.version !== Bokeh.version) {\n Bokeh.versions.set(NewBokeh.version, NewBokeh)\n }\n root.Bokeh = Bokeh;\n }\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 // If the timeout and bokeh was not successfully loaded we reset\n // everything and try loading again\n root._bokeh_timeout = Date.now() + 5000;\n root._bokeh_is_initializing = false;\n root._bokeh_onload_callbacks = undefined;\n root._bokeh_is_loading = 0\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 const bokeh_loaded = root.Bokeh != null && (root.Bokeh.version === py_version || (root.Bokeh.versions !== undefined && root.Bokeh.versions.has(py_version)));\n if (!reloading && !bokeh_loaded) {\n if (root.Bokeh) {\n root.Bokeh = undefined;\n }\n console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n }\n load_libs(css_urls, js_urls, js_modules, js_exports, function() {\n console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n run_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));" + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": [ + "\n", + "if ((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", + "\n", + "var JS_MIME_TYPE = 'application/javascript';\n", + "var HTML_MIME_TYPE = 'text/html';\n", + "var EXEC_MIME_TYPE = 'application/vnd.holoviews_exec.v0+json';\n", + "var CLASS_NAME = 'output';\n", + "\n", + "/**\n", + " * Render data to the DOM node\n", + " */\n", + "function 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", + " */\n", + "function 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", + " */\n", + "function 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", + " */\n", + "function 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", + " */\n", + "function handle_update_output(event, handle) {\n", + " handle_clear_output(event, {cell: {output_area: handle.output_area}})\n", + " handle_add_output(event, handle)\n", + "}\n", + "\n", + "function 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", + "\n", + "if (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": "\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" + }, + "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": "p1002" + } + }, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": [ + "(function(root) {\n", + " function now() {\n", + " return new Date();\n", + " }\n", + "\n", + " const force = false;\n", + " const py_version = '3.6.0'.replace('rc', '-rc.').replace('.dev', '-dev.');\n", + " const reloading = true;\n", + " const Bokeh = root.Bokeh;\n", + "\n", + " // Set a timeout for this load but only if we are not already initializing\n", + " if (typeof (root._bokeh_timeout) === \"undefined\" || (force || !root._bokeh_is_initializing)) {\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", + " // Don't load bokeh if it is still initializing\n", + " console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n", + " return null;\n", + " } else if (js_urls.length === 0 && js_modules.length === 0 && Object.keys(js_exports).length === 0) {\n", + " // There is nothing to load\n", + " run_callbacks();\n", + " return null;\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(e) {\n", + " const src_el = e.srcElement\n", + " console.error(\"failed to load \" + (src_el.href || src_el.src));\n", + " }\n", + "\n", + " const skip = [];\n", + " if (window.requirejs) {\n", + " window.requirejs.config({'packages': {}, 'paths': {}, 'shim': {}});\n", + " root._bokeh_is_loading = css_urls.length + 0;\n", + " } else {\n", + " root._bokeh_is_loading = css_urls.length + js_urls.length + js_modules.length + Object.keys(js_exports).length;\n", + " }\n", + "\n", + " const existing_stylesheets = []\n", + " const links = document.getElementsByTagName('link')\n", + " for (let i = 0; i < links.length; i++) {\n", + " const link = links[i]\n", + " if (link.href != null) {\n", + " existing_stylesheets.push(link.href)\n", + " }\n", + " }\n", + " for (let i = 0; i < css_urls.length; i++) {\n", + " const url = css_urls[i];\n", + " const escaped = encodeURI(url)\n", + " if (existing_stylesheets.indexOf(escaped) !== -1) {\n", + " on_load()\n", + " continue;\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", + " } var existing_scripts = []\n", + " const scripts = document.getElementsByTagName('script')\n", + " for (let i = 0; i < scripts.length; i++) {\n", + " var script = scripts[i]\n", + " if (script.src != null) {\n", + " existing_scripts.push(script.src)\n", + " }\n", + " }\n", + " for (let i = 0; i < js_urls.length; i++) {\n", + " const url = js_urls[i];\n", + " const escaped = encodeURI(url)\n", + " if (skip.indexOf(escaped) !== -1 || existing_scripts.indexOf(escaped) !== -1) {\n", + " if (!window.requirejs) {\n", + " on_load();\n", + " }\n", + " continue;\n", + " }\n", + " const 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 (let i = 0; i < js_modules.length; i++) {\n", + " const url = js_modules[i];\n", + " const escaped = encodeURI(url)\n", + " if (skip.indexOf(escaped) !== -1 || existing_scripts.indexOf(escaped) !== -1) {\n", + " if (!window.requirejs) {\n", + " on_load();\n", + " }\n", + " continue;\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", + " const url = js_exports[name];\n", + " const escaped = encodeURI(url)\n", + " if (skip.indexOf(escaped) >= 0 || root[name] != null) {\n", + " if (!window.requirejs) {\n", + " on_load();\n", + " }\n", + " continue;\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", + " const js_urls = [\"https://cdn.holoviz.org/panel/1.5.3/dist/bundled/reactiveesm/es-module-shims@^1.10.0/dist/es-module-shims.min.js\"];\n", + " const js_modules = [];\n", + " const js_exports = {};\n", + " const css_urls = [];\n", + " const inline_js = [ function(Bokeh) {\n", + " Bokeh.set_log_level(\"info\");\n", + " },\n", + "function(Bokeh) {} // ensure no trailing comma for IE\n", + " ];\n", + "\n", + " function run_inline_js() {\n", + " if ((root.Bokeh !== undefined) || (force === true)) {\n", + " for (let i = 0; i < inline_js.length; i++) {\n", + " try {\n", + " inline_js[i].call(root, root.Bokeh);\n", + " } catch(e) {\n", + " if (!reloading) {\n", + " throw e;\n", + " }\n", + " }\n", + " }\n", + " // Cache old bokeh versions\n", + " if (Bokeh != undefined && !reloading) {\n", + " var NewBokeh = root.Bokeh;\n", + " if (Bokeh.versions === undefined) {\n", + " Bokeh.versions = new Map();\n", + " }\n", + " if (NewBokeh.version !== Bokeh.version) {\n", + " Bokeh.versions.set(NewBokeh.version, NewBokeh)\n", + " }\n", + " root.Bokeh = Bokeh;\n", + " }\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", + " // If the timeout and bokeh was not successfully loaded we reset\n", + " // everything and try loading again\n", + " root._bokeh_timeout = Date.now() + 5000;\n", + " root._bokeh_is_initializing = false;\n", + " root._bokeh_onload_callbacks = undefined;\n", + " root._bokeh_is_loading = 0\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", + " const bokeh_loaded = root.Bokeh != null && (root.Bokeh.version === py_version || (root.Bokeh.versions !== undefined && root.Bokeh.versions.has(py_version)));\n", + " if (!reloading && !bokeh_loaded) {\n", + " if (root.Bokeh) {\n", + " root.Bokeh = undefined;\n", + " }\n", + " console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n", + " }\n", + " load_libs(css_urls, js_urls, js_modules, js_exports, function() {\n", + " console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n", + " run_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": "(function(root) {\n function now() {\n return new Date();\n }\n\n const force = false;\n const py_version = '3.6.0'.replace('rc', '-rc.').replace('.dev', '-dev.');\n const reloading = true;\n const Bokeh = root.Bokeh;\n\n // Set a timeout for this load but only if we are not already initializing\n if (typeof (root._bokeh_timeout) === \"undefined\" || (force || !root._bokeh_is_initializing)) {\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 // Don't load bokeh if it is still initializing\n console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n return null;\n } else if (js_urls.length === 0 && js_modules.length === 0 && Object.keys(js_exports).length === 0) {\n // There is nothing to load\n run_callbacks();\n return null;\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(e) {\n const src_el = e.srcElement\n console.error(\"failed to load \" + (src_el.href || src_el.src));\n }\n\n const skip = [];\n if (window.requirejs) {\n window.requirejs.config({'packages': {}, 'paths': {}, 'shim': {}});\n root._bokeh_is_loading = css_urls.length + 0;\n } else {\n root._bokeh_is_loading = css_urls.length + js_urls.length + js_modules.length + Object.keys(js_exports).length;\n }\n\n const existing_stylesheets = []\n const links = document.getElementsByTagName('link')\n for (let i = 0; i < links.length; i++) {\n const link = links[i]\n if (link.href != null) {\n existing_stylesheets.push(link.href)\n }\n }\n for (let i = 0; i < css_urls.length; i++) {\n const url = css_urls[i];\n const escaped = encodeURI(url)\n if (existing_stylesheets.indexOf(escaped) !== -1) {\n on_load()\n continue;\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 } var existing_scripts = []\n const scripts = document.getElementsByTagName('script')\n for (let i = 0; i < scripts.length; i++) {\n var script = scripts[i]\n if (script.src != null) {\n existing_scripts.push(script.src)\n }\n }\n for (let i = 0; i < js_urls.length; i++) {\n const url = js_urls[i];\n const escaped = encodeURI(url)\n if (skip.indexOf(escaped) !== -1 || existing_scripts.indexOf(escaped) !== -1) {\n if (!window.requirejs) {\n on_load();\n }\n continue;\n }\n const 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 (let i = 0; i < js_modules.length; i++) {\n const url = js_modules[i];\n const escaped = encodeURI(url)\n if (skip.indexOf(escaped) !== -1 || existing_scripts.indexOf(escaped) !== -1) {\n if (!window.requirejs) {\n on_load();\n }\n continue;\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 const url = js_exports[name];\n const escaped = encodeURI(url)\n if (skip.indexOf(escaped) >= 0 || root[name] != null) {\n if (!window.requirejs) {\n on_load();\n }\n continue;\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 const js_urls = [\"https://cdn.holoviz.org/panel/1.5.3/dist/bundled/reactiveesm/es-module-shims@^1.10.0/dist/es-module-shims.min.js\"];\n const js_modules = [];\n const js_exports = {};\n const css_urls = [];\n const 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 (let i = 0; i < inline_js.length; i++) {\n try {\n inline_js[i].call(root, root.Bokeh);\n } catch(e) {\n if (!reloading) {\n throw e;\n }\n }\n }\n // Cache old bokeh versions\n if (Bokeh != undefined && !reloading) {\n var NewBokeh = root.Bokeh;\n if (Bokeh.versions === undefined) {\n Bokeh.versions = new Map();\n }\n if (NewBokeh.version !== Bokeh.version) {\n Bokeh.versions.set(NewBokeh.version, NewBokeh)\n }\n root.Bokeh = Bokeh;\n }\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 // If the timeout and bokeh was not successfully loaded we reset\n // everything and try loading again\n root._bokeh_timeout = Date.now() + 5000;\n root._bokeh_is_initializing = false;\n root._bokeh_onload_callbacks = undefined;\n root._bokeh_is_loading = 0\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 const bokeh_loaded = root.Bokeh != null && (root.Bokeh.version === py_version || (root.Bokeh.versions !== undefined && root.Bokeh.versions.has(py_version)));\n if (!reloading && !bokeh_loaded) {\n if (root.Bokeh) {\n root.Bokeh = undefined;\n }\n console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n }\n load_libs(css_urls, js_urls, js_modules, js_exports, function() {\n console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n run_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));" + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": [ + "\n", + "if ((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", + "\n", + "var JS_MIME_TYPE = 'application/javascript';\n", + "var HTML_MIME_TYPE = 'text/html';\n", + "var EXEC_MIME_TYPE = 'application/vnd.holoviews_exec.v0+json';\n", + "var CLASS_NAME = 'output';\n", + "\n", + "/**\n", + " * Render data to the DOM node\n", + " */\n", + "function 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", + " */\n", + "function 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", + " */\n", + "function 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", + " */\n", + "function 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", + " */\n", + "function handle_update_output(event, handle) {\n", + " handle_clear_output(event, {cell: {output_area: handle.output_area}})\n", + " handle_add_output(event, handle)\n", + "}\n", + "\n", + "function 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", + "\n", + "if (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": "\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" + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "======================================================================\n", + "📦 Data Loading Module (No ODC Database Required)\n", + "======================================================================\n", + "\n", + "💡 Usage:\n", + " from load_data_no_odc import load_and_process_s2, load_and_process_s1\n", + "\n", + " bbox = (lon_min, lat_min, lon_max, lat_max)\n", + " date_range = ('2022-09-01', '2023-10-01')\n", + "\n", + " data_s2 = load_and_process_s2(bbox, date_range)\n", + " data_s1 = load_and_process_s1(bbox, date_range)\n", + "\n", + " # Load training data\n", + " train = load_train_data('train/data.shp', label_mapping)\n", + " X, y = extract_features_at_points(train, data_s2, data_s1)\n", + " X_train, X_val, X_test, y_train, y_val, y_test = split_train_data(X, y)\n", + "======================================================================\n", + "\n", + "======================================================================\n", + "📦 Data Loading Module (No ODC Database Required)\n", + "======================================================================\n", + "\n", + "💡 Usage:\n", + " from load_data_no_odc import load_and_process_s2, load_and_process_s1\n", + "\n", + " bbox = (lon_min, lat_min, lon_max, lat_max)\n", + " date_range = ('2022-09-01', '2023-10-01')\n", + "\n", + " data_s2 = load_and_process_s2(bbox, date_range)\n", + " data_s1 = load_and_process_s1(bbox, date_range)\n", + "\n", + " # Load training data\n", + " train = load_train_data('train/data.shp', label_mapping)\n", + " X, y = extract_features_at_points(train, data_s2, data_s1)\n", + " X_train, X_val, X_test, y_train, y_val, y_test = split_train_data(X, y)\n", + "======================================================================\n", + "\n", + "✅ All modules loaded successfully!\n", + "📡 Data source: Microsoft Planetary Computer\n", + "🚀 Infrastructure: Dask Gateway + ODC\n", + "CPU times: user 17.3 s, sys: 3.18 s, total: 20.5 s\n", + "Wall time: 11.9 s\n" + ] + } + ], + "source": [ + "%%time\n", + "%matplotlib inline\n", + "\n", + "import sys\n", + "import os\n", + "sys.path.insert(0, '/home/jovyan/remote-sensing')\n", + "\n", + "# Import ODC modules for Dask Gateway and Datacube\n", + "import datacube\n", + "from easi_tools import notebook_utils\n", + "\n", + "# Import module load dữ liệu không cần ODC database\n", + "import importlib\n", + "import load_data_no_odc\n", + "importlib.reload(load_data_no_odc)\n", + "\n", + "from load_data_no_odc import (\n", + " load_and_process_s2,\n", + " load_and_process_s1,\n", + " load_sentinel2_stac,\n", + " load_sentinel1_stac,\n", + " mask_clean_s2,\n", + " calculate_ndvi,\n", + " fill_nan_temporal,\n", + " resample_monthly\n", + ")\n", + "\n", + "# Standard imports\n", + "import numpy as np\n", + "import pandas as pd\n", + "import xarray as xr\n", + "import matplotlib.pyplot as plt\n", + "import seaborn as sns\n", + "sns.set_style('whitegrid')\n", + "\n", + "# ML imports\n", + "from sklearn.tree import DecisionTreeClassifier\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.metrics import classification_report, confusion_matrix, accuracy_score\n", + "import joblib\n", + "import json\n", + "from datetime import datetime\n", + "\n", + "print(\"✅ All modules loaded successfully!\")\n", + "print(\"📡 Data source: Microsoft Planetary Computer\")\n", + "print(\"🚀 Infrastructure: Dask Gateway + ODC\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "53397b2f", + "metadata": {}, + "source": [ + "## 🚀 Step 1: Initialize Dask Gateway + Datacube\n", + "\n", + "Khởi tạo Dask Gateway với adaptive scaling và cấu hình S3 access\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "3c4d6779", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "🚀 Step 1: Dask Gateway + Datacube Initialization\n", + "======================================================================\n", + "Starting new cluster\n" + ] + }, + { + "ename": "AttributeError", + "evalue": "module 'easi_tools.notebook_utils' has no attribute 'configure_s3_access'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "File \u001b[0;32m:11\u001b[0m\n", + "\u001b[0;31mAttributeError\u001b[0m: module 'easi_tools.notebook_utils' has no attribute 'configure_s3_access'" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2026-03-05 13:38:57,465 - distributed.client - ERROR - \n", + "Traceback (most recent call last):\n", + " File \"/env/lib/python3.12/site-packages/distributed/utils.py\", line 837, in wrapper\n", + " return await func(*args, **kwargs)\n", + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", + " File \"/env/lib/python3.12/site-packages/distributed/client.py\", line 1549, in _reconnect\n", + " await self._ensure_connected(timeout=timeout)\n", + " File \"/env/lib/python3.12/site-packages/distributed/client.py\", line 1579, in _ensure_connected\n", + " comm = await connect(\n", + " ^^^^^^^^^^^^^^\n", + " File \"/env/lib/python3.12/site-packages/distributed/comm/core.py\", line 342, in connect\n", + " comm = await wait_for(\n", + " ^^^^^^^^^^^^^^^\n", + " File \"/env/lib/python3.12/site-packages/distributed/utils.py\", line 1952, in wait_for\n", + " return await fut\n", + " ^^^^^^^^^\n", + " File \"/env/lib/python3.12/site-packages/dask_gateway/comm.py\", line 48, in connect\n", + " stream = await plain_stream.start_tls(\n", + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", + "asyncio.exceptions.CancelledError\n" + ] + } + ], + "source": [ + "%%time\n", + "\n", + "print(\"🚀 Step 1: Dask Gateway + Datacube Initialization\")\n", + "print(\"=\" * 70)\n", + "\n", + "# Cấu hình Dask Gateway\n", + "cluster, client = notebook_utils.initialize_dask(use_gateway=True, workers=(1, 10))\n", + "\n", + "# Khai báo Datacube\n", + "dc = datacube.Datacube()\n", + "\n", + "# Cấu hình truy cập dịch vụ S3\n", + "notebook_utils.configure_s3_access(aws_unsigned=False, requester_pays=True, client=client)\n", + "\n", + "print(f\"\\n✅ Dask Gateway + Datacube + S3 ready!\")\n", + "print(f\" Dask dashboard: {client.dashboard_link}\")\n", + "print(f\" Workers: Adaptive scaling (1-10)\")\n", + "print(\"=\" * 70)\n" + ] + }, + { + "cell_type": "markdown", + "id": "28bc5035", + "metadata": {}, + "source": [ + "## 📍 Step 2: Define Area of Interest (AOI)\n", + "\n", + "Định nghĩa vùng nghiên cứu và khoảng thời gian" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "15a5291c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "📍 Area of Interest:\n", + " Bbox: (105.5, 9.2, 106.4, 10.0)\n", + " Lon range: 105.5 to 106.4\n", + " Lat range: 9.2 to 10.0\n", + " Date range: 2022-09-01 to 2023-10-01\n", + "======================================================================\n" + ] + } + ], + "source": [ + "# Cấu hình vùng và thời gian\n", + "date_range = (\"2022-09-01\", \"2023-10-01\")\n", + "\n", + "# Bounding box: (lon_min, lat_min, lon_max, lat_max)\n", + "bbox = (105.5, 9.2, 106.4, 10.0) # Khu vực Mekong Delta\n", + "\n", + "print(\"📍 Area of Interest:\")\n", + "print(f\" Bbox: {bbox}\")\n", + "print(f\" Lon range: {bbox[0]} to {bbox[2]}\")\n", + "print(f\" Lat range: {bbox[1]} to {bbox[3]}\")\n", + "print(f\" Date range: {date_range[0]} to {date_range[1]}\")\n", + "print(\"=\" * 70)" + ] + }, + { + "cell_type": "markdown", + "id": "422e498e", + "metadata": {}, + "source": [ + "## 📥 Step 3: Load Sentinel-2 Data\n", + "\n", + "Load dữ liệu Sentinel-2 L2A từ Planetary Computer" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "611cd1c2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "📥 Loading Sentinel-2 data from Planetary Computer...\n", + "----------------------------------------------------------------------\n", + "🔍 Searching Sentinel-2 data...\n", + " Bbox: (105.5, 9.2, 106.4, 10.0)\n", + " Date: 2022-09-01 to 2023-10-01\n", + " Bands: ['red', 'nir', 'blue', 'green', 'nir08', 'swir16', 'swir22', 'SCL']\n", + "✅ Found 777 Sentinel-2 scenes\n", + "📥 Loading data...\n", + "❌ Error loading data: No such band/alias: nir08\n", + "❌ Failed to load Sentinel-2 data\n", + "CPU times: user 925 ms, sys: 19.8 ms, total: 945 ms\n", + "Wall time: 7.23 s\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Traceback (most recent call last):\n", + " File \"/home/jovyan/remote-sensing/load_data_no_odc.py\", line 71, in load_sentinel2_stac\n", + " data = odc.stac.load(\n", + " ^^^^^^^^^^^^^^\n", + " File \"/env/lib/python3.12/site-packages/odc/stac/_stac_load.py\", line 358, in load\n", + " gbox = output_geobox(\n", + " ^^^^^^^^^^^^^^\n", + " File \"/env/lib/python3.12/site-packages/odc/stac/_mdtools.py\", line 971, in output_geobox\n", + " rr = _auto_load_params(items, bands)\n", + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", + " File \"/env/lib/python3.12/site-packages/odc/stac/_mdtools.py\", line 784, in _auto_load_params\n", + " geoboxes = [gbox for gbox in map(_extract_gbox, items) if gbox is not None]\n", + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", + " File \"/env/lib/python3.12/site-packages/odc/stac/_mdtools.py\", line 781, in _extract_gbox\n", + " gbx = item.geoboxes(bands)\n", + " ^^^^^^^^^^^^^^^^^^^^\n", + " File \"/env/lib/python3.12/site-packages/odc/stac/model.py\", line 244, in geoboxes\n", + " b = self.bands.get(self.collection.band_key(name), None)\n", + " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n", + " File \"/env/lib/python3.12/site-packages/odc/stac/model.py\", line 144, in band_key\n", + " raise ValueError(f\"No such band/alias: {band}\")\n", + "ValueError: No such band/alias: nir08\n" + ] + } + ], + "source": [ + "%%time\n", + "\n", + "print(\"📥 Loading Sentinel-2 data from Planetary Computer...\")\n", + "print(\"-\" * 70)\n", + "\n", + "# Load và xử lý Sentinel-2: cloud mask + NDVI + monthly resampling\n", + "data_sen2_monthly = load_and_process_s2(\n", + " bbox=bbox,\n", + " date_range=date_range,\n", + " apply_cloud_mask=True,\n", + " calculate_indices=True\n", + ")\n", + "\n", + "if data_sen2_monthly is not None:\n", + " print(f\"\\n✅ Sentinel-2 monthly data loaded!\")\n", + " print(f\" Dimensions: {dict(data_sen2_monthly.dims)}\")\n", + " print(f\" Variables: {list(data_sen2_monthly.data_vars)}\")\n", + " print(f\" Time steps: {len(data_sen2_monthly.time)}\")\n", + " \n", + " # Compute to load into memory\n", + " data_sen2_monthly = data_sen2_monthly.compute()\n", + " print(f\" ✓ Data computed and loaded into memory\")\n", + "else:\n", + " print(\"❌ Failed to load Sentinel-2 data\")" + ] + }, + { + "cell_type": "markdown", + "id": "79ef9736", + "metadata": {}, + "source": [ + "## 📥 Step 4: Load Sentinel-1 Data\n", + "\n", + "Load dữ liệu Sentinel-1 RTC (SAR) từ Planetary Computer" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "3bda3dc0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "📥 Loading Sentinel-1 data from Planetary Computer...\n", + "----------------------------------------------------------------------\n", + "🔍 Searching Sentinel-1 data...\n", + " Bbox: (105.5, 9.2, 106.4, 10.0)\n", + " Date: 2022-09-01 to 2023-10-01\n", + " Bands: ['vv', 'vh']\n", + "✅ Found 149 Sentinel-1 scenes\n", + "📥 Loading data...\n", + "✅ Loaded Sentinel-1 data: FrozenMappingWarningOnValuesAccess({'y': 8874, 'x': 9902, 'time': 64})\n", + " Shape: {'y': 8874, 'x': 9902, 'time': 64}\n", + " Bands: ['VV', 'VH']\n", + "📅 Resampling to monthly using mean...\n", + "✅ Resampled to monthly: FrozenMappingWarningOnValuesAccess({'time': 13, 'y': 8874, 'x': 9902})\n", + "\n", + "✅ Sentinel-1 monthly data loaded!\n", + " Dimensions: {'time': 13, 'y': 8874, 'x': 9902}\n", + " Variables: ['VV', 'VH']\n", + " Time steps: 13\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/home/jovyan/remote-sensing/load_data_no_odc.py:158: FutureWarning: The return type of `Dataset.dims` will be changed to return a set of dimension names in future, in order to be more consistent with `DataArray.dims`. To access a mapping from dimension names to lengths, please use `Dataset.sizes`.\n", + " print(f\" Shape: {dict(data.dims)}\")\n", + ":12: FutureWarning: The return type of `Dataset.dims` will be changed to return a set of dimension names in future, in order to be more consistent with `DataArray.dims`. To access a mapping from dimension names to lengths, please use `Dataset.sizes`.\n" + ] + }, + { + "ename": "CommClosedError", + "evalue": "in : Stream is closed", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mStreamClosedError\u001b[0m Traceback (most recent call last)", + "File \u001b[0;32m/env/lib/python3.12/site-packages/distributed/comm/tcp.py:225\u001b[0m, in \u001b[0;36mTCP.read\u001b[0;34m(self, deserializers)\u001b[0m\n\u001b[1;32m 222\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 223\u001b[0m \u001b[38;5;66;03m# Don't store multiple numpy or parquet buffers into the same buffer, or\u001b[39;00m\n\u001b[1;32m 224\u001b[0m \u001b[38;5;66;03m# none will be released until all are released.\u001b[39;00m\n\u001b[0;32m--> 225\u001b[0m frames_nosplit_nbytes_bin \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mawait\u001b[39;00m stream\u001b[38;5;241m.\u001b[39mread_bytes(fmt_size)\n\u001b[1;32m 226\u001b[0m (frames_nosplit_nbytes,) \u001b[38;5;241m=\u001b[39m struct\u001b[38;5;241m.\u001b[39munpack(fmt, frames_nosplit_nbytes_bin)\n", + "\u001b[0;31mStreamClosedError\u001b[0m: Stream is closed", + "\nThe above exception was the direct cause of the following exception:\n", + "\u001b[0;31mCommClosedError\u001b[0m Traceback (most recent call last)", + "File \u001b[0;32m:17\u001b[0m\n", + "File \u001b[0;32m/env/lib/python3.12/site-packages/xarray/core/dataset.py:1046\u001b[0m, in \u001b[0;36mDataset.compute\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 1022\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Manually trigger loading and/or computation of this dataset's data\u001b[39;00m\n\u001b[1;32m 1023\u001b[0m \u001b[38;5;124;03mfrom disk or a remote source into memory and return a new dataset.\u001b[39;00m\n\u001b[1;32m 1024\u001b[0m \u001b[38;5;124;03mUnlike load, the original dataset is left unaltered.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1043\u001b[0m \u001b[38;5;124;03mdask.compute\u001b[39;00m\n\u001b[1;32m 1044\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1045\u001b[0m new \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcopy(deep\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[0;32m-> 1046\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mnew\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mload\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/env/lib/python3.12/site-packages/xarray/core/dataset.py:873\u001b[0m, in \u001b[0;36mDataset.load\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 870\u001b[0m chunkmanager \u001b[38;5;241m=\u001b[39m get_chunked_array_type(\u001b[38;5;241m*\u001b[39mlazy_data\u001b[38;5;241m.\u001b[39mvalues())\n\u001b[1;32m 872\u001b[0m \u001b[38;5;66;03m# evaluate all the chunked arrays simultaneously\u001b[39;00m\n\u001b[0;32m--> 873\u001b[0m evaluated_data: \u001b[38;5;28mtuple\u001b[39m[np\u001b[38;5;241m.\u001b[39mndarray[Any, Any], \u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[43mchunkmanager\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcompute\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 874\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mlazy_data\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalues\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\n\u001b[1;32m 875\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 877\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m k, data \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m(lazy_data, evaluated_data, strict\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m):\n\u001b[1;32m 878\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvariables[k]\u001b[38;5;241m.\u001b[39mdata \u001b[38;5;241m=\u001b[39m data\n", + "File \u001b[0;32m/env/lib/python3.12/site-packages/xarray/namedarray/daskmanager.py:86\u001b[0m, in \u001b[0;36mDaskManager.compute\u001b[0;34m(self, *data, **kwargs)\u001b[0m\n\u001b[1;32m 81\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcompute\u001b[39m(\n\u001b[1;32m 82\u001b[0m \u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39mdata: Any, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs: Any\n\u001b[1;32m 83\u001b[0m ) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28mtuple\u001b[39m[np\u001b[38;5;241m.\u001b[39mndarray[Any, _DType_co], \u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m]:\n\u001b[1;32m 84\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mdask\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01marray\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m compute\n\u001b[0;32m---> 86\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mcompute\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/env/lib/python3.12/site-packages/dask/base.py:662\u001b[0m, in \u001b[0;36mcompute\u001b[0;34m(traverse, optimize_graph, scheduler, get, *args, **kwargs)\u001b[0m\n\u001b[1;32m 659\u001b[0m postcomputes\u001b[38;5;241m.\u001b[39mappend(x\u001b[38;5;241m.\u001b[39m__dask_postcompute__())\n\u001b[1;32m 661\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m shorten_traceback():\n\u001b[0;32m--> 662\u001b[0m results \u001b[38;5;241m=\u001b[39m \u001b[43mschedule\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdsk\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeys\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 664\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m repack([f(r, \u001b[38;5;241m*\u001b[39ma) \u001b[38;5;28;01mfor\u001b[39;00m r, (f, a) \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m(results, postcomputes)])\n", + "File \u001b[0;32m/env/lib/python3.12/site-packages/distributed/utils_comm.py:459\u001b[0m, in \u001b[0;36mretry_operation\u001b[0;34m(coro, operation, *args, **kwargs)\u001b[0m\n\u001b[1;32m 453\u001b[0m retry_delay_min \u001b[38;5;241m=\u001b[39m parse_timedelta(\n\u001b[1;32m 454\u001b[0m dask\u001b[38;5;241m.\u001b[39mconfig\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdistributed.comm.retry.delay.min\u001b[39m\u001b[38;5;124m\"\u001b[39m), default\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124ms\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 455\u001b[0m )\n\u001b[1;32m 456\u001b[0m retry_delay_max \u001b[38;5;241m=\u001b[39m parse_timedelta(\n\u001b[1;32m 457\u001b[0m dask\u001b[38;5;241m.\u001b[39mconfig\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdistributed.comm.retry.delay.max\u001b[39m\u001b[38;5;124m\"\u001b[39m), default\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124ms\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 458\u001b[0m )\n\u001b[0;32m--> 459\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mawait\u001b[39;00m retry(\n\u001b[1;32m 460\u001b[0m partial(coro, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs),\n\u001b[1;32m 461\u001b[0m count\u001b[38;5;241m=\u001b[39mretry_count,\n\u001b[1;32m 462\u001b[0m delay_min\u001b[38;5;241m=\u001b[39mretry_delay_min,\n\u001b[1;32m 463\u001b[0m delay_max\u001b[38;5;241m=\u001b[39mretry_delay_max,\n\u001b[1;32m 464\u001b[0m operation\u001b[38;5;241m=\u001b[39moperation,\n\u001b[1;32m 465\u001b[0m )\n", + "File \u001b[0;32m/env/lib/python3.12/site-packages/distributed/utils_comm.py:438\u001b[0m, in \u001b[0;36mretry\u001b[0;34m(coro, count, delay_min, delay_max, jitter_fraction, retry_on_exceptions, operation)\u001b[0m\n\u001b[1;32m 436\u001b[0m delay \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m \u001b[38;5;241m+\u001b[39m random\u001b[38;5;241m.\u001b[39mrandom() \u001b[38;5;241m*\u001b[39m jitter_fraction\n\u001b[1;32m 437\u001b[0m \u001b[38;5;28;01mawait\u001b[39;00m asyncio\u001b[38;5;241m.\u001b[39msleep(delay)\n\u001b[0;32m--> 438\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mawait\u001b[39;00m coro()\n", + "File \u001b[0;32m/env/lib/python3.12/site-packages/distributed/core.py:1254\u001b[0m, in \u001b[0;36mPooledRPCCall.__getattr__..send_recv_from_rpc\u001b[0;34m(**kwargs)\u001b[0m\n\u001b[1;32m 1252\u001b[0m prev_name, comm\u001b[38;5;241m.\u001b[39mname \u001b[38;5;241m=\u001b[39m comm\u001b[38;5;241m.\u001b[39mname, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mConnectionPool.\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m+\u001b[39m key\n\u001b[1;32m 1253\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m-> 1254\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mawait\u001b[39;00m send_recv(comm\u001b[38;5;241m=\u001b[39mcomm, op\u001b[38;5;241m=\u001b[39mkey, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 1255\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m 1256\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpool\u001b[38;5;241m.\u001b[39mreuse(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39maddr, comm)\n", + "File \u001b[0;32m/env/lib/python3.12/site-packages/distributed/core.py:1013\u001b[0m, in \u001b[0;36msend_recv\u001b[0;34m(comm, reply, serializers, deserializers, **kwargs)\u001b[0m\n\u001b[1;32m 1011\u001b[0m \u001b[38;5;28;01mawait\u001b[39;00m comm\u001b[38;5;241m.\u001b[39mwrite(msg, serializers\u001b[38;5;241m=\u001b[39mserializers, on_error\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraise\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 1012\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m reply:\n\u001b[0;32m-> 1013\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mawait\u001b[39;00m comm\u001b[38;5;241m.\u001b[39mread(deserializers\u001b[38;5;241m=\u001b[39mdeserializers)\n\u001b[1;32m 1014\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 1015\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n", + "File \u001b[0;32m/env/lib/python3.12/site-packages/distributed/comm/tcp.py:236\u001b[0m, in \u001b[0;36mTCP.read\u001b[0;34m(self, deserializers)\u001b[0m\n\u001b[1;32m 234\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstream \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 235\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_closed \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m--> 236\u001b[0m \u001b[43mconvert_stream_closed_error\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43me\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 237\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m:\n\u001b[1;32m 238\u001b[0m \u001b[38;5;66;03m# Some OSError, CancelledError or another \"low-level\" exception.\u001b[39;00m\n\u001b[1;32m 239\u001b[0m \u001b[38;5;66;03m# We do not really know what was already read from the underlying\u001b[39;00m\n\u001b[1;32m 240\u001b[0m \u001b[38;5;66;03m# socket, so it is not even safe to retry here using the same stream.\u001b[39;00m\n\u001b[1;32m 241\u001b[0m \u001b[38;5;66;03m# The only safe thing to do is to abort.\u001b[39;00m\n\u001b[1;32m 242\u001b[0m \u001b[38;5;66;03m# (See also GitHub #4133, #6548).\u001b[39;00m\n\u001b[1;32m 243\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mabort()\n", + "File \u001b[0;32m/env/lib/python3.12/site-packages/distributed/comm/tcp.py:142\u001b[0m, in \u001b[0;36mconvert_stream_closed_error\u001b[0;34m(obj, exc)\u001b[0m\n\u001b[1;32m 140\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m CommClosedError(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124min \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mobj\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mexc\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mexc\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mexc\u001b[39;00m\n\u001b[1;32m 141\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 142\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m CommClosedError(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124min \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mobj\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mexc\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mexc\u001b[39;00m\n", + "\u001b[0;31mCommClosedError\u001b[0m: in : Stream is closed" + ] + } + ], + "source": [ + "%%time\n", + "\n", + "print(\"📥 Loading Sentinel-1 data from Planetary Computer...\")\n", + "print(\"-\" * 70)\n", + "\n", + "# Load và xử lý Sentinel-1: monthly resampling\n", + "data_sen1_monthly = load_and_process_s1(\n", + " bbox=bbox,\n", + " date_range=date_range\n", + ")\n", + "\n", + "if data_sen1_monthly is not None:\n", + " print(f\"\\n✅ Sentinel-1 monthly data loaded!\")\n", + " print(f\" Dimensions: {dict(data_sen1_monthly.dims)}\")\n", + " print(f\" Variables: {list(data_sen1_monthly.data_vars)}\")\n", + " print(f\" Time steps: {len(data_sen1_monthly.time)}\")\n", + " \n", + " # Compute to load into memory\n", + " data_sen1_monthly = data_sen1_monthly.compute()\n", + " print(f\" ✓ Data computed and loaded into memory\")\n", + "else:\n", + " print(\"❌ Failed to load Sentinel-1 data\")" + ] + }, + { + "cell_type": "markdown", + "id": "de80d48d", + "metadata": {}, + "source": [ + "## 🎯 Step 5: Load Training Data\n", + "\n", + "Load dữ liệu mẫu huấn luyện (training samples)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "70925d09", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "🎯 Label mapping:\n", + " 0: Lua tom\n", + " 1: Lua\n", + " 2: CHN\n", + " 3: CLN\n", + " 4: TS\n", + " 5: Song\n", + " 6: Dat xay dung\n", + " 7: Rung\n", + "\n", + "📂 Loading training data from: train/ST_training_data_updated_1130points_new.shp\n", + "📂 Loading training data from: train/ST_training_data_updated_1130points_new.shp\n", + "✅ Loaded 1130 training points\n", + " Columns: ['No', 'X', 'Y', 'LU2022', 'Hientrang', 'HT_code', 'geometry']\n", + "\n", + "✅ Training data loaded successfully!\n", + " Total points: 1130\n", + " CRS: EPSG:32648\n" + ] + } + ], + "source": [ + "from load_data_no_odc import load_train_data\n", + "\n", + "# Ánh xạ nhãn lớp đất\n", + "label_mapping = {\n", + " \"Lua tom\": 0,\n", + " \"Lua\": 1,\n", + " \"CHN\": 2,\n", + " \"CLN\": 3,\n", + " \"TS\": 4,\n", + " \"Song\": 5,\n", + " \"Dat xay dung\": 6,\n", + " \"Rung\": 7,\n", + "}\n", + "\n", + "print(\"🎯 Label mapping:\")\n", + "for label, idx in label_mapping.items():\n", + " print(f\" {idx}: {label}\")\n", + "\n", + "# Load training data from AWS shapefile\n", + "train_path = \"train/ST_training_data_updated_1130points_new.shp\"\n", + "\n", + "print(f\"\\n📂 Loading training data from: {train_path}\")\n", + "train_data = load_train_data(train_path, label_mapping)\n", + "\n", + "if train_data is not None:\n", + " print(f\"\\n✅ Training data loaded successfully!\")\n", + " print(f\" Total points: {len(train_data)}\")\n", + " print(f\" CRS: {train_data.crs}\")\n", + "else:\n", + " print(\"❌ Failed to load training data\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "fed9a8f1", + "metadata": {}, + "source": [ + "## 🔧 Step 6: Extract Features\n", + "\n", + "Trích xuất features từ Sentinel-1 và Sentinel-2 tại các điểm mẫu" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "62c41cd0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "🔧 Extracting features from satellite data...\n", + "----------------------------------------------------------------------\n", + "❌ Missing data: Please check training data or satellite data\n", + " train_data: ✓\n", + " data_sen2_monthly: ✗\n", + " data_sen1_monthly: ✓\n", + "CPU times: user 233 μs, sys: 34 μs, total: 267 μs\n", + "Wall time: 221 μs\n" + ] + } + ], + "source": [ + "%%time\n", + "from load_data_no_odc import extract_features_at_points\n", + "\n", + "print(\"🔧 Extracting features from satellite data...\")\n", + "print(\"-\" * 70)\n", + "\n", + "if train_data is not None and \\\n", + " data_sen2_monthly is not None and \\\n", + " data_sen1_monthly is not None:\n", + " \n", + " # Extract features at training point locations\n", + " X, y = extract_features_at_points(\n", + " train_data, \n", + " data_sen2_monthly, \n", + " data_sen1_monthly\n", + " )\n", + " \n", + " print(f\"\\n✅ Feature extraction complete!\")\n", + " print(f\" Total samples: {len(X)}\")\n", + " print(f\" Feature dimension: {X.shape[1]}\")\n", + " print(f\" Classes: {sorted(set(y.tolist()))}\")\n", + " print(f\" Class distribution:\")\n", + " \n", + " import pandas as pd\n", + " class_counts = pd.Series(y).value_counts().sort_index()\n", + " for class_id, count in class_counts.items():\n", + " class_name = [k for k, v in label_mapping.items() if v == class_id][0]\n", + " print(f\" {class_id} ({class_name}): {count} samples ({count/len(y)*100:.1f}%)\")\n", + " \n", + "else:\n", + " print(\"❌ Missing data: Please check training data or satellite data\")\n", + " print(f\" train_data: {'✓' if train_data is not None else '✗'}\")\n", + " print(f\" data_sen2_monthly: {'✓' if data_sen2_monthly is not None else '✗'}\")\n", + " print(f\" data_sen1_monthly: {'✓' if data_sen1_monthly is not None else '✗'}\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "f7cd0ca2", + "metadata": {}, + "source": [ + "## 📊 Step 7: Split Data\n", + "\n", + "Chia dữ liệu thành train/val/test sets" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "523e1249", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "❌ Features not extracted yet. Please run Step 6 first.\n" + ] + } + ], + "source": [ + "from load_data_no_odc import split_train_data\n", + "\n", + "if 'X' in locals() and 'y' in locals():\n", + " # Split data into train/val/test sets\n", + " X_train, X_val, X_test, y_train, y_val, y_test = split_train_data(\n", + " X, y, \n", + " test_size=0.2, \n", + " val_size=0.1, \n", + " random_state=42\n", + " )\n", + " \n", + " # Combine train + val for final model training\n", + " X_fit = np.concatenate([X_train, X_val], axis=0)\n", + " y_fit = np.concatenate([y_train, y_val], axis=0)\n", + " \n", + " print(f\"\\n✅ Final training set:\")\n", + " print(f\" X_fit shape: {X_fit.shape}\")\n", + " print(f\" y_fit shape: {y_fit.shape}\")\n", + " print(f\"\\n X_test shape: {X_test.shape}\")\n", + " print(f\" y_test shape: {y_test.shape}\")\n", + " \n", + "else:\n", + " print(\"❌ Features not extracted yet. Please run Step 6 first.\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "a469500c", + "metadata": {}, + "source": [ + "## 🌲 Step 8: Train Decision Tree Model\n", + "\n", + "Huấn luyện mô hình Decision Tree" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "445c2d92", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "❌ Data not ready. Please run Step 7 first.\n", + "CPU times: user 265 μs, sys: 39 μs, total: 304 μs\n", + "Wall time: 249 μs\n" + ] + } + ], + "source": [ + "%%time\n", + "from sklearn.tree import DecisionTreeClassifier\n", + "\n", + "if 'X_fit' in locals() and 'y_fit' in locals():\n", + " \n", + " print(\"🚀 Training Decision Tree model...\")\n", + " print(\"-\" * 70)\n", + " \n", + " # Train Decision Tree with tuned hyperparameters\n", + " model = DecisionTreeClassifier(\n", + " max_depth=30,\n", + " min_samples_leaf=2,\n", + " min_samples_split=5,\n", + " class_weight=\"balanced\",\n", + " random_state=42,\n", + " )\n", + " \n", + " model.fit(X_fit, y_fit)\n", + " \n", + " # Evaluate on validation set\n", + " val_acc = model.score(X_val, y_val)\n", + " \n", + " print(f\"\\n✅ Training complete!\")\n", + " print(f\" Model: Decision Tree\")\n", + " print(f\" Tree depth: {model.get_depth()}\")\n", + " print(f\" Number of leaves: {model.get_n_leaves()}\")\n", + " print(f\" Training samples: {len(X_fit)}\")\n", + " print(f\" Validation accuracy: {val_acc:.4f} ({val_acc*100:.2f}%)\")\n", + " \n", + "else:\n", + " print(\"❌ Data not ready. Please run Step 7 first.\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "7c1e681e", + "metadata": {}, + "source": [ + "## 📈 Step 9: Hyperparameter Analysis\n", + "\n", + "Phân tích độ sâu tối ưu (max_depth) cho Decision Tree" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "d13274e4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "❌ Data not ready. Please run Step 7 first.\n", + "CPU times: user 50 μs, sys: 8 μs, total: 58 μs\n", + "Wall time: 57.5 μs\n" + ] + } + ], + "source": [ + "%%time\n", + "import matplotlib.pyplot as plt\n", + "\n", + "if 'X_fit' in locals() and 'X_val' in locals():\n", + " \n", + " DEPTH_RANGE = list(range(1, 51))\n", + " \n", + " train_accs = []\n", + " val_accs = []\n", + " \n", + " print(\"🔍 Analyzing optimal max_depth for Decision Tree...\")\n", + " print(\"-\" * 70)\n", + " print(\"Testing depths from 1 to 50...\")\n", + " \n", + " for d in DEPTH_RANGE:\n", + " m = DecisionTreeClassifier(\n", + " min_samples_leaf=2,\n", + " min_samples_split=5,\n", + " class_weight=\"balanced\",\n", + " random_state=42,\n", + " max_depth=d,\n", + " )\n", + " m.fit(X_fit, y_fit)\n", + " train_accs.append(m.score(X_fit, y_fit))\n", + " val_accs.append(m.score(X_val, y_val))\n", + " \n", + " if d % 10 == 0:\n", + " print(f\" Depth {d:2d}: train={train_accs[-1]:.4f}, val={val_accs[-1]:.4f}\")\n", + " \n", + " train_accs = np.array(train_accs)\n", + " val_accs = np.array(val_accs)\n", + " \n", + " best_depth = DEPTH_RANGE[np.argmax(val_accs)]\n", + " best_val_acc = np.max(val_accs)\n", + " \n", + " print(f\"\\n✅ Optimal max_depth: {best_depth}\")\n", + " print(f\" Best validation accuracy: {best_val_acc:.4f} ({best_val_acc*100:.2f}%)\")\n", + " \n", + " # Plot convergence analysis\n", + " fig, ax = plt.subplots(1, 1, figsize=(12, 5))\n", + " ax.plot(DEPTH_RANGE, train_accs, 'b-o', markersize=3, label='Train Accuracy')\n", + " ax.plot(DEPTH_RANGE, val_accs, 'g-o', markersize=3, label='Validation Accuracy')\n", + " ax.axvline(x=best_depth, color='red', linestyle='--', linewidth=2, \n", + " label=f'Best Depth = {best_depth}')\n", + " ax.set_xlabel('max_depth', fontsize=12)\n", + " ax.set_ylabel('Accuracy', fontsize=12)\n", + " ax.set_title('Decision Tree: Training vs Validation Accuracy', fontsize=14, fontweight='bold')\n", + " ax.legend(fontsize=10)\n", + " ax.grid(True, alpha=0.3)\n", + " plt.tight_layout()\n", + " plt.show()\n", + " \n", + "else:\n", + " print(\"❌ Data not ready. Please run Step 7 first.\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "b551cdb4", + "metadata": {}, + "source": [ + "## 📊 Step 10: Evaluate Model\n", + "\n", + "Đánh giá mô hình trên tập test" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "bc3d6b9a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "❌ Model not trained yet. Please run Step 8 first.\n" + ] + } + ], + "source": [ + "from sklearn.metrics import accuracy_score, classification_report, confusion_matrix\n", + "import seaborn as sns\n", + "\n", + "if 'model' in locals() and 'X_test' in locals():\n", + " \n", + " print(\"📊 Evaluating model on test set...\")\n", + " print(\"-\" * 70)\n", + " \n", + " # Make predictions\n", + " y_pred = model.predict(X_test)\n", + " acc = accuracy_score(y_test, y_pred)\n", + " \n", + " print(f\"\\n🎯 Test Accuracy: {acc:.4f} ({acc*100:.2f}%)\\n\")\n", + " print(\"📋 Classification Report:\")\n", + " print(classification_report(y_test, y_pred, digits=4))\n", + " \n", + " # Confusion Matrix\n", + " class_names = list(label_mapping.keys())\n", + " cm = confusion_matrix(y_test, y_pred)\n", + " \n", + " plt.figure(figsize=(10, 8))\n", + " sns.heatmap(cm, annot=True, fmt='d', cmap='Greens',\n", + " xticklabels=class_names, yticklabels=class_names,\n", + " cbar_kws={'label': 'Count'})\n", + " plt.xlabel('Predicted Label', fontsize=12)\n", + " plt.ylabel('True Label', fontsize=12)\n", + " plt.title('Confusion Matrix — Decision Tree (Test Set)', fontsize=14, fontweight='bold')\n", + " plt.tight_layout()\n", + " plt.show()\n", + " \n", + "else:\n", + " print(\"❌ Model not trained yet. Please run Step 8 first.\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "44d226fc", + "metadata": {}, + "source": [ + "## 💾 Step 11: Save Model\n", + "\n", + "Lưu mô hình và metadata" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "044eefc4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "❌ Model not trained yet or evaluation not complete.\n" + ] + } + ], + "source": [ + "import joblib\n", + "import json\n", + "from datetime import datetime\n", + "\n", + "if 'model' in locals() and 'acc' in locals():\n", + " \n", + " print(\"💾 Saving model and metadata...\")\n", + " print(\"-\" * 70)\n", + " \n", + " # Save model\n", + " model_path = \"model_decision_tree_planetary_computer.joblib\"\n", + " joblib.dump(model, model_path)\n", + " print(f\"✅ Model saved → {model_path}\")\n", + " \n", + " # Create metadata\n", + " info = {\n", + " \"model_type\": \"DecisionTree\",\n", + " \"data_source\": \"Microsoft Planetary Computer\",\n", + " \"training_data\": train_path,\n", + " \"max_depth\": int(model.get_depth()),\n", + " \"n_leaves\": int(model.get_n_leaves()),\n", + " \"n_features\": int(X_fit.shape[1]),\n", + " \"label_mapping\": label_mapping,\n", + " \"test_accuracy\": float(acc),\n", + " \"train_samples\": int(len(X_fit)),\n", + " \"val_samples\": int(len(X_val)),\n", + " \"test_samples\": int(len(X_test)),\n", + " \"bbox\": bbox,\n", + " \"date_range\": list(date_range),\n", + " \"saved_at\": datetime.now().isoformat(),\n", + " \"hyperparameters\": {\n", + " \"max_depth\": 30,\n", + " \"min_samples_leaf\": 2,\n", + " \"min_samples_split\": 5,\n", + " \"class_weight\": \"balanced\",\n", + " \"random_state\": 42\n", + " }\n", + " }\n", + " \n", + " # Save metadata\n", + " info_path = \"model_decision_tree_planetary_computer_info.json\"\n", + " with open(info_path, \"w\") as f:\n", + " json.dump(info, f, indent=2, ensure_ascii=False)\n", + " \n", + " print(f\"✅ Metadata saved → {info_path}\")\n", + " print(\"\\n📋 Model Info:\")\n", + " print(json.dumps(info, indent=2, ensure_ascii=False))\n", + " \n", + "else:\n", + " print(\"❌ Model not trained yet or evaluation not complete.\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "18c95152", + "metadata": {}, + "source": [ + "## 🧹 Step 12: Cleanup\n", + "\n", + "Đóng Dask cluster" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "20445068", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "✅ Dask Gateway cluster closed.\n" + ] + } + ], + "source": [ + "# Close Dask Gateway cluster\n", + "try:\n", + " client.close()\n", + " cluster.close()\n", + " print(\"✅ Dask Gateway cluster closed.\")\n", + "except Exception as e:\n", + " print(f\"⚠ Error closing cluster: {e}\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "046bf0f9", + "metadata": {}, + "source": [ + "---\n", + "\n", + "## ✅ Summary\n", + "\n", + "### Notebook này:\n", + "- ✅ Chạy trên **Server ODC/JupyterHub** với **Dask Gateway**\n", + "- ✅ Load dữ liệu từ **Microsoft Planetary Computer** qua STAC API\n", + "- ✅ Không cần **ODC Database** để load data (dùng Planetary Computer)\n", + "- ✅ Sử dụng infrastructure của ODC (Dask Gateway, S3 access)\n", + "- ✅ Tương thích 100% với dữ liệu ODC\n", + "\n", + "### Ưu điểm của approach này:\n", + "1. **Scalability**: Dask Gateway adaptive scaling (1-10 workers)\n", + "2. **Public data**: Planetary Computer không cần VPN/private network\n", + "3. **Consistent**: Cùng infrastructure với ODC training pipeline\n", + "4. **Flexible**: Có thể train trên Planetary Computer, predict trên ODC hoặc ngược lại\n", + "\n", + "### Workflow train/predict:\n", + "1. **Train trên server ODC**: Chạy notebook này với Dask Gateway\n", + "2. **Save model**: Export `.joblib` file \n", + "3. **Predict qua API**: api_server.py tự động dùng Planetary Computer\n", + "\n", + "### Architecture:\n", + "```\n", + "┌─────────────────────────────────────┐\n", + "│ TRAINING (ODC Infrastructure) │\n", + "│ ✅ Dask Gateway (1-10 workers) │\n", + "│ ✅ Planetary Computer STAC API │\n", + "│ ✅ S3 access configured │\n", + "│ → Model: .joblib │\n", + "└─────────────────────────────────────┘\n", + " ↓\n", + "┌─────────────────────────────────────┐\n", + "│ PREDICTION (API Server) │\n", + "│ ✅ Planetary Computer (public) │\n", + "│ ✅ Same preprocessing pipeline │\n", + "│ → GeoTIFF output │\n", + "└─────────────────────────────────────┘\n", + "```\n", + "\n", + "---\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "983478ae-8c91-456d-b4e4-aa5c92286e72", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "135c5a1d-f97a-423a-9dc4-46bda170c86e", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.12.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/train_files/01.train_DecisionTree_PlanetaryComputer.ipynb b/train_files/01.train_DecisionTree_PlanetaryComputer.ipynb index 8c74ded..52a1ad7 100644 --- a/train_files/01.train_DecisionTree_PlanetaryComputer.ipynb +++ b/train_files/01.train_DecisionTree_PlanetaryComputer.ipynb @@ -32,17 +32,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "df9820b8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "ImportError", + "evalue": "cannot import name 'notebook_utils' from 'deafrica_tools' (/home/jovyan/remote-sensing/deafrica_tools/__init__.py)", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", + "File \u001b[0;32m:9\u001b[0m\n", + "\u001b[0;31mImportError\u001b[0m: cannot import name 'notebook_utils' from 'deafrica_tools' (/home/jovyan/remote-sensing/deafrica_tools/__init__.py)" + ] + } + ], "source": [ "%%time\n", "%matplotlib inline\n", "\n", "import sys\n", "import os\n", - "sys.path.insert(0, '/media/x79/2A7D-FAA0/remote-sensing')\n", + "sys.path.insert(0, '/home/jovyan/remote-sensing')\n", "\n", "# Import ODC modules for Dask Gateway and Datacube\n", "import datacube\n", @@ -97,10 +109,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "3c4d6779", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "🚀 Step 1: Dask Gateway + Datacube Initialization\n", + "======================================================================\n" + ] + }, + { + "ename": "NameError", + "evalue": "name 'notebook_utils' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "File \u001b[0;32m:5\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'notebook_utils' is not defined" + ] + } + ], "source": [ "%%time\n", "\n", @@ -134,10 +166,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "15a5291c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "📍 Area of Interest:\n", + " Bbox: (105.5, 9.2, 106.4, 10.0)\n", + " Lon range: 105.5 to 106.4\n", + " Lat range: 9.2 to 10.0\n", + " Date range: 2022-09-01 to 2023-10-01\n", + "======================================================================\n" + ] + } + ], "source": [ "# Cấu hình vùng và thời gian\n", "date_range = (\"2022-09-01\", \"2023-10-01\")\n", @@ -165,10 +210,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "611cd1c2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "📥 Loading Sentinel-2 data from Planetary Computer...\n", + "----------------------------------------------------------------------\n" + ] + }, + { + "ename": "NameError", + "evalue": "name 'load_and_process_s2' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "File \u001b[0;32m:5\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'load_and_process_s2' is not defined" + ] + } + ], "source": [ "%%time\n", "\n", @@ -208,10 +273,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "3bda3dc0", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "📥 Loading Sentinel-1 data from Planetary Computer...\n", + "----------------------------------------------------------------------\n" + ] + }, + { + "ename": "NameError", + "evalue": "name 'load_and_process_s1' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "File \u001b[0;32m:5\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'load_and_process_s1' is not defined" + ] + } + ], "source": [ "%%time\n", "\n", @@ -249,10 +334,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "70925d09", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "======================================================================\n", + "📦 Data Loading Module (No ODC Database Required)\n", + "======================================================================\n", + "\n", + "💡 Usage:\n", + " from load_data_no_odc import load_and_process_s2, load_and_process_s1\n", + "\n", + " bbox = (lon_min, lat_min, lon_max, lat_max)\n", + " date_range = ('2022-09-01', '2023-10-01')\n", + "\n", + " data_s2 = load_and_process_s2(bbox, date_range)\n", + " data_s1 = load_and_process_s1(bbox, date_range)\n", + "\n", + " # Load training data\n", + " train = load_train_data('train/data.shp', label_mapping)\n", + " X, y = extract_features_at_points(train, data_s2, data_s1)\n", + " X_train, X_val, X_test, y_train, y_val, y_test = split_train_data(X, y)\n", + "======================================================================\n", + "\n", + "🎯 Label mapping:\n", + " 0: Lua tom\n", + " 1: Lua\n", + " 2: CHN\n", + " 3: CLN\n", + " 4: TS\n", + " 5: Song\n", + " 6: Dat xay dung\n", + " 7: Rung\n", + "\n", + "📂 Loading training data from: train/ST_training_data_updated_1130points_new.shp\n", + "📂 Loading training data from: train/ST_training_data_updated_1130points_new.shp\n", + "❌ Error loading training data: train/ST_training_data_updated_1130points_new.shp: No such file or directory\n", + "❌ Failed to load training data\n" + ] + } + ], "source": [ "from load_data_no_odc import load_train_data\n", "\n", @@ -298,10 +423,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "62c41cd0", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "🔧 Extracting features from satellite data...\n", + "----------------------------------------------------------------------\n", + "❌ Missing data: Please check training data or satellite data\n", + " train_data: ✗\n" + ] + }, + { + "ename": "NameError", + "evalue": "name 'data_sen2_monthly' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "File \u001b[0;32m:32\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'data_sen2_monthly' is not defined" + ] + } + ], "source": [ "%%time\n", "from load_data_no_odc import extract_features_at_points\n", @@ -351,10 +498,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "523e1249", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "❌ Features not extracted yet. Please run Step 6 first.\n" + ] + } + ], "source": [ "from load_data_no_odc import split_train_data\n", "\n", @@ -393,10 +548,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "445c2d92", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "❌ Data not ready. Please run Step 7 first.\n", + "CPU times: user 187 ms, sys: 0 ns, total: 187 ms\n", + "Wall time: 187 ms\n" + ] + } + ], "source": [ "%%time\n", "from sklearn.tree import DecisionTreeClassifier\n", @@ -443,10 +608,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "d13274e4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "❌ Data not ready. Please run Step 7 first.\n", + "CPU times: user 46 μs, sys: 0 ns, total: 46 μs\n", + "Wall time: 50.8 μs\n" + ] + } + ], "source": [ "%%time\n", "import matplotlib.pyplot as plt\n", @@ -516,10 +691,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "bc3d6b9a", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "❌ Model not trained yet. Please run Step 8 first.\n" + ] + } + ], "source": [ "from sklearn.metrics import accuracy_score, classification_report, confusion_matrix\n", "import seaborn as sns\n", @@ -567,10 +750,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "044eefc4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "❌ Model not trained yet or evaluation not complete.\n" + ] + } + ], "source": [ "import joblib\n", "import json\n", @@ -636,10 +827,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "id": "20445068", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "⚠ Error closing cluster: name 'client' is not defined\n" + ] + } + ], "source": [ "# Close Dask Gateway cluster\n", "try:\n", @@ -700,8 +899,22 @@ } ], "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, "language_info": { - "name": "python" + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.3" } }, "nbformat": 4,