-
Notifications
You must be signed in to change notification settings - Fork 6
/
data_model.py
541 lines (463 loc) · 20.9 KB
/
data_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
""" Data model for the execution status of COMBINE/OMEX archives
:Author: Jonathan Karr <karr@mssm.edu>
:Date: 2020-12-06
:Copyright: 2020, Center for Reproducible Biomedical Modeling
:License: MIT
"""
from ..config import get_config
import enum
import os
import yaml
__all__ = [
'Status',
'Log',
'CombineArchiveLog',
'SedDocumentLog',
'TaskLog',
'OutputLog',
'ReportLog',
'Plot2DLog',
'Plot3DLog',
'StandardOutputErrorCapturerLevel',
]
class Status(str, enum.Enum):
""" Status of COMBINE/OMEX archive or one of its components """
QUEUED = 'QUEUED'
RUNNING = 'RUNNING'
SUCCEEDED = 'SUCCEEDED'
SKIPPED = 'SKIPPED'
# component will not / wasn't executed e.g., a plot won't be created because a
# simulation doesn't have the ability to create plots
FAILED = 'FAILED'
class Log(object):
""" Log of a COMBINE/OMEX archive or one of its components
Attributes:
status (:obj:`Status`): execution status of the archive
exception (:obj:`Exception`): exception
skip_reason (:obj:`Exception`): reason of skip
output (:obj:`str`): output
duration (:obj:`float`): duration in seconds
parent (:obj:`Log`): execution status of parent COMBINE/OMEX archive
out_dir (:obj:`str`): directory to export status
"""
def __init__(self, id=None, status=None, exception=None, skip_reason=None, output=None, duration=None, parent=None, out_dir=None):
"""
Args:
status (:obj:`Status`, optional): execution status of the archive
exception (:obj:`Exception`, optional): exception
skip_reason (:obj:`Exception`, optional): reason of skip
output (:obj:`str`, optional): output
duration (:obj:`float`, optional): duration in seconds
parent (:obj:`Log`, optional): execution status of parent COMBINE/OMEX archive
out_dir (:obj:`str`, optional): directory to export status
"""
self.status = status
self.exception = exception
self.skip_reason = skip_reason
self.output = output
self.duration = duration
self.parent = parent
self.out_dir = out_dir
def finalize(self):
""" Mark all unexecuted elements as skipped """
if self.status == Status.QUEUED:
self.status = Status.SKIPPED
elif self.status == Status.RUNNING:
self.status = Status.FAILED
def to_json(self):
""" Generate a JSON-compatible representation
Returns:
:obj:`dict`: JSON-compatible representation
"""
value = {}
value['status'] = self.status.value if self.status else None
if self.exception:
value['exception'] = {
'type': self.exception.__class__.__name__,
'message': str(self.exception),
}
else:
value['exception'] = None
if self.skip_reason:
value['skipReason'] = {
'type': self.skip_reason.__class__.__name__,
'message': str(self.skip_reason),
}
else:
value['skipReason'] = None
value['output'] = self.output
value['duration'] = self.duration
return value
def export(self):
""" Write to a file """
if self.out_dir:
log_path = get_config().LOG_PATH
if log_path:
path = os.path.join(self.out_dir, log_path)
if not os.path.isdir(self.out_dir):
os.makedirs(self.out_dir)
with open(path, 'w') as file:
file.write(yaml.dump(self.to_json()))
elif self.parent:
self.parent.export()
class CombineArchiveLog(Log):
""" Log of a COMBINE/OMEX archive
Attributes:
status (:obj:`Status`): execution status of the archive
exception (:obj:`Exception`): exception
skip_reason (:obj:`Exception`): reason of skip
output (:obj:`str`): output
duration (:obj:`float`): duration in seconds
sed_documents (:obj:`dict` of :obj:`str` to :obj:`SedDocumentLog`): execution status of each
SED document in the archive
out_dir (:obj:`str`): directory to export status
"""
def __init__(self, id=None, status=None, exception=None, skip_reason=None, output=None, duration=None, sed_documents=None,
out_dir=None):
"""
Args:
status (:obj:`Status`, optional): execution status of the archive
exception (:obj:`Exception`, optional): exception
skip_reason (:obj:`Exception`, optional): reason of skip
output (:obj:`str`, optional): output
duration (:obj:`float`, optional): duration in seconds
sed_documents (:obj:`dict` of :obj:`str` to :obj:`SedDocumentLog`, optional): execution status of each
SED document in the archive
out_dir (:obj:`str`, optional): directory to export status
"""
super(CombineArchiveLog, self).__init__(status=status, exception=exception,
skip_reason=skip_reason, output=output,
duration=duration, out_dir=out_dir)
self.sed_documents = sed_documents
def finalize(self):
""" Mark all unexceuted elements as skipped """
super(CombineArchiveLog, self).finalize()
if self.sed_documents:
for sed_document in self.sed_documents.values():
sed_document.finalize()
def to_json(self):
""" Generate a JSON-compatible representation
Returns:
:obj:`dict`: JSON-compatible representation
"""
value = super(CombineArchiveLog, self).to_json()
value['sedDocuments'] = (
[doc_log.to_json() for doc_log in self.sed_documents.values() if doc_log]
if self.sed_documents is not None
else None
)
return value
class SedDocumentLog(Log):
""" Log of a SED document
Attributes:
location (:obj:`str`): location of the SED document
status (:obj:`Status`): execution status of the archive
exception (:obj:`Exception`): exception
skip_reason (:obj:`Exception`): reason of skip
output (:obj:`str`): output
duration (:obj:`float`): duration in seconds
tasks (:obj:`dict` of :obj:`str` to :obj:`TaskLog`): execution status of each
task
outputs (:obj:`dict` of :obj:`str` to :obj:`OutputLog`): execution status of each
output
parent (:obj:`CombineArchiveLog`): execution status of parent COMBINE/OMEX archive
out_dir (:obj:`str`): directory to export status
"""
def __init__(self, location=None, status=None, exception=None, skip_reason=None, output=None, duration=None,
tasks=None, outputs=None, parent=None, out_dir=None):
"""
Args:
location (:obj:`str`, optional): location of the SED document
status (:obj:`Status`, optional): execution status of the archive
exception (:obj:`Exception`, optional): exception
skip_reason (:obj:`Exception`, optional): reason of skip
output (:obj:`str`, optional): output
duration (:obj:`float`, optional): duration in seconds
tasks (:obj:`dict` of :obj:`str` to :obj:`TaskLog`, optional): execution status of each
task
outputs (:obj:`dict` of :obj:`str` to :obj:`OutputLog`, optional): execution status of each
output
parent (:obj:`CombineArchiveLog`, optional): execution status of parent COMBINE/OMEX archive
out_dir (:obj:`str`, optional): directory to export status
"""
super(SedDocumentLog, self).__init__(status=status, exception=exception,
skip_reason=skip_reason, output=output,
duration=duration, parent=parent, out_dir=out_dir)
self.location = location
self.tasks = tasks
self.outputs = outputs
def finalize(self):
""" Mark all unexceuted elements as skipped """
super(SedDocumentLog, self).finalize()
if self.tasks:
for task in self.tasks.values():
task.finalize()
if self.outputs:
for output in self.outputs.values():
output.finalize()
def to_json(self):
""" Generate a JSON-compatible representation
Returns:
:obj:`dict`: JSON-compatible representation
"""
value = super(SedDocumentLog, self).to_json()
value['location'] = self.location
value['tasks'] = (
[task_log.to_json() for task_log in self.tasks.values() if task_log]
if self.tasks is not None
else None
)
value['outputs'] = (
[output_log.to_json() for output_log in self.outputs.values() if output_log]
if self.outputs is not None
else None
)
return value
class TaskLog(Log):
""" Log of a SED task
Attributes:
id (:obj:`str`): id of the task
status (:obj:`Status`): execution status of the archive
exception (:obj:`Exception`): exception
skip_reason (:obj:`Exception`): reason of skip
output (:obj:`str`): output
duration (:obj:`float`): duration in seconds
algorithm (:obj:`str`): KiSAO id of the requested algorithm
simulator_details (:obj:`dict`): additional simulator-specific information
parent (:obj:`SedDocumentLog`): execution status of parent SED document
out_dir (:obj:`str`): directory to export status
"""
def __init__(self, id=None, status=None, exception=None, skip_reason=None, output=None, duration=None,
algorithm=None, simulator_details=None, parent=None, out_dir=None):
"""
Args:
id (:obj:`str`, optional): id of the task
status (:obj:`Status`, optional): execution status of the archive
exception (:obj:`Exception`, optional): exception
skip_reason (:obj:`Exception`, optional): reason of skip
output (:obj:`str`, optional): output
duration (:obj:`float`, optional): duration in seconds
algorithm (:obj:`str`, optional): KiSAO id of the executed algorithm
simulator_details (:obj:`dict`, optional): additional simulator-specific information
parent (:obj:`SedDocumentLog`): execution status of parent SED document
out_dir (:obj:`str`, optional): directory to export status
"""
super(TaskLog, self).__init__(status=status, exception=exception,
skip_reason=skip_reason, output=output,
duration=duration, parent=parent, out_dir=out_dir)
self.id = id
self.algorithm = algorithm
self.simulator_details = simulator_details
def to_json(self):
""" Generate a JSON-compatible representation
Returns:
:obj:`dict`: JSON-compatible representation
"""
value = super(TaskLog, self).to_json()
value['id'] = self.id
value['algorithm'] = self.algorithm
value['simulatorDetails'] = (
[{'key': key, 'value': value} for key, value in self.simulator_details.items()]
if self.simulator_details
else None
)
return value
class OutputLog(Log):
""" Log of a SED output
Attributes:
id (:obj:`str`): id of the output
status (:obj:`Status`): execution status of the archive
exception (:obj:`Exception`): exception
skip_reason (:obj:`Exception`): reason of skip
output (:obj:`str`): output
duration (:obj:`float`): duration in seconds
parent (:obj:`SedDocumentLog`): execution status of parent SED document
out_dir (:obj:`str`): directory to export status
"""
def __init__(self, id=None, status=None, exception=None, skip_reason=None, output=None, duration=None,
parent=None, out_dir=None):
"""
Args:
id (:obj:`str`, optional): id of the report
status (:obj:`Status`, optional): execution status of the archive
exception (:obj:`Exception`, optional): exception
skip_reason (:obj:`Exception`, optional): reason of skip
output (:obj:`str`, optional): output
duration (:obj:`float`, optional): duration in seconds
parent (:obj:`SedDocumentLog`, optional): execution status of parent SED document
out_dir (:obj:`str`, optional): directory to export status
"""
super(OutputLog, self).__init__(status=status, exception=exception, skip_reason=skip_reason,
output=output, duration=duration, parent=parent, out_dir=out_dir)
self.id = id
def to_json(self):
""" Generate a JSON-compatible representation
Returns:
:obj:`dict`: JSON-compatible representation
"""
value = super(OutputLog, self).to_json()
value['id'] = self.id
return value
class ReportLog(OutputLog):
""" Log of a SED report
Attributes:
id (:obj:`str`): id of the report
status (:obj:`Status`): execution status of the archive
exception (:obj:`Exception`): exception
skip_reason (:obj:`Exception`): reason of skip
output (:obj:`str`): output
duration (:obj:`float`): duration in seconds
data_sets (:obj:`dict` of :obj:`str` to :obj:`Status`): execution status of each
data set
parent (:obj:`SedDocumentLog`): execution status of parent SED document
out_dir (:obj:`str`): directory to export status
"""
def __init__(self, id=None, status=None, exception=None, skip_reason=None, output=None, duration=None, data_sets=None,
parent=None, out_dir=None):
"""
Args:
id (:obj:`str`, optional): id of the report
status (:obj:`Status`, optional): execution status of the archive
exception (:obj:`Exception`, optional): exception
skip_reason (:obj:`Exception`, optional): reason of skip
output (:obj:`str`, optional): output
duration (:obj:`float`, optional): duration in seconds
data_sets (:obj:`dict` of :obj:`str` to :obj:`Status`, optional): execution status of each
data set
parent (:obj:`SedDocumentLog`, optional): execution status of parent SED document
out_dir (:obj:`str`, optional): directory to export status
"""
super(ReportLog, self).__init__(id=id, status=status, exception=exception, skip_reason=skip_reason,
output=output, duration=duration, parent=parent, out_dir=out_dir)
self.data_sets = data_sets
def finalize(self):
""" Mark all unexceuted elements as skipped """
super(ReportLog, self).finalize()
if self.data_sets:
for id, status in self.data_sets.items():
if status == Status.QUEUED:
self.data_sets[id] = Status.SKIPPED
elif status == Status.RUNNING:
self.data_sets[id] = Status.FAILED
def to_json(self):
""" Generate a JSON-compatible representation
Returns:
:obj:`dict`: JSON-compatible representation
"""
dict_log = super(ReportLog, self).to_json()
dict_log['dataSets'] = (
[{'id': id, 'status': status.value if status else None} for id, status in self.data_sets.items()]
if self.data_sets is not None
else None
)
return dict_log
class Plot2DLog(OutputLog):
""" Log of a 2D SED plot
Attributes:
id (:obj:`str`): id of the plot
status (:obj:`Status`): execution status of the archive
exception (:obj:`Exception`): exception
skip_reason (:obj:`Exception`): reason of skip
output (:obj:`str`): output
duration (:obj:`float`): duration in seconds
curves (:obj:`dict` of :obj:`str` to :obj:`Status`): execution status of each
curve
parent (:obj:`SedDocumentLog`): execution status of parent SED document
out_dir (:obj:`str`): directory to export status
"""
def __init__(self, id=None, status=None, exception=None, skip_reason=None, output=None, duration=None, curves=None,
parent=None, out_dir=None):
"""
Args:
id (:obj:`str`, optional): id of the plot
status (:obj:`Status`, optional): execution status of the archive
exception (:obj:`Exception`, optional): exception
skip_reason (:obj:`Exception`, optional): reason of skip
output (:obj:`str`, optional): output
duration (:obj:`float`, optional): duration in seconds
curves (:obj:`dict` of :obj:`str` to :obj:`Status`, optional): execution status of each
curve
parent (:obj:`SedDocumentLog`, optional): execution status of parent SED document
out_dir (:obj:`str`, optional): directory to export status
"""
super(Plot2DLog, self).__init__(id=id, status=status, exception=exception, skip_reason=skip_reason,
output=output, duration=duration, parent=parent, out_dir=out_dir)
self.curves = curves
def finalize(self):
""" Mark all unexceuted elements as skipped """
super(Plot2DLog, self).finalize()
if self.curves:
for id, status in self.curves.items():
if status == Status.QUEUED:
self.curves[id] = Status.SKIPPED
elif status == Status.RUNNING:
self.curves[id] = Status.FAILED
def to_json(self):
""" Generate a JSON-compatible representation
Returns:
:obj:`dict`: JSON-compatible representation
"""
dict_log = super(Plot2DLog, self).to_json()
dict_log['curves'] = (
[{'id': id, 'status': status.value if status else None} for id, status in self.curves.items()]
if self.curves is not None
else None
)
return dict_log
class Plot3DLog(OutputLog):
""" Log of a 3D SED plot
Attributes:
id (:obj:`str`): id of the plot
status (:obj:`Status`): execution status of the archive
exception (:obj:`Exception`): exception
skip_reason (:obj:`Exception`): reason of skip
output (:obj:`str`): output
duration (:obj:`float`): duration in seconds
surfaces (:obj:`dict` of :obj:`str` to :obj:`Status`): execution status of each
surface
parent (:obj:`SedDocumentLog`): execution status of parent SED document
out_dir (:obj:`str`): directory to export status
"""
def __init__(self, id=None, status=None, exception=None, skip_reason=None, output=None, duration=None, surfaces=None,
parent=None, out_dir=None):
"""
Args:
id (:obj:`str`, optional): id of the report
status (:obj:`Status`, optional): execution status of the archive
exception (:obj:`Exception`, optional): exception
skip_reason (:obj:`Exception`, optional): reason of skip
output (:obj:`str`, optional): output
duration (:obj:`float`, optional): duration in seconds
surfaces (:obj:`dict` of :obj:`str` to :obj:`Status`, optional): execution status of each
surface
parent (:obj:`SedDocumentLog`, optional): execution status of parent SED document
out_dir (:obj:`str`, optional): directory to export status
"""
super(Plot3DLog, self).__init__(id=id, status=status, exception=exception, skip_reason=skip_reason,
output=output, duration=duration, parent=parent, out_dir=out_dir)
self.surfaces = surfaces
def finalize(self):
""" Mark all unexceuted elements as skipped """
super(Plot3DLog, self).finalize()
if self.surfaces:
for id, status in self.surfaces.items():
if status == Status.QUEUED:
self.surfaces[id] = Status.SKIPPED
elif status == Status.RUNNING:
self.surfaces[id] = Status.FAILED
def to_json(self):
""" Generate a JSON-compatible representation
Returns:
:obj:`dict`: JSON-compatible representation
"""
dict_log = super(Plot3DLog, self).to_json()
dict_log['surfaces'] = (
[{'id': id, 'status': status.value if status else None} for id, status in self.surfaces.items()]
if self.surfaces is not None
else None
)
return dict_log
class StandardOutputErrorCapturerLevel(int, enum.Enum):
""" Level at which stdout/stderr should be captured """
python = 0
c = 1