-
Notifications
You must be signed in to change notification settings - Fork 1
/
tikztable.py
220 lines (191 loc) · 7.89 KB
/
tikztable.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
#!/usr/bin/env python
##############################################################################
## ##
## smtlib_schanda ##
## ##
## Copyright (C) 2017, Altran UK Limited ##
## ##
## This file is part of smtlib_schanda. ##
## ##
## smtlib_schanda is free software: you can redistribute it and/or modify ##
## it under the terms of the GNU General Public License as published by ##
## the Free Software Foundation, either version 3 of the License, or ##
## (at your option) any later version. ##
## ##
## smtlib_schanda 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 General Public License for more details. ##
## ##
## You should have received a copy of the GNU General Public License ##
## along with smtlib_schanda. If not, see <http://www.gnu.org/licenses/>. ##
## ##
##############################################################################
COL_AWARD_HIGH = "col_award_high"
COL_AWARD_LOW = "col_award_low"
COL_ERROR = "col_error"
class TikzTable(object):
COLORING_OPTIONS = (COL_AWARD_HIGH, COL_AWARD_LOW, COL_ERROR)
def __init__(self, title, columns,
noncomp=[],
col_fmt_fn=None,
col_rule="ZColor2",
fmt_good=None,
fmt_bad=None,
fmt_ncomp=None,
transposed=False):
assert type(title) is str
assert type(columns) is list
self.col_rule = col_rule
if fmt_good is None:
self.fmt_good = lambda x: "{\\color{ZColorGreen}%s}" % x
else:
self.fmt_good = fmt_good
if fmt_bad is None:
self.fmt_bad = lambda x: "{\\color{ZColorRed}%s}" % x
else:
self.fmt_bad = fmt_bad
if fmt_ncomp is None:
self.fmt_ncomp = lambda x: "{\\color{ZGray3}%s}" % x
else:
self.fmt_ncomp = fmt_ncomp
self.cat_title = title
self.columns = columns
self.rows = []
self.competing = {c: c not in noncomp
for c in columns}
self.transposed = transposed
if col_fmt_fn is None:
self.col_fmt_fn = lambda x: x
else:
self.col_fmt_fn = col_fmt_fn
def add_row(self, title, data, format_fn, coloring, notes={}):
assert type(title) is str
assert type(data) is dict
assert coloring is None or coloring in TikzTable.COLORING_OPTIONS
competing_data = {col : item
for col, item in data.iteritems()
if col in self.columns and self.competing[col]}
row = {"kind" : "row",
"title" : title,
"data" : {}}
for col, item in data.iteritems():
if col not in self.columns:
continue
txt = format_fn(item)
if not self.competing[col]:
txt = self.fmt_ncomp(txt)
elif coloring == COL_ERROR:
if item == 0.0:
txt = self.fmt_good(txt)
else:
txt = self.fmt_bad(txt)
elif coloring == COL_AWARD_HIGH:
if item == max(competing_data.itervalues()):
txt = self.fmt_good(txt)
elif coloring == COL_AWARD_LOW:
if item == min(competing_data.itervalues()):
txt = self.fmt_good(txt)
if col in notes:
txt += "$^%s$" % notes[col]
row["data"][col] = txt
self.rows.append(row)
def start_footer(self):
self.rows.append({"kind" : "rule"})
def emit(self):
def tr_x(x):
if x == 0:
return 0.0
else:
return 2.0 + float(x - 1) * 0.9
def tr_y(y):
return float(-y) * 0.35
def emit_box(x, y, txt):
if self.transposed:
x, y = y, x
title = (y == 0) and x > 0
n_atr = ["anchor=west"]
# n_atr.append("fill=black!10")
if title:
n_atr.append("rotate=65")
n = r"\node[%s]" % ",".join(n_atr)
final_x = tr_x(x)
final_y = tr_y(y)
if title:
final_x += 0.25
n += " at (%.3f, %.3f)" % (final_x, final_y)
n += " {\\tiny %s};" % txt
rv.append(n)
def emit_hrule(after_y):
if self.transposed:
width = len(self.rows)
else:
width = len(self.columns)
r = r"\draw[%s,thick]" % self.col_rule
y = (tr_y(after_y) + tr_y(after_y + 1)) / 2.0 + 0.05
r += " (%.3f, %.3f)" % (tr_x(0), y)
r += " -- (%.3f, %.3f)" % (tr_x(width + 1), y)
r += ";"
rv.append(r)
rv = [r"\begin{tikzpicture}"]
emit_box(0, 0, self.cat_title)
for col_id, col in enumerate(self.columns):
emit_box(col_id + 1, 0, self.col_fmt_fn(col))
emit_hrule(0)
row_id = 0
for row in self.rows:
if row["kind"] == "row":
row_id += 1
emit_box(0, row_id, row["title"])
for col, txt in row["data"].iteritems():
emit_box(self.columns.index(col) + 1,
row_id,
txt)
elif row["kind"] == "rule":
emit_hrule(row_id)
rv.append(r"\end{tikzpicture}")
return "\n".join(rv)
def emit_plain(self, filename, cwidth=None):
tbl = {}
def put(x, y, txt):
if self.transposed:
x, y = y, x
if y not in tbl:
tbl[y] = {x: txt}
else:
tbl[y][x] = txt
# Heading
put(0, 0, self.cat_title)
for i, txt in enumerate(self.columns):
put(i+1, 0, self.col_fmt_fn(txt))
# Rows
row_id = 0
for row in self.rows:
if row["kind"] == "row":
row_id += 1
put(0, row_id, row["title"])
for col, txt in row["data"].iteritems():
put(self.columns.index(col) + 1, row_id, txt)
with open(filename, "w") as fd:
width = len(tbl[0])
na = r"\noalign{\smallskip}"
rule = r"\hline"
if cwidth is None:
fd.write("\\begin{tabular}{%s}\n" % ("l" * width))
else:
col = "p{%s}" % cwidth
fd.write("\\begin{tabular}{l|%s}\n" % (col * (width - 1)))
fd.write(rule + na + "\n")
for row_id in xrange(len(tbl)):
for col_id in xrange(width):
if col_id > 0:
fd.write(" & ")
if col_id not in tbl[row_id]:
fd.write("")
else:
fd.write(tbl[row_id][col_id])
fd.write("\\\\\n")
if row_id == 0:
fd.write(na + rule + na + "\n")
fd.write(rule + "\n")
fd.write("\\end{tabular}\n")