# -*- coding: cp949 -*-
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
from wrapper import TkDND
from socket import *
import socket as so
import tkinter
import time
import threading

msgs = []
addrs = []
user_ip = ''
UDPqueue = []
TCPqueue = []
running = True
connected = True

# UI 관련 스레드
class UI(threading.Thread):
    root = ''
    new = ''
    index = 0
    def run(self):
        # UDP 브로드캐스팅
        def broadcast():
            global UDPqueue
            global msgs
            global addrs
            msgs.clear()
            addrs.clear()
            UDPqueue.append('broadcast')

        # 대답을 한 기기 중에서 선택
        def select(msg, ip):
            global user_ip
            global UDPqueue
            global connected
            print('select ' + msg)
            user_ip = ip
            TCPqueue.clear()
            UDPqueue.append(msg)
            lbl = Label(Frame3, text='IP: ' + user_ip, bg="#777777", borderwidth=2, relief="solid", width=23,
                         anchor="w")
            lbl.grid(row=1, column=1, padx=10, sticky=W, columnspan='3')
            lbl.config(font=('', 15,))
            connected = False
            self.new.destroy()

        # 선택한 기기와 TCP연결
        def connect():
            global msgs
            global addrs
            msgs.clear()
            addrs.clear()
            broadcast()
            time.sleep(1)
            self.new = Toplevel()
            self.new.title('ip list')
            length = len(msgs)
            for i in range(0, length):
                lbl8 = Label(self.new, text=msgs[i])
                lbl8.grid(row=i, column=0, padx=10, pady=10)
                lbl9 = Label(self.new, text=addrs[i])
                lbl9.grid(row=i, column=1, padx=10, pady=10)
                btn8 = Button(self.new, text='select', command=lambda x=i: select(msgs[x], addrs[x]))
                btn8.grid(row=i, column=2, padx=10, pady=10)

        # 선택한 데이터 전송
        def senddata():
            global TCPqueue
            global connected
            text = text1.get(0, END)
            if connected:
                messagebox.showinfo('warnning', 'Disconnected')
            elif text1.size() == 0:
                messagebox.showinfo('warnning', 'Select file')
            elif screentype.get() == 0:
                messagebox.showinfo('warnning', 'Select type of screen')
            elif dirtype.get() == 0:
                messagebox.showinfo('warnning', 'Select type of direction')
            else:
                for list in text:
                    file = open(list, 'rb')
                    line = list.split('/')
                    length = len(line)
                    data = file.read()
                    TCPqueue.append(('data ' + line[length-1] + ' ' + str(screentype.get()) + ' '
                                     + str(dirtype.get()) + ' ' + str(len(data))).encode())
                    TCPqueue.append(data)
                    TCPqueue.append('end'.encode())
                    delay = text2.get()
                    if delay == '':
                        delay = '0'
                    time.sleep(int(delay))
                    print('p')

        # PC에서 파일 선택
        def findfile():
            filename = filedialog.askopenfilename(initialdir="C:/", title="choose your file",
                                                  filetypes=(("jpeg files", "*.jpg"), ("mp4 files", "*.mp4")))
            text1.insert(str(self.index), filename)
            self.index += 1

        def deletefile():
            self.index -= 1
            for list in text1.curselection()[::-1]:
                print(list)
                text1.delete(list)
            self.index = text1.size()

        # 녹화시작 전송
        def startrecording():
            global TCPqueue
            global connected
            if connected:
                messagebox.showinfo('warnning', 'Disconnected')
            else:
                TCPqueue.append('start'.encode())

        # 녹화종료 전송
        def stoprecording():
            global TCPqueue
            global connected
            if connected:
                messagebox.showinfo('warnning', 'Disconnected')
            else:
                TCPqueue.append('stop'.encode())

        # 분석결과 전송
        def stopapp():
            global TCPqueue
            global connected
            if connected:
                messagebox.showinfo('warnning', 'Disconnected')
            else:
                TCPqueue.append('loading'.encode())
                time.sleep(5)
                TCPqueue.append('result'.encode())

        # 드래그 이벤트
        def drop(event):
            event.widget.insert(str(self.index), event.data[1:len(event.data) - 1])
            self.index += 1

        self.root = Tk()
        self.root["bg"] = "#777777"
        screentype = tkinter.IntVar()
        dirtype = tkinter.IntVar()
        self.root.title('I-EGO')
        dnd = TkDND(self.root)

        Frame1 = Frame(self.root, height=200, bg="#777777")
        title = Label(Frame1, text='I-EGO', bg="#777777")
        title.pack()
        title.config(font=('', 30,))
        Frame1.pack(side=TOP, padx=10, pady=10)

        Frame2 = Frame(self.root, height=200, bg="#777777")

        Framel = Frame(Frame2, height=200, bg="#777777")
        lbl1 = Label(Framel, text='select file', bg="#777777")
        lbl1.pack(pady=10)
        lbl1.config(font=('', 20,))
        scroll = Scrollbar(Framel)
        scroll.pack(side=RIGHT, fill=Y)
        text1 = Listbox(Framel, width=50, yscrollcommand=scroll.set, selectmode=MULTIPLE, selectbackground="#333333")
        text1.pack(side=LEFT, fill=Y)
        dnd.bindtarget(text1, drop, 'text/uri-list')
        Framel.pack(side=LEFT, fill=Y)

        Framer = Frame(Frame2, height=200, bg="#777777")
        blink = Label(Framer, width=2, height=3, bg="#777777")
        blink.grid(row=0, column=0)
        btnAdd = Button(Framer, text='+', command=findfile, width=2, height=1)
        btnAdd.grid(row=1, column=0)
        btnDelete = Button(Framer, text='-', command=deletefile, width=2, height=1)
        btnDelete.grid(row=2, column=0)
        Framer.pack(side=RIGHT, fill=Y, padx=5)

        Frame2.pack(side=LEFT, fill=Y, padx=10, pady=10)

        Frame3 = Frame(self.root, height=200, bg="#777777")
        lbl2 = Label(Frame3, text='kind of file', bg="#777777")
        lbl2.grid(row=0, column=0, padx=10, pady=10, columnspan='4')
        lbl2.config(font=('', 20,))
        lbl3 = Label(Frame3, text='IP: ' + user_ip, bg="#777777", borderwidth=2, relief="solid", width=23, anchor="w")
        lbl3.grid(row=1, column=1, padx=10, sticky=W, columnspan='3')
        lbl3.config(font=('', 15,))
        btn2 = Button(Frame3, text='connect', command=connect, width=15)
        btn2.grid(row=1, column=0, padx=10)
        btn3 = Button(Frame3, text='start recording', command=startrecording, width=15)
        btn3.grid(row=2, column=0, padx=10)
        btn4 = Button(Frame3, text='stop recording', command=stoprecording, width=15)
        btn4.grid(row=3, column=0, padx=10)
        btn5 = Button(Frame3, text='send', command=senddata, width=15)
        btn5.grid(row=4, column=0, padx=10, pady=10)
        btn6 = Button(Frame3, text='result', command=stopapp, width=15)
        btn6.grid(row=5, column=0, padx=10)

        cb1 = Radiobutton(Frame3, text='stereo', value=1, variable=screentype, bg="#777777")
        cb1.grid(row=2, column=1, padx=10, pady=10)
        cb2 = Radiobutton(Frame3, text='180 angle', value=2, variable=screentype, bg="#777777")
        cb2.grid(row=2, column=2, padx=10, pady=10)
        cb3 = Radiobutton(Frame3, text='360 angle', value=3, variable=screentype, bg="#777777")
        cb3.grid(row=2, column=3, padx=10, pady=10)
        cb4 = Radiobutton(Frame3, text='none', value=1, variable=dirtype, bg="#777777")
        cb4.grid(row=3, column=1, padx=10)
        cb5 = Radiobutton(Frame3, text='topbottom', value=2, variable=dirtype, bg="#777777")
        cb5.grid(row=3, column=2, padx=10)
        cb6 = Radiobutton(Frame3, text='leftright', value=3, variable=dirtype, bg="#777777")
        cb6.grid(row=3, column=3, padx=10)
        lbl4 = Label(Frame3, text='Delay', bg="#777777", borderwidth=2, relief="solid", width=7)
        lbl4.grid(row=4, column=1, padx=10)
        text2 = Entry(Frame3, borderwidth=2, relief="solid")
        text2.grid(row=4, column=2, padx=10, columnspan='2')
        Frame3.pack(side=RIGHT, fill=Y, padx=10, pady=10)
        self.root.mainloop()

# TCP 스레드
# TCPqueue에 들어있는 데이터 전달
class TCP(threading.Thread):
    global running

    def run(self):
        sock = socket(AF_INET, SOCK_STREAM)
        while running:
            try:
                global user_ip
                global TCPqueue
                global connected
                while running:
                    while running:
                        if user_ip != '':
                            break
                        else:
                            time.sleep(1)
                    if running:
                        ip = user_ip
                        connected = False
                    while running:
                        if ip != user_ip:
                            sock.close()
                            connected = True
                            break
                        if len(TCPqueue) != 0:
                            if TCPqueue[0] != '':
                                sock = socket(AF_INET, SOCK_STREAM)
                                sock.connect((ip, 7001))
                                sock.sendto(TCPqueue[0], (ip, 7001))
                                print(TCPqueue[0])
                                TCPqueue.pop(0)
                                sock.close()
                        else:
                            time.sleep(1)
            finally:
                sock.close()

# UDPread 스레드
# 통신이 올 때 까지 대기
# 통신이 오면 메세지와 주소 저장
class UDPread(threading.Thread):
    global running
    sock = socket(AF_INET, SOCK_DGRAM)

    def run(self):
        try:
            self.sock = socket(AF_INET, SOCK_DGRAM)
            self.sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
            self.sock.bind(('', 8002))
            global UDPqueue
            global msgs
            global addrs
            while running:
                s, addr = self.sock.recvfrom(1024)
                print(s)
                print(addr)
                msgs.append(s.decode())
                addrs.append(addr[0])
        except OSError as e:
            print(e)
        finally:
            self.sock.close()

    def stop(self):
        self.sock.shutdown(2)
        self.sock.close()

# UDPwrite 스레드
# IP 서브넷 획득
# UDPqueue에 들어있는 데이터 전달
class UDPwrite(threading.Thread):
    global running
    ip = so.gethostbyname(so.getfqdn())
    print(ip)
    list = ip.split('.')
    ip = list[0]+'.'+list[1]+'.'+list[2]+'.255'
    print(ip)

    def run(self):
        sock = socket(AF_INET, SOCK_DGRAM)
        try:
            sock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
            global UDPqueue
            global msgs
            global addrs
            while running:
                if len(UDPqueue) != 0:
                    sock.sendto(UDPqueue[0].encode(), (self.ip, 8000))
                    # sock.sendto(UDPqueue[0].encode(), ("127.0.0.1", 8000))
                    print(UDPqueue[0])
                    UDPqueue.pop(0)
                else:
                    time.sleep(1)
        finally:
            sock.close()

# 메인 - 스레드 관리
if __name__ == '__main__':
    pr1 = UI()
    pr2 = UDPwrite()
    pr3 = UDPread()
    pr4 = TCP()
    pr1.setDaemon(True)
    pr2.setDaemon(True)
    pr3.setDaemon(True)
    pr4.setDaemon(True)
    pr1.start()
    pr2.start()
    pr3.start()
    pr4.start()
    while pr1.isAlive():
        running = True
    running = False
    pr1.join()
    pr2.join()
    pr3.stop()
    pr3.join()
    pr4.join()


#SP点集配对问题
# Not Checked

INF = 0x3f3f3f3f

def subset_pair(mat):
    n = len(mat)
    dp = [[INF]*n]*(1<<n)
    for i in range(n):
        for s in range(1<<n):
            for j in range(i):
                if s&(1<<j)!=0:  #枚举子集
                    dp[i][s] = min(dp[i][s],mat[i][j]+dp[i-1][s^(1<<i)^(1<<j)])
    return dp[n-1][(1<<n)-1]

# 优化
def subset_pair_two(mat):
    n = len(mat)
    dp = [INF]*(1<<n)
    for s in range(1<<n):
        i = 0
        for i in range(n):
            if s&(1<<i)!=0:
                break
        for j in range(i+1,n):
            if s&(1<<j) != 0:
                dp[s] = min(dp[s], mat[i][j]+dp[s^(1<<i)^(1<<j)])
    return dp[n-1][(1<<n)-1]

# 返回距离矩阵
def read():
    n = int(input())
    mat = [[0]*n]*n
    for i in range(n):
        p = input().split()
        mat[int(p[0])][int(p[1])] = int(p[2])
    return mat

if __name__ == '__main__':
    print(subset_pair(read()))


# 76. チェックポイント
# 問題75のコードを改変し，各エポックのパラメータ更新が完了するたびに，チェックポイント（学習途中のパラメータ（重み行列など）の値や最適化アルゴリズムの内部状態）をファイルに書き出せ．

import torch
import joblib
import numpy as np
from tqdm import tqdm
from torch import nn, optim
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score

if __name__ == '__main__':
    X_train = joblib.load('X_train.joblib')
    y_train = joblib.load('y_train.joblib')
    X_train = torch.from_numpy(X_train.astype(np.float32)).clone()
    y_train = torch.from_numpy(y_train.astype(np.int64)).clone()

    # validを読み込む
    X_valid = joblib.load('X_valid.joblib')
    y_valid = joblib.load('y_valid.joblib')
    X_valid = torch.from_numpy(X_valid.astype(np.float32)).clone()
    y_valid = torch.from_numpy(y_valid.astype(np.int64)).clone()

    # モデルを作成する
    torch.manual_seed(0) # シードを固定する
    net = nn.Sequential(nn.Linear(300, 4), nn.PReLU())
    loss_fn = nn.CrossEntropyLoss()
    optimizer = optim.SGD(net.parameters(), lr = 0.01)

    train_losses = []
    valid_losses = []
    train_accs = []
    valid_accs = []
    for epoch in tqdm(range(100000)):
        net.train()
        optimizer.zero_grad() # 勾配をゼロで初期化する
        y_pred = net(X_train) # ラベル予測する
        loss = loss_fn(y_pred, y_train) # 損失を求める
        loss.backward() # 勾配を計算
        optimizer.step()

        # モデルを保存する
        joblib.dump(net.state_dict(), f'checkpoint_{epoch}.joblib')

        net.eval()
        train_losses.append(loss)
        valid_losses.append(loss_fn(net(X_valid), y_valid))
        # values, indices = torch.max(tensor, 0)
        values, y_pred_train = torch.max(net(X_train), 1)
        values, y_pred_valid = torch.max(net(X_valid), 1)
        train_accs.append(accuracy_score(y_pred_train, y_train))
        valid_accs.append(accuracy_score(y_pred_valid, y_valid))

    plt.plot(train_losses, label = 'train loss')
    plt.plot(valid_losses, label = 'valid loss')
    plt.legend()
    plt.show()

    plt.plot(train_accs, label = 'train acc')
    plt.plot(valid_accs, label = 'valid acc')
    plt.legend()
    plt.show()


'''
The proper divisors of a number are all the divisors excluding the number itself.
For example, the proper divisors of 28 are 1, 2, 4, 7, and 14.
As the sum of these divisors is equal to 28, we call it a perfect number.

Interestingly the sum of the proper divisors of 220 is 284 and the sum of the proper divisors of 284 is 220,
forming a chain of two numbers. For this reason, 220 and 284 are called an amicable pair.

Perhaps less well known are longer chains. For example, starting with 12496, we form a chain of five numbers:
12496 → 14288 → 15472 → 14536 → 14264 (→ 12496 → ...)

Since this chain returns to its starting point, it is called an amicable chain.

Find the smallest member of the longest amicable chain with no element exceeding one million.
'''
def calcDivisorsList(target):
    if target < 0:
        return []
    divisors = [1]
    for i in range(2,int(target**(1/2))+1):
        if target % i == 0:
            if target/i == i:
                divisors.append(i)
            else:
                divisors.append(i)
                divisors.append(target/i)
    return divisors

def longestChain(lim):
    chains = {}
    for x in range(1,lim):
        chain = set()

    for x in chains:
        print(x, chains[x])
    return

def main():
    longestChain(12500)
    return

if __name__ == '__main__':
    main()


import numpy as np
import random
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
import pandas as pd

var_e = 0.2
var_h = 0.4

mean_h = [0, 0]
mean_e1 = [-1, 1]
mean_e2 = [1, 1]

p = 0.5

pop = []
for i in range(10000):
    mean, var = random.choices(
        population=[(mean_h, var_h), (mean_e1, var_e), (mean_e2, var_e)],
        weights=[p, (1 - p) / 2, (1 - p) / 2]
    )[0]
    pop.append(np.random.multivariate_normal(mean, (var ** 2) * np.identity(2)))

z1 = np.array(pop, dtype=np.dtype('float'))
plt.scatter(z1[:, 0], z1[:, 1])

X = np.array([[1, 2], [1, 4], [1, 0],
              [10, 2], [10, 4], [10, 0]])
kmeans = KMeans(n_clusters=3, random_state=0).fit(pop)
labels = kmeans.labels_
df = pd.DataFrame(data=pop, columns=['X1', 'X2']).assign(cluster=kmeans.labels_)

plt.scatter(df[df['cluster'] == 1]['X1'], df[df['cluster'] == 1]['X2'])
plt.scatter(df[df['cluster'] == 2]['X1'], df[df['cluster'] == 2]['X2'])
plt.scatter(df[df['cluster'] == 0]['X1'], df[df['cluster'] == 0]['X2'])


import os
import glob
import cv2
import numpy as np
from PIL import Image
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn import metrics
from sklearn.ensemble import RandomForestClassifier
import matplotlib.pyplot as plt
import random
import pickle

flag =0
def extractWindowFake(img,mask):
	l=[]
	rows,cols=img.shape[:2]
	windSize=64
	stride=32
	for i in range(0,rows-windSize+1,32):
		for j in range(0,cols-windSize+1,32):
			if (i+windSize)>=rows or (j+windSize) >=cols:
				continue
			subSec=[]
			count1s=0
			for u in range(windSize):
				temp=[]
				for v in range(windSize):
					r=img[i+u][j+v][0]
					g=img[i+u][j+v][1]
					b=img[i+u][j+v][2]
					temp.append((r,g,b))
					temp_val = 0
					try:
						temp_val = mask[i+u][j+v]
					except:
						pass
					if temp_val==255:
						count1s+=1
				subSec.append(temp)
			if count1s>1600 and (windSize**2-count1s)>1600:
				l.append(subSec)
	return l
def extractWindow(img):
	l=[]
	rows,cols=img.shape[:2]
	windSize=64
	stride=32
	for i in range(0,rows-windSize+1,32):
		for j in range(0,cols-windSize+1,32):
			if (i+windSize)>=rows or j+windSize>=cols:
				continue
			if random.randint(0,40)!=5:
				continue
			subSec=[]
			count1s=0
			for u in range(windSize):
				temp=[]
				for v in range(windSize):
					r=img[i+u][j+v][0]
					g=img[i+u][j+v][1]
					b=img[i+u][j+v][2]
					temp.append((r,g,b))
				subSec.append(temp)
			l.append(subSec)

	return l
path1="D:\\Downloads\\4cam_splc"
path2="D:\\Downloads\\4cam_auth"
path3="D:\\Downloads\\4cam_splc\\edgemask"
mylist1 = [f for f in glob.glob(os.path.join(path1,"*.tif"))]
mylist2 = [f for f in glob.glob(os.path.join(path2,"*.tif"))]
mylistMask=[f for f in glob.glob(os.path.join(path3,"*.jpg"))]

r1=range(170,180,1)	# Decrease the range by 10 to create more batches
r2=range(300,320,2) # Decrease the range by 20 to create more batches
data=[]
tag=[]
for i,j in zip(r1,r2):
	flag+=1
	img=cv2.imread(mylist1[i])
	mask=cv2.imread(mylistMask[j])
	mask=cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
	ret,mask = cv2.threshold(mask,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
	l=extractWindowFake(img,mask)
	for someThing in l:
		data.append(someThing)
		tag.append(1)
	'''
	cv2.imshow("org",img)
	cv2.imshow("mask",mask)
	cv2.waitKey(0)
	break
	'''
for i in r1:
	flag+=1
	img=cv2.imread(mylist2[i])
	l=extractWindow(img)
	for someThing in l:
		data.append(someThing)
		tag.append(0)

dict1={'data':data,'tag':tag}
f1 = open('tastyPickle16', 'ab')
pickle.dump(dict1, f1)
f1.close()


#  Hint:  You may not need all of these.  Remove the unused functions.
from hashtables import (HashTable,
                        hash_table_insert,
                        hash_table_remove,
                        hash_table_retrieve,
                        hash_table_resize)

def get_indices_of_item_weights(weights, length, limit):
    ht = HashTable(16)

    """
    YOUR CODE HERE
    """
    for i in range(0, length - 1):
        hash_table_insert(ht, weights[i], i)

    print(ht.storage)

    for i in range(0, length - 1):
        temp = limit - weights[i]
        x = hash_table_retrieve(ht, temp)
        if x is not None:
            if x == i:
                return (x + 1, i)
            return (x, i)

    return None

def print_answer(answer):
    if answer is not None:
        print(str(answer[0] + " " + answer[1]))
    else:
        print("None")

get_indices_of_item_weights([4, 4], 2, 8)


import unittest

import polib

from sportorg import config
from sportorg.language import get_languages

class TestLang(unittest.TestCase):

    def setUp(self):
        print('=========>', self.__class__.__name__, self._testMethodName)

    def test_duplicates(self):
        locales = get_languages()
        name = config.NAME.lower()
        for locale in locales:
            path = config.base_dir(config.LOCALE_DIR, locale, 'LC_MESSAGES', name)
            po = polib.pofile(path + '.po', check_for_duplicates=True)
            po.save_as_mofile(path + '.mo')


import FWCore.ParameterSet.Config as cms

process = cms.Process("Digianalyzer")

process.load("FWCore.MessageService.MessageLogger_cfi")

process.load('Configuration.StandardSequences.Services_cff')
process.load('SimGeneral.HepPDTESSource.pythiapdt_cfi')
process.load('FWCore.MessageService.MessageLogger_cfi')
process.load('Configuration.EventContent.EventContent_cff')
process.load('FWCore.MessageService.MessageLogger_cfi')
process.load('Configuration.EventContent.EventContent_cff')
process.load('SimGeneral.MixingModule.mixNoPU_cfi')
process.load('Configuration.Geometry.GeometryExtended2023HGCalMuonReco_cff')
process.load ('Configuration.Geometry.GeometryExtended2023HGCalMuon_cff')
process.load('Configuration.StandardSequences.MagneticField_38T_PostLS1_cff')
process.load('Configuration.StandardSequences.Generator_cff')
process.load('IOMC.EventVertexGenerators.VtxSmearedGauss_cfi')
process.load('GeneratorInterface.Core.genFilterSummary_cff')
process.load('Configuration.StandardSequences.SimIdeal_cff')
process.load('Configuration.StandardSequences.Digi_cff')
process.load('Configuration.StandardSequences.SimL1Emulator_cff')
process.load('Configuration.StandardSequences.DigiToRaw_cff')
process.load('Configuration.StandardSequences.EndOfProcess_cff')
process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_cff')

from Configuration.AlCa.GlobalTag import GlobalTag
process.GlobalTag = GlobalTag(process.GlobalTag, 'auto:upgradePLS3', '')

process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(-1) )

process.source = cms.Source("PoolSource",
                            # replace 'myfile.root' with the source file you want to use
                            fileNames = cms.untracked.vstring(
        #'file:/afs/cern.ch/work/k/khurana/HGCAL/Digi/CMSSW_6_2_0_SLHC16/src/EventGeneration/HGC/FourMuEvents_140PU_200GenSimDigi.root'
        #'file:/afs/cern.ch/work/k/khurana/HGCAL/Digi/CMSSW_6_2_0_SLHC16/src/Validation/HGCalValidation/Muon_500Events.root'
        #'file:../../Samples/Muon_500Events.root'
        #'file:/afs/cern.ch/work/k/khurana/HGCAL/Digi/CMSSW_6_2_0_SLHC16/src/Samples/12200_FourMuPt1_200+FourMuPt_1_200_Extended2023HGCalMuon_GenSimFull+DigiFull_Extended2023HGCalMuon+RecoFull_Extended2023HGCalMuon+HARVESTFull_Extended2023HGCalMuon/step2_1event.root'
        #'file:/afs/cern.ch/work/k/khurana/HGCAL/Digi/CMSSW_6_2_0_SLHC16/src/Samples/step2.root'
        #'file:/afs/cern.ch/work/k/khurana/HGCAL/Digi/CMSSW_6_2_0_SLHC16/src/EventGeneration/HGC/step2_10FourMuEvents_140PU.root'

        #'file:/afs/cern.ch/work/k/khurana/HGCAL/Digi/CMSSW_6_2_0_SLHC16/src/EventGeneration/HGC/step2_SingleMuonEvents_140PU_NoShaping.root'
        #'file:/afs/cern.ch/work/k/khurana/HGCAL/Digi/CMSSW_6_2_0_SLHC16/src/EventGeneration/HGC/gen-sim-digi_Pt-10_Eta-1.75_ID-13.root'
        #'file:/tmp/khurana/gen-sim-digi-Pt-10.0_Eta-2.0__TauValue-20.0.root'
        #'file:/tmp/khurana/gen-sim-digi-Pt-10.0_Eta-2.0_140PU__TauValue-5.0.root'
        'root://eoscms.cern.ch//store/user/khurana/HGCAL/Digi/SingleParticle/INPUTFILENAME'
        )
                            )

process.digianalyzerEE = cms.EDAnalyzer('DigiAnalyzer',
                                        DetectorName = cms.string("HGCalEESensitive"),
                                        DigiSource = cms.string("HGCDigisEE"),
                                        Verbosity     = cms.untracked.int32(1)
                                        )

process.TFileService = cms.Service("TFileService",
                                   fileName = cms.string("DigiHisto-INPUTFILENAME.root"),
                                   closeFileFast = cms.untracked.bool(True)
                                   )

process.p = cms.Path(process.digianalyzerEE )


import cowrie.core.output

from csirtgsdk.indicator import Indicator
from csirtgsdk.client import Client
from datetime import datetime
from pprint import pprint
import logging
import os

logger = logging.getLogger(__name__)

USERNAME = os.environ.get('CSIRTG_USER')
FEED = os.environ.get('CSIRTG_FEED')
TOKEN = os.environ.get('CSIRG_TOKEN')

class Output(cowrie.core.output.Output):
    def __init__(self, cfg):
        cowrie.core.output.Output.__init__(self, cfg)
        self.user = cfg.get('output_csirtg', 'username') or USERNAME
        self.feed = cfg.get('output_csirtg', 'feed') or FEED
        self.token = cfg.get('output_csirtg', 'token') or TOKEN
        self.port = os.environ.get('COWRIE_PORT', 22)
        self.context = {}
        self.client = Client(token=self.token)

    def start(self,):
        pass

    def stop(self):
        pass

    def write(self, e):
        sid = e['session']
        peerIP = e['src_ip']
        ts = e['timestamp']

        today = str(datetime.now().date())
        logger.info('today is %s' % today)

        if not self.context.get(today):
            logger.info('resetting context for %s' % today)
            self.context = {}
            self.context[today] = {}

        if not self.context[today].get(peerIP):
            self.context[today][peerIP] = []

            i = {
                'user': self.user,
                'feed': self.feed,
                'indicator': peerIP,
                'portlist': self.port,
                'protocol': 'tcp',
                'tags': 'scanner,ssh',
                'firsttime': ts,
                'lasttime': ts
            }

            ret = Indicator(self.client, i).submit()

            logger.info('logged to csirtg %s ' % ret['indicator']['location'])
        else:
            pprint(self.context)
        self.context[today][peerIP].append(sid)


import jingo
from addons.models import Persona
from twitter import search
from . import tags, email_enabled
from . import teams as teams_config

import logging

log = logging.getLogger('z.firefoxcup')

# Create your views here.
def index(request):

    tweets = search(tags['all'], lang=request.LANG)

    if len(tweets) < 15:
        extra = search(tags['all'], 'all')
        tweets.extend(extra)

    # we only want 15 tweets
    tweets = tweets[:15]

    teams = dict((813, t) for t in teams_config)
    for persona in Persona.objects.filter(persona_id__in=teams.keys()):
        teams[persona.persona_id]['persona'] = persona

    return jingo.render(request, 'firefoxcup/index.html', {
        'tweets': tweets,
        'teams': teams.values(),
        'email_enabled': email_enabled,
    })


# 6.10.5-1
# チェックポイント

import sys
import os

# コマンドライン
c_arg = len(sys.argv)
argument = sys.argv

#  変数代入
file = argument[0]                  # 実行ファイル
intput_file_path = argument[1]      # 入力ファイル
output_file_path = argument[2]      # 出力ファイル

# データのsplit
def split_data():
    # 出力ファイルがあれば削除
    if(os.path.isfile(output_file_path)):
        os.remove(output_file_path)

    # fileがない!
    if(os.path.isfile(intput_file_path) == False):
        print('{}: Oh no! No such file or directory (´・ω・)'.format(intput_file_path))
        exit(1)

    data_list = []
    # fileある
    with open (intput_file_path, 'r') as r_file:
        for line_data in r_file:
            # stripしてsplit
            split_data = (line_data.strip()).split(' ')

            # リストに同じ単語がなければ代入
            for i in range(len(split_data)):
                if split_data[i] not in data_list:
                    data_list.append(split_data[i])
        # ファイル書き出し
        out_file(data_list)

# ファイル書き出し
def out_file(out_data):
    # ファイルへ書き出し
    with open(output_file_path, 'a') as w_file:
        for i in range(len(out_data)):
            w_file.write(out_data[i]+' ')

# 本体プログラム
split_data()


#!/usr/bin/env python

import jinja2
import pathlib
import os
import sys

def main():
    src_path = pathlib.Path(sys.argv[1])
    dir_path = src_path.parent
    env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(str(dir_path)),
        lstrip_blocks=True,
        trim_blocks=True,
        line_comment_prefix='#',
    )

    tmpl = env.get_template('cpu.json.j2')
    config = {
        'CACHE_WIDTH': 64,
        'L1_CACHE_SIZE': 32,
    }
    tmpl.stream(config).dump(sys.stdout)

if __name__ == '__main__':
    main()


"""
archive.interface
==========
@authors: timo.schroeder@ipp-hgw.mpg.de
data rooturl database view    project strgrp stream idx    channel
lev  0       1        2       3       4      5      6      7
"""
import os as _os
import mmap as _mmap
import json as _json
import MDSplus as _mds
import numpy as _np

try:
    import h5py as _h5
except:
    print('WARNING: "h5py" package not found.\nImage upload will not be available')
from . import base as _base
from . import cache as _cache
from . import support as _sup
from . import version as _ver

SQCache =  _cache.cache(_ver.tmpdir+'archive_cache'+str(_ver.pyver[0]))

class URLException(Exception):
    def __init__(self,value):
        return value

def write_logurl(url, parms, time):
    log = {'label' : 'parms',
           'values': [parms],
           'dimensions': [_base.TimeInterval(time).fromT.ns,-1]
           }
    return(post(url, json=log))  # , data=json.dumps(cfg)

def write_data(path, data, dimof, t0=0):
    # path=Path, data=numpy.array, dimof=numpy.array
    if not isinstance(data, _np.ndarray):
        raise Exception('write_data: data must be numpy.ndarray')
    dimof = _np.array(dimof)
    if dimof.dtype == float:
        dimof = (dimof*1e9).astype('uint64')
    dimof = dimof + t0
    if dimof.ndim == 0:  # we need to add one level
        dimof = [dimof.tolist()]
        data  = data.reshape(*(list(data.shape)+[1]))
    else:
        dimof = dimof.tolist()
    if data.ndim > 2:
        return(_write_vector(_base.Path(path), data, dimof))
    else:
        return(_write_scalar(_base.Path(path), data, dimof))

def _write_scalar(path, data, dimof):
    # path=Path, data=numpy.array, dimof=list of long
    dtype = str(data.dtype)
    if dtype in ['bool','int8','uint8','int16','uint16']:
        datatype='short'
    elif dtype in ['int32','uint32']:
        datatype='int'
    elif dtype in ['int64','uint64']:
        datatype='long'
    else:
        datatype='float'
    jdict = {'values': data.tolist(), 'datatype':datatype, 'dimensions': dimof}
    return(post(path.url_datastream(), json=jdict))

def _write_vector(path, data, dimof):
    # path=Path, data=numpy.array, dimof=list of long
    dtype = str(data.dtype)
    stream = path.stream
    tmpfile = _ver.tmpdir+"archive_"+stream+".h5"
    try:
        with _h5.File(tmpfile, 'w') as f:
            g = f.create_group('data')
            g.create_dataset(stream, data=data.transpose(*list(range(1,data.ndim)+[0])).tolist(), dtype=dtype,
                             compression="gzip")
            g.create_dataset('timestamps', data=list(dimof), dtype='int64',
                             compression="gzip")
        headers = {'Content-Type': 'application/x-hdf'}
        link = path.url_streamgroup()+'?dataPath=data/'+stream+'&timePath=data/timestamps'
        with open(tmpfile, 'rb') as f:
            return(post(link, headers=headers, data=f))
    finally:
        _os.remove(tmpfile)

def read_signal(path, time, t0=0, *arg):
    path = _base.Path(path)
    time = _base.TimeInterval(time)
    sig = None
    if time.uptoT != -1:
        key = path.path()+'?'+','.join(map(str,[time.fromT,time.uptoT]+list(arg)))
        sig = SQCache.get(key)
    if sig is None:
        print('get web-archive: '+key)
        stream = get_json(path.url_data(), time, *arg)
        sig = _base.createSignal(stream['values'], stream['dimensions'], str(stream.get('unit', 'unknown')))
        SQCache.set(key, sig)
    if t0!=0:
        args = list(sig.args)
        time = _mds.Float64((args[2].args[1]-_mds.Int64(t0))*1E-9)
        wind = _mds.Window(time[0], time[time.shape[0]-1], t0)
        args[2] = _mds.Dimension(wind, time)
        args[2].setUnits('s')
        sig.args = tuple(args)
    return sig

def get_json(url, *arg):
    class reader(object):
        def __init__(self, value):
            self.value = value
        def read(self,*argin):
            return _ver.tostr(self.value.read(*argin))
    url = _base.Path(url).url(-1, *arg)
    _debug(url)
    headers = {'Accept': 'application/json'}
    handler = get(url, headers)
    if handler.getcode() != 200:
        raise Exception('request failed: code '+str(handler.getcode()))
    if handler.headers.get('content-type') != 'application/json':
        raise Exception('requested content-type mismatch: ' +
                        handler.headers.get('content-type'))

    return _json.load(reader(handler), strict=False)

def post(url, headers={}, data=None, json=None):
#    if url[-1] != '/':
 #       url += '/'
    if json is not None:
        data = _json.dumps(json)
        headers['content-type'] = 'application/json'
    if isinstance(data, (file)):
        _debug(data.name)
        data = _mmap.mmap(data.fileno(), 0, access=_mmap.ACCESS_READ)
    else:
        _debug(data)
        data = _ver.tobytes(data)
    _debug(url)
    result = get(url, headers, data)
    if isinstance(data,(_mmap.mmap)):
        data.close()
    return result

def get(url, headers={}, *data):
    req = _ver.urllib.Request(url)
    for k, v in headers.items():
        req.add_header(k, v)
    try:
        handler = _ver.urllib.urlopen(req, *data)
    except _ver.urllib.HTTPError as err:
        print(err.reason)
        print(err.read())
        raise(err)
    return(handler)

def parseXML(toparse):
    import xml.etree.cElementTree as ET
    from re import compile as recmp
    re = recmp('(?:\{[^\}]*\}|)(.*)')

    def addvalue(res, name, value):
        if name in res.keys():
            if type(res[name]) is list:
                res[name].append(value)
            else:
                res[name] = [res[name], value]
        else:
            res[name] = value

    def xmltodict(node):
        res = {}
        if len(node):
            for n in list(node):
                name = re.match(n.tag).group(1)
                addvalue(res, name, xmltodict(n))
        else:  # is leaf
            res = node.text
        return res
    tree = ET.fromstring(toparse.encode('utf-8'))
    return xmltodict(tree)

def read_parlog(path, time=[1, 0], *args):
    def rmfield(dic, field):
        if field in dic.keys():
            del(dic[field])

    def dict2list(d):
        l = len(d.keys())*[None]
        for k, v in d.items():
            l[int(k[1:-1])] = v
        return l

    def integrateChLst(chans):
        chtmp = parseXML(chans['xmlDescription'])['channel']
        active = chans.get('active', None)
        pQ = chans.get('physicalQuantity', None)
        chans = [{} for i in chtmp]
        for ch in chtmp:
            ch['active'] = active
            ch['physicalQuantity'] = pQ
            i = int(ch['channelNumber'])
            rmfield(ch, 'channelNumber')
            for k, v in ch.items():
                if v is not None:
                    chans[i][k] = _sup.cp(v)
        return chans
    url = _base.Path(path).url_parlog(time, *args)
    par = get_json(url)
    if type(par) is not dict:
        raise Exception('parlog not found:\n'+url)
    par = par['values'][-1]
    if 'chanDescs' in par.keys():
        cD = par['chanDescs']
        if isinstance(cD, dict):
            cD = dict2list(cD)
        if isinstance(cD, list) and len(cD) == 1 and 'xmlDescription' in cD[0].keys():
            cD = integrateChLst(cD[0])
        par['chanDescs'] = cD
    return par

def read_cfglog(path, time=[0, 0], *args):
    url = _base.Path(path).url_cfglog(time, *args)
    par = get_json(url)
    if type(par) is not dict:
        raise Exception('cfglog not found:\n'+url)
    return par

def read_jpg_url(url, time, skip=0):
    time = _base.TimeInterval(time)
    link = url + '/_signal.jpg?' + str(time) + '&skip=' + str(skip)
    _debug(link)
    r = get(link)
    return r

def read_png_url(url, time, skip=0):
    time = _base.TimeInterval(time)
    link = url + '/_signal.png?' + str(time) + '&skip=' + str(skip)
    _debug(link)
    r = get(link)
    return r

def read_raw_url(url, time, skip=0):
    time = _base.TimeInterval(time)
    link = url + '/_signal.png?' + str(time) + '&skip=' + str(skip)
    _debug(link)
    r = get(link)
    return r

def _debug(msg):
    import inspect
    try:
        print(inspect.stack()[1][3] + ': ' + str(msg))
    except:
        print(msg)


from helper.bigquery_hook import BigQueryHook
from airflow.operators.check_operator import \
    CheckOperator, ValueCheckOperator, IntervalCheckOperator
from airflow.utils.decorators import apply_defaults

class BigQueryCheckOperator(CheckOperator):
    """
    Performs checks against BigQuery. The ``BigQueryCheckOperator`` expects
    a sql query that will return a single row. Each value on that
    first row is evaluated using python ``bool`` casting. If any of the
    values return ``False`` the check is failed and errors out.

    Note that Python bool casting evals the following as ``False``:

    * ``False``
    * ``0``
    * Empty string (``""``)
    * Empty list (``[]``)
    * Empty dictionary or set (``{}``)

    Given a query like ``SELECT COUNT(*) FROM foo``, it will fail only if
    the count ``== 0``. You can craft much more complex query that could,
    for instance, check that the table has the same number of rows as
    the source table upstream, or that the count of today's partition is
    greater than yesterday's partition, or that a set of metrics are less
    than 3 standard deviation for the 7 day average.

    This operator can be used as a data quality check in your pipeline, and
    depending on where you put it in your DAG, you have the choice to
    stop the critical path, preventing from
    publishing dubious data, or on the side and receive email alerts
    without stopping the progress of the DAG.

    :param sql: the sql to be executed
    :type sql: str
    :param bigquery_conn_id: reference to the BigQuery database
    :type bigquery_conn_id: str
    :param use_legacy_sql: Whether to use legacy SQL (true)
        or standard SQL (false).
    :type use_legacy_sql: bool
    """

    template_fields = ('sql',)
    template_ext = ('.sql', )

    @apply_defaults
    def __init__(self,
                 sql,
                 bigquery_conn_id='bigquery_default',
                 use_legacy_sql=True,
                 location=None,
                 *args, **kwargs):
        super(BigQueryCheckOperator, self).__init__(sql=sql, *args, **kwargs)
        self.bigquery_conn_id = bigquery_conn_id
        self.sql = sql
        self.use_legacy_sql = use_legacy_sql
        self.location = location

    def get_db_hook(self):
        return BigQueryHook(bigquery_conn_id=self.bigquery_conn_id,
                            use_legacy_sql=self.use_legacy_sql,
                            location=self.location)

class BigQueryValueCheckOperator(ValueCheckOperator):
    """
    Performs a simple value check using sql code.

    :param sql: the sql to be executed
    :type sql: str
    :param use_legacy_sql: Whether to use legacy SQL (true)
        or standard SQL (false).
    :type use_legacy_sql: bool
    """

    template_fields = ('sql', 'pass_value',)
    template_ext = ('.sql', )

    @apply_defaults
    def __init__(self, sql,
                 pass_value,
                 tolerance=None,
                 bigquery_conn_id='bigquery_default',
                 location=None,
                 use_legacy_sql=True,
                 *args, **kwargs):
        super(BigQueryValueCheckOperator, self).__init__(
            sql=sql, pass_value=pass_value, tolerance=tolerance,
            *args, **kwargs)
        self.bigquery_conn_id = bigquery_conn_id
        self.use_legacy_sql = use_legacy_sql
        self.location = location

    def get_db_hook(self):
        return BigQueryHook(bigquery_conn_id=self.bigquery_conn_id,
                            use_legacy_sql=self.use_legacy_sql,
                            location=self.location)

class BigQueryIntervalCheckOperator(IntervalCheckOperator):
    """
    Checks that the values of metrics given as SQL expressions are within
    a certain tolerance of the ones from days_back before.

    This method constructs a query like so ::

        SELECT {metrics_threshold_dict_key} FROM {table}
        WHERE {date_filter_column}=<date>

    :param table: the table name
    :type table: str
    :param days_back: number of days between ds and the ds we want to check
        against. Defaults to 7 days
    :type days_back: int
    :param metrics_threshold: a dictionary of ratios indexed by metrics, for
        example 'COUNT(*)': 1.5 would require a 50 percent or less difference
        between the current day, and the prior days_back.
    :type metrics_threshold: dict
    :param use_legacy_sql: Whether to use legacy SQL (true)
        or standard SQL (false).
    :type use_legacy_sql: bool
    """

    template_fields = ('table',)

    @apply_defaults
    def __init__(self, table, metrics_thresholds, date_filter_column='ds',
                 days_back=-7, bigquery_conn_id='bigquery_default',
                location=None, use_legacy_sql=True, *args, **kwargs):
        super(BigQueryIntervalCheckOperator, self).__init__(
            table=table, metrics_thresholds=metrics_thresholds,
            date_filter_column=date_filter_column, days_back=days_back,
            *args, **kwargs)
        self.bigquery_conn_id = bigquery_conn_id
        self.use_legacy_sql = use_legacy_sql
        self.location = location

    def get_db_hook(self):
        return BigQueryHook(bigquery_conn_id=self.bigquery_conn_id,
                            use_legacy_sql=self.use_legacy_sql,
                            location=self.location)


import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
import os.path
import networkx as nx
import pickle

G1 = nx.read_edgelist("/Users/alexwang/Documents/UCLA 2019 Fall/Wasserstein Research/GraphMatching/model/s-gwl/data/aids_toy1.edgelist")
G2 = nx.read_edgelist("/Users/alexwang/Documents/UCLA 2019 Fall/Wasserstein Research/GraphMatching/model/s-gwl/data/aids_toy2.edgelist")

data = {}

# degree1 =
probs1 = [x[1] for x in G1.degree]
probs1 = np.array(probs1 / np.sum(probs1)).reshape((len(probs1), 1))

# degree2 =
probs2 = [x[1] for x in G2.degree]
probs2 = np.array(probs2 / np.sum(probs2)).reshape((len(probs2), 1))
probs = [probs1, probs2]

adj1 = nx.adjacency_matrix(G1)
adj2 = nx.adjacency_matrix(G2)

data['costs'] = [adj1, adj2]

data['probs'] = probs

idx2nodes1 = dict(zip(range(len(G1.nodes)), sorted(G1.nodes)))
idx2nodes2 = dict(zip(range(len(G2.nodes)), sorted(G2.nodes)))
idx2nodes = [idx2nodes1, idx2nodes2]
data['idx2nodes'] = idx2nodes

import methods.EvaluationMeasure as Eval
import methods.GromovWassersteinGraphToolkit as GwGt
import pickle
import time
import warnings

warnings.filterwarnings("ignore")

database = data

num_iter = 2000
ot_dict = {'loss_type': 'L2',  # the key hyperparameters of GW distance
           'ot_method': 'proximal',
           'beta': 0.025,
           'outer_iteration': num_iter,
           # outer, inner iteration, error bound of optimal transport
           'iter_bound': 1e-30,
           'inner_iteration': 2,
           'sk_bound': 1e-30,
           'node_prior': 1e3,
           'max_iter': 4,  # iteration and error bound for calcuating barycenter
           'cost_bound': 1e-26,
           'update_p': False,  # optional updates of source distribution
           'lr': 0,
           'alpha': 0}

for i in range(1):
    cost_s = database['costs'][0]
    cost_t = database['costs'][i+1]
    p_s = database['probs'][0]
    p_t = database['probs'][i+1]
    idx2node_s = database['idx2nodes'][0]
    idx2node_t = database['idx2nodes'][i+1]
    num_nodes = min([len(idx2node_s), len(idx2node_t)])

    time_s = time.time()
    ot_dict['outer_iteration'] = num_iter
    pairs_idx, pairs_name, pairs_confidence, soft_X = GwGt.recursive_direct_graph_matching(
        0.5 * (cost_s + cost_s.T), 0.5 * (cost_t + cost_t.T), p_s, p_t, idx2node_s, idx2node_t, ot_dict,
        weights=None, predefine_barycenter=False, cluster_num=2,
        partition_level=3, max_node_num=0)
    runtime = time.time() - time_s
    nc = Eval.calculate_node_correctness(pairs_name, num_correspondence=num_nodes)
    print(pairs_idx)
    print(pairs_name)
    print('soft_X', soft_X)

    print('method: s-gwl, duration {:.4f}s, nc {:.4f}.'.format(runtime, nc))
    with open('results/sgwl_ppi_syn_{}.pkl'.format(i + 1), 'wb') as f:
        pickle.dump([nc, runtime], f)


import numpy as np

class mwis:
    def __init__(self, txt_name):
        self.txt_name = txt_name

    def read_file(self):
        with open(self.txt_name) as f:
            f_lis = f.readlines()
        self.v_num = int(f_lis[0])
        self.weight = [int(f_lis[i]) for i in range(1,self.v_num+1)]

    def solve(self):
        self.A = np.zeros((self.v_num + 1,))
        self.A[0] = 0
        self.A[1] = self.weight[0]
        for i in range(2, self.v_num + 1):
            self.A[i] = max(self.A[i - 1], self.A[i - 2] + self.weight[i-1])

    def solve_vertices(self):
        self.S = np.zeros((self.v_num,))
        i = self.v_num
        while i >= 1:
            if self.A[i] > self.A[i - 1]:
                self.S[i - 1] = 1
                i = i - 2
            else:
                i = i - 1

solver = mwis('mwis.txt')
solver.read_file()
solver.solve()
solver.solve_vertices()
print(solver.S)
print(solver.S[[0, 1, 2, 3, 16, 116, 516, 996]])


num_int = int(input("Input a number: "))    # Do not change this line

#Assign max_int as 0
#While num_int is greater or equal to 0, the loop that follows runs
    #num_int is assigned as the numer the user inputs
    #if max_int smaller than num_int
        #max_int will be assigned the value of num_int

max_int = 0

while num_int >= 0:
    num_int = int(input("Input another number: "))
    if max_int < num_int:
        max_int = num_int

print("The maximum is", max_int)    # Do not change this line


import os
import glob
import dlib
import cv2
import numpy as np

detectorFace = dlib.get_frontal_face_detector()
detectorPontos = dlib.shape_predictor("recursos/shape_predictor_68_face_landmarks.dat")
reconhecimentoFacial = dlib.face_recognition_model_v1("recursos/dlib_face_recognition_resnet_model_v1.dat")
indices = np.load("recursos/indices_luenina.pickle")
descritoresFaciais = np.load("recursos/descritores_luenina.npy")
limiar = 0.5

for arquivo in glob.glob(os.path.join("luenina/teste", "*.jpg")):
    imagem = cv2.imread(arquivo)
    facesDetectadas = detectorFace(imagem)
    for face in facesDetectadas:
        e, t, d, b = (int(face.left()), int(face.top()), int(face.right()), int(face.bottom()))
        pontosFaciais = detectorPontos(imagem, face)
        descritorFacial = reconhecimentoFacial.compute_face_descriptor(imagem, pontosFaciais)
        listaDescritorFacial = [fd for fd in descritorFacial]
        npArrayDescritorFacial = np.asarray(listaDescritorFacial, dtype=np.float64)
        npArrayDescritorFacial = npArrayDescritorFacial[np.newaxis, :]

        distancias = np.linalg.norm(npArrayDescritorFacial - descritoresFaciais, axis=1)
        minimo = np.argmin(distancias)
        distanciaMinima = distancias[minimo]

        if distanciaMinima <= limiar:
            if os.path.split(indices[minimo])[1].split(".")[0].split("0")[0] == "lu":
                nome = "Luiza"
            else:
                nome = "Maria"
        else:
            nome = ' '

        cv2.rectangle(imagem, (e, t), (d, b), (0, 255, 0), 3)
        texto = "{}".format(nome)
        cv2.putText(imagem, texto, (d, t), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0))

    cv2.imshow("Detector Lu e Nina", imagem)
    cv2.waitKey(0)

cv2.destroyAllWindows()


import panel as pn

def category_selection(overview):
    overview_only_cats = overview[overview.data_type == "Ordinal"]

    cat_selector = \
        pn.widgets.Select(name="Category",
                          options=list(overview_only_cats.name),
                          value='')

    return cat_selector


#%%
import pickle
import os
import numpy as np
import pandas as pd
from datetime import datetime

#%%
def open_pickle(path):
    with open(path, "rb") as f:
        file = pickle.load(f)
    return file

matchInfoList = [
    "gameId",
    "gameCreation",
    "gameDuration",
    "gameVersion",
]

matchPUUID = ["puuid"]

playerStatList = [
    "summonerName",
    "championId",
    "teamId",
    "win",
    "kills",
    "deaths",
    "assists",
    "goldEarned",
    "champLevel",
    "item0",
    "item1",
    "item2",
    "item3",
    "item4",
    "item5",
    "item6",
    "totalMinionsKilled",
    # TANK STATS
    "totalDamageTaken",
    "magicDamageTaken",
    "physicalDamageTaken",
    "trueDamageTaken",
    # DMG STATS
    "totalDamageDealtToChampions",
    "magicDamageDealtToChampions",
    "physicalDamageDealtToChampions",
    "trueDamageDealtToChampions",
    # OTHER COMBAT STATS
    "timeCCingOthers",
    "totalHeal",
]

def pull_raw_values_per_match(match):
    matchStats = None
    matchStats = []
    for playerStat, puuid in zip(
        match["info"]["participants"], match["metadata"]["participants"]
    ):
        playerRow = []

        for matchInfo in matchInfoList:
            playerRow.append(match["info"][matchInfo])

        playerRow.append(puuid)

        for playerStatInfo in playerStatList:
            playerRow.append(playerStat[playerStatInfo])

        matchStats.append(playerRow)

    return matchStats

# match = open_pickle("data/raw/NA1_3982842863.pkl")
# test = pull_raw_values_per_match(match)
# test

#%%
def iterate_matches_pull_raw_data():
    fullStats = []
    count = 0
    allMatchStats = []
    for subdir, dirs, files in os.walk("data/raw"):
        for file in files:
            match = open_pickle(os.path.join(subdir, file))
            matchStats = pull_raw_values_per_match(match)
            allMatchStats.extend(matchStats)

            count += 1
            if count % 200 == 0:
                print(count, ": ", datetime.now())

    fullColumns = matchInfoList + matchPUUID + playerStatList
    fullStats_df = pd.DataFrame(allMatchStats, columns=fullColumns)
    fullStats_dfdd = fullStats_df.drop_duplicates()

    fullStats_dfdd.to_csv("data/interim/rawFlatStats.csv", index=False)

    return fullStats_df

# %%
fullStats_df = iterate_matches_pull_raw_data()
# %%


from torch.nn import Module

from horch.models.layers import Conv2d, Act, Identity, GlobalAvgPool, Linear, Sequential

class BasicBlock(Module):
    expansion = 1

    def __init__(self, in_channels, channels, stride, erase_relu):
        super().__init__()
        out_channels = channels * self.expansion
        self.conv1 = Conv2d(in_channels, out_channels, kernel_size=3, stride=stride,
                            norm='def', act='def')
        self.conv2 = Conv2d(out_channels, out_channels, kernel_size=3,
                            norm='def')

        if stride != 1 or in_channels != out_channels:
            self.shortcut = Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, norm='def')
        else:
            self.shortcut = Identity()
        self.act = Act() if not erase_relu else Identity()

    def forward(self, x):
        identity = self.shortcut(x)
        x = self.conv1(x)
        x = self.conv2(x)
        x = x + identity
        x = self.act(x)
        return x

class Bottleneck(Module):
    expansion = 4

    def __init__(self, in_channels, channels, stride, erase_relu):
        super().__init__()
        out_channels = channels * self.expansion
        self.conv1 = Conv2d(in_channels, channels, kernel_size=1,
                            norm='def', act='def')
        self.conv2 = Conv2d(channels, channels, kernel_size=3, stride=stride,
                            norm='def', act='def')
        self.conv3 = Conv2d(channels, out_channels, kernel_size=1,
                            norm='def')
        if stride != 1 or in_channels != out_channels:
            self.shortcut = Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, norm='def')
        else:
            self.shortcut = Identity()
        self.act = Act() if not erase_relu else Identity()

    def forward(self, x):
        identity = self.shortcut(x)
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.conv3(x)
        x = x + identity
        x = self.act(x)
        return x

class ResNet(Module):

    def __init__(self, depth, block='basic', erase_relu=False, num_classes=10, stages=(16, 16, 32, 64)):
        super().__init__()
        self.stages = stages
        if block == 'basic':
            block = BasicBlock
            layers = [(depth - 2) // 6] * 3
        else:
            block = Bottleneck
            layers = [(depth - 2) // 9] * 3

        self.stem = Conv2d(3, self.stages[0], kernel_size=3, norm='def', act='def')
        self.in_channels = self.stages[0]

        self.layer1 = self._make_layer(
            block, self.stages[1], layers[0], stride=1,
            erase_relu=erase_relu)
        self.layer2 = self._make_layer(
            block, self.stages[2], layers[1], stride=2,
            erase_relu=erase_relu)
        self.layer3 = self._make_layer(
            block, self.stages[3], layers[2], stride=2,
            erase_relu=erase_relu)

        self.avgpool = GlobalAvgPool()
        self.fc = Linear(self.in_channels, num_classes)

    def _make_layer(self, block, channels, blocks, stride=1, **kwargs):
        layers = [block(self.in_channels, channels, stride=stride,
                        **kwargs)]
        self.in_channels = channels * block.expansion
        for i in range(1, blocks):
            layers.append(block(self.in_channels, channels, stride=1,
                                **kwargs))
        return Sequential(layers)

    def forward(self, x):
        x = self.stem(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)

        x = self.avgpool(x)
        x = self.fc(x)
        return x


#使用celery
from celery import Celery
from django.conf import settings
from django.core.mail import send_mail
from django.template import loader
import time

# 初始化环境，在任务处理者那一端加
import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dayfreshnew.settings')
django.setup()
#创建一个celery的对象,并命名，指定broker
app = Celery('celery_tasks.tasks',broker='redis://192.168.116.128:6379/8')

@app.task
def send_register_active_email(to_email, username, token):
    """发送激活邮件"""
    # 发送邮件
    subject = '天天生鲜激活信息'
    message = ''
    sender = settings.EMAIL_FROM
    receiver = [to_email]
    html_message = '
%s, 欢迎您成为天天生鲜注册会员
请点击下面链接激活您的账户
http://127.0.0.1:8000/user/active/%s' % (
        username, token, token)

    try:
        send_mail(subject,
                  message,
                  sender,
                  receiver,
                  html_message=html_message, fail_silently=False)
    except Exception as e:
        print(e)

# 类的导入写在celery配置完成的下方
from goods.models import GoodsType, IndexGoodsBanner, IndexPromotionBanner, IndexTypeGoodsBanner

@app.task
def generate_static_index_html():
    """产生首页静态化页面"""

    # 获取商品的种类信息
    types = GoodsType.objects.all()

    # 获取轮播图信息
    banners = IndexGoodsBanner.objects.all().order_by('index')

    # 获取促销信息
    promotion_banners = IndexPromotionBanner.objects.all().order_by('index')

    # 获取首页分类商品展示信息
    for type in types:
        image_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=1).order_by('index')
        title_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=1).order_by('index')

        type.image_banners = image_banners
        type.title_banners = title_banners

    context = {
        'types': types,
        'goods_banners': banners,
        'promotion_banners': promotion_banners,
    }

    # 产生静态界面
    temp = loader.get_template('staticindex.html')
    static_index_html = temp.render(context)

    save_path = os.path.join(settings.BASE_DIR, 'static/index.html')

    with open(save_path, 'w') as f:
        f.write(static_index_html)


import random

import numpy as np

class NetWork:
    def __init__(self, sizes):
        # 网络层数
        self.num_layers = len(sizes)
        # 每层神经元的个数
        self.sizes = sizes

        self.biases = None
        self.weights = None
        self.default_weight_initializer()

    def default_weight_initializer(self):
        # 初始化每层的偏置
        self.biases = [np.random.randn(y, 1) for y in self.sizes[1:]]
        # 初始化每层的权重
        self.weights = [np.random.randn(y, x) / np.sqrt(x)
                        for x, y in zip(self.sizes[:-1], self.sizes[1:])]

    def large_weight_initializer(self):
        self.biases = [np.random.randn(y, 1) for y in self.sizes[1:]]
        self.weights = [np.random.randn(y, x)
                        for x, y in zip(self.sizes[:-1], self.sizes[1:])]

    def feed_forward(self, a):
        for b, w in zip(self.biases, self.weights):
            a = sigmoid(np.dot(w, a) + b)
        return a

    def gd(self, training_data, epochs, eta):
        """
        梯度下降
        :param eta:
        :param training_data: 训练数据集
        :param epochs: 需要训练的元素
        :return:
        """
        # 开始训练，循环每一个epochs
        for j in range(epochs):
            # 洗牌，打乱训练数据
            random.shuffle(training_data)

            # 保存每一层的偏导数
            nable_b = [np.zeros(b.shape) for b in self.biases]
            nable_w = [np.zeros(w.shape) for w in self.weights]

            # 训练每一个数据
            for x, y in training_data:
                delta_nable_b, delta_nable_w = self.back_prop(x, y)
                # 保存一次训练网络中每层的偏导数
                nable_b = [nb + dnb for nb, dnb in zip(nable_b, delta_nable_b)]
                nable_w = [nw + dnw for nw, dnw in zip(nable_w, delta_nable_b)]

            # 更新权重和偏置 w 下标n+1 = w 下标n - eta * nw
            self.weights = [w - eta * nw for w, nw in zip(self.weights, nable_w)]
            self.biases = [b - eta * nb for b, nb in zip(self.biases, nable_b)]
            print("Epoch %d complete" % j)

    def sgd(self, training_data, epochs, mini_batch_size, eta, test_data=None):
        """
        随机梯度下降
        :param test_data:
        :param training_data:
        :param epochs:
        :param mini_batch_size:
        :param eta:
        :return:
        """
        n_test = 0
        training_data = list(training_data)
        # 训练数据的总个数
        n = len(training_data)

        if test_data:
            test_data = list(test_data)
            n_test = len(test_data)

        # 开始训练 循环每一个epochs
        for j in range(epochs):
            # 洗牌 打乱训练数据
            random.shuffle(training_data)
            # mini_batch
            mini_batches = [
                training_data[k: k + mini_batch_size]
                for k in range(0, n, mini_batch_size)]
            # 训练 mini_batch
            for mini_batch in mini_batches:
                self.update_mini_batch(mini_batch, eta)

            if test_data:
                print("Epoch {} : {} / {}".format(j, self.evaluate(test_data), n_test))
            else:
                print("Epoch {} complete".format(j))

    def update_mini_batch(self, mini_batch, eta):
        """
        更新 mini_batch
        :param mini_batch:
        :param eta:
        :return:
        """
        # 保存每一层的偏导数
        nabla_b = [np.zeros(b.shape) for b in self.biases]
        nabla_w = [np.zeros(w.shape) for w in self.weights]

        # 训练每一个 mini_batch
        for x, y in mini_batch:
            delta_nable_b, delta_nable_w = self.back_prop(x, y)

            # 保存一次
            nabla_b = [nb + dnb for nb, dnb in zip(nabla_b, delta_nable_b)]
            nabla_w = [nw + dnw for nw, dnw in zip(nabla_w, delta_nable_b)]
        self.weights = [w - (eta / len(mini_batch)) * nw for w, nw in zip(self.weights, nabla_w)]
        self.biases = [b - (eta / len(mini_batch)) * nb for b, nb in zip(self.biases, nabla_b)]

    def back_prop(self, x, y):
        """
        反向传播
        :param x:
        :param y:
        :return:
        """
        # 保存每一层的偏导数
        nabla_b = [np.zeros(b.shape) for b in self.biases]
        nabla_w = [np.zeros(w.shape) for w in self.weights]

        # 前向过程
        activation = x
        # 保存每一层的激励值 a = sigmoid(z)
        activations = [x]
        # 保存每一层的 z = wx + b
        zs = []
        # 前向传播
        for b, w in zip(self.biases, self.weights):
            # 计算每一层的 z
            z = np.dot(w, activation) + b
            # 保存每一层的 z
            zs.append(z)
            # 计算每一层的激励值 a
            activation = sigmoid(z)
            # 保存每一层的 a
            activations.append(activation)

        # 反向过程
        # 计算最后一层的误差
        delta = cost_derivative(activations[-1], y) * sigmoid_prime(zs[-1])
        # 最后一层的权重和偏置的导数
        nabla_b[-1] = delta
        nabla_w[-1] = np.dot(delta, activations[-2].transpose())
        # 倒数第二层直至第一层的权重和偏置的导数
        for layer in range(2, self.num_layers):
            z = zs[-layer]
            sp = sigmoid_prime(z)
            # 当前层的误差
            delta = np.dot(self.weights[-layer + 1].transpose(), delta) * sp
            # 当前层偏置和权重的导数
            nabla_b[-layer] = delta
            nabla_w[-layer] = np.dot(delta, activations[-layer - 1].transpose())

        return nabla_b, nabla_w

    def evaluate(self, test_data):
        test_results = [(np.argmax(self.feed_forward(x)), y) for (x, y) in test_data]
        return sum(int(x == y) for (x, y) in test_results)

# 定义神经网络结构
def cost_derivative(out_activation, y):
    return out_activation - y

def sigmoid(z):
    """
    激励函数
    :param z:
    :return:
    """
    return 1.0 / (1.0 + np.exp(-z))

def sigmoid_prime(z):
    """
    激励函数的导数
    :param z:
    :return:
    """
    return sigmoid(z) * (1 - sigmoid(z))


import copy
import itertools
import math
import os.path
import pdb
import random
import sys

import scipy.stats

def safety_log(x):
    try:
        return math.log(x)
    except ValueError:
        return -sys.maxsize

class Clusterer:

    def __init__(self, matrices):
        # Core data structures
        self.matrices = matrices
        self.partitions = []

        # Caching stuff
        self.dirty_theta = True
        self.dirty_parts = []
        self.crp_likelihood = 0.0
        self.likelihoods = []

        # Model params
        self.theta = 1.00
        self.within_mu = 0.25
        self.within_sigma = 0.1
        self.between_mu = 0.75
        self.between_sigma = 0.1

        # Controls
        self.verbose = True
        self.change_partition = True
        self.change_params = True

    def init_partitions(self):
        """Construct initial partitions for all matrices.  This is not done
        randomly, but rather we try to start things off in a state which is
        likely to be not too terrible."""

        for matrix in self.matrices:
            # Start off by sticking 0 in it's own class
            part = [[0]]
            # Then, for everything else...
            for i in range(1,len(matrix)):
                assigned = False
                for bit in part:
                    # Put it in a class if it's close to the first member of that class
                    if matrix[i][bit[0]] <= 0.35:
                        bit.append(i)
                        assigned = True
                        break
                # If this item didn't get put in an existing class, put it in a new one
                if not assigned:
                    part.append([i])

            # Make sure there are no duplicates or missing values
            assert sum([len(bit) for bit in part]) == len(matrix)

            self.partitions.append(part)
            self.dirty_parts.append(True)
            self.likelihoods.append(0)

    def find_MAP(self, iterations=1000):
        """Attempt to find the partition and parameter values which
        maximimise the posterior probability of the data.  Runs for the
        specified number of iterations, or terminates after 100 consecutive
        failures to find an improved posterior."""

        self.poster = self.compute_posterior()
        self.failed_attempts = 0
        if self.verbose:
            print("\t".join("Prior Lh Poster Theta W_mu W_sigma B_mu B_sigma".split()))
        for i in range(0,iterations):
            self.snapshot()
            self.dirty_theta = False
            self.dirty_parts = [False for part in self.partitions]
            self.draw_proposal()
            new_poster = self.compute_posterior()
            if self.prior and new_poster > self.poster:
                # Accept
                self.poster = new_poster
                self.failed_attempts = 0
                if self.verbose:
                    self.instrument()
            else:
                # Reject
                self.revert()
                self.failed_attempts +=1
                if self.failed_attempts == 100:
                    break

    def compute_posterior(self):
        return self.compute_prior() + self.compute_lh()

    def compute_prior(self):
        """Compute log prior on model parameters."""

        prior = 0

        # Prior on theta
        # A fairly arbitrary Gamma prior which is basically chosen
        # to trade off between gernally preferring lower theta over higher
        # theta, but not wanting *too* low of a theta.
        p = scipy.stats.gamma.pdf(self.theta, 4.0, loc=0.0, scale=1/2.0)
        prior += safety_log(p)

        # Prior on within_mu
        # (Beta distribution prior)
        dist = scipy.stats.beta(2, 5)
        prior += safety_log(dist.pdf(self.within_mu))

        # Prior on within_sigma
        # (exponential prior)
        prior += safety_log(0.01*math.exp(-1*0.01*self.within_sigma))

        # Prior on between_mu
        # (Beta distribution prior)
        dist = scipy.stats.beta(5, 2)
        prior += safety_log(dist.pdf(self.between_mu))

        # Prior on between_sigma
        # (exponential prior)
        prior += safety_log(0.01*math.exp(-1*0.01*self.between_sigma))
        self.prior = prior
        return prior

    def compute_lh(self):
        """Compute log likelihood of partition (under CRP process) and
        distance matrices (under sampling from two distributions according
        to the partition)."""

        lh = self.get_partition_lh()
        lh += self.get_matrix_lh()
        self.lh = lh
        return lh

    def get_partition_lh(self):
        """Compute the probability of the current partition according to the
        current CRP model parameters."""
        if not self.dirty_theta:
            return self.crp_likelihood
        lh = 0
        for matrix, part in zip(self.matrices, self.partitions):
            lh += safety_log( math.gamma(self.theta)*self.theta**len(part) / math.gamma(self.theta + len(matrix)) )
            for bit in part:
                lh += safety_log(math.gamma(len(bit)))
        self.crp_likelihood = lh
        return lh

    def get_matrix_lh(self):
        """Compute the probability of the data matrix according to the
        current partition and distance distribution parameters."""
        for i, (part, dirty, matrix) in enumerate(zip(self.partitions, self.dirty_parts, self.matrices)):
            if not dirty:
                continue
            within = scipy.stats.norm(loc=self.within_mu,scale=self.within_sigma)
            between = scipy.stats.norm(loc=self.between_mu,scale=self.between_sigma)
            lh = 0
            for x,y in itertools.combinations(range(0,len(part)),2):
                if any([x in bit and y in bit for bit in part]):
                    lh += safety_log(within.pdf(matrix[x][y]))
                else:
                    lh += safety_log(between.pdf(matrix[x][y]))
            self.likelihoods[i] = lh
            continue

            # In principle, we want to accurately marginalise over all points
            # possibly being the center of their subset.  To speed things up,
            # we do approximate inference by drawing a uniformly random
            # centre for each subset, repeating this 10 times and using the
            # mean likelihood over these samples.
            # TODO: assess how good an idea this is!
            for i in range(0,10):
                partition_lh = 1
                centres = [random.sample(cluster,1)[0] for cluster in part]
                between_dists = [matrix[x][y] for x,y in itertools.combinations(centres,2)]
                between_probs = between.pdf(between_dists)
                for p in between_probs:
                    partition_lh *= p
                for centre, cluster in zip(centres, part):
                    distances = []
                    for x in cluster:
                        if x == centre:
                            continue
                        distances.append(matrix[centre][x])
                    probs = within.pdf(distances)
                    for prob in probs:
                        partition_lh *= prob
                mean_lh += partition_lh
            if mean_lh == 0.0 or mean_lh / 10.0 == 0.0:
                self.likelihoods[i] = -sys.maxsize
            else:
                self.likelihoods[i] = math.log(mean_lh / 10.0)
        return sum(self.likelihoods)

    def snapshot(self):
        """Backup everything which is modified by drawing a proposal."""
        self.snapped_partitions = copy.deepcopy(self.partitions)
        self.snapped_crp_likelihood = self.crp_likelihood
        self.snapped_likelihoods = self.likelihoods[:]
        self.snapped_theta = self.theta
        self.snapped_within_mu = self.within_mu
        self.snapped_within_sigma = self.within_sigma
        self.snapped_between_mu = self.between_mu
        self.snapped_between_sigma = self.between_sigma

    def revert(self):
        """Restore from backup everything which is modified by drawing a
        proposal."""
        self.partitions = self.snapped_partitions
        self.crp_likelihood = self.snapped_crp_likelihood
        self.likelihoods = self.snapped_likelihoods
        self.theta = self.snapped_theta
        self.within_mu = self.snapped_within_mu
        self.within_sigma = self.snapped_within_sigma
        self.between_mu = self.snapped_between_mu
        self.between_sigma = self.snapped_between_sigma

    def draw_proposal(self):
        """Make a random change to the state space."""
        roll = random.random()
        if self.change_params and 0 <= roll < 0.50:
            # Half the time, change the parameters
            self.move_change_params()
        else:
            # The other half, change the partition
            self.move_change_partition()

    def move_change_params(self):
        """Choose one of the model parameters at random and multiply it by a
        Normally distributed random scale."""
        # Choose a scaling value
        roll = random.randint(1,4)
        if roll == 1:
            mult = random.normalvariate(1.0,0.05)
        elif roll == 2:
            mult = random.normalvariate(1.0,0.10)
        elif roll == 3:
            mult = random.normalvariate(1.0,0.20)
        else:
            mult = random.normalvariate(1.0,0.30)

        # Choose a parameter and scale it
        roll = random.random()
        if roll < 0.20:
            self.theta *= mult
            self.dirty_theta = True
            # Return now so that dirty_parts is not touched
            return
        elif 0.20 <= roll < 0.40:
            self.within_mu *= mult
        elif 0.40 <= roll < 0.60:
            self.within_sigma *= mult
        elif 0.60 <= roll < 0.80:
            self.between_mu *= mult
        else:
            self.between_sigma *= mult
        self.dirty_parts = [True for part in self.partitions]

    def move_change_partition(self):
        """Sample one of the partition changing moves at random and apply
        it."""

        moved = False
        while not moved:
            index = random.randint(0,len(self.partitions)-1)
            part = self.partitions[index]
            operator = random.sample(
                    (   self.move_merge,
                        self.move_split,
                        self.move_reassign,
                        self.move_swap,
                        self.move_shuffle,
                        self.move_smart),
                    1)[0]
            if operator == self.move_smart:
                matrix = self.matrices[index]
                moved = operator(part, matrix)
            else:
                moved = operator(part)
        self.dirty_parts[self.partitions.index(part)] = True

    def move_merge(self, part):
        """Choose two sets of the partition at random and merge them."""
        random.shuffle(part)
        if len(part) == 1:
            # If the partition is just one big set there's nothing to merge!
            return False
        newset = []
        newset.extend(part.pop())
        newset.extend(part.pop())
        part.append(newset)
        self.dirty_theta = True
        return True

    def move_split(self, part):
        """Choose a set of the partition at random and split it in two."""
        if all([len(bit) == 1 for bit in part]):
            # If all sets of the partition are singletons there's nothing to split!
            return False
        random.shuffle(part)
        partbit = part.pop()
        while len(partbit) == 1:
            part.append(partbit)
            random.shuffle(part)
            partbit = part.pop()
        if len(partbit) == 2:
            part.append([partbit[0],])
            part.append([partbit[1],])
        else:
            random.shuffle(partbit)
            pivot = random.randint(1,len(partbit)-2)
            part.append(partbit[0:pivot])
            part.append(partbit[pivot:])
        self.dirty_theta = True
        return True

    def move_reassign(self, part):
        """Choose a random element of a random set and move it to a new
        random set."""
        if len(part) == 1:
            # If the partition is just one big set then we can't do anything!
            return False
        bit_a, bit_b = random.sample(part,2)
        bit_b.append(bit_a.pop())
        if not bit_a:
            part.remove(bit_a)
        self.dirty_theta = True
        return True

    def move_swap(self, part):
        """Choose two random sets and swap a random element of one with a
        random element of the other."""
        if len(part) == 1:
            # We need at least two partitions
            return False
        bit_a, bit_b = random.sample(part,2)
        random.shuffle(bit_a)
        x_a = bit_a.pop()
        random.shuffle(bit_b)
        x_b = bit_b.pop()
        bit_a.append(x_b)
        bit_b.append(x_a)
        return True

    def move_shuffle(self, part):
        """Randomly shuffle elements among the sets of the partition, while
        keeping the number and sizes of partitions constant."""
        n = sum([len(bit) for bit in part])
        words = list(range(0,n))
        random.shuffle(words)
        new_part = []
        while part:
            bit = part.pop()
            new_bit = []
            for i in bit:
                new_bit.append(words.pop())
            new_part.append(new_bit)
        part.extend(new_part)
        self.dirty_theta = True
        return True

    def move_smart(self, part, mat):
        """For MAP searches: attempt a very smart move, which uses the
        distance matrices to make optimal choices."""
        if len(part) == 1:
            # Given only a single grouping, find the word
            # with the greatest mean distance to other words in the group
            # and remove it to form its own group
            bit = part[0]
            mean_dists = [sum([mat[i][j] for j in bit if i!= j])/(len(bit)-1) for i in bit]
            max_dist = max(mean_dists)
            max_index = mean_dists.index(max_dist)
            mover = bit[max_index]
            bit.remove(mover)
            part.append([mover])
        else:
            # Given multiple groupings, pick a random word from a random group
            # and put it in the group which minimises its mean distance to
            # other words in the group
            random.shuffle(part)
            partbit = part.pop()
            random.shuffle(partbit)
            mover = partbit.pop()
            mean_dists = [sum([mat[mover][j] for j in bit])/len(bit) for bit in part]
            min_dist = min(mean_dists)
            min_index = mean_dists.index(min_dist)
            part[min_index].append(mover)
            if partbit:
                part.append(partbit)
        self.dirty_theta = True
        return True

    def instrument(self):
        print("\t".join(["%.2f" % x for x in (self.prior, self.lh, self.poster, self.theta, self.within_mu, self.within_sigma, self.between_mu, self.between_sigma)]))


'''

'''

import os
from PyPDF2 import PdfFileReader, PdfFileMerger

files_dir = ""  #indicar o caminho dos arquivos
pdf_files = [f for f in os.listdir(files_dir) if f.endswith("pdf")]
merger = PdfFileMerger()

print(os.listdir(files_dir))

for filename in pdf_files:
    merger.append(PdfFileReader(os.path.join(files_dir, filename), "rb"))

merger.write(os.path.join(files_dir, "pdf_unido.pdf"))


import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import sys
import numpy as np

database = {}

timesFont = {'fontname': 'Times New Roman'}

def seconds(time):
	# print(time)
	t = time.split(':')
	t = list(map(lambda x: int(x), t))
	return t[0]*60*60 + t[1]*60 + t[2]

def data(filenames):
	for file in filenames:
		with open(file, 'r')as f:
			lines = f.readlines()

			time = []
			pss = []
			seperation = []
			for x in range(1,len(lines)):
				splitline = lines[x].split(',')
				# if file == 'gmail_go_app1.csv':
				# 	print(splitline)

				if(splitline[0] == ''):
					continue

				if(splitline[2] != '\n'):
					seperation.append(seconds(splitline[0]))

				time.append(seconds(splitline[0]))
				pss.append(int(float(splitline[1])))

			# if file == 'gmail_go_app1.csv':
			# 	print(seperation)
			name = ''
			if file == 'basic_gmail_browser.csv':
				name = 'Basic Gmail on Browser'
			elif file == 'standard_gmail_browser.csv':
				name = 'Standard Gmail on Browser'
			elif file == 'latest_gmail_app.csv':
				name = 'Gmail App'
			elif file == 'gmail_go_app.csv':
				name = 'Gmail Go App'
			elif file == 'older_gmail_app':
				name = 'what ever verion'

			database[file] = {'time': time, 'pss': pss, 'seperate': seperation, 'name':name}

def normalize(file):
	seperate = database[file]['seperate'][0]
	index = 0
	sTime = database[file]['time'][0]
	while(seperate > sTime):
		index +=1
		sTime = database[file]['time'][index]

	startingTime = database[file]['time'][0]
	database[file]['startingTime'] = startingTime

	database[file]['seperate'] = list(map(lambda x: x - startingTime, database[file]['seperate']))
	ans = list(map(lambda x: x-startingTime, database[file]['time']))
	return ans

def equal(standard):
	for each in database:
		cut = database[each]['seperate'][0] - standard
		database[each]['x'] = list(map(lambda x: x - cut , database[each]['x']))

		index = 0
		while(database[each]['x'][index] < 0):
			index += 1

		database[each]['x'] = database[each]['x'][index:]
		database[each]['y'] = database[each]['y'][index:]
		# database[each]['x'] = database[each]['x'][cut:]
		# start = database[each]['x'][0]
		# database[each]['x'] = list(map(lambda x: x - start, database[each]['x']))
		# database[each]['y'] = database[each]['y'][cut:]

def graph():
	for each in database:
		x = normalize(each)
		database[each]['x'] = x
		y = database[each]['pss']
		database[each]['y'] = y

	#pick one and draw the rest accordingly can't normalize each
	#one idea is to just pick the first. standardize it and then get the rest and make sure their

	standarda = 500
	standardb = 500
	for each in database:
		print(database[each]['seperate'][0])
		if(database[each]['seperate'][0] < standarda):
			standarda = database[each]['seperate'][0]
			standardb = database[each]['seperate'][1]

	equal(standarda - 1)

	for each in database:
		if each == 'gmail_go_app1.csv':
			print(database[each]['y'])
		x = database[each]['x']
		y = database[each]['y']

		plt.plot(x,y, label = database[each]['name'])

		# if(each == 'older_gmail_app.csv'):
		plt.axvline(x = 30, linestyle = '-', color = 'k', linewidth = 0.75)
		plt.axvline(x = 60, linestyle = '-', color = 'k', linewidth = 0.75)
		plt.xlabel('Time (sec)', fontsize = 16)
		plt.ylabel('PSS (MB)', fontsize = 16)
		plt.xlim([0,90])
		plt.ylim([0,140])
		plt.gca().xaxis.set_major_locator(ticker.MultipleLocator(30))
		#plt.title(' default page                        text email                                    image email               ')
		plt.text(15,130, 'Default view', horizontalalignment='center', verticalalignment='center', fontsize = 20, **timesFont)
		plt.text(45,130, 'Text email', horizontalalignment='center', verticalalignment='center', fontsize = 20, **timesFont)
		plt.text(75,130, 'Image email', horizontalalignment='center', verticalalignment='center', fontsize = 20, **timesFont)
		labelLineY = np.array([120 for i in range(95)])
		labelLinex = np.arange(95)
		plt.plot(labelLinex,labelLineY, color = 'k' , linewidth = 0.75)

		plt.legend(loc = 4, fontsize = 16)
		plt.gca().yaxis.grid(True)
		plt.gca().xaxis.set_tick_params(labelsize=16)
		plt.gca().yaxis.set_tick_params(labelsize=16)

		plt.tight_layout()

	plt.show()

def main():
	if(len(sys.argv) < 2):
		print("enter file names")
	else:
		data(sys.argv[1:])
		graph()

if __name__ == '__main__':
	main()


'''
Create a tuple with your first name, last name, and age. Create a list,
people, and append your tuple to it. Make more tuples with the
corresponding information from your friends and append them to the
list. Sort the list. When you learn about sort method, you can use the
key parameter to sort by any field in the tuple, first name, last name,
or age.
'''

me = ('Ayush', 'Acharya', 23)
people = []
people.append(me)
people.append(('Aashish', 'Acharya', 20))
people.append(('Ram', 'Dahal',30))
people.append(('John', 'Doe', 33))
people.append(('Bruno', 'Fernandes', 24))
print(people)
# Sort by age
people.sort(key=lambda x: x[2])
print("\nSorted by age:", people)
# Sort by first name
people.sort(key=lambda x : x[0])
print("\nSorted by First Name : ", people)
# Sort by last name
people.sort(key=lambda x : x[1])
print("\nSorted by Last Name : ", people)