Using ParamDict
[1]:
import pymecht as pmt
import numpy as np
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.
[2]:
mat = pmt.MatModel('nh')
sample = pmt.UniaxialExtension(mat)
params = sample.parameters
print(params)
------------------------------------------------------------------
Keys Value Fixed? Lower bound Upper bound
------------------------------------------------------------------
L0 1.00 No 1.00e-04 1.00e+03
A0 1.00 No 1.00e-04 1.00e+03
mu_0 1.00 No 1.00e-04 1.00e+02
------------------------------------------------------------------
Setting/fixing parameters
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.
[3]:
params.set('L0',2) #set the value of L0 to 2, but keep in non-fixed
params.fix('A0',5) #fix the value of A0 to 5
print(params)
------------------------------------------------------------------
Keys Value Fixed? Lower bound Upper bound
------------------------------------------------------------------
L0 2.00 No 1.00e-04 1.00e+03
A0 5.00 Yes - -
mu_0 1.00 No 1.00e-04 1.00e+02
------------------------------------------------------------------
[4]:
params.set_lb('mu_0',1) #set the lower bound of mu_0
params.set_ub('mu_0',1000) #set the upper bound of mu_0
print(params)
------------------------------------------------------------------
Keys Value Fixed? Lower bound Upper bound
------------------------------------------------------------------
L0 2.00 No 1.00e-04 1.00e+03
A0 5.00 Yes - -
mu_0 1.00 No 1.00 1.00e+03
------------------------------------------------------------------
Converting into dataframe or csv
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.
[5]:
params_df = params.to_pandas() #convert to a pandas data frame
print(params_df)
Initial value Lower bound Upper bound Fixed
L0 2.0 0.0001 1000.0 False
A0 5.0 5.0000 5.0 True
mu_0 1.0 1.0000 1000.0 False
[6]:
params.save('params.csv') #save to a csv file
We can also read a saved csv file into a new parameter dictionary (probably after modifying it)
[7]:
params.read('params.csv')
Param data type and common error
We can also see one value of the dictionary, for example
[8]:
print(params['mu_0'])
type(params['mu_0'])
1.00 No 1.00 1.00e+03
[8]:
pymecht.ParamDict.Param
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.
[9]:
try:
params['mu_0']=1.
except AssertionError as e:
print("Assertion error occured:", e)
Assertion error occured:
Instead, one can set or fix the value using its methods, for example
[10]:
params['L0'].fix()
print(params)
params['mu_0'].set(10)
print(params)
------------------------------------------------------------------
Keys Value Fixed? Lower bound Upper bound
------------------------------------------------------------------
L0 2.00 Yes - -
A0 5.00 Yes - -
mu_0 1.00 No 1.00 1.00e+03
------------------------------------------------------------------
------------------------------------------------------------------
Keys Value Fixed? Lower bound Upper bound
------------------------------------------------------------------
L0 2.00 Yes - -
A0 5.00 Yes - -
mu_0 10.00 No 1.00 1.00e+03
------------------------------------------------------------------
Adding additional parameters
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.
[11]:
params['phi'] = pmt.Param(30,0,90) #set the current value to 30, lower bound to 0, and upper bound to 90 degrees
print(params)
------------------------------------------------------------------
Keys Value Fixed? Lower bound Upper bound
------------------------------------------------------------------
L0 2.00 Yes - -
A0 5.00 Yes - -
mu_0 10.00 No 1.00 1.00e+03
phi 30.00 No 0.00 90.00
------------------------------------------------------------------