diff --git a/examples/python_large_output/download_output_file.py b/examples/python_large_output/download_output_file.py new file mode 100644 index 00000000..24953afd --- /dev/null +++ b/examples/python_large_output/download_output_file.py @@ -0,0 +1,93 @@ +# Copyright (C) 2022 - 2024 ANSYS, Inc. and/or its affiliates. +# SPDX-License-Identifier: MIT +# +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +""" +Example to query resources from a project. + +- Query values from evaluated jobs, computing some simple statistics on parameter values. +- Download files from the project + +""" +import argparse +import logging +import os +import time + +from ansys.hps.client import Client, HPSError +from ansys.hps.client.jms import JmsApi, ProjectApi + +log = logging.getLogger(__name__) + + +def download_files(client, project_name): + """Download files.""" + out_path = os.path.join(os.path.dirname(__file__), "downloads") + log.info(f"Downloading files to {out_path}") + + jms_api = JmsApi(client) + project = jms_api.get_project_by_name(name=project_name) + project = jms_api.get_project(id=project.id) + + log.info(f"Project id: {project.id}") + project_api = ProjectApi(client, project.id) + + jobs = project_api.get_jobs(eval_status="evaluated", fields=["id", "values", "elapsed_time"]) + log.info(f"# evaluated jobs: {len(jobs)}") + num = len(jobs) + + log.info( + f"=== Example 1: Downloading output files of {num} jobs using ProjectApi.download_file()" + ) + for job in jobs[0:num]: + log.info(f"Job {job.id}") + for task in project_api.get_tasks(job_id=job.id): + log.info(f"Task {task.id}") + files = project_api.get_files(id=task.output_file_ids) + for f in files: + fpath = os.path.join(out_path, f"task_{task.id}") + log.info(f"Download output file {f.evaluation_path} to {fpath}") + start = time.process_time() + project_api.download_file(file=f, target_path=fpath) + log.info(f"Time taken to download output file: {(time.time() - start):.2f} seconds" + ) + +if __name__ == "__main__": + + parser = argparse.ArgumentParser() + parser.add_argument("-n", "--name", type=str, default="Python Large Output Files") + parser.add_argument("-U", "--url", default="https://127.0.0.1:8443/hps") + parser.add_argument("-u", "--username", default="repuser") + parser.add_argument("-p", "--password", default="repuser") + args = parser.parse_args() + + logger = logging.getLogger() + logging.basicConfig(format="%(message)s", level=logging.DEBUG) + + try: + log.info("Connect to HPC Platform Services") + client = Client(url=args.url, username=args.username, password=args.password) + log.info(f"HPS URL: {client.url}") + + download_files(client=client, project_name=args.name) + + except HPSError as e: + log.error(str(e)) \ No newline at end of file diff --git a/examples/python_large_output/project_setup.py b/examples/python_large_output/project_setup.py index de1b2810..c79ec844 100644 --- a/examples/python_large_output/project_setup.py +++ b/examples/python_large_output/project_setup.py @@ -46,12 +46,17 @@ log = logging.getLogger(__name__) -def main(client, use_exec_script, python_version=None) -> Project: +def create_project(client, name, use_exec_script, python_version=None) -> Project: """ Create project that runs a Python script to generate a large output file. + + After creating the project, 7 job definitions are created each meant to create + a Large download file of variable size in GB. + + Download files from the project """ log.debug("=== Project") - proj = Project(name="Python Large Output Files", priority=1, active=True) + proj = Project(name=name, priority=1, active=True) jms_api = JmsApi(client) proj = jms_api.create_project(proj, replace=True) project_api = ProjectApi(client, proj.id) @@ -169,6 +174,7 @@ def main(client, use_exec_script, python_version=None) -> Project: if __name__ == "__main__": parser = argparse.ArgumentParser() + parser.add_argument("-n", "--name", type=str, default="Python Large Output Files") parser.add_argument("-U", "--url", default="https://127.0.0.1:8443/hps") parser.add_argument("-u", "--username", default="repuser") parser.add_argument("-p", "--password", default="repuser") @@ -182,6 +188,6 @@ def main(client, use_exec_script, python_version=None) -> Project: client = Client(url=args.url, username=args.username, password=args.password) try: - main(client, use_exec_script=args.use_exec_script, python_version=args.python_version) + create_project(client, name=args.name, use_exec_script=args.use_exec_script, python_version=args.python_version) except HPSError as e: log.error(str(e)) diff --git a/tests/test_examples.py b/tests/test_examples.py index 10ecc595..5e82a0cf 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -274,6 +274,27 @@ def test_cfx_static_mixer(client): jms_api.delete_project(project) +def test_python_large_output(client): + + from examples.python_large_output.project_setup import create_project + + project = create_project( + client, name="Python Large Output Files test", use_exec_script=True, python_version=3.10 + ) + assert project is not None + + jms_api = JmsApi(client) + project_api = ProjectApi(client, project.id) + + assert len(project_api.get_jobs()) == 7 + jms_api.get_project(id=project.id).name == "Python Large Output Files test" + + job = project_api.get_jobs()[0] + assert job.name == "Job 1 GB" + + jms_api.delete_project(project) + + def test_python_multi_steps(client): from examples.python_multi_process_step.project_setup import main as create_project