Skip to content

Instantly share code, notes, and snippets.

@bchartoff
Created May 19, 2016 14:29
Show Gist options
  • Save bchartoff/e69735f0d1cbb0999543aa053f640418 to your computer and use it in GitHub Desktop.
Save bchartoff/e69735f0d1cbb0999543aa053f640418 to your computer and use it in GitHub Desktop.
import csv
import json
import subprocess
jData = []
remap = {"Custodial Arrest":"custodial","NonCustodial Arrest":"noncustodial","Contact":"contact"}
cr = csv.reader(open("data/source_data.csv",'rU'))
#Reformat source csv
head = cr.next()
iData = {}
for row in cr:
person = int(row[2])
offense = remap[row[0]]
date = int(row[1])
if(str(row[3]) != ''):
los = int(row[3])
else:
los = -99
if person not in iData:
iData[person] = ["none"]*91
iData[person][date] = offense
if(los != -99):
for i in range(date+1, date+1+los):
try:
iData[person][i] = "jail"
except IndexError:
break
for person in iData:
obj = {"id": person}
obj["vals"] = iData[person]
jData.append(obj)
with open("graphic_data.json","w") as f:
json.dump(jData, f)
VALS = {"none":("(245,245,245)", "#f5f5f5", "srgb(245,245,245)"), "contact":("(22,150,210)", "#1696d2", "srgb(22,150,210)"), "custodial":("(85,183,72)", "#55b748", "srgb(85,183,72)"),"jail":("(253,191,17)", "#fdbf11", "srgb(253,191,17)"),"noncustodial":("(6,38,53)", " #062635", "srgb(6,38,53)")}
with open('graphic_data.txt', 'w') as f:
#write a .txt file which Imagemagick can convert to a png (a pixel map)
#Specify the dimensions and color space (1 color gets a 3x3 square of pixels, for sharper images)
f.write("# ImageMagick pixel enumeration: 273,1602,255,srgb\n")
for o in jData:
row = int(o["id"])
for i in range(0, len(o["vals"])):
v = o["vals"][i]
cols = VALS[v]
# 0,0: (251, 255, 255) #fbffff srgb(251, 255, 255)
for j in range(0,9):
R = i*3
C = row*3
tups = [(R, C),(R,C+1),(R,C+2),(R+1,C),(R+1,C+1),(R+1,C+2),(R+2,C),(R+2,C+1),(R+2,C+2)]
f.write("%i,%i: %s %s %s\n"%(tups[j][0], tups[j][1], cols[0], cols[1], cols[2]))
subprocess.call(["convert","graphic_data.txt","graphic.png"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment