|
1
|
|
|
from __future__ import print_function |
|
2
|
|
|
import copy |
|
3
|
|
|
from niprov.format import Format |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class SimpleFormat(Format): |
|
7
|
|
|
|
|
8
|
|
|
_expectedFields = ['acquired','subject','protocol','dimensions','path'] |
|
9
|
|
|
|
|
10
|
|
|
def serializeList(self, images): |
|
11
|
|
|
"""Publish the provenance for several images on the commandline. |
|
12
|
|
|
|
|
13
|
|
|
Args: |
|
14
|
|
|
provenance (list): List of provenance dictionaries. |
|
15
|
|
|
""" |
|
16
|
|
|
text = '' |
|
17
|
|
|
text += '\n' |
|
18
|
|
|
text += '{0:20} {1:12} {2:24} {3:20} {4:24}'.format(*self._expectedFields) |
|
19
|
|
|
for image in images: |
|
20
|
|
|
text += self.serializeSummary(image) |
|
21
|
|
|
text += '\n' |
|
22
|
|
|
text += '\n' |
|
23
|
|
|
return text |
|
24
|
|
|
|
|
25
|
|
|
def serializeSingle(self, img): |
|
26
|
|
|
"""Publish the provenance for one image on the commandline. |
|
27
|
|
|
|
|
28
|
|
|
Args: |
|
29
|
|
|
provenance (dict): Provenance for one image file |
|
30
|
|
|
""" |
|
31
|
|
|
text = '' |
|
32
|
|
|
text += '\n' |
|
33
|
|
|
for field, value in img.provenance.items(): |
|
34
|
|
|
text += '{0:24} {1}\n'.format(field+':', str(value)) |
|
35
|
|
|
text += '\n' |
|
36
|
|
|
return text |
|
37
|
|
|
|
|
38
|
|
|
def serializeSummary(self, image): |
|
39
|
|
|
"""Publish a summary of the provenance for one image as one line in |
|
40
|
|
|
the terminal. |
|
41
|
|
|
|
|
42
|
|
|
Args: |
|
43
|
|
|
provenance (dict): Provenance for one image file |
|
44
|
|
|
""" |
|
45
|
|
|
provcopy = copy.deepcopy(image.provenance) |
|
46
|
|
|
for field in self._expectedFields: |
|
47
|
|
|
if field not in provcopy: |
|
48
|
|
|
provcopy[field] = None |
|
49
|
|
|
tmp = ('{0[acquired]!s} {0[subject]!s:12} {0[protocol]!s:24} ' |
|
50
|
|
|
'{0[dimensions]!s:20} {0[path]!s:24}') |
|
51
|
|
|
return tmp.format(provcopy) |
|
52
|
|
|
|
|
53
|
|
|
def serializeStatistics(self, stats): |
|
54
|
|
|
"""Publish statistics for collected provenance in the terminal. |
|
55
|
|
|
|
|
56
|
|
|
Args: |
|
57
|
|
|
stats (dict): Dictionary with summary values. |
|
58
|
|
|
""" |
|
59
|
|
|
text = '' |
|
60
|
|
|
text += '\n' |
|
61
|
|
|
text += ' Number of files: {0}\n'.format(stats['count']) |
|
62
|
|
|
text += ' Total file size: {0}\n'.format(stats['totalsize']) |
|
63
|
|
|
text += '\n' |
|
64
|
|
|
return text |
|
65
|
|
|
|
|
66
|
|
|
def serializePipeline(self, pipeline): |
|
67
|
|
|
"""Pretty-print a pipeline to the terminal |
|
68
|
|
|
|
|
69
|
|
|
Args: |
|
70
|
|
|
pipeline (Pipeline): Pipeline object to publish. |
|
71
|
|
|
""" |
|
72
|
|
|
tree = pipeline.asFilenameTree() |
|
73
|
|
|
def prettyPrintTree(tree, s='', lvl=0): |
|
74
|
|
|
for key, value in tree.items(): |
|
75
|
|
|
s += '{0}+---{1}\n'.format('| '*lvl,key) |
|
76
|
|
|
s = prettyPrintTree(value, s, lvl+1) |
|
77
|
|
|
return s |
|
78
|
|
|
return prettyPrintTree(tree) |
|
79
|
|
|
|
|
80
|
|
|
|