Skip to main content
This notebook shows a best-practice approach to data validation in Granta MI by defining a set of simple data validation as individual Python functions. Note: The examples shown here do not cover all the possible ways in which data stored in Granta MI could be validated, but show a framework which could be extended to perform regular checking of data in a Granta MI deployment. The approach described in this notebook can be split into three phases:
  1. Define the validation rules
  2. Validate the data
  3. Output the results

1. Define the validation rules

Before we define the rules, import the Scripting Toolkit so we can use Python type hints.

check_attribute_is_populated

Next, define the first data validation rule. This rule will check that an attribute is populated.
All validation rules will have a similar structure, in that they are a function with the same parameters:
  • record: a Scripting Toolkit Record object.
  • attribute_name: the name of the attribute to be validated.
The functions also all return a tuple containing the result as a boolean value (True is a successful check, and False is a failure), and an optional message as a str. The message describes the failure, and so is omitted if the check passed.

check_attribute_is_positive

Now define a second validation rule, which checks that the attribute is positive.
This check will fail with an unhandled exception if attribute.value is non-numeric or empty. The case where the attribute value is empty will be handled by only running this check if the check_attribute_is_populated check was successful. The case where the attribute value is non-numeric should be avoided at runtime by ensuring this check is only applied to numeric attributes.

check_attribute_is_monotonically_increasing

Finally, define a more complex validation rule that checks that the data in series functional attribute is monotonically increasing for each individual series.
Again, the case where the attribute is not a series functional attribute or is empty is unhandled.

Define test cases

Finally, we now create a dictionary to describe the test cases, formed by mapping each attribute to the sequence of validation rules to be applied to that attribute. For example, for text attributes the check only ensures that they are populated, but numeric attributes are checked to ensure that the value is both populated and then that the value is positive. Note that validation rules will be checked in order.
Note that because the function names are not followed by (), the functions are not invoked. This dictionary only contains references to the validation functions, it does not include the results.

2. Validate the data

After defining our validation rules, we can run them against the database. First, connect to Granta MI and fetch the records to validate. This example uses the table.all_records() method and then filters out example records and unreleased v1 records. Alternatively, any approach could be used that produces a list of records, for example searching for a last modified date, or using a Record List.
Previous cell output:
Next, use the keys of the test_cases dictionary defined in the previous section to bulk fetch the data required for these checks.
Finally, run each validation rule in sequence against each record, and store the result in a pandas DataFrame. The results are run in such a way that if a check against a specific record/attribute combination fails, each subsequent check will be skipped.

3. Output the results

The results are stored in a DataFrame, so they can be easily displayed in the notebook. The code below uses the .value_counts() method applied to the Result column to show a summary of the results. Specify dropna=False to include None results, i.e. tests that were skipped.
Previous cell output:
Filter out the passed tests, leaving only the failed or skipped tests, and display the result.
Record Name Record URL Attribute Check Result Message
11 Aluminum alloys, 7075-T6, Sheet http://my.server.name/mi/datasheet.aspx?dbKey=… Common Name check_attribute_is_populated False Attribute “Common Name” is not populated for r…
13 Aluminum alloys, 7075-T6, Sheet http://my.server.name/mi/datasheet.aspx?dbKey=… Statistical Basis check_attribute_is_populated False Attribute “Statistical Basis” is not populated…
16 Aluminum alloys, 7075-T6, Sheet http://my.server.name/mi/datasheet.aspx?dbKey=… Thermal Conductivity check_attribute_is_populated False Attribute “Thermal Conductivity” is not popula…
17 Aluminum alloys, 7075-T6, Sheet http://my.server.name/mi/datasheet.aspx?dbKey=… Thermal Conductivity check_attribute_is_positive None Skipped
18 Aluminum alloys, 7075-T6, Sheet http://my.server.name/mi/datasheet.aspx?dbKey=… Yield Strength, L check_attribute_is_populated False Attribute “Yield Strength, L” is not populated…
19 Aluminum alloys, 7075-T6, Sheet http://my.server.name/mi/datasheet.aspx?dbKey=… Yield Strength, L check_attribute_is_positive None Skipped
20 Aluminum alloys, 7075-T6, Sheet http://my.server.name/mi/datasheet.aspx?dbKey=… Tensile Stress/Strain, L check_attribute_is_populated False Attribute “Tensile Stress/Strain, L” is not po…
21 Aluminum alloys, 7075-T6, Sheet http://my.server.name/mi/datasheet.aspx?dbKey=… Tensile Stress/Strain, L check_attribute_is_monotonically_increasing None Skipped
24 Titanium alloys, Ti-6Al-4V http://my.server.name/mi/datasheet.aspx?dbKey=… Statistical Basis check_attribute_is_populated False Attribute “Statistical Basis” is not populated…
31 Titanium alloys, Ti-6Al-4V http://my.server.name/mi/datasheet.aspx?dbKey=… Tensile Stress/Strain, L check_attribute_is_populated False Attribute “Tensile Stress/Strain, L” is not po…
32 Titanium alloys, Ti-6Al-4V http://my.server.name/mi/datasheet.aspx?dbKey=… Tensile Stress/Strain, L check_attribute_is_monotonically_increasing None Skipped
38 Nickel alloys, Inconel 718, Forging http://my.server.name/mi/datasheet.aspx?dbKey=… Thermal Conductivity check_attribute_is_populated False Attribute “Thermal Conductivity” is not popula…
39 Nickel alloys, Inconel 718, Forging http://my.server.name/mi/datasheet.aspx?dbKey=… Thermal Conductivity check_attribute_is_positive None Skipped
42 Nickel alloys, Inconel 718, Forging http://my.server.name/mi/datasheet.aspx?dbKey=… Tensile Stress/Strain, L check_attribute_is_populated False Attribute “Tensile Stress/Strain, L” is not po…
43 Nickel alloys, Inconel 718, Forging http://my.server.name/mi/datasheet.aspx?dbKey=… Tensile Stress/Strain, L check_attribute_is_monotonically_increasing None Skipped
Alternatively, write the complete set of results to Excel, making it easy to review and share the validation results with other Granta MI data stakeholders. First, ensure the output directory exists. This directory will already exist if a notebook was run previously that saved a file to disk.
Next, save the validation results.

Next steps

The data validation framework shown here illustrates a scalable and extensible approach to validating data in Granta MI with the Scripting Toolkit. It shows only very basic data validation operations though, and could be extended in the following ways:
  • Additional text validation rules. For example, validating spelling and grammar in long text attributes.
  • Additional numeric validation rules. For example, comparing attribute values to each other, or to some generally accepted value range.
  • Additional functional validation rules. For example, checking that a functional attribute evaluated at room temperature is equal to a certain attribute.
  • Automatically modifying the database to ensure consistency.
  • Running the script automatically in batch mode. For more information see Authentication.
  • Tracking previous failures to provide a delta between multiple validation runs.
  • Using a Python testing framework like pytest to organize test cases and provide result reports.