Calculations are a powerful tool in SENAITE to define Analysis Services that take no manual input, but are calculated out of either user-defined parameters (Interim fields) or Results of other Analyses.
During this guide we created an Analysis Service for
However, we did not specify a calculation so far, but would like to be it a sum
of the Results from the
Navigate to the LIMS Setup by clicking the ⚙️ button in the upper right corner and then on the Calculations tile.
Enter the following values in the add form:
Calculate the total hardness of Water based on the Calcium and Magnesium Results
- Calculation Formula
[Ca] + [Mg]
☝️Note: You can use any valid Python Expression in the Calculation Formula
Click the Save button when you are done.
☝️Note: The calculation formula references the Keywords of the Analysis Services we defined in the Quickstart section earlier. Selecting a meaningful Analysis Service Keyword is therefore from absolute importance, since changing it later will break existing Calculations!
Calculations also understand a selected set of Python Builtins
Using the calculation in an Analysis Service
Next we need to hook the calculation to the Analysis Service. Navigate to the
LIMS Setup and chose the Analysis Services tile. Select the
service from the listing and click on the Edit tab.
Navigate to the Method tab and deselect the option Use the Default Calculation of Method. The Calculation selection should then be automatically filled with our new created calculation. Press the Save button when you are done.
Applying an Analysis with a Calculation to a Sample
As already mentioned in the Sample Analyses section, our
change in the Analysis Service for
Total Hardness does not affect any
existing Analyses in our Samples.
Consequently, we must either create a new Sample with the
Analysis, e.g. by copying an existing one, or remove and re-add this Analysis
from an existing Sample .
Here we are going to create a new Sample by copying an existing one and save the copied values without any changes and receive it afterwards.
The Manage Results listing looks almost similar to the previous ones, but has
the Results field of the
Total Hardness Analysis greyed out, so that you can
not introduce their a manual result anymore.
Magnesium Analyses still take manual Results and we
can test our Calculation by entering the following values for them:
Since we defined the formula as
[Ca] + [Mg], we expect for the
Total Hardness a Result of
The Calculation for
Total Hardness got executed after both dependent
Analyses were saved and the final Result was marked for submission
The same behavior like for Analysis Services applies also for Calculations, so that changes in the Calculation does not affect any of the Analysis created with prior versions of the Calculation.
To demonstrate this, we are going to change the Formula of our
Calculation and introduce a new parameter (Interim) to our formula:
Navigate to the LIMS Setup and edit the prior created Calculation to enter the following values:
- Calculation Interim Fields:
- Default value:
- Calculation Formula:
([Ca] + [Mg]) * [factor]
Press the Save button when you are done.
☝️Note: User-defined parameters (Interim fields) also introduce Keywords that can be referenced in Calculation Formulas like the keywords of Analysis Services. Thus, they are checked for uniqueness in the system.
Navigate back to the prior created Sample and click on the blue 🛈 Button next to
Total Hardness Analysis to view the details of the Analysis.
As you can see in the Calculation section of the displayed popup, the formula
still references the initial version (
[Ca] + [Mg]) and it has not been changed
for this Sample. This mechanism ensures pre-existing tests won't change, even
after modifying the electronic records they are based on. Therefore, this
guarantees the integrity of the data, that is required for compliance with
However, creating a copy of this Sample uses the new Formula.
The new created Sample displays now also the calculation parameter (Interim
field) we introduced earlier. Clicking again on the blue 🛈 Button next to the
Total Hardness Analysis reveals the new version of the Calculation with the
([Ca] + [Mg] * [factor].
Create a file called
calculations.py and put it somewhere in the
on your SENAITE Server. For simplicity, we simply put it directly into the
site-packages directory of the used Python interpreter.
Use this command to find out where the
site-packages directory is located on
python -c "import site; print(site.getsitepackages())"
Add the following contents into
def total_hardness(a, b, factor=1): return float(a + b) * factor
☝️Note: You need to restart your SENAITE instances for this new module to be imported!
Navigate to the prior created calculation and enter the following values:
- Additional Python Libraries:
- Calculation Formula
total_hardness([Ca], [Mg], factor=[factor])
Copy now one of the previous Samples to a new one to use the new Calculation Formula. Also receive the Sample to be able to introduce new Results.
Click again on the blue 🛈 Button next to the
Total Hardness Analysis to verify
that the new Formula is set to
total_hardness([Ca], [Mg], factor=[factor]).
Introduce now some Results for
Magnesium and the
Congratulations 🙌 you successfully created your first Calculation in SENAITE and learned how to import external modules for complex computations.