-
-
Save syhw/1cc4a46805628da31b023bdaa33c7254 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# tmux requires unrecognized OSC sequences to be wrapped with DCS tmux; | |
# <sequence> ST, and for all ESCs in <sequence> to be replaced with ESC ESC. It | |
# only accepts ESC backslash for ST. | |
function print_osc() { | |
if [[ -n $TERM ]] ; then | |
printf "\033Ptmux;\033\033]" | |
else | |
printf "\033]" | |
fi | |
} | |
function print_csi() { | |
printf "\033[" | |
} | |
# More of the tmux workaround described above. | |
function print_st() { | |
if [[ -n $TERM ]] ; then | |
printf "\a\033\\" | |
else | |
printf "\a" | |
fi | |
} | |
# Return the height of the image, or 200 if imagemagick is unavailable. | |
function get_height() { | |
identify -format "%h" - 2>/dev/null || echo 200 | |
} | |
# print_image filename inline base64contents print_filename | |
# filename: Filename to convey to client | |
# inline: 0 or 1 | |
# base64contents: Base64-encoded contents | |
# print_filename: If non-empty, print the filename | |
# before outputting the image | |
function print_image() { | |
VERSION=$(base64 --version 2>&1) | |
if [[ "$VERSION" =~ fourmilab ]]; then | |
BASE64ARG=-d | |
elif [[ "$VERSION" =~ GNU ]]; then | |
BASE64ARG=-di | |
else | |
BASE64ARG=-D | |
fi | |
local -i HEIGHT=$(printf "%s" "$3" | base64 $BASE64ARG | get_height) | |
HEIGHT=$(( (HEIGHT + 19) / 20 )) | |
for (( I = 0; I < $HEIGHT; I++ )); do | |
printf "\n" | |
done | |
print_csi | |
printf "?25l" | |
print_csi | |
printf "${HEIGHT}F" | |
print_osc | |
printf '1337;File=' | |
if [[ -n "$1" ]]; then | |
printf 'name='`printf "%s" "$1" | base64`";" | |
fi | |
printf "%s" "$3" | base64 $BASE64ARG | wc -c | awk '{printf "size=%d",$1}' | |
printf ";inline=$2" | |
printf ";height=$HEIGHT" | |
printf ":" | |
printf "%s" "$3" | |
print_st | |
printf '\n' | |
if [[ -n "$4" ]]; then | |
echo $1 | |
fi | |
print_csi | |
printf "${HEIGHT}E" | |
print_csi | |
printf "?25h" | |
} | |
function error() { | |
echo "ERROR: $*" 1>&2 | |
} | |
function show_help() { | |
echo "Usage: imgcat [-p] filename ..." 1>& 2 | |
echo " or: cat filename | imgcat" 1>& 2 | |
} | |
## Main | |
if [ -t 0 ]; then | |
has_stdin=f | |
else | |
has_stdin=t | |
fi | |
# Show help if no arguments and no stdin. | |
if [ $has_stdin = f -a $# -eq 0 ]; then | |
show_help | |
exit | |
fi | |
# Look for command line flags. | |
while [ $# -gt 0 ]; do | |
case "$1" in | |
-h|--h|--help) | |
show_help | |
exit | |
;; | |
-p|--p|--print) | |
print_filename=1 | |
;; | |
-*) | |
error "Unknown option flag: $1" | |
show_help | |
exit 1 | |
;; | |
*) | |
if [ -r "$1" ] ; then | |
has_stdin=f | |
print_image "$1" 1 "$(base64 < "$1")" "$print_filename" | |
else | |
error "imgcat: $1: No such file or directory" | |
exit 2 | |
fi | |
;; | |
esac | |
shift | |
done | |
# Read and print stdin | |
if [ $has_stdin = t ]; then | |
print_image "" 1 "$(cat | base64)" "" | |
fi | |
exit 0 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/public/apps/anaconda3/5.0.1/bin/python | |
usage=""" | |
Usage: streams_plot [series_separator] [-h|-bar|-line|-linesmooth|-lineexpsmooth|-bardelta] < data | |
It will output a png on stdout, that you can redirect or pipe. | |
Default series_separator is END (the string). | |
Default plotting is line. | |
Simple line example: | |
echo "lala\n1\n2\n3\n4\nEND\npo\n4\n3\n2\n1\n0\n-1\nEND" | streams_plot | itc | |
Bar plot with xticks (note that xticks are optional and only the longest sequence of ticks will be used): | |
echo "lala\n1\n2\n3\n4\nEND\npo\n4 a\n3 b\n2 c\n1 d\nEND" | streams_plot -bar | itc | |
Line plot with xticks: | |
echo "lala\n1 a\n2 b\n3 c\n4 d\nEND\npo\n4 a\n3 b\n2 c\n1 d\n-2 e\nEND" | streams_plot | itc | |
Line example with value, bottom, top (e.g. min, max, or value-stddev and value+stddev): | |
echo "lala\n1 0.9 1.1\n2 1.5 2.2\n3 2.6 2.9\n4\nEND\npo\n4\n3\n2\n1\n0\n-1\nEND" | streams_plot | itc | |
""" | |
### another more complicated example: | |
### { echo "master"; ./scripts/ladder/ladder -a -o all --output /checkpoint02/gab/ladderoutput --sort-by op -n master-sep-13 | rg -o "\(\d+%\)" | sed 's/[^0-9]//g'; echo "END"; echo "overlord scouting"; ./scripts/ladder/ladder -a -o all --output /checkpoint02/gab/ladderoutput --sort-by op -n overlord-scouting-sep-13-2 | sed 's/Against//' | rg "\(\d+%\)" | awk '{print $8,$1}' | sed 's/[()%]//g'; echo "END" } | streams_plot -bardelta | itc | |
import sys, math | |
import numpy as np | |
from collections import OrderedDict | |
import matplotlib as mpl | |
mpl.use('Agg') | |
import matplotlib.cm as cmx | |
import matplotlib.colors as colors | |
from matplotlib import pyplot as plt | |
#plt.style.use('fivethirtyeight') | |
#plt.style.use('dark_background') | |
plt.style.use('ggplot') | |
plt.rcParams['font.size'] = 10 | |
plt.rcParams['axes.labelsize'] = 10 | |
plt.rcParams['axes.labelweight'] = 'bold' | |
plt.rcParams['axes.titlesize'] = 10 | |
plt.rcParams['xtick.labelsize'] = 8 | |
plt.rcParams['ytick.labelsize'] = 8 | |
plt.rcParams['legend.fontsize'] = 10 | |
plt.rcParams['figure.titlesize'] = 12 | |
def moving_average(a, n=10): | |
if n > len(a): | |
n = len(a) - 1 | |
ret = np.cumsum(a, dtype=float) | |
ret[n:] = ret[n:] - ret[:-n] | |
return ret[n - 1:] / n | |
def expdecay_average(a, gamma=0.99): | |
ret = np.zeros_like(a) | |
ret[0] = a[0] | |
for i in range(1, ret.shape[0]): | |
ret[i] = ret[i-1] * gamma + a[i] * (1.0-gamma) | |
return ret | |
endofstream = 'END' | |
plottype = 'line' | |
if len(sys.argv) > 1: | |
for argv in sys.argv[1:]: | |
if "-" in argv: | |
if argv == "-h": | |
sys.stderr.write(usage + '\n') | |
sys.exit(0) | |
else: | |
plottype = argv[1:] | |
else: | |
endofstream = argv | |
d = OrderedDict() # key -> values | |
dfill = OrderedDict() # key -> (min, max) | |
ck = '' | |
xticks = [] | |
tmpxticks = [] | |
for line in sys.stdin: | |
tmp = line.rstrip().strip() | |
if tmp == '' or tmp == 'nan': | |
continue | |
try: | |
t = tmp.split() | |
d.setdefault(ck, []).append(float(t[0])) | |
# TODO add t[1] t[2] when existing | |
if len(t) > 1: | |
if len(t) == 3: # y ymin ymax | |
try: | |
dfill.setdefault(ck, [[], []])[0].append(float(t[1])) | |
dfill[ck][1].append(float(t[2])) | |
except ValueError: # not a number on t[1:] | |
tmpxticks.append(' '.join(t[1:])) | |
else: | |
tmpxticks.append(' '.join(t[1:])) | |
except ValueError: | |
if ck == '': | |
ck = tmp | |
else: | |
sys.stderr.write(ck + '\n') | |
if tmp == endofstream: | |
if len(tmpxticks) > len(xticks): | |
xticks = tmpxticks | |
tmpxticks = [] | |
ck = '' | |
if len(d[ck]) == 0: | |
d.pop(ck) | |
### moving averaging | |
if plottype == 'linesmooth': | |
n = 100 | |
for k, v in d.items(): | |
d[k] = moving_average(v, n) | |
xticks = xticks[n-1:] | |
### exponentially decaying averaging | |
if plottype == 'lineexpsmooth': | |
for k, v in d.items(): | |
d[k] = expdecay_average(v, 0.99) | |
### plot size | |
L = max([len(v) for v in d.values()]) | |
fig, ax = plt.subplots() | |
fig.set_size_inches(12, 7) | |
### bardelta | |
if plottype == 'bardelta': | |
sys.stderr.write("!!! doing a bar plot that does first series minus second series and ignores the rest !!!\n") | |
plottype = 'bar' | |
i = 0 | |
a, b = [], [] | |
for k, v in d.items(): | |
if i == 0: | |
a = v | |
elif i == 1: | |
b = v | |
i += 1 | |
delta = np.array(b) - np.array(a) | |
d.clear() | |
d['delta'] = delta | |
### colors and linestyles | |
bar_width = 0.80 / len(d) | |
linestyles = ['-', '--', '-.'] | |
hatchstyles = ['+', '\\', 'O', '/', '.'] | |
if len(d) <= 10: | |
linestyles = ['-'] | |
hatchstyles = [' '] | |
lls = len(linestyles) | |
lhs = len(hatchstyles) | |
numstyles = lls | |
if plottype[:3] == 'bar': | |
numstyles = lhs | |
csmap = cmx.ScalarMappable(norm=colors.Normalize(vmin=0, vmax=math.ceil((len(d)-1)/numstyles)), cmap=plt.get_cmap('plasma')) # could use vmax=len(d)-1 and csmap.to_rgba(i) | |
if len(d) <= 10*numstyles: | |
csmap = cmx.ScalarMappable(norm=colors.Normalize(vmin=0, vmax=10), cmap=plt.get_cmap('tab10')) | |
### actual plotting | |
for i, (k, v) in enumerate(d.items()): | |
ind = np.arange(len(v)) | |
if plottype[:4] == 'line': | |
ax.plot(ind, v, linestyles[i%lls], label=k, color=csmap.to_rgba(i//lls), linewidth=2.0) | |
if k in dfill: | |
ax.fill_between(ind, dfill[k][0], dfill[k][1], alpha=0.2) | |
elif plottype == 'bar': | |
ax.bar(ind + i*bar_width, v, bar_width, label=k, color=csmap.to_rgba(i//lhs), hatch=hatchstyles[i%lhs]) | |
if len(xticks) > 0: | |
if plottype[:4] == 'line': | |
plt.xticks(ind, xticks, rotation='vertical') | |
elif plottype == 'bar': | |
plt.xticks(ind + 0.40, xticks, rotation='vertical') | |
handles, labels = ax.get_legend_handles_labels() | |
lgd = ax.legend(handles, labels, loc='center left', bbox_to_anchor=(1, 0.5)) | |
#plt.tight_layout() | |
if sys.version_info[0] >= 3: | |
plt.savefig(sys.stdout.buffer, bbox_extra_artists=(lgd,), bbox_inches='tight') | |
else: | |
plt.savefig(sys.stdout, bbox_extra_artists=(lgd,), bbox_inches='tight') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment