Calc: Difference between revisions

From SweepMe! Wiki
Jump to navigation Jump to search
(→‎Execution: Link to phases)
 
(15 intermediate revisions by 3 users not shown)
Line 14: Line 14:
# Select the SweepMe! internal variables and relate them to 'x1', 'x2', and so on.
# Select the SweepMe! internal variables and relate them to 'x1', 'x2', and so on.
# Type in your formula in section 'Formula' using 'x1', 'x2', etc. as placeholders for
# Type in your formula in section 'Formula' using 'x1', 'x2', etc. as placeholders for
Calc is performing its calculation when it is part of the active branch. It can process last known parameter values but has no access to the history of a parameter. The selected values can stem from modules that are present in the same branch, but also from modules in previously active branches.


== Execution ==
== Execution ==


Calc can calculate the actual value at different positions during the measurement procedure, e.g. before a new measurement point is set and measured or after all variables of a meaurement point have been taken.  
Calc can calculate new values at different positions during the measurement procedure, e.g. before a new measurement point is set and measured or after all variables of a meaurement point have been taken.
* '''Always:'''  Use this default value if you do not matter and just want to have actual values all the time.
 
* '''At the beginning:''' Needed if you like to modify a SweepValue of a certain module that has to be handed over to another Module as a SweepValue before the next measurement point is run through.
* '''Always:'''  Use this default value if you do not matter and just want to have actual values all the time. It is a combination of the other options.
* '''At the end:''' Can be used, if you like to post-process the latest measurement data.
* '''Before branch (in 'configure')''' calculates new values during the [[configure()]] phase of Calc and thus even before configure of child modules.
* '''Before each point (before 'start')''' calculates new values before the [[start()]] phase of all modules. The right order of modules in a branch are important. Arrange the modules in the order of how values must be processed.
* '''Before branch and each point (in 'configure' + before 'start')''' combines both of the above.<br>''In module versions before 2024-04-16 this option was called "At the beginning".''
* '''After each point (in 'process')''' calculates new values when all Modules have measured their values during the [[process()]] phase.<br>''In module versions before 2024-04-16 this option was called "At the end".''


== Further information ==
== Further information ==
Line 28: Line 34:
* Several Calc modules can be used and combined to create more complex calculations.
* Several Calc modules can be used and combined to create more complex calculations.
* Calc only provides the data of the last measurement point, which can be 0-dimensional, e.g. in case of voltage, a time etc. or 1-dimensional, e.g. in case of a spectrum or a transient of an oscilloscope.
* Calc only provides the data of the last measurement point, which can be 0-dimensional, e.g. in case of voltage, a time etc. or 1-dimensional, e.g. in case of a spectrum or a transient of an oscilloscope.
* If you need to access all points, that have been measured so far, the module 'CustomFunction' is recommended that allows you to write your own python scrips and load additional libraries.
* If you need to access all points, that have been measured so far, the module [[CustomFunction]] is recommended that allows you to write your own python scrips and load additional libraries.
 
== Examples ==
== Examples ==


* Current density in mA/cm²: '''1000 * x1 / 0.04''' where x1 is the current of an SMU module in Ampere.
* Current density in mA/cm²: '''1000 * x1 / x2''' where x1 is the current of an SMU module in Ampere and x2 the active area of the sample in cm².
* Normalize spectrum by max: '''x1/max(x1)''' where x1 is a spectrum of a Spectrometer module.
* Normalize spectrum by max: '''x1/max(x1)''' where x1 is a spectrum of a Spectrometer module.
* Calibrate temperatures: '''a * x1 + b''' where x1 is a measured temperature of the Temperature module and a,b are the coefficients of a linear calibration function.
* Calibrate temperatures: '''a * x1 + b''' where x1 is a measured temperature of the Temperature module and a,b are the coefficients of a linear calibration function.
Line 40: Line 46:
* Peak position: '''min(x2[x1==max(x1)])''' where x1 are 1-dimensional intensities and x2 are the corresponding 1-dimensional wavelengths. The function finds the first wavelength, the maximum value is achieved.
* Peak position: '''min(x2[x1==max(x1)])''' where x1 are 1-dimensional intensities and x2 are the corresponding 1-dimensional wavelengths. The function finds the first wavelength, the maximum value is achieved.
* String formatting: '''"%i" % x1''' where x1 is an integer. Please note that the result will be a string.
* String formatting: '''"%i" % x1''' where x1 is an integer. Please note that the result will be a string.
* Create a message: '''"The measurement has finished"''' where no variable is needed. Such messages can be used to display them during a measuerement.
* Create a message: '''"The measurement has finished"''' where no variable is needed. Such messages can be used to display them during a measurement in a [[Monitor]] widget.
* Print to debug (from version: 1.5.4.): '''print("My value is" , x1)''' where x1 can be any selected value. The output will be seen in the Debug window (F2).
* Print to debug (from version: 1.5.4.): '''print("My value is" , x1)''' where x1 can be any selected value. The output will be seen in the Debug window (F2).


== Supported Functions ==
== Supported Functions ==


There is a limited number of functions that can be used inside the Calc module. The following list gives the string that can be used and the corresponding function that will be loaded. If there are further functions needed, please let us know (support@sweep-me.net).
There is a limited number of functions that can be used inside the Calc module. The following list gives the commands that can be used in Calc and the corresponding python function that will be called. If there are further functions needed, please let us know (support@sweep-me.net).


The list is related to version: 1.5.4
The list is related to version: 1.5.4


=== [https://docs.python.org/3/library/functions.html python built-in functions] ===
=== [https://docs.python.org/3/library/functions.html python built-in functions] ===
* 'abs' = abs
 
* 'len' = len
{| class="wikitable unsortable"
* 'max' = max
|-
* 'min' = min
! Calc command !! Python function
* 'int' = int
|-
* 'float' = float
| abs || abs
* 'round' = round
|-
* 'str' = str
| len || len
* 'repr' = repr
|-
* 'print' = print
| max || max
* 'type' = type
|-
| min || min
|-
| int || int
|-
| float || float
|-
| round || round
|-
| str || str
|-
| repr || repr
|-
| print || print
|-
| type || type
|}


=== [https://docs.python.org/3/library/datetime.html datetime functions] ===
=== [https://docs.python.org/3/library/datetime.html datetime functions] ===
* 'now' = datetime.datetime.now  
{| class="wikitable unsortable"
|-
! Calc command !! Python function
|-
| now || datetime.datetime.now  
|}


=== [https://docs.python.org/3/library/math.html math functions] ===
=== [https://docs.python.org/3/library/math.html math functions] ===
* 'ceil' = math.ceil
 
* 'degrees' = math.degrees
{| class="wikitable unsortable"
* 'fabs' = math.fabs
|-
* 'floor' = math.floor
! Calc command !! Python function
* 'fmod' = math.fmod
|-
* 'frexp' = math.frexp
| ceil || math.ceil
* 'hypot' = math.hypot
|-
* 'ldexp' = math.ldexp
| degrees || math.degrees
* 'modf' = math.modf
|-
* 'pi' = math.pi
| fabs || math.fabs
* 'pow' = math.pow
|-
* 'radians' = math.radians
| floor || math.floor
|-
| fmod || math.fmod
|-
| frexp || math.frexp
|-
| hypot || math.hypot
|-
| ldexp || math.ldexp
|-
| modf || math.modf
|-
| pi || math.pi
|-
| pow || math.pow
|-
| radians || math.radians
|}


=== [https://docs.python.org/3/library/random.html random functions] ===
=== [https://docs.python.org/3/library/random.html random functions] ===
* 'random' = random.random
 
* 'randint' = random.randint
{| class="wikitable unsortable"
|-
! Calc command !! Python function
|-
| random || random.random
|-
| randint || random.randint
|}


=== [https://docs.scipy.org/doc/numpy/reference/routines.html numpy functions] ===
=== [https://docs.scipy.org/doc/numpy/reference/routines.html numpy functions] ===
* 'sqrt' = np.sqrt
{| class="wikitable unsortable"
 
|-
* 'absolute' = np.absolute
! Calc command !! Python function
* 'real' = np.real
|-
* 'imag' = np.imag
| sqrt || np.sqrt
* 'angle' = np.angle
|-
* 'conj' = np.conj
| absolute || np.absolute
 
|-
* 'sin' = np.sin
| real || np.real
* 'sinh' = np.sinh
|-
* 'asin' = np.arcsin
| imag || np.imag
* 'arcsin' = np.arcsin
|-
 
| angle || np.angle
* 'cos' = np.cos
|-
* 'cosh' = np.cosh
| conj || np.conj
* 'acos' = np.arccos
|-
* 'arccos' = np.arccos
| sin || np.sin
 
|-
* 'tan' = np.tan
| sinh || np.sinh
* 'tanh' = np.tanh
|-
* 'arctan' = np.arctan
| asin || np.arcsin
* 'atan' = np.arctan
|-
* 'arctan2' = np.arctan2
| arcsin || np.arcsin
* 'atan2' = np.arctan2
|-
| cos || np.cos
|-
| cosh || np.cosh
|-
|acos || np.arccos
|-
| arccos || np.arccos
|-
| tan || np.tan
|-
| tanh || np.tanh
|-
| arctan || np.arctan
|-
| atan || np.arctan
|-
| arctan2 || np.arctan2
|-
| atan2 || np.arctan2
|-
| exp || np.exp
|-
| ln || np.log
|-
| log || np.log
|-
| log10 || np.log10
|-
| average || np.average
|-
| mean || np.mean
|-
| arange || np.arange
|-
| linspace || np.linspace
|-
| logspace || np.logspace
|-
| divide || np.divide
|-
| sum || np.sum
|-
| trapz || np.trapz
|-
| sign || np.sign
|-
| isnan || np.isnan


* 'exp' = np.exp
|}
* 'ln' = np.log
* 'log' = np.log
* 'log10' = np.log10
* 'average' = np.average
* 'mean' = np.mean
* 'arange' = np.arange
* 'linspace' = np.linspace
* 'logspace' = np.logspace
* 'divide' = np.divide
* 'sum' = np.sum
* 'trapz' = np.trapz
* 'sign' = np.sign
* 'isnan' = np.isnan  # modifications: returns False if object is no float, returns bool instead of np.bool_


Modifications of isnan: returns False if object is no float, returns bool instead of np.bool_


[[Category:Modules]]
[[Category:Modules]]
[[Category:Basic Modules]]
[[Category:Basic Modules]]

Latest revision as of 11:47, 17 April 2024

Calc is a simple calculator that helps you to do simple math based on the latest measurement values.

Screenshot of the Calc module. Based on the current of the module SMU1, the variable 'Current density' is calculated in units of 'mA/cm^2' assuming an active area of 0.04 cm^2.


Usage

The typical way to use Calc involves the following steps:

  1. Create a Calc module in the sequencer and change to the corresponding tab
  2. Change the label of the module if desired
  3. Enter a name of the calculated variable and its unit
  4. Define whether the variable can be plotted and whether it should be saved
  5. In section 'Values' create as many fields you need using 'Add value field'
  6. Select the SweepMe! internal variables and relate them to 'x1', 'x2', and so on.
  7. Type in your formula in section 'Formula' using 'x1', 'x2', etc. as placeholders for


Calc is performing its calculation when it is part of the active branch. It can process last known parameter values but has no access to the history of a parameter. The selected values can stem from modules that are present in the same branch, but also from modules in previously active branches.

Execution

Calc can calculate new values at different positions during the measurement procedure, e.g. before a new measurement point is set and measured or after all variables of a meaurement point have been taken.

  • Always: Use this default value if you do not matter and just want to have actual values all the time. It is a combination of the other options.
  • Before branch (in 'configure') calculates new values during the configure() phase of Calc and thus even before configure of child modules.
  • Before each point (before 'start') calculates new values before the start() phase of all modules. The right order of modules in a branch are important. Arrange the modules in the order of how values must be processed.
  • Before branch and each point (in 'configure' + before 'start') combines both of the above.
    In module versions before 2024-04-16 this option was called "At the beginning".
  • After each point (in 'process') calculates new values when all Modules have measured their values during the process() phase.
    In module versions before 2024-04-16 this option was called "At the end".

Further information

  • 'x0' is the last return value of the Calc module. It might be of interest to do some iterative recalculation and refinement. Please choose the correct execution, e.g. "At the beginning" or "At the end". Otherwise, the result will be modified twice per measurement point.
  • 'x0' is always initialized as float('nan') (i.e. not a number of type float). You can test whether is x0 is such a 'nan' using isnan(x0)
  • Several Calc modules can be used and combined to create more complex calculations.
  • Calc only provides the data of the last measurement point, which can be 0-dimensional, e.g. in case of voltage, a time etc. or 1-dimensional, e.g. in case of a spectrum or a transient of an oscilloscope.
  • If you need to access all points, that have been measured so far, the module CustomFunction is recommended that allows you to write your own python scrips and load additional libraries.

Examples

  • Current density in mA/cm²: 1000 * x1 / x2 where x1 is the current of an SMU module in Ampere and x2 the active area of the sample in cm².
  • Normalize spectrum by max: x1/max(x1) where x1 is a spectrum of a Spectrometer module.
  • Calibrate temperatures: a * x1 + b where x1 is a measured temperature of the Temperature module and a,b are the coefficients of a linear calibration function.
  • Sum up a value: x1 if isnan(x0) else x0 + x1 where x1 is the value to be summed up and x0 is the return value. Use Execution = "At the end". In the first step, y is set to x1, so that in subsequent steps x0 (which is the last y) is initialized to a start value.
  • Create a message: "Ok" if x1 < 50 else "Warning" where x1 is a process parameter that is checked whether it would be ok 50 or not.
  • Indexing: x1[62] where x1 is a variable with 1-dimensional data, e.g. a spectrum. Here, the value at index 62 is returned. Please note that indices start from 0.
  • Peak position: min(x2[x1==max(x1)]) where x1 are 1-dimensional intensities and x2 are the corresponding 1-dimensional wavelengths. The function finds the first wavelength, the maximum value is achieved.
  • String formatting: "%i" % x1 where x1 is an integer. Please note that the result will be a string.
  • Create a message: "The measurement has finished" where no variable is needed. Such messages can be used to display them during a measurement in a Monitor widget.
  • Print to debug (from version: 1.5.4.): print("My value is" , x1) where x1 can be any selected value. The output will be seen in the Debug window (F2).

Supported Functions

There is a limited number of functions that can be used inside the Calc module. The following list gives the commands that can be used in Calc and the corresponding python function that will be called. If there are further functions needed, please let us know (support@sweep-me.net).

The list is related to version: 1.5.4

python built-in functions

Calc command Python function
abs abs
len len
max max
min min
int int
float float
round round
str str
repr repr
print print
type type

datetime functions

Calc command Python function
now datetime.datetime.now

math functions

Calc command Python function
ceil math.ceil
degrees math.degrees
fabs math.fabs
floor math.floor
fmod math.fmod
frexp math.frexp
hypot math.hypot
ldexp math.ldexp
modf math.modf
pi math.pi
pow math.pow
radians math.radians

random functions

Calc command Python function
random random.random
randint random.randint

numpy functions

Calc command Python function
sqrt np.sqrt
absolute np.absolute
real np.real
imag np.imag
angle np.angle
conj np.conj
sin np.sin
sinh np.sinh
asin np.arcsin
arcsin np.arcsin
cos np.cos
cosh np.cosh
acos np.arccos
arccos np.arccos
tan np.tan
tanh np.tanh
arctan np.arctan
atan np.arctan
arctan2 np.arctan2
atan2 np.arctan2
exp np.exp
ln np.log
log np.log
log10 np.log10
average np.average
mean np.mean
arange np.arange
linspace np.linspace
logspace np.logspace
divide np.divide
sum np.sum
trapz np.trapz
sign np.sign
isnan np.isnan

Modifications of isnan: returns False if object is no float, returns bool instead of np.bool_