Skip to content

Commit de28097

Browse files
committed
update cli for serve/process, add test
1 parent 526ceff commit de28097

File tree

5 files changed

+52
-40
lines changed

5 files changed

+52
-40
lines changed

pyhdx/batch_processing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class JobParser(object):
237237

238238
cwd = param.ClassSelector(Path, doc='Path of the current working directory')
239239

240-
def __init__(self, job_spec, cwd=None, ):
240+
def __init__(self, job_spec, cwd=None):
241241
self.job_spec = job_spec
242242
self.cwd = cwd or Path().cwd()
243243

pyhdx/cli.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
1-
import argparse
21
import time
3-
from ipaddress import ip_address
4-
from pyhdx.web import serve
5-
from pyhdx.config import cfg
6-
from pyhdx.local_cluster import verify_cluster, default_cluster
2+
from typing import Union, Optional
3+
from pathlib import Path
74

5+
import typer
6+
from ipaddress import ip_address
7+
import yaml
88

9-
# todo add check to see if the web module requirements are installed
109

10+
app = typer.Typer()
1111

12-
def main():
13-
parser = argparse.ArgumentParser(prog="pyhdx", description="PyHDX Launcher")
12+
@app.command()
13+
def serve(scheduler_address: Optional[str] = typer.Option(None, help="Address for dask scheduler to use")):
14+
"""Launch the PyHDX web application"""
1415

15-
parser.add_argument("serve", help="Runs PyHDX Dashboard")
16-
parser.add_argument(
17-
"--scheduler_address", help="Run with local cluster <ip>:<port>"
18-
)
19-
args = parser.parse_args()
16+
from pyhdx.config import cfg
17+
from pyhdx.local_cluster import verify_cluster, default_cluster
2018

21-
if args.scheduler_address:
22-
ip, port = args.scheduler_address.split(":")
19+
if scheduler_address is not None:
20+
ip, port = scheduler_address.split(":")
2321
if not ip_address(ip):
2422
print("Invalid IP Address")
2523
return
2624
elif not 0 <= int(port) < 2 ** 16:
2725
print("Invalid port, must be 0-65535")
2826
return
29-
cfg.set("cluster", "scheduler_address", args.scheduler_address)
27+
cfg.set("cluster", "scheduler_address", scheduler_address)
3028

3129
scheduler_address = cfg.get("cluster", "scheduler_address")
3230
if not verify_cluster(scheduler_address):
@@ -37,8 +35,9 @@ def main():
3735
scheduler_address = f"{ip}:{port}"
3836
print(f"Started new Dask LocalCluster at {scheduler_address}")
3937

40-
if args.serve:
41-
serve.run_apps()
38+
# Start the PyHDX web application
39+
from pyhdx.web import serve as serve_pyhdx
40+
serve_pyhdx.run_apps()
4241

4342
loop = True
4443
while loop:
@@ -49,11 +48,22 @@ def main():
4948
loop = False
5049

5150

52-
if __name__ == "__main__":
53-
import sys
51+
@app.command()
52+
def process(
53+
jobfile: Path = typer.Argument(..., help="Path to .yaml jobfile"),
54+
cwd: Optional[Path] = typer.Option(None, help="Optional path to working directory")
55+
):
56+
"""
57+
Process a HDX dataset according to a jobfile
58+
"""
59+
60+
from pyhdx.batch_processing import JobParser
5461

55-
sys.argv.append("serve")
56-
sys.argv.append("--scheduler_address")
57-
sys.argv.append("127.0.0.1:53270")
62+
job_spec = yaml.safe_load(jobfile.read_text())
63+
parser = JobParser(job_spec, cwd=cwd)
5864

59-
main()
65+
parser.execute()
66+
67+
68+
if __name__ == "__main__":
69+
app()

setup.cfg

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[metadata]
22
name = PyHDX
33
author = Jochem H. Smit
4-
author-email = [email protected]
4+
author_email = [email protected]
55
maintainer = Jochem H. Smit
6-
maintainer-email = [email protected]
6+
maintainer_email = [email protected]
77
url = https://github.com/Jhsmit/PyHDX
88
license = MIT
99
license_files = LICENSE
@@ -31,6 +31,7 @@ install_requires =
3131
sympy==1.5.1
3232
torch
3333
tqdm
34+
typer
3435
dask
3536
distributed
3637
packaging
@@ -41,7 +42,7 @@ python_requires =
4142

4243
[options.entry_points]
4344
console_scripts =
44-
pyhdx = pyhdx.cli:main
45+
pyhdx = pyhdx.cli:app
4546

4647

4748
[options.extras_require]

tests/test_batchprocessing.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from pyhdx.batch_processing import yaml_to_hdxm, yaml_to_hdxmset, StateParser
1+
from pyhdx.batch_processing import StateParser, JobParser
22
from pyhdx.models import HDXMeasurement, HDXMeasurementSet
33
import numpy as np
44
from pathlib import Path
55
import yaml
6+
import shutil
67

78
cwd = Path(__file__).parent
89
input_dir = cwd / 'test_data' / 'input'
@@ -17,16 +18,6 @@ def test_load_from_yaml(self):
1718
yaml_pth = Path(input_dir / 'data_states.yaml')
1819
data_dict = yaml.safe_load(yaml_pth.read_text())
1920

20-
hdxm = yaml_to_hdxm(data_dict['SecB_tetramer'], data_dir=input_dir)
21-
assert isinstance(hdxm, HDXMeasurement)
22-
23-
assert hdxm.metadata['temperature'] == data_dict['SecB_tetramer']['temperature']['value'] + 273.15
24-
assert hdxm.name == 'SecB WT apo'
25-
26-
hdxm_set = yaml_to_hdxmset(data_dict, data_dir=input_dir)
27-
assert isinstance(hdxm_set, HDXMeasurementSet)
28-
assert hdxm_set.names == list(data_dict.keys())
29-
3021
parser = StateParser(data_dict, data_src=input_dir)
3122

3223
hdxm = parser.load_hdxm('SecB_tetramer')
@@ -39,4 +30,14 @@ def test_load_from_yaml(self):
3930
assert isinstance(hdxm_set, HDXMeasurementSet)
4031
assert hdxm_set.names == list(data_dict.keys())
4132

33+
def test_load_job_parser(self):
34+
fit_output_dir = input_dir / 'fit_result_output_1'
35+
if fit_output_dir.exists():
36+
shutil.rmtree(fit_output_dir, ignore_errors=True)
37+
38+
job_spec = yaml.safe_load((input_dir / 'jobfile.yaml').read_text())
39+
parser = JobParser(job_spec, cwd=input_dir)
40+
parser.execute()
4241

42+
assert fit_output_dir.exists()
43+
shutil.rmtree(fit_output_dir, ignore_errors=True)

tests/test_data/input/jobfile.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ steps:
22
- task: load_hdxm_set
33
name: load_data
44
state_file: data_states.yaml
5-
- task: estimate_rates # todo allow specification of method etc
5+
- task: estimate_rates
66
name: rates
77
hdxm_set: $(load_data.out)
88
- task: create_guess

0 commit comments

Comments
 (0)