-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation_reporter.py
191 lines (160 loc) · 5.93 KB
/
validation_reporter.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
# Reporter for GASKAP validation script
#
# Author James Dempsey
# Date 23 Nov 2019
from astropy.table import Table
from astropy.io.votable import from_table, writeto
from astropy.io.votable.tree import Param
import numpy as np
class ReportSection(object):
def __init__(self, title, target=None):
self.title = title
self.target = target
self.items = {}
self.current_row = 0
self.items[self.current_row] = []
def add_item(self, title, value=None, link=None, image=None):
item = ReportItem(title, value, link, image)
self.items[self.current_row].append(item)
def start_new_row(self):
self.current_row += 1
self.items[self.current_row] = []
class ReportItem(object):
def __init__(self, title, value=None, link=None, image=None):
self.title = title
self.value = value
self.link = link
self.image = image
class ValidationMetric(object):
def __init__(self, title, description, value, status):
self.title = title
self.description = description
self.value = value
self.status = status
class ValidationReport(object):
def __init__(self, title, metrics_subtitle='GASKAP HI Validation Metrics'):
self.title = title
self.metrics_subtitle = metrics_subtitle
self.sections = []
self.metrics = []
self.project = None
self.sbid = None
def add_section(self, section):
self.sections.append(section)
def add_metric(self, metric):
self.metrics.append(metric)
def _output_header(f, reporter):
f.write('<html>\n<head>\n<title>{}</title>'.format(reporter.title))
with open('style.html') as style:
f.write(style.read())
f.write('\n</head>\n<body>\n<h1 align="middle">{}</h1>'.format(reporter.title))
return
def _output_report_table_header(f, title, target=None):
if title:
f.write('\n<h2 align="middle">{}</h2>'.format(title))
if target:
f.write('\n<h4 align="middle"><i>File: \'{}\'</i></h4>'.format(target))
f.write('\n<table class="reportTable">')
return
def _get_metric_class_name(status):
if status == 1:
return 'good'
elif status == 2:
return 'uncertain'
else:
return 'bad'
def _output_metrics(f, reporter):
_output_report_table_header(f, reporter.metrics_subtitle)
f.write('\n<tr>')
for metric in reporter.metrics:
f.write('\n<th title="{}">{}</th>'.format(metric.description, metric.title))
f.write('\n</tr>\n<tr>')
for metric in reporter.metrics:
f.write('\n<td class={} title="{}">{}</td>'.format(_get_metric_class_name(metric.status), metric.description,
metric.value))
f.write('\n</tr>\n<table>')
return
def _output_single_item(f, item):
if item.link:
f.write('<a href="{}" target="_blank">'.format(item.link))
if item.value:
f.write(str(item.value))
elif item.image:
f.write('<img src="{}">'.format(item.image))
else:
f.write(' ')
if item.link:
f.write('</a>')
def _output_multiple_link_item(f, item):
for idx, link in enumerate(item.link):
if idx > 0:
f.write('<br/>')
f.write('<a href="{}" target="_blank">'.format(item.link[idx]))
if item.value:
if np.ndim(item.value) == 0:
f.write(str(item.value))
else:
f.write(str(item.value[idx]))
elif item.image:
if np.ndim(item.image) == 0:
f.write('<img src="{}">'.format(item.image))
else:
f.write('<img src="{}">'.format(item.image[idx]))
else:
f.write(' ')
f.write('</a>')
def _output_section(f, section):
for idx in range(section.current_row+1):
_output_report_table_header(f, section.title if idx == 0 else None, target=section.target)
items = section.items[idx]
f.write('\n<tr>')
for item in items:
f.write('\n<th>{}</th>'.format(item.title))
f.write('\n</tr>\n<tr>')
for item in items:
f.write('\n<td>')
if item.link and np.ndim(item.link) > 0:
_output_multiple_link_item(f, item)
else:
_output_single_item(f, item)
f.write('</td>')
f.write('\n</tr>')
f.write('\n<table>')
return
def _output_footer(f, reporter):
f.write('\n\n</body>\n</html>')
return
def output_html_report(reporter, dest_folder):
with open(dest_folder + '/index.html', 'w') as f:
_output_header(f, reporter)
_output_metrics(f, reporter)
for section in reporter.sections:
_output_section(f, section)
_output_footer(f, reporter)
return
def output_metrics_xml(reporter, dest_folder):
titles = []
descs = []
values = []
statuses = []
for metric in reporter.metrics:
# title, description, value, status
titles.append(metric.title)
descs.append(metric.description)
values.append(metric.value)
statuses.append(metric.status)
temp_table = Table([titles, descs, values, statuses], names=['metric_name', 'metric_description', 'metric_value', 'metric_status'],
dtype=['str', 'str', 'double', 'int'])
votable = from_table(temp_table)
votable.version = "1.3"
table = votable.get_first_table()
if reporter.project:
table.params.append(Param(votable, name="project", datatype="char", arraysize="*", value=reporter.project))
if reporter.sbid:
table.params.append(Param(votable, name="sbid", datatype="char", arraysize="*", value=reporter.sbid))
table.get_field_by_id('metric_name').datatype = 'char'
table.get_field_by_id('metric_description').datatype = 'char'
table.get_field_by_id('metric_status').datatype = 'int'
filename = dest_folder + '/gaskap-metrics.xml'
writeto(votable, filename)
return