Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions examples/00-wrapping_numpy_capabilities.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n\n# Write user defined Operator\nThis example shows how to create a simple DPF python plugin holding a single Operator.\nThis Operator called \"easy_statistics\" computes simple statistics quantities on a scalar Field with\nthe help of numpy.\nIt's a simple example displaying how routines can be wrapped in DPF python plugins.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Write Operator\nTo write the simplest DPF python plugins, a single python script is necessary.\nAn Operator implementation deriving from :class:`ansys.dpf.core.custom_operator.CustomOperatorBase`\nand a call to :py:func:`ansys.dpf.core.custom_operator.record_operator` are the 2 necessary steps to create a plugin.\nThe \"easy_statistics\" Operator will take a Field in input and return the first quartile, the median,\nthe third quartile and the variance. The python Operator and its recording seat in the\nfile plugins/easy_statistics.py. This file `easy_statistics.py` is downloaded and displayed here:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from ansys.dpf.core import examples\n\nGITHUB_SOURCE_URL = \"https://github.com/pyansys/pydpf-core/raw/examples/first_python_plugins/python-plugins\"\nEXAMPLE_FILE = GITHUB_SOURCE_URL + \"/easy_statistics.py\"\noperator_file_path = examples.downloads._retrieve_file(EXAMPLE_FILE, \"easy_statistics.py\", \"python-plugins\")\n\nimport IPython\nprint(IPython.display.Code(operator_file_path))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load Plugin\nOnce a python plugin is written, it can be loaded with the function :py:func:`ansys.dpf.core.core.load_library`\ntaking as first argument the path to the directory of the plugin, as second argument ``py_`` + the name of\nthe python script, and as last argument the function's name used to record operators.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import os\nfrom ansys.dpf import core as dpf\nfrom ansys.dpf.core import examples\n\noperator_server_file_path = dpf.upload_file_in_tmp_folder(operator_file_path)\ndpf.load_library(os.path.dirname(operator_server_file_path), \"py_easy_statistics\", \"load_operators\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once the Operator loaded, it can be instantiated with:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"new_operator = dpf.Operator(\"easy_statistics\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To use this new Operator, a workflow computing the norm of the displacement\nis connected to the \"easy_statistics\" Operator.\nMethods of the class ``easy_statistics`` are dynamically added thanks to the Operator's\nspecification defined in the plugin.\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. graphviz::\n\n digraph foo {\n graph [pad=\"0.5\", nodesep=\"0.3\", ranksep=\"0.3\"]\n node [shape=box, style=filled, fillcolor=\"#ffcc00\", margin=\"0\"];\n rankdir=LR;\n splines=line;\n ds [label=\"ds\", shape=box, style=filled, fillcolor=cadetblue2];\n ds -> displacement [style=dashed];\n displacement -> norm;\n norm -> easy_statistics;\n }\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use the Custom Operator\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"ds = dpf.DataSources(dpf.upload_file_in_tmp_folder(examples.static_rst))\ndisplacement = dpf.operators.result.displacement(data_sources=ds)\nnorm = dpf.operators.math.norm(displacement)\nnew_operator.inputs.connect(norm)\n\n\nprint(\"first quartile is\", new_operator.outputs.first_quartile())\nprint(\"median is\", new_operator.outputs.median())\nprint(\"third quartile is\", new_operator.outputs.third_quartile())\nprint(\"variance is\", new_operator.outputs.variance())"
]
}
],
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
129 changes: 129 additions & 0 deletions examples/01-package_python_operators.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n\n# Write user defined Operators as a package\nThis example shows how more complex DPF python plugins of Operators can be created as standard python packages.\nThe benefits of writing packages instead of simple scripts are: componentization (split the code in several\npython modules or files), distribution (with packages, standard python tools can be used to upload and\ndownload packages) and documentation (READMEs, docs, tests and examples can be added to the package).\n\nThis plugin will hold 2 different Operators:\n - One returning all the scoping ids having data higher than the average\n - One returning all the scoping ids having data lower than the average\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Write Operator\nFor this more advanced use case, a python package is created.\nEach Operator implementation derives from :class:`ansys.dpf.core.custom_operator.CustomOperatorBase`\nand a call to :py:func:`ansys.dpf.core.custom_operator.record_operator` records the Operators of the plugin.\nThe python package `average_filter_plugin` is downloaded and displayed here:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import IPython\nimport os\nfrom ansys.dpf.core import examples\n\nprint('\\033[1m average_filter_plugin')\nfile_list = [\"__init__.py\", \"operators.py\", \"operators_loader.py\", \"common.py\"]\nplugin_folder = None\nGITHUB_SOURCE_URL = \"https://github.com/pyansys/pydpf-core/raw/examples/first_python_plugins/python-plugins/average_filter_plugin\"\n\nfor file in file_list:\n EXAMPLE_FILE = GITHUB_SOURCE_URL + \"/average_filter_plugin/\" + file\n operator_file_path = examples.downloads._retrieve_file(EXAMPLE_FILE, file, \"python-plugins/average_filter_plugin\")\n plugin_folder = os.path.dirname(operator_file_path)\n print(f'\\033[1m {file}:\\n \\033[0m')\n print('\\t\\t\\t'.join(('\\n' + str(IPython.display.Code(operator_file_path)).lstrip()).splitlines(True)))\n print(\"\\n\\n\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load Plugin\nOnce a python plugin is written as a package, it can be loaded with the function\n:py:func:`ansys.dpf.core.core.load_library` taking as first argument the path to the directory of the plugin,\nas second argument ``py_`` + any name identifying the plugin,\nand as last argument the function's name exposed in the __init__ file and used to record operators.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import os\nfrom ansys.dpf import core as dpf\nfrom ansys.dpf.core import examples\n\n\ntmp = dpf.make_tmp_dir_server()\ndpf.upload_files_in_folder(\n dpf.path_utilities.join(tmp, \"average_filter_plugin\"),\n plugin_folder\n)\ndpf.load_library(\n os.path.join(dpf.path_utilities.join(tmp, \"average_filter_plugin\")),\n \"py_average_filter\",\n \"load_operators\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once the Plugin loaded, Operators recorded in the plugin can be used with:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"new_operator = dpf.Operator(\"ids_with_data_lower_than_average\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To use this new Operator, a workflow computing the norm of the displacement\nis connected to the \"ids_with_data_lower_than_average\" Operator.\nMethods of the class ``ids_with_data_lower_than_average`` are dynamically added thanks to the Operator's\nspecification.\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. graphviz::\n\n digraph foo {\n graph [pad=\"0.5\", nodesep=\"0.3\", ranksep=\"0.3\"]\n node [shape=box, style=filled, fillcolor=\"#ffcc00\", margin=\"0\"];\n rankdir=LR;\n splines=line;\n ds [label=\"ds\", shape=box, style=filled, fillcolor=cadetblue2];\n ds -> displacement [style=dashed];\n displacement -> norm;\n norm -> ids_with_data_lower_than_average;\n }\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use the Custom Operator\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"ds = dpf.DataSources(dpf.upload_file_in_tmp_folder(examples.static_rst))\ndisplacement = dpf.operators.result.displacement(data_sources=ds)\nnorm = dpf.operators.math.norm(displacement)\nnew_operator.inputs.connect(norm)\n\n\nnew_scoping = new_operator.outputs.scoping()\nprint(\"scoping in was:\", norm.outputs.field().scoping)\nprint(\"----------------------------------------------\")\nprint(\"scoping out is:\", new_scoping)"
]
}
],
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading