from matplotlib.dates import AutoDateFormatter, AutoDateLocator
import numpy as np
import pandas as pd
from scipy.integrate import odeint
from csv import reader
from csv import writer
import scipy
from scipy.integrate import solve_ivp
from scipy.optimize import minimize
import matplotlib.pyplot as plt
from datetime import timedelta, datetime
import argparse
import sys
import os
import json
import ssl
import urllib.request
import statsmodels.api as sm

import pandasql as psql

from patsy import dmatrices

import matplotlib as mpl
mpl.rc('figure', max_open_warning = 0)

def load_data(path,sourcefile):
        df = pd.read_csv(path+"/"+sourcefile)
        return df

def generate_dataset(data, r_combined):
    tti = 0
    states = data['state'].drop_duplicates()
    data2 = data.copy()
    data3 = pd.DataFrame()
    for s in states:
        rc = r_combined[r_combined['state']==s]
        #print(rc)
        dat = data2[data2['state']==s]

        if len(dat)>0 and len(rc)>0:
            print(s)
            dat = dat.sort_values(by=['dateval'])
            #dat = dat.reset_index()
            beta = rc['lag_confirmed'].values[0]
            N = dat['population'].values[0]
            dat['susceptible'] = np.where(dat['holdout']==0, N+0.0,0.0)
            alpha = dat['R_0'].values[0]*beta
            dat['alpha'] = alpha
            if tti==0:
               data3 = dat
            else:
                data3 = data3.append(dat,ignore_index=True )
            tti = tti +1
    print(data3['state'].drop_duplicates())
    return data3

def test_covid(dataset,location_name):

    data = dataset[dataset['confirmed']>0]
    data = psql.sqldf("""
    select {loc},d_temp,period_group,did1,
    avg(ifnull(recovered,0)) as recovered,
    avg(ifnull(death,0)) as death,
    avg(ifnull(confirmed,0)) as positive from data
    group by 1,2,3,4,5
    """.format(loc=location_name))

    Y = data['positive']
    Y = (data['positive']-np.average(data['positive']))/np.std(data['positive'])
    X = data[['d_temp','period_group','did1']]

    formula = """positive ~ d_temp + period_group + did1 """
    response, predictors = dmatrices(formula, data, return_type='dataframe')
    po_results = sm.GLM(response, predictors, family=sm.families.Poisson()).fit()
    print(po_results.summary())

    Y = data['recovered']
    Y = (data['recovered']-np.average(data['recovered']))/np.std(data['recovered'])
    formula = """recovered ~ d_temp + period_group + did1 """
    response, predictors = dmatrices(formula, data, return_type='dataframe')
    po_results = sm.GLM(response, predictors, family=sm.families.Poisson()).fit()
    print(po_results.summary())

    Y = data['death']
    Y = (data['death']-np.average(data['death']))/np.std(data['death'])

    formula = """death ~ d_temp + period_group + did1 """
    response, predictors = dmatrices(formula, data, return_type='dataframe')
    po_results = sm.GLM(response, predictors, family=sm.families.Poisson()).fit()
    print(po_results.summary())

def test_gov_covid(dataset,location_name):

    data = dataset[dataset['positive']>0]
    Y = data['positive']
    Y = (data['positive']-np.average(data['positive']))/np.std(data['positive'])
    X = data[['d_temp','period_group','did1']]
    formula = """positive ~ d_temp  + period_group+CS+ED+GP+NEBC+OTH+SAH + did1 """
    response, predictors = dmatrices(formula, data, return_type='dataframe')
    po_results = sm.GLM(response, predictors, family=sm.families.Poisson()).fit()
    print(po_results.summary())
    Y = data['recovered']
    Y = (data['recovered']-np.average(data['recovered']))/np.std(data['recovered'])
    formula = """recovered ~ d_temp  + period_group+CS+ED+GP+NEBC+OTH+SAH + did1 """
    response, predictors = dmatrices(formula, data, return_type='dataframe')
    po_results = sm.GLM(response, predictors, family=sm.families.Poisson()).fit()
    print(po_results.summary())
    Y = data['death']
    Y = (data['death']-np.average(data['death']))/np.std(data['death'])
    formula = """death ~ d_temp  + period_group+CS+ED+GP+NEBC+OTH+SAH + did1 """
    response, predictors = dmatrices(formula, data, return_type='dataframe')
    po_results = sm.GLM(response, predictors, family=sm.families.Poisson()).fit()
    print(po_results.summary())

def solve_one(state,df,cfr,alpha,beta,c_x,c_names,N,horizon):
    def mysysfunc(h,s):
        z1 = 0.0
        z2 = 0.0
        tt = int(s)
        if tt>horizon and horizon<=len(df['dateval']):
            tt=horizon-1
            z1 = 0.0
            z2 = 0.0
        try:
            for i in range(len(c_x)):
                z1 = z1 + df[c_names[i]].values[tt]*c_x[i]

        except:
            z1 = z1

        dsdt = - (alpha/N) * h[0]*h[1]
        didt = (alpha/N)*h[0]*h[1] - beta*h[1]  -z1
        drdt = z1+beta*h[1]

        dhdt = [dsdt,didt,drdt]
        return dhdt

    t = [i for i in range(horizon)]
    # ############SETUP AND RUN THE SIMULATION=============
    h0 = [df['population'].values[0],df['confirmed'].values[0],df['removed'].values[0]]

    y = odeint(mysysfunc,h0,t) # integrate
    y = np.where(y<0,0,y)

    return y

def runDynamicSimulator(data1,coefsdfR,sir_names,xnamesr,horizon1):
    ###DEFINE SIMULATION CONSTANTS#######################################################
	# horizon = len(date_list)  # days . - forecast days
    # data1 starts from end of historical data date minus 7 days (from training)
    data = data1.copy()
    data = data.fillna(0)

    coefsR = coefsdfR.copy()

    coefsR = coefsR.fillna(0.0)

    its = 0
    for state in data['state'].drop_duplicates():

        if 1==1:
        #try:

            df = data[data['state']==state]
            print(df[0:10])
            if len(df['dateval'])>0:
                df = df.sort_values(by=['dateval'])
                df = df.loc[df['confirmed'].ne(0).idxmax():]
                df['pred_confirmed'] = 0.0
                df['pred_susceptible'] = 0.0
                df['pred_removed'] = 0.0
                #df = df.reset_index()
                dd = df[df['confirmed']>0]
                N = np.max(df['population'])
                print("Simulating state= "+state)
                horizon = len(df['dateval'])

                cfr = coefsR[coefsR['state']==state]
                alpha = df['alpha'].values[0]
                beta =  coefsR['lag_confirmed'].values[0]
                horizon_days = [i for i in range(horizon)]
                x1 = ['Intercept','gov_action','TAVG']
                c_xr = np.zeros(len(x1))

                df['pred_susceptible']= N #df['susceptible']
                for t in horizon_days:
                    if t==0:
                        df['pred_confirmed'].values[t] =df['confirmed'].values[t]
                        df['pred_susceptible'].values[t] = N #df['susceptible'].values[t]
                        df['pred_removed'].values[t] = df['removed'].values[t]

                    if t>0 and t<horizon:
                        z1 = 0.0
                        for c in range(len(x1)):
                            z1 = z1 + cfr[x1[c]].values[0]*df[x1[c]].values[t]
                        df['pred_confirmed'].values[t] = df['pred_confirmed'].values[t-1]+(alpha)*df['pred_susceptible'].values[t-1]*df['pred_confirmed'].values[t-1] - beta*df['pred_confirmed'].values[t-1] - z1

                        df['pred_removed'].values[t] = df['pred_removed'].values[t-1]+beta*df['pred_confirmed'].values[t-1] + z1

                        df['pred_susceptible'].values[t] = df['pred_susceptible'].values[t-1] - (alpha)*df['pred_susceptible'].values[t-1]*df['pred_confirmed'].values[t-1]

            if its ==0:
                out_df = df
            else:
                out_df = out_df.append(df)

            # # plot results
            plt.figure(1)
            plt.plot(out_df['dateval'],out_df['susceptible'],'b-')
            plt.plot(out_df['dateval'],out_df['confirmed'],'r--')
            plt.plot(out_df['dateval'],out_df['removed'],'g--')

            plt.xticks(rotation=45)
            plt.xlabel('Time')
            plt.ylabel('Populations')
            plt.legend(['Suceptibes','Confirmed','Removed'])
            plt.tight_layout()
            plt.savefig(os.path.join('/Users/aokossi/Documents/public service resarch/output/covid_plot_'+state+'.png'))
            plt.clf()
            its = its +1

        else:
        #except:
            print("skipped state for lack of government data "+state)

    return out_df

def runSimulator(data1,coefsdfR,sir_names,xnamesr,horizon1,date_gov_adjust, print_graph):
    	###DEFINE SIMULATION CONSTANTS#######################################################
	# horizon = len(date_list)  # days . - forecast days
    # data1 starts from end of historical data date minus 7 days (from training)
    # print(data1.columns)
    if date_gov_adjust > 0:
        data1.loc[data1.date>date_gov_adjust, 'gov_action'] = 0
    data = data1.copy()
    data = data.fillna(0)

    coefsR = coefsdfR.copy()

    coefsR = coefsR.fillna(0.0)

    df = pd.DataFrame()
    out_df = pd.DataFrame()
    its = 0
    for state in coefsR['state'].drop_duplicates():
        if 1==1:
        #try:
            cr = coefsR[coefsR['state']==state]
            df = data[data['state']==cr['state'].values[0]]
            if len(df)>0 and len(cr)>0:
            #if len(df['dateval'])>0:
                df = df.sort_values(by=['dateval'])
                df = df.reset_index()
                df = df.loc[df['confirmed'].ne(0).idxmax():]

                dd = df[df['confirmed']>0]
                N = np.max(df['population'])
                print("Simulating state= "+state)
                horizon = len(df['dateval']) # np.where(len(df['dateval'])<horizon1,len(df['dateval']),horizon1)

                cfr = cr
                alpha = df['alpha'].values[0]
                #t = np.linspace(0, horizon)  #[0:horizon] # times to report solution . - time in day sequence
                horizon_days = [i for i in range(horizon)]
                x1 = ['Intercept','gov_action','TAVG']
                c_xr = np.zeros(len(x1))
                for c in range(len(x1)):
                    c_xr[c] = cfr[x1[c]].values[0]
                c_xd = np.zeros(len(x1))

                # print(cfr)
                # print(c_xr)
                # print(c_xd)

                y = solve_one(state=state,df=df,cfr=cfr,alpha=alpha,beta=cfr['lag_confirmed'].values[0],c_x = c_xr,c_names = x1,N=N,horizon=horizon)

                print("solution for "+state)
                df['pred_susceptible'] = y[0:horizon,0]
                df['pred_confirmed'] = y[0:horizon,1]
                df['pred_removed'] = y[0:horizon,2]

                if print_graph == True:
                    # plot results
                    plt.figure(1)
                    plt.figure(figsize=(15,10))
                    xtick_locator = AutoDateLocator()
                    xtick_formatter = AutoDateFormatter(xtick_locator)

                    date_list = pd.to_datetime(df['dateval'])

                    ax = plt.axes()
                    ax.xaxis.set_major_locator(xtick_locator)
                    ax.xaxis.set_major_formatter(xtick_formatter)
                    plt.plot(date_list,y[:horizon,0],'b-')
                    plt.plot(date_list,y[:horizon,1],'r--')
                    plt.plot(date_list,y[:horizon,2],'g--')
                    plt.xlabel('Time')
                    plt.ylabel('Populations')
                    plt.legend(['Suceptibes','Confirmed','removed'])
                    plt.title('Prediction at '+state)
                    plt.savefig(os.path.join('output/covid_plot/covid_plot_'+state+'_'+str(date_gov_adjust)+'.png'))
                    plt.clf()
                    plt.close()
                out_df = out_df.append(df,ignore_index=True )

            its = its +1

        else:
        #except:
            print("skipped state for lack of government data "+state)

    return out_df

def causal_simulation(path,start_date,f_start_date,datafile="dataset_full.csv",govpolicyfile="gov_dates_mandates.csv", num_date_omit=0, print_graph=True):
    data = pd.read_csv(path+"/"+datafile)

    start_dt = datetime.strptime(start_date, '%m/%d/%y').strftime('%Y-%m-%d')
    print(start_dt)

    dateval = pd.date_range(start_dt, periods=horizon+180).tolist()
    dates = pd.DataFrame({'dateval': dateval})
    dates['dateval'] = dates['dateval'].apply(lambda x: datetime.strptime(str(x),'%Y-%m-%d %H:%M:%S').strftime('%Y-%m-%d') )
    data['dateval'] = data['date'].apply(lambda x: datetime.strptime(str(x),'%Y%m%d' ).strftime('%Y-%m-%d') )
    if num_date_omit > 0:
        temp_start_date = datetime.strptime(f_start_date, '%m/%d/%y') - timedelta(days=30)
        temp_start_date = int(temp_start_date.strftime('%Y%m%d'))
        print(temp_start_date)

        temp = data.loc[(data['confirmed'].isna())&(data['date']>temp_start_date), ['location_name','confirmed','dateval']].sort_values(by='dateval').reset_index(drop=True)
        temp_start_date = temp.loc[0, 'dateval']
        temp_start_training_date = (datetime.strptime(temp_start_date, '%Y-%m-%d') - timedelta(days=num_date_omit)).strftime('%Y-%m-%d')
        print(temp_start_training_date)
        temp_start_simulation_date = (datetime.strptime(temp_start_date, '%Y-%m-%d') - timedelta(days=num_date_omit-1)).strftime('%Y-%m-%d')

    data['state'] = data['province_state']

    zx = 0

    data = psql.sqldf("""
    select province_state , country_region as country, date, confirmed,
       recovered, death, population, TAVG/10 as TAVG,
     a1.location_name, a1.dateval, country_region ||'-'||state as state,
    case when TAVG<=0 then 1 else 0 end as is_freezing,
    case when TAVG>0 and TAVG/10<20 then 1 else 0 end as is_cold,
    case when TAVG>=20 and TAVG/10<35 then 1 else 0 end as is_warm,
    case when TAVG>=35 then 1 else 0 end as is_hot,
    case when TAVG>=20 then 1 else 0 end as temp_th,

      case when julianday(a1.dateval)>julianday('2020-03-20') then 1 else 0 end
       as gov_action
    from data a1
    """).drop_duplicates()

    data['Intercept'] = 1.0
    data = data[(data['dateval']>=start_dt)]
    data['holdout'] = np.where((data['dateval']>=datetime.strptime(f_start_date, '%m/%d/%y').strftime('%Y-%m-%d')),1,0)

    print(data)

    data_save = data.copy()

    # data smoothing to correct irregular data issues: like dropped cumulative values
    data1 = pd.DataFrame()

    z = 0

    for state in data['state'].drop_duplicates():
            dat = data[(data['state']==state)].sort_values(by=['dateval'])
            if len(dat['dateval'])>1:
                dat = dat.fillna(0)
                dat = dat.loc[dat['confirmed'].ne(0.0).idxmax():]
                rho_data = dat[dat['confirmed']>0].sort_values(by=['dateval'])
                rho_data = rho_data[0:30]

                zz1 = 0.0
                zz2 = 0.0
                zz3 = 0.0
                for s in range(len(rho_data['dateval'])):
                    if (s>0):
                        if rho_data['confirmed'].values[s]-rho_data['confirmed'].values[s-1]>0:
                            zz1 = zz1 + rho_data['confirmed'].values[s]-rho_data['confirmed'].values[s-1]
                        if rho_data['recovered'].values[s]-rho_data['recovered'].values[s-1]>0:
                            zz2 = zz2 + rho_data['recovered'].values[s]-rho_data['recovered'].values[s-1]
                        if rho_data['death'].values[s]-rho_data['death'].values[s-1]>0:
                            zz3  = zz3 + rho_data['death'].values[s]-rho_data['death'].values[s-1]
                rho = 0.0
                if (zz2+zz3) >0.0:
                    rho = (zz1+zz2+zz3)/(zz2+zz3)
                print("R_0 for "+state +" : "+str(rho))
                dat['lag_confirmed'] = 0.0
                dat['lag_recovered'] = 0.0
                dat['lag_death'] = 0.0
                dat['lag_removed'] = 0.0

                dat['d_recovered'] = 0.0
                dat['d_death'] = 0.0
                dat['d_removed'] = 0.0

                N = dat['population'].values[0]
                dd = dat[dat['confirmed']>1]

                dat['R_0'] = rho
                z1 = 0
                tt = 1
                dat['removed'] = dat['death'] + dat['recovered']

                for t in range(len(dat)):

                    if t>0 and t<=len(dat):
                        dat['lag_confirmed'].values[t] = dat['confirmed'].values[t-1]
                        dat['lag_removed'].values[t] = dat['removed'].values[t-1]
                        dat['lag_recovered'].values[t] = dat['recovered'].values[t-1]
                        dat['lag_death'].values[t] = dat['death'].values[t-1]
                        dat['d_recovered'].values[t] =  dat['recovered'].values[t] - dat['recovered'].values[t-1]
                        dat['d_death'].values[t] = dat['death'].values[t]-dat['death'].values[t-1]
                        dat['d_removed'].values[t] = dat['removed'].values[t]-dat['removed'].values[t-1]

                data1 = data1.append(dat,ignore_index=True)
                z = z +1

    data = data1.copy()
    rb = np.mean(data[data['R_0']>0]['R_0'])
    data['R_0'] = np.where(data['R_0']==0,rb,data['R_0'])
    data = data.fillna(0)
    data.to_csv("output/simulation_output/input_data.csv")
    if num_date_omit > 0:
        data_train = data[(data['removed']>0) & ((data['holdout']==0) | (data['dateval']<=temp_start_training_date))][['dateval','Intercept','state','TAVG','gov_action','is_freezing','is_cold','is_warm','is_hot','lag_confirmed','lag_death','lag_recovered','d_death','d_recovered','d_removed','removed']]
        print(temp_start_training_date)
        print(max(data_train['dateval']))
    else:
        data_train = data[(data['removed']>0) & (data['holdout']==0)][['dateval','Intercept','state','TAVG','gov_action','is_freezing','is_cold','is_warm','is_hot','lag_confirmed','lag_death','lag_recovered','d_death','d_recovered','d_removed','removed']]

    endog =data_train['d_removed']
    exog = data_train[[ 'Intercept','gov_action','TAVG','lag_confirmed']]
    model = sm.MixedLM(endog, exog, exog_re=exog[[ 'Intercept','lag_confirmed']],  groups=data_train["state"])
    po_results = model.fit()
    print(po_results.summary())

    # Get Coefficient

    k = []
    v1 = []
    v2 = []
    v3 = []
    d = po_results.random_effects

    for i in d:
        my_str = ''.join((ch if ch in '0123456789.-' else ' ') for ch in str(d[i]))
        listOfNumbers = [float(i) for i in my_str.split()]
        v1 = v1 +[str(d[i]).split(" ")[0] ]
        l = str(d[i]).split(" ")
        if str(listOfNumbers[0]).strip()=='':
            v2 = v2 +[0.00 ]
            v3 = v3 + [0.0]
        else:
            v2 = v2 +[listOfNumbers[0] ]
            v3 = v3 +[listOfNumbers[1] ]
        k = k + [i]

    r_combined = pd.DataFrame({'state':k,'coef_name':v1,'coef_value':v2,'re_lag_confirmed':v3})

    r_combined['fe_Intercept'] = po_results.fe_params['Intercept']

    r_combined['Intercept'] = r_combined['fe_Intercept']+r_combined['coef_value']

    r_combined['lag_confirmed'] = po_results.fe_params['lag_confirmed'] +r_combined["re_lag_confirmed"]
    r_combined['gov_action'] = po_results.fe_params['gov_action']
    r_combined['TAVG'] = po_results.fe_params['TAVG']

    r_combined.fillna(0.0)
    r_combined.to_csv("output/simulation_output/recover_coefs.csv")

    mean_beta = np.mean(r_combined[r_combined['lag_confirmed']>0]['lag_confirmed'])
    r_combined['lag_confirmed'] = np.where(r_combined['lag_confirmed']<0,mean_beta,r_combined['lag_confirmed'])

    # Get Prediction and Bias

    t_dat = generate_dataset(data, r_combined)
    if num_date_omit > 0:
        t_dat = t_dat[(t_dat['removed']>0) & ((t_dat['holdout']==0) | (t_dat['dateval']<=temp_start_training_date))]
    else:
        t_dat = t_dat[(t_dat['removed']>0) & (t_dat['holdout']==0)]

    pred_on_train = runSimulator(data1=t_dat,
    coefsdfR=r_combined,
    sir_names=['susceptible','confirmed','death','removed'],
    xnamesr=['Intercept','gov_action','TAVG','lag_confirmed'],
    horizon1=60, date_gov_adjust=0, print_graph=print_graph)

    # == Adjust Prediction of Removed with Bias

    pred_on_train['bias_removed'] = pred_on_train['pred_removed'] - pred_on_train['removed']
    # pred_on_train.to_csv('output/simulation_output/pred_on_train.csv')
    mean_bias = pred_on_train.groupby('location_name')['bias_removed'].mean().reset_index()
    mean_bias.to_csv('output/simulation_output/bias.csv')

    loc_list = set(pred_on_train['location_name'])
    for loc in loc_list:
        bias = mean_bias.loc[mean_bias['location_name']==loc, 'bias_removed'].iloc[0]
        if bias > 0:
            pred_on_train.loc[pred_on_train['location_name']==loc, 'pred_removed'] = pred_on_train.loc[pred_on_train['location_name']==loc, 'pred_removed']-bias
        elif bias < 0:
            pred_on_train.loc[pred_on_train['location_name']==loc, 'pred_removed'] = pred_on_train.loc[pred_on_train['location_name']==loc, 'pred_removed']+bias

        loc_name = "".join(c for c in loc if c.isalnum())
        temp_for_plot = pred_on_train.loc[pred_on_train['location_name']==loc, ['dateval', 'removed', 'pred_removed']]
        plt.figure(figsize=(12,12))
        fig = temp_for_plot.plot(x='dateval', y=['removed', 'pred_removed'], rot=45, ax=plt.gca()).get_figure()
        fig.savefig(os.path.join('output/covid_plot/actual_pred/covid_plot_compare_'+loc_name+'.png'))
        fig.clf()

    pred_on_train['pred_removed'] = np.where(pred_on_train['pred_removed']<0, 0, pred_on_train['pred_removed'])

    pred_on_train.to_csv('output/simulation_output/adjusted_pred_on_train.csv')

    print(pred_on_train)

    # Adjust R-Combined with Bias

    mean_bias2 = mean_bias.copy()
    r_combined2 = r_combined.copy()
    mean_bias2['location_name'] = mean_bias2['location_name'].str.replace('[^a-zA-Z]', '')
    r_combined2['state'] = r_combined2['state'].str.replace('[^a-zA-Z]', '')
    loc_list = set(mean_bias2['location_name'])
    for loc in loc_list:
        bias = mean_bias2.loc[mean_bias2['location_name']==loc, 'bias_removed'].iloc[0]
        if bias > 0:
            r_combined2.loc[r_combined2['state']==loc, 'Intercept'] = r_combined2.loc[r_combined2['state']==loc, 'Intercept']-bias
        elif bias < 0:
            r_combined2.loc[r_combined2['state']==loc, 'Intercept'] = r_combined2.loc[r_combined2['state']==loc, 'Intercept']+bias

    r_combined = r_combined2

    tti = 0

    states = data['state'].drop_duplicates().str.replace('[^a-zA-Z]', '')
    data2 = data.copy()
    data2['state'] = data2['state'].str.replace('[^a-zA-Z]', '')
    data3 = pd.DataFrame()
    for s in states:
        rc = r_combined[r_combined['state']==s]
        #print(rc)
        dat = data2[data2['state']==s]

        if len(dat)>0 and len(rc)>0:
            print(s)
            dat = dat.sort_values(by=['dateval'])
            #dat = dat.reset_index()
            beta = rc['lag_confirmed'].values[0]
            N = dat['population'].values[0]
            dat['susceptible'] = np.where(dat['holdout']==0, N+0.0,0.0)

            alpha = dat['R_0'].values[0]*beta
            dat['alpha'] = alpha

            if tti==0:
               data3 = dat
            else:
                data3 = data3.append(dat,ignore_index=True )
            tti = tti +1

    print(data3)
    print(data3['state'].drop_duplicates())

    # data3.to_csv(path+"/before_sim_data_test.csv")
    #runDynamicSimulator
    #runSimulator(data1,coefsdfR,sir_names,xnamesr,horizon1)
    date_start_sim = 20200510

    if num_date_omit > 0:
        sim_data = data3[(data3['holdout']==1)&(data3['dateval']>=temp_start_simulation_date)]
        print(temp_start_simulation_date)
        print(min(sim_data['dateval']))
    else:
        sim_data = data3[(data3['holdout']==1)]

    sim_data_output_after = runSimulator(data1=sim_data,
    coefsdfR=r_combined,
    sir_names=['susceptible','confirmed','death','removed'],
    xnamesr=['Intercept','gov_action','TAVG','lag_confirmed'],
    horizon1=60, date_gov_adjust=date_start_sim, print_graph=print_graph)
    sim_data_output_after.to_csv("output/simulation_output/simulations_after_adjust_at_"+str(date_start_sim)+"_omitlastD_"+str(num_date_omit)+".csv")

    sim_data_output_before = runSimulator(data1=sim_data,
    coefsdfR=r_combined,
    sir_names=['susceptible','confirmed','death','removed'],
    xnamesr=['Intercept','gov_action','TAVG','lag_confirmed'],
    horizon1=60, date_gov_adjust=0, print_graph=print_graph)
    sim_data_output_before.to_csv("output/simulation_output/simulations_before_adjust_at_"+str(date_start_sim)+"_omitlastD_"+str(num_date_omit)+".csv")

    sim_data_compare = sim_data_output_after.merge(sim_data_output_before, on=['index', 'province_state', 'country','date','dateval','location_name'], suffixes=('_after', '_before'))
    sim_data_compare['diff_susceptible'] = sim_data_compare['pred_susceptible_after'] - sim_data_compare['pred_susceptible_before']
    sim_data_compare['diff_confirmed'] = sim_data_compare['pred_confirmed_after'] - sim_data_compare['pred_confirmed_before']
    sim_data_compare['diff_removed'] = sim_data_compare['pred_removed_after'] - sim_data_compare['pred_removed_before']

    sim_data_compare = sim_data_compare.loc[:, ['province_state', 'country','date','dateval','location_name','pred_susceptible_after', 'pred_confirmed_after', 'pred_removed_after', 'pred_susceptible_before', 'pred_confirmed_before', 'pred_removed_before', 'diff_susceptible', 'diff_confirmed', 'diff_removed']]
    sim_data_compare.to_csv("output/simulation_output/simulations_compare"+str(date_start_sim)+"_omitlastD_"+str(num_date_omit)+".csv")

    if print_graph == True:
        for location in sim_data_compare['location_name'].drop_duplicates():
            dat = sim_data_compare[(sim_data_compare['location_name']==location)].sort_values(by=['date'])
            # plot results
            plt.figure(1)
            plt.figure(figsize=(15,10))
            xtick_locator = AutoDateLocator()
            xtick_formatter = AutoDateFormatter(xtick_locator)
            date_list = pd.to_datetime(dat['dateval'])
            ax = plt.axes()
            ax.xaxis.set_major_locator(xtick_locator)
            ax.xaxis.set_major_formatter(xtick_formatter)
            plt.plot(date_list,dat['diff_susceptible'],'b-')
            plt.plot(date_list,dat['diff_confirmed'],'r--')
            plt.plot(date_list,dat['diff_removed'],'g--')
            plt.xlabel('Time')
            plt.ylabel('Populations')
            plt.title('Compare Before/After Gov. Intervention Adjust at '+''.join(e for e in location if e.isalnum())+' : '+str(date_start_sim))
            plt.legend(['Diff Suceptibes','Diff Confirmed','Diff Removed'])
            plt.savefig(os.path.join('output/covid_plot/covid_plot_compare_'+"omitlastD_"+str(num_date_omit)+''.join(e for e in location if e.isalnum())+'_'+str(date_start_sim)+'.png'))
            plt.clf()
            plt.close()

if __name__=="__main__":

    path="data/simulation_data"
    datasource="dataset_full.csv"
    pop_source="pop_dataset.csv"
    location_name="country_region"
    start_date="2/22/20"
    horizon = 30

    causal_simulation(path=path,start_date=start_date,f_start_date="4/20/20", datafile="dataset_full.csv",govpolicyfile="gov_dates_mandates.csv", print_graph=True, num_date_omit=7)

    exit(0)


import tqdm
import networkx as nx
import logging
import os

from .dynamic_graph import DynamicGraph

logger = logging.getLogger(__name__)

GRAPH_NAME = 'email'
USER_RELATION_NAME = 'sent'
ATTR_RELATION_NAME = 'dept'

def get_user_name(id, dept_id):
    return "dept{}:user{}".format(dept_id, id)

def get_dept_name(id):
    return "dept{}".format(id)

def load_users_relationship(graph, file_path, dept_id, entity2dept, t2entity):
    with open(file_path, 'r') as f:
        lines = f.readlines()
        lines = list(map(lambda line: line.rstrip().split(' '), lines))

        # entity_ids = set()
        time_insts = set()
        for u, v, t in lines:
            u_name = get_user_name(u, dept_id)
            v_name = get_user_name(v, dept_id)
            time_insts.add(t)
            dept_name = get_dept_name(dept_id)

            graph.add_relationship_edge(u_name, USER_RELATION_NAME, v_name, t)

            entity2dept[u_name] = dept_name
            entity2dept[v_name] = dept_name

            entities = t2entity.get(t, set())
            entities.add(u_name)
            entities.add(v_name)
            t2entity[t] = entities

            # u_key = "{}_{}".format(u_name, t)
            # v_key = "{}_{}".format(v_name, t)
            # if u_key in entity_ids:
            #     count_before = graph.get_num_edges(t)

            # graph.add_attribute_edge(u_name, ATTR_RELATION_NAME, dept_name, t)

            # if u_key in entity_ids:
            #     count_after = graph.get_num_edges(t)

            #     assert count_before == count_after, "before: {} - after:{}".format(count_before, count_after)

            # if v_key in entity_ids:
            #     count_v_before=graph.get_num_edges(t)

            # graph.add_attribute_edge(v_name, ATTR_RELATION_NAME, dept_name, t)

            # if v_key in entity_ids:
            #     count_v_after = graph.get_num_edges(t)
            #     assert count_v_before == count_v_after, "before: {} - after: {}".format(count_v_before, count_v_after)

            # entity_ids.add(u_key)
            # entity_ids.add(v_key)

def add_attribute_relationships(graph, entity2dept, t2entity):
    time_ints = graph.time_instances

    for t, entities in tqdm.tqdm(t2entity.items()):
        for entity in entities:
            dept_name = entity2dept[entity]
            graph.add_attribute_edge(entity, ATTR_RELATION_NAME, dept_name, t)
    # for entity, dept_name in tqdm.tqdm(entity2dept.items()):
    #     for t in time_ints:
    #         graph.add_attribute_edge(entity, dept_name, dept_name, t)

class EmailTempGraph(DynamicGraph):
    def __init__(self):
        super().__init__()

    @staticmethod
    def from_raw_file(data_dir, args):
        # load attributes
        # load relationships
        graph = EmailTempGraph()
        entity2dept = {}
        t2entity = {}

        user_relationship_path = os.path.join(data_dir, 'email-Eu-core-temporal.txt')
        load_users_relationship(graph, user_relationship_path, 0, entity2dept, t2entity)

        logger.debug("{} entities2dept: of dept 0".format(len(entity2dept)))
        logger.debug("{} t2entity: of dept 0".format(len(t2entity)))

        logger.info("adding users relationships")
        for dept_id in range(1,5):
            user_relationship_path = os.path.join(data_dir, 'email-Eu-core-temporal-Dept{}.txt'.format(dept_id))
            load_users_relationship(graph, user_relationship_path, dept_id, entity2dept, t2entity)

        logger.info("adding attribute relationships")
        add_attribute_relationships(graph, entity2dept, t2entity)

        return graph


import statsmodels.api as sm
import logging
from housing_model import HousingModel

from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error

import pandas as pd

# this should go to a config file with all the other (future) hyper-parameters
random_state = 237

class OLS(HousingModel):
    def __init__(self):
        super().__init__()

    def predict(self, x, x_test, cv=False):
        """
        IModel's predict functions OLS implementation.

        :param x: training data set
        :param x_test: test data set
        :param cv: True if prediction should be made with cross-validation
        """

        logging.info("Performing OLS prediction...")
        if cv:
            logging.warning("Cross validation not implemented for OLS.")

        # cut off target variable and id from the data
        y = pd.DataFrame(x['SalePrice'], columns=['SalePrice'])
        x.drop(['SalePrice'], axis=1, inplace=True)

        test_ids = x_test.Id
        x_test.drop(['Id'], axis=1, inplace=True)

        # standardize target variable
        mu = y.mean()
        sigma = y.std()

        y = (y - mu) / sigma

        # splitting the data into training and validation sets
        x_train, x_valid, y_train, y_valid = train_test_split(
            x, y, train_size=0.9, test_size=0.1, random_state=random_state
        )

        x_train = sm.add_constant(x_train)
        x_valid = sm.add_constant(x_valid)
        x_test = sm.add_constant(x_test)

        # OLS
        model = sm.OLS(list(y_train.SalePrice), x_train)
        results = model.fit()

        # make predictions for validation data and transform
        y_valid = self.inverse_transform(y_valid, mu, sigma)
        prediction = self.inverse_transform(pd.DataFrame(results.predict(x_valid), columns=['SalePrice']), mu, sigma)

        logging.info(f"OLS validation MAE: {mean_absolute_error(y_valid, prediction)}")

        test_pred = self.inverse_transform(pd.DataFrame(results.predict(x_test), columns=['SalePrice']), mu, sigma)
        test_pred = pd.DataFrame({'Id': test_ids, 'SalePrice': test_pred})
        test_pred.to_csv('submissions\\submission_OLS.csv', index=False)


class Solution:
    def reverseOnlyLetters(self, s: str) -> str:
        # covert s to list
        # initalise two pointer on at 0 the other at one end
        # check if both pointer is at english alphabets
        s_list = list(s)
        l = 0
        r = len(s)-1
        while l < r :
            letter1 = s_list[l]
            letter2 = s_list[r]
            if letter1.isalpha() and letter2.isalpha():
                s_list[l] = letter2
                s_list[r] = letter1
                l += 1
                r -= 1

            elif letter1.isalpha():
                r -= 1

            else:
                l += 1

        return ''.join(s_list)


# Classe principal do código
#
# exec -> python runTests.py caminha_ate_a_pasta_do_projeto
#
# <b>runTests</b>: Esse script controla a execução de todas as partes do algoritmo.
# É nele que são lidas as imagens e estas são passadas para as demais etapadas do algoritmo
# Ao término no processo, ele gera arquivos .csv (tabelas) com os resultados individuais de cada imagem
# e a média geral de todas as execuções.
#
# Explicando resumidamente o código:
#
#
# author:
# Pablo Vinicius - https://github.com/pabloVinicius
#
# contributors:
# Filipe A. Sampaio - https://github.com/filipeas

# imports dos arquivos necessários
from main import vsf # responsável pela separação dos superpixels
from features_extraction import ftet # responsável por extrair as caracteristicas de cada superpixel usadas no modelo
from select_and_classify import classify, select_random_seeds # responsável pela classificação do modelo criado
from statistics import mean, pvariance
import os, csv, time
import numpy as np
import sys

def index(original, marcada):
    # Porcentagens de dados das imagens usadas para treino.
    percentages = [
        # 0.01,
        # 0.05,
        # 0.1,
        # 0.15,
        # 0.2,
        0.25,
        # 0.3,
        # 0.35,
        # 0.4,
        # 0.5
    ]

    # Quantidade de segmentos para o superpixel
    qtdSegments = [
        1500,
        # 2000,
        # 2500,
        # 3000,
        # 4000,
        # 5000
    ]

    # Tenta encontrar o diretório results. Se ele não existir, o cria.
    results_path = 'results/'
    if not os.path.exists(results_path):
        os.makedirs(results_path)

    # Cria a estrutura de dados que irá guardar os resultados das médias das execuções para todas as imagens e para todas as métricas.
    # Funciona como uma matriz  de (Nª de porcentagenx utilizadas)x(número de métricas, que no caso são 4).
    # No final, essa estrutura é utilizada para calcular a média das médias de cada porcentagem de dados pra treino.
    total_means = [[[],[],[],[]] for i in range(len(percentages))]

    # instanciando variavel pra guardar todas as acuracias da imagem corrente
    acuracia_corrent = [[[],[],[],[],[]] for i in range(len(percentages))]

    # instanciando variavel pra guardar todas as sensibilidade da imagem corrente
    sensibilidade_corrent = [[[],[],[],[],[]] for i in range(len(percentages))]

    # instanciando variavel pra guardar todas as especificidade da imagem corrente
    especificidade_corrent = [[[],[],[],[],[]] for i in range(len(percentages))]

    # instanciando variavel pra guardar todas as dice da imagem corrente
    dice_corrent = [[[],[],[],[],[]] for i in range(len(percentages))]

    print(f'\n\n########### Executando a imagem ############\n\n')

    # Marcando o tempo de execução.
    start = time.time()

    # Abrindo a imagem original e a imagem marcada.
    path = f'{original}' # original
    path_marked = f'{marcada}' # marcada

    print(path + '\n' + path_marked)

    # iterando sobre a quantidade de segmentos de imagem para o superpixel
    for segment in qtdSegments:
        # Executa a primeira etapa do algoritmo, responsável pela separação dos superpixels da imagem (arquivo main.py).
        vsf(path, path_marked, segment)

        # Executa a segunda etapa do algoritmo, responsável pela extração de características de cada superpixel gerado na etapa anterior.
        # Disponível no arquivo features_extraction.py
        ftet()

        # Para cada percentual:
        for index, percent in enumerate(percentages):

            # Cria uma estrutura de dados para salvar os resultados e posteriormente calcular as médias das métricas
            metrics_media = [[], [], [], []]

            # Executa 5 vezes para depois tirar a média (5 vezes pois são 5 métricas diferentes)
            for i in range(1):

                # Terceira etapa do algoritmo:
                # Usa as características extraídas na etapa anterior para classificar e gerar as métricas para cada imagem
                # Disponível no arquivo select_and_classify.py
                acc, sen, spe, dice = classify(percent) # classificação por Random Forest
                # acc, sen, spe, dice = select_random_seeds(image, percent) # classificação por sfc-means

                # Coloca as métricas na estrutura de dados
                metrics_media[0].append(acc)
                metrics_media[1].append(sen)
                metrics_media[2].append(spe)
                metrics_media[3].append(dice)

            # Depois adiciona na estrutura de dados os dados gerais
            total_means[index][0].append(mean(metrics_media[0])*100) # acuracia
            total_means[index][1].append(mean(metrics_media[1])*100) # sensibilidade
            total_means[index][2].append(mean(metrics_media[2])*100) # especificidade
            total_means[index][3].append(mean(metrics_media[3])) # dice

            if(segment == 1500):
                acuracia_corrent[index][0].append(mean(metrics_media[0])*100)
                sensibilidade_corrent[index][0].append(mean(metrics_media[1])*100)
                especificidade_corrent[index][0].append(mean(metrics_media[2])*100)
                dice_corrent[index][0].append(mean(metrics_media[3])*100)
            elif(segment == 2000):
                acuracia_corrent[index][1].append(mean(metrics_media[0])*100)
                sensibilidade_corrent[index][1].append(mean(metrics_media[1])*100)
                especificidade_corrent[index][1].append(mean(metrics_media[2])*100)
                dice_corrent[index][1].append(mean(metrics_media[3])*100)
            elif(segment == 2500):
                acuracia_corrent[index][2].append(mean(metrics_media[0])*100)
                sensibilidade_corrent[index][2].append(mean(metrics_media[1])*100)
                especificidade_corrent[index][2].append(mean(metrics_media[2])*100)
                dice_corrent[index][2].append(mean(metrics_media[3])*100)
            elif(segment == 3000):
                acuracia_corrent[index][3].append(mean(metrics_media[0])*100)
                sensibilidade_corrent[index][3].append(mean(metrics_media[1])*100)
                especificidade_corrent[index][3].append(mean(metrics_media[2])*100)
                dice_corrent[index][3].append(mean(metrics_media[3])*100)
            elif(segment == 4000):
                acuracia_corrent[index][4].append(mean(metrics_media[0])*100)
                sensibilidade_corrent[index][4].append(mean(metrics_media[1])*100)
                especificidade_corrent[index][4].append(mean(metrics_media[2])*100)
                dice_corrent[index][4].append(mean(metrics_media[3])*100)

    # calcula o tempo de execução do algoritmo para a imagem atual.
    end = time.time()
    print(f'\n\n########### Fim da execucao da imagem no tempo de {end-start} segundos ############\n\n')
    print("Result = ", acuracia_corrent[0][0], sensibilidade_corrent[0][0], especificidade_corrent[0][0], dice_corrent[0][0], " Fim")
    return acuracia_corrent[0][0], sensibilidade_corrent[0][0], especificidade_corrent[0][0], dice_corrent[0][0]


#!/usr/bin/env python
import re
from functools import partial

import boto3
import requests
from relay_sdk import Dynamic as D
from relay_sdk import Interface

def instance_to_dict(ec2, instance):
    shape = ec2.meta.client.meta.service_model.shape_for('Instance')
    attrs = instance.meta.resource_model.get_attributes(shape)

    d = {}
    for mapped, (name, shape) in attrs.items():
        d[name] = getattr(instance, mapped)

    return d

relay = Interface()

session_token = None
try:
  session_token = relay.get(D.aws.connection.sessionToken)
except:
  pass

sess = boto3.Session(
  aws_access_key_id=relay.get(D.aws.connection.accessKeyID),
  aws_secret_access_key=relay.get(D.aws.connection.secretAccessKey),
  region_name=relay.get(D.aws.region),
  aws_session_token=session_token
)

list_of_filters = []
try:
  pattern = re.compile(r'(?<!^)(?=[A-Z])')

  raw_filters = relay.get(D.filters)
  for key, value in raw_filters.items():
    f = {}
    f['Name'] = pattern.sub('-', key).lower()
    f['Values'] = [value]
    list_of_filters.append(f)
except requests.exceptions.HTTPError as e:
    if e.response.status_code == 422:
        pass
    else:
        raise

ec2 = sess.resource('ec2')

# Get instances
raw_instances = None
if (len(list_of_filters) != 0):
  raw_instances = ec2.instances.filter(Filters=list_of_filters)
else:
  raw_instances = ec2.instances.all()

instance_list = [instance for instance in raw_instances]
if (len(instance_list) == 0):
    relay.outputs.set('instances', list())
    print("No instances found")
    exit(0)

print('Found the following EC2 instances:\n')
print("{:<30} {:<30} {:<30}".format('ID', 'STATE', 'TYPE'))
for instance in raw_instances:
  print("{:<30} {:<30} {:<30}".format(instance.instance_id, instance.state['Name'], instance.instance_type))
instances = list(map(partial(instance_to_dict, ec2), raw_instances))

print('\nAdding {0} instance(s) to the output `instances`'.format(len(instances)))
relay.outputs.set('instances', instances)


from random import randint
from scope import *
from statements import *
from variable import Variable
from copy import deepcopy

class Function:
    def __init__(self, name: str, return_type: DataType, parameters, new = True, scope = Scope()):
        self.name = name
        self.return_type = return_type
        self.parameters = parameters

        self.scope = scope
        self.statements = []

        self.new = new

        for parameter in self.parameters:
            self.scope.add_to_scope(parameter)

    def add_random_statement(self):
        s = randint(0, 4)

    def add_statement(self, statement):
        self.statements.append(statement)

    def __str__(self):

        if self.new:
            s = "{} {} (".format(self.return_type, self.name)

            for parameter in self.parameters:
                s += '{}, '.format(parameter)
            if len(self.parameters) > 0:
                s = s[:-2]
            s+='){\n'

            for statement in self.statements:
                s+= str(statement)

                if not isinstance(statement, (IfStatement, ForStatement, WhileStatement)):
                    s+= ';'

                s += '\n'

            s+='}'

            return s
        else:
            s = '{}('.format(self.name)
            for i in range(len(self.parameters)):
                s += str(self.parameters[i].value)
                if i != len(self.parameters)-1:
                    s += ', '
            s += ')'
            return s

    def get_return_type(self):
        return self.return_type

    def copy(self):
        return Function(self.name, self.return_type, deepcopy(self.parameters, memo=None, _nil=[]), self.new, self.scope)

    @staticmethod
    def random(scope: Scope = None, min_num_statements = 5, max_num_statements = 15):
        if scope == None:
            scope = Scope()
        num_params = random_with_prob(0, 10, 1, 20, 2, 40, 3, 20, 4, 10)

        params = []

        for _ in range(num_params):
            v = Variable.random(scope)
            scope.add_to_scope(v)
            params.append(v)

        func =  Function('func'+str(scope.get_num_functions()), DataType.get_random_data_type(), params, scope=scope)

        num_statements = randint(min_num_statements, max_num_statements)
        last_statement = None
        for i in range(num_statements):

            if isinstance(last_statement, AssignmentStatement) or last_statement == None:
                statement = Statement.random(func.scope, whitelist=[AssignmentStatement, IfStatement, WhileStatement, ForStatement])
                func.add_statement(statement)
                last_statement = statement
            else:
                probs = ['new_scope', 40, 'old_scope', 10]

                p = random_with_prob(probs)

                if p == 'new_scope' or len(last_statement.statements) == 0:
                    statement = Statement.random(last_statement.scope, whitelist=[AssignmentStatement, IfStatement, WhileStatement, ForStatement])

                    last_statement.add_statement(statement)

                    '''
                    if isinstance(last_statement, IfStatement):
                        if (random_with_prob(True, 50, False, 50)):
                            last_statement.add_else_statement( Statement.random(last_statement.scope, whitelist=[AssignmentStatement, IfStatement, WhileStatement, ForStatement]))
                    '''

                    if not isinstance(statement, AssignmentStatement):
                        last_statement = statement
                elif p == 'old_scope':
                    statement = Statement.random(func.scope, whitelist=[AssignmentStatement, IfStatement, WhileStatement, ForStatement])
                    func.add_statement(statement)
                    last_statement = statement

        func.add_statement(ReturnStatement.random(func.scope, func.return_type))

        return func


import json
import uuid

from scrapy.http import HtmlResponse

from qidian.items import *

class WanbenSpider(scrapy.Spider):
    name = 'wanben'
    allowed_domains = []
    start_urls = ['https://www.qidian.com/finish']
    token = ''

    def parse(self, response: HtmlResponse):
        self.token = dict(response.headers).get(b'Set-Cookie')[0].decode('utf-8').split(';')[0].split('=')[-1]

        print(self.token)
        print('sdddd')
        if response.status == 200:
            lis = response.css('.all-img-list li')
            for li in lis:
                item = BookItem()
                item['book_id'] = uuid.uuid4().hex
                a = li.xpath('.//div[@class="book-img-box"]/a')
                item['book_url'] = "https:{}".format(a.xpath('./@href').get())
                item['book_cover'] = "https:{}".format(a.xpath('./img/@src').get())
                item['book_name'] = li.xpath('.//div[2]/h4//text()').get()
                item['author'], *item['tags'] = li.css('.author a::text').extract()
                item['summary'] = li.css('.intro::text').get().strip()
                book_singal = item['book_url'].split('/')[-1]
                detail_url = "https://book.qidian.com/ajax/book/category?_csrfToken={}&bookId={}".format(self.token,
                                                                                book_singal)

                print(detail_url)
                # 请求详情页章节
                yield scrapy.Request(detail_url, callback=self.parse_info,
                                     meta={'book_id': item['book_id']})

                yield item
            next_page = response.css('.lbf-pagination-item-list').xpath('./li[last()]/a/@href').get()
            if next_page.find('javascript') == -1:  # 存在下一页
                yield scrapy.Request('https:{}'.format(next_page))

    def parse_info(self, response: HtmlResponse):
        """解析小说的详情页章节"""
        book_id = response.meta['book_id']
        chapters = json.loads(response.text).get("data").get('vs')

        for chapter in chapters:
            cs = chapter.get('cs')
            for data in cs:
                item = SegItem()
                item['seg_id'] = uuid.uuid4().hex
                item['book_id'] = book_id
                item['url'] = "https://read.qidian.com/chapter/{}".format(data.get('cU'))  # 章节详情标识
                item['title'] = data.get('cN')  # 章节标题
                yield scrapy.Request(item['url'], callback=self.parse_seg, meta={'seg_id': item['seg_id']})
                yield item

    def parse_seg(self, response):
        """解析章节的小说内容"""
        print("-" * 50, "进入章节内容", "-" * 50)
        item = SegDetailItem()
        item['seg_id'] = response.meta['seg_id']
        contents = "".join(response.css('.read-content p::text').extract())
        item['content'] = contents.replace('\u3000', '').replace('\n', '').strip()
        yield item


#!/usr/bin/env python3
# _coding_: utf8

import json
import sys
import time
import traceback
import random as rd
import pandas as pd
from threading import Thread
from broker.client import BrokerClient

class icoMarketMaking(object):

    def __init__(self, symbol, pm):
        self.symbol = symbol
        self.pm = pm

        self.api = BrokerClient(entry_point=self.pm.get('entry_point', 'https://api.bhex.com/openapi/'), api_key=self.pm['api_key'], secret=self.pm['secret'])

        # self.api = BhexClient(api_key=self.pm['api_key'], secret=self.pm['secret'], entry_point=self.pm.get('entry_point', 'https://api.bhex.com/openapi/'))
        self.imitate_symbol = self.pm.get('imitate_symbol', '')

        # load order config file
        self.tgtStructBid = pd.DataFrame.from_dict(self.pm['tgtStructBid'])
        self.tgtStructAsk = pd.DataFrame.from_dict(self.pm['tgtStructAsk'])
        # print(self.tgtStructBid.loc[str(1), 'fromBestBid'], self.tgtStructAsk)

        self.orderThread = None
        self.klineThread = None

    def start(self):
        orders = self.api.open_orders(symbol=self.symbol, limit=1000)
        self.tgtStructBid['cid'] = [''] * len(self.tgtStructBid)
        self.tgtStructAsk['cid'] = [''] * len(self.tgtStructAsk)

        orders = sorted(orders, key=lambda x: float(x['price']), reverse=True)
        i = 0
        for o in orders:
            if o['side'] == 'BUY' and o['clientOrderId'][:len(self.pm['orderPrefix'])] == self.pm['orderPrefix']:
                if i < self.pm['orderNum']:
                    self.tgtStructBid.loc[str(i), 'cid'] = o['clientOrderId']
                    i += 1
                else:
                    self.api.order_cancel(client_order_id=o['clientOrderId'])

        i = 0
        for o in reversed(orders):
            if o['side'] == 'SELL' and o['clientOrderId'][:len(self.pm['orderPrefix'])] == self.pm['orderPrefix']:
                if i < self.pm['orderNum']:
                    self.tgtStructAsk.loc[str(i), 'cid'] = o['clientOrderId']
                    i += 1
                else:
                    self.api.order_cancel(client_order_id=o['clientOrderId'])

        self.orderThread = Thread(target=self.orderWorker)
        self.orderThread.start()

        self.klineThread = Thread(target=self.drawKline)
        self.klineThread.start()

    # to calculate the fair bid/ask price
    def getFairPrice(self):
        try:
            depth = self.api.depth(symbol=self.symbol, limit=100)
            print(str(depth['bids']))
            bids = depth['bids']
            pqsum = qsum = 0
            for b in bids:
                qsum += float(b[1])
                pqsum += float(b[0]) * float(b[1])
                if qsum >= self.pm['fairBidPriceQty']:
                    break
            fair_bid_price = pqsum / qsum

            asks = depth['asks']
            pqsum = qsum = 0
            for a in asks:
                qsum += float(a[1])
                pqsum += float(a[0]) * float(a[1])
                if qsum >= self.pm['fairAskPriceQty']:
                    break
            fair_ask_price = pqsum / qsum

            if self.imitate_symbol:
                tgt = self.api.klines(symbol=self.imitate_symbol, limit=1)[0]
                chg = float(tgt[4]) / float(tgt[1])
                fair_bid_price *= chg
                fair_ask_price *= chg

            return fair_bid_price, fair_ask_price
        except:
            traceback.print_exc()

    # order placing fn, will check one pair of bid and ask at one time
    def orderWorker(self):
        while True:
            for i in reversed(range(self.pm['orderNum'])):
                print(i)
                try:
                    fair_bid_price, fair_ask_price = self.getFairPrice()
                    # print(fair_bid_price, fair_ask_price)
                    fair_mid_price = 0.5 * (fair_bid_price + fair_ask_price)
                    best_bid = fair_mid_price * (1 - self.pm['bid_spread'])
                    best_ask = fair_mid_price * (1 + self.pm['ask_spread'])

                    send_flag = True
                    price = best_bid * (1 - self.tgtStructBid.loc[str(i), 'fromBestBid'])
                    order = self.api.order_get(orig_client_order_id=self.tgtStructBid.loc[str(i), 'cid'])
                    print(str(order))
                    try:
                        if order['status'] == 'FILLED':
                            None
                        elif abs(float(order['price']) - price) > 0.0005 * price:
                            self.api.order_cancel(client_order_id=self.tgtStructBid.loc[str(i), 'cid'])
                        else:
                            send_flag = False
                    except:
                        traceback.print_exc()
                        print(order)

                    if send_flag:
                        params = {'symbol': self.symbol, 'side': 'BUY', 'type': 'LIMIT_MAKER',
                                  'quantity': self.pm['orderQty'] * self.tgtStructBid.loc[str(i), 'qtyMultiplier'] * (
                                          1 + 0.6 * (rd.random() - 0.5)),
                                  'price': price * (1 + 0.0005 * (rd.random() - 0.5)),
                                  'newClientOrderId': self.pm['orderPrefix'] + str(int(time.time() * 1000)) + 'b' + str(i)}
                        self.prepareOrder(params)
                        self.tgtStructBid.loc[str(i), 'cid'] = params['newClientOrderId']

                    send_flag = True
                    price = best_ask * (1 + self.tgtStructAsk.loc[str(i), 'fromBestAsk'])
                    order = self.api.order_get(orig_client_order_id=self.tgtStructAsk.loc[str(i), 'cid'])
                    try:
                        if order['status'] == 'FILLED':
                            None
                        elif abs(float(order['price']) - price) > 0.0005 * price:
                            self.api.order_cancel(client_order_id=self.tgtStructAsk.loc[str(i), 'cid'])
                        else:
                            send_flag = False
                    except:
                        traceback.print_exc()
                        print(order)

                    if send_flag:
                        params = {'symbol': self.symbol, 'side': 'SELL', 'type': 'LIMIT_MAKER',
                                  'quantity': self.pm['orderQty'] * self.tgtStructAsk.loc[str(i), 'qtyMultiplier'] * (
                                          1 + 0.6 * (rd.random() - 0.5)),
                                  'price': price * (1 + 0.0005 * (rd.random() - 0.5)),
                                  'newClientOrderId': self.pm['orderPrefix'] + str(int(time.time() * 1000)) + 'a' + str(i)}
                        self.prepareOrder(params)
                        self.tgtStructAsk.loc[str(i), 'cid'] = params['newClientOrderId']

                except:
                    traceback.print_exc()

                time.sleep(self.pm.get('orderRefreshInterval', 5))

    # draw k line fn
    def drawKline(self):
        while True:
            try:
                res = self.api.ticker_24hr(symbol=self.symbol)
                # print(res)
                best_bid = float(res['bestBidPrice'])
                best_ask = float(res['bestAskPrice'])

                cid = str(int(time.time()))

                order = {'symbol': self.symbol,
                         'quantity': self.pm['orderQty'] * (0.2 + 1.6 * rd.random()),
                         'price': round(best_bid + (best_ask - best_bid) * rd.random(), self.pm['priceDecimalLength']),
                         'newClientOrderId': cid}

                qty = order['quantity']

                if best_bid + 0.5 * (best_ask - best_bid) < order['price'] < best_ask or order['price'] == best_ask:
                    order['side'] = 'SELL'
                    order['type'] = 'LIMIT_MAKER'
                    self.prepareOrder(order)
                    # time.sleep(3 + 7 * rd.random())
                    order.pop('newClientOrderId')
                    order['side'] = 'BUY'
                    order['type'] = 'LIMIT'
                    order['timeInForce'] = 'IOC'
                    order['quantity'] = qty * (0.1 + 0.8 * rd.random())
                    self.prepareOrder(order)
                    # time.sleep(2 + 5 * rd.random())
                    if rd.random() < 0.2:
                        order['quantity'] = qty - order['quantity']
                        self.prepareOrder(order)
                    self.api.order_cancel(client_order_id=cid)

                elif best_bid < order['price'] < best_bid + 0.5 * (best_ask - best_bid) or order['price'] == best_bid:
                    order['side'] = 'BUY'
                    order['type'] = 'LIMIT_MAKER'
                    self.prepareOrder(order)
                    # time.sleep(3 + 7 * rd.random())
                    order.pop('newClientOrderId')
                    order['side'] = 'SELL'
                    order['type'] = 'LIMIT'
                    order['timeInForce'] = 'IOC'
                    order['quantity'] = qty * (0.1 + 0.8 * rd.random())
                    self.prepareOrder(order)
                    # time.sleep(2 + 5 * rd.random())
                    if rd.random() < 0.2:
                        order['quantity'] = qty - order['quantity']
                        self.prepareOrder(order)
                    self.api.order_cancel(client_order_id=cid)

            except:
                traceback.print_exc()
                print(res)

            time.sleep(10 + 30 * rd.random())

    # random cancel order fn
    def randomCancel(self):
        try:
            orders = self.api.open_orders(symbol=self.symbol, limit=1000)
            i = int(rd.random() * len(orders))
            self.api.order_cancel(client_order_id=orders[i]['clientOrderId'])
            print('randomCancel', orders[i])
        except:
            traceback.print_exc()

    # send order fn
    def prepareOrder(self, params):
        params['price'] = round(params['price'], self.pm['priceDecimalLength'])
        params['quantity'] = round(params['quantity'], self.pm['qtyDecimalLength'])
        res = self.api.order_new(**params)
        if res.get('msg', '') == 'Balance insufficient ':
            print(res, params)
            self.randomCancel()
            print(self.api.account())
        elif 'msg' in res:
            print(res, params)
        # else:
        #     print(res, params)

if __name__ == '__main__':

    # try:
    #     symbol = str.upper(sys.argv[1])
    # except:
    #     symbol = ''
    #     print("NEED SYMBOL")
    #     exit(0)

    symbol = 'EEGUSDT'

    with open("parameter.json") as f:
        json_file = json.load(f)

    a = icoMarketMaking(symbol=symbol,
                        pm=json_file[symbol])

    a.start()


import boto3
from boto3.dynamodb.conditions import Key
from cStringIO import StringIO
import json
import decimal
import logging

LOG = logging.getLogger()
LOG.setLevel(logging.DEBUG)
BUCKET = "testing.submityoursalary.com"
PENDING_KEY = "pending-reviews/pending-reviews.json"
REVIEW_KEY = "reviews/reviews.json"
SUBMISSIONS_TABLE_NAME = 'submityoursalary-submissions'

def value_convert(v):
    if isinstance(v, decimal.Decimal):
        if v % 1 > 0:
            return float(v)
        else:
            return int(v)
    return v

def delete_item(key_id):
    dynamo_submissions = b.resource('dynamodb').Table(SUBMISSIONS_TABLE_NAME)
    dynamo_submissions.delete_item(Key={'id': key_id})

b = boto3.session.Session(profile_name='submityoursalary', region_name='us-east-1')
s = b.resource('dynamodb').Table(SUBMISSIONS_TABLE_NAME)
s3 = b.resource('s3')

FIELDS = ["id", "emoji", "company", "create_time", "position", "review"]

pending = Key('approve').lt(2)
approved = Key('approve').gte(2)
rejected = Key('reject').gte(2)

# rejected
r = s.scan(FilterExpression=rejected)
if 'Items' in r:
    for i in r['Items']:
        LOG.info("Deleting {}".format(i['id']))
        print("Deleting {}".format(i['id']))
        delete_item(i['id'])

# pending
p = s.scan(FilterExpression=pending)
if 'Items' in p:
    pending = [{k: value_convert(d[k]) for k in FIELDS} for d in p['Items']]
    pending_contents = StringIO(json.dumps(pending, indent=4, sort_keys=True))
    bucket = s3.Bucket(BUCKET)
    bucket.put_object(ACL='public-read', Bucket=BUCKET, Key=PENDING_KEY, Body=pending_contents.read())

# approved
a = s.scan(FilterExpression=approved)
if 'Items' in a:
    approved = [{k: value_convert(d[k]) for k in FIELDS} for d in a['Items']]
    approved_contents = StringIO(json.dumps(approved, indent=4, sort_keys=True))
    bucket = s3.Bucket(BUCKET)
    bucket.put_object(ACL='public-read', Bucket=BUCKET, Key=REVIEW_KEY, Body=approved_contents.read())


import threading, time, socket, datetime, json, copy
from gpiozero import PWMOutputDevice

# Set the to be loaded slots. Has to be full paths or else it won't start on boot!
play1 = "/home/pi/Desktop/whos_afraid/slot_1.json"

# Set the UPD port here
UDP_PORT = 6006
# Set the debug level, 0 = no debug messages, 1 = UDP messages, 2 = inverter messages, 3 = both
DEBUG = 2

inv_1 = PWMOutputDevice("BOARD37")
inv_2 = PWMOutputDevice("BOARD35")
inv_3 = PWMOutputDevice("BOARD33")
inv_4 = PWMOutputDevice("BOARD31")
inv_5 = PWMOutputDevice("BOARD29")
inv_6 = PWMOutputDevice("BOARD27")

##
## AFTER THIS IS CODE.
##

# Declare variables
data = ''   # Declare an empty variable
playing = False
stop_player = False

# UDP setup for listening
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setblocking(1)
sock.bind(('', UDP_PORT))
#Set console color for easier UDP income reading
CRED = '\033[91m'
CEND = '\033[0m'

#load composition 1
print(f"opening {play1}")
f = open(play1, "r")
recording = json.loads(f.read())
rec_dict1 = {entry["time"]:entry["values"] for entry in recording}
if DEBUG == 2:
    print(rec_dict1)
last_time1 = list(recording)[-1]["time"]
print(f"{play1} is playing for {last_time1} seconds")

# Define a function for the thread
def listening_thread():
    global data
    global addr
    global stop_player
    while True:
        data_raw, addr = sock.recvfrom(1024)
        data = data_raw.decode()    # My test message is encoded
        if DEBUG == 1 or DEBUG == 3:
            print(f"{CRED}{datetime.datetime.now().time()} UDP MESSAGE: {data}{CEND}")
        decode_list = data.split()
        if decode_list[0].startswith("stop"):                 # if the list starts with "stop"
            print("trying to stop player")
            stop_player = True
            print(decode_list[0],decode_list[1])                    # debug purposes
        elif decode_list[0].startswith("exit"):                 # if the list starts with "exit"
            sock.close()                                            # close the socket
            exit()
# Load the thread
try:
    threading.Thread(target=listening_thread).start()
except:
    print ("Error: unable to start thread")
    quit()

# Define a function for the composition playback
def player (dict, last_entry, slot, stop):
    global playing
    global stop_player
    global pin
    global pwm
    playing = True
    stop_player = False
    # dict_copy = copy.deepcopy(dict)
    print(f"last_entry from {slot}: {last_entry}")
    t0 = time.time()
    while True:
        t1 = time.time() - t0
        # print ('time: ', t1)
        t_check = round(t1, 3)
        values = dict.get(t_check, None)
        if values:
            print(f"{datetime.datetime.now().time()} time: {t_check} value: {values}")
            inv_1.value = values[0]
            inv_2.value = values[1]
            inv_3.value = values[2]
            inv_4.value = values[3]
            inv_5.value = values[4]
            inv_6.value = values[5]
        if t1 >= last_entry:
            print(f"{datetime.datetime.now().time()} done playing {slot}")
            t0 = 0
            t1 = 0
            playing = False
            return
        elif stop is True:
            print("Stopped before finishing. Stopping thread now.")
            return

# Check if there's something playing and if not, starts a player thread
while True:
    if playing is False:
        # threading.Thread(target=player,
        #                      args=(rec_dict1, last_time1, play1, stop_player),
        #                      kwargs={},
        #                 ).start()
        player(rec_dict1, last_time1, play1, stop_player)


from Modules import printer

import datetime
import json

with open('Text.json','r') as fObj:
    Text = json.load(fObj)

Channel = printer.Queue('General',Testing=True)
Start = datetime.datetime.now()
for x in Text:
    Offset = datetime.timedelta(seconds=x[0])
    Channel.Add(x[2],Start+Offset,x[1])


#!/usr/bin/env python3

import arbor
import pandas
import seaborn
import sys

try:
    from bluepyopt import ephys
except ImportError:
    raise ImportError("Please install bluepyopt to run this example.")

# (1) Read the cell JSON description referencing morphology, label dictionary and decor.

if len(sys.argv) < 2:
    print("No JSON file passed to the program")
    sys.exit(0)

cell_json_filename = sys.argv[1]
cell_json, morpho, decor, labels = ephys.create_acc.read_acc(cell_json_filename)

# (2) Define labels for stimuli and voltage recordings.

labels["soma_center"] = "(location 0 0.5)"
labels["dend1"] = (
    '(restrict-to (distal-translate (proximal (region "apic")) 660)'
    " (proximal-interval (distal (branch 123))))"
)

# (3) Define stimulus and spike detector, adjust discretization

decor.place(
    '"soma_center"', arbor.iclamp(tstart=295, duration=5, current=1.9), "soma_iclamp"
)

# Add spike detector
decor.place('"soma_center"', arbor.threshold_detector(-10), "detector")

# Adjust discretization (single CV on soma, default everywhere else)
decor.discretization(arbor.cv_policy_max_extent(1.0) | arbor.cv_policy_single('"soma"'))

# (4) Create the cell.

cell = arbor.cable_cell(morpho, decor, labels)

# (5) Declare a probe.

probe = arbor.cable_probe_membrane_voltage('"dend1"')

# (6) Create a class that inherits from arbor.recipe
class single_recipe(arbor.recipe):
    # (6.1) Define the class constructor
    def __init__(self, cell, probes):
        # The base C++ class constructor must be called first, to ensure that
        # all memory in the C++ class is initialized correctly.
        arbor.recipe.__init__(self)
        self.the_cell = cell
        self.the_probes = probes

        self.the_props = arbor.neuron_cable_properties()

        # Add catalogues with explicit qualifiers
        self.the_props.catalogue = arbor.catalogue()
        self.the_props.catalogue.extend(arbor.default_catalogue(), "default::")
        self.the_props.catalogue.extend(arbor.bbp_catalogue(), "BBP::")

    # (6.2) Override the num_cells method
    def num_cells(self):
        return 1

    # (6.3) Override the num_targets method
    def cell_kind(self, gid):
        return arbor.cell_kind.cable

    # (6.4) Override the cell_description method
    def cell_description(self, gid):
        return self.the_cell

    # (6.5) Override the probes method
    def probes(self, gid):
        return self.the_probes

    # (6.6) Overrode the global_properties method
    def global_properties(self, gid):
        return self.the_props

# Instantiate recipe
# Pass the probe in a list because that it what single_recipe expects.
recipe = single_recipe(cell, [probe])

# (7) Create a simulation (using defaults for context and partition_load_balance)
sim = arbor.simulation(recipe)

# Instruct the simulation to record the spikes and sample the probe
sim.record(arbor.spike_recording.all)

probe_id = arbor.cell_member(0, 0)
handle = sim.sample(probe_id, arbor.regular_schedule(0.02))

# (8) Run the simulation
sim.run(tfinal=600, dt=0.025)

# (9) Print or display the results
spikes = sim.spikes()
print(len(spikes), "spikes recorded:")
for s in spikes:
    print(s)

data = []
meta = []
for d, m in sim.samples(handle):
    data.append(d)
    meta.append(m)

df_list = []
for i in range(len(data)):
    df_list.append(
        pandas.DataFrame(
            {
                "t/ms": data[i][:, 0],
                "U/mV": data[i][:, 1],
                "Location": str(meta[i]),
                "Variable": "voltage",
            }
        )
    )
df = pandas.concat(df_list, ignore_index=True)
seaborn.relplot(
    data=df,
    kind="line",
    x="t/ms",
    y="U/mV",
    hue="Location",
    col="Variable",
    errorbar=None,
).savefig("single_cell_bluepyopt_l5pc_bAP_dend1.svg")


'''
Google Movie Showtimes parser class for Python.

This script provides a Python class that can be used to parse Google Movie
Showtimes (www.google.com/movies) pages into dictionary objects.

@author Vaidik Kapoor
@version 0.1
'''

import httplib, urllib, BeautifulSoup, re
from copy import deepcopy
from BeautifulSoup import BeautifulSoup

'''
GoogleMovieShowtimes class
    This class is used for getting response from www.google.com/movies
'''
class IMDB:

    def __init__(self):
        return

    def getIDs(self, name):
        self.params = {'q': name.encode("latin_1")}

        params = deepcopy(self.params)
        for key, val in params.iteritems():
            if val == '':
                self.params.pop(key)
        params = urllib.urlencode(self.params)

        conn = httplib.HTTPConnection('www.imdb.com')
        conn.request("GET", "/find?" + params, "")

        response = conn.getresponse()
        self.response_code = response.status
        self.response = response.getheaders

        self.response_body = ""
        while 1:
            data = response.read()
            if not data:
                break
            self.response_body += data

        if (self.response_code == 200):
            self.html = BeautifulSoup(self.response_body)

        results = self.html.findAll('td', attrs={'class': 'result_text'})
        self.ids = []
        for td in results:
          self.ids.append(td.a.attrs[0][1])

        return self.ids

    def getTitleExtra(self, title):

        conn = httplib.HTTPConnection('www.imdb.com')
        conn.request("GET", title, "")

        response = conn.getresponse()
        self.response_code = response.status
        self.response = response.getheaders

        self.response_body = ""
        while 1:
            data = response.read()
            if not data:
                break
            self.response_body += data

        if (self.response_code == 200):
            self.html = BeautifulSoup(self.response_body)

        results = self.html.findAll('span', attrs={'class': 'title-extra'})
        self.titles = []
        for span in results:
          title = span.contents[0]
          title = title.replace('\n', ' ')
          title = title.replace('"', ' ')
          title = title.strip()
          self.titles.append(title)

        return self.titles


import random
import time
#speed for time.sleep()
speed = 0.3
Snakes={}

#Gets the names for the players
def get_player_names():
    PlayerName1 = str(input("----- Enter Player 1 name = "))
    PlayerName2 = str(input("----- Enter Player 2 name = "))
    return PlayerName1, PlayerName2

#Checks if the names are properly included
def playerConfirmation(PlayerName1, PlayerName2):
    print("\nPlayer 1 is: " + PlayerName1 + " and " + "player 2 is: " +
          PlayerName2 + "\n")

#Dice roll function
def dice_roll(User):
    dice_value = random.randint(1, 6)
    print(f"{User} rolled a {dice_value}")
    return dice_value

def Snakes_and_ladder_count():
    numsnakes = int(input("Enter the number of snakes: ") or 0)
    numladders = int(input("Enter the number of ladders: ") or 0)
    return numsnakes, numladders

snake_head_values = []
snake_tail_values = []

def SnakeMaker(headlist, taillist, numsnakes):
    for i in range(0, numsnakes):
      headNumber  = random.randint(20, 90)
      headlist.append(headNumber)
      taillist.append(random.randint(5, headNumber))

    global Snakes
    Snakes = {}
    for j in range(0, len(headlist)):
        Snakes.update({headlist[j]: taillist[j]})
    print(Snakes)
    return Snakes

ladder_low_values = []
ladder_high_values = []

def LadderMaker(lowlist, highlist, numladder):
    for i in range(0, numladder):
        lowlist.append(random.randint(0, 20))
        highlist.append(random.randint(30, 90))

    global Ladders
    Ladders = {}
    for k in range(0, len(lowlist)):
        Ladders.update({lowlist[k]: highlist[k]})
    return Ladders

#Calculates the new position after rolling dice. Includes basic movement, snakes, and ladder.
def new_position(User, curr_pos, dice_value):
    old_pos = curr_pos
    curr_pos = curr_pos + dice_value

    if curr_pos > 100:
        print(f"Sorry, you need a {100 - old_pos} to win\n")
        return old_pos
    if curr_pos in Snakes:
        final_curr_pos = Snakes.get(curr_pos)
        #print(final_curr_pos)
        print(f'''Oh no, {User} landed on a SNAKE.
~~~~~ You move down to {final_curr_pos} ~~~~~\n''')

    elif curr_pos in Ladders:
        final_curr_pos = Ladders.get(curr_pos)
        print(f'''Wow, {User} landed on a LADDER.
##### You climb up to {final_curr_pos} #####\n''')

    else:
        final_curr_pos = curr_pos
        print(f'''----- {User} moved to {final_curr_pos} -----\n''')

    return final_curr_pos

#Checks if there is a winner
def check_if_win(User, curr_pos):
    if curr_pos == 100:
        print(f"\n ----- Congratulations {User}, YOU WIN! -----\n")
        return True

def play_quit(User1, User2):
    quit = "q"
    user_input = str(
        input(f"\n {User1} type q to leave or enter to play on: "))
    if user_input.upper() == quit.upper():
        print(f"{User1} has quit the game")
        print(f"\n ----- Congratulations {User2}, YOU WIN! -----\n")
        return True
    else:
        return False

#This function will combine all the elements together in one function
def StartGame():
    User1_curr = 0
    User2_curr = 0

    PlayerName1, PlayerName2 = get_player_names()

    playerConfirmation(PlayerName1, PlayerName2)

    numsnakes, numladders = Snakes_and_ladder_count()

    SnakeMaker(snake_head_values, snake_tail_values, numsnakes)
    LadderMaker(ladder_low_values, ladder_high_values, numladders)

    #This loop is the actual gameplay. Loop will only break once there is a winner by the check_if_win() function
    while True:
        if play_quit(PlayerName1, PlayerName2):
            break
        dice_value = dice_roll(PlayerName1)
        time.sleep(speed)
        User1_curr = new_position(PlayerName1, User1_curr, dice_value)
        time.sleep(speed)
        if check_if_win(PlayerName1, User1_curr):
            break

        if play_quit(PlayerName2, PlayerName1):
            break
        dice_value = dice_roll(PlayerName2)
        time.sleep(speed)
        User2_curr = new_position(PlayerName2, User2_curr, dice_value)
        time.sleep(speed)
        if check_if_win(PlayerName2, User2_curr):
            break

StartGame()


class SetCover():
    # initializes the object with the dataset, parses the data
    def __init__(self, inputFile, printData=False):
        with open(inputFile) as f:
            content = f.read()
            lines = content.split("\n")
            firstLine = lines[0].split()
            # m is the number of rows, which correspond to elements
            self.m = int(firstLine[0])
            # n is the number of columns, which correspond to sets
            self.n = int(firstLine[1])
            # sets is a list where sets[i] corresponds to the weight of the i-th set
            sets = []
            # elements is a list of lists where elements[i] is the set of sets that contain element i
            elements = []
            temp = []
            for i in range(1, len(lines)):
                line = lines[i].split()
                if len(sets) < self.n:
                    for weight in line:
                        sets.append(int(weight))
                elif len(line) == 1:
                    if len(temp) != 0:
                        elements.append(temp)
                        temp = []
                    else:
                        continue
                else:
                    for setNumber in line:
                        temp.append(int(setNumber))
            elements.append(temp)
            self.sets = sets
            self.elements = elements
            if printData:
                print(f"m is {self.m}")
                print(f"n is {self.n}")
                print(self.sets)
                print(self.elements)

    # the Integer Linear Program version of Set Cover, solved exactly using the CP-SAT ILP Solver
    def solveILP(self, timeLimit=None, printData=False):
        from ortools.sat.python import cp_model
        model = cp_model.CpModel()
        elements = self.elements
        sets = self.sets

        # indicator variables for whether or not the i-th set is included in the solution
        set_vars = [model.NewBoolVar(f'indicator for set {i}') for i in range(self.n)]

        # constraint that each vertex needs to be covered
        for element in elements:
            setCoverConstraint = 0
            for elementSet in element:
                setCoverConstraint += set_vars[elementSet - 1]
            model.Add(setCoverConstraint >= 1)

        # minimization of the total weight
        total_weight = 0
        for i in range(len(sets)):
            total_weight += (sets[i] * set_vars[i])
        model.Minimize(total_weight)

        # solve the constraint program and print the results
        solver = cp_model.CpSolver()
        if timeLimit is not None:
            solver.parameters.max_time_in_seconds = timeLimit
        from timeit import default_timer as timer
        t = timer()
        result = solver.Solve(model)
        if result == cp_model.OPTIMAL:
            answer = 0
            for j in range(len(sets)):
                answer += (solver.Value(set_vars[j]) * sets[j])
            if printData:
                print(f'the ILP scheme takes time {timer() - t} seconds')
                print(f'the optimal set cover weight is {answer}')
            return (timer() - t, answer)
        elif result == cp_model.FEASIBLE:
            answer = 0
            for j in range(len(sets)):
                answer += (solver.Value(set_vars[j]) * sets[j])
            if printData:
                print(f'the ILP scheme takes time {timer() - t} seconds')
                print(f'the feasible set cover weight is {answer}')
            return (timer() - t, answer)
        else:
            print('this instance of Set Cover is not solvable')

    # the Linear Programming + Randomized Rounding Scheme version of Set Cover, approximated by using the GLOP solver and rounding scheme
    def solveLP_Rounding(self, printData=False):
        from ortools.linear_solver import pywraplp
        import random
        solver = pywraplp.Solver('set_cover_LP_solver', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
        elements = self.elements
        sets = self.sets
        # indicator variables for whether or not the i-th set is included in the solution
        set_vars = [solver.NumVar(0, 1, f'indicator for set {i}') for i in range(self.n)]
        # constraint that each vertex needs to be covered
        for element in elements:
            setCoverConstraint = 0
            for elementSet in element:
                setCoverConstraint += set_vars[elementSet - 1]
            solver.Add(setCoverConstraint >= 1)
        # minimization of the total weight using linear programming
        total_weight = 0
        for i in range(len(sets)):
            total_weight += (sets[i] * set_vars[i])
        solver.Minimize(total_weight)

        # solve the constraint program
        from timeit import default_timer as timer
        t = timer()
        if solver.Solve() == pywraplp.Solver.OPTIMAL:
            set_answers = [set_vars[i].solution_value() for i in range(len(set_vars))]
        else:
            print("impossible to solve this instance of set cover!")
            return

        # randomzied rounding scheme: add the set to the solution with probability corresponding to the value of the variable. Run this log n times
        answer = 0
        counter = 1
        added = set()
        while (counter < self.n):
            for setElem in range(len(sets)):
                if random.random() < set_answers[setElem]:
                    added.add(setElem)
            counter = counter * 2
        for addedSet in added:
            answer += sets[addedSet]

        # for elements that are not covered after the rounding scheme, add the minimum weight set that covers it
        for element in elements:
            covered = False
            for elementSet in element:
                if (elementSet - 1) in added:
                    covered = True
                    break
            if not covered:
                elementWeights = [sets[i - 1] for i in element]
                answer += min(elementWeights)
        # print the results
        if printData:
            print(f'the LP Rounding scheme takes time {timer() - t} seconds')
            print(f'the optimal set cover weight is {answer}')

        return (timer() - t, answer)


class UnionFind:
    def __init__(self, nodes):
        self.parents = nodes
        self.rank = [0 for _ in range(len(nodes))]

    def root(self, i):
        while self.parents[i] != i:
            i = self.parents[i]
        return i

    def same(self, i, j):
        return self.root(i) == self.root(j)

    def union(self, i, j):
        a = self.root(i)
        b = self.root(j)
        if self.rank[a] < self.rank[b]:
            self.parents[a] = b
        elif self.rank[b] < self.rank[a]:
            self.parents[b] = a
        else:
            self.parents[a] = b
            self.rank[b] += 1

n, m = input().split()
n, m = int(n), int(m)

dsu = UnionFind(nodes=[i for i in range(n+1)])

edge_freq = {i: {} for i in range(1, n+1)}

edges_og = []

for _ in range(m):
    u, v, w = input().split()
    u, v, w = int(u), int(v), int(w)

    edge_freq[u][w] = edge_freq[u].get(w, 0) + 1
    edge_freq[v][w] = edge_freq[v].get(w, 0) + 1

    edges_og.append((w, u, v))

edges = edges_og[:]
edges.sort()

res = {}

for w, u, v in edges:
    # check both u and v nodes to see if there are any duplicate weights
    if edge_freq[u][w] == 1 and edge_freq[v][w] == 1 and not dsu.same(u, v):
        dsu.union(u, v)
        res[(u, v)] = "any"
    elif edge_freq[u][w] > 1 or edge_freq[v][w] > 1 and not dsu.same(u, v):
        dsu.union(u, v)
        res[(u, v)] = "at least one"
    else:
        res[(u, v)] = "none"

for w, u, v in edges_og:
    print(res[(u, v)])


from common_analysis_base import AnalysisPlugin, FileAnalysisMixin, URLAnalysisMixin, IPAnalysisMixin, DomainAnalysisMixin
from common_helper_files import md5sum
from . import __version__
from virus_total_apis import PrivateApi as VirustotalApi
import logging
import time
import datetime
import random
import threading

logger = logging.getLogger('CommonAnalysisVirusTotal')
logger.setLevel(logging.INFO)

system_version = __version__

VT_FILE_ANALYZIS_PENDING = -2
VT_FILE_UNKOWN = 0
VT_FILE_REPORT_SUBMITTED = 1

class DelayedVirustotalApi():
    last_request_time = datetime.datetime(1970, 1, 1)
    lock = threading.Lock()

    def __init__(self, api_key, backoff_time):
        self.api = VirustotalApi(api_key)
        self.backoff_time = backoff_time
        random.seed()
        rand_seconds = random.randrange(3, 16)
        self.last_request_time = datetime.datetime.now() - datetime.timedelta(seconds=rand_seconds)

    def __getattr__(self, name):
        def delay_wrapper(*args, **kwargs):
            with self.lock:
                now = datetime.datetime.now()
                time_delta = now - self.last_request_time
                if time_delta.seconds >= self.backoff_time:
                    logger.info('VT_DELAY calling {} (time_delta={})'.format(name, time_delta.seconds))
                else:
                    cur_backoff_time = self.backoff_time - time_delta.seconds
                    logger.info('Called to soon, wait for {}sec'.format(cur_backoff_time))
                    time.sleep(cur_backoff_time + 1)
                    logger.info('VT_DELAY calling {}'.format(name))
                method = getattr(self.api, name)
                vt_ret_value = method(*args, **kwargs)
                self.last_request_time = datetime.datetime.now()
            return vt_ret_value
        return delay_wrapper

class CommonAnalysisVirusTotal(AnalysisPlugin, FileAnalysisMixin, URLAnalysisMixin, IPAnalysisMixin, DomainAnalysisMixin):
    def __init__(self, api_key, backoff_time=16):
        """
        CommonAnalysisVirusTotal

        :param api_key: API Key for Virustotal
        :param backoff_time: Time to sleep between requests to Virustotal in seconds.
        """
        super(CommonAnalysisVirusTotal, self).__init__(system_version)
        self.virustotal = DelayedVirustotalApi(api_key, backoff_time)

    def _send_file_to_virustotal(self, file_path):
        resp = self.virustotal.scan_file(file_path)
        if 'status_code' in resp and resp['status_code'] != 201:
            raise RuntimeError
        logger.info('{} uploaded to Virustotal.'.format(file_path))

    def _get_virustotal_file_result(self, file_path):
        with open(file_path, 'rb') as f:
            file_md5 = md5sum(f)
        vt_response = self.virustotal.get_file_report(file_md5)
        return vt_response['results']

    def wait_for_report(self, resource):
        response_code = VT_FILE_ANALYZIS_PENDING
        report = self.prepare_analysis_report_dictionary()
        if response_code == VT_FILE_UNKOWN:
            raise RuntimeError("File is unkown to Virustotal.")
        while response_code != VT_FILE_REPORT_SUBMITTED:
            logger.info('Requesting VirusTotal for result on {}'.format(resource))
            vt_response = self.virustotal.get_file_report(resource)
            if 'error' in vt_response:
                logger.error('VirustotalApi Error: {}'.format(vt_response['error']))
                continue
            result = vt_response['results']
            response_code = result['response_code']
        report['result'] = result
        return report

    def analyze_file(self, file_path, asynchronous=False):
        """
        Analyze a file.

        :param file_path: Path to the file.
        :param asynchronous: If False the method waits until Virustotal has analyzed
        the file and returns the report. If True the Virustotal resource is
        returned as handle.

        :return: Virustotal resource (md5sum) or report dict
        """
        # check if already submitted
        result = self._get_virustotal_file_result(file_path)
        if result['response_code'] != VT_FILE_REPORT_SUBMITTED:
            self._send_file_to_virustotal(file_path)

        if asynchronous:
            return result['resource']
        else:
            return self.wait_for_report(result['resource'])

    def analyze_ip(self, ip):
        report = self.prepare_analysis_report_dictionary()
        result = self.virustotal.get_ip_report(ip)['results']
        report.update(result)
        return report

    def analyze_url(self, url):
        report = self.prepare_analysis_report_dictionary()
        result = self.virustotal.get_url_report(url)['results']
        report.update(result)
        return report

    def analyze_domain(self, domain):
        report = self.prepare_analysis_report_dictionary()
        result = self.virustotal.get_domain_report(domain)['results']
        report.update(result)
        return report


import pytest
import string
import getpass
from playwright.sync_api import Page, Playwright
from test_commonmethod import test_open_page_sso,test_create_ppe_codespace,test_createAndinstall
from test_delete_codespace import test_deleteAllCodespace

@pytest.mark.daily
@pytest.mark.disconnect
def test_delete_all_codespaces(playwright: Playwright):
    test_deleteAllCodespace(playwright)

@pytest.mark.daily
@pytest.mark.disconnect
def test_disconnect_ViewCreationLog_codespace(playwright: Playwright):
    pageurl="https://github.com/codespaces/new?location=EastUs"
    page=test_open_page_sso(playwright,pageurl)
    try:
        page.context.pages[-2].close()
        test_create_ppe_codespace(page, "Microsoft/vscode-remote-try-python")
        test_createAndinstall(page, "16-core")
        #View Creation Log command
        page.wait_for_timeout(15000)
        page.keyboard.press("Control+Shift+P")
        page.keyboard.type("View Creation Log")
        page.keyboard.press("ArrowDown")
        page.keyboard.press("Enter")
        page.wait_for_timeout(1500)
        if not page.get_by_role("tab").get_by_title("/workspaces/.codespaces/.persistedshare/creation.log").is_visible():
            page.wait_for_timeout(4000)
            page.keyboard.press("Control+Shift+P")
            page.keyboard.type("View Creation Log")
            page.keyboard.press("ArrowDown")
            page.keyboard.press("Enter")
            page.wait_for_timeout(1500)
        assert page.get_by_role("tab").get_by_title("/workspaces/.codespaces/.persistedshare/creation.log").is_visible()
        #disconnect codespace
        page.keyboard.press("Control+Shift+P")
        page.keyboard.type("Codespaces: details")
        page.keyboard.press("ArrowDown")
        page.keyboard.press("Enter")
        page.wait_for_timeout(1500)
        page.get_by_role("tree").nth(0).hover()
        page.get_by_title("Disconnect").click()
        page.wait_for_timeout(1500)
        assert 'https://github.com/codespaces/' in page.url
    finally:
        page.close()


# https://leetcode.com/problems/two-best-non-overlapping-events/
class Solution:
    def maxTwoEvents(self, events: List[List[int]]) -> int:
        events.sort(key = lambda x: x[0])
        n = len(events)

        start_time = [e[0] for e in events]
        max_value = [0] * n
        max_value[-1] = events[-1][2]

        ans = 0

        for i in range(n - 2, -1, -1):
            val = events[i][2]
            max_value[i] = max(val, max_value[i + 1])
            idx = bisect_right(start_time, events[i][1], i + 1)

            if idx == n:
                ans = max(ans, max_value[i])
            else:
                ans = max(ans, val + max_value[idx])

        return ans

2054. Two Best Non-Overlapping Events - medium.py