from functools import partial
import copy

from Tkinter import *
from ttk import Notebook
from tkFileDialog import *
from tkMessageBox import showerror, showwarning, showinfo

from metascripts import *
from regiscripts import *
from mergscripts import *
from tomosaic.merge.merge import _get_algorithm_kwargs

def mergtab_ui(ui):

    formMerg = Frame(ui.tabMerg)
    bottMerg = Frame(ui.tabMerg)

    # source line

    frameMergSrc = Frame(formMerg)
    labMergSrc = Label(frameMergSrc, text='Source folder:')
    labMergSrc.pack(side=LEFT)
    ui.entMergSrc = Entry(frameMergSrc)
    ui.entMergSrc.pack(side=LEFT, fill=X, expand=True)
    buttMergSrcBrowse = Button(frameMergSrc, text='Browse...', command=partial(getMergSrcFolder, ui))
    buttMergSrcBrowse.pack(side=LEFT)
    buttMergSrcDefault = Button(frameMergSrc, text='Same as raw folder', command=partial(getRawFolder, ui))
    buttMergSrcDefault.pack(side=LEFT)

    # dest line

    frameMergDest = Frame(formMerg)
    labMergDest = Label(frameMergDest, text='Destination filename:')
    labMergDest.pack(side=LEFT)
    ui.entMergDest = Entry(frameMergDest)
    ui.entMergDest.pack(side=LEFT, fill=X, expand=True)
    buttMergDestBrowse = Button(frameMergDest, text='Browse...', command=partial(getMergDestFile, ui))
    buttMergDestBrowse.pack(side=LEFT)

    # method line

    frameMergMethod = Frame(formMerg)
    labMergMeth1 = Label(frameMergMethod, text='Blending method:')
    labMergMeth1.pack(side=LEFT)
    ui.varMergMeth1 = StringVar()
    ui.varMergMeth1.set('Max')
    lsMergMeth1 = ('overlay', 'max', 'min', 'alpha', 'pyramid', 'poisson')
    ui.optMergMeth1 = OptionMenu(frameMergMethod, ui.varMergMeth1, command=partial(updateOpt, ui, 0), *lsMergMeth1)
    ui.optMergMeth1.pack(side=LEFT, fill=X, expand=True)
    labMergMeth2 = Label(frameMergMethod, text=' Blending method (Y, opt):')
    labMergMeth2.pack(side=LEFT)
    ui.varMergMeth2 = StringVar()
    ui.varMergMeth2.set('Same as X')
    lsMergMeth2 = ('Same as X', 'overlay', 'max', 'min', 'alpha', 'pyramid', 'poisson')
    ui.optMergMeth2 = OptionMenu(frameMergMethod, ui.varMergMeth2, command=partial(updateOpt, ui, 1), *lsMergMeth2)
    ui.optMergMeth2.pack(side=LEFT, fill=X, expand=True)

    # option 1 line

    ui.lstAlpha = [None, None]
    ui.lstDepth = [None, None]
    ui.lstBlur = [None, None]
    ui.lstOrder = [None, None]
    ui.frameMergOpt1 = Frame(formMerg)
    labMergOpt1 = Label(ui.frameMergOpt1, text='Options: ')
    labMergOpt1.pack(side=LEFT)
    ui.frameOpt1Inp = Frame(ui.frameMergOpt1)
    labOpt1Default = Label(ui.frameOpt1Inp, text='Select a method.')
    labOpt1Default.pack(side=LEFT)

    # option 2 line

    ui.frameMergOpt2 = Frame(formMerg)
    labMergOpt2 = Label(ui.frameMergOpt2, text='Options: ')
    labMergOpt2.pack(side=LEFT)
    ui.frameOpt2Inp = Frame(ui.frameMergOpt2)
    labOpt2Default = Label(ui.frameOpt2Inp, text='Select a method.')
    labOpt2Default.pack(side=LEFT)

    # mpi line

    frameMergMPI = Frame(formMerg)
    labMergMPI = Label(frameMergMPI, text='Use MPI:')
    labMergMPI.pack(side=LEFT)
    radMPIY = Radiobutton(frameMergMPI, variable=ui.ifmpi, text='Yes', value=True)
    radMPIY.pack(side=LEFT)
    radMPIN = Radiobutton(frameMergMPI, variable=ui.ifmpi, text='No', value=False)
    radMPIN.pack(side=LEFT, padx=10)
    labMergNCore = Label(frameMergMPI, text='Number of processes to initiate:')
    labMergNCore.pack(side=LEFT)
    ui.entMergNCore = Entry(frameMergMPI)
    ui.entMergNCore.insert(0, '5')
    ui.entMergNCore.pack(side=LEFT, fill=X, expand=True)

    # out line

    frameMergOut = Frame(formMerg, height=245)
    frameMergOut.pack_propagate(False)
    ui.boxMergOut = Text(frameMergOut)
    ui.boxMergOut.insert(END, 'Merging\n')
    ui.boxMergOut.insert(END, 'Refer to initial terminal window for intermediate output.\n--------------\n')
    ui.boxMergOut.pack(side=LEFT, fill=BOTH, expand=YES)

    # button line

    buttMergLaunch = Button(bottMerg, text='Launch', command=partial(launchMerging, ui))
    buttMergLaunch.grid(row=0, column=0, sticky=W+E)
    buttMergConfirm = Button(bottMerg, text='Confirm parameters', command=partial(readMergPars, ui))
    buttMergConfirm.grid(row=0, column=1, sticky=W+E)

    ui.frameOpt1Inp.pack(side=LEFT, fill=X)
    ui.frameOpt2Inp.pack(side=LEFT, fill=X)
    frameMergSrc.pack(fill=X)
    frameMergDest.pack(fill=X)
    frameMergMethod.pack(fill=X)
    ui.frameMergOpt1.pack(fill=X)
    ui.frameMergOpt2.pack(fill=X)
    frameMergMPI.pack(fill=X)
    frameMergOut.pack(fill=X)
    formMerg.pack(fill=X)
    bottMerg.pack(side=BOTTOM)

def updateOpt(ui, uid, meth):

    if uid == 0:
        field = ui.frameOpt1Inp
    elif uid == 1:
        field = ui.frameOpt2Inp

    for w in field.winfo_children():
        w.destroy()

    default_opts = _get_algorithm_kwargs()

    if meth in ('max', 'min', 'poisson'):
        lab0 = Label(field, text='No options available for the selected method.')
        lab0.pack(side=LEFT)
    elif meth == 'overlay':
        lab0 = Label(field, text='Image on the top (1 or 2): ')
        lab0.pack(side=LEFT)
        ui.lstOrder[uid] = Entry(field)
        ui.lstOrder[uid].insert(0, '2')
        ui.lstOrder[uid].pack(side=LEFT, fill=X)
    elif meth == 'alpha':
        lab0 = Label(field, text='Alpha: ')
        lab0.pack(side=LEFT)
        ui.lstAlpha[uid] = Entry(field)
        ui.lstAlpha[uid].insert(0, default_opts['alpha'])
        ui.lstAlpha[uid].pack(side=LEFT, fill=X)
    elif meth == 'pyramid':
        lab0 = Label(field, text='Depth: ')
        lab0.pack(side=LEFT)
        ui.lstDepth[uid] = Entry(field)
        ui.lstDepth[uid].insert(0, default_opts['depth'])
        ui.lstDepth[uid].pack(side=LEFT)
        lab1 = Label(field, text=' Blur: ')
        lab1.pack(side=LEFT)
        ui.lstBlur[uid] = Entry(field)
        ui.lstBlur[uid].insert(0, default_opts['blur'])
        ui.lstBlur[uid].pack(side=LEFT)
    else:
        lab0 = Label(field, text='No options available for the selected method.')
        lab0.pack(side=LEFT)

def getMergSrcFolder(ui):

    src = askdirectory()
    ui.entMergSrc.insert(0, src)

def getRawFolder(ui):

    try:
        ui.entMergSrc.insert(0, ui.raw_folder)
    except:
        showerror(message='Raw folder must be specified in metadata tab.')

def getMergDestFile(ui):

    dest = asksaveasfilename()
    ui.entMergDest.insert(0, dest)

def launchMerging(ui):

    readMergPars(ui)
    merge_mpi(ui)

def buildMergOpts(ui, meth, dict, uid):

    if meth == 'overlay':
        dict['order'] = 1 if ui.lstOrder[uid].get() == 2 else 2
    elif meth == 'alpha':
        dict['alpha'] = float(ui.lstAlpha[uid].get())
    elif meth == 'pyramid':
        dict['depth'] = int(ui.lstDepth[uid].get())
        dict['blur'] = float(ui.lstBlur[uid].get())

def readMergPars(ui):

    ui.merge_src = ui.entMergSrc.get()
    ui.merge_dest_fname = os.path.basename(ui.entMergDest.get())
    ui.merge_dest_folder = ui.entMergDest.get().split(ui.merge_dest_fname)[0]
    ui.merge_meth1 = ui.varMergMeth1.get()
    ui.merge_meth2 = ui.varMergMeth2.get()
    buildMergOpts(ui, ui.merge_meth1, ui.merge_opts1, 0)
    buildMergOpts(ui, ui.merge_meth2, ui.merge_opts2, 1)
    ui.merge_mpi_ncore = int(ui.entMergNCore.get())
    ui.boxMergOut.insert(END, 'Parameters read.\n')


#!/usr/bin/env python
import socket

TCP_IP = "192.168.1.104"
TCP_PORT = 5000
BUFFER_SIZE = 1024  # Normally 1024, but we want fast response

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Se crea un socket indicando en el constructor:
# -socket.AF_INET que permite la comunicación utilizando protocolos de Internet como TCP o UDP
# -socket.SOCK_STREAM es una de las clasificación de socket, se utiliza si el servicio desea usar TCP,
# de lo contrario se utilizaría socket.SOCK_DGRAM para UDP

server.bind((TCP_IP, TCP_PORT))
server.listen(1)
socket_c, (host_c, addr_c) = server.accept()
i=0

while (1):
  if(i==0):
    ip=socket_c.recv(BUFFER_SIZE)
    print("IP del cliente es: "+ip.decode())
    nombre=socket_c.recv(BUFFER_SIZE)
    print("Nombre del cliente es: "+nombre.decode())
    i+=1
  print ("Enviar un mensaje al cliente: ")
  mensaje=input()
  socket_c.send(mensaje.encode())
  print ("Esperando respuesta...\n")
  respuesta= socket_c.recv(BUFFER_SIZE)
  if (respuesta.decode()=='over'):
    break
  print ("Cliente: "+ respuesta.decode())
print ("\n...")
print ("\n  Desconectando...")
socket_c.close()
server.close()


# https://www.hackerearth.com/ru/practice/basic-programming/bit-manipulation/basics-of-bit-manipulation/practice-problems/algorithm/samu-and-her-birthday-party-1/

def solution():
    T = int(input())

    for _ in range(T):
        friends = []
        N, K = map(int, input().strip().split())
        for _ in range(N):
            friends.append(int(input(), 2))

        min_dishes = K
        for i in range(1, 1 << K):
            all_friends_fed = True
            for friend in friends:
                if i & friend == 0:
                    all_friends_fed = False
                    break
            if all_friends_fed:
                min_dishes = min(min_dishes, bin(i).count('1'))

        print(min_dishes)

solution()

02-BIT MANIPULATION/04 - hackerearth - Samu and her Birthday Party.py


from ml_utils.logging.tb_log import Logger
from typing import Union, List
from datetime import datetime
import shutil
import os

def create_directories(name: Union[List[str], str]) -> None:
    """
    Creates one or multiple directories. If a string value is provided, then the single directory is created. If a list
    is provided, then all directory names within the list are created.

    Parameters
    ----------
    name: A list, containing the names of directories, which should be created.
    """

    if type(name) == 'str':
        name = [name]

    for directory in name:
        if not os.path.exists(directory):
            os.makedirs(directory)

def copy_files(directory: str, files: Union[str, List[str]]) -> None:
    """
    Copies provided files into a provided directory.

    Parameters
    ----------
    directory: Path to the directory, in which the files should be stored.
    files: Files which should be copied into the provided directory.
    """

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

    if type(files) == 'str':
        files = [files]

    for source_file in files:
        shutil.copy(source_file, directory)

def create_date_time_string() -> str:
    """
    Creates a string consisting of YYYY_MM_DD__hh_mm_ss, where Y represents numbers of the current year, M represents
    numbers of the current month, D represents numbers of the current day, h represents numbers of the current hour, m
    represents numbers of the current minute and s represents numbers of the current seconds.

    Returns
    -------
    now: Generated string from the current date and time.
    """

    now = str(datetime.now()).replace('-', '_').replace(':', '_').replace(' ', '__')[0:20]
    return now

def init_experiment_dir(dir_root, tb_log: bool = False, tb_log_names: Union[None, list] = None) -> tuple:
    """
    Creates a new directory, in which results and logs of a new experiment can be stored.

    Parameters
    ----------
    dir_root: Path to a directory, in which the root directory for a new experiment is created.
    tb_log: Variable, which defines, whether to use Tensorboard or not. If the value of this variable is None, then the
            experiment is initialized without tensorboard. If the value of this variable is an integer, than this number
            of tb_logger instances is created.
    tb_log_names: Names of the directories of the Tensorboard logger (if the names are not None). Each directory is
                  created in the as sub-directory of the tb directory in the experience root path.

    Returns
    -------
    dir_name: Path to the experiment directory
    tb_logger_tuple: Tuple containing the tensorboard logger instances.
    """

    # get the name of the root directory
    dir_name = dir_root + create_date_time_string() + '/'
    source_file_dir = dir_name + 'src/'
    dir_list = [dir_name, source_file_dir]
    output = tuple(dir_name)

    # if we want to include tensorboard logging
    if tb_log:

        # get the name of the tensorboard directory
        tb_root_dir = dir_name + 'tb/'
        dir_list.append(tb_root_dir)

        # if multiple logging instances should be initialized, get the name of their directories
        if tb_log_names is not None:
            tb_logger_list = []
            for name in tb_log_names:

                # compute the directory of the logger instance
                logger_dir = tb_root_dir + name + '/'

                # create a logger instance and store the instance as well as the corresponding directory
                tb_logger_list.append(Logger(logger_dir))
                dir_list.append(logger_dir)
                output = (dir_name, tb_logger_list)

    # create the directories and return the root directory of the new created experiment
    create_directories(dir_list)
    return output


"""
https://www.youtube.com/watch?v=PjhlUzp_cU0&t=960s

나도코딩
파이썬 코딩 무료 강의 (활용편5) - 데이터 분석 및 시각화, 이 영상 하나로 끝내세요

2022-07-29 03:05:23 ~ 03:11:24
"""

import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family'] = 'AppleGothic'
matplotlib.rcParams['font.size'] = 15
matplotlib.rcParams['axes.unicode_minus'] = False

x = [1, 2, 3]
y = [2, 4, 8]

'''
2. 축
'''

# title

plt.plot(x, y)
plt.title('꺾은선 그래프', fontdict={'family': 'Maplestory OTF', 'size': 20})
# 기본 폰트는 애플고딕이지만, 개별적으로 설정도 가능

# label

plt.plot(x, y)
plt.xlabel('X축', color='red', loc='right')
# x축 loc : left, center, right
plt.ylabel('Y축', color='#00aa00', loc='top')
# y축 loc : top, center, bottom

# tick

plt.plot(x, y)
plt.xticks([1, 2, 3])
plt.yticks([3, 6, 9, 12])
plt.show()


import bpy
from math import degrees

def exportAnimationToCSV():
    sce = bpy.context.scene
    ob = bpy.context.active_object
    if ob.animation_data is not None:
        if ob.animation_data.action is not None:
            actionName = ob.animation_data.action.name
            fileName = f"{actionName}.csv" # Will name the file as the action name
            currentFrame = sce.frame_current
            file=open(fileName,'w')
            for frame in range(sce.frame_start,sce.frame_end+1):
                sce.frame_set(frame)
                base=ob.pose.bones["Base"].rotation_euler.y
                shoulder=ob.pose.bones["Shoulder"].rotation_euler.z
                elbow=ob.pose.bones["Elbow"].rotation_euler.z
                wrist=ob.pose.bones["Wrist vertical"].rotation_euler.z
                toArmRot = lambda angle:int((degrees(angle)+90)%360)
                file.write(f"{toArmRot(base)};{toArmRot(shoulder)};{toArmRot(elbow)};{toArmRot(wrist)}\n")
            file.close()
            sce.frame_set(currentFrame)

if __name__ == "__main__":
    exportAnimationToCSV()


a = 8
type(a)

f = 1.23

from decimal import *
getcontext().prec = 30
Decimal(10) / Decimal(3)
Decimal(10) / Decimal(3)
type(Decimal(5))

from fractions import *
Fraction(1, 4)       # Tạo phân số

complex(3, 1)     #tạo số phức
c = 2 +1j
c.real  # lấy phần thực
c.imag #lấy phần ảo
complex(2)
type(3 +1j)    #in ra kiểu dữ liệu

fac1 = Fraction(1, 2)
fac2 = Fraction(2, 3)
fac3 = fac1 + fac2

fac4 = fac3 ** fac1

import math   #lấy nội dung của thư viện math
math.trunc(4.5)
math.floor(4.3)
print(math.ceil(4.3))
print(math.fabs(-4.5))
math.sqrt(4)
print(math.gcd(3, 7))


def lo_que_sea():
    return "Lo que sea"

print(lo_que_sea())

#Return en funciones
def sumar(a,b):
    return a + b
print(f"La respuesta de la suma es: {sumar(5,6)}")

def multiplicar(a, b):
    return a*b
print(f"La respuesta de la multiplicación es: {multiplicar(5,6)}")

def maximo(a,b):
    if a > b:
        return a
    else:
        return b
print(f"El mayor es: {maximo(5,6)}")

def minimo(a,b):
    if a < b:
        return a
    else:
        return b
print(f"El menor es: {minimo(5,6)}")

#Invertir lista
lista = [1,2,3,4,5,6,7,8,9]
def invertir_lista(lista):
    lista_invertida = []
    for x in lista[::-1]:
        lista_invertida.append(x)
    return lista_invertida
print(f"Esta esa la lista original {lista}")
print(f"Esta es la lista invertida {invertir_lista(lista)}")

#Invertir Diccionario
diccionario = {
    "1": "Nombre",
    "2": "Edad",
    "3": "Ciudad"}

def invertir_dic(diccionario):
    dic_invertido = {}
    for clave, valor in diccionario.items():
        dic_invertido[valor] = clave
    return dic_invertido

print(f"Diccionario ral es {diccionario}")
print(f"El diccionario invertido es {invertir_dic(diccionario)}")

def sumar_arg(*args):
    suma = 0
    for y in args:
        suma += y
    return suma

resultado = sumar_arg(2,5,3)
print(f"El resultado de la suma es: {resultado}")


sentence = input("Enter a sentence: ")
dictionary = dict()

print("\nNumber of times each character appears in sentence:\n")

for character in sentence:
    occurences = sentence.count(character)
    dictionary[character] = occurences

print(dictionary)


# coding: utf-8

import Tkinter as Tk
import os
import tkFileDialog as Ofd

import yellow

class YellowGUI:
    def __init__(self):
        self.yellow = 0
        self.txtPath = 0
        self.build()

    def build(self):
        fen = Tk.Tk()
        fen.title('MonProgramme')
        self.txtPath = Tk.StringVar()
        fen.resizable(0, 0)
        Tk.Label(fen, text='Source').grid(row=1, column=1, sticky='EW')
        Tk.Entry(fen, textvar=self.txtPath).grid(row=1, column=2, sticky='EW')
        Tk.Button(fen, text='Open', command=self.open).grid(row=1, column=3, sticky='EW')
        Tk.Button(fen, text='Read', command=self.read).grid(row=2, column=3, sticky='EW')
        Tk.Button(fen, text='Display', command=self.disp).grid(row=3, column=3, sticky='EW')
        fen.mainloop()

    def open(self):
        dirpath = Ofd.askdirectory(title='Open model', initialdir=os.getcwd())
        if dirpath:
            self.txtPath.set(dirpath)
            self.yellow = yellow.Yellow(dirpath)

    def read(self):
        if isinstance(self.yellow, yellow.Yellow):
            self.yellow.lire()
        else:
            print('choose srcDir')

    def disp(self):
        if isinstance(self.yellow, yellow.Yellow):
            self.yellow.afficher()
        else:
            print('choose srcDir')

if __name__ == '__main__':
    YellowGUI()


import tensorflow as tf
from tensorflow.contrib.data import Dataset
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import matplotlib.pyplot as plt

mnist: Dataset = input_data.read_data_sets('data/MNIST')

num_steps: int = 20000
batch_size: int = 32
input_features: int = 28 * 28
gen_hidden_features: int = 256
disc_hidden_features: int = 256
noise_features: int = 200

def generator(x, reuse=False):
    """
    x : noise
    :rtype: image
    """
    with tf.variable_scope('Generator', reuse=reuse):
        x = tf.layers.dense(x, units=6 * 6 * 128)
        x = tf.nn.tanh(x)
        x = tf.reshape(x, shape=[-1, 6, 6, 128])

        # [None,14,14,64]
        x = tf.layers.conv2d_transpose(x, 64, 4, strides=2)

        # [None,28,28,1]
        x = tf.layers.conv2d_transpose(x, 1, 2, strides=2)

        x = tf.nn.sigmoid(x)

    return x

def discriminator(x, reuse=False):
    with tf.variable_scope('Discriminator', reuse=reuse):
        x = tf.layers.conv2d(x, 64, 4)
        x = tf.nn.tanh(x)
        x = tf.layers.max_pooling2d(x, 2, 2)
        x = tf.layers.conv2d(x, 128, 4)
        x = tf.nn.tanh(x)
        x = tf.layers.max_pooling2d(x, 2, 2)
        x = tf.contrib.layers.flatten(x)
        x = tf.layers.dense(x, 1024)
        x = tf.nn.tanh(x)
        x = tf.layers.dense(x, 2)
    return x

noise_input = tf.placeholder(tf.float32, [None, noise_features])
real_image_input = tf.placeholder(tf.float32, [None, 28, 28, 1])

gen_sample = generator(noise_input)

disc_real = discriminator(real_image_input)
disc_fake = discriminator(gen_sample, reuse=True)
disc_contat = tf.concat([disc_real, disc_fake], axis=0)

stacked_gen = discriminator(gen_sample, reuse=True)

disc_target = tf.placeholder(tf.int32, shape=[None])
gen_target = tf.placeholder(tf.int32, shape=[None])

disc_loss = tf.reduce_mean(
    tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=disc_contat, labels=disc_target
    )
)

gen_loss = tf.reduce_mean(
    tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=stacked_gen, labels=gen_target
    )
)

optimizer_gen = tf.train.AdamOptimizer(learning_rate=0.001)
optimizer_disc = tf.train.AdamOptimizer(learning_rate=0.001)

gen_vars = tf.get_collection(
    tf.GraphKeys.TRAINABLE_VARIABLES, scope='Generator'
)

disc_vars = tf.get_collection(
    tf.GraphKeys.TRAINABLE_VARIABLES, scope='Discriminator'
)

train_gen_op = optimizer_gen.minimize(gen_loss, var_list=gen_vars)
train_disc_op = optimizer_disc.minimize(disc_loss, var_list=disc_vars)

with tf.Session() as session:
    session.run(tf.global_variables_initializer())

    for i in range(1, num_steps + 1):
        batch_x, _ = mnist.train.next_batch(batch_size)
        batch_x = np.reshape(batch_x, newshape=[-1, 28, 28, 1])

        z = np.random.uniform(-1.0, 1.0, size=[batch_size, noise_features])

        # Prepare Targets (Real image: 1, Fake image: 0)
        # The first half of data fed to the generator are real images,
        # the other half are fake images (coming from the generator).
        batch_disc_y = np.concatenate(
            [np.ones([batch_size]), np.zeros([batch_size])], axis=0
        )

        # Generator tries to fool the discriminator, thus targets are 1.
        batch_gen_y = np.ones([batch_size])

        feed_dict: dict = {
            real_image_input: batch_x,
            noise_input: z,
            disc_target: batch_disc_y,
            gen_target: batch_gen_y
        }

        session.run([train_gen_op, train_disc_op], feed_dict=feed_dict)

        if i % 100 == 0 or i == 1:
            _, _, gl, dl = session.run([train_gen_op, train_disc_op, gen_loss, disc_loss], feed_dict=feed_dict)
            print('Step: \t{0}\t Generator loss: \t{1}\t Discriminator loss: \t{2}'.format(str(i), str(gl), str(dl)))

    # Generate images from noise, using the generator network.
    f, a = plt.subplots(4, 10, figsize=(10, 4))
    for i in range(10):
        # Noise input.
        z = np.random.uniform(-1., 1., size=[4, noise_features])
        g = session.run(gen_sample, feed_dict={noise_input: z})
        for j in range(4):
            # Generate image from noise. Extend to 3 channels for matplot figure.
            img = np.reshape(np.repeat(g[j][:, :, np.newaxis], 3, axis=2),
                             newshape=(28, 28, 3))
            a[j][i].imshow(img)

    f.show()
    plt.draw()
    plt.waitforbuttonpress()


from datetime import date

nome = 'Pedro Lisboa'
idade = 28
altura = 1.90
peso = 130
ano_atual = date.today().year
ano_nascimento = ano_atual - idade
imc = (peso / altura ** 2)

print(f'{nome} nasceu em {ano_nascimento} portanto tem {idade} anos')
print(f'Seu peso e de {peso}kg e sua altura {altura} m seu imc = {imc:.2f}')
print('Teste : é è ç ^ ã ê ')


import sys
import os

sys.path.append(os.getcwd())

from models.resunet_joint_training_other.modules import *
import torch.utils
import torch.utils.data
import torch.nn.functional as F
from utils.f_helper import FDomainHelper
from torchlibrosa.stft import magphase
import numpy as np
import pytorch_lightning as pl
from torchlibrosa import STFT
import time
from utils.file_io import *
from models.config import Config
from utils.overlapadd import LambdaOverlapAdd

class L1(nn.Module):
    def __init__(self):
        super(L1, self).__init__()
        self.loss = torch.nn.L1Loss()

    def __call__(self, output, target):
        return self.loss(output,target)

class BN_GRU(torch.nn.Module):
    def __init__(self,input_dim,hidden_dim,layer=1, bidirectional=False, batchnorm=True, dropout=0.0):
        super(BN_GRU, self).__init__()
        self.batchnorm = batchnorm
        if(batchnorm):self.bn = nn.BatchNorm2d(1)
        self.gru = torch.nn.GRU(input_size=input_dim,
                hidden_size=hidden_dim,
                num_layers=layer,
                bidirectional=bidirectional,
                dropout=dropout,
                batch_first=True)
        self.init_weights()

    def init_weights(self):
        for m in self.modules():
            if type(m) in [nn.GRU, nn.LSTM, nn.RNN]:
                for name, param in m.named_parameters():
                    if 'weight_ih' in name:
                        torch.nn.init.xavier_uniform_(param.data)
                    elif 'weight_hh' in name:
                        torch.nn.init.orthogonal_(param.data)
                    elif 'bias' in name:
                        param.data.fill_(0)

    def forward(self,inputs):
        # (batch, 1, seq, feature)
        if(inputs.size()[1] != 1):
            inputs = inputs[:,0:1,...]
        if(self.batchnorm):inputs = self.bn(inputs)
        out,_ = self.gru(inputs.squeeze(1))
        return out.unsqueeze(1)

class L1_Wav_L1_Sp(nn.Module):
    def __init__(self):
        super(L1_Wav_L1_Sp, self).__init__()
        self.f_helper = FDomainHelper()
        self.window_size = 2048
        hop_size = 441
        center = True
        pad_mode = "reflect"
        window = "hann"

        self.l1 = L1()
        self.stft = STFT(
            n_fft=self.window_size,
            hop_length=hop_size,
            win_length=self.window_size,
            window=window,
            center=center,
            pad_mode=pad_mode,
            freeze_parameters=True,
        )

    def __call__(self, output, target, alpha_t=0.85):
        wav_loss = self.l1(output, target)

        sp_loss = self.l1(
            self.f_helper.wav_to_spectrogram(output, eps=1e-8),
            self.f_helper.wav_to_spectrogram(target, eps=1e-8)
        )

        sp_loss /= math.sqrt(self.window_size)

        return alpha_t*wav_loss + (1-alpha_t)*sp_loss

class UNetResComplex_100Mb(pl.LightningModule):
    def __init__(self, channels, stem="", nsrc=1,subband=4, use_lsd_loss=False,
                 lr=0.002, gamma=0.9,
                 batchsize=None, frame_length=None,
                 sample_rate=None,
                 warm_up_steps=1000, reduce_lr_steps=15000,
                 # datas
                 check_val_every_n_epoch=5,  # inside a validation set, how many samples gonna saved
                 ):
        # sub4 52.041G 66.272M
        super(UNetResComplex_100Mb, self).__init__()
        window_size = 2048
        hop_size = 441
        center = True
        pad_mode = 'reflect'
        window = 'hann'
        activation = 'relu'
        momentum = 0.01
        freeze_parameters = True
        self.hop_size = 441
        self.center = True
        self.pad_mode = 'reflect'
        self.stem = stem
        self.window_size = 2048
        self.window = 'hann'
        self.activation = 'relu'
        self.momentum = 0.01
        self.freeze_parameters = True
        self.use_lsd_loss = use_lsd_loss
        self.save_hyperparameters()
        self.nsrc = nsrc
        self.channels = channels
        self.lr = lr
        self.gamma = gamma
        self.subband = subband
        self.sample_rate = sample_rate
        self.batchsize = batchsize
        self.frame_length = frame_length

        if(stem == "all"): # training mode
            self.l1loss = L1_Wav_L1_Sp()

        # self.lsd_loss = get_loss_function("lsd")
        self.train_step = 0
        self.val_step = 0
        self.check_val_every_n_epoch = check_val_every_n_epoch
        self.val_result_save_dir = None
        self.val_result_save_dir_step = None
        self.downsample_ratio = 2 ** 6  # This number equals 2^{#encoder_blcoks}
        self.f_helper = FDomainHelper(
            window_size=window_size,
            hop_size=hop_size,
            center=center,
            pad_mode=pad_mode,
            window=window,
            freeze_parameters=freeze_parameters,
            subband=self.subband,
            root=Config.ROOT
        )

        if (subband == 8):
            self.bn0 = nn.BatchNorm2d(129, momentum=momentum)
        elif (subband == 4):
            self.bn0 = nn.BatchNorm2d(257, momentum=momentum)
        elif (subband == 2):
            self.bn0 = nn.BatchNorm2d(513, momentum=momentum)
        else:
            self.bn0 = nn.BatchNorm2d(1025, momentum=momentum)

        self.encoder_block1 = EncoderBlockRes4(in_channels=channels * nsrc * subband, out_channels=32,
                                               downsample=(2, 2), activation=activation, momentum=momentum)
        self.encoder_block2 = EncoderBlockRes4(in_channels=32, out_channels=64,
                                               downsample=(2, 2), activation=activation, momentum=momentum)
        self.encoder_block3 = EncoderBlockRes4(in_channels=64, out_channels=128,
                                               downsample=(2, 2), activation=activation, momentum=momentum)
        self.encoder_block4 = EncoderBlockRes4(in_channels=128, out_channels=256,
                                               downsample=(2, 2), activation=activation, momentum=momentum)
        self.encoder_block5 = EncoderBlockRes4(in_channels=256, out_channels=384,
                                               downsample=(2, 2), activation=activation, momentum=momentum)
        self.encoder_block6 = EncoderBlockRes4(in_channels=384, out_channels=384,
                                               downsample=(2, 2), activation=activation, momentum=momentum)
        self.conv_block7 = EncoderBlockRes4(in_channels=384, out_channels=384,
                                            downsample=(1, 1), activation=activation, momentum=momentum)
        self.conv_block8 = EncoderBlockRes4(in_channels=384, out_channels=384,
                                            downsample=(1, 1), activation=activation, momentum=momentum)
        self.conv_block9 = EncoderBlockRes4(in_channels=384, out_channels=384,
                                            downsample=(1, 1), activation=activation, momentum=momentum)
        self.conv_block10 = EncoderBlockRes4(in_channels=384, out_channels=384,
                                             downsample=(1, 1), activation=activation, momentum=momentum)
        self.decoder_block1 = DecoderBlockRes4(in_channels=384, out_channels=384,
                                               stride=(2, 2), activation=activation, momentum=momentum)
        self.decoder_block2 = DecoderBlockRes4(in_channels=384, out_channels=384,
                                               stride=(2, 2), activation=activation, momentum=momentum)
        self.decoder_block3 = DecoderBlockRes4(in_channels=384, out_channels=256,
                                               stride=(2, 2), activation=activation, momentum=momentum)
        self.decoder_block4 = DecoderBlockRes4(in_channels=256, out_channels=128,
                                               stride=(2, 2), activation=activation, momentum=momentum)
        self.decoder_block5 = DecoderBlockRes4(in_channels=128, out_channels=64,
                                               stride=(2, 2), activation=activation, momentum=momentum)
        self.decoder_block6 = DecoderBlockRes4(in_channels=64, out_channels=32,
                                               stride=(2, 2), activation=activation, momentum=momentum)
        self.bass_blocks = self.get_after_conv_block()
        self.drums_blocks = self.get_after_conv_block()
        self.other_blocks = self.get_after_conv_block()

        self.init_weights()
        self.lr_lambda = lambda step: self.get_lr_lambda(step,
                                                         gamma=self.gamma,
                                                         warm_up_steps=warm_up_steps,
                                                         reduce_lr_steps=reduce_lr_steps)
    def get_after_conv_block(self):
        li = nn.ModuleList()
        li.append(EncoderBlockRes4(in_channels=32, out_channels=32, downsample=(1, 1),
                                   activation=self.activation, momentum=self.momentum))
        li.append(nn.Conv2d(in_channels=32, out_channels=self.channels * self.nsrc * 4 * self.subband,
                            kernel_size=(1, 1), stride=(1, 1), padding=(0, 0), bias=True))
        return li

    def divide(self, data):
        """
        :param data: [batchsize, 8, samples]
        :return: Vocals: [batchsize, 2, samples], Bass: [batchsize, 2, samples], Drums, Others
        """
        return data[:,0:2,:], data[:,2:4,:], data[:,4:6,:]

    def after_conv_block_forward(self, blocks: nn.ModuleList, x):
        block1, block2 = blocks[0], blocks[1]
        x,_ = block1(x)
        return block2(x)

    def get_lr_lambda(self, step, gamma, warm_up_steps, reduce_lr_steps):
        r"""Get lr_lambda for LambdaLR. E.g.,

        .. code-block: python
            lr_lambda = lambda step: get_lr_lambda(step, warm_up_steps=1000, reduce_lr_steps=10000)

            from torch.optim.lr_scheduler import LambdaLR
            LambdaLR(optimizer, lr_lambda)
        """
        if step <= warm_up_steps:
            return step / warm_up_steps
        else:
            return gamma ** (step // reduce_lr_steps)

    def init_weights(self):
        after_conv_blocks = [self.bass_blocks, self.drums_blocks, self.other_blocks]
        init_bn(self.bn0)
        for block in after_conv_blocks:
            for b in block:
                if(type(b) == nn.Conv2d): init_layer(b)

    def forward(self, input, stem=None):
        """
        Args:
          input: (batch_size, channels_num, segment_samples)

        Outputs:
          output_dict: {
            'wav': (batch_size, channels_num, segment_samples),
            'sp': (batch_size, channels_num, time_steps, freq_bins)}
        """

        # sp, cos_in, sin_in = self.f_helper.wav_to_spectrogram_phase(input)

        sp, cos_in, sin_in = self.f_helper.wav_to_mag_phase_subband_spectrogram(input)

        """(batch_size, channels_num, time_steps, freq_bins)"""

        # Batch normalization
        x = sp.transpose(1, 3)
        """(batch_size, freq_bins, time_steps, channels_num)"""
        x = self.bn0(x)  # normalization to freq bins
        """(batch_size, freq_bins, time_steps, channels_num)"""
        x = x.transpose(1, 3)
        """(batch_size, chanenls, time_steps, freq_bins)"""

        # Pad spectrogram to be evenly divided by downsample ratio.
        origin_len = x.shape[2]  # time_steps
        pad_len = int(np.ceil(x.shape[2] / self.downsample_ratio)) * self.downsample_ratio - origin_len
        x = F.pad(x, pad=(0, 0, 0, pad_len))
        cos_in = F.pad(cos_in, pad=(0, 0, 0, pad_len))
        sin_in = F.pad(sin_in, pad=(0, 0, 0, pad_len))
        """(batch_size, channels, padded_time_steps, freq_bins)"""

        # Let frequency bins be evenly divided by 2, e.g., 513 -> 512
        x = x[..., 0: x.shape[-1] - 1]  # (bs, channels, T, F)

        (N_, C_, T_, F_) = x.shape

        # UNet
        (x1_pool, x1) = self.encoder_block1(x)  # x1_pool: (bs, 32, T / 2, F / 2)
        (x2_pool, x2) = self.encoder_block2(x1_pool)  # x2_pool: (bs, 64, T / 4, F / 4)
        (x3_pool, x3) = self.encoder_block3(x2_pool)  # x3_pool: (bs, 128, T / 8, F / 8)
        (x4_pool, x4) = self.encoder_block4(x3_pool)  # x4_pool: (bs, 256, T / 16, F / 16)
        (x5_pool, x5) = self.encoder_block5(x4_pool)  # x5_pool: (bs, 512, T / 32, F / 32)
        (x6_pool, x6) = self.encoder_block6(x5_pool)  # x6_pool: (bs, 1024, T / 64, F / 64)
        x_center,_ = self.conv_block7(x6_pool)  # (bs, 2048, T / 64, F / 64)
        x_center,_ = self.conv_block8(x_center)  # (bs, 2048, T / 64, F / 64)
        x_center,_ = self.conv_block9(x_center)  # (bs, 2048, T / 64, F / 64)
        x_center,_ = self.conv_block10(x_center)  # (bs, 2048, T / 64, F / 64)
        x7 = self.decoder_block1(x_center, x6)  # (bs, 1024, T / 32, F / 32)
        x8 = self.decoder_block2(x7, x5)  # (bs, 512, T / 16, F / 16)
        x9 = self.decoder_block3(x8, x4)  # (bs, 256, T / 8, F / 8)
        x10 = self.decoder_block4(x9, x3)  # (bs, 128, T / 4, F / 4)
        x11 = self.decoder_block5(x10, x2)  # (bs, 64, T / 2, F / 2)
        x12 = self.decoder_block6(x11, x1)  # (bs, 32, T, F)

        res = []

        if(self.stem == "bass"):
            after_conv_blocks = [self.bass_blocks]
        elif (self.stem == "drums"):
            after_conv_blocks = [self.drums_blocks]
        elif (self.stem == "other"):
            after_conv_blocks = [self.other_blocks]
        else:
            after_conv_blocks = [self.bass_blocks, self.drums_blocks, self.other_blocks]

        for block in after_conv_blocks:
            x = self.after_conv_block_forward(block,x12)

            # Recover shape
            x = F.pad(x, pad=(0, 1))
            x = x[:, :, 0: origin_len, :]

            cos_in = cos_in[:, :, 0:origin_len, :]
            sin_in = sin_in[:, :, 0:origin_len, :]

            mag, real,imag, residual, cos,sin,cos_out,sin_out = [],[],[],[],[],[],[], []

            sub_channels = self.subband*self.channels*self.nsrc

            for i in range(self.subband*self.channels*self.nsrc):
                # print(i + sub_channels, i + 1 + sub_channels)
                real.append(x[:,i+sub_channels:i+1+sub_channels,:,:])
                # print(i + sub_channels*2, i + 1 + sub_channels*2)
                imag.append(x[:, i + sub_channels*2:i + 1 + sub_channels*2, :, :])
                # print(i + sub_channels * 3, i + 1 + sub_channels * 3)
                residual.append(x[:, i + sub_channels*3:i + 1 + sub_channels*3, :, :])
                mag.append(torch.relu(torch.sigmoid(x[:, i:i + 1, :, :]) * sp[:, i:i + 1, :, :] + residual[-1]))
                (_, sub_cos, sub_sin) = magphase(real[-1],imag[-1])
                cos.append(sub_cos)
                sin.append(sub_sin)
                cos_out.append(cos_in[:,i:i+1,:,:]*sub_cos-sin_in[:,i:i+1,:,:]*sub_sin)
                sin_out.append(sin_in[:,i:i+1,:,:]*sub_cos+cos_in[:,i:i+1,:,:]*sub_sin)

            length = input.shape[2] // self.subband
            mag = torch.cat(mag,dim=1)
            cos_out = torch.cat(cos_out, dim=1)
            sin_out = torch.cat(sin_out, dim=1)

            wav_out = self.f_helper.mag_phase_subband_spectrogram_to_wav(sps=mag,coss = cos_out,sins=sin_out,length=length)

            # # This part is added after the competition
            # ###############################################################################
            # length = wav_out.size()[-1]
            # mag,cos,sin = self.f_helper.wav_to_spectrogram_phase(wav_out)
            # mag[:,:,:,495:525] *= 0
            # wav_out = self.f_helper.spectrogram_phase_to_wav(mag,cos,sin,length=length)
            # ###############################################################################

            pad_tail = input.size()[-1]-wav_out.size()[-1]
            wav_out = F.pad(wav_out,(0,pad_tail))

            res.append(wav_out)
        res = torch.cat(res, dim=1)
        output_dict = {'wav': res}
        return output_dict

    def configure_optimizers(self):
        optimizer = torch.optim.Adam(self.parameters(), lr=self.lr, amsgrad=True)
        # StepLR = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=self.gamma)
        scheduler = {
            'scheduler': torch.optim.lr_scheduler.LambdaLR(optimizer, self.lr_lambda),
            'interval': 'step',
            'frequency': 1,
        }
        return [optimizer], [scheduler]

    def preprocess(self, batch, train=False):
        if (train):
            bass = batch['bass'].float().permute(0, 2, 1)
            vocals = batch['vocals'].float().permute(0, 2, 1)
            drums = batch['drums'].float().permute(0, 2, 1)
            other = batch['other'].float().permute(0, 2, 1)
            mixture = bass + drums + other + vocals
            return  bass, drums, other, mixture, vocals
        else:  # during test or validaton
            bass = batch['bass'].float().permute(0, 2, 1)
            vocals = batch['vocals'].float().permute(0, 2, 1)
            drums = batch['drums'].float().permute(0, 2, 1)
            other = batch['other'].float().permute(0, 2, 1)
            mixture = bass + drums + other + vocals
            return  bass, drums, other, mixture, vocals, batch['fname'][0]  # a sample for a batch

    def info(self,string:str):
        lg.info("On trainer-" + str(self.trainer.global_rank) + ": " + string)

    def calc_loss(self, output, vocal, name: str):
        l1 = self.l1loss(output, vocal)
        self.log(name, l1, on_step=True, on_epoch=False, logger=True, sync_dist=True, prog_bar=True)
        return l1

    def training_step(self, batch, batch_nb):
        bass, drums, other, mixture, vocals = self.preprocess(batch, train=True)
        est_bass, est_drums, est_other = self.divide(self(mixture)['wav'])
        loss = self.calc_loss(est_other, other, "o")
        loss = loss + self.calc_loss(est_bass, bass, "b")
        loss = loss + self.calc_loss(est_drums, drums, "d")
        if(self.train_step > 10000):
            loss = loss + self.calc_loss(est_bass + est_drums + est_other, mixture-vocals, "m")
        self.log("All-loss", loss, on_step=True, on_epoch=True, logger=True, sync_dist=True)
        self.train_step += 1
        return {"loss": loss}

    def validation_step(self, batch, batch_nb):
        bass, drums, other, mixture, vocals, fname = self.preprocess(batch)
        continuous_nnet = LambdaOverlapAdd(
            nnet=self,
            n_src=self.channels * self.nsrc * 3,
            window_size=self.sample_rate * 20,
            in_margin =int(self.sample_rate*1.5),
            window="boxcar",
            reorder_chunks=False,
            enable_grad=False,
            device=self.device
        )
        est_bass, est_drums, est_other = self.divide(continuous_nnet.forward(mixture))

        loss = self.calc_loss(est_other, other, "o")
        loss = loss + self.calc_loss(est_bass, bass, "b")
        loss = loss + self.calc_loss(est_drums, drums, "d")
        loss = loss + self.calc_loss(est_bass + est_drums + est_other, mixture-vocals, "m")

        est_bass = torch.transpose(est_bass,2,1)
        est_other = torch.transpose(est_other,2,1)
        est_drums = torch.transpose(est_drums,2,1)
        os.makedirs(os.path.join(self.val_result_save_dir_step, str(fname)),exist_ok=True)
        save_wave((tensor2numpy(est_bass) * 2 ** 15).astype(np.short),
                  fname=os.path.join(self.val_result_save_dir_step, str(fname), "bass.wav"))
        save_wave((tensor2numpy(est_drums) * 2 ** 15).astype(np.short),
                  fname=os.path.join(self.val_result_save_dir_step, str(fname), "drums.wav"))
        save_wave((tensor2numpy(est_other) * 2 ** 15).astype(np.short),
                  fname=os.path.join(self.val_result_save_dir_step, str(fname), "other.wav"))
        return {'val_loss':loss}

    def validation_epoch_end(self, outputs):
        # Use the default log function to gather info from gpus
        avg_loss = torch.stack([x['val_loss'] for x in outputs]).mean()
        self.log("val_loss", avg_loss, on_step=False, on_epoch=True, logger=True, sync_dist=True)

if __name__ == '__main__':
    model = UNetResComplex_100Mb(channels=1)
    print(model)


from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
import time
import math
import numpy as np
import pandas as pd
from scipy.spatial import distance_matrix
from sklearn.neighbors import DistanceMetric
from geopy.geocoders import Nominatim

class Coors:
  def __init__(self, latitude=None, longitude=None, geoString = None):
    self.latitude = latitude
    self.longitude = longitude
    self.geoString = geoString
    if self.geoString:
      locator = Nominatim(user_agent = "myGeocoder")
      location = locator.geocode(geoString)
      self.latitude = location.latitude
      self.longitude = location.longitude

class Vehicle:
  def __init__(self, capacity):
    self.capacity = capacity

class TimingWindow:
  def __init__(self, startTime, endTime):
    self.startTime =  startTime
    self.endTime = endTime

class Node:
  def __init__(self, coors, demand = None):
    self.coors = coors
    self.id = id(self)
    self.demand = demand

class Network:
  def __init__(self, depotNode = 0, numVehicles=1, nodes = None, vehicles = None, pickups_deliveries = None):
    self.nodes = []
    self.depot = depotNode
    self.numVehicles = numVehicles
    self.vehicles = vehicles
    self.pickups_deliveries = pickups_deliveries

  def addNodeToNetwork(self, node):
    self.nodes.append(node)

  def addNodeFromCoors(self, coors):
    newNode = Node(coors)
    self.nodes.append(newNode)

class DataModel:
  def __init__(self, network):
    self.data = {}
    self.network = network
    self.data["num_vehicles"] = self.network.numVehicles
    self.data["depot"] = self.network.depot
    self.data["names"] = []
    self.data["demands"] = []
    self.data["vehicle_capacities"] = []
    self.data["pickups_deliveries"] = []

  def calculateDistanceMatrix(self):
    nodeCoorList =[[np.radians(node.coors.latitude), np.radians(node.coors.longitude)] for node in self.network.nodes]

    metric = DistanceMetric.get_metric("haversine")
    self.data["distance_matrix"] =  metric.pairwise(nodeCoorList)*6373

  def assignNames(self):
    self.data["names"] = [node.coors.geoString for node in self.network.nodes]

  def setDemands(self):
    for node in self.network.nodes:
      self.data["demands"].append(node.demand)

  def setVehicleCapacities(self):
    for vehicle in self.network.vehicles:
      self.data["vehicle_capacities"].append(vehicle.capacity)
  def setPickupsAndDeliveries(self):
    self.data["pickups_deliveries"] = self.network.pickups_deliveries

  def getData(self):
    self.setVehicleCapacities()
    self.setDemands()
    self.assignNames()
    self.calculateDistanceMatrix()
    self.setPickupsAndDeliveries()
    return self.data

class vrpWrap:
  def __init__(self, data):
    self.data = data
    self.manager = pywrapcp.RoutingIndexManager(len(data["distance_matrix"]), data["num_vehicles"], data["depot"])
    self.routingManager = pywrapcp.RoutingModel(self.manager)
    self.transit_callback_index = None
    self.solution = None
    self.demand_callback_index = None
    self.distace_dimension = None

  def addDistanceDimension(self):
    self.routingManager.AddDimension(
        self.transit_callback_index,
        0,  # no slack
        3000,  # vehicle maximum travel distance
        True,  # start cumul to zero
        "Distance"
      )
    self.distance_dimension = self.routingManager.GetDimensionOrDie("Distance")
    self.distance_dimension.SetGlobalSpanCostCoefficient(100)

  def addCapacityDimension(self):
    self.routingManager.AddDimensionWithVehicleCapacity(
      self.demand_callback_index,
      0,
      self.data["vehicle_capacities"],
      True,
      "Capacity"
    )

  def distanceCallback(self, fromIndex, toIndex):
    fromNode = self.manager.IndexToNode(fromIndex)
    toNode = self.manager.IndexToNode(toIndex)
    return self.data['distance_matrix'][fromNode][toNode]

  def demandCallback(self, fromIndex):
    fromNode = self.manager.IndexToNode(fromIndex)
    return self.data['demands'][fromNode]

  def solve(self):
    self.transit_callback_index = self.routingManager.RegisterTransitCallback(self.distanceCallback)

    self.routingManager.SetArcCostEvaluatorOfAllVehicles(self.transit_callback_index)

    self.demand_callback_index = self.routingManager.RegisterUnaryTransitCallback(self.demandCallback)

    self.addDistanceDimension()
    self.addCapacityDimension()

    if self.data["pickups_deliveries"]:
      for request in self.data["pickups_deliveries"]:
        pickup_index = self.manager.NodeToIndex(request[0])
        delivery_index = self.manager.NodeToIndex(request[1])
        self.routingManager.AddPickupAndDelivery(
          pickup_index, delivery_index
        )
        self.routingManager.solver().Add(
          self.routingManager.VehicleVar(pickup_index) == self.routingManager.VehicleVar(delivery_index)
        )

        self.routingManager.solver().Add(
          self.distance_dimension.CumulVar(pickup_index) <= self.distance_dimension.CumulVar(delivery_index)
        )

    search_parameters = pywrapcp.DefaultRoutingSearchParameters()
    search_parameters.first_solution_strategy = (
        routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC
    )
    search_parameters.local_search_metaheuristic = (
        routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH
    )

    search_parameters.time_limit.FromSeconds(1)

    self.solution = self.routingManager.SolveWithParameters(search_parameters)

    return self.solution

  def print_solution(self):
    """Prints solution on console."""
    print(f'Objective: {self.solution.ObjectiveValue()}')
    total_distance = 0
    total_load = 0
    max_route_distance = 0
    for vehicle_id in range(self.data['num_vehicles']):
        index = self.routingManager.Start(vehicle_id)
        plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
        route_distance = 0
        route_load = 0
        while not self.routingManager.IsEnd(index):
            node_index = self.manager.IndexToNode(index)
            route_load += self.data['demands'][node_index]
            plan_output += ' {0} Load({1}) -> '.format(self.data['names'][node_index], route_load)

            previous_index = index
            index = self.solution.Value(self.routingManager.NextVar(index))
            route_distance += self.routingManager.GetArcCostForVehicle(
                previous_index, index, vehicle_id
            )
            print(route_distance)

        plan_output += '{0}, Load({1}) \n '.format(self.data['names'][self.manager.IndexToNode(index)], route_load)

        plan_output += 'Distance of the route: {}\n'.format(route_distance)
        plan_output += 'Load of the route: {}\n'.format(route_load)

        print(plan_output)
        total_distance += route_distance
        total_load += route_load

    print('Total distance of all routes: {}km'.format(total_distance))
    print('Total load of all routes: {}'.format(total_load))

if __name__ == '__main__':
  nodes = []
  vehicleNumber =2
  depotNode = 0

  vehicleS = [Vehicle(8),Vehicle(8)]

  coors = [Coors(geoString = "Ambawadi Circle, Ahmedabad"),
           Coors(geoString = "Club 07, Bopal"),
           Coors(geoString = "Naroda Patiya"),
           Coors(geoString = "The Fern Hotel, Sola"),
           Coors(geoString = "Trimandir, Adalaj")]

  demands = [0, 3, 5, 2, 6]
  pickupNdeliveries = [[1,2]]

  network =  Network(depotNode,vehicleNumber,vehicles=vehicleS, pickups_deliveries=pickupNdeliveries)

  for i in range(0, len(coors)):
    newNode = Node(coors[i], demands[i])
    network.addNodeToNetwork(newNode)

  print(DataModel(network).getData())

  vrp = vrpWrap(DataModel(network).getData())
  solution = vrp.solve()

  print(solution)
  print("Solution\n")
  print(vrp.print_solution())


import os
import json
import pandas as pd
import altair as alt
import pathlib
import random
import argparse

HERE = pathlib.Path(__file__).parent

PLOTS = HERE / "plots"
PLOTS.mkdir(exist_ok=True)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    # Add an argument for the path to the results file
    parser.add_argument("results_file", type=str)

    # Add an optional argument for the chart title.
    parser.add_argument("--title", type=str, default=None)

    # Parse the command line arguments
    args = parser.parse_args()
    bench_group, git_hash = args.results_file[:-5].split("_")

    path = pathlib.Path(args.results_file)
    data = json.loads(path.read_text())

    chart_title = args.title or f"Results from {path.name}"

    rows = []

    for result in data["results"]:
        command = result["command"]
        for time in result["times"]:
            rows.append(
                {
                    "Run Group": command,
                    "Time (s)": time,
                }
            )

    df = pd.DataFrame(rows)

    chart = (
        alt.Chart(df)
        .mark_point(size=18)
        .encode(
            y="Run Group",
            x=alt.X(
                "Time (s):Q",
            ).scale(domain=(df["Time (s)"].min() - 2, df["Time (s)"].max() + 2)),
            yOffset="jitter:Q",
            color="Run Group",
        )
        .properties(
            width=200,
            height=200,
            title=chart_title,
        )
        .configure_axisY(title=None)
        .transform_calculate(
            # Generate Gaussian jitter with a Box-Muller transform
            jitter="sqrt(-1*log(random()))*cos(1*PI*random())"
        )
        .resolve_scale(yOffset="independent")
    )

    chart.save(PLOTS / f"{path.name}.svg")


"""This file contains simulation of game between bandits
on the insurance environment.
"""
import numpy as np
import matplotlib.pyplot as plt

from envs import InsuranceMarket
from bandits import EpsGreedy, EXP3, UCBTuned, UCBV
from ARC_GP import ARCPricingModel
from ARC import ARCPricing
from scipy.special import expit

# np.random.seed(0)

# Initialize the environment
env = InsuranceMarket(2, 0.15, 0.5, 1., 0.15*np.sqrt(0.5+0.5*2))

# Initialize number of simulations
no_sim = 1
# Initialize number of time steps
T = 20

# Initialize actions
action_set = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
no_actions = action_set.shape[0]

# Initialize action frequencies
agent1_frequencies = np.zeros(shape=(T, no_actions))
agent2_frequencies = np.zeros(shape=(T, no_actions))

# Initialize agents
dimension = 2
agent_cb = ARCPricing(candidate_margins=action_set,
                      rho=0.1, beta=0.98, sigma=3)
agent_gp = ARCPricingModel(candidate_margins=action_set,
                           rho=0.1, beta=0.98, sigma=3)
dummy_action = 0.75

# Check reward profile
rewards = np.zeros_like(action_set)
frequencies = np.zeros_like(action_set)
no_samples = 200
action_indices = []
observations = []
for j in range(no_samples):
    for i in range(action_set.shape[0]):
        rewards_step, observations_step = env.step(
            np.array([action_set[i], dummy_action]))
        frequencies[i] += observations_step[0, 0]/no_samples
        rewards[i] += rewards_step[0, 0]/no_samples
        observations.append(rewards_step[0, 0])
        action_indices.append(i)

print('Rewards per action', rewards)
print('Acceptance frequencies', frequencies)

# Perform posterior updates
for it in range(0, len(observations)//20):
    # print(f'Action index {action_indices[it]} observation {observations[it]}')
    m, d = agent_cb.update(action_indices[it], observations[it])
    m2, d2 = agent_gp.update(action_indices[it], observations[it])
    # print('Update')
    # print(m, d)
    agent_cb.m = m
    agent_cb.d = d
    agent_gp.m = m2
    agent_gp.d = d2

# Showing computed parameters
rewards_cb = agent_cb.m.flatten()
parameters_gp = agent_gp.m.flatten()
rewards_gp = [parameters_gp[0]]
for i in range(1, len(action_set)):
    rewards_gp.append(rewards_gp[-1]+parameters_gp[i])
plt.plot(action_set, rewards_cb, label='ARC independent arms')
plt.plot(action_set, rewards_gp, label='ARC GP')
plt.grid()
plt.legend()
plt.scatter(action_set, rewards)
plt.xlabel('actions')
plt.ylabel('rewards')
plt.savefig(f'posterior_test_ARC_GP.png')
plt.show()
plt.close()


import fileinput

scores = {")": 3, "]": 57, "}": 1197, ">": 25137}
scores2 = {")": 1, "]": 2, "}": 3, ">": 4}

open_close = {"(": ")", "[": "]", "{": "}", "<": ">"}

incomplete = []

def add(elem):
    global incomplete
    if elem:
        incomplete.append(elem)

total = 0
for line in fileinput.input():
    stack = [line[0]]
    for elem in line[1:-1]:
        if elem in open_close:
            stack.append(elem)
            continue
        if elem != open_close[stack[-1]]:
            total += scores[elem]
            break
        stack.pop()
    else:
        total_score = 0
        while stack:
            total_score *= 5
            closing = open_close[stack.pop()]
            total_score += scores2[closing]
        add(total_score)

print(total)
incomplete.sort()
print(incomplete[len(incomplete) // 2])


import os.path
from ChemReload import *
import numpy as np
import rdkit
from rdkit.Chem import Draw
import networkx as nx
from tqdm import tqdm
from torch.utils.data.sampler import SubsetRandomSampler
from dgl.dataloading import GraphDataLoader
import pickle
from rdkit import Chem
class GenData(object):
    def __init__(self, g_list, node_labels, graph_labels):
        self.g_list = g_list
        self.node_labels = node_labels
        self.graph_labels = graph_labels

class FileLoader(object):
    def __init__(self, args,weight = []):
        self.args = args
        self.weight = weight
    def mol_messages(self,mol  ,bond_label ,atom_label):
        adj =  Chem.rdmolops.GetAdjacencyMatrix(mol)
        edges = []
        edge_labels = []
        atom_symbol = {}
        have_visted = {}
        indices = np.where(adj == 1)
        indices = list(zip(indices[0],indices[1]))

        for indice in indices:
            begin , end = indice
            if (begin,end) not in have_visted and (end,begin) not in have_visted:
                have_visted[(begin,end)] = 1
                bt = GetBondTypeAsDouble(GetBond(mol,begin,end))

                edge_labels.append(bt)
                # beginsymbol= mol.GetAtomWithIdx(int(begin)).GetSymbol()
                # endsymbol = mol.GetAtomWithIdx(int(end)).GetSymbol()
                edges.append(((begin ,end)))
                # edge_labels.append(label[begin][end])
                atom_symbol[begin] = atom_label[begin]
                atom_symbol[end] = atom_label[end]
                # atom_symbol[begin] = GetAtomSymbol(GetAtomByid(mol,begin))
                # atom_symbol[end] = GetAtomSymbol(GetAtomByid(mol,end))
        graph_label = np.where(bond_label>0)[0]
        if len(edges) == 0:
            edges.append((0,0))
            edge_labels.append(0)
            atom_symbol[0] = atom_label[0]
            print(Chem.MolToSmiles(mol))
        if graph_label.size == 0:
            graph_label=0
        else :
            graph_label = graph_label.size/2
        #可以提出来symbol组合了 但是 里边还有字符串和元组 需要 修改
        # indices = np.where(bond_label > 0)
        # atom_symbol = []
        # if len(indices[0]) == 0:
        #     atom_indices = np.where(atom_label>0)
        #     for atom_indice in  atom_indices[0] :
        #         neighbor_symbol =  mol.GetAtomWithIdx(int(atom_indice)).GetSymbol()
        #
        #         atoms = mol.GetAtomWithIdx(int(atom_indice)).GetNeighbors()
        #         for atom in atoms:
        #             neighbor_symbol += atom.GetSymbol()
        #         if neighbor_symbol not in atom_symbol:
        #             atom_symbol.append(tupple(neighbor_symbol))
        #
        # else:
        #     indices = list(zip(indices[0], indices[1]))
        #     bond_2 = np.where(bond_label==2)
        #     if len(bond_2[0])==0:
        #             for indice in indices :
        #                 begin,end = mol.GetAtomWithIdx(int(indice[0])).GetSymbol(),mol.GetAtomWithIdx(int(indice[1])).GetSymbol()
        #
        #
        #                 # beginmap,endmap = mol.GetAtomWithIdx(int(indice[0])).GetAtomMapNum(),mol.GetAtomWithIdx(int(indice[1])).GetAtomMapNum()
        #                 # Draw.MolToImage(mol).save('123.png')
        #                 if (begin,end) not in atom_symbol and (end,begin) not in atom_symbol:
        #                     atom_symbol.append((begin,end))
        #     else:
        #         atom_indices = np.where(atom_label > 0)
        #         for atom_indice in atom_indices[0]:
        #             for indice in indices:
        #                 if atom_indice in indice:
        #                     begin, end = mol.GetAtomWithIdx(int(indice[0])).GetSymbol(), mol.GetAtomWithIdx(
        #                         int(indice[1])).GetSymbol()
        #                     if (begin, end) not in atom_symbol and (end, begin) not in atom_symbol:
        #                         atom_symbol.append((begin, end))
        #     if len(atom_symbol)>1 :
        #         neighbos = []
        #         atoms = []
        #         new_symbol = []
        #         atom_indices = np.where(atom_label>0)
        #         for i  in atom_indices[0]:
        #             i = int(i)
        #
        #             new_symbol.append(mol.GetAtomWithIdx(i).GetSymbol())
        #             neigh = mol.GetAtomWithIdx(i).GetNeighbors()
        #             for nei in neigh:
        #                 neighbos.append(nei.GetIdx())
        #         flag = True
        #         for atom in atom_indices[0]:
        #             if atom not in neighbos:
        #                 flag = False
        #             else:
        #                 continue
        #         if flag == True:
        #             atom_symbol.clear()
        #             atom_symbol .append(tuple(new_symbol))
        #

        return edges,edge_labels,graph_label,atom_symbol
    def load_data(self):
        dataname = self.args.data
        dir = './data/%s/' % (dataname)
        all_edges = []
        all_graph_labels= []
        all_edge_weights = []
        all_edge_labels = []
        all_atom_symbols = []
        count = 0
        for tag in ['train','valid','test']:
            data_dir = os.path.join(dir, tag)
            data_files = [
                f for f in os.listdir(data_dir) if f.endswith('.pkl')
            ]
            data_files.sort()
            # with open(os.path.join(data_dir, 'rxn_data_10009.pkl'), 'rb') as f:
            #     data = pickle.load(f)
            #     mol = data['product_mol']
            #     mol2 = data['reactant_mol']
            #     Draw.MolToImage(mol).save('123.png')
            #     Draw.MolToImage(mol2).save('456.png')
            for file in tqdm(data_files,desc='read pkl'):
                with open(os.path.join(data_dir,file),'rb') as f :
                    data = pickle.load(f)
                mol = data['product_mol']
                bond_labels = data['bond_label']
                atom_labels = data['atom_label']

                edges,edge_labels,graph_label,atom_symbols = self.mol_messages(mol,bond_labels,atom_labels)
                data['graph_label'] = graph_label
                data['graph_id'] = count

                count += 1
                with open(os.path.join(data_dir,file),'wb') as f:
                    pickle.dump(data,f)
                if len(edges) == 0 :
                    print(Chem.MolToSmiles(mol))
                    continue
                if self.args.edge_weight:
                    weight = []
                    for i in len(range(len(edges))):
                        weight.append(self.weight[i])
                else:
                    weight = []
                    for i in range(len(edges)):
                        weight.append(1)
                all_edge_weights.append(weight)
                all_edges.append(edges)
                all_graph_labels.append(graph_label)
                all_atom_symbols.append(atom_symbols)
                all_edge_labels.append(edge_labels)
                #以下是针对symbol组合的
                # if len(atom_symbols)>1:
                #     c=atom_symbols
                #     mol = data['product_mol']
                #     smilse = Chem.MolToSmiles(mol)
                #     mol2 = data['reactant_mol']
                #     Draw.MolToImage(mol).save('123.png')
                #     Draw.MolToImage(mol2).save('456.png')
                # for i in atom_symbols:
                #     b = self.shift_right(i)
                #     if i not in all_atom_symbols :
                #         if b not in all_atom_symbols:
                #             all_atom_symbols[i] = 1
                #         else:
                #             all_atom_symbols[b] += 1
                #     else:
                #         all_atom_symbols[i] += 1

        g_list = []
        for i in tqdm(range(len(all_graph_labels)),desc='create graphs'):

            g_list.append(self.gen_graph(all_edges[i],all_edge_weights[i]))

        return GenData(g_list,all_atom_symbols,all_graph_labels)

    def gen_graph(self, data, weights):
        edges = data
        weights = weights
        g1 = []
        for i in range(len(edges)):
            l = list(edges[i])
            l.append(weights[i])
            g1.append(tuple(l))

        g = nx.Graph()
        g.add_weighted_edges_from(g1)
        return g
    def shift_right(self,l):
        if type(l) == tuple:
            l = list(l)
            return tuple([l[-1]] + l[:-1])
        elif type(l) == str:
            l = l.split(',')

            res = tuple([l[-1]] + l[:-1])
            return res
        else:
            print('ERROR!')
class GINDataLoader():
    def __init__(self,
                 dataset,
                 batch_size,
                 device,
                 collate_fn=None,
                 seed=0,):

        self.seed = seed
        self.kwargs = {'pin_memory': True} if 'cuda' in device.type else {}
        print(len(dataset))
        labels = [l for _, l in dataset]
        idx = []
        for i in range(len(labels)):
            idx.append(i)

        sampler = SubsetRandomSampler(idx)

        self.train_loader = GraphDataLoader(
            dataset, sampler=sampler,
            batch_size=batch_size, collate_fn=collate_fn, **self.kwargs)

    def train_valid_loader(self):
        return self.train_loader


from collections import defaultdict
all=open('5','r').readline().strip()

testdata = "dabAcCaCBAcCcaDA"
testdata = "abbadabAcCaCBAcCcaDAabbaad"

#all = testdata
#all = "aabAAB"
#all = "dabAcCaCBAcCcaDA"
print(len(all))

def polymerize(all):
	i = 0
	while i < len(all)-1:
		c1, c2 = all[i], all[i+1]
		if (c1 == c2.upper() or c1.upper() == c2) and c1 != c2:
	#		print("removed {0} at {1}".format(c1 + c2, i))
			all = all[:i] + all[i+2:]
	#		print("=> {0}".format(all))
			i = max(0, i-1)
		else:
			i += 1
	return len(all)

def removeunit(c, s):
	r, i, c = "", 0, c.upper()
	while i < len(s):
		if c != s[i].upper():
#			print("removing {0} at {1}".format(s[i], i))
			r += s[i]
		i += 1
	return r

cmin, rmin = "", rmin

for c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
	r = polymerize(removeunit(c, all))
	print(c, r)
	if r < rmin:
		rmin, cmin = r, c

print("=> {0} - {1}".format(cmin, rmin))
#print(polymerize(all))
#print(all)
#print(removeunit('a', all))


import folium
import pandas

data = pandas.read_csv("volcanoes.txt")
lat = list(data.loc[:, 'LAT'])
lon = list(data.loc[:, 'LON'])
volname = list(data.loc[:, "NAME"])
volstatus = list(data.loc[:, 'STATUS'])
volelev = list(data.loc[:, "ELEV"])
vollocation = list(data.loc[:, 'LOCATION'])
voltype = list(data.loc[:, 'TYPE'])

html_popup = """
Volcano information:

Name: %s

Status: %s

Height: %s m

Location: %s

Type: %s

"""

pythonmaps = folium.Map(location=[
                        18.918321222877136, - 73.51323138761227], zoom_start=9, tiles="Stamen Terrain")

fgv = folium.FeatureGroup(name="Volcanoes")

def color_producer(elevation):
    if elevation < 1000:
        return 'green'
    elif 1001 <= elevation < 3000:
        return 'orange'
    else:
        return 'red'

for lt, ln, na, st, el, lc, tp in zip(lat, lon, volname, volstatus, volelev, vollocation, voltype):
    iframe = folium.IFrame(html=html_popup % (
        str(na), str(st), str(el), str(lc), str(tp)), width=200, height=100)
    fgv.add_child(folium.Marker(
        location=[lt, ln], popup=folium.Popup(iframe), icon=folium.Icon(color=color_producer(el))))

fgp = folium.FeatureGroup(name="Population")

fgp.add_child(folium.GeoJson(
    data=open('world.json', "r", encoding="utf-8-sig").read(),
    style_function=lambda x: {'fillColor': 'green' if x['properties']['POP2005'] < 10000000
                              else 'orange' if 10000001 < x['properties']['POP2005'] < 100000000 else 'red'}))

pythonmaps.add_child(fgv)

pythonmaps.add_child(fgp)

pythonmaps.add_child(folium.LayerControl())

pythonmaps.save("Python_Webmaps.html")


import hashlib
import os
import ruamel.yaml as yaml
from tripmining.model.checkin import Checkin
from tripmining.model.coordinate import Coordinate
from tripmining.model.location import Location
import datetime
from datetime import timedelta
import random
import string

def generate_location_id():
    return ''.join([random.choice(string.ascii_letters + string.digits) for n in range(32)])

def generate_home_checkins(user_id, location, from_date, to_date, number_of_checkins):
    """
    Generating some extra checkins to be added before and after a trip. This is to make home location the most frequent
    checked-in location
    :param user_id:
    :param location:
    :param from_date:
    :param to_date:
    :param number_of_checkins:
    :return:
    """
    home_checkins = []
    if not from_date:
        for day in range(number_of_checkins, -1, -1):
            home_checkins.append(Checkin(user=user_id, location=location, local_date=to_date - timedelta(days=day)))
    if not to_date:
        for day in range(number_of_checkins):
            home_checkins.append(Checkin(user=user_id, location=location, local_date=from_date + timedelta(days=day)))
    return home_checkins

def generate_trip_checking(user_id, location, date, number_of_checkins):
    trip_checkins = []
    for checkin in range(number_of_checkins):
        trip_checkins.append(Checkin(user=user_id, location=location, local_date=date))
    return trip_checkins

def generate_checkins(test_data):
    user_home_info = test_data['home']
    last_home_checkin_date = datetime.datetime.strptime(test_data['last_home_before_trip'], '%Y-%m-%d %H:%M:%S')
    first_home_after_trip = datetime.datetime.strptime(test_data['first_home_after_trip'], '%Y-%m-%d %H:%M:%S')
    home_coordinate = Coordinate(lat=user_home_info['location']['lat'], lng=user_home_info['location']['lng'])
    home_location = Location(location_id='23456', country_code=user_home_info['country'], coordinate=home_coordinate,
                             category='admin')
    checkins_before_trip = generate_home_checkins(user_id=test_data['user_id'], location=home_location, from_date=None,
                                                  to_date=last_home_checkin_date, number_of_checkins=10)
    trip_checkins = [] + checkins_before_trip

    for block in test_data['trip']['blocks']:
        block_info = block['block']
        block_coordinate = Coordinate(lat=block_info['location']['lat'], lng=block_info['location']['lng'])
        block_location = Location(location_id=generate_location_id(), coordinate=block_coordinate,
                                  country_code=block_info['location']['code'], category='admin')
        for day in block_info['days']:
            trip_checkins = trip_checkins + generate_trip_checking(test_data['user_id'], block_location,
                                                                   datetime.datetime.strptime(day['day']['date'],
                                                                                '%Y-%m-%d %H:%M:%S'),
                                                                   day['day']['number_of_checkins'])

    checkins_after_trip = generate_home_checkins(user_id=test_data['user_id'], location=home_location, to_date=None,
                                                 from_date=first_home_after_trip, number_of_checkins=10)
    trip_checkins = trip_checkins + checkins_after_trip
    return trip_checkins

def prepare_chekins_from_raw_data(test_data):
    trip_checkins = generate_checkins(test_data)
    return trip_checkins

def prepare_traveler_from_raw_data(trips: list):
    """
    Construct a traveler by combining a list of single trips
    :param trips:
    :return:
    """
    traveler_chekins = []
    for trip in trips:
        traveler_chekins = traveler_chekins + generate_checkins(trip)
    return sorted(traveler_chekins, key=lambda x: x.date)

def load_trip(trip_name):
    root_dir = os.getcwd()
    with open(os.path.join(os.path.join(os.path.join(root_dir, 'test'), 'quality_metrics'),
                           os.path.join('resources', os.path.join('trips', trip_name + '.yml')))) as file:
        test_data = yaml.load(file, Loader=yaml.Loader)
        return test_data["test"]

def imitate_trip_id(trip):
    """
    Re-producing the trip id which is assiged for each trip in the library. This trip id is mapped with the test name to
    the trip name to verify filtered trips
    :param trip:
    :return:
    """
    return hashlib.sha1((str(trip["user_id"]) + str(prepare_chekins_from_raw_data(trip)[11].date) +
                         str(prepare_chekins_from_raw_data(trip)[-11].date)).encode('utf-8')).hexdigest()[:10]


##############################################################################################
# FILE : ex9.py
# WRITER : Bar Aloni
# EXERCISE : intro2cs ex9 2016-2017
# DESCRIPTION :
#   def __init__:
# A constructor for a GameRunner object
#   def move_object:
# changes position of the given object
#   def run:
# runs the game
#   def _do_loop:
# runs the game loop in loops
#   def asteroid_split_to_one:
# creates a new, smaller asteroid which takes characters of the original
# asteroid and torpedo that hit it. the asteroid registers in Screen and
# GameRunner
#   def ship_part:
# conduct the ships movements, updates rotation speed and location
#   def asteroid_part:
# conduct all movement of all asteroids.
#   def torpedo_part:
# conducts all creation, registration, movements and disposal of
# torpedoes
#   def collision_ship_asteroid:
# when asteroid hits our ship: updates status of ship: lives, and of
# asteroid: registration
#   def collision_torpedo_asteroid:
# when torpedo hits asteroid: updates score, asteroid splits, updates
# torpedo and asteroids registration status
# def main:
# runs the game
####################################################################################

from screen import Screen
from ship import Ship
from asteroid import Asteroid
from torpedo import Torpedo
from random import randint
import sys
import math

DEFAULT_ASTEROIDS_NUM = 3

LOSING_LIFE_TITLE = 'BE CAREFUL!'
LOSING_LIFE_MSG = 'You got hit by an asteroid!\nYou lost 1 life point.'

WIN_TITLE = 'HURRAY! :)'
WIN_MSG = 'You managed to survive the asteroid attack\nWELL DONE!'

LOSE_TITLE = 'OH NO...'
LOSE_MSG = "You're out of lives.\nBETTER LUCK NEXT TIME!"

class GameRunner:
    """
    Class GameRunner description:
    A class representing an Asteroids game.
    A game is composed of asteroids that are moving on a square board and a
    space ship controlled by user with the array keys, which can rotate,
    accelerate and fire torpedoed to destroy the asteroids.
    """
    SCORE = 0
    DEGREES = 7

    def __init__(self, asteroids_amnt):
        """
        A constructor for a GameRunner object
        :param asteroids_amnt: int, asteroids to start the game with
        """
        self._screen_max_x = Screen.SCREEN_MAX_X
        self._screen_max_y = Screen.SCREEN_MAX_Y
        self._screen_min_x = Screen.SCREEN_MIN_X
        self._screen_min_y = Screen.SCREEN_MIN_Y
        self._delta_x = self._screen_max_x - self._screen_min_x
        self._delta_y = self._screen_max_y - self._screen_min_y

        self._screen = Screen()

        self._my_ship = Ship(randint(self._screen_min_x, self._screen_max_x),
                            randint(self._screen_min_y, self._screen_max_y))

        self._asteroids = []
        for num in range(asteroids_amnt):
            self._asteroids.append(Asteroid(randint(self._screen_min_x,
                        self._screen_max_x), randint(self._screen_min_y,
                        self._screen_max_y), randint(10,30), randint(10,30),
                                           Asteroid._SIZE))
        for asteroid in self._asteroids:
            self._screen.register_asteroid(asteroid , asteroid.get_size())

    def move_object(self, obj):
        """
        changes position of the given object
        :param object: object from class Ship Torpedo or Asteroid
        :param x: int, self.x of the object: location on X axis
        :param y: int, self.y f the object: location on Y axis
        """
        new_coor_x = (obj.get_speed_x() + obj.get_coordinate_x() -
                      self._screen_min_x) % self._delta_x + self._screen_min_x
        new_coor_y = (obj.get_speed_y() + obj.get_coordinate_y() -
                      self._screen_min_y) % self._delta_y + self._screen_min_y
        obj.set_coordinate_x(new_coor_x)
        obj.set_coordinate_y(new_coor_y)

    def run(self):
        """
        runs the game
        """
        self._do_loop()
        self._screen.start_screen()

    def _do_loop(self):
        """
        runs the game loop in loops
        """
        # You don't need to change this method!
        self._game_loop()

        # Set the timer to go off again
        self._screen.update()
        self._screen.ontimer(self._do_loop,5)

    def asteroid_split_to_one(self, asteroid, torpedo, dir):
        """
        creates a new, smaller asteroid which takes characters of the original
        asteroid and torpedo that hit it. the asteroid registers in Screen and
        GameRunner
        :param asteroid: a given instance of Asteroid Class
        :param torpedo: a given instance of Torpedo Class
        :param dir: direction of the new asteroid : 1 or -1.
        """
        new_speed_x = (torpedo.get_speed_x() + dir*asteroid.get_speed_x()) /\
            ((asteroid.get_speed_x()**2 + asteroid.get_speed_y()**2)**(1/2))
        new_speed_y = (torpedo.get_speed_y() + dir*asteroid.get_speed_y()) /\
            ((asteroid.get_speed_x()**2 + asteroid.get_speed_y()**2)**(1/2))
        baby_asteroid = Asteroid(asteroid.get_coordinate_x(),
                    asteroid.get_coordinate_y(), new_speed_x, new_speed_y,
                                 (asteroid.get_size()-1))
        self._asteroids.append(baby_asteroid)
        self._screen.register_asteroid(baby_asteroid, baby_asteroid.get_size())

    def ship_part(self):
        """
        conduct the ships movements, updates rotation speed and location
        """
        if self._screen.is_up_pressed():
            self._my_ship.accelerate()
        elif self._screen.is_right_pressed():
            self._my_ship.set_heading(-self.DEGREES)
        elif self._screen.is_left_pressed():
            self._my_ship.set_heading(self.DEGREES)
        self.move_object(self._my_ship)
        self._screen.draw_ship(self._my_ship.get_coordinate_x(),
                self._my_ship.get_coordinate_y(), self._my_ship.get_heading())

    def asteroid_part(self):
        """
        conduct all movement of all asteroids.
        """
        for asteroid in self._asteroids:
            self.move_object(asteroid)
            self._screen.draw_asteroid(asteroid, asteroid.get_coordinate_x(),
                                       asteroid.get_coordinate_y())

    def torpedo_part(self):
        """
        conducts all creation, registration, movements and disposal of
        torpedoes
        """
        if self._screen.is_space_pressed() and len(self._my_ship.get_torpedo_list())\
                < self._my_ship.get_torpedo_limit():
            torpedo = self._my_ship.make_torpedo()
            self._screen.register_torpedo(torpedo)
        for torpedo in self._my_ship.get_torpedo_list():
            self.move_object(torpedo)
            self._screen.draw_torpedo(torpedo, torpedo.get_coordinate_x(),
                                      torpedo.get_coordinate_y(), torpedo.get_heading())
            if torpedo.get_round() == torpedo.get_lifetime():
                self._screen.unregister_torpedo(torpedo)
                self._my_ship.remove_torpedo(torpedo)
            else:
                torpedo.add_round_count()

    def collision_ship_asteroid(self):
        """
        when asteroid hits our ship: updates status of ship: lives, and of
        asteroid: registration
        """
        for asteroid in self._asteroids:
            if asteroid.has_intersection(self._my_ship):
                self._my_ship.set_life()
                self._screen.show_message(LOSING_LIFE_TITLE, LOSING_LIFE_MSG)
                self._screen.remove_life()
                self._screen.unregister_asteroid(asteroid)
                self._asteroids.remove(asteroid)

    def collision_torpedo_asteroid(self):
        """
        when torpedo hits asteroid: updates score, asteroid splits, updates
        torpedo and asteroids registration status
        """
        for torpedo in self._my_ship.get_torpedo_list():
            for asteroid in self._asteroids:
                if asteroid.has_intersection(torpedo):
                    self.SCORE += asteroid.get_size() * 10
                    self._screen.set_score(self.SCORE)
                    if asteroid.get_size() > 1:
                        self.asteroid_split_to_one(asteroid, torpedo, 1)
                        self.asteroid_split_to_one(asteroid, torpedo, -1)
                    self._screen.unregister_asteroid(asteroid)
                    self._asteroids.remove(asteroid)
                    if torpedo in self._my_ship.get_torpedo_list():
                    # when torpedo hits 2 asteroids simultaneously
                        self._screen.unregister_torpedo(torpedo)
                        self._my_ship.remove_torpedo(torpedo)

    def _game_loop(self):
        # exits:
        if self._asteroids == []:
            self._screen.show_message(WIN_TITLE, WIN_MSG)
            self._screen.end_game()
            sys.exit()
        elif self._my_ship.get_life() < 1:
            self._screen.show_message(LOSE_TITLE, LOSE_MSG)
            self._screen.end_game()
            sys.exit()
        elif self._screen.should_end():
            self._screen.end_game()
            sys.exit()
        # game conducting:
        self.ship_part()
        self.asteroid_part()
        self.collision_ship_asteroid()
        self.torpedo_part()
        self.collision_torpedo_asteroid()

def main(amnt):
    """
    runs the game
    :param amnt: asteroids instances amount in int
    :return:
    """
    runner = GameRunner(amnt)
    runner.run()

if __name__ == "__main__":
    if len(sys.argv) > 1:
        main( int( sys.argv[1] ) )
    else:
        main( DEFAULT_ASTEROIDS_NUM )


import json
import psycopg2
import pprint
import pdb

pp = pprint.PrettyPrinter(indent=4)
DEBUG = True

def routes(query):
    resp={}

    db='pistes_api_osm2pgsql'
    global conn
    global cur
    # ~ try:
    conn = psycopg2.connect("dbname="+db+" user=osmuser")
    cur = conn.cursor()
    # ~ except:
        # ~ pass

    cs=query.split(';')
    coords=[]
    for c in cs:
        coord={}
        coord['lon']=float(c.split(',')[0])
        coord['lat']=float(c.split(',')[1])
        coords.append(coord)

    for c in coords:
        waypoint=snap(c)
        if waypoint:
            if 'waypoints' not in resp:
                resp['waypoints']=[]
            wpt={}
            wpt['edge_id'], wpt['way_osm_id'], wpt['coord'], wpt['ratio'] = waypoint
            resp['waypoints'].append(wpt)
        else:
            conn.close()
            return resp
    # ~ if (DEBUG):
      # ~ print(resp)

    for i in range(len(resp['waypoints']) -1):
        leg = route(resp['waypoints'][i],resp['waypoints'][i+1])
        if (DEBUG):
          print("leg: "+str(leg))
        if leg['geometry'] != '':
            if 'routes' not in resp:
                resp['routes']=[]
                resp['routes'].append({})
            if 'legs' not in resp['routes'][0]:
                resp['routes'][0]['legs']=[]
            resp['routes'][0]['legs'].append(leg)
        else:
            conn.close()
            return {}
    # ~ if (DEBUG):
      # ~ print(resp)
    # re-compute each step distance

    distance=0
    for leg in resp['routes'][0]['legs']:
        for step in leg['steps']:
            step['distance']=step['length']
            distance += step['length']
        leg['distance']=distance
    resp['routes'][0]['distance']=distance

    # compute the whole route geometry
    enc_pol_sql_cat=''
    distance=0
    for leg in resp['routes'][0]['legs']:
        geo = leg['geometry']
        if(DEBUG): pp.pprint(geo)
        enc_pol_sql_cat += 'ST_LineFromEncodedPolyline(\''+geo+'\'),'

    enc_pol_sql_cat = enc_pol_sql_cat[:-1]
    query= ("""
    SELECT ST_AsEncodedPolyline(ST_LineMerge(ST_Collect(ARRAY[%s])));
    """ % (enc_pol_sql_cat,))

    cur.execute(query)
    enc_pol = cur.fetchone()[0]
    resp['routes'][0]['geometry'] = enc_pol

    conn.close()
    if len(resp['routes']) == 1 and len(resp['routes'][0]['legs']) == len(resp['waypoints'])-1:
        return resp
    else:
        return {}

def route (wpt1, wpt2):
    leg={}
    leg['length']=0
    leg['steps']=[]
    leg['geometry']={}
    leg['geometry']=''
    query = ("""
    select * FROM pgr_trsp(
    'SELECT CAST(id AS INT4) AS id , CAST(source AS INT4), CAST(target AS INT4), cost, reverse_cost
    FROM lines_noded
    WHERE geom && ST_Expand(
        (SELECT ST_Collect(geom) FROM lines_noded WHERE id IN( %s,%s ) ) , 0.1)
    ',
     %s,%s,%s,%s,
     true, true
    );
    """ % ( wpt1['edge_id'],
            wpt2['edge_id'],
            wpt1['edge_id'],
            wpt1['ratio'],
            wpt2['edge_id'],
            wpt2['ratio'], ))
    cur.execute(query)
    pgr = cur.fetchall()
    if(DEBUG):
        print("pgr: \n")
        pp.pprint(pgr)

    """
    seq, id1 (node), id2 (edge), cost
    [(0, -1, 44640, 279.111767683319),
    (1, -2, -1, 0.0)]

    [   (0, -1, 44640, 407.395229956857),
    (1, 59439, 386348, 39.3195107816717),
    (2, -2, -1, 0.0)]

    [   (0, -1, 48415, 463.229257602512),
    (1, 45853, 48414, 1140.50141060662),
    (2, 42851, 34474, 816.039162049905),
    (3, 43909, 45001, 300.405949609085),
    (4, -2, -1, 0.0)]
    """

    for p in pgr[:-1]:
        step={}
        query = ("""
        SELECT old_id
        FROM lines_noded
        WHERE id = %s
        ;
        """ % (p[2],) )

        cur.execute(query)
        osm_id = cur.fetchone()[0]
        if(DEBUG):
            print(p[2], osm_id)

        step['way_osm_id']=osm_id
        step['name']=osm_id
        step['edge_id']=p[2]

        query = ("""
        SELECT length_m
        FROM lines_noded
        WHERE id = %s
        ;
        """ % (p[2],))
        cur.execute(query)
        l = cur.fetchone()[0]

        step['length']=l
        leg['steps'].append(step)

    edges_ids=''
    for p in pgr[:-1]:
        edges_ids += str(p[2])+','
    edges_ids = edges_ids[:-1]
    # Compute overall leg geometry
    if len(pgr) == 2 : # Routing along a single edge
        query = ("""
                SELECT ST_LineLocatePoint(geom, ST_SetSRID(ST_MakePoint(%s,%s),4326)),
                       ST_LineLocatePoint(geom, ST_SetSRID(ST_MakePoint(%s,%s),4326))
                FROM lines_noded WHERE id = %s;
                """ % ( wpt1['coord']['lon'],
                        wpt1['coord']['lat'],
                        wpt2['coord']['lon'],
                        wpt2['coord']['lat'],
                        pgr[0][2]
                        ))

        cur.execute(query)
        r1, r2 = cur.fetchone()
        if r2 > r1:
            ratio = str(r1) + ',' + str(r2)
        else:
            ratio = str(r2) + ',' + str(r1)
        query = ("""
                SELECT ST_AsEncodedPolyline(ST_LineSubstring(geom, %s)),
                    ST_AsText(ST_LineSubstring(geom, %s))
                FROM lines_noded WHERE id = %s;
                """ % ( ratio,
                        ratio,
                        pgr[0][2]
                        ))
        cur.execute(query)
        pol, wkt = cur.fetchone()
        leg['geometry']=pol

        leg['steps'][0]['length']= leg['steps'][0]['length'] * abs(r2-r1)

    if len(pgr) > 2 : # len(pgr) > 2
        # Fist computing first edge ratio, knowing that the edge is not neccessarily
        # in the direction of the way:
        query = ("""
            WITH end_first_edge AS (
                SELECT ST_EndPoint(geom)
                AS geom
                FROM lines_noded WHERE id = %s
                ),
                next_vertice AS (
                SELECT the_geom
                AS geom
                FROM lines_noded_vertices_pgr WHERE id =  %s
                ),
                ratio AS (
                SELECT ST_LineLocatePoint(geom, ST_SetSRID(ST_MakePoint(%s,%s),4326))::float as r
                FROM lines_noded WHERE id = %s
                )
            SELECT CASE
            WHEN
                ST_Equals(end_first_edge.geom, next_vertice.geom)
            THEN
                (ratio.r, 1.0)
            ELSE
                (0.0,ratio.r)
            END
            FROM end_first_edge CROSS JOIN next_vertice CROSS JOIN ratio;
                """ % ( pgr[0][2],
                        pgr[1][1],
                        wpt1['coord']['lon'],
                        wpt1['coord']['lat'],
                        pgr[0][2]))

        cur.execute(query)
        first_ratio = cur.fetchone()[0].strip('()')

        if first_ratio.find('0.0') != -1 :
            r = float(first_ratio[1:-2].split(',')[1])
            leg['steps'][0]['length']= leg['steps'][0]['length'] * r
        else:
            r = float(first_ratio[1:-2].split(',')[0])
            leg['steps'][0]['length']= leg['steps'][0]['length'] * ( 1 - r)

        # Same for last edge
        query = ("""
            WITH start_last_edge AS (
                SELECT ST_StartPoint(geom)
                AS geom
                FROM lines_noded WHERE id = %s
                ),
                prev_vertice AS (
                SELECT the_geom
                AS geom
                FROM lines_noded_vertices_pgr WHERE id =  %s
                ),
                ratio AS (
                SELECT ST_LineLocatePoint(geom, ST_SetSRID(ST_MakePoint(%s,%s),4326))::float as r
                FROM lines_noded WHERE id = %s
                )
            SELECT CASE
            WHEN
                ST_Equals(start_last_edge.geom, prev_vertice.geom)
            THEN
                (0.0,ratio.r)
            ELSE
                (ratio.r, 1.0)
            END
            FROM start_last_edge CROSS JOIN prev_vertice CROSS JOIN ratio;
                """ % ( pgr[-2][2],
                        pgr[-2][1],
                        wpt2['coord']['lon'],
                        wpt2['coord']['lat'],
                        pgr[-2][2]))

        cur.execute(query)
        last_ratio = cur.fetchone()[0].strip('()')

        if last_ratio.find('0.0') != -1:
            r = float(last_ratio[1:-2].split(',')[1])
            leg['steps'][-1]['length']= leg['steps'][-1]['length'] * r
        else:
            r = float(last_ratio[1:-2].split(',')[0])
            leg['steps'][-1]['length']= leg['steps'][-1]['length'] * ( 1 - r)

        if len(pgr) > 3:
            query = ("""
                WITH
                    start_edge AS (
                        SELECT ST_LineSubstring(ST_SnapToGrid(geom,0.0000001), %s)
                        AS geom
                        FROM lines_noded WHERE id = %s
                    ),
                    end_edge AS (
                        SELECT ST_LineSubstring(ST_SnapToGrid(geom,0.0000001), %s)
                        AS geom
                        FROM lines_noded WHERE id = %s
                    ),
                    middle_edges AS (
                        SELECT
                            ST_LineMerge(ST_Collect(ST_SnapToGrid(geom,0.0000001)))
                        AS geom
                        FROM lines_noded WHERE id IN (%s)
                    )
                SELECT
                    ST_AsEncodedPolyline(
                            ST_LineMerge(ST_Collect(ARRAY[start_edge.geom, middle_edges.geom, end_edge.geom]))
                    ),
                    ST_AsText(
                        ST_LineMerge(ST_Collect(ARRAY[start_edge.geom, middle_edges.geom, end_edge.geom]))
                    )
                FROM start_edge , middle_edges , end_edge
                ;
                """ % ( first_ratio,
                        pgr[0][2],
                        last_ratio,
                        pgr[-2][2],
                        ','.join(edges_ids.split(',')[1:-1])
                        )
                )
            if (DEBUG):
              print(query)
            cur.execute(query)
            pol, wkt = cur.fetchone()
            leg['geometry']=pol
            if (DEBUG):
              print(wkt)
        else: # only two edges
            query = ("""
                WITH
                    start_edge AS (
                        SELECT ST_LineSubstring(ST_SnapToGrid(geom,0.0000001), %s)
                        AS geom
                        FROM lines_noded WHERE id = %s
                    ),
                    end_edge AS (
                        SELECT ST_LineSubstring(ST_SnapToGrid(geom,0.0000001), %s)
                        AS geom
                        FROM lines_noded WHERE id = %s
                    )
                SELECT
                    ST_AsEncodedPolyline(
                            ST_LineMerge(ST_Collect(ARRAY[start_edge.geom, middle_edges.geom, end_edge.geom]))
                    ),
                    ST_AsText(
                        ST_LineMerge(ST_Collect(ARRAY[start_edge.geom, middle_edges.geom, end_edge.geom]))
                    )
                FROM start_edge CROSS JOIN end_edge
                ;
                """ % ( first_ratio,
                        pgr[0][2],
                        last_ratio,
                        pgr[-2][2]
                        )
                )
            cur.execute(query)
            pol, wkt = cur.fetchone()
            leg['geometry']=pol
    return leg

def snap(c):
    osmid=1
    coord={}
    ratio=1.0

    # define the bbox where requesting the data
    lon=c['lon']
    lat=c['lat']
    bottom=c['lat']-0.05
    top=c['lat']+0.05
    left=c['lon']-0.05
    right=c['lon']+0.05

    cur.execute("""
    select id, old_id, ST_LineLocatePoint(geom, ST_GeometryFromText('POINT(%s %s)', 4326))
    FROM lines_noded
    WHERE geom && st_setsrid('BOX3D(%s %s,%s %s)'::box3d, 4326)  \
    ORDER BY ST_Distance(geom, ST_GeometryFromText('POINT(%s %s)', 4326)) LIMIT 1;
    """ % (lon, lat, left, bottom, right, top, lon, lat) )
    edid, osmid, ratio = cur.fetchone()

    cur.execute("""
    select ST_X(ST_LineInterpolatePoint(geom,%s)),
    ST_Y(ST_LineInterpolatePoint(geom,%s))
    FROM lines_noded
    WHERE id = %s
    LIMIT 1;
    """ % (ratio,ratio,edid) )
    coord['lon'], coord['lat'] = cur.fetchone()
    return (edid, str(osmid), coord, ratio)


#  Faça um programa que peça o tamanho de um arquivo para download (em MB) e a velocidade
#  de um link de Internet (em Mbps), calcule e informe o tempo aproximado de download
#  do arquivo usando este link (em minutos).

class CalculadorDeVelocidade:
    def __init__(self, tamanho, velocidade):
        self.velocidade = velocidade  # em Mbps
        self.tamanho = tamanho  # em MB

    def calculador(self):
        segundo = 0
        em_bytes = self.tamanho * 1_048_576
        v_em_bytes = self.velocidade * 1_048_576
        while em_bytes > 0:
            segundo += 1
            em_bytes -= v_em_bytes
        minuto = int(segundo / 60)
        return f'Aproximadamente {minuto} minutos para baixar por esse link'

if __name__ == '__main__':
    medidor = CalculadorDeVelocidade(500, 1.72)
    print(medidor.calculador())

arquivo = float(input('Qual o tamanho do arquivo em MB?: '))  # resposta certa, corrigido da internet
link = float(input('Qual a velocidade do link de internet em Mbps? '))

tempo = (arquivo / link)
tempo_min = tempo // 60
tempo_seg = tempo - tempo_min

print(f'O tempo aproximado de download é de {tempo_min} minutos e {tempo_seg} segundos')


import re

cleaned_lines = list()

"""
# Using lists
def remove_words(file, words):
    with open(file, 'r+') as f:
        # parse line by line
        for line in f:
            x = line.strip().split(' ')
            #
            cleaned_line = [word for word in x if word not in words]
            #
            cleaned_lines.append(cleaned_line)

        # converting back to a string from lists
        list_of_joined_strings = [' '.join(element) for element in cleaned_lines]
        output = ' \n'.join(list_of_joined_strings) + '\n'

        f.seek(0)
        f.write(output)
        f.truncate()
"""

# Same, but using regular expressions
def remove_words(file, words):
    patterns = [r'\b' + word + r'\b' for word in words]
    patterns = [re.compile(element) for element in patterns]
    with open(file, 'r+') as f:
        string_to_process = f.read()
        for p in patterns:
            string_to_process = p.sub('', string_to_process)
        f.seek(0)
        f.write(string_to_process)
        f.truncate()

filename = 'remove_words.txt'
words_to_remove = ['word', 'to', 'remove']
remove_words(filename, words_to_remove)