Skip to main content
The following script demonstrates how to compare raw test data to specification values that define acceptable ranges for the test data. In this example, the specification values are stored in a separate record that is linked to the test data.

Connect to Granta MI

Connect to Granta MI and specify the database and table containing the test results. Since this operation is likely to be performed as a batch operation, see the notebook on Storing Credentials Securely for more information how to connect to Granta MI securely in a batch Python script.
Previous cell output:

Find test data to process

Before we start, we need to find the Test Data records we want to process by applying the following criteria:
  1. The test record has test data which has not previously been compared to a specification.
  2. The test record is linked to a specification record.

Criteria 1: Test data that has not been compared to a specification

First, define the attributes that contain the relevant test data, and the name of the meta-attribute that contains specification comparison results (if they exist). A dictionary is used for test data attributes so they can be mapped to the corresponding specification attributes.
Next, define an unprocessed records function that takes an attribute name as input and returns the list of records where the attribute is populated, but the meta-attribute is empty.
Iterate over each attribute defined in the ATTRIBUTES constant to get the previously unprocessed records for each one. Use a dictionary with the record.record_guid as the key to ensure you only get unique records (a single record may be missing multiple attributes, and we only want to process each record once).
Finally, extract the record values from the dict and confirm the number of unprocessed records identified.
Previous cell output:

Criteria 2: Test data with linked specifications

To compare test data to specification values, each unprocessed record must also be linked to a specification record containing those values. Therefore, we need to filter the unprocessed_records list to include only those records that have a link to a Specification Values record. Use the test_data_table.bulk_link_fetch() method to improve performance by fetching links for all records in a single operation. The link group name is stored in a constant, since we will need to refer to it later.
Previous cell output:

Compare test data to specification values

Now check each record in the records_to_process list against its linked specification.

Pre-fetch the data

First, pre-fetch all the attributes in the Specification Values and Tensile Test Data tables that are required for the analysis. (This step is optional, but helps improve performance for large datasets.) Pre-fetching data using the bulk_fetch() operation generally requires three types of objects: a list of attributes, a list of records, and the table to fetch the data from. For the test data, we already have the test_data_table object and the list of records. However, we need to construct the list of attributes. We want to export the following:
  • The Specimen ID attribute, used for printing status information while performing the comparison
  • The list of test data attributes defined as values in the COMPARISON_ATTRIBUTES dictionary
  • The Specification Status meta-attribute
The cell below builds this list of attributes, and then uses bulk_fetch() to fetch the data.
Previous cell output:
For the specification values we know the attributes we need, but we do not have a list of records or a table object. The following steps must be performed:
  1. Create a single flat list of all linked specification records. Use a set as the intermediary container to only include unique records.
  2. Extract the table name from one of the specification records and create a specification_table object.
  3. Use the specification_table object to bulk fetch the data.
Previous cell output:

Comparison logic

Next, write a function that compares the test data attribute and the specification attribute and returns the result as a string. This function includes an initial check to ensure values exist for both attributes. If either attribute does not have a value, return None.

Compare the data

Finally, iterate over all records in the records_to_process list and all attributes in the COMPARISON_ATTRIBUTES constant. For each combination of record and attribute, call the function defined above and store the result in the Specification Status meta-attribute. The comparison is skipped if the meta-attribute is already populated.
Previous cell output:

Push the results to Granta MI

The final step is to update the processed records in Granta MI. Once the cell below has completed, click the link to view the results in MI Viewer.
Previous cell output:

Clean up the database

Uncomment the bottom line and run this cell to revert the changes made in this script. This is useful if you wish to run the script again.