Calc
Calc is a simple calculator that helps you to do simple math based on the latest measurement values.
Usage
The typical way to use Calc involves the following steps:
- Create a Calc module in the sequencer and change to the corresponding tab
- Change the label of the module if desired
- Enter a name of the calculated variable and its unit
- Define whether the variable can be plotted and whether it should be saved
- In section 'Values' create as many fields you need using 'Add value field'
- 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
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 |
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_