# Python: Game statistics reports by Bella Kocsis

import csv

# count game = length of the imported list
def count_games(file_name):
    # import data as nested lists
    try:
        imported_data = []
        with open(file_name) as csvfile:
            readCSV = csv.reader(csvfile, delimiter="\t")
            for row in readCSV:
                imported_data.append(row)
        return len(imported_data)  # there are as many nested lists (items) as games
    except FileNotFoundError:
        return "File not found."

# checking whether there's a game from a given year
def decide(file_name, year=None):
    try:
        imported_data = []
        with open(file_name) as csvfile:
            readCSV = csv.reader(csvfile, delimiter="\t")
            for row in readCSV:
                imported_data.append(row)
        years = []
        for game in imported_data:
            years.append(game[2])  # every game has release year as its 3rd element
        if year is not None:  # when something is entered as year
            if str(year) in years:  # if the asked year is in the list, the function returns True
                return True
            else:
                return False
        else:  # when nothing is entered as year
            return "No year entered."
    except FileNotFoundError:
        return "File not found."

# get the title of the latest game
def get_latest(file_name):
    try:
        imported_data = []
        with open(file_name) as csvfile:
            readCSV = csv.reader(csvfile, delimiter="\t")
            for row in readCSV:
                imported_data.append(row)
        years = []
        for game in imported_data:
            years.append(game[2])  # release year is the 3rd element
        latest_year = max(years)  # max is the latest release year
        latest_title = str(imported_data[years.index(latest_year)][0])
        # getting the 0th elemnt (=title), that belongs to the latest release year
        # years.index works, because the order of the years list is the same as the imported data
        # so the first game's release date is the first in the years list as well
        return latest_title
    except FileNotFoundError:
        return "File not found."

# counts games by genre
def count_by_genre(file_name, genre=None):
    try:
        imported_data = []
        with open(file_name) as csvfile:
            readCSV = csv.reader(csvfile, delimiter="\t")
            for row in readCSV:
                imported_data.append(row)
        genres = []  # make a list of genres
        for game in imported_data:
            genres.append(game[3])  # genre is the 4rd element
        genres = list(set(genres))  # so every genre appears only once
        if genre is not None:  # if something is given as genre
            if genre in genres:  # if it is in the list of our genres
                occurrence = 0
                for game in imported_data:
                    if str(genre) in game[3]:
                        # checks every nested list (=game),
                        # and if the 4rd element, which is the genre, is the
                        # same as the one in the function call, the count is increased
                        occurrence += 1
                return occurrence
            else:
                return "No such genre."
        else:  # when no genre is given
            return "No genre entered."
    except FileNotFoundError:
        return "File not found."

# gives the line number of a chosen title
def get_line_number_by_title(file_name, title=None):
    try:
        imported_data = []
        with open(file_name) as csvfile:
            readCSV = csv.reader(csvfile, delimiter="\t")
            for row in readCSV:
                imported_data.append(row)
        try:
            titles = []
            for item in imported_data:
                titles.append(item[0])  # append the 0th element of every nested list, which is the title
            if title is not None:  # when something is given as title in the function call
                if title in titles:  # the function checks if there is such title in the file
                    for i in range(len(imported_data)):
                        if title == imported_data[i][0]:  # when it is found in the file
                            line_number = int(i) + 1  # int(item) is just the
                            # index of the game, line number needs to start from 1, instead of 0
                            return line_number
                        else:
                            continue
                else:  # there is no title in the file like the one given in the function call
                    raise ValueError
            else:  # when nothing is given as title
                return "No title entered."
        except ValueError:  # when the title is not found in the file
            return "No such title."
    except FileNotFoundError:
        return "File not found."

# EXTRAS

# sorted titles - with sorted()
def sort_abc(file_name):
    try:
        imported_data = []
        with open(file_name) as csvfile:
            readCSV = csv.reader(csvfile, delimiter="\t")
            for row in readCSV:
                imported_data.append(row)
        titles = []
        for item in imported_data:
            titles.append(item[0])  # append the 0th element of every nested list, which is the title
        titles = sorted(titles)
        return titles
    except FileNotFoundError:
        return "File not found."

# alternative sorting
def sort_abc_alg(file_name):
    try:
        imported_data = []
        with open(file_name) as csvfile:
            readCSV = csv.reader(csvfile, delimiter="\t")
            for row in readCSV:
                imported_data.append(row)
        unsorted_titles = []
        sorted_titles = []
        for item in imported_data:
            unsorted_titles.append(item[0])
        while unsorted_titles:
            minimum = unsorted_titles[0]  # it uses the very first element as minimum
            for t in unsorted_titles:
                if t < minimum:  # checks every other element, whether they are  smaller
                    minimum = t
            sorted_titles.append(minimum)  # if they are smaller, they get appended to the sorted list
            unsorted_titles.remove(minimum)  # the items that get appended to the sorted list,
            # removed from the original, so they are checked only once
        return sorted_titles  # titles are in alphabetical order appended to this list
    except FileNotFoundError:
        return "File not found."

# lists all genres in the file
def get_genres(file_name):
    try:
        imported_data = []
        with open(file_name) as csvfile:
            readCSV = csv.reader(csvfile, delimiter="\t")
            for row in readCSV:
                imported_data.append(row)
        genres = []
        for game in imported_data:  # check all games
            genres.append(game[3])  # append the genre
        genres = sorted(list(set(genres)), key=str.lower)  # unique genres in
        # alphabetical order, regardless of their case (uppercase is higher alphabetical order than lower)
        # but they are not modified (turned into lowercase)
        return genres
    except FileNotFoundError:
        return "File not found."

# find the release date of the top sold fps game
def when_was_top_sold_fps(file_name):
    try:
        imported_data = []
        with open(file_name) as csvfile:
            readCSV = csv.reader(csvfile, delimiter="\t")
            for row in readCSV:
                imported_data.append(row)
        try:
            fps_games = []  # a list for all fps games and their data
            for game in imported_data:  # checking every game's genre
                if game[3] == "First-person shooter":  # every fps is appended
                    fps_games.append(game)
            copies_sold = []  # a list for sold amounts by game
            for item in fps_games:
                copies_sold.append(float(item[1]))  # creating a list of the amount sold
            top_sold = max(copies_sold)  # this is the highest sold game
            for game in fps_games:
                if str(top_sold) in game:  # if the highest number is found
                    # in that game's list, it's release is what we are looking for
                    release_of_top_sold_game = game[2]
                else:
                    continue
            return int(release_of_top_sold_game)
        except ValueError:  # when there is no match
            return 'No game with genre "First-person shooter".'
    except:
        return "File not found."


#!/usr/bin/env python
#
# by Kevin J. Walchko 26 Aug 2014
#
# Log:
# 12 Oct 14 Broke out into its own file
#
from __future__ import division
from __future__ import print_function
import time
# import datetime as date
import multiprocessing as mp
import logging
import MotorDriver as md
# import RPi.GPIO as GPIO  # IR
import sys
import os
sys.path.insert(0, os.path.abspath('..'))
import lib.zmqclass as zmq
# import lib.Messages as Msg
# import lib.FileStorage as Fs

# def test():
# 	import time
# 	mmd = md.MotorDriver(17,18,22,23)
#
# 	go  = {'dir': MotorDriver.FORWARD, 'duty': 50}
# 	rev = {'dir': MotorDriver.REVERSE, 'duty': 100}
# 	stp = {'dir': MotorDriver.REVERSE, 'duty': 0}
#
# 	mmd.setMotors(go,go,go,go)
# 	time.sleep(10)
# 	mmd.setMotors(rev,rev,rev,rev)
# 	time.sleep(10)
# 	mmd.setMotors(go,stp,go,stp)
# 	time.sleep(10)
# 	mmd.setMotors(stp,rev,stp,rev)
# 	time.sleep(10)
# 	mmd.allStop()

# class DigitalIR(object):
# 	"""
# 	Read IR sensors on digital inputs
#
# 	Pololu sds01a 0-5 cm pwr 3V output max 3V
# 	Sharp xxx 0-15 cm pwr 5V output max 3V - digital gives 3 inch range
# 	"""
# 	def __init__(self, pins):
# 		GPIO.setwarnings(False)
# 		GPIO.setmode(GPIO.BCM)
# 		for p in pins: GPIO.setup(p, GPIO.IN)
# 		self.pins = pins
#
# 	def read(self):
# 		ir = []
# 		for p in pins: ir.append( GPIO.input(p) )
# 		return ir

class RobotHardwareServer(mp.Process):
	"""
	RobotHardwareServer handles incoming commands streamed from somewhere else.
	in: commands/etc
	out: sensor readings
	"""
	def __init__(self, host="localhost", port=9000):
		mp.Process.__init__(self)
		self.host = host
		self.port = port
		logging.basicConfig(level=logging.INFO)
		self.logger = logging.getLogger('robot')
		self.md = None

	def createMotorCmd(self, dir, duty):
		return {'dir': dir, 'duty': duty}

	def soundCmd(self, cmd):
		self.logger.info(cmd)

	def parseMsg(self, msg):
		if 'quit' in msg:
			self.shutdown()
		elif 'cmd' in msg:
			cmd = msg['cmd']
			if 'm' in cmd:
				self.motorCmd(cmd)
			elif 's' in cmd:
				self.soundCmd(cmd)

	def __del__(self):
		self.md.allStop()

	def on_message(self, client, userdata, msg):
		print(msg.topic + ' ' + str(msg.payload))

	def shutdown(self):
		"""
		Needed?
		"""
		pass

	def test(self):
		dt = 5
		go = {'dir': md.MotorDriver.FORWARD, 'duty': 10}
		rev = {'dir': md.MotorDriver.REVERSE, 'duty': 10}
		stp = {'dir': md.MotorDriver.REVERSE, 'duty': 10}

		self.md.setMotors(go, go, go, go)
		time.sleep(dt)
		self.md.setMotors(rev, rev, rev, rev)
		time.sleep(dt)
		self.md.setMotors(go, stp, go, stp)
		time.sleep(dt)
		self.md.setMotors(stp, rev, stp, rev)
		time.sleep(dt)
		self.md.allStop()

	def run(self):
		self.logger.info(str(self.name) + '[' + str(self.pid) + '] started on' +
			str(self.host) + ':' + str(self.port) + ', Daemon: ' + str(self.daemon))
		# p = Publisher((self.host,self.port))
		# self.pub = p.accept()
		# self.logger.info('Accepted connection: ')

		self.sub = zmq.Sub(['cmds'])
		self.pub = zmq.Pub()

		while True:
			time.sleep(0.05)  # 0.5 => 20Hz
			# get info
			topic, msg = self.sub.recv()
			if msg:
				print('msg:', topic, msg)
# 				self.parseMsg( msg )
			# get IR sensors
			# get drop sensors
			# send motor commands
			# send head servo commands
			# self.test()

class NonHolonomic(RobotHardwareServer):
	def __init__(self, host="localhost", port=9000):
		RobotHardwareServer.__init__(self, host, port)
		# self.md = md.MotorDriver(11, 12)  # FIXME: 20160528 why these two pins?
		# self.md.allStop()

	def motorCmd(self, cmd):
		self.logger.info(cmd)
		# self.md.setMotors(cmd)

class Holonomic(RobotHardwareServer):
	def __init__(self, host="localhost", port=9000, pinA=11, pinB=12, pinC=15, pinD=16):
		RobotHardwareServer.__init__(self, host, port)
		self.md = md.MotorDriver(pinA, pinB, pinC, pinD)
		self.md.allStop()

	def motorCmd(self, cmd):
		self.logger.info(cmd)
		# self.md.setMotors(cmd)

if __name__ == '__main__':
	c = Holonomic()
	c.run()


from constants import *
from class_basic import *
from pygame import *
from heart import *
from functions import *

### ну тут понятно я думаю ###

class Enemy(Basic):

    def make_pew(self, kind_of_pew, pos):
        if kind_of_pew == 'mel':
            self.make_pew_mel(pos)

    def follow(self, tar):  # ИИ врага
        if self.alive:
            tar_x, tar_y = tar.get_pos()
            if tar_y - 40 > self.y and abs(tar_y - self.y) < 200 and abs(tar_x - self.x) < 200:
                self.rect.top += 1
                self.y_change(1)
            elif tar_y + 40 < self.y and abs(tar_y - self.y) < 200 and abs(tar_x - self.x) < 200:
                self.rect.top -= 1
                self.y_change(-1)
            if tar_x - 40 > self.x and abs(tar_x - self.x) < 200 and abs(tar_y - self.y) < 200:
                self.rect.left += 1
                self.x_change(1)
            elif tar_x + 40 < self.x and abs(tar_x - self.x) < 200 and abs(tar_y - self.y) < 200:
                self.rect.left -= 1
                self.x_change(-1)

            if (tar_y - 40 <= self.y and tar_y + 40 >= self.y and  # атака врага
                    tar_x - 40 <= self.x and tar_x + 40 >= self.x):
                if self.tics + 500 < pygame.time.get_ticks():
                    self.tics = pygame.time.get_ticks()
                    self.hit = pygame.mixer.Sound("Data/hit.wav")  # звук атаки
                    self.hit.play()
                    self.ht = self.counter_hp()
                    if self.alive:
                        self.heart = Heart(load_image("dead_heart.jpg", Color('white')), 1, 1,
                                           self.ht, 0)  # убавление жизней

    def counter_hp(self):
        if self.sp:  # проверка на количество жизней гг
            return self.sp.pop()
        self.alive = False
        self.dead_screen()

    def dead_screen(self):  # экран смерти
        intro_text = ["You died :("]

        fon = pygame.transform.scale(load_image('nothing.png'), (1500, 1000))
        screen.blit(fon, (0, 0))
        font = pygame.font.Font(None, 30)
        text_coord = 20
        for line in intro_text:
            string_rendered = font.render(line, 2, pygame.Color('white'))
            intro_rect = string_rendered.get_rect()
            text_coord += 15
            intro_rect.top = text_coord
            intro_rect.x = 650
            text_coord += intro_rect.height
            screen.blit(string_rendered, intro_rect)

        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    running = False
                elif event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN:
                    pygame.quit()
                    running = False
            pygame.display.flip()
            clock.tick(60)

    def follow_and_hit(self, tar):
        u1, u2 = True, True
        tar_x, tar_y = tar.get_pos()
        print(tar_x, tar_y)
        print(self.x, self.y)
        if tar_y - 40 > self.y:
            self.rect.top += 1
            self.y_change(1)
            u1 = False
        elif tar_y + 40 < self.y:
            self.rect.top -= 1
            self.y_change(-1)
            u1 = False
        # else:
        #    u1 = True
        if tar_x - 40 > self.x:
            self.rect.left += 1
            self.x_change(1)
            u2 = False
        elif tar_x + 40 < self.x:
            self.rect.left -= 1
            self.x_change(-1)
            u2 = False
        if u1 and u2:
            pass

    def make_pew_mel(self, pos):
        new_bullet = Mel(load_image("mel.jpg",
                                    Color('White')), 1, 1, pos[0], pos[1])

    def get_hitted(self, player):  # урон врага
        self.robot_hit.play()
        self.chars[1] -= 1
        if self.chars[1] == 0:
            self.kill()
            player.change_score(1)

    def is_alive(self):  # проверка на то, жив ли гг
        return(self.alive)


import tvm
from tvm.relay import transform
import tvm.relay as relay
import numpy as np
from tvm.contrib import graph_runtime

def batch_norm_infer(data,
                     gamma=None,
                     beta=None,
                     moving_mean=None,
                     moving_var=None,
                     **kwargs):
    name = kwargs.get("name")
    kwargs.pop("name")
    if not gamma:
        gamma = relay.var(name + "_gamma")
    if not beta:
        beta = relay.var(name + "_beta")
    if not moving_mean:
        moving_mean = relay.var(name + "_moving_mean")
    if not moving_var:
        moving_var = relay.var(name + "_moving_var")
    return relay.nn.batch_norm(data,
                               gamma=gamma,
                               beta=beta,
                               moving_mean=moving_mean,
                               moving_var=moving_var,
                               **kwargs)[0]

def conv2d(data, weight=None, **kwargs):
    name = kwargs.get("name")
    kwargs.pop("name")
    if not weight:
        weight = relay.var(name + "_weight")
    return relay.nn.conv2d(data, weight, **kwargs)

def conv_block(data, name, channels, kernel_size=(3, 3), strides=(1, 1),
               padding=(1, 1), epsilon=1e-5):
    conv = conv2d(
        data=data,
        channels=channels,
        kernel_size=kernel_size,
        strides=strides,
        padding=padding,
        data_layout='NCHW',
        name=name+'_conv')
    bn = batch_norm_infer(data=conv, epsilon=epsilon, name=name + '_bn')
    act = relay.nn.relu(data=bn)
    return act

data_shape = (1, 3, 224, 224)
kernel_shape = (32, 3, 3, 3)
dtype = "float32"
data = relay.var("data", shape=data_shape, dtype=dtype)
act = conv_block(data, "graph", 32, strides=(2, 2))
func = relay.Function(relay.analysis.free_vars(act),act)

mod = relay.Module.from_expr(func)
mod = relay.transform.InferType()(mod)
shape_dict = {
    v.name_hint : v.checked_type for v in mod["main"].params}
np.random.seed(0)
params = {}
for k, v in shape_dict.items():
    if k == "data":
        continue
    init_value = np.random.uniform(-1, 1, v.concrete_shape).astype(v.dtype)
    params[k] = tvm.nd.array(init_value, ctx=tvm.cpu(0))

target = "llvm"
ctx = tvm.context(target, 0)
print("Relay module function:\n", mod.astext(show_meta_data=False))
print("TVM parameters:\n", params.keys())

with relay.build_config(opt_level=3):
  graph, lib, params = relay.build(mod, target, params=params)

print("TVM graph:\n", graph)
print("TVM parameters:\n", params.keys())
# print("TVM compiled target function:\n", lib.get_source())
module = graph_runtime.create(graph, lib, ctx)
data_tvm = tvm.nd.array((np.random.uniform(-1, 1, size=data_shape)).astype(dtype))
module.set_input('data', data_tvm)
module.set_input(**params)
module.run()
output = module.get_output(0)


"""
Wide ResNet by Sergey Zagoruyko and Nikos Komodakis
Fixup initialization by Hongyi Zhang, Yann N. Dauphin, Tengyu Ma
Based on code by xternalz and Andy Brock:
https://github.com/xternalz/WideResNet-pytorch
https://github.com/ajbrock/BoilerPlate
"""

import math
import torch
import torch.nn as nn
import torch.nn.functional as F

def get_Q(t,dims=[0]):
    a = t - torch.mean(t,[0])
    return a*a

class Swish(nn.Module):

    def forward(self, input):
        return (input + torch.sigmoid(input))

    def __repr__(self):
        return self.__class__.__name__ + '  ()'

class BasicBlock(nn.Module):
    droprate = 0.0
    use_bn = True
    use_fixup = False
    fixup_l = 12
    varnet = False
    sigmaW = 1.0
    def __init__(self, in_planes, out_planes, stride):
        super(BasicBlock, self).__init__()

        self.bn = nn.BatchNorm2d(in_planes)

##        self.bn = nn.BatchNorm2d(in_planes)
##        self.relu = nn.Softplus()
##        self.relu = Swish()
        self.relu = nn.ReLU(inplace=True)
##        self.relu = nn.Hardtanh()
        print(self.relu)

        gain = torch.nn.init.calculate_gain('relu')
        self.conv = nn.Conv2d(
            in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False
        )

        self.equalInOut = in_planes == out_planes

        self.conv_res = nn.Conv2d(
            in_planes, out_planes, kernel_size=1, stride=stride, padding=0, bias=False
        )
        self.conv_res = not self.equalInOut and self.conv_res or None
        ##self.no_act =  not self.equalInOut

        ##print("Using linear networks!")

        assert (
            self.use_fixup or self.use_bn
        ), "Need to use at least one thing: Fixup or BatchNorm"

        self.scale = nn.Parameter(torch.ones(1))
        self.biases = nn.ParameterList(
            [nn.Parameter(torch.zeros(1)) for _ in range(2)]
        )

        k = (
            self.conv.kernel_size[0]
            * self.conv.kernel_size[1]
            * self.conv.out_channels
        )

        if self.varnet:
            self.conv.weight.data.normal_(
                0, gain * math.sqrt(self.sigmaW / k)
            )
        else:
            if self.sigmaW == 0.0:
                self.conv.weight.data.zero_()
            else:
                self.conv.weight.data.normal_(
                    0, gain * math.sqrt(self.sigmaW / k)
                )

        if self.conv_res is not None:
            gain = 1.0 ##doesn't currently pass through relu.
            k = (
                self.conv_res.kernel_size[0]
                * self.conv_res.kernel_size[1]
                * self.conv_res.out_channels
            )
            if self.varnet:
                self.conv_res.weight.data.normal_(0, gain * math.sqrt(self.sigmaW / k))
            else:
                ConstAvg(self.conv_res.weight, self.conv_res.bias)
                ##self.conv_res.weight.data.fill_(gain**2 / self.conv_res.in_channels)

    def forward(self, x):
        if self.use_bn:
            x_out = self.bn(x)

            ##x_out = x
            ##out = x_out
            ##out = self.relu(self.conv(x_out + self.biases[0]))

            out = self.conv(self.relu(x_out))
            ##out = self.conv(x_out)
            if self.droprate > 0:
                out = F.dropout(out, p=self.droprate, training=self.training)
        else:
            x_out = x + self.biases[0]
            out = self.conv(self.relu(x_out)) + self.biases[1]
            ##out = self.noise(out)

##            x_out = self.relu(x + self.biases[0])
##            out = self.conv(x_out) + self.biases[1]

            if self.droprate > 0:
                out = F.dropout(out, p=self.droprate, training=self.training)

            out = self.scale * out

        if self.equalInOut:
            out =  torch.add(x, out)
        else:
            out = torch.add(self.conv_res(x_out), out)

        return out

class NetworkBlock(nn.Module):
    def __init__(self, nb_layers, in_planes, out_planes, block, stride):
        super(NetworkBlock, self).__init__()
        self.layer = self._make_layer(block, in_planes, out_planes, nb_layers, stride)

    def _make_layer(self, block, in_planes, out_planes, nb_layers, stride):
        layers = []

        for i in range(int(nb_layers)):
            _in_planes = i == 0 and in_planes or out_planes
            _stride = i == 0 and stride or 1
            layers.append(block(_in_planes, out_planes, _stride))

        return nn.Sequential(*layers)

    def forward(self, x):

        out = self.layer(x)

        return self.layer(x)

class WideResNet(nn.Module):
    def __init__(
        self,
        depth,
        num_classes,
        widen_factor=1,
        droprate=0.0,
        use_bn=True,
        use_fixup=False,
        varnet=False,
        noise=0.0,
        lrelu=0.0,
        sigmaW = 1.0,
        init = None,
        dropl1 = False,
    ):
        super(WideResNet, self).__init__()

        nChannels = [16, 16 * widen_factor, 32 * widen_factor, 64 * widen_factor]

        assert (depth - 4) % 3 == 0, "You need to change the number of layers"
        n = (depth - 4) / 3

        BasicBlock.droprate = droprate
        BasicBlock.use_bn = use_bn
        BasicBlock.fixup_l = n * 3
        BasicBlock.use_fixup = use_fixup
        BasicBlock.varnet = varnet
        BasicBlock.sigmaW = sigmaW

        self.droprate = droprate
        self.dropl1 = dropl1
        ##print("Use fixup WideResnet:")
        ##print(use_fixup)
        ##print("Use BN WideResnet:")
        ##print(use_bn)
        block = BasicBlock

        self.conv1 = nn.Conv2d(
            3, nChannels[0], kernel_size=3, stride=1, padding=1, bias=False
        )

        k = (
            self.conv1.kernel_size[0]
            * self.conv1.kernel_size[1]
            * self.conv1.out_channels
        )

        gain = torch.nn.init.calculate_gain('relu')
        if varnet:
            self.conv1.weight.data.normal_(0, 1.0 * math.sqrt(sigmaW / k))
        else:
            ##makeLambdaDeltaOrthogonal(self.conv1.weight, self.conv1.bias, 1/3)
            ConstAvg(self.conv1.weight, self.conv1.bias)

        self.block1 = NetworkBlock(n, nChannels[0], nChannels[1], block, 1)
        self.block2 = NetworkBlock(n, nChannels[1], nChannels[2], block, 2)
        self.block3 = NetworkBlock(n, nChannels[2], nChannels[3], block, 2)

        self.relu = nn.ReLU(inplace=True)

##        self.relu = nn.Softplus()
##        self.relu = Swish()

        self.fc = nn.Linear(nChannels[3], num_classes)
        self.nChannels = nChannels[3]

        self.fc.bias.data.zero_()
        self.fc.weight.data.zero_()

        for m in self.modules():
            if isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def forward(self, x):
        out = self.conv1(x)
        if self.dropl1:
            out = F.dropout(out, p=self.droprate, training=self.training)

        out = self.block1(out)
        out = self.block2(out)
        out = self.block3(out)
        out = self.relu(out)
        out = F.avg_pool2d(out, 8)
        out = out.view(-1, self.nChannels)
        return self.fc(out)

def genOrthgonal(dim):
    a = torch.zeros((dim, dim)).normal_(0, 1)
    q, r = torch.qr(a)
    d = torch.diag(r, 0).sign()
    diag_size = d.size(0)
    d_exp = d.view(1, diag_size).expand(diag_size, diag_size)
    q.mul_(d_exp)
    return q

def makeLambdaDeltaOrthogonal(weights, bias, gain):
    rows = weights.size(0)
    cols = weights.size(1)
    if bias is not None:
        nn.init.constant_(bias, 0)
    dim = max(rows, cols)
    mid1 = weights.size(2) // 2
    mid2 = weights.size(3) // 2
    nn.init.constant_(weights, 0)
    weights.data[:, :, mid1, mid2] = gain ##torch.ones([rows, cols]) * gain

def ConstAvg(weights, bias, gain=1.0, const = 1.0):
    rows = weights.size(0)
    cols = weights.size(1)

    if const < 1.0:
        k = cols * weights.size(2) * weights.size(3)
        weights.data.normal_(0, math.sqrt(gain*(1.0-const) / k))
    else:
        nn.init.constant_(weights, 0)

    if bias is not None:
        nn.init.constant_(bias, 0)

    if const > 0.0:

        mid1 = weights.size(2) // 2
        mid2 = weights.size(3) // 2
        factor = const/cols

        weights.data[:, :, mid1, mid2] += factor

def ConstIdentity(weights, bias, gain, const = 1.0):

    rows = weights.size(0)
    cols = weights.size(1)

    assert(rows==cols)

    if const < 1.0:
        k = cols * weights.size(2) * weights.size(3)
        weights.data.normal_(0, math.sqrt(gain*(1.0-const) / k))
    else:
        nn.init.constant_(weights, 0)

    if bias is not None:
        nn.init.constant_(bias, 0)

    if const > 0.0:
        mid1 = weights.size(2) // 2
        mid2 = weights.size(3) // 2
##        factor = (1.0/cols*rows)*const
        factor = const

        for d0 in range(rows):
            weights.data[d0, d0, mid1, mid2] += factor


from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score
import seaborn as sb
from sklearn.decomposition import PCA

#load data
iris = sb.load_dataset("iris")
x = iris.drop("species",axis=1)
y = iris['species']

#pca
pca = PCA(n_components=3)
x_pca = pca.fit_transform(x) #เป็น array

#จับ PAC ยัดเข้า x
#show befor after              #เป็น DF
x['PAC1'] = x_pca[:,0]#ไม่เอาเเถวเอาเเต่คอลัม ที่0
x['PAC2'] = x_pca[:,1]
x['PAC3'] = x_pca[:,2]

x_train,x_test,y_train,y_test = train_test_split(x,y)

#COMPLETEDATA
x_train = x_train.loc[:,['PAC1','PAC2','PAC3']]
x_test = x_test.loc[:,['PAC1','PAC2','PAC3']]

model = GaussianNB()
model.fit(x_train,y_train)
y_pred = model.predict(x_test)

#accuracy score ปกติเเบบไม่ใช้ PCA =95%
print('Accuracy = ',accuracy_score(y_test,y_pred))


# -*- coding: utf-8 -*-
import re
import urllib.request
import threading
import queue
import time

def pretend():
    headers = {
        'Accept':
        'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding':
        'gb2312,utf-8',
        'Accept-Language':
        'zh-CN,zh;q=0.8',
        'Connection':
        'keep-alive',
        'User-Agent':
        'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235',
    }
    opener = urllib.request.build_opener(urllib.request.HTTPHandler)
    headall = []
    for key, value in headers.items():
        item = (key, value)
        headall.append(item)
    opener.addheaders = headall
    urllib.request.install_opener(opener)

class A(threading.Thread):
    def __init__(self, ipque, num, t):
        threading.Thread.__init__(self)
        self.num = num
        self.ipque = ipque
        self.t = t

    def run(self):
        url = "http://www.xicidaili.com/" + self.t
        pat1 = '<tr class=.*?
'
        pat2 = '(.*?)	'
        for i in range(int(self.num)):
            data1 = urllib.request.urlopen(url + str(i + 1)).read().decode(
                "utf-8", errors="ignore")
            data2 = re.compile(pat1, re.S).findall(data1)
            for data in data2:
                data3 = re.compile(pat2).findall(str(data))
                ip = data3[0]
                port = data3[1]
                ip_port = ip + ":" + port
                self.ipque.put(ip_port)
                self.ipque.task_done()
                time.sleep(0.2)

class B(threading.Thread):
    def __init__(self, ipque, time, ipList):
        threading.Thread.__init__(self)
        self.ipque = ipque
        self.time = time
        self.ipList = ipList

    def run(self):
        while (self.ipque.empty()):
            time.sleep(3)
        url = "http://www.baidu.com"

        while (not self.ipque.empty()):
            ip = self.ipque
            ipaddr = ip.get()
            try:
                proxy = urllib.request.ProxyHandler({'http': ipaddr})
                opener = urllib.request.build_opener(
                    proxy, urllib.request.HTTPHandler)
                opener.open(url, timeout=self.time)
                print("Success\t\t" + ipaddr)
                self.ipList.append(ipaddr)
            except:
                print("Fail\t\t" + ipaddr)

def main():
    print('国内高匿代理 ------ 1')
    print('国内普通代理 ------ 2')
    print('国内HTTPS代理 ------ 3')
    print('国内HTTP代理 ------ 4')
    temp = int(input('请选择类型（1-4）：'))
    Type = ['nn/', 'nt/', 'wn/', 'wt/']
    ty = Type[temp - 1]

    pageNum = int(input("输入查取的页数（每页存在100IP）："))
    time = int(input("输入网页超时时间（用于验证IP）："))
    n = int(input("输入验证线程数量："))
    ipque = queue.Queue()
    pretend()
    ipList = []
    threads = []

    t = A(ipque, pageNum, ty)
    threads.append(t)
    for i in range(n):
        t = B(ipque, time, ipList)
        threads.append(t)

    for i in range(len(threads)):
        threads[i].start()

    for i in range(len(threads)):
        threads[i].join()

    print("验证完毕！\r\n以下IP可用")
    for ip in ipList:
        print(ip)

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
"""
Created on Mon May 10 08:48:30 2021

@author: Koma
"""
import os
import json
import requests
from Upload_Artworks import invoke_log_CC

# -------------------------------------------------------------------
# Get owner and creator of the artwork through calling artwork_CC API
# -------------------------------------------------------------------
def get_roles(api, tokenID):
    par = {"tokenID": tokenID}

    try:
        r = requests.get(api, json=par)
        r.raise_for_status()
        res = json.loads(r.text)  #{'response': '{"Creator": ...}'}
        res = json.loads(res['response'])  #{'Creator': 'Koma', 'Owner': ...}

        return res
    except requests.exceptions.RequestException as e:
        # Whoops 200 isn't return
        print(e)
        return 0

# -------------------------------------------------------------------
# Get owner and creator of the artwork through calling artwork_CC API
# -------------------------------------------------------------------
def change_balance(api, newCollector, collector, creator, price, r_y):
    par = {"newCollector": newCollector, "collector": collector, "creator": creator, "price": price, "r_y": r_y}

    try:
        r = requests.post(api, json=par)
        r.raise_for_status()
        print(r.text)
    except requests.exceptions.RequestException as e:
        # Whoops 200 isn't return
        print(e)

# ----------------------
# Invoke artowrk_CC API
# ----------------------
def invoke_artwork_CC(api, tokenID, newCollector, multiHash):
    par = {"tokenID": tokenID, "newCollector": newCollector, "multihash": multiHash}
    try:
        r = requests.post(api, json=par)
        r.raise_for_status()
        print(r.text)
        return 1
    except requests.exceptions.RequestException as e:
        # Whoops 200 isn't return
        print(e)
        return 0

if __name__== "__main__":
    # (1) Invoke artwork_CC
    # ----------------------
    tokenID = input('Input tokenID: ')
    api = "http://140.123.105.138:8080/spchain/readArtwork/"
    res = get_roles(api, tokenID)

    # (2) Invoke wallet_CC
    # ---------------------
    creator, owner = res['Creator'], res['Owner']
    r_y = str(input('Input royalty rate: '))
    newCollector = input('Input new collector: ')
    price = str(input('Input price: '))  # We simplify query the price from orbitdb here

    api = "http://140.123.105.138:8080/spchain/transferBalance/"
    change_balance(api, newCollector, owner, creator, price, r_y)

    # (3) Update orbitdb information
    # ----------------------------------------------------------------
    # -> node setup.js transfer -t $tokenID -n $newCollector -o $owner

    # (4) Invoke artwork_CC
    # ---------------------
    api = "http://140.123.105.138:8080/spchain/transferArtwork/"
    multiHash = input('multi-hash: ')
    res = invoke_artwork_CC(api, tokenID, newCollector, multiHash)

    # (5) Invoke log_CC
    # -----------------
    api = "http://140.123.105.138:8080/spchain/addLog/"
    path = os.path.join(os.getcwd(), newCollector)
    operation = "Transfer the artwork"
    invoke_log_CC(api, path, res, operation)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Ed Mountjoy
#
"""
Takes the output of multi loci clustering script and outputs a file containing
snps on which to condition.
"""

import sys

def main():

    # Args
    inf = sys.argv[1]
    outf = sys.argv[2]
    index_snp = sys.argv[3]

    # Find conditional snps
    conditional_snps = None
    with open(inf, "r") as in_h:
        in_h.readline()
        for line in in_h:
            parts = line.rstrip().split()
            if parts[0] == index_snp:
                try:
                    conditional_snps = parts[1].split(";")
                except IndexError:
                    conditional_snps = []

    # Raise error if none are found
    if conditional_snps is None:
        sys.exit("Error: {0} not found in {1}".format(index_snp, inf))

    # Write output
    with open(outf, "w") as out_h:
        for conditional_snp in conditional_snps:
            out_h.write(conditional_snp + "\n")

    return 0

if __name__ == '__main__':

    main()


import numpy as np
import tensorflow as tf
import cv2
import time

class_names = ['With mask', 'Without mask']

def detect_faces_and_classify(img, face_cascade, model):
    global class_names
    # obtenemos las caras usando el detector aplicado a la imagen
    faces = face_cascade.detectMultiScale(img, 1.1, 4)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    for i, (x, y, w, h) in enumerate(faces):
        # sacamos la region de interes
        roi = gray[y:y + h, x:x + w]
        # resize para ingresar al modelo
        resized = cv2.resize(roi, (180, 180))
        # normalized = resized/255.
        reshaped = np.reshape(resized, (1, 180, 180, 1))

        # obtenemos las probabilidades para cada clase
        predictions = model.predict(reshaped)
        score = tf.nn.softmax(predictions[0])
        # generamos la label partiendo de las probabilidades
        label = np.argmax(score)

        # mostramos el recuadro con label rojo o verde dependiendo de lo obtenido
        # Red color for no mask
        color = (0, 0, 255)
        if label == 0:
            # Green color for mask
            color = (0, 255, 0)

        # agregamos la label y el rectangulo a la imagen
        cv2.putText(img, class_names[label], (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.75, color, 2, cv2.LINE_AA)
        cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)

# para probar la clasificacion en imagenes
def face_classifier():
    # tomamos el modelo guardado (proveniente del collab)
    model_path = "./saved_model/classifier"
    # cargamos el modelo
    model = tf.keras.models.load_model(model_path)
    # elegimos el detector de caras
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

    # leemos las imagenes
    img = cv2.imread('./with_mask/1.png')
    img2 = cv2.imread('./without_mask/1.png')

    # le pasamos al metodo la imagen, el detector de caras y el modelo. Obtenemos la misma imagen con tags
    detect_faces_and_classify(img, face_cascade, model)
    detect_faces_and_classify(img2, face_cascade, model)

    # mostramos las imagenes
    cv2.imshow('With Mask', img)
    cv2.imshow('Without Mask', img2)
    cv2.waitKey()

def video_capture_classifier():
    # tomamos el modelo guardado (proveniente del collab)
    model_path = "./saved_model/classifier"
    # cargamos el modelo
    model = tf.keras.models.load_model(model_path)
    # elegimos el detector de caras
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

    # capturamos imagenes de la webcam
    cap = cv2.VideoCapture(0)

    while True:
        _, frame = cap.read()
        flipped = cv2.flip(frame, 1)
        # a medida que lee cada frame, le pasamos el frame a la funcion. Obtenemos frame con tags
        detect_faces_and_classify(flipped, face_cascade, model)

        # mostramos el frame
        cv2.namedWindow('MaskNet', cv2.WINDOW_NORMAL)
        cv2.imshow('MaskNet', flipped)
        cv2.resizeWindow('MaskNet', 1000, 750)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

def video_classifier():
    # tomamos el modelo guardado (proveniente del collab)
    model_path = "./saved_model/classifier"
    # cargamos el modelo
    model = tf.keras.models.load_model(model_path)
    # elegimos el detector de caras
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

    # elegimos el path del video que queremos seleccionar
    video_path = "diego2rapido.mp4"
    # video_path = "florblancorapido.mp4"

    cap = cv2.VideoCapture(video_path)

    while cap.isOpened():
        ret, frame = cap.read()

        if not ret:
            print("Can't receive frame (stream end?). Exiting ...")
            break

        flipped = cv2.flip(frame, 1)
        # por cada frame del video aplicamos la funcion, obtenemos el frame con tags
        detect_faces_and_classify(flipped, face_cascade, model)

        # mostramos el frame
        cv2.namedWindow('MaskNet', cv2.WINDOW_NORMAL)
        cv2.imshow('MaskNet', flipped)
        cv2.resizeWindow('MaskNet', 1000, 750)
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break

    cap.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    # face_classifier()
    # video_capture_classifier()
    video_classifier()


from unittest import TestCase

from openslides_backend.shared.patterns import (
    Collection,
    FullQualifiedField,
    FullQualifiedId,
)

from .utils import Client, ResponseWrapper, create_test_application

class WSGIApplicationTester(TestCase):
    """
    Tests the WSGI application in general.
    """

    def setUp(self) -> None:
        self.application = create_test_application(
            user_id=0, view_name="ActionsView"
        )  # User is anonymous

    # def test_create_application(self) -> None:
    #     # This test does not use our test application but the real one.
    #     from openslides_backend.main import application
    #
    #     self.assertTrue(isinstance(application, OpenSlidesBackendESGIApplication))

    # def test_create_application_2(self) -> None:
    #     # This test does not use our test application but the real one.
    #     from openslides_backend.main import create_application
    #
    #     os.environ["OPENSLIDES_BACKEND_DEBUG"] = "1"
    #     app = create_application()
    #     self.assertTrue(isinstance(app, OpenSlidesBackendApplication))

    def test_wsgi_request_wrong_method(self) -> None:
        client = Client(self.application, ResponseWrapper)
        response = client.get("/")
        self.assertEqual(response.status_code, 405)

    def test_wsgi_request_wrong_media_type(self) -> None:
        client = Client(self.application, ResponseWrapper)
        response = client.post("/")
        self.assertEqual(response.status_code, 400)
        self.assertIn("Wrong media type.", str(response.data))

    def test_wsgi_request_missing_body(self) -> None:
        client = Client(self.application, ResponseWrapper)
        response = client.post("/", content_type="application/json")
        self.assertEqual(response.status_code, 400)
        self.assertIn("Failed to decode JSON object", str(response.data))

    def test_wsgi_request_fuzzy_body(self) -> None:
        client = Client(self.application, ResponseWrapper)
        response = client.post(
            "/", json={"fuzzy_key_Eeng7pha3a": "fuzzy_value_eez3Ko6quu"},
        )
        self.assertEqual(response.status_code, 400)
        self.assertIn("data must be array", str(response.data))

    def test_wsgi_request_fuzzy_body_2(self) -> None:
        client = Client(self.application, ResponseWrapper)
        response = client.post(
            "/", json=[{"fuzzy_key_Voh8in7aec": "fuzzy_value_phae3iew4W"}],
        )
        self.assertEqual(response.status_code, 400)
        self.assertIn(
            "data[0] must contain [\\'action\\', \\'data\\'] properties",
            str(response.data),
        )

    def test_wsgi_request_no_existing_action(self) -> None:
        client = Client(self.application, ResponseWrapper)
        response = client.post(
            "/", json=[{"action": "fuzzy_action_hamzaeNg4a", "data": [{}]}],
        )
        self.assertEqual(response.status_code, 400)
        self.assertIn(
            "Action fuzzy_action_hamzaeNg4a does not exist.", str(response.data)
        )

    def test_health_route(self) -> None:
        client = Client(self.application, ResponseWrapper)
        response = client.get("/health")
        self.assertEqual(response.status_code, 200)
        self.assertIn("healthinfo", str(response.data))

class TypesTester(TestCase):
    """
    Tests some utils and types
    """

    def test_collection(self) -> None:
        collection = Collection("collection_vi8cah2Eih")
        self.assertEqual(str(collection), "collection_vi8cah2Eih")

    def test_collection_repr(self) -> None:
        collection = Collection("collection_yag3Boaqui")
        self.assertEqual(repr(collection), "Collection collection_yag3Boaqui")

    def test_collection_comparing(self) -> None:
        collection_1 = Collection("collection_aeboo3ieSh")
        collection_2 = Collection("collection_aeboo3ieSh")
        self.assertEqual(collection_1, collection_2)

    def test_collection_hashing(self) -> None:
        collection = Collection("collection_Din9chosoo")
        self.assertEqual(hash(collection), hash("collection_Din9chosoo"))

    def test_full_qualified_id(self) -> None:
        fqid = FullQualifiedId(Collection("collection_Aid6ahdooT"), 8283937728)
        self.assertEqual(str(fqid), "collection_Aid6ahdooT/8283937728")

    def test_full_qualified_id_repr(self) -> None:
        fqid = FullQualifiedId(Collection("collection_oozaiX1pee"), 7099085886)
        self.assertEqual(repr(fqid), "FQId collection_oozaiX1pee/7099085886")

    def test_full_qualified_id_comparing(self) -> None:
        fqid_1 = FullQualifiedId(Collection("collection_reeyie3Woo"), 2133862900)
        fqid_2 = FullQualifiedId(Collection("collection_reeyie3Woo"), 2133862900)
        self.assertEqual(fqid_1, fqid_2)

    def test_full_qualified_id_hashing(self) -> None:
        fqid = FullQualifiedId(Collection("collection_ia5Ooyuiso"), 9638688299)
        self.assertEqual(hash(fqid), hash("collection_ia5Ooyuiso/9638688299"))

    def test_full_qualified_field(self) -> None:
        fqfield = FullQualifiedField(
            Collection("collection_Shoo1uut4u"), 7208641662, "field_ais1aBau6d"
        )
        self.assertEqual(
            str(fqfield), "collection_Shoo1uut4u/7208641662/field_ais1aBau6d"
        )

    def test_full_qualified_field_repr(self) -> None:
        fqfield = FullQualifiedField(
            Collection("collection_yaiS2uthi8"), 78718784720, "field_ueth3Rohv8"
        )
        self.assertEqual(
            repr(fqfield), "FQField collection_yaiS2uthi8/78718784720/field_ueth3Rohv8"
        )

    def test_full_qualified_field_comparing(self) -> None:
        fqfield_1 = FullQualifiedField(
            Collection("collection_ioMohcui0u"), 7208641662, "field_epee2jeRee"
        )
        fqfield_2 = FullQualifiedField(
            Collection("collection_ioMohcui0u"), 7208641662, "field_epee2jeRee"
        )
        self.assertEqual(fqfield_1, fqfield_2)

    def test_full_qualified_field_hashing(self) -> None:
        fqfield = FullQualifiedField(
            Collection("collection_ohf3Thoo9i"), 8432643375, "field_Raechee5ee"
        )
        self.assertEqual(
            hash(fqfield), hash("collection_ohf3Thoo9i/8432643375/field_Raechee5ee")
        )

    def test_full_qualified_field_fqid(self) -> None:
        fqfield = FullQualifiedField(
            Collection("collection_quephah8Oo"), 3148072663, "field_Ein2Aos0Ku"
        )
        self.assertEqual(
            fqfield.fqid,
            FullQualifiedId(Collection("collection_quephah8Oo"), 3148072663),
        )


# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.linux-x86_64/egg/Labtools/Apps/PyMouse-MPU6050-accel-control.py
# Compiled at: 2015-05-19 05:34:01
import numpy as np
from Labtools import interface
import pymouse
P = pymouse.PyMouse()
if __name__ == '__main__':
    I = interface.Interface()
    from gyro import *
    gyro = MPU6050(I)
    noise1 = []
    noise2 = []
    for a in range(500):
        x, y, z = gyro.getAccel()
        noise1.append(np.arcsinh(x * np.pi))
        noise2.append(np.arcsinh(y * np.pi))

    std1 = np.std(noise1)
    std2 = np.std(noise2)
    var1 = 0.0001
    var2 = 0.0001
    eVar1 = std1 ** 2
    eVar2 = std2 ** 2
    K1 = KalmanFilter(var1, eVar1)
    K2 = KalmanFilter(var2, eVar2)
    while 1:
        x, y, z = gyro.getAccel()
        K1.input_latest_noisy_measurement(np.arcsinh(x * np.pi))
        K2.input_latest_noisy_measurement(np.arcsinh(y * np.pi))
        V1 = K1.get_latest_estimated_measurement()
        V2 = K2.get_latest_estimated_measurement()
        P.move((V2 + 0.5) * 4 * 400, (V1 + 0.5) * 4 * 200)


#!/usr/bin/env python

from __future__ import print_function
from vm import VMManager
import time

VM_PREFIX = "aos"

if __name__ == '__main__':
    manager = VMManager()
    vms=manager.getRunningVMNames(filterPrefix=VM_PREFIX)
    for vmname in vms:
        manager.shutdownVM(vmname)
    time.sleep(5)


from __future__ import print_function
import requests
import argparse
import json
from csiot import util

def enroll_node_cert(addr, cn):
	host, port = addr
	res = requests.get("http://%s:%s/enroll/cert?common_name=%s" % (host, port, cn))
	if res.status_code != requests.codes.ok:
		print("A server-side error occurred while enrolling certificate")
		return

	data = json.loads(res.content.decode("UTF-8"))

	util.store_cert("server", data["pem"])
	util.store_private_key("server", data["key"])
	util.store_cacert(data["ca"])

def enroll_node(addr, common_name):
	host, port = addr
	res = requests.get("http://%s:%s/enroll/node" % (host, port))
	if res.status_code != requests.codes.ok:
		print("A server-side error occurred while enrolling for a node-key")
		return

	data = json.loads(res.content.decode("UTF-8"))
	util.store_key("node", "node", data["node-key"])

	enroll_node_cert(addr, common_name)

def enroll_service(addr):
	host, port = addr
	res = requests.get("http://%s:%s/enroll/service" % (host, port))
	if res.status_code != requests.codes.ok:
		print("A server-side error occurred while enrolling for a service-key")
		return

	data = json.loads(res.content.decode("UTF-8"))
	util.store_key("node", "service", data["service-key"])

if __name__ == "__main__":
	parser = argparse.ArgumentParser()
	# node and service should take hostname:port pairs of their counterparts
	# e.g. python enroll.py --service node.domain.net:6667
	#      python enroll.py --node master.domain.net:6666
	parser.add_argument("--node", dest='node', required=False)
	parser.add_argument("--service", dest='service', required=False)
	parser.add_argument("--common-name", dest="common_name", default="localhost")
	args = parser.parse_args()

	# Default port 5000
	if args.node:
		if ':' not in args.node:
			args.node += ":5000"

		enroll_node(args.node.split(":"), args.common_name)

	if args.service:
		enroll_service(args.node.split(":"))


# errorManager.py

import traceback

error_msg = None

def getTracebackStr():
    lines = traceback.format_exc().strip().split('\n')
    if error_msg is not None:
        rl = [error_msg, lines[-1]]
    else:
        rl = [lines[-1]]
    lines = lines[1:-1]
    lines.reverse()
    for i in range(0, len(lines), 2):
        rl.append('  %s at %s' % (lines[i].strip(), lines[i + 1].strip()))
    return '\n'.join(rl)


class DQNAgent :
    def __init__(self, env, state_size, action_size) :

        self.state_size = state_size
        self.action_size = action_size
        self.memory = deque(maxlen = 5000)
        self.gamma = 0.995  # discount rate
        self.exploration_rate = 1.  # exploration rate
        self.original_epsilon = 1.
        self.min_epsilon = 0.01
        self.exploration_rate_decay = 150
        #        self.n_episodes = 2500 * 200
        self.n_game_max = 300
        self.model = self._build_model()
        self.model.summary()
        np.random.seed(42)

    def _build_model(self) :
        input_state = Input((self.state_size,))
        model = Dense(30, activation = "tanh")(input_state)
        q_value = Dense(self.action_size, activation = "linear")(model)
        dqn = Model([input_state], q_value)
        dqn.compile(optimizer = tf.keras.optimizers.Adam(lr = 0.01), loss = "mse")
        return dqn

    def remember(self, state, action, reward, next_state, done) :
        self.memory.append((state, action, reward, next_state, done))

    def act(self, state) :
        if np.random.rand() <= self.exploration_rate :
            return np.random.randint(self.action_size)
        act_values = self.model.predict(state)
        return np.argmax(act_values[0])  # returns action

    def replay(self, batch_s) :

        input_batch, action_batch, reward_batch, next_state_batch = [], [], [], []
        minibatch = random.sample(self.memory, min(len(self.memory), batch_s))
        targets = []
        states = []
        for state, action, reward, next_state, done in minibatch :
            # DQN FIT
            vanilla_target = reward
            if not done :
                # Double DQN
                vanilla_target = reward + self.gamma * np.amax(self.model.predict(next_state)[0])
            vanilla_target_f = self.model.predict(state)
            vanilla_target_f[0][action] = vanilla_target
            # Train the Neural Net with the state and target_f
            states.append(state[0])
            targets.append(vanilla_target_f[0])
        self.vanillaLoss = self.model.fit(np.array(states), np.array(targets), epochs = 1, verbose = 0).history["loss"][
            0]

    def run(self) :
        done = False
        batch_size = 32
        state = env.reset()
        state = np.reshape(state, (1, self.state_size))
        self.cumulative_reward = 0
        score = []
        epsilon_logger = []
        score_average = []
        cpt_game = 0

        for timer in range(100000) :
            action = self.act(state)
            next_state, reward, done, _ = env.step(action)
            next_state = np.reshape(next_state, (1, self.state_size))
            self.remember(state, action, reward, next_state, done)

            self.cumulative_reward += reward
            state = next_state
            if done :
                cpt_game += 1
                state = env.reset()
                score_average.append(np.mean(score[-10 :]))
                state = np.reshape(state, (1, self.state_size))
                score.append(self.cumulative_reward)
                print("episode: {}, score: {}, e: {:.2} "
                      .format(cpt_game, self.cumulative_reward, self.exploration_rate))
                self.cumulative_reward = 0
                loss = self.replay(batch_size)
                self.exploration_rate = max(
                    self.exploration_rate - (self.original_epsilon / self.exploration_rate_decay), \
                    self.min_epsilon)
                epsilon_logger.append(self.exploration_rate)
            if timer % 1000 == 0 :
                clear_output(True)
                self.model.summary()
                plt.subplot(311)
                axis = plt.gca()
                axis.set_ylim([0, 200])
                plt.plot(score, label = "Cumulative reward ")
                plt.legend()
                plt.grid()
                plt.show()
                plt.subplot(312)

                axis = plt.gca()
                axis.set_ylim([0, 200])
                plt.plot(score_average, label = "Cumulative Mean reward ")
                plt.legend()
                plt.grid()
                plt.show()
                plt.subplot(313)

                axis = plt.gca()
                axis.set_ylim([0, 1])
                plt.plot(epsilon_logger, label = "Epsilon")
                plt.grid()
                plt.legend()
                plt.show()


###############################################################################
# MIT OCW 6.00.x Intro to Computer Science and Programing 2008
# http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/index.htm
# -----------------------------------------------------------------------------
# Fibonacci recursion programming example from lecture:
# Assumes 1 pair rabbits at month 0 that can begin mating at month 1
# All future generations of rabbits can and do begin mating at 1 month old at
# a rate of one new pair of rabbits per month.
# -- modified to include user input, reference to the historical rabbit
# problem, and an attempt at visualizing the flow of # execution of the
# recursive function --
###############################################################################

def fib(x):
    """Return fibonacci of x, where x is a non-negative int"""
    space = ' ' * (4 * x)
    print(space + "x = " + str(x))

    if x == 0:
        fibNum = 1
        print(space + "returning " + str(fibNum))
        return fibNum
    if x == 1:
        fibNum = 1
        print(space + "returning " + str(fibNum))
        return fibNum
    else:
        fibNum = fib(x - 1) + fib(x - 2)
        print(space + "returning " + str(fibNum))
        return fibNum

print("Hello.")

number = input("For how many months will the rabbits be mating? ")
number = int(number)

result = fib(number)

print("result = " + str(result))


import sys
# binary search 二分查找算法,针对的是有序数组
# 四个经典的变种问题，有序数组中可能存在重复元素
# 查找第一个值等于给定值的元素
def bsFindIndex(arr,val):
  if arr[0] == val:
    return 0
  low = 0
  high = len(arr) - 1
  while low <= high:
    mid = low + (high-low)//2
    if arr[mid] == val:
      # 找到其中一个值，检查它的左边，如果相等，继续二分
      if arr[mid-1] != val:
        return mid
      else:
        high = mid - 1
    elif arr[mid] > val:
      high = mid - 1
    else:
      low = mid + 1
  return None

# 查找最后一个值等于给定值的元素，与上述类似
def bsFindLastIndex(arr,val):
  low = 0
  high = len(arr) - 1
  if arr[high] == val:
    return high
  while low <= high:
    mid = low + (high-low)//2
    if arr[mid] == val:
      # 找到其中一个值，检查它的右边
      if arr[mid+1] != val:
        return mid
      else:
        low = mid + 1
    elif arr[mid] > val:
      high = mid - 1
    else:
      low = mid + 1
  return None

# 查找第一个 >= 给定值的元素
def bsFindGreaterIndex(arr,val):
  if arr[0] >= val:
    return 0
  low = 0
  high = len(arr) - 1
  while low <= high:
    mid = low + (high - low) // 2
    if arr[mid] < val:
      low = mid + 1
    else: # 大于等于 val 的同时检查左边
      if arr[mid-1] < val:
        return mid
      else:
        high = mid - 1
  return None

# 查找最后一个小于等于给定值的元素
def bsFindLastSmallerIndex(arr,val):
  leng = len(arr)
  if arr[-1] <= val:
    return leng
  low = 0
  high = leng
  while low <= high:
    mid = low + ((high - low) >> 1)
    if arr[mid] > val:
      high = mid - 1
    else:
      # 检查右边
      if arr[mid+1] > val:
        return mid
      else:
        low = mid + 1
  return None

# 找出目标值在数组的下标，或者插入数组后应得的下标
def searchInsert(nums,target):
  if nums[0] >= target:
    return 0
  low = 0
  high = len(nums) - 1
  while low <= high:
    mid = low + ((high-low)>>1)
    val = nums[mid]
    if val == target:
      return mid
    elif val > target:
      high = mid - 1
    else:
      if mid == len(nums)-1 or nums[mid+1] > target:
        return mid + 1
      else:
        low = mid + 1

# 平方根的整数位
import math
def mySqrt(x:int) -> int:
  if x == 0:
    return 0
  low = 1
  high = x
  while low <= high:
    mid = low +((high-low)>>1)
    if mid*mid > x:
      high = mid - 1
    else:
      if (mid+1)*(mid+1) > x:
        return math.floor(mid)
      low = mid + 1

# 有序数组中寻找和为给定值的两个元素
def twoSum(numbers,target):
  leng = len(numbers)
  hashmap = set(numbers)
  for i in range(leng):
    temp = target - numbers[i]
    if temp in hashmap:
      idx2 = bsFindLastIndex(numbers,temp)
      return [i+1,idx2+1]
  return None

# 给一个API判断版本好坏，用最少的调用次数判断第一个损坏的版本
def isBadVersion(v):
  pass
def firstBadVersion(n):
  low = 1
  high = n
  while low <= high:
    mid = ((low+high)>>1)
    if isBadVersion(mid):
      if mid == 1 or not isBadVersion(mid-1): # 第一个坏的，或者前一个是好的，说明当前就是所求
        return mid
      # 否则继续往前找
      high = mid - 1
    else:
      low = mid + 1
  return None

# 判断是否能完美开方
def isPerfectSquare(num):
  low = 1
  high = num
  while low <= high:
    mid = low + ((high-low)>>1) # 整除，最小间隔就是 1
    if mid*mid > num:
      high = mid - 1
    else:
      if mid*mid == num:
        return True
      else:
        low = mid + 1
  if low > high:
    return False

# 子串匹配，假设有 1 billion 个子串需要匹配
# 双指针很简洁，但只能做到 O(N)，需要换一种思路
# 预处理主串，维护一个哈希表，key 是字符，val 是字符下标组成的数组（有序）
# 子串匹配了上一个字符，那么下一个字符如果匹配，下标一定大于 prev
# 问题就转化为：有序数组中找到第一个大于等于 prev 值的元素，O(logN)
def isSubsequence(s,t):
  hashmap = {}
  for i in range(len(t)):
    cha = t[i]
    if cha not in hashmap:
      hashmap[cha] = []
    hashmap[cha].append(i)
  prev = -1 # 记录上一个匹配字符在主串对应的下标，初始设为 0
  for j in range(len(s)):
    cha2 = s[j]
    if cha2 not in hashmap:
      return False # 哈希表中没有下标记录，说明不匹配
    arr = hashmap[cha2]
    # 下标组成的有序数组中，查找第一个大于等于给定值的元素，需要注意的是
    # 如果相邻的字符相同，那么此次需要找到大于 prev 的值
    current = bsFindGreaterIndex(arr,prev)
    if current is None:
      return False
    elif current == len(arr) - 1 and arr[current] == prev:
      return False
    if arr[current] == prev:
      # 注意我们要的是主串中的下标，所以这里取值
      prev = arr[current+1]
    else:
      prev = arr[current]
  return True

# 给定房子、加热器的坐标有序数组，求满足全覆盖的最小半径
# 对每一所房子，找出它离左右加热器的距离，取较小的那个
# 对上述计算所得的所有距离，取最大的那个
# TODO
def findRadius(houses,heaters):
  res = 0
  for i in houses:
    rightIdx = bsFindGreaterIndex(heaters,i)
    if rightIdx is not None:
      right = heaters[rightIdx] - i
      if rightIdx >= 1:
        left = i - heaters[rightIdx-1]
      else:
        left = sys.maxsize
    else:
      right = sys.maxsize
      left = i - heaters[-1]
    temp = min([left,right])
    res = max([res,temp])
  return res

# 给一个山峰型数组，找出山顶元素
def peakIndexInMountainArray(A=[]):
  low = 0
  high = len(A) - 1
  while low <= high:
    mid = low + ((high-low)>>1)
    if A[mid] < A[mid+1]:
      low = mid + 1
    else:
      if A[mid-1] < A[mid]:
        return mid
      else:
        high = mid - 1

# !有序数组在某个点被切开，比如456012，求最小值
# 取最左侧的值来跟 mid 不断比较缩进
def findMin(nums):
  low = 0
  high = len(nums) - 1
  while low <= high:
    if nums[low] <= nums[high]:
      return low
    mid = low + ((high - low) >> 1)
    # 左侧有序，往右边找
    if nums[low] <= nums[mid]:
      low = mid + 1
    else:
      # mid 有可能是目标值，因此不能减1
      high = mid

# !有序数组被分成两段，比如 456123，求目标值下标
# 需要将问题转化为可以使用二分查找的模型
# 当 target 与 mid 在同一侧时，按正常缩减范围
# 当 target 与 mid 在不同侧时，事实上都不需要比较，只要继续往 target 那一侧缩减范围
# 但是为了简化代码的目的，我们维护一个 comparetor
# 说实话这种解法很难想得到，建议下面的解法 ↓
def search(nums,target):
  low = 0
  high = len(nums) - 1
  # 维护一个 comparetor，目的是简化代码
  comparetor = None
  while low <= high:
    mid = low + ((high-low)>>1)
    # 如果在同一侧
    if (nums[mid] < nums[0]) == (target < nums[0]):
      comparetor = nums[mid]
    # target 在右侧，将 mid 看作 -Inf 继续模拟二分查找
    elif target < nums[0]:
      comparetor = -sys.maxsize
    else:
      comparetor = sys.maxsize
    # 开始标准的二分查找
    if comparetor == target:
      return mid
    elif comparetor < target:
      low = mid + 1
    else:
      high = mid - 1
  return -1

# 平易近人的解法：首先我们找出波谷的位置，然后取其中一边来 BS 即可
def searchRotated(nums,target):
  minIdx = findMin(nums)
  n = len(nums)
  # 将目标值与最右侧比较，来确定在哪边
  low = minIdx if target <= nums[-1] else 0
  high = n - 1 if target <= nums[-1] else minIdx - 1
  # 正常二分
  while low <= high:
    mid = low + (high - low)//2
    if nums[mid] == target:
      return mid
    elif nums[mid] < target:
      low = mid + 1
    else:
      high = mid - 1

# 接上题，假如有序数组中有重复元素
# 当 nums[mid] 等于 nums[0] 的时候，上题的左右判断会有问题
# 需要换一种思路，每次找出有序的一半，如果target在范围内就进行二分，否则找另一半
# TODO
def searchPro(nums,target):
  pass

# 有序的二维数组（矩阵）里，检查元素是否存在
def searchMatrix(matrix,target):
  # 桶排序的思想，以及二分查找的变种问题：
  # 先找到最后一个 nums[0] 小于等于 target 的子数组，然后检查它
  low = 0
  high = len(matrix) - 1
  # 先尝试找到目标子数组
  targetArr = None
  while low <= high:
    mid = low + ((high-low)>>1)
    head = matrix[mid][0]
    if head > target:
      high = mid - 1
    else:
      if mid == len(matrix)-1 or matrix[mid+1][0] > target:
        targetArr = matrix[mid]
        break
      else:
        low = mid + 1
  if targetArr is None:
    return False
  # 正常二分查找检查是否存在
  low = 0
  high = len(targetArr) - 1
  while low <= high:
    mid = low + ((high-low)>>1)
    midVal = targetArr[mid]
    if midVal == target:
      return True
    elif midVal < target:
      low = mid + 1
    else:
      high = mid - 1
  return False

# 上题的另一种解法，（整齐的）二维矩阵下标，与一维数组下标是可以互相转换的
# 下标转换后，因此当作常规的二分查找即可
def searchMatrixPro(matrix,target):
  n = len(matrix)
  m = len(matrix[0])
  low = 0
  high = n * m - 1
  while low <= high:
    mid = low + ((high - low) >> 1)
    # 将一维数组下标转换为矩阵下标
    val = matrix[mid//m][mid%m]
    if val == target:
      return True
    elif val < target:
      low = mid + 1
    else:
      high = mid - 1
  return False

# 寻找最小连续 subArray 使得 sums >= s
def findMinSubArray(nums,s):
  pass

# 给定字符串 s 和 t，寻找最小连续区间，使得区间包含 t 的所有字符
# 经典的 subArray/Sliding window 问题：双指针 + minD + hashmap
# 快指针 j 向右遍历，每当 window 满足条件时，循环尝试将慢指针 i 右移一位
# 在这过程中尝试更新 minD 以及它对应的 i 下标
# 当快指针走到最右时，流程结束，minD 及其对应的 i 即为所求
# 优化点：此题是字符串，颗粒度更细，不需要每次完整判断 window 是否满足条件，只需判断左右边界即可
# 我们维护 t 的字符哈希表以及一个计数器 counter 来实现这个优化点
def minWindow(s: str, t: str) -> str:
  minD = sys.maxsize
  hashmap = {}
  strCache = {} # t 字符哈希表
  counter = len(t) # 可以理解为不匹配度
  i = 0
  for c in t:
    if c not in strCache:
      strCache[c] = 1
    else:
      strCache[c] += 1
  # 快指针开始遍历
  for j in range(len(s)):
    if s[j] not in strCache:
      strCache[s[j]] = 0
    elif strCache[s[j]] > 0:
      counter -= 1
    # !无论当前字符是否存在 t 中，都减去 1，这样 window 中那些多余字符，值会小于 0
    strCache[s[j]] -= 1
    # 最简化判断 window 是否满足条件
    while counter == 0:
      if j - i < minD:
        minD = j - i
        hashmap[minD] = i
      # i 向右移之前，检查 s[i] 是否为匹配 t 的必须条件
      # tricky part：字符哈希表值如果小于0，说明 window 里面字符有多余，相反则不够用
      strCache[s[i]] += 1 # 上面无差别减 1，这里需要加回去，可以认为不匹配程度上升
      if strCache[s[i]] > 0:
        counter += 1 # 不匹配度 +1
      i += 1
  # 流程结束，取当前的 minD
  if minD == sys.maxsize:
    return ''
  return s[hashmap[minD]:hashmap[minD]+minD+1]

# !上述很多求最长/最短 sliding window 的变种问题，核心往往是如何高效判断 window 是否满足条件

# 二维矩阵 row、col 都是有序，检查目标值是否存在
def searchMatrix2(matrix,target):
  # 先找出最后一个 num[0] 小于等于 target 的数组 k
  low = 0
  high = len(matrix) - 1
  k = None
  while low <= high:
    mid = low + ((high - low) >> 1)
    val = matrix[mid][0]
    if val > target:
      high = mid - 1
    else:
      if mid == len(matrix) - 1 or matrix[mid+1][0] > target:
        k = mid
        break
      else:
        low = mid + 1
  if k is None:
    return False
  # 对前 k 个数组分别进行二分查找
  for i in range(k+1):
    if bsFindIndex(matrix[i],target):
      return True
  return False

# h-index 定义：有 N 篇论文被引用至少 N 次以上，则 h-index 是 N
# 定义的解法：按引用次数从大到小排序，然后遍历寻找第一个 val < index + 1 的元素（因为 val 是递减的，index 是递增的）
# 变种问题，给定从小到大排序的 citations 数组，求 h-index
# 正常二分查找 val + index == n，如果最后找不到，则 n - (high + 1) 就是答案
def hIndex(citations=[]):
  n = len(citations)
  low = 0
  high = n-1
  while low <= high:
    mid = low + ((high - low) >> 1)
    if citations[mid] + mid == n:
      return n - mid
    elif citations[mid] + mid > n:
      high = mid - 1
    else:
      low = mid + 1
  return n - (high + 1)


import pandas as pd
import numpy as np
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.exceptions import UndefinedMetricWarning

import random as rd
import sys

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore", category=RuntimeWarning)
warnings.filterwarnings("ignore", category=UndefinedMetricWarning)

from config.config import *

from detection.detection import Detection
from detection.preprocessing.preprocessing import process_labels

MIN_SIZE = 1
MAX_SIZE = max(MIN_SIZE+1, 20)

np.set_printoptions(linewidth=150, precision=2)

def outlier_report(df_y, outlier_indices):
    """

    :param df_y:
    :param outlier_indices:

    :return:
    """
    detected_attack = 0
    detected_normal = 0
    false_negtive = 0
    false_positive = 0
    count = 0
    max_count = len(outlier_indices)

    for i in range(df_y.shape[0]):
        label_num = df_y[i]

        if count < max_count and i == outlier_indices[count]:
            if label_num != 0:
                detected_attack += 1
            else:
                false_positive += 1
            count += 1
        else:
            if label_num != 0:
                false_negtive += 1
            else:
                detected_normal += 1
    total = detected_attack + detected_normal + false_positive + false_negtive
    print("")
    print("=========================================")
    print("             OUTLIER ACCURACY            ")
    print("=========================================")
    print("          |   Det Attack       Det Normal  ")
    print("----------|--------------------------------")
    print("Is Attack |   ", detected_attack, "          ", false_negtive)
    print("Is Normal |   ", false_positive, "           ", detected_normal)
    print("")
    print("          |  Det Attack        Det Normal ")
    print("-----------------------------------------")
    print("Is Attack |  ", round(detected_attack / total * 100, 4), "         ", round(false_negtive / total * 100, 4))
    print("Is Normal |  ", round(false_positive / total * 100, 4), "          ",
          round(detected_normal / total * 100, 4))
    print("")
    if detected_attack > 0:
        precision = detected_attack/(detected_attack + false_positive)
        recall = detected_attack/(detected_attack + false_negtive)
        f1 = (2 * precision * recall) / (precision + recall)
    else:
        precision = 0
        recall = 0
        f1 = 0

    print("Precision: ", round(precision, 3))
    print("Recall: ", round(recall, 3))
    print("F1-Score: ", round(f1, 3))
    return [precision, recall, f1]

def classification_result(y, y_pred):
    """

    :param y:
    :param y_pred:

    :return:
    """
    assert len(y) == len(y_pred)
    correct = []
    wrong = []
    for i in range(len(y)):
        if y[i] == y_pred[i]:
            correct.append(i)
        else:
            wrong.append(i)
    return correct, wrong

def read_file(name):
    """

    :param name:

    :return:
    """
    df = pd.read_csv(name, na_values="Infinity", dtype=dtypes)
    df = df.replace("Infinity", sys.maxsize)
    df = df.fillna(sys.maxsize)

    df_X = df.iloc[:, 0:-1].copy()
    df_X = df_X.apply(pd.to_numeric)

    df_y = df.iloc[:, -1].copy()
    df_y = process_labels(df_y, labels, classes)

    #df_X = df_X.sample(frac=1)
    #df_y = df_y.iloc[df_X.index]

    return df_X, df_y

if __name__ == '__main__':

    data_directoy = "../TestData/"
    filemon = "normal.csv"
    filetue = "dos.csv"
    filewed = "portscan.csv"
    filethr1 = "ddos.csv"

    file_list = [filemon, filetue, filewed, filethr1]
    file_list = [data_directoy + x for x in file_list]

    print("Loading data...")
    df_X, df_y = read_file(file_list[0])
    new_features = df_X.columns

    det = Detection(classes=classes , threshold_percentile=threshold_percentile, truth_size=truth_size,
                    truth_update_frac=truth_update_frac, truth_save_folder=truth_save_folder,
                    outlier_save_folder=outlier_save_folder, classifier_save_folder=classifier_save_folder)

    print("Initializing detection system...")
    det.initialize(df_X, df_X, np.ndarray.flatten(df_y.values))
    print("Training Complete")

    name = ["DoS", "PortScan", "DDoS"]

    for i in range(1,len(file_list)):
        print(name[i-1] + " Test...")
        df_X, df_y = read_file(file_list[i])
        y_pred = []
        outlier_indices = []
        size = df_X.shape[0]
        c = 0
        index = df_X.index.tolist()
        for k in range(len(index)):
            j = index[k]
            if c % 50 == 0 and c > 0:
                print("\rStep " + str(c) +" of " + str(size), sep=' ', end='', flush=True)
                #print("Step " + str(c) +" of " + str(size))
                """
                y_now = df_y.iloc[index[:k]]
                cls_rep = classification_report(y_now.values, y_pred, labels=classes, output_dict=True)
                print("F1 Scores => ", end='')
                print("Normal: ", np.round(cls_rep['0']['f1-score'],2), end='')
                print(" | Dos: ", np.round(cls_rep['1']['f1-score'],2), end='')
                print(" | PortScan: ", np.round(cls_rep['2']['f1-score'],2), end='')
                print(" | DDoS: ", np.round(cls_rep['3']['f1-score'],2), end='')
                print(" | Weighted: ", np.round(cls_rep['weighted avg']['f1-score'],2))
                """
            c += 1

            X = df_X.iloc[[j]]
            y = df_y.iloc[[j]]
            outlier = det.detect_outliers(X)
            if outlier:
                outlier_indices.append(j)
                pred = det.classfiy(X)[0]
                if pred == 0:
                    det.update_outlier(X)
                det.update_classifier(X, np.ndarray.flatten(y.values))
                y_pred.append(pred)
            else:
                det.update_outlier(X)
                y_pred.append(0)
        print("")
        print(classification_report(df_y.values, y_pred, labels=classes))


from ellipticcurve.ecdsa import Ecdsa
from ellipticcurve.privateKey import PrivateKey

# Gerando as chaves
chave_privada = PrivateKey()
chave_publica = chave_privada.publicKey()

mensagem = "Segredo secreto :O"

# Gerando a assinatura
carloscabral = Ecdsa.sign(mensagem,chave_privada)

# Verificando
Resposta = Ecdsa.verify(mensagem,carloscabral,chave_publica)
print(Resposta)


import json
import cv2

labels = {
            'left': 0,
            'right': 1,
            'front': 2,
            'behind': 3
        }

data_root = '/home/asaran/research/spatial-relations-attention/data/CLEVR_v1.0/'
with open(data_root+'scenes/CLEVR_train_scenes.json') as datafile:
    train_data = json.load(datafile)

with open(data_root+'scenes/CLEVR_val_scenes.json') as datafile:
    test_data = json.load(datafile)

train_data_list = []
test_data_list = []

for img in train_data['scenes']:
    objects = img['objects']
    #print('image_index: '+str(img['image_index']))
    #print('len(objects):' + str(len(objects)))
    #im = cv2.imread(data_root+'images/'+img['image_filename'])
    #width, height =
    for relation in img['relationships']:
        for subj in range(len(img['relationships'][relation])):
            for obj in img['relationships'][relation][subj]:
                #print('obj: '+str(obj))
                #print('subj: '+str(subj))
                data_dict = {}
                data_dict['img_path'] = data_root+'images/train/'+img['image_filename']
                obj_pos = objects[obj]['pixel_coords']
                subj_pos = objects[subj]['pixel_coords']

                pos = obj_pos + subj_pos
                data_dict['position'] = pos
                data_dict['label'] = labels[relation]

                train_data_list.append(data_dict)

train_outfile = '/media/internal/asaran/CLEVR/train_relations.json'
with open(train_outfile, 'w') as outfile:
    json.dump(train_data_list, outfile)

for img in test_data['scenes']:
    objects = img['objects']
    #im = cv2.imread(data_root+'images/'+img['image_filename'])
    #width, height =
    for relation in img['relationships']:
        for subj in range(len(img['relationships'][relation])):
            for obj in img['relationships'][relation][subj]:
                data_dict = {}
                data_dict['img_path'] = data_root+'images/val/'+img['image_filename']
                obj_pos = objects[obj]['pixel_coords']
                subj_pos = objects[subj]['pixel_coords']

                pos = obj_pos + subj_pos
                data_dict['position'] = pos
                data_dict['label'] = labels[relation]

                test_data_list.append(data_dict)

test_outfile = '/media/internal/asaran/CLEVR/test_relations.json'
with open(test_outfile, 'w') as outfile:
    json.dump(test_data_list, outfile)


# -*- coding: utf-8 -*-
#!/usr/bin/env python2
"""
API통합 모듈
해당 모듈을 통해 API를 사용
"""

from slackclient import SlackClient
from config import CrawlerConfig

class SlackAPI:
	"""
	Description
		네이버 메인뉴스 크롤링 데이터를 가공하는 클래스

	Func
		__init__ : 생성자. 초기 셋팅 작업
		process_item : 전체 프로세스를 담당하는 함수
		send_news_to_slack : slack에 뉴스를 발송하는 함수

	Attr
		None
	"""

	def __init__(self):
		self.sc = SlackClient(CrawlerConfig().bot_token)

	def call_send_api(self, msg):
		"""
		Description
			API를 호출하는 함수
			만약 다른 채널 혹은 사용자에게 메세지를 보내기 위해서는

		Args
			msg : 발송하려는 메세지가 담겨있는 변수
		"""

		self.sc.api_call(
		"chat.postMessage", channel="#every_news_playwings", text=msg,
		username='every_news', icon_emoji=':robot_face:'
		)


import discord
from discord.ext import commands
from bs4 import BeautifulSoup
from requests.utils import quote
import aiohttp

class CardSkillMixedException(Exception):
        def __init__(self, message):
            super().__init__(message)

class HTMLParseException(Exception):
        def __init__(self, message):
            super().__init__(message)

class DuelLinksLinkNotFoundException(Exception):
        def __init__(self, message):
            super().__init__(message)

class DuelLinksCog:

    def __init__(self, bot):
        self.bot = bot

    @commands.command(pass_context=True, aliases=["s"])
    async def skill(self, ctx, *skill_names):
        """Show a Yu-Gi-Oh! Duel Links Skill.
        Surround your card name(s) with apostrophes (') for exact matches.
        Use a comma-separated list for multiple skills."""

        joined_skill_names = ' '.join(skill_names)
        if not any([letter.isalpha() for letter in joined_skill_names]):
            await self.bot.send_cmd_help(ctx)
            return

        skill_list = joined_skill_names.split(",")
        for skill_name in skill_list:
            if not skill_name.isspace():
                try:
                    skill_link = await self._get_duel_links_link(skill_name.strip())
                except DuelLinksLinkNotFoundException as e:
                    await self.bot.say(e)
                    return
                try:
                    skill_details = await self._get_skill_details(skill_link)
                except CardSkillMixedException as e:
                    await self.bot.say(e)
                    return
                except HTMLParseException as e:
                    await self.bot.say("%s\n%s" % (e, card_link))
                    return
                skill_title = "**`%s`**" % skill_details["Name"]
                skill_description = "```\n%s```" % skill_details["Description"]
                await self.bot.say("%s\n%s\n%s" % (skill_title, skill_description, skill_link))

    @commands.command(pass_context=True, aliases=["c"])
    async def card(self, ctx, *card_names):
        """Show a Yu-Gi-Oh! card.
        Surround your card name(s) with apostrophes (') for exact matches.
        Use a comma-separated list for multiple cards."""

        joined_card_names = ' '.join(card_names)
        if not any([letter.isalpha() for letter in joined_card_names]):
            await self.bot.send_cmd_help(ctx)
            return

        card_list = joined_card_names.split(",")
        for card_name in card_list:
            if not card_name.isspace():
                try:
                    card_link = await self._get_duel_links_link(card_name.strip())
                except DuelLinksLinkNotFoundException as e:
                    await self.bot.say(e)
                    return
                try:
                    card_details = await self._get_card_details(card_link)
                except CardSkillMixedException as e:
                    await self.bot.say(e)
                    return
                except HTMLParseException as e:
                    await self.bot.say("%s\n%s" % (e, card_link))
                    return
                bot_message = []

                title_message = "**`%s" % card_details["Name"]
                if "Level" in card_details:
                    title_message += " %s" % card_details["Level"]
                if "Rarity" in card_details:
                    title_message += " %s" % card_details["Rarity"]
                title_message += "`**"
                bot_message.append(title_message)

                if "Attribute" in card_details and "Monster Type" in card_details:
                    bot_message.append("**`Attribute: `**`%s` **`Type: `**`%s`" % (card_details["Attribute"], card_details["Monster Type"]))

                bot_message.append("```\n%s```" % card_details["Description"])

                if "ATK" in card_details and "DEF" in card_details:
                    bot_message.append("**`ATK: `**`%s` **`DEF: `**`%s`" % (card_details["ATK"], card_details["DEF"]))

                bot_message.append(card_link)

                await self.bot.say("\n".join(bot_message))

    async def _get_duel_links_link(self, query):
        query_is_quoted = query.startswith("'") and query.endswith("'")
        google_search = "http://www.google.com/search?q=" + quote("duel links %s" % query)
        browser_headers = {
            "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"
        }
        async with aiohttp.get(google_search, headers=browser_headers) as response:
            google_search_soup = BeautifulSoup(await response.text(), "html.parser")

        h3_text = list(map(lambda h3: h3.text, google_search_soup.find_all("h3")))
        cite_text = list(map(lambda cite: cite.text, google_search_soup.find_all("cite")))
        search_results = zip(h3_text, cite_text)
        for search_results in search_results:
            if len(search_results) is not 2:
                continue
            if not search_results[1].startswith("duellinks.gamea.co"):
                continue
            if query_is_quoted:
                search_result_card_name = search_results[0].split("|")[0].strip()
                if query.strip("'").lower() == search_result_card_name.lower():
                    return "http://%s" % search_results[1]
                continue
            return "http://%s" % search_results[1]
        raise DuelLinksLinkNotFoundException("Could not find the Duel Links link.")

    async def _get_card_details(self, card_link):
        async with aiohttp.get(card_link) as response:
            duel_links_soup = BeautifulSoup(await response.text(), "html.parser")

        card_details = {}
        card_details["Name"] = duel_links_soup.find("title").text.split("|")[0].strip()
        card_details["Description"] = duel_links_soup.find("p").text

        if "(skill)" in card_details["Name"]:
            raise CardSkillMixedException("%s is a skill - not a card." % card_details["Name"][:-8])

        # get the rarity
        rarity_levels = ["[N]", "[R]", "[SR]", "[UR]"]
        found_rarity = False
        article_parts = duel_links_soup.findAll("div", {"class": "article-part"})
        for i in range(0, len(article_parts)):
            # need to search case-insensitive for the how to get/rarity section
            rarity_location_title = article_parts[i].text.lower()
            if "rarity" in rarity_location_title or "how to get" in rarity_location_title:
                rarity_location = article_parts[i + 1]
                for rarity_table_td in rarity_location.find_all("td"):
                    rarity_table_cell_text = rarity_table_td.get_text()
                    for rarity_level in rarity_levels:
                        # search everything inside the <td> via get_text()
                        if rarity_level in rarity_table_cell_text:
                            card_details["Rarity"] = rarity_level
                            found_rarity = True
                            break
            if found_rarity:
                break

        # get other card details
        all_tr = duel_links_soup.find_all("tr")
        for tr in all_tr:
            all_td = tr.find_all("td")
            if not all_td:
                continue
            th = tr.find_all("th")
            key_td = th[0] if th else all_td[0]
            value_td = all_td[-1]
            if key_td.text in ["Rating", "Pack"]:
                return card_details
            elif key_td.text in ["Passcode"]:
                continue
            card_details[key_td.text] = value_td.text
        raise HTMLParseException("The HTML retrieved from the Duel Links site was fucked.")

    async def _get_skill_details(self, skill_link):
        async with aiohttp.get(skill_link) as response:
            duel_links_soup = BeautifulSoup(await response.text(), "html.parser")
        skill_details = {}
        skill_details["Name"] = duel_links_soup.find("title").text.split("|")[0].strip()

        if "(skill)" not in skill_details["Name"]:
            raise CardSkillMixedException("%s is a card - not a skill." % skill_details["Name"])

        article_parts = duel_links_soup.findAll("div", {"class": "article-part"})
        for i in range(0, len(article_parts)):
            if article_parts[i].text.strip() == "Description":
                skill_details["Description"] = article_parts[i + 1].text.strip()
                break
        return skill_details

def setup(bot):
    bot.add_cog(DuelLinksCog(bot))


import pymongo
import json
import jieba
import jieba.analyse

from bosonnlp import BosonNLP
from bloom_filter import BloomFilter
from pyquery import PyQuery as pq
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By

'''
WeiboSpider.py:
                爬取微博健康相关的数据并保存在mongodb。用于DataAnalyzer来分析数据
'''

# 配置布隆过滤器，用于网址链接去重
filter = BloomFilter(filename='weibo_bloom', max_elements=100000, error_rate=0.001)

# 启动浏览器chrome模拟访问weibo.com, 设置selenium参数
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 10)

# 连接MongoDB, 配置MongoDB参数
MONGO_TBL = 'healthy_info'
client = pymongo.MongoClient('localhost')
db = client['weibo']

# BosonNLP
nlp = BosonNLP('vtWvmE_M.33585.9k7qpYIWc6Aq')

# JiebaNLP设置参数
TOP_K = 8             # 抽取关键词的词数

# 要爬取的xhr对象页数，即要爬取多少页微博内容（首页+xhr页数)
XHR_PAGES = 30

class WeiboSpider:
    '''
    微博爬虫
    爬取过程分为两步：
    1. 爬取微博第一页内容，即分析html内容并提取正文内容，对应函数是__parse_url
    2. 爬取微博后面的异步内容，即分析xhr内容并提取正文内容，对应函数是__parse_xhr
    '''

    def __init__(self):
        self.run()

    def __parse_url(self, url):
        '''
        解析url内容，（html格式)并提取出里面正文内容并保存入数据库
        :param url: html对象的url
        :return: None
        '''
        try:
            browser.get(url)
        except TimeoutException:
            return None
        wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.WB_text.W_f14')))
        html = browser.page_source
        page = pq(html)
        items = page('.WB_text.W_f14').items()
        for item in items:
            content = item.text().replace('\n', '').replace('#', '').replace('【', '').replace('】', '')
            content = nlp.summary('', content)
            is_success = self.__save_to_mongo(content)
            if (is_success is True):
                filter.add(url)

    def __parse_xhr(self, url):
        '''
        解析xhr内容，（json格式)并提取出里面正文内容并保存入数据库
        :param url: xhr对象的url
        :return: None
        '''
        try:
            browser.get(url)
        except TimeoutException:
            return None
        # 等待标签 <pre>
        wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'pre')))
        # 分析网页内容
        html = browser.page_source
        doc = pq(html)
        content = doc.text()
        # json.load(content)将content从字符创变为字典
        # 接下来用get('data')将字典中的key为'data'的内容提取出来并保存在data中
        # data中保存的是字符串，保存的xhr的json内容，用于ajax加载增量的数据
        data = json.loads(content).get('data')
        # 将data用pyquery解析
        data_doc = pq(data)
        # 提取出微博该url的正文内容
        items = data_doc('.WB_text.W_f14').items()
        # 转为字典并保存到mongo中，添加到布隆过滤器中，用于以后的去重
        for item in items:
            # 首先先替换掉\n # 【 】
            content2 = item.text().replace('\n', '').replace('#', '').replace('【', '').replace('】', '')
            content3 = nlp.summary('', content2)  # 提取摘要
            # 如果是字母或数字则pass
            if (content3[0] >= '0' and content3[0] <= '9') or (content3[0] >= 'a' and content3[0] <= 'z'):
                continue
            is_success = self.__save_to_mongo(content3)
            if (is_success is True):
                filter.add(url)

    def __get_keywords(self, content):
        '''
        分析content并提取关键词存储在keywords并返回
        :param content: 文本内容
        :return: 一个存储关键词的列表 keywords_ls
        '''
        keywords_ls = jieba.analyse.extract_tags(content, topK=TOP_K, withWeight=False)    # 抽取关键词
        return keywords_ls

    def __save_to_mongo(self, content):
        '''
        将result结果存储到mongodb中
        :param result: 要存储入mongodb的数据
        :return: None
        '''
        if content is None:
            return False
        keywords = self.__get_keywords(content)
        result = {
            'content': content,             # 文章精简内容
            'keywords': keywords            # 关键词序列
        }
        try:
            if db[MONGO_TBL].insert_one(result):
                print("save to db success", result)
                return True
        except Exception:
            return False

    def run(self):
        '''
        爬虫开始运行
        :return: None
        '''
        # urls: 要爬取网址首页内容
        urls = [
                'https://weibo.com/shengmingshibao',            # 生命时报
                'https://weibo.com/dxydoctor',                  # 丁香医生
                'https://weibo.com/sinahealth',                 # 新浪健康
                'https://www.weibo.com/jiankangzhongguo',       # 健康中国
                'https://www.weibo.com/jiankangshibaoblog',     # 健康时报
                'https://weibo.com/cnhealthcare',               # 健康界
                'https://weibo.com/u/6387189723',               # 中国健康科普主场
                'https://weibo.com/u/6559157240',               # 健康类主流媒体
                'https://weibo.com/xiehehospital',              # 北京协和医院
                'https://weibo.com/wyjiankang',                 # 网易健康

        ]
        for url in urls:
            if url not in filter:
                self.__parse_url(url)

        ''' 要爬取网址xhr对象们 '''
        # xhr对象链接的公共部分
        part1 = 'https://weibo.com/p/aj/v6/mblog/mbloglist?ajwvr=6&domain=100206&is_hot=1&pagebar='
        # xhr对象链接不相同的部分
        xhr_part2 = [
            # 生命时报
            '&pl_name=Pl_Official_MyProfileFeed__28&id=1002061774057271&script_uri=/shengmingshibao&feed_type=0&page=1&pre_page=1&domain_op=100206&__rnd=1552821214799',
            # 丁香医生
            '&pl_name=Pl_Official_MyProfileFeed__25&id=1006062212518065&script_uri=/dxydoctor&feed_type=0&page=1&pre_page=1&domain_op=100606&__rnd=1552821290923',
            # 新浪健康
            '&pl_name=Pl_Official_MyProfileFeed__28&id=1002061268642527&script_uri=/sinahealth&feed_type=0&page=1&pre_page=1&domain_op=100206&__rnd=1552821037728',
            # 健康中国
            '&pl_name=Pl_Official_MyProfileFeed__25&id=1001062834480301&script_uri=/jiankangzhongguo&feed_type=0&page=1&pre_page=1&domain_op=100106&__rnd=1553266384513',
            # 健康时报
            '&pl_name=Pl_Official_MyProfileFeed__28&id=1002061411163204&script_uri=/jiankangshibaoblog&feed_type=0&page=1&pre_page=1&domain_op=100206&__rnd=1553266519100',
            # 健康界
            '&pl_name=Pl_Official_MyProfileFeed__25&id=1006062651153623&script_uri=/cnhealthcare&feed_type=0&page=1&pre_page=1&domain_op=100606&__rnd=1553269434318',
            # 中国健康科普主场
            '&pl_name=Pl_Official_MyProfileFeed__25&id=1006066387189723&script_uri=/u/6387189723&feed_type=0&page=1&pre_page=1&domain_op=100606&__rnd=1553269648205',
            # 健康类主流媒体
            '&pl_name=Pl_Official_MyProfileFeed__28&id=1002066559157240&script_uri=/u/6559157240&feed_type=0&page=1&pre_page=1&domain_op=100206&__rnd=1553269753856',
            # 北京协和医院
            '&pl_name=Pl_Official_MyProfileFeed__25&id=1001061654801402&script_uri=/xiehehospital&feed_type=0&page=1&pre_page=1&domain_op=100106&__rnd=1553270327353',
            # 网易健康
            'https://weibo.com/p/aj/v6/mblog/mbloglist?ajwvr=6&domain=100206&is_hot=1&pagebar=0&pl_name=Pl_Official_MyProfileFeed__28&id=1002065341241739&script_uri=/wyjiankang&feed_type=0&page=1&pre_page=1&domain_op=100206&__rnd=1553270598357',
        ]

        xhr_part2_len = len(xhr_part2)
        for i in range(0, XHR_PAGES):
            for j in range(0, xhr_part2_len):
                url = part1+str(i)+xhr_part2[j]
                if url not in filter:
                    self.__parse_xhr(url)

if __name__ == '__main__':
    weibo = WeiboSpider()


#!/usr/bin/env python

import sys
import argparse
from github import Github

def parse_args(args):
        parser = argparse.ArgumentParser()
        parser.add_argument("--repo_list",
                            action="store")
        parser.add_argument("--pull_id_list",
                            action="store")
        parser.add_argument("--repo",
                            action="store")
        parser.add_argument("--pull_id",
                            action="store")
        parser.add_argument("--jenkins_url",
                            action="store")
        parser.add_argument("--status",
                            action="store")
        parser.add_argument("--description",
                            action="store")
        parser.add_argument("--home",
                            action="store")
        args = parser.parse_args(args)
        return args

args = parse_args(sys.argv[1:])
repo_list=[]
pull_id_list=[]
if args.pull_id_list != "none":
    tmp_repo_list = args.repo_list.split(',')
    for n, pull_id in enumerate(args.pull_id_list.split(',')):
        if pull_id.isalnum():
            repo_list.append(tmp_repo_list[n])
            pull_id_list.append(long(pull_id))
    args.description += "[dependency]"
elif args.repo != "none":
    repo = args.repo
    repo_list.append(repo)
    pull_id_list.append(long(args.pull_id))

with open('{0}/.ghtoken'.format(args.home), 'r') as file:
    TOKEN = file.read().strip('\n')
gh = Github(TOKEN)

for n, repo in enumerate(repo_list):
    pr = gh.get_repo(repo).get_pull(long(pull_id_list[n]))
    COMMIT = pr.get_commits().reversed[0]
    COMMIT.create_status(args.status, \
                        description=args.description.replace(',',' '), \
                        context='Jenkins')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-+

"""  """
import keras.backend as K

""" tensorflow """
import tensorflow as tf

"""  """
import numpy as np

""" lib """
from .actor_network import ActorNetwork
from .critic_network import CriticNetwork
from .replay_buffer import ReplayBuffer

import math

MAX_EPISODES = 1000
MAX_EP_STEPS = 200
GAMMA = 0.99

class DDPG(object):
    def __init__(self,env,state_size,action_size,action_bound):
        self.state_size = state_size
        self.action_size = action_size
        self.action_bound = action_bound
        self.env = env

        """ Tensorflow GPU optimization """
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=config)
        K.set_session(self.sess)

        """ init """
        self.Init_Params()

        """ network """
        self.actor = ActorNetwork(self.sess,state_size,action_size,self.batch_size,self.TAU,self.lr_a)
        self.critic = CriticNetwork(self.sess,state_size,action_size,self.batch_size,self.TAU,self.lr_c)

        self.actor.Load_Model()
        self.critic.Load_Model()
        """ replayBuffer """
        self.replay_buffer = ReplayBuffer(self.buffer_size,1337)

    def Init_Params(self):
        """ Target Network HyperParameters """
        self.TAU = 0.001

        """ learning rate """
        self.lr_a = 0.0001
        self.lr_c = 0.001

        """ seed """
        np.random.seed(1337)

        """ buffer size """
        self.buffer_size = 100000

        """ batch size """
        self.batch_size = 32

    def Sim_Train_HER(self,train = 0):
        for i in range(MAX_EPISODES):
            state = self.env.reset(True)
            state = np.reshape(state,(1,self.state_size))

            ep_reward = 0
            ep_ave_max_q = 0
            test = 2.0
            for j in range(MAX_EP_STEPS):
            # for j in range(self.env.spec.timestep_limit):
                loss = 0
                self.env.render()

                action = self.actor.Evaluate_Actor(state)
                action_ = np.clip(np.random.normal(action,1), *self.env.action_bound)
                # print(action)
                # if(action[0][0] >= action[0][1]):
                #     action_ = 0
                # else:
                #     action_ = 1

                # tmp = abs(action[0])
                # action_ = np.argmax(tmp)
                # action_ = 0

                # new_state,reward,done,info = self.env.step(action)
                new_state,reward,done,info = self.env.step(action_)

                self.replay_buffer.Add_Buffer(np.reshape(state,(self.state_size,)),\
                                              np.reshape(action,(self.action_size,)),\
                                              reward,\
                                              np.reshape(new_state,(self.state_size,)),\
                                              done)

                """ update batch """
                s_batch,a_batch,r_batch,s2_batch,d_batch,y_t = self.replay_buffer.Get_Batch(self.batch_size)

                target_q_value = self.critic.Evaluate_Target_Actor([s2_batch,self.actor.Evaluate_Target_Actor(s2_batch)])

                for k in range(len(r_batch)):
                    if(d_batch[k]):
                        y_t[k] = r_batch[k]
                    else:
                        y_t[k] = r_batch[k] + GAMMA*target_q_value[k]
                if(train):
                    # print(y_t)
                    loss += self.critic.model.train_on_batch([s_batch,a_batch], y_t)
                    a_for_grad = self.actor.Evaluate_Actor(s_batch)
                    grads = self.critic.Gradient(s_batch,a_for_grad)
                    self.actor.Train(s_batch,grads)
                    self.actor.Target_Train()
                    self.critic.Target_Train()

                ep_reward += reward
                state = np.reshape(new_state,(1,self.state_size))
                # print("Episode", i, "Step", j, "Actions", action, "Reward", reward,'loss',loss)
                print("Episode", i, "Step", j,"Actions", action, 'action',action_,"Reward", reward,'loss',loss)

                if(done):
                    break
            print("TOTAL REWARD @ " + str(i) +"-th Episode  : Reward " + str(ep_reward))
            print("Total Step: " + str(j))
            print("")
        if(train):
            print("save model")
            self.actor.Save_Model()
            self.actor.Save_Weight_JSON()
            self.critic.Save_Model()
            self.critic.Save_Weight_JSON()
        print("Finish")