-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot_viz.py
308 lines (268 loc) · 12.2 KB
/
dot_viz.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# -*- coding: utf-8 -*-
import argparse
import math
import os
from PIL import Image, ImageDraw, ImageFont
from pprint import pprint
import sys
import lib.io_utils as io
import lib.list_utils as lu
import lib.math_utils as mu
# input
parser = argparse.ArgumentParser()
parser.add_argument('-in', dest="INPUT_FILE", default="data/processed/all_normalized.csv", help="File generated by normalize_data.py")
parser.add_argument('-min', dest="MIN_ITEM_COUNT", default=100, type=int, help="Minumum value needed to not be considered 'Other'")
parser.add_argument('-width', dest="IMAGE_WIDTH", default=1920, type=int, help="Width of image in px")
parser.add_argument('-margin', dest="MARGIN", default=16, type=int, help="Base margin in px")
parser.add_argument('-dotwidth', dest="DOT_WIDTH", default=4, type=int, help="Dot width in px")
parser.add_argument('-minalpha', dest="MIN_ALPHA", default=50, type=int, help="Min dot alpha (0-255)")
parser.add_argument('-fontsize', dest="FONT_SIZE", default=16, type=int, help="Base font size in pixels")
parser.add_argument('-out', dest="OUTPUT_FILE", default="output/viz/%s.png", help="Output image file pattern")
a = parser.parse_args()
# Make sure output dirs exist
io.makeDirectories([a.OUTPUT_FILE])
TITLE_FONT_SIZE = int(round(a.FONT_SIZE * 2.8))
SECTION_FONT_SIZE = int(round(a.FONT_SIZE * 2))
font = ImageFont.truetype(font="fonts/Open_Sans/OpenSans-Bold.ttf", size=a.FONT_SIZE)
fontSection = ImageFont.truetype(font="fonts/Open_Sans/OpenSans-Bold.ttf", size=SECTION_FONT_SIZE)
fontTitle = ImageFont.truetype(font="fonts/Open_Sans/OpenSans-Bold.ttf", size=TITLE_FONT_SIZE)
fieldNames, items = io.readCsv(a.INPUT_FILE)
itemCount = len(items)
collections = [
{
"key": "Acquisition Year",
"confidenceKey": "Acquisition Year Confidence",
"groupKey": "Acquisition Era"
},{
"key": "Acquisition Type",
"confidenceKey": "Acquisition Type Confidence",
"noMinumum": True
},{
"key": "Country",
"confidenceKey": "Country Confidence",
"groupKey": "Region"
},{
"key": "Donor",
"confidenceKey": "Donor Confidence"
},{
"key": "Hall",
"emptyLabel": "Not displayed in hall",
"noMinumum": True
}
]
for i, item in enumerate(items):
if item["Acquisition Year"] >= 9999:
items[i]["Acquisition Year"] = None
items = mu.addNormalizedValues(items, "Acquisition Year", "nalpha")
for i, item in enumerate(items):
if item["Acquisition Year"] is None:
items[i]["Acquisition Year"] = 9999
# Process data into sections and groups within each section
for i, col in enumerate(collections):
# Break items into sections
itemsBySection = []
groupBy = None
if "groupKey" in col:
groupBy = col["groupKey"]
itemsBySection = lu.groupList([item for item in items if groupBy in item and len(str(item[groupBy])) > 0], groupBy)
itemsBySection = sorted(itemsBySection, key=lambda k: k[groupBy])
else:
itemsBySection = [
{"items": items, "count": itemCount}
]
# Break groups down further into groups
sections = []
for sectionItems in itemsBySection:
sectionItemsByGroup = lu.groupList(sectionItems["items"], col["key"], sort=True)
# group by year if year
if "Year" in col["key"]:
sectionItemsByGroup = sorted(sectionItemsByGroup, key=lambda k: k[col["key"]])
# group items with less than minimum into "Other" category
else:
validGroups = []
otherGroup = {"items": [], "count": 0}
otherGroup[col["key"]] = "Other"
otherCount = 0
for sectionGroupItems in sectionItemsByGroup:
if sectionGroupItems["count"] >= a.MIN_ITEM_COUNT or "noMinumum" in col:
validGroups.append(sectionGroupItems)
else:
otherGroup["items"] += sectionGroupItems["items"]
otherGroup["count"] += sectionGroupItems["count"]
otherCount += 1
otherGroup[col["key"]] = "%s Others" % otherCount
if otherCount > 0:
validGroups.append(otherGroup)
sectionItemsByGroup = validGroups
# add confidence
if "confidenceKey" in col:
confidenceKey = col["confidenceKey"]
# each group, sort by confidence
for k, sectionGroupItems in enumerate(sectionItemsByGroup):
sectionItemsByGroup[k]["items"] = sorted(sectionGroupItems["items"], key=lambda k: (-k[confidenceKey], k["Acquisition Year"]))
else:
for k, sectionGroupItems in enumerate(sectionItemsByGroup):
sectionItemsByGroup[k]["items"] = sorted(sectionGroupItems["items"], key=lambda k: k["Acquisition Year"])
# add group titles
for k, sectionGroupItems in enumerate(sectionItemsByGroup):
groupTitle = str(sectionGroupItems[col["key"]]) if sectionGroupItems[col["key"]] is not None else ""
if groupTitle == "9999":
groupTitle = ""
if len(groupTitle) < 1:
groupTitle = "Unknown" if "emptyLabel" not in col else col["emptyLabel"]
sectionItemsByGroup[k]["title"] = groupTitle
section = {}
if groupBy is not None:
section["title"] = str(sectionItems[groupBy])
section["groups"] = sectionItemsByGroup
sections.append(section)
collections[i]["sections"] = sections
# Determine pixel values, positions, dimensions
chunkSize = 100
chunkColCount = int(math.sqrt(chunkSize))
chunkWidth = chunkColCount * a.DOT_WIDTH + (chunkColCount-1)
maxChunksPerRow = int(((a.IMAGE_WIDTH - a.MARGIN * 2) + 2.0) / (2 + chunkWidth))
# print(maxChunksPerRow)
drawData = []
for i, col in enumerate(collections):
confidenceKey = col["confidenceKey"] if "confidenceKey" in col else None
sectionX = 0
sectionY = TITLE_FONT_SIZE + a.MARGIN * 2
colSections = []
print("Calculating %s" % col["key"])
# for each section
for j, section in enumerate(col["sections"]):
groupX = 0
groupY = SECTION_FONT_SIZE + a.MARGIN * 2
groupRowHeight = 0
if j == 0 and "title" not in section or section["title"] is None:
sectionY = a.MARGIN * 2
# for each group
sectionGroups = []
for k, group in enumerate(section["groups"]):
groupItems = group["items"]
# determine group size based on chunks
chunkCount = int(math.ceil(1.0 * group["count"] / chunkSize))
chunkCols = min(int(math.ceil(math.sqrt(chunkCount))), maxChunksPerRow)
chunkRows = int(math.ceil(1.0 * chunkCount / chunkCols))
# for each chunk
groupChunks = []
for l in range(chunkCount):
# for each dot
chunkDots = []
for m in range(chunkSize):
groupItemsIndex = l * chunkSize + m
if groupItemsIndex >= len(groupItems):
break
# determine pixel color
item = groupItems[groupItemsIndex]
r = g = b = int(round(mu.lerp((255-a.MIN_ALPHA, 0), item["nalpha"])))
if confidenceKey is not None:
confidence = item[confidenceKey]
if confidence < 1.0:
if item["Acquisition Year"] >= 9999:
r = g = b = 0
r = int(round(mu.lerp((255.0, r), confidence)))
# determine position
dotRow = int(1.0 * m / chunkColCount)
dotCol = m % chunkColCount
dotDrawData = {
"x": dotCol * a.DOT_WIDTH + dotCol-1,
"y": dotRow * a.DOT_WIDTH + dotRow-1,
"width": a.DOT_WIDTH,
"height": a.DOT_WIDTH,
"color": (r, g, b, 255)
}
chunkDots.append(dotDrawData)
chunkRow = int(1.0 * l / chunkCols)
chunkCol = l % chunkCols
chunkDrawData = {
"x": chunkCol * chunkWidth + (chunkCol-1) * 2,
"y": chunkRow * chunkWidth + (chunkRow-1) * 2,
"width": max([d["x"]+d["width"] for d in chunkDots]),
"height": max([d["y"]+d["height"] for d in chunkDots]),
"dots": chunkDots
}
groupChunks.append(chunkDrawData)
groupWidth = max([c["x"]+c["width"] for c in groupChunks])
groupDataWidth = groupWidth
groupHeight = max([c["y"]+c["height"] for c in groupChunks])
groupTitleW, groupTitleH = font.getsize(group["title"])
groupWidth = max(groupWidth, groupTitleW)
if groupWidth > (a.IMAGE_WIDTH - a.MARGIN * 2):
print("Group width too big: %s" % groupWidth)
# sys.exit()
# go to next row
if groupX > 0 and (groupX + groupWidth) > (a.IMAGE_WIDTH - a.MARGIN):
groupX = 0
groupY += groupRowHeight + a.MARGIN * 3 + a.FONT_SIZE
groupRowHeight = 0
groupDrawData = {
"x": groupX,
"y": groupY,
"width": groupWidth,
"height": groupHeight,
"dataWidth": groupDataWidth,
"titleWidth": groupTitleW,
"title": group["title"],
"chunks": groupChunks
}
sectionGroups.append(groupDrawData)
groupX += groupWidth + a.MARGIN
if groupHeight > groupRowHeight:
groupRowHeight = groupHeight
sectionWidth = max([g["x"]+g["width"] for g in sectionGroups])
sectionHeight = max([g["y"]+g["height"] for g in sectionGroups])
sectionDrawData = {
"x": sectionX,
"y": sectionY,
"title": section["title"] if "title" in section else None,
"width": sectionWidth,
"height": sectionHeight,
"groups": sectionGroups
}
sectionY += sectionHeight + a.MARGIN * 2 + SECTION_FONT_SIZE
colSections.append(sectionDrawData)
drawData.append({
"title": col["key"],
"x": a.MARGIN,
"y": a.MARGIN,
"sections": colSections
})
# break
for col in drawData:
imW = a.IMAGE_WIDTH
imH = max([s["y"]+s["height"] for s in col["sections"]]) + a.MARGIN + TITLE_FONT_SIZE + a.MARGIN * 2
im = Image.new('RGBA', (imW, imH), (255,255,255,0))
title = col["title"]
imageFn = a.OUTPUT_FILE % title
draw = ImageDraw.Draw(im)
# draw title
titleW, titleH = fontTitle.getsize(col["title"])
titleX = int(round((imW - titleW) * 0.5))
titleY = col["y"]
draw.text((titleX, titleY), col["title"], font=fontTitle, fill=(0,0,0,255))
for section in col["sections"]:
# draw section title
if "title" in section and section["title"] is not None:
sTitleW, sTitleH = fontSection.getsize(section["title"])
sTitleX = int(round((imW - sTitleW) * 0.5))
sTitleY = col["y"]+section["y"]
draw.text((sTitleX, sTitleY), section["title"], font=fontSection, fill=(0,0,0,255))
for group in section["groups"]:
# draw group title
gTitleW = group["titleWidth"]
gWidth = group["width"]
gTitleX = col["x"]+section["x"]+group["x"] + int(round((gWidth - gTitleW) * 0.5))
if group["dataWidth"] < gWidth:
gTitleX = col["x"]+section["x"]+group["x"]
gTitleY = col["y"]+section["y"]+group["y"]
draw.text((gTitleX, gTitleY), group["title"], font=font, fill=(0,0,0,255))
# draw dots
for chunk in group["chunks"]:
for dot in chunk["dots"]:
x0 = col["x"]+section["x"]+group["x"]+chunk["x"]+dot["x"]
y0 = col["y"]+section["y"]+group["y"]+chunk["y"]+dot["y"] + a.FONT_SIZE + a.MARGIN
draw.rectangle([x0, y0, x0+dot["width"]-1, y0+dot["height"]-1], fill=dot["color"])
im.save(imageFn, "PNG")
print("Saved %s" % imageFn)