text:
import sys
import os
from os.path import join
import cv2 as cv
import imagehash
from PIL import Image, ImageDraw, ImageFilter, ImageOps
# import matplotlib.pyplot as plt
import imutils
import time
import numpy as np
import datetime

def ensure_pil(im):
    """Ensure image is Pillow format"""
    try:
        im.verify()
        return im
    except:
        return Image.fromarray(im.astype('uint8'), 'RGB')

def ensure_np(im):
    """Ensure image is numpy array"""
    if type(im) == np.ndarray:
        return im
    return np.asarray(im, np.uint8)

def num_channels(im):
  '''Returns number of channels in numpy.ndarray image'''
  if len(im.shape) > 2:
    return im.shape[2]
  else:
    return 1

def is_grayscale(im, threshold=5):
  """Returns True if image is grayscale
  :param im: (numpy.array) image
  :return (bool) of if image is grayscale"""
  b = im[:,:,0]
  g = im[:,:,1]
  mean = np.mean(np.abs(g - b))
  return mean < threshold

def compute_features(fe,frames,phashes,phash_thresh=1):
  """
  Get vector embedding using FeatureExtractor
  :param fe: FeatureExtractor class
  :param frames: list of frame images as numpy.ndarray
  :param phash_thresh: perceptual hash threshold
  :returns: list of feature vectors
  """
  vals = []
  phash_pre = phashes[0]
  for i,im in enumerate(frames):
    if i == 0 or (phashes[i] - phashes[i-1]) > phash_thresh:
      vals.append(fe.extract(im))
    else:
      vals.append(vals[i-1])
  return vals

def np2pil(im, swap=True):
  """Ensure image is Pillow format
    :param im: image in numpy or PIL.Image format
    :returns: image in Pillow RGB format
  """
  try:
      im.verify()
      return im
  except:
    if swap:
      im = cv.cvtColor(im,cv.COLOR_BGR2RGB)
    return Image.fromarray(im.astype('uint8'), 'RGB')

def pil2np(im, swap=True):
  """Ensure image is Numpy.ndarry format
    :param im: image in numpy or PIL.Image format
    :returns: image in Numpy uint8 format
  """
  if type(im) == np.ndarray:
    return im
  im = np.asarray(im, np.uint8)
  if swap:
    im = cv.cvtColor(im, cv.COLOR_RGB2BGR)
  return im

def resize(im, width=0, height=0):
  """resize image using imutils. Use w/h=[0 || None] to prioritize other edge size
    :param im: a Numpy.ndarray image
    :param wh: a tuple of (width, height)
  """
  # TODO change to cv.resize and add algorithm choices
  w = width
  h = height
  if w is 0 and h is 0:
    return im
  elif w > 0 and h > 0:
    ws = im.shape[1] / w
    hs = im.shape[0] / h
    if ws > hs:
      return imutils.resize(im, width=w)
    else:
      return imutils.resize(im, height=h)
  elif w > 0 and h is 0:
    return imutils.resize(im, width=w)
  elif w is 0 and h > 0:
    return imutils.resize(im, height=h)
  else:
    return im

# Utilities for analyzing frames

def compute_gray(im):
  im = cv.cvtColor(im,cv.COLOR_BGR2GRAY)
  n_vals = float(im.shape[0] * im.shape[1])
  avg = np.sum(im[:]) / n_vals
  return avg

def compute_rgb(im):
  im = cv.cvtColor(im,cv.COLOR_BGR2RGB)
  n_vals = float(im.shape[0] * im.shape[1])
  avg_r = np.sum(im[:,:,0]) / n_vals
  avg_g = np.sum(im[:,:,1]) / n_vals
  avg_b = np.sum(im[:,:,2]) / n_vals
  avg_rgb = np.sum(im[:,:,:]) / (n_vals * 3.0)
  return avg_r, avg_b, avg_g, avg_rgb

def compute_hsv(im):
  im = cv.cvtColor(im,cv.COLOR_BGR2HSV)
  n_vals = float(im.shape[0] * im.shape[1])
  avg_h = np.sum(frame[:,:,0]) / n_vals
  avg_s = np.sum(frame[:,:,1]) / n_vals
  avg_v = np.sum(frame[:,:,2]) / n_vals
  avg_hsv = np.sum(frame[:,:,:]) / (n_vals * 3.0)
  return avg_h, avg_s, avg_v, avg_hsv

def pys_dhash(im, hashSize=8):
  # resize the input image, adding a single column (width) so we
  # can compute the horizontal gradient
  resized = cv.resize(im, (hashSize + 1, hashSize))
  # compute the (relative) horizontal gradient between adjacent
  # column pixels
  diff = resized[:, 1:] > resized[:, :-1]
  # convert the difference image to a hash
  return sum([2 ** i for (i, v) in enumerate(diff.flatten()) if v])

############################################
# ImageHash
# pip install imagehash
############################################

def compute_ahash(im):
  """Compute average hash using ImageHash library
    :param im: Numpy.ndarray
    :returns: Imagehash.ImageHash
  """
  return imagehash.average_hash(ensure_pil(im_pil))

def compute_phash(im):
  """Compute perceptual hash using ImageHash library
    :param im: Numpy.ndarray
    :returns: Imagehash.ImageHash
  """
  return imagehash.phash(ensure_pil(im))

def compute_dhash(im):
  """Compute difference hash using ImageHash library
    :param im: Numpy.ndarray
    :returns: Imagehash.ImageHash
  """
  return imagehash.dhash(ensure_pil(im))

def compute_whash(im):
  """Compute wavelet hash using ImageHash library
    :param im: Numpy.ndarray
    :returns: Imagehash.ImageHash
  """
  return imagehash.whash(ensure_pil(im))

def compute_whash_b64(im):
  """Compute wavelest hash base64 using ImageHash library
    :param im: Numpy.ndarray
    :returns: Imagehash.ImageHash
  """
  return lambda im: imagehash.whash(ensure_pil(im), mode='db4')

############################################
# Pillow
############################################

def sharpen(im):
  """Sharpen image using PIL.ImageFilter
  param: im: PIL.Image
  returns: PIL.Image
  """
  im = ensure_pil(im)
  im.filter(ImageFilter.SHARPEN)
  return ensure_np(im)

def fit_image(im,targ_size):
  """Force fit image by cropping
  param: im: PIL.Image
  param: targ_size: a tuple of target (width, height)
  returns: PIL.Image
  """
  im_pil = ensure_pil(im)
  frame_pil = ImageOps.fit(im_pil, targ_size,
    method=Image.BICUBIC, centering=(0.5, 0.5))
  return ensure_np(frame_pil)

def compute_entropy(im):
  entr_img = entropy(im, disk(10))

############################################
# scikit-learn
############################################

def compute_entropy(im):
  # im is grayscale numpy
  return entropy(im, disk(10))

############################################
# OpenCV
############################################

def bgr2gray(im):
  """Wrapper for cv2.cvtColor transform
    :param im: Numpy.ndarray (BGR)
    :returns: Numpy.ndarray (Gray)
  """
  return cv.cvtColor(im,cv.COLOR_BGR2GRAY)

def gray2bgr(im):
  """Wrapper for cv2.cvtColor transform
    :param im: Numpy.ndarray (Gray)
    :returns: Numpy.ndarray (BGR)
  """
  return cv.cvtColor(im,cv.COLOR_GRAY2BGR)

def bgr2rgb(im):
  """Wrapper for cv2.cvtColor transform
    :param im: Numpy.ndarray (BGR)
    :returns: Numpy.ndarray (RGB)
  """
  return cv.cvtColor(im,cv.COLOR_BGR2RGB)

def compute_laplacian(im):
  # below 100 is usually blurry
  return cv.Laplacian(im, cv.CV_64F).var()

# http://radjkarl.github.io/imgProcessor/index.html#

def modifiedLaplacian(img):
    ''''LAPM' algorithm (Nayar89)'''
    M = np.array([-1, 2, -1])
    G = cv.getGaussianKernel(ksize=3, sigma=-1)
    Lx = cv.sepFilter2D(src=img, ddepth=cv.CV_64F, kernelX=M, kernelY=G)
    Ly = cv.sepFilter2D(src=img, ddepth=cv.CV_64F, kernelX=G, kernelY=M)
    FM = np.abs(Lx) + np.abs(Ly)
    return cv.mean(FM)[0]

def varianceOfLaplacian(img):
    ''''LAPV' algorithm (Pech2000)'''
    lap = cv.Laplacian(img, ddepth=-1)#cv.cv.CV_64F)
    stdev = cv.meanStdDev(lap)[1]
    s = stdev[0]**2
    return s[0]

def tenengrad(img, ksize=3):
    ''''TENG' algorithm (Krotkov86)'''
    Gx = cv.Sobel(img, ddepth=cv.CV_64F, dx=1, dy=0, ksize=ksize)
    Gy = cv.Sobel(img, ddepth=cv.CV_64F, dx=0, dy=1, ksize=ksize)
    FM = Gx**2 + Gy**2
    return cv.mean(FM)[0]

def normalizedGraylevelVariance(img):
    ''''GLVN' algorithm (Santos97)'''
    mean, stdev = cv.meanStdDev(img)
    s = stdev[0]**2 / mean[0]
    return s[0]

def compute_if_blank(im,width=100,sigma=0,thresh_canny=.1,thresh_mean=4,mask=None):
  # im is graysacale np
  #im = imutils.resize(im,width=width)
  #mask = imutils.resize(mask,width=width)
  if mask is not None:
    im_canny = feature.canny(im,sigma=sigma,mask=mask)
    total = len(np.where(mask > 0)[0])
  else:
    im_canny = feature.canny(im,sigma=sigma)
    total = (im.shape[0]*im.shape[1])
  n_white = len(np.where(im_canny > 0)[0])
  per = n_white/total
  if np.mean(im) < thresh_mean or per < thresh_canny:
    return 1
  else:
    return 0

def print_timing(t,n):
    t = time.time()-t
    print('Elapsed time: {:.2f}'.format(t))
    print('FPS: {:.2f}'.format(n/t))

############################################
# Point, Rect
############################################

class Point(object):
  def __init__(self, x, y):
    self.x = x
    self.y = y

class Rect(object):
  def __init__(self, p1, p2):
    '''Store the top, bottom, left and right values for points
           p1 and p2 are the (corners) in either order
    '''
    self.left   = min(p1.x, p2.x)
    self.right  = max(p1.x, p2.x)
    self.top    = min(p1.y, p2.y)
    self.bottom = max(p1.y, p2.y)

def overlap(r1, r2):
    '''Overlapping rectangles overlap both horizontally & vertically
    '''
    return range_overlap(r1.left, r1.right, r2.left, r2.right) and \
      range_overlap(r1.top, r1.bottom, r2.top, r2.bottom)

def range_overlap(a_min, a_max, b_min, b_max):
  '''Neither range is completely greater than the other
  '''
  return (a_min <= b_max) and (b_min <= a_max)

def merge_rects(r1,r2):
  p1 = Point(min(r1.left,r2.left),min(r1.top,r2.top))
  p2 = Point(max(r1.right,r2.right),max(r1.bottom,r2.bottom))
  return Rect(p1,p2)

def is_overlapping(r1,r2):
  """r1,r2 as [x1,y1,x2,y2] list"""
  r1x = Rect(Point(r1[0],r1[1]),Point(r1[2],r1[3]))
  r2x = Rect(Point(r2[0],r2[1]),Point(r2[2],r2[3]))
  return overlap(r1x,r2x)

def get_rects_merged(rects,bounds,expand=0):
  """rects: list of points in [x1,y1,x2,y2] format"""
  rects_expanded = []
  bx,by = bounds
  # expand
  for x1,y1,x2,y2 in rects:
    x1 = max(0,x1-expand)
    y1 = max(0,y1-expand)
    x2 = min(bx,x2+expand)
    y2 = min(by,y2+expand)
    rects_expanded.append(Rect(Point(x1,y1),Point(x2,y2)))

  #rects_expanded = [Rect(Point(x1,y1),Point(x2,y2)) for x1,y1,x2,y2 in rects_expanded]
  rects_merged = []
  for i,r in enumerate(rects_expanded):
    found = False
    for j,rm in enumerate(rects_merged):
      if overlap(r,rm):
        rects_merged[j] = merge_rects(r,rm) #expand
        found = True
    if not found:
      rects_merged.append(r)
  # convert back to [x1,y1,x2,y2] format
  rects_merged = [(r.left,r.top,r.right,r.bottom) for r in rects_merged]
  # contract
  rects_contracted = []
  for x1,y1,x2,y2 in rects_merged:
    x1 = min(bx,x1+expand)
    y1 = min(by,y1+expand)
    x2 = max(0,x2-expand)
    y2 = max(0,y2-expand)
    rects_contracted.append((x1,y1,x2,y2))

  return rects_contracted

############################################
# Image display
############################################

def montage(frames,ncols=4,nrows=None,width=None):
  """Convert list of frames into a grid montage
  param: frames: list of frames as Numpy.ndarray
  param: ncols: number of columns
  param: width: resize images to this width before adding to grid
  returns: Numpy.ndarray grid of all images
  """

  # expand image size if not enough frames
  if nrows is not None and len(frames) < ncols * nrows:
    blank = np.zeros_like(frames[0])
    n = ncols * nrows - len(frames)
    for i in range(n): frames.append(blank)

  rows = []
  for i,im in enumerate(frames):
    if width is not None:
      im = imutils.resize(im,width=width)
    h,w = im.shape[:2]
    if i % ncols == 0:
      if i > 0:
        rows.append(ims)
      ims = []
    ims.append(im)
  if len(ims) > 0:
    for j in range(ncols-len(ims)):
      ims.append(np.zeros_like(im))
    rows.append(ims)
  row_ims = []
  for row in rows:
    row_im = np.hstack(np.array(row))
    row_ims.append(row_im)
  contact_sheet = np.vstack(np.array(row_ims))
  return contact_sheet


text:
"""
Actor-Critic with continuous action using TD-error as the Advantage, Reinforcement Learning.

The Pendulum example (based on https://github.com/dennybritz/reinforcement-learning/blob/master/PolicyGradient/Continuous%20MountainCar%20Actor%20Critic%20Solution.ipynb)

Cannot converge!!! oscillate!!!

Using:
tensorflow
gym
"""

import tensorflow as tf
import numpy as np
import gym

np.random.seed(2)
tf.set_random_seed(2)  # reproducible

class Actor(object):
    def __init__(self, sess, n_features, action_bound, lr=0.0001):
        self.sess = sess

        self.s = tf.placeholder(tf.float32, [1, n_features], "state")
        self.a = tf.placeholder(tf.float32, None, name="act")
        self.td_error = tf.placeholder(tf.float32, None, name="td_error")  # TD_error

        l1 = tf.layers.dense(
            inputs=self.s,
            units=30,  # number of hidden units
            activation=tf.nn.relu,
            kernel_initializer=tf.random_normal_initializer(0., .1),  # weights
            bias_initializer=tf.constant_initializer(0.1),  # biases
            name='l1'
        )

        mu = tf.layers.dense(
            inputs=l1,
            units=1,  # number of hidden units
            activation=tf.nn.tanh,
            kernel_initializer=tf.random_normal_initializer(0., .1),  # weights
            bias_initializer=tf.constant_initializer(0.1),  # biases
            name='mu'
        )

        sigma = tf.layers.dense(
            inputs=l1,
            units=1,  # output units
            activation=tf.nn.softplus,  # get action probabilities
            kernel_initializer=tf.random_normal_initializer(0., .1),  # weights
            bias_initializer=tf.constant_initializer(1.),  # biases
            name='sigma'
        )
        global_step = tf.Variable(0, trainable=False)
        # self.e = epsilon = tf.train.exponential_decay(2., global_step, 1000, 0.9)
        self.mu, self.sigma = tf.squeeze(mu*2), tf.squeeze(sigma+0.1)
        self.normal_dist = tf.distributions.Normal(self.mu, self.sigma)

        self.action = tf.clip_by_value(self.normal_dist.sample(1), action_bound[0], action_bound[1])

        with tf.name_scope('exp_v'):
            log_prob = self.normal_dist.log_prob(self.a)  # loss without advantage
            self.exp_v = log_prob * self.td_error  # advantage (TD_error) guided loss
            # Add cross entropy cost to encourage exploration
            self.exp_v += 0.01*self.normal_dist.entropy()

        with tf.name_scope('train'):
            self.train_op = tf.train.AdamOptimizer(lr).minimize(-self.exp_v, global_step)    # min(v) = max(-v)

    def learn(self, s, a, td):
        s = s[np.newaxis, :]
        feed_dict = {self.s: s, self.a: a, self.td_error: td}
        _, exp_v = self.sess.run([self.train_op, self.exp_v], feed_dict)
        return exp_v

    def choose_action(self, s):
        s = s[np.newaxis, :]
        return self.sess.run(self.action, {self.s: s})  # get probabilities for all actions

class Critic(object):
    def __init__(self, sess, n_features, lr=0.01):
        self.sess = sess
        with tf.name_scope('inputs'):
            self.s = tf.placeholder(tf.float32, [1, n_features], "state")
            self.v_ = tf.placeholder(tf.float32, [1, 1], name="v_next")
            self.r = tf.placeholder(tf.float32, name='r')

        with tf.variable_scope('Critic'):
            l1 = tf.layers.dense(
                inputs=self.s,
                units=30,  # number of hidden units
                activation=tf.nn.relu,
                kernel_initializer=tf.random_normal_initializer(0., .1),  # weights
                bias_initializer=tf.constant_initializer(0.1),  # biases
                name='l1'
            )

            self.v = tf.layers.dense(
                inputs=l1,
                units=1,  # output units
                activation=None,
                kernel_initializer=tf.random_normal_initializer(0., .1),  # weights
                bias_initializer=tf.constant_initializer(0.1),  # biases
                name='V'
            )

        with tf.variable_scope('squared_TD_error'):
            self.td_error = tf.reduce_mean(self.r + GAMMA * self.v_ - self.v)
            self.loss = tf.square(self.td_error)    # TD_error = (r+gamma*V_next) - V_eval
        with tf.variable_scope('train'):
            self.train_op = tf.train.AdamOptimizer(lr).minimize(self.loss)

    def learn(self, s, r, s_):
        s, s_ = s[np.newaxis, :], s_[np.newaxis, :]

        v_ = self.sess.run(self.v, {self.s: s_})
        td_error, _ = self.sess.run([self.td_error, self.train_op],
                                          {self.s: s, self.v_: v_, self.r: r})
        return td_error

OUTPUT_GRAPH = False
MAX_EPISODE = 1000
MAX_EP_STEPS = 200
DISPLAY_REWARD_THRESHOLD = -300  # renders environment if total episode reward is greater then this threshold
RENDER = False  # rendering wastes time
GAMMA = 0.9
LR_A = 0.001    # learning rate for actor
LR_C = 0.01     # learning rate for critic

env = gym.make('Pendulum-v0')
env.seed(1)  # reproducible
env = env.unwrapped

N_S = env.observation_space.shape[0]
A_BOUND = env.action_space.high

sess = tf.Session()

actor = Actor(sess, n_features=N_S, lr=LR_A, action_bound=[-A_BOUND, A_BOUND])
critic = Critic(sess, n_features=N_S, lr=LR_C)

sess.run(tf.global_variables_initializer())

if OUTPUT_GRAPH:
    tf.summary.FileWriter("logs/", sess.graph)

for i_episode in range(MAX_EPISODE):
    s = env.reset()
    t = 0
    ep_rs = []
    while True:
        if RENDER:
            env.render()
        a = actor.choose_action(s)

        s_, r, done, info = env.step(a)
        r /= 10

        td_error = critic.learn(s, r, s_)  # gradient = grad[r + gamma * V(s_) - V(s)]
        actor.learn(s, a, td_error)  # true_gradient = grad[logPi(s,a) * td_error]

        s = s_
        t += 1
        ep_rs.append(r)
        if t > MAX_EP_STEPS:
            ep_rs_sum = sum(ep_rs)
            if 'running_reward' not in globals():
                running_reward = ep_rs_sum
            else:
                running_reward = running_reward * 0.9 + ep_rs_sum * 0.1
            if running_reward > DISPLAY_REWARD_THRESHOLD: RENDER = True  # rendering
            print("episode:", i_episode, "  reward:", int(running_reward))
            break

ext: py

reason: import numpy


text:
from simtk import unit
from benchmark.integrators.kyle.xchmc import XCGHMCIntegrator
import numpy as np
from simtk import openmm as mm

from benchmark import simulation_parameters

timestep = 0.5 * unit.femtoseconds

def evaluate(timestep, extra_chances):

    xchmc = XCGHMCIntegrator(temperature=simulation_parameters['temperature'],
                            steps_per_hmc=1, timestep=timestep,
                             extra_chances=extra_chances, steps_per_extra_hmc=1,
                             collision_rate=1.0 / unit.picosecond)

    from benchmark.testsystems import dhfr_constrained
    testsystem = dhfr_constrained

    platform = mm.Platform.getPlatformByName('CUDA')
    platform.setPropertyDefaultValue('CudaPrecision', 'mixed')
    platform.setPropertyDefaultValue('DeterministicForces', 'true')
    testsystem.platform = platform
    sim = testsystem.construct_simulation(xchmc)

    # pick an equilibrated initial condition
    testsystem.load_or_simulate_x_samples() # make sure x_samples is there...
    sim.context.setPositions(testsystem.x_samples[-1])

    from mdtraj.reporters import HDF5Reporter
    name = "dhfr_xchmc_timestep={}fs, extra_chances={}".format(timestep.value_in_unit(unit.femtoseconds), extra_chances)
    reporter = HDF5Reporter(file=name + ".h5", reportInterval=1000, velocities=True, cell=True)
    sim.reporters.append(reporter)

    from tqdm import tqdm

    for _ in tqdm(range(10000)):
        sim.context.setPositions(testsystem.sample_x_from_equilibrium())
        sim.context.setVelocitiesToTemperature(simulation_parameters['temperature'])
        sim.step(1)

        print(xchmc.all_counts)
    np.save(name + ".npy", xchmc.all_counts)

if __name__ == "__main__":
    evaluate(timestep=0.5 * unit.femtoseconds,
             extra_chances=20)

max_stars_repo_name: choderalab/integrator-benchmark

max_issues_repo_name: choderalab/integrator-benchmark

max_forks_repo_name: choderalab/integrator-benchmark


text:
import csv
import json
import gym

import numpy as np
import tensorflow as tf
import os.path as osp

from gym.core import Wrapper
from multiprocessing import Process, Pipe

def sf01(arr):
    """
    swap and then flatten axes 0 and 1
    """
    s = arr.shape
    return arr.swapaxes(0, 1).reshape(s[0] * s[1], *s[2:])

def safemean(xs):
    return np.nan if len(xs) == 0 else np.mean(xs)

def explained_variance(ypred,y):
    """
    Computes fraction of variance that ypred explains about y.
    Returns 1 - Var[y-ypred] / Var[y]

    interpretation:
        ev=0  =>  might as well have predicted zero
        ev=1  =>  perfect prediction
        ev<0  =>  worse than just predicting zero

    """
    assert y.ndim == 1 and ypred.ndim == 1
    vary = np.var(y)
    return np.nan if vary==0 else 1 - np.var(y-ypred)/vary

def get_all_params(model):
    params = []
    for net in model:
        params += net.all_params

    return params

def tf_sum(x, axis=None, keepdims=False):
    axis = None if axis is None else [axis]
    return tf.reduce_sum(x, axis=axis, keep_dims=keepdims)

class Pd(object):
    """
    A particular probability distribution
    """
    def flatparam(self):
        raise NotImplementedError
    def mode(self):
        raise NotImplementedError
    def neglogp(self, x):
        # Usually it's easier to define the negative logprob
        raise NotImplementedError
    def kl(self, other):
        raise NotImplementedError
    def entropy(self):
        raise NotImplementedError
    def sample(self):
        raise NotImplementedError
    def logp(self, x):
        return - self.neglogp(x)

class PdType(object):
    """
    Parametrized family of probability distributions
    """
    def pdclass(self):
        raise NotImplementedError
    def pdfromflat(self, flat):
        return self.pdclass()(flat)
    def param_shape(self):
        raise NotImplementedError
    def sample_shape(self):
        raise NotImplementedError
    def sample_dtype(self):
        raise NotImplementedError

    def param_placeholder(self, prepend_shape, name=None):
        return tf.placeholder(dtype=tf.float32, shape=prepend_shape+self.param_shape(), name=name)
    def sample_placeholder(self, prepend_shape, name=None):
        return tf.placeholder(dtype=self.sample_dtype(), shape=prepend_shape+self.sample_shape(), name=name)

class DiagGaussianPdType(PdType):
    def __init__(self, size):
        self.size = size
    def pdclass(self):
        return DiagGaussianPd
    def param_shape(self):
        return [2*self.size]
    def sample_shape(self):
        return [self.size]
    def sample_dtype(self):
        return tf.float32

class DiagGaussianPd(Pd):
    def __init__(self, flat):
        self.flat = flat
        mean, logstd = tf.split(axis=len(flat.shape)-1, num_or_size_splits=2, value=flat)
        self.mean = mean
        self.logstd = logstd
        self.std = tf.exp(logstd)
    def flatparam(self):
        return self.flat
    def mode(self):
        return self.mean
    def neglogp(self, x):
        return 0.5 * tf_sum(tf.square((x - self.mean) / self.std), axis=-1) \
               + 0.5 * np.log(2.0 * np.pi) * tf.to_float(tf.shape(x)[-1]) \
               + tf_sum(self.logstd, axis=-1)
    def kl(self, other):
        assert isinstance(other, DiagGaussianPd)
        return tf_sum(other.logstd - self.logstd + (tf.square(self.std) + tf.square(self.mean - other.mean)) / (2.0 * tf.square(other.std)) - 0.5, axis=-1)
    def entropy(self):
        return tf_sum(self.logstd + .5 * np.log(2.0 * np.pi * np.e), axis=-1)
    def sample(self):
        return self.mean + self.std * tf.random_normal(tf.shape(self.mean))
    @classmethod
    def fromflat(cls, flat):
        return cls(flat)

class VecEnv(object):
    """
    Vectorized environment base class
    """
    def step(self, vac):
        """
        Apply sequence of actions to sequence of environments
        actions -> (observations, rewards, news)
        where 'news' is a boolean vector indicating whether each element is new.
        """
        raise NotImplementedError
    def reset(self):
        """
        Reset all environments
        """
        raise NotImplementedError
    def close(self):
        pass

class DummyVecEnv(VecEnv):
    def __init__(self, env_fns):
        self.envs = [fn() for fn in env_fns]
        env = self.envs[0]
        self.action_space = env.action_space
        self.observation_space = env.observation_space
        self.ts = np.zeros(len(self.envs), dtype='int')
    def step(self, action_n):
        results = [env.step(a) for (a,env) in zip(action_n, self.envs)]
        obs, rews, dones, infos = map(np.array, zip(*results))
        self.ts += 1
        for (i, done) in enumerate(dones):
            if done:
                obs[i] = self.envs[i].reset()
                self.ts[i] = 0
        return np.array(obs), np.array(rews), np.array(dones), infos
    def reset(self):
        results = [env.reset() for env in self.envs]
        return np.array(results)

    def render(self):
        return self.envs[0].render()
    @property
    def num_envs(self):
        return len(self.envs)

class DummyVecTestEnv(VecEnv):
    def __init__(self, env_fns):
        self.envs = [fn() for fn in env_fns]
        env = self.envs[0]
        self.action_space = env.action_space
        self.observation_space = env.observation_space
        self.ts = np.zeros(len(self.envs), dtype='int')
    def step(self, action_n):
        results = [env.step(a) for (a,env) in zip(action_n, self.envs)]
        obs, rews, dones, infos = map(np.array, zip(*results))
        return np.array(obs), np.array(rews), np.array(dones), infos
    def reset(self):
        results = [env.reset() for env in self.envs]
        return np.array(results)

    def render(self):
        return self.envs[0].render()
    @property
    def num_envs(self):
        return len(self.envs)

class VecNormalize(VecEnv):
    """
    Vectorized environment base class
    """
    def __init__(self, venv, ob=True, ret=True, clipob=10., cliprew=10., gamma=0.99, epsilon=1e-8):
        self.venv = venv
        self._observation_space = self.venv.observation_space
        self._action_space = venv.action_space
        self.ob_rms = RunningMeanStd(shape=self._observation_space.shape) if ob else None
        self.ret_rms = RunningMeanStd(shape=()) if ret else None
        self.clipob = clipob
        self.cliprew = cliprew
        self.ret = np.zeros(self.num_envs)
        self.gamma = gamma
        self.epsilon = epsilon
    def step(self, vac):
        """
        Apply sequence of actions to sequence of environments
        actions -> (observations, rewards, news)
        where 'news' is a boolean vector indicating whether each element is new.
        """
        obs, rews, news, infos = self.venv.step(vac)
        self.ret = self.ret * self.gamma + rews
        obs = self._obfilt(obs)
        if self.ret_rms:
            self.ret_rms.update(self.ret)
            rews = np.clip(rews / np.sqrt(self.ret_rms.var + self.epsilon), -self.cliprew, self.cliprew)
        return obs, rews, news, infos
    def _obfilt(self, obs):
        if self.ob_rms:
            self.ob_rms.update(obs)
            obs = np.clip((obs - self.ob_rms.mean) / np.sqrt(self.ob_rms.var + self.epsilon), -self.clipob, self.clipob)
            return obs
        else:
            return obs
    def reset(self):
        """
        Reset all environments
        """
        obs = self.venv.reset()
        return self._obfilt(obs)
    @property
    def action_space(self):
        return self._action_space
    @property
    def observation_space(self):
        return self._observation_space
    def close(self):
        self.venv.close()
    @property
    def num_envs(self):
        return self.venv.num_envs

class VecNormalizeTest(VecEnv):
    def __init__(self, venv, mean, var, clipob=10., epsilon=1e-8):
        self.venv = venv
        self._observation_space = self.venv.observation_space
        self._action_space = venv.action_space

        self.mean = mean
        self.var = var
        self.clipob = clipob
        self.epsilon = epsilon

    def render(self):
        return self.venv.render()

    def step(self, vac):
        obs, rews, dones, infos = self.venv.step(vac)
        obs = self._obfilt(obs)
        return obs, rews, dones, infos

    def _obfilt(self, obs):
        obs = np.clip((obs - self.mean) / np.sqrt(self.var + self.epsilon), -self.clipob, self.clipob)
        return obs

    def reset(self):
        obs = self.venv.reset()
        return self._obfilt(obs)

    @property
    def action_space(self):
        return self._action_space
    @property
    def observation_space(self):
        return self._observation_space
    def close(self):
        self.venv.close()
    @property
    def num_envs(self):
        return self.venv.num_envs

def worker(remote, parent_remote, env_fn_wrapper):
    parent_remote.close()
    env = env_fn_wrapper.x()
    while True:
        cmd, data = remote.recv()
        if cmd == 'step':
            ob, reward, done, info = env.step(data)
            if done:
                ob = env.reset()
            remote.send((ob, reward, done, info))
        elif cmd == 'reset':
            ob = env.reset()
            remote.send(ob)
        elif cmd == 'reset_task':
            ob = env.reset_task()
            remote.send(ob)
        elif cmd == 'close':
            remote.close()
            break
        elif cmd == 'get_spaces':
            remote.send((env.action_space, env.observation_space))
        else:
            raise NotImplementedError

class CloudpickleWrapper(object):
    """
    Uses cloudpickle to serialize contents (otherwise multiprocessing tries to use pickle)
    """
    def __init__(self, x):
        self.x = x
    def __getstate__(self):
        import cloudpickle
        return cloudpickle.dumps(self.x)
    def __setstate__(self, ob):
        import pickle
        self.x = pickle.loads(ob)

class SubprocVecEnv(VecEnv):
    def __init__(self, env_fns):
        """
        envs: list of gym environments to run in subprocesses
        """
        self.closed = False
        nenvs = len(env_fns)
        self.remotes, self.work_remotes = zip(*[Pipe() for _ in range(nenvs)])
        self.ps = [Process(target=worker, args=(work_remote, remote, CloudpickleWrapper(env_fn)))
            for (work_remote, remote, env_fn) in zip(self.work_remotes, self.remotes, env_fns)]
        for p in self.ps:
            p.daemon = True # if the main process crashes, we should not cause things to hang
            p.start()
        for remote in self.work_remotes:
            remote.close()

        self.remotes[0].send(('get_spaces', None))
        self.action_space, self.observation_space = self.remotes[0].recv()

    def step(self, actions):
        for remote, action in zip(self.remotes, actions):
            remote.send(('step', action))
        results = [remote.recv() for remote in self.remotes]
        obs, rews, dones, infos = zip(*results)
        return np.stack(obs), np.stack(rews), np.stack(dones), infos

    def reset(self):
        for remote in self.remotes:
            remote.send(('reset', None))
        return np.stack([remote.recv() for remote in self.remotes])

    def reset_task(self):
        for remote in self.remotes:
            remote.send(('reset_task', None))
        return np.stack([remote.recv() for remote in self.remotes])

    def close(self):
        if self.closed:
            return

        for remote in self.remotes:
            remote.send(('close', None))
        for p in self.ps:
            p.join()
        self.closed = True

    @property
    def num_envs(self):
        return len(self.remotes)

class RunningMeanStd(object):
    # https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm
    def __init__(self, epsilon=1e-4, shape=()):
        self.mean = np.zeros(shape, 'float64')
        self.var = np.zeros(shape, 'float64')
        self.count = epsilon

    def update(self, x):
        batch_mean = np.mean(x, axis=0)
        batch_var = np.var(x, axis=0)
        batch_count = x.shape[0]

        delta = batch_mean - self.mean
        tot_count = self.count + batch_count

        new_mean = self.mean + delta * batch_count / tot_count
        m_a = self.var * (self.count)
        m_b = batch_var * (batch_count)
        M2 = m_a + m_b + np.square(delta) * self.count * batch_count / (self.count + batch_count)
        new_var = M2 / (self.count + batch_count)

        new_count = batch_count + self.count

        self.mean = new_mean
        self.var = new_var
        self.count = new_count

class Monitor(Wrapper):
    EXT = "monitor.csv"
    f = None

    def __init__(self, env, filename, allow_early_resets=False, reset_keywords=()):
        Wrapper.__init__(self, env=env)
        self.tstart = time.time()
        if filename is None:
            self.f = None
            self.logger = None
        else:
            if not filename.endswith(Monitor.EXT):
                if osp.isdir(filename):
                    filename = osp.join(filename, Monitor.EXT)
                else:
                    filename = filename + "." + Monitor.EXT
            self.f = open(filename, "wt")
            self.f.write('#%s\n'%json.dumps({"t_start": self.tstart, "gym_version": gym.__version__,
                "env_id": env.spec.id if env.spec else 'Unknown'}))
            self.logger = csv.DictWriter(self.f, fieldnames=('r', 'l', 't')+reset_keywords)
            self.logger.writeheader()

        self.reset_keywords = reset_keywords
        self.allow_early_resets = allow_early_resets
        self.rewards = None
        self.needs_reset = True
        self.episode_rewards = []
        self.episode_lengths = []
        self.total_steps = 0
        self.current_reset_info = {} # extra info about the current episode, that was passed in during reset()

    def _reset(self, **kwargs):
        if not self.allow_early_resets and not self.needs_reset:
            raise RuntimeError("Tried to reset an environment before done. If you want to allow early resets, wrap your env with Monitor(env, path, allow_early_resets=True)")
        self.rewards = []
        self.needs_reset = False
        for k in self.reset_keywords:
            v = kwargs.get(k)
            if v is None:
                raise ValueError('Expected you to pass kwarg %s into reset'%k)
            self.current_reset_info[k] = v
        return self.env.reset(**kwargs)

    def _step(self, action):
        if self.needs_reset:
            raise RuntimeError("Tried to step environment that needs reset")
        ob, rew, done, info = self.env.step(action)
        self.rewards.append(rew)
        if done:
            self.needs_reset = True
            eprew = sum(self.rewards)
            eplen = len(self.rewards)
            epinfo = {"r": round(eprew, 6), "l": eplen, "t": round(time.time() - self.tstart, 6)}
            epinfo.update(self.current_reset_info)
            if self.logger:
                self.logger.writerow(epinfo)
                self.f.flush()
            self.episode_rewards.append(eprew)
            self.episode_lengths.append(eplen)
            info['episode'] = epinfo
        self.total_steps += 1
        return (ob, rew, done, info)

    def close(self):
        if self.f is not None:
            self.f.close()

    def get_total_steps(self):
        return self.total_steps

    def get_episode_rewards(self):
        return self.episode_rewards

    def get_episode_lengths(self):
        return self.episode_lengths


text:
#! /usr/bin/enc python
# -*- coding: utf-8 -*-
# author: Irving He
# email: 1910646@tongji.edu.cn

import pickle
import random

from tensorboardX import SummaryWriter
import logging
import numpy as np
import imageio
from glob import glob # glob.glob():与Linux ls命令类似
import os
import yaml
import gym
# import dmc2gym
from multiprocessing import Queue

OPENAI_MUJOCO_PREFIX = [
    "Hopper","Humaniod"
]

"""
1. Logger
2. OU Noise
3. GiF making API
4. .yaml config reading API
5. torch queue clear API
6. PER
7. ReplayBuffer (Independicies: segment Tree)
    7.1 ReplayBuffer Ver1
    7.2 Prioritized Experience Replay Buffer
8. Env
    8.1 EnvWrapper
    8,2 CreateEnvWrapper
"""

logger = logging.getLogger(__name__)

# 就是一个SummaryWriter的封装形式
class Logger(object):
    def __init__(self, log_dir):
        """
        General logger.
        Args:
            log_dir (str): log directory
        """
        self.writer = SummaryWriter(log_dir)
        self.info = logger.info
        self.debug = logger.debug
        self.warning = logger.warning

    def scalar_summary(self, tag, value, step):
        """
        Log scalar value to the disk.
        Args:
            tag (str): name of the value
            value (float): value
            step (int): update step
        """
        self.writer.add_scalar(tag, value, step)

    def close(self):
        self.writer.close()

# OU Noise
class OUNoise(object):
    def __init__(self, dim, low, high, mu=0.0, theta=0.15, max_sigma=0.3, min_sigma=0.3, decay_period=10_000):
        self.mu = mu
        self.theta = theta
        self.sigma = max_sigma
        self.max_sigma = max_sigma
        self.min_sigma = min_sigma
        self.decay_period = decay_period
        self.action_dim = dim
        self.low = low
        self.high = high

    def reset(self):
        self.state = np.ones(self.action_dim) * self.mu

    def evolve_state(self):
        x = self.state
        dx = self.theta * (self.mu - x) + self.sigma * np.random.randn(self.action_dim)
        self.state = x + dx
        return self.state

    def get_action(self, action, t=0):
        # Action with OU Noise
        ou_state = self.evolve_state()
        self.sigma = self.max_sigma - (self.max_sigma - self.min_sigma) * min(1.0, t/self.decay_period)
        action = action.cpu().detach().numpy()
        return np.clip(action + ou_state, self.low, self.high)

# Gif Making
def make_gif(source_dir, output):
    """
    Make gif file from set of .jpeg images.
    Args:
        source_dir (str): path with .jpeg images
        output (str): path to the output .gif file
    Returns: None
    """
    batch_sort = lambda s: int(s[s.rfind('/')+1:s.rfind('.')])
    image_paths = sorted(glob(os.path.join(source_dir, "*.png")),
                         key=batch_sort)
    images = []
    for filename in image_paths:
        images.append(imageio.imread(filename))
    imageio.mimsave(output, images)

# .yaml config reading API
def read_config(path):
    """
    Return python dict from .yml file.
    Args:
        path (str): path to the .yml config.
    Returns (dict): configuration object.
    """
    with open(path, 'r') as ymlfile:
        # yaml.load(file, Loader=yaml.FullLoader)
        cfg = yaml.load(ymlfile,Loader=yaml.FullLoader)
    return cfg

# empty torch queue
def empty_torch_queue(q):
    while True:
        try:
            o = q.get_nowait()
            del o
        except:
            break
    q.close()

'''
## Prioritised Experience Replay (PER) Memory ##
# Adapted from: https://github.com/openai/baselines/blob/master/baselines/deepq/replay_buffer.py
# Creates prioritised replay memory buffer to add experiences to and sample batches of experiences from
'''
class ReplayBuffer(object):
    def __init__(self, size):
        """
        Create replay buffer.
        Args:
            size (int): max number of transitions to store in the buffer. When the buffer
            overflows the old memories are dropped.
        """
        self._storage = []
        self._maxsize = size
        self._next_idx = 0

    def __len__(self):
        return len(self._storage)

    def add(self, obs_t, action, reward, obs_tp1, done, gamma):
        data = (obs_t, action, reward, obs_tp1, done, gamma)

        self._storage.append(data)

        self._next_idx += 1

    def remove(self, num_samples):
        del self._storage[:num_samples]
        self._next_idx = len(self._storage)

    def _encode_sample(self, idxes):
        obses_t, actions, rewards, obses_tp1, dones, gammas = [], [], [], [], [], []
        for i in idxes:
            data = self._storage[i]
            obs_t, action, reward, obs_tp1, done, gamma = data
            obses_t.append(np.array(obs_t, copy=False))
            actions.append(np.array(action, copy=False))
            rewards.append(reward)
            obses_tp1.append(np.array(obs_tp1, copy=False))
            dones.append(done)
            gammas.append(gamma)
        return [np.array(obses_t), np.array(actions), np.array(rewards), np.array(obses_tp1), np.array(dones), np.array(
            gammas)]

    def sample(self, batch_size, **kwags):
        """Sample a batch of experiences.
        Parameters
        ----------
        batch_size: int
            How many transitions to sample.
        Returns
        -------
        obs_batch: np.array
            batch of observations
        act_batch: np.array
            batch of actions executed given obs_batch
        rew_batch: np.array
            rewards received as results of executing act_batch
        next_obs_batch: np.array
            next set of observations seen after executing act_batch
        done_mask: np.array
            done_mask[i] = 1 if executing act_batch[i] resulted in
            the end of an episode and 0 otherwise.
        gammas: np.array
            product of gammas for N-step returns
        """
        idxes = [random.randint(0, len(self._storage) - 1) for _ in range(batch_size)]
        weights = np.zeros(len(idxes))
        inds = np.zeros(len(idxes))
        return self._encode_sample(idxes) + [weights, inds]

    def dump(self, save_dir):
        fn = os.path.join(save_dir, "replay_buffer.pkl")
        with open(fn, 'wb') as f:
            pickle.dump(self._storage, f)
        print(f"Buffer dumped to {fn}")

class PrioritizedReplayBuffer(ReplayBuffer):
    def __init__(self, size, alpha):
        """Create Prioritized Replay buffer.
        Parameters
        ----------
        size: int
            Max number of transitions to store in the buffer. When the buffer
            overflows the old memories are dropped.
        alpha: float
            how much prioritization is used
            (0 - no prioritization, 1 - full prioritization)
        See Also
        --------
        ReplayBuffer.__init__
        """
        super(PrioritizedReplayBuffer, self).__init__(size)
        assert alpha >= 0
        self._alpha = alpha

        self.it_capacity = 1
        while self.it_capacity < size * 2:  # We use double the soft capacity of the PER for the segment trees to allow for any overflow over the soft capacity limit before samples are removed
            self.it_capacity *= 2

        self._it_sum = SumSegmentTree(self.it_capacity)
        self._it_min = MinSegmentTree(self.it_capacity)
        self._max_priority = 1.0

    def add(self, *args, **kwargs):
        idx = self._next_idx
        assert idx < self.it_capacity, "Number of samples in replay memory exceeds capacity of segment trees. Please increase capacity of segment trees or increase the frequency at which samples are removed from the replay memory"

        super().add(*args, **kwargs)
        self._it_sum[idx] = self._max_priority ** self._alpha
        self._it_min[idx] = self._max_priority ** self._alpha

    def remove(self, num_samples):
        super().remove(num_samples)
        self._it_sum.remove_items(num_samples)
        self._it_min.remove_items(num_samples)

    def _sample_proportional(self, batch_size):
        res = []
        p_total = self._it_sum.sum(0, len(self._storage) - 1)
        every_range_len = p_total / batch_size
        for i in range(batch_size):
            mass = random.random() * every_range_len + i * every_range_len
            idx = self._it_sum.find_prefixsum_idx(mass)
            res.append(idx)
        return res

    def sample(self, batch_size, beta):
        """Sample a batch of experiences.
        compared to ReplayBuffer.sample
        it also returns importance weights and idxes
        of sampled experiences.
        Parameters
        ----------
        batch_size: int
            How many transitions to sample.
        beta: float
            To what degree to use importance weights
            (0 - no corrections, 1 - full correction)
        Returns
        -------
        obs_batch: np.array
            batch of observations
        act_batch: np.array
            batch of actions executed given obs_batch
        rew_batch: np.array
            rewards received as results of executing act_batch
        next_obs_batch: np.array
            next set of observations seen after executing act_batch
        done_mask: np.array
            done_mask[i] = 1 if executing act_batch[i] resulted in
            the end of an episode and 0 otherwise.
        gammas: np.array
            product of gammas for N-step returns
        weights: np.array
            Array of shape (batch_size,) and dtype np.float32
            denoting importance weight of each sampled transition
        idxes: np.array
            Array of shape (batch_size,) and dtype np.int32
            idexes in buffer of sampled experiences
        """
        assert beta > 0

        idxes = self._sample_proportional(batch_size)

        weights = []
        p_min = self._it_min.min() / self._it_sum.sum()
        max_weight = (p_min * len(self._storage)) ** (-beta)

        for idx in idxes:
            p_sample = self._it_sum[idx] / self._it_sum.sum()
            weight = (p_sample * len(self._storage)) ** (-beta)
            weights.append(weight / max_weight)
        weights = np.array(weights)
        encoded_sample = self._encode_sample(idxes)
        return tuple(list(encoded_sample) + [weights, idxes])

    def update_priorities(self, idxes, priorities):
        """Update priorities of sampled transitions.
        sets priority of transition at index idxes[i] in buffer
        to priorities[i].
        Parameters
        ----------
        idxes: [int]
            List of idxes of sampled transitions
        priorities: [float]
            List of updated priorities corresponding to
            transitions at the sampled idxes denoted by
            variable `idxes`.
        """
        assert len(idxes) == len(priorities)
        for idx, priority in zip(idxes, priorities):
            assert priority > 0
            assert 0 <= idx < len(self._storage)
            self._it_sum[idx] = priority ** self._alpha
            self._it_min[idx] = priority ** self._alpha

            self._max_priority = max(self._max_priority, priority)

    def dump(self, save_dir):
        fn = os.path.join(save_dir, "replay_buffer.pkl")
        with open(fn, 'wb') as f:
            pickle.dump(self._storage, f)
        print(f"Buffer dumped to {fn}")

'''
## Segment Tree ##
# Adapted from: https://github.com/openai/baselines/blob/master/baselines/common/segment_tree.py
# Segment tree data structures used to store the priorities of the samples in the PER for efficient priority-based sampling.
'''

import operator
class SegmentTree(object):
    def __init__(self, capacity, operation, neutral_element):
        """Build a Segment Tree data structure.
        https://en.wikipedia.org/wiki/Segment_tree
        Can be used as regular array, but with two
        important differences:
            a) setting item's value is slightly slower.
               It is O(lg capacity) instead of O(1).
            b) user has access to an efficient ( O(log segment size) )
               `reduce` operation which reduces `operation` over
               a contiguous subsequence of items in the array.
        Paramters
        ---------
        capacity: int
            Total size of the array - must be a power of two.
        operation: lambda obj, obj -> obj
            and operation for combining elements (eg. sum, max)
            must form a mathematical group together with the set of
            possible values for array elements (i.e. be associative)
        neutral_element: obj
            neutral element for the operation above. eg. float('-inf')
            for max and 0 for sum.
        """
        assert capacity > 0 and capacity & (capacity - 1) == 0, "capacity must be positive and a power of 2."
        self._capacity = capacity
        self.neutral_element = neutral_element
        self._value = [neutral_element for _ in range(2 * capacity)]
        self._operation = operation

    def _reduce_helper(self, start, end, node, node_start, node_end):
        if start == node_start and end == node_end:
            return self._value[node]
        mid = (node_start + node_end) // 2
        if end <= mid:
            return self._reduce_helper(start, end, 2 * node, node_start, mid)
        else:
            if mid + 1 <= start:
                return self._reduce_helper(start, end, 2 * node + 1, mid + 1, node_end)
            else:
                return self._operation(
                    self._reduce_helper(start, mid, 2 * node, node_start, mid),
                    self._reduce_helper(mid + 1, end, 2 * node + 1, mid + 1, node_end)
                )

    def reduce(self, start=0, end=None):
        """Returns result of applying `self.operation`
        to a contiguous subsequence of the array.
            self.operation(arr[start], operation(arr[start+1], operation(... arr[end])))
        Parameters
        ----------
        start: int
            beginning of the subsequence
        end: int
            end of the subsequences
        Returns
        -------
        reduced: obj
            result of reducing self.operation over the specified range of array elements.
        """
        if end is None:
            end = self._capacity
        if end < 0:
            end += self._capacity
        end -= 1
        return self._reduce_helper(start, end, 1, 0, self._capacity - 1)

    def __setitem__(self, idx, val):
        # index of the leaf
        idx += self._capacity
        self._value[idx] = val
        idx //= 2
        while idx >= 1:
            self._value[idx] = self._operation(
                self._value[2 * idx],
                self._value[2 * idx + 1]
            )
            idx //= 2

    def __getitem__(self, idx):
        assert 0 <= idx < self._capacity
        return self._value[self._capacity + idx]

    def remove_items(self, num_items):
        """Removes num_items leaf nodes from the tree and appends num_items leaf nodes of neutral_element value (0 or inf) to end of tree,
           effectively left shifting remaining leaf nodes by num_items"""
        del self._value[self._capacity:(self._capacity + num_items)]
        neutral_elements = [self.neutral_element for _ in range(num_items)]
        self._value += neutral_elements
        for idx in range(self._capacity - 1, 0, -1):
            self._value[idx] = self._operation(
                self._value[2 * idx],
                self._value[2 * idx + 1]
            )

'''
## Segment Tree ##
# Adapted from: https://github.com/openai/baselines/blob/master/baselines/common/segment_tree.py
# Segment tree data structures used to store the priorities of the samples in the PER for efficient priority-based sampling.
'''
class SumSegmentTree(SegmentTree):
    def __init__(self, capacity):
        super(SumSegmentTree, self).__init__(
            capacity=capacity,
            operation=operator.add,
            neutral_element=0.0
        )

    def sum(self, start=0, end=None):
        """Returns arr[start] + ... + arr[end]"""
        return super(SumSegmentTree, self).reduce(start, end)

    def find_prefixsum_idx(self, prefixsum):
        """Find the highest index `i` in the array such that
            sum(arr[0] + arr[1] + ... + arr[i - i]) <= prefixsum
        if array values are probabilities, this function
        allows to sample indexes according to the discrete
        probability efficiently.
        Parameters
        ----------
        perfixsum: float
            upperbound on the sum of array prefix
        Returns
        -------
        idx: int
            highest index satisfying the prefixsum constraint
        """
        assert 0 <= prefixsum <= self.sum() + 1e-5
        idx = 1
        while idx < self._capacity:  # while non-leaf
            if self._value[2 * idx] > prefixsum:
                idx = 2 * idx
            else:
                prefixsum -= self._value[2 * idx]
                idx = 2 * idx + 1
        return idx - self._capacity
class MinSegmentTree(SegmentTree):
    def __init__(self, capacity):
        super(MinSegmentTree, self).__init__(
            capacity=capacity,
            operation=min,
            neutral_element=float('inf')
        )

    def min(self, start=0, end=None):
        """Returns min(arr[start], ...,  arr[end])"""

        return super(MinSegmentTree, self).reduce(start, end)

def create_replay_buffer(config):
    size = config['replay_mem_size']
    if config['replay_memory_prioritized']:
        alpha = config['priority_alpha']
        return PrioritizedReplayBuffer(size=size, alpha=alpha)
    return ReplayBuffer(size)

"""
EnvWrapper
"""

class EnvWrapper:
    def __init__(self,env_name):
        self.env_name = env_name
        open_ai_env = len([pref for pref in OPENAI_MUJOCO_PREFIX if pref in env_name]) > 0
        if open_ai_env:
            self.env = gym.make(self.env_name)
        else:
            print("env error...")

    def reset(self):
        state = self.env.reset()
        return state

    def get_random_action(self):
        action = self.env.action_space.sample()
        return action

    def step(self, action):
        next_state, reward, terminal, _ = self.env.step(action.ravel())
        return next_state, reward, terminal

    def set_random_seed(self, seed):
        self.env.seed(seed)

    def render(self):
        frame = self.env.render(mode='rgb_array')
        return frame

    def close(self):
        self.env.close()

    def get_action_space(self):
        return self.env.action_space

    def normalise_state(self, state):        return state

    def normalise_reward(self, reward):
        return reward

def create_env_wrapper(config):
    env_name = config['env']
    return EnvWrapper(env_name)

max_stars_repo_path: Distributional RL/D4PG/utils.py

max_issues_repo_path: Distributional RL/D4PG/utils.py

max_issues_repo_name: HzcIrving/DLRL-PlayGround

max_forks_repo_path: Distributional RL/D4PG/utils.py

max_forks_repo_name: HzcIrving/DLRL-PlayGround


text:
import sys
import os
sys.path.append(os.path.abspath('.'))
sys.path.append(os.path.abspath('../'))
sys.path.append(os.path.abspath('../../'))

import matplotlib.pyplot as plt
import numpy as np

from fusion.scheduling import batch_size
from fusion.scheduling import Resource
from fusion.scheduling import LoopLowerBound
from fusion.scheduling import ScheduleGenerator
from fusion.scheduling import extract_arch_info, extract_dataflow_info
from fusion.scheduling import CostModel
from fusion.scheduling import res_parse

from fusion.nn_models import import_network

def do_scheduling():
    """
    Get optimal scheduling for given problem. Return a result schedule.
    """

    buffer = [128, 128, 256, 256, 512, 512, 512, 512]
    pe_array = [16, 32, 16, 32, 16, 16, 32, 64]

    # Network.
    batch_size.init(4)
    network = import_network("squeezenet")

    dataflow_info = extract_dataflow_info('./fusion/dataflow/dataflow_Ow_Cout.json')

    access_list = []
    energy_list = []

    for pe, bf in zip(pe_array, buffer):
        arch_file = './fusion/arch/3_level_mem_{}KB.json'.format(bf)
        arch_info = extract_arch_info(arch_file)
        arch_info["parallel_count"][0] = pe ** 2
        if pe == 8:
            arch_info["parallel_cost"][0] = 0.05
        resource = Resource.arch(arch_info)

        # Unroll loop lower bound
        dataflow_info["partitioning_size"] = [pe] * len(dataflow_info["partitioning_size"])
        loop_lower_bound = LoopLowerBound.dataflow(dataflow_info)

        print("\n===========================================================")
        print('PE-array: {}x{}, buffer size: {}(KB)'.format(pe, pe, bf))
        print("waiting...")
        cost_model = CostModel(network, resource)

        # optimal schedule
        sg = ScheduleGenerator(network, resource, cost_model, loop_lower_bound)
        schedule_info_list, _ = sg.schedule_search()
        print("done!\n\n")
        energy, access = res_parse(schedule_info_list, resource,
                                   cost_model, sg, network,
                                   loop_lower_bound,
                                   './result/pe_array', arch_info)
        energy_list.append(energy)
        access_list.append(access)

    x = ["16x16,128", "32x32,128", "16x16,256", "32x32,256", "8x8,512", "16x16,512", "32x32,512", "64x64,512"]
    energy_list = np.array(energy_list) / energy_list[0]
    access_list = np.array(access_list) / access_list[0]
    plt.figure(figsize=(8, 2))
    plt.plot(x, energy_list, label="Normalized Energy")
    plt.plot(x, access_list, label="Normalized DRAM Access")
    plt.ylim(0.2, 1.2)
    plt.legend()
    plt.savefig('./result/pe_array/pe_array.png')
    plt.show()

def main():
    """
    Main function.
    """

    do_scheduling()
    return 0

if __name__ == '__main__':
    sys.exit(main())

max_stars_repo_name: SheaCai/optimus

max_issues_repo_name: SheaCai/optimus

max_forks_repo_name: SheaCai/optimus


text:
import numpy as np

from src.utils.distances import hausdorff

def make_distance_matrix_for_segments(vectorized_segments, distance='hausdorff'):
    """
    Returns the distance matrix for the documents having the given segments.

    :param vectorized_segments: array of size M, where each element is a "bag" of segments: matrix of
        shape (*,NUM_FEATURES), and each row on this matrix is the feature vector for one segment.
    :param distance: how to compare the two bags
    :return: pairwise distance matrix (MxM matrix)
    """
    if distance.lower().strip() != 'hausdorff':
        raise Exception("Only 'hausdorff' distance supported right now.")

    num_samples = len(vectorized_segments)

    distance_function = lambda a, b: hausdorff(a.toarray(), b.toarray())

    distance_matrix = np.zeros((num_samples, num_samples))

    for i, segments_for_document_a in enumerate(vectorized_segments):
        for j, segments_for_document_b in enumerate(vectorized_segments):
            distance = distance_function(segments_for_document_a, segments_for_document_b)
            distance_matrix[i][j] = distance

    return distance_matrix

def vectorize_segments(segments, vectorizer):
    """
    Turn each segment into a bag-of-words array using the provided vectorizer.

    :param segments: array of size M, where each element is a "bag" of segments, i.e. a list of
        strings.
    :param vectorizer: instance of CountVectorizer, TfIdfvectorizer, etc.
    :return: vectorized segments (array of size M where each element is a matrix)
    """
    vectorized_segments_list = []

    for segments in segments:
        vectorized = vectorizer.transform(segments)
        vectorized_segments_list.append(vectorized)

    vectorized_segments = np.array(vectorized_segments_list)

    return vectorized_segments

max_stars_repo_name: queirozfcom/auto-tagger

max_issues_repo_name: queirozfcom/auto-tagger

max_forks_repo_name: queirozfcom/auto-tagger


text:
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Created on Fri May  1 10:09:24 2015

@author: ddboline
"""
#from __future__ import absolute_import
#from __future__ import division
#from __future__ import print_function
#from __future__ import unicode_literals

import os
import gzip
import cPickle as pickle

import numpy as np

from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.cross_validation import train_test_split
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import mean_squared_error

from load_data import load_data

NCPU = len([x for x in open('/proc/cpuinfo').read().split('\n')
            if x.find('processor') == 0])

def transform_to_log(y):
    return np.log1p(y)

def transform_from_log(ly):
    return np.round(np.expm1(ly)).astype(int)

def scorer(estimator, X, y):
    ypred = estimator.predict(X)
    return 1.0/mean_squared_error(ypred, y)

def train_model_parallel_xgb(xtrain, ytrain, index=0, depth=2):
    import xgboost as xgb
    xTrain, xTest, yTrain, yTest = train_test_split(xtrain, ytrain[:, index],
                                                    test_size=0.25)
    dtrain = xgb.DMatrix(xTrain, label=yTrain)
    dtest = xgb.DMatrix(xTest, label=yTest)

    param = {'bst:max_depth':depth,
             'bst:eta':0.3,
             'silent':1,
             'objective':'count:poisson' }
    param['nthread'] = NCPU
    plst = param.items()
    plst += [('eval_metric', 'rmse')]

    evallist  = [(dtest,'eval'), (dtrain,'train')]
    num_round = 100
    model = xgb.train(plst, dtrain, num_round, evallist,
                    early_stopping_rounds=10)
    with gzip.open('model_bst_%d.pkl.gz' % index, 'wb') as pklfile:
        pickle.dump(model, pklfile, protocol=2)

def test_model_parallel_xgb(xtrain, ytrain):
    import xgboost as xgb
    print('start test_model_parallel_xgb')
    xTrain, xTest, yTrain, yTest = train_test_split(xtrain, ytrain,
                                                    test_size=0.25)
    ypred = np.zeros((yTest.shape[0], 3))
    dtest = xgb.DMatrix(xTest)
    for idx in range(3):
        with gzip.open('model_bst_%d.pkl.gz' % idx, 'rb') as pklfile:
            model = pickle.load(pklfile)
        ypred[:, idx] = model.predict(dtest, ntree_limit=model.best_iteration)
    rmsle = np.sqrt(mean_squared_error(yTest, ypred))
    print('\nRMSLE %s\n' % rmsle)
    return rmsle

def prepare_submission_parallel_xgb(xtest, ytest):
    import xgboost as xgb
    print('start prepare_submission_parallel_xgb')
    YLABELS = [u'*', u'**', u'***']
    print(ytest.columns)
    dtest = xgb.DMatrix(xtest)
    for idx in range(3):
        model = xgb.Booster({'nthread':NCPU})
        with gzip.open('model_bst_%d.pkl.gz' % idx, 'rb') as pklfile:
            model = pickle.load(pklfile)
        key = YLABELS[idx]
        ytest.loc[:, key] = transform_from_log(model.predict(dtest,
                                               ntree_limit=model.best_iteration))
    print(ytest.shape)
    with gzip.open('submission.csv.gz', 'wb') as subfile:
        ytest.to_csv(subfile, index=False)
    return

def train_model_parallel(xtrain, ytrain, index=0):
    xTrain, xTest, yTrain, yTest = train_test_split(xtrain, ytrain[:, index],
                                                    test_size=0.25)
#    model = RandomForestRegressor()
#    model = LogisticRegression()
    model = GradientBoostingRegressor(verbose=1)

    n_est = [10, 50]
    m_dep = [5, 3]

    model = GridSearchCV(estimator=model,
                                param_grid=dict(n_estimators=n_est,
                                                max_depth=m_dep),
                                scoring=scorer,
                                n_jobs=-1, verbose=1)

    model.fit(xTrain, yTrain)
    ypred = model.predict(xTest)
    if hasattr(model, 'best_params_'):
        print('best_params', model.best_params_)
    print('score %d %s' % (index, model.score(xTest, yTest)))
    print('RMSLE %d %s' % (index, np.sqrt(mean_squared_error(yTest, ypred))))
    with gzip.open('model_%d.pkl.gz' % index, 'wb') as pklfile:
        pickle.dump(model, pklfile, protocol=2)
    return

def test_model_parallel(xtrain, ytrain):
    xTrain, xTest, yTrain, yTest = train_test_split(xtrain, ytrain,
                                                    test_size=0.25)
    ypred = np.zeros((yTest.shape[0], 3))
    for idx in range(3):
        with gzip.open('model_%d.pkl.gz' % idx, 'rb') as pklfile:
            model = pickle.load(pklfile)
        ypred[:, idx] = model.predict(xTest)
    rmsle = np.sqrt(mean_squared_error(yTest, ypred))
    print('RMSLE %s' % rmsle)
    return rmsle

def prepare_submission_parallel(xtest, ytest):
    YLABELS = [u'*', u'**', u'***']
    print(ytest.columns)
    for idx in range(3):
        with gzip.open('model_%d.pkl.gz' % idx, 'rb') as pklfile:
            model = pickle.load(pklfile)
        key = YLABELS[idx]
        ytest.loc[:, key] = transform_from_log(model.predict(xtest))
    print(ytest.shape)
    with gzip.open('submission.csv.gz', 'wb') as subfile:
        ytest.to_csv(subfile, index=False)
    return

def my_model(index=0):
    xtrain, ytrain, xtest, ytest = load_data()

    ytrain = transform_to_log(ytrain)

    results = {}
    for depth in range(10,20):
        for idx in range(3):
            train_model_parallel_xgb(xtrain, ytrain, index=idx, depth=depth)

        rmsle = test_model_parallel_xgb(xtrain, ytrain)
        prepare_submission_parallel_xgb(xtest, ytest)
        os.rename('submission.csv.gz',
                  'submission_dep_%d_20150603.csv.gz' % depth)
        results[depth] = rmsle

    for depth in sorted(results):
        print(depth, results[depth])

    return

if __name__ == '__main__':
    my_model()


text:
import os
import torch as T
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
from datetime import datetime

class DeepQNetwork(nn.Module):

    def __init__(self, lr, n_actions, name, input_dims, chkpt_dir, fc1_dims, fc2_dims, seed, optimizer):
        super(DeepQNetwork, self).__init__()
        self.checkpoint_dir = chkpt_dir
        self.checkpoint_file = os.path.join(self.checkpoint_dir, name)
        self.checkpoint_file_final = os.path.join(self.checkpoint_dir, str(name+'_final'))

        # Sets the seed for generating random numbers (here initial weights and biases?)
        self.seed = T.manual_seed(seed)

        # linear input connection from input to first hidden layer neurons
        # applies a linear transformation to the incoming data: y = xA^T + b
        self.fc1 = nn.Linear(input_dims,fc1_dims)

        # linear input connection from the hidden layer to output
        self.fc3 = nn.Linear(fc1_dims,n_actions)

        # Allows you to select optimizer with a string in the main method
        if optimizer == 'RMSprop':
            self.optimizer = optim.RMSprop(self.parameters(), lr=lr)
        elif optimizer == 'Adam':
            self.optimizer = optim.Adam(self.parameters(), lr=lr)
        elif optimizer == 'SGD':
            self.optimizer = optim.SGD(self.parameters(), lr=lr)

        self.loss = nn.MSELoss()
        self.device = T.device('cuda:0' if T.cuda.is_available() else 'cpu')
        self.to(self.device)

    def forward(self, state):
        observation = T.Tensor(state).to(self.device)
        x = F.relu(self.fc1(observation))
        actions = self.fc3(x)

        return actions

    def save_checkpoint(self):
        #print('... saving checkpoint ...')
        T.save(self.state_dict(), self.checkpoint_file)

    def save_checkpoint_final(self):
        #print('... saving final checkpoint ...')
        T.save(self.state_dict(), self.checkpoint_file_final)

    def load_checkpoint(self):
        #print('... loading checkpoint ...')
        self.load_state_dict(T.load(self.checkpoint_file))

    def load_checkpoint_final(self):
        #print('... loading final checkpoint ...')
        self.load_state_dict(T.load(self.checkpoint_file_final))

max_stars_repo_name: johannesruetten/ChargingEnvironment

max_issues_repo_name: johannesruetten/ChargingEnvironment

max_forks_repo_name: johannesruetten/ChargingEnvironment


text:
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
import six
import functools
import unittest

import nose.tools
import numpy as np
import pandas

import trackpy
from trackpy import predict

# Catch attempts to set values on an inadvertent copy of a Pandas object.
trackpy.utils.make_pandas_strict()

def mkframe(n=1, Nside=3):
    xg, yg = np.mgrid[:Nside,:Nside]
    dx = (n - 1)
    dy = -(n - 1)
    return pandas.DataFrame(
            dict(x=xg.flatten() + dx, y=yg.flatten() + dy, frame=n))

def link(frames, linker, *args, **kw):
    defaults = {'neighbor_strategy': 'KDTree'}
    defaults.update(kw)
    return pandas.concat(linker(frames,  *args, **defaults),
                       ignore_index=True)

def get_linked_lengths(frames, linker, *args, **kw):
    """Track particles and return the length of each trajectory."""
    return link(frames, linker, *args, **kw).groupby('particle').x.count()

Nside_oversize = int(np.sqrt(100)) # Make subnet linker fail

class BaselinePredictTests(unittest.TestCase):
    def test_null_predict(self):
        """Make sure that a prediction of no motion does not interfere
        with normal tracking.
        """
        # link_df_iter
        pred = predict.NullPredict()
        ll = get_linked_lengths((mkframe(0), mkframe(0.25)),
                                pred.link_df_iter, 0.45)
        assert all(ll.values == 2)

        # link_df
        pred = predict.NullPredict()
        ll_df = pred.link_df(pandas.concat((mkframe(0), mkframe(0.25))), 0.45)
        assert all(ll_df.groupby('particle').x.count().values == 2)

        # Make sure that keyword options are handled correctly
        # (This checks both link_df and link_df_iter)
        features = pandas.concat((mkframe(0), mkframe(0.25)))
        features.rename(columns=lambda n: n + '_', inplace=True)
        pred = predict.NullPredict()
        ll_df = pred.link_df(features, 0.45, t_column='frame_', pos_columns=['x_', 'y_'])
        assert all(ll_df.groupby('particle').x_.count().values == 2)

    def test_predict_decorator(self):
        """Make sure that a prediction of no motion does not interfere
        with normal tracking.
        """
        pred = predict.null_predict
        pred_link = functools.partial(trackpy.link_df_iter, predictor=pred)
        ll = get_linked_lengths((mkframe(0), mkframe(0.25)),
                                pred_link, 0.45)
        assert all(ll.values == 2)

    def test_fail_predict(self):
        ll = get_linked_lengths((mkframe(0), mkframe(0.25), mkframe(0.65)),
                                trackpy.link_df_iter, 0.45)
        assert not all(ll.values == 2)

    @nose.tools.raises(trackpy.SubnetOversizeException)
    def test_subnet_fail(self):
        Nside = Nside_oversize
        ll = get_linked_lengths((mkframe(0, Nside),
                                 mkframe(0.25, Nside),
                                 mkframe(0.75, Nside)), trackpy.link_df_iter, 1)

class VelocityPredictTests(object):
    def test_simple_predict(self):
        pred = self.predict_class()
        ll = get_linked_lengths((self.mkframe(0), self.mkframe(0.25), self.mkframe(0.65)),
                                pred.link_df_iter, 0.45)
        assert all(ll.values == 3)

    def test_big_predict(self):
        Nside = Nside_oversize
        pred = self.predict_class()
        ll = get_linked_lengths((self.mkframe(0, Nside), self.mkframe(0.25, Nside),
                                 self.mkframe(0.75, Nside)),
                                pred.link_df_iter, 0.45)
        assert all(ll.values == 3)

    def test_predict_newparticles(self):
        """New particles should get the velocities of nearby ones."""
        pred = self.predict_class()
        ll = get_linked_lengths((self.mkframe(0), self.mkframe(0.25), self.mkframe(0.65, 4),
                                self.mkframe(1.05, 4)),
                                pred.link_df_iter, 0.45)
        assert not any(ll.values == 1)

    def test_predict_memory(self):
        pred = self.predict_class()
        frames = [self.mkframe(0), self.mkframe(0.25), self.mkframe(0.65),
                  self.mkframe(1.05), self.mkframe(1.45)]
        ll = get_linked_lengths(frames, pred.link_df_iter, 0.45)
        assert all(ll.values == len(frames))

        # Knock out a particle. Make sure tracking fails.
        frames[3].x[5] = np.nan
        frames[3] = frames[3].dropna()
        pred = self.predict_class()
        tr = link(frames, pred.link_df_iter, 0.45)
        starts = tr.groupby('particle').frame.min()
        ends = tr.groupby('particle').frame.max()
        assert not all(ends - starts == 1.45)

        pred = self.predict_class()
        tr = link(frames, pred.link_df_iter, 0.45, memory=1)
        starts = tr.groupby('particle').frame.min()
        ends = tr.groupby('particle').frame.max()
        assert all(ends - starts == 1.45), 'Prediction with memory fails.'

    def test_predict_diagnostics(self):
        """Minimally test predictor instrumentation."""
        pred = self.instrumented_predict_class()
        Nside = Nside_oversize
        ll = get_linked_lengths((self.mkframe(0, Nside), self.mkframe(0.25, Nside),
                                 self.mkframe(0.75, Nside)),
                                pred.link_df_iter, 0.45)
        assert all(ll.values == 3)
        diags = pred.dump()
        assert len(diags) == 2

class NearestVelocityPredictTests(VelocityPredictTests, unittest.TestCase):
    def setUp(self):
        self.predict_class = predict.NearestVelocityPredict
        self.instrumented_predict_class = \
            predict.instrumented()(self.predict_class)
        self.mkframe = mkframe
    def test_initial_guess(self):
        """When an accurate initial velocity is given, velocities
        in the first pair of frames may be large."""
        pred = self.predict_class(
            initial_guess_positions=[(0., 0.)],
            initial_guess_vels=[(1., -1.)])
        ll = get_linked_lengths((self.mkframe(0), self.mkframe(1.),
                                 self.mkframe(2.)),
                                pred.link_df_iter, 0.45)
        assert all(ll.values == 3)

class DriftPredictTests(VelocityPredictTests, unittest.TestCase):
    def setUp(self):
        self.predict_class = predict.DriftPredict
        self.instrumented_predict_class = \
            predict.instrumented()(self.predict_class)
        self.mkframe = mkframe
    def test_initial_guess(self):
        """When an accurate initial velocity is given, velocities
        in the first pair of frames may be large."""
        pred = self.predict_class(initial_guess=(1., -1.))
        ll = get_linked_lengths((self.mkframe(0), self.mkframe(1.),
                                 self.mkframe(2.)),
                                pred.link_df_iter, 0.45)
        assert all(ll.values == 3)

class ChannelPredictXTests(VelocityPredictTests, unittest.TestCase):
    def setUp(self):
        self.predict_class = functools.partial(
            predict.ChannelPredict,  3, minsamples=3)
        self.instrumented_predict_class = functools.partial(
            predict.instrumented()(predict.ChannelPredict), 3, minsamples=3)
        self.mkframe = self._channel_frame
    def _channel_frame(self, n=1, Nside=3):
        xg, yg = np.mgrid[:Nside,:Nside]
        dx = (n - 1) * np.sqrt(2)
        dy = 0.
        return pandas.DataFrame(
                dict(x=xg.flatten() + dx, y=yg.flatten() + dy, frame=n))
    def test_initial_guess(self):
        """When an accurate initial velocity profile is given, velocities
        in the first pair of frames may be large."""
        def _shear_frame(t=1., Nside=4):
            xg, yg = np.mgrid[:Nside,:Nside]
            dx = 0.45 * t * yg
            return pandas.DataFrame(
                dict(x=(xg + dx).flatten(), y=yg.flatten(), frame=t))
        inity = np.arange(4)
        initprof = np.vstack((inity, inity*0.45)).T
        # We need a weird bin size (1.1) to avoid bin boundaries coinciding
        # with particle positions.
        pred = predict.ChannelPredict(1.1, minsamples=3,
                                      initial_profile_guess=initprof)
        print(_shear_frame(1.))
        ll = get_linked_lengths((_shear_frame(0), _shear_frame(1.),
                                 _shear_frame(2), _shear_frame(3)),
                        pred.link_df_iter, 0.45)
        assert all(ll.values == 4)

class ChannelPredictYTests(VelocityPredictTests, unittest.TestCase):
    def setUp(self):
        self.predict_class = functools.partial(
            predict.ChannelPredict, 3, 'y', minsamples=3)
        self.instrumented_predict_class = functools.partial(
            predict.instrumented()(predict.ChannelPredict),
            3, 'y', minsamples=3)
        self.mkframe = self._channel_frame
    def _channel_frame(self, n=1, Nside=3):
        xg, yg = np.mgrid[:Nside,:Nside]
        dx = 0.
        dy = (n - 1) * np.sqrt(2)
        return pandas.DataFrame(
            dict(x=xg.flatten() + dx, y=yg.flatten() + dy, frame=n))

if __name__ == '__main__':
    unittest.main()

max_stars_repo_name: sciunto/trackpy

max_issues_repo_name: sciunto/trackpy

max_forks_repo_name: sciunto/trackpy


text:
#!/usr/bin/python
# -*- coding: UTF-8 -*-

# TensorRT Version 7.1.3

import tensorrt as trt
import pycuda.driver as cuda
#import pycuda.driver as cuda2
import pycuda.autoinit
import numpy as np
import cv2
import scipy.special

tusimple_row_anchor = [ 64,  68,  72,  76,  80,  84,  88,  92,  96, 100, 104, 108, 112,
                        116, 120, 124, 128, 132, 136, 140, 144, 148, 152, 156, 160, 164,
                        168, 172, 176, 180, 184, 188, 192, 196, 200, 204, 208, 212, 216,
                        220, 224, 228, 232, 236, 240, 244, 248, 252, 256, 260, 264, 268,
                        272, 276, 280, 284
                        ]   # 56

cls_num_per_lane = 56

def load_engine(engine_path):
    #TRT_LOGGER = trt.Logger(trt.Logger.WARNING)  # INFO
    TRT_LOGGER = trt.Logger(trt.Logger.ERROR)
    with open(engine_path, 'rb') as f, trt.Runtime(TRT_LOGGER) as runtime:
        return runtime.deserialize_cuda_engine(f.read())

path ='./ldw_fp32.engine'

# 1. 建立模型，构建上下文管理器
engine = load_engine(path)
context = engine.create_execution_context()
context.active_optimization_profile = 0

#2. 读取数据，数据处理为可以和网络结构输入对应起来的的shape，数据可增加预处理
imgpath = './img_to_test/7.jpg'
vis = cv2.imread(imgpath)
img_w, img_h = 1280, 720
image = cv2.cvtColor(vis, cv2.COLOR_BGR2RGB)
image = cv2.resize(vis, (800, 288))         # => 800x288
image = image/255.0                         # => [0.0, 1.0]
image[:,:,0] = (image[:,:,0]-0.485) / 0.229
image[:,:,1] = (image[:,:,1]-0.456) / 0.224
image[:,:,2] = (image[:,:,2]-0.406) / 0.225
image = np.transpose(image, (2, 0, 1))      # HWC->CHW
# cv2.imwrite("./test1.jpg", image)
image = np.expand_dims(image, 0)            # Add batch dimension. NCHW
image = np.ascontiguousarray(image, dtype=np.float32)

#3.分配内存空间，并进行数据cpu到gpu的拷贝
#动态尺寸，每次都要set一下模型输入的shape，0代表的就是输入，输出根据具体的网络结构而定，可以是0,1,2,3...其中的某个头。
context.set_binding_shape(0, image.shape)
d_input = cuda.mem_alloc(image.nbytes)  #分配输入的内存。

output_shape = context.get_binding_shape(1)
buffer = np.empty(output_shape, dtype=np.float32)
d_output = cuda.mem_alloc(buffer.nbytes)    #分配输出内存。
cuda.memcpy_htod(d_input, image)
bindings = [d_input ,d_output]

#4.进行推理，并将结果从gpu拷贝到cpu。
context.execute_v2(bindings)  #可异步和同步
cuda.memcpy_dtoh(buffer,d_output)
output = buffer.reshape(output_shape)  # (num_grad + 1) * sample_rows * num_lane

#5.对推理结果进行后处理。这里只是举了一个简单例子，可以结合官方静态的yolov3案例完善。
col_sample = np.linspace(0, 800 - 1, 100)   # 横向格子个数
col_sample_w = col_sample[1] - col_sample[0] # 每个格子宽度

out_j = output[0][:, ::-1, :]                           #! reverse sample_rows dim
prob = scipy.special.softmax(out_j[:-1, :, :], axis=0)  # out_j[:-1, :, :] only process num_grad dim
idx = np.arange(100) + 1
idx = idx.reshape(-1, 1, 1)
loc = np.sum(prob * idx, axis=0)
out_j = np.argmax(out_j, axis=0)
loc[out_j == 100] = 0
out_j = loc                                             # (sample_rows, num_lane)

# import pdb; pdb.set_trace()
for i in range(out_j.shape[1]):                         # num_lanes
    if np.sum(out_j[:, i] != 0) > 2:
        for k in range(out_j.shape[0]):                 # sample_rows
            if out_j[k, i] > 0:
                ppp = (int(out_j[k, i] * col_sample_w * img_w / 800) - 1, int(img_h * (tusimple_row_anchor[cls_num_per_lane-1-k]/288)) - 1 )
                cv2.circle(vis, ppp, 5, (0,255,0), -1)
cv2.imwrite("./result.jpg", vis)

print("Done")

max_stars_repo_name: aimuch/AIEnvConfig

max_issues_repo_name: aimuch/AIEnvConfig

max_forks_repo_name: aimuch/AIEnvConfig

reason: import numpy,import scipy,import pycuda


text:
##
## Copyright (C) 2017, Amit Aides, all rights reserved.
##
## This file is part of Camera Network
## (see https://bitbucket.org/amitibo/cameranetwork_git).
##
## Redistribution and use in source and binary forms, with or without modification,
## are permitted provided that the following conditions are met:
##
## 1)  The software is provided under the terms of this license strictly for
##     academic, non-commercial, not-for-profit purposes.
## 2)  Redistributions of source code must retain the above copyright notice, this
##     list of conditions (license) and the following disclaimer.
## 3)  Redistributions in binary form must reproduce the above copyright notice,
##     this list of conditions (license) and the following disclaimer in the
##     documentation and/or other materials provided with the distribution.
## 4)  The name of the author may not be used to endorse or promote products derived
##     from this software without specific prior written permission.
## 5)  As this software depends on other libraries, the user must adhere to and keep
##     in place any licensing terms of those libraries.
## 6)  Any publications arising from the use of this software, including but not
##     limited to academic journal and conference publications, technical reports and
##     manuals, must cite the following works:
##     Dmitry Veikherman, Amit Aides, Yoav Y. Schechner and Aviad Levis, "Clouds in The Cloud" Proc. ACCV, pp. 659-674 (2014).
##
## THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
## EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
## INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
## BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
## LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
## OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
## ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.##
"""Utilites for handling export to solver.
"""
from __future__ import division
from CameraNetwork.utils import sun_direction
from CameraNetwork.image_utils import calcSunMaskRect
import cPickle
import cv2
from enaml.application import deferred_call, is_main_thread
import logging
import math
import os
import numpy as np
import pymap3d
import traceback

def exportToShdom(
    base_path,
    array_items,
    grid,
    lat,
    lon,
    alt,
    progress_callback):
    """Process export of reconstruction data on separate thread.
    The export is intended for use in SHDOM.

    Args:
        base_path (str): Path to store export data.
        array_items (list): List of array items.
        grid (list): List of grid array. This is the grid to reconstruct.
        lat, lon, alt (float): The latitude, longitude and altitude of the center
            of the grid.
        progress_callback (function): Callback function to update the (GUI) with
            the progress of the export.

    Note:
        The directions in the Qt view are as follows:
        x axis (horizontal) goes from West (left) to East (right)
        y axis (vertical) goes from South (down) to North (up).
        this makes it a EN axis system
    """

    #
    # Reset the progress indicatort.
    #
    progress_cnt = len(array_items)
    deferred_call(progress_callback, 0)

    #
    # Convert the grid from NED to ECEF
    #
    GRID = np.meshgrid(*grid)
    ecef_grid = pymap3d.ned2ecef(GRID[0], GRID[1], GRID[2], lat, lon, alt)

    #
    # Loop on all servers and collect information.
    #
    export_data = {}
    for i, (server_id, (array_model, array_view)) in enumerate(array_items.items()):
        try:
            #
            # Store extra data like camera center, etc.
            #
            extra_data, sun_alt, sun_az = extraReconstructionData(
                array_model, array_view, lat0=lat, lon0=lon, h0=alt)

            img_array = array_model.img_array

            #
            # Calculate azimuth and elevation of each pixel.
            # Note:
            # The interpolation is done in the Y_shdom, X_shdom to avoid
            # the seam artifact of PHI at 180 degrees.
            #
            Y_shdom, X_shdom = np.meshgrid(
                np.linspace(-1, 1, array_model.img_array.shape[1]),
                np.linspace(-1, 1, array_model.img_array.shape[0])
            )
            Y_shdom = array_view.image_widget.getArrayRegion(Y_shdom)
            X_shdom = array_view.image_widget.getArrayRegion(X_shdom)
            PHI_shdom, PSI_shdom = getShdomDirections(Y_shdom, X_shdom, array_model.fov)

            #
            # Calculate Masks.
            # Note:
            # sunshader mask is calculate using grabcut. This is used for removing the
            # sunshader.
            # Manual mask is the (ROI) mask marked by the user.
            # sun mask is a mask the blocks the sun.
            #
            manual_mask = array_view.image_widget.mask
            joint_mask = (manual_mask * array_model.sunshader_mask).astype(np.uint8)

            # calculate rectangle around sun mask
            rect_sun_mask = calcSunMaskRect (
                array_model.img_array.shape ,
                sun_alt ,
                sun_az ,
                radius = array_model.sun_mask_radius
            )

            #
            # Project the grid on the image and check viewed voxels.
            # Note:
            # This measurement is used for checking how many cameras see each voxel.
            # TODO:
            # This procedure is time expensive and can be cached.
            # This should probably be a method of the camera, and this method should
            # cache the result, or even be triggered by setting the grid.
            #
            visibility = projectGridOnCamera(ecef_grid, array_model, joint_mask)
        except Exception, e:
            logging.error(
                "Server {} ignored due to exception:\n{}".format(
                    server_id,
                    traceback.format_exc()
                )
            )
            continue

        export_data[server_id] = dict(
            extra_data=extra_data,
            R=array_view.image_widget.getArrayRegion(img_array[..., 0]),
            G=array_view.image_widget.getArrayRegion(img_array[..., 1]),
            B=array_view.image_widget.getArrayRegion(img_array[..., 2]),
            PHI=PHI_shdom,
            PSI=PSI_shdom,
            MASK = array_view.image_widget.getArrayRegion(joint_mask),
            SUN_MASK =  array_view.image_widget.getArrayRegion(array_model.sun_mask),
            Visibility = visibility,
            manual_mask = array_view.image_widget.getArrayRegion(manual_mask),
            cloud_mask = array_view.image_widget.getArrayRegion(array_model.cloud_weights),
            sunshader_mask = array_view.image_widget.getArrayRegion(array_model.sunshader_mask),
            rect_sun_mask = array_view.image_widget.getArrayRegion(rect_sun_mask)
            # TODO: check why getArrayRegion() fails when sending returnMappedCoords=True
        )

        deferred_call(progress_callback, i / progress_cnt)

    #
    # Save the results.
    #
    with open(os.path.join(base_path, 'export_data.pkl'), 'wb') as f:
        cPickle.dump(export_data, f)

    deferred_call(progress_callback, 0)

def getShdomDirections(Y_shdom, X_shdom, fov=math.pi/2):
    """Calculate the (SHDOM) direction of each pixel.

    Directions are calculated in SHDOM convention where the direction is
    of the photons.
    """

    PHI_shdom = np.pi + np.arctan2(Y_shdom, X_shdom)
    PSI_shdom = -np.pi + fov * np.sqrt(X_shdom**2 + Y_shdom**2)
    return PHI_shdom, PSI_shdom

def extraReconstructionData(array_model, array_view, lat0, lon0, h0):
    """Get extra data for the reconstruction

    This includes camera position, sun angle, time etc.

    Note:
        The coordinates are given in the following conventions:
        1) Camera position is given in NEU.
        2) sun_mu, sun_az are given in the SHDOM convention
           of photons directions.
    """

    #
    # Calculate the center of the camera.
    # Note that the coords are stored as NEU (in contrast to NED)
    #
    n, e, d = pymap3d.geodetic2ned(
        array_model.latitude, array_model.longitude, array_model.altitude,
        lat0=lat0, lon0=lon0, h0=h0)

    #
    # Calculate bounding coords (useful for debug visualization)
    #
    #bounding_phi, bounding_psi = calcROIbounds(array_model, array_view)

    #
    # Sun azimuth and altitude
    #
    sun_alt, sun_az = sun_direction(
        latitude=str(array_model.latitude),
        longitude=str(array_model.longitude),
        altitude=array_model.altitude,
        at_time=array_model.img_data.name_time)

    #
    # Note:
    # shdom_mu = cos(pi/2-alt-pi)=cos(-alt-pi/2)=cos(alt+pi/2)
    #
    extra_data = \
        dict(
            at_time=array_model.img_data.name_time,
            sun_mu=math.cos(float(sun_alt)+np.pi/2),
            sun_az=float(sun_az)-np.pi,
            x=n,
            y=e,
            z=-d,
            #bounding_phi=bounding_phi,
            #bounding_psi=bounding_psi
        )
    return extra_data, sun_alt, sun_az

def projectGridOnCamera(ecef_grid, array_model, joint_mask):
    """Project reconstruction grid on camera.

    This is used to estimate the visibility of each voxel by the camera.
    """

    xs, ys, fov_mask = array_model.projectECEF(ecef_grid, filter_fov=False)
    xs = xs.astype(np.uint32).flatten()
    ys = ys.astype(np.uint32).flatten()

    grid_visibility = np.zeros_like(xs, dtype=np.uint8)
    grid_visibility[fov_mask] = \
        joint_mask[ys[fov_mask], xs[fov_mask]].astype(np.uint8)

    return grid_visibility.reshape(*ecef_grid[0].shape)

def calcROIbounds(array_model, array_view):
    """Calculate bounds of ROI in array_view

    Useful for debug visualization.
    """

    #
    # Get the ROI size
    #
    size = array_model.ROI_state['size']

    #
    # Get the transform from the ROI to the data.
    #

    #~, tr = roi.getArraySlice(array_model.img_array, array_view.image_widget.img_item)
    #
    # Calculate the bounds.
    #
    center = float(array_model.img_array.shape[0])/2
    pts = np.array(
        [tr.map(x, y) for x, y in \
         ((0, 0), (size.x(), 0), (0, size.y()), (size.x(), size.y()))]
    )
    pts = (pts - center) / center
    X, Y = pts[:, 1], pts[:, 0]
    bounding_phi = np.arctan2(X, Y)
    bounding_psi = array_model.fov * np.sqrt(X**2 + Y**2)

    return bounding_phi, bounding_psi

max_stars_repo_name: Addalin/cameranetwork

max_issues_repo_name: Addalin/cameranetwork

max_forks_repo_name: Addalin/cameranetwork


text:
"""Tests for Convergence Plots."""
import pytest
from tardis.visualization.tools.convergence_plot import (
    ConvergencePlots,
    transition_colors,
)
from collections import defaultdict
import plotly.graph_objects as go
from astropy import units as u

@pytest.fixture(scope="module", params=[0, 1, 2])
def convergence_plots(request):
    """Initialize ConvergencePlots class and build empty plots."""
    cplots = ConvergencePlots(iterations=request.param)
    cplots.build(display_plot=False)
    return cplots

@pytest.fixture()
def fetch_luminosity_data(convergence_plots):
    """Prepare data for t_inner and luminosity plot."""
    for item in [2] * convergence_plots.iterations:
        convergence_plots.fetch_data(
            name="t_inner", value=item, item_type="value"
        )
        for item2 in convergence_plots.luminosities:
            convergence_plots.fetch_data(
                name=item2, value=item, item_type="value"
            )

def test_transition_colors():
    """Test whether the object returned by the transition_colors function is a list of appropriate length."""
    iterations = 3
    colors = transition_colors(length=iterations)
    assert type(colors) == list
    assert len(colors) == iterations

def test_convergence_construction(convergence_plots):
    """Test the construction of the ConvergencePlots class."""
    assert convergence_plots.iterable_data == {}
    assert convergence_plots.value_data == defaultdict(list)
    assert convergence_plots.luminosities == [
        "Emitted",
        "Absorbed",
        "Requested",
    ]

def test_fetch_data(convergence_plots):
    """Test values of variables updated by fetch_data function."""
    convergence_plots.fetch_data(
        name="iterable", value=range(3), item_type="iterable"
    )
    convergence_plots.fetch_data(name="value", value=0, item_type="value")

    assert convergence_plots.iterable_data["iterable"] == range(3)
    assert convergence_plots.value_data["value"] == [0]

def test_build(convergence_plots):
    """Test if convergence plots are instances of plotly.graph_objs.FigureWidget() and have appropriate number of traces."""
    assert type(convergence_plots.plasma_plot) == go.FigureWidget
    assert type(convergence_plots.t_inner_luminosities_plot) == go.FigureWidget

    # check number of traces
    assert len(convergence_plots.t_inner_luminosities_plot.data) == 5
    assert len(convergence_plots.plasma_plot.data) == 2

@pytest.mark.usefixtures("fetch_luminosity_data")
def test_update_t_inner_luminosities_plot(convergence_plots):
    """Test the number of traces and length of x and y values."""
    n_iterations = convergence_plots.iterations
    convergence_plots.update_t_inner_luminosities_plot()

    # check number of traces
    assert len(convergence_plots.t_inner_luminosities_plot.data) == 5

    for index in range(0, 5):
        # check x and y values for all traces
        assert (
            len(convergence_plots.t_inner_luminosities_plot.data[index].x)
            == n_iterations
        )
        assert (
            len(convergence_plots.t_inner_luminosities_plot.data[index].y)
            == n_iterations
        )

    # check range of x axes
    for axis in ["xaxis", "xaxis2", "xaxis3"]:
        convergence_plots.t_inner_luminosities_plot["layout"][axis]["range"] = [
            0,
            convergence_plots.iterations + 1,
        ]

@pytest.mark.usefixtures("fetch_luminosity_data")
def test_update_plasma_plots(convergence_plots):
    """Test the state of plasma plots after updating."""
    n_iterations = convergence_plots.iterations
    expected_n_traces = 2 * n_iterations + 2
    velocity = range(0, n_iterations) * u.m / u.s

    convergence_plots.fetch_data(
        name="velocity", value=velocity, item_type="iterable"
    )

    w_val = list(range(n_iterations))
    t_rad_val = [item * 2 for item in w_val]

    for _ in range(n_iterations):
        convergence_plots.fetch_data(
            name="t_rad",
            value=t_rad_val,
            item_type="iterable",
        )
        convergence_plots.fetch_data(
            name="w",
            value=w_val,
            item_type="iterable",
        )
        convergence_plots.update_plasma_plots()
        convergence_plots.current_iteration += 1

    # check number of traces
    assert len(convergence_plots.plasma_plot.data) == expected_n_traces

    # traces are added alternatively
    # trace 0 and 1 and empty
    assert convergence_plots.plasma_plot.data[0].x == None
    assert convergence_plots.plasma_plot.data[1].x == None

    # check other traces
    for index in list(range(expected_n_traces))[::2][1:]:
        # check values for t_rad subplot
        assert convergence_plots.plasma_plot.data[index].xaxis == "x"
        assert convergence_plots.plasma_plot.data[index].yaxis == "y"
        assert convergence_plots.plasma_plot.data[index].y == tuple(t_rad_val)
        assert convergence_plots.plasma_plot.data[index].x == tuple(
            velocity.to(u.km / u.s).value
        )

    for index in list(range(expected_n_traces))[1::2][1:]:
        # check values for w subplot
        assert convergence_plots.plasma_plot.data[index].xaxis == "x2"
        assert convergence_plots.plasma_plot.data[index].yaxis == "y2"
        assert convergence_plots.plasma_plot.data[index].y == tuple(w_val)
        assert convergence_plots.plasma_plot.data[index].x == tuple(
            velocity.to(u.km / u.s).value
        )

@pytest.mark.usefixtures("fetch_luminosity_data")
def test_override_plot_parameters(convergence_plots):
    """Test if default plot properties are overridden properly."""
    parameters = {
        "data": {
            "line": {"dash": "dot"},
            "mode": "lines+markers",
        },
        "layout": {"xaxis2": {"showgrid": False}},
    }
    convergence_plots.override_plot_parameters(
        convergence_plots.plasma_plot, parameters=parameters
    )
    convergence_plots.override_plot_parameters(
        convergence_plots.t_inner_luminosities_plot, parameters=parameters
    )

    # data properties will be applied across all traces equally
    # testing plot parameters of t_inner and luminosity plot
    for trace_index in range(5):
        assert (
            convergence_plots.t_inner_luminosities_plot.data[
                trace_index
            ].line.dash
            == "dot"
        )
        assert (
            convergence_plots.t_inner_luminosities_plot.data[trace_index].mode
            == "lines+markers"
        )
    # checking layout
    assert (
        convergence_plots.t_inner_luminosities_plot["layout"]["xaxis2"][
            "showgrid"
        ]
        == False
    )

    # testing plot parameters for plasma plot
    for trace_index in range(2):
        assert (
            convergence_plots.plasma_plot.data[trace_index].line.dash == "dot"
        )
        assert (
            convergence_plots.plasma_plot.data[trace_index].mode
            == "lines+markers"
        )
    # checking layout for plasma plot
    assert (
        convergence_plots.plasma_plot["layout"]["xaxis2"]["showgrid"] == False
    )

max_stars_repo_name: jaladh-singhal/tardis

max_issues_repo_name: jaladh-singhal/tardis

max_forks_repo_name: jaladh-singhal/tardis

reason: from astropy