{ "cells": [ { "cell_type": "markdown", "id": "ce060fd4", "metadata": {}, "source": [ "# Process phase\n", "\n", "This example shows how to explore the process phase results of a sustainability summary query.\n", "\n", "The following supporting files are required for this example:\n", "\n", "* [sustainability-bom-2412.xml](../supporting-files/sustainability-bom-2412.xml)\n", "\n", "For help on constructing an XML BoM, see [BoM examples](../6_BoMs/index.rst)." ] }, { "cell_type": "markdown", "id": "e8697bbf", "metadata": {}, "source": [ "
\n", "\n", "**Info:**\n", "\n", "This example uses an input file that is in the 24/12 XML BoM format. This structure requires Granta MI Restricted\n", "Substances and Sustainability Reports 2025 R2 or later.\n", "\n", "To run this example with an older version of the reports bundle, use\n", "[sustainability-bom-2301.xml](../supporting-files/sustainability-bom-2301.xml) instead. Some sections of this example\n", "will produce different results from the published example when this BoM is used.\n", "
" ] }, { "cell_type": "markdown", "id": "8985e684", "metadata": {}, "source": [ "## Run a sustainability summary query" ] }, { "cell_type": "code", "execution_count": null, "id": "ad90e5d1", "metadata": {}, "outputs": [], "source": [ "from ansys.grantami.bomanalytics import Connection, queries\n", "\n", "MASS_UNIT = \"kg\"\n", "ENERGY_UNIT = \"MJ\"\n", "DISTANCE_UNIT = \"km\"\n", "\n", "server_url = \"http://my_grantami_server/mi_servicelayer\"\n", "cxn = Connection(server_url).with_credentials(\"user_name\", \"password\").connect()\n", "\n", "xml_file_path = \"../supporting-files/sustainability-bom-2412.xml\"\n", "with open(xml_file_path) as f:\n", " bom = f.read()\n", "\n", "sustainability_summary_query = (\n", " queries.BomSustainabilitySummaryQuery()\n", " .with_bom(bom)\n", " .with_units(mass=MASS_UNIT, energy=ENERGY_UNIT, distance=DISTANCE_UNIT)\n", ")\n", "sustainability_summary = cxn.run(sustainability_summary_query)" ] }, { "cell_type": "markdown", "id": "f8dc1a40", "metadata": {}, "source": [ "## Process phase\n", "\n", "The environmental contributions from primary and secondary processing (applied to materials) and the joining and\n", "finishing processes (applied to parts) are summarized in the ``primary_processes_details``,\n", "``secondary_processes_details``, and ``joining_and_finishing_processes_details`` properties respectively.\n", "Each of these properties lists the unique process-material pairs (for primary and secondary processing) or\n", "individual processes (for joining and finishing) that contribute at least 5% of the total impact for that\n", "category of process. The percentage contributions are relative to the total contribution of all processes\n", "from the same category. Processes that do not meet the contribution threshold are aggregated under the\n", "``Other`` item, with the material set to ``None``." ] }, { "cell_type": "markdown", "id": "6f81fe31", "metadata": {}, "source": [ "### Primary processing" ] }, { "cell_type": "code", "execution_count": null, "id": "d8fc5c32", "metadata": {}, "outputs": [], "source": [ "sustainability_summary.primary_processes_details" ] }, { "cell_type": "code", "execution_count": null, "id": "f8f46ee9", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "EE_HEADER = f\"EE [{ENERGY_UNIT}]\"\n", "CC_HEADER = f\"CC [{MASS_UNIT}]\"\n", "\n", "primary_process_df = pd.DataFrame.from_records(\n", " [\n", " {\n", " \"Process name\": item.process_name,\n", " \"Material name\": item.material_identity,\n", " \"EE%\": item.embodied_energy_percentage,\n", " EE_HEADER: item.embodied_energy.value,\n", " \"CC%\": item.climate_change_percentage,\n", " CC_HEADER: item.climate_change.value,\n", " }\n", " for item in sustainability_summary.primary_processes_details\n", " ]\n", ")\n", "primary_process_df" ] }, { "cell_type": "markdown", "id": "645af781", "metadata": {}, "source": [ "Add a ``Name`` to each item that represents the process-material pair name." ] }, { "cell_type": "code", "execution_count": null, "id": "5451c8bc", "metadata": {}, "outputs": [], "source": [ "primary_process_df[\"Name\"] = primary_process_df.apply(\n", " lambda row: f\"{row['Process name']} - {row['Material name']}\", axis=1\n", ")\n", "primary_process_df" ] }, { "cell_type": "markdown", "id": "dbc688df", "metadata": {}, "source": [ "This example produces multiple plots which all consist of a pair of pie charts representing the\n", "\"Embodied Energy\" and \"Climate Change CO2 equivalent\" impacts respectively. Define a\n", "helper function to create these plots." ] }, { "cell_type": "code", "execution_count": null, "id": "b4576040", "metadata": { "lines_to_end_of_cell_marker": 0, "lines_to_next_cell": 1 }, "outputs": [], "source": [ "import plotly.graph_objects as go\n", "from plotly.subplots import make_subplots\n", "\n", "\n", "def plot_impact(df, title):\n", " fig = make_subplots(\n", " rows=1,\n", " cols=2,\n", " specs=[[{\"type\": \"domain\"}, {\"type\": \"domain\"}]],\n", " subplot_titles=[\"Embodied Energy\", \"Climate Change\"],\n", " )\n", " fig.add_trace(go.Pie(labels=df[\"Name\"], values=df[EE_HEADER], name=ENERGY_UNIT), 1, 1)\n", " fig.add_trace(go.Pie(labels=df[\"Name\"], values=df[CC_HEADER], name=MASS_UNIT), 1, 2)\n", " fig.update_layout(title_text=title, legend=dict(orientation=\"h\"))\n", " fig.update_traces(textposition=\"inside\", textinfo=\"percent\", hoverinfo=\"value+name+label\")\n", " fig.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "2b488bf8", "metadata": {}, "outputs": [], "source": [ "plot_impact(primary_process_df, \"Aggregated primary processes impact\")" ] }, { "cell_type": "markdown", "id": "17c1affc", "metadata": {}, "source": [ "### Secondary processing" ] }, { "cell_type": "code", "execution_count": null, "id": "5f5bd900", "metadata": {}, "outputs": [], "source": [ "sustainability_summary.secondary_processes_details" ] }, { "cell_type": "code", "execution_count": null, "id": "1b6f1190", "metadata": {}, "outputs": [], "source": [ "secondary_process_df = pd.DataFrame.from_records(\n", " [\n", " {\n", " \"Process name\": item.process_name,\n", " \"Material name\": item.material_identity,\n", " \"EE%\": item.embodied_energy_percentage,\n", " EE_HEADER: item.embodied_energy.value,\n", " \"CC%\": item.climate_change_percentage,\n", " CC_HEADER: item.climate_change.value,\n", " }\n", " for item in sustainability_summary.secondary_processes_details\n", " ]\n", ")\n", "secondary_process_df" ] }, { "cell_type": "markdown", "id": "2b442d00", "metadata": {}, "source": [ "Add a ``Name`` to each item that represents the process-material pair name." ] }, { "cell_type": "code", "execution_count": null, "id": "04afa8fa", "metadata": {}, "outputs": [], "source": [ "secondary_process_df[\"Name\"] = secondary_process_df.apply(\n", " lambda row: f\"{row['Process name']} - {row['Material name']}\", axis=1\n", ")\n", "plot_impact(secondary_process_df, \"Aggregated secondary processes impact\")" ] }, { "cell_type": "markdown", "id": "1a07f971", "metadata": {}, "source": [ "### Joining and finishing\n", "\n", "Joining and finishing processes apply to parts or assemblies and therefore don't include a material identity." ] }, { "cell_type": "code", "execution_count": null, "id": "ae0da7b9", "metadata": {}, "outputs": [], "source": [ "sustainability_summary.joining_and_finishing_processes_details" ] }, { "cell_type": "code", "execution_count": null, "id": "e7013b1b", "metadata": {}, "outputs": [], "source": [ "joining_and_finishing_processes_df = pd.DataFrame.from_records(\n", " [\n", " {\n", " \"Name\": item.process_name,\n", " \"EE%\": item.embodied_energy_percentage,\n", " EE_HEADER: item.embodied_energy.value,\n", " \"CC%\": item.climate_change_percentage,\n", " CC_HEADER: item.climate_change.value,\n", " }\n", " for item in sustainability_summary.joining_and_finishing_processes_details\n", " ]\n", ")\n", "joining_and_finishing_processes_df" ] }, { "cell_type": "code", "execution_count": null, "id": "60176bd2", "metadata": {}, "outputs": [], "source": [ "plot_impact(joining_and_finishing_processes_df, \"Aggregated joining and finishing processes impact\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }