{ "cells": [ { "cell_type": "markdown", "id": "6ad28007", "metadata": {}, "source": [ "# Performing a Material Compliance Query" ] }, { "cell_type": "markdown", "id": "1f3a5b60", "metadata": {}, "source": [ "A Material Compliance Query determines whether one or more materials are compliant with the specified indicators. This\n", "is done by first determining compliance for the substances associated with the material, and then rolling up the\n", "results to the material." ] }, { "cell_type": "markdown", "id": "2231da1e", "metadata": {}, "source": [ "## Connecting to Granta MI" ] }, { "cell_type": "markdown", "id": "9b08c8f7", "metadata": {}, "source": [ "Import the ``Connection`` class and create the connection. See the [Getting Started](../0_Getting_started.ipynb)\n", "example for more details." ] }, { "cell_type": "code", "execution_count": null, "id": "da3cc760", "metadata": { "tags": [] }, "outputs": [], "source": [ "from ansys.grantami.bomanalytics import Connection\n", "\n", "server_url = \"http://my_grantami_server/mi_servicelayer\"\n", "cxn = Connection(server_url).with_credentials(\"user_name\", \"password\").connect()" ] }, { "cell_type": "markdown", "id": "222ec63c", "metadata": {}, "source": [ "## Defining an Indicator" ] }, { "cell_type": "markdown", "id": "228f3d9a", "metadata": {}, "source": [ "A Compliance query determines compliance against 'Indicators', as opposed to an Impacted Substances query which\n", "determines compliance directly against legislations.\n", "\n", "There are two types of Indicator object (``WatchListIndicator`` and ``RohsIndicator``), and the syntax presented below\n", "applies to both. The differences in the internal implementation of the two objects are described in the API\n", "documentation.\n", "\n", "Generally speaking, if a substance is impacted by a legislation associated with an indicator, and in a quantity\n", "above a specified threshold, the substance is non-compliant with that indicator. This non-compliance applies to\n", "any other items in the BoM hierarchy that directly or indirectly include that substance." ] }, { "cell_type": "markdown", "id": "fdb32c01", "metadata": {}, "source": [ "First, create two ``WatchListIndicator`` objects." ] }, { "cell_type": "code", "execution_count": null, "id": "39495d04", "metadata": { "tags": [] }, "outputs": [], "source": [ "from ansys.grantami.bomanalytics import indicators\n", "\n", "svhc = indicators.WatchListIndicator(\n", " name=\"SVHC\",\n", " legislation_ids=[\"Candidate_AnnexXV\"],\n", " default_threshold_percentage=0.1,\n", ")\n", "sin = indicators.WatchListIndicator(\n", " name=\"SIN\",\n", " legislation_ids=[\"SINList\"]\n", ")" ] }, { "cell_type": "markdown", "id": "9f9e360d", "metadata": { "tags": [] }, "source": [ "## Building and Running the Query" ] }, { "cell_type": "markdown", "id": "5aad1d80", "metadata": {}, "source": [ "Next define the query itself. Materials can be referenced by Granta MI record reference or Material ID.\n", "The table containing the Material records is not required, since this is enforced by the Restricted Substances\n", "database schema." ] }, { "cell_type": "code", "execution_count": null, "id": "6ee1aa01", "metadata": { "tags": [] }, "outputs": [], "source": [ "from ansys.grantami.bomanalytics import queries\n", "\n", "mat_query = queries.MaterialComplianceQuery().with_indicators([svhc, sin])\n", "mat_query = mat_query.with_material_ids([\"plastic-pa66-60glassfiber\",\n", " \"zinc-pb-cdlow-alloy-z21220-rolled\",\n", " \"stainless-316h\"])" ] }, { "cell_type": "markdown", "id": "ac511301", "metadata": {}, "source": [ "Finally, run the query. Passing a ``MaterialComplianceQuery`` object to the ``Connection.run()`` method returns a\n", "``MaterialComplianceQueryResult`` object." ] }, { "cell_type": "code", "execution_count": null, "id": "778b00ec", "metadata": { "tags": [] }, "outputs": [], "source": [ "mat_result = cxn.run(mat_query)\n", "mat_result" ] }, { "cell_type": "markdown", "id": "b5c11049", "metadata": { "tags": [] }, "source": [ "The result object contains two properties: ``compliance_by_material_and_indicator`` and ``compliance_by_indicator``." ] }, { "cell_type": "markdown", "id": "418ca0b1", "metadata": {}, "source": [ "## Results Grouped by Material" ] }, { "cell_type": "markdown", "id": "29ad8201", "metadata": { "tags": [] }, "source": [ "The ``compliance_by_material_and_indicator`` property contains a list of ``MaterialWithComplianceResult`` objects with\n", "the reference to the material record and the compliance status for each indicator. The\n", "``SubstanceWithComplianceResult`` objects are also included because compliance was determined based on the substances\n", "associated with the material object. These are also accompanied by their compliance status for each indicator." ] }, { "cell_type": "markdown", "id": "5b113b9f", "metadata": {}, "source": [ "Initially, we can print just the results for the reinforced PA66 record." ] }, { "cell_type": "code", "execution_count": null, "id": "fb55f741", "metadata": { "lines_to_end_of_cell_marker": 0, "lines_to_next_cell": 1, "tags": [] }, "outputs": [], "source": [ "pa_66 = mat_result.compliance_by_material_and_indicator[0]\n", "print(f\"PA66 (60% glass fiber): {pa_66.indicators['SVHC'].flag.name}\")" ] }, { "cell_type": "markdown", "id": "68477229", "metadata": {}, "source": [ "The reinforced PA66 record has a status of 'WatchListHasSubstanceAboveThreshold', which tells us the material is not\n", "compliant with the indicator, and therefore contains SVHCs above the 0.1% threshold." ] }, { "cell_type": "markdown", "id": "22884593", "metadata": {}, "source": [ "To understand which substances have caused this status, we can print the substances that are not compliant with the\n", "legislation and the percentage amount of that substance in the parent material. The possible states of the indicator\n", "are available on the ``Indicator.available_flags`` attribute and can be compared using standard Python operators.\n", "\n", "For substances, the critical threshold is the state 'WatchListAboveThreshold'." ] }, { "cell_type": "code", "execution_count": null, "id": "698e4ed8", "metadata": { "tags": [] }, "outputs": [], "source": [ "def convert_substance_to_string(substance):\n", " result = f\"Substance record history identity: {substance.record_history_identity}\"\n", " if substance.percentage_amount is None:\n", " return f\"{result}, Unknown concentration\"\n", " else:\n", " return f\"{result}, {substance.percentage_amount}%\"\n", "\n", "\n", "above_threshold_flag = svhc.available_flags.WatchListAboveThreshold\n", "pa_66_svhcs = [sub for sub in pa_66.substances\n", " if sub.indicators[\"SVHC\"] >= above_threshold_flag\n", " ]\n", "print(f\"{len(pa_66_svhcs)} SVHCs\")\n", "for substance in pa_66_svhcs:\n", " print(convert_substance_to_string(substance))" ] }, { "cell_type": "markdown", "id": "d678ed1d", "metadata": {}, "source": [ "Substances with an unknown amount are always treated as if they have a 100% concentration. Note that children of items\n", "passed into the compliance query are returned with record references based on record history identities only. The\n", "Granta MI Scripting Toolkit for Python can be used to translate record history identities into CAS Numbers if\n", "required." ] }, { "cell_type": "markdown", "id": "b8a84684", "metadata": {}, "source": [ "Next, look at the state of the zinc alloy record." ] }, { "cell_type": "code", "execution_count": null, "id": "60f7c919", "metadata": { "tags": [] }, "outputs": [], "source": [ "zn_pb_cd = mat_result.compliance_by_material_and_indicator[1]\n", "print(f\"Zn-Pb-Cd low alloy: {zn_pb_cd.indicators['SVHC'].flag.name}\")" ] }, { "cell_type": "markdown", "id": "3410ab4d", "metadata": {}, "source": [ "The zinc alloy record has the status 'WatchListAllSubstancesBelowThreshold', which means there are substances present\n", "that are impacted by the legislation, but are below the 0.1% threshold." ] }, { "cell_type": "markdown", "id": "8201a098", "metadata": {}, "source": [ "We can print these substances using the 'WatchListBelowThreshold' flag as the threshold." ] }, { "cell_type": "code", "execution_count": null, "id": "1782cb6e", "metadata": { "tags": [] }, "outputs": [], "source": [ "below_threshold_flag = svhc.available_flags.WatchListBelowThreshold\n", "zn_svhcs_below_threshold = [sub for sub in zn_pb_cd.substances\n", " if sub.indicators[\"SVHC\"].flag == below_threshold_flag]\n", "print(f\"{len(zn_svhcs_below_threshold)} SVHCs below threshold\")\n", "for substance in zn_svhcs_below_threshold:\n", " print(convert_substance_to_string(substance))" ] }, { "cell_type": "markdown", "id": "fe2cb3a4", "metadata": {}, "source": [ "Finally, look at the stainless steel record." ] }, { "cell_type": "code", "execution_count": null, "id": "74357d69", "metadata": { "tags": [] }, "outputs": [], "source": [ "ss_316h = mat_result.compliance_by_material_and_indicator[2]\n", "print(f\"316H stainless steel: {ss_316h.indicators['SVHC'].flag.name}\")" ] }, { "cell_type": "markdown", "id": "aeaa0d02", "metadata": {}, "source": [ "The stainless steel record has the status 'WatchListCompliant', which means there are no impacted substances in the\n", "material." ] }, { "cell_type": "markdown", "id": "6312ed0f", "metadata": {}, "source": [ "We can print these substances using the 'WatchListNotImpacted' flag as the threshold." ] }, { "cell_type": "code", "execution_count": null, "id": "b5db4792", "metadata": { "tags": [] }, "outputs": [], "source": [ "not_impacted_flag = svhc.available_flags.WatchListNotImpacted\n", "ss_not_impacted = [\n", " sub\n", " for sub in ss_316h.substances\n", " if sub.indicators[\"SVHC\"].flag == not_impacted_flag\n", "]\n", "print(f\"{len(ss_not_impacted)} non-SVHC substances\")\n", "for substance in ss_not_impacted:\n", " print(convert_substance_to_string(substance))" ] }, { "cell_type": "markdown", "id": "29de16c0", "metadata": {}, "source": [ "## Results Grouped by Indicator" ] }, { "cell_type": "markdown", "id": "d9e8ce43", "metadata": {}, "source": [ "Alternatively, using the ``compliance_by_indicator`` property provides a single indicator result that summarizes the\n", "results across all materials in the query. This would be useful in a situation where we have a 'concept' assembly\n", "stored outside of Granta MI, and want to determine its compliance. We know it contains the materials specified in\n", "the query above, and so using ``compliance_by_indicator`` will tell us if that concept assembly is compliant based on\n", "the worst result from individual materials." ] }, { "cell_type": "code", "execution_count": null, "id": "9506ff51", "metadata": { "tags": [] }, "outputs": [], "source": [ "if mat_result.compliance_by_indicator[\"SVHC\"] >= above_threshold_flag:\n", " print(\"One or more materials contains an SVHC in a quantity > 0.1%\")\n", "else:\n", " print(\"No SVHCs, or SVHCs are present in a quantity < 0.1%\")" ] }, { "cell_type": "markdown", "id": "2ec6053f", "metadata": {}, "source": [ "Note that this cannot tell us which material is responsible for the non-compliance. This would require performing a\n", "more granular analysis as shown above, or importing the assembly into Granta MI and running the compliance on that\n", "part record." ] } ], "metadata": { "jupytext": { "formats": "ipynb,py:light" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }