# from os import listdir
# import shutil
# from os.path import isfile, join
# img_path = "/home/jamal/Desktop/LFW attributes/lfw/"
# save_path = "/home/jamal/Desktop/LFW attributes/more_than_once/"
# onlyfiles = [f for f in listdir(img_path)]
#
# for i in range(len(onlyfiles)):
#     tem_path = img_path + onlyfiles[i]
#     if len(listdir(tem_path))>1:
#         s_path = save_path + onlyfiles[i]
#         shutil.copytree(tem_path,s_path)
#     print(i/len(onlyfiles))
#
#
import pandas as pd
import numpy as np
#import matplotlib.pyplot as plt
import os
from skimage import io, transform
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from torch.utils.data.sampler import SubsetRandomSampler
import sklearn.metrics as mt

# additional files is located in /home/jamal/Downloads/additional_files/
def save_checkpoint(state, filename='/home/jamal/Downloads/additional_files/checkpoint.pth.tar'):
    torch.save(state, filename)

file_path ='/home/jamal/Downloads/additional_files/umdfaces_batch2_ultraface_new1.csv'

images_path = '/home/jamal/Downloads/umdfaces_batch2/'

load_net = 1
# columns = ['ImgId','5_o_Clock_Shadow', ' Arched_Eyebrows', 'Attractive', 'Bags_Under_Eyes', 'Bald', 'Bangs', 'Big_Lips', 'Big_Nose', 'Black_Hair', 'Blond_Hair', 'Blurry', 'Brown_Hair', 'Bushy_Eyebrows', 'Chubby', 'Double_Chin', 'Eyeglasses', 'Goatee', 'Gray_Hair', 'Heavy_Makeup', 'High_Cheekbones', 'Male', 'Mouth_Slightly_Open', 'Mustache', 'Narrow_Eyes', 'No_Beard', 'Oval_Face', 'Pale_Skin', 'Pointy_Nose', 'Receding_Hairline', 'Rosy_Cheeks', 'Sideburns', 'Smiling', 'Straight_Hair', 'Wavy_Hair', 'Wearing_Earrings', 'Wearing_Hat', 'Wearing_Lipstick', 'Wearing_Necklace', 'Wearing_Necktie', 'Young']

columns=['SUBJECT_ID','FILE','FACE_X','FACE_Y','FACE_H','FACE_W','PR_MALE','PR_FEMALE']

cele_attrib = pd.read_csv(file_path,delimiter = ",",names = columns)
# lfw = cele_attrib.set_index('SUBJECT_ID')
lfw = cele_attrib

#len_attrib = len(cele_attrib)
# Select random images form celeba dataset
# rnd_set = np.random.permutation(len_attrib)[0:5]
# for i in rnd_set:
#     idx = ("{:06d}.png".format(i))
#     img_path = images_path+idx
#     img = plt.imread(img_path)
#     plt.imshow(img)
#     plt.show()
#     print(cele_attrib['Male'][i-1])
#
import torch
import torch.nn.functional as F
import torch.nn as nn

class Net(nn.Module):

    def __init__(self):
        super(Net,self).__init__()
        self.conv11 = nn.Conv2d(3,64,5)
        self.n11 = nn.BatchNorm2d(64)
        self.conv21 = nn.Conv2d(64,96,5)
        self.n21 = nn.BatchNorm2d(96)
        self.conv31 = nn.Conv2d(96,128,3)
        self.n31 = nn.BatchNorm2d(128)

        self.conv12 = nn.Conv2d(3,32,7)
        self.n12 = nn.BatchNorm2d(32)
        self.conv22 = nn.Conv2d(32,75,3)
        self.n22 = nn.BatchNorm2d(75)
        #self.conv32 = nn.Conv2d(75,100,3)
        #self.n32 = nn.BatchNorm2d(100)

        self.ip1 = nn.Linear(1712,128)   # change the first parameter in case you change the size of the small image
        self.ip2 = nn.Linear(128,2)
        self.ip3 = nn.Linear(1712,128)   # change the first parameter in case you change the size of the small image
        self.ip4 = nn.Linear(128,2)

    def forward(self,x,y):
        x = self.conv11(x)
        x = self.n11(x)
        x = F.relu(x)

        x = F.max_pool2d(x,5,stride = 3)

        x = self.conv21(x)

        x = self.n21(x)
        x = F.relu(x)
        x = F.max_pool2d(x,5,stride = 3)
        x = self.conv31(x)

        x = self.n31(x)
        x = F.relu(x)
        x = F.max_pool2d(x,5)
        x = x.view(-1,512)

        y = self.conv12(y)

        y = self.n12(y)
        y = F.relu(y)
        # y = F.max_pool2d(y,5,stride = 1)
        y = self.conv22(y)
        y = self.n22(y)
        y = F.relu(y)
        # y = F.max_pool2d(y,5,stride = 2)

        #y = self.conv32(y)
        #y = F.max_pool2d(y,3)
        #y = self.n32(y)
        #y = F.relu(y)
        y = y.view(-1,1200)  # change the second parameter in case you change the size of the small image

        x = torch.cat((x,y),1)
        x1 = self.ip1(x)
        x1 = F.relu(x1)
        x1 = self.ip2(x1)
        x2 = self.ip3(x)
        x2 = F.relu(x2)
        x2 = self.ip4(x2)
        # x2 = x2.mul(-1)
        # x = F.relu(x)
        # x = F.softmax(x,1)
        return x1, x2

class Male_Female_dataset(Dataset):

    def __init__(self,root_dir,shape,transform=None):
        self.root_dir = root_dir
        self.transform = transform
        self.shape = shape
        self.images_names = os.listdir(images_path)

    def __len__(self):
        return len(cele_attrib)

    def __getitem__(self,idx):
        t = torch.rand(1);
        lst = cele_attrib.loc[idx].tolist()
        img1_name = os.path.join(self.root_dir,lst[1])
        image1 = io.imread(img1_name)
        image1 = image1[int(lst[3]):int(lst[3]+lst[5]),int(lst[2]):int(lst[2]+lst[4]),:]
        # import cv2
        # cv2.imshow('a',img)
        # cv2.waitKey(1000)
        # cv2.destroyAllWindows()
        image1 = transform.resize(image1,(150,150))

        if t > 0.5: #different
            different = False
            while not(different):
                h = torch.randint(0,len(self),(1,1))
                ht = torch.Tensor.numpy(h)
                if lfw.loc[idx][0] != lfw.loc[int(ht[0])][0]:
                    different = True

            lstd = cele_attrib.loc[int(ht[0])].tolist()

            img2_name = os.path.join(self.root_dir,lstd[1])
            image2 = io.imread(img2_name)
            image2 = image2[int(lstd[3]):int(lstd[3]+lstd[5]),int(lstd[2]):int(lstd[2]+lstd[4]),:]
            # import cv2
            # cv2.imshow('a',image2)
            # cv2.waitKey(1000)
            # cv2.destroyAllWindows()
            image2 = transform.resize(image2,self.shape)
            image2 = torch.Tensor.float(torch.from_numpy(image2))
            image2 = (torch.Tensor.permute(image2,(2,0,1)))
            image2 = image2/256.0
            image1 = torch.Tensor.float(torch.from_numpy(image1))
            image1 = (torch.Tensor.permute(image1,(2,0,1)))
            image1 = image1/256.0
            annot = 0
            gender =  1 if lst[-2]>.5 else -1

        else: #same
            id = cele_attrib.loc[idx].tolist()[0]
            idx_new = idx+1
            if  cele_attrib.loc[idx+1].tolist()[0]==id:
                lstd = cele_attrib.loc[idx+1].tolist()
                sta = os.path.join(self.root_dir,lstd[1])
            else:
                lstd = cele_attrib.loc[idx-1].tolist()
                sta = os.path.join(self.root_dir,lstd[1])
            # st = img1_name.split('/')
            # st = st[-1]
            # st = st.split('_')
            # nn = st[0:-1]
            # name = ' '.join(nn)
            # num = int(st[-1].split('.')[0])

            # cc = lfw.loc[name]
            # if isinstance(cc.imagenum,str):
            #     print(cc)
            # ls = cc.imagenum.tolist()
            # if str(num) in ls:
            #     ls.remove(str(num))
            #
            # h = torch.randint(0,len(ls),(1,1))
            # ht = torch.Tensor.numpy(h)
            # ht = ht[0]
            # num2 = str(int(ls[int(ht)])).zfill(4)
            # sta = img1_name.split('_')
            # stemp = sta[-1].split('.')
            # stemp[0] = num2
            # stemp = '.'.join(stemp)
            # sta[-1]=stemp
            # sta = '_'.join(sta)
            image2 = io.imread(sta)
            image2 = transform.resize(image2,self.shape)
            image2 = torch.Tensor.float(torch.from_numpy(image2))
            image2 = (torch.Tensor.permute(image2,(2,0,1)))
            image2 = image2/256.0
            image1 = torch.Tensor.float(torch.from_numpy(image1))
            image1 = (torch.Tensor.permute(image1,(2,0,1)))
            image1 = image1/256.0
            annot = 1
            gender = 1 if lst[-2]>.5 else -1
        # annot = annot.astype('float').reshape(-1,2)
        sample = {'image1':image1,'image2':image2, 'same' : annot, 'gender' : gender}
        return sample

use_cuda = torch.cuda.is_available()
use_cuda=0
device = torch.device("cuda" if use_cuda else "cpu")
print(device)
shape = (12,12)
ip_shape = (150,150)
train_ratio = 0
val_ratio = 0
test_ratio = 1-train_ratio-val_ratio
dataset = Male_Female_dataset(images_path,shape)
# dataset.__getitem__(175534-1)
if load_net:
    net = Net().to(device)
    checkpoint = torch.load('/home/jamal/Desktop/trained nn/checkpoint.pth.tar',map_location='cpu')
    net.load_state_dict ( checkpoint['state_dict'])
    optimizer = optim.Adam(net.parameters(),lr = 0.001, weight_decay = 0.0005)
    optimizer.load_state_dict = checkpoint['optimizer']
else:

    net = Net().to(device)
    optimizer = optim.Adam(net.parameters(),lr = 0.001, weight_decay = 0.0005)
    checkpoint= {'epoch':0}

optimizer = optim.Adam(net.parameters(),lr = 0.01, weight_decay = 0.0005)
index = np.random.permutation(len(dataset)-5)
train_data_length = int(train_ratio*len(index))
val_data_length = int(val_ratio*len(index))
test_data_length = int(test_ratio*len(index))
train_index = index[:train_data_length]
val_index = index[train_data_length:(train_data_length+val_data_length)]
test_index = index[train_data_length+val_data_length:]

train_dataloader = DataLoader(dataset,batch_size=30,sampler = SubsetRandomSampler(train_index))

err_same = []
err_gender = []

acc1 = []
acc2 = []

criterion = nn.CrossEntropyLoss()
criterion2 = nn.CrossEntropyLoss()

# for ep in range(checkpoint['epoch'],5):
#     for i,data in enumerate(train_dataloader):
#         optimizer.zero_grad()
#         input1, input2 , label, gender = data.items()
#         input1, input2 = input1[1].to(device), input2[1].to(device)
#         label1 = label[1].to(device)
#         gender = gender[1].to(device)
#         gender = (gender+1)/2
#         #label = torch.Tensor.long(label[1])
#         output = net(input1,input2)
#         loss1 = criterion (output[0],label1)
#         loss1.backward(retain_graph=True)
#         # optimizer.step()
#         loss2 = criterion2 (output[1],gender)
#         loss2 = loss1 - loss2.mul(0.0001)  #new line
#         loss2.backward()
#         optimizer.step()
#         err_same.append(loss1.item())
#         err_gender.append(loss2.item())
#         if (i%5==0):
#             print (loss1,i,ep)
#             print (loss2,i,ep)
#
#         if (i%50==0):
#             val_index1 = np.random.permutation(val_index)[:100]
#             val_dataloader = DataLoader(dataset,batch_size=30,sampler = SubsetRandomSampler(val_index1))
#             val_iter = iter(val_dataloader)
#
#             total = 0
#             correct1 = 0
#             correct2 = 0
#             for j,dataj in enumerate(val_dataloader):
#                 input1j, input2j, labelj, gender = dataj.items()
#                 input1j, input2j = input1j[1].to(device), input2j[1].to(device)
#
#                 labelj = labelj[1].to(device)
#                 gender = gender[1].to(device)
#                 gender = (gender+1)/2
#                 output = net(input1j,input2j)
#                 _,predicted1 = torch.max(output[0].data,1)
#                 _,predicted2 = torch.max(output[1].data,1)
#                 total +=labelj.size(0)
#                 correct1 += (predicted1 == labelj).sum().item()
#                 correct2 += (predicted2 == gender).sum().item()
#             print('Accuracy_LH: %d %%'%(100*correct1/total))
#             print('Accuracy_MF: %d %%'%(100*correct2/total))
#             acc1.append(100*correct1/total) #low high
#             acc2.append(100*correct2/total) # male female
#     save_checkpoint({
#             'epoch': ep + 1,
#             'state_dict': net.state_dict(),
#
#             'optimizer' : optimizer.state_dict(),
#         })
# #
val_index1 = np.random.permutation(val_index)[:100]
val_dataloader = DataLoader(dataset,batch_size=10,sampler = SubsetRandomSampler(test_index))
val_iter = iter(val_dataloader)
predicted_lables = []
real_labels_g = []
predicted_lables_g = []
real_labels = []
total = 0
correct1 = 0
correct2 = 0
for j,dataj in enumerate(val_dataloader):
    input1j, input2j, labelj, gender = dataj.items()
    input1j, input2j = input1j[1].to(device), input2j[1].to(device)

    labelj = labelj[1].to(device)
    gender = gender[1].to(device)
    gender = (gender+1)/2
    output = net(input1j,input2j)
    _,predicted1 = torch.max(output[0].data,1)
    _,predicted2 = torch.max(output[1].data,1)
    total +=labelj.size(0)
    correct1 += (predicted1 == labelj).sum().item()
    correct2 += (predicted2 == gender).sum().item()
    predicted_lables.append(torch.Tensor.numpy(predicted1))
    real_labels.append(torch.Tensor.numpy(labelj))

    predicted_lables_g.append(torch.Tensor.numpy(predicted2))
    real_labels_g.append(torch.Tensor.numpy(gender))
    print('Accuracy_LH: %d %%'%(100*correct1/total))
    print('Accuracy_MF: %d %%'%(100*correct2/total))
    acc1.append(100*correct1/total)
    acc2.append(100*correct2/total)
    print(mt.confusion_matrix(np.array(real_labels).flatten(),np.array(predicted_lables).flatten()))

    print(mt.confusion_matrix(np.array(real_labels_g).flatten(),np.array(predicted_lables_g).flatten()))
# # for ep in range(checkpoint['epoch'],5):
#     for i,data in enumerate(train_dataloader):
#         optimizer.zero_grad()
#         input1, input2 , label = data.items()
#         input1, input2 = input1[1].to(device), input2[1].to(device)
#         label1 = label[1].to(device)
#         #label = torch.Tensor.long(label[1])
#         output = net(input1,input2)
#         loss = criterion (output,label1)
#         loss.backward()
#         optimizer.step()
#         err.append(loss.item())
#         if (i%5==0):
#             print (loss,i,ep)
#
#         if (i%50==0):
#             val_index1 = np.random.permutation(val_index)[:100]
#             val_dataloader = DataLoader(dataset,batch_size=30,sampler = SubsetRandomSampler(val_index1))
#             val_iter = iter(val_dataloader)
#
#             total = 0
#             correct = 0
#             for j,dataj in enumerate(val_dataloader):
#                 input1j, input2j, labelj = dataj.items()
#                 input1j, input2j = input1j[1].to(device), input2j[1].to(device)
#
#                 labelj = labelj[1].to(device)
#                 output = net(input1j,input2j)
#                 _,predicted = torch.max(output.data,1)
#                 total +=labelj.size(0)
#                 correct += (predicted == labelj).sum().item()
#             print('Accuracy: %d %%'%(100*correct/total))
#             acc.append(100*correct/total)
#     save_checkpoint({
#             'epoch': ep + 1,
#             'state_dict': net.state_dict(),
#
#             'optimizer' : optimizer.state_dict(),
#         })
#
#
#
#
#
#
#
# # sample = dataset[1]
# # img = plt.imread(images_path+'000001.png')
# # img2 = np.expand_dims(img,0)
# # img3 = np.rollaxis(img2,3,1)
# # output = Net(torch.from_numpy(img3))


print("Loading modules...")
from os import remove, environ, path
environ["DISPLAY"] = ":0" # this line may or may not be needed depending on the system
from concurrent.futures import ProcessPoolExecutor
from csv import writer
from ctypes import c_double
from hyperspy.api import load
from math import ceil
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import cm
from matplotlib import colors
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from matplotlib.ticker import LinearLocator
from multiprocessing import Array
from numpy import sqrt, array, ndenumerate, arange, min, max, percentile, linspace
from numpy.ctypeslib import as_array
from pandas import DataFrame
from PIL import Image, ImageTk
from pixstem.api import PixelatedSTEM
from seaborn import heatmap
import tkinter as tk
from tkinter import font

print("Modules loaded.")

file = None
distances = None
currFunc = None

def setCurrFunc(funcName):
    global currFunc, file, distances
    currFunc = str(funcName)
    entry.delete(0, tk.END)
    if currFunc is "loadFile":
        entry.bind("<Return>", getEntry)
        label1['text'] = label1['text'] + "Please enter the path of the input file in the text box provided then press Enter.\n"
    elif currFunc is "toCSV":
        if file is None:
            label1['text'] = label1['text'] + "Please load a file before saving data.\n"
        elif distances is None:
            label1['text'] = label1['text'] + "Please analyze the file before saving data.\n"
        else:
            entry.bind("<Return>", getEntry)
            label1['text'] = label1['text'] + "Please enter the path of the file you want to save to in the text box provided then press Enter.\n"
    elif currFunc is "analysis":
        if file is None:
            label1['text'] = label1['text'] + "Please load a file before starting analysis.\n"
        else :
            entry.bind("<Return>", getEntry)
            label1['text'] = label1['text'] + "Please enter the number of rows and columns you would like to analyze, as integers, seperated by spaces. Press Enter when ready.\n"

def getEntry(event):
    global currFunc
    if currFunc is "loadFile":
        entry.unbind("<Return>")
        loadFile(entry.get())
    elif currFunc is "analysis":
        entry.unbind("<Return>")
        startAnalysis(entry.get())
    elif currFunc is "toCSV":
        entry.unbind("<Return>")
        toCSV(entry.get())

def loadFile(filename = None):
    global file
    label1['text'] = label1['text'] + "Loading file...\n"
    root.update()
    try:
        file = load(filename)
        label1['text'] = label1['text'] + "File loaded.\n"
    except:
        label1['text'] = label1['text'] + "Error loading. Please check path and try again.\n"
    entry.delete(0, tk.END)
    #entry.unbind("<Return>")

def distance(x1, y1, x2, y2):
    return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2))

def intensity(values):
    s = PixelatedSTEM(file.inav[values[0], values[1]])
    imarray = array(s)
    distances[values[1]][values[0]] = imarray[values[3]][values[2]]

def findCenter(im, peak):
    center = (0,0)
    maximum = 0
    for (x,y) in ndenumerate(peak):
        for (a, b) in y:
            if (int(a) < len(im) and int(b) < len(im) and im[int(a)][int(b)] > maximum):
                maximum = im[int(a)][int(b)]
                center = (b, a)
    return center

def multiprocessing_func(values):
    global distances
    s = PixelatedSTEM(file.inav[values[0], values[1]])
    imarray = array(s)
    s = s.rotate_diffraction(0,show_progressbar=False)
    ############################################################################################################################
    st = s.template_match_disk(disk_r=5, lazy_result=False, show_progressbar=False)
    peak_array = st.find_peaks(lazy_result=False, show_progressbar=False)
    peak_array_com = s.peak_position_refinement_com(peak_array, lazy_result=False, show_progressbar=False)
    s_rem = s.subtract_diffraction_background(lazy_result=False, show_progressbar=False)
    peak_array_rem_com = s_rem.peak_position_refinement_com(peak_array_com, lazy_result=False, show_progressbar=False)
    ############################################################################################################################
    center = findCenter(imarray, peak_array_rem_com)

    # finds the specific spot and adding that distance to the array
    posDistance = 0
    closestPoint = center

    for (x,y) in ndenumerate(peak_array_rem_com):
        minimum = 999999
        for (a, b) in y:
            dis = distance(values[2], values[3], b, a)
            if dis < minimum:
                minimum = dis
                closestPoint = (b, a)
    posDistance = distance(closestPoint[0], closestPoint[1], center[0], center[1])
    distances[values[1]][values[0]] = round(posDistance, 2)

def startAnalysis(values = None):
    global file, currFunc
    pointxy = None
    methodOfAnalysis = ""
    if pointxy is None:
        def assignMethod(method):
            global methodOfAnalysis
            methodOfAnalysis = method
        def Mousecoords(event):
            global methodOfAnalysis
            pointxy = (int(event.x * 144 / 400), int(event.y * 144 / 400)) # get the mouse position from event
            l['text'] = l['text'] + str(pointxy[0]) + " " + str(pointxy[1]) + "\n"
            l['text'] = l['text'] + "Starting analysis...\n"
            r.update()
            analysis(pointxy, values, methodOfAnalysis)
            remove("temp.png")
            c2.unbind('<Button-1>')
            r.destroy()
            label1['text'] = label1['text'] + "Analysis complete.\n"

        s = PixelatedSTEM(file.inav[0, 0])
        s.save("temp.png")
        img = Image.open("temp.png")
        img = img.resize((400,400), Image.ANTIALIAS)
        img.save('temp.png')

        r = tk.Toplevel(root)

        c = tk.Canvas(r, height=720, width=1080)
        c.pack()
        f = tk.Frame(r, bg='#333333')
        f.place(relwidth=1, relheight=1)
        l = tk.Message(f, bg='#999999', font=('Calibri', 15), anchor='nw', justify='left', highlightthickness = 0, bd=0, width = 1000)
        l.place(relx=0.05, rely=0.7, relwidth=0.9, relheight=0.2)
        b1 = tk.Button(f, text='Intensity Mapping', bg='#404040', font=('Calibri', 15), highlightthickness = 0, bd=0, activebackground='#666666', activeforeground='#ffffff', command=lambda: assignMethod("intensity"), pady=0.02, fg='#ffffff')
        b1.place(relx=0.2, rely=0.6, relwidth=0.2, relheight=0.05)
        b2 = tk.Button(f, text='Strain Mapping', bg='#404040', font=('Calibri', 15), highlightthickness = 0, bd=0, activebackground='#666666', activeforeground='#ffffff', command=lambda: assignMethod("strain"), pady=0.02, fg='#ffffff')
        b2.place(relx=0.6, rely=0.6, relwidth=0.2, relheight=0.05)
        c2 = tk.Canvas(r, width=400, height=400)
        c2.place(relx=0.3)
        img = ImageTk.PhotoImage(Image.open("temp.png"))
        c2.create_image(0, 0, anchor='nw', image=img)
        c2.bind('<Button-1>', Mousecoords)
        l['text'] = l['text'] + "Please click on the method of analysis and then the point you would like to analyze from the diffraction pattern above.\n"
        r.mainloop()

def analysis(pointxy, values, methodOfAnalysis):
    global file, distances
    t = values.split(" ")
    COL = int(t[1])
    ROW = int(t[0])

    list = []
    for r in range(ROW):
        for c in range(COL):
            list.append((r, c, pointxy[0], pointxy[1]))

    shared_array_base = Array(c_double, ROW*COL)
    distances = as_array(shared_array_base.get_obj())
    distances = distances.reshape(COL, ROW)

    with ProcessPoolExecutor() as executor:
        if methodOfAnalysis is "strain":
            executor.map(multiprocessing_func, list)
        else:
            executor.map(intensity, list)
    entry.delete(0, tk.END)

def toCSV(filename = None):
    global distances
    f = open(filename, "w")
    w = writer(f)
    for i in distances:
        w.writerow(i)
    f.close()
    label1['text'] = label1['text'] + "File saved.\n"
    entry.delete(0, tk.END)
    #entry.unbind("<Return>")

def heatMapMaker(minimum, maximum, parity = 0):
    global distances
    data = distances.copy()
    df = DataFrame(data, columns=arange(len(data[0])), index=arange(len(data)))
    if parity == 1:
        _, a = plt.subplots(figsize=(6,5.5))
        gray = cm.get_cmap('gray', 512)
        newcolors = gray(linspace(0.15, 0.85, 2048))
        white = array([255/256, 255/256, 255/256, 1])
        newcolors[:1, :] = white
        newcolors[2047:, :] = white
        newcmp = colors.ListedColormap(newcolors)
        chart = heatmap(df, cmap=newcmp, vmin = minimum, vmax = maximum, square=True)
        return chart.get_figure()
    else:
        _, a = plt.subplots(figsize=(6,5.5))
        chart1 = heatmap(df, cmap=cm.get_cmap("gray"),ax=a, vmin = minimum, vmax = maximum, square=True)
        return chart1.get_figure()

def barChart(INTERVAL = 0.01):
    global distances
    if file is None:
        label1['text'] = label1['text'] + "Please load a file before creating a bar chart.\n"
    elif distances is None:
        label1['text'] = label1['text'] + "Please analyze the file before creating a bar chart.\n"
    else:
        label1['text'] = label1['text'] + "Creating bar chart. This might take several minutes depending on the size of data.\n"
        root.update()
        dist = distances.copy()
        dist = dist.flatten()
        x_pos = arange(min(dist), max(dist), INTERVAL) # this 0.01 is the distance between each x-axis label. So for example it goes 1.0, 1.01, 1.02, 1.03...
        x_pos = [round(num, 2) for num in x_pos]
        y_pos = arange(len(x_pos))
        ################################################################################################################################
        from collections import Counter
        counter = Counter(dist)
        counts = []
        for i in x_pos:
            counts.append(counter[i]) if i in counter.keys() else counts.append(0)
        ################################################################################################################################
        fig, a = plt.subplots(figsize=(6,5.5))
        plt.xticks(y_pos, x_pos, fontsize = 8)
        plt.xlabel('Distance from center peek', fontsize = 10)
        plt.ylabel('Counts', fontsize = 10)
        plt.title('Distance Counts', fontsize = 10)
        plt.bar(y_pos, counts, align='center', alpha=0.95) # creates the bar plot

        axs = plt.gca()
        plt.setp(axs.get_xticklabels(), rotation=90)
        axs.tick_params(axis='x', which='major', labelsize=10)
        axs.tick_params(axis='y', which='major', labelsize=10)

        [l.set_visible(False) for (i,l) in enumerate(axs.xaxis.get_ticklabels()) if i % (ceil((len(distances[0]) * len(distances)) / 100) * 2) != 0]
        # The '2' is the every nth number of labels its shows on the x-axis. So rn is shows every 2nd label.

        plt.gcf().subplots_adjust(bottom = 0.23)
        plt.rcParams["figure.dpi"] = 100

        def scopeHeatMap(event):
            values = e.get().split(" ")
            minimum = float(values[0])
            maximum = float(values[1])
            f = heatMapMaker(minimum, maximum, 1)
            chart_type = FigureCanvasTkAgg(f, barChartWindow)
            chart_type.draw()
            chart_type.get_tk_widget().place(relx=0.51, rely=0.2)

        barChartWindow = tk.Toplevel(root)
        barChartWindow.geometry('1920x1080')
        chart_type = FigureCanvasTkAgg(plt.gcf(), barChartWindow)
        chart_type.draw()
        chart_type.get_tk_widget().place(relx=0.0, rely=0.2, relwidth=0.5)
        m = tk.Message(barChartWindow, font=('Calibri', 15), highlightthickness = 0, bd=0, width=1000, justify='center')
        m['text'] = "Enter the minimum value and the maximum value seperated by a space. Press Enter to create the heatmap with these specifications"
        m.place(relx = 0.25, rely=0.05)
        e = tk.Entry(barChartWindow, font=('Calibri', 15))
        e.place(relx=0.44, rely=0.1)
        e.bind("<Return>", scopeHeatMap)

def outlier(data):
    data = data.flatten()
    q1 = percentile(data, 25)
    q3 = percentile(data, 75)
    iqr = q3 - q1
    minimum = q1 - (1.5 * iqr)
    maximum = q3 + (1.5 * iqr)
    return minimum, maximum

def heatMap():
    global distances
    if file is None:
        label1['text'] = label1['text'] + "Please load a file before creating a heat map.\n"
    elif distances is None:
        label1['text'] = label1['text'] + "Please analyze the file before creating a heat map.\n"
    else:
        minimum, maximum = outlier(distances)
        fig = heatMapMaker(minimum, maximum)
        heatMapWindow = tk.Toplevel(root)
        heatMapWindow.geometry('1280x720')
        chart_type = FigureCanvasTkAgg(fig, heatMapWindow)
        chart_type.draw()
        chart_type.get_tk_widget().pack()

if __name__ == "__main__":

    HEIGHT = 1080
    WIDTH = 1920

    root = tk.Tk()

    canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
    canvas.pack()
    frame = tk.Frame(root, bg='#333333')
    frame.place(relwidth=1, relheight=1)

    # Menu Label
    label = tk.Label(frame, text='Menu', bg='#333333', font=('Times New Roman', 50), fg='#ffffff')
    label.place(relx=0.40, rely=0.05, relwidth=0.2, relheight=0.05)

    # Text Output box
    label1 = tk.Message(frame, bg='#999999', font=('Calibri', 15), anchor='nw', justify='left', highlightthickness = 0, bd=0, width = 1500)
    label1.place(relx=0.1, rely=0.5, relwidth=0.8, relheight=0.35)

    # Entry box
    entry = tk.Entry(frame, font=('Calibri', 15))
    entry.place(relx=0.1, rely=0.9, relwidth=0.8, relheight=0.05)

    # Buttons
    button = tk.Button(frame, text='Load File', bg='#404040', font=('Calibri', 30), highlightthickness = 0, bd=0, activebackground='#666666', activeforeground='#ffffff', command=lambda: setCurrFunc("loadFile"), pady=0.02, fg='#ffffff')
    button.place(relx=0.42, rely=0.15, relwidth=0.16, relheight=0.05)

    button1 = tk.Button(frame, text='Start Analysis', bg='#404040', font=('Calibri', 30), highlightthickness = 0, bd=0, activebackground='#666666', activeforeground='#ffffff', command=lambda: setCurrFunc("analysis"), pady=0.02, fg='#ffffff')
    button1.place(relx=0.39, rely=0.22, relwidth=0.22, relheight=0.05)

    button2 = tk.Button(frame, text='Create Bar Chart', bg='#404040', font=('Calibri', 30), highlightthickness = 0, bd=0, activebackground='#666666', activeforeground='#ffffff', command=lambda: barChart(), pady=0.02, fg='#ffffff')
    button2.place(relx=0.375, rely=0.29, relwidth=0.25, relheight=0.05)

    button3 = tk.Button(frame, text='Create Heat Map', bg='#404040', font=('Calibri', 30), highlightthickness = 0, bd=0, activebackground='#666666', activeforeground='#ffffff', command=lambda: heatMap(), pady=0.02, fg='#ffffff')
    button3.place(relx=0.38, rely=0.36, relwidth=0.24, relheight=0.05)

    button4 = tk.Button(frame, text='Transfer Data to .csv', bg='#404040', font=('Calibri', 30), highlightthickness = 0, bd=0, activebackground='#666666', activeforeground='#ffffff', command=lambda: setCurrFunc("toCSV"), pady=0.02, fg='#ffffff')
    button4.place(relx=0.34, rely=0.43, relwidth=0.32, relheight=0.05)

    root.mainloop()
    if path.exists("temp.png"):
        remove("temp.png")


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from flask import current_app, request
from flask.ext.restful import fields, marshal, abort
from flask.ext.bouncer import requires, POST, PUT, DELETE
from api.BaseApplicationResource import BaseApplicationResource
from isodate import parse_date, parse_time
from api_exceptions import format_exception
from domain.models import Rammetid, RammetidSlot
from BaseResource import ISO8601DateTime, get_resource_for_uri, get_umbrella_organisation_for_uri
from ResourceResource import resource_fields
from common_fields import umbrella_organisation_fields
from BaseResource import BaseResource

generic_slot_fields = {
    'id': fields.Integer,
    'start_date': ISO8601DateTime,
    'end_date': ISO8601DateTime,
    'start_time': ISO8601DateTime,
    'end_time': ISO8601DateTime,
    'week_day': fields.Integer,
}

rammetid_fields = {
    'id': fields.Integer,
    'text': fields.String,
    'umbrella_organisation': fields.Nested(umbrella_organisation_fields),
    'resource': fields.Nested(resource_fields),
    'rammetid_slots': fields.Nested(generic_slot_fields),
    'status': fields.String,
    'create_time': ISO8601DateTime
}

def parse_repeating_slot(data):
    start_date = parse_date(data["start_date"])
    end_date = parse_date(data["end_date"])
    start_time = parse_time(data["start_time"])
    end_time = parse_time(data["end_time"])
    week_day = data["week_day"]
    return RammetidSlot(
        week_day,
        start_date,
        end_date,
        start_time,
        end_time
    )

class RammetidResource(BaseResource):
    t = Rammetid
    type_name = "rammetid"
    method_decorators = [format_exception]

    def get(self, rammetid_id=None):
        if rammetid_id:
            rammetid = self.get_object_by_id(rammetid_id)
            return marshal(rammetid, rammetid_fields)
        else:
            resource_uri = request.args.get('resource_uri')
            umbrella_organisation_uri = request.args.get('umbrella_organisation_uri')
            if "only_periods" in request.args and resource_uri and umbrella_organisation_uri:
                resource = self.get_resource_with_id_or_uri(None, resource_uri)
                umbrella_organisation = get_umbrella_organisation_for_uri(umbrella_organisation_uri)
                rammetids = current_app.db_session.query(RammetidSlot.start_date.label('start_date'), RammetidSlot.end_date.label('end_date')) \
                    .filter(Rammetid.id == RammetidSlot.rammetid_id, Rammetid.resource_id == resource.id, Rammetid.umbrella_organisation_id == umbrella_organisation.id) \
                    .distinct(RammetidSlot.start_date, RammetidSlot.end_date) \
                    .group_by(RammetidSlot.start_date, RammetidSlot.end_date) \
                    .order_by(RammetidSlot.start_date, RammetidSlot.end_date)

                res = [dict(zip(['start_date', 'end_date'], r)) for r in rammetids.all()]

                return marshal(res, {'start_date': ISO8601DateTime, 'end_date': ISO8601DateTime})
            else:
                rammetids = current_app.db_session.query(Rammetid).order_by(Rammetid.id)
                return marshal(rammetids.all(), rammetid_fields)

    @requires(POST, Rammetid)
    def post(self):
        data = request.get_json()

        resource_data = data.get("resource", None)
        if resource_data:
            resource_uri = resource_data["uri"]
            resource = get_resource_for_uri(resource_uri)
            if not resource:
                abort(403, __error__=[u'Ressursen ble ikke funnet.'])
        else:
            abort(403, __error__=[u'Data om ressursen mangler.'])

        umbrella_organisation_data = data.get("umbrella_organisation", None)
        if umbrella_organisation_data:
            umbrella_organisation_uri = umbrella_organisation_data["uri"]
            umbrella_organisation = get_umbrella_organisation_for_uri(umbrella_organisation_uri)
            if not umbrella_organisation:
                abort(403, __error__=[u'Paraplyorganisasjonen ble ikke funnet.'])
        else:
            abort(403, __error__=[u'Data om paraplyorganisasjonen mangler.'])

        rammetid = Rammetid(umbrella_organisation, resource)

        rammetid_slots_data = data.get("rammetid_slots", None)
        if rammetid_slots_data:
            for rammetid_slot in rammetid_slots_data:
                rammetid_slot_parsed = parse_repeating_slot(rammetid_slot)
                if BaseApplicationResource.is_conflict_rammetid(resource, rammetid_slot_parsed.start_date, rammetid_slot_parsed.end_date, rammetid_slot_parsed.week_day,
                                                                rammetid_slot_parsed.start_time, rammetid_slot_parsed.end_time):
                    abort(
                        400,
                        __error__=[u'Tiden er ikke tilgjengelig']
                    )
                if BaseApplicationResource.is_conflict_blocked_time(resource, rammetid_slot_parsed.start_date, rammetid_slot_parsed.end_date, rammetid_slot_parsed.week_day,
                                                                    rammetid_slot_parsed.start_time, rammetid_slot_parsed.end_time):
                    abort(
                        400,
                        __error__=[u'Tiden er blokkert']
                    )
                rammetid.add_rammetid_slot(rammetid_slot_parsed)

        current_app.db_session.add(rammetid)
        current_app.db_session.commit()
        current_app.db_session.refresh(rammetid)

        return marshal(rammetid, rammetid_fields), 201

    @requires(PUT, Rammetid)
    def put(self, rammetid_id):
        data = request.get_json()

        rammetid = self.get_object_by_id(rammetid_id)

        status = data.get("status", None)

        resource_data = data.get("resource", None)
        if resource_data:
            resource_uri = resource_data["uri"]
            resource = get_resource_for_uri(resource_uri)
            if not resource:
                abort(403, __error__=[u'Ressursen ble ikke funnet.'])
        else:
            abort(403, __error__=[u'Data om ressursen mangler.'])

        # The rammetid might have been
        # moved to a different resource
        rammetid.resource = resource

        # The rammtid status might
        # have been changed
        rammetid.status = status

        rammetid_slots_data = data.get("rammetid_slots", None)
        if rammetid_slots_data:
            for rammetid_slot in rammetid_slots_data:
                rammetid_slot_parsed = parse_repeating_slot(rammetid_slot)
                if BaseApplicationResource.is_conflict_rammetid(resource, rammetid_slot_parsed.start_date, rammetid_slot_parsed.end_date, rammetid_slot_parsed.week_day,
                                                                rammetid_slot_parsed.start_time, rammetid_slot_parsed.end_time):
                    abort(
                        400,
                        __error__=[u'Tiden er ikke tilgjengelig']
                    )
                if BaseApplicationResource.is_conflict_blocked_time(resource, rammetid_slot_parsed.start_date, rammetid_slot_parsed.end_date, rammetid_slot_parsed.week_day,
                                                                    rammetid_slot_parsed.start_time, rammetid_slot_parsed.end_time):
                    abort(
                        400,
                        __error__=[u'Tiden er blokkert']
                    )

                rammetid.add_rammetid_slot(rammetid_slot_parsed)

        current_app.db_session.add(rammetid)
        current_app.db_session.commit()
        current_app.db_session.refresh(rammetid)

        return marshal(rammetid, rammetid_fields)

    @requires(DELETE, Rammetid)
    def delete(self, rammetid_id):
        rammetid = self.get_object_by_id(rammetid_id)
        current_app.db_session.delete(rammetid)
        current_app.db_session.commit()
        return "", 204


inputs = ['ball', 'strike', 'ball', 'strike', 'strike']

b_count = 0
s_count = 0

for i in range(len(inputs)):

    if inputs[i] == "ball":
        b_count += 1
    else:
        s_count += 1

    if b_count >= 4:
        print("fourball!")
    elif s_count >= 3:
        print("out!")
    else:
        print(inputs[i]+"!")


from CvPythonExtensions import *
import CvUtil
import ScreenInput
import CvScreenEnums
gc = CyGlobalContext()
bNoMovies = False

class CvPediaMovie:
	def __init__(self, main):
		self.iType = -1
		self.iEntry = -1
		self.top = main
		self.W_EXIT = 100
		self.H_EXIT = 30

	def interfaceScreen(self, iType, iEntry):
		screen = CyGInterfaceScreen( "MoviePlayer", CvScreenEnums.PLATYPEDIA_MOVIE)
		self.iType = iType
		self.iEntry = iEntry
		global bNoMovies
		bNoMovies = CyUserProfile().getGraphicOption(GraphicOptionTypes.GRAPHICOPTION_NO_MOVIES)
		CyUserProfile().setGraphicOption(GraphicOptionTypes.GRAPHICOPTION_NO_MOVIES, False)
		self.W_MOVIE = screen.getXResolution()
		self.H_MOVIE = screen.getXResolution() * 2 / 3
		if self.H_MOVIE > screen.getYResolution():
			self.H_MOVIE = screen.getYResolution()
			self.W_MOVIE = self.H_MOVIE * 3 / 2
		self.X_MOVIE = (screen.getXResolution() - self.W_MOVIE) / 2
		self.Y_MOVIE = (screen.getYResolution() - self.H_MOVIE) / 2
		screen.showScreen(PopupStates.POPUPSTATE_IMMEDIATE, False)
		screen.addPanel( "MoviePlayerBG", u"", u"", True, False, -10, -10, screen.getXResolution() +20, screen.getYResolution() + 20, PanelStyles.PANEL_STYLE_MAIN )
		screen.enableWorldSounds(False)

		if self.iType == 8201:
			sDescription =gc.getCorporationInfo(iEntry).getDescription()
			self.szMovieFile = gc.getCorporationInfo(self.iEntry).getMovieFile()
		elif self.iType == 7869:
			sDescription =gc.getReligionInfo(iEntry).getDescription()
			self.szMovieFile = gc.getReligionInfo(self.iEntry).getMovieFile()
		elif self.iType == 7870:
			sDescription =gc.getBuildingInfo(iEntry).getDescription()
			self.szMovieFile = gc.getBuildingInfo(self.iEntry).getMovie()
		elif self.iType == 6785:
			sDescription =gc.getProjectInfo(iEntry).getDescription()
			self.szMovieFile = CyArtFileMgr().getMovieArtInfo(gc.getProjectInfo(self.iEntry).getMovieArtDef()).getPath()
		elif self.iType == 6786:
			sDescription =gc.getVictoryInfo(iEntry).getDescription()
			self.szMovieFile = CyArtFileMgr().getMovieArtInfo(gc.getVictoryInfo(self.iEntry).getMovie()).getPath()
		if self.szMovieFile.find(".nif") > -1:
			screen.addReligionMovieWidgetGFC("Movie", self.szMovieFile, self.X_MOVIE, self.Y_MOVIE, self.W_MOVIE, self.H_MOVIE, WidgetTypes.WIDGET_GENERAL, -1, -1)
		else:
			screen.playMovie(self.szMovieFile, -1, -1, -1, -1, 0)
		screen.setLabel("MoviePlayerDescription", "Background", u"" + sDescription.upper() + "", CvUtil.FONT_CENTER_JUSTIFY, screen.getXResolution() /2, 8, -0.1, FontTypes.TITLE_FONT, WidgetTypes.WIDGET_GENERAL, -1, -1)
		screen.setText("MoviePlayerExit", "Background", u"" + CyTranslator().getText("TXT_KEY_PEDIA_SCREEN_EXIT", ()).upper() + "", CvUtil.FONT_RIGHT_JUSTIFY, screen.getXResolution() - 30, screen.getYResolution() - 42, -0.1, FontTypes.TITLE_FONT, WidgetTypes.WIDGET_PEDIA_MAIN, self.top.PLATYPEDIA_MOVIE, -1)
		screen.setButtonGFC("MoviePlayerExit", CyTranslator().getText("TXT_KEY_MAIN_MENU_OK", ()), "", screen.getXResolution()/2 - self.W_EXIT/2, screen.getYResolution() - self.H_EXIT - 8, self.W_EXIT , self.H_EXIT, WidgetTypes.WIDGET_CLOSE_SCREEN, -1, -1, ButtonStyles.BUTTON_STYLE_STANDARD )

	def handleInput (self, inputClass):
		if (inputClass.getNotifyCode() == NotifyCode.NOTIFY_CHARACTER and inputClass.getData() == int(InputTypes.KB_ESCAPE)) or inputClass.getFunctionName() == "MoviePlayerExit":
			CyUserProfile().setGraphicOption(GraphicOptionTypes.GRAPHICOPTION_NO_MOVIES, bNoMovies)
			screen = CyGInterfaceScreen( "MoviePlayer", CvScreenEnums.PLATYPEDIA_MOVIE)
			screen.hideScreen()
			self.top.showScreen(self.top.PLATYPEDIA_MOVIE)
		return 0

	def update(self, fDelta):
		return 1


import sys

# 保存先ファイルの中身を初期化する
def initFile(SAVE_PATH):
    print("保存場所：" + SAVE_PATH)
    with open("%s/TRAIN_DATA.txt" % SAVE_PATH, "w") as f1:
        f1.write("")
    with open("%s/WOLF_LABEL_DATA.txt" % SAVE_PATH, "w") as f2:
        f2.write("")
    with open("%s/SEER_LABEL_DATA.txt" % SAVE_PATH, "w") as f2:
        f2.write("")
    with open("%s/POSS_LABEL_DATA.txt" % SAVE_PATH, "w") as f2:
        f2.write("")
    with open("%s/PARAM.txt" % SAVE_PATH, "w") as f2:
        f2.write("")

# INFO内の情報をプレイヤーの数や特徴量の数に合わせて初期化する
def initInfo(PLAY_NUM, FEATUER_NUM):
    INFO = {}
    for p in range(1, PLAY_NUM+1):
        INFO[str(p)] = []
        for j in range(FEATUER_NUM):
            INFO[str(p)].append(0)
    return INFO

# 使用するターン数に合わせてLSTM_INFOを初期化する
def initLstmInfo(DAY_NUM, START_TURN, END_TURN, PLAY_NUM, FEATUER_NUM):
    LSTM_INFO = {}
    for day in range(1, DAY_NUM+1):
        LSTM_INFO[str(day)] = {}
        for turn in range(START_TURN, END_TURN):
            LSTM_INFO[str(day)][str(turn)] = {}
            for p in range(1, PLAY_NUM+1):
                LSTM_INFO[str(day)][str(turn)][str(p)] = []
                for j in range(FEATUER_NUM):
                    LSTM_INFO[str(day)][str(turn)][str(
                        p)].append(-1)  # 超過部分用といて-1で初期化する
    # print(LSTM_INFO["1"].keys())
    # print(LSTM_INFO["2"].keys())
    # sys.exit()
    return LSTM_INFO

# TALK_HISTORYの初期化
def initTalkHistroy(DAY_NUM):
    TALK_HISTORY = {}
    for day in range(1, DAY_NUM + 1):
        TALK_HISTORY[str(day)] = {}
    # print(TALK_HISTORY)
    # sys.exit()
    return TALK_HISTORY

# AGREE_PLAYERの初期化
def initAgreePlayer(PLAY_NUM):
    AGREE_PLAYER = {}
    for p in range(1, PLAY_NUM + 1):
        AGREE_PLAYER[str(p)] = []
    # print(AGREE_PLAYER)
    # sys.exit()
    return AGREE_PLAYER

# DISAGREE_PLATERの初期化
def initDisagreePlayer(PLAY_NUM):
    DISAGREE_PLAYER = {}
    for p in range(1, PLAY_NUM + 1):
        DISAGREE_PLAYER[str(p)] = []
    # print(DISAGREE_PLAYER)
    # sys.exit()
    return DISAGREE_PLAYER

# VOTE_PLAYERの初期化
def initVotePlayer(PLAY_NUM):
    VOTE_PLAYER = {}
    for p in range(1, PLAY_NUM + 1):
        VOTE_PLAYER[str(p)] = {"TARGET": None, "RUN": False}
    # print(VOTE_PLAYER)
    # sys.exit()
    return VOTE_PLAYER

# ESTIMATE_PLAYERの初期化
def initEstimatePlayer(ESTIMATE_ROLE, PLAY_NUM):
    ESTIMATE_PLAYER = {}
    for p in range(1, PLAY_NUM + 1):
        ESTIMATE_PLAYER[str(p)] = {}
        for role, val in ESTIMATE_ROLE.items():
            if val[0]:
                ESTIMATE_PLAYER[str(p)][role] = []
    # print(ESTIMATE_PLAYER)
    # sys.exit()
    return ESTIMATE_PLAYER

# DIVINED_PLAYERの初期化
def initDivinedPlayer(DIVINED_ROLE, PLAY_NUM):
    DIVINED_PLAYER = {}
    for p in range(1, PLAY_NUM + 1):
        DIVINED_PLAYER[str(p)] = {}
        for role, val in DIVINED_ROLE.items():
            if val[0]:
                DIVINED_PLAYER[str(p)][role] = []
    # print(DIVINED_PLAYER)
    # sys.exit()
    return DIVINED_PLAYER

# TRAIN_LISTを初期化する
def initTrainList(START_TURN, END_TURN):
    """
    [[0ターン目],
     [1ターン目],
     [0ターン目(2日目)]
     [1ターン目(2日目)]]
    """
    TRAIN_LIST = {}
    for turn in range(START_TURN, END_TURN):
        TRAIN_LIST[str(turn)] = []
    # print(TRAIN_LIST)
    # sys.exit()
    return TRAIN_LIST

# predictListの初期化

def initPredictList(DAY_NUM, START_TURN, END_TURN):
    roles = ["WOLF", "SEER", "POSS"]
    predictList = {}
    for day in range(1, DAY_NUM+1):
        predictList[str(day)] = {}
        for turn in range(START_TURN, END_TURN):
            predictList[str(day)][str(turn)] = {}
            for role in roles:
                predictList[str(day)][str(turn)][role] = 0
        # print(predictList)
        # sys.exit()
        predictList[str(day)]["vote"] = {}
        predictList[str(day)]["divine"] = {}
        predictList[str(day)]["attack"] = {}
        for role in roles:
            predictList[str(day)]["vote"][role] = 0
            predictList[str(day)]["divine"][role] = 0
            predictList[str(day)]["attack"][role] = 0
    return predictList


#!/usr/bin/env python

from reviewboard.extensions.packaging import setup

PACKAGE = 'rbseverity'
VERSION = '0.1'

setup(
    name=PACKAGE,
    version=VERSION,
    description='Comment severity support for Review Board',
    url='http://www.beanbaginc.com/',
    author='Beanbag, Inc.',
    author_email='support@beanbaginc.com',
    maintainer='Beanbag, Inc.',
    maintainer_email='support@beanbaginc.com',
    packages=['rbseverity'],
    install_requires=[
        'ReviewBoard>=1.8alpha0.dev',
    ],
    entry_points={
        'reviewboard.extensions': [
            'rbseverity = rbseverity.extension:SeverityExtension',
        ],
    },
    package_data={
        'rbseverity': [
            'templates/rbseverity/*.html',
        ]
    },
)


import argparse
import os
import re
import time
import grpc
import tritonclient.grpc as grpcclient
import torch
import pandas as pd
from trustnet_utils import VideoReader, FaceExtractor, confident_strategy, predict_on_video_set_with_trustnet_api
from training.zoo.myclassifiers import DeepFakeClassifier

done_list = []

if __name__=="__main__":
    parser = argparse.ArgumentParser("Predict test videos")
    arg = parser.add_argument
    arg('--model-name', type=str, required=True, help="TrustNet API Model Name")
    arg('--test-dir', type=str, required=True, help="path to directory with videos")
    arg('--output', type=str, required=False, help="path to output csv", default="submission.csv")
    arg('--size', type=int, required=False, help="input size", default=380)
    arg('--range1', type=int, required=False, help="list(range($range1, $range2))", default=36)
    arg('--range2', type=int, required=False, help="list(range($range1, $range2))", default=50)
    args = parser.parse_args()
    test_list = list(range(args.range1, args.range2))
    try:
        trustnet_client = grpcclient.InferenceServerClient(
            url="localhost:8001",
            verbose=False,
            ssl=False)
    except Exception as e:
        print("channel creation failed: " + str(e))

    for number in test_list:
        if number in done_list: continue
        test_dir = f"{args.test_dir}dfdc_train_part_{number}" # Input path of test datas
        output = f"result/{args.model_name}_{number}.csv"
        print(f"Start inference {test_dir}")

        frames_per_video = 32
        video_reader = VideoReader()
        video_read_fn = lambda x: video_reader.read_frames(x, num_frames=frames_per_video)
        face_extractor = FaceExtractor(video_read_fn)
        input_size = args.size
        strategy = confident_strategy
        stime = time.time()

        test_videos = sorted([x for x in os.listdir(test_dir) if x[-4:] == ".mp4"])
        print("Predicting {} videos".format(len(test_videos)))
        predictions = predict_on_video_set_with_trustnet_api(face_extractor=face_extractor, input_size=input_size, trustnet_client=trustnet_client, model_name=args.model_name,
                                           strategy=strategy, frames_per_video=frames_per_video, videos=test_videos,
                                           num_workers=16, test_dir=test_dir)
        print(len(test_videos))
        print(len(predictions))
        submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
        submission_df.to_csv(output, index=False)
        print("Elapsed:", time.time() - stime)


#
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# (c) Copyright 2021 Xilinx Inc.

"""
aiecc - AIE compiler driver for MLIR tools
"""

import itertools
import os
import platform
import sys
import time
from subprocess import PIPE, run, call
# from joblib import Parallel, delayed
import tempfile
import shutil

import aiecc.cl_arguments

def do_call(command):
    global opts
    if(opts.verbose):
        print(" ".join(command))
    ret = call(command)
    if(ret != 0):
        print("Error encountered while running: " + " ".join(command))
        sys.exit(1)

def do_run(command):
    global opts
    if(opts.verbose):
        print(" ".join(command))
    ret = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True)
    return ret

def run_flow(opts, tmpdirname):
    thispath = os.path.dirname(os.path.realpath(__file__))
    me_basic_o = os.path.join(thispath, '..','..','runtime_lib', 'me_basic.o')
    libc = os.path.join(thispath, '..','..','runtime_lib', 'libc.a')
    libm = os.path.join(thispath, '..','..','runtime_lib', 'libm.a')
    chess_intrinsic_wrapper_cpp = os.path.join(thispath, '..','..','runtime_lib', 'chess_intrinsic_wrapper.cpp')

    file_with_addresses = os.path.join(tmpdirname, 'input_with_addresses.mlir')
    do_call(['aie-opt', '--lower-affine', '--aie-assign-buffer-addresses', '-convert-scf-to-std', opts.filename, '-o', file_with_addresses])
    t = do_run(['aie-translate', '--aie-generate-corelist', file_with_addresses])
    cores = eval(t.stdout)

    if(opts.xchesscc == True):
      chess_intrinsic_wrapper = os.path.join(tmpdirname, 'chess_intrinsic_wrapper.ll')
      do_call(['xchesscc_wrapper', '-c', '-d', '-f', '+f', '+P', '4', chess_intrinsic_wrapper_cpp, '-o', chess_intrinsic_wrapper])
      do_call(['sed', '-i', 's/^target.*//', chess_intrinsic_wrapper])

      do_call(['sed', '-i', 's/noalias_sidechannel[^,]*,//', chess_intrinsic_wrapper])

    def corefile(dirname, core, ext):
        (corecol, corerow) = core
        return os.path.join(dirname, 'core_%d_%d.%s' % (corecol, corerow, ext))

    def tmpcorefile(core, ext):
        return corefile(tmpdirname, core, ext)

    # Extract included files from the given Chess linker script.
    # We rely on gnu linker scripts to stuff object files into a compile.  However, the Chess compiler doesn't
    # do this, so we have to explicitly specify included files on the link line.
    def extract_input_files(file_core_bcf):
        t = do_run(['awk', '/_include _file/ {print($3)}', file_core_bcf])
        return ' '.join(t.stdout.split())

    def process_core(core):
        (corecol, corerow) = core
        file_core = tmpcorefile(core, "mlir")
        do_call(['aie-opt', '--aie-standard-lowering=tilecol=%d tilerow=%d' % core, file_with_addresses, '-o', file_core])
        file_opt_core = tmpcorefile(core, "opt.mlir")
        do_call(['aie-opt', '--aie-normalize-address-spaces',
                            '--canonicalize',
                            '--cse',
                            '--aie-vector-opt',
                            '--convert-vector-to-llvm',
                            '--convert-std-to-llvm=use-bare-ptr-memref-call-conv', file_core, '-o', file_opt_core])
        #do_call(['aie-opt', '--aie-llvm-lowering=tilecol=%d tilerow=%d' % core, file_with_addresses, '-o', file_core])
        file_core_bcf = tmpcorefile(core, "bcf")
        do_call(['aie-translate', file_with_addresses, '--aie-generate-bcf', '--tilecol=%d' % corecol, '--tilerow=%d' % corerow, '-o', file_core_bcf])
        file_core_ldscript = tmpcorefile(core, "ld.script")
        do_call(['aie-translate', file_with_addresses, '--aie-generate-ldscript', '--tilecol=%d' % corecol, '--tilerow=%d' % corerow, '-o', file_core_ldscript])
        file_core_llvmir = tmpcorefile(core, "ll")
        do_call(['aie-translate', '--mlir-to-llvmir', file_opt_core, '-o', file_core_llvmir])
        file_core_llvmir_stripped = tmpcorefile(core, "stripped.ll")
        do_call(['opt', '-O2', '-strip', '-S', file_core_llvmir, '-o', file_core_llvmir_stripped])
        file_core_elf = corefile(".", core, "elf")
        file_core_obj = tmpcorefile(core, "o")
        if(opts.xchesscc):
          file_core_llvmir_chesshack = tmpcorefile(core, "chesshack.ll")
          do_call(['cp', file_core_llvmir_stripped, file_core_llvmir_chesshack])
          do_call(['sed', '-i', 's/noundef//', file_core_llvmir_chesshack])
          do_call(['sed', '-i', 's/noalias_sidechannel[^,],//', file_core_llvmir_chesshack])
          file_core_llvmir_chesslinked = tmpcorefile(core, "chesslinked.ll")
          do_call(['llvm-link', file_core_llvmir_chesshack, chess_intrinsic_wrapper, '-S', '-o', file_core_llvmir_chesslinked])
          do_call(['sed', '-i', 's/noundef//', file_core_llvmir_chesslinked])
          # Formal function argument names not used in older LLVM
          do_call(['sed', '-i', '-E', '/define .*@/ s/%[0-9]*//g', file_core_llvmir_chesslinked])
          do_call(['sed', '-i', '-E', 's/mustprogress//g', file_core_llvmir_chesslinked])
          if(opts.xbridge):
            link_with_obj = extract_input_files(file_core_bcf)
            do_call(['xchesscc_wrapper', '-d', '-f', '+P', '4', file_core_llvmir_chesslinked, link_with_obj, '+l', file_core_bcf, '-o', file_core_elf])
          else:
            do_call(['xchesscc_wrapper', '-c', '-d', '-f', '+P', '4', file_core_llvmir_chesslinked, '-o', file_core_obj])
            do_call(['clang', '-O2', '--target=aie', file_core_obj, me_basic_o, '-Wl,-T,'+file_core_ldscript, '-o', file_core_elf])
        else:
          do_call(['llc', file_core_llvmir_stripped, '-O2', '--march=aie', '--filetype=obj', '-o', file_core_obj])
          if(opts.xbridge):
            link_with_obj = extract_input_files(file_core_bcf)
            do_call(['xchesscc_wrapper', '-d', '-f', file_core_obj, link_with_obj, '+l', file_core_bcf, '-o', file_core_elf])
          else:
            do_call(['clang', '-O2', '--target=aie', file_core_obj, me_basic_o, libm,
            '-Wl,-T,'+file_core_ldscript, '-o', file_core_elf])

    # Compile each core in parallel
    # Parallel(n_jobs=8, require='sharedmem')(delayed(process_core)(core) for core in cores)
    for core in cores:
      process_core(core)

    # Generate the included host interface
    file_physical = os.path.join(tmpdirname, 'input_physical.mlir')
    do_call(['aie-opt', '--aie-create-flows', file_with_addresses, '-o', file_physical]);
    file_inc_cpp = os.path.join(tmpdirname, 'aie_inc.cpp')
    do_call(['aie-translate', '--aie-generate-xaie', file_physical, '-o', file_inc_cpp])

    # Lastly, compile the generated host interface with any ARM code.
    cmd = ['clang','--target=aarch64-linux-gnu', '-std=c++11']
    if(opts.sysroot):
      cmd += ['--sysroot=%s' % opts.sysroot]
    cmd += ['-I%s/opt/xaiengine/include' % opts.sysroot]
    cmd += ['-L%s/opt/xaiengine/lib' % opts.sysroot]
    cmd += ['-I%s' % tmpdirname]
    cmd += ['-fuse-ld=lld','-rdynamic','-lxaiengine','-lmetal','-lopen_amp','-ldl']

    if(len(opts.arm_args) > 0):
      do_call(cmd + opts.arm_args)

def main(builtin_params={}):
    thispath = os.path.dirname(os.path.realpath(__file__))

    # Assume that aie-opt, etc. binaries are relative to this script.
    aie_path = os.path.join(thispath, '..')

    if('VITIS' not in os.environ):
      # Try to find vitis in the path
      vpp_path = shutil.which("v++")
      if(vpp_path):
        vitis_bin_path = os.path.dirname(os.path.realpath(vpp_path))
        vitis_path = os.path.dirname(vitis_bin_path)
        os.environ['VITIS'] = vitis_path
        print("Found Vitis at " + vitis_path)
        os.environ['PATH'] = os.pathsep.join([vitis_bin_path, os.environ['PATH']])

    if('VITIS' in os.environ):
      vitis_path = os.environ['VITIS']
      # Find the aietools directory, needed by xchesscc_wrapper

      aietools_path = os.path.join(vitis_path, "aietools")
      if(not os.path.exists(aietools_path)):
        aietools_path = os.path.join(vitis_path, "cardano")
      os.environ['AIETOOLS'] = aietools_path

      aietools_bin_path = os.path.join(aietools_path, "bin")
      os.environ['PATH'] = os.pathsep.join([aietools_bin_path, os.environ['PATH']])

    os.environ['PATH'] = os.pathsep.join([aie_path, os.environ['PATH']])

    global opts
    opts = aiecc.cl_arguments.parse_args()
    is_windows = platform.system() == 'Windows'

    if(opts.verbose):
        sys.stderr.write('\ncompiling %s\n' % opts.filename)

    if(opts.tmpdir):
      tmpdirname = opts.tmpdir
      try:
        os.mkdir(tmpdirname)
      except FileExistsError:
        pass
      if(opts.verbose):
        print('created temporary directory', tmpdirname)

      run_flow(opts, tmpdirname)
    else:
      with tempfile.TemporaryDirectory() as tmpdirname:
        run_flow(opts, tmpdirname)


#!/usr/bin/python3

from collections import Counter, defaultdict
import json
import math
from pprint import pprint
import random
import string
translator = str.maketrans({key: None for key in string.punctuation})

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
from nltk.corpus import wordnet as wn

from sklearn.cluster import AffinityPropagation, SpectralClustering
from sklearn.metrics.pairwise import euclidean_distances

import parser

TRAIN_FILE = "data/tagged_clues.train"
TEST_FILE = "data/tagged_clues.test"

def create_tagged_graph():
    with open(TRAIN_FILE, "r") as f:
        clues = [json.loads(l.strip()) for l in f]

        # Wordnet likes multiple word synsets to be '_'-joined.
        anagrinds = ["_".join([wn.morphy(word)
                     if wn.morphy(word) is not None else word
                     for word in c['anagrind']]) for c in clues]
        c = Counter(anagrinds)

        tagged_graph = nx.Graph()
        done = []
        cs = list(c.items())
        cs.sort(key=lambda x: -x[1])
        for word, freq in cs:
            word = word if wn.morphy(word) is None else wn.morphy(word)
            print("{}\tProcessing ({},{})".format(len(done), word, freq))
            for (old_word, _) in done:
                best, best_path = parser.compare_words(word, old_word)
                #import pdb; pdb.set_trace()
                peak_reached = False
                for i in range(len(best_path)-1):
                    u = best_path[i]
                    v = best_path[i+1]

                    # Either side of path to root weighted differently.
                    if u.min_depth() == 1:
                        peak_reached = True
                    if peak_reached:
                        deep = u.min_depth()
                    else:
                        deep = v.min_depth()

                    if tagged_graph.has_edge(u.name(),v.name()):
                        tagged_graph[u.name()][v.name()]['freq'] += freq
                    else:
                        tagged_graph.add_edge(u.name(), v.name())
                        tagged_graph[u.name()][v.name()]['deep'] = deep
                        tagged_graph[u.name()][v.name()]['freq'] = freq
            done.append((word, freq))

    return tagged_graph

def weight_graph(tagged_graph, w_depth, w_freq):
    for u,v in tagged_graph.edges():
        tagged_graph[u][v]['weight'] = \
            1/(tagged_graph[u][v]['freq']**w_freq * \
            math.exp(w_depth * tagged_graph[u][v]['deep']))

def process_graph_component(graph):
    #print("Processing component of size {}".format(graph.size()))
    X = nx.to_numpy_matrix(graph)
    n_clusters = min(10, int(graph.size()/20))
    sp = SpectralClustering(
            n_clusters=n_clusters, n_components=min(1000,graph.size()), affinity='precomputed').fit(X)
    return sp, X

def get_graph_components(tagged_graph):
    #print([g.size() for g in nx.connected_component_subgraphs(tagged_graph)])
    tagged_graphs = list(nx.connected_component_subgraphs(tagged_graph))
    graphs = [graph for graph in tagged_graphs if graph.size() > 100]

    return graphs

def find_centres(sp, graph):
    centres = []
    #import pdb; pdb.set_trace()
    nodes = graph.nodes()
    for i in range(sp.means.shape[0]):
        vec = sp.means[i].reshape(1, -1)
        distances = list(euclidean_distances(vec, sp.maps).transpose()) #@@@
        nearest = distances.index(min(distances))
        centres.append(wn.synset(nodes[nearest]))
    return centres

def predict_once(clue_words, centres):
    best = 1000
    prediction = None
    closest_centre = None
    for word in clue_words:
        red_word = word.lower().translate(translator)
        morphy_word = word if wn.morphy(red_word) is None else wn.morphy(red_word)
        ratings = [parser.compare_synset_word(centre, morphy_word)[0]
                   for centre in centres]
        curr = min(ratings)
        curr_centre = centres[ratings.index(curr)]
        if curr < best:
            best = curr
            prediction = red_word
            closest_centre = curr_centre

    return prediction, best, closest_centre

def predict(centres):
    with open(TEST_FILE, "r") as f:
        lines = [json.loads(line.strip()) for line in f]
        entries = [(line['clue'], line['anagrind']) for line in lines]
        correct_predictions = 0
        total_predictions = 0
        for clue, anagrind in entries:
            words = clue.split()
            pred, _, pred_centre = predict_once(words, centres)
            #print("{}, {}, ({})".format(pred, anagrind, pred_centre))

            if pred in anagrind:
                correct_predictions += 1
            total_predictions += 1
        acc = correct_predictions/total_predictions
        #print("{} of {} correct: {}".format(
        #    correct_predictions, total_predictions, acc))
    return acc

def tag_train(sp, comp, X, centres):
    labels = []
    for node in comp.nodes(data=True):
        #import pdb; pdb.set_trace()
        word = [wn.synset(node[0])._lemma_names[0]]
        _, _, label = predict_once(word, centres)
        if label is not None:
            labels.append(centres.index(label))
        else:
            labels.append(0)
    sp.new_labels = labels

def train(tagged_graph, w_depth, w_freq):
    weight_graph(tagged_graph, w_depth, w_freq)
    comps = get_graph_components(tagged_graph)
    all_centres = []
    #import pdb; pdb.set_trace()
    for comp in comps:
        sp, X = process_graph_component(comp)
        centres = find_centres(sp, comp)
        tag_train(sp, comp, X, centres)
        draw_graph(sp, comp, X, w_freq, w_depth)
        print(centres)
        all_centres += centres
    return all_centres

def draw_graph(sp, comp, X, w_freq, w_depth):
    #import pdb; pdb.set_trace()
    colors = ['b','g','r','c','m','y','k','w','teal','pink','gray']
    color_map = {i:colors[i] for i in range(len(colors))}
    plt.figure()
    #plt.title("{}, {}".format(w_freq, w_depth))
    #nx.draw(comp, node_color=[color_map[label] for label in sp.labels_])
    nx.draw(comp, node_color=sp.new_labels)
    plt.draw()

PICKLE_FILE = 'data/tagged.p'
def get_graph():
    import os
    import pickle
    if os.path.isfile(PICKLE_FILE):
        graph = pickle.load(open(PICKLE_FILE, 'rb'))
    else:
        graph = create_tagged_graph()
        pickle.dump(graph, open(PICKLE_FILE, 'wb'))
    return graph

if __name__ == "__main__":
    results = []
    tagged_graph = get_graph()
    for weight_freq in (1,):#2,3):
        for weight_depth in (1,):#0.1,0.3,1,3,10,30):
            centres = train(tagged_graph, weight_depth, weight_freq)
            accuracy = predict(centres)
            print(accuracy, weight_depth, weight_freq)
            results.append((accuracy, weight_depth, weight_freq, centres))
    results.sort(key=lambda x: x[0])
    plt.show()
    pprint(results)


from django.contrib import admin
from django.urls import path, include
from catalog import views

urlpatterns = [
    path('', views.catalog, name='catalog'),
    path('category/<slug>', views.category, name='category'),
    path('products', views.products, name='products'),
    path('products/<slug>', views.product, name='product'),
    path('add_product', views.add_product, name='add_product'),
    path('cart', views.cart, name='cart'),
    path('make_order', views.make_order, name='make_order'),
    path('api/get_attributes', views.GetAttributesView.as_view()),
    path('api/get_sel_attributes', views.GetSelectableAttributesView.as_view()),
]


# type: ignore
"""company object

Revision ID: 1227c50194d8
Revises: b98b16007267
Create Date: 2020-08-09 16:50:56.371548

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "1227c50194d8"
down_revision = "b98b16007267"
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "companies",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column(
            "number_of_employees_estimate",
            sa.String(length=255),
            server_default="",
            nullable=True,
        ),
        sa.Column("name", sa.String(length=255), server_default="", nullable=False),
        sa.Column("address", sa.String(length=255), server_default="", nullable=False),
        sa.Column("is_active", sa.Boolean(), nullable=False),
        sa.Column(
            "status", sa.String(length=255), server_default="active", nullable=False
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_companies")),
    )
    op.add_column("users", sa.Column("company_id", sa.Integer(), nullable=True))
    op.create_foreign_key(
        op.f("fk_users_company_id_companies"),
        "users",
        "companies",
        ["company_id"],
        ["id"],
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(
        op.f("fk_users_company_id_companies"), "users", type_="foreignkey"
    )
    op.drop_column("users", "company_id")
    op.drop_table("companies")
    # ### end Alembic commands ###


from bs4 import BeautifulSoup
import urllib2

URL = "http://wso2.com/api-management/"

response = urllib2.urlopen(URL)
soup = BeautifulSoup(response, from_encoding=response.info().getparam('charset'))

links = []

for link in soup.find_all('a', href=True):
    links.append(link['href'])

for i in links:
    print(i)


import torch
from torch.utils import data
from torchvision.datasets import ImageFolder
from torchvision.transforms import ToTensor

colors = ['blue', 'cyan', 'gray', 'green', 'magenta']
object_types = ['box', 'sphere', 'cylinder', 'torus', 'ellipsoid']

class ColoredFiguresDataset(ImageFolder):

    def __getitem__(self, index):
        filename, _ = self.imgs[index]
        color, figure = filename.split('/')[-2].split('-')
        color_idx, figure_idx = colors.index(color), object_types.index(figure)
        label = torch.LongTensor([color_idx, figure_idx])
        return super(ColoredFiguresDataset, self).__getitem__(index)[0], label

def prepare_datasets():
    train_path = 'train'
    test_path = 'test'

    train_dataset = ColoredFiguresDataset(root=train_path, transform=ToTensor())
    test_dataset = ColoredFiguresDataset(root=test_path, transform=ToTensor())
    full_dataset = data.ConcatDataset([train_dataset, test_dataset])
    return full_dataset, train_dataset, test_dataset


#!/usr/bin/env python3

""" Solution """
def uni_char1(s):
	return len(set(s)) == len(s)

def uni_char2(s):
	chars = set()
	for let in s:
		# Check if in set
		if let in chars:
			return False
		else:
			#Add it to the set
			chars.add(let)
	return True

""" Own """
def uni_char(s):
	characters = []
	for letter in s:
		if letter not in characters:
			characters += letter
		else:
			return False

	return True

""" SOLUTION TESTING """
import unittest
class TestUnique(unittest.TestCase):

	def test(self, sol):
		self.assertEqual(sol(''), True)
		self.assertEqual(sol('goo'), False)
		self.assertEqual(sol('abcdefg'), True)
		print('ALL TEST CASES PASSED')

t = TestUnique()
print('## Testing Own Solution')
t.test(uni_char)
print('## Testing Solution 1')
t.test(uni_char1)
print('## Testing Solution 2')
t.test(uni_char2)

Array Sequences/Array Sequences Interview Questions/Array Sequence Interview Questions/Unique-Characters-in-String.py


## ===========
## Run on digest data
## ===========
import FWCore.ParameterSet.Config as cms
process = cms.Process( "TEST" )

## ===========
## MessageLogger and output report
## ===========
process.load("FWCore.MessageLogger.MessageLogger_cfi")
process.MessageLogger.cerr.FwkSummary = cms.untracked.PSet(
    reportEvery = cms.untracked.int32(100),
    limit = cms.untracked.int32(10000000)
)
process.MessageLogger.cerr.FwkReport = cms.untracked.PSet(
    reportEvery = cms.untracked.int32(100),
    limit = cms.untracked.int32(10000000)
)
process.options = cms.untracked.PSet(
    wantSummary = cms.untracked.bool( False )
)

## ===========
## Source
## ===========
process.source = cms.Source(
    "PoolSource",
    fileNames = cms.untracked.vstring('file:./MyPatTrigger_Task1_SingleMU.root') #Created from Task1
     #fileNames = cms.untracked.vstring('/store/users/srimanob/PAT-Dec11/Trigger/patData_SingleMu.root')
)
process.maxEvents = cms.untracked.PSet(
    input = cms.untracked.int32( -1 )
)

## ===========
## outputs
## ===========
process.TFileService = cms.Service("TFileService",
      fileName = cms.string("MyPatTrigger_Task3.root"),
      closeFileFast = cms.untracked.bool(True)
)

## ===========
## MyPatTriggerClass
## ===========
process.MyPatTrigger = cms.EDAnalyzer(
    'MyPatTriggerTask3',
    HLTNames       = cms.vstring("HLT_Mu40_v9"),
    triggerEvent   = cms.InputTag( "patTriggerEvent" ),
    metSrc         = cms.InputTag( "patMETs" )
)

## ===========
## Let it run
## ===========
process.p = cms.Path(
    process.MyPatTrigger
)


""" Sanity checks FASM output from IOB fuzzer.

The IOB fuzzer is fairly complicated, and it's output is hard to verify by
inspected.  For this reason, check_results.py was written to compare the
specimen's generated and their FASM output.  The FASM output does pose a
chicken and egg issue.  The test procedure is a follows:

1. Build the database (e.g. make -j<N> run)
2. Build the database again (e.g. make -j<N> run)
3. Run check_results.py

The second time that the database is run, the FASM files in the specimen's
will have the bits documented by fuzzer.

"""
import os
import os.path
from prjxray import verilog
import json
import generate

def process_parts(parts):
    if parts[0] == 'INOUT':
        yield 'type', 'IOBUF_INTERMDISABLE'

    if parts[0] == 'IN_ONLY':
        yield 'type', 'IBUF'

    if parts[0] == 'SLEW':
        yield 'SLEW', verilog.quote(parts[1])

    if parts[0] == 'PULLTYPE':
        yield 'PULLTYPE', verilog.quote(parts[1])

    if len(parts) > 1 and parts[1] == 'IN':
        yield 'IOSTANDARDS', parts[0].split('_')

    if len(parts) > 1 and parts[1] == 'DRIVE':
        yield 'IOSTANDARDS', parts[0].split('_')
        yield 'DRIVES', parts[2].split('_')

def create_sites_from_fasm(root):
    sites = {}

    with open(os.path.join(root, 'design.fasm')) as f:
        for l in f:
            if 'IOB33' not in l:
                continue

            parts = l.strip().split('.')
            tile = parts[0]
            site = parts[1]
            if (tile, site) not in sites:
                sites[(tile, site)] = {
                    'tile': tile,
                    'site_key': site,
                }

            for key, value in process_parts(parts[2:]):
                sites[(tile, site)][key] = value

    for key in sites:
        if 'type' not in sites[key]:
            if 'IOSTANDARDS' not in sites[key]:
                sites[key]['type'] = None
            else:
                assert 'IOSTANDARDS' in sites[key], sites[key]
                assert 'DRIVES' in sites[key]
                sites[key]['type'] = "OBUF"

    return sites

def process_specimen(root):
    sites = create_sites_from_fasm(root)

    with open(os.path.join(root, 'params.jl')) as f:
        params = json.load(f)

    for p in params:
        tile = p['tile']
        site = p['site']
        site_y = int(site[site.find('Y') + 1:]) % 2

        if generate.skip_broken_tiles(p):
            continue

        site_key = 'IOB_Y{}'.format(site_y)

        if (tile, site_key) not in sites:
            assert p['type'] is None, p
            continue

        site_from_fasm = sites[(tile, site_key)]

        assert p['type'] == site_from_fasm['type'], (
            tile, site_key, p, site_from_fasm)

        if p['type'] is None:
            continue

        assert p['PULLTYPE'] == site_from_fasm['PULLTYPE'], (
            tile, site_key, p, site_from_fasm)

        assert verilog.unquote(
            p['IOSTANDARD']) in site_from_fasm['IOSTANDARDS'], (
                tile, site_key, p, site_from_fasm)

        if p['type'] != 'IBUF':
            assert p['SLEW'] == site_from_fasm['SLEW'], (
                tile, site_key, p, site_from_fasm)

            assert 'I{}'.format(p['DRIVE']) in site_from_fasm['DRIVES'], (
                tile, site_key, p, site_from_fasm)

def main():
    for root, dirs, files in os.walk('build'):
        if os.path.basename(root).startswith('specimen_'):
            print('Processing', os.path.basename(root))
            process_specimen(root)

    print('No errors found!')

if __name__ == "__main__":
    main()


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

import logging

from scrapy.exceptions import CloseSpider
from scrapy.spidermiddlewares.httperror import HttpErrorMiddleware, HttpError

from fooltrader import settings

logger = logging.getLogger(__name__)

# spider middleware
class FoolErrorMiddleware(HttpErrorMiddleware):
    def process_spider_exception(self, response, exception, spider):
        if isinstance(exception, HttpError):
            if response.status == 456:
                # response.meta['fool_blocked'] = True
                # return None
                raise CloseSpider('catch forbidden,close for a while')

# downloader middleware
class ForbiddenHandleMiddleware(object):
    forbidden_codes = (456, 403)

    def process_response(self, request, response, spider):
        proxy_count = len(settings.g_http_proxy_items)
        count = 0

        if request.meta.get('dont_proxy', False):
            return response
        if response.status in self.forbidden_codes:
            # 新浪财经
            if 'sina.com' in request.url:
                proxy_json = settings.g_http_proxy_items[count % proxy_count]
                count += 1
                if proxy_json['type'] == 'HTTPS':
                    proxy = 'https://{}:{}'.format(proxy_json['ip'], proxy_json['port'])
                elif proxy_json['type'] == 'HTTP':
                    proxy = 'http://{}:{}'.format(proxy_json['ip'], proxy_json['port'])
                request.meta['proxy'] = proxy
                logger.info("using")
                return request
        return response


from django.urls import path
from . import views

urlpatterns = [
    # Login
    path('', views.LoginPageView.as_view(), name='login'), # Login url
    path('signup/', views.SignUpPageView.as_view(), name='signup'), # SignUp url
    path('forgotpassword/', views.ForgotPasswordPageView.as_view(), name='forgotpassword'), # Forgot Password url
    path('user/create/', views.create_user, name='create_user'), # user cration api
    path('user/remove/', views.remove_user, name='remove_user'), # user remove api
    path('loginStudent/', views.login_student, name='student_login'), # student login api
    path('loginAdmin/', views.login_admin, name='admin_login'), # admin login api
    path('forgotPassword/', views.forgotPassword, name='forgotPassword'), # forgot Password api

    # Admin
    path('admin/dashboard/', views.AdminDashboardPageView.as_view(), name='Admindashboard'), # Admin dashboard url
    path('admin/manageAdmin/', views.manageAdminPageView.as_view(), name='manageAdmin'), # manage Admin url
    path('admin/sports/', views.adminSportPageView.as_view(), name='adminSport'), # admin Sports url
    path('admin/sports/<sport_name>/', views.SportInfoPageView.as_view(), name='SportInfo'), # SportInfo url
    path('admin/teams/', views.AdminTeamsPageView.as_view(), name='Adminteams'), # Admin teams url
    path('admin/teams/teamApprove/', views.TeamApprovePageView.as_view(), name='teamApprove'), # Admin team approve url
    path('admin/generate_schedule/', views.Generate_SchedulePageView.as_view(), name='Genereate_Schedule'), # Generate Schedule url
    path('admin/getAllAdmins/', views.get_all_admins, name='getAllAdmins'), # Get All Admins api

    # Student
    path('student/dashboard/', views.StudentDashboardPageView.as_view(), name='Studentdashboard'), # Student dashboard url
    path('student/team/', views.StudentTeamPageView.as_view(), name='StudentTeam'), # Student Team url
    path('student/sports/', views.StudentSportPageView.as_view(), name='StudentSport'), # Student Sport url
    path('student/sports/<sport_name>/', views.SportInfoPageView.as_view(), name='SportInfo'), # SportInfo url
    path('student/team/createTeam/', views.StudentCreateTeamPageView.as_view(), name='StudentCreateTeam'), # Student Create Team url
    path('student/team/teamInfo/', views.StudentTeamInfoPageView.as_view(), name='StudentTeamInfo'), # Student Team Info url
    path('sport/create/', views.create_sport, name='create_sport'), # sport cration api
    path('sport/remove/', views.remove_sport, name='remove_sport'), # sport remove api
    path('sport/getAllSports/', views.get_all_sports, name='getAllSports'), # Get All Sports api
    path('team/create/', views.create_team, name='create_team'), #create team api
    path('team/getAllTeams/', views.get_all_teams, name='getAllTeams'), #get all teams api
    path('team/getNewTeams/', views.get_new_teams, name='getNewTeams'), #get new teams api
    path('team/getApprovedTeams/', views.get_approved_teams, name='getApprovedTeams'), #get all approved teams api
    path('team/ApproveTeam/', views.approve_team, name='approveTeam'), #approve team api
    path('team/DenyTeam/', views.deny_team, name='denyTeam'), #deny team api
    path('sport/getTeamsForSport/', views.get_teams_for_sport, name='getTeamsForSport'), #get teams for perticular sport api
    path('sport/getTeams/', views.get_teams, name='getTeams'), #get teams for perticular sport api with parameter
    path('sport/getSchedule/', views.get_schedule, name='getSchedule'), #get schodule for perticular sport api with parameter
    path('team/getUserTeams/', views.get_user_teams, name='getUserTeams'), #get user teams api
    path('student/team/<team_name>/', views.TeamInfoPageView.as_view(), name='TeamsInfo'), # SportInfo url
    # Score
	path('score/update/', views.update_score, name='updateScore'),
	path('score/createGame/', views.create_game, name='createGame'),
	path('score/getActiveGames/', views.get_active_games, name='getActiveGames'),
	path('score/endGame/', views.end_game, name='endGame'),

    # Schedule
    path('schedule/create/', views.create_schedule, name='create_schedule'), #create schedule api

    # Player
    path('player/create/', views.create_player, name='create_player'), #create team api
    path('player/getPlayersForTeam/', views.get_players_for_team, name='getPlayersForTeam'), #get player for team
]


from decimal import Decimal
from typing import List, Set, Tuple
import geopy.distance
from autocomplete.core import IResultHandler, ResultMatch, ScoreWeight

class RedundantPlacesIdFilter(IResultHandler):

    def handle(self, results: List[ResultMatch]) -> None:

        filteredResult: List[ResultMatch] = []
        hashSet: Set[int] = set()

        for r in sorted(results, key=lambda r: r.getScore(), reverse=True):  # O(n log(n))
            if r.owner.uid not in hashSet:  # O(1)
                hashSet.add(r.owner.uid)  # O(1)
                filteredResult.append(r)  # O(1)

        results.clear()
        results.extend(filteredResult)

# if the word is in chinese, or greek, we display exactly the word ("alternative name")
# that was a match in the same language.
# However, if it was a search using a "simple" alphabet, we return the
# "principal" name instead of the "alternative" name that was a match against the search
class PlaceDisplayNameMatchOverride(IResultHandler):

    def handle(self, results: List[ResultMatch]) -> None:

        def intersects(list1: list, list2: list) -> bool:
            return any(set(list1) & set(list2))

        override: bool = False

        for r in results:
            matchName: str = r.word.lower()
            asciiName: str = r.owner.nameAscii.lower()
            override = (matchName == asciiName or intersects(matchName.split(), asciiName.split()))

            if override:
                break

        if override:
            for r in results:
                r.word = r.owner.name

class PopulationSizeScoreWeightsGenerator(IResultHandler):

    def __init__(self, weight: float):
        self._weight = weight

    def handle(self, results: List[ResultMatch]) -> None:
        for r in results:
            populationScore: float = r.owner.population
            r.scores.append(ScoreWeight(populationScore, self._weight))

class CoordinatesScoreWeightsGenerator(IResultHandler):

    def __init__(self, latitude: Decimal, longitude: Decimal, weight: float):

        if latitude is None or longitude is None:
            raise ValueError("coordinates cannot be empty")

        self._referenceCoords: Tuple[Decimal, Decimal] = (latitude, longitude)
        self._weight = weight

    def handle(self, results: List[ResultMatch]) -> None:

        for r in results:
            # weights distance using Vincenty's distance, ellipsoid standard WGS-84. Time complexity O(1) amortized
            distanceScore: float = (-1) * geopy.distance.vincenty(self._referenceCoords, (r.owner.latitude, r.owner.longitude)).km
            r.scores.append(ScoreWeight(distanceScore, self._weight))


"""
Create a grounding line contour.
"""

from varglas.data.data_factory import DataFactory
from varglas.utilities import DataInput, MeshGenerator
from numpy import *

# Get the Antarctica data sets
bedmap2 = DataFactory.get_bedmap2()
db2 = DataInput(bedmap2)
# Get the grounding line by eliminating the shelves
db2.set_data_val('mask',1,127)

# Create a grounding line countour
mg = MeshGenerator(db2, 'mesh', '')
mg.create_contour('mask', 10, skip_pts=4)
mg.eliminate_intersections(dist=20)
cont = mg.longest_cont

# Convert (x,y) coordinates to (lon,lat)
cont_lons, cont_lats = db2.p(cont[:,0], cont[:,1], inverse = True)

# Save the contour
savetxt('grounding_cont.out', array(zip(cont_lons, cont_lats)))


def solution(n, lost, reserve):
    isHave = [1] * (n + 1)
    for lost_id in lost:
        if lost_id in reserve:
            reserve.remove(lost_id)
        else:
            isHave[lost_id] = 0

    for reserve_id in reserve:
        before = reserve_id - 1
        if before >= 1 and before <= n:
            if not isHave[before]:
                isHave[before] = 1
                continue

        after = reserve_id + 1
        if after >= 1 and after <= n:
            if not isHave[after]:
                isHave[after] = 1
                continue

    answer = sum(isHave) -1
    return answer


from xai.brain.wordbase.nouns._moulding import _MOULDING

#calss header
class _MOULDINGS(_MOULDING, ):
	def __init__(self,):
		_MOULDING.__init__(self)
		self.name = "MOULDINGS"
		self.specie = 'nouns'
		self.basic = "moulding"
		self.jsondata = {}


from pynput.mouse import Button, Controller
import fastgrab.screenshot as ss
import time
import cv2
import numpy as np

def click(x, y):
    mouse.position = (x,y)
    mouse.press(Button.left)
    mouse.release(Button.left)

def clickfirstblock(im):
    global gameCoordinates, previouslane, score
    for y_ in range(5, len(im)-5, 5):
        for i in range(4):
            if previouslane == i:
                continue
            w = gameCoordinates[2] - gameCoordinates[0]
            y = len(im) - y_
            x = i*w//4 + w//8
            if im[y,x] < 30:
                actual_x = x + gameCoordinates[0]
                actual_y = y + gameCoordinates[1]
                if score > 100:
                    actual_y += 20
                if score > 300:
                    actual_y += 20
                if score > 500:
                    actual_y += 20
                if score > 700:
                    actual_y += 20
                if score > 1000:
                    actual_y += 20
                if score > 1100:
                    actual_y += 20
                if score > 1300:
                    actual_y += 20
                click(actual_x, actual_y)
                score += 1
                previouslane = i
                return
time.sleep(5)
gameCoordinates = [680, 243, 1243, 1079]
starttime = time.time()
mouse = Controller()
mouse.position = (742, 729) # First
mouse.position = (885, 721) # Second
mouse.position = (1025, 729) # Third
mouse.position = (1170, 725) # Fourth
mouse.press(Button.left)
mouse.release(Button.left)
score = 0
# time.sleep(1)
num = 1
previouslane = -1
while True:
#     ss_time = time.time()
    im = ss.Screenshot().capture(bbox = [680, 243, 563, 836])
    im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    clickfirstblock(im)
#     print(f'Time taken by {num} frame is {time.time() - ss_time} seconds ')
#     num += 1
    pos = mouse.position
    if pos[0] > gameCoordinates[2]:
        break


import gym
import numpy as np
import time, pickle, os
import matplotlib.pyplot as plt

class Model():
    def __init__(self, n_states, n_actions):
        self.transitions = np.zeros((n_states, n_actions), dtype=np.uint8)
        self.rewards = np.zeros((n_states, n_actions))

    def add(self, state, action, state2, reward):
        self.transitions[state, action] = state2
        self.rewards[state, action] = reward

    def sample(self, env):
        state, action = 0, 0
        # Random visited state
        if all(np.sum(self.transitions, axis=1)) <= 0:
            state = np.random.randint(env.observation_space.n)
        else:
            state = np.random.choice(np.where(np.sum(self.transitions, axis=1) > 0)[0])

        # Random action in that state
        if all(self.transitions[state]) <= 0:
            action = np.random.randint(env.action_space.n)
        else:
            action = np.random.choice(np.where(self.transitions[state] > 0)[0])

        return state, action

    def step(self, state, action):
        state2 = self.transitions[state, action]
        # Reward + bonus
        reward = self.rewards[state, action] + kappa * np.sqrt(agent.timesteps[state])
        return state2, reward

class Agent:
    def __init__(self, env):
        self.env = env
        self.Q = np.zeros((env.observation_space.n, env.action_space.n))
        self.model = Model(env.observation_space.n, env.action_space.n)
        self.timesteps = np.zeros((env.observation_space.n, 1))

    def choose_action(self, state):
        if np.random.uniform(0, 1) < epsilon:
            return self.env.action_space.sample()
        else:
            return np.argmax(self.Q[state, :])

    def learn(self, state, state2, reward, action):
        # predict = Q[state, action]
        # Q[state, action] = Q[state, action] + lr_rate * (target - predict)
        target = reward + gamma * np.max(self.Q[state2, :])
        self.Q[state, action] = (1 - lr_rate) * self.Q[state, action] + lr_rate * target

    def planning(self, n_steps):
        # if len(self.transitions)>planning_steps:
        for i in range(n_steps):
            state, action =  self.model.sample(self.env)
            state2, reward = self.model.step(state, action)

            self.learn(state, state2, reward, action)

def train():
    total_rewards = []
    for episode in range(total_episodes):
        state = agent.env.reset()
        t = 0
        ep_rewards = 0

        while t < max_steps:
            # env.render()

            action = agent.choose_action(state)
            state2, reward, done, info = agent.env.step(action)

            agent.learn(state, state2, reward, action)

            agent.model.add(state, action, state2, reward)
            agent.timesteps[state] += 1
            agent.planning(planning_steps)

            state = state2

            ep_rewards+= reward
            t += 1
            if done:
                break

        total_rewards.append(ep_rewards)
        # epsilon = min_epsilon + (max_epsilon - min_epsilon) * np.exp(-decay_rate * episode)
        # time.sleep(0.1)

        # if done:
        #     break
    return total_rewards

def test():
    total_rewards = []
    for episode in range(total_episodes):
        ep_rewards = 0
        state = agent.env.reset()
        for t in range(max_steps):
            #agent.env.render()
            #time.sleep(0.5)
            act = np.argmax(agent.Q[state,:])
            state2, reward, done, info = agent.env.step(act)
            if done:
                ep_rewards += reward
                break
            else:
                state = state2
        total_rewards.append(ep_rewards)
    return total_rewards

def train_details(total_rewards):
    print("Q table:\n", agent.Q)
    print("Model Transitions:\n", agent.model.transitions)

    # Perfect actions: [1 2 1 0 1 0 1 0 2 1 1 0 0 2 2 0]
    print("Total Rewards in training: {0} in {1} episodes".format(sum(total_rewards), total_episodes))

    # Policy
    def act_names(x):
        dt = {0:'left', 1:'down', 2:'right', 3:'up'}
        return dt[x]
    act_np = np.vectorize(act_names)
    act = np.argmax(agent.Q, axis=1)
    act = act_np(act)
    act = act.reshape(4,4)
    print("Policy:\n", act)

def test_details(total_rewards):
    print("\n\nTotal Rewards in testing: {0} in {1} episodes".format(sum(total_rewards), total_episodes))

    # Policy
    def act_names(x):
        dt = {0:'left', 1:'down', 2:'right', 3:'up'}
        return dt[x]
    act_np = np.vectorize(act_names)
    act = np.argmax(agent.Q, axis=1)
    act = act_np(act)
    act = act.reshape(4,4)
    print("Policy:\n", act)

# Setup
env = gym.make('FrozenLake-v0')

epsilon = 0.9
lr_rate = 0.1
gamma = 0.95
planning_steps = 100
kappa = 1e-4 # bonus reward weight

total_episodes = 10000
max_steps = 100

agent = Agent(env)

print("Total States:", env.observation_space.n)
print("Total Actions:", env.action_space.n)

print("####### Training ########")
total_rewards = train()
train_details(total_rewards)

print("####### Testing ########")
total_test_rewards = test()
test_details(total_test_rewards)

# with open("frozenLake_qTable.pkl", 'wb') as f:
#     pickle.dump(Q, f)


import torch.nn as nn
import torch.utils.data
import torch.optim as optim
from torch.autograd import Variable
import matplotlib.pyplot as plt

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.nn = nn.Sequential(
            nn.Linear(1,50),
            nn.Tanh(),
            nn.Linear(50, 100),
            nn.Tanh(),
            nn.Linear(100, 20),
            nn.Tanh(),
            nn.Linear(20,1),
        )
    def forward(self, input):
        return self.nn(input)

if __name__ == '__main__':
    n = Net()
    print(n)
    input = torch.unsqueeze(torch.linspace(-10, 10, 100), dim=1)
    output = torch.pow(input,2) + 1
    ds = torch.utils.data.TensorDataset(input,output)
    loader = torch.utils.data.DataLoader(ds,batch_size=20, shuffle=True)
    optimizer = optim.SGD(n.parameters(), lr=0.001)
    loss = torch.nn.MSELoss()
    for i in range(100):
        for batch_idx, (data, target) in enumerate(loader):
            data, target = Variable(data), Variable(target)
            out = n(data)
            optimizer.zero_grad()
            l = loss(out,target)
            l.backward()
            optimizer.step()
        out = n(Variable(input,requires_grad = False))
        if i % 5 == 0:
            plt.cla()
            plt.scatter(input.numpy(), output.numpy())
            plt.plot(input.numpy(), out.data.numpy(),'r-')
            plt.text(-0.75, 2, 'Round=%d,Loss=%.4f' % (i,l.data[0]), fontdict={'size': 20, 'color':  'red'})
            plt.pause(0.3)
    plt.close()
    input2 = torch.unsqueeze(torch.linspace(-20, 20, 200), dim=1)
    out_real = torch.pow(input2,2) + 1
    out2 = n(Variable(input2)).data
    plt.scatter(input2.numpy(), out_real.numpy())
    plt.plot(input2.numpy(), out2.numpy(), 'r-')
    plt.text(-3, 16, 'Round=%d,Loss=%.4f' % (100, l.data[0]), fontdict={'size': 20, 'color': 'red'})
    plt.show()