####该脚本考虑实际红移，观察Mh的分布
import sys
#sys.path.insert(0,'D:/Python1/pydocument/seniorproject_quenching2/practice/_pycache_/session_1')
sys.path.insert(0,'D:/Python1/pydocument/seniorproject_quenching2/practice')
##导入自己编写的脚本，需要加入这两句，一句声明符号应用，然后声明需要引入文-
#件的路径具体到文件夹
import os.path
from read_m16_1 import test_read_m16_ds_1
import numpy as np
import matplotlib.pyplot as plt
#import pandas as pd
from scipy.stats import t as st
#import seaborn as sns
#from scipy.stats import chisquare as chi
#画误差棒函数须引入scipy库
#导入的质量区间为11~15dex-Msolar
def input_mass_bin(v):
    _mh_path ='D:/Python1/pydocument/seniorproject_quenching2/practice/'
    fname = os.path.join(_mh_path,'z_eff.txt')
    z = np.loadtxt(fname,delimiter=',',dtype=np.float,unpack=True)
    z = np.array(z)
    N = 4
    m = np.array([np.linspace(11,12.75,1001),np.linspace(11,12.75,1001),\
                  np.linspace(11.75,13,2001),np.linspace(12.25,13.75,2001)])
    #针对不同质量区间设置拟合质量范围
    return m,N,z
#input_mass_bin(v=True)
def calcu_sigmaz(Rpc,m_,x,z):
    global omega_m
    omega_m = 0.315
    omegam = omega_m
#a flat CDM model,omegak=0;omegalamd=0
    global h_
    h_ = 0.673
    h = h_
    global G_
    G_ = 6.67*10**(-11)
    G = G_
    #global ms_
    #ms_ = 1.989*10**(30)
    #ms = ms_
    global c_
    c_ = 6
    ##调试结果c=3.25~3.75之间比较合适
    c = c_
    #下面开始计算
    #对导入的数据做单位转化转为:太阳质量/h
    m1 = h*m_*10**x
    L = len(Rpc)
    Rps = np.zeros(L,dtype=np.float)
    g_x = np.zeros(L,dtype=np.float)
    deltaSigma = np.zeros(L,dtype=np.float)
    Sigma = np.zeros(L,dtype=np.float)
    # rs =  np.zeros(LL,dtype=np.float)
    # r_200 =  np.zeros(LL,dtype=np.float)
    # rho_0 = np.zeros(LL,dtype=np.float)
    #单位修正因子
    #Qc = 0.1412#对rouc的单位修正（此时对roum、rou0的也完成了修正）
    #Q200 = 0.7
    ##重新对单位修正因子订正如下：
    Qc = 3.084*10**(-2)/(1.989*h**2)#对rouc的单位修正（此时对roum、rou0的也完成了修正）
    #Qc = 3.084*10**(-2)/(1.989)#对rouc的单位修正（此时对roum、rou0的也完成了修正）
    #rhom = 52700242579.0
    #m1 = m_*10**x
    Q200 = 1#对r200的单位修正（此时对rs也做了修正）
    H = h*100
    ##修正可以选择两处，对于计算过程涉及的物理量修正，或者对于常数修正
    rhoc = (Qc*(3*H**2)/(8*np.pi*G))/(1/(1+z))**3
    rhom = 200*rhoc*omegam
    #rhom=52700242579.0*200/h**2
    #考虑所有的红移为0.1
    r_200 = Q200*(3*m1/(4*np.pi*rhom))**(1/3)
    rs = r_200/c
    rho_0 = m1/((np.log(1+c)-c/(1+c))*4*np.pi*rs**3)
    for t in range(0,L):
        Rps[t] = Rpc[t]
        #引入中间函数
        f0 = Rps[t]/rs#这是考虑把R参数化的情况
        if Rps[t]<rs:
           f1 = np.arctanh(np.sqrt((1-f0)/(1+f0)))
           f2 = np.log(f0/2)
           f4 = f0**2*np.sqrt(1-f0**2)
           f5 = (f0**2-1)*np.sqrt(1-f0**2)
           g_x[t] = 8*f1/f4+4*f2*f0**(-2)-2/(f0**2-1)+4*f1/f5
           delta_segma = rs*rho_0*g_x[t]
          # deltasigma[n,t] = delta_segma  #(原有计算,面积单位为Mpc^2)
          # 为了和观测对比，把单位转为Msun*h/pc^2,即把面积单位转为pc^2
           deltaSigma[t] = delta_segma*10**(-12)
           Sigma[t] = (2*rs*rho_0/(f0**2-1))*(1-2*f1/(np.sqrt(1-f0**2)))
        elif Rps[t]==rs:
             g_x[t] = 10/3+4*np.log(1/2)
             delta_segma = rs*rho_0*g_x[t]
            # deltasigma[n,t] = delta_segma  #(原有计算,面积单位为Mpc^2)
            # 为了和观测对比，把单位转为Msun*h/pc^2,即把面积单位转为pc^2
             deltaSigma[t] = delta_segma*10**(-12)
             Sigma[t] = 2*rs*rho_0/3
        else:
             f1 = np.arctan(np.sqrt((f0-1)/(f0+1)))
             f2 = np.log(f0/2)
             f4 = f0**2*np.sqrt(f0**2-1)
             f5 = (f0**2-1)**(3/2)
             g_x[t]= 8*f1/f4+4*f2*f0**(-2)-2/(f0**2-1)+4*f1/f5
             delta_segma = rs*rho_0*g_x[t]
            # deltasigma[n,t] = delta_segma  #(原有计算,面积单位为Mpc^2)
            # 为了和观测对比，把单位转为Msun*h/pc^2,即把面积单位转为pc^2
             deltaSigma[t] = delta_segma*10**(-12)
             Sigma[t] = 2*rs*rho_0*(1-2*f1/np.sqrt(f0**2-1))/(f0**2-1)
    return Rpc,Sigma,deltaSigma,rs
#calcu_sigmaz(Rpc=True,m_=True,x=True,z=True)
##定义一个五个质量最佳预测和观测的对比图象
def fig_0(Rpc,ds_sim,lmw,r,mass_bin):
    test_read_m16_ds_1(mass_bin=mass_bin)
    for k in range(0,lmw):
        pa = r[k,1]
        plt.plot(Rpc[:],ds_sim[k,pa,:],'-*')
        nn = ds_sim[k,pa,:].size#求样本大小
        x_mean = np.mean(ds_sim[k,pa,:])#求算术平均值
        x_std = np.std(ds_sim[k,pa,:])#求标准偏差
        x_se = x_std/np.sqrt(nn)#求标准误差
        dof = nn-1#自由度计算
        alpha = 1.0-0.95
        conf_region = st.ppf(1-alpha/2.,dof)*x_std*\
        np.sqrt(1.+1./nn)#设置置信区间
        plt.errorbar(Rpc[:],ds_sim[k,pa,:],yerr=x_std,fmt='-',linewidth=0.5)
    plt.xlabel(r'$R-Mpc/h$')
    plt.ylabel(r'$\Delta\Sigma-hM_\odot/{pc^2}$')
    return x_mean,x_se,conf_region
#fig_0(Rpc=True,ds_sim=True,lmw=True,r=True)
#fig_0(f=True)
##定义一个质量最佳预测和观测的对比图象
def fig_0f1(Rpc,ds_sim,k,mass_bin):
    test_read_m16_ds_1(mass_bin=mass_bin)
    plt.plot(Rpc[:],ds_sim[k,:],'--')
    nn = ds_sim[k,:].size#求样本大小
    x_mean = np.mean(ds_sim[k,:])#求算术平均值
    x_std = np.std(ds_sim[k,:])#求标准偏差
    x_se = x_std/np.sqrt(nn)#求标准误差
    dof = nn-1#自由度计算
    alpha = 1.0-0.95
    conf_region = st.ppf(1-alpha/2.,dof)*x_std*\
    np.sqrt(1.+1./nn)#设置置信区间
    plt.errorbar(Rpc[:],ds_sim[k,:],yerr=x_std,fmt='-',linewidth=0.5)
    plt.xlabel(r'$R-Mpc/h$')
    plt.ylabel(r'$\Delta\Sigma-hM_\odot/{pc^2}$')
    return x_mean,x_se,conf_region
#fig_0f1(f=True)
def fig_0f2(Rpc,ds_sim,k,t,mass_bin):
    test_read_m16_ds_1(mass_bin=mass_bin)
    plt.plot(Rpc[:],ds_sim[k,t,:],'--',)
    nn = ds_sim[k,t,:].size#求样本大小
    x_mean = np.mean(ds_sim[k,t,:])#求算术平均值
    x_std = np.std(ds_sim[k,t,:])#求标准偏差
    x_se = x_std/np.sqrt(nn)#求标准误差
    dof = nn-1#自由度计算
    alpha = 1.0-0.95
    conf_region = st.ppf(1-alpha/2.,dof)*x_std*np.sqrt(1.+1./nn)#设置置信区间
    plt.errorbar(Rpc[:],ds_sim[k,t,:],yerr=x_std,fmt='-',linewidth=0.5)
    plt.xlabel(r'$R-Mpc/h$')
    plt.ylabel(r'$\Delta\Sigma-hM_\odot/{pc^2}$')
    return x_mean,x_se,conf_region
#fig_0f2(f=True)
#def fit_datar(y):
def fit_datar_1(mass_bin,z_int,T):
    h = 0.673
    #先找出观测值对应的rp
    #rsa,dssa,ds_errsa = test_read_m16_ds_1()
    rsa,dssa,ds_errsa = test_read_m16_ds_1(mass_bin=mass_bin)
    # print('ds=',dssa)
    # print('ds_err=',ds_errsa)
    #print('rp=',rsa)
    #a = np.shape(dssa)
    #print('size a=',a)
    ##注意到观测数值选取的是物理坐标，需要靠里尺度银子修正，修正如下
    #z_r = 0.1
    #a_r = 1/(1+z_r)
    ##此时对于预测的R也要做修正
    rp = np.array([rsa[0,k] for k in range(0,len(rsa[0,:])) if rsa[0,k]*h<=5])
   # rp = [rsa[0,k] for k in range(0,len(rsa[0,:])) if rsa[0,k]*h<=2]
   # rp = rsa[0,:]
    #该句直接对数组筛选，计算2Mpc以内（保证NFW模型适用的情况下）信号
    #print(rp)
    b = len(rp)
    m,N,z = input_mass_bin(v=True)
    mr = m[T]
    #print('mr=',mr)
    lm_bin = len(mr)
    #下面做两组数据的方差比较,注意不能对观测数据进行插值
    #比较方法，找出观测的sigma数值对应的Rp,再根据模型计算此时的模型数值Sigma（该步以完成）
    ds_simr = np.zeros((lm_bin,b),dtype=np.float)
    #rr = np.zeros(lm_bin,dtype=np.float)
    #该句用于下面相应作图部分
    for t in range(0,lm_bin):
        m_ = 1
        x = mr[t]
        z_r = z_int
        #Rpc = rp
        #Rpc,Sigma,deltaSigma = calcu_sigma(Rpc,m_,x)
        #ds_simr[k,t,:] = deltaSigma
        #对比文献，加入尺度因子修正如下,把物理的距离转为共动的距离
        #计算模型在对应的投射距离上的预测信号取值
        Rpc = rp
        Rpc,Sigma,deltaSigma,rs = calcu_sigmaz(Rpc,m_,x,z_r)
        #对预测信号做相应的变化，把共动的转为物理的
        ds_simr[t,:] = deltaSigma
        #下面部分作图对比
        #rr[t] = rs
        #fig_0f1(rp,ds_simr,t,mass_bin)
    #plt.title('Red')
    #plt.legend(m[:],bbox_to_anchor=(1,1))
    #plt.legend(m[:])
    #plt.savefig('Fit-red2.png',dpi=600)
    #plt.savefig('compare_1_rd.png',dpi=600)
    #plt.show()
    yy = np.shape(ds_simr)
    #print('rr=',rr)#输出查看ds_sim的维度，即模型预测下的透镜信号
    #比较观测的sigma和预测的Sigma,比较结果用fitr和fitb记录比较结果
    delta_r = np.zeros(lm_bin,dtype=np.float)
    for t in range(0,lm_bin):
        d_r = 0
        for n in range(0,yy[1]):
            d_r = d_r+((ds_simr[t,n]-dssa[0,n])/ds_errsa[0,n])**2
        delta_r[t] = d_r
    #print(delta_r)
    #下面这段求每个质量级对应的方差最小值
    deltar = delta_r.tolist()
    xr = deltar.index(min(deltar))
    bestfmr = mr[xr]
    best_mr = bestfmr+np.log10(h)
    #作图对比最佳情况
    '''
    plt.figure()
    k = xr
    fig_0f1(rp,ds_simr,k,mass_bin)
    plt.title('Red')
    #plt.savefig('fit_r.png',dpi=600)
    plt.show()
    '''
    #print('x^2=',min(deltar))
    #print('mr=',best_mr)
    #print('positionr=',xr)
    return rp,best_mr,delta_r,bestfmr
#fit_datar_1(y=True)
#def fit_datab_1(y):
def fit_datab_1(mass_bin,z_int,T):
    h = 0.673
    #先找出观测值对应的rp
    #rsa,dssa,ds_errsa = test_read_m16_ds_1()
    rsa,dssa,ds_errsa = test_read_m16_ds_1(mass_bin=mass_bin)
    # print('ds=',dssa)
    # print('ds_err=',ds_errsa)
    #print('rp=',rsa)
    #a = np.shape(dssa)
    #print('size a=',a)
    ##注意到观测数值选取的是物理坐标，需要靠里尺度银子修正，修正如下
    #z_b = 0.1
    #a_b = 1/(1+z_b)
    ##此时对于预测的R也要做修正
    rp = np.array([rsa[0,k] for k in range(0,len(rsa[0,:])) if rsa[0,k]*h<=5])
    # rp = [rsa[0,k] for k in range(0,len(rsa[0,:])) if rsa[0,k]*h<=2]
    # rp = rsa[0,:]
    #该句直接对数组筛选，计算2Mpc以内（保证NFW模型适用的情况下）信号
    #print(rp)
    b = len(rp)
    m,N,z = input_mass_bin(v=True)
    mb = m[T]
    #print('mr=',mb)
    lm_bin = len(mb)
    #下面做两组数据的方差比较,注意不能对观测数据进行插值
    #比较方法，找出观测的sigma数值对应的Rp,再根据模型计算此时的模型数值Sigma（该步以完成）
    ds_simb = np.zeros((lm_bin,b),dtype=np.float)
    #rb = np.zeros(lm_bin,dtype=np.float)
    #该句对应下面作图部分
    for t in range(0,lm_bin):
        m_ = 1
        x = mb[t]
        z_b = z_int
        #Rpc = rp
        #Rpc,Sigma,deltaSigma = calcu_sigma(Rpc,m_,x)
        #ds_simr[k,t,:] = deltaSigma
        #对比文献，加入尺度因子修正如下,把物理的距离转为共动的距离
        #计算模型在对应的投射距离上的预测信号取值
        Rpc = rp
        Rpc,Sigma,deltaSigma,rs = calcu_sigmaz(Rpc,m_,x,z_b)
        #对预测信号做相应的变化，把共动的转为物理的
        ds_simb[t,:] = deltaSigma
        ##下面部分作图对比
        #rb[t] = rs
        #fig_0f1(rp,ds_simb,t,mass_bin)
    #plt.title('Blue')
    #plt.legend(m[:],bbox_to_anchor=(1,1))
    #plt.legend(m[:])
    #plt.savefig('Fit-blue2.png',dpi=600)
    #plt.savefig('compare_2_bl.png',dpi=600)
    #plt.show()
    yy = np.shape(ds_simb)
    #print('rr=',rr)#输出查看ds_sim的维度，即模型预测下的透镜信号
    #比较观测的sigma和预测的Sigma,比较结果用fitr和fitb记录比较结果
    delta_b = np.zeros(lm_bin,dtype=np.float)
    for t in range(0,lm_bin):
        d_b = 0
        for n in range(0,yy[1]):
            d_b = d_b+((ds_simb[t,n]-dssa[1,n])/ds_errsa[1,n])**2
        delta_b[t] = d_b
    #print(delta_r)
    #下面这段求每个质量级对应的方差最小值
    deltab = delta_b.tolist()
    xb = deltab.index(min(deltab))
    bestfmb = mb[xb]
    best_mb = bestfmb+np.log10(h)
    #作图对比最佳情况
    '''
    plt.figure()
    k = xb
    fig_0f1(rp,ds_simb,k,mass_bin)
    plt.title('Blue')
    #plt.savefig('fit_r.png',dpi=600)
    plt.show()
    '''
    #print('x^2=',min(deltab))
    #print('mb=',best_mb)
    #print('positionr=',xb)
    return rp,best_mb,delta_b,bestfmb
#fit_datab_1(y=True)
#下面做图比较最佳预测值与观测的对比情况
def fig_1(tt):
    m,N,z = input_mass_bin(v=True)
    #调用时需要修改下面两句函数的关键字
    rp,best_mr,delta_r,bestfmr = fit_datar_1(y=True)
    rp,best_mb,delta_b,bestfmb = fit_datab_1(y=True)
    #rp,best_mr,delta_r = fit_datar(mass_bin)
    #rp,best_mb,delta_b = fit_datab(mass_bin)
    plt.subplot(121)
    plt.plot(m,np.log10(delta_r),'r')
    plt.title('R-galaxy')
    plt.xlabel(r'$log(\frac{M_h}{M_\odot})$')
    plt.ylabel(r'$log(\chi^2)$')
    plt.grid()
    plt.subplot(122)
    plt.plot(m,np.log10(delta_b),'b')
    plt.xlabel(r'$log(\frac{M_h}{M_\odot})$')
    plt.ylabel(r'$log(\chi^2)$')
    plt.title('B-galaxy')
    plt.grid()
    plt.tight_layout()
    plt.show()
    print('mr=',best_mr)
    print('mb=',best_mb)
    return m,N,best_mr,best_mb
#fig_(tt=True)
def fig_mass(pp):
    m_bin = ['10.0_10.4','10.4_10.7','10.7_11.0','11.0_15.0']
    mbin = [10.2,10.55,10.9,13.0]
    #作图取点
    m,N,z = input_mass_bin(v=True)
    z_use = np.zeros((len(m_bin),2),dtype=np.float)
    ##计算误差棒并存储的数组
    err_bar_r = np.zeros((len(m_bin),2),dtype=np.float)
    err_bar_b = np.zeros((len(m_bin),2),dtype=np.float)
    for k in range(0,len(m_bin)):
        if k==len(m_bin)-1:
            z_use[k,:] = z[:,-1]
        else:
            z_use[k,:] = z[:,k]
    print(z_use)
    #print(m_bin)
    #该句导入拟合质量
    Pr_mh = np.zeros((len(m_bin),2),dtype=np.float)
    b_m_r = np.zeros(len(m_bin),dtype=np.float)
    b_m_b = np.zeros(len(m_bin),dtype=np.float)
    #Pr_mh的第一行存red的质量，第二行存blue的质量,x_std也一样
    for k in range(0,len(m_bin)):
        rp,best_mr,delta_r,bestfmr = fit_datar_1(m_bin[k],z_use[k,0],k)
        rp,best_mb,delta_b,bestfmb = fit_datab_1(m_bin[k],z_use[k,1],k)
        Pr_mh[k,0] = best_mr
        Pr_mh[k,1] = best_mb
        b_m_r[k] = bestfmr
        b_m_b[k] = bestfmb
        ##下面把x^2转化为相应的概率分布，并让mh的划分区间服从这个分布
        mr = m[k]
        mb = m[k]
        rsa,dssa,ds_errsa = test_read_m16_ds_1(mass_bin=m_bin[k])
        pr = np.ones(len(m[k]),dtype=np.float)
        pb = np.ones(len(m[k]),dtype=np.float)
        ds_errsar = ds_errsa[0,0:len(rp)]
        ds_errsab = ds_errsa[1,0:len(rp)]
        for q in range(0,len(mr)):
            ss=1
            for t in range(0,len(rp)):
                ss = ss*(1/(np.sqrt(np.pi*2)*ds_errsar[t]))*np.exp((-1/2)*delta_r[q])
            pr[q]=ss
        for q in range(0,len(mb)):
            qq=1
            for t in range(0,len(rp)):
                qq = qq*(1/(np.sqrt(np.pi*2)*ds_errsab[t]))*np.exp((-1/2)*delta_b[q])
            pb[q]=qq
        ##第一次归一化，把概率密度函数归一到0~1之间
        pr = (pr-np.min(pr))/(np.max(pr)-np.min(pr))
        pb = (pb-np.min(pb))/(np.max(pb)-np.min(pb))
        '''
        plt.figure()
        plt.plot(mr,pr,'r')
        plt.title('Probability distribution')
        plt.xlabel(r'$log(M_h/M_\odot)$')
        plt.ylabel(r'$dP(M_h|M_\ast)/dM_h$')
        plt.show()
        plt.plot(mb,pb,'b')
        plt.title('Probability distribution')
        plt.xlabel(r'$log(M_h/M_\odot)$')
        plt.ylabel(r'$dP(M_h|M_\ast)/dM_h$')
        plt.show()
        '''
        #print('r=',delta_r)
        #print('b=',delta_b)
        ##第二次归一化，目的是让所有概率求和为1
        fr=np.zeros(len(mr),dtype=np.float)
        fb=np.zeros(len(mb),dtype=np.float)
        ss=0
        qq=0
        Fr=np.zeros(len(mr),dtype=np.float)
        Fb=np.zeros(len(mb),dtype=np.float)
        for q in range(0,len(mr)):
            ss=ss+pr[q]*(mr[1]-mr[0])
            fr[q]=ss
        for q in range(0,len(mb)):
            qq=qq+pb[q]*(mb[1]-mb[0])
            fb[q]=qq
        Ar = np.max(fr)
        Ab = np.max(fb)
        ss=0
        qq=0
        for q in range(0,len(mr)):
            ss=ss+pr[q]*(mr[1]-mr[0])/Ar
            Fr[q]=ss
        for q in range(0,len(mb)):
            qq=qq+pb[q]*(mb[1]-mb[0])/Ab
            Fb[q]=qq
        #plt.figure()
        #plt.plot(mr,Fr,'r')
        #plt.show()
        #plt.plot(mb,Fb,'b')
        #plt.show()
        vr = np.interp(0.16,Fr,mr)
        ur = np.interp(0.84,Fr,mr)
        err_bar_r[k,:] = [vr-bestfmr,ur-bestfmr]
        vb = np.interp(0.16,Fb,mb)
        ub = np.interp(0.84,Fb,mb)
        err_bar_b[k,:] = [vb-bestfmb,ub-bestfmb]
    ##对应for循环前面的plt.figure
    #print('phy_mr=',Pr_mh[:,0])
    #print('phy_mb=',Pr_mh[:,1])
    #print('errbar_r=',err_bar_r)
    #print('errbar_b=',err_bar_b)
    #####注意errorbar作图命令
    plt.figure()
    color_list = ['r','b','g']
    line1,caps1,bars1=plt.errorbar(mbin,Pr_mh[:,0],yerr=[err_bar_r[:,0],err_bar_r[:,1]],fmt="ks-",linewidth=1,
                                elinewidth=0.5,ecolor='k',capsize=1,capthick=0.5,label='red')
    line3,caps3,bars3=plt.errorbar(mbin,Pr_mh[:,1],yerr=[err_bar_b[:,0],err_bar_b[:,1]],fmt="k^-",linewidth=1,
                                elinewidth=0.5,ecolor='k',capsize=1,capthick=0.5,label='blue')
    plt.fill_between(mbin,Pr_mh[:,0]+err_bar_r[:,0],Pr_mh[:,0]+err_bar_r[:,1],
                         facecolor=color_list[0],alpha=.20)
    plt.fill_between(mbin,Pr_mh[:,1]+err_bar_b[:,0],Pr_mh[:,1]+err_bar_b[:,1],
                         facecolor=color_list[1],alpha=.20)
    plt.xlabel(r'$log[M_\ast/M_\odot]$')
    plt.ylabel(r'$log{\langle M_{200} \rangle/[M_\odot h^{-1}]}$')
    plt.legend(loc=2)
    plt.title(r'$Standard Deviation-1\sigma_{p}$')
    plt.axis([10,14,11,14])
    #plt.savefig('Standard_deviation.png',dpi=600)
    plt.show()
    return Pr_mh
fig_mass(pp=True)
'''
if __name__ == "__main__":
    dolad_data(m=True,hz=True)
    pass
'''


from collections import defaultdict

def printSolution(x):
    print(f"The solution is: {x}")

def main():

    file = open("input.txt", "r")

    detected = {
        "children": 3,
        "cats": 7,
        "samoyeds": 2,
        "pomeranians": 3,
        "akitas": 0,
        "vizslas": 0,
        "goldfish": 5,
        "trees": 3,
        "cars": 2,
        "perfumes": 1,
    }

    aunts = {}
    matches = defaultdict(int)
    for line in file:
        line = line.strip().lstrip("Sue ")
        line = line.replace(":", "").replace(",", "")
        line = line.split()

        aunt = int(line.pop(0))
        aunts[aunt] = {}
        while line:
            k, v = line.pop(0), line.pop(0)
            aunts[aunt][k] = int(v)

    for aunt, details in aunts.items():
        for k, v in details.items():
            if k in detected and detected[k] == v:
                matches[aunt] += 1

    printSolution(max(matches, key=matches.get))

if __name__ == "__main__":
    main()


""" Contains Ros OrderState classes """

import uuid

class OrderState(object):
    """ A Ros Order State """
    def __init__(self):
        self.uuid = ""
        self.type = 0
        self.status = 0
        self.state = 0

    @classmethod
    def CreateObjectRosMsg(cls, msg):
        os = OrderState()
        try:
            os.uuid = str(uuid.UUID(bytes=msg.order_id.uuid))
            # print(os.uuid)
            os.status = msg.order_status
            os.type = msg.order_type
            os.state = msg.order_state

        except:
            return None
        return os

class rosOrderStatus():
    UNKNOWN = 0
    STARTED = 10
    ONGOING = 20
    FINISHED = 30
    WAITING = 40
    ERROR = 255

class rosOrderType():
    UNKNOWN = 0
    MOVE_ORDER = 10
    TRANSPORT_ORDER_STEP = 20
    TRANSPORT_ORDER = 30

class rosMoveOrderStates():
    MOVE_ORDER_UNAVAILABLE = 10
    MOVE_ORDER_START = 11
    MOVE_ORDER_ONGOING = 12
    MOVE_ORDER_FINISHED = 13
    MOVE_ORDER_ERROR = 14

class rosTransportOrderStepStates():
    TOS_MOVE_ORDER_START = 20
    TOS_MOVE_ORDER_ONGOING = 21
    TOS_MOVE_ORDER_FINISHED = 22
    TOS_MOVE_ORDER_ERROR = 23
    TOS_ACTION_START = 24
    TOS_ACTION_ONGOING = 25
    TOS_ACTION_FINISHED = 26
    TOS_ACTION_ERROR = 27

class rosTransportOrderStates():
    # Transport order
    TO_LOAD_MOVE_ORDER_START = 40
    TO_LOAD_MOVE_ORDER_ONGOING = 41
    TO_LOAD_MOVE_ORDER_FINISHED = 42
    TO_LOAD_MOVE_ORDER_ERROR = 43
    TO_LOAD_ACTION_START = 44
    TO_LOAD_ACTION_ONGOING = 45
    TO_LOAD_ACTION_FINISHED = 46
    TO_LOAD_ACTION_ERROR = 47
    TO_UNLOAD_MOVE_ORDER_START = 48
    TO_UNLOAD_MOVE_ORDER_ONGOING = 49
    TO_UNLOAD_MOVE_ORDER_FINISHED = 50
    TO_UNLOAD_MOVE_ORDER_ERROR = 51
    TO_UNLOAD_ACTION_START = 52
    TO_UNLOAD_ACTION_ONGOING = 53
    TO_UNLOAD_ACTION_FINISHED = 54
    TO_UNLOAD_ACTION_ERROR = 55


from sys import stdin
from functools import lru_cache
def solve(grid):
    print(grid)
    def neig(row, col):
        if row + 1 < len(grid):
            yield (row+1, col)
            if col + 1 < len(grid[1]):
               yield (row+1, col+1)
            if col-1 >=0:
                yield (row + 1, col-1)

    @lru_cache(None)
    def max_coins(agent1_loc, agent2_loc):
        #print(agent1_loc, agent2_loc)
        if agent1_loc == (len(grid)-1,0) and agent2_loc == (len(grid)-1,len(grid[0])-1):
            return 0
        cmax = -float('inf')
        for agen1_neig in neig(*agent1_loc):
            for agen2_neig in neig(*agent2_loc):
                currn = max_coins(agen1_neig, agen2_neig)
                #print(agen1_neig, agen2_neig, currn)
                if agen1_neig == agen2_neig:
                    if currn + grid[agen1_neig[0]][agen1_neig[1]] > cmax:
                        cmax = currn + grid[agen1_neig[0]][agen1_neig[1]]
                elif currn +  grid[agen1_neig[0]][agen1_neig[1]] +  grid[agen2_neig[0]][agen2_neig[1]] > cmax:
                    cmax = currn +  grid[agen1_neig[0]][agen1_neig[1]] +  grid[agen2_neig[0]][agen2_neig[1]]
        #print("Return ", cmax)
        return cmax
    output = max_coins((0,0), (0, len(grid[0])-1))
    #print("Output ", grid, output + grid[0][0] + grid[0][len(grid[0])-1])
    return output + grid[0][0] + grid[0][len(grid[0])-1]

def read_grid(num):
    grid = []
    for row in range(num):
        line = stdin.readline()
        grid.append([int(val) for val in line.split()])
    return grid

def read_test_cases():
    test_cases = int(stdin.readline())
    stdin.readline()
    for tc in range(test_cases):
        grid_size = int(stdin.readline())
        grid = read_grid(grid_size)
        max_coins = solve(grid)
        print('Case #%d: %d'%(tc+1, max_coins))
        stdin.readline()

read_test_cases()


import torch
import torchvision.transforms as transforms
import torch.optim as optim
import time
import torch.nn as nn
from torchvision import models
import numpy as np
from tqdm import tqdm
from torch.utils.data import Dataset, DataLoader
import pandas as pd
import nibabel as nib

# GPU setup
if torch.cuda.is_available():
	device = torch.device("cuda:0")
	print("Run on GPU")
else:
	device = torch.device("cpu")
	print("Run on CPU")

# Convert 1 channel image to 3 channel
def grey_to_rgb(image):
	rgb_image = np.repeat(image[np.newaxis, ...], 3, 0)
	return rgb_image

# Model creation
class Net(nn.Module):

	def __init__(self):
		super().__init__()
		vgg16 = models.vgg16(pretrained=True)

		# Freeze up to the last 7th layer
		for param in vgg16.features[:-7].parameters():
			param.requires_grad = False

		self.feature_extractor = vgg16.features

		x = grey_to_rgb(torch.randn(224,224)).view(-1,3,224,224)

		self._to_linear = None
		self.convs(x)

		self.classifier = nn.Sequential(
			nn.Linear(self._to_linear, 100), nn.BatchNorm1d(100), nn.Dropout2d(),
			nn.ReLU(True),
			nn.Linear(100, 100), nn.BatchNorm1d(100),
			nn.ReLU(True),
			nn.Linear(100,2),
			nn.Softmax(dim=1)
		)

	def convs(self, x):
		x = self.feature_extractor(x)
		if self._to_linear is None:
			self._to_linear = x[0].shape[0]*x[0].shape[1]*x[0].shape[2]
		return x

	def forward(self, x):
		output = self.feature_extractor(x)
		output = output.view(-1, self._to_linear)
		output = self.classifier(output)
		return output

# Read nifti image files
def read_nifti_file(filepath):
	scan = nib.load(filepath)
	scan = scan.get_fdata()
	return scan

# Normalize image
def normalize(volume):
    volume = (volume - volume.min()) / (volume.max() - volume.min())
    volume = volume.astype("float32")
    return volume

def process_scan(path):
    volume = read_nifti_file(path)
    volume = normalize(volume)
    volume = grey_to_rgb(volume)
    return torch.Tensor(volume)

# Custom dataloader
class CID(Dataset):
	def __init__(self, annotations_file, transform=None):
		self.img_path = pd.read_csv(annotations_file, header=None)
		np.random.shuffle(self.img_path.values)
		self.transform = transform

	def __len__(self):
		return len(self.img_path)

	def __getitem__(self, idx):
		if torch.is_tensor(idx):
			idx = idx.tolist()
		img_path = self.img_path.iloc[idx, 0]
		image = process_scan(img_path)

		# Image label for label classifier
		if '/ADNI_' in img_path:
			label = torch.Tensor([1, 0])
		elif '/AD_' in img_path:
			label = torch.Tensor([0, 1])
		if self.transform:
			image = self.transform(image)
		sample = {'image': image, 'label': label}
		return sample

BATCH_SIZE = 256
file_path = '/home/justin.park/Summer_Research/dataset_creator/updated_dataset/training_dataset_path.csv'
dataset = CID(file_path, transform=transforms.Compose([transforms.Resize((224,224))]))
dataloader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True)

def fwd_pass(X, y, train=False):
	if train:
		model.zero_grad()
	outputs = model(X)
	matches = [torch.argmax(i) == torch.argmax(j) for i, j in zip(outputs, y)]
	acc = matches.count(True)/len(matches)
	loss = loss_function(outputs, y)

	if train:
		loss.backward()
		optimizer.step()
	return acc, loss

model = Net().to(device)
optimizer = optim.Adam(model.parameters(), lr=0.001)

# Loss function
loss_function = nn.MSELoss()

MODEL_NAME = f'model-{int(time.time())}'
print(MODEL_NAME)

def train():

	EPOCHS = 8 			#Generally 5 to 10
	counter = 0

	with open('model.log', 'a') as f:
		for epoch in range(EPOCHS):
			for data in tqdm(dataloader):
				batch_X = data['image'].to(device)
				batch_y = data['label'].to(device)
				acc, loss = fwd_pass(batch_X.float(), batch_y, train=True)
				counter += 1

				if counter % 5 == 0:
					f.write(f'{MODEL_NAME}, {round(time.time(),3)}, {round(float(acc),2)}, {round(float(loss),4)}\n')

train()

# save model
model_path = 'VGG16_Classifier.pt'
torch.save(model.state_dict(), model_path)


#!/usr/bin/python

from mininet.net import Mininet
from mininet.node import Host, RemoteController
from mininet.node import OVSKernelSwitch
from mininet.log import setLogLevel, info
from lib.controller import POXBridge as AmiController
from mininet.cli import CLI

def AMINetwork():

    net = Mininet( topo=None,
                   build=False,
                   ipBase='10.0.0.0/8')

    info('*** Adding controller \n')
    c1 = net.addController('c1',
                           controller=AmiController,
                           #protocol='tcp',
                           port=6633)
    c2 = RemoteController('c2', ip='127.0.0.1', port=6635)
    #net.addController(c2)

    info( '*** Add switches\n')
    s1 = net.addSwitch('s1', cls=OVSKernelSwitch)
    s3 = net.addSwitch('s3', cls=OVSKernelSwitch)
    s2 = net.addSwitch('s2', cls=OVSKernelSwitch)

    info( '*** Add hosts\n')
    h1 = net.addHost('h1', cls=Host, ip='10.0.0.10', defaultRoute=None)
    h2 = net.addHost('h2', cls=Host, ip='10.0.0.20', defaultRoute=None)
    h3 = net.addHost('h3', cls=Host, ip='10.0.0.30', defaultRoute=None)
    h4 = net.addHost('h4', cls=Host, ip='10.0.0.31', defaultRoute=None)

    info('*** Add links\n')
    net.addLink(h1, s1)
    net.addLink(s1, s2)
    net.addLink(h2, s2)
    net.addLink(s2, s3)
    net.addLink(h3, s3)
    net.addLink(h4, s3)

    info( '*** Starting network\n')
    net.build()
    # info ('*** Starting controllers\n')
    for controller in net.controllers:
        controller.start()

    # info('*** Starting switches\n')
    net.get('s1').start([c1])
    net.get('s3').start([c1])
    net.get('s2').start([c1])

    info('*** Post configure switches and hosts\n')
    #net.pingAll()
    CLI(net)
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    AMINetwork()


from salesforce_ocapi.auth import CommerceCloudClientSession
import requests

session = CommerceCloudClientSession(
    client_id="ADDCLIENTID",
    client_secret="ADDSECRET",
    instance="https://production-eu01-example.demandware.net",
)

categories = ["CHLBAKTEA", "BAKBRDBRN"]

def get_category_information(catalogid: str, categoryid: str):
    url = f"{session.instance}/s/-/dw/data/v20_4/catalogs/{catalogid}/categories/{categoryid}"
    headers = {"Authorization": session.AuthHeader()}
    response = requests.request("GET", url, headers=headers)
    if response.ok:
        return response.json()
    else:
        return response.status_code

for category in categories:
    print(get_category_information("master-catalog", category))


from time import sleep
from pubsub import pub
from smart_bar import SmartBar
from order_manager import OrderManager
from threading import Thread
import requests

class App:
    def run(self):
        self.setup()
        self.loop()

    def setup(self):
        print("Create App")
        requests.get('http://smart-bar-app.herokuapp.com/api/orders/delete_all')

        pub.subscribe(self.notify, pub.ALL_TOPICS)

        self.order_manager = OrderManager()
        self.smart_bar = SmartBar()

        order_manager_thread = Thread(target = self.order_manager.run, daemon = True)
        order_manager_thread.start()

    def loop(self):
        order = {}

        try:
            while True:
                if not self.smart_bar.isProcessing() and bool(order):
                    pub.sendMessage('order-creating', status='creating')
                    self.smart_bar.processDrink(order["drink"])
                    requests.get('http://smart-bar-app.herokuapp.com/api/orders/delete_all')
                    order = {}
                elif self.smart_bar.isProcessing() == False:
                    order = self.getNewOrder()

                if (self.order_manager.cancel):
                    print(self.order_manager.cancel)
                    self.smart_bar.stop()
                    self.order_manager.cancel = False

                sleep(0.1)

            print("Done making orders")
        except KeyboardInterrupt:
            print ("\nCtrl-C pressed.")
            self.smart_bar.stop()

    def notify(self, topicObj=pub.AUTO_TOPIC, **msgData):
        status = False

        if bool(msgData) and msgData['status']:
            status = msgData['status']

        self.order_manager.queueUpdateOrder(topicObj.getName(), status)
        print('topic "%s": %s' % (topicObj.getName(), msgData))

    def getNewOrder(self):
        return self.order_manager.getLatestOrder()

app = App()
app.run()


# https://leetcode.com/problems/populating-next-right-pointers-in-each-node/

from collections import deque

from TreeNode import TreeNode as Node
from TreeNode import list_to_treenodes as ltt

def add_next_prop(treenode):
    if not treenode:
        return
    treenode.next = None
    add_next_prop(treenode.left)
    add_next_prop(treenode.right)
    return treenode

class Solution:
    def connect(self, root: Node) -> Node:
        if not root or not root.left or not root.right:
            return root

        count, lvc = 1, 3
        stack = deque([root.left, root.right])
        while stack:
            node1 = stack.popleft()
            if node1.left:
                stack.append(node1.left)
                stack.append(node1.right)
            count += 1
            if count != lvc and stack:
                node1.next = stack[0]
            else:
                lvc = lvc * 2 + 1

        return root

    def connect(self, root):
        p = root
        n1 = n2 = root.left if root else None
        while n2:
            if n2 != p.left:
                n2.next = p.left
            else:
                n2.next = p.right
                p = p.next
            n2 = n2.next
            if not p:
                p, n1, n2 = n1, n1.left, n1.left
        return root

s = Solution()
root = add_next_prop(ltt("[1,2,3,4,5,6,7]"))
s.connect(root)
print(root)

leetcode/Populating Next Right Pointers in Each Node.py


import pytorch_lightning as pl
from . import tools

class ForexDataModule(pl.LightningDataModule):

    def __init__(self, window_size, batch_size, label_size, filenames):
        super().__init__()
        self.window_size = window_size
        self.batch_size = batch_size
        self.label_size = label_size
        self.filenames = filenames

    def prepare_data(self):

        price_data, price_labels = tools.build_dataset(
            *self.filenames,
            window_size=self.window_size,
            label_size=self.label_size
        )
        self.train_loader, self.val_loader = tools.split_and_create_loaders(price_data, price_labels, self.batch_size)

    def train_dataloader(self):
        return self.train_loader

    def val_dataloader(self):
        return self.val_loader


from flask import Flask, render_template, request
import requests
import json
from pprint import pprint
from models import *

app = Flask(__name__)

@app.route('/', methods = ['GET', 'POST'])
def index():
    return render_template('index.html')

@app.route('/result', methods = ['GET', 'POST'])
def results():
    businesses = []
    if(request.method == 'POST'):
        choice = request.form['choice']
        location = request.form['location']
        businesses = findBusiness(choice , location)

    return render_template('results.html' , businesses = businesses)

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


import casadi
import numpy as np
from math import *
import matplotlib.pyplot as plt
import time

'''

This code uses Orthogonal Collocation Direct Transcription(OCDT) to find the
numerical solution of the optimal control problem. It transforms solving the optimal
control problem into solving the Nonlinear Programing NLP problem.

Author:Han Shuo
Data:2022.3.26

'''

# OCDT parameter
Nfe = 10
K_radau = 3
I = np.arange(1,Nfe+1)
I1 = np.arange(1,Nfe)
J = np.arange(1,K_radau+1)
K = np.arange(0,K_radau+1)
tao = np.array([0.0, 0.1550510257216822, 0.6449489742783178, 1.0])
dljtauk = np.zeros((4,4))
dljtauk[0,:] = [-9.0000, 10.0488, -1.3821, 0.3333]
dljtauk[1,:] = [-4.1394, 3.2247, 1.1678, -0.2532]
dljtauk[2,:] = [1.7394, -3.5678, 0.7753, 1.0532]
dljtauk[3,:] = [-3, 5.5320, -7.5320, 5.0000]
dljtauk = dljtauk.transpose()
# print(dljtauk)

#vehicle parameter
L_wheelbase = 2.8
a_max = 0.3
v_max = 2.0
phy_max = 0.72
w_max = 0.54

#decision variable
x = casadi.SX.sym('x', 11, 4)
y = casadi.SX.sym('y', 11, 4)
theta = casadi.SX.sym('theta', 11, 4)
v = casadi.SX.sym('v', 11, 4)
phy = casadi.SX.sym('phy', 11, 4)

a = casadi.SX.sym('a', 11, 4)
w = casadi.SX.sym('w', 11, 4)
tf = casadi.SX.sym('tf')
hi = casadi.SX.sym('hi')

X = casadi.transpose(x[1:,:])#Casadi is different from numpy, its reshape is depend on column
X = casadi.reshape(X,-1,1)
Y = casadi.transpose(y[1:,:])
Y = casadi.reshape(Y,-1,1)
THETA = casadi.transpose(theta[1:,:])
THETA = casadi.reshape(THETA,-1,1)
V = casadi.transpose(v[1:,:])
V = casadi.reshape(V,-1,1)
PHY = casadi.transpose(phy[1:,:])
PHY = casadi.reshape(PHY,-1,1)
A = casadi.transpose(a[1:,1:])
A = casadi.reshape(A,-1,1)
W = casadi.transpose(w[1:,1:])
W = casadi.reshape(W,-1,1)

#equality constrains
g = []
gl = []
gu = []
#dynamics contrains
for i in I:
    for k in J:
        temp_gx     = dljtauk[0,k]*x[i,0]     + dljtauk[1,k]*x[i,1]     + dljtauk[2,k]*x[i,2]     + dljtauk[3,k]*x[i,3]     - hi*v[i,k]*casadi.cos(theta[i,k])
        temp_gy     = dljtauk[0,k]*y[i,0]     + dljtauk[1,k]*y[i,1]     + dljtauk[2,k]*y[i,2]     + dljtauk[3,k]*y[i,3]     - hi*v[i,k]*casadi.sin(theta[i,k])
        temp_gtheta = dljtauk[0,k]*theta[i,0] + dljtauk[1,k]*theta[i,1] + dljtauk[2,k]*theta[i,2] + dljtauk[3,k]*theta[i,3] - hi*casadi.tan(phy[i,k])*v[i,k]/L_wheelbase
        temp_gv     = dljtauk[0,k]*v[i,0]     + dljtauk[1,k]*v[i,1]     + dljtauk[2,k]*v[i,2]     + dljtauk[3,k]*v[i,3]     - hi*a[i,k]
        temp_gphy   = dljtauk[0,k]*phy[i,0]   + dljtauk[1,k]*phy[i,1]   + dljtauk[2,k]*phy[i,2]   + dljtauk[3,k]*phy[i,3]   - hi*w[i,k]
        g.append(temp_gx)
        gl.append(0.0)
        gu.append(0.0)
        g.append(temp_gy)
        gl.append(0.0)
        gu.append(0.0)
        g.append(temp_gtheta)
        gl.append(0.0)
        gu.append(0.0)
        g.append(temp_gv)
        gl.append(0.0)
        gu.append(0.0)
        g.append(temp_gphy)
        gl.append(0.0)
        gu.append(0.0)

#boundary continuous contrains
for i in I1:
    temp_eqx = 0
    temp_eqy = 0
    temp_eqtheta = 0
    temp_eqv = 0
    temp_eqphy = 0
    for j in K:
        temp_eqx_ = 1
        temp_eqy_ = 1
        temp_eqtheta_ = 1
        temp_eqv_ = 1
        temp_eqphy_ = 1
        for k in K:
            if j != k:
                temp_eqx_ *= (1-tao[k])/(tao[j]-tao[k])
                temp_eqy_ *= (1-tao[k])/(tao[j]-tao[k])
                temp_eqtheta_ *= (1-tao[k])/(tao[j]-tao[k])
                temp_eqv_ *= (1-tao[k])/(tao[j]-tao[k])
                temp_eqphy_ *= (1-tao[k])/(tao[j]-tao[k])
        temp_eqx += temp_eqx_ * x[i,j]
        temp_eqy += temp_eqy_ * y[i,j]
        temp_eqtheta += temp_eqtheta_ * theta[i,j]
        temp_eqv += temp_eqv_ * v[i,j]
        temp_eqphy += temp_eqphy_ * phy[i,j]

    g.append(x[i+1,0]-temp_eqx)
    gl.append(0.0)
    gu.append(0.0)
    g.append(y[i+1,0]-temp_eqy)
    gl.append(0.0)
    gu.append(0.0)
    g.append(theta[i+1,0]-temp_eqtheta)
    gl.append(0.0)
    gu.append(0.0)
    g.append(v[i+1,0]-temp_eqv)
    gl.append(0.0)
    gu.append(0.0)
    g.append(phy[i+1,0]-temp_eqphy)
    gl.append(0.0)
    gu.append(0.0)

#inequatility constrains
#box contrains
for i in I:
    for j in K:
        g.append(v[i,j])
        gl.append(-v_max)
        gu.append(v_max)

        g.append(phy[i,j])
        gl.append(-phy_max)
        gu.append(phy_max)

for i in I:
    for j in J:
        g.append(a[i,j])
        gl.append(-a_max)
        gu.append(a_max)

        g.append(w[i,j])
        gl.append(-w_max)
        gu.append(w_max)

#init contrains
g.append(x[1,0])
gl.append(1.03)
gu.append(1.03)

g.append(y[1,0])
gl.append(2.41)
gu.append(2.41)

g.append(theta[1,0])
gl.append(-0.03)
gu.append(-0.03)

g.append(v[1,0])
gl.append(0.1)
gu.append(0.1)

g.append(phy[1,0])
gl.append(-0.08)
gu.append(-0.08)

#terminal contrains
g.append(x[Nfe,K_radau])
gl.append(5.31)
gu.append(5.31)

g.append(y[Nfe,K_radau])
gl.append(2.41)
gu.append(2.41)

g.append(theta[Nfe,K_radau])
gl.append(-3.1416)
gu.append(-3.1416)

g.append(v[Nfe,K_radau])
gl.append(0.0)
gu.append(0.0)

g.append(phy[Nfe,K_radau])
gl.append(0.0)
gu.append(0.0)

g.append(a[Nfe,K_radau])
gl.append(0.0)
gu.append(0.0)

g.append(w[Nfe,K_radau])
gl.append(0.0)
gu.append(0.0)

g.append(tf)
gl.append(0.0)
gu.append(inf)

g.append(hi-tf/Nfe)
gl.append(0.0)
gu.append(0.0)

#cost function
F = 0
for i in I:
    for j in J:
        F += w[i,j]*w[i,j]
F = F + 10*tf

#nlp problem
nlp = {'x':casadi.vertcat(X,Y,THETA,V,PHY,A,W,tf,hi), 'f':F, 'g':casadi.vertcat(*g)}

#solve the nlp problem
S = casadi.nlpsol('S', 'ipopt', nlp)

#init solution
init_soution = np.load('init/init_planning.npz')
x = init_soution['x']
y = init_soution['y']
theta = init_soution['theta']
v = init_soution['v']
phy = init_soution['phy']
a = init_soution['a']
w = init_soution['w']
tf = init_soution['tf']
hi = init_soution['hi']

init_x = x[1:,:].reshape(-1).tolist()
init_y = y[1:,:].reshape(-1).tolist()
init_theta = theta[1:,:].reshape(-1).tolist()
init_v = v[1:,:].reshape(-1).tolist()
init_phy = phy[1:,:].reshape(-1).tolist()
init_a = a[1:,1:].reshape(-1).tolist()
init_w = w[1:,1:].reshape(-1).tolist()
init_tf = tf.tolist()
init_hi = hi.tolist()

init = init_x+init_y+init_theta+init_v+init_phy+init_a+init_w+init_tf+init_hi

t_start = time.perf_counter()
r = S(x0=init, lbg = gl, ubg=gu)
t_end =  time.perf_counter()
runtime = t_end - t_start
print('computing time:',runtime)

#print result
res = r['x']
X_star = res[0:40]
Y_star = res[40:80]
THETA_star = res[80:120]
V_star = res[120:160]
PHY_star = res[160:200]
A_star = res[200:230]
W_star = res[230:260]
TF_star = res[260:261]
HI_star = res[261:262]

# print(X_star[-2:-1])
# print(Y_star[-2:-1])
# print(THETA_star[-2:-1])
# print(V_star[-2:-1])
# print(PHY_star[-2:-1])
# print(A_star[-2:-1])
# print(W_star[-2:-1])

#plot
X_star=X_star.elements()
Y_star=Y_star.elements()

plt.plot(X_star,Y_star)
plt.show()


# Databricks notebook source
# MAGIC %md
# MAGIC
# MAGIC ## MLP with features selected by LR and balancing of training set

# COMMAND ----------

# importing custom functions
from Code.funcs import blob_connect, write_parquet_to_blob, create_validation_blocks
import csv
import json

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from pyspark.sql.window import Window
from pyspark.sql.types import IntegerType, FloatType, DoubleType, ArrayType, StringType
from pyspark.sql.functions import size, to_timestamp, mean as _mean, stddev as _stddev, col, sum as _sum, rand, when, collect_list, udf, date_trunc, count, lag, first, last, percent_rank, array
from pyspark.ml.feature import StringIndexer, OneHotEncoder, VectorAssembler, StandardScaler, IndexToString, StringIndexerModel
from pyspark.ml import Pipeline
from pyspark.ml.classification import LogisticRegression as LR, MultilayerPerceptronClassifier as MLP
from pyspark.ml.evaluation import MulticlassClassificationEvaluator as MLPEval

team_blob_url = blob_connect()

# COMMAND ----------

# MAGIC %md
# MAGIC ## Read from storage

# COMMAND ----------

joined = spark.read.parquet(f"{team_blob_url}/BK/pure_train")

# COMMAND ----------

# Read features selected by LR
with open('lr_eng_sig_feat.json', 'r') as fp:
    features = json.load(fp)

features = dict(filter(lambda x: True if x[1] >=2 else False, features.items()))

all_simple_features = ['DISTANCE', 'origin_DailySnowfall',  'origin_DailyPrecipitation'  , 'origin_DailyDepartureFromNormalAverageTemperature' ,  'origin_DailyAverageDryBulbTemperature',  'origin_DailyAverageRelativeHumidity' ,  'origin_DailyAverageStationPressure' , 'origin_DailySustainedWindDirection' ,  'origin_DailySustainedWindSpeed' ,  'dest_DailySnowfall' , 'dest_DailyPrecipitation',   'dest_DailyDepartureFromNormalAverageTemperature',  'dest_DailyAverageDryBulbTemperature',   'dest_DailyAverageRelativeHumidity',  'dest_DailyAverageStationPressure' ,  'dest_DailySustainedWindDirection',  'dest_DailySustainedWindSpeed' ,  'origin_HourlyDryBulbTemperature' ,   'origin_HourlyWindDirection' , 'origin_HourlyPrecipitation',  'origin_HourlyVisibility', 'dest_HourlyDryBulbTemperature', 'dest_HourlyStationPressure' , 'dest_HourlyWindDirection' , 'dest_HourlyPrecipitation' , 'dest_HourlyVisibility' ]

eng_features = ['origin_3Hr_DryBulbTemperature', 'origin_3Hr_PressureChange', 'origin_3Hr_StationPressure',  'origin_3Hr_Visibility', 'dest_3Hr_DryBulbTemperature',  'dest_3Hr_PressureChange', 'dest_3Hr_StationPressure',  'dest_3Hr_Precipitation',  'dest_3Hr_Visibility',  "Av_airport_delay", "Prev_delay_tailnum", "Av_carrier_delay", "weekly_flights_tailnum", 'airport_congestion', 'hourly_flights_origin', 'precip_severity','snow_severity']

all_categorical_features = ['DAY_OF_WEEK', 'OP_UNIQUE_CARRIER', 'MONTH', 'ORIGIN', 'DEST']

aux_features = ['sched_depart_date_time_UTC', 'DEP_DELAY', 'TAIL_NUM', 'DEP_DEL15']

bad_days = []
bad_airlines = []
bad_origins = []
bad_destinations = []
bad_months = []

bad_lsts = [bad_days, bad_airlines, bad_months, bad_origins, bad_destinations]

sig_num = []
categorical_features = []

# Fill the list of the simple features that came out to be significant in the trivial case
for f in all_simple_features + eng_features:
    for key in features.keys():
        if f in key:
           sig_num.append(key.split(' ')[-1])

# Fill the lists of the siginificance categories of categorical features
for f, f_lst in zip(all_categorical_features, bad_lsts):
    for key in features.keys():
        if f in key:
            f_lst.append(key.split(' ')[-1])

# Fill the list of the categorical features that came out to be significant in the trivial case
for f, f_lst in zip (all_categorical_features , bad_lsts):
    if len(f_lst) > 0:
        categorical_features.append(f)

# COMMAND ----------

sig_num

# COMMAND ----------

# take only columns needed
df_clean = joined[aux_features + sig_num].dropna()

# COMMAND ----------

# MAGIC %md
# MAGIC ## Generate new columns for categorical values.
# MAGIC ### These columns only have selected levels of the category

# COMMAND ----------

# MAGIC %md
# MAGIC ### Train / Test Split

# COMMAND ----------

number_of_folds = 5
test_val_sets = create_validation_blocks(df_clean, "sched_depart_date_time_UTC", blocks=number_of_folds, split=0.8)
balanced_sets = []

# Fix the imbalance on the train_df by dropping ~80% of random on-time flights
on_time_correction = 0.2 # We assume thwere is about 5 on-time flaight for each delayed
for train_df, val_df in test_val_sets:
    # Set an aux "to keep" column that is true is the flight is delayed, or 20% chance true if the flight is on time
    train_df = train_df.withColumn('to_keep', when(
        ((rand(seed = 42) < on_time_correction) & (col('DEP_DEL15') < 1)) | (col('DEP_DEL15') > 0)
        , True   ))
    # Take only records that are flagged to keep in the previous step
    train_df = train_df.filter(train_df.to_keep).drop('to_keep')

    balanced_sets.append((train_df, val_df))

# COMMAND ----------

# MAGIC %md
# MAGIC # Assemble stages for transform pipeline

# COMMAND ----------

# Assemble all features in one vector
input_cols = sig_num
assembler = VectorAssembler().setInputCols(input_cols).setOutputCol('feat_vec')

# Scale features to assist in regularization.
scaler = StandardScaler().setInputCol('feat_vec').setOutputCol('feat_scaled')

# Make Labels column
lbl_indexer = StringIndexer(stringOrderType = 'alphabetAsc').setInputCol('DEP_DEL15').setOutputCol('label')

# COMMAND ----------

# MAGIC %md
# MAGIC ##Training

# COMMAND ----------

# Create an object model that is heavily biased toward LASSO regularization
pipelines = []
predictions = []
predictions_train = []
feature_maps = []
layers = [len(input_cols), len(input_cols)+10, len(input_cols)+10, 2]
for train_df, val_df in balanced_sets:
    mlp = MLP(featuresCol='feat_scaled', labelCol='label', layers = layers, seed=42, maxIter=20)
    pipe = Pipeline(stages=[assembler, scaler, lbl_indexer, mlp])
    pipelines.append(pipe.fit(train_df))
    predictions.append(pipelines[-1].transform(val_df))
    predictions_train.append(pipelines[-1].transform(train_df))

# COMMAND ----------

# Combine predictions made on test data from all blocks into a single dataframe
prediction = predictions[0]
for p in predictions[1:]:
    prediction = prediction.union(p)

# Combine predictions made on train data from all blocks into a single dataframe
prediction_train = predictions_train[0]
for p in predictions_train[1:]:
    prediction_train = prediction_train.union(p)

# Convert probability output column to a column with probability of positive
extract_prob_udf = udf(lambda x: float(x[1]), DoubleType())
prediction = prediction.withColumn("prob_pos", extract_prob_udf(col("probability")))
prediction_train = prediction_train.withColumn("prob_pos", extract_prob_udf(col("probability")))

# COMMAND ----------

# MAGIC %md
# MAGIC # Performance evaluation
# MAGIC ### Perfomance on test data

# COMMAND ----------

# Set decison cut offs
CutOffs = [0, 0.30, 0.40, 0.45, 0.60, 0.80]

# Define functions to labeling a prediction as FP(TP)
# Based on teh cut off
def TP(prob_pos, label):
    return [ 1 if (prob_pos >= cut_off) and (label > 0)  else 0 for cut_off in CutOffs]
def FP(prob_pos, label):
    return [ 1 if (prob_pos >= cut_off) and (label < 1)  else 0 for cut_off in CutOffs]

# Define udfs based on these functions
# These udfs return arrays of the same length as the cut-off array
# With 1 if the decision would be TP(FP) at this cut off
make_TP = udf(TP,  ArrayType(IntegerType()))
make_FP = udf(FP,  ArrayType(IntegerType()))

# Generate these arrays in the dataframe returned by prediction
prediction = prediction.withColumns({'TP':make_TP(prediction.prob_pos, prediction.DEP_DEL15), 'FP':make_FP(prediction.prob_pos, prediction.DEP_DEL15)})

# Produce a pair-wise sum of these arrays over the entire dataframe, calculate total true positive along the way
num_cols = len(CutOffs)
TP_FP_pd = prediction.agg(array(*[_sum(col("TP")[i]) for i in range(num_cols)]).alias("sumTP"),
                        array(*[_sum(col("FP")[i]) for i in range(num_cols)]).alias("sumFP"),
                        _sum(col("label")).alias("Positives")
                        ).toPandas()

# Convert the result into the pd df of precisions and recalls for each cu-off
results_pd= pd.DataFrame({'Cutoff':CutOffs, 'TP':TP_FP_pd.iloc[0,0], 'FP':TP_FP_pd.iloc[0,1]})
results_pd['Precision'] = 100*results_pd['TP']/(results_pd['TP'] + results_pd['FP'])
results_pd['Recall']= 100*results_pd['TP']/TP_FP_pd.iloc[0,2]
results_pd.to_csv('../Data/Eng_LR_validation.csv')
results_pd

# COMMAND ----------

# MAGIC %md
# MAGIC ### Performance on train data

# COMMAND ----------

# Generate these arrays in the dataframe returned by prediction
prediction_train = prediction_train.withColumns({
    'TP':make_TP(prediction_train.prob_pos, prediction_train.label)
    , 'FP':make_FP(prediction_train.prob_pos, prediction_train.label)})

# Produce a pair-wise sum of these arrays over the entire dataframe, calculate total true positive along the way
num_cols = len(CutOffs)
TP_FP_pd = prediction_train.agg(array(*[_sum(col("TP")[i]) for i in range(num_cols)]).alias("sumTP"),
                        array(*[_sum(col("FP")[i]) for i in range(num_cols)]).alias("sumFP"),
                        _sum(col("label")).alias("Positives")
                        ).toPandas()

# Convert the result into the pd df of precisions and recalls for each cu-off
results_pd_train= pd.DataFrame({'Cutoff':CutOffs, 'TP':TP_FP_pd.iloc[0,0], 'FP':TP_FP_pd.iloc[0,1]})
results_pd_train['Precision'] = 100*results_pd_train['TP']/(results_pd_train['TP'] + results_pd_train['FP'])
results_pd_train['Recall']= 100*results_pd_train['TP']/TP_FP_pd.iloc[0,2]
results_pd_train.to_csv('../Data/Eng_LR_train.csv')
results_pd_train

# COMMAND ----------

# MAGIC %md
# MAGIC
# MAGIC ### Visualize overfitting

# COMMAND ----------

def impute_precision(x,y, x_to_impute):
    int_idx = 0
    for i in range(len(x)-1):
        if ((x[i] > x_to_impute) & (x[i+1] < x_to_impute)):
            int_idx = i
    impute_value = y[int_idx+1] - (y[int_idx+1] - y[int_idx])* ((x[int_idx+1]-x_to_impute)/(x[int_idx+1]-x[int_idx]))
    return impute_value

# COMMAND ----------

dfs = {"Validation" :results_pd, "Train":results_pd_train}
colors = {"Validation" :'g', "Train":'r'}

for df in dfs.values():
  df.drop(df[df.Precision < 1].index, inplace=True)
  df.drop(df[df.Recall < 1].index, inplace=True)
  df.drop(df[df.Precision > 90].index, inplace=True)

# Instantiate figure and axis
num_rows = 1
num_columns = 1
fig, axes = plt.subplots(num_rows, num_columns, sharex=True)
fig.set_figheight(10)
fig.set_size_inches(8, 6)

#Fill the axis with data
for name, df in dfs.items():
  axes.plot(df.Recall, df.Precision, label = name, color = colors[name])
  axes.scatter(df.Recall, df.Precision, color =  colors[name])
  # Write cutoff vaulues on the graph
  for index in range(len(df.Cutoff)):
    axes.text(df.Recall[index]-0.02, 1 + df.Precision[index], df.Cutoff[index], size=7)

# Draw a vertical line to show 80% recall
axes.axvline(x=80, ymin=0.05, ymax=0.45, color='gray', ls = '--')
axes.text(70, 40, '80% Recall', size=12)

#Set legend position
axes.legend(loc = 'upper right')

#Setup the x and y
axes.set_ylabel('Precision')
axes.set_xlabel('Recall')
#axes.set_ylim(5, 80)

# Remove the bounding box to make the graphs look less cluttered
axes.spines['right'].set_visible(False)
axes.spines['top'].set_visible(False)

plt.show()
fig.savefig(f"../Images/Eng_LR_overfitting.jpg", bbox_inches='tight', dpi = 300)

# COMMAND ----------

prec_dic = {}
for name, df in dfs.items():
    prec_dic[name] = [round(impute_precision(df['Recall'], df['Precision'], 80), 1)]

prec_df = pd.DataFrame.from_dict(prec_dic)
prec_df

# COMMAND ----------

# MAGIC %md
# MAGIC ### Retrain on full train set

# COMMAND ----------

test_train_sets = create_validation_blocks(joined[aux_features + sig_num], "sched_depart_date_time_UTC", blocks=1, split=0.01)

# Fix the imbalance on the train_df by dropping ~80% of random on-time flights
on_time_correction = 0.2 # We assume thwere is about 5 on-time flaight for each delayed
train_new = test_train_sets[0][1]

# Set an aux "to keep" column that is true is the flight is delayed, or 20% chance true if the flight is on time
train_new = train_new.withColumn('to_keep', when(
    ((rand(seed = 42) < on_time_correction) & (col('DEP_DEL15') < 1)) | (col('DEP_DEL15') > 0)
    , True   ))
# Take only records that are flagged to keep in the previous step
train_new = train_new.filter(train_new.to_keep).drop('to_keep')

# COMMAND ----------

# Assemble all features in one vector
input_cols = sig_num
assembler_new = VectorAssembler().setInputCols(input_cols).setOutputCol('feat_vec')

# Scale features to assist in regularization.
scaler_new = StandardScaler().setInputCol('feat_vec').setOutputCol('feat_scaled')

# Make Labels column
lbl_indexer_new = StringIndexer(stringOrderType = 'alphabetAsc').setInputCol('DEP_DEL15').setOutputCol('label')

layers = [len(input_cols), len(input_cols)+10, len(input_cols)+10, 2]
MLP_new = MLP(featuresCol='feat_scaled', labelCol='label', layers = layers, seed=42, maxIter=20)

pipeline_new = Pipeline(stages=[assembler_new, scaler_new, lbl_indexer_new, MLP_new])
pipeline_new_model = pipeline_new.fit(train_new)

# COMMAND ----------

# Load data
val_data = spark.read.parquet(f"{team_blob_url}/BK/pure_val")

pred_mlp = pipeline_new_model.transform(val_data)

# Convert probability output column to a column with probability of positive
extract_prob_udf = udf(lambda x: float(x[1]) , DoubleType())
threshold = 0.43
pred_mlp = pred_mlp.withColumns({"mlp_prob_pos": extract_prob_udf(col("probability")),
                                 "mlp_pred_lbl": when(col("mlp_prob_pos") >=threshold, 1).otherwise(0),
                                   'label':  when(col("DEP_DELAY") >=15, 1).otherwise(0)  })

MLPpred = pred_mlp[['sched_depart_date_time_UTC', 'TAIL_NUM', 'DEP_DELAY', 'label', 'mlp_prob_pos', 'mlp_pred_lbl']]

write_parquet_to_blob(MLPpred, 'BK/mlp_pred')

# COMMAND ----------


#!/bin/env python3

from os import path

from setuptools import setup, find_packages

here = path.abspath(path.dirname(__file__))
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='borgmacator',
    version='0.0.1',
    description='A GNOME AppIndicator for Borgmatic',
    long_description=long_description,
    long_description_content_type='text/markdown',
    url='https://github.com/N-Coder/borgmacator',
    author='Niko Fink',
    author_email='borgmacator@niko.fink.bayern',
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Environment :: X11 Applications :: Gnome',
        'Operating System :: Unix',
        'Intended Audience :: End Users/Desktop',
        'Topic :: Desktop Environment :: Gnome',
        'Topic :: System :: Archiving :: Backup',
        'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
        'Programming Language :: Python :: 3 :: Only',
    ],
    keywords='borg borgmatic backup appindicator indicator gnome',
    packages=find_packages(),
    python_requires='>=3.5, <4',
    install_requires=['PyGObject', 'pystemd', 'python-dateutil', 'requests', 'sh', 'appdirs'],
    entry_points={
        'console_scripts': [
            'borgmacator=borgmacator.main:main',
            'borgmacator-autostart=borgmacator.install:install',
            'borgmacator-restart=borgmacator.install:restart',
        ],
    },
    zip_safe=False,
    package_data={
        'borgmacator': ["img/*.svg"],
    }
)


import numpy as np
import os
from PIL import Image
import numpy as np
import torch
import gc

def load_one(i):
    # ../
    im_directory = "data/images/"
    mask_directory = "data/masks/"
    f_im = os.path.join(im_directory, str(i) + ".jpg")
    f_mask = os.path.join(mask_directory, str(i) + ".jpg")
    im = Image.open(f_im)
    mask = Image.open(f_mask)
    im_data = im.getdata()
    mask_data = mask.convert("L").getdata()

    im.close()
    mask.close()

    # extract data into numpy array
    im_data = np.array(im_data).T
    mask_data = np.array(mask_data).T

    # get data into right shape and concat image data to final data array
    data_im = np.array([im_data.reshape(3, 256, 256)])
    data_mask = np.array([mask_data.reshape(1, 256, 256)])
    gc.collect()
    return torch.tensor(data_im).float(), torch.tensor(data_mask).float()

class DataLoader:
    def __init__(self, set_size, train_size, batch_size, test_batch_size=100):
        self.set = np.arange(set_size)
        self.trainingset = []
        self.testset = []
        self.set_size = set_size
        self.test_batch_size = test_batch_size

        self.batch_size = batch_size

        self.train_size = train_size
        self.trainingset = np.random.randint(set_size, size=train_size)

        self.test_size = self.set_size - self.train_size
        self.batch_counter = 0
        self.test_counter = 0
        print(os.getcwd())
        self.testset = list(set(self.set).difference(self.trainingset))

        self.load_all_counter = 0

    def start_new_epoch(self):
        self.batch_counter = 0
        np.random.shuffle(self.trainingset)

    def epoch_finished(self):
        return self.batch_counter >= int(self.train_size / self.batch_size)

    def trainloader(self):
        batch = self.trainingset[
            (self.batch_counter * self.batch_size) : (
                (self.batch_counter + 1) * self.batch_size
            )
        ]
        self.batch_counter += 1
        return self.batchloader(batch=batch)

    def testdata_loaded(self):
        if self.test_counter >= int(self.test_size / self.test_batch_size):
            self.test_counter = 0
            return True

        return False

    def testloader(self):
        batch = self.testset[
            (self.test_counter * self.test_batch_size) : (
                (self.test_counter + 1) * self.test_batch_size
            )
        ]
        self.test_counter += 1
        return self.batchloader(batch=batch)

    def batchloader(self, batchsize=None, batch=None):
        im_directory = "data/images/"
        mask_directory = "data/masks/"
        # if batch is given iterate throught batch indices
        if batchsize == None and any(batch) != None:
            j = 0
            for i in batch:
                # open image
                f_im = im_directory + str(i) + ".jpg"
                f_mask = mask_directory + str(i) + ".jpg"
                with Image.open(f_im) as im:
                    im_data = np.array(im.getdata()).T
                with Image.open(f_mask) as mask:
                    mask_data = np.array(mask.convert("L").getdata()).T / 255
                # im = Image.open(f_im)
                # mask = Image.open(f_mask)
                # mask = mask.convert("L")
                # extract data into numpy array

                # get data into right shape and concat image data to final data array
                if j == 0:
                    data_im = np.array([im_data.reshape(3, 256, 256)])
                    data_mask = np.array([mask_data.reshape(1, 256, 256)])
                    j += 1

                else:
                    im_data = im_data.reshape(1, 3, 256, 256)
                    data_im = np.concatenate((data_im, im_data))

                    mask_data = mask_data.reshape(1, 1, 256, 256)
                    data_mask = np.concatenate((data_mask, mask_data))
                # im.close()
                # mask.close()
                gc.collect()
            return torch.tensor(data_im).float(), torch.tensor(data_mask).float()

        if batchsize != None and any(batch) == None:
            print("WIRD DOCH BENUTZT")
            # if batch is not given generate random batch of size batchsize
            batch = np.random.randint(5108, size=batchsize)
            j = 0
            for i in batch:
                # open image
                f_im = os.path.join(im_directory, str(i) + ".jpg")
                f_mask = os.path.join(mask_directory, str(i) + ".jpg")
                im = Image.open(f_im)
                mask = Image.open(f_mask)
                mask = mask.convert("L")

                # extract data into numpy array
                im_data = np.array(im.getdata()).T
                mask_data = np.array(mask.getdata()).T

                # get data into right shape and concat image data to final data array
                if j == 0:

                    data_im = np.array([im_data.reshape(3, 256, 256)])
                    data_mask = np.array([mask_data.reshape(1, 256, 256)])
                    j += 1

                else:
                    im_data = im_data.reshape(1, 3, 256, 256)
                    data_im = np.concatenate((data_im, im_data))

                    mask_data = mask_data.reshape(1, 1, 256, 256)
                    data_mask = np.concatenate((data_mask, mask_data))
                im.close()
                mask.close()
                gc.collect()
            return torch.tensor(data_im).float(), torch.tensor(data_mask).float()

        if batchsize == None and batch == None:
            # if batchsize and batch are None raise Exception
            raise Exception("Weder batchsize noch batch deklariert!")


import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

# %matplotlib inline

def f(x, y, z, regla):
    return regla & (1 << (x << 2 | y << 1 | z))

def ac1(regla=135, E0="azar", alto=100, ancho=100):
    fig = plt.figure(figsize=(alto / 10, ancho / 10))
    E = np.zeros((alto, ancho), dtype=np.bool_)
    if E0 == "central":
        E[0, int(ancho / 2)] = 1  # 1 central
    elif E0[0] in ['0', '1']:
        ancho = len(E0)
        E = np.zeros((alto, ancho), dtype=np.bool_)
        for r in range(256):
            E[0, :] = np.array(list(E0))
    else:
        E[0, :] = np.round(np.random.rand(1, ancho))  # renglon aleatorio
    for i in range(1, alto):
        for j in range(1, ancho - 1):
            E[i, j] = f(E[i - 1, j - 1], E[i - 1, j], E[i - 1, j + 1], regla)
        E[i, 0] = f(E[i - 1, ancho - 1], E[i - 1, 0], E[i - 1, 1], regla)  # ahorra tiempo separar estos 2 casos
        E[i, ancho - 1] = f(E[i - 1, ancho - 2], E[i - 1, ancho - 1], E[i - 1, 0],
                            regla)  # no deben estar en el ciclo interno pues no depemden de j (pasa de 15s a 9s)
    plt.imshow(E, cmap=cm.Blues)
    plt.show()

ac1(115, "azar", alto=90)
#ac1(90, "central", alto=500, ancho=1000)
#ac1(94, "01110000000000001111100001111")
#ac1(alto=100, ancho=1000)


import joblib

class bipolar:
    def __init__(self):
        self.bipolar_model = joblib.load(
            "/media/raza/asd/Django/test/MindBliss/ClassesModels/bipolar.pkl")

    # Function that takes a binary array of size 11 as input and returns the predicted outcome for the bipolar test
    def bipolar_prediction_with_array(self,answers_array):
        prediction = self.bipolar_model.predict([answers_array])
        switcher = {
             0: 'Negative bipolar',
            1.0: 'Positive bipolar'
        }

        return switcher.get(float(prediction[0]), "Wrong Result")

    # Function that takes in 11 binary values as input and returns the predicted outcome for the bipolar test
    def bipolar_prediction_with_values(self,q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11):
        prediction = self.bipolar_model.predict([[q1, q2, q3, q4, q5, q6, q7, q8 ,q9, q10, q11]])
        switcher = {
            0: 'Negative bipolar',
            1.0: 'Positive bipolar'
        }

        return switcher.get(float(prediction[0]), "Wrong Result")


import re
import scrapy
from scrapy.loader import ItemLoader
from ..items import UuttarabankItem
from itemloaders.processors import TakeFirst
import datetime

pattern = r'(\xa0)?'
base = 'https://www.uttarabank-bd.com/index.php/home/news/{}'

class UuttarabankSpider(scrapy.Spider):
	now = datetime.datetime.now()
	year = now.year
	name = 'uttarabank'
	start_urls = [base.format(year)]

	def parse(self, response):
		yield response.follow(response.url, self.parse_post, dont_filter=True)

		if self.year > 2016:
			self.year -= 1
			yield response.follow(base.format(self.year), self.parse)

	def parse_post(self, response):
		articles = response.xpath('//div[@class="tabscontent"]//div[@class="row"]')
		length = len(articles)

		for index in range(length):
			item = ItemLoader(item=UuttarabankItem(), response=response)
			item.default_output_processor = TakeFirst()

			date = response.xpath(f'(//span[@style="color:#ff0000; font-size:12px;"])[{index + 1}]//text()').get()
			title = response.xpath(f'(//div[@class="floatleft col75"]/span[@title="Project Details"])[{index + 1}]//text()').get()
			content = response.xpath(f'(//div[@class="eleven columns row"])[{index + 1}]//text()').getall()
			content = [p.strip() for p in content if p.strip()]
			content = re.sub(pattern, "",' '.join(content))

			item.add_value('title', title)
			item.add_value('link', response.url)
			item.add_value('content', content)
			item.add_value('date', date)

			yield item.load_item()


import random
import discord
from discord_token import token

intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)

special_rolls = {1: "GM Intrusion", 17: "Damage Bonus +1", 18: "Damage Bonus +2", 19: "Minor Effect or +3 damage", 20: "Major effect or +4 damage"}

@client.event
async def on_ready():
  print(f"we've logged on as {client.user}")

@client.event
async def on_message(message):
  if message.author == client.user:
    return
  if message.content.split()[0] == '$hello':
      await greeting(message)
  if message.content.split()[0] == '$help':
      await help(message)
  if message.content.split()[0] == '$r' or message.content.split()[0] == '$roll':
      await roll(message)
  if message.content.split()[0] == '$hero':
      await hero(message)
  if message.content.split()[0] == '$h' or message.content.split()[0] == '$heal':
      await heal(message)
  if message.content.startswith('$d'):
      await rollD(message)

def greeting(message):
  return message.channel.send(f"Hello {message.author.display_name}, I'm Instant, a Cypher RPG die rolling bot.  Type $help for more details on how I can help")

def help(message):
  return message.channel.send("Instant:\n  $help\n    show this help\n  $r 3\n    make a level 3 roll\n  $d10 + 3\n    rolls a d10 and adds 3\n  $h +3\n    make a healing roll and add 3\n  $hello\n    Say hi.  Everybody appreciates politeness\nYou can also type $roll instead of $r, or $heal instead of $h -- I'm flexible.\nAnd you choose to skip spaces after + signs, or the + sign at all")

def roll(message):
  params = message.content.split()
  if (len(params) > 2):
      return message.channel.send(f"I'm not sure how to make that roll.  The syntax I'm expecting is $r 3, but you put more than one thing after the $r")
  if (len(params) < 2):
    level = 0
  else:
    try:
      level = int(params[1])
    except:
      return message.channel.send(f"I was expecting a number after the $r, but did you say '{params[1]}'?")
  roll = random.randint(1,20)
  target = 3 * level
  success = (roll >= target)
  success_message = ""
  if success:
      success_message = "**Success!**"
  else:
      success_message = "**Failed.**"
  m = success_message
  if roll in special_rolls.keys():
      m = f"{m}\n{special_rolls[roll]}."
  return message.channel.send(f">>> {message.author.display_name} rolled a **{roll}**, and the target was {target}\n{m}")

def parseBonus(params):
  bonus = 0
  for param in params:
    try:
      b = int(param)
      bonus = bonus + b
    except ValueError:
      b = 0
  return bonus

def heal(message):
  params = message.content.split()
  bonus = 0
  if (len(params) > 1):
    bonus = parseBonus(params[1:])
  roll = random.randint(1,6)
  if (bonus == 0):
    reply = f">>> {message.author.display_name} rolled **{roll}** on their heal roll"
  else:
    reply = f">>> {message.author.display_name} rolled **{roll}** + {bonus} = {roll + bonus} on their heal roll"
  return message.channel.send(reply)

def rollD(message):
  params = message.content.split()
  try:
    d = int(params[0][2:])
  except:
    return message.channel.send(f"I was expecting $d6 or $d8, not '{params[0]}.  What did you want me to do?'")
  bonus = 0
  if (len(params) > 1):
    bonus = parseBonus(params[1:])
  roll = random.randint(1, d)
  if bonus == 0:
    reply = f">>> {message.author.display_name} rolled **{roll}** on 1d{d}"
  else:
    reply = f">>> {message.author.display_name} rolled **{roll}** + {bonus} = {roll + bonus} on 1d{d} + {bonus}"
  return message.channel.send(reply)

def hero(message):
  return message.channel.send(f"You've got this {message.author.display_name}.  You're the hero here.")

client.run(token)


from flask import Flask, render_template, request
import pickle
from pandas import DataFrame
from sklearn.base import BaseEstimator
from sklearn.pipeline import Pipeline
from feature_engine.encoding import OneHotEncoder
from feature_engine.wrappers import SklearnTransformerWrapper
from sklearn.preprocessing import MinMaxScaler

###########################################################################################
#preprocessing pipeline
cat_features=['Fuel_Type', 'Seller_Type', 'Transmission']

pipe = Pipeline(steps=[

    ('one hot encoder', OneHotEncoder(variables=cat_features)),

    ('min max scaler', SklearnTransformerWrapper( variables = ['Kms_Driven'],
                                                 transformer=MinMaxScaler(feature_range=(0, 100) ) ) )

])
###########################################################################################
with open("pipe.pkl", "rb") as f:
	preprocessor = pickle.load(f)

with open("best_model.pkl", "rb") as f:
    model = pickle.load(f)

def predict_price(ex):
    columns = ['Year','Present_Price','Kms_Driven','Fuel_Type','Seller_Type', 'Transmission','Owner']
    df = DataFrame([ex], columns=columns)
    x = preprocessor.transform(df)
    y = model.predict(x)
    return round(y[0],2)
###################################################################################
app = Flask(__name__)

######################################################
@app.route("/", methods=['GET'])
def home():
   return render_template("home.html")

#######################################################
@app.route("/result", methods = ["GET","POST"])
def predict():
	if request.method == 'POST':
		Year = int(request.form.get('year'))
		Year = 2021 - Year
		Present_Price = float(request.form.get('pr_price'))
		Kms_Driven = float(request.form.get('kms'))
		Fuel_Type = request.form.get('fuel')
		Seller_Type = request.form.get('seller')
		Transmission = request.form.get('trans')
		Owner = int(request.form.get('owner'))

		x = [Year, Present_Price, Kms_Driven, Fuel_Type, Seller_Type, Transmission, Owner]
		y = predict_price(x)

		return render_template("result.html", price = y)
	else:
		return render_template("home.html")

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


'''
@author: bighouse
'''
from _collections import defaultdict
from ngram import NgramSequenceClassifier, Classifier, choose_max

class InterpolationClassifier(Classifier):

    def __init__(self, n, weights, verbose = False, cached = False):
        self.cached = cached
        if self.cached :
            self.cache = {}

        self.weights = weights
        self.n = n
        self.verbose = verbose
        self.k = len(weights)
        self.classifiers = []

        for i in range(self.k) :
            self.classifiers.append(NgramSequenceClassifier(n - i))

    def train(self, data, label):
        for i in xrange(self.k) :
            self.classifiers[i].train(data[i:], label)

    def prepare(self):
        pass

    def get_prob(self, denominator, classifier, label, current_ngram):
        model = classifier.models[label]
        numerator = classifier.get_prior_probabability_numerator(model, current_ngram[:-1])
        prior = numerator / denominator
        return model.conditional_probability(current_ngram) * prior

    def add_terms_of_submodel(self, probas, current_ngram, classifier, weight) :
            denominator = classifier.get_prior_probabability_denominator(current_ngram[:-1])
                # If the prior has never been seen
            if denominator == 0 :
                return 0.0
            else:
                for label in classifier.models :
                    frequency = self.get_prob(denominator, classifier, label, current_ngram)
                    probas[label] +=  frequency * weight

    def classify_suffix(self, probas, data, idx):
        for j in xrange(self.k):
            weight = self.weights[j]
            classifier = self.classifiers[j]
            subsequence = data[idx - self.n + j : idx + 1]
            self.add_terms_of_submodel(probas, subsequence, classifier, weight)
        return probas

    def classify(self, data):
        d_length = len(data)
        if d_length < self.n + 1 :
            return 'NONE'
        predictions = []
        for i in xrange(self.n, d_length) :
            probas = defaultdict(lambda : 0.0)
            probas = self.classify_suffix(probas, data, i)
            predictions.append(probas)
        result = choose_max(predictions)
        if len(result) == 0 :
            return "NONE"
        return result[0][0]


import socket
import kivy
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App

kivy.require('1.0.6')  # replace with your current kivy version !
__version__ = "1.0.3"

host = 'localhost'  # The server's hostname or IP address
port = 12345  # The port used by the server

class MainWindow(BoxLayout):
    input_text = ObjectProperty()
    status = ObjectProperty()
    port_data = ObjectProperty()
    host_data = ObjectProperty()
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    def light_led(self):
        print(self.input_text.text)
        message = self.input_text.text
        self.client.send(message.encode())

    def connect(self):
        self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        print("Trying to connect to the server")

        try:
            if self.port_data.text != "" and self.host_data.text != "":
                self.client.connect((self.host_data.text, int(self.port_data.text)))
            else:
                self.client.connect((host, port))
            print("Connection successful!")
            self.status.text = "Connected"
        except socket.error as exc:
            print("Caught exception socket.error : %s" % exc)
            self.status.text = str(exc)

    def disconnect(self):
        self.status.text = "Disconnected"
        print("Disconnecting from the server")
        self.client.close()

class MainApp(App):

    def build(self):
        Window.bind(on_request_close=self.on_request_close)
        return MainWindow()

    def on_request_close(self, *args):
        print("goodbye")
        MainWindow.client.close()
        self.stop()
        return True

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