{ "cells": [ { "cell_type": "markdown", "id": "9f56ba90", "metadata": { "tags": [] }, "source": [ "# Performing a Part Impacted Substances Query" ] }, { "cell_type": "markdown", "id": "a77060b6", "metadata": {}, "source": [ "A Part Impacted Substances Query is used to determine the substances associated with a part that are impacted by one\n", "or more defined legislations. The part record can represent either a single component, a subassembly, or a finished\n", "product, and therefore the substances can be associated with either the part record itself, or any other record that\n", "the part directly or indirectly references." ] }, { "cell_type": "markdown", "id": "5176e0a8", "metadata": {}, "source": [ "This example shows how to perform an Impacted Substance query on part records, and how to process the results." ] }, { "cell_type": "markdown", "id": "7fdc6ed8", "metadata": {}, "source": [ "## Connecting to Granta MI" ] }, { "cell_type": "markdown", "id": "a585cbbf", "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": "1a18edf3", "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": "f01c923b", "metadata": { "tags": [] }, "source": [ "## Building and Running the Query" ] }, { "cell_type": "markdown", "id": "f99b8e9e", "metadata": {}, "source": [ "The query is assembled by providing lists of part references and legislations of interest. The query will return\n", "the substances that are present in the specified parts and are impacted by the specified legislations.\n", "\n", "In this example, the 'Drill' part will be used. In contrast to the Material version of this query shown in\n", "a previous example, the Drill part does not reference any substances directly. Instead, it references\n", "sub-components, which in turn reference materials, which then reference substances. The Part Impacted Substances\n", "Query flattens all these layers of complexity and aggregates them together into a single list." ] }, { "cell_type": "markdown", "id": "f3ca7d66", "metadata": {}, "source": [ "First specify some constants that contain the part and legislation references we will use." ] }, { "cell_type": "code", "execution_count": null, "id": "a49843a5", "metadata": { "tags": [] }, "outputs": [], "source": [ "DRILL = \"DRILL\"\n", "WING = \"asm_flap_mating\"\n", "SIN_LIST = \"SINList\"\n", "REACH = \"Candidate_AnnexXV\"" ] }, { "cell_type": "markdown", "id": "b3d77413", "metadata": {}, "source": [ "Next, import the ``queries`` module and build the query with the references in the previous cell." ] }, { "cell_type": "code", "execution_count": null, "id": "a2635c8d", "metadata": { "tags": [] }, "outputs": [], "source": [ "from ansys.grantami.bomanalytics import queries\n", "\n", "part_query = (\n", " queries.PartImpactedSubstancesQuery()\n", " .with_part_numbers([DRILL, WING])\n", " .with_legislation_ids([SIN_LIST, REACH])\n", ")" ] }, { "cell_type": "markdown", "id": "04497f5f", "metadata": {}, "source": [ "Finally, run the query. Passing a ``PartImpactedSubstancesQuery`` object to the ``Connection.run()`` method returns a\n", "``PartImpactedSubstancesQueryResult`` object." ] }, { "cell_type": "code", "execution_count": null, "id": "d9432bec", "metadata": { "tags": [] }, "outputs": [], "source": [ "part_result = cxn.run(part_query)\n", "part_result" ] }, { "cell_type": "markdown", "id": "927f9616", "metadata": {}, "source": [ "A ``PartImpactedSubstancesQueryResult`` object contains three properties:\n", "``impacted_substances_by_part``, ``impacted_substances_by_legislation`` and ``impacted_substances``. They provide\n", "different views of the impacted substances at different levels of granularity." ] }, { "cell_type": "markdown", "id": "faae1dea", "metadata": {}, "source": [ "## Results Grouped by Part" ] }, { "cell_type": "markdown", "id": "df93e1ea", "metadata": {}, "source": [ "This property is structured first as a list of ``PartWithImpactedSubstancesResult`` objects, each of which contains\n", "a dictionary of lists of ``ImpactedSubstance`` objects keyed by legislation, or a single flat list of all\n", "substances." ] }, { "cell_type": "markdown", "id": "de5687dc", "metadata": {}, "source": [ "We can simplify the structure because we are only using Part Numbers. First, create a\n", "dictionary that maps Part Numbers to lists of substances impacted by the 'SIN List'." ] }, { "cell_type": "code", "execution_count": null, "id": "bfffb309", "metadata": { "tags": [] }, "outputs": [], "source": [ "substances_by_part = {}\n", "for part in part_result.impacted_substances_by_part:\n", " part_substances = part.substances_by_legislation[SIN_LIST]\n", " substances_by_part[part.part_number] = part_substances" ] }, { "cell_type": "markdown", "id": "5c2c4e20", "metadata": {}, "source": [ "Then use the ``tabulate`` package to print a table of the substances and their quantities for the wing assembly only." ] }, { "cell_type": "code", "execution_count": null, "id": "7c876c03", "metadata": { "tags": [] }, "outputs": [], "source": [ "from tabulate import tabulate\n", "\n", "rows = [(substance.cas_number, substance.max_percentage_amount_in_material)\n", " for substance in substances_by_part[WING]]\n", "\n", "print(f'Substances impacted by \"{SIN_LIST}\" in \"{WING}\"')\n", "print(tabulate(rows, headers=[\"CAS Number\", \"Amount (wt. %)\"]))" ] }, { "cell_type": "markdown", "id": "7e61ffe9", "metadata": {}, "source": [ "## Results Grouped by Legislation" ] }, { "cell_type": "markdown", "id": "983705d6", "metadata": {}, "source": [ "This property merges the results across all parts, returning a single dictionary of legislations that contain\n", "all impacted substances for all parts." ] }, { "cell_type": "markdown", "id": "bd370199", "metadata": {}, "source": [ "As before, use the ``tabulate`` package to print a table of substances. This time we are including substances in\n", "all parts, but only those on the SIN List." ] }, { "cell_type": "code", "execution_count": null, "id": "6c22eb56", "metadata": { "tags": [] }, "outputs": [], "source": [ "part_substances_sin = part_result.impacted_substances_by_legislation[SIN_LIST]\n", "rows = [(substance.cas_number, substance.max_percentage_amount_in_material)\n", " for substance in part_substances_sin]\n", "print(f'Substances impacted by \"{SIN_LIST}\" in all parts (5/{len(rows)})')\n", "print(tabulate(rows[:5], headers=[\"CAS Number\", \"Amount (wt. %)\"]))" ] }, { "cell_type": "markdown", "id": "3f3cc649", "metadata": {}, "source": [ "## Results as a Flat List" ] }, { "cell_type": "markdown", "id": "2c9cf93c", "metadata": {}, "source": [ "This property reduces the granularity further, producing a single flattened list of substances across all legislations\n", "and for all parts." ] }, { "cell_type": "markdown", "id": "8fed2ccf", "metadata": {}, "source": [ "Use the ``tabulate`` package to print a third table of substances. Because we are using the\n", "``impacted_substances`` property, we only have one list of ``ImpactedSubstance`` objects, which covers both\n", "legislations and both the parts specified above." ] }, { "cell_type": "code", "execution_count": null, "id": "b871229c", "metadata": { "tags": [] }, "outputs": [], "source": [ "part_substances_all = part_result.impacted_substances\n", "rows = [(substance.cas_number, substance.max_percentage_amount_in_material)\n", " for substance in part_substances_all]\n", "print(f'Impacted substances across all legislations in \"DRILL\" (5/{len(rows)})')\n", "print(tabulate(rows[:5], headers=[\"CAS Number\", \"Amount (wt. %)\"]))" ] } ], "metadata": { "jupytext": { "formats": "ipynb,py:light" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }