{ "cells": [ { "cell_type": "markdown", "id": "81769017-a1f8-4b47-9cf8-5df2a5fa25a4", "metadata": {}, "source": [ "# ... add an affix-function to a standard name table during runtime?\n", "If you are using the [standard name convention](../userguide/convention/index.rst), you are familiar with transformation functions like \"derivative_of_\\_wrt_\\\" or \"square_of_\\\".\n", "\n", "Now, you want to add your custom transformation function without changing the package code. Say, you want to add the transformation \"maximum_of_\", referring to the maximum value of your data.\n", "\n", "Here is the current state, which fails as expected:" ] }, { "cell_type": "code", "execution_count": 1, "id": "791af21d-0c99-4d0b-bb0b-b5f59ecb1c8c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"maximum_of_pressure\" not found in Standard Name Table \"Test\".\n" ] } ], "source": [ "import h5rdmtoolbox as h5tbx\n", "from h5rdmtoolbox.convention.standard_names.transformation import Transformation\n", "from h5rdmtoolbox.convention.standard_names import StandardName\n", "\n", "snt = h5tbx.convention.standard_names.StandardNameTable.from_zenodo(doi_or_recid=10428795)\n", "\n", "# check if the problem really exists:\n", "try:\n", " snt['maximum_of_pressure']\n", "except h5tbx.errors.StandardNameError as e:\n", " print(e)" ] }, { "cell_type": "markdown", "id": "04b2c358-783c-4e6b-8d56-a1ffee7fdf70", "metadata": {}, "source": [ "## Implementing a new transformation function\n", "\n", "We need to do two things:\n", " 1. write a `function` which takes a regex result and generates the new standard name\n", " 2. init a `Transformation` object which performs the regex match\n", "\n", "The respective regex pattern to the above is `^maximum_of_(.*)$`.\n", "\n", "The respective function is implemented in the following. It takes the result from a `re.match()` call and the standard name table object. The function then constructs a new standard name and returns it. Even if the regex match succeeded, it is still possible, that the function raises an error because the base standard_name may not exist." ] }, { "cell_type": "code", "execution_count": 2, "id": "dc0db89c-c3b9-4c41-87dc-6a5ae91fc356", "metadata": {}, "outputs": [], "source": [ "def maximum_of(match, snt):\n", " # match is the result of `re.match(`^maximum_of_(.*)$, )`\n", " groups = match.groups()\n", " assert len(groups) == 1\n", " sn = snt[groups[0]]\n", " new_description = f\"Maximum of {sn.name}. {sn.description}\"\n", " return StandardName(match.string, sn.units, new_description)" ] }, { "cell_type": "markdown", "id": "b634d764-aeb9-4dde-bb00-7cf59941c15f", "metadata": {}, "source": [ "The actual transformation is managed by the class `Transformation`. It takes the above defined function and the respective regex pattern:" ] }, { "cell_type": "code", "execution_count": 3, "id": "e10d2b65-b6ea-42ce-909d-a375a0ba71ed", "metadata": {}, "outputs": [], "source": [ "max_of = Transformation(r\"^maximum_of_(.*)$\", maximum_of)" ] }, { "cell_type": "markdown", "id": "70de8f86-6a83-4b70-9fa6-b1f20fecaa98", "metadata": {}, "source": [ "We can already check if the pattern matching works:" ] }, { "cell_type": "code", "execution_count": 4, "id": "9f9d5fa2-9dfa-46ff-bc9b-8b9db8f5870a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max_of.match('maximum_static_pressure') is None" ] }, { "cell_type": "code", "execution_count": 5, "id": "f54490f6-0272-4483-86f2-71fba12207c0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max_of.match('maximum_of_static_pressure') is None" ] }, { "cell_type": "markdown", "id": "949bac12-f381-43a2-adfb-44848dafb559", "metadata": {}, "source": [ "## Add it to an existing standard name table during runtime:" ] }, { "cell_type": "code", "execution_count": 6, "id": "a5ae8c3a-3115-400f-bd02-3d75578c707a", "metadata": {}, "outputs": [], "source": [ "snt.add_transformation(max_of)" ] }, { "cell_type": "code", "execution_count": 7, "id": "835d7ccb-afca-429a-a5f1-38b8a18e4230", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "
  • \n", " \n", " \n", "
      \n", "
    • units : Pa
    • description : Maximum of static_pressure. Static pressure refers to the force per unit area exerted by a fluid. Pressure is a scalar quantity.
    • \n", "
    \n", "
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "snt['maximum_of_static_pressure']" ] }, { "cell_type": "code", "execution_count": 8, "id": "ccc894ff-2748-4e1b-9677-5b37d0b7d792", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "snt.transformations[-1] == max_of" ] }, { "cell_type": "code", "execution_count": null, "id": "18d18150-ab33-480f-9d9e-0c02a26e9aa9", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.18" } }, "nbformat": 4, "nbformat_minor": 5 }