-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdberg_ml.py
487 lines (415 loc) · 17 KB
/
dberg_ml.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import matplotlib as mpl
mpl.use('agg')
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import networkx as nx
from sys import argv
import pandas as pd
import numpy as np
import pylab
import argparse
import numpy.ma as ma
from choice_functions import dberg_likelihood
from decay_functions import *
import os
from collections import defaultdict
MIN_PHEROMONE = 0
MIN_DETECTABLE_PHEROMONE = 0
INIT_WEIGHT = 0
IND_PLOT = False
NOCUT = ['6', '7a', '7c', '8a', '8b', '9b', '14','15', '16', '17', '21a', '21b', '23a', '23d']
CUT = ['7b', '9a', '9c', '9d', '10', '11', '12', '13', '18', '19', '20', '21c', '22', '23b', '23c', '23e', '23f']
ALL = NOCUT + CUT
NOCUT2 = ['6', '7a', '7c', '9b', '14','15', '16', '21a', '21b', '23a', '23d']
ALL2 = NOCUT2 + CUT
STRATEGY = 'dberg'
STRATEGIES = [STRATEGY]
datasets_dict = {'nocut' : NOCUT, 'nocut2' : NOCUT2, 'cut' : CUT, 'all' : ALL, 'all2' : ALL2}
#DATASETS_DIR = 'datasets/reformated_csv'
DATASETS_DIR = 'datasets/reformated_csv2'
OUT_DIR = 'ml_plots'
ML_OUTFILE = '%s/dberg_ml.csv' % OUT_DIR
DECAY_RATE = 0.01
DECAY_TYPE = 'exp'
def reset_graph(G, init_weight=INIT_WEIGHT):
for u, v in G.edges_iter():
G[u][v]['weight'] = init_weight
G[u][v]['units'] = [init_weight]
def make_graph(sources, dests, ghost=False, init_weight=INIT_WEIGHT):
assert len(sources) == len(dests)
G = nx.Graph()
for i in xrange(len(sources)):
source = sources[i]
dest = dests[i]
G.add_edge(source, dest)
G[source][dest]['weight'] = init_weight
G[source][dest]['units'] = [init_weight]
if ghost:
max_degree = 0
for u in G.nodes_iter():
max_degree = max(max_degree, G.degree(u))
for u in G.nodes():
if G.degree(u) == 1:
v = 'canopy_%s' % u
G.add_edge(u, v)
G[u][v]['weight'] = init_weight
G[u][v]['units'] = [init_weight]
#print G.nodes()
#print G.edges()
return G
def check_graph(G, check_units=False):
for u, v in G.edges_iter():
weight = G[u][v]['weight']
assert weight >= MIN_PHEROMONE
if check_units:
units = G[u][v]['units']
if len(units) == 0:
assert weight == MIN_PHEROMONE
else:
wt = 0
for unit in G[u][v]['units']:
assert unit > MIN_PHEROMONE
wt += unit
assert wt == weight
def param_likelihood(choices, exponent, offset, decay=DECAY_RATE,\
decay_type=DECAY_TYPE, G=None, ghost=False):
assert 0 <= decay <= 1
explore = 1.0 / exponent
#assert 0 <= explore <= 1
df = pd.read_csv(choices, header=None, names=['source', 'dest', 'dt'], skipinitialspace=True)
df['dt'] = pd.to_datetime(df['dt'])
df.sort_values(by='dt', inplace=True)
sources = list(df['source'])
dests = list(df['dest'])
dts = list(df['dt'])
assert len(sources) == len(dests)
if G == None:
G = make_graph(sources, dests, ghost)
else:
reset_graph(G)
decay_func = get_decay_func_graph(decay_type)
check_units = False
if decay_type == 'linear':
check_units = True
log_likelihood = 0
'''
G[sources[0]][dests[0]]['weight'] += 1
if decay_type == 'linear':
G[sources[0]][dests[0]]['units'].append(1)
'''
G2 = G.copy()
max_degree = 0
for u in G.nodes():
max_degree = max(max_degree, len(G.neighbors(u)))
for i in xrange(0, len(sources)):
#check_graph(G, check_units)
#check_graph(G2, check_units)
source = sources[i]
dest = dests[i]
log_likelihood += np.log(dberg_likelihood(G, source, dest, explore=explore, offset=offset))
if log_likelihood == float("-inf"):
break
curr = dts[i]
prev = curr
if i > 0:
prev = dts[i - 1]
if curr != prev:
diff = curr - prev
seconds = diff.total_seconds()
G = G2
decay_func(G, decay, seconds)
G2 = G.copy()
add_amount = 1
if G[source][dest]['weight'] <= MIN_DETECTABLE_PHEROMONE:
pass #add_amount *= 2
G2[source][dest]['weight'] += add_amount
if decay_type == 'linear':
G2[source][dest]['units'].append(add_amount)
return log_likelihood
def param_likelihood2(choices, exponent, offset, decay=DECAY_RATE,\
decay_type=DECAY_TYPE, ghost=False):
assert 0 <= decay <= 1
explore = 1.0 / exponent
#assert 0 <= explore <= 1
graph_line = None
with open(choices) as f:
graph_line = f.readline()
graph_line = graph_line.strip('\n')
edges = graph_line.split(',')
G = nx.Graph()
for edge in edges:
edge = edge.strip()
u, v = edge.split('-')
u = u.strip()
v = v.strip()
G.add_edge(u, v)
reset_graph(G)
df = pd.read_csv(choices, header=None, names=['source', 'dest', 'dt'],\
skipinitialspace=True, skiprows=1)
df['dt'] = pd.to_datetime(df['dt'])
df.sort_values(by='dt', inplace=True)
sources = list(df['source'])
dests = list(df['dest'])
dts = list(df['dt'])
assert len(sources) == len(dests)
decay_func = get_decay_func_graph(decay_type)
check_units = False
if decay_type == 'linear':
check_units = True
log_likelihood = 0
'''
G[sources[0]][dests[0]]['weight'] += 1
if decay_type == 'linear':
G[sources[0]][dests[0]]['units'].append(1)
'''
G2 = G.copy()
max_degree = 0
for u in G.nodes():
max_degree = max(max_degree, len(G.neighbors(u)))
for i in xrange(0, len(sources)):
#check_graph(G, check_units)
#check_graph(G2, check_units)
source = sources[i]
dest = dests[i]
log_likelihood += np.log(dberg_likelihood(G, source, dest, explore=explore, offset=offset))
if log_likelihood == float("-inf"):
break
curr = dts[i]
prev = curr
if i > 0:
prev = dts[i - 1]
if curr != prev:
diff = curr - prev
seconds = diff.total_seconds()
G = G2
decay_func(G, decay, seconds)
G2 = G.copy()
add_amount = 1
if G[source][dest]['weight'] <= MIN_DETECTABLE_PHEROMONE:
pass #add_amount *= 2
G2[source][dest]['weight'] += add_amount
if decay_type == 'linear':
G2[source][dest]['units'].append(add_amount)
return log_likelihood
def max_likelihood_estimates(likelihoods, exponents, offsets):
max_likelihood = float("-inf")
max_values = []
pos = 0
for offset in offsets:
for exponent in exponents:
i, j = pos / len(exponents), pos % len(exponents)
pos += 1
likelihood = likelihoods[i, j]
if likelihood == float("-inf"):
continue
if likelihood > max_likelihood:
max_values = [(exponent, offset)]
max_likelihood = likelihood
elif max_likelihood == likelihood:
max_values.append((exponent, offset))
return max_likelihood, max_values
def likelihood_matrix(sheet, exponents, offsets, decay_rate=DECAY_RATE, decay_type=DECAY_TYPE,\
ghost=False):
likelihoods = pylab.zeros((len(offsets), len(exponents)))
G = None
choices = '%s/reformated_counts%s.csv' % (DATASETS_DIR, sheet)
pos = 0
for offset in offsets:
for exponent in exponents:
i, j = pos / len(exponents), pos % len(exponents)
'''
likelihood, G = param_likelihood(choices, exponent, offset,\
decay=decay_rate,\
decay_type=decay_type, G=G,\
ghost=ghost)
'''
likelihood = param_likelihood2(choices, exponent, offset,\
decay=decay_rate,\
decay_type=decay_type,\
ghost=ghost)
#likelihood = pos
likelihoods[i, j] = likelihood
pos += 1
return likelihoods
def make_title_str(max_likelihood, max_values):
title_str = ['max likelihood %f at:' % max_likelihood]
for explore, decay in max_values:
title_str.append('(e=%0.2f, o=%0.2f)' % (explore, decay))
title_str = '\n'.join(title_str)
return title_str
def likelihood_plot(likelihoods, exponents, offsets, outname):
pos = 0
x = []
y = []
for offset in offsetss:
best_exponents = []
best_likelihood = float("-inf")
for exponent in exponents:
i, j = pos / len(exponents), pos % len(exponents)
likelihood = likelihoods[i, j]
pos += 1
if np.isnan(likelihood):
continue
if likelihood > best_likelihood:
best_likelihood = likelihood
best_exponents = [exponent]
elif likelihood == best_likelihood:
best_exponents.append(exponents)
for best_exponent in best_exponents:
x.append(offset)
y.append(best_exponent)
pylab.figure()
pylab.scatter(x, y)
pylab.xlabel('offset')
pylab.ylabel('best exponent')
pylab.savefig(outname + '_plot.pdf', format='pdf', transparent=True, bbox_inches='tight')
pylab.close()
def likelihood_heat(likelihoods, max_likelihood, max_values, exponents,\
offsets, outname):
likelihoods = ma.masked_invalid(likelihoods)
title_str = make_title_str(max_likelihood, max_values)
print title_str
pylab.figure()
hm = pylab.pcolormesh(likelihoods, cmap='nipy_spectral')
curr_ax = pylab.gca()
curr_ax.axis('tight')
curr_ax.set_aspect('equal')
cb = pylab.colorbar(hm)
cb.ax.set_ylabel('log-likelihood', fontsize=20)
pylab.tick_params(which='both', bottom='off', top='off', left='off', right='off', \
labeltop='off', labelbottom='off', labelleft='off', labelright='off')
pylab.xlabel("explore probability (%0.2f - %0.2f)" % (min(exponents), max(exponents)), fontsize=20)
pylab.ylabel("pheromone decay (%0.2f-%0.2f)" % (min(offsets), max(offsets)), fontsize=20)
#pylab.title(title_str)
#pylab.savefig(outname + '.png', format="png", transparent=True, bbox_inches='tight')
pylab.savefig(outname + '_heat.pdf', format='pdf', transparent=True, bbox_inches='tight')
pylab.close()
#print 'convert %s.png %s.pdf' % (outname, outname)
#os.system('convert %s.png %s.pdf' % (outname, outname))
def write_likelihoods(likelihoods, sheet, num_lines, decay_type,\
decay_rate, ghost, exponents, offsets,\
outfile=ML_OUTFILE):
f = open(outfile, 'a')
pos = 0
for offset in offsets:
for exponent in exponents:
i, j = pos / len(exponents), pos % len(exponents)
likelihood = likelihoods[i, j]
pos += 1
f.write('%s, %d, %s, %s, %d, %f, %f, %f\n' % (sheet, num_lines,\
STRATEGY, decay_type,\
int(ghost), exponent,\
offset, likelihood))
f.close()
def ml_analysis(label, sheets, decay_types, decay_rate=DECAY_RATE, omin=0.01,\
omax=2, emin=0.01, emax=2, ostep=0.01, estep=0.01,\
cumulative=False, individual=False, out=False, ghost=False,\
heat=True, plot=False, write_file=False):
offsets = np.arange(omin, omax + ostep, ostep)
exponents = np.arange(emin, emax + estep, estep)
hist_file = open('%s/dberg_ml_hist.csv' % OUT_DIR, 'a')
for decay_type in decay_types:
print decay_type
out_str = 'dberg_ml_%s_%s' % (STRATEGY, decay_type)
cumulative_likelihoods = None
total_lines = 0
for sheet in sheets:
print sheet
num_lines = sum(1 for line in open('%s/reformated_counts%s.csv' % (DATASETS_DIR, sheet)))
total_lines += num_lines
likelihoods = likelihood_matrix(sheet, exponents, offsets,\
decay_rate=decay_rate,\
decay_type=decay_type, ghost=ghost)
if write_file:
write_likelihoods(likelihoods, sheet, num_lines, decay_type,\
decay_rate, ghost, exponents, offsets)
max_likelihood, max_values = max_likelihood_estimates(likelihoods, exponents, offsets)
outname = '%s_%s' % (out_str, sheet)
if cumulative:
if not isinstance(cumulative_likelihoods, np.ndarray):
cumulative_likelihoods = np.copy(likelihoods)
else:
cumulative_likelihoods += likelihoods
if individual:
os.system('mkdir -p %s/individual/%s' % (OUT_DIR, STRATEGY))
if heat:
likelihood_heat(likelihoods, max_likelihood, max_values, exponents, \
offsets, '%s/individual/%s/%s' % (OUT_DIR, STRATEGY, outname))
if plot:
likelihood_plot(likelihoods, exponents, offsets, \
'%s/individual/%s/%s' % (OUT_DIR, STRATEGY, outname))
if out:
for exponent, offset in max_values:
hist_file.write('%0.2f, %0.2f, %s, %s, %s, %d\n' % \
(exponent, offset, STRATEGY, decay_type, label, num_lines))
if cumulative:
cumulative_likelihoods
outname = 'cumulative_%s_%s' % (out_str, label)
max_likelihood, max_values = max_likelihood_estimates(cumulative_likelihoods,\
decays, explores)
os.system('mkdir -p %s/cumulative/%s' % (OUT_DIR, STRATEGY))
if heat:
likelihood_heat(cumulative_likelihoods, max_likelihood, max_values, \
exponents, offsets, '%s/cumulative/%s/%s' % (OUT_DIR, strategy, outname))
if plot:
likelihood_plot(cumulative_likelihoods, exponents, offsets, \
'%s/cumulative/%s/%s' % (OUT_DIR, STRATEGY, outname))
print "plotted"
hist_file.close()
def main():
#strategy_choices = ['uniform', 'max', 'maxz', 'rank']
#decay_choices = ['linear', 'const', 'exp']
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--label', default=None)
parser.add_argument('-sh', '--sheets', nargs='+', required=True)
parser.add_argument('-d', '--decay', type=float, default=DECAY_RATE)
parser.add_argument('-dt', '--decay_types', nargs='+', choices=DECAY_CHOICES, required=True)
parser.add_argument('-c', '--cumulative', action='store_true')
parser.add_argument('-i', '--individual', action='store_true')
parser.add_argument('-omin', type=float, default=0.01)
parser.add_argument('-omax', type=float, default=2)
parser.add_argument('-emin', type=float, default=0.01)
parser.add_argument('-emax', type=float, default=2)
parser.add_argument('-ostep', type=float, default=0.01)
parser.add_argument('-estep', type=float, default=0.01)
parser.add_argument('-o', '--out', action='store_true')
parser.add_argument('-g', '--ghost', action='store_true')
parser.add_argument('--plot', action='store_true')
parser.add_argument('--heat', action='store_true')
parser.add_argument('--write_file', action='store_true')
args = parser.parse_args()
label = args.label
sheets = args.sheets
if label == None:
assert len(sheets) == 1
label = sheets[0]
if sheets == ['nocut']:
sheets = NOCUT
elif sheets == ['nocut2']:
sheets = NOCUT2
elif sheets == ['cut']:
sheets = CUT
elif sheets == ['all']:
sheets = ALL
elif sheets == ['all2']:
sheets = ALL2
decay_types = args.decay_types
decay_rate = args.decay
omin, omax, emin, emax = args.omin, args.omax, args.emin, args.emax
ostep, estep = args.ostep, args.estep
cumulative = args.cumulative
individual = args.individual
out = args.out
ghost = args.ghost
if ghost:
label += '_ghost'
heat = args.heat
plot = args.plot
write_file = args.write_file
if not (heat or plot or write_file):
print "error: must select an action"
return None
ml_analysis(label, sheets, decay_types, decay_rate, omin, omax, emin, emax, ostep, estep, \
cumulative, individual, out, ghost, heat, plot, write_file)
if __name__ == '__main__':
main()