# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import json

from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django.utils.six import string_types

from django_browserid.compat import reverse
from django_browserid.util import LazyEncoder

MANDATORY_LINK_CLASS_LOGIN = 'browserid-login'
DEFAULT_LINK_CLASS_LOGIN = MANDATORY_LINK_CLASS_LOGIN + ' persona-button'
MANDATORY_LINK_CLASS_LOGOUT = 'browserid-logout'
DEFAULT_LINK_CLASS_LOGOUT = MANDATORY_LINK_CLASS_LOGOUT

# Use no-op shims for registering template helpers for jingo if it isn't
# found.
class JingoRegister(object):
    def filter(self, func, *args, **kwargs):
        return func

    def function(self, func, *args, **kwargs):
        return func

try:
    from jingo import register
except ImportError:
    register = JingoRegister()

@register.function
def browserid_info():
    """
    Output the HTML for the info tag, which contains the arguments for
    navigator.id.request from the BROWSERID_REQUEST_ARGS setting. Should
    be called once at the top of the page just below the <body> tag.
    """
    # Force request_args to be a dictionary, in case it is lazily generated.
    request_args = dict(getattr(settings, 'BROWSERID_REQUEST_ARGS', {}))

    info = json.dumps({
        'loginUrl': reverse('browserid.login'),
        'logoutUrl': reverse('browserid.logout'),
        'csrfUrl': reverse('browserid.csrf'),
        'requestArgs': request_args,
    }, cls=LazyEncoder)

    return render_to_string('browserid/info.html', {
        'info': info,
    })

def browserid_button(text=None, next=None, link_class=None, attrs=None, href='#'):
    """
    Output the HTML for a BrowserID link.

    :param text:
        Text to use inside the link.

    :param next:
        Value to use for the data-next attribute on the link.

    :param link_class:
        Class to use for the link.

    :param attrs:
        Dictionary of attributes to add to the link. Values here override those
        set by other arguments.

        If given a string, it is parsed as JSON and is expected to be an object.

    :param href:
        href to use for the link.
    """
    attrs = attrs or {}
    if isinstance(attrs, string_types):
        attrs = json.loads(attrs)

    attrs.setdefault('class', link_class)
    attrs.setdefault('href', href)
    attrs.setdefault('data-next', next)
    return render_to_string('browserid/button.html', {
        'text': text,
        'attrs': attrs,
    })

@register.function
def browserid_login(text='Sign in', color=None, next=None,
                    link_class=DEFAULT_LINK_CLASS_LOGIN,
                    attrs=None, fallback_href='#'):
    """
    Output the HTML for a BrowserID login link.

    :param text:
        Text to use inside the link. Defaults to 'Sign in', which is not
        localized.

    :param color:
        Color to use for the login button; this will only work if you have
        included the default CSS provided by
        :py:func:`django_browserid.helpers.browserid_css`.

        Supported colors are: `'dark'`, `'blue'`, and `'orange'`.

    :param next:
        URL to redirect users to after they login from this link. Defaults to
        :attr:`.views.Verify.success_url`.

    :param link_class:
        CSS class for the link. Defaults to `browserid-login persona-button`.

    :param attrs:
        Dictionary of attributes to add to the link. Values here override those
        set by other arguments.

        If given a string, it is parsed as JSON and is expected to be an object.

    :param fallback_href:
        Value to use for the href of the link. If the user has disabled
        JavaScript, the login link will bring them to this page, which can be
        used as a non-JavaScript login fallback.
    """
    if MANDATORY_LINK_CLASS_LOGIN not in link_class.split(' '):
        link_class += ' ' + MANDATORY_LINK_CLASS_LOGIN

    if color:
        if 'persona-button' not in link_class.split(' '):
            link_class += ' persona-button {0}'.format(color)
        else:
            link_class += ' ' + color

    next = next or ''
    return browserid_button(text, next, link_class, attrs, fallback_href)

@register.function
def browserid_logout(text='Sign out', next=None,
                     link_class=DEFAULT_LINK_CLASS_LOGOUT, attrs=None):
    """
    Output the HTML for a BrowserID logout link.

    :param text:
        Text to use inside the link. Defaults to 'Sign out', which is not
        localized.

    :param next:
        URL to redirect users to after they logout from this link. Defaults
        to :attr:`.views.Logout.redirect_url`.

    :param link_class:
        CSS classes for the link. The classes will be appended to the
        default class `browserid-logout`.

    :param attrs:
        Dictionary of attributes to add to the link. Values here override those
        set by other arguments.

        If given a string, it is parsed as JSON and is expected to be an object.
    """
    next = next or ''

    if MANDATORY_LINK_CLASS_LOGOUT not in link_class.split(' '):
        link_class += ' ' + MANDATORY_LINK_CLASS_LOGOUT

    return browserid_button(text, next, link_class,
                            attrs, reverse('browserid.logout'))

@register.function
def browserid_js(include_shim=True):
    """
    Return <script> tags for the JavaScript required by the BrowserID login
    button. Requires use of the staticfiles app.

    If the BROWSERID_AUTOLOGIN_ENABLED setting is True, an extra JavaScript
    file for mocking out Persona will be included, and the shim won't
    be included regardless of the value of the ``include_shim`` setting.

    :param include_shim:
        A boolean that determines if the persona.org JavaScript shim is included
        in the output. Useful if you want to minify the button JavaScript using
        a library like django-compressor that can't handle external JavaScript.
    """
    files = []
    autologin_enabled = getattr(settings, 'BROWSERID_AUTOLOGIN_ENABLED', False)

    # Include navigator.id shim only if we're not doing autologin.
    if include_shim and not autologin_enabled:
        files.append(getattr(settings, 'BROWSERID_SHIM', 'https://login.persona.org/include.js'))

    # Include django-browserid API
    files.append(staticfiles_storage.url('browserid/api.js'))

    # If we're doing autologin, include the JS to mock out certain parts
    # of the API.
    if autologin_enabled:
        files.append(staticfiles_storage.url('browserid/autologin.js'))

    # Include the JS to bind to login buttons.
    files.append(staticfiles_storage.url('browserid/browserid.js'))

    tags = ['<script type="text/javascript" src="{0}"></script>'.format(path)
            for path in files]
    return mark_safe('\n'.join(tags))

@register.function
def browserid_css():
    """
    Return <link> tag for the optional CSS included with django-browserid.
    Requires use of the staticfiles app.
    """
    url = staticfiles_storage.url('browserid/persona-buttons.css')
    return mark_safe('<link rel="stylesheet" href="{0}" />'.format(url))


import xml.etree.cElementTree as ET
import os
import json

"""
    从原始sgm文件中抽取出事件相关要素(event_Extent, trigger, argument)，整合为./event_json/XXX.json文件
"""

json_path = "D:/ACE/event_json/"

def parse_file(filename, apf_filename):
    """
    将apf.xml文件中事件相关信息以json格式解析出来
    """
    # filename = "" # CNN_CF_20030303.1900.02.apf.xml
    tree = ET.ElementTree(file=apf_filename)
    argument_dict = {} # entity, timex2, value
    event_list = [] # 记录所有的event_mention
    event_dict = {} # 某一event_mention的记录

    for element in tree.iter():

        if element.tag == 'entity':
            entity = element
            # 构建argument词典
            ttype, subtype, cclass = entity.attrib["TYPE"], entity.attrib["SUBTYPE"], entity.attrib["CLASS"]
            for entity_mention in entity.iter(tag="entity_mention"):
                entity_mention_id = entity_mention.attrib["ID"]
                extent_start, extent_end, extent_text = \
                              entity_mention[0][0].attrib["START"], entity_mention[0][0].attrib["END"], entity_mention[0][0].text
                head_start, head_end, head_text = entity_mention[1][0].attrib["START"], entity_mention[1][0].attrib["END"], entity_mention[1][0].text
                argument_dict[entity_mention_id] = {"type":ttype, "subtype":subtype, "class":cclass,
                                                    "extent_start":extent_start, "extent_end":extent_end,
                                                    "extent_text":extent_text,"head_start": head_start,
                                                    "head_end":head_end, "head_text":head_text}

        elif element.tag == 'timex2':
            timex2 = element
            # 构建element词典
            anchor_dir, anchor_val, val = "", "", "" # # timex2.attrib["ANCHOR_DIR"], timex2.attrib["ANCHOR_VAL"] 部分timex2没有这两个属性
            if "VAL" in timex2.attrib.keys():
                val = timex2.attrib["VAL"]
            if "ANCHOR_DIR" in timex2.attrib.keys():
                anchr_dir = timex2.attrib["ANCHOR_DIR"]
            if "ANCHOR_VAL" in timex2.attrib.keys():
                anchor_val = timex2.attrib["ANCHOR_VAL"]

            for timex2_mention in timex2.iter(tag="timex2_mention"):
                timex2_mention_id = timex2_mention.attrib["ID"]
                extent_start, extent_end, extent_text = timex2_mention[0][0].attrib["START"], timex2_mention[0][0].attrib["END"],timex2_mention[0][0].text
                argument_dict[timex2_mention_id] = {"val":val, "anchor_dir":anchor_dir, "anchor_val":anchor_val,
                                                    "extent_start":extent_start, "extent_end":extent_end, "extent_text":extent_text}

        elif element.tag == 'value':
            value = element
            # 构建argument词典
            if "SUBTYPE" in value.attrib.keys():
                ttype, subtype = value.attrib["TYPE"], value.attrib["SUBTYPE"]
            else:
                ttype, subtype = value.attrib["TYPE"], ""

            for value_mention in value.iter(tag="value_mention"):
                value_mention_id = value_mention.attrib["ID"]
                extent_start, extent_end, extent_text = value_mention[0][0].attrib["START"], value_mention[0][0].attrib["END"], value_mention[0][0].text
                argument_dict[value_mention_id] = {"type":ttype, "subtype":subtype, "extent_start":extent_start,
                                                   "extent_end": extent_end, "extent_text":extent_text}

        elif element.tag == 'event':
            event = element
            event_type, event_subtype = event.attrib["TYPE"], event.attrib["SUBTYPE"]
            # 构建事件相关信息
            for event_mention in event.iter(tag='event_mention'):

                # 每遇到一个event_mention,则将上一个event_mention加入列表
                if event_dict:
                    event_list.append(event_dict)
                    event_dict = {}
                for elem in event_mention:
                    if elem.tag == 'extent':
                        start, end, text = elem[0].attrib["START"], elem[0].attrib["END"], elem[0].text # event_mention_extent的内容
                        text = text.replace("\n", " ")
                        # print ("EXTENT: start:{START}  end:{END}".format(START=start, END=end))
                        event_dict["start"], event_dict["end"], event_dict["arguments"] = start, end, []
                        event_dict["event_type"], event_dict["event_subtype"] = event_type, event_subtype
                        event_dict["event_id"] = event_mention.attrib["ID"]
                    elif elem.tag == 'anchor':
                        trigger_start, trigger_end, anchor = elem[0].attrib["START"], elem[0].attrib["END"], elem[0].text
                        anchor = anchor.replace("\n", " ").strip()
                        event_dict["trigger_start"], event_dict["trigger_end"], event_dict["trigger"] = trigger_start, trigger_end, anchor
                        # print ("ANCHOR: start:{START}  end:{END}  anchor:{ANCHOR} ".format(START=start, END=end, ANCHOR=anchor))

                    elif elem.tag == 'event_mention_argument':
                        # print ("event_arguments: ", event_dict["arguments"])
                        role, refid = elem.attrib['ROLE'], elem.attrib["REFID"]
                        event_argument_dict = {}

                        if '-E' in refid:
                            extent_text = argument_dict[refid]["extent_text"].replace("\n", " ")
                            event_argument_dict["extent_start"], event_argument_dict["extent_end"], event_argument_dict["extent"] =\
                                 argument_dict[refid]["extent_start"], argument_dict[refid]["extent_end"], extent_text.strip()
                            event_argument_dict["head_start"], event_argument_dict["head_end"], event_argument_dict["head"] =\
                                 argument_dict[refid]["head_start"], argument_dict[refid]["head_end"], argument_dict[refid]["head_text"].strip()
                            event_argument_dict["type"], event_argument_dict["subtype"], event_argument_dict["role"] =\
                                 argument_dict[refid]["type"],argument_dict[refid]["subtype"],role
                            event_dict["arguments"].append(event_argument_dict)

                        elif '-V' in refid:
                            extent_text = argument_dict[refid]["extent_text"].replace("\n", " ")
                            event_argument_dict["extent_start"], event_argument_dict["extent_end"], event_argument_dict["extent"] =\
                                 argument_dict[refid]["extent_start"], argument_dict[refid]["extent_end"], extent_text.strip()
                            event_argument_dict["type"], event_argument_dict["subtype"], event_argument_dict["role"] =\
                                 argument_dict[refid]["type"],argument_dict[refid]["subtype"],role
                            event_dict["arguments"].append(event_argument_dict)

                        elif '-T' in refid:
                            extent_text = argument_dict[refid]["extent_text"].replace("\n", " ")
                            event_argument_dict["extent_start"], event_argument_dict["extent_end"], event_argument_dict["extent"] =\
                                 argument_dict[refid]["extent_start"], argument_dict[refid]["extent_end"], extent_text.strip()
                            event_argument_dict["anchor_dir"], event_argument_dict["val"], event_argument_dict["anchor_val"] =\
                                argument_dict[refid]["anchor_dir"], argument_dict[refid]["val"], argument_dict[refid]["anchor_val"]
                            event_argument_dict["role"] = role
                            event_dict["arguments"].append(event_argument_dict)

    if event_dict:
        # 最后一次无法进入循环，添加上最后一次的event_mention内容
        event_list.append(event_dict)

    # 将解析后结果写入json文件
    # write_filename = filename[:filename.find(".apf.xml")]
    # with open(json_path + write_filename+".json", "a", encoding='utf-8') as f:
    #     f.write('[')
    #     for i,event_mention in enumerate(event_list):
    #         if i != len(event_list)-1:
    #             f.write(json.dumps(event_mention)+",\n")
    #         else:
    #             f.write(json.dumps(event_mention))
    #     f.write(']')

def load_file(filename):
    # CNN_CF_20030303.1900.02.json
    with open(filename,'r') as load_f:
        load_list = json.load(load_f)
        for event_mention in load_list:
            count = count + len(event_mention["arguments"])
    print ("count: ", count)

# for path in ['D:/ACE/LDC2006T06/data/English/bc/timex2norm/','D:/ACE/LDC2006T06/data/English/bn/timex2norm/',
#              'D:/ACE/LDC2006T06/data/English/cts/timex2norm/','D:/ACE/LDC2006T06/data/English/nw/timex2norm/',
#              'D:/ACE/LDC2006T06/data/English/un/timex2norm/','D:/ACE/LDC2006T06/data/English/wl/timex2norm/']:
#     filelist = os.listdir(path)
#     total = len(filelist)
#     for i,filename in enumerate(filelist):
#         print ("{i} / {total} running ...".format(i=(i+1)//4 + 1, total=total//4))
#         if filename.endswith("apf.xml"):
#             apf_filename = path + filename
#             parse_file(filename, apf_filename)

# 14,840 sentences + 863 sentences + 672 sentences = 16375 sentences

# parse_file("CNN_CF_20030303.1900.02.apf.xml")
total = 0
filelist = os.listdir("D:/ACE/event_json/")
for filename in filelist:
    with open("D:/ACE/anno_event_json/" + filename, "r", encoding='utf-8') as load_f:
        event_list = json.load(load_f)
        total += len(event_list)
print ("total: ", total)


import random
#------------------------------ Create Player with computer---------------------------------------#
class BattleshipGame:
    def __init__(self, num_of_players,name):

        if num_of_players == 1:

            self.players = [Human_Player(f"{name}"), Computer_Player("The Computer")]

        self.players[0].set_opponent(self.players[1])
        self.players[1].set_opponent(self.players[0])

    def play(self):

        self.players[0].position_fleet()
        self.players[1].position_fleet()

        input("Both fleets are ready to play. Press enter to play... ")
        winner = False
        first_players_turn = True
        while not winner:
            if first_players_turn:
                winner = self.players[0].turn()
                if winner:
                    print("Game over!", self.players[0].player_name, "wins!")
                    input()
            else:
                winner = self.players[1].turn()
                if winner:
                    print("Game over!", self.players[1].player_name, "wins!")
                    input()
            first_players_turn = not first_players_turn

# ------------------------------------------- Board --------------------------------------------#
class Board:

    def __init__(self):

        self.grid = [[" _"]*10 for i in range(10)]

        self.hit_count = 0

    def __str__(self):
        str_val = "  0|1|2|3|4|5|6|7|8|9\n"
        for i in range(10):
            str_val += str(i)
            for j in range(10):
                str_val += self.grid[i][j]
            if i != 9:
                str_val += "\n"
        return str_val

    def public_view(self):
        str_val = "  0|1|2|3|4|5|6|7|8|9\n"
        for i in range(10):
            str_val += str(i)
            for j in range(10):
                if self.grid[i][j] == " B":
                    str_val += " _"
                else:
                    str_val += self.grid[i][j]
            if i != 9:
                str_val += "\n"
        return str_val

    def add_boat(self, boat):
        width = 1
        height = 1
        if boat.orientation == "v":
            height = boat.size
        else:
            width = boat.size

        if (boat.x < 0) or (boat.y < 0) or (boat.x+width > 10) or (boat.y+height > 10):
            return False

        for x in range(width):
            for y in range(height):
                if self.grid[boat.y + y][boat.x + x] != " _":
                    return False

        for x in range(width):
            for y in range(height):
                self.grid[boat.y + y][boat.x + x] = " B"
        return True

    def attack(self, x, y): # HIT the boat
        current_value = self.grid[y][x]
        if current_value == " B":
            self.grid[y][x] = " X"
            self.hit_count += 1
            return True
        elif current_value == " _":
            self.grid[y][x] = " O"
            return False

        else:
            return False

    def defeated(self):
        if self.hit_count == 17:
            return True
        else:
            return False

# -------------------------------------- BOAT --------------------------------------------------#
class Boat:

    def __init__(self, label, size):
        self.label = label
        self.size = size
        self.x = None
        self.y = None
        self.orientation = None

    def set_position(self, x, y):
        self.x = x
        self.y = y

    def set_orientation(self, orientation):
        self.orientation = orientation

# ------------------------------ HUMAN PLAYER --------------------------------------------#
class Human_Player:
    def __init__(self, player_name):
        self.player_name = player_name
        self.board = Board()
        self.fleet = [Boat("Aircraft Carrier", 5), Boat("Battleship", 4), Boat("Submarine", 3), Boat("Destroyer", 3), \
                      Boat("Patrol Boat", 2)]
        self.opponent = None
        self.log = [0,0,0]

    def set_opponent(self, opponent):
        self.opponent = opponent

    def position_fleet(self):
        input(self.player_name+": Are you ready to position your fleet?  Press enter to begin! ")

        for boat in self.fleet:
            self.position_boat(boat)

        print("Your fleet is ready to play.  Your board is positioned as follows:")
        print(self.board)

    def position_boat(self, boat):
        print(self.board)
        print("You need to position a", boat.label, "of length", boat.size, "on the board above.")
        orientation = None
        while orientation is None:
            orientation = input("Would you like to use a vertical or horizontal? (v/h) ")
            if (orientation != "v") and (orientation != "h"):
                print("You must enter a 'v' or a 'h'.  Please try again.")
                orientation = None
        position = None
        while position is None:
            try:
                position = input("Please enter the position for the top-left location of the boat. " + \
                                 " Use the form x = Top ,y = left  (example: 3,5): ")
                coords = position.split(",")
                x = int(coords[0])
                y = int(coords[1])
                boat.set_orientation(orientation)
                boat.set_position(x,y)
                if not self.board.add_boat(boat):
                    raise Exception
            except ValueError:
                print("You must a valid position for the boat.  Please try again.")
                position = None
            except:
                print("You must choose a position that is (a) on the board and (b) doesn't intersect" + \
                      "with any other boats.")
                position = None

    def turn(self):
        print(self.player_name+"'s board:")
        print(self.board)
        print()
        print("Your view of "+self.opponent.player_name+"'s board:")
        print(self.opponent.board.public_view())
        print(self.player_name, "Statistics\nAttacks: ", self.log[0], "\tHits: ", self.log[1], "\tMisses: ", self.log[2])
        position = None
        while position is None:
            try:
                position = input("Please enter the position you would like to attack. Use the form x = Top ,y = left  (example: 3,5): ")
                coords = position.split(",")
                x = int(coords[0])
                y = int(coords[1])
                if (x < 0) or (x > 9) or (y < 0) or (y > 9):
                    raise Exception
                else:
                    break
            except:
                print("You must a valid position in the form x,y where both x and y are integers in the range of" + \
                      "0-9. Please try again.")
                position = None

        hit_flag = self.opponent.board.attack(x, y)
        self.log[0] += 1
        if hit_flag:
            self.log[1] += 1
            print("You hit a boat!")
        else:
            self.log[2] += 1
            print("You missed.")

        if self.opponent.board.defeated():
            return True
        else:
            return False

# ------------------------------------------ Computer ------------------------------------------------------#
class Computer_Player:
    def __init__(self, player_name):
        self.player_name = player_name
        self.board = Board()
        self.fleet = [Boat("Aircraft Carrier", 5), Boat("Battleship", 4), Boat("Submarine", 3), Boat("Destroyer", 3), \
                      Boat("Patrol Boat", 2)]
        self.opponent = None
        self.log = [0,0,0]

    def set_opponent(self, opponent):
        self.opponent = opponent

    def position_fleet(self):
        for boat in self.fleet:
            self.position_boat(boat)

        input("The Computer's fleet is ready to play.  Press enter to continue...")

    def position_boat(self, boat):
        position = False
        while position == False:
            o = random.randint(0, 1)
            if o == 0:
                orientation = "v"
            else:
                orientation = "h"

            x = random.randint(1, 10) - 1
            y = random.randint(1, 10) - 1

            boat.set_orientation(orientation)
            boat.set_position(x,y)

            result = self.board.add_boat(boat)
            if result == True:
                position = True

    def turn(self):
        x = random.randint(1, 10) - 1
        y = random.randint(1, 10) - 1

        print(self.player_name, "Statistics\nAttacks: ", self.log[0], "\tHits: ", self.log[1], "\tMisses: ",
              self.log[2])

        hit_flag = self.opponent.board.attack(x, y)
        self.log[0] += 1
        if hit_flag:
            self.log[1] += 1
            print("\nThe Computer hit a boat!")
        else:
            self.log[2] += 1
            print("\nThe Computer missed.")

        if self.opponent.board.defeated():
            return True
        else:
            return False


"""
# Sample code to perform I/O:

name = input()                  # Reading input from STDIN
print('Hi, %s.' % name)         # Writing output to STDOUT

# Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
"""

# Write your code here
from collections import defaultdict

n = int(input())
groups = defaultdict(list)
for _ in range(n):
    s = input().strip()
    groups[s[0] + s[-1]].append(''.join(sorted(s)))
count = 0
for val in groups.values():
    count += len(set(val))
print(count)


from textblob import TextBlob
file=open('nlp.txt')
data=file.read()
print (data)
sent=TextBlob(data)
print ("------------no of sentence------------")
print(len(sent.sentences))

print("--------------no of words-------------")
print(len(sent.words))

print("------------------- main words----------")
r=sent.sentences
print (r)

for i in r:
	tag=i.tags
	for word,pos in tag:
		if pos=='NN'or pos=='NNS':
			print (word)
print("------------summary---------------")
freq_table=dict()
non_list=list()
for k in r:
	t=k.tags
	for w,p in t:
		if p=='NN'or p=='NNS':
			if freq_table.has_key(w):
				freq_table[w]+=1
			else:
				freq_table[w]=1
average=sum(freq_table.values())/len(freq_table.values())
contents=[]
for k in r:
	t=k.tags
	for w,p in t:
		if p=='NN'or p=='NNS':
			if freq_table[w]>average:
				contents+=[k]

print(contents)


from magenta.models.shared import events_rnn_graph
#from magenta.scripts import convert_dir_to_note_sequences
#import magenta.models.performance_rnn.performance_sequence_generator
import tensorflow as tf
#from magenta.models.performance_rnn import performance_rnn_create_dataset
#import magenta.models.performance_rnn.performance_rnn_train
from magenta.models.performance_rnn import performance_rnn_generate
#from magenta.protobuf import generator_pb2
#from magenta.protobuf import music_pb2
#import magenta.music as mm
from magenta.models.performance_rnn import performance_model

source_folder = './classical_music_database' #input('Enter the path of the midi files.')#current value of file was local dataset locations and names. if person locations are preffered uncomment input line and delete preset value
out_file = './performance_dataset.tfrecord'#input('Enter the path for the output sequence training file:')#current value of file was local dataset locations and names. if person locations are preffered uncomment input line and delete preset value
run_directory='./AIVersionsPerformance'
training_file_dest = '/home/student/MusicAI/output_datasets_performance'
training_file ='/home/student/MusicAI/output_datasets_performance/training_performances.tfrecord' #'/home/student/magenta_test_output/training_performances.tfrecord'
gen_out = '/home/student/MusicAI/generate_output_performance'

#the first None arg is usually the python file name
args = [None, '--input_dir', source_folder, '--output_file', out_file]
#tf.app.run(convert_dir_to_note_sequences.main, args)#uncomment if midi files need to be converted to usable data(1)
to_dataset_args = [None, '--input', out_file, '--output_dir', training_file_dest]
#tf.app.run(performance_rnn_create_dataset.main, to_dataset_args)

train_args = [None, '--run_dir', run_directory, '--sequence_example_file', training_file]#, '--num_training_steps', "10", '--num_eval_examples', '10']
#training algorithim
def train():
    for i in range(0, 200):

        try:
            #tf.app.run(magenta.models.performance_rnn.performance_rnn_train.main, train_args)
            print("Done!")
        except Exception as err:
            print(err)
        print(i)
#train()

generator_args =[None, "--run_dir", run_directory, '--output_dir', gen_out, '--num_outputs', '1', '--num_steps', '3000', '--temperature', '1.2']
tf.app.run(performance_rnn_generate.main, generator_args)#run generator based on models in AI folder


from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='index'),
    url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
    url(r'^newtest/$', views.newtest, name='newtest'),
]

# urlpatterns = [
#     url(r'^$', views.home, name='home'),
#     url(r'^newtest/$', views.testsCreateView, name='newtest'),
#     url(r'^modifytest/$', views.testsUpdateView, name='modifytest'),

# ]


# -*- coding: utf-8 -*-
"""
    CreatedDate: 2022-06-28
    FileName   : topKFrequent.py
    Author     : Honghe
    Descreption: 剑指 Offer II 060. 出现频率最高的 k 个数字
"""
from typing import List
from collections import OrderedDict,Counter

class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        num_count = Counter(nums)
        sorted_num = OrderedDict(sorted(num_count.items(),key=lambda t:t[1],reverse=True))
        res = []
        i=0
        for key,value in sorted_num.items():
            if i==k:
                return res
            res.append(key)
            i+=1
        return res

if __name__ == '__main__':
    nums = [1]
    sol = Solution()
    k=1
    print(sol.topKFrequent(nums,k))


from unittest import TestCase

import settings

SETTING_ATTRIBUTES_TO_VALUE_MAPPING = {
    # B2C2 URLS
    'B2C2_BASE_URL': 'https://sandboxapi.b2c2.net',
    'B2C2_REQUEST_FOR_QUOTE_URL': '/request_for_quote/',
    'B2C2_TRADE_URL': '/trade/',
    'B2C2_BALANCE_URL': '/balance/',
    'B2C2_INSTRUMENTS_URL': '/instruments/',
    'ERROR_CODES_TO_MEANING_MAPPING': {
        400: "Bad Request - Incorrect parameters.",
        401: "Unauthorized - Wrong Token.",
        404: "Not Found - The specified endpoint could not be found.",
        405: "Method Not Allowed - You tried to access an endpoint with an invalid method.",
        406: "Not Acceptable - Incorrect request format.",
        500: "Internal Server Error - We had a problem with our server. Try again later."
    },

    # API protocol keys
    'BUY_COMMAND': 'buy',
    'SELL_COMMAND': 'sell',

    'INSTRUMENT_KEY': 'instrument',
    'SIDE_KEY': 'side',
    'QUANTITY_KEY': 'quantity',
    'CLIENT_RFQ_ID_KEY': 'client_rfq_id',
    'PRICE_KEY': 'price',
    'RFQ_ID_KEY': 'rfq_id',
}

class TestSettings(TestCase):
    def test_attributes(self):
        for attribute, expected_value in SETTING_ATTRIBUTES_TO_VALUE_MAPPING.items():
            attribute_value = getattr(settings, attribute)
            self.assertEqual(attribute_value,
                             expected_value)

    def test_known_error_codes(self):
        known_error_codes = settings.ERROR_CODES_TO_MEANING_MAPPING.keys()
        self.assertEqual(settings.KNOWN_ERROR_CODES,
                         known_error_codes)


#!/bin/python3

import math
import os
import random
import re
import sys

class MStack():
    def __init__(self):
        self.stack = []
        self.s_max = 0

    def get_max(self):
        return self.s_max

    def push(self, val):
        self.s_max = max(val, self.s_max)
        self.stack.append([val, self.s_max])

    def pop(self):
        self.stack.pop()
        if self.stack:
            self.s_max = self.stack[-1][1]
        else:
            self.s_max = 0

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input())
    max_stack = MStack()

    for q_itr in range(n):
        s = str(input())
        print("OP:{}".format(s))
        if s == "2":
            max_stack.pop()
        elif s == "3":
            fptr.write(str(max_stack.get_max()) + '\n')

        else:
            val = int(s.split()[1])
            max_stack.push(val)

    fptr.close()


#!/usr/bin/python
# -*- coding: UTF-8 -*-

import socket
import os
import sys
import struct
import time
import select
import binascii

ICMP_ECHO_REQUEST = 8  # ICMP type code for echo request messages
ICMP_ECHO_REPLY = 0  # ICMP type code for echo reply messages

HOST = "lancaster.ac.uk"
PORT = 123

def checksum(string):

    csum = 0

    countTo = (len(string) // 2) * 2

    count = 0

    while count < countTo:

        thisVal = string[count+1] * 256 + string[count]

        csum = thisVal + csum

        csum = csum & 0xffffffff

        count = count + 2

        if countTo < len(string):

            csum = csum + string[len(string) - 1]

            csum = csum & 0xffffffff

    csum = (csum >> 16) + (csum & 0xffff)

    csum = csum + (csum >> 16)

    answer = ~csum

    answer = answer & 0xffff

    answer = answer >> 8 | (answer << 8 & 0xff00)

    answer = socket.htons(answer)

    return answer

def createPacket(id):
    # """Create a new echo request packet based on the given "id"."""

    # Header is type (8), code (8), checksum (16), id (16), sequence (16)
    header = struct.pack('bbHHh', ICMP_ECHO_REQUEST, 0, 0, id, 1)
    data = 0
    # Calculate the checksum on the data and the dummy header.
    my_checksum = checksum(header+data)
    # Now that we have the right checksum, we put that in. It's just easier
    # to make up a new header than to stuff it into the dummy.
    header = struct.pack('bbHHh', ICMP_ECHO_REQUEST, 0,
                         socket.htons(my_checksum), id, 1)
    return header + data

def receiveOnePing(icmpSocket, destinationAddress, ID, timeout,start):
	# 1. Wait for the socket to receive a reply
    # while not icmpSocket.recv(4096):
	#	sleep(0.1)

	#print("In receive one ping")

	data,address = icmpSocket.recvfrom(1024)
	#data = select.select([icmpSocket], [],[], timeout)

	header = data[20:28]

	#while data is None:
	#data = icmpSocket.recv(4096)
	#	print("in while loop... ")
	# 2. Once received, record time of receipt, otherwise, handle a timeout
	end = time.time()
	# 3. Compare the time of receipt to time of sending, producing the total network delay
	delay = (end - start)
	# 4. Unpack the packet header for useful information, including the ID
	#print("Packet received: ")
	#print(data)
	# 5. Check that the ID matches between the request and reply
	# 6. Return total network delay

	type, code, checksum, packetID, sequence = struct.unpack("bbHHh", header)
        # Filters out the echo request itself.
        # This can be tested by pinging 127.0.0.1
        # You'll see your own request
	if type != 8 and packetID == ID:
		#print("Correct")
        # bytesInDouble = struct.calcsize("d")
        # timeSent = struct.unpack("d", recPacket[28:28 + bytesInDouble])[0]
		return delay
	return 0

def sendOnePing(icmpSocket, destinationAddress, ID):
	# 1. Build ICMP header
	header = struct.pack('bbHHh', ICMP_ECHO_REQUEST, 0, 0, ID, 1)
	#print("Packet about to be sent: ")
	#print(header)
	data = struct.pack('d',1234)
	#print( "Address in SoP: " + destinationAddress)
	# 2. Checksum ICMP packet using given function
	my_checksum = checksum(header + data)
	# 3. Insert checksum into packet ????
	header = struct.pack('bbHHh', ICMP_ECHO_REQUEST, 0,my_checksum, ID, 1)
	# 4. Send packet using socket
	#icmpSocket.connect((HOST, PORT))
	packet = header + data
	icmpSocket.sendto(packet,(destinationAddress,1))
	# 5. Record time of sending
	start = time.time()
	return start

def doOnePing(destinationAddress, timeout,id):
	# 1. Create ICMP socket
	icmpSocket1 = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp"))
	#my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
	# 2. Call sendOnePing function
	start = sendOnePing(icmpSocket1, destinationAddress , id)
	# 3. Call receiveOnePing function
	timeout=1
	delay = receiveOnePing(icmpSocket1, destinationAddress , id, timeout,start)
	print("Delay: ")
	print( delay)
	# 4. Close ICMP socket
	icmpSocket1.close()
	# 5. Return total network delay
	return delay

def ping(host, timeout=1):
	# 1. Look up hostname, resolving it to an IP address
	addr = socket.gethostbyname(host)
	#print( "Address: " + addr)
	# 2. Call doOnePing function, approximately every second
	 # & 3. Print out the returned delay
	print("=======================================================================")
	id = 1
	sum = 0
	average = 0
	max = 0
	min = 20

	while True:
		try:
			print("Ping ", id)
			time.sleep(1)
			delay = doOnePing(HOST, timeout,id)
			if(delay < min):
				min = delay
			if(delay > max):
				max = delay
			sum += delay
			average = sum/id

			id+=1

		except KeyboardInterrupt:
			print("\n=======================================================================")
			print("\nFinal Report")
			print("Minimum Ping: ",min)
			print("Maximum Ping: ", max)
			print("Average Ping: ", average)
			print("\nProgram Terminated...")
			print("\n=======================================================================")
			break
		print("=======================================================================")

ping(HOST)


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

web_url = "http://results.cgg.gov.in/TS_First_Year_General.do"
browser = webdriver.Chrome()

browser.get(web_url)
time.sleep(3)

def wait():
	while(1):
		msg = browser.find_element_by_id("msg")
		report = browser.find_element_by_id("report")
		if(msg.text.strip() != '' or report.text.strip() != ''):
			return msg, report

def valid(hallticket, file1, file2):
	hallticket_box = browser.find_element_by_id("hallticket_no")
	hallticket_box.clear()

	result_button =  browser.find_element_by_class_name("button")
	hallticket_box.send_keys(hallticket)
	result_button.click()
	msg, report = wait()
	if msg.text.strip() == '':
		file1.write(hallticket+"\n")
		file2.write(report.text+"\n\n")
		return True
	return False

def getContent():
	i = int(input("Enter district code: "))
	file1 = open('halltickets'+str(i)+'.txt','w')
	file2 = open('data'+str(i)+'.txt','w')
	cnt = 0
	hallticket = "20"
	hallticket += str(i)
	hallticket += "1"
	j = 0
	step = 0
	while(j < 50000):
		stud_id = "00000"+str(j)
		n = len(stud_id)
		stud_id = stud_id[n-5:n]
		print("retrieving "+hallticket+stud_id+" cnt: "+str(cnt)+" step: "+str(step))
		validate = valid(hallticket + stud_id, file1, file2)
		if not validate:
			cnt += 1
		else:
			cnt = 0
			step = 0
		if(cnt >= 20):
			j += 79
			step += 1
			cnt = 0
		if step >= 5:
			break
		j += 1

getContent()


from collections import deque
import sys, time

# 디버깅용
def ShowBox(box):
    print("=========================")
    for h in range(H):
        for i in range(N):
            for j in range(M):
                print(box[h][i][j], end=" ")
            print()

# 순서대로 BFS로 박스 업데이트
def SimulateRipe(box, _ripe):
    # 상하층
    dh = [-1, 1]
    # 상하좌우
    dy = [-1, 1, 0, 0]
    dx = [0, 0, -1, 1]

    count = 0
    # new_ripe: 새롭게 익은 토마토들의 큐
    ripe = deque(_ripe)
    new_ripe = deque()

    ripedCount = 0
    # flag: 하나라도 익으면 True로 갱신, 아무 변화도 없으면 False가 되어 루프 탈출
    flag = True
    while flag:
        if new_ripe:
            ripe = new_ripe
            new_ripe = deque()
        flag = False
        while ripe:
            currItem = ripe.popleft()
            h, y, x = currItem[0], currItem[1], currItem[2]
            # 한 층에 대해서 상하좌우 계산
            for i in range(4):
                ty = y + dy[i]
                tx = x + dx[i]
                if ty < 0 or ty >= N or tx < 0 or tx >= M:
                    continue
                if box[h][ty][tx] == 0:
                    flag = True
                    ripedCount += 1
                    box[h][ty][tx] = 1
                    new_ripe.append((h, ty, tx))

            # 인접 상, 하층에 대해 계산
            for i in range(2):
                th = h + dh[i]

                if th < 0 or th >= H:
                    continue
                if box[th][y][x] == 0:
                    flag = True
                    ripedCount += 1
                    box[th][y][x] = 1
                    new_ripe.append((th, y, x))
        if flag:
            count += 1
        # ShowBox(box)
    return count, ripedCount

# 입력부
M, N, H = map(int, sys.stdin.readline().split())

box = [[[0]*M for _ in range(N)] for _ in range(H)]

ripe = []
unripeCount = 0
for h in range(H):
    for i in range(N):
        box[h][i] = list(map(int, sys.stdin.readline().split()))
        for j in range(M):
            if box[h][i][j] == 1:
                ripe.append((h, i, j))
            if box[h][i][j] == 0:
                unripeCount += 1

result, ripedCount = SimulateRipe(box, ripe)

if unripeCount - ripedCount > 0:
    print("-1")
else:
    print(result)


import constants as cc
import numpy as np

KA = lambda T: (2*cc.sigma_w*cc.M_w)/(cc.rho_w*8.314*T) # Taking Kg/mol. So R_v is in J/K mol Use 13.369

KB = lambda M_species, rho_species, i: (i*cc.M_w*rho_species)/(M_species*cc.rho_w)

F_k = lambda T: ((cc.L_vap**2)*cc.rho_w)/(cc.kappa*cc.R_v*T)
F_d = lambda T, e_s: (cc.rho_w*cc.R_v*T)/(e_s*cc.D_v)
C_ = lambda T, e_s: F_k(T) + F_d(T, e_s)

A = lambda T: (((cc.epsilon*cc.L_vap)/(cc.cp*T)-1)*cc.g)/(cc.R_dry*T)
B = lambda T, e_s, p: (cc.epsilon*(cc.L_vap**2))/(cc.cp*cc.R_dry*(T**2)) + p/(cc.epsilon*e_s)

r0 = lambda wt, rho: (wt/((4/3)*np.pi*rho))**(1/3)

"""
def B(T, e_s, p):
    b1 = (cc.R_v*T)/(cc.epsilon*e_s)
    b2 = (cc.epsilon*(cc.L_vap**2))/(p*T*cc.cp)
    B_ = cc.rho_air*(b1 + b2)
    return B_
"""


'''
用高精度计算出 S=1!+2!+3!+⋯+n!（n≤50）。
输入格式
一个正整数 n。
输出格式
一个正整数 S，表示计算结果。
'''
a = int(input())

jx = 1
sum = 0
for s in range(a+1):
    for i in range(1,s+1):
        jx = jx*i
    #print(jx)
    sum = sum+jx
    jx=1

print(sum-1)


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

order = 1

def DFS(graph, node):
    global order
    priority[node] = order

    if not graph[node]:
        return

    for n in graph[node]:
        if not visited[n]:
            visited[n] = True
            order += 1
            DFS(graph, n)

N, M, R = map(int, input().split())

graph = [[]*(N+1) for _ in range(N+1)]
visited = [False] * (N+1)
priority = [0] * (N+1)

for i in range(M):
    u, v = map(int, input().split())
    graph[u].append(v)
    graph[v].append(u)

# 인접 정점 오름차순 정렬
for i in range(N):
    graph[i] = sorted(graph[i])

visited[R] = True

DFS(graph, R)
for p in range(1, N+1):
    print(priority[p])

백준/Silver/24479. 알고리즘 수업 － 깊이 우선 탐색 1/알고리즘 수업 － 깊이 우선 탐색 1.py

알고리즘 수업 － 깊이 우선 탐색 1.py


import aiohttp

async def correct(session: aiohttp.ClientSession, text: str) -> str:
    if not text:
        return ''

    text = text.replace('\r\n', '\n').replace('\r', '\n')

    url = 'https://speller.yandex.net/services/spellservice.json/checkText'
    async with session.post(url, data={'text': text}) as resp:
        data = await resp.json()

    data.sort(key=lambda x: x['pos'], reverse=True)

    for el in data:
        p = el['pos']
        if p and text[p-1] == '@':
            continue
        l = el['len']
        s = el['s'][0]
        text = text[:p] + s + text[p+l:]

    return text


import math
import sys
sys.stdout = open('./output.txt', 'w')
sys.stdin = open('./input.txt', 'r')

def answer_finder():
    b, ls = [int(x) for x in input().split()]
    rs_min = pow(ls,2) - pow(b,2)
    rs_min = math.sqrt(rs_min)
    rs_max = pow(ls,2) + pow(b,2)
    rs_max = math.sqrt(rs_max)
    print(round(rs_min,5), end=' ')
    print(round(rs_max,5))

test_cases = int(input())
while test_cases:
    test_cases -= 1
    answer_finder()


# coding: utf-8

from const import *
from envi import *

import math
import random
# import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np

file_path = 'checkpoint.pth.tar'

def bar():
    print('-' * 30)

class Net(nn.Module):

    def __init__(self, input_size, output_size):
        super().__init__()

        hidden_size = [72, 324, 72]

        self.linear1 = nn.Linear(input_size, hidden_size[0])
        self.linear2 = nn.Linear(hidden_size[0], hidden_size[1])
        self.linear3 = nn.Linear(hidden_size[1], hidden_size[2])
        self.linear4 = nn.Linear(hidden_size[2], output_size)

    def forward(self, x):
        x = torch.sigmoid(self.linear1(x))
        x = F.relu(self.linear2(x))
        x = F.relu(self.linear3(x))
        x = self.linear4(x)
        return x

class Agent_RL(object):
    def __init__(self, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

        self.eval_net = Net(self.state_space_dim, self.action_space_dim)

        if self.training:

            self.optimizer = optim.Adam(self.eval_net.parameters(), lr = self.lr)
            self.buffer = []#ExperienceReplay(buffer_size = self.buffer_size, unusual_sample_factor = self.unusual_sample_factor)
            self.steps = 0

            self.epsi = self.epsi_high

        else:

            self.epsi = 0

    def act(self, s0, epsi = None):
        if self.training:
            self.epsi = self.epsi_low + (self.epsi_high - self.epsi_low) * (math.exp(-1.0 * self.steps/self.decay))

        if epsi == None:
            epsi = self.epsi

        if random.random() < epsi:
            a0 = np.random.choice(self.action_space_dim, p = net_random_prior)
        else:
            s0 = torch.tensor(s0, dtype = torch.float).view(1, -1)
            a0 = torch.argmax(self.eval_net(s0)).item()

        return a0

    def put(self, *transition):
        self.steps += 1

        # self.buffer.add(transition)

        if len(self.buffer)==self.buffer_size:
            self.buffer.pop(0)
        self.buffer.append(transition)

    def __lr_modify(self):
        if self.steps < 40000:
            self.lr = 0.01
        elif self.steps < 400000:
            self.lr = 0.001
        else:
            self.lr = 0.0001

    def learn(self):
        if len(self.buffer) < self.batch_size:
            return

        '''
            s0: state before;
            a0: action;
            r1: reward(after);
            s1: state after.
        '''

        # samples = self.buffer.sample(self.batch_size)

        samples = random.sample(self.buffer, self.batch_size)

        s0, a0, r1, s1 = zip(*samples)
        s0 = torch.tensor(s0, dtype=torch.float)
        a0 = torch.tensor(a0, dtype=torch.long).view(self.batch_size, -1)
        r1 = torch.tensor(r1, dtype=torch.float).view(self.batch_size, -1)
        s1 = torch.tensor(s1, dtype=torch.float)

        y_true = r1 + self.gamma * torch.max( self.eval_net(s1).detach(), dim=1)[0].view(self.batch_size, -1)
        y_pred = self.eval_net(s0).gather(1, a0)

        loss_fn = nn.MSELoss()
        loss = loss_fn(y_pred, y_true)

        self.optimizer.zero_grad()
        loss.backward()
        self.optimizer.step()

        self.__lr_modify()

    def save(self):
        if not self.training:
            print('Can\'t save because of testing mode')
            return

        bar()
        print('Saving checkpoint to', 'checkpoint.pth.tar')

        try:

            torch.save({
                'epoch': self.steps,
                'model_state_dict': self.eval_net.state_dict(),
                'optimizer_state_dict': self.optimizer.state_dict(),
                'lr': self.lr,
            }, file_path)

        except Exception as the_exception:

            print("Checkpoint saving error:", the_exception)

        else:

            print('Checkpoint Saved')

        bar()
        print()

    def load(self):
        bar()
        print('Load checkpoint from', 'checkpoint.pth.tar')

        try:

            checkpoint = torch.load(file_path)

            self.eval_net.load_state_dict(checkpoint['model_state_dict'])
            self.steps = checkpoint['epoch']

            if self.training:
                self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
                self.lr = checkpoint['lr']

            self.eval_net.eval()

        except Exception as the_exception:

            print("Checkpoint loading error:", the_exception)
            print("The AI will run based on an untrained model")

        else:

            print("Checkpoint loaded successfully")
            print("Steps:", checkpoint['epoch'])

        bar()
        print()

    '''
    def plot(self, score, mean):
        from IPython import display
        display.clear_output(wait=True)
        display.display(plt.gcf())
        plt.figure(figsize=(20,10))
        plt.clf()

        plt.title('Training...')
        plt.xlabel('Episode')
        plt.ylabel('Duration')
        plt.plot(score)
        plt.plot(mean)
        plt.text(len(score)-1, score[-1], str(score[-1]))
        plt.text(len(mean)-1, mean[-1], str(mean[-1]))
    '''


__author__ = 'yeborui'
import timeit
import lstm_lmodel
import os

def start():
    rnn_model = lstm_lmodel.RNN_language_model("http://localhost:8000/vector?w=",
                               "../data/test_data")
    rnn_model.build_model(load_weights=True, weights_file='my_model_weights_stable.h5')
    # train the model, output generated text after each iteration
    line_size = 1000
    for iteration in range(1, 60):
        print()
        print('-' * 50)
        print('Iteration', iteration)
        start = timeit.default_timer()
        for i in range(0, rnn_model.file_line / line_size + 1):
            print('Iteration', iteration)
            print('Batch', i)
            X,y = rnn_model.read_file(line_size=line_size, read_read_line=True)
            rnn_model.model.fit(X, y, batch_size=min(128, len(X)), nb_epoch=1)
            if 'my_model_weights_dev.h5' in os.listdir('.'):
                os.remove('my_model_weights_dev.h5')
            rnn_model.save('my_model_weights_dev.h5', 'read_line')
        rnn_model.f.close()
        rnn_model.f = open(rnn_model.file_name, 'r')
    #    perp = rnn_model.cal_perplexity('../data/test')
    #    print 'calculate perp:' + str(perp)
    #    stop = timeit.default_timer()
    #    print 'time per iter:  ' + str(stop - start)
    # for diversity in [0.2, 0.5, 1.0, 1.2]:
    #     rnn_model.generate('../data/test', 5, 20, diversity=diversity)

start()


import sys
sys.setrecursionlimit(50000)
c = int(input())
n = int(input())
cnt = 0
arr = [[0]*(c+1) for _ in range(c+1)]
visit = [False]*(c+1)
for i in range(n):
    link = list(map(int, input().split(' ')))
    arr[link[0]][link[1]] = 1
    arr[link[1]][link[0]] = 1

def dfs(k, c):
    global cnt
    visit[k] = True
    for i in range(1, c+1):
        if arr[k][i] == 1 and visit[i] == False:
            cnt += 1
            dfs(i, c)

dfs(1, c)
result = [i for i in visit if i == True]
# print(arr)
# print(len(result)-1)
print(len(result)-1)


class Solution(object):
    def updateBoard(self, board, click):
        """
        :type board: List[List[str]]
        :type click: List[int]
        :rtype: List[List[str]]
        """
        n = len(board)
        m = len(board[0])
        dij = ((-1, -1), (-1, 0), (-1, 1),
               ( 0, -1),          ( 0, 1),
               ( 1, -1), ( 1, 0), ( 1, 1))
        def count_adjacent_mines(i, j):
            ret = 0
            for di, dj in dij:
                if 0 <= i+di < n and 0 <= j+dj < m and board[i+di][j+dj] == 'M':
                    ret += 1
            return ret
        def reveal(i, j):
            if not (0 <= i < n and 0 <= j < m and board[i][j] == 'E'):
                return
            cnt= count_adjacent_mines(i, j)
            if cnt > 0:
                board[i][j] = str(cnt)
                return
            board[i][j] = 'B'
            for di, dj in dij:
                reveal(i+di, j+dj)
        i, j = click
        if board[i][j] == 'M':
            board[i][j] = 'X'
        else:
            reveal(i, j)
        return board


from WaniKaniTools.api import APIv2
from WaniKaniTools.dir import database_path

def create_id_txt(path=database_path('id.txt')):
    api_v2 = APIv2()
    result = api_v2.GET('subjects')

    with open(path,'w') as fout:
        while True:
            for data in result['data']:
                meaning_array = []
                for meaning in data["data"]["meanings"]:
                    if meaning['primary']:
                        meaning_array += [ meaning['meaning'] ]

                to_print = (
                    str(data["id"]),
                    data["object"],
                    data["data"]["characters"] if "characters" in data["data"] else data["data"]["character"],
                    ', '.join(meaning_array),
                    data["data"]["document_url"]
                )
                print(*to_print)
                fout.write('\t'.join(str(x) for x in to_print) + '\n')

            next_url = result['pages']['next_url']

            if next_url is None:
                break

            result = api_v2.GETurl(next_url)


# importing the module
import math
import os
import pickle
import threading
import time
import cv2
import numpy as np
from shapely.geometry import Point, Polygon
from Detector import Detector
import pygame
from progress.bar import Bar

from BoardScene import BoardScene
from Board import Board
from Constants import POINT_BBOXS, POINT_CENTERS, NUM_POINTS, NUM_POINT_HOMOGRAPHY
from Point import Point
from Disk import Disk, Color
from Utils import pointInPoly
from Dice import Dice
from Class import Class
from BoardPosition import BoardPosition
from Snapshot import Snapshot
from Snapshots import Snapshots

class BackgammonCV:
    def __init__(self):

        self.video = 0
        self.total_frames = 0
        self.detection_every_n_frames = 5
        self.fps = 0
        self.duration = 0
        self.frame_index = 0
        self.template_aligned = False
        self.isPlaying = False
        self.isReplaying = False
        self.replay_frame_index = 0
        self.detect_thread = threading.Thread(target=self.detect, args=(0,))
        self.replay_thread = threading.Thread(target=self.replay)

        self.snapshots = Snapshots()

        self.template = []
        self.frame = []
        self.overlay_text = []
        self.overlay = []
        self.transparent_overlay = []

        self.template_width = 0
        self.template_height = 0
        self.image_width = 0
        self.image_height = 0
        self.alpha_overlay = 0.5

        self.points_template = []
        self.points_homography = []
        self.transformation_matrix = []

        self.point_centers = POINT_CENTERS
        self.point_bboxs = POINT_BBOXS

        self.board = Board()
        self.board_scene = 0

        self.p_min = 0.2
        self.threshold_nms = 0.3
        self.detector = Detector(self.p_min, self.threshold_nms)

        self.bar = Bar("Processing", max=20)
        self.replayBar = Bar("Replay", max=20)
        self.saveBar = Bar("Saving", max=20)

        self.initBoardUI()
        self.loadTemplate()

    def initBoardUI(self):
        pygame.init()
        # UI absolute window position
        os.environ["SDL_VIDEO_WINDOW_POS"] = "%d, %d" % (2, 100)
        self.board_scene = BoardScene()

    def loadTemplate(self):
        self.template = cv2.imread("../data/images/test/template.jpg")
        self.template_height, self.template_width, _ = self.template.shape
        self.points_template = [(0, 0), (self.template_width, 0), (self.template_width, self.template_height), (0, self.template_height)]

    def alignTemplate(self, image):

        if isinstance(image, str):
            self.frame = cv2.imread(image)
        else:
            self.frame = image

        self.board_scene.alligning()
        self.overlay_text = self.frame.copy()
        self.image_height, self.image_width, _ = self.frame.shape

        self.points_homography = []

        # Add points to board
        for i in range(len(self.point_bboxs)):
            point = Point((i + 1), self.point_centers[i], bbox=self.point_bboxs[i])
            self.board.addPoint(point)

        # displaying the image
        cv2.imshow("Source", self.frame)
        cv2.setMouseCallback("Source", self.mouseEvent)

    def mouseEvent(self, event, x, y, flags, params):

        # Test pointInPoly
        if event == cv2.EVENT_MOUSEMOVE:
            if len(self.points_homography) == NUM_POINT_HOMOGRAPHY:
                for point in self.board.points:
                    if pointInPoly((x, y), point.bbox_warped):
                        # print("inside " + str(point.id))
                        overlay_copy = self.overlay.copy()
                        if point.id == 25:
                            cv2.fillConvexPoly(overlay_copy, point.bbox_warped, (0, 0, 255))
                        else:
                            cv2.fillConvexPoly(overlay_copy, point.bbox_warped, (127))
                        self.overlay_text = cv2.addWeighted(overlay_copy, self.alpha_overlay, self.overlay, 1 - self.alpha_overlay, 0)
                        cv2.imshow("Source", self.overlay_text)

        if event == cv2.EVENT_LBUTTONDOWN:

            self.points_homography.append((x, y))

            if len(self.points_homography) > NUM_POINT_HOMOGRAPHY:

                self.board.reset()
                self.board_scene.updateBoard(self.board)
                self.points_homography.clear()
                self.overlay_text = self.frame.copy()

            for cords in self.points_homography:
                x, y = cords
                cv2.putText(self.overlay_text, str(cords[0]) + ", " + str(y), (x + 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 1, cv2.LINE_AA)
                cv2.circle(self.overlay_text, (x, y), 2, (0, 0, 255), 2)
            cv2.imshow("Source", self.overlay_text)

            # print((x, y))

            # Align template----------------------------------------------------------------------------------

            if len(self.points_homography) == NUM_POINT_HOMOGRAPHY:
                # print(points_img)
                self.overlay_text = self.frame.copy()

                self.board.bbox = self.points_homography.copy()

                # Draw
                for cords in self.points_homography:
                    x, y = cords
                    cv2.circle(self.overlay_text, (x, y), 2, (0, 0, 255), 2)

                # MATRIX TRANFORM --------------------------------------------------------------

                # Find matrix
                source = np.asarray(self.points_template, dtype=np.float32)
                destination = np.asarray(self.points_homography, dtype=np.float32)
                self.transformation_matrix = cv2.getPerspectiveTransform(source, destination)
                image_out = cv2.warpPerspective(self.template, self.transformation_matrix, (self.image_width, self.image_height))

                # Apply transofmration to all point's bbox
                for point in self.board.points:
                    point.bbox_warped = np.asarray(point.bbox_warped, dtype=np.float32)
                    point.bbox_warped = point.bbox_warped.reshape((-1, 1, 2))

                    point.bbox_warped = cv2.perspectiveTransform(point.bbox_warped, self.transformation_matrix)

                    point.bbox_warped = np.array(point.bbox_warped, dtype=np.int32)

                    # point.center = self.warp_point(point.center, self.transformation_matrix)

                    # Draw
                    overlay_copy = self.overlay_text.copy()
                    cv2.fillConvexPoly(overlay_copy, point.bbox_warped, (255, 255, 255))

                    self.overlay_text = cv2.addWeighted(overlay_copy, self.alpha_overlay, self.overlay_text, 1 - self.alpha_overlay, 0)

                self.overlay = cv2.add(self.overlay_text, image_out)

                # print(self.board)
                self.template_aligned = True
                self.board_scene.update()
                cv2.imshow("Source", self.overlay)
                # self.detect(self.image)

        # checking for right mouse clicks
        if event == cv2.EVENT_RBUTTONDOWN:
            if len(self.points_homography) > 0:
                self.points_homography.pop()
                self.board.reset()
            self.overlay_text = self.frame.copy()
            for cords in self.points_homography:
                x, y = cords
                cv2.putText(self.overlay_text, str(cords[0]) + ", " + str(y), (x + 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 1, cv2.LINE_AA)
                cv2.circle(self.overlay_text, (x, y), 2, (0, 0, 255), 2)
            cv2.imshow("Source", self.overlay_text)

    def detectThread(self, image):
        if self.detect_thread.is_alive():
            self.detect_thread.join()
        self.detect_thread = threading.Thread(target=self.detect, args=(image,))
        self.detect_thread.start()

    def detect(self, image):

        if not self.template_aligned:
            print("\nFirst you need to align the template. Click clockwise on the 4 extreme points of the board starting from the top left one\n")
            return

        if self.isPlaying:
            self.bar.goto(self.frame_index + 1)
        else:
            self.board_scene.detecting()

        if isinstance(image, str):
            self.frame = cv2.imread(image)
        else:
            self.frame = image

        # cv2.imshow("Source", self.frame)

        self.board.clear()

        self.image_height, self.image_width, _ = self.frame.shape

        self.detector.detect(self.frame)

        self.overlay = self.detector.drawResult()
        self.transparent_overlay = self.detector.drawBboxs()

        self.board.dices = []  # self.board.clear()

        # Generate objects from YOLO detection ---------------------------------------------------------------
        for i in range(len(self.detector.centers)):

            # DISK WHITE
            if self.detector.class_numbers[i] == Class.DISK_WHITE:
                newDisk = Disk(self.detector.centers[i], self.detector.confidences[i], Color.BLACK)
                self.board.addDisk(newDisk)

            # DISK BLACK
            if self.detector.class_numbers[i] == Class.DISK_BLACK:
                newDisk = Disk(self.detector.centers[i], self.detector.confidences[i], Color.WHITE)
                self.board.addDisk(newDisk)

            # DICE
            if self.detector.class_numbers[i] < Class.DISKS:
                newDice = Dice(self.detector.class_numbers[i], self.detector.centers[i], self.detector.confidences[i])

                # Dice position binarization
                if newDice.center[0] >= self.board.getBar().bbox_warped[0][0][0]:
                    newDice.board_position = BoardPosition.RIGHT
                else:
                    newDice.board_position = BoardPosition.LEFT

                self.board.addDice(newDice)

        # print("Dices: " + str(self.board.dices))

        self.board_scene.updateBoard(self.board)

        # Add snapshot
        snapshot = Snapshot(self.frame_index, self.board.copy(), self.frame.copy(), self.transparent_overlay.copy())
        self.snapshots.addSnapshot(snapshot)
        # print(self.snapshots)

        cv2.imshow("Source", self.overlay)

        if self.isPlaying:
            # time.sleep(0.1)
            self.nextFrame()
            self.detect(self.frame)

    # def warp_point(self, center, M):
    #     x, y = center
    #     d = M[2, 0] * x + M[2, 1] * y + M[2, 2]

    #     return (
    #         int((M[0, 0] * x + M[0, 1] * y + M[0, 2]) / d),  # x
    #         int((M[1, 0] * x + M[1, 1] * y + M[1, 2]) / d),  # y
    #     )

    def saveReplay(self):
        # print("\nSaving replay")
        fourcc = cv2.VideoWriter_fourcc(*"mp4v")
        out = cv2.VideoWriter("../data/videos/result/replay.mp4", fourcc, 20.0, (1280, 720))
        s = ""
        currentSnapshotIndex = 0
        # overlay = np.zeros((self.image_height, self.image_width, 3), np.uint8)
        # frame_index = self.snapshots.getLastSnapshot().frame_index
        # print(frame_index)
        self.saveBar.max = self.total_frames
        self.saveBar.fill = "\u258C"
        self.saveBar.suffix = "%(index)d/%(max)d (%(percent)d%%) [remaining %(eta_time)s]"
        self.saveBar.goto(1)
        for i in range(self.total_frames - 1):
            self.saveBar.next()
            self.video.set(1, i)
            _, frame = self.video.read()

            # if frame processed and saved in snapshot add overlay
            if i == self.snapshots.getSnapshot(currentSnapshotIndex).frame_index and currentSnapshotIndex < self.snapshots.getLastSnapshot().id:
                currentSnapshotIndex += 1
                # self.board_scene.updateBoard(self.snapshots.getSnapshot(currentSnapshotIndex - 1).board)

            overlay = self.snapshots.getSnapshot(currentSnapshotIndex - 1).overlay
            frame = cv2.add(overlay, frame)
            out.write(frame)

        # for snapshot in self.snapshots.snapshots:
        #     # cv2.imshow("Snaphsots", snapshot.overlay)
        #     # cv2.imshow("Snaphsots", snapshot.overlay)
        #     # time.sleep(0.2)
        #     out.write(snapshot.overlay)
        #     # self.board_scene.updateBoard(snapshot.board)
        #     s += "Frame " + str(snapshot.frame_index) + "\n"
        #     s += str(snapshot.board) + "\n\n"
        out.release()
        # text_file = open("snapshots.txt", "w")
        # text_file.write(s)
        # text_file.close()
        print("\nReplay saved in replay.mp4 and snapshots.txt ")

    def replayThread(self):
        if self.replay_thread.is_alive():
            self.replay_thread.join()
            # print("Stop replay")
        else:
            self.replay_thread = threading.Thread(target=self.replay)
            self.replay_thread.start()

    def replay(self):
        # print("\nReplay")
        # for snapshot in self.snapshots.snapshots:
        #     # cv2.imshow("Snaphsots", snapshot.overlay)
        #     cv2.imshow("Source", snapshot.overlay)
        #     # time.sleep(0.2)
        #     self.board_scene.updateBoard(snapshot.board)
        #     time.sleep(0.06)
        currentSnapshotIndex = 0
        # overlay = np.zeros((self.image_height, self.image_width, 3), np.uint8)
        max_frame_index = self.snapshots.getLastSnapshot().frame_index
        # print(max_frame_index)
        self.replayBar.max = max_frame_index
        self.replayBar.fill = "\u258C"
        self.replayBar.suffix = "%(index)d/%(max)d (%(percent)d%%) [remaining %(eta_time)s]"
        self.replayBar.goto(0)

        for i in range(max_frame_index):
            self.replayBar.next()
            self.video.set(1, i)
            _, frame = self.video.read()

            # if frame processed and saved in snapshot add overlay
            if i == self.snapshots.getSnapshot(currentSnapshotIndex).frame_index:
                currentSnapshotIndex += 1
                self.board_scene.updateBoard(self.snapshots.getSnapshot(currentSnapshotIndex - 1).board)

            overlay = self.snapshots.getSnapshot(currentSnapshotIndex - 1).overlay
            frame = cv2.add(overlay, frame)

            cv2.imshow("Source", frame)

            time.sleep(0.016)
        # print("\nEnd replay")

    def nextFrame(self):
        self.frame_index += self.detection_every_n_frames
        self.frame_index = min(self.frame_index, self.total_frames - 1)
        if self.frame_index == self.total_frames - 1:
            self.stop()
            self.video.set(1, self.frame_index)
            _, self.frame = self.video.read()
            if not self.isPlaying:
                cv2.imshow("Source", self.frame)
            # time.sleep(3)
            self.saveSnapshots()
            # self.bar.finish()
            # self.replay()
        else:
            self.video.set(1, self.frame_index)
            _, self.frame = self.video.read()
            if not self.isPlaying:
                cv2.imshow("Source", self.frame)
        # self.detect(self.frame)
        # return self.frame_index

    def previousFrame(self):
        self.frame_index -= self.detection_every_n_frames
        self.frame_index = max(0, self.frame_index)
        self.video.set(1, self.frame_index)
        _, self.frame = self.video.read()
        if not self.isPlaying:
            cv2.imshow("Source", self.frame)
        # print(type(self.frame))
        # self.detect(self.frame)
        # return self.frame_index

    def togglePlay(self):
        if self.isPlaying:
            self.isPlaying = False
            self.stop()
        else:
            self.isPlaying = True
            self.play()

    # def toggleReplay(self):
    #     if self.isReplaying:
    #         self.isReplaying = False
    #         self.stopReplay()
    #     else:
    #         self.isReplaying = True
    #         self.replay()

    def play(self):
        # print("Play")
        self.isPlaying = True
        self.detectThread(self.frame)

    def stop(self):
        # print("Stop")
        self.isPlaying = False

    def saveSnapshots(self):
        print("\nSaving snapshots")
        picklefile = open("../data/snapshots/snapshots.bcv", "wb")
        pickle.dump(self.snapshots, picklefile)
        picklefile.close()
        print("Snapshots saved")

    def loadSnapshots(self):
        print("\n\nLoading snapshots")
        picklefile = open("../data/snapshots/snapshots.bcv", "rb")
        # unpickle the dataframe
        snapshots = pickle.load(picklefile)
        picklefile.close()
        # close file
        self.snapshots = snapshots
        # print(self.snapshots)
        print(str(len(self.snapshots.snapshots)) + " snapshots loaded\n")


"""Update schedules table to be text

Revision ID: 6acef6edafc6
Revises: 3e4ea1d3c842
Create Date: 2023-12-14 14:48:57.659638

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import BYTEA

# revision identifiers, used by Alembic.
revision = '6acef6edafc6'
down_revision = '3e4ea1d3c842'
branch_labels = None
depends_on = None

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('schedule', 'image',
               existing_type=BYTEA,
               type_=sa.Text)
    # ### end Alembic commands ###

def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('schedule', 'image',
               type_=sa.Text,
               nullable=True)
    # ### end Alembic commands ###


class GetEmailID:

	def get_email_id(self, email_id, chat_id):
		'''
		Determine whether the email needs to be sent to @tg_chat_id.
		 - Return an email id (PK) if it needs to be sent
		 - Return None if it doesn't need to be sent
		'''
		if self.__is_sent(email_id, chat_id):
			return

		res = self.__email_id(email_id)
		if not bool(res):
			return

		return int(res[0])

	def __is_sent(self, email_id, channel_id):
		'''
		Checking if this email has already been sent
		or not.
		 - Return True if it's already been sent
		'''
		q = """
			SELECT dc_emails.id, dc_mail_msg.id FROM dc_emails
			LEFT JOIN dc_mail_msg
			ON dc_emails.id = dc_mail_msg.email_id
			WHERE dc_emails.message_id = %(email_msg_id)s
			AND dc_mail_msg.channel_id = %(channel_id)s
			LIMIT 1
		"""

		self.cur.execute(q, {
			"email_msg_id": email_id,
			"channel_id": channel_id
		})
		res = self.cur.fetchone()
		return bool(res)

	def __email_id(self, email_id):
		'''
		Get the email id if match with the email message_id.
		 - Return the result if it's match and exists
		'''
		q = """
			SELECT id FROM dc_emails WHERE message_id = %(email_id)s
		"""

		self.cur.execute(q, {"email_id": email_id})
		res = self.cur.fetchone()
		return res


import torch as torch
import os

import torch.utils.data as data
import numpy as np
from tqdm import tqdm
import argparse

from models.RCA import AE
from data_process import RealDataset

class Solver_RCA:
    def __init__(
        self,
        data_name,
        hidden_dim=128,  # number of hidden neurons in RCA
        z_dim=10,  # bottleneck dimension
        seed=0,  # random seed
        learning_rate=1e-3,  # learning rate
        batch_size=128,  #  batchsize
        training_ratio=0.8,  #  training data percentage
        max_epochs=100,  #  training epochs
        coteaching=1.0,  #  whether selects sample based on loss value
        oe=0.0,  # how much we overestimate the ground-truth anomaly ratio
        missing_ratio=0.0,  # missing ratio in the data
    ):
        # Data loader
        # read data here
        np.random.seed(seed)
        torch.manual_seed(seed)
        torch.cuda.manual_seed(seed)

        use_cuda = torch.cuda.is_available()
        self.data_name = data_name
        self.device = torch.device("cuda" if use_cuda else "cpu")
        data_path = "./data/" + data_name + ".npy"
        self.missing_ratio = missing_ratio
        self.model_save_path = "./trained_model/{}/{}/RCA/{}/".format(
            data_name, missing_ratio, seed
        )
        if oe == 0.0:
            self.result_path = "./results/{}/{}/RCA/{}/".format(
                data_name, missing_ratio, seed
            )
        else:
            self.result_path = "./results/{}/{}/RCA_{}/{}/".format(
                data_name, missing_ratio, oe, seed
            )

        os.makedirs(self.model_save_path, exist_ok=True)
        self.learning_rate = learning_rate
        self.dataset = RealDataset(
            data_path, missing_ratio=self.missing_ratio
        )
        self.seed = seed
        self.hidden_dim = hidden_dim
        self.z_dim = z_dim
        self.max_epochs = max_epochs
        self.coteaching = coteaching
        self.beta = 0.0  # initially, select all data
        self.alpha = 0.5
        self.data_path = data_path

        self.data_anomaly_ratio = self.dataset.__anomalyratio__() + oe

        self.input_dim = self.dataset.__dim__()
        self.data_normaly_ratio = 1 - self.data_anomaly_ratio

        n_sample = self.dataset.__len__()
        self.n_train = int(n_sample * (training_ratio))
        self.n_test = n_sample - self.n_train
        print(
            "|data dimension: {}|data noise ratio:{}".format(
                self.dataset.__dim__(), self.data_anomaly_ratio
            )
        )

        self.decay_ratio = abs(self.beta - (1 - self.data_anomaly_ratio)) / (
            self.max_epochs / 2
        )
        training_data, testing_data = data.random_split(
            dataset=self.dataset, lengths=[self.n_train, self.n_test]
        )

        self.training_loader = data.DataLoader(
            training_data, batch_size=batch_size, shuffle=True
        )

        self.testing_loader = data.DataLoader(
            testing_data, batch_size=self.n_test, shuffle=False
        )
        self.ae = None
        self.discriminator = None
        self.build_model()
        self.print_network()

    def build_model(self):
        self.ae = AE(
            input_dim=self.input_dim, hidden_dim=self.hidden_dim, z_dim=self.z_dim
        )
        self.ae = self.ae.to(self.device)

    def print_network(self):
        num_params = 0
        for p in self.ae.parameters():
            num_params += p.numel()
        print("The number of parameters: {}".format(num_params))

    def train(self):
        optimizer = torch.optim.Adam(self.ae.parameters(), lr=self.learning_rate)
        self.ae.eval()
        loss_mse = torch.nn.MSELoss(reduction='none')
        if self.data_name == 'optdigits':
            loss_mse = torch.nn.BCELoss(reduction='none')

        for epoch in tqdm(range(self.max_epochs)):  # train 3 time classifier
            for i, (x, y) in enumerate(self.training_loader):
                x = x.to(self.device).float()
                n = x.shape[0]
                n_selected = int(n * (1-self.beta))

                if config.coteaching == 0.0:
                    n_selected = n
                if i == 0:
                    current_ratio = "{}/{}".format(n_selected, n)

                optimizer.zero_grad()

                with torch.no_grad():
                    self.ae.eval()
                    z1, z2, xhat1, xhat2 = self.ae(x.float(), x.float())

                    error1 = loss_mse(xhat1, x)
                    error1 = error1
                    error2 = loss_mse(xhat2, x)
                    error2 = error2

                    error1 = error1.sum(dim=1)
                    error2 = error2.sum(dim=1)
                    _, index1 = torch.sort(error1)
                    _, index2 = torch.sort(error2)

                    index1 = index1[:n_selected]
                    index2 = index2[:n_selected]

                    x1 = x[index2, :]
                    x2 = x[index1, :]

                self.ae.train()
                z1, z2, xhat1, xhat2 = self.ae(x1.float(), x2.float())
                loss = loss_mse(xhat1, x1) + loss_mse(xhat2, x2)
                loss = loss.sum()
                loss.backward()
                optimizer.step()

            if self.beta < self.data_anomaly_ratio:
                self.beta = min(
                    self.data_anomaly_ratio, self.beta + self.decay_ratio
                )

    def test(self):
        print("======================TEST MODE======================")
        self.ae.train()
        mse_loss = torch.nn.MSELoss(reduction='none')
        if self.data_name == 'optdigits':
            mse_loss = torch.nn.BCELoss(reduction='none')

        error_list = []
        for _ in range(1000):  # ensemble score over 100 stochastic feedforward
            with torch.no_grad():
                for _, (x, y) in enumerate(self.testing_loader):  # testing data loader has n_test batchsize, if it is image data, need change this part
                    y = y.data.cpu().numpy()
                    x = x.to(self.device).float()
                    _, _, xhat1, xhat2 = self.ae(x.float(), x.float())
                    error = mse_loss(xhat1, x) + mse_loss(xhat2, x)
                    error = error.mean(dim=1)
                error = error.data.cpu().numpy()
                error_list.append(error)
        error_list = np.array(error_list)
        error = error_list.mean(axis=0)
        from sklearn.metrics import (
            precision_recall_fscore_support as prf,
            accuracy_score,
            roc_auc_score,
        )
        gt = y.astype(int)

        thresh = np.percentile(error, self.dataset.__anomalyratio__() * 100)
        print("Threshold :", thresh)

        pred = (error > thresh).astype(int)
        gt = y.astype(int)
        auc = roc_auc_score(gt, error)
        accuracy = accuracy_score(gt, pred)
        precision, recall, f_score, support = prf(gt, pred, average="binary")

        print(
            "Accuracy : {:0.4f}, Precision : {:0.4f}, Recall : {:0.4f}, F-score : {:0.4f}, AUC : {:0.4f}".format(
                accuracy, precision, recall, f_score, auc
            )
        )

        os.makedirs(self.result_path, exist_ok=True)

        np.save(
            self.result_path + "result.npy",
            {
                "accuracy": accuracy,
                "precision": precision,
                "recall": recall,
                "f1": f_score,
                "auc": auc,
            },
        )
        print("result save to {}".format(self.result_path))
        return accuracy, precision, recall, f_score, auc

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="RCA")
    parser.add_argument("--algorithm", type=str, default="RCA", required=False)
    parser.add_argument("--seed", type=int, default=0, required=False)
    parser.add_argument("--data", type=str, default="pima", required=False)
    parser.add_argument("--max_epochs", type=int, default=200, required=False)
    parser.add_argument("--hidden_dim", type=int, default=256, required=False)
    parser.add_argument("--batch_size", type=int, default=128, required=False)
    parser.add_argument("--oe", type=float, default=0.0, required=False)
    parser.add_argument("--training_ratio", type=float, default=0.6, required=False)
    parser.add_argument("--learning_rate", type=float, default=3e-4, required=False)
    parser.add_argument("--z_dim", type=int, default=10, required=False)
    parser.add_argument("--coteaching", type=float, default=1.0, required=False)
    parser.add_argument("--missing_ratio", type=float, default=0.0, required=False)
    config = parser.parse_args()
    """
    read data
    """
    np.random.seed(config.seed)
    torch.manual_seed(config.seed)
    torch.cuda.manual_seed(config.seed)
    torch.backends.cudnn.benchmark = True
    Solver = Solver_RCA(
        data_name=config.data,
        hidden_dim=config.hidden_dim,
        z_dim=config.z_dim,
        seed=config.seed,
        learning_rate=config.learning_rate,
        batch_size=config.batch_size,
        training_ratio=config.training_ratio,
        max_epochs=config.max_epochs,
        missing_ratio=config.missing_ratio,
        oe=config.oe,
    )

    Solver.train()
    Solver.test()
    print("Data {} finished".format(config.data))


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

import datetime
import os, re
import random
import sys, traceback
import posixpath
import user

import web
import board, article
import config
from config import render
from datetime import datetime
import i18n
import bbcode
_ = i18n.custom_gettext

lang_map = { 'ko': u'한국어',
             'en': u'English',
             'ru': u'Русский', }

def session_helper(func):
    def _exec(*args, **kwargs):
        try:
            current_uid = web.ctx.session.uid
        except:
            raise web.seeother(link('/+login'))
            #raise web.unauthorized(render[mobile].error(error_message = _("NOT_LOGGED_IN"), help_context = 'error'))
        if current_uid < 1:
            web.ctx.session.uid = 0
            web.ctx.session.kill()
            raise web.internalerror(default_render.error(error_message = _("INVALID_UID"), help_context = 'error'))
        kwargs.update({'current_uid': current_uid})
        return func(*args, **kwargs)
    _exec.__name__ == func.__name__
    _exec.__doc__ == func.__doc__
    return _exec

def confirmation_helper(func):
    def _exec(*args, **kwargs):
        i = web.input()
        if i.has_key('ok'):
            return func(*args, **kwargs)
        else:
            raise web.seeother(i.referer)
    _exec.__name__ == func.__name__
    _exec.__doc__ == func.__doc__
    return _exec

def theme(func):
    def _exec(*args, **kwargs):
        arglist = [args[0]]
        theme = args[1]

        if theme is not None and theme.endswith('/'):
            theme = theme[:len(theme)-1]

        if theme is None or theme == '':
            web.config.theme = 'default'
        elif theme in config.render:
            web.config.theme = theme
        else:
            raise NameError('Invalid theme: ' + theme)

        for i in range(2, len(args)):
            arglist.append(args[i])

        res = func(*tuple(arglist), **kwargs)
        return res
    return _exec

def error_catcher(func):
    def _exec(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except (web.webapi.unauthorized, web.webapi._NotFound, web.webapi._InternalError, web.webapi.SeeOther): # 웹 프로그램의 오류. 대개 해결 가능.
            raise
        except Exception as e:
            current_ctx = web.ctx
            error_text = traceback.format_exc()
            store_error(current_ctx, error_text)
            raise web.internalerror(config.default_render.error(error_message = e,
                error_detail = error_text, help_context = 'error'))
    _exec.__name__ == func.__name__
    _exec.__doc__ == func.__doc__
    return _exec

def store_error(current_ctx, error_text):
    if not config.store_error_report:
        return
    today = datetime.today()
    filename = 'error_%s.txt' % today.strftime('%Y%m%d_%H%M%S')
    try:
        f = open(os.path.join(config.error_report_path, filename), 'w')
        f.write('Traceback:\n')
        f.write(error_text)
        f.write('\n')
        f.write('Context:\n')
        f.write(str(current_ctx))
        f.write('\n')
        f.close()
    except OSError:
        pass

def validate_username(name):
    match = re.search(r'\W+', name)
    if match == None:
        return True
    else:
        return False

def validate_boardname(name):
    match = re.search(r'[^\w/]+', name)
    if match == None:
        return True
    else:
        return False

def format(original):
    b = bbcode.parse
    try:
        return b(process_noah12k_quote(original))
    except:
        text = traceback.format_exc()
        #text = process_noah12k_quote(original)
        return text.replace(' ', ' ').replace('\n', '
\n')

def remove_bracket(original):
    # BBCode 파서 안에 [/]이 들어가면 파서가 헷갈리므로
    # 글 제목에 들어간 [/]을 (/)으로 바꿈.
    return original.replace('[', '(').replace(']', ')')

def process_noah12k_quote(original):
    # noah 1k/2k의 인용구를 BBCode 형태로 바꿔 줌.
    # 반드시 HTML로 변환하기 전에 호출해야 함.
    indent = '> '
    header = [u'"에서: ',u'"에서 : ']
    lines = original.split('\n')
    levels = []
    ret = u''
    for i in range(0, len(lines)):
        last_q = 0
        while lines[i].find(indent) == 0:
            last_q = last_q + 1
            lines[i] = lines[i][len(indent):]
        levels.append(last_q)
    assert(len(levels) == len(lines))

    trimBegin = False
    for i in range(0, len(lines)):
        if i==0 or (levels[i-1] < levels[i]):
            trimBegin = True
        if trimBegin and lines[i].strip() == '':
            lines[i] = ''
        else:
            trimBegin = False

    trimEnd = False
    for i in range(len(lines)-1, -1, -1):
        if i==len(levels)-1 or levels[i] > levels[i+1]:
            trimEnd = True
        if trimEnd and lines[i] == '' and lines[i].strip() == '':
            lines[i] = ''
        else:
            trimEnd = False

    for i in range(0, len(lines)):
        prevLevel = 0
        if i>0:
            prevLevel = levels[i-1]
        while levels[i] != prevLevel:
            if levels[i] > prevLevel:
                if lines[i-1].endswith(header[0]) or lines[i-1].endswith(header[1]):
                    ret += '[quote=%s]\n' % remove_bracket(lines[i-1])
                else:
                    ret += '[quote]\n'
                prevLevel = prevLevel + 1
            if levels[i] < prevLevel:
                ret +='[/quote]\n'
                prevLevel = prevLevel - 1
        if lines[i] != '':
            if not( lines[i].endswith(header[0]) or lines[i].endswith(header[1])):
                ret +=lines[i]
            else:
                continue
        if i < len(lines)-1:
            ret +='\n'
    return ret

def get_login_notice(notice_board = '/noah/welcome'):
    # /noah/welcome에서 공지 표시된 글 중 아무거나 하나를 랜덤으로 돌려 줌.
    # 공지 표시된 글이 없는 경우 '공지가 없습니다.'를 돌려 줌.
    articles = []
    board_id = board._get_board_id_from_path(notice_board)
    if board_id < 0:
        return _('INVALID_NOTICE_BOARD')
    for i in article.get_marked_article(board_id):
        articles.append(i)
    if len(articles) == 0:
        return _('NO_NOTICE')
    return articles[random.randint(0, len(articles)-1)].aContent

def lcode_to_name(lcode):
    return lang_map[lcode]

def get_lang_map():
    return lang_map

def traverse_board_path(path, theme=None):
    if path == '^root':
        path = '/'
    ret = ""
    id = board._get_board_id_from_path(path)
    while id != 1 and id > 0:
        i = board.get_board_info(id)
        if i.bType == 0:
            if theme:
                ret = '/%s' % (theme, i.bName, i.bName.split('/')[-1]) + ret
            else:
                ret = '/%s' % (i.bName, i.bName.split('/')[-1]) + ret
        else:
            if theme:
                ret = '/%s' % (theme, i.bName, i.bName.split('/')[-1]) + ret
            else:
                ret = '/%s' % (i.bName, i.bName.split('/')[-1]) + ret
        new_id = board.get_parent(id)
        if new_id == id:
            break
        id = new_id
#    ret = (u'%s' % _('Board')) + ret
    return ret

def get_type(path):
    if path == '^root':
        path = '/'
    id = board._get_board_id_from_path(path)
    if id < 0:
        return -1
    data = board.get_board_info(id)
    return data.bType

def choose_banner():
    banners = []
    try:
        f = open(config.banner_path, 'r')
        banners = f.readlines()
        f.close()
    except IOError:
        return ['default.png', '#']
    cumulative_list = []
    j = 0
    for b in banners:
        banners[j] = b.strip().split(' ')
        banners[j][2] = int(banners[j][2])
        if len(cumulative_list) == 0:
            cumulative_list.append(banners[j][2])
        else:
            cumulative_list.append(cumulative_list[-1] + banners[j][2])
        j = j + 1
    banner_id = random.randint(1, max(cumulative_list))
    j = 0
    ret = tuple()
    cond = False
    for i in cumulative_list:
        if j == 0:
            cond = (banner_id <= i)
        else:
            cond = (cumulative_list[j-1] < banner_id) and (banner_id <= i)
        if cond:
            ret = tuple((banners[j][0], banners[j][1]))
            break
        else:
            j = j + 1
    return ret

#always starts with traling /
def link(url):
    if web.config.theme is not 'default':
        if url.startswith('/'):
            url = '/' + web.config.theme + url
        else:
            url = web.config.theme + '/' + url
    return url

def https():
    return config.https_domain;

def render():
    try:
        if web.config.theme == 'default':
            return config.render[user.get_user(web.ctx.session.uid)[1].theme]
        else:
            return config.render[web.config.theme]
    except:
        return config.render[web.config.theme]

def strip_path(path):
    elems = path.split('/')
    for i in range(len(elems)-1):
        elems[i] = elems[i][:1]
    return '/'.join(elems)


import requests
import json
import time

# BASIC FUNCTIONS
# ---------------------------------------------------

# Creates access token necessary for OAuth with givenid and password
def create_access_token(client_id, client_secret, region = 'eu'):
    data = { 'grant_type': 'client_credentials' }
    response = requests.post('https://%s.battle.net/oauth/token' % region, data=data, auth=(client_id, client_secret))
    return response.json()['access_token']

# Returns a list of connected realms
def connected_realms_list(token):
    response = requests.get('https://eu.api.blizzard.com/data/wow/search/connected-realm?namespace=dynamic-eu&locale=en_GB&orderby=id&access_token='+token)
    results = response.json()['results']
    realms_nr = nr_conn_realms(token)
    data = list()
    i = 0
    while i<realms_nr:
        data.append(results[i]['data'])
        i += 1
    return data

# Returns the number of connected realms
def nr_conn_realms(token):
    response = requests.get('https://eu.api.blizzard.com/data/wow/connected-realm/index?namespace=dynamic-eu&locale=en_GB&access_token='+token)
    return len(response.json()['connected_realms'])

# FUNCTIONS FOR DISPLAYING CHARACTER IMAGE
# -------------------------------------------

# Returns the image of a character
def get_character_img(charName, server, token):
    response = requests.get('https://eu.api.blizzard.com/profile/wow/character/'+server+'/'+charName.lower()+'/character-media?namespace=profile-eu&locale=en_GB&access_token='+token)
    img_link = ""

    try:
        if response.json().get('assets'):
            img_link = response.json()['assets'][2]['value']
        else:
            img_link = response.json()['render_url']
    except KeyError:
        img_link = ""
    return img_link

# Returns a list with all realm names
def get_realm_names(token):
    realm_list = connected_realms_list(token)
    realm_names = list()
    i=0
    while i<len(realm_list):
        realms = realm_list[i]['realms']
        j=0
        while j<len(realms):
            realm_names.append(realm_list[i]['realms'][j]['slug'])
            j += 1
        i+=1
    return realm_names

# FUNCTIONS FOR AUCTION HOUSE DISPLAY
# ---------------------------------------------------

# Returns all the auction listings for a connected realm
def get_auction_listing(realm_id, token):
    response = requests.get('https://eu.api.blizzard.com/data/wow/connected-realm/'+realm_id+'/auctions?namespace=dynamic-eu&locale=en_GB&access_token='+token)
    return response.json()

# Returns the cheapest listing for an item in a connected realm
def get_min_auction_listing(item_id, realm_id, token):
    with open(str(realm_id)) as f:
        json_file = json.load(f)
    auctions = json_file['auctions']
    i=0
    min_price = 10000000
    while i<len(auctions):
        curr_item_id = auctions[i]['item']['id']
        if curr_item_id == item_id:
            item_price = auctions[i]['unit_price']
            if item_price < min_price:
                min_price = item_price
        i+=1
    print(min_price)
    return min_price

# Returns a list of realms names included in the given connected realm ids
def get_realm_names_conn(conn_ids, token):
    realm_names = list()
    i=0
    while i<len(conn_ids):
        response = requests.get('https://eu.api.blizzard.com/data/wow/connected-realm/'+str(conn_ids[i])+'?namespace=dynamic-eu&access_token='+token)
        results = response.json()['realms']
        j=0
        while j<len(results):
           realm_names.append(results[j]['name']['en_US'])
           j+=1
        i+=1
    return realm_names

# Returns a dictionary containing the cheapest listing for an items along with the realms
def min_listing(item_id, token):
    start = time.time()
    i = 0
    id_list = get_conn_realm_id_list(token)
    min_price = 10000000
    chosen_realms = list()
    while i < len(id_list):
        curr_realm_price = get_min_auction_listing(item_id, id_list[i], token)
        if curr_realm_price < min_price:
            min_price = curr_realm_price
            chosen_realms.clear()
            chosen_realms.append(id_list[i])
        elif curr_realm_price == min_price:
            chosen_realms.append(id_list[i])
        i += 1
    realms = get_realm_names_conn(chosen_realms, token)
    results = {
        "realms": realms,
        "price": min_price
        }
    end = time.time()
    print(end - start)
    return results

# Returns a dictionary with all suggested items and their id based on user input
def get_item_list(item, token):
    response = requests.get('https://eu.api.blizzard.com/data/wow/search/item?namespace=static-eu&locale=en_GB&name.en_GB='+item+'&orderby=id&_page=1&access_token='+token)
    results = response.json()['results']
    i = 0
    item_list = {}
    while i < response.json()['pageSize']:
        if item.lower() in results[i]['data']['name']['en_GB'].lower():
            item_list[results[i]['data']['name']['en_GB']] = results[i]['data']['id']
        i += 1
    return item_list

# Returns a list that contains the ids of all connected realms
def get_conn_realm_id_list(token):
    realm_list = connected_realms_list(token)
    id_list = list()
    i = 0
    while i<len(realm_list):
        id_list.append(realm_list[i]['id'])
        i += 1
    return id_list

# Returns the icon of an item
def get_item_icon(item_id, token):
    #response = requests.get('https://eu.api.blizzard.com/data/wow/media/item/'+str(item_id)+'?namespace=static-9.0.5_37760-eu&access_token='+token)
    print('response='+response)
    results = response.json()['assets'][0]['value']
    print(results)
    return results


# @lc app=leetcode id=229 lang=python3
#
# [229] Majority Element II
#

# @lc tags=array

# @lc imports=start
import collections
from imports import *
# @lc imports=end

# @lc idea=start
#
#
#
# @lc idea=end

# @lc group=

# @lc rank=

# @lc code=start
class Solution:
    def majorityElement(self, nums: List[int]) -> List[int]:
        """ Solution 1: use Moore voting algo """
        ans = []
        ele1, ele2 = 0, 0
        vote1, vote2 = 0, 0
        for num in nums:
            if vote1 > 0 and num == ele1:
                vote1 += 1
            elif vote2 > 0 and num == ele2:
                vote2 += 1
            elif vote1 == 0:
                ele1 = num
                vote1 += 1
            elif vote2 == 0:
                ele2 = num
                vote2 += 1
            else:
                vote1 -= 1
                vote2 -= 1
        cnt1, cnt2 = 0, 0
        for num in nums:
            if vote1 > 0 and num == ele1:
                cnt1 += 1
            if vote2 > 0 and num == ele2:
                cnt2 += 1
        if vote1 > 0 and cnt1 > len(nums) // 3:
            ans.append(ele1)
        if vote2 > 0 and cnt2 > len(nums) // 3:
            ans.append(ele2)

        return ans

        """ Solution 2: Hash table """
        majority = collections.Counter(nums)

        ans = []
        for key in majority.keys():
            if majority[key] > len(nums) // 3:
                ans.append(key)
        return ans
        pass
# @lc code=end

# @lc main=start
if __name__ == '__main__':
    print('Example 1:')
    print('Input : ')
    print('nums = [3,2,3]')
    print('Exception :')
    print('[3]')
    print('Output :')
    print(str(Solution().majorityElement([3,2,3])))
    print()

    print('Example 2:')
    print('Input : ')
    print('nums = [1]')
    print('Exception :')
    print('[1]')
    print('Output :')
    print(str(Solution().majorityElement([1])))
    print()

    print('Example 3:')
    print('Input : ')
    print('nums = [1,2]')
    print('Exception :')
    print('[1,2]')
    print('Output :')
    print(str(Solution().majorityElement([1,2])))
    print()

    pass
# @lc main=end