Skip to content

Commit fd3597b

Browse files
authored
Merge pull request #240 from clamsproject/develop
releasing 1.3.0
2 parents 9fb4d2a + 9c5e1bd commit fd3597b

4 files changed

Lines changed: 46 additions & 3 deletions

File tree

clams/app/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import warnings
66
from abc import ABC, abstractmethod
77
from contextlib import contextmanager
8+
from datetime import datetime
89
from urllib import parse as urlparser
910

1011
__all__ = ['ClamsApp']
@@ -44,6 +45,14 @@ class ClamsApp(ABC):
4445
'name': 'pretty', 'type': 'boolean', 'choices': None, 'default': False, 'multivalued': False,
4546
'description': 'The JSON body of the HTTP response will be re-formatted with 2-space indentation',
4647
},
48+
{
49+
'name': 'runningTime', 'type': 'boolean', 'choices': None, 'default': False, 'multivalued': False,
50+
'description': 'The running time of the app will be recorded in the view metadata',
51+
},
52+
{
53+
'name': 'hwFetch', 'type': 'boolean', 'choices': None, 'default': False, 'multivalued': False,
54+
'description': 'The hardware information (architecture, GPU and vRAM) will be recorded in the view metadata',
55+
},
4756
]
4857

4958
# this key is used to store users' raw input params in the parameter dict
@@ -136,6 +145,7 @@ def annotate(self, mmif: Union[str, dict, Mmif], **runtime_params: List[str]) ->
136145
refined = self._refine_params(**runtime_params)
137146
self.logger.debug(f"Refined parameters: {refined}")
138147
pretty = refined.get('pretty', False)
148+
t = datetime.now()
139149
with warnings.catch_warnings(record=True) as ws:
140150
annotated = self._annotate(mmif, **refined)
141151
if ws:
@@ -144,6 +154,27 @@ def annotate(self, mmif: Union[str, dict, Mmif], **runtime_params: List[str]) ->
144154
warnings_view = annotated.new_view()
145155
self.sign_view(warnings_view, refined)
146156
warnings_view.metadata.warnings = issued_warnings
157+
td = datetime.now() - t
158+
runningTime = refined.get('runningTime', False)
159+
hwFetch = refined.get('hwFetch', False)
160+
runtime_recs = {}
161+
if hwFetch:
162+
import platform, shutil, subprocess
163+
runtime_recs['architecture'] = platform.machine()
164+
# runtime_recs['processor'] = platform.processor() # this only works on Windows
165+
runtime_recs['cuda'] = []
166+
if shutil.which('nvidia-smi'):
167+
for gpu in subprocess.run(['nvidia-smi', '--query-gpu=name,memory.total', '--format=csv,noheader'],
168+
stdout=subprocess.PIPE).stdout.decode('utf-8').strip().split('\n'):
169+
name, mem = gpu.split(', ')
170+
runtime_recs['cuda'].append(f'{name} ({mem})')
171+
for annotated_view in annotated.views:
172+
if annotated_view.metadata.app == self.metadata.identifier:
173+
if runningTime:
174+
annotated_view.metadata.set_additional_property('appRunningTime', str(td))
175+
if len(runtime_recs) > 0:
176+
annotated_view.metadata.set_additional_property('appRunningHardware', runtime_recs)
177+
147178
return annotated.serialize(pretty=pretty, sanitize=True)
148179

149180
@abstractmethod

documentation/appmetadata.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ input specification can be as simple as the following:
9191
# and more app metadata fields,
9292
}
9393
94-
In the above example, the developer is declaring the app is expecting ``Token``annotation objects, with a ``posTagSet``
94+
In the above example, the developer is declaring the app is expecting ``Token`` annotation objects, with a ``posTagSet``
9595
property of which value is the URL of the Penn Treebank POS tag set, *verbatim*, in the input MMIF, and all other
9696
existing annotation types in the input MMIF will be ignored during processing. There are some *grammar* of how this
9797
``input`` list can be written.

documentation/autodoc/clams.appmetadata.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,15 @@ Module contents
66

77
.. autoclass:: clams.appmetadata.AppMetadata
88
:members:
9+
10+
.. autoclass:: clams.appmetadata.Input
11+
:members:
12+
:inherited-members:
13+
14+
.. autoclass:: clams.appmetadata.Output
15+
:members:
16+
:inherited-members:
17+
18+
.. autoclass:: clams.appmetadata.RuntimeParameter
19+
:members:
20+
:inherited-members:

tests/test_clamsapp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,14 @@ def test_sign_view(self):
253253
args3 = {'undefined_param1': ['value1']}
254254
self.app.sign_view(v3, self.app._refine_params(**args3))
255255
self.assertEqual(len(v3.metadata.parameters), 1)
256-
self.assertEqual(len(v3.metadata.appConfiguration), 3) # universal (pretty) + `raise_error` in metadata.py + `multivalued_param`
256+
self.assertEqual(len(v3.metadata.appConfiguration), 5) # universal (pretty, runningtime, hwfetch) + `raise_error` in metadata.py + `multivalued_param`
257257
self.assertEqual(v3.metadata.appConfiguration['multivalued_param'], [])
258258
v4 = m.new_view()
259259
multiple_values = ['value1', 'value2']
260260
args4 = {'multivalued_param': multiple_values}
261261
self.app.sign_view(v4, self.app._refine_params(**args4))
262262
self.assertEqual(len(v4.metadata.parameters), 1)
263-
self.assertEqual(len(v4.metadata.appConfiguration), 3)
263+
self.assertEqual(len(v4.metadata.appConfiguration), 5)
264264
self.assertEqual(len(v4.metadata.parameters['multivalued_param']), len(str(multiple_values)))
265265

266266
def test_annotate(self):

0 commit comments

Comments
 (0)