{ "cells": [ { "cell_type": "markdown", "id": "acbe253a", "metadata": {}, "source": [ "# Perform a material impacted substances query" ] }, { "cell_type": "markdown", "id": "031d1817", "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": "7a9c7e4f", "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": "7d96f773", "metadata": {}, "source": [ "## Connect to Granta MI" ] }, { "cell_type": "markdown", "id": "3b45770f", "metadata": {}, "source": [ "Import the ``Connection`` class and create the connection. For more information, see the\n", "[Getting Started](../0_Getting_started.ipynb) example." ] }, { "cell_type": "code", "execution_count": null, "id": "da03cd37", "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": "a501274b", "metadata": {}, "source": [ "## Build and run the query" ] }, { "cell_type": "markdown", "id": "9f40d0e9", "metadata": {}, "source": [ "The query is assembled by providing lists of material references and legislations of interest. The query returns\n", "the substances that are present in the specified materials and are impacted by the specified legislations." ] }, { "cell_type": "markdown", "id": "2383e02c", "metadata": {}, "source": [ "First specify some constants that contain the material and legislation references to use." ] }, { "cell_type": "code", "execution_count": null, "id": "37b44a54", "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": "dd3b075c", "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": "8a4ba45e", "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": "52bc2191", "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": "38894669", "metadata": { "tags": [] }, "outputs": [], "source": [ "results = cxn.run(mat_query)\n", "results" ] }, { "cell_type": "markdown", "id": "d72300fd", "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": "ee399201", "metadata": {}, "source": [ "## View results grouped by material" ] }, { "cell_type": "markdown", "id": "01c66d90", "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": "1e5542bf", "metadata": {}, "source": [ "First, you can simplify the structure somewhat because you are only using Material IDs. The following cell creates a\n", "dictionary that maps material IDs to lists of substances impacted by the ``SIN_LIST``." ] }, { "cell_type": "code", "execution_count": null, "id": "56ef7554", "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": "bdef51ef", "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": "21bd4e63", "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": "b13db319", "metadata": {}, "source": [ "## View results grouped by legislation" ] }, { "cell_type": "markdown", "id": "8fd67cec", "metadata": {}, "source": [ "This property merges the results across all materials, resulting in a single dictionary of legislations that contains\n", "all impacted substances for all materials." ] }, { "cell_type": "markdown", "id": "de61c969", "metadata": {}, "source": [ "Again use the ``tabulate`` package to print a table of substances, but this time include the substances in\n", "all materials, one again limited to the ``SIN_LIST`` only." ] }, { "cell_type": "code", "execution_count": null, "id": "9aa0485f", "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": "9e860fcd", "metadata": {}, "source": [ "## View results as a flat list" ] }, { "cell_type": "markdown", "id": "19859ecd", "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": "80cebcaa", "metadata": {}, "source": [ "The following cell uses the ``tabulate`` package to print a table of substances. Because you are using the\n", "``impacted_substances`` property, you only have one list of ``ImpactedSubstance`` objects, which covers both\n", "legislations and both materials." ] }, { "cell_type": "code", "execution_count": null, "id": "2f691c4a", "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 }