from tkinter import *
import sqlite3

"""
Jak maji tlacitka pracovat:
dHledat:
    - Pokud v DB existuje zaznam se shodujicim se jmenem spolecnosti nebo tel. cisla
    - Vypise jej
dUlozit jako nove:
    - Ulozi vstup jako novy pohovor, pokud je vyplnena alespon spolecnost nebo tlf
dUlozit zmeny:
    - Ulozi zmeny v nactenem pohovoru
dVypis vse:
    - Do pozn8mky vypise Spolecnost, pohovor (dle ulozeni v db)
dVypis dle data:
    - Do pozn8mky vypise Spolecnost, pohovor (dle nejblizsiho data)
dVycisti:
    - Vycisti vstupni pole
dVymaz oznacene:
    - Vymaze oznaceny radek z DB
dVypnout:
    - Vypinaci tlacitko
TODO:
-pokud budes mit volno (ach, kez by :-)) predelej vypis dle data tak, aby to bralo a pocitalo jen s realnym datumem
- predej to na dva fily - back+front end... bude to srani, ale ano, udelej to
"""
"""
BACKEND
"""

def smaz_oznacene():
    id_k_smazani = selected_row
    spojeni = sqlite3.connect('pohovory.db')
    kurzor = spojeni.cursor()
    kurzor.execute("DELETE FROM pohovory WHERE id = ?", str(id_k_smazani))
    spojeni.commit()
    spojeni.close()
    vloz_do_t1('Smazan pohovor s id: {}'.format(id_k_smazani), prepis = True)

def get_selected_row(event):
    global selected_row
    index = list1_vypis_polozek.curselection()[0]
    selected_row = list1_vypis_polozek.get(index)[0]#[0]
    vypsat_oznacene_do_poli()

def vycisti_pole():
    for pole in seznam_vstupnich_poli[:-1]:
        pole.delete(0, END)
    seznam_vstupnich_poli[-1].delete(1.0, END)

def spojeni_s_db():
    spojeni = sqlite3.connect('pohovory.db')
    kurzor = spojeni.cursor()
    kurzor.execute("CREATE TABLE IF NOT EXISTS pohovory(id INTEGER PRIMARY KEY, firma text, pohovor text, osoba1 text, osoba2 text, osoba3 text, mail1 text, mail2 text, mail3 text, telefon1 text, telefon2 text, telefon3 text, poznamka1 text, poznamka2 text, poznamka3 text, poznamky text)")
    spojeni.commit()
    spojeni.close()

def vypsat_oznacene_do_poli():
    id_k_vypsani = selected_row
    spojeni = sqlite3.connect('pohovory.db')
    kurzor = spojeni.cursor()
    kurzor.execute("SELECT * FROM pohovory WHERE id = ?", str(id_k_vypsani))
    seznam_k_vypsani = kurzor.fetchall()
    vycisti_pole()
    for i, obj in enumerate(seznam_k_vypsani[0][1:]):
        seznam_vstupnich_poli[i].insert(END, obj)
    spojeni.close()

def vlozeni_noveho_zaznamu():
    if len(e1_firma_value.get()) == len(e9_number1_value.get()) ==  len(e2_pohovor_value.get()) == 0:
        vloz_do_t1('Neni co ukladat... co takhle se více snažit?')
        return
    spojeni = sqlite3.connect('pohovory.db')
    kurzor = spojeni.cursor()
    kurzor.execute("INSERT INTO pohovory VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (e1_firma_value.get(), e2_pohovor_value.get(), e3_osoba1_value.get(), e4_osoba2_value.get(), e5_osoba3_value.get(), e6_mail1_value.get(), e7_mail2_value.get(), e8_mail3_value.get(), e9_number1_value.get(), e10_number2_value.get(), e11_number3_value.get(), e12_popisek1_value.get(), e13_popisek2_value.get(), e14_popisek3_value.get(), t1_poznamky.get("1.0", 'end-1c')))
    spojeni.commit()
    spojeni.close()
    vloz_do_t1('Zaznam uspesne pridan :-)', True)

def vloz_do_t1(objekt, prepis = True):
    # Funkce, ktera do Poznamkoveho pole vklada hodnoty. Pokud prepis = True:smaze puvodni vypsany obsah pole
    if prepis:
        list1_vypis_polozek.delete(0, END)
    list1_vypis_polozek.insert(END, objekt)

def nacti_vse_dle_abecedy():
    spojeni = sqlite3.connect('pohovory.db')
    kurzor = spojeni.cursor()
    kurzor.execute("SELECT id, firma, pohovor FROM pohovory ORDER BY firma ASC")
    vytah_z_db = kurzor.fetchall()
    # vycisti_pole()
    list1_vypis_polozek.delete(0, END)
    for objekt in vytah_z_db:
        vloz_do_t1(objekt, prepis = False)
    spojeni.close()

def nacti_vse_dle_data():
    spojeni = sqlite3.connect('pohovory.db')
    kurzor = spojeni.cursor()
    kurzor.execute("SELECT id, firma, pohovor FROM pohovory ORDER BY pohovor ASC")
    vytah_z_db = kurzor.fetchall()
    list1_vypis_polozek.delete(0, END)
    for objekt in vytah_z_db:
        vloz_do_t1(objekt, prepis = False)
    spojeni.close()

def hledat():
    if len(e1_firma_value.get()) == len(e9_number1_value.get()) ==  len(e2_pohovor_value.get()) == 0:
        vloz_do_t1('Není dle čeho hledat. Víc se snaž')
        return
    spojeni = sqlite3.connect('pohovory.db')
    kurzor = spojeni.cursor()
    kurzor.execute(
        "SELECT id, firma, pohovor FROM pohovory WHERE telefon1 = ? OR telefon2 = ? OR telefon3 = ?",
        (e9_number1_value.get(), e9_number1_value.get(),
         e9_number1_value.get()))
    vysledek = kurzor.fetchall()[0]
    vloz_do_t1(vysledek, prepis = True)
    spojeni.close()

def update_zapisu():
    spojeni = sqlite3.connect('pohovory.db')
    kurzor = spojeni.cursor()
    kurzor.execute("UPDATE pohovory SET firma = ?, pohovor = ?, osoba1 = ?, osoba2 = ?, osoba3 = ?, mail1 = ?, mail2 = ?, mail3 = ?, telefon1 = ?, telefon2 = ?, telefon3 = ?, poznamka1 = ?, poznamka2 = ?, poznamka3 = ?, poznamky = ? WHERE firma = ?", (e1_firma_value.get(), e2_pohovor_value.get(), e3_osoba1_value.get(), e4_osoba2_value.get(), e5_osoba3_value.get(), e6_mail1_value.get(), e7_mail2_value.get(), e8_mail3_value.get(), e9_number1_value.get(), e10_number2_value.get(), e11_number3_value.get(), e12_popisek1_value.get(), e13_popisek2_value.get(), e14_popisek3_value.get(), t1_poznamky.get("1.0", 'end-1c'), e1_firma_value.get()))
    spojeni.commit()
    spojeni.close()

def vypnout():
    window.destroy()

spojeni_s_db()
"""
FRONTEND
"""

window = Tk()
window.wm_title('Pohovory v0.2')

l1_firma = Label(window, text = 'Společnost:')
l1_firma.grid(row = 0, column = 0, sticky = W+S)
e1_firma_value = StringVar()
e1_firma = Entry(window, textvariable = e1_firma_value)
e1_firma.grid(row = 1, column = 0, sticky = W+S+N)
e1_firma.config(font=("Times", 12, 'bold'))

l2_pohovor = Label(window, text = '      Pohovor:')
l2_pohovor.grid(row = 1, column = 1, columnspan = 2, sticky = W+S)
e2_pohovor_value = StringVar()
e2_pohovor = Entry(window, textvariable = e2_pohovor_value)
e2_pohovor.grid(row = 1, column = 1, columnspan = 2, sticky = S)

l3_osoba1 = Label(window, text = 'Osoba1:')
l3_osoba1.grid(row = 2, column = 0, sticky = S+W)
e3_osoba1_value = StringVar()
e3_osoba1 = Entry(window, textvariable = e3_osoba1_value)
e3_osoba1.grid(row = 3, column = 0, sticky = W)

l4_osoba2 = Label(window, text = 'Osoba2:')
l4_osoba2.grid(row = 2, column = 1, sticky = W+S)
e4_osoba2_value = StringVar()
e4_osoba2 = Entry(window, textvariable = e4_osoba2_value)
e4_osoba2.grid(row = 3, column = 1, sticky = W)

l5_osoba3 = Label(window, text = 'Osoba3:')
l5_osoba3.grid(row = 2, column = 2, sticky = S+W)
e5_osoba3_value = StringVar()
e5_osoba3 = Entry(window, textvariable = e5_osoba3_value)
e5_osoba3.grid(row = 3, column = 2, sticky = W)

l6_mail1 = Label(window, text = 'E-mail:')
l6_mail1.grid(row = 4, column = 0, sticky = S+W)
e6_mail1_value = StringVar()
e6_mail1 = Entry(window, textvariable = e6_mail1_value)
e6_mail1.grid(row = 5, column = 0, sticky = W)

e7_mail2_value = StringVar()
e7_mail2 = Entry(window, textvariable = e7_mail2_value)
e7_mail2.grid(row = 5, column = 1, sticky = W)

e8_mail3_value = StringVar()
e8_mail3 = Entry(window, textvariable = e8_mail3_value)
e8_mail3.grid(row = 5, column = 2, sticky = W)

l7_telefon1 = Label(window, text = 'Telefonní číslo:')
l7_telefon1.grid(row = 6, column = 0, sticky = S+W)
e9_number1_value = StringVar()
e9_number1 = Entry(window, textvariable = e9_number1_value)
e9_number1.grid(row = 7, column = 0, sticky = W)
e10_number2_value = StringVar()
e10_number2 = Entry(window, textvariable = e10_number2_value)
e10_number2.grid(row = 7, column = 1, sticky = W)
e11_number3_value = StringVar()
e11_number3 = Entry(window, textvariable = e11_number3_value)
e11_number3.grid(row = 7, column = 2, sticky = W)

l8_popisek1 = Label(window, text = 'Pozn.:')
l8_popisek1.grid(row = 8, column = 0, sticky = S+W)
e12_popisek1_value = StringVar()
e12_popisek1 = Entry(window, textvariable = e12_popisek1_value)
e12_popisek1.grid(row = 9, column = 0, sticky = W)
e13_popisek2_value = StringVar()
e13_popisek2 = Entry(window, textvariable = e13_popisek2_value)
e13_popisek2.grid(row = 9, column = 1, sticky = W)
e14_popisek3_value = StringVar()
e14_popisek3 = Entry(window, textvariable = e14_popisek3_value)
e14_popisek3.grid(row = 9, column = 2, sticky = W)

list1_vypis_polozek = Listbox(window, height = 14, width = 44)
list1_vypis_polozek.grid(row = 10, column = 0, rowspan = 14, columnspan = 2, sticky = W+N)

s1 = Scrollbar(window)
s1.grid(row = 18, column = 2, rowspan = 15, sticky = E+N+S)
list1_vypis_polozek.bind('<<ListboxSelect>>', get_selected_row)

t1_poznamky = Text(window, height = 15, width = 70)
t1_poznamky.configure(yscrollcommand = s1.set)
t1_poznamky.grid(row = 18, column = 0, columnspan = 3, sticky = W)
s1.configure(command = t1_poznamky.yview)

seznam_vstupnich_poli = [e1_firma, e2_pohovor, e3_osoba1, e4_osoba2, e5_osoba3, e6_mail1, e7_mail2, e8_mail3, e9_number1, e10_number2, e11_number3, e12_popisek1, e13_popisek2, e14_popisek3, t1_poznamky]

### Tlacitka

b1_find = Button(window, text = 'Hledat')
b1_find.config(height = 0, command = hledat)
b1_find.grid(row = 10, column = 2, sticky = W+E)
b2_save_new = Button(window, text = 'Uložit jako nové', command = vlozeni_noveho_zaznamu)
b2_save_new.grid(row = 11, column = 2, sticky = W+E)
b3_save_changes = Button(window, text = 'Uložit změny', command = update_zapisu)
b3_save_changes.grid(row = 12, column = 2, sticky = W+E)
b4_print_all = Button(window, text = 'Vypiš vše dle abecedy', command = nacti_vse_dle_abecedy)
b4_print_all.grid(row = 13, column = 2, sticky = W+E)
b5_sort_time = Button(window, text = 'Vypiš vše dle data', command = nacti_vse_dle_data)
b5_sort_time.grid(row = 14, column = 2, sticky = W+E)
b6_clear = Button(window, text = 'Vyčisti pole', command = vycisti_pole)
b6_clear.grid(row = 15, column = 2, sticky = W+E)
b7_delete_selected = Button(window, text ='Vymazat označené', command = smaz_oznacene)
b7_delete_selected.grid(row = 16, column = 2, sticky = W + E)
b8_exit = Button(window, text = 'Vypnout', command = vypnout)
b8_exit.grid(row = 17, column = 2, sticky = W+E)

window.mainloop()


import sys

encode_val = {}

# A Huffman Tree Node
class Node:
    def __init__(self, freq, letter, left=None, right=None):
        self.freq = freq
        self.letter = letter
        self.left = left
        self.right = right
        self.direction = ''

def print_nodes(node, val=''):
    global encode_val
    # huffman code for current node
    new_val = val + str(node.direction)
    # if node is not an edge node
    # then traverse inside it
    if node.left:
        print_nodes(node.left, new_val)
    if node.right:
        print_nodes(node.right, new_val)
    # if node is edge node then display its huffman code
    if not node.left and not node.right:
        print(f"{node.letter} -> {new_val}")
        encode_val[node.letter] = new_val

def frequency_calc(data):
    if data is None or data == "" or type(data) is not str:
        return None
    data = "".join(data.split())
    freq_holder_dict = {}
    for char in data:
        if char in freq_holder_dict:
            freq_holder_dict[char] += 1
        else:
            freq_holder_dict[char] = 1
    ordered_dict = dict(sorted(freq_holder_dict.items(), key=lambda v: v[1]))
    print(ordered_dict)
    return ordered_dict

def huffman_tree_nodes_builder(letter_freq_dict):
    nodes = []
    for key in letter_freq_dict:
        nodes.append(Node(letter_freq_dict[key], key))
    return nodes

def huffman_tree_nodes(nodes):
    while len(nodes) > 1:
        nodes = sorted(nodes, key=lambda x: x.freq)
        # pick two smallest nodes from the nodes
        left_node = nodes[0]
        right_node = nodes[1]

        # assign direction of 0 to left node and direction of 1 to right node
        left_node.direction = 0
        right_node.direction = 1

        # combine the nodes
        combined_node = Node(
            left_node.freq + right_node.freq, left_node.letter + right_node.letter, left_node, right_node)
        #   remove the combined node
        nodes.remove(left_node)
        nodes.remove(right_node)
        nodes.append(combined_node)
    return nodes[0]

def get_encoded_record(data):
    global encode_val
    if data is None or data == "" or type(data) is not str:
        return None
    data = "".join(data.split())
    encoded_string = ""
    for char in data:
        encoded_string += encode_val[char]
    return encoded_string

def huffman_encoding(data):
    if type(data) is not str or data is None or data == "":
        return "Invalid Entry", ""
    frequency_ordered_dict = frequency_calc(data)
    if frequency_ordered_dict is None:
        return "Invalid Entry"
    nodes = huffman_tree_nodes_builder(frequency_ordered_dict)
    huffman_getter = huffman_tree_nodes(nodes)
    print_nodes(huffman_getter)
    return get_encoded_record(data), huffman_getter

def huffman_decoding(data, tree):
    huffman_tree = tree
    decoded_output = []
    for char in data:
        if char == "0":
            tree = tree.left
        elif char == "1":
            tree = tree.right
        try:
            if tree.left.letter is None and tree.right.letter is None:
                pass
        except AttributeError:
            decoded_output.append(tree.letter)
            tree = huffman_tree
    string = ''.join([str(item) for item in decoded_output])
    return string

if __name__ == "__main__":
    codes = {}

    a_great_sentence = "The bird is the word"

    print("The size of the data is: {}\n".format(sys.getsizeof(a_great_sentence)))
    print("The content of the data is: {}\n".format(a_great_sentence))

    encoded_data, tree = huffman_encoding(a_great_sentence)

    print("The size of the encoded data is: {}\n".format(sys.getsizeof(int(encoded_data, base=2))))
    print("The content of the encoded data is: {}\n".format(encoded_data))

    decoded_data = huffman_decoding(encoded_data, tree)

    print("The size of the decoded data is: {}\n".format(sys.getsizeof(decoded_data)))
    print("The content of the encoded data is: {}\n".format(decoded_data))

test1_text_code, test1_text_code_tree = huffman_encoding("AAAAAAABBBCCCCCCCDDEEEEEE")

print(test1_text_code)   #return 1010101010101000100100111111111111111000000010101010101

decoded_text1 = huffman_decoding(test1_text_code, test1_text_code_tree)

print(decoded_text1) #return AAAAAAABBBCCCCCCCDDEEEEEE

test2_text_code, test2_text_code_tree = huffman_encoding("")

print(test2_text_code) #return Invalid Entry

test3_text_code, test3_text_code_tree = huffman_encoding("JJJJJJJJGGGGG")

print(test3_text_code) #return 1111111100000

decoded_text3 = huffman_decoding(test3_text_code, test3_text_code_tree)

print(decoded_text3) #return JJJJJJJJGGGGG


from turtle import Turtle,Screen
import random
s=Screen()
s.setup(width=500, height=400)
colors=["red", "green","blue" ,"pink", "yellow", "purple"]
tim=[]
y=175
play=True

for i in range(0,6):
    tim.append(Turtle(shape="turtle"))
    tim[i].color(colors[i])
    tim[i].up()
    y-=50
    tim[i].goto(x=-230,y=y)

user=s.textinput(title="Hello!!", prompt="Which turtle will win the race?Enter color:")

while play:
    for t in tim:
        if t.xcor()>220:
            play=False
            print(f'Winner:>{t.pencolor()}')
            if t.pencolor()==user:
                print("YOU WIN")
            else:
                print("BETTER LUCK NEXT TIME")
            break
        dis=random.randint(0,10)
        t.forward(dis)

s.exitonclick()


from collections import deque
def solution(queue1, queue2):
    arr1 = deque()
    arr2 = deque()
    for i in queue1:
        arr1.append(i)
    for i in queue2:
        arr2.append(i)
    a = sum(queue1)
    b = sum(queue2)
    _sum = a + b
    cnt = 0
    if _sum % 2:
        return -1
    while (a != b):
        if b > a:
            tem = arr2.popleft()
            a += tem
            b -= tem
            arr1.append(tem)
            cnt += 1
        else:
            tem = arr1.popleft()
            b += tem
            a -= tem
            arr2.append(tem)
            cnt += 1
        if cnt > len(queue1) + len(queue2) + 10 or not len(arr1) or not len(arr2):
            cnt = -1
            break
    answer = cnt
    return answer

프로그래머스/lv2/118667. 두 큐 합 같게 만들기/두 큐 합 같게 만들기.py

두 큐 합 같게 만들기.py


from frasco import Feature, action, Blueprint, current_app, hook, signal
from frasco.utils import import_string
from .view import AdminView, AdminBlueprint
from blueprint import admin_bp
import os

class AdminFeature(Feature):
    name = "admin"
    requires = ["bootstrap"]
    blueprints = [admin_bp]
    view_files = [("admin/*", AdminView)]
    defaults = {"url_prefix": "/admin",
                "subdomain": None}

    init_admin_signal = signal('init_admin')

    def init_app(self, app):
        self.app = app
        self.admin_checker_func = None
        app.features.menu.ensure("admin")

        app.assets.register({
            "admin": [
                "@jquery-bootstrap-all-cdn",
                "@font-awesome-cdn",
                "admin/layout.css",
                "admin/admin.js"]})

        app.jinja_env.macros.register_file(
            os.path.join(os.path.dirname(__file__), "macros.html"), "admin.html")

        self.dashboard_counters = []

    def admin_checker(self, func):
        self.admin_checker_func = func

    def is_admin(self, user):
        if self.admin_checker_func:
            return self.admin_checker_func(user)
        return getattr(user, 'is_admin', False)

    def init_blueprints(self, app):
        self.register_blueprint(admin_bp)
        for feature in app.features:
            if hasattr(feature, "init_admin"):
                feature.init_admin(self, app)
        self.init_admin_signal.send(app, admin=self)

    def register_blueprint(self, bp):
        if isinstance(bp, str):
            bp = import_string(bp)
        self.app.register_blueprint(bp, **self.get_blueprint_options(bp))

    def get_blueprint_options(self, bp=None):
        url_prefix = self.options["url_prefix"]
        if bp and bp.url_prefix:
            url_prefix = (url_prefix + "/" + bp.url_prefix.lstrip("/")).rstrip("/")
        return dict(url_prefix=url_prefix, subdomain=self.options["subdomain"])

    def register_dashboard_counter(self, label, value_func, icon, color='blue'):
        self.dashboard_counters.append((label, icon, color, value_func))


# Script for evaluating the models systematically
import pandas as pd
import numpy as np
from gensim.models import Doc2Vec, doc2vec, FastText
from gensim.models.doc2vec import TaggedDocument
from itertools import compress
import importlib
import argparse

from collections import Counter

from sklearn import utils
from sklearn.preprocessing import minmax_scale
from sklearn import metrics
from sklearn.model_selection import train_test_split

from nltk.tokenize import word_tokenize
import re
import utils as ut

parser = argparse.ArgumentParser(description='Process model parameters')
parser.add_argument('-ct', '--cancer-type', type=str, default="prostate",
                    help='model used for training')
parser.add_argument('-t', '--to-combine', type=int, default=1,
                    help='whether to combine the treatments. 0: False. 1: True')
parser.add_argument('-c', '--min-count', type=int, default=70,
                    help='minimum count for treatments')
parser.add_argument('-d', '--data-dir', type=str, default="/home/jiaming/scirdb/",
                    help='data directory')
parser.add_argument('-md', '--model-dir', type=str, default="/share/pi/rubin/jiaming/models/",
                    help='model directory')
parser.add_argument('-o', '--output-dir', type=str, default="/share/pi/rubin/jiaming/nlp_results/",
                    help='model directory')
parser.add_argument('-m', '--model', type=str, default="fasttext",
                    help='embedding model')
parser.add_argument('-a', '--alg', type=int, default=0,
                    help='algorithm for embedding model')
parser.add_argument('-vs', '--vector-size', type=int, default=100,
                    help='vector size of embedding model')
parser.add_argument('-w', '--window', type=int, default=3,
                    help='window of maximum distance between words')
args = parser.parse_args()

# Create txt file for recording or results
missing_models = open(args.output_dir + "missing_models_{}_t{}_vs{}_w{}.txt".format(
    args.model, args.alg, args.vector_size, args.window), "w+")

# Load the dataset
notes = pd.read_pickle(args.data_dir + 'data/related_notes.pkl')
data_train, data_test, treatments = ut.load_dataset(args.data_dir, args.cancer_type, args.min_count, args.to_combine)

if args.cancer_type == "prostate":
    structured = ['age', 'sex', 'clinical_stage', 'radiation_eb',
                   'radiation_bt', 'prostatectomy', 'hormonal',
                   'race_asian', 'race_black', 'race_other', 'race_white',
                   'ethnicity_hispanic', 'ethnicity_nonhispanic']
elif args.cancer_type == "oropharynx":
    structured = ['age', 'sex', 'clinical_stage', 'radiation_eb',
                   'radiation_bt', 'oropharynx_surgery', 'chemo',
                   'race_asian', 'race_black', 'race_other', 'race_white',
                   'ethnicity_hispanic', 'ethnicity_nonhispanic']
elif args.cancer_type == "esophagus":
    structured = ['age', 'sex', 'clinical_stage', 'radiation_eb',
                   'radiation_bt', 'esophagus_surgery', 'chemo',
                   'race_asian', 'race_other', 'race_white',
                   'ethnicity_hispanic', 'ethnicity_nonhispanic']

# Create df and file for saving results
metrics = ["precision", "recall", "f1"]
ci = ["", "lower", "upper"]
train_metrics = []
test_metrics = []
for treatment in treatments:
    train_metrics += ["train_{}_{}".format(treatment, metric) for metric in metrics]
    test_metrics += ["{}_{}_{}".format(treatment, metric, c) for metric in metrics for c in ci]

columns = ["model", "alg", "vs", "epochs", "alpha", "window", "sample", "ns_exponent",
           "method", "train_accuracy"] + train_metrics + ["accuracy", "accuracy_lower", "accuracy_upper"] + test_metrics
df_results = pd.DataFrame(columns=columns)

# Record the dataset size
print("Size of training: " + str(data_train.shape) + '\n')
print(*list(zip(range(len(treatments)), treatments, [len(data_train[data_train.Treatment == i]) for i in range(len(treatments))])), sep="\n")
print("Size of testing:" + str(data_test.shape) + '\n')
print(*list(zip(range(len(treatments)), treatments, [len(data_test[data_test.Treatment == i]) for i in range(len(treatments))])), sep="\n")

# Load relevant treatment terms
terms = pd.read_excel(args.data_dir + 'data/cancer_term_dictionary.xlsx')
terms.columns = ['vocab', 'category']
terms = terms[terms.category.isin([1, 2, 3, 6])]
treatment_terms = [word.lower() for word in terms.vocab]

# Load the notes data from the associated note_ids
data_train = ut.load_relevant_notes(data_train, notes, treatment_terms, embedding=True)
data_test = ut.load_relevant_notes(data_test, notes, treatment_terms, embedding=True)

# Turn some variables into one-hot vectors
data_train = ut.data_onehot(data_train, args.cancer_type)
data_test = ut.data_onehot(data_test, args.cancer_type)

# Normalize structured data
X_structured_train = minmax_scale(data_train[structured])
y_train = data_train.Treatment.to_numpy().astype('int')

X_structured_test = minmax_scale(data_test[structured])
y_test = data_test.Treatment.to_numpy().astype('int')

## Perform analysis on structured + notes dataset
# Set hyperparameters
epochs=[5, 10, 30]
alpha=[0.0025, 0.025, 0.25]
sample=[1e-4, 1e-2, 0]
ns_exponent=[0.75]

for e in epochs:
    for a in alpha:
        for s in sample:
            for ns in ns_exponent:
                # Record the parameters
                args.epochs = e
                args.alpha = a
                args.sample = s
                args.ns_exponent = ns

                # Set column prefix
                col_prefix = [args.model, args.alg, args.vector_size, args.epochs,
                              args.alpha, args.window, args.sample, args.ns_exponent]

                # Read in results file to see what has not been run
                try:
                    df_results = pd.read_csv(args.output_dir + "{}_{}_t{}_vs{}_w{}_results.csv".format(
                        args.cancer_type, args.model, args.alg, args.vector_size, args.window), index_col=0)
                    #print(df_results.head())
                    finished_runs = len([run for run in df_results.iloc[:,0:8].to_numpy() if set(run) == set(col_prefix)])
                    #print(finished_runs)
                    if finished_runs == 10: # 5 runs each, 2 sets
                        print(col_prefix, "already ran.")
                        continue
                except:
                    pass

                if args.model == 'doc2vec':
                    model_name = "doc2vec_v{}_a{}_e{}_t{}_w{}_s{}_ns{}.model".format(args.vector_size, args.alpha,
                                                                               args.epochs, args.alg,
                                                                               args.window, args.sample,
                                                                               args.ns_exponent)
                    try:
                        print ("Loading model " + model_name)
                        model = Doc2Vec.load(args.model_dir + model_name)
                    except:
                        missing_models.write(model_name + "\n")
                        missing_models.flush()
                        continue

                     # Turn notes into vectors
                    print ("Inferring doc2vec vectors...")
                    merged_notes_train = ut.label_sentences(data_train.merged_notes, 'Train')
                    merged_notes_test = ut.label_sentences(data_test.merged_notes, 'Test')

                    notes_train_vectors = ut.vec_for_learning(model, merged_notes_train)
                    notes_test_vectors = ut.vec_for_learning(model, merged_notes_test)

                elif args.model == 'fasttext':
                    model_name = "fasttext_v{}_a{}_e{}_t{}_w{}_s{}_ns{}.model".format(args.vector_size, args.alpha,
                                                                                args.epochs, args.alg,
                                                                                args.window, args.sample,
                                                                                args.ns_exponent)
                    try:
                        print ("Loading model " + model_name)
                        model = FastText.load(args.model_dir + model_name)
                    except:
                        missing_models.write(model_name + "\n")
                        missing_models.flush()
                        continue

                    # Turn notes into vectors
                    print ("Inferring fasttext vectors...")
                    merged_notes_train = ut.word_tokenizer(data_train.merged_notes)
                    merged_notes_test = ut.word_tokenizer(data_test.merged_notes)

                    notes_train_vectors = ut.fasttext_vectors(model, merged_notes_train, args.vector_size)
                    notes_test_vectors = ut.fasttext_vectors(model, merged_notes_test, args.vector_size)

                # Evaluating for just doc2vec models
                print ("Evaluating models with just embedding model...")
                results = ut.evaluate_models(notes_train_vectors, notes_test_vectors,
                                              y_train, y_test, args.model, args.cancer_type)
                results2 = pd.DataFrame([col_prefix + r for r in results], columns=columns)
                df_results = df_results.append(results2, ignore_index = True, sort=False)
                df_results.to_csv(args.output_dir + "{}_{}_t{}_vs{}_w{}_results.csv".format(
                    args.cancer_type, args.model, args.alg, args.vector_size, args.window))

                # Evaluating for structured and unstructured
                # Combine the structured and unstructured data together
                print ("Evaluating models for both structured+unstructured data...")
                X_combined_train = ut.concat_vectors(X_structured_train, notes_train_vectors)
                X_combined_test = ut.concat_vectors(X_structured_test, notes_test_vectors)

                results = ut.evaluate_models(X_combined_train, X_combined_test,
                                              y_train, y_test, "structured+" + args.model, args.cancer_type)
                results2 = pd.DataFrame([col_prefix + r for r in results], columns=columns)
                df_results = df_results.append(results2, ignore_index = True, sort=False)
                df_results.to_csv(args.output_dir + "{}_{}_t{}_vs{}_w{}_results.csv".format(
                    args.cancer_type, args.model, args.alg, args.vector_size, args.window))

missing_models.close()
print("Finished.")


import requests
import json
import logging
import datetime
from datetime import datetime, timedelta
import schedule
import time
import datetime
import xml.etree.ElementTree as ET
from System_func.send_telegram_message import Alert

# Создание объекта логгера для ошибок и критических событий
check_error_logger = logging.getLogger('Check_Ticket_Error')
check_error_logger.setLevel(logging.ERROR)
check_error_handler = logging.FileHandler('./logs/check_ticket-errors.log')
check_error_handler.setLevel(logging.ERROR)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M')
check_error_handler.setFormatter(formatter)
check_error_logger.addHandler(check_error_handler)

# Создание объекта логгера для информационных сообщений
check_info_logger = logging.getLogger('Check_Ticket_Info')
check_info_logger.setLevel(logging.INFO)
check_info_handler = logging.FileHandler('./logs/check_ticket-info.log')
check_info_handler.setLevel(logging.INFO)
check_info_handler.setFormatter(formatter)
check_info_logger.addHandler(check_info_handler)

# Создаем объект класса Alert
alert = Alert()

### Авторизация в HappyFox
# Указываем путь к файлу с данными
CONFIG_FILE = "Main.config"
# открываем файл и загружаем данные
with open(CONFIG_FILE, 'r', encoding='utf-8-sig') as f:
    data_config = json.load(f)
# извлекаем значения API_KEY и API_SECRET
API_ENDPOINT = data_config['HAPPYFOX_SETTINGS']['API_ENDPOINT']
API_KEY = data_config['HAPPYFOX_SETTINGS']['API_KEY']
API_SECRET = data_config['HAPPYFOX_SETTINGS']['API_SECRET']
# сохраняем значения в переменную auth
HEADERS = {'Content-Type': 'application/json'}

def get_time_diff(date_str):
    """Функция подсчёта времени с последнего ответа клиента"""
    current_time = datetime.datetime.now()
    date_time = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S') + datetime.timedelta(hours=3)

    # Определяем день недели
    day_of_week = date_time.weekday()

    # Смещаем дату назад на количество дней, равное количеству выходных
    days_to_subtract = 0
    if day_of_week == 5:  # суббота
        days_to_subtract = 1
    elif day_of_week == 6:  # воскресенье
        days_to_subtract = 2

    # Вычисляем время между датами, исключая выходные
    business_days = 0
    elapsed_days = (current_time - date_time).days
    for day in range(1, elapsed_days+1):
        d = date_time + datetime.timedelta(days=day)
        if d.weekday() not in (5, 6):
            business_days += 1
    elapsed_days = business_days - days_to_subtract
    diff = datetime.timedelta(days=elapsed_days, seconds=(current_time - date_time).seconds)

    return diff

def get_assigned_name(assigned_to):
    if isinstance(assigned_to, list):
        for assigned in assigned_to:
            assigned_name = assigned.get('name')
            break
    else:
        if assigned_to is not None:
            assigned_name = assigned_to.get('name')
        else:
            assigned_name = "Нет исполнителя"
    return assigned_name

def get_name_info(contact_info):
    name_info = None
    for contact in contact_info:
        name_info = contact.get('name')
        break
    return name_info

def get_contact_name(contact_groups):
    """Функция для получения имени контактной группы"""
    name_info = None
    for contact in contact_groups:
        name_info = contact.get('name')
        break
    return name_info

def get_alert_chat_id(assigned_name):
    """"Функция получения chat_id для отправки сообщения"""
    # Разбор XML-файла и получение корневого элемента
    tree = ET.parse('data.xml')
    root = tree.getroot()
    # Находим все элементы header_footer внутри элемента user
    header_footer_elements = root.findall('.//user/header_footer')
    # Задаем начальное значение alert_chat_id
    alert_chat_id = None
    # Проходим циклом по всем найденным элементам header_footer
    for header_footer in header_footer_elements:
        # Сравниваем значение элемента name с assignee_name
        if header_footer.find('name').text == assigned_name:
            # Если значения совпадают, сохраняем значение элемента chat_id в alert_chat_id
            alert_chat_id = header_footer.find('chat_id').text
            break  # Выходим из цикла, т.к. нужный элемент уже найден

    # Если alert_chat_id не был найден, выводим ошибку
    if alert_chat_id is None:
        check_error_logger.error("Не удалось найти chat_id для пользователя {assigned_name} %s")
    return alert_chat_id

def process_ticket(ticket_data):
    """Функция по формированию данных из тикета"""
    status = ticket_data.get('status').get('behavior')
    # Узнаём в работе ли ещё тикет
    if status == "pending":
        ticket_id = ticket_data.get('id')
        priority_info = ticket_data.get('priority')
        priority_name = priority_info.get('name')
        user_info = ticket_data.get('user')
        contact_info = user_info.get('contact_groups')
        assigned_to = ticket_data.get('assigned_to')
        # Проверяем, что значение для поля last_staff_reply_at существует
        if ticket_data.get('last_staff_reply_at'):
            reple_client_time = ticket_data.get('last_staff_reply_at')
            time_difference = get_time_diff(reple_client_time)
            # Проверяем, что в тикете нет ответа более 3х дней
            if time_difference >= timedelta(days=3):
                name_info = get_contact_name(contact_info)
                assigned_name = get_assigned_name(assigned_to)
                # print(f"Время последнего ответа клиента в тикете {time_difference}")

                ticket_message = (f'Ожидание ответа клиента более 3-х дней.\nНомер тикета:: {ticket_id}\nПриоритет: {priority_name}\nНазвание клиента: {name_info}\nНазначен: {assigned_name}')

                # Получаем chat_id для отправки сообщения
                alert_chat_id = get_alert_chat_id(assigned_name)

                # Отправляем сообщение в телеграм-бот
                alert.send_telegram_message(alert_chat_id, ticket_message)
                check_info_logger.info('Сотруднику: %s в чат отправлена информация о 3х дневном неотвеченном тикете: %s', assigned_name, ticket_id)

def get_tickets():
    """Функция проверки тикетов, у которых нет ответа"""
    check_info_logger.info('Задача запущена')
    params = {
        #'status': '1,2,3',  # выбираем все статусы, кроме "Закрыт"
        #'status_neq': '4',       # Исключить закрытые статусы
        'category': '1',
        'status': '_pending',
        #'q': 'behavior:pending'
        #'q': 'unresponded:"true"'
        #'q': 'duedate:"today"'
    }
    url = API_ENDPOINT + '/tickets/?size=1&page=1'
    # проверка на доступность сервера, если сервер недоступен, выводит ошибку
    try:
        response = requests.get(url, auth=(API_KEY, API_SECRET), headers=HEADERS, params=params, timeout=10)
        response.raise_for_status()
    except requests.exceptions.Timeout as error_message:
        check_error_logger.error("Timeout error: request timed out: %s", error_message)
    except requests.exceptions.RequestException as error_message:
        check_error_logger.error("Error occurred: %s", error_message)

    data_res = response.json()
    page_info = data_res.get('page_info')
    last_index = page_info.get('last_index')
    if last_index == 0:
        print('No tickets')
    else:
        for page in range(last_index):
            url = API_ENDPOINT + f'/tickets/?size=1&page={page + 1}'
            # проверка на доступность сервера, если сервер недоступен, выводит ошибку
            try:
                response = requests.get(url, auth=(API_KEY, API_SECRET), headers=HEADERS, params=params, timeout=10)
                response.raise_for_status()
            except requests.exceptions.Timeout as error_message:
                check_error_logger.error("Timeout error: request timed out: %s", error_message)
            except requests.exceptions.RequestException as error_message:
                check_error_logger.error("Error occurred: %s", error_message)
            data = response.json()
            for ticket_data in data.get('data'):
                process_ticket(ticket_data)

# Создадим задачу на отправку алертов в чат
schedule.every().day.at("10:25").do(get_tickets)
check_info_logger.info('Задача на отправку алертов по 3х дневным простоям создана')

while True:
    # Получаем текущую дату и время
    now = datetime.datetime.now()
    # Проверяем, является ли текущий день недели будним днём (пн-пт)
    if now.weekday() < 5:
        # Запускаем отложенные задачи
        schedule.run_pending()
    # Ждём 1 секунду перед проверкой снова
    time.sleep(1)

if __name__ == '__main__':
    get_tickets()


import socket
from urllib.parse import urlparse

from ..exceptions import PortalConfiguration

_API_URI_MAX_LEN = 300
_API_URI_SCHEMES = ('https', 'http')

def validate_api_url(url: str) -> str:
    """Validates URL as DCSO Portal API endpoint, and returns this
    validated URL.

    Raises `DCSOPortalConfiguration` when url argument is not valid.
    """
    url = url.strip()

    if not url:
        raise PortalConfiguration("API URL is required")

    if len(url) > 300:
        raise PortalConfiguration(f"API URL exceeds maximum of {_API_URI_MAX_LEN} characters")

    # URL should not have any query or fragment parts
    if '?' in url or '#' in url:
        raise PortalConfiguration(f"API URL must contain query or fragments")

    u = urlparse(url)

    if u.scheme not in _API_URI_SCHEMES:
        raise PortalConfiguration(f"API URL use unsupported scheme '{u.scheme}'")

    if not u.netloc:
        raise PortalConfiguration(f"API URL does not specify a domain")

    return url

def free_localhost_tcp_port() -> int:
    """Finds a free TCP port on the localhost (using address 127.0.0.1)."""
    s = socket.socket(socket.AF_INET, type=socket.SOCK_STREAM)
    s.bind(('127.0.0.1', 0))
    port = s.getsockname()[1]
    s.close()
    return port


import taichi as ti
import argparse
import os
from FLIP import FLIP_2D
import imageio
import numpy as np

ti.init(arch=ti.cpu, default_ip=ti.i32)

obstacle = ti.Vector.field(2, dtype=ti.f32, shape=1)
env = FLIP_2D(1.0 / 120.0, 100, 100, 1.0, 1.0, 35 * 35)

obstacle[0][0] = 0.7
obstacle[0][1] = 0.25
obstacleRadius = 0.15

@ti.kernel
def setup_scene():
    for i, j in ti.ndrange(env.grid_x, env.grid_y):
        if i < 35 and j < 35:
            env.pos[i *35 + j] = ti.Vector([0.1 + 4 * env.radius * i, 0.9 - 4 * env.radius * j])
        env.status[i * 100 + j] = 1.0
        if (i == 0 or i == env.grid_x - 1 or j ==0):
            env.status[i * 100 + j] = 0.0
        xx = (i + 0.5) * env.dx
        yy = (j + 0.5) * env.dx
        if (xx - 0.7)**2 + (yy - 0.25)**2 < obstacleRadius**2:
            env.status[i * 100 + j] = 0.0

def main(args):

    window = ti.ui.Window("Taichi Paper Simulation on GGUI", (960, 960),
                          vsync=True, show_window=False)
    canvas = window.get_canvas()
    canvas.set_background_color((1, 1, 1))
    setup_scene()

    tot_step = 0
    frames = []
    save_path = f"imgs/FLIP"
    canvas.set_background_color((1, 1, 1))
    canvas.circles(env.pos, env.radius, (0, 0, 1))
    canvas.circles(obstacle, obstacleRadius, (0.1, 0.1, 0.1))
    canvas.set_image(window.get_image_buffer_as_numpy())
    window.save_image(os.path.join(save_path, f"{0}.png"))
    while window.running:
        tot_step += 1
        if(tot_step > 20):
            break
        print(tot_step)

        env.simulate_step(0.95, 2, 50, 1.9, obstacle[0][0], obstacle[0][1], obstacleRadius)
        canvas.set_background_color((1, 1, 1))
        canvas.circles(env.pos, env.radius, (0, 0, 1))
        canvas.circles(obstacle, obstacleRadius, (0.1, 0.1, 0.1))
        canvas.set_image(window.get_image_buffer_as_numpy())
        window.save_image(os.path.join(save_path, f"{tot_step}.png"))

    for i in range(1, tot_step):
        filename = os.path.join(save_path, f"{i}.png")
        frames.append(imageio.imread(filename))

    gif_name = os.path.join(save_path, f"GIF_{args.exp_id}.gif")
    imageio.mimsave(gif_name, frames, 'GIF', duration=0.02)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--exp_id", type=int, default=0)
    main(parser.parse_args())


import pandas as pd
from sklearn import tree
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix, plot_confusion_matrix
import graphviz
import matplotlib.pyplot as plt
import seaborn as sns

def readInputFile():
    df = pd.read_csv('D:\Homework\Artificial Intelligent\Lab2\mushrooms.csv', sep=',') # read mushrooms.csv to dataframe
    data = df.drop('class', axis=1).copy()
    X = pd.get_dummies(data, columns=data.columns.values.tolist()).copy()

    res = df['class'].copy()
    res[res == 'p'] = 1 # change p to 1
    res[res == 'e'] = 0 # change e to 0
    y = res.values.tolist()

    return X, y

def prepDataset(X, y):
    ratio = [0.4, 0.6, 0.8, 0.9] # list of ratios (train/total) using for prepare the subsets
    subsetItems = []

    for r in ratio:
        X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=r)
        subsetItems.append({'X_train': X_train, 'X_test': X_test, 'y_train': y_train, 'y_test': y_test})

    return subsetItems # list of objects which contain train sets and test sets for each ratio above

def constructDecisionTree(trainTestSubsets):
    split_ratio = [0.4, 0.6, 0.8, 0.9]
    decisionTrees = []

    for subset, ratio in zip(trainTestSubsets, split_ratio):
        clf = tree.DecisionTreeClassifier(criterion='entropy')
        clf = clf.fit(subset['X_train'], subset['y_train'])
        dot_data = tree.export_graphviz(clf, out_file=None, feature_names=subset['X_train'].columns, class_names=['e','p'], rounded=True, filled=True)
        graph = graphviz.Source(dot_data)
        graph.render('output/train_{}_decision_tree'.format(ratio), cleanup=True) #file's name and file's path for exporting
        decisionTrees.append(clf)

    return decisionTrees # list of decision trees

def printOutAllReports(decisionTrees, testSets):
    for tree, test, i in zip(decisionTrees, testSets, range(1, len(decisionTrees) + 1)):
        y_pred = tree.predict(test['X_test'])
        print("Report #{}: \n".format(i), classification_report(test['y_test'], y_pred, target_names=['e','p']))
        plot_confusion_matrix(tree, test['X_test'], test['y_test'])
        filename = 'output/confusion_matrix_{}.png'.format(i) #file's name and file's path for exporting
        print('Confusion matrix was saved as {}\n'.format(filename))
        plt.savefig(filename)
        plt.clf()
    return

def constructDecisionTreeWithDepth(subsets):
    depthList = [None, 2, 3, 4, 5, 6, 7]
    for depth in depthList:
        # build dicision trees of different depths
        clf = tree.DecisionTreeClassifier(criterion='entropy', max_depth=depth)
        clf = clf.fit(subsets[2]['X_train'], subsets[2]['y_train'])
        dot_data = tree.export_graphviz(clf, out_file=None, feature_names=subsets[2]['X_train'].columns, class_names=['e','p'], rounded=True, filled=True)
        graph = graphviz.Source(dot_data)
        graph.render('output/decision_tree_with_depth_{}'.format(depth), cleanup=True) #file's name and file's path for exporting

        # print out classification report and confusion matrix for each tree with corresponding test sets
        y_pred = clf.predict(subsets[2]['X_test'])
        print("Report (depth = {}): \n".format(depth), classification_report(subsets[2]['y_test'], y_pred, target_names=['e','p']))
        plot_confusion_matrix(clf, subsets[2]['X_test'], subsets[2]['y_test'])
        filename = 'output/confusion_matrix_depth_{}.png'.format(depth) #file's name and file's path for exporting
        print('Confusion matrix was saved as {}\n'.format(filename))
        plt.savefig(filename)
        plt.clf()
    return


import torch
from torch import einsum
import numpy as np
from transformers import CLIPTokenizer
from einops import rearrange, repeat

from ldm.modules.attention import default, exists

def get_word_inds(text, words, tokenizer):
    split_text = text.split(" ")
    if type(words) is int:
        words = [words]
    elif type(words) is str:
        words = words.split(" ")
    if type(words[0]) is str:
        words = [i for i, word in enumerate(split_text) if word in words]
    out = []
    if len(words) > 0:
        words_encode = [tokenizer.decode([item]).strip("#") for item in tokenizer.encode(text)][1:-1]
        cur_len, ptr = 0, 0
        for i in range(len(words_encode)):
            cur_len += len(words_encode[i])
            if ptr in words:
                out.append(i + 1)
            if cur_len >= len(split_text[ptr]):
                ptr += 1
                cur_len = 0
    return np.array(out)

def cross_attention_masks(mask=None, tokens_in=None, tokens_out=None, w_in=1., w_out=1., max_tokens=77):
    if mask is not None:
        mask = mask[:, 0, :, :]
        b, h, w = mask.shape
        token_mask = torch.zeros((b, max_tokens, h, w)).to(mask)
        if tokens_in is not None:
            for token in tokens_in:
                token_mask[:, token, ...] = mask * w_in
        if tokens_out is not None:
            for token in tokens_out:
                token_mask[:, token, ...] = (1 - mask) * w_out
    else:
        token_mask = None
    return token_mask

def register_masked_cross_attention(model, token_mask=None, stop_masking=0):
    """
    Replaces all cross-attention layers in model with new forward that implements paint-by-word for localized token
    enhancement.
    """
    def get_forward(self):
        def forward(x, context=None, mask=None):
            h = self.heads

            q = self.to_q(x)
            context, t, beta = default(context, (x, 0, 0))
            k = self.to_k(context)
            v = self.to_v(context)

            q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> (b h) n d', h=h), (q, k, v))

            sim = einsum('b i d, b j d -> b i j', q, k) * self.scale

            if exists(mask):
                mask = rearrange(mask, 'b ... -> b (...)')
                max_neg_value = -torch.finfo(sim.dtype).max
                mask = repeat(mask, 'b j -> (b h) () j', h=h)
                sim.masked_fill_(~mask, max_neg_value)

            if exists(token_mask) and t.min().item() >= stop_masking:
                b = token_mask.shape[0]
                latent_size = int(sim.shape[1] ** 0.5)  # using -b*h as indexing makes it work in classifier-free guidance case (batch*2)
                token_mask_r = torch.nn.functional.interpolate(token_mask, size=latent_size)
                token_mask_r = rearrange(token_mask_r, 'b T h w -> b (h w) T')
                token_mask_r = repeat(token_mask_r, 'b ... -> (b h) ...', h=h)
                beta = repeat(beta[-b:], 'b ... -> (b h) 1 1 ...', h=h)
                wA = torch.log(1 + beta) * sim.max() * token_mask_r  # paint by word scheduling weight
                sim[-b*h:] = sim[-b*h:] + wA.type(sim.dtype)  # paint by word (only for cond samples)

            # attention, what we cannot get enough of
            attn = sim.softmax(dim=-1)

            out = einsum('b i j, b j d -> b i d', attn, v)
            out = rearrange(out, '(b h) n d -> b n (h d)', h=h)
            return self.to_out(out)

        return forward

    def register_ca(name_, net_, count):
        if name_ == 'attn2' and net_.__class__.__name__ == 'CrossAttention':
            net_.forward = get_forward(net_)
            return count + 1
        elif hasattr(net_, 'named_children'):
            for name__, net__ in net_.named_children():
                count = register_ca(name__, net__, count)
        return count

    cross_att_count = 0
    for name, net in model.model.named_children():
        cross_att_count += register_ca(name, net, 0)

def masked_cross_attention(model, prompts, words_in, words_out, mask, tokenizer, w_in=0., w_out=0., stop_masking=0):
    if exists(mask) and (exists(words_in) or exists(words_out)):
        token_mask = []
        for i, prompt in enumerate(prompts):
            tokens_in = get_word_inds(prompt, words_in, tokenizer) if exists(words_in) else list()
            tokens_out = get_word_inds(prompt, words_out, tokenizer) if exists(words_out) else list()
            token_mask.append(cross_attention_masks(mask[i:i+1], tokens_in, tokens_out, w_in, w_out))
        token_mask = torch.concat(token_mask, dim=0)
        register_masked_cross_attention(model, token_mask, stop_masking)

if __name__ == "__main__":
    tokenizer = CLIPTokenizer.from_pretrained("openai/clip-vit-large-patch14")
    print(get_word_inds("A photograph of a sofa in a living room", ["living", "room"], tokenizer))


from mrjob.job import MRJob

##########################################################################
# Quero saber a minima temperatura maxima e maxima temperatura minima ####
##########################################################################

class TempMinMax(MRJob):
  # Step 1
  def mapper(self, _, line):
    tokens = line.split(',')

    location = tokens[0]
    metric = tokens[2]
    value = tokens[3]

    if metric == 'TMIN' or metric == 'TMAX':
      # Agrupar por localizaca e metrica
      yield (location, metric), float(value)

  def reducer(self, key, values):
    items = list(values)
    location = key[0]
    metric = key[1]

    # Menor temp. observada
    if metric == 'TMAX':
      yield location, min(items)
    else:
      yield location, max(items)

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


from django.shortcuts import render, redirect
#from django.contrib.auth.forms import UserCreationForm
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .forms import UserRegistrationForm, UserUpdateForm, ProfileUpdateForm

# Create your views here.
def register(request):
    if request.method == 'POST':
        form = UserRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            #messages.success(request, f'Account created for {username}!')
            messages.success(request, f'Your account has been created! You are now able to log in')
            return redirect('login')
    else:
        form = UserRegistrationForm()

    return render(request, 'users/register.html',{'form': form})

@login_required
def profile(request):
    if request.method == 'POST':
        u_from = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST,request.FILES, instance=request.user.profile)
        #a = request.POST.get('username')

        if u_from.is_valid() and p_form.is_valid():
            u_from.save()
            p_form.save()

            messages.success(request, f'Your Profile has been updated Successfully')
            return redirect('login')
    else:
        u_from = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)

    context = {
        'u_form' : u_from,
        'p_form' : p_form
    }
    return render(request, 'users/profile.html', context)


import os
import numpy as np
import json
import cv2
from tqdm import tqdm
import random

def create_dict():
    dict = {"instruction": [], "input": [], "output": []}
    return dict

def get_obj_list(scene_name_all, scene_name_list_one):

    diff_list = list(set(scene_name_all).difference(set(scene_name_list_one)))
    scene_name_list_one_copy = scene_name_list_one.copy()
    change_number = random.randint(1, int(len(scene_name_list_one_copy) / 2))
    random.shuffle(scene_name_list_one_copy)
    if change_number < len(diff_list):
        new_obj_name_list = scene_name_list_one_copy[:-change_number] + random.sample(diff_list, change_number)
    else:
        new_obj_name_list = scene_name_list_one_copy[:-len(diff_list)] + diff_list

    if len(scene_name_list_one_copy) <= 40:
        change_number = random.randint(int(len(scene_name_list_one_copy) / 2), len(scene_name_list_one_copy))
    else:
        change_number = random.randint(int(len(scene_name_list_one_copy) / 2), 40)
    new_obj_name_list = random.sample(new_obj_name_list, change_number)

    return new_obj_name_list

base_path = os.getcwd()
data_path = os.path.join(base_path, "dataset", "train")
scene_file_list = os.listdir(data_path)

scene_name_list = []

kitchens_train = [f"FloorPlan{i}" for i in range(1, 21)]
living_rooms_train = [f"FloorPlan{200 + i}" for i in range(1, 21)]
bedrooms_train = [f"FloorPlan{300 + i}" for i in range(1, 21)]
bathrooms_train = [f"FloorPlan{400 + i}" for i in range(1, 21)]

scene_name_list.append(kitchens_train)
scene_name_list.append(living_rooms_train)
scene_name_list.append(bedrooms_train)
scene_name_list.append(bathrooms_train)

save_path = "alpaca_6k.json"
json_list = []
for scene_name_one in tqdm(scene_name_list):
    scene_name_all = []
    scene_name_all_list = []
    for scene_one in scene_name_one:
        scene_one_path = os.path.join(data_path, scene_one + "_" + str(0))
        scene_one_name_txt = os.path.join(scene_one_path, "name.txt")
        with open(scene_one_name_txt, "r") as tf:
            name_list_one = tf.read().split(',')
        scene_name_all.extend(name_list_one)
        scene_name_all_list.append(name_list_one)

    scene_name_all = list(set(scene_name_all))
    for scene_ind in range(len(scene_name_one)):
        for i in range(80):
            new_obj_list = get_obj_list(scene_name_all, scene_name_all_list[scene_ind])
            new_dict = create_dict()
            new_obj_list = ",".join(new_obj_list)
            new_obj_list = "[" + new_obj_list.strip() + "]"
            new_dict["input"] = new_obj_list.strip()
            json_list.append(new_dict)

with open(save_path, 'w') as f:
    json_str = json.dumps(json_list, indent=2)
    f.write(json_str)
    f.write('\n')


from random import*
def Bigbaicai():
    quan=(eval)(input("请输入0(石头)1(剪刀)2(布):"))
    computer=randint(0,3)
    print("你输入的值是:{}".format(quan))
    print("电脑输入的值是:{}".format(computer))
    if (quan==computer):
        print("you are equals")
    elif((quan==1 and computer==2)or(quan==2 and computer==0) or(quan==0 and computer==1)):
        print("you are winner")
    else:
        print("you are loser")
def Isprime(b):
    a=[]
    for i in range(2,b):
        for j in range(2,i):
            if (i%j==0):
                break
        else:
            a.append(i)
    return a
def shuixianhua(a,b):
    l=[]
    for i in range(a,b):
        z=i%10
        x=(i//10)%10
        y=i//100
        if(z**3+x**3+y**3==i):
            l.append(i)
    return l
def Steal():
    day=9
    n=1
    for i in range(9):
        n=(n+1)*2
    return n
if __name__=='__main__':
    print(Steal())
    print(Isprime(100))
    print(shuixianhua(100,1000))
    Bigbaicai()


class Solution:
    def minPathSum(self, grid: List[List[int]]) -> int:
        m = len(grid)
        n = len(grid[0])
        dp = [ [math.inf]*n for i in range(m)]

        for i in range(0, m):
            for j in range(0, n):
                if i == 0 and j == 0:
                    # set starting point
                    dp[0][0] = grid[0][0]
                # since you can only move down or right, therefore the best path for straight down is itself + previous cell
                elif i == 0:
                    dp[i][j] = grid[i][j] + dp[i][j-1]
                elif j == 0:
                    dp[i][j] = grid[i][j] + dp[i-1][j]
                else:
                    # update current cell with the minimum of the result of previously going down or right
                    dp[i][j] = min(grid[i][j] + dp[i-1][j], grid[i][j] + dp[i][j-1])
        # return result for final cell
        return dp[m-1][n-1]


import time
from typing import Any
from absl import logging
import pandas as pd

ACTION = 'Action'
CAMPAIGN_STATUS = 'Campaign status'
CUSTOMER_ID = 'Customer ID'
CAMPAIGN = 'Campaign'
CAMPAIGN_TYPE = 'Campaign type'
BID_STRATEGY_TYPE = 'Bid strategy type'
LABEL = 'Label'
UPDATES_APPLIED = 'Updates applied'

_COLS = [
    ACTION,
    CAMPAIGN_STATUS,
    CUSTOMER_ID,
    CAMPAIGN,  # Campaign name
    CAMPAIGN_TYPE,
    BID_STRATEGY_TYPE,
    LABEL,
    UPDATES_APPLIED,  # Updates applied to this DataFrame / Row.
]

_DEFAULT_ACTION = 'Add'
_DEFAULT_STATUS = 'Paused'
_DEFAULT_CUSTOMER_ID = 'Enter customer ID'
_DEFAULT_LABEL = 'Keyword Translator'

class Campaigns:
  """A class to represent data for a customer's Google Ads campaigns.

  The columns should match the columns specified in a Google Ads Editor "Create
  New Campaign" template.
  See:
  https://support.google.com/google-ads/answer/10702525?hl=en#zippy=%2Ccampaigns

  Example usage:

  # Initializes campaigns DataFrame from Google Ads API response.
  get_campaign_data_response = google_ads_api_client.get_campaign_data(...)
  campaigns = campaigns_lib.Campaigns(get_campaigns_response)

  # Iterates around the data using df() method.
  for index, row in campaigns.df().iterrows():
    print(row)

  # Passes into GoogleAdsObjects class for transformation.
  google_ads_objects = google_ads_objects_lib.GoogleAdsObjects(campaigns...)

  translator_response = keyword_translator(google_ads_objects)

  campaigns = translator_response.get_campaigns()

  # Records an update to the campaign data. For example, record that translation
  # was applied to 'campaign 1'.
  campaigns.add_update(update='translated', campaigns='campaign 1')

  # Writes campaign data to CSV.
  campaigns.to_csv()
  """

  def __init__(self, response_jsons: list[list[Any]]) -> None:
    """Initializes the Campaigns object.

    Creates a DataFrame containing Campaign data from a Google Ads API
    searchStream response.

    See class docstring for more details.

    Args:
      response_jsons: A list of JSON responses from a Google Ads API
        searchStream request. The requests should contain the following fields:
        campaign.name
        campaign.advertising_channel_type
        cmapaign.bidding_strategy_type
    """
    self._df = self._build_campaign_df(response_jsons=response_jsons)
    logging.info('Initialized Campaigns DataFrame with length %d.', self.size())
    self._campaigns_list = self._build_campaigns_list(
        response_jsons=response_jsons
    )

  def _build_campaign_df(self, response_jsons: list[Any]) -> pd.DataFrame:
    """Builds a DataFrame containing Campaign data from Google Ads API JSON.

    Args:
      response_jsons: A list of JSON responses from a Google Ads API
        searchStream request.

    Returns:
      A DataFrame containing Campaign data.
    """
    campaigns = []
    for response_json in response_jsons:
      for batch in response_json:
        for result in batch['results']:
          campaigns.append(
              {
                  ACTION: _DEFAULT_ACTION,
                  CAMPAIGN_STATUS: _DEFAULT_STATUS,
                  CUSTOMER_ID: _DEFAULT_CUSTOMER_ID,
                  CAMPAIGN: result['campaign']['name'],
                  CAMPAIGN_TYPE: result['campaign']['advertisingChannelType'],
                  BID_STRATEGY_TYPE: result['campaign']['biddingStrategyType'],
                  LABEL: _DEFAULT_LABEL,
                  UPDATES_APPLIED: [],
              })

    return pd.DataFrame(campaigns, columns=_COLS)

  def _build_campaigns_list(
      self, response_jsons: list[Any]
  ) -> list[dict[str, str]]:
    """Builds a list of Campaign data from Google Ads API JSON.

    Args:
      response_jsons: A list of JSON responses from a Google Ads API
        searchStream request.

    Returns:
      A list of dicts with campaign id and name.
    """
    campaigns = []
    for response_json in response_jsons:
      for batch in response_json:
        for result in batch['results']:
          campaigns.append({
              'id': result['campaign']['id'],
              'name': result['campaign']['name'],
          })

    return campaigns

  def df(self) -> pd.DataFrame:
    """Returns the DataFrame containing Campaign data."""
    return self._df

  def campaigns_list(self) -> list[dict[str, str]]:
    return self._campaigns_list

  def csv_data(self) -> str:
    """Returns the DataFrame as CSV."""
    return self._df.to_csv(index=False) or str(_COLS)

  def csv_file_name(self) -> str:
    """Returns the CSV file name."""
    time_str = time.strftime('%Y%m%d-%H%M%S')
    return f'campaigns_{time_str}.csv'

  def add_update(self, update: str) -> None:
    """Marks the row as having been updated by a processor.

    Args:
      update: The name of the update to add. E.g. "translated".
    """
    for index in range(self.size()):
      self._df.loc[index, UPDATES_APPLIED].append(update)
    logging.info('Applied update to Campaign DataFrame: %s.', update)

  def columns(self) -> list[str]:
    """Returns the Campaign columns."""
    return _COLS

  def size(self) -> int:
    """Returns the number of Campaign rows."""
    return len(self._df)

  def campaign_names(self) -> list[str]:
    """Returns a list of unique Campaign names."""
    return list(self._df[CAMPAIGN].unique())


from ast import Sub
import gzip
import hashlib
import os
from typing import Dict, List
from urllib.request import urlretrieve

import boto3
from botocore import UNSIGNED
from botocore.client import Config
from canvasapi import Canvas
import numpy as np
import numpy as np
import pandas as pd
from patsy import dmatrices
import plotly.graph_objects as go
import plotly.express as px
from plotly import colors
from plotly.subplots import make_subplots
import quandl
import requests
from scipy import stats
import statsmodels.api as sm
from tqdm.notebook import tqdm

# =============================================================================
# Credentials
# =============================================================================

quandl.ApiConfig.api_key = os.getenv("QUANDL_API_KEY")

# =============================================================================
# Canvas
# =============================================================================

def download_files(filename_frag: str):
    """Downloads files from Canvas with `filename_frag` in filename."""

    url = os.getenv("CANVAS_URL")
    token = os.getenv("CANVAS_TOKEN")

    course_id = 33395
    canvas = Canvas(url, token)
    course = canvas.get_course(course_id)

    for f in course.get_files():
        if filename_frag in f.filename:
            print(f.filename, f.id)
            file = course.get_file(f.id)
            file.download(file.filename)

# =============================================================================
# Reading Data
# =============================================================================

def get_trade_data(pair: str, year: str, path: str = "accumulation_opportunity/data"):
    """Reads local gzipped trade data file and return dataframe."""

    dtypes = {
        "PriceMillionths": int,
        "Side": int,
        "SizeBillionths": int,
        "timestamp_utc_nanoseconds": int,
    }

    filename = f"trades_narrow_{pair}_{year}.delim.gz"
    delimiter = {"2018": "|", "2021": "\t"}[year]

    with gzip.open(f"{path}/{filename}") as f:
        df = pd.read_csv(f, delimiter=delimiter, usecols=dtypes.keys(), dtype=dtypes)

    df.timestamp_utc_nanoseconds = pd.to_datetime(df.timestamp_utc_nanoseconds)

    return df.set_index("timestamp_utc_nanoseconds")

# =============================================================================
# Price Data
# =============================================================================

def get_table(dataset_code: str, database_code: str = "ZACKS"):
    """Downloads Zacks fundamental table from export api to local zip file."""

    url = (
        f"https://www.quandl.com/api/v3/datatables/{database_code}/{dataset_code}.json"
    )
    r = requests.get(
        url, params={"api_key": os.getenv("QUANDL_API_KEY"), "qopts.export": "true"}
    )
    data = r.json()
    urlretrieve(
        data["datatable_bulk_download"]["file"]["link"],
        f"zacks_{dataset_code.lower()}.zip",
    )

def load_table_files(table_filenames: Dict):
    """Loads Zacks fundamentals tables from csv files."""

    dfs = []
    for v in tqdm(table_filenames.values()):
        dfs.append(pd.read_csv(v, low_memory=False))

    return dfs

def get_hash(string: str) -> str:
    """Returns md5 hash of string."""

    return hashlib.md5(str(string).encode()).hexdigest()

def fetch_ticker(
    dataset_code: str, query_params: Dict = None, database_code: str = "EOD"
):
    """Fetches price data for a single ticker."""

    url = f"https://www.quandl.com/api/v3/datasets/{database_code}/{dataset_code}.json"

    params = dict(api_key=os.getenv("QUANDL_API_KEY"))
    if query_params is not None:
        params = dict(**params, **query_params)

    r = requests.get(url, params=params)

    dataset = r.json()["dataset"]
    df = pd.DataFrame(
        dataset["data"], columns=[c.lower() for c in dataset["column_names"]]
    )
    df["ticker"] = dataset["dataset_code"]

    return df.sort_values("date")

def fetch_all_tickers(tickers: List, query_params: Dict) -> pd.DataFrame:
    """Fetches price data from Quandl for each ticker in provide list and
    returns a dataframe of them concatenated together.
    """

    df_prices = pd.DataFrame()
    for t in tqdm(tickers):
        try:
            df = fetch_ticker(t, query_params)
            df_prices = pd.concat([df_prices, df])
        except:
            print(f"Couldn't get prices for {t}.")

    not_missing_data = (
        df_prices.set_index(["ticker", "date"])[["adj_close"]]
        .unstack("date")
        .isna()
        .sum(axis=1)
        == 0
    )

    df_prices = df_prices[
        df_prices.ticker.isin(not_missing_data[not_missing_data].index)
    ]

    return df_prices.set_index(["ticker", "date"])

# =============================================================================
# Download Preprocessed Files from S3
# =============================================================================

def upload_s3_file(filename: str):
    """Uploads file to S3. Requires credentials with write permissions to exist
    as environment variables.
    """

    client = boto3.client("s3")
    client.upload_file(filename, "finm33150", filename)

def download_s3_file(filename: str):
    """Downloads file from read only S3 bucket."""

    client = boto3.client("s3", config=Config(signature_version=UNSIGNED))
    client.download_file("finm33150", filename, filename)

# =============================================================================
# Prepare Data
# =============================================================================

def get_data(date_range: pd.date_range) -> pd.DataFrame:
    """
    Loads spreads and prices, calculates spreads and returns dataframe.
    """

    # load spreads data
    df_spreads = (
        pd.read_csv("Liq5YCDS.delim", sep="\t", index_col=0, parse_dates=["date"])
        .set_index(["ticker", "date"])
        .unstack("ticker")
        .loc[date_range, ["spread5y"]]
    )
    df_returns = np.log(df_spreads / df_spreads.shift())
    df_returns.columns = df_returns.columns.set_levels(["r_spread"], level=0)
    df_spreads = pd.concat([df_spreads, df_returns], axis=1)

    # load prices
    df_prices = (
        pd.read_csv("df_prices_new.csv", parse_dates=["date"])
        .set_index(["ticker", "date"])[["adj_close"]]
        .unstack("ticker")
        .reindex(date_range)
        .ffill()
    )
    assert len(df_prices) == len(df_spreads)

    df_prices.columns = df_prices.columns.set_names(["series", "ticker"])
    df_returns = np.log(df_prices / df_prices.shift())
    df_returns.columns = df_returns.columns.set_levels(["r_equity"], level=0)
    df_prices = pd.concat([df_prices, df_returns], axis=1)
    df_data = pd.concat([df_prices, df_spreads], axis=1).iloc[1:]
    df_data = df_data.stack("ticker")
    df_data.index = df_data.index.set_names(["date", "ticker"])

    # Excludes return of subject security
    df_data["r_index"] = (
        df_data.groupby("date")["r_spread"].transform("sum") - df_data["r_spread"]
    ) / (df_data.groupby("date")["r_spread"].transform("count") - 1)

    df_data["r_spy"] = df_data.index.get_level_values("date").map(
        df_data[df_data.index.get_level_values("ticker") == "SPY"]
        .reset_index()
        .set_index("date")
        .r_equity
    )

    df_data = df_data[df_data.index.get_level_values("ticker") != "SPY"]
    df_data.index = df_data.index.remove_unused_levels()

    return df_data

def get_daily_data():
    """
    Loads spreads and prices, calculates spreads and returns dataframe on daily
    basis with no re-indexing.
    """

    df_spreads = (
        pd.read_csv("Liq5YCDS.delim", sep="\t", index_col=0, parse_dates=["date"])
        .set_index(["ticker", "date"])
        .unstack("ticker")[["spread5y"]]
    )
    df_returns = np.log(df_spreads / df_spreads.shift())
    df_returns.columns = df_returns.columns.set_levels(["r_spread"], level=0)
    df_spreads = pd.concat([df_spreads, df_returns], axis=1)

    df_prices = (
        pd.read_csv("df_prices_new.csv", parse_dates=["date"])
        .set_index(["ticker", "date"])[["adj_close"]]
        .unstack("ticker")
        .ffill()
    )

    df_prices.columns = df_prices.columns.set_names(["series", "ticker"])
    df_returns = np.log(df_prices / df_prices.shift())
    df_returns.columns = df_returns.columns.set_levels(["r_equity"], level=0)
    df_prices = pd.concat([df_prices, df_returns], axis=1)
    df_data = pd.concat([df_prices, df_spreads], axis=1).iloc[1:]
    df_data = df_data.stack("ticker")
    df_data.index = df_data.index.set_names(["date", "ticker"])

    # Excludes return of subject security
    df_data["r_index"] = (
        df_data.groupby("date")["r_spread"].transform("sum") - df_data["r_spread"]
    ) / (df_data.groupby("date")["r_spread"].transform("count") - 1)

    df_data["r_spy"] = df_data.index.get_level_values("date").map(
        df_data[df_data.index.get_level_values("ticker") == "SPY"]
        .reset_index()
        .set_index("date")
        .r_equity
    )

    df_data = df_data[df_data.index.get_level_values("ticker") != "SPY"]
    df_data.index = df_data.index.remove_unused_levels()
    df_data = df_data.loc["2018-01-03":"2020-04-20"]

    return df_data

# =============================================================================
# Regression
# =============================================================================

def get_errors(
    df_data: pd.DataFrame,
    model: str = "OLS",
    penalty: str = "Huber",
    weeks_in: int = 16,
    weeks_out: int = 1,
    formula: str = "r_equity ~ r_spy + 1",
):

    errors_list = []
    for day in df_data.index.levels[0][: -(weeks_in + weeks_out - 1)]:
        df_in = df_data.loc[day : day + pd.DateOffset(weeks=weeks_in - 1)]
        df_out = df_data.loc[
            day
            + pd.DateOffset(weeks=weeks_in) : day
            + pd.DateOffset(weeks=weeks_in + weeks_out - 1)
        ]

        for ticker in df_in.index.levels[1]:
            y_in, X_in = dmatrices(
                formula,
                data=df_in.loc[df_in.index.get_level_values("ticker") == ticker],
                return_type="dataframe",
            )

            if model == "OLS":
                res = sm.OLS(y_in, X_in).fit()
            else:
                if penalty == "Tukey":
                    res = sm.RLM(y_in, X_in, M=sm.robust.norms.TukeyBiweight()).fit()
                else:
                    res = sm.RLM(y_in, X_in).fit()

            y_out, X_out = dmatrices(
                formula,
                data=df_out.loc[df_out.index.get_level_values("ticker") == ticker],
                return_type="dataframe",
            )
            df_e = (res.predict(X_out) - y_out[formula.split(" ~ ")[0]]).to_frame(
                name="resid"
            )
            df_e["scale"] = res.scale
            df_e["sresid"] = df_e.resid / df_e.scale
            df_e["model_date"] = df_in.reset_index().date.min()

            df_e["distance"] = df_e.groupby(["date"]).ngroup() + 1
            errors_list.append(df_e)

    return pd.concat(errors_list), res.summary()

def get_contemp_resids(df_data: pd.DataFrame) -> pd.DataFrame:
    """
    Wrapper function to return contemporaneous equity and cds
    residuals.
    """

    df_eq_errors, _ = get_errors(df_data, model="OLS")
    df_cds_errors, _ = get_errors(
        df_data, model="RLM", formula="r_spread ~ r_equity + r_index + 1"
    )

    df_resid = pd.concat(
        [df_cds_errors.resid, df_eq_errors.resid.groupby("ticker").shift()], axis=1
    )

    df_resid.columns = ["cds_resid", "eq_resid"]

    return df_resid.dropna()

def get_predicted_resids(df_resid: pd.DataFrame) -> pd.DataFrame:
    """
    Calculates regression coefficients and predicted residuals using
    discounted least squares for exponentially weighted and boxcar
    windows of 4 to 60 weeks.
    """

    df_resid = df_resid.unstack("ticker")
    win_lengths = np.arange(6, 47, 2)

    col_list = []
    for w in win_lengths:
        for s in df_resid.columns.levels[1]:
            df_pair = df_resid.loc[:, [("cds_resid", s), ("eq_resid", s)]]
            df_pair.columns = ["cds_resid", "eq_resid"]
            for win_type, df_win in {
                "exp_wm": df_pair.ewm(alpha=1 / w),
                "boxcar": df_pair.rolling(window=2 * w, min_periods=4),
            }.items():
                df_cov = df_win.cov()
                df_mean = df_win.mean()

                s_var = df_cov["eq_resid"].xs("eq_resid", level=1)
                s_var.name = ("var_x", win_type, f"t_{w:02d}", s)

                s_cov = df_cov["eq_resid"].xs("cds_resid", level=1)
                s_cov.name = ("cov_xy", win_type, f"t_{w:02d}", s)

                s_beta_1 = s_cov / s_var
                s_beta_1.name = ("beta_1", win_type, f"t_{w:02d}", s)

                s_beta_0 = s_beta_1 * df_mean.eq_resid - df_mean.cds_resid
                s_beta_0.name = ("beta_0", win_type, f"t_{w:02d}", s)

                s_resid_sq = (
                    s_beta_0 + df_pair.eq_resid * s_beta_1 - df_pair.cds_resid
                ).pow(2)
                s_resid_sq.name = ("resid_sq", win_type, f"t_{w:02d}", s)

                s_error_sq = (
                    df_pair.cds_resid.loc[~s_resid_sq.isna()]
                    - df_pair.cds_resid.loc[~s_resid_sq.isna()].mean()
                ).pow(2)
                s_error_sq.name = ("error_sq", win_type, f"t_{w:02d}", s)

                col_list.extend(
                    [s_resid_sq, s_error_sq, s_beta_0, s_beta_1, s_var, s_cov]
                )

    df_betas = pd.concat(col_list, axis=1)
    df_betas.columns.names = ["stat", "win_type", "win_length", "ticker"]
    df_betas = df_betas.stack("ticker")
    df_betas = df_betas.swaplevel("stat", "win_type", axis=1)

    return df_betas.sort_index(axis=1)

def get_ticker_rsq(df_resid_pred: pd.DataFrame) -> pd.DataFrame:
    """
    Calculates R-squared for each ticker for each combination of window
    methodology and length.
    """

    ticker_rsq = []
    for t in df_resid_pred.index.levels[1]:
        df = df_resid_pred.loc[df_resid_pred.index.get_level_values("ticker") == t]
        for w in df_resid_pred.columns.levels[0]:
            rsq = 1 - df[w].resid_sq.sum() / df[w].error_sq.sum()
            rsq.name = (t, w)
            ticker_rsq.append(rsq)

    df_ticker_rsq = pd.concat(ticker_rsq, axis=1).sort_index(axis=1)
    df_ticker_rsq.columns.names = ["ticker", "win_type"]

    return df_ticker_rsq

# =============================================================================
# Charts
# =============================================================================

COLORS = colors.qualitative.T10

def make_lines_chart(
    series: pd.Series,
    title: str = "5-Year CDS Spread",
) -> go.Figure:
    fig = go.Figure()

    for t in series.index.levels[1]:
        fig.add_trace(
            go.Scatter(
                x=series.index.levels[0],
                y=series.loc[series.index.get_level_values("ticker") == t],
                name=t,
                line=dict(width=1),
            )
        )

    fig.update(layout_title=title)

    return fig

IS_labels = [
    ("obs", lambda x: f"{x:>7d}"),
    ("min:max", lambda x: f"{x[0]:>0.4f}:{x[1]:>0.3f}"),
    ("mean", lambda x: f"{x:>7.4f}"),
    ("std", lambda x: f"{x:>7.4f}"),
    ("skewness", lambda x: f"{x:>7.4f}"),
    ("kurtosis", lambda x: f"{x:>7.4f}"),
]

def get_moments_annotation(
    s: pd.Series,
    xref: str,
    yref: str,
    x: float,
    y: float,
    xanchor: str,
    title: str,
    labels: List,
) -> go.layout.Annotation:
    """Calculates summary statistics for a series and returns and
    Annotation object.
    """
    moments = list(stats.describe(s.to_numpy()))
    moments[3] = np.sqrt(moments[3])

    sharpe = s.mean() / s.std()

    return go.layout.Annotation(
        text=(
            ("
").join(
                [f"{k[0]:<9}{k[1](moments[i])}" for i, k in enumerate(labels)]
            )
        ),
        align="left",
        showarrow=False,
        xref=xref,
        yref=yref,
        x=x,
        y=y,
        bordercolor="black",
        borderwidth=0.5,
        borderpad=2,
        bgcolor="white",
        xanchor=xanchor,
        yanchor="top",
    )

def make_overview_chart(
    series: pd.DataFrame, title: str, subtitle_base: str = "Residuals"
) -> go.Figure:

    fig = make_subplots(
        rows=1,
        cols=2,
        subplot_titles=[
            f"{subtitle_base} Distribution",
            f"Q/Q Plot",
        ],
        vertical_spacing=0.09,
        horizontal_spacing=0.08,
    )

    # Returns Distribution
    series_cuts = pd.cut(series, 100).value_counts().sort_index()
    midpoints = series_cuts.index.map(lambda interval: interval.right).to_numpy()
    norm_dist = stats.norm.pdf(midpoints, loc=series.mean(), scale=series.std())

    fig.add_trace(
        go.Bar(
            x=[interval.mid for interval in series_cuts.index],
            y=series_cuts / series_cuts.sum(),
            name="pct. of returns",
            marker=dict(color=COLORS[0]),
        ),
        row=1,
        col=1,
    )

    fig.add_trace(
        go.Scatter(
            x=[interval.mid for interval in series_cuts.index],
            y=norm_dist / norm_dist.sum(),
            name="normal",
            line=dict(width=1, color=COLORS[1]),
        ),
        row=1,
        col=1,
    )

    # Q/Q Data
    returns_norm = ((series - series.mean()) / series.std()).sort_values()
    norm_dist = pd.Series(
        list(map(stats.norm.ppf, np.linspace(0.001, 0.999, len(series)))),
        name="normal",
    )

    fig.append_trace(
        go.Scatter(
            x=norm_dist,
            y=returns_norm,
            name="resid. norm.",
            mode="markers",
            marker=dict(color=COLORS[0], size=3),
        ),
        row=1,
        col=2,
    )

    fig.add_trace(
        go.Scatter(
            x=norm_dist,
            y=norm_dist,
            name="norm.",
            line=dict(width=1, color=COLORS[1]),
        ),
        row=1,
        col=2,
    )

    fig.add_annotation(
        get_moments_annotation(
            series.dropna(),
            xref="paper",
            yref="paper",
            x=0.95,
            y=0.45,
            xanchor="right",
            title="Returns",
            labels=IS_labels,
        ),
        font=dict(size=6, family="Courier New, monospace"),
    )

    fig.update_xaxes(showline=True, linewidth=1, linecolor="black", mirror=True)
    fig.update_yaxes(showline=True, linewidth=1, linecolor="black", mirror=True)

    fig.update_layout(
        title_text=(
            f"{title}
"
            f"{series.index.levels[0].min().strftime('%Y-%m-%d')}"
            f" - {series.index.levels[0].max().strftime('%Y-%m-%d')}"
        ),
        showlegend=False,
        font=dict(size=10),
        margin=dict(l=50, r=50, b=50, t=100),
        yaxis=dict(tickformat="0.3f"),
        yaxis2=dict(tickformat="0.1f"),
        xaxis2=dict(tickformat="0.1f"),
    )

    for i in fig["layout"]["annotations"]:
        i["font"]["size"] = 12

    fig.update_annotations(font=dict(size=10))

    return fig

def make_residual_scatter(df_resid: pd.DataFrame, n_trend_obs: int = 200) -> go.Figure:
    df_resid.eq_resid = df_resid.eq_resid.shift()
    df_resid = df_resid.dropna()

    ols_result = stats.linregress(df_resid.eq_resid, df_resid.cds_resid)
    # print(ols_result.slope ** 2)

    fig = px.scatter(
        y=df_resid.cds_resid,
        x=df_resid.eq_resid,
        title="Equity Return Residual vs. CDS Return Residual",
        labels=dict(y="cds residual (n)", x="equity residual (n-1)"),
    )

    fig.add_scatter(
        x=[-0.3, 0.3],
        y=[ols_result.intercept + ols_result.slope * x for x in [-0.3, 0.3]],
        mode="lines",
    )

    fig.update_layout(showlegend=False)

    return fig

def make_rsq_comparison(df_resid_pred: pd.DataFrame) -> go.Figure:
    fig = go.Figure()

    for w in df_resid_pred.columns.levels[0]:
        rsq = 1 - df_resid_pred[w].resid_sq.sum() / df_resid_pred[w].error_sq.sum()
        fig.add_trace(go.Bar(x=rsq.index, y=rsq.values, name=w))

    fig.update_layout(title="Comparision of R-squared: Window Type by Length")

    return fig

def make_rsq_ticker_comparison(df_resid_pred: pd.DataFrame) -> go.Figure:
    tickers = {
        "BA": (1, 1),
        "C": (1, 2),
        "DD": (2, 1),
        "F": (2, 2),
        "GE": (3, 1),
        "JPM": (3, 2),
        "LOW": (4, 1),
        "LUV": (4, 2),
        "MAR": (5, 1),
        "T": (5, 2),
        "WFC": (6, 1),
        "XOM": (6, 2),
    }

    fig = make_subplots(
        rows=6,
        cols=2,
        subplot_titles=list(tickers.keys()),
        vertical_spacing=0.09,
        horizontal_spacing=0.08,
    )

    for t, (r, c) in tickers.items():
        for j, w in enumerate(df_resid_pred.columns.levels[1]):
            fig.add_trace(
                go.Bar(
                    x=df_resid_pred.index,
                    y=df_resid_pred[t][w],
                    name=w,
                    marker=dict(color=COLORS[j]),
                    showlegend=True if t == "BA" else False,
                    legendgroup=w,
                ),
                row=r,
                col=c,
            )

    fig.update_layout(title="Comparision of R-squared: Window Type by Length")

    fig.update_layout(height=1600)
    return fig


"""
    该函数是计算非刚性配准的客观评价指标
    PSNR:两幅图像之间的峰值信噪比度量 （值越大，图像的配准质量越好）
    SSIM:两幅图像之间的结构相似度（越接近1越好）
    CC:两幅图像之间的相关系数（越接近1越好）
    RV：两幅图像之间的残差图方差（越小，说明算法越稳定）
    RM：两幅图像之间的残差图均值（越小，说明算法越稳定）
"""

import numpy as np
import math
import cv2

def psnr(target, ref, border=0):
    # target: 目标图像   ref: 参考图像   scale: 尺寸大小  border:边缘大小（即不参与计算的部分）
    # assume RGB image
    if not target.shape == ref.shape:
        raise ValueError('Input images must have the same dimensions.')
    b, c, h, w = target.shape
    target = target.squeeze(0).cpu().numpy()
    ref = ref.squeeze(0).cpu().numpy()
    target = target[:, border:h-border, border:w-border]
    ref = ref[:, border:h-border, border:w-border]

    target = target.astype(np.float64)
    ref = ref.astype(np.float64)
    mse = np.mean((target - ref) ** 2)

    if mse == 0:
        return float('inf')
    return 20 * math.log10(255.0 / math.sqrt(mse))

def ssim(target, ref, border=0):

    def ssim_two(img1, img2):
        C1 = math.pow((0.01 * 255), 2)
        C2 = math.pow((0.03 * 255), 2)

        img1 = img1.astype(np.float64)
        img2 = img2.astype(np.float64)
        kernel = cv2.getGaussianKernel(11, 1.5)
        window = np.outer(kernel, kernel.transpose())

        mu1 = cv2.filter2D(img1, -1, window)[5:-5, 5:-5]  # valid
        mu2 = cv2.filter2D(img2, -1, window)[5:-5, 5:-5]  # valid
        mu1_sq = mu1 ** 2
        mu2_sq = mu2 ** 2
        mu1_mu2 = mu1 * mu2
        sigma1_sq = cv2.filter2D(img1 ** 2, -1, window)[5:-5, 5:-5] - mu1_sq
        sigma2_sq = cv2.filter2D(img2 ** 2, -1, window)[5:-5, 5:-5] - mu2_sq
        sigma12 = cv2.filter2D(img1 * img2, -1, window)[5:-5, 5:-5] - mu1_mu2

        ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) *
                                                                (sigma1_sq + sigma2_sq + C2))
        return ssim_map.mean()

    if not target.shape == ref.shape:
        raise ValueError('Input images must have the same dimensions.')
    b, c, h, w = target.shape
    target = target.squeeze(0).cpu().numpy()
    ref = ref.squeeze(0).cpu().numpy()
    target = target[:, border:h - border, border:w - border]
    ref = ref[:, border:h - border, border:w - border]
    if c == 1:
        return ssim_two(np.squeeze(target), np.squeeze(ref))
    elif c == 3:
        ssims = []
        for i in range(3):
            ssims.append(ssim_two(target[i], ref[i]))
        return np.array(ssims).mean()

def cc(target, ref, border=0):
    if not target.shape == ref.shape:
        raise ValueError('Input images must have the same dimensions.')
    b, c, h, w = target.shape
    target = target.squeeze(0).cpu().numpy()
    ref = ref.squeeze(0).cpu().numpy()
    target = target[:, border:h - border, border:w - border]
    ref = ref[:, border:h - border, border:w - border]
    target = target.astype(np.float64)
    ref = ref.astype(np.float64)
    target_E = target - np.mean(target)
    ref_E = ref - np.mean(ref)
    r = np.sum(target_E * ref_E) / np.sqrt(np.sum(target_E * target_E) * np.sum(ref_E * ref_E))
    return r

def rv_rm(target, ref, border=0):
    if not target.shape == ref.shape:
        raise ValueError('Input images must have the same dimensions.')
    b, c, h, w = target.shape
    target = target.squeeze(0).cpu().numpy()
    ref = ref.squeeze(0).cpu().numpy()
    target = target[:, border:h - border, border:w - border]
    ref = ref[:, border:h - border, border:w - border]
    target = target.astype(np.float64)
    ref = ref.astype(np.float64)
    # res计算target和ref之间的残差图
    res = np.abs(target - ref)
    rm = np.mean(res)
    rv = np.var(res)
    return rv, rm


import streamlit as st
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from skimage.metrics import peak_signal_noise_ratio, mean_squared_error, structural_similarity
import warnings
from tensorflow import keras
warnings.filterwarnings("ignore")

gen = keras.models.load_model('generator_100')

def inference_gan(gen):
    st.markdown(f"
DISTILLED LEARNING GAN
", unsafe_allow_html=True)
    x = st.slider("Adjust x", min_value=0, max_value=100, value=5)
    low_res_image_path = f"Data/LR/{x}.png"
    high_res_image_path = f"Data/HR/{x}.png"

    lr_img = Image.open(low_res_image_path)
    lr_img = np.array(lr_img.resize((96, 96))) / 255.0
    lr_img = np.expand_dims(lr_img, axis=0)
    hr_img = Image.open(high_res_image_path)
    hr_img = np.array(hr_img.resize((384, 384))) / 255.0
    gen_img = gen.predict(lr_img)
    gen_img = np.squeeze(gen_img, axis=0)
    gen_img = np.clip(gen_img, 0, 1)
    col1, col2 = st.columns(2)
    with col1:
        st.image(lr_img[0], caption="Low-Resolution (LR)", use_column_width=True)
    with col2:
        st.image(gen_img, caption="Generated High-Resolution (HR)", use_column_width=True)
    psnr = peak_signal_noise_ratio(hr_img, gen_img)
    mse = mean_squared_error(hr_img, gen_img)
    # ssim = structural_similarity(hr_img, gen_img, multichannel=True)
    st.markdown(f"
PSNR: {psnr}
", unsafe_allow_html=True)
    st.markdown(f"
MSE: {mse}
", unsafe_allow_html=True)
    # st.markdown(f"
SSIM: {ssim}
", unsafe_allow_html=True)

inference_gan(gen)


import random

Rock = '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

Paper = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

Scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''

game_images = [Rock, Paper, Scissors]

Player_Choice = int(input("What do you choose? Type 0 for Rock, Type 1 for Paper, Type 2 for Scissors\n"))
print(game_images[Player_Choice])

Computer_Choice = random.randint(0, 2)
print("Computer Chose:")
print((game_images[Computer_Choice]))

if Player_Choice >= 3 or Player_Choice < 0:
    print(("You have typed invalid choice, You Loose!"))
elif Player_Choice == 0 or Player_Choice == 2:
    print(("You Win!!!"))
elif Computer_Choice == 0 or Computer_Choice == 2:
    print(("You Loose!!!"))
elif Player_Choice > Computer_Choice:
    print(("You Win!!!"))
elif Computer_Choice == Player_Choice:
    print(("It's a Draw!!!"))


import requests as r
from pokemon import Pokemon
from team import Team
import json
import random

def create_pokemon():
    poke_id = random.randint(1, 100)
    raw_res = r.get("https://pokeapi.co/api/v2/pokemon/" + str(poke_id))
    json_res = raw_res.json()
    name, abilities, id = json_res["name"], json_res["abilities"], json_res["id"]
    pokemon = Pokemon(name, abilities, id)
    print('id: ', pokemon.get_id())
    print('name: ', pokemon.get_name())
    spot = 1
    for ability in pokemon.get_abilities():
        print(f"ability {spot}: {ability['ability']['name']}")
        spot += 1
    return pokemon

def create_team():
    new_team = Team("best team")
    for i in range(0, 6):
        new_pokemon = create_pokemon()
        new_team.add_pokemon(new_pokemon)
    print(json.dumps(new_team.get_team()))

create_team()


#K번째 최단경로 찾기(1854번)
#############################################
    # 문제: https://www.acmicpc.net/problem/1854
    # python3 14%시간초과, pypy3 4844ms 통과 → 코드 개선 필요
    # 다익스트라 알고리즘
    # 일반적인 다익스트라 알고리즘은 최단 거리 테이블에 이미 최단 경로가 기록되어 있다면 중복을 무시하지만, 이 문제의 경우 K번째 최단 거리를 찾아야하므로,
    # 중복을 포함해서 탐색을 해야한다.
    # 풀이의 핵심은 원래 우선순위 큐가 있고, 우선순위 큐로 구성된 2차원의 최단 거리 테이블을 선언하는 것이다.
    # 원래의 우선순위 큐는 최소힙으로 처리하고, 최단 거리 테이블의 우선순위 큐는 최대힙으로 처리한다.
    # 최단거리 배열의 우선 순위 큐의 길이가 k 이하면, 원본 우선 순위 큐에 해당 길이와 다음 위치를 넣고, 최단거리 배열의 우선순위 큐에는 해당 거리값을 최대힙으로 처리한다.
    # 만약 최단거리 배열의 우선 순위 큐 길이가 k 이면, 해당 큐에서 값을 뽑아 현재의 거리와 비교하여 현재 거리가 더 작으면 그 값을 최대 힙으로 푸시한다.
    # 최종적으로 만약 최단 거리 테이블 안의 배열 길이가 k 이하면 -1을 출력하고, k 이면 힙팝을 하여 출력한다.
#############################################
import sys,heapq
input=sys.stdin.readline
n,m,k=map(int,input().split())
distance=[[] for i in range(n+1)]
graph=[[] for i in range(n+1)]
for i in range(m):
    a,b,c=map(int,input().split())
    graph[a].append((b,c))
q=[]
heapq.heappush(q,(0,1)) # 우선 순위 큐에 (현재 거리, 현재 위치)를 push
heapq.heappush(distance[1],0) # 현재 위치의 최단거리 테이블에 0을 push (왜냐면 i에서 i로 가는 첫번째 최단 거리는 항상 0이므로)
len_dist=[0]*(n+1) # 최단거리 테이블 안의 배열의 길이를 기록하기 위한 배열
len_dist[1]=1 # 현재 1번 최단거리 테이블 안에 0을 넣었으니까 길이는 1임
while q:
    dist,cur=heapq.heappop(q)
    for nxt,cost in graph[cur]:
        d=dist+cost
        if len_dist[nxt]<k: # 다음 위치의 최단 거리 테이블 길이가 k 이하면,
            heapq.heappush(q,(d,nxt)) # 우선 순위 큐에 새로운 거리를 삽입(최소 힙)
            heapq.heappush(distance[nxt],-d) # 다음 위치의 최단 거리 테이블에 새로운 거리의 음수 값을 삽입(최대 힙)
            len_dist[nxt]+=1
        else: # 만약 다음 위치의 최단 거리 테이블의 길이가 k 이면,
            tmp_d=-heapq.heappop(distance[nxt]) #일단, 최댓값을 뽑고
            if d<tmp_d: # 새로운 거리와 뽑은 값을 비교하여 새로운 거리가 더 작으면 그 값을 우선 순위 큐에 삽입한다.
                heapq.heappush(distance[nxt],-d)
                heapq.heappush(q,(d,nxt))
            else:
                heapq.heappush(distance[nxt],-tmp_d) # 아니라면 다시 삽입
for i in range(1,n+1):
    if len_dist[i]<k:
        print(-1)
    else:
        print(-heapq.heappop(distance[i])) # i번째 최단거리 테이블의 길이는 무조건 k이므로 최댓값을 뽑으면 그게 k번째 최단 경로인 셈이다.


from distutils.core import setup, Extension

description = """A C extension for physbr

Usage:
import physbr
distance = physbr.getdistance((x,y),(x2,y2))
"""

setup (name = 'physbr',
       version = '0.0.1',
       description = 'Physbr C Extensions',
       long_description = description,
        author = "Nebual",
        author_email = "nebual@nebtown.info",
       ext_modules = [Extension('physbr', sources = ['physbr.c'])])


#!/usr/bin/env python

#This node drives the robot based on information from the other nodes.

import rospy
from geometry_msgs.msg import Twist
from std_msgs.msg import Int16, Float32

LINEAR_SPEED = 0.2
ANGULAR_SPEED = 0.3

#Makes the twist object sent from PID global
def cb_twist(msg):
    global t_pid
    t_pid = msg

#Init node
rospy.init_node('driver')

#Make publisher for cmd_vel
pub_vel = rospy.Publisher('cmd_vel', Twist, queue_size = 1)

#Make all subscribers
sub_pid_twist = rospy.Subscriber('pid_twist', Twist, cb_twist)

#Rate object
rate = rospy.Rate(10)

#Create two twist variable, one is modified here, one is copied from the PID messages
t_pub = Twist()
t_pid = Twist()

should_follow_pid = False

while not rospy.is_shutdown():
    if (should_follow_pid):
        t_pub = t_pid
    else:
        t_pub.linear.x = LINEAR_SPEED
        t_pub.angular.z = 0
    pub_vel.publish(t_pub)
    rate.sleep()


from typing import List
class Solution:
    def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:
        """
        Do not return anything, modify nums1 in-place instead.
        """
        cur = m + n -1
        cur_1 = m - 1
        cur_2 = n - 1
        while cur >= 0:
            if cur_1 < 0:
                nums1[cur] = nums2[cur_2]
                cur_2 -= 1
            elif cur_2 < 0:
                nums1[cur] = nums1[cur_1]
                cur_1 -= 1
            elif nums1[cur_1] > nums2[cur_2]:
                nums1[cur] = nums1[cur_1]
                cur_1 -= 1
            else:
                nums1[cur] = nums2[cur_2]
                cur_2 -= 1
            cur -= 1

s = Solution()
nums1 = [1,2,3,0,0,0]
m = 3
nums2 = [2,5,6]
n = 3
s.merge(nums1, m, nums2, n)

88_合并两个有序数组.py


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

import datetime
import tables
import tstables
import os
import pandas as pd
from _utility_v2 import *

if __name__ == '__main__':

    account_type = 'live'
    symbol = 'EUR_USD'
    granularity = '1H'
    filename = '{}_{}.hdf'.format(symbol, granularity)
    df = read_hdf_file(filename)

    # Filter only bid prices
    df = df[['bid_o', 'bid_h', 'bid_l', 'bid_c', 'volume']]

    # Change column names for better readibility
    df.columns = ['open', 'high', 'low', 'close', 'volume']

    # Create columns for model
    df_reg = pd.DataFrame()
    df_reg['returns'] = np.log(df['close'] / df['close'].shift(1))
    df_reg.dropna(inplace=True)

    # Add lagged returns as predictors
    lags = 5

    cols = []
    for lag in range(1, lags + 1):
        col = 'lag_{}'.format(lag)
        df_reg[col] = df_reg['returns'].shift(lag)
        cols.append(col)

    df_reg.dropna(inplace=True)

    df_reg.describe()

    #plot_pairwise_distributions(df_reg)
    #plot_pairwise_correlation(df_reg)

    # Assign data to X and y from dataframe
    X = df_reg[cols]
    y = df_reg['returns']

    # Ordinary Linear Regression with polynomial terms added
    poly_reg = PolynomialFeatures(degree=5)
    X_poly = poly_reg.fit_transform(X)

    model = LinearRegression()
    model.fit(X_poly, y)
    print( 'Intercept: {}    Coefficients: {}'.format(model.intercept_, model.coef_) )
    df_reg['returns_pred'] = model.predict(X_poly)

    # Joint plot
    sns.jointplot(x=X['lag_1'], y=y, kind='reg', height=10)
    plt.show()

    ax1 = sns.distplot(y, hist=False, color="r", label="Actual Value")
    sns.distplot(df_reg['returns_pred'], hist=False, color="b", label="Fitted Values" , ax=ax1)
    plt.show()

    df_reg[['returns','returns_pred']].plot()

    # Accuracy test:
    df_reg['sign_returns'] = np.sign(df_reg['returns'])
    df_reg['sign_returns_pred'] = np.sign(df_reg['returns_pred'])
    df_reg['hit'] = df_reg['sign_returns'] == df_reg['sign_returns_pred']
    print( df_reg['hit'].value_counts(normalize=True) )


# Reading the input file:

f = open('input.txt', 'r')
input_ml = f.read()

example = """1000
2000
3000

4000

5000
6000

7000
8000
9000

10000"""

# data = example.splitlines()
data = input_ml.splitlines()

# Creating dictionary with calories for each elf:

lista = {0: 0}
elf = 0

for i, x in enumerate(data):
    if x == '':
        elf += 1 # new elf
        lista[elf] = 0
    else:
        lista[elf] = lista[elf]+int(x) # adding the calories for each elf

top1, top2, top3 = 0,0,0

# Finding the top 3 elves carrying the most calories

for chave,valor in lista.items():
    if valor > top1:
        top3 = top2
        top2 = top1
        top1 = valor
    elif valor >= top2:
        top3 = top2
        top2 = valor
    elif valor >= top3:
        top3 = valor

sum = top1 + top2 + top3

print(f'Solution part 1: {top1}')
print(f'Solution part 1: {sum}')