import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

class PlotMarker():
    def __init__(self):
        self.plot_width = 50
        self.plot_forward = 20
        self.plot_backward = 40
        self.map_yaw = math.pi * 43/32

        self.map_rot = np.array([[math.cos(self.map_yaw), math.sin(self.map_yaw)],
                        [-math.sin(self.map_yaw), math.cos(self.map_yaw)]])
        self.map_rot_wh = np.array([[math.cos(self.map_yaw), 0],
                                    [0, math.sin(self.map_yaw)]])

        self.ego_vec_length = 3
        self.robot_length = 3.5
        self.robot_width = 1.5

    def plot_status(self,
                    ego_pose = None,
                    object_pose = None,
                    left_bound = None,
                    right_bound = None,
                    path=None,
                    path_index_left=None,
                    path_index_next_left=None,
                    path_index_right= None,
                    path_index_next_right=None,
                    rotation = False,
                    predicted_goal_pose=None,
                    predicted_trajectory=None,
                    curve_plot=None,
                    curve_forward_point=None,
                    curve_backward_point=None,
                    vis_point=None
                    ):

        if ego_pose is not None:
            plt.cla()

            ego_pose_origin = ego_pose
            if rotation:
                ego_pose = np.array([0, 0])

            ## Plot ego pose
            plot_xmin = ego_pose[0] - self.plot_width / 2.0
            plot_xmax = ego_pose[0] + self.plot_width / 2.0
            plot_ymin = ego_pose[1] - self.plot_backward/ 2.0
            plot_ymax = ego_pose[1] + self.plot_forward / 2.0
            plt.plot(ego_pose[0], ego_pose[1], "xr")

            yaw = ego_pose_origin[2]
            ego_rot = np.array([[math.cos(yaw), math.sin(yaw)],
                            [-math.sin(yaw), math.cos(yaw)]])

            outline = np.array([
                               [-self.robot_length / 2, self.robot_length / 2,
                                (self.robot_length / 2), -self.robot_length / 2,
                                -self.robot_length / 2],
                                [self.robot_width / 2, self.robot_width / 2,
                                - self.robot_width / 2, -self.robot_width / 2,
                                self.robot_width / 2]
                                ])
            outline = (outline.T.dot(ego_rot)).T

            outline[0, :] += ego_pose[0]
            outline[1, :] += ego_pose[1]
            plt.plot(np.array(outline[0, :]).flatten(),
                    np.array(outline[1, :]).flatten(), "-k")

            ## Plot allow
            ego_vec = np.array([np.cos(yaw), np.sin(yaw)]) * self.ego_vec_length
            ego_vec = ego_pose[0:2] + ego_vec
            trajec = np.array([[ego_pose[0], ego_pose[1]],
                              [ego_vec[0], ego_vec[1]]])
            plt.plot(trajec[:,0], trajec[:,1], "red")

            ## Plot object pose
            if object_pose is not None:
                for ob in object_pose:
                    object_xy = ob[0:2]
                    object_wh = ob[2:4]
                    if rotation:
                        object_xy = object_xy[0:2] - ego_pose_origin[0:2]
                        object_xy = object_xy[0:2] @ self.map_rot
                        object_wh = object_wh[0:2] @ self.map_rot_wh

                    object_x_min = object_xy[0] - object_wh[0] / 2.0
                    object_y_min = object_xy[1] - object_wh[1] / 2.0
                    object_x_max = object_xy[0] + object_wh[0] / 2.0
                    object_y_max = object_xy[1] + object_wh[1] / 2.0
                    ob_box = np.array([[object_x_min, object_y_min],
                                       [object_x_min, object_y_max],
                                       [object_x_max, object_y_max],
                                       [object_x_max, object_y_min],
                                       [object_x_min, object_y_min]])

                    plt.plot(ob_box[:,0], ob_box [:,1], "blue")

            if path is not None:
                self.plot_path(path)

            ## Plot bound
            if (left_bound is not None) and (right_bound is not None):
                if rotation:
                    left_bound = left_bound[:,0:2] - ego_pose_origin[0:2]
                    left_bound = left_bound[:,0:2] @ self.map_rot

                    right_bound = right_bound[:,0:2] - ego_pose_origin[0:2]
                    right_bound = right_bound[:,0:2] @ self.map_rot

                plt.plot(left_bound[:, 0], left_bound[:, 1], color="black")
                plt.plot(right_bound[:,0], right_bound[:,1], color="black")

                if (path_index_left is not None) and (path_index_right is not None):
                    if path_index_next_left is None:
                        path_index_next_left = path_index_left + 1
                    if path_index_next_left is None:
                        path_index_next_left = path_index_right + 1

                    plt.plot(left_bound[path_index_left:path_index_next_left + 1 , 0], left_bound[path_index_left:path_index_next_left + 1, 1], color="green")
                    plt.plot(right_bound[path_index_right:path_index_next_right + 1, 0], right_bound[path_index_right:path_index_next_right + 1, 1], color="green")

            ## Plot goal pose
            if predicted_goal_pose is not None:
                self.plot_point(predicted_goal_pose)

            ## Plot trajectory
            if predicted_trajectory is not None:
                self.plot_traj(predicted_trajectory)

            ## Plot curve path
            if curve_plot is not None:
                self.plot_red_line(curve_plot)

            if curve_forward_point is not None:
                self.plot_point_delta(curve_forward_point)

            if curve_backward_point is not None:
                self.plot_point_delta(curve_backward_point)

            if vis_point is not None:
                self.vis_point(vis_point)

            plt.xlim(plot_xmin, plot_xmax)
            plt.ylim(plot_ymin, plot_ymax)
            plt.pause(0.01)
            #plt.show()

        else:
            return

    def plot_red_line(self, line):
        plt.plot(line[:, 0], line[:, 1], color="Magenta")

    def plot_path(self, path):
        plt.plot(path[:, 0], path[:, 1], color="yellow", marker=".")
        #plt.plot(path[:, 0], path[:, 1], color="yellow")

    def plot_traj(self, traj):
        plt.plot(traj[:, 0], traj[:, 1], color="green", marker="^")

    def plot_point(self, point):
        plt.plot(point[0], point[1], marker="x")

    def plot_point_delta(self, point):
        plt.plot(point[0], point[1], marker="^",markersize=10)

    def vis_point(selfm, point):
        if len(point.shape) > 1:
            plt.scatter(point[:,0], point[:, 1], s=20, marker="D")
        else:
            plt.plot(point[0], point[1], marker="8",markersize=8)


"""
    PROJECT    : Outer billiards visualization project.
    FILE       : render.py
    AUTHOR     : Andrey Dmitrenko.
    PURPOSE    : Render abstract entity implementation module.
    LAST UPDATE: 19.05.2021.
"""

import pygame

'''
    Coordinate system representation class.

    Fields:
        cx, cy (float): Relative center position.
        w, h (float): The field sizes.
        sw, sh (int): Actual screen sizes in pixels.

    Methods:
        __init__(cx, cy, w, h, sw, sh): The constructor.
        to_screen(xy): Convert local point coordinates to pixel coordinates.
'''
class cor_sys:
    '''
        The coordinate system class constructor.
        Arguments:
            cx, cy (float): Relative center position.
            w, h (float): The field sizes.
            sw, sh (int): Actual screen sizes in pixels.
    '''
    def __init__(self, cx, cy, w, h, sw, sh):
        self.cx = cx
        self.cy = cy

        self.w = w
        self.h = h

        self.sw = sw
        self.sh = sh
    # End of '__init__' function

    '''
        Convert local point coordinates to pixel coordinates.
        Arguments:
            xy (tuple): The point coordinates.
        Returns:
            (tuple) Two integers representing the coordinates of the resulting pixel.
    '''
    def to_screen(self, xy):
        return (int(((float(xy[0]) + self.cx) + self.w / 2) / self.w * self.sw),
                int(((float(-(xy[1] + self.cy))) + self.h / 2) / self.h * self.sh))
    # End of 'to_screen' function
# End of 'cor_sys' class

'''
    Render entity representation class.

    Fields:
        screen (Surface): The screen to render to.
        cs (cor_sys): The coordinate system to work with.
        draw(): The drawing callback.

    Methods:
        __init__(sw, sh, draw): The constructor.
        run(rnd): Run the main program loop.

        draw_segment(p1, p2, color): Draw the segment.
'''
class render:
    '''
         The render class constructor.
         Arguments:
            sw, sh (int): Screen sizes in pixels.
            draw(rnd): The drawing callback.
    '''
    def __init__(self, cs, draw):
        pygame.init()

        pygame.display.set_caption("Outer billiard by AD1")
        icon = pygame.image.load("pent.png")
        pygame.display.set_icon(icon)

        self.screen = pygame.display.set_mode([cs.sw, cs.sh])
        self.cs = cs
        self.draw = draw
    # End of '__init__' function

    '''
        Run the main program loop.
        Arguments: None.
        Returns: None.
    '''
    def run(self):
        running = True

        while running:
            # Process the quit event
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False

            # Draw stuff
            self.screen.fill((255, 255, 255))
            self.draw(self)

            # Flip the display
            pygame.display.flip()
        pygame.quit()
    # End of 'run' function

    '''
        Draw the line segment.
        Arguments:
            p1, p2 (point): The edges of the segment.
            color (tuple): 3-component RGB color to draw with.
        Returns: None.
    '''
    def draw_segment(self, p1, p2, color=(0, 0, 0)):
        pygame.draw.line(self.screen, color, self.cs.to_screen(tuple(p1)), self.cs.to_screen(tuple(p2)))
    # End of 'draw_segment' function
# End of 'render' class

# END OF 'render.py' FILE


'''
5428. Shuffle the Array
User Accepted:6794
User Tried:6848
Total Accepted:6976
Total Submissions:7425
Difficulty:Easy
Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].

Return the array in the form [x1,y1,x2,y2,...,xn,yn].
'''

class Solution:
    def shuffle(self, nums: List[int], n: int) -> List[int]:
        res = []
        for i in range(n):
            res.append(nums[i])
            res.append(nums[n + i])
        return res


##https://leetcode.com/problems/delete-node-in-a-bst/solution/
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
"""
use recursion to recursively delete the node

if root == None: return None
if key > root.val,
    then we need to delete node from right-subtree
    root.right = self.deleteNode(root.right, key)
if key < root.val,
    then we need to delete node from left-subtree
    root.left = self.deleteNode(root.left, key)

if key == root.val, then we found the node that need to be deleted:
    if root is a leaf: then root = None
    if root has right child, then we set root.val = successor(root), and call
        root.right = deleteNode(root.right, root.val)

    elif root only has left child, then root.val = predecessor(root), and call
        root.left = deleteNode(root.left, root.val)

"""
class Solution:
    def successor(self, root):
        root = root.right
        while root.left:
            root = root.left
        return root.val

    def predecessor(self, root):
        root = root.left
        while root.right:
            root = root.right
        return root.val

    def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
        if not root:
            return None
        if key > root.val:
            root.right = self.deleteNode(root.right, key)

        elif key < root.val:
            root.left = self.deleteNode(root.left, key)

        else:
            if not root.left and not root.right:
                root = None

            elif root.right:
                root.val = self.successor(root)
                root.right = self.deleteNode(root.right, root.val)

            else:
                root.val = self.predecessor(root)
                root.left = self.deleteNode(root.left, root.val)

        return root


# --*-- coding:utf8 --*--
from apps.common.api.basedata_api import Basedata_Unit_Api
from test.test_data import apartment

def test_basedata_unit_api():
    test_get_unit_list()
    test_get_unit_list_by_filter()

def test_get_unit_list():
    params = {
        "province": apartment['province'],
        "city": apartment['city'],
        "project": apartment['project'],
        "group": apartment['group'],
        "build": apartment['build'],
    }
    Basedata_Unit_Api().get_unit_list(**params)

def test_get_unit_list_by_filter():
    params = {
        "province": apartment['province'],
        "city": apartment['city'],
        "project": apartment['project'],
        "group": apartment['group'],
        "build": apartment['build'],
        "unit": apartment['unit'],
        "project_id": apartment['project_id'],
        "group_id": apartment['group_id'],
        "build_id": apartment['build_id'],
    }
    Basedata_Unit_Api().get_unit_list_by_filter(**params)


## QUESTÃO 8 ##
# Escreva um programa que leia uma data do usuário e calcule seu sucessor imediato.
# Por exemplo, se o usuário inserir valores que representem 2013-11-18, seu programa
# deve exibir uma mensagem indicando que o dia imediatamente após 2013-11-18 é
# 2013-11-19. Se o usuário inserir valores que representem 2013-11-30, o programa deve
# indicar que o dia seguinte é 2013-12-01. Se o usuário inserir valores que representem
# 2013-12-31 então o programa deve indicar que o dia seguinte é 2014-01-01. A data
# será inserida em formato numérico com três instruções de entrada separadas;
# uma para o ano, uma para o mês e uma para o dia. Certifique-se de que o seu programa
# funciona corretamente para anos bissextos.
##

##
# A sua resposta da questão deve ser desenvolvida dentro da função main()!!!
# Deve-se substituir o comado print existente pelo código da solução.
# Para a correta execução do programa, a estrutura atual deve ser mantida,
# substituindo apenas o comando print(questão...) existente.
##
def main():

    ano = int(input('Digite o ano: '))
    mes = int(input('Digite o mês: '))
    dia = int(input('Digite o dia: '))

    anoBissexto = ano % 4 == 0 and ano % 100 != 0 or ano % 400 == 0

    if (mes==1 or mes==3 or mes==5 or mes==7 or mes==8 or mes==10 or mes==12):
        if dia==31:
            if mes==12:
                mes = 1
                ano = ano + 1
            else:
                mes = mes + 1
            dia = 1
        elif (dia>31 or dia<=0):
            print('ENTRADA INVALIDA DIA')
        elif (mes>12 or mes<=0):
            print('ENTRADA INVALIDA MES')
        else:
            dia = dia + 1
    elif (mes==4 or mes==6 or mes==9 or mes==11):
        if dia==30:
            mes = mes + 1
            dia = 1
        elif (dia>30 or dia<=0):
            print('ENTRADA INVALIDA')
        elif (mes>12 or mes<=0):
            print('ENTRADA INVALIDA MES')
        else:
            dia = dia + 1

    elif mes==2:

        if anoBissexto:
            if dia<=0 or dia>29:
                print('ENTRADA INCORRETA')
            else:
                if dia==29:
                    dia = 1
                    mes = mes + 1
                else:
                    dia = dia + 1

        ## Se não for bissexto
        else:
            if dia<=0 or dia>28:
                print('ENTRADA INCORRETA')
            else:
                if dia==28:
                    dia = 1
                    mes = mes + 1
                else:
                    dia = dia + 1
    else:
        print('ENTRADA INCORRETA')

    print('Dia posterior: {}-{}-{}'.format(ano, mes, dia))

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
'''
$Id: show_position.py
$auth: Steve Torchinsky <satorchi@apc.in2p3.fr>
$created: Fri 18 Feb 2022 13:21:47 CET
$license: GPLv3 or later, see https://www.gnu.org/licenses/gpl-3.0.txt

          This is free software: you are free to change and
          redistribute it.  There is NO WARRANTY, to the extent
          permitted by law.

show the position of the platform
'''
import datetime as dt
from qubichk.platform import get_position

header = '%s   %s   %s' % ('time'.center(20),'az'.center(7),'el'.center(7))
print(header,end='\n')

while True:
    az,el,azwarn,elwarn = get_position()
    date_str = dt.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
    if type(az)==str:
        az_str = 'ERROR'.center(9)
    else:
        if azwarn:
            az_str = '%7.2f ?' % az
        else:
            az_str = '%7.2f  ' % az
    if type(el)==str:
        el_str = 'ERROR'.center(9)
    else:
        el_str = '%7.2f  ' % el

    line = '%s   %s   %s' % (date_str,az_str,el_str)
    print(line,end='\r')


import json
import re
import os
import time
import numpy as np
from random import shuffle, choice
from argparse import ArgumentParser
from gensim.models import KeyedVectors
import sys

class DataSet:
    def __init__(self, word2vec, training_features, training_label, testing_features, testing_label, batch_size=4):
        self.word2vec           = KeyedVectors.load(word2vec, mmap='r')
        self.embedding_dim      = self.word2vec['<BOS>'].shape[0]
        self.vocab_size         = len(self.word2vec.vocab)
        self.training_label_txt = {} # training label in words with video id as key
        self.training_label_vec = {} # training label in vector form with video id as key
        self.testing_label_txt  = {}
        self.training_ids       = [] # the ids of the videos for training
        self.testing_ids        = [] # the ids of the videos for testing
        self.batch_size         = batch_size
        self.max_length         = None
        self.batch_index        = 0
        self.training_num       = None
        self.max_batch_index    = None
        self.sentence_limit     = 30 # default max length of a sentence

        with open(training_label, 'r') as f:
            data = json.loads(f.read())
        for movie in data:
            self.training_label_txt[movie['id']]  = []
            self.training_label_txt[movie['id']] += [('<BOS> '+re.sub(',', '', x[:-1].lower())+' <EOS>').strip().split() for x in movie['caption']]

        with open(testing_label, 'r') as f:
            data = json.loads(f.read())
        for movie in data:
            self.testing_label_txt[movie['id']]  = []
            self.testing_label_txt[movie['id']] += [('<BOS> '+re.sub(',', '', x[:-1].lower())+' <EOS>').strip().split() for x in movie['caption']]

        with open(training_features, 'r') as f:
            for line in f:
                self.training_ids.append(line.strip())

        with open(testing_features, 'r') as f:
            for line in f:
                self.testing_ids.append(line.strip())

        # create mappings
        self.word2id = {'<PAD>':0, '<BOS>':1, '<EOS>':2}
        self.id2word = {0:'<PAD>', 1:'<BOS>', 2:'<EOS>'}

        curID = 3
        for word in self.word2vec.vocab:
            if word not in self.word2id:
                self.word2id[word]  = curID
                self.id2word[curID] = word
                curID += 1

        # remove sentences with OOV words
        self.check_labels()

        self.training_num    = len(self.training_label_txt)
        self.max_batch_index = self.training_num // self.batch_size - 1

        print ('word model    -> {}'.format(word2vec))
        print ('vocab size    -> {}'.format(self.vocab_size))
        print ('max length    -> {}'.format(self.max_length))
        print ('training data -> {}'.format(self.training_num))
        print ('batch num     -> {}'.format(self.max_batch_index+1))

    def check_labels(self):
        self.max_length = 0
        for id, labels in self.training_label_txt.items():
            self.training_label_vec[id] = []
            ignore_sent = []
            for sentence in labels:
                sent_vec   = []
                legal_sent = True
                if len(sentence) > self.sentence_limit: legal_sent = False
                for word in sentence:
                    if not legal_sent:
                        ignore_sent.append(sentence)
                        break
                    try:
                        word_vec = self.word2vec[word]
                    except:
                        ignore_sent.append(sentence)
                        legal_sent = False
                        break
                    sent_vec.append(word_vec)
                if legal_sent:
                    self.training_label_vec[id].append(sent_vec)
                    self.max_length = max(self.max_length, len(sentence))
            for sent in ignore_sent:
                self.training_label_txt[id].remove(sent)

    def Word2ID(self, word):
        return self.word2id[word]

    def ID2Word(self, id):
        return self.id2word[id]

    def Word2Vec(self, word):
        if word == '<PAD>': return np.zeros(self.embedding_dim)
        return self.word2vec[word]

    def VocabSize(self):
        return self.vocab_size

    def MaxSeqLength(self):
        return self.max_length

    def EmbeddingDim(self):
        return self.embedding_dim

    def next_batch(self):
        if self.batch_index == 0:
            shuffle(self.training_ids)

        '''
        shape:
            frames : (batch size, number of frames, number of features)
            labels : (batch size, max seq length,   latent dimension)
            onehot : (batch size, max seq length,   vocab size)
        '''

        ids    = self.training_ids[self.batch_index*self.batch_size:(self.batch_index+1)*self.batch_size]

        videos = [ os.path.join('MLDS_hw2_1_data/training_data/feat', x+'.npy') for x in ids ]
        frames = [ np.load(v) for v in videos ]

        index  = [ choice(np.arange(len(self.training_label_txt[ID]))) for ID in ids ]
        labels = [ self.training_label_vec[l][index[idx]] for idx, l in enumerate(ids) ]
        labels = self.padSeqVec(labels)

        texts  = [ self.training_label_txt[l][index[idx]] for idx, l in enumerate(ids) ]
        onehot = self.toOnehot(texts)
        onehot = self.padSeqOnehot(onehot)

        self.batch_index += 1
        if self.batch_index > self.max_batch_index: self.batch_index = 0

        return frames, labels, onehot

    def padSeqVec(self, labels):
        for i in range(len(labels)):
            while len(labels[i]) < self.max_length:
                labels[i].append(self.Word2Vec('<PAD>'))
            labels[i] = np.array(labels[i])
        return np.array(labels)

    def toOnehot(self, texts):
        OnehotLabels = []
        for sentence in texts:
            OnehotSentence = []
            for word in sentence:
                OnehotWord = np.zeros(self.vocab_size)
                OnehotWord[self.Word2ID(word)] = 1
                OnehotSentence.append(OnehotWord)
            OnehotLabels.append(OnehotSentence)
        return OnehotLabels

    def IDs2VECs(self, ids):
        vecs = []
        for ID in ids:
            vecs.append(self.Word2Vec(self.ID2Word(ID)))
        return np.array(vecs)

    def padSeqOnehot(self, onehot):
        for i in range(len(onehot)):
            while len(onehot[i]) < self.max_length:
                OnehotPad = np.zeros(self.vocab_size)
                OnehotPad[self.Word2ID('<PAD>')] = 1
                onehot[i].append(OnehotPad)
            onehot[i] = np.array(onehot[i])
        return np.array(onehot)

    def test_batch(self):
        '''
        returns None when all of the data are tested
        '''
        ids    = self.testing_ids

        videos = [ os.path.join('MLDS_hw2_1_data/testing_data/feat', x+'.npy') for x in ids ]

        frames = [ np.load(v) for v in videos ]

        return frames, ids

def main(args):
    training_label    = args.file1
    testing_label     = args.file2
    training_features = args.file3
    testing_features  = args.file4
    word2vec          = args.model

    dataset = DataSet(word2vec, training_features, training_label, testing_features, testing_label)
    print (dataset.EmbeddingDim())

def parse():
    parser = ArgumentParser()
    parser.add_argument('--file1', '-f1',  default='MLDS_hw2_1_data/training_label.json', help='the file that contains texts')
    parser.add_argument('--file2', '-f2',  default='MLDS_hw2_1_data/testing_label.json', help='the file that contains texts')
    parser.add_argument('--file3', '-f3',  default='MLDS_hw2_1_data/training_data/id.txt', help='the file that contains training features')
    parser.add_argument('--file4', '-f4',  default='MLDS_hw2_1_data/testing_data/id.txt', help='the file that contains testing features')
    parser.add_argument('--model', '-m',  default='word_model/model.wv', help='the pretrained word2vec model')
    return parser.parse_args()

if __name__ == '__main__':
    main(parse())


import argparse
import glob
import os
import subprocess
import time
from fontTools.ttLib import TTFont

# Initialize flag parser
parser = argparse.ArgumentParser()
parser.add_argument(
    "--drawbot", help="Render a specimen with DrawBot", action="store_true"
)
parser.add_argument(
    "--fontbakery", help="Test fonts with fontbakery", action="store_true"
)
parser.add_argument(
    "--googlefonts", help="Store GoogleFonts directory name"
)
parser.add_argument(
    "--ttfautohint", help="Store ttfautohint flags"
)
parser.add_argument(
    "--static", help="Build static fonts", action="store_true"
)
parser.add_argument(
    "--fixnonhinting", help="Fix nonhinting with gs tools", action="store_true"
)
args = parser.parse_args()

# Initialize empty lists
sources = []
sources_styles = []

def printR(prt):
    """
    Print in red
    """
    print("\033[91m {}\033[00m".format(prt))

def printG(prt):
    """
    Print in green
    """
    print("\033[92m {}\033[00m".format(prt))

def printY(prt):
    """
    Print in yellow
    """
    print("\033[93m {}\033[00m".format(prt))

def intro():
    """
    Gives basic script info.
    """
    printG("#    # #####        #####    ################")
    printG("#    # #            #   #    #   ##         #")
    printG(" #  #  ####          #   #  #   # #   #######")
    printG(" #  #  #     <---->  #    ##    # #      #")
    printG("  ##   #              #        #  #   ####")
    printG("  ##   #              ##########  #####")
    print("\n**** Starting variable font build script:")
    print("     [+]", time.ctime())
    printG("    [!] Done")
    time.sleep(4)

def display_args():
    """
    Gives info about the flags.
    """
    print("\n**** Settings:")
    print("     [+] --drawbot\t\t", args.drawbot)
    print("     [+] --googlefonts\t\t", args.googlefonts)
    print("     [+] --ttfautohint\t\t", args.ttfautohint)
    print("     [+] --fontbakery\t\t", args.fontbakery)
    print("     [+] --static\t\t", args.static)
    print("     [+] --fixnonhinting\t", args.fixnonhinting)
    printG("    [!] Done")
    time.sleep(4)

def check_root_dir():
    """
    Checks to make sure script is run from a git repo root directory.
    """
    print("\n**** Looking for the font repo root directory:")
    REPO_ROOT = [".gitignore", ".git"]
    repo_test = os.listdir(path=".")
    repo_test_result = all(elem in repo_test for elem in REPO_ROOT)
    if repo_test_result:
        print("     [+] OK: Looks good")
        printG("    [!] Done")
    else:
        printR("     [!] ERROR: Run script from the root directory")
    time.sleep(2)

def get_source_list():
    """
    Gets a list of source files.
    """
    print("\n**** Making a list of Glyphsapp source files:")
    os.chdir("sources")
    for name in glob.glob("*.glyphs"):
        sources.append(os.path.splitext(name)[0])
    os.chdir("..")
    print("     [+] SOURCES: List of sources =", sources)
    time.sleep(1)
    printG("    [!] Done")

def get_style_list():
    """
    Gets a list of styles from the source list.
    """
    print("\n**** Starting build process:")
    for source in sources:
        time.sleep(0.5)
        print("     [+] SOURCES: Preparing to build", source)
        print("     [+] SOURCES: Style =", source.rpartition("-")[2])
        sources_style = str(source.rpartition("-")[2])
        sources_styles.append(sources_style)
    print("     [+] SOURCES: Styles =", sources_styles)
    time.sleep(1)
    printG("    [!] Done")

def run_fontmake_variable():
    """
    Builds ttf variable font files with FontMake.
    """
    for source in sources:
        print("\n**** Building %s variable font files with FontMake:" % source)
        print("     [+] Run: fontmake ")
        subprocess.call(
            "fontmake \
                      -g sources/%s.glyphs \
                      -o variable \
                      --output-path fonts/%s-VF.ttf"
            % (source, source),
            shell=True,
        )
        print("     [!] Done")
    printG("    [!] Done")

def run_fontmake_static():
    """
    Builds ttf static font files with FontMake.
    """
    for source in sources:
        print("\n**** Building %s static font files with FontMake:" % source)
        print("     [+] Run: fontmake ")
        subprocess.call(
            "fontmake \
                      -g sources/%s.glyphs \
                      -o ttf \
                      --keep-overlaps -i"
            % (source),
            shell=True,
        )
        print("     [!] Done")
    printG("    [!] Done")

def prep_static_fonts():
    """
    Move static fonts to the fonts/static directory.
    Run ttfautohint on all fonts and fix missing dsig
    """
    print("\n**** Moving static fonts:")

    for path in glob.glob("instance_ttf/*.ttf"):
        print(path)
        subprocess.call("cp %s fonts/static-fonts/" % path, shell=True)
    subprocess.call("rm -rf instance_ttf", shell=True)

    for static_font in glob.glob("fonts/static-fonts/*.ttf"):
        print(static_font)
        subprocess.call("gftools fix-dsig %s --autofix" % static_font, shell=True)

        if args.fixnonhinting == True:
            print("FIXING NONHINTING")
            subprocess.call("gftools fix-nonhinting %s %s.fix" % (static_font, static_font), shell=True)
            subprocess.call("mv %s.fix %s" % (static_font, static_font), shell=True)
            subprocess.call("rm -rf %s.fix" % static_font, shell=True)
            subprocess.call("rm -rf fonts/static-fonts/*gasp.ttf", shell=True)
            print("     [+] Done:", static_font)

        if args.ttfautohint == True:
            subprocess.call(
                "ttfautohint %s %s temp.ttf"
                % (args.ttfautohint, static_font),
                shell=True,
            )
            subprocess.call("cp temp.ttf %s" % static_font, shell=True)
            subprocess.call("rm -rf temp.ttf", shell=True)
    time.sleep(1)
    printG("    [!] Done")

def rm_build_dirs():
    """
    Cleanup build dirs
    """
    print("\n**** removing build directories")
    print("     [+] run: rm -rf variable_ttf master_ufo instance_ufo")
    subprocess.call("rm -rf variable_ttf master_ufo instance_ufo", shell=True)
    printG("    [!] Done")
    time.sleep(1)

def fix_dsig():
    """
    Fixes DSIG table
    """
    print("\n**** Run: gftools: fix DSIG")
    for source in sources:
        subprocess.call(
            "gftools fix-dsig fonts/%s-VF.ttf --autofix"
            % source,
            shell=True,
        )
        print("     [+] Done:", source)
    printG("    [!] Done")
    time.sleep(1)

def fix_nonhinting():
    """
    Fixes non-hinting
    """
    print("\n**** Run: gftools: fix nonhinting")
    for path in glob.glob("fonts/*.ttf"):
        print(path)
        subprocess.call("gftools fix-nonhinting %s %s.fix" % (path, path), shell=True)
        subprocess.call("mv %s.fix %s" % (path, path), shell=True)
        subprocess.call("rm -rf %s.fix" % path, shell=True)
        subprocess.call("rm -rf fonts/*gasp.ttf", shell=True)
        print("     [+] Done:", path)
    printG("    [!] Done")
    time.sleep(1)

def ttfautohint():
    """
    Runs ttfautohint with various flags set. For more info run: ttfautohint --help
    """
    print("\n**** Run: ttfautohint")
    os.chdir("fonts")
    cwd = os.getcwd()
    print("     [+] In Directory:", cwd)
    for source in sources:
        subprocess.call(
            "ttfautohint %s %s-VF.ttf %s-VF-Fix.ttf"
            % (args.ttfautohint, source, source),
            shell=True,
        )
        subprocess.call("cp %s-VF-Fix.ttf %s-VF.ttf" % (source, source), shell=True)
        subprocess.call("rm -rf %s-VF-Fix.ttf" % source, shell=True)
        print("     [+] Done:", source)
    os.chdir("..")
    cwd = os.getcwd()
    print("     [+] In Directory:", cwd)
    printG("    [!] Done")
    time.sleep(1)

def ttfautohint_static():
    """
    Runs ttfautohint with various flags set. For more info run: ttfautohint --help
    """
    print("\n**** Run: ttfautohint")
    os.chdir("fonts")
    cwd = os.getcwd()
    print("     [+] In Directory:", cwd)
    for source in sources:
        subprocess.call(
            "ttfautohint %s %s-VF.ttf %s-VF-Fix.ttf"
            % (args.ttfautohint, source, source),
            shell=True,
        )
        subprocess.call("cp %s-VF-Fix.ttf %s-VF.ttf" % (source, source), shell=True)
        subprocess.call("rm -rf %s-VF-Fix.ttf" % source, shell=True)
        print("     [+] Done:", source)
    os.chdir("..")
    cwd = os.getcwd()
    print("     [+] In Directory:", cwd)
    printG("    [!] Done")
    time.sleep(1)

def google_fonts():
    """
    Copy font output to the GoogleFonts repo.
    """
    print("\n**** Copying font output to the GoogleFonts repo.")
    if args.googlefonts is not None:
        for source in sources:
            subprocess.call(
                "cp fonts/%s-VF.ttf %s/" % (source, args.googlefonts), shell=True
            )
            print("     [+] Done:", source)
    for path in glob.glob("fonts/static-fonts/*.ttf"):
        print(path)
        subprocess.call("cp %s %s/static/" % (path, args.googlefonts), shell=True)
    else:
        pass
    printG("    [!] Done")
    time.sleep(1)

def fontbakery():
    """
    Run FontBakery on the GoogleFonts repo.
    """
    print("\n**** Run: FontBakery:")
    for source in sources:
        subprocess.call("fontbakery check-googlefonts %s/%s-VF.ttf --ghmarkdown docs/FONTBAKERY-REPORT-%s.md" % (args.googlefonts, source, source), shell=True, )
        print("     [+] Done:", source)
    printG("    [!] Done")
    time.sleep(1)

def render_specimens():
    """
    Render specimens
    """
    print("\n**** Run: DrawBot")
    subprocess.call(
        "python3 docs/drawbot-sources/basic-specimen.py \
        > /dev/null 2>&1",
        shell=True,
    )
    printG("    [!] Done")
    time.sleep(1)

def main():
    """
    Executes font build sequence
    """
    intro()
    display_args()
    check_root_dir()
    get_source_list()
    get_style_list()
    run_fontmake_variable()

    # fix non-hinting
    if args.fixnonhinting == True:
        fix_nonhinting()
    else:
        pass

    # make static fonts
    if args.static == True:
        run_fontmake_static()
        prep_static_fonts()
    else:
        pass

    rm_build_dirs()
    fix_dsig()

    # ttfautohint
    if args.ttfautohint is not None:
        ttfautohint()
    else:
        pass

    # GoogleFonts
    if args.googlefonts is not None:
        google_fonts()
    else:
        pass

    # FontBakery
    if args.fontbakery == True:
        fontbakery()
    else:
        pass

    # DrawBot
    if args.drawbot == True:
        render_specimens()
    else:
        pass

if __name__ == "__main__":
    main()


import xlsxwriter
import datetime
import pandas as pd
from openpyxl import load_workbook
from decimal import Decimal

import dbworker

exchanges = dbworker.get_all_exchange()

def df(seconds):
    id_operation = []
    t_id = []
    create_dt = []
    type = []
    X = []
    commission = []
    X_without_comm = []
    currency_formula = []
    currency_sum = []

    end_dt = []
    admin_id = []

    if exchanges != 0:
        for exchange in exchanges:

            if (((datetime.datetime.now().replace(tzinfo=None) - exchange.end_dt.replace(tzinfo=None)).seconds) +
                (datetime.datetime.now().replace(tzinfo=None) - exchange.end_dt.replace(
                    tzinfo=None)).days * 24 * 3600) < seconds\
                    and exchange.status == 2:
                id_operation.append(exchange.id)
                t_id.append(exchange.t_id)
                create_dt.append(exchange.create_dt)
                if exchange.type == 0:
                    type.append('ПРОДАЖА')
                    X.append(f'{exchange.btc_value:.8f} BTC')
                    commission.append(f'{exchange.commission:.8f} BTC')
                    X_without_comm.append(f'{(exchange.btc_value - exchange.commission):.8f} BTC')

                elif exchange.type == 1:
                    type.append('ПОКУПКА')
                    X.append(f'{exchange.rub_value:.2f} RUB')
                    commission.append(f'{exchange.commission:.2f} RUB')
                    X_without_comm.append(f'{(exchange.rub_value - exchange.commission):.2f} RUB')

                else:
                    type.append('НЕОПРЕДЕЛЕН')
                    X.append(' ')
                    commission.append(' ')
                    X_without_comm.append(' ')
                    currency_formula.append(' ')
                currency_formula.append(
                    f'{exchange.currency_btc}*{exchange.currency_usd}')
                currency_sum.append(f'{(exchange.currency_btc * exchange.currency_usd):.2f}₽')
                end_dt.append(exchange.end_dt)
                admin_id.append(exchange.admin_id)

    df = pd.DataFrame({
        'ID операции': id_operation,
        'ID пользователя': t_id,
        'Время создания операции': create_dt,
        'Направление операции': type,
        'Количество': X,
        'Комиссия': commission,
        'Без комиссии': X_without_comm,
        'Курс-формула': currency_formula,
        'Курс-сумма': currency_sum,
        'Завершение операции': end_dt,
        'ID администратора': admin_id
    })

    if len(df['ID операции']) != 0:
        df['Время создания операции'] = df['Время создания операции'].dt.tz_localize(None)
        df['Завершение операции'] = df['Завершение операции'].dt.tz_localize(None)
    return df

def columns(writer):
    workbook= writer.book

    worksheet = writer.sheets['Обмен']

    worksheet.set_column('A:Z', width=17, cell_format=workbook.add_format({'align': 'center'}))
    worksheet.set_column('D:D', 22)
    worksheet.set_column('C:C', 25)
    worksheet.set_column('H:H', 20)
    worksheet.set_column('I:I', 17)
    worksheet.set_column('K:K', 23)
    worksheet.set_column('J:J', 20)

'''
writer = pd.ExcelWriter('upload.xlsx', engine ='xlsxwriter')
df(24 * 3600).to_excel(writer, sheet_name='Обмен', index=False)
columns(writer)
writer.save()
'''
#print(pd.read_excel('upload.xlsx'))


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

# Standard Library Imports
from collections import defaultdict
import logging
import inspect
import re

# Kodi imports
import xbmcplugin

# Package imports
from codequick.script import Script
from codequick.support import logger_id, auto_sort
from codequick.utils import ensure_native_str

__all__ = ["Route", "validate_listitems"]
_UNSET = object()

# Logger specific to this module
logger = logging.getLogger("%s.route" % logger_id)

# Localized string Constants
SELECT_PLAYBACK_ITEM = 25006
NO_DATA = 33077

def validate_listitems(raw_listitems):
    """Check if listitems are valid"""

    # Convert a generator of listitem into a list of listitems
    if inspect.isgenerator(raw_listitems):
        raw_listitems = list(raw_listitems)
    # Silently ignore False values
    elif raw_listitems is False:
        return False

    if raw_listitems:
        if isinstance(raw_listitems, (list, tuple)):
            if len(raw_listitems) == 1 and raw_listitems[0] is False:
                return False
            else:
                return raw_listitems
        else:
            raise ValueError("Unexpected return object: {}".format(type(raw_listitems)))
    else:
        raise RuntimeError("No items found")

class Route(Script):
    """
    This class is used to create "Route" callbacks. “Route" callbacks, are callbacks that
    return "listitems" which will show up as folders in Kodi.

    Route inherits all methods and attributes from :class:`codequick.Script<codequick.script.Script>`.

    The possible return types from Route Callbacks are.
        * ``iterable``: "List" or "tuple", consisting of :class:`codequick.listitem<codequick.listing.Listitem>` objects.
        * ``generator``: A Python "generator" that return's :class:`codequick.listitem<codequick.listing.Listitem>` objects.
        * ``False``: This will cause the "plugin call" to quit silently, without raising a RuntimeError.

    :raises RuntimeError: If no content was returned from callback.

    :example:
        >>> from codequick import Route, Listitem
        >>>
        >>> @Route.register
        >>> def root(_):
        >>>     yield Listitem.from_dict("Extra videos", subfolder)
        >>>     yield Listitem.from_dict("Play video",
        >>>           "http://www.example.com/video1.mkv")
        >>>
        >>> @Route.register
        >>> def subfolder(_):
        >>>     yield Listitem.from_dict("Play extra video",
        >>>           "http://www.example.com/video2.mkv")
    """

    # Change listitem type to 'folder'
    is_folder = True

    def __init__(self):
        super(Route, self).__init__()
        self.update_listing = self.params.get(u"_updatelisting_", False)
        self.category = re.sub(u"\(\d+\)$", u"", self._title).strip()
        self.cache_to_disc = self.params.get(u"_cache_to_disc_", True)
        self.redirect_single_item = False
        self._manual_sort = list()
        self.content_type = _UNSET
        self.autosort = True

    def _process_results(self, raw_listitems):
        """Handle the processing of the listitems."""
        raw_listitems = validate_listitems(raw_listitems)
        if raw_listitems is False:
            xbmcplugin.endOfDirectory(self.handle, False)
            return None

        # Create a new list containing tuples, consisting of path, listitem, isfolder.
        listitems = []
        folder_counter = 0.0
        mediatypes = defaultdict(int)
        for listitem in raw_listitems:
            if listitem:  # pragma: no branch
                # noinspection PyProtectedMember
                listitem_tuple = listitem._close()
                listitems.append(listitem_tuple)
                if listitem_tuple[2]:  # pragma: no branch
                    folder_counter += 1

                if "mediatype" in listitem.info:
                    mediatypes[listitem.info["mediatype"]] += 1

        # If redirect_single_item is set to True then redirect view to the first
        # listitem if it's the only listitem and that listitem is a folder
        if self.redirect_single_item and len(listitems) == 1 and listitems[0][2] is True:
            return listitems[0][0]  # return the listitem path

        # Guess if this directory listing is primarily a folder or video listing.
        # Listings will be considered to be a folder if more that half the listitems are folder items.
        isfolder = folder_counter > (len(listitems) / 2)

        # Sets the category for skins to display modes.
        xbmcplugin.setPluginCategory(self.handle, ensure_native_str(self.category))

        if self.content_type is not None:
            self.__content_type("files" if isfolder else "videos", mediatypes)

        # Add sort methods only if not a folder(Video listing)
        if not isfolder:
            self.__add_sort_methods(self._manual_sort)

        # Pass the listitems and relevant data to kodi
        success = xbmcplugin.addDirectoryItems(self.handle, listitems, len(listitems))
        xbmcplugin.endOfDirectory(self.handle, success, self.update_listing, self.cache_to_disc)

    def __content_type(self, default_type, mediatypes):  # type: (str, defaultdict) -> None
        """Configure plugin properties, content, category and sort methods."""

        # See if we can guess the content_type based on the mediatypes from the listitem
        if mediatypes and self.content_type is _UNSET:
            if len(mediatypes) > 1:
                from operator import itemgetter
                # Sort mediatypes by there count, and return the highest count mediatype
                mediatype = sorted(mediatypes.items(), key=itemgetter(1))[-1][0]
            else:
                mediatype = mediatypes.popitem()[0]

            # Convert mediatype to a content_type, not all mediatypes can be converted directly
            if mediatype in ("video", "movie", "tvshow", "episode", "musicvideo", "song", "album", "artist"):
                self.content_type = mediatype + "s"

        # Set the add-on content type
        content_type = self.content_type if self.content_type and self.content_type is not _UNSET else default_type
        xbmcplugin.setContent(self.handle, content_type)
        logger.debug("Content-type: %s", content_type)

    def __add_sort_methods(self, sortmethods):  # type: (list) -> None
        """Add sort methods to kodi."""
        _addSortMethod = xbmcplugin.addSortMethod

        if self.autosort:
            # Add unsorted sort method if not sorted by date and no manually set sortmethods are given
            if auto_sort and not (sortmethods or xbmcplugin.SORT_METHOD_DATE in auto_sort):
                sortmethods.append(xbmcplugin.SORT_METHOD_UNSORTED)

            # Keep the order of the manually set sort methods
            # Only sort the auto sort methods
            for method in sorted(auto_sort):
                if method not in sortmethods:
                    sortmethods.append(method)

        if sortmethods:
            for sortMethod in sortmethods:
                _addSortMethod(self.handle, sortMethod)
        else:
            # If no sortmethods are given then set sort mehtod to unsorted
            _addSortMethod(self.handle, xbmcplugin.SORT_METHOD_UNSORTED)

    def add_sort_methods(self, *methods, **kwargs):
        """
        Add sorting method(s).

        Any number of sort method's can be given as multiple arguments.
        Normally this should not be needed, as sort method's are auto detected.

        You can pass an optional keyword only argument, 'disable_autosort' to disable auto sorting.

        :param int methods: One or more Kodi sort method's.

        .. seealso:: The full list of sort methods can be found at.\n
                     https://codedocs.xyz/xbmc/xbmc/group__python__xbmcplugin.html#ga85b3bff796fd644fb28f87b136025f40
        """
        # Disable autosort if requested
        if kwargs.get("disable_autosort", False):
            self.autosort = False

        # Can't use sets here as sets don't keep order
        for method in methods:
            self._manual_sort.append(method)


from collections import deque
m , n = map(int,input().split())

deq= deque(map(int,input().split()))
com=deq[n]
cnt=0
while 1:
    # print("deq는 ", deq)
    # print("n값은 {}".format(n))
    tmp = deq.popleft()
    for i in range(len(deq)):
        if tmp < deq[i]:
            deq.append(tmp)
            if n > 0:
                n = n -1
            elif n == 0:
                n = len(deq) - 1
            break
    else:
        cnt+=1
        if n!=0:
            n = n-1
        else:
            print(cnt)
            break

인프런코딩테스트준비/섹션 5 자료구조 활용/응급실.py


import numpy as np
import h5py
import cv2
import io
from PIL import Image
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from skimage import io
from skimage.io import imsave as sk_imsave
from mpl_point_clicker import clicker
from matplotlib.widgets import RadioButtons
from math import sqrt

def angle(vectors):

    #Returns the angles between vectors.

    vector2 = vectors[1:]
    vector1=vectors[:-1]
    return np.arccos((vector1*vector2).sum(axis=1)/(
        np.sqrt((vector1**2).sum(axis=1)*(vector2**2).sum(axis=1))))

tolerance = 70
min_angle = np.pi*0.22

def distance(a, b):
    return  sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)

def point_line_distance(point, start, end):
    if (start == end):
        return distance(point, start)
    else:
        n = abs(
            (end[0] - start[0]) * (start[1] - point[1]) -
            (start[0] - point[0]) * (end[1] - start[1])
        )
        d = sqrt(
            (end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2
        )
        return n / d

def rdp(points, epsilon):

    dmax = 0.0
    index = 0
    for i in range(1, len(points) - 1):
        d = point_line_distance(points[i], points[0], points[-1])
        if d > dmax:
            index = i
            dmax = d

    if dmax >= epsilon:
        results = rdp(points[:index+1], epsilon)[:-1] + rdp(points[index:], epsilon)
    else:
        results = [points[0], points[-1]]

    return results

def rotateImg(img, da):
    #da = rotation angle

    h, w = img.shape
    center = (w / 2, h / 2)     #centre of image

    #so no resizing
    scale = 1

    #finds affine transform matrix needed to rotate image about its centre
    m = cv2.getRotationMatrix2D(center, da, scale)

    #applies afine transform
    rotatedImg = cv2.warpAffine(img, m, (w,h))

    return rotatedImg

def contourPrep(r, img, lowBinaryThreshold, highBinaryThreshold):
    #binarises cropped image based on low binary threshold.
    #black = pixels below  low binary threshold
    #white = all pixels above low binary threshold

    #cropps to user selected ROI
    currentBox = img[int(r[1]):int(r[1]+r[3]), int(r[0]):int(r[0]+r[2])]

    #readjusts contrast of just ROI so features are noticable
    currentBox = (currentBox-np.nanmin(currentBox))/(np.nanmax(currentBox)-np.nanmin(currentBox))
    currentBox = currentBox * 255
    currentBox = np.array(currentBox, dtype="uint8") #openCV needs this datatype

    #output image saved - to see with adjusted contrast
    scaledBox = currentBox

    #to limit effect of dead pixels, etc
    currentBox = cv2.medianBlur(currentBox, 3)

    #binarises image - convert to just black or white pixels
    ret,thresh = cv2.threshold(currentBox,lowBinaryThreshold,highBinaryThreshold,0) #120, 255, 0
    currentBox = thresh

    #used to display all edges found in binarised image
    edgeImg = cv2.Canny(currentBox, 0, 200)

    #finds edge lines within binarised image.  Stores arrays of points for all edges.
    contours, higherarchy = cv2.findContours(currentBox, 1, cv2.CHAIN_APPROX_TC89_KCOS)

    #Longest contours will be at index 0.  Smallest contours will be furthest from 0.
    #Longest contour should be main edge visible in image.
    sorted_contours = sorted(contours, key=cv2.contourArea, reverse=True)

    return currentBox, scaledBox, edgeImg, sorted_contours

imageNumbers = ["321124-321125", "321127-321136", "321227-321236", "321457-321466", "321527-321536", "321617-321626", "321627-321636"]

differenceImages = []
intensityImages = []

for i in imageNumbers:

    differenceImg = io.imread(r"RC294 - Cross Centre\%s_Diffference.tif" % str(i))
    intensityImg = io.imread(r"RC294 - Cross Centre\%s_Intensity.tif" % str(i))
    #plt.imshow(differenceImg, cmap="gray", vmin = -0.08, vmax = 0.1)
    #plt.show()

    differenceImages = differenceImages  + [differenceImg]
    intensityImages = intensityImages + [intensityImg]

#rotates images manually

offsetAngle = 50

angles = [0, 0, -30, -45, -50, -50, -90]
angles = np.array(angles, dtype = np.int)
angles = angles + offsetAngle

for i in range(0, len(intensityImages)):

    intensityImages[i] = rotateImg(intensityImages[i], angles[i])
    differenceImages[i] = rotateImg(differenceImages[i], angles[i])

initialIntensityImages = intensityImages
initialDifferenceImages = differenceImages

imageStack = []

#select corners for zoom in

firstImgPositions = [] #used to align

#arrays of aligned images
correctedDifferenceImages = []
correctedIntensityImages = []

for i in range(0, len(differenceImages)):

    newImgPositions = [] #for current image in loop

    #adjust contrast so image can be seen
    displayImg = np.clip(intensityImages[i], 10, 30)
    displayImg = (displayImg-np.nanmin(displayImg))/(np.nanmax(displayImg)-np.nanmin(displayImg))

    # Select ROI - 4 corners in cross
    w, h = displayImg.shape

    #loop to select each corner
    for x in range(0, 4):
        #default values for binarising
        lowBinaryThreshold = 120
        highBinaryThreshold = 255

        # Naming a window
        cv2.namedWindow("select ROI", cv2.WINDOW_NORMAL)

        #resizes window, not actual image
        cv2.resizeWindow("select ROI", w , h)

        #gets box dimentions
        r = cv2.selectROI("select ROI", displayImg)
        r = np.array(r)

        #gets edge images and contours
        currentBox, scaledBox, edgeImg, sorted_contours = contourPrep(r, differenceImages[i], 120, 255)

        while len(sorted_contours) < 1:
            #user reselects ROI if no contours found in image - stops program crashing later

            # Naming a window
            cv2.namedWindow("select ROI", cv2.WINDOW_NORMAL)

            #resizes window, not actual image
            cv2.resizeWindow("select ROI", w , h)

            #gets box dimentions
            r = cv2.selectROI("select ROI", displayImg)
            r = np.array(r)

            #gets edge images and contours
            currentBox, scaledBox, edgeImg, sorted_contours = contourPrep(r, differenceImages[i], 120, 255)

        linePoints = []
        first = True

        for point in sorted_contours[0]:

            if point[0, 0] != 0 and point[0, 1] != 0 and point[0, 0] != w and point[0, 1] != h:

                linePoints.append((int(point[0, 0]), int(point[0, 1])))

        #to find smoothed trajectory
        simplified_trajectory = rdp(linePoints, epsilon = 3) #original epsilon = 200

        #x and y points on curve
        sx = [t[0] for t in simplified_trajectory]
        sy = [t[1] for t in simplified_trajectory]
        sx = np.array(sx, dtype = np.int)
        sy = np.array(sy, dtype = np.int)

        # Minimum angle to consider as a turning point
        min_angle = np.pi / 3.0

        # Direction vectors for simplified trajectory
        directions = np.diff(simplified_trajectory, axis=0)
        theta = angle(directions)

        # finds points with dramatic angle changes
        idx = np.where(theta > min_angle)[0] + 1
        idx = np.array(idx, dtype = np.int)
        turningPointsX = sx[idx]
        turningPointsY = sy[idx]
        turningPoints = []

        #extract likely turning points
        for j in range(0, len(turningPointsX)):
            x = turningPointsX[j]
            y = turningPointsY[j]
            turningPoints = turningPoints + [[x, y]]

        plt.figure()
        plt.axis("off")
        #cv2.drawContours(scaledBox, sorted_contours, 0, (0,255,0), 1)

        ax1 = plt.subplot(2, 2, 1)
        ax2 = plt.subplot(2, 2, 2)
        ax3 = plt.subplot(2, 2, 3)

        cv2.drawContours(scaledBox, sorted_contours, 0, (0,255,0), 1)

        ax2.imshow(currentBox, cmap = "gray")
        ax3.imshow(255 - scaledBox, cmap="gray")
        ax3.plot(turningPointsX, turningPointsY, "*")
        ax1.imshow(edgeImg)

        rax = plt.axes([0.47, 0.5, 0.1, 0.15])

        #use to manually adjust binarisation cutoff
        #user changes until good contour found
        radioHandle = RadioButtons(rax, ("lower threshold", "higher threshold"))

        def buttonFunc(label):
            global lowBinaryThreshold
            global highBinaryThreshold
            global r
            global currentBox
            global scaledBox
            global edgeImg
            global sorted_contour
            ax1.clear()
            ax2.clear()
            ax3.clear()

            #adjust binarisation cutoff
            if label == "lower threshold":
                lowBinaryThreshold = lowBinaryThreshold - 5
            if label == "higher threshold":
                lowBinaryThreshold = lowBinaryThreshold + 5

            #gets contours and edge positions
            currentBox, scaledBox, edgeImg, sorted_contours = contourPrep(r, differenceImages[i], lowBinaryThreshold, highBinaryThreshold)
            linePoints = []

            first = True
            for point in sorted_contours[0]:
                #avoid edge of ROI
                if point[0, 0] != 0 and point[0, 1] != 0 and point[0, 0] != w and point[0, 1] != h:
                    linePoints.append((int(point[0, 0]), int(point[0, 1])))

            #smooths trajectory as before
            simplified_trajectory = rdp(linePoints, epsilon = 3)

            #all x and y poitns
            sx = [t[0] for t in simplified_trajectory]
            sy = [t[1] for t in simplified_trajectory]
            sx = np.array(sx, dtype = np.int)
            sy = np.array(sy, dtype = np.int)

            # Use to find turning points
            min_angle = np.pi / 3.0

            #Find angles of direction changes
            directions = np.diff(simplified_trajectory, axis=0)
            theta = angle(directions)

            # Find significant turning points
            idx = np.where(theta > min_angle)[0] + 1
            idx = np.array(idx, dtype = np.int)

            turningPointsX = sx[idx]
            turningPointsY = sy[idx]
            turningPoints = []

            #extract possible turning points
            for j in range(0, len(turningPointsX)):
                x = turningPointsX[j]
                y = turningPointsY[j]
                turningPoints = turningPoints + [[x, y]]

            cv2.drawContours(scaledBox, sorted_contours, 0, (0,255,0), 1)

            ax2.imshow(currentBox, cmap = "gray")
            ax3.imshow(255 - scaledBox, cmap="gray")
            ax3.plot(turningPointsX, turningPointsY, "*")
            ax1.imshow(edgeImg)

            plt.draw()

        radioHandle.on_clicked(buttonFunc)

        plt.show()

        #found contours, next need to place corner in image
        print("i = " + str(i))
        print("x = " + str(x))

        if i == 0:
            img1 = scaledBox

            fig, ax = plt.subplots(constrained_layout=True)
            ax.imshow(img1, cmap="gray")

            #user places point near a corner
            klicker = clicker(ax, ["event"], markers=["x"])
            plt.show()

            #gets position of point placed by user
            positions1 = klicker.get_positions()
            positions1 = np.array(positions1["event"], dtype = np.float32)

            minDist = 9999999999
            posIndex = 0

            for cornerIndex in range(0, len(turningPoints)):
                corner = turningPoints[cornerIndex]
                dist = (corner[0] - positions1[0][0])**2 + (corner[1] - positions1[0][1])
                if dist < minDist:
                    minDist = dist
                    posIndex = cornerIndex
            imgW, imgH = differenceImages[i].shape

            #possible corner positions within ROI selected
            positions1[0][0] = turningPoints[cornerIndex][0]
            positions1[0][1] = turningPoints[cornerIndex][1]

            #Turn ROI corner positions to corner position in overall image
            positions1[0][0] = r[0] + positions1[0][0]
            positions1[0][1] = r[1] - positions1[0][1]

            if len(firstImgPositions) > 0:

                firstImgPositions = np.append(firstImgPositions, positions1, axis = 0)

            else:

                firstImgPositions = np.array(positions1)

        else:

            img2 = scaledBox
            fig, ax = plt.subplots(constrained_layout=True)
            ax.imshow(img2, cmap="gray")

            #gets user placed point
            klicker = clicker(ax, ["event"], markers=["x"])
            plt.show()

            #coordinates of user placed point
            positions2 = klicker.get_positions()
            positions2 = np.array(positions2["event"], dtype = np.float32)

            minDist = 9999999999
            posIndex = 0

            for contourIndex in range(0, len(sorted_contours[0])):
                currentContour = sorted_contours[0][contourIndex]

                dist = (currentContour[0][0] - positions2[0][0])**2 + (currentContour[0][1] - positions2[0][1])**2

                if dist < minDist:
                    minDist = dist
                    posIndex = contourIndex

            #corner positions in ROI
            positions2 = sorted_contours[0][posIndex][:]

            imgW, imgH = differenceImages[i].shape

            #Corner positions in overall image
            positions2[0][0] = r[0] + positions2[0][0]
            positions2[0][1] = r[1] + positions2[0][1]

            #stores corner positions found
            if len(newImgPositions) > 0:

                newImgPositions = np.append(newImgPositions, positions2, axis = 0)
            else:

                newImgPositions = np.array(positions2)

        if i != 0 and x == 3:

            des1 = np.array(firstImgPositions, dtype = np.float32)
            des2 = np.array(newImgPositions, dtype = np.float32)

            plt.imshow(intensityImages[i], cmap="gray", vmin = 10, vmax = 30)
            plt.plot(des2[:,0], des2[:, 1], "*")
            plt.show()

            w,h = img1.shape

            img2 = np.array(intensityImages[i], dtype = np.float32)

            img2 = (img2-np.nanmin(img2))/(np.nanmax(img2)-np.nanmin(img2)) #renormalises - after contrast adjust so shows
            img2 = img2 * 255
            img2 = np.clip(img2, 0, 255)

            img2 = np.array(img2, dtype = np.float32)

            w, h = differenceImages[0].shape

            #estimates affine transformation
            #uses closest match for the 4 points between first image and current image
            m = cv2.estimateAffinePartial2D(des2, des1)
            m = m[0]

            #can remove any rotating
            #m[0][1] = 0
            #m[1][0] = 0

            #applies transformation to align current diffence image
            im2 = cv2.warpAffine(differenceImages[i], (m), (w,h))

            correctedDifferenceImages = correctedDifferenceImages + [im2]

            #applies transformation to align current intensity image
            im2 = cv2.warpAffine(intensityImages[i], (m), (w,h))
            correctedIntensityImages = correctedIntensityImages + [im2]

plt.figure()
plt.axis("off")

ax1 = plt.subplot(1, 2, 1)
ax2 = plt.subplot(1, 2, 2)

img1 = correctedDifferenceImages[0]
img2 = correctedIntensityImages[9]

fig1 = ax1.imshow(img1, cmap="gray")
fig2 = ax2.imshow(img2, cmap="gray")

#defines slider axis
axs = plt.axes([0.15, 0.001, 0.65, 0.03])
sb = Slider(axs, 'image no', 0, 6, valinit=0, valstep = 1)

#runs when slider moved
def update(val):
    img1 = correctedDifferenceImages[sb.val]
    img2 = correctedIntensityImages[sb.val]

    fig1.set_data(img1)
    fig2.set_data(img2)

    plt.draw()

ax1.set_title("differences")

sb.on_changed(update)
plt.show()

for i in range(0, len(correctedDifferenceImages)):
    sk_imsave("finalCross/alignedCrossFinal_D_%s.tif" % str(i), correctedDifferenceImages[i])
    sk_imsave("finalCross/alignedCrossFinal_D_%s.tif" % str(i), correctedIntensityImages[i])

Final Code/Edges with curve smoothing .py


from flask import Flask, request, jsonify, send_file
from lista import lista
import os
import asyncio
import sys

app = Flask(__name__)

@app.route('/')
def pagina_padrao():
    return 'Esta é a página padrão do meu site!'

@app.route('/upload', methods=['POST'])
def upload_file():
    file = request.files['file']
    if file:
        filename = file.filename
        excelFile = os.path.join('uploads', filename)
        file.save(excelFile)

        blocosFileZip = lista(excelFile)
        return send_file(blocosFileZip, as_attachment=True, download_name="BLOCOS-BRANCOS.zip", mimetype='application/zip')
    else:
        return jsonify({'message': 'Nenhum arquivo enviado.'}), 400

# Erro no Handling
def uncaught_exception_handler(ex):
    print("Uncaught Exception:", ex)

def unhandled_rejection_handler(promise, reason):
    print("[GRAVE] Rejeição possivelmente não tratada em: Promise", promise, "motivo:", reason)

sys.excepthook = uncaught_exception_handler

loop = asyncio.get_event_loop()
loop.set_exception_handler(unhandled_rejection_handler)

if __name__ == '__main__':
    from waitress import serve
    serve(app, host="0.0.0.0", port=8080)


from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from . models import *
# Create your views here.
@login_required
def index(request):
    context = {}
    context['languages'] = Language.objects.all()
    context['title'] = "Languages"
    context['language_section'] = "current_section"
    context['language_index'] = "act_item"
    return render(request, 'language/language.html', context )

@login_required
def view_setting(request):
    context = {}
    context['languages'] = Language.objects.all()
    context['title'] = "Languages Setting"
    context['language_section'] = "current_section"
    context['language_setting'] = "act_item"
    return render(request, 'language/setting.html', context )

@login_required
def event_add_language(request):
    context = {}
    if request.method == "POST":
        message = "Language added successfully"
        language = Language.objects.create(
            title = request.POST.get("title"),
            code = request.POST.get("code"),
            flag = request.POST.get("flag")
        )

        language.save()
        context['message'] = message

    context['title'] = "Add Languages"
    context['language_section'] = "current_section"
    context['language_index'] = "act_item"
    return render(request, 'language/add_language.html', context)

@login_required
def event_update_language(request, pk):
    language = Language.objects.get(id = pk)
    language.is_active = False if language.is_active else True
    language.save()
    return redirect("/language/")


from tkinter import *
import time
from PIL import ImageTk,Image

root=Tk()

canvas=Canvas(root,width=1350,height=700)
image=ImageTk.PhotoImage(Image.open("C:\youtube\\clock.png"))

canvas.create_image(0,0,anchor=NW,image=image)
canvas.pack()
def clock():
    h=str(time.strftime("%H"))
    m=str(time.strftime("%M"))
    s=str(time.strftime("%S"))
    #print(h,m,s)

    if int(h)>12 and int(m)>0:
        lbl_noon.config(text="PM")

    if int(h)>12:
        h=str((int(h)-12))

    lbl_hr.config(text=h)
    lbl_min.config(text=m)
    lbl_sec.config(text=s)

    lbl_hr.after(200,clock)

lbl_hr=Label(root,text="12",font=("times new romam",50,"bold"),bg="white",fg="#0875B7")
lbl_hr.place(x=250,y=200,width=200,height=200)

lbl_hr2=Label(root,text="Hour",font=("EB Garamond",35,"bold"),bg="white",fg="#0875B7")
lbl_hr2.place(x=250,y=420,width=200,height=80)

lbl_min=Label(root,text="12",font=("times new romam",50,"bold"),bg="#f5c32f",fg="white")
lbl_min.place(x=450,y=200,width=200,height=200)

lbl_min2=Label(root,text="Min",font=("EB Garamond",35,"bold"),bg="#f5c32f",fg="white")
lbl_min2.place(x=450,y=420,width=200,height=80)

lbl_sec=Label(root,text="12",font=("times new romam",50,"bold"),bg="#ff0000",fg="white")
lbl_sec.place(x=650,y=200,width=200,height=200)

lbl_sec2=Label(root,text="Secs",font=("EB Garamond",35,"bold"),bg="#ff0000",fg="white")
lbl_sec2.place(x=650,y=420,width=200,height=80)

lbl_noon=Label(root,text="AM",font=("times new romam",50,"bold"),bg="#59ff00",fg="white")
lbl_noon.place(x=850,y=200,width=200,height=200)

lbl_noon2=Label(root,text="Noon",font=("EB Garamond",35,"bold"),bg="#59ff00",fg="white")
lbl_noon2.place(x=850,y=420,width=200,height=80)
clock()
root.mainloop()


##USAGE: should only be executed by Makefile, e.g. "make lev_matrix"

from GameUtils.PronunciationUtils.PronunciationUtils import PronunciationUtils
from GameUtils.Curriculum import Curriculum

import pandas as pd
import os
import numpy as np
from weighted_levenshtein.clev import levenshtein as lev

WPSM_PATH = '/GameUtils/PronunciationUtils/data/wpsm.csv'
myUtils = PronunciationUtils()
myCurriculum = [p for p in dir(Curriculum)
                       if isinstance(getattr(Curriculum, p), str)
                       and not p.startswith('__')]

def create_substitution_matrix():

    # read weighted phonemic similarity matrix,
    # downloaded from https://github.com/benhixon/benhixon.github.com/blob/master/wpsm.txt

    # the subsitution matrix

    # because levenshtein is computed on a character-by-character basis, we use the nettalk phonemic
    # representation instead of Arpabet for this task

    # the

    # load the matrix csv file into a dataframe
    df = pd.read_csv(os.getcwd() + WPSM_PATH, sep=',', header=0, index_col=0)

    arpabet_phonemes = list(df.keys())
    #print(df['B'])

    substitute_costs = np.ones((128, 128), #128 is size of ASCII table we care about
                               dtype=np.float64)  # make a 2D array of 1's. ASCII table

    # update the original substitution matrix
    lowest = 100
    lowkey1 = None
    lowkey2 = None

    highest = -100
    lowkey1 = None
    lowkey2 = None

    # calculate min/max values of non-diagonal wpsm scores
    for key1 in arpabet_phonemes:
        for key2 in arpabet_phonemes:
            if not key1 == key2:
                if (-1 * df[key1][key2]) < lowest:
                    lowkey1 = key1
                    lowkey2 = key2
                    lowest = -1 * df[key1][key2]  # "P/B"

                if (-1 * df[key1][key2]) > highest:
                    highkey1 = key1
                    highkey2 = key2
                    highest = -1 * df[key1][key2] #TH DH

    print("lowest score was ")
    print(lowest)
    print(df[lowkey1][lowkey2])

    print("highest score was ")
    print(highest)
    print(df[highkey1][highkey2])

    range = highest - lowest
    print('Should be 1.0 followed by 0.0')
    print(normalize_wpsm_cost(highest, highest, lowest))
    print(normalize_wpsm_cost(lowest, highest, lowest))
    print('----------')

    for key1 in arpabet_phonemes:
        for key2 in arpabet_phonemes:
            nkey1 = myUtils.arpabet_map[key1]
            nkey2 = myUtils.arpabet_map[key2]

            if (nkey1 == nkey2):
                substitute_costs[ord(nkey1), ord(
                    nkey2)] = 0.0  # use zero substitution cost for diagonal entries

                #print("COST was 0 for")
                #print(nkey1)
                #print(nkey2)
            else:
                raw_cost = -1 * df[key1][key2] # negative scores in wpsm indicate less similarity, hence higher cost
                normalized_cost = normalize_wpsm_cost(raw_cost, highest, lowest)
                substitute_costs[ord(nkey1), ord(nkey2)] = normalized_cost

    np.save(os.getcwd() + PronunciationUtils.PHONEME_SUB_COST_PATH, substitute_costs)
    np.savetxt(os.getcwd() + PronunciationUtils.PHONEME_SUB_COST_PATH + '.txt', substitute_costs)

def create_arpabet_covariance_matrix():
    # update the original substitution matrix

    df = pd.read_csv(os.getcwd() + WPSM_PATH, sep=',', header=0, index_col=0)

    arpabet_phonemes = list(df.keys())
    arpabet_size = len(arpabet_phonemes) #SHOULD BE 39 x 39
    print(list(arpabet_phonemes))

    arpabet_covariance = np.ones((arpabet_size, arpabet_size), #128 is size of ASCII table we care about
                               dtype=np.float64)  # make a 2D array of 1's. ASCII table
    lowest = 100
    lowkey1 = None
    lowkey2 = None

    highest = -100
    lowkey1 = None
    lowkey2 = None

    # calculate min/max values of non-diagonal wpsm scores
    for key1 in arpabet_phonemes:
        for key2 in arpabet_phonemes:
            if not key1 == key2:
                if (df[key1][key2]) < lowest:
                    lowkey1 = key1
                    lowkey2 = key2
                    lowest = df[key1][key2]  # "P/B"

                if (df[key1][key2]) > highest:
                    highkey1 = key1
                    highkey2 = key2
                    highest = df[key1][key2] #TH DH

    # print("lowest score was ")
    # print(lowest)
    # print(df[lowkey1][lowkey2])

    # print("highest score was ")
    # print(highest)
    # print(df[highkey1][highkey2])

    range = highest - lowest
    print('Should be 0.8 followed by 0.01')
    print(normalize_wpsm_cost_for_covariance(highest, highest, lowest))
    print(normalize_wpsm_cost_for_covariance(lowest, highest, lowest))
    print('----------')

    for key1 in arpabet_phonemes:
        for key2 in arpabet_phonemes:
            nkey1 = myUtils.arpabet_map[key1]
            nkey2 = myUtils.arpabet_map[key2]

            if (nkey1 == nkey2):

                # give covariance of 1 for identical keys
                arpabet_covariance[arpabet_phonemes.index(key1)][arpabet_phonemes.index(key2)] = 1.0
                #print("COV was 1 for")
                #print(nkey1)
                #print(nkey2)
            else:
                raw_cost = df[key1][key2]
                normalized_cov = normalize_wpsm_cost_for_covariance(raw_cost, highest, lowest)
                arpabet_covariance[arpabet_phonemes.index(key1)][arpabet_phonemes.index(key2)] = normalized_cov #covariance is 0-1

                #print(key1 + " AND " + key2 + " have covariance: " + str(normalized_cov))

    # a = substitute_costs[ord(nkey1)][:]
    # print(ord(nkey1))
    # print(a)
    # b = [x for x in a if not x == 1.0]
    # print(b)
    # print(len(b))
    # c = [x for x in a if x == 0.0]
    # print(c)
    # print(len(c))

    np.save(os.getcwd() + PronunciationUtils.ARPABET_COVARIANCE_PATH, arpabet_covariance)
    np.savetxt(os.getcwd() + PronunciationUtils.ARPABET_COVARIANCE_PATH + '.txt', arpabet_covariance)
def create_levenshtein_cost_matrix():
    weighted_levenshtein_distances = np.ones((len(myCurriculum), len(myCurriculum)),
                               dtype=np.float64)  # make a 2D array of 1's. ASCII table

    for i in range(0, len(myCurriculum)):
        for j in range(0, len(myCurriculum)):
            #yeah I know we're calculating it twice - TODO: Optimize
            weighted_levenshtein_distances[i][j] = measure_weighted_levenshtein_distance(
                myCurriculum[i], myCurriculum[j])

    np.save(os.getcwd() + PronunciationUtils.WEIGHTED_LEV_DISTANCE_PATH, weighted_levenshtein_distances)
    np.savetxt(os.getcwd() + PronunciationUtils.WEIGHTED_LEV_DISTANCE_PATH + '.txt', weighted_levenshtein_distances)

def measure_weighted_levenshtein_distance(word1, word2):
    # import weighted levenshtein library. if clev is missing, change the __init__.py in the weighted_levenshtein lib to add clev.so path to sys.
    # /anaconda3/lib/python3.6/site-packages/weighted_levenshtein
    # delete "from clev import *" in __init__.py

    # read the phoneme substitution costs from disk
    loaded_substitute_costs = np.load(os.getcwd() + myUtils.PHONEME_SUB_COST_PATH + '.npy')
    result = lev(myUtils.get_phonetic_similarity_rep(word1).encode(),
                 myUtils.get_phonetic_similarity_rep(word2).encode(),
                 substitute_costs=loaded_substitute_costs)

    # normalize the levenshtein score by taking max(str1,str2)
    denominator = max(len(word1), len(word2))
    normalized_score = (result / float(denominator))
    #print("weighted-lev distance between " + word1 + " and " + word2 + " was " + str(normalized_score))
    return normalized_score

def normalize_wpsm_cost(raw_cost, highest, lowest):
    """
    converts a raw_cost into the range (0,1), given the highest and lowest wpsm costs
    See: https://stackoverflow.com/questions/5294955/how-to-scale-down-a-range-of-numbers-with-a-known-min-and-max-value
    """
    # print(raw_cost)
    # print("TURNED INTO")
    scaled_cost = ((1 - 0) * (raw_cost - lowest) / (highest - lowest))
    # print(str(scaled_cost) + '\n')
    return scaled_cost

def normalize_wpsm_cost_for_covariance(raw_cost, highest, lowest):
    """
    converts a raw_cost into the range (0,.8), given the highest and lowest wpsm costs
    See: https://stackoverflow.com/questions/5294955/how-to-scale-down-a-range-of-numbers-with-a-known-min-and-max-value
    """
    # print(raw_cost)
    # print("TURNED INTO")
    scaled_cost = ((.8 - 0.01) * (raw_cost - lowest) / (highest - lowest))
    # print(str(scaled_cost) + '\n')
    return scaled_cost + .01

create_substitution_matrix()
create_arpabet_covariance_matrix()
create_levenshtein_cost_matrix()


from turtle import Screen, Turtle
import random

class Shape:
    def __init__(self):
        self.turtles = [
                        self.init_turtle('red', 0, -170, -130),
                        self.init_turtle('blue', 110, 170, -100),
                        self.init_turtle('purple', 55, 10, -210)
                        ]

    def draw(self, times=30):
        for time in range(times):
            turtle = random.choice(self.turtles)
            turtle.circle(500, extent=50)
            turtle.circle(75, extent=130)

    def init_turtle(self, color, deg, x, y):
        turtle = Turtle(shape="circle")
        turtle.penup()
        turtle.setpos(x=x , y=y)
        turtle.pendown()

        turtle.color(color)
        turtle.left(deg)
        turtle.pensize(3)
        turtle.speed(10)

        return turtle

if __name__ == "__main__":
    root = Screen()
    root.bgcolor('black')
    root.title('Atom Shape')

    shape = Shape()
    shape.draw()

    root.exitonclick()


from myhdl import *
import os

import ptp

module = 'ptp_perout'
testbench = 'test_%s' % module

srcs = []

srcs.append("../rtl/%s.v" % module)
srcs.append("%s.v" % testbench)

src = ' '.join(srcs)

build_cmd = "iverilog -o %s.vvp %s" % (testbench, src)

def bench():

    # Parameters
    FNS_ENABLE = 1
    OUT_START_S = 0x0
    OUT_START_NS = 0x0
    OUT_START_FNS = 0x0000
    OUT_PERIOD_S = 1
    OUT_PERIOD_NS = 0
    OUT_PERIOD_FNS = 0x0000
    OUT_WIDTH_S = 0x0
    OUT_WIDTH_NS = 1000
    OUT_WIDTH_FNS = 0x0000

    # Inputs
    clk = Signal(bool(0))
    rst = Signal(bool(0))
    current_test = Signal(intbv(0)[8:])

    input_ts_96 = Signal(intbv(0)[96:])
    input_ts_step = Signal(bool(0))
    enable = Signal(bool(0))
    input_start = Signal(intbv(0)[96:])
    input_start_valid = Signal(bool(0))
    input_period = Signal(intbv(0)[96:])
    input_period_valid = Signal(bool(0))
    input_width = Signal(intbv(0)[96:])
    input_width_valid = Signal(bool(0))

    # Outputs
    locked = Signal(bool(0))
    error = Signal(bool(0))
    output_pulse = Signal(bool(0))

    # PTP clock
    ptp_clock = ptp.PtpClock()

    ptp_logic = ptp_clock.create_logic(
        clk,
        rst,
        ts_96=input_ts_96
    )

    # DUT
    if os.system(build_cmd):
        raise Exception("Error running build command")

    dut = Cosimulation(
        "vvp -m myhdl %s.vvp -lxt2" % testbench,
        clk=clk,
        rst=rst,
        current_test=current_test,
        input_ts_96=input_ts_96,
        input_ts_step=input_ts_step,
        enable=enable,
        input_start=input_start,
        input_start_valid=input_start_valid,
        input_period=input_period,
        input_period_valid=input_period_valid,
        input_width=input_width,
        input_width_valid=input_width_valid,
        locked=locked,
        error=error,
        output_pulse=output_pulse
    )

    @always(delay(32))
    def clkgen():
        clk.next = not clk

    @instance
    def check():
        yield delay(100)
        yield clk.posedge
        rst.next = 1
        yield clk.posedge
        rst.next = 0
        yield clk.posedge
        yield delay(100)
        yield clk.posedge

        # testbench stimulus

        enable.next = 1

        yield clk.posedge
        print("test 1: Test pulse out")
        current_test.next = 1

        input_start.next = 100 << 16
        input_start_valid.next = 1
        input_period.next = 100 << 16
        input_period_valid.next = 1
        input_width.next = 50 << 16
        input_width_valid.next = 1

        yield clk.posedge

        input_start_valid.next = 0
        input_period_valid.next = 0
        input_width_valid.next = 0

        yield delay(10000)

        yield delay(100)

        yield clk.posedge
        print("test 2: Test pulse out")
        current_test.next = 2

        input_start.next = 0 << 16
        input_start_valid.next = 1
        input_period.next = 100 << 16
        input_period_valid.next = 1
        input_width.next = 50 << 16
        input_width_valid.next = 1

        yield clk.posedge

        input_start_valid.next = 0
        input_period_valid.next = 0
        input_width_valid.next = 0

        yield delay(10000)

        yield delay(100)

        raise StopSimulation

    return instances()

def test_bench():
    sim = Simulation(bench())
    sim.run()

if __name__ == '__main__':
    print("Running test...")
    test_bench()


"""
Some common things, mostly based on specutils.

E.g. Collapsing, but also collapsing the uncertainty.
"""

import numpy as np
from astropy.nddata import StdDevUncertainty
from copy import deepcopy

def collapse_flux_and_unc(s3d):
    """

    s3d: 3D Spectrum1D cube with uncertainty

    returns: Spectrum1D
        Single spectrum

    """
    spec_avg = s3d.collapse(method=np.nanmean, axis="spatial")

    N = np.count_nonzero(s3d.uncertainty.array, axis=(0, 1))
    spec_avg_unc = np.sqrt(
        np.nansum(np.square(s3d.uncertainty.array), axis=(0, 1))
    ) / np.count_nonzero(np.isfinite(s3d.uncertainty.array), axis=(0, 1))
    spec_avg.uncertainty = StdDevUncertainty(spec_avg_unc * s3d.flux.unit)
    return spec_avg

def clean_spikes(s3d):
    """Some rules to mask out the worst of the spikes in the data"""
    spec2 = deepcopy(s3d)
    f = spec2.flux.value
    u = spec2.uncertainty.array
    median_per_slice = np.array(
        [np.nanmedian(f[..., i][f[..., i] > 0]) for i in range(f.shape[2])]
    )
    too_big = np.abs(f) > 125 * np.abs(median_per_slice)[np.newaxis, np.newaxis, :]
    zero_unc = u <= 0
    bad_unc = ~np.isfinite(u)

    spec2.flux.value[too_big | zero_unc | bad_unc] = np.nan
    spec2.uncertainty.array[too_big | zero_unc | bad_unc] = np.nan
    spec2.mask = ~(np.isfinite(f) & np.isfinite(u))
    spec2.meta = s3d.meta

    return spec2


import time
from typing import Dict

import tensorflow as tf

from .helpers import get_mnist, MNIST_CLASSES, MNIST_FEATURES

def full_precision_net(features: tf.placeholder, weights: Dict[str, tf.Variable]):
    """
    Constructs full precision model
    :param features: model input in form of placeholder
    :param weights: dictionary of weights
    """
    # First hidden layer
    z_1 = tf.matmul(features, weights['h1'])
    a_1 = tf.nn.relu(z_1)

    # Second hidden layer
    z_2 = tf.matmul(a_1, weights['h2'])
    a_2 = tf.nn.relu(z_2)

    # Third hidden layer
    z_3 = tf.matmul(a_2, weights['h3'])
    a_3 = tf.nn.relu(z_3)

    # Network output
    out_layer = tf.matmul(a_3, weights['out'])
    return out_layer

def run_full_precision_mlp(learning_rate: float,
                           num_steps: int,
                           batch_size: int,
                           display_step: int,
                           hidden_size: int):
    # get MNIST dataset
    mnist = get_mnist()

    # region <<<Network build>>>

    # construct stateless graph
    features = tf.placeholder("float", [None, MNIST_FEATURES])
    targets = tf.placeholder("float", [None, MNIST_CLASSES])

    # store layers weights
    weights = {
        'h1': tf.Variable(tf.random_normal([MNIST_FEATURES, hidden_size])),
        'h2': tf.Variable(tf.random_normal([hidden_size, hidden_size])),
        'h3': tf.Variable(tf.random_normal([hidden_size, hidden_size])),
        'out': tf.Variable(tf.random_normal([hidden_size, MNIST_CLASSES]))
    }

    # construct model
    logits = full_precision_net(
        features=features,
        weights=weights
    )

    prediction = tf.nn.softmax(logits)

    # define loss and optimizer
    loss_op = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=targets)
    )

    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
    train_op = optimizer.minimize(loss_op)

    # evaluate model
    correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(targets, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

    # initialize the variables
    init = tf.global_variables_initializer()
    # endregion

    # region <<<Training>>>
    with tf.Session() as sess:

        # run initializer
        sess.run(init)

        start_time = time.time()

        # repeat required number of steps
        for step in range(1, num_steps + 1):

            # get mini-batch
            batch_features, batch_targets = mnist.train.next_batch(batch_size)

            # feed batch into network and optimize
            sess.run(
                train_op,
                feed_dict={
                    features: batch_features,
                    targets: batch_targets
                }
            )

            # logging
            if step % display_step == 0 or step == 1:
                # calculate loss and accuracy for the batch
                loss, acc = sess.run(
                    [loss_op, accuracy],
                    feed_dict={
                        features: batch_features,
                        targets: batch_targets
                    }
                )

                # print the result
                print('[{:5d}] loss: {:10.2f}\taccuracy: {:2.2f}%'.format(
                    step, loss, acc * 100.0
                ))
        # endregion

        elapsed_time = time.time() - start_time
        print("elapsed time: {:.4f} seconds".format(elapsed_time))

        # region <<<Testing>>>
        test_accuracy = sess.run(
            accuracy,
            feed_dict={
                features: mnist.test.images,
                targets: mnist.test.labels
            }
        ) * 100.0
        print("Testing Accuracy: {:.4f}%".format(test_accuracy))
    # endregion


import os
import sqlite3
import unittest
from typing import Union

import helpers.db as db
import setup_db
from econ_types.trader import Trader
from logger import Logger

logger: Logger = Logger('[ test_db]')

class TestDb(unittest.TestCase):

    def setUp(self):
        logger.important('Setting up the test...', True)

        self.test_db_path = "tests/db/traders_test.db"
        os.makedirs(os.path.dirname(self.test_db_path), exist_ok=True)

        setup_db.setup(os.path.abspath(self.test_db_path), False)

        self.conn = sqlite3.connect(os.path.abspath(self.test_db_path))

        cursor = self.conn.cursor()
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='traders'")
        table_exists = cursor.fetchone()

        if table_exists:
            cursor.execute("DROP TABLE traders")
            cursor.execute(
                """
            CREATE TABLE IF NOT EXISTS traders (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                user_id INTEGER NOT NULL,
                low_grade_balance INTEGER NOT NULL DEFAULT 0,
                medium_grade_balance INTEGER NOT NULL DEFAULT 0,
                high_grade_balance INTEGER NOT NULL DEFAULT 0
            )
            """
            )

            trader = Trader(user_id=1, bal_low_grade=10, bal_medium_grade=20, bal_high_grade=30)
            db.create_trader(trader, self.conn)

            self.conn.commit()

        trader = Trader(user_id=1, bal_low_grade=10, bal_medium_grade=20, bal_high_grade=30)
        db.create_trader(trader, self.conn)

        cursor.close()

    def tearDown(self):
        if self.conn:
            self.conn.close()

        if os.path.exists(self.test_db_path):
            os.remove(self.test_db_path)

    def test_create_trader(self):
        logger.info('Running test: test_create_trader')

        retrieved_trader = db.get_trader(1, self.conn)

        self.assertEqual(retrieved_trader.user_id, 1)
        logger.success('TEST [1/4]: user_id')

        self.assertEqual(retrieved_trader.low_grade_balance, 10)
        logger.success('TEST [2/4]: low_grade_balance')

        self.assertEqual(retrieved_trader.medium_grade_balance, 20)
        logger.success('TEST [3/4]: medium_grade_balance')

        self.assertEqual(retrieved_trader.high_grade_balance, 30)
        logger.success('TEST [4/4]: high_grade_balance')

        logger.important('Passed all test_create_trader tests.')

    def test_get_trader(self):
        logger.info('Running test: test_get_trader')

        test_result = db.get_trader(1, self.conn)

        if not type(test_result).__name__ == 'Trader':
            self.fail(
                f"get_trader did not return an object of type `Trader`.\n"
                f"It returned type: {type(test_result)}\n"
                f"type.__name__...: {type(test_result).__name__}")

        self.assertEqual(test_result.user_id, 1)
        logger.success('TEST [1/4]: user_id')

        self.assertEqual(test_result.low_grade_balance, 10)
        logger.success('TEST [2/4]: low_grade_balance')

        self.assertEqual(test_result.medium_grade_balance, 20)
        logger.success('TEST [3/4]: medium_grade_balance')

        self.assertEqual(test_result.high_grade_balance, 30)
        logger.success('TEST [4/4]: high_grade_balance')

        logger.important('Passed all test_get_trader tests.')

    def test_update_trader(self):
        logger.info('Running test: test_update_trader')

        test_result = db.update_trader(1, self.conn,
                                       low_grade_balance=111,
                                       medium_grade_balance=222,
                                       high_grade_balance=333)

        if test_result is not True:
            self.fail('test_update_trader FAILED, it did not return a bool of True.\n'
                      f'It returned type {type(test_result).__name__}: {test_result}')

        self.assertTrue(test_result)
        logger.success('TEST [1/1]: True received')

        logger.important('Passed all test_update_trader tests.')

    def test_delete_trader(self):
        logger.info('Running test: test_delete_trader')

        # verify the trader exists before deleting
        retrieved_trader_before = db.get_trader(1, self.conn)
        self.assertIsNotNone(retrieved_trader_before)
        logger.success('TEST [1/3]: Trader exists')

        test_result = db.delete_trader(1, self.conn)

        self.assertTrue(test_result)
        logger.success('TEST [2/3]: Trader deleted')

        # verify that the trader no longer exists after deletion
        retrieved_trader_after = db.get_trader(1, self.conn)
        self.assertFalse(retrieved_trader_after)
        logger.success('TEST [3/3]: Trader does not exist after deletion')

        logger.important('Passed all test_delete_trader tests.')

    def test_reset_trader_all_balances(self):
        logger.info('Running test: test_reset_trader_all_balances')

        test_result = db.reset_trader_all_balances(1, self.conn)

        if test_result is not True:
            self.fail('test_reset_trader_all_balances FAILED, it did not return a bool of True.\n'
                      f'It returned type {type(test_result).__name__}: {test_result}')

        self.assertTrue(test_result)
        logger.success('TEST [1/4]: True received')

        test_result_confirm = db.get_trader(1, self.conn)

        self.assertEqual(test_result_confirm.low_grade_balance, 0)
        logger.success('TEST [2/4]: low_grade_balance is 0')

        self.assertEqual(test_result_confirm.medium_grade_balance, 0)
        logger.success('TEST [3/4]: medium_grade_balance is 0')

        self.assertEqual(test_result_confirm.high_grade_balance, 0)
        logger.success('TEST [4/4]: high_grade_balance is 0')

        logger.important('Passed all test_reset_trader_all_balances tests.')

    def test_reset_trader_grade_balance(self):
        logger.info('Running test: test_reset_trader_grade_balance')

        test_result_low_grade = db.reset_trader_grade_balance(1, 'low_grade_balance', self.conn)
        test_result_medium_grade = db.reset_trader_grade_balance(1, 'medium_grade_balance', self.conn)
        test_result_high_grade = db.reset_trader_grade_balance(1, 'high_grade_balance', self.conn)

        if test_result_low_grade is not True:
            self.fail('test_reset_trader_grade_balance FAILED, it did not return a bool of True.\n'
                      f'It returned type {type(test_result_low_grade).__name__}: {test_result_low_grade}')

        elif test_result_medium_grade is not True:
            self.fail('test_reset_trader_grade_balance FAILED, it did not return a bool of True.\n'
                      f'It returned type {type(test_result_medium_grade).__name__}: {test_result_medium_grade}')

        elif test_result_high_grade is not True:
            self.fail('test_reset_trader_grade_balance FAILED, it did not return a bool of True.\n'
                      f'It returned type {type(test_result_high_grade).__name__}: {test_result_high_grade}')

        self.assertTrue(test_result_low_grade)
        logger.success('TEST [1/6]: True received from test_result_low_grade')

        self.assertTrue(test_result_medium_grade)
        logger.success('TEST [2/6]: True received from test_result_medium_grade')

        self.assertTrue(test_result_high_grade)
        logger.success('TEST [3/6]: True received from test_result_high_grade')

        test_result = db.get_trader(1, self.conn)

        self.assertEqual(test_result.low_grade_balance, 0)
        logger.success('TEST [4/6]: low_grade_balance is 0')

        self.assertEqual(test_result.medium_grade_balance, 0)
        logger.success('TEST [5/6]: medium_grade_balance is 0')

        self.assertEqual(test_result.high_grade_balance, 0)
        logger.success('TEST [6/6]: high_grade_balance is 0')

        logger.important('Passed all test_reset_trader_grade_balance tests.')