{ "cells": [ { "cell_type": "markdown", "id": "f0f1ae63-b069-477d-9d76-0195f473f974", "metadata": {}, "source": [ "# Using ParamDict" ] }, { "cell_type": "code", "execution_count": 1, "id": "ead25c96-98df-429f-9262-6f377f365fa5", "metadata": {}, "outputs": [], "source": [ "import pymecht as pmt\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "15cac4a3-e08c-4c44-90e3-25880d333f24", "metadata": {}, "source": [ "In pyMechT, the parameters are stored in a custom dictionary called `ParamDict` with keys being the names of the parameters and values of a custom data type `Param` which stores the current value, lower bound, upper bound, and fixed/non-fixed flag. For example, after we have created a sample, we can get its parameters and print them." ] }, { "cell_type": "code", "execution_count": 2, "id": "ab133d95-7e7a-406f-a57d-c95f1808576d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------\n", "Keys Value Fixed? Lower bound Upper bound \n", "------------------------------------------------------------------\n", "L0 1.00 No 1.00e-04 1.00e+03 \n", "A0 1.00 No 1.00e-04 1.00e+03 \n", "mu_0 1.00 No 1.00e-04 1.00e+02 \n", "------------------------------------------------------------------\n", "\n" ] } ], "source": [ "mat = pmt.MatModel('nh')\n", "sample = pmt.UniaxialExtension(mat)\n", "params = sample.parameters\n", "print(params)" ] }, { "cell_type": "markdown", "id": "a5d76af5-13d1-4f5f-a6cb-2d35949b1535", "metadata": {}, "source": [ "## Setting/fixing parameters" ] }, { "cell_type": "markdown", "id": "4cbfd7a7-7ca5-4fa5-a664-c401bced185e", "metadata": {}, "source": [ "The thinking behind this custom dictionary is to make it easy to handle large number of parameters in the various parameter-related studies (such as fitting and inference). There are several methods that allow controlling/changing the values of these. These are demonstrated below." ] }, { "cell_type": "code", "execution_count": 3, "id": "933a0126-3696-4209-8c64-bb09de58031c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------\n", "Keys Value Fixed? Lower bound Upper bound \n", "------------------------------------------------------------------\n", "L0 2.00 No 1.00e-04 1.00e+03 \n", "A0 5.00 Yes - - \n", "mu_0 1.00 No 1.00e-04 1.00e+02 \n", "------------------------------------------------------------------\n", "\n" ] } ], "source": [ "params.set('L0',2) #set the value of L0 to 2, but keep in non-fixed\n", "params.fix('A0',5) #fix the value of A0 to 5\n", "print(params)" ] }, { "cell_type": "code", "execution_count": 4, "id": "f95d1fa7-d805-480c-a0f9-09a77400d620", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------\n", "Keys Value Fixed? Lower bound Upper bound \n", "------------------------------------------------------------------\n", "L0 2.00 No 1.00e-04 1.00e+03 \n", "A0 5.00 Yes - - \n", "mu_0 1.00 No 1.00 1.00e+03 \n", "------------------------------------------------------------------\n", "\n" ] } ], "source": [ "params.set_lb('mu_0',1) #set the lower bound of mu_0\n", "params.set_ub('mu_0',1000) #set the upper bound of mu_0\n", "print(params)" ] }, { "cell_type": "markdown", "id": "fe87c74a-e888-49fc-a92a-f44eb0e5a4fb", "metadata": {}, "source": [ "## Converting into dataframe or csv" ] }, { "cell_type": "markdown", "id": "ab07d13c-50e3-4350-8be3-0a6020edbfe1", "metadata": {}, "source": [ "The above can get cumbersome for large number of parameters. Therefore, there are methods to convert the dictionary into a pandas dataframe or save it to a csv (comma separated value) file which can be modified in a text editor or Excel." ] }, { "cell_type": "code", "execution_count": 5, "id": "9e45b687-571e-407b-9b76-bbcf8bd725b6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Initial value Lower bound Upper bound Fixed\n", "L0 2.0 0.0001 1000.0 False\n", "A0 5.0 5.0000 5.0 True\n", "mu_0 1.0 1.0000 1000.0 False\n" ] } ], "source": [ "params_df = params.to_pandas() #convert to a pandas data frame\n", "print(params_df)" ] }, { "cell_type": "code", "execution_count": 6, "id": "15c6362b-e3b1-490e-a6a6-f57af22f743f", "metadata": {}, "outputs": [], "source": [ "params.save('params.csv') #save to a csv file" ] }, { "cell_type": "markdown", "id": "202dee33-2742-4124-bdfa-227614f44617", "metadata": {}, "source": [ "We can also read a saved csv file into a new parameter dictionary (probably after modifying it)" ] }, { "cell_type": "code", "execution_count": 7, "id": "46b08f29-24e0-44cc-9dc1-ff83058b6db5", "metadata": {}, "outputs": [], "source": [ "params.read('params.csv')" ] }, { "cell_type": "markdown", "id": "98e0bf04-b672-4021-98ef-fee5bd463afa", "metadata": {}, "source": [ "## Param data type and common error" ] }, { "cell_type": "markdown", "id": "55ea85e2-3a1e-404f-a42f-c2ae323102cf", "metadata": {}, "source": [ "We can also see one value of the dictionary, for example" ] }, { "cell_type": "code", "execution_count": 8, "id": "d48a34cb-f261-4c7a-b7f3-49b58fd51606", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.00 No 1.00 1.00e+03 \n" ] }, { "data": { "text/plain": [ "pymecht.ParamDict.Param" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(params['mu_0'])\n", "type(params['mu_0'])" ] }, { "cell_type": "markdown", "id": "292710f2-6c31-4305-aca0-33c57e11d153", "metadata": {}, "source": [ "If we try to use it in an incorrect way, we will get an error. This is to avoid corrupting the dictionary where some values are of type `Param` while others are of type `float`." ] }, { "cell_type": "code", "execution_count": 9, "id": "4542f75f-205b-4d6f-8232-77728d0aff71", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Assertion error occured: \n" ] } ], "source": [ "try:\n", " params['mu_0']=1.\n", "except AssertionError as e:\n", " print(\"Assertion error occured:\", e)" ] }, { "cell_type": "markdown", "id": "e711bf88-e155-40d6-a53c-8ad1484de75d", "metadata": {}, "source": [ "Instead, one can set or fix the value using its methods, for example" ] }, { "cell_type": "code", "execution_count": 10, "id": "87634847-127a-44c7-80d6-db17b33f65d3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------\n", "Keys Value Fixed? Lower bound Upper bound \n", "------------------------------------------------------------------\n", "L0 2.00 Yes - - \n", "A0 5.00 Yes - - \n", "mu_0 1.00 No 1.00 1.00e+03 \n", "------------------------------------------------------------------\n", "\n", "------------------------------------------------------------------\n", "Keys Value Fixed? Lower bound Upper bound \n", "------------------------------------------------------------------\n", "L0 2.00 Yes - - \n", "A0 5.00 Yes - - \n", "mu_0 10.00 No 1.00 1.00e+03 \n", "------------------------------------------------------------------\n", "\n" ] } ], "source": [ "params['L0'].fix()\n", "print(params)\n", "params['mu_0'].set(10)\n", "print(params)" ] }, { "cell_type": "markdown", "id": "db3f37cf-c93a-4113-bd8f-fdc7e6d30278", "metadata": {}, "source": [ "## Adding additional parameters" ] }, { "cell_type": "markdown", "id": "7445ee74-b978-40ba-97c7-18707b4d6700", "metadata": {}, "source": [ "Lastly, one might have additional parameters that they want to vary, such as fiber direction which are not part of the default list material/sample. This can be added as follows." ] }, { "cell_type": "code", "execution_count": 11, "id": "e240c0be-299e-4284-a439-00f309a21d7c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "------------------------------------------------------------------\n", "Keys Value Fixed? Lower bound Upper bound \n", "------------------------------------------------------------------\n", "L0 2.00 Yes - - \n", "A0 5.00 Yes - - \n", "mu_0 10.00 No 1.00 1.00e+03 \n", "phi 30.00 No 0.00 90.00 \n", "------------------------------------------------------------------\n", "\n" ] } ], "source": [ "params['phi'] = pmt.Param(30,0,90) #set the current value to 30, lower bound to 0, and upper bound to 90 degrees\n", "print(params)" ] } ], "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.9.16" } }, "nbformat": 4, "nbformat_minor": 5 }