import pickle
import os
import numpy as np
import re
from multiprocessing import Pool
import instruction2vec
import eval_utils as utils
from collections import Counter

embed_info = {}
type_info = {
    'char': 0,
    'int': 1,
    'float': 2,
    'pointer': 3,
    'enum': 4,
    'struct': 5,
    'union': 6
}

def approximate_type(type_str):
    int_list = ['_Bool', 'unsigned int', 'int', 'long long int', 'long long unsigned int', 'unsigned short',
                'short unsigned int', 'short', 'long unsigned int', 'short int', 'long int']
    char_list = ['char', 'unsigned char', 'signed char']
    if type_str[-1] == '*' or type_str == 'func_ptr' or type_str.split()[0][-1] == '*':
        return 'pointer'
    elif type_str in int_list:
        return 'int'
    elif type_str[:5] == 'enum ':
        return 'enum'
    elif type_str in char_list:
        return 'char'
    elif type_str[:7] == 'struct ':
        return 'struct'
    elif type_str[:6] == 'union ':
        return 'union'
    elif type_str == 'double' or type_str == 'long double':
        return 'float'
    else:
        return type_str

def one_hot_encoding(label_id, class_num):
    temp = np.zeros(class_num)
    temp[label_id] = 1
    return temp

def get_single_num_args(folder_path, file_name, func_list, embed_dim, max_length, class_num, embd_type, vocab=None):
    def parse_instruction(ins):
        import re
        ins = re.sub('\s+', ', ', ins, 1)
        parts = ins.split(', ')
        return ','.join(parts)

    def parse_instruction_trans(ins):
        import re
        ins = re.sub('\s+', ', ', ins, 1)
        parts = ins.split(', ')
        operand = []
        token_lst = []
        if len(parts) > 1:
            operand = parts[1:]
        token_lst.append(parts[0])
        for i in range(len(operand)):
            symbols = re.split('([0-9A-Za-z]+)', operand[i])
            symbols = [s.strip() for s in symbols if s != '']
            token_lst.extend(symbols)
        token_lst = [t for t in token_lst if t]
        return ' '.join(token_lst)

    usable_encoder = utils.UsableTransformer(model_path="/path/to/palmtree/model", vocab_path="/path/to/palmtree/vocabulary")
    file_path = os.path.join(folder_path, file_name)
    extract_info = {}
    with open(file_path) as f:
        file_info = pickle.load(f)
    for func_name in func_list:
        func_tag = '%s#%s' % (file_name, func_name)
        extract_info[func_tag] = {}
        inst_bytes = file_info['functions'][func_name]['inst_bytes']
        inst_strings = file_info['functions'][func_name]['inst_strings']
        temp_data = []

        inst_strings = [parse_instruction_trans(str(ins)) for ins in inst_strings]
        if len(inst_strings) >= max_length:
            inst_strings = inst_strings[:max_length]
        temp_data = usable_encoder.encode(inst_strings)
        if temp_data.shape[0] < max_length:
            extract_info[func_tag]['length'] = temp_data.shape[0]
            temp_zero = np.zeros((max_length - temp_data.shape[0], embed_dim))
            temp_data = np.concatenate((temp_data, temp_zero), axis=0)
        else:
            extract_info[func_tag]['length'] = temp_data.shape[0]
        extract_info[func_tag]['data'] = temp_data
        extract_info[func_tag]['label'] = one_hot_encoding(file_info['functions'][func_name]['num_args'], class_num)
    return extract_info

def get_single_args_type(folder_path, file_name, func_list, embed_dim, max_length, class_num, arg_no):
    file_path = os.path.join(folder_path, file_name)
    extract_info = {}
    with open(file_path) as f:
        file_info = pickle.load(f)
    for func_name in func_list:
        func_tag = '%s#%s' % (file_name, func_name)
        extract_info[func_tag] = {}
        inst_bytes = file_info['functions'][func_name]['inst_bytes']
        temp_data = []
        for inst in inst_bytes:
            if str(inst) in embed_info:
                temp_data.append(embed_info[str(inst)]['vector'])
            else:
                temp_data.append([0.0] * embed_dim)
            if len(temp_data) >= max_length:
                break
        temp_data = np.asarray(temp_data)
        if temp_data.shape[0] < max_length:
            extract_info[func_tag]['length'] = temp_data.shape[0]
            temp_zero = np.zeros((max_length - temp_data.shape[0], embed_dim))
            temp_data = np.concatenate((temp_data, temp_zero), axis=0)
        else:
            extract_info[func_tag]['length'] = temp_data.shape[0]
        extract_info[func_tag]['data'] = temp_data
        temp_type = approximate_type(file_info['functions'][func_name]['args_type'][arg_no])
        extract_info[func_tag]['label'] = one_hot_encoding(type_info[temp_type], class_num)
    return extract_info

class Dataset(object):
    def __init__(self, data_folder, func_path, embed_path, thread_num, embed_dim, max_length, class_num, tag, embd_type):
        global embed_info
        self.data_folder = data_folder
        self.tag = tag #num_args or type#0
        if self.tag == 'num_args':
            pass
        else:
            self.arg_no = int(self.tag.split('#')[-1])
        self.thread_num = thread_num
        self.embed_dim = embed_dim
        self.max_length = max_length
        self.class_num = class_num
        self.embd_type = embd_type
        print(func_path)
        with open(func_path) as f:
            func_info = pickle.load(f)
        self.train_func_list = np.asarray(func_info['train'])
        self.train_num = len(self.train_func_list)
        print('Loaded train function information ... %s' % func_path)
        print('Train Function Number: %d' % self.train_num)
        self.func_list = np.asarray(func_info['test'])
        self.func_num = len(self.func_list)
        print('Loaded train function information ... %s' % func_path)
        print('Train Function Number: %d' % self.func_num)

        with open(embed_path) as f:
            embed_info = pickle.load(f)
        print('Loaded embed information ... %s' % embed_path)
        self.test_tag = True
        self._index_in_epoch = 0
        self._index_in_test = 0
        self._complete_epochs = 0

        # get vocabulary
        if self.embd_type == '1hot':
            counter = Counter()
            binaries = os.listdir(self.data_folder)
            for binary in binaries:
                print(binary)
                file_path = os.path.join(self.data_folder, binary)
                with open(file_path) as f:
                    file_info = pickle.load(f)
                print(len(file_info['functions']))
                for func in file_info['functions'].values():
                    counter.update(func['inst_strings'])
            self.vocabulary = sorted(counter, key=counter.get, reverse=True)[:5000]
            self.vocabulary.append('UNK')
        else:
            self.vocabulary = None

    def get_batch_data(self, batch_func_list):
        func_list = sorted(batch_func_list)
        binary_name = ''
        input_func_list = []
        batch_info = {}
        pool = Pool(self.thread_num)
        if self.tag == 'num_args':
            for whole_func_name in func_list:
                if binary_name == '':
                    binary_name = whole_func_name.split('#')[0]
                    input_func_list.append(whole_func_name.split('#')[1])
                else:
                    if binary_name == whole_func_name.split('#')[0]:
                        input_func_list.append(whole_func_name.split('#')[1])
                    else:
                        pool.apply_async(
                            get_single_num_args,
                            args= (self.data_folder, binary_name, input_func_list, self.embed_dim, self.max_length, self.class_num, self.embd_type, self.vocabulary),
                            callback= batch_info.update
                        )
                        binary_name = whole_func_name.split('#')[0]
                        input_func_list = [whole_func_name.split('#')[1]]
            if len(input_func_list) == 0:
                pass
            else:
                pool.apply_async(
                    get_single_num_args,
                    args=(self.data_folder, binary_name, input_func_list, self.embed_dim, self.max_length, self.class_num, self.embd_type, self.vocabulary),
                    callback=batch_info.update
                )
        else: #self.tag == 'type#0'
            for whole_func_name in func_list:
                if binary_name == '':
                    binary_name = whole_func_name.split('#')[0]
                    input_func_list.append(whole_func_name.split('#')[1])
                else:
                    if binary_name == whole_func_name.split('#')[0]:
                        input_func_list.append(whole_func_name.split('#')[1])
                    else:
                        pool.apply_async(
                            get_single_args_type,
                            args=(self.data_folder, binary_name, input_func_list, self.embed_dim, self.max_length,
                                  self.class_num, self.arg_no),
                            callback= batch_info.update
                        )
                        binary_name = whole_func_name.split('#')[0]
                        input_func_list = [whole_func_name.split('#')[1]]
            if len(input_func_list) == 0:
                pass
            else:
                pool.apply_async(
                    get_single_args_type,
                    args=(self.data_folder, binary_name, input_func_list, self.embed_dim, self.max_length, self.class_num, self.arg_no),
                    callback=batch_info.update
                )
        pool.close()
        pool.join()
        new_batch_data = {
            'data': [],
            'label': [],
            'length': [],
            'func_name':[]
        }
        for full_func_name in batch_info:
            new_batch_data['data'].append(batch_info[full_func_name]['data'])
            new_batch_data['label'].append(batch_info[full_func_name]['label'])
            new_batch_data['length'].append(batch_info[full_func_name]['length'])
            new_batch_data['func_name'].append(full_func_name)
        batch_info = {
            'data': np.asarray(new_batch_data['data'], dtype=np.float32),
            'label': np.asarray(new_batch_data['label'], dtype=np.float32),
            'length': np.asarray(new_batch_data['length'], dtype=np.float32),
            'func_name': np.asarray(new_batch_data['func_name'])
        }
        return batch_info

    def get_batch(self, batch_size):
        start = self._index_in_epoch
        # shuffle for the first round

        if self._complete_epochs == 0 and self._index_in_epoch == 0:
            perm0 = np.arange(self.train_num)
            np.random.shuffle(perm0)
            self.train_func_list = self.train_func_list[perm0]
        # go to the next epoch
        if start + batch_size > self.train_num:
            self._complete_epochs += 1
            rest_example_num = self.train_num - start
            rest_func_list = self.train_func_list[start:self.train_num]
            # shuffle for the new epoch
            perm = np.arange(self.train_num)
            np.random.shuffle(perm)
            self.train_func_list = self.train_func_list[perm]
            # start a new epoch
            start = 0
            self._index_in_epoch = batch_size - rest_example_num
            end = self._index_in_epoch
            new_func_list = self.train_func_list[start:end]
            func_list_batch = np.concatenate((rest_func_list, new_func_list), axis=0)
            train_batch = self.get_batch_data(func_list_batch)
            return train_batch
        else:  # process current epoch
            self._index_in_epoch += batch_size
            end = self._index_in_epoch
            func_list_batch = self.train_func_list[start:end]
            train_batch = self.get_batch_data(func_list_batch)
            return train_batch

    def get_test_batch(self, batch_size):
        start = self._index_in_test
        if start + batch_size >= self.func_num:
            self.test_tag = False
            func_list_batch = self.func_list[start:]
            test_batch = self.get_batch_data(func_list_batch)
            return test_batch
        else:
            self._index_in_test += batch_size
            end = self._index_in_test
            func_list_batch = self.func_list[start: end]
            test_batch = self.get_batch_data(func_list_batch)
            return test_batch


# ENTERING CLIENT DETAILS

name = input("Please input your name: ")
amount = input("Please input the amount you got: ")
amount = int(amount)
days_spent = input("Enter days taken with the loan: ")
days_spent = int(days_spent)

if days_spent <= 30:
    Total = (amount * 10 / 100) + amount
    print(f"The total loan to be paid is {Total}")

if days_spent > 30:
    Total = (amount * 10 / 100) + (amount) + (amount * 1 / 100)
    print(f"The total loan to be paid is {Total}")


"""
Programming_1 finall assignment
Author: Stylianos Mavrianos
Date: 25/01/2022
Version: 2

Supporting file for finall assignment
"""

#Importing Libralies
import pandas as pd
import numpy as np
from bokeh.io import output_notebook
from bokeh.plotting import figure, show
from bokeh.transform import jitter
from bokeh.models import ColumnDataSource,Range1d
from bokeh.layouts import row
import matplotlib.pyplot as plt

def get_NP_level(df, np):
     #######################################################
    #
    # Arguments:
    #
    # df                  pd.dataframe
    # np                  level of nanoplastic concentration
    #
    # Author:            Stylianos Mavrianos
    # Date:              2021-11-06
    #
    ########################################################

    df_np_level = df[df['NP_treatment'] == np]

    return df_np_level

def get_INF_level(df, inf):
     #######################################################
    #
    # Arguments:
    #
    # df                  pd.dataframe
    # np                  level of infection
    #
    # Author:            Stylianos Mavrianos
    # Date:              2021-11-06
    #
    ########################################################
    df_inf_level = df[df['Inf_treatment'] == inf]

    return df_inf_level

def get_values(df, var):
     #######################################################
    #
    # Arguments:
    #
    # df                  pd.dataframe
    # var                 variable of interest
    #
    # Author:            Stylianos Mavrianos
    # Date:              2021-11-06
    #
    ########################################################
    if var == 'Age_death':
        x = 3
    if var == 'Total_juv':
        x = 4
    if var == 'Spore_yield':
        x = 8
    values = np.array(df.iloc[0:, x])

    return values

def get_means(df, var):
    #######################################################
    #
    # Arguments:
    #
    # df                  pd.dataframe
    # var                 variable of interest
    #
    # Author:            Stylianos Mavrianos
    # Date:              2021-11-06
    #
    ########################################################
    means = pd.DataFrame(data=df.groupby(['Inf_treatment','NP_treatment'])[var].mean())
    means = means.reset_index()
    means = means.drop(columns=['Inf_treatment'])

    return means

def get_std(df, var):
    #######################################################
    #
    # Arguments:
    #
    # df                  pd.dataframe
    # var                 variable of interest
    #
    # Author:            Stylianos Mavrianos
    # Date:              2021-11-06
    #
    ########################################################
    high, low, zero = df.groupby('NP_treatment')

    std_zero = np.std(zero[1][var], ddof=1) / np.sqrt(np.size(zero[1][var]))
    std_low = np.std(low[1][var], ddof=1) / np.sqrt(np.size(low[1][var]))
    std_high = np.std(high[1][var], ddof=1) / np.sqrt(np.size(high[1][var]))

    d = {'NP_treatment': ['HIGH','LOW','ZERO'], 'std': [std_high,std_low,  std_zero,]}
    std =  pd.DataFrame(data=d)

    return std

def errorbar_df(df, var):
    #######################################################
    #
    # Arguments:
    #
    # df                  pd.dataframe
    # var                 variable of interest
    #
    # Author:            Stylianos Mavrianos
    # Date:              2021-11-06
    #
    ########################################################
    mean = get_means(df, var)
    std = get_std(df, var)
    mean['std_lower'] = mean[var] - std['std']
    mean['std_upper'] = mean[var] + std['std']

    return mean

def make_plot(df_main, df_errorbar, var, title,axis_label, num):
    #######################################################
    #
    # Arguments:
    #
    # df_main                  pd.dataframe
    # df_errorbar              pd.dataframe
    # var                      variable of interest
    # title                    title of the plot
    # axis_label               label of the plot
    # num                      y_max_range
    #
    # Author:            Stylianos Mavrianos
    # Date:              2021-11-06
    #
    ########################################################
    np_levels = ['ZERO', 'LOW', 'HIGH']

    source = ColumnDataSource(df_main)
    source_1 = ColumnDataSource(df_errorbar)

    p = figure(tools="pan, hover")
    p = figure(x_range= np_levels,y_range = (0,num),
               toolbar_location=None,
               title=title,
               plot_height=300, plot_width=300)
    p.yaxis.axis_label = axis_label

    #Scatter data
    p.dot(x= jitter('NP_treatment',width=0.2, range=p.x_range), y=jitter(var, width=1, range=p.y_range), source= source , alpha=0.3, size = 35)

    p.line(x = 'NP_treatment', y = var, source= source_1, color = 'black')

    # Error asterisks
    p.asterisk(x = 'NP_treatment', y = 'std_lower', source= source_1, color = 'black', size = 3)
    p.asterisk(x = 'NP_treatment', y = 'std_upper', source= source_1, color = 'black', size = 3)

    # Mean
    p.dot(x = 'NP_treatment', y = var, source= source_1, size = 35, color = 'red')

    # p.ygrid.grid_line_color = None

    return p

def make_barplot(df_main, df_errorbar, var, title, axis_label):
    #######################################################
    #
    # Arguments:
    #
    # df_main                  pd.dataframe
    # df_errorbar              pd.dataframe
    # var                 variable of interest
    # title                    title of the plot
    # axis_label               label of the plot
    #
    # Author:            Stylianos Mavrianos
    # Date:              2021-11-06
    #
    ########################################################
    from bokeh.palettes import Spectral3

    h, l, z = df_main.groupby(['NP_treatment'])[var].sum() / df_main.groupby(['NP_treatment'])[var].count()

    NP_treatment = ['ZERO', 'LOW', 'HIGH']
    counts = [z, l, h]

    source = ColumnDataSource(data=dict(NP_treatment = NP_treatment, counts = counts, color=Spectral3))
    source_1 = ColumnDataSource(df_errorbar)

    p = figure(x_range = NP_treatment, y_range = (0,1.05), height = 250, title = title,
               toolbar_location = None, tools = "")

    p.vbar(x='NP_treatment', top = 'counts', width=0.2, color='color', source = source)
    p.xgrid.grid_line_color = None
    p.yaxis.axis_label = axis_label

    # Error asterisks
    p.asterisk(x = 'NP_treatment', y = 'std_lower', source= source_1, color = 'black', size = 3)
    p.asterisk(x = 'NP_treatment', y = 'std_upper', source= source_1, color = 'black', size = 3)

    return p

def anova(df, Response_var, *Explanatory_var ):
    #######################################################
    #
    # Arguments:
    #
    # df                  pd.dataframe
    # Response_var        Response variable
    # Explanatory_var     Explanatory variables
    #
    # Author:            Stylianos Mavrianos
    # Date:              2021-11-06
    #
    ########################################################

    import pingouin as pg

    aov = pg.anova(dv=Response_var, between=[*Explanatory_var], data= df,
               detailed=True)

    return aov.round(4)

def figures():

    zero_counts = np.array([11, 14])
    zero_labels = ["Infected", "Not infected",]
    zero_colors = ['r', 'g']
    plt.pie(zero_counts, labels = zero_labels, colors = zero_colors, shadow = True)
    plt.title("Zero concentration")
    plt.show()

    low_counts = np.array([1, 2, 6, 16])
    low_labels = ['excluded', 'early death', "Infected", "Not infected"]
    low_colors = ['k', 'gray', 'r', 'g']
    plt.title("Low concentration")
    plt.pie(low_counts, labels = low_labels, colors = low_colors, shadow = True, counterclock = True)
    plt.show()

    high_counts = np.array([2, 6, 3, 14])
    high_labels = ['excluded', 'early death', "Infected", "Not infected"]
    high_colors = ['k', 'gray', 'r', 'g']
    plt.title("High concentration")
    plt.pie(high_counts, labels = high_labels, colors = high_colors, shadow = True)
    plt.show()

def Q_Q_Plot(y, est = 'robust', **kwargs):

    ################################################################################
    #
    # Arguments:
    #
    # y                  data array
    # est                Estimation method for normal parameters mu and sigma:
    #                    either 'robust' (default), or 'ML' (Maximum Likelihood),
    #                    or 'preset' (given values)
    # If est='preset' than the optional parameters mu, sigma must be provided
    #
    # Author:            M.E.F. Apol
    # Date:              2020-01-06
    #
    ################################################################################

    import numpy as np
    from scipy.stats import iqr # iqr is the Interquartile Range function
    import matplotlib.pyplot as plt

    # First, get the optional arguments mu and sigma:
    mu_0 = kwargs.get('mu', None)
    sigma_0 = kwargs.get('sigma', None)

    n = len(y)

    # Calculate order statistic:
    y_os = np.sort(y)

    # Estimates of mu and sigma:
    # ML estimates:
    mu_ML = np.mean(y)
    sigma2_ML = np.mean((y - mu_ML)**2)
    sigma_ML = np.sqrt(sigma2_ML) # biased estimate
    s2 = n/(n-1) * sigma2_ML
    s = np.sqrt(s2) # unbiased estimate
    # Robust estimates:
    mu_R = np.median(y)
    sigma_R = iqr(y)/1.349

    # Assign values of mu and sigma for z-transform:
    if est == 'ML':
        mu, sigma = mu_ML, s
    elif est == 'robust':
        mu, sigma = mu_R, sigma_R
    elif est == 'preset':
        mu, sigma = mu_0, sigma_0
    else:
        print('Wrong estimation method chosen!')

    print('Estimation method: ' + est)
    print('mu = ',mu,', sigma = ',sigma)

    # Perform z-transform: sample quantiles z.i
    z_i = (y_os - mu)/sigma

    # Calculate cumulative probabilities p.i:
    i = np.array(range(n)) + 1
    p_i = (i - 0.5)/n

    # Calculate theoretical quantiles z.(i):
    from scipy.stats import norm
    z_th = norm.ppf(p_i, 0, 1)

    # Calculate SE or theoretical quantiles:
    SE_z_th = (1/norm.pdf(z_th, 0, 1)) * np.sqrt((p_i * (1 - p_i)) / n)

    # Calculate 95% CI of diagonal line:
    CI_upper = z_th + 1.96 * SE_z_th
    CI_lower = z_th - 1.96 * SE_z_th

    # Make Q-Q plot:
    plt.plot(z_th, z_i, 'o', color='k', label='experimental data')
    plt.plot(z_th, z_th, '--', color='r', label='normal line')
    plt.plot(z_th, CI_upper, '--', color='b', label='95% CI')
    plt.plot(z_th, CI_lower, '--', color='b')
    plt.xlabel('Theoretical quantiles, $z_{(i)}$')
    plt.ylabel('Sample quantiles, $z_i$')
    plt.title('Q-Q plot (' + est + ')')
    plt.legend(loc='best')
    plt.show()

def Q_Q_Hist(y, est='robust', **kwargs):

    ################################################################################
    #
    # Arguments:
    #
    # y                  data array
    # est                Estimation method for normal parameters mu and sigma:
    #                    either 'robust' (default), or 'ML' (Maximum Likelihood),
    #                    or 'preset' (given values)
    # If est='preset' than the optional parameters mu, sigma must be provided
    #
    # Author:            M.E.F. Apol
    # Date:              2020-01-06
    #
    ################################################################################

    import numpy as np
    from scipy.stats import iqr # iqr is the Interquartile Range function
    from scipy.stats import norm
    import matplotlib.pyplot as plt

    # First, get the optional arguments mu and sigma:
    mu_0 = kwargs.get('mu', None)
    sigma_0 = kwargs.get('sigma', None)

    n = len(y)

    # Estimates of mu and sigma:
    # ML estimates:
    mu_ML = np.mean(y)
    sigma2_ML = np.mean((y - mu_ML)**2)
    sigma_ML = np.sqrt(sigma2_ML) # biased estimate
    s2 = n/(n-1) * sigma2_ML
    s = np.sqrt(s2) # unbiased estimate
    # Robust estimates:
    mu_R = np.median(y)
    sigma_R = iqr(y)/1.349

    # Assign values of mu and sigma for z-transform:
    if est == 'ML':
        mu, sigma = mu_ML, s
    elif est == 'robust':
        mu, sigma = mu_R, sigma_R
    elif est == 'preset':
        mu, sigma = mu_0, sigma_0
    else:
        print('Wrong estimation method chosen!')
    print('Estimation method: ' + est)
    print('mu = ',mu,', sigma = ',sigma)

    # Calculate the CLT normal distribution:
    x = np.linspace(np.min(y), np.max(y), 501)
    rv = np.array([norm.pdf(xi, loc = mu, scale = sigma) for xi in x])

    # Make a histogram with corresponding normal distribution:
    nn, bins, patches = plt.hist(x=y, density=True,
                                 bins='auto',
                                 color='darkgrey',alpha=1, rwidth=1, label='experimental')
    h = plt.plot(x, rv, 'r', label='normal approximation')
    plt.grid(axis='y', alpha=0.5)
    plt.xlabel('Values, $y$')
    plt.ylabel('Probability $f(y)$')
    plt.title('Histogram with corresponding normal distribution (' + est + ')')
    plt.legend(loc='best')
    plt.show()

if __name__ == "__main__":
    main()


import argparse
import os
import shutil
import subprocess
import sys
import tempfile
import zipfile

import utils

from android_build_client import AndroidBuildClient

class Benchmark(object):

    def __init__(self, name):
        self.name = name

    def apct_test_tag(self):
        raise NotImplementedError()

    def profdata_file(self, suffix=''):
        profdata = os.path.join(self.name, '{}.profdata'.format(self.name))
        if suffix:
            profdata += '.' + suffix
        return profdata

    def profraw_files(self):
        raise NotImplementedError()

    def merge_profraws(self, profile_dir, output):
        profraws = [
            os.path.join(profile_dir, p)
            for p in self.profraw_files(profile_dir)
        ]
        utils.run_llvm_profdata(profraws, output)

class NativeExeBenchmark(Benchmark):

    def apct_test_tag(self):
        return 'apct/perf/pgo/profile-collector'

    def profraw_files(self, profile_dir):
        if self.name == 'hwui':
            return [
                'hwuimacro.profraw', 'hwuimacro_64.profraw',
                'hwuimicro.profraw', 'hwuimicro_64.profraw',
                'skia_nanobench.profraw', 'skia_nanobench_64.profraw'
            ]
        elif self.name == 'hwbinder':
            return ['hwbinder.profraw', 'hwbinder_64.profraw']

class APKBenchmark(Benchmark):

    def apct_test_tag(self):
        return 'apct/perf/pgo/apk-profile-collector'

    def profdata_file(self, suffix=''):
        profdata = os.path.join('art',
                                '{}_arm_arm64.profdata'.format(self.name))
        if suffix:
            profdata += '.' + suffix
        return profdata

    def profraw_files(self, profile_dir):
        return os.listdir(profile_dir)

def BenchmarkFactory(benchmark_name):
    if benchmark_name == 'dex2oat':
        return APKBenchmark(benchmark_name)
    elif benchmark_name in ['hwui', 'hwbinder']:
        return NativeExeBenchmark(benchmark_name)
    else:
        raise RuntimeError('Unknown benchmark ' + benchmark_name)

def extract_profiles(build, test_tag, build_client, output_dir):
    pgo_zip = build_client.download_pgo_zip(build, test_tag, output_dir)

    zipfile_name = os.path.join(pgo_zip)
    zip_ref = zipfile.ZipFile(zipfile_name)
    zip_ref.extractall(output_dir)
    zip_ref.close()

KNOWN_BENCHMARKS = ['ALL', 'dex2oat', 'hwui', 'hwbinder']

def parse_args():
    """Parses and returns command line arguments."""
    parser = argparse.ArgumentParser()

    parser.add_argument(
        'build',
        metavar='BUILD',
        help='Build number to pull from the build server.')

    parser.add_argument(
        '-b', '--bug', type=int, help='Bug to reference in commit message.')

    parser.add_argument(
        '--use-current-branch',
        action='store_true',
        help='Do not repo start a new branch for the update.')

    parser.add_argument(
        '--add-do-not-merge',
        action='store_true',
        help='Add \'DO NOT MERGE\' to the commit message.')

    parser.add_argument(
        '--profdata-suffix',
        type=str,
        default='',
        help='Suffix to append to merged profdata file')

    parser.add_argument(
        'benchmark',
        metavar='BENCHMARK',
        help='Update profiles for BENCHMARK.  Choices are {}'.format(
            KNOWN_BENCHMARKS))

    parser.add_argument(
        '--skip-cleanup',
        '-sc',
        action='store_true',
        default=False,
        help='Skip the cleanup, and leave intermediate files (in /tmp/pgo-profiles-*)'
    )

    return parser.parse_args()

def get_current_profile(benchmark):
    profile = benchmark.profdata_file()
    dirname, basename = os.path.split(profile)

    old_profiles = [f for f in os.listdir(dirname) if f.startswith(basename)]
    if len(old_profiles) == 0:
        return ''
    return os.path.join(dirname, old_profiles[0])

def main():
    args = parse_args()
    utils.check_gcertstatus()

    if args.benchmark == 'ALL':
        worklist = KNOWN_BENCHMARKS[1:]
    else:
        worklist = [args.benchmark]

    profiles_project = os.path.join(utils.android_build_top(), 'toolchain',
                                    'pgo-profiles')
    os.chdir(profiles_project)

    if not args.use_current_branch:
        branch_name = 'update-profiles-' + args.build
        utils.check_call(['repo', 'start', branch_name, '.'])

    build_client = AndroidBuildClient()

    for benchmark_name in worklist:
        benchmark = BenchmarkFactory(benchmark_name)

        # Existing profile file, which gets 'rm'-ed from 'git' down below.
        current_profile = get_current_profile(benchmark)

        # Extract profiles to a temporary directory.  After extraction, we
        # expect to find one subdirectory with profraw files under the temporary
        # directory.
        extract_dir = tempfile.mkdtemp(prefix='pgo-profiles-' + benchmark_name)
        extract_profiles(args.build, benchmark.apct_test_tag(), build_client,
                         extract_dir)

        extract_subdirs = [
            os.path.join(extract_dir, sub)
            for sub in os.listdir(extract_dir)
            if os.path.isdir(os.path.join(extract_dir, sub))
        ]
        if len(extract_subdirs) != 1:
            raise RuntimeError(
                'Expected one subdir under {}'.format(extract_dir))

        # Merge profiles.
        profdata = benchmark.profdata_file(args.profdata_suffix)
        benchmark.merge_profraws(extract_subdirs[0], profdata)

        # Construct 'git' commit message.
        message_lines = [
            'Update PGO profiles for {}'.format(benchmark_name), '',
            'The profiles are from build {}.'.format(args.build), ''
        ]

        if args.add_do_not_merge:
            message_lines[0] = '[DO NOT MERGE] ' + message_lines[0]

        if args.bug:
            message_lines.append('')
            message_lines.append('Bug: http://b/{}'.format(args.bug))
        message_lines.append('Test: Build (TH)')
        message = '\n'.join(message_lines)

        # Invoke git: Delete current profile, add new profile and commit these
        # changes.
        if current_profile:
            utils.check_call(['git', 'rm', current_profile])
        utils.check_call(['git', 'add', profdata])
        utils.check_call(['git', 'commit', '-m', message])

        if not args.skip_cleanup:
            shutil.rmtree(extract_dir)

if __name__ == '__main__':
    main()


#
# @lc app=leetcode id=189 lang=python
#
# [189] Rotate Array
#

# @lc code=start
class Solution(object):
    def rotate(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: None Do not return anything, modify nums in-place instead.
        """

        while k > 0:
            last = nums.pop()
            nums.insert(0, last)
            k -= 1
            # print(nums)
        return nums

# @lc code=end


import torch
from torch.utils.data import Dataset
import json
import numpy as np
import os
from PIL import Image
from torchvision import transforms as T
import time

from ray_utils import *
from einops import rearrange,repeat
import torch.nn.functional as F

class BlenderDataset(Dataset):
    def __init__(self, root_dir, split='train', img_wh=(800, 800)):
        self.root_dir = root_dir
        self.split = split
        assert img_wh[0] == img_wh[1], 'image width must equal image height!'
        self.img_wh = img_wh
        self.transforms = T.ToTensor()

        self.read_meta()
        self.white_back = True

    def read_meta(self):
        with open(os.path.join(self.root_dir,
                               f"transforms_{self.split}.json"), 'r') as f:
            self.meta = json.load(f)

        self.w, self.h = self.img_wh
        self.focal = 0.5*800/np.tan(0.5*self.meta['camera_angle_x']) # original focal length
                                                                     # when W=800

        self.focal *= self.img_wh[0]/800 # modify focal length to match size self.img_wh

        # bounds, common for all scenes
        self.near = 2.0
        self.far = 6.0

        self.directions = get_ray_directions(self.h, self.w, self.focal) # (h, w, 3)

        if self.split == 'train':
            self.image_paths = []
            self.poses = []
            self.all_ray_o = []
            self.all_ray_d = []
            self.all_rgbs = []
            self.viewdirs = []
            print('get_training_rays: start')
            eps_time = time.time()
            for frame in self.meta['frames']:
                pose = np.array(frame['transform_matrix'])[:3, :4]
                self.poses += [pose]
                c2w = torch.FloatTensor(pose)

                image_path = os.path.join(self.root_dir, f"{frame['file_path']}.png")
                self.image_paths += [image_path]
                img = Image.open(image_path)
                img = img.resize(self.img_wh, Image.LANCZOS)
                img = self.transforms(img) # (4, h, w)
                img = img.view(4, -1).permute(1, 0) # (h*w, 4) RGBA
                img = img[:, :3]*img[:, -1:] + (1-img[:, -1:]) # blend A to RGB
                self.all_rgbs += [img]

                rays_o, rays_d = get_rays(self.directions, c2w)  # both (h*w, 3)

                #rays_o, rays_d = get_view_rays(800, 800, pose_c2w = c2w, focal = self.focal, pixel_mode='center')
                #rays_o = rays_o.reshape(-1,3)
                #rays_d = rays_d.reshape(-1,3)

                viewdirs = rays_d / rays_d.norm(dim=-1, keepdim=True)

                self.all_ray_o += [rays_o]
                self.all_ray_d += [rays_d]
                self.viewdirs += [viewdirs]

            self.all_ray_o = torch.cat(self.all_ray_o, 0) # (len(self.meta['frames])*h*w, 3)
            self.all_ray_d = torch.cat(self.all_ray_d, 0) # (len(self.meta['frames])*h*w, 3)
            self.all_rgbs = torch.cat(self.all_rgbs, 0) # (len(self.meta['frames])*h*w, 3)
            self.viewdirs = torch.cat(self.viewdirs, 0)  # (len(self.meta['frames])*h*w, 3)
            print('get_training_rays: finish (eps time:', eps_time, 'sec)')

    def get_bound(self):
        return self.near, self.far

    def define_bbox(self):
        print('Compute_bbox: start')
        xyz_min = torch.Tensor([np.inf, np.inf, np.inf])
        xyz_max = -xyz_min
        pts_nf = torch.stack([self.all_ray_o+self.viewdirs*self.near, self.all_ray_o+self.viewdirs*self.far])
        xyz_min = torch.minimum(xyz_min, pts_nf.amin((0,1)))
        xyz_max = torch.maximum(xyz_max, pts_nf.amax((0,1)))
        print('Compute_bbox: xyz_min', xyz_min)
        print('Compute_bbox: xyz_max', xyz_max)
        print('Compute_bbox: finish')
        return xyz_max, xyz_min

    def voxel_count_views(self, channel, Nx, Ny, Nz, xyz_max, xyz_min, voxel_size):
        print('dvgo: voxel_count_views start')
        eps_time = time.time()
        count = torch.zeros([1, channel, Nx, Ny, Nz]).to('cuda')
        samples_per_ray = int(np.linalg.norm(np.array([Nx, Ny, Nz])+1) / 0.5) + 1
        z_vals = torch.arange(samples_per_ray).to('cuda')*voxel_size*0.5
        chunk_size = 10000

        rays_o = self.all_ray_o.to('cuda').split(chunk_size)
        rays_d = self.all_ray_d.to('cuda').split(chunk_size)

        ones = torch.ones([1, channel, Nx, Ny, Nz]).to('cuda').requires_grad_()
        for ray_os, ray_ds in zip(rays_o, rays_d):
            ray_d_norm = ray_ds.norm(dim=-1, keepdim=True)
            #viewdirs = ray_ds/ray_d_norm

            t_1 = ((xyz_max.to(ray_os.device) - ray_os) / (ray_ds + 1e-6)) #(num_rays,3)
            t_2 = ((xyz_min.to(ray_os.device) - ray_os) / (ray_ds + 1e-6)) #(num_rays,3)

            t_min = torch.minimum(t_1, t_2).amax(-1).clamp(min=self.near, max=self.far)
            t_max = torch.maximum(t_1, t_2).amin(-1).clamp(min=self.near, max=self.far)

            delta = torch.einsum('ij,ik->ikj', [ray_ds/ray_d_norm, t_min[...,None]*ray_d_norm  + z_vals[None,:]])
            rays_pts = ray_os[...,None, :] + delta
            '''
            ray_start = ray_os + t_min[...,None] * ray_ds
            rays_pts = ray_start[...,
                      None, :] + z_vals[..., :, None] * viewdirs[..., None, :]
            '''
            xyz_min_ = xyz_min.to(rays_pts.device).reshape(1, 1, -1)
            xyz_max_ = xyz_max.to(rays_pts.device).reshape(1, 1, -1)
            rays_pts = ((rays_pts - xyz_min_)/(xyz_max_ - xyz_min_))*2 - 1
            rays_pts = rays_pts.reshape(1, 1, 1, -1, 3)
            output = F.grid_sample(ones, rays_pts, mode='bilinear', align_corners=True)
            output = output.sum().backward()

            with torch.no_grad():
                count += (ones.grad > 1)

        eps_time = time.time() - eps_time
        print('dvgo: voxel_count_views finish (eps time:', eps_time, 'sec)')
        return count

    def __len__(self):
        if self.split == 'train':
            return len(self.all_rgbs)
        if self.split == 'val':
            return 8 # only validate 8 images (to support <=8 gpus)
        return len(self.meta['frames'])

    def __getitem__(self, idx):
        if self.split == 'train':
            sample = {  'ray_o': self.all_ray_o[idx],
                    'ray_d': self.all_ray_d[idx],
                    'viewdirs' : self.viewdirs[idx],
                    'rgbs': self.all_rgbs[idx]}

        else: # create data for each image separately
            frame = self.meta['frames'][idx]
            c2w = torch.FloatTensor(frame['transform_matrix'])[:3, :4]

            img = Image.open(os.path.join(self.root_dir, f"{frame['file_path']}.png"))
            img = img.resize(self.img_wh, Image.LANCZOS)
            img = self.transforms(img) # (4, H, W)
            valid_mask = (img[-1]>0).flatten() # (H*W) valid color area
            img = img.view(4, -1).permute(1, 0) # (H*W, 4) RGBA
            img = img[:, :3]*img[:, -1:] + (1-img[:, -1:]) # blend A to RGB

            rays_o, rays_d = get_rays(self.directions, c2w)

            '''
            rays_o, rays_d = get_view_rays(800, 800, pose_c2w = c2w, focal = self.focal, pixel_mode='center')
            rays_o = rays_o.reshape(-1,3)
            rays_d = rays_d.reshape(-1,3)
            '''

            viewdirs = rays_d / rays_d.norm(dim=-1, keepdim=True)

            sample = {'ray_o': rays_o,
                      'ray_d': rays_d,
                      'viewdirs' : viewdirs,
                      'rgbs': img,
                      'c2w': c2w,
                      'valid_mask': valid_mask}

        return sample

if __name__ == "__main__":
    dataset = BlenderDataset(
            root_dir = 'data/nerf_synthetic/lego', split='val', img_wh=(800, 800)
        )

    from torch.utils.data import DataLoader
    dataloader = DataLoader(
            dataset,
            shuffle=False,
            num_workers=1,
            batch_size = 1,
            pin_memory=True
        )

    for data in dataloader:
        rays_o = data['rays_o']
        rays_d = data['rays_d']
        #print(rays_o.shape)
        #print(rays_d.shape)
        break


class Solution:
    def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
        _dict = defaultdict(int)

        for cpdomain in cpdomains:
            temp = cpdomain.split()
            num = int(temp[0])
            first_point = temp[1].find('.') + 1
            second_point = temp[1].find('.', first_point + 1)
            if temp[1].count('.') == 2:
                _dict[temp[1]] += num
            if temp[1].count('.') >= 1:
                _dict[temp[1][first_point:]] += num
            _dict[temp[1][second_point + 1:]] += num

        ans = []
        for key in _dict:
            ans.append(str(_dict[key]) + ' ' + str(key))

        return ans


# -*- coding: utf-8 -*-
# @Time    : 2020/3/6 19:01
# @Author  : Deng Wenxing
# @Email   : dengwenxingae86@163.com
# @File    : data_count.py
# @Software: PyCharm
# @content : 统计每个表的数量
from pyspark import SparkConf
from pyspark.sql import SparkSession
from pyspark.sql.functions import *
import time
import os,sys
import logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s: %(message)s')
logger = logging.getLogger(__name__)

reload(sys)
sys.setdefaultencoding('utf-8')

conf=SparkConf().set('spark.driver.maxResultSize', '30g')
conf.set('spark.yarn.am.cores', 4)
conf.set('spark.executor.memory', '15g')
conf.set('spark.executor.instances', 100)
conf.set('spark.executor.cores', 4)
conf.set('spark.executor.extraJavaOptions', '-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseG1GC')

spark = SparkSession \
    .builder \
    .config(conf=conf) \
    .enableHiveSupport() \
    .getOrCreate()

from common import *
from jg_info import vertex_table_info,edge_table_info

path_prefix = '/phoebus/_fileservice/users/slmp/shulianmingpin/midfile/open_phone'
save_root = 'relation_theme_extenddir'
zp_source_root = 'wa_data/zp_jg'
steal_oil_source_root = 'wa_data/touyou_jg'

exists_files = vertex_table_info.keys() + edge_table_info.keys()
zp_file_cols = {
    'vertex_phonenumber':['phone', 'xm','province', 'city'],
    'vertex_person':['zjhm', 'zjlx', 'gj', 'xm', 'ywxm', 'zym', 'xb', 'csrq', 'mz', 'jg',
                                      'whcd', 'hyzk', 'zzmm', 'hkszdxz', 'sjjzxz'],
    'vertex_case':['asjbh','ajmc','0 asjfskssj','0 asjfsjssj','"" asjly','ajlb','"" fxasjsj','fxasjdd_dzmc',
                                        'jyaq','"" ajbs','larq','city','link_app'],
    'vertex_qq':['qq'],
    'vertex_alipay':['alipay'],
    'vertex_qq_group':['groupid','groupname'],
    'vertex_bankcard':['yhkh banknum','khh','sfzh','khrq'],
    'edge_qqgroup_link_banknum':['banknum','groupid','relate_time','start_time','end_time','relate_num'],
    'edge_qq_link_group':['qq','groupid','start_time','end_time'],
    'edge_qq_link_qq':['qq1','qq2','start_time','end_time'],
    'edge_person_link_phone':['phone','sfzh','start_time','end_time'],
    'edge_case_link_wechat':['wechatid wechat','ajbh asjbh','start_time','end_time'],
    'edge_case_link_qq':['qq','ajbh asjbh','start_time','end_time'],
    'edge_case_link_phone':['phone','ajbh asjbh','start_time','end_time'],
    'edge_case_link_person':['sfzh','ajbh asjbh','start_time','end_time'],
    'edge_case_link_banknum':['banknum','ajbh asjbh','start_time','end_time'],
    'edge_case_link_alipay':['alipay','ajbh asjbh','start_time','end_time'],
}

steal_file_cols = {
    'vertex_case':['asjbh','ajmc','asjfskssj','asjfsjssj','asjly','ajlb','fxasjsj','fxasjdd_dzmc',
                                        'jyaq','ajbs','larq','"" city','"" link_app'],
    'edge_case_link_person':['sfzh','ajbh asjbh','start_time','end_time'],
    'edge_case_link_phone':['phone','ajbh asjbh','start_time','end_time'],

}

def deal_zp_data():
    for file,cols in zp_file_cols.items():
        logger.info('dealing %s'%file)
        logger.info(add_save_path(file+"_jg",cp='*',root=zp_source_root))
        df = read_orc(spark,add_save_path(file+"_jg",cp='*',root=zp_source_root)).selectExpr(*cols)
        file_name = file+"_zp" if file in exists_files else file
        write_orc(df,add_save_path(file_name,root=save_root))

def deal_steal_data():
    for file,cols in steal_file_cols.items():
        df = read_orc(spark,add_save_path(file+"_jg",cp='*',root=steal_oil_source_root)).selectExpr(*cols)
        file_name = file+"_steal" if file in exists_files else file
        write_orc(df,add_save_path(file_name,root=save_root))

if __name__ == "__main__":
    logger.info('========================start time:%s==========================' % (
        time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))

    # deal_zp_data()
    deal_steal_data()

    logger.info('========================end time:%s==========================' % (
        time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))


import torch
from torch import nn
import torch.nn.functional as F
from torchvision import models
from torch.nn.utils.spectral_norm import spectral_norm
# from spatial_correlation_sampler import spatial_correlation_sample
from .resample2d import Resample2d
import torchvision.utils as vutils

class Discriminator(nn.Module):
    def __init__(self, input_dim=3, dim=64, n_layers=3,
                norm='none', activ='lrelu', pad_type='reflect', use_sn=True):
        super(Discriminator, self).__init__()

        self.model = nn.ModuleList()
        self.model.append(Conv2dBlock(input_dim,dim,4,2,1,'none',activ,pad_type,use_sn=use_sn))
        dim_in = dim
        for i in range(n_layers - 1):
            dim_out = min(dim*8, dim_in*2)
            self.model.append(DownsampleResBlock(dim_in,dim_out,'none',activ,pad_type,use_sn))
            dim_in = dim_out

        self.model.append(Conv2dBlock(dim_in,1,1,1,activation='none',use_bias=False, use_sn=use_sn))
        self.model = nn.Sequential(*self.model)

    def forward(self, x):
        return self.model(x)

class MultiDiscriminator(nn.Module):
    def __init__(self, **parameter_dic):
        super(MultiDiscriminator, self).__init__()
        self.model_1 = Discriminator(**parameter_dic)
        self.down = nn.AvgPool2d(3, stride=2, padding=[1, 1], count_include_pad=False)
        self.model_2 = Discriminator(**parameter_dic)

    def forward(self, x):
        pre1 = self.model_1(x)
        pre2 = self.model_2(self.down(x))
        return [pre1, pre2]

class StructureGen(nn.Module):
    def __init__(self, input_dim=3, dim=64, n_res=4, activ='relu',
                 norm='in', pad_type='reflect', use_sn=True):
        super(StructureGen, self).__init__()

        self.down_sample=nn.ModuleList()
        self.up_sample=nn.ModuleList()
        self.content_param=nn.ModuleList()

        self.input_layer = Conv2dBlock(input_dim*2+1, dim, 7, 1, 3, norm, activ, pad_type, use_sn=use_sn)
        self.down_sample += [nn.Sequential(
            Conv2dBlock(dim, 2*dim, 4, 2, 1, norm, activ, pad_type, use_sn=use_sn),
            Conv2dBlock(2*dim, 2*dim, 5, 1, 2, norm, activ, pad_type, use_sn=use_sn))]

        self.down_sample += [nn.Sequential(
            Conv2dBlock(2*dim, 4*dim, 4, 2, 1,norm, activ, pad_type, use_sn=use_sn),
            Conv2dBlock(4*dim, 4*dim, 5, 1, 2,norm, activ, pad_type, use_sn=use_sn))]

        self.down_sample += [nn.Sequential(
            Conv2dBlock(4*dim, 8*dim, 4, 2, 1,norm, activ, pad_type, use_sn=use_sn))]
        dim = 8*dim
        # content decoder
        self.up_sample += [(nn.Sequential(
            ResBlocks(n_res, dim, norm, activ, pad_type=pad_type),
            nn.Upsample(scale_factor=2),
            Conv2dBlock(dim, dim // 2, 5, 1, 2, norm, activ, pad_type, use_sn=use_sn)) )]

        self.up_sample += [(nn.Sequential(
            ResBlocks(n_res, dim//2, norm, activ, pad_type=pad_type),
            nn.Upsample(scale_factor=2),
            Conv2dBlock(dim//2, dim//4, 5, 1, 2,norm, activ, pad_type, use_sn=use_sn)) )]

        self.up_sample += [(nn.Sequential(
            ResBlocks(n_res, dim//4, norm, activ, pad_type=pad_type),
            nn.Upsample(scale_factor=2),
            Conv2dBlock(dim//4, dim//8, 5, 1, 2,norm, activ, pad_type, use_sn=use_sn)) )]

        self.content_param += [Conv2dBlock(dim//2, dim//2, 5, 1, 2, norm, activ, pad_type)]
        self.content_param += [Conv2dBlock(dim//4, dim//4, 5, 1, 2, norm, activ, pad_type)]
        self.content_param += [Conv2dBlock(dim//8, dim//8, 5, 1, 2, norm, activ, pad_type)]

        self.image_net = Get_image(dim//8, input_dim)

    def forward(self, inputs):
        x0 = self.input_layer(inputs)
        x1 = self.down_sample[0](x0)
        x2 = self.down_sample[1](x1)
        x3 = self.down_sample[2](x2)

        u1 = self.up_sample[0](x3) + self.content_param[0](x2)
        u2 = self.up_sample[1](u1) + self.content_param[1](x1)
        u3 = self.up_sample[2](u2) + self.content_param[2](x0)

        images_out = self.image_net(u3)
        return images_out

class FlowGen(nn.Module):
    def __init__(self, input_dim=3, dim=64, n_res=2, activ='relu',
                norm_flow='ln', norm_conv='in', pad_type='reflect', use_sn=True):
        super(FlowGen, self).__init__()

        self.flow_column = FlowColumn(input_dim, dim, n_res, activ,
                                      norm_flow, pad_type, use_sn)
        self.conv_column = ConvColumn(input_dim, dim, n_res, activ,
                                      norm_conv, pad_type, use_sn)

    def forward(self, inputs):
        flow_map = self.flow_column(inputs)
        images_out = self.conv_column(inputs, flow_map)
        return images_out, flow_map

class ConvColumn(nn.Module):
    def __init__(self, input_dim=3, dim=64, n_res=2, activ='lrelu',
                 norm='ln', pad_type='reflect', use_sn=True):
        super(ConvColumn, self).__init__()

        self.down_sample  = nn.ModuleList()
        self.up_sample    = nn.ModuleList()

        self.down_sample += [nn.Sequential(
                            Conv2dBlock(input_dim*2+1, dim//2, 7, 1, 3, norm, activ, pad_type, use_sn=use_sn),
                            Conv2dBlock(dim//2, dim,   4, 2, 1, norm, activ, pad_type, use_sn=use_sn),
                            Conv2dBlock(dim,    dim,   5, 1, 2, norm, activ, pad_type, use_sn=use_sn),
                            Conv2dBlock(dim,    2*dim, 4, 2, 1, norm, activ, pad_type, use_sn=use_sn),
                            Conv2dBlock(2*dim,  2*dim, 5, 1, 2, norm, activ, pad_type, use_sn=use_sn))]

        self.down_sample += [nn.Sequential(
                            Conv2dBlock(2*dim,  4*dim, 5, 1, 2, norm, activ, pad_type, use_sn=use_sn),
                            Conv2dBlock(4*dim,  8*dim, 4, 2, 1, norm, activ, pad_type, use_sn=use_sn))]
        dim = 8*dim

        # content decoder
        self.up_sample   += [(nn.Sequential(
                             ResBlocks(n_res, dim, norm, activ, pad_type=pad_type),
                             nn.Upsample(scale_factor=2),
                             Conv2dBlock(dim, dim//2, 5, 1, 2, norm, activ, pad_type, use_sn=use_sn)) )]

        self.up_sample   += [(nn.Sequential(
                            Conv2dBlock(dim, dim//2, 5, 1, 2,norm, activ, pad_type, use_sn=use_sn),
                            ResBlocks(n_res, dim//2, norm, activ, pad_type=pad_type),
                            nn.Upsample(scale_factor=2),
                            Conv2dBlock(dim//2, dim//4, 5, 1, 2,norm, activ, pad_type, use_sn=use_sn),

                            ResBlocks(n_res, dim//4, norm, activ, pad_type=pad_type),
                            nn.Upsample(scale_factor=2),
                            Conv2dBlock(dim//4, dim//8, 5, 1, 2,norm, activ, pad_type, use_sn=use_sn),
                            Get_image(dim//8, input_dim)) )]

        self.resample16 = Resample2d(16, 1, sigma=4)
        self.resample4  = Resample2d(4,  1, sigma=2)

    def forward(self, inputs, flow_maps):
        x1 = self.down_sample[0](inputs)
        x2 = self.down_sample[1](x1)
        flow_fea = self.resample_image(x1, flow_maps)

        u1 = torch.cat((self.up_sample[0](x2), flow_fea), 1)
        images_out = self.up_sample[1](u1)
        return images_out

    def resample_image(self, img, flow):
        output16 = self.resample16(img, flow)
        output4  = self.resample4 (img, flow)
        outputs = torch.cat((output16,output4), 1)
        return outputs

class FlowColumn(nn.Module):
    def __init__(self, input_dim=3, dim=64, n_res=2, activ='lrelu',
                 norm='in', pad_type='reflect', use_sn=True):
        super(FlowColumn, self).__init__()

        self.down_sample_flow  = nn.ModuleList()
        self.up_sample_flow    = nn.ModuleList()

        self.down_sample_flow.append( nn.Sequential(
                                    Conv2dBlock(input_dim*2+1, dim//2, 7, 1, 3, norm, activ, pad_type, use_sn=use_sn),
                                    Conv2dBlock(dim//2, dim, 4, 2, 1, norm, activ, pad_type, use_sn=use_sn),
                                    Conv2dBlock(   dim, dim, 5, 1, 2, norm, activ, pad_type, use_sn=use_sn)))
        self.down_sample_flow.append( nn.Sequential(
                                    Conv2dBlock(  dim, 2*dim, 4, 2, 1, norm, activ, pad_type, use_sn=use_sn),
                                    Conv2dBlock(2*dim, 2*dim, 5, 1, 2, norm, activ, pad_type, use_sn=use_sn)))
        self.down_sample_flow.append(nn.Sequential(
                                    Conv2dBlock(2*dim, 4*dim, 4, 2, 1, norm, activ, pad_type, use_sn=use_sn),
                                    Conv2dBlock(4*dim, 4*dim, 5, 1, 2, norm, activ, pad_type, use_sn=use_sn)))
        self.down_sample_flow.append(nn.Sequential(
                                    Conv2dBlock(4*dim, 8*dim, 4, 2, 1, norm, activ, pad_type, use_sn=use_sn),
                                    Conv2dBlock(8*dim, 8*dim, 5, 1, 2, norm, activ, pad_type, use_sn=use_sn)))
        dim = 8*dim

        # content decoder
        self.up_sample_flow.append(nn.Sequential(
                                    ResBlocks(n_res, dim, norm, activ, pad_type=pad_type),
                                    TransConv2dBlock(dim, dim//2, 6, 2, 2, norm=norm, activation=activ) ))

        self.up_sample_flow.append(nn.Sequential(
                                    Conv2dBlock(dim, dim//2, 5, 1, 2, norm, activ, pad_type, use_sn=use_sn),
                                    ResBlocks(n_res, dim//2, norm, activ, pad_type=pad_type),
                                    TransConv2dBlock(dim//2, dim//4, 6, 2, 2, norm=norm, activation=activ) ))

        self.location = nn.Sequential(
                                    Conv2dBlock(dim//2, dim//8, 5, 1, 2, norm, activ, pad_type, use_sn=use_sn),
                                    Conv2dBlock(dim//8, 2, 3, 1, 1, norm='none', activation='none', pad_type=pad_type, use_bias=False) )

    def forward(self, inputs):
        f_x1 = self.down_sample_flow[0](inputs)
        f_x2 = self.down_sample_flow[1](f_x1)
        f_x3 = self.down_sample_flow[2](f_x2)
        f_x4 = self.down_sample_flow[3](f_x3)

        f_u1 = torch.cat((self.up_sample_flow[0](f_x4), f_x3), 1)
        f_u2 = torch.cat((self.up_sample_flow[1](f_u1), f_x2), 1)
        flow_map = self.location(f_u2)
        return flow_map

##################################################################################
# Basic Blocks
##################################################################################
class Get_image(nn.Module):
    def __init__(self, input_dim, output_dim, activation='tanh'):
        super(Get_image, self).__init__()
        self.conv = Conv2dBlock(input_dim, output_dim, kernel_size=3, stride=1,
                     padding=1, pad_type='reflect', activation=activation)
    def forward(self, x):
        return self.conv(x)

class ResBlocks(nn.Module):
    def __init__(self, num_blocks, dim, norm='in', activation='relu', pad_type='zero', use_sn=False):
        super(ResBlocks, self).__init__()
        self.model = []
        for i in range(num_blocks):
            self.model += [ResBlock(dim, norm=norm, activation=activation, pad_type=pad_type, use_sn=use_sn)]
        self.model = nn.Sequential(*self.model)

    def forward(self, x):
        return self.model(x)

class ResBlock(nn.Module):
    def __init__(self, dim, norm='in', activation='relu', pad_type='zero', use_sn=False):
        super(ResBlock, self).__init__()

        model = []
        model += [Conv2dBlock(dim ,dim, 3, 1, 1, norm=norm, activation=activation, pad_type=pad_type, use_sn=use_sn)]
        model += [Conv2dBlock(dim ,dim, 3, 1, 1, norm=norm, activation='none', pad_type=pad_type, use_sn=use_sn)]
        self.model = nn.Sequential(*model)

    def forward(self, x):
        residual = x
        out = self.model(x)
        out += residual
        return out

class DilationBlock(nn.Module):
    def __init__(self, dim, norm='in', activation='relu', pad_type='zero'):
        super(DilationBlock, self).__init__()

        model = []
        model += [Conv2dBlock(dim ,dim, 3, 1, 2, norm=norm, activation=activation, pad_type=pad_type, dilation=2)]
        model += [Conv2dBlock(dim ,dim, 3, 1, 4, norm=norm, activation=activation, pad_type=pad_type, dilation=4)]
        model += [Conv2dBlock(dim ,dim, 3, 1, 8, norm=norm, activation=activation, pad_type=pad_type, dilation=8)]
        self.model = nn.Sequential(*model)

    def forward(self, x):
        out = self.model(x)
        return out

class Conv2dBlock(nn.Module):
    def __init__(self, input_dim ,output_dim, kernel_size, stride,
                 padding=0, norm='none', activation='relu', pad_type='zero', dilation=1,
                 use_bias=True, use_sn=False):
        super(Conv2dBlock, self).__init__()
        self.use_bias = use_bias
        # initialize padding
        if pad_type == 'reflect':
            self.pad = nn.ReflectionPad2d(padding)
        elif pad_type == 'replicate':
            self.pad = nn.ReplicationPad2d(padding)
        elif pad_type == 'zero':
            self.pad = nn.ZeroPad2d(padding)
        else:
            assert 0, "Unsupported padding type: {}".format(pad_type)

        # initialize normalization
        norm_dim = output_dim
        if norm == 'bn':
            self.norm = nn.BatchNorm2d(norm_dim)
        elif norm == 'in':
            self.norm = nn.InstanceNorm2d(norm_dim)
        elif norm == 'ln':
            self.norm = LayerNorm(norm_dim)
        elif norm == 'adain':
            self.norm = AdaptiveInstanceNorm2d(norm_dim)
        elif norm == 'none':
            self.norm = None
        else:
            assert 0, "Unsupported normalization: {}".format(norm)

        # initialize activation
        if activation == 'relu':
            self.activation = nn.ReLU(inplace=True)
        elif activation == 'lrelu':
            self.activation = nn.LeakyReLU(0.2, inplace=True)
        elif activation == 'prelu':
            self.activation = nn.PReLU()
        elif activation == 'selu':
            self.activation = nn.SELU(inplace=True)
        elif activation == 'tanh':
            self.activation = nn.Tanh()
        elif activation == 'sigmoid':
            self.activation = nn.Sigmoid()
        elif activation == 'none':
            self.activation = None
        else:
            assert 0, "Unsupported activation: {}".format(activation)

        # initialize convolution
        if use_sn:
            self.conv = spectral_norm(nn.Conv2d(input_dim, output_dim, kernel_size, stride, bias=self.use_bias, dilation=dilation))
        else:
            self.conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride, bias=self.use_bias, dilation=dilation)

    def forward(self, x):
        x = self.conv(self.pad(x))
        if self.norm:
            x = self.norm(x)
        if self.activation:
            x = self.activation(x)
        return x

class TransConv2dBlock(nn.Module):
    def __init__(self, input_dim ,output_dim, kernel_size, stride,
                 padding=0, norm='none', activation='relu'):
        super(TransConv2dBlock, self).__init__()
        self.use_bias = True

        # initialize normalization
        norm_dim = output_dim
        if norm == 'bn':
            self.norm = nn.BatchNorm2d(norm_dim)
        elif norm == 'in':
            self.norm = nn.InstanceNorm2d(norm_dim)
        elif norm == 'in_affine':
            self.norm = nn.InstanceNorm2d(norm_dim, affine=True)
        elif norm == 'ln':
            self.norm = LayerNorm(norm_dim)
        elif norm == 'adain':
            self.norm = AdaptiveInstanceNorm2d(norm_dim)
        elif norm == 'none':
            self.norm = None
        else:
            assert 0, "Unsupported normalization: {}".format(norm)

        # initialize activation
        if activation == 'relu':
            self.activation = nn.ReLU(inplace=True)
        elif activation == 'lrelu':
            self.activation = nn.LeakyReLU(0.2, inplace=True)
        elif activation == 'prelu':
            self.activation = nn.PReLU()
        elif activation == 'selu':
            self.activation = nn.SELU(inplace=True)
        elif activation == 'tanh':
            self.activation = nn.Tanh()
        elif activation == 'sigmoid':
            self.activation = nn.Sigmoid()
        elif activation == 'none':
            self.activation = None
        else:
            assert 0, "Unsupported activation: {}".format(activation)

        # initialize convolution
        self.transConv = nn.ConvTranspose2d(input_dim, output_dim, kernel_size, stride, padding, bias=self.use_bias)

    def forward(self, x):
        x = self.transConv(x)
        if self.norm:
            x = self.norm(x)
        if self.activation:
            x = self.activation(x)
        return x

class AdaptiveInstanceNorm2d(nn.Module):
    def __init__(self, num_features, eps=1e-5, momentum=0.1):
        super(AdaptiveInstanceNorm2d, self).__init__()
        self.num_features = num_features
        self.eps = eps
        self.momentum = momentum
        # weight and bias are dynamically assigned
        self.weight = None
        self.bias = None
        # just dummy buffers, not used
        self.register_buffer('running_mean', torch.zeros(num_features))
        self.register_buffer('running_var', torch.ones(num_features))

    def forward(self, x):
        assert self.weight is not None and self.bias is not None, "Please assign weight and bias before calling AdaIN!"
        b, c = x.size(0), x.size(1)
        running_mean = self.running_mean.repeat(b)
        running_var = self.running_var.repeat(b)

        # Apply instance norm
        x_reshaped = x.contiguous().view(1, b * c, *x.size()[2:])

        out = F.batch_norm(
            x_reshaped, running_mean, running_var, self.weight, self.bias,
            True, self.momentum, self.eps)

        return out.view(b, c, *x.size()[2:])

    def __repr__(self):
        return self.__class__.__name__ + '(' + str(self.num_features) + ')'

class LayerNorm(nn.Module):
    def __init__(self, n_out, eps=1e-5, affine=True):
        super(LayerNorm, self).__init__()
        self.n_out = n_out
        self.affine = affine

        if self.affine:
          self.weight = nn.Parameter(torch.ones(n_out, 1, 1))
          self.bias = nn.Parameter(torch.zeros(n_out, 1, 1))

    def forward(self, x):
        normalized_shape = x.size()[1:]
        if self.affine:
          return F.layer_norm(x, normalized_shape, self.weight.expand(normalized_shape), self.bias.expand(normalized_shape))
        else:
          return F.layer_norm(x, normalized_shape)

class DownsampleResBlock(nn.Module):
    def __init__(self, input_dim, output_dim, norm='in', activation='relu', pad_type='zero', use_sn=False):
        super(DownsampleResBlock, self).__init__()
        self.conv_1 = nn.ModuleList()
        self.conv_2 = nn.ModuleList()

        self.conv_1.append(Conv2dBlock(input_dim,input_dim,3,1,1,'none',activation,pad_type,use_sn=use_sn))
        self.conv_1.append(Conv2dBlock(input_dim,output_dim,3,1,1,'none',activation,pad_type,use_sn=use_sn))
        self.conv_1.append(nn.AvgPool2d(kernel_size=2, stride=2))
        self.conv_1 = nn.Sequential(*self.conv_1)

        self.conv_2.append(nn.AvgPool2d(kernel_size=2, stride=2))
        self.conv_2.append(Conv2dBlock(input_dim,output_dim,1,1,0,'none',activation,pad_type,use_sn=use_sn))
        self.conv_2 = nn.Sequential(*self.conv_2)

    def forward(self, x):
        out = self.conv_1(x) + self.conv_2(x)
        return out


from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
import numpy as np

x = np.arange(20).reshape((10, 2))
x2 = np.arange(10).reshape((5, 2))
y = np.arange(10)
y2 = np.arange(5)

cv = KFold(n_splits=5, shuffle=True, random_state=0)
for (train1_idx, eval1_idx), (train2_idx, eval2_idx) in zip(cv.split(x), cv.split(x2)):
    print('train idx:', train1_idx, train2_idx)
    print('eval idx:', eval1_idx, eval2_idx)
    print('.' * 50)

    print(x[train1_idx])


# SWEA에 제출하면 오답으로 나오는데 이유가 궁금합니다.. 제가 어떤 부분을 놓친건지 모르겠습니다.

T = int(input())

for t in range (1, T+1) :
    S = list(input())
    ans = ''
    for i in S :
        for j in S :
            if S.count(i) and S.count(j) == 2:
                ans = 'Yes'
            else :
                ans = 'No'
    print(f"#{t} {ans}")


# keywords
IF = 1 # if
THEN = 2 # then
ELSE = 3 # else
END = 4 # end
REPEAT = 5 # repeat
UNTIL = 6 # until
READ = 7 # read
WRITE = 8 # write

# operators
PLUS = 101  # +
MINUS = 102 # -
MULTIPLY = 103 # *
DIVIDE = 104 # /
EQUAL = 105 # =
LESS_THAN = 106 # <
LEFT_BRACE = 107 # (
RIGHT_BRACE = 108 # )
SEMICOLON = 109 # ;
ASSIGN = 110 # :=

# literal and identifier
NUMBER = 201 # 1 or more digits
IDENTIFIER = 202 # 1 or more letters

EOS = 301 # end of whole string
ERROR = 302 # error when scaning token

# for debug dump info
TOKEN_TO_STRING = {
    1:'IF',
    2:'THEN',
    3:'ELSE',
    4:'END',
    5:'REPEAT',
    6:'UNTIL',
    7:'READ',
    8:'WRITE',

    101:'PLUS',
    102:'MINUS',
    103:'MULTIPLY',
    104:'DIVIDE',
    105:'EQUAL',
    106:'LESS_THAN',
    107:'LEFT_BRACE',
    108:'RIGHT_BRACE',
    109:'SEMICOLON',
    110:'ASSIGN',

    201:'NUMBER',
    202:'IDENTIFIER',

    301:'EOS',
    302:'ERROR',
}


from django.shortcuts import render, redirect
from .models import ImageList, Tester, Rating
import os
import csv
import logging

logger = logging.getLogger(__name__)

# 登录页面后先查看实验须知
def experiment_intro(request):
    return render(request, 'experiment_intro.html')

# 确认进入实验后提交被试编号和被试信息
#
def enter_info(request):
    if request.method == 'POST':
        age = request.POST.get('age')
        gender = request.POST.get('gender')
        subject_number = request.POST.get('subject_number')
        school_number = request.POST.get('school_number')
        account = request.POST.get("account")

        tester = Tester.objects.create(age=age, gender=gender, subject_number=subject_number, account=account, school_number=school_number)

        return redirect('experiment', tester_id=tester.id)  # 跳转到实验界面
    return render(request, 'enter_info.html')

def test_temp(request):
    return render(request, 'htmltemp.html')

def thank_you(request):
    return render(request, 'thank_you.html')

def experiment(request, tester_id):
    # 初始化tester
    tester = Tester.objects.get(pk=tester_id)

    # 根据被试输入的编号找到对应的图片列表
    subject_number = tester.subject_number
    image_names = ImageList.objects.get(name=f'image_list_{subject_number}.txt')
    print(image_names)

    # 当点击评分按钮后的反应
    if request.method == 'POST':
        # 处理用户提交的评分数据
        rating_value = request.POST.get('rating')
        rated_image_name = request.session.get('rated_image_name')
        print(f"rated_image_name: {rated_image_name}")
        logger.debug(f"rated_image_name : {rated_image_name}")
        # 保存评分到数据库
        rating = Rating.objects.create(user=tester, image=rated_image_name, rating=rating_value)

    # 从缓存中读取最新的图片列表（包含初始化
    image_list = request.session.get('image_list', image_names.get_image_names())
    # 从缓存中读取当前的图片序号
    no = request.session.get('no',0)
    # print(image_list)
    no = no + 1
    # 获取当前图片的名字和序号
    if no==150:
        current_image_name = "lowmeaning-pacth1.png"
    else:
        current_image_name = image_list[0] if image_list else None  # 默认为第一张图片
        # 更新图片列表，删除已经保存并即将递送显示的第一张图片
        image_list = image_list[1:]
    # 更新图片列表到缓存
    request.session['image_list'] = image_list
    request.session['rated_image_name'] = current_image_name
    request.session['no'] = no
    print(current_image_name)

    return render(request, "experiment.html", {'current_image_name': current_image_name, 'no': no})

def test_rate_single_image(request):
    csv_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data', 'outcome.csv')
    print("here")

    if request.method == 'POST':
        # 处理用户提交的评分数据
        rating = request.POST.get('rating')
        rated_image_name = request.session.get('rated_image_name')

        # 保存评分到数据库
        if not os.path.exists(csv_file_path):
            # 如果不存在，创建一个新的csv文件并写入头部信息和当前数据
            with open(csv_file_path, 'w', newline='') as csvfile:
                fieldnames = ['rated_image_name', 'rating']
                writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
                writer.writeheader()
                writer.writerow({'rated_image_name': rated_image_name, 'rating': rating})
        else:
            # 如果已存在，直接追加数据
            with open(csv_file_path, 'a', newline='') as csvfile:
                fieldnames = ['rated_image_name', 'rating']
                writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

                # 将current_image_name和rating写入csv
                writer.writerow({'rated_image_name': rated_image_name, 'rating': rating})

    # 从缓存中读取最新的图片列表
    image_list = request.session.get('image_list', ['fine/4_1.png', 'fine/4_2.png', 'fine/4_3.png'])
    print(image_list)
    # 获取当前图片的名字
    current_image_name = image_list[0] if image_list else None  # 默认为第一张图片
    image_list = image_list[1:]  # 删除已经显示的第一张图片
    # 更新图片列表到缓存
    request.session['image_list'] = image_list
    request.session['rated_image_name'] = current_image_name
    print(current_image_name)

    return render(request, 'test_rate_single_image.html', {'current_image_name': current_image_name})


from django.conf.urls import include, url
from paytm import views
from django.urls import path

urlpatterns = [
    # Examples:
    path('paytm/', views.home, name='home'),
    path('paytm/payment/', views.payment, name='payment'),
    path('paytm/response/', views.response, name='response'),
    path('charge/', views.app_charge, name='razor'),
    path('charge-razor/', views.razor_final, name='razor-final'),
]


from .read_meta_quandl import ( all_products, all_nrbys, all_months, all_years, all_fields, all_names, train_years, test_years, verify_years )
from .GSCI import GSCIData

products     = all_products()
nrbys        = all_nrbys()
months       = all_months()
years        = all_years()
fields       = all_fields()
name_map     = all_names()
train_years  = train_years()
test_years   = test_years()
verify_years = verify_years()


from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms
from django.contrib.auth.forms import AuthenticationForm
from django.utils.translation import gettext_lazy as _
from .models import Customer

class SignUpForm(UserCreationForm):
    date_of_birth = forms.DateField()

    class Meta:
        model = User
        fields = ('username', 'email', 'password1', 'password2', 'date_of_birth')

class RememberMeAuthenticationForm(AuthenticationForm):
    remember_me = forms.BooleanField(
        required=False,
        initial=True,
        widget=forms.CheckboxInput(attrs={'class': 'form-check-input'}),
        label=_('Remember me')
    )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if 'remember_me' in self.data:
            self.fields['remember_me'].initial = self.data['remember_me']

class CustomerForm(forms.ModelForm):
    class Meta:
        model = Customer
        fields = [
            'first_name',
            'middle_name',
            'last_name',
            'date_of_birth',
            'ssn',
            'citizenship',
            'country_of_residence',
            'address',
            'state',
            'zipcode',
            'passport',
            'utility_bill'
        ]


from assets import Asset
from indi import CCI

STOCKS = ['DIS', 'WFC', 'VZ', 'T', 'KO', 'BA', 'ADBE', 'CAT', 'INTC', 'AAPL', 'AXP', 'C', 'CSCO', 'DIS', 'EBAY', 'F', 'FB', 'GS', 'HD', 'HOG', 'HPQ', 'IBM', 'ITX', 'JNJ', 'FE', 'SCI', 'GTN', 'MSGN', 'USM', 'DISCA', 'OGE', 'AROW', 'EXPO', 'TLP', 'MMT', 'LION', 'ATI', 'MYGN']

#STOCKS = ['T']
black=0
white=0
doji =0
hammer = 0
ss = 0
gapup = 0
gapdown = 0
ssum = 0
bs=0
ccup = 0
ccupc = 0

for s in STOCKS:
    chart = Asset()
    chart.load_av_history('AVHD', s)
    chart.range_from_last(750)

    for i in range(chart.range_from, chart.range_to):
        this = chart.get()
        prev = chart.get(-1)

        ssum+=this.close_price - this.open_price

        delta = this.close_price - prev.close_price
        #delta = this.close_price - this.open_price

        if prev.is_bullish():
            white = white+delta
        if prev.is_bearish():
            black+=delta
        if prev.is_doji():
            doji+=delta
        if prev.is_hammer():
            hammer +=delta
        if prev.is_shooting_star():
            ss +=delta
        if prev.is_shooting_star() and prev.is_bearish():
            bs+=delta

        cond = CCI(chart.last(14,-1))<0
        if CCI(chart.last(2,-1))>CCI(chart.last(2,-2)) and CCI(chart.last(2,-2))<CCI(chart.last(2,-3)) and cond:
            ccup+=delta
            ccupc +=1
            print (ccup)

        chart.next()

print("B: %r, W: %r, D:%r, H:%r, S:%r, CCUP:%r" % (black, white, doji, hammer, ss, ccup))
print(ccupc)


import matplotlib.pyplot as plt
import numpy as np
import tkinter as tk
from tkinter import filedialog
from tkinter import simpledialog
from tkinter import messagebox
root = tk.Tk()
root.withdraw()

answer = simpledialog.askstring("Input", "Bitte gib die Nachricht ein!",
                                parent=root)

print( answer)

output = "{"

for zahl in range(0,len(answer),3):
    output = output +("0x"+answer[zahl:zahl+2]+", ")
output = output[0:len(output)-2]+"},"

root.clipboard_clear()
root.clipboard_append(output)
root.update()

messagebox.showinfo("Ergebnis wurde in die Zwischenablage kopiert!",output)
print (output)


'''
Schrijf een script dat gebruikt kan worden om getallen af te printen.
Het script ondersteunt de volgende argumenten:

    --init      Beginwaarde. Optioneel, standaardwaarde 0.
    --to        Eindwaarde. Verplicht.
    --step      Stapwaarde. Optioneel, standaardwaarde 1.

Voorbeeld 1

    python student.py --to 5

geeft

    0
    1
    2
    3
    4
    5

Voorbeeld 2

    python student.py --init 2 --to 10 --step 2

drukt af

    2
    4
    6
    8
    10

Steun op de module argparse (zoek informatie online en experimenteer)
om de command line arguments te parsen. Bijvoorbeeld,

    parser = argparse.ArgumentParser()
    parser.add_argument('--to', type=int, required=True)
    args = parser.parse_args()

zorgt ervoor dat je script een parameter "--to N" verwacht, waarbij
N een integer is. De variabele args bevat nadien een object
met veld 'to'. Bijvoorbeeld,

    python student.py --to 42

zorgt ervoor dat args.to gelijk is aan 42. Lees de documentatie
van argparse om te weten te komen hoe je optionele parameters kan instellen.
'''

import argparse

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--init', type=int)
    parser.add_argument('--to', type=int)
    parser.add_argument('--step', type=int)
    args = parser.parse_args()

    for i in range(args.init, args.to + 1, args.step):
        print(i)


"""Ontology Evaluator class for evaluating the cross-ontology mappings computed by OA models.
"""
from typing import List, Optional, Union

import pandas as pd

import bertmap
from bertmap.utils import uniqify
from pandas.core.frame import DataFrame

class OntoEvaluator:

    na_vals = bertmap.na_vals
    namespaces = bertmap.namespaces

    def __init__(
        self,
        pre_tsv: Union[str, DataFrame],
        ref_tsv: Union[str, DataFrame],
        ref_ignored_tsv: Optional[Union[str, DataFrame]] = None,
        threshold: float = 0.0,
    ):

        # filter the prediction mappings according to similarity scores
        self.pre = self.read_mappings(pre_tsv, threshold=threshold)
        self.id_maps = []  # mapping of same iris should be ignored
        self.identity_mapping_check()
        self.num_ignored = len(self.id_maps)

        # reference mappings and illegal mappings to be ignored
        self.ref = self.read_mappings(ref_tsv)
        if ref_ignored_tsv is None:
            self.ref_ignored = None
        else:
            self.ref_ignored = self.read_mappings(ref_ignored_tsv)

        # convert list to set for fast computation
        self.pre_set = set(self.pre)
        self.ref_set = set(self.ref)
        if self.ref_ignored:
            self.ref_ignored_set = set(self.ref_ignored)
            self.pre_unignored_set = self.pre_set - self.ref_ignored_set
            self.ref_unignored_set = self.ref_set - self.ref_ignored_set

        # compute Precision, Recall and Macro-F1
        try:
            self.P = self.precision()
            self.R = self.recall()
            self.F1 = self.f1()
        except:
            self.P = -1.0
            self.R = -1.0
            self.F1 = -1.0

    def precision(self) -> float:
        """
        % of predictions are correct:
            P = TP / (TP + FP)
        """
        if self.ref_ignored:
            num_pre_ignored = len(self.pre_set) - len(self.pre_unignored_set)
            self.num_ignored += num_pre_ignored
        # True Positive = the number of unignored prediction mappings that are True
        tp = len(self.pre_unignored_set.intersection(self.ref_set))
        # False Positive = the number of unignored prediction mappings that are False
        fp = len(self.pre_set) - tp - num_pre_ignored
        return tp / (tp + fp)

    def recall(self) -> float:
        """
        % of correct retrieved
            R = TP / (TP + FN)
        """
        if self.ref_ignored:
            num_ref_ignored = len(self.ref_set) - len(self.ref_unignored_set)
            self.num_ignored += num_ref_ignored
        # True Positive = the number of unignored reference mappings that are Positive
        tp = len(self.ref_unignored_set.intersection(self.pre_set))
        # False Negative = the number of unignored reference mappings that are Negative
        fn = len(self.ref_set) - tp - num_ref_ignored
        return tp / (tp + fn)

    def f1(self) -> float:
        return 2 * self.P * self.R / (self.P + self.R)

    def identity_mapping_check(self) -> None:
        for p_map in self.pre:
            if p_map.split("\t")[0] == p_map.split("\t")[1]:
                self.id_maps.append(p_map)
        self.pre = list(set(self.pre) - set(self.id_maps))
        if len(self.id_maps) > 0:
            print(f"detect and delete {len(self.id_maps)} mappings of identical iris ...")

    @classmethod
    def read_mappings(
        cls, mapping_file: Union[str, DataFrame], threshold: float = 0.0
    ) -> List[str]:
        """read unique mappings from tsv file or pandas.DataFrame"""
        if type(mapping_file) is DataFrame:
            _df = mapping_file
        else:
            _df = pd.read_csv(mapping_file, sep="\t", na_values=cls.na_vals, keep_default_na=False)
        mappings = [
            "\t".join(_df.iloc[i][:-1]) for i in range(len(_df)) if _df.iloc[i][-1] >= threshold
        ]
        return uniqify(mappings)


from warnings import catch_warnings
from numpy import character
import sqlalchemy as sql
from sqlalchemy.sql import text
import pandas as pd
import numpy as np

db_connection_str = 'mysql+pymysql://username:password@localhost:3306/AcademicWorld' #connect your loacal sql by your password and name
db_connection = sql.create_engine(db_connection_str)

def getTop5Professor():
    sql = "SELECT f.name, f.position, p.title, f.research_interest, f.photo_url, u.name FROM publication p JOIN faculty_publication fp on fp.publication_id = p.id JOIN faculty f on fp.faculty_id = f.id JOIN university u on f.university_id = u.id ORDER BY num_citations DESC LIMIT 5;"
    data = db_connection.execute(text(sql)).fetchall();
    data2 = [list(i) for i in data]
    return data2

def getTop5Areas():
    sql = "SELECT name, count FROM keyword JOIN (SELECT keyword_id, COUNT(keyword_id) as count FROM publication_keyword GROUP BY keyword_id ORDER BY COUNT(keyword_id) DESC LIMIT 5) temp ON id = temp.keyword_id;"
    data = db_connection.execute(text(sql)).fetchall();
    return data

def getTop5Article(keyword_name):
    sql = "SELECT title, num_citations FROM keyword f JOIN publication_keyword pk on f.id = pk.keyword_id JOIN publication p ON pk.publication_id = p.id WHERE f.name = :x ORDER BY num_citations DESC LIMIT 5;"
    data = db_connection.execute(text(sql), {'x': keyword_name}).fetchall()
    return data

def getPopularArticle():
    top5Areas = getTop5Areas()
    info = []
    character = [i[0] for i in top5Areas]
    parent = ["", "", "", "", ""]
    value = [i[1] for i in top5Areas]
    for i in top5Areas:
        area = i[0]
        article = getTop5Article(area)
        info.append(article)
        for j in article:
            character.append(j[0])
            parent.append(i[0])
            value.append(j[1])
    return character, parent, value

def insertCitaion(titleName):
    sql1 = "SELECT p.id FROM publication p WHERE title = :x;"
    data = db_connection.execute(text(sql1), {'x': titleName}).fetchall()
    if(len(data) == 0):
        return []
    try:
        sql2 = "INSERT INTO CitationList VALUES (:y);"
        db_connection.execute(text(sql2), {'y': data[0][0]})
    except:
        return [0]
    else:
        return data

def createTable():
    sql0 = "DROP TABLE IF EXISTS CitationList;"
    db_connection.execute(text(sql0))
    sql = "CREATE TABLE CitationList ( publication_id int primary key );"
    db_connection.execute(text(sql))

def getCitationList():
    sql = "SELECT title, name, year, num_citations FROM publication p JOIN CitationList c on p.id = c.publication_id JOIN faculty_publication fp ON p.id = fp.publication_id JOIN faculty f ON f.id = fp.faculty_id;"
    data = db_connection.execute(text(sql)).fetchall()
    data2 = [list(i) for i in data]
    data3 = list(map(list, zip(*data2)))
    return data3

def getTrend(topic_name):
    sql = "SELECT year, count(id) FROM (SELECT p.id, year, keyword_id FROM publication p JOIN publication_keyword pk ON p.id = pk.publication_id JOIN keyword k on pk.keyword_id = k.id WHERE k.name = :x) temp GROUP BY year ORDER BY year;"
    data = db_connection.execute(text(sql), {'x': topic_name}).fetchall()
    year = [i[0] for i in data]
    num = [i[1] for i in data]
    return year, num

def removeCitationList():
    if (getCitationList() == []):
        return;
    titles = getCitationList()[0]
    for j in titles:
        str = "UPDATE publication SET num_citations = num_Citations + 1 WHERE title = :x";
        db_connection.execute(text(str), {"x":j})
    sql = 'DELETE FROM CitationList;'
    db_connection.execute(text(sql))
    return;


#!/usr/bin/env python3
import rclpy
from rclpy.node import Node
from rclpy.qos import QoSDurabilityPolicy, QoSReliabilityPolicy
from rclpy.qos import QoSProfile

from example_interfaces.msg import String
from sensor_msgs.msg import LaserScan

from time import sleep

### LIDAR RANGE VALUE INDICES ###

     ######## FRONT ########
     ######## 0/450 ########
     ######
     ### 112,5     337,5 ###
     ######
     ########  225  ########
     ########  BACK ########

######## COUNTERCLOCKWISE #######

class lidarComms(Node):
    def __init__(self):
        super().__init__("lidar_comms_node")

        # creating compatible quality of service profile for laser scan data
        qos_profile = QoSProfile(depth=10)
        qos_profile.reliability = QoSReliabilityPolicy.BEST_EFFORT
        qos_profile.durability = QoSDurabilityPolicy.VOLATILE

        # get data from lidar
        self.laser_subscriber = self.create_subscription(LaserScan, "/ldlidar_node/scan", self.handle_scan, qos_profile)

        # publish environment data based on computated lidar data
        self.environment_state = "path_blocked"
        self.publish_rate = 0.01

        self.environment_state_publisher = self.create_publisher(String, "environment_state", 10)
        self.publish_timer = self.create_timer(self.publish_rate, self.publish_state)

        self.get_logger().info("LIDAR COMMUNICATION SUCCESSFULLY INITIATED")

    def handle_scan(self, msg):
        MIN_DISTANCE = 6.0

        try:
            # checking left of x axis
            for i in range(0, 22):
                if (msg.ranges[i] * 10) <= MIN_DISTANCE:
                    self.environment_state = "path_blocked"
                    return

            # checking right of x axis
            for i in range(430, 450):
                if (msg.ranges[i] * 10) <= MIN_DISTANCE:
                    self.environment_state = "path_blocked"
                    return

            self.environment_state = "path_clear"

        except IndexError:
            # IndexError can occur because laser range[] index can varry between 445 - 455
            pass

        # throttling computation
        sleep(0.01)

    def publish_state(self):
        msg = String()
        msg.data = self.environment_state
        self.environment_state_publisher.publish(msg)

def main(args=None):
    rclpy.init(args=args)
    node = lidarComms()
    rclpy.spin(node)
    rclpy.shutdown()

if __name__ == "__main__":
    main()


### READ ME ###
# IMPORTANT: You must run the bot by being on the main file
# then running else if you try running the bot in a cog, it will not start.
#
# HI, So i re created your whole bot, don't know what your bot is called
# so i just called this project (NoobsBot) im asuming you know how to
# rename files/projects ect... anyways every single comment you can
# delete if you want or keep. I suggest you save comments so that you
# can use it as later refrenece, This whole top msg can be deleted.
# PS: You are going to need to download some moudles if you do not have them
# i'll try giving you the commands assuming you know how to install moudles,
# its the same way you had to install the discord moudle. any more help lmk
# - Skely :)

# NOTE: ANYTHING THAT ONLY USES @Client and not @Commands, such as your meme, must be put here in the main file.
### End ###

import discord
import random
import os
from discord.ext import tasks
from discord.ext import commands
from dotenv import load_dotenv # Moudle: py -m pip install python-dotenv

# Grabs Token from .env | This stops your token from getting taken & leaked by hackers
# Note: Do Not add anything else to the .env file else it will error
load_dotenv()
Token = os.getenv('DISCORD_TOKEN')

client = commands.Bot(command_prefix="=")
# Disables default help command
client.remove_command("help")

# Optional welcome on console
print('Welcome name here')

# Creates a new cog | cog is a extension file, it helps to organize
# Read More: https://discordpy.readthedocs.io/en/stable/ext/commands/cogs.html

# Note, Create cogs in the folder nammed "cogs" find it in solution explorer
# Steps, "cogs" folder>Right Click>Add>New Item...>Empty Python File>Name it name then add .py
# example, Skely.py Then add to the extentions
# example
# extensions = ['cogs.help', # MAKE SURE U ADD A , (Comma)
#              'cogs.moderation',
#              'cogs.Skely.py'] # THE LAST COG DOES NOT NEED TO HAVE A COMMA

# To set up a cog, head over to help.py where i show you how.

extensions = ['cogs.help',
              'cogs.moderation',
              'cogs.fun',
              'cogs.eventhandler',
              'cogs.meme',
              'cogs.games',
              'cogs.report']

# Loads cogs when running
if __name__ == '__main__':
    for extension in extensions:
        client.load_extension(extension)

# Loads bot
@client.event
async def on_ready():
    # Shows username
    print(f'logged in as: {client.user.name}')
    # Shows bot
    print(f"Connected to {client.user}")
    # Shows how many servers your bot is in
    print(f"serving in {len(client.guilds)} servers")

# Runs bot with token
client.run(Token)


from playwright.sync_api import sync_playwright

# 'url' is the URL that playwright's browser shpuld visit
# 'session_data' is the data stored in your own session, this needs to be transferred to be worked with
def take_screenshot_from_url(url, session_data):
    with sync_playwright() as playwright:   # Using a context manager to wrap playwright code, ensures python sets up and tears down playwright instance properly
        webkit = playwright.webkit  # Create headless webkit browser
        browser = webkit.launch()   # launch the webkit browser
        browser_context = browser.new_context(device_scale_factor=2)    # set up new context for browser, and increae deviec_scale_factor at 2 to ensure pictures aren't pixelated
        browser_context.add_cookies([session_data]) # Add session info to the browser
        page = browser_context.new_page()   # open a new page in the browser_context, like a new browser window
        page.goto(url)  # visit the target url
        screenshot_bytes = page.locator(".code").screenshot()   # Take a ss of the image, this returns a buffer with the image
        browser.close()
        return screenshot_bytes # Return the image buffer


import heapq
class Solution:
    def nthUglyNumber(self, n: int) -> int:
        heap = []
        heapq.heappush(heap, 1)

        seen = set()
        seen.add(1)

        factors = [2, 3, 5]

        curr_ugly = 1

        for _ in range(n):
            # 每次弹出当前最小丑数
            curr_ugly = heapq.heappop(heap)
            # 生成新的丑数
            for f in factors:
                new_ugly = curr_ugly * f
                if new_ugly not in seen:
                    seen.add(new_ugly)
                    heapq.heappush(heap, new_ugly)
        return curr_ugly

import heapq
class Solution:
    def nthUglyNumber(self, n: int) -> int:
        uglys = []
        uglys.append(1)

        p2, p3, p5 = 0, 0, 0

        for i in range(1, n):
            last_number = uglys[i - 1]
            while uglys[p2] * 2 <= last_number:
                p2 += 1
            while uglys[p3] * 3 <= last_number:
                p3 += 1
            while uglys[p5] * 5 <= last_number:
                p5 += 1

            uglys.append(min(uglys[p2] * 2, uglys[p3] * 3, uglys[p5] * 5))

        return uglys[n - 1]

      #time complexity: O(n), but thought process isn't easy, so don't recommend this method


import csv

from boss_zhipin import Boss
from zhilian_zhaopin import Zhilian
from lagou_net import Lagou

header = ['职位名','薪资','地点','经验','学历','公司名','详细信息url']
with open('jobs.csv','w',newline='')as f:
    csv_writer = csv.writer(f)
    csv_writer.writerow(header)

boss = Boss()
zhilian = Zhilian()
lagou = Lagou()

print('************开始爬BOSS直聘************')
boss.main()
print('************开始爬智联招聘************')
zhilian.main()
print('************开始爬拉钩网************')
lagou.main()


#znalezc max, min przy pomocy 3/2n porownan

def solve(t):
    n = len(t)
    curr_min, curr_max = t[0]
    for i in range(1, n - 1, 2):
        if t[i] > t[i + 1]:
            curr_max = max(curr_max, t[i])
            curr_min = min(curr_min, t[i])
        else:
            curr_max = max(curr_max, t[i + 1])
            curr_min = min(curr_min, t[i + 1])
    if n % 2 == 0:
        curr_max = max(curr_max, t[n - 1])

# dana jest posorotwana tablica t z dodatnimi wartosciami oraz zadajemy
# liczbe x, znalezc takie 2 indeksy i, j, ze roznica t[j] - t[i] = x

def solve(t, target):
    n = len(t)
    i, j = 0, 1
    curr_val = t[j] - t[i]
    while curr_val != target:
        curr_val = t[j] - t[i]
        if i == n or j == n:
            return -1, -1
        if curr_val < target:
            j += 1
        elif curr_val > target:
            i += 1
        else:
            return i, j

print(solve([1, 3, 6, 8, 10], 5))

# posorotwany ciag liczb i elementy a0,...,an-1, wartosci ai to liczby
# z zakresu [0, m-1] liczby w ciagu sa parami rozne
# n < m najmniejsza liczba calkowita ktorej nie ma w tym ciagu


# !/usr/bin/python
# -*- coding:utf-8 -*-

import requests
import re
import csv

def getHtml(url):
    """
    get the webpage source code.
    :param url:
    :return:
    """
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0'
    }
    try:
        r = requests.get(url=url, headers=headers, timeout=10)
        r.raise_for_status()
        r.encoding = 'utf-8'
        return r.text
    except:
        return ''

def parsePage(html, infoList):
    """
    parse html,return commodity infomation list.
    :param html:
    :return: infoList
    """
    try:
        price = re.findall(r'"view_price":"(.*?)"', html)
        num = len(price)
        title = re.findall(r'"raw_title":"(.*?)"', html)
        sales = re.findall(r'"view_sales":"(\d+)人付款"', html)
        taobaoID = re.findall(r'"nick":"(.*?)"', html)[:num]
        location = re.findall(r'"item_loc":"(.*?)"', html)
        for i in range(num):
            infoList.append([title[i], price[i], sales[i], taobaoID[i], location[i]])
        return infoList
    except:
        return ''

def writeCommodity(infoList):
    """
    write the commodity list to the taobaoCommodity.csv file,
    :param infoList:
    :return:
    """
    with open('csv/书包.csv', 'w', encoding='utf-8_sig', newline='') as f:
        writer = csv.writer(f)
        writer.writerow(['名称', '价格', '付款人数', '淘宝ID', '店铺地址'])
        writer.writerows(infoList)

def main():
    commodity = '书包'
    depth = 101
    infoList = []
    for i in range(depth):
        try:
            i *= 44
            url = 'https://s.taobao.com/search?q={}&s={}'.format(commodity, i)
            html = getHtml(url)
            infoList = parsePage(html, infoList)
        except:
            continue
    infoList = sorted(infoList, key=lambda x:int(x[2]), reverse=True)   # 商品按销量降序排列
    writeCommodity(infoList)

if __name__ == '__main__':
    main()