# The setup script installs all the required dependencies
# and create a virtual environment to run the code

import argparse
import os
import subprocess

parser = argparse.ArgumentParser(description='This script is for first time setup on windows machine.\
                                              It creates a python virtual environment and installs \
                                              all the required packages. For help type: python setup.py --help.\
                                              To run this setup script type: python setup.py')
args = parser.parse_args()

def install_cmd(cmd):
    command_run = subprocess.call(cmd, shell=True)
    if command_run == 0:
        # Success
        return 0
    else:
        # Cmd failed
        return 1

if __name__ == "__main__":
    print("Installing python-pip\n")
    pip_cmd = install_cmd("python3 -m ensurepip --upgrade")
    if pip_cmd:
        print("Unable to install python-pip. Terminating ...")
        quit()

    print("Installing python-virtualenv\n")
    pip_cmd = install_cmd("pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org virtualenv")
    if pip_cmd:
        print("Unable to install python-virtualenv. Terminating ...")
        quit()

    print("Creating virtual enviroment virtual-env\n")
    pip_cmd = install_cmd("python3 -m venv virtual-env")
    if pip_cmd:
        print("Unable to craete python virtual environment. Terminating ...")
        quit()

    pip_path = "virtual-env{sep}Scripts{sep}pip3.exe".format(sep=os.sep)
    print("Installing all the required dependencies ...\n")
    pip_cmd = install_cmd("{pip_path} install --trusted-host pypi.org --trusted-host files.pythonhosted.org -r requirements.txt"
                        .format(pip_path=pip_path))
    if pip_cmd:
        print("Unable to install dependencies. Terminating ...")
        quit()

    print("Virtual environment created.\n")
    print("To activate the virtual environment type the following command:\n")
    print("$ virtual-env\\Scripts\\activate")


from pyspark.sql import SparkSession
from pyspark.sql.functions import *

spark = SparkSession.builder.appName("wiki").config("spark.sql.streaming.checkpointLocation", "/tmp/checkpoint").getOrCreate()

df = spark \
  .readStream \
  .format("kafka") \
  .option("kafka.bootstrap.servers", "localhost:9092") \
  .option("subscribe", "click") \
  .option("failOnDataLoss", "false") \
  .option("startingOffsets", "earliest") \
  .load() \
  .selectExpr("CAST(value AS STRING)")

schema = "prev_title STRING, timestamp TIMESTAMP"
df = df.select(from_json(col("value"), schema).alias("data")).select("data.*")

watermarkedDF = df \
    .withWatermark("timestamp", "10 minutes") \
    .groupBy(window("timestamp", "5 minutes"), "prev_title") \
    .agg(count("*").alias("count"), max("timestamp").alias("timestamp"))

watermarkedDF.createOrReplaceTempView("wiki")

sqlDF = spark.sql("SELECT prev_title as source, count, timestamp FROM wiki")

query = sqlDF.writeStream \
    .outputMode("append") \
    .format("org.apache.spark.sql.cassandra") \
    .option("keyspace", "wiki") \
    .option("table", "realtime") \
    .start()

query.awaitTermination()


import urllib.request
from bs4 import BeautifulSoup

'''
urllib.error.URLError:
SSL認証エラー発生
macOSが原因
https://qiita.com/orangain/items/0a641d980019fd7e0c52

'''

class Scraper:
    def __init__(self, site):
        self.site = site

    def scrape(self):
        response = urllib.request.urlopen(self.site)
        html = response.read()
        parser = "html.parser"
        soup = BeautifulSoup(html, parser)

        with open("news_headline.txt", "w") as file:
            for tag in soup.find_all("a"):
                url = tag.get("href")
                # 日本語のニュースページにはhtmlを含むhref属性がない
                if url and  "articles" in url:
                    file.write("/n" + url)
        print("file save end")

news = "https://news.google.com/"
Scraper(news).scrape()


class Eror_mniejsza(Exception):
    pass

a=1 #poczatek przedzialu
while True:
    try:
        b=int(input("Prosze podac koniec przedzialu: ")) #koniec przedzialu
        break
    except:
        print("zla wartosc konca przedzialu ")
        continue
n=100 #liczba prostokatow na ktore dzielimy szukane pole
if a>=b:
    print("za maly przedzial")
    raise Eror_mniejsza
    quit()
x=(b-a)/n #szerokosc boku 1dnego prostokats
s=a+x/2 #srodek prostokata
P=0
#print(x) #debug
for i in range(0,n): #f(srodek w danym momencie prostokata) czyli sumujemy wysokosci az do (n-1)
    P+=(1/(s+(i*x)))
    #print(1/(s+(i*x))) #debug
P*=x #bo przek
print("Pole wynosi:")
print(P)


from tkinter import *

from src.widgets.Custom_button import Custom_button

class X_button(Custom_button):
    def __init__(self, master=None):
        Custom_button.__init__(self,master)

    def create_shape(self,x = 5,y = 5,x_size = 50,y_size = 25,radius = 50,color = "#000000",**kwargs): #round
        points = [
            x               ,y,
            x+radius        ,y,
            x+x_size-radius ,y,

            x+x_size        ,y,
            x+x_size        ,y+radius ,
            x+x_size        ,y+y_size-radius,

            x+x_size        ,y+y_size,
            x+x_size-radius ,y+y_size,
            x+radius        ,y+y_size,

            x               ,y+y_size,
            x               ,y+y_size-radius,
            x               ,y+radius
        ]

        return self.canva.create_polygon(points, smooth=True, fill=color)


#! /usr/local/bin/python3.7
"""
Consider the following "magic" 3-gon ring, filled with the numbers 1 to 6, and
each line adding to nine.

       4
        \
         \
          3
         / \
        /   \
       1-----2-----6
      /
     /
    5

Working clockwise, and starting from the group of three with the numerically
lowest external node (4,3,2 in this example), each solution can be described
uniquely. For example, the above solution can be described by the
set {4,3,2; 6,2,1; 5,1,3}.

It is possible to complete the ring with four different totals: 9, 10, 11, and
12. There are eight solutions in total:

    Total    Solution set
     9       4,2,3; 5,3,1; 6,1,2
     9       4,3,2; 6,2,1; 5,1,3
    10       2,3,5; 4,5,1; 6,1,3
    10       2,5,3; 6,3,1; 4,1,5
    11       1,4,6; 3,6,2; 5,2,4
    11       1,6,4; 5,4,2; 3,2,6
    12       1,5,6; 2,6,4; 3,4,5
    12       1,6,5; 3,5,4; 2,4,6

By concatenating each group it is possible to form 9-digit strings; the maximum
string for a 3-gon ring is 432621513.

Using the numbers 1 to 10, and depending on arrangements, it is possible to form
16- and 17-digit strings. What is the maximum 16-digit string for a "magic"
5-gon ring?

Solution: 6531031914842725
"""
from itertools import permutations

def PE_68():
    """
    There are 10 x 5 x 4 = 120 options for the first 3-node set
    There are 3 x 2 x 1 = 10 options for the second 3-node set
    There are 1 x 1 x 1 = 1 options for the third 3-node set
    So there are 120 x 10 x 1 = 720 total options
    Check each to see if test condition holds, and if so add result to output

    >>> PE_68()
    6531031914842725
    """
    known_strings = []
    base = set(range(1, 11))
    for a, b, c in permutations(base, 3):
        subtotal = a + b + c
        for e, d in generate_next(base, (a, b, c), 2, subtotal - c):
            for f, g in generate_next(base, (a, b, c, d, e), 2, subtotal - e):
                for h, i in generate_next(base, (a, b, c, d, e, f, g), 2, subtotal - g):
                    for (j,) in generate_next(base, (a, b, c, d, e, f, g, h, i), 1, subtotal - b - i):
                        string = ''.join(map(str, (a, b, c, d, c, e, f, e, g, h, g, i, j, i, b)))
                        if not_a_rotation(string, known_strings):
                            known_strings.append(string)
    return int(known_strings[-1])

def generate_next(base, known, size, control_subtotal):
    return (digits for digits in permutations(base - set(known), size)
            if sum(digits) == control_subtotal)

def not_a_rotation(string, known_strings):
    return not any(map(lambda known_string: are_rotations(string, known_string),
                       known_strings))

def are_rotations(string1, string2):
    for _ in range(len(string1)):
        string2 = string2[1:] + string2[0]
        if string1 == string2:
            return True
    return False

if __name__ == '__main__':
    import doctest; doctest.testmod()


import tkinter as tk
from functools import partial
import socket

class BoardClass:
    """BoardClass provides method to use and create the interface and also the method that used to run Tic Tac Toe"""
    window = 0
    hostAddress = 0
    hostPort = 0
    userName = 0
    otherPlayer = 0
    lastPlayer = 0
    numTotal = 0
    numWins = 0
    numTies = 0
    numLoss = 0
    move = 0
    socket = 0
    buttonList = [[' ', ' ', ' '],
                  [' ', ' ', ' '],
                  [' ', ' ', ' ']]
    boardList = [[' ', ' ', ' '],
                 [' ', ' ', ' '],
                 [' ', ' ', ' ']]

    def __init__(self, name: str, move: str, socket: socket) -> None:
        """set up window and some basic buttons
            transform all variable to the type can be used in tk inter
            disable all board button for player2

            args:
                name: user's name that's going to be presented at the stats area's first line
                move: character presents your move
                socket: object to receive and send information with the other player"""
        self.setUpWindow()
        self.initTkVariable()
        self.createLabelAndEntry()
        self.createButton()
        self.runUI()
        self.move.set(move)
        self.userName.set(name)
        self.createStatsArea()
        self.socketSetUp(socket)
        if move == 'o' or move == 'O':
            for i in range(3):
                for j in range(3):
                    self.buttonList[i][j]['state'] = 'disabled'

    def socketSetUp(self, socket: socket) -> None:
        """create socket attribute for later information change with competitor"""
        self.socket = socket

    def runUI(self) -> None:
        """keep updating the interface"""
        self.window.update_idletasks()
        self.window.update()

    def initTkVariable(self) -> None:
        """Change all variable type then they can be used for tk interface"""
        self.hostAddress = tk.StringVar()
        self.hostPort = tk.IntVar()
        self.move = tk.StringVar()
        self.userName = tk.StringVar()
        self.numTotal = tk.IntVar()
        self.numTies = tk.IntVar()
        self.numWins = tk.IntVar()
        self.numLoss = tk.IntVar()
        self.otherPlayer = tk.StringVar()
        self.lastPlayer = tk.StringVar()

    def setUpWindow(self) -> None:
        """create interface"""
        self.window = tk.Tk()
        self.window.geometry('600x600')
        self.window.title('Tic Tac Toe')
        self.window.configure(background='LightSteelBlue')
        self.window.resizable(0, 0)

    def createLabelAndEntry(self) -> None:
        """create host address and port labels and entrys
            create last player's label and winner's label"""
        self.hostAddressEntry = tk.Entry(self.window, textvariable=self.hostAddress, width=10)
        self.hostAddressEntry.grid(row=0, column=4)
        self.AddressLabel = tk.Label(self.window, text='Please Enter The Host Address Here:')
        self.AddressLabel.grid(row=0, column=3)

        self.hostPortEntry = tk.Entry(self.window, textvariable=self.hostPort, width=10)
        self.hostPortEntry.grid(row=1, column=4)
        self.portLabel = tk.Label(self.window, text='Please Enter Your Port Number Here:')
        self.portLabel.grid(row=1, column=3)

        self.lastPlayer = tk.Label(self.window, text=self.lastPlayer.get())
        self.lastPlayer.grid(row=0, column=1)

        self.winner = tk.Label(self.window, text='winner:')
        self.winner.grid(row=4, column=1)

    def createButton(self) -> None:
        """create 9 board buttons, by clicking on the button, player can indicate occupation of the place"""
        self.quitButton = tk.Button(self.window, text='Quit', command=self.window.destroy)
        self.quitButton.grid(row=5, column=4)
        for row in range(3):
            for col in range(3):
                new_command = partial(self.changeBoardAndCheckWinner, row, col)
                self.buttonList[row][col] = tk.Button(self.window, text='', command=new_command, width=5, height=4)
                self.buttonList[row][col].grid(row=row + 1, column=col)

    def changeBoardAndCheckWinner(self, row, col) -> None:
        """Command for board button
            change the text of button to player's move character and disable all 9 buttons untill the player's next round arrive
            check if the updated board has a winner, """
        if self.move.get() == 'o' or self.move.get() == 'O':
            self.buttonList[row][col].config(text='O')
            self.boardList[row][col] = 'O'
        elif self.move.get() == 'x' or self.move.get() == 'X':
            self.buttonList[row][col].config(text='X')
            self.boardList[row][col] = 'X'

        for i in range(3):
            for j in range(3):
                self.buttonList[i][j]['state'] = 'disabled'

        self.lastPlayer.config(text=self.otherPlayer.get())
        self.socket.send(bytes('Move ' + str(row) + ' ' + str(col), 'ascii'))
        if self.isWinner() == True:
            self.numTotal.set(self.numTotal.get() + 1)
            self.numWins.set(self.numWins.get() + 1)
            self.winner.config(text='winner: ' + self.userName.get())
            if self.move.get() == 'x' or self.move.get() == 'X':
                self.createPlayAgainButton()
            self.createStatsArea()
            for i in range(3):
                for j in range(3):
                    self.buttonList[i][j]['state'] = 'disabled'
        elif self.boardIsFull() == True and self.isWinner() == False:
            self.numTotal.set(self.numTotal.get() + 1)
            self.numTies.set(self.numTies.get() + 1)
            self.winner.config(text='Tie')
            if self.move.get() == 'x' or self.move.get() == 'X':
                self.createPlayAgainButton()
            self.createStatsArea()
            for i in range(3):
                for j in range(3):
                    self.buttonList[i][j]['state'] = 'disabled'

    def createStatsArea(self) -> None:
        """Create a label displays user name, other player's n=ame, total number of games
            number of wins, loss and ties"""
        self.stats = tk.Label(self.window, text=f'Player: {self.userName.get()}\n\n'
                                                f'Player: {self.otherPlayer.get()}\n\n'
                                                f'Total number of games: {self.numTotal.get()}\n\n'
                                                f'Number of wins: {self.numWins.get()}\n\n'
                                                f'Number of ties: {self.numTies.get()}\n\n'
                                                f'Number of loss: {self.numLoss.get()}\n\n')
        self.stats.grid(row=4, column=3)

    def boardIsFull(self) -> bool:
        """Check whether the board is full."""
        emptyNum  = 9
        for rows in self.boardList:
            for col in rows:
                if col != ' ':
                    emptyNum -= 1
        if emptyNum == 0:
            return True
        else:
            return False

    def isWinner(self) -> bool:
        """Check if the last player win this game."""
        isWin = False
        for row in self.boardList:
            if row[0] == row[1] and row[0] == row[2] and row[0] != ' ':
                isWin = True

        for c in range(3):
            if self.boardList[0][c] == self.boardList[1][c] and self.boardList[0][c] == self.boardList[2][c] and self.boardList[0][c] != ' ':
                isWin = True

        if self.boardList[1][1] == self.boardList[0][0] and self.boardList[1][1] == self.boardList[2][2] and self.boardList[1][1] != ' ':
            isWin = True
        elif self.boardList[1][1] == self.boardList[0][2] and self.boardList[1][1] == self.boardList[2][0] and self.boardList[1][1] != ' ':
            isWin = True

        return isWin

    def createBindButton(self) -> None:
        """create bind button"""
        self.bind = tk.Button(self.window, text='Bind', command=self.bindSocket)
        self.bind.grid(row=2, column=3)

    def bindSocket(self) -> None:
        """command for bind button. By clicking a server socket will be bind. If error occurs during this process
            ifBindAgain function will be called.
            And before connect client, it will call ifAcceptConnectWindow to check if player2 wants to connect"""
        try:
            address = self.hostAddressEntry.get()
            port = int(self.hostPortEntry.get())
            self.socket.bind((address, port))
            self.bind.destroy()
            self.socket.listen(5)
            clientSocket, clientAddress = self.socket.accept()
            self.socket = clientSocket
            self.ifAcceptConnectWindow()
        except:
            self.ifBindAgain()

    def ifAcceptConnectWindow(self) -> None:
        """create a little window asking player2 if he/she wants to play
            yesButton will destroy the little window and back to normal playing process
            noButton will destry the little window and wait for player1 for his/her next move"""
        self.acceptWindow = tk.Toplevel(self.window)
        self.acceptWindow.title('if accept connection')
        self.info = tk.Label(self.acceptWindow, text='Do you want to accept the connection?')
        self.info.grid(row=0, column=1)
        self.yesButton = tk.Button(self.acceptWindow, text='Yes', command=self.acceptWindow.destroy)
        self.yesButton.grid(row=1, column=0)
        self.noButton = tk.Button(self.acceptWindow, text='No', command=lambda: [self.socket.send(bytes('reject', 'ascii')),
                                                                                self.socket.close(),
                                                                                self.acceptWindow.destroy()])
        self.noButton.grid(row=1, column=2)

    def ifBindAgain(self) -> None:
        """Create a little window displaying the error message and two buttons asking user to
            decide whether bind again."""
        self.failBindWindow = tk.Toplevel(self.window)
        self.failBindWindow.title('Bind Fail')
        self.errorInfo = tk.Label(self.failBindWindow, text='Unsuccessful Binding. Try Again?')
        self.errorInfo.grid(row=0, column=1)
        self.yesButton = tk.Button(self.failBindWindow, text='Yes', command=self.failBindWindow.destroy)
        self.yesButton.grid(row=1, column=0)
        self.noButton = tk.Button(self.failBindWindow, text='No', command=self.window.destroy)
        self.noButton.grid(row=1, column=2)

    def createConnectButton(self) -> None:
        """create connect button"""
        self.connect = tk.Button(self.window, text='Connect', command=self.connectSocketAndCreateNameEntry)
        self.connect.grid(row=2, column=3)

    def connectSocketAndCreateNameEntry(self) -> None:
        """command for connectButton. By clicking it, player1's socket will try to connect player2's.
            If failed, call the createFailConnectWindow function"""
        try:
            address = self.hostAddressEntry.get()
            port = int(self.hostPortEntry.get())
            self.socket.connect((address, port))
            self.connect.destroy()

            self.nameLabel = tk.Label(self.window, text='Enter Your Username Here:')
            self.nameLabel.grid(row=2, column=3)
            self.nameEntry = tk.Entry(self.window, textvariable=self.userName, width=10)
            self.nameEntry.grid(row=2, column=4)

            self.nameButton = tk.Button(self.window, text='Send', command=self.getAndSendName)
            self.nameButton.grid(row=3, column=3)
        except:
            self.createFailConnectWindow()

    def createNewConnectButton(self) -> None:
        """create a New ConnectButton"""
        self.connect = tk.Button(self.window, text='Connect', command=self.connectSocketAndCreateNameEntry)
        self.connect.grid(row=3, column=4)

    def createFailConnectWindow(self) -> None:
        """create a window displaying error message
             yesButton to close the little window
             noButton to close all windows for player1"""
        self.failConnectWindow = tk.Toplevel(self.window)
        self.failConnectWindow.title('Connect Fail')
        self.errorInfo = tk.Label(self.failConnectWindow, text='Unsuccessful Connection. Try Again?')
        self.errorInfo.grid(row=0, column=1)
        self.yesButton = tk.Button(self.failConnectWindow, text='Yes', command=self.failConnectWindow.destroy)
        self.yesButton.grid(row=1, column=0)
        self.noButton = tk.Button(self.failConnectWindow, text='No', command=self.window.destroy)
        self.noButton.grid(row=1, column=2)

    def getAndSendName(self) -> None:
        """check if the username is alphanumeric, if is, send it to player2
            if not, call createWarningwindow function"""
        try:
            for element in self.nameEntry.get():
                if ord(element) not in range(48, 58) and ord(element) not in range(65, 91) and ord(element) not in range(97,123):
                 raise ValueError
            self.nameButton.destroy()
            self.userName.set(self.nameEntry.get())
            self.lastPlayer.config(text=self.userName.get())
            self.createStatsArea()
            self.socket.send(bytes('Name' + ' ' + self.userName.get(), 'ascii'))
        except:
            self.createWarningWindow()

    def createWarningWindow(self) -> None:
        """create a little window displaying name error message
            okButton to close the little window"""
        self.warningWindow = tk.Toplevel(self.window)
        self.warningInfo = tk.Label(self.warningWindow, text='Please enter alphanumeric username.')
        self.warningInfo.grid(row=0, column=0)
        self.okButton = tk.Button(self.warningWindow, text="Ok", command=self.warningWindow.destroy)
        self.okButton.grid(row=1, column=0)

    def createPlayAgainButton(self) -> None:
        """create playAgainButton and stopPlayButton"""
        self.playAgainButton = tk.Button(self.window, text='Play Again', command=self.playAgain)
        self.playAgainButton.grid(row=3, column=3)

        self.stopPlayButton = tk.Button(self.window, text='Stop Play', command=self.stopPlay)
        self.stopPlayButton.grid(row=3, column=4)

    def playAgain(self) -> None:
        """command for playAgainButton. it will destroy both playAgainButton and StopPlay button
            and send play again message to player 2"""
        self.stopPlayButton.destroy()
        self.playAgainButton.destroy()
        self.resetBoard()
        self.socket.send(bytes('Play Again', 'ascii'))

    def stopPlay(self) -> None:
        """command for playAgainButton. it will destroy both playAgainButton and StopPlay button
                and send stop play message to player 2"""
        self.stopPlayButton.destroy()
        self.playAgainButton.destroy()
        self.socket.send(bytes('Fun Times', 'ascii'))
        self.createStatsWindow()

    def resetBoard(self) -> None:
        """Reset both button list which destroys 9 old button and create 9 news
            and board list which used to tell if a winner if exist"""
        self.buttonList = [[' ', ' ', ' '],
                           [' ', ' ', ' '],
                           [' ', ' ', ' ']]

        for row in range(3):
            for col in range(3):
                new_command = partial(self.changeBoardAndCheckWinner, row, col)
                self.buttonList[row][col] = tk.Button(self.window, text='', command=new_command, width=5, height=4)
                self.buttonList[row][col].grid(row=row + 1, column=col)

        if self.move.get() == 'o' or self.move.get() == 'O':
            for i in range(3):
                for j in range(3):
                    self.buttonList[i][j]['state'] = 'disabled'

        self.boardList = [[' ', ' ', ' '],
                          [' ', ' ', ' '],
                          [' ', ' ', ' ']]

        self.winner.config(text='winner:')

        if self.move.get() == 'X':
            self.lastPlayer.config(text=self.userName.get())
        elif self.move.get() == "O":
            self.lastPlayer.config(text=self.otherPlayer.get())

    def createStatsWindow(self) -> None:
        """This will create a window to show stats when the player decides not to play again.
            and the window has a button that can destroy the board window."""
        self.statsWindow = tk.Toplevel(self.window)
        self.statsLabel = tk.Label(self.statsWindow, text=f'Player: {self.userName.get()}\n\n'
                                                          f'Player: {self.otherPlayer.get()}\n\n'
                                                          f'Total number of games: {self.numTotal.get()}\n\n'
                                                          f'Number of wins: {self.numWins.get()}\n\n'
                                                          f'Number of ties: {self.numTies.get()}\n\n'
                                                          f'Number of loss: {self.numLoss.get()}\n\n')
        self.statsLabel.grid(row=1, column=1)

        self.quit = tk.Button(self.statsWindow, text='Quit', command=self.window.destroy)
        self.quit.grid(row=1, column=0)


import uuid

from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.data_classes import APIGatewayProxyEvent
from pydantic import ValidationError

from api.models import Action, EnumerationQueryArgs, LambdaResponse
from api.repository import DynamoActionRepository
from api.responses import BadRequest, NotFound, Ok

logger = Logger(service="gw-api", utc=True)

def introspect(context) -> LambdaResponse:
    return Ok.as_json({"version": context.function_version, "schema": ""})

def enumerate(event: APIGatewayProxyEvent) -> LambdaResponse:
    raw_qargs = (
        {} if event["queryStringParameters"] is None else event["queryStringParameters"]
    )
    try:
        qargs = EnumerationQueryArgs(**raw_qargs)
    except ValidationError as ve:
        logger.info("Unable to parse query args", extra={"errors": ve.errors()})
        return BadRequest.as_json(ve.errors())

    uid = str(uuid.UUID(int=0))
    repo = DynamoActionRepository()
    if qargs.status:
        result = repo.get_actions_by_status(uid, qargs.status)
    elif qargs.created_at:
        result = repo.get_actions_by_created_at(
            uid, since=qargs.created_at.since, until=qargs.created_at.until
        )
    elif qargs.completed_at:
        result = repo.get_actions_by_completed_at(
            uid, since=qargs.completed_at.since, until=qargs.completed_at.until
        )
    else:
        result = repo.enumerate_actions_for_user(uid)
    return Ok.as_json(result)

def run(event: APIGatewayProxyEvent) -> LambdaResponse:
    # Do something interesting
    repo = DynamoActionRepository()
    action = Action(details={"endpoint": "run"}, created_by=uuid.UUID(int=0))
    repo.store_actions(action)
    return Ok.as_json(action)

def status(event: APIGatewayProxyEvent) -> LambdaResponse:
    uid = str(uuid.UUID(int=0))
    repo = DynamoActionRepository()

    assert event.path_parameters
    action_id = event.path_parameters["action_id"]
    action = repo.get_action_by_id(uid, action_id)

    if action is None:
        logger.info(
            "Unable to find Action", extra={"user_id": uid, "action_id": action_id}
        )
        return NotFound.as_json(f"Action with ID {action_id} was not found.")
    return Ok.as_json(action)

def cancel(event: APIGatewayProxyEvent) -> LambdaResponse:
    return Ok.as_json({"Endpoint": "cancel"})

def release(event: APIGatewayProxyEvent) -> LambdaResponse:
    return Ok.as_json({"Endpoint": "release"})


"""There is already an impleentation of rouge_l in rouge.py but this is specifically for AC model """
import numpy as np
import torch
from loaders.caption import PAD

def _lcs(x, y):
    n = len(x)
    m = len(y)
    table = dict()

    for i in range(n + 1):
        for j in range(m + 1):
            if i == 0 or j == 0:
                table[i, j] = 0
            elif x[i - 1] == y[j - 1]:
                table[i, j] = table[i - 1, j - 1] + 1
            else:
                table[i, j] = max(table[i - 1, j], table[i, j - 1])

    def recon(i, j):
        if i == 0 or j == 0:
            return []
        elif x[i - 1] == y[j - 1]:
            return recon(i - 1, j - 1) + [x[i - 1]]
        elif table[i - 1, j] > table[i, j - 1]:
            return recon(i - 1, j)
        else:
            return recon(i, j - 1)

    return len(recon(n, m)), n, m

def rouge_l(evals, refs):
    # if evals.size() != refs.size():
    # print(evals.size())
    # print(refs.size())
    use_cuda = evals.is_cuda

    evals, refs = map(lambda x: x.data.cpu().numpy(), [evals, refs])

    scores = []
    for eva, ref in zip(evals, refs):
        same_len, eva_len, ref_len = map(float,
                                         _lcs(eva, ref[np.where(ref > PAD)]))

        if ref_len != 0:
            # print("{} ref len \t {} eva len".format(ref_len, eva_len))
            r_lcs, p_lcs = same_len / ref_len, same_len / eva_len

        beta = p_lcs / (r_lcs + 1e-12)
        f_lcs = ((1 + (beta**2)) * r_lcs * p_lcs) / \
            (r_lcs + ((beta**2) * p_lcs) + 1e-12)
        scores.append(f_lcs)

    scores = np.asarray(scores, dtype=np.float32)
    scores = torch.autograd.Variable(torch.from_numpy(scores), requires_grad=False)

    if use_cuda:
        scores = scores.cuda()

    return scores

def mask_score(props, words, scores):
    assert words.size() == scores.size()
    mask = (words > 0).float()

    return props * scores * mask


import builtins
import numpy as np
import symengine as se
from importlib import import_module
from core.cbfs.cbf_wrappers import symbolic_cbf_wrapper_singleagent
from core.mathematics.symbolic_functions import ramp

vehicle = builtins.PROBLEM_CONFIG["vehicle"]
control_level = builtins.PROBLEM_CONFIG["control_level"]
mod = "models." + vehicle + "." + control_level + ".system"

# Programmatic import
try:
    module = import_module(mod)
    globals().update({"f": getattr(module, "f")})
    globals().update({"ss": getattr(module, "xs")})
except ModuleNotFoundError as e:
    print("No module named '{}' -- exiting.".format(mod))
    raise e

# Defining Physical Params
R = 1.0
cx1 = -2.0
cy1 = 0.0
cx2 = 1.25
cy2 = -1.25

# Define Physical Distances
dx1 = ss[0] - cx1
dy1 = ss[1] - cy1
dx2 = ss[0] - cx2
dy2 = ss[1] - cy2

# Define more quantities
phidot = f(np.zeros((len(ss),)), True)[6]
thedot = f(np.zeros((len(ss),)), True)[7]

# Speed CBF Symbolic
h_oa1_symbolic = dx1**2 + dy1**2 - R**2
h_oa2_symbolic = dx2**2 + dy2**2 - R**2
h_oa3_symbolic = ss[2] + f(np.zeros((len(ss),)), True)[2]
h_oa4_symbolic = (
    -phidot * se.sin(ss[6]) * se.cos(ss[7])
    - thedot * se.cos(ss[6]) * se.sin(ss[7])
    + (se.cos(ss[6]) * se.cos(ss[7]) - np.cos(np.pi / 3)) * 0.5
)

# h_oa4_symbolic = -10 * (
#     phidot * (se.sin(ss[6]) * se.cos(ss[7]))
#     + thedot * (se.cos(ss[7]) + se.sin(ss[6]) * se.cos(ss[7]) + se.cos(ss[6]) * se.sin(ss[7]))
#     + (se.sin(ss[7]) - se.sin(ss[6]) * se.cos(ss[7]) - se.cos(ss[6]) * se.cos(ss[7]))
# )

dhdx_oa1_symbolic = (se.DenseMatrix([h_oa1_symbolic]).jacobian(se.DenseMatrix(ss))).T
dhdx_oa2_symbolic = (se.DenseMatrix([h_oa2_symbolic]).jacobian(se.DenseMatrix(ss))).T
dhdx_oa3_symbolic = (se.DenseMatrix([h_oa3_symbolic]).jacobian(se.DenseMatrix(ss))).T
dhdx_oa4_symbolic = (se.DenseMatrix([h_oa4_symbolic]).jacobian(se.DenseMatrix(ss))).T

d2hdx2_oa1_symbolic = dhdx_oa1_symbolic.jacobian(se.DenseMatrix(ss))
d2hdx2_oa2_symbolic = dhdx_oa2_symbolic.jacobian(se.DenseMatrix(ss))
d2hdx2_oa3_symbolic = dhdx_oa3_symbolic.jacobian(se.DenseMatrix(ss))
d2hdx2_oa4_symbolic = dhdx_oa4_symbolic.jacobian(se.DenseMatrix(ss))

h_oa1_func = symbolic_cbf_wrapper_singleagent(h_oa1_symbolic, ss)
h_oa2_func = symbolic_cbf_wrapper_singleagent(h_oa2_symbolic, ss)
h_oa3_func = symbolic_cbf_wrapper_singleagent(h_oa3_symbolic, ss)
h_oa4_func = symbolic_cbf_wrapper_singleagent(h_oa4_symbolic, ss)

dhdx_oa1_func = symbolic_cbf_wrapper_singleagent(dhdx_oa1_symbolic, ss)
dhdx_oa2_func = symbolic_cbf_wrapper_singleagent(dhdx_oa2_symbolic, ss)
dhdx_oa3_func = symbolic_cbf_wrapper_singleagent(dhdx_oa3_symbolic, ss)
dhdx_oa4_func = symbolic_cbf_wrapper_singleagent(dhdx_oa4_symbolic, ss)

d2hdx2_oa1_func = symbolic_cbf_wrapper_singleagent(d2hdx2_oa1_symbolic, ss)
d2hdx2_oa2_func = symbolic_cbf_wrapper_singleagent(d2hdx2_oa2_symbolic, ss)
d2hdx2_oa3_func = symbolic_cbf_wrapper_singleagent(d2hdx2_oa3_symbolic, ss)
d2hdx2_oa4_func = symbolic_cbf_wrapper_singleagent(d2hdx2_oa4_symbolic, ss)

# Tau Formulation for PCA-CBF
dvx = f(np.zeros((len(ss),)), True)[0]
dvy = f(np.zeros((len(ss),)), True)[1]

tau_sym = se.Symbol("tau", real=True)

# tau* for computing tau
epsilon = 1e-3
tau1_star_symbolic = -(dx1 * dvx + dy1 * dvy) / (dvx**2 + dvy**2 + epsilon)
dtau1stardx_symbolic = (se.DenseMatrix([tau1_star_symbolic]).jacobian(se.DenseMatrix(ss))).T
d2tau1stardx2_symbolic = dtau1stardx_symbolic.jacobian(se.DenseMatrix(ss))
tau1_star = symbolic_cbf_wrapper_singleagent(tau1_star_symbolic, ss)
dtau1stardx = symbolic_cbf_wrapper_singleagent(dtau1stardx_symbolic, ss)
d2tau1stardx2 = symbolic_cbf_wrapper_singleagent(d2tau1stardx2_symbolic, ss)

tau2_star_symbolic = -(dx2 * dvx + dy2 * dvy) / (dvx**2 + dvy**2 + epsilon)
dtau2stardx_symbolic = (se.DenseMatrix([tau2_star_symbolic]).jacobian(se.DenseMatrix(ss))).T
d2tau2stardx2_symbolic = dtau2stardx_symbolic.jacobian(se.DenseMatrix(ss))
tau2_star = symbolic_cbf_wrapper_singleagent(tau2_star_symbolic, ss)
dtau2stardx = symbolic_cbf_wrapper_singleagent(dtau2stardx_symbolic, ss)
d2tau2stardx2 = symbolic_cbf_wrapper_singleagent(d2tau2stardx2_symbolic, ss)

# tau for computing PCA-CBF
Tmax = 10.0
kh = 1000.0
tau_star_sym = se.Symbol("tau_star", real=True)
tau_symbolic = tau_star_sym * ramp(tau_star_sym, kh, 0.0) - (tau_star_sym - Tmax) * ramp(
    tau_star_sym, kh, Tmax
)
dtaudtaustar_symbolic = se.diff(tau_symbolic, tau_star_sym)
d2taudtaustar2_symbolic = se.diff(dtaudtaustar_symbolic, tau_star_sym)
tau = symbolic_cbf_wrapper_singleagent(tau_symbolic, [tau_star_sym])
dtaudtaustar = symbolic_cbf_wrapper_singleagent(dtaudtaustar_symbolic, [tau_star_sym])
d2taudtaustar2 = symbolic_cbf_wrapper_singleagent(d2taudtaustar2_symbolic, [tau_star_sym])

# Predictive Obstacle Avoidance CBF1
h1_predictive_oa_symbolic = (dx1 + tau_sym * dvx) ** 2 + (dy1 + tau_sym * dvy) ** 2 - R**2
dhdx1_predictive_oa_symbolic = (
    se.DenseMatrix([h1_predictive_oa_symbolic]).jacobian(se.DenseMatrix(ss))
).T
dh1dtau_predictive_oa_symbolic = se.diff(h1_predictive_oa_symbolic, tau_sym)
d2h1dx2_predictive_oa_symbolic = dhdx1_predictive_oa_symbolic.jacobian(se.DenseMatrix(ss))
d2h1dtau2_predictive_oa_symbolic = se.diff(dh1dtau_predictive_oa_symbolic, tau_sym)
d2h1dtaudx_predictive_oa_symbolic = (
    se.DenseMatrix([dh1dtau_predictive_oa_symbolic]).jacobian(se.DenseMatrix(ss))
).T
h1_predictive_oa = symbolic_cbf_wrapper_singleagent(h1_predictive_oa_symbolic, ss)
dh1dx_predictive_oa = symbolic_cbf_wrapper_singleagent(dhdx1_predictive_oa_symbolic, ss)
dh1dtau_predictive_oa = symbolic_cbf_wrapper_singleagent(dh1dtau_predictive_oa_symbolic, ss)
d2h1dx2_predictive_oa = symbolic_cbf_wrapper_singleagent(d2h1dx2_predictive_oa_symbolic, ss)
d2h1dtaudx_predictive_oa = symbolic_cbf_wrapper_singleagent(d2h1dtaudx_predictive_oa_symbolic, ss)
d2h1dtau2_predictive_oa = symbolic_cbf_wrapper_singleagent(d2h1dtau2_predictive_oa_symbolic, ss)

# Predictive Obstacle Avoidance CBF2
h2_predictive_oa_symbolic = (dx2 + tau_sym * dvx) ** 2 + (dy2 + tau_sym * dvy) ** 2 - R**2
dhdx2_predictive_oa_symbolic = (
    se.DenseMatrix([h2_predictive_oa_symbolic]).jacobian(se.DenseMatrix(ss))
).T
dh2dtau_predictive_oa_symbolic = se.diff(h2_predictive_oa_symbolic, tau_sym)
d2h2dx2_predictive_oa_symbolic = dhdx2_predictive_oa_symbolic.jacobian(se.DenseMatrix(ss))
d2h2dtau2_predictive_oa_symbolic = se.diff(dh2dtau_predictive_oa_symbolic, tau_sym)
d2h2dtaudx_predictive_oa_symbolic = (
    se.DenseMatrix([dh2dtau_predictive_oa_symbolic]).jacobian(se.DenseMatrix(ss))
).T
h2_predictive_oa = symbolic_cbf_wrapper_singleagent(h2_predictive_oa_symbolic, ss)
dh2dx_predictive_oa = symbolic_cbf_wrapper_singleagent(dhdx2_predictive_oa_symbolic, ss)
dh2dtau_predictive_oa = symbolic_cbf_wrapper_singleagent(dh2dtau_predictive_oa_symbolic, ss)
d2h2dx2_predictive_oa = symbolic_cbf_wrapper_singleagent(d2h2dx2_predictive_oa_symbolic, ss)
d2h2dtaudx_predictive_oa = symbolic_cbf_wrapper_singleagent(d2h2dtaudx_predictive_oa_symbolic, ss)
d2h2dtau2_predictive_oa = symbolic_cbf_wrapper_singleagent(d2h2dtau2_predictive_oa_symbolic, ss)

# Relaxed Predictive Collision Avoidance
relaxation = 0.1  # Quadrotor Simulation

def h0_oa1(ego):
    return h_oa1_func(ego)

def h0_oa2(ego):
    return h_oa2_func(ego)

def dh0dx_oa1(ego):
    ret = dhdx_oa1_func(ego)

    return np.squeeze(np.array(ret).astype(np.float64))

def dh0dx_oa2(ego):
    ret = dhdx_oa2_func(ego)

    return np.squeeze(np.array(ret).astype(np.float64))

def d2h0dx2_oa1(ego):
    ret = d2hdx2_oa1_func(ego)

    return np.squeeze(np.array(ret).astype(np.float64))

def d2h0dx2_oa2(ego):
    ret = d2hdx2_oa2_func(ego)

    return np.squeeze(np.array(ret).astype(np.float64))

def h_poa1(ego):
    func = h1_predictive_oa(ego)

    try:
        ret = func.subs({tau_sym: tau([tau1_star(ego)])})
    except AttributeError:
        ret = func

    return ret

def h_poa2(ego):
    func = h2_predictive_oa(ego)

    try:
        ret = func.subs({tau_sym: tau([tau2_star(ego)])})
    except AttributeError:
        ret = func

    return ret

def dhdx_poa1(ego):
    func1 = dh1dx_predictive_oa(ego)
    func2 = dh1dtau_predictive_oa(ego)

    try:
        ret1 = func1.subs({tau_sym: tau([tau1_star(ego)])})
    except AttributeError:
        ret1 = func1

    try:
        ret2 = func2.subs({tau_sym: tau([tau1_star(ego)])})
    except AttributeError:
        ret2 = func2

    ret = ret1 + ret2 * dtaudtaustar([tau1_star(ego)]) * dtau1stardx(ego)

    return np.squeeze(np.array(ret).astype(np.float64))

def dhdx_poa2(ego):
    func1 = dh2dx_predictive_oa(ego)
    func2 = dh2dtau_predictive_oa(ego)

    try:
        ret1 = func1.subs({tau_sym: tau([tau2_star(ego)])})
    except AttributeError:
        ret1 = func1

    try:
        ret2 = func2.subs({tau_sym: tau([tau2_star(ego)])})
    except AttributeError:
        ret2 = func2

    ret = ret1 + ret2 * dtaudtaustar([tau2_star(ego)]) * dtau2stardx(ego)

    return np.squeeze(np.array(ret).astype(np.float64))

# Necessary for stochastic systems
def d2hdx2_poa1(ego):
    func1 = d2h1dx2_predictive_oa(ego)
    func2 = dh1dtau_predictive_oa(ego)

    try:
        ret1 = func1.subs({tau_sym: tau([tau1_star(ego)])})
    except AttributeError:
        ret1 = func1

    try:
        ret2 = func2.subs({tau_sym: tau([tau1_star(ego)])})
    except AttributeError:
        ret2 = func2

    d2hdx2_eval = ret1
    dtaustardx_eval = dtau1stardx(ego)
    dtaudtaustar_eval = dtaudtaustar([tau1_star(ego)])
    dhdtau_eval = ret2
    d2hdtau2_eval = d2h1dtau2_predictive_oa(ego)
    d2taudtaustar2_eval = d2taudtaustar2([tau1_star(ego)])
    d2taustardx2_eval = d2tau1stardx2(ego)
    outer = np.outer(dtaustardx_eval, dtaustardx_eval)

    ret = (
        d2hdx2_eval
        + dtaudtaustar_eval * d2hdtau2_eval * dtaudtaustar_eval * outer
        + dhdtau_eval * d2taudtaustar2_eval * outer
        + dhdtau_eval * d2taustardx2_eval * dtaudtaustar_eval
    )

    return np.squeeze(np.array(ret).astype(np.float64))

def d2hdx2_poa2(ego):
    func1 = d2h2dx2_predictive_oa(ego)
    func2 = dh2dtau_predictive_oa(ego)

    try:
        ret1 = func1.subs({tau_sym: tau([tau2_star(ego)])})
    except AttributeError:
        ret1 = func1

    try:
        ret2 = func2.subs({tau_sym: tau([tau2_star(ego)])})
    except AttributeError:
        ret2 = func2

    d2hdx2_eval = ret1
    dtaustardx_eval = dtau2stardx(ego)
    dtaudtaustar_eval = dtaudtaustar([tau2_star(ego)])
    dhdtau_eval = ret2
    d2hdtau2_eval = d2h2dtau2_predictive_oa(ego)
    d2taudtaustar2_eval = d2taudtaustar2([tau2_star(ego)])
    d2taustardx2_eval = d2tau2stardx2(ego)
    outer = np.outer(dtaustardx_eval, dtaustardx_eval)

    ret = (
        d2hdx2_eval
        + dtaudtaustar_eval * d2hdtau2_eval * dtaudtaustar_eval * outer
        + dhdtau_eval * d2taudtaustar2_eval * outer
        + dhdtau_eval * d2taustardx2_eval * dtaudtaustar_eval
    )

    return np.squeeze(np.array(ret).astype(np.float64))

def h_oa1(ego):
    return relaxation * h0_oa1(ego) + h_poa1(ego)

def h_oa2(ego):
    return relaxation * h0_oa2(ego) + h_poa2(ego)

def h_oa3(ego):
    return h_oa3_func(ego)

def h_oa4(ego, force):
    return h_oa4_func(ego)

def dhdx_oa1(ego):
    ret = relaxation * dh0dx_oa1(ego) + dhdx_poa1(ego)

    return np.squeeze(np.array(ret).astype(np.float64))

def dhdx_oa2(ego):
    ret = relaxation * dh0dx_oa2(ego) + dhdx_poa2(ego)

    return np.squeeze(np.array(ret).astype(np.float64))

def dhdx_oa3(ego):
    ret = dhdx_oa3_func(ego)

    return np.squeeze(np.array(ret).astype(np.float64))

def dhdx_oa4(ego, force):
    ret = dhdx_oa4_func(ego)

    return np.squeeze(np.array(ret).astype(np.float64))

# Necessary for stochastic systems
def d2hdx2_oa1(ego):
    ret = relaxation * d2h0dx2_oa1(ego) + d2hdx2_poa1(ego)

    return np.squeeze(np.array(ret).astype(np.float64))

def d2hdx2_oa2(ego):
    ret = relaxation * d2h0dx2_oa2(ego) + d2hdx2_poa2(ego)

    return np.squeeze(np.array(ret).astype(np.float64))

def d2hdx2_oa3(ego):
    ret = d2hdx2_oa3_func(ego)

    return np.squeeze(np.array(ret).astype(np.float64))

def d2hdx2_oa4(ego):
    ret = d2hdx2_oa4_func(ego)

    return np.squeeze(np.array(ret).astype(np.float64))


import heapq  # python에서 heap을 사용하기 위한 라이브러리
import sys    # python에서는 min heap을 지원한다.
N = int(input())
arr =[]

for _ in range(N):
    num=int(sys.stdin.readline())
    heapq.heappush(arr,num)  # arr를 heap으로 이용
for _ in range(N):
    print(heapq.heappop(arr)) # heap에서 pop

알고리즘1주차/24수정렬하기2.py


from fastapi import FastAPI, Request
from starlette.middleware.sessions import SessionMiddleware

from backend.db.database import async_session_maker
from backend.api import admin_router, reader_router

from front.reader_pages import router as reader_pages

app = FastAPI()

app.add_middleware(SessionMiddleware, secret_key='secret')

@app.get('')
async def home():
    return {'Hello': 'World'}

@app.middleware('http')
async def db_session_middleware(request: Request, call_next):
    request.state.db_session = async_session_maker()
    response = await call_next(request)
    await request.state.db_session.close()
    return response

app.include_router(
    admin_router,
    prefix='/admin',
    tags=['admin']
)

app.include_router(
    reader_router,
    prefix='/reader',
    tags=['reader']
)

app.include_router(
    reader_pages,
    prefix='/reader_pages',
    tags=['reader_pages']
)


from django.conf.urls import url
from .views import (
    login_view,
    logout_view,
    polls_view,
    poll_vote_view,
    poll_results_view,
)

urlpatterns = [
    url(r'^$', login_view, name='login'),
    url(r'^logout$', logout_view, name='logout'),
    url(r'^polls/$', polls_view, name='public_polls'),
    url(r'^polls/(?P<poll_id>[0-9]+)$', polls_view, name='poll'),
    url(r'^polls/(?P<poll_id>[0-9]+)/vote$', poll_vote_view, name='poll_vote'),
    url(r'^polls/(?P<poll_id>[0-9]+)/results$', poll_results_view, name='poll_results'),
]


"""Onionr - Private P2P Communication.

DBCreator, creates sqlite3 databases used by Onionr
"""
"""
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
"""
import sqlite3, os
from coredb import dbfiles
import filepaths

def createAddressDB():
    '''
        Generate the address database

        types:
            1: I2P b32 address
            2: Tor v2 (like facebookcorewwwi.onion)
            3: Tor v3
    '''
    if os.path.exists(dbfiles.address_info_db):
        raise FileExistsError("Address database already exists")
    conn = sqlite3.connect(dbfiles.address_info_db)
    c = conn.cursor()
    c.execute('''CREATE TABLE adders(
        address text,
        type int,
        knownPeer text,
        speed int,
        success int,
        powValue text,
        failure int,
        lastConnect int,
        lastConnectAttempt int,
        trust int,
        introduced int
        );
    ''')
    conn.commit()
    conn.close()

def createPeerDB():
    '''
        Generate the peer sqlite3 database and populate it with the peers table.
    '''
    if os.path.exists(dbfiles.user_id_info_db):
        raise FileExistsError("User database already exists")
    # generate the peer database
    conn = sqlite3.connect(dbfiles.user_id_info_db)
    c = conn.cursor()
    c.execute('''CREATE TABLE peers(
        ID text not null,
        name text,
        adders text,
        dateSeen not null,
        trust int,
        hashID text);
    ''')
    c.execute('''CREATE TABLE forwardKeys(
    peerKey text not null,
    forwardKey text not null,
    date int not null,
    expire int not null
    );''')
    conn.commit()
    conn.close()
    return

def createBlockDB():
    '''
        Create a database for blocks

        hash         - the hash of a block
        dateReceived - the date the block was recieved, not necessarily when it was created
        decrypted    - if we can successfully decrypt the block (does not describe its current state)
        dataType     - data type of the block
        dataFound    - if the data has been found for the block
        dataSaved    - if the data has been saved for the block
        sig    - optional signature by the author (not optional if author is specified)
        author       - multi-round partial sha3-256 hash of authors public key
        dateClaimed  - timestamp claimed inside the block, only as trustworthy as the block author is
        expire int   - block expire date in epoch
    '''
    if os.path.exists(dbfiles.block_meta_db):
        raise FileExistsError("Block database already exists")
    conn = sqlite3.connect(dbfiles.block_meta_db)
    c = conn.cursor()
    c.execute('''CREATE TABLE hashes(
        hash text not null,
        dateReceived int,
        decrypted int,
        dataType text,
        dataFound int,
        dataSaved int,
        sig text,
        author text,
        dateClaimed int,
        expire int
        );
    ''')
    conn.commit()
    conn.close()
    return

def createBlockDataDB():
    if os.path.exists(dbfiles.block_data_db):
        raise FileExistsError("Block data database already exists")
    else:
        if not os.path.exists(filepaths.block_data_location):
            os.mkdir(filepaths.block_data_location)
    conn = sqlite3.connect(dbfiles.block_data_db)
    c = conn.cursor()
    c.execute('''CREATE TABLE blockData(
        hash text not null,
        data blob not null
        );
    ''')
    conn.commit()
    conn.close()

def createForwardKeyDB():
    '''
        Create the forward secrecy key db (*for *OUR* keys*)
    '''
    if os.path.exists(dbfiles.forward_keys_db):
        raise FileExistsError("Block database already exists")
    conn = sqlite3.connect(dbfiles.forward_keys_db)
    c = conn.cursor()
    c.execute('''CREATE TABLE myForwardKeys(
        peer text not null,
        publickey text not null,
        privatekey text not null,
        date int not null,
        expire int not null
        );
    ''')
    conn.commit()
    conn.close()
    return

def create_blacklist_db():
    if os.path.exists(dbfiles.blacklist_db):
        raise FileExistsError("Blacklist db already exists")
    conn = sqlite3.connect(dbfiles.blacklist_db, timeout=10)
    c = conn.cursor()
    # Create table
    c.execute('''CREATE TABLE blacklist(
            hash text primary key not null,
            dataType int,
            blacklistDate int,
            expire int
            );
        ''')
    conn.commit()
    conn.close()

create_funcs = [createAddressDB, createPeerDB,
                createBlockDB, createBlockDataDB,
                createForwardKeyDB, create_blacklist_db]


import pandas as pd
import numpy as np
from interfacepoints import interface_points
from vectorizedangles import all_angles,prob_dists

bike_vector_df = pd.read_csv("/Users/noahwiley/Documents/Bike UROP/MeasureML-main/Frame Datasets/bike_vector_df_with_id.csv")
# Col with down tube length
#print("bike_vector_df\n", bike_vector_df)
without_id_array = bike_vector_df.iloc[:, 2:].values
#print("without_id_array", without_id_array)
int_point_array = interface_points(without_id_array)
int_point_df = pd.DataFrame(columns=["hand_x", "hand_y", "seat_x", "seat_y", "crank_length"])
for i, col in enumerate(["hand_x", "hand_y", "seat_x", "seat_y", "crank_length"]):
    int_point_df[col] = int_point_array[:, i]
print("int_point_df\n", int_point_df)
combined_df = bike_vector_df.merge(int_point_df, left_index=True, right_index=True, how="inner")

combined_df.to_csv("/Users/noahwiley/Documents/Bike UROP/MeasureML-main/Frame Datasets/bikes_with_int_points.csv")

print(combined_df[np.isnan(combined_df["hand_x"])])


import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import yfinance as yf
from dataset import SlidingWindowTransformer
from models.lstm import LSTMForecaster
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from training_arguments import TrainingArguments

AMZN = yf.download('AMZN', start='2013-01-01', end='2019-12-31', progress=False)
all_data = AMZN[['Adj Close', 'Open', 'High', 'Low', "Close", "Volume"]].round(2)

train_df, test_df = train_test_split(all_data, test_size=0.2, shuffle=False)

input_cols = ['Adj Close', 'Open', 'High', 'Low', "Close", "Volume"]
output_cols = ['Adj Close']

X_train, y_train = train_df[input_cols], train_df[output_cols]
X_test, y_test = test_df[input_cols], test_df[output_cols]

window_size = 24
forecast_size = 1
step_size = 1

lstm = LSTMForecaster(
    window_size=window_size,
    forecast_size=forecast_size,
    hidden_size=128,
    num_layers=1,
    in_features=len(input_cols),
    out_features=len(output_cols),
    training_args=TrainingArguments(
        criterion=nn.MSELoss,
        optimizer=torch.optim.Adam,
        lr=0.003,
        max_epochs=150,
        batch_size=32,
        device='cuda',
    ),
)

model = Pipeline(
    steps=[
        ('scaler', StandardScaler()),
        ('slding', SlidingWindowTransformer(window_size=window_size, forecast_size=forecast_size, step_size=step_size)),
        ('lstm', lstm),
    ]
)
model.fit(X=X_train, y=y_train)
y_pred = model.predict(X=X_test)
_, y_true = model['slding'].transform(X=None, y=y_test)

mse_list = [mean_squared_error(true, pred) for true, pred in zip(y_true, y_pred)]
average_mse = np.mean(mse_list)

print(average_mse)
# loss = F.mse_loss(torch.from_numpy(y_pred), torch.from_numpy(y_true)).item()
# print(loss)


# -*- coding: utf-8 -*-
"""
Created on Thu Aug  4 14:59:48 2022

@author: BEMC
"""

import os
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from hmtoolbox.WB_SWAN import SWAN_read_tab
from hmtoolbox.WB_basic import list_files_folders
from hmtoolbox.WB_basic import save_plot

# results SWAN 1D
path_results_1D = r'z:\130991_Systeemanalyse_ZSS\3.Models\SWAN\1D\Waddenzee\tests\test_04_veg_layers'
files = list_files_folders.list_files('.TAB',path_results_1D)

# input SWAN 1D
path_input = r'z:\130991_Systeemanalyse_ZSS\3.Models\SWAN\1D\Waddenzee\tests\test_04_veg_layers\input'
file_input = r'SWAN2D_output_WZ_6004026_HRext03.csv'

# results SWAN 2D
path_results_2D = r'z:\130991_Systeemanalyse_ZSS\3.Models\SWAN\1D\Waddenzee\tests\test_04_veg_layers\input'
file_output_2D = 'output_batch_03_1D_input_6004026_HRext02.xlsx'

# output path
output_path = r'z:\130991_Systeemanalyse_ZSS\3.Models\SWAN\1D\Waddenzee\tests\test_04_veg_layers'

save_fig = True

Xp_comp= 343
Xpteen = 43
Xp_basis = 99.8

#%% load file with simulation input

df_input = pd.read_csv(os.path.join(path_input, file_input), sep=';',dtype={'ZSS-scenario':str})

# df_swan2d = pd.read_csv(os.path.join(path_results_2D, file_output_2D), sep=';',dtype={'ZSS-scenario':str})

#%% loop trough simulations and plot results

for file in files:

    scene = file.split('\\')[-3]
    simulation = file.split('\\')[-2]

    Hs_ref = df_input[df_input['Scenario']==scene]['HS'].iloc[0]
    Tp_ref = df_input[df_input['Scenario']==scene]['TP'].iloc[0]

    data, headers = SWAN_read_tab.Freadtab(file)

    data['Hsig'][data['Hsig']<=0] = np.nan
    data['Hsig'][data['Hsig']==0] = np.nan
    data['Tm_10'][data['Tm_10']<=0] = np.nan
    data['TPsmoo'][data['TPsmoo']<=0] = np.nan
    data['Wlen'][data['Wlen']<=0] = np.nan
    data['Botlev'][data['Botlev']<-20] = -10
    Wlen = float(data['Lwavp'].iloc[-1])

    #%% Determine location toe of dike
    # ii = 0
    # slope = list()
    # Xpteen = data['Xp'].iloc[-1]
    # Ypteen = data['Yp'].iloc[-1]
    # for x1, x2, y1, y2 in zip(data['Xp'][1:-1], data['Xp'][:-2], data['Botlev'][1:-1], data['Botlev'][:-2]):
    #     dx = x1 - x2
    #     dy = y1 - y2
    #     if dy == 0:
    #         dydx = 0
    #     else:
    #         dydx = dy/dx
    #         if dydx >= 1/60 and ii == 0:
    #             Xpteen = x1
    #             Ypteen = y2
    #             ii = ii +1
    #     slope.append(dydx)

    # # max_slope = max(slope)
    # # imax = np.argmax(slope)

    Xpteen = Xpteen

    #%% wave parameters at incoming boundary
    Xpin = data['Xp'].iloc[-1]
    Hs_in = data['Hsig'].iloc[-1]
    Tm10_in = data['Tm_10'].iloc[-1]

    #%% Get output at output location (1/2 wavelength from toe of dike)
    Xpout = float(Xpteen) + Wlen*0.5
    Ypout = 0
    Hs_out = data['Hsig'][data['Xp'] > Xpout].iloc[0]
    Tm10_out = data['Tm_10'][data['Xp'] > Xpout].iloc[0]

    # maximum values for ylimits
    Hs_max = max(data['Hsig'])
    Tm10_max = max(data['Tm_10'])

    #%% get output at SWAN 2D location
    Hs_comp = data['Hsig'][data['Xp'] >= Xp_comp].iloc[0]
    Tm10_comp = data['Tm_10'][data['Xp'] >= Xp_comp].iloc[0]

    #%% plotting
    fig = plt.figure(figsize=(8,7))
    ax1 = plt.subplot(2,1,1)
    ax1_copy = ax1.twinx()

    ax1.plot(data['Xp'],-data['Botlev'],'k', linewidth = 3, label = 'bodem')
    ax1.plot(data['Xp'], data['Watlev'], 'b-', linewidth = 1.5, label = 'waterstand')
    ax1.axvline(x = Xpteen, color = 'k', linestyle='--', label = 'teen')
    ax1.axvline(x = Xpout, color = 'r', linestyle='--', label = 'teen + 1/2*L')
    ax1.axvline(x = Xp_comp, color = 'tab:orange', linestyle='--', label = 'teen + 300m')
    ax1.axvline(x = Xp_basis, color = 'y', linestyle='--', label = 'HRbasis')
    ax1.set_ylabel('hoogte [m+NAP]')
    ax1.set_xlabel('afstand [m]')
    ax1.legend(loc = 'lower right')
    # ax1.set_xlim(30,100)
    # ax1.set_ylim(-20,10)

    ax1_copy.plot(data['Xp'], data['Hsig'],'g', linewidth = 1.5, label = '$H_s$ [m]')
    ax1_copy.set_ylabel('$H_s$ [m]',color='g')
    ax1_copy.tick_params(labelcolor='g')
    ax1_copy.text(Xpin,Hs_in,f'Hs = {Hs_in:.2f} m',color='g',fontweight = 'bold')
    ax1_copy.text(Xpout+10,Hs_out,f'Hs = {Hs_out:.2f} m',color='r',fontweight = 'bold')
    ax1_copy.text(Xp_comp+10,Hs_comp,f'Hs = {Hs_comp:.2f} m',color='tab:orange',fontweight = 'bold')
    plt.title(f'{scene}\n{simulation}\n Hs = {Hs_ref:.2f} m, Tp = {Tp_ref:.2f} s')
    ax1_copy.legend(loc = 'center right')
    ax1_copy.set_ylim(0,np.ceil(Hs_max))

    ax2 = plt.subplot(2,1,2)
    ax2_copy = ax2.twinx()
    ax2.plot(data['Xp'],-data['Botlev'],'k', linewidth = 3, label = 'bodem')
    ax2.plot(data['Xp'], data['Watlev'], 'b-', linewidth = 1.5, label = 'waterstand')
    ax2.axvline(x = Xpteen, color = 'k', linestyle='--', label = 'teen')
    ax2.axvline(x = Xpout, color = 'r', linestyle='--', label = 'teen + 1/2*L')
    ax2.axvline(x = Xp_comp, color = 'tab:orange', linestyle='--', label = 'teen + 300m')
    ax2.axvline(x = Xp_basis, color = 'y', linestyle='--', label = 'HRbasis')
    ax2.set_ylabel('hoogte [m+NAP]')
    ax2.set_xlabel('afstand [m]')
    ax2.legend(loc = 'lower right')
    # ax2.set_xlim(30,100)
    # ax2.set_ylim(-20,10)

    # ax2_copy.plot(data['Xp'], data['Tm_10'],color='orange')
    ax2_copy.plot(data['Xp'], data['Tm_10'],'m', linewidth = 1.5,label = '$T_m-1,0$ [m]')
    ax2_copy.set_ylabel('$H_s$ [m]',color='m')
    ax2_copy.tick_params(labelcolor='m')
    ax2_copy.text(Xpin,Tm10_in,f'Tm_10 = {Tm10_in:.2f} s',color='m',fontweight = 'bold')
    ax2_copy.text(Xpout+10,Tm10_out,f'Tm_10 = {Tm10_out:.2f} s',color='r',fontweight = 'bold')
    ax2_copy.text(Xp_comp+10,Tm10_comp,f'Tm_10 = {Tm10_comp:.2f} s',color='tab:orange',fontweight = 'bold')
    ax2_copy.set_ylabel('$T_{m-1.0}$ [s]')
    ax2_copy.legend(loc = 'center right')
    ax2_copy.set_ylim(0,np.ceil(Tm10_max))

    if save_fig:
        save_name = os.path.join(output_path, scene+'_'+simulation+'.png')
        save_plot.save_plot(fig,save_name,ax = ax1_copy, dx = -0.05)


import random
import os

# =================== variaveis de inicio =========================
lista_itens = [1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8]
lista_resultado = random.sample(lista_itens, 16)

#l --- linhas apresentadas ---
linha_1 = ['x', 'x', 'x', 'x']
linha_2 = ['x', 'x', 'x', 'x']
linha_3 = ['x', 'x', 'x', 'x']
linha_4 = ['x', 'x', 'x', 'x']

# ----- linhas gabarito --------
linhaf_1 = lista_resultado[0:4]
linhaf_2 = lista_resultado[4:8]
linhaf_3 = lista_resultado[8:12]
linhaf_4 = lista_resultado[12:]

# ========functions ========
def imprimir_quadro(line_1 = linha_1, line_2 = linha_2, line_3 = linha_3, line_4 = linha_4):
    print('='  * 30)
    lista_linhas = [line_1, line_2, line_3, line_4]
    for lista in lista_linhas:
        print('')
        i = 0
        print('| ', end = '')
        while i < 4:
            print(lista[i], '', end = '')
            i += 1
        print('|', end = '')
    print('\n')
    print('='  * 30)

def pegar_posicao():
    #pega posição
    def main():
        valid = False
        while not valid:
            x = validar_inteiro('linha ')
            y = validar_inteiro('coluna')
            valid = verificar_posicao(x, y)

        posicao = [x, y]
        return posicao

    def validar_inteiro(texto):  #valida as entradas
        valid = False
        while not valid:
            try:
                x = int(input(f'Digite um valor para a {texto}: '))
            except ValueError:
                print('\nValor inválido. Digite novamente')
            else:
                return x

    def verificar_posicao(x, y): #verificar se posição é válida
        if ((x != 0) and (x != 1) and (x != 2) and (x != 3)) or ((y != 0) and (y != 1) and (y != 2) and (y != 3)):
            print('posição inválida')
            return False
        else:
            if x == 0:
                if linha_1[y] != 'x':
                    print('Célula já revelada. Digite uma posição válida.')
                    return False
                else:
                    return True

            elif x == 1:
                if linha_2[y] != 'x':
                    print('Célula já revelada. Digite uma posição válida.')
                    return False
                else:
                    return True
            elif y == 2:
                if linha_3[y] != 'x':
                    print('Célula já revelada. Digite uma posição válida.')
                    return False
                else:
                    return True
            elif x == 3:
                if linha_4[y] != 'x':
                    print('Célula já revelada. Digite uma posição válida.')
                    return False
                else:
                    return True
            else:
                return True

    return main()

def clearConsole():
    command = 'clear'
    if os.name in ('nt', 'dos'):  # If Machine is running on Windows, use cls
        command = 'cls'
    os.system(command)


class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """

        res  = ''
        for i in digits:
            res  =  res + str(i)

        print(res,type(res))
        res = int(res)+1
        print(res,type(res))
        res = [int(x) for x in str(res)]
        print(res)

digits =[1,2,3]
digits= [9,9]
obj = Solution().plusOne(digits)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Anthony J. Vasquez
email: avasque1@jh.edu
phone: 315-920-8877

Class: Introduction to Machine Learning

Description: Naive classifier that output the majority class for any test input
"""
import pandas as pd

class NaiveClassifier:

    '''Test algorthm to predict majority class'''

    def __init__(self, X_train, y_train, test_set_len):
        print('\nNaive Classifier Initialized')
        self.X_train = X_train
        self.y_train = y_train
        self.test_set_len = test_set_len

    def model(self):
        class_cnt_dict = {'class': [], 'count': []}
        classes = list(set(self.y_train))
        for clss in classes:
            cnt = 0
            for sample in self.y_train:
                if sample == clss:
                    cnt+=1
            class_cnt_dict['class'].append(clss)
            class_cnt_dict['count'].append(cnt)

        max_num = max(class_cnt_dict['count'])
        index = class_cnt_dict['count'].index(max_num)
        max_class = class_cnt_dict['class'][index]

        output_list = []
        for i in range(self.test_set_len):
            output_list.append(max_class)

        list_df = pd.DataFrame(output_list)
        return list_df


import numpy as np
import pandas as pd
import os

landmarks = [
        'nose',
        'left_eye_inner', 'left_eye', 'left_eye_outer',
        'right_eye_inner', 'right_eye', 'right_eye_outer',
        'left_ear', 'right_ear',
        'mouth_left', 'mouth_right',
        'left_shoulder', 'right_shoulder',
        'left_elbow', 'right_elbow',
        'left_wrist', 'right_wrist',
        'left_pinky_1', 'right_pinky_1',
        'left_index_1', 'right_index_1',
        'left_thumb_2', 'right_thumb_2',
        'left_hip', 'right_hip',
        'left_knee', 'right_knee',
        'left_ankle', 'right_ankle',
        'left_heel', 'right_heel',
        'left_foot_index', 'right_foot_index',
    ]

index_from_landmark = {name: i for i,name in enumerate(landmarks)}

def rescaleEntry(name, x, zero, scale):
    if name.startswith('x'):
        return (x - zero[0]) / scale
    elif name.startswith('y'):
        return (x - zero[1]) / scale
    elif name.startswith('z'):
        return (x - zero[2]) / scale
    else:
        return x

def rescale(x):
    ## nose is 0
    nose = np.array([coord for name, coord in x.items() if name[1:] == str(index_from_landmark['nose'])])
    leftEye = np.array([coord for name, coord in x.items() if name[1:] == str(index_from_landmark['left_eye_outer'])])
    rightEye = np.array([coord for name, coord in x.items() if name[1:] == str(index_from_landmark['right_eye_outer'])])
    scale = np.linalg.norm(leftEye - rightEye)
    return {(name + "_rescaled" if name[0] in ['x', 'y', 'z'] else name) : rescaleEntry(name, coord, nose, scale) for name, coord in x.items()}

def lightenUp(x, byFactor=2):
    return np.add(x, np.divide((255 - x), byFactor)).astype(int)

def kdenLiveTimeToFrame(time, fps=15):
    secs = time.split(":")
    return int(secs[0]) * 60 + int(secs[1]) * fps + int(secs[2])

def rescaleDf(df):
    df_rescaled_list = list()
    for i, row in df.iterrows():
        df_rescaled_list.append(rescale(row))
    return pd.DataFrame(df_rescaled_list).drop(["x0_rescaled", "y0_rescaled", "z0_rescaled"], axis=1)

def generateDataFromAnnotation(df, anno, batch_size=4, timeToFrame=kdenLiveTimeToFrame, asDict=True):
    result = dict()
    onlyCoordinateColumns = [c for c in df.columns if c[0] in ['x', 'y', 'z']]
    for name, speclist in anno.items():
        # print(name)
        result_list = list()
        for specs in speclist:
            fr = timeToFrame(specs["from"])
            to = timeToFrame(specs["till"])
            till = to - batch_size + 1
            if till < fr:
                continue
            for i in range(fr, till + 1):
                dat = df.loc[(fr + i):(fr + i + batch_size), onlyCoordinateColumns]
                result_list.append(dat.to_numpy().flatten())
        result[name] = np.array(result_list)
    if asDict:
        return result
    return np.vstack(tuple(result.values()))

def loadData(fileName = "MariaMovementSequence_xyz_27Sept", fps=15):
    df = pd.read_csv(
        os.path.join("..","data", "csv", fileName + ".csv"),
        header=0,
        index_col=0)
    df["time"] = 1000 * df.index / fps
    return df

def turnDictDataIntoArray(dictData):
    return np.vstack(tuple(dictData.values()))


# Main script for experimenting with training on a subgraph
from collections import Counter
import argparse
import numpy as np
import sys
import os
import json
import time
import random

import torch
import torch.nn as nn

from model import LinkPredictor
from reader import AtomicTSVReader, ConceptNetTSVReader, FB15kReader
import utils
import reader_utils
import evaluation_utils

torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False

def set_seeds(seed):
    random.seed(42)
    np.random.seed(42)
    torch.manual_seed(42)
    torch.cuda.manual_seed(42)

def load_data(dataset, reader_cls, data_dir, sim_relations):
    train_network = reader_cls(dataset)
    dev_network = reader_cls(dataset)
    test_network = reader_cls(dataset)

    train_network.read_network(data_dir=data_dir, split="train")
    train_network.print_summary()
    node_list = train_network.graph.iter_nodes()
    node_degrees = [node.get_degree() for node in node_list]
    degree_counter = Counter(node_degrees)
    avg_degree = sum([k * v for k, v in degree_counter.items()]) / sum([v for k, v in degree_counter.items()])
    print("Average Degree: ", avg_degree)

    dev_network.read_network(data_dir=data_dir, split="valid", train_network=train_network)
    test_network.read_network(data_dir=data_dir, split="test", train_network=train_network)

    word_vocab = train_network.graph.node2id

    # Add sim nodes
    if sim_relations:
        print("Adding sim edges..")
        train_network.add_sim_edges_bert()

    train_data, _ = reader_utils.prepare_batch_dgl(word_vocab, train_network, train_network)
    test_data, test_labels = reader_utils.prepare_batch_dgl(word_vocab, test_network, train_network)
    valid_data, valid_labels = reader_utils.prepare_batch_dgl(word_vocab, dev_network, train_network)

    return train_data, valid_data, test_data, valid_labels, test_labels, train_network

def get_model_name(args):

    name = '_subgraph_model_state.pth'
    name = "_" + args.gcn_type + "_" + args.decoder + name

    if args.sim_relations:
        name = "_sim_relations" + name

    if args.sim_sim:
        name = "_sim-sim" + name

    if args.bert_concat:
        name = "_bert_concat" + name

    if args.bert_mlp:
        name = "_bert_mlp" + name

    if args.tying:
        name = "_tying" + name

    if args.bert_sum:
        name = "_bert_sum" + name

    if args.input_layer == "bert":
        name = "_inp-bert" + name

    model_state_file = args.dataset + name
    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)
    model_state_file = os.path.join(args.output_dir, model_state_file)

    return model_state_file

def main(args):
    set_seeds(args.seed)

    # load graph data
    if args.dataset == "FB15K-237":
        dataset_cls = FB15kReader
        data_dir = "data/FB15k-237/"
    elif args.dataset == "atomic":
        dataset_cls = AtomicTSVReader
        data_dir = "data/atomic/"
    elif args.dataset == "conceptnet":
        dataset_cls = ConceptNetTSVReader
        data_dir = "data/ConceptNet/"
    else:
        raise ValueError("Invalid option for dataset.")

    # Store entity-wise dicts for filtered metrics
    train_data, valid_data, test_data, valid_labels, test_labels, train_network = load_data(args.dataset,
                                                                                dataset_cls,
                                                                                data_dir,
                                                                                args.sim_relations)
    num_nodes = len(train_network.graph.nodes)
    num_rels = len(train_network.graph.relations)
    all_tuples = train_data.tolist() + valid_data.tolist() + test_data.tolist()

    # for filtered ranking
    all_e1_to_multi_e2, all_e2_to_multi_e1 = reader_utils.create_entity_dicts(all_tuples, num_rels, args.sim_relations)

    # for training
    train_e1_to_multi_e2, train_e2_to_multi_e1 = reader_utils.create_entity_dicts(train_data.tolist(), num_rels,
                                                                                args.sim_relations)
    # the below dicts `include` sim relations
    sim_train_e1_to_multi_e2, sim_train_e2_to_multi_e1 = reader_utils.create_entity_dicts(train_data.tolist(), num_rels)

    # check cuda
    use_cuda = torch.cuda.is_available()
    if use_cuda and not args.no_cuda:
        torch.cuda.set_device(args.gpu)

    cpu_decoding = args.cpu_decoding

    # atomic graph is much larger, so we perform evaluation on cpu
    cpu_eval = True if args.dataset == "atomic" else False

    # create model
    model = LinkPredictor(num_nodes,
                          num_rels,
                          args,
                          use_cuda=use_cuda)

    # build graph
    graph_train_data = train_data
    test_graph, test_rel, test_norm = utils.build_test_graph(num_nodes, num_rels, graph_train_data)
    test_deg = test_graph.in_degrees(range(test_graph.number_of_nodes())).float().view(-1, 1)
    test_node_id = torch.arange(0, num_nodes, dtype=torch.long).view(-1, 1)
    test_rel = torch.from_numpy(test_rel).view(-1, 1)
    test_norm = torch.from_numpy(test_norm).view(-1, 1)

    # transfer graph data to gpu
    if use_cuda and not args.no_cuda and not cpu_decoding:
        test_node_id = test_node_id.cuda()
        test_norm = test_norm.cuda()
        test_rel = test_rel.cuda()

    # validation and testing triplets
    valid_data = torch.LongTensor(valid_data)
    test_data = torch.LongTensor(test_data)

    if use_cuda and not args.no_cuda and not cpu_eval:
        valid_data = valid_data.cuda()
        test_data = test_data.cuda()

    test_graph.ndata.update({'id': test_node_id, 'norm': test_norm})
    test_graph.edata['type'] = test_rel

    if use_cuda and not args.no_cuda:
        # model = nn.DataParallel(model, device_ids=[0,1])
        model = model.cuda()

    model_state_file = get_model_name(args)

    # writer = SummaryWriter("runs/" + model_state_file.replace(".pth",".log"))

    # check if only evaluation needs to be performed
    if args.eval_only:
        if args.load_model:
            model_state_file = args.load_model
        else:
            print("Please provide model path for evaluation (--load_model)")
            sys.exit(0)

        checkpoint = torch.load(model_state_file)

        if use_cuda and not args.no_cuda and cpu_eval:
            model.cpu()
            test_graph.ndata['id'] = test_graph.ndata['id'].cpu()
            test_graph.ndata['norm'] = test_graph.ndata['norm'].cpu()
            test_graph.edata['type'] = test_graph.edata['type'].cpu()
            model.decoder.no_cuda = True

        model.eval()
        model.load_state_dict(checkpoint['state_dict'])
        print(model)

        print("================DEV=================")
        mrr = evaluation_utils.ranking_and_hits(test_graph, model, valid_data, all_e1_to_multi_e2, train_network,
                                                fusion="graph-only", sim_relations=args.sim_relations,
                                                write_results=args.write_results, debug=args.debug)

        print("================TEST================")
        mrr = evaluation_utils.ranking_and_hits(test_graph, model, test_data, all_e1_to_multi_e2, train_network,
                                                fusion="graph-only", sim_relations=args.sim_relations, debug=args.debug)

        sys.exit(0)

    if os.path.isfile(model_state_file):
        print(model_state_file)
        overwrite = input('Model already exists. Overwrite? Y = yes, N = no\n')
        if overwrite.lower() == 'n':
            print("Quitting")
            sys.exit(0)
        elif overwrite.lower() != 'y':
            raise ValueError("Invalid Option")

    # build adj list and calculate degrees for sampling
    adj_list, degrees, sparse_adj_matrix, rel = utils.get_adj_and_degrees(num_nodes, num_rels, train_data)

    # remove sim edges from sampling_edge_ids (we sample from the original graph and then densify it)
    if args.sim_relations:
        sim_edge_ids = np.where(graph_train_data[:, 1] == num_rels - 1)[0]
        sampling_edge_ids = np.delete(np.arange(len(graph_train_data)), sim_edge_ids)
    else:
        sampling_edge_ids = None

    # optimizer
    optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)

    forward_time = []
    backward_time = []

    # training loop
    print("Starting training...")
    epoch = 0
    best_mrr = 0

    while True:
        model.train()
        epoch += 1

        cur_train_data = graph_train_data[:]

        # build dgl graph
        g, node_id, edge_type, node_norm, data, labels = \
            utils.generate_sampled_graph_and_labels(
                cur_train_data, args.graph_batch_size,
                num_rels, adj_list, degrees, args.negative_sample, args.sim_sim, args.sim_relations,
                sim_train_e1_to_multi_e2, sampling_edge_ids)

        node_id_copy = np.copy(node_id)
        node_dict = {v: k for k, v in dict(enumerate(node_id_copy)).items()}

        # set node/edge feature
        node_id = torch.from_numpy(node_id).view(-1, 1)
        edge_type = torch.from_numpy(edge_type)
        node_norm = torch.from_numpy(node_norm).view(-1, 1)

        if use_cuda and not args.no_cuda:
            node_id = node_id.cuda()
            edge_type, node_norm = edge_type.cuda(), node_norm.cuda()

        g.ndata.update({'id': node_id, 'norm': node_norm})
        g.edata['type'] = edge_type

        batch_size = args.decoder_batch_size
        e1_keys = list(train_e1_to_multi_e2.keys())
        sub_e1_keys = {}

        # Add inverse edges to training samples
        src, dst = np.concatenate((cur_train_data[:, 0], cur_train_data[:, 2])), \
                   np.concatenate((cur_train_data[:, 2], cur_train_data[:, 0]))
        rel = cur_train_data[:, 1]
        rel = np.concatenate((rel, rel + num_rels))
        cur_train_data = np.stack((src, rel, dst)).transpose()

        # The loop below constructs a dict for the decoding step
        # with the key (src, rel) and the value as the list of nodes present in the original graph
        # where the source and target nodes both belong to the list of sampled nodes in subgraph

        for e in cur_train_data:
            rel = e[1]
            # Don't use sim relations for decoding
            if args.sim_relations:
                if rel == num_rels - 1 or rel == (num_rels * 2) - 1:
                    continue
                elif rel >= num_rels:
                    rel -= 1

            if e[0] in node_id_copy and e[2] in node_id_copy:
                subgraph_src_idx = node_dict[e[0]]
                subgraph_tgt_idx = node_dict[e[2]]
                if (subgraph_src_idx, rel) not in sub_e1_keys:
                    sub_e1_keys[(subgraph_src_idx, rel)] = [subgraph_tgt_idx]
                else:
                    sub_e1_keys[(subgraph_src_idx, rel)].append(subgraph_tgt_idx)

        key_list = list(sub_e1_keys.keys())

        random.shuffle(key_list)
        cum_loss = 0.0

        for i in range(0, len(key_list), batch_size):

            optimizer.zero_grad()

            # compute graph embeddings
            graph_embeddings = model.get_graph_embeddings(g, epoch)
            #model.decoder.module.cur_embedding = graph_embeddings
            model.decoder.cur_embedding = graph_embeddings

            batch = key_list[i: i + batch_size]

            # Don't train with batches of size 1 and always set batch_size > 1 since batch norm
            # fails with batch_size=1
            if len(batch) == 1:
                continue

            e1 = torch.LongTensor([elem[0] for elem in batch])
            rel = torch.LongTensor([elem[1] for elem in batch])

            # e2 -> list of target nodes in subgraph
            e2 = [sub_e1_keys[(k[0], k[1])] for k in batch]
            batch_len = len(batch)

            if use_cuda and not args.no_cuda and not cpu_decoding:
                target = torch.cuda.FloatTensor(batch_len, node_id_copy.shape[0]).fill_(0)
                e1 = e1.cuda()
                rel = rel.cuda()
            else:
                target = torch.zeros((batch_len, node_id_copy.shape[0]))

            # construct target tensor
            for j, inst in enumerate(e2):
                target[j, inst] = 1.0

            # perform label smoothing
            target = ((1.0 - args.label_smoothing_epsilon) * target) + (1.0 / target.size(1))

            if cpu_decoding:
                graph_embeddings = graph_embeddings.cpu()
                model.decoder.cpu()
                model.decoder.no_cuda = True

            t0 = time.time()

            loss = model.get_score(e1, rel, target, graph_embeddings)
            loss = torch.mean(loss)
            cum_loss += loss.cpu().item()
            t1 = time.time()
            loss.backward()
            torch.nn.utils.clip_grad_norm_(model.parameters(), args.grad_norm)  # clip gradients
            optimizer.step()

            t2 = time.time()

            forward_time.append(t1 - t0)
            backward_time.append(t2 - t1)

            del graph_embeddings, target, batch, loss, e1, rel, e2

            # the below make training very slow
            # gc.collect()
            # torch.cuda.empty_cache()

        print("Epoch {:04d} | Loss {:.4f} | Best MRR {:.4f} | Forward {:.4f}s | Backward {:.4f}s".
              format(epoch, cum_loss, best_mrr, forward_time[-1], backward_time[-1]))
        # writer.add_scalar('data/loss', cum_loss , epoch)

        # Save model every 100 epochs
        # if epoch + 1 % 100==0:
        #    print("saving current model..")
        #    torch.save({'state_dict': model.state_dict(), 'epoch': epoch},
        #                 model_state_file)

        # validation
        if epoch % args.evaluate_every == 0:
            # perform validation on CPU when full graph is too large
            if use_cuda and not args.no_cuda and cpu_eval:
                model.cpu()
                test_graph.ndata['id'] = test_graph.ndata['id'].cpu()
                test_graph.ndata['norm'] = test_graph.ndata['norm'].cpu()
                test_graph.edata['type'] = test_graph.edata['type'].cpu()
                model.decoder.no_cuda = True

            model.eval()
            print("start eval")

            print("===========DEV============")
            mrr = evaluation_utils.ranking_and_hits(test_graph, model, valid_data, all_e1_to_multi_e2,
                                                    train_network, fusion="graph-only", sim_relations=args.sim_relations,
                                                    debug=args.debug, epoch=epoch)

            # writer.add_scalar('data/mrr', mrr, epoch)

            # save best model
            # torch.save({'state_dict': model.state_dict(), 'epoch': epoch},
            #                model_state_file)
            if mrr < best_mrr:
                if epoch >= args.n_epochs:
                    break
            else:
                best_mrr = mrr
                print("[saving best model so far]")
                torch.save({'state_dict': model.state_dict(), 'epoch': epoch},
                           model_state_file)

            metrics = {"best_mrr": best_mrr,
                       "cum_loss": cum_loss
                       }

            with open(os.path.join(args.output_dir, 'metrics.json'), 'w') as f:
                f.write(json.dumps(metrics))

            # transfer graph back to gpu device
            if use_cuda and not args.no_cuda and cpu_eval:
                model.cuda()
                test_graph.ndata['id'] = test_graph.ndata['id'].cuda()
                test_graph.ndata['norm'] = test_graph.ndata['norm'].cuda()
                test_graph.edata['type'] = test_graph.edata['type'].cuda()
                model.decoder.no_cuda = False

    print("training done")
    print("Mean forward time: {:4f}s".format(np.mean(forward_time)))
    print("Mean Backward time: {:4f}s".format(np.mean(backward_time)))

    # writer.export_scalars_to_json("./all_scalars.json")
    # writer.close()

    print("\nStart testing")

    # use best model checkpoint
    checkpoint = torch.load(model_state_file)
    model.eval()
    model.load_state_dict(checkpoint['state_dict'])
    print("Using best epoch: {}".format(checkpoint['epoch']))

    evaluation_utils.ranking_and_hits(test_graph, model, test_data, all_e1_to_multi_e2, train_network, fusion="graph-only",
                                      sim_relations=args.sim_relations)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Options for Commonsense Knowledge Base Completion')

    # General
    parser.add_argument("-d", "--dataset", type=str, required=True,
                        help="dataset to use")
    parser.add_argument("--sim_relations", action='store_true', default=False,
                        help="add similarity edges when constructing graph")
    parser.add_argument("--sim_sim", action='store_true', default=False,
                        help="add sim-sim edges to graph")
    parser.add_argument("--load_model", type=str, default=None, help="Path to model file")
    parser.add_argument("--decoder", type=str, default='ConvTransE', help="decoder used to compute scores")
    parser.add_argument("--n-epochs", type=int, default=200,
                        help="number of minimum training epochs")
    parser.add_argument("--evaluate-every", type=int, default=10,
                        help="perform evaluation every n epochs")
    parser.add_argument("--output_dir", type=str, required=False, default="saved_models",
                        help="output directory to store metrics and model file")
    parser.add_argument("--bert_concat", action='store_true', default=False,
                        help="concat bert embeddings before decoder layer")
    parser.add_argument("--bert_sum", action='store_true', default=False,
                        help="sum bert embeddings before decoder layer")
    parser.add_argument("--bert_mlp", action='store_true', default=False,
                        help="use mlp after concatenated bert+gcn embeddings before decoder layer")
    parser.add_argument("--tying", action='store_true', default=False,
                        help="tie input bert layer to gcn with concatenated tensor before decoding")
    parser.add_argument("--cpu_decoding", action='store_true', default=False,
                        help="perform decoding on cpu")
    parser.add_argument("--eval_only", action='store_true', default=False,
                        help="only evaluate using an existing model")
    parser.add_argument("--write_results", action='store_true', default=False,
                        help="write top-k candidate tuples for evaluation set to file")
    parser.add_argument("--eval-batch-size", type=int, default=500,
                        help="batch size when evaluating")
    parser.add_argument("--gpu", type=int, default=-1,
                        help="gpu")
    parser.add_argument("--no_cuda", action='store_true', default=False,
                        help="prevents using cuda")
    parser.add_argument("--seed", type=int, default=42,
                        help="random seed value")
    parser.add_argument("--debug", action='store_true', default=False,
                        help="use fewer eval instances in debugging mode")

    # GCN
    parser.add_argument("--init_embedding_dim", type=int, default=200,
                        help="embedding dimension of input to GCN")
    parser.add_argument("--input_layer", type=str, default="lookup",
                        help="initialization layer for GCN")
    parser.add_argument("--n-bases", type=int, default=100,
                        help="number of weight blocks for each relation (for RGCN)")
    parser.add_argument("--n-layers", type=int, default=2,
                        help="number of propagation rounds")
    parser.add_argument("--gcn_type", type=str, default="WGCNAttentionLayer",
                        help="type of GCN to be used (class name)")

    # Miscellaneous Hyperparameters
    parser.add_argument("--lr", type=float, default=1e-4,
                        help="learning rate")
    parser.add_argument("--dropout", type=float, default=0.2,
                        help="dropout probability")
    parser.add_argument("--input_dropout", type=float, default=0.2,
                        help="input dropout")
    parser.add_argument("--feature_map_dropout", type=float, default=0.2,
                        help="feature map dropout")
    parser.add_argument("--label_smoothing_epsilon", type=float, default=0.1,
                        help="epsilon for performing label smoothing")
    parser.add_argument("--embedding_dim", type=int, default=200,
                        help="output embedding dimension of GCN")
    parser.add_argument("--n-hidden", type=int, default=200,
                        help="number of hidden units")
    parser.add_argument("--use_bias", action='store_true', default=False,
                        help="use bias")
    parser.add_argument("--regularization", type=float, default=0.1,
                        help="regularization weight")
    parser.add_argument("--grad-norm", type=float, default=1.0,
                        help="norm to clip gradient to")
    parser.add_argument("--graph-batch-size", type=int, default=30000,
                        help="number of edges to sample in each iteration")
    parser.add_argument("--negative-sample", type=int, default=0,
                        help="number of negative samples per positive sample")
    parser.add_argument("--decoder_batch_size", type=int, default=128,
                        help="batch size for decoder")
    parser.add_argument("--layer_norm", action='store_true', default=False,
                        help="use layer normalization on embeddings fed to decoder")

    args = parser.parse_args()
    print(args)
    try:
        main(args)
    except KeyboardInterrupt:
        print('Interrupted')
        # writer.export_scalars_to_json("./all_scalars.json")
        # writer.close()