import numpy as np; np.random.seed(1)
from scipy.linalg import solve_discrete_lyapunov, eigh
from scipy.optimize import fixed_point
from control import dlyap
from math import exp
from time import time
from scipy.sparse.linalg import svds

#coder graph labélisés
class Kernel:

    def __init__(self, lbd, k=None):
        self.lbd = lbd
        self.mu = lambda x: pow(self.lbd,x)
        # k is used in different kernels differently, usually number of loops of algorithms
        self.k = k
        # computation time
        self.comp_time = 0

    def kron(self, A1, A2):
        if type(A1)==np.matrix or type(A1)==np.ndarray:
            return np.kron(A1,A2)
        else:
            Wx = np.kron(A1[0],A2[0])
            for i in range(1,len(A1)):
                Wx = Wx + np.kron(A1[i],A2[i])
            return Wx

    def shape(self, A):
        if type(A)==np.matrix or type(A)==np.ndarray:
            return A.shape
        else:
            return A[0].shape

    def conjugate_grad(self,A, b, x=None):
        """
        Description
        -----------
        Solve a linear equation Ax = b with conjugate gradient method.
        Parameters
        ----------
        A: 2d numpy.array of positive semi-definite (symmetric) matrix
        b: 1d numpy.array
        x: 1d numpy.array of initial point
        Returns
        -------
        1d numpy.array x such that Ax = b
        Source : https://gist.github.com/sfujiwara/b135e0981d703986b6c2
        """
        n = len(b)
        if x is None:
            x = np.ones(n)
        r = np.dot(A, x) - b
        p = - r
        r_k_norm = np.dot(r.T, r)
        for i in range(self.k):
            Ap = np.dot(A, p)
            alpha = r_k_norm / np.dot(p.T, Ap)
            alpha = np.asscalar(alpha)
            x += alpha * p
            r += alpha * Ap
            r_kplus1_norm = np.dot(r.T, r)
            beta = r_kplus1_norm / r_k_norm
            beta = np.asscalar(beta)
            r_k_norm = r_kplus1_norm
            if r_kplus1_norm < 1e-5:
                #print('Itr:', i+1)
                break
            p = beta * p - r
        return x

    def raw_kernel(self, A1, A2):
        Wx = self.kron(A1,A2)
        n = Wx.shape[0]
        px = np.ones((n,1))/n
        qx = np.ones((n,1))/n
        return np.sum([self.mu(k) * qx.T @ np.power(Wx,k) @ px for k in range(self.N)])/(self.N)

    def inv_kernel(self, A1, A2):
        # (16)
        Wx = self.kron(A1,A2)
        n = Wx.shape[0]
        m = len(Wx.nonzero()[0])
        px = np.ones((n,1))/n
        qx = np.ones((n,1))/n
        return qx.T @ np.linalg.inv(np.identity(n)-self.lbd*Wx)@px

    #ne fonctionne pas quand matrices singulieres
    def sylv_eq_kernel(self, A1, A2):
        """
        Sylvester equation Methods (lyapunov)
        O(n^3)
        for graph with no labels only : else implement "COMPUTATION OF THE CANONICAL DECOMPOSITION BYMEANS OF A SIMULTANEOUS GENERALIZED SCHURDECOMPOSITION∗"
        with https://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.linalg.qz.html
        """
        #https://python-control.readthedocs.io/en/0.8.0/generated/control.dlyap.html
        #resoudre AXQt - X + C
        n1 = A1.shape[0]
        n2 = A2.shape[0]
        #in case the matrix is empty i add a diagonal, works great
        A1 = (A1+A1.T)/2 + np.eye(n1)*1e1
        A2 = (A2+A2.T)/2 + np.eye(n2)*1e1
        n = A1.shape[0]*A2.shape[0]
        px = np.ones((n,1))/n
        qx = np.ones((n,1))/n
        M = dlyap(A=self.lbd*A1,Q=A2,C=px.reshape((A1.shape[0],A2.shape[0])))
        return -1*np.asscalar(qx.T @ M.reshape((-1,1)))

    def conj_grad_kernel(self, A1, A2):
         """
         Conjugate Gradient Methods
         O(n^3)
         for graph with discrete edge labels
         """
         Wx = self.kron(A1,A2)
         n = Wx.shape[0]
         I = np.identity(n)
         M = I-self.lbd*Wx
         assert M.shape==Wx.shape
         px = np.ones((n,1))/n
         qx = np.ones((n,1))/n
         # donner M essentiel simplifier M=inv(M)
         v = np.random.randint(0,n,size=(n,1))
         #x,_ = cg(M,px,x0=px,M=v@v.T,maxiter=self.k)
         x = self.conjugate_grad(M,px,px)
         return qx.T@x

    def fixed_point_kernel(self, A1, A2):
        Wx = self.kron(A1,A2)
        n = Wx.shape[0]
        m = len(Wx.nonzero()[0])
        px = np.ones((n,1))/n
        qx = np.ones((n,1))/n
        #diagonaliser
        Wx = (Wx + Wx.T)/2

        # if self.lbd >= 1/abs(eigh(Wx,eigvals_only=True,eigvals=(n-1,n-1))[0]):
        #     print("Cannot converge")
        #     raise ValueError()

        func = lambda x: np.asarray(px+(self.lbd*Wx)@x)
        #x = fixed_point(func, np.asarray(px),maxiter=1500)

        x0 = np.asarray(px)
        itera = self.k if self.k is not None else 1000
        last = -99999999999
        x = x0
        for i in range(itera):
            last = x
            x = func(x)
            if np.linalg.norm(x-last)<=1e-5:
                break

        k = np.real(np.asscalar(qx.T@x))
        return k

    def spec_decomp_kernel(self, A1, A2):
        # Wx = self.kron(A1,A2)
        # #diagonaliser Wx
        # #Wx = (Wx + Wx.T)/2
        # Dx,Px = np.linalg.eig(Wx)
        # # real = np.isreal(Dx)
        # # print("Px shape before",Px.shape)
        # # Dx = Dx[np.where(real==True)]
        # # Px = np.delete(Px, np.where(real==False),axis=0)
        # # Px = np.delete(Px, np.where(real==False),axis=1)
        # # print("Px shape after",Px.shape)

        # # matrice singuliere pour graphe staR ?
        # if np.linalg.det(Px)==0:
        #     return

        # Dx = np.diag(Dx)
        # Px1 = np.linalg.inv(Px)

        # n = Dx.shape[0]
        # m = len(Wx.nonzero()[0])
        # px = np.ones((n,1))/self.N
        # qx = np.ones((n,1))/self.N
        # k = qx.T @ Px @ np.linalg.inv(np.identity(n)-self.lbd*Dx) @ Px1 @ px
        # k = np.real(np.asscalar(k))
        n1 = A1.shape[0]
        n2 = A2.shape[0]
        #in case the matrix is empty i add a diagonal, works great
        A1 = (A1+A1.T)/2 + np.eye(n1)
        A2 = (A2+A2.T)/2 + np.eye(n2)
        # check_sym = lambda a : np.allclose(a, a.T, rtol=1e-05, atol=1e-08)
        # assert check_sym(A1)==True
        # assert check_sym(A2)==True
        n1 = A1.shape[0]
        n2 = A2.shape[0]
        D1,P1 = np.linalg.eig(A1)
        D2,P2 = np.linalg.eig(A2)

        if not np.all(np.isreal(D1)) or not np.all(np.isreal(D2)):
            return 0

        D1 = np.diag(D1)
        D2 = np.diag(D2)
        Pinv1 = np.linalg.inv(P1)
        Pinv2 = np.linalg.inv(P2)

        p1 = np.ones((n1,1))/n1
        q1 = np.ones((n1,1))/n1
        p2 = np.ones((n2,1))/n2
        q2 = np.ones((n2,1))/n2

        # always using an exponential kernel
        part1 = self.kron(q1.T@P1,q2.T@P2)
        part2 = 1/np.diag(np.identity(n1*n2)-self.lbd*np.kron(D1,D2))
        part2[np.isnan(part2)]=0
        part2=np.diag(part2)
        part3 = self.kron(Pinv1@p1,Pinv2@p2)
        k = part1 @ part2 @part3
        return k

    def build_gram_matrix(self, db, kernel, nkp=False):
        try:
            self.N = np.max([x.shape[0] for x in db])
        except:
            self.N = np.max([x[0].shape[0] for x in db])
        self.comp_time = time()
        gram = np.empty((len(db),len(db)))
        for i in range(len(db)):
            for j in range(i+1):
                A = db[i] # np.copy() ?
                B = db[j]
                if nkp:
                    A,B,_ = self.nkp(self.kron(A,B), self.shape(A), self.shape(B), hermit=False)
                ker = kernel(A,B)
                gram[i, j] = ker
                if i != j:
                    gram[j, i] = ker
        self.comp_time = time() - self.comp_time
        # Normalisation
        gram -= np.ones(gram.shape)*np.min(gram)
        gram /= np.max(gram)
        return gram

    #optimiser
    def build_gram_matrix_nonsq(self, X, Z, kernel):
        self.N = np.max([x.shape[0] for x in X])
        gram = np.empty((len(X),len(Z)))
        for i in range(len(X)):
            for j in range(len(Z)):
                A = X[i]
                B = Z[j]
                ker = kernel(A,B)
                gram[i, j] = ker
        gram -= np.ones(gram.shape)*np.min(gram)
        gram /= np.max(gram)
        return gram

    def scale_and_compare(self,M1,M2,normalize=True):
        if normalize == True:
            M1 = np.copy(M1)
            M1 -= np.ones(M1.shape)*np.min(M1)
            M1 /= np.max(M1)
            M2 = np.copy(M2)
            M2 -= np.ones(M2.shape)*np.min(M2)
            M2 /= np.max(M2)
        return np.linalg.norm(M1-M2)/(M1.shape[0]*M1.shape[1])

    def nkp(self, A, bdim, cdim, hermit=False):
        A = np.asarray(A)
        m = A.shape[0]
        n = A.shape[1]
        m1 = bdim[0]
        n1 = bdim[1]
        m2 = cdim[0]
        n2 = cdim[1]

        assert m == m1 * m2
        assert n == n1 * n2

        if hermit:
            assert m1 == n1
            assert m2 == n2
            A = 0.5 * ( A + A.T)

        R = A.reshape((m2, m1, n2, n1))
        R = np.transpose(R, (1, 3, 0, 2))
        R = R.reshape((m1*n1, m2*n2))

        B, S, C  =  svds(R,1)

        SqrtS = np.sqrt(np.asscalar(S))

        B = (B * SqrtS).reshape((m1, n1))
        C = (C * SqrtS).reshape((m2, n2))

        if hermit:
            B = 0.5 * ( B + B.T)
            C = 0.5 * ( C + C.T)

            if np.all(np.diag( B ) < 0) and np.all(np.diag( C ) < 0):
                B = -B
                C = -C

        D = A - np.kron(B,C)
        if hermit:
            D = 0.5 * ( D + D.T)
        return B, C, np.linalg.norm(D)/(m*n)


import customtkinter

customtkinter.set_appearance_mode("System") # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("kac.json")  # Themes: "blue" (standard), "green", "dark-blue"

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        # Window/App Customization
        self.geometry("500x250")
        self.title("Starter Code Example")

        # Grid Configuration
        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(0, weight=1)
        self.grid_rowconfigure(1, weight=1)

        # Widgets
        label = customtkinter.CTkLabel(self, text="Hello Blog World!", font=("Arial", 25), anchor="center")
        button = customtkinter.CTkButton(self, text="Click Me!")

        # Placing Widgets on Grid
        label.grid(row=0, column=0)
        button.grid(row=1, column=0)

app = App()
app.mainloop()


#!/usr/bin/env python3

import pandas as pd
from datasets import load_dataset
from datasets import Dataset, DatasetDict
import matplotlib.pyplot as plt
from functools import partial
from brownian_motion import geometric_brownian_motion
import datetime

# dataset = load_dataset("monash_tsf", "tourism_monthly")
# print(dataset)

# Generate a list of 100 values
sample_data = geometric_brownian_motion(1, 252, 0.15, 0.2, 100)
prediction_len = 20
train_dataset = Dataset.from_dict({"target": sample_data[0 : -prediction_len * 2]})
test_dataset = Dataset.from_dict({"target": sample_data[0:-prediction_len]})
validation_dataset = Dataset.from_dict({"target": sample_data[0::]})

dataset = DatasetDict(
    {"train": train_dataset, "test": test_dataset, "validation": validation_dataset}
)

figure, axes = plt.subplots()
axes.plot(train_dataset["target"], color="blue")
axes.plot(validation_dataset["target"], color="red", alpha=0.5)
plt.show()

freq = "1D"
now = datetime.datetime.now()
start = now - datetime.timedelta(days=100)
start = pd.Period(start, freq)
print(start)


import tensorflow as tf

"""
!wget --no-check-certificate \
    https://storage.googleapis.com/laurencemoroney-blog.appspot.com/horse-or-human.zip \
    -O /tmp/horse-or-human.zip
"""

model = tf.keras.models.Sequential([
    # Note the input shape is the desired size of the image 300x300 with 3 bytes color
    # This is the first convolution
    tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(300, 300, 3)),
    tf.keras.layers.MaxPooling2D(2, 2),
    # The second convolution
    tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # The third convolution
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # The fourth convolution
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # The fifth convolution
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # Flatten the results to feed into a DNN
    tf.keras.layers.Flatten(),
    # 512 neuron hidden layer
    tf.keras.layers.Dense(512, activation='relu'),
    # Only 1 output neuron. It will contain a value from 0-1 where 0 for 1 class ('horses') and 1 for the other ('humans')
    tf.keras.layers.Dense(1, activation='sigmoid')
])
model.summary()

from tensorflow.keras.optimizers import RMSprop
model.compile(loss='binary_crossentropy',
              optimizer=RMSprop(lr=0.001),
              metrics=['accuracy'])

from tensorflow.keras.preprocessing.image import ImageDataGenerator
# All images will be rescaled by 1./255
train_datagen = ImageDataGenerator(rescale=1/255)
# Flow training images in batches of 128 using train_datagen generator
train_generator = train_datagen.flow_from_directory(
        'C:\study\MachineLearning\TrainingData\horse-or-human',  # This is the source directory for training images
        target_size=(300, 300),  # All images will be resized to 150x150
        batch_size=128,
        # Since we use binary_crossentropy loss, we need binary labels
        class_mode='binary')

history = model.fit(
      train_generator,
      steps_per_epoch=8,
      epochs=15,
      verbose=1)


import postprocess
import numpy as np
import math
import cv2
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw

def order_points(pts): # format: (x, y)
    # initialzie a list of coordinates that will be ordered
    # such that the first entry in the list is the top-left,
    # the second entry is the top-right, the third is the
    # bottom-right, and the fourth is the bottom-left
    rect = np.zeros((4, 2), dtype = "float32")

    # the top-left point will have the smallest sum, whereas
    # the bottom-right point will have the largest sum
    s = pts[...,0]**2 + pts[...,1]**2

    lr = np.argmin(s)
    rd = np.argmax(s)

    rect[0] = pts[lr]
    rect[2] = pts[rd]

    mask = np.ones(4, dtype=np.bool)
    mask[lr] = False
    mask[rd] = False

    pts = pts[mask]

    rect[1] = pts[np.argmax(pts[...,0])] # upper-right
    rect[3] = pts[np.argmin(pts[...,0])] # lower-left

    # return the ordered coordinates
    return rect
def three_pts_to_four_pts(pts): # (3, 2)
    apts = np.append(pts[-1:], pts, axis=0) # (4, 2)
    bpts = np.append(pts, pts[0:1], axis=0) # (4, 2)
    a2b = apts-bpts # (4, 2)
    dists = np.sqrt(np.sum((a2b)**2, axis=-1)) # (4,)
    max_dist = np.argmax(dists)
    a = max_dist-1
    b = max_dist
    mask = np.ones(3,dtype=np.bool)
    mask[[a,b]] = False
    c = np.squeeze(pts[mask]) # 1 point
    a, b = pts[a], pts[b]
    m = 0.5 * (a+b)
    v = m - c
    d = m + v
    return np.array([a, c, b, d], dtype=np.float32)

def robust_unwarp(image, pts):
    if len(pts)<3: # too less points
        return image # give up!
    elif len(pts)==3: # hmmmmmmmmm
        pts = three_pts_to_four_pts(pts)
        return unwarp(image, pts) # no problem
    else:
        return unwarp(image, pts) # no problem

def unwarp(image, pts): # format: (y,x)
    # obtain a consistent order of the points and unpack them
    # individually
    rect = order_points(pts[...,::-1]) # format: (x,y)
    # print(rect)
    (tl, tr, br, bl) = rect

    # compute the width of the new image, which will be the
    # maximum distance between bottom-right and bottom-left
    # x-coordiates or the top-right and top-left x-coordinates
    widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
    widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
    # aveWidth  = int((widthA+widthB)//2) # max(int(widthA), int(widthB))
    aveWidth  = int(math.sqrt(widthA*widthB))

    # compute the height of the new image, which will be the
    # maximum distance between the top-right and bottom-right
    # y-coordinates or the top-left and bottom-left y-coordinates
    heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
    heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
    # aveHeight = int((heightA+heightB)//2) # max(int(heightA), int(heightB))
    aveHeight = int(math.sqrt(heightA*heightB))

    # now that we have the dimensions of the new image, construct
    # the set of destination points to obtain a "birds eye view",
    # (i.e. top-down view) of the image, again specifying points
    # in the top-left, top-right, bottom-right, and bottom-left
    # order
    dst = np.array([
        [0, 0],
        [aveWidth - 1, 0],
        [aveWidth - 1, aveHeight - 1],
        [0, aveHeight - 1]], dtype = "float32")

    # compute the perspective transform matrix and then apply it
    M = cv2.getPerspectiveTransform(rect, dst)
    warped = cv2.warpPerspective(image, M, (aveWidth, aveHeight), flags=cv2.INTER_AREA)

    # M = cv2.getAffineTransform(rect[:3], dst[:3])
    # warped = cv2.warpAffine(image, M, (aveWidth, aveHeight))

    # return the warped image
    return warped

if __name__ == '__main__':
    from skimage.io import imread
    from skimage.draw import circle
    import matplotlib.pyplot as plt
    points = np.array([
                        [10, 20 ], # lu
                        # [20, 100], # ru
                        [100,120], # rd
                        [100, 20], # ld
                      ])
    test_img = np.zeros((192,128), dtype=np.uint8)
    for p in points:
        rr, cc = circle(*p, 5, shape=(192,128))
        test_img[rr, cc] = 1
    points = postprocess.find_corner_condidate(test_img)
    img = Image.fromarray(test_img*255)
    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype("assets/calibri.ttf", 18)
    draw.text((50, 50),"hi",255,font=font)
    test_img = np.array(img)
    w = robust_unwarp(test_img, points)
    plt.imshow(test_img)
    plt.show()
    plt.imshow(w)
    plt.show()


from colorthief import ColorThief
import color_utils

def get_main_colors(filename):
  ct = ColorThief(filename)
  pallete = ct.get_palette(color_count=2)
  return pallete

def get_main_color(filename):
  ct = ColorThief(filename)
  color_rgb = ct.get_color()
  return color_rgb

def get_complementary_color(hls):
  return normalise_degrees(hls[0] - 0.5), hls[1], hls[2]

def get_split_complementary_colors(hls):
  return get_colors_by_percents(hls, 0.42)

def get_triad_colors(hls):
  return get_colors_by_percents(hls, 0.33)

def get_tint_colors(hls):
  first = normalise_percents(hls[1] + 0.3)
  second = normalise_percents(hls[1] + 0.6)
  first_color_hex = get_specified_color_hex(hls[0], first, first)
  second_color_hex = get_specified_color_hex(hls[0], second, second)
  return [first_color_hex, second_color_hex]

def get_colors_by_percents(hls, percent):
  first_color_hex = get_specified_color_hex(
      normalise_degrees(hls[0] - percent), hls[1], hls[2])
  second_color_hex = get_specified_color_hex(
      normalise_degrees(hls[0] - 1 + percent), hls[1],
      hls[2])
  return [first_color_hex, second_color_hex]

def normalise_degrees(degree):
  if degree >= 1:
    return degree - 1
  if degree < 0:
    return degree + 1
  return degree

def normalise_percents(percent):
  if percent >= 1:
    return percent - 0.9
  if percent < 0:
    return percent + 0.9
  return percent

def get_specified_color_hex(h_old, l_old, s_old):
  h = h_old * 360
  l = l_old * 100
  s = s_old * 100
  if s > 10:
    if 15 <= h < 45:
      if 0 <= l < 33:
        return "#7B3F00"
      elif 33 <= l < 66:
        return "#FF4F00"
      elif 66 <= l < 100:
        return "#FF7F50"
    elif 45 <= h < 75:
      if 0 <= l < 33:
        return "#806B2A"
      elif 33 <= l < 66:
        return "#FFFF00"
      elif 66 <= l < 100:
        return "#FFFF99"
    elif 75 <= h < 105:
      if 0 <= l < 33:
        return "#2A5C03"
      elif 33 <= l < 66:
        return "#F5F5DC"
      elif 66 <= l < 100:
        return "#77DD77"
    elif 105 <= h < 135:
      if 0 <= l < 33:
        return "#006400"
      elif 33 <= l < 66:
        return "#00FF00"
      elif 66 <= l < 100:
        return "#99FF99"
    elif 135 <= h < 165:
      if 0 <= l < 33:
        return "#006633"
      elif 33 <= l < 66:
        return "#00FF7F"
      elif 66 <= l < 100:
        return "#7FFFD4"
    elif 165 <= h < 195:
      if 0 <= l < 33:
        return "#006D5B"
      elif 33 <= l < 66:
        return "#00FFFF"
      elif 66 <= l < 100:
        return "#42AAFF"
    elif 195 <= h < 225:
      if 0 <= l < 33:
        return "#00416A"
      elif 33 <= l < 66:
        return "#008CF0"
      elif 66 <= l < 100:
        return "#7FC7FF"
    elif 225 <= h < 255:
      if 0 <= l < 33:
        return "#000080"
      elif 33 <= l < 66:
        return "#0000FF"
      elif 66 <= l < 100:
        return "#DF73FF"
    elif 255 <= h < 285:
      if 0 <= l < 33:
        return "#310062"
      elif 33 <= l < 66:
        return "#800080"
      elif 66 <= l < 100:
        return "#E0B0FF"
    elif 285 <= h < 315:
      if 0 <= l < 33:
        return "#660066"
      elif 33 <= l < 66:
        return "#FF00FF"
      elif 66 <= l < 100:
        return "#FF77FF"
    elif 315 <= h < 345:
      if 0 <= l < 33:
        return "#6F0035"
      elif 33 <= l < 66:
        return "#FF1493"
      elif 66 <= l < 100:
        return "#FF97BB"
    else:
      if 0 <= l < 33:
        return "#65000B"
      elif 33 <= l < 66:
        return "#FF0000"
      elif 66 <= l < 100:
        return "##FF9BAA"
  else:
    if 0 <= l < 15:
      return "#000000"
    elif 15 <= l < 45:
      return "#464451"
    elif 45 <= l < 66:
      return "#9C9C9C"
    elif 66 <= l < 100:
      return "#FFFFFF"


import os
import xml.dom.minidom
from tqdm import tqdm
import json
import re
import tarfile
import shutil
from transformers import AutoTokenizer
from collections import defaultdict
import torch

"""
NYTimes Reference: https://catalog.ldc.upenn.edu/LDC2008T19
"""

sample_ratio = 0.02
train_ratio = 0.7
min_per_node = 200

# tokenizer = AutoTokenizer.from_pretrained('facebook/bart-base')
source = []
labels = []
label_ids = []
label_dict = {}
sentence_ids = []
hiera = defaultdict(set)
ROOT_DIR = 'Nytimes/'
label_f = 'nyt_label.vocab'

# 2003-07
def read_nyt(id_json):
    f = open(id_json, 'r')
    ids = f.readlines()
    f.close()
    print(ids[:2])
    f = open(label_f, 'r')
    label_vocab_s = f.readlines()
    f.close()
    label_vocab = []
    for label in label_vocab_s:
        label = label.strip()
        label_vocab.append(label)
    id_list = []
    for i in ids:
        id_list.append(int(i[13:-5]))
    print(id_list[:2])
    corpus = []

    for file_name in tqdm(ids):
        xml_path = file_name.strip()
        try:
            sample = {}
            dom = xml.dom.minidom.parse(xml_path)
            root = dom.documentElement
            tags = root.getElementsByTagName('p')
            text = ''
            for tag in tags[1:]:
                text += tag.firstChild.data
            if text == '':
                continue
            source.append(text)
            sample_label = []
            tags = root.getElementsByTagName('classifier')
            for tag in tags:
                type = tag.getAttribute('type')
                if type != 'taxonomic_classifier':
                    continue
                hier_path = tag.firstChild.data
                hier_list = hier_path.split('/')
                if len(hier_list) < 3:
                    continue
                for l in range(1, len(hier_list) + 1):
                    label = '/'.join(hier_list[:l])
                    if label == 'Top':
                        continue
                    if label not in sample_label and label in label_vocab:
                        sample_label.append(label)
            labels.append(sample_label)
            sentence_ids.append(file_name)
            sample['doc_topic'] = []
            sample['doc_keyword'] = []
            corpus.append(sample)
        except AssertionError:
            print(xml_path)
            print('Something went wrong...')
            continue

if __name__ == '__main__':
    files = os.listdir('nyt_corpus/data')
    for year in files:
        month = os.listdir(os.path.join('nyt_corpus/data', year))
        for m in month:
            if os.path.isdir(os.path.join('nyt_corpus/data', year, m)):
                new_month = os.listdir(os.path.join('nyt_corpus/data', year, m))
                for nm in new_month:
                    f = tarfile.open(os.path.join('nyt_corpus/data', year, m, nm))
                    f.extractall(os.path.join('Nytimes', year))
            else:
                f = tarfile.open(os.path.join('nyt_corpus/data', year, m))
                f.extractall(os.path.join('Nytimes', year))
    files = os.listdir('Nytimes')
    for year in files:
        month = os.listdir(os.path.join('Nytimes', year))
        for m in month:
            days = os.listdir(os.path.join('Nytimes', year, m))
            for d in days:
                file = os.listdir(os.path.join('Nytimes', year, m, d))
                for f in file:
                    shutil.move(os.path.join('Nytimes', year, m, d, f), os.path.join('Nytimes', year, f))
    read_nyt('idnewnyt_train.json')
    read_nyt('idnewnyt_val.json')
    read_nyt('idnewnyt_test.json')
    rev_dict = {}
    max_length = 0
    for l in labels:
        max_length = max(max_length, len(l))
        for l_ in l:
            split = l_.split('/')
            if l_ not in label_dict:
                label_dict[l_] = len(label_dict)
            for i in range(1, len(split) - 1):
                hiera[label_dict['/'.join(split[:i + 1])]].add(label_dict['/'.join(split[:i + 2])])
                assert '/'.join(split[:i + 2]) not in rev_dict or rev_dict['/'.join(split[:i + 2])] == '/'.join(
                    split[:i + 1])
                rev_dict['/'.join(split[:i + 2])] = '/'.join(split[:i + 1])
    print(max_length)

    value_dict = {i: v.split('/')[-1] for v, i in
                  label_dict.items()}
    torch.save(value_dict, 'value_dict.pt')
    torch.save(hiera, 'slot.pt')

    train_split = open('idnewnyt_train.json', 'r').readlines()
    dev_split = open('idnewnyt_val.json', 'r').readlines()
    test_split = open('idnewnyt_test.json', 'r').readlines()
    train, test, val = [], [], []
    for i in range(len(sentence_ids)):
        if sentence_ids[i] in train_split:
            train.append(i)
        elif sentence_ids[i] in dev_split:
            val.append(i)
        elif sentence_ids[i] in test_split:
            test.append(i)
        else:
            raise RuntimeError

    with open('nyt_train.json', 'w') as f:
        for i in train:
            line = json.dumps({'token': source[i], 'label': [label_dict[i] for i in labels[i]]})
            f.write(line + '\n')

    with open('nyt_dev.json', 'w') as f:
        for i in val:
            line = json.dumps({'token': source[i], 'label': [label_dict[i] for i in labels[i]]})
            f.write(line + '\n')

    with open('nyt_test.json', 'w') as f:
        for i in test:
            line = json.dumps({'token': source[i], 'label': [label_dict[i] for i in labels[i]]})
            f.write(line + '\n')


import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="gamma-facet", # Replace with your own username
    version="0.0.1",
    author="Konstantin Hemker",
    author_email="konstantin.hemker@googlemail.com",
    description="Mock package",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/konst-int-i/facet-mock",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.7',
)


from forta_agent import FindingSeverity, create_transaction_event

import agent
from evmdasm import EvmBytecode
from web3_mock import (
    BENIGN_CONTRACT,
    CONTRACT_NO_ADDRESS,
    CONTRACT_WITH_ADDRESS,
    EOA_ADDRESS,
    MALICIOUS_TOKEN_CONTRACT,
    MALICIOUS_TOKEN_CONTRACT_DEPLOYER,
    MALICIOUS_TOKEN_CONTRACT_DEPLOYER_NONCE,
    SHORT_CONTRACT,
    Web3Mock,
)

w3 = Web3Mock()

class TestMaliciousSmartContractML:
    def test_is_contract_eoa(self):
        assert not agent.is_contract(
            w3, EOA_ADDRESS
        ), "EOA shouldn't be identified as a contract"

    def test_is_contract_contract(self):
        assert agent.is_contract(
            w3, CONTRACT_NO_ADDRESS
        ), "Contract should be identified as a contract"

    def test_opcode_addresses_eoa(self):
        # EOAs don't have bytecode or opcodes
        bytecode = w3.eth.get_code(EOA_ADDRESS)
        opcodes = EvmBytecode(bytecode.hex()).disassemble()
        _, addresses = agent.get_features(w3, opcodes, EOA_ADDRESS)
        assert len(addresses) == 0, "should be empty"

    def test_opcode_addresses_no_addr(self):
        bytecode = w3.eth.get_code(CONTRACT_NO_ADDRESS)
        opcodes = EvmBytecode(bytecode.hex()).disassemble()
        _, addresses = agent.get_features(w3, opcodes, EOA_ADDRESS)
        assert len(addresses) == 0, "should be empty"

    def test_opcode_addresses_with_addr(self):
        bytecode = w3.eth.get_code(CONTRACT_WITH_ADDRESS)
        opcodes = EvmBytecode(bytecode.hex()).disassemble()
        _, addresses = agent.get_features(w3, opcodes, EOA_ADDRESS)

        assert len(addresses) == 1, "should not be empty"

    def test_storage_addresses_with_addr(self):
        addresses = agent.get_storage_addresses(w3, CONTRACT_WITH_ADDRESS)
        assert len(addresses) == 1, "should not be empty"

    def test_storage_addresses_on_eoa(self):
        addresses = agent.get_storage_addresses(w3, EOA_ADDRESS)
        assert len(addresses) == 0, "should be empty; EOA has no storage"

    def test_calc_contract_address(self):
        contract_address = agent.calc_contract_address(w3, EOA_ADDRESS, 9)
        assert (
            contract_address == "0x728ad672409DA288cA5B9AA85D1A55b803bA97D7"
        ), "should be the same contract address"

    def test_get_features(self):
        bytecode = w3.eth.get_code(MALICIOUS_TOKEN_CONTRACT)
        opcodes = EvmBytecode(bytecode.hex()).disassemble()
        features, _ = agent.get_features(w3, opcodes, EOA_ADDRESS)
        assert len(features) == 24312, "incorrect features length obtained"

    def test_finding_MALICIOUS_TOKEN_CONTRACT_creation(self):
        agent.initialize()

        tx_event = create_transaction_event(
            {
                "transaction": {
                    "hash": "0",
                    "from": MALICIOUS_TOKEN_CONTRACT_DEPLOYER,
                    "nonce": MALICIOUS_TOKEN_CONTRACT_DEPLOYER_NONCE,
                },
                "block": {"number": 0},
                "traces": [
                    {
                        "type": "create",
                        "action": {
                            "from": MALICIOUS_TOKEN_CONTRACT_DEPLOYER,
                            "init": w3.eth.get_code(MALICIOUS_TOKEN_CONTRACT),
                            "value": 1,
                        },
                        "result": {"address": MALICIOUS_TOKEN_CONTRACT},
                    }
                ],
                "receipt": {"logs": []},
            }
        )
        findings = agent.detect_malicious_token_contract_tx(w3, tx_event)
        assert len(findings) == 1, "this should have triggered a finding"
        finding = next(
            (x for x in findings if x.alert_id == "SUSPICIOUS-TOKEN-CONTRACT-CREATION"),
            None,
        )
        assert finding.severity == FindingSeverity.High

    def test_detect_malicious_token_contract_benign(self):
        agent.initialize()
        bytecode = w3.eth.get_code(BENIGN_CONTRACT)
        findings = agent.detect_malicious_token_contract(
            w3, EOA_ADDRESS, BENIGN_CONTRACT, bytecode
        )
        assert len(findings) == 1
        finding = findings[0]
        assert finding.alert_id == "SAFE-TOKEN-CONTRACT-CREATION"

    def test_detect_malicious_token_contract_short(self):
        agent.initialize()
        bytecode = w3.eth.get_code(SHORT_CONTRACT)
        findings = agent.detect_malicious_token_contract(
            w3, EOA_ADDRESS, SHORT_CONTRACT, bytecode
        )
        assert len(findings) == 0, "this should not have triggered a finding"


import math
import random
import functools
import operator

import torch
from torch import nn
from torchvision import models
import torch.utils.model_zoo as model_zoo

from torch.nn import functional as F
from torch.autograd import Function
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
from torch.autograd import Variable

from op import FusedLeakyReLU, fused_leaky_relu, upfirdn2d, conv2d_gradfix

from miscc.config import cfg

class PixelNorm(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, input):
        return input * torch.rsqrt(torch.mean(input ** 2, dim=1, keepdim=True) + 1e-8)

def make_kernel(k):
    k = torch.tensor(k, dtype=torch.float32)

    if k.ndim == 1:
        k = k[None, :] * k[:, None]

    k /= k.sum()

    return k

class Upsample(nn.Module):
    def __init__(self, kernel, factor=2):
        super().__init__()

        self.factor = factor
        kernel = make_kernel(kernel) * (factor ** 2)
        self.register_buffer("kernel", kernel)

        p = kernel.shape[0] - factor

        pad0 = (p + 1) // 2 + factor - 1
        pad1 = p // 2

        self.pad = (pad0, pad1)

    def forward(self, input):
        out = upfirdn2d(input, self.kernel, up=self.factor, down=1, pad=self.pad)

        return out

class Downsample(nn.Module):
    def __init__(self, kernel, factor=2):
        super().__init__()

        self.factor = factor
        kernel = make_kernel(kernel)
        self.register_buffer("kernel", kernel)

        p = kernel.shape[0] - factor

        pad0 = (p + 1) // 2
        pad1 = p // 2

        self.pad = (pad0, pad1)

    def forward(self, input):
        out = upfirdn2d(input, self.kernel, up=1, down=self.factor, pad=self.pad)

        return out

class Blur(nn.Module):
    def __init__(self, kernel, pad, upsample_factor=1):
        super().__init__()

        kernel = make_kernel(kernel)

        if upsample_factor > 1:
            kernel = kernel * (upsample_factor ** 2)

        self.register_buffer("kernel", kernel)

        self.pad = pad

    def forward(self, input):
        out = upfirdn2d(input, self.kernel, pad=self.pad)

        return out

class EqualConv2d(nn.Module):
    def __init__(
        self, in_channel, out_channel, kernel_size, stride=1, padding=0, bias=True
    ):
        super().__init__()

        self.weight = nn.Parameter(
            torch.randn(out_channel, in_channel, kernel_size, kernel_size)
        )
        self.scale = 1 / math.sqrt(in_channel * kernel_size ** 2)

        self.stride = stride
        self.padding = padding

        if bias:
            self.bias = nn.Parameter(torch.zeros(out_channel))

        else:
            self.bias = None

    def forward(self, input):
        out = conv2d_gradfix.conv2d(
            input,
            self.weight * self.scale,
            bias=self.bias,
            stride=self.stride,
            padding=self.padding,
        )

        return out

    def __repr__(self):
        return (
            f"{self.__class__.__name__}({self.weight.shape[1]}, {self.weight.shape[0]},"
            f" {self.weight.shape[2]}, stride={self.stride}, padding={self.padding})"
        )

class EqualLinear(nn.Module):
    def __init__(
        self, in_dim, out_dim, bias=True, bias_init=0, lr_mul=1, activation=None
    ):
        super().__init__()

        self.weight = nn.Parameter(torch.randn(out_dim, in_dim).div_(lr_mul))

        if bias:
            self.bias = nn.Parameter(torch.zeros(out_dim).fill_(bias_init))

        else:
            self.bias = None

        self.activation = activation

        self.scale = (1 / math.sqrt(in_dim)) * lr_mul
        self.lr_mul = lr_mul

    def forward(self, input):
        if self.activation:
            out = F.linear(input, self.weight * self.scale)
            out = fused_leaky_relu(out, self.bias * self.lr_mul)

        else:
            out = F.linear(
                input, self.weight * self.scale, bias=self.bias * self.lr_mul
            )

        return out

    def __repr__(self):
        return (
            f"{self.__class__.__name__}({self.weight.shape[1]}, {self.weight.shape[0]})"
        )

class ModulatedConv2d(nn.Module):
    def __init__(
        self,
        in_channel,
        out_channel,
        kernel_size,
        style_dim,
        demodulate=True,
        upsample=False,
        downsample=False,
        blur_kernel=[1, 3, 3, 1],
        fused=True,
    ):
        super().__init__()

        self.eps = 1e-8
        self.kernel_size = kernel_size
        self.in_channel = in_channel
        self.out_channel = out_channel
        self.upsample = upsample
        self.downsample = downsample

        if upsample:
            factor = 2
            p = (len(blur_kernel) - factor) - (kernel_size - 1)
            pad0 = (p + 1) // 2 + factor - 1
            pad1 = p // 2 + 1

            self.blur = Blur(blur_kernel, pad=(pad0, pad1), upsample_factor=factor)

        if downsample:
            factor = 2
            p = (len(blur_kernel) - factor) + (kernel_size - 1)
            pad0 = (p + 1) // 2
            pad1 = p // 2

            self.blur = Blur(blur_kernel, pad=(pad0, pad1))

        fan_in = in_channel * kernel_size ** 2
        self.scale = 1 / math.sqrt(fan_in)
        self.padding = kernel_size // 2

        self.weight = nn.Parameter(
            torch.randn(1, out_channel, in_channel, kernel_size, kernel_size)
        )

        self.modulation = EqualLinear(style_dim, in_channel, bias_init=1)

        self.demodulate = demodulate
        self.fused = fused

    def __repr__(self):
        return (
            f"{self.__class__.__name__}({self.in_channel}, {self.out_channel}, {self.kernel_size}, "
            f"upsample={self.upsample}, downsample={self.downsample})"
        )

    def forward(self, input, style):
        batch, in_channel, height, width = input.shape

        if not self.fused:
            weight = self.scale * self.weight.squeeze(0)
            style = self.modulation(style)

            if self.demodulate:
                w = weight.unsqueeze(0) * style.view(batch, 1, in_channel, 1, 1)
                dcoefs = (w.square().sum((2, 3, 4)) + 1e-8).rsqrt()

            input = input * style.reshape(batch, in_channel, 1, 1)

            if self.upsample:
                weight = weight.transpose(0, 1)
                out = conv2d_gradfix.conv_transpose2d(
                    input, weight, padding=0, stride=2
                )
                out = self.blur(out)

            elif self.downsample:
                input = self.blur(input)
                out = conv2d_gradfix.conv2d(input, weight, padding=0, stride=2)

            else:
                out = conv2d_gradfix.conv2d(input, weight, padding=self.padding)

            if self.demodulate:
                out = out * dcoefs.view(batch, -1, 1, 1)

            return out

        style = self.modulation(style).view(batch, 1, in_channel, 1, 1)
        weight = self.scale * self.weight * style

        if self.demodulate:
            demod = torch.rsqrt(weight.pow(2).sum([2, 3, 4]) + 1e-8)
            weight = weight * demod.view(batch, self.out_channel, 1, 1, 1)

        weight = weight.view(
            batch * self.out_channel, in_channel, self.kernel_size, self.kernel_size
        )

        if self.upsample:
            input = input.view(1, batch * in_channel, height, width)
            weight = weight.view(
                batch, self.out_channel, in_channel, self.kernel_size, self.kernel_size
            )
            weight = weight.transpose(1, 2).reshape(
                batch * in_channel, self.out_channel, self.kernel_size, self.kernel_size
            )
            out = conv2d_gradfix.conv_transpose2d(
                input, weight, padding=0, stride=2, groups=batch
            )
            _, _, height, width = out.shape
            out = out.view(batch, self.out_channel, height, width)
            out = self.blur(out)

        elif self.downsample:
            input = self.blur(input)
            _, _, height, width = input.shape
            input = input.view(1, batch * in_channel, height, width)
            out = conv2d_gradfix.conv2d(
                input, weight, padding=0, stride=2, groups=batch
            )
            _, _, height, width = out.shape
            out = out.view(batch, self.out_channel, height, width)

        else:
            input = input.view(1, batch * in_channel, height, width)
            out = conv2d_gradfix.conv2d(
                input, weight, padding=self.padding, groups=batch
            )
            _, _, height, width = out.shape
            out = out.view(batch, self.out_channel, height, width)

        return out

class NoiseInjection(nn.Module):
    def __init__(self):
        super().__init__()

        self.weight = nn.Parameter(torch.zeros(1))

    def forward(self, image, noise=None):
        if noise is None:
            batch, _, height, width = image.shape
            noise = image.new_empty(batch, 1, height, width).normal_()

        return image + self.weight * noise

class ConstantInput(nn.Module):
    def __init__(self, channel, size=4):
        super().__init__()

        self.input = nn.Parameter(torch.randn(1, channel, size, size))

    def forward(self, input):
        batch = input.shape[0]
        out = self.input.repeat(batch, 1, 1, 1)

        return out

class StyledConv(nn.Module):
    def __init__(
        self,
        in_channel,
        out_channel,
        kernel_size,
        style_dim,
        upsample=False,
        blur_kernel=[1, 3, 3, 1],
        demodulate=True,
    ):
        super().__init__()

        self.conv = ModulatedConv2d(
            in_channel,
            out_channel,
            kernel_size,
            style_dim,
            upsample=upsample,
            blur_kernel=blur_kernel,
            demodulate=demodulate,
        )

        self.noise = NoiseInjection()
        # self.bias = nn.Parameter(torch.zeros(1, out_channel, 1, 1))
        # self.activate = ScaledLeakyReLU(0.2)
        self.activate = FusedLeakyReLU(out_channel)

    def forward(self, input, style, noise=None):
        out = self.conv(input, style)
        out = self.noise(out, noise=noise)
        # out = out + self.bias
        out = self.activate(out)

        return out

class ToRGB(nn.Module):
    def __init__(self, in_channel, style_dim, upsample=True, blur_kernel=[1, 3, 3, 1]):
        super().__init__()

        if upsample:
            self.upsample = Upsample(blur_kernel)

        self.conv = ModulatedConv2d(in_channel, 3, 1, style_dim, demodulate=False)
        self.bias = nn.Parameter(torch.zeros(1, 3, 1, 1))

    def forward(self, input, style, skip=None):
        out = self.conv(input, style)
        out = out + self.bias

        if skip is not None:
            skip = self.upsample(skip)

            out = out + skip

        return out

class Generator(nn.Module):
    def __init__(
        self,
        size,
        style_dim,
        n_mlp,
        channel_multiplier=2,
        blur_kernel=[1, 3, 3, 1],
        lr_mlp=0.01,
    ):
        super().__init__()

        self.size = size

        self.style_dim = style_dim
        layers = [PixelNorm()]

        for i in range(n_mlp):
            layers.append(
                EqualLinear(
                    style_dim, style_dim, lr_mul=lr_mlp, activation="fused_lrelu"
                )
            )

        self.style = nn.Sequential(*layers)

        self.channels = {
            4: 512,
            8: 512,
            16: 512,
            32: 512,
            64: 256 * channel_multiplier,
            128: 128 * channel_multiplier,
            256: 64 * channel_multiplier,
            512: 32 * channel_multiplier,
            1024: 16 * channel_multiplier,
        }

        self.input = ConstantInput(self.channels[4])
        self.conv1 = StyledConv(
            self.channels[4], self.channels[4], 3, style_dim, blur_kernel=blur_kernel
        )
        self.to_rgb1 = ToRGB(self.channels[4], style_dim, upsample=False)

        self.log_size = int(math.log(size, 2))
        self.num_layers = (self.log_size - 2) * 2 + 1

        self.convs = nn.ModuleList()
        self.upsamples = nn.ModuleList()
        self.to_rgbs = nn.ModuleList()
        self.noises = nn.Module()

        in_channel = self.channels[4]

        for layer_idx in range(self.num_layers):
            res = (layer_idx + 5) // 2
            shape = [1, 1, 2 ** res, 2 ** res]
            self.noises.register_buffer(f"noise_{layer_idx}", torch.randn(*shape))

        for i in range(3, self.log_size + 1):
            out_channel = self.channels[2 ** i]

            self.convs.append(
                StyledConv(
                    in_channel,
                    out_channel,
                    3,
                    style_dim,
                    upsample=True,
                    blur_kernel=blur_kernel,
                )
            )

            self.convs.append(
                StyledConv(
                    out_channel, out_channel, 3, style_dim, blur_kernel=blur_kernel
                )
            )

            self.to_rgbs.append(ToRGB(out_channel, style_dim))

            in_channel = out_channel

        self.n_latent = self.log_size * 2 - 2

    def make_noise(self):
        device = self.input.input.device

        noises = [torch.randn(1, 1, 2 ** 2, 2 ** 2, device=device)]

        for i in range(3, self.log_size + 1):
            for _ in range(2):
                noises.append(torch.randn(1, 1, 2 ** i, 2 ** i, device=device))

        return noises

    def mean_latent(self, n_latent):
        latent_in = torch.randn(
            n_latent, self.style_dim, device=self.input.input.device
        )
        latent = self.style(latent_in).mean(0, keepdim=True)

        return latent

    def get_latent(self, input):
        return self.style(input)

    def forward(
        self,
        styles,
        return_latents=False,
        inject_index=None,
        truncation=1,
        truncation_latent=None,
        input_is_latent=False,
        noise=None,
        randomize_noise=True,
    ):
        if not input_is_latent:
            styles = [self.style(s) for s in styles]

        if noise is None:
            if randomize_noise:
                noise = [None] * self.num_layers
            else:
                noise = [
                    getattr(self.noises, f"noise_{i}") for i in range(self.num_layers)
                ]

        if truncation < 1:
            style_t = []

            for style in styles:
                style_t.append(
                    truncation_latent + truncation * (style - truncation_latent)
                )

            styles = style_t

        if len(styles) < 2:
            inject_index = self.n_latent

            if styles[0].ndim < 3:
                latent = styles[0].unsqueeze(1).repeat(1, inject_index, 1)

            else:
                latent = styles[0]

        else:
            if inject_index is None:
                inject_index = random.randint(1, self.n_latent - 1)

            latent = styles[0].unsqueeze(1).repeat(1, inject_index, 1)
            latent2 = styles[1].unsqueeze(1).repeat(1, self.n_latent - inject_index, 1)

            latent = torch.cat([latent, latent2], 1)

        out = self.input(latent)
        out = self.conv1(out, latent[:, 0], noise=noise[0])

        skip = self.to_rgb1(out, latent[:, 1])

        i = 1
        for conv1, conv2, noise1, noise2, to_rgb in zip(
            self.convs[::2], self.convs[1::2], noise[1::2], noise[2::2], self.to_rgbs
        ):
            out = conv1(out, latent[:, i], noise=noise1)
            out = conv2(out, latent[:, i + 1], noise=noise2)
            skip = to_rgb(out, latent[:, i + 2], skip)

            i += 2

        image = skip

        if return_latents:
            return image, latent

        else:
            return image, None

class ConvLayer(nn.Sequential):
    def __init__(
        self,
        in_channel,
        out_channel,
        kernel_size,
        downsample=False,
        blur_kernel=[1, 3, 3, 1],
        bias=True,
        activate=True,
    ):
        layers = []

        if downsample:
            factor = 2
            p = (len(blur_kernel) - factor) + (kernel_size - 1)
            pad0 = (p + 1) // 2
            pad1 = p // 2

            layers.append(Blur(blur_kernel, pad=(pad0, pad1)))

            stride = 2
            self.padding = 0

        else:
            stride = 1
            self.padding = kernel_size // 2

        layers.append(
            EqualConv2d(
                in_channel,
                out_channel,
                kernel_size,
                padding=self.padding,
                stride=stride,
                bias=bias and not activate,
            )
        )

        if activate:
            layers.append(FusedLeakyReLU(out_channel, bias=bias))

        super().__init__(*layers)

class ResBlock(nn.Module):
    def __init__(self, in_channel, out_channel, blur_kernel=[1, 3, 3, 1]):
        super().__init__()

        self.conv1 = ConvLayer(in_channel, in_channel, 3)
        self.conv2 = ConvLayer(in_channel, out_channel, 3, downsample=True)

        self.skip = ConvLayer(
            in_channel, out_channel, 1, downsample=True, activate=False, bias=False
        )

    def forward(self, input):
        out = self.conv1(input)
        out = self.conv2(out)

        skip = self.skip(input)
        out = (out + skip) / math.sqrt(2)

        return out

class Discriminator(nn.Module):
    def __init__(self, size, channel_multiplier=2, blur_kernel=[1, 3, 3, 1]):
        super().__init__()

        channels = {
            4: 512,
            8: 512,
            16: 512,
            32: 512,
            64: 256 * channel_multiplier,
            128: 128 * channel_multiplier,
            256: 64 * channel_multiplier,
            512: 32 * channel_multiplier,
            1024: 16 * channel_multiplier,
        }

        # TODO
        # should become an argument later on
        self.ef_dim = 256
        self.img_size = 256

        convs = [ConvLayer(3 + self.ef_dim, channels[size], 1)]

        log_size = int(math.log(size, 2))

        in_channel = channels[size]

        for i in range(log_size, 2, -1):
            out_channel = channels[2 ** (i - 1)]

            convs.append(ResBlock(in_channel, out_channel, blur_kernel))

            in_channel = out_channel

        self.convs = nn.Sequential(*convs)

        self.stddev_group = 4
        self.stddev_feat = 1

        self.final_conv = ConvLayer(in_channel + 1, channels[4], 3)
        self.final_linear = nn.Sequential(
            EqualLinear(channels[4] * 4 * 4, channels[4], activation="fused_lrelu"),
            EqualLinear(channels[4], 1),
        )
        self.sigmoid_layer = nn.Sigmoid()

    def forward(self, input, c_code=None):
        if c_code is not None:
            c_code = c_code.view(-1, self.ef_dim, 1, 1)
            c_code = c_code.repeat(1, 1, self.img_size, self.img_size)
            input = torch.cat((input, c_code), 1)
        out = self.convs(input)

        batch, channel, height, width = out.shape
        group = min(batch, self.stddev_group)

        stddev = out.view(
            group, -1, self.stddev_feat, channel // self.stddev_feat, height, width
        )
        stddev = torch.sqrt(stddev.var(0, unbiased=False) + 1e-8)
        stddev = stddev.mean([2, 3, 4], keepdims=True).squeeze(2)
        stddev = stddev.repeat(group, 1, height, width)
        out = torch.cat([out, stddev], 1)

        out = self.final_conv(out)

        out = out.view(batch, -1)
        out = self.final_linear(out)
        logits = self.sigmoid_layer(out)
        return out, logits

class RNN_ENCODER(nn.Module):
    def __init__(
        self,
        ntoken,
        ninput=300,
        drop_prob=0.5,
        nhidden=128,
        nlayers=1,
        bidirectional=True,
    ):
        super(RNN_ENCODER, self).__init__()
        self.n_steps = cfg.TEXT.WORDS_NUM
        self.ntoken = ntoken  # size of the dictionary
        self.ninput = ninput  # size of each embedding vector
        self.drop_prob = drop_prob  # probability of an element to be zeroed
        self.nlayers = nlayers  # Number of recurrent layers
        self.bidirectional = bidirectional
        self.rnn_type = cfg.RNN_TYPE
        if bidirectional:
            self.num_directions = 2
        else:
            self.num_directions = 1
        # number of features in the hidden state
        self.nhidden = nhidden // self.num_directions

        self.define_module()
        self.init_weights()

    def define_module(self):
        self.encoder = nn.Embedding(self.ntoken, self.ninput)
        self.drop = nn.Dropout(self.drop_prob)
        if self.rnn_type == "LSTM":
            # dropout: If non-zero, introduces a dropout layer on
            # the outputs of each RNN layer except the last layer
            self.rnn = nn.LSTM(
                self.ninput,
                self.nhidden,
                self.nlayers,
                batch_first=True,
                dropout=self.drop_prob,
                bidirectional=self.bidirectional,
            )
        elif self.rnn_type == "GRU":
            self.rnn = nn.GRU(
                self.ninput,
                self.nhidden,
                self.nlayers,
                batch_first=True,
                dropout=self.drop_prob,
                bidirectional=self.bidirectional,
            )
        else:
            raise NotImplementedError

    def init_weights(self):
        initrange = 0.1
        self.encoder.weight.data.uniform_(-initrange, initrange)
        # Do not need to initialize RNN parameters, which have been initialized
        # http://pytorch.org/docs/master/_modules/torch/nn/modules/rnn.html#LSTM
        # self.decoder.weight.data.uniform_(-initrange, initrange)
        # self.decoder.bias.data.fill_(0)

    def init_hidden(self, bsz):
        device = "cuda:1"
        weight = next(self.parameters()).data
        if self.rnn_type == "LSTM":
            return (
                Variable(
                    weight.new(
                        self.nlayers * self.num_directions, bsz, self.nhidden
                    ).zero_()
                ).cuda(),
                Variable(
                    weight.new(
                        self.nlayers * self.num_directions, bsz, self.nhidden
                    ).zero_()
                ).cuda(),
            )
        else:
            return Variable(
                weight.new(
                    self.nlayers * self.num_directions, bsz, self.nhidden
                ).zero_()
            ).cuda()

    def forward(self, captions, cap_lens, hidden, mask=None):
        # input: torch.LongTensor of size batch x n_steps
        # --> emb: batch x n_steps x ninput
        emb = self.drop(self.encoder(captions))
        #
        # Returns: a PackedSequence object
        cap_lens = cap_lens.data.tolist()
        emb = pack_padded_sequence(emb, cap_lens, batch_first=True)
        # #hidden and memory (num_layers * num_directions, batch, hidden_size):
        # tensor containing the initial hidden state for each element in batch.
        # #output (batch, seq_len, hidden_size * num_directions)
        # #or a PackedSequence object:
        # tensor containing output features (h_t) from the last layer of RNN
        output, hidden = self.rnn(emb, hidden)
        # PackedSequence object
        # --> (batch, seq_len, hidden_size * num_directions)
        output = pad_packed_sequence(output, batch_first=True)[0]
        # output = self.drop(output)
        # --> batch x hidden_size*num_directions x seq_len
        words_emb = output.transpose(1, 2)
        # --> batch x num_directions*hidden_size
        if self.rnn_type == "LSTM":
            sent_emb = hidden[0].transpose(0, 1).contiguous()
        else:
            sent_emb = hidden.transpose(0, 1).contiguous()
        sent_emb = sent_emb.view(-1, self.nhidden * self.num_directions)
        return words_emb, sent_emb

# CNN Encoder
def conv1x1(in_planes, out_planes, bias=False):
    "1x1 convolution with padding"
    return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=1,
                     padding=0, bias=bias)

class CNN_ENCODER(nn.Module):
    def __init__(self, nef):
        super(CNN_ENCODER, self).__init__()
        if cfg.TRAIN.FLAG:
            self.nef = nef
        else:
            self.nef = 256  # define a uniform ranker

        model = models.inception_v3()
        url = 'https://download.pytorch.org/models/inception_v3_google-1a9a5a14.pth'
        model.load_state_dict(model_zoo.load_url(url))
        for param in model.parameters():
            param.requires_grad = False
        print('Load pretrained model from ', url)
        # print(model)

        self.define_module(model)
        self.init_trainable_weights()

    def define_module(self, model):
        self.Conv2d_1a_3x3 = model.Conv2d_1a_3x3
        self.Conv2d_2a_3x3 = model.Conv2d_2a_3x3
        self.Conv2d_2b_3x3 = model.Conv2d_2b_3x3
        self.Conv2d_3b_1x1 = model.Conv2d_3b_1x1
        self.Conv2d_4a_3x3 = model.Conv2d_4a_3x3
        self.Mixed_5b = model.Mixed_5b
        self.Mixed_5c = model.Mixed_5c
        self.Mixed_5d = model.Mixed_5d
        self.Mixed_6a = model.Mixed_6a
        self.Mixed_6b = model.Mixed_6b
        self.Mixed_6c = model.Mixed_6c
        self.Mixed_6d = model.Mixed_6d
        self.Mixed_6e = model.Mixed_6e
        self.Mixed_7a = model.Mixed_7a
        self.Mixed_7b = model.Mixed_7b
        self.Mixed_7c = model.Mixed_7c

        self.emb_features = conv1x1(768, self.nef)
        self.emb_cnn_code = nn.Linear(2048, self.nef)

    def init_trainable_weights(self):
        initrange = 0.1
        self.emb_features.weight.data.uniform_(-initrange, initrange)
        self.emb_cnn_code.weight.data.uniform_(-initrange, initrange)

    def forward(self, x):
        features = None
        # --> fixed-size input: batch x 3 x 299 x 299
        x = nn.functional.interpolate(x,size=(299, 299), mode='bilinear', align_corners=False)
        # 299 x 299 x 3
        x = self.Conv2d_1a_3x3(x)
        # 149 x 149 x 32
        x = self.Conv2d_2a_3x3(x)
        # 147 x 147 x 32
        x = self.Conv2d_2b_3x3(x)
        # 147 x 147 x 64
        x = F.max_pool2d(x, kernel_size=3, stride=2)
        # 73 x 73 x 64
        x = self.Conv2d_3b_1x1(x)
        # 73 x 73 x 80
        x = self.Conv2d_4a_3x3(x)
        # 71 x 71 x 192

        x = F.max_pool2d(x, kernel_size=3, stride=2)
        # 35 x 35 x 192
        x = self.Mixed_5b(x)
        # 35 x 35 x 256
        x = self.Mixed_5c(x)
        # 35 x 35 x 288
        x = self.Mixed_5d(x)
        # 35 x 35 x 288

        x = self.Mixed_6a(x)
        # 17 x 17 x 768
        x = self.Mixed_6b(x)
        # 17 x 17 x 768
        x = self.Mixed_6c(x)
        # 17 x 17 x 768
        x = self.Mixed_6d(x)
        # 17 x 17 x 768
        x = self.Mixed_6e(x)
        # 17 x 17 x 768

        # image region features
        features = x
        # 17 x 17 x 768

        x = self.Mixed_7a(x)
        # 8 x 8 x 1280
        x = self.Mixed_7b(x)
        # 8 x 8 x 2048
        x = self.Mixed_7c(x)
        # 8 x 8 x 2048
        x = F.avg_pool2d(x, kernel_size=8)
        # 1 x 1 x 2048
        # x = F.dropout(x, training=self.training)
        # 1 x 1 x 2048
        x = x.view(x.size(0), -1)
        # 2048

        # global image features
        cnn_code = self.emb_cnn_code(x)
        # 512
        if features is not None:
            features = self.emb_features(features)

        return features, cnn_code


# def printLeader(arr, n, start_index, output):
#   if start_index is n - 1:
#     output.append(arr[start_index])
#     return output
#
#   temp_output = printLeader(arr, n, start_index + 1, output)
#   if arr[start_index] >= temp_output[len(temp_output) - 1]:
#     output.append(arr[start_index])
#
#   return output
#
#
# if __name__ == '__main__':
#     t = int(input())
#     for _ in range(t):
#       n = int(input())
#       arr = list(map(int, input().strip().split()))
#       output = []
#       printLeader(arr, n, 0, output)
#       while len(output):
#         print(output.pop(), end=" ")
#       print()

def printLeader(arr, n):
  stack = [arr[-1]]
  for i in range(n-2, 0, -1):
    if arr[i] >= stack[-1]:
      stack.append(arr[i])

  while len(stack):
    print(stack.pop(), end=" ")

if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
      n = int(input())
      arr = list(map(int, input().strip().split()))
      printLeader(arr, n)
      print()


#coding: utf-8

from django.shortcuts import render
from django.views.generic.base import View
from automaticPort.views.loginrequired import LoginRequiredMixin
from automaticPort.extras.moduloMensagem import MessengerError,MessengerSucess
from automaticPort.models.modelUsuarioAmbiente import UsuarioAmbiente
from automaticPort.models.modelMicrocontrolador import Microcontrolador
import requests as rqts

class viewAmbienteAutorizado(LoginRequiredMixin,View):
    template = "usuario/ambiente/ambienteAutorizado.html"
    conteudo = {
        'controleMenu':'Ambiente',
    }
    def get(self, request, id_ambiente=None):
        self.conteudo.update({'messengerSucess': '', 'messengerError': ''})

        if id_ambiente:
            self.conteudo.update({'messengerSucess': MessengerError().Usuario(3),})

        try:
            pesquisa_usuario = UsuarioAmbiente.objects.filter(usuario=request.user.id).order_by("ambiente__bloco__nome")
            self.conteudo.update({"ambientes": pesquisa_usuario})
        except UsuarioAmbiente.DoesNotExist:
            self.conteudo.update({'messengerSucess': MessengerError().Usuario(4),})

        return render(request, self.template, self.conteudo)

    def post(self, request):
        self.conteudo.update({'messengerSucess': '', 'messengerError': ''})

        if request.POST.get("id_ambiente"):
            id_ambiente = request.POST.get("id_ambiente")
            pesquisa_ambiente = UsuarioAmbiente.objects.get(ambiente=id_ambiente, usuario=request.user.id)
            microcontrolador = Microcontrolador.objects.get(ambiente=pesquisa_ambiente.ambiente.id)

            if pesquisa_ambiente.usuario.id == request.user.id:
                context = {
                    'command': 'openthedoor',
                    'secretKey':microcontrolador.secretKey ,
                    'ipAddress':microcontrolador.ipAddress,
                    'port':microcontrolador.port}

                try:
                    response = rqts.get(
                        "http://{}:{}/automaticport/resquestarduinoteste/{}/{}".format(
                            context.get('ipAddress'),
                            context.get('port'),
                            context.get('command'),
                            context.get('secretKey')), timeout=5,)
                    self.conteudo.update({'messengerSucess': MessengerSucess().Usuario(1),})

                except:
                    self.conteudo.update({'messengerSucess': MessengerError().Usuario(5),})

            else:
                pesquisa_ambiente = None
                self.conteudo.update({'messengerSucess': MessengerError().Usuario(3),})

        return render(request, self.template, self.conteudo)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 06 10:51:00 2019

@author: jerome

The goal of this package is to build a simple linear equation solver for
equations of the form Ax = b.

The hope is to be versitile, easy to understand and modify, and
work well.  This way should errors present themselves in usage,
finding and fixing the problem should be easy.

This is not intended to be a heavy-duty solver, more a simple and robust
package.
"""

import numpy as np
import numpy.linalg as la

def get_length(arr):
    try:
        l = len(arr)
    except:
        l = 1
    return l

def shape(arr):
    """
    Get the shape of an array

    Input:
        arr
    Ouptut:
        shape - 2 x 1 array
    """
    return np.shape(np.array(arr))

def matmul(m1, m2):
    """
    Matrix multiplication wrapper to deal with vector * scalar

    Input:
        m1, m2 - numpy arrays
    Output:
        m - resulting matrix
    """

    # numpy matrix multiplication

    # verify sizes

    size1 = shape(m1)
    size2 = shape(m2)

    if len(size1) == 0 or len(size2) == 0:
        # one of the values is a scalar
        m = m1 * m2
    else:
        m = m1 @ m2
    return m

def backward_substitute(R, b):
    """
    Backward substitution for an upper triangular system

    Input:
        R - upper triangular matrix, m x m
        b - solution matrix m x 1
    Output:
        x - solution to Rx = b, m x 1

    Note:
        we suppose here that R is invertible, i.e. no diagonal
        entries are zero.
        If there are zeros on the diagonal, then we must satisfy the
        Fredholm alternative, i.e. forall v in the null space of R,
        we must have that v is orthogonal to b.
    """

    [m, n] = np.shape(R)
    if m != n:
        print("Error, rectangular solution not yet implemented")
        return None

    x = np.zeros(m)

    mult_diag = np.prod(np.diag(R))
    if mult_diag == 0:
        print("Error, singular solution not yet implemented")
        return None

    for i in range(m-1, -1, -1):
        if i == m-1:
            x[i] = b[i] / R[i, i]
        else:
            after = R[i, i+1:]
            x[i] = (b[i] - np.dot(after, x[i+1:])) / R[i, i]
    return x

def jacobi_method(R, b, x0=None, tol=1e-5, maxiter=500):
    """
    Jacobi iterative method for solving a system

    Input:
        R - upper triangular matrix (for now) m x m
        b - solution m x 1
    Output:
        x - solution to Rx = b, m x 1

    Note: this method currently assumes R is invertible
    Needs to be updated
    """

    D = np.diagflat(np.diag(R))
    Dn = np.diagflat(1.0 / np.diag(R))
    C = R - D

    if type(x0) == None:
        xprev = b
    else:
        xprev = b

    ite = 0
    error = np.inf

    while ite < maxiter and error > tol:
        xnew = np.matmul(Dn, b - np.matmul(C, xprev))

        error = np.linalg.norm(np.matmul(R, xnew) - b)
        #error = np.linalg.norm(xnew - xprev)
        xprev = xnew
        ite += 1

    if ite == maxiter:
        print("Warning, maximum number of iterations reached")

    print("iterations: ", ite)
    return xnew

def householder_triangulation_solve(A, b):
    """
    Implicit QR via householder triangulation

    Input:
        A - matrix to factorize m x n
        b - RHS for Ax = b, m x 1
    Output:
        x - solution to Ax = b, n x 1

    Note: this is error prone, will need to be revised
    """

    [m, n] = np.shape(A)

    R = np.copy(A)
    beta = np.copy(b)
    R = R.astype(np.float32)
    beta = beta.astype(np.float32)

    for k in range(n):

        # select area to modify
        z = R[k:, k]

        # build unit vector e1
        e1 = np.zeros(np.shape(z))
        e1[0] = 1

        # build v vector
        vec = z[0] / np.abs(z[0]) * np.linalg.norm(z) * e1 + z
        vec /= np.linalg.norm(vec)      # normalize
        v = np.matrix(vec)
        v = v.T

        # update R and beta
        R[k:, k:] = R[k:, k:] - 2 * np.matmul(v, np.matmul(np.transpose(v), R[k:, k:]))
        if k < n-1:
            R[k+1:, k] = 0

        beta[k:] -= 2.0 * np.float(np.dot(v.T, beta[k:])) * v

    # loop leaves last row unaltered
    R[-1, -1] *= -1
    beta[-1] *= -1

    # build solution
    x = backward_substitute(R, beta)

    return x

def qr_solve(A, b):
    """
    Solve the linear system Ax = b

    Input:
        A - m x n
        b - m x 1
    Output:
        x - n x 1
    """

    x = householder_triangulation_solve(A, b)

    return x

def build_vandermonde(x, max_power):
    """
    Build a vandermonde matrix

    Input:
        x - x values for vandermonde - m nodes
        max_power - maximum power of x in vandermonde - n-1
    Output:
        V - vander monde matrix - m x n
    """

    # vector lengths
    m = len(x)
    n = max_power + 1

    # build powers of each row
    powers = np.arange(0, max_power+1)

    # allocate space and set positions in vandermonde system
    V = np.tile(x, [n, 1])

    # perform power operations
    powers = np.tile(powers, [m, 1])
    V = np.power(V, powers.T)

    return V

def solve(A, b):
    """
    Wrapper/dispatcher for solving a linear system

    Input:
        A - matrix to invert
        b - right hand side vector
    Output:
        x - solution to Ax = b
    """

    success = False

    sizeA = shape(A)
    sizeb = shape(b)

    # matrix solver
    if len(sizeA) > 1:
        # square matrix, standard solving
        if sizeA[0] == sizeA[1]:
            try:
                x = np.linalg.solve(A, b)
                success = True
            except:
                success = False
        # no success yet, or more equations than unknowns - QR
        if not success or sizeA[0] > sizeA[1]:
            [Q, R] = np.linalg.qr(A)
            tmp = matmul(Q.T, b)
            # solve upper triangular system
            try:
                x = np.linalg.solve(R, tmp)
                success = True
            except:
                x = backward_substitute(R, tmp)
                success = (np.sum(np.isinf(x)) == 0)
        # no success yet, or more unknowns than equations - SVD
        if not success or sizeA[0] < sizeA[1]:
            [U, S, V] = np.linalg.svd(A)
            tmp = matmul(U.T, b)
            tmp2 = np.zeros(n)
            tmp2[:m] = tmp / S           # pointwise division
            x = matmul(V.T, tmp2)
    # vector solving
    else:
        x = b / A

    return x

def linalg_solve(A, b):
    """
    Wrapper/dispatcher for solving a linear system

    Input:
        A - matrix to invert
        b - right hand side vector
    Output:
        x - solution to Ax = b
    """
    l = get_length(b)
    try:
        [m, n] = np.shape(A)
    except ValueError:
        Atmp = np.matrix(A)
        [m, n] = np.shape(Atmp)
    if l != m:
        print("Error, right hand side of wrong shape")
        return None

    else:   # may be able to solve, begin dispatcher
        if n == 1 and m == 1:
            # scalar x scalar = scalar
            x = b / A
        elif n == 1 and m != 1:
            # vector x scalar = vector
            x = la.norm(b) / la.norm(A)

        else:
            # matrix x vector = vector

            if m == n:
                # square system, apply standard solver, won't complain
                x = la.solve(A, b)

            elif m > n:
                # more equations than unknown, use qr
                [Q, R] = la.qr(A)
                z = matmul(Q.T, b)
                x = backward_substitute(R, z)

            else:
                # m < n more unknowns than equations, use SVD
                try:
                    [U, S, V] = la.svd(A)
                except la.LinAlgError:
                    A = np.reshape(A, [1, len(A)])
                    [U, S, V] = la.svd(A)
                # here S is a list of the singular values
                z = matmul(U.T, b)
                y = np.zeros(n)
                y[:m] = z / S           # pointwise division
                x = matmul(V.T , y)

        return x


"""Defines how track data is stored."""
import datetime
from JukeBot.utils import humanize_duration

class Track:
    """Represents a track in the queue."""
    def __init__(self, ytdl_data, ctx):
        # Track data from YouTube
        self.source = ytdl_data["formats"][0]["url"]
        self.title = ytdl_data["title"]
        self.thumb = ytdl_data["thumbnails"][2]["url"]
        self.duration = datetime.timedelta(seconds=ytdl_data["duration"])
        self.web_url = ytdl_data["webpage_url"]

        # Additional data for track
        self.human_duration = humanize_duration(self.duration.total_seconds())
        self.requestor = f"{ctx.author.name}#{ctx.author.discriminator}"
        self.requestor_uid = ctx.author.id
        self.voice_channel = None  # Will be set later on in audio_commands.search_yt()
        self.time_started = None  # Will eventually be set via arrow.utcnow() when track begins playing
        self.time_paused = None  # Will eventually be set via arrow.utcnow() when player is paused
        self.total_pause_time = 0  # Stores how long the track has been paused for.

    def __str__(self):
        return f"\"{self.title}\" ({self.duration}), requested by {self.requestor} [{self.web_url}]"

    def time_left(self, time_now):
        diff = (time_now - self.time_started).total_seconds() - self.total_pause_time

        time_left = humanize_duration(self.duration.total_seconds() - diff)
        return time_left


import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn import preprocessing
from sklearn.metrics import f1_score, accuracy_score, precision_score, recall_score
from sklearn.model_selection import train_test_split
from tensorflow.python.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint
from tqdm import tqdm
from transformers import BertTokenizer, TFDistilBertForSequenceClassification
import matplotlib.pyplot as plt

data = pd.read_csv('data/data_mapbook.csv', quotechar="$", header=0, names=["id", "class", "text"], usecols=[1, 2])

# Transform positive/negative values to 1/0s
label_encoder = preprocessing.LabelEncoder()
data['class'] = label_encoder.fit_transform(data['class'])
# data.head()
X = (np.array(data['text']))
y = (np.array(data['class']))

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
X_val, X_test, y_val, y_test = train_test_split(X_test, y_test, test_size=0.5)
print("Train dataset shape: {0}, \nTest dataset shape: {1} \nValidation dataset shape: {2}".format(
    X_train.shape, X_test.shape, X_val.shape))

bert_model = TFDistilBertForSequenceClassification.from_pretrained("sampathkethineedi/industry-classification")
bert_tokenizer = BertTokenizer.from_pretrained("sampathkethineedi/industry-classification")

pad_token = 0
pad_token_segment_id = 0
max_length = 256

def plot_loss(history, label):
    # Use a log scale on y-axis to show the wide range of values.
    plt.semilogy(history.epoch, history.history['loss'],
                 color='b', label='Train ' + label)
    plt.semilogy(history.epoch, history.history['val_loss'],
                 color='b', label='Val ' + label,
                 linestyle="--")
    plt.xlabel('Epoch')
    plt.ylabel('Loss')
    plt.show()

def convert_to_input(text):
    input_ids, attention_masks, token_type_ids = [], [], []

    for x in tqdm(text, position=0, leave=True):
        inputs = bert_tokenizer.encode_plus(x, add_special_tokens=True, max_length=max_length)

        i, t = inputs["input_ids"], inputs["token_type_ids"]
        m = [1] * len(i)

        padding_length = max_length - len(i)

        i = i + ([pad_token] * padding_length)
        m = m + ([0] * padding_length)
        t = t + ([pad_token_segment_id] * padding_length)

        input_ids.append(i)
        attention_masks.append(m)
        token_type_ids.append(t)

    return [np.asarray(input_ids), np.asarray(attention_masks), np.asarray(token_type_ids)]

X_test = convert_to_input(X_test)
X_train = convert_to_input(X_train)
X_val = convert_to_input(X_val)

def to_features(input_ids, attention_masks, token_type_ids, y):
    return {"input_ids": input_ids, "attention_mask": attention_masks, "token_type_ids": token_type_ids}, y

train_ds = tf.data.Dataset.from_tensor_slices((X_train[0], X_train[1], X_train[2], y_train)).map(to_features) \
    .shuffle(100).batch(12).repeat(5)
val_ds = tf.data.Dataset.from_tensor_slices((X_val[0], X_val[1], X_val[2], y_val)).map(to_features).batch(12)
test_ds = tf.data.Dataset.from_tensor_slices((X_test[0], X_test[1], X_test[2], y_test)).map(to_features) \
    .batch(12)

# learning parameters
optimizer = tf.keras.optimizers.Adam(0.00000001)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
metric = tf.keras.metrics.SparseCategoricalAccuracy('accuracy')

file_path = "models/weights_best.h5"

reduce_on_plateau = ReduceLROnPlateau(factor=0.1, patience=10)
checkpoint = ModelCheckpoint(file_path, verbose=1, save_best_only=True)

callbacks_list = [checkpoint, reduce_on_plateau]

bert_model.compile(optimizer=optimizer, loss=loss, metrics=[metric])

bert_model.load_weights("models/weights_at_finish.h5")
bert_history = bert_model.fit(train_ds, epochs=10, validation_data=val_ds, callbacks=callbacks_list)
plot_loss(bert_history, 'loss')

bert_model.save_weights("models/weights_at_finish.h5")

# results
results_true = test_ds.unbatch()
results_true = np.asarray([element[1].numpy() for element in results_true])
print(results_true)

results = bert_model.predict(test_ds)
print(f"Model predictions:\n {results.logits}")

results_predicted = np.argmax(results.logits, axis=1)

print(f"F1 score: {f1_score(results_true, results_predicted, average='weighted')}")
print(f"Accuracy score: {accuracy_score(results_true, results_predicted)}")
print(f"Precision: {precision_score(results_true, results_predicted, average='weighted')}")
print(f"Recall: {recall_score(results_true, results_predicted, average='weighted')}")

# save model
bert_model.save_pretrained('models/bert_pretrained.h5')


"""
different implementations of quicksort (some in-place, some not):

from:

- https://stackoverflow.com/questions/18262306/quicksort-with-python
- https://www.geeksforgeeks.org/python-program-for-quicksort/

"""
import copy

DATA = [1, 200, 50, 485, 22, 22, 3534, 22112]
SORTED = [1, 22, 22, 50, 200, 485, 3534, 22112]

def quicksort1(L):
        x = y = 0
        if L == []:
            return []
        pivot = L[0]
        return (
            quicksort1([x for x in L[1:] if x < pivot])
            +
            [pivot]
            +
            quicksort1([y for y in L[1:] if y >= pivot]))

def test_quicksort1():
    assert quicksort1(DATA) == SORTED

def quicksort2(array):
    """Sort the array by using quicksort."""

    less = []
    equal = []
    greater = []

    if len(array) > 1:
        pivot = array[0]
        for x in array:
            if x < pivot:
                less.append(x)
            elif x == pivot:
                equal.append(x)
            elif x > pivot:
                greater.append(x)
        return quicksort2(less) + equal + quicksort2(greater)
    else:
        return array

def test_quicksort2():
    assert quicksort2(DATA) == SORTED

def quicksort3(xs):
    return xs and (
        quicksort3([i for i in xs[1:] if i < xs[0]])
        +
        [xs[0]]
        +
        quicksort3([i for i in xs[1:] if i >= xs[0]])
    )

def test_quicksort3():
    assert quicksort3(DATA) == SORTED

def test_sorted():
    assert sorted(DATA) == SORTED

def partition(array, low, high):
    pivot = array[high]
    i = low - 1
    for j in range(low, high):
        if array[j] <= pivot:
            i = i + 1
            (array[i], array[j]) = (array[j], array[i])
    (array[i + 1], array[high]) = (array[high], array[i + 1])
    return i + 1

def quicksort4(array, low, high):
    if low < high:
        pi = partition(array, low, high)
        quicksort4(array, low, pi - 1)
        quicksort4(array, pi + 1, high)

def test_quicksort4():
    # in-place
    data = copy.copy(DATA)
    size = len(data)
    quicksort4(data, 0, size - 1)
    assert data == SORTED

def test_all():
    test_quicksort1()
    test_quicksort2()
    test_quicksort3()
    test_quicksort4()
    test_sorted()

if __name__ == '__main__':
    test_all()


from subprocess import call
from stockwalk.models import dbsession, Company, Quote
from os.path import abspath, dirname, join, expanduser, isfile

# lib_dir is where rfbp.R is located
lib_dir = abspath(join(dirname(__file__), '..', '..', 'lib'))
rfbp_loc = join(lib_dir, 'rfbp.R')

attribute = "close"

graph_dir = join(expanduser("~"), "stockwalk_graphs")

# Get company_id of companies who we have at least one quote for
symbols = dbsession.query(Company.id, Company.symbol).filter(Company.id == Quote.company_id).distinct().all()

for s in symbols:
    symbol = s[1]
    file_name = symbol + "_" + attribute + ".png"
    file_full = join(graph_dir, file_name)
    if not isfile(file_full):
        call(["Rscript", rfbp_loc, symbol, attribute])


import torch.nn as nn
import torch
import torch.nn as nn
import torch.nn.functional as F
from .common import *
class DLSS(nn.Module):
    def __init__(self,channels,):
        super(DLSS, self).__init__()
        layers1 = []
        layers1.append(nn.Upsample(scale_factor=2, mode='bicubic', align_corners=None))
        self.net1 = nn.Sequential(*layers1)
        layers2 = []
        layers2.append(nn.Conv2d(in_channels=channels, out_channels=16, kernel_size=3, padding=1, bias=True))
        layers2.append(nn.ReLU(inplace=True))
        layers2.append(nn.Conv2d(in_channels=16, out_channels=64, kernel_size=3, padding=1, bias=True))
        layers2.append(nn.Upsample(scale_factor=2, mode='bicubic', align_corners=None))
        layers2.append(nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1, bias=True))
        layers2.append(nn.ReLU(inplace=True))
        layers2.append(nn.Conv2d(in_channels=64, out_channels=1, kernel_size=3, padding=1, bias=True))
        self.net2 = nn.Sequential(*layers2)
    def forward(self, input):
        return self.net1(input)+self.net2(input)


from rest_framework import generics, mixins, authentication, permissions
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import Product
from .serializers import ProductSerializer
from django.shortcuts import get_object_or_404
from api.mixins import StaffEditorPermissionMixin, UserQuerySetMixin

class ProductListCreateAPIView(
    UserQuerySetMixin,
    StaffEditorPermissionMixin,
    generics.ListCreateAPIView):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer

    def perform_create(self, serializer):
        # If we are having user we can also use below method.
        # serializer.save(user=self.request.user)
        title = serializer.validated_data['title']
        content = serializer.validated_data['content'] or None
        if content is None:
            content = title
        serializer.save(user=self.request.user, content=content)

    # def get_queryset(self, *args, **kwargs):
    #     qs = super().get_queryset(*args, **kwargs)
    #     user = self.request.user
    #     if not user.is_authenticated:
    #         return Product.objects.none()
    #     return qs.filter(user=user)

class ProductDetailAPIView(
    UserQuerySetMixin,
    StaffEditorPermissionMixin,
    generics.RetrieveAPIView):
    """Product Detail API view."""
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    # lookup_field = pk

class ProductUpdateAPIView(
    UserQuerySetMixin,
    StaffEditorPermissionMixin,
    generics.UpdateAPIView):
    """Product Update API view."""
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    lookup_field = 'pk'

    def perform_update(self, serializer):
        instance = serializer.save()
        if not instance.content:
            instance.content = instance.title

class ProductDeleteAPIView(
    UserQuerySetMixin,
    StaffEditorPermissionMixin,
    generics.DestroyAPIView):
    """Product Delete API view."""
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    lookup_field = 'pk'

# class ProductCreateAPIView(generics.CreateAPIView):
#     queryset = Product.objects.all()
#     serializer_class = ProductSerializer
#
#     def perform_create(self, serializer):
#         # If we are having user we can also use below method.
#         # serializer.save(user=self.request.user)
#         title = serializer.validated_data['title']
#         content = serializer.validated_data['content'] or None
#         if content is None:
#             content = title
#         serializer.save(content=content)

class ProductMixinView(
    mixins.ListModelMixin,
    mixins.RetrieveModelMixin,
    mixins.CreateModelMixin,
    mixins.UpdateModelMixin,
    mixins.DestroyModelMixin,
    generics.GenericAPIView):
    """Generic API View for Product."""

    queryset = Product.objects.all()
    serializer_class = ProductSerializer

    def get(self, request, *args, **kwargs):
        pk = kwargs.get('pk')
        if pk is not None:
            return self.retrieve(request, *args, **kwargs)
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, **args, **kwargs)

    def patch(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, **args, **kwargs)

@api_view(["GET", "POST"])
def product_alt_view(request, pk=None, *args, **kwargs):
    """
    Function Based view for API requests.
    """
    method = request.method

    if method == "GET":
        """
        GET Product data.
        LIST Product data.
        url_args ? -> for filtering the data
        """
        if pk is not None:
            # Details View
            obj = get_object_or_404(Product, pk=pk)  # Raises 404 if not found
            data = ProductSerializer(obj, many=False).data
            return Response(data)

        # List View
        queryset = Product.objects.all()
        data = ProductSerializer(queryset, many=True).data
        return Response(data)

    if method == "POST":
        """
        Create an Item/Product
        """
        # Passing the Received data to the serializer
        serializer = ProductSerializer(data=request.data)
        # Serializer checks if the data is valid or not.
        # Adding a raise_exception here will help us better with our
        # exception handling as it will handle the backend validation.
        if serializer.is_valid(raise_exception=True):
            # We are saving instance of the Product Object
            # a like we do with forms for that -> Instance = ProductForm.save()
            title = serializer.validated_data['title']
            content = serializer.validated_data['content'] or None
            if content is None:
                content = title
            serializer.save(content=content)
            return Response(serializer.data)

        return Response({"invalid": "Please Enter a valid data!"}, status=400)


import streamlit as st
import pandas as pd
import requests

st.header("US Data")

params = {
    'order': 'desc',
    'sort': 'percentage_of_total_budget_authority'
}

@st.cache_data
def load_data():
    toptier_codes_response = requests.get(f"https://api.usaspending.gov/api/v2/references/toptier_agencies", params=params)
    toptier_codes_df = pd.DataFrame(toptier_codes_response.json().get('results'))
    return toptier_codes_df

st.subheader("Top 10 Highest Budgets")
col1, col2 = st.columns([1,1])
total_budget = load_data()['budget_authority_amount'].sum()

def top_five(i):
    first_budget = load_data().budget_authority_amount[i:].max()
    first_budget_name = load_data().loc[load_data()['budget_authority_amount'] == first_budget, 'agency_name'].item()
    first_percent = first_budget / total_budget

    col1.metric(f"Number: {i+1} - {first_budget_name}", value=f"${first_budget:,}")
    col2.metric(f"Percentage", value=f"{first_percent:,.2%}")

for i in range(10):
    top_five(i)

df_visual = load_data()[['abbreviation', 'agency_name', 'toptier_code', 'budget_authority_amount']]
df_visual = df_visual.head(10).sort_values(by='budget_authority_amount', ascending=False)
df_visual = df_visual.rename(columns={'abbreviation': 'Abbrev.',
                          'agency_name': 'Agency Name',
                          'toptier_code': 'Top Tier Code',
                          'budget_authority_amount': 'Budget Amount',
                          }).set_index('Top Tier Code')

st.dataframe(df_visual)

st.bar_chart(load_data().head(10), y='budget_authority_amount', x='agency_name')

# response = requests.get("https://api.usaspending.gov/api/v2/agency/310/")

# depart_name = response.json().get('name')
# depart_abbr = response.json().get('abbreviation')
# depart_toptier_code = response.json().get('toptier_code')

# response_df = pd.DataFrame(response.json())

# st.write(depart_name)
# st.write(depart_abbr)
# st.write(depart_toptier_code)

# st.write(response.json())


#!/usr/bin/env python3

from sys import argv

def eraseFile(filename):
    print("Erasing file")
    file = open(filename, 'w')
    file.truncate()
    file.close()

def main(filename):
    print(f"We are going to erase {filename}")
    print(f"Contents of {filename}")
    print(open(filename).read())
    answer = input('[Y/n] ')

    if answer == 'Y':
        eraseFile(filename)

if __name__ == "__main__":
    filename = argv[1]
    main(filename)


# @lc app=leetcode id=524 lang=python3
#
# [524] Longest Word in Dictionary through Deleting
#

# @lc tags=two-pointers;sort

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

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

# @lc group=

# @lc rank=

# @lc code=start
class Solution:
    def findLongestWord(self, s: str, dictionary: List[str]) -> str:
        """ Solution 1 """
        # first sort the dictionary by len of word and then by alphabet
        dictionary.sort(key=lambda x: (-len(x), x))

        n = len(s)
        # then for each word, search the string and see if they can 'match'
        for word in dictionary:
            m = len(word)
            i = 0
            j = 0
            while i < n and j < m:
                if s[i] == word[j]:
                    j += 1
                i += 1
            if j == m:
                return word
        return ''
        """ SOlution 2: without osrting the dictionary """
        def isSubsequence(target, source):
            i = 0
            j = 0
            while i < len(target) and j < len(source):
                if target[i] == source[j]:
                    i += 1
                    j += 1
                else:
                    j += 1
            return i == len(target)
        max_str = ''
        for word in dictionary:
            if isSubsequence(word, s):
                if len(word) > len(max_str) or (len(word) == len(max_str) and word < max_str):
                    max_str = word
        return max_str
        pass
# @lc code=end

# @lc main=start
if __name__ == '__main__':
    print('Example 1:')
    print('Input : ')
    print('s = "abpcplea", dictionary = ["ale","apple","monkey","plea"]')
    print('Exception :')
    print('"apple"')
    print('Output :')
    print(str(Solution().findLongestWord("abpcplea",["ale","apple","monkey","plea"])))
    print()

    print('Example 2:')
    print('Input : ')
    print('s = "abpcplea", dictionary = ["a","b","c"]')
    print('Exception :')
    print('"a"')
    print('Output :')
    print(str(Solution().findLongestWord("abpcplea",["a","b","c"])))
    print()

    pass
# @lc main=end


##########################################################################
####### Reference: https://github.com/pytorch/pytorch/issues/2001#########
##########################################################################

import torch as th
import torch.nn as nn
from torch.autograd import Variable
from collections import OrderedDict

BYTES_PER_PARAM = 4
BYTE_TO_MEGA_BYTE_RATIO = 1024 ** 2

def network_summary(input_size, model, batch_size=None):
    def register_hook(module):
        def hook(module, input, output):
            class_name = str(module.__class__).split('.')[-1].split("'")[0]
            module_idx = len(summary)

            m_key = '%s-%i' % (class_name, module_idx + 1)
            summary[m_key] = OrderedDict()
            summary[m_key]['input_shape'] = list(input[0].size())
            summary[m_key]['input_shape'][0] = batch_size

            if isinstance(output, (list, tuple)):
                summary[m_key]["output_shape"] = [
                    [-1] + list(o.size())[1:] for o in output
                ]
            else:
                summary[m_key]["output_shape"] = list(output.size())
                summary[m_key]["output_shape"][0] = batch_size

            params = 0
            if hasattr(module, 'weight') and hasattr(module.weight, "size"):
                params += th.prod(th.LongTensor(list(module.weight.size())))
                if module.weight.requires_grad:
                    summary[m_key]['trainable'] = True
                else:
                    summary[m_key]['trainable'] = False
            if hasattr(module, 'bias') and hasattr(module.bias, "size"):
                params += th.prod(th.LongTensor(list(module.bias.size())))
            summary[m_key]['nb_params'] = params

        if not isinstance(module, nn.Sequential) and \
                not isinstance(module, nn.ModuleList) and \
                not (module == model):
            hooks.append(module.register_forward_hook(hook))

    # check if there are multiple inputs to the network
    if isinstance(input_size[0], (list, tuple)):
        x = [Variable(th.rand(1, *in_size)) for in_size in input_size]
    else:
        x = Variable(th.rand(1, *input_size))

    # create properties
    summary = OrderedDict()
    hooks = []
    # register hook
    model.apply(register_hook)
    # make a forward pass
    model(x)
    # remove these hooks
    for h in hooks:
        h.remove()

    return summary

import torch
from torchvision import models
from torchbench.network.core import AlexNet

network_name = "resnet152"

num_classes = 1000
model_alexnet = AlexNet(num_classes=num_classes)

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
vgg = models.resnet152().to(device)

input_size = (3, 227, 227)

sm1 = network_summary(input_size, vgg)

total = 0
trainable_params = 0
layers = []
input_shapes = []
output_shapes = []
param_items = []
param_items_bytes = []
trainable_param_items_bytes = []
trainable_params_list = []
row_data = []

delimiter = ":"

save_str = "Layer" + delimiter + "Input Shape" + delimiter + "Output Shape" + delimiter + "Num of Params" + delimiter + "Params (MB)" + delimiter + "Num of Trainable Params" + delimiter + "Trainable Params (MB)\n"

for layer in sm1:
    layer_name = layer
    input_shape = str(sm1[layer]["input_shape"])
    output_shape = str(sm1[layer]["output_shape"])
    params = sm1[layer]["nb_params"]
    layers.append(layer_name)
    input_shapes.append(input_shape)
    output_shapes.append(output_shape)
    trainable_param = 0

    if isinstance(params, torch.Tensor):
        params = params.item()

    param_items.append(params)
    params_in_bytes = params * BYTES_PER_PARAM / BYTE_TO_MEGA_BYTE_RATIO
    param_items_bytes.append(params_in_bytes)

    if "trainable" in sm1[layer]:
        if sm1[layer]["trainable"] == True:
            trainable_param = params
            trainable_params += trainable_param

    trainable_param_items_bytes.append(trainable_param)
    trainable_param_in_bytes = trainable_param * BYTES_PER_PARAM / BYTE_TO_MEGA_BYTE_RATIO
    total += params
    row_data.append(
        [layer_name, input_shape, output_shape, params, params_in_bytes, trainable_param, trainable_param_in_bytes])
    save_str += layer_name + delimiter + str(input_shape) + delimiter + str(output_shape) + delimiter + str(
        params) + delimiter + str(
        params_in_bytes) + delimiter + str(trainable_param) + delimiter + str(trainable_param_in_bytes) + "\n"

    # print(layer, str(input_shape), str(output_shape), str(params))

import termtables as tt

header = ["Layer", "Input Shape", "Output Shape", "Num of Params", "Params (MB)", "Num of Trainable Params",
          "Trainable Params (MB)"]
# data = [
#     [1, 2, 3], [613.23236243236, 613.23236243236, 613.23236243236]
# ]
print(row_data)

tt.print(
    row_data,
    header=header,
    style=tt.styles.ascii_thin,
    padding=(1, 1, 1, 1),
    alignment="ccccccc"
)

print("Total Param Memory : {} MB, Total Trainable Param Memory {} MB".format(total * 4 / (1024 ** 2),
                                                                              trainable_params * 4 / (1024 ** 2)))

with open("stats/" + network_name + ".info", "w") as fp:
    fp.write(save_str)


# HW11:
# Napisać kalkulator który dodaje i odejmuje i podpiąć do niego loggera
# W przypadku gdy podamy wartość string wyrzuci błąd i zapisze go do loggera (poziom WARN lub ERROR)
# Jeśli podane dane były poprawne to zapisze dane wejściowe i wynik do loggera (poziom INFO)
import logging

for handler in logging.root.handlers[:]:
    logging.root.removeHandler(handler)
logging.basicConfig(
    level=logging.INFO,
    filename="example.log")

class Kalkulator():
    @staticmethod
    def dodawanie(a, b):
        if type(a) == int and type(b) == int:
            logging.info("dane poprawne, wynik osiagniety")
            return a + b
        else:
            komunikat = "podane dane są niepoprawne"
            logging.error(komunikat)
            raise TypeError(komunikat)

    @staticmethod
    def odejmowanie(a, b):
        return a - b

if __name__ == "__main__":
    wynik = Kalkulator.dodawanie("5", 4)
    print(wynik)


from discord.ext import commands, tasks
from val import valrank
import discord
class MyCog(commands.Cog):
    def __init__(self, channel, players):
        self.channel = channel
        self.players = players
    @tasks.loop(seconds=60)
    async def val_update(self):
        for person in self.players:
            person[3] = person[2]
            person[2] = valrank(person[0], person[1])
            if person[2]['elo'] != person[3]['elo']:

                if person[2]['elo']//100 > person[3]['elo']//100:
                    msg = f"yipee {person[0]} isnt hardstuck anymore! they went from {person[3]['currenttierpatched']} to {person[2]['currenttierpatched']}!"
                if person[2]['elo']//100 < person[3]['elo']//100:
                    msg = f"common {person[0]} L! they went from {person[3]['currenttierpatched']} to {person[2]['currenttierpatched']}!"
                if person[2]['elo'] > person[3]['elo']:
                    msg = f"yay! {person[0]} gained {person[2]['mmr_change_to_last_game']} rr!"
                if person[2]['elo'] < person[3]['elo']:
                    msg = f"lmao dogwater!!! {person[0]} lost {person[2]['mmr_change_to_last_game']} rr!"
                embedVar = discord.Embed(description=f"""{msg}""")
                await self.channel.send(embed = embedVar)