text:
#! /usr/bin/env python
import copy
from copy import deepcopy
import rospy
import threading
import quaternion
import numpy as np
from geometry_msgs.msg import Point
from visualization_msgs.msg import *
from franka_interface import ArmInterface
from panda_robot import PandaArm
import matplotlib.pyplot as plt
from scipy.spatial.transform import Rotation
np.set_printoptions(precision=2)
"""

This is a FORCE-BASED VARIABLE IMPEDANCE CONTROLLER based on [Huang1992: Compliant Motion Control of Robots by Using Variable Impedance]

To achieve force tracking, the apparent stiffness (K) and damping (B) is dynamically adjusted through functions dependent on the error in position, velocity and force

About the code/controller:

1] Only stiffness and damping in the 'z'-direction is adaptive, the rest are static

2] Due to the faulted joint velocities (read from rostopics), the more noisy,
    numerically derived derivatives of the joint position are prefered to be
        used in the controller { get_x_dot(..., numerically = True) }

3] You can now choose between perform_torque_Huang1992() and perform_torque_DeSchutter()
    - DeSchutter's control-law offers geometrically consistent stiffness and is more computationally expensive

4] The default desired motion- and force-trajectories are now made in a time-consistent matter, so that the PUBLISH RATE can be altered without messing up the desired behaviour.
    The number of iterations is calculated as a function of the controller's control-cycle, T: (max_num_it = duration(=15 s) / T)

5] IN THE FIRST LINE OF CODE BELOW "if __name__ == "__main__":" YOU HAVE TO SET SIM EQUAL TO "True" OR "False"
            - if True: starting position = neutral
            - if False: starting position = cartboard, Fz = (-) robot.endpoint_effort...
"""
# --------- Constants -----------------------------

#print(robot.joint_ordered_angles()) #Read the robot's joint-angles

cartboard = {'panda_joint1': 1.5100039307153879, 'panda_joint2': 0.6066719992230666, 'panda_joint3': 0.024070900507747097, 'panda_joint4': -2.332000750114692, 'panda_joint5': -0.037555063873529436, 'panda_joint6': 2.9529732850154575, 'panda_joint7': 0.7686490028450895}

# Stiffness
Kp = 30
Kpz = 50 #initial value (adaptive)
Ko = 900
K = np.array([[Kp, 0, 0, 0, 0, 0],
                [0, Kp, 0, 0, 0, 0],
                [0, 0, Kpz, 0, 0, 0],
                [0, 0, 0, Ko, 0, 0],
                [0, 0, 0, 0, Ko, 0],
                [0, 0, 0, 0, 0, Ko]])

# Damping
Bp = Kp/30
Bpz = Bp # #initial value (adaptive)
Bo = 10
B = np.array([[Bp, 0, 0, 0, 0, 0],
                [0, Bp, 0, 0, 0, 0],
                [0, 0, Bpz, 0, 0, 0],
                [0, 0, 0, Bo, 0, 0],
                [0, 0, 0, 0, Bo, 0],
                [0, 0, 0, 0, 0, Bo]])

# Apparent inertia
Mp = 10
Mo = 10
M_diag = np.array([Mp,Mp,Mp,Mo,Mo,Mo])
M = np.diagflat(M_diag)

# Constant matrices appearing in equation (50) of [Huang1992]
K_v = np.identity(6)
P = np.identity(6)
gamma = np.identity(18)

#gamma_M = 12
gamma_B = 0.001*10 #2    # The damping's rate of adaptivity (high value = slow changes)
gamma_K = 0.0005/10 #1    # The stiffness' rate of adaptivity (high value = slow changes)
#gamma[2,2] = gamma_M
gamma[8,8] = gamma_B
gamma[14,14] = gamma_K

duration = 15 #seconds SHOULD NOT BE ALTERED

"""Functions for generating desired MOTION trajectories"""

#1  Generate a desired trajectory for the manipulator to follow
def generate_desired_trajectory(iterations,T):
    a = np.zeros((6,iterations))
    v = np.zeros((6,iterations))
    p = np.zeros((3,iterations))
    p[:,0] = get_p()

    if iterations > 300:
        a[2,0:100]=-0.00001/T**2
        a[2,250:350]=0.00001/T**2

    if iterations > 6500:
        a[0,4500:4510]=0.00001/T**2
        a[0,6490:6500]=-0.00001/T**2
    for i in range(max_num_it):
        if i>0:
            v[:,i]=v[:,i-1]+a[:,i-1]*T
            p[:,i]=p[:,i-1]+v[:3,i-1]*T
    return a,v,p

#2  Generate a desired trajectory for the manipulator to follow
def generate_desired_trajectory_express(iterations,T):
    a = np.zeros((6,iterations))
    v = np.zeros((6,iterations))
    p = np.zeros((3,iterations))
    p[:,0] = get_p()

    if iterations > 175:
        a[2,0:50]=-0.00002/T**2
        a[2,50:100]=0.00002/T**2

    if iterations > 3250:
        a[0,2250:2255]=0.00002/T**2
        a[0,3245:3250]=-0.00002/T**2
    for i in range(max_num_it):
        if i>0:
            v[:,i]=v[:,i-1]+a[:,i-1]*T
            p[:,i]=p[:,i-1]+v[:3,i-1]*T
    return a,v,p

#3  Generate a (time-consistent) desired motion trajectory
def generate_desired_trajectory_tc(iterations,T,move_in_x=False, move_down=True):
    a = np.zeros((6,iterations))
    v = np.zeros((6,iterations))
    p = np.zeros((3,iterations))
    p[:,0] = get_p()

    if move_down:
        a[2,0:int(iterations/75)]=-0.25
        a[2,int(iterations*2/75):int(iterations/25)]= 0.25

    if move_in_x:
        a[0,int(max_num_it*4/10):int(max_num_it*5/10)]=0.015
        a[0,int(max_num_it*7/10):int(max_num_it*8/10)]=-0.015

    for i in range(max_num_it):
        if i>0:
            v[:,i]=v[:,i-1]+a[:,i-1]*T
            p[:,i]=p[:,i-1]+v[:3,i-1]*T
    return a,v,p

"""Functions for generating desired FORCE trajectories"""

#1  Generate a desired force trajectory that takes offset into consideration
def generate_F_d_robot(max_num_it,T,sim=False):
    a = np.zeros((6,max_num_it))
    v = np.zeros((6,max_num_it))
    s = np.zeros((6,max_num_it))
    s[2,0]= get_Fz(sim)
    a[2,0:100] = 0.0005/T**2
    a[2,100:200] = - 0.0005/T**2

    for i in range(max_num_it):
        if i>0:
            v[2,i]=v[2,i-1]+a[2,i-1]*T
            s[2,i]=s[2,i-1]+v[2,i-1]*T

    return s

#2  Generate an efficient desired force trajectory
def generate_F_d_express(max_num_it,T):
    a = np.zeros((6,max_num_it))
    v = np.zeros((6,max_num_it))
    s = np.zeros((6,max_num_it))

    a[2,0:50] = 0.0010/T**2
    a[2,100:150] = - 0.0010/T**2
    if max_num_it > 275:
        a[2,250:275] = 0.0008/T**2
    if max_num_it >2001:
        a[2,750:775]=-0.0008/T**2
        it = 1000
        while it <= 2000:
            a[2,it]= (-9*(np.pi**2)*(T/4)**2*np.sin(2*it*T/4*2*np.pi+np.pi/2))/T**2
            it+=1
        a[2,2001]=0.0001/T**2

    for i in range(max_num_it):
        if i>0:
            v[2,i]=v[2,i-1]+a[2,i-1]*T
            s[2,i]=s[2,i-1]+v[2,i-1]*T

    return s

#3  Generate a (time-consistent) desired force trajectory
def generate_Fd_advanced(max_num_it,T):
    a = np.zeros((6,max_num_it))
    v = np.zeros((6,max_num_it))
    s = np.zeros((6,max_num_it))

    a[2,0:int(max_num_it/75)] = 62.5
    a[2,int(max_num_it/37.5):int(max_num_it/25)] = - 62.5
    if max_num_it > 275:
        a[2,int(max_num_it/15):int(max_num_it*11/150)] = 50
    if max_num_it >2001:
        a[2,int(max_num_it/5):int(max_num_it*31/150)]=-50
        it = int(max_num_it*4/15)
        while it <= int(max_num_it*8/15):
            a[2,it]= (-9*(np.pi**2)*(T/4)**2*np.sin(2*it*T/4*2*np.pi+np.pi/2))/T**2
            it+=1
        a[2,int(max_num_it*8/15+1)]=6.25

    for i in range(max_num_it):
        if i>0:
            v[2,i]=v[2,i-1]+a[2,i-1]*T
            s[2,i]=s[2,i-1]+v[2,i-1]*T

    return s

# generate a STEEP desired force trajectory [STABLE]
def generate_Fd_steep(max_num_it,T,sim=False):
    a = np.zeros((6,max_num_it))
    v = np.zeros((6,max_num_it))
    s = np.zeros((6,max_num_it))
    s[2,0]= get_Fz(sim)
    v[2,0]=2.5
    a[2,max_num_it/15:3*max_num_it/15] = - 1.25

    for i in range(max_num_it):
        if i>0:
            v[2,i]=v[2,i-1]+a[2,i-1]*T
            s[2,i]=s[2,i-1]+v[2,i-1]*T

    return s

# ------------ Helper functions --------------------------------

# Calculate the numerical derivative of a each row in a vector
def get_derivative_of_vector(history,iteration,time_per_iteration):
    size = history.shape[0]
    if iteration > 0:
        T = float(time_per_iteration[int(iteration)]-time_per_iteration[int(iteration-1)])
        if T > 0:
            return np.subtract(history[:,iteration],history[:,iteration-1])/T

    return np.zeros(size)

# Saturation-function
def ensure_limits(lower,upper,matrix):
    for i in range(6):
        if matrix[i,i] > upper:
            matrix[i,i] = upper
        elif matrix[i,i] < lower:
            matrix[i,i] = lower

# Return the cartesian (task-space) inertia of the manipulator [alternatively the inverse of it]
def get_W(jacobian,robot_inertia, inv = False):
    W = np.linalg.multi_dot([jacobian,np.linalg.inv(robot_inertia),jacobian.T])
    if inv == True:
        return np.linalg.inv(W)
    else:
        return W

# Return the external forces (everything except for z-force is set to 0 due to offsets)
def construct_F_ext(Fz,two_dim = False):
    if two_dim == True:
        return np.array([0,0,Fz,0,0,0]).reshape([6,1])
    else:

        return np.array([0,0,Fz,0,0,0])

#Return only the force in z
def get_Fz(sim=False):
    if sim:
        return robot.endpoint_effort()['force'][2]
    else:
        return -robot.endpoint_effort()['force'][2]

# Return the position and (relative) orientation
def get_x(goal_ori):
    pos_x = robot.endpoint_pose()['position']
    rel_ori = quatdiff_in_euler_radians(np.asarray(robot.endpoint_pose()['orientation']), goal_ori)  #used to be opposite
    return np.append(pos_x,rel_ori)

# Return the linear and angular velocities
# Numerically = True -> return the derivarive of the state-vector
# Numerically = False -> read values from rostopic (faulty in sim when interacting with the environment)
def get_x_dot(x_hist,i,time_per_iteration, numerically=False):
    if numerically == True:
        return get_derivative_of_vector(x_hist,i,time_per_iteration)
    else:
        return np.append(robot.endpoint_velocity()['linear'],robot.endpoint_velocity()['angular'])

# Return the error in position and orientation
def get_delta_x(x, p_d, two_dim = False):
    delta_pos = p_d - x[:3]
    delta_ori = x[3:]
    if two_dim == True:
        return np.array([np.append(delta_pos,delta_ori)]).reshape([6,1])

    else:
        return np.append(delta_pos,delta_ori)

# Return the error in linear and angular velocities
def get_x_dot_delta(x_d_dot,x_dot, two_dim = True):
    if two_dim == True:
        return (x_d_dot - x_dot).reshape([6,1])
    else:
        return x_d_dot - x_dot

# Return the error in linear and angular acceleration
def get_x_ddot_delta(x_d_ddot,v_history,i,time_per_iteration):
    a = get_derivative_of_vector(v_history,i,time_per_iteration)
    return x_d_ddot-a

# Return the cartesian (task-space) position [only used in offline planning of the desired trajectory]
def get_p(two_dim=False):
    if two_dim == True:
        return robot.endpoint_pose()['position'].reshape([3,1])
    else:
        return robot.endpoint_pose()['position']

# Compute difference between quaternions and return Euler angle in radians as difference
def quatdiff_in_euler_radians(quat_curr, quat_des):
    curr_mat = quaternion.as_rotation_matrix(quat_curr)
    des_mat = quaternion.as_rotation_matrix(quat_des)
    rel_mat = des_mat.T.dot(curr_mat)
    rel_quat = quaternion.from_rotation_matrix(rel_mat)
    vec = quaternion.as_float_array(rel_quat)[1:]
    if rel_quat.w < 0.0:
        vec = -vec
    return -des_mat.dot(vec)

# -------------- Main functions --------------------

# Get xi as it is described in equation (44) in [Huang1992]
def get_xi(x_dot, x_d_dot, x_d_ddot, delta_x,  x_dot_history, i, time_per_iteration):
    E = -delta_x
    E_dot = -get_x_dot_delta(x_d_dot,x_dot, two_dim = False)
    E_ddot = -get_x_ddot_delta(x_d_ddot,x_dot_history,i,time_per_iteration)
    E_diag = np.diagflat(E)
    E_dot_diag = np.diagflat(E_dot)
    E_ddot_diag = np.diagflat(E_ddot)
    return np.block([E_ddot_diag,E_dot_diag,E_diag])

# Calculate lambda_dot as in equation (50) in [Huang1992]
def get_lambda_dot(gamma,xi,K_v,P,F_d,F_ext_2D,i,time_per_iteration,T):
    if i > 0:
        T = float(time_per_iteration[i]-time_per_iteration[i-1])
    return np.linalg.multi_dot([-np.linalg.inv(gamma),xi.T,np.linalg.inv(K_v),P,F_ext_2D-F_d.reshape([6,1])])*T

# Return the updated (adapted) Inertia, Damping and Stiffness matrices.
def update_MBK_hat(lam,M,B,K):
    M_hat = M # + np.diagflat(lam[0:6]) M is chosen to be constant
    B_hat = B + np.diagflat(lam[6:12])
    K_hat = K + np.diagflat(lam[12:18])
    #ensure_limits(1,5000,M_hat)
    ensure_limits(1,300,B_hat)
    ensure_limits(10,1000,K_hat)
    return M_hat, B_hat, K_hat

# Calculate and perform the torque as in equation (10) in [Huang1992]
def perform_torque_Huang1992(M, B, K, x_d_ddot, x_d_dot,x,x_dot, p_d, F_ext_2D, jacobian,robot_inertia,joint_names):
    a = np.linalg.multi_dot([jacobian.T,get_W(jacobian,robot_inertia,inv=True),np.linalg.inv(M)])
    b = np.array([np.dot(M,x_d_ddot)]).reshape([6,1]) + np.array([np.dot(B,get_x_dot_delta(x_d_dot,x_dot))]).reshape([6,1]) + np.array([np.dot(K,get_delta_x(x,p_d,two_dim = True))]).reshape([6,1])
    c = robot.coriolis_comp().reshape([7,1])
    d = (np.identity(6)-np.dot(get_W(jacobian,robot_inertia,inv=True),np.linalg.inv(M))).reshape([6,6])
    total_torque = np.array([np.dot(a,b)]).reshape([7,1]) + c + np.array([np.linalg.multi_dot([jacobian.T,d,F_ext_2D])]).reshape([7,1])
    robot.set_joint_torques(dict(list(zip(joint_names,total_torque))))

"""
    TESTING AREA (Functions needed to run an adaptive version of DeSchutter's impedance controller)
    [with geometrically consistent stiffness]
"""
def skew(vector):
    return np.array([[0, -vector[2], vector[1]],
                     [vector[2], 0, -vector[0]],
                     [-vector[1], vector[0], 0]])

def from_three_to_six_dim(matrix):
    return np.block([[matrix,np.zeros((3,3))],[np.zeros((3,3)),matrix]])

def get_K_Pt_dot(R_d,K_pt,R_e):
    return np.array([0.5*np.linalg.multi_dot([R_d,K_pt,R_d.T])+0.5*np.linalg.multi_dot([R_e,K_pt,R_e.T])])

def get_K_Pt_ddot(p_d,R_d,K_pt,delta_x):
    return np.array([0.5*np.linalg.multi_dot([skew(delta_x[:3]),R_d,K_pt,R_d.T])])

def E_quat(quat_n,quat_e):
    return np.dot(quat_n,np.identity(3))-skew(quat_e)

def get_K_Po_dot(quat_n,quat_e,R_e,K_po):
    return np.array([2*np.linalg.multi_dot([E_quat(quat_n,quat_e).T,R_e,K_po,R_e.T])])

def get_h_delta(K_pt_dot,K_pt_ddot,p_delta,K_po_dot,quat_e):
    f_delta_t = np.array([np.dot(K_pt_dot,p_delta)])
    m_delta_t = np.array([np.dot(K_pt_ddot,p_delta)])
    null = np.zeros((3,1))
    m_delta_o = np.array([np.dot(K_po_dot,quat_e)])

    return np.array([np.append(f_delta_t.T,m_delta_t.T)]).T + np.array([np.append(null.T,m_delta_o.T)]).T

def perform_torque_DeSchutter(M, B, K, x_d_ddot, x_d_dot,x_dot,delta_x, p_d, Rot_e,Rot_d,F_ext_2D,jacobian, robot_inertia, joint_names,sim): # must include Rot_d
    Rot_e_bigdim = from_three_to_six_dim(Rot_e)
    Rot_e_dot = np.dot(skew(x_dot[3:]),Rot_e) #not a 100 % sure about this one
    Rot_e_dot_bigdim = from_three_to_six_dim(Rot_e_dot)

    quat = quaternion.from_rotation_matrix(np.dot(Rot_e.T,Rot_d)) #orientational displacement represented as a unit quaternion
    quat_e_e = np.array([quat.x,quat.y,quat.z]) # vector part of the unit quaternion in the frame of the end effector
    quat_e = np.dot(Rot_e.T,quat_e_e) # ... in the base frame
    quat_n = quat.w

    p_delta = delta_x[:3]

    K_Pt_dot = get_K_Pt_dot(Rot_d,K[:3,:3],Rot_e)
    K_Pt_ddot = get_K_Pt_ddot(p_d,Rot_d,K[:3,:3],delta_x)
    K_Po_dot = get_K_Po_dot(quat_n,quat_e,Rot_e,K[3:,3:])

    h_delta_e = np.array(np.dot(Rot_e_bigdim,get_h_delta(K_Pt_dot,K_Pt_ddot,p_delta,K_Po_dot,quat_e))).reshape([6,1])
    h_e_e = np.array(np.dot(Rot_e_bigdim,F_ext_2D))

    a_d_e = np.dot(Rot_e_bigdim,x_d_ddot).reshape([6,1])
    v_d_e = np.dot(Rot_e_bigdim,x_d_dot).reshape([6,1])
    alpha_e = a_d_e + np.dot(np.linalg.inv(M),(np.dot(B,v_d_e.reshape([6,1])-np.dot(Rot_e_bigdim,x_dot).reshape([6,1]))+h_delta_e-h_e_e)).reshape([6,1])
    alpha = np.dot(Rot_e_bigdim.T,alpha_e).reshape([6,1])+np.dot(Rot_e_dot_bigdim.T,np.dot(Rot_e_bigdim,x_dot)).reshape([6,1])
    torque = np.linalg.multi_dot([jacobian.T,get_W(jacobian, robot_inertia, inv=True),alpha]).reshape((7,1)) + np.array(robot.coriolis_comp().reshape((7,1))) + np.dot(jacobian.T,F_ext_2D).reshape((7,1))
    robot.set_joint_torques(dict(list(zip(joint_names,torque))))
"""
    TESTING AREA
"""
# -------------- Plotting ------------------------

def plot_result(time_per_iteration,p,p_d, x_dot,delta_x, F_ext_raw,F_d_raw, z_dynamics,M,B,K,publish_rate):

    Fz_ext_raw = F_ext_raw[2]
    Fz_ext = Fz_ext_raw -Fz_ext_raw[0]#remove offset
    Fz_d_raw = F_d_raw[2]
    Fz_d = Fz_d_raw - Fz_d_raw[0]#remove offset

    adjusted_time_per_iteration = time_per_iteration - time_per_iteration[0]
    new_list = np.zeros(len(p[0]))
    new_list[0]=adjusted_time_per_iteration[0]
    for i in range(len(adjusted_time_per_iteration)):
        if i >0:
            new_list[i] = adjusted_time_per_iteration[i]-adjusted_time_per_iteration[i-1]

    plt.subplot(231)
    plt.title("External force")
    plt.plot(adjusted_time_per_iteration, Fz_ext, label="force z [N]")
    plt.plot(adjusted_time_per_iteration, Fz_d, label="desired force z [N]", color='b',linestyle='dashed')
    plt.xlabel("Real time [s]")
    plt.legend()

    plt.subplot(232)
    plt.title("Position")
    plt.plot(adjusted_time_per_iteration, p[0,:], label = "true x [m]")
    plt.plot(adjusted_time_per_iteration, p[1,:], label = "true y [m]")
    plt.plot(adjusted_time_per_iteration, p[2,:], label = "true z [m]")

    plt.plot(adjusted_time_per_iteration, p_d[0,:], label = "desired x [m]", color='b',linestyle='dashed')
    plt.plot(adjusted_time_per_iteration, p_d[1,:], label = "desired y [m]", color='C1',linestyle='dashed')
    plt.plot(adjusted_time_per_iteration, p_d[2,:], label = "desired z [m]", color='g',linestyle='dashed')
    plt.xlabel("Real time [s]")
    plt.legend()

    plt.subplot(233)
    plt.title("Orientation error in Euler")
    plt.plot(adjusted_time_per_iteration, delta_x[3]*(180/np.pi), label = "error  Ori_x [degrees]")
    plt.plot(adjusted_time_per_iteration, delta_x[4]*(180/np.pi), label = "error  Ori_y [degrees]")
    plt.plot(adjusted_time_per_iteration, delta_x[5]*(180/np.pi), label = "error  Ori_z [degrees]")
    plt.xlabel("Real time [s]")
    plt.legend()

    plt.subplot(234)
    plt.title("Adaptive dynamics along the z-axis")
    plt.plot(adjusted_time_per_iteration, z_dynamics[0], label = "inertia (M_z)")
    plt.plot(adjusted_time_per_iteration, z_dynamics[1], label = "damping (B_z)")
    plt.plot(adjusted_time_per_iteration, z_dynamics[2], label = "stiffness (K_z)")
    plt.axhline(y=M[2][2], label = "initial inertia (M_z)", color='b',linestyle='dashed')
    plt.axhline(y=B[2][2], label = "initial damping (B_z)", color='C1',linestyle='dashed')
    plt.axhline(y=K[2][2], label = "initial stiffness (K_z)", color='g',linestyle='dashed')
    plt.xlabel("Real time [s]")
    plt.legend()

    plt.subplot(235)
    plt.title("velocity used in calculation")
    plt.plot(adjusted_time_per_iteration, x_dot[0], label = "vel x")
    plt.plot(adjusted_time_per_iteration,x_dot[1], label = "vel y")
    plt.plot(adjusted_time_per_iteration,x_dot[2], label = "vel z")
    plt.plot(adjusted_time_per_iteration,x_dot[3], label = "ang x")
    plt.plot(adjusted_time_per_iteration,x_dot[4], label = "ang y")
    plt.plot(adjusted_time_per_iteration,x_dot[5], label = "ang z")
    plt.xlabel("Real time [s]")
    plt.legend()

    plt.subplot(236)
    plt.title("Time per iteration")
    plt.plot(new_list, label = "time per iteration")
    plt.axhline(y=1/float(publish_rate), label = 'desired time-step', color='C1', linestyle = 'dashed')
    plt.xlabel("iterations")
    plt.legend()
    """
    plt.subplot(235)
    plt.title("velocity read from rostopic")
    plt.plot(adjusted_time_per_iteration, v[0], label = "vel x")
    plt.plot(adjusted_time_per_iteration, v[1], label = "vel y")
    plt.plot(adjusted_time_per_iteration, v[2], label = "vel z")
    plt.plot(adjusted_time_per_iteration, v[3], label = "ang x")
    plt.plot(adjusted_time_per_iteration, v[4], label = "ang y")
    plt.plot(adjusted_time_per_iteration, v[5], label = "ang z")
    plt.xlabel("Real time [s]")
    plt.legend()

    plt.subplot(236)
    plt.title("numerically calculated velocity")
    plt.plot(adjusted_time_per_iteration, v_num[0], label = "vel x")
    plt.plot(adjusted_time_per_iteration, v_num[1], label = "vel y")
    plt.plot(adjusted_time_per_iteration, v_num[2], label = "vel z")
    plt.plot(adjusted_time_per_iteration, v_num[3], label = "ang x")
    plt.plot(adjusted_time_per_iteration, v_num[4], label = "ang y")
    plt.plot(adjusted_time_per_iteration, v_num[5], label = "ang z")
    plt.xlabel("Real time [s]")
    plt.legend()
    """

    plt.show()

# move to neutral or alternative starting position (Dependent on sim/not sim)
def move_to_start(alternative_position, sim):
    if sim:
        robot.move_to_neutral()
    else:
        robot.move_to_joint_positions(alternative_position)

def fetch_states(i,time_per_iteration, p_d, x_history, x_dot_history, sim):

    x = get_x(goal_ori)
    x_history[:,i] = x
    p = x[:3]
    jacobian = robot.zero_jacobian()
    robot_inertia = robot.joint_inertia_matrix()
    Fz = get_Fz(sim)
    F_ext = construct_F_ext(Fz)
    F_ext_2D = F_ext.reshape([6,1])
    if sim:
        x_dot = get_x_dot(x_history,i,time_per_iteration, numerically=True)
    else:
        x_dot = get_x_dot(x_history,i,time_per_iteration, numerically=False)
    x_dot_history[:,i] = x_dot
    delta_x = get_delta_x(x, p_d)
    Rot_e = robot.endpoint_pose()['orientation_R']

    return Rot_e, p,x,x_dot, x_history, x_dot_history, delta_x, jacobian,robot_inertia,Fz,F_ext,F_ext_2D

if __name__ == "__main__":

    # ---------- Initialization -------------------
    sim = True
    rospy.init_node("impedance_control")
    robot = ArmInterface() #PandaArm()
    joint_names = robot.joint_names()
    publish_rate = 50
    rate = rospy.Rate(publish_rate)
    T = 0.001*(1000/publish_rate)
    max_num_it = duration*publish_rate
    move_to_start(cartboard,sim)

    # List used to contain data needed for calculation of the torque output
    lam = np.zeros(18)
    #v_history = np.zeros((6,max_num_it))
    x_dot_history = np.zeros((6,max_num_it))

    # Lists providing data for plotting
    p_history = np.zeros((3,max_num_it))
    v_history_num = np.zeros((6,max_num_it))
    x_history = np.zeros((6,max_num_it))
    delta_x_history = np.zeros((6,max_num_it))
    F_ext_history = np.zeros((6,max_num_it))
    z_dynamics_history = np.zeros((3,max_num_it))

    time_per_iteration = np.zeros(max_num_it)

    # Specify the desired behaviour of the robot
    x_d_ddot, x_d_dot, p_d = generate_desired_trajectory_tc(max_num_it,T,move_in_x = True)
    goal_ori = np.asarray(robot.endpoint_pose()['orientation']) # goal orientation = current (initial) orientation [remains the same the entire duration of the run]
    Rot_d = robot.endpoint_pose()['orientation_R'] # used by the DeSchutter implementation
    F_d = generate_Fd_steep(max_num_it,T,sim)

    # ----------- The control loop  -----------
    for i in range(max_num_it):
        # update state-lists
        time_per_iteration[i]=rospy.get_time()
        Rot_e,p, x, x_dot,x_history,x_dot_history, delta_x, jacobian, robot_inertia, Fz, F_ext, F_ext_2D = fetch_states(i,time_per_iteration, p_d[:,i],x_history,x_dot_history,sim)

        #v_history_num[:,i] = get_x_dot(x_history,i,T, numerically=True) # only for plotting
        #v_history[:,i] = get_x_dot(x_history,i,T) # for calculating error in acceleration

        # adapt M,B and K
        xi = get_xi(x_dot, x_d_dot[:,i], x_d_ddot[:,i], delta_x, x_dot_history, i, time_per_iteration)
        lam = lam.reshape([18,1]) + get_lambda_dot(gamma,xi,K_v,P,F_d[:,i],F_ext_2D, i,time_per_iteration,T).reshape([18,1])
        M_hat,B_hat,K_hat = update_MBK_hat(lam,M,B,K)

        # Apply the resulting torque to the robot
        """CHOOSE ONE OF THE TWO CONTROLLERS BELOW"""
        perform_torque_Huang1992(M_hat, B_hat, K_hat, x_d_ddot[:,i], x_d_dot[:,i],x, x_dot, p_d[:,i],F_ext_2D, jacobian, robot_inertia,joint_names)
        #perform_torque_DeSchutter(M_hat, B_hat, K_hat, x_d_ddot[:,i], x_d_dot[:,i],x_dot, delta_x,p_d[:,i], Rot_e, Rot_d,F_ext_2D, jacobian, robot_inertia,joint_names, sim)

        # plotting and printing
        z_dynamics_history[0][i]=M_hat[2][2]
        z_dynamics_history[1][i]=B_hat[2][2]
        z_dynamics_history[2][i]=K_hat[2][2]

        p_history[:,i] = p
        x_history[:,i] = x
        delta_x_history[:,i] = delta_x
        F_ext_history[:,i] = F_ext

        # Live printing to screen when the controller is running
        if i%100 == 0:
            print(i,'/',max_num_it,' = ',T*i,' [s]   ) Force in z: ',F_ext_history[2,i])
            print(K_hat[2][2])
            print('')

        rate.sleep()

    #Uncomment the block below to save plotting-data
    """
    np.save('VIC_p_d.npy',p_d)
    np.save('VIC_p.npy',p_history)
    np.save('VIC_Fz_d.npy',F_d)
    np.save('VIC_Fz.npy',F_ext_history[2])
    np.save('VIC_delta_x.npy',delta_x_history) #orientation error in radians
    np.save('VIC_adaptive_gains.npy',z_dynamics_history)
    """

    plot_result(time_per_iteration, p_history, p_d, x_dot_history, delta_x_history, F_ext_history, F_d, z_dynamics_history,M,B,K,publish_rate)


text:
import sys

sys.path.append('./')
import os

import faiss
import numpy as np
from arguments import IndexArguments, DataArguments, ModelArguments, TrainingArguments
from transformers import HfArgumentParser
from evaluate import validate, load_test_data

from LibVQ.base_index import FaissIndex

faiss.omp_set_num_threads(32)

if __name__ == '__main__':
    parser = HfArgumentParser((IndexArguments, DataArguments, ModelArguments, TrainingArguments))
    index_args, data_args, model_args, training_args = parser.parse_args_into_dataclasses()

    # Load embeddings
    emb_size = 768
    doc_embeddings = np.memmap(os.path.join(data_args.embeddings_dir, 'docs.memmap'),
                               dtype=np.float32, mode="r")
    doc_embeddings = doc_embeddings.reshape(-1, emb_size)
    query_embeddings = np.memmap(os.path.join(data_args.embeddings_dir, 'test-queries.memmap'),
                                 dtype=np.float32, mode="r")
    query_embeddings = query_embeddings.reshape(-1, emb_size)

    # Creat Faiss index
    index = FaissIndex(index_method=index_args.index_method,
                       emb_size=len(doc_embeddings[0]),
                       ivf_centers_num=index_args.ivf_centers_num,
                       subvector_num=index_args.subvector_num,
                       subvector_bits=index_args.subvector_bits,
                       dist_mode=index_args.dist_mode)

    print('Training the index with doc embeddings')
    # index.CPU_to_GPU(0)
    index.fit(doc_embeddings)
    index.add(doc_embeddings)
    # index.GPU_to_CPU()

    index_file = os.path.join(data_args.embeddings_dir,
                              f'{index_args.index_method}_ivf{index_args.ivf_centers_num}_pq{index_args.subvector_num}x{index_args.subvector_bits}.index')
    index.save_index(index_file)
    # index.load_index(index_file)

    # Test the performance
    scores, ann_items = index.search(query_embeddings, topk=100, nprobe=index_args.nprobe)
    test_questions, test_answers, collections = load_test_data(
        query_andwer_file='./data/NQ/raw_dataset/nq-test.qa.csv',
        collections_file='./data/NQ/dataset/collection.tsv')
    validate(ann_items, test_questions, test_answers, collections)

ext: py

max_stars_repo_name: staoxiao/LibVQ

max_issues_repo_name: staoxiao/LibVQ

max_forks_repo_name: staoxiao/LibVQ

reason: import numpy


text:
import numpy as np
import torch
import cv2
import imageio
from skimage.transform import resize
from datetime import datetime

import torchvision.datasets
import torchvision.transforms as transforms

def resize_batch(batch_img, size, batch_size):
    return resize(batch_img.permute(0, 2, 3, 1), (batch_size, size, size))

def get_img_cube(img_batch, img_i):
    cur_img = img_batch[img_i]
    cur_img_cube = None
    for j in range(4):
        cur_anim = cur_img[j*64:(j+1)*64]
        for i in range(9):
            cur_frame = cur_anim[:,i*64:(i+1)*64]
            cur_img_cube = cur_frame if cur_img_cube is None else np.concatenate((cur_img_cube, cur_frame), axis=2)
    return cur_img_cube

def convert_to_img_plane(real_inputs, batch_size):
    real_img_cubes = None
    for batch_img_i in range(batch_size):
        cur_img = real_inputs[batch_img_i]
        cur_img_cube = None
        for anim_i in range(4):
            cur_anim = cur_img[:,anim_i*64:(anim_i+1)*64]
            for frame_i in range(9):
                cur_frame = cur_anim[:,:,frame_i*64:(frame_i+1)*64]
                cur_img_cube = cur_frame if cur_img_cube is None else torch.cat((cur_img_cube, cur_frame), dim=0)
        cur_img_cube = cur_img_cube.unsqueeze(0)
        real_img_cubes = cur_img_cube if real_img_cubes is None else torch.cat((real_img_cubes, cur_img_cube), dim=0)
    return real_img_cubes

def convert_to_img_cube(real_inputs, batch_size):
    real_img_cubes = None
    for batch_img_i in range(batch_size):
        cur_img = real_inputs[batch_img_i]
        cur_img_cube = None
        for anim_i in range(4):
            cur_anim = cur_img[:,anim_i*64:(anim_i+1)*64]
            for frame_i in range(9):
                cur_frame = cur_anim[:,:,frame_i*64:(frame_i+1)*64].unsqueeze(1)
                cur_img_cube = cur_frame if cur_img_cube is None else torch.cat((cur_img_cube, cur_frame), dim=1)
        cur_img_cube = cur_img_cube.unsqueeze(0)
        real_img_cubes = cur_img_cube if real_img_cubes is None else torch.cat((real_img_cubes, cur_img_cube), dim=0)
    return real_img_cubes

def preprocess_data(batch_size=64, data_size=40_000):
    assert data_size % batch_size == 0
    dataset = torchvision.datasets.ImageFolder(root="data", transform=transforms.Compose([transforms.ToTensor()]))
    dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=False)
    processed_data = torch.empty([data_size, 108, 64, 64], dtype=torch.uint8)
    for batch_index, (real_images, _) in enumerate(dataloader, 0):
        real_images = convert_to_img_plane((real_images * 255).type(torch.ByteTensor), batch_size)
        processed_data[batch_index*batch_size:(batch_index+1)*batch_size] = real_images
        if (batch_index + 1) % 10 == 0:
            print(f"{datetime.now()} {(batch_index+1)*batch_size}")
        if (batch_index+1)*batch_size >= data_size:
            break
    return processed_data

def preprocess_scaled_data(scale, batch_size=64, data_size=40_000):
    assert data_size % batch_size == 0

    img_sizes = {0: 4,
                 1: 8,
                 2: 16,
                 3: 32,
                 4: 64}

    dataset = torchvision.datasets.ImageFolder(root="data", transform=transforms.Compose([transforms.ToTensor()]))
    dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=False)

    if scale >= 3:
        processed_data = torch.empty([data_size, 108, img_sizes[scale], img_sizes[scale]], dtype=torch.uint8)
    else:
        processed_data = torch.empty([data_size, 108, img_sizes[scale], img_sizes[scale]], dtype=torch.float32)
        normalize_t = torch.Tensor([0.5])

    for batch_index, (real_images, _) in enumerate(dataloader, 0):
        real_images = convert_to_img_plane((real_images * 255).type(torch.ByteTensor), batch_size)
        real_images = resize_batch(real_images, img_sizes[scale], batch_size)

        if scale >= 3:
            real_images = real_images * 255
            real_images = torch.from_numpy(real_images.transpose((0, 3, 1, 2))).type(torch.ByteTensor)
        else:
            real_images = (torch.from_numpy(real_images.transpose((0, 3, 1, 2))).type(torch.FloatTensor) - normalize_t) / normalize_t

        processed_data[batch_index*batch_size:(batch_index+1)*batch_size] = real_images

        if (batch_index + 1) % 10 == 0:
            print(f"{datetime.now()} {(batch_index+1)*batch_size}")
        if (batch_index+1)*batch_size >= data_size:
            break
    return processed_data

def animate_img_cube(input_img_cube, anim_file, training_outputs=False):
    frames = []
    for i in range(4*9):
        img_cube = input_img_cube[int((input_img_cube.shape[0]-1)*(i/35))][0] if training_outputs else input_img_cube
        append_img = ((img_cube[:,:,i*3:(i+1)*3]+1.0)*0.5*255).astype(np.uint8)
        append_img = cv2.resize(append_img, dsize=(0,0), fx=4.0, fy=4.0, interpolation=cv2.INTER_NEAREST)
        frames.append(append_img)

    imageio.mimsave(anim_file, frames, 'GIF', fps=8)
    return anim_file

def animate_img_batch(img_batch, anim_file, get_img_cubes=True, training_outputs=False):
    if get_img_cubes:
        cube_0 = get_img_cube(img_batch, 0)
        cube_1 = get_img_cube(img_batch, 1)
        cube_2 = get_img_cube(img_batch, 2)
        cube_3 = get_img_cube(img_batch, 3)
    else:
        cube_0 = img_batch[0]
        cube_1 = img_batch[1]
        cube_2 = img_batch[2]
        cube_3 = img_batch[3]

    frames = []
    for i in range(4*9):
        if training_outputs:
            cube_0 = img_batch[int((img_batch.shape[0]-1)*(i/35))][0]
            cube_1 = img_batch[int((img_batch.shape[0]-1)*(i/35))][1]
            cube_2 = img_batch[int((img_batch.shape[0]-1)*(i/35))][2]
            cube_3 = img_batch[int((img_batch.shape[0]-1)*(i/35))][3]

        append_img = ((cube_0[:,:,i*3:(i+1)*3]+1.0)*0.5*255).astype(np.uint8)
        append_img = cv2.resize(append_img, dsize=(0,0), fx=2.0, fy=2.0, interpolation=cv2.INTER_NEAREST)
        top_img = append_img
        append_img = ((cube_1[:,:,i*3:(i+1)*3]+1.0)*0.5*255).astype(np.uint8)
        append_img = cv2.resize(append_img, dsize=(0,0), fx=2.0, fy=2.0, interpolation=cv2.INTER_NEAREST)
        top_img = np.hstack((top_img, append_img))

        append_img = ((cube_2[:,:,i*3:(i+1)*3]+1.0)*0.5*255).astype(np.uint8)
        append_img = cv2.resize(append_img, dsize=(0,0), fx=2.0, fy=2.0, interpolation=cv2.INTER_NEAREST)
        bot_img = append_img
        append_img = ((cube_3[:,:,i*3:(i+1)*3]+1.0)*0.5*255).astype(np.uint8)
        append_img = cv2.resize(append_img, dsize=(0,0), fx=2.0, fy=2.0, interpolation=cv2.INTER_NEAREST)
        bot_img = np.hstack((bot_img, append_img))

        combined_img = np.vstack((top_img, bot_img))
        frames.append(combined_img)

    imageio.mimsave(anim_file, frames, 'GIF', fps=8)
    return anim_file

def get_sample_frames(anim_frames, from_gif=False, multiple=False, dim=64):
    if from_gif:
        if multiple:
            return np.hstack((np.vstack((anim_frames[0][:dim,:dim], anim_frames[0][:dim,dim:dim+dim], anim_frames[0][dim:dim+dim,:dim], anim_frames[0][dim:dim+dim,dim:dim+dim])),
                              np.vstack((anim_frames[3][:dim,:dim], anim_frames[3][:dim,dim:dim+dim], anim_frames[3][dim:dim+dim,:dim], anim_frames[3][dim:dim+dim,dim:dim+dim])),
                              np.vstack((anim_frames[9][:dim,:dim], anim_frames[9][:dim,dim:dim+dim], anim_frames[9][dim:dim+dim,:dim], anim_frames[9][dim:dim+dim,dim:dim+dim])),
                              np.vstack((anim_frames[10][:dim,:dim], anim_frames[10][:dim,dim:dim+dim], anim_frames[10][dim:dim+dim,:dim], anim_frames[10][dim:dim+dim,dim:dim+dim])),
                              np.vstack((anim_frames[18][:dim,:dim], anim_frames[18][:dim,dim:dim+dim], anim_frames[18][dim:dim+dim,:dim], anim_frames[18][dim:dim+dim,dim:dim+dim])),
                              np.vstack((anim_frames[19][:dim,:dim], anim_frames[19][:dim,dim:dim+dim], anim_frames[19][dim:dim+dim,:dim], anim_frames[19][dim:dim+dim,dim:dim+dim])),
                              np.vstack((anim_frames[27][:dim,:dim], anim_frames[27][:dim,dim:dim+dim], anim_frames[27][dim:dim+dim,:dim], anim_frames[27][dim:dim+dim,dim:dim+dim])),
                              np.vstack((anim_frames[30][:dim,:dim], anim_frames[30][:dim,dim:dim+dim], anim_frames[30][dim:dim+dim,:dim], anim_frames[30][dim:dim+dim,dim:dim+dim]))))
        return np.hstack((anim_frames[0], anim_frames[3], anim_frames[9], anim_frames[10], anim_frames[18], anim_frames[19], anim_frames[27], anim_frames[30]))
    assert multiple == False
    anim_frames = anim_frames.numpy().transpose(1, 2, 0)
    return np.hstack((np.hstack((anim_frames[dim*0:dim*(0+1),dim*0:dim*(0+1)], anim_frames[dim*0:dim*(0+1),dim*3:dim*(3+1)])),
                      np.hstack((anim_frames[dim*1:dim*(1+1),dim*0:dim*(0+1)], anim_frames[dim*1:dim*(1+1),dim*1:dim*(1+1)])),
                      np.hstack((anim_frames[dim*2:dim*(2+1),dim*0:dim*(0+1)], anim_frames[dim*2:dim*(2+1),dim*1:dim*(1+1)])),
                      np.hstack((anim_frames[dim*3:dim*(3+1),dim*0:dim*(0+1)], anim_frames[dim*3:dim*(3+1),dim*3:dim*(3+1)]))))

max_stars_repo_name: suleymanaslan/animation-gan

max_issues_repo_name: suleymanaslan/animation-gan

max_forks_repo_name: suleymanaslan/animation-gan


text:
"""
Run parameters - can be in separate file
"""
# Laser parameters
laser_waist = 5.e-6  # The waist of the laser (in meters)
laser_duration = 15.e-15  # The duration of the laser (in seconds)
laser_a0 = 4. # Amplitude of the normalized vector potential

"""
Physics part - can be in separate file
"""
import numpy as np

# This should be the only line that needs to be changed for different codes
from plasmacode import picmi

# Define the laser profile laser
laser = picmi.GaussianLaser(
            a0 = laser_a0,
            wavelength = 0.8e-6,  # The wavelength of the laser (in meters)
            waist = laser_waist,  # The waist of the laser (in meters)
            duration = laser_duration,  # The duration of the laser (in seconds)
            polarization_angle = np.pi/2,  # The main polarization vector
            focal_position = [0., 0., 100.e-6],  # Focal position (m)
            centroid_position = [0., 0., 0.],  # Position of the laser centroid at t=0 (m)
            propagation_direction = [0., 0., 1.])

# Define the electron beam
beam_dist = picmi.GaussianBunchDistribution(
                n_physical_particles = 1.e9,
                rms_bunch_size = [2.e-6, 2.e-6, 1.e-6],
                rms_velocity = [0.,0.,10.],
                centroid_position = [0.,0.,100.e-6],
                centroid_velocity = [0.,0.,1000.],
                velocity_divergence = [0.,0.,0.])

beam = picmi.Species(
            particle_type = 'electron',
            initial_distribution = beam_dist)

# Define plasma
plasma_dist = picmi.AnalyticDistribution(
                density_expression = "1.e23*tanh((z - 20.e-6)/100.e-6)",
                fill_in=True)

plasma = picmi.MultiSpecies(
                particle_types = ['He', 'Ar', 'electron'],
                names = ['He+', 'Argon', 'e-'],
                charge_states = [1, 5, None],
                proportions = [0.2, 0.8, 0.2 + 5*0.8],
                initial_distribution=plasma_dist)

# Individual species in a `MultiSpecies` can be addressed either
# with their index (using Python indexing conventions) or with their name
# (if the user provided a name)
# Set the ionization for the species number 1 (Argon)
# and place the created electrons into the species number 2 (electron)
plasma['Argon'].activate_ionization(model="ADK", target_species=plasma['e-'])

"""
Numerics part - can be in separate file
"""
import numpy as np
from scipy.constants import c
from plasmacode import picmi

# Define the grid
nx = 64
ny = 64
nz = 480
xmin = -30.e-6
xmax = +30.e-6
ymin = -30.e-6
ymax = +30.e-6
zmin = -56.e-6
zmax = +12.e-6
v_window = (0., 0., c)

# Setup the grid ; this may be code dependent
# Note that code specific arguments use the code name as a prefix.
if picmi.codename == 'plasmacode':
    nr = nx/2.
    grid = picmi.CylindricalGrid(
        nr=nr, rmin=0., rmax=xmax, bc_rmax='reflective',
        nz=nz, zmin=zmin, zmax=zmax, bc_zmin='open', bc_zmax='open',
        n_azimuthal_modes=2,
        moving_window_velocity=v_window,
        plasmacode_mode_phase = 0.) # "mode_phase" is a specific argument for the code plasmacode
elif picmi.codename in ['plasmacode3D', 'AMRplasmacode']:
    grid = picmi.Cartesian3DGrid(
        nx=nx, xmin=xmin, xmax=xmax, bc_xmin='periodic', bc_xmax='periodic',
        ny=ny, ymin=ymin, ymax=ymax, bc_ymin='periodic', bc_ymax='periodic',
        nz=nz, zmin=zmin, zmax=zmax, bc_zmin='open', bc_zmax='open',
        moving_window_velocity=v_window,
        AMRplasmacode_max_grid_size=32, AMRplasmacode_max_level=0) # "max_grid" and "max_level" are specific arguments for the code AMRplasmacode

# Setup the electromagnetic solver
smoother = picmi.BinomialSmoother(n_pass=2, compensation=True)
solver = picmi.ElectromagneticSolver(grid=grid, cfl=1.0,
                                     source_smoother=smoother)

# Initialize the simulation object
# Note that the time step size is obtained from the solver
sim = picmi.Simulation(
        solver=solver)

# Inject the laser through an antenna
antenna = picmi.LaserAntenna(
                position = (0, 0, 9.e-6),
                normal_vector = (0, 0, 1.))
sim.add_laser(laser, injection_method = antenna)

# Add the plasma: continuously injected by the moving window
if picmi.codename == 'plasmacode':
    n_macroparticle_per_cell = {'r':2, 'z':2, 'theta':4}
else:
    n_macroparticle_per_cell = {'x':2, 'y':2, 'z':2}

plasma_layout = picmi.GriddedLayout(
                    grid = grid,
                    n_macroparticle_per_cell = n_macroparticle_per_cell)
sim.add_species(species=plasma, layout=plasma_layout)
# For Python-driven codes: macroparticles are created at this point

# Add the beam
beam_layout = picmi.PseudoRandomLayout(
                n_macroparticles = 10**5,
                seed = 0)
sim.add_species(species=beam, layout=beam_layout, initialize_self_field=True)

# Add field and particle diagnostics
field_diag_slicexz = picmi.FieldDiagnostic(grid = grid,
                                           data_list = ['E', 'rho'],
                                           number_of_cells = [128, 1, 64],
                                           lower_bound = [-1.e-6, 0.e-6, -10.e-6],
                                           upper_bound = [ 1.e-6, 0.e-6, 10.e-6],
                                           period = 100)
sim.add_diagnostic(field_diag_slicexz)

particle_diag_beam = picmi.ParticleDiagnostic(grid = grid,
                                              species = [beam],
                                              period = 100)
sim.add_diagnostic(particle_diag_beam)

"""
picmi input script
"""
import numpy as np
from plasmacode import picmi

run_python_simulation = True

if run_python_simulation:
    sim.step(1000)
else:
    sim.set_max_step(1000)
    sim.write_input_file(file_name='input_script')

max_stars_repo_name: hvincenti/picmi

max_issues_repo_name: hvincenti/picmi

max_forks_repo_name: hvincenti/picmi

reason: import numpy,from scipy


text:
from dataclasses import dataclass
from typing import Tuple, Dict
import numpy
import math
import cv2

IntPos = Tuple[int, int]
FloatPos = Tuple[float, float]

Marker = Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray]

def distance(a, b):
    return math.sqrt(sum(map(lambda x: x ** 2, a - b)))

@dataclass
class ComputeResult:
    points: list[(int, int)]
    mm_width: float
    mm_height: float
    width: int
    height: int

def compute(
    markers: numpy.ndarray,
    marker_size_mm: float,
    offset_mm: float,
) -> ComputeResult:
    offset_scale = offset_mm / marker_size_mm
    top_left = markers[0][0] - (markers[0][2] - markers[0][0]) * offset_scale
    top_right = markers[1][1] - (markers[1][3] - markers[1][1]) * offset_scale
    bottom_right = markers[2][2] - (markers[2][0] - markers[2][2]) * offset_scale
    bottom_left = markers[3][3] - (markers[3][1] - markers[3][3]) * offset_scale
    # cv2.circle(image, tuple(map(int, markers[0][0])), 4, (0, 0, 255), -1)
    # cv2.circle(image, tuple(map(int, top_left)), 4, (255, 0, 255), -1)
    # cv2.circle(image, tuple(map(int, markers[1][1])), 4, (0, 0, 255), -1)
    # cv2.circle(image, tuple(map(int, top_right)), 4, (255, 0, 255), -1)
    # cv2.circle(image, tuple(map(int, markers[2][2])), 4, (0, 0, 255), -1)
    # cv2.circle(image, tuple(map(int, bottom_right)), 4, (255, 0, 255), -1)
    # cv2.circle(image, tuple(map(int, markers[3][3])), 4, (0, 0, 255), -1)
    # cv2.circle(image, tuple(map(int, bottom_left)), 4, (255, 0, 255), -1)
    pixels_width_top = distance(top_right, top_left)
    pixels_width_bottom = distance(bottom_right, bottom_left)
    pixels_height_left = distance(bottom_left, top_left)
    pixels_height_right = distance(bottom_right, top_right)
    marker_pixels_width = (distance(markers[0][1], markers[0][0]) + distance(markers[1][1], markers[1][0])) / 2
    marker_pixels_height = (distance(markers[0][3], markers[0][0]) + distance(markers[3][3], markers[3][0])) / 2
    mm_width = pixels_width_top / marker_pixels_width * marker_size_mm
    mm_height = pixels_height_left / marker_pixels_height * marker_size_mm
    # cv2.circle(image, tuple(map(int, top_left)), int(pixels_width_right), (0, 0, 0), 4)
    # cv2.circle(image, tuple(map(int, top_left)), int(pixels_height_left), (0, 0, 0), 4)
    ratio = mm_width / mm_height
    width = max(max(pixels_width_top, pixels_width_bottom), max(pixels_height_left, pixels_height_right) * ratio)
    height = width / ratio
    return ComputeResult(
        points=list(map(lambda x: x.tolist(), [top_left, top_right, bottom_right, bottom_left])),
        mm_width=mm_width,
        mm_height=mm_height,
        width=int(width),
        height=int(height),
    )

arucoDict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_ARUCO_ORIGINAL)
arucoParams = cv2.aruco.DetectorParameters_create()

def analyse_img(frame, target_ids: list[Tuple[int, int, int, int]]) -> Dict[int, ComputeResult]:
    (corners, ids, rejected) = cv2.aruco.detectMarkers(
        frame,
        arucoDict,
        parameters=arucoParams
    )
    results = {}
    # cv2.aruco.drawDetectedMarkers(frame, corners, ids)
    if len(corners) > 0:
        print(ids.flatten())
        marker_dict = dict(zip(ids.flatten(), corners))
        for i, current_ids in enumerate(target_ids):
            if all(my_id in marker_dict for my_id in current_ids):
                my_corners = list(marker_dict[my_id] for my_id in current_ids)
                results[i] = compute(numpy.reshape(my_corners, (4, 4, 2)), 40, 10)
    return results

max_stars_repo_name: dominik-korsa/board-camera

max_issues_repo_name: dominik-korsa/board-camera

max_forks_repo_name: dominik-korsa/board-camera


text:
# Regressions tests on result types of some signal functions

import numpy as np
from numpy.testing import assert_

from scipy.signal import (decimate,
                          lfilter_zi,
                          lfiltic,
                          sos2tf,
                          sosfilt_zi)

def test_decimate():
    ones_f32 = np.ones(32, dtype=np.float32)
    assert_(decimate(ones_f32, 2).dtype == np.float32)

    ones_i64 = np.ones(32, dtype=np.int64)
    assert_(decimate(ones_i64, 2).dtype == np.float64)

def test_lfilter_zi():
    b_f32 = np.array([1, 2, 3], dtype=np.float32)
    a_f32 = np.array([4, 5, 6], dtype=np.float32)
    assert_(lfilter_zi(b_f32, a_f32).dtype == np.float32)

def test_lfiltic():
    # this would return f32 when given a mix of f32 / f64 args
    b_f32 = np.array([1, 2, 3], dtype=np.float32)
    a_f32 = np.array([4, 5, 6], dtype=np.float32)
    x_f32 = np.ones(32, dtype=np.float32)

    b_f64 = b_f32.astype(np.float64)
    a_f64 = a_f32.astype(np.float64)
    x_f64 = x_f32.astype(np.float64)

    assert_(lfiltic(b_f64, a_f32, x_f32).dtype == np.float64)
    assert_(lfiltic(b_f32, a_f64, x_f32).dtype == np.float64)
    assert_(lfiltic(b_f32, a_f32, x_f64).dtype == np.float64)
    assert_(lfiltic(b_f32, a_f32, x_f32, x_f64).dtype == np.float64)

def test_sos2tf():
    sos_f32 = np.array([[4, 5, 6, 1, 2, 3]], dtype=np.float32)
    b, a = sos2tf(sos_f32)
    assert_(b.dtype == np.float32)
    assert_(a.dtype == np.float32)

def test_sosfilt_zi():
    sos_f32 = np.array([[4, 5, 6, 1, 2, 3]], dtype=np.float32)
    assert_(sosfilt_zi(sos_f32).dtype == np.float32)

max_stars_repo_name: lorentzenchr/scipy

max_issues_repo_name: lorentzenchr/scipy

max_forks_repo_name: lorentzenchr/scipy

reason: import numpy,from numpy,from scipy


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

# In[1]:

import math
import numpy as np
import pandas as pd
import h5py
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import PIL
import tensorflow as tf
from tensorflow.python.framework import ops
import scipy
from scipy import ndimage
from tqdm import tqdm
from sklearn.model_selection import train_test_split
import os
import cv2
import time

os.environ['KMP_DUPLICATE_LIB_OK']='True'

train_set = pd.read_csv(r"C:\Users\barbayrak\Desktop\The_Picnic_Hackathon_2019/train.tsv",header=0 , sep='\t')

IMAGE_DIRECTORY = './train/'
IMAGE_SIZE = 64

def tf_read_and_resize_images(imageNames,Y_labels):
    X_data = []
    Y = []
    tf.reset_default_graph()

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session(config = config) as sess:
        sess.run(tf.global_variables_initializer())
        # Each image is resized individually as different image may be of different size.
        counter = 0
        for img_name in tqdm(imageNames):
            try:
                img = mpimg.imread(IMAGE_DIRECTORY + img_name)
                if img_name.endswith("png"):
                    img = img[:,:,:3]
                elif img_name.endswith("jpeg"):
                    img = img.astype(np.float32)/255.0
                elif img_name.endswith("jpg"):
                    img = img.astype(np.float32)/255.0
                resized_img = cv2.resize(img, dsize=(IMAGE_SIZE, IMAGE_SIZE), interpolation=cv2.INTER_NEAREST)[:,:,:3]
                if(resized_img.shape[0] >= IMAGE_SIZE):
                    X_data.append(resized_img)
                    Y.append(Y_labels[counter])
            except Exception as e:
                print(e)
                pass
            counter = counter + 1
    X_data = np.asarray(X_data, dtype = np.float32) # Convert to numpy
    Y_data = np.array(Y)
    return X_data,Y_data

def central_scale_images(X_imgs,Y_labels, scales):
    # Various settings needed for Tensorflow operation
    boxes = np.zeros((len(scales), 4), dtype = np.float32)
    for index, scale in enumerate(scales):
        x1 = y1 = 0.5 - 0.5 * scale # To scale centrally
        x2 = y2 = 0.5 + 0.5 * scale
        boxes[index] = np.array([y1, x1, y2, x2], dtype = np.float32)
    box_ind = np.zeros((len(scales)), dtype = np.int32)
    crop_size = np.array([IMAGE_SIZE, IMAGE_SIZE], dtype = np.int32)

    X_scale_data = []
    Y_scaled_labels = []
    tf.reset_default_graph()
    X = tf.placeholder(tf.float32, shape = (1, IMAGE_SIZE, IMAGE_SIZE, 3))
    # Define Tensorflow operation for all scales but only one base image at a time
    tf_img = tf.image.crop_and_resize(X, boxes, box_ind, crop_size)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        counter = 0
        for img_data in tqdm(X_imgs):
            batch_img = np.expand_dims(img_data, axis = 0)
            scaled_imgs = sess.run(tf_img, feed_dict = {X: batch_img})
            X_scale_data.extend(scaled_imgs)
            for i in range(len(scales)):
                Y_scaled_labels.append(Y_labels[counter])
            counter += 1

    X_scale_data = np.array(X_scale_data, dtype = np.float32)
    Y_scaled_labels = np.array(Y_scaled_labels)
    return X_scale_data,Y_scaled_labels

def rotate_images(X_imgs,Y_labels):
    X_rotate = []
    Y_rotate_labels = []
    tf.reset_default_graph()
    X = tf.placeholder(tf.float32, shape = (IMAGE_SIZE, IMAGE_SIZE, 3))
    k = tf.placeholder(tf.int32)
    tf_img = tf.image.rot90(X, k = k)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        counter = 0
        for img in X_imgs:
            for i in range(3):  # Rotation at 90, 180 and 270 degrees
                rotated_img = sess.run(tf_img, feed_dict = {X: img, k: i + 1})
                X_rotate.append(rotated_img)
                Y_rotate_labels.append(Y_labels[counter])
            counter += 1

    X_rotate = np.array(X_rotate, dtype = np.float32)
    Y_rotate_labels = np.array(Y_rotate_labels)
    return X_rotate,Y_rotate_labels

def flip_images(X_imgs,Y_labels):
    X_flip = []
    Y_fliped_labels = []
    tf.reset_default_graph()
    X = tf.placeholder(tf.float32, shape = (IMAGE_SIZE, IMAGE_SIZE, 3))
    tf_img1 = tf.image.flip_left_right(X)
    tf_img2 = tf.image.flip_up_down(X)
    tf_img3 = tf.image.transpose_image(X)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        counter = 0
        for img in X_imgs:
            flipped_imgs = sess.run([tf_img1, tf_img2, tf_img3], feed_dict = {X: img})
            X_flip.extend(flipped_imgs)
            Y_fliped_labels.extend([Y_labels[counter],Y_labels[counter],Y_labels[counter]])
            counter += 1
    X_flip = np.array(X_flip, dtype = np.float32)
    Y_fliped_labels = np.array(Y_fliped_labels)
    return X_flip,Y_fliped_labels

def add_gaussian_noise(X_imgs,Y_labels):
    gaussian_noise_imgs = []
    Y_gaussian_labels = []
    row, col, _ = X_imgs[0].shape
    # Gaussian distribution parameters
    mean = 0
    var = 0.1
    sigma = var ** 0.5

    counter = 0
    for X_img in X_imgs:
        gaussian = np.random.random((row, col, 1)).astype(np.float32)
        gaussian = np.concatenate((gaussian, gaussian, gaussian), axis = 2)
        gaussian_img = cv2.addWeighted(X_img, 0.75, 0.25 * gaussian, 0.25, 0)
        gaussian_noise_imgs.append(gaussian_img)
        Y_gaussian_labels.append(Y_labels[counter])
        counter += 1
    gaussian_noise_imgs = np.array(gaussian_noise_imgs, dtype = np.float32)
    Y_gaussian_labels = np.array(Y_gaussian_labels)
    return gaussian_noise_imgs,Y_gaussian_labels

######################################################################
IMAGE_SIZE = 64
#imgNames = train_set.iloc[:,0].values
#Y = train_set.iloc[:,1].values

#print(imgNames)
#X_resized_images,Y_resized_labels = tf_read_and_resize_images(imgNames[:100],Y[:100])
#print(X_resized_images.shape)
#print(Y_resized_labels.shape)
#np.save('X_resized_images_train.npy', X_resized_images[:100])
#np.save('Y_resized_labels_train.npy',Y_resized_labels[:100])

X_resized_images = np.load('X_resized_images_train.npy')
Y_resized_labels = np.load('Y_resized_labels_train.npy')

X_train,X_test,Y_train,Y_test = train_test_split(X_resized_images,Y_resized_labels, test_size = 0.15,shuffle=True)

np.save('X_train_resized.npy',X_train)
np.save('Y_train_resized.npy',Y_train)
np.save('X_test.npy',X_test)
np.save('Y_test.npy',Y_test)

X_scaled_images,Y_scaled_labels = central_scale_images(X_train,Y_train, [0.90, 0.75, 0.60])
X_rotated_images,Y_rotated_labels = rotate_images(X_train,Y_train)
X_fliped_images,Y_fliped_labels = flip_images(X_train,Y_train)
X_gaussian_images,Y_gaussian_labels = add_gaussian_noise(X_train,Y_train)

X_augmented_images = np.concatenate((X_train,X_scaled_images) , axis = 0)
X_augmented_images = np.concatenate((X_augmented_images,X_rotated_images) , axis = 0)
X_augmented_images = np.concatenate((X_augmented_images,X_fliped_images) , axis = 0)
X_augmented_images = np.concatenate((X_augmented_images,X_gaussian_images) , axis = 0)

print(X_augmented_images.shape)

np.save('X_train.npy',X_augmented_images)

Y_augmented_labels = np.concatenate((Y_train,Y_scaled_labels) , axis = 0)
Y_augmented_labels = np.concatenate((Y_augmented_labels,Y_rotated_labels) , axis = 0)
Y_augmented_labels = np.concatenate((Y_augmented_labels,Y_fliped_labels) , axis = 0)
Y_augmented_labels = np.concatenate((Y_augmented_labels,Y_gaussian_labels) , axis = 0)

print(Y_augmented_labels.shape)

np.save('Y_train.npy',Y_augmented_labels)

#imgNames = train_set.iloc[:,0].values
#Y = train_set.iloc[:,1].values

#X_resized_images = tf_read_and_resize_images(imgNames)
#X_fliped_images,Y_fliped_labels = flip_images(X_resized_images,Y)

#X_augmented_images = np.concatenate((X_resized_images,X_fliped_images) , axis = 0)
#Y_augmented_labels = np.concatenate((Y,Y_fliped_labels) , axis = 0)

#np.save('X_augmented_images_train.npy',X_resized_images)
#np.save('Y_augmented_labels_train.npy',Y)

margin=50 # pixels
spacing =35 # pixels
dpi=100. # dots per inch
width = (200+200+2*margin+spacing)/dpi # inches
height= (180+180+2*margin+spacing)/dpi
left = margin/dpi/width #axes ratio
bottom = margin/dpi/height
wspace = spacing/float(200)
fig, axes  = plt.subplots(2,2, figsize=(width,height), dpi=dpi)
fig.subplots_adjust(left=left, bottom=bottom, right=1.-left, top=1.-bottom,
                    wspace=wspace, hspace=wspace)
for ax, im, name in zip(axes.flatten(),X_augmented_images[47856:,:,:,:], list("ABCD")):
    ax.axis('off')
    ax.set_title('restored {}'.format(name))
    ax.imshow(im)
plt.show()

margin=50 # pixels
spacing =35 # pixels
dpi=100. # dots per inch
width = (200+200+2*margin+spacing)/dpi # inches
height= (180+180+2*margin+spacing)/dpi
left = margin/dpi/width #axes ratio
bottom = margin/dpi/height
wspace = spacing/float(200)
fig, axes  = plt.subplots(2,2, figsize=(width,height), dpi=dpi)
fig.subplots_adjust(left=left, bottom=bottom, right=1.-left, top=1.-bottom,
                    wspace=wspace, hspace=wspace)
for ax, im, name in zip(axes.flatten(),X_test[3:,:,:,:], list("ABCD")):
    ax.axis('off')
    ax.set_title('restored {}'.format(name))
    ax.imshow(im)
plt.show()

max_stars_repo_name: barbayrak/PicnicHackathon

max_issues_repo_name: barbayrak/PicnicHackathon

max_forks_repo_name: barbayrak/PicnicHackathon

reason: import numpy,import scipy,from scipy


text:
import numpy as np

import gym
from gym import Wrapper
from gym.spaces import Discrete, Box

class MuJoCoHistoryEnv(Wrapper):

    """
    takes observations from an environment and stacks to history given hist_len of history length
    """

    def __init__(self, env_id, hist_len=4, history_type='pomdp'):
        """
        Parameters
        ----------
        env_id - id of registered gym environment
        history_type - * pomdp: remove specified dimensions, meaning to return history of remaining dimensions
                       * mixed_full_pomdp: [flag=0] + [full observation] + history of [pomdp observation (see above)]
                       * history_full: history of [flag=1] + [full observation]
                       * history_pomdp: history of [flag=0] + [pomdp observation (replaced by zeros)]
                       * history_ac_full: history of [flag=1] + [full observation] + [action]
                       * history_ac_pomdp: history of [flag=0] + [pomdp observation (replaced by zeros)] + [action]
        hist_len - length of the history (hist_len==0 is without history, just current observation)
        """
        env = gym.make(env_id)
        super(MuJoCoHistoryEnv, self).__init__(env)
        self._wrapped_env = env
        self.hist_len = hist_len
        self.hist_type = history_type
        self.history = None
        self.full_obs_dim = self._wrapped_env.observation_space.shape[0]
        self.ac_dim = self._wrapped_env.action_space.shape[0]

        if self.hist_len <= 0:
            raise ValueError("History length must be greater or equals zero!")

        # specify ob dimensions which are inactive in POMDP versions
        if env_id == "Ant-v2":
            self.remove_dim_start = 57
            self.remove_dim_end = 110
        elif env_id == "Hopper-v2":
            # angle of thigh + leg + foot, velocity of x + z
            self.remove_dim_start = 2
            self.remove_dim_end = 6
        elif env_id == "HalfCheetah-v2":
            # angle of bthigh + bshin + bfoot + fthigh + fshin + ffoot, velocity of x + y
            self.remove_dim_start = 2
            self.remove_dim_end = 9
        elif env_id == "Humanoid-v2":
            # angle of left hip(x,y,z) + knee
            self.remove_dim_start = 6
            self.remove_dim_end = 15
        elif env_id == "InvertedDoublePendulum-v2":
            # angle of pole1 + pole2, y pos of pole1 + pole2
            self.remove_dim_start = 0
            self.remove_dim_end = 3
        elif env_id == "Reacher-v2":
            # target x and y pos, angular velocity both joints, dist fingertip <-> target
            self.remove_dim_start = 4
            self.remove_dim_end = 10
        elif env_id == "Swimmer-v2":
            # angle of mid + back, velocity of x + y, angular velocity of z
            self.remove_dim_start = 1
            self.remove_dim_end = 5
        elif env_id == "Walker2d-v2":
            # velocity of x + y, angular velocity of z + right thigh + leg + foot
            self.remove_dim_start = 8
            self.remove_dim_end = 13
        elif self.hist_type == "history_full" or self.hist_type == "history_ac_full":
            self.remove_dim_start = None
            self.remove_dim_end = None
        else:
            raise NotImplementedError("POMDP version not implemented for "+env_id)
        if self.hist_type != "history_full" and self.hist_type != "history_ac_full":
            if self.remove_dim_end < self.remove_dim_start:
                raise ValueError("revome_dim_end < remove_dim_start")
            self.remove_dim = self.remove_dim_end - self.remove_dim_start + 1

        # specify observation space and arrangement according to selected history type
        if self.hist_type == "pomdp":
            self.historyIgnoreIdx = 0
            self.genObservation = self.generateObservationPomdp
            self.total_obs_dim = self.full_obs_dim-self.remove_dim
            self.ob_space_dim = (self.full_obs_dim-self.remove_dim)*(self.hist_len+1)
        elif self.hist_type == "mixed_full_pomdp":
            self.historyIgnoreIdx = 1 + self.full_obs_dim
            self.genObservation = self.generateObservationMixed
            self.total_obs_dim = self.historyIgnoreIdx + (self.full_obs_dim-self.remove_dim)
            self.ob_space_dim = self.historyIgnoreIdx + (self.full_obs_dim-self.remove_dim)*(self.hist_len+1)
        elif self.hist_type == "history_full":
            self.historyIgnoreIdx = 0
            self.genObservation = self.generateObservationHistoryFull
            self.total_obs_dim = 1+self.full_obs_dim
            self.ob_space_dim = self.total_obs_dim*(self.hist_len+1)
        elif self.hist_type == "history_pomdp":
            self.historyIgnoreIdx = 0
            self.genObservation = self.generateObservationHistoryPomdp
            self.total_obs_dim = 1+self.full_obs_dim
            self.ob_space_dim = self.total_obs_dim*(self.hist_len+1)
        elif self.hist_type == "history_ac_full":
            self.historyIgnoreIdx = 0
            self.genObservation = self.generateObservationHistoryActionFull
            self.total_obs_dim = 1+self.full_obs_dim+self.ac_dim
            self.ob_space_dim = self.total_obs_dim*(self.hist_len+1)
        elif self.hist_type == "history_ac_pomdp":
            self.historyIgnoreIdx = 0
            self.genObservation = self.generateObservationHistoryActionPomdp
            self.total_obs_dim = 1+self.full_obs_dim+self.ac_dim
            self.ob_space_dim = self.total_obs_dim*(self.hist_len+1)
        else:
            raise NameError("error: wrong history type: " + self.hist_type)
        #self.total_obs_dim = self.historyIgnoreIdx + (self.full_obs_dim-self.remove_dim)
        self.observation_space = Box(low=-float('inf'), high=float('inf'), shape=(self.ob_space_dim,))

        self.observation_dim_hist_part = self.total_obs_dim - self.historyIgnoreIdx
        print('-------- History Info: --------')
        print('total obs dim:', self.total_obs_dim)
        print('original obs dim:', self.full_obs_dim)
        print('history obs dim:', self.observation_dim_hist_part)
        print('-------------------------------')

    def reset_history(self, new_):
        self.history = np.zeros((self.observation_space.shape[0]-self.historyIgnoreIdx, ))
        self.history[0:self.observation_dim_hist_part] = new_[self.historyIgnoreIdx:]

    def add_to_history(self, new_):
        self.history[self.observation_dim_hist_part:] = self.history[:-self.observation_dim_hist_part]
        self.history[0:self.observation_dim_hist_part] = new_[self.historyIgnoreIdx:]

    def reset(self):
        obs = self._wrapped_env.reset()
        new_ob = self.genObservation(obs, np.zeros(self.ac_dim))
        self.reset_history(new_ob)
        # we return copy so that we can modify the history without changing already returned histories
        return np.concatenate([new_ob[0:self.historyIgnoreIdx],self.history])

    def step(self, action):
        next_obs, reward, done, info = self._wrapped_env.step(action)
        ob = self.genObservation(next_obs, action)
        self.add_to_history(ob)
        # we return copy so that we can modify the history without changing already returned histories
        return np.concatenate([ob[0:self.historyIgnoreIdx],self.history]), reward, done, info

    def generateObservationPomdp(self, ob, ac):
        return np.concatenate([ob[0:self.remove_dim_start], ob[self.remove_dim_end+1:]])

    def generateObservationMixed(self, ob, ac):
        # flag + full observation + history of pomdp observations
        flag = 1
        return np.concatenate([[flag], ob, self.generateObservationPomdp(ob, ac)])

    def generateObservationHistoryFull(self, ob, ac):
        # history of: flag + full obsservations
        flag = 1
        return np.concatenate([[flag], ob])

    def generateObservationHistoryPomdp(self, ob, ac):
        # history of: flag + pomdp obsservations
        flag = 0
        return np.concatenate([[flag], ob[0:self.remove_dim_start], np.zeros(self.remove_dim), ob[self.remove_dim_end+1:]])

    def generateObservationHistoryActionFull(self, ob, ac):
        # history of: flag + full obsservations + action
        flag = 1
        return np.concatenate([[flag], ob, ac])

    def generateObservationHistoryActionPomdp(self, ob, ac):
        # history of: flag + pomdp obsservations + action
        flag = 0
        return np.concatenate([[flag], ob[0:self.remove_dim_start], np.zeros(self.remove_dim), ob[self.remove_dim_end+1:], ac])

max_stars_repo_name: stweigand/gym-pomdp-wrappers

max_issues_repo_name: stweigand/gym-pomdp-wrappers

max_forks_repo_name: stweigand/gym-pomdp-wrappers


text:
#!/usr/bin/env python
"""
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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.

This program parses JMH benchmarks and graphs them.
"""

from collections import namedtuple
import json
import locale
from os import mkdir, path

from cycler import cycler
from enum import Enum
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats

# Make sure the output directory exists
output_directory = path.join('./performance_graphs/')
if not path.exists(output_directory):
    mkdir(output_directory)

# Named tuples and enum
Benchmark = namedtuple('Benchmark', ['filename', 'config_files'])
MeasurementPlot = namedtuple('MeasurementPlot', ['benchmark_name', 'configs', 'params', 'plot_options'])
RunParams = namedtuple('RunParams', ['rows', 'columns', 'keySize', 'valueSize'])
RunKey = namedtuple('RunKey', ['config_file', 'run_params'])
RunValue = namedtuple('RunValue', ['score', 'error'])
class GraphType(Enum):
    total_time = 1
    time_per_op = 2
    ops_per_time = 3

# Available benchmarks
benchmarks = {
    'Encryption (Read)': Benchmark('jmh-encryption-read.json', {
        'Accumulo': '',
        'Field-Level': 'encryption/encrypt-baseline.ini',
        'CEABAC': 'encryption/encrypt-value.ini',
        'CEABAC-Entry': 'encryption/encrypt-entry.ini',
        'Searchable': 'encryption/searchable.ini',
    }),
    'Encryption (Write)': Benchmark('jmh-encryption-write.json', {
        'Accumulo': '',
        'Field-Level': 'encryption/encrypt-baseline.ini',
        'CEABAC': 'encryption/encrypt-value.ini',
        'CEABAC-Entry': 'encryption/encrypt-entry.ini',
        'Searchable': 'encryption/searchable.ini',
    }),
    'Signature (Read)': Benchmark('jmh-signature-read.json', {
        'Accumulo': '',
        'Value': 'signature/read/value.ini',
        'Visibility': 'signature/read/column.ini',
        'Table': 'signature/read/table.ini',
        'RSA-PKCS1': 'signature/rsa-pkcs1.ini',
        'RSA-PSS': 'signature/read/value.ini',
        'DSA': 'signature/dsa.ini',
        'ECDSA': 'signature/ecdsa.ini'
    }),
    'Signature (Write)': Benchmark('jmh-signature-write.json', {
        'Accumulo': '',
        'Value': 'signature/write/value.ini',
        'Visibility': 'signature/write/column.ini',
        'Table': 'signature/write/table.ini',
        'RSA-PKCS1': 'signature/rsa-pkcs1.ini',
        'RSA-PSS': 'signature/rsa-pss.ini',
        'DSA': 'signature/dsa.ini',
        'ECDSA': 'signature/write/value.ini'
    })
}

# Options list and various configurations
default_options = {
    'title': None,                  # graph's (optional) title
    'x_label': '',                  # graph's X label
    'x_tick_labels': '',            # graph's X tick labels
    'y_label': 'Operations / sec',  # graph's Y label

    'confidence_interval': .95,     # confidence interval to calculate
    'mode': GraphType.ops_per_time, # mode to graph
    'scale': 1000.0,                # multiply each data point by scale
    'figure_size': [8.0, 3.0],      # size of the figure area in inches

    'number_format': '%.0f',        # number format for data point labels
    'number_rotation': 75,          # rotation of numbers
    'number_y_offset': 0,           # offset of the y value to the bars

    'legend': True,                 # whether to display a legend
    'legend_location': 'best',      # legend location
    'legend_bounding_box': None,    # legend bounding box
    'legend_ncol': 2,               # number of columns in the legend

    'filename': None                # name of the file to save
}

configs_encryption = ['Accumulo', 'Field-Level', 'CEABAC', 'CEABAC-Entry', 'Searchable']
configs_signature = ['Accumulo', 'Value', 'Visibility', 'Table']
configs_signature_modes = ['RSA-PKCS1', 'RSA-PSS', 'DSA', 'ECDSA']

params_set_data_size = [
    RunParams(rows='1000', columns='10',   keySize='10', valueSize='10'),
    RunParams(rows='1000', columns='10',   keySize='100', valueSize='10'),
    RunParams(rows='1000', columns='10',   keySize='10', valueSize='1000'),
    RunParams(rows='1000', columns='10',   keySize='100', valueSize='1000')
]
params_set_operations = [
    RunParams(rows='10',   columns='1',   keySize='100', valueSize='1000'),
    RunParams(rows='100',  columns='1',   keySize='100', valueSize='1000'),
    RunParams(rows='1000', columns='1',   keySize='100', valueSize='1000'),
    RunParams(rows='1000', columns='10',  keySize='100', valueSize='1000')
]

x_label_operations = 'Batch Size'
x_label_data_size = 'Data Size'

x_tick_labels_operations = ('10', '100', '1,000', '10,000')
x_tick_labels_data_size = ('key=10 bytes,\nvalue=10 bytes',
                           'key=10 bytes,\nvalue=1,000 bytes',
                           'key=100 bytes,\nvalue=10 bytes',
                           'key=100 bytes,\nvalue=1,000 bytes')

figure_size_normal = [8.0, 3.0]
figure_size_wide = [16.0, 3.0]

# Load runs
def load_runs(benchmark_name, configs, params, options):
    """Function to load a benchmark."""
    benchmark = benchmarks[benchmark_name]
    with open(benchmark.filename) as file_handle:
        contents = json.load(file_handle)

        included_config_files = [benchmark.config_files[name] for name in configs]
        runs = dict()

        for run in contents:
            config_file = run['params']['configFile']
            run_params = RunParams(
                run['params']['rowCount'],
                run['params']['columnCount'],
                run['params']['keyFieldSize'],
                run['params']['valueFieldSize'])

            # Only save runs to be plotted
            if not config_file in included_config_files:
                continue
            if not run_params in params:
                continue

            # Generate the metric appropriately
            ops = float(run_params.rows) * float(run_params.columns)
            data = np.array([score for scores in run['primaryMetric']['rawData'] for score in scores])

            mode = options['mode']
            if mode is GraphType.total_time:
                pass
            elif mode is GraphType.time_per_op:
                data = data / ops
            elif mode is GraphType.ops_per_time:
                data = ops / data
            else:
                raise 'invalid graph type'

            data = data * options['scale']

            mean = np.mean(data)
            standard_error = stats.sem(data)
            interval = standard_error * stats.t.ppf((1 + options['confidence_interval']) / 2, len(data) - 1)
            runs[RunKey(config_file, run_params)] = RunValue(mean, interval)

        return runs

# Create graphs
plt.style.use('ggplot')

locale.setlocale(locale.LC_ALL, 'en_US')

colors = []
for item in plt.rcParams['axes.prop_cycle']:
    colors.append(item['color'])
colors.pop(3)
colors.append('purple')
colors.append('orange')
plt.rc('axes', prop_cycle=cycler('color', colors))

def plot_measurements(benchmark_name, configs, params, plot_options, show_figure=False):
    """ Plot measurements. """
    options = default_options.copy()
    options.update(plot_options)
    runs = load_runs(benchmark_name, configs, params, options)

    # Create the bar chart
    fig, ax = plt.subplots(figsize=options['figure_size'])
    extra_artists = []

    bar_locs = np.arange(len(params)) * (len(configs) + 1) + 1
    bar_graphs = []

    for index, config in enumerate(configs):
        config_file = benchmarks[benchmark_name].config_files[config]
        means = []
        stds = []

        for run_params in params:
            run = runs[RunKey(config_file, run_params)]
            means.append(run.score)
            stds.append(run.error)

        bar_graph = ax.bar(bar_locs + index,
                        means,
                        color=colors[index % len(colors)],
                        yerr=stds,
                        error_kw=dict(ecolor='black', lw=.5, capsize=4, capthick=.5))
        bar_graphs.append(bar_graph)

    # Add some text for labels, title and axes ticks
    if options['title'] is not None:
        extra_artists.append(ax.set_title(options['title']))
    ax.set_xlabel(options['x_label'])
    ax.set_xticks(bar_locs + len(configs) / 2.)
    ax.set_xticklabels(options['x_tick_labels'])
    ax.set_ylabel(options['y_label'])

    if options['legend']:
        extra_artists.append(ax.legend(
            tuple([bar_graph[0] for bar_graph in bar_graphs]),
            tuple(configs),
            loc=options['legend_location'],
            bbox_to_anchor=options['legend_bounding_box'],
            ncol=options['legend_ncol']))

    for bar_graph in bar_graphs:
        for bar in bar_graph:
            height = bar.get_height()
            extra_artists.append(ax.text(
                bar.get_x() + bar.get_width() * 2./3.,
                height + options['number_y_offset'],
                locale.format(options['number_format'], height, grouping=True),
                ha='center',
                va='bottom',
                rotation=options['number_rotation']))

    # Show the plot
    if show_figure:
        plt.show()
    fig.savefig(path.join(output_directory, options['filename']),
                bbox_extra_artists=tuple(extra_artists),
                bbox_inches='tight')

def plot_legend(configs, filename, ncol=None):
    """Draw a legend by itself."""
    fig = plt.figure(figsize=(0.1, 0.1))
    patches = [mpatches.Patch(color=color, label=config) for config, color in zip(configs, colors)]
    legend = fig.legend(patches, configs, loc='center', ncol=ncol if ncol is not None else len(configs))
    fig.savefig(filename, bbox_extra_artists=(legend,), bbox_inches='tight')

# Plot legends.

# List all measurement plots and create them
plots = [
    MeasurementPlot('Encryption (Write)', configs_encryption, params_set_data_size, {
        'x_label': x_label_data_size,
        'x_tick_labels': x_tick_labels_data_size,
        'number_y_offset': 2500,
        'filename': 'encryption_write_data_size.pdf'
    }),
    MeasurementPlot('Encryption (Read)', configs_encryption, params_set_data_size, {
        'x_label': x_label_data_size,
        'x_tick_labels': x_tick_labels_data_size,
        'number_y_offset': 1000,
        'filename': 'encryption_read_data_size.pdf'
    }),
    MeasurementPlot('Encryption (Write)', configs_encryption, params_set_operations, {
        'x_label': x_label_operations,
        'x_tick_labels': x_tick_labels_operations,
        'number_y_offset': 250,
        'filename': 'encryption_write_operations.pdf'
    }),
    MeasurementPlot('Encryption (Read)', configs_encryption, params_set_operations, {
        'x_label': x_label_operations,
        'x_tick_labels': x_tick_labels_operations,
        'number_y_offset': 250,
        'filename': 'encryption_read_operations.pdf'
    }),
    MeasurementPlot('Signature (Write)', configs_signature, params_set_data_size, {
        'x_label': x_label_data_size,
        'x_tick_labels': x_tick_labels_data_size,
        'number_y_offset': 2500,
        'filename': 'signature_write_data_size.pdf'
    }),
    MeasurementPlot('Signature (Read)', configs_signature, params_set_data_size, {
        'x_label': x_label_data_size,
        'x_tick_labels': x_tick_labels_data_size,
        'number_y_offset': 1250,
        'filename': 'signature_read_data_size.pdf'
    }),
    MeasurementPlot('Signature (Write)', configs_signature, params_set_operations, {
        'x_label': x_label_operations,
        'x_tick_labels': x_tick_labels_operations,
        'number_y_offset': 125,
        'filename': 'signature_write_operations.pdf'
    }),
    MeasurementPlot('Signature (Read)', configs_signature, params_set_operations, {
        'x_label': x_label_operations,
        'x_tick_labels': x_tick_labels_operations,
        'number_y_offset': 125,
        'filename': 'signature_read_operations.pdf'
    }),
    MeasurementPlot('Signature (Write)', configs_signature_modes, params_set_data_size, {
        'x_label': x_label_data_size,
        'x_tick_labels': x_tick_labels_data_size,
        'number_y_offset': 250,
        'filename': 'signature_write_data_size_modes.pdf'
    }),
    MeasurementPlot('Signature (Read)', configs_signature_modes, params_set_data_size, {
        'x_label': x_label_data_size,
        'x_tick_labels': x_tick_labels_data_size,
        'number_y_offset': 250,
        'filename': 'signature_read_data_size_modes.pdf'
    }),
    MeasurementPlot('Signature (Write)', configs_signature_modes, params_set_operations, {
        'x_label': x_label_operations,
        'x_tick_labels': x_tick_labels_operations,
        'number_y_offset': 125,
        'filename': 'signature_write_operations_modes.pdf'
    }),
    MeasurementPlot('Signature (Read)', configs_signature_modes, params_set_operations, {
        'x_label': x_label_operations,
        'x_tick_labels': x_tick_labels_operations,
        'number_y_offset': 125,
        'filename': 'signature_read_operations_modes.pdf'
    })
]

for plot in plots:
    plot_measurements(plot.benchmark_name, plot.configs, plot.params, plot.plot_options)

reason: import numpy,import scipy


text:
from GUI import GUI
from World import World
from Train import Train
from file_processing import *
import argparse
import numpy as np

SQUARE_NUMBER = 22 # The GUI states the square numbers. SO have to bypass this

parser = argparse.ArgumentParser()
parser.add_argument('--debug', help='Starts the game in debug mode if stated',
                    action='store_true')
parser.add_argument('--terminal', help='Starts the game in terminal mode',
                    action='store_true')
parser.add_argument('--ai', help='Starts the game in AI mode',
                    action='store_true')
parser.add_argument('--size', help='Set the world size, should be divisible by 22 unless square count is changed',
                    default=660)
args = parser.parse_args()

SIZE     = args.size
DEBUG    = args.debug
TERMINAL = args.terminal
AI       = args.ai

# Implement Hamiltonian cycle to complete game
def startGame(debug):

    game = GUI(SIZE, debug)
    game.draw()
    game.master.mainloop()

def AIGame():
    params    = loadParams()
    config    = loadGameConfig()
    worldSize = config["square_number"]
    agent     = Train(params, True)
    game      = GUI(SIZE, True, agent)

    game.draw()
    game.master.mainloop()

if __name__ == "__main__":
    AIGame()
    #startGame(True)

max_stars_repo_name: kokapelli/Snake-AI

max_issues_repo_name: kokapelli/Snake-AI

max_forks_repo_name: kokapelli/Snake-AI


text:
#! /usr/bin/env python3

"""
Example of invocation of this script:

python scalapack.py -mmax 5000 -nmax 5000 -nodes 1 -cores 32 -nprocmin_pernode 1 -ntask 20 -nrun 800 -machine cori -jobid 0

where:
    -mmax (nmax) is the maximum number of rows (columns) in a matrix
    -nodes is the number of compute nodes
    -cores is the number of cores per node
    -nprocmin_pernode is the minimum number of MPIs per node for launching the application code
    -ntask is the number of different matrix sizes that will be tuned
    -nrun is the number of calls per task
    -machine is the name of the machine
    -jobid is optional. You can always set it to 0.
"""

################################################################################

from pdqrdriver import pdqrdriver
from autotune.search import *
from autotune.space import *
from autotune.problem import *
from gptune import GPTune
from data import Data
from options import Options
from computer import Computer
import sys
import os
import numpy as np
import argparse
import pickle
from random import *
from callopentuner import OpenTuner
import math

sys.path.insert(0, os.path.abspath(__file__ + "/../../GPTune/"))

sys.path.insert(0, os.path.abspath(__file__ + "/../scalapack-driver/spt/"))

################################################################################
''' The objective function required by GPTune. '''

# should always use this name for user-defined objective function
def objectives(point):
    m = point['m']
    n = point['n']
    mb = point['mb']
    nb = point['nb']
    nproc = point['nproc']
    p = point['p']

    # this becomes useful when the parameters returned by TLA1 do not respect the constraints
    if(nproc == 0 or p == 0 or nproc < p):
        print('Warning: wrong parameters for objective function!!!', nproc,p)
        return 1e12

    npernode =  math.ceil(float(nproc)/nodes)
    nthreads = int(cores / npernode)
    q = int(nproc / p)
    params = [('QR', m, n, nodes, cores, mb, nb, nthreads, nproc, p, q, 1., npernode)]

    elapsedtime = pdqrdriver(params, niter=3, JOBID=JOBID)
    print(params, ' scalapack time: ', elapsedtime)

    return elapsedtime

# should always use this name for user-defined model function
def models(point):
    m = point['m']
    n = point['n']
    nproc = point['nproc']
    p = point['p']
    mb = point['mb']
    nb = point['nb']

    # this become useful when the parameters returned by TLA1 do not respect the constraints
    if(nproc == 0 or p == 0 or nproc < p):
        print('Warning: wrong parameters for models function!!!', nproc,p)
        return 1e12

    # nthreads = int(nprocmax / nproc)
    q = int(nproc / p)

    b = max(mb,nb)
    # elapsedtime = (2*n**2*(3*m-n)/3/nproc + b*n**2/2/q + 3*b*n*(2*m-n)/2/p + b**2*n/3/p)/(max(m,n)*min(m,n)**2)
    elapsedtime = max(m,n)*min(m,n)**2/nproc/(max(m,n)*min(m,n)**2)

    return [elapsedtime]

def main():

    global ROOTDIR
    global nodes
    global cores
    global JOBID
    global nprocmax
    global nprocmin

    # Parse command line arguments
    args = parse_args()

    mmax = args.mmax
    nmax = args.nmax
    ntask = args.ntask
    nodes = args.nodes
    cores = args.cores
    nprocmin_pernode = args.nprocmin_pernode
    machine = args.machine
    nruns = args.nruns
    truns = args.truns
    JOBID = args.jobid
    TUNER_NAME = args.optimization

    os.environ['MACHINE_NAME'] = machine
    os.environ['TUNER_NAME'] = TUNER_NAME
    os.system("mkdir -p scalapack-driver/bin/%s; cp ../build/pdqrdriver scalapack-driver/bin/%s/.;" %(machine, machine))

    nprocmax = nodes*cores-1  # YL: there is one proc doing spawning, so nodes*cores should be at least 2
    nprocmin = min(nodes*nprocmin_pernode,nprocmax-1)  # YL: ensure strictly nprocmin<nprocmax, required by the Integer space

    mmin=128
    nmin=128
    m = Integer(mmin, mmax, transform="normalize", name="m")
    n = Integer(nmin, nmax, transform="normalize", name="n")
    mb = Integer(1, 128, transform="normalize", name="mb")
    nb = Integer(1, 128, transform="normalize", name="nb")
    nproc = Integer(nprocmin, nprocmax, transform="normalize", name="nproc")
    p = Integer(0, nprocmax, transform="normalize", name="p")
    r = Real(float("-Inf"), float("Inf"), name="r")

    IS = Space([m, n])
    PS = Space([mb, nb, nproc, p])
    OS = Space([r])
    cst1 = "mb * p <= m"
    cst2 = "nb * nproc <= n * p"
    cst3 = "nproc >= p"
    constraints = {"cst1": cst1, "cst2": cst2, "cst3": cst3}
    print(IS, PS, OS, constraints)

    # problem = TuningProblem(IS, PS, OS, objectives, constraints, models) # use performance models
    problem = TuningProblem(IS, PS, OS, objectives, constraints, None) # no performance model

    computer = Computer(nodes=nodes, cores=cores, hosts=None)
    """ Set and validate options """
    options = Options()
    options['model_processes'] = 1
    # options['model_threads'] = 1
    options['model_restarts'] = 1
    # options['search_multitask_processes'] = 1
    # options['model_restart_processes'] = 1
    # options['model_restart_threads'] = 1

    # options['objective_evaluation_parallelism'] = True

    options['distributed_memory_parallelism'] = False
    options['shared_memory_parallelism'] = False
    # options['mpi_comm'] = None
    options['model_class'] = 'Model_GPy_LCM'
    options['verbose'] = False

    options.validate(computer=computer)
    # giventask = [[2000, 2000]]
    # giventask = [[randint(mmin,mmax),randint(nmin,nmax)] for i in range(ntask)]
    giventask = [[460, 500], [800, 690]]

    data = Data(problem)
    gt = GPTune(problem, computer=computer, data=data, options=options,driverabspath=os.path.abspath(__file__))

    if(TUNER_NAME=='GPTune'):
        """ Building MLA with the given list of tasks """
        NI = len(giventask)
        NS = nruns
        (data, model, stats) = gt.MLA(NS=NS, NI=NI, Igiven=giventask, NS1=max(NS//2, 1))
        print("stats: ", stats)
        pickle.dump(gt, open('MLA_nodes_%d_cores_%d_mmax_%d_nmax_%d_machine_%s_jobid_%d.pkl' % (
            nodes, cores, mmax, nmax, machine, JOBID), 'wb'))

        """ Print all input and parameter samples """
        for tid in range(NI):
            print("tid: %d" % (tid))
            print("    m:%d n:%d" % (data.I[tid][0], data.I[tid][1]))
            print("    Ps ", data.P[tid])
            print("    Os ", data.O[tid])
            print('    Popt ', data.P[tid][np.argmin(data.O[tid])], 'Yopt ', min(data.O[tid])[0], 'nth ', np.argmin(data.O[tid]))

        """ Call TLA for 2 new tasks using the constructed LCM model"""
        newtask = [[400, 500], [800, 600]]
        (aprxopts, objval, stats) = gt.TLA1(newtask, NS=None)
        print("stats: ", stats)

        """ Print the optimal parameters and function evaluations"""
        for tid in range(len(newtask)):
            print("new task: %s" % (newtask[tid]))
            print('    predicted Popt: ', aprxopts[tid], ' objval: ', objval[tid])

    if(TUNER_NAME=='opentuner'):
        NI = ntask
        NS = nruns
        (data,stats)=OpenTuner(T=giventask, NS=NS, tp=problem, computer=computer, run_id="OpenTuner", niter=1, technique=None)
        print("stats: ", stats)

        """ Print all input and parameter samples """
        for tid in range(NI):
            print("tid: %d" % (tid))
            print("    m:%d n:%d" % (data.I[tid][0], data.I[tid][1]))
            print("    Ps ", data.P[tid])
            print("    Os ", data.O[tid].tolist())
            print('    Popt ', data.P[tid][np.argmin(data.O[tid])], 'Oopt ', min(data.O[tid])[0], 'nth ', np.argmin(data.O[tid]))

    if(TUNER_NAME=='hpbandster'):
        NI = ntask
        NS = nruns
        (data,stats)=HpBandSter(T=giventask, NS=NS, tp=problem, computer=computer, run_id="HpBandSter", niter=1)
        print("stats: ", stats)
        """ Print all input and parameter samples """
        for tid in range(NI):
            print("tid: %d" % (tid))
            print("    m:%d n:%d" % (data.I[tid][0], data.I[tid][1]))
            print("    Ps ", data.P[tid])
            print("    Os ", data.O[tid].tolist())
            print('    Popt ', data.P[tid][np.argmin(data.O[tid])], 'Oopt ', min(data.O[tid])[0], 'nth ', np.argmin(data.O[tid]))

def parse_args():

    parser = argparse.ArgumentParser()

    # Problem related arguments
    parser.add_argument('-mmax', type=int, default=-1, help='Number of rows')
    parser.add_argument('-nmax', type=int, default=-
                        1, help='Number of columns')
    # Machine related arguments
    parser.add_argument('-nodes', type=int, default=1,
                        help='Number of machine nodes')
    parser.add_argument('-cores', type=int, default=1,
                        help='Number of cores per machine node')
    parser.add_argument('-nprocmin_pernode', type=int, default=1,help='Minimum number of MPIs per machine node for the application code')
    parser.add_argument('-machine', type=str,
                        help='Name of the computer (not hostname)')
    # Algorithm related arguments
    parser.add_argument('-optimization', type=str, default='GPTune', help='Optimization algorithm (opentuner, hpbandster, GPTune)')
    parser.add_argument('-ntask', type=int, default=-1, help='Number of tasks')
    parser.add_argument('-nruns', type=int, help='Number of runs per task')
    parser.add_argument('-truns', type=int, help='Time of runs')
    # Experiment related arguments
    # 0 means interactive execution (not batch)
    parser.add_argument('-jobid', type=int, default=-
                        1, help='ID of the batch job')
    parser.add_argument('-stepid', type=int, default=-1, help='step ID')
    parser.add_argument('-phase', type=int, default=0, help='phase')

    args = parser.parse_args()

    return args

if __name__ == "__main__":
    main()


text:
# Copyright 2018 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.
# ==============================================================================
"""Unit test for tf.scan under eager execution."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import time

import numpy as np
import tensorflow as tf

class ScanBenchmark(tf.test.Benchmark):

  def runScan(self, n):
    elems = np.arange(n)
    start_time = time.time()
    _ = tf.scan(lambda a, x: a + x, elems, parallel_iterations=1)
    wall_time = time.time() - start_time

    self.report_benchmark(
        name='scan',
        iters=n,
        wall_time=wall_time)

  def benchmarkScan2000(self):
    self.runScan(2000)

  def benchmarkScan4000(self):
    self.runScan(4000)

  def benchmarkScan8000(self):
    self.runScan(8000)

  def benchmarkScan16000(self):
    self.runScan(16000)

  def benchmarkScan32000(self):
    self.runScan(32000)

if __name__ == '__main__':
  tf.enable_eager_execution()
  tf.test.main()

max_stars_repo_name: shanest/tensorflow

max_issues_repo_name: shanest/tensorflow

max_forks_repo_name: shanest/tensorflow


text:
################################
##  Set diagnostics to True   ##
##  If you want display the   ##
##      Tracking process.     ##
################################

diagnostics=False

#################################
##      Import packages        ##
#################################
import sys
import os

from trackeddy.tracking import *
from trackeddy.savedata import *
from numpy import *
from pylab import *
import random
import pytest
import time

#################################
##   Import tools to create    ##
##     syntetic fields         ##
#################################

from trackeddy.utils.gaussian_field_functions import *
import trackeddy.utils.field_generator as fg

n=2
a  = 0.1
b  = 0.1
t0 = 0
t  = 1

xx=linspace(10,12,200)
yy=linspace(10,12,200)

gf=fg.Generate_field(a,b,n,xx,yy,'Nint')

data = abs(gf.assemble_field(t))

x=linspace(10,13,300)
y=linspace(10,13,300)

preferences={'ellipse':0.85,'eccentricity':0.85,'gaussian':0.8}
eddytd={}
eddytdn={}

levels = {'max':data.max(),'min':0.1,'step':0.1}
eddytd = analyseddyzt(data,x,y,t0,t,1,levels,preferences=preferences,areamap='',mask='',maskopt='forcefit'\
                    ,destdir='',physics='',diagnostics=False,plotdata=False,pprint=True,debug=False)

@pytest.mark.ttrackeddy_data
def test_data2npy():
    save_data('./test.npy', eddytd)
    assert os.path.isfile('./test.npy')

@pytest.mark.ttrackeddy_data
def test_tracknpy2nc():
    track2nc = Trackeddy2dataset('./test.npy','./','nc')
    track2nc.file2dict()
    track2nc.trackeddy2nc()
    assert os.path.isfile('./output_000.nc')

@pytest.mark.ttrackeddy_data
def test_trackdata2nc():
    track2nc = Trackeddy2dataset(eddytd,'./','nc')
    track2nc.trackeddy2nc()
    assert os.path.isfile('./output_001.nc')

@pytest.mark.ttrackeddy_data
def test_rm_files():
    os.remove('./test.npy')
    os.remove('./output_000.nc')
    os.remove('./output_001.nc')

max_stars_repo_name: navidcy/trackeddy

max_issues_repo_name: powerwordlearner/trackeddy

max_forks_repo_name: powerwordlearner/trackeddy

reason: from numpy


text:
import os
import re
import numpy as np
import pandas as pd
from eunjeon import Mecab
from DB_connect_setting import DB
os.environ['TF_CPP_MIN_LOG_LEVEL'] = "3"
from DB_train_get_data import _user_skill, _user_search_log, _user_view_log, _other_view_log

# Preprocessing Functions
def regex(input_data):
    result = []

    for text in input_data:
        result.append(re.sub(r'[^ㄱ-ㅎㅏ-ㅣ가-힣0-9a-zA-Z ]', '', text))

    return result

def lower(input_data):
    result = []

    for text in input_data:
        result.append(text.lower())

    return result

def nouns(input_data):
    stopwords = ['의', '가', '이', '은', '들', '는', '좀', '잘', '걍', '과',
                 '도', '를', '으로', '자', '에', '와', '한', '하다']
    result = []
    mecab = Mecab()

    for text in input_data:
        token = mecab.nouns(text)
        token = [t for t in token if t not in stopwords]
        token = [t for t in token if len(token) > 1]
        token = " ".join(token)
        result.append(token)

    return result

# Get DataFrame from PiPi MySQL DB with PyMySQL
def get_data(email):
    skill_df = _user_skill(email)
    search_log_df = _user_search_log(email)
    view_log_df = _user_view_log(email)
    other_view_log_df = _other_view_log(email)
    # try:
    #     skill_df = _user_skill(email)
    #     search_log_df = _user_search_log(email)
    #     view_log_df = _user_view_log(email)
    #     other_view_log_df = _other_view_log(email)
    #
    # finally:
    #     DB.close()

    return skill_df, search_log_df, view_log_df, other_view_log_df

def processed_data(email):
    # US: User_Skillset - user_skill
    # USL: User_Search_Log - search_log
    # UVL: User_View_Log - id, title, skillset, content, view_log, label
    # OVL: Other_USer_Log - id, title, skillset, content, view_log, label
    US, USL, UVL, OVL = get_data(email)

    US.user_skill = lower(US.user_skill)
    USL.search_log = lower(USL.search_log)
    UVL.id = list(map(str, UVL.id))
    UVL.title = nouns(regex(lower(UVL.title)))
    UVL.skillset = lower(UVL.skillset)
    UVL.content = nouns(regex(lower(UVL.content)))
    UVL.view_log = lower(UVL.view_log)
    OVL.id = list(map(str, OVL.id))
    OVL.title = nouns(regex(lower(OVL.title)))
    OVL.skillset = nouns(regex(lower(OVL.title)))
    OVL.content = nouns(regex(lower(OVL.content)))
    OVL.view_log = lower(OVL.view_log)

    train_data_A = pd.concat([US, USL], axis=1, ignore_index=True)
    train_data_B = pd.concat([UVL, OVL], ignore_index=True)
    train_dataset = pd.concat([train_data_A, train_data_B], axis=1, ignore_index=True).sample(frac=1).reset_index(drop=True)
    train_dataset.columns = ["user_skill", "search_log", "id", "title", "skillset", "content", "view_log", "label"]
    train_dataset = train_dataset.fillna("undefined")

    X_sum = []
    X_token = []
    X_raw = train_dataset[[x for x in train_dataset.columns if x != "label"]]
    Y_data = np.array(train_dataset["label"])

    from tensorflow.keras.preprocessing.text import Tokenizer
    from tensorflow.keras.preprocessing.sequence import pad_sequences

    tokenizer = Tokenizer(oov_token="undefined")

    for idx in range(len(X_raw)):
        loc = list(X_raw.loc[idx])
        tokenizer.fit_on_texts(loc)
        X_token.append(list(tokenizer.texts_to_sequences(loc)))

    for idx in range(len(X_token)):
        loc = sum(X_token[idx], [])
        X_sum.append(loc)

    max_len = 50
    input_dim = len(tokenizer.word_index) + 1
    X_data = np.array(pad_sequences(X_sum, maxlen=max_len, padding="post"))

    # print(X_data)
    # print(X_data.shape)

    return input_dim, max_len, X_data, Y_data

max_stars_repo_name: Pi-Pi-project/PiPi-Kermit

max_issues_repo_name: Pi-Pi-project/PiPi-Kermit

max_forks_repo_name: Pi-Pi-project/PiPi-Kermit