from tkinter import *
import time
import tkinter.messagebox
#from bot import chat
#from chatgui import send
import pyttsx3
import threading
import nltk
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
import pickle
import numpy as np

from keras.models import load_model
model = load_model('chatbot_model.h5')
import json
import random
intents = json.loads(open('intents.json').read()) # loads converts json to python dictionary
words = pickle.load(open('words.pkl','rb'))
classes = pickle.load(open('classes.pkl','rb'))

saved_username = ["You"]
#ans=["jeff"]
window_size="400x400"

def clean_up_sentence(sentence):
    # tokenize the pattern - split words into array
    sentence_words = nltk.word_tokenize(sentence)
    # stem each word - create short form for word example rocks-->rock
    sentence_words = [lemmatizer.lemmatize(word.lower()) for word in sentence_words]
    return sentence_words

# return bag of words array: 0 or 1 for each word in the bag that exists in the sentence

def bow(sentence, words, show_details=True):
    # tokenize the pattern
    #Tokenizing is the process of breaking the whole text into small parts like words.
    sentence_words = clean_up_sentence(sentence)
    # bag of words - matrix of N words, vocabulary matrix
    bag = [0]*len(words)
    for s in sentence_words:
        for i,w in enumerate(words):
            if w == s:
                # assign 1 if current word is in the vocabulary position
                bag[i] = 1
                if show_details:
                    print ("found in bag: %s" % w)
    return(np.array(bag))

def predict_class(sentence, model):
    # filter out predictions below a threshold
    p = bow(sentence, words,show_details=False)
    res = model.predict(np.array([p]))[0]
    ERROR_THRESHOLD = 0.25
    results = [[i,r] for i,r in enumerate(res) if r>ERROR_THRESHOLD]
    # sort by strength of probability
    results.sort(key=lambda x: x[1], reverse=True)
    return_list = []
    for r in results:
        return_list.append({"intent": classes[r[0]], "probability": str(r[1])})
    return return_list

def getResponse(ints, intents_json):
    tag = ints[0]['intent']
    list_of_intents = intents_json['intents']
    for i in list_of_intents:
        if(i['tag']== tag):
            result = random.choice(i['responses'])
            break
    return result

def chatbot_response(msg):
    ints = predict_class(msg, model)
    res = getResponse(ints, intents)
    return res

#Creating GUI with tkinter
def send(msg):
    if msg != '':
        return chatbot_response(msg)
    else:
        return "Sorry, I am afraid I don't Understand."

class ChatInterface(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master = master

        # sets default bg for top level windows
        self.tl_bg = "#EEEEEE"
        self.tl_bg2 = "#EEEEEE"
        self.tl_fg = "#000000"
        self.font = "Verdana 10"

        menu = Menu(self.master)
        self.master.config(menu=menu, bd=5)
# Menu bar

    # File
        file = Menu(menu, tearoff=0)
        menu.add_cascade(label="File", menu=file)
       # file.add_command(label="Save Chat Log", command=self.save_chat)
        file.add_command(label="Clear Chat", command=self.clear_chat)
      #  file.add_separator()
        file.add_command(label="Exit",command=self.chatexit)

    # Options
        options = Menu(menu, tearoff=0)
        menu.add_cascade(label="Options", menu=options)

        # username

        # font
        font = Menu(options, tearoff=0)
        options.add_cascade(label="Font", menu=font)
        font.add_command(label="Default",command=self.font_change_default)
        font.add_command(label="Times",command=self.font_change_times)
        font.add_command(label="System",command=self.font_change_system)
        font.add_command(label="Helvetica",command=self.font_change_helvetica)
        font.add_command(label="Fixedsys",command=self.font_change_fixedsys)

        # color theme
        color_theme = Menu(options, tearoff=0)
        options.add_cascade(label="Color Theme", menu=color_theme)
        color_theme.add_command(label="Default",command=self.color_theme_default)
       # color_theme.add_command(label="Night",command=self.)
        color_theme.add_command(label="Grey",command=self.color_theme_grey)
        color_theme.add_command(label="Blue",command=self.color_theme_dark_blue)

        color_theme.add_command(label="Torque",command=self.color_theme_turquoise)
        color_theme.add_command(label="Hacker",command=self.color_theme_hacker)
       # color_theme.add_command(label='Mkbhd',command=self.MKBHD)

        help_option = Menu(menu, tearoff=0)
        menu.add_cascade(label="Help", menu=help_option)
        #help_option.add_command(label="Features", command=self.features_msg)
        help_option.add_command(label="About jeff", command=self.msg)
        help_option.add_command(label="Developers", command=self.about)

        self.text_frame = Frame(self.master, bd=6)
        self.text_frame.pack(expand=True, fill=BOTH)

        # scrollbar for text box
        self.text_box_scrollbar = Scrollbar(self.text_frame, bd=0)
        self.text_box_scrollbar.pack(fill=Y, side=RIGHT)

        # contains messages
        self.text_box = Text(self.text_frame, yscrollcommand=self.text_box_scrollbar.set, state=DISABLED,
                             bd=1, padx=6, pady=6, spacing3=8, wrap=WORD, bg=None, font="Verdana 10", relief=GROOVE,
                             width=10, height=1)
        self.text_box.pack(expand=True, fill=BOTH)
        self.text_box_scrollbar.config(command=self.text_box.yview)

        # frame containing user entry field
        self.entry_frame = Frame(self.master, bd=1)
        self.entry_frame.pack(side=LEFT, fill=BOTH, expand=True)

        # entry field
        self.entry_field = Entry(self.entry_frame, bd=1, justify=LEFT)
        self.entry_field.pack(fill=X, padx=6, pady=6, ipady=3)
        # self.users_message = self.entry_field.get()

        # frame containing send button and emoji button
        self.send_button_frame = Frame(self.master, bd=0)
        self.send_button_frame.pack(fill=BOTH)

        # send button
        self.send_button = Button(self.send_button_frame, text="Send", relief=GROOVE, bg='white',
                                  bd=1,width=5, command=lambda: self.send_message_insert(None), activebackground="#FFFFFF",
                                  activeforeground="#000000")
        self.send_button.pack(side=LEFT, ipady=8)
        self.master.bind("<Return>", self.send_message_insert)

        self.last_sent_label(date="No messages sent.")
        #t2 = threading.Thread(target=self.send_message_insert(, name='t1')
        #t2.start()

    def playResponce(self,responce):
        x=pyttsx3.init()
        #print(responce)
        li = []
        if len(responce) > 100:
            if responce.find('--') == -1:
                b = responce.split('--')
                #print(b)

        x.setProperty('rate',120)
        x.setProperty('volume',100)
        x.say(responce)
        x.runAndWait()
        #print("Played Successfully......")

    def last_sent_label(self, date):

        try:
            self.sent_label.destroy()
        except AttributeError:
            pass

        self.sent_label = Label(self.entry_frame, font="Verdana 7", text=date, bg=self.tl_bg2, fg=self.tl_fg)
        self.sent_label.pack(side=LEFT, fill=X, padx=3)

    def clear_chat(self):
        self.text_box.config(state=NORMAL)
        self.last_sent_label(date="No messages sent.")
        self.text_box.delete(1.0, END)
        self.text_box.delete(1.0, END)
        self.text_box.config(state=DISABLED)

    def chatexit(self):
        exit()

    def msg(self):
        tkinter.messagebox.showinfo("jeff v1.1",'jeff is a chatbot for Making a general conversation\nIt is based on retrival-based NLP using pythons NLTK tool-kit module\nGUI is based on Tkinter\nIt can answer questions regarding many things')

    def about(self):
        tkinter.messagebox.showinfo("jeff Developers","Affan Murtaza")

    def send_message_insert(self, message):
        user_input = self.entry_field.get()
        pr1 = "Human : " + user_input + "\n"
        self.text_box.configure(state=NORMAL)
        self.text_box.insert(END, pr1)
        self.text_box.configure(state=DISABLED)
        self.text_box.see(END)
        #t1 = threading.Thread(target=self.playResponce, args=(user_input,))
        #t1.start()
        #time.sleep(1)
        ob=send(user_input)
        pr="jeff : " + ob + "\n"
        self.text_box.configure(state=NORMAL)
        self.text_box.insert(END, pr)
        self.text_box.configure(state=DISABLED)
        self.text_box.see(END)
        self.last_sent_label(str(time.strftime( "Last message sent: " + '%B %d, %Y' + ' at ' + '%I:%M %p')))
        self.entry_field.delete(0,END)
        time.sleep(0)
        t2 = threading.Thread(target=self.playResponce, args=(ob,))
        t2.start()
        #return ob

    def font_change_default(self):
        self.text_box.config(font="Verdana 10")
        self.entry_field.config(font="Verdana 10")
        self.font = "Verdana 10"

    def font_change_times(self):
        self.text_box.config(font="Times")
        self.entry_field.config(font="Times")
        self.font = "Times"

    def font_change_system(self):
        self.text_box.config(font="System")
        self.entry_field.config(font="System")
        self.font = "System"

    def font_change_helvetica(self):
        self.text_box.config(font="helvetica 10")
        self.entry_field.config(font="helvetica 10")
        self.font = "helvetica 10"

    def font_change_fixedsys(self):
        self.text_box.config(font="fixedsys")
        self.entry_field.config(font="fixedsys")
        self.font = "fixedsys"

    def color_theme_default(self):
        self.master.config(bg="#EEEEEE")
        self.text_frame.config(bg="#EEEEEE")
        self.entry_frame.config(bg="#EEEEEE")
        self.text_box.config(bg="#FFFFFF", fg="#000000")
        self.entry_field.config(bg="#FFFFFF", fg="#000000", insertbackground="#000000")
        self.send_button_frame.config(bg="#EEEEEE")
        self.send_button.config(bg="#FFFFFF", fg="#000000", activebackground="#FFFFFF", activeforeground="#000000")
        #self.emoji_button.config(bg="#FFFFFF", fg="#000000", activebackground="#FFFFFF", activeforeground="#000000")
        self.sent_label.config(bg="#EEEEEE", fg="#000000")

        self.tl_bg = "#FFFFFF"
        self.tl_bg2 = "#EEEEEE"
        self.tl_fg = "#000000"

    # Dark
    def color_theme_dark(self):
        self.master.config(bg="#2a2b2d")
        self.text_frame.config(bg="#2a2b2d")
        self.text_box.config(bg="#212121", fg="#FFFFFF")
        self.entry_frame.config(bg="#2a2b2d")
        self.entry_field.config(bg="#212121", fg="#FFFFFF", insertbackground="#FFFFFF")
        self.send_button_frame.config(bg="#2a2b2d")
        self.send_button.config(bg="#212121", fg="#FFFFFF", activebackground="#212121", activeforeground="#FFFFFF")
       # self.emoji_button.config(bg="#212121", fg="#FFFFFF", activebackground="#212121", activeforeground="#FFFFFF")
        self.sent_label.config(bg="#2a2b2d", fg="#FFFFFF")

        self.tl_bg = "#212121"
        self.tl_bg2 = "#2a2b2d"
        self.tl_fg = "#FFFFFF"

    # Grey
    def color_theme_grey(self):
        self.master.config(bg="#444444")
        self.text_frame.config(bg="#444444")
        self.text_box.config(bg="#4f4f4f", fg="#ffffff")
        self.entry_frame.config(bg="#444444")
        self.entry_field.config(bg="#4f4f4f", fg="#ffffff", insertbackground="#ffffff")
        self.send_button_frame.config(bg="#444444")
        self.send_button.config(bg="#4f4f4f", fg="#ffffff", activebackground="#4f4f4f", activeforeground="#ffffff")
        #self.emoji_button.config(bg="#4f4f4f", fg="#ffffff", activebackground="#4f4f4f", activeforeground="#ffffff")
        self.sent_label.config(bg="#444444", fg="#ffffff")

        self.tl_bg = "#4f4f4f"
        self.tl_bg2 = "#444444"
        self.tl_fg = "#ffffff"

    def color_theme_turquoise(self):
        self.master.config(bg="#003333")
        self.text_frame.config(bg="#003333")
        self.text_box.config(bg="#669999", fg="#FFFFFF")
        self.entry_frame.config(bg="#003333")
        self.entry_field.config(bg="#669999", fg="#FFFFFF", insertbackground="#FFFFFF")
        self.send_button_frame.config(bg="#003333")
        self.send_button.config(bg="#669999", fg="#FFFFFF", activebackground="#669999", activeforeground="#FFFFFF")
        #self.emoji_button.config(bg="#669999", fg="#FFFFFF", activebackground="#669999", activeforeground="#FFFFFF")
        self.sent_label.config(bg="#003333", fg="#FFFFFF")

        self.tl_bg = "#669999"
        self.tl_bg2 = "#003333"
        self.tl_fg = "#FFFFFF"

    # Blue
    def color_theme_dark_blue(self):
        self.master.config(bg="#263b54")
        self.text_frame.config(bg="#263b54")
        self.text_box.config(bg="#1c2e44", fg="#FFFFFF")
        self.entry_frame.config(bg="#263b54")
        self.entry_field.config(bg="#1c2e44", fg="#FFFFFF", insertbackground="#FFFFFF")
        self.send_button_frame.config(bg="#263b54")
        self.send_button.config(bg="#1c2e44", fg="#FFFFFF", activebackground="#1c2e44", activeforeground="#FFFFFF")
        #self.emoji_button.config(bg="#1c2e44", fg="#FFFFFF", activebackground="#1c2e44", activeforeground="#FFFFFF")
        self.sent_label.config(bg="#263b54", fg="#FFFFFF")

        self.tl_bg = "#1c2e44"
        self.tl_bg2 = "#263b54"
        self.tl_fg = "#FFFFFF"

    # Torque
    def color_theme_turquoise(self):
        self.master.config(bg="#003333")
        self.text_frame.config(bg="#003333")
        self.text_box.config(bg="#669999", fg="#FFFFFF")
        self.entry_frame.config(bg="#003333")
        self.entry_field.config(bg="#669999", fg="#FFFFFF", insertbackground="#FFFFFF")
        self.send_button_frame.config(bg="#003333")
        self.send_button.config(bg="#669999", fg="#FFFFFF", activebackground="#669999", activeforeground="#FFFFFF")
        #self.emoji_button.config(bg="#669999", fg="#FFFFFF", activebackground="#669999", activeforeground="#FFFFFF")
        self.sent_label.config(bg="#003333", fg="#FFFFFF")

        self.tl_bg = "#669999"
        self.tl_bg2 = "#003333"
        self.tl_fg = "#FFFFFF"

    # Hacker
    def color_theme_hacker(self):
        self.master.config(bg="#0F0F0F")
        self.text_frame.config(bg="#0F0F0F")
        self.entry_frame.config(bg="#0F0F0F")
        self.text_box.config(bg="#0F0F0F", fg="#33FF33")
        self.entry_field.config(bg="#0F0F0F", fg="#33FF33", insertbackground="#33FF33")
        self.send_button_frame.config(bg="#0F0F0F")
        self.send_button.config(bg="#0F0F0F", fg="#FFFFFF", activebackground="#0F0F0F", activeforeground="#FFFFFF")
        #self.emoji_button.config(bg="#0F0F0F", fg="#FFFFFF", activebackground="#0F0F0F", activeforeground="#FFFFFF")
        self.sent_label.config(bg="#0F0F0F", fg="#33FF33")

        self.tl_bg = "#0F0F0F"
        self.tl_bg2 = "#0F0F0F"
        self.tl_fg = "#33FF33"

    # Default font and color theme
    def default_format(self):
        self.font_change_default()
        self.color_theme_default()

root=Tk()

a = ChatInterface(root)
root.geometry(window_size)
root.title("jeff")
#root.iconbitmap('i.ico')
root.mainloop()


#!/bin/python3

import sys
import argparse
from api import *
import csv
import json
import config as conf
sys.path.insert(0, '../')
from utils import *

def add_partner(csv_file,):
    session = MosipSession(conf.server, conf.partner_user, conf.partner_pwd, 'partner', ssl_verify=conf.ssl_verify)
    reader = csv.DictReader(open(csv_file, 'rt'))
    for row in reader:
        myprint('Adding partner %s' % row['name'])
        r = session.add_partner(row['name'], row['contact'], row['address'], row['email'], row['id'],
                                    row['type'], row['policy_group'])
        myprint(r)

def add_policy_group(csv_file):
    session = MosipSession(conf.server, conf.policym_user, conf.policym_pwd, 'partner', ssl_verify=conf.ssl_verify)
    reader = csv.DictReader(open(csv_file, 'rt'))
    for row in reader:
        myprint('Adding policy group %s' % row['name'])
        r = session.add_policy_group(row['name'], row['description'])
        myprint(r)
        if len(r['errors']) > 0:
            if r['errors'][0]['errorCode'] == 'PMS_POL_014': # Policy group already exists
                continue

def get_policy_group_id(policy_group_name):
        session = MosipSession(conf.server, conf.policym_user, conf.policym_pwd, 'partner', ssl_verify=conf.ssl_verify)
        r = session.get_policy_groups()
        policy_groups = r['response']
        pg_id = None
        for pg in policy_groups:
             if pg['policyGroup']['name'] == policy_group_name:
                 pg_id = pg['policyGroup']['id']
                 break
        return pg_id

def get_policy_id(policy_name):
    session = MosipSession(conf.server, conf.policym_user, conf.policym_pwd, 'partner', ssl_verify=conf.ssl_verify)
    r = session.get_policies()
    policies =  r['response']
    policy_id = None
    for policy in policies:
        if policy['policyName'] == policy_name:
            policy_id = policy['policyId']
    return policy_id

def add_policy(csv_file):
    session = MosipSession(conf.server, conf.policym_user, conf.policym_pwd, 'partner', ssl_verify=conf.ssl_verify)
    reader = csv.DictReader(open(csv_file, 'rt'))
    for row in reader:
        myprint('Adding policy "%s"' % row['name'])
        json_str = open(row['policy_file'], 'rt').read()
        policy = json.loads(json_str)
        r = session.add_policy(row['policy_id'],row['name'], row['description'], policy, row['policy_group'],
                               row['policy_type'])
        myprint(r)
        if len(r['errors']) == 0:
            policy_id = r['response']['id']
        else:
            if r['errors'][0]['errorCode'] == 'PMS_POL_009':  # Policy exists
                myprint('Updating policy "%s"' % row['name'])
                policy_id = get_policy_id(row['name'])
                if policy_id is None:
                    myprint('Policy id for policy "%s" could not be found, skipping..' % row['name'])
                    continue
                r = session.update_policy(row['name'], row['description'], policy, row['policy_group'],
                                          row['policy_type'], policy_id)
                myprint(r)
            else:
                continue  # Can't do much

        # publish policy
        myprint('Getting policy group id for policy group "%s"' % row['policy_group'])
        pg_id = get_policy_group_id(row['policy_group'])
        if pg_id is None:
            myprint('Could not find id for policy group "%s"' % (row['policy_group']))
            continue
        myprint('Publishing policy "%s"' % row['name'])
        r = session.publish_policy(pg_id, policy_id)
        myprint(r)

def upload_ca_certs(csv_file):
    session = MosipSession(conf.server, conf.partner_manager_user, conf.partner_manager_pwd, 'partner',
                           ssl_verify=conf.ssl_verify)
    reader = csv.DictReader(open(csv_file, 'rt'))
    for row in reader:
        myprint('Uploading CA certificate "%s"' % row['ca_cert_path'])
        cert_data = open(row['ca_cert_path'], 'rt').read()
        r = session.upload_ca_certificate(cert_data, row['partner_domain'])
        myprint(r)

def upload_partner_certs(csv_file):
    session = MosipSession(conf.server, conf.partner_user, conf.partner_pwd, 'partner', ssl_verify=conf.ssl_verify)
    reader = csv.DictReader(open(csv_file, 'rt'))
    for row in reader:
        myprint('Uplading partner certificate for "%s"' % row['org_name'])
        cert_data = open(row['cert_path'], 'rt').read()
        r = session.upload_partner_certificate(cert_data, row['org_name'], row['partner_domain'],
                                               row['partner_id'], row['partner_type'])
        myprint(r)
        mosip_signed_cert_path = os.path.join(os.path.dirname(row['cert_path']), 'mosip_signed_cert.pem')
        if r['errors'] is None:
            mosip_signed_cert = r['response']['signedCertificateData']
            open(mosip_signed_cert_path, 'wt').write(mosip_signed_cert)
            myprint('MOSIP signed certificate saved as %s' % mosip_signed_cert_path)

def map_partner_policy(csv_file):
    session1 = MosipSession(conf.server, conf.partner_user, conf.partner_pwd, 'partner', ssl_verify=conf.ssl_verify)
    session2 = MosipSession(conf.server, conf.partner_manager_user, conf.partner_manager_pwd, 'partner',
                            ssl_verify=conf.ssl_verify)
    reader = csv.DictReader(open(csv_file, 'rt'))
    for row in reader:
        myprint('Getting policies for a partner "%s" to check if this is a duplicate entry' % row['partner_id'])
        r = session1.get_partner_api_key_requests(row['partner_id'], row['policy_name'], row['description'])
        myprint(r)
        if r['errors'] is not None:
            if r['errors']['errorCode'] == 'PMS_PRT_005':  # Partner does not exist, add only then
                myprint('Sending partner-policy mapping request for (PARTNER: %s, POLICY: %s)' % (row['partner_id'],
                         row['policy_name']))
                r = session1.add_partner_api_key_requests(row['partner_id'], row['policy_name'], row['description'])
                myprint(r)
                api_request_id = r['response']['apiRequestId']

                # Approve
                myprint('Approving request for (PARTNER: %s, POLICY: %s)' % (row['partner_id'], row['policy_name']))
                r =  session2.approve_partner_policy(api_request_id, 'Approved')
                myprint(r)
        else:
            if r['response'][0]['apiKeyRequestStatus'] == 'In-Progress':
                api_request_id = r['response'][0]['apiKeyReqID']
                myprint('Approving request for (PARTNER: %s, POLICY: %s)' % (row['partner_id'], row['policy_name']))
                r =  session2.approve_partner_policy(api_request_id, 'Approved')
                myprint(r)
            else:
                myprint('ERROR: Skipping..')
                continue

def create_misp(csv_file):
    session = MosipSession(conf.server, conf.misp_user, conf.misp_pwd, 'partner', ssl_verify=conf.ssl_verify)
    reader = csv.DictReader(open(csv_file, 'rt'))
    for row in reader:
        myprint('Creating MISP "%s"' % row['org_name'])
        r = session.create_misp(row['org_name'], row['address'], row['contact'], row['email'])
        myprint(r)
        if len(r['errors']) == 0:   # No error
            misp_id = r['response']['mispID']
        else:
            myprint('Get misp id for "%s"' % row['org_name'])
            if r['errors'][0]['errorCode'] == 'PMS_MSP_003':  # Already exists
                r = session.get_misps()
                misps = r['response']
                for misp in misps:
                    myprint(misp)
                    if misp['misp']['name'] == row['org_name']:
                        misp_id = misp['misp']['ID']
            else:
               continue # Can't do much but to myprint error
        # Approving
        myprint('Approving MISP "%s"' % row['org_name'])
        r = session.approve_misp(misp_id, 'approved')
        myprint(r)

def add_extractor(csv_file):
    session = MosipSession(conf.server, conf.partner_user, conf.partner_pwd, ssl_verify=conf.ssl_verify)
    reader = csv.DictReader(open(csv_file, 'rt'))
    for row in reader:
        myprint('Adding extractor for %s -- %s' % (row['partner_id'], row['policy_id']))
        r = session.add_extractor(row['partner_id'], row['policy_id'], row['attribute'], row['biometric'],
                                  row['provider'], row['version'])
        myprint(r)

def args_parse():
   parser = argparse.ArgumentParser()
   parser.add_argument('action', help='policy_group|policy|extractor|partner|upload_certs|partner_policy|misp|all')
   parser.add_argument('--server', type=str, help='Full url to point to the server.  Setting this overrides server specified in config.py')
   parser.add_argument('--disable_ssl_verify', help='Disable ssl cert verification while connecting to server', action='store_true')
   args = parser.parse_args()
   return args, parser

def main():
    args, parser =  args_parse()

    if args.server:
        conf.server = args.server   # Overide

    if args.disable_ssl_verify:
        conf.ssl_verify = False

    init_logger('./out.log')
    try:
        if args.action == 'policy_group' or args.action == 'all':
            add_policy_group(conf.csv_policy_group)
        if args.action == 'policy' or args.action == 'all':
            add_policy(conf.csv_policy)
        if args.action == 'partner' or args.action == 'all':
            add_partner(conf.csv_partner)
        if args.action == 'extractor' or args.action == 'all':
            add_extractor(conf.csv_extractor)
        if args.action == 'upload_certs' or args.action == 'all':
            upload_ca_certs(conf.csv_partner_ca_certs)
            upload_partner_certs(conf.csv_partner_certs) #TODO: make sure  key_alias.py is called below api
        if args.action == 'partner_policy' or args.action == 'all':
            map_partner_policy(conf.csv_partner_policy_map)
        if args.action == 'misp'or args.action == 'all':
            create_misp(conf.csv_misp)
    except:
        formatted_lines = traceback.format_exc()
        myprint(formatted_lines)
        sys.exit(1)

    return sys.exit(0)

if __name__=="__main__":
    main()


from collections import defaultdict

file = 'data.txt'
N =[int(x) for x in open(file).read().strip().split(",")]

X = defaultdict(int)
for n in N:
    if n not in X:
        X[n] = 0
    X[n] += 1

for day in range(256):
    Y = defaultdict(int)
    for x,cnt in X.items():
        if x == 0:
            Y[6] += cnt
            Y[8] += cnt
        else:
            Y[x-1] += cnt
    X = Y
print(sum(X.values()))


# -*- coding: utf-8 -*-
import os
import json

import numpy as np
import tensorflow as tf

from preprocess.audio import AudioProcess
from preprocess.data_parse import DataGenerator
from models.language import LanguageHyperParams
from models.acoustics import AcousticsHyperParams
from models.acoustics.cnn_ctc import AcousticsModel
from models.language.transformer import LanguageModel

root = os.path.dirname(os.path.abspath(__file__))

os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
os.environ['KMP_DUPLICATE_LIB_OK'] = "TRUE"

class Predict:

    def __init__(self, file):
        self.file = file

        with open("vocab_json/vocab_counter.json", "r") as f:
            data = json.load(f)
            self.am_vocab_len = data.get("am_vocab")
            self.pny_vocab_len = data.get("pny_vocab")
            self.han_vocab_len = data.get("han_vocab")

        with open("vocab_json/am_vocab.json", "r") as f:
            self.am_vocab = json.load(f)

        with open("vocab_json/pny_vocab.json", "r") as f:
            self.pny_vocab = json.load(f)

        with open("vocab_json/han_vocab.json", "r") as f:
            self.han_vocab = json.load(f)

    def load_acoustics_model(self):
        am_args = AcousticsHyperParams.ap
        am_args.vocab_size = self.am_vocab_len
        am = AcousticsModel(am_args)
        am.ctc_model.load_weights(os.path.join(root, "outcome/acoustics/model.h5"))
        return am

    def load_language_model(self):
        lm_args = LanguageHyperParams.lp
        lm_args.input_vocab_size = self.pny_vocab_len
        lm_args.label_vocab_size = self.han_vocab_len
        lm = LanguageModel(lm_args)
        sess = tf.Session(graph=lm.graph)
        with lm.graph.as_default():
            saver = tf.train.Saver()
        with sess.as_default():
            latest = tf.train.latest_checkpoint(os.path.join(root, "outcome/language"))
            saver.restore(sess, latest)
        return lm, sess

    @staticmethod
    def wav2array(file):
        wav_data_lst = []
        frequency_bank = AudioProcess.frequency_domain(file)
        pad_bank = np.zeros((frequency_bank.shape[0] // 8 * 8 + 8, frequency_bank.shape[1]))
        pad_bank[:frequency_bank.shape[0], :] = frequency_bank
        wav_data_lst.append(pad_bank)
        pad_wav_data, input_length = DataGenerator.wav_padding(wav_data_lst)
        return pad_wav_data

    def run(self):
        pad_wav_data = self.wav2array(self.file)

        am = self.load_acoustics_model()
        lm, sess = self.load_language_model()

        am_predict = am.model.predict(pad_wav_data, steps=1)
        text = AudioProcess.decoder(am_predict, self.am_vocab)
        text = ' '.join(text)
        print("AM:-)", text)

        with sess.as_default():
            text = text.strip('\n').split(' ')
            x = np.array([self.pny_vocab.index(pny) for pny in text])
            x = x.reshape(1, -1)
            lm_predict = sess.run(lm.preds, {lm.x: x})
            got = ''.join(self.han_vocab[idx] for idx in lm_predict[0])
            print("LM:-)", got)
        sess.close()

        return got

if __name__ == "__main__":
    fs = "/Users/Maxwell_Lu/Documents/" \
         "/audio_data/data_thchs30/train/A11_1.wav"
    Predict(fs).run()


import logging
import random
import traceback
from datetime import datetime

import numpy as np
import torch

from configs.arguments import TrainingArguments
from dataset.processor import MyDataset
from models.STGDL import STGDL
from trainer import Trainer
from utils.file_utils import ensure_dir

def main(config: TrainingArguments):
    torch.autograd.set_detect_anomaly(True)
    time = datetime.strftime(datetime.now(), "%m%d_%H%M%S")
    setup(config, time)

    try:
        logging.info("Loading dataset...")
        dataset = MyDataset(config)
        # 这里 supports 是接下来要被分解的 G，一般只有一个图，也即邻接矩阵
        # 按理说不应该放在 dataset 这里，但要算 DTW 距离，就放这了
        supports = dataset.supports
        scaler = dataset.scaler

        logging.info("Loading model...")
        device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
        model = STGDL(config, supports, scaler, device).to(device)
        print_parameter_count(model)
        trainer = Trainer(config, model, dataset, time)

        logging.info("Start Training.")
        # --load 0601_235959 加载已训练模型，可能是断点训练也可能是 debug/可视化
        if config.load != "":
            trainer.load(f"saved_dict/{config.load}.ckpt")

        if config.load == "":
            trainer.train(time)
            trainer.load(trainer.save_path)
        elif config.continue_training_epoch != -1:
            trainer.load(trainer.save_path)
            trainer.train(time, config.continue_training_epoch)

        if config.save_graph:
            trainer.visualize("subgraphs")

        trainer.test()
        print_parameter_count(model)
    except Exception as e:
        traceback.print_exc()

def setup(config, time):
    ensure_dir("saved_dict")
    ensure_dir(f"logs/{time}")

    # logging config
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s   %(levelname)s   %(message)s')
    logger = logging.getLogger()
    formats = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    file_handler = logging.FileHandler(f'logs/log{time}.txt')
    file_handler.setFormatter(formats)
    logger.setLevel(logging.DEBUG)
    logger.addHandler(file_handler)

    for x in config:
        logging.info(x)

    setup_seed(config.seed)

def print_parameter_count(model):
    total = sum(p.numel() for p in model.parameters())
    total_req = sum(p.numel() for p in model.parameters() if p.requires_grad)
    logging.info(f"All para: {total}, trainable: {total_req}")

def setup_seed(seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    np.random.seed(seed)
    random.seed(seed)
    torch.backends.cudnn.deterministic = True


from app.forms.auth import RegisterForm
from app.forms.auth import LoginForm

from app.models.user import User
from app.models.base import db

from flask import request, jsonify

from . import web

from app.lib.http import login_required, create_token, verify_token

@web.route('/api/user/logout', methods=['GET'])
def logout():
    try:
        token = request.headers['Authorization']
        user = verify_token(token)
        if user:
            with db.auto_commit():
                result = User.query.filter_by(id=user.id).first()
                result.launched = False
        return jsonify({
            'code': 200,
            'message': '退出成功'
        })
    except:
        return jsonify({
            'code': 200,
            'message': '退出失败'
        })

@web.route('/api/user/info/get', methods=['GET'])
def getuserinfo():
    token = request.headers['Authorization']
    user = verify_token(token)
    id = user.id
    name = user.name
    roles = user.roles.split(',')

    return jsonify({
        'code': 200,
        'message': '请求用户信息成功',
        'data': {
            'userList': {
                'roles': roles,
                'name': name,
                'id': id
            }
        }
    })

@web.route('/api/user/register', methods=['POST'])
def register():
    form = request.form
    form = RegisterForm(request.form)
    if request.method  == 'POST' and form.validate():
        with db.auto_commit():
            user = User()
            user.set_attrs(form.data)
            db.session.add(user)
        return jsonify({
            'code': 200,
            'message': '注册成功'
        })
    return jsonify({
            'code': 201,
            'message': '注册失败'
        })

@web.route('/api/user/login', methods=['POST'])
def login():

    form = request.form
    form = LoginForm(request.form)
    if request.method == 'POST' and form.validate():
        user = User.query.filter_by(name=form.name.data).first()
        if user and user.check_password(form.password.data):
            with db.auto_commit():

                user.launched = True

            token = create_token(user.id)
            return jsonify({
                'code': 200,
                'message': '登录成功',
                'token': token,
                'username': user.name,
                'roles': user.roles.split(',')
            })
        return jsonify({
            'code': 201,
            'message': '用户名或密码有误,请在仔细检查一下重新输入'
        })
    else:
        return jsonify({
            'code': 201,
            'message': '登录失败'
        })


import random
from typing import List, Optional
from argparse import ArgumentParser

class RandomChoiceIterator:

    def __init__(self, values: List[int], num_iters: Optional[int] = None, seed: Optional[int|float] = None):
        if not values:
            raise ValueError('List of values is empty! It must contain at least one value!')
        self.values = values
        self.num_iters = num_iters
        random.seed(seed)
        if self.num_iters is not None:
            self.current_iteration = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.num_iters is None:
            return random.choice(self.values)
        elif self.current_iteration >= self.num_iters:
            raise StopIteration()
        else:
            self.current_iteration += 1
            return random.choice(self.values)

if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('-v', '--values', required=True, nargs='*', type=int)
    parser.add_argument('-n', '--num-iters', required=False, type=int, default=None)
    parser.add_argument('-s', '--seed', required=False, type=float, default=None)
    args = parser.parse_args()

    for value in RandomChoiceIterator(values=args.values, num_iters=args.num_iters, seed=args.seed):
        print(value)


"""mbl URL Configuration"""
from django.urls import include, re_path
from django.contrib import admin

from rest_framework import routers

from browser import views
from browser import rest

router = routers.DefaultRouter()
router.register(r'coursegroups', rest.CourseGroupViewSet)
router.register(r'courses', rest.CourseViewSet)
router.register(r'people', rest.PersonViewSet)
router.register(r'institutions', rest.InstitutionViewSet)
router.register(r'locations', rest.LocationViewSet)

urlpatterns = [
    re_path('', include('social.apps.django_app.urls', namespace='social')),
    re_path(r'^logout/$', views.logout_view, name='logout'),
    re_path(r'^admin/', admin.site.urls),
    re_path(r'^$', views.home, name="home"),
    re_path(r'^bulk/(?P<model>[a-z]+)/$', views.bulk_action, name="bulk-action"),

    re_path(r'^course/(?P<course_id>[0-9]+)/$', views.course, name="course"),
    re_path(r'^course/(?P<course_id>[0-9]+)/delete/$', views.course_delete, name="delete-course"),
    re_path(r'^course/(?P<course_id>[0-9]+)/edit/$', views.edit_course, name="edit-course"),
    re_path(r'^course/(?P<course_id>[0-9]+)/add/$', views.attendee_create, name="add-attendee"),
    re_path(r'^course/$', views.course, name="courses"),
    re_path(r'^course/create/$', views.course_create, name="create-course-nogroup"),
    re_path(r'^person/(?P<person_id>[0-9]+)/$', views.person, name="person"),
    re_path(r'^person/(?P<person_id>[0-9]+)/edit/$', views.handle_person, name="edit-person"),
    re_path(r'^person/(?P<person_id>[0-9]+)/split/$', views.split_person, name="split-person"),
    re_path(r'^person/merge/$', views.merge_people, name="merge-people"),
    re_path(r'^person/$', views.person, name="person_list"),
    re_path(r'^person/add/$', views.handle_person, name="add-person"),
    re_path(r'^person/(?P<person_id>[0-9]+)/investigator/add/$',views.add_investigator_record,name='add-investigator'),
    re_path(r'^person/(?P<person_id>[0-9]+)/investigator/(?P<research_id>[0-9]+)/edit/$',views.edit_investigator_record,name='edit-investigator'),
    re_path(r'^person/(?P<person_id>[0-9]+)/investigator/(?P<research_id>[0-9]+)/delete/$', views.delete_investigator_record, name='delete-investigator'),
    re_path(r'^person/(?P<person_id>[0-9]+)/affiliation/(?P<affiliation_id>[0-9]+)/edit/$', views.edit_affiliation, name='edit-affiliation'),
    re_path(r'^person/(?P<person_id>[0-9]+)/affiliation/(?P<affiliation_id>[0-9]+)/delete/$', views.delete_affiliation_record, name='delete-affiliation'),
    re_path(r'^person/(?P<person_id>[0-9]+)/position(?:/(?P<position_id>[0-9]+))?/$', views.position, name='position'),
    re_path(r'^person/(?P<person_id>[0-9]+)/position/(?P<position_id>[0-9]+)/delete/$', views.delete_position, name='delete-position'),
    re_path(r'^institution/(?P<institution_id>[0-9]+)/$', views.institution, name="institution"),
    re_path(r'^institution/(?P<institution_id>[0-9]+)/edit/$', views.edit_institution, name="edit-institution"),
    re_path(r'^institution/$', views.institution, name="institution_list"),

    re_path(r'^location/(?P<location_id>[0-9]+)/$', views.location, name="location"),
    re_path(r'^location/(?P<location_id>[0-9]+)/edit/$', views.edit_location, name="edit-location"),
    re_path(r'^location/(?P<location_id>[0-9]+)/split/$', views.split_location, name="split-location"),
    re_path(r'^location/$', views.location, name="location_list"),

    re_path(r'^coursegroup/(?P<coursegroup_id>[0-9]+)/$', views.coursegroup, name="coursegroup"),
    re_path(r'^coursegroup/(?P<coursegroup_id>[0-9]+)/edit/$', views.edit_coursegroup, name="edit-coursegroup"),
    re_path(r'^coursegroup/(?P<coursegroup_id>[0-9]+).json$', views.coursegroup_data, name="coursegroup_data"),
    re_path(r'^coursegroup/(?P<coursegroup_id>[0-9]+)/create/$', views.course_create, name="create-course"),
    re_path(r'^coursegroup/$', views.coursegroup, name="coursegroup_list"),
    re_path(r'^about/$', views.about, name="about"),
    re_path(r'^search/$', views.generic_autocomplete, name="search"),

    re_path(r'^rest/', include(router.urls)),
    re_path(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]


from __future__ import annotations

from typing import Callable

import numpy as np

import darsia as da

class Solver:
    """
    Solver base class. Contains an apply method.
    All solvers that are implemented as subclasses should have their own
    apply method, and inherit the stopping criterion.

    Attributes:
        stoppingCriterion (darsia.StoppingCriterion): stopping criterion for the solver
        verbose (bool): set to True to print output data

    """

    def __init__(
        self, stoppingCriterion: da.StoppingCriterion, verbose: bool = False
    ) -> None:
        """
        Constructor for solver class.

        Arguments:
            stoppingCriterion (darsia.StoppingCriterion): stopping criterion for the solver
            verbose (bool): set to True to print output data
        """
        self.stoppingCriterion = stoppingCriterion
        self.stoppingCriterion.verbose = verbose

    # def apply(self) -> None:
    #     print("Please choose a solver!")

class CG(Solver):
    """
    Conjugate Gradients solver.

    Attributes:
        apply: applies the solver method

    """

    def apply(self, operator: Callable, rhs: np.ndarray, im0: np.ndarray) -> np.ndarray:
        """
        Applies the conjugate gradients solver.

        Arguments:
            operator (Callable): The left hand side operator that is to be inverted.
            rhs (np.ndarray): Right-hand side.
            im0 (np.ndarray): Initial guess.
        """

        # Set im to initial guess and compute initial residual and conjugate matrix
        im = im0
        res = operator(im0) - rhs
        p = -res

        # Define iteration counter
        iterations = 0

        # Iterate as long as the stopping criterion is not satisfied
        while not (self.stoppingCriterion.check(res, iterations)):

            # Compute the operator applied to the conjugate matrix
            # (I think that it is more efficient to store this new matrix
            # rather than doing the operation twice).
            po = operator(p)

            # Compute step length
            alpha = -da.im_product(res, p) / da.im_product(po, p)

            # Update the image and compute new residual and conjugate matrix
            im = im + alpha * p
            res = operator(im) - rhs
            beta = da.im_product(res, po) / da.im_product(po, p)
            p = -res + beta * p

            # Update the iteration counter
            iterations += 1

        # Jump once further in the search direction and return the solution image
        alpha = -da.im_product(res, p) / da.im_product(operator(p), p)
        im = im + alpha * p
        return im

class ModifiedRichardson(Solver):
    """
    Modified Richardson solver.

    Attributes:
        apply: applies the solver method

    """

    def apply(
        self, operator: Callable, rhs: np.ndarray, im0: np.ndarray, omega: float
    ) -> np.ndarray:
        """
        Applies the modified Richardson solver.

        Arguments:
            operator (Callable): The left hand side operator that is to be inverted.
            rhs (np.ndarray): Right-hand side.
            im0 (np.ndarray): Initial guess.
            omega (float): Update parameter.
        """
        # Set im to initial guess and compute initial residual
        im = im0
        res = rhs - operator(im)

        # Define iteration counter
        iteration = 0

        # Iterate as long as the stopping criterion is not satisfied
        while not (self.stoppingCriterion.check(res, iteration)):

            # Update the image and compute new residual
            im = im + omega * res
            res = rhs - operator(im)

            # Update the iteration counter
            iteration += 1

        # Return image
        return im


from fastapi import FastAPI
from fastapi import HTTPException
from pydantic import BaseModel
from vncorenlp import VnCoreNLP
import os
import numpy as np
app = FastAPI()

annotator =  VnCoreNLP("./vncorenlp/VnCoreNLP-1.1.jar", annotators="wseg,pos,ner,parse", max_heap_size='-Xmx2g')

# đường đã đến file trả kết quả (gẫn đến thư mục của thuận toán CRF++-0.58)
file_out_khong_tag = "test.data"
# đường đẫn từ thư mục của thuật toán CRF++-0.58 lưu file output.data
ten_file_du_lieu1 = "output.data"
# thư mục chưa code thuật toán crf
dir = ".\\CRF++-0.58\\"

class Item(BaseModel):
    noi_dung: str

def themdauchamcau(list_):
        data = ""
        for a in list_:
            int_ = len(a)
            b = ""
            if (int_ >2):
                if (a[int_-1] != "\n"):
                    if (a[int_-1] not in [".",";","!","?"]):
                        b = a + ".\n"
                    else:
                        b = a
                else:
                    if (a[int_-2] not in [".",";","!","?"]):
                        b = a[:(int_ -1)] + ".\n"
                    else:
                        b = a
            data = data +"\n"+ b
        return data

def tao_file_du_lieu(data_input):
    doc = annotator.annotate(data_input)
    with open(dir + file_out_khong_tag, mode='w', encoding="utf-8" ) as file:
            for cau in doc["sentences"]:
                for tu in cau:
                    tab = "\t"
                    if (len(tu["form"]) <= 15):
                        tab += "\t"
                    if (len(tu["form"]) <= 7):
                        tab += "\t"
                    if(tu["form"] in [".",";","!","?","\n"]):
                        file.writelines("\n")
                    elif(["form"] not in [")",">","/",":", ",", "\"","`","(","<","”","“"]):
                        file.writelines((tu["form"]).lower() + tab + tu["posTag"] + "\n")

def getTrieuChungBenh(ten_file_du_lieu):
    f = open(ten_file_du_lieu, 'r', encoding='UTF-8')
    du_lieu_input = f.readlines()
    output = []
    temp = {'trieu_chung':'','thoi_gian':''}
    count = 0
    count_TG = 0
    for a in du_lieu_input:
        row = a.split()
        rows = np.array(row)
        if (len(rows)==3):
            if (rows[2] in ["B-TC","B-TCTL"]):
                count +=1
            if (rows[2] == "B-TG"):
                count_TG +=1
            if (count <= 1):
                if(rows[2] in ["B-TC","I-TC","B-TCTL","I-TCTL"]):
                    temp["trieu_chung"] += rows[0].replace("_"," ") + " "
                if(rows[2] in ["B-TG","I-TG"]):
                    if( count_TG  >1 and rows[2] == "B-TG"):
                        temp["thoi_gian"] += ", " + rows[0].replace("_"," ") + " "
                    else:
                        temp["thoi_gian"] +=rows[0].replace("_"," ") + " "
            else:
                count = 1
                count_TG = 0
                output.append(temp)
                temp = {'trieu_chung': rows[0].replace("_"," ") + " ",'thoi_gian':''}
    output.append(temp)
    return output

@app.post("/crf_get_trieu_chung/")
async def root(item: Item):

    du_lieu_input = item.noi_dung
    if (du_lieu_input == ""):

        raise HTTPException(status_code=400, detail="Dữ liệu truyền lên rỗng")
    du_lieu_input = du_lieu_input.split("\n")

    data_out = themdauchamcau(du_lieu_input)

    # tạo file dữ liệu test.data
    tao_file_du_lieu(data_out)

    os.chdir(dir)
    os.system("crf_test -m model1 test.data > output.data")

    data_benh = getTrieuChungBenh(dir + ten_file_du_lieu1 )
    if(len(data_benh) == 0):
        return {"message":"Không tìm thấy triệu chứng", "data": data_benh}
    return {"message":"Thành công", "data": data_benh}


from selenium import webdriver
from selenium.webdriver.common.by import By
from minitools.selenium.geetest import ClickSelenium
from selenium.webdriver.support import expected_conditions as EC

class GongShang(ClickSelenium):

    def __init__(self):
        self.driver = webdriver.Chrome('D:\chromedriver.exe')
        self.driver.get("http://www.gsxt.gov.cn/index.html")
        self.init_property()

    def capture_interface(self):
        search = self.waiter.until(EC.element_to_be_clickable((By.ID, 'keyword')))
        submit = self.waiter.until(EC.element_to_be_clickable((By.ID, 'btn_query')))
        self.sleep(2)
        search.clear()
        search.send_keys("武汉数博科技")
        self.sleep(2)
        submit.click()

    def calculate_track(self, picture):
        """How to get track???"""

    def check(self):
        self.waiter.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.search_logo_100000')))
        element = self.driver.find_element_by_xpath('//*[@class="search_list_item db"][1]')
        print(element.get_attribute('href'))
        print(self.driver.get_cookies())

if __name__ == '__main__':
    GongShang().run()


from django.contrib.auth import get_user_model
from rest_framework import serializers
from comment.serializers import CommentSerializer

User = get_user_model()

class UserSerializer(serializers.ModelSerializer):
    avatar_url = serializers.SerializerMethodField()
    background_image_url = serializers.SerializerMethodField()

    def get_avatar_url(self, obj):
        try:
            domain_name = 'https://luna2.propulsion-learn.ch'
            full_path = domain_name + obj.avatar.url
            return full_path
        except Exception:
            return None

    def get_background_image_url(self, obj):
        try:
            domain_name = 'https://luna2.propulsion-learn.ch'
            full_path = domain_name + obj.background_image.url
            return full_path
        except Exception:
            return None

    class Meta:
        model = User
        fields = ['id', 'email', 'username', 'date_joined', 'first_name', 'last_name', 'phone',
                  'interests', 'avatar', 'about_me', 'reviews', 'background_image', 'avatar_url', 'comments',
                  'background_image_url']
        read_only_fields = ['id', 'email', 'username', 'reviews']

    def to_representation(self, instance):
        representation = super().to_representation(instance)
        representation['avatar'] = representation.pop('avatar_url')
        representation['background_image'] = representation.pop('background_image_url')
        representation['comments'] = CommentSerializer(instance.comments, many=True, context=self.context).data
        return representation

class SearchSerializer(serializers.Serializer):
    type = serializers.CharField(min_length=1, max_length=100)
    search_string = serializers.CharField(min_length=1, max_length=500)

    def validate(self, data):
        request_type = data.get('type')
        if request_type.lower() == 'restaurants' or request_type.lower() == 'reviews' \
                or request_type.lower() == 'users':
            return data
        else:
            raise serializers.ValidationError('Incorrect type. You must use either restaurants, reviews or users')


import cv2
import numpy as np
import sys
from PIL import Image

def boxfilter(I,r):
    h,w = I.shape
    dstI = np.zeros((h, w))

    #cumulation over Y axis
    cumI = np.cumsum(I,axis=0)
    #difference over Y axis
    dstI[0:r+1,:] = cumI[r:2*r+1,:]
    dstI[r+1:h-r,:] = cumI[2*r+1:h,:] - cumI[0:h-2*r-1,:];
    dstI[h-r:h,:] = np.tile(cumI[h-1,:],(r,1)) - cumI[h-2*r-1:h-r-1,:]

    #cumulation over X axis
    cumI = np.cumsum(dstI,axis=1)
    #difference over X axis
    dstI[:,0:r+1] = cumI[:,r:2*r+1]
    dstI[:,r+1:w-r] = cumI[:,2*r+1:w] - cumI[:,0:w-2*r-1];
    dstI[:,w-r:w] = np.tile((cumI[:,w-1].reshape(h,1)),(1,r)) - cumI[:,w-2*r-1:w-r-1]

    return dstI

filename = "input/cat.jpg"
p = cv2.imread(filename,0)/1.0
I = p
r = int(sys.argv[1])
eps = float(sys.argv[2])
N = boxfilter(np.ones((I.shape[0], I.shape[1])), r);

meanI = boxfilter(I,r)/N
meanp = boxfilter(p,r)/N
meanIp = boxfilter(I*p,r)/N
# this is the covariance of (I, p) in each local patch.
covIp = meanIp - meanI * meanp

meanII = boxfilter(I*I,r)/N
varI = meanII - meanI * meanI

a = covIp / (varI + eps);
b = meanp - a * meanI;

meana = boxfilter(a, r)/N;
meanb = boxfilter(b, r)/N;

q = meana * I  + meanb;

formattedI = q
cv2.imwrite("output/"+str(r)+"-"+str(eps)+".jpg", formattedI)
cv2.imwrite("output/mean.jpg",meanI)


from django.urls import path
from . import views

app_name = 'ilaclar'

urlpatterns = [
    path('ilaclar', views.IlacList.as_view(), name='ilac-list'),
    # path('ilac/<ilac_adi>/', views.IlacMustahzarList.as_view(), name='ilac-detay'),
    path('etkenmadde/', views.EtkenMaddeList.as_view(), name='etkenmadde-list'),
]


__author__ = 'soloomi'

# import Bio
from Bio import SeqIO, SeqFeature
from collections import defaultdict

def find_annotation(read_cigar_dict, annotation_file, file_format='genbank'):
    """
    Finds the annotation for the locations on the genome where multi-reads map to
    :param read_cigar_dict: a nested dictionary like: {read_id: {CIGAR: [list of mapping positions]}
    :param annotation_file: the path to the reference genome annotation file
    :param file_format: the format of annotation file: 'genbank' or 'fasta'
    """
    position_cigar = defaultdict(list)
    for read_id, cigar_dict in read_cigar_dict.items():
        # if this read has only one CIGAR and has aligned only once; it's a unique read
        if len(cigar_dict) == 1:
             for cigar in cigar_dict:  # it has only one key (CIGAR)
                if len(cigar_dict[cigar]) == 1:  # list of mapping positions
                    # it's not a multi-read
                    pass
                else:
                    # a multi-read with the same alignment
                    for cigar, positions_list in cigar_dict.items():
                        for position in positions_list:
                            # for each position, create a list of read alignments to that location
                            position_cigar[position].append(cigar)

        else:
            # a multi-read with different alignments
            for cigar, positions_list in cigar_dict.items():
                for position in positions_list:
                    # for each position, create a list of read alignments to that location
                    position_cigar[position].append(cigar)

    # All locations on the genome which have a multi-read mapped to
    sorted_positions = sorted(position_cigar.keys())

    i = 0
    out_file = open('ot-annotation.txt', 'w')
    record = SeqIO.read(annotation_file, file_format)
    print(len(record.features))
    # For each gene, find what multi-reads are in its region, if any
    for feature in record.features:
        # print(feature.location.start, feature.location.end, feature.location.strand)
        if feature.type == 'source':
            continue
        cigars_list = []
        while sorted_positions[i] in range(feature.location.start, feature.location.end + 1):
            cigars_list.append((sorted_positions[i], position_cigar[sorted_positions[i]]))
            if i < len(sorted_positions):
                i += 1
        if cigars_list:
            # print(feature.location.start, '-', feature.location.end, feature.type, cigars_list)
            out_file.write("{}-{} {} {}\n".format(feature.location.start, feature.location.end, feature.type, cigars_list))
    out_file.close()


"""backend URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from api.views import router, loggedIn, login, addcomment, deletePost
from api.views import addreply, addpost, deleteReply, deleteComment
from rest_framework import routers
from api.all import PostListView
urlpatterns = [
    path('admin/', admin.site.urls),
    path('login/', login),
    path('api/', include('rest_framework.urls', namespace='rest_framework')),
    path('api/auth', loggedIn),
    path('api/addcomment/', addcomment),
    path('api/addreply/', addreply),
    path('api/addpost/', addpost),
    path('api/deletereply/', deleteReply),
    path('api/deletecomment/', deleteComment),
    path('api/deletepost/', deletePost),
    # path('api/allofpost', PostListView.as_view())
]
r = routers.DefaultRouter()

for route in router  + [PostListView] :
    rn = routers.SimpleRouter()
    rn.register(route.name, route)
    r.registry.extend(rn.registry)

urlpatterns += [path('api/', include(r.urls))]


import pyrenn
import LandMarkPointsExtractor.extractAndSaveLandmarkConfigurations as LM
import sys
import glob
import os
import numpy as np

def getDiff(x,y):
    xx = [ [] for i in range(len(x))]
    for i in range(len(x)):
        for j in range(len(x[0])):
            xx[i].append(x[i][j] - y[j])
    return xx

faces_folder_path = sys.argv[1]
caricaturesFolderPath = sys.argv[2]

XFaces = []
XCaricatures = []
YFaces = []
YCaricatures = []

for f in glob.glob(os.path.join(faces_folder_path, "*.jp*g")):
    LandMarkPoints = LM.saveAndReturnLandMarkPoints(f);
    xCoordis = []
    yCoordis = []
    for point in LandMarkPoints:
        xCoordis.append(point[0])
        yCoordis.append(point[1])
    XFaces.append(xCoordis)
    YFaces.append(yCoordis)

for f in glob.glob(os.path.join(caricaturesFolderPath, "*.jp*g")):
    LandMarkPoints = LM.saveAndReturnLandMarkPoints(f);
    xCoordis = []
    yCoordis = []
    for point in LandMarkPoints:
        xCoordis.append(point[0])
        yCoordis.append(point[1])
    XCaricatures.append(xCoordis)
    YCaricatures.append(yCoordis)

meanImageCoordinates = LM.saveAndReturnLandMarkPoints("Dataset/averageFace.jpg")

XFacesMeanDiff = getDiff(XFaces, [ meanImageCoordinates[i][0] for i in range(len(meanImageCoordinates)) ])
YFacesMeanDiff = getDiff(YFaces, [ meanImageCoordinates[i][1] for i in range(len(meanImageCoordinates)) ])

for i in range(len(XCaricatures)):
    for j in range(len(XCaricatures[0])):
        XCaricatures[i][j] = XCaricatures[i][j] - XFaces[i][j]
        YCaricatures[i][j] = YCaricatures[i][j] - YFaces[i][j]

print("XFacesMeand DIff:\n")
for i in XFacesMeanDiff:
    print(i)

print("YFacesMeand DIff:\n")
for i in YFacesMeanDiff:
    print(i)

print("XFacesCaricature DIff:\n")
for i in XCaricatures:
    print(i)

print("YFacesCaricature DIff:\n")
for i in YCaricatures:
    print(i)

net = pyrenn.CreateNN([68,68,68])
import pdb;
pdb.set_trace()
trainX = np.array(XFacesMeanDiff)
trainX = np.transpose(trainX)
outY = np.array(XCaricatures)
outY = np.transpose(outY)

pyrenn.train_LM(trainX, outY, net, verbose=True, k_max=1, E_stop=100 )

Y = pyrenn.NNOut(trainX, net)
print("Output OF NN")
for i in Y:
    print(i)


'''
In this problem the graph is like y = sin(x)
I need to find the values of:
-----------------------------
5 * sin(theta) + 3 = 0
-----------------------------
'''

import numpy as np
import math
import matplotlib.pyplot as plt

theta = math.asin(-3/5)
'''
old_theta_deg = math.fabs(theta*(180/math.pi))
'''

old_theta_deg = math.fabs(math.degrees(theta))
print(old_theta_deg)
theta_1 = old_theta_deg + 180

theta_2 = 360 - old_theta_deg

t_1 = float("{0:.3f}".format(theta_1))
t_2 = float("{0:.3f}".format(theta_2))

print("Hence the value for theta from 0 to 360 is {} deg. or {} deg.".format(t_1, t_2))

'''
The code for the graph
I am trying to plot the graph which will ensure that I plot the values properly.
This will be achieved by creating the angles like in previous problem.

'''

angles = np.linspace(0,360)

x = [math.radians(j) for j in angles]

y = [5*math.sin(i) + 3 for i in x]

plt.plot(angles,y)
plt.grid()
plt.show()


# import Python's JSON library for its loads() method
import json
#import sys

# import time for its sleep method
from time import sleep

# import the datetime libraries datetime.now() method
from datetime import datetime

# use the Elasticsearch client's helpers class for _bulk API
from elasticsearch import Elasticsearch, helpers
import glob
# declare a client instance of the Python Elasticsearch library
client = Elasticsearch("localhost:9200")
#file_name=str(sys.argv[1])
#index_name=str(sys.argv[2])

"""
JSON DATA IN FILE:
{"str field": "some string", "int field": 12345, "bool field": True}
{"str field": "another string", "int field": 42, "bool field": False}
{"str field": "random string", "int field": 3856452, "bool field": True}
{"str field": "string value", "int field": 11111, "bool field": False}
{"str field": "last string", "int field": 54321, "bool field": True}
"""

file_list=["agent_file_events.log","agent_process_events.log","agent_socket_events.log","conn.log","dhcp.log","dns.log","http.log","ssl.log","files.log"]
# assign directory
directory = '/opt/zeek/logs/current'
for filename in glob.iglob(f'{directory}/*'):
	file_name=filename.split('/')[-1]
	if file_name in file_list:
		print(file_name)
		index_name=file_name.split(".")[0]
		# define a function that will load a text file
		def get_data_from_text_file(self):
		# the function will return a list of docs
		    return [l.strip() for l in open(str(self), encoding="utf8", errors='ignore')]

		# call the function to get the string data containing docs
		docs = get_data_from_text_file(directory+'/'+file_name)

		# print the length of the documents in the string
		print ("String docs length:", len(docs))

		# define an empty list for the Elasticsearch docs
		doc_list = []
		history_list=[]

		# use Python's enumerate() function to iterate over list of doc strings
		for num, doc in enumerate(docs):

		# catch any JSON loads() errors
		    try:

		        # prevent JSONDecodeError resulting from Python uppercase boolean
		        doc = doc.replace("True", "true")
		        doc = doc.replace("False", "false")
		        doc = doc.replace("version","network_version")

		        # convert the string to a dict object
		        dict_doc = json.loads(doc)
		        #print("\n\ndic_doc ",num,dict_doc)
		        string_dict_doc=str(dict_doc)
		        if string_dict_doc not in history_list:
		            history_list.append(string_dict_doc)
		        else:
		            #print("found")
		            continue

		        # add a dict key called "_id" if you'd like to specify an ID for the doc
		        #dict_doc["_id"] = num

		        # append the dict object to the list []
		        doc_list += [dict_doc]
		        #print("doc_list",doc_list)

		    except json.decoder.JSONDecodeError as err:
		        # print the errors
		        print ("ERROR for num:", num, "-- JSONDecodeError:", err, "for doc:", doc)

		        print ("Dict docs length:", len(doc_list))

		    # attempt to index the dictionary entries using the helpers.bulk() method
		try:
		    print ("\nAttempting to index the list of docs using helpers.bulk()")

		    # use the helpers library's Bulk API to index list of Elasticsearch docs
		    resp = helpers.bulk(
		    client,
		    doc_list,
		    index = index_name,
		    doc_type = "_doc"
		    )

		    # print the response returned by Elasticsearch
		    # print ("helpers.bulk() RESPONSE:", resp)
		    # print ("helpers.bulk() RESPONSE:", json.dumps(resp, indent=4))

		except Exception as err:

		    # print any errors returned w
		    ## Prerequisiteshile making the helpers.bulk() API call
		    print("Elasticsearch helpers.bulk() ERROR:", err)
		    quit()

# get all of docs for the index
# Result window is too large, from + size must be less than or equal to: [10000]
# query_all = {
# 'size' : 10_000,
# 'query': {
# 'match_all' : {}
# }
# }

# print ("\nSleeping for a few seconds to wait for indexing request to finish.")
# sleep(2)

# # pass the query_all dict to search() method
# resp = client.search(
# index = "some_index",
# body = query_all
# )

# print ("search() response:", json.dumps(resp, indent=4))

# # print the number of docs in index
# print ("Length of docs returned by search():", len(resp['hits']['hits']))


# Enter your code here. Read input from STDIN. Print output to STDOUT
# Author : Arpit Shukla
# Date : 27th May, 2020

# Hackerrank itertools product solution python

from itertools import product

def cartproduct(A,B):
    ls=sorted(list(product(A,B)))
    print(*ls)

A=list(map(int,input().split()))
B=list(map(int,input().split()))

cartproduct(A,B)


import chess.pgn
import matplotlib.pyplot as plt

locs = [chess.A1, chess.A2, chess.A3, chess.A4, chess.A5, chess.A6, chess.A7, chess.A8,
     chess.B1, chess.B2, chess.B3, chess.B4, chess.B5, chess.B6, chess.B7, chess.B8,
     chess.C1, chess.C2, chess.C3, chess.C4, chess.C5, chess.C6, chess.C7, chess.C8,
     chess.D1, chess.D2, chess.D3, chess.D4, chess.D5, chess.D6, chess.D7, chess.D8,
     chess.E1, chess.E2, chess.E3, chess.E4, chess.E5, chess.E6, chess.E7, chess.E8,
     chess.F1, chess.F2, chess.F3, chess.F4, chess.F5, chess.F6, chess.F7, chess.F8,
     chess.G1, chess.G2, chess.G3, chess.G4, chess.G5, chess.G6, chess.G7, chess.G8,
     chess.H1, chess.H2, chess.H3, chess.H4, chess.H5, chess.H6, chess.H7, chess.H8]

loc_map = {chess.A1: (7, 0), chess.B1: (7, 1), chess.C1: (7, 2), chess.D1: (7, 3), chess.E1: (7, 4), chess.F1: (7, 5), chess.G1: (7, 6), chess.H1: (7, 7),
                chess.A2: (6, 0), chess.B2: (6, 1), chess.C2: (6, 2), chess.D2: (6, 3), chess.E2: (6, 4), chess.F2: (6, 5), chess.G2: (6, 6), chess.H2: (6, 7),
                chess.A3: (5, 0), chess.B3: (5, 1), chess.C3: (5, 2), chess.D3: (5, 3), chess.E3: (5, 4), chess.F3: (5, 5), chess.G3: (5, 6), chess.H3: (5, 7),
                chess.A4: (4, 0), chess.B4: (4, 1), chess.C4: (4, 2), chess.D4: (4, 3), chess.E4: (4, 4), chess.F4: (4, 5), chess.G4: (4, 6), chess.H4: (4, 7),
                chess.A5: (3, 0), chess.B5: (3, 1), chess.C5: (3, 2), chess.D5: (3, 3), chess.E5: (3, 4), chess.F5: (3, 5), chess.G5: (3, 6), chess.H5: (3, 7),
                chess.A6: (2, 0), chess.B6: (2, 1), chess.C6: (2, 2), chess.D6: (2, 3), chess.E6: (2, 4), chess.F6: (2, 5), chess.G6: (2, 6), chess.H6: (2, 7),
                chess.A7: (1, 0), chess.B7: (1, 1), chess.C7: (1, 2), chess.D7: (1, 3), chess.E7: (1, 4), chess.F7: (1, 5), chess.G7: (1, 6), chess.H7: (1, 7),
                chess.A8: (0, 0), chess.B8: (0, 1), chess.C8: (0, 2), chess.D8: (0, 3), chess.E8: (0, 4), chess.F8: (0, 5), chess.G8: (0, 6), chess.H8: (0, 7)
                }
loc_map_2 = {"a1": (7, 0), "b1": (7, 1), "c1": (7, 2), "d1": (7, 3), "e1": (7, 4), "f1": (7, 5), "g1": (7, 6), "h1": (7, 7),
                "a2": (6, 0), "b2": (6, 1), "c2": (6, 2), "d2": (6, 3), "e2": (6, 4), "f2": (6, 5), "g2": (6, 6), "h2": (6, 7),
                "a3": (5, 0), "b3": (5, 1), "c3": (5, 2), "d3": (5, 3), "e3": (5, 4), "f3": (5, 5), "g3": (5, 6), "h3": (5, 7),
                "a4": (4, 0), "b4": (4, 1), "c4": (4, 2), "d4": (4, 3), "e4": (4, 4), "f4": (4, 5), "g4": (4, 6), "h4": (4, 7),
                "a5": (3, 0), "b5": (3, 1), "c5": (3, 2), "d5": (3, 3), "e5": (3, 4), "f5": (3, 5), "g5": (3, 6), "h5": (3, 7),
                "a6": (2, 0), "b6": (2, 1), "c6": (2, 2), "d6": (2, 3), "e6": (2, 4), "f6": (2, 5), "g6": (2, 6), "h6": (2, 7),
                "a7": (1, 0), "b7": (1, 1), "c7": (1, 2), "d7": (1, 3), "e7": (1, 4), "f7": (1, 5), "g7": (1, 6), "h7": (1, 7),
                "a8": (0, 0), "b8": (0, 1), "c8": (0, 2), "d8": (0, 3), "e8": (0, 4), "f8": (0, 5), "g8": (0, 6), "h8": (0, 7)
                }

def increase_board(big_board, index):
    if index[0] >=0 and index[0] <= 7 and index[1] >= 0 and index[1] <= 7:
        big_board[index[0]][index[1]] += 1
    return big_board

def analyze_black_pawn(big_board, loc):
    index = loc_map[loc]
    big_board = increase_board(big_board, (index[0] + 1, index[1] - 1))
    big_board = increase_board(big_board, (index[0] + 1, index[1] + 1))
    return big_board

def analyze_white_pawn(big_board, loc):
    index = loc_map[loc]
    big_board = increase_board(big_board, (index[0] - 1, index[1] - 1))
    big_board = increase_board(big_board, (index[0] - 1, index[1] + 1))
    return big_board

def analyze_rook(big_board, loc):
    index = loc_map[loc]
    for i in range(8):
        big_board = increase_board(big_board, (index[0], i))
        big_board = increase_board(big_board, (i, index[1]))
    return big_board

def analyze_knight(big_board, loc):
    index = loc_map[loc]
    big_board = increase_board(big_board, (index[0] + 2, index[1] + 1))
    big_board = increase_board(big_board, (index[0] + 2, index[1] - 1))
    big_board = increase_board(big_board, (index[0] - 2, index[1] + 1))
    big_board = increase_board(big_board, (index[0] - 2, index[1] - 1))
    big_board = increase_board(big_board, (index[0] + 1, index[1] + 2))
    big_board = increase_board(big_board, (index[0] + 1, index[1] - 2))
    big_board = increase_board(big_board, (index[0] - 1, index[1] + 2))
    big_board = increase_board(big_board, (index[0] - 1, index[1] - 2))
    return big_board

def analyze_bishop(big_board, loc):
    index = loc_map[loc]
    for i in range(8):
        big_board = increase_board(big_board, (index[0] + i, index[1] + i))
        big_board = increase_board(big_board, (index[0] - i, index[1] + i))
        big_board = increase_board(big_board, (index[0] + i, index[1] - i))
        big_board = increase_board(big_board, (index[0] - i, index[1] - i))
    return big_board

def analyze_queen(big_board, loc):
    index = loc_map[loc]
    for i in range(8):
        big_board = increase_board(big_board, (index[0], i))
        big_board = increase_board(big_board, (i, index[1]))
        big_board = increase_board(big_board, (index[0] + i, index[1] + i))
        big_board = increase_board(big_board, (index[0] - i, index[1] + i))
        big_board = increase_board(big_board, (index[0] + i, index[1] - i))
        big_board = increase_board(big_board, (index[0] - i, index[1] - i))
    return big_board

def analyze_king(big_board, loc):
    index = loc_map[loc]
    big_board = increase_board(big_board, (index[0] + 1, index[1] + 1))
    big_board = increase_board(big_board, (index[0] + 1, index[1] - 1))
    big_board = increase_board(big_board, (index[0] - 1, index[1] + 1))
    big_board = increase_board(big_board, (index[0] - 1, index[1] - 1))
    big_board = increase_board(big_board, (index[0] + 1, index[1]))
    big_board = increase_board(big_board, (index[0] - 1, index[1]))
    big_board = increase_board(big_board, (index[0], index[1] + 1))
    big_board = increase_board(big_board, (index[0], index[1] - 1))
    return big_board

def analyze_board(big_board, board):

    # pieces = {'p', 'r', 'n', 'b', 'q', 'k', 'P', 'R', 'N', 'B', 'Q', 'K'}
    for loc in locs:
        piece = board.piece_at(loc)
        if piece is not None:
            p = piece.symbol()
            if p == 'p':
                big_board = analyze_black_pawn(big_board, loc)
            elif p == 'r' or p == 'R':
                big_board = analyze_rook(big_board, loc)
            elif p == 'n' or p == 'N':
                big_board = analyze_knight(big_board, loc)
            elif p == 'b' or p == 'B':
                big_board = analyze_bishop(big_board, loc)
            elif p == 'q' or p == 'Q':
                big_board = analyze_queen(big_board, loc)
            elif p == 'k' or p == 'K':
                big_board = analyze_king(big_board, loc)
            elif p == 'P':
                big_board = analyze_white_pawn(big_board, loc)
    return big_board

def update_move_board(move_board, row, col):
    if row >=0 and row <= 7 and col >= 0 and col <= 7:
        move_board[row][col] += 1
    return move_board

def analyze_move(move_board, mover):
    col_to_num = {"a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5, "g": 6, "h": 7}
    #convert move to string
    move = str(mover)
    print(move)
    start_row, start_col = loc_map_2[move[0:2]]
    print(start_col, start_row)
    end_row, end_col = loc_map_2[move[2:4]]

    # start_col = int(col_to_num[move[0]])
    # start_row = int(move[1]) - 1
    # end_col = int(col_to_num[move[2]])
    # end_row = int(move[3]) - 1
    if start_col == end_col:
        # vertical move
        for i in range(min(start_row, end_row), max(start_row, end_row)+1):
            move_board = update_move_board(move_board, i, start_col)
    elif start_row == end_row:
        # horizontal move
        for i in range(min(start_col, end_col), max(start_col, end_col)+1):
            move_board = update_move_board(move_board, start_row, i)
    elif abs(start_col - end_col) == abs(start_row - end_row):
        # diagonal move
        for i in range(abs(start_col - end_col)+1):
            move_board = update_move_board(move_board, min(start_row, end_row) + i, min(start_col, end_col) + i)
    else:
        # knight move
        # default to do the 1 first and then the two
        move_board = update_move_board(move_board, start_row, start_col)
        if abs(start_row - end_row) == 1:
            # 1 row shift
            move_board = update_move_board(move_board, end_row, start_col)
            if end_col > start_col:
                move_board = update_move_board(move_board, end_row, start_col + 1)
                move_board = update_move_board(move_board, end_row, start_col + 2)
            else:
                move_board = update_move_board(move_board, end_row, start_col - 1)
                move_board = update_move_board(move_board, end_row, start_col - 2)
        else:
            # 1 col shift
            move_board = update_move_board(move_board, start_row, end_col)
            if end_row > start_row:
                move_board = update_move_board(move_board, start_row + 1, end_col)
                move_board = update_move_board(move_board, start_row + 2, end_col)
            else:
                move_board = update_move_board(move_board, start_row - 1, end_col)
                move_board = update_move_board(move_board, start_row - 2, end_col)
    return move_board

def simulate_game(pgn_file, big_board, move_board):
    with open(pgn_file) as f:
        game = chess.pgn.read_game(f)

    board = game.board()

    print("Simulating chess game...\n")

    for move in game.mainline_moves():
        board.push(move)
        print(f"Move: {move}")
        move_board = analyze_move(move_board, move)
        # print(f"Board:\n{board}\n")
        big_board = analyze_board(big_board, board)
        # print(big_board)
    # print(move_board)
    print("Game over.")
    print("Result: " + game.headers["Result"])

# Example usage:

big_board = [[0 for _ in range(8)] for _ in range(8)]
move_board = [[0 for _ in range(8)] for _ in range(8)]
pgn_file = "gukesh_giri_norway_chess_2023.pgn"
simulate_game(pgn_file, big_board, move_board)
for i in move_board:
    print(i)

# print move_board as a heat map
plt.imshow(move_board, cmap='hot', interpolation='nearest')
plt.show()


# -*- coding: utf-8 -*-
"""
Created on Thu Nov  3 19:34:32 2022

@author: taylo
"""

'''
Disclaimer all code and all examples shown here are for the most part taken from argoverse-api
here is the link:
    https://github.com/argoai/argoverse-api

You'll also want to make sure you have data which you get from their webiste here:
    https://www.argoverse.org/av1.html#stereo-link
Keep scrolling down to the bottom!

Additionally you want to make sure you have all the below imports installed, additionally
you want the following packages installed (you likely have a good amount already
installed):
    logging, pathlib, typing, imageio, typing_extensions, pandas, multiprocessing, argparse,
    functools, dataclasses, glob, scipy
    '''

from pathlib import Path
import json

import os
from argoverse_light.stereo_dataloader import ArgoverseStereoDataLoader
from argoverse_light.camera_stats import RECTIFIED_STEREO_CAMERA_LIST
from argoverse_light.calibration import get_calibration_config
from argoverse_light.eval import StereoEvaluator

#import open3d as o3d <- this ruined everything for me, bad install....
import cv2
import numpy as np
import matplotlib.pyplot as plt
import plotly.io as pio
pio.renderers.default='browser'
import plotly.graph_objects as go
import open3d as o3d

STEREO_FRONT_LEFT_RECT = RECTIFIED_STEREO_CAMERA_LIST[0]
STEREO_FRONT_RIGHT_RECT = RECTIFIED_STEREO_CAMERA_LIST[1]
print(STEREO_FRONT_LEFT_RECT)

#NOTE: YOU WANT HTIS TO BE RELATIVE AT LEAST ON WINDOWS
data_dir = os.path.join('..','..', 'shared', 'data')
stereo_img_dir = os.path.join(data_dir, 'rectified_stereo_images_v1.1')
disparity_img_dir = os.path.join(data_dir, 'disparity_maps_v1.1')

print('Data is in: ', data_dir)

split_name = "train"
log_id = "273c1883-673a-36bf-b124-88311b1a80be"
idx = 34
stereo_data_loader = ArgoverseStereoDataLoader(data_dir, split_name)

# Loading the left rectified stereo image paths for the chosen log.
left_stereo_img_fpaths = stereo_data_loader.get_ordered_log_stereo_image_fpaths(
    log_id=log_id,
    camera_name=STEREO_FRONT_LEFT_RECT,
)

# Loading the right rectified stereo image paths for the chosen log.
right_stereo_img_fpaths = stereo_data_loader.get_ordered_log_stereo_image_fpaths(
    log_id=log_id,
    camera_name=STEREO_FRONT_RIGHT_RECT,
)

# Loading the disparity map paths for the chosen log.
disparity_map_fpaths = stereo_data_loader.get_ordered_log_disparity_map_fpaths(
    log_id=log_id,
    disparity_name="stereo_front_left_rect_disparity",
)

# Loading the disparity map paths for foreground objects for the chosen log.
disparity_obj_map_fpaths = stereo_data_loader.get_ordered_log_disparity_map_fpaths(
    log_id=log_id,
    disparity_name="stereo_front_left_rect_objects_disparity",
)

#Now use those functions there and actually load the data
stereo_front_left_rect_image = stereo_data_loader.get_rectified_stereo_image(left_stereo_img_fpaths[idx])
stereo_front_right_rect_image = stereo_data_loader.get_rectified_stereo_image(right_stereo_img_fpaths[idx])

# Loading the ground-truth disparity maps.
stereo_front_left_rect_disparity = stereo_data_loader.get_disparity_map(disparity_map_fpaths[idx])
# Loading the ground-truth disparity maps for foreground objects only.
stereo_front_left_rect_objects_disparity = stereo_data_loader.get_disparity_map(disparity_obj_map_fpaths[idx])

#Look at the data:
stereo_front_left_rect_disparity_dil = cv2.dilate(
    stereo_front_left_rect_disparity,
    kernel=np.ones((2, 2), np.uint8),
    iterations=7,
)

stereo_front_left_rect_objects_disparity_dil = cv2.dilate(
    stereo_front_left_rect_objects_disparity,
    kernel=np.ones((2, 2), np.uint8),
    iterations=7,
)

plt.figure(figsize=(9, 9))
plt.subplot(2, 2, 1)
plt.title("Rectified left stereo image")
plt.imshow(stereo_front_left_rect_image)
plt.axis("off")
plt.subplot(2, 2, 2)
plt.title("Rectified right stereo image")
plt.imshow(stereo_front_right_rect_image)
plt.axis("off")
plt.subplot(2, 2, 3)
plt.title("Left disparity map")
plt.imshow(
    stereo_front_left_rect_disparity_dil,
    cmap="nipy_spectral",
    vmin=0,
    vmax=192,
    interpolation="none",
)
plt.axis("off")
plt.subplot(2, 2, 4)
plt.title("Left object disparity map")
plt.imshow(
    stereo_front_left_rect_objects_disparity_dil,
    cmap="nipy_spectral",
    vmin=0,
    vmax=192,
    interpolation="none",
)
plt.axis("off")
plt.tight_layout()

#Recovering calibration info:
# First, we need to load the camera calibration. Specifically, we want the camera intrinsic parameters.
calib_data = stereo_data_loader.get_log_calibration_data(log_id=log_id)
camera_config = get_calibration_config(calib_data, camera_name=STEREO_FRONT_LEFT_RECT)

# Getting the focal lenght and baseline. Note that the baseline is constant for the Argoverse stereo rig setup.
focal_lenght = camera_config.intrinsic[0, 0]  # Focal length in pixels.
BASELINE = 0.2986  # Baseline in meters.

# We consider disparities greater than zero to be valid disparities.
# A zero disparity corresponds to an infinite distance.
valid_pixels = stereo_front_left_rect_disparity > 0

# Using the stereo relationship previsouly described, we can recover the depth map by:
stereo_front_left_rect_depth = \
    np.float32((focal_lenght * BASELINE) / (stereo_front_left_rect_disparity + (1.0 - valid_pixels)))

# Recovering the colorized point cloud using Open3D.
left_image_o3d = o3d.geometry.Image(stereo_front_left_rect_image)
depth_o3d = o3d.geometry.Image(stereo_front_left_rect_depth)
rgbd_image_o3d = o3d.geometry.RGBDImage.create_from_color_and_depth(
    left_image_o3d,
    depth_o3d,
    convert_rgb_to_intensity=False,
    depth_scale=1.0,
    depth_trunc=200,
)
pinhole_camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
pinhole_camera_intrinsic.intrinsic_matrix = camera_config.intrinsic[:3, :3]
pinhole_camera_intrinsic.height = camera_config.img_height
pinhole_camera_intrinsic.width = camera_config.img_width
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image_o3d, pinhole_camera_intrinsic)

# Showing the colorized point cloud using the interactive Plotly.
points = np.asarray(pcd.points)
colors = np.asarray(pcd.colors)

fig = go.Figure(
    data=[
        go.Scatter3d(
            x=points[:, 0],
            y=points[:, 1],
            z=points[:, 2],
            mode="markers",
            marker=dict(size=1, color=colors),
            )
    ],
  layout=dict(
      scene=dict(
            xaxis=dict(visible=False),
            yaxis=dict(visible=False),
          zaxis=dict(visible=False),
            aspectmode="data",
        )
    ),
)
fig.show() #<- not working on my system...

# Defining the SGM parameters (please check the OpenCV documentation for details).
# We found this parameters empirically and based on the Argoverse Stereo data.
max_disp = 192
win_size = 10
uniqueness_ratio = 15
speckle_window_size = 200
speckle_range = 2
block_size = 11
P1 = 8 * 3 * win_size ** 2
P2 = 32 * 3 * win_size ** 2

# Defining the Weighted Least Squares (WLS) filter parameters.
lmbda = 0.1
sigma = 1.0

# Defining the SGM left matcher.
left_matcher = cv2.StereoSGBM_create(
    minDisparity=0,
    numDisparities=max_disp,
    blockSize=block_size,
    P1=P1,
    P2=P2,
    disp12MaxDiff=max_disp,
    uniquenessRatio=uniqueness_ratio,
    speckleWindowSize=speckle_window_size,
    speckleRange=speckle_range,
    mode=cv2.STEREO_SGBM_MODE_SGBM_3WAY,
)

# Defining the SGM right matcher needed for the left-right consistency check in the WLS filter.
right_matcher = cv2.ximgproc.createRightMatcher(left_matcher)

# Defining the WLS filter.
wls_filter = cv2.ximgproc.createDisparityWLSFilter(matcher_left=left_matcher)
wls_filter.setLambda(lmbda)
wls_filter.setSigmaColor(sigma)

# Computing the disparity maps.
left_disparity = left_matcher.compute(stereo_front_left_rect_image, stereo_front_right_rect_image)
right_disparity = right_matcher.compute(stereo_front_right_rect_image, stereo_front_left_rect_image)

# Applying the WLS filter.
left_disparity_pred = wls_filter.filter(left_disparity, stereo_front_left_rect_image, None, right_disparity)

# Recovering the disparity map.
# OpenCV produces a disparity map as a signed short obtained by multiplying subpixel shifts with 16.
# To recover the true disparity values, we need to divide the output by 16 and convert to float.
left_disparity_pred = np.float32(left_disparity_pred) / 16.0

# OpenCV will also set negative values for invalid disparities where matches could not be found.
# Here we set all invalid disparities to zero.
left_disparity_pred[left_disparity_pred < 0] = 0

plt.figure(figsize=(9, 9))
plt.subplot(2, 2, 1)
plt.title("Rectified left stereo image")
plt.imshow(stereo_front_left_rect_image)
plt.axis("off")
plt.subplot(2, 2, 2)
plt.title("Rectified right stereo image")
plt.imshow(stereo_front_right_rect_image)
plt.axis("off")
plt.subplot(2, 2, 3)
plt.title("Ground-truth left disparity map")
plt.imshow(
    stereo_front_left_rect_disparity_dil,
    cmap="nipy_spectral",
    vmin=0,
    vmax=192,
    interpolation="none",
)
plt.axis("off")
plt.subplot(2, 2, 4)
plt.title("Estimated left disparity map")
plt.imshow(
    left_disparity_pred,
    cmap="nipy_spectral",
    vmin=0,
    vmax=192,
    interpolation="none"
)
plt.axis("off")
plt.tight_layout()

# We consider disparities greater than zero to be valid disparities.
# A zero disparity corresponds to an infinite distance.
valid_pixels = left_disparity_pred > 0

# Using the stereo relationship previsouly described, we can recover the predicted depth map by:
left_depth_pred = \
    np.float32((focal_lenght * BASELINE) / (left_disparity_pred + (1.0 - valid_pixels)))

# Recovering the colorized point cloud using Open3D.
#left_image_o3d = o3d.geometry.Image(stereo_front_left_rect_image)
#depth_o3d = o3d.geometry.Image(left_depth_pred)
#rgbd_image_o3d = o3d.geometry.RGBDImage.create_from_color_and_depth(
#    left_image_o3d,
#    depth_o3d,
#    convert_rgb_to_intensity=False,
#    depth_scale=1.0,
#    depth_trunc=200,
#)
#pinhole_camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
#pinhole_camera_intrinsic.intrinsic_matrix = camera_config.intrinsic[:3, :3]
#pinhole_camera_intrinsic.height = camera_config.img_height
#pinhole_camera_intrinsic.width = camera_config.img_width
#pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image_o3d, pinhole_camera_intrinsic)

# Showing the colorized point cloud using the interactive Plotly.
#points = np.asarray(pcd.points)
# Randomly sampling indices for faster rendering.
#indices = np.random.randint(len(points), size=100000)
#points = points[indices]
#colors = np.asarray(pcd.colors)[indices]

#fig = go.Figure(
#     data=[
#         go.Scatter3d(
#             x=points[:, 0],
#             y=points[:, 1],
#             z=points[:, 2],
#             mode="markers",
#             marker=dict(size=1, color=colors),
#         )
#     ],
#     layout=dict(
#         scene=dict(
#             xaxis=dict(visible=False),
#             yaxis=dict(visible=False),
#             zaxis=dict(visible=False),
#             aspectmode="data",
#         ),
#     ),
# )
# fig.show()

# Encoding the real disparity values to an uint16 data format to save as an uint16 PNG file.
left_disparity_pred = np.uint16(left_disparity_pred * 256.0)

timestamp = int(Path(disparity_map_fpaths[idx]).stem.split("_")[-1])

# Change the path to the directory you would like to save the result.
# The log id must be consistent with the stereo images' log id.
save_dir_disp = f"."

# The predicted disparity filename must have the format: 'disparity_[TIMESTAMP OF THE LEFT STEREO IMAGE].png'
filename = f"disparity_{timestamp}.png"

# Writing the PNG file to disk.
cv2.imwrite(filename, left_disparity_pred)

# Path to the predicted disparity maps.
pred_dir = Path(save_dir_disp)

# Path to the ground-truth disparity maps.
gt_dir = Path(f"{data_dir}/disparity_maps_v1.1/{split_name}/{log_id}")
gt_dir = Path(os.path.join(data_dir, 'disparity_maps_v1.1', 'train', log_id))

# Path to save the disparity error image.
save_figures_dir = Path(os.path.join('save_figs'))
save_figures_dir.mkdir(parents=True, exist_ok=True)

print(pred_dir)
print(gt_dir)

# Creating the stereo evaluator.
print('Creating Stereo Evaluator')
evaluator = StereoEvaluator(
    pred_dir,
    gt_dir,
    save_figures_dir,
    save_disparity_error_image=True,
    num_procs=1,
)
print('Running Stereo Evaluator')
# Running the stereo evaluation.
#metrics = evaluator.evaluate() this fails because I only look at a single value, but will come in handy later

# Printing the quantitative results (using json trick for organized printing).
#print(metrics)


import requests

h={"user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"}

url="https://amzn.to/3G1Wboz"
res = requests.get(url,headers=h)

print(res.reason)
print(res.cookies)
print(res.request.headers)

with open("static/codigo_200.html","w",encoding="utf-8") as f:
    f.write(res.text)


"""
Utility functions to convert ``sqsgenerator.core.Structure`` to ``ase.Atoms`` and ``pymatgen.core.Structure``
and vice versa
"""

import numpy as np
from operator import attrgetter as attr
from sqsgenerator.core import Structure, transpose
from sqsgenerator.compat import Feature as F, require

@require(F.pymatgen)
def to_pymatgen_structure(structure: Structure):
    """
    Convert structure {structure} to a ``pymatgen.core.Structure`` object

    :param structure: the structure to convert
    :type structure: Structure
    :return: the ``pymatgen.core.Structure`` object
    :rtype: pymatgen.core.Structure
    """
    from pymatgen.core import Structure as PymatgenStructure, Lattice as PymatgenLattice
    structure = structure.without_vacancies()
    symbols = list(map(attr('symbol'), structure.species))
    lattice = PymatgenLattice(structure.lattice)
    return PymatgenStructure(lattice, symbols, structure.frac_coords, coords_are_cartesian=False, validate_proximity=True)

@require(F.ase)
def to_ase_atoms(structure: Structure):
    """
    Convert structure {structure} to a ``ase.Atoms`` object

    :param structure: the structure to convert
    :type structure: Structure
    :return: the ``ase.Atoms`` object
    :rtype: ase.Atoms
    """
    from ase import Atoms
    structure = structure.without_vacancies()
    numbers = list(map(attr('Z'), structure.species))
    return Atoms(cell=structure.lattice, scaled_positions=structure.frac_coords, numbers=numbers, pbc=structure.pbc)

@require(F.pyiron_atomistics)
def to_pyiron_atoms(structure: Structure):
    """
    Convert structure {structure} to as ``pyiron_atomistics.atomistics.structure.Atoms`` object

    :param structure: the structure to convert
    :type structure: Structure
    :return: the ``pyiron_atomistics.atomistics.structure.Atoms`` object
    :rtype: pyiron_atomistics.atomistics.structure.Atoms
    """
    from pyiron_atomistics.atomistics.structure.atoms import Atoms
    structure = structure.without_vacancies()
    numbers = list(map(attr('Z'), structure.species))
    return Atoms(cell=structure.lattice, numbers=numbers, scaled_positions=structure.frac_coords, pbc=structure.pbc)

@require(F.pymatgen)
def from_pymatgen_structure(structure) -> Structure:
    """
    Convert the ``pymatgen.core.Structure`` {structure} object to a ``sqsgenerator.core.Structure`` object

    :param structure: the structure to convert
    :type structure: pymatgen.core.Structure
    :return: the structure object
    :rtype: Structure
    """
    lattice = structure.lattice.matrix
    data = map(lambda site: (site.species_string, site.frac_coords), structure)
    species, frac_coords = transpose(data)
    return Structure(np.array(lattice), np.array(frac_coords), species, (True, True, True))

@require(F.ase)
def from_ase_atoms(atoms) -> Structure:
    """
    Convert the ``ase.Atoms`` {structure} object to a ``sqsgenerator.core.Structure`` object

    :param atoms: the structure to convert
    :type atoms: ase.Atoms
    :return: the structure object
    :rtype: Structure
    """
    lattice = np.array(atoms.cell)
    frac_coords = np.array(atoms.get_scaled_positions()).copy()
    if tuple(atoms.pbc) != (True, True, True):
        raise RuntimeWarning("At present I can only handle fully periodic structures. "
                             "I'will overwrite ase.Atoms.pbc setting to (True, True, True)")
    return Structure(lattice, frac_coords, list(atoms.symbols), (True, True, True))

@require(F.pyiron_atomistics)
def from_pyiron_atoms(atoms) -> Structure:
    """
    Convert the ``ase.Atoms`` {structure} object to a ``pyiron_atomistics.atomistics.structure.Atoms`` object

    :param atoms: the structure to convert
    :type atoms: pyiron_atomistics.atomistics.structure.Atoms
    :return: the structure object
    :rtype: Structure
    """
    return from_ase_atoms(atoms)


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from config import CHROME_DATA_PATH
from time import sleep
import os
from selenium.webdriver.common.keys import Keys
import pandas as pd

# os.system("taskkill /im chrome.exe /f")
option = webdriver.ChromeOptions()
option.add_argument(CHROME_DATA_PATH)

driver = webdriver.Chrome(executable_path='chromedriver.exe',options=option)
driver.get("https://web.whatsapp.com/")
sleep(20)

def send_message(number, msg):
    search_box = driver.find_element_by_xpath('//*[@id="side"]/div[1]/div/label/div/div[2]')
    search_box.send_keys(number)
    sleep(2)

    try:
        driver.find_element_by_xpath('//*[@id="pane-side"]/div[1]/div/div/div[11]').click()
    except:
        try:
            driver.find_element_by_xpath('//*[@id="pane-side"]/div[1]/div/div/div[8]').click()
        except:
            print("Xpath error")

    sleep(2)

    msg_box = driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div/div/div[2]/div[1]/div/div[2]')
    msg_box.send_keys(msg)
    msg_box.send_keys(Keys.ENTER)
    return "sent"

df = pd.read_excel('number.xlsx',header=None,names=["number","status"])
number = "+"+str(df["number"][0])

df["status"][0] = send_message(number,"Hello dear...")
print(df)
writer = pd.ExcelWriter('number.xlsx')

df.to_excel(writer, index=False, header=False)
writer.save()


'''
    func:爬取头条街拍美图
    author:monty
    date:2018/11/21
'''

from urllib.parse import urlencode
import requests
from pyquery import PyQuery as pq
import json
import os
from hashlib import md5

base_url='https://www.toutiao.com/search/?'

param={
    'keyword':'街拍'
}

url=base_url+urlencode(param)
# print(url)
headers={
    'Host':'https://www.toutiao.com',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36',
    'referer':url,
    'X-Requested-With':'XMLHttpRequest'
}

def getonePage(offset):
    b_url='https://www.toutiao.com/search_content/?'
    params={
        'offset':str(offset),
        'format':'json',
        'keyword':'街拍',
        'autoload':'true',
        'count':'20',
        'cur_tab':'1',
        'from':'search_tab',
    }

    url=b_url+urlencode(params)
    try:
        reponse=requests.get(url)
        if reponse.status_code==200:
            return reponse.json()
    except requests.ConnectionError as e:
        print('error',e.args)

def parseData(json1):
    if json1:
        data=json1.get('data')
        for item in data:
            data1={}
            if item.get('cell_type'):
                continue
            # data1['abstract']=item.get('abstract')
            data1['article_url']=item.get('article_url')
            data1['title']=item.get('title')
            data1['image_url']=['https:'+i['url'] for i in item.get('image_list')]
            yield data1

#保存图片
def save_image(items):
    for item in items:
        if not os.path.exists('头条/'+item.get('title')):
            os.mkdir('头条/'+item.get('title'))
        for image_url in item.get('image_url'):
            try:
                response = requests.get(image_url)
                if response.status_code == 200:
                    file_path = '{0}/{1}.{2}'.format('头条/'+item.get('title'), md5(response.content).hexdigest(), 'jpg')
                    if not os.path.exists(file_path):
                        with open(file_path, 'wb') as f:
                            f.write(response.content)
                    else:
                        print('Already Downloaded', file_path)
            except requests.ConnectionError:
                print('Failed to Save Image')

if __name__=='__main__':
    for i in range(8):
        json1=getonePage(i*20)
        toutiao=parseData(json1)
        save_image(toutiao)