Skip to content

Calculations in Forms and Result Grids

A Calculation field computes its value from other fields with an Excel-style formula. It works the same way in every kind of form section:

SectionColumnsValues are saved as
FormOne set of fieldsForm entries
Grid formOne column per subject, e.g. per SampleForm entries per subject
Result grid (+ Section > Add Results > Result grid)One column per subject, e.g. per SampleResult values per subject

A result grid is a grid form whose values, calculated ones included, are saved as result values of the subjects. Everything on this page about grid forms also applies to result grids, including the fm_ variables. Variables are shared across all of them, so a calculation in a result grid can use a field from a form, and the other way round. This page covers the formula language, date and time maths, the helpers for grid forms, the Scifeon functions for entity fields and result values, and the extra variables the fermentation app adds when several fermentations or cultivations share one experiment.

The examples come from fed-batch and shake flask procedures. All names and values are made up.

Every field that should take part in a calculation needs a Variable, e.g. od or vol_start. The formula refers to the field by that variable. See Naming rules for what a variable may look like.

These field types can be used as variables:

Field typeValue in a formula
Number, ConstantThe number
CalculationThe calculated value, so calculations can build on each other
Date, Time, Date & timeA date serial number, see Date and time
Text, Select, Radio, CheckboxThe text
Select EntityThe ID of the selected entity, e.g. for ENTITYVALUE

Variables are shared by the whole experiment. A formula can use a variable from another form section in the same step or from a form in another step. That is why a variable must be unique in the experiment.

Add a field with the editor Calculation (formula) and enter the formula in Calculation formula (1). Press Ctrl + Space in the formula box to get a list of the variables you can use.

The settings of a Calculation field, with the formula AVERAGEA(d_1, d_2, d_3) in the Calculation formula box

Data type sets what the result is:

  • Number (default). Decimal Places controls the rounding.
  • Text, e.g. for a pass/fail message.
  • Date, Time or Date & time. The result is shown as a date or time. A negative result gives the error results with output type date and/or time cannot be negative.

The value is calculated in the experiment, as soon as a field it depends on changes, and saved with the form. Calculations do not run in a procedure template, because there are no values there. Hover over a calculated value to see the values of the variables it was calculated from.

Formulas use the same syntax and functions as Excel, e.g. IF, AND, ROUND, AVERAGE, LN, EXP, TODAY and NOW. The calculation engine is HyperFormula. The full list of functions .

// The average of the fields a, b and c:
AVERAGE(a, b, c)
// "High" or "Low", with Data type "Text":
IF(od > 10, "High", "Low")

Scifeon adds these functions:

FunctionReturnsNeeds
ENTITYVALUE(Entity, "Field")A field of a selected entity or of a grid column’s subject-
RVNUMBER(Subject, "Type", "Unit"), RVTEXT(...)A result value of a subject-
FM_SUM_SAMPLES(RunID, "SampleType", "FieldName")The sum of a form field over the samples of a runFermentation app

Units are not converted. A formula uses the number as it was entered, whatever unit the field has. If a volume is in mL and a concentration is in g/L, divide by 1000 in the formula yourself, and give the Calculation field the unit of the result.

  • A calculation that uses a field without a value shows a warning that the variable is missing a value, and has no result until the field is filled in.
  • A field marked N/A counts as empty, e.g. as 0 in a sum. The calculation shows the warning Some variables were marked N/A.
  • A variable that does not exist gives an error that asks you to make sure the variable is defined.

Dates and times are numbers of days, as in Excel. A Date & time value is the date plus the time as a fraction of a day, so 12:00 is 0.5. That makes the difference between two date and time values a number of days.

// Hours between "Start" (t_start) and "End" (t_end), both Date & time fields:
(t_end - t_start) * 24
// Minutes between them:
(t_end - t_start) * 1440
// Days since "Thaw date" (thaw), counted from today:
TODAY() - thaw
// Planned harvest 14 days after "Inoculation" (dti_inoc), with Data type "Date & time":
dti_inoc + 14
// Hours between two Time fields when the end may be after midnight:
MOD(t_end - t_start, 1) * 24
// A readable duration, with Data type "Text":
INT(t_end - t_start) & " days, " & HOUR(t_end - t_start) & " hours"

A grid form has one column per subject, e.g. per Sample or per Cultivation, and the same fields in every column. See Multiple Columns for how to set one up. A result grid is a grid form too, and this section applies to it in the same way. A formula in a grid is calculated once per column, with the values of that column.

The columns follow the order of the Subjects list in the form settings. Sort that list, e.g. by the time the samples were taken, and the grid keeps that order. All the column helpers below depend on it.

Quick filters above the grid only hide columns. Calculations still see every column, so a hidden column is still the “previous” column of the next one.

WriteGivesExample
_indexThe column number, starting at 0IF(_index=0, 1000, vol_prev - feed)
_idThe ID of the column’s subjectENTITYVALUE(_id, "name")
x_prevx in the previous columnod - od_prev
x_nextx in the next columnseed_vol_next * 0.35 / seed_od
x_firstx in the first columnt - t_first
x_c2x in a given column, here the thirdod / od_c0

In the first column x_prev has no value, and in the last column x_next has none. They count as empty, and the field shows the warning Value was calculated without _prev variables. Use _index to choose another value in the first column:

// Volume after each day: 1000 mL at the start, then the previous volume minus the feed:
IF(_index=0, 1000, vol_prev - feed)

The value is right, but the first column still shows the warning, because the formula refers to vol_prev.

  • A normal form variable in a grid formula has the same value in every column, e.g. od / od_target with od_target from a normal form.
  • A grid variable in a normal form needs a column: x_c0, x_c3 or x_first. Plain x gives an error, because there is one x per column.
  • A variable from another grid form is taken from the column with the same number, not from the same subject. Column 3 of one grid reads column 3 of the other. This only works if both grids list the same subjects in the same order. For samples of fermentations and cultivations, use the fm_ variables, which match by sample and run.

_prev, _first and _index work on the columns of the grid. That is what you want when the grid holds the samples of one fermentation or cultivation. When the grid holds the samples of several runs, they give the wrong values.

Take a grid with the samples of two bioreactors, sorted by bioreactor:

Column012345
SampleBR1 day 0BR1 day 1BR1 day 2BR2 day 0BR2 day 1BR2 day 2
_index012345
od_prev-BR1 day 0BR1 day 1BR1 day 2BR2 day 0BR2 day 1
od_firstBR1 day 0BR1 day 0BR1 day 0BR1 day 0BR1 day 0BR1 day 0

For BR2 day 0, od_prev is the last sample of BR1, and _index is 3, not 0. od_first is the first sample of BR1 for every column. Sorted by time taken, the samples of the bioreactors alternate, and od_prev jumps from one bioreactor to the other in every column.

So IF(_index=0, 0, (t - t_prev) * 24) gives the right sampling interval for one fermentation, and a wrong one as soon as a second fermentation is added to the same grid. A template that was built for one run must be changed before it is used for several.

The fermentation app solves this with the fm_ variables below. They work per run, whatever the column order.

The fm_ variables are added to a grid form or a result grid when at least one column’s subject is a Sample that belongs to a Fermentation or a Cultivation. A sample belongs to a run through its Fermentation or Cultivation field. A derived sample, e.g. a supernatant, belongs to the run of its root sample.

For each sample, the samples of the same run are put in a group, in the column order of the grid. Only the samples in this grid form count. A sample whose run cannot be found gets no fm_ variables.

Ctrl + Space lists the fm_ variables when the grid’s Entity Class is Sample.

VariableGives
fm_indexThe position of the sample among the samples of its run in this grid, starting at 0
fm_dtiInoculationThe inoculation date and time of the sample’s run
fm_<var>The field <var> of the sample’s run, from a grid form with Entity Class Fermentation or Cultivation
fm_prev_<var>The field <var> of the previous sample of the same run
fm_first_<var>The field <var> of the first sample of the same run

<var> in fm_prev_<var> and fm_first_<var> can be any variable in a grid form with Entity Class Sample in the experiment, including the form itself and Calculation fields.

For the grid above, fm_index is 0, 1, 2, 0, 1, 2, and for BR2 day 0 fm_prev_od has no value and fm_first_od is the OD of BR2 day 0.

For the first sample of each run, fm_prev_<var> has no value. It counts as empty, like a field marked N/A, and the field shows the warning Some variables were marked N/A, also when the formula does not use the value there.

Values that belong to the run, such as the start volume, the feed concentration or the limits of a run, go in a grid form with one column per run:

  1. Add a form to a step that has the Fermentations or Cultivations as input or output.
  2. In the form settings, enable the grid, set Entity Class to Fermentation or Cultivation, and pick the runs as Subject Sources.
  3. Add fields with variables, e.g. V0 for the start volume.

In the sample grid, fm_V0 is then the start volume of each sample’s own run.

The examples use these fields:

FormFieldVariableField type
Run grid (Entity Class Cultivation)Start volume (mL)V0Number
Run gridpH low limitph_loNumber
Run gridpH high limitph_hiNumber
Sample grid (Entity Class Sample)Time takenTtDate & time
Sample gridAdded since last sample (mL)TaNumber
Sample gridSample volume (mL)sNumber
Sample gridOD600odNumber
Sample gridTiter (g/L)titerNumber
Sample gridpHphNumber

Each grid can be a grid form or a result grid. Use a result grid for the sample grid when the measured and calculated values should be saved as results of the samples.

Culture volume (vol, Number, mL). The first sample of each run starts from the run’s start volume, the next ones from the previous sample’s volume:

IF(fm_index=0, fm_V0, fm_prev_vol) + Ta - s

fm_prev_vol refers to the field’s own variable in the previous sample, so the volume carries over from sample to sample within each run.

Culture age in hours (age, Number, h):

(Tt - fm_dtiInoculation) * 24

Specific growth rate and doubling time (mu, Number, 1/h, and td, Number, h):

// Growth rate since the previous sample of the same run:
LN(od / fm_prev_od) / (age - fm_prev_age)
// Doubling time:
LN(2) / mu
// Doubling time over the whole run, from the first sample:
(age - fm_first_age) * LN(2) / LN(od / fm_first_od)

The first sample of each run has no previous sample, so mu divides by zero there and shows an error. The whole-run doubling time divides by zero in the first sample too.

Volumetric productivity since the previous sample (prod, Number, g/L/h):

(titer - fm_prev_titer) / (age - fm_prev_age)

Total added since the first sample (Number, mL):

IF(fm_index=0, 0, Ta + fm_prev_tot_added)

Give the field the variable tot_added, so it can refer to its own previous value.

In range? (Data type Text), with limits that can differ per run:

IF(AND(ph >= fm_ph_lo, ph <= fm_ph_hi), "OK", "Out of range")

Culture age from the sample’s own time taken, instead of a form field, with ENTITYVALUE:

(ENTITYVALUE(_id, "dtiTaken") - fm_dtiInoculation) * 24

Using fields of a selected entity (ENTITYVALUE)

Section titled “Using fields of a selected entity (ENTITYVALUE)”

ENTITYVALUE(Entity, "Field") returns a field of the entity selected in a Select Entity field, so a calculation can use e.g. the purity of the Batch a user picked.

  • Entity is the Variable of a Select Entity field. In a grid form, _id gives each column’s subject.
  • Field is the field’s accessor, e.g. "purityValue", or its name, e.g. "Purity value", in quotes. Upper and lower case do not matter. The accessor is shown when the field is created under Fields, and it stays the same if the field is renamed, so it is the safer choice.
// The name of the Batch selected in the Select Entity field with the variable batch:
ENTITYVALUE(batch, "name")
// Grams of the Batch needed for 10 g of pure product, from its custom field "Purity value" (in %):
10 / (ENTITYVALUE(batch, "purityValue") / 100)
// Days until the Batch's custom field "Expiry date":
ENTITYVALUE(batch, "Expiry date") - TODAY()
// In a grid form: the name of each column's Sample
ENTITYVALUE(_id, "name")

Number fields give numbers and date fields give dates you can calculate with. Other fields give text.

Metadata or current value. If the field is shown as Entity Metadata on the Select Entity field, ENTITYVALUE uses the stored metadata, i.e. the value shown in the form. When the entity has changed, the user clicks View information changes on the field and accepts, and the calculation updates at once. Every other field gives the entity’s current value. If such a value changes after the form was filled in, the experiment shows There are outdated calculations, and Apply Calculations updates the calculated fields, as for result values.

So, if a calculation must keep the value the user saw when filling in the form, show the field as Entity Metadata. If it should follow the entity, do not.

Limits and errors:

  • ENTITYVALUE works in form calculations, not in the Spreadsheet.
  • The Entity must be a variable. A typed-in ID, e.g. ENTITYVALUE("BAT001", "name"), gives #VALUE.
  • Until an entity is selected, the calculation shows an error, as for any other empty variable. A Select Entity field that allows several entities has no single entity to read, and always shows this error.
  • A field that does not exist on the entity class gives #VALUE naming the field.
  • A field without a value gives #N/A.

RVNUMBER(Subject, "Type", "Unit") returns the number of a result value, and RVTEXT(Subject, "Type", "Unit") returns its text. Both also work in the Spreadsheet.

  • Subject is the ID of the entity the result belongs to, e.g. a Sample: the Variable of a Select Entity field, _id in a grid form, or an ID in quotes, e.g. "S00123".
  • Type is the result type, e.g. "Mass". Upper and lower case do not matter, and the unit may be part of it, e.g. "Mass [mg]".
  • Unit is optional. Give it when the same result type is stored in more than one unit; otherwise the first value found is used.
// The mass in mg of the Sample selected in the Select Entity field with the variable sample:
RVNUMBER(sample, "Mass", "mg")
// In a grid form: the color recorded for each column's Sample
RVTEXT(_id, "Color")

The functions find the result values uploaded in this experiment, and every result value of the entities used as inputs or outputs of its steps, including results from other experiments. A result that is not found, or has no value, gives #N/A.

When results in the experiment are saved or uploaded, also by a colleague, form calculations that use them show There are outdated calculations, and Apply Calculations updates them. In a Spreadsheet, the cells recalculate.

Sum over the samples of a run (FM_SUM_SAMPLES)

Section titled “Sum over the samples of a run (FM_SUM_SAMPLES)”

FM_SUM_SAMPLES(RunID, "SampleType", "FieldName") adds up a number field over the samples of one run, e.g. the total volume taken out for sampling.

  • RunID is the ID of the Fermentation or Cultivation. In a run grid, use _id.
  • SampleType is the sample’s Type, e.g. "Broth". Upper and lower case do not matter.
  • FieldName is the field’s Name, not its variable, written exactly as in the form.
// In the run grid: the volume taken out of each run by the Broth samples
FM_SUM_SAMPLES(_id, "Broth", "Sample volume")
// The same for one run, by its ID:
FM_SUM_SAMPLES("CUL0012", "Broth", "Sample volume")
  • It counts the samples that are subjects of a form in this experiment, and their saved values. After a save, the sum updates.
  • Empty and N/A values are skipped. A value that is not a number gives #VALUE.
  • No matching samples gives 0.
  • It also works in the Spreadsheet.
  • fm_ variables and FM_SUM_SAMPLES need the fermentation app, and samples that belong to a Fermentation or a Cultivation.
  • fm_ variables only exist in grid forms and result grids whose columns are samples. They are not available in normal forms or in the Spreadsheet. In a Spreadsheet, read form values with FORMV.
  • fm_prev_ and fm_first_ only look at the samples of the same run in the same grid form, in column order. A sample of the run in another step’s grid does not count.
  • fm_dtiInoculation and the list of runs are read from the server and kept for up to a minute. A change to a run’s inoculation time can take that long to show up in the form.
  • _prev, _next, _first, _index and variables from other grid forms work by column number, not by subject.
  • Quick filters do not change calculations.
  • Units are not converted.
  • Calculations only run in experiments, not in procedure templates.