-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_heatmaps.py
221 lines (159 loc) · 6.59 KB
/
generate_heatmaps.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
import os
import sys
import csv
import json
from glob import iglob as glob
from itertools import izip
import re
import cPickle as pickle
import numpy as np
import scipy.signal
def _config(**kwargs):
INPUT_DIR = kwargs.get('INPUT_DIR') or 'run1'
INPUT_FILES = 'viewHist_*.txt'
#INPUT_DIR = 'heatpilot1'
INPUT_FILES_RE = re.compile(INPUT_FILES.replace('*','(.*)'))
INPUT_CACHE = os.path.join(INPUT_DIR, 'viewHist.pickle')
OUTPUT_DIR = INPUT_DIR
# median
SMOOTH_WINDOW_1 = 11
#SMOOTH_WINDOW_2 = 25
SMOOTH_WINDOW_2 = 5
# Alan's paper, approximately, but it actually used a Kaiser filter
#SMOOTH_WINDOW_1 = 27
#SMOOTH_WINDOW_2 = 59
# concensus
CONCENSUS = 3
for k, v in locals().iteritems():
if k != 'kwargs':
globals()[k] = v
_config()
def get_outpath(path, tpl):
fname = os.path.basename(path)
dirname = os.path.dirname(path)
new_fname = tpl % INPUT_FILES_RE.match(fname).group(1)
return os.path.join(dirname, new_fname)
def write_heatmap(path, tpl, heatmap):
outpath = get_outpath(path, tpl)
with open(outpath, 'w') as fh:
for x in heatmap:
print >>fh, x
def mustd_scaled(array):
mu = np.mean(array)
std = np.std(array)
scaled = np.zeros(len(array))
np.clip(array, 0, mu+std*2, scaled)
scaled /= (mu+std*2)
return scaled
# http://stackoverflow.com/a/3041990/399990
def query_yes_no(question, default="yes"):
valid = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = raw_input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")
def main():
# path -> [hist]
histograms = {} # raw
histograms_smooth = {} # median and normalized
histograms_binary = {} # binary (1 = watched)
# path -> [0|1]
concensus = {} # 1 = watched by <CONCENSUS> people
load_from_cache = os.path.exists(INPUT_CACHE) and query_yes_no('CACHE detected. Load from cache?')
if load_from_cache:
print '!! Reading raw histograms from CACHE !!',
with open(INPUT_CACHE, 'rb') as fh:
histograms = pickle.load(fh)
else:
# read viewHist_*.txt
print 'Reading raw histograms...',
for path in glob(os.path.join(INPUT_DIR, INPUT_FILES)):
print '\rReading raw histograms...', path,
hists_for_cur_file = histograms[path] = []
with open(path, 'r') as fh:
for line in fh:
hists_for_cur_file.append( np.array(eval(line)) )
with open(INPUT_CACHE, 'wb') as fh:
pickle.dump(histograms, fh)
print
# compute histogram variants
print 'Computing histograms...',
for path in histograms:
print '\rComputing histograms...', path,
outpath = get_outpath(path, 'viewHistSmooth_%s.txt')
outpath2 = get_outpath(path, 'viewHistSmoothNorm_%s.txt')
with open(outpath, 'w') as fh:
with open(outpath2, 'w') as fh2:
for h in histograms[path]:
h2 = scipy.signal.medfilt(h, SMOOTH_WINDOW_1)
print >>fh, list(h2)
s = np.max(h2)
h3 = h2 / (s if s else 1.0)
print >>fh2, list(h3)
histograms_smooth.setdefault(path, []).append(h3)
h_binary = np.copy(h) # alt: h2?
h_binary[h_binary >= 1] = 1
histograms_binary.setdefault(path, []).append(h_binary)
print
# consensus computation
print 'Computing concensus...',
for path in histograms_binary:
print '\rComputing concensus...', path,
W = len(max(histograms_binary[path], key=len))
c = np.zeros(W)
for h in histograms_binary[path]:
c[:len(h)] += h
c[c < CONCENSUS] = 0
c[c >= CONCENSUS] = 1
concensus[path] = c
print
#-----------------------------------------------------------------------
heatmaps = {}
for path in histograms_smooth:
W = len(max(histograms_smooth[path], key=len))
heatmap = np.zeros(W)
for h in histograms_smooth[path]:
heatmap[:len(h)] += h
write_heatmap(path, 'viewHistSmoothNormSum_%s.txt', heatmap)
heatmaps[path] = heatmap
heatmap_norm = mustd_scaled(heatmap)
write_heatmap(path, 'viewHistSmoothNormSumNorm_%s.txt', heatmap_norm)
heatmaps_nonsmoothed = {}
heatmaps_smooth = {}
heatmaps_scaled = {}
heatmaps_concensus_smooth = {}
heatmaps_concensus_scaled = {}
for path, orig_heatmap in heatmaps.iteritems():
orig_heatmap_scaled = mustd_scaled(orig_heatmap)
heatmaps_nonsmoothed[path] = orig_heatmap_scaled
heatmap = scipy.signal.medfilt(orig_heatmap, SMOOTH_WINDOW_2)
heatmaps_smooth[path] = heatmap
heatmap_scaled = mustd_scaled(heatmap)
heatmaps_scaled[path] = heatmap_scaled
write_heatmap(path, 'viewHistSmoothNormSumSmooth_%s.txt', heatmap)
write_heatmap(path, 'viewHistSmoothNormSumSmoothNorm_%s.txt', heatmap_scaled)
# concensus-based
heatmap_c = orig_heatmap * concensus[path]
heatmap_c = scipy.signal.medfilt(heatmap_c, SMOOTH_WINDOW_2)
heatmap_c_scaled = mustd_scaled(heatmap_c)
heatmaps_concensus_scaled[path] = heatmap_c_scaled
write_heatmap(path, 'viewHistSmoothNormSumConcensus_%s.txt', heatmap_c)
write_heatmap(path, 'viewHistSmoothNormSumConcensusSmooth_%s.txt', heatmap_c_scaled)
print '\nDone'
if __name__ == '__main__':
main()