Skip to content

Commit c07e55f

Browse files
committed
Add download files to example Python Large Output Files
1 parent 28fa37e commit c07e55f

File tree

3 files changed

+123
-3
lines changed

3 files changed

+123
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright (C) 2022 - 2024 ANSYS, Inc. and/or its affiliates.
2+
# SPDX-License-Identifier: MIT
3+
#
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
"""
24+
Example to query resources from a project.
25+
26+
- Query values from evaluated jobs, computing some simple statistics on parameter values.
27+
- Download files from the project
28+
29+
"""
30+
import argparse
31+
import logging
32+
import os
33+
import time
34+
35+
from ansys.hps.client import Client, HPSError
36+
from ansys.hps.client.jms import JmsApi, ProjectApi
37+
38+
log = logging.getLogger(__name__)
39+
40+
41+
def download_files(client, project_name):
42+
"""Download files."""
43+
out_path = os.path.join(os.path.dirname(__file__), "downloads")
44+
log.info(f"Downloading files to {out_path}")
45+
46+
jms_api = JmsApi(client)
47+
project = jms_api.get_project_by_name(name=project_name)
48+
project = jms_api.get_project(id=project.id)
49+
50+
log.info(f"Project id: {project.id}")
51+
project_api = ProjectApi(client, project.id)
52+
53+
jobs = project_api.get_jobs(eval_status="evaluated", fields=["id", "values", "elapsed_time"])
54+
log.info(f"# evaluated jobs: {len(jobs)}")
55+
num = len(jobs)
56+
57+
log.info(
58+
f"=== Example 1: Downloading output files of {num} jobs using ProjectApi.download_file()"
59+
)
60+
for job in jobs[0:num]:
61+
log.info(f"Job {job.id}")
62+
for task in project_api.get_tasks(job_id=job.id):
63+
log.info(f"Task {task.id}")
64+
files = project_api.get_files(id=task.output_file_ids)
65+
for f in files:
66+
fpath = os.path.join(out_path, f"task_{task.id}")
67+
log.info(f"Download output file {f.evaluation_path} to {fpath}")
68+
start = time.process_time()
69+
project_api.download_file(file=f, target_path=fpath)
70+
log.info(f"Time taken to download output file: {(time.time() - start):.2f} seconds"
71+
)
72+
73+
if __name__ == "__main__":
74+
75+
parser = argparse.ArgumentParser()
76+
parser.add_argument("-n", "--name", type=str, default="Python Large Output Files")
77+
parser.add_argument("-U", "--url", default="https://127.0.0.1:8443/hps")
78+
parser.add_argument("-u", "--username", default="repuser")
79+
parser.add_argument("-p", "--password", default="repuser")
80+
args = parser.parse_args()
81+
82+
logger = logging.getLogger()
83+
logging.basicConfig(format="%(message)s", level=logging.DEBUG)
84+
85+
try:
86+
log.info("Connect to HPC Platform Services")
87+
client = Client(url=args.url, username=args.username, password=args.password)
88+
log.info(f"HPS URL: {client.url}")
89+
90+
download_files(client=client, project_name=args.name)
91+
92+
except HPSError as e:
93+
log.error(str(e))

examples/python_large_output/project_setup.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,17 @@
4646
log = logging.getLogger(__name__)
4747

4848

49-
def main(client, use_exec_script, python_version=None) -> Project:
49+
def create_project(client, name, use_exec_script, python_version=None) -> Project:
5050
"""
5151
Create project that runs a Python script to generate a large output file.
52+
53+
After creating the project, 7 job definitions are created each meant to create
54+
a Large download file of variable size in GB.
55+
56+
Download files from the project
5257
"""
5358
log.debug("=== Project")
54-
proj = Project(name="Python Large Output Files", priority=1, active=True)
59+
proj = Project(name=name, priority=1, active=True)
5560
jms_api = JmsApi(client)
5661
proj = jms_api.create_project(proj, replace=True)
5762
project_api = ProjectApi(client, proj.id)
@@ -169,6 +174,7 @@ def main(client, use_exec_script, python_version=None) -> Project:
169174

170175
if __name__ == "__main__":
171176
parser = argparse.ArgumentParser()
177+
parser.add_argument("-n", "--name", type=str, default="Python Large Output Files")
172178
parser.add_argument("-U", "--url", default="https://127.0.0.1:8443/hps")
173179
parser.add_argument("-u", "--username", default="repuser")
174180
parser.add_argument("-p", "--password", default="repuser")
@@ -182,6 +188,6 @@ def main(client, use_exec_script, python_version=None) -> Project:
182188
client = Client(url=args.url, username=args.username, password=args.password)
183189

184190
try:
185-
main(client, use_exec_script=args.use_exec_script, python_version=args.python_version)
191+
create_project(client, name=args.name, use_exec_script=args.use_exec_script, python_version=args.python_version)
186192
except HPSError as e:
187193
log.error(str(e))

tests/test_examples.py

+21
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,27 @@ def test_cfx_static_mixer(client):
274274
jms_api.delete_project(project)
275275

276276

277+
def test_python_large_output(client):
278+
279+
from examples.python_large_output.project_setup import create_project
280+
281+
project = create_project(
282+
client, name="Python Large Output Files test", use_exec_script=True, python_version=3.10
283+
)
284+
assert project is not None
285+
286+
jms_api = JmsApi(client)
287+
project_api = ProjectApi(client, project.id)
288+
289+
assert len(project_api.get_jobs()) == 7
290+
jms_api.get_project(id=project.id).name == "Python Large Output Files test"
291+
292+
job = project_api.get_jobs()[0]
293+
assert job.name == "Job 1 GB"
294+
295+
jms_api.delete_project(project)
296+
297+
277298
def test_python_multi_steps(client):
278299

279300
from examples.python_multi_process_step.project_setup import main as create_project

0 commit comments

Comments
 (0)