Skip to content

Instantly share code, notes, and snippets.

View pengzhenghao's full-sized avatar
🤩
Focusing

Zhenghao Peng pengzhenghao

🤩
Focusing
View GitHub Profile
@pengzhenghao
pengzhenghao / tmp.sh
Created January 22, 2019 04:21
How to write a python list in bash
#!/usr/bin/env bash
arr=(1 10 100)
for d in ${arr[*]}
do
echo $d
echo
done
from collections import deque
class FPSTimer(object):
def __init__(self, log_interval=10):
self.cnt = 0
self.log_interval = log_interval
self.queue = deque(maxlen=log_interval)
self.queue.append(time.time())
@pengzhenghao
pengzhenghao / visualize.py
Created May 6, 2019 09:33
A beautiful choice interface
index = input("You are giving a exp_dir which supposed to contain only one trial, "
"but we found many. \nPlease input the index of the trial you want to "
"visualize:\n\n{}\nYour index:".format(
"".join(["{}: {}\n".format(i, t) for i, t in enumerate(potential_trial_paths)])))
"""The output look as following, it's quite clean.
You are giving a exp_dir which supposed to contain only one trial, but we found many.
Please input the index of the trial you want to visualize:
0: /Users/pengzhenghao/ray_results/debug/PPO_tollgate_0_2019-04-09_21-10-02k6f8lt62
@pengzhenghao
pengzhenghao / utils.py
Created May 11, 2019 08:43
MovingAverage without exponential weights
from collections import deque
class MovingAverage(object):
def __init__(self, max_len=10):
self.val = deque(maxlen=max_len)
self.avg = 0
self.maxlen = max_len
def update(self, val):
self.val.append(val)
self.avg = sum(self.val) / self.maxlen
@pengzhenghao
pengzhenghao / test_render.py
Created August 12, 2019 16:22
An OpenCV implementation of the renderer of the BipedaoWalker environment. This implementation allows user to run renderer at headless server. Usage: 1. Ask administrator to install xvfb 2. run: xvfb-run -s "-screen 0 600x400x24" python test_render.py Use mode="human" to see the pop-up OpenCV window. Use mode="rgb_array" to get the (X, X, 4) nda…
from gym.envs.box2d.bipedal_walker import (
BipedalWalker, VIEWPORT_H, VIEWPORT_W, SCALE, TERRAIN_HEIGHT, TERRAIN_STEP
)
from Box2D.b2 import circleShape
import cv2
import numpy as np
import copy
import uuid
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# agents_name = ["ours", "DDPG"]
x_name = "uniqueness"
y_name = "performance"
dpi = 300
figsize = (12, 8)
import IPython
import tempfile
import PIL
def animate(img_array):
path = tempfile.mkstemp(suffix=".gif")[1]
images = [PIL.Image.fromarray(frame) for frame in img_array]
images[0].save(
path,
save_all=True,
@pengzhenghao
pengzhenghao / beautiful_seaborn_fig.py
Created April 23, 2020 01:53
Highly customized seaborn figure
from toolbox.process_data import parse
data3 = []
a2c_data3 = parse("/home/zhpeng/OLD-novel-rl-archive-20200210/0206-a2c-baseline-large-Walker2d-v3-Walker2d-v3-50000000ts.pkl")
a2c_data3["label"] = "A2C"
a2c_data3.episode_reward_mean *= 5
data3.append(a2c_data3)
ppo_data3 = parse("/home/zhpeng/0119-ppo-baseline-walker-Walker2d-v3-50000000ts.pkl")
@pengzhenghao
pengzhenghao / json_dump.py
Created April 24, 2020 02:16
dump numpy object using json to a file
import json
import numpy as np
class NumpyEncoder(json.JSONEncoder):
""" Special json encoder for numpy types """
def default(self, obj):
if isinstance(obj, (np.int_, np.intc, np.intp, np.int8,
np.int16, np.int32, np.int64, np.uint8,
@pengzhenghao
pengzhenghao / beautiful_seaborn2.py
Created April 28, 2020 15:24
Another highly customized seaborn drawing example
x = "timesteps_total"
y = "novelty_reward_mean"
ppo_path = "/home/sunhao/ray_results/0131-dece-use_bisector-use_bisector"
clip = None
dece_path = "/home/sunhao/ray_results/0102-dece"
dece_df = parse(dece_path,
["seed", "constrain_novelty", "delay_update", "replay_values", "use_diversity_value_network"],