import matplotlib.pyplot as plt
import math
import random

"""
函数里面所有以plot开头的函数都可以注释掉，没有影响
求解的目标表达式为：
y = 10 * math.sin(5 * x) + 7 * math.cos(4 * x)
"""

def main():
    print('y = 10 * math.sin(5 * x) + 7 * math.cos(4 * x)')
    plot_obj_func()
    pop_size = 500  # 种群数量
    upper_limit = 10  # 基因中允许出现的最大值
    chromosome_length = 10  # 染色体长度
    iter = 500
    pc = 0.6 # 杂交概率
    pm = 0.01  # 变异概率
    results = []  # 存储每一代的最优解，N个二元组
    # pop = [[0, 1, 0, 1, 0, 1, 0, 1, 0, 1] for i in range(pop_size)]
    pop = init_population(pop_size, chromosome_length)
    best_X = []
    best_Y = []
    for i in range(iter):
        obj_value = calc_obj_value(pop, chromosome_length, upper_limit)  # 个体评价，有负值
        fit_value = calc_fit_value(obj_value)  # 个体适应度，不好的归0，可以理解为去掉上面的负值
        best_individual, best_fit = find_best(pop, fit_value)  # 第一个是最优基因序列, 第二个是对应的最佳个体适度
        # 下面这句就是存放每次迭代的最优x值是最佳y值
        results.append([binary2decimal(best_individual, upper_limit, chromosome_length), best_fit])

        # 查看一下种群分布
        # plot_currnt_individual(decode_chromosome(pop, chromosome_length, upper_limit), obj_value)

        selection(pop, fit_value)  # 选择
        crossover(pop, pc)  # 染色体交叉（最优个体之间进行0、1互换）
        mutation(pop, pm)  # 染色体变异（其实就是随机进行0、1取反）
        # 最优解的变化
        if iter % 20 == 0:
            best_X.append(results[-1][0])
            best_Y.append(results[-1][1])
    print("x = %f, y = %f" % (results[-1][0], results[-1][1]))
    # 看种群点的选择
    plt.scatter(best_X, best_Y, s=3, c='r')
    X1 = [i / float(10) for i in range(0, 100, 1)]
    Y1 = [10 * math.sin(5 * x) + 7 * math.cos(4 * x) for x in X1]
    plt.plot(X1, Y1)
    plt.show()
    # 看迭代曲线
    plot_iter_curve(iter, results)

# 看看我们要处理的目标函数
def plot_obj_func():
    """y = 10 * math.sin(5 * x) + 7 * math.cos(4 * x)"""
    X1 = [i / float(10) for i in range(0, 100, 1)]
    Y1 = [10 * math.sin(5 * x) + 7 * math.cos(4 * x) for x in X1]
    plt.plot(X1, Y1)
    plt.show()

# 看看当前种群个体的落点情况
def plot_currnt_individual(X, Y):
    X1 = [i / float(10) for i in range(0, 100, 1)]
    Y1 = [10 * math.sin(5 * x) + 7 * math.cos(4 * x) for x in X1]
    plt.plot(X1, Y1)
    plt.scatter(X, Y, c='r', s=5)
    plt.show()

# 看看最终的迭代变化曲线
def plot_iter_curve(iter, results):
    X = [i for i in range(iter)]
    Y = [results[i][1] for i in range(iter)]
    plt.plot(X, Y)
    plt.show()

# 计算2进制序列代表的数值
def binary2decimal(binary, upper_limit, chromosome_length):
    t = 0
    for j in range(len(binary)):
        t += binary[j] * 2 ** j
    t = t * upper_limit / (2 ** chromosome_length - 1)
    return t

def init_population(pop_size, chromosome_length):
    # 形如[[0,1,..0,1],[0,1,..0,1]...]
    pop = [[random.randint(0, 1) for i in range(chromosome_length)] for j in range(pop_size)]
    return pop

# 解码并计算值
def decode_chromosome(pop, chromosome_length, upper_limit):
    X = []
    for ele in pop:
        temp = 0
        # 二进制变成实数，种群中的每个个体对应一个数字
        for i, coff in enumerate(ele):
            # 就是把二进制转化为十进制的
            temp += coff * (2 ** i)

        # 这个是把前面得到的那个十进制的数，再次缩放为另一个实数
        # 注意这个实数范围更广泛，可以是小数了，而前面二进制解码后只能是十进制的数
        # 参考https://blog.csdn.net/robert_chen1988/article/details/79159244
        X.append(temp * upper_limit / (2 ** chromosome_length - 1))
    return X

def calc_obj_value(pop, chromosome_length, upper_limit):
    obj_value = []
    X = decode_chromosome(pop, chromosome_length, upper_limit)
    for x in X:
        # 把缩放过后的那个数，带入我们要求的公式中
        # 种群中个体有几个，就有几个这种“缩放过后的数”
        obj_value.append(10 * math.sin(5 * x) + 7 * math.cos(4 * x))
    # 这里先返回带入公式计算后的数值列表，作为种群个体优劣的评价
    return obj_value

# 淘汰
def calc_fit_value(obj_value):
    fit_value = []
    # 去掉小于0的值，更改c_min会改变淘汰的下限
    # 比如设成10可以加快收敛
    # 但是如果设置过大，有可能影响了全局最优的搜索
    c_min = 10
    for value in obj_value:
        if value > c_min:
            temp = value
        else:
            temp = 0.
        fit_value.append(temp)
    # fit_value保存的是活下来的值
    return fit_value

# 找出最优解和最优解的基因编码
def find_best(pop, fit_value):
    # 用来存最优基因编码
    best_individual = []
    # 先假设第一个基因的适应度最好
    best_fit = fit_value[0]
    for i in range(1, len(pop)):
        if (fit_value[i] > best_fit):
            best_fit = fit_value[i]
            best_individual = pop[i]
    # best_fit是值
    # best_individual是基因序列
    return best_individual, best_fit

# 计算累计概率
def cum_sum(fit_value):
    # 输入[1, 2, 3, 4, 5]，返回[1,3,6,10,15]，matlab的一个函数
    # 这个地方遇坑，局部变量如果赋值给引用变量，在函数周期结束后，引用变量也将失去这个值
    temp = fit_value[:]
    for i in range(len(temp)):
        fit_value[i] = (sum(temp[:i + 1]))

# 轮赌法选择
def selection(pop, fit_value):
    # https://blog.csdn.net/pymqq/article/details/51375522

    p_fit_value = []
    # 适应度总和
    total_fit = sum(fit_value)
    # 归一化，使概率总和为1
    for i in range(len(fit_value)):
        p_fit_value.append(fit_value[i] / total_fit)
    # 概率求和排序

    # https://www.cnblogs.com/LoganChen/p/7509702.html
    cum_sum(p_fit_value)
    pop_len = len(pop)
    # 类似搞一个转盘吧下面这个的意思
    ms = sorted([random.random() for i in range(pop_len)])
    fitin = 0
    newin = 0
    newpop = pop[:]
    # 转轮盘选择法
    while newin < pop_len:
        # 如果这个概率大于随机出来的那个概率，就选这个
        if (ms[newin] < p_fit_value[fitin]):
            newpop[newin] = pop[fitin]
            newin = newin + 1
        else:
            fitin = fitin + 1
    # 这里注意一下，因为random.random()不会大于1，所以保证这里的newpop规格会和以前的一样
    # 而且这个pop里面会有不少重复的个体，保证种群数量一样

    # 之前是看另一个人的程序，感觉他这里有点bug，要适当修改
    pop = newpop[:]

# 杂交
def crossover(pop, pc):
    # 一定概率杂交，主要是杂交种群种相邻的两个个体
    pop_len = len(pop)
    for i in range(pop_len - 1):
        # 随机看看达到杂交概率没
        if (random.random() < pc):
            # 随机选取杂交点，然后交换数组
            cpoint = random.randint(0, len(pop[0]))
            temp1 = []
            temp2 = []
            temp1.extend(pop[i][0:cpoint])
            temp1.extend(pop[i + 1][cpoint:len(pop[i])])
            temp2.extend(pop[i + 1][0:cpoint])
            temp2.extend(pop[i][cpoint:len(pop[i])])
            pop[i] = temp1[:]
            pop[i + 1] = temp2[:]

# 基因突变
def mutation(pop, pm):
    px = len(pop)
    py = len(pop[0])
    # 每条染色体随便选一个杂交
    for i in range(px):
        if (random.random() < pm):
            mpoint = random.randint(0, py - 1)
            if (pop[i][mpoint] == 1):
                pop[i][mpoint] = 0
            else:
                pop[i][mpoint] = 1

if __name__ == '__main__':
    main()


import datetime
import os
import tensorflow as tf
import numpy as np

class RunnerA2C:
    def __init__(self, agent, env, train, load_path, num_scores_average: int, file_format: str = '.pkl'):

        self.agent = agent
        self.env = env
        self.train = train  # run only or train_model model?

        self.num_scores_average = num_scores_average
        self.score_average_list = []
        self.loss_average_list = []
        self.score_average = 0
        self.loss_average = 0
        self.loss = 0

        self.file_format = file_format

        self.score = 0  # store for the scores of an episode
        self.episode = 1  # episode counter

        self.path = './results/' + datetime.datetime.now().strftime("%y%m%d_%H%M") \
                    + ('_train_' if self.train else 'run_') \
                    + type(agent).__name__

        self.writer = tf.summary.FileWriter(self.path, tf.get_default_graph())
        if not self.train and load_path is not None and os.path.isdir(load_path):
            self.agent.load_model(load_path)

    def summarize(self):
        self.writer.add_summary(tf.Summary(
            value=[tf.Summary.Value(tag='Average Score per Episode', simple_value=self.score_average)]),
            self.episode
        )
        self.writer.add_summary(tf.Summary(
            value=[tf.Summary.Value(tag='Loss', simple_value=self.loss_average)]),
            self.episode
        )
        if self.train and self.episode % 10 == 0:
            try:
                self.agent.update_target_model()
            except AttributeError:
                ...
            self.agent.save_model(self.path + '/' + str(self.episode) + self.file_format)
        self.episode += 1
        self.score = 0
        self.loss = 0

    def run(self, episodes):
        while self.episode <= episodes:

            obs = self.env.reset()
            self.agent.counter = 0
            while True:
                if self.train:
                    self.loss, avg_reward, episode_finished = self.agent.step(obs)
                    self.score += avg_reward
                    if episode_finished:
                        break
                else:
                    action = self.agent.step(obs)
                    self.score += obs.reward
                    if obs.last():
                        break
                    obs = self.env.step(action)

            if self.train:
                self.update_loss(self.loss)
            self.add_score()
            self.summarize()

    def add_score(self):
        self.score_average_list.append(self.score)
        self.score_average = np.mean(np.array(self.score_average_list)[-min(len(self.score_average_list), self.num_scores_average):])
        self.score = 0

    def update_loss(self, loss):
        self.loss_average_list.append(loss)
        self.loss_average = np.mean(np.array(self.loss_average_list)[-min(len(self.loss_average_list), 100):])


import sys
from random import randint
from datetime import timedelta
from collections import OrderedDict

""" the real purpose of this is to explore some data visualization and practice.
    A nice side-effect is exploring the reality of playing video poker."""

try:
    stake, target, bet, rate_of_return = sys.argv[1:]
except:
    stake = 400
    target = 600
    bet = 15
    rate_of_return = 0.98

trials = 2000
seconds_per_hand = 8

def game1(bet, rate_of_return=rate_of_return):
    """ a generic fictional game that can approximate the long-term rate of return for any casino game """
    return (bet + bet*rate_of_return) * randint(0,1)

""" paytable and odds from https://wizardofodds.com/games/video-poker/tables/jacks-or-better/
    Jacks or better, '9-5' style """

paytable = OrderedDict([(10862898027756, 0), (4288342040640, 1), (2577431192796, 2),(1484332642620, 3),
                        (223861063908, 4), (217120426644, 5),(229510637676, 9),(47100799404, 25),  (2137447980, 50),
                        (496237776, 800)])

possibles = sum(paytable.keys())

def game(bet, possibles=possibles, paytable=paytable):
    """ play video poker or some other game according to a pre-determined paytable odds chart
        that is ordered from the most-to-least likely outcomes"""
    spin = randint(0, possibles)
    cumulate = 0
    for k in paytable:
        cumulate += k
        if spin < cumulate: break
    return bet * paytable[k], 1 if paytable[k] >= 800 else 0

def ruin(stake=stake, target=target, bet=bet, trials=trials):
    """ play the game starting with a 'stake' and quitting when reaching the 'target' or losing so much that
     another full bet cannot be made. 'bet' the same amount during all 'trial'
     Report the dollar turnover and win percentage"""
    original_stake = stake
    wins = 0
    promo_history = []
    jackpots = 0
    for t in range(trials):
        promo = 0
        while ((stake - bet) > 0) and (stake < target):
            winnings, jackpot = game(bet)
            promo += bet
            stake += (winnings - bet)
            jackpots += jackpot
        if stake > original_stake: # if above ended ahead, its a win!
            wins += 1
        promo_history.append(promo)
        stake = original_stake
    promo_avg = sum(promo_history) / float(trials)
    promo_max = max(promo_history)
    promo_min = min(promo_history)
    hands_played = int(promo_avg / bet)
    time_spent = str(timedelta(seconds=(hands_played * seconds_per_hand)))
    print("win {:.2f}%  risk ${} to get ${} @ ${} / bet, avg rollover: ${:.2f} in {}  min:${:.2f}, max ${:.2f} jackpots: {}"
          .format(100*(wins/float(trials)), stake, target, bet, promo_avg, time_spent, promo_min, promo_max, jackpots))

stakes = [400, 515]
targets = [600, 700]
bets = [5, 15]

def bigone():
    for bet in bets:
        for stake in stakes:
            for target in targets:
                ruin(stake=stake, target=target, bet=bet, trials=trials)

if __name__ == "__main__":
    print(" *** If playing perfect video poker until ruination or a target goal ({} trials): ***".format(trials))
    bigone()


# 1
# def countdown(max):
#     newList = []
#     for x in range(0,max + 1):
#         newList.append(x)
#     return newList
# print(countdown(50))

# 2
# def print_return(val1,val2):
#     print(val1)
#     return val2
# print(print_return(2,20))

# 3
# def first_plus_length(list):
#     sum = list[0] + len(list)
#     return sum
# print(first_plus_length([12,34,46,78,95,1]))

# 4
# def vals_great_sec(list):
#     new_list = []
#     for x in range(0,len(list)):
#         if(list[x] > list[x-1] or x == 0):
#             new_list.append(list[x])
#     return new_list

# print(vals_great_sec([5,2,3,2,1,4]))

# 5
def first_plus_length(size,value):
    new_list = []
    for x in range(0,size):
        new_list.append(value)
    return new_list
print(first_plus_length(2,7))


import chess
import chess_bot_search as search
import random

def game(coefficient_list, depth):
    is_players_turn = random.randint(0,1)
    ai_is_white = not is_players_turn
    board = chess.Board()
    while not board.is_game_over():
        print(board)
        if is_players_turn:
            player_move = input("Enter your move in Short Algebraic Notation: ")
            try:
                board.push_san(player_move)
                is_players_turn = False
            except:
                print("Move you entered is invalid.  Please try again.")
        else:
            try:
                ai_move = search.iterative_deepening(1, board, coefficient_list, ai_is_white)
                print(ai_move)
                board.push(ai_move)
                is_players_turn = True
            except:
                print("Something went wrong.")
                return

if __name__ == "__main__":
    coefficient_list = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    game(coefficient_list, 2)


from slacker import Slacker
import random
from private.tokens import slack as slack_token

slack = Slacker(slack_token)

def lunch_ask_bot():
    attachments_dict = {}

    attachments_dict['pretext'] = "오늘의 점심조 참여 여부를 확인해주세요 :mememe:"
    attachments_dict['text'] = "특히 제외해야한다면 꼭! 댓글로 알려주세요 :)"
    attachments = [attachments_dict]

    # slack bot으로 알림을 보냅니다.
    slack.chat.post_message(channel='# 81_일반_업무', attachments=attachments, as_user=True)

def lunch_match_bot(add_no_lunch):

    lunch_people = [
        '조엘', '이본', '루씨', '제나', '센스', '안젤라', '영',
        '린다', '하니', '피터',
        '엘로이', '테드', '써니', '데니스', '에이제이',
        '존', '앤디',
    ]

    attachments_dict = {}

    for i in add_no_lunch:
        lunch_people.remove(i)

    def make_matches(lunch_people, match_count):
        lunch_dict = {}
        num = 0

        for i in range(match_count):
            lunch_dict['%s조' % str(i + 1)] = []

        random.shuffle(lunch_people)
        print(lunch_people)

        while num < len(lunch_people):
            for j in lunch_dict.keys():
                try:
                    lunch_dict[j].append(lunch_people[num])
                    num += 1
                except:
                    break

        return lunch_dict

    if len(lunch_people) // 4 == 0:
        attachments_dict['pretext'] = "오늘의 점심조입니다 :mememe:"
        attachments_dict['text'] = "오늘은 점심조가 없습니다. 점심 맛있게 드세요 :wink:"
        attachments = [attachments_dict]

    else:
        match_count = (len(lunch_people) // 5) +1
        lunch_dict = make_matches(lunch_people, match_count)

        lunch_dict['열외'] = add_no_lunch

        keys = lunch_dict.keys()
        slack_bot_text = ''

        for key in keys:
            slack_bot_text += '*%s* (%s명)   ' % (key, str(len(lunch_dict[key])))

            text = ''
            for people in lunch_dict[key]:
                text += people + ' '

            slack_bot_text += '%s\n' % text

        # slack bot setting
        attachments_dict['pretext'] = "오늘의 점심조입니다 :mememe:"
        attachments_dict['text'] = slack_bot_text
        attachments_dict['mrkdwn_in'] = ["text", "pretext"]
        attachments = [attachments_dict]

    # slack bot으로 알림을 보냅니다.
    slack.chat.post_message(channel='# 81_일반_업무', attachments=attachments, as_user=True)

    print('lunch_bot Done')


#!/usr/bin/env python
import argparse
from charmbenchmark import Benchmark

def main():
    parser = argparse.ArgumentParser(
        description='Inform the Benchmark GUI of available benchmarks'
    )
    parser.add_argument(
        "benchmarks",
        metavar='benchmark(s)',
        nargs='+',
        help='A space-delimited list of benchmarks exposed by the charm.'
    )
    args = parser.parse_args()

    Benchmark(args.benchmarks)

if __name__ == "__main__":
    main()


from PyQt5 import QtGui
from PyQt5.Qt import *
from PyQt5.QtWidgets import *

from source.store.operations_and_invariants.invariants import UtilsToInvariants

class GraphInformationDock(QDockWidget):

    def __init__(self):
        super().__init__()
        self.model = QtGui.QStandardItemModel()

        self.table = QTableView()

        self.set_content_attributes()

    def set_content_attributes(self):
        self.setWindowTitle("Info")
        self.setObjectName("Info")

        self.setFeatures(QDockWidget.DockWidgetClosable |
                         QDockWidget.DockWidgetMovable |
                         QDockWidget.DockWidgetFloatable
                         )

        self.model.setHorizontalHeaderLabels(['Invariants', 'Results'])
        self.table.setModel(self.model)

        self.table.setWordWrap(False)
        self.table.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.table.verticalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
        self.table.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.table.horizontalHeader().setSectionResizeMode(1, QHeaderView.Interactive)
        self.table.setColumnWidth(1, 400)
        self.setWidget(self.table)

    def update_table(self, invariants_selected):
        self.model.removeRows(0, self.model.rowCount())
        column_max_width = 0
        for key, value in invariants_selected.items():
            row = []
            invariant_name = QtGui.QStandardItem(str(key))
            invariant_name.setEditable(False)

            invariant_value = QtGui.QStandardItem(value)
            invariant_value.setEditable(False)

            current_column_max_width = 0
            current_column_max_width = max(UtilsToInvariants.max_line_of_string(value), current_column_max_width)
            if current_column_max_width > column_max_width:
                column_max_width = current_column_max_width

            row.append(invariant_name)
            row.append(invariant_value)
            self.model.appendRow(row)
            self.table.resizeColumnToContents(1)

        if 5.1 * column_max_width < self.table.columnWidth(1):
            self.table.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch)
            self.table.horizontalHeader().setSectionResizeMode(0, QHeaderView.Interactive)
            self.table.setColumnWidth(0, 200)
        elif 5.1 * column_max_width > self.table.columnWidth(1):
            self.table.horizontalHeader().setSectionResizeMode(0, QHeaderView.Interactive)
            self.table.horizontalHeader().setSectionResizeMode(1, QHeaderView.Interactive)
            self.table.setColumnWidth(0, 200)


# https://github.com/Noixas/BERT-Toxicity

import numpy as np
import torch.utils.data
import torch
import json
import regex as re
import csv
from datetime import datetime
from pytorch_pretrained_bert import BertTokenizer, BertForSequenceClassification

BATCH_SIZE = 256
MAX_SEQUENCE_LENGTH = 128

def convert_lines(texts, max_seq_length, tokenizer):
    max_seq_length -= 2
    all_tokens = []
    longer = 0
    for text in texts:
        tokens_a = tokenizer.tokenize(text)
        if len(tokens_a) > max_seq_length:
            tokens_a = tokens_a[:max_seq_length]
            longer += 1
        one_token = tokenizer.convert_tokens_to_ids(['[CLS]']+tokens_a+['[SEP]'])+[0] * (max_seq_length - len(tokens_a))
        all_tokens.append(one_token)
    return np.array(all_tokens)

def get_predictions(comments):
    model.eval()
    with torch.no_grad():
        valid_preds_pews = np.zeros((len(comments)))
        valid_pews = torch.utils.data.TensorDataset(torch.tensor(comments, dtype=torch.long))
        valid_loader_pews = torch.utils.data.DataLoader(valid_pews, batch_size=BATCH_SIZE, shuffle=False)
        for i, (x_batch,) in enumerate(valid_loader_pews):
            pred_pews = model(x_batch.to(device), attention_mask=(x_batch > 0).to(device), labels=None)
            valid_preds_pews[i*BATCH_SIZE:(i+1)*BATCH_SIZE] = pred_pews[:,0].detach().cpu().squeeze().numpy()
        test_df_pews = torch.sigmoid(torch.tensor(valid_preds_pews)).numpy()
        return test_df_pews

device = torch.device('cuda')
model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=1)
model.load_state_dict(torch.load('bert_pytorch.bin'))
model.to(device)

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

def process_json(name):
    print('Processing', name)
    with open('json/' + name + '.json') as f:
        lines = f.readlines()

    date_to_threads = {}

    for line in lines:
        data = json.loads(line)
        text = data['title']
        if 'selftext' in data:
            text += ' ' + data['selftext']
        date = datetime.utcfromtimestamp(data['created_utc']).strftime('%Y-%m-%d')
        if date not in date_to_threads:
            date_to_threads[date] = []
        date_to_threads[date].append(text)

    with open('csv/' + name + '_texts.csv', 'a') as texts_csv_file, open('csv/' + name + '_output.csv', 'a') as output_csv_file:
        texts_writer = csv.writer(texts_csv_file, delimiter=',')
        output_writer = csv.writer(output_csv_file, delimiter=',')
        dates = [date for date, _ in date_to_threads.items()]
        dates.sort(reverse=True)
        for date in dates:
            texts = date_to_threads[date]
            converted = convert_lines(texts, MAX_SEQUENCE_LENGTH, tokenizer)
            predictions = get_predictions(converted)
            mean_prediction = np.mean(predictions)
            toxic_amount = len([tox for tox in predictions if tox > 0.5])
            total_threads = len(predictions)
            print(date, str(total_threads), str(toxic_amount), str(mean_prediction))
            for i in range(len(texts)):
                texts_writer.writerow([date, re.sub(r'[\r\n\t;]', ' ', texts[i])[0:MAX_SEQUENCE_LENGTH], str(predictions[i])])
            output_writer.writerow([date, str(total_threads), str(toxic_amount), str(mean_prediction)])

process_json('pathofexile')
process_json('diablo3')
process_json('Wolcen')
process_json('Warframe')
process_json('leagueoflegends')
process_json('GlobalOffensive')
process_json('DotA2')
process_json('Overwatch')
process_json('VALORANT')


#%% 5 Package import（包导入）
import paddle
import paddleseg.transforms as T
from paddleseg.datasets import Dataset
# from paddleseg.models import OCRNet, HRNet_W18  # OCRNet
from paddleseg.models.segformer import SegFormer_B2  # segformer
from paddleseg.models.losses import MixedLoss, BCELoss, DiceLoss, LovaszHingeLoss
from paddleseg.core import train, evaluate

#%% * Label processing（标签处理）
import numpy as np
from paddleseg.cvlibs import manager

@manager.TRANSFORMS.add_component
class InitMask:
    def __init__(self):
        pass

    def __call__(self, im, label=None):
        label = np.clip(label, 0, 1)
        if label is None:
            return (im, )
        else:
            if len(label.shape) == 3:
                label = np.mean(label, axis=-1).astype("uint8")
            return (im, label)

#%% 6 Data set establishment（数据集建立）
# Build the training set
# 建立训练数据集
train_transforms = [
    InitMask(),  # This is not necessary（这不是必要的）
    T.RandomHorizontalFlip(),
    T.RandomVerticalFlip(),
    T.RandomRotation(),
    T.RandomScaleAspect(),
    T.RandomBlur(),
    T.Resize(target_size=(512, 512)),
    T.Normalize()
]
train_dataset = Dataset(
    transforms=train_transforms,
    dataset_root="data/dataset",
    num_classes=2,
    mode="train",
    train_path="data/dataset/train.txt",
    separator=" "
)

# Build validation set
# 建立评估数据集
val_transforms = [
    InitMask(),
    T.Resize(target_size=(512, 512)),
    T.Normalize()
]
val_dataset = Dataset(
    transforms=val_transforms,
    dataset_root="data/dataset",
    num_classes=2,
    mode="val",  # If your data does not need `initmask()`, please use "val"（如果你的数据不需要`InitMask()`，请使用val"）
    val_path="data/dataset/val.txt",
    separator=" "
)

#%% * Check dataset（检查数据）
# for img, lab in val_dataset:
#     print(img.shape, lab.shape)
#     print(np.unique(lab))

#%% 7 Training parameter setting（训练参数设置）
base_lr = 3e-5
epochs = 2
batch_size = 16
iters = epochs * len(train_dataset) // batch_size

# model = OCRNet(num_classes=2,
#                backbone=HRNet_W18(),
#                backbone_indices=[0],
#                pretrained="output/best_model/model.pdparams")

model = SegFormer_B2(num_classes=2,
                     pretrained="output_segformer/best_model/model.pdparams")

# lr = paddle.optimizer.lr.LinearWarmup(base_lr, warmup_steps=iters // epochs, start_lr=base_lr / 10, end_lr=base_lr)
lr = paddle.optimizer.lr.PolynomialDecay(base_lr, decay_steps=iters // epochs, end_lr=base_lr / 5)
optimizer = paddle.optimizer.AdamW(lr, beta1=0.9, beta2=0.999, weight_decay=0.01, parameters=model.parameters())
losses = {}
losses["types"] = [MixedLoss([BCELoss(), DiceLoss(), LovaszHingeLoss()], [2, 1, 1])]  #  * 2
losses["coef"] = [1]  # [1, 0.4]

#%% 8 Start training（开始训练）
model.train()

train(
    model=model,
    train_dataset=train_dataset,
    val_dataset=val_dataset,
    optimizer=optimizer,
    save_dir="output_segformer",
    iters=iters,
    batch_size=batch_size,
    save_interval=iters // 5,
    log_iters=10,
    num_workers=0,
    losses=losses,
    use_vdl=True)

#%% 9 Model evaluation（模型评估）
# model.eval()

# evaluate(
#     model,
#     val_dataset)


# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from paginate_sqlalchemy import SqlalchemyOrmPage as SQLAlchemyORMPage
from pyramid.httpexceptions import HTTPBadRequest, HTTPNotFound, HTTPSeeOther
from pyramid.view import view_config

from warehouse.malware.models import (
    MalwareCheck,
    MalwareVerdict,
    VerdictClassification,
    VerdictConfidence,
)
from warehouse.utils.paginate import paginate_url_factory

@view_config(
    route_name="admin.verdicts.list",
    renderer="admin/malware/verdicts/index.html",
    permission="moderator",
    request_method="GET",
    uses_session=True,
)
def get_verdicts(request):
    result = {}
    result["check_names"] = set(
        [name for (name,) in request.db.query(MalwareCheck.name)]
    )
    result["classifications"] = set([c.value for c in VerdictClassification])
    result["confidences"] = set([c.value for c in VerdictConfidence])

    validate_fields(request, result)

    result["verdicts"] = SQLAlchemyORMPage(
        generate_query(request.db, request.params),
        page=int(request.params.get("page", 1)),
        items_per_page=25,
        url_maker=paginate_url_factory(request),
    )

    return result

@view_config(
    route_name="admin.verdicts.detail",
    renderer="admin/malware/verdicts/detail.html",
    permission="moderator",
    request_method="GET",
    uses_session=True,
)
def get_verdict(request):
    verdict = request.db.query(MalwareVerdict).get(request.matchdict["verdict_id"])

    if verdict:
        return {
            "verdict": verdict,
            "classifications": list(VerdictClassification.__members__.keys()),
        }

    raise HTTPNotFound

@view_config(
    route_name="admin.verdicts.review",
    permission="moderator",
    request_method="POST",
    uses_session=True,
    require_methods=False,
    require_csrf=True,
)
def review_verdict(request):
    verdict = request.db.query(MalwareVerdict).get(request.matchdict["verdict_id"])

    try:
        classification = getattr(VerdictClassification, request.POST["classification"])
    except (KeyError, AttributeError):
        raise HTTPBadRequest("Invalid verdict classification.") from None

    verdict.manually_reviewed = True
    verdict.reviewer_verdict = classification

    request.session.flash(
        "Verdict %s marked as reviewed." % verdict.id, queue="success"
    )

    # If no query params are provided (e.g. request originating from
    # admins.verdicts.detail view), then route to the default list view
    query = request.GET or {"classification": "threat", "manually_reviewed": "0"}
    return HTTPSeeOther(request.route_path("admin.verdicts.list", _query=query))

def validate_fields(request, validators):
    try:
        int(request.params.get("page", 1))
    except ValueError:
        raise HTTPBadRequest("'page' must be an integer.") from None

    validators = {**validators, **{"manually_revieweds": set(["0", "1"])}}

    for key, possible_values in validators.items():
        # Remove the trailing 's'
        value = request.params.get(key[:-1])
        additional_values = set([None, ""])
        if value not in possible_values | additional_values:
            raise HTTPBadRequest(
                "Invalid value for '%s': %s." % (key[:-1], value)
            ) from None

def generate_query(db, params):
    """
    Returns an SQLAlchemy query wth request params applied as filters.
    """
    query = db.query(MalwareVerdict)
    if params.get("check_name"):
        query = query.join(MalwareCheck)
        query = query.filter(MalwareCheck.name == params["check_name"])
    if params.get("confidence"):
        query = query.filter(MalwareVerdict.confidence == params["confidence"])
    if params.get("classification"):
        query = query.filter(MalwareVerdict.classification == params["classification"])
    if params.get("manually_reviewed"):
        query = query.filter(
            MalwareVerdict.manually_reviewed == bool(int(params["manually_reviewed"]))
        )

    return query.order_by(MalwareVerdict.run_date.desc())


from django.conf.urls import patterns, url

from .views import (
	createLine,
	getLine,
	getAllLines,

	createLocation,
        getLocation,

        createEmotion,
        getEmotion,
        getAllEmotions,

        createPledgeBreak1,
        getPledgeBreak,
        getAllPledgeBreaks,

        createPartyQuirk,
        getPartyQuirk,
        getAllPartyQuirks,

        createPartyQuirk2,
        getPartyQuirk2,
        getAllPartyQuirks2,

        createPartyQuirk3,
        getPartyQuirk3,
        getAllPartyQuirks3,

        wstest,
        wsline,
        wsline1,
        wsscript,
        wsgeturl,
        getSlides,

        createHaloblue,
        createHalogreen,
        createHaloyellow,
        createHaloredyellow,
        createHalopurple,

        getHaloblue,
        getHalogreen,
        getHaloyellow,
        getHaloredyellow,
        getHalopurple
)

urlpatterns = (

	url(r"^lines/create/$", createLine, name="createLine"),
	url(r"^lines/get/$", getLine, name="getLine"),
	url(r"^lines/getall/$", getAllLines, name="getAllLines"),

        url(r"^dinnerparty/create/$", createLocation, name="createLocation"),
        url(r"^dinnerparty/get/$", getLocation, name="getLocation"),

        url(r"^jumpgenre/create/$", createEmotion, name="createStyle"),
        url(r"^jumpgenre/get/$", getEmotion, name="getStyle"),
        url(r"^jumpgenre/getall/$", getAllEmotions, name="getAllStyles"),

        url(r"^productpitch/create/$", createPledgeBreak1, name="createNoun"),
        url(r"^productpitch/get/$", getPledgeBreak, name="getNoun"),
        url(r"^productpitch/getall/$", getAllPledgeBreaks, name="getAllNouns"),

        url(r"^partyquirks1/create/$", createPartyQuirk, name="createQuirk"),
        url(r"^partyquirks1/get/$", getPartyQuirk, name="getQuirk"),
        url(r"^partyquirks1/getall/$", getAllPartyQuirks, name="getAllQuirks"),

        url(r"^partyquirks2/create/$", createPartyQuirk2, name="createCelebrity"),
        url(r"^partyquirks2/get/$", getPartyQuirk2, name="getCelebrity"),
        url(r"^partyquirks2/getall/$", getAllPartyQuirks2, name="getAllCelebrities"),

        url(r"^partyquirks3/create/$", createPartyQuirk3, name="createQuirk3"),
        url(r"^partyquirks3/get/$", getPartyQuirk3, name="getQuirk3"),
        url(r"^partyquirks3/getall/$", getAllPartyQuirks3, name="getAllQuirks3"),

        url(r"^slides/get/$", getSlides, name="getSlides"),

        url(r"^ws/$", wstest, name="wstest"),
        url(r"^ws/get/$", wsgeturl, name="wsgeturl"),
        url(r"^ws/line/$", wsline, name="wsline"),
        url(r"^ws/line1/$", wsline1, name="wsline1"),
        url(r"^ws/loadscript/$", wsscript, name="wsscript"),

        url(r"^api/haloblue/create/$", createHaloblue, name="createHaloblue"),
        url(r"^api/haloblue/get/$", getHaloblue, name="getHaloblue"),

        url(r"^api/halogreen/create/$", createHalogreen, name="createHalogreen"),
        url(r"^api/halogreen/get/$", getHalogreen, name="getHalogreen"),

        url(r"^api/haloyellow/create/$", createHaloyellow, name="createHaloyellow"),
        url(r"^api/haloyellow/get/$", getHaloyellow, name="getHaloyellow"),

        url(r"^api/haloredyellow/create/$", createHaloredyellow, name="createHaloredyellow"),
        url(r"^api/haloredyellow/get/$", getHaloredyellow, name="getHaloredyellow"),

        url(r"^api/halopurple/create/$", createHalopurple, name="createHalopurple"),
        url(r"^api/halopurple/get/$", getHalopurple, name="getHalopurple")

)


"""Sample poses for testing the averaging algorithms.

The visualizations of this poses are stored in the folder: tests/data/viz_sample_poses

Authors: Ayush Baid
"""
import copy
from typing import Dict, List, Tuple

import numpy as np
from gtsam import Cal3_S2, Point3, Pose3, Rot3, Unit3
from gtsam.examples import SFMdata

DEFAULT_ROTATION = Rot3.RzRyRx(0, np.deg2rad(10), 0)
DEFAULT_TRANSLATION = np.array([0, 2, 0])

def generate_relative_from_global(
    wTi_list: List[Pose3], pair_indices: List[Tuple[int, int]]
) -> Dict[Tuple[int, int], Pose3]:
    """Generate relative poses from global poses.

    Args:
        wTi_list: global poses.
        pair_indices: pairs (i1, i2) to construct relative poses for.

    Returns:
        Dictionary (i1, i2) -> i2Ti1 for all requested pairs.
    """
    return {(i1, i2): wTi_list[i2].between(wTi_list[i1]) for i1, i2 in pair_indices}

"""4 poses in the circle of radius 5m, all looking at the center of the circle.

For relative poses, each pose has just 2 edges, connecting to the immediate neighbors.
"""
CIRCLE_TWO_EDGES_GLOBAL_POSES = SFMdata.createPoses(Cal3_S2(fx=1, fy=1, s=0, u0=0, v0=0))[::2]

CIRCLE_TWO_EDGES_RELATIVE_POSES = generate_relative_from_global(
    CIRCLE_TWO_EDGES_GLOBAL_POSES, [(1, 0), (2, 1), (3, 2), (0, 3)]
)

"""4 poses in the circle of radius 5m, all looking at the center of the circle.

For relative poses, each pose is connected to every other (3) pose.
"""
CIRCLE_ALL_EDGES_GLOBAL_POSES = copy.copy(CIRCLE_TWO_EDGES_GLOBAL_POSES)

CIRCLE_ALL_EDGES_RELATIVE_POSES = generate_relative_from_global(
    CIRCLE_TWO_EDGES_GLOBAL_POSES, [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
)

"""3 poses on a line, simulating forward motion, with large translations and no relative rotation.

For relative poses, we have a fully connected graph.
"""
LINE_LARGE_EDGES_GLOBAL_POSES = [
    Pose3(DEFAULT_ROTATION, DEFAULT_TRANSLATION + np.array([0, 0, 0])),
    Pose3(DEFAULT_ROTATION, DEFAULT_TRANSLATION + np.array([0, 0, 5])),
    Pose3(DEFAULT_ROTATION, DEFAULT_TRANSLATION + np.array([0, 0, 10])),
]

LINE_LARGE_EDGES_RELATIVE_POSES = generate_relative_from_global(LINE_LARGE_EDGES_GLOBAL_POSES, [(0, 1), (0, 2), (1, 2)])

"""3 poses on a line, simulating forward motion, with small translations and no relative rotation.

For relative poses, we have a fully connected graph.
"""
LINE_SMALL_EDGES_GLOBAL_POSES = [
    Pose3(DEFAULT_ROTATION, DEFAULT_TRANSLATION + np.array([0, 0, 0])),
    Pose3(DEFAULT_ROTATION, DEFAULT_TRANSLATION + np.array([0, 0, 1e-3])),
    Pose3(DEFAULT_ROTATION, DEFAULT_TRANSLATION + np.array([0, 0, 5e-3])),
]

LINE_SMALL_EDGES_RELATIVE_POSES = generate_relative_from_global(LINE_SMALL_EDGES_GLOBAL_POSES, [(0, 1), (0, 2), (1, 2)])

"""3 poses in a panorama (i.e. 3 translation values being the same but large relative rotations i.e. pitch varies)

For relative poses, we have a fully connected graph.
"""
PANORAMA_GLOBAL_POSES = [
    Pose3(Rot3.RzRyRx(0, np.deg2rad(-30), 0), DEFAULT_TRANSLATION),
    Pose3(Rot3.RzRyRx(0, 0, 0), DEFAULT_TRANSLATION),
    Pose3(Rot3.RzRyRx(0, np.deg2rad(+30), 0), DEFAULT_TRANSLATION),
]

PANORAMA_RELATIVE_POSES = generate_relative_from_global(PANORAMA_GLOBAL_POSES, [(0, 1), (0, 2), (1, 2)])

def convert_data_for_rotation_averaging(
    wTi_list: List[Pose3], i2Ti1_dict: Dict[Tuple[int, int], Pose3]
) -> Tuple[Dict[Tuple[int, int], Rot3], List[Rot3]]:
    """Converts the poses to inputs and expected outputs for a rotation averaging algorithm.

    Args:
        wTi_list: List of global poses.
        i2Ti1_dict: Dictionary of (i1, i2) -> i2Ti1 relative poses.

    Returns:
        i2Ti1_dict's values mapped to relative rotations i2Ri1.
        wTi_list mapped to global rotations.
    """

    wRi_list = [x.rotation() for x in wTi_list]
    i2Ri1_dict = {k: v.rotation() for k, v in i2Ti1_dict.items()}

    return i2Ri1_dict, wRi_list

def convert_data_for_translation_averaging(
    wTi_list: List[Pose3], i2Ti1_dict: Dict[Tuple[int, int], Pose3]
) -> Tuple[List[Rot3], Dict[Tuple[int, int], Unit3], List[Point3]]:
    """Converts the poses to inputs and expected outputs for a translation averaging algorithm.

    Args:
        wTi_list: List of global poses.
        i2Ti1_dict: Dictionary of (i1, i2) -> i2Ti1 relative poses.

    Returns:
        wTi_list mapped to global rotations.
        i2Ti1_dict's values mapped to relative unit translations i2Ui1.
        wTi_list mapped to global translations.
    """

    wRi_list = [x.rotation() for x in wTi_list]
    wti_list = [x.translation() for x in wTi_list]
    i2Ui1_dict = {k: Unit3(v.translation()) for k, v in i2Ti1_dict.items()}

    return wRi_list, i2Ui1_dict, wti_list


import argparse
import enum
import os
import sys
import subprocess
from yaml import load, dump
try:
    from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
    from yaml import Loader, Dumper

ConfigFilePath = './config.yaml'

class Configuration:
    """
    Parse the config.yaml file.
    Configuration holds all the params defined in configfile.
    """
    def __init__(self, args, config_file_path=ConfigFilePath):
        data = load(open(config_file_path, 'r').read())
        self.fill_config_from_args(args, data)

        # parse Options from config file.
        self.DebugConfig = data['DEBUG'] if 'DEBUG' in data else None
        self.framework = data['OPTIONS']['Framework']
        self.SavePath = data['OPTIONS']['SavePath'] \
                if data['OPTIONS']['SavePath'][-1] == '/' \
                else data['OPTIONS']['SavePath'] + '/'
        self.ResultName = data['OPTIONS']['ResultName']
        #self.intermediateModelPath = data['OPTIONS']['SavePath'] \
        #        + data['OPTIONS']['ResultName'] + "anakin.bin"
        self.LaunchBoard = True if data['OPTIONS']['Config']['LaunchBoard'] else False
        self.ip = data['OPTIONS']['Config']['Server']['ip']
        self.port = data['OPTIONS']['Config']['Server']['port']
        self.hasOptimizedGraph = True if data['OPTIONS']['Config']['OptimizedGraph']['enable'] else False
        self.optimizedGraphPath = data['OPTIONS']['Config']['OptimizedGraph']['path'] \
                if self.hasOptimizedGraph else ""
        self.logger_dict = data['OPTIONS']['LOGGER']
        self.framework_config_dict = data['TARGET'][self.framework]
        self.check_protobuf_version()
        if 'ProtoPaths' in data['TARGET'][self.framework].keys():
            proto_list = data['TARGET'][self.framework]['ProtoPaths']
            self.__refresh_pbs(proto_list)
        self.generate_pbs_of_anakin()

    def fill_config_from_args(self, args, data):
        """Fill config from args
        """
        # set common args
        if args.debug is not None:
            data['DEBUG'] = args.debug
        if args.framework is not None:
            data['OPTIONS']['Framework'] = str(args.framework)
        if args.save_path is not None:
            data['OPTIONS']['SavePath'] = args.save_path
        if args.result_name is not None:
            data['OPTIONS']['ResultName'] = args.result_name
        if args.open_launch_board is not None:
            data['OPTIONS']['Config']['LaunchBoard'] = True if args.open_launch_board != 0 else False
        if args.board_server_ip is not None:
            data['OPTIONS']['Config']['Server']['ip'] = args.board_server_ip
        if args.board_server_port is not None:
            data['OPTIONS']['Config']['Server']['port'] = args.board_server_port
        if args.optimized_graph_enable is not None:
            data['OPTIONS']['Config']['OptimizedGraph']['enable'] = True if args.optimized_graph_enable != 0 else False
        if args.optimized_graph_path is not None:
            data['OPTIONS']['Config']['OptimizedGraph']['path'] = args.optimized_graph_path
        if args.log_path is not None:
            data['OPTIONS']['LOGGER']['LogToPath'] = args.log_path
        if args.log_with_color is not None:
            data['OPTIONS']['LOGGER']['WithColor'] = args.log_with_color

        # set framwork specific args
        # caffe
        if args.caffe_proto_paths is not None:
            data['TARGET']['CAFFE']['ProtoPaths'] = args.caffe_proto_paths
        if args.caffe_proto_txt_path is not None:
            data['TARGET']['CAFFE']['PrototxtPath'] = args.caffe_proto_txt_path
        if args.caffe_model_path is not None:
            data['TARGET']['CAFFE']['ModelPath'] = args.caffe_model_path
        if args.caffe_remark is not None:
            data['TARGET']['CAFFE']['Remark'] = args.caffe_remark

        # fluid
        if args.fluid_debug is not None:
            data['TARGET']['FLUID']['Debug'] = args.fluid_debug
        if args.fluid_model_path is not None:
            data['TARGET']['FLUID']['ModelPath'] = args.fluid_model_path
        if args.fluid_net_type is not None:
            data['TARGET']['FLUID']['NetType'] = args.fluid_net_type

        # lego
        if args.lego_proto_path is not None:
            data['TARGET']['LEGO']['ProtoPath'] = args.lego_proto_path
        if args.lego_prototxt_path is not None:
            data['TARGET']['LEGO']['PrototxtPath'] = args.lego_prototxt_path
        if args.lego_model_path is not None:
            data['TARGET']['LEGO']['ModelPath'] = args.lego_model_path

        # tensorflow
        if args.tensorflow_model_path is not None:
            data['TARGET']['TENSORFLOW']['ModelPath'] = args.tensorflow_model_path
        if args.tensorflow_outputs is not None:
            data['TARGET']['TENSORFLOW']['OutPuts'] = args.tensorflow_outputs

        # onnx
        if args.onnx_model_path is not None:
            data['TARGET']['ONNX']['ModelPath'] = args.onnx_model_path

        # houyi
        if args.houyi_model_path is not None:
            data['TARGET']['HOUYI']['ModelPath'] = args.houyi_model_path
        if args.houyi_weights_path is not None:
            data['TARGET']['HOUYI']['WeightsPath'] = args.houyi_weights_path

    def check_protobuf_version(self):
        """
        Check if the pip-protoc version is equal to sys-protoc version.
        """
        assert sys.version_info[0] == 2
        for path in sys.path:
            module_path = os.path.join(path, 'google', 'protobuf', '__init__.py')
            if os.path.exists(module_path):
                with open(module_path) as f:
                    __name__ = '__main__'
                    exec(f.read(), locals())
                break
        try:
            protoc_out = subprocess.check_output(["protoc", "--version"]).split()[1]
        except OSError as exc:
            raise OSError('Can not find Protobuf in system environment.')
        try:
            __version__
        except NameError:
            raise OSError('Can not find Protobuf in python environment.')
        sys_versions = map(int, protoc_out.split('.'))
        pip_versions = map(int, __version__.split('.'))
        assert sys_versions[0] >= 3 and pip_versions[0] >= 3, \
            "Protobuf version must be greater than 3.0. Please refer to the Anakin Docs."
        assert pip_versions[1] >= sys_versions[1], \
            "ERROR: Protobuf must be the same.\nSystem Protobuf %s\nPython Protobuf %s\n" \
            % (protoc_out, __version__) + "Try to execute pip install protobuf==%s" % (protoc_out)

    def generate_pbs_of_anakin(self):
        protoFilesStr = subprocess.check_output(["ls", "parser/proto/"])
        filesList = protoFilesStr.split('\n')
        protoFilesList = []
        for file in filesList:
            suffix = file.split('.')[-1]
            if suffix == "proto":
                protoFilesList.append("parser/proto/" + file)
        if len(protoFilesList) == 0:
            raise NameError('ERROR: There does not have any proto files in proto/ ')
        self.__refresh_pbs(protoFilesList, "parser/proto/")

    def pbs_eraser(self, folder_path):
        """
        Delete existing pb2 to avoid conflicts.
        """
        for file_name in os.listdir(os.path.dirname(folder_path)):
            if "pb2." in file_name:
                file_path = os.path.join(folder_path, file_name)
                os.remove(file_path)

    def __refresh_pbs(self, proto_list, default_save_path="parser/pbs/"):
        """
        Genetrate pb files according to proto file list and saved to parser/pbs/ finally.
        parameter:
                proto_list: ['/path/to/proto_0','/path/to/proto_1', ... ]
                default_save_path: default saved to 'parser/pbs/'
        """
        self.pbs_eraser(default_save_path)
        assert type(proto_list) == list, \
        "The ProtoPaths format maybe incorrect, please check if there is any HORIZONTAL LINE."
        for pFile in proto_list:
            assert os.path.exists(pFile), "%s does not exist.\n" % (pFile)
            subprocess.check_call(['protoc', '-I',
                                   os.path.dirname(pFile) + "/",
                                   '--python_out', os.path.dirname(default_save_path) + "/", pFile])


# stocker l'image das un variable

from PIL import Image

# Calculer le nombre de personne présente sur l'image
# sachant qu'un personne correspond aux pixels entre M - 10 et M
# 1 personne = 40 pixel dans le techlab
# convertir en entier

# print(int(count / 40))

# importer les 8 images i.png pour i allant de 1 à 8, les convertir en gris et les stocker dans une matrice

# Path: img.jpg

im1 = Image.open("images/1.jpg")

# calculer le nombre de personne présente sur chaque image

def countPerson(img):
    img_grey = img.convert("L")
    width, height = img_grey.size
    nb_pixels_per_person = 70
    pixelsGrey = [[0 for x in range(width)] for y in range(height)]
    for x in range(width):
        for y in range(height):
            g = img_grey.getpixel((x, y))
            pixelsGrey[y][x] = g
    M = max(
        [max(pixelsGrey[i]) for i in range(len(pixelsGrey))]
    )
    count = 0
    for i in range(len(pixelsGrey)):
        for j in range(len(pixelsGrey[0])):
            if pixelsGrey[i][j] >= M - 10:
                count += 1
    return int(count / nb_pixels_per_person)

# calculer le nombre de personne présente sur chaque image
# et afficher le résultat

print(countPerson(im1))


from tkinter import *

def dropDownMenuTest():
    print("I'm just testing something!")

window = Tk()

menu = Menu(window)
window.config(menu=menu)

subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New Project", command=dropDownMenuTest)
subMenu.add_command(label="File", command=dropDownMenuTest)
subMenu.add_separator()
subMenu.add_command(label="New...", command=dropDownMenuTest)

anotherSubMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=anotherSubMenu)
anotherSubMenu.add_command(label="Undo", command=dropDownMenuTest)

window.mainloop()


from apiDev.models import Store, Product, Category, Customer, Order
from apiDev.serializers import StoreSerializer, ProductSerializer, CategorySerializer, CustomerSerializer, OrderSerializer, RegisterSerializer
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.authtoken.serializers import AuthTokenSerializer
from rest_framework import viewsets, status
from knox.auth import AuthToken

# user auth views
@api_view(['POST'])
def login_view(request):
    serializer = AuthTokenSerializer(data=request.data)
    serializer.is_valid(raise_exception=True)
    user = serializer.validated_data['user']

    _, token = AuthToken.objects.create(user)

    return Response({
        'user_info':
        {
            'id':user.id,
            'username':user.username,
        },
        'token':token
    })

@api_view(["GET"])
def get_user_data(request):
    user = request.user

    if user.is_authenticated:
        return Response({
            'user_info':{
                "id":"user.id",
                "username":user.username,
            },

        })

    return Response({'error':'You are not authorize to access this end point'}, status=status.HTTP_400_BAD_REQUEST)

@api_view(['POST'])
def register_user(request):
    serializer = RegisterSerializer(data = request.data)
    serializer.is_valid(raise_exception=True)

    user = serializer.save()

    _, token = AuthToken.objects.create(user)

    return Response({
                        'user_info':
                        {
                            'id':user.id,
                            'username':user.username,
                        },
                        'token':token
                    })

class StoreViewSet(viewsets.ModelViewSet):
    serializer_class = StoreSerializer
    queryset = Store.objects.all()

class ProductViewSet(viewsets.ModelViewSet):
    serializer_class = ProductSerializer
    queryset = Product.objects.all()

class CategoryViewSet(viewsets.ModelViewSet):
    serializer_class = CategorySerializer
    queryset = Category.objects.all()

class CustomerViewSet(viewsets.ModelViewSet):
    serializer_class = CustomerSerializer
    queryset = Customer.objects.all()

class OrderViewSet(viewsets.ModelViewSet):
    serializer_class = OrderSerializer
    queryset = Order.objects.all()


import ssl
import json
from datetime import datetime
import pandas as pd
from util import company_rename, most_common, strip_cols, idify, get_company_list, apply_system_id, set_cwd_to_script, updated_month_year
ssl._create_default_https_context = ssl._create_unverified_context
set_cwd_to_script()

def listify(series):
    series = series.fillna(value=0)
    series = [int(x) for x in series]
    series = list(series)
    return series

def optimize_json(df):
    delete_after_meta = ['Event Number',
                         'Company Name',
                         'Dig Count',
                         'Activity Unplanned',
                         'Activity Acquiring New Land In Agreement With Landowner',
                         'Nearest Populated Centre',
                         'New Land Area Needed',
                         'Length Of Replacement Pipe']

    for delete in delete_after_meta:
        del df[delete]

    df['year'] = df['Commencement Date'].dt.year
    for col in df.columns:
        if "date" in col.lower():
            del df[col]

    # turn column names into id's
    df = df.rename(columns={"Integrity Dig": "id",
                            "Province/Territory": "p",
                            "Fish Present": "fp",
                            "In Stream Work Required": "is",
                            "Species At Risk Present": "sr"})
    # group data here
    series = {}
    for col in df.columns:
        if col != "year":
            df_series = df[['year', col]].copy()
            df_series[col] = df_series[col].fillna(value=0)
            df_series['value'] = 1
            df_series = df_series.groupby(by=['year', col]).count()
            df_series = df_series.reset_index()
            df_series = pd.pivot_table(df_series,
                                      values="value",
                                      index=['year'],
                                      columns=[col])
            df_series = df_series.reset_index()
            df_series = df_series.sort_values(by="year")
            this_series = {}
            this_data = []
            for s_name in df_series.columns:
                if s_name != 'year':
                    this_data.append({"id": s_name,
                                     "data": listify(df_series[s_name])})
                else:
                    this_series[s_name] = listify(df_series[s_name])
            this_series["data"] = this_data
            series[col] = this_series
    return series

def metadata(df, company, test):
    def filter_near(city):
        if len(city) <= 2:
            return False
        else:
            return True

    this_company_meta = {}
    this_company_meta["totalEvents"] = int(len(list(set(df['Event Number']))))
    this_company_meta["totalDigs"] = int(df['Dig Count'].sum())

    # nearby in the last year
    df['Nearest Populated Centre'] = [str(x) for x in df['Nearest Populated Centre']]
    df['Nearest Populated Centre'] = [x.split(",")[0].strip() for x in df['Nearest Populated Centre']]

    df_near = df.copy()
    filter_list = ['various',
                   'various locations as per the attached documents.',
                   'as per the attached documents',
                   'as per the attached document',
                   'business']

    df_near = df_near[~df_near['Nearest Populated Centre'].str.lower().isin(filter_list)]
    if not test:
        last_full_year = datetime.today().year - 1
    else:
        last_full_year = 2020
    df_near = df_near[df_near['Commencement Date'].dt.year == last_full_year]
    if not df_near.empty:
        # deal with mnp
        city = []
        for city_string in df_near["Nearest Populated Centre"]:
            if "The project is located" in city_string:
                city.append(city_string.split("of")[-1].strip())
            else:
                city.append(city_string)
        df_near["Nearest Populated Centre"] = city
        for split_char in [",", "("]:
            df_near['Nearest Populated Centre'] = [x.split(split_char)[0].strip() for x in df_near['Nearest Populated Centre']]
        near_list = list(df_near['Nearest Populated Centre']+" "+df_near['Province/Territory'].str.upper())
        near_list = [x.replace("Jasper BC", "Jasper AB") for x in near_list]
        near_list = filter(filter_near, near_list)

        df_near = pd.DataFrame(near_list)
        most_common(df_near,
                    this_company_meta,
                    0,
                    "nearby",
                    3,
                    "list",
                    False,
                    False)
        this_company_meta["nearbyYear"] = last_full_year
    else:
        this_company_meta["nearby"] = None

    this_company_meta["atRisk"] = sum([1 if x == "y" else 0 for x in df['Species At Risk Present']])
    # RLG didnt want the land statistic
    # new_land = df['New Land Area Needed'].sum()
    # this_company_meta["landRequired"] = int(new_land)
    # this_company_meta["iceRinks"] = int(round((new_land*2.471)/0.375, 0))
    this_company_meta["company"] = company
    return this_company_meta

def column_insights(df):
    df = idify(df, "Province/Territory", "region")
    return df

def process_oandm(remote=False, companies=False, test=False):

    lang = "en"
    if remote:
        link = "https://can01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.cer-rec.gc.ca%2Fopen%2Foperations%2Foperation-and-maintenance-activity.csv&data=04%7C01%7CMichelle.Shabits%40cer-rec.gc.ca%7Cbbc3fece7b3a439e253908d8f9ec4eab%7C56e9b8d38a3549abbdfc27de59608f01%7C0%7C0%7C637534140608125634%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=HvG6KtuvEzJiNy4CZ4OyplKnfx2Zk5sPjUNNutoohic%3D&reserved=0"

        print('downloading remote oandm file')
        df = pd.read_csv(link,
                         skiprows=0,
                         encoding="latin-1",
                         engine="python",
                         error_bad_lines=False)

        df.to_csv("./raw_data/oandm_"+lang+".csv", index=False)
    elif test:
        print('reading test oandm file')
        path = "./raw_data/test_data/oandm_en.csv"
        df = pd.read_csv(path,
                         skiprows=0,
                         encoding="utf-8",
                         error_bad_lines=False)

    else:
        print('reading local oandm file')
        if lang == 'en':
            path = "./raw_data/oandm_en.csv"
            encoding = "utf-8"

        df = pd.read_csv(path,
                         skiprows=0,
                         encoding=encoding,
                         error_bad_lines=False)

    df = strip_cols(df)
    df = df.rename(columns={x: x.replace("\xa0", " ") for x in df.columns})
    df = df.replace({"Yes": "y", "No": "n"})
    # Event Number and nearest populated center should be deleted later
    # New Land Area Needed is probably the total land
    for delete in ['Company City',
                   'Company Postal Code',
                   'Company Province/Territory',
                   'Circumstance(s)',
                   'Result Of A Class Location Change',
                   'Distance To Closest Building',
                   'Event Creation Date',
                   'Submission Date',
                   'Pipeline Name',
                   'Pipeline Outside Diameter',
                   'Pipeline Length',
                   'Commodity Carried',
                   'Facility Name',
                   'Facility Type',
                   'New Permanent Land Needed',
                   'Activity Acquiring New Private Land',
                   'Activity Acquiring New Land Under Compliance',
                   'Land Within Critical Habitat',
                   'Activity Crossing Water Body',
                   'New Temporary Land Needed',
                   'Vehicle Crossing Count',
                   'Provincial and federal authorities been consulted',
                   'Activity Using Navigable Water',
                   'Activity Following DFO Fish Measures For In Stream Work',
                   'Navigable Water Frozen Or Dry',
                   'Activity Following DFO Fish Measures For Crossing',
                   'Ground Disturbance Near Water Required',
                   'Navigable Water Activity Meeting Transport Canada Minor Works And Waters Order']:
        del df[delete]

    for date_col in df.columns:
        if "date" in date_col.lower():
            df[date_col] = pd.to_datetime(df[date_col])

    df['Company Name'] = df['Company Name'].replace(company_rename())
    df = apply_system_id(df, "Company Name")
    df = column_insights(df)
    df = df.rename(columns={"Species At Risk Present At Activity Site": "Species At Risk Present"})
    df = df[df['Commencement Date'].dt.year >= 2015].reset_index(drop=True)
    if companies:
        company_files = companies
    else:
        company_files = get_company_list("all")

    for company in company_files:
        try:
            folder_name = company.replace(' ', '').replace('.', '')
            df_c = df[df['Company Name'] == company].copy().reset_index(drop=True)
            df_c = df_c.drop_duplicates(subset=['Event Number'])
            this_company_data = {}
            if not df_c.empty:
                this_company_data["meta"] = metadata(df_c, company, test)
                this_company_data["build"] = True
                this_company_data["data"] = optimize_json(df_c)
                if not test:
                    with open('../data_output/oandm/'+folder_name+'.json', 'w') as fp:
                        json.dump(this_company_data, fp)
            else:
                # there are no o and m events
                this_company_data['data'] = df_c.to_dict(orient='records')
                this_company_data['meta'] = {"companyName": company}
                this_company_data["build"] = False
                if not test:
                    with open('../data_output/oandm/'+folder_name+'.json', 'w') as fp:
                        json.dump(this_company_data, fp)
            print("completed: "+company)
        except:
            print("o&m error: "+company)
            raise

    updated_month_year("oandm")
    return this_company_data

if __name__ == '__main__':
    print('starting oandm...')
    df_ = process_oandm(remote=True, test=False)
    print('completed oandm!')


import sys
from PySide6 import QtWidgets
# import wx
from demo.MainTreeWidget import MainTreeWidget

def main():
    # app = wx.App()
    app.MainLoop()

if __name__ == "__main__":
    app = QtWidgets.QApplication([])

    widget = MainTreeWidget()
    widget.resize(300, 1200)
    widget.show()

    sys.exit(app.exec())


# -*- coding: utf-8 -*-
"""
Created on Fri Nov 27 2020

@author: Este
"""

j1 = input("Quel est ton nom ? ")
chances = 8
score = 0
import random
liste_mots = open('mots.txt','r')
liste = liste_mots.readlines()
mot = (liste[random.randint(0,11)])
liste_mots.close()
longueur = len(mot)
barre = "_ "
resultat = mot[0] + " " + barre*(longueur-1)
print(resultat)
restant = longueur
citees=[]

while chances != 0 and restant != 0 :
    lettre = input("Choisir une lettre : ")
    if lettre in citees :
        print("Déjà donnée")
        print(resultat)
    elif lettre in mot :
        print("Oui")
        nombre = mot.count(lettre)
        resultat = resultat.split(" ")
        position = 0
        while nombre != 0 :
            position = int(mot.index(lettre,position + 1))
            resultat.pop(position)
            resultat.insert(position,lettre)
            nombre += -1
        resultat=" ".join(resultat)
        citees.append(lettre)
        restant += -1
        print(resultat)
    else :
        print("Non")
        citees.append(lettre)
        chances += -1
        print("Vie(s) restante(s) : ",chances)
        print(resultat)

if chances == 0 :
    print("Perdu")
elif restant == 0 :
    print("Gagné")
    score += 1


def solution(keymap, targets):
    answer = []
    dic = {}
    for i in keymap:
        for j in range(len(i)):
            if i[j] not in dic: dic[i[j]] = j+1
            else: dic[i[j]] = min(dic[i[j]], j+1)
    for i in targets:
        tem = 0
        for j in i:
            if j not in dic:
                tem = -1
                break
            else : tem += dic[j]
        answer.append(tem)

    return answer

프로그래머스/unrated/160586. 대충 만든 자판/대충 만든 자판.py


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup
import datetime
import time
import os

# Specify the path where chromedriver is installed
chrome_path = r"C:\Users\Siddhant Mahurkar\Desktop\chromedriver.exe"

driver = webdriver.Chrome(chrome_path)
driver.get("http://sat.gov.in/scripts/search.asp")

def get_data(year, month):

	date, appeal_no, ma_ra_no, title, pdf_link = [],[],[],[],[]

	select_year = Select(driver.find_element_by_xpath('//*[@id="form1"]/table/tbody/tr[2]/td/p/font/select[1]'))
	select_year.select_by_visible_text(year)

	select_month = Select(driver.find_element_by_xpath('//*[@id="form1"]/table/tbody/tr[2]/td/p/font/select[2]'))
	select_month.select_by_visible_text(month)

	driver.find_element_by_xpath('//*[@id="form1"]/table/tbody/tr[2]/td/p/font/input').click()

	a = []
	ar = driver.find_element_by_xpath('//*[@id="form1"]/table').get_attribute(('innerHTML'))
	table = BeautifulSoup(ar, "lxml")
	# for tr in table.find_all('tr'):
	# 	a.append(tr)

	for links in table.find_all('a'):

		link = links["href"]
		link = link[2:]

		pdf_link.append("http://sat.gov.in" + str(link))
		title.append(links.text)

	row_count = len(title)
	# t_data = driver.find_element_by_xpath('//*[@id="form1"]/table/tbody/tr[4]/td[1]')
	# t_data = t_data.text

	# for i in range(4,(((row_count+1)*2)+2),2):

	for i in range(4,(4 + (row_count)*2)+1,2):

		t_data_date = driver.find_element_by_xpath('//*[@id="form1"]/table/tbody/tr[' + str(i) + ']/td[1]')
		date.append(t_data_date.text)

		t_data_appeal = driver.find_element_by_xpath('//*[@id="form1"]/table/tbody/tr[' + str(i) + ']/td[2]')
		appeal_no.append(t_data_appeal.text)

		t_data_mara = driver.find_element_by_xpath('//*[@id="form1"]/table/tbody/tr[' + str(i) + ']/td[3]')
		ma_ra_no.append(t_data_mara.text)

	return date, appeal_no, ma_ra_no, title, pdf_link
# //*[@id="form1"]/table/tbody/tr[4]
# //*[@id="form1"]/table/tbody/tr[100]/td[2]
# //*[@id="form1"]/table/tbody/tr[120]/td[2]
# //*[@id="form1"]/table/tbody/tr[122]/td[2]
# //*[@id="form1"]/table/tbody/tr[78]/td[2]

date, appeal_no, ma_ra_no, title, pdf_link = get_data('2018', 'March')

print(date)
# print(len(date))
print("\n")
print(appeal_no)
# print(len(appeal_no))
print("\n")
print(ma_ra_no)
# print(len(ma_ra_no))
print("\n")
print(title)
# print(len(title))
print("\n")
print(pdf_link)
print(len(pdf_link))


# livebio-tg plugin
__plugin__ = {"name": "discord", "author": "LaptopCat", "version": "1.4", "link": "https://github.com/LaptopCat/livebio-plugins/blob/main/plugins/discord/README.md"} # plugin manifest
from config import Config
from websockets import connect
from json import loads, dumps
from helpers import console
discord = Config.plugins.discord
print=console.print
from asyncio import run, create_task, sleep
heartbeat = 0
activity = []
activities = []
def activity_parser(strings, activities):
  result = []
  for i in activities:
      result2 = [strings[i["type"]], i["name"]]
      if i.get('id') == "spotify:1":
        result2.append(i["state"])
        result2.append(i["details"])
      else:
        if i['type'] == 1:
          result2.append(i["details"])
        elif i["type"] == 4:
          result2.append(i["state"])
        else:
          result2.append(i["name"])
      result.append(result2)
  return result
def activity_selector(activities, config):
    print(activities)
    if len(activities) != 0:
        if config.pass_custom:
            if len(activities) > 1:
                return activities[1]
            else:
                return activities[0]
        else:
            if activities[0][0] == config.get_logstring("activities")[4]:
                return activities[1]
            else:
                return activities[0]
discord.activity_selector = getattr(discord, "activity_selector", activity_selector)
discord.activity_parser = getattr(discord, "activity_parser", activity_parser)
discord.logstrings = getattr(discord, "logstrings", None)
discord.gateway_url = getattr(discord, "gateway", "wss://gateway.discord.gg/?v=10&encoding=json")
discord.pass_custom = getattr(discord, "pass_custom", False)
default_logstrings = {
        "ready": "\[discord] Client is up! Running as [bold blue]{}#{}[/bold blue]",
        "reconnecting": "\[discord] Reconnecting to gateway",
        "activities": ["Playing", "Streaming", "Listening to", "Watching", "", "Competing in"],
        "by": "by"
}
def logstring(name):
  return getattr(discord.logstrings, name, default_logstrings[name])
discord.get_logstring = logstring
async def send_message(ws, opcode, data={}):
    await ws.send(dumps({"op": opcode, "d": data}))

async def heartbeats(ws, event):
    while not event.is_set():
      try:
        await send_message(ws, 1)
        await sleep(heartbeat)
      except:
        return
async def handle_message(ws, msg, event):
    global heartbeat, activities
    if msg["op"] == 10:
        heartbeat = msg["d"]["heartbeat_interval"]/1000
        create_task(heartbeats(ws, event))
        await send_message(ws, 2, {"token": discord.token, "properties": {
                    "os": "linux",
                    "browser": "firefox",
                    "device": "pc"
                }, "presence": {
                    "activities": [],
                    "status": "online",
                    "since": 91879201,
                    "afk": False
                },
                "intents": 256})
    elif msg["op"] == 1:
        await send_message(ws, 11)
    elif msg["op"] == 0:
        if msg["t"] == "READY":
            user = msg["d"]["user"]
            console.log(logstring("ready").format(user['username'], user['discriminator']))
        elif msg["t"] == "PRESENCE_UPDATE":
            data = msg["d"]
            if data["user"]["id"] == str(discord.user):
              activities = discord.activity_parser(logstring("activities"), data["activities"])
    elif msg["op"] == "7" or msg["op"] == 9:
        console.log(logstring("reconnecting") + " (Opcode {})".format(msg["op"]))
        return "RECONNECT"
async def main(event):
  async for ws in connect(discord.gateway_url):
      try:
            while not event.is_set():
                msg = loads(await ws.recv())
                if await handle_message(ws, msg, event) == "RECONNECT":
                    await ws.close()
            return
      except Exception as e:
        console.log(logstring("reconnecting") + " ({}: {})".format(type(e).__name__, str(e)))

async def gather(): # This function is called to give back the string that needs to be added
  global actname, doing, activity
  if len(activities)<1:
    return ""
  actname = ""
  doing = ""
  activity = discord.activity_selector(activities, discord)
  if activity is None:
    return ""
  try:
      actname = activity[2]
      doing = activity[0]
      if doing == "":
        action = actname
      else:
        if len(activity) == 4:
          action = f'{doing} {activity[3]} {logstring("by")} {activity[2]}'
        else:
          action = f"{doing} {actname}"
  except:
    action = ""
  return action

async def postprocess(generated, old):
    generated = str(generated)
    action = None
    complete = False
    phase = 0
    if len(activity) == 4:
      while complete is False:
        if len(generated) < Config.script.max_length:
          return action
          complete = True
        if phase == 0:
          action = f'{doing} {activity[2]} - {activity[3]}'
          generated=generated.replace(old,action,1)
          phase = 1
        elif phase == 1:
          old = str(action)
          action = f'{doing} {activity[3]} {logstring("by")} {activity[2].split(", ")[0].split("; ")[0]}'
          generated=generated.replace(old,action,1)
          phase = 2
        elif phase == 2:
          old = str(action)
          action = f'{doing} {activity[2].split(", ")[0].split("; ")[0]} - {activity[3]}'
          generated=generated.replace(old,action,1)
          phase = 3
        elif phase == 3:
          old = str(action)
          action = f'{doing} {activity[1]}'
          generated=generated.replace(old,action,1)
          phase = 4
        elif phase == 4:
          old = str(action)
          action = ""
          generated=generated.replace(old,action,1)
          phase = 5
        else:
          return action
    elif doing != logstring("activities")[4] and actname != "":
      while complete is False:
        if len(generated) < Config.script.max_length:
          return action
          complete = True
        if phase == 0:
          action = f'{doing} {actname}'
          generated=generated.replace(old,action,1)
          phase = 1
        elif phase == 1:
          old = str(action)
          action = f'{doing}'
          generated=generated.replace(old,action,1)
          phase = 2
        elif phase == 2:
          old = str(action)
          action = ""
          generated=generated.replace(old,action,1)
          phase = 3
        else:
          return action
def setup(event):
  run(main(event))


# NIM/Nama : 16520289/Gagas Praharsa Bahar
# Tanggal  : 19 Oktober 2020
# Deskripsi: Problem 2 - Program Kalkulator

# Kamus
# Angka pertama = a
# Angka kedua = b
# Operator = op
# Hasil = hasil

#masukan
a = int(input("Masukkan angka pertama: ")) #Input angka pertama
b = int(input("Masukkan angka kedua: ")) #Input angka kedua
op = input("Masukkan operator: ") #Input tipe operator

#proses
if op == "+":
    hasil = a + b #penjumlahan
elif op == "-":
    hasil = a - b #pengurangan
elif op == "*":
    hasil = a * b #perkalian
elif op == "/":
    hasil = a // b #pembagian dengan pembulatan kebawah
elif op == "%":
    hasil = a % b #sisa pembagian

#keluaran
print(a," ", op," ", b, " = ", hasil)


import math

import pygame as pygame

from road import Road
from visualisation import from_id

class RoadAnimation:
    road: Road
    screen_width: int
    screen_height: int
    road_width: int
    speed: float
    vehicle_size: float
    screen: pygame.Surface

    def __init__(self,
                 road: Road,
                 screen_width: int = 400,
                 screen_height: int = 600,
                 road_width: int = 10,
                 speed: float = 1.,
                 vehicle_size: float = 2):
        self.road = road
        self.screen_width = screen_width
        self.screen_height = screen_height
        self.road_width = road_width
        self.speed = speed
        self.radius = min(screen_width, screen_height) * 0.4
        self.vehicle_size = vehicle_size
        self.screen = pygame.display.set_mode([self.screen_width, self.screen_height], flags=pygame.HIDDEN)

    def draw_car(self, position, color):
        angle = 2 * position/self.road.length * math.pi

        car_width = self.road_width * 1.1
        car_length = 2 * math.pi * self.radius * self.vehicle_size / self.road.length

        center_x = self.screen_width // 2 + self.radius * math.cos(angle)
        center_y = self.screen_height // 2 + self.radius * math.sin(angle)

        width_diff_x, width_diff_y = car_width/2 * math.cos(angle), car_width/2 * math.sin(angle)
        length_diff_x, length_diff_y = car_length/2 * math.sin(angle), - car_length/2 * math.cos(angle)

        border = [[center_x + a * width_diff_x + b * length_diff_x, center_y + a * width_diff_y + b * length_diff_y]
                  for a, b in [(1.25, 1.15), (1.25, -1.15), (-1.25, -1.15), (-1.25, 1.15)]]
        corners = [[center_x + a * width_diff_x + b * length_diff_x, center_y + a * width_diff_y + b * length_diff_y]
                   for a, b in [(1, 1), (1, -1), (-1, -1), (-1, 1)]]
        pygame.draw.polygon(self.screen, (0, 0, 0), border)
        pygame.draw.polygon(self.screen, color, corners)

    def draw_road(self):
        pygame.draw.circle(self.screen, (100, 100, 100), (self.screen_width // 2, self.screen_height // 2),
                           self.radius + self.road_width/2, self.road_width)

    def draw_frame(self, t):
        self.draw_road()
        positions = [self.road.road_data[t, n][0] for n in range(self.road.number_of_vehicles)]
        for i, pos in enumerate(positions):
            self.draw_car(pos, from_id(i))

    def show(self):
        self.screen = pygame.display.set_mode([self.screen_width, self.screen_height], flags=pygame.SHOWN)
        pygame.init()
        running = True
        clock = pygame.time.Clock()
        limit = self.road.time_horizon if self.road.crashed_at < 0 else self.road.crashed_at
        fps = int(30 * self.speed)

        for t in range(limit):
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
            if not running:
                self.screen = pygame.display.set_mode([self.screen_width, self.screen_height], flags=pygame.HIDDEN)
            self.screen.fill((255, 255, 255))
            self.draw_frame(t)
            pygame.display.flip()
            clock.tick(fps)

    def dispose(self):
        pygame.quit()


import cv2 as cv
import numpy as np

blank = np.zeros((500,500,3),dtype='uint8')
cv.imshow('Blank',blank)

'''img = cv.imread("Images\img2.jpg")
cv.imshow('Image',img)'''

#Paint the image
'''blank[:]=0,255,0
cv.imshow('Green',blank)

blank[0:100,10:110]=0,0,255
cv.imshow('Red',blank)'''

#Draw a rectangle
cv.rectangle(blank,(0,0),(blank.shape[0]//3,blank.shape[1]//3),(255,0,0),thickness=-1)
cv.imshow("Rectangle",blank)

cv.rectangle(blank,(0,0),(200,200),(255,0,0),thickness=-1)
cv.imshow("Rectangle",blank)

#Draw a circle
blank1 = np.zeros((500,500,3),dtype='uint8')
cv.circle(blank1,(blank1.shape[0]//2,blank1.shape[1]//2),50,(200,200,200),thickness=-1)
cv.imshow('Circle',blank1)

#Draw a line
cv.line(blank1,(0,0),(250,250),(255,255,255),thickness=4)
cv.imshow('Line',blank1)

#Write text on an image
cv.putText(blank1,'I am C.V.Anish',(250,250),cv.FONT_HERSHEY_DUPLEX,1,(0,0,255),2)
cv.imshow("Text",blank1)

cv.waitKey(0)


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
from scipy.interpolate import griddata
import os
# matplotlib.style.use('seaborn')
# plt.rc('font', family='serif', serif='Times')
plt.rc('font', variant='small-caps')
plt.rc('text', usetex=False)
plt.rc('xtick', labelsize=10)
plt.rc('ytick', labelsize=10)
plt.rc('axes', labelsize=10)

def plot_decision_boundry_custom_color(name, df, resolution=100, save_dir='results/plots/new_colors/',color_1='#000000',color_2='#ffffff',bd_color='#000000',df_scatter=None,scatter=False):
    ax = plt.figure(figsize=(10,10))
    ax.set_facecolor('white')
    ax1 = plt.gca()
    ax1.get_xaxis().set_visible(False)
    ax1.get_yaxis().set_visible(False)
    x0 = df[0].values.reshape((resolution, resolution))
    x1 = df[1].values.reshape((resolution, resolution))
    z = df['y'].values.reshape(x0.shape)
    cs = plt.contourf(x0, x1, z, levels=[0, 0.7],
    colors=[bd_color, '#ffffff'], extend='both')
    if scatter is not None and scatter is True:
        plt.scatter
    cs.cmap.set_over(color_1)
    cs.cmap.set_under(color_2)
    cs.changed()
    fig = ax.get_figure()
    # plt.title(name)
    plt.savefig(os.path.join(save_dir, name + '.png'),dpi=300)

def plot_decision_boundry_2d(name, df, resolution=100, save_dir='results/plots'):
    print("resolution",resolution)
    ax = df.plot.scatter(x = 0, y = 1, c = 'y', cmap = 'ocean', colorbar = False, marker=".")
    ax.set_xlabel('x0')
    ax.set_ylabel('x1')
    ax.set_title(name)
    x0 = df[0].values.reshape((resolution, resolution))
    x1 = df[1].values.reshape((resolution, resolution))
    z = df['y'].values.reshape(x0.shape)
    ax.contourf(x0, x1,z ,cmap ='ocean')
    fig = ax.get_figure()
    fig.savefig(os.path.join(save_dir, name + '.png'))

def plot_decision_boundry_2d_old(name, df, grid_df):
    ax = grid_df.plot.scatter(x = 0, y = 1, c = 'white') # background
#     train_df.plot.scatter(x = 0, y = 1, c = 'y', cmap = 'tab10', colorbar = False, ax = ax)
    df.plot.scatter(x = 0, y = 1, c = 'y', cmap = 'tab10', colorbar = False, ax = ax)
    ax.set_xlabel('x0')
    ax.set_ylabel('x1')
    ax.set_title(name)

def plot(file_name, x, y, labels, x_low=None, x_high=None, y_low=None, y_high=None):
    def is_valid(i, y_pred):
        if y_pred[i] is None:
            return False
        if x_low is not None and x[i] < x_low:
            return False
        if x_high is not None and x[i] > x_high:
            return False
        if y_low is not None and y_pred[i] < y_low:
            return False
        if y_high is not None and y_pred[i] > y_high:
            return False
        return True

    inds = []
    for i in range(len(x)):
        if is_valid(i, y[1]):
            inds.append(i) # y_pred from gpt

    plt.figure(figsize=(12,6))
    x_valid = x[inds]
    y = np.asarray(y)
    y_valid = y[:, inds]
    colors = ['g', 'b', 'r', 'k', 'y', 'c', 'm']
#     plt.plot(x_valid, y_valid[0], c='g', label='True function')
#     plt.plot(x_valid, y_valid[1], c='b',label='GPT-J')
    for k in range(len(y)):
        plt.plot(x_valid, y_valid[k], c=colors[k], label=labels[k])

    plt.legend()
    plt.title("Robustnesss")
    plt.xlabel('x')
    plt.ylabel('y')

    if file_name is None:
        plt.savefig('./plot.png')
    else:
        try:
            plt.savefig(file_name)
        except:
            plt.savefig('./plot.png')

def plot_function(x_train, y_train, X_out=None, y_out=None):
    plt.figure(figsize=(12,6))
    plt.scatter(x_train, y_train, c='g', label='True function')
    if X_out is not None:
        plt.scatter(X_out, y_out, c='r', label='outliers')
    plt.legend()
    plt.title("1D visualization")
    plt.xlabel('x')
    plt.ylabel('y')
    plt.show()

def eval(y_test_outputs, y_grid_outputs, test_df,plot=False,X_grid=None, y_grid=None,file_name=None):
    valid_test_x = [test_df[0][i] for i in range(len(y_test_outputs)) if y_test_outputs[i] != None]
    valid_test_y = [test_df["y"][i] for i in range(len(y_test_outputs)) if y_test_outputs[i] != None]
    valid_test_y_outputs = [y_test_outputs[i] for i in range(len(y_test_outputs)) if y_test_outputs[i] != None]

    print("Valid #outputs/Total #outputs:%d/%d" % (len(valid_test_x),len(y_test_outputs)))
    l2_err = L2error(np.array(valid_test_y_outputs), np.array(valid_test_y))
    print('L2-error {:.4f}'.format(l2_err))

    if plot and X_grid is not None:
#         y_grid_outputs = list(map(query,grid_prompts))
        valid_plot_x = np.array([X_grid[i,0] for i in range(len(y_grid_outputs)) if y_grid_outputs[i] != None])
        valid_plot_y = [y_grid[i] for i in range(len(y_grid_outputs)) if y_grid_outputs[i] != None]
        valid_plot_y_outputs = np.array([y_grid_outputs[i] for i in range(len(y_grid_outputs)) if y_grid_outputs[i] != None])
        #     for i in range(len(y_grid_outputs)):
#         if y_grid_outputs[i] is not None:
#             x.append(X_grid[i,0])
#             y_true.append(y_grid[i])
#             y_pred.append(y_grid_outputs[i])

        plt.figure(figsize=(12,6))
        plt.scatter(valid_plot_x,valid_plot_y_outputs,c=['b']*len(valid_plot_x),label='GPTJ Predicted Labels')
        plt.plot(valid_plot_x,valid_plot_y,c='g',label='Observed Labels')

        plt.legend()
        plt.title("1D_visualization")
        plt.xlabel('x')
        plt.ylabel('y')
        if file_name is None:
            plt.savefig('./plot.png')
        else:
            try:
                plt.savefig(file_name)
            except:
                plt.savefig('./plot.png')

    return y_test_outputs,len(valid_test_x),l2_err


N = input()
K = input()
S = str(input())
assert 1 <= N <= 10**6
assert 1 <= K <= 10**6

#print(bool(S))
lenT = len(S)

b = S[lenT-1] #-48
newS = str(b)

b = int(b) ^ int(S[lenT-2]) #-48)
newS = str(b) + newS

for i in range(lenT-3, N-K-1, -1):
    lenS = len(newS)
    bb = newS[lenS-1]
    for j in range(lenS-2, -1, -1):
        bb = int(bb)^int(newS[j])
    b = int(S[i])^int(bb)
    newS = str(b) + newS

print(newS)

#ans5 = int(ans4) ^ (arr[lenT-5]-48)
#print(ans5)

#print(toggleBit(int(S[lenT-1]),0))


# -*- coding: utf-8 -*-
"""
Created on Wed Nov 28 19:22:33 2018

@author: sadra
"""

import numpy as np

from pypolycontain.lib.zonotope import zonotope,zonotope_order_reduction_inner,zonotope_distance_cocenter
from pypolycontain.visualization.visualize_2D import visualize_2D_zonotopes as visZ

G=np.array([[1,0,1,1,1,-2,-3,3,2,-1,2,7],[0,1,1,-1,3,2,6,-1,-1,3,6,-1]])
x=np.array([0,1]).reshape(2,1)
Z=zonotope(x,G)

G_r=np.array([[1,0,1,2],[0,2,1,-1]])
(Z_r,Z_r_list)=zonotope_order_reduction_inner(Z,G_r)
for i in range(15):
    e=zonotope_distance_cocenter(Z,Z_r_list[i])
    fig=visZ([Z,Z,Z_r_list[i]],title=r"Iteration %d - Inner Approximation $d_H(\mathbb{Z}_r,\mathbb{Z})=%0.02f$"%(i,e),axis_limit=50)
    fig.savefig('figures/reduction_inner %d'%i,dpi=100)


from dash.dependencies import Input, Output
import numpy as np
import pandas as pd
from sklearn.cluster import KMeans
import dash
# from dash import dcc
# from dash import html
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import plotly.graph_objects as go

# Removing outliers
terror_df = pd.read_csv('/Users/shubhangigupta/Desktop/Data Visualization and communication/Project/2-Global terrorism database dashboard in python by plotly dash/globalterrorismdb_0718dist.csv', encoding ='latin1')
terror_df = terror_df[terror_df['nkill'] <= 4].reset_index(drop=True)
terror_df = terror_df[terror_df['nwound'] <= 7].reset_index(drop=True)
corr_df = terror_df[['region', 'attacktype1', 'targtype1']].corr()

# Selecting features with high variance
features = [
    'longitude',
    'latitude',

    'nwound',
    'nkill',

    'natlty1_txt',
    'targtype1_txt',
    'targsubtype1_txt',
    'weaptype1_txt',
    'attacktype1_txt',
]

X = pd.get_dummies(terror_df[features])
X = X.T[X.var() > 0.05].T.fillna(0)
X = X.fillna(0)

app = dash.Dash(__name__)

graph_style = {'width':'100%', 'display':'inline-block'}

scatters = ['Default', 'K-Means']

app.layout = html.Div([
    html.H1('A Visualization of Global Terrorism Incidents (1970-2017)'),
    html.Div([
        html.P('Plotting Options:'),
        dcc.Dropdown(
            id = 'dropdown',
            options = [{'label': i, 'value': i} for i in scatters],
            value = 'Default'
        ),
        dcc.Graph(id ='scatter', style = graph_style)
    ], style = {'width':'50%', 'display':'inline-block'}),

    html.Div([
        html.P('Features to Correlate:'),
        dcc.Checklist(
            id = 'checklist',
            options = [{'label': x, 'value': x} for x in corr_df.columns],
            value = corr_df.columns.tolist(),
            labelStyle={'display': 'flex'}
        ),
        dcc.Graph(id ='heatmap', style = graph_style)
    ], style = {'width':'50%', 'display':'inline-block'})

], style = graph_style)

@app.callback(
    Output('scatter', 'figure'),
    [Input('dropdown', 'value')]
)
def display_plots(name):
    if name == 'Default':
        fig = display_scatter()
    else:
        fig = display_kmeans()
    return fig

@app.callback(
    Output('heatmap', 'figure'),
    [Input('checklist', 'value')]
)
def display_heatmap(cols):
    fig = px.imshow(corr_df[cols])
    return fig

def display_scatter():
    fig = px.scatter(X, x='longitude', y='latitude', title='Global Terrorism Incidents (1970-2017)')
    return fig

def display_kmeans():
    fig = go.Figure()

    kmeans = KMeans(n_clusters=6)
    kmeans.fit(X)

    # predictions from kmeans

    pred = kmeans.predict(X)
    frame = pd.DataFrame(X)
    frame['cluster'] = pred
    frame = frame[['longitude', 'latitude', 'cluster']]

    frame.head()
    frame.columns = ['longitude', 'latitude', 'cluster']

    clusters_centroids = dict()
    clusters_radii = dict()

    X_arr = X.values
    for k in range(0, 6):
        data = frame[frame["cluster"] == k]
    #
    # frame.head()
    # frame.columns = ['longitude', 'latitude', 'cluster']
    color = ['blue', 'green', 'cyan', 'black', 'magenta', 'yellow', 'red']
    for k in range(0, 6):
        data = frame[frame["cluster"] == k]
        fig.add_trace(go.Scatter(x=data["longitude"], y=data["latitude"], mode='markers'))
        clusters_centroids[k] = list(zip(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1]))[k]
        fig.add_trace(go.Scatter(x=kmeans.cluster_centers_[:, 0], y=kmeans.cluster_centers_[:, 1],
                                 mode='markers', marker_symbol='x', marker_line_color='darkred',
                                 marker_color='orangered'))
        clusters_radii[k] = max(
            [np.linalg.norm(np.subtract(i, clusters_centroids[k])) for i in zip(X_arr[pred == k, 0], X_arr[pred == k, 1])])
        fig.add_shape(type='circle', line_color='blue',
                      x0=min(data['longitude']), y0=min(data['latitude']),
                      x1=max(data['longitude']), y1=max(data['latitude']))

    fig.update_layout(title='Clusters, Centroids and Radii (K-Means)',
                  xaxis_title='longitude',
                  yaxis_title='latitude',
                  showlegend=False)
    return fig

# app.run_server(debug=True, host='0.0.0.0', port=8086, threaded=True)
app.run_server(debug=False)