from flask import render_template, redirect, url_for, flash, request, make_response, jsonify, send_from_directory, send_file
from app import db
from app.models import User, Teacher_Class, Class, Student_Class, Activity_Results, Student_Activity, Misspelled_Word, Class_Configs, Student_Class_Level, Find_Me_Configurations
from flask_login import login_user, logout_user, current_user, login_required
from app.main import bp
from datetime import datetime
import unidecode
import urllib.parse as urlparse
import random
from app.utils import checkForIncorrectCharacters
import requests
from app.doodle.routes import findWordForDoodle
from app.everyday_object_classification.routes import findWord, findCustomWord
from app.image_classification.routes import getCategoryWord, findCategoryWord
from app.create_classifier.routes import findCustomGameWord, getCustomGameWord
import os
import app
import json

languageIndices = {"French": 0, "Spanish": 1}
sports = {"golf":["golf", "golf"],"tennis":["tennis", "tenis"],"football":["foot", "fútbol"],"boxing":["boxe", "boxeo"],"skiing":["ski", "esquí"],"horseriding":["équitation", "equitación"],"basketball":["basket", "baloncesto"],"swimming":["nager", "nadando"],"volleyball":["volley-ball", "voleibol"],"rugby":["rugby", "rugby"]}
emotionsEmojis = {"angry":["fâché", "enojado"],"awkward":["gênant", "torpe"],"annoyed":["agacé", "irritado"],"cool":["impressionnant", "increíble"],"sad":["triste", "triste"],"shock":["choc", "choque"],"love":["amour", "amor"],"tired":["fatigué", "cansado"],"sick":["malade", "enfermos"], "scared":["effrayé", "asustado"], "funny":["marrant", "gracioso"]}
animals = {"bird":["oiseau", "pájaro"],"cat":["chat", "gato"],"cow":["vache", "vaca"],"dog":["chien", "perro"],"frog":["grenouille", "rana"],"hippo":["hipopotame", "hipopótamo"],"horse":["cheval", "caballo"],"lion":["leon", "león"],"monkey":["singe", "mono"], "bear": ["ours", "oso"], "fish":["poisson", "pez"], "pig":["porc", "cerdo"], "sheep": ["mouton", "oveja"], "tiger": ["tigre", "tigre"]}
vehicles = {"aeroplane":["avion", "avión"],"bike":["vélo", "bicicleta"],"bus":["autobus", "autobús"],"car":["voiture", "coche"],"train":["train", "entrenar"],"ferry":["traversier", "transportar"],"helicopter":["hélicoptère", "helicóptero"],"motorbike":["moto", "moto"],"taxi":["taxi", "taxi"], "truck":["camion", "camión"]}
classroom = {"monitor":["moniteur"],"glasses":["lunettes"],"computer":["ordinateur"],"mouse":["souris"],"CD Player":["lecteur CD"],"pencil case":["trousse"],"pencil sharpener":["taille-crayon"],"pen":["stylo"],"bottle":["bouteille"], "chair":["chaise"], "desk":["bureau"], "eraser":["gomme"], "printer":["imprimante"], "projector":["projecteur"], "backpack":["sac à dos"], "clock":["l'horloge"], "ruler":["règle"]}
allVocab = dict(sports)
allVocab.update(emotionsEmojis)
allVocab.update(animals)
allVocab.update(vehicles)
numbersDict = {1: ['un', "uno"], 2: ['deux', "dos"],3: ['trois', "tres"],4: ['quatre', "cuatro"],5: ['cinq', "cinco"],6: ['six', "seis"],7: ['sept', "siete"],8: ['huit', "ocho"],9: ['neuf', "nueve"],10: ['dix', "diez"],11: ['onze', "once"]}
voicesForApplication = {0: "French Female", 1: "Spanish Male"}
translationLanguage = {0: "fr", 1:"es"}

def get_class_vocab(dict):
    config = Class_Configs.query.filter_by(class_id=current_user.current_class).first()
    if config is None:
        return dict
    vocab_data = json.loads(config.vocab_data)
    vocab_data = vocab_data["Scramble"]
    vocab_dict = {}
    userClass = Class.query.filter_by(class_id=current_user.current_class).first_or_404()
    classLanguage = userClass.get_language()
    if classLanguage == "French":
        for eng in vocab_data:
            vocab_dict[eng] = [vocab_data[eng], ""]
    else:
        for eng in vocab_data:
            vocab_dict[eng] = ["" , vocab_data[eng]]

    print(vocab_dict)
    dict.update(vocab_dict)
    return dict

@bp.before_request
def before_request():
    if current_user.is_authenticated:
        current_user.last_seen = datetime.utcnow()
        db.session.commit()

@bp.route('/')
@login_required
def index():
    # print(current_user.current_class)
    if current_user.current_class == None:
        return redirect(url_for("user_management.createPracticeArea"))
    userClass = Class.query.filter_by(class_id=current_user.current_class).first_or_404()
    classLanguage = userClass.get_language()
    languageIndex = getLanguageIndex(classLanguage)
    get_class_vocab(allVocab)
    print(classLanguage)
    resp = make_response(render_template('index.html'))
    resp.set_cookie('word', " ")
    resp.set_cookie('correct', "0")
    resp.set_cookie('numGames', "0")
    resp.set_cookie('activity_id' " ")
    resp.set_cookie('languageIndex', str(languageIndex))
    return resp

def getLanguageIndex(lanaguage):
    return languageIndices[lanaguage]

@bp.route('/difficulty', methods=['GET', 'POST'])
@login_required
def diff():
    if request.method == 'POST':
        selectedClass = Class.query.filter_by(class_id=current_user.current_class).first()
        customGames = selectedClass.get_custom_games()
        config = Class_Configs.query.filter_by(class_id=current_user.current_class).first()
        if config is None:
            resp = make_response(render_template('level.html', config={}, current_level=0, customGames=customGames))
        else:
            currentLevel = current_user.get_current_level()
            configData = {'layout': json.loads(config.layout_data), 'data': json.loads(config.vocab_data),
                          'unlock_data': json.loads(config.unlock_data)}
            resp = make_response(render_template('level.html', config=configData, current_level=currentLevel,customGames=customGames))
        resp.set_cookie('correct', "0")
        resp.set_cookie('numGames', "0")
        resp.set_cookie('activity_id' " ")
        resp.set_cookie('word', " ")
        return resp
    else:
        selectedClass = Class.query.filter_by(class_id=current_user.current_class).first()
        customGames = selectedClass.get_custom_games()
        config = Class_Configs.query.filter_by(class_id=current_user.current_class).first()
        if config is None:
            resp = make_response(render_template('level.html', config={}, current_level=0, customGames=customGames))
        else:
            currentLevel = current_user.get_current_level()
            print(currentLevel)
            configData = {'layout': json.loads(config.layout_data), 'data': json.loads(config.vocab_data),
                          'unlock_data': json.loads(config.unlock_data)}
            resp = make_response(render_template('level.html', config=configData, current_level=currentLevel, customGames=customGames))
        resp.set_cookie('correct', "0")
        resp.set_cookie('numGames', "0")
        resp.set_cookie('activity_id' " ")
        resp.set_cookie('word', " ")
        return resp

@bp.route('/chooseGame', methods=['GET', 'POST'])
@login_required
def chooseGame():
    if request.method == 'POST':
        resp = make_response(render_template('level.html', config={}, current_level=0))
        resp.set_cookie('correct', "0")
        resp.set_cookie('numGames', "0")
        resp.set_cookie('activity_id' " ")
        resp.set_cookie('word', " ")
        return resp
    else:
        resp = make_response(render_template('level.html', config={}, current_level=0))
        resp.set_cookie('correct', "0")
        resp.set_cookie('numGames', "0")
        resp.set_cookie('activity_id' " ")
        resp.set_cookie('word', " ")
        return resp

@bp.route('/checkguess',methods=['POST'])
@login_required
def check():
    print("IN check guess")
    numGames = int(request.cookies.get("numGames"))
    languageIndex = int(request.cookies.get("languageIndex"))
    numGames += 1
    numCorrect = int(request.cookies.get("correct"))
    url = request.referrer
    path = urlparse.urlparse(url).path
    print(path)
    guess = request.form['guess'].strip()
    print(guess + "in check guess")
    word = request.cookies.get('word')
    print(word, "from swipe")
    if path == "/numbersOrSpelling" or path == "/swipeWords":
        word = allVocab[word][languageIndex]
        word = unidecode.unidecode(word)
    print(word + "------------")
    if "/swipe1" in path:
        print("in swipe1")
        category = path.split("/")[2]
        if languageIndex == 0:
            language = "fr"
        else:
            language = "es"
        word = findCategoryWord(word, category, language)
        print(word + " from swipe1")
        print(guess + " from swwipe1")
    if "/customSwipeGame" in path:
        category = path.split("/")[3]
        print(category)
        word = findCustomGameWord(word, category, current_user.current_class)
        print(word, guess)
    guess = guess.lower()
    word = word.lower()

    if guess == word or guess==(word+" "):

        numCorrect += 1
        resp = make_response(render_template('correct.html', path=path, score=[numCorrect, numGames]))
        resp.set_cookie("correct", str(numCorrect))
        resp.set_cookie("numGames", str(numGames))

        return resp
    else:
        resp = make_response(render_template('incorrect.html', path=path, score=[numCorrect, numGames]))
        resp.set_cookie("numGames", str(numGames))
        if path == "/numbersOrSpelling":
            checkForIncorrectCharacters(guess, word)
        return resp

@bp.route('/scramble', methods=['POST', "GET"])
@login_required
def scramble():
    word = request.cookies.get('word')
    active_activity = request.cookies.get("activity_id")
    languageIndex = int(request.cookies.get("languageIndex"))
    if active_activity != "":
        activity_id = active_activity
    else:
        activity_id = request.form["activity_id"]
    # print(word)
    if word == " ":
        key = random.choice(list(allVocab.keys()))
        print(key)

        wordForGame = allVocab[key][languageIndex]
        word = wordForGame
        print(word)

    # else:
    #     wordForGame = allVocab[word][0]

    wordlist = list(word)
    scrwordlist = random.sample(wordlist, len(wordlist))

    scrword = " "

    for i in range(0, len(scrwordlist)):
        scrword = (scrword + scrwordlist[i] + " ")
    print(scrword)
    scramble = {"Your word is....": scrword}

    resp = make_response(render_template('guess.html', scramble=scramble))
    resp.set_cookie('word', word)
    resp.set_cookie('activity_id', activity_id)
    return resp

@bp.route('/tts', methods=['POST', "GET"])
@login_required
def tts():
    word = request.cookies.get('word')
    active_activity = request.cookies.get("activity_id")
    languageIndex = int(request.cookies.get("languageIndex"))
    if active_activity != "":
        activity_id = active_activity
    else:
        print("from form")
        activity_id = request.form["activity_id"]
    print(active_activity, activity_id)
    # print(word)
    if word == " ":
        key = random.choice(list(allVocab.keys()))
        print(key)
        word = key
        print(word)
        wordForGame = allVocab[key][languageIndex]
    else:
        wordForGame = allVocab[word][languageIndex]

    voice = getVoice(languageIndex)

    scramble = {"Your word is....": [wordForGame, voice]}

    resp = make_response(render_template('tts.html', scramble=scramble))
    resp.set_cookie('word', word)
    resp.set_cookie('activity_id', activity_id)
    return resp

@bp.route('/about')
def about():
    return render_template('about.html')

@bp.route('/download')
def downloadUserManual():
    if current_user.role == "Student":
        return send_file('static/manuals/StudentManual.pdf', as_attachment=True)
    return send_file('static/manuals/TeacherManual.pdf', as_attachment=True)

@bp.route('/playAgain', methods=['POST'])
def playAgain():
    print(request.values)
    status = request.form["status"]
    path = request.form["path"]
    print(path)
    if(status == "correct"):
        resp = make_response(redirect(path))
        resp.set_cookie('word', " ")
        return resp
    resp = make_response(redirect(path))
    return resp

@bp.route('/answer',methods=['POST'])
def answer():
    #print(current_user.role)
    word = request.cookies.get('word')
    print(word)
    path = request.form["path"]
    languageIndex = int(request.cookies.get("languageIndex"))
    voice = ""
    english = word

    if path == "/doodle":
        translated, model = findWordForDoodle(word, languageIndex)
    elif path == "/find":
        translated = findWord(word, languageIndex)
    elif path == "/customFind":
        findMeConfig = Find_Me_Configurations.query.filter_by(class_id=current_user.current_class).first()
        translated = findCustomWord(word, findMeConfig.for_loc, findMeConfig.eng_loc)

    elif "findCategory" in path or "swipe1" in path:
        if languageIndex == 0:
            language = "fr"
        else:
            language = "es"
        category = path.split("/")[2]
        translated = findCategoryWord(word, category, language)

    elif "customGame" in path or "customSwipeGame" in path:
        print("customGame")
        translated = findCustomGameWord(word, path.split("/")[3], path.split("/")[2])

    elif path == "/tilesGame":
        print("tiles")
        newVocab = current_user.get_students_new_vocab(current_user.current_class).all()
        englishWords = [vocab.english for vocab in newVocab]
        translations =  [vocab.translation for vocab in newVocab]
        index = englishWords.index(word)
        english = word
        word = translations[index]
        translated = word

    elif path == "/numbersOrSpelling" or path == "/swipeWords":
        english = word
        word = allVocab[word][languageIndex]
        translated = word

    elif path == "/numbers":
        english = word
        translated = numbersDict[int(english)][languageIndex]

    elif word in allVocab:
        translated = allVocab[word][languageIndex]

    else:
        print(word)
        translated = word
        #word = allVocab[word][languageIndex]
        translated = word

    if path == "/speech":
        voice = getVoice(languageIndex)

    if path != "/speech" and path != "/scramble":
        misspelled_word = Misspelled_Word(class_id=current_user.current_class, user_id=current_user.user_id, word=english, translated_word=translated)
        db.session.add(misspelled_word)
        db.session.commit()
    dword = { "Your word was....":word}
    resp = make_response(render_template('answer.html',dword=dword, path=path, voice=voice))
    resp.set_cookie("word", " ")
    return resp

@bp.route("/endgame")
def endGame():
    #Update DB with score and currect user ID
    if(int(request.cookies.get("numGames")) == 0):
        score = 0
    else:
        score = int(request.cookies.get("correct")) / int(request.cookies.get("numGames")) * 100
    activity_id = int(request.cookies.get("activity_id"))
    print(activity_id)
    activity_result = Activity_Results(activity_id=activity_id, score=score, class_id=current_user.current_class)
    db.session.add(activity_result)
    db.session.commit()
    student_activity = Student_Activity(activity_instance_id=activity_result.activity_instance_id, student_id=current_user.user_id)

    db.session.add(student_activity)
    db.session.commit()

    config = Class_Configs.query.filter_by(class_id=current_user.current_class).first()
    if config is not None:
        current_level = current_user.get_current_level()
        active_level = current_user.get_active_level()
        print(current_level, active_level)
        if active_level < current_level:
            ##do update here
            current_user.freeze_stats()
            current_user.update_level(current_level)
            print("Current Level: " + str(current_level))
            if active_level != 3:
                flash("Congratulations, you have completed Course " + str(active_level) + ", you can now see your stats for this course!")
            else:
                flash("Congratulations you have completed all courses in your class! More statistics have been unlocked as well as a Certificate of Completion!")
            return redirect(url_for("user_management.user", username=current_user.username))

    return redirect(url_for("main.index"))

@bp.route("/swipeWords", methods=["POST", "GET"])
@login_required
def swipeWords():
    word = request.cookies.get('word')
    print(word, "empty")
    get_class_vocab(allVocab)
    verbs.update(allVocab)
    active_activity = request.cookies.get("activity_id")
    languageIndex = int(request.cookies.get("languageIndex"))
    if active_activity != "":
        activity_id = active_activity
    else:
        print("from form")
        activity_id = request.form["activity_id"]
    print(active_activity, activity_id)
    print(word)
    if word == " ":
        key = random.choice(list(verbs.keys()))
        print(key, "word")
        word = verbs[key][languageIndex]
        print(word)
        wordForGame = key
    else:
        wordForGame = word
    scramble = {"Your word is....": wordForGame}

    resp = make_response(render_template('swipeWords.html', scramble=scramble))
    print(word, wordForGame)
    resp.set_cookie('word', wordForGame)
    resp.set_cookie('activity_id', activity_id)
    return resp

verbs = {"to go": ["aller", "ir"], "to think": ["penser", "pensar"], "to talk": ["parler", "hablar"]}
from random import shuffle

@bp.route("/getWords/<word>")
def getWords(word):
    get_class_vocab(allVocab)
    verbs.update(allVocab)
    languageIndex = int(request.cookies.get("languageIndex"))
    print(languageIndex)
    word = " ".join(word.split("_"))
    print(word)
    keyWord = verbs[word][languageIndex]
    print(keyWord)
    data = [keyWord]
    for i in range(9):
        print(i)
        data.append(verbs[random.choice(list(verbs.keys()))][languageIndex])
    shuffle(data)
    print(data)
    response = {'data': data}
    return jsonify(response)

@bp.route("/phraseTranslator", methods=['POST', 'GET'])
@login_required
def phraseTranslator():
    languageIndex = int(request.cookies.get("languageIndex"))
    language = getTranslationLanguage(languageIndex)
    return render_template("tagger.html", language=language)

@bp.route("/info", methods=['POST', 'GET'])
def info():
    return render_template("landing_page.html")

def getTranslationLanguage(languageIndex):
    return translationLanguage[languageIndex]

def getVoice(languageIndex):
    return voicesForApplication[languageIndex]


from django.shortcuts import render
from bookstore.models import BookInfo
from django.contrib.auth.models import User

# Create your views here.

def homepage(request):
    books = None
    if request.user.is_authenticated:
        user = request.user
        books = BookInfo.objects.filter(department=user.profile.department)[:10]
    else:
         books = BookInfo.objects.all()[:10]

    return render(request,"index.html",{"page":"home","books":books})

def aboutpage(request):

    user = User.objects.get(username="sale")

    return render(request,"about.html",{"page":"about","author":user})


# gather all the competition information
competition = [int(x) for x in input().split()]
participants = competition[0]
rounds = competition[1]

# initialize empty score and lowest lists
score = [0 for x in range(participants)]
lowest = [0 for x in range(participants)]

# for every round
for i in range(rounds):

    # get the result data from the round
    results = [int(x) for x in input().split()]

    # make a list of the participants order
    order = list()
    for r in range(participants):
        order.append(r + 1)

    # add the participants score
    for a in range(len(results)):
        score[a] += results[a]

    # sort the scores and order
    for j in range(len(results)):
        for y in range(len(results) - 1):
            if results[y] > results[y + 1]:
                tempResults = results[y]
                tempOrder = order[y]
                results[y] = results[y + 1]
                order[y] = order[y + 1]
                results[y + 1] = tempResults
                order[y + 1] = tempOrder

    order.reverse()
    print(results, order)

    # adding the lowest position of a contestant to a list
    for p in range(len(order)):
        lowest[order[p] - 1] = max(lowest[order[p] - 1], p + 1)
    print("lowest", lowest, "score", score)

# initializing the highest score variable
top_score = 0

# finding the highest score variable
for u in range(len(score)):
    top_score = max(top_score, score[u])

# printing the winning contestant(s), score(s), and worst rank(s)
for q in range(len(score)):
    if score[q] == top_score:
        print("Yodeller " + str(q + 1) + " is the TopYodeller: score " + str(score[q]) + ", worst rank " + str(lowest[q]))

print(score, lowest)
# Sample Input
'''
5 2
99 97 100 85 -4
95 97 100 62 1000
'''


import tkinter as tk
import requests
import config

window = tk.Tk()

window.geometry(f"{window.winfo_screenwidth()}x{window.winfo_screenheight()}")

def format_response(weather):
    try:
        name = weather["name"]
        desc = weather["weather"][0]["description"]
        temp = weather["main"]["temp"]
        final = f"""City: {name}\nConditions: {desc}\nTemperature(°C): {str(temp)}"""
    except:
        final = "There was a problem retrieving the data"
    return final

def get_weather(entry):
    url = "https://api.openweathermap.org/data/2.5/weather"
    params = {"APPID": config.key, "q": entry, "units": "metric"}
    response = requests.get(url, params=params)
    weather = response.json()
    label["text"] = format_response(weather)

background_image = tk.PhotoImage(file="mountains5.png")
background_Label = tk.Label(window, image=background_image)
background_Label.pack()

frame = tk.Frame(window, bg="#fcdf03", bd=5)
frame.place(relx=0.1, rely=0.15, relheight=0.07, relwidth=0.8)

entry = tk.Entry(frame, font=("System", 15))
entry.place(relheight=1, relwidth=0.8)

button = tk.Button(frame, text="Get Weather",
                   command=lambda: get_weather(entry.get()))
button.place(relx=0.8, relheight=1, relwidth=0.2)

lower_frame = tk.Frame(window, bg="#fcdf03", bd=5)
lower_frame.place(relx=0.1, rely=0.3, relheight=0.6, relwidth=0.8)

label = tk.Label(lower_frame, font=("Microsoft Himalaya", 30),
                 anchor="nw", justify="left", bd=5)
label.place(relheight=1, relwidth=1)

window.mainloop()


import os
import delta_helpers
import discord
import openai
import requests
from io import BytesIO
import asyncio
import banana_dev as banana
import base64

from dotenv import load_dotenv
from discord.ext import commands

load_dotenv()
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
BANANA_API_KEY = os.getenv("BANANA_API_KEY")
DEBUG = os.getenv("DEBUG")
SD_KEY = os.getenv("SD_KEY")
PASTEL_MIX_KEY = os.getenv("PASTEL_MIX_KEY")
SYSTEM_DETAILS = os.getenv("SYSTEM_DETAILS")
USER_ID = os.getenv("USER_ID")

openai.api_key = OPENAI_API_KEY

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!",intents=intents)

@bot.event
async def on_ready():
    print(f"{bot.user.name} has connected to Discord!")

@bot.event
async def on_message(message):
    if DEBUG != "1" or str(message.author.id) == USER_ID:
        if message.author == bot.user:
            return

        if bot.user.mentioned_in(message):
            asyncio.create_task(generate_reply(message))

        if message.content.startswith("!delta-imagegen"):
            asyncio.create_task(generate_image(message))

        if message.content.startswith("!delta-dalle"):
            asyncio.create_task(generate_dalle(message))

        if message.content.startswith("!delta-pastelgen"):
            asyncio.create_task(generate_pastel_mix(message))

        await bot.process_commands(message)

async def generate_reply(message):
    reply_chain = [
        {"role": "system", "content": f"{SYSTEM_DETAILS} You are a cheerful android that responds to the name Delta, you care very much for your creator and do a lot of errands around your local town for them. She is also fond of using emotes in her replies. Your replies are short and rather to the point. If someone asks you a question then you do your best to reply!"},
        {"role": "user", "content": "Keep your replies short, do not start any reply with 'Delta:', 'Delta Bot:' or anything similar. If you wish to mention someone, you can use <@[USER ID]>, user messages start with [USER NAME]|[USER ID] and the ID for the mention can be pulled from there"}
    ]
    message_channel = message.channel
    current_message = message
    chat_response = ""
    current_chain_length = 3
    async with message_channel.typing():
        try:
            while current_message.reference is not None:
                if current_chain_length % 2 == 1:
                    reply_chain.insert(2, {"role": "user", "content": f"{current_message.author.name}|{current_message.author.id}: {current_message.clean_content}"})
                else:
                    reply_chain.insert(2, {"role": "assistant", "content": f"{current_message.clean_content}"})
                current_message = await message_channel.fetch_message(current_message.reference.message_id)
                current_chain_length = current_chain_length + 1

            if current_chain_length % 2 == 1:
                reply_chain.insert(2, {"role": "user", "content": f"{current_message.author.name}|{current_message.author.id}: {current_message.clean_content}"})
            else:
                reply_chain.insert(2, {"role": "assistant", "content": f"{current_message.clean_content}"})

            input_message = message.content.replace(f"<@!{bot.user.id}>", "").strip()
            chat_output = openai.ChatCompletion.create(
                model="gpt-3.5-turbo",
                messages=reply_chain,
                temperature=0.7
            )
            chat_response = chat_output.choices[0].message.content.replace("Delta: ", "").replace("Delta Bot: ", "")
        except Exception as ex:
            chat_response = f"I'm sorry but there is an issue with processing a reply\nException Message:\n```{str(ex)}```"
            pass
        if len(chat_response) > 2000:
            chunk_size = 1980
            chunks = [chat_response[i:i+chunk_size] for i in range(0, len(chat_response), chunk_size)]
            chunks_processed = chunks.copy()
            i = 0
            while i < len(chunks):
                chunk = chunks[i]
                if i > 0 and delta_helpers.check_unclosed_formatting(chunks[i - 1]):
                    chunk = f"```\n{chunk}"
                    chunks[i] = chunk

                if delta_helpers.check_unclosed_formatting(chunk):
                    chunk = f"{chunk}```"

                chunks_processed[i] = chunk
                i += 1

            num_chunks = len(chunks)
            chunks_with_id = [f"{i+1}/{num_chunks}\n{chunk}" for i, chunk in enumerate(chunks_processed)]

            current_reply = await message.reply(chunks_with_id[0])
            for string_chunk in chunks_with_id:
                if string_chunk != chunks_with_id[0]:
                    current_reply = await current_reply.reply(string_chunk)
        else:
            await message.reply(chat_response)

async def generate_image(message):
    async with message.channel.typing():
        await generate_banana(message, SD_KEY)

async def generate_pastel_mix(message):
    async with message.channel.typing():
        await generate_banana(message, PASTEL_MIX_KEY)

async def generate_banana(message, model_id, width = 768, height = 768):
    try:
        input_message = message.clean_content.replace("!delta-imagegen", "").strip()
        model_inputs = {
        "endpoint": "txt2img",
        "params": {
            "prompt": input_message,
            "steps": 40,
            "sampler_name": "Euler a",
            "cfg_scale": 7.5,
            "seed": None,
            "batch_size": 1,
            "n_iter": 1,
            "width": width,
            "height": height
            }
        }

        api_key = BANANA_API_KEY
        model_key = model_id

        # Run the model
        out = banana.run(api_key, model_key, model_inputs)
        image_base64 = out["modelOutputs"][0]["images"][0]
        image_data = base64.b64decode(image_base64)
        file = discord.File(BytesIO(image_data), filename='image.png')
        await message.reply(file=file)
    except Exception as ex:
        await message.reply(f"Sorry, but your image request could not be completed. This may be due to the image generation returning possibly adult content or an error with my programming.\nException Message:\n```{str(ex)}```")

async def generate_dalle(message):
    async with message.channel.typing():
        try:
            input_message = message.clean_content.replace("!delta-dalle", "").strip()
            response = openai.Image.create(
                prompt=input_message,
                n=1,
                size='1024x1024',
            )
            image_url = response['data'][0]['url']
            image_data = requests.get(image_url).content
            file = discord.File(BytesIO(image_data), filename='image.png')
            await message.reply(file=file)
        except Exception as ex:
            await message.reply(f"Sorry, but your image request could not be completed. This may be due to the image generation returning possibly adult content or an error with my programming.\nException Message:\n```{str(ex)}```")

bot.run(DISCORD_TOKEN)


"""
Basic stats on OSM file (supporting formats managed by osmium).

Usage:
python tools/osm_stats.py [files to analyze]

Example:
python tools/osm_stats.py tests/data/lon6*view1_*.pbf
"""

import os
import sys

import npyosmium

class Counter(npyosmium.SimpleHandler):
    def __init__(self):
        npyosmium.SimpleHandler.__init__(self)
        self.num_nodes = 0
        self.num_ways = 0
        self.num_relations = 0

    # this method runs for each node
    def node(self, n):
        self.num_nodes += 1

    # this method runs for each way
    def way(self, w):
        self.num_ways += 1

    # this method runs for each relation
    def relation(self, r):
        self.num_relations += 1

def main() -> None:
    for file_name in sys.argv[1:]:
        counter = Counter()
        counter.apply_file(file_name)

        print(f"File name: {file_name}")
        print(f"File size: {os.stat(file_name).st_size / 1024:.2f} KiB")
        print(f"Number of nodes : {counter.num_nodes}")
        print(f"Number of ways : {counter.num_ways}")
        print(f"Number of relations: {counter.num_relations}\n")

if __name__ == "__main__":
    main()
    sys.exit(0)


class node(object):

    def __init__(self,value):
        self.node = node
        self.a ={}

    def addOpposite(self, OppositeN): #Adds The Opposite Node
        self.a[OppositeN] = OppositeN

class unweightedGraph: #This creates the Graph to use
    def __init__(self):
        self.v = {}

    def addVertex(self,value): #Adds the vertex
        vertex = node(value)
        self.v[value] = vertex
        print ("Vertex Created " + str(value)) #This prints the vertex and name

    def addEdge(self, value, value2):
        self.v[value].addOpposite(self.v[value2]) #This adds the opposite vertex and connects it to the already connected vertex
        self.v[value2].addOpposite(self.v[value])

    def printGraph(self):
        List = []
        for key in self.v:
            List.append(key)
        for key in List:

            print(str(key))

graph = unweightedGraph()

graph.addVertex(1)
graph.addVertex(2)
graph.addVertex(3)
graph.addVertex(4)

graph.addEdge(1,2)
graph.addEdge(2,3)
graph.addEdge(3,4)

graph.printGraph()


#!/usr/bin/python
#reverse_alignments.py
#Author: Allyson Byrd
#Purpose: Reverse list of alignment coordinates using the length of the segment
	#If input is list of aligned coordinates, list of unaligned coordinates will be returned and vice versa
#Input: 1) aligned.txt: file of nonrepetitive alignment coordinates (likely generated from parse_aligned_file.py)
			#File format: genome	genome_piece	start	end	piece_length
			#i.e. Propionibacterium_acnes_KPA171202.fa	gi|50841496|ref|NC_006085.1|	1	4	2560265
			#File needs to be sorted based on the piece and starting coordinate
			#To sort file: sort -n -k 3 unsorted.txt > sorted.txt
		#2) unaligned.txt: output file of reversed coordinates

#Output: unaligned.txt: file of reversed coordinates

#Usage: python reverse_alignments.py aligned.txt unaligned.txt
	#or python reverse_alignments.py unaligned.txt aligned.txt

#Misc: If the input is aligned sequences, sequences with complete alignements are recorded in a list at the bottom of the outfile

import sys

#Inputs
input = sys.argv[1] #file of condensed coordinates
output = sys.argv[2] #file of reversed coordinated, with list of those completely covered at end

#filled in from input file
pieces_dic = {} #key: strain:piece	#values: list of piece length, then start and stop coordinates [100,(1,5),(7,19),(24,28),...]

#inferred from information in pieces_dic
others_unaligned_dic = {}	#key: strain #values: list of (piece:unalign_start:unalign_end:unalign_length)

FILE = open(input, 'r')

for line in FILE:

	line = line.strip().split('\t')
	strain = line[0]
	piece = line[1]
	start = int(line[2])
	end = int(line[3])
	if len(line) == 5:
		piece_length = int(line[4])

	key = '%s:%s' % (strain, piece)
	if not pieces_dic.has_key(key):
		pieces_dic[key] = [piece_length]

	pieces_dic[key].append((start,end))

	if not others_unaligned_dic.has_key(strain):
		others_unaligned_dic[strain] = []

FILE.close()

previous = ''

#stores only those segments where the whole thing was covered in the input file
#if the input is aligned sequences, sequences in complete_pieces_dic are those sequences which were fully aligned
complete_pieces_dic = {} #key: piece

#works most of the time, except when a reference region aligns to the non reference more than once
for piece in pieces_dic:
	x = 0
	for something in pieces_dic[piece]:
		if x == 0:
			length = something
		else:
			start = something[0]
			end = something[1]
			if start == 1 and end == length:
				#the whole segment aligned
				complete_pieces_dic[piece] = ''
			else:
				if x == 1:
					if start == 1:
						#means the beginning of the sequence aligned
						unalign_start = end + 1
						#print pieces_dic[piece]
						#verifying if there is more than one range listed for a particular piece
						#the unalign end is 1 minus the next start sequence in the range
						if len(pieces_dic[piece]) != 2:
							unalign_end = pieces_dic[piece][x+1][0] -1
						else:
							unalign_end = length
					else:
						#means the beginning of the sequence failed to align
						unalign_start = 1
						unalign_end = start -1
						#need to add something here to add the other end

				unalign_length = unalign_end - unalign_start + 1
				info = '%s:%s:%s:%s' % (piece, unalign_start, unalign_end,unalign_length)
				if not info == previous:
					others_unaligned_dic[strain].append('%s:%s:%s:%s' % (piece, unalign_start, unalign_end,unalign_length))
				previous = info

				if x == len(pieces_dic[piece]) - 1:
				#means this is the last region of overlap
					if end == length:
						#means the end of the sequence aligned
						pass
					else:
						unalign_start = end + 1
						unalign_end = length
				else:
					unalign_start = end + 1
					unalign_end = pieces_dic[piece][x+1][0] -1

				#bt2 alignment 1-based offset into the forward reference strand where leftmost character of the alignment occurs
				unalign_length = unalign_end - unalign_start + 1
				info = '%s:%s:%s:%s' % (piece, unalign_start, unalign_end,unalign_length)
				if not info == previous:
					others_unaligned_dic[strain].append('%s:%s:%s:%s' % (piece, unalign_start, unalign_end,unalign_length))
				previous = info
		x += 1

#writing out the results, segments with complete alignment are listed at the end
OUT = open(output, 'w')

for strain in others_unaligned_dic:
	#print strain
	for range in others_unaligned_dic[strain]:
		#print range
		split_range = range.split(':')
		strain = split_range[0]
		piece = split_range[1]
		start = split_range[2]
		end = split_range[3]
		length = split_range[4]
		OUT.write('%s\t%s\t%s\t%s\t%s\n' % (strain, piece, start, end, length) )

for piece in complete_pieces_dic:
	OUT.write("COMPLETE\t%s\n" % piece)

OUT.close()


"""Implementation of PipelineEnhancer class."""
from typing import List, Union, cast, TypeVar

from py4ai.analytics.ml.core import Enhancer
from py4ai.analytics.ml.core.pipeline import Pipeline, Step
from py4ai.analytics.ml.eda import compose
from py4ai.data.model.text import Document, DocumentsUtilsMixin

TDocumentsUtilsMixin = TypeVar("TDocumentsUtilsMixin", bound=DocumentsUtilsMixin)

class PipelineEnhancer(Enhancer, Pipeline):
    """Enhancer constituted by a sequence of Enhancers."""

    def __init__(self, steps: List[Step]) -> None:
        """
        Class initializer.

        :param steps: list of Enhancer instances that compose the pipeline.
        """
        super(PipelineEnhancer, self).__init__(steps=steps)

    def _validate_steps(self) -> None:
        """
        Check if all steps are Enhancers.

        :raises TypeError: if any of the steps is not an Enhancer
        """
        if any([not isinstance(stage[1], Enhancer) for stage in self.steps]):
            raise TypeError("Each step of the pipeline must be an Enhancer")

    def apply(
        self, documents: Union[Document, TDocumentsUtilsMixin]
    ) -> Union[Document, TDocumentsUtilsMixin]:
        """
        Sequentially enhance documents.

        :param documents: documents to enhance
        :return: enhanced documents
        """
        composer = compose(
            *list(
                map(
                    lambda x: lambda y: cast(Enhancer, x).enhance(y),
                    reversed(self.steps),
                )
            )
        )
        return composer(object)


# [SublimeLinter @python:3]

from ..utils import echo

def patch(csv_file, patch_file, reverse=False, dry_run=False, force=False):
    read_count = 0
    applied_count = 0

    with open(csv_file, "r+b") as fp_csv, \
            open(patch_file, "r") as fp_patch:
        for line in fp_patch:
            if line.startswith("#"):
                continue

            try:
                offset, expected_code, replace_code = map(int, line.rstrip().split(":"))
            except ValueError:
                echo("ERROR: invalid patch file")
                return 1

            if reverse:
                expected_code, replace_code = replace_code, expected_code

            fp_csv.seek(offset)
            char = fp_csv.read(1)

            if not char:
                echo("ERRPR: patch is not applicable")
                return 1

            char_code = ord(char)
            read_count += 1

            if char_code == replace_code:
                continue

            if char_code != expected_code:
                echo("ERROR: unexpected char at", offset)

                if not force:
                    return 1

            fp_csv.seek(offset)

            if not dry_run:
                fp_csv.write(bytes((replace_code, )))

            applied_count += 1

    echo("Replacements applied: {} / {}".format(applied_count, read_count))
    return 0


from flask import Flask
import ml
import api

app = Flask(__name__)

rainRecord = [18.5, 0.7, 0, 0, 0, 0, 0, 0, 103, 19.1]
rainRecordIdx = 0

predRecord = [1.5, 1.3, 1.9,  2.3,  3.4,  5.4,  1.2, 1.3, 5.6,   8.1,  9.3,  10.2, 11.4,  9.5,  8.7,  7.3, 6.5, 4.3, 2.1, 1.3]

@app.route("/rain", methods=['GET'])
def getRain():
  #########
  global rainRecordIdx
  res = api.getRainFromDummy(rainRecordIdx)
  rainRecordIdx += 1
  #########
  # res = api.getRainFromApi()
  rainRecord.pop(0)
  rainRecord.append(res)

  # prediction = ml.fl.predict(rainRecord)
  prediction = [predRecord[rainRecordIdx%20]]
  return str(res) + "," + str(prediction[0])

@app.route("/waterlevel", methods=['GET'])
def getWaterlevel():
  prediction = ml.fl.predict(rainRecord)
  print(rainRecord)
  return str(prediction[0])


from architectures.celeba_net import CelebaNet
from architectures.fashion_mnist import FashionMnistNet
from architectures.mnist_net import MnistNet
# from architectures.mnist_net_pate import MnistNetPate
from architectures.mnist_net_pate_timing import MnistNetPate
from architectures.resnet import ResNet10, ResNet12, ResNet14, ResNet16, \
    ResNet18
from architectures.retino_net import RetinoNet, SimpleRetinoNet
from architectures.small_resnet import ResNet8
from architectures.tiny_resnet import ResNet6
from architectures.vggs import VGG
from datasets.deprecated.chexpert.model.classifier import SingleClassClassifier
from datasets.deprecated.coco.models.tresnet import tresnet_m
from datasets.deprecated.coco.models.tresnet import tresnet_l
from datasets.deprecated.coco.models.tresnet import tresnet_xl
from architectures.densenet import densenet121_capc
from architectures.densenet_xray import get_densenet121_xray
from architectures.densenet_pre import densenetpre
from architectures.resnet_pre import resnetpre
from models.utils_models import get_model_type_by_id, get_model_name_by_id
from architectures.PascalNetwork import *

def get_private_model(name, model_type, args):
    """Private model held by each party."""
    if args.use_pretrained_models == True:
        if model_type == 'densenet121_cxpert':
            model = densenetpre()
            model.dataset = 'cxpert'
        elif model_type == 'resnet50':
            model = resnetpre()
            model.dataset == 'pascal'
    elif model_type.startswith('VGG'):
        model = VGG(name=name, args=args, model_type=model_type)
    elif model_type == 'ResNet6':
        model = ResNet6(name=name, args=args)
    elif model_type == 'ResNet8':
        model = ResNet8(name=name, args=args)
    elif model_type == 'ResNet10':
        model = ResNet10(name=name, args=args)
    elif model_type == 'ResNet12':
        model = ResNet12(name=name, args=args)
    elif model_type == 'ResNet14':
        model = ResNet14(name=name, args=args)
    elif model_type == 'ResNet16':
        model = ResNet16(name=name, args=args)
    elif model_type == 'ResNet18':
        model = ResNet18(name=name, args=args)
    elif model_type == 'resnet50':
        return ResNet(Bottleneck, num_classes=args.num_classes)
    elif model_type.startswith('chexpert'):
        model = SingleClassClassifier(name=name, args=args)
    elif model_type == 'MnistNet':
        model = MnistNet(name=name, args=args)
    elif model_type == 'MnistNetPate':
        model = MnistNetPate(name=name, args=args)
    elif model_type == 'FashionMnistNet':
        model = FashionMnistNet(name=name, args=args)
    elif model_type == 'RetinoNet':
        model = RetinoNet(name=name, args=args)
    elif model_type == 'SimpleRetinoNet':
        model = SimpleRetinoNet(name=name, args=args)
    elif model_type == 'CelebaNet':
        model = CelebaNet(name=name, args=args)
    elif model_type == 'tresnet_m':
        model = tresnet_m(name=name, args=args)
    elif model_type == 'tresnet_l':
        model = tresnet_l(name=name, args=args)
    elif model_type == 'tresnet_xl':
        model = tresnet_xl(name=name, args=args)
    elif model_type == 'densenet121_capc':
        model = densenet121_capc(name=name, args=args)
    elif model_type == 'densenet121_mimic':
        model = get_densenet121_xray(name=name, args=args)
        model.dataset = 'mimic'
    elif model_type == 'densenet121_cxpert':
        model = get_densenet121_xray(name=name, args=args)
        model.dataset = 'cxpert'
    elif model_type == 'densenet121_padchest':
        model = get_densenet121_xray(name=name, args=args)
        model.dataset = 'padchest'
    elif model_type == 'densenet121_vin':
        model = get_densenet121_xray(name=name, args=args)
        model.dataset = 'vin'
    elif model_type in ['densenet121_' + str(x) for x in args.xray_datasets]:
        assert args.dataset in args.xray_datasets
        model = get_densenet121_xray(name=name, args=args)
    else:
        raise Exception(f'Unknown architecture: {model_type}')

    # Set the attributes if not already set.
    if getattr(model, 'dataset', None) == None:
        model.dataset = args.dataset
    if getattr(model, 'model_type', None) == None:
        model.model_type = model_type

    return model

def get_private_model_by_id(args, id=0):
    model_type = get_model_type_by_id(args=args, id=id)
    name = get_model_name_by_id(id=id)
    model = get_private_model(name=name, args=args, model_type=model_type)
    return model


from email import header
from email.policy import default
from textwrap import indent

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   evaluate_realtions.py
@Time    :   2022/03/08 10:51:08
@Author  :   Xixin Hu
@Version :   1.0
@Contact :   xixinhu97@foxmail.com
@Desc    :   None
'''

# here put the import lib

import json
from typing import DefaultDict
from pkg_resources import run_script

from tqdm import tqdm
import csv
import sys
import os
# print(sys.path)
sys.path.append(os.getcwd())
# sys.path.append("..") # CWQ dir

from components.utils import dump_json, load_json
import torch
from tqdm import tqdm
import pandas as pd

def gen_rel_match_data_set(split, filter_base=True):
    """make sentence-relation pair for cross-encoder training"""
    res_lines = []

    if filter_base:
        relation_data_file_name = f'data/rel_match/CWQ_{split}_relation_data_old.json'
    else:
        relation_data_file_name = f'data/rel_match/CWQ_{split}_relation_data.json'

    train_relation_map = load_json(f'data/label_maps/CWQ_{split}_label_maps.json')

    print(f'Loading {split}...')
    with open(relation_data_file_name,'r') as f:
        databank = json.load(f)
        print(len(databank))
        idx =0
        for qid,data in tqdm(databank.items(),desc=f'Processing {split}'):

            if split=='train': # for training
                for rel in data['positive_rels']:
                    if filter_base: # filter base relation
                        if rel.startswith('base.'):
                            continue

                    if '\t' in rel: # process illegal relation
                        rel = rel.split('\t')[0]
                    line = str(idx)+"\t"+data['question'] + '\t' + rel+'\t'+'1'
                    res_lines.append(line+'\n')
                    idx+=1
                for rel in data['negative_rels']:
                    if '\t' in rel:
                        rel = rel.split('\t')[0]
                    line = str(idx)+"\t"+data['question'] + '\t' + rel+'\t'+'0'
                    res_lines.append(line+'\n')
                    idx+=1
            else:
                added_rels = set()
                for rel in data['cand_rels']: # take cand_rels
                    if filter_base: # filter base relation
                        if rel.startswith('base.'):
                            continue

                    if '\t' in rel: # process illegal relation
                        rel = rel.split('\t')[0]
                    added_rels.add(rel)
                    if rel in data['positive_rels']:
                        line = str(idx)+"\t"+data['question'] + '\t' + rel+'\t'+'1'
                    else:
                        line = str(idx)+"\t"+data['question'] + '\t' + rel+'\t'+'0'
                    res_lines.append(line+'\n')
                    idx+=1

                for rel in train_relation_map.keys(): # add rels in train set
                    if rel not in added_rels:
                        added_rels.add(rel)
                        if '\t' in rel: # process illegal relation
                            rel = rel.split('\t')[0]
                        if rel in data['positive_rels']:
                            line = str(idx)+"\t"+data['question'] + '\t' + rel+'\t'+'1'
                        else:
                            line = str(idx)+"\t"+data['question'] + '\t' + rel+'\t'+'0'
                        res_lines.append(line+'\n')
                        idx+=1

    with open(f'data/rel_match/CWQ_{split}_rel_match.tsv','w') as f:
        f.writelines(res_lines)

def make_pseudo_sorted_dataset(split):
    pseudo_sorted_data = {}
    rel_data_bank = load_json(f'data/rel_match/CWQ_{split}_relation_data.json')
    for qid,data in tqdm(rel_data_bank.items(),desc='Building pseudo cand rel set'):
        pos_rels = data['positive_rels']
        neg_rels = data['negative_rels']
        pseudo_sorted_data[qid] = pos_rels+neg_rels

    dump_json(pseudo_sorted_data,f'data/rel_match/CWQ_{split}_cand_rels_sorted.json',indent=4)

def make_sorted_dataset(split, from_cand=False):
    # with open('logits.pt','r') as f:

    logits = torch.load(f'data/rel_match/predict/{split}/logits.pt',map_location=torch.device('cpu'))

    # print(logits)
    # print(len(logits))
    logits_list = list(logits.squeeze().numpy())
    print('Logits len:',len(logits_list))

    split_tsv = f'CWQ_{split}_rel_match_old.tsv'

    # with open(split_tsv,'r') as f:
    #     tsv_lines = f.readlines()
    # print('TSV lines:', len(tsv_lines))

    split_df = pd.read_csv(split_tsv, header=None, delimiter='\t'
                            ,names=['id','question','rel','label']
                            ,error_bad_lines=True
                            ,quoting=csv.QUOTE_NONE)
                            #,dtype={"id":int, "question":str, "rel":str, 'label':int}

    print('Dataframe len:',len(split_df))

    # length check
    assert len(logits_list) == len(split_df)

    enumerate_cand_rel_bank  = load_json(f'data/rel_match/CWQ_{split}_relation_data_old.json')

    # with open('logits.txt','w') as f:
    #     for logit in tqdm(logits_list):
    #         f.write(str(logit)+'\n')

    split_dataset = f'data/origin/ComplexWebQuestions_{split}.json'
    if split=='test_new':
        split_dataset= 'data/origin/ComplexWebQuestions_test.json'
    split_dataset = load_json(split_dataset)
    question2id = {x['question']:x['ID'] for x in split_dataset}

    # cand_rel_bank = {} # Dict[Question, List[Relation:logit]]
    cand_rel_bank = DefaultDict(dict)
    cand_rel_bank_from_enumerate = {}
    for idx,logit in tqdm(enumerate(logits_list),total=len(logits_list),desc=f'Reading logits {split}'):
        logit = float(logit)
        question = split_df.loc[idx]['question']
        rel = split_df.loc[idx]['rel']
        cwq_id = question2id.get(question,None)

        if not cwq_id:
            continue
        else:
            if not from_cand:
                cand_rel_bank[cwq_id][rel]=logit
            else:
                # only retain the relations in cand rels
                if cwq_id in cand_rel_bank_from_enumerate:
                    cand_rel_enumreate = cand_rel_bank_from_enumerate[cwq_id]
                else:
                    cand_rel_enumreate = set(enumerate_cand_rel_bank[cwq_id]['cand_rels'])
                    cand_rel_bank_from_enumerate[cwq_id] = cand_rel_enumreate

                if rel in cand_rel_enumreate: # only retain the relations in cand rels
                    cand_rel_bank[cwq_id][rel]=logit

    cand_rel_file_name = f'data/rel_match/CWQ_{split}_cand_rels_from_cand.json' if from_cand else f'data/rel_match/CWQ_{split}_cand_rels.json'
    dump_json(cand_rel_bank,cand_rel_file_name,indent=4)

    final_candRel_map = {} # Dict[Question,List[Rel]]   sorted by logits

    for ori_data in tqdm(split_dataset,total=len(split_dataset),desc=f'Sort by logits {split}'):
        qid = ori_data['ID']
        cand_rel_map = cand_rel_bank.get(qid,None)
        if not cand_rel_map:
            final_candRel_map[qid]=[]
        else:
            cand_rel_list = list(cand_rel_map.keys())
            cand_rel_list.sort(key=lambda x:float(cand_rel_map[x]),reverse=True)
            final_candRel_map[qid]=cand_rel_list

    sorted_cand_rel_name = f'data/rel_match/CWQ_{split}_cand_rels_sorted.json' if not from_cand else f'data/rel_match/CWQ_{split}_cand_rels_sorted_from_cand.json'
    dump_json(final_candRel_map,sorted_cand_rel_name,indent=4)

def check_sorted_dataset_coverage(split,sorted_rel_file):

    sorted_rel_bank = load_json(sorted_rel_file)
    label_map_file = f'data/label_maps/CWQ_{split}_label_maps.json'
    if split=='test_new':
        label_map_file = f'data/label_maps/CWQ_test_label_maps.json'
    label_maps_bank = load_json(label_map_file)

    gt_0_pre = 0
    gt_0_recall = 0
    top1_pre = 0
    top1_recall = 0
    top3_pre = 0
    top3_recall = 0
    top4_pre = 0
    top4_recall = 0
    top5_pre = 0
    top5_recall = 0
    top10_pre = 0
    top10_recall = 0
    total = len(sorted_rel_bank)
    for qid in sorted_rel_bank:
        cand_rel_list = sorted_rel_bank[qid]
        gold_rel_set = label_maps_bank[qid]['rel_label_map'].keys()

        top1_cand = set([x[0] for x in cand_rel_list[:1]])
        top3_cand = set([x[0] for x in cand_rel_list[:3]])
        top4_cand = set([x[0] for x in cand_rel_list[:4]])
        top5_cand = set([x[0] for x in cand_rel_list[:5]])
        top10_cand = set([x[0] for x in cand_rel_list[:10]])

        gt_0_cand = set([x[0] for x in cand_rel_list if x[1]>=0])
        if not gt_0_cand:
            gt_0_cand = set([x[0] for x in cand_rel_list[:1]])

        gt_0_pre += len(gt_0_cand & gold_rel_set) / len(gt_0_cand) if gt_0_cand else 0
        gt_0_recall += len(gt_0_cand & gold_rel_set) / len(gold_rel_set) if gold_rel_set else 0
        gt_0_f1 = 2*gt_0_pre*gt_0_recall/(gt_0_pre+gt_0_recall) if (gt_0_pre+gt_0_recall)>0 else 0

        top1_pre += len(top1_cand & gold_rel_set) / len(top1_cand) if top1_cand else 0
        top1_recall += len(top1_cand & gold_rel_set) / len(gold_rel_set) if gold_rel_set else 0
        top1_f1 = 2*top1_pre*top1_recall/(top1_pre+top1_recall) if (top1_pre+top1_recall)>0 else 0

        top3_pre += len(top3_cand & gold_rel_set) / len(top3_cand) if top3_cand else 0
        top3_recall += len(top3_cand & gold_rel_set) / len(gold_rel_set) if gold_rel_set else 0
        top3_f1 = 2*top3_pre*top3_recall/(top3_pre+top3_recall) if (top3_pre+top3_recall)>0 else 0

        top4_pre += len(top4_cand & gold_rel_set) / len(top4_cand) if top4_cand else 0
        top4_recall += len(top4_cand & gold_rel_set) / len(gold_rel_set) if gold_rel_set else 0
        top4_f1 = 2*top4_pre*top4_recall/(top4_pre+top4_recall) if (top4_pre+top4_recall)>0 else 0

        top5_pre += len(top5_cand & gold_rel_set) / len(top5_cand) if top5_cand else 0
        top5_recall += len(top5_cand & gold_rel_set) / len(gold_rel_set) if gold_rel_set else 0
        top5_f1 = 2*top5_pre*top5_recall/(top5_pre+top5_recall) if (top5_pre+top5_recall)>0 else 0

        top10_pre += len(top10_cand & gold_rel_set) / len(top10_cand) if top10_cand else 0
        top10_recall += len(top10_cand & gold_rel_set) / len(gold_rel_set) if gold_rel_set else 0
        top10_f1 = 2*top10_pre*top10_recall/(top10_pre+top10_recall) if (top10_pre+top10_recall)>0 else 0

    print(split,'TOTAL: ',total)
    print(f'GT 0 pre: {gt_0_pre/total}, GT 0 recall: {gt_0_recall/total}, GT 0 f1: {gt_0_f1/total}')
    print(f'TOP 1 pre: {top1_pre/total}, TOP 1 recall: {top1_recall/total}, TOP 1 f1: {top1_f1/total}')
    print(f'TOP 3 pre: {top3_pre/total}, TOP 3 recall: {top3_recall/total}, TOP 3 f1: {top3_f1/total}')
    print(f'TOP 4 pre: {top4_pre/total}, TOP 4 recall: {top4_recall/total}, TOP 4 f1: {top4_f1/total}')
    print(f'TOP 5 pre: {top5_pre/total}, TOP 5 recall: {top5_recall/total}, TOP 5 f1: {top5_f1/total}')
    print(f'TOP 10 pre: {top10_pre/total}, TOP 10 recall: {top10_recall/total}, TOP 10 f1: {top10_f1/total}')

def check_cand_dataset_coverage(split):
    """Check the recall of candidate relations"""
    data_bank = load_json(f'data/rel_match/CWQ_{split}_relation_data_old.json')
    cand_recall = 0
    for qid,data in data_bank.items():
        positive_rels = data['positive_rels']
        negative_rels = data['negative_rels']
        cand_rels = data['cand_rels']

        cand_recall += len(set(cand_rels)& set(positive_rels))/len(set(positive_rels))

    print(f'{split} cand recall: {cand_recall/len(data_bank)}')

def make_sorted_dataset_from_logits(split, logits_file, tsv_file, output_dir):

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    logits = torch.load(logits_file,map_location=torch.device('cpu'))

    # print(logits)
    # print(len(logits))
    logits_list = list(logits.squeeze().numpy())
    print('Logits len:',len(logits_list))

    tsv_df = pd.read_csv(tsv_file, delimiter='\t',dtype={"id":int, "question":str, "relation":str, 'label':int})
                            #quoting=csv.QUOTE_NONE,

    print('Tsv len:', len(tsv_df))
    # print(tsv_df.head())
    print('Question Num:',len(tsv_df['question'].unique()))

    assert(len(logits_list)==len(tsv_df))

    split_dataset = load_json(f'data/origin/ComplexWebQuestions_{split}.json')
    question2id = {x['question']:x['ID'] for x in split_dataset}

    # cand_rel_bank = {} # Dict[Question, List[Relation:logit]]
    cand_rel_bank = DefaultDict(dict)
    for idx,logit in tqdm(enumerate(logits_list),total=len(logits_list),desc=f'Reading logits {split}'):
        logit = float(logit)
        question = tsv_df.loc[idx]['question']
        rel = tsv_df.loc[idx]['relation'].split("|")[0]
        cwq_id = question2id.get(question,None)

        if not cwq_id:
            print(question)
            cand_rel_bank[cwq_id]= {}
        else:
            cand_rel_bank[cwq_id][rel]=logit

    cand_rel_logit_map = {}
    for qid in tqdm(cand_rel_bank,total=len(cand_rel_bank),desc='Sorting rels...'):
        cand_rel_maps = cand_rel_bank[qid]
        cand_rel_list = [(rel,logit) for rel,logit in cand_rel_maps.items()]
        cand_rel_list.sort(key=lambda x:x[1],reverse=True)

        cand_rel_logit_map[qid]=cand_rel_list

    dump_json(cand_rel_logit_map,os.path.join(output_dir,f'CWQ_{split}_cand_rel_logits.json'),indent=4)

    final_candRel_map = DefaultDict(list) # Dict[Question,List[Rel]]   sorted by logits

    for ori_data in tqdm(split_dataset,total=len(split_dataset),desc=f'{split} Dumping... '):
        qid = ori_data['ID']
        # cand_rel_map = cand_rel_bank.get(qid,None)
        cand_rel_list = cand_rel_logit_map.get(qid,None)
        if not cand_rel_list:
            final_candRel_map[qid]=[]
        else:
            # cand_rel_list = list(cand_rel_map.keys())
            # cand_rel_list.sort(key=lambda x:float(cand_rel_map[x]),reverse=True)
            final_candRel_map[qid]=[x[0] for x in cand_rel_list]

    sorted_cand_rel_name = os.path.join(output_dir,f'CWQ_{split}_cand_rels_sorted.json')
    dump_json(final_candRel_map,sorted_cand_rel_name,indent=4)

if __name__=='__main__':

    output_dir = 'data/rel_match/relations/sorted_results_new'
    # split = 'dev'
    for split in ['dev']:
        logits_file = f'data/rel_match/relations/predictions_new/{split}/logits.pt'
        tsv_file = f'data/rel_match/relations/biEncoder_top200_new/CWQ.{split}.biEncoder.train_all.richRelation.top200.tsv'
        make_sorted_dataset_from_logits(split, logits_file, tsv_file, output_dir)

    # split = 'test'
    for split in ['dev']:
        sorted_logit_file_name = os.path.join(output_dir,f'CWQ_{split}_cand_rel_logits.json')
        check_sorted_dataset_coverage(split,sorted_logit_file_name)

    # for split in ['train','test','dev']:
    #     # gen_rel_match_data_set(split)
    #     check_cand_dataset_coverage(split)

    # gen_rel_match_data_set('test')

    # make_pseudo_sorted_dataset('dev')
    # make_pseudo_sorted_dataset('train')
    # for split in ['test','dev','train']:
    #     make_sorted_dataset(split)

    # 根据logits重建排序后的候选关系
    # make_sorted_dataset('test_new')
    # make_sorted_dataset('test',from_cand=True)
    # make_sorted_dataset('dev',from_cand=True)
    # make_sorted_dataset('train',from_cand=True)

    # 检查排序后的候选关系的覆盖率
    # check_sorted_dataset_coverage('test')
    # check_sorted_dataset_coverage('dev')
    # check_sorted_dataset_coverage('train')
    # check_sorted_dataset_coverage('test_new')

    # check_sorted_dataset_coverage('dev')
    # check_sorted_dataset_coverage('train')


def app1():
    import streamlit as st
    import subprocess
    import time
    import json
    import csv
    import random
    session_token = random.randint(0,10000)

    with open("../Assets/Catalogs/size_catalog.json", "r") as size_catalog:
        size_dict=json.load(size_catalog)

    with open("../Assets/Catalogs/brand_catalog.json", "r") as brand_catalog:
        brand_dict=json.load(brand_catalog)

    with open("../Assets/Catalogs/category_catalog.json", "r") as category_catalog:
        category_dict=json.load(category_catalog)

    with open("../Assets/Catalogs/color_catalog.json", "r") as color_catalog:
        color_dict = json.load(color_catalog)

    # Initialize an empty dictionary to store your data
    collection_dict = {}

    # Open your CSV file
    with open('../Assets/Data/item_quantities_per_tags_and_collections.csv', 'r', encoding='utf-8') as f:
        # Use the csv library to read the file
        reader = csv.DictReader(f)

        # Loop through each row in the file
        for row in reader:
            # Extract the title
            title = row["Title"]
            id = row["ID"]

            # Add the new dictionary to the item quantities dictionary
            collection_dict[title] = id

    query = st.text_input("Quel vêtement cherchez-vous ?")
    selected_brand = st.multiselect("Quelles marque(s)", list(brand_dict.keys()))
    selected_category = st.multiselect("Quelle catégorie (une seule)", list(category_dict.keys()))
    pieces_a_chercher = st.number_input("Combien de pièces souhaitez-vous ?", min_value=1, step=1)
    selected_sizes = st.multiselect("Sélectionnez une ou plusieurs tailles", list(size_dict.keys()))
    selected_colors = st.multiselect("Sélectionnez une ou plusieurs couleurs", list(color_dict.keys()))
    selected_collection = st.selectbox("Dans quelle catégorie iront les produits ?",list(collection_dict.keys()))

    # Create an empty placeholder element for the progress bar
    progress_placeholder = st.empty()

    # Create an empty placeholder element for the status message
    status_placeholder = st.empty()

    if st.button("Lancer la recherche"):
        if pieces_a_chercher and selected_category:
            brand_ids = [brand_dict[brand] for brand in selected_brand]
            category_ids = [category_dict[category] for category in selected_category]
            size_ids = [size_dict[size] for size in selected_sizes]
            color_ids = [color_dict[color]["id"] for color in selected_colors]
            collection = collection_dict[selected_collection]

            base_url = "https://www.vinted.fr/catalog?{}{}{}{}{}"

            query = "search_text={}".format(query)

            parameters1 = ""
            if color_ids:
                parameters1 = "".join([f"&color_ids[]={color_id}" for color_id in color_ids])

            parameter2 = f"&catalog[]={','.join(map(str, category_ids))}"

            parameters3 = ""
            if brand_ids:
                parameters3 = "".join([f"&brand_id[]={brand_id}" for brand_id in brand_ids])

            parameters4 = "".join([f"&size_id[]={size_id}" for size_id in size_ids])

            site = base_url.format(query, parameters1, parameter2, parameters3, parameters4)

            try:
                cmd = ['python', '../Utilitaries/vinted_scraping_script.py', site, str(pieces_a_chercher), str(query),
                       str(session_token), str(selected_collection), str(collection)]
                process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            except Exception as e:
                st.error(f"An error occurred while running the script: {str(e)}")

            while process.poll() is None:
                with open("../Assets/Data/progress_bar_data.txt", "r") as f:
                    progress = float(f.read().strip())
                    normalized_progress = progress / 100
                    progress_placeholder.progress(normalized_progress)
                    time.sleep(1)

            status_placeholder.success("Script B has finished.")
            st.balloons()

            stdout, stderr = process.communicate()
            if stderr:
                st.error(f"An error occurred during the subprocess:\n{stderr}")
            else:
                st.success(f"Subprocess completed successfully. Output:\n{stdout}")


###2DO########
# Create a script split_nums.py that will:
#
# ask the user for a number
# split the given number in halves (e.g. 123456 -> split to 123 and 456, 12345 -> 12 and 345)
# convert both halves into an integer
# if both halves are an even integer, print: 'Success' - e.g. 12 and 34
# if only the first part is even, print: 'First' - e.g. 12 and 345
# if the second part is even, print: 'Second' - e.g. 123 and 456
# if neither of the numbers is even print: 'Neither' - 123 and 455
# if nothing has been entered (the user just hit Enter), print: 'No input provided'
#########

num=input('Please, enter number:')
if num:
    num_1st = int(num[:(len(num)//2)])
    num_2nd = int(num[(len(num)//2):])
    print('Number splitted into: ' + str(num_1st) +' ' + str(num_2nd))

    num_1st_res = bool(num_1st % 2)     # 1=True=odd, 0=False=even
    num_2nd_res = bool(num_2nd % 2)

    if not num_1st_res and not num_2nd_res: #better to read >> if 0 == num_1st % 2 and 0 == num_2nd % 2
        print('Success (both even)')
    elif num_1st_res and num_2nd_res:
        print('Neither (both are odd) ')
    elif not num_1st_res:
        print('First part is even')
    else:
        print('Second is even')
else:
    print('No input provided')


import requests
import json
import pyautogui

# 검색어 입력
keyword = pyautogui.prompt('검색어를 입력하세요!')

addition = ['ㄱ', 'ㄴ', 'ㄷ', 'ㄹ', 'ㅁ', 'ㅂ',
            'ㅅ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']

with open('naver_keywords.txt', 'w', encoding='utf8') as f:
    f.write(f'***** {keyword} ㄱ~ㅎ 연관 검색어 출력 *****\n')
    for idx in range(len(addition)):
        response = requests.get(
            f'https://ac.search.naver.com/nx/ac?q={keyword + addition[idx]}&con=1&frm=nv&ans=2&r_format=json&r_enc=UTF-8&r_unicode=0&t_koreng=1&run=2&rev=4&q_enc=UTF-8&st=100&_callback=_jsonp_4')

        text = response.text

        doc = json.loads(text.split('(')[1][:-1])

        items = doc['items'][0]

        result = list(map(lambda x: x[0], items))

        for i in range(len(result)):
            f.write(f'{(idx * 10) + i + 1}) {result[i]}\n')


def solution(n):
    answer = [[0]*n for _ in range(n)]

    dx = [0, 1, 0, -1]
    dy = [1, 0, -1, 0]

    tmp=1
    index=0
    x,y=0,0
    answer[x][y]=tmp
    while tmp<n*n:
        nx=x+dx[index]
        ny=y+dy[index]

        if 0>nx or nx>=n or 0>ny or ny>=n:
            index=(index+1) % 4
            continue

        if answer[nx][ny]==0:
            tmp+=1
            answer[nx][ny]=tmp
            x,y=nx,ny
        else:
            index=(index+1) % 4

    return answer

print(solution(6))

프로그래머스/_소용돌이.py


#Define DateEncode as basic features such as day of week, month int, and year int.
#Define groupencode as placing into buckets
#Define mapencode as mapping values to a higher-level grouping

#Define relavent fields to model and their associated data types and feature engineering methods

colDict = {
    "COMP_CODE": {"Type":"Categorical", "Method":"1hot"}, #In
    #"REF_KEY1":{"Type":"Categorical", "Method":"1hot"},
    "AC_DOC_NR": {"Type":"ID", "Method":"N/A"},
    "PSTNG_DATE":{"Type":"Date", "Method":"DateEncode"},
    #"CREATEDON":{"Type":"Date", "Method":"DateEncode"},
    "DOC_DATE":{"Type":"Date", "Method":"DateEncode"},
    "PROFIT_CTR":{"Type":"Categorical", "Method":"1hot"},
    "AC_DOC_TYP":{"Type":"Categorical", "Method":"1hot"},
    "POST_KEY":{"Type":"Categorical", "Method":"1hot"},
    "V.VENDOR":{"Type":"Categorical", "Method":"MapEncode"},#Was creditor
    "V.NAME":{"Type":"Text", "Method":"Tokenize"}, #Was name
    "GL_ACCOUNT":{"Type":"Categorical", "Method":"1hot"},
    #"CUSTOMER":{"Type":"Categorical", "Method":"MapEncode"}, no longer included
    "POSTXT":{"Type":"Text", "Method":"Tokenize"},
    #"BIC_ZUSERNAM":{"Type":"Categorical", "Method":"MapEncode"}, no longer included
    "ZBKTXT":{"Type":"Text", "Method":"Tokenize"},
    "ZTCODE":{"TYpe":"Categorical", "Method":"1hot"},
    "DEB_CRE_USD":{"Type":"Numerical", "Method":"GroupEncode"},
    "V.BIC_ZTERMPAY":{"Type":"Categorical", "Method":"1hot"},
    "MATL_GROUP":{"Type":"Categorical", "Method":"1hot"},
    "ACCNT_GRPV":{"Type":"Categorical", "Method":"1hot"},
    "HCPClass":{"Type":"Categorical", "Method":"LabelEncode"}, #Target
    "Compliance Bucket":{"Type":"Categorical", "Method":"1hot"},
    "V.BI_ACCNT_TGRPV":{"Type":"Categorical", "Method":"1hot"} #Vendor type
}

oneHotFeatures = ["PROFIT_CTR","AC_DOC_TYP","POST_KEY","GL_ACCOUNT","ZTCODE", "MATL_GROUP", "ACCNT_GRPV","V.BI_ACCNT_TGRPV", "V.BIC_ZTERMPAY"]

#Set pandas datatypes for each field
dfValidateValidate['COMP_CODE'] = dfValidateValidate['COMP_CODE'].astype('object')
#dfValidateValidate['REF_KEY1'] = dfValidateValidate['REF_KEY1'].astype('object')
dfValidateValidate['AC_DOC_NR'] = dfValidateValidate['AC_DOC_NR'].astype('str')
dfValidateValidate['PSTNG_DATE'] = pd.to_datetime(dfValidateValidate['PSTNG_DATE'], format="%Y%m%d")
#dfValidateValidate['CREATEDON'] = pd.to_datetime(dfValidateValidate['CREATEDON'].astype('object'), format="%Y%m%d")
dfValidateValidate['DOC_DATE'] = pd.to_datetime(dfValidateValidate['DOC_DATE'], format="%Y%m%d")
dfValidateValidate['PROFIT_CTR'] = dfValidateValidate['PROFIT_CTR'].astype('str')
dfValidateValidate['AC_DOC_TYP'] = dfValidateValidate['AC_DOC_TYP'].astype('object')
dfValidateValidate['POST_KEY'] = dfValidateValidate['POST_KEY'].astype('str')
#dfValidateValidate_test['POST_KEY'] = dfValidateValidate_test['POST_KEY'].astype('int')
dfValidateValidate['V.VENDOR'] = dfValidateValidate['V.VENDOR'].astype('object') #was CREDITOR
dfValidate['V.NAME'] = dfValidate['V.NAME'].astype('object') #Was #name
dfValidate['GL_ACCOUNT'] = dfValidate['GL_ACCOUNT'].astype('int')
#dfValidate_test['GL_ACCOUNT'] = dfValidate_test['GL_ACCOUNT'].astype('object')
#dfValidate['CUSTOMER'] = dfValidate['CUSTOMER'].astype('object') no longer included
dfValidate['POSTXT'] = dfValidate['POSTXT'].astype('object')
#dfValidate['BIC_ZUSERNAM'] = dfValidate['BIC_ZUSERNAM'].astype('object') no longer included, but may be useful to bring back
dfValidate['ZBKTXT'] = dfValidate['ZBKTXT'].astype('object')
dfValidate['ZTCODE'] = dfValidate['ZTCODE'].astype('str')
dfValidate['DEB_CRE_USD'] = pd.to_numeric(dfValidate['DEB_CRE_USD'])
dfValidate['V.BIC_ZTERMPAY'] = dfValidate['V.BIC_ZTERMPAY'].astype('str') #no longer included, but may be useful to bring back
dfValidate['MATL_GROUP'] = dfValidate['MATL_GROUP'].astype('str')
dfValidate['ACCNT_GRPV'] = dfValidate['ACCNT_GRPV'].astype('str')
dfValidate['HCPClass'] = dfValidate['HCPClass'].astype('object')
#dfValidate['Compliance Bucket'] = dfValidate['Compliance Bucket'].astype("object")
dfValidate['V.BI_ACCNT_TGRPV'] = dfValidate['V.BI_ACCNT_TGRPV'].astype("str")

#Filter data for relavent rows
dfValidate = dfValidate[ ((dfValidate.POST_KEY == '40') | (dfValidate.POST_KEY == '81')) & (dfValidate.GL_ACCOUNT >= 40000000)]

dfValidate['GL_ACCOUNT'] = dfValidate['GL_ACCOUNT'].astype('str')
#Filter data for relavent rows
#dfValidate_test = dfValidate_test[ ((dfValidate_test.POST_KEY == 40) | (dfValidate_test.POST_KEY == 81)) & (dfValidate_test.GL_ACCOUNT >= 40000000)]

#Get list of relavent fields
colSelect = list(colDict.keys())
colSelect = ['COMP_CODE', 'AC_DOC_NR', 'PSTNG_DATE', 'DOC_DATE', 'PROFIT_CTR', 'AC_DOC_TYP', 'POST_KEY', 'V.VENDOR', 'V.NAME', 'GL_ACCOUNT', 'POSTXT', 'ZBKTXT' ,'ZTCODE', 'DEB_CRE_USD', 'V.BIC_ZTERMPAY', 'MATL_GROUP', 'ACCNT_GRPV', 'V.BI_ACCNT_TGRPV', 'HCPClass']

#dfValidate[colSelect]
dfValidate = dfValidate[colSelect]

#dfValidate_test[colSelect]

dfValidate


import subprocess as sub
import re
import sys
import pyperclip
from pathlib import Path
audio = re.compile('(?<!res[0-9])(.mp3|.mpeg|.wav)$',re.IGNORECASE)

if len(sys.argv) != 2 :
    print("Invalid arguments supplied , try again with valid command line arguments")
    sys.exit(1)

path = sub.check_output(['pwd'],cwd=sys.argv[1])
path=path.decode('utf-8')
if path.endswith('\n') :
    path = path[:-1]
if not path.endswith('/') :
    path+='/'
all_files = sub.Popen(['find','.','-printf','%f\n'],cwd=sys.argv[1],stdout=sub.PIPE)
all_files=sub.check_output(['sort','-n','-'],stdin=all_files.stdout)
all_files = all_files.decode('utf-8')
all_files=all_files.split('\n')
# print(all_files)
# print(path)
for f in all_files :
    f=path+f
    # print(f)
# all_files.sort()
all_files = [Path(path+f) for f in all_files]
command = "ffmpeg "
n=0
for f in all_files :
    if audio.search(str(f)) != None :
        print(f.name)
        command+="-i \""+str(f)+"\" "
        n+=1
command+="-filter_complex "
command+="\"concat=n="+str(n)+":v=0:a=1[aout]\" "
command+="-map '[aout]' "
command+="-codec:a libmp3lame -q:a 5 res1.mp3"
print(command)
pyperclip.copy(command)


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf
from keras.utils import np_utils
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.pipeline import Pipeline
import numpy
def coeff_determination(y_true, y_pred):
    from keras import backend as K
    SS_res =  K.sum(K.square( y_true-y_pred ))
    SS_tot = K.sum(K.square( y_true - K.mean(y_true) ) )
    return ( 1 - SS_res/(SS_tot + K.epsilon()) )
#dataset = pd.read_csv('trainingi50.txt',delimiter=';')
#datasett = pd.read_csv('testi50.txt',delimiter=';')
dataset = pd.read_csv('ic.csv', sep=';', engine='python',
    na_values=['NA','?'])
datasett = pd.read_csv('ic.csv', sep=';', engine='python',
    na_values=['NA','?'])

X = dataset.iloc[:, 1:129].values
y = dataset.iloc[:, 0].values
X = X.astype('float32')
X_test = datasett.iloc[:, 1:129].values
y_test = datasett.iloc[:, 0].values
X_test = X_test.astype('float32')
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X = sc.fit_transform(X)
X = X.reshape(X.shape[0], 16, 8, 1)
X_test = sc.transform(X_test)
X_test = X_test.reshape(X_test.shape[0],16,8,1)
input_shape = (16, 8, 1)
import keras
from keras.models import Sequential
from keras.layers import *
model= Sequential()
model.add(Conv2D(32, kernel_size=5, padding="same",input_shape= input_shape, activation = 'relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(64, kernel_size=3, padding="same", activation = 'relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(256, kernel_size=2, padding="valid", activation = 'relu'))
model.add(MaxPooling2D(pool_size=(1, 1), strides=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(16,activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(16,activation='relu'))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam', metrics=[coeff_determination,'mse','mae'])
model.fit(X,y,batch_size=len(X),nb_epoch=1000)
model_json = model.to_json()
with open("3DQsarRegression.json", "w") as json_file:
    json_file.write(model_json)
model.save_weights("3DQsarRegression.h5")
print("Saved model to disk")
print("validated performances")
scores = model.evaluate(X_test, y_test, verbose=0)
print(scores)
print(model.predict(X))


import json
from scheduler.resources import Resource
from scheduler.exceptions import KubeHTTPException

class PersistentVolumeClaim(Resource):
    short_name = 'pvc'

    def manifest(self, namespace, name, version=None, **kwargs):
        labels = {
            'heritage': 'drycc',
        }
        data = {
            "apiVersion": self.api_version,
            "kind": "PersistentVolumeClaim",
            "metadata": {
                "name": name,
                "namespace": namespace,
                'labels': labels
            },
            "spec": {
                "accessModes": [
                    "ReadWriteMany"
                ],
                "resources": {
                    "requests": {
                        "storage": kwargs.get('size')
                    },
                },
                "storageClassName": kwargs.get("storage_class"),
                "volumeMode": "Filesystem",
            }
        }
        if version:
            data["metadata"]["resourceVersion"] = version
        return data

    def get(self, namespace, name=None):
        """
        Fetch a single persistentvolumeclaim or a list of persistentvolumeclaim
        """
        if name is not None:
            url = self.api('/namespaces/{}/persistentvolumeclaims/{}',
                           namespace, name)
            message = 'get persistentvolumeclaim ' + name
        else:
            url = self.api('/namespaces/{}/persistentvolumeclaims', namespace)
            message = 'get persistentvolumeclaims'
        response = self.http_get(url)
        if self.unhealthy(response.status_code):
            raise KubeHTTPException(response, message)
        return response

    def create(self, namespace, name, **kwargs):
        """
        Create persistentvolumeclaim
        """
        url = self.api('/namespaces/{}/persistentvolumeclaims', namespace)
        data = self.manifest(namespace, name, **kwargs)
        response = self.http_post(url, json=data)
        if not response.status_code == 201:
            raise KubeHTTPException(
                response,
                "create persistentvolumeclaim {}".format(namespace))
        return response

    def patch(self, namespace, name, **kwargs):
        url = self.api('/namespaces/{}/persistentvolumeclaims/{}', namespace,
                       name)
        data = self.manifest(namespace, name, **kwargs)
        response = self.http_patch(url, json=data, headers={"Content-Type": "application/merge-patch+json"})  # noqa
        if self.unhealthy(response.status_code):
            self.log(namespace, 'template used: {}'.format(json.dumps(data, indent=4)), 'DEBUG')  # noqa
            raise KubeHTTPException(response, 'update persistentvolumeclaims "{}"', name)
        return response

    def delete(self, namespace, name):
        """
        Delete persistentvolumeclaim
        """
        url = self.api('/namespaces/{}/persistentvolumeclaims/{}', namespace,
                       name)
        response = self.http_delete(url)
        if self.unhealthy(response.status_code):
            raise KubeHTTPException(response,
                                    'delete persistentvolumeclaim ' + name)
        return response


#!/usr/bin/env python
# encoding: utf-8
"""
@author: HuRuiFeng
@file: 034_finds-a-single-entry-free-area.py
@time: 2023/8/1 14:52
@project: huawei-od-python
@desc: 034 查找单入口空闲区域
"""

def dfs(i, j, count, out):
    # 如果到了边界、遇到了X或已经在连通域中，则直接返回结果。
    if i < 0 or i >= m or j < 0 or j >= n \
            or areas[i][j] == "X" or (i, j) in checked:
        return count

    checked.add((i, j))
    # 当遇到入口时，则加入到结果列表中
    if i == 0 or i == m - 1 or j == 0 or j == n - 1:
        out.append([i, j])

    count += 1
    # 继续对各个方向进行深度优先搜索
    for offsetX, offsetY in directions:
        newI = i + offsetX
        newJ = j + offsetY
        count = dfs(newI, newJ, count, out)

    return count

def solve_method(areas):
    global checked, m, n, directions
    m = len(areas)
    n = len(areas[0])
    checked = set()
    directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]

    ans = []

    for i in range(m):
        for j in range(n):
            if areas[i][j] == "O" and (i, j) not in checked:
                out = []
                count = dfs(i, j, 0, out)
                if len(out) == 1:
                    # 只能有一个入口
                    tmp = out[0][:]
                    ans.append((tmp[0], tmp[1], count))

    if len(ans) == 0:
        return "NULL"
    elif len(ans) == 1 or ans[0][2] > ans[1][2]:
        return ans[0]
    elif len(ans) > 1:
        # 如果有多个，取出最大单入口空闲区域
        ans.sort(key=lambda x: -x[2])
        return ans[0][2]

if __name__ == '__main__':
    areas = [["X", "X", "X", "X"],
             ["X", "O", "O", "X"],
             ["X", "O", "O", "X"],
             ["X", "O", "X", "X"]]
    assert solve_method(areas) == (3, 1, 5)

    areas = [["X", "X", "X", "X", "X"],
             ["O", "O", "O", "O", "X"],
             ["X", "O", "O", "O", "X"],
             ["X", "O", "X", "X", "O"]]
    assert solve_method(areas) == (3, 4, 1)

    areas = [["X", "X", "X", "X"],
             ["X", "O", "O", "O"],
             ["X", "O", "O", "O"],
             ["X", "O", "O", "X"],
             ["X", "X", "X", "X"]]
    assert solve_method(areas) == "NULL"

    areas = [["X", "X", "X", "X"],
             ["X", "O", "O", "O"],
             ["X", "X", "X", "X"],
             ["X", "O", "O", "O"],
             ["X", "X", "X", "X"]]
    assert solve_method(areas) == 3


# DrivenData Blood Donation Prediction Challenge

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

# Importing the dataset
train = pd.read_csv('train.csv')
test = pd.read_csv('test.csv')
X_train = train.iloc[:, 1:5].values
y_train = train.iloc[:, -1].values
X_test = test.iloc[:, 1::].values

# EDA
# Seaborn plots - work only on Dataframes
sns.pairplot(train.iloc[:, 1:5])
''' It is observed that No. of Donations and Volume Donated are highly correlated
    Hence, removing one of the correlated variables - Volume Donated'''
X_train = np.delete(X_train, 2, axis = 1)
X_test = np.delete(X_test, 2, axis = 1)

# 2-D plot showing class labels
from matplotlib.colors import ListedColormap
for i, j in enumerate(np.unique(y_train)):
    plt.scatter(X_train[y_train == j, 1], X_train[y_train == j, 2],
                c = ListedColormap(('red', 'green'))(i), label = j)
# 3-D plot showing class labels
import pylab
from mpl_toolkits.mplot3d import Axes3D
fig = pylab.figure()
ax = Axes3D(fig)
for c, j in [('r', 0), ('b', 1)]:
    ax.scatter(X_train[y_train == j, 0], X_train[y_train == j, 1], X_train[y_train == j, 2], c = c)
plt.show()

# Adding new feature : Donation frequency
don_freq_train = ((X_train[:, 2] - X_train[:, 0]).astype(float) / X_train[:, 1]).reshape(576,1)
don_freq_test = ((X_test[:, 2] - X_test[:, 0]).astype(float) / X_test[:, 1]).reshape(200,1)
X_train = np.append(X_train, don_freq_train, axis = 1)
X_test = np.append(X_test, don_freq_test, axis = 1)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)

# Model 1 : Regularized Logistic Regression
from sklearn.linear_model import LogisticRegression
classifier1 = LogisticRegression(penalty = 'l2', C = 0.05, solver = 'liblinear')
classifier1.fit(X_train,y_train)

# Model 2 : SVM Classification
from sklearn.svm import SVC
classifier2 = SVC(kernel = 'poly', degree = 5, probability = True, C = 50)
classifier2.fit(X_train, y_train)

# Model 3 : Naive Bayes Classification
from sklearn.naive_bayes import GaussianNB
classifier3 = GaussianNB()
classifier3.fit(X_train, y_train)

# Model 4 : Random Forest Classification
from sklearn.ensemble import RandomForestClassifier
classifier4 = RandomForestClassifier(n_estimators = 300, criterion = "entropy")
classifier4.fit(X_train, y_train)

# Applying k-Fold Cross Validation
from sklearn.model_selection import cross_val_score
accuracies = cross_val_score(estimator = classifier1, X = X_train, y = y_train, cv = 10)
CV_accuracy = accuracies.mean()
CV_std = accuracies.std()

# Applying Grid Search to find the best model and the best parameters
from sklearn.model_selection import GridSearchCV
parameters = [{'C' : [0.005, 0.01, 0.02, 0.05, 0.1]}
             ]
'''{'C' : [50, 75, 100],
               'kernel' : ['poly'],
               'degree' : [7,9]}'''
'''{'C' : [0.05, 0.1, 0.5, 1, 5, 10, 50],
               'kernel' : ['rbf', 'linear',]}'''
'''{'n_estimators' : [100, 101, 300, 501]}  '''

grid_search = GridSearchCV(estimator = classifier1,
                           param_grid = parameters,
                           scoring = "neg_log_loss",
                           cv = 10, n_jobs = -1)
grid_search = grid_search.fit(X_train, y_train)
best_metric = grid_search.best_score_
best_params = grid_search.best_params_

# Predicting the Test Set results
y_pred1 = classifier1.predict_proba(X_test)[:,1]
y_pred2 = classifier2.predict_proba(X_test)[:,1]
y_pred3 = classifier3.predict_proba(X_test)[:,1]
y_pred4 = classifier4.predict_proba(X_test)[:,1]

# Creating predictions from ensemble models
ensemble1 = (0.6*y_pred1) + (0.4*y_pred4)

# Writing the results to a csv file
np.savetxt('results.csv', ensemble1)

DrivenData - Predict Blood Donation/DD_model01.py