old_contents: # -*- coding: utf8 -*-
  
  import argparse
  from scipy import signal
  import numpy as np
  from os import path
  import json
  
  def discount_rewards(x, gamma):
      """
      Given vector x, computes a vector y such that
      y[i] = x[i] + gamma * x[i+1] + gamma^2 x[i+2] + ...
      """
      return signal.lfilter([1], [1, -gamma], x[::-1], axis=0)[::-1]
  
  # Source: http://stackoverflow.com/a/12201744/1735784
  def rgb2gray(rgb):
      """
      Convert an RGB image to a grayscale image.
      Uses the formula Y' = 0.299*R + 0.587*G + 0.114*B
      """
      return np.dot(rgb[..., :3], [0.299, 0.587, 0.114])
  
  def preprocess_image(img):
      """
      Preprocess an image by converting it to grayscale and dividing its values by 256
      """
      img = img[35:195]  # crop
      img = img[::2, ::2]  # downsample by factor of 2
      return (rgb2gray(img) / 256.0)[:, :, None]
  
  def save_config(directory, config, envs):
      """Save the configuration of an agent to a file."""
      config["envs"] = envs
      with open(path.join(directory, "config.json"), "w") as outfile:
          json.dump(config, outfile)
  
  def json_to_dict(filename):
      """Load a json file as a dictionary."""
      with open(filename) as f:
          return json.load(f)
  
  def ge(minimum):
      """Require the value for an argparse argument to be an integer >= minimum."""
      def f(value):
          ivalue = int(value)
          if ivalue < minimum:
              raise argparse.ArgumentTypeError("{} must be an integer of at least 1.".format(value))
          return ivalue
      return f
  
new_contents: # -*- coding: utf8 -*-
  
  import argparse
  from scipy import signal
  import numpy as np
  from os import path
  import json
  
  def discount_rewards(x, gamma):
      """
      Given vector x, computes a vector y such that
      y[i] = x[i] + gamma * x[i+1] + gamma^2 x[i+2] + ...
      """
      return signal.lfilter([1], [1, -gamma], x[::-1], axis=0)[::-1]
  
  # Source: http://stackoverflow.com/a/12201744/1735784
  def rgb2gray(rgb):
      """
      Convert an RGB image to a grayscale image.
      Uses the formula Y' = 0.299*R + 0.587*G + 0.114*B
      """
      return np.dot(rgb[..., :3], [0.299, 0.587, 0.114])
  
  def preprocess_image(img):
      """
      Preprocess an image by converting it to grayscale and dividing its values by 256
      """
      img = img[35:195]  # crop
      img = img[::2, ::2]  # downsample by factor of 2
      return (rgb2gray(img) / 256.0)[:, :, None]
  
  def save_config(directory, config, envs):
      """Save the configuration of an agent to a file."""
      config["envs"] = envs
      # Save git information if possible
      try:
          import pygit2
          repo = pygit2.Repository(path.abspath(path.join(path.dirname(path.realpath(__file__)), "..")))
          git = {
              "branch": repo.head.name,
              "commit": str(repo.head.target)
          }
          config["git"] = git
      except:
          pass
      with open(path.join(directory, "config.json"), "w") as outfile:
          json.dump(config, outfile)
  
  def json_to_dict(filename):
      """Load a json file as a dictionary."""
      with open(filename) as f:
          return json.load(f)
  
  def ge(minimum):
      """Require the value for an argparse argument to be an integer >= minimum."""
      def f(value):
          ivalue = int(value)
          if ivalue < minimum:
              raise argparse.ArgumentTypeError("{} must be an integer of at least 1.".format(value))
          return ivalue
      return f
  
subject: Save git information to config file if possible