Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added explicit download file functions #384

Merged
merged 2 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements/core.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ shapely<=1.8.0,>=1.7.0
descartes
pydantic>=1.9.0
PyYAML
boto3
boto3==1.23.1
requests
dask
gdspy
22 changes: 22 additions & 0 deletions tests/_test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ def test_webapi_6_load():
_ = sim_data[first_monitor_name]


@clear_tmp
def test_webapi_7_download_json():
"""download the simulation data"""
task_id = _get_gloabl_task_id()
web.download_json(task_id)


@clear_tmp
def test_webapi_8_download_log():
"""download the simulation data"""
task_id = _get_gloabl_task_id()
web.download_log(task_id)


@clear_tmp
def test_webapi_9_load_simulation():
"""download the simulation data"""
task_id = _get_gloabl_task_id()
sim = web.load_simulation(task_id)
assert sim == sim_original


# For some reason this test interferes with tests 5 and 6
# def test_webapi_7_delete():
# """test that we can monitor task"""
Expand Down
2 changes: 1 addition & 1 deletion tidy3d/web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
import sys

from .webapi import run, upload, get_info, start, monitor, delete, download, load, estimate_cost
from .webapi import get_tasks, delete_old
from .webapi import get_tasks, delete_old, download_json, download_log, load_simulation
from .container import Job, Batch
from .auth import get_credentials
67 changes: 57 additions & 10 deletions tidy3d/web/webapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,21 +318,59 @@ def download(task_id: TaskId, path: str = "simulation_data.hdf5") -> None:
path : str = "simulation_data.hdf5"
Download path to .hdf5 data file (including filename).

Note
----
To load downloaded results into data, call :meth:`load` with option `replace_existing=False`.
"""

# TODO: it should be possible to load "diverged" simulations
task_info = get_info(task_id)
if task_info.status in ("error", "deleted"):
raise WebError(f"can't download task '{task_id}', status = '{task_info.status}'")
_download_file(task_id, fname="monitor_data.hdf5", path=path)

directory, _ = os.path.split(path)
if directory != "":
os.makedirs(directory, exist_ok=True)

_download_file(task_id, fname="monitor_data.hdf5", path=path)
def download_json(task_id: TaskId, path: str = "simulation.json") -> None:
"""Download the `.json` file associated with the :class:`.Simulation` of a given task.

Parameters
----------
task_id : str
Unique identifier of task on server. Returned by :meth:`upload`.
path : str = "simulation.json"
Download path to .json file of simulation (including filename).
"""
_download_file(task_id, fname="simulation.json", path=path)


def load_simulation(task_id: TaskId, path: str = "simulation.json") -> Simulation:
"""Download the `.json` file of a task and load the associated :class:`.Simulation`.

Parameters
----------
task_id : str
Unique identifier of task on server. Returned by :meth:`upload`.
path : str = "simulation.json"
Download path to .json file of simulation (including filename).

Returns
-------
:class:`.Simulation`
Simulation loaded from downloaded json file.
"""
download_json(task_id, path=path)
return Simulation.from_file(path)


def download_log(task_id: TaskId, path: str = "tidy3d.log") -> None:
"""Download the tidy3d log file associated with a task.

Parameters
----------
task_id : str
Unique identifier of task on server. Returned by :meth:`upload`.
path : str = "tidy3d.log"
Download path to log file (including filename).

Note
----
To load downloaded results into data, call :meth:`load` with option `replace_existing=False`.
"""
_download_file(task_id, fname="tidy3d.log", path=path)


def load(
Expand Down Expand Up @@ -534,6 +572,15 @@ def _download_file(task_id: TaskId, fname: str, path: str) -> None:
path : str
Path where the file will be downloaded to (including filename).
"""

task_info = get_info(task_id)
if task_info.status in ("error", "deleted"):
raise WebError(f"can't download task '{task_id}', status = '{task_info.status}'")

directory, _ = os.path.split(path)
if directory != "":
os.makedirs(directory, exist_ok=True)

log.info(f'downloading file "{fname}" to "{path}"')

try:
Expand Down