- Define the validation rules
- Validate the data
- 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.record: a Scripting ToolkitRecordobject.attribute_name: the name of the attribute to be validated.
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.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.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.(), 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 thetable.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.
test_cases dictionary defined in the previous section to bulk fetch the data required
for these checks.
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.
| 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 |
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. For more information see Storing Credentials Securely.
- 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.