Changes

Using Python to Create Functions, Models & Scripts

384 bytes added, 03:50, 6 September 2016
/* Creating Custom Python Functions */
== Creating Custom Python Functions ==
Besides the standard mathematical functions and a large set of library functions, [[EM.Cube]] allows you to create custom Python functions of your own.If you are an avid Python programmer, you already know how to write Python function scripts and import the corresponding module using [[EM.Cube]]'s Python command line.  For those new to Python programming, [[EM.Cube]] offers a blank Python file, which you can open and modify as a starting point. [[EM.Cube]] provides a list of all the preloaded Python scripts along with your own "user-defined" Python functions in its "Models Dialog". Open [[EM.Cube]]'s Models Dialog by clicking the '''Models''' button [[File:Models_icon.png]] of the Simulate Toolbar or selecting the menu item '''Simulate → Models...''' or using the keyboard shortcut {{key|Ctrl+L}}. Locate the Python file called "blank.PY" in the list, select and highlight it and click the {{key|Open}} button of the dialog. The Python file is opened using the text editor you have associated with ".PY" files in Windows. For example, Notepad++ is a powerful and popular choice.  The easiest way of defining a Python function is using the "def" statement. The contents of the blank file are shown below: ----- import sysfrom math import *from emag import *import numpy as npimport scipy.special as sp def func_name(x): #Write your python code here   return x ----- It is recommended that you choose the same name for both your Python function and your Python file. First, save the opened blank file as a new Python file with your new name using Windows' "Save As..." operation. Then, replace "func_name" in the above script with your new name. By default, a single argument "x" is assumed. You can have any arbitrary number of arguments using a comma-separated list. Keep in mind that indentation is very important in Python. You can use either spaces or tabs to create indentation. But you cannot mix them together in the same script. Enter the body of your function in the space provided with the proper indentation. Make sure you return a value for your function.  As an example, the script below shows a new Python function called "MyFunc" save in the Python file "MyFunc.PY":  ----- import sysfrom math import *from emag import *import numpy as npimport scipy.special as sp def MyFunc(x): #Write your python code here return cos(x)-x ----- 
Follow the procedure below to define a new Python model in [[EM.Cube]]:
* Open [[EM.Cube]]'s Models Dialog by clicking the '''Models''' button [[File:Models_icon.png]] of the Simulate Toolbar or selecting '''Menu > Simulate > Models...''' or using the keyboard shortcut {{key|Ctrl+L}}.
* To add a new model, click the '''Add''' button to open the "Add Model" dialog. Choose one of the three options: Functional, Tabular or Python. Keep in mind that HDMR models are built differently through a special sweep process.
* In the Model Definition dialog, type in the list of the arguments of your model separated by commas, e.g. x,y,z, ...
* Choose a name for your new model. This same name will also be given later to a ".DAT" data file associated with your new model. Therefore, choose a unique name for your model that distinct from your project's existing data files.
* In the case of a functional model, enter the mathematical expression as a function of the arguments you defined earlier.
Once you close the model definition dialog, a new entry is added to the Models List. A functional model file with a '''.UDM''' file extension is created in your project's folder. It is recommended that you use the same name for functional models and their model files. The example in the figure below shows a two-variable function called Sinc2D with two arguments x and y. From this point on, you can use your new function Sinc2D(x,y) everywhere in [[EM.Cube]] as you would use any standard or library function. You can always edit a model using the {{key|Edit}} button of the Models dialog. For example, you can change the mathematical expression of a functional model or add new arguments to it.
{{Note|As soon as you define any new model, all of its arguments are added to [[EM.Cube]]'s Variables List as new variables that you can use elsewhere.}}
<table>
<tr>
28,333
edits