| 1 |
#!/usr/bin/env python |
| 2 |
import glob |
| 3 |
import matplotlib.colors as mcolors |
| 4 |
import matplotlib.pyplot as plt |
| 5 |
import numpy as np |
| 6 |
import optparse |
| 7 |
import re |
| 8 |
|
| 9 |
|
| 10 |
def sort_nicely(l): |
| 11 |
""" Sort the given list in the way that humans expect. """ |
| 12 |
convert = lambda s: int(s) if s.isdigit() else s |
| 13 |
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] # turn a string into a list of string and number chunks ("z23a" -> ["z", 23, "a"]) |
| 14 |
|
| 15 |
l.sort(key=alphanum_key) |
| 16 |
return l |
| 17 |
|
| 18 |
def make_colormap(seq): |
| 19 |
"""Return a LinearSegmentedColormap |
| 20 |
seq: a sequence of floats and RGB-tuples. The floats should be increasing |
| 21 |
and in the interval (0,1). |
| 22 |
""" |
| 23 |
return mcolors.LinearSegmentedColormap.from_list('CustomMap', seq, N=len(seq)) |
| 24 |
|
| 25 |
def moveon(event): |
| 26 |
plt.close() |
| 27 |
|
| 28 |
def main(): |
| 29 |
p = optparse.OptionParser() |
| 30 |
|
| 31 |
# env arguments |
| 32 |
p.add_option('-p', dest="pv", action="store_const", const='p', default='p') |
| 33 |
p.add_option('-v', dest="pv", action="store_const", const='v') |
| 34 |
p.add_option('-d', '--data', dest="data", default="status") |
| 35 |
p.add_option('-l', '--level', dest="L", default=0, type='int') |
| 36 |
p.add_option('-i', '--interactive', dest="ui", action="store_true", default=True) |
| 37 |
p.add_option('-b', '--batch', dest="ui", action="store_false") |
| 38 |
p.add_option('-s', '--skip', dest="skip", default=0, type='int') |
| 39 |
|
| 40 |
# parse |
| 41 |
options, arguments = p.parse_args() |
| 42 |
|
| 43 |
V = options.pv |
| 44 |
L = options.L |
| 45 |
ui = options.ui |
| 46 |
skip = options.skip |
| 47 |
data = options.data |
| 48 |
|
| 49 |
# We assume that the coordinates are amalgamated, i.e. there are no |
| 50 |
# duplicates for velocity |
| 51 |
coords = np.loadtxt("coord-l" + str(L) + "-" + V) |
| 52 |
x = coords[:, 0] |
| 53 |
y = coords[:, 1] |
| 54 |
|
| 55 |
area = 90 |
| 56 |
|
| 57 |
cc = mcolors.ColorConverter().to_rgb |
| 58 |
cvals = [cc('w'), cc('y'), cc('b'), cc('m'), cc('r'), cc('c')] |
| 59 |
q2q1 = make_colormap(cvals) |
| 60 |
|
| 61 |
min_color = 0 |
| 62 |
max_color = len(cvals)-1 |
| 63 |
|
| 64 |
plt.ion() |
| 65 |
fig = plt.figure() |
| 66 |
|
| 67 |
k = 0 |
| 68 |
for file in sort_nicely(glob.glob(data + "-l" + str(L) + "-" + V + "-*")): |
| 69 |
k = k+1 |
| 70 |
if k <= skip: |
| 71 |
continue |
| 72 |
|
| 73 |
print(file) |
| 74 |
|
| 75 |
colors = np.loadtxt(file) |
| 76 |
assert len(colors) == len(x) |
| 77 |
|
| 78 |
# one can use any key on keyboard to progress to the next plot |
| 79 |
if ui == True: |
| 80 |
cid = fig.canvas.mpl_connect('key_press_event', moveon) |
| 81 |
|
| 82 |
s = plt.scatter(x, y, c=colors, vmin=min_color, vmax=max_color, s=area, cmap=q2q1) |
| 83 |
s.set_alpha(0.75) |
| 84 |
plt.title(file) |
| 85 |
|
| 86 |
if ui == False: |
| 87 |
plt.savefig(file + '.png', format='png') |
| 88 |
else: |
| 89 |
fig.canvas.draw() |
| 90 |
raw_input('>') |
| 91 |
|
| 92 |
plt.clf() |
| 93 |
|
| 94 |
if __name__ == '__main__': |
| 95 |
main() |