Skip to content

Commit

Permalink
Add download files to example Python Large Output Files
Browse files Browse the repository at this point in the history
  • Loading branch information
saimanikant committed Nov 28, 2024
1 parent 28fa37e commit c07e55f
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 3 deletions.
93 changes: 93 additions & 0 deletions examples/python_large_output/download_output_file.py
Original file line number Diff line number Diff line change
@@ -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))
12 changes: 9 additions & 3 deletions examples/python_large_output/project_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -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))
21 changes: 21 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c07e55f

Please sign in to comment.