#!/bin/env python3
#-*-coding:utf-8-*-
import requests
import datetime
import json
import pytz
import sys
from icalendar import Calendar, Event

# 在此处输入获取到的cookie
cookie = ""
# 在此处输入文件的路径
filename = ""

headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6",
"Connection": "keep-alive",
"Content-Length": "14",
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
"Cookie": "",
"Host": "jxgl.sau.edu.cn",
"Origin": "https://jxgl.sau.edu.cn",
"Referer": "https://jxgl.sau.edu.cn/jwglxt/kbcx/xskbcx_cxXskbcxIndex.html?gnmkdm=N2151&layout=default&su=183405010224",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36",
"X-Requested-With": "XMLHttpRequest"
}

url = "https://jxgl.sau.edu.cn/jwglxt/kbcx/xskbcx_cxXsKb.html?gnmkdm=N2151"

# 根据课程的节数返回上课的时间
def GetCourseTime(time):
    course_time_dict = {
    '1-2': [[8, 20, 0], [10, 0, 0]],
    '3-4': [[10, 20, 0], [12, 0, 0]],
    '1-4': [[8, 20, 0], [12, 0, 0]],
    '5-6': [[13, 30, 0], [15, 10, 0]],
    '7-8': [[15, 30, 0], [17, 10, 0]],
    '5-8': [[13, 30, 0], [17, 10, 0]],
    '9-10': [[18, 10, 0], [19, 50, 0]],
    '7-10': [[15, 30, 0], [19, 50, 0]]
    }
    return course_time_dict[time][0], course_time_dict[time][1]

# 抓取课表信息并转换为字典
def GetCourseInfo_dict(data):
    headers["Cookie"] = cookie
    request = requests.post(url, headers=headers, data=data)
    if request.status_code != 200:
        print("RESPONSE", request.status_code)
        print("ERROR! Check the cookie you input")
        exit(0)
    a = request.text
    b = json.loads(a)
    return b

#从文件中导入课表数据
def GetCourseInfoFromFile(filename):
    try:
        file = open(filename, mode='r')
    except:
        print("Error: Unable to open file: ", filename)
        exit(1)
    a = file.read()
    b = json.loads(a)
    file.close()
    print("Successfully read file: ", filename)
    return b

# 返回上课的周的区间
def GetCourseTakeWeeks(week_num):
    if '-' in week_num: # A-B周区间内上课
        weekstart = int(week_num[0:week_num.find('-')])
        weekend = int(week_num[week_num.find('-')+1:week_num.find('周')])
    else: # 仅A周上课
        weekstart = weekend = int(week_num[0:week_num.find('周')])
    return weekstart, weekend

# 根据学期和周数返回这节课所在的天数
def GetCourseDate(year, semester, week, course_in_day_of_week):
    if semester == 1:
        start = datetime.datetime(year, 8, 31, 0) #2019.9.1.00
    else:
        start = datetime.datetime(year, 3, 1, 0) #2019.3.1.00
    for i in range(7):
        if start.weekday() == 0: # if the start day is monday
            break
        else:
            start = start + datetime.timedelta(days=1)
    time = start + datetime.timedelta(days=(week-1) * 7 + (course_in_day_of_week-1))
    print("{}.{}.{}".format(time.year, time.month, time.day), "星期", time.weekday()+1)
    return time

# 创建事件
def CreateEvent(calendar, course_weeks_start, course_weeks_end, year, semester, a):
    time_zone = pytz.timezone('Asia/Shanghai')
    course_in_day_of_week = int(a['xqj']) # 星期几
    course_begin_time, course_end_time = GetCourseTime(a['jcor'])   # 根据课程的节数返回上课的时间
    print("\n课程名称: ", a['kcmc'], "星期:", a['xqj'])
    print("该课程由第{}周上到第{}周".format(course_weeks_start, course_weeks_end))
    for week in range(course_weeks_start, course_weeks_end+1):
        print("第{}周:".format(week))
        course_date = GetCourseDate(year, semester, week, course_in_day_of_week)
        event = Event()
        event.add('summary', a['kcmc'])
        event.add('dtstart',datetime.datetime(course_date.year,
            course_date.month, course_date.day, course_begin_time[0], course_begin_time[1], course_begin_time[2], tzinfo=time_zone))
        event.add('dtend', datetime.datetime(course_date.year,
            course_date.month, course_date.day, course_end_time[0],course_end_time[1], course_end_time[2], tzinfo=time_zone))
        event.add('dtstamp', datetime.datetime(course_date.year,
            course_date.month, course_date.day, course_begin_time[0], course_begin_time[1], course_begin_time[2], tzinfo=time_zone))
        event.add('description', "周数:{},教师:{}".format(a['zcd'], a['xm']))
        event.add('location', a['cdmc'])
        calendar.add_component(event)
    return calendar

def ConvertCalendar(course_dict):
    calendar = Calendar()
    calendar.add('prodid', '-//My calendar product//mxm.dk//')
    calendar.add('version', '2.0')
    semester = int(course_dict['xsxx']['XQMMC'])
    # year = int(course_dict['xsxx']['XNM'])
    yearname = course_dict['xsxx']['XNMC'].split('-')
    if semester == 2:
        year = int(yearname[1])
    else:
        year = int(yearname[0])
    for num in course_dict['kbList']:
        a = num
        week_string_list = a['zcd'].split(',')
        for week_string in week_string_list:
            course_weeks_start, course_weeks_end = GetCourseTakeWeeks(week_string)
            calendar = CreateEvent(calendar, course_weeks_start, course_weeks_end,  year, semester, a)

    output_file_name = '{}:{}-{}.ics'.format(course_dict['xsxx']['XH'], course_dict['xsxx']['XNMC'], course_dict['xsxx']['XQMMC'])
    output_file = open(output_file_name, 'wb')
    output_file.write(calendar.to_ical())
    output_file.close()
    print('Successfully write your calendar to', output_file_name)

def ShowHelp():
    print("Usage:\npython3 ./sau_class_schedule_to_ics.py [option]")
    print("\nOptions:")
    print("     -c, --cookie [COOKIE]\tUse cookie get course data. (From internet)")
    print("     -f, --file [Directory]\tImport course data from file.")
    print("     -v, --verstion\t\tShow version.")
    print("     -h, --help\t\t\tShow this help.")

def main():
    global cookie, filename
    now = datetime.datetime.now()
    year = now.year
    if now.month <= 1 or now.month >= 9:
        semester = "3"
        data = {"xnm": "{}".format(now.year), "xqm": semester}

    else:
        semester = "12"
        data = {"xnm": "{}".format(now.year-1), "xqm": semester}

    if len(sys.argv) == 1:
        course_dict = GetCourseInfo_dict(data)
        ConvertCalendar(course_dict)
        exit(0)

    for i in range(1, len(sys.argv)):
        if sys.argv[i] == '-f' or sys.argv[i] == '--file':
            if i+1 < len(sys.argv):
                filename = sys.argv[i+1]
            elif filename == "":
                ShowHelp()
                exit(1)

            course_dict = GetCourseInfoFromFile(filename)
            ConvertCalendar(course_dict)
            exit(0)

        elif sys.argv[i] == '-v' or sys.argv[i] == '--version':
            print("Version: 1.4 - debug - 2020.3.24")
            print("by: STARRY-S")
            exit(0)

        elif sys.argv[i] == '-h' or sys.argv[i] == '--help':
            ShowHelp()
            exit(0)

        elif sys.argv[i] == '-c' or sys.argv[i] == '--cookie':
            if i+1 < len(sys.argv):
                cookie = sys.argv[i+1]
            elif cookie == "":
                ShowHelp()
                exit(1)
            course_dict = GetCourseInfo_dict(data)
            ConvertCalendar(course_dict)
            exit(0)

    ShowHelp()

if __name__ == '__main__':
    main()


from A3.character import create_character
from A3.monster import *
from A3.story import *
import random

def roll_die(number_of_rolls: int, number_of_sides: int) -> int:
    """
    Roll a die.

    Rolls a die with a given number of sides a given number of times.

    :param number_of_rolls: an int
    :param number_of_sides: an int
    :precondition: number_of_rolls is an int >=1
    :precondition: number_of_sides is an int >=1
    :postcondition: randomly determines the value of each roll
    :return: the sum of all rolls as an int
    """
    if number_of_rolls < 1 or number_of_sides < 1:
        return 0
    else:
        die = range(1, number_of_sides + 1)
        return sum(random.choices(die, k=number_of_rolls))

def loot(character: dict, monster: dict):
    """
    Check if the character loots the Ashbringer.

    :param character: a dict
    :param monster: a dict
    :precondition: character is a character dictionary
    :precondition: monster is a monster dictionary
    :postcondition: checks if the monster has the Ashbinger and the character doesn't
    :postcondition: if so, adds the Ashbringer to the character
    """
    if monster['Ashbringer'] and not character['Ashbringer']:
        character['Ashbringer'] = True
        print(f'As {monster["Name"].title()} collapses, a flash of light blinds you through the storm.\nThere, laying '
              f'on the icy floor, is the fabled Ashbringer.\nAs you pick up the sword, you feel light energy coursing '
              f'through your body.\nYou are finally ready.\n')

def run(character: dict, monster: dict):
    """
    Run away from a monster.

    :param character: a dict
    :param monster: a dict
    :precondition: character is a character dictionary
    :precondition: monster is a monster dictionary
    :postcondition: checks for a 1/10 chance of the character being struck as they flee
    :postcondition: if the character is struck, calculates and applies the damage to their HP
    """
    if roll_die(1, 10) == 10:
        damage = roll_die(1, 4)
        print(f'The {monster["Name"].title()} strikes you for {damage} damage as you flee!')
        character['HP'] -= damage
    else:
        print('You manage to escape back into the storm!')

def hit_check(current_attacker: dict) -> bool:
    """
    Check if an attack hits.

    :param current_attacker: a dict
    :precondition: current_attacker is a character or monster dictionary
    :postcondition: checks for a 1/6 chance of missing
    :return: True if the attack will hit, otherwise False and prints a message
    """
    if roll_die(1, 6) != 1:
        return True
    else:
        print(f'{current_attacker["Name"].title()} misses!')
        return False

def attack(attacker: dict, defender: dict):
    """
    Execute a round of attacks.

    :param attacker: a dict
    :param defender: a dict
    :precondition: attacker is a character or monster dictionary
    :precondition: defender is a character or monster dictionary
    :postcondition: checks if the attacker hits
    :postcondition: calculates and applies the attacker's damage to the defender's HP
    :postcondition: if the defender survives, checks if their retaliation hits
    :postcondition: calculates and applies the defender's damage to the attacker's HP
    """
    if hit_check(attacker):
        damage_1 = roll_die(1, 6)
        attack_1 = random.choice(attacker['Attacks'])
        print(f'{attacker["Name"].title()} {attack_1} {defender["Name"].title()} for {damage_1} damage!')
        defender['HP'] -= damage_1

    if defender['HP'] > 0:
        if hit_check(defender):
            damage_2 = roll_die(1, 6)
            attack_2 = random.choice(defender['Attacks'])
            print(f'{defender["Name"].title()} {attack_2} {attacker["Name"].title()} for {damage_2} damage!')
            attacker['HP'] -= damage_2

def attack_round_one(character: dict, monster: dict):
    """
    Begin the first round of combat.

    :param character: a dict
    :param monster: a dict
    :precondition: character is a character dictionary
    :precondition: monster is a monster dictionary
    :postcondition: adds "the" to the monster's name for formatting
    :postcondition: checks for a 50/50 chance of who attacks first and prints their name
    :postcondition: execute the first round of combat
    """
    monster['Name'] = 'the ' + monster['Name']

    if roll_die(1, 2) == 1:
        print(f'\nYou strike before {monster["Name"].title()} can react!')
        attack(character, monster)
    else:
        print(f'\n{monster["Name"].title()} surprises you out of the storm!')
        attack(monster, character)

def combat(character: dict, monster: dict):
    """
    Engage in combat with a monster.

    :param character: a dict
    :param monster: a dict
    :precondition: character is a character dictionary
    :precondition: monster is a monster dictionary
    :postcondition: execute the first round of combat
    :postcondition: continues to execute rounds of combat until either party dies
    :postcondition: if the monster dies, checks if it was the boss, and if so prints the story and ends the game
    :postcondition: if the monster was not the boss, checks for loot
    """
    attack_round_one(character, monster)  # Handles the first round of combat separately to print who strikes first

    while character['HP'] > 0 and monster['HP'] > 0:  # Continually runs rounds of attacks as long as both are alive
        if roll_die(1, 2) == 1:
            attack(character, monster)
        else:
            attack(monster, character)

    if character['HP'] < 1:  # If the character dies
        if monster['Name'] == 'the lich king':
            story_boss_defeat()
        print('You die.')
        return
    else:  # If the monster dies
        if monster['Name'] == 'the lich king':
            story_boss_victory()
            character['HP'] = 0  # Sets the character's HP to 0 to end the game
            return
        else:
            print(f'{monster["Name"].title()} dies.\n')
            loot(character, monster)

def boss_fight(character: dict):
    """
    Initiate the boss fight.

    :param character: a dict
    :precondition: character is a character dictionary
    :postcondition: checks if the character has the Ashbringer
    :postcondition: prints encounter story
    :postcondition: begins combat with the boss
    """
    # story
    if character['Ashbringer']:
        story_boss_engage()
        combat(character, create_boss())
    else:  # Beating the boss without Ashbringer is impossible
        story_boss_no_ashbringer()
        story_boss_defeat()
        character['HP'] = 0
    return

def boss_check(character: dict) -> bool:
    """
    Check if the character is at the boss.

    :param character: a dict
    :precondition: character is a character dictionary
    :postcondition: checks if the character is at the boss square and if so begins the encounter
    :postcondition: checks if the character is at the square before the boss and prints a warning
    :return: True if the boss fight occurs, False otherwise
    """
    if character['Location'] == (1, 10):
        boss_fight(character)
        return True
    elif character['Location'] == (1, 9):
        story_approaching_boss()
        return False
    else:
        return False

def heal(character: dict):
    """
    Heal the character 2 HP.

    :param character: a dict
    :precondition: character is a character dictionary
    :postcondition: increases the character's HP by 2 up to a maximum of 10
    """
    if character['HP'] < 10:
        character['HP'] += 2
    if character['HP'] > 10:
        character['HP'] = 10

def encounter(character: dict):
    """
    Check for an encounter.

    :param character: a dict
    :precondition: character is a character dictionary
    :postcondition: checks if the character is at or near the boss
    :postcondition: checks for a 1/4 chance of the character encountering a monster
    :postcondition: if they encounter a monster, prompts them to attack or run and executes their choice
    :postcondition: if they do not, heals the character
    """
    if boss_check(character):  # The game ends after the character encounters the boss
        return

    if roll_die(1, 4) == 4:
        monster = create_monster()
        choice = input(f'You encounter a(n) {monster["Name"].title()}!\nDo you [a]ttack or [r]un?\n').lower()

        while choice != 'a':
            if choice == 'r':
                run(character, monster)
                return
            else:
                choice = input('Do you [a]ttack or [r]un?\n')
        combat(character, monster)

    else:
        story_move(character)
        heal(character)

def navigate(character: dict, direction: str, world_map: list):
    """
    Move the character.

    :param character: a dict
    :param direction: 'n', 'e', 's', 'w'
    :param world_map: a list of tuples
    :precondition: character is a character dictionary
    :precondition: direction is 'n', 'e', 's', or 'w'
    :precondition: world_map is the map as a list of tuples in the form (x, y)
    :postcondition: checks if the character is trying to move outside the map, and prints a warning if they are
    :postcondition: if the move is valid, updates the character's location and checks for an encounter
    """
    if direction == 'n':
        new_location = (character['Location'][0], character['Location'][1] + 1)
    elif direction == 'e':
        new_location = (character['Location'][0] + 1, character['Location'][1])
    elif direction == 's':
        new_location = (character['Location'][0], character['Location'][1] - 1)
    elif direction == 'w':
        new_location = (character['Location'][0] - 1, character['Location'][1])
    else:
        print('Error: Enter a valid direction.')
        return

    if new_location in world_map:
        character['Location'] = new_location
        encounter(character)
    else:
        story_edge_of_map(character, direction)

def generate_map() -> list:
    """
    Generate the world map.

    :postcondition: creates a list of tuples representing the (x, y) coordinates of the map
    :return: the world map as a list of tuples

    >>> generate_map()
    [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), \
(1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), \
(2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (1, 10)]
    """
    world_map = []
    for x in range(3):
        for y in range(10):
            world_map.append((x, y))
    world_map.append((1, 10))
    return world_map

def game():
    """
    Run the game.

    :postcondition: creates the user's character and world map
    :postcondition: prompts the user to select a direction or quit
    :postcondition: as long as the user does not quit, invokes navigate with their chosen direction
    :postcondition: if the character's HP falls below 0, announces their death and ends the game
    """
    character = create_character()
    world_map = generate_map()
    story_initial()
    print('At any point, you may enter [quit] to end the game.\n')
    user_input = input('Move [N]orth, [E]ast, [S]outh, or [W]est.\n').lower()

    while user_input != 'quit':
        navigate(character, user_input, world_map)
        if character['HP'] > 0:
            user_input = input('Move [N]orth, [E]ast, [S]outh, or [W]est.\n').lower()
        else:  # Ends the game
            return

def main():
    """
    Start the game.

    :postcondition: invokes the game function
    """
    game()

if __name__ == '__main__':
    main()


import matplotlib.pyplot as plt

# 均值为0, 方差为1的随机数
import numpy as np
n = 100
x = np.random.normal(0, 1, n)
y = np.random.normal(0, 1, n)
x=len(y)           # 设置x轴，以y轴数组长度为宽度
x=range(x)      # 以0开始的递增序列作为x轴数据

#==============================
#声明一张新的图
plt.figure(figsize=(16,8),dpi=80)  # 画图之前首先设置figure对象，此函数相当于设置一块自定义大小的画布，使得后面的图形输出在这块规定了大小的画布上，其中参数figsize设置画布大小
plt.subplot(221) # 将figure设置的画布大小分成几个部分，参数‘221’表示2(row)x2(colu),即将画布分成2x2，两行两列的4块区域，1表示选择图形输出的区域在第一块，图形输出区域参数必须在“行x列”范围                       ，此处必须在1和2之间选择——如果参数设置为subplot(111)，则表示画布整个输出，不分割成小块区域，图形直接输出在整块画布上
plt.plot(x,y) # 在2x2画布中第一块区域输出图形
plt.subplot(222)
plt.plot(x,y)    #在2x2画布中第二块区域输出图形
plt.subplot(223)  #在2x2画布中第三块区域输出图形
plt.plot(x,y)
plt.subplot(224)  # 在在2x2画布中第四块区域输出图形
plt.plot(x,y)

plt.show()
#==============================


import selenium
from selenium import  webdriver
from selenium.webdriver import TouchActions
import  time

class TestActionTouch:

    def  setup(self):
        opt = webdriver.ChromeOptions()
        opt.add_experimental_option('w3c', False)
        self.driver = webdriver.Chrome(executable_path="/Users/yuanbao/Downloads/chromedriver",chrome_options=opt)
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)

    def teardown(self):
        self.driver.quit()

    def test_touchaction_scrollballbtton(self):
        self.driver.get("https://www.baidu.com/")
        ele_b=self.driver.find_element_by_id("kw")
        ele_search=self.driver.find_element_by_id("su")
        ele_b.send_keys("selenium测试")
        action=TouchActions(self.driver)
        action.tap(ele_search)
        action.perform()
        action.scroll_from_element(ele_b,0,10000).perform()
        time.sleep(10)


import os
import cv2 as cv
import numpy as np

log_path = "/data/Train_Log/N1"
choice_img = ["5", "6", "7", "11", "15"]  # kitti
# choice_img = [str(k) for k in range(23)]
# choice_img = ["1", "6", "12", "14", "16"]  # CS
output_name = "N1_cp"

def get_img(img_dir, img):
    for name in choice_img:
        img_path = os.path.join(img_dir, name + ".png")
        metric_img = cv.imread(img_path)
        h, w, _ = metric_img.shape
        img_h = int(h / 3)
        if not metric_img is None:
            print("-->find {}.png".format(name))
            valid_img = metric_img[img_h: 2*img_h, ...]
            img.append(valid_img)

img =  []

get_img(log_path, img)
output = np.vstack(img)
output_name = os.path.join(log_path, output_name + '.png')
cv.imwrite(output_name, output)


# Reverse Linked List II
"""
Given the head of a singly linked list and two integers left and right where left <= right,
reverse the nodes of the list from position left to position right, and return the reversed list.
"""
# class ListNode:
# 	def __init__(self, val=0, next=None):
# 		self.val = val
# 		self.next = next
# prev=0, cur = 1
# tail aage aur con peeche.
class Solution:
    def reverseBetween(self, head, m, n):
        """
        :type head: ListNode
        :type m: int
        :type n: int
        :rtype: ListNode
        """

        # Empty list
        if not head:
            return None

        # Move the two pointers until they reach the proper starting point
        # in the list.
        cur, prev = head, None
        while m > 1:
            prev = cur
            cur = cur.next
            m, n = m - 1, n - 1

        # The two pointers that will fix the final connections.
        tail, con = cur, prev

        # Iteratively reverse the nodes until n becomes 0. # i.e. from m to n
        while n:
            third = cur.next
            cur.next = prev
            prev = cur
            cur = third
            n -= 1

        # Adjust the final connections as explained in the algorithm
        if con:
            con.next = prev
        else:
            head = prev
        tail.next = cur
        return head


from unittest.mock import MagicMock, patch

from django.test import override_settings
from integrations.tasks import notify_new_submission, switchable_task

def test_notify_new_submission():
    with patch("integrations.slack.send_message") as m1:
        notify_new_submission(
            "test_title",
            "test_elevator_pitch",
            "test_submission_type",
            "test_admin_url",
            "test_topic",
            42,
        )

    blocks = m1.call_args[0][0]
    attachments = m1.call_args[0][1]

    assert blocks[0]["text"]["text"] == "New _test_submission_type_ Submission"
    assert (
        attachments[0]["blocks"][0]["text"]["text"]
        == "*<test_admin_url|Test_title>*\n*"
        "Elevator Pitch*\ntest_elevator_pitch"
    )
    assert attachments[0]["blocks"][0]["fields"][2]["text"] == "42"
    assert attachments[0]["blocks"][0]["fields"][3]["text"] == "test_topic"

@override_settings(USE_SCHEDULER=True)
def test_switchable_task():
    def dummy_task():
        pass

    dummy_task.delay = MagicMock()
    switchable_dummy_task = switchable_task(dummy_task)

    switchable_dummy_task()

    assert dummy_task.delay.called


#! /usr/bin/env python3
# coding=utf-8

def main(max=10):
    for n in range(1, max + 1):
        print(f"{n:8,}  {n**2:8,}  {n**3:8,}")

if __name__ == "__main__":
    main(40)


from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('blogs/', views.BlogListView.as_view(), name='blogs'),
    path('blogs/<int:pk>', views.BlogDetailView.as_view(), name='blog-detail'),
    path('authors/', views.AuthorListView.as_view(), name='authors'),
    path('authors/<int:pk>', views.AuthorDetailView.as_view(), name='author-bio'),
    path('blogs/<int:pk>/comment/', views.BlogCommentCreate.as_view(), name='blog_comment'),
]


'''
    Search k-D tree by using distances
'''

from sys import path
from numpy import where
path.insert(0, '/media/gtron/files/ml/ml/utils')
from path_setter import insert_paths
insert_paths()

from knn import classify
from kd_tree import kd_tree, nn_search_tree
from process_data import process_data

def test():
    '''
        Test kdtree searching algorithm
    '''
    data, normalizing, labeling = process_data('datingTestSet.txt')
    normalized_dataset, ranges, min_vals, max_vals = normalizing
    label_indices, labels = labeling

    # set size of test set
    ho_ratio = 0.1
    m = data.shape[0]
    test_dataset_size = int(m * ho_ratio)

    training_set = normalized_dataset[test_dataset_size:]
    training_set_label_indices = label_indices[test_dataset_size:]

    tree = kd_tree(training_set)

    error_count = 0
    for i in range(test_dataset_size):
        # find nearest neighbor
        normalized_test_node = (data[:test_dataset_size][i] - min_vals) / ranges
        normalized_test_node_index = label_indices[:test_dataset_size][i]

        search_results = nn_search_tree(tree, normalized_test_node)

        kd_label = labels[training_set_label_indices[where(((training_set[:, 0] == search_results[0]) &
                                                            (training_set[:, 1] == search_results[1]) &
                                                            (training_set[:, 2] == search_results[2])))[0]]][0]

        if kd_label is not labels[normalized_test_node_index]:
            error_count += 1
    return error_count, test_dataset_size

error_count, test_dataset_size = test()
print(error_count / test_dataset_size)


import tkinter

from tkinter import *
from utilities import predict, load_saved_model
import nltk

def predict_slots():
  varContent = t1.get("1.0", "end-1c")
  print ("Predicting...")
  original_tokens, return_tags = predict(varContent, model)
  zipped_tags =  list(zip( original_tokens, *return_tags)) # a list of tokens, where token [0] is original text
  print (zipped_tags)
  listbox_word.delete(0, END)
  listbox_token.delete(0, END)

  for r, tokens in enumerate(zipped_tags):
    if(len(tokens) >= 2):
        word = tokens[0]
        tag = " / ".join(tokens[1:])
        listbox_word.insert(END, word)
        listbox_token.insert(END, tag)
    else:
        word = tokens[0]
        listbox_word.insert(END, word)
        listbox_token.insert(END, "No relevant slots!")

  print ('Printed on GUI.')
nltk.download('punkt')

# load the model
model = load_saved_model()

root = Tk()
root.resizable(width=FALSE, height=FALSE)
root.geometry("600x600")
root.config(bg="grey")

#create containers
top_frame = Frame(root, bg="cyan", width =600, height = 100)
top_frame.grid(row=0)
top_frame.pack_propagate(0)
bottom_frame = Frame(root, bg="green", width=600, height = 500)
bottom_frame.grid(row=1)

submit_button = Button(top_frame, text ='Predict Slots', command = predict_slots)
l1=Label(top_frame, text='Please type a sentence below, and press <Predict Slots>.')
t1 = Text(top_frame,bd=0,font='Fixdsys -14')

submit_button.pack()
l1.pack()
t1.pack()

g1 = Label(bottom_frame, text="Word")
g2 = Label(bottom_frame, text="Slots")
g1.grid ( row=0, column=0)
g2.grid (row=0, column=1)
listbox_word = Listbox(bottom_frame, width=50, height=30)
listbox_word.grid (row=1, column=0)
listbox_token = Listbox(bottom_frame, width=50, height=30)
listbox_token.grid (row=1, column=1)

root.mainloop()


import yaml

from .models import Locus
from .schemas import LocusSchema

def load_locus_yaml(fpath: str) -> Locus:
    schema = LocusSchema()
    with open(fpath) as handle:
        yd = yaml.load(handle, Loader=yaml.SafeLoader)
    unmarshalled = schema.load(yd)
    if len(unmarshalled.errors) != 0:
        raise ValueError("Could not load Locus because {0}".format(unmarshalled.errors))
    return unmarshalled.data

def dump_locus(locus: Locus) -> dict:
    schema = LocusSchema()
    marshalled = schema.dump(locus)
    if len(marshalled.errors) != 0:
        raise ValueError("Could not dump Locus because {0}".format(marshalled.errors))
    return marshalled.data


#Given a list of integers named “ages,” form a new list named “minor_ages”
#consisting of all those ages from the “ages” list that are less than 18.

#Author : Kelvin Ngure
def minor():
    minors = []
    adult = []
    all_ages = [[minors], [adult]] #COMBINING THE TWO LISTS
    age_in = int(input("Enter an age: "))

    #APPENDING AGES UNDER 18 TO MINORS LIST AND AGES OVER 17 TO ADULT LIST
    while age_in >= 0:
        if age_in > 17:
            adult.append(age_in)
        else:
            minors.append(age_in)
        age_in = int(input("Enter an age: "))

    print("The ages of the minors are: ")

    #LOOP TO PRINT ALL AGES IN MINORS LIST
    for young in minors:
        print(young)

minor()


import requests
from fake_useragent import UserAgent
import re, time, os
from random import randint

# 获取主页面 然后进行分析
def get_html(url):
    headers = {
        'User-Agent': UserAgent().chrome
    }
    proxies = {
        "http": "http://10.10.1.10:3128",
    }
    time.sleep(randint(5, 10))
    response = requests.get(url, headers=headers, proxies=proxies, timeout=30)
    response.encoding = 'utf-8'
    if response.status_code == 200:
        html_text = response.text
        # print(html_text)

        return html_text
    else:
        time.sleep(randint(3, 8))
        print('----------------当前网页获取失败---------------')

# 获取每个电影的url
def get_urls(html_text):
    ids = re.findall(r'a href="/films/(\d+)" target="_blank" data-act="movies-click', html_text)
    # 获取当前页面电影的所有id
    # print(ids)
    urls = []
    for i in ids:
        url = 'https://maoyan.com/films/{}'.format(i)
        urls.append(url)
    # print(urls)
    return urls

# 解析并获取需要的信息
def get_film(html_text):
    # 分析需要获取的信息
    # 影片名称
    # 类型 地区国家  影片时长  上映时间
    # 评分  评分人数  累计票房  由于是动态获取不到
    # 剧情简介
    # 演职人员 导演  演员：饰演角色
    # 图集  暂时不处理
    # 热门短评 用户名：评语
    # 影片名称
    Ch_title = re.findall(r'
(.*)
', html_text)[0]
    Eng_title = re.findall(r'
(.*)
', html_text)[0]
    print(Ch_title)

    # 类型
    type = re.findall(r'
(.*)
', html_text)[0]
    # 地区
    relist = re.findall(r'
\s+(.*)\s+/ (\d+.*)\s+
', html_text)[0]
    # print(relist)
    area = relist[0]
    # 时长
    duration = relist[1]
    # 上映时间
    on_time = re.findall(r'
\s+
(.+)
\s+
', html_text)[0]

    # 简介
    intro = re.findall(r'(.+)', html_text)[0]

    # 演职人员
    # 导演
    relist = re.findall(r'\s+(\S+)\s+', html_text)
    # print(relist)
    director = relist[0]
    # 演员
    # 获取的是所有的演员 和饰演角色 而且有重复的  后面想办法处理

    actor_list = re.findall(
        r'\s+(.*)\s+\s+
(.*)',
        html_text)
    # print(actor_list)
    # 删除重复出现的演员名单
    # 看规律貌似是前四个  具体后面分析再说
    # 想到set集合可以去重  添加到集合然后转list 遍历出来
    try:
        for i in range(4):
            actor_list.pop(0)
    except:
        pass
    actors = ''
    for actor in actor_list:
        actor = actor[0] + '  ' + actor[1]
        actors = actors + '     ' + actor
        # print(actors)
    print(actors)
    # 热门评语
    # 用户名
    # 获取是个列表 多个用户
    username_list = re.findall(r' (\S+)', html_text)
    # 评论
    # 获取是个列表 多个评论  每个用户对于一个评论  索引一样
    comment_list = re.findall(r'
(.*)', html_text)
    comments = ''
    for username, comment in zip(username_list, comment_list):
        comment = username + '\n' + comment + '\n\n'
        comments += comment
        # print(comments)
    # print(comments)
    # 知道需要获取的信息 然后用正则匹配出来即可
    num = 0
    try:
        film = f'影片名称:{Ch_title}({Eng_title})\n' \
            f'类型:{type} \n地区:{area} \n时长:{duration}\n上映时间:{on_time}\n' \
            f'影片简介:{intro} \n' \
            f'导演:{director} \n' \
            f'演员:\n{actors} \n\n' \
            f'热门评论:\n{comments}'
        return film, Ch_title
    except:
        film = '匹配可能有错！'
        num += 1
        return film, num

# 保存信息
def save_film(film, name):
    path = os.path.join(f'猫眼电影信息爬取/{name}.txt')
    with open(path, 'w', encoding='utf-8') as f:
        f.write(film)

def save_urls(url):
    path = os.path.join('猫眼电影信息爬取/urls.txt')
    with open(path, 'a', encoding='utf-8') as f:
        f.write(url + '\n')

def read_urls(url, num):
    path = os.path.join('猫眼电影信息爬取/urls.txt')
    with open(path, 'a', encoding='utf-8') as f:
        f.write(f'{num}. ')
    with open(path, 'r', encoding='utf-8') as f:
        urls = f.read()
        if url in urls:
            return False
        else:
            return True

# 主方法
def main():
    # 创建文件夹  和  文件urls.txt用于存储所有的url
    try:
        os.mkdir('猫眼电影信息爬取')
    except:
        pass

    # 获取一页的源码html
    url = 'https://maoyan.com/films'
    html_text = get_html(url)

    # 根据获取到的html分析 得到每个电影的id 然后拼接成url
    urls = get_urls(html_text)

    # 然后根据每个url获取每个电影的信息
    # 遍历urls  得到url  然后分析
    num = 0
    for url in urls:
        num += 1
        print(f'----------------------获取第{num}个电影信息--------------------')
        print(url)
        # 将url保存到文件中, 每次访问文件  查看url是否存在  不存在的时候再进行爬取

        if read_urls(url, num):
            save_urls(url)
            html_text = get_html(url)
            film, name = get_film(html_text)
            save_film(film, name)
        else:
            print('---------------当前电影信息已爬取，跳过---------------')
            continue

if __name__ == '__main__':
    main()

1.猫眼电影数据爬取(改).py


import os
import boto3
from botocore.exceptions import ClientError
import tempfile
import re

from ..artefacts import Artefact, File, Directory
from ..manager import RemoteManager
from .. import exceptions

class Amazon(RemoteManager):

    def __init__(
        self,
        bucket: str,
        aws_access_key_id: str = None,
        aws_secret_access_key: str = None,
        region_name: str = None
    ):

        self._bucketName = bucket
        self._aws_access_key_id = aws_access_key_id
        self._aws_secret_access_key = aws_secret_access_key
        self._region_name = region_name

        self._s3Client = boto3.client(
            "s3",
            aws_access_key_id=aws_access_key_id,
            aws_secret_access_key=aws_secret_access_key,
            region_name=region_name
        )
        self._s3Resource = boto3.resource(
            's3',
            aws_access_key_id=aws_access_key_id,
            aws_secret_access_key=aws_secret_access_key,
            region_name=region_name
        )

        # Create a paginator object for iterating through remote objects
        self._clientPaginator = self._s3Client.get_paginator('list_objects')

        # Create a reference to the AWS bucket - create a Directory to represent it
        self._bucket = self._s3Resource.Bucket(name=bucket) # pylint: disable=no-member

        super().__init__()

    def __repr__(self): return '<Manager(S3): {}>'.format(self._bucketName)

    def abspath(self, relpath: str):
        """ Difference between AWS and manager path is the removal of a leading '/'. As such remove the first character
        """
        return self.relpath(relpath)[1:]

    def _isdir(self, relpath: str):

        try:
            # Try and load the object outright - if successful then file not dir
            self._bucket.Object(self.abspath(relpath)).load()
            return False

        except ClientError as e:
            # No object existed at location - check if a directory exists
            if e.response["Error"]["Code"] != "404": raise

            # Get all directories for that level
            dirs = self._clientPaginator.paginate(
                Bucket=self._bucketName,
                Prefix=self.abspath(self.dirname(relpath)) + "/",
                Delimiter='/'
            )

            # Loop through the returned directires and if any match return True
            for dirObj in dirs.search("CommonPrefixes"):
                if dirObj is None: continue
                if relpath == self.relpath(dirObj.get("Prefix")): return True

            raise exceptions.ArtefactNotFound("Couldn't find artefact with relpath: {}".format(relpath))

    def _makefile(self, remotePath: str):
        try:
            awsObject = self._bucket.Object(self.abspath(remotePath))
            awsObject.load()
        except Exception as e:
            raise exceptions.ArtefactNotFound(
                "Couldn't create file at {} as it doesn't exist".format(remotePath)
            ) from e

        return File(self, remotePath, awsObject.last_modified, awsObject.content_length)

    def _get(self, src_remote: Artefact, dest_local: str):

        if isinstance(src_remote, Directory):

            # Identify the prefix for the directory
            prefix = self.abspath(src_remote.path)

            for object in self._bucket.objects.filter(Prefix=prefix):
                # Collect the objects in that directory - downlad each one

                # Write the relative path
                relative_path = object.key[len(prefix) + 1:]

                # Create object absolute path
                path = os.path.abspath(os.path.join(dest_local, relative_path))

                # Ensure the directory for that object
                os.makedirs(os.path.dirname(path), exist_ok=True)

                # Download the file to that location
                self._bucket.download_file(object.key, path)

        else:
            self._bucket.download_file(self.abspath(src_remote.path), dest_local)

    def _put(self, src_local, dest_remote):

        if os.path.isdir(src_local):
            # A directory of items is to be uploaded - walk local directory and uploade each file

            isUploaded = False
            for path, _, files in os.walk(src_local):

                # For each file at this point - construct their local absolute path and their relative remote path
                for file in files:
                    self._bucket.upload_file(
                        os.path.join(path, file),
                        self.abspath(self.join(dest_remote, path[len(src_local):], file))
                    )
                    isUploaded = True

            if not isUploaded:
                # Make a placeholder file for this directory
                placeholder_path = self.abspath(self.join(dest_remote, self._PLACEHOLDER))
                self._bucket.put_object(Key=placeholder_path, Body=b'')

        else:
            # Putting a file
            self._bucket.upload_file(src_local, dest_remote)

    def _rm(self, artefact: Artefact):

        key = self.abspath(artefact.path)
        if isinstance(artefact, Directory):
            for obj in self._bucket.objects.filter(Prefix=key):
                obj.delete()

        else:
            self._bucket.Object(key).delete()

    def _mvFile(self, source, destination):

        self._bucket.Object(destination).copy_from(CopySource={'Bucket': self._bucketName, 'Key': source})
        self._bucket.Object(source).delete()

    def _mv(self, srcObj: Artefact, destPath: str):
        """ Move the object to the desintation """

        source_path, dest_path = self.abspath(srcObj.path), self.abspath(destPath)
        if isinstance(srcObj, Directory):
            for obj in self._bucket.objects.filter(Prefix=source_path):
                relative = obj.key[len(source_path):]
                self._mvFile(obj.key, self.abspath(self.join(dest_path, relative)))

        else:
            self._mvFile(source_path, dest_path)

    def _listdir(self, relpath: str):

        abspath = self.abspath(relpath) + "/"

        # Extract the relevent objects from s3
        dirs = self._clientPaginator.paginate(Bucket=self._bucketName, Prefix=abspath, Delimiter='/')
        files = self._bucket.objects.filter(Prefix=abspath, Delimiter='/')

        # Expand and convert s3 objects
        dirs = {self.relpath(p.get("Prefix")) for p in dirs.search("CommonPrefixes") if p is not None}
        files = {self.relpath(obj.key) for obj in files if obj.key != abspath and obj.key.split('/')[-1] != self._PLACEHOLDER}

        return dirs, files

    def mkdir(self, path):

        with tempfile.TemporaryDirectory() as directory:
            fp = os.path.join(directory, self._PLACEHOLDER)
            open(fp, 'w').close()
            self._bucket.upload_file(Filename=fp, Key=self.abspath(self.join(path, self._PLACEHOLDER)))

        # Identify the owning directory
        owning_directory = self._backfillHierarchy(self.dirname(path))
        art = Directory(self, path)

        # Save the new artefact
        owning_directory._add(art)
        self._paths[path] = art
        return art

    def toConfig(self):
        return {
            'manager': 'AWS',
            'bucket': self._bucketName,
            'aws_access_key_id': self._aws_access_key_id,
            'aws_secret_access_key': self._aws_secret_access_key,
            'region_name': self._region_name
        }


from django.contrib import admin
from .models import *
import translation
from modeltranslation.admin import TranslationAdmin
translation.call()

class ProductsInLine(admin.StackedInline):
    model = Product
    extra = 1

class KeyWordsInline(admin.StackedInline):
    model = KeyWord
    extra = 1

class CompanyAdmin(TranslationAdmin):
    inlines = [
        KeyWordsInline,
        ProductsInLine,
    ]

    list_display = ('name', )

    class Media:
        js = (
            '/static/modeltranslation/js/force_jquery.js',
            'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js',
            '/static/modeltranslation/js/tabbed_translation_fields.js',
        )
        css = {
            'screen': ('/static/modeltranslation/css/tabbed_translation_fields.css',),
        }

admin.site.register(Company, CompanyAdmin)
admin.site.register(Tariff)


##---
Year='2018'
#eleWP='mvaFall17V1Iso_WP90'
eleWP='POGTight'
#eleWP='cutFall17V1Iso_Tight'
#muWP='cut_Tight_HWWW'
muWP='cut_Tight_POG'

bAlgo='DeepB'
bWP='0.1241'

maxtau21='0.45'
min_jetId='4'
max_jetId='10'

FatSel=['MW','PT']

elePtCut='35'
muPtCut='27'
## double lep pt cut
eePtCut=('35','35') #('26','15') # HLT_Ele23_Ele12_CaloIdL_TrackIdL_IsoVL
mmPtCut=('27','27') #('20','11') # HLT_Mu17_TrkIsoVVL_Mu8_TrkIsoVVL_DZ_Mass3p8
emPtCut=('35','27') #('26','15') # HLT_Mu12_TrkIsoVVL_Ele23_CaloIdL_TrackIdL_IsoVL_DZ
mePtCut=('27','35') #('26','15') # HLT_Mu23_TrkIsoVVL_Ele12_CaloIdL_TrackIdL_IsoVL


import time

from colored_logs import logger

log = logger.Logger(id='Test-id-1')

log.info('This is an info log')
time.sleep(0.5)

log.id='Test-id-2'
log.info('This is an info log with a new id')
log.id='Test-id-1'
time.sleep(0.5)

log.success('This is a success log')
time.sleep(0.5)
log.warning('This is a warning log')
time.sleep(0.5)
log.error('This is an error log')
time.sleep(0.5)
log.fail('This is a fail log')

time.sleep(1)

log.start_process('This is taking a while')
time.sleep(3.5)
log.info('This is an info log while also logging the active process')

time.sleep(3.5)

duration_float_seconds = log.stop_process(
    log_type=logger.LogType.Success,
    values='Successfully finished task'
)


from src.modules.reporting import *
from src.modules.constants import *
from matplotlib import pyplot as plt
from src.modules.classes import SqliteFetcher
from src.modules.thesis_plotting import *
from src.modules.reporting import *
import os

setup_pgf_plotting()

# ============================================================================
# IMPORT/MAKE DATA
# ============================================================================

def make_data(
    models,
    perf_key,
    legend_loc=None,
    ylabel=None,
    f=None,
    i_plot=0,
    legend=False,
    rel_imp_label=True,
    right_ylabel=True
):

    perfs = []
    errs = []
    retro_perfs = []
    retro_errs = []
    edges = []
    rel_imp = []
    rel_imp_err = []

    # Load the predictions
    for i_model, model_full in enumerate(models):
        model = webpage_to_modelname(model_full)
        path = locate_model(model) + '/data/Performance.pickle'
        perf = pickle.load(open(path, 'rb'))

        perfs.append(getattr(perf, perf_key + '_sigma'))
        errs.append(getattr(perf, perf_key + '_sigmaerr'))
        retro_perfs.append(getattr(perf, 'retro_' + perf_key + '_sigma'))
        retro_errs.append(getattr(perf, 'retro_' + perf_key + '_sigmaerr'))
        edges.append(getattr(perf, 'bin_edges'))
        rel_imp.append(getattr(perf, perf_key + '_RI'))
        rel_imp_err.append(getattr(perf, perf_key + '_RIerr'))

        energy_counts = getattr(perf, 'counts')

    d = {
        'edges': [edges[0]]*3,
        'y': [perfs[0], retro_perfs[0], perfs[1]],
        'yerr': [errs[0], retro_errs[0], errs[1]],
        'color': ['tab:blue', 'tab:orange', 'tab:cyan'],
        'xlabel': r'log(E) [E/GeV]',
        'ylabel': ylabel,
        'grid': False,
        'label': ['Ensemble', 'Retro', 'Best submodel'],
        'yrange': {'bottom': 0.001},
        'markersize': 0.8,
        'linewidth': 1.2,
    }
    if not legend:
        del d['label']
    f = make_plot(
        d,
        position=[0.125, 0.26, 0.475, 0.42],
        for_thesis=True,
        h_figure=f
    )

     # ENERGY PLOT
    d = {
        'data': [edges[0][:-1]],
        'bins': [edges[0]],
        'weights': [energy_counts*8],
        'histtype': ['step'],
        'log': [True],
        'color': ['gray'],
        'twinx': True,
        'grid': False,
        'label': ['Training Events'],
        'combine_labels': True,
        'ylabel': 'Training Events',
        'legend_loc': legend_loc,
        'frameon': False
    }
    if not legend:
        del d['label']
    if not right_ylabel:
        del d['ylabel']
    f = make_plot(d, h_figure=f, axes_index=0)

    # RELATIVE IMPROVEMENT PLOT
    d = {
        'edges': [edges[0], edges[1]],
        'y': [rel_imp[0], rel_imp[1]],
        'yerr': [rel_imp_err[0], rel_imp_err[1]],
        'color': ['tab:blue', 'tab:cyan'],
        'xlabel': r'$\log_{10}(E)$ [$E$/GeV]',
        'ylabel': 'Rel. Imp.',
        'grid': True,
        'y_minor_ticks_multiple': 0.2
    }

    if not rel_imp_label:
        del d['ylabel']

    yrange_d = {}
    if max(-0.5, min(rel_imp[0])) == -0.5:
        yrange_d['bottom'] = -0.5
        yrange_d['top'] = 0.5
        d['yrange'] = yrange_d

    d['subplot'] = True
    d['axhline'] = [0.0]
    # f = make_plot(d, h_figure=f, position=[0.625, 0.11, 0.475, 0.15])
    f = make_plot(d, h_figure=f, position=[0.125, 0.11, 0.475, 0.15])

    return f

energy_models = [
    'https://app.wandb.ai/cubeml/cubeml/runs/2020-09-08-19.05.11?workspace=user-bjoernmoelvig', # ER ens
    'https://app.wandb.ai/cubeml/cubeml/runs/2020-09-04-05.14.55?workspace=user-bjoernmoelvig', # Fullreg
]

direction_models = [
    'https://app.wandb.ai/cubeml/cubeml/runs/2020-09-08-13.01.59?workspace=user-bjoernmoelvig', # DR ens
    'https://app.wandb.ai/cubeml/cubeml/runs/2020-09-02-00.33.27?workspace=user-bjoernmoelvig', # DR best single
]
f1  = make_data(
    energy_models,
    'log_frac_E_error',
    ylabel=r'$W\left(\log_{10} \left[ \frac{E_{pred}}{E_{true}} \right]\right)$',
    legend_loc=(0.15, 0.05),
    legend=True,
    right_ylabel=False
)
f2  = make_data(
    direction_models,
    'polar_error',
    ylabel=r'$W(\Delta \theta)$ [deg]',
    legend_loc=(0.15, 0.05),
    rel_imp_label=False
)

# SAVE LOGFRAC 2D ERROR DISTRIBUTION ASWELL
model = webpage_to_modelname(energy_models[0])
model_path = locate_model(model)
wanted_plot = 'log_frac_E_error_2DPerformance'
data_path = model_path + '/figures/pickle_'

plotname = 'log_frac_E_2D'

save_path = get_project_root() + '/reports/thesis_plots/all_pgf/ensemble_performance_'
path = data_path + wanted_plot + '.pickle'
with open(path, 'rb') as f:
    fig = pickle.load(f)
    savefig_path = save_path + plotname + '.png'
    fig.savefig(savefig_path, bbox_inches='tight')

# Move copies of z-score distributions
import os
newname = 'ensemble_performance_zscore_energy.pgf'
pgf_path = model_path + '/figures/distributions/z_score_true_primary_energy.pgf'
destination = ' /home/bjoernhm/CubeML/reports/thesis_plots/all_pgf/'
os.system('cp ' + pgf_path + destination + newname)

model = webpage_to_modelname(direction_models[0])
model_path = locate_model(model)
newname = 'ensemble_performance_zscore_z_dir.pgf'
pgf_path = model_path + '/figures/distributions/z_score_true_primary_direction_z.pgf'
os.system('cp ' + pgf_path + destination + newname)

# Standard ratio of width to height it 6.4/4.8
# Standard figure: FOTW = 1.0
# Subfigure 1/2: FOTW = 0.65. Remember to use a .5 cm of left and 0 cm of right
# broad_figure: FOTW = 2.
# single_fig, 2subfigs
for f, name in zip([f1, f2], ['energy', 'polar']):
    FOTW = get_frac_of_textwidth(keyword='single_fig')
    width = get_figure_width(frac_of_textwidth=FOTW)
    height = get_figure_height(width=width)
    f.set_size_inches(1.25*width, 1.25*height)

    path = Path(os.path.realpath(__file__))
    save_thesis_pgf(path, f, save_pgf=True, png_name=name, pgf_name=name)


# -*- coding: utf-8 -*-
import base64
import urllib
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.views.decorators.csrf import csrf_exempt
from avlnx_moip import moip_settings
import urllib2
from avlnx_moip.helpers import build, get
from avlnx_moip.models import MoipToken
from avlnx_value.models import Invoice, FinancialInformation
from common.helpers.get_tenant_for_user import get_tenant_for_user
from django.utils.translation import ugettext as _
import settings

# Initialization settings
if moip_settings.MOIP_SANDBOX:
    MOIP_URL_BASE = 'https://desenvolvedor.moip.com.br/sandbox/'
    MOIP_TOKEN = moip_settings.MOIP_SANDBOX_TOKEN
    MOIP_KEY = moip_settings.MOIP_SANDBOX_KEY
else:
    MOIP_URL_BASE = 'https://www.moip.com.br/'
    MOIP_TOKEN = moip_settings.MOIP_PRODUCTION_TOKEN
    MOIP_KEY = moip_settings.MOIP_PRODUCTION_KEY

@login_required()
def get_response_XML(request, invoice_id):
    # first check if invoice and financial id exists
    invoice = get_object_or_404(Invoice, id=invoice_id)

    try:
        financial_info = FinancialInformation.objects.get(tenant=get_tenant_for_user(request.user))
    except FinancialInformation.DoesNotExist:
        # all fields are required
        messages.error(request, _(u'Você precisa preencher suas informações financeiras antes de fazer o pagamento. Este procedimento deverá ser realizado apenas uma vez.'))
        return HttpResponseRedirect(reverse('value:edit_financial_info'))

    # check if this tenant is paying his own bills
    if invoice.tenant != get_tenant_for_user(request.user):
        messages.error(request, _(u'Ops. Parece que você está tentando pagar uma fatura de outra pessoa! Deve ter sido um erro honesto. Se você acha que isso se deve a um erro nosso, por favor entre em contato.'))
        return HttpResponseRedirect(reverse('value:index'))

    base = '{token}:{key}'.format(token=MOIP_TOKEN, key=MOIP_KEY)
    url = '{moip_base}ws/alpha/EnviarInstrucao/Unica'.format(moip_base=MOIP_URL_BASE)

    # check if this invoice already has a moip token
    try:
        moip_token = MoipToken.objects.get(invoice=invoice)
        # this token was already built, just redirect the user over there
        instruction_token = moip_token.token
        return HttpResponseRedirect('{moip_base}Instrucao.do?token={instruction_token}&layout=valizze'.format(moip_base=MOIP_URL_BASE,instruction_token=instruction_token))
    except MoipToken.DoesNotExist:
        # no token was found, go get it now
        pass

    auth = base64.b64encode(base)
    headers = {'Authorization': 'Basic {auth}'.format(auth=auth)}

    data = build.get_xml(invoice=invoice,financial_info=financial_info)

    req = urllib2.Request(url=url, data=data, headers=headers)
    raw_response = urllib2.urlopen(req)
    response_xml = raw_response.read()

#    DEBUG
#    return HttpResponse(response_xml)

    instruction_token = get.response_token(response_xml)

    # save the token created
    MoipToken.objects.create(
        tenant = get_tenant_for_user(request.user),
        token = instruction_token,
        id_proprio = '{app_name}_{invoice_id}'.format(app_name=settings.APP_NAME,invoice_id=invoice.id),
        invoice = invoice
    )

    return HttpResponseRedirect('{moip_base}Instrucao.do?token={instruction_token}&layout=valizze'.format(moip_base=MOIP_URL_BASE,instruction_token=instruction_token))

@csrf_exempt
def process_NASP(request, invoice_id):
    invoice = get_object_or_404(Invoice, id=invoice_id)
    moip_instance = get_object_or_404(MoipToken, invoice=invoice)

    # TODO send email confirming payment
    tenants_email = request.POST.get('email_consumidor')

    status_num = request.POST.get('status_pagamento')

    moip_codes_dict = {
        '1': 'autorizado', '2': 'iniciado', '3': 'boleto impresso', '4': 'concluido',
        '5': 'cancelado', '6': 'em análise', '7': 'estornado'
    }

    critical_statuses = {
        '4': 'paid', '5': 'unpaid', '7':'unpaid'
    }

#       autorizado 	1 	Pagamento já foi realizado porém ainda não foi creditado na Carteira MoIP recebedora (devido ao floating da forma de pagamento)
#       iniciado 	2 	Pagamento está sendo realizado ou janela do navegador foi fechada (pagamento abandonado)
#       boleto impresso 	3 	Boleto foi impresso e ainda não foi pago
#    concluido 	4 	Pagamento já foi realizado e dinheiro já foi creditado na Carteira MoIP recebedora
#    cancelado 	5 	Pagamento foi cancelado pelo pagador, instituição de pagamento, MoIP ou recebedor antes de ser concluído
#       em análise 	6 	Pagamento foi realizado com cartão de crédito e autorizado, porém está em análise pela Equipe MoIP. Não existe garantia de que será concluído
#    estornado 	7 	Pagamento foi estornado pelo pagador, recebedor, instituição de pagamento ou MoIP

    for key,value in moip_codes_dict.items():
        if status_num == key:
            moip_instance.moip_status = value
            moip_instance.save()
            # if status change is critical, update invoice too
            if key in critical_statuses.keys():
                invoice.status = critical_statuses[key]
                invoice.save()

    # TODO check if system is blocked and if it needs to be unlocked

    return HttpResponse('True')

#DEBUG
def test_XML(request,invoice_id):
    invoice = get_object_or_404(Invoice, id=invoice_id)
    financial_info = FinancialInformation.objects.get(tenant = get_tenant_for_user(request.user))
    return HttpResponse(build.get_xml(invoice,financial_info))


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

from module.infos.ultrasonic_info import UltrasonicInfo
from module.infos.motor_info import MotorInfo

class ParkingBehavior():
    def __init__(self):
        self.ar_info1 = None
        self.ar_info2 = None

        self.behaviors = [
            self._detect(),
            self._stage_1_1(),
            self._stage_1_2(),
            self._stage_1_3(),
            self._stage_2(),
            self._stage_3()]
        self.behavior_idx = 0

    def get_motor_info(self, ultrasonic_info, ar_info1, ar_info2):
        self.ultrasonic_info = ultrasonic_info
        self.ar_info1 = ar_info1
        self.ar_info2 = ar_info2

        for _, angle, speed in self._play():
            return MotorInfo(angle, speed)

    def _play(self):
        if len(self.behaviors) == self.behavior_idx:
            yield True, 0, 0

        behavior = self.behaviors[self.behavior_idx]
        while True:
            is_done, angle, speed = behavior.next()
            if is_done:
                self.behavior_idx += 1
                break
            yield is_done, angle, speed
        yield True, angle, speed

    def _stage_1_1(self):
        # 초기 주차 방향 및 자리를 맞춰주는 부분(1단계: 직진)
        # substage1 for going forward for distance1 mm
        print("stage_1_1")
        speed = 20
        distance = 400
        cycle = distance // speed

        for _ in range(cycle-1):
            angle = 0
            speed = 20
            yield False, angle, speed

        # angle = 0
        # speed = 0
        yield True, angle, speed

    def _stage_1_2(self):
        # 초기 주차 방향 및 자리를 맞춰주는 부분(2단계: 왼쪽 꺽기)
        # substage2 for going left for distance2 mm
        print("stage_1_2")
        speed = 20
        distance = 360
        cycle = distance // speed

        for _ in range(cycle-1):
            angle = -50
            speed = 20
            yield False, angle, speed

        # angle = 0
        # speed = 0
        yield True, angle, speed

    def _stage_1_3(self):
        # 초기 주차 방향 및 자리를 맞춰주는 부분(3단계: 후진)
        # substage3 for going backward for distance3 mm
        print("stage_1_3")
        speed = -20
        distance = 600
        cycle = distance // abs(speed)

        for _ in range(cycle-1):
            angle = 5
            speed = -20
            yield False, angle, speed

        # angle = 0
        # speed = 0
        yield True, angle, speed

    def _stage_2(self):
        print("stage_2")
        # 초음파 센서를 이용하여 최대 후진(장애물 고려)
        # stage 2 for going backward
        backward_info = UltrasonicInfo()
        value1 = 60
        backward_info.back_middle = value1+8
        backward_info.back_right = value1

        forward_info = UltrasonicInfo()
        value2 = 15+3
        forward_info.back_left = value2
        forward_info.back_middle = value2+10
        forward_info.back_right = value2

        while True:
            print("main ultrasonic", self.ultrasonic_info)
            if self.ultrasonic_info.back_middle < backward_info.back_middle    \
            or self.ultrasonic_info.back_right < backward_info.back_right:
                if self.ultrasonic_info.back_left < forward_info.back_left       \
                or self.ultrasonic_info.back_middle < forward_info.back_middle   \
                or self.ultrasonic_info.back_right < forward_info.back_right:
                    angle = 0
                    speed = 0
                    yield True, angle, speed

                angle = -30
                speed = -17
                yield False, -50, -17

            # exception
            elif self.ultrasonic_info.back_left < forward_info.back_left  \
            or self.ultrasonic_info.back_middle < forward_info.back_middle:
                    angle = 0
                    speed = 0
                    yield True, angle, speed
            else:
                angle = 5
                speed = -20
                yield False, angle, speed

    def _stage_3(self):
        print("stage_3")
        # 전방 AR만 이용해서 자리맞추면서 주차
        # stage 3 for heading AR tag
        value_for_ar = 52

        while True:
            if self.ar_info2.dz > value_for_ar:
                print("stage 3 ", self.ar_info2.pitch, self.ar_info2.dx, self.ar_info2.dz)
                angle = self.ar_info2.dx #+ 9
                speed = 17
                yield False, angle, speed
            # exception
            elif self.ar_info2.dz == 0:
                angle = 0
                speed = -10
                yield False, angle, speed
            else:
                print("Parked")
                angle = 0
                speed = 0
                yield True, angle, speed

    def _detect(self):
        print("detect")
        # weights for angle
        k1 = 1.0
        k2 = 2.0

        while True:
            if self.ar_info1 or self.ar_info2:
                if self.ar_info1:
                    print("=======================")
                    print("AR1 found")
                    print("pitch : " + str(round(self.ar_info1.pitch, 1)))
                    print(" x : " + str(self.ar_info1.dx))
                    print(" z : " + str(self.ar_info1.dz))
                if self.ar_info2:
                    print("=======================")
                    print("AR2 found")
                    print("pitch : " + str(round(self.ar_info2.pitch, 1)))
                    print(" x : " + str(self.ar_info2.dx))
                    print(" z : " + str(self.ar_info2.dz))

                if self.ar_info1:
                    angle = 0
                    speed = 23
                    yield False, angle, speed

                # 처음 2번 AR 감지되고 나서, 초기값 세팅
                if self.ar_info2:
                    # exception
                    if self.ar_info2.dz == 0:
                        angle = 0
                        speed = 23
                        yield False, angle, speed

                    # go to stage 1
                    if 0 < self.ar_info2.dz < 70:
                        print("===Stage1===")
                        angle = 0
                        speed = 0
                        yield True, angle, speed

                    """중요"""
                    # self.angle = k1 * math.atan((max(self.xs) - 50) / max(self.zs)) + self.pitch * k2
                    angle = (self.ar_info2.dx - 65) * k1 + self.ar_info2.pitch * k2  # better working
                    speed = 23
                    yield False, angle - 5, speed

            angle = 0
            speed = 23
            yield False, angle, speed

if __name__ == "__main__":
    parking = ParkingBehavior()
    for is_done, angle, speed in parking._play():
        print (is_done, angle, speed)

    # for is_done, angle, speed in stage1.play():
    #     print(is_done, angle, speed)


import numpy as np
from collections import Counter
from sklearn.metrics import precision_recall_fscore_support

"""function used to calculate the euclidean distance of two vectors"""
distance = lambda a, b: np.linalg.norm(a-b)

def majority_vote(labels):
    """choose the majority label from list of labels sorted by distance

    Args:
        labels ([float]): labels, ordered from nearest to farthest

    Returns:
        [float]: majority label
    """

    vote_counts = Counter(labels)
    winner, winner_count = vote_counts.most_common(1)[0]
    num_winners = len([count
        for count in vote_counts.values()
        if count == winner_count]
    )
    if num_winners == 1:
        return winner
    else:
        return majority_vote(labels[:-1]) # try again without the farthest neighbor

def classify(k, points, unknown):
    """k nearest neighbors classifier

    Args:
        k (int): k
        points ([([float], int)]): list of known (vector, label) tuples
        unknown ([float]): unknown vector to classify

    Returns:
        [int]: classification
    """

    dist_to_unknown = sorted(
       points,
       key=lambda point_label: distance(point_label[0], unknown)
    )

    k_nearest_labels = [label for _, label in dist_to_unknown[:k]]

    return majority_vote(k_nearest_labels)

def validate(classifier, test):
    """validate a classifier

    Args:
        classifier (function([float])): classification function that accepts
            a vector and returns a predicted label
        test ([([float], float)]): list of known (vector, label) tuples

    Returns:
        (float, float, float, float): classifier precision, recall, f1 score, and support
    """
    predictions = []
    labels = []
    for unknown, label in test:
        labels.append(label)
        predictions.append(classifier(unknown))

    p, r, f1, s = precision_recall_fscore_support(labels, predictions, labels=[1])
    return p[0], r[0], f1[0], s[0]


abc = 'abcdefghijklmnopqrstuvwxyz'
encry_dict = {}
front3 = abc[:3]
end23 = abc[3:]
subText = end23 + front3
encry_dict = dict(zip(subText,abc))
print(encry_dict)

msgTest = 'python'
cipher = []
for i in msgTest:
    v = encry_dict[i]
    cipher.append(v)
cipherText = ''.join(cipher)

print('原始 :',msgTest)
print('加密 :',cipherText)


import weakref

import zmq

from .zmqSocket import ZmqSocket
from malcolm.core.socketInterface import ServerSocket
from collections import OrderedDict
from malcolm.core.serialize import SType
from malcolm.core.base import weak_method

class ZmqServerSocket(ZmqSocket, ServerSocket):

    def make_zmq_sock(self, address):
        """Make the zmq sock and bind or connect to address, returning it"""
        sock = self.context.socket(zmq.ROUTER)
        sock.bind(address)
        return sock

    def make_send_function(self, kwargs):
        """Make a send function that will call send with suitable arguments
        to be used as a response function"""
        if not hasattr(self, "_send_functions"):
            self._send_functions = weakref.WeakValueDictionary()
        zmq_id = kwargs.pop("zmq_id")
        _id = kwargs.pop("id")
        if (zmq_id, _id) not in self._send_functions:
            self = weakref.proxy(self)

            def send(typ, value=None, **kwargs):
                kwargs = OrderedDict(id=_id)
                if typ == SType.Error:
                    kwargs.update(message=value.message)
                else:
                    if hasattr(value, "to_dict"):
                        value = value.to_dict()
                    kwargs.update(value=value)
                msg = self.serialize(typ, kwargs)
                self.send([zmq_id, msg])

            self._send_functions[(zmq_id, _id)] = send
        return self._send_functions[(zmq_id, _id)]

ZmqServerSocket.register("zmq://")


from __future__ import print_function
import argparse
import torch
import torch.nn.functional as F
from torch import nn, optim
from torchvision import datasets, transforms
from tqdm import tqdm
from scipy.io import loadmat
from torch.autograd import Variable
import numpy as np
import torch.utils.data as utils
from torchvision.utils import save_image
import matplotlib.pyplot as plt

torch.manual_seed(11111)
###Load data set
device = torch.device("cuda")
dat_mat = loadmat('./data/HCP_samples.mat')
tensor = dat_mat['loaded_tensor_sub']

###Load the adjacentcy matrix
A_mat = np.mean(np.squeeze(tensor[18:86, 18:86,1,:]), axis=2)
A_mat = A_mat + A_mat.transpose()

### Choose the proper type of loss function
loss_type = "poisson"
### If the loss_type = "poisson", set the proper offset for the mean
offset = 100
### Set the neighborhood size for GATE
n_size = 32

### Load networks
net_data = []
for i in range(tensor.shape[3]):
    ith = np.float32(tensor[:,:,0,i] + np.transpose(tensor[:,:,0,i]))
    np.fill_diagonal(ith,np.mean(ith, 0))
    ith = ith[18:86, 18:86]
    ith = ith.flatten()
    ith = ith/offset
    net_data.append(ith)

###Load the phenotype
trait_data = np.squeeze(torch.load('./data/trait.pt')[:,0])
trait_data = trait_data.numpy()
trait_data = (trait_data-np.mean(trait_data))/np.std(trait_data)
trait_data = torch.from_numpy(trait_data)
trait_data = trait_data.float()
# 0 - Oral Reading Recognition Test
# 1 - Picture Vocabulary Test
# 2 - Line Orientation: Total number correct
# 3 - Line Orientation: Total positions off for all trials
batch_size = 5
tensor_y = torch.stack([torch.Tensor(i) for i in net_data])
y = utils.TensorDataset(tensor_y, trait_data) # create your datset
train_loader = utils.DataLoader(y, batch_size)

def to_var(x, requires_grad=False, volatile=False):
    """
    Varialbe type that automatically choose cpu or cuda
    """
    if torch.cuda.is_available():
        x = x.cuda()
    return Variable(x, requires_grad=requires_grad, volatile=volatile)

class GraphCNN(nn.Linear):
    def __init__(self, in_features, out_features, bias=True):
        super(GraphCNN, self).__init__(in_features, out_features, bias)
        self.mask_flag = False
    def set_mask(self, mask):
        self.mask = to_var(mask, requires_grad=False)
        self.weight.data = self.weight.data*self.mask.data
        self.mask_flag = True
    def get_mask(self):
        print(self.mask_flag)
        return self.mask
    def forward(self, x):
        if self.mask_flag == True:
            weight = self.weight*self.mask
            return F.linear(x, weight, self.bias)
        else:
            return F.linear(x, self.weight, self.bias)

class VAE(nn.Module):
    def __init__(self):
        super(VAE, self).__init__()
        latent_dim = 68
        self.w = Variable(torch.randn(5, 1), requires_grad=True).cuda()
        self.a = Variable(torch.randn(68*68), requires_grad=True).cuda()
        self.fc1 = nn.Linear(68*68, 256)
        self.fc21 = nn.Linear(256, latent_dim)
        self.fc22 = nn.Linear(256, latent_dim)
        self.fc31 =  nn.Linear(latent_dim, 68)
        self.fc32 = nn.Linear(latent_dim,68)
        self.fc33 = nn.Linear(latent_dim,68)
        self.fc34 = nn.Linear(latent_dim,68)
        self.fc35 = nn.Linear(latent_dim,68)
        self.fc41 = GraphCNN(68,68)
        self.fc42 = GraphCNN(68,68)
        self.fc43 = GraphCNN(68,68)
        self.fc44 = GraphCNN(68,68)
        self.fc45 = GraphCNN(68,68)
        self.fcy1 = nn.Linear(latent_dim,1)
        self.fcintercept = nn.Linear(68*68, 68*68)
    def encode(self, x):
        h1 = F.relu(self.fc1(x))
        return self.fc21(h1), self.fc22(h1)
    def reparameterize(self, mu, logvar):
        std = torch.exp(0.5*logvar)
        eps = torch.randn_like(std)
        return mu + eps*std
    def decode(self, z):
        h31= F.sigmoid(self.fc31(z))
        h31= F.sigmoid(self.fc41(h31))
        h31_out = torch.bmm(h31.unsqueeze(2), h31.unsqueeze(1))
        h32 = F.sigmoid(self.fc32(z))
        h32 = F.sigmoid(self.fc42(h32))
        h32_out = torch.bmm(h32.unsqueeze(2), h32.unsqueeze(1))
        h33 = F.sigmoid(self.fc33(z))
        h33 = F.sigmoid(self.fc43(h33))
        h33_out = torch.bmm(h33.unsqueeze(2), h33.unsqueeze(1))
        h34 = F.sigmoid(self.fc34(z))
        h34 = F.sigmoid(self.fc44(h34))
        h34_out = torch.bmm(h34.unsqueeze(2), h34.unsqueeze(1))
        h35 = F.sigmoid(self.fc35(z))
        h35 = F.sigmoid(self.fc45(h35))
        h35_out = torch.bmm(h35.unsqueeze(2), h35.unsqueeze(1))
        # h30 = h31_out + h32_out + h33_out + h34_out + h35_out
        h30 = torch.cat((h31_out.view(-1,68*68,1), h32_out.view(-1,68*68,1), h33_out.view(-1,68*68,1), h34_out.view(-1,68*68,1), h35_out.view(-1,68*68,1)), 2)
        h30 = torch.bmm(h30, self.w.expand(batch_size,5,1))
        h30 = self.a.expand(batch_size, 68*68) + h30.view(-1,68*68)
        # h30 = (h30)
        h30 = self.fcintercept(h30)
        return h30.view(-1, 68*68)
    def forward(self, x):
        mu, logvar = self.encode(x.view(-1, 68*68))
        z = self.reparameterize(mu, logvar)
        recon = self.decode(z)
        trait = self.fcy1(mu)
        return recon.view(-1, 68*68), mu, logvar, trait.view(-1,1)
    def set_mask(self, masks):
        self.fc41.set_mask(masks[0])
        self.fc42.set_mask(masks[1])
        self.fc43.set_mask(masks[2])
        self.fc44.set_mask(masks[3])
        self.fc45.set_mask(masks[4])
        # self.fcintercept.set_mask(masks[5])

def loss_function(recon_x, that, x, t , mu, logvar):
    # BCE = F.binary_cross_entropy(recon_x, x , reduction='sum')
    BCE = F.poisson_nll_loss(recon_x , x, reduction='sum', log_input=True)
    NCE = F.mse_loss(that, t, reduction='sum')
    KLD = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
    return 0.01*(BCE + KLD) + NCE

def train(epoch, train_loader_k):
    model.train()
    train_loss = 0
    for batch_idx, (data, trait) in enumerate(train_loader_k):
        data = data.to(device)
        trait = trait.to(device)
        optimizer.zero_grad()
        recon_batch, mu, logvar, traithat = model(data.view(-1,68*68))
        loss = loss_function(recon_batch.view(-1,68*68), traithat.view(-1,1), data.view(-1,68*68), trait.view(-1,1), mu, logvar)
        loss.backward()
        train_loss += loss.item()
        optimizer.step()
    print('====> Epoch: {} Average loss: {:.4f}'.format(
          epoch, train_loss / len(train_loader_k.dataset)))

def test(epoch, test_loader_k, test_index):
    model.eval()
    test_loss = 0
    pred_val = torch.zeros(len(test_index))
    test_val = torch.zeros(len(test_index))
    with torch.no_grad():
        for i, (data, trait) in enumerate(test_loader_k):
            data = data.to(device)
            trait = trait.to(device)
            recon_batch, mu, logvar, traithat = model(data)
            test_loss += F.mse_loss(traithat, trait.view(-1,1) ,reduction='sum')
            start = i*batch_size
            end = start + batch_size
            if i == len(test_loader_k) - 1:
                end = len(test_index)
            pred_val[start:end] = traithat.view(-1).cpu()
            test_val[start:end] = trait.view(-1).cpu()
    test_loss /= len(test_index)
    print('====> Test set loss: {:.4f}'.format(test_loss))
    return test_loss, pred_val, test_val

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
model = VAE().to(device)
masks = []
mask_2NN = (np.argsort(np.argsort(A_mat, axis=-1),axis=-1)<n_size+1)
masks.append(torch.from_numpy(np.float32(mask_2NN)).float())
mask_4NN = (np.argsort(np.argsort(A_mat, axis=-1),axis=-1)<n_size+1)
masks.append(torch.from_numpy(np.float32(mask_4NN)).float())
mask_8NN = (np.argsort(np.argsort(A_mat, axis=-1),axis=-1)<n_size+1)
masks.append(torch.from_numpy(np.float32(mask_8NN)).float())
mask_16NN = (np.argsort(np.argsort(A_mat, axis=-1),axis=-1)<n_size+1)
masks.append(torch.from_numpy(np.float32(mask_16NN)).float())
mask_16NN = (np.argsort(np.argsort(A_mat, axis=-1),axis=-1)<n_size+1)
masks.append(torch.from_numpy(np.float32(mask_16NN)).float())
# mask_intercept = np.identity(87*87)
# masks.append(torch.from_numpy(np.float32(mask_intercept)).float())
# model.set_mask(masks)
optimizer = optim.Adam(model.parameters(), lr=1e-3)
batch_size = 5
learning_rate=0.01

nepoch = 200
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
model = VAE().to(device)
optimizer = optim.Adam(model.parameters(), lr=0.001)

for j in range(nepoch):
    train(j, train_loader)
    valid_loss, traithat, trait = test(1, train_loader, np.arange(len(train_loader.dataset)))
    print(valid_loss)


def freeze(is_training):
    import sys
    import tensorflow as tf

    import bn_common

    tf.reset_default_graph()

    raw_inputs = tf.placeholder(dtype=tf.uint8, shape=(None, 600, 800, 3),
                                name='inputs_600_800')
#     raw_inputs = tf.placeholder(dtype=tf.uint8, shape=(None, 192, 256, 3),
#                                 name='inputs_192_256')

    sess, raw_output_up = bn_common.recreate_bn_model(raw_inputs, is_training=is_training)
#     sess, raw_output_up = bn_common.recreate_bn_model(raw_inputs, is_training=is_training, crop_size=None)

    print('raw_output_up.name', raw_output_up.name)

    graph_def = tf.graph_util.convert_variables_to_constants(
        sess,
        sess.graph_def,
        output_node_names=['output_2positiveclasses']
    )

    with tf.gfile.FastGFile('./frozen_inference_graph.pb', 'wb') as f:
        f.write(graph_def.SerializeToString())

    sess.close()

freeze(True)


class Solution:
    def findRepeatedDnaSequences(self, s: str) -> List[str]:
        if len(s) < 10:
            return []
        start = 0
        end = 10
        mymap = collections.defaultdict(int)
        ret = []
        while end <= len(s):
            sub = s[start:end]
            mymap[sub]+=1
            if mymap[sub]>1:
                ret.append(sub)
            start += 1
            end += 1
        return list(set(ret))


from mc_server.mc_message import McMessage
import subprocess
import threading
import select
import time

class Server:
    def __init__(self, screen, log_path):
        self.screen = screen
        self.log_path = log_path
        self.events = []

        thread = threading.Thread(target=self.run, args=())
        thread.daemon = True
        thread.start()

    def run_cmd(self, cmd):
        try:
            return subprocess.check_output(cmd, shell=True)
        except subprocess.CalledProcessError:
            print("Echec de la commande :" + cmd)

    def send(self, string):
        if len(string) > 600:
            length = 600
            chunks = [string[i:i + length] for i in range(0, len(string), length)]
            for chunk in chunks:
                chunk = chunk.replace('"', '\\"')
                self.run_cmd('screen -S %s -X stuff "%s"' % (self.screen, chunk))
            self.run_cmd('screen -S %s -X stuff "^M"' % self.screen)
        else:
            string = string.replace('"', '\\"')
            self.run_cmd('screen -p 0 -S %s -X stuff "%s $(printf \\\r)"' % (self.screen, string))

    def run(self):
        logfile = subprocess.Popen(['tail', '-F', '-n', '0', self.log_path],
                                   stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        newlines = select.poll()
        while True:
            newlines.register(logfile.stdout)
            if newlines.poll(1):
                self.events.append(McMessage(logfile.stdout.readline()))
            time.sleep(0.05)

    def get_events(self):
        events = self.events
        self.events = []
        return events


import datetime

from connexion import NoContent
from flask import request
from flask.views import MethodView

class PetsView(MethodView):
    """ Create Pets service
    """
    method_decorators = []
    pets = {}

    def post(self):
      body= request.json
      name = body.get("name")
      tag = body.get("tag")
      count = len(self.pets)
      pet = {}
      pet['id'] = count + 1
      pet["tag"] = tag
      pet["name"] = name
      pet['last_updated'] = datetime.datetime.now()
      self.pets[pet['id']] = pet
      return pet, 201

    def put(self, petId):
      body = request.json
      name = body["name"]
      tag = body.get("tag")
      id_ = int(petId)
      pet = self.pets.get(petId, {"id": id_})
      pet["name"] = name
      pet["tag"] = tag
      pet['last_updated'] = datetime.datetime.now()
      self.pets[id_] = pet
      return self.pets[id_], 201

    def delete(self, petId):
      id_ = int(petId)
      if self.pets.get(id_) is None:
          return NoContent, 404
      del self.pets[id_]
      return NoContent, 204

    def get(self, petId):
      id_ = int(petId)
      if self.pets.get(id_) is None:
          return NoContent, 404
      return self.pets[id_]

    def search(self, limit=100):
      # NOTE: we need to wrap it with list for Python 3 as dict_values is not JSON serializable
      return list(self.pets.values())[0:limit]


def is_power(n):
	for i in range(1, n + 1, 2):
		n = n - i
		if n == 0:
			return True
		elif n < 0:
			return False
	return False

if __name__ == "__main__":
	print(is_power(-1))


import numpy as np
from abc import abstractmethod, ABC

class GA(ABC):

    """
    Implementation of genetic algorithm. Note that for a given iteration this implementation
    only requires O(1) operation besides the cost of selecting the fittest parents
    The implementation models population as a python list with O(1) access cost.

    It is based on a so called generational type of algorithm where crossover/mutation occurs on the
    two fittest at each iteration.
    """

    def __init__(self, pop_size=100, n_genes=2, mutation_rate=0.1, selection_rate=0.5, input_limits=[0, 1],
                 max_gen=100, stop=None):
        self.pop_size = pop_size
        self.n_genes = n_genes

        self.n_mutations = np.ceil((pop_size - 1) * n_genes * mutation_rate)
        self.pop_keep = np.floor(selection_rate * pop_size)
        self.input_limits = input_limits
        self.mutation_rate = mutation_rate
        self.max_gen = max_gen
        self.stop = stop

    @abstractmethod
    def fitness_function(self, individual):
        """
        Implements the logic that calculates the fitness
        measure of an individual.
        :param individual: chromosome of genes representing an individual
        :return: the fitness of the individual
        """

        raise NotImplementedError

    @abstractmethod
    def select_parents(self, fitness):
        """
        Selects the parents

        :param fitness: numpy array with fitness value for each individual in the population

        :return: 2 arrays with selected individuals corresponding to each parent
        """

        raise NotImplementedError

    def initialize_population(self):
        """
        Initializes the population of the problem according to the
        population size and number of genes.
        :param pop_size: number of individuals in the population
        :param n_genes: number of genes (variables) in the problem
        :param input_limits: tuple containing the minimum and maximum allowed
        :return: a numpy array with a randomly initialized population
        """

        population = [np.random.uniform(self.input_limits[0], self.input_limits[1], size=self.n_genes)
                      for _ in range(self.pop_size)]

        return population

    def crossover(self, first_parent, sec_parent):
        """
        Creates an offspring from 2 parents. It performs the crossover
        according the following rule:
        p_new = first_parent[crossover_pt] + beta * (first_parent[crossover_pt] - sec_parent[crossover_pt])
        offspring = [first_parent[:crossover_pt], p_new, sec_parent[crossover_pt + 1:]
        where beta is a random number between 0 and 1, and can be either positive or negative
        depending on if it's the first or second offspring
        :param first_parent: first parent's chromosome
        :param sec_parent: second parent's chromosome
        :param offspring_number: whether it's the first or second offspring from a pair of parents.

        :return: the resulting offspring.
        """
        crossover_pt = np.random.randint(0, high=self.n_genes)
        offspring1 = np.concatenate([first_parent[:crossover_pt], sec_parent[crossover_pt:]])
        offspring2 = np.concatenate([sec_parent[:crossover_pt], first_parent[crossover_pt:]])

        return offspring1, offspring2

    def mutate(self, individual):
        """
        Mutate an individual's i with probability self.mutation_rate
        :param population: the population at a given iteration
        :param n_mutations: number of mutations to be performed.
        :param input_limits: tuple containing the minimum and maximum allowed
         values of the problem space.

        :return: the mutated population
        """

        u = np.random.rand()
        if u < self.mutation_rate:
            mutation_genes = np.random.randint(0, high=self.n_genes)
            individual[mutation_genes] = np.random.uniform(self.input_limits[0], self.input_limits[1])

        return individual

    def calculate_fitness(self, population):
        """
        Calculates the fitness of the population
        :param population: population state at a given iteration
        :return: the fitness of the current population
        """

        return list(map(self.fitness_function, population))

    def run(self):

        """
            Performs the genetic algorithm optimization according to the
            global scope initialized parameters

            :return: (best individual, best fitness)
            """

        # initialize the population
        population = self.initialize_population()

        # Calculate the fitness of the population
        fitness = self.calculate_fitness(population)

        gen_n = 0
        while True:

            gen_n += 1

            # Get index for parents pairs: mother is the fittest
            ma, pa = self.select_parents(fitness)
            mother = population[ma]
            father = population[pa]

            # Get indices of individuals to be replaced
            to_replace = np.random.randint(0, self.pop_size, size=2)
            while len({to_replace[0], to_replace[1]}.intersection({ma, pa})) > 0:
                to_replace = np.random.randint(0, self.pop_size, size=2)

            # Get crossover point for each individual
            offspring1, offspring2 = self.crossover(mother, father)

            # Mutate offspring
            offspring1 = self.mutate(offspring1)
            offspring2 = self.mutate(offspring2)

            # update population
            population[to_replace[0]] = offspring1
            population[to_replace[1]] = offspring2

            # update fitness
            fitness_offspring1 = self.fitness_function(offspring1)
            fitness_offspring2 = self.fitness_function(offspring2)

            fitness[to_replace[0]] = fitness_offspring1
            fitness[to_replace[1]] = fitness_offspring2

            best_fitness = self.fitness_function(mother)

            print(f'Best fitness so far: {best_fitness}')

            if gen_n >= self.max_gen:
                break

            if self.stop is not None and best_fitness <= self.stop:
                break

        return mother, best_fitness


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#import numpy as np
import ex02

def dnaToAminoAcid(chain, start):
    codonList = {'TTT':'F', # Phe \
                 'TTC':'F', # Phe \
                 'TTA':'L', # Leu \
                 'TTG':'L', # Leu \
                 'TCT':'S', # Ser \
                 'TCC':'S', # Ser \
                 'TCA':'S', # Ser \
                 'TCG':'S', # Ser \
                 'TAT':'Y', # Tyr \
                 'TAC':'Y', # Tyr \
                 'TAA':'_', # STOP \
                 'TAG':'_', # STOP \
                 'TGT':'C', # Cys \
                 'TGC':'C', # Cys \
                 'TGA':'_', # STOP \
                 'TGG':'W', # Trp \
                 'CTT':'L', # Leu \
                 'CTC':'L', # Leu \
                 'CTA':'L', # Leu \
                 'CTG':'L', # Leu \
                 'CCT':'P', # Pro \
                 'CCC':'P', # Pro \
                 'CCA':'P', # Pro \
                 'CCG':'P', # Pro \
                 'CAT':'H', # His \
                 'CAC':'H', # His \
                 'CAA':'Q', # Gln \
                 'CAG':'Q', # Gln \
                 'CGT':'R', # Arg \
                 'CGC':'R', # Arg \
                 'CGA':'R', # Arg \
                 'CGG':'R', # Arg \
                 'ATT':'I', # Ile \
                 'ATC':'I', # Ile \
                 'ATA':'I', # Ile \
                 'ATG':'M', # Met \
                 'ACT':'T', # Thr \
                 'ACC':'T', # Thr \
                 'ACA':'T', # Thr \
                 'ACG':'T', # Thr \
                 'AAT':'N', # Asn \
                 'AAC':'N', # Asn \
                 'AAA':'K', # Lys \
                 'AAG':'K', # Lys \
                 'AGT':'S', # Ser \
                 'AGC':'S', # Ser \
                 'AGA':'R', # Arg \
                 'AGG':'R', # Arg \
                 'GTT':'V', # Val \
                 'GTC':'V', # Val \
                 'GTA':'V', # Val \
                 'GTG':'V', # Val \
                 'GCT':'A', # Ala \
                 'GCC':'A', # Ala \
                 'GCA':'A', # Ala \
                 'GCG':'A', # Ala \
                 'GAT':'D', # Asp \
                 'GAC':'D', # Asp \
                 'GAA':'E', # glu \
                 'GAG':'E', # glu \
                 'GGT':'G', # Gly \
                 'GGC':'G', # Gly \
                 'GGA':'G', # Gly \
                 'GGG':'G', # Gly \
                 }
    frameList = [chain[i: i+3] for i in range(start, len(chain), 3)]
    end = len(frameList) - 1
    if len(frameList[end]) < 3:
        frameList.pop(end)

    return ''.join(map(lambda frame: codonList[frame], frameList))

if __name__ == "__main__":
    f = open('NT_113952.1.fasta', 'r')
    f.readline()
    chain = f.read()
    chain = chain.replace('\n', '')
    rcomp = ex02.reverseComplement(chain)
    print('chain:')
    for i in range(3):
        print(str(i) + ': ' + dnaToAminoAcid(chain, i))
    print('reverse complement:')
    for i in range(3):
        print(str(i) + ': ' + dnaToAminoAcid(rcomp, i))


#------------------------------------------------------------------------------
# Introdução a Programação de Computadores - IPC
# Universidade do Estado do Amazonas - UEA
# Prof. Jucimar Jr.
#
# João Vitor de Cordeiro B. Gonçalves   1515140036
# Joelson Pereira Lima                  1715310060
# Judá Salazar Braga                    1515200050
# Kethelen Tamara Braga Barbosa         1525212002
# Lucas Gabriel Silveira                1715310053
# Silas Castro de Mendonça              1715310066
# Leia os quatro valores correspondentes aos eixos x e y de dois pontos quaisquer no plano, p1(x1,y1) e p2(x2,y2) e calcule a distância entre eles, mostrando 4 casas decimais após a vírgula, segundo a fórmula:
#------------------------------------------------------------------------------

line1 = (input().split())
line2 = (input().split())
x1 = line1
y1 = line1
x2 = line2
y2 = line2
distance = ((((float(x2)-float(x1))**2)+((float(y2)-float(y1))**2))**0.5)

print('%0.4f'%distance)


import math
# from PIL import ImageColor, Image
# from matplotlib.pyplot import *

def Find_point(distanse, rotation, x1, y1, direct):
    if rotation == 0:
        x2 =x1 + distanse * math.cos(direct)
        y2 =y1 + distanse * math.sin(direct)
    else:

        radius = abs( distanse / rotation)

        x_forward = math.cos(direct) * radius
        y_forward = math.sin(direct) * radius

        if rotation < 0:
            x_forward2 = y_forward
            y_forward2 = -x_forward
            new_angle = direct + (math.pi / 2) + rotation

        else:
            x_forward2 = -y_forward
            y_forward2 = x_forward
            new_angle = direct - (math.pi / 2) + rotation

        cenx = x1 + x_forward2
        ceny = y1 + y_forward2
        #return [cenx,ceny]

        x2 =cenx + radius * math.cos(new_angle)
        y2 =ceny + radius * math.sin(new_angle)
    return [x2,y2]
inp = input().split(" ")
cnt_test = int(inp[0])
dt = int(inp[1])/1000
dataC = {

}
masrx = []
masry = []
angle = 0
x = 0
y = 0
colors = []
for i in range(cnt_test):
    inp = input().split(" ")
    v_line = float(inp[0])
    v_angle = float(inp[1])*1000
    dist = float(inp[2])
    color = inp[3]
    angl = v_angle * dt
    res = Find_point(v_line*dt,v_angle*dt,x,y,angle)
    masrx.append(x)
    masry.append(y)

    if color != "000000":
        xc = x + math.cos(angle)*dist
        yc = y + math.sin(angle)*dist
        if color in colors:
            dataC[color].append([xc,yc])
        else:
            dataC[color] = [[xc,yc]]
            colors.append(color)
    x=  res[0]
    y = res[1]
    angle += angl

def find_centr(x1,y1,x2,y2,x3,y3):
    A = x2 - x1
    B = y2 - y1
    C = x3 - x1
    D = y3 - y1
    E = A * (x1 + x2) + B * (y1 + y2)
    F = C * (x1 + x3) + D * (y1 + y3)
    G = 2 * (A * (y3 - y2) - B * (x3 - x2))
    if G!=0:
        Cx = (D * E - B * F) / G
        Cy = (A * F - C * E) / G
        return [Cx,Cy,True]
    return [0,0,False]

def maximus(r1,r2):
    s1 = convert_base(r1,10,16)
    s2 = convert_base(r2,10,16)
    if s1 > s2:
        return r1
    else:
        return r2
def minimus(r1,r2):
    s1 = convert_base(r1,10,16)
    s2 = convert_base(r2,10,16)
    if s1 < s2:
        return r1
    else:
        return r2
def convert_base(num, to_base=10, from_base=10):
    # first convert to decimal number
    if isinstance(num, str):
        n = int(num, from_base)
    else:
        n = int(num)
    # now convert decimal to 'to_base' base
    alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    if n < to_base:
        return alphabet[n]
    else:
        return convert_base(n // to_base, to_base) + alphabet[n % to_base]
def find_midle(data):
    masx = []
    masy = []
    for i in range(len(data)-2):
        for j in range(i+1,len(data)-1):
            for c in range(j+1,len(data)):
                res = find_centr(data[i][0],data[i][1],data[j][0],data[j][1],data[c][0],data[c][1])
                if res[2]:
                    masx.append(res[0])
                    masy.append(res[1])
    rex= sum(masx)/len(data)
    rey= sum(masy)/len(data)
    return [rex,rey]
def dist(x1,y1,x2,y2):
    return math.sqrt((x1 - x2)*(x1-x2)+ (y1-y2)*(y1 - y2))
answ = []
for i in range(len(colors)-1):
    cur = dataC[colors[i]]
    cen = find_centr(cur[0][0],cur[0][1],cur[1][0],cur[1][1],cur[2][0],cur[2][1])
    if cen[2]:
        for j in range(i+1,len(colors)):
            cur1 = dataC[colors[j]]
            cen1 = find_centr(cur1[0][0],cur1[0][1],cur1[1][0],cur1[1][1],cur1[2][0],cur1[2][1])
            if cen[2]:
                if  dist(cen[0],cen[1],cen1[0],cen1[1])<40:
                    answ.append([minimus(colors[i],colors[j]),maximus(colors[i],colors[j]) ])

anws = sorted(answ,key = lambda s: s[0])
newansw = []
for i in anws:
    print(i[0],i[1])

# for i in range(len(anws)-1):
#     if answ[i][0]==answ[i+1][0]:
#         if answ[i][1]>answ[i+1][1]:
#             newansw+=[answ[i+1],answ[i]]
#         else:
#             newansw+=[answ[i],answ[i+1]]
#     else:
#         newansw+=[answ[i]]

# for i in newansw:
#     print(i)

# if len(answ)==1:
#     print(answ[0])

# im = Image.new("RGB", (1000, 1000))
# masx = []
# masy = []
# for i in range(len(colors)):

#     cur =dataC[colors[i]]
#     for j in range(len(cur)):
#         masx.append(cur[j][0])
#         masy.append(cur[j][1])
# figure()

# plot(masx,masy,"ro")
# xlabel('x')
# ylabel('y')
# title('one circle')
# #Выполнит тоже самое
# #

# show()
# figure()

# plot(masrx,masry,"r")
# xlabel('x')
# ylabel('y')
# title('robot move')
# show()
# A = x2 - x1
# B = y2 - y1
# C = x3 - x1
# D = y3 - y1
# E = A * (x1 + x2) + B * (y1 + y2)
# F = C * (x1 + x3) + D * (y1 + y3)
# G = 2 * (A * (y3 - y2) - B * (x3 - x2))
# if G != 0:
#     Cx = (D * E - B * F) / G
#     Cy = (A * F - C * E) / G
#     print(Cx,Cy)
# print(dataC)


import serial
import time

# Action Order
ORD_STRAIGHT     = 24
ORD_TURNLEFT     = 20
ORD_TURNRIGHT    = 21
ORD_TURNLEFT_90  = 22
ORD_TURNRIGHT_90 = 23

# Setup variable
serial_use     = 1
serial_port    = None
Read_Rx        = 0
receiving_exit = 1
threding_Time  = 0.01

# Setup for Communication
BPS = 4800

serial_port = serial.Serial('/dev/ttyS0', BPS, timeout = 0.01)
serial_port.flush() # Clear buffer

# Send data by one byte
def TX_data(ser, one_byte):
    ser.write(serial.to_bytes([one_byte]))

# Recive data
def RX_data(ser):
    # if ser.inWaitting() > 0: inWaitting -> in_waiting changed in version 3.0
    # Return the number of bytes in the receive buffer
    if ser.in_waiting() > 0:
        # read(1) == 1 byte data read from the port
        result = ser.read(1)
        RX = ord(result)
        return RX
    else:
        return 0

# Main Function
# This code is not excute when imported other code
if __name__ == '__main__':
    TX_data(serial_port, 128)

    time.sleep(1)

    print("Return DATA : " + str(RX_data(serial_port)))

    exit(1)