"""
This code obtained from, the code published by Karim et al.
# which implemented LSTM-FCN model https://github.com/titu1994/LSTM-FCN
# Special thanks for the code availability
# Please cite our paper and Karim et al. paper in case of using our code.
"""
import numpy as np
import pandas as pd
import os
import matplotlib as mpl
import matplotlib.pylab as plt

mpl.style.use('seaborn-paper')

from utils.constants import TRAIN_FILES, TEST_FILES, MAX_SEQUENCE_LENGTH_LIST, NB_CLASSES_LIST

def load_dataset_at(index, normalize_timeseries=False, verbose=True) -> (np.array, np.array):
    assert index < len(TRAIN_FILES), "Index invalid. Could not load dataset at %d" % index
    if verbose: print("Loading train / test dataset : ", TRAIN_FILES[index], TEST_FILES[index])

    if os.path.exists(TRAIN_FILES[index]):
        df = pd.read_csv(TRAIN_FILES[index], header=None, encoding='latin-1')
    elif os.path.exists(TRAIN_FILES[index][1:]):
        df = pd.read_csv(TRAIN_FILES[index][1:], header=None, encoding='latin-1')
    else:
        raise FileNotFoundError('File %s not found!' % (TRAIN_FILES[index]))

    is_timeseries = True # assume all input data is univariate time series

    # remove all columns which are completely empty
    df.dropna(axis=1, how='all', inplace=True)

    if not is_timeseries:
        data_idx = df.columns[1:]
        min_val = min(df.loc[:, data_idx].min())
        if min_val == 0:
            df.loc[:, data_idx] += 1

    # fill all missing columns with 0
    df.fillna(0, inplace=True)

    # cast all data into integer (int32)
    if not is_timeseries:
        df[df.columns] = df[df.columns].astype(np.int32)

    # extract labels Y and normalize to [0 - (MAX - 1)] range
    y_train = df[[0]].values
    nb_classes = len(np.unique(y_train))
    y_train = (y_train - y_train.min()) / (y_train.max() - y_train.min()) * (nb_classes - 1)

    # drop labels column from train set X
    df.drop(df.columns[0], axis=1, inplace=True)

    X_train = df.values

    if is_timeseries:
        X_train = X_train[:, np.newaxis, :]
        # scale the values
        if normalize_timeseries:
            X_train_mean = X_train.mean()
            X_train_std = X_train.std()
            X_train = (X_train - X_train_mean) / (X_train_std + 1e-8)

    if verbose: print("Finished loading train dataset..")

    if os.path.exists(TEST_FILES[index]):
        df = pd.read_csv(TEST_FILES[index], header=None, encoding='latin-1')
    elif os.path.exists(TEST_FILES[index][1:]):
        df = pd.read_csv(TEST_FILES[index][1:], header=None, encoding='latin-1')
    else:
        raise FileNotFoundError('File %s not found!' % (TEST_FILES[index]))

    # remove all columns which are completely empty
    df.dropna(axis=1, how='all', inplace=True)

    if not is_timeseries:
        data_idx = df.columns[1:]
        min_val = min(df.loc[:, data_idx].min())
        if min_val == 0:
            df.loc[:, data_idx] += 1

    # fill all missing columns with 0
    df.fillna(0, inplace=True)

    # cast all data into integer (int32)
    if not is_timeseries:
        df[df.columns] = df[df.columns].astype(np.int32)

    # extract labels Y and normalize to [0 - (MAX - 1)] range
    y_test = df[[0]].values
    nb_classes = len(np.unique(y_test))
    y_test = (y_test - y_test.min()) / (y_test.max() - y_test.min()) * (nb_classes - 1)

    # drop labels column from train set X
    df.drop(df.columns[0], axis=1, inplace=True)

    X_test = df.values

    if is_timeseries:
        X_test = X_test[:, np.newaxis, :]
        # scale the values
        if normalize_timeseries:
            X_test = (X_test - X_train_mean) / (X_train_std + 1e-8)

    if verbose:
        print("Finished loading test dataset..")
        print()
        print("Number of train samples : ", X_train.shape[0], "Number of test samples : ", X_test.shape[0])
        print("Number of classes : ", nb_classes)
        print("Sequence length : ", X_train.shape[-1])

    return X_train, y_train, X_test, y_test, is_timeseries

def calculate_dataset_metrics(X_train):
    is_timeseries = len(X_train.shape) == 3
    if is_timeseries:
        # timeseries dataset
        max_sequence_length = X_train.shape[-1]
        max_nb_words = None
    else:
        # transformed dataset
        max_sequence_length = X_train.shape[-1]
        max_nb_words = np.amax(X_train) + 1

    return max_nb_words, max_sequence_length

def plot_dataset(dataset_id, seed=None, limit=None, cutoff=None,
                 normalize_timeseries=False, plot_data=None,
                 type='Context', plot_classwise=False):
    np.random.seed(seed)

    if plot_data is None:
        X_train, y_train, X_test, y_test, is_timeseries = load_dataset_at(dataset_id,
                                                               normalize_timeseries=normalize_timeseries)

        if not is_timeseries:
            print("Can plot time series input data only!\n"
                  "Continuing without plot!")
            return

        max_nb_words, sequence_length = calculate_dataset_metrics(X_train)

        if sequence_length != MAX_SEQUENCE_LENGTH_LIST[dataset_id]:
            if cutoff is None:
                choice = cutoff_choice(dataset_id, sequence_length)
            else:
                assert cutoff in ['pre', 'post'], 'Cutoff parameter value must be either "pre" or "post"'
                choice = cutoff

            if choice not in ['pre', 'post']:
                return
            else:
                X_train, X_test = X_test(X_train, X_test, choice, dataset_id, sequence_length)

        X_train_attention = None
        X_test_attention = None

    else:
        X_train, y_train, X_test, y_test, X_train_attention, X_test_attention = plot_data

    if limit is None:
        train_size = X_train.shape[0]
        test_size = X_test.shape[0]
    else:
        if not plot_classwise:
            train_size = limit
            test_size = limit
        else:
            assert limit == 1, 'If plotting classwise, limit must be 1 so as to ensure number of samples per class = 1'
            train_size = NB_CLASSES_LIST[dataset_id] * limit
            test_size = NB_CLASSES_LIST[dataset_id] * limit

    if not plot_classwise:
        train_idx = np.random.randint(0, X_train.shape[0], size=train_size)
        X_train = X_train[train_idx, 0, :]
        X_train = X_train.transpose((1, 0))

        if X_train_attention is not None:
            X_train_attention = X_train_attention[train_idx, 0, :]
            X_train_attention = X_train_attention.transpose((1, 0))
    else:
        classwise_train_list = []
        for y_ in sorted(np.unique(y_train[:, 0])):
            class_train_idx = np.where(y_train[:, 0] == y_)
            classwise_train_list.append(class_train_idx[:])

        classwise_sample_size_list = [len(x[0]) for x in classwise_train_list]
        size = min(classwise_sample_size_list)
        train_size = min([train_size // NB_CLASSES_LIST[dataset_id], size])

        for i in range(len(classwise_train_list)):
            classwise_train_idx = np.random.randint(0, len(classwise_train_list[i][0]), size=train_size)
            classwise_train_list[i] = classwise_train_list[i][0][classwise_train_idx]

        classwise_X_train_list = []
        classwise_X_train_attention_list = []

        for classwise_train_idx in classwise_train_list:
            classwise_X = X_train[classwise_train_idx, 0, :]
            classwise_X = classwise_X.transpose((1, 0))
            classwise_X_train_list.append(classwise_X)

            if X_train_attention is not None:
                classwise_X_attn = X_train_attention[classwise_train_idx, 0, :]
                classwise_X_attn = classwise_X_attn.transpose((1, 0))
                classwise_X_train_attention_list.append(classwise_X_attn)

        classwise_X_train_list = [np.asarray(x) for x in classwise_X_train_list]
        classwise_X_train_attention_list = [np.asarray(x) for x in classwise_X_train_attention_list]

        # classwise x train
        X_train = np.concatenate(classwise_X_train_list, axis=-1)

        # classwise x train attention
        if X_train_attention is not None:
            X_train_attention = np.concatenate(classwise_X_train_attention_list, axis=-1)

    if not plot_classwise:
        test_idx = np.random.randint(0, X_test.shape[0], size=test_size)
        X_test = X_test[test_idx, 0, :]
        X_test = X_test.transpose((1, 0))

        if X_test_attention is not None:
            X_test_attention = X_test_attention[test_idx, 0, :]
            X_test_attention = X_test_attention.transpose((1, 0))
    else:
        classwise_test_list = []
        for y_ in sorted(np.unique(y_test[:, 0])):
            class_test_idx = np.where(y_test[:, 0] == y_)
            classwise_test_list.append(class_test_idx[:])

        classwise_sample_size_list = [len(x[0]) for x in classwise_test_list]
        size = min(classwise_sample_size_list)
        test_size = min([test_size // NB_CLASSES_LIST[dataset_id], size])

        for i in range(len(classwise_test_list)):
            classwise_test_idx = np.random.randint(0, len(classwise_test_list[i][0]), size=test_size)
            classwise_test_list[i] = classwise_test_list[i][0][classwise_test_idx]

        classwise_X_test_list = []
        classwise_X_test_attention_list = []

        for classwise_test_idx in classwise_test_list:
            classwise_X = X_test[classwise_test_idx, 0, :]
            classwise_X = classwise_X.transpose((1, 0))
            classwise_X_test_list.append(classwise_X)

            if X_test_attention is not None:
                classwise_X_attn = X_test_attention[classwise_test_idx, 0, :]
                classwise_X_attn = classwise_X_attn.transpose((1, 0))
                classwise_X_test_attention_list.append(classwise_X_attn)

        classwise_X_test_list = [np.asarray(x) for x in classwise_X_test_list]
        classwise_X_test_attention_list = [np.asarray(x) for x in classwise_X_test_attention_list]

        # classwise x test
        X_test = np.concatenate(classwise_X_test_list, axis=-1)

        # classwise x test attention
        if X_test_attention is not None:
            X_test_attention = np.concatenate(classwise_X_test_attention_list, axis=-1)

    print('X_train shape : ', X_train.shape)
    print('X_test shape : ', X_test.shape)

    columns = ['Class %d' % (i + 1) for i in range(X_train.shape[1])]
    train_df = pd.DataFrame(X_train,
                            index=range(X_train.shape[0]),
                            columns=columns)

    test_df = pd.DataFrame(X_test,
                           index=range(X_test.shape[0]),
                           columns=columns)

    if plot_data is not None:
        rows = 2
        cols = 2
    else:
        rows = 1
        cols = 2

    fig, axs = plt.subplots(rows, cols, squeeze=False,
                           tight_layout=True, figsize=(8, 6))
    axs[0][0].set_title('Train dataset', size=16)
    axs[0][0].set_xlabel('timestep')
    axs[0][0].set_ylabel('value')
    train_df.plot(subplots=False,
                  legend='best',
                  ax=axs[0][0],)

    axs[0][1].set_title('Test dataset', size=16)
    axs[0][1].set_xlabel('timestep')
    axs[0][1].set_ylabel('value')
    test_df.plot(subplots=False,
                 legend='best',
                 ax=axs[0][1],)

    if plot_data is not None and X_train_attention is not None:
        columns = ['Class %d' % (i + 1) for i in range(X_train_attention.shape[1])]
        train_attention_df = pd.DataFrame(X_train_attention,
                            index=range(X_train_attention.shape[0]),
                            columns=columns)

        axs[1][0].set_title('Train %s Sequence' % (type), size=16)
        axs[1][0].set_xlabel('timestep')
        axs[1][0].set_ylabel('value')
        #axs[1][0].set_ylim([X_train.min(), X_train.max()])
        train_attention_df.plot(subplots=False,
                                legend='best',
                                ax=axs[1][0])

    if plot_data is not None and X_test_attention is not None:
        columns = ['Class %d' % (i + 1) for i in range(X_test_attention.shape[1])]
        test_df = pd.DataFrame(X_test_attention,
                               index=range(X_test_attention.shape[0]),
                               columns=columns)

        axs[1][1].set_title('Test %s Sequence' % (type), size=16)
        axs[1][1].set_xlabel('timestep')
        axs[1][1].set_ylabel('value')
        #axs[1][1].set_ylim([X_test.min(), X_test.max()])
        test_df.plot(subplots=False,
                     legend='best',
                     ax=axs[1][1])

    plt.show()

def cutoff_choice(dataset_id, sequence_length):
    print("Original sequence length was :", sequence_length, "New sequence Length will be : ",
          MAX_SEQUENCE_LENGTH_LIST[dataset_id])
    choice = input('Options : \n'
                   '`pre` - cut the sequence from the beginning\n'
                   '`post`- cut the sequence from the end\n'
                   '`anything else` - stop execution\n'
                   'To automate choice: add flag `cutoff` = choice as above\n'
                   'Choice = ')

    choice = str(choice).lower()
    return choice

def cutoff_sequence(X_train, X_test, choice, dataset_id, sequence_length):
    assert MAX_SEQUENCE_LENGTH_LIST[dataset_id] < sequence_length, "If sequence is to be cut, max sequence" \
                                                                   "length must be less than original sequence length."
    cutoff = sequence_length - MAX_SEQUENCE_LENGTH_LIST[dataset_id]
    if choice == 'pre':
        if X_train is not None:
            X_train = X_train[:, :, cutoff:]
        if X_test is not None:
            X_test = X_test[:, :, cutoff:]
    else:
        if X_train is not None:
            X_train = X_train[:, :, :-cutoff]
        if X_test is not None:
            X_test = X_test[:, :, :-cutoff]
    print("New sequence length :", MAX_SEQUENCE_LENGTH_LIST[dataset_id])
    return X_train, X_test

if __name__ == "__main__":
    # word_list = []
    # seq_len_list = []
    # classes = []
    #
    # for index in range(6, 9):
    #     x, y, x_test, y_test, is_timeseries = load_dataset_at(index)
    #     nb_words, seq_len = calculate_dataset_metrics(x)
    #     print("-" * 80)
    #     print("Dataset : ", index + 1)
    #     print("Train :: X shape : ", x.shape, "Y shape : ", y.shape, "Nb classes : ", len(np.unique(y)))
    #     print("Test :: X shape : ", x_test.shape, "Y shape : ", y_test.shape, "Nb classes : ", len(np.unique(y)))
    #     print("Classes : ", np.unique(y))
    #     print()
    #
    #     word_list.append(nb_words)
    #     seq_len_list.append(seq_len)
    #     classes.append(len(np.unique(y)))
    #
    # print("Word List : ", word_list)
    # print("Sequence length list : ", seq_len_list)
    # print("Max number of classes : ", classes)

    #print()
    plot_dataset(dataset_id=39, seed=1, limit=1, cutoff=None, normalize_timeseries=False,
                 plot_classwise=True)


import asyncio
import async_timeout

async def minisleep(timeout=0.00001, name=None):
    """
    Sometimes very much useful for switching coroutine
    """
    await asyncio.sleep(timeout)

async def get_from_queue(output_queue, timeout=0.1):
    """
    Gets everything from internal queue and puts it in list, stops after queue being empty for some time (timeout=0.1)

    :param output_queue:
    :param timeout:
    :return: list of objects from a queue
    """
    result = []

    data = await output_queue.get()
    result.append(data)
    while True:
        try:
            with async_timeout.timeout(timeout):
                data = await output_queue.get()
                result.append(data)
        except:
            break
    return result

async def wait_completed(coroutine_or_future,  wait_timeout: float):
    try:
        with async_timeout.timeout(wait_timeout):
            return await coroutine_or_future
    except:
        raise

def stop_loop(loop):
    loop.call_soon_threadsafe(loop.stop)


# https://www.acmicpc.net/problem/10769

input = input().replace(":-)", " *HAPPY* ").replace(":-(", " *SAD* ")
str_list = input.split()
str_list = list(filter(lambda x : x == "*HAPPY*" or x == "*SAD*", str_list))
answers = {'happy' : 0, 'sad' : 0}

if len(str_list) == 0 :
    print("none")
else :
    for s in str_list :
        if s == "*HAPPY*" :
            answers["happy"] += 1
        if s == "*SAD*" :
            answers["sad"] += 1

    if answers["happy"] == answers["sad"] :
        print("unsure")
    elif answers["happy"] > answers["sad"] :
        print("happy")
    else :
        print("sad")


import numpy as np
import pandas as pd
import util
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from joblib import dump, load
import os
from sklearn.metrics import mean_absolute_error, make_scorer

###################################################################
#                         Utilities
###################################################################

def MAE(y_true, y_pred):
    return mean_absolute_error(y_true, y_pred)

def drop_cancel(X, is_canceled:pd.Series):
    '''
        Drop is_cancel by boolean index.
    '''
    drop_is_canceled = pd.Series([i != 1 for i in is_canceled])
    return X[drop_is_canceled]

def get_daily_revenue(adr:pd.Series, stay_nights:pd.Series, days:pd.Series):
    '''
        Calculate daily revenue.
    '''
    print('Calculating daily revenue...', end='')
    request_revenue = adr * stay_nights
    daily_revenue = 0
    daily_revenue_list = []
    for idx in range(len(days)):
        if idx > 0 and not days.iloc[idx] == days.iloc[idx-1]:
            daily_revenue_list.append(daily_revenue)
            daily_revenue = 0
        else:
            daily_revenue += request_revenue.iloc[idx]
    daily_revenue_list.append(daily_revenue)
    daily_revenue_list = np.array(daily_revenue_list)

    print(' Finished')
    return daily_revenue_list[:, np.newaxis]

###################################################################
#                           Predict
###################################################################

def predict(X, clf, clf2, clf3):
    print('Predicting...')
    is_canceled = clf.predict(X)
    print('is_canceled predicting finished')
    adr = clf2.predict(X)
    print('adr predicting finished')

    adr = drop_cancel(adr, is_canceled)
    stay_nights = test_x['stays_in_weekend_nights'] + test_x['stays_in_week_nights']
    stay_nights = drop_cancel(stay_nights, is_canceled)
    days = test_x['arrival_date_day_of_month']
    days = drop_cancel(days, is_canceled)
    daily_revenue_list = get_daily_revenue(adr, stay_nights, days)

    scale = clf3.predict(daily_revenue_list)
    print('scale predicting finished')

    # Output result
    output_path = 'result.csv'
    result = pd.DataFrame({'label': scale})
    result = pd.concat([test_y, result], axis=1)
    result.to_csv(output_path, index=False)
    print('Predict finished. Result Saved as', output_path)

def eval(X, y, clf):
    y_pred = clf.predict(X)
    return MAE(y, y_pred)

###################################################################
#                            Train
###################################################################

def train(X, y, model, n_estimators_list:list, task:str):
    '''
        Train task by model.
    '''
    if os.path.isfile('Joblib/{}.joblib'.format(task)):
        print('Model {}.joblib detected, loading...'.format(task), end='')
        clf = load('Joblib/{}.joblib'.format(task))
        print(' Success')
    else:
        min_val_score = float('inf')
        for n_estimators in n_estimators_list:
            clf = make_pipeline(MinMaxScaler(), model(n_estimators, n_jobs=-1))
            score = make_scorer(mean_absolute_error)
            val_score = cross_val_score(clf, X, y, cv=10, scoring=score).mean()

            if val_score < min_val_score:
                min_val_score = val_score
                best_n_estimators = n_estimators
        print('The n_estimators be chose:', best_n_estimators)
        print('The minimum validation score:', min_val_score)
        clf = make_pipeline(MinMaxScaler(), model(best_n_estimators, n_jobs=-1))
        clf.fit(X, y)
        print('{} training finished...'.format(task))
        dump(clf, 'Joblib/{}.joblib'.format(task))
        print('Model Saved as Joblib/{}.joblib'.format(task))

    y_pred = clf.predict(X)
    print('{} MAE: {}'.format(task, MAE(y, y_pred)))

    print('--------------------------------------------')
    return clf

def train_main(X:pd.DataFrame, is_canceled:pd.Series, adr:pd.Series,
                train_y:pd.Series):
    '''
        Main function for training.
    '''
    # dir for saving model
    if not os.path.isdir('Joblib'):
        os.mkdir('Joblib')

    n_estimators_list = [1500]
    clf = train(X, is_canceled, RandomForestClassifier, n_estimators_list, 'is_canceled')
    n_estimators_list = [550]
    clf2 = train(X, adr, RandomForestRegressor, n_estimators_list, 'adr')

    adr = drop_cancel(adr, is_canceled)
    stay_nights = train_x['stays_in_weekend_nights'] + train_x['stays_in_week_nights']
    stay_nights = drop_cancel(stay_nights, is_canceled)
    days = train_x['arrival_date_day_of_month']
    days = drop_cancel(days, is_canceled)
    daily_revenue_list = get_daily_revenue(adr, stay_nights, days)

    n_estimators_list = [i for i in range(20)]
    clf3 = train(daily_revenue_list, train_y, RandomForestClassifier, n_estimators_list, 'scale')

    return clf, clf2, clf3

###################################################################
#                            Main
###################################################################

drop_features_train = ['ID', 'is_canceled', 'arrival_date_week_number', 'adr'
    , 'reservation_status', 'reservation_status_date', 'concat_date']
drop_features_test = ['ID', 'arrival_date_week_number', 'concat_date']

# Read data
train_x, train_y, test_x, test_y = util.get_data()

if __name__ == '__main__':
    # Split data
    is_canceled = train_x['is_canceled']
    adr = train_x['adr']
    X_train = train_x.drop(drop_features_train, axis=1)
    X_test = test_x.drop(drop_features_test, axis=1)
    print('Input data shape:', X_train.shape)

    clf, clf2, clf3 = train_main(X_train, is_canceled, adr, train_y)
    predict(X_test, clf, clf2, clf3)


import numpy as np
from numpy import matlib as mb # matlib must be imported separately

def compute_spatial_similarity(conv1,conv2):
    """
    Takes in the last convolutional layer from two images, computes the pooled output
    feature, and then generates the spatial similarity map for both images.
    conv1 [H*W, Channel], conv2 [H*W, Channel]

    Paper:
        Stylianou, Abby, Richard Souvenir, and Robert Pless.
        "Visualizing deep similarity networks."
        2019 IEEE Winter Conference on Applications of Computer Vision (WACV). IEEE, 2019.

    """
    pool1 = np.mean(conv1,axis=0) # axis=0 represents rows (Channel, )
    pool2 = np.mean(conv2,axis=0) # (Channel, )

    out_sz = (int(np.sqrt(conv1.shape[0])),int(np.sqrt(conv1.shape[0])))  # (H, H)
    # Equ 3 in paper

    conv1_normed = conv1 / np.linalg.norm(pool1) / conv1.shape[0]  # (H^2, C) Normalize - np.linalg.norm -> Frobenius norm
    conv2_normed = conv2 / np.linalg.norm(pool2) / conv2.shape[0]  # (H^2, C)

    im_similarity = np.zeros((conv1_normed.shape[0],conv1_normed.shape[0]))  # (H^2, H^2)

    for zz in range(conv1_normed.shape[0]):   # loop for each pixel
        repPx = mb.repmat(conv1_normed[zz,:],conv1_normed.shape[0],1) # (H^2, c) - Matlib repmat - Repeat a matrix MXN times
        im_similarity[zz,:] = np.multiply(repPx,conv2_normed).sum(axis=1)

    similarity1 = np.reshape(np.sum(im_similarity,axis=1),out_sz)
    similarity2 = np.reshape(np.sum(im_similarity,axis=0),out_sz)

    return similarity1, similarity2


import random

def run():
    numero_aleatorio = random.randint(1,100)
    numero = int(input('\nEscribe un numero del 1 al 100: '))
    while numero_aleatorio != numero:
          if numero < numero_aleatorio:
              print('\n\tBusca un numero más grande')
          else:
              print('\n\tBusca un numero más pequeño')

          numero = int(input('\t::::Elige otro número: '))

    print('\n\n\t\t*****:::::You WIN..!! =):::::*****')

if __name__ == "__main__":
    run()


import os

import click
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

def listdir_nohidden(path):
    """
     List only non hidden files (useful in case when we want to ignore files/folders like __pychache__, .gitignore etc.)
    :param path: Path to files that we want to show.
    :return: List of files path with full directory.
    """
    for f in os.listdir(path):
        if not (f.startswith('.') or f.startswith('_')):
            f_out = os.path.join(path, f)
            yield f_out

@click.command()
@click.option('--clients_secrets_path', default='client_secrets.json',
              help='Path to the file with Google Drive secrets.')
@click.option('--files_folder', default='aging',
              help='Path to folder containing files that are going to be uploaded.')
@click.option('--creds_file', default='mycreds.txt',
              help='Name of the file containing credentials needed to authorize the app.')
@click.option('--notebook_training', default='Aging.ipynb',
              help='Name of the notebook with model training.')
def upload(clients_secrets_path, files_folder, creds_file, notebook_training):
    # Login to Google Drive and create drive object
    try:
        GoogleAuth.DEFAULT_SETTINGS["client_config_file"] = str(clients_secrets_path)
    except FileNotFoundError as e:
        print("Please provide correct name of secrets file.")
        raise e
    gauth = GoogleAuth()
    # Try to load saved client credentials
    gauth.LoadCredentialsFile(creds_file)
    if gauth.credentials is None:
        # Authenticate if they're not there
        # This is what solved the issues:
        gauth.GetFlow()
        gauth.flow.params.update({'access_type': 'offline'})
        gauth.flow.params.update({'approval_prompt': 'force'})
        gauth.LocalWebserverAuth()
    elif gauth.access_token_expired:
        # Refresh them if expired
        gauth.Refresh()
    else:
        # Initialize the saved creds
        gauth.Authorize()

    # Save the current credentials to a file
    gauth.SaveCredentialsFile(creds_file)
    drive = GoogleDrive(gauth)
    # Create folder on Google Drive to store files from given directory
    folder_metadata = {'title': files_folder, 'mimeType': 'application/vnd.google-apps.folder'}
    folder = drive.CreateFile(folder_metadata)
    folder.Upload()
    folderid = folder['id']
    for file in listdir_nohidden(files_folder):
        with open(file, "r") as f:
            fn = os.path.basename(f.name)
            file_drive = drive.CreateFile({'title': fn, "parents": [{"kind": "drive#fileLink", "id": folderid}]})
            file_drive.SetContentString(f.read())
            file_drive.Upload()
        print(f"The file: {fn} has been uploaded")
    file_train = drive.CreateFile({'title': notebook_training})
    file_train.SetContentFile(notebook_training)
    file_train.Upload()
    print(f"The file: {notebook_training} has been uploaded")

if __name__ == '__main__':
    upload()


from typing import List, Optional

from sfml import sf

from ..data.game_obj import GameObject
from ..data import const
from ..data.rect import Rect
from .. import ui

from . import camera
from . import scenes
from . import debug
from . import transitions
from . import window

class App:
    def __init__(self, title: str = "NasNas game", w_width: int = 960, w_height: int = 540,
                 v_width: float = 960, v_height: float = 540, fps: Optional[int] = 60):
        """
        Initializes the engine and creates:
            - a window
            - a main scene
            - a camera named game_camera
        Args:
            title (str): title of the game window
            w_width (int): window width
            w_height (int): window height
            v_width (int): game view width
            v_height (int): game view height
            fps (int): desired fps
        """
        GameObject.game = self

        self.W_WIDTH, self.W_HEIGHT = w_width, w_height
        self.V_WIDTH, self.V_HEIGHT = v_width, v_height

        # Game name, displayed in window title
        self.name = title

        self.desired_fps = fps
        self._window = window.RenderWindow(sf.VideoMode(w_width, w_height), self.name)

        if self.desired_fps:
            self._window.framerate_limit = self.desired_fps

        self._level = None

        self._fullscreen = False

        # inputs from keyboard are stored in this list
        self._inputs: List[int] = []
        # list of all cameras/view used in the game
        self._cameras: List[camera.Camera] = []
        # list of all scenes used in the game, usually 1 is enough
        self._scenes: List[scenes.Scene] = []
        # list of all playing transitions
        self._transitions: List[transitions.Transition] = []
        # list of all menus
        self._menus: List[ui.Menu] = []

        # Scene is where everything is drawn on
        self.scene = self.create_scene(w_width, w_height)

        # game camera, usually looks at the main scene (world map, scrolling level ...)
        self.game_camera = self.create_camera("game", 0, Rect((0, 0), (v_width, v_height)))

        # clock and dt used for FPS calculation
        self.clock = sf.Clock()
        self.dt = 1.0

        self.debug = False
        self.debug_texts = []  # list of DebugText

        self.scale_view()

    @property
    def window(self) -> window.RenderWindow:
        return self._window

    @property
    def level(self):
        return self._level

    @level.setter
    def level(self, value):
        self._level = value

    @property
    def fullscreen(self) -> bool:
        return self._fullscreen

    @property
    def inputs(self) -> List[int]:
        return self._inputs

    @property
    def cameras(self) -> List[camera.Camera]:
        return self._cameras

    @property
    def scenes(self) -> List[scenes.Scene]:
        return self._scenes

    @property
    def transitions(self) -> List[transitions.Transition]:
        return self._transitions

    @property
    def menus(self) -> List[ui.Menu]:
        return self._menus

    def create_scene(self, width: int, height: int):
        """
        Creates a new Scene and returns it.

        Args:
            width (int): width of the new scene
            height (int): height of the new scene

        Returns:
            Returns the created Scene.
        """
        s = scenes.Scene(width, height)
        self.scenes.append(s)
        return s

    def create_camera(self, name: str, order: int, camwindow: Rect, viewport: Rect = Rect((0, 0), (1, 1))) -> camera.Camera:
        """
        Creates a new Camera and returns it

        Args:
            name (str):
                The name of the new Camera, should be unique.
            order (int):
                The order of display. 0 will be displayed first, 1 will be displayed over 0, etc
            camwindow (Rect):
                The window the Camera is looking at.
                For exemple, a Camera with a window=sf.Rect((0, 10), (20, 20)) will look at a square 20x20 located at (0, 10)
            viewport (Rect):
                The place and surface the Camera will take on the window.
                If we take the previous window, and a viewport of sf.Rect((0.5, 0), (0.2, 0.2)), the window looked at will
                be displayed on a surface equal to 20% x 20% of the window located at (50%, 0) of the window size.

        Returns:
            Returns the created Camera.
        """
        cam = camera.Camera(name, order)
        cam.reset(camwindow.topleft, camwindow.size)
        cam.reset_viewport(viewport.topleft, viewport.size)
        self.cameras.append(cam)
        return cam

    def add_debug_text(self, instance: object, attr_name: str, position: tuple, float_round: int = None):
        """
        Adds a DebugText to self.debug_texts. The text will be drawn at the given position

        Args:
            instance (object):
                An instance of any object.
            attr_name (str):
                Attribute name of the instance.
            position (tuple):
                The position where the text will be drawn
            float_round (int):
                If the attribute value is a float, rounds that float using round(instance.attr_name, float_round)
        """
        if float_round:
            self.debug_texts.append(debug.DebugText(instance, attr_name, position, float_round))
        else:
            self.debug_texts.append(debug.DebugText(instance, attr_name, position))

    def toggle_fullscreen(self):
        if not self.fullscreen:
            self.window.close()
            self._window = window.RenderWindow(sf.VideoMode(const.SCREEN_W, const.SCREEN_H), self.name, sf.Style.NONE)
            self.window.framerate_limit = self.desired_fps
            self.window.mouse_cursor_visible = False
            self.window.vertical_synchronization = True
            self._fullscreen = True
            self._inputs = []
            self.scale_view()
        else:
            self.window.close()
            self._window = window.RenderWindow(sf.VideoMode(self.W_WIDTH, self.W_HEIGHT), self.name, sf.Style.DEFAULT)
            self.window.framerate_limit = self.desired_fps
            self.window.mouse_cursor_visible = True
            self.window.vertical_synchronization = True
            self._fullscreen = False
            self._inputs = []
            self.scale_view()

    def scale_view(self):
        """
        Scales all Cameras to keep a good aspect ratio and avoid deformation when resizing the window.
        """
        # wider than usual window
        if float(self.window.size.x) / float(self.window.size.y) > self.W_WIDTH / self.W_HEIGHT:
            viewport_w = (float(self.window.size.y) * (self.W_WIDTH / self.W_HEIGHT)) / float(self.window.size.x)
            viewport_h = 1
            viewport_x = (1-viewport_w)/2
            viewport_y = 0
        # higher than usual window
        else:
            viewport_w = 1
            viewport_h = (float(self.window.size.x) / (self.W_WIDTH / self.W_HEIGHT)) / float(self.window.size.y)
            viewport_x = 0
            viewport_y = (1-viewport_h)/2

        def new_viewport(x):
            return sf.Rect(
                (viewport_x + x.vp_base_pos.x * (1 - 2 * viewport_x), viewport_y + x.vp_base_pos.y * (1 - 2 * viewport_y)),
                (viewport_w * x.vp_base_size.x, viewport_h * x.vp_base_size.y)
            )
        for cam in self.cameras:
            cam.viewport = new_viewport(cam)
        self.window.ui_view.viewport = new_viewport(self.window.ui_view)

    def _store_inputs(self, event: sf.Event):
        """
        Handles window close event.
        Calls self.scale_view() when the window is resized
        Update inputs list.

        Args:
            event (sf.Event): a window event
        """
        if event == sf.Event.KEY_PRESSED:
            if event['code'] not in self.inputs:
                self.inputs.insert(0, event['code'])
        elif event == sf.Event.KEY_RELEASED:
            if event['code'] in self.inputs:
                self.inputs.remove(event['code'])

    def event_handler(self, event: sf.Event):
        """
        event_handler is called every time there is an event happening (key pressed, mouse move, lost focus ...)
        Override this method to make your own event handling

        Args:
            event (sf.Event): a SFML event
        """
        pass

    def update(self):
        """
        Write here your game logic
        """

    def _render(self):
        """
        Draw all Scenes and Layers in order.
        Layer 0 will be drawn first, layer 1 will be drawn over layer 0 etc
        Internal usage only, do not override or call this method.
        """
        for scene in self.scenes:
            scene.render()
        for cam in self.cameras:
            cam.update()
        self.cameras.sort(key=lambda x: x.render_order)
        for cam in self.cameras:
            if cam.visible:
                self.window.view = cam
                if cam.has_scene():
                    self.window.draw(cam.scene)

        # drawing transitions on top of everything else directly on the window
        self.window.view = self.window.ui_view
        for menu in self.menus:
            menu.update()
            self.window.draw(menu)
        for transition in self.transitions:
            transition.update()
            self.window.draw(transition)

    def run(self):
        """
        Starts the game loop.
        """
        while self.window.is_open:
            self.dt = self.clock.restart().seconds
            self.window.title = self.name+" - FPS:" + str(round(1 / self.dt))

            for event in self.window.events:
                if not self._menus:
                    self._store_inputs(event)
                    self.event_handler(event)
                else:
                    self._inputs = []
                    self._menus[-1].event_handler(event)

            self.update()

            self.window.clear(sf.Color.BLACK)

            self._render()

            self.window.view = self.window.default_view
            if self.debug:
                for txt in self.debug_texts:
                    txt.update()
                    self.window.draw(txt)

            self.window.display()


#vær program
import os
def main():
    run = True
    day = 0
    days = ["Mandag", "Tirstag", "Onsdag", "Torstag", "Fredag", "Lørdag", "Søndag"]
    path = os.getcwd()
    filename = "temp.txt"
    dir = os.listdir(path)
    if not filename in dir:
        f = open(filename, "w")
        f.write("Weather file \n")
        f.close()
    def data():
        f = open(filename, "r")
        content = f.read()
        f.close()
        print (content)
    print("\n What do you want to do? print earlier content or write in more data ")
    command = input("p/w :")
    if command == "p":
        data()
    elif command == "w":
        week = input("\n Week:")
        f = open(filename, "r")
        content = f.read()
        f.close()
        f = open(filename, "w")
        f.write(content + "\n"+ "Week: " + week +"\n")
        f.close()
        while run:
            print("\n " ,days[day] )
            temp = input("\n Tempratur:")
            vind = input("\n Wind:")
            regn = input("\n Rain:")
            ok = input("\n All ok:")
            if temp != "No" and vind != "No" and regn != "No" and ok != "No":
                f = open(filename, "r")
                content = f.read()
                f.close()
                f = open(filename, "w")
                f.write(content)
                temp = "temperature: " + temp + "° celsius"
                vind = "wind speed: " + vind + " m/s"
                regn = "rain amount: " + regn + " mm"
                data = days[day] + "\n" + temp + "\n"+ vind  + "\n"+ regn + "\n"
                f.write(data)
                f.close()
                if day == 6:
                    day = 0
                    run = False
                else:
                    day += 1
    else:
        print("not a valid response")
if __name__ == "__main__":
    main()


from matrix import *

def encode(d, m, word):
    ans = []
    null = ""
    for p in range(len(word) // d):  # 分组，一组dim个
        for i in range(d):  # 对固定的某一组而言
            tmp = 0
            for j in range(d):
                tmp += m[j][i] * (ord(word[p * d + j]) - ord('a'))
            tmp %= 26
            ans.append(chr(tmp + ord('a')))
    return null.join(ans)

def decode(d, m, word):
    m_inv = inv_mod26(m)
    ans = []
    null = ""
    for p in range(len(word) // d):  # 分组，一组dim个
        for i in range(d):  # 对固定的某一组而言
            tmp = 0
            for j in range(d):
                tmp += m_inv[j][i] * (ord(word[p * d + j]) - ord('a'))
            tmp %= 26
            ans.append(chr(tmp + ord('a')))
    return null.join(ans)

if __name__ == '__main__':
    dim = int(input())
    matrix = [[0 for i in range(dim)] for j in range(dim)]
    for i in range(dim):
        matrix[i] = [int(j) for j in input().split()]
    string = input().strip().replace('\n', '').replace('\r', '')
    mode = int(input())

    if mode == 1:
        result = encode(dim, matrix, string)
    else:
        result = decode(dim, matrix, string)

    print(result)


import multiprocessing
import os
from sys import stdout

from configs import base, sysconf

if not os.path.exists(sysconf.LOG_DIR):
    os.makedirs(sysconf.LOG_DIR)

bind = sysconf.BIND

# 启动的进程数
workers = sysconf.WORK_NUMS or multiprocessing.cpu_count()

x_forwarded_for_header = 'X-FORWARDED-FOR'

loglevel = sysconf.LOG_LEVEL

# timeout
timeout = 30

file_name = os.path.join(sysconf.LOG_DIR, "log.log")
simple_format = '[%(asctime)s: %(levelname)s/%(processName)s (%(filename)s:%(funcName)s:%(lineno)d)] %(message)s'

# 这里的logging会直接复写掉app的logger.
logconfig_dict = {
    'version': 1,
    'disable_existing_loggers': True,
    'root': {},
    'formatters': {
        'default': {
            'format': simple_format
        },
        'simple': {
            'format': simple_format
        }
    },
    'loggers': {
        "gunicorn.error": {
            "handlers": ["default_outfile", "stream"],  # 对应下面的键
            "qualname": "gunicorn.error",
            "level": loglevel,
        },
        "gunicorn.access": {
            "handlers": ["default_outfile", "stream"],
            "qualname": "gunicorn.access",
            "level": loglevel
        },
        base.PROJECT_NAME: {
            "handlers": ["default_outfile", "stream"],
            "level": loglevel
        }
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple',
        },
        'default_outfile': {
            'level': 'INFO',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'formatter': 'default',
            'filename': file_name,
            'when': 'midnight',
            'backupCount': 10,
            'encoding': 'utf-8',
        },
        "stream": {
            "class": "logging.StreamHandler",
            "stream": stdout
        }
    }
}

# 如果部署的时候开启了debug模式, 可以启用auto_reload
reload = sysconf.AUTO_RELOAD


from flask import Flask
from flask_apscheduler import APScheduler
import py_eureka_client.eureka_client as eureka_client
import os, traceback, threading, logging
from config import config_dict

class Config(object):
    SCHEDULER_API_ENABLED = True

logging.basicConfig(level=logging.INFO)
scheduler = APScheduler()
app = Flask(__name__)

env = os.getenv('CAPITAL_BREATH_A_ENV')
app.config.from_object(config_dict[env])
# app.config.from_object(config_dict['dev'])
app.logger.info("当前环境: " + env)

@app.route('/')
def hello_world():
    return 'Capital Breath A Service [started]'

@scheduler.task('cron', id='stock_daily', day='*', hour='10', minute='0', second='0')
def daily_job():
    app.logger.info('daily job [started]')

if env != 'product_china':
    eureka_client.init(eureka_server=app.config.get('EUREKA_SERVER'),
                       app_name=app.config.get('APP_NAME'),
                       instance_host=app.config.get('SERVER_HOST'),
                       instance_port=app.config.get('SERVER_PORT'),
                       ha_strategy=eureka_client.HA_STRATEGY_RANDOM)

if __name__ == '__main__':
    app.config.from_object(Config())
    scheduler.init_app(app)
    scheduler.start()
    app.run(debug=app.config.get('DEBUG'),
            threaded=app.config.get('THREADED'),
            port=app.config.get('SERVER_PORT'),
            host=app.config.get('HOST'))


def sumOfSquares(num):
    result = 0
    for i in range(1,num+1):
        result += i**2
    return result

def squareOfSums(num):
    result = 0
    for i in range(1,num+1):
        result += i
    return result**2

num = 100
difference = squareOfSums(num) - sumOfSquares(num)
print(difference)


from flask import Flask
import slai
import pandas
import os
import json
from flask import request

app = Flask(__name__)

slai.login(
    client_id=os.environ["SLAI_CLIENT_ID"],
    client_secret=os.environ["SLAI_CLIENT_SECRET"]
)

model = slai.model(os.environ["SLAI_PROJECT_URL"])

@app.route("/")
def root():
    return app.send_static_file('index.html')

@app.route("/favicon.ico")
def favicon():
    return app.send_static_file('favicon.ico')

@app.route("/slai", methods = ["POST"])
def slai_proxy():
    data = {key: [request.json[key]] for key in request.json}
    df = pandas.DataFrame(data=data)
    predictions = model(x1=df)['pred']
    return json.dumps(predictions.tolist())

# For developer sanity. So changes to calculator.js do not require a hard refresh
@app.after_request
def add_header(r):
    """
    Add headers to both force latest IE rendering engine or Chrome Frame,
    and also to cache the rendered page for 10 minutes.
    """
    r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
    r.headers["Pragma"] = "no-cache"
    r.headers["Expires"] = "0"
    r.headers['Cache-Control'] = 'public, max-age=0'
    return r


import cv2
import time
from argparse import ArgumentParser
from input_feeder import InputFeeder
from face_detection import Model_Face_Detection
from facial_landmark_detection import Model_Facial_Landmark_Detection
from gaze_estimation import Model_Gaze_Estimation
from head_pose_estimation import Model_Head_Pose_estimation
from mouse_controller import MouseController
import logging as log
import numpy as np

class Application:

    def __init__(self):
        self.args = None
        self.feed = None
        self.face_detection_model = None
        self.facial_landmark_detection_model = None
        self.gaze_estimation_model = None
        self.head_pose_estimation_model = None
        self.frame = None
        self.width = None
        self.Height = None
        self.mc = MouseController("high", "fast")
        self.face_detection_load_time = 0
        self.facial_landmark_detection_load_time = 0
        self.gaze_estimation_load_time = 0
        self.head_pose_estimation_load_time = 0
        self.face_detection_infer_time = 0
        self.facial_landmark_detection_infer_time = 0
        self.gaze_estimation_infer_time = 0
        self.head_pose_estimation_infer_time = 0
        self.frames = 0

    def initialize_argparser(self):
        """
        Parse command line arguments.

        :return: command line arguments
        """
        parser = ArgumentParser()
        parser.add_argument("-t", "--input-type", required=True, type=str,
                            help="Type of input (video or cam)")
        parser.add_argument("-i", "--input", required=True, type=str,
                            help="Input file")
        parser.add_argument("-o", "--out", type=str, default=None,
                            help="Output file with the processed content")
        parser.add_argument("-p", "--preview", action='store_true', default=False,
                            help="Should preview face and eyes")
        parser.add_argument("--notmove", action='store_true', default=False,
                            help="Should not move mouse")
        parser.add_argument("-m", "--model", type=str, default="FP32",
                            help="Model precision to use. One of FP32, FP16 or FP16-INT8")
        parser.add_argument("-d", "--device", type=str, default="CPU",
                            help="Device used to process model. One or CPU or GPU")
        parser.add_argument("-v", "--verbose", action='store_true', default=False,
                            help="Enable DEBUG messages")

        self.args = parser.parse_args()

    def initialize_logging(self):
        if self.args.verbose:
            log.basicConfig(level=log.DEBUG)
        else:
            log.basicConfig(level=log.ERROR)

    def initialize_feed(self):
        self.feed = InputFeeder(self.args.input_type, self.args.input)
        self.feed.load_data()

    def initialize_window(self):
        if self.args.preview:
            cv2.namedWindow('preview')
            cv2.namedWindow('face')
            cv2.namedWindow('left eye')
            cv2.namedWindow('right eye')
            cv2.namedWindow('gaze')

    def show_main_frame(self):
        cv2.imshow('preview', self.frame)

    def esc_key_pressed(self):
        key_pressed = cv2.waitKey(1)
        if key_pressed == 27:
            return True

    def infer_face(self):
        start = time.time()
        face_frame = self.face_detection_model.predict(self.frame)
        self.face_detection_infer_time += time.time() - start
        return face_frame

    def infer_eyes(self, face_frame, show=False):
        start = time.time()
        left_eye_pos, right_eye_pos, left_eye, right_eye = self.facial_landmark_detection_model.predict(face_frame)
        self.facial_landmark_detection_infer_time += time.time() - start

        if show:
            tmp_face = face_frame.copy()
            cv2.circle(tmp_face, (left_eye_pos[0], left_eye_pos[1]), 5, (0, 255, 0))
            cv2.circle(tmp_face, (right_eye_pos[0], right_eye_pos[1]), 5, (0, 255, 0))
            cv2.imshow('face', tmp_face)
            cv2.imshow('left eye', left_eye)
            cv2.imshow('right eye', right_eye)

        return left_eye, right_eye

    def infer_pose(self, face_frame, show=False):
        start = time.time()
        yaw, pitch, roll = self.head_pose_estimation_model.predict(face_frame)
        self.head_pose_estimation_infer_time += time.time() - start
        return yaw, pitch, roll

    def infer_gaze(self, cropped_left_eye, cropped_right_eye, yaw, pitch, roll, show=False):
        start = time.time()
        gaze = self.gaze_estimation_model.predict(cropped_left_eye, cropped_right_eye, yaw, pitch, roll)
        self.gaze_estimation_infer_time += time.time() - start
        if show:
            img = np.zeros([100, 100, 3], dtype=np.uint8)
            img.fill(255)
            cv2.circle(img, (50, 50), 50, (0, 255, 0))
            cv2.arrowedLine(img, (50, 50), (50 + int(gaze[0] * 70), 50 + int(-gaze[1] * 70)), (255, 0, 0), 2)
            cv2.imshow('gaze', img)
        return gaze

    def infer_frame(self):
        self.show_main_frame()
        if self.esc_key_pressed():
            return False
        self.frames += 1
        face_frame = self.infer_face()
        if face_frame is not None:
            cropped_left_eye, cropped_right_eye = self.infer_eyes(face_frame, self.args.preview)
            yaw, pitch, roll = self.infer_pose(face_frame, self.args.preview)
            gaze = self.infer_gaze(cropped_left_eye, cropped_right_eye, yaw, pitch, roll, self.args.preview)
            if not self.args.notmove:
                self.mc.move(gaze[0], gaze[1])

    def process_feed(self):
        try:
            for batch in self.feed.next_batch():
                self.frame = batch
                if batch is not None:
                    if self.infer_frame() is False:
                        break
                else:
                    break

            log.info("Face detection model load time: {:.2f}ms".format(
                1000 * self.face_detection_infer_time))
            log.info("Facial landmark detection model load time: {:.2f}ms".format(
                1000 * self.facial_landmark_detection_infer_time))
            log.info("Head Pose estimation model load: {:.2f}ms".format(
                1000 * self.head_pose_estimation_infer_time))
            log.info("Gaze estimation model load time: {:.2f}ms".format(
                1000 * self.gaze_estimation_infer_time))

            log.info("Face detection model inference mean time: {:.2f}ms".format(
                1000 * self.face_detection_infer_time / self.frames))
            log.info("Facial landmark detection model inference mean time: {:.2f}ms".format(
                1000 * self.facial_landmark_detection_infer_time / self.frames))
            log.info("Head Pose estimation model inference mean time: {:.2f}ms".format(
                1000 * self.head_pose_estimation_infer_time / self.frames))
            log.info("Gaze estimation model inference mean time: {:.2f}ms".format(
                1000 * self.gaze_estimation_infer_time / self.frames))

        except Exception as err:
            log.error("Could not infer. Cause: ", str(err))

    def initialize_models(self):
        try:
            model_precision = self.args.model.upper()

            self.face_detection_model = Model_Face_Detection(
                "models/intel/face-detection-adas-binary-0001/FP32-INT1/face-detection-adas-binary-0001")
            start = time.time()
            self.face_detection_model.load_model()
            self.face_detection_load_time = time.time() - start

            self.facial_landmark_detection_model = Model_Facial_Landmark_Detection(
                f"models/intel/landmarks-regression-retail-0009/{model_precision}/landmarks-regression-retail-0009",
                self.args.device.upper())
            start = time.time()
            self.facial_landmark_detection_model.load_model()
            self.facial_landmark_detection_load_time = time.time() - start

            self.head_pose_estimation_model = Model_Head_Pose_estimation(
                f"models/intel/head-pose-estimation-adas-0001/{model_precision}/head-pose-estimation-adas-0001",
                self.args.device.upper())
            start = time.time()
            self.head_pose_estimation_model.load_model()
            self.head_pose_estimation_load_time = time.time() - start

            self.gaze_estimation_model = Model_Gaze_Estimation(
                f"models/intel/gaze-estimation-adas-0002/{model_precision}/gaze-estimation-adas-0002",
                self.args.device.upper())
            start = time.time()
            self.gaze_estimation_model.load_model()
            self.gaze_estimation_load_time = time.time() - start
        except Exception as err:
            log.error("Could not load model. Cause: ", str(err))

    def run(self):
        self.initialize_argparser()
        self.initialize_logging()
        self.initialize_models()
        self.initialize_feed()
        self.initialize_window()
        self.process_feed()
        self.feed.close()

if __name__ == '__main__':
    app = Application()
    app.run()


dict={2:('a','b','c'),
      3:('d','e','f'),
      4:('g','h','i'),
      5:('j','k','l'),
      6:('m','n','o'),
      7:('p','q','r','s'),
      8:('t','u','v'),
      9:('w','x','y','z'),}
def PHONELETTERS(digits):
    digits=digits.replace('1',"")
    digits=digits.replace('0',"")
    if digits=='':
        return []
    ar=[int(i) for i in digits]
    result=[]
    i=1
    temp=dict[ar[0]]
    while i<len(ar):
        for j in dict[ar[i]]:
            for k in range(0,len(temp)):
                result.append(temp[k]+j)
        if result!=[] and i<len(ar)-1:
            temp=result
            result=[]
            i+=1
            continue
        i+=1
    if result==[]:
        return list(temp)
    return result
digits=input()
print(PHONELETTERS(digits))


"""empty message

Revision ID: 2bf03a0daad4
Revises: 65908de93686
Create Date: 2020-04-16 04:08:18.818613

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '2bf03a0daad4'
down_revision = '65908de93686'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('user_profiles', sa.Column('biography', sa.String(length=600), nullable=True))
    op.drop_column('user_profiles', 'bibliography')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('user_profiles', sa.Column('bibliography', sa.VARCHAR(length=600), autoincrement=False, nullable=True))
    op.drop_column('user_profiles', 'biography')
    # ### end Alembic commands ###


import pandas as pd
import plotly.express as px

def createCumSum():
  df = pd.read_csv('data_cumsum.csv')

  fig = px.line(df, x="Datum tenaamstelling", y="cumsum", color='Brandstof', log_y=True, title='Cumulatief aantal tenaamgestelde auto\'s per brandstofsoort per maand')

  fig.update_xaxes(title='Tijd in maanden')
  fig.update_yaxes(title='Totaal tenaamgestelde auto\'s')

  return fig


from __future__ import annotations

from schemas.operations import RecurrentTransferOperation, RecurrentTransferOperationLegacy

def test_recurrent_transfer_extension_hf_26_representation() -> None:
    # ARRANGE
    data = {
        "amount": {"amount": "1000", "precision": 3, "nai": "@@000000013"},
        "executions": 2,
        "extensions": [
            {
                "type": "recurrent_transfer_pair_id",
                "value": {"pair_id": 1},
            }
        ],
        "from": "sender",
        "memo": "{}",
        "recurrence": 48,
        "to": "receiver",
    }

    # ACT & ASSERT
    RecurrentTransferOperation(**data)

def test_recurrent_transfer_extension_legacy_representation() -> None:
    # ARRANGE
    data = {
        "amount": "1.000 HIVE",
        "executions": 2,
        "extensions": [
            [
                "recurrent_transfer_pair_id",
                {"pair_id": 1},
            ]
        ],
        "from": "sender",
        "memo": "{}",
        "recurrence": 48,
        "to": "receiver",
    }

    # ACT & ASSERT
    RecurrentTransferOperationLegacy(**data)


def pali(n):
    s=0
    while n>0:
        r=n%10
        s=s*10+r
        n//=10
    return s
def nextpali(n):
    t=n
    n+=1
    while n!=pali(n):
        n+=1
    return n
def beforpalin(n):
    n-=1
    while n!=pali(n):
        n-=1
    return n
n=int(input())
ne=nextpali(n)
be=beforpalin(n)
if (ne-n)==(n-be):
    print(be,ne)
elif ne-n > n-be :
    print(be)
else:
    print(ne)


'''
full dataset size: 96093
July 1, 2019
'''

from aip_detection import *

def main():
    file_type = ".jpg"

    #for a directory
    folder_path = "error_log_18.txt"

    arglist = sys.argv
    if(len(arglist) > 1):
        folder_path = str(sys.argv[1])

    text_file = open(folder_path, "r")
    lines = text_file.read().split('\n')

    print(len(lines))

    count = 0
    for file_dir in lines:
        #getting the location using baidu.ai #ref: http://ai.baidu.com/
        # json_data = connectwithbaiduai(file_dir)
        try:
            file_name = file_dir.replace(file_type,"")

            saveas_json(file_dir, file_name,1)
            count += 1
            if(count % 50 == 0):
                print(count, file_dir)

        except (TypeError, ValueError, KeyError) as error:
            pass

    print("fin")

main()


import pytest
from src.pages.broken_link.text_broken_link import BrokenLinks

@pytest.mark.usefixtures("set_up_chrome")
class TestBrokenLink:
    @pytest.mark.parametrize('link, link_assert_1, link_assert_2, link_assert_3, link_assert_4',
                        [("https://demoqa.com/broken",
                          "https://demoqa.com/images/Toolsqa.jpg",
                          "https://demoqa.com/images/Toolsqa_1.jpg",
                          "https://demoqa.com/",
                          "http://the-internet.herokuapp.com/status_codes/500")
                         ])
    def test_bl(self, link, link_assert_1, link_assert_2, link_assert_3, link_assert_4):
        """
        Данная функция работает со страничкой https://demoqa.com/broken.
        Для начала, проверяет наличие картинок путем сверки href с заданной и затем делает скриншот.
        Далее нажимает на ссылки и проверяет правильность перехода по нужной ссылке.
        """
        self.driver.get(link)
        self.broken_link = BrokenLinks(self.driver)
        assert self.broken_link.first_link_check() == link_assert_1
        assert self.broken_link.second_link_check() == link_assert_2
        self.broken_link.scroll_to()
        self.broken_link.find_valid_link()
        assert self.broken_link.valid_link_check() == link_assert_3
        self.broken_link.scroll_to()
        assert self.broken_link.broken_link_check() == link_assert_4

if __name__ == "__main__":
    TestBrokenLink().test_bl()


def is_float(element):
    try:
        float(element)
        return True

    except ValueError:
        return False

def format_clusters(clusters):
    final_st = ""

    for key, value in clusters.items():
        final_st += f"C{key + 1}: {value}\n"

    return final_st


import pygame
import time
import sys
import math
from Settings import *
from Scripts.Entities import PhysicsEntity, Player, Enemy
from Scripts.Utils import Load_IMG
from Scripts.Utils import Load_IMGS, Animation, Sprite_sheet_IMGS
from Scripts.Tilemap import Titlemap
from Scripts.Clouds import Clouds
from Scripts.Particles import Particles
from Scripts.Projectiles import En_Pro, Pl_Pro
from Scripts.Health_pack import Health
import Scripts.Boss
import random
import os, os.path

class Game():
    def __init__(self):
        global level
        self.assets = {
            'Decor' : Load_IMGS('tiles/decor'),
            'Grass' : Load_IMGS('tiles/grass'),
            'Stone' : Load_IMGS('tiles/stone'),
            'Large_decor' : Load_IMGS('tiles/large_decor'),
            'Player' : Load_IMG('entities/player.png'),
            'Background' : Load_IMG('background.png'),
            'Clouds' : Load_IMGS('clouds'),
            'Health' : Animation(Load_IMGS('tiles/health'), dur = 6),
            'Player/idle' : Animation(Load_IMGS('entities/player/idle'), dur= 6),
            'Player/run' : Animation(Load_IMGS('entities/player/run'), dur= 4),
            'Player/slide' : Animation(Load_IMGS('entities/player/slide')),
            'Player/jump' : Animation(Load_IMGS('entities/player/jump')),
            'Player/wall_slide' : Animation(Load_IMGS('entities/player/wall_slide')),
            'Particles/leaf' : Animation(Load_IMGS('particles/leaf'), dur= 20, loop = False),
            'Particles/particle' : Animation(Load_IMGS('particles/particle'), dur = 10, loop= False),
            'Particles/blood' : Animation(Load_IMGS('particles/blood'), dur = 10, loop= False),
            'Enemy/idle' : Animation(Load_IMGS('entities/enemy/idle'), dur = 6),
            'Enemy/run' : Animation(Load_IMGS('entities/enemy/run'), dur = 4),
            'Gun' : Load_IMG('gun.png'),
            'Projectile': Load_IMG('projectile.png'),
            'Shuriken' : Load_IMG('shuriken.png'),
            'Boss_1/attack1' : Animation(Sprite_sheet_IMGS('entities/boss1/Attack.png', (59, 51), 81, 50, 150, 8), dur = 10),
            'Boss_1/idle' : Animation(Sprite_sheet_IMGS('entities/boss1/Idle.png', (53,45), 36, 57, 150, 8 ), dur= 8),
            'Boss_1/move' : Animation(Sprite_sheet_IMGS('entities/boss1/Move.png', (50, 35), 49, 66, 150, 8), dur= 8),
            'Boss_1/death' : Animation(Sprite_sheet_IMGS('entities/boss1/Death.png', (53,51), 51, 56, 150, 5), dur= 8, loop= False),
            'Boss_1/hit' : Animation(Sprite_sheet_IMGS('entities/boss1/Take Hit.png', (54, 45), 37, 55, 150, 4), dur= 8),
        }

        self.Particles = []
        self.Health = []

        self.Player = Player((50,50), (8, 15), self.assets, self.Particles)
        self.Tilemap = Titlemap(self.assets)
        self.scroll = [0, 0]
        self.Clouds = Clouds(self.assets['Clouds'])

        dest = 'data/maps/' + str(level) + '.json'
        try:
            self.Tilemap.load(dest)
        except FileNotFoundError:
            pass

        self.Boss = []
        for boss in self.Tilemap.extract([('Boss', 0)]):
            if boss['variant'] == 0:
                self.Boss.append(Scripts.Boss.Boss_1(self, (boss['pos'][0], boss['pos'][1]), 'Boss_1', 'Evil Wizard'))

        self.Leaf_spawner = []
        for tree in self.Tilemap.extract([('Large_decor', 2)], True):
            self.Leaf_spawner.append(pygame.Rect(tree['pos'][0] + 4, tree['pos'][1] + 4, 23, 13))

        self.enemies = []
        self.Projectiles = []

        for spawner in self.Tilemap.extract([('Spawner', 0), ('Spawner', 1)]):
            if spawner['variant'] == 0:
                self.Player.pos = spawner['pos']
            else:
                self.enemies.append(Enemy(spawner['pos'], (8,16), self.assets, self.Particles, self.Player))

        self.End = False

        self.transition = -30

        if len(self.Boss):
            self.target = self.Boss[0]

    def Boss_health_name(self, screen, boss):
        surf = Font.render(boss.name, True, "white")
        screen.blit(surf, (280, 410))
        Health_bar = 600 * boss.health / boss.health_max
        Health_bar_rect = pygame.Rect(20, 440, Health_bar, 25)
        pygame.draw.rect(screen, 'aquamarine3', Health_bar_rect)

    def Run(self):

        if not len(self.enemies) and not len(self.Boss):
            self.transition += 1
        if self.transition < 0:
            self.transition += 1

        Display.fill( (0,0,0,0) )
        Display2.blit(self.assets['Background'], (0,0))
        self.scroll[0] += (self.Player.rect().centerx - Display.get_width()/2 - self.scroll[0]) / 30 # type: ignore
        self.scroll[1] += (self.Player.rect().centery - Display.get_height()/2 - self.scroll[1]) / 20 # type: ignore
        render_scroll = (int(self.scroll[0]), int(self.scroll[1]))

        for rect in self.Leaf_spawner:
            if random.random() * 20000 < rect.width * rect.height:
                pos = (rect.x + random.random()* rect.width, rect.y + random.random() * rect.height)
                self.Particles.append(Particles(self.assets, 'leaf', pos, velocity=[-0.1, 0.3], frame= random.randint(0 , 20)))

        self.Clouds.render(Display, offset= render_scroll)
        self.Clouds.update()

        self.Tilemap.render(Display, offset = render_scroll)

        for boss in self.Boss.copy():
            boss.update()
            boss.render(Display, offset= render_scroll)
            if boss.Dead:
                self.Boss.remove(boss)

        for enemy in self.enemies.copy():
            if enemy.update(self.Tilemap):
                if enemy.flip:
                    self.Projectiles.append(En_Pro(self, 'Projectile', (enemy.rect().centerx - 2, enemy.rect().centery -9 ), Vel= [-2,0] ))
                else:
                    self.Projectiles.append(En_Pro(self, 'Projectile', (enemy.rect().centerx + 2, enemy.rect().centery -9 ), Vel= [2, 0] ))
            if enemy.Dead:
                if random.randint(0, 100) <= 10:
                    self.Health.append(Health(self.assets, (enemy.pos[0], enemy.pos[1] + 10), random.choice([100, 150, 50])))
                self.enemies.remove(enemy)
                for i in range(100):
                    angle = random.random() * math.pi * 2
                    Pvel = [ math.cos(angle) * 1, math.sin(angle) * 1]
                    self.Particles.append(Particles(self.assets, 'blood', enemy.rect().center, velocity= Pvel, frame= random.randint(0, 30)))
            enemy.render(Display, offset = render_scroll)

        for pro in self.Projectiles.copy():
            pro.update(self.Tilemap)
            if pro.kill:
                if pro.count > pro.limit:
                    self.Projectiles.remove(pro)
            else:
                pro.render(Display, offset = render_scroll)

        if self.Player.update(self.Tilemap):
            if self.Player.flip:
                self.Projectiles.append(Pl_Pro(self, 'Shuriken', (self.Player.rect().centerx - 2, self.Player.rect().centery - 9), Vel= [-2,0], flip= self.Player.flip))
            else:
                self.Projectiles.append(Pl_Pro(self, 'Shuriken', (self.Player.rect().centerx + 2, self.Player.rect().centery - 9), Vel= [2, 0], flip= self.Player.flip ))

        if self.Player.Dead:
            self.End = True

        self.Player.render(Display, offset = render_scroll)

        for health in self.Health.copy():
            if self.Player.rect().colliderect(health.rect()):
                Healing_sound.play()
                self.Player.Health += health.Health
                health.kill = True
            if health.kill:
                self.Health.remove(health)
            health.update()
            health.render(Display, offset = render_scroll)

        Display_mask = pygame.mask.from_surface(Display)
        Display_sillouehette = Display_mask.to_surface(setcolor=(0,0,0,180), unsetcolor=(0,0,0,0))
        Display2.blit(Display_sillouehette, (2, 1))
        # for offset in [(1,0), (0,1), (-1, 0), (0, -1)]:
        #     Display2.blit(Display_sillouehette, offset)

        for Particle in self.Particles.copy():
            kill = Particle.update()
            if Particle.type == 'leaf':
                Particle.pos[0] += math.sin(Particle.animation.frame * (math.pi / 360)) * 0.3
            Particle.render(Display, offset = render_scroll)
            if kill:
                self.Particles.remove(Particle)

        if not len(self.enemies) and not len(self.Boss):
            global level
            global max_level
            level = min(level + 1, max_level)
            self.End = True

        if self.transition:
            trans_surf = pygame.Surface(Display.get_size())
            pygame.draw.circle(trans_surf, "white", (Display.get_width() /2, Display.get_height() /2), (30 - abs(self.transition)) * 8 )
            trans_surf.set_colorkey("white")
            Display.blit(trans_surf, (0,0))

if __name__ == '__main__':
    pygame.init()
    Font = pygame.font.Font('data/Pixeltype.ttf', 35)
    screen = pygame.display.set_mode((screen_w, screen_h))
    clock = pygame.time.Clock()

    pygame.mixer.music.load("data/music.wav")
    pygame.mixer.music.set_volume(0.5)

    Ambience = pygame.mixer.Sound('data/sfx/ambience.wav')
    Ambience.set_volume(0.5)
    pygame.mixer.music.play(-1)
    Ambience.play(-1)

    Healing_sound = pygame.mixer.Sound('data/sfx/heal.wav')
    Healing_sound.set_volume(0.5)

    #FPS
    FPS = 60  #FRAMERATE LIMITER
    FPS_Target = 60
    Prev_Time = time.time()
    Dt = 0
    max_level = len(os.listdir('data/maps'))
    level = 0

    Display = pygame.Surface((320, 240), pygame.SRCALPHA)
    Display2 = pygame.Surface((320, 240))
    Display3 = pygame.Surface((320, 240))

    Pause = False

    game = Game()

    while True:
        #CALCULATING DELTA TIME
        clock.tick(FPS)
        now = time.time()
        Dt = (now - Prev_Time) * FPS_Target
        Prev_Time = now
        pygame.display.set_caption( str( round(clock.get_fps(), 1 ) ) )

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    Pause = not Pause

        if game.End:
            game = Game()

        if not Pause:
            game.Run()
            Display2.blit(Display, (0,0))
            screen.blit(pygame.transform.scale(Display2, (screen_w, screen_h)), (0, 0))
            Health_display(game.Player.Health, screen)
            try:
                if game.target:
                    game.Boss_health_name(screen, game.target)
            except:
                pass
        else:
            text = Font.render('PAUSE',  True, 'white')
            screen.blit(text, (screen_w/2 - 10, screen_h/2 - 10))
        pygame.display.flip()


#!/usr/bin/env python

import argparse
import os
import sys
sys.path.insert(1, '.')
from vdetlib.vdet.dataset import imagenet_vdet_classes
from vdetlib.utils.visual import unique_colors, add_bbox
from vdetlib.utils.common import imread, imwrite
from vdetlib.utils.protocol import proto_dump, proto_load, top_detections, frame_path_at, track_box_at_frame
import cv2

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('vid_file')
    parser.add_argument('annot_file')
    parser.add_argument('--save_dir', default=None)
    args = parser.parse_args()

    vid_proto = proto_load(args.vid_file)
    annot_proto = proto_load(args.annot_file)

    colors = unique_colors(len(annot_proto['annotations']))

    for frame in vid_proto['frames']:
        img = imread(frame_path_at(vid_proto, frame['frame']))
        boxes = [track_box_at_frame(tracklet, frame['frame']) \
                for tracklet in [annot['track'] for annot in annot_proto['annotations']]]
        tracked = add_bbox(img, boxes, None, 10)
        if args.save_dir:
            if not os.path.isdir(args.save_dir):
                os.makedirs(args.save_dir)
            imwrite(os.path.join(args.save_dir, "{:04d}.jpg".format(frame['frame'])),
                    tracked)
        else:
            cv2.imshow('tracks', tracked)
            if cv2.waitKey(0) == ord('q'):
                cv2.destroyAllWindows()
                sys.exit(0)
            cv2.destroyAllWindows()


import wave
import srt
from pydub import AudioSegment
import matplotlib.pyplot as plt
import wave
import numpy as np

audio_path = "data/raw/MIT5_07SCF13_JoAnne_Intro.wav"
srt_path = "data/raw/MIT5_07SCF13_JoAnne_Intro.srt"
# aligned_path = "data/raw/MIT5_07SCF13_JoAnne_Intro.aligned.srt"
aligned_path = "data/raw/MIT5_07SCF13_JoAnne_Intro.trimmed.srt"

with open(srt_path, 'r') as srt_file:
    srt_str = srt_file.read()
with open(aligned_path, 'r') as srt_file:
    aligned_str = srt_file.read()

srt_data = list(srt.parse(srt_str))
aligned_data = list(srt.parse(aligned_str))

# wfile = AudioSegment.from_wav(audio_path)
# time = wfile.duration_seconds
#
# st = int(sd.start.total_seconds() * 1000)
# end = int(sd.end.total_seconds() * 1000)
# clip = wfile[st:end]

# look for where the text matches
pairs = {}
for item in srt_data:
    pair = next((x for x in aligned_data
        if x.content == item.content), None)
        # if x.content.strip().startswith(item.content.replace('\n',' ').strip())),None)
    if pair is not None:
        pairs[item.content] = (item,pair)

f = wave.open(audio_path, "rb")

# (nchannels, sampwidth, framerate, nframes, comptype, compname)
params = f.getparams()
nchannels, sampwidth, framerate, nframes = params[:4]

str_data = f.readframes(nframes)
f.close()

wave_data = np.fromstring(str_data, dtype=np.short)
#wave_data.shape = -1, 1
wave_data = wave_data.T
# wave_data = wave_data
time = np.arange(0, nframes) * (1.0 / framerate)

fig,ax = plt.subplots(1)
#pl.subplot(211)

for content, (cc, align) in pairs.items():
    st, end = cc.start.total_seconds(), cc.end.total_seconds()
    st_a, end_a = align.start.total_seconds(), align.end.total_seconds()

    # plt.axvline(x=cc.start.total_seconds(), color='b')
    # plt.axvline(x=align.start.total_seconds(), color='r')
    #
    # plt.axvline(x=cc.end.total_seconds(), color='b')
    # plt.axvline(x=align.end.total_seconds(), color='r')
    #
    rect = plt.Rectangle((st_a,0.0), end_a-st_a, 10000.0,
                  color='b', alpha=0.3)
    plt.text(st_a, 11000, content)
    ax.add_patch(rect)
    rect = plt.Rectangle((st,-10000.0), end-st, 10000.0,
                  color='r', alpha=0.4)
    ax.add_patch(rect)
    plt.text(st, -11000, content)

plt.plot(time, wave_data)
plt.xlabel("time (seconds)")
plt.show()


import numpy as np

def read_metabolites(metabolites_path):
    met_dicts = []
    with open(metabolites_path) as f:
        f_lines = f.readlines()
        # num_metabolites = f_lines[0]
        for met_info in f_lines[1:]:
            met_splt = met_info.split('[')
            met_id = met_splt[0][1:]
            compartment = met_splt[1][0]
            met_dicts.append({'full_id': met_info[1:-2], 'met_id': met_id, 'compartment': compartment})
    return met_dicts

def read_reactions(reactions_path):
    rxn_dicts = []
    with open(reactions_path) as f:
        f_lines = f.readlines()
        for rxn_info in f_lines[1:]:
            rxn_str = rxn_info[:-1]
            if rxn_str == '** manually added':
                pass
            elif not rxn_str.isspace():
                rxn_dicts.append({'rxn_id': rxn_str[1:-1], 'type': 0, 'lower_bound': 0, 'upper_bound': 0})
    return rxn_dicts

def read_off_reactions(off_rxns_path):
    off_rxn_ids = []
    with open(off_rxns_path) as f:
        f_lines = f.readlines()
        for rxn_info in f_lines[1:]:
            rxn_str = rxn_info[:-1]
            if rxn_str[0] == '*':
                off_rxn_ids.append(rxn_str[2:-1])
            else:
                off_rxn_ids.append(rxn_str[1:-1])
    return off_rxn_ids

def read_uptake_reactions(uptake_rxns_path):
    uptake_rxn_ids = []
    with open(uptake_rxns_path) as f:
        f_lines = f.readlines()
        for rxn_info in f_lines[1:]:
            rxn_str = rxn_info[:-1]
            uptake_rxn_ids.append(rxn_str[1:-1])
    return uptake_rxn_ids

def read_stoichimetry_matrix(stoichimetry_matrix_path):
    stoic_elements = []
    with open(stoichimetry_matrix_path) as f:
        f_lines = f.readlines()
        for stoch_info in f_lines[1:]:
            if stoch_info[:16] == '* Manually added':
                pass
            elif not stoch_info.isspace():
                stoch_splt = stoch_info.split(' ')
                met_rxn_info = stoch_splt[0].split('.')
                stoch_coeff = float(stoch_splt[1])
                met_id = met_rxn_info[0][1:-1]
                rxn_id = met_rxn_info[1][1:-1]
                stoic_elements.append({'met_full_id': met_id, 'rxn_id': rxn_id, 'coeff': stoch_coeff})
    return stoic_elements

def find_reaction(rxn_id, reaction_dicts):
    for rxn_dict in reaction_dicts:
        if rxn_dict['rxn_id'] == rxn_id:
            return rxn_dict
    print("missed id is: " + rxn_id)
    raise Exception

def update_rxn_types(reaction_dicts, rxn_types_path):
    with open(rxn_types_path) as f:
        f_lines = f.readlines()
        for type_info in f_lines[1:]:
            type_str = type_info[:-1]
            if type_str == '** manually added':
                pass
            elif type_str and not type_str.isspace():
                type_splt = type_str.split(' ')
                rxn_id = type_splt[0][1:-1]
                rxn_dict = find_reaction(rxn_id, reaction_dicts)
                rxn_dict['type'] = int(type_splt[1])
    return reaction_dicts

def modify_bounds(reaction_dicts, updake_reactions_ids, off_reactions_ids):
    for rxn_dict in reaction_dicts:
        rxn_id = rxn_dict['rxn_id']
        rxn_type = rxn_dict['type']
        if rxn_type == 0:
            rxn_dict['lower_bound'] = 0
            rxn_dict['upper_bound'] = 1000
        elif rxn_type == 1:
            rxn_dict['lower_bound'] = -1000
            rxn_dict['upper_bound'] = 1000
        elif rxn_type == 2:
            rxn_dict['lower_bound'] = 0
            rxn_dict['upper_bound'] = 0
        elif rxn_type == 4:
            rxn_dict['lower_bound'] = 0
            rxn_dict['upper_bound'] = 1000

        if rxn_id in updake_reactions_ids:
            rxn_dict['lower_bound'] = -1000
        elif rxn_id == 'Ec_biomass_iAF1260_core_59p81M':
            rxn_dict['lower_bound'] = 0
            rxn_dict['upper_bound'] = 0
        elif rxn_id in off_reactions_ids:
            rxn_dict['lower_bound'] = 0
            rxn_dict['upper_bound'] = 0
        elif rxn_id == 'ATPM':
            rxn_dict['lower_bound'] = 8.39
            rxn_dict['upper_bound'] = 8.39
        elif rxn_id == 'EX_glc(e)':
            rxn_dict['lower_bound'] = -10;
        elif rxn_id == 'EX_o2(e)':
            rxn_dict['lower_bound'] = -20;
        elif rxn_id == 'LDH_D_f':
            rxn_dict['lower_bound'] = 0
            rxn_dict['upper_bound'] = 0
        elif rxn_id == 'LDH_D2':
            rxn_dict['lower_bound'] = 0
            rxn_dict['upper_bound'] = 0
        elif rxn_id == 'L-LACD2':
            rxn_dict['lower_bound'] = 0
            rxn_dict['upper_bound'] = 0
        elif rxn_id == 'L-LACD3':
            rxn_dict['lower_bound'] = 0
            rxn_dict['upper_bound'] = 0
        elif rxn_id == 'PFL':
            rxn_dict['lower_bound'] = 0
            rxn_dict['upper_bound'] = 0
    return reaction_dicts

def get_met_index_in_order(met_id, metabolites_dicts):
    index = 0
    for met_dict in metabolites_dicts:
        if met_dict['full_id'] == met_id:
            return index
        index += 1
    print("missed id: " + met_id)
    raise Exception

def get_rxn_index_in_order(rxn_id, reaction_dicts):
    index = 0
    for rxn_dict in reaction_dicts:
        if rxn_dict['rxn_id'] == rxn_id:
            return index
        index += 1
    print("missed id: " + rxn_id)
    raise Exception

def make_stoichiometry_matrix(stoic_elements, reaction_dicts, metabolites_dicts, _m, _n):
    stoic = np.zeros((_m, _n))
    for the_element in stoic_elements:
        met_id = get_met_index_in_order(the_element['met_full_id'], metabolites_dicts)
        rxn_id = get_rxn_index_in_order(the_element['rxn_id'], reaction_dicts)
        stoic[met_id, rxn_id] = the_element['coeff']
    return stoic

def make_bounds_vector(reaction_dicts, _n):
    lb = np.zeros(_n)
    ub = np.zeros(_n)
    index = 0
    for rxn_dict in reaction_dicts:
        # print(rxn_dict)
        lb[index] = rxn_dict['lower_bound']
        ub[index] = rxn_dict['upper_bound']
        index += 1
    return lb, ub

def get_model_metabolite(rxn_vec, mdl_metabolites, _m):
    rxn_metabolites = {}
    for i in range(_m):
        if rxn_vec[i] != 0:
            the_metabolite = mdl_metabolites[i]
            rxn_metabolites[the_metabolite] = rxn_vec[i]
    return rxn_metabolites

Computational Systems Biology - Fall 2021 (TA)/Other codes and data/BioModelParser.py


from functools import wraps
from unittest import TestCase

import six

from .store_generators import all_store_generators

class TestGenerator(type):

    def __new__(cls,name,bases,attrs):

        def backend_wrapper(f):

            @wraps(f)
            def wrapper(self ,*args, **kwargs):

                for StoreGeneratorCls in self.store_generators:
                    print("Testing with %s" % StoreGeneratorCls.__name__)
                    generator = StoreGeneratorCls(self)
                    self.store = generator.store
                    try:
                        f(self,*args,**kwargs)
                    finally:
                        generator.cleanup()

            return wrapper

        for name in attrs:
            if name.startswith("test_") and callable(attrs[name]):
                attrs[name] = backend_wrapper(attrs[name])

        return super(TestGenerator,cls).__new__(cls,name,bases,attrs)

@six.add_metaclass(TestGenerator)
class StoreTest(TestCase):

    store_generators = all_store_generators


import json
import math
import socket
import sys
import time
import os
from dotenv import load_dotenv
import requests
from multiprocessing import Process, Pool, Queue, Pipe
import threading
from enum_utils import CommandURLs

WORKLOAD_SESSION_SECRET_TOKEN = "do_not_alter_this_secret_session_token"

# command line usage: "py workload_generator.py --1"
# Where '--1' is the workload file to use as stated below

workload_paths = {
    "--1": "./workload_files/1userWorkLoad.txt",
    "--2": "./workload_files/2userWorkLoad.txt",
    "--10": "./workload_files/10User_testWorkLoad.txt",
    "--45": "./workload_files/45User_testWorkLoad.txt",
    "--100": "./workload_files/100User_testWorkLoad.txt",
    "--1000": "./workload_files/1000User_testWorkLoad.txt",
    "--2006": "./workload_files/final2006WorkLoad.txt",
    "--2007": "./workload_files/final2007.txt",
    "--2009": "./workload_files/final_workload_2009.txt",
    "--2010": "./workload_files/final_workload_spring_2010.txt",
    "--2011": "./workload_files/final_workload_2011.txt",
    "--2013": "./workload_files/final_workload_2013.txt",
    "--2014": "./workload_files/final_workload_2014.txt",
    "--2015": "./workload_files/final_workload_2015.txt",
    "--2016": "./workload_files/2016.txt",
    "--2017": "./workload_files/final_workload_2017.txt",
    "--2018": "./workload_files/final_workload_2018.txt",
    "--2019": "./workload_files/final_workload_2019.txt",
    "--2_test": "./workload_files/2user_test.txt",
    "--4_test": "./workload_files/4user_test.txt"
}

BUFFER_SIZE = 4096

class UserThread(threading.Thread):
    def __init__(self, name, args: tuple):
        super().__init__(name=name)
        self.name = name
        self.args = args

    def run(self):
        forward_requests(self.name, self.args[0], self.args[1])

def process_dumplog(output_filename, dumplog_response):
    if not dumplog_response:
        print("\033[1;31mDumplog empty!!!\033[0;0m")
    try:
        xml_string = json.loads(dumplog_response)["data"]
        with open(output_filename, 'w+') as f:
            f.write(xml_string)
    except KeyError:
        print(f"\033[1;31mWork_Gen-Error: no data attribute found in response. Response status {json.loads(dumplog_response)['status']}\033[0;0m")
    except Exception as e:
        print(f"\033[1;31mWork_Gen-Error: {e}\033[0;0m")

# Read work load file, process into a dictionary of user command lists that can be run in parallel:
# e.g. {"user1": [all commands for user1], "user2": [all commands for user2]}
# noinspection PyPep8Naming
def workload_to_user_command_dicts(file_obj):
    user_divided_commands = {}
    client_actions_raw = [line.rstrip().split()[-1].split(",") for line in file_obj]
    for i, action in enumerate(client_actions_raw):
        command = action[0]
        next_command = {"Command": command, "session": WORKLOAD_SESSION_SECRET_TOKEN}
        try:
            if command == "ADD":
                next_command["userid"] = action[1]
                next_command["amount"] = action[2]
            elif command == "QUOTE":
                next_command["userid"] = action[1]
                next_command["StockSymbol"] = action[2]
            elif command == "BUY":
                next_command["userid"] = action[1]
                next_command["StockSymbol"] = action[2]
                next_command["amount"] = action[3]
            elif command == "COMMIT_BUY":
                next_command["userid"] = action[1]
            elif command == "CANCEL_BUY":
                next_command["userid"] = action[1]
            elif command == "SELL":
                next_command["userid"] = action[1]
                next_command["StockSymbol"] = action[2]
                next_command["amount"] = action[3]
            elif command == "COMMIT_SELL":
                next_command["userid"] = action[1]
            elif command == "CANCEL_SELL":
                next_command["userid"] = action[1]
            elif command == "SET_BUY_AMOUNT":
                next_command["userid"] = action[1]
                next_command["StockSymbol"] = action[2]
                next_command["amount"] = action[3]
            elif command == "CANCEL_SET_BUY":
                next_command["userid"] = action[1]
                next_command["StockSymbol"] = action[2]
            elif command == "SET_BUY_TRIGGER":
                next_command["userid"] = action[1]
                next_command["StockSymbol"] = action[2]
                next_command["amount"] = action[3]
            elif command == "SET_SELL_AMOUNT":
                next_command["userid"] = action[1]
                next_command["StockSymbol"] = action[2]
                next_command["amount"] = action[3]
            elif command == "CANCEL_SET_SELL":
                next_command["userid"] = action[1]
                next_command["StockSymbol"] = action[2]
            elif command == "SET_SELL_TRIGGER":
                next_command["userid"] = action[1]
                next_command["StockSymbol"] = action[2]
                next_command["amount"] = action[3]
            elif command == "DISPLAY_SUMMARY":
                next_command["userid"] = action[1]
            elif command == "DUMPLOG":
                filenameIndex = 1
                usernameIndex = None
                if len(action) == 3:
                    filenameIndex = 2
                    usernameIndex = 1
                next_command["filename"] = action[filenameIndex]
                if usernameIndex is not None:
                    username = action[usernameIndex]
                    next_command["userid"] = username
                    if username in user_divided_commands:
                        user_divided_commands[username].append(next_command)
                    else:
                        user_divided_commands[username] = [next_command]
                elif "admin" in user_divided_commands:
                    user_divided_commands["admin"].append(next_command)
                else:
                    user_divided_commands["admin"] = [next_command]
            else:
                print(f"\033[1;31mInvalid command: #{i + 1} -> {action}\033[0;0m")
                continue
        except Exception as e:
            print(f"\033[1;31mWork_Gen:{e} | {action}\033[0;0m")
            continue

        if command == "DUMPLOG":
            continue
        elif action[1] in user_divided_commands:
            user_divided_commands[action[1]].append(next_command)
        else:
            user_divided_commands[action[1]] = [next_command]

    return user_divided_commands

# Creates a thread for each set of user requests, runs all created threads, assembles and returns responses
def process_user_requests(request_sets, main_pipe):
    # Create the user threads
    user_threads = []
    for usr_idx, user_requests in enumerate(request_sets):
        a, b = Pipe()
        thread = UserThread(name=f"{os.getpid()}|{usr_idx}", args=(user_requests, b))
        user_threads.append((thread, (a, b)))
        thread.start()

    # Wait for all threads to finish
    responses = []
    for t in user_threads:
        t[0].join()             # Halt main thread until all threads are complete
        t[1][1].close()         # Close pipe
        # resp = t[1][0].recv()   # Receive responses from the closed pipe
        # responses.append(resp)  # Append responses to the main response list

    # Send back list of lists of responses
    main_pipe.send(responses)
    print(f"num user response sets:{len(responses)}")

# Forwards requests for a single user to the web servers(or load balancer), returns responses
def forward_requests(thread_name, user_requests, user_pipe):
    # session = requests.session()
    responses = []
    for idx, user_request in enumerate(user_requests):
        try:
            print(f"-->SENT:{idx + 1} | pid|thr:{thread_name} | rqst:{user_request}")
            sckt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sckt.settimeout(5)
            sckt.connect((load_balancer_ip, load_balancer_port))
            data = json.dumps(user_request)
            http_request = f"POST {CommandURLs[user_request['Command']].value} HTTP/1.1\nHOST: {load_balancer_ip}:{load_balancer_port}\nContent-Type: application/json\nAccept: application/json\n{data}"
            # print(http_request)
            sckt.sendall(str.encode(http_request))
            #TODO: We are possibly receiving 2 responses at the same time, or the threads are printing at the same time, latter is unlikely though.
            response = sckt.recv(BUFFER_SIZE).decode()
            # responses.append(response)
            sckt.shutdown(socket.SHUT_RDWR)
            sckt.close()
            print(f"<--RCVD:{idx + 1} | pid|thr:{thread_name} | {response}")
        except Exception as e:
            print(f"\033[1;31mWork_Gen:{e} | {user_request}\033[0;0m")
    print(f"finished:{thread_name} | active:{threading.active_count()}")
    # user_pipe.send(responses)
    # user_pipe.close()

def recvall(sock):
    # Helper function to recv all bytes from response
    # Must have server side close connection when finished to ensure EOF is caught on this end.
    data = bytearray()
    while True:
        try:
            packet = sock.recv(BUFFER_SIZE)
            # print(f"pktlen:{len(packet)}")
            if len(packet) < BUFFER_SIZE:
                data.extend(packet)
                sock.close()
                break
            data.extend(packet)
        except Exception as e:
            print(f"\033[1;31mWork_Gen.recvall:{e}\033[0;0m")
    return data

if __name__ == "__main__":
    load_dotenv()
    load_balancer_ip = os.environ.get("load_balancer_host")
    load_balancer_port = int(os.environ.get("load_balancer_port"))
    protocol = "http"
    load_balancer_url = f"{protocol}://{load_balancer_ip}:{load_balancer_port}"
    startT = time.time()
    file_index = sys.argv[1]
    file_object = open(workload_paths[file_index])
    user_cmd_dict = workload_to_user_command_dicts(file_object)
    admin_dumplog = user_cmd_dict.pop("admin", None)[0]
    user_cmd_dict_list = list(user_cmd_dict.values())
    num_users = len(user_cmd_dict_list)

    # The lab computers are dual core. Should use threading beyond this to simulate multiple active users.
    processes_to_create = 2
    users_on_first_process = math.ceil(num_users / processes_to_create)
    users_on_second_process = num_users - users_on_first_process
    print(f"num_users:{num_users}")

    try:
        a1, b1 = Pipe()
        a2, b2 = Pipe()
        l1 = user_cmd_dict_list[:users_on_first_process]
        l2 = user_cmd_dict_list[users_on_first_process:]
        p1 = Process(target=process_user_requests, args=(l1, b1))
        p2 = Process(target=process_user_requests, args=(l2, b2))

        # Start the processes
        p1.start()
        p2.start()
        # Halt main process until both sub-processes finish
        p1.join()
        p2.join()

    except KeyboardInterrupt:
        p1.kill()
        p2.kill()
        exit()

    # TODO: Need to fix the pipe so we can analyze returned data
    # b1.close()
    # b2.close()
    # resps1 = a1.recv()
    # resps2 = a2.recv()
    # num_resps = 0
    # for element in resps1:
    #     num_resps += len(element)
    # for element in resps2:
    #     num_resps += len(element)
    # print(f"Total number responses:{num_resps}")

    endT = time.time()
    print(f"runtime: {endT - startT}")
    print("dumplog stubbed!")

    # if admin_dumplog is not None:
    #     try:
    #         print(f"Printing dumplog!")
    #         sckt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #         # sckt.settimeout(2)
    #         sckt.connect((load_balancer_ip, load_balancer_port))
    #         data = json.dumps(admin_dumplog)
    #         http_request = f"POST {CommandURLs[admin_dumplog['Command']].value} HTTP/1.1\nHOST: {load_balancer_ip}:{load_balancer_port}\nContent-Type: application/json\nAccept: application/json\n{data}"
    #         # print(http_request)
    #         sckt.sendall(str.encode(http_request))
    #         print("Receiving dumplog...")
    #         response = recvall(sckt).decode()
    #         print("Dumplog received!")
    #         process_dumplog(admin_dumplog["filename"], response)
    #     except KeyError:
    #         pass
    #     except Exception as e:
    #         print(f"\033[1;31mWork_Gen-Error: {e}\033[0;0m")