import contextlib
import json
import os

from .utils.utils import runcmd

async def meme_type(message):
    if message:
        try:
            if message.photo:
                return "Photo"
            if message.audio:
                return "Audio"
            if message.voice:
                return "Voice"
            if message.video_note:
                return "Round Video"
            if message.gif:
                return "Gif"
            if message.sticker:
                mime = message.document.mime_type
                if mime == "application/x-tgsticker":
                    return "Animated Sticker"
                return "Video Sticker" if mime == "video/webm" else "Static Sticker"
            if message.video:
                return "Video"
            if message.document:
                mime = message.document.mime_type
                ext = os.path.splitext(message.file.name)[1]
                if mime != "image/gif" and mime.split("/")[0] == "image":
                    return "Photo"
                if mime == "image/gif":
                    return "Gif"
                if mime.split("/")[0] == "video":
                    return "Video"
                if mime == "application/x-tgsticker":
                    return "Animated Sticker"
                return (
                    "Video" if ext in [".mp4", ".mkv", ".avi", ".m4v"] else "Document"
                )
        except AttributeError:
            return await file_type(message)
    return None

async def media_type(message):
    if message:
        try:
            if message.photo:
                return "Photo"
            if message.audio:
                return "Audio"
            if message.voice:
                return "Voice"
            if message.video_note:
                return "Round Video"
            if message.gif:
                return "Gif"
            if message.sticker:
                return "Sticker"
            if message.video:
                return "Video"
            if message.document:
                return "Document"
        except AttributeError:
            media = await file_type(message)
            if media and media in [
                "Video Sticker",
                "Animated Sticker",
                "Static Sticker",
            ]:
                return "Sticker"
            return media
    return None

async def fileinfo(file):
    x, y, z, s = await runcmd(f"mediainfo '{file}' --Output=JSON")
    cat_json = json.loads(x)["media"]["track"]
    dic = {
        "path": file,
        "size": int(cat_json[0]["FileSize"]),
        "extension": cat_json[0]["FileExtension"],
        "type": "None",
        "format": "None",
        "audio": "None",
    }
    with contextlib.suppress(IndexError, KeyError):
        dic["format"] = cat_json[0]["Format"]
        dic["type"] = cat_json[1]["@type"]
        if "ImageCount" not in cat_json[0]:
            dic["duration"] = int(float(cat_json[0]["Duration"]))
            dic["bitrate"] = int(cat_json[0]["OverallBitRate"]) // 1000
        dic["height"] = int(cat_json[1]["Height"])
        dic["width"] = int(cat_json[1]["Width"])
        dic["audio"] = "Present" if cat_json[0]["AudioCount"] else "None"
    return dic

async def file_type(message):
    if not os.path.exists(message):
        return None
    media = await fileinfo(message)
    if media["type"] == "Image":
        if media["format"] == "GIF":
            return "Gif"
        return "Static Sticker" if media["format"] == "WebP" else "Photo"
    elif media["type"] == "Video":
        if media["audio"] == "None":
            return "Video Sticker" if media["format"] == "WebM" else "Gif"
        return "Video"
    elif media["type"] == "Audio":
        return "Voice" if media["format"] == "Ogg" else "Audio"
    elif media["extension"] == "tgs":
        return "Animated Sticker"
    return "Document"


import sys

n, m = map(int, sys.stdin.readline().split())
a, b, d = map(int, sys.stdin.readline().split())
map = [list(map(int, sys.stdin.readline().split())) for x in range(m)]
visited = [[0]*m for x in range(n)]
visited[a][b] = 1

def change_d(d):
    if d-1 == -1:
        d = 3
    else:
        d -= 1
    return d

def check(new_a, new_b):
    if 0 <= new_a < n and 0 <= new_b < m and map[new_a][new_b] == 0 and visited[new_a][new_b] == 0:
        return True
    else:
        return False

steps = [(0, -1), (1, 0), (0, 1), (-1, 0)]
result = 1
turn_time = 0
while True:
    d = change_d(d)
    new_a = a+steps[d][0]
    new_b = b+steps[d][1]
    if check(new_a, new_b):
        a = new_a
        b = new_b
        turn_time = 0
        result += 1
        visited[a][b] = 1
        continue
    else:
        turn_time += 1

    if turn_time == 4:
        new_a = a-steps[d][0]
        new_b = b-steps[d][1]
        if check(new_a, new_b):
            a = new_a
            b = new_b
            result += 1
            visited[a][b] = 1
        else:
            break
print(result)

이것이 코딩 테스트다 with python/구현/게임 개발.py


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('sfotipy', '0004_auto_20151118_1318'),
    ]

    operations = [
        migrations.AlterField(
            model_name='album',
            name='cover',
            field=models.ImageField(default=b'coverdefault.png', upload_to=b'sfotipy'),
        ),
    ]


import PySimpleGUI as sg
import coordinates as co

def open_gui(input_val: str = "", scale: float = 8.0, message: str = "") -> list:
    layout = [
        [sg.Text("Coordinates (DMS): \t"), sg.In(size=(25, 1), enable_events=True, key="-COORDINATES-", default_text=input_val)],
        [sg.Text("Size (m): \t\t"), sg.In(size=(25, 1), enable_events=True, key="-SCALE-", default_text=scale)],
        [sg.Text(message)],
        [sg.Button("Enter"), sg.Button("Exit")]
    ]
    window = sg.Window("3D Map Viewer", layout)
    while True:
        event, values = window.read()
        if event == "Enter" or event == "Exit":
            last_state = event
            break
        if event == sg.WIN_CLOSED:
            last_state = "Exit"
            break
        if event == "-COORDINATES-":
            input_val = values["-COORDINATES-"]
        if event == "-SCALE-":
            scale_input = values["-SCALE-"]
            if co.is_decimal(scale_input):
                scale = float(scale_input)
    window.close()
    if last_state == "Exit":
        return None
    x, y = co.convert_coords_to_decimals(input_val.split())
    return [x, y, scale, input_val, last_state]


'''
최소 힙
널리 잘 알려진 자료구조 중 최소 힙이 있다. 최소 힙을 이용하여 다음과 같은 연산을 지원하는 프로그램을 작성하시오.
1. 배열에 자연수 x를 넣는다.
2. 배열에서 가장 작은 값을 출력하고, 그 값을 배열에서 제거한다.
프로그램은 처음에 비어있는 배열에서 시작하게 된다.
'''
import heapq
from sys import stdin, stdout
heap = []
for _ in range(int(stdin.readline())):
    value = int(stdin.readline())
    if value==0:
        stdout.write(str(heapq.heappop(heap)) + '\n') if len(heap) != 0 else stdout.write(str(0) + '\n')
    else: heapq.heappush(heap, value)


from .base import get_registered_exporter_plugins_uids, exporter_registry
from .exceptions import ExporterDoesNotExist

__all__ = (
    'do_export',
)

def do_export(exporter_uid=None):
    """Export data.

    :param exporter_uid:
    :type exporter_uid: str
    :return:
    """
    counter = 0
    exporter_plugins_uids = get_registered_exporter_plugins_uids()
    if exporter_uid:
        if exporter_uid in exporter_plugins_uids:
            exporter_cls = exporter_registry.get(exporter_uid)
            exporter = exporter_cls()
            number_of_items = len(exporter.run())
            if number_of_items:
                counter += number_of_items
            exporter.teardown()
        else:
            raise ExporterDoesNotExist("{} does not exist".format(
                exporter_uid)
            )
    else:
        for exporter_uid in exporter_plugins_uids:
            exporter_cls = exporter_registry.get(exporter_uid)
            exporter = exporter_cls()
            number_of_items = len(exporter.run())
            if number_of_items:
                counter += number_of_items
            exporter.teardown()
    return counter


import pygame
import constants
from spritesheet_functions import SpriteSheet

class Player(pygame.sprite.Sprite):
        """ This class represents the player. """

        # Direction of the player
        direction = "R"
        speed_x = 0
        speed_y = 0
        walking_frames_l = []
        walking_frames_r = []
        walking_frames_u = []
        walking_frames_d = []

        def __init__(self):

                pygame.sprite.Sprite.__init__(self)
                sprite_sheet = SpriteSheet("Ninja.png")

                #This holds all the images for the animated walk

                #Load all the right facing images into a list
                image = sprite_sheet.get_image(14, 54, 24, 24)
                self.walking_frames_r.append(image)
                image = sprite_sheet.get_image(51, 56, 24, 24)
                self.walking_frames_r.append(image)
                image = sprite_sheet.get_image(86, 54, 24, 24)
                self.walking_frames_r.append(image)
                image = sprite_sheet.get_image(114, 57, 24, 24)
                self.walking_frames_r.append(image)

                #Load all the right facing images, tehn flip them(left)
                image = sprite_sheet.get_image(14, 54, 24, 24)
                image = pygame.transform.flip(image, True, False)
                self.walking_frames_l.append(image)
                image = sprite_sheet.get_image(51, 56, 24, 24)
                image = pygame.transform.flip(image, True, False)
                self.walking_frames_l.append(image)
                image = sprite_sheet.get_image(86, 54, 24, 24)
                image = pygame.transform.flip(image, True, False)
                self.walking_frames_l.append(image)
                image = sprite_sheet.get_image(114, 57, 24, 24)
                image = pygame.transform.flip(image, True, False)
                self.walking_frames_l.append(image)

                #Load UP facing images
                image = sprite_sheet.get_image(96, 118, 24, 24)
                self.walking_frames_u.append(image)
                image = sprite_sheet.get_image(123, 118, 24, 24)
                self.walking_frames_u.append(image)

                #Load Down facing images
                image = sprite_sheet.get_image(99, 90, 24, 24)
                self.walking_frames_d.append(image)

                #Set the image the player starts with
                self.image = self.walking_frames_r[0]

                #Set a referance to the image rect
                self.rect = self.image.get_rect()

        def update(self):
                self.rect.x += self.speed_x
                self.rect.y += self.speed_y
                posx = self.rect.x
                posu = self.rect.y
                if self.speed_x == 0 and self.speed_y == 0:
                        self.direction = "S"
                if self.direction == "R":
                        # Number 30 is the speed to change animations
                        frame = (posx // 30) % len(self.walking_frames_r)
                        self.image = self.walking_frames_r[frame]
                elif self.direction == "L":
                        frame = (posx // 30) % len(self.walking_frames_l)
                        self.image = self.walking_frames_l[frame]
                elif self.direction == "U":
                        frame = (posu // 30) % len(self.walking_frames_u)
                        self.image = self.walking_frames_u[frame]
                elif self.direction == "D":
                        frame = (posu // 30) % len(self.walking_frames_d)
                        self.image = self.walking_frames_d[frame]
                else:
                        self.image = self.walking_frames_r[0]

        #Player-controlled movement:
        def go_left(self):
                self.speed_x = -6
                self.direction = "L"

        def go_right(self):
                self.speed_x = 6
                self.direction = "R"

        def go_up(self):
                self.speed_y = -6
                self.direction = "U"

        def go_down(self):
                self.speed_y = 6
                self.direction = "D"

        def stopx(self):
                self.speed_x = 0

        def stopy(self):
                self.speed_y = 0


with open('picture.txt') as f:
    content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip() for x in content]
print(len(content))

g = open("newdata.txt", "w")

for i in range(0,len(content)):
	g.write(content[i] + " ")
	if((i%20)==19):
		g.write("\n")


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

import json
import argparse
from raw_data_preprocess_transfertransfo import *
from pathlib import Path

# set default path for data and test data
project_dir = Path(__file__).resolve().parent
datasets_dir = project_dir.joinpath('./data/')
images_test_dir = project_dir.joinpath('./online_test_data/images_test/')

def preprocessing_ctx(ctx_list):
    """预处理上下文text, 删除空行"""
    clean_ctx_list = []
    flag = False  # 标志上一轮是否为空(Q或者A)
    for single_ctx in ctx_list:
        if not single_ctx[2:]:
            # 当前轮为空
            flag = True
            continue
        if flag:
            # 直接将当前轮数据与clean_ctx_list[-1]拼接(他们是同一个人说的)
            clean_ctx_list[-1] = clean_ctx_list[-1] + "|||" + single_ctx[2:]
            flag = False
        else:
            # 原始数据不为空，直接不做任何处理
            clean_ctx_list.append(single_ctx)

    if len(clean_ctx_list) == 1 and clean_ctx_list[0][0] == 'Q':
        clean_ctx_list.append("A:~")  # 人为的多＋一个波浪线answer(主要是为了不丢弃这条Q)

    return clean_ctx_list

def gen_test_set(args):
    sess_length = args.sess_turns * 2
    with open(os.path.join(args.directory + 'test_questions.json'), mode='r', encoding='utf-8') as f:
        items = json.load(f)

    if args.kb_f:
        product_info = ProductInfo(args.kb_f)

    datasets = {}
    # all conversations
    conversations = []
    for item in items:
        # single conversation 所有数据
        conversation = {}
        utterances = []
        single_data = {}

        img_list = list()
        src_str = ''
        # ctx_str = ''
        # qus_str = ''

        ques_id = item['Id']
        session_id = item['SessionId']
        p_id = item["ProductId"]
        # ctx_list = item['Context']
        origin_ctx_list = item['Context']
        ques = item['Question']

        ctx_list = preprocessing_ctx(origin_ctx_list)
        ctx_str_list = []
        for idx, sent_i in enumerate(ctx_list):
            if sent_i == '':
                continue
            sent_i_type = sent_i[0]  # 说话人角色
            if (len(ctx_list) % 2 == 1
                    and sent_i_type == "A"
                    and idx == 0):
                # 历史轮数为奇数, 且第一轮为bot表达
                continue
            sent_i = sent_i[2:].strip()
            sent_i_list = sent_i.split('|||')

            single_ctx_str = str()
            for sent_j in sent_i_list:
                # 清理过长空格
                sent_j = ' '.join(sent_j.strip().split())
                # fir_ans = (idx <= 1 and sent_i_type == "A" and len(item['Context']) == len(ctx_list))
                # if match_robot(sent_j, fir_ans):
                #     continue
                if len(sent_j) > MAX_LEN:
                    # 超长截取
                    sent_j = sent_j[:MAX_LEN]

                if sent_j.endswith('.jpg'):
                    img_list.append(sent_j)
                    sent_j = '<img>'
                else:
                    img_list.append('NULL')
                    sent_j = clean_text(sent_j)
                if args.seg_voc:
                    sent_seg = ' '.join(tokenize_spt(sent_j.strip()))
                else:
                    # bert预训练模型不需要分词
                    sent_seg = sent_j.strip()

                if sent_seg:
                    src_str = src_str + sent_seg + '</s>'
                    single_ctx_str = single_ctx_str + sent_seg + ' '
                else:
                    img_list.pop(-1)
            ctx_str_list.append(single_ctx_str)

        assert len(ctx_str_list) % 2 == 0, "非2的倍数"
        ques_list = ques.split('|||')
        qus_str = str()
        for sent in ques_list:
            # 清理过长空格
            sent = ' '.join(sent.strip().split())
            if len(sent) > MAX_LEN:
                # 超长截取
                sent = sent[:MAX_LEN]

            if sent.endswith('.jpg'):
                img_list.append(sent)
                sent = '<img>'
            else:
                img_list.append('NULL')
                sent = clean_text(sent)

            if sent:
                if args.seg_voc:
                    sent_seg = ' '.join(tokenize_spt(sent.strip()))
                else:
                    sent_seg = sent.strip()
                src_str = src_str + sent_seg + '</s>'
                qus_str = qus_str + sent_seg + ' '  # 行与行之间使用空格隔开
            else:
                img_list.pop(-1)
        ctx_str_list.append(qus_str)

        if src_str.endswith("</s>"):
            src_str = src_str[:-4]
        img_str = ' '.join(img_list)
        src_list = src_str.split('</s>')
        assert len(src_list) == len(img_list)
        # lazy to reuse train dataloader code
        candidates = []
        trg_str = 'NULL'
        candidates.append(trg_str)
        # 收集单条数据
        single_data['candidates'] = candidates
        single_data['history'] = ctx_str_list
        single_data['img_list'] = img_list
        single_data['sid'] = session_id
        # 单条数据的集合
        utterances.append(single_data)

        pid_str = None
        if args.kb_f:
            # 获取PID info
            pid_str = product_info.get_info_by_pid(p_id)
            if args.seg_voc:
                # 分词
                seg_pid_str = pid_str
                single_seg_pid = tokenize_spt(seg_pid_str.strip())
                pid_str = str()  # 置空重新赋值
                pid_str = ' '.join(single_seg_pid)

        conversation['personality'] = pid_str
        conversation['utterances'] = utterances
        # 将conversation数据存入列表
        conversations.append(conversation)
    # 将测试数据加入datasets
    datasets['test'] = conversations

    save_info_to_json(os.path.join(datasets_dir, args.out_json_name), datasets)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="tool to process online test data")

    parser.add_argument('-d', '--directory', default='online_test_data/')
    parser.add_argument('-t', '--sess_turns', default=2)
    parser.add_argument('-out', '--out_json_name', default='test_datasets.json')
    parser.add_argument('-f', '--kb_f', default='./data/kb_info.json')
    parser.add_argument('--seg_voc', action='store_true', default=True, help='是否分词')

    args = parser.parse_args()

    # get the online test file to generate internal data format
    gen_test_set(args)


from algorithms.avl_tree import AVLTree
from triangular_grid.node import Node
from triangular_grid.grid import Grid
from tecplot.reader import read_tecplot
from triangular_grid.face import Face
from geom.vector import Vector
from geom.point import Point
from geom.basics import *

def test_comparing_of_nodes():
    n1 = Node(0, 0, 0, 1)
    n2 = Node(1, 0, 0, 2)
    n3 = Node(10e-18, 0, 0)
    n4 = Node(10e-17, 0, 0)
    n5 = Node(0, 10e-18, 0)
    n6 = Node(0, 10e-17, 0)
    n7 = Node(0, 0, 10e-18)
    n8 = Node(0, 0, 10e-17)
    assert AVLTree.is_node_less(n1, n2), 'Wrong node comparison'
    assert AVLTree.is_node_less(n3, n4), 'Wrong node comparison'
    assert AVLTree.is_node_less(n5, n6), 'Wrong node comparison'
    assert AVLTree.is_node_less(n7, n8), 'Wrong node comparison'
    print('Node comparison OK')

def test_avl():
    avl = AVLTree()

    n1 = Node(0, 0, 0, 1)
    n2 = Node(1, 0, 0, 2)
    n3 = Node(-1, 0, 0, 3)
    n4 = Node(1, 1, 0, 4)
    n5 = Node(1, -1, 0, 5)
    n6 = Node(1, -1, 1, 6)

    nodes = [n1, n2, n3, n4, n5]

    for n in nodes:
        avl.insert(n)

    assert avl.find(n5).Id == 5, 'Wrong node found'
    assert not avl.find(n6), 'Wrong node found'
    print('Test 1 OK')

def test2():
    avl = AVLTree()

    n1 = Node(0, 0, 0, 1)
    n2 = Node(1, 0, 0, 2)
    n3 = Node(-1, 0, 0, 3)
    n4 = Node(1, 1, 0, 4)
    n5 = Node(1, -1, 0.000000000000001, 5)
    n6 = Node(1, -1, 0.0000000000000001, 6)

    nodes = [n1, n2, n3, n4, n5]

    for n in nodes:
        avl.insert(n)

    assert avl.find(n5).Id == 5, 'Wrong node found'
    assert not avl.find(n6), 'Wrong node found'
    print('Test 2 OK')

def test3():
    avl = AVLTree()

    n1 = Node(0, 0, 0, 1)
    n2 = Node(1, 0, 0, 2)
    n4 = Node(-1, 1, 0, 4)
    n3 = Node(-1, 0, 0, 3)
    n5 = Node(-1, 2, 0, 5)
    n6 = Node(-1, 3, 0, 6)
    n8 = Node(-1, 1.0000000000001, 0, 8)
    n9 = Node(0.0000000000000001, 0, 0, 9)

    nodes = [n1, n2, n3, n4, n5, n6]

    for n in nodes:
        avl.insert(n)

    assert not avl.find(n8), 'Wrong nearest neighbour found. test3.'
    assert not avl.find(n9), 'Wrong nearest neighbour found. test3.'
    print('Test 3 OK')

def test_isomorphism():
    grid = Grid()
    isomorphic_grid = Grid()
    non_isomorphic_grid = Grid()
    read_tecplot(grid, '../wing_1.dat')
    read_tecplot(isomorphic_grid, '../wing_1_tm.dat')
    read_tecplot(non_isomorphic_grid, '../wing_10.dat')

    assert grid.is_isomprphic_to(isomorphic_grid), 'Grids are not isomorphic'
    assert not grid.is_isomprphic_to(non_isomorphic_grid), 'Grids are isomorphic, but shouldn\'t be'
    print('Test isomorphism OK')

def test_normals():
    n1 = Node(0, 0, 0)
    n2 = Node(0, -1, 0)
    n3 = Node(1, 0, 0)
    f = Face()
    f.nodes = [n1, n2, n3]
    assert f.normal().x == 0, 'Wrong normal'
    assert f.normal().y == 0, 'Wrong normal'
    assert f.normal().z == 1, 'Wrong normal'

    n1 = Node(0, 0, 0)
    n2 = Node(0, -1, 0)
    n3 = Node(0, -2, 1)
    f = Face()
    f.nodes = [n1, n2, n3]
    assert f.normal().x == -1, 'Wrong normal'
    assert f.normal().y == 0, 'Wrong normal'
    assert f.normal().z == 0, 'Wrong normal'

def test_dot():
    v1 = Vector(1, 1, 1)
    v2 = Vector(1, 1, 1)
    assert dot_product(v1, v2) == 3, 'Wrong dot product'

    v1 = Vector(0, 0, 0)
    v2 = Vector(0, 0, 0)
    assert dot_product(v1, v2) == 0, 'Wrong dot product'

    v1 = Vector(-1, 0, 1)
    v2 = Vector(2, 1, -1)
    assert dot_product(v1, v2) == -3, 'Wrong dot product'

    v1 = Vector(0.5, 4, -1)
    v2 = Vector(2, 3, 1)
    assert dot_product(v1, v2) == 12, 'Wrong dot product'

def test_cross():
    v1 = Vector(1, 1, 1)
    v2 = Vector(1, 1, 1)
    assert cross_product(v1, v2).x == 0, 'Wrong cross product'
    assert cross_product(v1, v2).y == 0, 'Wrong cross product'
    assert cross_product(v1, v2).z == 0, 'Wrong cross product'

    v1 = Vector(-1, 4, 19)
    v2 = Vector(1.5, 5, -3)
    assert cross_product(v1, v2).x == -107, 'Wrong cross product'
    assert cross_product(v1, v2).y == 25.5, 'Wrong cross product'
    assert cross_product(v1, v2).z == -11, 'Wrong cross product'

    v1 = Vector(-1, -1, -13)
    v2 = Vector(-32, -1, -12.4)
    assert cross_product(v1, v2).x == -0.5999999999999996, 'Wrong cross product'
    assert cross_product(v1, v2).y == 403.6, 'Wrong cross product'
    assert cross_product(v1, v2).z == -31, 'Wrong cross product'

def test_area():
    n1 = Node(0, 0, 0)
    n2 = Node(0, -0.5, 0)
    n3 = Node(1, -0.5, 0)
    f = Face()
    f.nodes = [n1, n2, n3]
    assert f.area() == 0.25, 'Wrong area'

    n1 = Node(10, -7, 1.45)
    n2 = Node(82, -0.5, 0)
    n3 = Node(1.222, 56, -18)
    f = Face()
    f.nodes = [n1, n2, n3]
    assert f.area() == 2402.8282235671577, 'Wrong area'

    n1 = Node(0.0001, 0.1122, -0.00012)
    n2 = Node(0.000001, -0.23123, -0.00000005)
    n3 = Node(1.222, -0.231200000001, -18)
    f = Face()
    f.nodes = [n1, n2, n3]
    assert f.area() == 3.0979846549411496, print(f.area())

def test_vector():
    v = Vector()
    v2 = Vector(1, 0.1, -1)
    assert v.coords() == (0, 0, 0), "Wrong vector initialization"
    v.sum(v2)
    assert v.coords() == (1, 0.1, -1), 'Wrong vector sum'
    v.sub(v2)
    assert v.coords() == (0, 0, 0), 'Wrong subtraction'
    v.sum(v2)
    assert v.coords() == (1, 0.1, -1), print('Wrong vector sum', v.coords())
    v.mul(3.1)
    assert v.coords() == (3.1, 0.31000000000000005, -3.1), print('Wrong vector multiplication', v.coords())
    v.dev(2)
    assert v.coords() == (1.55, 0.15500000000000003, -1.55), print('Wrong vector division', v.coords())

def test_alpha_quality_measure():
    f = Face()
    n1 = Node(0, 0, 0)
    n2 = Node(1, 0, 0)
    n3 = Node(0.5, 0, 0.866)
    e1 = Edge()
    e1.nodes = [n1, n2]
    e2 = Edge()
    e2.nodes = [n2, n3]
    e3 = Edge()
    e3.nodes = [n3, n1]
    f.edges = [e1, e2, e3]
    f.nodes = [n1, n2, n3]
    assert 1 - f.alpha_quality_measure() < 10e-6, print(f.alpha_quality_measure())

def test_all():
    test_comparing_of_nodes()
    test_avl()
    test2()
    test3()
    # test_isomorphism()
    test_vector()
    test_normals()
    test_dot()
    test_cross()
    test_area()
    test_alpha_quality_measure()

if __name__ == '__main__':
    test_all()


from __future__ import absolute_import
import logging
import os
import shutil
import json
from collections import deque
import time
import re
import datetime
import traceback
import numpy as np
from typing import Dict, Optional, List, Tuple, Union, Iterable, Any, Set
import pdb

import torch
import torch.optim.lr_scheduler
from torch.nn.parallel import replicate, parallel_apply
from torch.nn.parallel.scatter_gather import scatter_kwargs, gather
from tensorboardX import SummaryWriter

from allennlp.common import Params
from allennlp.common.checks import ConfigurationError
from allennlp.common.util import peak_memory_mb, gpu_memory_mb
from allennlp.common.tqdm import Tqdm
from allennlp.data.instance import Instance
from allennlp.data.iterators.data_iterator import DataIterator
from allennlp.models.model import Model
from allennlp.nn import util
from allennlp.training.learning_rate_schedulers import LearningRateScheduler
from allennlp.training.optimizers import Optimizer

from AttentionSegmentation.commons.trainer_utils import is_sparse,\
    sparse_clip_norm, move_optimizer_to_cuda, TensorboardWriter
# from AttentionSegmentation.visualization.visualize_attns \
#     import html_visualizer
from AttentionSegmentation.model.attn2labels import BasePredictionClass
logger = logging.getLogger(__name__)

TQDM_COLUMNS = 200

class Trainer(object):
    def __init__(self,
                 model: Model,
                 optimizer: torch.optim.Optimizer,
                 iterator: DataIterator,
                 train_dataset: Iterable[Instance],
                 validation_dataset: Optional[Iterable[Instance]] = None,
                 segmenter: Optional[BasePredictionClass] = None,
                 patience: Optional[int] = None,
                 validation_metric: str = "-loss",
                 num_epochs: int = 20,
                 base_dir: Optional[str] = None,
                 num_serialized_models_to_keep: Optional[int] = None,
                 cuda_device: Union[int, List] = -1,
                 grad_norm: Optional[float] = None,
                 grad_clipping: Optional[float] = None,
                 learning_rate_scheduler: Optional[LearningRateScheduler] = None
                 ) -> None:
        """
        Parameters
        ----------
        model : ``Model``, required.
            An AllenNLP model to be optimized. Pytorch Modules can also be optimized if
            their ``forward`` method returns a dictionary with a "loss" key, containing a
            scalar tensor representing the loss function to be optimized.
        optimizer : ``torch.nn.Optimizer``, required.
            An instance of a Pytorch Optimizer, instantiated with the parameters of the
            model to be optimized.
        iterator : ``DataIterator``, required.
            A method for iterating over a ``Dataset``, yielding padded indexed batches.
        train_dataset : ``Dataset``, required.
            A ``Dataset`` to train on. The dataset should have already been indexed.
        validation_dataset : ``Dataset``, optional, (default = None).
            A ``Dataset`` to evaluate on. The dataset should have already been indexed.
        segmenter : ``BasePredictionClass`` for converting attention segmentation to labels.
            None if we don't want to visualize
        patience : Optional[int] > 0, optional (default=None)
            Number of epochs to be patient before early stopping: the training is stopped
            after ``patience`` epochs with no improvement. If given, it must be ``> 0``.
            If None, early stopping is disabled.
        validation_metric : str, optional (default="loss")
            Validation metric to measure for whether to stop training using patience
            and whether to serialize an ``is_best`` model each epoch. The metric name
            must be prepended with either "+" or "-", which specifies whether the metric
            is an increasing or decreasing function.
            Allowed values are -loss, +precision-overall, +recall-overall, +f1-measure-overall
        num_epochs : int, optional (default = 20)
            Number of training epochs.
        base_dir : str, optional (default=None)
            Path to directory for the experiment. Models will be saved at
            {base_dir}/models, and won't be saved if this parameter is not passed.
        num_serialized_models_to_keep: int, optional (default=None)
            The number of models to keep during training
        cuda_device : ``int``, optional (default = -1)
            An integer specifying the CUDA device to use. If -1, the CPU is used.
        grad_norm : ``float``, optional, (default = None).
            If provided, gradient norms will be rescaled to have a maximum of this value.
        grad_clipping : ``float``, optional (default = ``None``).
            If provided, gradients will be clipped `during the backward pass` to have an (absolute)
            maximum of this value.  If you are getting ``NaNs`` in your gradients during training
            that are not solved by using ``grad_norm``, you may need this.
        learning_rate_scheduler : ``PytorchLRScheduler``, optional, (default = None)
            A Pytorch learning rate scheduler. The learning rate will be decayed with respect to
            this schedule at the end of each epoch. If you use
            :class:`torch.optim.lr_scheduler.ReduceLROnPlateau`, this will use the ``validation_metric``
            provided to determine if learning has plateaued.  To support updating the learning
            rate on every batch, this can optionally implement ``step_batch(batch_num_total)`` which
            updates the learning rate given the batch number.
        visualization_dirname : The directory to save the visualization in

        """
        self._model = model
        self._iterator = iterator
        self._optimizer = optimizer
        self._train_data = train_dataset
        self._validation_data = validation_dataset
        self._segmenter = segmenter
        self._base_dir = base_dir

        if patience is None:  # no early stopping
            if validation_dataset:
                logger.warning('You provided a validation dataset but patience was set to None, '
                               'meaning that early stopping is disabled')
        elif (not isinstance(patience, int)) or patience <= 0:
            raise ConfigurationError('{} is an invalid value for "patience": it must be a positive integer '
                                     'or None (if you want to disable early stopping)'.format(patience))
        self._patience = patience
        self._num_epochs = num_epochs

        serialization_dir = None
        if base_dir is not None:
            serialization_dir = os.path.join(base_dir, "models")
        self._serialization_dir = serialization_dir
        assert os.path.exists(self._serialization_dir), "Directory {0} does not exist".format(self._serialization_dir)

        self._serialized_paths = deque()
        # self._last_permanent_saved_checkpoint_time = time.time()
        # self._model_save_interval = model_save_interval

        self._grad_norm = grad_norm
        self._grad_clipping = grad_clipping
        self._learning_rate_scheduler = learning_rate_scheduler

        increase_or_decrease = validation_metric[0]
        if increase_or_decrease not in ["+", "-"]:
            raise ConfigurationError("Validation metrics must specify whether they should increase "
                                     "or decrease by pre-pending the metric name with a +/-.")
        self._validation_metric = validation_metric[1:]
        self._validation_metric_decreases = increase_or_decrease == "-"

        if not isinstance(cuda_device, int) and not isinstance(cuda_device, list):
            raise ConfigurationError("Expected an int or list for cuda_device, got {}".format(cuda_device))

        if isinstance(cuda_device, list):
            logger.info(f"WARNING: Multiple GPU support is experimental not recommended for use. "
                        "In some cases it may lead to incorrect results or undefined behavior.")
            self._multiple_gpu = True
            self._cuda_devices = cuda_device
            # data_parallel will take care of transfering to cuda devices,
            # so the iterator keeps data on CPU.
            self._iterator_device = -1
        else:
            self._multiple_gpu = False
            self._cuda_devices = [cuda_device]
            self._iterator_device = cuda_device

        if self._cuda_devices[0] != -1:
            self._model = self._model.cuda(self._cuda_devices[0])

        self._log_interval = 10  # seconds

        self._batch_num_total = 0

        self._last_log = 0.0  # time of last logging
        self._num_serialized_models_to_keep = num_serialized_models_to_keep

    def _enable_gradient_clipping(self) -> None:
        if self._grad_clipping is not None:
            # Pylint is unable to tell that we're in the case that _grad_clipping is not None...
            # pylint: disable=invalid-unary-operand-type
            clip_function = lambda grad: grad.clamp(-self._grad_clipping, self._grad_clipping)
            for parameter in self._model.parameters():
                if parameter.requires_grad:
                    parameter.register_hook(clip_function)

    def _rescale_gradients(self) -> Optional[float]:
        """
        Performs gradient rescaling.
        Is a no-op if gradient rescaling is not enabled.
        """
        if self._grad_norm:
            parameters_to_clip = [p for p in self._model.parameters()
                                  if p.grad is not None]
            return sparse_clip_norm(parameters_to_clip, self._grad_norm)
        return None

    def _data_parallel(self, batch):
        """
        Do the forward pass using multiple GPUs.  This is a simplification
        of torch.nn.parallel.data_parallel to support the allennlp model
        interface.
        """
        inputs, module_kwargs = scatter_kwargs((), batch,
                                               self._cuda_devices, 0)
        used_device_ids = self._cuda_devices[:len(inputs)]
        replicas = replicate(self._model, used_device_ids)
        outputs = parallel_apply(replicas, inputs,
                                 module_kwargs, used_device_ids)

        # Only the 'loss' is needed.
        # a (num_gpu, ) tensor with loss on each GPU
        losses = gather([output['loss']
                         for output in outputs], used_device_ids[0], 0)
        return {'loss': losses.mean()}

    def _batch_loss(self, batch: torch.Tensor,
                    for_training: bool) -> torch.Tensor:
        """
        Does a forward pass on the given batch
        and returns the ``loss`` value in the result.
        If ``for_training`` is `True` also applies regularization penalty.
        """
        if self._multiple_gpu:
            output_dict = self._data_parallel(batch)
        else:
            output_dict = self._model(**batch)

        try:
            loss = output_dict["loss"]
            if for_training:
                loss += self._model.get_regularization_penalty()
        except KeyError:
            if for_training:
                raise RuntimeError("The model you are trying to optimize does not contain a"
                                   " 'loss' key in the output of model.forward(inputs).")
            loss = None

        return loss

    def _get_metrics(self, total_loss: float,
                     num_batches: int, reset: bool = False) -> Dict[str, float]:
        """
        Gets the metrics but sets ``"loss"`` to
        the total loss divided by the ``num_batches`` so that
        the ``"loss"`` metric is "average loss per batch".
        """
        metrics = self._model.get_metrics(reset=reset)
        metrics["loss"] = float(total_loss / num_batches) if num_batches > 0 else 0.0
        return metrics

    def _train_epoch(self, epoch: int) -> Dict[str, float]:
        """
        Trains one epoch and returns metrics.
        """
        logger.info(f"Peak CPU memory usage MB: {peak_memory_mb()}")
        if torch.cuda.is_available():
            for gpu, memory in gpu_memory_mb().items():
                logger.info(f"GPU {gpu} memory usage MB: {memory}")

        train_loss = 0.0
        # Set the model to "train" mode.
        self._model.train()

        # Get tqdm for the training batches
        train_generator = self._iterator(self._train_data,
                                         num_epochs=1,
                                         cuda_device=self._iterator_device)
        num_training_batches = self._iterator.get_num_batches(self._train_data)
        train_generator_tqdm = Tqdm.tqdm(train_generator,
                                         total=num_training_batches
                                         )
        self._last_log = time.time()
        last_save_time = time.time()

        batches_this_epoch = 0
        if self._batch_num_total is None:
            self._batch_num_total = 0

        for batch in train_generator_tqdm:
            batches_this_epoch += 1
            self._batch_num_total += 1
            batch_num_total = self._batch_num_total

            self._optimizer.zero_grad()
            loss = self._batch_loss(batch, for_training=True)
            loss.backward()

            # Make sure Variable is on the cpu before converting to numpy.
            # .cpu() is a no-op if you aren't using GPUs.
            train_loss += loss.data.cpu().numpy()

            batch_grad_norm = self._rescale_gradients()

            # This does nothing if batch_num_total is None or you are using an
            # LRScheduler which doesn't update per batch.
            if self._learning_rate_scheduler:
                self._learning_rate_scheduler.step_batch(batch_num_total)
            self._optimizer.step()

            # Update the description with the latest metrics
            metrics = self._get_metrics(train_loss, batches_this_epoch)
            description = self._description_from_metrics(metrics)

            train_generator_tqdm.set_description(description, refresh=False)
            if hasattr(self, "_tf_params") and self._tf_params is not None:
                # We have TF logging
                if self._batch_num_total % self._tf_params["log_every"] == 0:
                    self._tf_log(metrics, self._batch_num_total)

        return self._get_metrics(train_loss, batches_this_epoch, reset=True)

    def _should_stop_early(self, metric_history: List[float]) -> bool:
        """
        uses patience and the validation metric to determine if training should stop early
        """
        if self._patience and self._patience < len(metric_history):
            # Pylint can't figure out that in this branch `self._patience` is an int.
            # pylint: disable=invalid-unary-operand-type

            # Is the best score in the past N epochs worse than the best score overall?
            if self._validation_metric_decreases:
                return min(metric_history[-self._patience:]) > min(metric_history)
            else:
                return max(metric_history[-self._patience:]) < max(metric_history)

        return False

    def _metrics_to_console(self,  # pylint: disable=no-self-use
                            train_metrics: dict,
                            val_metrics: dict = None) -> None:
        """
        Logs all of the train metrics (and validation metrics, if provided) to the console.
        """
        val_metrics = val_metrics or {}
        dual_message_template = "Training %s : %3f    Validation %s : %3f "
        message_template = "%s %s : %3f "

        metric_names = set(sorted(train_metrics.keys()))
        if val_metrics:
            metric_names.update(val_metrics.keys())

        write_buf = "Metrics:\n"

        for name in metric_names:
            train_metric = train_metrics.get(name)
            val_metric = val_metrics.get(name)
            if val_metric is not None and train_metric is not None:
                training = "Training"
                validation = "Validation"
                write_buf += f"{training:>32s} {name:18s}: {train_metric:4.3f}  "
                write_buf += f"{validation} {name:18s}: {val_metric:4.3f}\n"
            elif val_metric is not None:
                # logger.info(message_template, "Validation", name, val_metric)
                write_buf += "{0:>32s} {1:18s}: {2:4.3f}".format(
                    "Validation", name, val_metric)
            elif train_metric is not None:
                # logger.info(message_template, "Training", name, train_metric)
                write_buf += "{0:>32s} {1:18s}: {2:4.3f}".format(
                    "Training", name, train_metric)
        logger.info(write_buf)

    def test(self, data):
        """
            Computes the metrics for data
        """
        model_path = os.path.join(self._serialization_dir, "best.th")
        logger.info("Loading best model from {0}".format(model_path))
        model_state = torch.load(model_path,
                                 map_location=util.device_mapping(-1))
        self._model.load_state_dict(model_state)
        loss, num_batches = self._inference_loss(data, logger_string="Testing")
        metrics = self._get_metrics(loss, num_batches, reset=True)
        metric_names = metrics.keys()
        message_template = "%s %s : %3f"
        for name in metric_names:
            test_metric = metrics.get(name)
            logger.info(message_template, "Test", name, test_metric)
        return metrics

    def _inference_loss(self, data,
                        logger_string="validation") -> Tuple[float, int]:
        """
        Computes the loss on the data passed.
        Returns it and the number of batches.
        Is primarily used for validation and testing
        """
        logger.info("Starting with {0}".format(logger_string))

        self._model.eval()

        inference_generator = self._iterator(data,
                                             num_epochs=1,
                                             cuda_device=self._iterator_device,
                                             for_training=False)
        num_batches = self._iterator.get_num_batches(data)
        inference_generator_tqdm = Tqdm.tqdm(inference_generator,
                                             total=num_batches
                                             )
        batches_this_epoch = 0
        inference_loss = 0
        for batch in inference_generator_tqdm:

            loss = self._batch_loss(batch, for_training=False)
            if loss is not None:
                # You shouldn't necessarily have to compute a loss for validation, so we allow for
                # `loss` to be None.  We need to be careful, though - `batches_this_epoch` is
                # currently only used as the divisor for the loss function, so we can safely only
                # count those batches for which we actually have a loss.  If this variable ever
                # gets used for something else, we might need to change things around a bit.
                batches_this_epoch += 1
                inference_loss += loss.data.cpu().numpy()

            # Update the description with the latest metrics
            inference_metrics = self._get_metrics(inference_loss, batches_this_epoch)
            description = self._description_from_metrics(inference_metrics)
            inference_generator_tqdm.set_description(description, refresh=False)
        logger.info("{0} done".format(logger_string))

        return inference_loss, batches_this_epoch

    def _get_validation_metric(self, val_metrics):
        return val_metrics[self._validation_metric]

    def train(self, continue_training=False) -> Dict[str, Any]:
        """
        Trains the supplied model with the supplied parameters.

        Parameters
        ------------
        continue_training: bool (default=False)
            Load a checkpoint and start training
        """
        epoch_counter, validation_metric_per_epoch = 0, []
        if continue_training:
            try:
                epoch_counter, validation_metric_per_epoch = self._restore_checkpoint()
            except RuntimeError:
                traceback.print_exc()
                raise ConfigurationError("Could not recover training from the checkpoint.  Did you mean to output to "
                                         "a different serialization directory or delete the existing serialization "
                                         "directory?")

        self._enable_gradient_clipping()

        logger.info("Beginning training.")

        train_metrics: Dict[str, float] = {}
        val_metrics: Dict[str, float] = {}
        epochs_trained = 0
        training_start_time = time.time()
        for epoch in range(epoch_counter, self._num_epochs):
            logger.info("=" * 50)
            logger.info("Starting Training Epoch %d/%d",
                        epoch + 1, self._num_epochs)
            epoch_start_time = time.time()
            train_metrics = self._train_epoch(epoch)

            if self._validation_data is not None:
                # We have a validation set, so compute all the metrics on it.
                val_loss, num_batches = self._inference_loss(
                    self._validation_data, logger_string="Validation")
                val_metrics = self._get_metrics(
                    val_loss, num_batches, reset=True)

                # Check validation metric for early stopping
                this_epoch_val_metric = self._get_validation_metric(
                    val_metrics)
                validation_metric_per_epoch.append(this_epoch_val_metric)
                if self._should_stop_early(validation_metric_per_epoch):
                    logger.info("Ran out of patience. Stopping training.")
                    break

                # Check validation metric to see if it's the best so far
                if self._validation_metric_decreases:
                    is_best_so_far = this_epoch_val_metric == min(
                        validation_metric_per_epoch)
                else:
                    is_best_so_far = this_epoch_val_metric == max(
                        validation_metric_per_epoch)
            else:
                # No validation set, so just assume it's the best so far.
                is_best_so_far = True
                val_metrics = {}
                this_epoch_val_metric = None

            self._save_checkpoint(
                epoch, validation_metric_per_epoch, is_best=is_best_so_far)
            self._metrics_to_console(train_metrics, val_metrics)
            # Now the predictions and visualization
            if self._segmenter is not None and is_best_so_far:
                visualization_file = os.path.join(
                    self._base_dir, "visualization", "validation.html")
                prediction_file = os.path.join(
                    self._base_dir, "predictions.json")
                logger.info(
                    f"Writing validation visualization at {visualization_file}"
                )
                self._segmenter.get_predictions(
                    instances=self._validation_data,
                    model=self._model,
                    cuda_device=self._iterator_device,
                    prediction_file=prediction_file,
                    visualization_file=visualization_file,
                    verbose=True)
                logger.info(f"Writing predictions to {prediction_file}")

            if self._learning_rate_scheduler:
                # The LRScheduler API is agnostic to whether your schedule requires a validation metric -
                # if it doesn't, the validation metric passed here is ignored.
                old_lr = min(param_group['lr']
                             for param_group in self._optimizer.param_groups)
                self._learning_rate_scheduler.step(
                    this_epoch_val_metric, epoch)
                new_lr = min(param_group['lr']
                             for param_group in self._optimizer.param_groups)
                logger.info("Reducing LR: {0:.2e} -> {1:.2e}".format(
                    old_lr, new_lr))

            epoch_elapsed_time = time.time() - epoch_start_time
            logger.info("Epoch duration: %s",
                        time.strftime("%H:%M:%S",
                                      time.gmtime(epoch_elapsed_time)))

            if epoch < self._num_epochs - 1:
                training_elapsed_time = time.time() - training_start_time
                estimated_time_remaining = training_elapsed_time * \
                    ((self._num_epochs - epoch_counter) / float(
                        epoch - epoch_counter + 1) - 1)
                formatted_time = time.strftime(
                    "%H:%M:%S", time.gmtime(estimated_time_remaining))
                logger.info("Estimated training time remaining: %s",
                            formatted_time)

            epochs_trained += 1
            logger.info("=" * 50)

        training_elapsed_time = time.time() - training_start_time
        metrics = {
            "training_duration": time.strftime(
                "%H:%M:%S", time.gmtime(training_elapsed_time)),
            "training_start_epoch": epoch_counter,
            "training_epochs": epochs_trained
        }
        for key, value in train_metrics.items():
            metrics["training_" + key] = value
        for key, value in val_metrics.items():
            metrics["validation_" + key] = value

        if validation_metric_per_epoch:
            # We may not have had validation data, so we need to hide this behind an if.
            if self._validation_metric_decreases:
                best_validation_metric = min(validation_metric_per_epoch)
            else:
                best_validation_metric = max(validation_metric_per_epoch)
            best_val_key = f"best_validation_{self._validation_metric}"
            metrics[best_val_key] = best_validation_metric
            metrics['best_epoch'] = [
                i for i, value in enumerate(validation_metric_per_epoch)
                if value == best_validation_metric][-1]
        return metrics

    def _description_from_metrics(self, metrics: Dict[str, float]) -> str:
        # pylint: disable=no-self-use
        return ', '.join(["%s: %.4f" % (name, value)
                          for name, value in metrics.items()]) + " ||"

    def _save_checkpoint(self,
                         epoch: Union[int, str],
                         val_metric_per_epoch: List[float],
                         is_best: Optional[bool] = None) -> None:
        """
        Saves a checkpoint of the model to self._serialization_dir.
        Is a no-op if self._serialization_dir is None.

        Parameters
        ----------
        epoch : Union[int, str], required.
            The epoch of training.  If the checkpoint is saved in the middle
            of an epoch, the parameter is a string with the epoch and timestamp.
        is_best: bool, optional (default = None)
            A flag which causes the model weights at the given epoch to
            be copied to a "best.th" file. The value of this flag should
            be based on some validation metric computed by your model.
        """
        if self._serialization_dir is not None:
            model_path = os.path.join(self._serialization_dir,
                                      "model_state_epoch_{}.th".format(epoch))
            model_state = self._model.state_dict()
            torch.save(model_state, model_path)

            training_state = {'epoch': epoch,
                              'val_metric_per_epoch': val_metric_per_epoch,
                              'optimizer': self._optimizer.state_dict(),
                              'batch_num_total': self._batch_num_total}
            training_path = os.path.join(
                self._serialization_dir,
                "training_state_epoch_{}.th".format(epoch))
            torch.save(training_state, training_path)
            if is_best:
                logger.info("Best validation performance so far. "
                            "Copying weights to '%s/best.th'.", self._serialization_dir)
                shutil.copyfile(
                    model_path, os.path.join(
                        self._serialization_dir, "best.th"))

            if self._num_serialized_models_to_keep:
                self._serialized_paths.append(
                    [time.time(), model_path, training_path])
                if len(self._serialized_paths) > self._num_serialized_models_to_keep:
                    paths_to_remove = self._serialized_paths.popleft()
                    for fname in paths_to_remove[1:]:
                        os.remove(fname)

    def _restore_checkpoint(self) -> Tuple[int, List[float]]:
        """
        Restores a model from a serialization_dir to the last saved checkpoint.
        This includes an epoch count and optimizer state, which is serialized separately
        from  model parameters. This function should only be used to continue training -
        if you wish to load a model for inference/load parts of a model into a new
        computation graph, you should use the native Pytorch functions:
        `` model.load_state_dict(torch.load("/path/to/model/weights.th"))``

        If ``self._serialization_dir`` does not exist or does not contain any checkpointed weights,
        this function will do nothing and return 0.

        Returns
        -------
        epoch: int
            The epoch at which to resume training, which should be one after the epoch
            in the saved training state.
        """
        have_checkpoint = (
            self._serialization_dir is not None and
            any(
                "model_state_epoch_" in x
                for x in os.listdir(os.path.join(self._serialization_dir))
            )
        )
        if not have_checkpoint:
            # No checkpoint to restore, start at 0
            logger.info("Didn't find any models to restore."
                        "Starting training from scratch")
            return 0, []

        serialization_files = os.listdir(
            os.path.join(self._serialization_dir))
        model_checkpoints = [x for x in serialization_files
                             if "model_state_epoch" in x]
        # Get the last checkpoint file.  Epochs are specified as either an
        # int (for end of epoch files) or with epoch and timestamp for
        # within epoch checkpoints, e.g. 5.2018-02-02-15-33-42
        found_epochs = [
            # pylint: disable=anomalous-backslash-in-string
            re.search("model_state_epoch_([0-9\.\-]+)\.th", x).group(1)
            for x in model_checkpoints
        ]
        int_epochs: Any = []
        for epoch in found_epochs:
            pieces = epoch.split('.')
            if len(pieces) == 1:
                # Just a single epoch without timestamp
                int_epochs.append([int(pieces[0]), 0])
            else:
                # has a timestamp
                int_epochs.append([int(pieces[0]), pieces[1]])
        last_epoch = sorted(int_epochs, reverse=True)[0]
        if last_epoch[1] == 0:
            epoch_to_load = str(last_epoch[0])
        else:
            epoch_to_load = '{0}.{1}'.format(last_epoch[0], last_epoch[1])

        model_path = os.path.join(
            self._serialization_dir,
            "model_state_epoch_{}.th".format(epoch_to_load))
        training_state_path = os.path.join(
            self._serialization_dir,
            "training_state_epoch_{}.th".format(epoch_to_load))

        # Load the parameters onto CPU, then transfer to GPU.
        # This avoids potential OOM on GPU for large models that
        # load parameters onto GPU then make a new GPU copy into the parameter
        # buffer. The GPU transfer happens implicitly in load_state_dict.
        model_state = torch.load(
            model_path, map_location=util.device_mapping(-1))
        training_state = torch.load(
            training_state_path, map_location=util.device_mapping(-1))
        self._model.load_state_dict(model_state)
        self._optimizer.load_state_dict(training_state["optimizer"])
        move_optimizer_to_cuda(self._optimizer)

        # We didn't used to save `validation_metric_per_epoch`, so we can't assume
        # that it's part of the trainer state. If it's not there, an empty list is all
        # we can do.
        if "val_metric_per_epoch" not in training_state:
            logger.warning("trainer state `val_metric_per_epoch`"
                           "not found, using empty list")
            val_metric_per_epoch: List[float] = []
        else:
            val_metric_per_epoch = training_state["val_metric_per_epoch"]

        if isinstance(training_state["epoch"], int):
            epoch_to_return = training_state["epoch"] + 1
        else:
            epoch_to_return = int(training_state["epoch"].split('.')[0]) + 1

        # For older checkpoints with batch_num_total missing, default to old behavior where
        # it is unchanged.
        batch_num_total = training_state.get('batch_num_total')
        if batch_num_total is not None:
            self._batch_num_total = batch_num_total

        return epoch_to_return, val_metric_per_epoch

    @classmethod
    def get_args(cls,
                 model: Model,
                 base_dir: str,
                 iterator: DataIterator,
                 train_data: Iterable[Instance],
                 validation_data: Optional[Iterable[Instance]],
                 segmenter: Optional[BasePredictionClass],
                 params: Params) -> Dict[str, Any]:
        patience = params.pop_int("patience", None)
        validation_metric = params.pop("validation_metric", "-loss")
        num_epochs = params.pop_int("num_epochs", 20)
        cuda_device = params.pop_int("cuda_device", -1)
        grad_norm = params.pop_float("grad_norm", None)
        grad_clipping = params.pop_float("grad_clipping", None)
        lr_scheduler_params = params.pop("learning_rate_scheduler", None)
        num_serialized_models_to_keep = params.pop(
            "num_serialized_models_to_keep", None)

        if cuda_device >= 0:
            model = model.cuda(cuda_device)
        parameters = [[n, p] for n, p in model.named_parameters()
                      if p.requires_grad]
        optimizer = Optimizer.from_params(parameters, params.pop("optimizer"))

        if lr_scheduler_params:
            scheduler = LearningRateScheduler.from_params(
                optimizer, lr_scheduler_params)
        else:
            scheduler = None
        params.assert_empty(cls.__name__)
        kwargs = {}
        kwargs['model'] = model
        kwargs['optimizer'] = optimizer
        kwargs['iterator'] = iterator
        kwargs['train_dataset'] = train_data
        kwargs['validation_dataset'] = validation_data
        kwargs['segmenter'] = segmenter
        kwargs['patience'] = patience
        kwargs['validation_metric'] = validation_metric
        kwargs['num_epochs'] = num_epochs
        kwargs['base_dir'] = base_dir
        kwargs['cuda_device'] = cuda_device
        kwargs['grad_norm'] = grad_norm
        kwargs['grad_clipping'] = grad_clipping
        kwargs['learning_rate_scheduler'] = scheduler
        kwargs['num_serialized_models_to_keep'] = num_serialized_models_to_keep
        return kwargs

    @classmethod
    def from_params(cls, *args, **kwargs) -> 'Trainer':
        new_args = cls.get_args(*args, **kwargs)
        return Trainer(**new_args)


import unittest

import numpy as np
from numpy.testing import assert_allclose
from multistate_kernel.util import MultiStateData

from snad import transform_bands_msd, BRI_to_gri

class BRI2griTestCase(unittest.TestCase):
    def test_msd(self):
        x = np.array([0.])
        err = np.array([0.])
        magnB = 20
        magnR = 20
        magnI = 20
        items = (('B', np.rec.array((x, np.array([10 ** (-0.4 * magnB)]), err),
                                    dtype=[('x', float), ('y', float), ('err', float)])),
                 ('R', np.rec.array((x, np.array([10 ** (-0.4 * magnR)]), err),
                                    dtype=[('x', float), ('y', float), ('err', float)])),
                 ('I', np.rec.array((x, np.array([10 ** (-0.4 * magnI)]), err),
                                    dtype=[('x', float), ('y', float), ('err', float)])))
        msd = MultiStateData.from_state_data(items)
        new_msd = transform_bands_msd(msd, BRI_to_gri)
        magn = -2.5 * np.log10((new_msd.arrays.y * new_msd.norm))
        assert_allclose(magn, [19.84211267, 20.06126321, 20.32025792])


def min_max_sum(n:int) -> int:
    min_dig = 9
    max_dig = 0
    while n:
        num =  n % 10
        if num < min_dig:
            min_dig = num
        if num > max_dig:
            max_dig = num
        n //= 10
    return(min_dig + max_dig)

def is_div_a(n:int, a:int) -> bool:
    x = min_max_sum(n)
    return(x % a == 0)


# Jason Sanders & Bradley White
# CSCI 466: Networks
# Battleship HTTP: Client
# September 19, 2016
#
# This python module sends fire messages as POST requests to play Battleship over HTTP
# The program takes four arguments and should be invoked every time a fire message needs to be sent:
# server IP address, server port, x_coordinate, y_coordinate

import argparse
import urllib.parse
import http.client
import Board

# Parse command line arguments for server IP, port and fire coordinates
def parseArgs():
    # Start a new argument parser
    parser = argparse.ArgumentParser()

    # Add the four arguments
    parser.add_argument("ip")
    parser.add_argument("port", type=int)
    parser.add_argument("x_coordinate", type=int)
    parser.add_argument("y_coordinate", type=int)
    args = parser.parse_args()

    # Save the arguments into new variables
    server_address = args.ip
    port = args.port
    coordinates = (args.x_coordinate, args.y_coordinate)
    return (server_address, port, coordinates)

# Opens connection with the server and sends a fire message as a POST request
def openConnection(arguments):
    # Connect to the server at the IP and the port specified
    server_address = (arguments[0], arguments[1])
    connection = http.client.HTTPConnection(arguments[0], arguments[1])
    print('Connecting to IP: {} on port: {}'.format(*server_address))

    # Parse the coordinates into a proper POST request
    params = urllib.parse.urlencode({'x': arguments[2][0], 'y': arguments[2][1]})
    # Create the header
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    # Send the POST request with parameters and header to the server
    connection.request("POST", "", params, headers)
    # Wait for the response
    r2 = connection.getresponse()
    # Print the status code and message or type of error to terminal
    print(r2.status, r2.reason)
    # Close the connection
    connection.close()
    # If the fire message was legitimate, updated the opponent_board with the results
    if r2.status is 200:
        coord = (arguments[2][0], arguments[2][1])
        # Split the message into a list
        msg2 = r2.reason.split('=')
        # Send the coordinates and a 1 or 0 for hit or miss to Board for updates
        Board.update_eboard(coord, msg2[1])

# Invokes the other functions
def main():
    # Pass terminal arguments for opening the connection
    arguments = parseArgs()
    openConnection(arguments)

# Start!
main()


from keras import backend as K

def earth_movers_distance(y_true, y_pred):
    cdf_true = K.cumsum(y_true, axis=-1)
    cdf_pred = K.cumsum(y_pred, axis=-1)
    emd = K.sqrt(K.mean(K.square(cdf_true - cdf_pred), axis=-1))
    return K.mean(emd)

def age_mae(y_true, y_pred):
    true_age = K.sum(y_true * K.arange(0, 100, dtype="float32"), axis=-1)
    pred_age = K.sum(y_pred * K.arange(0, 100, dtype="float32"), axis=-1)
    mae = K.mean(K.abs(true_age - pred_age))
    return mae


desconto_base = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
previsao_pedidos = [400, 100, 3600, 4100, 4900, 7600, 9500]
previsao_base = [1000, 2000, 3000, 4000, 5000, 6000, 7000]
dias_semana = [1, 2, 3, 4, 5, 6, 7]

desconto_diario = []
for dia in dias_semana:
    if previsao_pedidos[dia-1] > previsao_base[dia-1]:
        desconto_final = desconto_base[dia-1] + ((previsao_pedidos[dia-1] - previsao_base[dia-1])/100)/100
        desconto_diario.append(desconto_final)
    else:
        desconto_final = desconto_base[dia-1]
        desconto_diario.append(desconto_final)

print("O desconto diário é: ")
print(desconto_diario)


import sqlite3
from typing import List
from audio_converter import AudioFile

class Database:
    def __init__(self, db_path: str = "audio_conversions.db"):
        self.db_path = db_path
        self.connection = sqlite3.connect(self.db_path)
        self.cursor = self.connection.cursor()
        self.create_table()

    def create_table(self):
        self.cursor.execute("""
            CREATE TABLE IF NOT EXISTS conversions (
                file_path TEXT,
                format TEXT,
                text TEXT
            )
        """)

    def save_conversion(self, audio_file: AudioFile):
        self.cursor.execute("""
            INSERT INTO conversions (file_path, format, text) VALUES (?, ?, ?)
        """, (audio_file.file_path, audio_file.format, audio_file.text))
        self.connection.commit()

    def get_previous_conversions(self) -> List[AudioFile]:
        self.cursor.execute("""
            SELECT * FROM conversions
        """)
        rows = self.cursor.fetchall()
        audio_files = [AudioFile(row[0], row[1], row[2]) for row in rows]
        return audio_files

    def close(self):
        self.connection.close()


# Description: Program that computes the volume of the sphere
# Author: John Royce Punay
# Date created: March 3, 2018 9:30PM
# 4(3.14)(r3) / 3 formula for getting the volume of sphere

def get_volume_sphere(radius):
    pi_value = 3.14
    sphere_volume = ((4 * pi_value) * (int(radius) ** 3)) / 3
    formatted_value = format(sphere_volume, '.5f')
    print(formatted_value)

def main():
    print("<----- Calculate the volume of sphere ---->")
    while True:
        try:
            radius_value = float(input("Enter radius value: "))
            get_volume_sphere(radius_value)
            break
        except:
            ValueError
            print('Invalid radius values please enter number!')
            continue
main()


"""Contains the `SphericalHarmonic` class."""
import numpy as np
import numpy.typing as npt
import pydantic
import typing_extensions

import pyssht as ssht

import sleplet._string_methods
import sleplet._validation
import sleplet.harmonic_methods
from sleplet.functions.flm import Flm

@pydantic.dataclasses.dataclass(config=sleplet._validation.validation, kw_only=True)
class SphericalHarmonic(Flm):
    """Create spherical harmonic functions."""

    ell: int = 0
    r"""Degree \(\ell \geq 0\)."""
    m: int = 0
    r"""Order \(\leq |\ell|\)"""

    def _create_coefficients(
        self: typing_extensions.Self,
    ) -> npt.NDArray[np.complex_ | np.float_]:
        ind = ssht.elm2ind(self.ell, self.m)
        return sleplet.harmonic_methods._create_spherical_harmonic(self.L, ind)

    def _create_name(self: typing_extensions.Self) -> str:
        return (
            f"{sleplet._string_methods._convert_camel_case_to_snake_case(self.__class__.__name__)}"
            f"{sleplet._string_methods.filename_args(self.ell, 'l')}"
            f"{sleplet._string_methods.filename_args(self.m, 'm')}"
        )

    def _set_reality(self: typing_extensions.Self) -> bool:
        return False

    def _set_spin(self: typing_extensions.Self) -> int:
        return 0

    def _setup_args(self: typing_extensions.Self) -> None:
        if isinstance(self.extra_args, list):
            num_args = 2
            if len(self.extra_args) != num_args:
                msg = f"The number of extra arguments should be {num_args}"
                raise ValueError(msg)
            self.ell, self.m = self.extra_args

    @pydantic.field_validator("ell")
    def _check_ell(
        cls,  # noqa: ANN101
        v: int,
        info: pydantic.ValidationInfo,
    ) -> int:
        if not isinstance(v, int):
            msg = "ell should be an integer"
            raise TypeError(msg)
        if v < 0:
            msg = "ell should be positive"
            raise ValueError(msg)
        if v >= info.data["L"]:
            msg = "ell should be less than or equal to L"
            raise ValueError(msg)
        return v

    @pydantic.field_validator("m")
    def _check_m(
        cls,  # noqa: ANN101
        v: int,
        info: pydantic.ValidationInfo,
    ) -> int:
        if not isinstance(v, int):
            msg = "m should be an integer"
            raise TypeError(msg)
        if abs(v) > info.data["ell"]:
            msg = "the magnitude of m should be less than ell"
            raise ValueError(msg)
        return v


"""
Fecha: 23 Noviembre-2022
Autor: David Santiago Zárate Diaz
Parcial
"""

def multiplo(a, b, m):
  s:int = 0
  if a < 0 or b < 0:
    print("Ingrese valores positivos")
  else:
    for i in range(a, b):
        if i%m == 0:
            s = s + i
    return s


from celery.result import AsyncResult
from sqlalchemy.orm import Session

from danswer.background.celery.celery import sync_document_set_task
from danswer.background.utils import interval_run_job
from danswer.db.document_set import (
    fetch_document_sets,
)
from danswer.db.engine import get_sqlalchemy_engine
from danswer.utils.logger import setup_logger

logger = setup_logger()

_ExistingTaskCache: dict[int, AsyncResult] = {}

def _document_sync_loop() -> None:
    # cleanup tasks
    existing_tasks = list(_ExistingTaskCache.items())
    for document_set_id, task in existing_tasks:
        if task.ready():
            logger.info(
                f"Document set '{document_set_id}' is complete with status "
                f"{task.status}. Cleaning up."
            )
            del _ExistingTaskCache[document_set_id]

    # kick off new tasks
    with Session(get_sqlalchemy_engine()) as db_session:
        # check if any document sets are not synced
        document_set_info = fetch_document_sets(db_session=db_session)
        for document_set, _ in document_set_info:
            if not document_set.is_up_to_date:
                if document_set.id in _ExistingTaskCache:
                    logger.info(
                        f"Document set '{document_set.id}' is already syncing. Skipping."
                    )
                    continue

                logger.info(
                    f"Document set {document_set.id} is not synced. Syncing now!"
                )
                task = sync_document_set_task.apply_async(
                    kwargs=dict(document_set_id=document_set.id),
                )
                _ExistingTaskCache[document_set.id] = task

if __name__ == "__main__":
    interval_run_job(
        job=_document_sync_loop, delay=5, emit_job_start_log=False
    )  # run every 5 seconds


from functools import lru_cache
from glob import glob
from pathlib import Path
from typing import Dict, List, Text, Tuple

import aep.tools.libs.data

def get_bundles(args) -> List[Text]:
    bundle_list = []
    for bundle_type in ("incident", "threatactors"):
        for bundle in glob(f"{args.data_dir}/{bundle_type}/*"):
            bundle_list.append(Path(bundle_type) / Path(bundle).name)

    return sorted(bundle_list)

@lru_cache()
def read_promise_description_file(filename: Path) -> List:
    return aep.tools.libs.data.read_promise_description_file(open(Path(filename)))

@lru_cache()
def read_technique_promises(
    tech_promises_file: Path, promise_descriptions_file: Path, conditions_file: Path
) -> Tuple[Dict, Dict, bool]:

    return aep.tools.libs.data.read_technique_promises(
        tech_promises_file, promise_descriptions_file, conditions_file
    )


from setup_matplotlib import *
import matplotlib.cm as cm
import numpy as N
from pylab import *
import matplotlib.colors as colors
import matplotlib.cm as cm
from mpl_toolkits.axes_grid1 import make_axes_locatable

directory="."
#prefix='testbed1046'
#prefix='test'
prefix=sys.argv[1]

# Create the plot
width=300
fig = plt.figure(figsize=(cm2inch(width), 1.5*cm2inch(width)))

# this should be changed for making a panel of multiple figures
f, axarr = plt.subplots(2,1)

filename = prefix+'_tod.dat'
data = N.loadtxt(filename)

data = data[0:300000,:]

axarr[0].plot(data[:,0], data[:,2])
axarr[0].plot(data[:,0], data[:,2]-data[:,1],color='red')
axarr[0].plot([0,max(data[:,0])], [0,0], linestyle='--')
axarr[0].xaxis.set_visible(False)
axarr[0].set_xlim(0,max(data[:,0]))
axarr[0].set_ylim(0.9*min(data[:,2]),1.1*max(data[:,2]))
axarr[0].set_ylabel('TOD [spec unit]')

axarr[1].plot(data[:,0], data[:,1])
axarr[1].plot([0,max(data[:,0])], [0,0], linestyle='--')
axarr[1].set_xlim([0,max(data[:,0])])
axarr[1].set_xlabel('Time [sec]')
axarr[1].set_ylabel('TOD [spec unit]')

#axarr[0].ylim([-ymax, ymax]);
#axarr[0].xlim([0, xmax]);

plt.savefig(prefix+"_tod.pdf", bbox_inches='tight', bbox_extra_artists=[],pad_inches=0.03)


# pylint: disable=line-too-long
"""Base class for subscribable objects"""
from abc import ABC, abstractmethod
import logging

_LOGGER = logging.getLogger(__name__)

class SubscriberBase(ABC):
    """Base class for subscribable objects"""

    def __init__(self):
        self._dirty_list: list[str] = []
        self._callbacks = []
        self._dirty = False

    @abstractmethod
    def debug_string(self) -> str:
        """Returns string to be logged in messages"""

    def attr_updater(self, data_set, attr: str, property_name: str = None) -> bool:
        """Updates an attribue"""
        if attr in data_set:
            attr_val = data_set[attr]
            if property_name is None:
                property_name = attr
            if getattr(self, property_name) != attr_val:
                setattr(self, property_name, attr_val)
                self._dirty = True
                if property_name not in self._dirty_list:
                    self._dirty_list.append(property_name)
                    _LOGGER.debug(
                        "update_attr: [%s] attr [%s] value [%s]", self.debug_string(), property_name, attr_val
                    )
                return True
        return False

    def register_on_update_callback(self, callbackfunc, match=None):
        """Register a callback for changes"""
        self._callbacks.append({"func": callbackfunc, "match": match})

    def execute_on_update_callbacks(self):
        """Execute callbacks"""
        if self._dirty:
            for callback in self._callbacks:
                callbackfunc = callback["func"]
                match = callback["match"]
                matches = False
                if match is None:
                    matches = True
                else:
                    for match_item in match:
                        if match_item in self._dirty_list:
                            matches = True
                            break
                try:
                    if matches:
                        callbackfunc()
                except Exception:  # pylint: disable=broad-exception-caught
                    # Log and eat this exception so we can process other callbacks
                    _LOGGER.exception("executeOnUpdateCallback - [%s] - failed", self.debug_string())
        self._dirty = False
        self._dirty_list = []


import pandas as pd
import requests
import json
from datetime import datetime
import os

def get_job_posted(headers):
    """

    """

    url = "https://jsearch.p.rapidapi.com/search"

    querystring = {"query":"Data Engineer and Data Analyst in UK and USA","page":"1","num_pages":"1"}

    response = requests.get(url, headers=headers, params=querystring)

    response_data = response.json()

    with open ('raw/raw_job_posted_data.json', 'w') as jobfile:
        json.dump(response_data, jobfile)
        print('data successfully written to a raw database file')

def transformed_raw_data():

    # the raw csv is read by a json load function
    with open('raw/raw_job_posted_data.json', 'r') as job_file:
        reader = json.load(job_file)
        data = reader['data']

    # reading the data to pandas dataframe, and the selected columns required are listed in the column_name
    column_names = ['employer_website', 'job_id', 'job_employment_type', 'job_title', 'job_apply_link', 'job_description',
                    'job_city', 'job_country', 'job_posted_at_timestamp','employer_company_type']

    job_df = pd.DataFrame(data, columns=column_names)

    #converting the timestamp vale to date
    job_date=pd.to_datetime(job_df['job_posted_at_timestamp'], unit='s')
    job_df['job_posted_at_timestamp'] = job_date
    job_df = job_df.rename(columns={"job_employment_type": "employment_type", "job_city": "City",
                            "job_country": "Country", "job_posted_at_timestamp": "job_posted_date", "employer_company_type": "Sector"})

    # write the transformed data to a csv file
    filepath = 'Transformed/cleaned_job_posted.csv'
    job_df.to_csv(filepath, index=False)
    print('clean date succesffuly write to the transformed staging area')

def load(engine):
    # read the file from the filepath to pandas dataframe
    filepath = 'Transformed/cleaned_job_posted.csv'
    df = pd.read_csv(filepath)
    df.to_sql('talent', con=engine)


from steps.predatas import get_data
from steps.homog import compute_homography
from steps.intrinsics import get_camera_intrinsics
from steps.extrinsics import get_camera_extrinsics
from steps.distortion import estimate_lens_distortion

def calibrate():
    data = get_data()

    # print(data['sensed'])
    homographies = compute_homography(data)
    print("homographies shape:")
    print(homographies.shape)

    intrinsics = get_camera_intrinsics(homographies)
    print("intrinsics:")
    print(intrinsics)

    extrinsics = get_camera_extrinsics(intrinsics, homographies)
    print("extrinsics shape:")
    print(extrinsics.shape)

    distortion = estimate_lens_distortion(
        intrinsics,
        extrinsics,
        data['real'][0],
        data['sensed']
    )
    print("distortion:")
    print(distortion)

    return

calibrate()


##########################################################
################### simulating NUV-DoA ###################
##########################################################

import torch
import math
import time

from NUV import NUV_DoA

from simulations import utils
from simulations import config
from data.Data_generation import DataGenerator

#### initialization ####
args = config.general_settings()
args.use_cuda = False
# GPU or CPU
if args.use_cuda:
   if torch.cuda.is_available():
      device = torch.device('cuda')
      print("Using GPU")
   else:
      raise Exception("No GPU found, please set args.use_cuda = False")
else:
    device = torch.device('cpu')
    print("Using CPU")
# number of samples
args.sample = 5
samples_run = args.sample

args.resol = 0.05 # resolution of spatial filter
args.q_init = 0.1 # initial guess of q
centers = torch.linspace(args.B1, args.B2, int((args.B2 - args.B1) / args.resol + 1)).to(device) # centers of all windows
center_num = len(centers) # number of centers
half_window_size = args.resol * 50 # each window covers the interval [center - half_window_size, center + half_window_size]
args.m_SF = int((half_window_size * 2)/args.resol)+1  # number of steering vectors of each SF window, here m_SF=101

#### Generate data ####
generator = DataGenerator(args)
# x_dire, x_true, y_train = generator.generate_experiment_data()
# torch.save([x_dire, x_true, y_train], 'data/DoA_m=360_k=3_sample=5.pt')
[x_dire, x_true, y_train] = torch.load('data/DoA_m=360_k=3_sample=5.pt', map_location=device)

y_mean = y_train.mean(dim=1) # generate y_mean by averaging l snapshots for each sample

A, A_H = generator.ULA_refine(centers, half_window_size) # generate dictionary matrices for each little window
A = A.to(device)
A_H = A_H.to(device)

# #### estimation ####
import gc
x_pred = torch.zeros(samples_run, center_num, dtype=torch.cfloat, device=device)
theta_tuning = torch.zeros(samples_run, args.k, dtype=torch.float, device=device)
MSE = torch.zeros(samples_run, dtype=torch.float, device=device)

for i in range(samples_run): #r=r_2 * 6000 when k=1
  print(i)
  print('true doa is {}'.format(x_dire[i]))
  r_tuning =  [20000] # [20000, 15000, 10000, 300, 100, 90]
  MSE_tuning = torch.zeros(len(r_tuning), dtype=torch.float, device=device)
  for r in range(len(r_tuning)):
    print('r_tuning = {}'.format(r_tuning[r]))
    # NUV-SSR + SF
    x_pred[i], iterations = NUV_DoA(args, center_num, A, A_H, y_mean[i, :, 0], r_tuning[r])
    # peak finding and convert to DoA
    peak_indices = utils.peak_finding(x_pred[i], args.k)
    theta_tuning[i] = utils.convert_to_doa(peak_indices, centers)
    print('predict doa = {}'.format(theta_tuning[i]))
    # permuted MSE
    MSE_tuning[r] = utils.permuted_mse(theta_tuning[i], x_dire[i])
    print('MSE_tuning = {}'.format(MSE_tuning[r]))
    gc.collect()
    if args.use_cuda:
        torch.cuda.empty_cache()

  MSE[i] = torch.min(MSE_tuning)
  print('MSE[i] = {}'.format(MSE[i]))
  print('-----------------------------------------')
  gc.collect()
  if args.use_cuda:
    torch.cuda.empty_cache()

mean_MSE = torch.mean(MSE)
MSE_dB = 10 * (torch.log10(mean_MSE))
print('averaged MSE in dB = {}'.format(MSE_dB))
MSE_linear = torch.sqrt(mean_MSE)
print('averaged RMSE in linear = {}'.format(MSE_linear))
print('--------------------------------------------')
SNR = 10*math.log10((args.x_var + args.mean_c) / args.r2)
print('SNR = {}'.format(SNR))

#### plotting ####
# import matplotlib.pyplot as plt
# import numpy as np
# p = 0

# fig,ax = plt.subplots(figsize=(6, 3))

#   # x_c = xx_pred[p]
#   # axs = axs.ravel()
# x_c = x_pred[p].cpu()
# y_c = np.linspace(args.B1, args.B2, len(x_c), endpoint=False)
#   # y_c = np.linspace(-0.5, 0.5, len(x_c), endpoint=False)
# ax.plot(y_c, x_c, marker = '*')
# # plt.plot(0, x_c[50], "o")
# # ax.set_title('true doa = {}'.format(x_dire[p] ))
#   # argrelextrema(np.array(x_c), np.greater)
# print('true doa = {}'.format(x_dire[p]))
# print('predicted doa = {}'.format(theta[p]))
# print('σ = 3r2 = 30')


#  220807 17298 오큰수

import sys

n = int(sys.stdin.readline())
numbers = list(map(int, sys.stdin.readline().split()))

# 오큰수를 담을 리스트와 빈 스택을 만들어준다.
nge = [-1] * n
stack = []

for i in range(n):
    # 다음에 오는 수가 스택 최상단에 있는 수보다 크면 스택을 비워주고 해당 수는 스택에 있던 모든 수의 오큰수가 된다.
    while stack and numbers[stack[-1]] < numbers[i]:
        nge[stack.pop()] = numbers[i]
    # 스택이 비어있거나 스택 최상단에 있는 수(인덱스로 접근)보다 작으면 스택에 넣어준다(인덱스를 넣어줌)
    stack.append(i)

print(*nge)

'''
스택에 수 자체를 안 넣고(stack.append(nge[i])) 인덱스를 넣은 이유(stack.append(i))
- 처리 속도도 느려지고 스택에 있던 수를 빼서 오큰수 리스트에 넣어주는 게 복잡해서 인덱스로 접근
'''


#coding: utf-8
from django.core.management.base import BaseCommand
from django.db.models import get_models

class Command(BaseCommand):
    help = u"""
      ticket:9 Command that prints all project models and the
      count of objects in every model
    """
    args = ''
    requires_model_validation = True

    def handle(self, *args, **options):
        for m in get_models():
            data = "%s.%s:\t%i\n" % (m._meta.app_label,
                                                m._meta.module_name.capitalize(),
                                                m.objects.count())
            self.stdout.write(data)
            self.stderr.write("error: " + data)


from vedis import Vedis
import config

def get_current_price(user_id_price):
    with Vedis(config.db_file) as db:
        try:
            return db[user_id_price].decode()
        except KeyError:
            return config.States.User_Cost.value

def set_user_price(user_id_price, value):
    with Vedis(config.db_file) as db:
        try:
            db[user_id_price] = value
            return True
        except:
            # тут желательно как-то обработать ситуацию
            return False


# ch10/example6.py

from math import sqrt
import asyncio
from concurrent.futures import ProcessPoolExecutor
from timeit import default_timer as timer

#async def is_prime(x):
def is_prime(x):
    print('Processing %i...' % x)

    if x < 2:
        print('%i is not a prime number.' % x)

    elif x == 2:
        print('%i is a prime number.' % x)

    elif x % 2 == 0:
        print('%i is not a prime number.' % x)

    else:
        limit = int(sqrt(x)) + 1
        for i in range(3, limit, 2):
            if x % i == 0:
                print('%i is not a prime number.' % x)
                return

        print('%i is a prime number.' % x)

async def main():

    task1 = loop.run_in_executor(executor, is_prime, 9637529763296797)
    task2 = loop.run_in_executor(executor, is_prime, 427920331)
    task3 = loop.run_in_executor(executor, is_prime, 157)

    await asyncio.gather(*[task1, task2, task3])

if __name__ == '__main__':
    try:
        start = timer()

        executor = ProcessPoolExecutor(max_workers=3)
        loop = asyncio.get_event_loop()
        loop.run_until_complete(main())

        print('Took %.2f seconds.' % (timer() - start))

    except Exception as e:
        print('There was a problem:')
        print(str(e))

    finally:
        loop.close()