From f360b2e5cca65cc4cc3ba9827129d62f9ea86539 Mon Sep 17 00:00:00 2001 From: N-3ielo Date: Wed, 26 Mar 2025 14:15:26 +0000 Subject: [PATCH 1/7] This notebook is a demo for live plots whilst TD sim is running --- examples/Watchdog_live_view_demo.ipynb | 417 +++++++++++++++++++++++++ 1 file changed, 417 insertions(+) create mode 100644 examples/Watchdog_live_view_demo.ipynb diff --git a/examples/Watchdog_live_view_demo.ipynb b/examples/Watchdog_live_view_demo.ipynb new file mode 100644 index 0000000..b5ba320 --- /dev/null +++ b/examples/Watchdog_live_view_demo.ipynb @@ -0,0 +1,417 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 10, + "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.2/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.2/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));", + "application/vnd.holoviews_load.v0+json": "" + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": "\nif ((window.PyViz === undefined) || (window.PyViz instanceof HTMLElement)) {\n window.PyViz = {comms: {}, comm_status:{}, kernels:{}, receivers: {}, plot_index: []}\n}\n\n\n function JupyterCommManager() {\n }\n\n JupyterCommManager.prototype.register_target = function(plot_id, comm_id, msg_handler) {\n if (window.comm_manager || ((window.Jupyter !== undefined) && (Jupyter.notebook.kernel != null))) {\n var comm_manager = window.comm_manager || Jupyter.notebook.kernel.comm_manager;\n comm_manager.register_target(comm_id, function(comm) {\n comm.on_msg(msg_handler);\n });\n } else if ((plot_id in window.PyViz.kernels) && (window.PyViz.kernels[plot_id])) {\n window.PyViz.kernels[plot_id].registerCommTarget(comm_id, function(comm) {\n comm.onMsg = msg_handler;\n });\n } else if (typeof google != 'undefined' && google.colab.kernel != null) {\n google.colab.kernel.comms.registerTarget(comm_id, (comm) => {\n var messages = comm.messages[Symbol.asyncIterator]();\n function processIteratorResult(result) {\n var message = result.value;\n console.log(message)\n var content = {data: message.data, comm_id};\n var buffers = []\n for (var buffer of message.buffers || []) {\n buffers.push(new DataView(buffer))\n }\n var metadata = message.metadata || {};\n var msg = {content, buffers, metadata}\n msg_handler(msg);\n return messages.next().then(processIteratorResult);\n }\n return messages.next().then(processIteratorResult);\n })\n }\n }\n\n JupyterCommManager.prototype.get_client_comm = function(plot_id, comm_id, msg_handler) {\n if (comm_id in window.PyViz.comms) {\n return window.PyViz.comms[comm_id];\n } else if (window.comm_manager || ((window.Jupyter !== undefined) && (Jupyter.notebook.kernel != null))) {\n var comm_manager = window.comm_manager || Jupyter.notebook.kernel.comm_manager;\n var comm = comm_manager.new_comm(comm_id, {}, {}, {}, comm_id);\n if (msg_handler) {\n comm.on_msg(msg_handler);\n }\n } else if ((plot_id in window.PyViz.kernels) && (window.PyViz.kernels[plot_id])) {\n var comm = window.PyViz.kernels[plot_id].connectToComm(comm_id);\n comm.open();\n if (msg_handler) {\n comm.onMsg = msg_handler;\n }\n } else if (typeof google != 'undefined' && google.colab.kernel != null) {\n var comm_promise = google.colab.kernel.comms.open(comm_id)\n comm_promise.then((comm) => {\n window.PyViz.comms[comm_id] = comm;\n if (msg_handler) {\n var messages = comm.messages[Symbol.asyncIterator]();\n function processIteratorResult(result) {\n var message = result.value;\n var content = {data: message.data};\n var metadata = message.metadata || {comm_id};\n var msg = {content, metadata}\n msg_handler(msg);\n return messages.next().then(processIteratorResult);\n }\n return messages.next().then(processIteratorResult);\n }\n }) \n var sendClosure = (data, metadata, buffers, disposeOnDone) => {\n return comm_promise.then((comm) => {\n comm.send(data, metadata, buffers, disposeOnDone);\n });\n };\n var comm = {\n send: sendClosure\n };\n }\n window.PyViz.comms[comm_id] = comm;\n return comm;\n }\n window.PyViz.comm_manager = new JupyterCommManager();\n \n\n\nvar JS_MIME_TYPE = 'application/javascript';\nvar HTML_MIME_TYPE = 'text/html';\nvar EXEC_MIME_TYPE = 'application/vnd.holoviews_exec.v0+json';\nvar CLASS_NAME = 'output';\n\n/**\n * Render data to the DOM node\n */\nfunction render(props, node) {\n var div = document.createElement(\"div\");\n var script = document.createElement(\"script\");\n node.appendChild(div);\n node.appendChild(script);\n}\n\n/**\n * Handle when a new output is added\n */\nfunction handle_add_output(event, handle) {\n var output_area = handle.output_area;\n var output = handle.output;\n if ((output.data == undefined) || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {\n return\n }\n var id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n var toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n if (id !== undefined) {\n var nchildren = toinsert.length;\n var html_node = toinsert[nchildren-1].children[0];\n html_node.innerHTML = output.data[HTML_MIME_TYPE];\n var scripts = [];\n var nodelist = html_node.querySelectorAll(\"script\");\n for (var i in nodelist) {\n if (nodelist.hasOwnProperty(i)) {\n scripts.push(nodelist[i])\n }\n }\n\n scripts.forEach( function (oldScript) {\n var newScript = document.createElement(\"script\");\n var attrs = [];\n var nodemap = oldScript.attributes;\n for (var j in nodemap) {\n if (nodemap.hasOwnProperty(j)) {\n attrs.push(nodemap[j])\n }\n }\n attrs.forEach(function(attr) { newScript.setAttribute(attr.name, attr.value) });\n newScript.appendChild(document.createTextNode(oldScript.innerHTML));\n oldScript.parentNode.replaceChild(newScript, oldScript);\n });\n if (JS_MIME_TYPE in output.data) {\n toinsert[nchildren-1].children[1].textContent = output.data[JS_MIME_TYPE];\n }\n output_area._hv_plot_id = id;\n if ((window.Bokeh !== undefined) && (id in Bokeh.index)) {\n window.PyViz.plot_index[id] = Bokeh.index[id];\n } else {\n window.PyViz.plot_index[id] = null;\n }\n } else if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n var bk_div = document.createElement(\"div\");\n bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n var script_attrs = bk_div.children[0].attributes;\n for (var i = 0; i < script_attrs.length; i++) {\n toinsert[toinsert.length - 1].childNodes[1].setAttribute(script_attrs[i].name, script_attrs[i].value);\n }\n // store reference to server id on output_area\n output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n }\n}\n\n/**\n * Handle when an output is cleared or removed\n */\nfunction handle_clear_output(event, handle) {\n var id = handle.cell.output_area._hv_plot_id;\n var server_id = handle.cell.output_area._bokeh_server_id;\n if (((id === undefined) || !(id in PyViz.plot_index)) && (server_id !== undefined)) { return; }\n var comm = window.PyViz.comm_manager.get_client_comm(\"hv-extension-comm\", \"hv-extension-comm\", function () {});\n if (server_id !== null) {\n comm.send({event_type: 'server_delete', 'id': server_id});\n return;\n } else if (comm !== null) {\n comm.send({event_type: 'delete', 'id': id});\n }\n delete PyViz.plot_index[id];\n if ((window.Bokeh !== undefined) & (id in window.Bokeh.index)) {\n var doc = window.Bokeh.index[id].model.document\n doc.clear();\n const i = window.Bokeh.documents.indexOf(doc);\n if (i > -1) {\n window.Bokeh.documents.splice(i, 1);\n }\n }\n}\n\n/**\n * Handle kernel restart event\n */\nfunction handle_kernel_cleanup(event, handle) {\n delete PyViz.comms[\"hv-extension-comm\"];\n window.PyViz.plot_index = {}\n}\n\n/**\n * Handle update_display_data messages\n */\nfunction handle_update_output(event, handle) {\n handle_clear_output(event, {cell: {output_area: handle.output_area}})\n handle_add_output(event, handle)\n}\n\nfunction register_renderer(events, OutputArea) {\n function append_mime(data, metadata, element) {\n // create a DOM node to render to\n var toinsert = this.create_output_subarea(\n metadata,\n CLASS_NAME,\n EXEC_MIME_TYPE\n );\n this.keyboard_manager.register_events(toinsert);\n // Render to node\n var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n render(props, toinsert[0]);\n element.append(toinsert);\n return toinsert\n }\n\n events.on('output_added.OutputArea', handle_add_output);\n events.on('output_updated.OutputArea', handle_update_output);\n events.on('clear_output.CodeCell', handle_clear_output);\n events.on('delete.Cell', handle_clear_output);\n events.on('kernel_ready.Kernel', handle_kernel_cleanup);\n\n OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n safe: true,\n index: 0\n });\n}\n\nif (window.Jupyter !== undefined) {\n try {\n var events = require('base/js/events');\n var OutputArea = require('notebook/js/outputarea').OutputArea;\n if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n register_renderer(events, OutputArea);\n }\n } catch(err) {\n }\n}\n", + "application/vnd.holoviews_load.v0+json": "" + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.holoviews_exec.v0+json": "", + "text/html": [ + "
\n", + "
\n", + "
\n", + "" + ] + }, + "metadata": { + "application/vnd.holoviews_exec.v0+json": { + "id": "12b38477-4b9a-4529-abc7-9b23db001c55" + } + }, + "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.2/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));", + "application/vnd.holoviews_load.v0+json": "" + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": "\nif ((window.PyViz === undefined) || (window.PyViz instanceof HTMLElement)) {\n window.PyViz = {comms: {}, comm_status:{}, kernels:{}, receivers: {}, plot_index: []}\n}\n\n\n function JupyterCommManager() {\n }\n\n JupyterCommManager.prototype.register_target = function(plot_id, comm_id, msg_handler) {\n if (window.comm_manager || ((window.Jupyter !== undefined) && (Jupyter.notebook.kernel != null))) {\n var comm_manager = window.comm_manager || Jupyter.notebook.kernel.comm_manager;\n comm_manager.register_target(comm_id, function(comm) {\n comm.on_msg(msg_handler);\n });\n } else if ((plot_id in window.PyViz.kernels) && (window.PyViz.kernels[plot_id])) {\n window.PyViz.kernels[plot_id].registerCommTarget(comm_id, function(comm) {\n comm.onMsg = msg_handler;\n });\n } else if (typeof google != 'undefined' && google.colab.kernel != null) {\n google.colab.kernel.comms.registerTarget(comm_id, (comm) => {\n var messages = comm.messages[Symbol.asyncIterator]();\n function processIteratorResult(result) {\n var message = result.value;\n console.log(message)\n var content = {data: message.data, comm_id};\n var buffers = []\n for (var buffer of message.buffers || []) {\n buffers.push(new DataView(buffer))\n }\n var metadata = message.metadata || {};\n var msg = {content, buffers, metadata}\n msg_handler(msg);\n return messages.next().then(processIteratorResult);\n }\n return messages.next().then(processIteratorResult);\n })\n }\n }\n\n JupyterCommManager.prototype.get_client_comm = function(plot_id, comm_id, msg_handler) {\n if (comm_id in window.PyViz.comms) {\n return window.PyViz.comms[comm_id];\n } else if (window.comm_manager || ((window.Jupyter !== undefined) && (Jupyter.notebook.kernel != null))) {\n var comm_manager = window.comm_manager || Jupyter.notebook.kernel.comm_manager;\n var comm = comm_manager.new_comm(comm_id, {}, {}, {}, comm_id);\n if (msg_handler) {\n comm.on_msg(msg_handler);\n }\n } else if ((plot_id in window.PyViz.kernels) && (window.PyViz.kernels[plot_id])) {\n var comm = window.PyViz.kernels[plot_id].connectToComm(comm_id);\n comm.open();\n if (msg_handler) {\n comm.onMsg = msg_handler;\n }\n } else if (typeof google != 'undefined' && google.colab.kernel != null) {\n var comm_promise = google.colab.kernel.comms.open(comm_id)\n comm_promise.then((comm) => {\n window.PyViz.comms[comm_id] = comm;\n if (msg_handler) {\n var messages = comm.messages[Symbol.asyncIterator]();\n function processIteratorResult(result) {\n var message = result.value;\n var content = {data: message.data};\n var metadata = message.metadata || {comm_id};\n var msg = {content, metadata}\n msg_handler(msg);\n return messages.next().then(processIteratorResult);\n }\n return messages.next().then(processIteratorResult);\n }\n }) \n var sendClosure = (data, metadata, buffers, disposeOnDone) => {\n return comm_promise.then((comm) => {\n comm.send(data, metadata, buffers, disposeOnDone);\n });\n };\n var comm = {\n send: sendClosure\n };\n }\n window.PyViz.comms[comm_id] = comm;\n return comm;\n }\n window.PyViz.comm_manager = new JupyterCommManager();\n \n\n\nvar JS_MIME_TYPE = 'application/javascript';\nvar HTML_MIME_TYPE = 'text/html';\nvar EXEC_MIME_TYPE = 'application/vnd.holoviews_exec.v0+json';\nvar CLASS_NAME = 'output';\n\n/**\n * Render data to the DOM node\n */\nfunction render(props, node) {\n var div = document.createElement(\"div\");\n var script = document.createElement(\"script\");\n node.appendChild(div);\n node.appendChild(script);\n}\n\n/**\n * Handle when a new output is added\n */\nfunction handle_add_output(event, handle) {\n var output_area = handle.output_area;\n var output = handle.output;\n if ((output.data == undefined) || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {\n return\n }\n var id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n var toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n if (id !== undefined) {\n var nchildren = toinsert.length;\n var html_node = toinsert[nchildren-1].children[0];\n html_node.innerHTML = output.data[HTML_MIME_TYPE];\n var scripts = [];\n var nodelist = html_node.querySelectorAll(\"script\");\n for (var i in nodelist) {\n if (nodelist.hasOwnProperty(i)) {\n scripts.push(nodelist[i])\n }\n }\n\n scripts.forEach( function (oldScript) {\n var newScript = document.createElement(\"script\");\n var attrs = [];\n var nodemap = oldScript.attributes;\n for (var j in nodemap) {\n if (nodemap.hasOwnProperty(j)) {\n attrs.push(nodemap[j])\n }\n }\n attrs.forEach(function(attr) { newScript.setAttribute(attr.name, attr.value) });\n newScript.appendChild(document.createTextNode(oldScript.innerHTML));\n oldScript.parentNode.replaceChild(newScript, oldScript);\n });\n if (JS_MIME_TYPE in output.data) {\n toinsert[nchildren-1].children[1].textContent = output.data[JS_MIME_TYPE];\n }\n output_area._hv_plot_id = id;\n if ((window.Bokeh !== undefined) && (id in Bokeh.index)) {\n window.PyViz.plot_index[id] = Bokeh.index[id];\n } else {\n window.PyViz.plot_index[id] = null;\n }\n } else if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n var bk_div = document.createElement(\"div\");\n bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n var script_attrs = bk_div.children[0].attributes;\n for (var i = 0; i < script_attrs.length; i++) {\n toinsert[toinsert.length - 1].childNodes[1].setAttribute(script_attrs[i].name, script_attrs[i].value);\n }\n // store reference to server id on output_area\n output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n }\n}\n\n/**\n * Handle when an output is cleared or removed\n */\nfunction handle_clear_output(event, handle) {\n var id = handle.cell.output_area._hv_plot_id;\n var server_id = handle.cell.output_area._bokeh_server_id;\n if (((id === undefined) || !(id in PyViz.plot_index)) && (server_id !== undefined)) { return; }\n var comm = window.PyViz.comm_manager.get_client_comm(\"hv-extension-comm\", \"hv-extension-comm\", function () {});\n if (server_id !== null) {\n comm.send({event_type: 'server_delete', 'id': server_id});\n return;\n } else if (comm !== null) {\n comm.send({event_type: 'delete', 'id': id});\n }\n delete PyViz.plot_index[id];\n if ((window.Bokeh !== undefined) & (id in window.Bokeh.index)) {\n var doc = window.Bokeh.index[id].model.document\n doc.clear();\n const i = window.Bokeh.documents.indexOf(doc);\n if (i > -1) {\n window.Bokeh.documents.splice(i, 1);\n }\n }\n}\n\n/**\n * Handle kernel restart event\n */\nfunction handle_kernel_cleanup(event, handle) {\n delete PyViz.comms[\"hv-extension-comm\"];\n window.PyViz.plot_index = {}\n}\n\n/**\n * Handle update_display_data messages\n */\nfunction handle_update_output(event, handle) {\n handle_clear_output(event, {cell: {output_area: handle.output_area}})\n handle_add_output(event, handle)\n}\n\nfunction register_renderer(events, OutputArea) {\n function append_mime(data, metadata, element) {\n // create a DOM node to render to\n var toinsert = this.create_output_subarea(\n metadata,\n CLASS_NAME,\n EXEC_MIME_TYPE\n );\n this.keyboard_manager.register_events(toinsert);\n // Render to node\n var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n render(props, toinsert[0]);\n element.append(toinsert);\n return toinsert\n }\n\n events.on('output_added.OutputArea', handle_add_output);\n events.on('output_updated.OutputArea', handle_update_output);\n events.on('clear_output.CodeCell', handle_clear_output);\n events.on('delete.Cell', handle_clear_output);\n events.on('kernel_ready.Kernel', handle_kernel_cleanup);\n\n OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n safe: true,\n index: 0\n });\n}\n\nif (window.Jupyter !== undefined) {\n try {\n var events = require('base/js/events');\n var OutputArea = require('notebook/js/outputarea').OutputArea;\n if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n register_renderer(events, OutputArea);\n }\n } catch(err) {\n }\n}\n", + "application/vnd.holoviews_load.v0+json": "" + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import discretisedfield as df\n", + "import xarray as xr\n", + "import holoviews as hv\n", + "import numpy as np\n", + "import streamz\n", + "import time\n", + "import os\n", + "import holoviews as hv\n", + "import pandas as pd\n", + "import panel as pn\n", + "\n", + "from holoviews.streams import Pipe, Buffer\n", + "from watchdog.observers import Observer\n", + "from watchdog.events import FileSystemEventHandler\n", + "\n", + "hv.extension('bokeh','matplotlib', logo=False)\n", + "pn.extension()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "class DirectoryMonitorHandler(FileSystemEventHandler):\n", + " def __init__(self, observer,t, max_count,stop_flag):\n", + " self.observer = observer\n", + " self.max_count = max_count + 1 #+1 because the m0 file is not counted when user specifies how many time driver simulations\n", + " self.file_count = 0\n", + " self.t = t\n", + " self.stop_flag = stop_flag\n", + " self.monitored_directories = self.get_existing_subdirectories()\n", + " self.pipe1 = Pipe(data=[])\n", + " self.pipe2 = Pipe(data=[])\n", + " self.vector_dmap = hv.DynamicMap(hv.VectorField, streams=[self.pipe1]).opts(magnitude=\"mag\", aspect=1)\n", + " self.Image = hv.DynamicMap(hv.Image, streams=[self.pipe2]).opts(cmap=\"coolwarm\",colorbar=True)\n", + " self.plot = (self.Image * self.vector_dmap)\n", + " self.widget() \n", + "\n", + " def get_existing_subdirectories(self):\n", + " cwd = os.getcwd()\n", + " list_dir = os.listdir(cwd)\n", + " monitored_directories = {\n", + " os.path.join(cwd, i) for i in list_dir if os.path.isdir(os.path.join(cwd, i))\n", + " }\n", + " return monitored_directories\n", + "\n", + " def on_created(self, event):\n", + " if event.is_directory:\n", + " print(f\"New directory created: {event.src_path}\")\n", + " self.monitor_new_directory(event.src_path)\n", + " else:\n", + " print(f\"New file detected: {event.src_path}\")\n", + " #self.widget(event.src_path)\n", + " self.process_file(event.src_path)\n", + " \n", + " def monitor_new_directory(self, directory_path):\n", + " if directory_path not in self.monitored_directories:\n", + " self.monitored_directories.add(directory_path)\n", + " #self.observer.schedule(self, path=directory_path, recursive=False)\n", + " print(f\"Now monitoring: {directory_path}\")\n", + "\n", + " def process_file(self, file_path):\n", + " # Only process files with specific extensions\n", + " if file_path.endswith(r'.omf'):\n", + " field = df.Field.from_file(rf\"{file_path}\")\n", + " field_xr = field.to_xarray()\n", + " #x_array for each vdim component\n", + " xxa = field_xr.sel(vdims='x') # arrow_x at each coordinate\n", + " yxa = field_xr.sel(vdims='y') # arrow_y\n", + " zxa = field_xr.sel(vdims='z') # arrow_z\n", + "\n", + " angle = np.arctan2(yxa, xxa) # angle of vector line\n", + " mag = np.sqrt(xxa**2 + yxa**2) # magnitude of vector\n", + " \n", + "\n", + " table = xr.Dataset(dict(angle=angle[:,:,0], mag = mag[:,:,0], W = zxa[:,:,0])) #xarray of processed data needed for hv visualisation\n", + "\n", + " self.pipe1.send(table) #pipe for vectorfield\n", + " self.pipe2.send(table[\"W\"]) # pipe for scalar field\n", + "\n", + " self.file_count +=1\n", + " self.check_observer()\n", + "\n", + " self.time_step.value = (self.t/self.max_count)*(self.file_count+1)\n", + "\n", + " if file_path.endswith(r'm0.omf'):\n", + " \n", + " display(self.plot,self.time_step) # display one updating dmap that initially gets created with m0 file\n", + "\n", + " def widget(self):\n", + " time_step = pn.indicators.Number(\n", + " name=\"t:\",\n", + " format=\"{value:.2g}\",\n", + " title_size=\"10pt\", \n", + " font_size=\"10pt\",\n", + " default_color=\"white\"\n", + " )\n", + " self.time_step = time_step\n", + "\n", + " def check_observer(self):\n", + " if self.file_count >= self.max_count:\n", + " print(\"All files produced, stopping monitoring...\")\n", + " self.observer.stop()\n", + " self.stop_flag[0] = True" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "def main(t, max_count, working_directory = os.getcwd()):\n", + " \"\"\"\n", + " Function to plot hv plots whilst simultaneously running a Time Driver simulation\n", + "\n", + " Parameters\n", + " t: The length of the time driver simulation. Must match the input for 't' in oc.TimeDriver()\n", + " max_count: The number of stages in the simulation.\n", + " working_directory: The directory we want Watchdog to monitor. Defaulted to the current working directory\n", + " \"\"\"\n", + " # working_directory defaulted to cwd\n", + "\n", + " #stop flag used to terminate thread when all files are produced.\n", + " stop_flag = [False]\n", + "\n", + " # Set up the observer\n", + " observer = Observer()\n", + " handler = DirectoryMonitorHandler(observer, t, max_count, stop_flag)\n", + "\n", + " # Schedule the observer to monitor the working directory\n", + " observer.schedule(handler, working_directory, recursive=True)\n", + " print(f\"Monitoring: {working_directory}\")\n", + "\n", + " # Start the observer\n", + " observer.start()\n", + " \n", + " try:\n", + " while not stop_flag[0]:\n", + " time.sleep(5) # Keeps the script running\n", + " except KeyboardInterrupt:\n", + " print(\"Stopping monitoring...\")\n", + " finally:\n", + " observer.stop()\n", + "\n", + " observer.join()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Monitoring: c:\\Users\\Noahd\\IP code\n", + "New directory created: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\n", + "Now monitoring: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\n", + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\m0.omf\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "f21352e5b0904891bf495404f6722601", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "BokehModel(combine_events=True, render_bundle={'docs_json': {'03eb4df3-7c28-4400-9b16-8222c03d1526': {'version…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "14c7c6cdda7a47648e58e734f832e8d7", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "BokehModel(combine_events=True, render_bundle={'docs_json': {'44d85dae-cfb2-4f5c-a307-0eb58fbb8624': {'version…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\test_1.mif\n", + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\info.json\n", + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\test-noahs-laptop-23992\n", + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\test_1.restart-noahs-laptop-23992-oxs-checkpoint.tmp\n", + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\test-noahs-laptop-23992\n", + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\test_1.restart-noahs-laptop-23992-oxs-checkpoint.backup\n", + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\test_1.odt\n", + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\test_1-Oxs_TimeDriver-Magnetization-00-0003273.omf\n", + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\test_1-Oxs_TimeDriver-Magnetization-01-0006547.omf\n", + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\test_1-Oxs_TimeDriver-Magnetization-02-0009821.omf\n", + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\test_1-Oxs_TimeDriver-Magnetization-03-0013095.omf\n", + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\test_1-Oxs_TimeDriver-Magnetization-04-0016369.omf\n", + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\test_1-Oxs_TimeDriver-Magnetization-05-0019643.omf\n", + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\test_1-Oxs_TimeDriver-Magnetization-06-0022917.omf\n", + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\test_1-Oxs_TimeDriver-Magnetization-07-0026191.omf\n", + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\test_1-Oxs_TimeDriver-Magnetization-08-0029465.omf\n", + "New file detected: c:\\Users\\Noahd\\IP code\\test_1\\drive-96\\test_1-Oxs_TimeDriver-Magnetization-09-0032739.omf\n", + "All files produced, stopping monitoring...\n" + ] + } + ], + "source": [ + "main(t=5e-9, max_count=10) # replace t and max_count with data read from info.json" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 0b83c6215e1c9e4c5ba9503ddd0bd68ae54ae9bf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Mar 2025 14:19:44 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- examples/Watchdog_live_view_demo.ipynb | 83 ++++++++++++++------------ 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/examples/Watchdog_live_view_demo.ipynb b/examples/Watchdog_live_view_demo.ipynb index b5ba320..1820415 100644 --- a/examples/Watchdog_live_view_demo.ipynb +++ b/examples/Watchdog_live_view_demo.ipynb @@ -174,18 +174,15 @@ "import xarray as xr\n", "import holoviews as hv\n", "import numpy as np\n", - "import streamz\n", "import time\n", "import os\n", - "import holoviews as hv\n", - "import pandas as pd\n", "import panel as pn\n", "\n", - "from holoviews.streams import Pipe, Buffer\n", + "from holoviews.streams import Pipe\n", "from watchdog.observers import Observer\n", "from watchdog.events import FileSystemEventHandler\n", "\n", - "hv.extension('bokeh','matplotlib', logo=False)\n", + "hv.extension(\"bokeh\", \"matplotlib\", logo=False)\n", "pn.extension()" ] }, @@ -196,25 +193,33 @@ "outputs": [], "source": [ "class DirectoryMonitorHandler(FileSystemEventHandler):\n", - " def __init__(self, observer,t, max_count,stop_flag):\n", + " def __init__(self, observer, t, max_count, stop_flag):\n", " self.observer = observer\n", - " self.max_count = max_count + 1 #+1 because the m0 file is not counted when user specifies how many time driver simulations\n", + " self.max_count = (\n", + " max_count + 1\n", + " ) # +1 because the m0 file is not counted when user specifies how many time driver simulations\n", " self.file_count = 0\n", " self.t = t\n", " self.stop_flag = stop_flag\n", " self.monitored_directories = self.get_existing_subdirectories()\n", " self.pipe1 = Pipe(data=[])\n", " self.pipe2 = Pipe(data=[])\n", - " self.vector_dmap = hv.DynamicMap(hv.VectorField, streams=[self.pipe1]).opts(magnitude=\"mag\", aspect=1)\n", - " self.Image = hv.DynamicMap(hv.Image, streams=[self.pipe2]).opts(cmap=\"coolwarm\",colorbar=True)\n", - " self.plot = (self.Image * self.vector_dmap)\n", - " self.widget() \n", + " self.vector_dmap = hv.DynamicMap(hv.VectorField, streams=[self.pipe1]).opts(\n", + " magnitude=\"mag\", aspect=1\n", + " )\n", + " self.Image = hv.DynamicMap(hv.Image, streams=[self.pipe2]).opts(\n", + " cmap=\"coolwarm\", colorbar=True\n", + " )\n", + " self.plot = self.Image * self.vector_dmap\n", + " self.widget()\n", "\n", " def get_existing_subdirectories(self):\n", " cwd = os.getcwd()\n", " list_dir = os.listdir(cwd)\n", " monitored_directories = {\n", - " os.path.join(cwd, i) for i in list_dir if os.path.isdir(os.path.join(cwd, i))\n", + " os.path.join(cwd, i)\n", + " for i in list_dir\n", + " if os.path.isdir(os.path.join(cwd, i))\n", " }\n", " return monitored_directories\n", "\n", @@ -224,51 +229,53 @@ " self.monitor_new_directory(event.src_path)\n", " else:\n", " print(f\"New file detected: {event.src_path}\")\n", - " #self.widget(event.src_path)\n", + " # self.widget(event.src_path)\n", " self.process_file(event.src_path)\n", - " \n", + "\n", " def monitor_new_directory(self, directory_path):\n", " if directory_path not in self.monitored_directories:\n", " self.monitored_directories.add(directory_path)\n", - " #self.observer.schedule(self, path=directory_path, recursive=False)\n", + " # self.observer.schedule(self, path=directory_path, recursive=False)\n", " print(f\"Now monitoring: {directory_path}\")\n", "\n", " def process_file(self, file_path):\n", " # Only process files with specific extensions\n", - " if file_path.endswith(r'.omf'):\n", + " if file_path.endswith(r\".omf\"):\n", " field = df.Field.from_file(rf\"{file_path}\")\n", " field_xr = field.to_xarray()\n", - " #x_array for each vdim component\n", - " xxa = field_xr.sel(vdims='x') # arrow_x at each coordinate\n", - " yxa = field_xr.sel(vdims='y') # arrow_y\n", - " zxa = field_xr.sel(vdims='z') # arrow_z\n", + " # x_array for each vdim component\n", + " xxa = field_xr.sel(vdims=\"x\") # arrow_x at each coordinate\n", + " yxa = field_xr.sel(vdims=\"y\") # arrow_y\n", + " zxa = field_xr.sel(vdims=\"z\") # arrow_z\n", "\n", - " angle = np.arctan2(yxa, xxa) # angle of vector line\n", - " mag = np.sqrt(xxa**2 + yxa**2) # magnitude of vector\n", - " \n", + " angle = np.arctan2(yxa, xxa) # angle of vector line\n", + " mag = np.sqrt(xxa**2 + yxa**2) # magnitude of vector\n", "\n", - " table = xr.Dataset(dict(angle=angle[:,:,0], mag = mag[:,:,0], W = zxa[:,:,0])) #xarray of processed data needed for hv visualisation\n", + " table = xr.Dataset(\n", + " dict(angle=angle[:, :, 0], mag=mag[:, :, 0], W=zxa[:, :, 0])\n", + " ) # xarray of processed data needed for hv visualisation\n", "\n", - " self.pipe1.send(table) #pipe for vectorfield\n", - " self.pipe2.send(table[\"W\"]) # pipe for scalar field\n", + " self.pipe1.send(table) # pipe for vectorfield\n", + " self.pipe2.send(table[\"W\"]) # pipe for scalar field\n", "\n", - " self.file_count +=1\n", + " self.file_count += 1\n", " self.check_observer()\n", "\n", - " self.time_step.value = (self.t/self.max_count)*(self.file_count+1)\n", + " self.time_step.value = (self.t / self.max_count) * (self.file_count + 1)\n", "\n", - " if file_path.endswith(r'm0.omf'):\n", - " \n", - " display(self.plot,self.time_step) # display one updating dmap that initially gets created with m0 file\n", + " if file_path.endswith(r\"m0.omf\"):\n", + " display(\n", + " self.plot, self.time_step\n", + " ) # display one updating dmap that initially gets created with m0 file\n", "\n", " def widget(self):\n", " time_step = pn.indicators.Number(\n", " name=\"t:\",\n", " format=\"{value:.2g}\",\n", - " title_size=\"10pt\", \n", + " title_size=\"10pt\",\n", " font_size=\"10pt\",\n", - " default_color=\"white\"\n", - " )\n", + " default_color=\"white\",\n", + " )\n", " self.time_step = time_step\n", "\n", " def check_observer(self):\n", @@ -284,7 +291,7 @@ "metadata": {}, "outputs": [], "source": [ - "def main(t, max_count, working_directory = os.getcwd()):\n", + "def main(t, max_count, working_directory=os.getcwd()):\n", " \"\"\"\n", " Function to plot hv plots whilst simultaneously running a Time Driver simulation\n", "\n", @@ -295,7 +302,7 @@ " \"\"\"\n", " # working_directory defaulted to cwd\n", "\n", - " #stop flag used to terminate thread when all files are produced.\n", + " # stop flag used to terminate thread when all files are produced.\n", " stop_flag = [False]\n", "\n", " # Set up the observer\n", @@ -308,7 +315,7 @@ "\n", " # Start the observer\n", " observer.start()\n", - " \n", + "\n", " try:\n", " while not stop_flag[0]:\n", " time.sleep(5) # Keeps the script running\n", @@ -389,7 +396,7 @@ } ], "source": [ - "main(t=5e-9, max_count=10) # replace t and max_count with data read from info.json" + "main(t=5e-9, max_count=10) # replace t and max_count with data read from info.json" ] } ], From 7f5e1d149ed4d60c0c244c3d21d7713f9c11e554 Mon Sep 17 00:00:00 2001 From: N-3ielo Date: Sat, 5 Apr 2025 02:24:46 +0100 Subject: [PATCH 3/7] Added markdown and changed from user input to read info.json --- examples/Watchdog_live_view_demo.ipynb | 193 +++++++++++++++++-------- 1 file changed, 129 insertions(+), 64 deletions(-) diff --git a/examples/Watchdog_live_view_demo.ipynb b/examples/Watchdog_live_view_demo.ipynb index 1820415..78f730a 100644 --- a/examples/Watchdog_live_view_demo.ipynb +++ b/examples/Watchdog_live_view_demo.ipynb @@ -1,8 +1,60 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Holoviews live demo" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook is a demo on how we can concurrently visualise and run OC Time Driver simulations. \n", + "\n", + "This function is designed to work in parallel with a second notebook open. This second notebook should have an ubermag system initialised and it is where the Time Driver simulation is run. \n", + "\n", + "The system this function was built and tested on was a similar system to the Permalloy disk sample defined in the Ubermag demo. Please refer to the Ubermag demo for detail on how to initialise the system." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Module Info" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To concurrently visualise and run a Time Driver simulation we use common python modules but for the specialised tasks of monitoring directory events and handling a data pipeline we use the Watchdog and holoviews.streams.Pipe .\n", + "\n", + "\n", + "With Watchdog we can assign a directory to monitor. This directory will be where we have our simulation files produced and stored. Once we have assigned a directory to monitor we can create a visualisation pipeline with holoviews.streams.Pipe.\n", + "\n", + "\n", + "With holoviews.streams.Pipe we can pass formatted data from .omf files through a holoviews pipe where they are displayed onto an updating holoviews DynamicMap." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Simulation" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Firstly, we import the necessary modules" + ] + }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 47, "metadata": {}, "outputs": [ { @@ -49,12 +101,12 @@ "data": { "application/vnd.holoviews_exec.v0+json": "", "text/html": [ - "
\n", - "
\n", + "
\n", + "
\n", "
\n", "" + ] + }, + "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.2/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.2/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));", + "application/vnd.holoviews_load.v0+json": "" + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": "\nif ((window.PyViz === undefined) || (window.PyViz instanceof HTMLElement)) {\n window.PyViz = {comms: {}, comm_status:{}, kernels:{}, receivers: {}, plot_index: []}\n}\n\n\n function JupyterCommManager() {\n }\n\n JupyterCommManager.prototype.register_target = function(plot_id, comm_id, msg_handler) {\n if (window.comm_manager || ((window.Jupyter !== undefined) && (Jupyter.notebook.kernel != null))) {\n var comm_manager = window.comm_manager || Jupyter.notebook.kernel.comm_manager;\n comm_manager.register_target(comm_id, function(comm) {\n comm.on_msg(msg_handler);\n });\n } else if ((plot_id in window.PyViz.kernels) && (window.PyViz.kernels[plot_id])) {\n window.PyViz.kernels[plot_id].registerCommTarget(comm_id, function(comm) {\n comm.onMsg = msg_handler;\n });\n } else if (typeof google != 'undefined' && google.colab.kernel != null) {\n google.colab.kernel.comms.registerTarget(comm_id, (comm) => {\n var messages = comm.messages[Symbol.asyncIterator]();\n function processIteratorResult(result) {\n var message = result.value;\n console.log(message)\n var content = {data: message.data, comm_id};\n var buffers = []\n for (var buffer of message.buffers || []) {\n buffers.push(new DataView(buffer))\n }\n var metadata = message.metadata || {};\n var msg = {content, buffers, metadata}\n msg_handler(msg);\n return messages.next().then(processIteratorResult);\n }\n return messages.next().then(processIteratorResult);\n })\n }\n }\n\n JupyterCommManager.prototype.get_client_comm = function(plot_id, comm_id, msg_handler) {\n if (comm_id in window.PyViz.comms) {\n return window.PyViz.comms[comm_id];\n } else if (window.comm_manager || ((window.Jupyter !== undefined) && (Jupyter.notebook.kernel != null))) {\n var comm_manager = window.comm_manager || Jupyter.notebook.kernel.comm_manager;\n var comm = comm_manager.new_comm(comm_id, {}, {}, {}, comm_id);\n if (msg_handler) {\n comm.on_msg(msg_handler);\n }\n } else if ((plot_id in window.PyViz.kernels) && (window.PyViz.kernels[plot_id])) {\n var comm = window.PyViz.kernels[plot_id].connectToComm(comm_id);\n comm.open();\n if (msg_handler) {\n comm.onMsg = msg_handler;\n }\n } else if (typeof google != 'undefined' && google.colab.kernel != null) {\n var comm_promise = google.colab.kernel.comms.open(comm_id)\n comm_promise.then((comm) => {\n window.PyViz.comms[comm_id] = comm;\n if (msg_handler) {\n var messages = comm.messages[Symbol.asyncIterator]();\n function processIteratorResult(result) {\n var message = result.value;\n var content = {data: message.data};\n var metadata = message.metadata || {comm_id};\n var msg = {content, metadata}\n msg_handler(msg);\n return messages.next().then(processIteratorResult);\n }\n return messages.next().then(processIteratorResult);\n }\n }) \n var sendClosure = (data, metadata, buffers, disposeOnDone) => {\n return comm_promise.then((comm) => {\n comm.send(data, metadata, buffers, disposeOnDone);\n });\n };\n var comm = {\n send: sendClosure\n };\n }\n window.PyViz.comms[comm_id] = comm;\n return comm;\n }\n window.PyViz.comm_manager = new JupyterCommManager();\n \n\n\nvar JS_MIME_TYPE = 'application/javascript';\nvar HTML_MIME_TYPE = 'text/html';\nvar EXEC_MIME_TYPE = 'application/vnd.holoviews_exec.v0+json';\nvar CLASS_NAME = 'output';\n\n/**\n * Render data to the DOM node\n */\nfunction render(props, node) {\n var div = document.createElement(\"div\");\n var script = document.createElement(\"script\");\n node.appendChild(div);\n node.appendChild(script);\n}\n\n/**\n * Handle when a new output is added\n */\nfunction handle_add_output(event, handle) {\n var output_area = handle.output_area;\n var output = handle.output;\n if ((output.data == undefined) || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {\n return\n }\n var id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n var toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n if (id !== undefined) {\n var nchildren = toinsert.length;\n var html_node = toinsert[nchildren-1].children[0];\n html_node.innerHTML = output.data[HTML_MIME_TYPE];\n var scripts = [];\n var nodelist = html_node.querySelectorAll(\"script\");\n for (var i in nodelist) {\n if (nodelist.hasOwnProperty(i)) {\n scripts.push(nodelist[i])\n }\n }\n\n scripts.forEach( function (oldScript) {\n var newScript = document.createElement(\"script\");\n var attrs = [];\n var nodemap = oldScript.attributes;\n for (var j in nodemap) {\n if (nodemap.hasOwnProperty(j)) {\n attrs.push(nodemap[j])\n }\n }\n attrs.forEach(function(attr) { newScript.setAttribute(attr.name, attr.value) });\n newScript.appendChild(document.createTextNode(oldScript.innerHTML));\n oldScript.parentNode.replaceChild(newScript, oldScript);\n });\n if (JS_MIME_TYPE in output.data) {\n toinsert[nchildren-1].children[1].textContent = output.data[JS_MIME_TYPE];\n }\n output_area._hv_plot_id = id;\n if ((window.Bokeh !== undefined) && (id in Bokeh.index)) {\n window.PyViz.plot_index[id] = Bokeh.index[id];\n } else {\n window.PyViz.plot_index[id] = null;\n }\n } else if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n var bk_div = document.createElement(\"div\");\n bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n var script_attrs = bk_div.children[0].attributes;\n for (var i = 0; i < script_attrs.length; i++) {\n toinsert[toinsert.length - 1].childNodes[1].setAttribute(script_attrs[i].name, script_attrs[i].value);\n }\n // store reference to server id on output_area\n output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n }\n}\n\n/**\n * Handle when an output is cleared or removed\n */\nfunction handle_clear_output(event, handle) {\n var id = handle.cell.output_area._hv_plot_id;\n var server_id = handle.cell.output_area._bokeh_server_id;\n if (((id === undefined) || !(id in PyViz.plot_index)) && (server_id !== undefined)) { return; }\n var comm = window.PyViz.comm_manager.get_client_comm(\"hv-extension-comm\", \"hv-extension-comm\", function () {});\n if (server_id !== null) {\n comm.send({event_type: 'server_delete', 'id': server_id});\n return;\n } else if (comm !== null) {\n comm.send({event_type: 'delete', 'id': id});\n }\n delete PyViz.plot_index[id];\n if ((window.Bokeh !== undefined) & (id in window.Bokeh.index)) {\n var doc = window.Bokeh.index[id].model.document\n doc.clear();\n const i = window.Bokeh.documents.indexOf(doc);\n if (i > -1) {\n window.Bokeh.documents.splice(i, 1);\n }\n }\n}\n\n/**\n * Handle kernel restart event\n */\nfunction handle_kernel_cleanup(event, handle) {\n delete PyViz.comms[\"hv-extension-comm\"];\n window.PyViz.plot_index = {}\n}\n\n/**\n * Handle update_display_data messages\n */\nfunction handle_update_output(event, handle) {\n handle_clear_output(event, {cell: {output_area: handle.output_area}})\n handle_add_output(event, handle)\n}\n\nfunction register_renderer(events, OutputArea) {\n function append_mime(data, metadata, element) {\n // create a DOM node to render to\n var toinsert = this.create_output_subarea(\n metadata,\n CLASS_NAME,\n EXEC_MIME_TYPE\n );\n this.keyboard_manager.register_events(toinsert);\n // Render to node\n var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n render(props, toinsert[0]);\n element.append(toinsert);\n return toinsert\n }\n\n events.on('output_added.OutputArea', handle_add_output);\n events.on('output_updated.OutputArea', handle_update_output);\n events.on('clear_output.CodeCell', handle_clear_output);\n events.on('delete.Cell', handle_clear_output);\n events.on('kernel_ready.Kernel', handle_kernel_cleanup);\n\n OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n safe: true,\n index: 0\n });\n}\n\nif (window.Jupyter !== undefined) {\n try {\n var events = require('base/js/events');\n var OutputArea = require('notebook/js/outputarea').OutputArea;\n if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n register_renderer(events, OutputArea);\n }\n } catch(err) {\n }\n}\n", + "application/vnd.holoviews_load.v0+json": "" + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/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.2/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));", + "application/vnd.holoviews_load.v0+json": "" + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": "\nif ((window.PyViz === undefined) || (window.PyViz instanceof HTMLElement)) {\n window.PyViz = {comms: {}, comm_status:{}, kernels:{}, receivers: {}, plot_index: []}\n}\n\n\n function JupyterCommManager() {\n }\n\n JupyterCommManager.prototype.register_target = function(plot_id, comm_id, msg_handler) {\n if (window.comm_manager || ((window.Jupyter !== undefined) && (Jupyter.notebook.kernel != null))) {\n var comm_manager = window.comm_manager || Jupyter.notebook.kernel.comm_manager;\n comm_manager.register_target(comm_id, function(comm) {\n comm.on_msg(msg_handler);\n });\n } else if ((plot_id in window.PyViz.kernels) && (window.PyViz.kernels[plot_id])) {\n window.PyViz.kernels[plot_id].registerCommTarget(comm_id, function(comm) {\n comm.onMsg = msg_handler;\n });\n } else if (typeof google != 'undefined' && google.colab.kernel != null) {\n google.colab.kernel.comms.registerTarget(comm_id, (comm) => {\n var messages = comm.messages[Symbol.asyncIterator]();\n function processIteratorResult(result) {\n var message = result.value;\n console.log(message)\n var content = {data: message.data, comm_id};\n var buffers = []\n for (var buffer of message.buffers || []) {\n buffers.push(new DataView(buffer))\n }\n var metadata = message.metadata || {};\n var msg = {content, buffers, metadata}\n msg_handler(msg);\n return messages.next().then(processIteratorResult);\n }\n return messages.next().then(processIteratorResult);\n })\n }\n }\n\n JupyterCommManager.prototype.get_client_comm = function(plot_id, comm_id, msg_handler) {\n if (comm_id in window.PyViz.comms) {\n return window.PyViz.comms[comm_id];\n } else if (window.comm_manager || ((window.Jupyter !== undefined) && (Jupyter.notebook.kernel != null))) {\n var comm_manager = window.comm_manager || Jupyter.notebook.kernel.comm_manager;\n var comm = comm_manager.new_comm(comm_id, {}, {}, {}, comm_id);\n if (msg_handler) {\n comm.on_msg(msg_handler);\n }\n } else if ((plot_id in window.PyViz.kernels) && (window.PyViz.kernels[plot_id])) {\n var comm = window.PyViz.kernels[plot_id].connectToComm(comm_id);\n comm.open();\n if (msg_handler) {\n comm.onMsg = msg_handler;\n }\n } else if (typeof google != 'undefined' && google.colab.kernel != null) {\n var comm_promise = google.colab.kernel.comms.open(comm_id)\n comm_promise.then((comm) => {\n window.PyViz.comms[comm_id] = comm;\n if (msg_handler) {\n var messages = comm.messages[Symbol.asyncIterator]();\n function processIteratorResult(result) {\n var message = result.value;\n var content = {data: message.data};\n var metadata = message.metadata || {comm_id};\n var msg = {content, metadata}\n msg_handler(msg);\n return messages.next().then(processIteratorResult);\n }\n return messages.next().then(processIteratorResult);\n }\n }) \n var sendClosure = (data, metadata, buffers, disposeOnDone) => {\n return comm_promise.then((comm) => {\n comm.send(data, metadata, buffers, disposeOnDone);\n });\n };\n var comm = {\n send: sendClosure\n };\n }\n window.PyViz.comms[comm_id] = comm;\n return comm;\n }\n window.PyViz.comm_manager = new JupyterCommManager();\n \n\n\nvar JS_MIME_TYPE = 'application/javascript';\nvar HTML_MIME_TYPE = 'text/html';\nvar EXEC_MIME_TYPE = 'application/vnd.holoviews_exec.v0+json';\nvar CLASS_NAME = 'output';\n\n/**\n * Render data to the DOM node\n */\nfunction render(props, node) {\n var div = document.createElement(\"div\");\n var script = document.createElement(\"script\");\n node.appendChild(div);\n node.appendChild(script);\n}\n\n/**\n * Handle when a new output is added\n */\nfunction handle_add_output(event, handle) {\n var output_area = handle.output_area;\n var output = handle.output;\n if ((output.data == undefined) || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {\n return\n }\n var id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n var toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n if (id !== undefined) {\n var nchildren = toinsert.length;\n var html_node = toinsert[nchildren-1].children[0];\n html_node.innerHTML = output.data[HTML_MIME_TYPE];\n var scripts = [];\n var nodelist = html_node.querySelectorAll(\"script\");\n for (var i in nodelist) {\n if (nodelist.hasOwnProperty(i)) {\n scripts.push(nodelist[i])\n }\n }\n\n scripts.forEach( function (oldScript) {\n var newScript = document.createElement(\"script\");\n var attrs = [];\n var nodemap = oldScript.attributes;\n for (var j in nodemap) {\n if (nodemap.hasOwnProperty(j)) {\n attrs.push(nodemap[j])\n }\n }\n attrs.forEach(function(attr) { newScript.setAttribute(attr.name, attr.value) });\n newScript.appendChild(document.createTextNode(oldScript.innerHTML));\n oldScript.parentNode.replaceChild(newScript, oldScript);\n });\n if (JS_MIME_TYPE in output.data) {\n toinsert[nchildren-1].children[1].textContent = output.data[JS_MIME_TYPE];\n }\n output_area._hv_plot_id = id;\n if ((window.Bokeh !== undefined) && (id in Bokeh.index)) {\n window.PyViz.plot_index[id] = Bokeh.index[id];\n } else {\n window.PyViz.plot_index[id] = null;\n }\n } else if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n var bk_div = document.createElement(\"div\");\n bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n var script_attrs = bk_div.children[0].attributes;\n for (var i = 0; i < script_attrs.length; i++) {\n toinsert[toinsert.length - 1].childNodes[1].setAttribute(script_attrs[i].name, script_attrs[i].value);\n }\n // store reference to server id on output_area\n output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n }\n}\n\n/**\n * Handle when an output is cleared or removed\n */\nfunction handle_clear_output(event, handle) {\n var id = handle.cell.output_area._hv_plot_id;\n var server_id = handle.cell.output_area._bokeh_server_id;\n if (((id === undefined) || !(id in PyViz.plot_index)) && (server_id !== undefined)) { return; }\n var comm = window.PyViz.comm_manager.get_client_comm(\"hv-extension-comm\", \"hv-extension-comm\", function () {});\n if (server_id !== null) {\n comm.send({event_type: 'server_delete', 'id': server_id});\n return;\n } else if (comm !== null) {\n comm.send({event_type: 'delete', 'id': id});\n }\n delete PyViz.plot_index[id];\n if ((window.Bokeh !== undefined) & (id in window.Bokeh.index)) {\n var doc = window.Bokeh.index[id].model.document\n doc.clear();\n const i = window.Bokeh.documents.indexOf(doc);\n if (i > -1) {\n window.Bokeh.documents.splice(i, 1);\n }\n }\n}\n\n/**\n * Handle kernel restart event\n */\nfunction handle_kernel_cleanup(event, handle) {\n delete PyViz.comms[\"hv-extension-comm\"];\n window.PyViz.plot_index = {}\n}\n\n/**\n * Handle update_display_data messages\n */\nfunction handle_update_output(event, handle) {\n handle_clear_output(event, {cell: {output_area: handle.output_area}})\n handle_add_output(event, handle)\n}\n\nfunction register_renderer(events, OutputArea) {\n function append_mime(data, metadata, element) {\n // create a DOM node to render to\n var toinsert = this.create_output_subarea(\n metadata,\n CLASS_NAME,\n EXEC_MIME_TYPE\n );\n this.keyboard_manager.register_events(toinsert);\n // Render to node\n var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n render(props, toinsert[0]);\n element.append(toinsert);\n return toinsert\n }\n\n events.on('output_added.OutputArea', handle_add_output);\n events.on('output_updated.OutputArea', handle_update_output);\n events.on('clear_output.CodeCell', handle_clear_output);\n events.on('delete.Cell', handle_clear_output);\n events.on('kernel_ready.Kernel', handle_kernel_cleanup);\n\n OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n safe: true,\n index: 0\n });\n}\n\nif (window.Jupyter !== undefined) {\n try {\n var events = require('base/js/events');\n var OutputArea = require('notebook/js/outputarea').OutputArea;\n if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n register_renderer(events, OutputArea);\n }\n } catch(err) {\n }\n}\n", + "application/vnd.holoviews_load.v0+json": "" + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.holoviews_exec.v0+json": "", + "text/html": [ + "
\n", + "
\n", + "
\n", + "" + ] + }, + "metadata": { + "application/vnd.holoviews_exec.v0+json": { + "id": "3c95a725-3a27-49f4-826f-a0d1ff8bdf54" + } + }, + "output_type": "display_data" + } + ], + "source": [ + "import pyvista as pv\n", + "import discretisedfield as df\n", + "import micromagneticmodel as mm\n", + "import micromagneticdata as mdata\n", + "import oommfc as oc\n", + "import panel as pn\n", + "\n", + "pn.extension()" + ] + }, + { + "cell_type": "markdown", + "id": "b60581e3", + "metadata": {}, + "source": [ + "Below we initialise our system" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d2819758", + "metadata": {}, + "outputs": [], + "source": [ + "# Geometry\n", + "r = 50e-9 # Radius of the thin nano magnetic disk (m)\n", + "thickness = 10e-9 # sample thickness (m)\n", + "\n", + "# Material (Permalloy) parameters\n", + "Ms = 1e5 # saturation magnetisation (A/m)\n", + "A = 13e-12 # exchange energy constant (J/m)\n", + "\n", + "# Dynamics (LLG equation) parameters\n", + "gamma0 = mm.consts.gamma0 # gyromagnetic ratio (m/As)\n", + "alpha = 0.05 # Gilbert damping\n", + "\n", + "system = mm.System(name=\"test_1\")\n", + "\n", + "# Energy equation. We omit Zeeman energy term, because H=0.\n", + "system.energy = mm.Exchange(A=A) + mm.Demag()\n", + "\n", + "# Dynamics equation\n", + "system.dynamics = mm.Precession(gamma0=gamma0) + mm.Damping(alpha=alpha)\n", + "\n", + "\n", + "# initial magnetisation state\n", + "def m_init(point):\n", + " x, y, _ = point\n", + " c = 1e9 # (1/m)\n", + " return (-c * y, c * x, 0.1)\n", + "\n", + "\n", + "# Defining the geometry of the material as a circular disk\n", + "def Ms_func(point):\n", + " x, y, _ = point\n", + " if x**2 + y**2 <= r**2:\n", + " return Ms\n", + " else:\n", + " return 0\n", + "\n", + "\n", + "# Sample's centre is placed at origin\n", + "region = df.Region(p1=(-r, -r, -thickness / 2), p2=(r, r, thickness / 2))\n", + "mesh = df.Mesh(region=region, cell=(5e-9, 5e-9, 10e-9)) # default cell = (5e-9, 5e-9, 10e-9)\n", + "\n", + "system.m = df.Field(mesh, nvdim=3, value=m_init, norm=Ms_func, valid=\"norm\")" + ] + }, + { + "cell_type": "markdown", + "id": "25e93571", + "metadata": {}, + "source": [ + "We run our `TimeDriver` simulation below for **5** ns and save our magnetisation in **50** steps" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ef868e42", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "4137a904939141c590292a4bde1e808b", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Running OOMMF (ExeOOMMFRunner): 0%| | 0/100 files written [00:00]" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Running OOMMF (ExeOOMMFRunner)[2025/04/05 21:08] took 33.0 s\n" + ] + } + ], + "source": [ + "H = (3.4e4, 0, 0) #external magnetic field A/m\n", + "\n", + "td = oc.TimeDriver()\n", + "td.drive(system, t=5e-9, n=50, verbose=2) " + ] + }, + { + "cell_type": "markdown", + "id": "bf7b038a", + "metadata": {}, + "source": [ + "Our example drive to be used for analysis" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "84d6af40", + "metadata": {}, + "outputs": [], + "source": [ + "drive = mdata.Data(system.name)[-1]" + ] + }, + { + "cell_type": "markdown", + "id": "e0d705ca", + "metadata": {}, + "source": [ + "## Functions\n", + "\n", + "Below we have functions for four pyvista plotting functions. These are:\n", + "\n", + "1. `drive.pyvista.vector()`\n", + "2. `drive.pyvista.volume()`\n", + "3. `drive.pyvista.contour()`\n", + "4. `drive.pyvista.streamlines()`" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "407a9796", + "metadata": {}, + "outputs": [], + "source": [ + "def drive_pv_vector(drive):\n", + " \"\"\"\n", + " pyvista vector plot.\n", + "\n", + " This function visualises a vector field. We use a panel slider change the time step \n", + " and a panel number indicator to label the corresponding index for the time step we are on.\n", + "\n", + " Parameters\n", + " ----------\n", + " drive:\n", + "\n", + " Drive object that is used for analysis\n", + " \"\"\"\n", + " # Create a PyVista plotter\n", + " plot = pv.Plotter()\n", + "\n", + " def time_pv(value):\n", + " drive[value].pyvista.vector(name=\"x\", plotter=plot)\n", + " time_step.value = (drive.info['t'] / drive.n) * (value + 1)\n", + "\n", + " # Create a Panel slider\n", + " slider = pn.widgets.IntSlider(name=\"Index\", value=0, start=0, end=drive.n - 1, step=1)\n", + " \n", + " # Create a Panel FloatInput for the time step\n", + " time_step = pn.indicators.Number(\n", + " name=\"t:\",\n", + " format=\"{value:.2g}\",\n", + " title_size=\"10pt\", \n", + " font_size=\"10pt\",\n", + " default_color=\"white\"\n", + " )\n", + "\n", + " # Bind the slider to update function\n", + " slider.param.watch(lambda event: time_pv(event.new), \"value\") #callback function where event.new is the newest slider value\n", + " \n", + " # Initialize with first value\n", + " time_pv(slider.value)\n", + " \n", + " # Panel layout\n", + " layout = pn.Row(slider,\n", + " time_step,\n", + " styles={\"background\": \"black\",\n", + " \"padding\": \"10px\",\n", + " \"border-radius\": \"10px\",\n", + " \"border\":\"1px solid white\"})\n", + " \n", + " display(layout)\n", + " plot.show()\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "da3add81", + "metadata": {}, + "outputs": [], + "source": [ + "def drive_pv_volume(drive):\n", + " \"\"\"\n", + " pyvista volume plot.\n", + "\n", + " This function visualises the scalar field within a three-dimensional region by rendering a volume.\n", + " We use a panel slider change the time step and a panel number indicator to label the corresponding\n", + " index for the time step we are on.\n", + "\n", + " Parameters\n", + " ----------\n", + " drive:\n", + "\n", + " Drive object that is used for analysis\n", + " \"\"\"\n", + " # Create a PyVista plotter\n", + " plot = pv.Plotter()\n", + "\n", + " def time_pv(value):\n", + " drive[value].pyvista.volume(name=\"x\", plotter=plot)\n", + " time_step.value = (drive.info['t'] / drive.n) * (value + 1)\n", + "\n", + " # Create a Panel slider\n", + " slider = pn.widgets.IntSlider(name=\"Index\", value=0, start=0, end=drive.n - 1, step=1)\n", + " \n", + " # Create a Panel FloatInput for the time step\n", + " time_step = pn.indicators.Number(\n", + " name=\"t:\",\n", + " format=\"{value:.2g}\",\n", + " title_size=\"10pt\", \n", + " font_size=\"10pt\",\n", + " default_color=\"white\"\n", + " )\n", + "\n", + " # Bind the slider to update function\n", + " slider.param.watch(lambda event: time_pv(event.new), \"value\") #callback function where event.new is the newest slider value\n", + " \n", + " # Initialize with first value\n", + " time_pv(slider.value)\n", + " \n", + " # Panel layout\n", + " layout = pn.Row(slider, time_step,\n", + " styles={\"background\": \"black\",\n", + " \"padding\": \"10px\",\n", + " \"border-radius\": \"10px\",\n", + " \"border\":\"1px solid white\"})\n", + " \n", + " display(layout)\n", + " plot.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f7a9a4c0", + "metadata": {}, + "outputs": [], + "source": [ + "def drive_pv_contour(\n", + " drive,\n", + " isosurfaces = 10\n", + " ):\n", + " \"\"\"\n", + " pyvista contour plot.\n", + "\n", + " This function computes isosurfaces of the field. We use a panel slider \n", + " change the time step and a panel number indicator to label the corresponding\n", + " index for the time step we are on.\n", + "\n", + " Parameters\n", + " ----------\n", + " drive:\n", + "\n", + " Drive object that is used for analysis\n", + "\n", + " isosurfaces:\n", + "\n", + " Number of isosurfaces. Defaults to 10\n", + " \"\"\"\n", + " # Create a PyVista plotter\n", + " plot = pv.Plotter()\n", + "\n", + " def time_pv(value):\n", + " drive[value].pyvista.contour(name=\"x\", plotter=plot, isosurfaces=isosurfaces)\n", + " time_step.value = (drive.info['t'] / drive.n) * (value + 1)\n", + "\n", + " # Create a Panel slider\n", + " slider = pn.widgets.IntSlider(name=\"Index\", value=0, start=0, end=drive.n - 1, step=1)\n", + " \n", + " # Create a Panel FloatInput for the time step\n", + " time_step = pn.indicators.Number(\n", + " name=\"t:\",\n", + " format=\"{value:.2g}\",\n", + " title_size=\"10pt\", \n", + " font_size=\"10pt\",\n", + " default_color=\"white\"\n", + " )\n", + "\n", + " # Bind the slider to update function\n", + " slider.param.watch(lambda event: time_pv(event.new), \"value\") #callback function where event.new is the newest slider value\n", + " \n", + " # Initialize with first value\n", + " time_pv(slider.value)\n", + " \n", + " # Panel layout\n", + " layout = pn.Row(slider, time_step,\n", + " styles={\"background\": \"black\",\n", + " \"padding\": \"10px\",\n", + " \"border-radius\": \"10px\",\n", + " \"border\":\"1px solid white\"})\n", + " \n", + " display(layout)\n", + " plot.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "62b40d4f", + "metadata": {}, + "outputs": [], + "source": [ + "def drive_pv_streamlines(drive):\n", + " \"\"\"\n", + " pyvista streamline plot.\n", + "\n", + " This function generates a plot of streamines. We use a \n", + " panel slider change the time step and a panel number \n", + " indicator to label the correspondingindex for the time \n", + " step we are on.\n", + "\n", + " Parameters\n", + " ----------\n", + " drive:\n", + "\n", + " Drive object that is used for analysis\n", + " \"\"\"\n", + " # Create a PyVista plotter\n", + " plot = pv.Plotter()\n", + "\n", + " def time_pv(value):\n", + " drive[value].pyvista.streamlines(name=\"x\", plotter=plot)\n", + " time_step.value = (drive.info['t'] / drive.n) * (value + 1)\n", + "\n", + " # Create a Panel slider\n", + " slider = pn.widgets.IntSlider(name=\"Index\", value=0, start=0, end=drive.n - 1, step=1)\n", + " \n", + " # Create a Panel FloatInput for the time step\n", + " time_step = pn.indicators.Number(\n", + " name=\"t:\",\n", + " format=\"{value:.2g}\",\n", + " title_size=\"10pt\", \n", + " font_size=\"10pt\",\n", + " default_color=\"white\"\n", + " )\n", + "\n", + " # Bind the slider to update function\n", + " slider.param.watch(lambda event: time_pv(event.new), \"value\") #callback function where event.new is the newest slider value\n", + " \n", + " # Initialize with first value\n", + " time_pv(slider.value)\n", + " \n", + " # Panel layout\n", + " layout = pn.Row(slider, time_step,\n", + " styles={\"background\": \"black\",\n", + " \"padding\": \"10px\",\n", + " \"border-radius\": \"10px\",\n", + " \"border\":\"1px solid white\"})\n", + " \n", + " display(layout)\n", + " plot.show()" + ] + }, + { + "cell_type": "markdown", + "id": "07c56ab6", + "metadata": {}, + "source": [ + "## Code examples\n", + "\n", + "In the cells below we have an example of our `drive_pv_vector()` function and the holoviews equivalent, `drive.hv()`, to compare it with." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6bf2d1be", + "metadata": {}, + "outputs": [], + "source": [ + "drive_pv_vector(drive)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0060daf6", + "metadata": {}, + "outputs": [], + "source": [ + "drive.hv(kdims=[\"x\",\"y\"])" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From bbff7f95c8e6599b6a2aba3aa45430d773698b49 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 6 Apr 2025 15:13:00 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- examples/Pyvista_drive_demo.ipynb | 185 ++++++++++++++++++------------ 1 file changed, 109 insertions(+), 76 deletions(-) diff --git a/examples/Pyvista_drive_demo.ipynb b/examples/Pyvista_drive_demo.ipynb index f8e4ba2..eccaa50 100644 --- a/examples/Pyvista_drive_demo.ipynb +++ b/examples/Pyvista_drive_demo.ipynb @@ -282,7 +282,9 @@ "\n", "# Sample's centre is placed at origin\n", "region = df.Region(p1=(-r, -r, -thickness / 2), p2=(r, r, thickness / 2))\n", - "mesh = df.Mesh(region=region, cell=(5e-9, 5e-9, 10e-9)) # default cell = (5e-9, 5e-9, 10e-9)\n", + "mesh = df.Mesh(\n", + " region=region, cell=(5e-9, 5e-9, 10e-9)\n", + ") # default cell = (5e-9, 5e-9, 10e-9)\n", "\n", "system.m = df.Field(mesh, nvdim=3, value=m_init, norm=Ms_func, valid=\"norm\")" ] @@ -324,10 +326,10 @@ } ], "source": [ - "H = (3.4e4, 0, 0) #external magnetic field A/m\n", + "H = (3.4e4, 0, 0) # external magnetic field A/m\n", "\n", "td = oc.TimeDriver()\n", - "td.drive(system, t=5e-9, n=50, verbose=2) " + "td.drive(system, t=5e-9, n=50, verbose=2)" ] }, { @@ -374,7 +376,7 @@ " \"\"\"\n", " pyvista vector plot.\n", "\n", - " This function visualises a vector field. We use a panel slider change the time step \n", + " This function visualises a vector field. We use a panel slider change the time step\n", " and a panel number indicator to label the corresponding index for the time step we are on.\n", "\n", " Parameters\n", @@ -388,37 +390,44 @@ "\n", " def time_pv(value):\n", " drive[value].pyvista.vector(name=\"x\", plotter=plot)\n", - " time_step.value = (drive.info['t'] / drive.n) * (value + 1)\n", + " time_step.value = (drive.info[\"t\"] / drive.n) * (value + 1)\n", "\n", " # Create a Panel slider\n", - " slider = pn.widgets.IntSlider(name=\"Index\", value=0, start=0, end=drive.n - 1, step=1)\n", - " \n", + " slider = pn.widgets.IntSlider(\n", + " name=\"Index\", value=0, start=0, end=drive.n - 1, step=1\n", + " )\n", + "\n", " # Create a Panel FloatInput for the time step\n", " time_step = pn.indicators.Number(\n", " name=\"t:\",\n", " format=\"{value:.2g}\",\n", - " title_size=\"10pt\", \n", + " title_size=\"10pt\",\n", " font_size=\"10pt\",\n", - " default_color=\"white\"\n", - " )\n", + " default_color=\"white\",\n", + " )\n", "\n", " # Bind the slider to update function\n", - " slider.param.watch(lambda event: time_pv(event.new), \"value\") #callback function where event.new is the newest slider value\n", - " \n", + " slider.param.watch(\n", + " lambda event: time_pv(event.new), \"value\"\n", + " ) # callback function where event.new is the newest slider value\n", + "\n", " # Initialize with first value\n", " time_pv(slider.value)\n", - " \n", + "\n", " # Panel layout\n", - " layout = pn.Row(slider,\n", - " time_step,\n", - " styles={\"background\": \"black\",\n", - " \"padding\": \"10px\",\n", - " \"border-radius\": \"10px\",\n", - " \"border\":\"1px solid white\"})\n", - " \n", + " layout = pn.Row(\n", + " slider,\n", + " time_step,\n", + " styles={\n", + " \"background\": \"black\",\n", + " \"padding\": \"10px\",\n", + " \"border-radius\": \"10px\",\n", + " \"border\": \"1px solid white\",\n", + " },\n", + " )\n", + "\n", " display(layout)\n", - " plot.show()\n", - " " + " plot.show()" ] }, { @@ -447,33 +456,42 @@ "\n", " def time_pv(value):\n", " drive[value].pyvista.volume(name=\"x\", plotter=plot)\n", - " time_step.value = (drive.info['t'] / drive.n) * (value + 1)\n", + " time_step.value = (drive.info[\"t\"] / drive.n) * (value + 1)\n", "\n", " # Create a Panel slider\n", - " slider = pn.widgets.IntSlider(name=\"Index\", value=0, start=0, end=drive.n - 1, step=1)\n", - " \n", + " slider = pn.widgets.IntSlider(\n", + " name=\"Index\", value=0, start=0, end=drive.n - 1, step=1\n", + " )\n", + "\n", " # Create a Panel FloatInput for the time step\n", " time_step = pn.indicators.Number(\n", " name=\"t:\",\n", " format=\"{value:.2g}\",\n", - " title_size=\"10pt\", \n", + " title_size=\"10pt\",\n", " font_size=\"10pt\",\n", - " default_color=\"white\"\n", - " )\n", + " default_color=\"white\",\n", + " )\n", "\n", " # Bind the slider to update function\n", - " slider.param.watch(lambda event: time_pv(event.new), \"value\") #callback function where event.new is the newest slider value\n", - " \n", + " slider.param.watch(\n", + " lambda event: time_pv(event.new), \"value\"\n", + " ) # callback function where event.new is the newest slider value\n", + "\n", " # Initialize with first value\n", " time_pv(slider.value)\n", - " \n", + "\n", " # Panel layout\n", - " layout = pn.Row(slider, time_step,\n", - " styles={\"background\": \"black\",\n", - " \"padding\": \"10px\",\n", - " \"border-radius\": \"10px\",\n", - " \"border\":\"1px solid white\"})\n", - " \n", + " layout = pn.Row(\n", + " slider,\n", + " time_step,\n", + " styles={\n", + " \"background\": \"black\",\n", + " \"padding\": \"10px\",\n", + " \"border-radius\": \"10px\",\n", + " \"border\": \"1px solid white\",\n", + " },\n", + " )\n", + "\n", " display(layout)\n", " plot.show()" ] @@ -485,14 +503,11 @@ "metadata": {}, "outputs": [], "source": [ - "def drive_pv_contour(\n", - " drive,\n", - " isosurfaces = 10\n", - " ):\n", + "def drive_pv_contour(drive, isosurfaces=10):\n", " \"\"\"\n", " pyvista contour plot.\n", "\n", - " This function computes isosurfaces of the field. We use a panel slider \n", + " This function computes isosurfaces of the field. We use a panel slider\n", " change the time step and a panel number indicator to label the corresponding\n", " index for the time step we are on.\n", "\n", @@ -511,33 +526,42 @@ "\n", " def time_pv(value):\n", " drive[value].pyvista.contour(name=\"x\", plotter=plot, isosurfaces=isosurfaces)\n", - " time_step.value = (drive.info['t'] / drive.n) * (value + 1)\n", + " time_step.value = (drive.info[\"t\"] / drive.n) * (value + 1)\n", "\n", " # Create a Panel slider\n", - " slider = pn.widgets.IntSlider(name=\"Index\", value=0, start=0, end=drive.n - 1, step=1)\n", - " \n", + " slider = pn.widgets.IntSlider(\n", + " name=\"Index\", value=0, start=0, end=drive.n - 1, step=1\n", + " )\n", + "\n", " # Create a Panel FloatInput for the time step\n", " time_step = pn.indicators.Number(\n", " name=\"t:\",\n", " format=\"{value:.2g}\",\n", - " title_size=\"10pt\", \n", + " title_size=\"10pt\",\n", " font_size=\"10pt\",\n", - " default_color=\"white\"\n", - " )\n", + " default_color=\"white\",\n", + " )\n", "\n", " # Bind the slider to update function\n", - " slider.param.watch(lambda event: time_pv(event.new), \"value\") #callback function where event.new is the newest slider value\n", - " \n", + " slider.param.watch(\n", + " lambda event: time_pv(event.new), \"value\"\n", + " ) # callback function where event.new is the newest slider value\n", + "\n", " # Initialize with first value\n", " time_pv(slider.value)\n", - " \n", + "\n", " # Panel layout\n", - " layout = pn.Row(slider, time_step,\n", - " styles={\"background\": \"black\",\n", - " \"padding\": \"10px\",\n", - " \"border-radius\": \"10px\",\n", - " \"border\":\"1px solid white\"})\n", - " \n", + " layout = pn.Row(\n", + " slider,\n", + " time_step,\n", + " styles={\n", + " \"background\": \"black\",\n", + " \"padding\": \"10px\",\n", + " \"border-radius\": \"10px\",\n", + " \"border\": \"1px solid white\",\n", + " },\n", + " )\n", + "\n", " display(layout)\n", " plot.show()" ] @@ -553,9 +577,9 @@ " \"\"\"\n", " pyvista streamline plot.\n", "\n", - " This function generates a plot of streamines. We use a \n", - " panel slider change the time step and a panel number \n", - " indicator to label the correspondingindex for the time \n", + " This function generates a plot of streamines. We use a\n", + " panel slider change the time step and a panel number\n", + " indicator to label the correspondingindex for the time\n", " step we are on.\n", "\n", " Parameters\n", @@ -569,33 +593,42 @@ "\n", " def time_pv(value):\n", " drive[value].pyvista.streamlines(name=\"x\", plotter=plot)\n", - " time_step.value = (drive.info['t'] / drive.n) * (value + 1)\n", + " time_step.value = (drive.info[\"t\"] / drive.n) * (value + 1)\n", "\n", " # Create a Panel slider\n", - " slider = pn.widgets.IntSlider(name=\"Index\", value=0, start=0, end=drive.n - 1, step=1)\n", - " \n", + " slider = pn.widgets.IntSlider(\n", + " name=\"Index\", value=0, start=0, end=drive.n - 1, step=1\n", + " )\n", + "\n", " # Create a Panel FloatInput for the time step\n", " time_step = pn.indicators.Number(\n", " name=\"t:\",\n", " format=\"{value:.2g}\",\n", - " title_size=\"10pt\", \n", + " title_size=\"10pt\",\n", " font_size=\"10pt\",\n", - " default_color=\"white\"\n", - " )\n", + " default_color=\"white\",\n", + " )\n", "\n", " # Bind the slider to update function\n", - " slider.param.watch(lambda event: time_pv(event.new), \"value\") #callback function where event.new is the newest slider value\n", - " \n", + " slider.param.watch(\n", + " lambda event: time_pv(event.new), \"value\"\n", + " ) # callback function where event.new is the newest slider value\n", + "\n", " # Initialize with first value\n", " time_pv(slider.value)\n", - " \n", + "\n", " # Panel layout\n", - " layout = pn.Row(slider, time_step,\n", - " styles={\"background\": \"black\",\n", - " \"padding\": \"10px\",\n", - " \"border-radius\": \"10px\",\n", - " \"border\":\"1px solid white\"})\n", - " \n", + " layout = pn.Row(\n", + " slider,\n", + " time_step,\n", + " styles={\n", + " \"background\": \"black\",\n", + " \"padding\": \"10px\",\n", + " \"border-radius\": \"10px\",\n", + " \"border\": \"1px solid white\",\n", + " },\n", + " )\n", + "\n", " display(layout)\n", " plot.show()" ] @@ -627,7 +660,7 @@ "metadata": {}, "outputs": [], "source": [ - "drive.hv(kdims=[\"x\",\"y\"])" + "drive.hv(kdims=[\"x\", \"y\"])" ] } ],