webmo 1.2.0__tar.gz → 1.3.2__tar.gz

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.
@@ -1,12 +1,14 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: webmo
3
- Version: 1.2.0
3
+ Version: 1.3.2
4
4
  Summary: A Python-based interface to WebMO
5
5
  Home-page: https://github.com/jrschmidt2/webmo-python
6
- Download-URL: https://github.com/jrschmidt2/webmo-python/archive/refs/tags/1.2.0.tar.gz
6
+ Download-URL: https://github.com/jrschmidt2/webmo-python/archive/refs/tags/1.3.2.tar.gz
7
7
  Author: J.R. Schmidt
8
8
  Author-email: schmidt@webmo.net
9
9
  License: MIT
10
10
  Keywords: WEBMO,REST,API
11
11
  Provides-Extra: spectrum
12
+ Provides-Extra: util
13
+ Provides-Extra: gui
12
14
  License-File: LICENSE
@@ -33,6 +33,8 @@ setup(
33
33
  'websockets',
34
34
  ],
35
35
  extras_require={
36
- "spectrum": ["numpy"]
36
+ "spectrum": ["numpy"],
37
+ "util": ["PubChemPy"],
38
+ "gui": ["questionary", "ipywidgets"]
37
39
  }
38
40
  )
@@ -1,2 +1,2 @@
1
1
  from .webmo_rest import WebMOREST
2
- __version__ = "1.2.0"
2
+ __version__ = "1.3.2"
@@ -0,0 +1,181 @@
1
+ #handle abstract base class
2
+ from abc import ABC, abstractmethod
3
+
4
+ class _WebMOGUIBase(ABC):
5
+ """The _WebMOGUIBase class is an abstract base class for all Python-based GUIs for WebMO.
6
+ """
7
+
8
+ def __init__(self, template, query_vars, additional_vars):
9
+ self._query_vars = query_vars
10
+ self._additional_vars = additional_vars
11
+
12
+ @abstractmethod
13
+ def display(self):
14
+ """Presents the menu-driven JupyterGUI
15
+
16
+ Displays the JupyterGUI using a system of IPython widgets.
17
+
18
+ Args:
19
+ None
20
+
21
+ Returns:
22
+ None
23
+ """
24
+ pass
25
+
26
+ @abstractmethod
27
+ def get_variables(self):
28
+ """Returns a populated variables dictionary.
29
+
30
+ Returns a populated variables dictionary, generated using the user-selected
31
+ menu options, along with an additional user-specified key/value pairs (additional_vars)
32
+
33
+ Args:
34
+ None
35
+
36
+ Returns:
37
+ A populated dictionary of of variables and values.
38
+ """
39
+ pass
40
+
41
+ class JupyterGUI(_WebMOGUIBase):
42
+ """The JupyterGUI class is a menu-driven Jupyter GUI interface for setting up WebMO jobs.
43
+
44
+ The class utilizes IPython widgets to present a menu-driven interface, using a subset
45
+ of user-specified list of template variables.
46
+ """
47
+ try:
48
+ import ipywidgets
49
+ except:
50
+ #fail silently, since this should never happen in Jupyter
51
+ pass #print('ipywidgets not found; disabling support for JupyterGUI')
52
+
53
+ def __init__(self, template, query_vars, additional_vars=None):
54
+ """Constructor for JupyterGUI
55
+
56
+ This constructor generates a JupyterGUI object.
57
+
58
+ Args:
59
+ template(dict): A specific template, selected from WebMOREST.get_templates()
60
+ query_vars(list): A subset of variables to use when generating the GUI
61
+ additional_vars(dict, optional): A dictionary of additional variables/values to utilize during input file generation
62
+
63
+ Returns:
64
+ object: The newly constructed JupyterGUI object
65
+ """
66
+
67
+ _WebMOGUIBase.__init__(self, template, query_vars, additional_vars)
68
+ self._widgets = []
69
+
70
+ for var in query_vars:
71
+ if template['variables'][var]['type'] == 'checkbox':
72
+ #handle checkbox options
73
+ self._widgets.append(
74
+ self.ipywidgets.widgets.Checkbox(
75
+ value=True if template['variables'][var]['default'] == 'on' else False,
76
+ description=var,
77
+ disabled=False));
78
+ elif template['variables'][var]['type'] == 'dropdown':
79
+ #handle select/dropdown options
80
+ self._widgets.append(
81
+ self.ipywidgets.widgets.Dropdown(
82
+ options=template['variables'][var]['options'],
83
+ value=template['variables'][var]['default'],
84
+ description=var,
85
+ disabled=False));
86
+ else:
87
+ #handle text input
88
+ self._widgets.append(
89
+ self.ipywidgets.widgets.Text(
90
+ description=var,
91
+ value=template['variables'][var]['default'],
92
+ disabled=False));
93
+
94
+ def display(self):
95
+
96
+ for widget in self._widgets:
97
+ display(widget)
98
+
99
+ def get_variables(self):
100
+
101
+ # add in additional user-specified variables
102
+ if (self._additional_vars != None):
103
+ variables = {**self._additional_vars}
104
+ else:
105
+ variables = {}
106
+
107
+ for (var, widget) in zip(self._query_vars, self._widgets):
108
+ if isinstance(widget, self.ipywidgets.Checkbox):
109
+ variables[var] = "on" if widget.value == True else ""
110
+ else:
111
+ variables[var] = widget.value
112
+
113
+ return variables
114
+
115
+ class ConsoleGUI(_WebMOGUIBase):
116
+ """The ConsoleGUI class is a text-based interface for setting up WebMO jobs.
117
+
118
+ The class utilizes the questionary library to present a neat interface using a subset
119
+ of user-specified list of template variables.
120
+ """
121
+
122
+ def __init__(self, template, query_vars, additional_vars=None):
123
+ """Constructor for ConsoleGUI
124
+
125
+ This constructor generates a ConsoleGUI object.
126
+
127
+ Args:
128
+ template(dict): A specific template, selected from WebMOREST.get_templates()
129
+ query_vars(list): A subset of variables to use when generating the GUI
130
+ additional_vars(dict, optional): A dictionary of additional variables/values to utilize during input file generation
131
+
132
+ Returns:
133
+ object: The newly constructed ConsoleGUI object
134
+ """
135
+
136
+ import questionary as q
137
+
138
+ _WebMOGUIBase.__init__(self, template, query_vars, additional_vars)
139
+ self._questions = []
140
+ for var in query_vars:
141
+ # checkbox options
142
+ if template['variables'][var]['type'] == 'checkbox':
143
+ self._questions.append(q.confirm(var));
144
+ # select/dropdown options
145
+ elif template['variables'][var]['type'] == 'dropdown':
146
+ choices = []
147
+ for key, value in template['variables'][var]['options'].items():
148
+ choices.append(q.Choice(title=key, value=value))
149
+
150
+ self._questions.append(
151
+ q.select(
152
+ var,
153
+ choices=choices,
154
+ use_shortcuts=True));
155
+ # text input
156
+ else:
157
+ self._questions.append(
158
+ q.text(var));
159
+
160
+
161
+ def display(self):
162
+
163
+ self._answers = []
164
+ for question in self._questions:
165
+ self._answers.append(question.ask())
166
+
167
+ def get_variables(self):
168
+
169
+ # add in additional user-specified variables
170
+ if (self._additional_vars != None):
171
+ variables = {**self._additional_vars}
172
+ else:
173
+ variables = {}
174
+
175
+ for (var, value) in zip(self._query_vars, self._answers):
176
+ if isinstance(value, bool):
177
+ variables[var] = "on" if value else ""
178
+ else:
179
+ variables[var] = value
180
+
181
+ return variables
@@ -0,0 +1,25 @@
1
+ def xyz_from_name(name):
2
+ """Obtains an XYZ-formatted geometry for the molecule name via PubChem lookup.
3
+
4
+ Arguments:
5
+ name(str): The name of the molecule.
6
+
7
+ Returns:
8
+ A string containing the molecule's XYZ-formatted geometry.
9
+ """
10
+ import pubchempy as pcp
11
+
12
+ results = pcp.get_compounds(name, 'name', record_type='3d')
13
+ if (len(results) == 0):
14
+ raise ValueError("Specified molecule could not be found in PubChem")
15
+
16
+ record = results[0]
17
+ geom = ""
18
+ for atom in record.atoms:
19
+ element = atom.element
20
+ x = float(atom.x)
21
+ y = float(atom.y)
22
+ z = float(atom.z)
23
+ geom += f"{element}\t{x:f}\t{y:f}\t{z:f}\n"
24
+
25
+ return geom
@@ -52,8 +52,9 @@ class WebMOREST:
52
52
 
53
53
  This destructor automatically deletes the session token using the REST interface
54
54
  """
55
- #End the REST sessions
56
- r = requests.delete(self._base_url + '/sessions', params=self._auth)
55
+ #End the REST sessions; check in case requests has already been released
56
+ if requests.delete is not None:
57
+ r = requests.delete(self._base_url + '/sessions', params=self._auth)
57
58
  #do not raise an exception for a failed request in this case due to issues
58
59
  #with object management in Jupyter (i.e. on code re-run, a new token is made
59
60
  #prior to deletion!)
@@ -147,7 +148,7 @@ class WebMOREST:
147
148
  A list of folders
148
149
  """
149
150
 
150
- #append other relevant paramters
151
+ #append other relevant parameters
151
152
  params = self._auth.copy()
152
153
  params.update({'user' : target_user})
153
154
  r = requests.get(self._base_url + "/folders", params=params)
@@ -195,21 +196,36 @@ class WebMOREST:
195
196
  r = requests.get(self._base_url + "/jobs/%d" % job_number, params=self._auth)
196
197
  r.raise_for_status()
197
198
  return r.json()
198
-
199
- def get_job_results(self, job_number):
200
- """Returns detailed results of the calculation (e.g. energy, properties) from the specified job.
199
+
200
+ def _check_valid_job_list(self, job_numbers):
201
+ if len(job_numbers) == 0:
202
+ raise ValueError("One or more job number(s) must be specified")
203
+
204
+ def get_job_results(self, *job_numbers):
205
+ """Returns detailed results of the calculation (e.g. energy, properties) from the specified job(s).
201
206
 
202
207
  This call returns a JSON formatted string summarize all of the calculated and parsed properties
203
208
  from the specified job. This information is normally summarized on the View Job page.
204
209
 
205
210
  Arguments:
206
- job_number(int): The job about whom to return information
211
+ *job_numbers(int): The job(s) for which to generate the results
207
212
 
208
213
  Returns:
209
214
  A JSON formatted string summarizing the calculated properties
210
215
  """
211
-
212
- r = requests.get(self._base_url + "/jobs/%d/results" % job_number, params=self._auth)
216
+
217
+ self._check_valid_job_list(job_numbers)
218
+
219
+ if (len(job_numbers) == 1):
220
+ #handle case of single job, return JSON dictionary for single job
221
+ r = requests.get(self._base_url + "/jobs/%d/results" % job_numbers[0], params=self._auth)
222
+ else:
223
+ #handle case of multiple jobs, return JSON array of dictionaries
224
+ #append other relevant parameters
225
+ params = self._auth.copy()
226
+ params.update({'jobNumber' : ",".join(str(job_number) for job_number in job_numbers)})
227
+ r = requests.get(self._base_url + "/jobs/results", params=params)
228
+
213
229
  r.raise_for_status()
214
230
  return r.json()
215
231
 
@@ -245,19 +261,45 @@ class WebMOREST:
245
261
  r.raise_for_status()
246
262
  return r.text
247
263
 
248
- def get_job_archive(self, job_number):
249
- """Returns a WebMO archive from the specified job.
264
+ def get_job_archive(self, *job_numbers):
265
+ """Returns a WebMO archive from the specified job(s).
250
266
 
251
- This call generates and returns a binary WebMO archive (tar/zip) file from the specified job.
267
+ This call generates and returns a binary WebMO archive (tar/zip) file from the specified job(s).
252
268
 
253
269
  Arguments:
254
- job_number(int): The job about whom to generate the archive
270
+ *job_numbers(int): The job(s) for which to generate the archive
255
271
 
256
272
  Returns:
257
273
  The raw data (as a string) of the WebMO archive, appropriate for saving to disk
258
274
  """
275
+
276
+ self._check_valid_job_list(job_numbers)
277
+
278
+ #append other relevant parameters
279
+ params = self._auth.copy()
280
+ params.update({'jobNumber' : ",".join(str(job_number) for job_number in job_numbers)})
281
+ r = requests.get(self._base_url + "/jobs/archive" % (), params=params)
282
+ r.raise_for_status()
283
+ return r.text
284
+
285
+ def get_job_spreadsheet(self, *job_numbers):
286
+ """Returns a spreadsheet summary of the specified job(s).
287
+
288
+ This call generates and returns a CSV-formatted spreadsheet summary of the specified job(s).
259
289
 
260
- r = requests.get(self._base_url + "/jobs/%d/archive" % job_number, params=self._auth)
290
+ Arguments:
291
+ *job_numbers(int): The job(s) for which to generate the spreadsheet
292
+
293
+ Returns:
294
+ The raw data (as a string) of the spreadsheet, appropriate for saving to disk
295
+ """
296
+
297
+ self._check_valid_job_list(job_numbers)
298
+
299
+ #append other relevant parameters
300
+ params = self._auth.copy()
301
+ params.update({'jobNumber' : ",".join(str(job_number) for job_number in job_numbers)})
302
+ r = requests.get(self._base_url + "/jobs/spreadsheet" % (), params=params)
261
303
  r.raise_for_status()
262
304
  return r.text
263
305
 
@@ -295,26 +337,106 @@ class WebMOREST:
295
337
  r.raise_for_status()
296
338
  return r.json()["jobNumber"]
297
339
 
298
- def submit_job(self, job_name, input_file_contents, engine, queue=None):
340
+ def submit_job(self, job_name, input_file_contents, engine, queue=None, nodes=1, ppn=1, **kwargs):
299
341
  """Submits and executes a new WebMO job
300
342
 
301
- This call submits and executes a new job to a computational engine, generating a new WebMO job.
343
+ This call submits and executes a new job to a computational engine, generating a new WebMO job,
344
+ using nodes*ppn total cores.
302
345
 
303
346
  Arguments:
304
347
  job_name(str): The name of the new WebMO job
305
348
  input_file_contents(str): The contents of a valid input file to submit to a computational engine
306
349
  engine(str): The name of the computational engine
350
+ queue(str, optional): The name of the queue in which to submit the job_name (required for external queues)
351
+ nodes(int, optional): The number of nodes to use; defaults to 1
352
+ ppn(int, optional): The number of cores per node to use; defaults to 1
353
+ **kwargs(optional): Additional optional fields such as nodeType [PBS],
354
+ resourceList (aka constraints) [SGE/SLURM], timeLimit [SLURM], gpus [SLURM]
307
355
 
308
356
  Returns:
309
- The the job number of the new job, upon success
357
+ The the job number of the new job, upon success.
310
358
  """
311
359
 
312
360
  params = self._auth.copy()
313
- params.update({'jobName' : job_name, 'engine' : engine, 'inputFile': input_file_contents, 'queue': queue})
361
+ params.update({'jobName' : job_name, 'engine' : engine, 'inputFile': input_file_contents,
362
+ 'queue': queue, 'nodes': nodes, 'ppn': ppn, **kwargs})
314
363
  r = requests.post(self._base_url + '/jobs', data=params)
315
364
  r.raise_for_status()
316
365
  return r.json()["jobNumber"]
317
366
 
367
+ #
368
+ # Template resource
369
+ #
370
+ def get_templates(self, engine):
371
+ """Fetches a list of job templates available to the current user
372
+
373
+ This call returns a dictionary of available templates and associated job variables.
374
+
375
+ Arguments:
376
+ engine(str): The name of the computational engine for which to fetch templates.
377
+
378
+ Returns:
379
+ A dictionary of available templates
380
+ """
381
+
382
+ r = requests.get(self._base_url + "/templates/%s" % engine, params=self._auth)
383
+ r.raise_for_status()
384
+ return r.json()["templates"]
385
+
386
+
387
+ def generate_input(self, template, variables, auto_defaults=True):
388
+ """Generates an input file from the specified template and dictionary of template job variables.
389
+
390
+ This call returns a text-formatted input file appropriate for submission.
391
+
392
+ Arguments:
393
+ template(dict): The desired job tempate (from get_templates)
394
+ variables(dict): A dictionary of variables to be used to generate the input file from the template.
395
+ auto_defaults(bool): Automatically use WebMO default values for omitted template variables
396
+
397
+ Returns:
398
+ A text-formatted input file
399
+ """
400
+ amend_variables = variables.copy()
401
+ if auto_defaults:
402
+ #update and amend 'variables' with default values for missing template parameters
403
+ for var in template['variables']:
404
+ if not var in variables:
405
+ amend_variables[var] = template['variables'][var]['default']
406
+
407
+ #append other relevant paramters
408
+ params = self._auth.copy()
409
+ params.update({'variables' : json.dumps(amend_variables)})
410
+ r = requests.get(self._base_url + "/templates/%s" % template['id'], params=params)
411
+ r.raise_for_status()
412
+ return r.text
413
+
414
+ #
415
+ # Engines resource
416
+ #
417
+ def get_engines(self, target_user=""):
418
+ """Fetches a list of job templates available to the current user or specified target user
419
+
420
+ This call returns a JSON-formatted list of available engines and associated queues.
421
+
422
+ Arguments:
423
+ target_user(str, optional): The target username whose folders are retrieved. Otherwise, uses the authenticated user.
424
+
425
+ Returns:
426
+ A JSON-formatted list of available engines, associated queues/servers, and
427
+ node and processor limits.
428
+ """
429
+
430
+ #append other relevant parameters
431
+ params = self._auth.copy()
432
+ params.update({'user' : target_user})
433
+ r = requests.get(self._base_url + "/engines", params=params)
434
+ r.raise_for_status()
435
+ return r.json()["engines"]
436
+
437
+ #
438
+ # Jupyter-related
439
+ #
318
440
  async def display_job_property(self, job_number, property_name, property_index=1, peak_width=0.0, tms_shift=0.0, proton_coupling=0.0, nmr_field=400.0, x_range=None, y_range=None, width=400, height=400, background_color=(255,255,255), transparent_background=False, rotate=(0.,0.,0.)):
319
441
  """Uses Javascript and IPython to display an image of the specified molecule and property,
320
442
  calculated from a previous WebMO job.
@@ -683,7 +805,7 @@ class WebMOREST:
683
805
 
684
806
  async def _process_callback_response(self):
685
807
  from base64 import b64decode
686
- TIMEOUT = 10
808
+ TIMEOUT = 60
687
809
 
688
810
  json_msg = None
689
811
  for i in range(TIMEOUT):
@@ -1,12 +1,14 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: webmo
3
- Version: 1.2.0
3
+ Version: 1.3.2
4
4
  Summary: A Python-based interface to WebMO
5
5
  Home-page: https://github.com/jrschmidt2/webmo-python
6
- Download-URL: https://github.com/jrschmidt2/webmo-python/archive/refs/tags/1.2.0.tar.gz
6
+ Download-URL: https://github.com/jrschmidt2/webmo-python/archive/refs/tags/1.3.2.tar.gz
7
7
  Author: J.R. Schmidt
8
8
  Author-email: schmidt@webmo.net
9
9
  License: MIT
10
10
  Keywords: WEBMO,REST,API
11
11
  Provides-Extra: spectrum
12
+ Provides-Extra: util
13
+ Provides-Extra: gui
12
14
  License-File: LICENSE
@@ -3,7 +3,9 @@ README.md
3
3
  setup.cfg
4
4
  setup.py
5
5
  webmo/__init__.py
6
+ webmo/gui.py
6
7
  webmo/spectrum.py
8
+ webmo/util.py
7
9
  webmo/webmo_rest.py
8
10
  webmo.egg-info/PKG-INFO
9
11
  webmo.egg-info/SOURCES.txt
@@ -0,0 +1,12 @@
1
+ requests
2
+ websockets
3
+
4
+ [gui]
5
+ questionary
6
+ ipywidgets
7
+
8
+ [spectrum]
9
+ numpy
10
+
11
+ [util]
12
+ PubChemPy
@@ -1,5 +0,0 @@
1
- requests
2
- websockets
3
-
4
- [spectrum]
5
- numpy
File without changes
File without changes
File without changes
File without changes