# Imports from FindRoots
from FindRoots import fixPoint
from FindRoots import bisection
from FindRoots import falsPos
from FindRoots import newRap
from FindRoots import secant
from FindRoots import multipleRoot
from FindRoots import intervals
from FindRoots import derivatives

# Imports from Integrals
from Integrals import Riemann
from Integrals import trapecio
from Integrals import Simpson
from Integrals import Romberg
from Integrals import gaussSeidel
from Integrals import jacobi
from Integrals import jacobiMatriz

# Imports from Regressions
from Regressions import linear
from Regressions import polinomial

# Imports from Interpolations
from Interpolations import newtonInter
from Interpolations import lagrannge

# imports for operations
import math
import matplotlib.pyplot as plt
import numpy as np

# Menus
def mainMenu():
    print("-------------------------Menú Principal-------------------------")
    print(" 1. Raíces y Derivadas\n",
          "2. Integrales\n",
          "3. Matirces\n",
          "4. Regresiones\n",
          "5. Interpolaciones\n")

def derivativesAndRootsMenu():
    print("----------------------------------------------------------------")
    print(" 1. Encontrar derivadas\n",
          "2. Buscar Raíces\n")

def rootsMenu():
    print("--------------------------Menú Raíces---------------------------")
    print(" 1. Método de Punto Fijo (Modificar para g(x))\n",
          "2. Método de Bisección\n",
          "3. Método de Falsa Posición\n",
          "4. Método de Newton-Raphson\n",
          "5. Método de la Secante\n",
          "6. Método de Raíces Múltiples (Newton-Raphson modificado)\n")

def integralsMenu():
    print("-------------------------Menú Integrales------------------------")
    print(" 1. Integral por Riemann\n",
          "2. Integral por Trapecios\n",
          "3. Integral por Simpson\n",
          "4. Integral por Romberg\n")

def matrixsMenu():
    print("-------------------------Menú Matrices--------------------------")
    print(" 1. Matriz de GaussSeidel\n",
          "2. Matriz de Jacobi (Lambdas)\n",
          "3. Matriz de Jacobi (Algebra Matricial)\n",
          "4. Matriz identidad")

def regressionsMenu():
    print("------------------------Menú Regresiones------------------------")
    print(" 1. Regresión Lineal\n",
          "2. Regresión Polinomial\n")

def interpolationsMenu():
    print("----------------------Menú Interpolaciones----------------------")
    print(" 1. Interpolación por Newton\n",
          "2. Interpolación por Lagrannge\n")

# Grafic Function
def graphic_function(f : callable, x_range = (-20, 20), step = 0.1):
    x = np.arange(x_range[0], x_range[1], step)
    y = []

    for val in x:
        try:
            y_val = f(val)
            y.append(y_val)
        except:
            y.append(float('nan'))

    plt.figure(figsize = (8,6))
    plt.plot(x, y)
    plt.title('Gráfico de la función')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.grid(True)
    plt.show()

# Function
def f(x):
    #return x**2
    #return math.exp(-x**2)
    #return math.cos(x)
    #return math.sin(x)
    return math.log(x)

mainMenu()
mainMenu = int(input("Qué acción quiere realizar?\n"))

if mainMenu == 1: # Derivatives and Find roots
    derivativesAndRootsMenu()
    menuRaizDeri = int(input("Qué acción quiere realizar?\n"))

    if menuRaizDeri== 1:
        valueX = float(input("En que valor de X quiere la aproximación?\n"))

        derivatives.mainDerivates(f, valueX)
        graphic_function(f)

    elif menuRaizDeri == 2:
        rootsMenu()
        rootMetod = int(input("Qué método desea usar?\n")) # Options to select metod

        # Fixed Point Metod
        if rootMetod == 1:
            x0 = 1
            fixPoint.fix_point(x0, f)
            graphic_function(f)

        # Bisection Metod
        elif rootMetod == 2:
            intervals = intervals.Search_Interval(f, -100, 100)
            if intervals:
                x0, x1 = intervals[-1]
                bisection(x0, x1, f)
            else:
                print("No se encontraron intervalos para aplicar el método de bisección.")
            graphic_function(f)

        # False Position Metod
        elif rootMetod == 3:
            intervals = intervals.Search_Interval(f, -100, 100)
            if intervals:
                x0, x1 = intervals[-1]
                falsPos.false_position(x0, x1, f)
            else:
                print("No se encontraron intervalos para aplicar el método de falsa posición.")
            graphic_function(f)

        # Newton-Raphson Metod
        elif rootMetod == 4:
            x0 = 0
            newRap.newton_raphson(x0, f)
            graphic_function(f)

        # Secant Metod
        elif rootMetod == 5:
            intervals = intervals.Search_Interval(f, -100, 100)
            if intervals:
                x0, x1 = intervals[-1]
                secant.sec(x0, x1, f)
            else:
                print("No se encontraron intervalos para aplicar el método de la secante.")
            graphic_function(f)

        # Multiple Roots
        elif rootMetod == 6:
            x0 = 0
            multipleRoot.mul_New_Rap(x0, f)
            graphic_function(f)

        else:
            print("La opción elegida no existe")

    else:
        print("La opción elegida no existe")

elif mainMenu == 2: # Integrals
    integralsMenu()
    integralMetod = int(input("Qué método desea usar?\n")) # Options to select metod

    # Riemann Metod
    if integralMetod == 1:
        Riemann.RiemannLeftExt(0, 3, 5, f)
        Riemann.RiemannMid(0, 3, 5, f)
        Riemann.RiemannRightExt(0, 3, 5, f)

    # Trapeze Metod
    elif integralMetod == 2:
        trapecio.trapeze(0, 20000, 5, f)
        trapecio.ntrapeze(0, 20000, 5, f)

    # Simpson Metod
    elif integralMetod == 3:
        Simpson.Simpson(0, 20000, 3, f)
        Simpson.nSimpson(0, 20000, 8, f)

    # Romberg Metod
    elif integralMetod == 4:
        Romberg.Romberg(0,20000,4,f)

    else:
        print("La opción elegida no existe")

elif mainMenu == 3: # Matrixs
    matrixsMenu()
    matrixMetod = int(input("Qué método desea usar?\n")) # Options to select metod

    # Gauss-Seidel Matrix
    if matrixMetod == 1:
        A = np.array([[6, 2, 1],
                      [2, 3, 1],
                      [2, 1, 4]])
        b = np.array(
            [1, 0, 0]
        )
        gaussSeidel.gaussSeidel(A, b, 10, 2, tol= 0.001)

    # Jacobi Matrix (With Lambdas)
    elif matrixMetod == 2:
        f1 = lambda x, y ,z: (4-y-2*z)/3 #3x, f1--> x
        f2 = lambda x, y ,z: (6-2*x-z)/1 #y, f2--> y
        f3 = lambda x, y ,z: (2-x-4*y)/6 #6z, f3--> z

        jacobi.jacobi(f1, f2, f3)

    # Jacobi Matrix (Matrix Algebra)
    elif matrixMetod == 3:
        A = np.array([[3, 1, 2],
                      [2, 1, 1],
                      [1, 4, 6]])
        b = np.array(
            [4, 6, 2]
        )
        jacobiMatriz.jacobiMatriz(A, b, 10, 2)

    #Matriz inversa e identidad
    elif matrixMetod == 4:

        A = np.array([[6, 2, 1, 0],
                      [2, 3, 1, 0],
                      [2, 1, 4, 2],
                      [1, 0, 0, 3]])

        B = np.linalg.inv(A)

        C = np.dot(A,B)

        print("Matriz")
        print(A)

        print("\nInversa")
        print(B)

        print("\nIdentidad")
        print (C)

    else:
        print("La opción elegida no existe")

elif mainMenu == 4: # Regressions
    regressionsMenu()
    regressionType = int(input("Qué tipo de regresión desea usar?\n")) # Options to select metod

    # Linear Regression
    if regressionType == 1:
        dataX = np.array([1.1, 2, 3.01, 4, 4.98, 6, 7.02, 8])
        dataY = np.array([2.5, 5.1, 8, 9.6, 10.8, 14, 15.1, 18])
        a0, a1 = linear.minSqr(dataX, dataY)

        # Graphic Linear Regression
        resol = 20
        xx = np.linspace(-1,12, resol)
        yy = a0 +a1*xx
        fig, ax = plt.subplots()
        ax.plot(xx, yy, 'b')
        ax.plot(dataX, dataY, 'o')
        plt.grid()
        plt.show()

    # Polinomial Regression
    elif regressionType == 2:
        dataX = np.array([1.1, 2.1, 3.01, 4, 4.98, 6.1, 7.02, 8, 9, 10])
        dataY = np.array([4.1, 5.2, 12.2, 19, 31, 43, 52, 71, 84.6, 104])
        m = 2; n = len(dataX)
        if n < m + 1:
            print('Pocos datos (n < m + 1)')
        else:
            a0, a1, a2 = polinomial.minSqr(dataX, dataY, m, n)

        # Graphic Polinomial Regression
        resol = 100
        xx = np.linspace(-2, 12, resol)
        yy = a0 + a1*xx + a2*xx**2

        fig, ax = plt.subplots()
        ax.plot(xx, yy, 'r')
        ax.plot(dataX, dataY, 'o')
        plt.grid()
        plt.show()

    else:
        print("La opción elegida no existe")

elif mainMenu == 5: # Interpolations
    interpolationsMenu()
    interpolMetod = int(input("Qué método desea usar?\n")) # Options to select metod

    # Interpolation for Newton
    if interpolMetod == 1:
        x = 2
        interpolation = newtonInter.Newton(1,5,5,f,x)
        error = abs(interpolation - f(2))/(f(2))
        print("Interpolación por Newton : ", interpolation,"\nError: ",error*100)

    elif interpolMetod == 2:
        x = 2
        interpolation = lagrannge.Lagrannge(1,5,5,f,x)
        error = abs(interpolation - f(2))/(f(2))
        print("Interpolación por Lagrannge : ", interpolation,"\nError: ",error*100)

    else:
        print("La opción elegida no existe")

else:
        print("La opción elegida no existe")


import time
from school import clear

class Gym:
    def __init__(self, user):
        class_prompt = input("Enter Gym (Y/N): ")
        if class_prompt.lower() == "y":
            in_gym(user)
        elif class_prompt.lower() == "n":
            out_gym(user)

def in_gym(user):
    clear()
    choice = input("Do you want to run on the treadmill? (Y/N) :")
    if choice.lower() == "n":
        choice = input("Would you like to leave the gym? (Y/N) :")
        if choice.lower() == "y":
            out_gym(user)
    elif choice.lower() == "y":
        print("You are on a treadmill. How long do you want to run? (in minutes) : ")
        tread_inp = input()
        treadmill(tread_inp, user)

def treadmill(tread_inp, user):
    print("{} was on the treadmill for {} minutes".format(user, tread_inp))
    time.sleep(1)

def out_gym(user):
    print("Left Gym. {} gets robbed. Type in y to continue.".format(user))
    class_choice = input()
    if class_choice.lower() == "y":
        print("You have encountered a robber running away with your wallet. What do you do? (Type help for options)")
        class_prompt = input()

    while class_prompt.lower() == "help":
        print("1: Run after him with your cane.")
        print("2: Call your parents and cry.")
        print("3: 'Tactically' *cough* advance and attempt to retrieve your wallet.")
        class_prompt = input()

    if class_prompt.lower() == "1":
        print("You ran after him but could not quite catch up to him. Instead, he was hit by a car.")
        time.sleep(1)
    elif class_prompt.lower() == "2":
        print("Your parents call you pathetic and tell you to man up and solve your own problems. As you are talking a helicopter rams into you.")
        time.sleep(1)
    elif class_prompt.lower() == "3":
        print("You got distracted by 'FREE RAM' and was hit by a car.")
        time.sleep(1)


#有序字典
from collections import OrderedDict

from pyexcel_xls import get_data

def readXlsAndXlsxFile(path):
    #创建一个有序的字典，excel中的数据都是有顺序的，所以用有序字典
    dic = OrderedDict()

    #抓取数据
    xdata = get_data(path) #OrderedDict([('Sheet1', [['年度', '总人口(万人)', '出生人口(万人)', '死亡人口(万人)'.....
    for sheet in xdata:
        dic[sheet] = xdata[sheet]

    return dic

path1 =r"C:\Users\Lenovo\Desktop\课程设计\aleardy deal data.xls"
path2 =r"C:\Users\Lenovo\Desktop\课程设计\2015data.xlsx"

readXlsAndXlsxFile(path2)
dic = readXlsAndXlsxFile(path2)
print(dic)

自动化办公/4、excel自动化办公/3、返回xls和xlsx文件内容.py


import random

import numpy as np
import math

# =============================================== HELPER FUNCTIONS =====================================================

def split_set(x, y, val_size):
    """ Split the given training set into a validation and training set. """
    val_indices = random.sample(range(len(y)), k=val_size)

    X_val = x[val_indices, :]
    Y_val = y[val_indices]

    y_temp = np.delete(y, val_indices, axis=0)
    X_temp = np.delete(x, val_indices, axis=0)

    return X_temp, y_temp, X_val, Y_val

def normalize_data(X_train, X_test):
    """ Normalize all feature values with respect to its maximum """
    maximums_1 = X_train.max(axis=0)
    maximums_2 = X_test.max(axis=0)
    maximums = np.stack((maximums_1, maximums_2), axis=1).max(axis=1)
    return X_train / maximums, X_test / maximums

def remove_outliers(X_train, y_train, std=3):
    """ Remove any outlier data points with features outside of std * the standard deviation """
    prior_mean = np.mean(X_train, axis=0)
    prior_std = np.std(X_train, axis=0)

    delete = []
    for data_point in range(X_train.shape[0]):
        for feature in range(X_train.shape[1]):
            if abs(X_train[data_point, feature] - prior_mean[feature]) > prior_std[feature]*std:
                delete.append(data_point)

    return np.delete(X_train, delete, axis=0), np.delete(y_train, delete, axis=0)

def score(X_val, y_val, X_train, y_train):
    """ RMSE scoring function """
    return math.sqrt(1/y_val.shape[0] * sum((y_val - get_bayes_prediction(X_train, y_train, X_val))**2))

def get_bayes_prediction(X_temp, y_temp, X_val):
    """ Standard Bayesian regression model """

    # Compute the prior standard deviation and covariance
    prior_std = np.std(X_temp, axis=0)
    prior_cov = prior_std * np.eye(prior_std.shape[0])

    # Compute the weights
    XX = np.dot(X_temp.T, X_temp)
    XXinv = np.linalg.inv(XX)
    Xt = np.dot(X_temp.T, y_temp)
    w = np.dot(XXinv, Xt)

    # Get the sigma squared value based on the weights
    tXw = y_temp - np.dot(X_temp, w)
    sig_sq = np.dot(tXw.T, tXw) / len(y_temp)

    # Find the posterior mean and covariance
    posterior_cov = np.linalg.inv((1.0/sig_sq)*np.dot(X_temp.T,X_temp) + np.linalg.inv(prior_cov))
    posterior_mu = ((1.0/sig_sq))*np.dot(posterior_cov,np.dot(X_temp.T,y_temp))

    # Sample from this mean and return the predictions
    n_samps = 20000
    w_samples = np.random.multivariate_normal(posterior_mu.flatten(),posterior_cov,n_samps)
    predictions = []
    for w_s in w_samples:
        predictions.append(np.dot(w_s.T,X_val.T))

    return np.mean(predictions, axis=0)

# =============================================== CODE =================================================================

# Set if the model is being evaluated
evaluate = False
VAL_EPOCHS = 300

# Load training and testing data
X_train = np.loadtxt('X_train.csv', delimiter=',', skiprows=1)
X_test = np.loadtxt('X_test.csv', delimiter=',', skiprows=1)
y_train = np.loadtxt('y_train.csv', delimiter=',', skiprows=1)[:,1]

# Discard noisy features and datapoints
X_train = np.delete(X_train, [2,5], axis=1)
X_test = np.delete(X_test, [2,5], axis=1)
X_train, y_train = remove_outliers(X_train, y_train, 3)

# Evaluate the model
if evaluate:
    y_val = 0
    for i in range(VAL_EPOCHS):
        X_temp, y_temp, X_val, Y_val = split_set(X_train, y_train, 17)
        y_val += score(X_val, Y_val, X_temp, y_temp)

    print(y_val/VAL_EPOCHS)

# Fit the model and get predictions
y_pred = get_bayes_prediction(X_train, y_train, X_test)

# Arrange answer in two columns. First column (with header "Id") is an
# enumeration from 0 to n-1, where n is the number of test points. Second
# column (with header "EpiOrStroma" is the predictions.
test_header = "Id,PRP"
n_points = X_test.shape[0]
y_pred_pp = np.ones((n_points, 2))
y_pred_pp[:, 0] = range(n_points)
y_pred_pp[:, 1] = y_pred
np.savetxt('my_submission_bayes.csv', y_pred_pp, fmt='%d,%f', delimiter=",",
           header=test_header, comments="")

# Note: fmt='%d' denotes that all values should be formatted as integers which
# is appropriate for classification. For regression, where the second column
# should be floating point, use fmt='%d,%f'.


#! python3

import os
import pandas as pd
import numpy as np

tsv, prefix, threshold = os.sys.args[1], os.sys.args[2], float(os.sys.args[3])

expr = pd.read_csv(tsv, sep='\t', index_col=0)

####
top = []

for i in expr.columns:
    x = expr.loc[:, i].nlargest(10)
    top.append (list (x.index.values))
    top.append (x.tolist())

topdf = pd.DataFrame.from_records(top)

topdf.index = sum([[i + "__gene", i + "__value"] for i in expr.columns], [])
topdf.columns = [ "top_" + str(i) for i in range(1,11)]

topdf.to_csv (prefix + ".top10.tsv", sep="\t")

####
expr[expr < threshold] = pd.np.nan

d = expr.describe().T
d["median"] = expr.apply (lambda x: np.median(x[~x.isnull()]), axis=0)[d.index]
d = round(d, 2)

d.loc[:, "count"] = d.loc[:, "count"].astype(int)
d.columns = ["count", "mean", "std", "min", "Q1", "Q2", "Q3", "max", "median"]
d = d.loc[:, ["count", "mean", "median", "std", "min", "Q1", "Q2", "Q3", "max"]]

d.to_csv(prefix + ".summary.tsv", sep="\t")


import argparse
import sys
import datetime

from lib.data_holder import *
from lib.models import *
from lib.extremums import *

###
### GLOBALS
###

CACHE_DIR = 'cache'

data_holder = None

def lazy_data_holder():
    global data_holder
    if data_holder is None:
        data_holder = DataHolder()

    return data_holder

###
### PLOT POINT CLOUD
###

def plot_point_cloud():
    """Plot point cloud for protein-ligand pair."""

    options_parser = argparse.ArgumentParser(add_help=True)
    options_parser.add_argument('--protein', help='protein index', default=0, type=int)
    options_parser.add_argument('--ligand', help='ligand index', default=5, type=int)
    options = options_parser.parse_args()

    cache_file_name = '{}/{}_{}.points'.format(CACHE_DIR, options.protein, options.ligand)
    arr = create_cached(cache_file_name, lambda: lazy_data_holder().get_point_array(options.protein, options.ligand))
    print('Have {} points for pair ({}, {})'.format(len(arr.spherical), options.protein, options.ligand))

    subarr = arr.subarray_for_distance_range(4.5, 5.5)
    print(len(subarr.spherical))

    scatter_plot_point_array(subarr)
    plt.savefig('test.png')

###
### PLOT DENSITY
###

from matplotlib.backends.backend_pdf import PdfPages

def plot_density_with_params(protein, ligand, r_from, r_to):
    cache_file_name = '{}/{}_{}.points'.format(CACHE_DIR, protein, ligand)
    arr = create_cached(cache_file_name, lambda: lazy_data_holder().get_point_array(protein, ligand))
    print('Have {} points for pair ({}, {})'.format(len(arr.spherical), protein, ligand))

    # 0, 5, 4.75-5.25 ok
    # 0, 6, 4.75-5.25 ok
    # 1, 1, 4.25-5.75 ok

    pdf_output = PdfPages('plot_{}_{}.pdf'.format(protein, ligand))

    #subarr = arr.subarray_for_distance_range(4.75, 5.25)
    subarr = arr.subarray_for_distance_range(r_from, r_to)
    print(len(subarr.spherical))

    scatter_plot_point_array(subarr)
    plt.xlabel(r'$\varphi$')
    plt.ylabel(r'$\theta$')
    plt.savefig('plots/points.png')
    plt.title('Source points')
    pdf_output.savefig(plt.gcf())
    plt.close()

    def make_model_density_plot(model, output_file, title):
        dist_point_array = get_point_array_for_distance(5)
        density = model.get_density_for_points(dist_point_array)
        density = density.reshape(100, 100)

        imshow_plot_density(density)

        extremums = kernel_extremums_2d(density)
        plot_extremums(extremums, density)

        plt.xlabel(r'$\varphi$')
        plt.ylabel(r'$\theta$')
        plt.title(title)
        plt.savefig(output_file)
        pdf_output.savefig(plt.gcf())
        plt.close()

    if True:
        kernel_density_model = KernelDensityModel(subarr)
        make_model_density_plot(kernel_density_model, 'plots/density_kernel.png', 'Parzen')

    if True:
        histogram_density_model = HistogramDensityModel(subarr, r_steps=3, theta_steps=25, phi_steps=25)
        make_model_density_plot(histogram_density_model, 'plots/density_histogram.png', 'Histogram')

    if True:
        gm_density_model = GaussianMixtureDensityModel(subarr)
        make_model_density_plot(gm_density_model, 'plots/density_gmm.png', 'Gaussian mixture')

    def compare_models(models):
        dist_point_array = union_point_arrays([get_point_array_for_distance(5), get_point_array_for_distance(10), get_point_array_for_distance(15)])
        volumes = get_point_array_approx_volumes(dist_point_array, n_r=3, n_phi=100, n_theta=100)

        densities = [model.get_density_for_points(dist_point_array) for model in models]

        def get_average_diff_for_pair(i, j):
            # MAPE
            #return (np.abs(densities[i] - densities[j]) / (np.minimum(densities[i], densities[j]) + 1e-6) * volumes).sum() / volumes.sum()

            # MSE
            return ((densities[i] - densities[j]) ** 2 * volumes).sum() / volumes.sum()

        for i in range(len(models)):
            for j in range(len(models)):
                print('pair {} - {}: {}'.format(i, j, get_average_diff_for_pair(i, j)))

    compare_models([kernel_density_model, histogram_density_model, gm_density_model])
    pdf_output.close()

    if False:
        def make_model_density_plots_multiple(model, output_file):
            dist_point_array = get_point_array_for_distance(5)
            densities = model.get_density_for_points(dist_point_array)

            for index, density in enumerate(densities):
                imshow_plot_density(density.reshape(100, 100))
                plt.savefig(output_file.format(index))
                plt.close()

        N = 10
        model = NeuralNetworkModel(subarr, num_iterations=N)
        for t in range(300):
            make_model_density_plots_multiple(model, 'plots/nn/density_nn_{}_' + str(model.total_iterations) + '.png')
            model.optimize_for(N)

def plot_density():
    """Plot point cloud for protein-ligand pair."""

    options_parser = argparse.ArgumentParser(add_help=True)
    options_parser.add_argument('--protein', help='protein index', default=1, type=int)
    options_parser.add_argument('--ligand', help='ligand index', default=1, type=int)
    options = options_parser.parse_args()

    plot_density_with_params(options.protein, options.ligand, 4.25, 5.75)

def plot_density_preset():
    plot_density_with_params(0, 5, 4.75, 5.25)
    plot_density_with_params(0, 0, 4.75, 5.25)
    plot_density_with_params(1, 1, 4.25, 5.75)

###
### MOD CHOOSER
###

def mod_chooser_main(available_modes):
    available_mode_names = [
        mode.__name__ for mode in available_modes
    ]

    is_known_mode = len(sys.argv) >= 2 and sys.argv[1] in available_mode_names
    need_help = len(sys.argv) >= 2 and sys.argv[1] == '--help'

    if not need_help and not is_known_mode and len(sys.argv) >= 2:
        print('Unknown mode: "{}"\n'.format(sys.argv[1], available_mode_names))

    if not is_known_mode or need_help:
        print('Usage: {} <mode> <options>'.format(sys.argv[0]))
        print('')
        print('Available modes:')
        for mode in available_modes:
            print('{} - {}'.format(mode.__name__, mode.__doc__))
        sys.exit(1)

    mode = sys.argv[1]
    mode_func = available_modes[available_mode_names.index(mode)]
    del sys.argv[1]
    mode_func()

###
### MAIN
###

def main():
    available_modes = [
        plot_point_cloud,
        plot_density,
        plot_density_preset
    ]

    mod_chooser_main(available_modes)

if __name__ == '__main__':
    main()


import cv2
from mtcnn import MTCNN

capture = cv2.VideoCapture(0)

detector = MTCNN()

while True:

    ret, frame = capture.read()
    faces = detector.detect_faces(frame)

    for single_faces in faces:
        x, y, width, height = single_faces["box"]

        left_eyeX, left_eyeY = single_faces["keypoints"]["left_eye"]
        right_eyeX, right_eyeY = single_faces["keypoints"]["right_eye"]
        noseX, noseY = single_faces["keypoints"]["nose"]
        mouth_leftX, mouth_leftY = single_faces["keypoints"]["mouth_left"]
        mouth_rightX, mouth_rightY = single_faces["keypoints"]["mouth_right"]

        cv2.rectangle(frame, pt1=(x, y), pt2=(x + width, y + height), color=(255, 0, 0), thickness=3)
        cv2.circle(frame, center=(left_eyeX, left_eyeY), color=(255, 0, 0), thickness=3, radius=2)
        cv2.circle(frame, center=(right_eyeX, right_eyeY), color=(255, 0, 0), thickness=3, radius=2)
        cv2.circle(frame, center=(noseX, noseY), color=(255, 0, 0), thickness=3, radius=2)
        cv2.circle(frame, center=(mouth_leftX, mouth_leftY), color=(255, 0, 0), thickness=3, radius=2)
        cv2.circle(frame, center=(mouth_rightX, mouth_rightY), color=(255, 0, 0), thickness=3, radius=2)
        cv2.rectangle(frame, pt1=(x, y), pt2=(x + width, y + height), color=(255, 0, 0), thickness=3)

    cv2.imshow("front_cam", frame)

    if cv2.waitKey(1) & 0xFF == ord("b"):
        break

cv2.destroyAllWindows()


import pickle
from flask import Flask, request, app, jsonify,url_for, render_template
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler

app=Flask(__name__)
## load the model
regmodel=pickle.load(open('regmodel.pkl','rb'))
scalar=pickle.load(open('scaling.pkl','rb'))
@app.route('/')
def home():
    return render_template('home.html')
@app.route('/predict_api',methods=['POST'])
def predict_api():
    data=request.json['data']
    print(data)
    ## the data values receieved must be transformed into a list which is then changed into np array
    ## the numpy array is then reshaped to a single row, multiple columns(1,13)
    print(np.array(list(data.values())).reshape(1,-1))
    new_data=scalar.transform(np.array(list(data.values())).reshape(1,-1))
    output=regmodel.predict(new_data)
    # since this is a two dimensional array, we need output[0]
    print(output[0])
    return jsonify(output[0])

if __name__=="__main__":
    app.run(debug=True)


from flask import render_template,request,redirect
from models import *
from forms import CardForm, StoryForm, NewsForm
from upload import save_file
from app import app
from extensions import db
import requests
import xml.etree.ElementTree as ET

@app.route('/')
def index():
    story = Story.query.all()
    card = Cards.query.all()
    currency = ForeignCurrency.query.all()
    news = News.query.all()
    return render_template('index.html',story=story,cards=card,currency=currency,news=news)

@app.route('/cards/')
def card():
    card = Cards.query.all()
    story = Story.query.all()
    return render_template('card.html',cards=card,story=story, page_name='cards')

@app.route("/loans/")
def credits():
    card = Cards.query.all()
    story = Story.query.all()
    return render_template('kredit.html',page_name='credits',cards=card,story=story,)

@app.route('/add-card/', methods=['GET', 'POST'])
def addcard():
    form = CardForm()

    if request.method == 'POST':
        print('post')
        if form.validate:
            card_title = form.title.data
            card_desc = form.description.data
            card_term = form.term.data
            card_currency = form.currency.data
            card_cashback = form.cashback.data
            image_data = form.image.data
            print(image_data)
            card_image = save_file(image_data)
            card = Cards(card_title,card_desc,card_term,card_currency,card_cashback,card_image)
            card.save()
            print(card)
            return redirect('/cards')
    return render_template('card_add.html', form=form)

@app.route('/add-story/', methods=['GET', 'POST'])
def addStory():
    form = StoryForm()

    if request.method == 'POST':
        if form.validate_on_submit():
            story_tip = form.tip.data
            story_description = form.description.data
            story_image = save_file(form.image.data)
            story_color = form.color.data
            storys = Story(story_tip,story_description,story_image,story_color)
            storys.save()
            return redirect('/cards')
    return render_template('story_add.html', form=form)

@app.route('/valyuta/')
def loadRSS():
    url = f'https://www.cbar.az/currencies/{datetime.datetime.now().strftime("%d.%m.%Y")}.xml'
    response = requests.get(url, stream=True)
    tree = ET.fromstring(response.content)
    dom = tree.findall('ValType')

    db.session.query(ForeignCurrency).delete()
    db.session.commit()

    for attr_type in dom:
        name = attr_type.get('Type')
        if name == 'Xarici valyutalar':
            valute = attr_type.findall('Valute')
            for code in valute:
                code_name = code.get('Code')
                if code_name == 'USD' or code_name == "AZN" or code_name == "EUR":
                    nominal = code.find('Nominal').text
                    name = code.find('Name').text
                    course = code.find('Value').text
                    currency = ForeignCurrency(code_name, nominal, name, course)
                    currency.save()
    return redirect('/cards')

@app.route('/add-news/', methods=['GET', 'POST'])
def addnews():
    form = NewsForm()

    if request.method == 'POST':
        if form.validate_on_submit():
            news_title = form.title.data
            news = News(news_title)
            news.save()
    return render_template('news_add.html', form=form)


"""
50% - recursiva
"""

def saque(mapa):
    return saqueaux(mapa,(0,0),0)

def saqueaux(mapa,coords,valor):
    if mapa[coords[1]][coords[0]].isdigit():
        valor+= int(mapa[coords[1]][coords[0]])
    if coords[0] == len(mapa[0])-1 and coords[1] == len(mapa)-1:
        return valor
    if mapa[coords[1]][coords[0]] == '#':
        return 0
    if coords[0] >= len(mapa[0])-1:
        valor = max(valor,saqueaux(mapa,(coords[0],coords[1]+1),valor))
    elif coords[1] >= len(mapa)-1:
        valor = max(valor,saqueaux(mapa,(coords[0]+1,coords[1]),valor))
    else:
        valor = max(valor,saqueaux(mapa,(coords[0]+1,coords[1]),valor),saqueaux(mapa,(coords[0],coords[1]+1),valor))
    return valor

"""
80% - dinâmica
"""

def saque(mapa):
    return saqueaux(mapa,(0,0),0)

def saqueaux(mapa,coords,valor):
    if mapa == []:
        return 0
    while coords != (len(mapa[0])-1,len(mapa)-1):
        if mapa[coords[1]][coords[0]].isdigit():
            valor+= int(mapa[coords[1]][coords[0]])
        countx = 0
        county = 0
        for x in range(coords[0]+1,len(mapa[0])):
            if mapa[coords[1]][x].isdigit():
                countx+=int(mapa[coords[1]][x])
        for y in range(coords[1]+1,len(mapa)):
            if mapa[y][coords[1]].isdigit():
                county+=int(mapa[y][coords[1]])
        if (countx > county and coords[0] != len(mapa[0])-1) or coords[1] == len(mapa)-1:
            coords = (coords[0]+1,coords[1])
        elif (countx <= county and coords[1] != len(mapa)-1) or coords[0] == len(mapa[0])-1:
            coords = (coords[0],coords[1]+1)
    if mapa[coords[1]][coords[0]].isdigit():
            valor+= int(mapa[coords[1]][coords[0]])
    return valor


from mirl.agents.td3_agent import TD3Agent
import mirl.torch_modules.utils as ptu
import torch.nn.functional as F
import copy
from mirl.utils.logger import logger

class TD3BCAgent(TD3Agent):
    def __init__(
        self,
        env,
        policy,
        qf,
        qf_target,
        pool=None,
        normalize_obs=True,
        bc_alpha=2.5,
        **td3_kwargs
    ):
        super().__init__(
            env,
            policy,
            qf,
            qf_target,
            **td3_kwargs
        )
        self.bc_alpha = bc_alpha
        self.normalize_obs = normalize_obs
        if normalize_obs:
            assert pool is not None
            assert pool.compute_mean_std
            self.obs_mean, self.obs_std = pool.get_mean_std()['observations']
            self.obs_mean = ptu.from_numpy(self.obs_mean)
            self.obs_std = ptu.from_numpy(self.obs_std)

    def process_obs(self, obs):
        if self.normalize_obs:
            obs = obs = (obs-self.obs_mean)/(self.obs_std+1e-6)
        return obs

    def step_explore(self, o, **kwargs):
        raise NotImplementedError

    def step_exploit(self, o, **kwargs):
        with self.policy.deterministic_(True):
            o = ptu.from_numpy(o)
            o = self.process_obs(o)
            a,_ = self.policy.action(o, **kwargs)
            a = ptu.get_numpy(a)
        return a, {}

    def compute_policy_loss(
        self, obs, new_action,
        origin_action, v_pi_kwargs={}
    ):
        q_new_action, _ = self.qf.value(
            obs,
            new_action,
            return_ensemble=False,
            **v_pi_kwargs
        )
        q_pi_mean = q_new_action.mean()
        policy_loss = -q_pi_mean
        policy_info = self._log_policy_info(
            new_action, policy_loss, q_pi_mean)
        ### for td3bc ###
        bc_lambda = self.bc_alpha / q_pi_mean.detach().abs().mean()
        bc_loss = F.mse_loss(new_action, origin_action)
        policy_loss = policy_loss*bc_lambda + bc_loss
        if self._log_tb_or_not():
            logger.tb_add_scalar("policy/bc_loss", bc_loss, self.num_train_steps)
        ###### end ######
        return policy_loss, policy_info

    def train_from_torch_batch(self, batch):
        rewards = batch['rewards']
        terminals = batch['terminals']
        obs = batch['observations']
        actions = batch['actions']
        next_obs = batch['next_observations']
        obs = self.process_obs(obs)
        next_obs = self.process_obs(next_obs)
        self.log_batch(rewards, terminals)
        ################
        # update critic #
        ################
        q_target = self.compute_q_target(next_obs, rewards, terminals, self.next_v_pi_kwargs)
        qf_loss, train_qf_info = self.compute_qf_loss(obs, actions, q_target)
        self.qf_optimizer.zero_grad()
        qf_loss.backward()
        self.log_critic_grad_norm()
        self.qf_optimizer.step()
        if self.num_train_steps % self.target_update_freq == 0:
            self._update_target(self.soft_target_tau)
        self.train_info.update(train_qf_info)
        ################
        # update actor #
        ################
        if self.num_train_steps % self.policy_update_freq == 0:
            new_action, action_info = self.policy.action(obs, **self.current_sample_kwargs)
            policy_loss, train_policy_info = self.compute_policy_loss(
                obs,
                new_action,
                actions,  #NOTE: origin action for bc
                self.current_v_pi_kwargs
            )
            self.policy_optimizer.zero_grad()
            policy_loss.backward()
            self.policy_optimizer.step()
            self.train_info.update(train_policy_info)
        #####################
        # update statistics #
        #####################
        if self._need_to_update_eval_statistics:
            self._need_to_update_eval_statistics = False
            self.eval_statistics.update(self.train_info)
        self.log_train_info()
        self.num_train_steps += 1
        return copy.deepcopy(self.train_info)


'''
Yangbot #2 - elo ~ 1800 - 2000

FEATURE LIST:
- UCI interface                         [INCOMPLETE]
- opening book                          [OK]
- negamax                               [OK]
- quiscence search                      [OK]
- killer moves                          [TESTING]
- move ordering                         [TESTING]
    > MVV LVA                           [OK]
- PST                                   [OK]
- PV                                    [OK]
- evaluation                            [TESTING]
    > PeSTO evaluation                  [OK]
    > Incremental material counting     [INCOMPLETE]
- iterative deepening                   [OK]
- null moves                            [TESTING]
- late move reduction                   [TESTING]
- transposition tables                  [GOOD]
- mate distance pruning                 [TESTING]
- razoring                              [INCOMPLETE]
- check extensions                      [INCOMPLETE]

CHANGE LOGS:

- added mate distance pruning - however it seems to mess up move ordering so i made alpha and beta temporary variables
- moved outcomes from evaluation to search
- ~piece has more priority than psqt~ Tests found that equal play is superior

weaknesses:
- prioritises trading when it is better to attack the king
- increase speed and depth ~ try to reach depth, avg depth is 5 atm
    MAYBE USE CYTHON?
    MAYBE PRUNE MORE BRANCHES (killer huristics not confirmed to work)
    TEST NEW HASH TABLES?

'''

import chess
import chess.polyglot
from chessUtil import Piece_Square_Tables
import random
import cProfile
import pstats
import settings
import threading
import requests
import time

class HashEntry:

    NONE = 0
    EXACT = 1
    BETA = 2
    ALPHA = 3

    def __init__(self):
        self.key = ""
        self.move = None
        self.score = 0
        self.depth = 0
        self.flag = HashEntry.NONE

# bot class
class BOT():

    INFINITY = 137000000
    hashes = {}
    hash_table = {}
    pv_table = {}
    contempt = [5, 2.5, 0]

    def __init__(self):

        self.saved_moved = ""
        self.results = []

        self.clear_search_params()

        self.Mvv_Lva_Victim_scores = {
            "p" : 100,
            "n" : 200,
            "b" : 300,
            "r" : 400,
            "q" : 500,
            "k" : 600
        }

        # init mvv lva
        #self.Mvv_Lva_Scores = {}
        #for attacker in self.Mvv_Lva_Victim_scores.keys():
        #    self.Mvv_Lva_Scores[attacker] = {}
        #    for victim in self.Mvv_Lva_Victim_scores.keys():
        #        self.Mvv_Lva_Scores[attacker][victim] = int(self.Mvv_Lva_Victim_scores[victim] + 6  - (self.Mvv_Lva_Victim_scores[attacker] / 100)) + 1000000

    ############################## UTILITY FUNCTIONS ##############################

    def clear_search_params(self):
        self.nodes = 0
        #self.pv_table = {}
        self.killers = {0: {}, 1: {}}

        for i in range(0, 400):
            self.killers[0][i] = None
            self.killers[1][i] = None

        self.search_history = {}

        for i in range(0, 64):
            self.search_history[i] = {}
            for ii in range(0, 64):
                self.search_history[i][ii] = 0

    def get_opening_move(self, board):

        max_weight = 0
        max_move = None

        found_moves = {}

        with chess.polyglot.open_reader("chessAI/data/polyglot/performance.bin") as reader:
            for entry in reader.find_all(board):

                if not entry.weight in found_moves:
                    found_moves[entry.weight] = []

                found_moves[entry.weight].append(entry.move)

                if entry.weight > max_weight:
                    max_weight = entry.weight
                    max_move = entry.move

        # shuffle out of best moves

        if max_move is None:
            return None

        best_moves = found_moves[max_weight]

        random.shuffle(best_moves)

        return best_moves[0]

    def getPhase(self, board):
        l = len(board.piece_map())

        if 20 <= l <= 32:
            return 0
        elif 10 <= l < 20:
            return 1
        else:
            return 0

    def store_hash(self, pos, move, score, flag, depth):
        he = HashEntry()
        he.key = pos
        he.move = move
        he.score = score
        he.flag = flag
        he.depth = depth

        BOT.hash_table[pos] = he

    def get_hash(self, pos):

        if pos in BOT.hash_table:
            return BOT.hash_table[pos]

        return None

    def store_pvline(self, position_hash, move):
        BOT.pv_table[position_hash] = move

    def get_pvline(self, position_hash, turn):
        if position_hash not in BOT.pv_table:
            return None

        return BOT.pv_table[position_hash]

    def retrieve_pvline(self, board):

        pv_line = list()

        _b = board.copy()

        for _ in range(10000):
            #h = self.cu.get_board_hash_pychess(_b)
            h = _b.fen()
            hash_entry = self.get_hash(h)

            if hash_entry is not None and hash_entry.flag == HashEntry.EXACT:
                pv_line.append(hash_entry.move)
                _b.push(hash_entry.move)
            else:
                break

        return pv_line

    def get_pv_line_san(self, board, line):
        san_list = list()
        b = board.copy()

        for move in line:
            san_list.append(b.san(move))
            b.push(move)

        return san_list

    ############################ EVALUATION AND SEARCH ################################
    @staticmethod
    def evaluate_EVAL(board):

        #if board.is_checkmate():
        #    return -1 * (BOT.INFINITY - board.ply())
        #elif board.is_stalemate() or board.is_repetition() or board.is_insufficient_material():
        #   return 0

        mg, eg, phase = 0, 0, 0

        #bq, wq = (len(board.pieces(chess.QUEEN, chess.BLACK)) > 0), (len(board.pieces(chess.QUEEN, chess.WHITE)) > 0)

        pieces = board.piece_map()

        for pos, _piece in board.piece_map().items():
            piece = str(_piece)

            # piece values have more priority
            mg += ((Piece_Square_Tables.mg_values[piece.lower()] * (-1 if piece.islower() else 1))) / 100
            eg += ((Piece_Square_Tables.mg_values[piece.lower()] * (-1 if piece.islower() else 1))) / 100

            if piece.islower(): # black
                #if wq == False and piece.lower() == "k": # no restrictions if white queen of the table
                #    continue
                mg -= Piece_Square_Tables.PSQT_MG[piece][pos] / 100
                eg -= Piece_Square_Tables.PSQT_EG[piece][pos] / 100
            else:
                #if bq == False and piece.lower() == "k": # no restrictions if black queen of the table
                #    continue
                mg += Piece_Square_Tables.PSQT_MG[piece.lower()][Piece_Square_Tables.mirror_table[pos]] / 100
                eg += Piece_Square_Tables.PSQT_EG[piece.lower()][Piece_Square_Tables.mirror_table[pos]] / 100

            phase += Piece_Square_Tables.phase[piece.lower()]

        mobility = 0
        if not board.is_check():
            num_1 = board.legal_moves.count()
            board.push(chess.Move.null())
            num_2 = board.legal_moves.count()
            board.pop()
            if board.turn == chess.WHITE:
                mobility = num_1 - num_2
            else:
                mobility = num_2 - num_1

        mgp = min(phase, 24)
        egp = 24-mgp

        #evadj = 0

        '''
        if (len(board.pieces(chess.BISHOP, chess.BLACK)) >= 2):
            evadj -= 500
        if (len(board.pieces(chess.BISHOP, chess.WHITE)) >= 2):
            evadj += 500
        '''
        #sq = (mg * (256 - phase)) + ((eg * phase) / 256)
        sq = (mg * mgp + eg * egp) / 24 # idk about the legal moves

        white_pawns, black_pawns = [], []

        for k, v in pieces.items():
            if str(v) == 'P':
                white_pawns.append(k)
            elif str(v) == 'p':
                black_pawns.append(k)

        doubled_white = 0
        doubled_black = 0
        isolated_white = 0
        isolated_black = 0
        blocked_white = 0
        blocked_black = 0
        for pawn in white_pawns:
            # check for each pawn
            file_number = pawn % 8
            # is pawn blocked ( for white +8 for black -8)
            if pawn + 8 < 64 and board.piece_at(pawn + 8) is not None:
                blocked_white += 1
            has_left_neighbor = False
            has_right_neighbor = False
            if pawn % 8 == 0:
                has_left_neighbor = True
            if pawn % 8 == 7:
                has_right_neighbor = True
            # is it doubled, is another pawn on the file
            for other_pawn in white_pawns:
                if other_pawn != pawn and abs(pawn - other_pawn) % 8 == 0:
                    doubled_white += 1
                # isolation check left file ( if exists)
                other_file = other_pawn % 8
                if file_number - other_file == 1:
                    has_left_neighbor = True
                if file_number - other_file == -1:
                    has_right_neighbor = True
            if not has_left_neighbor and not has_right_neighbor:
                isolated_white += 1
        # check for black pawns
        for pawn in black_pawns:
            # check for each pawn
            file_number = pawn % 8
            # is pawn blocked ( for white +8 for black -8)
            if pawn - 8 >= 0 and board.piece_at(pawn - 8) is not None:
                blocked_black += 1
            has_left_neighbor = False
            has_right_neighbor = False
            if pawn % 8 == 0:
                has_left_neighbor = True
            if pawn % 8 == 7:
                has_right_neighbor = True
            # is it doubled, is another pawn on the file
            for other_pawn in black_pawns:
                if other_pawn != pawn and abs(pawn - other_pawn) % 8 == 0:
                    doubled_black += 1
                # isolation check left file ( if exists)
                other_file = other_pawn % 8
                if file_number - other_file == 1:
                    has_left_neighbor = True
                if file_number - other_file == -1:
                    has_right_neighbor = True
            if not has_left_neighbor and not has_right_neighbor:
                isolated_black += 1
        pawn_influence = ((doubled_white - doubled_black) + (isolated_white - isolated_black) + (blocked_white - blocked_black)) * 0.1
        #print(pawn_influence)
        sq -= pawn_influence

        return (sq + 0.1*mobility) * (-1 if board.turn == chess.BLACK else 1)

    # static exchange evaluation
    def static_exchange_evaluation(self, board, moveSquare):
        value = 0

        attackers = list(board.attackers(board.turn, moveSquare)) # get attackers to the square
        if attackers: # if we have attackers
            piece_types = [board.piece_type_at(x) for x in attackers] # get the piece type of these attackers
            lowest = min(piece_types, key=lambda x: Piece_Square_Tables.values[x]) # get lowest piece
            attacker_index = piece_types.index(lowest) # get index of lowest piece

            try:
                capture = Piece_Square_Tables.values[board.piece_type_at(moveSquare)]
            except:
                capture = 100 # pawn

            try:
                next_move = board.find_move(attackers[attacker_index], moveSquare) # make capture move
                board.push(next_move) # push
                value = max(0, capture - self.static_exchange_evaluation(board, moveSquare))
                board.pop()
            except:
                value = 0

        return value

    # see capture
    def start_SEE(self, board, move): # push capture move
        value = 0
        m = move.to_square
        try:
            capture = Piece_Square_Tables.values[board.piece_type_at(m)]
        except:
            capture = 100 # pawn
        board.push(move)
        value = max(0, capture - self.static_exchange_evaluation(board, m))
        board.pop()
        return value

    # MOVE ORDERING
    def orderM(self, board, unscored_moves, pv_move, Quiscence):
        scored_moves = {}

        if Quiscence:
            for move in unscored_moves:

                if pv_move is not None and move == pv_move:
                    scored_moves[move] = 20000000
                    continue

                ## all non captures are at the end of the list
                if board.is_capture(move):
                    ## all captures have to be scored and thus sorted
                    #attacker = board.piece_at(move.from_square).symbol().lower()
                    #try:
                    #    victim = board.piece_at(move.to_square).symbol().lower()
                    #except:
                    #    victim = 'p'

                    #scored_moves[move] = self.Mvv_Lva_Scores[attacker][victim]
                    scored_moves[move] = self.start_SEE(board, move)
        else:
            for move in unscored_moves:
                ## all non captures are at the end of the list
                if not board.is_capture(move):

                    ### check if non capture is killer move 1st order
                    if self.killers[0][board.ply()] == move:
                        scored_moves[move] = 900000
                    elif self.killers[1][board.ply()] == move: # ????
                        scored_moves[move] = 800000
                    else:
                        scored_moves[move] = self.search_history[move.from_square][move.to_square]

                    # pawn promotions
                    if move.promotion:
                        scored_moves[move] += Piece_Square_Tables.values[move.promotion]
                else:
                    ## all captures have to be scored and thus sorted
                    #attacker = board.piece_at(move.from_square).symbol().lower()
                    #try:
                    #    victim = board.piece_at(move.to_square).symbol().lower()
                    #except:
                    #    victim = 'p'

                    #scored_moves[move] = self.Mvv_Lva_Scores[attacker][victim]
                    scored_moves[move] = self.start_SEE(board, move)

        ordered_move_list = sorted(scored_moves, key=scored_moves.get)
        ordered_move_list.reverse()
        return ordered_move_list

    @staticmethod
    def evaluate(board):
        return BOT.evaluate_EVAL(board)

    # QUISCENCE SEARCH
    def SearchAllCaptures(self, board, alpha, beta):

        old_a = alpha
        best_move = None
        h = board.fen()
        pv_move = self.get_pvline(h, board.turn)
        self.nodes += 1

        eval = self.evaluate(board) # change this (wtm = true for odd, false for even)
        if (eval >= beta):
            return beta

        # delta pruning

        if eval < alpha-9:
            return alpha

        alpha = max(alpha, eval)

        l = board.legal_moves

        captures = self.orderM(board, l, pv_move, True)
        #print(captures)

        for m in captures:

            board.push(m)
            eval = -self.SearchAllCaptures(board, -beta, -alpha) # this line slow asf
            board.pop()

            if eval > alpha:
                if eval >= beta:
                    return beta
                alpha = eval
                best_move = m

        if alpha != old_a:
            self.store_pvline(h, best_move)

        return alpha

    def startSearch(self, board):
        best_move_FOUND = None
        self.saved_moved = None
        self.clear_search_params()

        entry_time = time.time()

        for depth in range(1, settings.DEPTH):
            if time.time() - entry_time > settings.MAXT/(depth/4): # dont bother starting another depth if we ate through most our time
                print("TIMEOUT")
                break
            self.nodes = 0
            self.fh = 0
            self.fhf = 0
            currentItSearchDepth = depth + 1
            _start = time.time()

            best_score = self.search(board, depth, -BOT.INFINITY, BOT.INFINITY, depth, True)

            pvM = self.retrieve_pvline(board)
            if len(pvM) > 0:
                best_move_FOUND = pvM[0]
            else:
                if depth == settings.DEPTH-1:
                    best_score = self.search(board, depth+1, -BOT.INFINITY, BOT.INFINITY, depth+1, True)

                    print(f"XTRA: Depth {depth+1} Nodes: {self.nodes} Move: {board.san(best_move_FOUND)} Time: {time.time() - _start} Score: {best_score}")

            print(f"Depth {currentItSearchDepth} | Nodes: {self.nodes} | Move: {board.san(best_move_FOUND)} | Time: {time.time() - _start} | Score: {best_score}")
            print(f"Move Ordering {self.fhf /max(self.fh,1)}")
            print("PV LINE: ",self.get_pv_line_san(board, pvM))

            if best_score >= BOT.INFINITY - 100:
                # found checkmate
                return best_move_FOUND

        return best_move_FOUND

    def search(self, board, depth, alpha, beta, maxd, null_move):

        move_score = -BOT.INFINITY

        if board.is_checkmate():
            return board.ply() - BOT.INFINITY #* (-1 if board.turn else 1)
        elif board.is_stalemate() or board.is_repetition() or board.is_insufficient_material():
            return -(0 + BOT.contempt[self.getPhase(board)]) #* (-1 if board.turn else 1)

        # mate distance pruning ?
        if board.ply() > 0:
            alphaM = max(alpha, board.ply() - BOT.INFINITY)
            betaM = max (beta, BOT.INFINITY - board.ply())
            if alphaM >= betaM:
                return alphaM

        # transposition tables
        h = board.fen()

        hash_entry = self.get_hash(h)
        if hash_entry is not None:
            if hash_entry.depth >= depth:
                if hash_entry.flag == HashEntry.EXACT:
                    return hash_entry.score
                elif hash_entry.flag == HashEntry.ALPHA and hash_entry.score <= alpha:
                    return alpha
                elif hash_entry.flag == HashEntry.BETA and hash_entry.score >= beta:
                    return beta

        self.nodes += 1
        l = board.legal_moves

        # no legal moves
        if l.count() == 0:
            return self.evaluate(board)

        razoringMargin = 5 # value of a queen (maybe change this)
        if depth <= 0:
            return self.SearchAllCaptures(board, alpha, beta)
        elif depth <= 2: # razoring
            if alpha == beta - 1:
                if (self.evaluate(board) + razoringMargin * depth) < beta:
                    value = self.SearchAllCaptures(board, alpha, beta)
                    #move_score = -self.search(board, depth-2, -window[1], -window[0], maxd, null_move)
                    if value < beta:
                        return value
        #elif depth <= 4:
        #    if alpha == beta-1:
        #        if self.evaluate(board) < beta - (200 + 2 * depth):
        #            # razoring
        #            result = self.SearchAllCaptures(board, alpha, beta)
        #            if result < beta:
        #                return result

        if null_move and not board.is_check() and board.ply() > 0 and depth >= 3 and len(board.piece_map()) >= 10: #NULL MOVE - also wanna prevent zugzwang
            board.push(chess.Move.null())
            move_score = -1 * self.search(board, depth - 3, -beta, -beta + 1, maxd, False)
            board.pop()

            if move_score >= beta:
                return beta

        move_score = -BOT.INFINITY
        best_score = -BOT.INFINITY

        old_a = alpha
        best_move = None

        moves = self.orderM(board, l, None, False)
        #print(moves)

        legal = 0
        pvM = self.retrieve_pvline(board)

        # implement PVS in the future

        # test first move

        for i, m in enumerate(moves):
            #m = Move.from_uci(m)
            if i == 0:
                board.push(m)
                move_score = -self.search(board, depth-1, -beta, -alpha, maxd, null_move)
            else:
                legal += 1

                wascheck = board.is_check()

                board.push(m)

                """
                elif depth <= 3:

                    ev = self.evaluate(board)
                    if depth < 2:
                        value = ev + 2.5
                    elif depth < 3:
                        value = ev + 3.2
                    else:
                        value = ev + 4.7

                    if value < beta:
                        board.pop()
                        best_move = m
                        self.store_hash(board.fen(), best_move, beta, HashEntry.BETA, depth)
                        continue
                """
                # Aspiration window
                window = (alpha, alpha+1) if alpha+1 < beta else (alpha, beta)

                if i > 3 and depth > 3 and not board.is_check() and not board.is_capture(m) and m not in pvM and not wascheck: # LMR
                    move_score = -self.search(board, depth-2, -window[1], -window[0], maxd, null_move)
                else: move_score = -self.search(board, depth-1, -window[1], -window[0], maxd, null_move) # PVS

                # REDO PVS
                if (move_score > alpha and move_score > beta): # PVS
                    move_score = -self.search(board, depth-1, -beta, -alpha, maxd, null_move) # do full search
                    if move_score > alpha:
                        alpha = move_score

            board.pop()
            if move_score > best_score:
                best_score = move_score
                best_move = m

                if move_score > alpha:
                    if move_score >= beta:
                        if legal == 1:
                            self.fhf += 1
                        self.fh += 1
                        # killer moves
                        if not board.is_capture(m):
                            self.killers[1][board.ply()] = self.killers[0][board.ply()]
                            self.killers[0][board.ply()] = m

                        self.store_hash(board.fen(), best_move, beta, HashEntry.BETA, depth)
                        return beta

                    alpha = move_score
                    best_move = m
                    if not board.is_capture(m):
                        self.search_history[best_move.from_square][best_move.to_square] += depth

        if alpha != old_a:

            self.store_hash(board.fen(), best_move, best_score, HashEntry.EXACT, depth)
        else:

            # STORE HASH pos, bestmove, alpha, ALPHA, depth
            self.store_hash(board.fen(), best_move, alpha, HashEntry.ALPHA, depth)

        return alpha

    ############################ BOT/MODULE INTERFACE #################################

    def tablebase(self, board) :
        if len(board.piece_map()) <= 7 :
            try :
                fen = board.fen().replace(' ', '_')
                info = requests.get(f'http://tablebase.lichess.ovh/standard?fen={fen}')
                return info.json()['moves'][0]['uci']
            except Exception :
                pass
        return None

    def play(self, board): #

        r = self.get_opening_move(board)
        if r is not None:
            return r

        r = self.tablebase(board)
        if r == None:
            r = self.startSearch(board)
        return r

# testing
if __name__ == "__main__":
    #board = chess.Board("1k1r4/pp1b1R2/3q2pp/4p3/2B5/4Q3/PPP2B2/2K5 b - - 0 1")
    board = chess.Board("r1b2r1k/2qnb1pp/p2pNn2/1p6/4P3/P1N1B1Q1/1PP2PPP/R4R1K b - - 0 16")
    #board = chess.Board("8/8/8/1K6/1p1kPb2/1Pp5/P1B5/8 b - - 6 64")
    #board = chess.Board("8/7K/8/8/8/8/R7/7k w - - 0 1")
    #print(list(board.pieces))
    board.turn = False
    pr = cProfile.Profile()
    pr.enable()
    print(BOT().play(board))
    pr.disable()
    ps = pstats.Stats(pr).sort_stats('tottime')
    ps.print_stats()


from pymongo import MongoClient
import json
#import numpy as np

puerto = 27017
#puerto = 50375
hostname = 'localhost'

def ConectToMongoDB(puerto, Hostname):
    # Conexión a la base de datos
    mongoClient = MongoClient(Hostname, puerto)

    # Creamos la base de datos Pulseras
    db = mongoClient.Pulseras

    # Obtenemos una coleccion para trabajar con ella que la llamaremos archivos
    collection = db.archivos

    return collection

collection = ConectToMongoDB(puerto, hostname)

def InsertarRutaFichero(ruta_fichero):

    insert = {"fichero": ruta_fichero}

    # Insertamos la ruta del fichero en la colección
    collection.insert_one(insert)

def InsertClassify(classification, timestamp,ruta_fichero):
    query = {'fichero': ruta_fichero}

    np_array_to_list = classification.tolist()

    json_str = json.dumps(np_array_to_list)
    json_timestamp = json.dumps(timestamp.tolist())

    # Insertamos el resultado de la clasificación donde tenemos insertada la ruta de ese fichero
    collection.update(query, {'$set': {'Classify': json_str,'timeStamp':json_timestamp}}, upsert=True)


import datetime
import math

from sqlalchemy import desc, asc
from app.main import db
from app.main.model.product_price_history import ProductPriceHistory
from app.main.model.product import Product

def save_product_price_history(data):
    errors = {}

    # Check null
    if data['effective_date'] == "":
        errors['effective_date'] = ['Price updated date must not be null!']

    if data['original_price'] == "":
        errors['original_price'] = ['Product original price must not be null!']

    if data['sale_price'] == "":
        errors['sale_price'] = ['Product sale price must not be null!']

    if data['product_id'] == "":
        errors['product_id'] = ['Product ID must not be null!']

    # Check type
    if data['original_price'].isnumeric() is False:
        errors['original_price'] = ['Product original price is not valid!']

    if data['sale_price'].isnumeric() is False:
        errors['sale_price'] = ['Product sale price is not valid!']

    if data['product_id'].isnumeric() is False:
        errors['product_id'] = ['Product ID is not valid!']

    # Check if foregin key is valid or not
    product = Product.query.filter_by(id=data['product_id']).first()
    if not product:
        errors['product_id'] = ['Product ID does not exist']

    if len(errors) > 0:
        response_object = {
            'status': 'FAILED',
            'message': 'Failed to create a new product price history!',
            'errors': errors
        }
        return response_object, 200
    else:
        # effective date and product_id cannot be the same at once record
        product_price = ProductPriceHistory.query.filter_by(
            product_id=data['product_id'],
            effective_date=data['effective_date']).first()

        if product_price:
            errors['product price'] = 'Product price is already existed!'
            response_object = {
                'status': 'FAILED ',
                'message': 'Failed to create a new product price history!',
                'errors': errors
            }
            return response_object, 200
        else:
            new_price_history = ProductPriceHistory(
                original_price=data['original_price'],
                sale_price=data['sale_price'],
                effective_date=datetime.datetime.strptime(
                    data['effective_date'], '%Y-%m-%d'),
                product_id=data['product_id'],
                created_on=datetime.datetime.utcnow(),
                updated_on=datetime.datetime.utcnow()
            )
            db.session.add(new_price_history)
            db.session.commit()

            output = {}
            output['id'] = str(new_price_history.id)
            output['original_price'] = str(new_price_history.original_price)
            output['sale_price'] = str(new_price_history.sale_price)
            output['effective_date'] = str(
                new_price_history.effective_date)
            output['product_id'] = str(new_price_history.product_id)
            output['created_on'] = str(new_price_history.created_on)
            output['updated_on'] = str(new_price_history.updated_on)

            response_object = {
                'status': 'SUCCESS',
                'message': 'A new product price history is created successfully!',
                'data': output
            }
            return response_object, 201

def get_all_product_price_history():
    all_price_history = ProductPriceHistory.query.all()
    output = []

    for price_history in all_price_history:
        price_history_data = {}
        price_history_data['id'] = str(price_history.id)
        price_history_data['original_price'] = str(
            price_history.original_price)
        price_history_data['sale_price'] = str(price_history.sale_price)
        price_history_data['effective_date'] = str(
            price_history.effective_date)
        price_history_data['product_id'] = str(price_history.product_id)
        price_history_data['created_on'] = str(price_history.created_on)
        price_history_data['updated_on'] = str(price_history.updated_on)

        output.append(price_history_data)

    data = {}
    data['product_price_history'] = output

    respone_object = {
        'status': 'SUCCESS',
        'message':  'Sucessfully getting information of all product price!',
        'data': data
    }

    return respone_object, 200

def get_product_price_history(id):
    errors = {}
    product_price_history = ProductPriceHistory.query.filter_by(id=id).first()

    if not product_price_history:
        errors['ID'] = ['Product Price History does not exist']
        response_object = {
            'status': 'FAILED',
            'message': 'Fail to get product price history',
            'errors': errors
        }
        return response_object, 200

    price_history_data = {}
    price_history_data['id'] = str(
        product_price_history.id)
    price_history_data['original_price'] = str(
        product_price_history.original_price)
    price_history_data['sale_price'] = str(
        product_price_history.sale_price)
    price_history_data['effective_date'] = str(
        product_price_history.effective_date)
    price_history_data['product_id'] = str(
        product_price_history.product_id)
    price_history_data['created_on'] = str(
        product_price_history.created_on)
    price_history_data['updated_on'] = str(
        product_price_history.updated_on)

    response_object = {
        'status': 'SUCCESS',
        'message':  'Sucessfully getting information of product price history!',
        'data': price_history_data
    }

    return response_object, 200

def delete_product_price_history(id):
    product_price_history = ProductPriceHistory.query.filter_by(
        id=id).first()

    if not product_price_history:
        respone_object = {
            'status': 'ERROR',
            'message': 'Product price history does not exist!'
        }
        return respone_object, 200
    else:
        db.session.delete(product_price_history)
        db.session.commit()

    response_object = {
        'status': 'SUCCESS',
        'message': 'Successfully deleted the product log!'
    }
    return response_object, 200

def update_product_price_history(id, data):
    product_price_history = ProductPriceHistory.query.filter_by(id=id).first()
    is_updated = False
    errors = {}

    if not product_price_history:
        errors['id'] = ["Product price history ID does not exist!"]
        response_object = {
            'status': 'FAILED',
            'message': 'Cannot get Product price history!',
            'errors': errors
        }
        return response_object, 200
    else:
        # Check null
        if data['product_id'] == "":
            errors['product_id'] = ['Product ID must not be null!']

        if data['original_price'] == "":
            errors['original_price'] = [
                'Product original price must not be null!']

        if data['sale_price'] == "":
            errors['sale_price'] = ['Product sale price must not be null!']

        if data['effective_date'] == "":
            errors['effective_date'] = [
                'Price effective date must not be null!']

        # Check if foregin key is valid or not
        product = Product.query.filter_by(id=data['product_id']).first()
        if not product:
            errors['product_id'] = ["Product ID does not exist!"]

        if (len(errors) > 0):
            response_object = {
                'status': 'FAILED',
                'message': "Can not update product price history!",
                'errors': errors
            }
            return response_object, 200
        else:
            # effective date and product_id cannot be the same at once record
            product_price = ProductPriceHistory.query.filter_by(
                product_id=data['product_id'],
                effective_date=data['effective_date']).first()
            if (product_price and
                (product_price.product_id != str(data['product_id']) or
                 product_price.effective_date != datetime.datetime.strptime(
                    data['effective_date'], '%Y-%m-%d'))):
                errors['effective_date'] = [
                    "Effective date and product_id is already existed"]
                response_object = {
                    'status': 'FAILED',
                    'message': "Can not update product price history!",
                    'errors': errors
                }
                return response_object, 200
            else:
                if data['product_id'] != str(product_price.product_id):
                    is_updated = True
                    product_price.product_id = data['product_id']

                if data['original_price'] != product_price.original_price:
                    is_updated = True
                    product_price.original_price = data['original_price']

                if data['sale_price'] != product_price.sale_price:
                    is_updated = True
                    product_price.sale_price = data['sale_price']

                if data['effective_date'] != product_price.effective_date:
                    is_updated = True
                    product_price.effective_date = datetime.datetime.strptime(
                        data['effective_date'], '%Y-%m-%d')

                if is_updated is True:
                    product_price.updated_on = datetime.datetime.utcnow()
                    db.session.commit()

            product_price_data = {}
            product_price_data['id'] = str(product_price.id)
            product_price_data['original_price'] = str(
                product_price.original_price)
            product_price_data['sale_price'] = str(product_price.sale_price)
            product_price_data['effective_date'] = str(
                product_price.effective_date)
            product_price_data['product_id'] = str(product_price.product_id)
            product_price_data['created_on'] = str(product_price.created_on)
            product_price_data['updated_on'] = str(product_price.updated_on)

            response_object = {
                'status': 'SUCCESS',
                'message': "Successfully updated product price history!",
                'data': product_price_data
            }
            return response_object, 200

def get_product_price_history_with_pagination(args):
    # Query Params
    page_size = 10
    current_page = 1
    next_page = False
    key_word = None
    sort_field = None
    sort_order = -1

    # Check query param value
    if "page_size" in args:
        page_size = int(args['page_size'])
    if "current_page" in args:
        current_page = int(args['current_page'])
    if "key_word" in args:
        key_word = args['key_word'].lower()
    if "sort_field" in args:
        sort_field = args['sort_field']
    if "sort_order" in args:
        sort_order = int(args['sort_order'])

    # Sort by order value
    if sort_field is None or sort_order is None:
        '''Default order by the lasted created_on value'''
        product_price_history = ProductPriceHistory.query.order_by(
            ProductPriceHistory.created_on.desc())
    else:
        if sort_order == -1:
            product_price_history = ProductPriceHistory.query.order_by(
                desc(sort_field))
        else:
            product_price_history = ProductPriceHistory.query.order_by(
                asc(sort_field))

    product_price_history_on_page = product_price_history.limit(
        page_size).offset((current_page - 1) * page_size)
    total_pages = math.ceil(product_price_history.count() / page_size)

    if math.ceil(product_price_history.count() - page_size*current_page > 0):
        next_page = True
    else:
        next_page = False

    output = []

    for product_price in product_price_history_on_page:
        product = Product.query.filter_by(
            id=product_price.product_id).first()
        # Sort by keyword
        if (key_word is not None):
            if (key_word in str(product_price.original_price)) or (
                key_word in str(product_price.sale_price)) or (
                    key_word in product.name.lower()):
                product_price_data = {}
                product_price_data['id'] = str(
                    product_price.id)
                product_price_data['original_price'] = str(
                    product_price.original_price)
                product_price_data['sale_price'] = str(
                    product_price.sale_price)
                product_price_data['effective_date'] = str(
                    product_price.effective_date)
                product_price_data['product'] = {}
                product_price_data['product']['id'] = str(
                    product.id)
                product_price_data['product']['name'] = str(
                    product.name)

                output.append(product_price_data)
        else:
            product_price_data = {}
            product_price_data['id'] = str(
                product_price.id)
            product_price_data['original_price'] = str(
                product_price.original_price)
            product_price_data['sale_price'] = str(
                product_price.sale_price)
            product_price_data['effective_date'] = str(
                product_price.effective_date)
            product_price_data['product'] = {}
            product_price_data['product']['id'] = str(
                product.id)
            product_price_data['product']['name'] = product.name

            output.append(product_price_data)

    data = {}
    data['products_price_history'] = output
    data['total_pages'] = total_pages
    data['current_page'] = current_page
    data['has_next_page'] = next_page

    response_object = {
        'status': 'SUCCESS',
        'message': 'Sucessfully getting information of all products price history',
        'data': data
    }
    return response_object, 200


# coding=utf8
from youtubesearchpython import Search
import pandas as pd
import numpy as np

search_query_test = 'Кончится Лето Kino'

def find_first_vid(search_query):

    allSearch = Search(search_query, limit = 1)

    result_dict = allSearch.result()
    result_dict2 = list(result_dict.values())[-1]
    result_dict3 = result_dict2[-1]
    result_dict4 = list(result_dict3.values())[-2]

    return result_dict4

df_songs = pd.read_csv('song_links_results.csv', encoding='utf8')
df_list_songs = df_songs.values.tolist()
song_list = []

for i in df_list_songs:
    song_list.append(i[1])

from pytube import YouTube
import os
from pathlib import Path

def youtube2mp3 (url,outdir):
    # url input from user
    yt = YouTube(url)

    ##@ Extract audio with 160kbps quality from video
    video = yt.streams.filter(abr='160kbps').last()

    ##@ Downloadthe file
    out_file = video.download(output_path=outdir)
    base, ext = os.path.splitext(out_file)
    new_file = Path(f'{base}.mp3')
    os.rename(out_file, new_file)
    ##@ Check success of download
    if new_file.exists():
        print(f'{yt.title} has been successfully downloaded.')
    else:
        print(f'ERROR: {yt.title}could not be downloaded!')

'''
for song in song_list[1619:]:
    print(song_list.index(song))
    try:
        youtube2mp3(song,'dirpath')
    except FileExistsError:
        continue
    except AttributeError:
        continue
    except KeyError:
        continue

df_convert = pd.read_csv('converted_spotify_music_list.csv', encoding='utf8')
#df.apply(lambda x: pd.lib.infer_dtype(x.values))

df_titles_artists = df_convert[['Track Name','Artist Name(s)']]
df_titles_artists.astype(str)
df_titles_artists["separator"] = '-'

df_titles_artists["combined_col"] = df_titles_artists["Track Name"] + df_titles_artists["separator"] + df_titles_artists["Artist Name(s)"]

df_combined = df_titles_artists["combined_col"]

print(df_combined)
df_combined = df_combined.dropna(axis=0)
titles_artists_list = df_combined.values.tolist()

#print(titles_artists_list)

song_link_list = []
#titles_artists_list_subset = titles_artists_list[0:50]
titles_artists_list_subset = titles_artists_list
for song in titles_artists_list_subset:
    print('trying:',song)
    result_link = find_first_vid(song)
    song_link_list.append(result_link)

df_song_links = pd.DataFrame(song_link_list)

df_song_links.to_csv('song_links_results.csv', encoding='utf-8')

'''


import unittest
import mysql.connector

class TestMySQL(unittest.TestCase):

    def test_connect(self):
        connection = mysql.connector.connect(host='127.0.0.1',
                                     user='root',
                                     passwd='')

        try:
            cursor = connection.cursor()
            sql = "SELECT name, email FROM mytable ORDER BY name, email"
            cursor.execute(sql)
            rows = cursor.fetchall()

            expected = [
                ("Evil Bob", "evilbob@gmail.com"),
                ("Jane Doe", "jane@doe.com"),
                ("John Doe", "john@doe.com"),
                ("John Doe", "johnalt@doe.com")
            ]

            self.assertEqual(expected, rows)
        finally:
            connection.close()

if __name__ == '__main__':
    unittest.main()


import string

#get alphabet using the string module so we don't have to type it out by ourselves
alphabet = list(string.ascii_lowercase)

direction = ''

def encrypt(plain_text, shift_amt):
    encr_text = ""
    for letter in plain_text:
        #if the plain text in a specific location contains a space,
        #just add space and continue with the next iteration
        if plain_text[letter] == " ":
            encr_text += letter
            continue

        position = alphabet.index(letter)
        new_position = ""

        # prevent out of range index by checking beforehand the operation that will be assigned to the new_position var
        if not (position + shift_amt > len(alphabet)):
            new_position = position + shift_amt
        else:
            new_position = (position + shift_amt) - len(alphabet)
        encr_text += alphabet[new_position]
    print("Encrypted text:")
    print(f"{encr_text}")

def decrypt(plain_text, shift_amt):
    dencr_text = ""
    for letter in plain_text:
        # if the plain text in a specific location contains a space,
        # just add space and continue with the next iteration
        if plain_text[letter] == " ":
            encr_text += letter
            continue

        position = alphabet.index(letter)
        new_position = ""

        #prevent out of range index
        if not (position - shift_amt < 0):
            new_position = position - shift_amt
        else:
            new_position = (position - shift_amt) + len(alphabet)
        dencr_text += alphabet[new_position]
    print("Dencrypted text:")
    print(f"{dencr_text}")

while not (direction == 'encode') or (direction == 'decode'):
    direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")

text = input("Type the message you want to encode/decode:\n").lower()

shift = int(input("Type the shift number:\n"))

if direction == 'encode':
    encrypt(text,shift)
elif direction == 'decode':
    decrypt(text,shift)


# Generators

def generator_func(num):
    for i in range(num):
        # after yielding i, it pauses and remembers the last yield
        yield i

g = generator_func(10)

# when next(g) is called, the generator_func will pick up from last yield and yields next value until it reaches the last index of range

# when the end is reached, StopIteration error occurs.

# The range() function is a generator and when the range ends, the for loop detects the StopIteration error and stops the loop accordingly. This functionality is hidden away under the hood of for loop

next(g)  # 0
next(g)  # 1

print(next(g))  # 2

# ============================================================

# Print Fibonacci sequence upto n number

# using generator function ( uses less memory as the numbers are yeilded one by one. Only one number is stored in the memory at a time.)

def fib(num):
    a = 0
    b = 1
    for i in range(num):
        yield a
        temp = a
        a = b
        b = temp + a

for x in fib(20):
    print(x)

# =============================================================

# using list (uses more memory as all the output numbers are stored in memory)

def fib2(num):
    a = 0
    b = 1
    result = []
    for i in range(num):
        result.append(a)
        temp = a
        a = b
        b = temp + a
    return result

# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]
print(fib2(20))

# ===============================================================


import numpy as np
import matplotlib.pyplot as plt
from tkinter import *
from tkinter import messagebox

# Own Fuctions
from harmGen            import *
from transformLibrary   import *
from visualization      import *
from preSettings        import preSet # Optional: copy another PreSet.py file with new default values to calculate

# === GUI ===

# Window

window = Tk()

# Menu Bar

def aboutMsg():

        content = ['Alpha–beta transformation',
              'of harmonics v1.0',
               ' '   ,
               'Jose Fernando Diaz Benavides',
              'Copyright © 2019',
               '  ',
              'This program is licenced under Apache 2.0']
        messagebox.showinfo('About', "\n".join(content))

menubar  = Menu(window)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="About", command=aboutMsg)
menubar.add_cascade(label="Help", menu=helpmenu)
window.config(menu=menubar)

# Put Labeles
putLabels(window)

# Put Entries
entries(window)

# Put Grid
putGrid(window)

# Pre-settings
preSet(window)

def calculate():

        plt.close()

        # Read Inputs

        fs          = float(window.efs.get())         # Sampling frequency
        time_total  = float(window.etime_total.get()) # Total sampled time
        time        = timeVector(fs,time_total)

        # Signal 1
        #L1
        a0_a        = float(window.e_a0_a.get())
        a0_ph       = float(window.e_a0_ph.get())
        a0_f        = float(window.e_a0_f.get())
        a0_dc       = float(window.e_a0_dc.get())

        #L2
        b0_a        = float(window.e_b0_a.get())
        b0_ph       = float(window.e_b0_ph.get())
        b0_f        = float(window.e_b0_f.get())
        b0_dc       = float(window.e_b0_dc.get())

        #L3
        c0_a        = float(window.e_c0_a.get())
        c0_ph       = float(window.e_c0_ph.get())
        c0_f        = float(window.e_c0_f.get())
        c0_dc       = float(window.e_c0_dc.get())

        # Signal 2
        #L1
        a1_a        = float(window.e_a1_a.get())
        a1_ph       = float(window.e_a1_ph.get())
        a1_f        = float(window.e_a1_f.get())
        a1_dc       = float(window.e_a1_dc.get())

        #L2
        b1_a        = float(window.e_b1_a.get())
        b1_ph       = float(window.e_b1_ph.get())
        b1_f        = float(window.e_b1_f.get())
        b1_dc       = float(window.e_b1_dc.get())

        #L3
        c1_a        = float(window.e_c1_a.get())
        c1_ph       = float(window.e_c1_ph.get())
        c1_f        = float(window.e_c1_f.get())
        c1_dc       = float(window.e_c1_dc.get())

        # Signal 3
        #L1
        a2_a        = float(window.e_a2_a.get())
        a2_ph       = float(window.e_a2_ph.get())
        a2_f        = float(window.e_a2_f.get())
        a2_dc       = float(window.e_a2_dc.get())

        #L2
        b2_a        = float(window.e_b2_a.get())
        b2_ph       = float(window.e_b2_ph.get())
        b2_f        = float(window.e_b2_f.get())
        b2_dc       = float(window.e_b2_dc.get())

        #L3
        c2_a        = float(window.e_c2_a.get())
        c2_ph       = float(window.e_c2_ph.get())
        c2_f        = float(window.e_c2_f.get())
        c2_dc       = float(window.e_c2_dc.get())

        # === LIST OF PARAMETERS ===

        # List of Parameters: amplitude,phase,frequency,time vector, dc

        a_0 = [a0_a,a0_ph,a0_f,time,a0_dc]   # L1 Signal 1
        b_0 = [b0_a,b0_ph,b0_f,time,b0_dc]   # L2
        c_0 = [c0_a,c0_ph,c0_f,time,c0_dc]   # L3

        a_1 = [a1_a,a1_ph,a1_f,time,a1_dc]   # Signal 2
        b_1 = [b1_a,b1_ph,b1_f,time,b1_dc]
        c_1 = [c1_a,c1_ph,c1_f,time,c1_dc]

        a_2 = [a2_a,a2_ph,a2_f,time,a2_dc]   # Signal 3
        b_2 = [b2_a,b2_ph,b2_f,time,b2_dc]
        c_2 = [c2_a,c2_ph,c2_f,time,c2_dc]

        ############# CALCULATE ##################

        # === SIGNAL 0 ===

        a_s_0 = sinusSignal(a_0)
        b_s_0 = sinusSignal(b_0)
        c_s_0 = sinusSignal(c_0)

        # === SIGNAL 1  ===

        a_s_1 = sinusSignal(a_1)
        b_s_1 = sinusSignal(b_1)
        c_s_1 = sinusSignal(c_1)

        # === SIGNAL 2 ===

        a_s_2 = sinusSignal(a_2)
        b_s_2 = sinusSignal(b_2)
        c_s_2 = sinusSignal(c_2)

        # === ADD OS ===

        a_total = a_s_0 + a_s_1 + a_s_2
        b_total = b_s_0 + b_s_1 + b_s_2
        c_total = c_s_0 + c_s_1 + c_s_2

        # === ALPHA BETTA GAMMA ===

        alpha, beta, gamma, amplitude, angle = clarkeTransformation(a_total,b_total,c_total)

        # === PLOT ===

        plotAll(a_total,b_total,c_total,alpha, beta, gamma, amplitude, angle, time)

        # === CALCULATE END ===

# Button

window.button = Button( master  = window,
                        text    = 'Show plots',
                        command = calculate)
window.button.grid(row=20, column=3)

window.mainloop()


project='dcpots'
version='0.1.1'
debug = 1
defs = []
verbose = 'on'
extra_c_flags = '-wno-unused-parameter'
extra_cxx_flags = '--std=c++11 -lpthread -lrt -ldl'
env={
'protoc':'protoc',
}
units = [{
            'name':'dcbase',
            'subdir':'base',
        },
        {
            'name':'dcnode',
            'subdir':'dcnode',
            'incs':['/usr/local/include','/usr/local/include/libmongoc-1.0','3rd'],
            'dsrcs':['dcnode/proto','3rd/pbjson'],
            'objs': [{
                'out':'{{cdir}}/proto/dcnode.pb.cc',
                'dep':'{{cdir}}/proto/dcnode.proto',
                'cmd':'{{protoc}} {{cdir}}/proto/dcnode.proto -I{{cdir}}/proto --cpp_out={{cdir}}/proto'
            }]
        },
        {
            'name':'dcagent',
            'subdir':'dcagent',
            'incs':['/usr/local/include','/usr/local/include/libmongoc-1.0','3rd'],
            'dsrcs':['dcagent/proto'],
            'objs': [{
                'out':'{{cdir}}/proto/dcagent.pb.cc',
                'dep':'{{cdir}}/proto/dcagent.proto',
                'cmd':'{{protoc}} {{cdir}}/proto/dcagent.proto -I{{cdir}}/proto --cpp_out={{cdir}}/proto'
            }]
        },
        {
            'name':'mongoproxyapi',
            'subdir':'app/mongoproxy/api',
            'incs':['/usr/local/include/libmongoc-1.0','3rd','{{root}}'],
            'lincs':['/usr/local/lib'],
            'dsrcs': ['app/mongoproxy/proto'],
            'objs': [{
                'out':'{{cdir}}/../proto/mongo.pb.cc',
                'dep':'{{cdir}}/../proto/mongo.proto',
                'cmd':'{{protoc}} {{cdir}}/../proto/mongo.proto -I{{cdir}}/../proto --cpp_out={{cdir}}/../proto'
            }]
        },
        {
            'name': 'dcrepoter',
            'subdir': 'reporter',
            'incs': ['base','dcnode','dcagent','/usr/local/include/libmongoc-1.0','3rd'],
        },
        {
            'name': 'pbjson',
            'subdir': '3rd/pbjson',
            'incs': ['3rd'],
        },
        {
            'name': 'dcutil-redis',
            'subdir': 'utility/redis',
            'incs': ['3rd'],
        },
        {
            'name': 'dcrpc',
            'subdir': 'dcrpc',
            'incs': [],
            'dsrcs': ['client/','server/','share/'],
            'srcs': ['share/dcrpc.pb.cc'],
            'objs': [{
                'out':'{{cdir}}/share/dcrpc.pb.cc',
                'dep':'{{cdir}}/proto/dcrpc.proto',
                'cmd':'{{protoc}} {{cdir}}/proto/dcrpc.proto -I{{cdir}}/proto --cpp_out={{cdir}}/share'
            }]
        },
        {
            'name':'dcutil-mysql',
            'subdir':'utility/mysql',
            'incs':['/usr/local/include'],
        },
        {
            'name':'dcutil-script',
            'subdir':'utility/script',
            'incs':['/usr/local/include'],
        },
        {
            'name':'dcutil-mongo',
            'subdir':'utility/mongo',
            'incs':['/usr/local/include/libbson-1.0','3rd'],
        },
        {
            'name':'dcutil-drs',
            'subdir':'utility/drs',
            'incs':['/usr/local/include','3rd'],
            'objs': [{
                'out':'{{cdir}}/extensions.pb.cc',
                'dep':'{{cdir}}/extensions.proto',
                'cmd':'{{protoc}} {{cdir}}/dcxconfig.proto {{cdir}}/extensions.proto -I{{cdir}}/ -I/usr/local/include --cpp_out={{cdir}}/'
            }]
        },
        {
            'name':'dcutil-crypt',
            'subdir':'utility/crypt',
        },
        {
            'name':'dctest',
            'subdir':'app/test',
            'type':'exe',
            'incs':['/usr/local/include','/usr/local/include/libmongoc-1.0','{{root}}'],
            'lincs':['/usr/local/lib'],
            'libs' : [
                'dcnode',
                'dcagent',
                'dcutil-drs',
                'dcutil-mysql',
                'dcutil-mongo',
                'dcutil-script',
                'dcutil-crypt',
                'dcbase',
                'pbjson',
                'python2.7',
                'libprotobuf.a',
                'mysqlclient',
                'mongoc-1.0',
                'bson-1.0',
                'ssl',
                'crypto',
            ],
            'objs': [{
                'out':'{{cdir}}/test_conf.pb.cc',
                'dep':'{{cdir}}/test_conf.proto',
                'cmd':'{{protoc}} {{cdir}}/test_conf.proto -I{{cdir}} --cpp_out={{cdir}}'
            }]
        },
        {
            'name':'mongoproxy',
            'type':'exe',
            'subdir':'app/mongoproxy',
            'incs':['/usr/local/include','/usr/local/include/libmongoc-1.0','{{root}}'],
            'lincs':['/usr/local/lib'],
            'dsrcs': ['app/mongoproxy/proto'],
            'libs' : [
                'mongoproxyapi',
                'dcnode',
                'dcagent',
                'dcutil-mongo',
                'dcutil-drs',
                'dcbase',
                'pbjson',
                'python2.7',
                'libprotobuf.a',
                'mongoc-1.0',
                'bson-1.0'
            ]
        },
        {
            'name':'mongoproxy_testapi',
            'type':'exe',
            'subdir':'app/mongoproxy/testapi',
            'incs':['/usr/local/include','{{root}}'],
            'lincs':['/usr/local/lib'],
            'dsrcs': [],
            'libs' : [
                'mongoproxyapi',
                'dcnode',
                'dcagent',
                'dcutil-mongo',
                'dcutil-drs',
                'dcbase',
                'pbjson',
                'python2.7',
                'libprotobuf.a',
                'mongoc-1.0',
                'bson-1.0'
            ],
            'objs': [{
                'out':'{{cdir}}/test.pb.cc',
                'dep':'{{cdir}}/test.proto',
                'cmd':'{{protoc}} {{cdir}}/test.proto -I{{cdir}} --cpp_out={{cdir}}'
            }]
        },
        {
            'name':'reporter',
            'type':'exe',
            'subdir':'app/reporter',
            'incs':['/usr/local/include','/usr/local/include/libmongoc-1.0','{{root}}'],
            'libs' : [
                'dcrepoter',
                'dcagent',
                'dcnode',
                'dcutil-script',
                'dcutil-drs',
                'dcbase',
                'pbjson',
                'python2.7',
                'protobuf',
            ]
        },
        {
            'name':'collector',
            'type':'exe',
            'subdir':'app/collector',
            'incs':['/usr/local/include','/usr/local/include/libmongoc-1.0','{{root}}'],
            'libs' : [
                'dcrepoter',
                'dcagent',
                'dcnode',
                'dcutil-script',
                'dcutil-drs',
                'dcbase',
                'pbjson',
                'python2.7',
                'protobuf',
            ]
        },
        {
            'name':'pingpong',
            'type':'exe',
            'subdir':'app/pingpong',
            'incs':['/usr/local/include','/usr/local/include/libmongoc-1.0','{{root}}'],
            'libs' : [
                'dcnode',
                'dcutil-drs',
                'dcbase',
                'pbjson',
                'protobuf',
            ]
        },
]


import urllib3
from kivy.clock import Clock
from functools import partial

class Ping():

	# Выводим статус подключения к сети на главной странице и странице Настроек
	def getPing(self, ti):
		try:
		    http=urllib3.PoolManager()
		    response = http.request('GET', 'http://google.com')
		    self.screens[0].ids.net_label.text = 'Online'
		    self.screens[3].ids.net_bottom.text = 'Online'

		except:
			self.screens[0].ids.net_label.text = 'Offline'
			self.screens[3].ids.net_bottom.text = 'Offline'

	def callPing(self):
		Clock.schedule_interval(partial(Ping.getPing, self), 15)


from cterasdk.core import cli
from tests.ut import base_core

class TestCoreCLI(base_core.BaseCoreTest):

    def setUp(self):
        super().setUp()
        self._cli_command = 'show /settings'

    def test_run_cli_command(self):
        execute_response = 'Success'
        self._init_global_admin(execute_response=execute_response)
        ret = cli.CLI(self._global_admin).run_command(self._cli_command)
        self._global_admin.execute.assert_called_once_with('', 'debugCmd', self._cli_command)
        self.assertEqual(ret, execute_response)


import matplotlib.pyplot as plt
import pyk4a
from pyk4a import Config, PyK4A
import open3d as o3d
import time
import numpy as np
import copy
import cv2
from modern_robotics import *

if __name__ == "__main__":

    k4a = PyK4A(
        Config(
            color_resolution=pyk4a.ColorResolution.RES_720P,
            depth_mode=pyk4a.DepthMode.NFOV_UNBINNED,
            synchronized_images_only=True
        )
    )
    k4a.start()

    # getters and setters directly get and set on device
    k4a.whitebalance = 4500
    assert k4a.whitebalance == 4500
    k4a.whitebalance = 4510
    assert k4a.whitebalance == 4510

    idx = 0
    pcd_list = []
    capture_idx = 4

    # Capture
    capture = k4a.get_capture()
    rgb = capture.color

    # ROI 선택
    x, y, w, h = cv2.selectROI(rgb)

    # 포인트 클라우드 ROI 마스크 생성
    mask = np.zeros(rgb.shape[:2], dtype=np.uint8)
    mask[y:y+h, x:x+w] = 1
    mask = mask.astype(np.int16)
    indicies = np.where(mask==1)
    mask = np.expand_dims(mask, axis=-1)

    cv2.destroyAllWindows()

    time.sleep(1)

    coord_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=0.5, origin=[0, 0, -1])

    while True:
        if idx == capture_idx:
                break

        print('capture idx {0}'.format(idx))
        capture = k4a.get_capture()
        rgb = capture.color
        raw_pcd = capture.transformed_depth_point_cloud

        cv2.imshow('Pointcloud capture', rgb)
        if cv2.waitKey(0) == ord('s'):
            idx += 1
            rgb = cv2.cvtColor(rgb, cv2.COLOR_BGR2RGB)
            rgb = rgb[:, :, :3].astype(np.float32) / 255

            raw_pcd = raw_pcd * mask
            rgb = rgb * mask

            raw_pcd = np.reshape(raw_pcd, [-1, 3])
            rgb = np.reshape(rgb, [-1, 3])

            max_range_mask = np.where(np.logical_and(raw_pcd[:, 2]<550, raw_pcd[:, 2]>400))
            raw_pcd = raw_pcd[max_range_mask]
            rgb = rgb[max_range_mask]

            pcd = o3d.geometry.PointCloud()
            pcd.points = o3d.utility.Vector3dVector(raw_pcd)
            pcd.colors = o3d.utility.Vector3dVector(rgb)

            # Compute mean distance of points from origin
            # distances = np.sqrt(np.sum(np.square(np.asarray(pcd.points)), axis=1))
            # mean_distance = np.mean(distances)
            # pcd.scale(1 / mean_distance, center=pcd.get_center())

            # center = pcd.get_center()
            # new_origin = [0, 0, 0]
            # translation_vector = np.subtract(new_origin, center)
            # pcd.translate(translation_vector)

            pcd_list.append(pcd)

    for i in range(len(pcd_list)):
        print('save_pointcloud {0}'.format(i))
        # Save point cloud
        o3d.io.write_point_cloud('./4way_pointclouds/test_pointcloud_{0}.pcd'.format(i), pcd_list[i])


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os

seeds = [6969, 4242, 6942, 123, 420, 5318008, 23, 22, 99, 10]
delta_vals = [0.1, 0.2, 0.3, 0.4, 0.6, 0.7, 0.8, 0.9, 0.001, 0.005, 0.01, 0.05, 0.5, 1, 2, 3, 5]
delta_vals = [1, 2, 3, 5]

root_folder = 'sumo/test_results'
test_results = 'newoptim-linear-False-test_seed_5318008_robust_factor_-1/0.01-train_data.csv'

full_path = os.path.join(root_folder, test_results)

das_data = pd.read_csv(full_path)

delta_vals = [0.01,0.5]

seeds = [99, 22, 23, 10]
for delta in delta_vals[:2]:
    for seed in seeds:

        test_results = f'newoptim-linear-False-test_seed_{seed}_robust_factor_-1/{delta}-train_score_data.csv'
        full_path = os.path.join(root_folder, test_results)
        das_data = pd.read_csv(full_path)
        plt.plot(das_data['scores'])
        plt.xlabel("Episode")
        plt.ylabel('Return')

        plt.title(f'Episode Return during training, delta = {delta}, seed = {seed}')
        plt.savefig(f'lossplots/Score-{delta}-{seed}.png')
        plt.clf()