rowan-python 0.0.1__py3-none-any.whl → 0.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
rowan/client.py CHANGED
@@ -3,105 +3,150 @@ from __future__ import annotations
3
3
  import cctk
4
4
  import httpx
5
5
  from typing import Optional
6
- import numpy as np
7
6
  import stjames
8
7
  from dataclasses import dataclass, field
9
8
  import time
10
9
 
11
10
  import rowan
12
11
 
13
- API_URL = "https://api.rowansci.com/calculation"
12
+ API_URL = "https://api.rowansci.com"
14
13
 
15
- # cf. /opt/miniconda3/envs/openai/lib/python3.9/site-packages/openai/__init__.py
16
14
 
17
15
  @dataclass
18
16
  class Client:
19
17
  blocking: bool = True
20
18
  print: bool = True
21
19
  ping_interval: int = 5
20
+ delete_when_finished: bool = False
22
21
 
23
22
  headers: dict = field(init=False)
24
23
 
25
24
  def __post_init__(self):
26
25
  self.headers = {"X-API-Key": rowan.utils.get_api_key()}
27
26
 
27
+ if not self.blocking and self.delete_when_finished:
28
+ print("Warning: ``delete_when_finished`` has no effect when ``blocking`` is False. To delete calculations, call ``Client.delete()`` manually.")
29
+
28
30
  def compute(
29
31
  self,
30
- input_molecule: cctk.Molecule,
32
+ type: Optional[str] = "calculation",
33
+ input_mol: Optional[cctk.Molecule] = None,
34
+ input_smiles: Optional[str] = None,
31
35
  name: Optional[str] = None,
32
- folder_id: Optional[str] = None,
36
+ folder_uuid: Optional[str] = None,
37
+ engine: str = "peregrine",
33
38
  **options,
34
- ) -> stjames.Calculation | int:
35
- atomic_numbers = input_molecule.atomic_numbers.view(np.ndarray)
36
- geometry = input_molecule.geometry.view(np.ndarray)
37
-
38
- atoms = list()
39
- for i in range(input_molecule.num_atoms()):
40
- atoms.append(
41
- stjames.Atom(atomic_number=atomic_numbers[i], position=geometry[i])
42
- )
43
-
44
- molecule = stjames.Molecule(
45
- atoms=atoms,
46
- charge=input_molecule.charge,
47
- multiplicity=input_molecule.multiplicity,
48
- )
49
- settings = stjames.Settings(**options)
50
- calc = stjames.Calculation(molecules=[molecule], name=name, settings=settings)
39
+ ) -> dict | str:
40
+ if (input_mol is None) == (input_smiles is None):
41
+ raise ValueError("Must specify exactly one of ``input_smiles`` and ``input_mol``!")
42
+
43
+ if input_mol is None:
44
+ input_mol = cctk.Molecule.new_from_smiles(input_smiles)
45
+
46
+ molecule = rowan.utils.cctk_to_stjames(input_mol)
51
47
 
52
48
  with httpx.Client() as client:
53
- response = client.post(
54
- f"{API_URL}",
55
- headers=self.headers,
56
- json={
57
- "json_data": calc.model_dump(mode="json"),
58
- "folder_id": folder_id,
59
- },
60
- )
49
+ if type == "calculation":
50
+ settings = stjames.Settings(**options)
51
+ calc = stjames.Calculation(molecules=[molecule], name=name, engine=engine, settings=settings)
52
+
53
+ response = client.post(
54
+ f"{API_URL}/calculation",
55
+ headers=self.headers,
56
+ json={
57
+ "json_data": calc.model_dump(mode="json"),
58
+ "folder_uuid": folder_uuid,
59
+ },
60
+ )
61
+
62
+ elif type in ["pka", "conformers", "tautomers"]:
63
+ response = client.post(
64
+ f"{API_URL}/workflow",
65
+ headers=self.headers,
66
+ json={
67
+ "initial_molecule": molecule.model_dump(mode="json"),
68
+ "workflow_type": type,
69
+ "name": name,
70
+ "folder_uuid": folder_uuid,
71
+ "workflow_data": options,
72
+ },
73
+ )
74
+
61
75
  response.raise_for_status()
62
76
  response_dict = response.json()
63
- calc_id = response_dict["id"]
64
-
65
- if self.print:
66
- print(f"Calculation {calc_id} submitted")
67
-
68
- if self.blocking:
69
- while not response_dict["is_finished"]:
70
- time.sleep(self.ping_interval)
71
- response = client.get(f"{API_URL}/{calc_id}", headers=self.headers)
72
- response_dict = response.json()
73
- if not response.status_code == 200:
74
- raise ValueError(
75
- f"Error {response.status_code}: {response.reason}"
76
- )
77
-
78
- # we finished! get proper schema
79
- response = client.get(
80
- f"{API_URL}/{calc_id}/stjames", headers=self.headers
81
- )
77
+ calc_uuid = response_dict["uuid"]
78
+
79
+ if self.blocking:
80
+ while not self.is_finished(calc_uuid, type):
81
+ time.sleep(self.ping_interval)
82
+ result = self.get(calc_uuid, type)
83
+
84
+ if self.delete_when_finished:
85
+ self.delete(calc_uuid, type)
86
+
87
+ return result
88
+
89
+ else:
90
+ return calc_uuid
91
+
92
+ def is_finished(self, calc_uuid: str, type: str = "calculation") -> bool:
93
+ with httpx.Client() as client:
94
+ if type == "calculation":
95
+ response = client.get(f"{API_URL}/calculation/{calc_uuid}", headers=self.headers)
96
+ response.raise_for_status()
82
97
  response_dict = response.json()
98
+ status = response_dict["status"]
83
99
 
84
- if self.print:
85
- print(
86
- f"Calculation {calc_id} completed after {response_dict['elapsed']:.1f} s of CPU time"
87
- )
88
- print(
89
- f"(View the results at labs.rowansci.com/calculations/{calc_id})"
90
- )
100
+ elif type in ["pka", "conformers", "tautomers"]:
101
+ response = client.get(f"{API_URL}/workflow/{calc_uuid}", headers=self.headers)
102
+ response.raise_for_status()
103
+ response_dict = response.json()
104
+ status = response_dict["object_status"]
91
105
 
92
- return stjames.Calculation.model_validate(response_dict)
106
+ else:
107
+ raise ValueError(f"Unknown type ``{type}``!")
108
+
109
+ return status in [2, 3, 4]
110
+
111
+ def get(self, calc_uuid: str, type: str = "calculation") -> dict:
112
+ with httpx.Client() as client:
113
+ if type == "calculation":
114
+ response = client.get(f"{API_URL}/calculation/{calc_uuid}/stjames", headers=self.headers)
115
+ response.raise_for_status()
116
+ response_dict = response.json()
117
+ return response_dict
118
+
119
+ elif type in ["pka", "conformers", "tautomers"]:
120
+ response = client.get(f"{API_URL}/workflow/{calc_uuid}", headers=self.headers)
121
+ response.raise_for_status()
122
+ response_dict = response.json()
123
+ return response_dict["object_data"]
93
124
 
94
125
  else:
95
- return calc_id
126
+ raise ValueError(f"Unknown type ``{type}``!")
96
127
 
97
- def get(self, calc_id: int) -> stjames.Calculation:
128
+ def stop(self, calc_uuid: str, type: str = "calculation") -> None:
98
129
  with httpx.Client() as client:
99
- response = client.get(f"{API_URL}/{calc_id}/stjames", headers=self.headers)
100
- response.raise_for_status()
101
- response_dict = response.json()
102
- return stjames.Calculation.model_validate(response_dict)
130
+ if type == "calculation":
131
+ response = client.post(f"{API_URL}/calculation/{calc_uuid}/stop", headers=self.headers)
132
+ response.raise_for_status()
103
133
 
104
- def stop(self, calc_id: int) -> None:
134
+ elif type in ["pka", "conformers", "tautomers"]:
135
+ response = client.post(f"{API_URL}/workflow/{calc_uuid}/stop", headers=self.headers)
136
+ response.raise_for_status()
137
+
138
+ else:
139
+ raise ValueError(f"Unknown type ``{type}``!")
140
+
141
+ def delete(self, calc_uuid: str, type: str = "calculation") -> None:
105
142
  with httpx.Client() as client:
106
- response = client.post(f"{API_URL}/{calc_id}/stop", headers=self.headers)
107
- response.raise_for_status()
143
+ if type == "calculation":
144
+ response = client.delete(f"{API_URL}/calculation/{calc_uuid}", headers=self.headers)
145
+ response.raise_for_status()
146
+
147
+ elif type in ["pka", "conformers", "tautomers"]:
148
+ response = client.delete(f"{API_URL}/folder/{calc_uuid}", headers=self.headers)
149
+ response.raise_for_status()
150
+
151
+ else:
152
+ raise ValueError(f"Unknown type ``{type}``!")
rowan/utils.py CHANGED
@@ -1,4 +1,7 @@
1
1
  import os
2
+ import cctk
3
+ import stjames
4
+ import numpy as np
2
5
 
3
6
  import rowan
4
7
 
@@ -13,3 +16,18 @@ def get_api_key() -> str:
13
16
  raise ValueError(
14
17
  "No API key provided. You can set your API key using 'rowan.api_key = <API-KEY>', or you can set the environment variable ROWAN_API_KEY=<API-KEY>)."
15
18
  )
19
+
20
+
21
+ def cctk_to_stjames(molecule: cctk.Molecule) -> stjames.Molecule:
22
+ atomic_numbers = molecule.atomic_numbers.view(np.ndarray)
23
+ geometry = molecule.geometry.view(np.ndarray)
24
+
25
+ atoms = list()
26
+ for i in range(molecule.num_atoms()):
27
+ atoms.append(stjames.Atom(atomic_number=atomic_numbers[i], position=geometry[i]))
28
+
29
+ return stjames.Molecule(
30
+ atoms=atoms,
31
+ charge=molecule.charge,
32
+ multiplicity=molecule.multiplicity,
33
+ )
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) Rowan (https://rowansci.com)
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,32 @@
1
+ Metadata-Version: 2.1
2
+ Name: rowan-python
3
+ Version: 0.0.3
4
+ Summary: Rowan Python Library
5
+ Author-email: Corin Wagen <corin@rowansci.com>
6
+ Project-URL: Homepage, https://github.com/rowansci/rowan-client
7
+ Project-URL: Bug Tracker, https://github.com/rowansci/rowan-client/issues
8
+ Requires-Python: >=3.8
9
+ Description-Content-Type: text/markdown
10
+ License-File: LICENSE
11
+ Requires-Dist: cctk >=0.2.18
12
+ Requires-Dist: httpx >=0.25
13
+ Requires-Dist: numpy >=1.24
14
+ Requires-Dist: stjames >=0.0.23
15
+
16
+ # Rowan Python Library
17
+
18
+ [![pypi](https://img.shields.io/pypi/v/rowan-python.svg)](https://pypi.python.org/pypi/rowan-python)
19
+ [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v1.json)](https://github.com/charliermarsh/ruff)
20
+
21
+ The Rowan Python library provides convenient access to the Rowan API from applications written in the Python language.
22
+
23
+ ## Documentation
24
+
25
+ The documentation is available [here](https://docs.rowansci.com/python-api).
26
+
27
+
28
+ ## Issues
29
+
30
+ To report issues, please use the "Issues" tab above.
31
+
32
+ *Corin Wagen, 2023*
@@ -0,0 +1,8 @@
1
+ rowan/__init__.py,sha256=0O7Cmo2Io01Uqqx9iL8xcWW0t0YfgwBX7RKxDPXnbVU,61
2
+ rowan/client.py,sha256=m3Zoi3tKtirejRQf1YKCnu6I2KL9OZwgJNhBXKdKwQA,5632
3
+ rowan/utils.py,sha256=AVldYWowm7g1Ffs7JhJY7X85goNuFHvMUel0zXguDrk,932
4
+ rowan_python-0.0.3.dist-info/LICENSE,sha256=i7ehYBS-6gGmbTcgU4mgk28pyOx2kScJ0kcx8n7bWLM,1084
5
+ rowan_python-0.0.3.dist-info/METADATA,sha256=Bvw7gSUtk4F7F6CSU1k3o40IqNvm9gQGVrqbkQ-PaTY,1067
6
+ rowan_python-0.0.3.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
7
+ rowan_python-0.0.3.dist-info/top_level.txt,sha256=0B0BJ1GvTwD5rxpsHctrermP7PVk4SFaQb2syJpGQl8,6
8
+ rowan_python-0.0.3.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.41.3)
2
+ Generator: bdist_wheel (0.43.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,78 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: rowan-python
3
- Version: 0.0.1
4
- Summary: Rowan Python Library
5
- Author-email: Corin Wagen <corin@rowansci.com>
6
- Project-URL: Homepage, https://github.com/rowansci/rowan-client
7
- Project-URL: Bug Tracker, https://github.com/rowansci/rowan-client/issues
8
- Requires-Python: >=3.8
9
- Description-Content-Type: text/markdown
10
- Requires-Dist: cctk >=0.2.18
11
- Requires-Dist: httpx >=0.25
12
- Requires-Dist: numpy >=1.24
13
- Requires-Dist: stjames >=0.0.5
14
-
15
- # Rowan Python Library
16
-
17
- The Rowan Python library provides convenient access to the Rowan API from applications written in the Python language.
18
-
19
- ## Documentation
20
-
21
- The documentation is available [here](https://docs.rowansci.com).
22
-
23
- ## Installation
24
-
25
- To install, run `pip install rowan-python`.
26
-
27
- ## Usage
28
-
29
- Rowan can be run in either blocking (wait until job is complete) or non-blocking (don't wait) modes.
30
- Both modes require generation of an API key at [labs.rowansci.com](https://labs.rowansci.com/account/api-keys).
31
-
32
- For now, molecules are specified through [*cctk*](https://cctk.rtfd.io). Additional ways to specify molecules will be added in the future.
33
-
34
- Results are returned as dictionaries in the [*stjames*](https://github.com/rowansci/stjames) format.
35
-
36
- ### Blocking
37
- ```
38
- import cctk
39
- import rowan
40
-
41
- rowan.api_key = "rowan-sk..."
42
- client = rowan.Client()
43
-
44
- # load molecule by name (cctk can also load in a variety of file formats)
45
- molecule = cctk.Molecule.new_from_name("cyclobutane")
46
-
47
- # run calculation remotely and return result
48
- result = client.compute(molecule, name="opt cyclobutane", method="b97-3c", tasks=["optimize", "charge"])
49
- print(result)
50
- ```
51
-
52
- ### Non-Blocking
53
- ```
54
- import cctk
55
- import rowan
56
-
57
- rowan.api_key = "rowan-sk..."
58
- client = rowan.Client(blocking=False)
59
-
60
- # load molecule by name (cctk can also load in a variety of file formats)
61
- molecule = cctk.Molecule.new_from_name("cyclobutane")
62
-
63
- # start calculation and return id
64
- calc_id = client.compute(molecule, name="opt cyclobutane", method="b97-3c", tasks=["optimize", "charge"])
65
-
66
- # retrieve result (and status)
67
- result = client.get(calc_id)
68
- print(result)
69
-
70
- # alternately, cancel queued or running calculation
71
- client.stop(calc_id)
72
- ```
73
-
74
- ## Issues
75
-
76
- To report issues, please use the "Issues" tab above.
77
-
78
- *Corin Wagen, 2023*
@@ -1,7 +0,0 @@
1
- rowan/__init__.py,sha256=0O7Cmo2Io01Uqqx9iL8xcWW0t0YfgwBX7RKxDPXnbVU,61
2
- rowan/client.py,sha256=K1EgAl7yuyziX_1L_-5QpBbdtQFeRdZLis19N7i3hbQ,3622
3
- rowan/utils.py,sha256=_dZVPMBwgp02loVkNesXlJWOIG8zUKM8SlbjZq5R840,421
4
- rowan_python-0.0.1.dist-info/METADATA,sha256=M7DxYJx42a0_uc-X7nHK6K6KAg_JTflqWVC4PwhsOGI,2228
5
- rowan_python-0.0.1.dist-info/WHEEL,sha256=Xo9-1PvkuimrydujYJAjF7pCkriuXBpUPEjma1nZyJ0,92
6
- rowan_python-0.0.1.dist-info/top_level.txt,sha256=0B0BJ1GvTwD5rxpsHctrermP7PVk4SFaQb2syJpGQl8,6
7
- rowan_python-0.0.1.dist-info/RECORD,,