#! /usr/bin/python

import sys
import os
import time

import math
import numpy as np
import matplotlib.pyplot as plt

sys.path.append("/home/oks/catkin_ws/src/framework_sim/clrn_ptracker/scripts/")
import controller
import configurator
sys.path.append("/home/oks/catkin_ws/src/framework_sim/clrn_analyzer/scripts/")
import live_plotter

import rospy
from geometry_msgs.msg import Twist
from nav_msgs.msg import Odometry
from std_msgs.msg import Float64
from std_msgs.msg import Float64MultiArray

def init_globals():
    global x_history_g, y_history_g, yaw_history_g, speed_history_g, time_history_g, seq_history_g, seq_pre_g, reached_the_end_g
    reached_the_end_g = False
    seq_pre_g = 0
    x_history_g = []
    y_history_g = []
    yaw_history_g = []
    speed_history_g = []
    time_history_g = []
    seq_history_g = []

def init_nav():
    init_globals()
    config = configurator.get_config()
    path = parse_path(config)
    pub_obj = init_ros(config)
    try:
        os.remove("/home/oks/catkin_ws/src/framework_sim/gen_txtfiles/controller_output.txt")
        os.remove("/home/oks/catkin_ws/src/framework_sim/gen_txtfiles/cont_fb_out.txt")
    except:
        pass
    return config, path, pub_obj

def init_ros(config):
    try:
        rospy.init_node(config.trkr_out_node)
        pub_obj = rospy.Publisher(config.trkr_out_topic, Float64MultiArray, queue_size=10)
        return pub_obj
    except rospy.ROSInterruptException:
        print('ROS Connection Lost!')
        return

def parse_path(config):
    course_txt = ["wpx\n", "wpy\n", "fg\n", "cx\n", "cy\n", "cyaw\n", "ck\n", "cs\n", "tv\n", "sp\n"]
    waypoints_x = []
    waypoints_y = []
    final_goal = []
    course_x = []
    course_y = []
    course_yaw = []
    course_k = []
    course_s = []
    target_velocity = []
    speed_profile = []
    try:
        course_file = open(config.gen_files_path, 'r')
        lines = course_file.readlines()
        for line in range(len(lines) - 1):
            if lines[line] == course_txt[0]:
                for i in lines[line + 1].split():
                    waypoints_x.append(float(i))
            elif lines[line] == course_txt[1]:
                for i in lines[line + 1].split():
                    waypoints_y.append(float(i))
            elif lines[line] == course_txt[2]:
                for i in lines[line + 1].split():
                    final_goal.append(float(i))
            elif lines[line] == course_txt[3]:
                for i in lines[line + 1].split():
                    course_x.append(float(i))
            elif lines[line] == course_txt[4]:
                for i in lines[line + 1].split():
                    course_y.append(float(i))
            elif lines[line] == course_txt[5]:
                for i in lines[line + 1].split():
                    course_yaw.append(float(i))
            elif lines[line] == course_txt[6]:
                for i in lines[line + 1].split():
                    course_k.append(float(i))
            elif lines[line] == course_txt[7]:
                for i in lines[line + 1].split():
                    course_s.append(float(i))
            elif lines[line] == course_txt[8]:
                for i in lines[line + 1].split():
                    target_velocity.append(float(i))
            elif lines[line] == course_txt[9]:
                for i in lines[line + 1].split():
                    speed_profile.append(float(i))
    except:
        print("No File!")
    return [waypoints_x, waypoints_y, final_goal, course_x, course_y, course_yaw, course_k, course_s, target_velocity, speed_profile]

def quaternion_to_euler(qx, qy, qz, qw):
    t0 = +2.0 * (qw * qx + qy * qz)
    t1 = +1.0 - 2.0 * (qx * qx + qy * qy)
    roll = math.atan2(t0, t1)
    t2 = +2.0 * (qw * qy - qz * qx)
    t2 = +1.0 if t2 > +1.0 else t2
    t2 = -1.0 if t2 < -1.0 else t2
    pitch = math.asin(t2)
    t3 = +2.0 * (qw * qz + qx * qy)
    t4 = +1.0 - 2.0 * (qy * qy + qz * qz)
    yaw = math.atan2(t3, t4)
    return yaw, pitch, roll

def send_init_cmd(config, pub):
    cmd_list_msg = config.cmd_list_msg_t
    cmd_throttle = 0.0
    cmd_brake = 0.0
    cmd_steer = 0.0
    cmd_list_msg.data = [cmd_throttle, cmd_brake, cmd_steer]
    pub.publish(cmd_list_msg)

def convert_cmd_to_twist(cmd_throttle, cmd_steer, cmd_brake):
    global config_g
    twist_cmd = config_g.cmd_vel_msg_t
    if cmd_throttle > 0.0 and cmd_throttle <= 1.0:
        twist_cmd.linear.x = 1.0
    elif cmd_throttle == 0.0:
        twist_cmd.linear.x = 0.0
    twist_cmd.linear.x = cmd_throttle * 1.5
    if cmd_steer > 0.0:
        twist_cmd.angular.z = 1.0
    elif cmd_steer < 0.0:
        twist_cmd.angular.z = -1.0
    elif cmd_steer == 0.0:
        twist_cmd.angular.z = 0.0
    twist_cmd.linear.x = cmd_throttle
    twist_cmd.angular.z = cmd_steer / 1.22
    return twist_cmd

def convert_cmd_to_pub(cmd_throttle, cmd_steer, cmd_brake):
    global config_g
    cmd_list_msg = config_g.cmd_list_msg_t
    cmd_list_msg.data = [cmd_throttle, cmd_brake, cmd_steer]
    return cmd_list_msg

def logger(commands, feedback):
    commands = [str(commands[0]), str(commands[1]), str(commands[2])]
    feedback = [str(feedback[0]), str(feedback[1])]
    cmd_log = open("/home/oks/catkin_ws/src/framework_sim/gen_txtfiles/controller_output.txt", "a")
    fb_log = open("/home/oks/catkin_ws/src/framework_sim/gen_txtfiles/cont_fb_out.txt", "a")
    for i in range(len(commands)):
        cmd_log.write(commands[i])
        cmd_log.write("\n")
    cmd_log.write("-------------------------\n")
    cmd_log.close()
    for i in range(len(feedback)):
        fb_log.write(feedback[i])
        fb_log.write("\n")
    fb_log.write("-------------------------\n")
    fb_log.close()

def feedback_callback(odom_data):
    global cmd_pub_g, config_g, waypoints_g, x_history_g, y_history_g, yaw_history_g, speed_history_g, time_history_g, seq_history_g, seq_pre_g, controller_g, cur_time_g, pre_time_g, closest_index_g, fg_g, reached_the_end_g, wp_x_g, wp_y_g
    reached_the_end = reached_the_end_g
    seq_cur = odom_data.header.seq
    x_history = x_history_g
    y_history = y_history_g
    yaw_history = yaw_history_g
    time_history = time_history_g
    speed_history = speed_history_g
    seq_history = seq_history_g
    controller_l = controller_g
    waypoints = waypoints_g[0]
    waypoints_np = waypoints_g[1]
    wp_distance = waypoints_g[2]
    wp_interp = waypoints_g[3]
    wp_interp_hash = waypoints_g[4]
    odom_x = odom_data.pose.pose.position.x
    odom_y = odom_data.pose.pose.position.y
    odom_qx = odom_data.pose.pose.orientation.x
    odom_qy = odom_data.pose.pose.orientation.y
    odom_qz = odom_data.pose.pose.orientation.z
    odom_qw = odom_data.pose.pose.orientation.w
    odom_vx = odom_data.twist.twist.linear.x
    odom_vy = odom_data.twist.twist.linear.y
    odom_rz = odom_data.twist.twist.angular.z
    odom_yaw, _, _ = quaternion_to_euler(odom_qx, odom_qy, odom_qz, odom_qw)
    current_x = odom_x
    current_y = odom_y
    current_yaw = odom_yaw
    current_speed = np.sqrt(odom_vx**2 + odom_vy**2)
    x_history.append(current_x)
    y_history.append(current_y)
    yaw_history.append(current_yaw)
    speed_history.append(current_speed)
    seq_history.append(seq_cur)
    if seq_pre_g == 0:
        time_history.append(0.1)
        pre_time_g = 0.0
        cur_time_g = 0.1
    else:
        time_history.append(time_history[-1] + 0.1)
        cur_time_g = pre_time_g + 0.1
    previous_timestamp = pre_time_g
    current_timestamp = cur_time_g
    if seq_pre_g == 0:
        closest_index_g = 0
    closest_index = closest_index_g
    closest_distance = np.linalg.norm(np.array([
            waypoints_np[closest_index, 0] - current_x,
            waypoints_np[closest_index, 1] - current_y]))
    new_distance = closest_distance
    new_index = closest_index
    while new_distance <= closest_distance:
        closest_distance = new_distance
        closest_index = new_index
        new_index += 1
        if new_index >= waypoints_np.shape[0]:
            break
        new_distance = np.linalg.norm(np.array([
                waypoints_np[new_index, 0] - current_x,
                waypoints_np[new_index, 1] - current_y]))
    new_distance = closest_distance
    new_index = closest_index
    while new_distance <= closest_distance:
        closest_distance = new_distance
        closest_index = new_index
        new_index -= 1
        if new_index < 0:
            break
        new_distance = np.linalg.norm(np.array([
                waypoints_np[new_index, 0] - current_x,
                waypoints_np[new_index, 1] - current_y]))
    waypoint_subset_first_index = closest_index - 1
    if waypoint_subset_first_index < 0:
        waypoint_subset_first_index = 0
    waypoint_subset_last_index = closest_index
    total_distance_ahead = 0
    while total_distance_ahead < 20:
        total_distance_ahead += wp_distance[waypoint_subset_last_index]
        waypoint_subset_last_index += 1
        if waypoint_subset_last_index >= waypoints_np.shape[0]:
            waypoint_subset_last_index = waypoints_np.shape[0] - 1
            break
    new_waypoints = \
            wp_interp[wp_interp_hash[waypoint_subset_first_index]:\
                      wp_interp_hash[waypoint_subset_last_index] + 1]
    controller_l.update_waypoints(new_waypoints)
    controller_l.update_values(current_x, current_y, current_yaw,
                             current_speed,
                             current_timestamp, 1)
    controller_l.update_controls()
    cmd_throttle, cmd_steer, cmd_brake = controller_l.get_commands()
    dist_to_last_waypoint = np.linalg.norm(np.array([
        waypoints[-1][0] - current_x,
        waypoints[-1][1] - current_y]))
    if  dist_to_last_waypoint < 10.0:
        reached_the_end = True
    if reached_the_end:
        print("Reaching the end of path.")
        cmd_throttle = 0.0
        cmd_brake = 1.0
    # msg_to_pub = convert_cmd_to_twist(cmd_throttle, cmd_steer, cmd_brake)
    msg_to_pub = convert_cmd_to_pub(cmd_throttle, cmd_steer, cmd_brake)
    cmd_pub_g.publish(msg_to_pub)
    commands = [cmd_throttle, cmd_steer, cmd_brake]
    feedback = [odom_x, odom_y]
    live_plotter.live_plot(wp_x_g, wp_y_g, odom_x, odom_y, closest_index)
    logger(commands, feedback)
    previous_timestamp = current_timestamp
    seq_pre_g = seq_cur
    # print "Seq INFO: ", seq_pre_g
    # print "Throttle CMD: ", cmd_throttle
    # print "Steer CMD: ", cmd_steer
    # print "Brake CMD: ", cmd_brake
    # print "X: ", current_x
    # print "Y: ", current_y
    # print "YAW: ", current_yaw
    # print "Speed: ", current_speed
    # print "Goal: ", fg_g[0], ", ", fg_g[1]
    # print "--------------------------"

def exec_nav(config, path, pub):
    global cmd_pub_g, waypoints_g, config_g, controller_g, fg_g, wp_x_g, wp_y_g
    cmd_pub_g = pub
    config_g = config
    wp_x = path[0]
    wp_y = path[1]
    fg = path[2]
    cx = path[3]
    cy = path[4]
    cyaw = path[5]
    ck = path[6]
    cs = path[7]
    tv = path[8]
    sp = path[9]
    v = []
    fg_g = fg
    for i in range(len(wp_x)):
        v.append(10.0)
    waypoints = list(zip(wp_x, wp_y, sp))
    waypoints_np = np.array(waypoints)
    wp_distance = []
    for i in range(1, waypoints_np.shape[0]):
        wp_distance.append(
                np.sqrt((waypoints_np[i, 0] - waypoints_np[i-1, 0])**2 +
                        (waypoints_np[i, 1] - waypoints_np[i-1, 1])**2))
    wp_distance.append(0)
    wp_interp      = []
    wp_interp_hash = []
    interp_counter = 0
    for i in range(waypoints_np.shape[0] - 1):
        wp_interp.append(list(waypoints_np[i]))
        wp_interp_hash.append(interp_counter)
        interp_counter+=1
        num_pts_to_interp = int(np.floor(wp_distance[i] /\
                                     float(0.01)) - 1)
        wp_vector = waypoints_np[i+1] - waypoints_np[i]
        wp_uvector = wp_vector / np.linalg.norm(wp_vector)
        for j in range(num_pts_to_interp):
            next_wp_vector = 0.01 * float(j+1) * wp_uvector
            wp_interp.append(list(waypoints_np[i] + next_wp_vector))
            interp_counter+=1
    wp_interp.append(list(waypoints_np[-1]))
    wp_interp_hash.append(interp_counter)
    interp_counter+=1
    num_iterations = 10
    if (10 < 1):
        num_iterations = 1
    waypoints_g = [waypoints, waypoints_np, wp_distance, wp_interp, wp_interp_hash]
    controller_l = controller.Controller(waypoints)
    controller_g = controller_l
    wp_x_g = wp_x
    wp_y_g = wp_y
    rospy.Subscriber(config.feedback_out_topic, Odometry, feedback_callback)
    send_init_cmd(config, pub)
    rospy.spin()

def main():
    while True:
        print('Get Ready for the Fuckin\' Ride')
        config, path, pub = init_nav()
        exec_nav(config, path, pub)
        print('Done.')
        return

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print('\nYou chose to leave. Bye!')


"""
Post processing on detected objects
"""
import pymongo
from pymongo import MongoClient
import time
import logging
logging.basicConfig(format='%(levelname)s :: %(asctime)s :: %(message)s', level=logging.DEBUG)
from joblib import Parallel, delayed
import click
from xgboost_model.inference import run_inference, PostprocessException
import os

def load_detected_pages(db, buffer_size):
    """
    """
    current_docs = []
    for doc in db.propose_pages.find({'postprocess': None, 'ocr': True}, no_cursor_timeout=True):
        current_docs.append(doc)
        if len(current_docs) == buffer_size:
            yield current_docs
            current_docs = []
    yield current_docs

def do_skip(page, client):
    db = client.pdfs
    coll = db.postprocess_pages
    return coll.count_documents({'pdf_name': page['pdf_name'], 'page_num': page['page_num']}, limit=1) != 0

def postprocess(db_insert_fn, num_processes, weights_pth, skip):
    logging.info('Starting post-processing over detected objects')
    start_time = time.time()
    client = MongoClient(os.environ["DBCONNECT"])
    logging.info(f'Connected to client: {client}.')
    db = client.pdfs
    for batch in load_detected_pages(db, 100):
        logging.info('Loaded next batch. Running postprocessing')
        try:
            pages = Parallel(n_jobs=num_processes)(delayed(run_inference)(page, weights_pth) for page in batch)
        except PostprocessException as e:
            logging.error(f'Postprocessing error in referenced page: {e.page}')
            logging.error(f'Original Exception: {e.original_exception}')
            continue

        db_insert_fn(pages, client)
    end_time = time.time()
    logging.info(f'Exiting post-processing. Time up: {end_time - start_time}')

def mongo_insert_fn(objs, client):
    db = client.pdfs
    for obj in objs:
        try:
            result = db.propose_pages.update_one({'_id': obj['_id']},
                                             {'$set':
                                                {
                                                    'pp_detected_objs': obj['pp_detected_objs'],
                                                    'postprocess': True
                                                }
                                             }, upsert=False)
            logging.info(f'Updated result: {result}')
        except pymongo.errors.WriterError as e:
            logging.error(f'Document write error: {e}\n Document id: obj["_id"]')

@click.command()
@click.argument("num_processes")
@click.argument("weights_pth")
@click.option('--skip/--no-skip')
def click_wrapper(num_processes, weights_pth, skip):
	postprocess(mongo_insert_fn, int(num_processes), weights_pth, skip)

if __name__ == '__main__':
	click_wrapper()


import requests
from bs4 import BeautifulSoup
import time
from redis import Redis

# 本代码用于获取猫眼电影的编码，存入redis中作为任务队列

def get_film_id():

    redis = Redis.from_url("redis://:fxb_fh@120.24.1.93:6379", decode_responses=True)

    # redis.sadd("film_id", "6379")
    # print(redis.spop("film_id"))
    # 清空这两个列表
    while redis.lpop("film_id") is not None:
        pass

    for year_id in [11, 12, 13, 14, 100]:
        for offset in range(0, 1980, 30):
            url = 'https://maoyan.com/films?showType=3&sortId=1&yearId='+str(year_id)+'&offset='+str(offset)
            header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
                                    ' (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36'}
            html = requests.get(url, headers=header)
            soup = BeautifulSoup(str(html.text), 'lxml')
            for a in soup.select('.channel-detail a'):
                f_id = a['data-val'].replace("{movieId:", "").replace("}", "")

                # 放入一份到任务队列
                redis.lpush("film_id", f_id)

                print(f_id)

    print("所有任务添加完成")

if __name__ == "__main__":
    get_film_id()


from lark import *
from lark.lexer import *

from z3 import *

grammar = r'''
        regex: union | simple_re
        union: regex "|" simple_re
        simple_re: concatenation | basic_re
        concatenation: simple_re basic_re
        basic_re: star | plus | elementary_re
        star: elementary_re "*"
        plus: elementary_re "+"
        elementary_re: group | any | eos | char | set
        group: "(" regex ")"
        any: "."
        eos: "$"
        char: non_meta | "\\" meta // specify non meta and meta character
        set: positive_set | negative_set
        positive_set: "[" set_items "]"
        negative_set: "[^" set_items "]"
        set_items: set_item | set_item set_items
        set_item: range | char
        range: char "-" char
        non_meta: "a".."z" | "A".."Z" | "0".."9" | " " | "@"
        meta: "-" | "+"
        '''

def lark2z3(tree):
    if isinstance(tree, Token):
        return Re(tree.value)
    else:
        if tree.data == "regex":
            return lark2z3(tree.children[0])
        elif tree.data == "union":
            result = lark2z3(tree.children[0])
            for i, c in enumerate(tree.children):
                if i != 0 and not isinstance(c, Token):
                    result = Union(result, lark2z3(c))
            return result
        elif tree.data == "simple_re":
            return lark2z3(tree.children[0])
        elif tree.data == "concatenation":
            result = lark2z3(tree.children[0])
            for i, c in enumerate(tree.children):
                if i != 0:
                    result = Concat(result, lark2z3(c))
            return result
        elif tree.data == "basic_re":
            return lark2z3(tree.children[0])
        elif tree.data == "star":
            return Star(lark2z3(tree.children[0]))
        elif tree.data == "plus":
            return Plus(lark2z3(tree.children[0]))
        elif tree.data == "elementary_re":
            return lark2z3(tree.children[0])
        elif tree.data == "group":
            return lark2z3(tree.children[1])
        elif tree.data == "any":
            return Full(ReSort(StringSort()))
#         elif tree.data == "eos":
#             return lark2z3(tree.children[0])
        elif tree.data == "char":
            return lark2z3(tree.children[-1])
        elif tree.data == "set":
            return lark2z3(tree.children[0])
        elif tree.data == "positive_set":
            return lark2z3(tree.children[1])
#         elif tree.data == "negative_set":
#             return lark2z3(tree.children[0])
        elif tree.data == "set_items":
            result = lark2z3(tree.children[0])
            for i, c in enumerate(tree.children):
                if i != 0 and not isinstance(c, Token):
                    result = Union(result, lark2z3(c))
            return result
        elif tree.data == "set_item":
            return lark2z3(tree.children[0])
        elif tree.data == "range":
            begin = tree.children[0].children[0].children[0].value
            end = tree.children[-1].children[0].children[0].value
            result = Re(begin)
            for i, c in enumerate(char_range(begin, end)):
                if i != 0:
                    result = Union(result, Re(c))
            return result
        elif tree.data == "non_meta":
            return Re(tree.children[0])
        elif tree.data == "meta":
            return Re(tree.children[0])
        else:
            return None

def char_range(c1, c2):
    """Generates the characters from `c1` to `c2`, inclusive."""
    for c in range(ord(c1), ord(c2)+1):
        yield chr(c)

# r'@@ \-([0-9]+).*\+([0-9]+).*@@(.*)'
pattern = r'@@ \-([0-9]+).*\+([0-9]+).*@@(.*)'
pattern1 = "ab+c"
pattern2 = "([0-9]+).*"

# parser = Lark(regex_grammar, start='regex')
parser = Lark(grammar, start='regex', keep_all_tokens=True)
print(parser.parse(pattern))
print("-----------")
print(lark2z3(parser.parse(pattern)))


import socket
import common
import time
import serial
from serial import Serial
def server2(q): # 두번째 서버
    cli_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print('Connecting to...', common.server_ip, common.server_port)
    cli_socket.connect((common.server_ip, common.server_port))
    print('Connected to the server...')

    while True:
        msgFromClient = cli_socket.recv(common.BUF_SIZE).decode('utf-8')
        print(msgFromClient)
        if msgFromClient:
            id = msgFromClient.split(' ')[0]
            x = int(float(msgFromClient.split(' ')[1]))
            y = int(float(msgFromClient.split(' ')[2]))
            line = msgFromClient.split(' ')[3]
            print(id)
            print(x)
            print(y)
            print(line)
            q.put(id)
            q.put(x)
            q.put(y)
            q.put(line)
        time.sleep(common.sleep_sec)

#--------------------------------------------------------------------------------------------------------------

import os, sys
import socket
import numpy as np
from server import server_utils as su

# 이선홍이 작성한 서버 gui 코드를 위한 import
import pickle, math
from tkinter import *
from PIL import Image, ImageTk
import threading
from queue import Queue
import time

import psutil

root = None
copy_of_image = None
canvas = None
picture_width = None
picture_height = None
canvas_image = None
id_arr = []

class Usar:  # 사용자 클래스
    def __init__(self, name, width, height):
        self.myrealwidth, self.myrealheight = width, height  # 사용자의 현실좌표
        self.id = name  # id
        x, y = return_image_coordinates(width, height)
        self.text = canvas.create_text(x, y, text=name, font=("나눔고딕코딩", 8), fill="red")

    def setLocation(self, width, height):  # 사용자의 현실위치 수정
        self.myrealwidth, self.myrealheight = width, height

    def getLocation(self):  # 사용자의 현실위치 리턴
        return self.myrealwidth, self.myrealheight

    def getId(self):  # 사용자의 아이디 리턴
        return self.id

    def getText(self):  # 사용자의 객체 리턴
        return self.text

def gui_interface():  # gui 화면 띄우는 함수
    global canvas, canvas_image, picture_width, picture_height, copy_of_image, root, id_arr

    root = Tk()
    root.title("GUI1")
    root.resizable(True, True)

    image = Image.open(su.MAP_IMAGE_FILENALE)  # su.MAP_IMAGE_FILENALE
    copy_of_image = image.copy()  # 카피 본 저장
    photo = ImageTk.PhotoImage(image)

    picture_width, picture_height = image.size  # 사진 크기 저장

    canvas = Canvas(root, width=picture_width, height=picture_height)  # 캔버스
    canvas.pack(fill="both", expand=True)
    canvas_image = canvas.create_image(0, 0, anchor=NW, image=photo)  # 캔버스에 이미지 추가

    canvas.bind('<Configure>', resize_image)  # 창의 크기가 변경되면 resize_image함수 실행
    canvas.bind('<Button-1>', print_x_y)  # 마우스 클릭한 곳에 현실위치 출력
    root.update()
    return root, canvas

def gui_interface_fire():  # gui 화면 띄우는 함수
    global canvas, canvas_image, picture_width, picture_height, copy_of_image, root, id_arr

    root = Tk()
    root.title("fire1")
    root.resizable(True, True)

    image = Image.open(su.MAP_IMAGE_FILENALE2)  # su.MAP_IMAGE_FILENALE
    copy_of_image = image.copy()  # 카피 본 저장
    photo = ImageTk.PhotoImage(image)

    picture_width, picture_height = image.size  # 사진 크기 저장

    canvas = Canvas(root, width=picture_width, height=picture_height)  # 캔버스
    canvas.pack(fill="both", expand=True)
    canvas_image = canvas.create_image(0, 0, anchor=NW, image=photo)  # 캔버스에 이미지 추가

    canvas.bind('<Configure>', resize_image)  # 창의 크기가 변경되면 resize_image함수 실행
    canvas.bind('<Button-1>', print_x_y)  # 마우스 클릭한 곳에 현실위치 출력
    root.update()
    return root, canvas

def gui_interface2(q):

    root, canvas = gui_interface()
    window_on = True
    window_fire_on = False
    my_x = 0
    my_y = 0
    a = None

    while True:

        if q.qsize() == 4:  # q에는 id,x,y순서로 데이터가 들어있어야 한다.
            assert q.qsize() >= 4, 'Q size must > 4'

            userID = q.get()
            x = q.get()
            y = q.get()
            line = q.get()
            gui_close_num=window_open(x,y)
            print("gui_close_num:",gui_close_num)
            print("DATA from Q : ", userID, x, y)
            if su.PRINT_DEBUG: print("DATA from Q : ", userID, x, y)
            if gui_close_num == 1 and line != "FIRE":
                if window_fire_on:
                    print("불창닫기")
                    root.destroy()
                    window_fire_on = False

                if not window_on:
                    print("창 실행")
                    root, canvas = gui_interface()
                    my_x, my_y = x, y
                    my_image_x, my_image_y = return_image_coordinates(x, y)
                    a = canvas.create_text(my_image_x, my_image_y, text=userID, font=("나눔고딕코딩", 10), fill="red")
                    window_on = True

                # 실시간 위치 이동
                if my_x != x or my_y != y:
                    print("위치이동", my_x, "->", x, " ", my_y, "->", y)
                    my_x, my_y = x, y
                    canvas.delete(a)
                    my_image_x, my_image_y = return_image_coordinates(x, y)
                    a = canvas.create_text(my_image_x, my_image_y, text=userID, font=("나눔고딕코딩", 10), fill="red")
                    root.update()  # 화면 업데이트
            elif gui_close_num == 1 and line =="FIRE":
                if window_on:
                    print("지도 창닫기")
                    root.destroy()
                    window_on = False

                if not window_fire_on:
                    print("불 창 실행")
                    root, canvas = gui_interface_fire()
                    my_x, my_y = x, y
                    my_image_x, my_image_y = return_image_coordinates(x, y)
                    a = canvas.create_text(my_image_x, my_image_y, text=userID, font=("나눔고딕코딩", 10), fill="red")
                    window_fire_on = True

                    # 실시간 위치 이동
                if my_x != x or my_y != y:
                    print("위치이동", my_x, "->", x, " ", my_y, "->", y)
                    my_x, my_y = x, y
                    canvas.delete(a)
                    my_image_x, my_image_y = return_image_coordinates(x, y)
                    a = canvas.create_text(my_image_x, my_image_y, text=userID, font=("나눔고딕코딩", 10), fill="red")
                    root.update()  # 화면 업데이트
            else:
                if window_on or window_fire_on:
                    print("창닫기")
                    root.destroy()
                    window_on = False

        try:
            root.update_idletasks()  # 잘 모르겠는데, 같이 쓰더라
            root.update()  # 화면 업데이트
        except:
            continue
        time.sleep(0.2)  # cpu사용량 낮추기 위해 사용

def resize_image(event):  # 창의 크기에 맞게 이미지 크기 조정
    global picture_width, picture_height, copy_of_image, canvas_image, canvas, root

    # 이미지 사이즈 조절
    image = copy_of_image.resize((event.width, event.height))
    photo = ImageTk.PhotoImage(image)
    canvas.itemconfig(canvas_image, image=photo)
    canvas.image = photo

    picture_width, picture_height = event.width, event.height  # 사진 크기 조정

    root.update()

    for us in id_arr:  # 사용자들의 위치를 이미지에 맞게 수정
        x, y = return_image_coordinates(us.getLocation()[0], us.getLocation()[1])
        move_width = x - int(canvas.coords(us.getText())[0])
        move_height = y - int(canvas.coords(us.getText())[1])
        canvas.move(us.getText(), move_width, move_height)
        root.update()

def return_image_coordinates(x, y):  # 현실 좌표를 받으면 사진좌표를 리턴하는 함수
    my_real_width = x
    my_real_height = y
    my_picture_width = my_real_width * picture_width // su.real_width
    my_picture_height = my_real_height * picture_height // su.real_height

    return my_picture_width, my_picture_height  # 사진 속 좌표 리턴

def usar_move(id, real_x, real_y):  # 유저이동함수
    global root
    picture_x, picture_y = return_image_coordinates(real_x, real_y)

    for us in id_arr:
        if us.getId() == id:
            picture_x1, picture_y1 = return_image_coordinates(us.getLocation()[0], us.getLocation()[1])
            move_width = picture_x - picture_x1  # x축 이동거리 계산
            move_height = picture_y - picture_y1  # y축 이동거리 계산
            us.setLocation(real_x, real_y)
            canvas.move(us.getText(), move_width, move_height)  # 공을 이동
            root.update()

def usar_add(id, x, y):  # 유저생성함수
    id_arr.append(Usar(id, x, y))

def usar_delete():  # 유저삭제함수
    num = 0
    for us in id_arr:
        if us.getId() == Entry.get(display_id):
            canvas.delete(us.getText())
            del id_arr[num]
        num = num + 1

def print_x_y(event):  # 클릭한 곳에 현실좌표 출력 하는 함수
    global picture_width, picture_height
    print("이미지 x좌표: ", event.x, "이미지 y좌표: ", event.y)
    real_x = event.x * su.real_width // picture_width
    real_y = event.y * su.real_height // picture_height
    print("현실 x좌표: ", real_x, "현실 y좌표: ", real_y)

def end():
    print("창닫기")
    root.qui

def window_open(x,y):
    gui_num = 0
    if x> 22508 and x < 24799 and y > 0 and y < 5761:
        gui_num = 1
    elif x >15474 and x<17725 and y>0 and y<5761:
        gui_num = 2
    else:
        gui_num = 0
    return gui_num

# 연습용 코드들 다 끝나면 지워도 무방
def de(q):
    while True:
        print("위치이동1")
        q.put("qwe")
        q.put(23593)
        q.put(1933)
        q.put("FIRE")
        time.sleep(2)

        print("위치이동2")
        q.put("qwe")
        q.put(23673)
        q.put(4510)
        q.put("FIRE")
        time.sleep(2)

        print("위치이동3")
        q.put("qwe")
        q.put(21322)
        q.put(4510)
        q.put("FIRE")
        time.sleep(2)

        print("위치이동2")
        q.put("qwe")
        q.put(18810)
        q.put(4510)
        q.put("FIRE")
        time.sleep(2)

        print("위치이동3")
        q.put("qwe")
        q.put(16599)
        q.put(4700)
        q.put(".")
        time.sleep(2)

        print("위치이동3")
        q.put("qwe")
        q.put(16559)
        q.put(2046)
        q.put(".")
        time.sleep(2)

if __name__ == "__main__":
    q = Queue()
    # rss값 수신 및 위치측정을 수행할 스레드 생성 & 시작
    thread_server = threading.Thread(target=de, args=(q,))
    # localization_function이 계산한 위치 값을 전달받아, gui로 표시해 줄 스레드 생성 & 시작
    thread_gui = threading.Thread(target=gui_interface2, args=(q,))
    # thread_end = threading.Thread(target=end, args=(q,))

    thread_server.start()  # 스레드 시작
    thread_gui.start()  # 스레드 시작
    # thread_end.start()
    time.sleep(3)


#cluster_genes.py
from Betsy.protocol_utils import Parameter
import normalize_file
PRETTY_NAME = "Cluster data set."
COMMON = 'Common Parameters'
NORMALIZE = 'Normalize Parameters'
OPTIONAL = 'Optional Parameters'
ILLUMINA = 'Illumina Normalize Parameters'
CLASS_NEIGHBORS = 'Class Neighbor Parameters'
CLUSTER = 'Cluster parameters'
CATEGORIES = [COMMON, NORMALIZE, OPTIONAL, ILLUMINA, CLASS_NEIGHBORS, CLUSTER]

#input  datatype
INPUTS = ['AgilentFiles', 'ExpressionFiles', 'RenameFile', 'GPRFiles',
          'GEOSeries', 'IDATFiles', 'CELFiles', '_SignalFile_Postprocess',
          'ClassLabelFile', 'RenameFile', 'GeneListFile']

#output datatype
OUTPUTS = 'ClusterReportFile'

#parameter objects
PARAMETERS = [
    Parameter('format',
              pretty_name='File Format',
              default_value='tdf',
              choices=['tdf', 'gct'],
              category=COMMON,
              description='output file format',
              datatype='SignalFile'),
    Parameter('preprocess',
              pretty_name='Preprocess',
              choices=normalize_file.PREPROCESS,
              category=COMMON,
              description='preprocess method',
              datatype='SignalFile'),
    Parameter('missing_algorithm',
              pretty_name='How to fill missing value',
              choices=["none", "median_fill", "zero_fill"],
              category=COMMON,
              description='method to the fill the missing value',
              datatype='SignalFile'),
    Parameter('logged',
              pretty_name='Logged or Not',
              default_value='yes',
              choices=['no', 'yes'],
              category=COMMON,
              description='signal is logged or not',
              datatype='SignalFile'),
    Parameter('filter',
              pretty_name='Filter',
              default_value='no',
              choices=['yes', 'no'],
              category=COMMON,
              description='filter the missing value or not',
              datatype='SignalFile'),
    Parameter('quantile_norm',
              pretty_name='Quantile',
              choices=['yes', 'no'],
              category=NORMALIZE,
              description='normalize by quanitle',
              datatype='SignalFile'),
    Parameter('combat_norm',
              pretty_name='Combat',
              choices=['yes', 'no'],
              category=NORMALIZE,
              description='normalize by combat',
              datatype='SignalFile'),
    Parameter('shiftscale_norm',
              pretty_name='Shiftscale',
              choices=['yes', 'no'],
              category=NORMALIZE,
              description='normalize by shiftscale',
              datatype='SignalFile'),
    Parameter('dwd_norm',
              pretty_name='Dwd',
              choices=['yes', 'no'],
              category=NORMALIZE,
              description='normalize by dwd',
              datatype='SignalFile'),
    Parameter('bfrm_norm',
              pretty_name='BFRM',
              choices=['yes', 'no'],
              category=NORMALIZE,
              description='normalize by bfrm',
              datatype='SignalFile'),
    Parameter('predataset',
              pretty_name='Predataset Process',
              choices=['yes', 'no'],
              category=COMMON,
              description='filter and threshold genes',
              datatype='SignalFile'),
    Parameter('gene_center',
              pretty_name='Gene Center',
              choices=['mean', 'median', 'no'],
              category=COMMON,
              description='gene center method',
              datatype='SignalFile'),
    Parameter('gene_normalize',
              pretty_name='Gene Normalize',
              choices=['variance', 'sum_of_squares', 'no'],
              category=COMMON,
              description='gene normalize method',
              datatype='SignalFile'),
    Parameter(
        'gene_order',
        pretty_name='Gene Order',
        choices=["no", "class_neighbors", "gene_list", "t_test_p", "t_test_fdr",
                 'diff_ttest', 'diff_sam', 'diff_ebayes', 'diff_fold_change'],
        category=NORMALIZE,
        description='gene order',
        datatype='SignalFile'),
    Parameter('annotate',
              pretty_name='Annotation',
              choices=['yes', 'no'],
              category=OPTIONAL,
              description='how to annotate the output file',
              datatype='SignalFile'),
    Parameter('unique_genes',
              pretty_name='Unique Genes',
              choices=['average_genes', 'high_var', 'first_gene', 'no'],
              category=COMMON,
              description='how to select unique genes',
              datatype='SignalFile'),
    Parameter('duplicate_probe',
              pretty_name='Duplicate Probe',
              choices=['high_var_probe', 'closest_probe', 'no'],
              category=OPTIONAL,
              description='how to remove duplicate probe',
              datatype='SignalFile'),
    Parameter('num_features',
              pretty_name='Select Gene Number',
              choices=['yes', 'no'],
              category=COMMON,
              description='select num of genes or not',
              datatype='SignalFile'),
    Parameter('platform',
              pretty_name='Platform',
              choices=['yes', 'no'],
              category=OPTIONAL,
              description='output platform',
              datatype='SignalFile'),
    Parameter('group_fc',
              pretty_name='Group Fold Change',
              choices=['yes', 'no'],
              category=OPTIONAL,
              description='filter genes by fold change across classes',
              datatype='SignalFile'),
    Parameter('rename_sample',
              pretty_name='Rename samples',
              choices=['yes', 'no'],
              category=OPTIONAL,
              description='Rename sample name',
              datatype='SignalFile'),
    ###for UserInput
    Parameter('num_factors',
              pretty_name='Number of Factors',
              category=OPTIONAL,
              description='num of factors for bfrm normalization',
              datatype='UserInput'),
    Parameter('filter_value',
              pretty_name='Filter missing Value by Percentage',
              category=OPTIONAL,
              description='percentage to filter the missing value(0-1)',
              datatype='UserInput'),
    Parameter('gene_select_threshold',
              pretty_name='Gene Selection Threshold',
              category=OPTIONAL,
              description='gene select threshold',
              datatype='UserInput'),
    Parameter(
        'platform_name',
        pretty_name='Platform name',
        choices=["'HG_U133_Plus_2'", "'HG_U133B'", "'HG_U133A'",
                 "'HG_U133A_2'", "'HG_U95A'", "'HumanHT_12'", "'HumanWG_6'",
                 "'HG_U95Av2'", "'Entrez_ID_human'", "'Entrez_symbol_human'",
                 "'Hu6800'", "'Mouse430A_2'", "'MG_U74Cv2'", "'Mu11KsubB'",
                 "'Mu11KsubA'", "'MG_U74Av2'", "'Mouse430_2'", "'MG_U74Bv2'",
                 "'Entrez_ID_mouse'", "'MouseRef_8'", "'Entrez_symbol_mouse'",
                 "'RG_U34A'", "'RAE230A'", 'unknown_platform'],
        description='output platform name',
        datatype='UserInput',
        category=OPTIONAL),
    Parameter('num_features_value',
              pretty_name='Select Gene Number',
              category=COMMON,
              description='select num of genes or not',
              datatype='UserInput'),
    Parameter('group_fc_num',
              pretty_name='group_fc value',
              category=COMMON,
              description='value for group fold change',
              datatype='UserInput'),
    ###illumina related
    Parameter(
        'ill_manifest',
        pretty_name='Illumina Manifest File',
        choices=
        ['HumanHT-12_V3_0_R2_11283641_A.txt',
         'HumanHT-12_V4_0_R2_15002873_B.txt', 'HumanHT-12_V3_0_R3_11283641_A.txt',
         'HumanHT-12_V4_0_R1_15002873_B.txt', 'HumanMI_V1_R2_XS0000122-MAP.txt',
         'HumanMI_V2_R0_XS0000124-MAP.txt', 'HumanRef-8_V2_0_R4_11223162_A.txt',
         'HumanRef-8_V3_0_R1_11282963_A_WGDASL.txt',
         'HumanRef-8_V3_0_R2_11282963_A.txt', 'HumanRef-8_V3_0_R3_11282963_A.txt',
         'HumanWG-6_V2_0_R4_11223189_A.txt', 'HumanWG-6_V3_0_R2_11282955_A.txt',
         'HumanWG-6_V3_0_R3_11282955_A.txt', 'MouseMI_V1_R2_XS0000127-MAP.txt',
         'MouseMI_V2_R0_XS0000129-MAP.txt', 'MouseRef-8_V1_1_R4_11234312_A.txt',
         'MouseRef-8_V2_0_R2_11278551_A.txt', 'MouseRef-8_V2_0_R3_11278551_A.txt',
         'MouseWG-6_V1_1_R4_11234304_A.txt', 'MouseWG-6_V2_0_R2_11278593_A.txt',
         'MouseWG-6_V2_0_R3_11278593_A.txt', 'RatRef-12_V1_0_R5_11222119_A.txt'],
        category=ILLUMINA,
        description='Illumina manifest file in tab-delimited (TXT) format',
        datatype="ILLUFolder"),
    Parameter('ill_chip',
              pretty_name='Illumina chip File',
              choices=['ilmn_HumanHT_12_V3_0_R3_11283641_A.chip',
                       'ilmn_HumanHT_12_V4_0_R1_15002873_B.chip',
                       'ilmn_HumanRef_8_V2_0_R4_11223162_A.chip',
                       'ilmn_HumanReF_8_V3_0_R1_11282963_A_WGDASL.chip',
                       'ilmn_HumanRef_8_V3_0_R3_11282963_A.chip',
                       'ilmn_HumanWG_6_V2_0_R4_11223189_A.chip',
                       'ilmn_HumanWG_6_V3_0_R3_11282955_A.chip',
                       'ilmn_MouseRef_8_V1_1_R4_11234312_A.chip',
                       'ilmn_MouseRef_8_V2_0_R3_11278551_A.chip',
                       'ilmn_MouseWG_6_V1_1_R4_11234304_A.chip',
                       'ilmn_MouseWG_6_V2_0_R3_11278593_A.chip',
                       'ilmn_RatRef_12_V1_0_R5_11222119_A.chip'],
              category=ILLUMINA,
              description='CHIP file to map probes to genes.',
              datatype="ILLUFolder"),
    Parameter('ill_clm',
              pretty_name='Illumina clm File',
              category=ILLUMINA,
              description='CLM file to map file names to sample names.',
              datatype="ILLUFolder"),
    Parameter('ill_custom_chip',
              pretty_name='Illumina Custom Chip File',
              category=ILLUMINA,
              description='Other CHIP file to map probes to genes.',
              datatype="ILLUFolder"),
    Parameter('ill_custom_manifest',
              pretty_name='Illumina Custom Manifest File',
              category=ILLUMINA,
              description=
              'Other Illumina manifest file in tab-delimited (TXT) format.',
              datatype="ILLUFolder"),
    Parameter('ill_bg_mode',
              pretty_name='Illumina Background Mode',
              choices=['ill_yes', 'ill_no'],
              category=ILLUMINA,
              description='Perform background subtraction.',
              datatype="ILLUFolder"),
    Parameter(
        'ill_coll_mode',
        pretty_name='Illumina Coll Mode',
        choices=['none', 'max', 'median'],
        category=ILLUMINA,
        description=
        'Collapse probes to genes based on the manifest or CHIP file (if provided).',
        datatype="ILLUFolder"),
    #class neighbors
    Parameter('cn_mean_or_median',
              pretty_name='cn mean or median',
              category=CLASS_NEIGHBORS,
              choices=['mean', 'median'],
              description='use mean or median for feature selection',
              datatype='GeneListFile'),
    Parameter('cn_ttest_or_snr',
              pretty_name='cn ttest or snr',
              category=CLASS_NEIGHBORS,
              choices=['t_test', 'snr'],
              description='use signal-to-noise or t-test to select neighbors',
              datatype='GeneListFile'),
    Parameter('cn_filter_data',
              choices=['yes', 'no'],
              category=CLASS_NEIGHBORS,
              pretty_name='cn filter data',
              description='if no, values below will be ignored',
              datatype='GeneListFile'),
    Parameter('cn_num_neighbors',
              pretty_name='cn num neighbors',
              category=CLASS_NEIGHBORS,
              description='number of neighbors to find',
              datatype='UserInput'),
    Parameter('cn_num_perm',
              pretty_name='cn num permutations',
              category=CLASS_NEIGHBORS,
              description='number of permutations in permutation test',
              datatype='UserInput'),
    Parameter('cn_user_pval',
              pretty_name='cn user pval',
              category=CLASS_NEIGHBORS,
              description='user-set significance value for permutation test',
              datatype='UserInput'),
    Parameter('cn_min_threshold',
              category=CLASS_NEIGHBORS,
              pretty_name='cn min threshold',
              description='minimum threshold for data',
              datatype='UserInput'),
    Parameter('cn_max_threshold',
              category=CLASS_NEIGHBORS,
              pretty_name='cn max threshold',
              description='maximum threshold for data',
              datatype='UserInput'),
    Parameter('cn_min_folddiff',
              category=CLASS_NEIGHBORS,
              pretty_name='cn min fold diff',
              description='minimum fold difference for filtering genes',
              datatype='UserInput'),
    Parameter('cn_abs_diff',
              category=CLASS_NEIGHBORS,
              pretty_name='cn abs diff',
              description='minimum absolute difference for filtering genes',
              datatype='UserInput'),
    #cluster related
    Parameter('color',
              pretty_name='Color',
              choices=['red_green', 'blue_yellow'],
              category=CLUSTER,
              description='heatmap color',
              datatype='Heatmap'),
    Parameter('hm_width',
              pretty_name='Heatmap Width',
              category=CLUSTER,
              description='heatmap width',
              datatype='UserInput'),
    Parameter('hm_height',
              pretty_name='Heatmap Height',
              category=CLUSTER,
              description='heatmap height',
              datatype='UserInput'),
    Parameter('k-value',
              pretty_name='K value',
              category=CLUSTER,
              description='K number in K means',
              datatype='UserInput'),
    Parameter('cluster_alg',
              pretty_name='Cluster algorithm',
              default_value='kmeans',
              choices=['kmeans', 'pca', 'hierarchical', 'som'],
              category=CLUSTER,
              description='cluster algorithm',
              datatype='ClusterFile'),
    Parameter('distance',
              pretty_name='Distance',
              default_value='correlation',
              choices=['correlation', 'euclidean'],
              category=CLUSTER,
              description='distance method in cluster',
              datatype='ClusterFile'),
    Parameter('contents',
              pretty_name='Contents',
              category=OPTIONAL,
              choices=normalize_file.CONTENTS,
              description='output group information',
              datatype='ClusterFile'),
]


from xai.brain.wordbase.nouns._bedbug import _BEDBUG

#calss header
class _BEDBUGS(_BEDBUG, ):
	def __init__(self,):
		_BEDBUG.__init__(self)
		self.name = "BEDBUGS"
		self.specie = 'nouns'
		self.basic = "bedbug"
		self.jsondata = {}


from __future__ import annotations
from abc import ABC, abstractmethod
from random import randrange
from typing import List

class CountTo:
    def __init__(self, upperBound):
        self.upperBound = upperBound
        self.numbersInEnglish = ["Один", "Два", "Три", "Четыре", "Пять", 'Шесть', 'Семь', 'Восемь', 'Девять']
        self.numbersInEnglish = self.numbersInEnglish[:upperBound]
        self.index = 0                                                          # Нужен для отслеживания текущей позиции итератора в списке
    def __iter__(self):
        return self
    def __next__(self):
        try:
            result = self.numbersInEnglish[self.index]
        except IndexError:
            print('В итерируемой структуре данных объектов меньше, чем введенное число')
            raise StopIteration

        self.index += 1
        return result

c = int(input('Введите количество обьектов (1-9)'))

countToN = CountTo(c)
for i in range(countToN.upperBound):
    print(next(countToN))


import torch
import torch.nn as nn
from ..utils import Anchors, FeaturePyramid
from ..utils import BBoxTransform, ClipBoxes
from ..losses import FocalLoss

def initwt(m):
    if isinstance(m, torch.nn.Conv2d):
        nn.init.xavier_uniform_(m.weight)
        if m.bias is not None:
            nn.init.zeros_(m.bias)

class Feat(nn.Module):

    def __init__(self):
        super(Feat, self).__init__()
        self.conv0 = nn.Sequential(nn.Conv2d(3, 16, 3, 1, 1, bias=False),
                                   nn.BatchNorm2d(16),
                                   nn.LeakyReLU(0.1))
        self.conv1 = nn.Sequential(nn.Conv2d(16, 32, 3, 1, 1, bias=False),
                                   nn.BatchNorm2d(32),
                                   nn.MaxPool2d(2, 2),
                                   nn.LeakyReLU(0.1))
        self.conv2 = nn.Sequential(nn.Conv2d(32, 64, 3, 1, 1, bias=False),
                                   nn.BatchNorm2d(64),
                                   nn.MaxPool2d(2, 2),
                                   nn.LeakyReLU(0.1))
        self.conv3 = nn.Sequential(nn.Conv2d(64, 128, 3, 1, 1, bias=False),
                                   nn.BatchNorm2d(128),
                                   nn.MaxPool2d(2, 2),
                                   nn.LeakyReLU(0.1))
        self.conv4 = nn.Sequential(nn.Conv2d(128, 256, 3, 1, 1, bias=False),
                                   nn.BatchNorm2d(256),
                                   nn.MaxPool2d(2, 2),
                                   nn.LeakyReLU(0.1))
        self.conv5 = nn.Sequential(nn.Conv2d(256, 512, 3, 1, 1, bias=False),
                                   nn.BatchNorm2d(512),
                                   nn.MaxPool2d(2, 2),
                                   nn.LeakyReLU(0.1))

    def forward(self, x):
        x = self.conv0(x)
        x = self.conv1(x)
        x = self.conv2(x)
        x3 = self.conv3(x)
        x4 = self.conv4(x3)
        x5 = self.conv5(x4)
        return [x3, x4, x5]

class Clsf(nn.Module):
    """ classification subnet """

    def __init__(self, ncls, nanc):
        super(Clsf, self).__init__()
        self.ncls = ncls
        self.nanc = nanc
        self.conv1 = nn.Sequential(nn.Conv2d(512, 512, 3, 1, 1, bias=False),
                                   nn.BatchNorm2d(512),
                                   nn.LeakyReLU(0.1))
        self.conv2 = nn.Sequential(nn.Conv2d(512, 512, 3, 1, 1, bias=False),
                                   nn.BatchNorm2d(512),
                                   nn.LeakyReLU(0.1))
        self.convc = nn.Sequential(nn.Conv2d(512, nanc*ncls, 3, 1, 1),
                                   nn.Sigmoid())

    def forward(self, ftr):
        out = self.conv1(ftr)
        out = self.conv2(out)
        out = self.convc(out)
        out = out.permute(0, 2, 3, 1)
        batchsz, width, height, channels = out.shape
        out = out.view(batchsz, width, height, self.nanc, self.ncls)
        return out.contiguous().view(ftr.shape[0], -1, self.ncls)

class Rgrs(nn.Module):
    """ regression subnet """

    def __init__(self, nanc):
        super(Rgrs, self).__init__()
        self.conv1 = nn.Sequential(nn.Conv2d(512, 512, 3, 1, 1, bias=False),
                                   nn.BatchNorm2d(512),
                                   nn.LeakyReLU(0.1))
        self.conv2 = nn.Sequential(nn.Conv2d(512, 512, 3, 1, 1, bias=False),
                                   nn.BatchNorm2d(512),
                                   nn.LeakyReLU(0.1))
        self.convr = nn.Conv2d(512, nanc*4, 3, 1, 1)

    def forward(self, ftr):
        out = self.conv1(ftr)
        out = self.conv2(out)
        out = self.convr(out)
        out = out.permute(0, 2, 3, 1)
        return out.contiguous().view(out.shape[0], -1, 4)

class MiniRetina(nn.Module):

    def __init__(self, cfg):
        super(MiniRetina, self).__init__()
        self.feat = Feat()
        self.fpns = FeaturePyramid()
        self.clsf = Clsf(cfg.ncls, len(cfg.ancs))
        self.rgrs = Rgrs(len(cfg.ancs))
        self.ancs = Anchors()
        self.regrBoxes = BBoxTransform()
        self.clipBoxes = ClipBoxes()
        self.loss = FocalLoss()
        self.training = False

    def forward(self, imgs, anns):
        ftr = self.feat(imgs)
        ftrs = self.fpns(ftr)
        regs = torch.cat([self.rgrs(ftr) for ftr in ftrs], dim=1)
        ctgs = torch.cat([self.clsf(ftr) for ftr in ftrs], dim=1)
        ancs = self.ancs(x)
        if self.training:
            return self.loss(ctgs, regs, ancs, anns)
        transformed_anchors = self.regrBoxes(anchors, regress)
        transformed_anchors = self.clipBoxes(transformed_anchors, imgbatch)
        scores = torch.max(classif, dim=2, keepdim=True)[0]
        scores_over_thresh = (scores>0.05)[0, :, 0]
        if scores_over_thresh.sum() == 0:
            return [torch.zeros(0), torch.zeros(0), torch.zeros(0, 4)]
        classif= classif[:, scores_over_thresh, :]
        transformed_anchors = transformed_anchors[:, scores_over_thresh, :]
        scores = scores[:, scores_over_thresh, :]
        anchors_nms_idx = nms(torch.cat([transformed_anchors, scores], dim=2)[0, :, :], 0.5)
        nms_scores, nms_class = classif[0, anchors_nms_idx, :].max(dim=1)

if __name__ == '__main__':
    from cfg import cfg

    miretina = MiniRetina(cfg)
    inp = torch.rand(4, 3, 288, 288)
    ctg, reg = miretina(inp)
    print(ctg.shape, reg.shape)


# -*- coding: utf-8 -*-
from logya.compat import configparser

class Config:
    """Site configuration access."""

    def __init__(self, filename):
        """Create ConfigParser object read from the given file."""

        self.config = configparser.ConfigParser()
        self.config.readfp(open(filename))

    def get(self, section, var, required=False):
        try:
            val = self.config.get(section, var)
        except Exception:
            if required:
                raise Exception
            else:
                return False
        else:
            return val

    def items(self, section):
        return self.config.items(section)


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def generateTrees(self, n: int):
        """
        https: // en.wikipedia.org / wiki / Catalan_number
        Time Complexity: O(C(n))
        Space Complexity: O(C(n))
        """
        def generator(low, upper):
            if len(arr[low: upper]) == 0:
                yield None
            for i in range(low, upper):
                for left in generator(low, i):
                    for right in generator(i + 1, upper):
                        yield TreeNode(arr[i], left, right)

        arr = list(range(1, n + 1))
        return [root for root in generator(0, n)]

if __name__ == '__main__':
    s = Solution()
    print(s.generateTrees(3))


# def say_hi():
#     print("hi")
#     print("bye")
# say_hi()

# def add_two_number(a, b):
#     print("Tong hai so la:", a+b)
# num1 = int(input("Nhap so thu nhat: "))
# num2 = int(input("Nhap so thu hai: "))
# add_two_number(num1, num2)

# def add_two_number(a,b):
#     return a + b
# num1 = int(input("Nhap so thu nhat: "))
# num2 = int(input("Nhap so thu hai: "))
# num3 = int(input("Nhap so thu ba: "))
# sum_1_2 = add_two_number(num1, num2)
# sum_3 = add_two_number(sum_1_2, num3)
# print("Tong ba so la:", sum_3)

# def abs_of_number(a):
#     if a > 0:
#         return a
#         print("Tri tuyet doi la:", a)
#     else:
#         return -a
#         print("Tri tuyet doi la:", a)
# x = abs_of_number(-12)
# tong = 12 + abs_of_number(-12)
# print(x)
# print(tong)

# def evaluate (a, b, operator):
#     if operator == "*":
#         return a*b
#     elif operator == "+":
#         return a+b
#     elif operator == "-":
#         return a-b
#     elif operator == "/":
#         return a/b
#     elif operator == "**":
#         return a**b
# print(evaluate (2, 3, "*"))

# t = 0
# n = int(input("Moi nhap so: "))
# for i in range (1, n+1):
#     du = n%i
#     if du == 0:
#         t = t + 1
# if t > 2:
#     print("Day khong phai la so nguyen to.")
# elif n < 2:
#     print("Day khong phai la so nguyen to.")
# else:
#     print("Day la so nguyen to")

def is_prime(n):
    if n < 2:
        return False
    for v in range(2,n):
        if n%v==0:
            return False
    return True
so = int(input("Nhap so can kiem tra: "))

for v in range(2, so+1):
    if is_prime(v):
        print("So nguyen to la:",v)

# if is_prime(so):
#     print ("La so nguyen to.")
# else:
#     print("Khong la so nguyen to")


try:
    import ujson as json
except ImportError:
    import json

def json_response(struct):
    body = json.dumps(struct)

    t = [
        b'HTTP/1.1 200 OK',
        b'Content-Type:application/json; charset=utf-8',
        b'',
        b'',
        b'\r\n']

    t[-2] = bytes(body, encoding='utf-8')
    t[-3] = bytes('Content-Length:'+str(len(body)+4) + "\r\n", encoding='utf-8')

    return b'\r\n'.join(t)


'''
Created on 2018-3-18
基础信息
@author: Administrator
'''
from app.models import Character, Minister, Property

def displayKing(characterid):
    '''君王信息展示
          @param characterid: int 君王主键id
    '''
    character = Character.query.filter_by(characterid=characterid).first()
    exp = character.characterexp
    level = 1
    while exp > 0:
        if exp - 200 * level >= 0:
            exp = exp - 200 * level
            level += 1
        else:
            break
    percent = round(exp / (character.characterlevel * 200.0) * 100, 2)  # 经验值百分比
    item = {}
    item['charactername'] = character.charactername
    item['charactersex'] = character.charactersex
    item['characterexp'] = exp
    item['characterexppercent'] = percent
    item['characterlevel'] = character.characterlevel
    item['characterpower'] = character.characterpower
    item['charactercoin'] = character.charactercoin
    item['miraclestate'] = character.miraclestate
    return {'result': item}

def displayMinisterList(characterid):
    '''大臣列表展示
        @param characterid: int 君王主键id
        @param ministerid: int 大臣主键id
     '''
    ministers = Minister.query.filter_by(characterid=characterid).all()
    item = {}
    result = []
    for x in range(len(ministers)):
        item[x] = {}
        exp = ministers[x].ministerexp
        level = 1
        while exp > 0:
            if exp - 100 * level >= 0:
                exp = exp - 100 * level
                level += 1
            else:
                break
        percent = round(exp / (ministers[x].ministerlevel * 200.0) * 100, 2)
        item[x]['ministerid'] = ministers[x].ministerid
        item[x]['ministername'] = ministers[x].ministername
        item[x]['typeid'] = ministers[x].typeid
        item[x]['ministerexp'] = exp
        item[x]['ministerexppercent'] = percent
        item[x]['ministerlevel'] = ministers[x].ministerlevel
        item[x]['ministerpower'] = ministers[x].ministerpower
        item[x]['cursetimes'] = ministers[x].cursetimes
        item[x]['is_K'] = ministers[x].is_K
    for x, y in item.items():
        result.append(y)
    return {'result': result}

def displayMinister(characterid):
    '''大臣姓名列表展示
        @param characterid: int 君王主键id
        @param ministerid: int 大臣主键id
     '''
    ministers = Minister.query.filter_by(characterid=characterid).all()
    item = {}
    id = []
    name = []
    for x in ministers:
        id.append(x.ministerid)
        name.append(x.ministername)
    item['id'] = id
    item['name'] = name
    return {'result': item}

def displayproperty(characterid):
    '''道具列表展示
        @param characterid: int 君王主键id
     '''
    character = Character.query.filter_by(characterid=characterid).first()
    propertise = eval(character.propertyid)
    keys = []
    numbers = []
    item = {}
    result = []
    for key, value in propertise.items():
        if value > 0:
            keys.append(key)
            numbers.append(value)

    for x in range(len(keys)):
        item[x] = {}
        p = Property.query.filter_by(propertyid=int(keys[x])).first()
        item[x]['propertyid'] = p.propertyid
        item[x]['propertyname'] = p.propertyname
        item[x]['propertyinfo'] = p.propertyinfo
        item[x]['numbers'] = numbers[x]
    for x,y in item.items():
        result.append(y)
    return {'result': result}


def board(inp):
    game = list(l[1:-1] for l in inp[1:-1])
    sol = [inp[0]]
    for i in range(len(game)):
        line = ""
        for j in range(len(game[0])):
            if game[i][j] == "*": line += "*"
            else:
                m = count_mines(i, j, game)
                line += str(m) if m else " "
        sol.append(f"|{line}|")
    sol.append(inp[-1])
    return sol
def count_mines(x, y, matrix):
    l, c = len(matrix), len(matrix[0])
    lines, cols = range(l), range(c)
    in_board = lambda x, y: x in lines and y in cols
    total = ((matrix[(x-1)%l][(y-1)%c]=="*")*in_board(x-1, y-1) + (matrix[x][(y-1)%c]=="*")*in_board(x, y-1) +
             (matrix[(x+1)%l][(y-1)%c]=="*")*in_board(x+1, y-1) + (matrix[(x-1)%l][y]=="*")*in_board(x-1, y) +
             (matrix[(x+1)%l][y]=="*")*in_board(x+1, y) + (matrix[(x-1)%l][(y+1)%c]=="*")*in_board(x-1, y+1) +
             (matrix[x][(y+1)%c]=="*")*in_board(x, y+1) + (matrix[(x+1)%l][(y+1)%c]=="*")*in_board(x+1, y+1))
    return total


# Configuration file for EmptySource

import FWCore.ParameterSet.Config as cms

process = cms.Process("TEST")

process.load("FWCore.Framework.test.cmsExceptionsFatal_cff")

process.maxEvents = cms.untracked.PSet(
    input = cms.untracked.int32(20)
)

process.options = cms.untracked.PSet(multiProcesses=cms.untracked.PSet(
        maxChildProcesses=cms.untracked.int32(3),
        maxSequentialEventsPerChild=cms.untracked.uint32(2)))

process.source = cms.Source("NewEventStreamFileReader", fileNames = cms.untracked.vstring("file:multiprocess_oneLumi_test.dat"))

ids = cms.VEventID()
numberOfEventsInRun = 0
numberOfEventsPerRun = 20
run = 101
event=0
for i in xrange(20):
   numberOfEventsInRun +=1
   event += 1
   if numberOfEventsInRun > numberOfEventsPerRun:
      numberOfEventsInRun=1
      run += 1
      event = 1
   ids.append(cms.EventID(run,event))
process.check = cms.EDAnalyzer("EventIDChecker",
                                eventSequence = cms.untracked(ids),
                                multiProcessSequentialEvents = process.options.multiProcesses.maxSequentialEventsPerChild)
process.print1 = cms.OutputModule("AsciiOutputModule")

process.p = cms.EndPath(process.check+process.print1)


# -*- coding: utf-8 -*-
"""
Created on Fri Mar 22 02:09:53 2019

@author: CodersMine
"""

import wikipedia

#wikipedia.search("apple")

#wikipedia.summary("apple",sentences=4)

name="Results"
text=""
c=""
import pandas as pd
df=pd.read_excel("list.xlsx").values

for i in range(len(df)):
	if(df[i][1]==1):
		try:
			tt=wikipedia.page(df[i][0])
			c=tt.content
			text+="\n\n"+df[i][0]+"\n\n"
		except:
			c=""
	elif(df[i][1]==2):
		try:
			file.write(text)
			file.close()
			text=""
			c=""
		except:
			print("file already closed")
		name=df[i][0]
		file=open(name+".txt",'w')
	else:
		try:
			c=wikipedia.summary(df[i][0])
			text+="\n\n"+df[i][0]+"\n\n"
		except:
			print("Not found for "+df[i][0])
			c=""
	text+=c

file.write(text)
file.close()


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

import os
import sys
import oss2

access_key_id = ""
access_key_secret = ""
bucket_name = "pi-miner"
painull_path = "tools/painull/v%s"

def upload_file(file, version):
    auth = oss2.Auth(access_key_id, access_key_secret)
    endpoint = "http://oss-cn-beijing.aliyuncs.com"
    bucket = oss2.Bucket(auth, endpoint, bucket_name)
    # info = bucket.get_bucket_info()

    oss_path = os.path.join(painull_path % version, os.path.basename(file))
    for i in range(5):
        try:
            bucket.put_object_from_file(oss_path, file)
        except Exception as e:
            print("Upload file error: %s" % e)
        else:
            print("Upload file completely")
            break

if __name__ == '__main__':
    if len(sys.argv) >= 3:
        file_version = sys.argv[1]
        file_name = sys.argv[2]
        print("Upload file: %s" % file_name)
        print("Upload file version: %s" % file_version)
        upload_file(file_name, file_version)
    else:
        print("Input parameter error")


from utils.luigi_ext import GZCSVLoadMixin, DataSourceConfig
import luigi

class TalHubETL(luigi.WrapperTask):
    id = luigi.IntParameter()

    def requires(self):
        yield PSRSkillLoad(id=self.id)
        yield PersonAssessmentLoad(id=self.id)
        yield AssessmentStatusLoad(id=self.id)
        yield AssessmentMethodLoad(id=self.id)
        yield AssessmentTypeLoad(id=self.id)
        yield ProficiencyLoad(id=self.id)
        yield PSRSkillTypeLoad(id=self.id)
        yield SpecialtySkillClassificationLoad(id=self.id)
        yield ValidationStatusLoad(id=self.id)
        yield LevelOfInterestLoad(id=self.id)
        yield PersonSpecialtyLoad(id=self.id)
        yield SourceSystemLoad(id=self.id)
        yield SpecialtyStatusLoad(id=self.id)
        yield SpecialiaztionsLoad(id=self.id)
        yield CompetancyHierarchyUnfoldedLoad(id=self.id)
        yield GeolocationLoad(id=self.id)
        yield PSRSkillProximityTypeLoad(id=self.id)
        yield PSRSkillPSRSkillTypeLoad(id=self.id)
        yield PSRSkillSkillCollectionLoad(id=self.id)
        yield SapSkillCollectionLoad(id=self.id)
        yield SkillCollectionLoad(id=self.id)
        yield SkillCollectionRelLoad(id=self.id)
        yield SkillCollectionTypeLoad(id=self.id)
        yield SkillHistoryLoad(id=self.id)

class GeolocationLoad(GZCSVLoadMixin):
    FILE_NAME = 'geolocation.csv.gz'
    TABLE_NAME = 'talhub.geolocation'
    base_path = DataSourceConfig().talhub_path
    columns = [
            ("id", "DECIMAL"),
            ("city", "TEXT"),
            ("country", "TEXT"),
            ("longitude", "DECIMAL"),
            ("latitude", "DECIMAL")
    ]

class CompetancyHierarchyUnfoldedLoad(GZCSVLoadMixin):
    FILE_NAME = 'competancyhierarchyunfolded.csv.gz'
    TABLE_NAME = 'talhub.competancyhierarchyunfolded'
    base_path = DataSourceConfig().talhub_path
    columns = [
            ("competencynodeid", "DECIMAL"),
            ("competencynodeshortnm", "TEXT"),
            ("competencynodelongnm", "TEXT"),
            ("validfromdttm", "TIMESTAMP"),
            ("validtodttm", "TIMESTAMP"),
            ("competencylevel1nodeid", "DECIMAL"),
            ("competencylevel1shortnm", "TEXT"),
            ("competencylevel1longnm", "TEXT"),
            ("competencylevel2nodeid", "DECIMAL"),
            ("competencylevel2shortnm", "TEXT"),
            ("competencylevel2longnm", "TEXT"),
            ("competencylevel3nodeid", "DECIMAL"),
            ("competencylevel3shortnm", "TEXT"),
            ("competencylevel3longnm", "TEXT"),
            ("competencylevel4nodeid", "DECIMAL"),
            ("competencylevel4shortnm", "TEXT"),
            ("competencylevel4longnm", "TEXT"),
            ("competencylevel5nodeid", "DECIMAL"),
            ("competencylevel5shortnm", "TEXT"),
            ("competencylevel5longnm", "TEXT"),
            ("competencylevel6nodeid", "DECIMAL"),
            ("competencylevel6shortnm", "TEXT"),
            ("competencylevel6longnm", "TEXT"),
            ("competencylevel7nodeid", "DECIMAL"),
            ("competencylevel7shortnm", "TEXT"),
            ("competencylevel7longnm", "TEXT"),
            ("competencylevel8nodeid", "DECIMAL"),
            ("competencylevel8shortnm", "TEXT"),
            ("competencylevel8longnm", "TEXT"),
            ("competencylevel9nodeid", "DECIMAL"),
            ("competencylevel9shortnm", "TEXT"),
            ("competencylevel9longnm", "TEXT"),
            ("competencylevel10nodeid", "DECIMAL"),
            ("competencylevel10shortnm", "TEXT"),
            ("competencylevel10longnm", "TEXT"),
            ("competencylevel11nodeid", "DECIMAL"),
            ("competencylevel11shortnm", "TEXT"),
            ("competencylevel11longnm", "TEXT"),
            ("competencylevel12nodeid", "DECIMAL"),
            ("competencylevel12shortnm", "TEXT"),
            ("competencylevel12longnm", "TEXT"),
            ("capabilityind", "BOOLEAN"),
            ("specialtyind", "BOOLEAN"),
            ("skillgroupind", "BOOLEAN"),
            ("createdttm", "TIMESTAMP"),
            ("updatedttm", "TIMESTAMP")
    ]

class PSRSkillLoad(GZCSVLoadMixin):
    FILE_NAME = 'psrskill.csv.gz'
    TABLE_NAME = 'talhub.psrskill'
    base_path = DataSourceConfig().talhub_path
    columns = [
            ("psrskillid", "DECIMAL"),
            ("psrskilltypecd", "DECIMAL"),
            ("skilllongnm", "TEXT"),
            ("skillshortnm", "TEXT"),
            ("skilldesc", "TEXT"),
            ("sapskillid", "DECIMAL"),
            ("sapcompoundskillid", "DECIMAL"),
            ("sapcapabilityid", "DECIMAL"),
            ("sapskilltype", "DECIMAL"),
            ("sapskillcategory", "DECIMAL"),
            ("nongovernedskillind", "BOOLEAN"),
            ("specialtyind", "BOOLEAN"),
            ("skillindmodifieddttm", "TIMESTAMP"),
            ("activeind", "BOOLEAN"),
            ("replacementsapskillid", "DECIMAL"),
            ("replacementsapcompoundskillid", "DECIMAL"),
            ("replacementsapcapabilityid", "DECIMAL"),
            ("createdttm", "TIMESTAMP"),
            ("updatedttm", "TIMESTAMP"),
            ]

class PSRSkillProximityTypeLoad(GZCSVLoadMixin):
    FILE_NAME = 'psrskillproximitytype.csv.gz'
    TABLE_NAME = 'talhub.psrskillproximitytype'
    base_path = DataSourceConfig().talhub_path
    columns = [
            ("psrskillproximitytypeid", "DECIMAL"),
            ("psrskillproximitytypenm", "TEXT"),
            ("createdttm", "TIMESTAMP"),
            ("updatedttm", "TIMESTAMP")
    ]

class PSRSkillPSRSkillTypeLoad(GZCSVLoadMixin):
    FILE_NAME = 'psrskillpsrskilltype.csv.gz'
    TABLE_NAME = 'talhub.psrskillpsrskilltype'
    base_path = DataSourceConfig().talhub_path
    columns = [
            ("psrskillpsrskilltypeid", "DECIMAL"),
            ("psrskillid", "DECIMAL"),
            ("psrskilltypecd", "DECIMAL"),
            ("createdttm", "TIMESTAMP"),
            ("updatedttm", "TIMESTAMP")
    ]

class PSRSkillSkillCollectionLoad(GZCSVLoadMixin):
    FILE_NAME = 'psrskillskillcollection.csv.gz'
    TABLE_NAME = 'talhub.psrskillskillcollection'
    base_path = DataSourceConfig().talhub_path
    columns = [
            ("psrskillskillcollectionid", "DECIMAL"),
            ("skillcollectionid", "DECIMAL"),
            ("psrskillid", "DECIMAL"),
            ("createdttm", "TIMESTAMP"),
            ("updatedttm", "TIMESTAMP")
    ]

class SapSkillCollectionLoad(GZCSVLoadMixin):
    FILE_NAME = 'sapskillcollection.csv.gz'
    TABLE_NAME = 'talhub.sapskillcollection'
    base_path = DataSourceConfig().talhub_path
    columns = [
            ("id", "DECIMAL"),
            ("role_id", "DECIMAL"),
            ("role_name", "TEXT"),
            ("skill_id", "DECIMAL"),
            ("skill_name", "TEXT")
    ]

class SkillCollectionLoad(GZCSVLoadMixin):
    FILE_NAME = 'skillcollection.csv.gz'
    TABLE_NAME = 'talhub.skillcollection'
    base_path = DataSourceConfig().talhub_path
    columns = [
            ("skillcollectionid", "DECIMAL"),
            ("skillcollectionnm", "TEXT"),
            ("skillcollectiondesc", "TEXT"),
            ("skillcollectiontypeid", "DECIMAL"),
            ("begineffectivedate", "TIMESTAMP"),
            ("endeffectivedate", "TIMESTAMP"),
            ("createdttm", "TIMESTAMP"),
            ("updatedttm", "TIMESTAMP"),
            ("anchorpsrskillid", "DECIMAL")
    ]

class SkillCollectionRelLoad(GZCSVLoadMixin):
    FILE_NAME = 'skillcollectionrel.csv.gz'
    TABLE_NAME = 'talhub.skillcollectionrel'
    base_path = DataSourceConfig().talhub_path
    columns = [
            ("skillcollectionrelid", "DECIMAL"),
            ("parentskillcollectionid", "DECIMAL"),
            ("childskillcollectionid", "DECIMAL"),
            ("createdttm", "TIMESTAMP"),
            ("updatedttm", "TIMESTAMP")
    ]

class SkillCollectionTypeLoad(GZCSVLoadMixin):
    FILE_NAME = 'skillcollectiontype.csv.gz'
    TABLE_NAME = 'talhub.skillcollectiontype'
    base_path = DataSourceConfig().talhub_path
    columns = [
            ("skillcollectiontypeid", "DECIMAL"),
            ("skillcollectionname", "TEXT"),
            ("skillcollectiondesc", "TEXT"),
            ("createdttm", "TIMESTAMP"),
            ("updatedttm", "TIMESTAMP")
    ]

class SkillHistoryLoad(GZCSVLoadMixin):
    FILE_NAME = 'skillhistory.csv.gz'
    TABLE_NAME = 'talhub.skillhistory'
    base_path = DataSourceConfig().talhub_path
    columns = [
            ("skillhistoryid", "DECIMAL"),
            ("personassessmentid", "DECIMAL"),
            ("roleid", "TEXT"),
            ("rolename", "TEXT"),
            ("skillid", "DECIMAL"),
            ("skillname", "TEXT"),
            ("projectid", "TEXT"),
            ("clientid", "TEXT"),
            ("recency", "TEXT"),
            ("duration", "TEXT"),
            ("sourcevariance", "TEXT"),
            ("skillcreditminusage", "TEXT"),
            ("startdate", "TIMESTAMP"),
            ("enddate", "TIMESTAMP"),
            ("rolestatus", "TEXT"),
            ("sourceroleevent", "TEXT"),
            ("commondemandid", "TEXT"),
            ("peoplekey", "TEXT"),
            ("sourcesystemid", "DECIMAL"),
            ("latestind", "BOOLEAN"),
            ("isdeleted", "BOOLEAN"),
            ("createdttm", "TIMESTAMP"),
            ("updatedttm", "TIMESTAMP"),
            ("rolechangeeventstoreid", "DECIMAL"),
            ("workexperienceid", "DECIMAL"),
            ("psrskillid", "DECIMAL")
    ]

class PersonAssessmentLoad(GZCSVLoadMixin):
    FILE_NAME = 'personassessment.csv.gz'
    TABLE_NAME = 'talhub.personassessment'
    base_path = DataSourceConfig().talhub_path
    columns = [
        ("personassessmentid", "TEXT"),
        ("peoplekey", "TEXT"),
        ("assessedskillid", "DECIMAL"),
        ("psrskillid", "DECIMAL"),
        ("proficiencycd", "DECIMAL"),
        ("proficiencyscalecd", "DECIMAL"),
        ("skillid", "DECIMAL"),
        ("compoundskillid", "DECIMAL"),
        ("skillcollectionid", "DECIMAL"),
        ("assessmentstatusid", "DECIMAL"),
        ("assessmenttypeid", "DECIMAL"),
        ("assessmentmethodid", "DECIMAL"),
        ("sourcesystemid", "DECIMAL"),
        ("validationstatusid", "DECIMAL"),
        ("sourcesystemassessmentmethodidentifierid", "DECIMAL"),
        ("completiondttm", "TIMESTAMP"),
        ("assessorpeoplekey", "TEXT"),
        ("latestind", "BOOLEAN"),
        ("sourcetransactionid", "DECIMAL"),
        ("experienceinmonths", "DECIMAL"),
        ("lastusedyear", "DECIMAL"),
        ("deletedind", "BOOLEAN"),
        ("createdttm", "TIMESTAMP"),
        ("updatedttm", "TIMESTAMP"),
        ("levelofinterestid", "DECIMAL"),
        ("duration", "DECIMAL")
    ]

class AssessmentStatusLoad(GZCSVLoadMixin):
    FILE_NAME = 'assessmentstatus.csv.gz'
    TABLE_NAME = 'talhub.assessmentstatus'
    base_path = DataSourceConfig().talhub_path
    columns = [
        ("assessmentstatusid", "DECIMAL"),
        ("assessmentstatusnm", "TEXT"),
        ("createdttm", "TIMESTAMP"),
        ("updatedttm", "TIMESTAMP")
    ]

class AssessmentMethodLoad(GZCSVLoadMixin):
    FILE_NAME = 'assessmentmethod.csv.gz'
    TABLE_NAME = 'talhub.assessmentmethod'
    base_path = DataSourceConfig().talhub_path
    columns = [
        ("assessmentmethodid", "DECIMAL"),
        ("assessmentmethodnm", "TEXT"),
        ("createdttm", "TIMESTAMP"),
        ("updatedttm", "TIMESTAMP")
    ]

class AssessmentTypeLoad(GZCSVLoadMixin):
    FILE_NAME = 'assessmenttype.csv.gz'
    TABLE_NAME = 'talhub.assessmenttype'
    base_path = DataSourceConfig().talhub_path
    columns = [
        ("assessmenttypeid", "DECIMAL"),
        ("assessmenttypenm", "TEXT"),
        ("createdttm", "TIMESTAMP"),
        ("updatedttm", "TIMESTAMP")

    ]

class ProficiencyLoad(GZCSVLoadMixin):
    FILE_NAME = 'proficiency.csv.gz'
    TABLE_NAME = 'talhub.proficiency'
    base_path = DataSourceConfig().talhub_path
    columns = [
        ("proficiencycd", "DECIMAL"),
        ("proficiencydesc", "TEXT"),
        ("proficiencyscalecd", "INT"),
        ("effectivestartdttm", "TIMESTAMP"),
        ("effectiveenddttm", "TIMESTAMP"),
        ("createdttm", "TIMESTAMP"),
        ("updatedttm", "TIMESTAMP")
    ]

class ProficiencyScaleLoad(GZCSVLoadMixin):
    FILE_NAME = 'proficiencyscale.csv.gz'
    TABLE_NAME = 'talhub.proficiencyscale'
    base_path = DataSourceConfig().talhub_path
    columns = [
            ("proficiencyscalecd", "TEXT"),
            ("proficiencyscaledesc", "TEXT"),
            ("createdttm", "TIMESTAMP"),
            ("updatedttm", "TIMESTAMP")
    ]

class PSRSkillTypeLoad(GZCSVLoadMixin):
    FILE_NAME = 'psrskilltype.csv.gz'
    TABLE_NAME = 'talhub.psrskilltype'
    base_path = DataSourceConfig().talhub_path
    columns = [
        ("psrskilltypecd", "DECIMAL"),
        ("psrskilltypenm", "TEXT"),
        ("createdttm", "TIMESTAMP"),
        ("updatedttm", "TIMESTAMP")
    ]

class SpecialtySkillClassificationLoad(GZCSVLoadMixin):
    FILE_NAME = 'specialtyskillclassification.csv.gz'
    TABLE_NAME = 'talhub.specialtyskillclassification'
    base_path = DataSourceConfig().talhub_path
    columns = [
        ("specialtyskillclassificationid", "DECIMAL"),
        ("specialtyskillclassificationnm", "TEXT"),
        ("specialtyskillclassificationdesc", "TEXT"),
        ("createdttm", "TIMESTAMP"),
        ("updatedttm", "TIMESTAMP")
    ]

class ValidationStatusLoad(GZCSVLoadMixin):
    FILE_NAME = 'validationstatus.csv.gz'
    TABLE_NAME = 'talhub.validationstatus'
    base_path = DataSourceConfig().talhub_path
    columns = [
        ("validationstatusid", "DECIMAL"),
        ("validationstatusnm", "TEXT"),
        ("createdttm", "TIMESTAMP"),
        ("updatedttm", "TIMESTAMP")
    ]

class LevelOfInterestLoad(GZCSVLoadMixin):
    FILE_NAME = 'levelofinterest.csv.gz'
    TABLE_NAME = 'talhub.levelofinterest'
    base_path = DataSourceConfig().talhub_path
    columns = [
        ("levelofinterestid", "DECIMAL"),
        ("levelofinterestnm", "TEXT"),
        ("createdttm", "TIMESTAMP"),
        ("updatedttm", "TIMESTAMP")
    ]

class PersonSpecialtyLoad(GZCSVLoadMixin):
    FILE_NAME = 'personspecialty.csv.gz'
    TABLE_NAME = 'talhub.personspecialty'
    base_path = DataSourceConfig().talhub_path
    columns = [
        ("personspecialtyid", "TEXT"),
        ("peoplekey", "TEXT"),
        ("psrskillid", "DECIMAL"),
        ("specialtyskillid", "DECIMAL"),
        ("specialtyskillclassificationid", "DECIMAL"),
        ("specialtystatusid", "DECIMAL"),
        ("validationstatusid", "DECIMAL"),
        ("skillid", "DECIMAL"),
        ("compoundskillid", "DECIMAL"),
        ("skillcollectionid", "DECIMAL"),
        ("approverpeoplekey", "TEXT"),
        ("addeddttm", "TIMESTAMP"),
        ("removeddttm", "TIMESTAMP"),
        ("latestind", "BOOLEAN"),
        ("deletedind", "BOOLEAN"),
        ("sourcesystemid", "DECIMAL"),
        ("createdttm", "TIMESTAMP"),
        ("updatedttm", "TIMESTAMP"),
        ("averagepscore", "DECIMAL"),
        ("percentmatch", "DECIMAL"),
    ]

class SourceSystemLoad(GZCSVLoadMixin):
    FILE_NAME = 'sourcesystem.csv.gz'
    TABLE_NAME = 'talhub.sourcesystem'
    base_path = DataSourceConfig().talhub_path
    columns = [
        ("sourcesystemid", "DECIMAL"),
        ("sourcesystemnm", "TEXT"),
        ("createdttm", "TIMESTAMP"),
        ("updatedttm", "TIMESTAMP")
    ]

class SpecialtyStatusLoad(GZCSVLoadMixin):
    FILE_NAME = 'specialtystatus.csv.gz'
    TABLE_NAME = 'talhub.specialtystatus'
    base_path = DataSourceConfig().talhub_path
    columns = [
        ("specialtystatusid", "DECIMAL"),
        ("statusnm", "TEXT"),
        ("createdttm", "TIMESTAMP"),
        ("updatedttm", "TIMESTAMP")

    ]

class SpecialiaztionsLoad(GZCSVLoadMixin):
    FILE_NAME = 'Specializations_11_14_18.csv'
    TABLE_NAME = 'talhub.specializations'
    base_path = DataSourceConfig().talhub_path
    columns = [
        ("specializationbranch", "TEXT"),
        ("specialization", "TEXT"),
        ("skillgrouping", "TEXT"),
        ("sapskillid", "DECIMAL"),
        ("skillname", "TEXT"),
    ]


import cv2
import cv2 as cv

imagename="C:\\Users\\admin\Desktop\\2.png"
image = cv2.imread(imagename, 0)
blood = cv2.normalize(image.astype('double'), None, 0.0, 1.0, cv2.NORM_MINMAX)  # Convert to normalized floating point
# 用normalize函数导致图像中的像素为浮点型，导致后面用HoughLinesP函数时出错？
outIm = FrangiFilter2D(blood)
edges = outIm * (10000)
# edges=outIm*(10000).astype(np.int8)  # 这样也不行

cv2.imshow('Frangi Filter Result', edges)

minLineLength = 200
maxLineGap = 50
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 80, minLineLength, maxLineGap)


#################################################################################
# WaterTAP Copyright (c) 2020-2023, The Regents of the University of California,
# through Lawrence Berkeley National Laboratory, Oak Ridge National Laboratory,
# National Renewable Energy Laboratory, and National Energy Technology
# Laboratory (subject to receipt of any required approvals from the U.S. Dept.
# of Energy). All rights reserved.
#
# Please see the files COPYRIGHT.md and LICENSE.md for full copyright and license
# information, respectively. These files are also available online at the URL
# "https://github.com/watertap-org/watertap/"
#################################################################################
"""
Test of db_api module
"""
import pytest
from ..db_api import ElectrolyteDB
from ..data_model import Component, Reaction, Base
from ..commands import _load_bootstrap
from pymongo import MongoClient
from .util import MockDB

@pytest.fixture
def mockdb():
    return MockDB()

# Test for MongoDB server at default URL
g_mongo_server = True
try:
    conn = MongoClient(
        ElectrolyteDB.DEFAULT_URL,
        serverSelectionTimeoutMS=1000,
    )
    conn.server_info()
except Exception as err:
    print(f"Cannot connect to MongoDB: {err}")
    g_mongo_server = False

requires_mongo = pytest.mark.skipif(
    g_mongo_server is False,
    reason=f"Cannot connect to MongoDB server at {ElectrolyteDB.DEFAULT_URL}",
)

@pytest.fixture(scope="module")
def edb():
    return ElectrolyteDB()

@pytest.mark.component
@requires_mongo
def test_edb_init(edb):
    assert edb is not None
    assert edb.connect_status_str == "Connection succeeded"
    assert type(edb.connect_status) is dict

@pytest.mark.component
@requires_mongo
def test_edb_load(edb):
    # Load bootstrap for temporary testing purposes
    _load_bootstrap(edb)
    base = edb.get_base("default_thermo")
    assert type(base) is Base
    edb.list_bases()

@pytest.mark.component
@requires_mongo
def test_edb_get_components(edb):
    res_obj_comps = edb.get_components(element_names=["H", "O"])
    for comp_obj in res_obj_comps:
        assert type(comp_obj) is Component

    # Just test the _process_species function
    assert edb._process_species("H2O") == "H2O"
    assert edb._process_species("H_+") == "H"
    assert edb._process_species("OH_-") == "OH"

    # Drop the bootstrap database for cleaning
    edb.drop_database(edb.DEFAULT_URL, edb.DEFAULT_DB)
    assert edb.is_empty()

@pytest.mark.unit
def test_edb_load_convention():
    # make sure the convention of collection names mapping to data_model objects is true, since
    # we rely on it in the ElectrolyteDB.load() method
    for wrapper_class in (Component, Reaction, Base):
        assert wrapper_class.__name__.lower() in ElectrolyteDB._known_collections

def insert_reactions(collection, data):
    for obj in data:
        collection.insert_one(obj)

# Data for get_reactions tests

data1 = [
    {
        "stoichiometry": {"Liq": {"H2O": -1, "CO2": -1, "H2CO3": 1}},
        "heat_of_reaction": "constant_dh_rxn",
        "equilibrium_constant": "van_t_hoff",
        "equilibrium_form": "log_power_law",
        "concentration_form": "ConcentrationForm.molarity",
        "parameter_data": {
            "dh_rxn_ref": [{"v": 0, "u": "kJ/mol", "i": 0}],
            "k_eq_ref": [{"v": 0.0017, "u": "m**3/mol", "i": 0}],
            "T_eq_ref": [{"v": 300, "u": "K", "i": 0}],
        },
        "type": "equilibrium",
        "name": "CO2_to_H2CO3",
        "components": ["CO2", "H2CO3"],
        "reactant_elements": ["C", "O", "H"],
    },
    {
        "stoichiometry": {"Liq": {"H2O": -1, "H_+": 1, "OH_-": 1}},
        "heat_of_reaction": "constant_dh_rxn",
        "equilibrium_constant": "van_t_hoff_aqueous",
        "equilibrium_form": "log_power_law",
        "concentration_form": "ConcentrationForm.molarity",
        "parameter_data": {
            "dh_rxn_ref": [{"v": 55.83, "u": "kJ/mol", "i": 0}],
            "ds_rxn_ref": [{"v": -80.7, "u": "J/mol/K", "i": 0}],
        },
        "type": "equilibrium",
        "name": "H2O_Kw",
        "components": ["H2O", "Kw"],
        "reactant_elements": ["O", "H"],
    },
]

# add one more record to data1
data2 = data1.copy() + [
    {
        "stoichiometry": {"Liq": {"H2CO3": -1, "H_+": 1, "HCO3_-": 1}},
        "heat_of_reaction": "constant_dh_rxn",
        "equilibrium_constant": "van_t_hoff_aqueous",
        "equilibrium_form": "log_power_law",
        "concentration_form": "ConcentrationForm.molarity",
        "parameter_data": {
            "dh_rxn_ref": [{"v": 7.7, "u": "kJ/mol", "i": 0}],
            "ds_rxn_ref": [{"v": -95.8, "u": "J/mol/K", "i": 0}],
        },
        "type": "equilibrium",
        "name": "H2CO3_Ka1",
        "components": ["H2CO3", "Ka1"],
        "reactant_elements": ["C", "O", "H"],
    }
]

# The way to read this parameterized test is:
#   With these 'components' and this 'data' in the DB,
#   getting reactions with *any* component should return 'any_num' records,
#   and getting reactions with *all* components should return 'all_num' records
#   and getting reactions with *all* components and *new* components should
#   return 'new_num' records
@pytest.mark.unit
@pytest.mark.parametrize(
    "components,data,any_num,all_num,new_num",
    [
        (["H2O", "CO2", "H2CO3"], data1, 2, 1, 2),
        (["H2O", "H +", "OH -", "H2CO3", "HCO3 -"], data2, 3, 2, 3),
        (["H2CO3"], data2, 2, 0, 2),
    ],
)
def test_get_reactions(mockdb, components, data, any_num, all_num, new_num):
    insert_reactions(mockdb._db.reaction, data)
    reactions = mockdb.get_reactions(components, any_components=True)
    assert len(list(reactions)) == any_num
    reactions = mockdb.get_reactions(components, any_components=False)
    assert len(list(reactions)) == all_num
    reactions = mockdb.get_reactions(
        components, any_components=False, include_new_components=True
    )
    assert len(list(reactions)) == new_num


# -*- coding: utf-8 -*-
from pyramid.security import Allow
from schematics.exceptions import ValidationError
from schematics.transforms import blacklist, whitelist
from schematics.types import StringType, BaseType, EmailType, BooleanType
from schematics.types.compound import ModelType
from schematics.types.serializable import serializable

from openprocurement.api.auth import ACCR_5
from openprocurement.api.constants import DK_CODES
from openprocurement.api.models import (
    Document,
    ListType,
    Classification as BaseClassification,
    PeriodEndRequired as BasePeriodEndRequired,
    Identifier as BaseIdentifier,
    Address as BaseAddress,
    ContactPoint as BaseContactPoint,
    schematics_embedded_role,
    schematics_default_role,
)
from openprocurement.api.models import Model
from openprocurement.framework.core.models import (
    Framework,
    Submission as BaseSubmission,
    Qualification as BaseQualification,
)
from openprocurement.framework.electroniccatalogue.utils import (
    AUTHORIZED_CPB,
    get_framework_unsuccessful_status_check_date,
)

class DKClassification(BaseClassification):
    scheme = StringType(required=True, choices=[u"ДК021"])
    id = StringType(required=True)

    def validate_id(self, data, id):
        if id not in DK_CODES:
            raise ValidationError(BaseType.MESSAGES["choices"].format(unicode(DK_CODES)))

class Identifier(BaseIdentifier):
    legalName = StringType(required=True)

class Address(BaseAddress):
    streetAddress = StringType(required=True)
    locality = StringType(required=True)
    region = StringType(required=True)
    postalCode = StringType(required=True)

class ContactPoint(BaseContactPoint):
    email = EmailType(required=True)
    telephone = StringType(required=True)

class CentralProcuringEntity(Model):
    class Options:
        roles = {
            "embedded": schematics_embedded_role,
            "view": schematics_default_role,
            "edit_draft": schematics_default_role,
            "edit_active": whitelist("contactPoint"),
        }
    name = StringType(required=True)
    name_en = StringType()
    name_ru = StringType()
    identifier = ModelType(Identifier, required=True)
    additionalIdentifiers = ListType(ModelType(Identifier))
    address = ModelType(Address, required=True)
    contactPoint = ModelType(ContactPoint, required=True)
    kind = StringType(choices=["central"], default="central")

    def validate_identifier(self, data, identifier):
        id_ = identifier.id
        cpb_with_statuses = {cpb["identifier"]["id"]: cpb["active"] for cpb in AUTHORIZED_CPB}
        if id_ not in cpb_with_statuses or not cpb_with_statuses[id_]:
            raise ValidationError("Can't create framework for inactive cpb")

class ElectronicCatalogueFramework(Framework):
    class Options:
        namespace = "Framework"
        _status_view_role = blacklist(
            "doc_type",
            "successful",
            "transfer_token",
            "owner_token",
            "revisions",
            "_id",
            "_rev",
            "__parent__",
        )
        _edit_role = _status_view_role + blacklist(
            "frameworkType",
            "prettyID",
            "period",
            "enquiryPeriod",
            "dateModified",
            "date",
            "doc_id"
        )
        _create_role = _edit_role + blacklist("status")

        roles = {
            "create": _create_role,
            "edit_draft": _edit_role + blacklist("owner", "old_date"),
            "edit_active": whitelist(
                "status",
                "procuringEntity",
                "qualificationPeriod",
                "description",
                "description_en",
                "description_ru",
                "documents",
                "frameworkDetails"
            ),
            "draft": _status_view_role,
            "active": _status_view_role,
            "complete": _status_view_role,
            "unsuccessful": _status_view_role,
            "view": _edit_role + whitelist(
                "date",
                "period",
                "enquiryPeriod",
                "prettyID",
                "documents",
                "doc_id",
                "dateModified",
                "status",
                "owner",
                "next_check",
            ),
            "chronograph": whitelist("next_check"),
            "chronograph_view": _status_view_role,
            "Administrator": whitelist("status", "mode"),
            "default": blacklist("doc_id", "__parent__"),  # obj.store() use default role
            "plain": blacklist(  # is used for getting patches
                "_attachments", "revisions", "dateModified", "_id", "_rev", "doc_type",
                "__parent__"
            ),
            "listing": whitelist("dateModified", "doc_id"),
            "embedded": blacklist("_id", "_rev", "doc_type", "__parent__"),
        }

    status = StringType(
        choices=[
            "draft",
            "active",
            "deleted",
            "complete",
            "unsuccessful",
        ],
        default="draft",
    )
    period = ModelType(BasePeriodEndRequired)
    qualificationPeriod = ModelType(BasePeriodEndRequired, required=True)
    enquiryPeriod = ModelType(BasePeriodEndRequired)
    frameworkType = StringType(default="electronicCatalogue")
    procuringEntity = ModelType(CentralProcuringEntity, required=True)
    classification = ModelType(DKClassification, required=True)
    additionalClassifications = ListType(ModelType(BaseClassification))
    documents = ListType(ModelType(Document, required=True), default=list())

    successful = BooleanType(required=True, default=False)

    procuring_entity_kinds = ["central"]
    central_accreditations = (ACCR_5,)
    edit_accreditations = (ACCR_5,)

    @serializable(serialize_when_none=False)
    def next_check(self):
        checks = []
        if self.status == "active":
            if not self.successful:
                unsuccessful_status_check = get_framework_unsuccessful_status_check_date(self)
                if unsuccessful_status_check:
                    checks.append(unsuccessful_status_check)
            checks.append(self.qualificationPeriod.endDate)
        return min(checks).isoformat() if checks else None

    def __acl__(self):
        acl = super(ElectronicCatalogueFramework, self).__acl__()
        acl.append((Allow, "{}_{}".format(self.owner, self.owner_token), "upload_framework_documents"))
        return acl

class Submission(BaseSubmission):

    status = StringType(
        choices=[
            "draft",
            "active",
            "deleted",
            "complete"
        ],
        default="draft",
    )
    submissionType = StringType(default="electronicCatalogue")

class Qualification(BaseQualification):

    status = StringType(
        choices=[
            "pending",
            "active",
            "unsuccessful"
        ],
        default="pending",
    )

    qualificationType = StringType(default="electronicCatalogue", required=True)


import os
import json

import requests

GOOGLE_API = 'https://www.googleapis.com/urlshortener/v1/url'
API_KEY = os.environ['api_key']
google_url = '{}?key={}'.format(GOOGLE_API, API_KEY)

def handler(event, context):

    print(json.dumps(event))

    payload = {'longUrl': event['url']}
    r = requests.post(google_url, data=json.dumps(payload), headers={'content-type': 'application/json'})
    result = r.json()
    print(json.dumps(result))

    return result


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

"""Load dataset for the CTC model (ERATO corpus).
   In addition, frame stacking and skipping are used.
   You can use only the single GPU version.
"""

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

from os.path import join, isfile
import pickle
import numpy as np

from utils.dataset.ctc import DatasetBase

class Dataset(DatasetBase):

    def __init__(self, data_type, label_type, ss_type, batch_size,
                 max_epoch=None, splice=1,
                 num_stack=1, num_skip=1,
                 shuffle=False, sort_utt=False, sort_stop_epoch=None,
                 progressbar=False):
        """A class for loading dataset.
        Args:
            data_type (string): train or dev or test
            label_type (string): kana
            ss_type (string): remove or insert_left or insert_both or insert_right
            batch_size (int): the size of mini-batch
            max_epoch (int, optional): the max epoch. None means infinite loop.
            splice (int, optional): frames to splice. Default is 1 frame.
            num_stack (int, optional): the number of frames to stack
            num_skip (int, optional): the number of frames to skip
            shuffle (bool, optional): if True, shuffle utterances. This is
                disabled when sort_utt is True.
            sort_utt (bool, optional): if True, sort all utterances by the
                number of frames and utteraces in each mini-batch are shuffled.
                Otherwise, shuffle utteraces.
            sort_stop_epoch (int, optional): After sort_stop_epoch, training
                will revert back to a random order
            progressbar (bool, optional): if True, visualize progressbar
        """
        super(Dataset, self).__init__()

        self.is_test = True if data_type == 'test' else False

        self.data_type = data_type
        self.label_type = label_type
        self.ss_type = ss_type
        self.batch_size = batch_size
        self.max_epoch = max_epoch
        self.splice = splice
        self.num_stack = num_stack
        self.num_skip = num_skip
        self.shuffle = shuffle
        self.sort_utt = sort_utt
        self.sort_stop_epoch = sort_stop_epoch
        self.progressbar = progressbar
        self.num_gpu = 1

        # paths where datasets exist
        dataset_root = ['/data/inaguma/erato',
                        '/n/sd8/inaguma/corpus/erato/dataset']

        input_path = join(dataset_root[0], 'inputs', data_type)
        # NOTE: ex.) save_path:
        # erato_dataset_path/inputs/data_type/speaker/***.npy
        label_path = join(dataset_root[0], 'labels',
                          ss_type, data_type, label_type)
        # NOTE: ex.) save_path:
        # erato_dataset_path/labels/ss_type/data_type/kana/speaker/***.npy

        # Load the frame number dictionary
        if isfile(join(input_path, 'frame_num.pickle')):
            with open(join(input_path, 'frame_num.pickle'), 'rb') as f:
                self.frame_num_dict = pickle.load(f)
        else:
            dataset_root.pop(0)
            input_path = join(dataset_root[0], 'inputs', data_type)
            label_path = join(dataset_root[0], 'labels',
                              ss_type, data_type, label_type)
            with open(join(input_path, 'frame_num.pickle'), 'rb') as f:
                self.frame_num_dict = pickle.load(f)

        # Sort paths to input & label
        axis = 1 if sort_utt else 0
        frame_num_tuple_sorted = sorted(self.frame_num_dict.items(),
                                        key=lambda x: x[axis])
        input_paths, label_paths = [], []
        for input_name, frame_num in frame_num_tuple_sorted:
            speaker = '_'.join(input_name.split('_')[:-1])
            input_paths.append(join(input_path, speaker, input_name + '.npy'))
            label_paths.append(join(label_path, speaker, input_name + '.npy'))
        self.input_paths = np.array(input_paths)
        self.label_paths = np.array(label_paths)
        # NOTE: Not load dataset yet

        self.rest = set(range(0, len(self.input_paths), 1))