text:
__author__ = 'martin'

def getTraj_simple(start_planet, arrive_planet, tlaunch, tarrive, N):
    '''
	Finds a trajectory between two objects orbiting the Sun

	USAGE: traj = getTraj(K1, K2, tlaunch, tarrive)
		K:  		array of object parameters.
		epoch:		epoch of Keplerian orbital elements (JD)
		a:  	 	semimajor axis (AU)
		e:  	 	eccentricity (none)
		i:  	 	inclination (deg)
		om: 	 	longitude of the ascending node (deg)
		w:  	 	argument of perihelion (deg)
		ma: 	 	mean anomaly at epoch (deg)
		mass: 	 	mass of object (kg)
		r: 	 	radius of object (m)
		sr:	 	safe radius to approach object (m)
		K1: 	 	[epoch1,a1,e1,i1,om1,w1,ma1,mass1,r1,sr1]
		K2: 	 	[epoch2,a2,e2,i2,om2,w2,ma2,mass2,r2,sr2]
		tlaunch: 	launch time (JD)
		tarrive: 	arrival time (JD)
		N:		number of points in calculated trajectory

	'''
    import numpy as np
    from PyKEP import epoch, DAY2SEC, SEC2DAY, AU, DEG2RAD, MU_SUN, planet, lambert_problem, propagate_lagrangian, fb_vel

    # Create PyKEP epoch objects and calculate flight time
    t1 = epoch(tlaunch)
    t2 = epoch(tarrive)
    dt = (tarrive - tlaunch) * DAY2SEC

    rev=0 #number of revolutions before intercept

    OBJ1 = planet.jpl_lp(start_planet)
    OBJ2 = planet.jpl_lp(arrive_planet)  # Calculate location of objects in flight path
    r1, v1 = OBJ1.eph(t1)
    r2, v2 = OBJ2.eph(t2)

    #Find trajectory
    l = lambert_problem(r1, r2, dt, MU_SUN)

    #extract relevant information from solution
    r = l.get_r1()
    v = l.get_v1()[0]
    mu = l.get_mu()

    #define the integration time
    dtn = dt / (N - 1)
    dtn_days = dtn * SEC2DAY

    #alocate the cartesian components for r
    t = np.array([0.0] * N)
    x = np.array([0.0] * N)
    y = np.array([0.0] * N)
    z = np.array([0.0] * N)

    #calculate the spacecraft position at each dt
    for i in range(N):
        t[i] = tlaunch + dtn_days * i
        x[i] = r[0] / AU
        y[i] = r[1] / AU
        z[i] = r[2] / AU
        r, v = propagate_lagrangian(r, v, dtn, mu)

    #traj = [t, x, y, z]
    vin=l.get_v1()[rev]
    vout=l.get_v2()[rev]

    dV=fb_vel(vin,vout,planet.jpl_lp(arrive_planet))
    #dV=np.sqrt( np.square(vout[0])+np.square(vout[1])+np.square(vout[2]))-np.sqrt( np.square(vin[0])+np.square(vin[1])+np.square(vin[2]))

    return dV

max_stars_repo_name: rSimulate/Cosmosium

max_issues_repo_name: rSimulate/Cosmosium

max_forks_repo_name: rSimulate/Cosmosium


text:
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2019.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

# Converts vehicle routing instnces into a list of Paulis,
# and provides some related routines (extracting a solution,
# checking its objective function value).

import numpy as np
from qiskit.quantum_info import Pauli

from qiskit.aqua.operators import WeightedPauliOperator

def get_vehiclerouting_matrices(instance, n, K):
    """Constructs auxiliary matrices from a vehicle routing instance,
        which represent the encoding into a binary quadratic program.
        This is used in the construction of the qubit ops and computation
        of the solution cost.

    Args:
        instance (numpy.ndarray) : a customers-to-customers distance matrix.
        n (integer) : the number of customers.
        K (integer) : the number of vehicles available.

    Returns:
        Q (numpy.ndarray) : a matrix defining the interactions between variables.
        g (numpy.ndarray) : a matrix defining the contribution from the individual variables.
        c (float) : the constant offset.
        """

    # N = (n - 1) * n
    A = np.max(instance) * 100  # A parameter of cost function

    # Determine the weights w
    instance_vec = instance.reshape(n**2)
    w_list = [instance_vec[x] for x in range(n**2) if instance_vec[x] > 0]
    w = np.zeros(n * (n - 1))
    for ii in range(len(w_list)):
        w[ii] = w_list[ii]

    # Some additional variables
    Id_n = np.eye(n)
    Im_n_1 = np.ones([n - 1, n - 1])
    Iv_n_1 = np.ones(n)
    Iv_n_1[0] = 0
    Iv_n = np.ones(n - 1)
    neg_Iv_n_1 = np.ones(n) - Iv_n_1

    v = np.zeros([n, n * (n - 1)])
    for ii in range(n):
        count = ii - 1
        for jj in range(n * (n - 1)):

            if jj // (n - 1) == ii:
                count = ii

            if jj // (n - 1) != ii and jj % (n - 1) == count:
                v[ii][jj] = 1.

    vn = np.sum(v[1:], axis=0)

    # Q defines the interactions between variables
    Q = A * (np.kron(Id_n, Im_n_1) + np.dot(v.T, v))

    # g defines the contribution from the individual variables
    g = w - 2 * A * (np.kron(Iv_n_1, Iv_n) + vn.T) - \
        2 * A * K * (np.kron(neg_Iv_n_1, Iv_n) + v[0].T)

    # c is the constant offset
    c = 2 * A * (n - 1) + 2 * A * (K**2)

    return (Q, g, c)

def get_vehiclerouting_cost(instance, n, K, x_sol):
    """Computes the cost of a solution to an instnance of a vehicle routing problem.

    Args:
        instance (numpy.ndarray) : a customers-to-customers distance matrix.
        n (integer) : the number of customers.
        K (integer) : the number of vehicles available.
        x_sol (numpy.ndarray): a solution, i.e., a path, in its binary representation.

    Returns:
        cost (float): objective function value.
        """
    (Q, g, c) = get_vehiclerouting_matrices(instance, n, K)
    def fun(x): return np.dot(np.around(x), np.dot(Q, np.around(x))) + np.dot(g, np.around(x)) + c
    cost = fun(x_sol)
    return cost

def get_vehiclerouting_qubitops(instance, n, K):
    """Converts an instance of a vehicle routing problem into a list of Paulis.

    Args:
        instance (numpy.ndarray) : a customers-to-customers distance matrix.
        n (integer) : the number of customers.
        K (integer) : the number of vehicles available.

    Returns:
        WeightedPauliOperator: operator for the Hamiltonian.
    """

    N = (n - 1) * n
    (Q, g, c) = get_vehiclerouting_matrices(instance, n, K)

    # Defining the new matrices in the Z-basis
    Iv = np.ones(N)
    Qz = (Q / 4)
    gz = (-g / 2 - np.dot(Iv, Q / 4) - np.dot(Q / 4, Iv))
    cz = (c + np.dot(g / 2, Iv) + np.dot(Iv, np.dot(Q / 4, Iv)))

    cz = cz + np.trace(Qz)
    Qz = Qz - np.diag(np.diag(Qz))

    # Getting the Hamiltonian in the form of a list of Pauli terms

    pauli_list = []
    for i in range(N):
        if gz[i] != 0:
            wp = np.zeros(N)
            vp = np.zeros(N)
            vp[i] = 1
            pauli_list.append((gz[i], Pauli(vp, wp)))
    for i in range(N):
        for j in range(i):
            if Qz[i, j] != 0:
                wp = np.zeros(N)
                vp = np.zeros(N)
                vp[i] = 1
                vp[j] = 1
                pauli_list.append((2 * Qz[i, j], Pauli(vp, wp)))

    pauli_list.append((cz, Pauli(np.zeros(N), np.zeros(N))))
    return WeightedPauliOperator(paulis=pauli_list)

def get_vehiclerouting_solution(instance, n, K, result):
    """Tries to obtain a feasible solution (in vector form) of an instance
        of vehicle routing from the results dictionary.

    Args:
        instance (numpy.ndarray) : a customers-to-customers distance matrix.
        n (integer) : the number of customers.
        K (integer) : the number of vehicles available.
        result (dictionary) : a dictionary obtained by QAOA.run or VQE.run containing key 'eigvecs'.

    Returns:
        x_sol (numpy.ndarray): a solution, i.e., a path, in its binary representation.
        """

    v = result['eigvecs'][0]
    N = (n - 1) * n

    index_value = [x for x in range(len(v)) if v[x] == max(v)][0]
    string_value = "{0:b}".format(index_value)

    while len(string_value) < N:
        string_value = '0' + string_value

    x_sol = list()
    for elements in string_value:
        if elements == '0':
            x_sol.append(0)
        else:
            x_sol.append(1)

    x_sol = np.flip(x_sol, axis=0)

    return x_sol

ext: py

reason: import numpy


text:
from pyrep.backend import vrep, utils
from pyrep.objects.dummy import Dummy
from pyrep.objects.shape import Shape
from pyrep.objects.joint import Joint
from pyrep.objects.proximity_sensor import ProximitySensor
from pyrep.robots.robot_component import RobotComponent
from pyrep.const import ConfigurationPathAlgorithms as Algos
from pyrep.errors import ConfigurationPathError
from pyrep.const import PYREP_SCRIPT_TYPE
from pyrep.misc.laser import Laser
from typing import List
import numpy as np
from math import pi, sqrt
from math import sqrt, atan2, sin, cos

class PioneerP3dx(RobotComponent):
	"""Base class representing a robot mobile base with path planning support.
	"""

	def __init__(self, count: int, num_wheels: int, num_sensors: int, name: str, sensor_name: str, laser_name: str):
		"""Count is used for when we have multiple copies of mobile bases.

		:param count: used for multiple copies of robots
		:param num_wheels: number of actuated wheels
		:param num_laser: number of lasers
		:param name: string with robot name (same as base in vrep model).

		"""

		joint_names = ['%s_m_joint%s' % (name, str(i + 1)) for i in
					   range(num_wheels)]

		super().__init__(count, name, joint_names)

		self.num_wheels = num_wheels
		suffix = '' if count == 0 else '#%d' % (count - 1)

		wheel_names = ['%s_wheel%s%s' % (name, str(i + 1), suffix) for i in
					   range(self.num_wheels)]
		self.wheels = [Shape(name) for name in wheel_names]

		#joint_slipping_names = [
		#    '%s_slipping_m_joint%s%s' % (name, str(i + 1), suffix) for i in
		#    range(self.num_wheels)]
		#self.joints_slipping = [Joint(jsname)
		#                        for jsname in joint_slipping_names]
		self.num_sensors = num_sensors
		sensor_names = ['%s_%s%s' % (name, sensor_name, str(i + 1)) for i in range(self.num_sensors)]
		self.sensors = [ProximitySensor(name) for name in sensor_names]

		self.lasers = Laser(laser_name,'')

		# Motion planning handles
		self.intermediate_target_base = Dummy(
			'%s_intermediate_target_base%s' % (name, suffix))

		self.target_base = Dummy('%s_target_base%s' % (name, suffix))

		self._collision_collection = vrep.simGetCollectionHandle(
			'Robot')
		self.target_z = self.target_base.get_position()[-1]
		# Robot parameters and handle
		self.z_pos = self.get_position()[2]
		#self.target_z = self.target_base.get_position()[-1]
		self.wheel_radius = self.wheels[0].get_bounding_box()[1]
		self.wheel_distance = np.linalg.norm(
			np.array(self.wheels[0].get_position()) -
			np.array(self.wheels[1].get_position()))

		# Make sure dummies are orphan if loaded with ttm
		self.intermediate_target_base.set_parent(None)
		self.target_base.set_parent(None)

		self.cummulative_error = 0
		self.prev_error = 0

		# PID controller values.
		# TODO: expose to user through constructor.
		self.Kp = 0.1
		self.Ki = 0.0
		self.Kd = 0.0
		self.desired_velocity = 0.1

		self._path_done = False
		self.i_path = -1
		self.inter_done = True

		self.drawing_handle = vrep.simAddDrawingObject(
			objectType=vrep.sim_drawing_points, size=10, duplicateTolerance=0,
			parentObjectHandle=-1, maxItemCount=99999,
			ambient_diffuse=[1, 0, 0])

	#def get_wheel_radius(self):
	#    min_x = vrep.simGetObjectFloatParameter(self.wheels[0]._handle, 15)
	#    max_x = vrep.simGetObjectFloatParameter(self.wheels[0]._handle, 18)
	#    return (max_x - min_x)/2.

	def get_velocity(self) -> List[float]:
		#print(self.wheel_radius)
		joint_velocities = self.get_joint_velocities()

		v_l = joint_velocities[0]
		v_r = joint_velocities[1]
		#print('get_velocity')
		#print(v_l)
		#print(v_r)
		v = self.wheel_radius/2. * (v_l + v_r)
		omega = self.wheel_radius/self.wheel_distance * (v_r - v_l)
		return [v, omega]

	def set_base_angular_velocites(self, velocity: List[float]):
		v = velocity[0]
		omega = velocity[1]

		vr = ((2. * v + omega * self.wheel_distance) /
			  (2. * self.wheel_radius))

		vl = ((2. * v - omega * self.wheel_distance) /
			  (2. * self.wheel_radius))

		self.set_joint_target_velocities([vl, vr])

	def get_sensor_data(self):
		data = []
		for i in range(self.num_sensors):
			ret, detected_point = self.sensors[i].get_sensor_data()
			dist = np.linalg.norm(detected_point)
			data.append(ret)
			data.append(dist)
		return data

	def get_2d_pose(self) -> List[float]:
		"""Gets the 2D (top-down) pose of the robot [x, y, yaw].

		:return: A List containing the x, y, yaw (in radians).
		"""
		return (self.get_position()[:2] +
				self.get_orientation()[-1:])

	def set_2d_pose(self, pose: List[float]) -> None:
		"""Sets the 2D (top-down) pose of the robot [x, y, yaw]

		:param pose: A List containing the x, y, yaw (in radians).
		"""
		x, y, yaw = pose
		self.set_position([x, y, self.z_pos])
		self.set_orientation([0, 0, yaw])

	def assess_collision(self):
		"""Silent detection of the robot base with all other entities present in the scene.

		:return: True if collision is detected
		"""
		return vrep.simCheckCollision(self._collision_collection,
									  vrep.sim_handle_all) == 1

	def get_nonlinear_path_points(self, position: List[float],
						   angle=0,
						   boundaries=8,
						   path_pts=600,
						   ignore_collisions=False,
						   algorithm=Algos.RRTConnect) -> List[List[float]]:
		"""Gets a non-linear (planned) configuration path given a target pose.

		:param position: The x, y, z position of the target.
		:param angle: The z orientation of the target (in radians).
		:param boundaries: A float defining the path search in x and y direction
		[[-boundaries,boundaries],[-boundaries,boundaries]].
		:param path_pts: number of sampled points returned from the computed path
		:param ignore_collisions: If collision checking should be disabled.
		:param algorithm: Algorithm used to compute path
		:raises: ConfigurationPathError if no path could be created.

		:return: A non-linear path (x,y,angle) in the xy configuration space.
		"""

		# Base dummy required to be parent of the robot tree
		# self.base_ref.set_parent(None)
		# self.set_parent(self.base_ref)

		# Missing the dist1 for intermediate target

		self.target_base.set_position([position[0], position[1], self.target_z])
		self.target_base.set_orientation([0, 0, angle])

		handle_base = self.get_handle()
		handle_target_base = self.target_base.get_handle()

		# Despite verbosity being set to 0, OMPL spits out a lot of text
		with utils.suppress_std_out_and_err():
			_, ret_floats, _, _ = utils.script_call(
				'getNonlinearPathMobile@PyRep', PYREP_SCRIPT_TYPE,
				ints=[handle_base, handle_target_base,
					  self._collision_collection,
					  int(ignore_collisions), path_pts], floats=[boundaries],
					  strings=[algorithm.value])

		# self.set_parent(None)
		# self.base_ref.set_parent(self)

		if len(ret_floats) == 0:
			raise ConfigurationPathError('Could not create path.')

		path = []
		for i in range(0, len(ret_floats) // 3):
			inst = ret_floats[3 * i:3 * i + 3]
			if i > 0:
				dist_change = sqrt((inst[0] - prev_inst[0]) ** 2 + (
				inst[1] - prev_inst[1]) ** 2)
			else:
				dist_change = 0
			inst.append(dist_change)

			path.append(inst)

			prev_inst = inst

		self._path_points = path
		self._set_inter_target(0)
		return path

	def visualize(self, path_points) -> None:
		"""Draws a visualization of the path in the scene.

		The visualization can be removed
		with :py:meth:`ConfigurationPath.clear_visualization`.
		"""
		if len(self._path_points) <= 0:
			raise RuntimeError("Can't visualise a path with no points.")

		self._drawing_handle = vrep.simAddDrawingObject(
			objectType=vrep.sim_drawing_lines, size=3, duplicateTolerance=0,
			parentObjectHandle=-1, maxItemCount=99999,
			ambient_diffuse=[1, 0, 1])
		vrep.simAddDrawingObjectItem(self._drawing_handle, None)
		init_pose = self.get_2d_pose()
		self.set_2d_pose(self._path_points[0][:3])
		prev_point = self.get_position()

		for i in range(len(self._path_points)):
			points = self._path_points[i]
			self.set_2d_pose(points[:3])
			p = self.get_position()
			vrep.simAddDrawingObjectItem(self._drawing_handle, prev_point + p)
			prev_point = p

		# Set the arm back to the initial config
		self.set_2d_pose(init_pose[:3])

	def get_base_actuation(self):
		"""A controller using PID.

		:return: A list with left and right joint velocity, and bool if target is reached.
		"""

		d_x, d_y, _ = self.intermediate_target_base.get_position(
			relative_to=self)
		#print('dx and dy')
		print(d_x)
		print(d_y)
		d_x_final, d_y_final, _ = self.target_base.get_position(
			relative_to=self)

		if sqrt((d_x_final) ** 2 + (d_y_final) ** 2) < 1.0:
			return [0., 0.]

		alpha = atan2(d_y, d_x)
		e = atan2(sin(alpha), cos(alpha))
		e_P = e
		e_I = self.cummulative_error + e
		e_D = e - self.prev_error
		w = self.Kp * e_P + self.Ki * e_I + self.Kd * e_D
		w = atan2(sin(w), cos(w))

		self.cummulative_error = self.cummulative_error + e
		self.prev_error = e

		return [0.1, 0]

	def step(self) -> bool:
		#print(self._path_points[0][0], self._path_points[0][1])
		pos_inter = self.intermediate_target_base.get_position(
			relative_to=None)
		vrep.simAddDrawingObjectItem(self.drawing_handle, pos_inter[:2] + [0.0])
		pos_inter = self.intermediate_target_base.get_position(
			relative_to=self)
		#print(pos_inter)

		if self.inter_done:
			self._next_i_path()
			self._set_inter_target(self.i_path)
			self.inter_done = False

		if sqrt((pos_inter[0]) ** 2 + (pos_inter[1]) ** 2) < 0.1:
			self.inter_done = True
			#actuation, _ = self.get_base_actuation()
		#else:
			#actuation, _ = self.get_base_actuation()

			#self._mobile.set_joint_target_velocities(actuation)

		#if self.i_path == len(self._path_points) - 1:
		#	self._path_done = True

		#actuation, self._path_done = self._mobile.get_base_actuation()
		#self._mobile.set_joint_target_velocities(actuation)

		return self.get_base_actuation(), False

	def _set_inter_target(self, i):
		self.intermediate_target_base.set_position(
			[self._path_points[i][0], self._path_points[i][1],
			 self.target_z])
		self.intermediate_target_base.set_orientation(
			[0, 0, self._path_points[i][2]])

	def _next_i_path(self):
		incr = 0.01
		dist_to_next = 0
		while dist_to_next < incr:
			self.i_path += 1
			if self.i_path == len(self._path_points) - 1:
				self.i_path = len(self._path_points) - 1
				break
			dist_to_next += self._path_points[self.i_path][-1]


text:
import numpy as np
from scipy.fftpack import fft , fft2 , rfft
import matplotlib.pyplot as plt
import time

freq = 32
sampling_rate=55

t= np.linspace (0, 2, 2 *sampling_rate, endpoint=False)
x1= np.sin(freq* 2* np.pi * t)
x2= np.cos(5* 2* np.pi * t)
x3= np.sin(25* 2* np.pi * t)

x=x1+x2+x3

fig = plt.figure(1)
ax1 = fig.add_subplot(211)
ax1.plot(t, x)
ax1.set_xlabel('Time [s]')
ax1.set_ylabel('Signal amplitude');

fx = fft(x)
freqs = np.fft.fftfreq(len(x)) * sampling_rate

ax2 = fig.add_subplot(212)
ax2.stem(freqs, np.abs(fx))
ax2.set_xlabel('Frequency in Hertz [Hz]')
ax2.set_ylabel('Frequency Domain (Spectrum) Magnitude')

plt.show()

max_stars_repo_path: Numpy Testing/NumpyWavesGraphing.py

max_stars_repo_name: rickyzhangNYC/Senior-Design

max_issues_repo_path: Numpy Testing/NumpyWavesGraphing.py

max_issues_repo_name: rickyzhangNYC/Senior-Design

max_forks_repo_path: Numpy Testing/NumpyWavesGraphing.py

max_forks_repo_name: rickyzhangNYC/Senior-Design

reason: import numpy,from scipy


text:
import numpy as np
from typing import Sequence, Union

__all__ = ['randcorr', 'var2cov', 'varroll', 'covmix']

def randcorr(d: int, k: int) -> np.ndarray:
    """Generate random correlation matrix.

    https://stats.stackexchange.com/questions/124538/how-to-generate-a-large-full-rank-random-correlation-matrix-with-some-strong-cor#answer-125020
    d - number of dimensions
    k - number of factors
    """
    from numpy import diag, sqrt
    from numpy.random import rand, randn
    W = randn(d, k)
    S = W @ W.T + diag(rand(1, d))
    S = diag(1./sqrt(diag(S))) @ S @ diag(1./sqrt(diag(S)))
    return S

def var2cov(σ2: Sequence[float],
            k: int = 10) -> np.ndarray:
    """Construct covariance matrix with random correlation matrix and given marginal variances on its diagonal.

    k ∈ ℝ
        factors for correlation matrix
    σ2 ∈ ℝ^n
        marginal variances
    D ∈ ℝ^n,n
        standard deviations on diagonal
        D = diag(σ)
    P ∈ ℝ^n,n
        correlation matrix - linear dependence structure
        symmetric & positive semi-definite
        P = D^-1 Σ D^-1
        diag(P) = 1
    Σ ∈ ℝ^n,n
        covariance matrix
        symmetric & positive semi-definite
        Σ = D P D
        diag(Σ) = σ2

    symmetry: A = A.T
    positive semi-definiteness: ew(A) >= 0
    """
    if np.ndim(σ2) == 1:
        from numpy import diag, sqrt
        n = len(σ2)
        P = randcorr(n, k)
        D = diag(sqrt(σ2))
        Σ = D @ P @ D
        return Σ
    # mix
    return np.array([var2cov(var, len(var)) for var in σ2])

def varroll(pos: Union[int, Sequence[int]],
            quant: Sequence[int],
            var: Sequence[float]) -> np.ndarray:
    '''Generate rolled sequence/s of variances.'''
    if np.ndim(pos) == 0:
        lvar = []
        for q, v in zip(quant, var):
            lvar += q*[v]
        return np.roll(lvar, pos * quant[0])
    # mix
    return np.array([varroll(i, quant, var) for i in pos])

def covmix(varmix: np.ndarray) -> np.ndarray:
    '''Covariance matrices with given variances and random correlations.'''
    return var2cov(varmix, len(varmix))

reason: import numpy,from numpy


text:
import torch
import os
import numpy as np
import matplotlib.pyplot as plt

from torch import nn
from torch.optim import Adam
from torch.nn.utils.convert_parameters import parameters_to_vector
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm
from .base import Expert
from .ppo import PPO, PPOExpert
from .utils import NoisePreferenceDataset
from cail.network import StateFunction, DeterministicPolicy
from cail.utils import disable_gradient
from cail.buffer import SerializedBuffer
from cail.env import NormalizedEnv

class DREX(PPO):
    """
    Implementation of D-REX

    Reference:
    ----------
    [1] Brown,  D.  S.,  Goo,  W.,  and  Niekum,  S.
    Better-than-demonstrator imitation learning via automatically-ranked demonstrations.
    In Conference on Robot Learning, pp.330–359. PMLR, 2020.

    Parameters
    ----------
    buffer_exp: SerializedBuffer
        buffer of demonstrations
    state_shape: np.array
        shape of the state space
    action_shape: np.array
        shape of the action space
    device: torch.device
        cpu or cuda
    seed: int
        random seed
    env: NormalizedEnv
        environment
    gamma: float
        discount factor
    rollout_length: int
        rollout length of the buffer
    mix_buffer: int
        times for rollout buffer to mix
    batch_size_bc: int
        batch size for training Behavior Cloning
    batch_size_reward: int
        batch size for training D-REX's reward function
    size_reward_dataset: int
        size of the dataset for training D-REX's reward function
    n_reward_model: int
        number of reward functions to train
    lr_actor: float
        learning rate of the actor
    lr_critic: float
        learning rate of the critic
    lr_bc: float
        learning rate of the Behavior Cloning
    lr_reward: float
        learning rate of the D-REX's reward function
    units_actor: tuple
        hidden units of the actor
    units_critic: tuple
        hidden units of the critic
    units_bc: tuple
        hidden units of the Behavior Cloning
    units_reward: tuple
        hidden units of the D-REX's reward function
    epoch_ppo: int
        at each update period, update ppo for these times
    epoch_bc: int
        training epoch for Behavior Cloning
    epoch_reward: int
        training epoch for the D-REX's reward function
    l2_ratio_bc: float
        ratio of the l2 loss in Behavior Cloning's loss
    l2_ratio_reward: float
        ratio of the l2 loss in D-REX's loss
    noise_range: np.array
        range of noise injected to the BC policy
    n_demo_traj: int
        number of demonstration trajectories in the noise dataset
    steps_exp: int
        length of demonstrations used while training T-REX's reward function
    clip_eps: float
        clip coefficient in PPO's objective
    lambd: float
        lambd factor
    coef_ent: float
        entropy coefficient
    max_grad_norm: float
        maximum gradient norm
    """
    def __init__(
            self,
            buffer_exp: SerializedBuffer,
            state_shape: np.array,
            action_shape: np.array,
            device: torch.device,
            seed: int,
            env: NormalizedEnv,
            gamma: float = 0.995,
            rollout_length: int = 4096,
            mix_buffer: int = 1,
            batch_size_bc: int = 128,
            batch_size_reward: int = 64,
            size_reward_dataset: int = 5000,
            n_reward_model: int = 3,
            lr_actor: float = 3e-4,
            lr_critic: float = 3e-4,
            lr_bc: float = 1e-3,
            lr_reward: float = 1e-4,
            units_actor: tuple = (64, 64),
            units_critic: tuple = (64, 64),
            units_bc: tuple = (256, 256, 256),
            units_reward: tuple = (256, 256),
            epoch_ppo: int = 10,
            epoch_bc: int = 50000,
            epoch_reward: int = 10000,
            l2_ratio_bc: float = 1e-3,
            l2_ratio_reward: float = 0.01,
            noise_range: np.array = np.arange(0., 1.0, 0.05),
            n_demo_traj: int = 5,
            steps_exp: int = 40,
            clip_eps: float = 0.2,
            lambd: float = 0.95,
            coef_ent: float = 0.0,
            max_grad_norm: float = 0.5
    ):
        super().__init__(
            state_shape, action_shape, device, seed, gamma, rollout_length,
            mix_buffer, lr_actor, lr_critic, units_actor, units_critic,
            epoch_ppo, clip_eps, lambd, coef_ent, max_grad_norm
        )

        # synthetic noisy dataset
        self.noisy_dataset = NoisePreferenceDataset(
            env=env,
            device=device,
            max_steps=50,
            min_margin=0.3,
        )
        self.env = env

        # expert's buffer
        self.buffer_exp = buffer_exp

        # BC policy
        self.bc = DeterministicPolicy(
            state_shape=state_shape,
            action_shape=action_shape,
            hidden_units=units_bc,
            hidden_activation=nn.ReLU(inplace=True),
        ).to(device)
        self.optim_bc = Adam(self.bc.parameters(), lr=lr_bc)
        self.loss_bc = nn.MSELoss()
        self.batch_size_bc = batch_size_bc
        self.epoch_bc = epoch_bc
        self.l2_ratio_bc = l2_ratio_bc
        self.learning_steps_bc = 0
        self.save_bc = False

        # reward function
        self.reward_funcs = []
        self.reward_optims = []
        for i in range(n_reward_model):
            self.reward_funcs.append(StateFunction(
                state_shape=state_shape,
                hidden_units=units_reward,
                hidden_activation=nn.ReLU(inplace=True),
            ).to(device))
            self.reward_optims.append(Adam(self.reward_funcs[i].parameters(), lr=lr_reward))
        self.batch_size_reward = batch_size_reward
        self.epoch_reward = epoch_reward
        self.l2_ratio_reward = l2_ratio_reward
        self.learning_steps_reward = 0
        self.save_reward_func = False
        self.steps_exp = steps_exp

        # train bc
        self.train_bc()
        self.eval_bc(episodes=10)

        # build synthetic noisy dataset
        self.noisy_dataset.build(
            actor=self.bc,
            noise_range=noise_range,
            n_trajs=n_demo_traj,
        )
        self.save_noisy_dataset = False

        # train reward function
        self.train_reward(size_reward_dataset)

    def train_bc(self):
        """Train Behavior Cloning using the demonstrations"""
        print("Training behavior cloning")
        for t_bc in tqdm(range(self.epoch_bc)):
            states, actions, _, _, _ = self.buffer_exp.sample(self.batch_size_bc)
            loss = self.loss_bc(self.bc(states), actions) + \
                   self.l2_ratio_bc * parameters_to_vector(self.bc.parameters()).norm() ** 2
            self.optim_bc.zero_grad()
            loss.backward()
            self.optim_bc.step()
            self.learning_steps_bc += 1

            if self.learning_steps_bc % 5000 == 0:
                tqdm.write(f'step: {self.learning_steps_bc}, loss: {loss.item():.3f}')
        print("BC finished training")

    def eval_bc(self, episodes: int = 10):
        """
        Evaluate the BC policy

        Parameters
        ----------
        episodes: int
            number of episodes to evaluate
        """
        state = self.env.reset()
        rewards = []
        for _ in range(episodes):
            t = 0
            reward_traj = 0
            while True:
                t += 1
                action = self.bc(torch.tensor(state, dtype=torch.float, device=self.device).unsqueeze(0))
                next_state, reward, done, _ = self.env.step(action.cpu().detach().numpy()[0])
                reward_traj += reward
                if done or t >= self.env.max_episode_steps:
                    state = self.env.reset()
                    rewards.append(reward_traj)
                    break
                state = next_state
        print(f'BC reward: {np.mean(rewards)}+/-{np.std(rewards)}')

    def train_reward(self, size_reward_dataset: int):
        """
        Train D-REX's reward function

        Parameters
        ----------
        size_reward_dataset: int
            size of the noise dataset
        """
        print("Training reward function")

        # train each reward function
        for i in range(len(self.reward_funcs)):
            print(f'Reward function: {i}')
            self.learning_steps_reward = 0

            # load data
            data = self.noisy_dataset.sample(size_reward_dataset)

            idxes = np.random.permutation(len(data))
            train_idxes = idxes[:int(len(data) * 0.8)]
            valid_idxes = idxes[int(len(data) * 0.8):]

            def _load(idx_list, add_noise=True):
                if len(idx_list) > self.batch_size_reward:
                    idx = np.random.choice(idx_list, self.batch_size_reward, replace=False)
                else:
                    idx = idx_list

                batch = []
                for j in idx:
                    batch.append(data[j])

                b_x, b_y, b_l = zip(*batch)
                x_split = np.array([len(x) for x in b_x], dtype=int)
                y_split = np.array([len(y) for y in b_y], dtype=int)
                b_x, b_y, b_l = np.concatenate(b_x, axis=0), np.concatenate(b_y, axis=0), np.array(b_l)

                if add_noise:
                    b_l = (b_l + np.random.binomial(1, 0.1, self.batch_size_reward)) % 2  # flip with probability 0.1

                return (
                    torch.tensor(b_x, dtype=torch.float, device=self.device),
                    torch.tensor(b_y, dtype=torch.float, device=self.device),
                    x_split,
                    y_split,
                    torch.tensor(b_l, dtype=torch.float, device=self.device),
                )

            for it in tqdm(range(self.epoch_reward)):
                states_x, states_y, states_x_split, states_y_split, labels = _load(train_idxes, add_noise=True)
                logits_x = self.reward_funcs[i](states_x)
                logits_y = self.reward_funcs[i](states_y)

                ptr_x = 0
                logits_x_split = torch.tensor([], dtype=torch.float, device=self.device, requires_grad=True)
                for i_split in range(states_x_split.shape[0]):
                    logits_x_split = \
                        torch.cat((logits_x_split, logits_x[ptr_x: ptr_x + states_x_split[i_split]].sum().unsqueeze(0)))
                    ptr_x += states_x_split[i_split]
                ptr_y = 0
                logits_y_split = torch.tensor([], dtype=torch.float, device=self.device, requires_grad=True)
                for i_split in range(states_y_split.shape[0]):
                    logits_y_split = \
                        torch.cat((logits_y_split, logits_y[ptr_y: ptr_y + states_y_split[i_split]].sum().unsqueeze(0)))
                    ptr_y += states_y_split[i_split]

                labels = labels.long()
                logits_xy = torch.cat((logits_x_split.unsqueeze(1), logits_y_split.unsqueeze(1)), dim=1)
                loss_cal = torch.nn.CrossEntropyLoss()
                loss_cmp = loss_cal(logits_xy, labels)
                loss_l2 = self.l2_ratio_reward * parameters_to_vector(self.reward_funcs[i].parameters()).norm() ** 2
                loss_reward = loss_cmp + loss_l2
                self.reward_optims[i].zero_grad()
                loss_reward.backward()
                self.reward_optims[i].step()
                self.learning_steps_reward += 1

                if self.learning_steps_reward % 1000 == 0:
                    tqdm.write(f'step: {self.learning_steps_reward}, loss: {loss_reward.item():.3f}')
        print("Reward function finished training")

    def eval_reward(self, save_dir: str):
        """
        Plot the true reward and the learned reward

        Parameters
        ----------
        save_dir: str
            path to save
        """
        states, _, rewards, _, _ = self.buffer_exp.get()
        rewards_pred = 0
        with torch.no_grad():
            for i in range(len(self.reward_funcs)):
                rewards_pred += self.reward_funcs[i](states) / len(self.reward_funcs)
        rewards_pred = rewards_pred.cpu().detach().numpy().squeeze(1)
        rewards = rewards.cpu().detach().numpy().squeeze(1)
        idxes = np.argsort(rewards)
        plt.figure(0)
        plt.scatter(np.arange(rewards.shape[0]),
                    (rewards_pred[idxes] - rewards_pred.min()) / (rewards_pred.max() - rewards_pred.min()),
                    s=0.1)
        plt.scatter(np.arange(rewards.shape[0]),
                    (rewards[idxes] - rewards.min()) / (rewards.max() - rewards.min()),
                    s=0.1)
        plt.savefig(os.path.join(save_dir, '..', 'reward.png'))
        plt.close()

    def update(self, writer: SummaryWriter):
        """
        Update the algorithm

        Parameters
        ----------
        writer: SummaryWriter
            writer for logs
        """
        self.learning_steps += 1
        states, actions, _, dones, log_pis, next_states = \
            self.buffer.get()
        rewards = torch.zeros((states.shape[0], 1)).type_as(states)
        with torch.no_grad():
            for i in range(len(self.reward_funcs)):
                rewards += self.reward_funcs[i](states) / len(self.reward_funcs)
        self.update_ppo(
            states, actions, rewards, dones, log_pis, next_states, writer)

    def save_models(self, save_dir: str):
        """
        Save the model

        Parameters
        ----------
        save_dir: str
            path to save
        """
        if not os.path.isdir(save_dir):
            os.mkdir(save_dir)
        if not self.save_reward_func:
            for i in range(len(self.reward_funcs)):
                torch.save(self.reward_funcs[i].state_dict(), f'{save_dir}/../reward_func_{i}.pkl')
            self.save_reward_func = True
            self.eval_reward(save_dir)
        if not self.save_bc:
            torch.save(self.bc.state_dict(), f'{save_dir}/../bc.pkl')
            self.save_bc = True
        if not self.save_noisy_dataset:
            self.noisy_dataset.save(f'{save_dir}/..')
            self.save_noisy_dataset = True
        torch.save(self.actor.state_dict(), f'{save_dir}/actor.pkl')

class DREXExpert(PPOExpert):
    """
    Well-trained D-REX agent

    Parameters
    ----------
    state_shape: np.array
        shape of the state space
    action_shape: np.array
        shape of the action space
    device: torch.device
        cpu or cuda
    path: str
        path to the well-trained weights
    units_actor: tuple
        hidden units of the actor
    """
    def __init__(
            self,
            state_shape: np.array,
            action_shape: np.array,
            device: torch.device,
            path: str,
            units_actor: tuple = (64, 64)
    ):
        super(DREXExpert, self).__init__(
            state_shape=state_shape,
            action_shape=action_shape,
            device=device,
            path=path,
            units_actor=units_actor
        )

class DREXBCExpert(Expert):
    """
    Well-trained D-REX's BC policy

    Parameters
    ----------
    state_shape: np.array
        shape of the state space
    action_shape: np.array
        shape of the action space
    device: torch.device
        cpu or cuda
    path: str
        path to the well-trained weights
    units_actor: tuple
        hidden units of the actor
    """
    def __init__(
            self,
            state_shape: np.array,
            action_shape: np.array,
            device: torch.device,
            path: str,
            units_actor: tuple = (256, 256, 256)
    ):
        super().__init__(
            state_shape=state_shape,
            action_shape=action_shape,
            device=device,
        )
        self.actor = DeterministicPolicy(
            state_shape=state_shape,
            action_shape=action_shape,
            hidden_units=units_actor,
            hidden_activation=nn.ReLU(inplace=True),
        ).to(device)
        self.actor.load_state_dict(torch.load(path, map_location=device))
        disable_gradient(self.actor)


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

"""
"""
import codecs
import logging
from zlib import adler32

import numpy

class Lexicon(object):
    """
    Represents a lexicon of words.
    Each added word in this lexicon will be represented by a integer.
    This class has a variable '__readOnly' that control if the lexicon will or not insert new words.
    If a word in new to lexicon and '__readOnly' is true, so this lexicon will return a index which
        is related with a unknown word.
    """

    def __init__(self, unknownSymbol="UUUNKKK", name=None):
        """
        :param unknownSymbol: the string which represents all unknown words.
            If this parameter is none, so a warning message will be showed and a default unknown symbol is generated.
        :param name: the name of the object. This parameter will be used when you save this object.
            If this parameter is None, so it won't be possible to save it.
        """
        self.__lexicon = []
        self.__lexiconDict = {}
        self.__readOnly = False
        self.__name = name
        self.__log = logging.getLogger(__name__)

        # Keep how many times a word was inserted in the lexicon
        self.__countInsWord = []

        if unknownSymbol is None:
            name = name if name is not None else ""
            self.__log.warning("The unknown symbol of the lexicon '" + name + "' wasn't defined.")

            self.unknown_index = -1
        else:
            self.unknown_index = self.put(unknownSymbol, False)

    def isReadOnly(self):
        """
        :return: return if lexicon is adding new words
        """
        return self.__readOnly

    def getLexiconList(self):
        """
        :return: return a list of all words of the lexicon. The position of the word in the list is the idx of this word
            in the lexicon
        """
        return self.__lexicon

    def getLexiconDict(self):
        """
        :return: return a dictionary which contains the integers which represent each word.
        """
        return self.__lexiconDict

    def getLen(self):
        """
        Return the number of words in the lexicon.
        """
        return len(self.__lexicon)

    def put(self, word, isToCount=True):
        """
        Include a new word in the lexicon and return its index. If the word is
        already in the lexicon, then just return its index.
        If a word in new to lexicon and '__readOnly' is true, so this lexicon will return a index which
        is related with all unknown words.
        """

        idx = self.__lexiconDict.get(word)

        if idx is None:
            if self.isReadOnly():
                return self.getUnknownIndex()

            # Insert a unseen word in the lexicon.
            idx = len(self.__lexicon)
            self.__lexicon.append(word)
            self.__lexiconDict[word] = idx

            self.__countInsWord.append(0)

        if not self.isReadOnly() and isToCount:
            # Count how many times a word was insert in the lexicon
            # Stop to count when the lexicon is only read
            self.__countInsWord[idx] += 1

        return idx

    def getLexicon(self, index):
        """
        Return the word in the lexicon that is stored in the given index.
        """
        return self.__lexicon[index]

    def getLexiconIndex(self, word):
        """
        Return the index of the given word. If the word is not in the lexicon,
            so returns the unknown index.
        """
        return self.__lexiconDict.get(word, self.unknown_index)

    def setUnknown(self, unknownSymbol):
        self.unknown_index = self.getLexiconIndex(unknownSymbol)

    def getUnknownIndex(self):
        return self.unknown_index

    def isUnknownIndex(self, index):
        return index == self.unknown_index

    def isUnknownSymbolDefined(self):
        return self.unknown_index != -1

    def exist(self, word):
        return not self.isUnknownIndex(self.getLexiconIndex(word))

    def stopAdd(self):
        """
        Tell the class to stop adding new words
        :return:
        """
        self.__readOnly = True

    def prune(self, minCount):
        """
        Remove the words that weren't inserted lesser than minCount.
        The lexicon can't be pruned when it's read only.
        :param minCount: Minimum number of times a word most have been inserted to don't be removed
        :return:
        """
        newLexicon = []
        newLexiconDict = {}
        newCountInsWord = []

        if self.isReadOnly():
            self.__log.warning("You can't prune a read only lexicon")
            return

        # The removed words  from dictionary will be treated as unknown and
        # yours frequencies will be added to unknown
        countNewUnknown = 0

        for idx, nm in enumerate(self.__countInsWord):
            word = self.__lexicon[idx]

            if nm >= minCount or idx is self.getUnknownIndex():
                newIdx = len(newLexicon)
                newLexicon.append(word)
                newLexiconDict[word] = newIdx
                newCountInsWord.append(nm)
            else:
                countNewUnknown += nm

        if self.isUnknownSymbolDefined():
            newCountInsWord[self.getUnknownIndex()] = countNewUnknown

        self.__log.info("Number of words pruned using minCount=%d:  %d of %d" % (
        minCount, self.getLen() - len(newLexicon), self.getLen()))

        self.__lexicon = newLexicon
        self.__lexiconDict = newLexiconDict
        self.__countInsWord = newCountInsWord

    def getFrequencyOfAllWords(self):
        '''
        It returns a vector which contains the frequency of all words.
        The words are encoded by integers and each dimension of this vector is the frequency
        of a specific word.
        :return a numpy vector of integers
        '''

        if not self.isReadOnly():
            self.__log.warning(
                "The word frequencies can change, because the lexicon is not read only ")

        return numpy.asarray(self.__countInsWord)

    @staticmethod
    def fromTextFile(filePath, hasUnknowSymbol, lexiconName=None):
        """
        Create lexicon object from a text file
        :param filePath: path of the file with the words
        :param hasUnknowSymbol: if this parameter is true, so the first word of the file will be consider as the unknown
            symbol. However, if this parameter is false, so the unknown symbol will be undefined.
        :param lexiconName: name of lexicon
        :return: Lexicon
        """
        f = codecs.open(filePath, "r", encoding="utf-8")

        if hasUnknowSymbol:
            unknownSym = f.readline().strip("\n")
        else:
            unknownSym = None

        lexicon = Lexicon(unknownSym, lexiconName)

        for l in f:
            lexicon.put(l.rstrip("\n"))

        lexicon.stopAdd()

        return lexicon

    @staticmethod
    def fromList(labels, hasUnknowSymbol, lexiconName=None):
        """
        Create lexicon object from a list of labels.
        :param labels: list of labels
        :param hasUnknowSymbol: if this parameter is true, so the first word of the file will be consider as the unknown
            symbol. However, if this parameter is false, so the unknown symbol will be undefined.
        :param lexiconName: name of lexicon
        :return: Lexicon
        """
        idx = 0
        if hasUnknowSymbol:
            unknownSym = labels[idx]
            idx += 1
        else:
            unknownSym = None

        lexicon = Lexicon(unknownSym, lexiconName)

        for l in labels[idx:]:
            lexicon.put(l)

        lexicon.stopAdd()

        return lexicon

    def save(self, filePath):
        """
        Save a lexicon in a text file
        :return: None
        """
        f = codecs.open(filePath, "w", encoding="utf-8")

        for word in self.__lexicon:
            f.write(word)
            f.write("\n")

    def getName(self):
        return self.__name

    def getAttributes(self):
        return {
            "lexicon": [word.encode("unicode_escape") for word in self.__lexicon],
            "unknownIndex": self.unknown_index
        }

    @staticmethod
    def fromPersistentManager(persistentManager, name):
        """
        Return a lexicon from a PersistentManager
        :type persistentManager: persistence.PersistentManager.PersistentManager
        :param persistentManager: this object is a bridge between the database and PersistentObject
        :type name: basestring
        :param name: name of the object in database
        :return: Lexicon
        """
        newLexicon = Lexicon(name=name)
        persistentManager.load(newLexicon)

        return newLexicon

    def load(self, attributes):
        lexicon = attributes["lexicon"]
        # The only way that I found to tranform a Dataset to integer
        self.unknown_index = numpy.int(numpy.asarray(attributes["unknownIndex"]))

        if self.unknown_index == -1:
            name = self.__name if self.__name is not None else ""
            self.__log.warning("The unknown symbol of the lexicon '" + name + "' wasn't defined.")

        self.__lexicon = []
        self.__lexiconDict = {}
        self.__readOnly = False

        for word in lexicon:
            word = word.decode("unicode_escape")
            self.put(word)

        self.__readOnly = True

    def save(self, filePath):
        """
        Save a lexicon in a text file
        :return: None
        """
        f = codecs.open(filePath, "w", encoding="utf-8")

        for word in self.__lexicon:
            f.write(word)
            f.write("\n")

class HashLexicon(object):
    """
    This is a very simple, but effective, way of representing a lexicon.
    We just hash the values and use the hash code as the value index.
    """

    def __init__(self, size):
        self.__lexicon = None
        self.__lexiconDict = None
        self.unknown_index = -1
        self.__readOnly = False
        self.__size = size

    def isReadOnly(self):
        """
        :return: return if lexicon is adding new words
        """
        return self.__readOnly

    def getLexiconList(self):
        """
        :return: return a list of all words of the lexicon. The position of the word in the list is the idx of this word
            in the lexicon
        """
        return self.__lexicon

    def getLexiconDict(self):
        """
        :return: return a dictionary which contains the integers which represent each word.
        """
        return self.__lexiconDict

    def getLen(self):
        """
        Return the number of words in the lexicon.
        """
        return self.__size

    def put(self, word):
        """
        Include a new word in the lexicon and return its index. If the word is
        already in the lexicon, then just return its index.
        If a word in new to lexicon and '__readOnly' is true, so this lexicon will return a index which
        is related with all unknown words.
        """

        return self.getLexiconIndex(word)

    def getLexicon(self, index):
        """
        Return the word in the lexicon that is stored in the given index.
        """
        return None

    def getLexiconIndex(self, word):
        """
        Return the code of the given word. It is its hash code.
        """
        return adler32(word) % self.__size

    def getUnknownIndex(self):
        return self.unknown_index

    def isUnknownIndex(self, index):
        return index == self.unknown_index

    def exist(self, word):
        return True

    def setUnknownIndex(self, unknown_index):
        self.unknown_index = unknown_index

    def stopAdd(self):
        """
        """
        self.__readOnly = True


text:
# -*- coding: utf-8 -*-
"""
@author:XuMing(xuming624@qq.com)
@description:

样式迁移常用的损失函数由3部分组成：内容损失使合成图像与内容图像在内容特征上接近，样式损失令合成图像与样式图像在样式特征上接近，而总变差损失则有助于减少合成图像中的噪点。
可以通过预训练的卷积神经网络来抽取图像的特征，并通过最小化损失函数来不断更新合成图像。
用格拉姆矩阵表达样式层输出的样式。
"""
import argparse
import time

import numpy as np
import torch
import torch.nn.functional as F
import torchvision
from PIL import Image

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

parser = argparse.ArgumentParser()
parser.add_argument('--content_img_file', type=str, default='../samples/style_transfer_png/mount.png')
parser.add_argument('--style_img_file', type=str, default='../samples/style_transfer_png/autumn.png')
parser.add_argument('--image_max_size', type=int, default=600)
parser.add_argument('--max_epochs', type=int, default=500)
parser.add_argument('--log_epochs', type=int, default=20)
parser.add_argument('--lr_decay_epoch', type=int, default=200)
parser.add_argument('--content_weight', type=float, default=1, help="weight of content loss")
parser.add_argument('--style_weight', type=float, default=1000, help="weight of style loss")
parser.add_argument('--tv_weight', type=float, default=10, help="weight of total variation de-noising loss")
parser.add_argument('--lr', type=float, default=0.01)
parser.add_argument('--output_img_file', type=str, default='style_demo.png')
args = parser.parse_args()
print(args)

content_img = Image.open(args.content_img_file)
style_img = Image.open(args.style_img_file)
# 样式迁移的损失函数即内容损失、样式损失和总变差损失的加权和。
content_weight, style_weight, tv_weight = args.content_weight, args.style_weight, args.tv_weight

# 预处理和后处理图像
rgb_mean = np.array([0.485, 0.456, 0.406])
rgb_std = np.array([0.229, 0.224, 0.225])

style_layers, content_layers = [0, 5, 10, 19, 28], [25]

def get_net():
    pretrained_net = torchvision.models.vgg19(pretrained=True)
    # 在抽取特征时，我们只需要用到VGG从输入层到最靠近输出层的内容层或样式层之间的所有层。
    net_list = []
    for i in range(max(content_layers + style_layers) + 1):
        net_list.append(pretrained_net.features[i])
    net = torch.nn.Sequential(*net_list)
    return net.to(device)

net = get_net()

def preprocess(PIL_img, image_shape):
    process = torchvision.transforms.Compose([
        torchvision.transforms.Resize(image_shape),
        torchvision.transforms.ToTensor(),
        torchvision.transforms.Normalize(mean=rgb_mean, std=rgb_std)])

    return process(PIL_img).unsqueeze(dim=0)  # (batch_size, 3, H, W)

def postprocess(img_tensor):
    inv_normalize = torchvision.transforms.Normalize(
        mean=-rgb_mean / rgb_std,
        std=1 / rgb_std)
    to_PIL_image = torchvision.transforms.ToPILImage()
    return to_PIL_image(inv_normalize(img_tensor[0].cpu()).clamp(0, 1))

# 需要中间层的输出，因此这里我们逐层计算，并保留内容层和样式层的输出。
def extract_features(X, content_layers, style_layers):
    contents = []
    styles = []
    for i in range(len(net)):
        X = net[i](X)
        if i in style_layers:
            styles.append(X)
        if i in content_layers:
            contents.append(X)
    return contents, styles

def get_contents(image_shape):
    """
    对内容图像抽取内容特征
    :param image_shape:
    :return:
    """
    content_X = preprocess(content_img, image_shape).to(device)
    contents_Y, _ = extract_features(content_X, content_layers, style_layers)
    return content_X, contents_Y

def get_styles(image_shape):
    """
    对样式图像抽取样式特征
    :param image_shape:
    :return:
    """
    style_X = preprocess(style_img, image_shape).to(device)
    _, styles_Y = extract_features(style_X, content_layers, style_layers)
    return style_X, styles_Y

def content_loss(Y_hat, Y):
    """内容损失"""
    return F.mse_loss(Y_hat, Y)

def gram(X):
    """用这样的格拉姆矩阵表达样式层输出的样式"""
    num_channels, n = X.shape[1], X.shape[2] * X.shape[3]
    X = X.view(num_channels, n)
    return torch.matmul(X, X.t()) / (num_channels * n)

def style_loss(Y_hat, gram_Y):
    """样式损失"""
    return F.mse_loss(gram(Y_hat), gram_Y)

def tv_loss(Y_hat):
    """
    合成图像里面有大量高频噪点，即有特别亮或者特别暗的颗粒像素。
    一种常用的降噪方法是总变差降噪（total variation de-noising）。
    降低总变差损失能够尽可能使邻近的像素值相似。
    :param Y_hat:
    :return:
    """
    return 0.5 * (F.l1_loss(Y_hat[:, :, 1:, :], Y_hat[:, :, :-1, :]) +
                  F.l1_loss(Y_hat[:, :, :, 1:], Y_hat[:, :, :, :-1]))

def compute_loss(X, contents_Y_hat, styles_Y_hat, contents_Y, styles_Y_gram):
    # 分别计算内容损失、样式损失和总变差损失
    contents_l = [content_loss(Y_hat, Y) * content_weight for Y_hat, Y in zip(
        contents_Y_hat, contents_Y)]
    styles_l = [style_loss(Y_hat, Y) * style_weight for Y_hat, Y in zip(
        styles_Y_hat, styles_Y_gram)]
    tv_l = tv_loss(X) * tv_weight
    # 对所有损失求和
    l = sum(styles_l) + sum(contents_l) + tv_l
    return contents_l, styles_l, tv_l, l

# 在样式迁移中，合成图像是唯一需要更新的变量。
class GeneratedImage(torch.nn.Module):
    def __init__(self, img_shape):
        super(GeneratedImage, self).__init__()
        self.weight = torch.nn.Parameter(torch.rand(*img_shape))

    def forward(self):
        return self.weight

# 样式图像在各个样式层的格拉姆矩阵styles_Y_gram将在训练前预先计算好。
def get_inits(X, lr, styles_Y):
    gen_img = GeneratedImage(X.shape).to(device)
    gen_img.weight.data = X.data
    optimizer = torch.optim.Adam(gen_img.parameters(), lr=lr)
    styles_Y_gram = [gram(Y) for Y in styles_Y]
    return gen_img(), styles_Y_gram, optimizer

# 训练
def train(X, contents_Y, styles_Y, lr, max_epochs, lr_decay_epoch):
    print("training on device:", device)
    X, styles_Y_gram, optimizer = get_inits(X, lr, styles_Y)
    scheduler = torch.optim.lr_scheduler.StepLR(optimizer, lr_decay_epoch, gamma=0.1)
    for i in range(max_epochs):
        start = time.time()

        contents_Y_hat, styles_Y_hat = extract_features(
            X, content_layers, style_layers)
        contents_l, styles_l, tv_l, l = compute_loss(
            X, contents_Y_hat, styles_Y_hat, contents_Y, styles_Y_gram)

        optimizer.zero_grad()
        l.backward(retain_graph=True)
        optimizer.step()
        scheduler.step()

        if i % args.log_epochs == 0 and i != 0:
            print('epoch %3d/%3d, content loss %.2f, style loss %.2f, total loss %.2f, TV loss %.2f, %.2f sec/epoch'
                  % (i, max_epochs, sum(contents_l).item(), sum(styles_l).item(), tv_l.item(), l.item(),
                     time.time() - start))
    return X.detach()

def main():
    image_shape = (150, 225)
    content_X, contents_Y = get_contents(image_shape)
    style_X, styles_Y = get_styles(image_shape)
    output = train(content_X, contents_Y, styles_Y, args.lr, args.max_epochs, args.lr_decay_epoch)
    out_img = postprocess(output)
    out_img.save("style_small.png")

    # 为了得到更加清晰的合成图像，下面我们在更大的300×450尺寸上训练
    image_shape = (300, 450)
    _, content_Y = get_contents(image_shape)
    _, style_Y = get_styles(image_shape)
    X = preprocess(postprocess(output), image_shape).to(device)
    big_output = train(X, content_Y, style_Y, args.lr, args.max_epochs, args.lr_decay_epoch)
    out_img = postprocess(big_output)
    out_img.save(args.output_img_file)

if __name__ == '__main__':
    main()


text:
from __future__ import print_function, division

"""
Reads in table prepared by Marusa with all of BANYAN $\Sigma$ bona fide
members paired with Gaia astrometry
"""

from astropy.table import Table
import numpy as np

ddir = '../data/'
gagne_file = 'gagne_bonafide_full_kinematics_with_best_radial_velocity.fits'
galah_file = 'galah_banyan_bonafide_and_candidates_kinematics' \
             '_for_chronostar.fits'

gagnet = Table.read(ddir+gagne_file)
galaht = Table.read(ddir+galah_file)

max_stars_repo_name: tcrundall/chronostar

max_issues_repo_name: tcrundall/chronostar

max_forks_repo_name: tcrundall/chronostar

reason: import numpy,from astropy


text:
# Copyright 2017 The TensorFlow Authors All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Python interface for Revisited Oxford/Paris dataset."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
from scipy.io import matlab
import tensorflow as tf

_GROUND_TRUTH_KEYS = ['easy', 'hard', 'junk', 'ok']

def ReadDatasetFile(dataset_file_path):
  """Reads dataset file in Revisited Oxford/Paris ".mat" format.

  Args:
    dataset_file_path: Path to dataset file, in .mat format.

  Returns:
    query_list: List of query image names.
    index_list: List of index image names.
    ground_truth: List containing ground-truth information for dataset. Each
      entry is a dict corresponding to the ground-truth information for a query.
      The dict may have keys 'easy', 'hard', 'junk' or 'ok', mapping to a list
      of integers; additionally, it has a key 'bbx' mapping to a list of floats
      with bounding box coordinates.
  """
  with tf.gfile.GFile(dataset_file_path, 'r') as f:
    cfg = matlab.loadmat(f)

  # Parse outputs according to the specificities of the dataset file.
  query_list = [str(im_array[0]) for im_array in np.squeeze(cfg['qimlist'])]
  index_list = [str(im_array[0]) for im_array in np.squeeze(cfg['imlist'])]
  ground_truth_raw = np.squeeze(cfg['gnd'])
  ground_truth = []
  for query_ground_truth_raw in ground_truth_raw:
    query_ground_truth = {}
    for ground_truth_key in _GROUND_TRUTH_KEYS:
      if ground_truth_key in query_ground_truth_raw.dtype.names:
        adjusted_labels = query_ground_truth_raw[ground_truth_key] - 1
        query_ground_truth[ground_truth_key] = adjusted_labels.flatten()

    query_ground_truth['bbx'] = np.squeeze(query_ground_truth_raw['bbx'])
    ground_truth.append(query_ground_truth)

  return query_list, index_list, ground_truth


text:
#!/usr/bin/env python
# coding: utf-8

# In[15]:

import pandas as pd
import numpy as np
import math
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
import datetime
import matplotlib.patches as mpatches
from datetime import datetime

# ## Exploratory data analysis
#
# Aims for the project:
# 1. Improve referral process for connecting rough sleepers to service providers and users
# 2. Better identification of duplicate notifications
# 3. understand trends over time and space
#

# In[3]:

#read file
path = 'xxxxx'
file = pd.read_csv(path, sep = ';', error_bad_lines = False)

# ### Challenge for Streetlink
#
# #### Number of alerts in various region (London, coastline)

# In[4]:

# scatter plot of longitude and latitude with alpha controlling weight/density
get_ipython().run_line_magic('matplotlib', 'inline')
file.plot(kind = 'scatter', x = 'geo_location_longitude', y = 'geo_location_latitude', alpha = .05, figsize = (10,7))
plt.legend()

# In[10]:

#region
region = pd.DataFrame(file.groupby(by = 'region')['Alert ID'].count())
region['pct'] = region['Alert ID']/sum(region['Alert ID']) * 100
print(region)

# ### On an average day in London, Streetlink receive....on a peak day, they receive...

# In[20]:

file['alert_open_date_formulaT'] = pd.to_datetime(file['alert_open_date_formula'])
london = file[file['region'] == 'London']
y = pd.DataFrame(london.groupby(london['alert_open_date_formulaT'].dt.date)['Alert ID'].count())
y.describe()

# In[21]:

#across the entire dataset: What is the max, min, mean, standard deviation, etc?
y = pd.DataFrame(file.groupby(file['alert_open_date_formulaT'].dt.date)['Alert ID'].count())
y.describe()

# ### Duplicate alerts by region
# * West Midlands and Wales have the most duplicates alerts detected at ~16%
# * East of England and South West have the lowest percent of duplicate alerts at ~10%

# In[75]:

dupCases = file.loc[file['duplication_status'] == 'Duplicate',]
display(pd.DataFrame(pd.value_counts(dupCases.region)/region['Alert ID']))

# ## Part 1: Descriptive statistices of the variables
#
# we have unique indexes for the alerts, variables related to the rough sleeper(age, gender, location, time), duplicate status. We don't have the parent child node relationship between the referrals yet, therefore, duplicate cases are included in the descriptive statistics.

# #### We have 256,547 alerts in total in the processed file received on July 2nd, 2019

# In[26]:

file.shape[0]

# ### Continuous variables: how many days an alert remain in open ?

# In[76]:

display(pd.DataFrame(file.cases_days_open.describe()))

# ### Categorical variables
# historic alert: alert before 2017, age group is not fine grained

# In[81]:

print('There are {} types of alert record, they include:'. format(len(file.alert_record_type.unique())), file.alert_record_type.unique())
print('\n')
print('There are {} types of status, they include:'. format(len(file.status.unique())), file.status.unique())
print('\n')
print('There are {} types of Streetlink users, they include:'. format(len(file.referrer_type.unique())), file.referrer_type.unique())
print('\n')
print('There are {} regions, they include:'. format(len(file.region.unique())), file.region.unique())
print('\n')
print('There are {} age groups, they include:'. format(len(file.age_group.unique())), file.age_group.unique())

# ### Now we see how many NAs in each variable
# Most of alerts are created by individuals, because more than 98% of group alert are NA, nearly 30% of alerts do not have appearance description, this could be a problem for our deduplication process. Only 40% of users agree streetlink staff to contact them, among which 10 percent is from self referrer.

# In[30]:

# calculate percent missing for each column
percent_missing = file.isnull().sum() * 100 / len(file)
missing_value_df = pd.DataFrame({'column_name' : file.columns,
                               'percent_missing' : percent_missing})
missing_value_df

# how many contacts give consent to be contacted?

# In[34]:

display(file.groupby('contact_consent').size())
p = 90371/(18436+90371) *100
print(f'Since changing the process to ask for consent, {p:.2f}% of people consent to be contacted')

# In[82]:

#age group
agegroup = pd.DataFrame(file.groupby(by = 'duplication_status')['Alert ID'].count())
agegroup['pct'] = agegroup['Alert ID']/sum(agegroup['Alert ID']) * 100
display(agegroup)

# ### Age
# There are 4 age groups, nearly 60% of the cases do not have age information, 30% of the rough sleepers in this sample are 25-50 years old

# In[60]:

#age group
agegroup = pd.DataFrame(file.groupby(by = 'age_group')['Alert ID'].count())
agegroup['pct'] = agegroup['Alert ID']/sum(agegroup['Alert ID']) * 100
display(pd.DataFrame(agegroup.sort_values(by='pct',ascending=False)))

# ### Gender
# * Almost 46% of alerts do not have gender information.
# * Only 10% of them indicate the rough sleeper is a female

# In[59]:

#gender
gender = pd.DataFrame(file.groupby(by = 'gender')['Alert ID'].count())
gender['pct'] = gender['Alert ID']/sum(gender['Alert ID']) * 100
display(pd.DataFrame(gender.sort_values(by='pct', ascending=False)))

# ###  Percentage of alerts by local authority

# In[61]:

#LA check LA alert by time
def checkVarPer(file1, groupName):
    group = pd.DataFrame(file1.groupby(by = groupName)['Alert ID'].count())
    group['pct'] = group['Alert ID']/sum(group['Alert ID']) * 100
    display(pd.DataFrame(group.sort_values(by='pct',ascending=False)))

checkVarPer(file, 'local_authority_name')

# ### Insights:
#
# * There are about 30% of alerts without appearance description
# * 50% without gender information
# * 60% without age information.
#
# Cases with multiple NA fields *could* be low quality cases. These cases also cause problems in the deduplicate process

# ### Referral type
# Nearly 90% of the alerts are created by members in the public compared to self-reported alerts.

# In[62]:

#referral type
referrertype = pd.DataFrame(file.groupby(by = 'referrer_type')['Alert ID'].count())
referrertype['pct'] = referrertype['Alert ID']/sum(referrertype['Alert ID']) * 100
display(pd.DataFrame(referrertype))

# ### Outcome
# Nearly 37% of the alert outcomes are 'person not found' , only 12% have 'Action taken',that's why we need to predict the likelihood of an alert having person not found as outcome.

# In[64]:

#outcome
outcome = pd.DataFrame(file.groupby(by = 'outcome_summary')['Alert ID'].count())
outcome['pct'] = outcome['Alert ID']/sum(outcome['Alert ID']) * 100
display(pd.DataFrame(outcome).sort_values(ascending=False,by='pct'))

# #### How long are the cases open in each region
# More than 50% of the alerts close in 1 day, 75% of alerts in London close in 1 day. The London team works very hard, but they have a lot of person not found cases

# In[43]:

def describeAll(Var):
    casesStats = pd.DataFrame(file.groupby(by = 'region')[Var].describe())
    med = pd.DataFrame(file.groupby(by = 'region')[Var].median())
    casesStats['med'] = med
    return casesStats
describeAll('cases_days_open')

# Number of alerts in different regions, London has the highest number of alerts

# Averaged number of alerts created in different times of a day

# In[70]:

# how many days in this data set
df = file.groupby('alert_open_date_formula').nunique()
print('There are {} days in this data set'. format(df.shape[0]))

# #### Alerts throughout the day by region
# * There is a pattern of when people report by region (i.e. ~10am and ~8pm in London)

# In[69]:

def plotRegionHour1(Region):
    regionData = file[file.region == Region]
    hour = pd.DataFrame(regionData.groupby(by = 'hour_of_day_alert_raised')['Alert ID'].count())
    plt.plot(hour.index, hour['Alert ID']/2330)
    plt.xlabel('Hour', fontsize = 18)
    plt.ylabel('Number of alerts', fontsize = 18)
    plt.title(Region, fontsize = 20)

# In[47]:

def plotRegionHour(Region):
    regionData = file[file.region == Region]
    hour = pd.DataFrame(regionData.groupby(by = 'hour_of_day_alert_raised')['Alert ID'].count())
    plt.plot(hour.index, hour['Alert ID']/2330)
    plt.xlabel('Hour', fontsize = 10)
    plt.ylabel('Number of alerts', fontsize = 10)
    plt.title(Region, fontsize = 12)

# In[48]:

figure(num=None, figsize=(12,12), dpi = 100, facecolor = 'w', edgecolor = 'k')
plt.subplots_adjust(hspace = 0.8)
plt.subplot(5,3,1)
plotRegionHour('London')

plt.subplot(5,3,2)
plotRegionHour('Wales')

plt.subplot(5,3,3)
plotRegionHour('East Midlands')

plt.subplot(5,3,4)
plotRegionHour('East of England')

plt.subplot(5,3,5)
plotRegionHour('North East')

plt.subplot(5,3,6)
plotRegionHour('North West')

plt.subplot(5,3,7)
plotRegionHour('South East')

plt.subplot(5,3,8)
plotRegionHour('South West')

plt.subplot(5,3,9)
plotRegionHour('West Midlands')

plt.show()

# ### Temporal Statistics

# In[49]:

file['alert_open_date_formula'] = pd.to_datetime(file['alert_open_date_formula'])

# In[50]:

yd = pd.DataFrame(file.groupby(file['alert_open_date_formulaT'].dt.year)['Alert ID'].count())

# ### Why do we need dedup?
# The busiest day in a year in london shows almost ~6000 alerts coming in one day. De-duping could offset the bottlenecke experienced by Homeless Link Staff during peak season.

# In[71]:

def PlotAlertsMonth(Origin):
   # timeMask = (file['alert_open_date_formula'].dt.year >= fromYear) & (file['alert_open_date_formula'].dt.year <= toYear)
   # yearData = file[timeMask]
    yearData = file[file['alert_origin'] == Origin]
    yearData['alert_open_date_formula'] = sorted(yearData['alert_open_date_formula'], reverse = True)
    yd = pd.DataFrame(yearData.groupby(yearData['alert_open_date_formulaT'].dt.month)['Alert ID'].count())
    plt.plot(yd.index, yd['Alert ID'])
    plt.xlabel('Month', fontsize = 10)
    plt.xticks(rotation=60)
    #plt.ylim (0,26000)
    plt.ylabel('Number of alerts', fontsize = 10)
    plt.title('Number of alert aggregated by month from 2012-2019', fontsize = 12)

# In[72]:

figure(num=None, figsize=(12,3), dpi = 100, facecolor = 'w', edgecolor = 'k')
blue = mpatches.Patch(color='blue', label='Phone line')
orange = mpatches.Patch(color='orange', label='Phone App')
green = mpatches.Patch(color='green', label='Website')
plt.legend(handles=[blue,orange,green])
plt.subplots_adjust(hspace = 1)
PlotAlertsMonth('Phone')
PlotAlertsMonth('App')
PlotAlertsMonth('Web')
plt.show()

# #### Most of the alerts appear to be coming in early in the year and late in the year when the weather is cold.

# In[53]:

def PlotAlertsYear(Origin):
    timeMask = (file['alert_open_date_formula'].dt.year >= 2012) & (file['alert_open_date_formula'].dt.year <= 2018)
    yearData = file[timeMask]
    yearData = yearData[yearData['alert_origin'] == Origin]
    yearData['alert_open_date_formula'] = sorted(yearData['alert_open_date_formula'], reverse = True)
    yd = pd.DataFrame(yearData.groupby(yearData['alert_open_date_formula'].dt.year)['Alert ID'].count())
    plt.plot(yd.index, yd['Alert ID'])
    plt.xlabel('Year', fontsize = 10)
    plt.xticks(rotation=60)
    plt.ylabel('Number of alerts', fontsize = 10)
    plt.title('Number of alert aggregated by year from 2012-2018', fontsize = 12)

# In[54]:

figure(num=None, figsize=(12,3), dpi = 100, facecolor = 'w', edgecolor = 'k')
blue = mpatches.Patch(color='blue', label='Phone line')
orange = mpatches.Patch(color='orange', label='Phone App')
green = mpatches.Patch(color='green', label='Website')
plt.legend(handles=[blue,orange,green])
plt.subplots_adjust(hspace = 1)
PlotAlertsYear('Phone')
PlotAlertsYear('App')
PlotAlertsYear('Web')
plt.show()

# ### The number of website alerts appears to be increasing over time.
# * Phone line alerts appear stable over time.
# * Phone App alerts are stronger than phone line alerts, but show a year over year drop between 2017 and 2018.


text:
import os
import glob
import numpy as np
from scipy import misc

import torch
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
import random

class ToTensor(object):
    def __call__(self, sample):
        return torch.tensor(sample, dtype=torch.float32)

class dataset_8s(Dataset):
    def __init__(self, root_dir, dataset_type, img_size, color_invert =True, transform=None,shuffle=False):
        self.root_dir = root_dir
        self.shuffle = shuffle
        self.color_invert = color_invert
        print(self.root_dir)
        self.transform = transform
        self.file_names = [f for f in glob.glob(os.path.join(root_dir, "*.npz")) \
                            if dataset_type in f]
        print('number of files loaded:',len(self.file_names))
        self.img_size = img_size

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

    def __getitem__(self, idx):
        data_path = self.file_names[idx]
        data = np.load(data_path)
        image = data["image"].reshape(8,16, 80, 80)
        target = data["target"]
        meta_target = data["meta_target"]
        if self.shuffle:
            for i in range(8):
                context = image[i,:8, :, :]
                choices = image[i,8:, :, :]
                indices = list(range(8))
                np.random.shuffle(indices)
                new_target = indices.index(target[i])
                new_choices = choices[indices, :, :]
                image[i] = np.concatenate((context, new_choices))
                target[i] = new_target

        if meta_target.dtype == np.int8:
            meta_target = meta_target.astype(np.uint8)

        del data
        if self.transform:
            image = self.transform(image)
            target = torch.tensor(target, dtype=torch.long)
            meta_target = self.transform(meta_target)

        return image, target, meta_target

class dataset(Dataset):
    def __init__(self, root_dir, dataset_type, img_size, color_invert =True, transform=None,shuffle=False):
        self.root_dir = root_dir
        self.color_invert = color_invert
        self.shuffle = shuffle
        print(self.root_dir)
        self.transform = transform
        self.file_names = [f for f in glob.glob(os.path.join(root_dir, "*.npz")) \
                            if dataset_type in f]
        print('number of files loaded:',len(self.file_names))
        self.img_size = img_size

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

    def get_num_data(self):
        return self.__len__()

    def __getitem__(self, idx):

        data_path = self.file_names[idx]
        data = np.load(data_path)
        image = data["image"].reshape(16, 160, 160)

        resize_image = []
        for idx in range(0, 16):
            resize_image.append(misc.imresize(image[idx,:,:], (self.img_size, self.img_size)))
        resize_image = np.stack(resize_image)
        resize_image = resize_image/255.0

        if self.color_invert:
            resize_image = 1-resize_image

        target = data["target"]
        meta_target = data["meta_target"]

        if self.shuffle:
            context = resize_image[:8, :, :]
            choices = resize_image[8:, :, :]
            indices = list(range(8))
            np.random.shuffle(indices)
            new_target = indices.index(target)
            new_choices = choices[indices, :, :]
            resize_image = np.concatenate((context, new_choices),axis=0)
            target = new_target

        if meta_target.dtype == np.int8:
            meta_target = meta_target.astype(np.uint8)

        del data
        if self.transform:
            resize_image = self.transform(resize_image)
            target = torch.tensor(target, dtype=torch.long)
            meta_target = self.transform(meta_target)
        return resize_image, target, meta_target

class dataset_raven(Dataset):
    def load_subfolder_files(self,root_dir,dataset_type):
        all_files = []
        for r, d, f in os.walk(root_dir):
            for filename in f:
                if dataset_type in filename and 'npz' in filename:
                    all_files.append(os.path.join(r,filename))

        return all_files

    def __init__(self, root_dir, dataset_type, img_size, color_invert =False, transform=None, subfolder = True,shuffle=False):
        self.root_dir = root_dir
        self.color_invert = color_invert
        self.shuffle = shuffle
        print(self.root_dir)
        #print(os.path.join(root_dir,'*','*.npz'))
        self.transform = transform
        if not subfolder:
            self.file_names = [f for f in glob.glob(os.path.join(root_dir, "*.npz")) \
                            if dataset_type in f]
        else:
            self.file_names = self.load_subfolder_files(root_dir,dataset_type)

        print('number of files loaded:',len(self.file_names))
        self.img_size = img_size

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

    def get_num_data(self):
        return self.__len__()

    def __getitem__(self, idx):
        data_path = self.file_names[idx]
        data = np.load(data_path)
        image = data["image"].reshape(16, 160, 160)
        target = data["target"]
        meta_target = data["meta_target"]
        if random.randint(0,1) == 1:
            context = image[:8, :, :].copy()
            image[:3,:,:] = context[3:6,:,:]
            image[3:6,:,:] = context[:3,:,:]

        if self.shuffle:
            context = image[:8, :, :]
            choices = image[8:, :, :]
            indices = list(range(8))
            np.random.shuffle(indices)
            new_target = indices.index(target)
            new_choices = choices[indices, :, :]
            image = np.concatenate((context, new_choices))
            target = new_target

        resize_image = []
        for idx in range(0, 16):
            resize_image.append(misc.imresize(image[idx,:,:], (self.img_size, self.img_size)))
        resize_image = np.stack(resize_image)
        resize_image = resize_image / 255.0
        if self.color_invert:
            resize_image = 1-resize_image

        if meta_target.dtype == np.int8:
            meta_target = meta_target.astype(np.uint8)

        del data
        if self.transform:
            resize_image = self.transform(resize_image)
            target = torch.tensor(target, dtype=torch.long)
            meta_target = self.transform(meta_target)
        return resize_image, target, meta_target

max_stars_repo_name: thematrixduo/MXGNet

max_issues_repo_name: thematrixduo/MXGNet

max_forks_repo_name: thematrixduo/MXGNet


text:
import time
import logging
import random
import numpy as np
from tqdm import tqdm

import utils  # Most of the code in adopted from https://github.com/huggingface/pytorch-transformers/blob/master/examples/lm_finetuning/finetune_on_pregenerated.py
from pytorch_transformers.modeling_auto import AutoModelWithLMHead
from pytorch_transformers.tokenization_auto import AutoTokenizer
from pytorch_transformers.optimization import AdamW, WarmupLinearSchedule

from torch.utils.data import DataLoader, RandomSampler
import torch
import torch_xla
import torch_xla_py.xla_model as tpu_xm
import torch_xla_py.data_parallel as tpu_dp

from pytorch_transformers.modeling_roberta import RobertaModel
def RobertaModel_forward(self, input_ids, token_type_ids=None, attention_mask=None, position_ids=None, head_mask=None):
    return super(RobertaModel, self).forward(input_ids, token_type_ids, attention_mask, position_ids, head_mask)
RobertaModel.forward = RobertaModel_forward  # RobertaModel.forward has a `.item()` which doesn't work nicely with TPUs

def main():
    parser = utils.get_args_parser_with_general_args()
    parser.add_argument('--one_tpu', action='store_true', help="Run on one tpu core for degugging. Makes it easy to use break points")
    parser.add_argument('--tpu_report', action='store_true', help="Print xla metric report")
    args = parser.parse_args()

    utils.init(args)  # set seeds, init logger, prepare output directory

    devices = tpu_xm.get_xla_supported_devices()
    if args.one_tpu:
        devices = [devices[0]]
    n_tpu = len(devices)
    logging.info(f'Found {n_tpu} TPU cores')

    tokenizer = AutoTokenizer.from_pretrained(args.bert_model)
    tokenizer.save_pretrained(args.output_dir)

    args.start_epoch = utils.prepare_last_checkpoint(args.bert_model)
    model = AutoModelWithLMHead.from_pretrained(args.bert_model)  # Only Masked Language Modeling
    logging.info(f"Saving initial checkpoint to: {args.output_dir}")
    model.save_pretrained(args.output_dir)
    model = tpu_dp.DataParallel(model, device_ids=devices)

    num_data_epochs, num_train_optimization_steps= utils.get_dataset_stats(args, n_tpu)

    def tpu_training_loop(model, loader, device, context):
        """ Called by torch_xla_py.data_parallel. This function is executed on each core of the TPU once per epoch"""

        param_optimizer = list(model.named_parameters())
        no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
        optimizer_grouped_parameters = [
            {'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], 'weight_decay': 0.01},
            {'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
        ]

        # one optimizer and scheduler per TPU core. Both objects are saved in `context` to be reused the next epoch
        optimizer = context.getattr_or(
            'optimizer',
            AdamW(optimizer_grouped_parameters, lr=args.learning_rate, eps=args.adam_epsilon, betas=tuple(args.betas)))

        # derive warmup info
        if args.warmup_proportion is not None:
            warmup_steps = int(args.warmup_proportion * num_train_optimization_steps + 0.5)
        elif args.warmup_steps is not None:
            warmup_steps = args.warmup_steps
        else:
            raise Exception('What is the warmup?? Specify either warmup proportion or steps')
        scheduler = context.getattr_or(
            'scheduler',
            WarmupLinearSchedule(optimizer, warmup_steps=warmup_steps, t_total=num_train_optimization_steps))

        tr_loss = None
        pbar = None
        if str(pbar_device) == str(device):  # All threads are in sync. Use progress bar only on one of them
            pbar = tqdm(total=int(pbar_steps), desc=f"device {device}", dynamic_ncols=True)

        tracker = tpu_xm.RateTracker()

        model.train()
        for step, batch in loader:
            input_ids, input_mask, segment_ids, lm_label_ids, _ = batch
            outputs = model(input_ids, segment_ids, input_mask, lm_label_ids)
            loss = outputs[0]
            if args.gradient_accumulation_steps > 1:
                loss = loss / args.gradient_accumulation_steps
            loss.backward()
            tracker.add(args.train_batch_size)

            tr_loss = loss * args.gradient_accumulation_steps if step == 0 else  tr_loss + loss * args.gradient_accumulation_steps
            if pbar is not None:
                pbar.update(1)
                # pbar.set_description(desc=f'LR: {scheduler.get_lr()}')
            if (step + 1) % args.gradient_accumulation_steps == 0:
                tpu_xm.optimizer_step(optimizer)
                prev_lr = scheduler.get_last_lr()[0]
                scheduler.step()
                curr_lr = scheduler.get_last_lr()[0]
                if args.track_learning_rate:
                    if pbar is not None:
                        pbar.set_description(f"Prev LR: {prev_lr} Curr LR: {curr_lr}")
                optimizer.zero_grad()

        return tr_loss.item() / step  # `.item()` requires a trip from TPU to CPU, which is very slow. Use it only once per epoch=

    for epoch in range(args.start_epoch, args.epochs):
        # Load one training file into memory
        epoch_dataset = utils.PregeneratedDataset(epoch=epoch, training_path=args.pregenerated_data, tokenizer=tokenizer,
                                            num_data_epochs=num_data_epochs, reduce_memory=args.reduce_memory)
        train_sampler = RandomSampler(epoch_dataset)
        train_dataloader = DataLoader(epoch_dataset, sampler=train_sampler, batch_size=args.train_batch_size)

        pbar_device = devices[0]
        pbar_steps = utils.compute_num_steps_in_epoch(num_samples=train_sampler.num_samples,
                                                      batch_size=args.train_batch_size,
                                                      grad_accum_steps=1, # the pbar steps should not take into account grad accumulation steps
                                                      n_tpu=n_tpu)
        logging.info(f'start training, epoch {epoch} on {len(devices)} cores for {pbar_steps} steps')
        start = time.time()
        losses = model(tpu_training_loop, train_dataloader)  # calls `tpu_training_loop` multiple times, once per TPU core
        logging.info(f'Epoch {epoch} took {round(time.time() - start, 2)} seconds. Average loss: {sum(losses)/len(losses)}')
        utils.save_checkpoint(model._models[0], epoch, args.output_dir)

    if args.tpu_report:
        logging.info(torch_xla._XLAC._xla_metrics_report())

if __name__ == '__main__':
    main()


text:
# License: Apache-2.0
from gators.encoders.onehot_encoder import OneHotEncoder
from pandas.testing import assert_frame_equal
import pytest
import numpy as np
import pandas as pd
import databricks.koalas as ks
ks.set_option('compute.default_index_type', 'distributed-sequence')

@pytest.fixture
def data():
    X = pd.DataFrame({
            'A': ['Q', 'Q', 'W'],
            'B': ['Q', 'W', 'W'],
            'C': ['W', 'Q', 'W'],
            'D': [1, 2, 3]})
    X_expected = pd.DataFrame(
        {'D': {0: 1.0, 1: 2.0, 2: 3.0},
         'A__W': {0: 0.0, 1: 0.0, 2: 1.0},
         'A__Q': {0: 1.0, 1: 1.0, 2: 0.0},
         'B__W': {0: 0.0, 1: 1.0, 2: 1.0},
         'B__Q': {0: 1.0, 1: 0.0, 2: 0.0},
         'C__W': {0: 1.0, 1: 0.0, 2: 1.0},
         'C__Q': {0: 0.0, 1: 1.0, 2: 0.0}})
    obj = OneHotEncoder().fit(X)
    return obj, X, X_expected

@pytest.fixture
def data_int16():
    X = pd.DataFrame({
            'A': ['Q', 'Q', 'W'],
            'B': ['Q', 'W', 'W'],
            'C': ['W', 'Q', 'W'],
            'D': [1, 2, 3]})
    X_expected = pd.DataFrame(
        {'D': {0: 1, 1: 2, 2: 3},
         'A__W': {0: 0, 1: 0, 2: 1},
         'A__Q': {0: 1, 1: 1, 2: 0},
         'B__W': {0: 0, 1: 1, 2: 1},
         'B__Q': {0: 1, 1: 0, 2: 0},
         'C__W': {0: 1, 1: 0, 2: 1},
         'C__Q': {0: 0, 1: 1, 2: 0}}).astype(np.int16)
    obj = OneHotEncoder(dtype=np.int16).fit(X)
    return obj, X, X_expected

@pytest.fixture
def data_no_cat():
    X = pd.DataFrame(
        np.arange(12).reshape(3, 4),
        columns=list('ABCD'), dtype=float,
    )
    obj = OneHotEncoder().fit(X)
    return obj, X, X.copy()

@pytest.fixture
def data_ks():
    X = ks.DataFrame({
            'A': ['Q', 'Q', 'W'],
            'B': ['Q', 'W', 'W'],
            'C': ['W', 'Q', 'W'],
            'D': [1, 2, 3]})
    X_expected = pd.DataFrame(
        {'D': {0: 1.0, 1: 2.0, 2: 3.0},
         'A__W': {0: 0.0, 1: 0.0, 2: 1.0},
         'A__Q': {0: 1.0, 1: 1.0, 2: 0.0},
         'B__W': {0: 0.0, 1: 1.0, 2: 1.0},
         'B__Q': {0: 1.0, 1: 0.0, 2: 0.0},
         'C__W': {0: 1.0, 1: 0.0, 2: 1.0},
         'C__Q': {0: 0.0, 1: 1.0, 2: 0.0}})
    obj = OneHotEncoder().fit(X)
    return obj, X, X_expected

@pytest.fixture
def data_int16_ks():
    X = ks.DataFrame({
            'A': ['Q', 'Q', 'W'],
            'B': ['Q', 'W', 'W'],
            'C': ['W', 'Q', 'W'],
            'D': [1, 2, 3]})
    X_expected = pd.DataFrame(
        {'D': {0: 1, 1: 2, 2: 3},
         'A__W': {0: 0, 1: 0, 2: 1},
         'A__Q': {0: 1, 1: 1, 2: 0},
         'B__W': {0: 0, 1: 1, 2: 1},
         'B__Q': {0: 1, 1: 0, 2: 0},
         'C__W': {0: 1, 1: 0, 2: 1},
         'C__Q': {0: 0, 1: 1, 2: 0}}).astype(np.int16)
    obj = OneHotEncoder(dtype=np.int16).fit(X)
    return obj, X, X_expected

@pytest.fixture
def data_no_cat_ks():
    X = ks.DataFrame(
        np.arange(12).reshape(3, 4),
        columns=list('ABCD'), dtype=float,
    )
    obj = OneHotEncoder().fit(X)
    return obj, X, X.copy().to_pandas()

def test_pd(data):
    obj, X, X_expected = data
    X_new = obj.transform(X)
    assert_frame_equal(X_new, X_expected)

@pytest.mark.koalas
def test_ks(data_ks):
    obj, X, X_expected = data_ks
    X_new = obj.transform(X)
    assert_frame_equal(X_new.to_pandas(), X_expected)

def test_pd_np(data):
    obj, X, X_expected = data
    X_numpy = X.to_numpy()
    X_numpy_new = obj.transform_numpy(X_numpy)
    X_new = pd.DataFrame(X_numpy_new, columns=X_expected.columns)
    assert_frame_equal(X_new, X_expected)

@pytest.mark.koalas
def test_ks_np(data_ks):
    obj, X, X_expected = data_ks
    X_numpy = X.to_numpy()
    X_numpy_new = obj.transform_numpy(X_numpy)
    X_new = pd.DataFrame(X_numpy_new, columns=X_expected.columns)
    assert_frame_equal(X_new, X_expected)

def test_int16_pd(data_int16):
    obj, X, X_expected = data_int16
    X_new = obj.transform(X)
    assert_frame_equal(X_new, X_expected)

@pytest.mark.koalas
def test_int16_ks(data_int16_ks):
    obj, X, X_expected = data_int16_ks
    X_new = obj.transform(X)
    assert_frame_equal(X_new.to_pandas(), X_expected)

def test_int16_pd_np(data_int16):
    obj, X, X_expected = data_int16
    X_numpy = X.to_numpy()
    X_numpy_new = obj.transform_numpy(X_numpy)
    X_new = pd.DataFrame(X_numpy_new, columns=X_expected.columns)
    assert_frame_equal(X_new, X_expected)

@pytest.mark.koalas
def test_int16_ks_np(data_int16_ks):
    obj, X, X_expected = data_int16_ks
    X_numpy = X.to_numpy()
    X_numpy_new = obj.transform_numpy(X_numpy)
    X_new = pd.DataFrame(X_numpy_new, columns=X_expected.columns)
    assert_frame_equal(X_new, X_expected)

def test_without_cat_pd(data_no_cat):
    obj, X, X_expected = data_no_cat
    X_new = obj.transform(X)
    assert_frame_equal(X_new, X_expected)

@pytest.mark.koalas
def test_without_cat_ks(data_no_cat_ks):
    obj, X, X_expected = data_no_cat_ks
    X_new = obj.transform(X)
    assert_frame_equal(X_new.to_pandas(), X_expected)

def test_without_cat_pd_np(data_no_cat):
    obj, X, X_expected = data_no_cat
    X_numpy = X.to_numpy()
    X_numpy_new = obj.transform_numpy(X_numpy)
    X_new = pd.DataFrame(X_numpy_new, columns=X_expected.columns)
    assert_frame_equal(X_new, X_expected)

@pytest.mark.koalas
def test_without_cat_ks_np(data_no_cat_ks):
    obj, X, X_expected = data_no_cat_ks
    X_numpy = X.to_numpy()
    X_numpy_new = obj.transform_numpy(X_numpy)
    X_new = pd.DataFrame(X_numpy_new, columns=X_expected.columns)
    assert_frame_equal(X_new, X_expected)

max_stars_repo_name: Aditya-Kapadiya/gators

max_issues_repo_name: Aditya-Kapadiya/gators

max_forks_repo_name: Aditya-Kapadiya/gators


text:
import os
import argparse
import numpy as np
from parse_archival_scan import parse_archival_scan, get_v0_output_file, get_v0_output_dir
from parse_archival_txt import parse_archival_txt
from parse_archival_fits import parse_archival_fits
from convert_to_equatorial import convert_to_equatorial
from add_contextual_info import add_contextual_info
from convert_llh_to_prob import convert_llh_to_prob
from create_plot import create_plot

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-o", "--output_dir")
    parser.add_argument("-c", "--cache_dir")
    parser.add_argument("-e", "--event", default=None)
    parser.add_argument("-d", "--distribution", default="IC160427A", help="IC160427A, IC170922A or diffuse")
    args = parser.parse_args()

    if args.event is not None:
        candidates = [args.event]
    elif args.cache_dir is not None:
        candidates = sorted([y for y in os.listdir(args.cache_dir) if not np.logical_and(
            np.logical_and("event" not in y, "Event" not in y),
            np.logical_and("run" not in y, "Run" not in y),
        )])
    else:
        candidates = sorted([y for y in os.listdir(get_v0_output_dir(args.output_dir)) if "event" in y])

    for candidate in candidates:
        print(candidate)
        try:
            if args.cache_dir is not None:

                try:

                    try:
                        cand_name = parse_archival_scan(candidate, args.output_dir, args.cache_dir)
                    except IOError:
                        cand_name = parse_archival_txt(candidate, args.output_dir, args.cache_dir)

                except IOError:
                    cand_name = parse_archival_fits(candidate, args.output_dir, args.cache_dir)

            else:
                 cand_name = get_v0_output_file(candidate)

            add_contextual_info(cand_name, args.output_dir)
            convert_to_equatorial(cand_name, args.output_dir)
            sys_filename = convert_llh_to_prob(cand_name, args.output_dir, args.distribution)
            create_plot(sys_filename, args.output_dir)

        except KeyError:
            pass


text:
# Real world example on a dataset FLINNG is good at.
# To use this script, download the promethion dataset form
# https://drive.google.com/file/d/1EIN8uUuy98oIqYfHadtc2KzOzRH_E1Cs/view?usp=sharing
# Then copy it to this folder and run this script. When running it again, you
# can changed saved_as_npy to true to use the created .npy file.

import numpy as np
import flinng
import time

saved_as_npy = False
if not saved_as_npy:
  promethion = np.loadtxt("promethion-data")
  np.save("promethion-data", promethion)
else:
  promethion = np.load("promethion-data.npy")
gts = np.load("promethion-gt.npy")

index = flinng.sparse_32_bit(num_rows=2, cells_per_row=50000, num_hash_tables=100, hashes_per_table=2, hash_range_pow=18)
index.add_points(promethion[10001:]) # This 100001 isn't a bug, for some reason the generated gt skips point 10000
index.prepare_for_queries()
start = time.time()
results = index.query(promethion[:10000], 100)
r10at100 = sum([sum([gt[i] in res for i in range(10)]) for gt, res in zip(gts, results)]) / 10 / 10000
print(f"R10@100 = {r10at100}")

max_stars_repo_name: JoshEngels/FLINNG

max_issues_repo_name: JoshEngels/FLINNG

max_forks_repo_name: JoshEngels/FLINNG


text:
import pycuda.driver as drv
drv.init()
if drv.Context.get_current() is None:
    import pycuda.autoinit

import os
from pycuda.compiler import SourceModule
from gpustats.util import get_cufiles_path

class CUDAModule(object):
    """
    Interfaces with PyCUDA

    Parameters
    ----------
    kernel_dict :
    """
    def __init__(self, kernel_dict):
        self.kernel_dict = kernel_dict
        self.support_code = _get_support_code()

        self.all_code = self._get_full_source()
        try:
            # dict mapping contexts to their respective loaded code modules
            self.pycuda_modules = {
                drv.Context.get_current(): SourceModule(self.all_code)
            }
        except Exception:
            f = open('foo.cu', 'w')
            print >> f, self.all_code
            f.close()
            raise

    def _get_full_source(self):
        formatted_kernels = [kern.get_code()
                             for kern in self.kernel_dict.values()]
        return '\n'.join([self.support_code] + formatted_kernels)

    def get_function(self, name):
        # get the module for this context
        context = drv.Context.get_current()
        try:
            mod = self.pycuda_modules[context]
        except KeyError:
            # if it's a new context, init the module
            self.pycuda_modules[context] = SourceModule(self.all_code)
            mod = self.pycuda_modules[context]
        return mod.get_function('k_%s' % name)

def _get_support_code():
    path = os.path.join(get_cufiles_path(), 'support.cu')
    return open(path).read()

def _get_mvcaller_code():
    # for multivariate PDFs
    path = os.path.join(get_cufiles_path(), 'mvcaller.cu')
    return open(path).read()

class Kernel(object):

    def __init__(self, name):
        if name is None:
            raise ValueError('Kernel must have a default name')

        self.name = name

    def get_code(self):
        logic = self.get_logic()
        caller = self.get_caller()
        return '\n'.join((logic, caller))

    def get_logic(self, **kwargs):
        raise NotImplementedError

    def get_caller(self, **kwargs):
        raise NotImplementedError

    def get_name(self, name=None):
        # can override default name, for transforms. this a hack?
        if name is None:
            name = self.name

        return name

class CUFile(Kernel):
    """
    Expose kernel contained in .cu file in the cufiles directory to code
    generation framework. Kernel need only have a template to be able to change
    the name of the generated kernel
    """
    def __init__(self, name, file_path):
        self.full_path = os.path.join(
            get_cufiles_path(),
            file_path
        )

        Kernel.__init__(self, name)

    def get_code(self):
        code = open(self.full_path).read()
        return code % {'name': self.name}

    def get_logic(self, **kwargs):
        raise NotImplementedError

    def get_caller(self, **kwargs):
        raise NotImplementedError

class MVDensityKernel(Kernel):
    """
    Generate kernel for multi-variate probability density function
    """
    _caller = _get_mvcaller_code()

    def __init__(self, name, logic_code):
        self.logic_code = logic_code
        Kernel.__init__(self, name)

    def get_logic(self, name=None):
        return self.logic_code % {'name': self.get_name(name)}

    def get_caller(self, name=None):
        return self._caller % {'name': self.get_name(name)}

class Exp(Kernel):
    op = 'expf'

    def __init__(self, name, kernel):
        self.kernel = kernel
        Kernel.__init__(self, name)

    def get_logic(self, name=None):
        name = self.get_name(name)

        actual_name = '%s_stub' % name
        kernel_logic = self.kernel.get_logic(name=actual_name)

        stub_caller = """
__device__ float %(name)s(float* x, float* params, int dim) {
    return %(op)s(%(actual_kernel)s(x, params, dim));
}
"""

        transform_logic = stub_caller % {
            'name': name,
            'actual_kernel': actual_name,
            'op': self.op
        }

        return '\n'.join((kernel_logic, transform_logic))

    def get_caller(self):
        return self.kernel.get_caller(self.name)

_cu_module = None

def get_full_cuda_module():
    import gpustats.kernels as kernels
    global _cu_module

    if _cu_module is None:
        all_kernels = dict(
            (k, v)
            for k, v in kernels.__dict__.iteritems()
            if isinstance(v, Kernel)
        )
        _cu_module = CUDAModule(all_kernels)

    return _cu_module

max_stars_repo_name: whitews/gpustats

max_issues_repo_name: whitews/gpustats

max_forks_repo_name: whitews/gpustats

reason: import pycuda,from pycuda