{ "cells": [ { "cell_type": "markdown", "id": "e008f3ce", "metadata": {}, "source": [ "# Determine compliance for BoMs in external data sources" ] }, { "cell_type": "markdown", "id": "d2d7a1df", "metadata": {}, "source": [ "You might have to deal with BoMs or other data structures stored in third-party systems. This\n", "example shows a scenario where compliance must be determined for a BoM-type structure in a JSON file,\n", "with the result added to the input file." ] }, { "cell_type": "markdown", "id": "318e0c1b", "metadata": {}, "source": [ "Although it is unlikely that the data structures and processing presented here match your\n", "requirements, this example demonstrates the principles behind using the Granta\n", "MI BoM Analytics API within your existing processes. It shows how a BoM-like data structure\n", "can be loaded from a neutral format and used as a starting point for compliance analysis.\n", "The approach is applicable to data in other formats or data loaded from other software\n", "platform APIs." ] }, { "cell_type": "markdown", "id": "e7055df3", "metadata": {}, "source": [ "You can [download](supporting-files/source_data.json) the external data source used in this\n", "example." ] }, { "cell_type": "markdown", "id": "306cb1a9", "metadata": {}, "source": [ "## Load the external data" ] }, { "cell_type": "markdown", "id": "18617201", "metadata": {}, "source": [ "First load the JSON file and use the ``json`` module to convert the text into a hierarchical structure of ``dict`` and\n", "``list`` objects." ] }, { "cell_type": "code", "execution_count": null, "id": "baed042a", "metadata": { "tags": [] }, "outputs": [], "source": [ "import json\n", "from pprint import pprint\n", "\n", "with open(\"supporting-files/source_data.json\") as f:\n", " data = json.load(f)\n", "pprint(data)" ] }, { "cell_type": "markdown", "id": "21e0e84d", "metadata": {}, "source": [ "Because the list of components is used frequently, store it in a variable for convenience." ] }, { "cell_type": "code", "execution_count": null, "id": "b5003373", "metadata": { "tags": [] }, "outputs": [], "source": [ "components = data[\"components\"]" ] }, { "cell_type": "markdown", "id": "f02fdc32", "metadata": {}, "source": [ "It is clear from viewing this data that some parts include multiple materials, and some materials appear in the JSON\n", "file more than once. However, the material compliance is not dependent on the component it is used in, and the\n", "compliance of a part only depends on the worst compliance status of the constituent materials. Therefore, you can\n", "simplify the compliance query by get the compliance for the unique set of materials in the JSON file. You can then\n", "perform some data manipulation of the results." ] }, { "cell_type": "markdown", "id": "52139f60", "metadata": {}, "source": [ "Because the compliance status of a material does not depend on which component it is used in and part compliance\n", "depends only on the worst compliance status of its constituent materials, you can simplify the query by running it\n", "against the set of unique materials in the JSON file. You can then rebuild the data structure from these results to\n", "view the compliance by component." ] }, { "cell_type": "markdown", "id": "24924f09", "metadata": {}, "source": [ "First, use a set comprehension to get the unique materials, which you can then cast into a list." ] }, { "cell_type": "code", "execution_count": null, "id": "d8fade07", "metadata": { "tags": [] }, "outputs": [], "source": [ "material_ids = {m for comp in components for m in comp[\"materials\"]}\n", "material_ids" ] }, { "cell_type": "markdown", "id": "11ee7bde", "metadata": {}, "source": [ "## Get the compliance status" ] }, { "cell_type": "markdown", "id": "1819d907", "metadata": {}, "source": [ "Next, create and run a compliance query using the list of material IDs, as shown in previous examples." ] }, { "cell_type": "code", "execution_count": null, "id": "95a1f2f6", "metadata": { "tags": [] }, "outputs": [], "source": [ "from ansys.grantami.bomanalytics import Connection, indicators, queries\n", "\n", "server_url = \"http://my_grantami_server/mi_servicelayer\"\n", "cxn = Connection(server_url).with_credentials(\"user_name\", \"password\").connect()\n", "svhc = indicators.WatchListIndicator(\n", " name=\"SVHC\",\n", " legislation_ids=[\"Candidate_AnnexXV\"],\n", " default_threshold_percentage=0.1,\n", ")\n", "mat_query = (\n", " queries.MaterialComplianceQuery()\n", " .with_indicators([svhc])\n", " .with_material_ids(material_ids)\n", ")\n", "mat_results = cxn.run(mat_query)\n", "mat_results" ] }, { "cell_type": "markdown", "id": "fc2aa7cc", "metadata": {}, "source": [ "## Postprocess the results" ] }, { "cell_type": "markdown", "id": "c06dceb4", "metadata": {}, "source": [ "The preceding results describe the compliance status for each material, but more work is needed to\n", "provide the compliance status for all the components in the original JSON file." ] }, { "cell_type": "markdown", "id": "9aa4803c", "metadata": {}, "source": [ "When a component contains only one material, the result can simply be copied over. In the general case, moving from\n", "material compliance to component compliance means taking the worst compliance result across all the constituent\n", "materials." ] }, { "cell_type": "markdown", "id": "d28340ac", "metadata": {}, "source": [ "To do this, first create a dictionary that maps a material ID to the indicator result returned by the query." ] }, { "cell_type": "code", "execution_count": null, "id": "7d408d9d", "metadata": { "tags": [] }, "outputs": [], "source": [ "material_lookup = {mat.material_id: mat.indicators[\"SVHC\"]\n", " for mat in mat_results.compliance_by_material_and_indicator}" ] }, { "cell_type": "markdown", "id": "60e5a179", "metadata": { "lines_to_next_cell": 2 }, "source": [ "Next, define a function that takes a list of material IDs and returns the worst compliance status associated with the\n", "materials in the list.\n", "\n", "You can use the built-in ``max()`` function to do this because you can compare ``WatchListIndicator`` with ``>``\n", "and ``<`` operators. The convention is that a worse result is *greater than* a better result." ] }, { "cell_type": "code", "execution_count": null, "id": "31c05e63", "metadata": { "tags": [] }, "outputs": [], "source": [ "def rollup_results(material_ids) -> str:\n", " indicator_results = [material_lookup[mat_id] for mat_id in material_ids]\n", " worst_result = max(indicator_results)\n", " return worst_result.flag.name" ] }, { "cell_type": "markdown", "id": "1762f2bd", "metadata": {}, "source": [ "Now call this function for each component in a ``dict`` comprehension to obtain a mapping between part number\n", "and compliance status." ] }, { "cell_type": "code", "execution_count": null, "id": "e16003d8", "metadata": { "tags": [] }, "outputs": [], "source": [ "component_results = {comp[\"part_number\"]: rollup_results(comp[\"materials\"])\n", " for comp in components}\n", "component_results" ] }, { "cell_type": "markdown", "id": "b1cf0cf5", "metadata": {}, "source": [ "These results include text defined by the API for compliance status. However, you might want the compliance\n", "status to determine the approvals required to release the part in a design review process. In this case, you can\n", "define a mapping between compliance status and approval requirements." ] }, { "cell_type": "code", "execution_count": null, "id": "0afa234a", "metadata": { "tags": [] }, "outputs": [], "source": [ "flags = indicators.WatchListFlag\n", "result_map = {\n", " flags.WatchListCompliant.name: \"No Approval Required\",\n", " flags.WatchListAllSubstancesBelowThreshold.name: \"Level 1 Approval Required\",\n", " flags.WatchListHasSubstanceAboveThreshold.name: \"Level 2 Approval Required\",\n", "}" ] }, { "cell_type": "markdown", "id": "a78969d9", "metadata": {}, "source": [ "You can now use this dictionary to map from the Granta MI result to the approval requirements." ] }, { "cell_type": "code", "execution_count": null, "id": "e14c14c8", "metadata": { "tags": [] }, "outputs": [], "source": [ "results = {part_number: result_map[result]\n", " for part_number, result in component_results.items()}\n", "results" ] }, { "cell_type": "markdown", "id": "89a6ab05", "metadata": {}, "source": [ "## Write the output" ] }, { "cell_type": "markdown", "id": "db50e043", "metadata": {}, "source": [ "Once you have your final result, you can take your result ``dict`` and use it to extend the original JSON data\n", "structure, with approval requirements added in." ] }, { "cell_type": "code", "execution_count": null, "id": "b3f540f7", "metadata": {}, "outputs": [], "source": [ "components_with_result = []\n", "for component in components:\n", " component_with_result = component\n", " part_number = component[\"part_number\"]\n", " component_with_result[\"approval\"] = results[part_number]\n", " components_with_result.append(component_with_result)\n", "\n", "data_results = {}\n", "data_results[\"components\"] = components_with_result" ] }, { "cell_type": "markdown", "id": "a239846f", "metadata": {}, "source": [ "Print the results to see that the new data structure includes the results." ] }, { "cell_type": "code", "execution_count": null, "id": "f1448edb", "metadata": { "tags": [] }, "outputs": [], "source": [ "pprint(data_results)" ] } ], "metadata": { "jupytext": { "formats": "ipynb,py:light" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }