text:
import json
import os
import pickle

import numpy as np

from experiments.analysis.utils import parse_log, read_in_correlation
from nasbench_analysis.search_spaces.search_space_1 import SearchSpace1
from nasbench_analysis.search_spaces.search_space_2 import SearchSpace2
from nasbench_analysis.search_spaces.search_space_3 import SearchSpace3

def search_space_id_to_obj(id):
    if int(id) == 1:
        return SearchSpace1()
    elif int(id) == 2:
        return SearchSpace2()
    elif int(id) == 3:
        return SearchSpace3()
    else:
        raise ValueError('Search space unknown.')

def get_directory_list(path):
    """Find directory containing config.json files"""
    directory_list = []
    # return nothing if path is a file
    if os.path.isfile(path):
        return []
    # add dir to directorylist if it contains .json files
    if len([f for f in os.listdir(path) if f == 'config.json']) > 0:
        directory_list.append(path)
    for d in os.listdir(path):
        new_path = os.path.join(path, d)
        if os.path.isdir(new_path):
            directory_list += get_directory_list(new_path)
    return directory_list

def get_key_from_scalar_configs(configs, key):
    metrics_to_stack = [list(config['scalars'][key]) for config in configs]
    shortest_metric = min([len(m) for m in metrics_to_stack])

    if 'validation_errors' == key or 'test_errors' == key:
        search_space = search_space_id_to_obj(configs[0]['search_space'])
        if 'test' in key:
            minimum = search_space.test_min_error
        elif 'valid' in key:
            minimum = search_space.valid_min_error
        else:
            raise ValueError('incorrect name in key')
    else:
        minimum = 0

    return np.mean(np.stack([metric[:shortest_metric] for metric in metrics_to_stack], axis=0), axis=-1) - minimum

class ExperimentDatabase:
    def __init__(self, root_dir):
        """Load all directories with trainings."""
        self._load(root_dir=root_dir)

    def query(self, conditions):
        searched_config = []
        for config in self._database:
            # Only select config if all conditions are satisfied
            conds_satisfied = [config.get(cond_key, None) == cond_val for cond_key, cond_val in conditions.items()]
            if all(conds_satisfied):
                searched_config.append(config)

        return searched_config

    def query_correlation(self, conditions):
        searched_config = []
        for config in self._database:
            # Only select config if all conditions are satisfied
            conds_satisfied = [config.get(cond_key, None) == cond_val for cond_key, cond_val in conditions.items()]
            if all(conds_satisfied):
                if config['scalars']['correlation_total'] is not None:
                    searched_config.append(config)

        return searched_config

    def _load(self, root_dir):
        self._database = []
        for directory in get_directory_list(root_dir):
            try:
                self._database.append(self._get_run_dictionary(directory))
            except Exception as e:
                print('Error occurred in loading', directory, e)

    def _get_run_dictionary(self, path):
        with open(os.path.join(path, 'config.json')) as fp:
            config = json.load(fp)

        with open(os.path.join(path, 'one_shot_validation_errors.obj'), 'rb') as fp:
            validation_errors = pickle.load(fp)

        with open(os.path.join(path, 'one_shot_test_errors.obj'), 'rb') as fp:
            test_errors = pickle.load(fp)

        one_shot_validation_errors, one_shot_training_errors = parse_log(path)
        correlation_total, correlation_top = read_in_correlation(path, config)

        config['scalars'] = {}
        config['scalars']['validation_errors'] = validation_errors
        config['scalars']['test_errors'] = test_errors
        config['scalars']['one_shot_validation_errors'] = one_shot_validation_errors
        config['scalars']['one_shot_training_errors'] = one_shot_training_errors
        config['scalars']['correlation_total'] = correlation_total
        config['scalars']['correlation_top'] = correlation_top

        return config

def main():
    experiment_database = ExperimentDatabase(root_dir='experiments/darts')
    pass

if __name__ == '__main__':
    main()


text:
#!/usr/bin/env python3
import rospy
from std_msgs.msg import String
from tf.transformations import euler_from_quaternion
from geometry_msgs.msg import Point, Twist
from std_msgs.msg import Float64
from nav_msgs.msg import Odometry
from sensor_msgs.msg import Imu
from std_msgs.msg import String
from operator import itemgetter
import matplotlib.pyplot as plt
from itertools import *
import sensor_msgs.msg
from math import atan2
import numpy as np
import random
import time
import math

LINX = 0.0 #Always forward linear velocity.
THRESHOLD = 1.5 #THRESHOLD value for laser scan.
PI = math.pi
data_ban=-1
data0_ban=1
Kp =.025
angz = 0.1
x = 0.0
y = 0.0
theta = 0.0
yaw=0
V=1
X=np.array([])
Y=np.array([])
ang=np.array([])
Yaw=np.array([])
def imu_data0(msg):
    global yaw
    yaw = msg.orientation.z

def newOdom(msg):
    global x
    global y
    global theta
    x = 1.1*msg.pose.pose.position.x
    y = 1.1*msg.pose.pose.position.y
    rot_q = msg.pose.pose.orientation
    (roll, pitch, theta) = euler_from_quaternion([rot_q.x, rot_q.y, rot_q.z, rot_q.w])
rospy.init_node("speed_controller")
sub = rospy.Subscriber("/imu_data", Imu,imu_data0)
sub = rospy.Subscriber("/odom", Odometry, newOdom)
pub_vel = rospy.Publisher("/cmd_vel", Twist , queue_size = 1)
pub_ban = rospy.Publisher("/simple_model/base_to_ban_joint_position_controller/command", Float64 , queue_size = 1)
pub_fl = rospy.Publisher("/simple_model/base_to_fl_steer_joint_position_controller/command", Float64 , queue_size = 1)
pub_fr = rospy.Publisher("/simple_model/base_to_fr_steer_joint_position_controller/command", Float64 , queue_size = 1)
r = rospy.Rate(3)
goal = Point()
goal.x  = 8
goal.y  = 8
i=1

while not rospy.is_shutdown():
	speed = Twist()
	inc_x = goal.x -x
	inc_y = goal.y -y
	speed = Twist()

	angle_to_goal = atan2(inc_y, inc_x)
	alpha = angle_to_goal - theta
#### command
	if (abs(alpha) > PI/2):
		angular_z = 0.5
		speed.linear.x  =  V
		#print "yes"
	else:
		speed.linear.x  =  V
		angular_z = alpha

#### stop command
	if abs(inc_x)     <= 0.5:
		if abs(inc_y) <= 0.5:
			#print "inc_x",inc_x , "inc_y",inc_y ,"xg",goal.x, "yg",goal.y, "x" , x, "y" , y
			speed.linear.x  = -V
			angular_z = (abs(inc_x)+abs(inc_y))/2
			pub_vel.publish(speed)
			#print "speed.linear.x", speed.linear.x
			time.sleep(4)
			speed.linear.x  = 0
			angular_z       = 0
	def LaserScanProcess(data):
		range_angels = np.arange(len(data.ranges))
		ranges = np.array(data.ranges)
		range_mask = (ranges > THRESHOLD)
		ranges = list(range_angels[range_mask])
		max_gap = 40
		# #print(ranges)
		gap_list = []
		for k, g in groupby(enumerate(ranges), lambda (i,x):i-x):
		    .gap_list.append(map(itemgetter(1), g))
		gap_list.sort(key=len)
		largest_gap = gap_list[-1]
		min_angle, max_angle = largest_gap[0]*((data.angle_increment)*180/PI), largest_gap[-1]*((data.angle_increment)*180/PI)
		average_gap = (max_angle - min_angle)/2

		turn_angle = min_angle + average_gap
		global LINX
		global angz
		#if average_gap < max_gap:
		#    angz = 0.
		if average_gap > max_gap:
		    LINX = 0.2
		    angz = Kp*(90 - turn_angle)
		if angz > 0.5:
			angz=0.5
		if angz < -0.5:
			angz = -0.5
	rospy.Subscriber("scan", sensor_msgs.msg.LaserScan , LaserScanProcess)
	if angular_z >  0.5:
		angular_z=  0.5
	if angular_z  <-0.5:
		angular_z =-0.5
	if speed.linear.x >1:
		speed.linear.x=1
	if angz < 0.1:
		pub_fl.publish(angular_z)
		pub_fr.publish(angular_z)
		pub_vel.publish(speed)
	else:
		speed = Twist()
		speed.linear.x=LINX
		speed.angular.z=0
		pub_fl.publish(-angz)
		pub_fr.publish(-angz)
		pub_vel.publish(speed)

	if angular_z == 0 and speed.linear.x == 0:
	    pub_ban.publish(data_ban)
	    time.sleep(3)
	    data0_ban=0
	    pub_ban.publish(data0_ban)
	    time.sleep(5)

	X=np.hstack((X,x))
	Y=np.hstack((Y,y))
	Yaw=np.hstack((Yaw,yaw))
	ang = np.hstack((ang,speed.angular.z))
	if np.size(Y) == np.size(X) and data0_ban==0:
		#plt.plot(angular_z,linestyle='solid', marker='*');
		#plt.show()
		plt.plot(X,Y,linestyle='solid', marker='*'),plt.grid(True);
		plt.show()
		break
	#print "angular_z", angular_z ,'speed.linear.x',speed.linear.x , 'angz' , angz
	time.sleep(.5)

ext: py

reason: import numpy


text:
"""

Defining Class of custom environment for V-Rep

@author: hussein
"""

import vrep_env
from vrep_env import vrep

import os
vrep_scenes_path = os.environ['/home/hussein/Desktop/Multi-agent-path-planning/Reinforcement Learning/examples/scenes']

import rclpy
from rclpy.node import Node
from tf2_msgs.msg import TFMessage
from std_msgs.msg import Float32
import sim

import math
import gym
from gym import spaces
from gym.utils import seeding
import numpy as np
import time

L = 1 # Parameter of robot
d = 0.5 # Parameter of robot
A = np.ones(6) - np.identity(6) # Adjancency Matrix fully connected case 6x6

ux = np.zeros((6,1)) # 6x1
uy = np.zeros((6,1)) # 6x1

" Connecting to V-Rep "
sim.simxFinish(-1) # just in case, close all opened connections
clientID=sim.simxStart('127.0.0.1',19997,True,True,-500000,5) # Connect to CoppeliaSim
N_SCENES = 80
scenes = np.hstack(( np.random.uniform(-2,2,size=(N_SCENES,2)), np.random.uniform(0,np.pi,size=(N_SCENES,1)), np.random.uniform(-2,2,(N_SCENES,2)), np.random.uniform(0,np.pi,size=(N_SCENES,1)) ))

def euler_from_quaternion(x, y, z, w):

     t3 = +2.0 * (w * z + x * y)
     t4 = +1.0 - 2.0 * (y * y + z * z)
     yaw_z = math.atan2(t3, t4)

     return yaw_z # in radians

"""
Description:
    Consensus environment of 6 robots, where each episode they converge towards each other. DQN applied to robot 1 and rest are controlled with the consensus algorithm.

Source:
    This environment corresponds to V-Rep simulator, integrated with ROS to publish actions & subscribe to observations.
Observation:
    Type: Box(4)
    Num     Observation               Min                     Max
    0       Mx                        -4.8                    4.8
    1       My                        -4.8                    4.8
    2       Phix                      -4.8                    4.8
    3       Phiy                      -4.8                    4.8

Actions:
    Type: Discrete(4)
    Num   Action
    0     Move the robot upwards
    1     Move the robot downwards
    2     Move the robot to the left
    3     Move the robot to the right

"""

class MobileRobotVrepEnv(vrep_env.VrepEnv):
    metadata = {
        'render.modes': ['human', 'rgb_array'],
        'video.frames_per_second' : 50
        }
    def __init__(self, server_addr='127.0.0.1', server_port=19997, scene_path=vrep_scenes_path+'/Scene_of_Six_Robots.ttt'):

        vrep_env.VrepEnv.__init__(self, server_addr, server_port, scene_path)
        super().__init__('minimal_publisher1')
        self.publisher_l1 = self.create_publisher(Float32, '/leftMotorSpeedrobot1', 0) #Change according to topic in child script,String to Float32
        self.publisher_r1 = self.create_publisher(Float32, '/rightMotorSpeedrobot1',0) #Change according to topic in child script,String to Float32
        self.publisher_l2 = self.create_publisher(Float32, '/leftMotorSpeedrobot2', 0) #Change according to topic in child script,String to Float32
        self.publisher_r2 = self.create_publisher(Float32, '/rightMotorSpeedrobot2',0) #Change according to topic in child script,String to Float32
        self.publisher_l3 = self.create_publisher(Float32, '/leftMotorSpeedrobot3', 0) #Change according to topic in child script,String to Float32
        self.publisher_r3 = self.create_publisher(Float32, '/rightMotorSpeedrobot3',0) #Change according to topic in child script,String to Float32
        self.publisher_l4 = self.create_publisher(Float32, '/leftMotorSpeedrobot4', 0) #Change according to topic in child script,String to Float32
        self.publisher_r4 = self.create_publisher(Float32, '/rightMotorSpeedrobot4',0) #Change according to topic in child script,String to Float32
        self.publisher_l5 = self.create_publisher(Float32, '/leftMotorSpeedrobot5', 0) #Change according to topic in child script,String to Float32
        self.publisher_r5 = self.create_publisher(Float32, '/rightMotorSpeedrobot5',0) #Change according to topic in child script,String to Float32
        self.publisher_l6 = self.create_publisher(Float32, '/leftMotorSpeedrobot6', 0) #Change according to topic in child script,String to Float32
        self.publisher_r6 = self.create_publisher(Float32, '/rightMotorSpeedrobot6',0) #Change according to topic in child script,String to Float32
        self.subscription = self.create_subscription(
            TFMessage,
            '/tf',
            self.listener_callback,
            0)

        " Timer Callback "
        timer_period = 0.03  # seconds
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.i = 0

        " Parameters "
        self.t = 0 # Just to intialized Phix's and Phiy's

        " Initialize Phi's "
        self.Phix1 = 0 # 1x1
        self.Phiy1 = 0 # 1x1
        self.Phix2 = 0 # 1x1
        self.Phiy2 = 0 # 1x1
        self.Phix3 = 0 # 1x1
        self.Phiy3 = 0 # 1x1
        self.Phix4 = 0 # 1x1
        self.Phiy4 = 0 # 1x1
        self.Phix5 = 0 # 1x1
        self.Phiy5 = 0 # 1x1
        self.Phix6 = 0 # 1x1
        self.Phiy6 = 0 # 1x1

        " Mobile Robot 1 Parameters "
        self.x1 = 0
        self.y1 = 0
        self.Theta1 = 0
        self.v1 = 0
        self.w1 = 0
        self.vL1 = 0
        self.vR1 = 0

        " Mobile Robot 2 Parameters "
        self.x2 = 0
        self.y2 = 0
        self.Theta2 = 0
        self.v2 = 0
        self.w2 = 0
        self.vL2 = 0
        self.vR2 = 0

        " Mobile Robot 3 Parameters "
        self.x3 = 0
        self.y3 = 0
        self.Theta3 = 0
        self.v3 = 0
        self.w3 = 0
        self.vL3 = 0
        self.vR3 = 0

        " Mobile Robot 4 Parameters "
        self.x4 = 0
        self.y4 = 0
        self.Theta4 = 0
        self.v4 = 0
        self.w4 = 0
        self.vL4 = 0
        self.vR4 = 0

        " Mobile Robot 5 Parameters "
        self.x5 = 0
        self.y5 = 0
        self.Theta5 = 0
        self.v5 = 0
        self.w5 = 0
        self.vL5 = 0
        self.vR5 = 0

        " Mobile Robot 6 Parameters "
        self.x6 = 0
        self.y6 = 0
        self.Theta6 = 0
        self.v6 = 0
        self.w6 = 0
        self.vL6 = 0
        self.vR6 = 0

        " Distance at which to fail the episode "
        self.distance_threshold = 2.2

        " Observation & Action Space "
        # Define Action Space

        self.action_space = spaces.Discrete(4)

        # Define Observation Space
        high_observation = np.array([4.8,
                                     4.8,
                                     4.8,
                                     4.8],
                                    dtype=np.float32)

        self.observation_space = spaces.Box(-high_observation, -high_observation, dtype=np.float32)

        self.seed()
        self.viewer = None
        self.state = None
        self.steps_beyond_done = None

    def listener_callback(self, msg):

        if msg.transforms[0].child_frame_id == 'robot1' :
            self.x1 = msg.transforms[0].transform.translation.x
            self.y1 = msg.transforms[0].transform.translation.y
            self.xr1 = msg.transforms[0].transform.rotation.x
            self.yr1 = msg.transforms[0].transform.rotation.y
            self.zr1 = msg.transforms[0].transform.rotation.z
            self.wr1 = msg.transforms[0].transform.rotation.w
            self.Theta1 = euler_from_quaternion(self.xr1,self.yr1,self.zr1,self.wr1)
            self.state1 = (self.x1,self.y1,self.Theta1)

        if  msg.transforms[0].child_frame_id == 'robot2' :
            self.x2 = msg.transforms[0].transform.translation.x
            self.y2 = msg.transforms[0].transform.translation.y
            self.xr2 = msg.transforms[0].transform.rotation.x
            self.yr2 = msg.transforms[0].transform.rotation.y
            self.zr2 = msg.transforms[0].transform.rotation.z
            self.wr2 = msg.transforms[0].transform.rotation.w
            self.Theta2 = euler_from_quaternion(self.xr2,self.yr2,self.zr2,self.wr2)
            self.state2 = (self.x2,self.y2,self.Theta2)

        if  msg.transforms[0].child_frame_id == 'robot3' :

            self.x3 = msg.transforms[0].transform.translation.x
            self.y3 = msg.transforms[0].transform.translation.y
            self.xr3 = msg.transforms[0].transform.rotation.x
            self.yr3 = msg.transforms[0].transform.rotation.y
            self.zr3 = msg.transforms[0].transform.rotation.z
            self.wr3 = msg.transforms[0].transform.rotation.w
            self.Theta3 = euler_from_quaternion(self.xr3,self.yr3,self.zr3,self.wr3)
            self.state3 = (self.x3,self.y3,self.Theta3)

        if  msg.transforms[0].child_frame_id == 'robot4' :

            self.x4 = msg.transforms[0].transform.translation.x
            self.y4 = msg.transforms[0].transform.translation.y
            self.xr4 = msg.transforms[0].transform.rotation.x
            self.yr4 = msg.transforms[0].transform.rotation.y
            self.zr4 = msg.transforms[0].transform.rotation.z
            self.wr4 = msg.transforms[0].transform.rotation.w
            self.Theta4 = euler_from_quaternion(self.xr4,self.yr4,self.zr4,self.wr4)
            self.state4 = (self.x4,self.y4,self.Theta4)

        if  msg.transforms[0].child_frame_id == 'robot5' :

            self.x5 = msg.transforms[0].transform.translation.x
            self.y5 = msg.transforms[0].transform.translation.y
            self.xr5 = msg.transforms[0].transform.rotation.x
            self.yr5 = msg.transforms[0].transform.rotation.y
            self.zr5 = msg.transforms[0].transform.rotation.z
            self.wr5 = msg.transforms[0].transform.rotation.w
            self.Theta5 = euler_from_quaternion(self.xr5,self.yr5,self.zr5,self.wr5)
            self.state5 = (self.x5,self.y5,self.Theta5)

        if  msg.transforms[0].child_frame_id == 'robot6' :

            self.x6 = msg.transforms[0].transform.translation.x
            self.y6 = msg.transforms[0].transform.translation.y
            self.xr6 = msg.transforms[0].transform.rotation.x
            self.yr6 = msg.transforms[0].transform.rotation.y
            self.zr6 = msg.transforms[0].transform.rotation.z
            self.wr6 = msg.transforms[0].transform.rotation.w
            self.Theta6 = euler_from_quaternion(self.xr6,self.yr6,self.zr6,self.wr6)
            self.state6 = (self.x6,self.y6,self.Theta6)

    def timer_callback(self):

        A = np.ones(6) - np.identity(6) # Adjancency Matrix

        self.X = np.array([ [self.x1], [self.x2], [self.x3], [self.x4], [self.x5], [self.x6]  ]) #6x1
        self.Y = np.array([ [self.y1], [self.y2], [self.y3], [self.y4], [self.y5], [self.y6]  ]) #6x1

        ux = np.zeros((6,1)) # 6x1
        uy = np.zeros((6,1)) # 6x1

        for i in range(1,7):
            for j in range(1,7):
                ux[i-1] += -(A[i-1][j-1])*(self.X[i-1]-self.X[j-1]) # 1x1 each
                uy[i-1] += -(A[i-1][j-1])*(self.Y[i-1]-self.Y[j-1]) # 1x1 each

        if self.action_input1[0]==0:
            self.v1 = -1.0
        else:
            self.v1 = +1.0

        if self.action_input1[1]==0:
            self.w1 = -1.0
        else:
            self.w1 = +1.0

        u2 = np.array([ [float(ux[1])], [float(uy[1])] ]) # 2x1
        u3 = np.array([ [float(ux[2])], [float(uy[2])] ]) # 2x1
        u4 = np.array([ [float(ux[3])], [float(uy[3])] ]) # 2x1
        u5 = np.array([ [float(ux[4])], [float(uy[4])] ]) # 2x1
        u6 = np.array([ [float(ux[5])], [float(uy[5])] ]) # 2x1

        " Calculate V1/W1, V2/W2, V3/W3, V4/W4, V5/W5, V6/W6 "

        S1 = np.array([[self.v1], [self.w1]]) #2x1
        # G1 = np.array([[1,0], [0,1/L]]) #2x2
        # R1 = np.array([[math.cos(self.Theta1),math.sin(self.Theta1)],[-math.sin(self.Theta1),math.cos(self.Theta1)]]) #2x2
        # S1 = np.dot(np.dot(G1, R1), u1) #2x1

        S2 = np.array([[self.v2], [self.w2]]) #2x1
        G2 = np.array([[1,0], [0,1/L]]) #2x2
        R2 = np.array([[math.cos(self.Theta2),math.sin(self.Theta2)],[-math.sin(self.Theta2),math.cos(self.Theta2)]]) #2x2
        S2 = np.dot(np.dot(G2, R2), u2) # 2x1

        S3 = np.array([[self.v3], [self.w3]]) #2x1
        G3 = np.array([[1,0], [0,1/L]]) #2x2
        R3 = np.array([[math.cos(self.Theta3),math.sin(self.Theta3)],[-math.sin(self.Theta3),math.cos(self.Theta3)]]) #2x2
        S3 = np.dot(np.dot(G3, R3), u3) #2x1

        S4 = np.array([[self.v4], [self.w4]]) #2x1
        G4 = np.array([[1,0], [0,1/L]]) #2x2
        R4 = np.array([[math.cos(self.Theta4),math.sin(self.Theta4)],[-math.sin(self.Theta4),math.cos(self.Theta4)]]) #2x2
        S4 = np.dot(np.dot(G4, R4), u4) #2x1

        S5 = np.array([[self.v5], [self.w5]]) #2x1
        G5 = np.array([[1,0], [0,1/L]]) #2x2
        R5 = np.array([[math.cos(self.Theta5),math.sin(self.Theta5)],[-math.sin(self.Theta5),math.cos(self.Theta5)]]) #2x2
        S5 = np.dot(np.dot(G5, R5), u5) #2x1

        S6 = np.array([[self.v6], [self.w6]]) #2x1
        G6 = np.array([[1,0], [0,1/L]]) #2x2
        R6 = np.array([[math.cos(self.Theta6),math.sin(self.Theta6)],[-math.sin(self.Theta6),math.cos(self.Theta6)]]) #2x2
        S6 = np.dot(np.dot(G6, R6), u6) #2x1

        " Calculate VL1/VR1, VL2/VR2, VL3/VR3, VL4/VR4, VL5/VR5, VL6/VR6 "

        D = np.array([[1/2,1/2],[-1/(2*d),1/(2*d)]]) #2x2
        Di = np.linalg.inv(D) #2x2

        Speed_L1 = np.array([[self.vL1], [self.vR1]]) # Vector 2x1 for Speed of Robot 1
        Speed_L2 = np.array([[self.vL2], [self.vR2]]) # Vector 2x1 for Speed of Robot 2
        Speed_L3 = np.array([[self.vL3], [self.vR3]]) # Vector 2x1 for Speed of Robot 3
        Speed_L4 = np.array([[self.vL4], [self.vR4]]) # Vector 2x1 for Speed of Robot 4
        Speed_L5 = np.array([[self.vL5], [self.vR5]]) # Vector 2x1 for Speed of Robot 5
        Speed_L6 = np.array([[self.vL6], [self.vR6]]) # Vector 2x1 for Speed of Robot 6

        M1 = np.array([[S1[0]],[S1[1]]]).reshape(2,1) #2x1
        M2 = np.array([[S2[0]],[S2[1]]]).reshape(2,1) #2x1
        M3 = np.array([[S3[0]],[S3[1]]]).reshape(2,1) #2x1
        M4 = np.array([[S4[0]],[S4[1]]]).reshape(2,1) #2x1
        M5 = np.array([[S5[0]],[S5[1]]]).reshape(2,1) #2x1
        M6 = np.array([[S6[0]],[S6[1]]]).reshape(2,1) #2x1

        Speed_L1 = np.dot(Di, M1) # 2x1 (VL1, VR1)
        Speed_L2 = np.dot(Di, M2) # 2x1 (VL2, VR2)
        Speed_L3 = np.dot(Di, M3) # 2x1 (VL3, VR3)
        Speed_L4 = np.dot(Di, M4) # 2x1 (VL4, VR4)
        Speed_L5 = np.dot(Di, M5) # 2x1 (VL5, VR5)
        Speed_L6 = np.dot(Di, M6) # 2x1 (VL6, VR6)

        VL1 = float(Speed_L1[0])
        VR1 = float(Speed_L1[1])
        VL2 = float(Speed_L2[0])
        VR2 = float(Speed_L2[1])
        VL3 = float(Speed_L3[0])
        VR3 = float(Speed_L3[1])
        VL4 = float(Speed_L4[0])
        VR4 = float(Speed_L4[1])
        VL5 = float(Speed_L5[0])
        VR5 = float(Speed_L5[1])
        VL6 = float(Speed_L6[0])
        VR6 = float(Speed_L6[1])

        " Publish Speed Commands to Robot 1 "

        msgl1 = Float32()
        msgr1 = Float32()
        msgl1.data = VL1
        msgr1.data = VR1
        self.publisher_l1.publish(msgl1)
        self.publisher_r1.publish(msgr1)

        " Publish Speed Commands to Robot 2 "

        msgl2 = Float32()
        msgr2 = Float32()
        msgl2.data = VL2
        msgr2.data = VR2
        self.publisher_l2.publish(msgl2)
        self.publisher_r2.publish(msgr2)

        " Publish Speed Commands to Robot 3 "

        msgl3 = Float32()
        msgr3 = Float32()
        msgl3.data = VL3
        msgr3.data = VR3
        self.publisher_l3.publish(msgl3)
        self.publisher_r3.publish(msgr3)

        " Publish Speed Commands to Robot 4 "

        msgl4 = Float32()
        msgr4 = Float32()
        msgl4.data = VL4
        msgr4.data = VR4
        self.publisher_l4.publish(msgl4)
        self.publisher_r4.publish(msgr4)

        " Publish Speed Commands to Robot 5 "

        msgl5 = Float32()
        msgr5 = Float32()
        msgl5.data = VL5
        msgr5.data = VR5
        self.publisher_l5.publish(msgl5)
        self.publisher_r5.publish(msgr5)

        " Publish Speed Commands to Robot 6 "

        msgl6 = Float32()
        msgr6 = Float32()
        msgl6.data = VL6
        msgr6.data = VR6
        self.publisher_l6.publish(msgl6)
        self.publisher_r6.publish(msgr6)

    def seed(self, seed=None):
        self.np_random, seed = seeding.np_random(seed)
        return [seed]

    def step(self, action):

        assert self.action_space.contains(action), "%r (%s) invalid"%(action, type(action))

        " Distance Threshold "
        self.distance = abs(self.x1 - self.x2) + abs(self.y1 - self.y2) + abs(self.x1 - self.x3) + abs(self.y1 - self.y3) + abs(self.x1 - self.x4) + abs(self.y1 - self.y4) + abs(self.x1 - self.x5) + abs(self.y1 - self.y5) + abs(self.x1 - self.x6) + abs(self.y1 - self.y6)

        " Use Adjacency Matrix to find Mxy and Phi's "

        A = np.ones(6) - np.identity(6) # Adjancency Matrix

        self.X = np.array([ [self.x1], [self.x2], [self.x3], [self.x4], [self.x5], [self.x6]  ]) #6x1
        self.Y = np.array([ [self.y1], [self.y2], [self.y3], [self.y4], [self.y5], [self.y6]  ]) #6x1

        Mx = np.zeros((6,1)) # 6x1
        My = np.zeros((6,1)) # 6x1

        for i in range(1,7):
            for j in range(1,7):
                Mx[i-1] += (A[i-1][j-1])*(self.X[j-1] - self.X[i-1]) # 1x1 each
                My[i-1] += (A[i-1][j-1])*(self.Y[j-1] - self.Y[i-1]) # 1x1 each

        Mx1 = float(Mx[0]) / 5 # 1x1
        My1 = float(My[0]) / 5 # 1x1

        Mx2 = float(Mx[1]) / 5 # 1x1
        My2 = float(My[1]) / 5 # 1x1

        Mx3 = float(Mx[2]) / 5 # 1x1
        My3 = float(My[2]) / 5 # 1x1

        Mx4 = float(Mx[3]) / 5 # 1x1
        My4 = float(My[3]) / 5 # 1x1

        Mx5 = float(Mx[4]) / 5 # 1x1
        My5 = float(My[4]) / 5 # 1x1

        Mx6 = float(Mx[5]) / 5 # 1x1
        My6 = float(My[5]) / 5 # 1x1

        self.Phix1 = ( Mx2 + Mx3 + Mx4 + Mx5 + Mx6 ) / 5 # 1x1
        self.Phiy1 = ( My2 + My3 + My4 + My5 + My6 ) / 5 # 1x1

        self.Phix2 = ( Mx1 + Mx3 + Mx4 + Mx5 + Mx6 ) / 5 # 1x1
        self.Phiy2 = ( My1 + My3 + My4 + My5 + My6 ) / 5 # 1x1

        self.Phix3 = ( Mx1 + Mx2 + Mx4 + Mx5 + Mx6 ) / 5 # 1x1
        self.Phiy3 = ( My1 + My2 + My4 + My5 + My6 ) / 5 # 1x1

        self.Phix4 = ( Mx1 + Mx2 + Mx3 + Mx5 + Mx6 ) / 5 # 1x1
        self.Phiy4 = ( My1 + My2 + My3 + My5 + My6 ) / 5 # 1x1

        self.Phix5 = ( Mx1 + Mx2 + Mx3 + Mx4 + Mx6 ) / 5 # 1x1
        self.Phiy5 = ( My1 + My2 + My3 + My4 + My6 ) / 5 # 1x1

        self.Phix6 = ( Mx1 + Mx2 + Mx3 + Mx4 + Mx5 ) / 5 # 1x1
        self.Phiy6 = ( My1 + My2 + My3 + My4 + My5 ) / 5 # 1x1

        observation_DQN = np.array([Mx1, My1, self.Phix1, self.Phiy1])

        self.action_input1 = action
        done = self.distance < self.distance_threshold
        done = bool(done)
        reward = -self.distanced

        return observation_DQN, reward, done, {}

    def reset(self):
        if self.sim_running:
            self.stop_simulation()
            # Stop Simulation
            sim.simxStopSimulation(clientID, sim.simx_opmode_oneshot_wait)

            # Retrieve some handles:

            ErrLocM1,LocM1 =sim.simxGetObjectHandle(clientID, 'robot1', sim.simx_opmode_oneshot_wait)

            if (not ErrLocM1==sim.simx_return_ok):
                pass

            ErrLocM2,LocM2 =sim.simxGetObjectHandle(clientID, 'robot2#0', sim.simx_opmode_oneshot_wait)

            if (not ErrLocM2==sim.simx_return_ok):
                pass

            ErrLoc1,Loc1 =sim.simxGetObjectPosition(clientID, LocM1, -1, sim.simx_opmode_oneshot_wait)

            if (not ErrLoc1==sim.simx_return_ok):
                pass

            ErrLoc2,Loc2 =sim.simxGetObjectPosition(clientID, LocM2, -1, sim.simx_opmode_oneshot_wait)

            if (not ErrLoc2==sim.simx_return_ok):
                pass

            ErrLocO1,OriRobo1 =sim.simxGetObjectOrientation(clientID,LocM1, -1, sim.simx_opmode_oneshot_wait)

            if (not ErrLocO1==sim.simx_return_ok):
                pass

            ErrLocO2,OriRobo2 =sim.simxGetObjectOrientation(clientID,LocM2, -1, sim.simx_opmode_oneshot_wait)

            if (not ErrLocO2==sim.simx_return_ok):
                pass

            OriRobo1[2] = scenes[self.scene][2]
            OriRobo2[2] = scenes[self.scene][5]

            # Set Robot Orientation

            sim.simxSetObjectOrientation(clientID, LocM1, -1, OriRobo1, sim.simx_opmode_oneshot_wait)
            sim.simxSetObjectOrientation(clientID, LocM2, -1, OriRobo2, sim.simx_opmode_oneshot_wait)

            Loc1[0] = scenes[self.scene][0]
            Loc2[0] = scenes[self.scene][3]

            Loc1[1] = scenes[self.scene][1]
            Loc2[1] = scenes[self.scene][4]

            # Set Robot Position

            sim.simxSetObjectPosition(clientID, LocM1, -1, Loc1, sim.simx_opmode_oneshot)
            sim.simxSetObjectPosition(clientID, LocM2, -1, Loc2, sim.simx_opmode_oneshot)

            # Nb of Scene Counter
            self.scene += 1

            " Use Adjacency Matrix to find Mxy and Phi's "

            A = np.ones(6) - np.identity(6) # Adjancency Matrix

            self.X = np.array([ [self.x1], [self.x2], [self.x3], [self.x4], [self.x5], [self.x6]  ]) #6x1
            self.Y = np.array([ [self.y1], [self.y2], [self.y3], [self.y4], [self.y5], [self.y6]  ]) #6x1

            Mx = np.zeros((6,1)) # 6x1
            My = np.zeros((6,1)) # 6x1

            for i in range(1,7):
                for j in range(1,7):
                    Mx[i-1] += (A[i-1][j-1])*(self.X[j-1] - self.X[i-1]) # 1x1 each
                    My[i-1] += (A[i-1][j-1])*(self.Y[j-1] - self.Y[i-1]) # 1x1 each

            Mx1 = float(Mx[0]) / 5 # 1x1
            My1 = float(My[0]) / 5 # 1x1

            Mx2 = float(Mx[1]) / 5 # 1x1
            My2 = float(My[1]) / 5 # 1x1

            Mx3 = float(Mx[2]) / 5 # 1x1
            My3 = float(My[2]) / 5 # 1x1

            Mx4 = float(Mx[3]) / 5 # 1x1
            My4 = float(My[3]) / 5 # 1x1

            Mx5 = float(Mx[4]) / 5 # 1x1
            My5 = float(My[4]) / 5 # 1x1

            Mx6 = float(Mx[5]) / 5 # 1x1
            My6 = float(My[5]) / 5 # 1x1

            self.Phix1 = ( Mx2 + Mx3 + Mx4 + Mx5 + Mx6 ) / 5 # 1x1
            self.Phiy1 = ( My2 + My3 + My4 + My5 + My6 ) / 5 # 1x1

            self.Phix2 = ( Mx1 + Mx3 + Mx4 + Mx5 + Mx6 ) / 5 # 1x1
            self.Phiy2 = ( My1 + My3 + My4 + My5 + My6 ) / 5 # 1x1

            self.Phix3 = ( Mx1 + Mx2 + Mx4 + Mx5 + Mx6 ) / 5 # 1x1
            self.Phiy3 = ( My1 + My2 + My4 + My5 + My6 ) / 5 # 1x1

            self.Phix4 = ( Mx1 + Mx2 + Mx3 + Mx5 + Mx6 ) / 5 # 1x1
            self.Phiy4 = ( My1 + My2 + My3 + My5 + My6 ) / 5 # 1x1

            self.Phix5 = ( Mx1 + Mx2 + Mx3 + Mx4 + Mx6 ) / 5 # 1x1
            self.Phiy5 = ( My1 + My2 + My3 + My4 + My6 ) / 5 # 1x1

            self.Phix6 = ( Mx1 + Mx2 + Mx3 + Mx4 + Mx5 ) / 5 # 1x1
            self.Phiy6 = ( My1 + My2 + My3 + My4 + My5 ) / 5 # 1x1

            observation_DQN = np.array([Mx1, My1, self.Phix1, self.Phiy1])

            # Start Simulation
            sim.simxStartSimulation(clientID, sim.simx_opmode_oneshot_wait)
            time.sleep(5)

        return observation_DQN

    def render(self):
        pass

    def close(self):
        vrep_env.VrepEnv.close(self)


text:
#!/usr/bin/env python

import numpy as np
import pathlib

from blastsight.view.viewer import Viewer

"""
In this demo, we'll show how you can animate a cross-section so you can simulate that you're scanning a mine.

A Cross-Section is equivalent as slicing a mesh, but without returning the vertices of each slice.
This means that we can easily generate a cross-section of all the rendered meshes in real time.

Internally, it's a visual effect, which means we have to manually activate the cross-section mode
with 'v.set_cross_section(True). This activates the cross-section with default values for the plane.

You can change the plane description with v.cross_section(origin, normal), where 'origin' is a point
on the plane, and 'normal' is the normal of the plane.

To deactivate the cross-section, just use 'v.set_cross_section(False)'.

"""

v = Viewer()
mesh_path = f'{pathlib.Path(__file__).parent.parent}/test_files/caseron.off'
block_path = f'{pathlib.Path(__file__).parent.parent}/test_files/rainbow.csv'

mesh = v.load_mesh(mesh_path, color=[1.0, 0.8, 0.0])
blocks = v.load_blocks(block_path)
low, high = v.bounding_box()

def scan(origin):
    v.cross_section(origin, np.array([1.0, 0.0, 0.0]))

def scan_left(*args, **kwargs):
    v.signal_animation_finished.disconnect()

    v.animate(high, low, scan, duration=2000, steps=60)
    v.signal_animation_finished.connect(scan_right)

def scan_right(*args, **kwargs):
    v.signal_animation_finished.disconnect()

    v.animate(low, high, scan, duration=2000, steps=60)
    v.signal_animation_finished.connect(scan_left)

# Hack to disconnect before starting
v.signal_animation_finished.connect(lambda: None)

# Mark all meshes and blocks as cross-sectionable
v.set_cross_section(True)

# Add the same mesh and block model, but without cross-sectioning, for comparison
v.load_mesh(mesh_path, color=[1.0, 0.8, 0.0], alpha=0.3)
v.load_blocks(block_path, alpha=0.3)

# Start animation
scan_right()

# Show viewer
v.show()

max_stars_repo_name: gsanhueza/BlastSight

max_issues_repo_name: gsanhueza/BlastSight

max_forks_repo_name: gsanhueza/BlastSight


text:
import random
import numpy as np
import math
import time

def makeTSP(nCities):
    positions = 2*np.random.rand(nCities, 2)-1
    distance = np.zeros((nCities, nCities))

    for i in range(nCities):
        for j in range(nCities):
            distance[i, j] = math.sqrt(
                (positions[i, 0] - positions[j, 0])**2 + (positions[i, 1] - positions[j, 1])**2)
            distance[j, i] = distance[i, j]
        return distance

def permutation(order):
    order = tuple(order)
    if len(order) == 1:
        yield order
    else:
        for i in range(len(order)):
            rest = order[:i] + order[i+1:]
            move = (order[i],)
            for smaller in permutation(rest):
                yield move + smaller

def exhaustive(distance):
    nCities = np.shape(distance)[0]

    cityOrder = np.arange(nCities)

    distanceTravelled = 0
    for i in range(nCities-1):
        distanceTravelled += distance[cityOrder[i], cityOrder[i+1]]
    distanceTravelled += distance[cityOrder[nCities-1], 0]

    for newOrder in permutation(range(nCities)):
        possibleDistanceTravelled = 0
        for i in range(nCities-1):
            possibleDistanceTravelled += distance[newOrder[i], newOrder[i+1]]
        possibleDistanceTravelled += distance[newOrder[nCities-1], 0]

        if possibleDistanceTravelled < distanceTravelled:
            distanceTravelled = possibleDistanceTravelled
            cityOrder = newOrder
    return cityOrder, distanceTravelled

def hillClimbing(distance):

    nCities = np.shape(distance)[0]

    cityOrder = np.arange(nCities)
    random.shuffle(cityOrder)

    distanceTravelled = 0
    for i in range(nCities-1):
        distanceTravelled += distance[cityOrder[i], cityOrder[i+1]]
    distanceTravelled += distance[cityOrder[nCities-1], 0]

    for i in range(1000):
        # Choose cities to swap
        city1 = np.random.randint(nCities)
        city2 = np.random.randint(nCities)

        if city1 != city2:
            # Reorder the set of cities
            possibleCityOrder = cityOrder.copy()
            possibleCityOrder = np.where(
                possibleCityOrder == city1, -1, possibleCityOrder)
            possibleCityOrder = np.where(
                possibleCityOrder == city2, city1, possibleCityOrder)
            possibleCityOrder = np.where(
                possibleCityOrder == 1, city2, possibleCityOrder)

            # Work out the new distance
            # This can be done more efficiently
            newDistanceTravelled = 0
            for j in range(nCities-1):
                newDistanceTravelled += distance[possibleCityOrder[j],
                                                 possibleCityOrder[j+1]]
            distanceTravelled += distance[cityOrder[nCities-1], 0]

            if newDistanceTravelled < distanceTravelled:
                distanceTravelled = newDistanceTravelled
                cityOrder = possibleCityOrder
    return cityOrder, distanceTravelled

def runAll():
    nCities = 5
    distance = makeTSP(nCities)

    print("Exhaustive search")
    start = time.time()
    print(exhaustive(distance))
    finish = time.time()
    print(finish-start)
    print("")
    print("Hill Climbing")
    start = time.time()
    print(hillClimbing(distance))
    finish = time.time()
    print(finish-start)

runAll()


text:
#!/usr/bin/env python

# stdlib imports
import os.path
import warnings

# third party imports
import numpy as np

# local imports
from gmprocess.io.geonet.core import read_geonet
from gmprocess.metrics.station_summary import StationSummary
from gmprocess.io.test_utils import read_data_dir

def test_pga():
    datafiles, _ = read_data_dir(
        'geonet', 'us1000778i', '20161113_110259_WTMC_20.V2A')
    datafile_v2 = datafiles[0]
    stream_v2 = read_geonet(datafile_v2)[0]
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        station_summary = StationSummary.from_stream(stream_v2,
                                                     ['channels', 'greater_of_two_horizontals', 'gmrotd50',
                                                      'gmrotd100', 'gmrotd0'],
                                                     ['pga', 'sa1.0', 'saincorrect'])
    pga_df = station_summary.pgms[station_summary.pgms.IMT == 'PGA']
    HN1 = pga_df[pga_df.IMC == 'H1'].Result.iloc[0]
    HN2 = pga_df[pga_df.IMC == 'H2'].Result.iloc[0]
    HNZ = pga_df[pga_df.IMC == 'Z'].Result.iloc[0]
    greater = pga_df[pga_df.IMC == 'GREATER_OF_TWO_HORIZONTALS'].Result.iloc[0]
    np.testing.assert_almost_equal(
        HN2, 81.28979591836733, decimal=1)
    np.testing.assert_almost_equal(
        HN1, 99.3173469387755, decimal=1)
    np.testing.assert_almost_equal(
        HNZ, 183.89693877551022, decimal=1)
    np.testing.assert_almost_equal(greater, 99.3173469387755, decimal=1)

if __name__ == '__main__':
    test_pga()

max_stars_repo_name: norfordb/groundmotion

max_issues_repo_name: norfordb/groundmotion

max_forks_repo_name: norfordb/groundmotion


text:
import logging
import mxnet as mx
import numpy as np
import cv2 as cv
import pickle
import os, sys
from collections import namedtuple
import time

Batch = namedtuple('Batch', ['data'])

class TinyFacesDetector:
    def __init__(self, model_root='./', prob_thresh=0.5, gpu_idx=0, cudnn_autotune=False, verbose=False):
        if verbose:
            logging.basicConfig(level = logging.INFO, format = '[INFO]:  %(message)s', handlers = [logging.StreamHandler()])
        logging.info('Loading detector...')
        os.environ['MXNET_CUDNN_AUTOTUNE_DEFAULT']=str(int(cudnn_autotune))
        self.MAX_INPUT_DIM=5000.0
        self.prob_thresh = prob_thresh
        self.nms_thresh = 0.1

        sym, arg_params, aux_params = mx.model.load_checkpoint(os.path.join(model_root, 'hr101'),0)
        all_layers = sym.get_internals()

        meta_file = open(os.path.join(model_root, 'meta.pkl'), 'rb')
        self.clusters = pickle.load(meta_file)
        self.averageImage = pickle.load(meta_file)
        meta_file.close()
        self.clusters_h = self.clusters[:,3] - self.clusters[:,1] + 1
        self.clusters_w = self.clusters[:,2] - self.clusters[:,0] + 1
        self.normal_idx = np.where(self.clusters[:,4] == 1)

        context=mx.gpu(gpu_idx)
        self.mod = mx.mod.Module(symbol=all_layers['fusex_output'], context=context, data_names=['data'], label_names=None)
        self.mod.bind(for_training=False, data_shapes=[('data', (1, 3, 224, 224))], label_shapes=None, force_rebind=False)
        self.mod.set_params(arg_params=arg_params, aux_params=aux_params, force_init=False)
        logging.info('Done loading.')

    @staticmethod
    def nms(dets, prob_thresh):
        x1 = dets[:, 0]
        y1 = dets[:, 1]
        x2 = dets[:, 2]
        y2 = dets[:, 3]
        scores = dets[:, 4]

        areas = (x2 - x1 + 1) * (y2 - y1 + 1)

        order = scores.argsort()[::-1]

        keep = []
        while order.size > 0:
            i = order[0]
            keep.append(i)
            xx1 = np.maximum(x1[i], x1[order[1:]])
            yy1 = np.maximum(y1[i], y1[order[1:]])
            xx2 = np.minimum(x2[i], x2[order[1:]])
            yy2 = np.minimum(y2[i], y2[order[1:]])
            w = np.maximum(0.0, xx2 - xx1 + 1)
            h = np.maximum(0.0, yy2 - yy1 + 1)
            inter = w * h

            ovr = inter / (areas[i] + areas[order[1:]] - inter)
            inds = np.where(ovr <= prob_thresh)[0]

            order = order[inds + 1]
        return keep

    def detect(self, img):
        raw_img = img
        raw_h = raw_img.shape[0]
        raw_w = raw_img.shape[1]
        raw_img = cv.cvtColor(raw_img, cv.COLOR_BGR2RGB)
        raw_img_f = raw_img.astype(np.float32)
        min_scale = min(np.floor(np.log2(np.max(self.clusters_w[self.normal_idx]/raw_w))), np.floor(np.log2(np.max(self.clusters_h[self.normal_idx]/raw_h))))
        max_scale = min(1.0, -np.log2(max(raw_h, raw_w)/self.MAX_INPUT_DIM))
        scales_down = np.arange(min_scale, 0+0.0001, 1.)
        scales_up = np.arange(0.5, max_scale+0.0001, 0.5)
        scales_pow = np.hstack((scales_down, scales_up))
        scales = np.power(2.0, scales_pow)

        start = time.time()
        bboxes = np.empty(shape=(0,5))
        for s in scales[::-1]:
            img = cv.resize(raw_img_f, (0,0), fx = s, fy = s)
            img = np.transpose(img,(2,0,1))
            img = img - self.averageImage

            tids = []
            if s <= 1. :
                tids = list(range(4, 12))
            else :
                tids = list(range(4, 12)) + list(range(18, 25))
            ignoredTids = list(set(range(0,self.clusters.shape[0]))-set(tids))
            img_h = img.shape[1]
            img_w = img.shape[2]
            img = img[np.newaxis, :]

            self.mod.reshape(data_shapes=[('data', (1, 3, img_h, img_w))])
            self.mod.forward(Batch([mx.nd.array(img)]))
            self.mod.get_outputs()[0].wait_to_read()
            fusex_res = self.mod.get_outputs()[0]

            score_cls = mx.nd.slice_axis(fusex_res, axis=1, begin=0, end=25, name='score_cls')
            score_reg = mx.nd.slice_axis(fusex_res, axis=1, begin=25, end=None, name='score_reg')
            prob_cls = mx.nd.sigmoid(score_cls)

            prob_cls_np = prob_cls.asnumpy()
            prob_cls_np[0,ignoredTids,:,:] = 0.

            _, fc, fy, fx = np.where(prob_cls_np > self.prob_thresh)

            cy = fy * 8 - 1
            cx = fx * 8 - 1
            ch = self.clusters[fc, 3] - self.clusters[fc,1] + 1
            cw = self.clusters[fc, 2] - self.clusters[fc, 0] + 1

            Nt = self.clusters.shape[0]

            score_reg_np = score_reg.asnumpy()
            tx = score_reg_np[0, 0:Nt, :, :]
            ty = score_reg_np[0, Nt:2*Nt,:,:]
            tw = score_reg_np[0, 2*Nt:3*Nt,:,:]
            th = score_reg_np[0,3*Nt:4*Nt,:,:]

            dcx = cw * tx[fc, fy, fx]
            dcy = ch * ty[fc, fy, fx]
            rcx = cx + dcx
            rcy = cy + dcy
            rcw = cw * np.exp(tw[fc, fy, fx])
            rch = ch * np.exp(th[fc, fy, fx])

            score_cls_np = score_cls.asnumpy()
            scores = score_cls_np[0, fc, fy, fx]

            tmp_bboxes = np.vstack((rcx-rcw/2, rcy-rch/2, rcx+rcw/2,rcy+rch/2))
            tmp_bboxes = np.vstack((tmp_bboxes/s, scores))
            tmp_bboxes = tmp_bboxes.transpose()
            bboxes = np.vstack((bboxes, tmp_bboxes))

        logging.info("Detection time: " + str(time.time()-start) + "secs.")
        refind_idx = self.nms(bboxes, self.nms_thresh)
        refind_bboxes = bboxes[refind_idx]
        refind_bboxes = refind_bboxes.astype(np.int32)

        return refind_bboxes


text:
import cv2
import numpy as np
import time

class CaptureManager(object):
    def __init__(self, capture, preview_window_manager=None, should_mirror_preview = False):

        self.preview_window_manager = preview_window_manager
        self.should_mirror_preview = should_mirror_preview

        self._capture = capture
        self._channel = 0
        self._entered_frame = False
        self._frame = None
        self._image_filename = None
        self._video_filename = None
        self._video_encoding = None
        self._video_writer = None
        self._start_time = None
        self._frames_elapsed = long(0)
        self._fps_est = None

    @property
    def channel(self):
        return self._channel

    @channel.setter
    def channel(self):
        return self._channel

    @property
    def frame(self):
        if self._entered_frame and self._frame is None:
            _, self._frame = self._capture.retrieve(channel=self.channel)
        return self._frame

    @property
    def is_writing_image(self):
        return self._image_filename is not None

    @property
    def is_writing_video(self):
        return self._video_filename is not None

    def enter_frame(self):
        # capture the next frame

        assert not self._entered_frame, 'previous enter_frame() had no matching exit_frame()'
        if self._capture is not None:
            self._entered_frame = self._capture.grab()

    def exit_frame(self):
        # draw to window, write to files, release the frame

        # frame is retrievable or not
        if self.frame is None:
            self._entered_frame = False
            return

        if self._frames_elapsed == 0:
            self._start_time = time.time()
        else:
            time_elapsed = time.time() - self._start_time
            self._fps_est = self._frames_elapsed / time_elapsed
        self._frames_elapsed += 1

        # draw
        if self.preview_window_manager is not None:
            if self.should_mirror_preview:
                mirrored_frame = np.fliplr(self._frame).copy()
                self.preview_window_manager.show(mirrored_frame)
            else:
                self.preview_window_manager.show(self._frame)

        # write to img
        if self.is_writing_image:
            cv2.imwrite(self._image_filename, self._frame)
            self._image_filename = None

        # write to video
        self._write_video_frame()

        # release the frame
        self._frame = None
        self._entered_frame = False

    def write_image(self, filename):
        self._image_filename = filename

    def start_writing_video(self, filename, encoding = cv2.cv.CV_FOURCC('I', '4', '2', '0')):
        self._video_filename = filename
        self._video_encoding = encoding

    def stop_writing_video(self):
        self._video_filename = None
        self._video_encoding = None
        self._video_writer = None

    def _write_video_frame(self):
        if not self.is_writing_video:
            return

        if self._video_writer is None:
            fps = self._capture.get(cv2.cv.CV_CAP_PROP_FPS)
            if fps == 0.0:
                if self._frames_elapsed < 20:
                    return

                else:
                    fps = self._fps_est

            size = (int(self._capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
                    int(self._capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))

            self._video_writer = cv2.VideoWriter(self._video_filename, self._video_encoding, fps, size)
            self._video_writer.write(self._frame)

class WindowManager(object):

    def __init__(self, window_name, keypress_callback = None):
        self.keypress_callback = keypress_callback
        self._window_name = window_name
        self._is_window_created = False

    @property
    def is_window_created(self):
        return self._is_window_created

    def create_window(self):
        cv2.namedWindow(self._window_name)
        self._is_window_created = True

    def show(self, frame):
        cv2.imshow(self._window_name, frame)

    def destroy_window(self):
        cv2.destroyWindow(self._window_name)
        self._is_window_created = False

    def process_events(self):
        keykode = cv2.waitKey(1)
        if self.keypress_callback is not None and keykode != -1:
            keykode &= 0xFF
            self.keypress_callback(keykode)


text:
import dataloader
from sklearn.svm import LinearSVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
import numpy as np
import random

if __name__ == "__main__":
    dtrain, dval, dtest = dataloader.load('material_dataset.txt', 'alex_fea1.npz')

    out = open('log.txt', 'w')

    for i in range(10):
        cc = 10 ** np.random.uniform(-2, 2)
        inter = 10 ** np.random.uniform(0, 2)
        pen = 'l1' if np.random.randint(2) == 0 else 'l2'
        loss = 'squared_hinge'

        model = LinearSVC(penalty=pen, loss=loss, dual=False, C=cc, intercept_scaling=inter, verbose=1, max_iter=1000)
        # model = KNeighborsClassifier(n_neighbors=5, n_jobs=4)
        # model = DecisionTreeClassifier()

        # dtrain = list(zip(dtrain[0], dtrain[1]))
        # random.shuffle(dtrain)
        # dtrain =list(zip(*dtrain))

        model.fit(dtrain[0], dtrain[1])
        pre_val = model.predict(dval[0])
        pre_test = model.predict(dtest[0])

        acc_val = (np.sum(pre_val == dval[1]), len(dval[1]))
        acc_test = (np.sum(pre_test == dtest[1]), len(dtest[1]))
        print(cc, inter, pen, loss, acc_val, acc_test, sep='\t', file=out)


text:
from datetime import timedelta
from statsmodels.tsa.holtwinters import ExponentialSmoothing
import pandas as pd
import numpy as np

from Helper import helper

class HW(object):

    def __init__(self, dataframe):
        df = dataframe.copy()
        self.data = helper.HW_Temp_data_cleaning(df)
        self.name = 'HW'

    def set_date(self, date):
        self.date = date

    def model_building(self, training_data, station=None):
        model = ExponentialSmoothing(training_data, seasonal='add', seasonal_periods=24 * 365)
        model_fit = model.fit()
        return model_fit

    def model_selection_mape_rmse(self, station):
        datetime = pd.to_datetime(self.date) - timedelta(days=2)
        train_end = datetime + timedelta(hours=7)
        test_start = datetime + timedelta(hours=8)
        test_end = datetime + timedelta(days=1) + timedelta(hours=23)

        train = self.data[station + '_Temp'].loc[:train_end]
        test = self.data[station + '_Temp'].loc[test_start:test_end]
        model = self.model_building(train, station)
        yhat = model.forecast(40)
        # yhat.index = test.index
        # self.mape = abs((yhat - test) / test).mean()
        # self.rmse = np.sqrt(((yhat - test) ** 2).mean())
        self.mape = helper.mape(test, yhat)
        self.rmse = helper.rmse(test, yhat)

    def predict_next_40hours_temp(self, station):
        train_end2 = pd.to_datetime(self.date) + timedelta(hours=7)
        train2 = self.data[station + '_Temp'].loc[:train_end2]
        model2 = self.model_building(train2, station)
        y_predict = model2.forecast(40)
        self.forecast = y_predict.to_list()
        return self.forecast

if __name__ == '__main__':
    path = '../../Data/Hourly_Temp_Humi_Load-6.csv'
    df = pd.read_csv(path)
    model_DR = HW(df)
    model_DR.set_date('2018-09-01')
    station = 'TRM'
    model_DR.model_selection_mape_rmse(station)
    print(f'MAPE: {model_DR.mape}, RMSE: {model_DR.rmse}')
    model_DR.predict_next_40hours_temp(station)
    print(model_DR.forecast)

max_stars_repo_name: yinghonglin/Projects

max_issues_repo_name: yinghonglin/Projects

max_forks_repo_name: yinghonglin/Projects

reason: import numpy,from statsmodels


text:
from typing import List, Union, Optional

import torch
from typing import Type
from ._interfaces import _ABCSelector, _ABCTorchModel
from ._owlqn import OWLQN

import warnings
import multiprocessing
import numpy as np

import scipy.stats
from sklearn.decomposition import PCA

from ._utils import TicToc, VerbosePrint
from ._tsne_torch_models import _RegTsneModel, _StratifiedRegTsneModel # , _SimpleRegTsneModel

class TsneL1(_ABCSelector):
    def __init__(self, *, w: Union[float, str, list, np.ndarray] = 'ones',
                 lasso: float = 1e-4, n_pcs: Optional[int] = None, perplexity: float = 30.,
                 use_beta_in_Q: bool = True,
                 max_outer_iter: int = 5, max_inner_iter: int = 20, owlqn_history_size: int = 100,
                 eps: float = 1e-12, verbosity: int = 2, torch_precision: Union[int, str, torch.dtype] = 32,
                 torch_cdist_compute_mode: str = "use_mm_for_euclid_dist",
                 t_distr: bool = True, n_threads: int = 1, use_gpu: bool = False, pca_seed=0, ridge=0.):
        """
        TsneL1 model

        :param w: initial value of w, weight of each marker. Acceptable values are 'ones' (all 1),
            'uniform' (random [0, 1] values), float numbers (all set to that number),
            or a list or numpy array with specific numbers.
        :param lasso: lasso strength
        :param n_pcs: Number of PCs used to generate P matrix. Skip PCA if set to `None`.
        :param perplexity: perplexity of t-SNE modeling
        :param use_beta_in_Q: whether to use the cell specific sigma^2 calculated from P in Q. (1 / beta)
        :param max_outer_iter: number of iterations of OWL-QN
        :param max_inner_iter: number of iterations inside OWL-QN
        :param owlqn_history_size: history size for OWL-QN.
        :param eps: epsilon for considering a value to be 0.
        :param verbosity: verbosity level (0 ~ 2).
        :param torch_precision: The dtype used inside torch model. By default, tf.float32 (a.k.a. tf.float) is used.
            However, if precision become an issue, tf.float64 may be worth trying. You can input 32, "32", 64, or "64".
        :param torch_cdist_compute_mode: cdist_compute_mode: compute mode for torch.cdist. By default,
            "use_mm_for_euclid_dist" to (daramatically) improve performance. However, if numerical stability became an
            issue, "donot_use_mm_for_euclid_dist" may be used instead. This option does not affect distances computed
            outside of pytorch, e.g., matrix P. Only matrix Q is affect.
        :param t_distr: By default, use t-distribution (1. / (1. + pdist2) for Q.
            Use Normal distribution instead (exp(-pdist2)) if set to False
        :param n_threads: number of threads (currently only for calculating P and beta)
        :param use_gpu: whether to use GPU to train the model.
        """
        super(TsneL1, self).__init__(verbosity)
        self._max_outer_iter = max_outer_iter
        self._max_inner_iter = max_inner_iter
        self._owlqn_history_size = owlqn_history_size
        self._n_pcs = n_pcs
        self.w = w
        self._lasso = lasso
        self._eps = eps
        self._use_beta_in_Q = use_beta_in_Q
        self._perplexity = perplexity
        self._torch_precision = torch_precision
        self._torch_cdist_compute_mode = torch_cdist_compute_mode
        self._t_distr = t_distr
        self._n_threads = n_threads
        self._use_gpu = use_gpu
        self._pca_seed = pca_seed
        self._ridge = ridge

    def fit(self, X, *, X_teacher=None, batches=None, P=None, beta=None, must_keep=None):
        """
        Select markers from one dataset to keep the cell-cell similarities in the same dataset

        :param X: data matrix (cells (rows) x genes/proteins (columns))
        :param X_teacher: get target similarities from this dataset
        :param batches: (optional) batch labels
        :param P: The P matrix, if calculated in advance
        :param beta: The beta associated with P, if calculated in advance
        :param must_keep: A boolean vector indicating if a feature must be kept.
            Those features will have a fixed weight 1.
        :return:
        """
        tictoc = TicToc()

        if X_teacher is None: # if there is no other assay to mimic, just mimic itself
            X_teacher = X

        if batches is None:
            if must_keep is None and (isinstance(self._lasso, float) or isinstance(self._lasso, str)):
                model_class = _RegTsneModel  # _SimpleRegTsneModel
            else:
                model_class = _RegTsneModel
            if self._n_pcs is None:
                P, beta = self._resolve_P_beta(X_teacher, P, beta, self._perplexity, tictoc, self.verbose_print.prints,
                                               self._n_threads)
            else:
                pcs = PCA(self._n_pcs, random_state=self._pca_seed).fit_transform(X_teacher)
                P, beta = self._resolve_P_beta(pcs, P, beta, self._perplexity, tictoc, self.verbose_print.prints,
                                               self._n_threads)
        else:
            model_class = _StratifiedRegTsneModel
            if P is None:
                X, P, beta = self._resolve_batches(X_teacher, None, batches, self._n_pcs, self._perplexity, tictoc,
                                                   self.verbose_print, self._n_threads)

        return self._fit_core(X, P, beta, must_keep, model_class, tictoc)

    @classmethod
    def tune(cls, target_n_features, X=None, *, X_teacher=None, batches=None,
             P=None, beta=None, must_keep=None, perplexity=30., n_pcs=None, w='ones',
             min_lasso=1e-8, max_lasso=1e-2, tolerance=0, smallest_log10_fold_change=0.1, max_iter=100,
             return_P_beta=False, n_threads=6,
             **kwargs):
        """
        Automatically find proper lasso strength that returns the preferred number of markers

        :param target_n_features: number of features
        :param return_P_beta: controls what to return
        :param kwargs: all other parameters are the same for a TsneL1 model or :func:`TsneL1.fit`.
        :return: if return_P_beta is True and there are batches, (model, X, P, beta);
                 if return_P_beta is True and there is no batches, (model, P, beta);
                 otherwise, only model by default.
        """
        if "lasso" in kwargs:
            raise ValueError("Parameter lasso should be substituted by max_lasso and min_lasso to set a range.")
        if "verbosity" in kwargs:
            verbosity = kwargs['verbosity']
        else:
            verbosity = 3
        verbose_print = VerbosePrint(verbosity)
        tictoc = TicToc()

        n_features = X.shape[1]

        # initialize w
        if isinstance(w, float) or isinstance(w, int):
            w = np.zeros([1, n_features]) + w
        elif isinstance(w, str) and w == 'uniform':
            w = np.random.uniform(size=[1, n_features])
        elif isinstance(w, str) and w == 'ones':
            w = np.ones([1, n_features])
        else:
            w = np.array(w).reshape([1, n_features])

        max_log_lasso = np.log10(max_lasso)
        min_log_lasso = np.log10(min_lasso)

        if X_teacher is None: # if there is no other assay to mimic, just mimic itself
            X_teacher = X

        if batches is None:
            model_class = _RegTsneModel
            if n_pcs is None:
                P, beta = cls._resolve_P_beta(X_teacher, P, beta, perplexity, tictoc, verbose_print.prints, n_threads)
            else:
                pcs = PCA(n_pcs).fit_transform(X_teacher)
                P, beta = cls._resolve_P_beta(pcs, P, beta, perplexity, tictoc, verbose_print.prints, n_threads)
        else:
            model_class = _StratifiedRegTsneModel
            if P is None:
                X, P, beta = cls._resolve_batches(X_teacher, None, batches, n_pcs, perplexity, tictoc, verbose_print,
                                                  n_threads)

        sup = n_features
        inf = 0

        model = None
        for it in range(max_iter):
            log_lasso = max_log_lasso / 2 + min_log_lasso / 2
            verbose_print(0, "Iteration", it, "with lasso =", 10 ** log_lasso,
                          "in [", 10 ** min_log_lasso, ",", 10 ** max_log_lasso, "]...", end=" ")
            model = cls(w=w, lasso=10 ** log_lasso, n_pcs=n_pcs, perplexity=perplexity, **kwargs)
            n = model._fit_core(X, P, beta, must_keep, model_class, tictoc).get_mask().sum()
            verbose_print(0, "Done. Number of features:", n, ".", tictoc.toc())
            if np.abs(n - target_n_features) <= tolerance:  # Good number of features, return
                break

            if it > 0 and np.abs(log_lasso - prev_log_lasso) < smallest_log10_fold_change:
                warnings.warn("smallest_log10_fold_change reached before achieving target number of features.")
                break

            prev_log_lasso = log_lasso

            if n > target_n_features:  # Too many features, need more l1 regularization
                if n <= sup:
                    sup = n
                else:
                    warnings.warn("Monotonicity is violated. Value larger than current supremum. "
                                  "Binary search may fail. "
                                  "Consider use more max_outer_iter (default: 5) and max_inner_iter (default: 20).")
                min_log_lasso = log_lasso
            elif n < target_n_features:  # Too few features, need less l1 regularization
                if n >= inf:
                    inf = n
                else:
                    warnings.warn("Monotonicity is violated. Value lower than current infimum. "
                                  "Binary search may fail. "
                                  "Consider use more max_outer_iter (default: 5) and max_inner_iter (default: 20).")
                max_log_lasso = log_lasso
        else:  # max_iter reached
            warnings.warn("max_iter before reached achieving target number of features.")

        if return_P_beta:
            if batches is None:
                return model, P, beta
            else:
                return model, X, P, beta
        else:
            return model

    def get_mask(self):
        """
        Get the feature selection mask.
        For AnnData in scanpy, it can be used as adata[:, model.get_mask()]

        :return: mask
        """
        return self.w > self._eps

    def transform(self, X):
        """
        Shrink a matrix / AnnData object with full markers to the selected markers only.
        If such operation is not supported by your data object,
        you can do it manually using :func:`~TsneL1.get_mask`.

        :param X: Matrix / AnnData to be shrunk
        :return: Shrunk matrix / Anndata
        """
        # if mask_only:
        return X[:, self.get_mask()]
        # else:
        #    return X[:, self.get_mask()] * self.w[self.get_mask()]

    def fit_transform(self, X, **kwargs):
        """
        Fit on a matrix / AnnData and then transfer it.

        :param X: The matrix / AnnData to be transformed
        :param kwargs: Other parameters for :func:`TsneL1.fit`.
        :return: Shrunk matrix / Anndata
        """
        return self.fit(X, **kwargs).transform(X)

    @staticmethod
    def _resolve_P_beta(X, P, beta, perplexity, tictoc, print_callbacks, n_threads):
        if P is None and beta is None:
            print_callbacks[0]("Calculating distance matrix and scaling factors...")
            P, beta = TsneL1._x2p(X, perplexity=perplexity, print_callback=print_callbacks[1], n_threads=n_threads)
            print_callbacks[0]("Done.", tictoc.toc())
        elif P is None and beta is not None:
            print_callbacks[0]("Calculating distance matrix...")
            P = TsneL1._x2p_given_beta(X, beta)
            print_callbacks[0]("Done.", tictoc.toc())

        return P, beta

    @staticmethod
    def _resolve_batches(X, beta, batches, n_pcs, perplexity, tictoc, verbose_print, n_threads):
        batches = np.array(batches)
        batch_names = np.unique(batches)
        Xs = []
        Ps = []
        betas = []
        for batch in batch_names:
            batch_mask = (batches == batch)
            verbose_print(0, "Batch", batch, "with", sum(batch_mask), "instances.")

            Xs.append(X[batch_mask, :])
            if n_pcs is None:
                if beta is not None:
                    new_beta = beta[batches == batch]
                else:
                    new_beta = None
                P, new_beta = TsneL1._resolve_P_beta(Xs[-1], None, new_beta, perplexity, tictoc, verbose_print.prints, n_threads)
            else:
                pcs = PCA(n_pcs).fit_transform(Xs[-1])
                P, new_beta = TsneL1._resolve_P_beta(pcs, None, None, perplexity, tictoc, verbose_print.prints, n_threads)
            Ps.append(P)
            betas.append(new_beta)
        return Xs, Ps, betas

    def _fit_core(self, X, P, beta, must_keep, model_class: Type[_ABCTorchModel], tictoc):

        self.verbose_print(0, "Optimizing...")
        if self._use_beta_in_Q:
            model = model_class(P, X, self.w, beta, self._torch_precision, self._torch_cdist_compute_mode,
                                self._t_distr, must_keep, self._ridge)
        else:
            model = model_class(P, X, self.w, None, self._torch_precision, self._torch_cdist_compute_mode,
                                self._t_distr, must_keep, self._ridge)

        if self._use_gpu:
            model.use_gpu()

        optimizer = OWLQN(model.parameters(), lasso=self._lasso, line_search_fn="strong_wolfe",
                          max_iter=self._max_inner_iter, history_size=self._owlqn_history_size)

        for t in range(self._max_outer_iter):
            def closure():
                if torch.is_grad_enabled():
                    optimizer.zero_grad()
                loss = model.forward()
                if loss.requires_grad:
                    loss.backward()

                return loss

            loss = optimizer.step(closure)
            self.verbose_print(1, t, 'loss:', loss.item(), "Nonzero:", (np.abs(model.get_w0()) > self._eps).sum(),
                               tictoc.toc())
            self.w = model.get_w()

        loss = model.forward()
        self.verbose_print(1, 'final', 'loss:', loss.item(), "Nonzero:", (np.abs(model.get_w0()) > self._eps).sum(),
                           tictoc.toc())

        return self

    @staticmethod
    def _Hbeta(D=np.array([]), beta=1.0):
        """
            Compute the perplexity and the P-row for a specific value of the
            precision of a Gaussian distribution.
        """

        # Compute P-row and corresponding perplexity
        P = np.exp(-D.copy() * beta)
        sumP = sum(P)
        P = P / sumP
        H = scipy.stats.entropy(P)
        return H, P

    @staticmethod
    def _x2p(X=np.array([]), tol=1e-5, perplexity=30.0, print_callback=print, *, n_threads):
        """
            Performs a binary search to get P-values in such a way that each
            conditional Gaussian has the same perplexity.
        """
        if n_threads > 1:
            return TsneL1._x2p_parallel(X, tol, perplexity, print_callback, n_threads)

        # Initialize some variables
        print_callback("Computing pairwise distances...")
        (n, d) = X.shape
        sum_X = np.sum(np.square(X), 1)
        D = np.add(np.add(-2 * np.dot(X, X.T), sum_X).T, sum_X)
        P = np.zeros((n, n))
        beta = np.ones((n, 1))
        logU = np.log(perplexity)

        # Loop over all datapoints
        for i in range(n):

            # Print progress
            if i % 500 == 0:
                print_callback("Computing P-values for point %d of %d..." % (i, n))

            # Compute the Gaussian kernel and entropy for the current precision
            betamin = -np.inf
            betamax = np.inf
            Di = D[i, np.concatenate((np.r_[0:i], np.r_[i + 1:n]))]
            (H, thisP) = TsneL1._Hbeta(Di, beta[i])

            # Evaluate whether the perplexity is within tolerance
            Hdiff = H - logU
            tries = 0

            while (not np.abs(Hdiff) < tol) and tries < 50:
                # If not, increase or decrease precision
                if Hdiff > 0:
                    betamin = beta[i].copy()
                    if betamax == np.inf or betamax == -np.inf:
                        beta[i] = beta[i] * 2.
                    else:
                        beta[i] = (beta[i] + betamax) / 2.
                else:
                    betamax = beta[i].copy()
                    if betamin == np.inf or betamin == -np.inf:
                        beta[i] = beta[i] / 2.
                    else:
                        beta[i] = (beta[i] + betamin) / 2.

                # Recompute the values
                (H, thisP) = TsneL1._Hbeta(Di, beta[i])
                Hdiff = H - logU
                tries += 1

            # Set the final row of P
            P[i, np.concatenate((np.r_[0:i], np.r_[i + 1:n]))] = thisP

        # Return final P-matrix
        print_callback("Mean value of sigma: %f" % np.mean(np.sqrt(1 / beta)))
        return P, beta

    @staticmethod
    def _x2p_given_beta(X, beta):
        (n, d) = X.shape
        sum_X = np.sum(np.square(X), 1)
        D = np.add(np.add(-2 * np.dot(X, X.T), sum_X).T, sum_X)
        P = np.zeros((n, n))
        for i in range(n):
            (H, P[i, np.concatenate((np.r_[0:i], np.r_[i + 1:n]))]) = TsneL1._Hbeta(
                D[i, np.concatenate((np.r_[0:i], np.r_[i + 1:n]))], beta[i])
        return P

    @staticmethod
    def _x2p_process(Di, logU, tol):
        beta = 1.
        betamin = -np.inf
        betamax = np.inf
        (H, thisP) = TsneL1._Hbeta(Di, beta)

        Hdiff = H - logU
        tries = 0

        while (not np.abs(Hdiff) < tol) and tries < 50:
            # If not, increase or decrease precision
            if Hdiff > 0:
                betamin = beta
                if betamax == np.inf or betamax == -np.inf:
                    beta = beta * 2.
                else:
                    beta = (beta + betamax) / 2.
            else:
                betamax = beta
                if betamin == np.inf or betamin == -np.inf:
                    beta = beta / 2.
                else:
                    beta = (beta + betamin) / 2.

            # Recompute the values
            (H, thisP) = TsneL1._Hbeta(Di, beta)
            Hdiff = H - logU
            tries += 1
        return thisP, beta
        # Set the final row of P

    @staticmethod
    def _x2p_parallel(X=np.array([]), tol=1e-5, perplexity=30.0, print_callback=print, n_threads=6):
        """
            Performs a binary search to get P-values in such a way that each
            conditional Gaussian has the same perplexity.
        """

        # Initialize some variables
        print_callback("Computing pairwise distances...")
        (n, d) = X.shape
        sum_X = np.sum(np.square(X), 1)
        D = np.add(np.add(-2 * np.dot(X, X.T), sum_X).T, sum_X)

        logU = np.log(perplexity)

        # Loop over all datapoints
        # for i in range(n):
        # Compute the Gaussian kernel and entropy for the current precision

        print_callback("Using", n_threads, "threads...")
        parameters = [(D[i, np.concatenate((np.r_[0:i], np.r_[i + 1:n]))], logU, tol) for i in range(n)]
        with multiprocessing.Pool(n_threads) as pool:
            results = pool.starmap(TsneL1._x2p_process, parameters)

        beta = np.ones((n, 1))
        P = np.zeros((n, n))
        for i in range(n):
            P[i, np.concatenate((np.r_[0:i], np.r_[i + 1:n]))] = results[i][0]
            beta[i] = results[i][1]

        # Return final P-matrix
        print_callback("Mean value of sigma: %f" % np.mean(np.sqrt(1 / beta)))
        return P, beta

max_stars_repo_name: KChen-lab/marker-selection

max_issues_repo_name: KChen-lab/marker-selection

max_forks_repo_name: KChen-lab/marker-selection

reason: import numpy,import scipy


text:
import argparse
import math
import sys

from tqdm import tqdm
import numpy as np
import torch as th
from torch import nn
import torch.nn.functional as F
import gym
import minerl
import os
from torchsummary import summary
import matplotlib.pyplot as plt
from torch.utils.data import TensorDataset, DataLoader
from sklearn.model_selection import train_test_split
from torch.utils.data.sampler import WeightedRandomSampler
from kairos_minerl.gail_wrapper import PovOnlyObservation
from torch.autograd import Variable
from minerl.herobraine.hero.spaces import Box
from kairos_minerl.gail_wrapper import ActionShaping_FindCave, ActionShaping_Waterfall, ActionShaping_Animalpen, ActionShaping_Villagehouse, ActionShaping_Navigation
from kairos_minerl.gail_wrapper import processed_actions_to_wrapper_actions_FindCave, processed_actions_to_wrapper_actions_Waterfall
from kairos_minerl.gail_wrapper import processed_actions_to_wrapper_actions_Animalpen, processed_actions_to_wrapper_actions_Villagehouse
from kairos_minerl.gail_wrapper import processed_actions_to_wrapper_actions_Navigation
from minerl.herobraine.wrappers import downscale_wrapper

# GAIL policy network
class PolicyNetwork(nn.Module):
    def __init__(self, state_dim, action_dim):
        super().__init__()

        self.input_shape = state_dim.shape
        self.input_channels = self.input_shape[2]
        self.action_dim = action_dim
        self.state_dim = state_dim

        # features
        self.features = nn.Sequential(
            nn.Conv2d(self.input_channels, 128, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(128),
            nn.ReLU(),
            nn.MaxPool2d((2,2)),
            nn.Conv2d(128, 64, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.MaxPool2d((2,2)),
            nn.Conv2d(64, 3, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(3),
            nn.ReLU(),
        ) #output size is 3x16x16 = 768

        # policy
        self.policy = nn.Sequential(
            #nn.Dropout(),
            nn.Linear(768, 128),
            nn.ReLU(),
            #nn.Dropout(),
            nn.Linear(128,128),
            nn.ReLU(),
            nn.Linear(128, self.action_dim)
        )

    def forward(self, states):
        x = self.features(states)
        x = th.flatten(x,1)
        probs = th.nn.functional.softmax(self.policy(x),dim=1)
        try:
            distb = th.distributions.Categorical(probs)
        except Exception as e:
            print('***torch.distribution.Categorical Error***')
            print(e)
            print('probs')
            print(probs.detach().cpu())
        return distb
        #return probs

class BehaviorCloning(nn.Module):
    "Behavior cloning model for MineRL"

    def __init__(self, action_dim, input_shape = (3, 64, 64)):
        super().__init__()
        self.input_shape = input_shape
        self.input_channels = input_shape[0]
        self.action_dim = action_dim

        # features
        self.features = nn.Sequential(
            nn.Conv2d(self.input_channels, 128, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.BatchNorm2d(128),
            nn.MaxPool2d((2, 2)),
            nn.Conv2d(128, 64, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.BatchNorm2d(64),
            nn.MaxPool2d((2, 2)),
            nn.Conv2d(64, 3, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.BatchNorm2d(3),
        )  # output size is 3x16x16 = 768

        # policy
        self.policy = nn.Sequential(
            nn.Dropout(),
            nn.Linear(768, 256),
            nn.ReLU(),
            nn.Dropout(),
            nn.Linear(256, 256),
            nn.ReLU(),
            nn.Linear(256, self.action_dim),
        )

    def forward(self, x: th.Tensor) -> th.Tensor:
        x = self.features(x)
        x = th.flatten(x, 1)
        x = self.policy(x)
        return x

class BehaviorCloning_128(nn.Module):
    "Behavior cloning model for MineRL"

    def __init__(self, action_dim, input_shape = (3, 64, 64)):
        super().__init__()
        self.input_shape = input_shape
        self.input_channels = input_shape[0]
        self.action_dim = action_dim

        # features
        self.features = nn.Sequential(
            nn.Conv2d(self.input_channels, 128, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.BatchNorm2d(128),
            nn.MaxPool2d((2, 2)),
            nn.Conv2d(128, 64, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.BatchNorm2d(64),
            nn.MaxPool2d((2, 2)),
            nn.Conv2d(64, 3, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.BatchNorm2d(3),
        )  # output size is 3x16x16 = 768

        # policy
        self.policy = nn.Sequential(
            nn.Dropout(),
            nn.Linear(768, 128),
            nn.ReLU(),
            nn.Dropout(),
            nn.Linear(128, 128),
            nn.ReLU(),
            nn.Linear(128, self.action_dim),
        )

    def forward(self, x: th.Tensor) -> th.Tensor:
        x = self.features(x)
        x = th.flatten(x, 1)
        x = self.policy(x)
        return x

class BehaviorCloningLSTM(nn.Module):
    "Behavior cloning model for MineRL"

    def __init__(self, action_dim, input_shape = (3, 64, 64)):
        super().__init__()
        self.input_shape = input_shape
        self.input_channels = input_shape[0]
        self.action_dim = action_dim

        # LSTM parameters
        self.n_layers = 1
        self.dropout_rate = 0
        self.batch_first = True
        self.hidden_dim = 128

        # features
        self.features = nn.Sequential(
            nn.Conv2d(self.input_channels, 128, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.BatchNorm2d(128),
            nn.MaxPool2d((2, 2)),
            nn.Conv2d(128, 64, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.BatchNorm2d(64),
            nn.MaxPool2d((2, 2)),
            nn.Conv2d(64, 3, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
            nn.BatchNorm2d(3),
        )  # output size is 3x16x16 = 768

        # policy layers
        self.embedding = nn.Linear(768, 128)
        self.lstm = nn.LSTM(128, self.hidden_dim, num_layers = self.n_layers,
                            batch_first = self.batch_first, dropout=self.dropout_rate)
        self.out_layer = nn.Linear(128, self.action_dim)

    def forward(self, x, hidden):
        # extract image features from conv net
        x = self.features(x)
        x = th.flatten(x, 1)

        # get first embedding
        x = F.relu(self.embedding(x))
        x = th.unsqueeze(x, 0)

        # lstm layer
        lstm_out, hidden = self.lstm(x, hidden)
        lstm_out = lstm_out.contiguous().view(-1, self.hidden_dim)

        # output prediction layer
        lstm_out = th.squeeze(lstm_out)
        out = self.out_layer(lstm_out)
        #out = out.view(batch_size, -1) #?
        #out = out[:,-1] #?
        return out, hidden

    def init_hidden(self, batch_size, device):
        weight = next(self.parameters()).data
        hidden = (weight.new(self.n_layers, batch_size, self.hidden_dim).zero_().to(device),
                      weight.new(self.n_layers, batch_size, self.hidden_dim).zero_().to(device))
        return hidden

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--num_epochs", "-e", type=int, default=40)
    parser.add_argument("--batch_size", "-b", type=int, default=32)
    parser.add_argument("--learning_rate", "-l", type=float, default=0.001)
    parser.add_argument("--val_interval", "-v", type=int, default=2)
    parser.add_argument("--save_interval", "-s", type=int, default=5)
    parser.add_argument("--model_type", "-m", type=str, default="combined", choices=["find_cave", "make_waterfall", "animal_pen", "village_house", "combined"])
    parser.add_argument("--navigation", "-n", type=bool, default = True)
    parser.add_argument("--rollout_model", "-r", type=str, default="")
    parser.add_argument("--high_res", type=bool, default=False)
    parser.add_argument("--gail", type=bool, default=False)
    parser.add_argument("--lstm", type=bool, default=False)
    args = parser.parse_args()
    return args

# load training data
def load_training_data(data_paths):
    data_images = None
    data_actions = None
    for data_path in data_paths:
        if data_images is None:
            data_images = np.load(os.path.join(data_path, "images.npy"))
            data_actions = np.load(os.path.join(data_path, "actions.npy"))
        else:
            data_images = np.concatenate([data_images, np.load(os.path.join(data_path, "images.npy"))], axis=0)
            data_actions = np.concatenate([data_actions, np.load(os.path.join(data_path, "actions.npy"))], axis=0)

    data_images = data_images.transpose(0,3,1,2)

    return data_images, data_actions

def save_model(model, model_type, loss, accuracy, epoch, experiment_name, lstm=False):
    if not os.path.exists("train/{}/{}".format(experiment_name, model_type)):
        os.makedirs("train/{}/{}".format(experiment_name, model_type))
    th.save(model, "train/{}/{}/{}_BC_v1_e{}_l{:.3f}_a{:.3f}.pth".format(experiment_name, model_type, model_type, epoch , loss, accuracy))
    th.save(model.state_dict(), "train/{}/{}/{}_BC_v1_e{}_l{:.3f}_a{:.3f}_dict.pth".format(experiment_name, model_type, model_type, epoch, loss, accuracy))

cosine_similarity = nn.CosineSimilarity(dim=1, eps=1e-6)

def save_best_model(model: BehaviorCloning, model_type, experiment_name):
    all_model_paths = os.listdir("train/{}/{}/".format(experiment_name, model_type))
    all_model_paths = [fname for fname in all_model_paths if "dict" not in fname and ".pth" in fname and "final" not in fname]

    all_model_paths_split = [(fname, float(fname.split("_")[5][1:]), index) for index, fname in enumerate(all_model_paths)]
    best_model_path = sorted(all_model_paths_split, key=lambda x: x[1], reverse=False)[0]
    best_model_state_dict_path = os.path.join("train/{}/{}".format(experiment_name, model_type), best_model_path[0].replace(".pth", "_dict.pth"))

    model.load_state_dict(th.load(best_model_state_dict_path))

    th.save(model, "train/{}/{}_best_BC_model.pth".format(experiment_name, model_type))
    th.save(model.state_dict(), "train/{}/{}_best_BC_model_dict.pth".format(experiment_name, model_type))

# compute accuracy of a trained model given a dataset
def compute_accuracy(model, dataset, loss_function, device, is_val, lstm = False):
    running_loss = 0.0
    num_correct = 0.0
    total_num = 0.0
    accuracy = 0.0
    model.eval()
    title = "Val" if is_val else "Test"
    with th.no_grad():
        #initialize lstm hidden state
        if lstm:
            hidden = model.init_hidden(1, device)
        bar = tqdm(total=len(dataset), file=sys.stdout)
        for i, data in enumerate(dataset, 0):
            inputs, labels = data
            if lstm:
                hidden = tuple([e.data for e in hidden])
                pred_labels, hidden = model(inputs.to(device), hidden)
            else:
                pred_labels = model(inputs.to(device))

            loss = loss_function(pred_labels, labels.long().to(device))

            # compute running training accuracy
            pred_labels = th.argmax(pred_labels, dim=1)
            num_correct += th.sum(labels.to(device)==pred_labels)
            total_num += len(pred_labels)
            accuracy = num_correct / total_num

            running_loss += loss.item()
            bar.set_description("[{}] loss: {:10.3f} | accuracy: {:10.3f}".format(title, loss, accuracy))
            bar.update()
        accuracy = (num_correct * 1.0 / len(dataset.dataset))
        bar.set_description("[{}] loss: {:10.3f} | accuracy: {:10.3f}".format(title, running_loss / len(dataset), accuracy))
        bar.close()

    accuracy = (num_correct * 1.0 / len(dataset.dataset))
    loss = running_loss * 1.0 / len(dataset)
    return loss, accuracy

# train module for training the full pipeline
def train(num_epochs = 40, batch_size = 32, learning_rate = 0.001, val_interval = 2, save_interval = 5, navigation = True,
          lstm = False, model_type = "find_cave", experiment_name = "test"):

    print("Training Behavior Cloning model for {} Task!".format(model_type))

    # model task type: defines the data path, and action processor/wrappers for each task
    model_type_dict = {
        "find_cave": {
            "paths": ['data/MineRLBasaltFindCave-v0'],
            "action_processor":processed_actions_to_wrapper_actions_FindCave,
            "action_wrapper": ActionShaping_FindCave
        },
        "animal_pen": {
            "paths": ['data/MineRLBasaltCreateVillageAnimalPen-v0',
                      'data/MineRLBasaltCreatePlainsAnimalPen-v0'],
            "action_processor":processed_actions_to_wrapper_actions_Animalpen,
            "action_wrapper": ActionShaping_Animalpen
        },
        "make_waterfall": {
            "paths": ['data/MineRLBasaltMakeWaterfall-v0'],
            "action_processor":processed_actions_to_wrapper_actions_Waterfall,
            "action_wrapper": ActionShaping_Waterfall
        },
        "village_house": {
            "paths": ['data/MineRLBasaltBuildVillageHouse-v0'],
            "action_processor":processed_actions_to_wrapper_actions_Villagehouse,
            "action_wrapper": ActionShaping_Villagehouse
        },
        "combined": {
            "paths": ['data/MineRLBasaltFindCave-v0',
                      'data/MineRLBasaltMakeWaterfall-v0',
                      'data/MineRLBasaltCreateVillageAnimalPen-v0',
                      'data/MineRLBasaltCreatePlainsAnimalPen-v0',
                      'data/MineRLBasaltBuildVillageHouse-v0',],
            "action_processor":processed_actions_to_wrapper_actions_Navigation,
            "action_wrapper": ActionShaping_Navigation
        },
        "navigation":{
            "action_processor":processed_actions_to_wrapper_actions_Navigation,
            "action_wrapper": ActionShaping_Navigation
            }
    }

    # load data
    data_paths = model_type_dict[model_type]["paths"]
    data_images, data_actions = load_training_data(data_paths)

    # process data
    if navigation == True:
        action_processor = model_type_dict["navigation"]["action_processor"]

    else:
        action_processor = model_type_dict[model_type]["action_processor"]

    data_actions = action_processor(data_actions)

    if navigation == True:
        data_images = data_images[data_actions!=99,:,:]
        data_actions=data_actions[data_actions!=99]

    num_classes = np.max(data_actions) + 1

    #data_actions = np.squeeze(np.eye(num_classes)[data_actions.reshape(-1)])

    input_shape = (3, 64, 64)

    # get device
    device = th.device("cuda" if th.cuda.is_available() else "cpu")

    # initialize autoencoder model (and place it on the gpu)
    if lstm:
        print("Initializing LSTM BC model!")
        bc_model = BehaviorCloningLSTM(num_classes).to(device)
    else:
        print("Initializing Standard BC model!")
        bc_model = BehaviorCloning(num_classes).to(device)
        summary(bc_model, input_shape)

    # setup optimizer and loss
    optimizer = th.optim.Adam(bc_model.parameters(), lr=learning_rate, weight_decay=1e-5)
    loss_function = nn.modules.CrossEntropyLoss()

    # split data into train/test
    if lstm:
        x_train, x_val, y_train, y_val = train_test_split(data_images, data_actions, test_size=0.1, shuffle=False)
    else:
        x_train, x_val, y_train, y_val = train_test_split(data_images, data_actions, test_size=0.1, random_state=2, stratify=data_actions)

    # convert into pytorch dataset and wrap in a dataloader
    x_train = th.Tensor(x_train)
    x_val = th.Tensor(x_val)
    y_train = th.LongTensor(y_train)
    y_val = th.LongTensor(y_val)

    # convert to dataset
    trainset = TensorDataset(x_train, y_train)
    valset = TensorDataset(x_val, y_val)

    # wrap in dataloader
    trainloader = DataLoader(trainset, batch_size=batch_size, shuffle=True)
    valloader = DataLoader(valset, batch_size=batch_size)

    # place network in train mode
    bc_model.train()

    # train model
    print('Training model')

    minimum_val_loss = sys.maxsize
    for epoch in range(num_epochs):  # loop over dataset num_epoch times
        running_loss = 0.0
        num_correct = 0.0
        total_num = 0.0
        accuracy = 0.0
        bc_model.train()
        #initialize lstm hidden state
        if lstm:
            hidden = bc_model.init_hidden(1, device)

        bar = tqdm(total=math.ceil(len(trainset)/trainloader.batch_size), file=sys.stdout, desc="[Epoch: {}] loss: {:10.3f} | accuracy: {:10.3f}".format(epoch + 1, running_loss, accuracy))
        for i, data in enumerate(trainloader):
            # get the inputs; data is a list of [inputs, labels]
            inputs, labels = data

            # zero the parameter gradients
            optimizer.zero_grad()

            # forward + backward + optimize
            if lstm:
                hidden = tuple([e.data for e in hidden])
                outputs, hidden = bc_model(inputs.to(device), hidden)
            else:
                outputs = bc_model(inputs.to(device))

            loss = loss_function(outputs, labels.long().to(device))
            loss.backward()
            optimizer.step()

            # compute running training accuracy
            pred_labels = th.argmax(outputs, dim=1)
            num_correct += th.sum(labels.to(device)==pred_labels)
            total_num += len(pred_labels)
            accuracy = num_correct / total_num

            # print statistics
            running_loss += loss.item()
            bar.set_description("[Epoch: {}] loss: {:10.3f} | accuracy: {:10.3f}".format(epoch + 1, loss, accuracy))
            bar.update()

        # Compute training metrics at the end of each epoch
        acc = (num_correct * 1.0 / len(trainset))
        bar.set_description("[Epoch: {}] loss: {:10.3f} | accuracy: {:10.3f}".format(epoch + 1, running_loss / len(trainloader), acc))
        bar.close()

        if epoch % val_interval == 0:
            print("\nComputing validation")
            val_loss, val_accuracy = compute_accuracy(bc_model, valloader, loss_function, device, True, lstm)
            if val_loss < minimum_val_loss:
                print("\nValidation loss lower: {:10.3f} < {:10.3f}, saving...".format(val_loss, minimum_val_loss))
                save_model(
                    bc_model, model_type, val_loss, val_accuracy, epoch, experiment_name=experiment_name, lstm = lstm)
                minimum_val_loss = val_loss
            else:
                print("\nValidation loss higher: {:10.3f} > {:10.3f}".format(val_loss, minimum_val_loss))

    print()
    print('Finished Training')

    # save best model
    save_best_model(bc_model, model_type, experiment_name)

def main(model_type = None):
    # learning parameters
    args = parse_args()
    num_epochs = args.num_epochs  # 50
    batch_size = args.batch_size  # 32
    learning_rate = args.learning_rate  # 1e-3
    val_interval = args.val_interval
    save_interval = args.save_interval
    navigation = args.navigation
    lstm = args.lstm

    if model_type is None:
        model_type = args.model_type

    train(num_epochs=num_epochs,
    batch_size=batch_size,
    learning_rate=learning_rate,
    val_interval = val_interval,
    save_interval = save_interval,
    navigation = navigation,
    lstm = lstm,
    model_type = model_type)

def rollout():
    print('Testing Behavior Cloning model')

    # get device
    device = th.device("cuda" if th.cuda.is_available() else "cpu")

    # get task type
    args = parse_args()
    model_type = args.model_type
    high_res = args.high_res
    if high_res:
        high_res_str = 'HighRes'
    else:
        high_res_str = ''
    lstm = args.lstm
    gail = args.gail

    # model task type: defines the data path, and action processor/wrappers for each task
    model_type_dict = {
        "find_cave": {
            "path": 'train/bc_model/find_cave',
            'env_name':'MineRLBasaltFindCave{}-v0'.format(high_res_str),
            "action_wrapper": ActionShaping_FindCave
        },
        "animal_pen": {
            "path": 'train/bc_model/animal_pen',
            'env_name':'MineRLBasaltCreateVillageAnimalPen{}-v0'.format(high_res_str),
            "action_wrapper": ActionShaping_Animalpen
        },
        "make_waterfall": {
            "path": 'train/bc_model/make_waterfall',
            'env_name':'MineRLBasaltMakeWaterfall{}-v0'.format(high_res_str),
            "action_wrapper": ActionShaping_Waterfall
        },
        "village_house": {
            "path": 'train/bc_model/village_house',
            'env_name':'MineRLBasaltBuildVillageHouse{}-v0'.format(high_res_str),
            "action_wrapper": ActionShaping_Villagehouse
        },
        "combined": {
            "path": 'train/bc_model_V3_CCE_False/combined',
            'env_name':'MineRLBasaltFindCave{}-v0'.format(high_res_str),
            "action_processor":processed_actions_to_wrapper_actions_Navigation,
            "action_wrapper": ActionShaping_Navigation
        },
    }

    # create gym environment
    env = gym.make(model_type_dict[model_type]['env_name'])
    if high_res:
        env = downscale_wrapper.DownscaleWrapper(env)
    env = PovOnlyObservation(env)
    ActionShaping = model_type_dict[model_type]['action_wrapper']
    env = ActionShaping(env)

    # load pre-trained BC model
    if gail:
        bc_model = PolicyNetwork(Box(-np.inf, np.inf, shape=(64,64,3)), 17).to(device)
        bc_model.load_state_dict(th.load(args.rollout_model))
    else:
        bc_model = th.load(os.path.join(model_type_dict[model_type]['path'], args.rollout_model)).to(device)

    num_actions = env.action_space.n
    action_list = np.arange(num_actions)

    # rollout 10 games
    for game_i in range(10):
        print("Rolling out game {} for task {}".format(game_i, model_type))
        obs = env.reset()
        done = False
        reward_sum = 0
        while not done:
            # Forward pass through model
            output = bc_model((th.tensor(np.expand_dims(obs,axis=0))).to(device))

            if gail:
                probabilities = output
                action = probabilities.sample().detach().cpu().numpy().squeeze()
            else:
                # Into numpy
                probabilities = th.softmax(output, 1)
                probabilities = probabilities.detach().cpu().numpy()
                # Sample action according to the probabilities
                action = probabilities
                #action = np.random.choice(action_list, p=probabilities[0])
            # Take a step in environment
            obs, reward, done, info = env.step(action)
            if info !={}:
                print(info)
            env.render()
            reward_sum += reward
        print("Game {}, total reward {}".format(game_i, reward_sum))

    env.close()

if __name__ == "__main__":
    args = parse_args()

    if args.rollout_model != "":
        # rollout model
        rollout()
    else:
        # train bc model
        main("make_waterfall")
        main("animal_pen")
        main("village_house")
        main("find_cave")


text:
import tensorflow.keras
from PIL import Image
import numpy as np
import random
import string

import cv2
import imutils
import glob
import os

categories = [1, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180]

sourceImageFileName = "TestIm4"
sourceImagePath = "./TestingImgsComplete/" + sourceImageFileName + ".png"
threshImagePath = "./TestingImgsComplete/" + sourceImageFileName + "-Thresh.png"
resizedImagePath = "./TestingImgsComplete/" + sourceImageFileName + "-Resized.png"
# Disable scientific notation for clarity
np.set_printoptions(suppress=True)

# Load the model
model = tensorflow.keras.models.load_model('./converted_keras/keras_model.h5')

# Create the array of the right shape to feed into the keras model
# The 'length' or number of images you can put into the array is
# determined by the first position in the shape tuple, in this case 1.
data = np.ndarray(shape=(1, 224, 224, 3 ), dtype=np.float32)

def colorToThresh():
    image = cv2.imread(sourceImagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)[1]
    thresh = thresh[80:195, 93:208]
    cv2.imwrite(threshImagePath, thresh)

colorToThresh()

image = cv2.imread(threshImagePath)

image = cv2.resize(image, (224, 224))

cv2.imwrite(resizedImagePath, image)

# Replace this with the path to your image
CaptchaImage = Image.open(resizedImagePath)
# Make sure to resize all images to 224, 224 otherwise they won't fit in the array
CaptchaImage = CaptchaImage.resize((224, 224))

image_array = np.asarray(CaptchaImage)

# Normalize the image
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
np.resize(normalized_image_array, (224, 224, 3))
# Load the image into the array
data[0] = normalized_image_array

# run the inference
prediction = model.predict(data)
print(prediction)

confidentIndex = np.argmax(prediction)
print(confidentIndex)
print(categories[confidentIndex])
print(prediction[0][confidentIndex])

os.remove(resizedImagePath)
os.remove(threshImagePath)

max_stars_repo_name: wyattpfeil/funcaptcha-solver-master

max_issues_repo_name: wyattpfeil/funcaptcha-solver-master

max_forks_repo_name: wyattpfeil/funcaptcha-solver-master