import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import sklearn.metrics as metrics
from sklearn.ensemble import RandomForestClassifier
import data_preparation_functions as prepfunctions
import ds_functions as ds
import os
from datetime import datetime

graphsDir = './Results/Random Forests/'
if not os.path.exists(graphsDir):
    os.makedirs(graphsDir)

print('------------------------------')
print('-                            -')
print('-     QOT Random Forests     -')
print('-                            -')
print('------------------------------')

data: pd.DataFrame = pd.read_csv('../Dataset/qsar_oral_toxicity.csv', sep=';', header=None)
train, test = train_test_split(data, train_size=0.7, stratify=data[1024].values)
testDatas = {}
datas = prepfunctions.prepare_dataset(train, 1024, False, False)
for key in datas:
    testDatas[key] = test.copy()

featured_datas = prepfunctions.mask_feature_selection(datas, 1024, True, './Results/FeatureSelection/QOT Feature Selection - Features')
featured_test_datas = prepfunctions.mask_feature_selection(testDatas, 1024, True, './Results/FeatureSelection/QOT Feature Selection - Features')

best_accuracies = {}

for key in datas:
    for do_feature_eng in [False, True]:
        if (do_feature_eng):
            data = featured_datas[key]
            testData = featured_test_datas[key].copy()
            subDir = graphsDir + 'FeatureEng/' +  key + '/'
            if not os.path.exists(subDir):
                os.makedirs(subDir)
        else:
            data = datas[key]
            testData = test.copy()
            subDir = graphsDir + key + '/'
            if not os.path.exists(subDir):
                os.makedirs(subDir)

        print('QOT Random Forests - Parameters Combinations')
        now = datetime.now()
        current_time = now.strftime("%H:%M:%S")
        print(current_time, ": Key: ", key, ", feature eng: ", do_feature_eng)

        trnY: np.ndarray = data.pop(1024).values
        trnX: np.ndarray = data.values
        tstY: np.ndarray = testData.pop(1024).values
        tstX: np.ndarray = testData.values

        n_estimators = [5, 10, 25, 50, 75, 100, 150, 200, 250, 300]
        max_depths = [5, 10, 25]
        max_features = [.1, .3, .5, .7, .9, 1]
        best = ('', 0, 0)
        last_best = 0
        best_tree = None
        overfit_values = {}

        cols = len(max_depths)
        plt.figure()
        fig, axs = plt.subplots(1, cols, figsize=(cols*ds.HEIGHT*2.5, ds.HEIGHT), squeeze=False)
        for k in range(len(max_depths)):
            d = max_depths[k]
            now = datetime.now()
            current_time = now.strftime("%H:%M:%S")
            print(current_time, ": d: ", d)
            values = {}
            overfit_values[d] = {}
            for f in max_features:
                yvalues = []
                train_acc_values = []
                test_acc_values = []
                now = datetime.now()
                current_time = now.strftime("%H:%M:%S")
                print(current_time, ": f: ", f)
                for n in n_estimators:
                    now = datetime.now()
                    current_time = now.strftime("%H:%M:%S")
                    print(current_time, ": n: ", n)
                    rf = RandomForestClassifier(n_estimators=n, max_depth=d, max_features=f)
                    rf.fit(trnX, trnY)
                    prdY = rf.predict(tstX)
                    prd_trainY = rf.predict(trnX)
                    yvalues.append(metrics.accuracy_score(tstY, prdY))
                    train_acc_values.append(metrics.accuracy_score(trnY, prd_trainY))
                    test_acc_values.append(metrics.accuracy_score(tstY, prdY))
                    if yvalues[-1] > last_best:
                        best = (d, f, n)
                        last_best = yvalues[-1]
                        last_best_train = train_acc_values[-1]
                        best_tree = rf

                values[f] = yvalues
                overfit_values[d][f] = {}
                overfit_values[d][f]['train'] = train_acc_values
                overfit_values[d][f]['test'] = test_acc_values
            ds.multiple_line_chart(n_estimators, values, ax=axs[0, k], title='Random Forests with max_depth=%d'%d,
                                xlabel='nr estimators', ylabel='accuracy', percentage=True)

        text = key
        if (do_feature_eng): text += ' with FS'
        best_accuracies[text] = [last_best_train, last_best]

        print('Best results with depth=%d, %1.2f features and %d estimators, with accuracy=%1.2f'%(best[0], best[1], best[2], last_best))
        fig.text(0.5, 0.03, 'Best results with depth=%d, %1.2f features and %d estimators, with accuracy=%1.2f'%(best[0], best[1], best[2], last_best), fontsize=7, ha='center', va='center')
        plt.suptitle('QOT Random Forests - ' + key + '- Parameters Combinations')
        plt.savefig(subDir + 'QOT Random Forests - ' + key + '- Parameters Combinations')
        print()

        plt.figure()
        fig, axs = plt.subplots(3, 6, figsize=(32, 16), squeeze=False)
        for k in range(len(max_depths)):
            d = max_depths[k]
            for i in range(len(max_features)):
                f = max_features[i]
                ds.multiple_line_chart(n_estimators, overfit_values[d][f], ax=axs[k,i], title='Overfitting for max_depth = %d with max_features = %f'%(d,f), xlabel='n_estimators', ylabel='accuracy', percentage=True)
        plt.suptitle('QOT Overfitting - Random Forests')
        plt.savefig(subDir + 'QOT Overfitting - Random Forests')

        print('QOT Random Forests - Performance & Confusion Matrix')
        prd_trn = best_tree.predict(trnX)
        prd_tst = best_tree.predict(tstX)
        ds.plot_evaluation_results(["negative", "positive"], trnY, prd_trn, tstY, prd_tst)
        plt.suptitle('QOT Random Forests - ' + key + ' - Performance & Confusion Matrix')
        plt.savefig(subDir + 'QOT Random Forests - ' + key + ' - Performance & Confusion Matrix')
        print()

plt.figure(figsize=(7,7))
ds.multiple_bar_chart(['Train', 'Test'], best_accuracies, ylabel='Accuracy')
plt.suptitle('QOT Sampling & Feature Selection')
plt.savefig(graphsDir + 'QOT Sampling & Feature Selection')


# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        res = []
        s = []

        p = root

        while len(s) > 0 or p is not None:
            if p:
                s.append(p)
                res.insert(0, p.val)
                p = p.right
            else:
                node = s.pop()
                p = node.left

        return res


from collections import Counter
N = int(input())
A = list(map(int, input().split()))
uf = UnionFind(max(A) + 1)
for i in range(N // 2):
    if A[i] != A[- 1 - i]:
        uf.union(A[i], A[- 1 - i])
roots = uf.roots()
ans = 0
for p in roots:
    ans += uf.size(p) - 1
print(ans)
#print(roots)


import os
from dotenv import load_dotenv
from telegram import Update, ForceReply
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
import openai
from Helper.functions import get_image, get_chat

load_dotenv()

# Get Telegram API key from environment variable
TELEGRAM_API_KEY = os.getenv('TELEGRAM_API_KEY')

# Get Chat GPT API key from environment variable
CHAT_GPT_API_KEY = os.getenv('CHAT_GPT_API_KEY')

# Authenticate OpenAI API
openai.api_key = CHAT_GPT_API_KEY

def start(update: Update, context: CallbackContext) -> None:
    """Send a message when the command /start is issued."""
    update.message.reply_text('Welcome, You can ask anything from me\n@RedXvirus')

def help_command(update: Update, context: CallbackContext) -> None:
    """Send a message when the command /help is issued."""
    update.message.reply_text('This bot can perform the following commands\n/image -> to create image from text\n/ask -> ask anything from me')

def image_command(update: Update, context: CallbackContext) -> None:
    """Create image from text."""
    text = update.message.text.replace("/image", "").strip().lower()

    if text:
        # Get image from OpenAI Dall-E API
        res = get_image(text)

        if res:
            context.bot.send_chat_action(chat_id=update.effective_message.chat_id, action='upload_photo')
            context.bot.send_photo(chat_id=update.effective_message.chat_id, photo=res, reply_to_message_id=update.message.message_id)
    else:
        context.bot.send_message(chat_id=update.effective_message.chat_id, text='You have to give some description after /image', reply_to_message_id=update.message.message_id)

def ask_command(update: Update, context: CallbackContext) -> None:
    """Get chat response from Chat GPT API."""
    text = update.message.text.replace("/ask", "").strip().lower()

    if text:
        # Get response from OpenAI GPT-3 API
        context.bot.send_chat_action(chat_id=update.effective_message.chat_id, action='typing')
        res = get_chat(text)

        if res:
            context.bot.send_message(chat_id=update.effective_message.chat_id, text=res, reply_to_message_id=update.message.message_id)
    else:
        context.bot.send_message(chat_id=update.effective_message.chat_id, text='Please ask anything after /ask', reply_to_message_id=update.message.message_id)

def main() -> None:
    """Start the bot."""
    updater = Updater(TELEGRAM_API_KEY)

    # Add handlers for Telegram commands
    updater.dispatcher.add_handler(CommandHandler("start", start))
    updater.dispatcher.add_handler(CommandHandler("help", help_command))
    updater.dispatcher.add_handler(CommandHandler("image", image_command))
    updater.dispatcher.add_handler(CommandHandler("ask", ask_command))

    # Start the bot
    updater.start_polling()

    # Run the bot until the user presses Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT
    updater.idle()

if name == 'main':
    main()


# programa que recebe notas, faltas, calcula a média e informa se o aluno está aprovado ou reprovado
nome = str(input("Digite o nome do aluno: "))
nota1 = float(input("Digite a primeira nota: "))
nota2 = float(input("Digite a primeira nota: "))
faltas = int(input("Digite a quantidade de faltas: "))
media = (nota1 + nota2) / 2

if (faltas > 3) or (media < 7):
    print("Nome do aluno: " + nome)
    print("Situação: REPROVADO")
else:
    print("Nome do aluno: " + nome)
    print("Situação: APROVADO")


import numpy as np

from ..HeightContainer import UniformTopographyInterface

def checkerboard_detrend(topography, subdivisions):
    """
    Perform tilt correction (and substract mean value) in each individual
    rectangle of a checkerboard decomposition of the surface. This is
    identical to subdividing the surface into individual, nonoverlapping
    rectangles and performing individual tilt corrections on them.

    The main application of this function is to carry out a variable
    bandwidth analysis of the surface.

    Parameters
    ----------
    topography : :obj:`Topography` or :obj:`UniformLineScan`
        Container storing the uniform topography map
    subdivisions : tuple
        Number of subdivision per dimension, i.e. physical_sizes of the checkerboard.

    Returns
    -------
    arr : array
        Array with height information, tilt-corrected within each
        checkerboard.
    """
    arr = topography.heights().copy()
    physical_sizes = topography.physical_sizes
    nb_dim = topography.dim

    shape = arr.shape

    # compute unique consecutive index for each subdivided region
    region_coord = [np.arange(shape[i]) * subdivisions[i] // shape[i] for i in range(nb_dim)]
    if nb_dim > 1:
        region_coord = np.meshgrid(*region_coord, indexing='ij')
    region_index = region_coord[0]
    for i in range(1, nb_dim):
        region_index = subdivisions[i] * region_index + region_coord[i]

    # compute x- and y-coordinate
    if nb_dim == 1:
        x = np.arange(shape[0])
    elif nb_dim == 2:
        x, y = np.meshgrid(*(np.arange(n) for n in shape), indexing='ij')
    else:
        raise ValueError('Cannot handle {}-dimensional topographies.'.format(nb_dim))

    region_index.shape = (-1, )
    x.shape = (-1, )
    arr.shape = (-1, )
    sum_1 = np.bincount(region_index)
    sum_x = np.bincount(region_index, x)
    sum_xx = np.bincount(region_index, x*x)
    sum_h = np.bincount(region_index, arr)
    sum_xh = np.bincount(region_index, x*arr)
    if nb_dim == 2:
        y.shape = (-1,)
        sum_y = np.bincount(region_index, y)
        sum_yy = np.bincount(region_index, y * y)
        sum_xy = np.bincount(region_index, x * y)
        sum_yh = np.bincount(region_index, y*arr)

    if nb_dim == 1:
        # Calculated detrended plane. Detrended plane is given by h0 + mx*x.
        A = np.array([[sum_1, sum_x],
                      [sum_x, sum_xx]])
        b = np.array([sum_h, sum_xh])
        h0, mx = np.linalg.solve(A.T, b.T).T

        arr -= h0[region_index] + mx[region_index]*x
    else:
        # Calculated detrended plane. Detrended plane is given by h0 + mx*x + my*y.
        A = np.array([[sum_1, sum_x, sum_y],
                      [sum_x, sum_xx, sum_xy],
                      [sum_y, sum_xy, sum_yy]])
        b = np.array([sum_h, sum_xh, sum_yh])
        h0, mx, my = np.linalg.solve(A.T, b.T).T

        arr -= h0[region_index] + mx[region_index]*x + my[region_index]*y

    arr.shape = shape

    return arr

def variable_bandwidth(topography, nb_grid_pts_cutoff=4):
    """
    Perform a variable bandwidth analysis by computing the mean
    root-mean-square height within increasingly finer subdivisions of the
    surface topography.

    Parameters
    ----------
    topography : :obj:`Topography` or :obj:`UniformLineScan`
        Container storing the uniform topography map
    nb_grid_pts_cutoff : int
        Minimum nb_grid_pts to allow for subdivision. The analysis will
        automatically analyze subdivision down to this nb_grid_pts.

    Returns
    -------
    magnifications : array
        Array containing the magnifications.
    bandwidths : array
        Array containing the bandwidths, here the physical_sizes of the subdivided
        topography. For 2D topography maps, this is the mean of the two physical_sizes
        lenghts of the subdivided section of the topography.
    rms_heights : array
        Array containing the rms height corresponding to the respective
        magnification.
    """
    magnification = 1
    physical_sizes = np.array(topography.physical_sizes)
    min_size = np.min(physical_sizes)
    subdivisions = np.round(topography.physical_sizes / min_size).astype(int)
    nb_grid_pts = np.array(topography.nb_grid_pts, dtype=int)
    magnifications = []
    bandwidths = []
    rms_heights = []
    while ((nb_grid_pts // subdivisions).min() >= nb_grid_pts_cutoff):
        magnifications += [magnification]
        bandwidths += [np.mean(physical_sizes / subdivisions)]
        rms_heights += [np.std(topography.checkerboard_detrend(subdivisions))]
        magnification *= 2
        subdivisions *= 2
    return np.array(magnifications), np.array(bandwidths), np.array(rms_heights)

### Register analysis functions from this module

UniformTopographyInterface.register_function('checkerboard_detrend', checkerboard_detrend)
UniformTopographyInterface.register_function('variable_bandwidth', variable_bandwidth)


from unittest import TestCase

from models import server
from models.user import User

class TestServerModelFields(TestCase):

    def test_server_model_exists(self):
        self.assertEqual(
            hasattr(server, "Server"), True,
            "Server doesn't exist"
        )

    def test_server_model_must_be_initializable(self):
        self.assertEqual(
            hasattr(server.Server, "__init__"), True,
            "Server must exist inside server's file"
        )

    def test_server_class_fields(self):
        data = {
            "umax": 2,
            "ttask": 4
        }
        instance = server.Server(**data)
        self.assertEqual(instance.umax, data["umax"])
        self.assertEqual(instance.ttask, data["ttask"])
        self.assertEqual(instance.users, [])

    def test_server_user_field_starting_value(self):
        """
        users has to always start with an empty list
        """
        data = {
            "umax": 2,
            "ttask": 4,
            "users": [User()]
        }
        self.assertRaises(TypeError, server.Server, **data)

class TestServerModelDisconnectMethod(TestCase):

    def setUp(self):
        data = {
            "umax": 3,
            "ttask": 4
        }
        self.server = server.Server(**data)

    def test_disconnect_inactive_users_exists(self):
        self.assertEqual(
            hasattr(self.server, "disconnect_inactive_users"), True,
            "server instance must have disconnect_inactive_users method"
        )

    def test_disconnect_inactive_users_must_be_callable(self):
        self.assertEqual(
            hasattr(server.Server.disconnect_inactive_users, "__call__"), True,
            "disconnect_inactive_users must be callable"
        )

    def test_disconnect_inactive_users_with_active_users(self):
        users = [User(), User()]
        self.server.users = users
        self.server.disconnect_inactive_users()
        self.assertEqual(self.server.users, users)

    def test_disconnect_inactive_users_with_inactive_users(self):
        user_1 = User()
        user_1.tick = 4
        user_2 = User()
        user_2.tick = 4

        users = [user_1, user_2]

        self.server.users = users
        self.server.disconnect_inactive_users()
        self.assertEqual(self.server.users, [])

    def test_disconnect_inactive_users_with_active_and_inactive_users(self):
        user_1 = User()
        user_1.tick = 4
        user_2 = User()

        users = [user_1, user_2]

        self.server.users = users
        self.server.disconnect_inactive_users()
        self.assertEqual(self.server.users, [user_2])

class TestServerModelIsFullMethod(TestCase):

    def setUp(self):
        data = {
            "umax": 3,
            "ttask": 4
        }
        self.server = server.Server(**data)

    def test_is_full_exists(self):
        self.assertEqual(
            hasattr(self.server, "is_full"), True,
            "server instance must have is_full method"
        )

    def test_is_full_must_be_callable(self):
        self.assertEqual(
            hasattr(server.Server.is_full, "__call__"), True,
            "is_full must be callable"
        )

    def test_is_full_with_empty_list(self):
        self.assertEqual(self.server.is_full(), False)

    def test_is_full_with_full_list(self):
        self.server.users = [User(),User(),User()]
        self.assertEqual(self.server.is_full(), True)

    def test_is_full_with_half_list(self):
        self.server.users = [User(),User()]
        self.assertEqual(self.server.is_full(), False)

class TestServerModelIsEmptyMethod(TestCase):

    def setUp(self):
        data = {
            "umax": 3,
            "ttask": 4
        }
        self.server = server.Server(**data)

    def test_is_empty_exists(self):
        self.assertEqual(
            hasattr(self.server, "is_empty"), True,
            "server instance must have is_empty method"
        )

    def test_is_empty_must_be_callable(self):
        self.assertEqual(
            hasattr(server.Server.is_empty, "__call__"), True,
            "is_empty must be callable"
        )

    def test_is_empty_with_empty_list(self):
        self.assertEqual(self.server.is_empty(), True)

    def test_is_empty_with_full_list(self):
        self.server.users = [User(),User(),User()]
        self.assertEqual(self.server.is_empty(), False)

    def test_is_empty_with_half_list(self):
        self.server.users = [User(),User()]
        self.assertEqual(self.server.is_empty(), False)

class TestServerModelAddUserMethod(TestCase):

    def setUp(self):
        data = {
            "umax": 3,
            "ttask": 4
        }
        self.server = server.Server(**data)

    def test_add_user_exists(self):
        self.assertEqual(
            hasattr(self.server, "add_user"), True,
            "server instance must have add_user method"
        )

    def test_add_user_must_be_callable(self):
        self.assertEqual(
            hasattr(server.Server.add_user, "__call__"), True,
            "add_user must be callable"
        )

    def test_add_user_with_empty_list(self):
        self.server.add_user()
        self.assertEqual(len(self.server.users), 1)
        self.server.add_user()
        self.assertEqual(len(self.server.users), 2)

    def test_add_user_with_full_list(self):
        self.server.users = [User(),User(),User()]
        self.server.add_user()
        self.assertEqual(len(self.server.users), 3)

class TestServerModelExecTickCycleMethod(TestCase):

    def setUp(self):
        data = {
            "umax": 3,
            "ttask": 4
        }
        self.server = server.Server(**data)

    def test_exec_a_tick_cycle_exists(self):
        self.assertEqual(
            hasattr(self.server, "exec_a_tick_cycle"), True,
            "server instance must have exec_a_tick_cycle method"
        )

    def test_exec_a_tick_cycle_must_be_callable(self):
        self.assertEqual(
            hasattr(server.Server.exec_a_tick_cycle, "__call__"), True,
            "exec_a_tick_cycle must be callable"
        )

    def test_exec_a_tick_cycle_increment_one_tick_on_each_user(self):
        user_1 = User()
        user_1.tick = 1
        user_2 = User()
        self.server.users = [user_1, user_2]

        self.server.exec_a_tick_cycle()
        self.assertEqual(self.server.users[0].tick, 2)
        self.assertEqual(self.server.users[1].tick, 1)


############## WRITE

# Efetuando a leitura de um arquivo, mesmo se tratando de um gravação o comando continua sendo open.
# Usando w - Caso o arquivo não exista ele será criado na execução, se existir o mesmo será substituído
# Usando a - Caso o arquivo não exista ele será criado na execução, se existir o os dados gerados serão incluídos.
# Caso ocorra a necessidade de efetuar a leitura e gravação no mesmo código use w+
demo = open("demo.txt","w")
demo.write("Criando primeira linha \n")
demo.write("Criando segunda linha \n")
demo.write("Criando terceira linha \n")
demo.write("Welcome!!")
## Atenção é indispensável o uso do close, senão o arquivo poderá abresentar dado sujo ou inválido... por estar em uso.
demo.close()

#print("A gravação ocorreu com sucesso")

# Efetuando uma interação para gravar em arquivo
dados = input("Informe os nomes dos convidados: ")

arquivo = open("demo.csv","w")
arquivo.write(dados)
arquivo.close()

print("Dados gravados com sucesso! ")

002 - Estudo direcionado/Projeto/Aula11 - Gravação de arquivos.py


"""
Module containing functions for genetic algorithm """
import numpy as np
from random import choice, random, randint
from math import isinf

class GaIndividual:
    def __init__(self, genes):
        self.fitness = None
        self.genes = genes

def mutate(individual, prob, values):
    """
    Return a mutated copy of individual.

    Args:
        individual (GaIndividual): individual to mutated
        prop (float): probability of gene mutation (value between 0 and 1)
        values (array-like): possible gene values

    Returns:
        array-like: mutated copy of individual
    """
    child = GaIndividual(individual.genes[:])
    i = randint(0, len(child.genes) - 1)
    child.genes[i] = choice(values)
    return child

def tournament_selection(population, k):
    """
    Tournament selection of individuals, based on their fitness values.

    Args:
        population (list): list of GaIndividual
        k (integer): tournament size

    Returns:
        GaIndividual
    """
    best = choice(population)
    for i in range(1, k + 1):
        ind = choice(population)
        if ind.fitness < best.fitness:
            best = ind
    return best

def crossover(a, b):
    """
    Performs single-point crossover.

    Args:
        a (GaIndividual): first parent
        b (GaIndividual): second parent

    Returns:
        tuple: two GaIndividual instances
    """
    pivot = randint(0, len(a.genes) - 1)

    child_a = a.genes[0:pivot] + b.genes[pivot:]
    child_b = b.genes[0:pivot] + a.genes[pivot:]

    return GaIndividual(child_a), GaIndividual(child_b)

class GaPredictors:

    def __init__(self, train_x, train_y, fraction, size):
        """
        Initialize the GaPredictors population
	Args:
	    train_x (array): features
	    train_y (array): target vectors
	    fraction (int): fraction of data to use for predictors
	    size (int): population size
	"""
        self.train_x = train_x
        self.train_y = train_y
        self.population_size = size

        self.predictor_size = len(train_x)//fraction

        self.gene_values = list(range(len(train_x)))

        self.population = []

        for _ in range(size):
            self.population.append(GaIndividual([randint(0, len(train_x)-1) for _ in range(self.predictor_size)]))

    def predictor_data(self, genes):
        """
        Probably should be in utils or something
        """
        return self.train_x[np.ix_(genes)], self.train_y[np.ix_(genes)]

    def evaluate_fitness(self, trainers_set):
        """
        Assign fitness to predictors in populations

        Args:
            trainers (TrainersSet): trainers used to calculate predictors fitness

        """
        for predictor in self.population:
            total = 0
            reduced_train_x, reduced_train_y = self.predictor_data(
                    predictor.genes)

            for trainer in trainers_set.population:
                f_predicted = trainers_set.fitness(trainer, reduced_train_x, reduced_train_y)

                total += (abs(trainer.fitness - f_predicted)/trainer.fitness)*100

            predictor.fitness = total/len(trainers_set.population)

    def predictors_evolution_step(self, trainers):
        new_population = []

        while len(new_population) < self.population_size:
            parent_a = tournament_selection(self.population, 2)
            parent_b = tournament_selection(self.population, 2)

            children = crossover(parent_a, parent_b)
            for child in children:
                if random() > 0.8:
                    child = mutate(child, 0.1, self.gene_values)
                new_population.append(child)
        self.population = new_population
        self.evaluate_fitness(trainers)

    @property
    def best_predictor(self):
        best = min(self.population, key=lambda p: p.fitness)
        return best

    def best_predictors_data(self):
        best = self.best_predictor
        return self.predictor_data(best.genes)


# http://www.python-course.eu/matrix_arithmetic.php

import numpy as np

x = np.array([1,2,3])
y = np.array([-7, 8, 9])

# a.b = a1b1 + a2b2 + a3b3
z = np.dot(x, y)
print(z)

# and a.b= |a|.|b|.cos(x)
# so
x_length = np.sqrt((x* x).sum())
y_length = np.sqrt((y* y).sum())

cosXVal = z / x_length / y_length

# base on cosx value to arc value
angle_arc = np.arccos(cosXVal)

# to based on arc value to calculate angle
angle = angle_arc * 360 / 2 / np.pi
print(angle)


import numpy as np
from numpy import pi
from . import model

class QGDiagnostics(model.Model):
    def _initialize_model_diagnostics(self):
        self.add_diagnostic('entspec',
                description='barotropic enstrophy spectrum',
                function= (lambda self:
                    np.abs((self.Hi[:,np.newaxis,np.newaxis]*self.qh).sum(axis=0)/self.H)**2/self.M**2),
                units='m s^-2',
                dims=('l','k')
        )

        self.add_diagnostic('paramspec_APEflux',
            description='total additional APE flux due to subgrid parameterization',
            function=(lambda self:
                -self._calc_paramspec_contribution(np.einsum("ij, jk..., k... -> i...",
                        self.S, self.a, self._calc_parameterization_contribution()))
                ),
            units='m^2 s^-3',
            dims=('l','k'),
            skip_comparison=True,
        )

        self.add_diagnostic('paramspec_KEflux',
            description='total additional KE flux due to subgrid parameterization',
            function=(lambda self:
                self.wv2*self._calc_paramspec_contribution(np.einsum("ij..., j... -> i...",
                        self.a, self._calc_parameterization_contribution()))
                ),
            units='m^2 s^-3',
            dims=('l','k'),
            skip_comparison=True,
        )

        self.add_diagnostic('ENSflux',
                 description='barotropic enstrophy flux',
                 function = (lambda self: (-self.Hi[:,np.newaxis,np.newaxis]*
                              (self.qh.conj()*self.Jq).real).sum(axis=0)/self.H/self.M**2),
                units='s^-3',
                dims=('l','k')
        )

        self.add_diagnostic('ENSgenspec',
                    description='the spectrum of the rate of generation of barotropic enstrophy',
                    function = (lambda self:
                                (self.Hi[:,np.newaxis,np.newaxis]*((self.ilQx-self.ikQy)*
                                self.Sph.conj()*self.ph).real).sum(axis=0)/self.H/self.M**2),
                units='s^-3',
                dims=('l','k')
        )

        self.add_diagnostic('ENSfrictionspec',
                    description='the spectrum of the rate of dissipation of barotropic enstrophy due to bottom friction',
                    function = (lambda self: self.rek*self.Hi[-1]/self.H*self.wv2*
                        (self.qh[-1].conj()*self.ph[-1]).real/self.M**2),
                units='s^-3',
                dims=('l','k')
        )

        self.add_diagnostic('APEgenspec',
                    description='the spectrum of the rate of generation of available potential energy',
                    function =(lambda self: (self.Hi[:,np.newaxis,np.newaxis]*
                                (self.Ubg[:,np.newaxis,np.newaxis]*self.k +
                                 self.Vbg[:,np.newaxis,np.newaxis]*self.l)*
                                (1j*self.ph.conj()*self.Sph).real).sum(axis=0)/self.H/self.M**2),
                units='m^2 s^-3',
                dims=('l','k')
        )

    def _calc_paramspec_contribution(self, term):
        height_ratios = (self.Hi/self.H)[:,np.newaxis,np.newaxis]
        return np.real(height_ratios*self.ph.conj()*term).sum(axis=0) / self.M**2

    def _calc_cfl(self):
        return np.abs(
            np.hstack([self.u + self.Ubg[:,np.newaxis,np.newaxis], self.v])
        ).max()*self.dt/self.dx

    # calculate KE: this has units of m^2 s^{-2}
    #   (should also multiply by H1 and H2...)
    def _calc_ke(self):
        ke = 0.
        for j in range(self.nz):
            ke += .5*self.Hi[j]*self.spec_var(self.wv*self.ph[j])
        return ke.sum() / self.H

    # calculate eddy turn over time
    # (perhaps should change to fraction of year...)
    def _calc_eddy_time(self):
        """ estimate the eddy turn-over time in days """
        ens = 0.
        for j in range(self.nz):
            ens = .5*self.Hi[j] * self.spec_var(self.wv2*self.ph[j])

        return 2.*pi*np.sqrt( self.H / ens.sum() ) / 86400


def solution(arr, queries):
    answer = []
    for lst in queries:
        new_arr = arr[lst[0]:lst[1]+1]
        minimum = -1
        standard = lst[2]
        for i in new_arr:
            if i > standard:
                if minimum == -1:
                    minimum = i
                elif i < minimum:
                    minimum = i
        answer.append(minimum)
    return answer

프로그래머스/unrated/181923. 수열과 구간 쿼리 2/수열과 구간 쿼리 2.py

수열과 구간 쿼리 2.py


import re
import subprocess
from typing import List, Tuple
import uuid
import time

# Top level bash script to run as init process (PID 1) in each docker container to make sure that
# the docker container exits when the calling python process exits
SH_TEMPLATE = """
EXIT_CODE_FILE=$(mktemp)
cleanup() {
    EXIT_CODE=$(cat $EXIT_CODE_FILE)
    exit $EXIT_CODE
}
trap cleanup INT
( COMMAND ; echo $? > $EXIT_CODE_FILE ; kill -s INT $$ ) &
read _
"""

# How often to poll to see if a container is running
CONTAINER_CHECK_PERIOD = 0.1

def check_container_running(name: str) -> bool:
    result = subprocess.run(["docker", "container", "inspect", name], # pylint: disable=subprocess-run-check
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)
    return result.returncode == 0

def wait_for_container(name: str, timeout: float = float("inf")):
    end_time = time.time() + timeout
    while time.time() < end_time:
        if check_container_running(name):
            return
        time.sleep(CONTAINER_CHECK_PERIOD)
    raise ValueError("Container did not start in time")

def get_container_ip(name: str) -> str:
    process = subprocess.run(["docker", "inspect", "-f",
                              "{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}", name],
                             stdout=subprocess.PIPE, check=True)
    return process.stdout.decode("utf-8")

def run_docker_target(bazel_target: str, args: List[str] = None,
                      docker_args: List[str] = None,
                      start_timeout: float = 120,
                      delay: int = 0) -> Tuple[subprocess.Popen, str]:
    # Set default arguments
    if args is None:
        args = []

    # Get the path of the bazel image
    regex = r"//(.+):(.+)"
    match = re.match(regex, bazel_target)
    if not match:
        raise ValueError(f"bazel_target \"{bazel_target}\" does not match regex: \"{regex}\"")
    package, target = match.groups()
    bundle_script = f"{package}/{target}"

    # Run the bundle script to add the image to the docker daemon, and get the hash
    bundle_result = subprocess.run([bundle_script], stdout=subprocess.PIPE, check=True)
    image_hash_match = re.search(r"Tagging (.+) as", bundle_result.stdout.decode("utf-8"))
    if not image_hash_match:
        raise ValueError(f"Could not determine image hash for target {bazel_target}")
    image_hash = image_hash_match.groups()[0]

    # Get the entrypoint command
    result = subprocess.run(["docker", "inspect", "-f", "{{.Config.Entrypoint}}", image_hash],
                            stdout=subprocess.PIPE, check=True).stdout.decode('utf-8')
    args = result[1:-2].split(" ") + args
    if delay != 0:
        args = ["sleep", str(delay), ";"] + args

    # Run a the container inside a special bash script that will exit if
    # the calling process dies, so the container will always exit
    name = f'bazel-test-{str(uuid.uuid4())}'
    script = SH_TEMPLATE.replace("COMMAND", " ".join(args))
    docker_cmd = ["docker", "run", "-i", "--rm", "--entrypoint", "sh", "--name", name]
    if docker_args is not None:
        docker_cmd.extend(docker_args)
    docker_cmd.extend([image_hash, "-c", script])
    print(" ".join(docker_cmd), flush=True)
    process = subprocess.Popen(docker_cmd, stdin=subprocess.PIPE)
    try:
        wait_for_container(name, timeout=start_timeout)
        address = get_container_ip(name).strip()
    except:
        process.kill()
        raise
    return process, address


def minSwaps(s: str) -> int:
        ans = n = len(s)
        gg, bb = 0, 0
        for c in s:
            if c == 'G':
                gg += 1
            else:
                bb += 1
        if abs(gg - bb) > 1: return -1
        if gg >= bb:
            s1 = 'GB' * (n // 2)
            s1 += 'G' if n % 2 else ''
            cnt = sum(c1 != c for c1, c in zip(s1, s))
            ans = cnt // 2
        if gg <= bb:
            s2 = 'BG' * (n // 2)
            s2 += 'B' if n % 2 else ''
            cnt = sum(c2 != c for c2, c in zip(s2, s))
            ans = min(ans, cnt // 2)
        return ans

string = input()
print(minSwaps(string),end='')


#general requirements
import pandas as pd
import numpy as np
import sys
import warnings
import pickle
from collections import Counter
#preprocessing stuff
import ExtraMLFxns as mlf #my personal extra fxns in CustomModules
from sklearn.model_selection import train_test_split, GridSearchCV, GroupShuffleSplit, StratifiedShuffleSplit, cross_validate
from sklearn import preprocessing
from imblearn.over_sampling import SMOTE, SMOTENC
from imblearn.combine import SMOTEENN, SMOTETomek
#classifiers
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier, DistanceMetric
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression, RidgeClassifier, PassiveAggressiveClassifier
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier, GradientBoostingClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis, LinearDiscriminantAnalysis
#process results
from sklearn.metrics import roc_curve, auc, recall_score, accuracy_score, precision_score, confusion_matrix, make_scorer, matthews_corrcoef, hamming_loss
warnings.filterwarnings(action="ignore")

name_index = int(sys.argv[1]) #from job array
over_samp = str(sys.argv[2]) #OverSamp, OvUnEEN, OvUnTomek; has to be OverSampNC because of the categorical features
cv_name = str(sys.argv[3]) #GSS for groupShuffleSplit, SSS for StratifiedShuffleSplit
group_name = str(sys.argv[4]) #select from 'cath_class', 'cath_arch', 'scop_class', 'scop_fold', 'ECOD_arch', 'ECOD_x_poshom', 'ECOD_hom'; we settled on using just ECOD_arch
data_subset = str(sys.argv[5]) #All_Sph, All_Shell, Gen, etc
opt_type = str(sys.argv[6]) #Prec Acc MCC Multi
relaxed = str(sys.argv[7]) #Relaxed or UnRelaxed

print(name_index, over_samp, cv_name, group_name, data_subset, opt_type, relaxed)

def scale_resample(X_train, y_train, X_test, over_samp_strat = "OverSampNC"):
    #split for scaling into categorical and not categorical
    not_cat_electro = ("geom_gRMSD", "geom_MaxgRMSDDev","geom_val", "geom_nVESCUM","geom_AtomRMSD")
    geom = [name for name in X_train if name.startswith("geom")]
    cat_data = [x for x in geom if not x in not_cat_electro]
    cat_data.extend(['MetalCodes', 'MetalAtoms', 'SEPocket', "groupID"])
    #print(cat_data)

    X_train_scale = preprocessing.scale( X_train[X_train.columns.difference(cat_data)] )
    scaler = preprocessing.StandardScaler().fit( X_train[X_train.columns.difference(cat_data)] ) #scale the training set
    X_test_scale = scaler.transform( X_test[X_test.columns.difference(cat_data)] ) #scale the test set based on the scale of the training set
    num_norm_cols = X_test_scale.shape[1]
    #add categorical data back
    #print(X_train[[ entry for entry in X_train.columns if entry in cat_data]])
    X_train_scale = np.concatenate((X_train_scale, X_train[[ entry for entry in X_train.columns if entry in cat_data]]), axis = 1)
    X_test_scale = np.concatenate((X_test_scale, X_test[[ entry for entry in X_test.columns if entry in cat_data]]), axis = 1)
    #print(X_test_scale)
    X_test_scale = X_test_scale[:,:-1] #drop out the foldID b/c not needed for X_test
    #print(X_test_scale[0:2,:])

    #can only use oversampling for canonicals
    if over_samp_strat == "OverSampNC":
        smote = SMOTENC(categorical_features= np.arange(num_norm_cols, X_train_scale.shape[1], 1) )
    else:
        print("Invalid oversampling term because of categoricals")
        sys.exit()

    X_train_new, y_train_new = smote.fit_resample(X_train_scale, y_train.Catalytic)
    #print(X_train_new.shape, Counter(y_train_new))
    fold_ids = X_train_new[:,-1] #last column is the foldID converted to numeric to assign to groups
    X_train_new = X_train_new[:,:-1] #drop the foldID out for final training

    print("Finished processing data")
    return(X_train_new, y_train_new, X_test_scale, fold_ids)

prec_score = make_scorer(mlf.prec_score_custom, greater_is_better = True)
mcc_score = make_scorer(mlf.mcc_score, greater_is_better = True)
tnr_score = make_scorer(mlf.tnr_score, greater_is_better = True)
hamm_score = make_scorer(mlf.hamming_score, greater_is_better = True)
if opt_type == "Prec":
    this_scoring = prec_score
elif opt_type == "Acc":
    this_scoring = "accuracy"
elif opt_type == "MCC":
    this_scoring = mcc_score
elif opt_type == "Multi":
    this_scoring = {"Acc":'accuracy', "MCC": mcc_score, "Hamm": hamm_score }
else:
    print("Invalid scoring term")
    sys.exit()

if cv_name == "GSS":
    cv_type = GroupShuffleSplit(n_splits=10)
elif cv_name == "SSS":
    cv_type = StratifiedShuffleSplit(n_splits=10)
else:
    cv_type = 10 #uses KFold by default in the GridSearchCV

fold_names = ['cath_class', 'cath_arch', 'scop_class', 'scop_fold', 'ECOD_arch', 'ECOD_x_poshom', 'ECOD_hom'] #there is data available relatively easily to use another fold type, but we settled on ECOD_arch as the best
if group_name != "ECOD_arch":
    print("Invalid fold selection, you can only pick ECOD_arch")
    sys.exit()

names = ["LogRegr", "Ridge", "PassAggr",
        "QDA", "LDA", "NaiveBayes",
        "NearNeigh",
        "LinSVM", "RBFSVM", "SigSVM",
        "RandomForest", "ExtraTrees", "GradBoost",
        "NeurNet"]

classifiers = [
    LogisticRegression(solver="liblinear", class_weight = "balanced", tol = 1e-4),
    RidgeClassifier(solver="auto", class_weight = "balanced", tol = 1e-4),
    PassiveAggressiveClassifier(class_weight = "balanced", tol = 1e-4),
    QuadraticDiscriminantAnalysis(priors=None), #also almost no real parameters to adjust
    LinearDiscriminantAnalysis(solver = "lsqr"),
    GaussianNB(priors=None), #only one real parameter to adjust!
    KNeighborsClassifier(algorithm = "ball_tree", weights = 'distance'),
    SVC(kernel="linear", max_iter=10000, class_weight = "balanced", tol = 1e-4),
    SVC(kernel="rbf", class_weight = "balanced", tol = 1e-4),
    SVC(kernel="sigmoid", class_weight = "balanced", tol = 1e-4),
    RandomForestClassifier(class_weight = "balanced", min_samples_split = 5, bootstrap = True),
    ExtraTreesClassifier(class_weight = "balanced", min_samples_split = 5, bootstrap = True),
    GradientBoostingClassifier(subsample = 0.8, criterion = 'friedman_mse', min_samples_split = 5, validation_fraction=0.2, n_iter_no_change=5, tol=0.01,),
    MLPClassifier(learning_rate_init = 0.01, early_stopping = True, n_iter_no_change = 5),
]

#read features
wp_scores = pd.read_table("WholeScores_%s.txt"%relaxed) #this now includes only ECOD_arch as the foldID
#Catalytic = 0 are enzymatic, Catalytic = 1 are non-catalytic
wp_scores['Catalytic'] = wp_scores['Catalytic'].apply(lambda x: 0 if x == True else 1) #flipped so that Catalytic are in the "positive" position of CFM
wp_scores = wp_scores.dropna(subset = ["SITE_ID"])
wp_scores.drop_duplicates(inplace = True)
#pull the validation set out separately
valid_set = wp_scores[wp_scores.ValidSet == True]
wp_scores = wp_scores[wp_scores.ValidSet == False]

#split into features and classification
X, y = mlf.subset_data(wp_scores, data_subset, group_name)
valid_set, y_valid = mlf.subset_data(valid_set, data_subset, group_name)
print(len(X), len(valid_set))

parameter_space = [
    { "C": np.logspace(0, 3, 4), "penalty": ["l1", "l2"] }, #LogRegr
    { "alpha": np.logspace(-4, 1, 6) }, #Ridge
    { "C": np.logspace(-1, 3, 5) }, #PassAggr
    { "reg_param": np.linspace(0.5, 1, 7) }, #QDA
    { "shrinkage": ["auto", 0, 0.1, 0.25, 0.5, 0.75, 1] }, #LDA
    { "var_smoothing": np.logspace(-9, 0, 10) }, #Gauss
    [ {"metric": ["minkowski"], "p":[2, 3], "n_neighbors": [5, 8, 10, 15]}, {"metric": ["chebyshev"], "n_neighbors": [5, 8, 10, 15]} ], #kNN
    { "C": np.logspace(-3, 1, 5) }, #SVC lin
    { "C": np.logspace(-3, 1, 5), "gamma": ["scale", "auto"] }, #SVC rbf
    { "C": np.logspace(-3, 1, 5),"gamma": ["scale", "auto"] }, #SVC sig
    { "max_features": ["auto", "sqrt", "log2"], "max_depth": [3, 4, 5, 8, 9, 10], "n_estimators": [100, 500, 1000, 2500, 5000] }, #RF
    { "max_features": ["auto", "sqrt", "log2"], "max_depth": [3, 4, 5, 8, 9, 10], "n_estimators": [100, 500, 1000, 2500, 5000], 'criterion': ["gini", 'entropy'] }, #ExtraTrees
    { "loss": ['deviance', 'exponential'], 'learning_rate': np.logspace(-4, 0, 10), "n_estimators": [100, 500, 1000, 2500, 5000], "max_features": ["auto", "sqrt", "log2"] },#GBClassifier
    { "hidden_layer_sizes": [(50,), (75,), (100,), (150,), (200,), (250,)], "activation": ["relu", "logistic", "tanh"], "alpha": np.logspace(-5, -1, 6) } #MLPClass
]

name = names[name_index]
this_clf = classifiers[name_index]
these_params = parameter_space[name_index]
#if we didn't have a separate validation group
"""outer_cv_results = []
for i, (train_idx, test_idx) in enumerate(cv_type.split(X,y.Catalytic, groups = X["groupID"])):
    print(i)
    X_train, X_test = X.iloc[train_idx], X.iloc[test_idx]
    #print(train_idx[0:10], X_train.head(10))
    y_train, y_test = y.iloc[train_idx], y.iloc[test_idx]
    #print(train_idx, y_train.head(10))

    #scale and sample features
    X_train_new, y_train_new, X_test_scale, fold_ids = scale_resample(X_train, y_train, X_test, over_samp)
    #print(X_train_new[0:10], y_train_new[0:10])
    #print(X_train_new)
    #run feature selection and CV
    clf = GridSearchCV(estimator = this_clf, n_jobs = -1, cv=cv_type, param_grid = these_params, scoring = this_scoring, iid = True, refit = False)
    clf.fit(X_train_new, y_train_new, groups = fold_ids)
    results = clf.cv_results_
    #somehow get best combination of multiple scoring terms
    #print(results)
    ranks = []
    for key in results:
        if "rank_test_" in key:
            ranks.append(results[key])
    best_params = results['params'][np.argmin(np.sum(np.asarray(ranks), axis = 0))]
    #print(best_params) #best params will have to be identified for full data set for final model building after best model is selected
    outer_clf = this_clf.set_params(**best_params) #set the new classifier to these parameters
    outer_clf.fit(X_train_new, y_train_new) #fit on all training data - this is what GridSearchCV(refit = True) will do anyways
    y_test = y_test.assign(Prediction=outer_clf.predict( X_test_scale)) #predict based on fitted outer CV model
    accuracy = accuracy_score(y_test.Catalytic, y_test.Prediction)#, sample_weight = y_test.SampleWeights)
    recall = recall_score(y_test.Catalytic, y_test.Prediction, pos_label=0) #calculates for catalytic = 1 which is now neg
    precision = precision_score(y_test.Catalytic, y_test.Prediction, pos_label=0)#, sample_weight = y_test.SampleWeights)
    true_neg_rate = len( y_test[(y_test.Catalytic == 1) & (y_test.Prediction == 1)] )/ len(y_test[(y_test.Catalytic == 1)])
    hamming = hamming_loss(y_test.Catalytic, y_test.Prediction)
    mcc = matthews_corrcoef(y_test.Catalytic, y_test.Prediction)
    dist_rand = (recall + -1*(1-true_neg_rate)) / np.sqrt(2)

    outer_cv_results.append([ accuracy, precision, recall, true_neg_rate, hamming, mcc, dist_rand ])
    #print([ accuracy, precision, recall, true_neg_rate, hamming, mcc, dist_rand ])
outer_cv_results = np.asarray(outer_cv_results)
print("Not applied to valid set:", outer_cv_results)
#print(np.mean(outer_cv_results, axis = 0))
#print(np.std(outer_cv_results, axis = 0))"""

outer_cv_results = []
for x in range(0,10):
    X_train_new, y_train_new, valid_scale, fold_ids = scale_resample(X, y, valid_set, over_samp)
    #print(X_train_new[0:10], y_train_new[0:10])
    #print(X_train_new)
    #run feature selection and CV
    clf = GridSearchCV(estimator = this_clf, n_jobs = -1, cv=cv_type, param_grid = these_params, scoring = this_scoring, iid = True, refit = False)
    clf.fit(X_train_new, y_train_new, groups = fold_ids)
    results = clf.cv_results_
    #somehow get best combination of multiple scoring terms
    #print(results)
    ranks = []
    for key in results:
        if "rank_test_" in key:
            ranks.append(results[key])
    best_params = results['params'][np.argmin(np.sum(np.asarray(ranks), axis = 0))]
    print(best_params) #best params will have to be identified for full data set for final model building after best model is selected
    outer_clf = this_clf.set_params(**best_params) #set the new classifier to these parameters
    outer_clf.fit(X_train_new, y_train_new) #fit on all training data - this is what GridSearchCV(refit = True) will do anyways, but its selection of params is not necessary mine

    y_valid = y_valid.assign(Prediction=outer_clf.predict( valid_scale)) #predict based on fitted outer CV model
    accuracy = accuracy_score(y_valid.Catalytic, y_valid.Prediction)#, sample_weight = y_valid.SampleWeights)
    recall = recall_score(y_valid.Catalytic, y_valid.Prediction, pos_label=0) #calculates for catalytic = 1 which is now neg
    precision = precision_score(y_valid.Catalytic, y_valid.Prediction, pos_label=0)#, sample_weight = y_valid.SampleWeights)
    true_neg_rate = len( y_valid[(y_valid.Catalytic == 1) & (y_valid.Prediction == 1)] )/ len(y_valid[(y_valid.Catalytic == 1)])
    hamming = hamming_loss(y_valid.Catalytic, y_valid.Prediction)
    mcc = matthews_corrcoef(y_valid.Catalytic, y_valid.Prediction)
    dist_rand = (recall + -1*(1-true_neg_rate)) / np.sqrt(2)
    outer_cv_results.append([ accuracy, precision, recall, true_neg_rate, hamming, mcc, dist_rand ])
    #print([ accuracy, precision, recall, true_neg_rate, hamming, mcc, dist_rand ])
outer_cv_results = np.asarray(outer_cv_results)
#print("No outer CV:", outer_cv_results)

with open("ClassifierScores/%s/ClassifierScores_%s_%s_%s_%s_%s_%s_%s.txt"%(over_samp, relaxed, data_subset, cv_name, group_name[0:4], over_samp, opt_type, name), "w+") as outData:
    outData.write("Classifier\tAccuracy\tPrecision\tRecall\tTrueNegRate\tHammingLoss\tMCC\tDistRand\tAcc_std\tPrec_std\tRecall_std\tTrueNegRate_std\tHammingLoss_std\tMCC_std\tDistRand_std\n")
    outData.write(name + "\t" + "\t".join(map(str, np.mean(outer_cv_results, axis = 0))) + "\t" + "\t".join(map(str, np.std(outer_cv_results, axis = 0))) + "\n")


#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2020/6/15 上午9:51
# @Author  : Boyka
# @Email   : upcvagen@163.com
# @Software: PyCharm

class Solution(object):
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        result = ""
        for i in zip(*strs):
            if len(set(i)) == 1:
                result += i[0]
            else:
                break
        return result

if __name__ == '__main__':
    solution = Solution()
    print(solution.longestCommonPrefix(["abc", "abcd", "azcd", "aacc"]))


#ESSE SCRIPT ESTÁ FUNCIONANDO
#Copyright (c) Jesús Manuel Mager Hois 2016
#https://gist.github.com/pywirrarika/6a8a05e9911cab260fe9de045a99b44e

import numpy as np
from mpl_toolkits.mplot3d import Axes3D

import matplotlib.pyplot as plt

class Perceptron():
    def __init__(self, data):
        self.ciclosx = []
        self.ciclosy = []

        self.d = data
        self.data = np.insert(self.d, len(self.d[0])+2, 1, axis=1)
        self.wn = []

    def plotD(self):
        nd = np.transpose(self.d)

        x3 = nd[0].tolist()
        y3 = nd[1].tolist()
        z3 = nd[2].tolist()

        x3 = x3[0]
        y3 = y3[0]
        z3 = z3[0]

        print(nd[0][0])
        print(nd[1][0])
        print(nd[2][0])

        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')

        ax.scatter(nd[0].tolist(), nd[1].tolist(), nd[2].tolist(), c='r', marker='o')

        ax.set_xlabel('X1') #nomeando eixo x1
        ax.set_ylabel('x2') #nomeando eixo x2
        ax.set_zlabel('x3') #nomeando eixo x3
        plt.title("Dados a classificar")

        plt.show()

    def train(self,convergence = 0.3, output = 1):
        r = convergence
        wn = np.array([1,1,1,r])
        newvec = 0
        classe = 0
        itr = 0
        while True:
            itr = itr + 1
            errors = 0
            j = 0
            if output:
                print("-"*75)
            for x in self.data:
                classereal = 0
                xnwn = 0

                x = np.array(x.tolist()[0])
                for i in range(len(x)):
                    xnwn= xnwn + x[i] * wn[i]
                if j < 5:
                    classereal = 1
                else:
                    classereal = 2
                # Des rule
                if xnwn < 0:
                    classe = 1
                else:
                    classe = 2

                pen = "0"
                if classe == 2 and classe != classereal:
                    newvec = wn - r*x
                    errors = errors + 1
                    pen = "-x"
                if classe == 1 and classe != classereal:
                    newvec = wn + r*x
                    errors = errors + 1
                    pen = "+x"

                if output:
                    print(str(x), "\t|", str(wn), "\t|", str(xnwn), "\t|", str(clase), "\t|", str(clasereal), "\t|", pen)

                wn = newvec

                j = j + 1
            if errors == 0:
                self.wn = np.array(wn)
                break

        print("Número de interações:", str(itr))
        self.ciclosx.append(r)
        self.ciclosy.append(itr)

    def classify(self, vector):
        xnwn = 0
        vector.append(1)
        x=np.array(vector)

        for i in range(len(x)):
            xnwn= xnwn + x[i] * self.wn[i]
        if xnwn < 0:
            classe = 1
        else:
            classe = 2
        print("Classe:", str(classe), str(xnwn), str(vector))
        return classe
    def eval(self):
        C1,C2 = np.split(self.d, 2)
        C=[C1,C2]
        i = 0
        self.confmatrix = np.zeros((len(C), len(C)))
        for Classe in C:
            i += 1
            for vector in Classe:
                vector = vector.tolist()[0]
                disc = self.classify(vector)
                self.confmatrix[i-1][disc-1] += 1
        print("Matriz de confusão por substituição")
        print(self.confmatrix)

        self.data= []
        probe = []
        for Classe in C:
            Classe = np.matrix(Classe)
            A,B = np.split(Classe, 2)
            self.data.append(A)
            probe.append(B)

        self.data = np.insert(self.d, len(self.d[0])+2, 1, axis=1)

        self.train(output=0)
        i=0
        self.confmatrix = np.zeros((len(probe), len(probe)))
        for b in probe:
            i += 1
            for vector in b:
                vector = vector.tolist()[0]
                disc = self.classify(vector)
                self.confmatrix[i-1][disc-1] += 1

        print("Matriz de confusão por Cross Validation")
        print(self.confmatrix)

if __name__ == "__main__":
    data = np.matrix([[0,0,0],
               [1,0,1],
               [1,0,0],
               [1,1,0],
               [0,0,1],
               [0,1,1],
               [0,1,0],
               [1,1,1]])
    neuron = Perceptron(data=data)

    neuron.plotD()
    neuron.train(output=0)
    neuron.eval()


import sys, os, re
import logging
import collections
import kenlm
from spelling_correcter_v2 import Spelling_Correcter
#from spelling_correcter import get_candidates
import time

arpa_path = 'data/lm_binary.arpa'
lm_model = kenlm.LanguageModel(arpa_path)

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(levelname)s %(message)s')

WORDS = []
with open('data/count.txt', 'r') as f:
    for line in f:
        word = line.strip()
        WORDS.append(word)

WORD_COUNTS = collections.Counter(WORDS)

Spell_C = Spelling_Correcter(WORD_COUNTS)

def correct_one(line):
    each_start_time = time.time()
    if ' ' in line.strip():
        utt, hpy = line.strip().split(' ', 1)
    else:
        utt = line.strip()
        hpy = ''
        return line.strip()
    hpy = re.sub(' +', ' ', hpy)
    words = hpy.strip().split(' ')
    for_time_start = time.time()
    for i in range(len(words)):
        #logging.info('words_num {}'.format(len(words)))
        each_for_time_start = time.time()
        word = words[i]
        ## 数字不调整
        #if word.isnumeric()
        #    continue
        candidates = list(Spell_C.get_candidates(word))
        #candidates = list(get_candidates(word))
        #logging.info('get candidates time {} {} {} {}'.format(utt, word, len(candidates), time.time() - each_for_time_start))
        if len(candidates) == 1:
            words[i] = candidates[0]
        else:
            max_lm_score = float('-inf')
            for candidate in candidates:
                cand_start_time = time.time()
                if i == 0:
                    if len(words) == 1:
                        tmp_hpy = candidate
                    else:
                        tmp_hpy = ' '.join([candidate, words[i+1]])
                elif i + 1 < len(words):
                    tmp_hpy = ' '.join([words[i-1], candidate, words[i+1]])
                else:
                    tmp_hpy = ' '.join([words[i-1], candidate])
                cand_end_time = time.time()

                lm_score = lm_model.score(tmp_hpy.upper())
                if lm_score > max_lm_score:
                    max_lm_score = lm_score
                    candidate_dict = {}
                    candidate_dict[str(candidate)] = lm_score
                lm_end_time = time.time()
                #logging.info('{} candidates_time: {}, lm_time: {}'.format(utt, cand_end_time-cand_start_time, lm_end_time-cand_end_time))
                    #print(tmp_hpy, lm_score)
            words[i] = list(candidate_dict.keys())[0]
        each_for_time_end = time.time()
        #if each_for_time_end - each_for_time_start > 1e-04:
        #    logging.info('bad word: {}'.format(word))
        #logging.info('each_for_time {} {} {}'.format(utt, word, (each_for_time_end - each_for_time_start)))
    for_time_end = time.time()
    #logging.info('for_time {} {}'.format(utt, (for_time_end - for_time_start)))
    result = utt + ' ' + (' '.join(words)).upper()
    each_end_time = time.time()
    #logging.info('each_time {} {}'.format(utt, (each_end_time-each_start_time)))
    return result

if __name__ == '__main__':
    hpy_txt, hpy_fix_txt = sys.argv[1:3]

    start_time = time.time()
    count = ''
    with open(hpy_txt, 'r') as f:
        for line in f:
            #print(line.strip())
            hpy_fix = correct_one(line)
            count += hpy_fix + '\n'

    with open(hpy_fix_txt, 'w') as f:
        f.write(count)

    end_time = time.time()
    logging.info('total_time: {}'.format(end_time - start_time))


from aiogram.types import Message, CallbackQuery
from aiogram.dispatcher import FSMContext
from aiogram.utils.exceptions import ChatNotFound
import asyncio

from tgbot.database.db import Database
from tgbot.states.admin import SpamState
from tgbot.states.admin import BalanceState
from tgbot.markups.admin import *

db = Database('database.sql')

#Рассылка
async def spam_start(call):
    await call.message.edit_text('Напишите сообщение для рассылки:', reply_markup=back_main)
    await SpamState.waiting_for_spam_text.set()

async def back_to_main(call, state: FSMContext):
    await state.reset_state()  # сбрасывается состояние ожидания сообщения
    await call.message.edit_text('Вы вернулись в главное меню:', reply_markup=main_menu)

async def get_spam_text(message: Message, state: FSMContext):
    text = message.text
    await state.finish()
    users = db.get_users()
    for row in users:
        user_id = row[0]
        try:
            chat = await message.bot.get_chat(user_id)
            await message.bot.send_message(chat.id, text, parse_mode="HTML")
        except ChatNotFound:
            print(f"User {user_id} not found")
        except Exception as e:
            print(f"Failed to send message to user {user_id}. Error: {e}")
    sent_message = await message.answer('Рассылка прошла успешно')
    await asyncio.sleep(1)
    await message.bot.delete_message(chat_id=sent_message.chat.id, message_id=sent_message.message_id)
    await message.answer('Вы в админ-панели', reply_markup=main_menu)

#рассылка с помощью команды
async def spam_cmd(message:Message):
    text = message.text[6:]
    users = db.get_users()
    for row in users:
        user_id = row[0]
        try:
            chat = await message.bot.get_chat(user_id)
            await message.bot.send_message(chat.id, text, parse_mode="Markdown")
        except ChatNotFound:
            print(f"User {user_id} not found")
        except Exception as e:
            print(f"Failed to send message to user {user_id}. Error: {e}")
    await message.answer('Рассылка прошла успешно')

#---Меню пользователи---
#Обработка кнопки пользователи
async def users_call(call):
    quantity = db.get_user_count()
    await call.message.edit_text(f'Количество активных пользователей: {quantity}', reply_markup=users_menu)

#Обработка кнопки активные пользователи
async def active_users_call(call):
    keyboard = create_keyboard()
    await call.message.edit_text('Активные пользователи', reply_markup=keyboard)

#Обработка кнопки забаненные пользователи
async def banned_users_call(call):
    keyboard = create_ban_keyboard()
    await call.message.edit_text('Активные пользователи', reply_markup=keyboard)

async def back_to_main_menu(call):
    await call.message.edit_text('Вы в админ-панели:', reply_markup=main_menu)

#нажатие кнопки назад в блоке ниже меню пользователей(возврат в меню пользователей)
async def back_to_users_call(call):
    await call.message.edit_text('Вы в админ-панели:', reply_markup=users_menu)

#нажатие на кнопку пользователя в блоке активные пользователи
async def user_info_call(call):
    global dm_user_id
    dm_user_id = call.data.split('_')[1]
    user = db.get_user(dm_user_id)
    global user_info_text
    user_info_text = f"{user[1]} (@{user[2]}) - зарегистрирован {user[3],} баланс {user[4]}"

    ban_button = InlineKeyboardButton('Забанить', callback_data=f'ban_user_{dm_user_id}')
    direct_message = InlineKeyboardButton('Личное Сообщение', callback_data='direct_message')
    add_balance =  InlineKeyboardButton('Добавить баланс', callback_data='add_balance')
    back_to_users = InlineKeyboardButton('Назад', callback_data='back_to_users')

    global users_func
    users_func = InlineKeyboardMarkup().add(ban_button, direct_message).add(back_to_users, add_balance)

    await call.message.edit_text(text=call.message.text + f"\n\n{user_info_text}", reply_markup=users_func)

async def add_balance(call):
    await call.message.edit_text('Пополнить баланс на:')
    await BalanceState.waiting_for_add_balance.set()

async def wait_add(message: Message, state: FSMContext):
    user_id = message.from_user.id
    amount = int(message.text)
    await state.finish()
    db.add_balance(user_id, amount)
    await message.answer('Баланс успешно пополнен!')

async def banned_user_info_call(call):
    user_id = call.data.split('_')[2]
    user = db.get_banned_user(user_id)

    text = f"{user[1]} (@{user[2]}) - зарегистрирован {user[3]}"

    ban_button = InlineKeyboardButton('Разбанить', callback_data=f'unban_user_{user_id}')
    back_to_users = InlineKeyboardButton('Назад', callback_data='back_to_banned_users')
    global ban_users_func
    ban_users_func = InlineKeyboardMarkup().add(ban_button).add(back_to_users)

    await call.message.edit_text(text=call.message.text + f"\n\n{text}", reply_markup=ban_users_func)

#обработчик кнопки назад (возврат к клавиатуре с активными польователями)
async def user_back(call):
    keyboard = create_keyboard()
    await call.message.edit_text('Список пользователей:', reply_markup=keyboard)

#тоже самое что выше, только в забанненых
async def banned_user_back(call):
    keyboard = create_ban_keyboard()
    await call.message.edit_text('Список пользователей:', reply_markup=keyboard)

#обработчик кнопки Забанить
async def ban_user(call):
    user_id = call.data.split('_')[2]
    chat = await call.bot.get_chat(user_id)
    username = chat.username

    db.ban_user(user_id)

    sent_message = await call.message.answer(f"Пользователь @{username} забанен!")
    await asyncio.sleep(3)
    await call.bot.delete_message(chat_id=sent_message.chat.id, message_id=sent_message.message_id)

async def unban_user(call):
    user_id = call.data.split('_')[2]
    print(user_id)
    chat = await call.bot.get_chat(user_id)
    username = chat.username

    db.unban_user(user_id)

    sent_message = await call.message.answer(f"Пользователь @{username} разбанен!")
    await asyncio.sleep(3)
    await call.bot.delete_message(chat_id=sent_message.chat.id, message_id=sent_message.message_id)

#обработчик кнопки личное сообщение пользователю

async def direct_message(call):
    await call.message.edit_text('Напишите сообщение для рассылки:', reply_markup=back_users_func)
    await SpamState.waiting_for_dm_text.set()

async def get_direct_message_text(message: Message, state: FSMContext):
    dm_text = message.text
    await state.finish()
    user_id = dm_user_id
    try:
        chat = await message.bot.get_chat(user_id)
        await message.answer(chat.id, dm_text, parse_mode="HTML")
    except ChatNotFound:
        print(f"User {user_id} not found")
    except Exception as e:
        print(f"Failed to send message to user {user_id}. Error: {e}")

    sent_message = await message.answer("Личное сообщение пользователю отправлено.")
    await asyncio.sleep(1)
    await message.bot.delete_message(chat_id=sent_message.chat.id, message_id=sent_message.message_id)
    await message.answer('Вы в админ-панели', reply_markup= main_menu)

async def back_to_user_func(call, state: FSMContext):
    await state.reset_state()  # сбрасывается состояние ожидания сообщения
    await call.message.edit_text(user_info_text, reply_markup=users_func)


from ntpath import join
from services.current_machine_ip import getIp;
from linqit import List;
from services.instant_proxies_service import *;
from services.data_text_file_service import *;
from datetime import datetime, timedelta;
from services.data_file_service import InstantProxiesDataFileService;

# Lay dia chi IP cua may tinh hien tai
currentMachineIp = getIp();

# Khoi tao chuc nang ghi danh sach IP cho phep va proxy duoc cung cap
dataFileService = InstantProxiesDataFileService();

# Kiem tra xem dia chi IP dang su dung co nam trong danh sach ip duoc cho phep da luu truoc do khong
authedIpList = dataFileService._ReadAuthedIpsFromDataFile();
if authedIpList != None and authedIpList.contains(currentMachineIp):
    # Neu dia chi IP hien tai cua may da co trong danh sach duoc cho phep
    pass;
else:
    # Neu dia chi IP chua them vao danh sach cho phep thi tien hanh vao trang InstantProxies de them
    SignInInstantProxies();
    # Trang InstantProxies tu dong phat hien dia chi IP dang su dung nen khong can truyen vao dia chi IP cua may hien tai
    newAuthedIpList = AddMachineIpToWebAuthedIpList();
    dataFileService._WriteAuthedIpsToDataFile(newAuthedIpList);
    SignOutInstantProxies();

# Kiem tra danh sach proxy da luu proxy nao hay chua
forCrawlDataProxyList = List();
proxyListData = dataFileService._ReadProvidedProxiesFromDataFile();
if proxyListData != None:
    writeTime, proxyListCount, providedProxyList = proxyListData;
    # Neu thoi gian luu file proxy den hien tai qua 24 gio thi vao web InstantProxies de lay danh sach proxy moi: https://stackoverflow.com/a/26313848/7182661
    if (datetime.now() - writeTime) < timedelta(hours=24):
        # less than 24 hours passed
        forCrawlDataProxyList = providedProxyList;
    else:
        # Lam moi danh sach proxy tu web InstantProxies
        SignInInstantProxies();
        newProvidedProxyList = GetProxyListOnWeb();
        dataFileService._WriteProvidedProxiesToDataFile(newProvidedProxyList);
        SignOutInstantProxies();
        forCrawlDataProxyList = newProvidedProxyList;
else:
    # Lam moi danh sach proxy tu web InstantProxies
    SignInInstantProxies();
    newProvidedProxyList = GetProxyListOnWeb();
    dataFileService._WriteProvidedProxiesToDataFile(newProvidedProxyList);
    SignOutInstantProxies();
    forCrawlDataProxyList = newProvidedProxyList;

print("Danh sach proxy duoc su dung de crawl du lieu: " + " ".join(forCrawlDataProxyList));


import pandas as pd
import matplotlib.pyplot as plot

df1 = pd.read_excel('ОАД1.xlsx', sheet_name='Справочник компаний')
df2 = pd.read_excel('ОАД1.xlsx', sheet_name='Результаты опроса о доходах')
df3 = pd.read_excel('ОАД1.xlsx', sheet_name='Информация о доходах')
'''перевод таблиц в датафрейм'''
"""print(len(df1.index))"""
i = 0
'''в эту фукнцию будем передавать таблицу с реальными зарплатами сотрудников'''
companies_green = ['ООО "ШАД-111"', 'ГРИНРУС-АТОМ', 'АО "Зелёная энергия"', 'ОАО "ГРИНТРАНПСОРТ"',
                   'АО "Эко-системы управления на транспорте"', 'ИП Иванов И.Б.',
                   'АО "ПИТС"', 'ООО "КБ-14"']

def f(df):
    sal_for_com = [0] * 10
    i = 0
    while i <= (len(df.index)):
        sal_for_per = 0
        # i это номер первого квартала
        if df['id-компании'].loc[df.index[i]] <= 8:
            for j in range(i, i + 4):
                sal_for_per += df["Сумма оклада за период (до вычета налогов)"].loc[df.index[j]] * (
                            100 - df['Величина налога,%'].loc[df.index[i]]) / 100
            ind = df.index[i]
            if df['id-компании'].loc[ind] == 1:
                sal_for_com[0] += sal_for_per
            if df['id-компании'].loc[ind] == 2:
                sal_for_com[1] += sal_for_per
            if df['id-компании'].loc[ind] == 3:
                sal_for_com[2] += sal_for_per
            if df['id-компании'].loc[ind] == 4:
                sal_for_com[3] += sal_for_per
            if df['id-компании'].loc[ind] == 5:
                sal_for_com[4] += sal_for_per
            if df['id-компании'].loc[ind] == 6:
                sal_for_com[5] += sal_for_per
            if df['id-компании'].loc[ind] == 7:
                sal_for_com[6] += sal_for_per
            if df['id-компании'].loc[ind] == 8:
                sal_for_com[7] += sal_for_per
            if df['id-компании'].loc[ind] == 9:
                sal_for_com[8] += sal_for_per
            if df['id-компании'].loc[ind] == 10:
                sal_for_com[9] += sal_for_per
        if i + 4 + 2 >= len(df.index):
            break
        i += 4
    return sal_for_com

# здесь работаем с таблицей с опросом
def opros_f(df):
    sal_for_com = [0] * 10
    i = 0
    while i < (len(df.index)):
        sal_for_per = 0
        ind = df.index[i]
        if df['Компания'].loc[df.index[i]] in companies_green:
            sal_for_per = df['Годовой доход после вычета налогов'].loc[ind]
            if df['Компания'].loc[df.index[i]] == companies_green[0]:
                sal_for_com[0] += sal_for_per
            if df['Компания'].loc[df.index[i]] == companies_green[1]:
                sal_for_com[1] += sal_for_per
            if df['Компания'].loc[df.index[i]] == companies_green[2]:
                sal_for_com[2] += sal_for_per
            if df['Компания'].loc[df.index[i]] == companies_green[3]:
                sal_for_com[3] += sal_for_per
            if df['Компания'].loc[df.index[i]] == companies_green[4]:
                sal_for_com[4] += sal_for_per
            if df['Компания'].loc[df.index[i]] == companies_green[5]:
                sal_for_com[5] += sal_for_per
            if df['Компания'].loc[df.index[i]] == companies_green[6]:
                sal_for_com[6] += sal_for_per
            if df['Компания'].loc[df.index[i]] == companies_green[7]:
                sal_for_com[7] += sal_for_per
        i += 1
    return sal_for_com

opros_salary = [int(x) for x in opros_f(df2)[:-2] ]
real_salary = [int(x) for x in f(df3)[:-2] ]
print(real_salary)
print(opros_salary)

# average salary
s1 = []
s2 = []
for i in range(len(df1.index)-2):
    s1.append(int(real_salary[i] / df1['Среднесписочная численность'].loc[df1.index[i]]))
    s2.append(int(opros_salary[i] / df1['Среднесписочная численность'].loc[df1.index[i]]))
print(s1)
print(s2)

#средний доход, медианный доход, минимальный и максимальный
plot.figure(figsize=(10,10))
plot.bar(companies_green,s1,label = 'Real_salary',width=0.7,bottom =50,align ='edge')
plot.bar(companies_green,s2,label = 'Opros_salary',width=0.7,bottom =50,align ='edge' )

plot.legend()
plot.show()


#!/usr/bin/env python

# 36. グループ分けを行うプログラムを作成せよ．

import sys
import random
import pandas as pd
import numpy as np

# 引数チェック
argvs = sys.argv
argc = len(argvs)

if argc!=3:
	print("Usage: makeGroup クラス人数 1班の人数")
	sys.exit(1)

classNum = int(argvs[1])
groupNum = int(argvs[2])

# ランダムシャッフル
member = [i+1 for i in range(classNum)]
random.seed(0)
random.shuffle(member)

# 班番号生成
group = []
for i in range(int(classNum/groupNum)):
	group.extend([i+1]*groupNum)

# pandasに投げて，memberでソートする
df = pd.DataFrame([member, group]).T
df.columns = ['member','group']
df = df.sort_values(by='member')
df = df.reset_index(drop=True)


def compare(x, y):
    w = ""
    if x == y:
        w = ("n is equal m:  " + str(x))
    elif x > y:
        w = (str(x) + "  is greater than  " + str(y))
    elif x < y:
        w = (str(x) + "  is smaller than  " + str(y))

    return w

t = int(input())
n = []
for i in range(t):
    d = input()
    d = d.split()
    n.append(d[0])
    n.append(d[1])
g = 0
for j in range(t):
    print(compare(int(n[g]), int(n[g+1])))
    g += 2
# 2
# 3 5
# 8 9


table = {
    "a": "According",
    "b": "To",
    "c": "All",
    "d": "Known",
    "e": "Laws",
    "f": "Of",
    "g": "Aviation",
    "h": "There",
    "i": "Is",
    "j": "No",
    "k": "Way",
    "l": "A",
    "m": "Bee",
    "n": "Should",
    "o": "Be",
    "p": "Able",
    "q": "Fly",
    "r": "Its",
    "s": "Wings",
    "t": "Are",
    "u": "Too",
    "v": "Small",
    "w": "Get",
    "x": "Fat",
    "y": "Little",
    "x": "Body",
    "1": "Off",
    "2": "The",
    "3": "Ground",
    "4": "Course",
    "5": "Flies",
    "6": "Anyway",
    "7": "Because",
    "8": "Bees",
    "9": "Do",
    "0": "Not",
    " ": "Care",
    ".": "What",
    ",": "Humans",
    "-": "Think",
    ":": "Posible",
    ";": "Yellow",
    "/": "Black",
    "?": "Ooh",
    "!": "And"
}

def encrypt(string):
    try:
        lowered = string.lower()
        listed = [*lowered]
    except:
        raise Exception('The input was not valid')

    encrypted = ""
    for i in listed:
        if i in table:
            encrypted += table[i]
        else:
            encrypted += "Barry"
    return encrypted


""" Ejercicio 1 """

alumnos_clase = ["María", "José", "Carlos", "Martina", "Isabel", "Tomás", "Daniela"]

for alumno in alumnos_clase:
    print(f"Hola {alumno}")

""" Ejercicio 2 """

lista_numeros = [1,5,8,7,6,8,2,5,2,6,4,8,5,9,8,3,5,4,2,5,6,4]
suma_numeros = 0

for n in lista_numeros:
    suma_numeros = suma_numeros + n

print(suma_numeros)

""" Ejercicio 3 """

suma_pares = 0
suma_impares = 0

for n in lista_numeros:
    if n % 2 == 0:
        suma_pares = suma_pares + n
    else :
        suma_impares = suma_impares + n

print(suma_pares)
print(suma_impares)


'''

'''

import math

def xor_sum(n):
    if n%4 == 0:
        return n
    elif n%4 == 1:
        return 1
    elif n%4 == 2:
        return n+1
    else:
        return 0

def find_num_seq(n, a):
    x = 0
    for i in range(n):
        x ^= a[i]
    return xor_sum(x)

def main():
    t = int(input())
    for i in range(t):
        n = int(input())
        a = [int(x) for x in input().split()]
        print(find_num_seq(n, a))

if __name__ == '__main__':
    main()


#!/usr/bin/python3
# coding=UTF-8

import sys
import time
import http.client as httplib
import sqlite3
from lxml import etree

# print(str(dbPointer))
# dbPointerr = common.getDBPointer()
# print(str(dbPointerr))

class SqliteWrapper:
    ''' docstring for SqliteWrapper '''
    def __init__(self, tableName):
        self.fundb = sqlite3.connect("./dji.db")
        self.fundb.text_factory = str
        self.dbPointer = self.fundb.cursor()
        self.tableName = tableName
        sql = "CREATE TABLE IF NOT EXISTS " + self.tableName + \
            " (postDate TEXT, postBy VARCHAR(20), device TEXT, postTitle TEXT, postLink TEXT UNIQUE, visitTimes INT, commentTimes INT, updateTime VARCHAR(20))"
        self.dbPointer.execute(sql)

    def fmtDate(self, dateStr):
        if(len(str(dateStr.split()[0]).split('-')) < 3):
            dateStr = str(time.localtime(time.time()).tm_year) + "-" + dateStr.split()[0]
        else:
            dateStr = dateStr.split()[0]
        t = time.strptime(dateStr, "%Y-%m-%d")
        return time.strftime("%Y-%m-%d", t)

    def fmtDateTime(self, dateStr):
        if(len(str(dateStr.split()[0]).split('-')) < 3):
            dateStr = str(time.localtime(time.time()).tm_year) + "-" + dateStr
        else:
            dateStr = dateStr
        t = time.strptime(dateStr, "%Y-%m-%d %H:%M")
        return time.strftime("%Y-%m-%d %H:%M", t)

    # save data to sqlite db
    def saveData(self, postDate, postBy, device, postTitle, postLink, visitTimes, commentTimes, updateTime):
        postDate = self.fmtDate(postDate)
        updateTime = self.fmtDateTime(updateTime)
        # sql = "CREATE TABLE IF NOT EXISTS " + self.tableName + \
        #     " (postDate TEXT, postBy VARCHAR(20), device TEXT, postTitle TEXT, postLink TEXT UNIQUE, visitTimes INT, commentTimes INT, updateTime VARCHAR(20))"
        # self.dbPointer.execute(sql)

        # INSERT data
        # print("deal with: %s, tableName: %s" %(date, tableName))
        try:
            self.dbPointer.execute("INSERT INTO " + self.tableName + \
                    # " VALUES (?, ?, ?, ?, ?)", (date(postDate), postBy, visitTimes, commentTimes, datetime(updateTime)))
                    " VALUES (?, ?, ?, ?, ?, ?, ?, ?)", (postDate, postBy, device, postTitle, postLink, visitTimes, commentTimes, updateTime))
        except:
            pass
            # 修改已经存在的值

        self.fundb.commit()

    def getLastestDate(self):
        self.dbPointer.execute("SELECT * FROM " + self.tableName + " ORDER BY DATE(postDate) DESC")
        return self.dbPointer.fetchone()

    def getRowNum(self):
        self.dbPointer.execute("SELECT COUNT(*) FROM " + self.tableName)
        return self.dbPointer.fetchone()[0]

    # dump data from db
    def dumpDb(self, tableName):
        pass
        self.dbPointer.execute("SELECT * FROM " + tableName)
        while(1):
            result = dbPointer.fetchone()
            if (result != None):
                print(result)
            else:
                break

    def onSale(self, startDate, endDate, onSaleTime):
        if (startDate < onSaleTime) and (onSaleTime < endDate):
            return True
        else:
            return False

    def getNoAirItem(self, itemName, startDate, endDate):
        # self.dbPointer.execute('SELECT * FROM ' + self.tableName + ' where postDate between "' \
        self.dbPointer.execute('SELECT ' + itemName + ' FROM ' + self.tableName + ' where postDate between "' \
                + startDate + '" and "' + endDate + '" and postDate != "' + endDate +  '" and device != "mavic air" ORDER BY postDate DESC')
        itemNames = list(self.dbPointer.fetchall())

        result = []
        for item in itemNames:
            result.append(item[0])
            # result.append(item)
        return result

    def getItem(self, itemName, startDate, endDate):
        # self.dbPointer.execute('SELECT * FROM ' + self.tableName + ' where postDate between "' \
        self.dbPointer.execute('SELECT ' + itemName + ' FROM ' + self.tableName + ' where postDate between "' \
                + startDate + '" and "' + endDate + '" and postDate != "' + endDate +  '" ORDER BY postDate DESC')
        itemNames = list(self.dbPointer.fetchall())

        result = []
        for item in itemNames:
            result.append(item[0])
            # result.append(item)
        return result

    def getDevice(self, startDate, endDate):
        startDate = self.fmtDate(startDate + "-01")
        endDate = self.fmtDate(endDate + "-01")
        # print(startDate)
        # print(endDate)
        airOnSaleTime = "2018-02-01"
        result = []
        if(self.onSale(startDate, endDate, airOnSaleTime)):
            result.extend(self.getNoAirItem("device", startDate, airOnSaleTime))
            result.extend(self.getItem("device", airOnSaleTime, endDate))
        elif (endDate < airOnSaleTime):
            result.extend(self.getNoAirItem("device", startDate, endDate))
        else:
            result.extend(self.getItem("device", startDate, endDate))

        return result

    def getPostLink(self, startDate, endDate):
        startDate = self.fmtDate(startDate + "-01")
        endDate = self.fmtDate(endDate + "-01")
        # print(startDate)
        # print(endDate)
        airOnSaleTime = "2018-02-01"
        result = []
        if(self.onSale(startDate, endDate, airOnSaleTime)):
            result.extend(self.getNoAirItem("postLink", startDate, airOnSaleTime))
            result.extend(self.getItem("postLink", airOnSaleTime, endDate))
        elif (endDate < airOnSaleTime):
            result.extend(self.getNoAirItem("postLink", startDate, airOnSaleTime))
        else:
            result.extend(self.getItem("postLink", startDate, endDate))

        return result


#!/usr/bin/env python

import os
from setuptools import setup
from django_numerators import __version__

def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

with open("README.md", "r") as fh:
    long_description = fh.read()

setup(
    name='django-products',
    version=__version__,
    description='Django polymorphic products, tests included',
    long_description=long_description,
    long_description_content_type="text/markdown",
    maintainer='Rizki Sasri Dwitama',
    maintainer_email='sasri.project@gmail.com',
    license="MIT",
    url='https://github.com/sasriawesome/django_products',
    packages=[
        'django_products',
        'django_products.migrations',
        'django_products.utils',
    ],
    install_requires=[
        'Django>=2.2',
    ],
    classifiers=[
        "Development Status :: 3 - Alpha",
        "Environment :: Web Environment",
        "Framework :: Django",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: BSD License",
        "Operating System :: OS Independent",
        "Programming Language :: Python",
        "Topic :: Software Development :: Libraries :: Python Modules",
    ],
    tes_suite="tests.run_tests.run_tests"
)


"""A hook for adding information to the user infobox."""

from __future__ import annotations

from django.template.loader import render_to_string
from djblets.extensions.hooks import ExtensionHook, ExtensionHookPoint

class UserInfoboxHook(ExtensionHook, metaclass=ExtensionHookPoint):
    """A hook for adding information to the user infobox.

    Extensions can use this hook to add additional pieces of data to the box
    which pops up when hovering the mouse over a user.
    """

    def initialize(self, template_name=None):
        """Initialize the hook.

        Args:
            template_name (unicode):
                The template to render with the default :py:func:`render`
                method.
        """
        self.template_name = template_name

    def get_extra_context(self, user, request, local_site, **kwargs):
        """Return extra context to use when rendering the template.

        This may be overridden in order to make use of the default
        :py:func:`render` method.

        Args:
            user (django.contrib.auth.models.User):
                The user whose infobox is being shown.

            request (django.http.HttpRequest):
                The request for the infobox view.

            local_site (reviewboard.site.models.LocalSite):
                The local site, if any.

            **kwargs (dict):
                Additional keyword arguments.

        Returns:
            dict:
            Additional context to include when rendering the template.
        """
        return {}

    def get_etag_data(self, user, request, local_site, **kwargs):
        """Return data to be included in the user infobox ETag.

        The infobox view uses an ETag to enable browser caching of the content.
        If the extension returns data which can change, this method should
        return a string which is unique to that data.

        Args:
            user (django.contrib.auth.models.User):
                The user whose infobox is being shown.

            request (django.http.HttpRequest):
                The request for the infobox view.

            local_site (reviewboard.site.models.LocalSite):
                The local site, if any.

            **kwargs (dict):
                Additional keyword arguments.

        Returns:
            unicode:
            A string to be included in the ETag for the view.
        """
        return ''

    def render(self, user, request, local_site, **kwargs):
        """Return content to include in the user infobox.

        This may be overridden in the case where providing a custom template
        and overriding :py:func:`get_extra_context` is insufficient.

        Args:
            user (django.contrib.auth.models.User):
                The user whose infobox is being shown.

            request (django.http.HttpRequest):
                The request for the infobox view.

            local_site (reviewboard.site.models.LocalSite):
                The local site, if any.

            **kwargs (dict):
                Additional keyword arguments.

        Returns:
            django.utils.safestring.SafeText:
            Text to include in the infobox HTML.
        """
        assert self.template_name is not None

        context = {
            'extension': self.extension,
            'user': user,
        }
        context.update(self.get_extra_context(user=user,
                                              request=request,
                                              local_site=local_site))
        return render_to_string(
            template_name=self.template_name,
            context=context,
            request=request)


# -*- coding:utf-8 -*-
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse, HttpResponseRedirect,JsonResponse
from django.shortcuts import redirect,render
from django.contrib.auth.decorators import login_required
from django.template.loader import get_template
from p_mes.models import SumReport
from django.db import connection
from django.db.models import Avg,Sum,Count
import time,datetime,json,pyodbc

# 日期转换
def timestr(str,tf):
    tsm=int(time.mktime(time.strptime(str,"%Y-%m-%d"))) + (tf*24*3600)
    return time.strftime("%Y-%m-%d",time.localtime(tsm))

#json自定义编码
class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, bytes):
            return str(obj, encoding='utf-8');
        elif isinstance(obj,pyodbc.Row):
            return str(list(obj)[0])
        return json.JSONEncoder.default(self, obj)

#SQL转JSON
def dictfetchall(cursor):
    columns = [col[0] for col in cursor.description]
    return [dict(zip(columns, row))for row in cursor.fetchall()]

#战情资讯
@login_required
@csrf_exempt
def dashboard(request):
    userCHNname =request.user.first_name
    username =request.user.username

    #局部刷新
    if  request.is_ajax():
        content={}
        dep=request.POST.get('dep')
        cell=request.POST.get('cell')
        std=request.POST.get('std')
        etd=request.POST.get('etd')
        dst=request.POST.get('dst')
        sle=request.POST.get('sle')
        if len(etd)==0:
            etd='2018-01-01'
            std='2018-01-01'
            std2='2018-01-01'
        std2=timestr(etd,-28)
        if (std2>std):
            std2=std
        if dst=='Weekly':
            sql2="select datepart(wk,WorkingDate) as WorkingDate,cast(sum(CompleteQty) as int) as FG,cast(sum(PQ) as int) as PQ,cast(cast(avg(UPPH) as numeric(8,2)) as varchar(8)) as UPPH,cast(avg(URT)*100 as int) as URT,cast(avg(PPM) as int) as PPM,cast(100*avg(FCY) as int) as FCY,cast(avg(Efficiency)*100 as int) as EFY,cast(avg(Productivity)*100 as int) as ACR from p_mes_sumreport where PL='"+ dep +"' and WorkingDate between '"+ std2 +"' and '"+ etd +"' group by datepart(wk,WorkingDate) order by datepart(wk,WorkingDate)"
            sql3="select datepart(wk,WorkingDate) as WorkingDate,cast(sum(CompleteQty) as int) as FG,cast(sum(PQ) as int) as PQ,cast(cast(avg(UPPH) as numeric(8,2)) as varchar(8)) as UPPH,cast(avg(URT)*100 as int) as URT,cast(avg(PPM) as int) as PPM,cast(100*avg(FCY) as int) as FCY,cast(avg(Efficiency)*100 as int) as EFY,cast(avg(Productivity)*100 as int) as ACR from p_mes_sumreport where Cell='"+ cell +"' and WorkingDate between '"+ std2 +"' and '"+ etd +"' group by datepart(wk,WorkingDate) order by datepart(wk,WorkingDate)"
        elif dst=='Monthly':
            sql2="select month(WorkingDate) as WorkingDate,cast(sum(CompleteQty) as int) as FG,cast(sum(PQ) as int) as PQ,cast(cast(avg(UPPH) as numeric(8,2)) as varchar(8)) as UPPH,cast(avg(URT)*100 as int) as URT,cast(avg(PPM) as int) as PPM,cast(100*avg(FCY) as int) as FCY,cast(avg(Efficiency)*100 as int) as EFY,cast(avg(Productivity)*100 as int) as ACR from p_mes_sumreport where PL='"+ dep +"' and WorkingDate between '"+ std2 +"' and '"+ etd +"' group by month(WorkingDate) order by month(WorkingDate)"
            sql3="select month(WorkingDate) as WorkingDate,cast(sum(CompleteQty) as int) as FG,cast(sum(PQ) as int) as PQ,cast(cast(avg(UPPH) as numeric(8,2)) as varchar(8)) as UPPH,cast(avg(URT)*100 as int) as URT,cast(avg(PPM) as int) as PPM,cast(100*avg(FCY) as int) as FCY,cast(avg(Efficiency)*100 as int) as EFY,cast(avg(Productivity)*100 as int) as ACR from p_mes_sumreport where Cell='"+ cell +"' and WorkingDate between '"+ std2 +"' and '"+ etd +"' group by month(WorkingDate) order by month(WorkingDate)"
        else:
            sql2="select right(convert(varchar(12),WorkingDate,112),4) as WorkingDate,cast(sum(CompleteQty) as int) as FG,cast(sum(PQ) as int) as PQ,cast(cast(avg(UPPH) as numeric(8,2)) as varchar(8)) as UPPH,cast(avg(URT)*100 as int) as URT,cast(avg(PPM) as int) as PPM,cast(100*avg(FCY) as int) as FCY,cast(avg(Efficiency)*100 as int) as EFY,cast(avg(Productivity)*100 as int) as ACR from p_mes_sumreport where PL='"+ dep +"' and WorkingDate between '"+ std2 +"' and '"+ etd +"' group by WorkingDate order by WorkingDate"
            sql3="select right(convert(varchar(12),WorkingDate,112),4) as WorkingDate,cast(sum(CompleteQty) as int) as FG,cast(sum(PQ) as int) as PQ,cast(cast(avg(UPPH) as numeric(8,2)) as varchar(8)) as UPPH,cast(avg(URT)*100 as int) as URT,cast(avg(PPM) as int) as PPM,cast(100*avg(FCY) as int) as FCY,cast(avg(Efficiency)*100 as int) as EFY,cast(avg(Productivity)*100 as int) as ACR from p_mes_sumreport where Cell='"+ cell +"' and WorkingDate between '"+ std2 +"' and '"+ etd +"' group by WorkingDate order by WorkingDate"

        if sle=='1':
        #by dep
            sql="select b.name as cell  from p_admin_department a inner join p_admin_department b on a.code=b.parent_id where b.category='C' and a.name='" + dep + "' order by  b.name"
            sql5="select case when len(FailureMode)=0 then 'NA' else FailureMode end as FM,sum(FailQty) as Qty from MES2017.dbo.v_FQC_Detail where Qdate between '"+ std2 +"' and '"+ etd +"' and PL='" + dep + "'  and PPM>0 group by FailureMode order by sum(FailQty) desc"
            cells=dictfetchall(connection.cursor().execute(sql))
            DQ=SumReport.objects.filter(PL='' + dep + '',WorkingDate__range=('' + std + '', ''+etd+'')).values('PL').annotate(FG=Sum('CompleteQty'),PQ=Sum('PQ'),UPPH=Avg('UPPH'),URT=Avg('URT'),PPM=Avg('PPM'),FCY=Avg('FCY'),ACR=Avg('Productivity'),EFY=Avg('Efficiency')).values('UPPH','FCY','PPM','URT','ACR','EFY','FG','PQ')
        else:
            if sle=='3':
                #update datesum to mes2018
                mes_refresh="exec P_ReportSum '"+ std +"','"+ etd +"'"
                connection.cursor().execute(mes_refresh)
        #by cell,std,etd
            if cell=='CellSUM':
                sql5="select case when len(FailureMode)=0 then 'NA' else FailureMode end as FM,sum(FailQty) as Qty from MES2017.dbo.v_FQC_Detail where Qdate between '"+ std2 +"' and '"+ etd +"' and PL='" + dep + "'  and PPM>0 group by FailureMode order by sum(FailQty) desc"
                DQ=SumReport.objects.filter(PL='' + dep + '',WorkingDate__range=('' + std + '', ''+etd+'')).values('PL').annotate(FG=Sum('CompleteQty'),PQ=Sum('PQ'),UPPH=Avg('UPPH'),URT=Avg('URT'),PPM=Avg('PPM'),FCY=Avg('FCY'),ACR=Avg('Productivity'),EFY=Avg('Efficiency')).values('UPPH','FCY','PPM','URT','ACR','EFY','FG','PQ')
            else:
                sql5="select case when len(FailureMode)=0 then 'NA' else FailureMode end as FM,sum(FailQty) as Qty from MES2017.dbo.v_FQC_Detail where Qdate between '"+ std2 +"' and '"+ etd +"' and Cell='" + cell + "'  and PPM>0 group by FailureMode order by sum(FailQty) desc"
                DQ=SumReport.objects.filter(Cell='' + cell + '',WorkingDate__range=('' + std + '', ''+etd+'')).values('Cell').annotate(FG=Sum('CompleteQty'),PQ=Sum('PQ'),UPPH=Avg('UPPH'),URT=Avg('URT'),PPM=Avg('PPM'),FCY=Avg('FCY'),ACR=Avg('Productivity'),EFY=Avg('Efficiency')).values('UPPH','FCY','PPM','URT','ACR','EFY','FG','PQ')
                sql2=sql3
            cells="CellSUM"
        #cellsum
        sql1="select Cell,cast(sum(CompleteQty) as int) as FG,cast(sum(PQ) as int) as PQ,cast(cast(avg(UPPH) as numeric(8,2)) as varchar(8)) as UPPH,cast(avg(URT)*100 as int) as URT,cast(avg(PPM) as int) as PPM,cast(100*avg(FCY) as int) as FCY,cast(avg(Efficiency)*100 as int) as EFY,cast(avg(Productivity)*100 as int) as ACR from p_mes_sumreport where PL='"+ dep +"' and WorkingDate between '"+ std +"' and '"+ etd +"' group by Cell"
        sql4="select tcode,cast(tucl as varchar(12)) as ucl,cast(tlcl as varchar(12)) as lcl from p_mes_deptarget a left join p_admin_department b on dep_id=b.code where name='"+ dep +"'"
        cellsum=dictfetchall(connection.cursor().execute(sql1))
        datesum=dictfetchall(connection.cursor().execute(sql2))
        targetsum=dictfetchall(connection.cursor().execute(sql4))
        fqcfmsum=dictfetchall(connection.cursor().execute(sql5))
        connection.close

        if DQ is not None:
            try:
                content['EFY']=('%d %%' % (100*float(dict(list(DQ)[0])['EFY'])))
                content['URT']=('%d %%' % (100*float(dict(list(DQ)[0])['URT'])))
                content['ACR']=('%d %%' % (100*float(dict(list(DQ)[0])['ACR'])))
                content['FG']=('%d' % float(dict(list(DQ)[0])['FG']))
                content['PQ']=('%d' % float(dict(list(DQ)[0])['PQ']))
                content['UPPH']=('%.2f' % float(dict(list(DQ)[0])['UPPH']))
                content['PPM']=('%d' % float(dict(list(DQ)[0])['PPM']))
                content['FCY']=('%d %%' % (100*float(dict(list(DQ)[0])['FCY'])))
            except:
                pass
        content['cells']=cells
        content['cellsum']=cellsum
        content['datesum']=datesum
        content['targetsum']=targetsum
        content['fqcfmsum']=fqcfmsum

        return JsonResponse(content)

    #首次加载
    else:
        sql0="select distinct code,name,url,icon,parent_id,level from p_admin_module left join p_admin_profile_module on code=module_id where profile_id='" + username + "' and level='1' UNION select DISTINCT code,name,url,icon,parent_id,level  from p_admin_module a left join p_admin_module_group b on code=module_id left join auth_user_groups c on b.group_id=c.group_id where user_id='" + username + "' and level='1'"
        menus=connection.cursor().execute(sql0).fetchall()

        sql="select a.name from p_admin_department a left join p_admin_profile_department b on  a.code=b.department_id where profile_id='" + username + "' and category='P'"
        deps=connection.cursor().execute(sql).fetchall()
        if len(deps):
            dept= deps[0].name
        else:
            template = get_template('index.html')
            return HttpResponse(template.render(locals()))

        sql="select b.name from p_admin_department a inner join p_admin_department b on a.code=b.parent_id where b.category='C' and a.name='" + dept + "' order by b.name"
        cells=connection.cursor().execute(sql).fetchall()
        connection.close
        startdate=(datetime.date.today()+datetime.timedelta(days=-1)).strftime('%Y-%m-%d')
        enddate=(datetime.date.today()+datetime.timedelta(days=-1)).strftime('%Y-%m-%d')
        template = get_template('pages/dashboard.html')
        return HttpResponse(template.render(locals()))

#子报表查询
@login_required
@csrf_exempt
def dashboardetail(request):
    pass


# -*- coding: utf-8 -*-
"""
Created on Tue May 6 16:33:55 2019

@author: 19038067
"""
import numpy as np
import cv2
from matplotlib.colors import hex2color
from skimage import color

# 构建reference图片的噪声的部分
def _make_noisy_ref(src, hex):
    src_gray = cv2.imread(src, cv2.IMREAD_GRAYSCALE)
    src_gray = cv2.resize(src_gray, (100, 100), interpolation=cv2.INTER_AREA)
    c_map = (src_gray - src_gray.mean()) / src_gray.std()

    rgb_pix = hex2color(hex)

    # 完成涂色
    im_tar = np.ones((100, 100, 3), dtype=np.float)
    for i in range(3):
        im_tar[:, :, i] = rgb_pix[i]

    # 加扰动，这一块纯经验值，可以看心情改
    im_tar = color.rgb2hsv(im_tar)
    if 0.45 < im_tar[0, 0, 0] < 0.55:       # 浅蓝色单独处理
        im_tar[:, :, 2] += c_map * 0.06
    elif im_tar[0, 0, 1] > 0.95:    # S>0.95
        im_tar[:, :, 2] += c_map * 0.05     # 亮度加扰动
    elif im_tar[0, 0, 1] > 0.9:     # 0.95>S>0.9
        im_tar[:, :, 2] += c_map * 0.002    # 亮度加扰动
    elif im_tar[0, 0, 1] > 0.8:     # 0.9 >S >0.8
        im_tar[:, :, 1] += c_map * 0.06     # 饱和度加扰动
    else:                           # S<0.8
        im_tar[:, :, 1] += c_map * 0.08     # 饱和度加扰动

    im_tar = np.uint8(color.hsv2rgb(im_tar) * 255)
    return im_tar

# Reinhard的目标颜色版本
def Reinhard(img, hex):
    image = cv2.imdecode(np.fromfile(src, dtype=np.uint8), -1)
    if image.shape[-1] == 4:
        alpha = image[:, :, 3:]

    image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
#     alpha = np.reshape(image[:,:,-1], (image.shape[0],-1,1))
    original = _make_noisy_ref(src, hex)
    original = cv2.cvtColor(original, cv2.COLOR_BGR2LAB)

    image_avg, image_std = np.mean(image,(0,1)), np.std(image,(0,1))
    original_avg, original_std = np.mean(original,(0,1)), np.std(original,(0,1))

    image = (image-image_avg)*(original_std/image_std)+(original_avg)
    image = np.where(image>255,255,image)
    image = np.where(image<0, 0, image)
    image = image.astype(np.uint8)

    image = cv2.cvtColor(image, cv2.COLOR_LAB2BGR)

    if alpha is not None:
        image = np.concatenate([image, alpha], -1)
    return image

if __name__ == '__main__':
    img = Image.fromarray(Reinhard('orignial.png','#f1f2f3'))
    img.save('Reinhard.png')