import random, time, copy
from termcolor import cprint,colored
import style
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

# import stdarray

#place bomb in random places in the grid, if random lands on an previous bomb, it loops again
def placeBomb(grid):
    while True:
        if not grid[random.randint(0, len(grid) - 1)][random.randint(0, len(grid[0]) - 1)] == '*':
            grid[random.randint(0, len(grid) - 1)][random.randint(0, len(grid[0]) - 1)] = '*'
            return

#adds to the grid numbers which represent the number of bombs in adjacent spaces
def updateValues(row_n, col, grid):
    #Row above.
    if row_n - 1 > -1:
        row = grid[row_n - 1]
        if col - 1 > -1:
            if not row[col - 1] == '*':
                row[col - 1] += 1
        if not row[col] == '*':
            row[col] += 1
        if len(grid[0]) > col + 1:
            if not row[col + 1] == '*':
                row[col + 1] += 1
    #Same row.
    row = grid[row_n]
    if col - 1 > -1:
        if not row[col - 1] == '*':
            row[col - 1] += 1
    if len(grid[0]) > col + 1:
        if not row[col + 1] == '*':
            row[col + 1] += 1
    #Row below.
    if len(grid) > row_n + 1:
        row = grid[row_n + 1]
        if col - 1 > -1:
            if not row[col - 1] == '*':
                row[col - 1] += 1
        if not row[col] == '*':
            row[col] += 1
        if len(grid[0]) > col + 1:
            if not row[col + 1] == '*':
                row[col + 1] += 1

def checkZeros(shown_grid, solution_grid, row, col):
    oldGrid = copy.deepcopy(shown_grid)

    zeroProcedure(row, col, shown_grid, solution_grid)

    while oldGrid != shown_grid:
        oldGrid = copy.deepcopy(shown_grid)
        for row in range(len(shown_grid)):
            for col in range(len(shown_grid[0])):
                if shown_grid[row][col] == 0:
                    zeroProcedure(row, col, shown_grid, solution_grid)
        if oldGrid == shown_grid:
            return

def zeroProcedure(row_n, col_n, shown_grid, solution_grid):
    #Row above
    if row_n-1 > -1:
        row = shown_grid[row_n-1]
        if col_n-1 > -1:
            row[col_n-1] = solution_grid[row_n -1 ][col_n -1]
        row[col_n] = solution_grid[row_n - 1][col_n]
        if len(shown_grid[0]) > col_n + 1:
            row[col_n+1] = solution_grid[row_n - 1][col_n + 1]
    #Same row
    row = shown_grid[row_n]
    if col_n - 1 > -1:
        row[col_n-1] = solution_grid[row_n][col_n -1]
    if len(shown_grid[0]) > col_n+1:
        row[col_n+1] = solution_grid[row_n][col_n + 1]
    #Row below
    if len(shown_grid) > row_n+1:
        row = shown_grid[row_n+1]
        if col_n-1 > -1:
            row[col_n-1] = solution_grid[row_n + 1][col_n - 1]
        row[col_n] = solution_grid[row_n + 1][col_n]
        if len(shown_grid[0]) > col_n+1:
            row[col_n+1] = solution_grid[row_n + 1][col_n + 1]

def playAgain(start_time):
    print('Time: ' + str(round(time.time() - start_time)) + 's')
    while True:
        playBla = input('Play again? (Y/N): ').lower()
        if playBla == 'y':
            return 1
        if playBla != 'n':
            cprint('Choice not supported, please type (Y/N)!', 'red')
            continue
        cprint('Thank you for playing!', 'yellow')
        quit()

def play(solution_grid, shown_grid, mines):
    start_time = time.time()
    while True:
        style.printGrid(shown_grid)
        row, col = choose(solution_grid, shown_grid)

        if solution_grid[row][col] == '*' and shown_grid[row][col] != '⚐':
            style.printGrid(solution_grid)
            print(f"You DEAD {colored('Looser!', 'red', 'on_cyan')}")
            if playAgain(start_time) == 1:
                return 1
        if shown_grid[row][col] != '⚐':
            shown_grid[row][col] = solution_grid[row][col]
        if solution_grid[row][col] == 0 and shown_grid[row][col] != '⚐':
            checkZeros(shown_grid, solution_grid, row, col)

        squaresLeft = 0
        for row in shown_grid:
            squaresLeft += row.count(' ') + row.count('⚐')

        if squaresLeft == mines:
            style.printGrid(solution_grid)
            print('You win!')
            if playAgain(start_time) == 1:
                return 1

def choose(solution_grid, shown_grid):
    letters = [chr(i) for i in range(97, 97 + len(shown_grid[0]))]
    numbers = [str(i) for i in range(len(shown_grid))]

    #Loop in case of invalid entry.
    while True:
        chosen = input('Choose a cell (eg. B2) or place a marker (eg. mB2): ').lower()
        if len(chosen) == 3 and chosen[0] == 'm' and chosen[1] in letters and chosen[2] in numbers:
            if shown_grid[int(chosen[2])][ord(chosen[1])-97] != ' ':
                print('You cannot put a marker on an already shown cell')
                continue
            shown_grid[int(chosen[2])][ord(chosen[1])-97] = '⚐'
            return (int(chosen[2]), ord(chosen[1])-97)
        elif len(chosen) == 2 and chosen[0] in letters and chosen[1] in numbers:
            return (int(chosen[1]), ord(chosen[0])-97)

def main():
    rows = 10
    columns = 10
    mines = 10
    choice = ''

    style.clear()
    while choice != 'P' and choice != 'I':
        print(f'''Welcome to {colored("MINESWEEPER", "red", "on_cyan")} 1.0!!
Created by {colored('Victor Hugo AIZPURUA', 'red')}
For instructions, type '{colored("I", "blue")}'
To play, type '{colored("P", "green")}'

        ''')

        choice = input("What's your choice?: ").upper()

        if choice == 'I':
            style.clear()
            cprint(open('instructions.txt', 'r').read(), 'blue')
            input('Press [enter] when ready to play. ')
        elif choice != 'P':
            style.clear()
            continue
        while True:
            solution_grid = [[0 for j in range(rows)] for i in range(columns)]
            for n in range (mines):
                placeBomb(solution_grid)
            for row in range(rows):
                for column in range(columns):
                    if solution_grid[row][column] == '*':
                        updateValues(row, column, solution_grid)

            shown_grid = [[' ' for j in range(rows)] for i in range(columns)]
            play(solution_grid, shown_grid, mines)

main()


import joblib
import numpy as np
import torch
import torch.autograd as autograd
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

import numpy as np
torch.manual_seed(1)
np.random.seed(1)

num_features = 1
num_hidden = 1
output_size = 1

#defining the model
lstm = nn.GRU(num_features, num_hidden).cuda()
output_layer = nn.Linear(num_hidden, output_size).cuda()

loss_function = nn.MSELoss()

optimizer = optim.SGD([
                {'params': lstm.parameters()},
                {'params': output_layer.parameters()}
            ], lr = 0.001)

counter = 0
loss_val = 0
#Training lstm on Synthetic Dataset
while(counter<5000):
        coin_flip = np.random.binomial(1,0.5,1)

        T = np.random.randint(10,15)
        input_data = np.random.normal(0.0,0.2,T)
        if coin_flip == 1:
            input_data[:3] = 1.0 + np.random.normal(0.0,0.2,3)
            y_label = 1.0
        else:
            input_data[:3] = -1.0 + np.random.normal(0.0,0.2,3)
            y_label = 0.0

        optimizer.zero_grad()

        hidden = autograd.Variable(torch.zeros(1, 1, num_hidden).type(torch.cuda.FloatTensor))
        input_torchvar = autograd.Variable(torch.cuda.FloatTensor(input_data), requires_grad=True)
        output, hidden = lstm(input_torchvar.view(len(input_data), 1, -1), hidden)

        output_2 = output_layer(output[-1])

        loss = loss_function(F.sigmoid(output_2).view(1), autograd.Variable(torch.cuda.FloatTensor([y_label])))
        loss_val += loss.item()
        loss.backward()
        print(loss)
        optimizer.step()

        counter += 1
print(loss_val/1000.0)
loss_val = 0
#saving model
torch.save(lstm, "toy_lstm_trained_model")
torch.save(output_layer, "toy_output_layer_trained_model")

'''Calculating attribution score via ACE and IG'''
import joblib
import numpy as np
import torch
import torch.autograd as autograd
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import matplotlib.pyplot as plt
import torch
import torch.autograd as autograd
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
torch.manual_seed(1)
np.random.seed(1)
import os,csv,math, sys,joblib
import matplotlib.pyplot as plt

import sklearn.model_selection, sklearn.preprocessing, copy
from scipy.special import factorial
from sklearn.preprocessing import MinMaxScaler
import numpy as np
torch.manual_seed(1)
np.random.seed(1)

num_features = 1
num_hidden = 1
output_size = 1

lstm = torch.load("toy_lstm_trained_model").cpu()#Loading trained model
output_layer = torch.load("toy_output_layer_trained_model").cpu()#Loading trained model

mean_vector = [0.0]*15
baseline_expectation_do_x  =[0.5076135909386664, 0.2711588819908657, 5.629422495855416, 0.022658583459495278, 0.011492128996702376, 0.011273759561358017, 0.011215446889473235, 0.01119493013212923, 0.011193315653370518, 0.011205433818904565, 0.011231432442822552, 0.011275079370316233, 0.011344116069245506, 0.011452266314474399, 0.011623865903464321]
cov_data=np.eye(15)*0.2
#sampled from Synthetic data distribution
input_data=np.array([ 1.07825653,  1.44238297,  0.96755107, -0.02216889,  0.28289655,
       -0.283329  ,  0.13470269, -0.15445888, -0.01877541, -0.0339548 ,
       -0.10822893,  0.10758952])

T=12#Length of sequence

##Integrated Gradient##
lstm.zero_grad()
integrated_gradients = np.zeros(input_data.shape[0])
path_vector = np.zeros(input_data.shape[0])
final_vector = input_data
for calls_to_gradients in range(5000):
    hidden = autograd.Variable(torch.zeros(1, 1, num_hidden).type(torch.FloatTensor), requires_grad=True)
    input_torchvar = autograd.Variable(torch.FloatTensor(path_vector), requires_grad=True)
    output, hidden = lstm(input_torchvar.view(len(path_vector), 1, -1), hidden)
    output_2 = F.sigmoid(output_layer(output[-1]))

    first_grads = torch.autograd.grad(output_2, input_torchvar, retain_graph=True, create_graph=True, only_inputs=True, allow_unused=False)
    integrated_gradients += first_grads[0].data.cpu().numpy()

    path_vector += (1.0/5000)*(final_vector - np.zeros(input_data.shape[0]))
myMatrix = (integrated_gradients/5000.0)*input_data.reshape(1,-1)

myMatrix = np.ma.masked_where(myMatrix < 0.0, myMatrix)
plt.imshow(myMatrix, cmap='coolwarm', interpolation='nearest')
plt.colorbar()

plt.title("IG" )

plt.show()
plt.clf()

##ACE##
lstm.zero_grad()
input_data = np.array(input_data)
average_causal_effects = []
for t in range(len(input_data)):
    expected_value = 0.0
    expectation_do_x = []
    inp = copy.deepcopy(mean_vector)

    inp[t] = input_data[t]
    hidden = autograd.Variable(torch.zeros(1, 1, num_hidden).type(torch.FloatTensor), requires_grad=True)
    input_torchvar = autograd.Variable(torch.FloatTensor(inp), requires_grad=True)
    output, hidden = lstm(input_torchvar.view(len(inp), 1, -1), hidden)
    output_2 = F.sigmoid(output_layer(output[-1]))

    val = output_2.data.view(1).cpu().numpy()[0]

    first_grads = torch.autograd.grad(output_2, input_torchvar, retain_graph=True, create_graph=True, only_inputs=True, allow_unused=False) #as only one output

    first_grad_shape = first_grads[0].data.size()
    lower_order_grads = first_grads

    for dimension in range(len(mean_vector)):
        if dimension == t:
            continue
        grad_mask = torch.zeros(first_grad_shape)
        grad_mask[dimension] = 1.0

        higher_order_grads = torch.autograd.grad(lower_order_grads, input_torchvar, grad_outputs=grad_mask, retain_graph=True, create_graph=True, only_inputs=True, allow_unused=False)
        higher_order_grads_array = np.array(higher_order_grads[0].data)

        temp_cov = copy.deepcopy(cov_data)
        temp_cov[dimension][t] = 0.0
        val += 0.5*np.sum(higher_order_grads_array*temp_cov[dimension])

    average_causal_effects.append(val)

average_causal_effects = np.array(average_causal_effects) - np.array(baseline_expectation_do_x)[:len(average_causal_effects)]

myMatrix = average_causal_effects.reshape(1,-1)
myMatrix = np.ma.masked_where(myMatrix < 0.0, myMatrix)
plt.imshow(myMatrix, cmap='coolwarm', interpolation='nearest')
plt.colorbar()
plt.title("ACE")

plt.show()
plt.clf()


# -*- coding: utf-8 -*-

from sklearn.metrics import *
from sklearn.model_selection import learning_curve
from sklearn.model_selection import GridSearchCV
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os
from cie.common import logger
# use TkAgg backend
matplotlib.use("TkAgg")

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['savefig.dpi'] = 200
plt.rcParams['figure.dpi'] = 200

tuning_folder = './output/tuning/'
if not os.path.exists(tuning_folder):
    os.makedirs(tuning_folder)

logger = logger.get_logger(name=logger.get_name(__file__))

def grid_search_tuning(estimator, x_train, y_train, x_test, y_test, param_grid=None, cv=5,
                       scores={"acc": ("accuracy", None)}, features_names=None, regressor=False, fit_params=None, do_plot=False):
    """
    参数grid search交叉tuning
    :param estimator: 模型评估器
    :param x_train: 训练数据特征
    :param y_train: 训练数据标签
    :param x_test: 测试数据特征
    :param y_test: 测试数据标签
    :param param_grid: 参数字典
    :param cv: cv类型
    :param scores: dict: key为别名， value为tuple(sklearn支持的score方法，score函数），对于分类问题，score函数可以设置为None;
    目前只支持单一score
    :param features_names: 特征名字列表
    :param regressor: 是否为回归，默认为False
    :return: best_estimator: 最好的estimator。
    """
    results = dict()
    for score in scores:
        logger.info("# 超参数tuning: %s" % score)
        score_new = scores[score][0]
        score_func = scores[score][1]
        if scores[score] in ['f1', 'precision', 'recall']:
            score_new = '%s_macro' % score_new
        logger.info(f"param_grid: {param_grid}")
        if regressor:
            gs = GridSearchCV(estimator, param_grid, cv=cv, fit_params=fit_params, scoring=score_new)
        else:
            gs = GridSearchCV(estimator, param_grid, cv=cv, fit_params=fit_params,
                              scoring={score: score_new}, refit=score, return_train_score=True)
            # gs = GridSearchCV(estimator, param_grid, cv=cv, scoring=score_new, class_weight=class_weight)
        gs.fit(x_train, y_train)

        if regressor:
            best_index_ = gs.cv_results_["rank_test_score"].argmax()
            best_params_ = gs.cv_results_["params"][best_index_]
            logger.info(f"交叉验证集上最好的参数：{best_params_}")
        else:
            logger.info(f"交叉验证集上最好的参数：{gs.best_params_}")
        logger.info("交叉验证集上的各个参数表现：")
        if regressor:
            means = gs.cv_results_['mean_test_score']
            stds = gs.cv_results_['std_test_score']
        else:
            means = gs.cv_results_['mean_test_' + score]
            stds = gs.cv_results_['std_test_' + score]
        for mean, std, params in zip(means, stds, gs.cv_results_['params']):
            logger.info("mean: %0.3f, std: (+/-%0.03f) for %r"
                  % (mean, std * 2, params))
        if y_test is not None:
            y_true, y_pred = y_test, gs.predict(x_test)
            if regressor:
                logger.info("回归评估 %s: %.3f" % (score, score_func(y_true, y_pred)))
            else:
                logger.info("测试集上的分类效果报告:")
                logger.info(classification_report(y_true, y_pred))
        results.update(gs.cv_results_)
        best_estimator = gs.best_estimator_

    if do_plot:
        X_axis = results['params']
        plt.figure(figsize=(13, 13))
        plt.title("GridSearchCV evaluating using multiple scorers simultaneously",
                  fontsize=16)

        plt.xlabel("Parameter")
        plt.ylabel("Score")

        ax = plt.gca()
        # if not regressor:
        #     ax.set_ylim(0.01, 1.01)

        # Get the regular numpy array from the MaskedArray

        colormap = plt.cm.get_cmap(lut=len(scores) + 1)
        colors = [colormap(i) for i in range(len(scores))]
        for scorer, color in zip(sorted(scores), colors):
            for sample, style in (('train', '--'), ('test', '-')):
                if regressor:
                    sample_score_mean = results['mean_%s_score' % sample]
                    sample_score_std = results['std_%s_score' % sample]
                else:
                    sample_score_mean = results['mean_%s_%s' % (sample, scorer)]
                    sample_score_std = results['std_%s_%s' % (sample, scorer)]
                ax.fill_between(X_axis, sample_score_mean - sample_score_std,
                                sample_score_mean + sample_score_std,
                                alpha=0.1 if sample == 'test' else 0, color=color)
                ax.plot(X_axis, sample_score_mean, style, color=color,
                        alpha=1 if sample == 'test' else 0.7,
                        label="%s (%s)" % (scorer, sample))
            # if regressor:
            #     best_index = np.nonzero(results['rank_test_score'] == len(results['rank_test_score']))[0][0]
            #     best_score = results['mean_test_score'][best_index]
            # else:
            best_index = np.nonzero(results['rank_test_%s' % scorer] == 1)[0][0]
            best_score = results['mean_test_%s' % scorer][best_index]

            # Plot a dotted vertical line at the best score for that scorer marked by x
            ax.plot([X_axis[best_index], ] * 2, [0, best_score],
                    linestyle='-.', color=color, marker='x', markeredgewidth=3, ms=8)

            # Annotate the best score for that scorer
            ax.annotate("%0.2f" % best_score,
                        (X_axis[best_index], best_score + 0.005))
        plt.xticks(rotation=75)
        plt.subplots_adjust(bottom=.3)
        plt.legend(loc="best")
        plt.grid('off')
        plt.show()

        # 分析feature importance
        if features_names is not None:
            feat_imp = pd.Series(best_estimator.fit(x_train, y_train).feature_importances_, features_names).sort_values(
                ascending=False)
        else:
            feat_imp = pd.Series(best_estimator.fit(x_train, y_train).feature_importances_).sort_values(ascending=False)
        feat_imp.plot(kind='bar', title='Feature Importance')
        plt.ylabel('Feature Importance Score')
        plt.show()

    return best_estimator

def plot_learning_curve(estimator, title, X, y, ylim=None, scoring=None, cv=None,
                        n_jobs=None, train_sizes=np.linspace(.1, 1.0, 5)):
    """
    Generate a simple plot of the test and training learning curve.

    Parameters
    ----------
    estimator : object type that implements the "fit" and "predict" methods
        An object of that type which is cloned for each validation.

    title : string
        Title for the chart.

    X : array-like, shape (n_samples, n_features)
        Training vector, where n_samples is the number of samples and
        n_features is the number of features.

    y : array-like, shape (n_samples) or (n_samples, n_features), optional
        Target relative to X for classification or regression;
        None for unsupervised learning.

    ylim : tuple, shape (ymin, ymax), optional
        Defines minimum and maximum yvalues plotted.
    scoring : string, callable or None, optional, default: None
        A string (see model evaluation documentation) or
        a scorer callable object / function with signature
        ``scorer(estimator, X, y)``.
    cv : int, cross-validation generator or an iterable, optional
        Determines the cross-validation splitting strategy.
        Possible inputs for cv are:
          - None, to use the default 3-fold cross-validation,
          - integer, to specify the number of folds.
          - :term:`CV splitter`,
          - An iterable yielding (train, test) splits as arrays of indices.

        For integer/None inputs, if ``y`` is binary or multiclass,
        :class:`StratifiedKFold` used. If the estimator is not a classifier
        or if ``y`` is neither binary nor multiclass, :class:`KFold` is used.

        Refer :ref:`User Guide <cross_validation>` for the various
        cross-validators that can be used here.

    n_jobs : int or None, optional (default=None)
        Number of jobs to run in parallel.
        ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all processors. See :term:`Glossary <n_jobs>`
        for more details.

    train_sizes : array-like, shape (n_ticks,), dtype float or int
        Relative or absolute numbers of training examples that will be used to
        generate the learning curve. If the dtype is float, it is regarded as a
        fraction of the maximum size of the training set (that is determined
        by the selected validation method), i.e. it has to be within (0, 1].
        Otherwise it is interpreted as absolute sizes of the training sets.
        Note that for classification the number of samples usually have to
        be big enough to contain at least one sample from each class.
        (default: np.linspace(0.1, 1.0, 5))
    """
    plt.figure()
    plt.title(title)
    if ylim is not None:
        plt.ylim(*ylim)
    plt.xlabel("Training examples")
    plt.ylabel("Score")
    train_sizes, train_scores, test_scores = learning_curve(
        estimator, X, y, scoring=scoring, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
    train_scores_mean = np.mean(train_scores, axis=1)
    train_scores_std = np.std(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)
    test_scores_std = np.std(test_scores, axis=1)
    plt.grid()

    plt.fill_between(train_sizes, train_scores_mean - train_scores_std,
                     train_scores_mean + train_scores_std, alpha=0.1,
                     color="r")
    plt.fill_between(train_sizes, test_scores_mean - test_scores_std,
                     test_scores_mean + test_scores_std, alpha=0.1, color="g")
    plt.plot(train_sizes, train_scores_mean, 'o-', color="r",
             label="Training score")
    plt.plot(train_sizes, test_scores_mean, 'o-', color="g",
             label="Cross-validation score")

    plt.legend(loc="best")
    return plt

def plot_roc(model, X, y):
    """
    Plot ROC curve of 'model' on X and y.

    Parameters
    ----------
    model: fitted model
    X : array-like, shape [n_samples, n_feature]
    y: the label,must be binary(i.e.

    Returns
    -------
    ROC curve
    """
    # plot setting
    plt.style.use("ggplot")
    f = plt.subplot()
    f.set_title('ROC Curve (%s)' % model.__class__.__name__, {'fontsize': 20})
    f.set_xlabel('fpr', {'fontsize': 20})
    f.set_ylabel('tpr', {'fontsize': 20})

    proba = model.predict_proba(X)[:, 1]
    fpr, tpr, thr = roc_curve(y, proba)
    line, = f.plot(fpr, tpr)

    f.legend(line, model.__class__.__name__, loc='lower right', fontsize='large')

def _round(x):
    if isinstance(x, float):
        return np.round(x, 3)
    else:
        return x

def classific_report(model, X, y):
    """
    classification performance of 'model' on X and y.

    Parameters
    ----------
    model: fitted model
    X : array-like, shape [n_samples, n_feature]
    y: the label,must be binary(i.e.

    Returns
    -------
    the values of main evaluation metrics with respect to 'model'
    """
    print(model.__class__.__name__)

    pred = model.predict(X)
    print('%d个特征' % X.shape[1])
    acc = model.score(X, y)
    print('准确率', acc)

    confusionMatrix = confusion_matrix(y, pred)
    print(confusionMatrix)

    if len(np.unique(y)) == 2:
        proba = model.predict_proba(X)[:, 1]
        auc = roc_auc_score(y, proba)
        print('AUC', auc)

    sensitivity = confusionMatrix[0, 0] / confusionMatrix.sum(axis=0)[0]
    specificity = confusionMatrix[1, 1] / confusionMatrix.sum(axis=0)[1]
    print('敏感度: %.3f, 特异度: %.3f' % (sensitivity, specificity))

    performance = [model.__class__.__name__, acc, sensitivity, specificity]
    performance = list(map(_round, performance))
    return performance

def regression_report(model, X, y):
    """
    regression performance of 'model' on X and y.

    Parameters
    ----------
    model: fitted model
    X : array-like, shape [n_samples, n_feature]
    y: the label,must be binary(i.e.

    Returns
    -------
    the values of main evaluation metrics with respect to 'model'
    """
    print(model.__class__.__name__)
    performance = []
    pred = model.predict(X)
    mae = mean_absolute_error(y, pred)
    mse = mean_squared_error(y, pred)
    evs = explained_variance_score(y, pred)
    r2 = r2_score(y, pred)
    performance = [model.__class__.__name__, mae, mse, evs, r2]
    performance = list(map(_round, performance))
    return performance


'''
Main program to perform the ETL on the Redshift cluster
'''

import configparser
import psycopg2
from sql_queries import copy_table_queries, insert_table_queries

def load_staging_tables(cur, conn):

    '''
    FUNCTION:   load_staging_tables
    PURPOSE:    Runs SQL queries to copy staging tables from S3
    INPUT:      cur:    a specified cursor on Redshift cluster
                conn:   a specified connection to Redshift cluster
    OUTPUT:     None
    '''

    for query in copy_table_queries:
        cur.execute(query)
        conn.commit()

        print('Executing query: {}'.format(query))

def insert_tables(cur, conn):

    '''
    FUNCTION:   insert_tables
    PURPOSE:    Runs SQL queries to insert values into specified tables
    INPUT:      cur:    a specified cursor on Redshift cluster
                conn:   a specified connection to Redshift cluster
    OUTPUT:     None
    '''

    for query in insert_table_queries:
        cur.execute(query)
        conn.commit()

        print('Executing query: {}'.format(query))

def main():

    '''
    FUNCTION:   main
    PURPOSE:    Connects to a Redshift cluster and runs the specified functions to perform the ETL
    INPUT:      None
    OUTPUT:     No explicit return, prints to the console
    '''

    config = configparser.ConfigParser()
    config.read('dwh.cfg')

    conn = psycopg2.connect("host={} dbname={} user={} password={} port={}".format(*config['CLUSTER'].values()))
    cur = conn.cursor()

    print('Copying staging tables. Please wait...')
    load_staging_tables(cur, conn)
    print('Staging tables successfully loaded!')

    print('Inserting into tables. Please wait...')
    insert_tables(cur, conn)
    print('Values inserted successfully into tables!')

    cur.close()
    conn.close()

if __name__ == "__main__":
    main()


import my_io
import io

# ugliest code ever writter, probably

input = my_io.get_clipboard_contents()

uniques = set()

lines = input.splitlines()

template = lines[0]

for c in template:
    uniques.add(c)

rules = dict()

for i in range(2, len(lines)):

    (key, value) = lines[i].split(' -> ')
    rules[key] = value

    (k1, k2) = key
    uniques.add(k1)
    uniques.add(k2)
    uniques.add(value)

replacements = dict()

# generate replacements from 0-10 steps

for key in rules:

    replacement = rules[key]

    seq = key

    reps = 21 * [None]

    reps[0] = seq

    for i in range(1, 11):

        next_index = 0

        while next_index < len(seq) - 1:

            pair = seq[next_index:next_index + 2]
            replacement = rules[pair]

            if pair in rules:
                seq = seq[:next_index + 1] + \
                    replacement + seq[next_index + 1:]
                next_index += 2
            else:
                next_index += 1

        reps[i] = seq

    replacements[key] = reps

# generate replacements for 20 steps

for key in rules:

    reps = replacements[key]

    seq = reps[10]

    buff = io.StringIO()

    for j in range(0, len(seq) - 1):

        pair = seq[j:j+2]
        rep = replacements[pair][10]

        buff.write(rep[:len(rep)-1])

    buff.write(rep[-1:])

    reps[20] = buff.getvalue()

template20 = ''

for i in range(0, len(template) - 1):

    pair = template[i:i+2]
    rep = replacements[pair][20]

    template20 += rep[:len(rep) - 1]
template20 += rep[-1:]

hits = dict()

hits_dict = dict()

for key in rules:

    hits_dict[key] = dict()

    for k in uniques:
        hits_dict[key][k] = 0

    rep = replacements[key][20]

    for i in range(0, len(rep) - 1):

        c = rep[i]
        hits_dict[key][c] += 1

hits = dict()

for k in uniques:
    hits[k] = 0

for i in range(0, len(template20) - 1):

    pair = template20[i: i+2]
    hits20 = hits_dict[pair]

    for k in uniques:
        hits[k] += hits20[k]

hits[template20[len(template20) - 1]] += 1

print(hits)


from time import sleep

from appium import webdriver

# server 启动参数
from appium.webdriver.common.touch_action import TouchAction

desired_caps = {}
# 设备信息
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '192.168.56.101:5555'
# 输入中文
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
# app的信息
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'

# 声明我们的driver对象
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

"""
    目标：模拟音量键
    需求：
        1. 减三次音量缩小键，每减1次暂停1秒
        2. 增三次音量增大键，每增1次暂停1秒

    提示：
        1. 24为增加音量键码
        2. 25为：减少音量键码
        3. 方法：keyevent()

"""
# 减少 3此音量键
for i in range(6):
    print("i:",i)
    sleep(1)
    driver.keyevent(25)
    sleep(1)
    driver.keyevent(24)

# # 增加 3此音量键
# for y in range(4):
#     sleep(1)
#     print("y:",y)
#     driver.keyevent(24)

sleep(10)
driver.quit()


from sklearn.cluster import KMeans
from sklearn.cluster import DBSCAN

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

import numpy as np
import pandas as pd

import sklearn.preprocessing as pro
from sklearn.preprocessing import LabelEncoder

import func as func

if __name__ == '__main__':
    # load data
    df1 = pd.read_table('.\data\Accelerometer_climb_descent_stairs\climb_stairs.txt', sep=' ', header=None)
    df2 = pd.read_table('.\data\Accelerometer_climb_descent_stairs\descend_stairs.txt', sep=' ', header=None)
    df1.columns = ['x','y','z']
    df2.columns = ['x','y','z']
    df1['ADL'] = 'climb_stairs'
    df2['ADL'] = 'descend_stairs'
    df = df1.append(df2)
    df = df.reset_index(drop=True)

    data = df.loc[:, ['x', 'y', 'z']]
    class_labels = df.loc[:, ['ADL']]
    true_labels = LabelEncoder().fit_transform(class_labels)

    min_max_scaler = pro.MinMaxScaler()  # normalization to [0,1]
    data = min_max_scaler.fit_transform(data)

    # clustering : K-means & DBSCAN
    k_means_pred = KMeans(n_clusters=2, random_state=128).fit_predict(data)

    # determine the parameters (Eps, minPts) for DBSCAN
    from sklearn.neighbors import NearestNeighbors
    nbrs = NearestNeighbors(n_neighbors=5).fit(data)
    distances, _ = nbrs.kneighbors(data)
    distanceDec = sorted(distances[:, 4], reverse=False)
    indices = range(len(distanceDec))
    plt.plot(indices, distanceDec)
    plt.show()
    dbscan_pred = DBSCAN(eps=0.11, min_samples=5).fit_predict(data)

    # compute evaluation criteria: DBI,SC,MI,NMI,AMI
    print("Class Label")
    func.cal_int_ind(data, true_labels)

    print("K-means")
    func.cal_int_ind(data, k_means_pred)
    func.cal_ext_ind(true_labels, k_means_pred)

    print("DBSCAN")
    func.cal_int_ind(data, dbscan_pred)
    func.cal_ext_ind(true_labels, dbscan_pred)

    # plot clustering result
    y_pred = pd.DataFrame(dbscan_pred)

    dt_0 = data[class_labels['ADL'] == 'climb_stairs']
    y_pred_0 = y_pred[class_labels['ADL'] == 'climb_stairs']
    dt_1 = data[class_labels['ADL'] == 'descend_stairs']
    y_pred_1 = y_pred[class_labels['ADL'] == 'descend_stairs']

    fig = plt.figure()
    ax = Axes3D(fig)

    ax.set_ylim(0, 0.9)
    ax.set_zlim(0, 0.9)
    ax.view_init(elev=40.,azim=135)

    color_0 = func.cal_col(np.array(y_pred_0))
    color_1 = func.cal_col(np.array(y_pred_1))
    ax.scatter(dt_0[:, 0], dt_0[:, 1], dt_0[:, 2], c=color_0, s=50, marker='^')
    ax.scatter(dt_1[:, 0], dt_1[:, 1], dt_1[:, 2], c=color_1, s=80, marker='.')
    #plt.legend(labels=['climb_stairs', 'descend_stairs'], loc=1, prop={'size': 15})

    ax.set_xticklabels([])  # remove the tick mark of x axis
    ax.set_yticklabels([])  # remove the tick mark of y axis
    ax.set_zticklabels([])  # remove the tick mark of z axis

    font2 = {'family': 'Times New Roman',
             'weight': 'normal',
             'size': 20,
             }
    ax.set_xlabel('x', fontdict=font2)
    ax.set_ylabel('y', fontdict=font2)
    ax.set_zlabel('z', fontdict=font2)
    #plt.savefig('results/Accelerometer_climb_descent_stairs/DBSCAN_0.010_5.png', dpi=1024)
    plt.show()


import re
import traceback
import asyncio
from concurrent.futures._base import CancelledError
from discord.errors import Forbidden, NotFound, HTTPException
from discord import User
from ..exceptions import PermissionError, CancelledPrompt, Message, CancelCommand, RobloxAPIError, RobloxDown, Error # pylint: disable=redefined-builtin, import-error
from ..structures import Bloxlink, Args, Command, Arguments, Response # pylint: disable=import-error, no-name-in-module
from ..constants import DEFAULTS, RELEASE # pylint: disable=import-error, no-name-in-module
from ..secrets import TOKEN # pylint: disable=import-error, no-name-in-module
from config import BOTS # pylint: disable=import-error, no-name-in-module, no-name-in-module

fetch = Bloxlink.get_module("utils", attrs=["fetch"])
get_guild_value = Bloxlink.get_module("cache", attrs=["get_guild_value"])

BOT_ID = BOTS[RELEASE]
COMMANDS_URL = f"https://discord.com/api/v8/applications/{BOT_ID}/commands" if RELEASE != "LOCAL" else f"https://discord.com/api/v8/applications/{BOT_ID}/guilds/842537684785823755/commands"
PREFIX = DEFAULTS["prefix"]

@Bloxlink.module
class Commands(Bloxlink.Module):
    def __init__(self):
        self.commands = {}

    async def __loaded__(self):
        """sync the slash commands"""

        slash_commands = [self.slash_command_to_json(c) for c in self.commands.values() if c.slash_enabled]
        text, response = await fetch(COMMANDS_URL, "PUT", json=slash_commands, headers={"Authorization": f"Bot {TOKEN}"}, raise_on_failure=False)

        if response.status == 200:
            Bloxlink.log("Successfully synced Slash Commands")
        else:
            print(slash_commands, flush=True)
            print(response.status, text, flush=True)

    async def redirect_command(self, command_name, ):
        pass

    async def command_checks(self, command, response, guild_data, author, channel, CommandArgs, message=None, guild=None, subcommand_attrs=None, slash_command=False):
        channel_id = str(channel.id) if channel else None
        dm = not bool(guild)
        subcommand_attrs = subcommand_attrs or {}

        if guild:
            if not command.bypass_channel_perms:
                ignored_channels = guild_data.get("ignoredChannels", {})
                disabled_commands = guild_data.get("disabledCommands", {})

                author_perms = author.guild_permissions

                if guild.owner_id != author.id and not (author_perms.manage_guild or author_perms.administrator):
                    if ignored_channels.get(channel_id) or (channel.category and ignored_channels.get(str(channel.category.id))):
                        await response.send(f"The server admins have **disabled** all commands in channel {channel.mention}.", dm=True, hidden=True, strict_post=True, no_dm_post=True)

                        if message:
                            try:
                                await message.delete()
                            except (Forbidden, NotFound):
                                pass

                        raise CancelCommand

                    if command.name in disabled_commands.get("global", []):
                        await response.send(f"The server admins have **disabled** the command `{command.name}` globally.", dm=True, hidden=True, strict_post=True, no_dm_post=True)

                        if message:
                            try:
                                await message.delete()
                            except (Forbidden, NotFound):
                                pass

                        raise CancelCommand

                    elif disabled_commands.get("channels", {}).get(channel_id, {}).get(command.name):
                        await response.send(f"The server admins have **disabled** the command `{command.name}` in channel {channel.mention}.", dm=True, hidden=True, strict_post=True, no_dm_post=True)

                        if message:
                            try:
                                await message.delete()
                            except (Forbidden, NotFound):
                                pass

                        raise CancelCommand

        if not (command.dm_allowed or guild):
            await response.send("This command does not support DM; please run it in a server.", hidden=True)
            raise CancelCommand

        try:
            await command.check_permissions(author, guild, dm=dm, **subcommand_attrs)
        except PermissionError as e:
            if subcommand_attrs.get("allow_bypass"):
                CommandArgs.has_permission = False
            elif command.permissions.allow_bypass:
                CommandArgs.has_permission = False
            else:
                await response.error(e, hidden=True)
                raise CancelCommand

        except Message as e:
            message_type = "send" if e.type == "info" else e.type
            response_fn = getattr(response, message_type, response.send)

            if e.message:
                await response_fn(e, hidden=True)

            if subcommand_attrs.get("allow_bypass"):
                CommandArgs.has_permission = False
            elif command.permissions.allow_bypass:
                CommandArgs.has_permission = False
            else:
                raise CancelCommand

        else:
            CommandArgs.has_permission = True

    async def execute_command(self, command, fn, response, CommandArgs, author, channel, arguments, guild_data=None, guild=None, message=None, after_text=None, slash_command=False):
        my_permissions = guild and guild.me.guild_permissions

        try:
            await arguments.initial_command_args(after_text)

            CommandArgs.add(prompt=arguments.prompt)
            response.prompt = arguments.prompt # pylint: disable=no-member

            await fn(CommandArgs)
        except PermissionError as e:
            if e.message:
                await response.error(e)
            else:
                await response.error("I'm missing permissions for this command. Please give me the appropriate permissions.")
        except Forbidden as e:
            if e.args:
                await response.error(e)
            else:
                await response.error("I'm missing permissions for this command. Please give me the appropriate permissions.")
        except CancelledPrompt as e:
            arguments.cancelled = True

            if e.message:
                text = f"**Cancelled prompt**: {e}"
            else:
                text = f"**Cancelled prompt.**"

            if ((e.type == "delete" and not e.dm) and guild_data.get("promptDelete", DEFAULTS.get("promptDelete"))):
                if my_permissions and my_permissions.manage_messages:
                    if message:
                        try:
                            await message.delete()
                        except (Forbidden, NotFound):
                            pass

                    if slash_command and response.first_slash_command:
                        await response.first_slash_command.delete()
                else:
                    await response.send(text, dm=e.dm, no_dm_post=True)
            else:
                if my_permissions and my_permissions.manage_messages:
                    if slash_command and response.first_slash_command:
                        await response.first_slash_command.edit(content="**_Command finished._**", view=None, embed=None)

                await response.send(text, dm=e.dm, no_dm_post=True)

        except Message as e:
            message_type = "send" if e.type == "send" else e.type
            response_fn = getattr(response, message_type, response.send)

            if e.message:
                await response_fn(e, hidden=e.hidden)
            else:
                await response_fn("This command closed unexpectedly.")
        except Error as e:
            if e.message:
                await response.error(e, hidden=e.hidden)
            else:
                await response.error("This command has unexpectedly errored.")
        except CancelCommand as e:
            if e.message:
                await response.send(e, mention_author=True)
        except NotImplementedError:
            await response.error("The option you specified is currently not implemented, but will be coming soon!")
        except CancelledError:
            pass
        except Exception as e:
            """
            error_id = Bloxlink.error(e, command=command_name, user=(author.id, str(author)), guild=guild and f"id:{guild.id}")

            if error_id:
                await response.error("We've experienced an unexpected error. You may report this "
                                        f"error with ID `{error_id}` in our support server: {SERVER_INVITE}.")
            else:
                await response.error("An unexpected error occured.")
            """

            await response.error("This command has unexpectedly errored.")
            Bloxlink.error(traceback.format_exc(), title=f"Error source: {command.name}.py\n{f'Guild ID: {guild.id}' if guild else ''}")

        finally:
            delete_messages = response.delete_message_queue
            prompt_messages = arguments.messages
            bot_responses   = response.bot_responses

            if arguments.dm_post and not response.webhook_only:
                if arguments.cancelled:
                    content = f"{author.mention}, **this DM prompt has been cancelled.**"
                else:
                    content = f"{author.mention}, **this DM prompt has finished.**"

                try:
                    await arguments.dm_post.edit(content=content)
                except NotFound:
                    pass

            if my_permissions and my_permissions.manage_messages:
                delete_options = await get_guild_value(guild, ["promptDelete", DEFAULTS.get("promptDelete")], ["deleteCommands", DEFAULTS.get("deleteCommands")])

                delete_commands_after = delete_options["deleteCommands"]
                prompt_delete         = delete_options["promptDelete"]

                if prompt_delete:
                    if prompt_messages:
                        delete_messages += prompt_messages
                else:
                    delete_messages = [] # we'll populate this with the command information

                if delete_commands_after:
                    if message:
                        delete_messages.append(message.id)

                    delete_messages += bot_responses

                    await asyncio.sleep(delete_commands_after)

                if delete_messages:
                    if slash_command and response.first_slash_command and not arguments.cancelled:
                        await response.first_slash_command.edit(content="_**Command finished.**_", embed=None, view=None)

                    try:
                        await channel.purge(limit=100, check=lambda m: (m.id in delete_messages) or (delete_commands_after and re.search(f"^[</{command.name}:{slash_command}>]", m.content)))
                    except (Forbidden, HTTPException):
                        pass

    async def parse_message(self, message, guild_data=None):
        guild = message.guild
        content = message.content
        author = message.author
        channel = message.channel

        guild_id   = guild and str(guild.id)

        client_match = re.search(f"^(<@!?{self.client.user.id}>)", content)
        check = (content[:len(PREFIX)].lower() == PREFIX.lower() and PREFIX) or client_match and client_match.group(0)

        if check:
            after = content[len(check):].strip()
            args = after.split(" ")
            command_name = args[0] and args[0].lower()
            del args[0]

            if command_name:
                for index, command in self.commands.items():
                    if index == command_name or command_name in command.aliases:
                        if guild:
                            if isinstance(author, User):
                                try:
                                    author = await guild.fetch_member(author.id)
                                except NotFound:
                                    raise CancelCommand

                        # guild_data = guild_data or (guild and (await self.r.table("guilds").get(guild_id).run() or {"id": guild_id})) or {}
                        guild_data = {"id": guild_id}

                        fn = command.fn
                        subcommand_attrs = {}
                        subcommand = False

                        if args:
                            # subcommand checking
                            subcommand = command.subcommands.get(args[0])
                            if subcommand:
                                fn = subcommand
                                subcommand_attrs = getattr(fn, "__subcommandattrs__", None)
                                del args[0]

                        after = args and " ".join(args) or ""

                        CommandArgs = Args(
                            command_name = index,
                            real_command_name = command_name,
                            message = message,
                            channel = message.channel,
                            author  = message.author,
                            guild = message.guild,
                            guild_data = guild_data,
                            flags = {},
                            has_permission = False,
                            command = command,
                            slash_command = False
                        )

                        if getattr(fn, "__flags__", False):
                            flags, flags_str = command.parse_flags(after)
                            content = content.replace(flags_str, "")
                            message.content = content
                            after = after.replace(flags_str, "")
                            CommandArgs.flags = flags

                        response = Response(CommandArgs, author, channel, guild, message, slash_command=False)

                        CommandArgs.add(response=response)

                        await self.command_checks(command, response, guild_data, author, channel, CommandArgs, message, guild, subcommand_attrs, slash_command=False)

                        arguments = Arguments(CommandArgs, author, channel, command, guild, message, subcommand=(subcommand, subcommand_attrs) if subcommand else None, slash_command=False)

                        await self.execute_command(command, fn, response, CommandArgs, author, channel, arguments, guild_data, guild, message, after, False)

                        break

    def slash_command_to_json(self, command):
        type_enums = {
            "string":  3,
            "number":  4,
            "boolean": 5,
            "user":    6,
            "channel": 7,
            "role":    8
        }

        def prompts_to_json(prompts):
            def single_prompt(prompt):
                option = {
                    "name": prompt["name"],
                    "type": type_enums.get(prompt.get("type", "string"), type_enums.get("string")),
                    "description": prompt.get("slash_desc", prompt["prompt"]),
                    "required": not (prompt.get("optional") or prompt.get("slash_optional")),
                    "choices": [{
                        "name": choice,
                        "value": choice

                    } for choice in prompt.get("choices", []) if len(prompt.get("choices", [])) <= 25 ]
                }

                return option

            if isinstance(prompts, dict):
                return single_prompt(prompts)
            else:
                options = []

                for prompt in prompts:
                    options.append(single_prompt(prompt))

                return options

        if command.slash_enabled:
            json = {
                "name": command.name,
                "description": command.description,
                "options": []
            }

            if command.subcommands:
                for subcommand_name, subcommand_fn in command.subcommands.items():
                    subcommand_attrs = getattr(subcommand_fn, "__subcommandattrs__")
                    subcommand_options = subcommand_attrs.get("slash_args") or subcommand_attrs.get("arguments")

                    json["options"].append({
                        "name": subcommand_name,
                        "type": 1,
                        "description": subcommand_attrs.get("slash_desc", subcommand_fn.__doc__),
                        "options": prompts_to_json(subcommand_options) if subcommand_options else None,
                    })

            elif command.slash_args or command.arguments:
                json["options"] += prompts_to_json(command.slash_args or command.arguments)

            return json

    def new_command(self, command_structure, addon=None):
        c = command_structure()
        command = Command(c)

        Bloxlink.log(f"Adding command {command.name}")

        if hasattr(c, "__setup__"):
            self.loop.create_task(c.__setup__())

        for attr_name in dir(command_structure):
            attr = getattr(c, attr_name)

            if callable(attr) and hasattr(attr, "__issubcommand__"):
                command.subcommands[attr_name] = attr

        self.commands[command.name] = command
        command.addon = addon

        self.loop.create_task(self.inject_command(command))

        return command_structure

    async def inject_command(self, command):
        subcommands = []

        if command.subcommands:
            for subcommand_name, subcommand in command.subcommands.items():
                subcommand_description = subcommand.__doc__ or "N/A"
                subcommands.append({"id": subcommand_name, "description": subcommand_description})


import os
import re
import numpy as np
from PIL import Image
from .annotation import read_json, output_json

class ImageManager:

    def __init__(self, img, filename):
        """initiate module"""
        self._filename = filename
        self._img = Image.open(img)
        self._rects = []
        self._load_rects()
        self._resized_ratio_w = 1
        self._resized_ratio_h = 1

    def _load_rects(self):
        rects_json = read_json(self._filename)
        if rects_json:
            self._rects = rects_json

    def get_img(self):
        """get the image object

        Returns:
            img(PIL.Image): the image object.
        """
        return self._img

    def get_rects(self):
        """get the rects

        Returns:
            rects(list): the bounding boxes of the image.
        """
        return self._rects

    def resizing_img(self, max_height=700, max_width=700):
        """resizing the image by max_height and max_width.

        Args:
            max_height(int): the max_height of the frame.
            max_width(int): the max_width of the frame.
        Returns:
            resized_img(PIL.Image): the resized image.
        """
        resized_img = self._img.copy()
        if resized_img.height > max_height:
            ratio = max_height / resized_img.height
            resized_img = resized_img.resize(
                (int(resized_img.width * ratio), int(resized_img.height * ratio))
            )
        if resized_img.width > max_width:
            ratio = max_width / resized_img.width
            resized_img = resized_img.resize(
                (int(resized_img.width * ratio), int(resized_img.height * ratio))
            )

        self._resized_ratio_w = self._img.width / resized_img.width
        self._resized_ratio_h = self._img.height / resized_img.height
        return resized_img

    def _resize_rect(self, rect):
        resized_rect = {}
        resized_rect["left"] = rect["left"] / self._resized_ratio_w
        resized_rect["width"] = rect["width"] / self._resized_ratio_w
        resized_rect["top"] = rect["top"] / self._resized_ratio_h
        resized_rect["height"] = rect["height"] / self._resized_ratio_h
        if "label" in rect:
            resized_rect["label"] = rect["label"]
        return resized_rect

    def get_resized_rects(self):
        """get resized the rects according to the resized image.

        Returns:
            resized_rects(list): the resized bounding boxes of the image.
        """
        return [self._resize_rect(rect) for rect in self._rects]

    def _chop_box_img(self, rect):
        rect["left"] = int(rect["left"] * self._resized_ratio_w)
        rect["width"] = int(rect["width"] * self._resized_ratio_w)
        rect["top"] = int(rect["top"] * self._resized_ratio_h)
        rect["height"] = int(rect["height"] * self._resized_ratio_h)
        left, top, width, height = (
            rect["left"],
            rect["top"],
            rect["width"],
            rect["height"],
        )

        raw_image = np.asarray(self._img).astype("uint8")
        prev_img = np.zeros(raw_image.shape, dtype="uint8")
        prev_img[top : top + height, left : left + width] = raw_image[
            top : top + height, left : left + width
        ]
        prev_img = prev_img[top : top + height, left : left + width]
        label = ""
        if "label" in rect:
            label = rect["label"]
        return (Image.fromarray(prev_img), label)

    def init_annotation(self, rects):
        """init annotation for current rects.

        Args:
            rects(list): the bounding boxes of the image.
        Returns:
            prev_img(list): list of preview images with default label.
        """
        self._current_rects = rects
        return [self._chop_box_img(rect) for rect in self._current_rects]

    def set_annotation(self, index, label):
        """set the label of the image.

        Args:
            index(int): the index of the list of bounding boxes of the image.
            label(str): the label of the bounding box
        """
        self._current_rects[index]["label"] = label

    def save_annotation(self):
        """output the xml annotation file."""
        # output_xml(self._filename, self._img, self._current_rects)
        output_json(self._filename, self._img, self._current_rects)


from django import forms
from django.shortcuts import get_object_or_404, redirect, render
from django.utils import timezone

from .models import Post
from blog import *

from .forms import PostForm, ContactForm
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse

# Create your views here.

def post_list(request):
    posts = Post.objects.all()
    return render(request, 'blog/post_lists.html', {'posts': posts})

# POST METHOD

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blog/post_detail.html', {'post': post})

def post_create(request):
    if request.method == 'POST':
        form = PostForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.date = timezone.now()
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = PostForm()
    return render(request, 'blog/post_edit.html', {'form': form})

def post_edit(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = PostForm(request.POST, instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.publish_date = timezone.now()
            post.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = PostForm(instance=post)
    return render(request, 'blog/post_edit.html', {'form': form, 'pk': pk})

def post_delete(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if post:
        post.delete()
        posts = Post.objects.all()
    return render(request, 'blog/post_lists.html', {'posts': posts})

# SEND EMAIL

def contact(request):
    if request.method == "POST":
        form = ContactForm(request.POST)
        if form.is_valid():
            subject = "Title of message"
            body = {
                'first_name': form.cleaned_data['first_name'],
                'last_name': form.cleaned_data['last_name'],
                'email_address': form.cleaned_data['email_address'],
                'message': form.cleaned_data['message']
            }
            message = '\n'.join(body.values())
            try:
                send_mail(subject, message, 'from@gmail.com',
                          ['to_help@gmail.com'])

            except BadHeaderError:
                return (HttpResponse('Find incorrect header'))
            return redirect('post_list')
    form = ContactForm()
    return render(request, 'blog/contact.html', {'form': form})


import json
from typing import Dict, List, Tuple
import re

from chatbot.system_messages.system import (
    function_res_agent,
    base_system_message,
    )
from chatbot.agents.function_mapper import FunctionMapper
from chatbot.agents.function_call_agent import FunctionCallAgent
from chatbot.agents.base_agent import RegularAgent
from chatbot.agents.function_response_agent import function_response_agent
from chatbot.agents.anthropic_base import AnthropicAgent

function_mapper = FunctionMapper()
fc = FunctionCallAgent()
ra = RegularAgent()
aa = AnthropicAgent()
function_call_agent = fc.call
regular_agent = ra.call
anthropic_agent = aa.call

def get_command(prompt):
    """Extract command from prompt if it exists"""
    match = re.search(r"/\w+", prompt)
    if match:
        return match.group(0)
    return None

def call_function(function_name, function_args):
    if function_name not in function_mapper.function_map:
        raise Exception(f"Function {function_name} not found.")
    if function_args is None:
        return function_mapper.function_map[function_name]()
    return function_mapper.function_map[function_name](*function_args.values())

def run_conversation(prompt: str, conversation: List[Dict[str, str]]) -> Tuple[str, List[Dict[str, str]], int, float]:
    agents = function_mapper.agents
    command = get_command(prompt)
    if command in agents:
        agent_properties = agents[command]
        prompt = prompt[agent_properties["command_length"]:].strip()
        if agent_properties["is_function"]:
            print(agent_properties["name"])
            response = function_call_agent(
                prompt=prompt,
                conversation=conversation,
                system_message=agent_properties["agent"].system_message,
                function_params=agent_properties["agent"].function_params,
                temperature=agent_properties["agent"].temperature,
                top_p=agent_properties["agent"].top_p,
                frequency_penalty=agent_properties["agent"].frequency_penalty,
                model=agent_properties["agent"].model,
            )
            message = response[0]

        else:
            print(agent_properties["name"])
            response = regular_agent(
                prompt=prompt,
                conversation=conversation,
                system_message=agent_properties["system_message"],
                stream=True,
            )
            return response

    elif command == "/claude":
        print('Anthropic')
        prompt = prompt[len(command):].strip()
        response = anthropic_agent(
            prompt=prompt,
            conversation=conversation,
            stream=True
            )
        return response
    else:
        print('Personal Assistant')
        response = regular_agent(
            prompt=prompt,
            conversation=conversation,
            system_message=base_system_message,
            stream=True
            )
        return response

    if message.get("function_call"):
        function_name = message["function_call"]["name"]
        function_args = json.loads(message["function_call"]["arguments"])
        print(f"Function name: {function_name}")
        print(f"Function arguments: {function_args}")

        function_response = call_function(function_name, function_args)
        if function_name in function_mapper.functions_that_append_to_conversation:
            print(f"Function response: {function_response}")
            conversation.append({
                "role": "assistant",
                "content": function_response,  # directly add function response to the conversation
            })
            return function_response
        else:
            print(f"Function response: {function_response}")
            second_response = function_response_agent(
                prompt=prompt,
                system_message=function_res_agent,
                function_name=function_name,
                function_response=function_response,
                message=message,
                stream=True
                )
            return second_response


import unittest
import requests

class myappTestCase(unittest.TestCase):
  """Test access to myapp which is written in Flask and python"""

  def test_root_page(self):
    resp = requests.get('http://10.0.2.182:5000/')
    root_text = resp.text
    self.assertEqual(root_text, 'Hello world')

unittest.main()


from ..backend import Backend
from ..result import UnfoldingResult
import ROOT
import numpy as np
import root_numpy
import math

class SVDBackend(Backend):
    def __init__(self, regularization_param, bins_min, bins_max):
        """
        Parameters
        ----------
        regularization_param : number
            Regularization parameter to use in the unfolding method
        bins_min : number
            Lower limit of the histograms' bins
        bins_max : number
            Upper limit of the histograms' bins
        """
        self.kreg = regularization_param
        self.bins_min = bins_min
        self.bins_max = bins_max

    def solve(self, data, statcov, xini, bini, R):
        bins_min = self.bins_min
        bins_max = self.bins_max
        # Transform the reponse matrix from probabilities to events
        # R = np.multiply(xini, R, where=xini != 0)
        # Transform python arrays to ROOT TH1D
        datar = ROOT.TH1D("data", "data", data.shape[0], bins_min, bins_max)
        root_numpy.array2hist(data, datar)
        xinir = ROOT.TH1D("xini", "xini", xini.shape[0], bins_min, bins_max)
        root_numpy.array2hist(xini, xinir)
        binir = ROOT.TH1D("bini", "bini", bini.shape[0], bins_min, bins_max)
        root_numpy.array2hist(bini, binir)
        Adet = ROOT.TH2D(
            "R",
            "R",
            xini.shape[0],
            bins_min,
            bins_max,
            bini.shape[0],
            bins_min,
            bins_max,
        )
        root_numpy.array2hist(R, Adet)
        # Compute covariance matrix assuming the amount of events in each bin
        # come from a Poisson distribution
        statcovr = ROOT.TH2D(
            "statcov",
            "statcov",
            data.shape[0],
            bins_min,
            bins_max,
            data.shape[0],
            bins_min,
            bins_max,
        )
        root_numpy.array2hist(statcov, statcovr)
        # Create TSVDUnfold object and initialise
        tsvdunf = ROOT.TSVDUnfold(datar, statcovr, binir, xinir, Adet)
        # It is possible to normalise unfolded spectrum to unit area
        tsvdunf.SetNormalize(ROOT.kFALSE)
        # Perform the unfolding with regularisation parameter kreg = self.kreg
        # - the larger kreg, the finer grained the unfolding, but the more fluctuations occur
        # - the smaller kreg, the stronger is the regularisation and the bias
        unfres = tsvdunf.Unfold(self.kreg)
        # Get the distribution of the d to cross check the regularization
        # - choose kreg to be the point where |d_i| stop being statistically significantly >>1
        ddist = tsvdunf.GetD()
        self.kreg_distribution = root_numpy.hist2array(ddist)
        # Get the distribution of the singular values
        svdist = tsvdunf.GetSV()
        # Compute the error matrix for the unfolded spectrum using toy MC
        # using the measured covariance matrix as input to generate the toys
        # 100 toys should usually be enough
        # The same method can be used for different covariance matrices separately.
        ustatcov = tsvdunf.GetUnfoldCovMatrix(statcovr, 100)
        # Now compute the error matrix on the unfolded distribution originating
        # from the finite detector matrix statistics
        uadetcov = tsvdunf.GetAdetCovMatrix(100)
        # Sum up the two (they are uncorrelated)
        ustatcov.Add(uadetcov)
        # Get the computed regularized covariance matrix (always corresponding to total uncertainty passed in constructor) and add uncertainties from finite MC statistics.
        utaucov = tsvdunf.GetXtau()
        utaucov.Add(uadetcov)
        # Get the computed inverse of the covariance matrix
        uinvcov = tsvdunf.GetXinv()
        # Convert result to a python array
        result = root_numpy.hist2array(unfres)
        error = root_numpy.hist2array(utaucov)
        # the std to plot is just:
        # np.sqrt(np.diagonal(error))
        return UnfoldingResult(result, error)


from asyncio import Queue
from typing import Dict, List

from app import schemas
from app.cruds import UserAccountCRUD, MessageCRUD
from app.schemas import RegisterResponse, AuthResponse, Dialog, Message, User, CheckSessionResponse, LoginRequest, \
    RegisterRequest, BaseRequest, FindUsersRequest, GetDialogsRequest, SendMessageRequest, GetDialogMessagesRequest, \
    SendMessageResponse, GetDialogsResponse, GetDialogMessagesResponse, FindUsersResponse, LogoutResponse, \
    LogoutRequest, GetUserByIdResponse, GetUserByIdRequest

class AuthMiddleWare:

    def __init__(self, active_users: Dict[str, schemas.User]):
        self._active_users: Dict[str, schemas.User] = active_users

    def check_session(self, request: BaseRequest) -> CheckSessionResponse:
        session = request.session
        if session in self._active_users:
            return CheckSessionResponse(success=True, session=session)
        return CheckSessionResponse(success=False, session=session)

class AuthService:

    def __init__(self, user_account_crud: UserAccountCRUD,
                 active_users: Dict[str, schemas.User]):
        self._user_account_crud = user_account_crud
        self._active_users: Dict[str, schemas.User] = active_users

    def register(self, request: RegisterRequest) -> RegisterResponse:
        try:
            user = self._user_account_crud.create(request.username)
        except BaseException as e:
            response = RegisterResponse(success=False, error=str(e))
        else:
            response = RegisterResponse(user=user)
        return response

    def auth(self, request: LoginRequest) -> AuthResponse:
        username = request.username
        try:
            user = self._user_account_crud.get_by_username(username)
        except BaseException as e:
            response = AuthResponse(success=False, error=str(e))
        else:
            session = username + '_session'
            self._active_users[session] = user
            response = AuthResponse(user=user, session=session)
        return response

    def logout(self, request: LogoutRequest) -> LogoutResponse:
        try:
            session = request.session
            self._active_users.pop(session)
        except KeyError:
            return LogoutResponse(success=False, error='wrong session')
        else:
            return LogoutResponse(success=True)

class ChatService:

    def __init__(self, user_account_crud: UserAccountCRUD, message_crud: MessageCRUD, finding_limit: int = 10):
        self._user_account_crud = user_account_crud
        self._message_crud = message_crud
        self._finding_limit = finding_limit

    def get_user_by_id(self, request: GetUserByIdRequest) -> GetUserByIdResponse:
        user_id = request.user_id
        user = self._user_account_crud.get_by_id(user_id)
        return GetUserByIdResponse(user=user)

    def find_users(self, request: FindUsersRequest) -> FindUsersResponse:
        username_part = request.username_part
        found_users = self._user_account_crud.find_by_username_part(username_part, self._finding_limit)
        return FindUsersResponse(users=found_users)

    def get_dialog_messages(self, request: GetDialogMessagesRequest) -> GetDialogMessagesResponse:
        user_id = request.user_id
        with_user_id = request.with_user_id
        dialog_messages = self._message_crud.get_user_dialog(user_id, with_user_id)
        return GetDialogMessagesResponse(messages=dialog_messages)

    def get_dialogs(self, request: GetDialogsRequest) -> GetDialogsResponse:
        user_id = request.user_id
        user = self._user_account_crud.get_by_id(user_id)
        dialogs_users_ids = self._message_crud.get_user_dialogs_users_ids(user_id)
        dialogs_users = [self._user_account_crud.get_by_id(dialog_user_id) for dialog_user_id in dialogs_users_ids]
        return GetDialogsResponse(dialogs=[Dialog(user=user, with_user=dialog_user) for dialog_user in dialogs_users])

    def send_message(self, request: SendMessageRequest) -> SendMessageResponse:
        message_text: str = request.message_text
        from_user_id: int = request.from_user_id
        to_user_id: int = request.to_user_id
        message = self._message_crud.create(message_text, from_user_id, to_user_id)
        return SendMessageResponse(message=message)

class NewMessagesStorage:

    def __init__(self):
        self._new_messages: Dict[Dialog, Queue] = {}

    def add(self, dialog: Dialog, message: Message):
        pass

    def get_new_messages_count(self, dialog: Dialog) -> int:
        try:
            new_messages_count = len(self._new_messages[dialog])
        except KeyError:
            new_messages_count = 0
        return new_messages_count

    def get_new_messages(self, dialog: Dialog) -> List[Message]:
        pass


"""Linux PWM driver sysfs interface"""

import os
__author__ = 'Scott Ellis'
__version__ = '1.0'
__license__ = 'New BSD'
__copyright__ = 'Copyright (c) 2016 Scott Ellis'

from types import (
    TracebackType,
)

from typing import (
    Optional,
    Type,
)

class PWM(object):
    """
    A class to work with the Linux PWM driver sysfs interface
    """

    def __init__(self, channel: int = 0, chip: int = 0) -> None:
        """ Specify channel and chip when creating an instance
        The Linux kernel driver exports a sysfs interface like this
            /sys/class/pwm/pwmchip<chip>/pwm<channel>
        A <chip> can have multiple <channels>.
        The channel and chip are determined by the kernel driver.
        For example the two PWM timers from the RPi kernel driver
        show up like this
            /sys/class/pwm/pwmchip0/pwm0
            /sys/class/pwm/pwmchip0/pwm1
        To use the RPi timers create instances this way
            pwm0 = PWM(0) or PWM(0,0)
            pwm1 = PWM(1) or PWM(1,0)
        """
        self._channel = channel
        self._chip = chip
        self.base = '/sys/class/pwm/pwmchip{:d}'.format(self._chip)
        self.path = self.base + '/pwm{:d}'.format(self._channel)
        if not os.path.isdir(self.base):
            raise FileNotFoundError('Directory not found: ' + self.base)

    # enable class as a context manager
    def __enter__(self) -> 'PWM':
        self.export()
        return self

    def __exit__(self,
            exc_type: Optional[Type[BaseException]],
            exc_value: Optional[BaseException],
            traceback: Optional[TracebackType]) -> None:
        self.enable = False
        self.inversed = False
        self.unexport()
        return

    def export(self) -> None:
        """Export the channel for use through the sysfs interface.
        Required before first use.
        """
        if not os.path.isdir(self.path):
            with open(self.base + '/export', 'w') as f:
                f.write('{:d}'.format(self._channel))

    def unexport(self) -> None:
        """Unexport the channel.
        The sysfs interface is no longer usable until it is exported again.
        """
        if os.path.isdir(self.path):
            with open(self.base + '/unexport', 'w') as f:
                f.write('{:d}'.format(self._channel))

    @property
    def channel(self) -> int:
        """The channel used by this instance.
        Read-only, set in the constructor.
        """
        return self._channel

    @property
    def chip(self) -> int:
        """The chip used by this instance.
        Read-only, set in the constructor.
        """
        return self._chip

    @property
    def period(self) -> int:
        """The period of the pwm timer in nanoseconds."""
        with open(self.path + '/period', 'r') as f:
            value = f.readline().strip()

        return int(value)

    @period.setter
    def period(self, value: int) -> None:
        with open(self.path + '/period', 'w') as f:
            f.write('{:d}'.format(value))

    @property
    def duty_cycle(self) -> int:
        """The duty_cycle (the ON pulse) of the timer in nanoseconds."""
        with open(self.path + '/duty_cycle', 'r') as f:
            value = f.readline().strip()

        return int(value)

    @duty_cycle.setter
    def duty_cycle(self, value: int) -> None:
        with open(self.path + '/duty_cycle', 'w') as f:
            f.write('{:d}'.format(value))

    @property
    def enable(self) -> bool:
        """Enable or disable the timer, boolean"""
        with open(self.path + '/enable', 'r') as f:
            value = f.readline().strip()

        return True if value == '1' else False

    @enable.setter
    def enable(self, value: bool) -> None:
        with open(self.path + '/enable', 'w') as f:
            if value:
                f.write('1')
            else:
                f.write('0')

    @property
    def inversed(self) -> bool:
        """normal polarity or inversed, boolean"""
        with open(self.path + '/polarity', 'r') as f:
            value = f.readline().strip()

        return True if value == 'inversed' else False

    @inversed.setter
    def inversed(self, value: bool) -> None:
        with open(self.path + '/polarity', 'w') as f:
            if value:
                f.write('inversed')
            else:
                f.write('normal')


import pygame
from pygame.mixer import Sound, music
from pygine import globals

MUSIC_PATH = ""
SOUND_PATH = ""
current_song = ""

def load_sound_paths():
    global MUSIC_PATH
    global SOUND_PATH
    if globals.on_cpi:
        MUSIC_PATH = '/home/cpi/games/Python/diner-mafia/pygine/assets/music/'
        SOUND_PATH = '/home/cpi/games/Python/diner-mafia/pygine/assets/sounds/'
    else:
        MUSIC_PATH = 'pygine/assets/music/'
        SOUND_PATH = 'pygine/assets/sounds/'

    pygame.mixer.init()
    music.set_volume(0.80)

def play_song(filename):
    global current_song
    global MUSIC_PATH
    if filename != current_song:
        music.load(MUSIC_PATH + filename)
        music.play(-1)
        current_song = filename

def play_sound(filename):
    global SOUND_PATH
    Sound(SOUND_PATH + filename).play()


from gensim.models import Word2Vec
import warnings
import nltk
warnings.simplefilter(action='ignore', category=FutureWarning)

file_dir = 'dir/'
file = 'file'

print("Retrieving text")

def get_file(file):
    with open(file, 'r', encoding = 'utf-8') as txt:
        txt = txt.read()
        return txt

text = get_file(file_dir+file+'.txt')
print("Text retrieved")

print("Tokenizing sentences")
all_sentences = nltk.sent_tokenize(text)
print("All sentences ready")

print("Tokenizing words")
all_words = [nltk.word_tokenize(sent) for sent in all_sentences]
print("All words ready")

print("Training model")
model1 = Word2Vec(all_words, min_count = 3,
                  size = 100, window = 5, workers = 8)

print("Model trained")
print("Saving model")
model1.save(file+'_w2v.model')
print(f"Model ready: {file}_w2v.model")


#!/usr/bin/env python3

import rospy
import math
import matplotlib.pyplot as plt
from geometry_msgs.msg import Twist
from turtlesim.msg import Pose

class TurtleBotController:
    def __init__(self, node, Kp, Ki, Kd):
        self.goal_x = 8.0
        self.goal_y = 3.0
        self.turtle_x = 5.0
        self.turtle_y = 5.0
        self.turtle_theta = 0.0
        self.linear_velocity = 0.0
        self.angular_velocity = 0.0
        self.Kp = Kp
        self.Ki = Ki
        self.Kd = Kd
        self.prev_error = 0.0
        self.integral = 0.0

        self.time = []
        self.position_error = []
        self.linear_velocities = []
        self.angular_velocities = []

        self.node = node
        self.pose_subscriber = rospy.Subscriber('/turtle1/pose', Pose, self.pose_callback)
        self.velocity_publisher = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
        self.rate = rospy.Rate(10)

    def pose_callback(self, data):
        self.turtle_x = data.x
        self.turtle_y = data.y
        self.turtle_theta = data.theta

    def update_error(self):
        distance = math.sqrt((self.goal_x - self.turtle_x) ** 2 + (self.goal_y - self.turtle_y) ** 2)
        angle_to_goal = math.atan2(self.goal_y - self.turtle_y, self.goal_x - self.turtle_x)
        error = angle_to_goal - self.turtle_theta
        return distance, error

    def pid_control(self):
        distance, error = self.update_error()

        self.integral += error
        derivative = error - self.prev_error

        self.linear_velocity = self.Kp * distance
        self.angular_velocity = self.Kp * error + self.Ki * self.integral + self.Kd * derivative

        self.prev_error = error

    def run(self):
        while not rospy.is_shutdown():
            self.pid_control()

            self.time.append(rospy.Time.now().to_sec())
            self.position_error.append(self.linear_velocity)
            self.linear_velocities.append(self.linear_velocity)
            self.angular_velocities.append(self.angular_velocity)

            twist_msg = Twist()
            twist_msg.linear.x = self.linear_velocity
            twist_msg.angular.z = self.angular_velocity
            self.velocity_publisher.publish(twist_msg)

            self.rate.sleep()

def plot_performance(gains_list, time_list, position_error_list, linear_velocities_list, angular_velocities_list):
    plt.figure(figsize=(12, 6))

    plt.subplot(2, 1, 1)
    for i, gains in enumerate(gains_list):
        plt.plot(time_list[i], position_error_list[i], label='Kp={}, Ki={}, Kd={}'.format(*gains))
    plt.xlabel('Time (s)')
    plt.ylabel('Position Error')
    plt.grid(True)
    plt.legend()

    plt.subplot(2, 1, 2)
    for i, gains in enumerate(gains_list):
        plt.plot(time_list[i], linear_velocities_list[i], label='Kp={}, Ki={}, Kd={}'.format(*gains))
    plt.xlabel('Time (s)')
    plt.ylabel('Velocities')
    plt.grid(True)
    plt.legend()

    plt.tight_layout()
    plt.show()

if __name__ == "__main__":
    try:
        rospy.init_node('turtle_pid_controller', anonymous=True)
        gains_list = [(1.0, 0.1, 0.05), (1.5, 0.2, 0.1), (0.8, 0.05, 0.02)]
        controller_list = []
        time_list = []
        position_error_list = []
        linear_velocities_list = []
        angular_velocities_list = []

        for gains in gains_list:
            controller = TurtleBotController(rospy, *gains)
            controller_list.append(controller)

        for controller in controller_list:
            time_data = []
            position_error_data = []
            linear_velocities_data = []
            angular_velocities_data = []

            while not rospy.is_shutdown():
                controller.pid_control()

                time_data.append(rospy.Time.now().to_sec())
                position_error_data.append(controller.linear_velocity)
                linear_velocities_data.append(controller.linear_velocity)
                angular_velocities_data.append(controller.angular_velocity)

                twist_msg = Twist()
                twist_msg.linear.x = controller.linear_velocity
                twist_msg.angular.z = controller.angular_velocity
                controller.velocity_publisher.publish(twist_msg)

                controller.rate.sleep()

                if math.sqrt((controller.goal_x - controller.turtle_x) ** 2 + (controller.goal_y - controller.turtle_y) ** 2) < 0.1:
                    break

            time_list.append(time_data)
            position_error_list.append(position_error_data)
            linear_velocities_list.append(linear_velocities_data)
            angular_velocities_list.append(angular_velocities_data)

        plot_performance(gains_list, time_list, position_error_list, linear_velocities_list, angular_velocities_list)

    except rospy.ROSInterruptException:
        pass


import mxnet as mx
import json

def check_metric(metric, *args, **kwargs):
    metric = mx.metric.create(metric, *args, **kwargs)
    str_metric = json.dumps(metric.get_config())
    metric2 = mx.metric.create(str_metric)

    assert metric.get_config() == metric2.get_config()

def test_metrics():
    check_metric('acc', axis=0)
    check_metric('f1')
    check_metric('perplexity', -1)
    composite = mx.metric.create(['acc', 'f1'])
    check_metric(composite)

if __name__ == '__main__':
    import nose
    nose.runmodule()


# import necessary databases and libraries
import pycurl,json,requests,sys,os
import pandas as pd
import numpy as np
# existing redcap API stipulates StringIO, but pycurl now works with BytesIO
from io import BytesIO
# this is where the supplementary function apidelete_records() is stored
import utils

#where would you like the data to be stored
#this is initialized to the downloads to prevent storage on github repo
data_folder = os.path.expanduser('~/Downloads/')
desktop = os.path.expanduser('~/OneDrive/Desktop/')

token1_file = 'repo_apitoken.txt'
token2_file = 'reg_apitoken.txt'

if token1_file is not None:
    token1_path = os.path.join(desktop, token1_file)
    if os.path.exists(token1_path):
        with open(token1_path, 'r') as myfile:
            token1=myfile.read().replace('\n', '')
else:
    token1 = input('What is the repository API token? ')

if token2_file is not None:
    token2_path = os.path.join(desktop, token2_file)
    if os.path.exists(token2_path):
        with open(token2_path, 'r') as myfile:
            token2=myfile.read().replace('\n', '')
else:
    token2 = input('What is the registry API token? ')

# say which repository report you want
# report 25085 is the report in the repository called "PIGS Recruitment"
pigs_report = {
    'token': token1,
    'content': 'report',
    'format': 'csv',
    'report_id': '41963',
    'rawOrLabel': 'raw',
    'rawOrLabelHeaders': 'raw',
    'exportCheckboxLabel': 'false',
    'returnFormat': 'csv'
}

# say which registry report you want
# report 12929 is the report in the registry  called "Emails"
emails_report = {
    'token': token2,
    'content': 'report',
    'format': 'csv',
    'report_id': '12929',
    'rawOrLabel': 'raw',
    'rawOrLabelHeaders': 'raw',
    'exportCheckboxLabel': 'false',
    'returnFormat': 'csv'
}

redcap_path = 'https://redcap.iths.org/api/'
repo_list = requests.post(redcap_path, data=pigs_report)
emails = requests.post(redcap_path, data=emails_report)

repo_filename =os.path.join(data_folder,'pigs_repo.csv')

with open(repo_filename, 'w') as repo_file:
        repo_file.write(repo_list.text)

emails_filename = os.path.join(data_folder,'pigs_emails.csv')

with open(emails_filename, 'w') as email_file:
        email_file.write(emails.text)

pigs_repo_data = pd.read_csv(repo_filename, encoding='latin-1')
email_data = pd.read_csv(emails_filename, encoding='latin-1')

combined = pigs_repo_data.set_index('record_id').\
join(email_data.set_index('record_id'),
lsuffix='_repo', rsuffix='_reg')

combined.to_csv(os.path.join(data_folder,'combined.csv'))


import tkinter
import io
import PIL.Image
import PIL.ImageTk
import requests
import tkinter.messagebox
import time

class CanvasCover():

	def __init__(self, frame):
		self.image_original_path = None
		self.image_thumb_path = None
		self.is_local = None
		self.pil_image_display = None
		self.pil_image_download = None
		self.image_object = None
		self.canvas = None

		self.canvas = tkinter.Canvas(frame, bg = "black")
		self.canvas.pack(expand = tkinter.YES, fill = tkinter.BOTH)
		self.canvas.bind("<Configure>", self.canvas_configure)

	def canvas_configure(self, event):
		self.refresh(event.width, event.height)

	def refresh(self, width, height):
		if not self.pil_image_display:
			self.canvas.delete("all")
		else:
			pil_image_resized = self.resize_image(self.pil_image_display, width, height)
			if pil_image_resized:
				self.image_object = PIL.ImageTk.PhotoImage(pil_image_resized) # reference to image must be kept to avoid garbage deletion
				self.canvas.create_image((width // 2, height // 2), image = self.image_object)

	def has_image(self):
		if not self.pil_image_display:
			return False
		return True

	def get_image_path(self):
		return self.image_original_path

	def create_pil_image(self, image_path, is_local):
		try:
			if is_local:
				return PIL.Image.open(image_path)
			else:
				response = requests.get(image_path)
				image_data = io.BytesIO(response.content)
				return PIL.Image.open(image_data)
		except:
			tkinter.messagebox.showerror("Error", "Image " + image_path + " cannot be loaded.")
			return None

	def load_image(self, image_original_path, image_thumb_path, is_local, resized_image_file_name):
		st = time.time()
		self.image_original_path = image_original_path
		self.image_thumb_path = image_thumb_path
		self.is_local = is_local
		self.pil_image_display = None
		self.pil_image_download = None
		self.image_object = None

		if image_thumb_path:
			image_path = image_thumb_path
		elif image_original_path:
			image_path = image_original_path
		else:
			image_path = None

		if image_path:
			self.pil_image_display = self.create_pil_image(image_path, is_local)

			if not self.image_thumb_path:
				self.pil_image_download = self.pil_image_display

			if resized_image_file_name and self.pil_image_display:
				pil_image_resized = self.resize_image(self.pil_image_display, 400, 400)
				if pil_image_resized:
					pil_image_resized_width, pil_image_resized_height = pil_image_resized.size
					image = PIL.Image.new('RGB', (400, 400), (0, 0, 0))
					image.paste(pil_image_resized, ((400 - pil_image_resized_width) // 2, (400 - pil_image_resized_height) // 2))
					image.save(resized_image_file_name)

		print(time.time(), "load_image (ms): ", (time.time() - st) * 1000)
		st = time.time()
		self.refresh(self.canvas.winfo_width(), self.canvas.winfo_height())
		print(time.time(), "refresh canvas (ms): ", (time.time() - st) * 1000)

	def resize_image(self, pil_image, width, height):
		img_width, img_height = pil_image.size

		ratio_width = img_width / width
		ratio_height = img_height / height

		ratio_img = img_width / img_height
		if ratio_width >= ratio_height:
			new_width = width - 10
			new_height = int(new_width / ratio_img)
		else:
			new_height = height - 10
			new_width = int(new_height * ratio_img)

		if new_width < 0 or new_height < 0:
			return None
		return pil_image.resize((new_width, new_height), PIL.Image.ANTIALIAS)

	def download_image(self, file_name):
		if not self.pil_image_display:
			return False

		if not self.pil_image_download:
			self.pil_image_download = self.create_pil_image(self.image_original_path, self.is_local)

		if self.pil_image_download:
			self.pil_image_download.save(file_name)

		return True


import os
import lzma
import asyncio
import functools
from typing import Optional
from aiofiles.os import stat

import diskcache

from .base import CacheBackend

CACHE_ROOT_DIR: str = os.environ.get('CACHE_ROOT_DIR', '/tmp/prerender')

class DiskCache(CacheBackend):
    def __init__(self) -> None:
        self._cache = diskcache.Cache(CACHE_ROOT_DIR)

    async def get(self, key: str, format: str = 'html') -> Optional[bytes]:
        loop = asyncio.get_event_loop()
        cache_get = self._cache.get
        data = await loop.run_in_executor(None, cache_get, key + format)
        if data is not None:
            res = await loop.run_in_executor(None, lzma.decompress, data)
            return res

    def set(self, key: str, payload: bytes, ttl: int = None, format: str = 'html') -> None:
        compressed = lzma.compress(payload)
        self._cache.set(key + format, compressed, expire=ttl)

    async def modified_since(self, key: str, format: str = 'html') -> Optional[float]:
        loop = asyncio.get_event_loop()
        cache_read = functools.partial(self._cache.get, read=True)
        file = await loop.run_in_executor(None, cache_read, key + format)
        if not file:
            return
        filename = file.name
        file.close()
        stats = await stat(filename)
        return stats.st_mtime


from jax import config

config.update("jax_enable_x64", True)
import jax
import jax.numpy as jnp
import jax.random as jr

import gpjax as gpx

class Sparse:
    param_names = ["n_data", "n_inducing"]
    params = [[2000, 5000, 10000, 20000], [10, 20, 50, 100, 200]]

    def setup(self, n_datapoints: int, n_inducing: int):
        key = jr.PRNGKey(123)
        self.X = jr.normal(key=key, shape=(n_datapoints, 1))
        self.y = jnp.sin(self.X[:, :1])
        self.data = gpx.Dataset(X=self.X, y=self.y)
        kernel = gpx.kernels.RBF(active_dims=list(range(1)))
        meanf = gpx.mean_functions.Constant()
        self.prior = gpx.Prior(kernel=kernel, mean_function=meanf)
        self.likelihood = gpx.likelihoods.Gaussian(num_datapoints=self.data.n)
        self.posterior = self.prior * self.likelihood

        Z = jnp.linspace(self.X.min(), self.X.max(), n_inducing).reshape(-1, 1)
        self.q = gpx.CollapsedVariationalGaussian(
            posterior=self.posterior, inducing_inputs=Z
        )
        self.objective = gpx.CollapsedELBO(negative=True)

    def time_eval(self, n_datapoints: int, n_dims: int):
        self.objective(self.q, self.data)

    def time_grad(self, n_datapoints: int, n_dims: int):
        jax.grad(self.objective)(self.q, self.data)


import json
from datetime import datetime
from channels.generic.websocket import AsyncWebsocketConsumer

CLIENTS = set()

class ChatConsumer(AsyncWebsocketConsumer):

    def get_secret_key(self):
        for key, value in self.scope['headers']:
            if key == b'sec-websocket-key':
                return value.decode(encoding='utf-8')
        return None

    async def connect(self):
        self.group_name = 'chat'
        await self.channel_layer.group_add(self.group_name, self.channel_name)
        await self.accept()

        key = self.get_secret_key()
        if key:
            CLIENTS.add(key)

            event = {
                'type': 'send_message',
                'users_count': len(CLIENTS)
            }

            await self.channel_layer.group_send(self.group_name, event)

    async def disconnect(self, code):
        key = self.get_secret_key()
        if key:
            CLIENTS.remove(key)

            event = {
                'type': 'send_message',
                'users_count': len(CLIENTS)
            }
            await self.channel_layer.group_send(self.group_name, event)

        await self.channel_layer.group_discard(self.group_name, self.channel_name)

    async def receive(self, text_data=None, bytes_data=None):
        data_json = json.loads(text_data)
        message = data_json['message']
        username = data_json['username']

        if not username:
            username = 'Anonymous'

        dt = datetime.now().strftime('%d.%m.%Y %H:%M')

        result = f'

{dt} - {username}:

{message}

'

        event = {
            'type': 'send_message',
            'message': result
        }

        await self.channel_layer.group_send(self.group_name, event)

    async def send_message(self, event):
        response = dict()

        message = event.get('message')
        if message:
            response['message'] = message

        users_count = event.get('users_count')
        if users_count:
            response['users_count'] = users_count

        await self.send(text_data=json.dumps(response))


#!/usr/bin/env python

from lib.exceptions.check_exception import CheckException
from lib.base.script.integration_test.case_mw import CaseMW

class Test200SmartFullIdevice019(CaseMW):
    """Comment
    No.: test_200smart_full_idevice_019
    Preconditions:
        1. Open Micro/WIN V2.5;
        2. In PN wizard, select plc role is "I-Device", click "Next" and add one transfer area;
    Step actions:
        1. Add comment with 256 characters;
        2. Add comment more than 256 characters;
        3. Add comment with Chinese characters;
    Expected results:
        1. Add successful;
        2. Add successful, the comment automatically retains the first 256 characters;
        3. Add successful;
    Priority: H
    Author: Cai, Yong
    ChangeInfo: Cai, Yong 2019-12-13 create
    """

    def prepare(self):
        """the preparation before executing the test steps
        Args:
        Example:
        Return:
        Author: Cai, Yong
        IsInterface: False
        ChangeInfo: Cai, Yong 2019-12-13 create
        """
        super(Test200SmartFullIdevice019, self).prepare()

        self.logger.info('Preconditions:')
        self.logger.info('1. Open Micro/WIN V2.5;')
        self.logger.info('2. In PN wizard, select plc role is "I-Device", click "Next" and add one transfer area;')

    def process(self):
        """execute the test steps
        Args:
        Example:
        Return:
        Author: Cai, Yong
        IsInterface: False
        ChangeInfo: Cai, Yong 2019-12-13 create
        """
        super(Test200SmartFullIdevice019, self).process()

        self.logger.info('Step actions:')
        self.logger.info('1. Add comment with 256 characters;')
        self.logger.info('2. Add comment more than 256 characters;')
        self.logger.info('3. Add comment with Chinese characters;')

        self.logger.info('Expected results:')
        self.logger.info('1. Add successful;')
        self.logger.info('2. Add successful, the comment automatically retains the first 256 characters;')
        self.logger.info('3. Add successful;')

    def cleanup(self):
        """clean up after performing the test steps
        Args:
        Example:
        Return:
        Author: Cai, Yong
        IsInterface: False
        ChangeInfo: Cai, Yong 2019-12-13 create
        """
        super(Test200SmartFullIdevice019, self).cleanup()


from django.contrib.auth.models import AbstractUser
from django.db import models
from django.db.models.deletion import SET_NULL, CASCADE
from django.utils import timezone

BEGINNER = 1
INTERMEDIATE = 2
EXPERT = 3
LEVEL_CHOICES = (
    (BEGINNER, "beginner"),
    (INTERMEDIATE, "intermediate"),
    (EXPERT, "expert"),
)

class User(AbstractUser):
    ADMIN = 1
    CHARITY = 2
    VOLUNTEER = 3
    USER_TYPES = (
        (ADMIN, 'admin'),
        (CHARITY, 'charity'),
        (VOLUNTEER, 'volunteer'),
    )
    user_type = models.PositiveSmallIntegerField(choices=USER_TYPES, default=ADMIN)

class Charity(models.Model):
    user = models.OneToOneField(User, on_delete=CASCADE)
    approved_by = models.ForeignKey(User, on_delete=SET_NULL, null=True, blank=True, related_name='charities_approved')

class Volunteer(models.Model):
    user = models.OneToOneField(User, on_delete=CASCADE)
    level = models.PositiveSmallIntegerField(choices=LEVEL_CHOICES, default=BEGINNER)
    skills = models.ManyToManyField('Skill', through='VolunteerHasSkill')

class Project(models.Model):
    FINANCIAL = 0
    NON_FINANCIAL = 1
    TYPES = (
        (FINANCIAL, 'Financial'),
        (NON_FINANCIAL, 'Non Financial'),
    )

    title = models.CharField(max_length=255)
    project_type = models.PositiveSmallIntegerField(choices=TYPES, default=FINANCIAL)
    start_time = models.DateTimeField(default=timezone.now)
    end_time = models.DateTimeField(null=True)
    created_by = models.ForeignKey(Charity, on_delete=SET_NULL, null=True)
    approved_by = models.ForeignKey(User, on_delete=SET_NULL, null=True, related_name='projects_approved')
    paid_by = models.ManyToManyField(Volunteer, through='Payment')

class Payment(models.Model):
    description = models.TextField(max_length=4095)
    amount = models.IntegerField()
    project = models.ForeignKey(Project, on_delete=CASCADE)
    volunteer = models.ForeignKey(Volunteer, on_delete=CASCADE)

class Opportunity(models.Model):
    description = models.TextField(max_length=4095)
    start_time = models.DateTimeField(default=timezone.now)
    end_time = models.DateTimeField()
    project = models.ForeignKey(Project, on_delete=CASCADE)
    volunteers = models.ManyToManyField(Volunteer)
    needed_skills = models.ManyToManyField('Skill')

class Skill(models.Model):
    title = models.CharField(max_length=255)

class VolunteerHasSkill(models.Model):
    level = models.PositiveSmallIntegerField(choices=LEVEL_CHOICES, default=BEGINNER)
    skill = models.ForeignKey(Skill, on_delete=CASCADE)
    volunteer = models.ForeignKey(Volunteer, on_delete=CASCADE)

class OpportunityNeedsSkill(models.Model):
    level = models.PositiveSmallIntegerField(choices=LEVEL_CHOICES, default=BEGINNER)
    skill = models.ForeignKey(Skill, on_delete=CASCADE)
    opportunity = models.ForeignKey(Opportunity, on_delete=CASCADE)

class Rating(models.Model):
    grade = models.PositiveSmallIntegerField()
    opportunity = models.ForeignKey(Opportunity, on_delete=CASCADE)
    charity = models.ForeignKey(Charity, on_delete=CASCADE)
    project = models.ForeignKey(Project, on_delete=CASCADE)