{ "cells": [ { "cell_type": "markdown", "id": "60057ab3", "metadata": {}, "source": [ "# Perform a material impacted substances query" ] }, { "cell_type": "markdown", "id": "7d9a42ec", "metadata": {}, "source": [ "A Material Impacted Substances Query is used to identify the substances associated with a material that are impacted\n", "by one or more defined legislations." ] }, { "cell_type": "markdown", "id": "865afcb2", "metadata": {}, "source": [ "This example shows how to perform an Impacted Substance query on material records, and how to process the results." ] }, { "cell_type": "markdown", "id": "d3e2bb0a", "metadata": {}, "source": [ "## Connecting to Granta MI" ] }, { "cell_type": "markdown", "id": "3f7ea417", "metadata": {}, "source": [ "Import the ``Connection`` class and create the connection. See the [Getting Started](../0_Getting_started.ipynb)\n", "example for more detail." ] }, { "cell_type": "code", "execution_count": null, "id": "37465b8b", "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": "8c1844ad", "metadata": {}, "source": [ "## Building and running the query" ] }, { "cell_type": "markdown", "id": "bf252027", "metadata": {}, "source": [ "The query is assembled by providing lists of material references and legislations of interest. The query will return\n", "the substances that are present in the specified materials and are impacted by the specified legislations." ] }, { "cell_type": "markdown", "id": "9c47a7ea", "metadata": {}, "source": [ "First specify some constants that contain the material and legislation references we will use." ] }, { "cell_type": "code", "execution_count": null, "id": "cde83a8d", "metadata": { "tags": [] }, "outputs": [], "source": [ "PPS_ID = \"plastic-pps-generalpurpose\"\n", "PC_ID = \"plastic-pc-20carbonfiber\"\n", "SIN_LIST = \"SINList\"\n", "REACH = \"Candidate_AnnexXV\"" ] }, { "cell_type": "markdown", "id": "a5a8c749", "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": "3c63a0b5", "metadata": { "tags": [] }, "outputs": [], "source": [ "from ansys.grantami.bomanalytics import queries\n", "\n", "mat_query = (\n", " queries.MaterialImpactedSubstancesQuery()\n", " .with_material_ids([PPS_ID, PC_ID])\n", " .with_legislation_ids([REACH, SIN_LIST])\n", ")" ] }, { "cell_type": "markdown", "id": "904d43ad", "metadata": {}, "source": [ "Finally, run the query. Passing a ``MaterialImpactedSubstancesQuery`` object to the ``Connection.run()`` method\n", "returns a ``MaterialImpactedSubstancesQueryResult`` object." ] }, { "cell_type": "code", "execution_count": null, "id": "3e9a1a39", "metadata": { "tags": [] }, "outputs": [], "source": [ "results = cxn.run(mat_query)\n", "results" ] }, { "cell_type": "markdown", "id": "29cac5a2", "metadata": {}, "source": [ "A ``MaterialImpactedSubstancesQueryResult`` object contains three properties:\n", "``impacted_substances_by_material``, ``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": "79404c1d", "metadata": {}, "source": [ "## View results grouped by material" ] }, { "cell_type": "markdown", "id": "41e5a522", "metadata": {}, "source": [ "This property is structured first as a list of ``MaterialWithImpactedSubstancesResult`` objects, each of which\n", "contains a dictionary of lists of ``ImpactedSubstance`` objects keyed by legislation or a single flat list of all\n", "substances." ] }, { "cell_type": "markdown", "id": "c71b56fb", "metadata": {}, "source": [ "First, we can simplify the structure somewhat because we are only using Material IDs. The cell below creates a\n", "dictionary that maps Material IDs to lists of substances impacted by the 'SIN List'." ] }, { "cell_type": "code", "execution_count": null, "id": "91ebe1c7", "metadata": { "tags": [] }, "outputs": [], "source": [ "substances_by_material = {}\n", "for material in results.impacted_substances_by_material:\n", " substances = material.substances_by_legislation[SIN_LIST]\n", " substances_by_material[material.material_id] = substances" ] }, { "cell_type": "markdown", "id": "396fe02f", "metadata": {}, "source": [ "Then use the ``tabulate`` package to print a table of the substances and their quantities for the polycarbonate\n", "material only." ] }, { "cell_type": "code", "execution_count": null, "id": "075fc243", "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_material[PC_ID]]\n", "\n", "print(f'Substances impacted by \"{SIN_LIST}\" in \"{PC_ID}\" (5/{len(rows)})')\n", "print(tabulate(rows[:5], headers=[\"CAS Number\", \"Amount (wt. %)\"]))" ] }, { "cell_type": "markdown", "id": "d5e2162a", "metadata": {}, "source": [ "## View results grouped by legislation" ] }, { "cell_type": "markdown", "id": "dbd28eb6", "metadata": {}, "source": [ "This property merges the results across all materials, resulting in a single dictionary of legislations that contain\n", "all impacted substances for all materials." ] }, { "cell_type": "markdown", "id": "e3acb03f", "metadata": {}, "source": [ "Again we use the ``tabulate`` package to print a table of substances, but this time we are including the substances in\n", "all materials, but again limited to the SIN List only." ] }, { "cell_type": "code", "execution_count": null, "id": "1413fb31", "metadata": { "tags": [] }, "outputs": [], "source": [ "material_substances_sin = results.impacted_substances_by_legislation[SIN_LIST]\n", "rows = [(substance.cas_number, substance.max_percentage_amount_in_material)\n", " for substance in material_substances_sin]\n", "print(f'Substances impacted by \"{SIN_LIST}\" in all materials (5/{len(rows)})')\n", "print(tabulate(rows[:5], headers=[\"CAS Number\", \"Amount (wt. %)\"]))" ] }, { "cell_type": "markdown", "id": "57f87ebe", "metadata": {}, "source": [ "## View results as a flat list" ] }, { "cell_type": "markdown", "id": "c320ad95", "metadata": {}, "source": [ "This property reduces the granularity further to produce a single flattened list of substances across all legislations\n", "for all materials." ] }, { "cell_type": "markdown", "id": "8a701ff9", "metadata": {}, "source": [ "The cell below uses the ``tabulate`` package to print a 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 materials." ] }, { "cell_type": "code", "execution_count": null, "id": "1744cc6f", "metadata": { "tags": [] }, "outputs": [], "source": [ "material_substances_all = results.impacted_substances\n", "rows = [(substance.cas_number, substance.max_percentage_amount_in_material)\n", " for substance in material_substances_all]\n", "print(f\"Impacted substances for all materials and legislations (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 }