-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.py
152 lines (126 loc) · 4.59 KB
/
checker.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
# Copyright 2012-2013 Johannes Reinhardt <jreinhardt@ist-dein-freund.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from os import listdir
from blt_parser import load_collection
import numpy as np
class TableChecker:
def __init__(self):
self.blts = []
self.errors = 0
def error(self,msg,coll,part):
print "%s coll: %s standard: %s" % (msg,coll["name"],str(part["standard"]))
def add_collection(self,blt):
self.blts.append(blt)
def check(self):
for blt in self.blts:
for part in blt["parts"]:
if not "table" in part.keys():
continue
table = part["table"]
self._check_row_length(blt,part,table)
self._check_monotonicity(blt,part,table)
self._check_order_of_magnitude(blt,part,table)
self._check_positivity(blt,part,table)
def _check_row_length(self,blt,part,table):
msg = "Inconsistent row sizes in row: %s"
row_length = len(table["columns"])
for key in table["data"].keys():
if len(table["data"][key]) != row_length:
error(msg % key,blt["collection"],part)
def _check_monotonicity(self,blt,part,table):
msg = "Non-monotonuous entries in row: %s"
rows = [list(row[1]) for row in sorted(table["data"].iteritems(),key=lambda x: float(x[0][1:]))]
n = len(rows)
m = len(rows[0])
for i in range(1,m):
direction = None
last_value = None
for j in range(1,n):
if rows[j][i] is None:
continue
current_value = rows[j][i]
if direction is None and not last_value is None:
#positive if ascending, negative if descending
direction = current_value - last_value
elif (not direction is None) and direction*current_value < direction*last_value:
self.error(msg % j,blt["collection"],part)
last_value = rows[j][i]
def _check_order_of_magnitude(self,blt,part,table):
msg = "Unusually magnitude for entry in row: %s"
rows = [list(row[1]) for row in sorted(table["data"].iteritems(),key=lambda x: float(x[0][1:]))]
n = len(rows)
m = len(rows[0])
for i in range(1,m):
last_value = None
for j in range(1,n):
if rows[j][i] is None:
last_value = None
continue
elif not last_value is None:
ratio = rows[j][i]/last_value
#threshholds are arbitrary, but small enough to catch shifted decimal points
if ratio > 7.5 or 40*ratio < 3.:
self.error(msg % j, blt["collection"],part)
last_value = rows[j][i]
def _check_positivity(self,blt,part,table):
msg = "Negative entry in row: %s"
rows = [list(row[1]) for row in sorted(table["data"].iteritems(),key=lambda x: float(x[0][1:]))]
n = len(rows)
m = len(rows[0])
for i in range(1,m):
for j in range(1,n):
if rows[j][i] is None:
continue
if rows[j][i] < 0:
self.error(msg % j, blt["collection"],part)
class CollectionChecker:
#List of accepted licenses
licenses = {
"CC-BY-NC-SA" : "http://creativecommons.org/licenses/by-nc-sa/3.0/",
"CC-BY-SA" : "http://creativecommons.org/licenses/by-sa/3.0/"
}
def __init__(self):
self.blts = []
self.errors = 0
def error(self,msg,coll):
print "%s coll: %s" % (msg,coll["name"])
def add_collection(self,blt):
self.blts.append(blt)
def check(self):
for blt in self.blts:
self._check_license(blt)
self._check_author(blt)
def _check_license(self,blt):
license = blt["collection"]["license"]
name, url = (part.strip("> ") for part in license.split("<"))
if not name in self.licenses.keys():
self.error("Unknown license: %s" % name,blt["collection"])
elif self.licenses[name] != url:
self.error("Wrong url for license %s: %s" % (name,url),blt["collection"])
def _check_author(self,blt):
author = blt["collection"]["author"]
name, email = (part.strip("> ") for part in author.split("<"))
#very rough check
if not "@" in email:
self.error("Invalid mail address in author: %s" % author, blt["collection"])
files = listdir('blt')
checkers = [TableChecker(),CollectionChecker()]
for file in files:
if file[-4:] == ".blt":
coll = load_collection(file)
for checker in checkers:
checker.add_collection(coll)
for checker in checkers:
checker.check()