.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/02_get_results_workflow.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_02_get_results_workflow.py: Get Results workflow ==================== This example shows how to use PyConcentEV to get a list of result parameters from a list of design_instance_ids These lists are provided in design_instance_ids.csv .. warning:: Assumes a front and rear motor architecture. Assumes a specific drive cycle result is available. If short results is true it gets in User Units Assumes first job in list for each project is the result we want. .. GENERATED FROM PYTHON SOURCE LINES 38-41 Perform required imports ------------------------ Get the necessary imports. .. GENERATED FROM PYTHON SOURCE LINES 41-54 .. code-block:: Python import datetime import json import time import matplotlib.pyplot as plt import pandas as pd from ansys.conceptev.core import app, auth from ansys.conceptev.core.settings import settings OCM_URL = settings.ocm_url .. GENERATED FROM PYTHON SOURCE LINES 55-68 Inputs ------ Change the following variables to match your data. .. warning: Setting short results to False. This may take a long time to get results of the server which may result in an error. Consider setting short_results to True for large drive cycles. .. note: Get Results off server this may take time depending on number of files needed to download. Recommendation: Run once then set get_results_off_server to False. This allows faster iteration of improving the Excel output. .. GENERATED FROM PYTHON SOURCE LINES 68-73 .. code-block:: Python short_results = True # For results created after 15/11/2024 improved performance. get_results_off_server = True # Generates an output file that can be read later. output_filename = "results.xlsx" # Output filename for results. .. GENERATED FROM PYTHON SOURCE LINES 74-78 Generate and run templates -------------------------- This function generates a new project and runs a template for each design_instance_id The design_instance_ids are returned for later use. .. GENERATED FROM PYTHON SOURCE LINES 78-102 .. code-block:: Python def generate_and_run_templates(client, account_id, hpc_id): project_id = app.create_new_project( client, account_id, hpc_id, f"New Project {datetime.datetime.now()}" ) template_ids = ["ae7ca4d7-4bac-48f5-be42-d5f0b6a24b00"] design_instance_ids = [] for template_id in template_ids: design_instance_id = app.create_design_instance( project_id["projectId"], f"New Concept {datetime.datetime.now()}", token ) concept = app.copy_concept(template_id, design_instance_id, client) job_info = app.create_submit_job( client, concept, account_id, hpc_id, job_name=f"cli_job: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')}", ) design_instance_ids.append(design_instance_id) return design_instance_ids .. GENERATED FROM PYTHON SOURCE LINES 103-109 Create a token and set up projects. ----------------------------------- This code creates a token. Creates a client with the token Gets the account_id and hpc_id Generates and runs templates .. GENERATED FROM PYTHON SOURCE LINES 109-119 .. code-block:: Python msal_app = auth.create_msal_app() token = auth.get_ansyId_token(msal_app) with app.get_http_client(token) as client: account_id = app.get_account_id(token) hpc_id = app.get_default_hpc(token, account_id) design_instance_ids = generate_and_run_templates(client, account_id, hpc_id) .. rst-class:: sphx-glr-script-out .. code-block:: none Encryption unavailable. Opting in to plain text. Traceback (most recent call last): File "/home/runner/work/pyconceptev/pyconceptev/.venv/lib/python3.13/site-packages/msal_extensions/libsecret.py", line 18, in import gi # https://github.com/AzureAD/microsoft-authentication-extensions-for-python/wiki/Encryption-on-Linux # pylint: disable=line-too-long ^^^^^^^^^ ModuleNotFoundError: No module named 'gi' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/runner/work/pyconceptev/pyconceptev/src/ansys/conceptev/core/auth.py", line 51, in build_persistence return build_encrypted_persistence(location) File "/home/runner/work/pyconceptev/pyconceptev/.venv/lib/python3.13/site-packages/msal_extensions/persistence.py", line 98, in build_encrypted_persistence return LibsecretPersistence(location) File "/home/runner/work/pyconceptev/pyconceptev/.venv/lib/python3.13/site-packages/msal_extensions/persistence.py", line 314, in __init__ from .libsecret import ( # This uncertain import is deferred till runtime LibSecretAgent, trial_run) File "/home/runner/work/pyconceptev/pyconceptev/.venv/lib/python3.13/site-packages/msal_extensions/libsecret.py", line 20, in raise ImportError("""Unable to import module 'gi' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<5 lines>... """) # Message via exception rather than log ^^^^ ImportError: Unable to import module 'gi' Runtime dependency of PyGObject is missing. Depends on your Linux distro, you could install it system-wide by something like: sudo apt install python3-gi python3-gi-cairo gir1.2-secret-1 If necessary, please refer to PyGObject's doc: https://pygobject.readthedocs.io/en/latest/getting_started.html .. GENERATED FROM PYTHON SOURCE LINES 120-123 Get the project results ----------------------------------- This code gets the project results from the ConceptEV API. .. GENERATED FROM PYTHON SOURCE LINES 123-152 .. code-block:: Python def get_project_results(client, design_instance_id, token): """Get the project results from ConceptEV API. Assumes the first results only. """ client.params = {"design_instance_id": design_instance_id} concept = app.get(client, "/concepts", id=design_instance_id) job_id = concept["jobs_ids"][0] # get ONLY the first id from each project. job_info = app.get_job_info(token, job_id) results = app.read_results( client, job_info, calculate_units=short_results, filtered=short_results ) # Need to get, Project Name, Component Name, Cost arch_id = concept["architecture_id"] architecture = app.get(client, f"/architectures/{arch_id}") project_results = { "architecture": architecture, "cost": architecture["components_cost"], "design_instance_id": design_instance_id, "component_map": app.get_component_id_map(client, design_instance_id), "design_name": app.get_design_title(token, design_instance_id), "results": results, } return project_results .. GENERATED FROM PYTHON SOURCE LINES 153-156 Get the project results from a list of design_instance_ids ---------------------------------------------------------- This code gets the project results from a list of design instance ids. .. GENERATED FROM PYTHON SOURCE LINES 156-172 .. code-block:: Python def get_results(design_instance_ids): """Get results from a list of design instance ids.""" msal_app = auth.create_msal_app() token = auth.get_ansyId_token(msal_app) with app.get_http_client(token) as client: client.timeout = 2000 project_results = [ get_project_results(client, design_instance_id, token) for design_instance_id in design_instance_ids ] return project_results .. GENERATED FROM PYTHON SOURCE LINES 173-176 Get component name from project results. ---------------------------------------------------------- This code gets the component name from the project results. .. GENERATED FROM PYTHON SOURCE LINES 176-184 .. code-block:: Python def get_component_name(project_result, name): """Get Component Name or returns empty string.""" name = project_result["component_map"].get(project_result["architecture"][name], "") return name .. GENERATED FROM PYTHON SOURCE LINES 185-188 Get results for a drive cycle requirement. ---------------------------------------------------------- Get the results for a drive cycle requirement. .. GENERATED FROM PYTHON SOURCE LINES 188-198 .. code-block:: Python def get_drive_cycle_result(project_result, name): """Assumes the first requirement in a concept with the matching name is the one we want.""" requirement = [ result for result in project_result["results"] if result["requirement"]["name"] == name ][0] return requirement .. GENERATED FROM PYTHON SOURCE LINES 199-202 Getting all the results. ---------------------------------------------------------- First get results of the server or load from file. .. GENERATED FROM PYTHON SOURCE LINES 202-212 .. code-block:: Python if get_results_off_server: time.sleep(20) # Wait for the server to process the results. project_results = get_results(design_instance_ids) # move to api? or file export mode? with open("project_results.json", "w") as f: json.dump(project_results, f) else: with open("project_results.json", "r") as f: project_results = json.load(f) .. rst-class:: sphx-glr-script-out .. code-block:: none Encryption unavailable. Opting in to plain text. Traceback (most recent call last): File "/home/runner/work/pyconceptev/pyconceptev/.venv/lib/python3.13/site-packages/msal_extensions/libsecret.py", line 18, in import gi # https://github.com/AzureAD/microsoft-authentication-extensions-for-python/wiki/Encryption-on-Linux # pylint: disable=line-too-long ^^^^^^^^^ ModuleNotFoundError: No module named 'gi' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/runner/work/pyconceptev/pyconceptev/src/ansys/conceptev/core/auth.py", line 51, in build_persistence return build_encrypted_persistence(location) File "/home/runner/work/pyconceptev/pyconceptev/.venv/lib/python3.13/site-packages/msal_extensions/persistence.py", line 98, in build_encrypted_persistence return LibsecretPersistence(location) File "/home/runner/work/pyconceptev/pyconceptev/.venv/lib/python3.13/site-packages/msal_extensions/persistence.py", line 314, in __init__ from .libsecret import ( # This uncertain import is deferred till runtime LibSecretAgent, trial_run) File "/home/runner/work/pyconceptev/pyconceptev/.venv/lib/python3.13/site-packages/msal_extensions/libsecret.py", line 20, in raise ImportError("""Unable to import module 'gi' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<5 lines>... """) # Message via exception rather than log ^^^^ ImportError: Unable to import module 'gi' Runtime dependency of PyGObject is missing. Depends on your Linux distro, you could install it system-wide by something like: sudo apt install python3-gi python3-gi-cairo gir1.2-secret-1 If necessary, please refer to PyGObject's doc: https://pygobject.readthedocs.io/en/latest/getting_started.html Connected to OCM Websockets. Status:PHASE_PROGRESS Progress:0.013953488372093023 Status:PHASE_PROGRESS Progress:0.027906976744186046 Status:PHASE_PROGRESS Progress:0.04186046511627907 Status:PHASE_PROGRESS Progress:0.05581395348837209 Status:PHASE_PROGRESS Progress:0.06976744186046512 Status:PHASE_PROGRESS Progress:0.08372093023255814 Status:PHASE_PROGRESS Progress:0.09767441860465116 Status:PHASE_PROGRESS Progress:0.11162790697674418 Status:PHASE_PROGRESS Progress:0.12558139534883722 Status:PHASE_PROGRESS Progress:0.13953488372093023 Progress:0.15348837209302327 Status:PHASE_PROGRESS Progress:0.16744186046511628 Status:PHASE_PROGRESS Status:PHASE_PROGRESS Progress:0.1813953488372093 Status:PHASE_PROGRESS Progress:0.19534883720930232 Status:PHASE_PROGRESS Progress:0.20930232558139536 Status:PHASE_PROGRESS Progress:0.22325581395348837 Status:PHASE_PROGRESS Progress:0.2372093023255814 Status:PHASE_PROGRESS Progress:0.25116279069767444 Progress:0.2651162790697674 Status:PHASE_PROGRESS Status:PHASE_PROGRESS Progress:0.27906976744186046 Progress:0.2930232558139535 Status:PHASE_PROGRESS Status:PHASE_PROGRESS Progress:0.30697674418604654 Progress:0.3209302325581395 Status:PHASE_PROGRESS Status:PHASE_PROGRESS Progress:0.33488372093023255 Status:PHASE_PROGRESS Progress:0.3488372093023256 Status:PHASE_PROGRESS Progress:0.3627906976744186 Progress:0.3767441860465116 Status:PHASE_PROGRESS Status:PHASE_PROGRESS Progress:0.39069767441860465 Status:PHASE_PROGRESS Progress:0.4046511627906977 Progress:0.4186046511627907 Status:PHASE_PROGRESS Progress:0.4325581395348837 Status:PHASE_PROGRESS Progress:0.44651162790697674 Status:PHASE_PROGRESS Status:PHASE_PROGRESS Progress:0.4604651162790698 Status:PHASE_PROGRESS Progress:0.4744186046511628 Status:PHASE_PROGRESS Progress:0.4883720930232558 Status:PHASE_PROGRESS Progress:0.5023255813953489 Status:PHASE_PROGRESS Progress:0.5162790697674419 Status:PHASE_PROGRESS Progress:0.5302325581395348 Status:PHASE_PROGRESS Progress:0.5441860465116279 Status:PHASE_PROGRESS Progress:0.5581395348837209 Status:PHASE_PROGRESS Progress:0.5720930232558139 Status:PHASE_PROGRESS Progress:0.586046511627907 Status:PHASE_PROGRESS Progress:0.6 Progress:0.6139534883720931 Status:PHASE_PROGRESS Status:PHASE_PROGRESS Progress:0.627906976744186 Status:PHASE_PROGRESS Progress:0.641860465116279 Status:PHASE_PROGRESS Progress:0.6558139534883721 Status:PHASE_PROGRESS Progress:0.6697674418604651 Status:PHASE_PROGRESS Progress:0.6837209302325581 Status:PHASE_PROGRESS Progress:0.6976744186046512 Status:PHASE_PROGRESS Progress:0.7116279069767442 Status:PHASE_PROGRESS Progress:0.7255813953488373 Progress:0.7395348837209302 Status:PHASE_PROGRESS Status:PHASE_PROGRESS Progress:0.7534883720930232 Status:PHASE_PROGRESS Progress:0.7674418604651163 Status:PHASE_PROGRESS Progress:0.7813953488372093 Status:PHASE_PROGRESS Progress:0.7953488372093023 Status:PHASE_PROGRESS Progress:0.8093023255813954 Status:PHASE_PROGRESS Progress:0.8232558139534883 Progress:0.8372093023255814 Status:PHASE_PROGRESS Status:PHASE_PROGRESS Progress:0.8511627906976744 Status:PHASE_PROGRESS Progress:0.8651162790697674 Status:PHASE_PROGRESS Progress:0.8790697674418605 Status:PHASE_PROGRESS Progress:0.8930232558139535 Status:PHASE_PROGRESS Progress:0.9069767441860465 Status:PHASE_PROGRESS Progress:0.9209302325581395 Status:PHASE_PROGRESS Progress:0.9348837209302325 Status:PHASE_PROGRESS Progress:0.9488372093023256 Status:PHASE_PROGRESS Progress:0.9627906976744186 Status:PHASE_PROGRESS Progress:0.9767441860465116 Status:PHASE_PROGRESS Progress:0.9906976744186047 Status:PHASE_DONE Status:uploading Status:PHASE_PROGRESS Status:PHASE_PROGRESS Status:PHASE_DONE Status:complete Encryption unavailable. Opting in to plain text. Traceback (most recent call last): File "/home/runner/work/pyconceptev/pyconceptev/.venv/lib/python3.13/site-packages/msal_extensions/libsecret.py", line 18, in import gi # https://github.com/AzureAD/microsoft-authentication-extensions-for-python/wiki/Encryption-on-Linux # pylint: disable=line-too-long ^^^^^^^^^ ModuleNotFoundError: No module named 'gi' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/runner/work/pyconceptev/pyconceptev/src/ansys/conceptev/core/auth.py", line 51, in build_persistence return build_encrypted_persistence(location) File "/home/runner/work/pyconceptev/pyconceptev/.venv/lib/python3.13/site-packages/msal_extensions/persistence.py", line 98, in build_encrypted_persistence return LibsecretPersistence(location) File "/home/runner/work/pyconceptev/pyconceptev/.venv/lib/python3.13/site-packages/msal_extensions/persistence.py", line 314, in __init__ from .libsecret import ( # This uncertain import is deferred till runtime LibSecretAgent, trial_run) File "/home/runner/work/pyconceptev/pyconceptev/.venv/lib/python3.13/site-packages/msal_extensions/libsecret.py", line 20, in raise ImportError("""Unable to import module 'gi' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<5 lines>... """) # Message via exception rather than log ^^^^ ImportError: Unable to import module 'gi' Runtime dependency of PyGObject is missing. Depends on your Linux distro, you could install it system-wide by something like: sudo apt install python3-gi python3-gi-cairo gir1.2-secret-1 If necessary, please refer to PyGObject's doc: https://pygobject.readthedocs.io/en/latest/getting_started.html .. GENERATED FROM PYTHON SOURCE LINES 213-219 Output the results to Excel File. ---------------------------------------------------------- Create an output results list to store outputs. Loop through all results. Get the data we want to output. Place in a records list for each row of Excel file. .. GENERATED FROM PYTHON SOURCE LINES 219-262 .. code-block:: Python output_results = [] for ( project_result ) in project_results: # For each project results get the data we want to output into the row. # Parse the results we are interested in. steady_drive = get_drive_cycle_result(project_result, "180 km/h") # Get the Component Names for each of components we are interested in. front_tranmsission_name = ( get_component_name(project_result, "front_transmission_id") + " (Front)" ) front_motor_name = get_component_name(project_result, "front_motor_id") + " (Front)" front_inverter_name = get_component_name(project_result, "front_inverter_id") + " (Front)" front_disconnect_clutch_name = ( get_component_name(project_result, "front_clutch_id") + " (Front)" ) rear_transmission_name = get_component_name(project_result, "rear_transmission_id") + " (Rear)" rear_motor_name = get_component_name(project_result, "rear_motor_id") + " (Rear)" rear_inverter_name = get_component_name(project_result, "rear_inverter_id") + " (Rear)" rear_disconnect_clutch_name = get_component_name(project_result, "rear_clutch_id") + " (Rear)" battery_name = get_component_name(project_result, "battery_id") # Creating a records list for each row. output_results.append( { "Project Name": project_result["design_name"], "Design Instance Id": project_result["design_instance_id"], "Front Transmission": front_tranmsission_name, "Front Motor": front_motor_name, "Front Inverter": front_inverter_name, "Rear Transmission": rear_inverter_name, "Rear Motor": rear_motor_name, "Rear Inverter": rear_inverter_name, "Battery": battery_name, "Cost": project_result["cost"], "total_tractive_power": steady_drive["requirement"]["total_tractive_power"], # Extend as required to add the data you need. } ) .. GENERATED FROM PYTHON SOURCE LINES 263-267 Output the results to Excel File. ---------------------------------------------------------- Convert the output results to a pandas DataFrame. Output the results to an Excel file. .. GENERATED FROM PYTHON SOURCE LINES 267-273 .. code-block:: Python all_results = pd.DataFrame(output_results) # Convert to Pandas DataFrame plt.plot(all_results["Cost"], all_results["total_tractive_power"], "*") # Plot the results. plt.show() all_results.to_excel(output_filename) # Output to excel. .. image-sg:: /auto_examples/images/sphx_glr_02_get_results_workflow_001.png :alt: 02 get results workflow :srcset: /auto_examples/images/sphx_glr_02_get_results_workflow_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (5 minutes 41.686 seconds) .. _sphx_glr_download_auto_examples_02_get_results_workflow.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 02_get_results_workflow.ipynb <02_get_results_workflow.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 02_get_results_workflow.py <02_get_results_workflow.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 02_get_results_workflow.zip <02_get_results_workflow.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_