-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
216 lines (172 loc) · 5.48 KB
/
utils.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
import difflib
import shelve
from datetime import datetime
import networkx as nx
import base64
from io import BytesIO
import matplotlib.pyplot as plt
from collections import OrderedDict
from collections import defaultdict
VOCAB = ["undergrad", "postgrad", "faculty", "academic",
"service", "software", "technology",
"consulting", "sales",
"natoc", "intoc", "indoc", "junoc",
"government", "agency", "survey", "localgov",
"mining",
"unemployed", "retired",
"startup", "self-employed",
"other", 'break',
]
def get_info(record):
"""
Take a single response and turn it into a list of careers.
Completely ignore the numbers for now.
"""
items = [tuple(i.strip().split()) for i in record.split(',')]
items = filter(None, items)
path, years = [], defaultdict(int)
for pair in items:
# Get employment.
m = difflib.get_close_matches(pair[0], VOCAB, n=1, cutoff=0.5)
if m:
job = m[0]
else:
job = 'other'
path.append(job)
# Get years.
try:
y = float(pair[1])
except ValueError: # Garbled number
y = 1
except IndexError: # None provided
y = 1
years[job] += y
return path, years
def store(record):
_ = store_entry(record)
path, years = get_info(record)
with shelve.open('edges') as db:
for pair in zip(path[:-1], path[1:]):
count = db.get(','.join(pair), 0)
db[','.join(pair)] = count + 1
with shelve.open('nodes') as db:
for k, v in years.items():
vi = db.get(k, 0)
db[k] = vi + v
with shelve.open('lasts') as db:
last = path[-1]
db[last] = db.get(last, 0) + 1
with shelve.open('lens') as db:
length = str(int(sum(years.values())))
db[length] = db.get(length, 0) + 1
return 'Thank you!'
def store_entry(data):
with open('log.txt', 'ta') as f:
d = datetime.utcnow().isoformat() + '\t'
f.write(d + data + '\n')
return 'Done'
def get_network(years):
"""
Get the network from the Shelf.
"""
G = nx.Graph()
G.add_nodes_from([(k, {'count': v}) for k, v in years.items()])
with shelve.open('edges') as db:
for e, w in db.items():
u, v = e.split(',')
G.add_edge(u, v, weight=w)
G.remove_nodes_from(list(nx.isolates(G)))
return G
def get_years():
with shelve.open('nodes') as db:
d = dict(db).copy()
return d
def get_lasts():
with shelve.open('lasts') as db:
d = dict(db)
return d
def chunks(lst, n):
for i in range(0, len(lst), n):
yield lst[i:i + n]
def get_lens():
with shelve.open('lens') as db:
d = dict(db)
bins = list(chunks([str(n) for n in range(50)], 5))
labels = ['<5', '5-9', '10-14', '15-19', '20-24', '25-29',
'30-34', '35-39', '40-44', '45-49', '>50']
data = [0 for L in labels]
for k, v in d.items():
for idx, row in enumerate(bins):
if k in row:
break
data[idx] += v
return OrderedDict((L, d) for L, d in zip(labels, data))
def plot_network(G, years, scale=10):
"""
Make a networkx plot and convert to base64-encoded string.
"""
edges = G.edges()
weights = [G[u][v]['weight'] for u, v in edges]
counts = [scale * nx.get_node_attributes(G, 'count')[u] for u in G.nodes()]
params = {
'node_size': counts,
'with_labels': True,
'verticalalignment': 'bottom',
'width': weights,
}
pos = nx.spring_layout(G)
fig = plt.figure(figsize=(12, 12))
nx.draw(G, pos, **params)
# Save as base64 string.
handle = BytesIO()
plt.savefig(handle, format='png', facecolor=fig.get_facecolor())
plt.close()
handle.seek(0)
figdata_png = base64.b64encode(handle.getvalue())
return figdata_png.decode('utf8')
def plot_bars(data, drop=False, sort=False, log=False, title=True, lpos=None):
"""
Generic bar plotting function. Does all the plots.
"""
if drop:
_ = data.pop('undergrad', None)
_ = data.pop('retired', None)
_ = data.pop('unemployed', None)
_ = data.pop('break', None)
labels = list(data.keys())
values = list(data.values())
if sort:
labels = [l for _, l in sorted(zip(values, labels), reverse=True)]
values = sorted(values, reverse=True)
y = list(range(len(values)))
y_min, y_max = y[0]-0.75, y[-1]+0.75
fig, ax = plt.subplots(figsize=(8, 8))
_ = ax.barh(y, values, color='orange', align='center', edgecolor='none')
ax.set_yticks(y)
if log:
ax.set_xscale('log')
ax.set_yticklabels(labels, size=12)
ax.set_ylim(y_max, y_min) # Label top-down.
ax.grid(c='black', alpha=0.15, which='both')
ax.patch.set_facecolor("white")
fig.patch.set_facecolor("none")
if title is True:
t = "{:.2f} person-careers of experience".format(sum(values)/40)
elif title:
t = title
else:
t = ""
ax.set_title(t)
if lpos is None:
lpos = min(values)
for i, d in enumerate(values):
ax.text(lpos, i, "{}".format(int(d)), va='center', size=12)
plt.tight_layout()
# Put in memory.
handle = BytesIO()
plt.savefig(handle, format='png', facecolor=fig.get_facecolor())
plt.close()
# Encode.
handle.seek(0)
figdata_png = base64.b64encode(handle.getvalue()).decode('utf8')
return figdata_png