# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from rdflib import Graph, Namespace, Literal
from rdflib.namespace import OWL, RDF, RDFS
from rdflib.plugins.sparql import prepareQuery
import rdflib.plugins.sparql as sparql
import sys, os

aplication_path = os.path.dirname(sys.executable)

# Creation of pyqt5 app
App = QApplication(sys.argv)
win = QMainWindow()
central = QWidget()
win.setCentralWidget(central)

# Widgets used
list_widget = QListWidget()
list_widget2 = QListWidget()
button_accept = QPushButton()
button_return = QPushButton()
label = None

# RDFLib (SmartCity.Madrid)
g = Graph()
g.bind("owl", OWL)
g.bind("rdf", RDF)
g.bind("rdfs", RDFS)

dbo = Namespace("http://www.smartcity.madrid.es/mobility/")
g.bind("dbo", dbo)

data = "output-with-links.nt"
g.parse(data, format="nt")

class Window(QMainWindow):

    def __init__(self):
        super().__init__()

        # setting title
        self.setWindowTitle("Movilidad Madrid")

        # setting geometry
        self.setGeometry(100, 100, 500, 400)

        # calling the Main Window method (Menu)
        self.UiComponents()

        # showing all the widgets (Menu)
        self.show()

    # Second Screen (Once you press the button)
    def buttonReturn():
        window.show()
        win.close()

    # Second Screen (Once you press the button)
    def buttonClicked(_list_widget):

        # creating the widgets
        button_return = QPushButton(win)

        # setting geometry to the widgets
        button_return.setGeometry(30, 595, 135, 30)

        # button widget actions
        button_return.setText("Atrás")

        rows = _list_widget.selectedItems()
        row = rows[0].text()

        # variables for SPARQL queries
        selected = ""
        busList = []
        busListW = []
        lrList =[]
        lrListW =[]
        underList = []
        underListW = []

        # District that has been selected in the last Screen
        items = list_widget.selectedItems()
        for x in range(len(list_widget)):
            if (list_widget.item(x).isSelected()):
                selected = list_widget.item(x).text()

        # Queries To SmartCity.Madrid
        # Bus
        queryB = 'SELECT DISTINCT ?y ?w WHERE { ?y <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.smartcity.madrid.es/mobility/ontology/Bus>. ?y <http://www.smartcity.madrid.es/mobility/ontology/District_Name> "' + selected + '" . ?y <http://www.w3.org/2002/07/owl#sameAs>  ?w . }'
        queryBpq = sparql.prepareQuery(queryB)

        for bus in g.query(queryBpq):
            busList.append(bus.y)
            busListW.append(bus.w)

        # Light rail
        queryLR = 'SELECT DISTINCT ?y ?w WHERE { ?y <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.smartcity.madrid.es/mobility/ontology/LightMetro>. ?y <http://www.smartcity.madrid.es/mobility/ontology/District_Name> "' + selected + '" . ?y <http://www.w3.org/2002/07/owl#sameAs>  ?w . }'
        queryLRpq = sparql.prepareQuery(queryLR)

        for lightRail in g.query(queryLRpq):
            lrList.append(lightRail.y)
            lrListW.append(lightRail.w)

        # Metro
        queryM = 'SELECT DISTINCT ?y ?w WHERE { ?y <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.smartcity.madrid.es/mobility/ontology/Metro>. ?y <http://www.smartcity.madrid.es/mobility/ontology/District_Name> "' + selected + '" . ?y <http://www.w3.org/2002/07/owl#sameAs>  ?w . }'
        queryMpq = sparql.prepareQuery(queryM)

        for metro in g.query(queryMpq):
            underList.append(metro.y)
            underListW.append(metro.w)

        # SCROLLS for the results
        widgetM = QWidget(win)
        widgetB = QWidget(win)
        widgetLR = QWidget(win)
        voxM = QVBoxLayout(win)
        voxB = QVBoxLayout(win)
        voxLR = QVBoxLayout(win)
        scrollAreaM = QScrollArea(win)
        scrollAreaB = QScrollArea(win)
        scrollAreaLR = QScrollArea(win)

        # Metro Scroll
        txtResultQueryM = QLabel(win)
        voxM.addWidget(txtResultQueryM)
        for i in range(len(underList)):
            txtResultQueryM = QLabel(win)
            if i%2 == 0:
                txtResultQueryM.setText(""+ underList[i] +"")
                txtResultQueryM.setTextFormat(Qt.RichText)
                txtResultQueryM.setTextInteractionFlags(Qt.TextBrowserInteraction)
                txtResultQueryM.setOpenExternalLinks(True)

                voxM.addWidget(txtResultQueryM)

        # Bus Scroll
        txtResultQueryB = QLabel(win)
        voxB.addWidget(txtResultQueryB)
        for i in range(len(busList)):
            txtResultQueryB = QLabel(win)
            txtResultQueryB.setText(""+ busList[i] +"")
            txtResultQueryB.setTextFormat(Qt.RichText)
            txtResultQueryB.setTextInteractionFlags(Qt.TextBrowserInteraction)
            txtResultQueryB.setOpenExternalLinks(True)

            voxB.addWidget(txtResultQueryB)

        # LightRail Scroll
        txtResultQueryLR = QLabel(win)
        voxLR.addWidget(txtResultQueryLR)
        for i in range(len(lrList)):
            txtResultQueryLR = QLabel(win)
            if i%2 != 0:
                txtResultQueryLR.setText(""+ lrList[i] +"")
                txtResultQueryLR.setTextFormat(Qt.RichText)
                txtResultQueryLR.setTextInteractionFlags(Qt.TextBrowserInteraction)
                txtResultQueryLR.setOpenExternalLinks(True)

                voxLR.addWidget(txtResultQueryLR)

        widgetM.setLayout(voxM)
        widgetB.setLayout(voxB)
        widgetLR.setLayout(voxLR)

        # ScrollArea Metro
        scrollAreaM.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        scrollAreaM.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        scrollAreaM.setWidgetResizable(True)
        scrollAreaM.setGeometry(200, 80, 640, 150)
        scrollAreaM.setWidget(widgetM)

        # ScrollArea Bus
        scrollAreaB.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        scrollAreaB.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        scrollAreaB.setWidgetResizable(True)
        scrollAreaB.setGeometry(200, 80+190, 640, 150)
        scrollAreaB.setWidget(widgetB)

        # ScrollArea LightRail
        scrollAreaLR.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        scrollAreaLR.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        scrollAreaLR.setWidgetResizable(True)
        scrollAreaLR.setGeometry(200, 80+190+190, 640, 150)
        scrollAreaLR.setWidget(widgetLR)

        title = QLabel("Movilidad en el distrito " + selected, win)
        title.setGeometry(200, -13, 280, 80)
        img = QLabel(win)
        path = os.path.join(os.path.dirname(sys.modules[__name__].__file__), 'distritos_madrid.png')
        mapDistritos = QPixmap(path)
        img.setPixmap(mapDistritos)
        img.setGeometry(1, 100, 200, 200)
        titleM = QLabel("Metros:", win)
        titleM.setGeometry(220, 25, 280, 80)
        titleB = QLabel("Buses:", win)
        titleB.setGeometry(220, 25+190, 280, 80)
        titleLR = QLabel("Metros ligeros:", win)
        titleLR.setGeometry(220, 25+190+190, 280, 80)
        button_return.clicked.connect(lambda: Window.buttonReturn())
        win.show()
        win.setGeometry(100, 100, 860, 640)
        win.setWindowTitle('Movilidad Madrid')
        window.hide()

    # method for components
    def UiComponents(self):
        global list_widget
        global button_accept

        # creating the widgets
        list_widget = QListWidget(self)
        button_accept = QPushButton(self)

        # setting geometry to the widgets
        list_widget.setGeometry(50, 70, 190, 300)
        button_accept.setGeometry(260, 345, 135, 30)

        # button widget actions
        button_accept.setText("Elegir Distrito")

        # list widget items (Districts in order)
        centro = QListWidgetItem("Centro")
        arganzuela = QListWidgetItem("Arganzuela")
        retiro = QListWidgetItem("Retiro")
        salamanca = QListWidgetItem("Salamanca")
        chamartin = QListWidgetItem("Chamartin")
        tetuan = QListWidgetItem("Tetuan")
        chamberi = QListWidgetItem("Chamberi")
        fuencarral_elpardo = QListWidgetItem("Fuencarral-El Pardo")
        moncloa_aravaca = QListWidgetItem("Moncloa-Aravaca")
        latina = QListWidgetItem("Latina")
        carabanchel = QListWidgetItem("Carabanchel")
        usera = QListWidgetItem("Usera")
        puentedevallecas = QListWidgetItem("Puente de Vallecas")
        moratalaz = QListWidgetItem("Moratalaz")
        ciudadlineal = QListWidgetItem("Ciudad Lineal")
        hortaleza = QListWidgetItem("Hortaleza")
        villaverde = QListWidgetItem("Villaverde")
        villadevallecas = QListWidgetItem("Villa de Vallecas")
        vicalvaro = QListWidgetItem("Vicalvaro")
        sanblas_canillejas = QListWidgetItem("San blas-Canillejas")
        barajas = QListWidgetItem("Barajas")

        # adding items to the list widget
        list_widget.addItem(centro)
        list_widget.addItem(arganzuela)
        list_widget.addItem(retiro)
        list_widget.addItem(salamanca)
        list_widget.addItem(chamartin)
        list_widget.addItem(tetuan)
        list_widget.addItem(chamberi)
        list_widget.addItem(fuencarral_elpardo)
        list_widget.addItem(moncloa_aravaca)
        list_widget.addItem(latina)
        list_widget.addItem(carabanchel)
        list_widget.addItem(usera)
        list_widget.addItem(puentedevallecas)
        list_widget.addItem(moratalaz)
        list_widget.addItem(ciudadlineal)
        list_widget.addItem(hortaleza)
        list_widget.addItem(villaverde)
        list_widget.addItem(villadevallecas)
        list_widget.addItem(vicalvaro)
        list_widget.addItem(sanblas_canillejas)
        list_widget.addItem(barajas)

        # setting selection mode property
        list_widget.setSelectionMode(QAbstractItemView.SingleSelection)

        # creating a label
        label = QLabel("Distritos :", self)

        # setting geometry to the label
        label.setGeometry(50, 15, 280, 80)

        # making label multi line
        label.setWordWrap(True)
        button_accept.clicked.connect(lambda: Window.buttonClicked(list_widget))
        label.setWordWrap(True)
        ttl1 = QLabel("Movilidad en Madrid", self)
        ttl1.setGeometry(200, -13, 280, 80)
        img = QLabel(self)
        path = os.path.join(os.path.dirname(sys.modules[__name__].__file__), 'distritos_madrid.png')
        mapDistritos = QPixmap(path)
        img.setPixmap(mapDistritos)
        img.setGeometry(300, 100, 200, 200)

# create the instance of our Window
window = Window()

# start the app
sys.exit(App.exec())


#coding=utf-8
__author__ = 'CoderSong'
__date__ = '2019/4/16 21:36'

from django import forms
from .models import Comment

'''表单用来收集并向服务器提交用户输入的数据，当用户想要发表评论时，
    根据表单要求发表评论，之后点击评论按钮，这些数据就会发送给某个URL，
    每一个URL对应一个视图函数来处理用户通过表单提交的数据，
    如果合法，那么Django就会将其写入到数据库中，如果不合法，Django就会将错误信息返回给用户，要求用户重新输入合法数据格式
'''
class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        '''model = Comment 表明这个表单对应的数据库模型是 Comment 类
            fields指明表单要显示的字段
        '''
        fields = ['name','email','url','text']


def solution(m, n, board):
    answer = 0
    for row in range(len(board)) :
        board[row] = list(board[row])

    while True :
        index_list = []
        for i in range(m-1) :
            for j in range(n-1) :
                temp = {board[i][j],board[i][j+1],board[i+1][j],board[i+1][j+1]}
                if len(temp) == 1 and type(temp.pop()) == str:
                    index_list.append([i,j])
                    index_list.append([i,j+1])
                    index_list.append([i+1,j])
                    index_list.append([i+1,j+1])
        if index_list == [] :
            return answer
        redundancy = []
        for value in index_list:
            if value not in redundancy:
                redundancy.append(value)
        answer += len(redundancy)

        for change in redundancy :
            board[change[0]][change[1]] = 0
        for i in range(m-2,-1,-1) :
            for j in range(n) :
                if type(board[i][j]) == str :
                    k = i + 1
                    while k < m and board[k][j] == 0:
                        k += 1
                    pop = board[k-1][j]
                    board[k-1][j] = board[i][j]
                    board[i][j] = pop

프로그래머스/2level/잉규식 알고리즘/[1차] 프렌즈4블록.py


import csv
from bubble_sort_week2 import bubble_sort

def read_columns_from_csv (filename, column_name):

    # Given a csv file this function will read in defined column header and return the contents as a list.
    # : param list : filename : Path to the CSV file and column_name : Name of the target column
    # : return : a list from the specified column

    with open (filename , mode = 'r') as file:
        csv_file = csv.DictReader (file)
        column_data = []

        for row in csv_file:
            if column_name in row:
                column_data.append(int(row[column_name]))

    return (column_data)

def get_minimum_value (list):

    # Given a list of numbers as input this function will return the minimum value .
    # : param list : the list of numbers given as input
    # : return : the minimum value in the list

    minimum = list [0]
    for l in list:
        if l < minimum:
            minimum = l
    return minimum

def get_average (list) :

# Given a list of numbers as input this function will return the numerical average .
# : param list : the list of numbers given as input
# : return : the numerical average of the list

    total = 0
    for l in list :
        total += l

    average = total / len ( list )
    return average

def get_median_value ( list ) :

    # Given a list of numbers as input this function will return the median value .
    # : param list : the function bubble_sort and a list of numbers are given as inputs
    # : return : the median (middle) value of the list

    list1 = list.copy()
    bubble_sort ( list1 )
    median = list1 [ int ( len ( list1 ) /2) ]
    return median

def get_mode (list) :

    # Given a list of numbers as input this function will return the mode value (apears most often) .
    # : param list : a list of numbers are given as inputs
    # : return : the mode value of the list

    highest_freq = 0
    mode = scores [0]
    for score in scores :
        frequency = 0
        for score2 in scores :
            if score == score2 :
                frequency += 1
        if frequency > highest_freq :
            mode = score
            highest_freq = frequency
    return mode

def get_maximum_value ( list ) :

    # Given a list of numbers as input this function will return the maximum value .
    # : param list : the list of numbers given as input
    # : return : the maximum value in the list

    maximum = list [0]
    for l in list :
        if maximum > l :
            maximum = l
        return maximum

def get_sum_of_sq_diff(list):

    # Given a list of numbers and average of the numbers as inputs this function will return the sum of the squared difference.
    # : param list : the list of numbers and a function called "get_average" are given as inputs
    # : return : the sum of squared differences of the list

    mean = get_average(list)
    return sum((x - mean) ** 2 for x in list)

def get_variance(list):

    # Given a list of numbers and the sum of squared differences of the numbers as inputs this function will return the variance.
    # : param list : the list of numbers and a function called "get_sum_of_sq_diff" are given as inputs
    # : return : the variance of the list

    sum_squared_diff = get_sum_of_sq_diff(list)
    return sum_squared_diff / len(list)

def get_stddev(list):

    # Given a list of numbers and the variance of the numbers as inputs this function will return the standard deviation.
    # : param list : the list of numbers and a function called "get_variance" are given as inputs
    # : return : the standard deviation of the list

    variance = get_variance(list)
    return variance ** 0.5

if __name__ == "__main__" :

    csv_file_path = 'example.csv'
    column_name = 'Score'
    scores = read_columns_from_csv(csv_file_path, column_name)

    #scores = read_columns_from_csv ('example.csv')

    average = get_average (scores)
    minimum = get_minimum_value (scores)
    maximum = get_maximum_value (scores)
    median = get_median_value (scores)
    mode = get_mode (scores)
    var = get_variance (scores)
    stddev = get_stddev (scores)

print(f' Average : { average } \n Median : { median } \n Smallest : { minimum } \n Largest : { maximum } ')
print(f' Mode : { mode } \n Variance : {var} \n Stanard Deviation : {stddev}')

import statistics

minimum_value = min(scores)
average_value = statistics.mean(scores)
median_value = statistics.median(scores)
mode_value = statistics.mode(scores)

print("Using the 'statistics' library \nthe minimum value is " + str(minimum_value) + '\n' + "the average is " + str(average_value) + '\n' + "the median is " + str(median_value) + '\n' + "and the mode is " + str(mode_value) )


import cv2
import numpy as np

faceCascade = cv2.CascadeClassifier('E:/FaceRecog/venv/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml')
video_capture = cv2.VideoCapture(0)
rec = cv2.face.LBPHFaceRecognizer_create()
rec.read('E:/FaceRecog/Recognizer/Data1.yml')
#id = 1
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
fontColor = (0, 0, 255)
while True:
    # Capture frame-by-frame
    ret, img = video_capture.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, )

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 250, 0), 4)
        id, conf = rec.predict(gray[y:y+h, x:x+w])
        print(id)
        if id==1 or id==2:
            id="Unlock"
        else:
            id="Locked"
        cv2.putText(img, str(id), (x+w, y+h), font, fontScale, fontColor, 3)
    # Display the resulting frame
    cv2.imshow('Face', img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()


# -*- coding: utf-8 -*-
"""
Created on Wed Dec  5 10:22:08 2018

--- Day 5: Alchemical Reduction ---
"""

with open('C:/Users/Aoi_B/Documents/adventofcode2018/day5/input.txt', 'r') as myfile:
    polymer=myfile.read().replace('\n', '')

old_length = len(polymer)

def reactPolymer(polymer):
    x = 0
    while x < len(polymer):
        if x != len(polymer)-1:
            if polymer[x].lower() == polymer[x+1].lower():
                if polymer[x] != polymer[x+1]:
                    polymer = polymer[:x] + polymer[x+2:]
                    x = 0
                else:
                    x = x + 1
            else:
                x = x + 1
        else:
            x = x + 1
    return polymer

def optimalPolymerEvaluator(element, polymer):
        experiment_polymer = polymer.replace(str(element),'')
        experiment_polymer = polymer.replace(str(element.upper()),'')
        return experiment_polymer

def polymerImprovementDevice(polymer):
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    optimal_length = 100000
    optimal_element = ''
    for letter in alphabet:
        experiment_polymer = optimalPolymerEvaluator(letter, polymer)
        new_polymer = reactPolymer(experiment_polymer)
        print(len(new_polymer))
        if len(new_polymer) < optimal_length:
            optimal_length = len(new_polymer)
            optimal_element = letter
    print("After extensive experimentation the most optimal polymer improvement is removing " +
          optimal_element + " from the polymer, resulting in polymer length " + str(optimal_length)
          + "!")

polymerImprovementDevice(polymer)


import copy
from typing import Optional, List
import math
import warnings

import torch
import torch.nn.functional as F
from torch import nn, Tensor
from torch.nn.init import xavier_uniform_, constant_, uniform_, normal_
from torch.autograd import Function
from torch.autograd.function import once_differentiable

import MultiScaleDeformableAttention as MSDA

def inverse_sigmoid(x, eps=1e-5):
    x = x.clamp(min=0, max=1)
    x1 = x.clamp(min=eps)
    x2 = (1 - x).clamp(min=eps)
    return torch.log(x1/x2)

class MSDeformAttnFunction(Function):
    @staticmethod
    def forward(ctx, value, value_spatial_shapes, value_level_start_index, sampling_locations, attention_weights, im2col_step):
        ctx.im2col_step = im2col_step
        output = MSDA.ms_deform_attn_forward(
            value, value_spatial_shapes, value_level_start_index, sampling_locations, attention_weights, ctx.im2col_step)
        ctx.save_for_backward(value, value_spatial_shapes, value_level_start_index, sampling_locations, attention_weights)
        return output

    @staticmethod
    @once_differentiable
    def backward(ctx, grad_output):
        value, value_spatial_shapes, value_level_start_index, sampling_locations, attention_weights = ctx.saved_tensors
        grad_value, grad_sampling_loc, grad_attn_weight = \
            MSDA.ms_deform_attn_backward(
                value, value_spatial_shapes, value_level_start_index, sampling_locations, attention_weights, grad_output, ctx.im2col_step)

        return grad_value, None, None, grad_sampling_loc, grad_attn_weight, None

def _is_power_of_2(n):
    if (not isinstance(n, int)) or (n < 0):
        raise ValueError("invalid input for _is_power_of_2: {} (type: {})".format(n, type(n)))
    return (n & (n-1) == 0) and n != 0

class MSDeformAttn(nn.Module):
    def __init__(self, d_model=256, n_levels=4, n_heads=8, n_points=4):
        """
        Multi-Scale Deformable Attention Module
        :param d_model      hidden dimension
        :param n_levels     number of feature levels
        :param n_heads      number of attention heads
        :param n_points     number of sampling points per attention head per feature level
        """
        super().__init__()
        if d_model % n_heads != 0:
            raise ValueError('d_model must be divisible by n_heads, but got {} and {}'.format(d_model, n_heads))
        _d_per_head = d_model // n_heads
        # you'd better set _d_per_head to a power of 2 which is more efficient in our CUDA implementation
        if not _is_power_of_2(_d_per_head):
            warnings.warn("You'd better set d_model in MSDeformAttn to make the dimension of each attention head a power of 2 "
                          "which is more efficient in our CUDA implementation.")

        self.im2col_step = 64

        self.d_model = d_model
        self.n_levels = n_levels
        self.n_heads = n_heads
        self.n_points = n_points

        self.sampling_offsets = nn.Linear(d_model, n_heads * n_levels * n_points * 2)
        self.attention_weights = nn.Linear(d_model, n_heads * n_levels * n_points)
        self.value_proj = nn.Linear(d_model, d_model)
        self.output_proj = nn.Linear(d_model, d_model)

        self._reset_parameters()

    def _reset_parameters(self):
        constant_(self.sampling_offsets.weight.data, 0.)
        thetas = torch.arange(self.n_heads, dtype=torch.float32) * (2.0 * math.pi / self.n_heads)
        grid_init = torch.stack([thetas.cos(), thetas.sin()], -1)
        grid_init = (grid_init / grid_init.abs().max(-1, keepdim=True)[0]).view(self.n_heads, 1, 1, 2).repeat(1, self.n_levels, self.n_points, 1)
        for i in range(self.n_points):
            grid_init[:, :, i, :] *= i + 1
        with torch.no_grad():
            self.sampling_offsets.bias = nn.Parameter(grid_init.view(-1))
        constant_(self.attention_weights.weight.data, 0.)
        constant_(self.attention_weights.bias.data, 0.)
        xavier_uniform_(self.value_proj.weight.data)
        constant_(self.value_proj.bias.data, 0.)
        xavier_uniform_(self.output_proj.weight.data)
        constant_(self.output_proj.bias.data, 0.)

    def forward(self, query, reference_points, input_flatten, input_spatial_shapes, input_level_start_index, input_padding_mask=None):
        """
        :param query                       (N, Length_{query}, C)
        :param reference_points            (N, Length_{query}, n_levels, 2), range in [0, 1], top-left (0,0), bottom-right (1, 1), including padding area
                                        or (N, Length_{query}, n_levels, 4), add additional (w, h) to form reference boxes
        :param input_flatten               (N, \sum_{l=0}^{L-1} H_l \cdot W_l, C)
        :param input_spatial_shapes        (n_levels, 2), [(H_0, W_0), (H_1, W_1), ..., (H_{L-1}, W_{L-1})]
        :param input_level_start_index     (n_levels, ), [0, H_0*W_0, H_0*W_0+H_1*W_1, H_0*W_0+H_1*W_1+H_2*W_2, ..., H_0*W_0+H_1*W_1+...+H_{L-1}*W_{L-1}]
        :param input_padding_mask          (N, \sum_{l=0}^{L-1} H_l \cdot W_l), True for padding elements, False for non-padding elements

        :return output                     (N, Length_{query}, C)
        """
        N, Len_q, _ = query.shape
        N, Len_in, _ = input_flatten.shape
        assert (input_spatial_shapes[:, 0] * input_spatial_shapes[:, 1]).sum() == Len_in

        value = self.value_proj(input_flatten)
        if input_padding_mask is not None:
            value = value.masked_fill(input_padding_mask[..., None], float(0))
        value = value.view(N, Len_in, self.n_heads, self.d_model // self.n_heads)
        sampling_offsets = self.sampling_offsets(query).view(N, Len_q, self.n_heads, self.n_levels, self.n_points, 2)
        attention_weights = self.attention_weights(query).view(N, Len_q, self.n_heads, self.n_levels * self.n_points)
        attention_weights = F.softmax(attention_weights, -1).view(N, Len_q, self.n_heads, self.n_levels, self.n_points)
        # N, Len_q, n_heads, n_levels, n_points, 2
        if reference_points.shape[-1] == 2:
            offset_normalizer = torch.stack([input_spatial_shapes[..., 1], input_spatial_shapes[..., 0]], -1)
            sampling_locations = reference_points[:, :, None, :, None, :] \
                                 + sampling_offsets / offset_normalizer[None, None, None, :, None, :]
        elif reference_points.shape[-1] == 4:
            sampling_locations = reference_points[:, :, None, :, None, :2] \
                                 + sampling_offsets / self.n_points * reference_points[:, :, None, :, None, 2:] * 0.5
        else:
            raise ValueError(
                'Last dim of reference_points must be 2 or 4, but get {} instead.'.format(reference_points.shape[-1]))
        output = MSDeformAttnFunction.apply(
            value, input_spatial_shapes, input_level_start_index, sampling_locations, attention_weights, self.im2col_step)
        output = self.output_proj(output)
        return output

class DeformableTransformer(nn.Module):
    def __init__(self, d_model=256, nhead=8,
                 num_encoder_layers=6, num_decoder_layers=6, dim_feedforward=1024, dropout=0.1,
                 activation="relu", return_intermediate_dec=False,
                 num_feature_levels=4, dec_n_points=4,  enc_n_points=4,
                 two_stage=False, two_stage_num_proposals=300):
        super().__init__()

        self.d_model = d_model
        self.nhead = nhead
        self.two_stage = two_stage
        self.two_stage_num_proposals = two_stage_num_proposals

        encoder_layer = DeformableTransformerEncoderLayer(d_model, dim_feedforward,
                                                          dropout, activation,
                                                          num_feature_levels, nhead, enc_n_points)
        self.encoder = DeformableTransformerEncoder(encoder_layer, num_encoder_layers)

        decoder_layer = DeformableTransformerDecoderLayer(d_model, dim_feedforward,
                                                          dropout, activation,
                                                          num_feature_levels, nhead, dec_n_points)
        self.decoder = DeformableTransformerDecoder(decoder_layer, num_decoder_layers, return_intermediate_dec)

        self.level_embed = nn.Parameter(torch.Tensor(num_feature_levels, d_model))

        if two_stage:
            self.enc_output = nn.Linear(d_model, d_model)
            self.enc_output_norm = nn.LayerNorm(d_model)
            self.pos_trans = nn.Linear(d_model * 2, d_model * 2)
            self.pos_trans_norm = nn.LayerNorm(d_model * 2)
        else:
            self.reference_points = nn.Linear(d_model, 2)

        self._reset_parameters()

    def _reset_parameters(self):
        for p in self.parameters():
            if p.dim() > 1:
                nn.init.xavier_uniform_(p)
        for m in self.modules():
            if isinstance(m, MSDeformAttn):
                m._reset_parameters()
        if not self.two_stage:
            xavier_uniform_(self.reference_points.weight.data, gain=1.0)
            constant_(self.reference_points.bias.data, 0.)
        normal_(self.level_embed)

    def get_proposal_pos_embed(self, proposals):
        num_pos_feats = 128
        temperature = 10000
        scale = 2 * math.pi

        dim_t = torch.arange(num_pos_feats, dtype=torch.float32, device=proposals.device)
        dim_t = temperature ** (2 * (dim_t // 2) / num_pos_feats)
        # N, L, 4
        proposals = proposals.sigmoid() * scale
        # N, L, 4, 128
        pos = proposals[:, :, :, None] / dim_t
        # N, L, 4, 64, 2
        pos = torch.stack((pos[:, :, :, 0::2].sin(), pos[:, :, :, 1::2].cos()), dim=4).flatten(2)
        return pos

    def gen_encoder_output_proposals(self, memory, memory_padding_mask, spatial_shapes):
        N_, S_, C_ = memory.shape
        base_scale = 4.0
        proposals = []
        _cur = 0
        for lvl, (H_, W_) in enumerate(spatial_shapes):
            mask_flatten_ = memory_padding_mask[:, _cur:(_cur + H_ * W_)].view(N_, H_, W_, 1)
            valid_H = torch.sum(~mask_flatten_[:, :, 0, 0], 1)
            valid_W = torch.sum(~mask_flatten_[:, 0, :, 0], 1)

            grid_y, grid_x = torch.meshgrid(torch.linspace(0, H_ - 1, H_, dtype=torch.float32, device=memory.device),
                                            torch.linspace(0, W_ - 1, W_, dtype=torch.float32, device=memory.device))
            grid = torch.cat([grid_x.unsqueeze(-1), grid_y.unsqueeze(-1)], -1)

            scale = torch.cat([valid_W.unsqueeze(-1), valid_H.unsqueeze(-1)], 1).view(N_, 1, 1, 2)
            grid = (grid.unsqueeze(0).expand(N_, -1, -1, -1) + 0.5) / scale
            wh = torch.ones_like(grid) * 0.05 * (2.0 ** lvl)
            proposal = torch.cat((grid, wh), -1).view(N_, -1, 4)
            proposals.append(proposal)
            _cur += (H_ * W_)
        output_proposals = torch.cat(proposals, 1)
        output_proposals_valid = ((output_proposals > 0.01) & (output_proposals < 0.99)).all(-1, keepdim=True)
        output_proposals = torch.log(output_proposals / (1 - output_proposals))
        output_proposals = output_proposals.masked_fill(memory_padding_mask.unsqueeze(-1), float('inf'))
        output_proposals = output_proposals.masked_fill(~output_proposals_valid, float('inf'))

        output_memory = memory
        output_memory = output_memory.masked_fill(memory_padding_mask.unsqueeze(-1), float(0))
        output_memory = output_memory.masked_fill(~output_proposals_valid, float(0))
        output_memory = self.enc_output_norm(self.enc_output(output_memory))
        return output_memory, output_proposals

    def get_valid_ratio(self, mask):
        _, H, W = mask.shape
        valid_H = torch.sum(~mask[:, :, 0], 1)
        valid_W = torch.sum(~mask[:, 0, :], 1)
        valid_ratio_h = valid_H.float() / H
        valid_ratio_w = valid_W.float() / W
        valid_ratio = torch.stack([valid_ratio_w, valid_ratio_h], -1)
        return valid_ratio

    def forward(self, srcs, masks, pos_embeds, query_embed=None, da=False):
        assert self.two_stage or query_embed is not None

        # prepare input for encoder
        src_flatten = []
        mask_flatten = []
        lvl_pos_embed_flatten = []
        spatial_shapes = []
        for lvl, (src, mask, pos_embed) in enumerate(zip(srcs, masks, pos_embeds)):
            bs, c, h, w = src.shape
            spatial_shape = (h, w)
            spatial_shapes.append(spatial_shape)
            src = src.flatten(2).transpose(1, 2)
            mask = mask.flatten(1)
            pos_embed = pos_embed.flatten(2).transpose(1, 2)
            lvl_pos_embed = pos_embed + self.level_embed[lvl].view(1, 1, -1)
            lvl_pos_embed_flatten.append(lvl_pos_embed)
            src_flatten.append(src)
            mask_flatten.append(mask)
        src_flatten = torch.cat(src_flatten, 1)
        mask_flatten = torch.cat(mask_flatten, 1)
        lvl_pos_embed_flatten = torch.cat(lvl_pos_embed_flatten, 1)
        spatial_shapes = torch.as_tensor(spatial_shapes, dtype=torch.long, device=src_flatten.device)
        level_start_index = torch.cat((spatial_shapes.new_zeros((1, )), spatial_shapes.prod(1).cumsum(0)[:-1]))
        valid_ratios = torch.stack([self.get_valid_ratio(m) for m in masks], 1)

        # encoder
        memory = self.encoder(src_flatten, spatial_shapes, level_start_index, valid_ratios, lvl_pos_embed_flatten, mask_flatten)
        if da:
            da_meta = []
            sizes = [(ss[0]*ss[1]).item() for ss in spatial_shapes]
            mem_split = memory.permute(0, 2, 1).split(sizes, dim=-1)
            feats = []
            for shapes, mem in zip(spatial_shapes, mem_split):
                feats.append(mem.view(bs, c, shapes[0], shapes[1]))
            da_meta.append(feats)
        else:
            da_meta = None

        # prepare input for decoder
        bs, _, c = memory.shape
        if self.two_stage:
            output_memory, output_proposals = self.gen_encoder_output_proposals(memory, mask_flatten, spatial_shapes)

            # hack implementation for two-stage Deformable DETR
            enc_outputs_class = self.decoder.class_embed[self.decoder.num_layers](output_memory)
            enc_outputs_coord_unact = self.decoder.bbox_embed[self.decoder.num_layers](output_memory) + output_proposals

            topk = self.two_stage_num_proposals
            topk_proposals = torch.topk(enc_outputs_class[..., 0], topk, dim=1)[1]
            topk_coords_unact = torch.gather(enc_outputs_coord_unact, 1, topk_proposals.unsqueeze(-1).repeat(1, 1, 4))
            topk_coords_unact = topk_coords_unact.detach()
            reference_points = topk_coords_unact.sigmoid()
            init_reference_out = reference_points
            pos_trans_out = self.pos_trans_norm(self.pos_trans(self.get_proposal_pos_embed(topk_coords_unact)))
            query_embed, tgt = torch.split(pos_trans_out, c, dim=2)
        else:
            query_embed, tgt = torch.split(query_embed, c, dim=1)
            query_embed = query_embed.unsqueeze(0).expand(bs, -1, -1)
            tgt = tgt.unsqueeze(0).expand(bs, -1, -1)
            reference_points = self.reference_points(query_embed).sigmoid()
            init_reference_out = reference_points

        # decoder
        hs, inter_references = self.decoder(tgt, reference_points, memory,
                                            spatial_shapes, level_start_index, valid_ratios, query_embed, mask_flatten)

        inter_references_out = inter_references
        if self.two_stage:
            return hs, init_reference_out, inter_references_out, enc_outputs_class, enc_outputs_coord_unact, da_meta
        return hs, init_reference_out, inter_references_out, None, None, da_meta

class DeformableTransformerEncoderLayer(nn.Module):
    def __init__(self,
                 d_model=256, d_ffn=1024,
                 dropout=0.1, activation="relu",
                 n_levels=4, n_heads=8, n_points=4):
        super().__init__()

        # self attention
        self.self_attn = MSDeformAttn(d_model, n_levels, n_heads, n_points)
        self.dropout1 = nn.Dropout(dropout)
        self.norm1 = nn.LayerNorm(d_model)

        # ffn
        self.linear1 = nn.Linear(d_model, d_ffn)
        self.activation = _get_activation_fn(activation)
        self.dropout2 = nn.Dropout(dropout)
        self.linear2 = nn.Linear(d_ffn, d_model)
        self.dropout3 = nn.Dropout(dropout)
        self.norm2 = nn.LayerNorm(d_model)

    @staticmethod
    def with_pos_embed(tensor, pos):
        return tensor if pos is None else tensor + pos

    def forward_ffn(self, src):
        src2 = self.linear2(self.dropout2(self.activation(self.linear1(src))))
        src = src + self.dropout3(src2)
        src = self.norm2(src)
        return src

    def forward(self, src, pos, reference_points, spatial_shapes, level_start_index, padding_mask=None):
        # self attention
        src2 = self.self_attn(self.with_pos_embed(src, pos), reference_points, src, spatial_shapes, level_start_index, padding_mask)
        src = src + self.dropout1(src2)
        src = self.norm1(src)

        # ffn
        src = self.forward_ffn(src)

        return src

class DeformableTransformerEncoder(nn.Module):
    def __init__(self, encoder_layer, num_layers):
        super().__init__()
        self.layers = _get_clones(encoder_layer, num_layers)
        self.num_layers = num_layers

    @staticmethod
    def get_reference_points(spatial_shapes, valid_ratios, device):
        reference_points_list = []
        for lvl, (H_, W_) in enumerate(spatial_shapes):

            ref_y, ref_x = torch.meshgrid(torch.linspace(0.5, H_ - 0.5, H_, dtype=torch.float32, device=device),
                                          torch.linspace(0.5, W_ - 0.5, W_, dtype=torch.float32, device=device))
            ref_y = ref_y.reshape(-1)[None] / (valid_ratios[:, None, lvl, 1] * H_)
            ref_x = ref_x.reshape(-1)[None] / (valid_ratios[:, None, lvl, 0] * W_)
            ref = torch.stack((ref_x, ref_y), -1)
            reference_points_list.append(ref)
        reference_points = torch.cat(reference_points_list, 1)
        reference_points = reference_points[:, :, None] * valid_ratios[:, None]
        return reference_points

    def forward(self, src, spatial_shapes, level_start_index, valid_ratios, pos=None, padding_mask=None):
        output = src
        reference_points = self.get_reference_points(spatial_shapes, valid_ratios, device=src.device)
        for _, layer in enumerate(self.layers):
            output = layer(output, pos, reference_points, spatial_shapes, level_start_index, padding_mask)

        return output

class DeformableTransformerDecoderLayer(nn.Module):
    def __init__(self, d_model=256, d_ffn=1024,
                 dropout=0.1, activation="relu",
                 n_levels=4, n_heads=8, n_points=4):
        super().__init__()

        # cross attention
        self.cross_attn = MSDeformAttn(d_model, n_levels, n_heads, n_points)
        self.dropout1 = nn.Dropout(dropout)
        self.norm1 = nn.LayerNorm(d_model)

        # self attention
        self.self_attn = nn.MultiheadAttention(d_model, n_heads, dropout=dropout)
        self.dropout2 = nn.Dropout(dropout)
        self.norm2 = nn.LayerNorm(d_model)

        # ffn
        self.linear1 = nn.Linear(d_model, d_ffn)
        self.activation = _get_activation_fn(activation)
        self.dropout3 = nn.Dropout(dropout)
        self.linear2 = nn.Linear(d_ffn, d_model)
        self.dropout4 = nn.Dropout(dropout)
        self.norm3 = nn.LayerNorm(d_model)

    @staticmethod
    def with_pos_embed(tensor, pos):
        return tensor if pos is None else tensor + pos

    def forward_ffn(self, tgt):
        tgt2 = self.linear2(self.dropout3(self.activation(self.linear1(tgt))))
        tgt = tgt + self.dropout4(tgt2)
        tgt = self.norm3(tgt)
        return tgt

    def forward(self, tgt, query_pos, reference_points, src, src_spatial_shapes, level_start_index, src_padding_mask=None):
        # self attention
        q = k = self.with_pos_embed(tgt, query_pos)
        tgt2 = self.self_attn(q.transpose(0, 1), k.transpose(0, 1), tgt.transpose(0, 1))[0].transpose(0, 1)
        tgt = tgt + self.dropout2(tgt2)
        tgt = self.norm2(tgt)

        # cross attention
        tgt2 = self.cross_attn(self.with_pos_embed(tgt, query_pos),
                               reference_points,
                               src, src_spatial_shapes, level_start_index, src_padding_mask)
        tgt = tgt + self.dropout1(tgt2)
        tgt = self.norm1(tgt)

        # ffn
        tgt = self.forward_ffn(tgt)

        return tgt

class DeformableTransformerDecoder(nn.Module):
    def __init__(self, decoder_layer, num_layers, return_intermediate=False):
        super().__init__()
        self.layers = _get_clones(decoder_layer, num_layers)
        self.num_layers = num_layers
        self.return_intermediate = return_intermediate
        # hack implementation for iterative bounding box refinement and two-stage Deformable DETR
        self.bbox_embed = None
        self.class_embed = None

    def forward(self, tgt, reference_points, src, src_spatial_shapes, src_level_start_index, src_valid_ratios,
                query_pos=None, src_padding_mask=None):
        output = tgt

        intermediate = []
        intermediate_reference_points = []
        for lid, layer in enumerate(self.layers):
            if reference_points.shape[-1] == 4:
                reference_points_input = reference_points[:, :, None] \
                                         * torch.cat([src_valid_ratios, src_valid_ratios], -1)[:, None]
            else:
                assert reference_points.shape[-1] == 2
                reference_points_input = reference_points[:, :, None] * src_valid_ratios[:, None]
            output = layer(output, query_pos, reference_points_input, src, src_spatial_shapes, src_level_start_index, src_padding_mask)

            # hack implementation for iterative bounding box refinement
            if self.bbox_embed is not None:
                tmp = self.bbox_embed[lid](output)
                if reference_points.shape[-1] == 4:
                    new_reference_points = tmp + inverse_sigmoid(reference_points)
                    new_reference_points = new_reference_points.sigmoid()
                else:
                    assert reference_points.shape[-1] == 2
                    new_reference_points = tmp
                    new_reference_points[..., :2] = tmp[..., :2] + inverse_sigmoid(reference_points)
                    new_reference_points = new_reference_points.sigmoid()
                reference_points = new_reference_points.detach()

            if self.return_intermediate:
                intermediate.append(output)
                intermediate_reference_points.append(reference_points)

        if self.return_intermediate:
            return torch.stack(intermediate), torch.stack(intermediate_reference_points)

        return output, reference_points

def _get_clones(module, N):
    return nn.ModuleList([copy.deepcopy(module) for i in range(N)])

def _get_activation_fn(activation):
    """Return an activation function given a string"""
    if activation == "relu":
        return F.relu
    if activation == "gelu":
        return F.gelu
    if activation == "glu":
        return F.glu
    raise RuntimeError(F"activation should be relu/gelu, not {activation}.")

def build_deforamble_transformer(args):
    return DeformableTransformer(
        d_model=args.hidden_dim,
        nhead=args.nheads,
        num_encoder_layers=args.enc_layers,
        num_decoder_layers=args.dec_layers,
        dim_feedforward=args.dim_feedforward,
        dropout=args.dropout,
        activation="relu",
        return_intermediate_dec=True,
        num_feature_levels=args.num_feature_levels,
        dec_n_points=args.dec_n_points,
        enc_n_points=args.enc_n_points,
        two_stage=args.two_stage,
        two_stage_num_proposals=args.num_queries)

class NestedTensor(object):
    def __init__(self, tensors, mask: Optional[Tensor]):
        self.tensors = tensors
        self.mask = mask

    def to(self, device, non_blocking=False):
        cast_tensor = self.tensors.to(device, non_blocking=non_blocking)
        mask = self.mask
        if mask is not None:
            assert mask is not None
            cast_mask = mask.to(device, non_blocking=non_blocking)
        else:
            cast_mask = None
        return NestedTensor(cast_tensor, cast_mask)

    def record_stream(self, *args, **kwargs):
        self.tensors.record_stream(*args, **kwargs)
        if self.mask is not None:
            self.mask.record_stream(*args, **kwargs)

    def decompose(self):
        return self.tensors, self.mask

    def __repr__(self):
        return str(self.tensors)

def nested_tensor_from_tensor_list(tensor_list: List[Tensor]):
    def _max_by_axis(the_list):
        # type: (List[List[int]]) -> List[int]
        maxes = the_list[0]
        for sublist in the_list[1:]:
            for index, item in enumerate(sublist):
                maxes[index] = max(maxes[index], item)
        return maxes
    # TODO make this more general
    if tensor_list[0].ndim == 3:
        # TODO make it support different-sized images
        max_size = _max_by_axis([list(img.shape) for img in tensor_list])
        # min_size = tuple(min(s) for s in zip(*[img.shape for img in tensor_list]))
        batch_shape = [len(tensor_list)] + max_size
        b, c, h, w = batch_shape
        dtype = tensor_list[0].dtype
        device = tensor_list[0].device
        tensor = torch.zeros(batch_shape, dtype=dtype, device=device)
        mask = torch.ones((b, h, w), dtype=torch.bool, device=device)
        for img, pad_img, m in zip(tensor_list, tensor, mask):
            pad_img[: img.shape[0], : img.shape[1], : img.shape[2]].copy_(img)
            m[: img.shape[1], :img.shape[2]] = False
    else:
        raise ValueError('not supported')
    return NestedTensor(tensor, mask)

class MLP(nn.Module):
    """ Very simple multi-layer perceptron (also called FFN)"""

    def __init__(self, input_dim, hidden_dim, output_dim, num_layers):
        super().__init__()
        self.num_layers = num_layers
        h = [hidden_dim] * (num_layers - 1)
        self.layers = nn.ModuleList(nn.Linear(n, k) for n, k in zip([input_dim] + h, h + [output_dim]))

    def forward(self, x):
        for i, layer in enumerate(self.layers):
            x = F.relu(layer(x)) if i < self.num_layers - 1 else layer(x)
        return x

class DeformableDETR(nn.Module):
    """ This is the Deformable DETR module that performs object detection """
    def __init__(self, backbone, transformer, num_classes, num_queries, num_feature_levels,
                 aux_loss=True, with_box_refine=False, two_stage=False, object_embedding_loss=False,
                 obj_embedding_head=None):
        """ Initializes the model.
        Parameters:
            backbone: torch module of the backbone to be used. See backbone.py
            transformer: torch module of the transformer architecture. See transformer.py
            num_classes: number of object classes
            num_queries: number of object queries, ie detection slot. This is the maximal number of objects
                         DETR can detect in a single image. For COCO, we recommend 100 queries.
            aux_loss: True if auxiliary decoding losses (loss at each decoder layer) are to be used.
            with_box_refine: iterative bounding box refinement
            two_stage: two-stage Deformable DETR
        """
        super().__init__()
        self.num_queries = num_queries
        self.transformer = transformer
        self.object_embedding_loss = object_embedding_loss
        hidden_dim = transformer.d_model
        self.class_embed = nn.Linear(hidden_dim, num_classes)
        if self.object_embedding_loss:
            if obj_embedding_head == 'intermediate':
                last_channel_size = 2*hidden_dim
            elif obj_embedding_head == 'head':
                last_channel_size = hidden_dim//2
            self.feature_embed = MLP(hidden_dim, hidden_dim, last_channel_size, 2)
        self.bbox_embed = MLP(hidden_dim, hidden_dim, 4, 3)
        self.num_feature_levels = num_feature_levels
        if not two_stage:
            self.query_embed = nn.Embedding(num_queries, hidden_dim*2)
        if num_feature_levels > 1:
            num_backbone_outs = len(backbone.strides)
            input_proj_list = []
            for _ in range(num_backbone_outs):
                in_channels = backbone.num_channels[_]
                input_proj_list.append(nn.Sequential(
                    nn.Conv2d(in_channels, hidden_dim, kernel_size=1),
                    nn.GroupNorm(32, hidden_dim),
                ))
            for _ in range(num_feature_levels - num_backbone_outs):
                input_proj_list.append(nn.Sequential(
                    nn.Conv2d(in_channels, hidden_dim, kernel_size=3, stride=2, padding=1),
                    nn.GroupNorm(32, hidden_dim),
                ))
                in_channels = hidden_dim
            self.input_proj = nn.ModuleList(input_proj_list)
        else:
            self.input_proj = nn.ModuleList([
                nn.Sequential(
                    nn.Conv2d(backbone.num_channels[0], hidden_dim, kernel_size=1),
                    nn.GroupNorm(32, hidden_dim),
                )])
        self.backbone = backbone
        self.aux_loss = aux_loss
        self.with_box_refine = with_box_refine
        self.two_stage = two_stage

        prior_prob = 0.01
        bias_value = -math.log((1 - prior_prob) / prior_prob)
        self.class_embed.bias.data = torch.ones(num_classes) * bias_value
        nn.init.constant_(self.bbox_embed.layers[-1].weight.data, 0)
        nn.init.constant_(self.bbox_embed.layers[-1].bias.data, 0)
        for proj in self.input_proj:
            nn.init.xavier_uniform_(proj[0].weight, gain=1)
            nn.init.constant_(proj[0].bias, 0)

        # if two-stage, the last class_embed and bbox_embed is for region proposal generation
        num_pred = (transformer.decoder.num_layers + 1) if two_stage else transformer.decoder.num_layers
        if with_box_refine:
            self.class_embed = _get_clones(self.class_embed, num_pred)
            # if self.object_embedding_loss:
            #     self.feature_embed = _get_clones(self.feature_embed, num_pred)
            self.bbox_embed = _get_clones(self.bbox_embed, num_pred)
            nn.init.constant_(self.bbox_embed[0].layers[-1].bias.data[2:], -2.0)
            # hack implementation for iterative bounding box refinement
            self.transformer.decoder.bbox_embed = self.bbox_embed
        else:
            nn.init.constant_(self.bbox_embed.layers[-1].bias.data[2:], -2.0)
            self.class_embed = nn.ModuleList([self.class_embed for _ in range(num_pred)])
            # if self.object_embedding_loss:
            #     self.feature_embed = nn.ModuleList([self.feature_embed for _ in range(num_pred)])
            self.bbox_embed = nn.ModuleList([self.bbox_embed for _ in range(num_pred)])
            self.transformer.decoder.bbox_embed = None
        if two_stage:
            # hack implementation for two-stage
            self.transformer.decoder.class_embed = self.class_embed
            for box_embed in self.bbox_embed:
                nn.init.constant_(box_embed.layers[-1].bias.data[2:], 0.0)

    def forward(self, samples, da=False):
        """ The forward expects a NestedTensor, which consists of:
               - samples.tensor: batched images, of shape [batch_size x 3 x H x W]
               - samples.mask: a binary mask of shape [batch_size x H x W], containing 1 on padded pixels

            It returns a dict with the following elements:
               - "pred_logits": the classification logits (including no-object) for all queries.
                                Shape= [batch_size x num_queries x (num_classes + 1)]
               - "pred_boxes": The normalized boxes coordinates for all queries, represented as
                               (center_x, center_y, height, width). These values are normalized in [0, 1],
                               relative to the size of each individual image (disregarding possible padding).
                               See PostProcess for information on how to retrieve the unnormalized bounding box.
               - "aux_outputs": Optional, only returned when auxilary losses are activated. It is a list of
                                dictionnaries containing the two above keys for each decoder layer.
        """
        if not isinstance(samples, NestedTensor):
            samples = nested_tensor_from_tensor_list(samples)
        return self.forward_samples(samples, da=da)

    def forward_samples(self, samples, da=False):
        features, pos = self.backbone(samples)
        srcs = []
        masks = []
        for l, feat in enumerate(features):
            src, mask = feat.decompose()
            srcs.append(self.input_proj[l](src))
            masks.append(mask)
            assert mask is not None
        if self.num_feature_levels > len(srcs):
            _len_srcs = len(srcs)
            for l in range(_len_srcs, self.num_feature_levels):
                if l == _len_srcs:
                    src = self.input_proj[l](features[-1].tensors)
                else:
                    src = self.input_proj[l](srcs[-1])
                m = samples.mask
                mask = F.interpolate(m[None].float(), size=src.shape[-2:]).to(torch.bool)[0]
                pos_l = self.backbone[1](NestedTensor(src, mask)).to(src.dtype)
                srcs.append(src)
                masks.append(mask)
                pos.append(pos_l)
        query_embeds = None
        if not self.two_stage:
            query_embeds = self.query_embed.weight
        hs, init_reference, inter_references, enc_outputs_class, enc_outputs_coord_unact, da_features = self.transformer(srcs, masks, pos, query_embeds, da=da)
        outputs_classes = []
        outputs_coords = []
        outputs_features = []
        for lvl in range(hs.shape[0]):
            if lvl == 0:
                reference = init_reference
            else:
                reference = inter_references[lvl - 1]
            reference = inverse_sigmoid(reference)
            outputs_class = self.class_embed[lvl](hs[lvl])
            tmp = self.bbox_embed[lvl](hs[lvl])
            if reference.shape[-1] == 4:
                tmp += reference
            else:
                assert reference.shape[-1] == 2
                tmp[..., :2] += reference
            outputs_coord = tmp.sigmoid()
            outputs_classes.append(outputs_class)
            outputs_coords.append(outputs_coord)
            # if self.object_embedding_loss:
            #     outputs_features.append(outputs_feat)
        outputs_class = torch.stack(outputs_classes)
        outputs_coord = torch.stack(outputs_coords)
        # if self.object_embedding_loss:
        #     outputs_features = torch.stack(outputs_features)

        out = {'pred_logits': outputs_class[-1], 'pred_boxes': outputs_coord[-1]}
        if self.object_embedding_loss:
            out['pred_features'] = self.feature_embed(hs[-1])

        if self.aux_loss:
            out['aux_outputs'] = self._set_aux_loss(outputs_class, outputs_coord) #, outputs_features)
        if self.two_stage:
            enc_outputs_coord = enc_outputs_coord_unact.sigmoid()
            out['enc_outputs'] = {'pred_logits': enc_outputs_class, 'pred_boxes': enc_outputs_coord}
            if self.object_embedding_loss:
                out['pred_features'] = outputs_features
        if da:
            return out, da_features
        else:
            return out

    @torch.jit.unused
    def _set_aux_loss(self, outputs_class, outputs_coord):#, outputs_features):
        # this is a workaround to make torchscript happy, as torchscript
        # doesn't support dictionary with non-homogeneous values, such
        # as a dict having both a Tensor and a list.
        return [{'pred_logits': a, 'pred_boxes': b}#, 'pred_features': c}
                for a, b in zip(outputs_class[:-1], outputs_coord[:-1])] #, outputs_features[:-1])]


# The base class of the model that can do the
import tensorflow as tf
import numpy as np
from gensim.models import Word2Vec
import gensim
from gensim.models import KeyedVectors
import sys
from newDataLoader import DataLoader
from time import time
from tqdm import tqdm
import os
from scipy.stats import spearmanr
from scipy.spatial.distance import cosine
import json
import pickle
import argparse

class Model:

    def __init__(self, data, args):
        # random initialize the weights

        self.data = data
        vocab_size = data.vocab_size
        self.vocab_size = data.vocab_size
        embed_size = self.embed_size = 300
        self.res = args.restrict
        self.weight = args.weight

        rel_length = args.relational_embedding_size

        with tf.variable_scope("model", reuse=tf.AUTO_REUSE):

            # The center embedding should be initialize during the training process by "assign"
            # Directly use const is not memory efficient, because the constant is saved multiple times in the memory
            # The trainable here is set for not trainable for now

            self.W_amod = tf.get_variable("amod_tranform_matrix", [rel_length, embed_size])
            self.W_nsubj = tf.get_variable("nsubj_tranform_matrix", [rel_length, embed_size])
            self.W_dobj = tf.get_variable("dobj_tranform_matrix", [rel_length, embed_size])

            self.center_emb = tf.get_variable("center_emb", [vocab_size, embed_size], trainable=args.center_trainable)

            self.small_amod_emb = tf.get_variable("amod_emb", [vocab_size, rel_length])
            self.small_nsubj_emb = tf.get_variable("nsubj_emb", [vocab_size, rel_length])
            self.small_dobj_emb = tf.get_variable("dobj_emb", [vocab_size, rel_length])

            self.amod_emb = tf.matmul(self.small_amod_emb, self.W_amod)
            self.nsubj_emb = tf.matmul(self.small_nsubj_emb, self.W_nsubj)
            self.dobj_emb = tf.matmul(self.small_dobj_emb, self.W_dobj)

            self.W_amod_context = tf.get_variable("amod_context_tranform_matrix", [rel_length, embed_size])
            self.W_nsubj_context = tf.get_variable("nsubj_context_tranform_matrix", [rel_length, embed_size])
            self.W_dobj_context = tf.get_variable("dobj_context_tranform_matrix", [rel_length, embed_size])

            self.center_emb_context = tf.get_variable("center_emb_context", [vocab_size, embed_size], trainable=args.center_trainable)

            self.small_amod_emb_context = tf.get_variable("amod_emb_context", [vocab_size, rel_length])
            self.small_nsubj_emb_context = tf.get_variable("nsubj_emb_context", [vocab_size, rel_length])
            self.small_dobj_emb_context = tf.get_variable("dobj_emb_context", [vocab_size, rel_length])

            self.amod_emb_context = tf.matmul( self.small_amod_emb_context, self.W_amod_context)
            self.nsubj_emb_context = tf.matmul( self.small_nsubj_emb_context, self.W_nsubj_context)
            self.dobj_emb_context = tf.matmul( self.small_dobj_emb_context, self.W_dobj_context)

            self.emb_placeholder = tf.placeholder(tf.float32, [vocab_size, embed_size])
            self.emb_init = self.center_emb.assign(self.emb_placeholder)

            self.emb_placeholder_context = tf.placeholder(tf.float32, [vocab_size, embed_size])
            self.emb_init_context = self.center_emb_context.assign(self.emb_placeholder_context)

            self.amod_restrict_emb = self.restrict(self.amod_emb)
            self.nsubj_restrict_emb = self.restrict(self.nsubj_emb)
            self.dobj_restrict_emb = self.restrict(self.dobj_emb)

            self.amod_restrict_emb_context = self.restrict(self.amod_emb_context)
            self.nsubj_restrict_emb_context = self.restrict(self.nsubj_emb_context)
            self.dobj_restrict_emb_context = self.restrict(self.dobj_emb_context)

            self.predicate_amod_ids = tf.placeholder(tf.int32, [None])
            self.argument_amod_ids = tf.placeholder(tf.int32, [None])
            self.argument_prime_amod_ids = tf.placeholder(tf.int32, [None])

            self.predicate_nsubj_ids = tf.placeholder(tf.int32, [None])
            self.argument_nsubj_ids = tf.placeholder(tf.int32, [None])
            self.argument_prime_nsubj_ids = tf.placeholder(tf.int32, [None])

            self.predicate_dobj_ids = tf.placeholder(tf.int32, [None])
            self.argument_dobj_ids = tf.placeholder(tf.int32, [None])
            self.argument_prime_dobj_ids = tf.placeholder(tf.int32, [None])

            self.add_pred_amod()
            self.add_pred_nsubj()
            self.add_pred_dobj()

            self.add_prediction()
            self.add_loss()
            self.add_optimize()

    def get_tensor_shape(self, x, dim):
        return x.get_shape()[dim].value or tf.shape(x)[dim]

    def add_prediction(self):

        self.accuracy_amod = tf.reduce_mean(tf.to_float(tf.greater(self.pred_amod, self.pred_amod_prime)))
        self.accuracy_nsubj = tf.reduce_mean(tf.to_float(tf.greater(self.pred_nsubj, self.pred_nsubj_prime)))
        self.accuracy_dobj = tf.reduce_mean(tf.to_float(tf.greater(self.pred_dobj, self.pred_dobj_prime)))

    def restrict(self, embeddings):
        num_examples = self.get_tensor_shape(embeddings, 0)
        embedding_norms = tf.norm(embeddings, axis=1)
        mask = tf.greater(embedding_norms, tf.ones([num_examples]) * self.res)
        normalized_embeddings = tf.nn.l2_normalize(embeddings, axis=1) * self.res
        return tf.where(mask, x=normalized_embeddings, y=embeddings)

    def softmax_loss(self, antecedent_scores, antecedent_labels):
        gold_scores = antecedent_scores + tf.log(tf.to_float(antecedent_labels))  # [k, max_ant + 1]
        marginalized_gold_scores = tf.reduce_logsumexp(gold_scores, [1])  # [k]
        log_norm = tf.reduce_logsumexp(antecedent_scores, [1])  # [k]
        return log_norm - marginalized_gold_scores  # [k]

    def add_pred_amod(self):
        # center embeddings
        predicate_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb, self.predicate_amod_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.amod_emb, self.predicate_amod_ids))
        argument_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb, self.argument_amod_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.amod_emb, self.argument_amod_ids))
        argument_prime_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb,
                                                          self.argument_prime_amod_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.amod_emb, self.argument_prime_amod_ids))

        # context embeddings
        predicate_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context, self.predicate_amod_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.amod_emb_context, self.predicate_amod_ids))
        argument_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context, self.argument_amod_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.amod_emb_context, self.argument_amod_ids))
        argument_prime_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context,
                                                          self.argument_prime_amod_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.amod_emb_context, self.argument_prime_amod_ids))

        self.overall_predicate_amod = predicate_embedding

        self.base_embedding_handler = tf.nn.embedding_lookup(self.center_emb, self.predicate_amod_ids)
        self.base_embedding_context_handler = tf.nn.embedding_lookup(self.center_emb_context, self.predicate_amod_ids)

        self.amod_embedding_handler = predicate_embedding
        self.amod_embedding_context_handler = argument_embedding_context

        # the score is calculated as pred * argu_context + argu * pred_context
        self.pred_amod = tf.reduce_sum(tf.multiply(predicate_embedding, argument_embedding_context), 1) + \
        tf.reduce_sum(tf.multiply(predicate_embedding_context, argument_embedding), 1)

        self.pred_amod_prime = tf.reduce_sum(tf.multiply(predicate_embedding, argument_prime_embedding_context), 1) + \
        tf.reduce_sum(tf.multiply(predicate_embedding_context, argument_prime_embedding), 1)

    def add_pred_nsubj(self):
        predicate_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb, self.predicate_nsubj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.nsubj_emb, self.predicate_nsubj_ids))
        argument_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb, self.argument_nsubj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.nsubj_emb, self.argument_nsubj_ids))
        argument_prime_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb,
                                                          self.argument_prime_nsubj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(self.nsubj_emb, self.argument_prime_nsubj_ids))

        # context embeddings
        predicate_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context, self.predicate_nsubj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.nsubj_emb_context, self.predicate_nsubj_ids))
        argument_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context, self.argument_nsubj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.nsubj_emb_context, self.argument_nsubj_ids))
        argument_prime_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context,
                                                          self.argument_prime_nsubj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.nsubj_emb_context, self.argument_prime_nsubj_ids))

        self.overall_predicate_nsubj = predicate_embedding

        self.nsubj_embedding_handler = predicate_embedding
        self.nsubj_embedding_context_handler = argument_embedding_context

        self.pred_nsubj = tf.reduce_sum(tf.multiply(predicate_embedding, argument_embedding_context), 1) + \
        tf.reduce_sum(tf.multiply(predicate_embedding_context, argument_embedding), 1)

        self.pred_nsubj_prime = tf.reduce_sum(tf.multiply(predicate_embedding, argument_prime_embedding_context), 1) + \
        tf.reduce_sum(tf.multiply(predicate_embedding_context, argument_prime_embedding), 1)

    def add_pred_dobj(self):
        predicate_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb, self.predicate_dobj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.dobj_emb, self.predicate_dobj_ids))
        argument_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb, self.argument_dobj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.dobj_emb, self.argument_dobj_ids))
        argument_prime_embedding = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb,
                                                          self.argument_prime_dobj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(self.dobj_emb, self.argument_prime_dobj_ids))

        # context embeddings
        predicate_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context, self.predicate_dobj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.dobj_emb_context, self.predicate_dobj_ids))
        argument_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context, self.argument_dobj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.dobj_emb_context, self.argument_dobj_ids))
        argument_prime_embedding_context = (1 - self.weight) * tf.nn.embedding_lookup(self.center_emb_context,
                                                          self.argument_prime_dobj_ids) + self.weight * self.restrict(
            tf.nn.embedding_lookup(
                self.dobj_emb_context, self.argument_prime_dobj_ids))

        self.overall_predicate_dobj = predicate_embedding

        self.dobj_embedding_handler = predicate_embedding
        self.dobj_embedding_context_handler = argument_embedding_context

        self.pred_dobj = tf.reduce_sum(tf.multiply(predicate_embedding, argument_embedding_context), 1) + \
        tf.reduce_sum(tf.multiply(predicate_embedding_context, argument_embedding), 1)

        self.pred_dobj_prime = tf.reduce_sum(tf.multiply(predicate_embedding, argument_prime_embedding_context), 1) + \
        tf.reduce_sum(tf.multiply(predicate_embedding_context, argument_prime_embedding), 1)

    def add_loss(self):
        num_examples = self.get_tensor_shape(self.pred_dobj, 0)

        self.prediction = tf.reshape(tf.concat([self.pred_amod, self.pred_nsubj, self.pred_dobj], 0), [3*num_examples, 1] )
        self.prediction_prime = tf.reshape(tf.concat([self.pred_amod_prime, self.pred_nsubj_prime, self.pred_dobj_prime], 0), [3*num_examples, 1])

        all_scores = tf.concat([self.prediction, self.prediction_prime], 1)  # [3 * num_examples, 2]
        all_labels = tf.concat([tf.ones([3 * num_examples, 1]), tf.zeros([3 * num_examples, 1], 1)], 1)
        self.loss = tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(logits = all_scores, labels = all_labels))
    def add_optimize(self):
        optimizer = tf.train.GradientDescentOptimizer(0.5)
        self.optimize = optimizer.minimize(self.loss)

def test_keller_overall(sess, model, data):
    relation_names = ["amod", "dobj"]
    word2id = data.word2id

    for relation_name in relation_names:

        word_pairs = []
        gold_scores = []
        with open("SPA-dataset/" + relation_name + "_keller.txt") as amod_f:
            for line in amod_f:
                line_list = line.strip().split()

                try:
                    id1 = word2id[line_list[0]]
                except:
                    # print("Not in scope ", line_list[0])
                    continue

                try:
                    id2 = word2id[line_list[1]]
                except:
                    # print("Not in scope ", line_list[1])
                    continue

                score = float(line_list[2])
                gold_scores.append(score)
                word_pairs.append([id1, id2])
        word_pairs = np.array(word_pairs)

        feed_dict = {
            model.predicate_amod_ids: word_pairs[:, 0],
            model.argument_amod_ids: word_pairs[:, 0],
            model.predicate_dobj_ids: word_pairs[:, 0],
            model.argument_dobj_ids: word_pairs[:, 0],
            model.predicate_nsubj_ids: word_pairs[:, 0],
            model.argument_nsubj_ids: word_pairs[:, 0]
        }

        embeddings1 = {"base": {},
                       "amod": {},
                       "nsubj": {},
                       "dobj": {}
                      }

        result1 = sess.run([model.base_embedding_handler,
                                model.base_embedding_context_handler,
                                model.amod_embedding_handler,
                                model.amod_embedding_context_handler,
                                model.nsubj_embedding_handler,
                                model.nsubj_embedding_context_handler,
                                model.dobj_embedding_handler,
                                model.dobj_embedding_context_handler
                                ], feed_dict = feed_dict)

        embeddings1["base"]["h"],  embeddings1["base"]["t"], embeddings1["amod"]["h"],  embeddings1["amod"]["t"], \
        embeddings1["nsubj"]["h"],  embeddings1["nsubj"]["t"], embeddings1["dobj"]["h"],  embeddings1["dobj"]["t"] = result1

        feed_dict = {
            model.predicate_amod_ids: word_pairs[:, 1],
            model.argument_amod_ids: word_pairs[:, 1],
            model.predicate_dobj_ids: word_pairs[:, 1],
            model.argument_dobj_ids: word_pairs[:, 1],
            model.predicate_nsubj_ids: word_pairs[:, 1],
            model.argument_nsubj_ids: word_pairs[:, 1]
        }

        embeddings2 = {"base": {},
                       "amod": {},
                       "nsubj": {},
                       "dobj": {}
                      }

        result2 = sess.run([model.base_embedding_handler,
                                model.base_embedding_context_handler,
                                model.amod_embedding_handler,
                                model.amod_embedding_context_handler,
                                model.nsubj_embedding_handler,
                                model.nsubj_embedding_context_handler,
                                model.dobj_embedding_handler,
                                model.dobj_embedding_context_handler
                                ], feed_dict = feed_dict)

        embeddings2["base"]["h"],  embeddings2["base"]["t"], embeddings2["amod"]["h"],  embeddings2["amod"]["t"], \
        embeddings2["nsubj"]["h"],  embeddings2["nsubj"]["t"], embeddings2["dobj"]["h"],  embeddings2["dobj"]["t"] = result2

        base_word_score = [ 1 - cosine(embeddings1["base"]["h"][j], embeddings2["base"]["t"][j]) for j in range(embeddings1["base"]["h"].shape[0])]

        base_corr, _ = spearmanr(gold_scores, base_word_score)

        rel_word_score  = [ 1 - cosine(embeddings1[relation_name]["h"][j], embeddings2[relation_name]["t"][j]) for j in range(embeddings1[relation_name]["h"].shape[0])]
        rel_corr, _ = spearmanr(gold_scores, rel_word_score)

        print("Keller Overall: ", relation_name, "corr: %.4f  base corr:  %.4f" % (rel_corr, base_corr))

def sp10k_test_overall(sess, model, data, relation_length, restriction):
    relation_names = ["amod", "nsubj", "dobj"]
    word2id = data.word2id
    result_list = []
    for relation_name in relation_names:

        word_pairs = []
        gold_scores = []
        with open("SPA-dataset/" + relation_name + "_annotation.txt") as amod_f:
            for line in amod_f:
                line_list = line.strip().split()

                try:
                    id1 = word2id[line_list[0]]
                except:
                    # print("Not in scope ", line_list[0])
                    continue

                try:
                    id2 = word2id[line_list[1]]
                except:
                    # print("Not in scope ", line_list[1])
                    continue

                score = float(line_list[2])
                gold_scores.append(score)
                word_pairs.append([id1, id2])
        word_pairs = np.array(word_pairs)

        feed_dict = {
            model.predicate_amod_ids: word_pairs[:, 0],
            model.argument_amod_ids: word_pairs[:, 0],
            model.predicate_dobj_ids: word_pairs[:, 0],
            model.argument_dobj_ids: word_pairs[:, 0],
            model.predicate_nsubj_ids: word_pairs[:, 0],
            model.argument_nsubj_ids: word_pairs[:, 0]
        }

        embeddings1 = {"base": {},
                       "amod": {},
                       "nsubj": {},
                       "dobj": {}
                      }

        result1 = sess.run([model.base_embedding_handler,
                                model.base_embedding_context_handler,
                                model.amod_embedding_handler,
                                model.amod_embedding_context_handler,
                                model.nsubj_embedding_handler,
                                model.nsubj_embedding_context_handler,
                                model.dobj_embedding_handler,
                                model.dobj_embedding_context_handler
                                ], feed_dict = feed_dict)

        embeddings1["base"]["h"],  embeddings1["base"]["t"], embeddings1["amod"]["h"],  embeddings1["amod"]["t"], \
        embeddings1["nsubj"]["h"],  embeddings1["nsubj"]["t"], embeddings1["dobj"]["h"],  embeddings1["dobj"]["t"] = result1

        feed_dict = {
            model.predicate_amod_ids: word_pairs[:, 1],
            model.argument_amod_ids: word_pairs[:, 1],
            model.predicate_dobj_ids: word_pairs[:, 1],
            model.argument_dobj_ids: word_pairs[:, 1],
            model.predicate_nsubj_ids: word_pairs[:, 1],
            model.argument_nsubj_ids: word_pairs[:, 1]
        }

        embeddings2 = {"base": {},
                       "amod": {},
                       "nsubj": {},
                       "dobj": {}
                      }

        result2 = sess.run([model.base_embedding_handler,
                                model.base_embedding_context_handler,
                                model.amod_embedding_handler,
                                model.amod_embedding_context_handler,
                                model.nsubj_embedding_handler,
                                model.nsubj_embedding_context_handler,
                                model.dobj_embedding_handler,
                                model.dobj_embedding_context_handler
                                ], feed_dict = feed_dict)

        embeddings2["base"]["h"],  embeddings2["base"]["t"], embeddings2["amod"]["h"],  embeddings2["amod"]["t"], \
        embeddings2["nsubj"]["h"],  embeddings2["nsubj"]["t"], embeddings2["dobj"]["h"],  embeddings2["dobj"]["t"] = result2

        base_word_score = [ 1 - cosine(embeddings1["base"]["h"][j], embeddings2["base"]["t"][j]) for j in range(embeddings1["base"]["h"].shape[0])]

        base_corr, _ = spearmanr(gold_scores, base_word_score)

        rel_word_score  = [ 1 - cosine(embeddings1[relation_name]["h"][j], embeddings2[relation_name]["t"][j]) for j in range(embeddings1[relation_name]["h"].shape[0])]
        rel_corr, _ = spearmanr(gold_scores, rel_word_score)

        print("Overall ", relation_name, "corr: %.4f  base corr:  %.4f" % (rel_corr, base_corr))
        result_list.append(rel_corr)

    with open("traverse/sp10k_result_l_%d_r_%.3f.csv "%(relation_length, restriction), "a+") as fout:
        fout.write("%.4f,%.4f,%.4f\n" % tuple(result_list))

def ws_test(sess, model, data, simlex_corps, num_cross, relation_length, restriction):

    word2id = data.word2id

    corp_names = ["v", "n", "adj", "all"]
    scores = {}

    # with open("ws_gensim_result_c_%d_l_%d_r_%.3f.p "%(int(num_cross), relation_length, restriction), "wb") as fout:

    for i in range(len(simlex_corps)):
        simlex_corpora = simlex_corps[i]

        id_score_triplets = []
        gold_scores = []
        for words_score_trplet in simlex_corpora:
            try:
                context1 = word2id[words_score_trplet[0]]
                context2 = word2id[words_score_trplet[1]]
                gold_score = words_score_trplet[2]
                gold_scores.append(gold_score)

                id_score_triplets.append([context1, context2])
            except:
                pass
        id_score_triplets = np.array(id_score_triplets)

        feed_dict = {
            model.predicate_amod_ids: id_score_triplets[:, 0],
            model.argument_amod_ids: id_score_triplets[:, 0],
            model.predicate_dobj_ids: id_score_triplets[:, 0],
            model.argument_dobj_ids: id_score_triplets[:, 0],
            model.predicate_nsubj_ids: id_score_triplets[:, 0],
            model.argument_nsubj_ids: id_score_triplets[:, 0]
        }

        embeddings1 = {"base": {},
                       "amod": {},
                       "nsubj": {},
                       "dobj": {}
                      }

        result1 = sess.run([model.base_embedding_handler,
                                model.base_embedding_context_handler,
                                model.amod_embedding_handler,
                                model.amod_embedding_context_handler,
                                model.nsubj_embedding_handler,
                                model.nsubj_embedding_context_handler,
                                model.dobj_embedding_handler,
                                model.dobj_embedding_context_handler
                                ], feed_dict = feed_dict)

        embeddings1["base"]["h"],  embeddings1["base"]["t"], embeddings1["amod"]["h"],  embeddings1["amod"]["t"], \
        embeddings1["nsubj"]["h"],  embeddings1["nsubj"]["t"], embeddings1["dobj"]["h"],  embeddings1["dobj"]["t"] = result1

        feed_dict = {
            model.predicate_amod_ids: id_score_triplets[:, 1],
            model.argument_amod_ids: id_score_triplets[:, 1],
            model.predicate_dobj_ids: id_score_triplets[:, 1],
            model.argument_dobj_ids: id_score_triplets[:, 1],
            model.predicate_nsubj_ids: id_score_triplets[:, 1],
            model.argument_nsubj_ids: id_score_triplets[:, 1]
        }

        embeddings2 = {"base": {},
                       "amod": {},
                       "nsubj": {},
                       "dobj": {}
                      }

        result2 = sess.run([model.base_embedding_handler,
                                model.base_embedding_context_handler,
                                model.amod_embedding_handler,
                                model.amod_embedding_context_handler,
                                model.nsubj_embedding_handler,
                                model.nsubj_embedding_context_handler,
                                model.dobj_embedding_handler,
                                model.dobj_embedding_context_handler
                                ], feed_dict = feed_dict)

        embeddings2["base"]["h"],  embeddings2["base"]["t"], embeddings2["amod"]["h"],  embeddings2["amod"]["t"], \
        embeddings2["nsubj"]["h"],  embeddings2["nsubj"]["t"], embeddings2["dobj"]["h"],  embeddings2["dobj"]["t"] = result2

        for key in embeddings1.keys():
            embeddings1[key]["h+t"] = embeddings1[key]["h"] + embeddings1[key]["t"]
            embeddings1[key]["[h,t]"] = np.concatenate((embeddings1[key]["h"],  embeddings1[key]["t"]), axis = 1)

        for key in embeddings2.keys():
            embeddings2[key]["h+t"] = embeddings2[key]["h"] + embeddings2[key]["t"]
            embeddings2[key]["[h,t]"] = np.concatenate((embeddings2[key]["h"],  embeddings2[key]["t"]), axis = 1)

        relation_result = {}

        for relation_name in embeddings1.keys():
            types_result = {}
            for type_name in embeddings1[relation_name].keys():

                model_predition_score = [ 1 - cosine(embeddings1[relation_name][type_name][j], embeddings2[relation_name][type_name][j]) for j in range(embeddings1[relation_name][type_name].shape[0])]

                r, p = spearmanr(gold_scores, model_predition_score)
                types_result[type_name] = r

            relation_result[relation_name] = types_result
            print(corp_names[i], relation_name, types_result)
        scores[corp_names[i]] = relation_result
        # pickle.dump(scores, fout)

    return scores

def sd_test(sess, model, data, batch_size, c, l, r):
    batch = data.get_sd_test_batch(batch_size)

    feed_dict = {
                    model.predicate_amod_ids: batch["amod"][:, 0],
                    model.argument_amod_ids: batch["amod"][:, 1],
                    model.argument_prime_amod_ids: batch["amod"][:, 2],

                    model.predicate_nsubj_ids: batch["nsubj"][:, 0],
                    model.argument_nsubj_ids: batch["nsubj"][:, 1],
                    model.argument_prime_nsubj_ids: batch["nsubj"][:, 2],

                    model.predicate_dobj_ids: batch["dobj"][:, 0],
                    model.argument_dobj_ids: batch["dobj"][:, 1],
                    model.argument_prime_dobj_ids: batch["dobj"][:, 2],
                }

    pos_amod, pos_nsubj, pos_dobj, neg_amod, neg_nsubj, neg_dobj= sess.run([model.pred_amod, model.pred_nsubj, model.pred_dobj,
        model.pred_amod_prime, model.pred_nsubj_prime, model.pred_dobj_prime], feed_dict=feed_dict)

    # with open("pd_gensim_result_c_%d_l_%d_r_%.3f.p "%(int(c), l, r), "wb") as fout:

    amod_acc = np.sum(pos_amod > neg_amod)/ batch_size
    nsubj_acc = np.sum(pos_nsubj > neg_nsubj) / batch_size
    dobj_acc = np.sum(pos_dobj > neg_dobj) / batch_size
    total_acc = (np.sum(pos_amod > neg_amod) + np.sum(pos_nsubj > neg_nsubj) + np.sum(pos_dobj > neg_dobj))/ (batch_size * 3)

    #     # pickle.dump({"amod": amod_acc, "nsubj": nsubj_acc, "dobj": dobj_acc, "overall": total_acc}, fout)
    print("sd scores:", amod_acc, nsubj_acc, dobj_acc, total_acc)

    return amod_acc, nsubj_acc, dobj_acc, total_acc

def main():

    parser = argparse.ArgumentParser()
    parser.add_argument("-p", "--pretrain", action="store_true")
    parser.add_argument("-t", "--center_trainable", action="store_true")
    parser.add_argument("-w", "--weight", type=float, default="0.5")
    parser.add_argument("-r", "--restrict", type=float, default="1")
    parser.add_argument("-c", "--cross_validate", type=int, default=0)
    parser.add_argument("-g", "--GPU", type=str, default="1")
    parser.add_argument("-e", "--num_epoch", type=int, default=20)
    parser.add_argument("-l", "--relational_embedding_size", type=int, default=10)
    args = parser.parse_args()
    print(args)

    os.environ["CUDA_VISIBLE_DEVICES"]=args.GPU
    statsPath = "data/2018_corpus_stats.pkl"

    num_cross = str(args.cross_validate)

    train_data = DataLoader()
    train_data.read_stats(statsPath)
    train_data.load_pairs_counting("data/train_count.pkl")
    train_data.load_argument_sample_table("data/argument_sample_table.p")

    test_data = DataLoader()
    test_data.read_stats(statsPath)
    test_data.load_pairs_counting("data/test_count.pkl")
    test_data.load_argument_sample_table("data/argument_sample_table.p")

    modelPath = "data/stage_one.model"
    word2vecModel= Word2Vec.load(modelPath)

    context_wv = KeyedVectors(vector_size=300)
    context_wv.vocab = word2vecModel.wv.vocab
    context_wv.index2word = word2vecModel.wv.index2word
    context_wv.syn0 = word2vecModel.syn1neg

    pretrain_center_emb = list()
    pretrain_context_emb = list()

    counter = 0

    for i in range(len(train_data.id2word)):
        tmp_w = train_data.id2word[i]
        if tmp_w in context_wv.vocab:
            pretrain_center_emb.append(word2vecModel[tmp_w])
            pretrain_context_emb.append(context_wv[tmp_w])
        else:
            pretrain_center_emb.append(np.zeros(300))
            pretrain_context_emb.append(np.zeros(300))
            counter += 1

    print("empty count", counter)

    pretrain_center_emb = np.asarray(pretrain_center_emb)

    wordsim_dir = "./Word-similarity-dataset/Simlex/"

    with open(wordsim_dir + "verb.json", "r") as f:
        verb_list = json.load(f)

    with open(wordsim_dir + "noun.json", "r") as f:
        noun_list = json.load(f)

    with open(wordsim_dir + "adjective.json", "r") as f:
        adjective_list = json.load(f)

    with open(wordsim_dir + "all.json", "r") as f:
        all_list = json.load(f)

    simlex_corpora = [verb_list, noun_list, adjective_list, all_list]
    simlex_names = ["verb_list", "noun_list", "adjective_list", "all_list"]

    m = Model(train_data, args)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        if args.pretrain:

            # Initial assign
            sess.run(m.emb_init, feed_dict={m.emb_placeholder: pretrain_center_emb})
            sess.run(m.emb_init_context , feed_dict={m.emb_placeholder_context: pretrain_context_emb})

        ws_test(sess, m, test_data, simlex_corpora, num_cross, args.relational_embedding_size, args.restrict)
        sp10k_test_overall(sess, m, train_data, args.relational_embedding_size, args.restrict)
        test_keller_overall(sess, m, train_data)

        num_epoch = args.num_epoch
        num_batch = 256

        batch_size = 1024
        for epoch in range(num_epoch):

            print(" epoch:", str(epoch + 1), "/", num_epoch)

            process_bar = tqdm(range(num_batch))
            for i in process_bar:
                batch = train_data.get_sd_train_batch(batch_size)

                feed_dict = {
                    m.predicate_amod_ids: batch["amod"][:, 0],
                    m.argument_amod_ids: batch["amod"][:, 1],
                    m.argument_prime_amod_ids: batch["amod"][:, 2],

                    m.predicate_nsubj_ids: batch["nsubj"][:, 0],
                    m.argument_nsubj_ids: batch["nsubj"][:, 1],
                    m.argument_prime_nsubj_ids: batch["nsubj"][:, 2],

                    m.predicate_dobj_ids: batch["dobj"][:, 0],
                    m.argument_dobj_ids: batch["dobj"][:, 1],
                    m.argument_prime_dobj_ids: batch["dobj"][:, 2],
                }

                loss, _ = sess.run([m.loss, m.optimize], feed_dict=feed_dict)

                process_bar.set_description("Loss: %0.4f" % loss)

            # test
            ws_test(sess, m, test_data, simlex_corpora, num_cross, args.relational_embedding_size, args.restrict)
            sp10k_test_overall(sess, m, train_data, args.relational_embedding_size, args.restrict)
            test_keller_overall(sess, m, train_data)

        # save the center, relational, and transpose matrix

        # center_emb, amod_emb, nsubj_emb, dobj_emb = sess.run([m.center_emb, m.amod_emb, m.nsubj_emb, m.dobj_emb])
        # center_emb_context, amod_emb_context, nsubj_emb_context, dobj_emb_context = sess.run([m.center_emb_context, m.amod_emb_context, m.nsubj_emb_context, m.dobj_emb_context])

        # with open('center_embedding_add_context.txt', 'w') as file_:
        #     for i in range(train_data.vocab_size):
        #       embed = center_emb[i, :] + center_emb_context[i, :]
        #       word = train_data.id2word[i]
        #       file_.write('%s %s\n' % (word, ' '.join(map(str, embed))))

        # with open('center_embedding_concat_context.txt', 'w') as file_:
        #     for i in range(train_data.vocab_size):
        #       embed = np.concatenate((center_emb[i, :], center_emb_context[i, :]), axis=0)
        #       word = train_data.id2word[i]
        #       file_.write('%s %s\n' % (word, ' '.join(map(str, embed))))

        # with open('all_embedding_add_context.txt', 'w') as file_:
        #     for i in range(train_data.vocab_size):
        #       embed = np.concatenate(((center_emb[i, :] + center_emb_context[i, :]), (amod_emb[i, :] + amod_emb_context[i, :]), (nsubj_emb[i, :] + nsubj_emb_context[i, :]), (dobj_emb[i, :] + dobj_emb_context[i, :])), axis=0)
        #       word = train_data.id2word[i]
        #       file_.write('%s %s\n' % (word, ' '.join(map(str, embed))))

        # with open('all_embedding_concat_context.txt', 'w') as file_:
        #     for i in range(train_data.vocab_size):
        #       embed = np.concatenate((center_emb[i, :], amod_emb[i, :], nsubj_emb[i, :], dobj_emb[i, :], center_emb_context[i, :], amod_emb_context[i, :], nsubj_emb_context[i, :], dobj_emb_context[i, :]), axis=0)
        #       word = train_data.id2word[i]
        #       file_.write('%s %s\n' % (word, ' '.join(map(str, embed))))

if __name__ == "__main__":
    main()


continue_program = "y"
while continue_program == "y":
    student_name = input("Enter Your name: ")
    print("Enter Your grades in 3 subjects: ")
    grade1 = int(input())
    grade2 = int(input())
    grade3 = int(input())
    array = [grade1, grade2, grade3]

    average = round((grade1 + grade2 + grade3 / 3))
    arr = []
    if 0 < average <= 100:
        arr.append(student_name)
        arr.append(array)
        arr.append(average)
        print(arr)
        continue_program = input("Do you want to continue? ('y/n)")
        if continue_program == "y":
            continue
            print(arr)


from random import randint

from kavenegar import *

api = KavenegarAPI('3264344264755A6C6F3352492F71684D50616A455930764D714B68634F784A544D55436C76684348715A453D')

def random_number_generator():
    rand_num = randint(10000, 99999)
    return rand_num

def send_message(phone_number, message, random_number):
    full_message = message + str(random_number)
    try:
        params = { 'sender' : '1000596446', 'receptor': phone_number, 'message' :full_message }
        response = api.sms_send( params)
        print(response)
    except APIException as e:
        print(e)
    except HTTPException as e:
        print(e)
    return random_number


# Pilotes, arista
N, M = map(int, input().strip().split())

# a, b
grafo = [[] for i in range(N)]
for i in range(M):
    a, b = map(int, input().strip().split())
    grafo[a].append(b)
    grafo[b].append(a)

visitados = set()
nodo = 0
cucharadas = 0
while nodo < N:
    if nodo not in visitados:
        cola = []
        cola.append(nodo)
        visitados.add(nodo)
        while len(cola) > 0:
            n = cola.pop()
            for a in grafo[n]:
                if a not in visitados:
                    visitados.add(a)
                    cola.append(a)
        cucharadas += 1
    nodo += 1
print(cucharadas)


#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""API Contract Test Case for divisions endpoint."""
import logging
from typing import Union

import pytest
import requests
from jsonschema import validate

from trafficops.tosession import TOSession

# Create and configure logger
logger = logging.getLogger()

Primitive = Union[bool, int, float, str, None]

def test_division_contract(to_session: TOSession,
	response_template_data: list[Union[dict[str, object], list[object], Primitive]],
	division_post_data: dict[str, object]) -> None:
	"""
	Test step to validate keys, values and data types from divisions endpoint
	response.
	:param to_session: Fixture to get Traffic Ops session.
	:param response_template_data: Fixture to get response template data from a prerequisites file.
	:param division_post_dat: Fixture to get sample division data and actual division response.
	"""
	# validate division keys from divisions get response
	logger.info("Accessing divisions endpoint through Traffic ops session.")

	division_name = division_post_data.get("name")
	if not isinstance(division_name, str):
		raise TypeError("malformed cdn in prerequisite data; 'name' not a string")

	division_get_response: tuple[
		Union[dict[str, object], list[Union[dict[str, object], list[object], Primitive]], Primitive],
		requests.Response
	] = to_session.get_divisions(query_params={"name": division_name})
	try:
		division_data = division_get_response[0]
		if not isinstance(division_data, list):
			raise TypeError("malformed API response; 'response' property not an array")

		first_division = division_data[0]
		if not isinstance(first_division, dict):
			raise TypeError("malformed API response; first division in response is not an dict")

		logger.info("Division Api get response %s", first_division)
		division_response_template = response_template_data.get("divisions")
		if not isinstance(division_response_template, dict):
			raise TypeError(
				f"Division response template data must be a dict, not '{type(division_response_template)}'")

		# validate division values from prereq data in divisions get response.
		prereq_values = division_post_data["name"]
		get_values = first_division["name"]

		assert validate(instance=first_division, schema=division_response_template) is None
		assert get_values == prereq_values
	except IndexError:
		logger.error("Either prerequisite data or API response was malformed")
		pytest.fail("Failed due to malformation")


from setuptools import setup, find_packages
import os

_home = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'Products', 'AttachmentField')

version = open(os.path.join(_home, 'version.txt')).read().strip()

setup(
    name='Products.AttachmentField',
    version=version,
    description="AttachmentField/Widget for Plone",
    long_description=(open(os.path.join(_home, "README.txt")).read() +
                      "\n\n" +
                      open(os.path.join(_home, "CHANGES")).read()),
    # Get more strings from http://www.python.org/pypi?%3Aaction=list_classifiers
    classifiers=[
        "Programming Language :: Python",
        "Framework :: Plone",
        "Topic :: Software Development :: Libraries :: Python Modules",
        ],
    keywords='Plone',
    author='Ingeniweb',
    author_email='support@ingeniweb.com',
    maintainer='Alex Clark',
    maintainer_email='aclark@aclark.net',
    url='http://github.com/collective/Products.AttachmentField',
    license='GPL',
    packages=find_packages(exclude=['ez_setup']),
    namespace_packages=['Products'],
    include_package_data=True,
    zip_safe=False,
    install_requires=[
        'setuptools',
        # -*- Extra requirements: -*-
        ],
    entry_points="""
    # -*- Entry points: -*-
    """,
    )