-
Notifications
You must be signed in to change notification settings - Fork 1
/
visualize.py
237 lines (214 loc) · 9.54 KB
/
visualize.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
import csv
import json
import string
import re
import logging
def clean_uri(uri):
uri = re.sub("^<", "", uri)
uri = re.sub(">$", "", uri)
return uri
def dict_to_aoa(self, valdict):
valarr = []
for key, val in valdict.iteritems():
this = []
this.append(key)
this.append(val)
valarr.append(this)
return sorted(valarr, key=first_number_column)
def first_number_column_index(arr):
i = 0
for item in arr:
try:
float(item)
break
except ValueError:
i += 1
return i
def first_number_column(arr):
return arr[first_number_column(arr)]
class Vis:
def __init__(self, **kwargs):
logging.basicConfig(level=logging.DEBUG)
self.output_dir = kwargs['output_dir']
self.template_dir = kwargs['template_dir']
self.analyses_dir = kwargs['analyses_dir']
self.datasets = kwargs['datasets']
self.numbers_per_dataset = {'total': {}}
for ds in self.datasets:
self.numbers_per_dataset[ds] = {}
def run_template(self, tpl_name, filename, **kwargs):
out_path = self.output_dir + '/' + filename
tpl_path = self.template_dir + '/' + tpl_name + '.html'
tpl = string.Template(open(tpl_path, 'rb').read())
out_str = tpl.substitute(kwargs)
with open(out_path, 'wb') as out_file:
out_file.write(out_str)
def average_across_datasets_no(self, analysis):
logging.debug("Average '%s'" % analysis)
valdict = {}
headers = [['string', 'dataset'], ['number', 'average']]
for dataset in self.datasets:
with open(self.analyses_dir + dataset + '-count-' + analysis + '.rq.tsv', 'rb') as tsvin:
dataset_stmts = 0
dataset_total = 0
reader = csv.reader(tsvin, delimiter='\t')
first_line = True
for row_tsv in reader:
no_index = 0
if len(row_tsv) > 1: no_index = 1
if first_line:
first_line = False
else:
dataset_total += int(row_tsv[no_index])
dataset_stmts += 1
valdict[dataset] = dataset_total / float(dataset_stmts)
valarr = dict_to_aoa(valdict)
for vis_type in ['pie', 'bar']:
self.run_template(vis_type,
'average_' + analysis + '_' + vis_type + '.html',
TABLE_ROWS=json.dumps(valarr),
TABLE_COLUMNS=json.dumps(headers),
TITLE=analysis)
def distribution_no(self, analysis):
valdict = {}
headers = [['string', 'number'], ['number', 'frequency']]
for dataset in self.datasets:
valdict_per_dataset = {}
try:
with open(self.analyses_dir + dataset + '-count-' + analysis + '.rq.tsv', 'rb') as tsvin:
reader = csv.reader(tsvin, delimiter='\t')
first_line = True
for row_tsv in reader:
no_index = 0
if len(row_tsv) > 1: no_index = 1
if first_line: first_line = False
else:
try:
valdict[row_tsv[no_index]] += 1
valdict_per_dataset[row_tsv[no_index]] += 1
except KeyError:
valdict[row_tsv[no_index]] = 1
valdict_per_dataset[row_tsv[no_index]] = 1
valarr_per_dataset = dict_to_aoa(valdict_per_dataset)
for vis_type in ['pie', 'bar']:
self.run_template(vis_type,
dataset + '_dist_' + analysis + '_' + vis_type + '.html',
TABLE_ROWS=json.dumps(valarr_per_dataset),
TABLE_COLUMNS=json.dumps(headers),
TITLE=analysis)
except IOError:
pass
valarr = dict_to_aoa(valdict)
for vis_type in ['pie', 'bar']:
self.run_template(vis_type,
'dist_' + analysis + '_' + vis_type + '.html',
TABLE_ROWS=json.dumps(valarr),
TABLE_COLUMNS=json.dumps(headers),
TITLE=analysis)
def collate_no(self, analysis):
logging.info("Collate '%s'" % analysis)
valdict = {}
headers = [['string','foo'],['number','bar']]
for dataset in self.datasets:
valdict_per_dataset = {}
with open(self.analyses_dir + dataset + '-count-' + analysis + '.rq.tsv', 'rb') as tsvin:
reader = csv.reader(tsvin, delimiter='\t')
first_line = True
for row_tsv in reader:
if len(row_tsv) == 1:
if first_line:
first_line = False
continue
key = dataset
val = int(row_tsv[0])
try:
valdict[key] += val
valdict_per_dataset[key] += val
except KeyError:
valdict[key] = val
valdict_per_dataset[key] = val
elif len(row_tsv) == 2:
if first_line:
first_line = False
continue
key = self.clean_uri(row_tsv[0])
val = int(row_tsv[1])
try:
valdict[key] += val
valdict_per_dataset[key] += val
except KeyError:
valdict[key] = val
valdict_per_dataset[key] = val
elif len(row_tsv) == 3:
if first_line:
first_line = False
continue
key = self.clean_uri(row_tsv[0]) + self.clean_uri(row_tsv[1])
val = int(row_tsv[2])
try:
valdict[key] += val
valdict_per_dataset[key] += val
except KeyError:
valdict[key] = val
valdict_per_dataset[key] = val
for vis_type in ['pie', 'bar']:
self.run_template(vis_type,
dataset + '_frequency' + analysis + '_' + vis_type + '.html',
TABLE_ROWS=json.dumps(dict_to_aoa(valdict_per_dataset)),
TABLE_COLUMNS=json.dumps(headers),
TITLE=analysis)
for vis_type in ['pie', 'bar']:
self.run_template(vis_type,
'frequency' + analysis + '_' + vis_type + '.html',
TABLE_ROWS=json.dumps(dict_to_aoa(valdict)),
TABLE_COLUMNS=json.dumps(headers),
TITLE=analysis)
def visualize_by_number_of_lines(self):
print
def visualize_map(self, analysis):
headers = [['number', 'Lat'], ['number', 'Lon'], ['string', 'uri']]
aoa = []
with open(self.analyses_dir + analysis + '.rq.tsv', 'rb') as tsvin:
reader = csv.reader(tsvin, delimiter='\t')
first_line = True
for row_tsv in reader:
if len(row_tsv) < 3:
logging.debug(row_tsv)
if first_line: first_line = False
else:
this_row = []
try:
this_row.append(float(row_tsv[3]))
this_row.append(float(row_tsv[2]))
this_row.append(row_tsv[0])
except IndexError:
logging.debug(this_row)
logging.debug(row_tsv)
aoa.append(this_row)
self.run_template('map',
'map_geonames.html',
TABLE_ROWS=json.dumps(aoa),
TABLE_COLUMNS=json.dumps(headers),
TITLE=analysis)
def visualize_total(self, tsv_fname, key_group, key_number):
valdict = {}
with open(self.analyses_dir + tsv_fname + ".tsv", "rb") as tsvin:
reader = csv.DictReader(tsvin)
for row in reader:
valdict[row[key_group]] = row[key_number]
return dict_to_aoa(valdict)
if __name__ == '__main__':
vis = Vis(
output_dir='out/',
template_dir='tpl/',
datasets='bbawdta geigeidigital mpiwgharriot mpiwgrara mpiwgrarafulltextsample onbabo onbcodices uberdingler ubffmsammlungen uibwab'.split(' '),
analyses_dir='analysis/',
)
# vis.visualize_total('numbers-per-dataset', '?nr_stmt_total')
# vis.collate_no('hostnames')
# vis.collate_no('license')
# # vis.collate_no('predicate-object-equal-statements')
# vis.collate_no('triples-per-dataset')
# vis.distribution_no('statements-per-resource')
# vis.average_across_datasets_no('statements-per-resource')
# vis.visualize_map('find-geonames')