# Formulas 1 - Basic Shape Analysis

## Summary:

Formulas make it possible to extend ProSEM’s analysis capability to report what you need. Formulas use the JavaScript programming language, but in many cases, it is not necessary to learn much JavaScript syntax. For more complex needs, there is plentiful online documentation and lessons for JavaScript programming.

To demonstrate formula basics, here some characteristics of measured shapes are computed using user-defined formulas, such as Aspect Ratio of rectangles, Circular shape computations such as circularity or ellipticity, or grating metrics such as duty cycle or fill fraction.

Note: ProSEM 2.3.0 or higher is required for these examples.

#### Demonstrates:

- Basics of using formulas

#### Measurement Example 1: Aspect Ratio

In this first example, a formula is created to compute the Aspect Ratio of measured rectangles.

- A measurement of a rectangle is setup, stored, and then that stored measurement is selected in the tree in the Images panel.

- In the Variables panel, the “Create New Variable…” button is clicked. In the dialog:

- The variable type is set to: “Formula”
- The variable name is defined as: AspectRatio
- The formula is defined as: Height / Width

- Optional: the new formula can be tested using the “Evaluate” button. If the variable is successfully setup, the value displayed will be the Aspect Ratio of the selected measurement.
- The OK button is clicked to complete the creation of the new formula variable.

This formula will be evaluated for all measurements in the project, including measurements already stored and all measurements stored after the variable is created. The values of any defined variables for the currently selected measurement are shown in the table in the Variables panel.

In addition, each variable defined will appear as a new column in each data table, so in this case, the value of Aspect Ratio will be calculated for all rectangles measured in the project.

### Notes:

- It isn’t strictly necessary to select a measurement before creating a variable, but in order to test the formula using the Evaluate button, a measurement must have been selected before editing the variable, so that ProSEM knows which measurement to compute the formula for. A measurement must also be selected in order to use the context portion of the Context Browser, covered a few sections below.
- The formula is evaluated using JavaScript, and all values in JavaScript are case sensitive, so names such as Height and Width must be entered in the correct case, here with an initial capital letter.
- The Display Precision field specifies how many decimal places to display for this variable.
- To edit an existing variable, double-click on the variable entry in the table in the Variables panel, or right-click on the name and choose “Edit Variable”
- When an element is selected in the Images tree which does not have the necessary properties to compute the formula, "Error" is displayed instead of a value. For example, for this formula, for a Feature Type of "Lines & Spaces", the AspectRatio is displayed as "Error", because there is no Height or Width property defined for a Line measurement. It is possible to have other information displayed; that will be demonstrated in later example.

#### Measurement Example 2: Eccentricity, More Shape Computations, using the Object Browser and Snippet Gallery

In this example, the Eccentricity, also sometimes called Ellipticity, of ellipses and circles is computed, and the Object Browser and Snippet Gallery are introduced.

To compute the Eccentricity of measured Ellipses and Circles, the formula to be used is:

Where a is the semimajor axis, or half the length of the major axis, and b is semiminor axis.

But how does one know the property names within ProSEM for these measured values? The Object Browser is displayed on the right side of the Edit Variables dialog when a Formula is being edited, and contains all of the properties of the selected measurement, as well as the group and the image containing the selected measurement.

For this example, a multi-line formula will be used, to increase the readability of the formula. So first a and b will be defined as variables, and then the full formula will be computed using those variables.

For example, to compute the Eccentricity of a measured ellipse:

- An ellipse measurement is setup, stored, and then the stored measurement selected n the Images tree.

- In the Variables panel, a new variable is created. In the Create New Variable dialog:

- The variable type is set to: “Formula”
- The variable name is defined as: Eccentricity

To compute the eccentricity, the lengths of the semimajor and semiminor axes are needed, each half of their respective diameters. Within the formula, these will be stored as local variables named a and b. The length of the major and minor axes could be typed into the formula, or in this case, the property name is drag-and-dropped from the Context branch of the Object Browser. Each line is terminated by a semicolon character “;” keeping with JavaScript syntax requirement.

- The formula for a, the semimajor axis, is defined by typing in a =
- Then in the object browser, the Context branch is opened, and the property MajorDiameter is drag-and-dropped into the formula
- The formula is completed by typing in: / 2 ;
- The formula for b is then created in the same way, but using the MinorDiameter property

- Finally, the full formula is entered, using drag-and-drop from the Math branch of the Object Browser to find the JavaScript syntax for the square root and power functions. The function names sqrt and pow could also be typed, but the Math branch provides a convenient reference of common functions. Of course, the same result as pow(a,2) is also obtained by just: (a * a) Either works fine.) Again, the Evaluate button tests the formula on the selected measured shape.

The complete formula contents for the Eccentricity in this example are:

```
a = MajorDiameter / 2;
b = MinorDiameter / 2;
sqrt(1- (pow(b,2) / pow(a,2)))
```

This could also be written on a single line as:

`sqrt(1- (pow((MinorDiameter/2),2) / pow((MajorDiameter/2),2)))`

The formulas in this example demonstrate some basic elements of using Formulas in ProSEM. See other examples on this site for more advanced examples.