import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim

import torchvision.transforms as transforms
import torchvision.models as models

from PIL import Image
import matplotlib.pyplot as plt

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
#device = torch.device("cpu")

# define style loss and content loss
# both losses calculate MSE between two sets of feature maps or gram matrices:
#     one coming from the input image, another - from the content or the style image, accordingly

# to compute style loss, we will need a gram matrix as a normalized result of multiplying a matrix
#     by its transposed matrix
# here, the original matrix is a reshaped version of the feature maps of a layer,
#     with dimensions corresponding to the number of feature maps in a layer and their lengths

def gram_matrix(input):
    a, b, c, d = input.size()
    features = input.view(a * b, c * d)
    G = torch.mm(features, features.t())
    return G.div(a * b * c * d)

class StyleLoss(nn.Module):

    def __init__(self, target_feature):
        super(StyleLoss, self).__init__()
        self.target = gram_matrix(target_feature).detach()

    def forward(self, input):
        G = gram_matrix(input)
        self.loss = F.mse_loss(G, self.target)
        return input

class ContentLoss(nn.Module):

    def __init__(self, target, ):
        super(ContentLoss, self).__init__()
        self.target = target.detach()

    def forward(self, input):
        self.loss = F.mse_loss(input, self.target)
        return input

# we will use VGG19. It is trained on normalized images, so we need to niormalize our images as well
# we will build a normalization class to fit it into nn.Sequential

class Normalization(nn.Module):
    def __init__(self, mean, std):
        super(Normalization, self).__init__()
        self.mean = torch.tensor(mean).view(-1, 1, 1)
        self.std = torch.tensor(std).view(-1, 1, 1)

    def forward(self, img):
        return (img - self.mean) / self.std

# function to resize the image, transform it to tensor, and add a fake batch dimension with unsqueeze
def load_image(image_name):

    #imsize = 256 if torch.cuda.is_available() else 128
    imsize = 256

    loader = transforms.Compose([
            transforms.Resize(imsize),
            transforms.CenterCrop(imsize),
            transforms.ToTensor()])

    image = Image.open(image_name)
    image = loader(image).unsqueeze(0)
    return image.to(device, torch.float)

# we will build a new nn.Sequential by putting Normalization in the beginning,
#     iterating over the layers of the initial imported model, and trimming

class Net():
    def __init__(self):
        # import pretrained VGG19 network
        self.cnn = models.vgg19(pretrained=True).features.to(device).eval()
        self.normalization_mean = torch.tensor([0.485, 0.456, 0.406]).to(device)
        self.normalization_std = torch.tensor([0.229, 0.224, 0.225]).to(device)
        self.content_layers = ['conv_4']
        self.style_layers = ['conv_1', 'conv_2', 'conv_3', 'conv_4', 'conv_5']

    def get_model(self, style_img, content_img):
        # normalization
        normalization = Normalization(self.normalization_mean, self.normalization_std).to(device)

        content_losses = []
        style_losses = []

        content_img = load_image(content_img)
        style_img = load_image(style_img)

        # iterate through layers
        model = nn.Sequential(normalization)

        i = 0
        for layer in self.cnn.children():
            if isinstance(layer, nn.Conv2d):
                i += 1
                name = 'conv_{}'.format(i)

            elif isinstance(layer, nn.ReLU):
                name = 'relu_{}'.format(i)
                layer = nn.ReLU(inplace=False)

            elif isinstance(layer, nn.MaxPool2d):
                name = 'pool_{}'.format(i)

            elif isinstance(layer, nn.BatchNorm2d):
                name = 'bn_{}'.format(i)

            else:
                raise RuntimeError('Unrecognized layer: {}'.format(layer.__class__.__name__))

            model.add_module(name, layer)

            # add content loss
            if name in self.content_layers:
                target = model(content_img).detach()
                content_loss = ContentLoss(target)
                model.add_module("content_loss_{}".format(i), content_loss)
                content_losses.append(content_loss)

            # add style loss
            if name in self.style_layers:
                target_feature = model(style_img).detach()
                style_loss = StyleLoss(target_feature)
                model.add_module("style_loss_{}".format(i), style_loss)
                style_losses.append(style_loss)

        # trim off the layers after the last content and style losses
        for i in range(len(model) - 1, -1, -1):
            if isinstance(model[i], ContentLoss) or isinstance(model[i], StyleLoss):
                break

        model = model[:(i + 1)]

        return model, style_losses, content_losses, content_img

    # in this application, we need to optimize not the model parameters, but the input image itself

    def run_style_transfer(self, model, style_losses, content_losses, input_img
                           , num_steps=300, style_weight=1000000, content_weight=1):

        # update requires_grad fields
        input_img.requires_grad_(True)
        model.requires_grad_(False)

        optimizer = optim.LBFGS([input_img])

        run = [0]
        while run[0] <= num_steps:

            def closure():

                # correct the values of updated input (content) image
                with torch.no_grad():
                    input_img.clamp_(0, 1)

                optimizer.zero_grad()
                model(input_img)
                style_score = 0
                content_score = 0

                for sl in style_losses:
                    style_score += sl.loss
                for cl in content_losses:
                    content_score += cl.loss

                style_score *= style_weight
                content_score *= content_weight

                loss = style_score + content_score
                loss.backward()

                run[0] += 1

                return style_score + content_score

            optimizer.step(closure)

        with torch.no_grad():
            input_img.clamp_(0, 1)

        return input_img


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/3/3 14:14
# @Author : 王志宝
# @Site :
# @File : alien.py
# @Software: PyCharm
import pygame
from pygame.sprite import Sprite

class Alien(Sprite):
	def __init__(self, ai_settings, screen):
		super(Alien, self).__init__()
		self.screen = screen
		self.ai_settings = ai_settings

		self.image = pygame.image.load('image/alien.bmp')		#加载图像，得到属性
		self.rect = self.image.get_rect()

		self.rect.x = self.rect.width		#每个外星人最初在屏幕左上角
		self.rect.y = self.rect.height

		self.x = float(self.rect.x)		#存储外星人位置

	def blitme(self):		#在指定位置绘制外星人
		self.screen.blit(self.image, self.rect)

	def check_edges(self):
		screen_rect = self.screen.get_rect()
		if self.rect.right >= screen_rect.right:
			return True
		elif self.rect.left <= 0:
			return True

	def update(self):		#向右移动外星人
		self.x += self.ai_settings.alien_speed * self.ai_settings.fleet_direction
		self.rect.x = self.x


from itertools import combinations
from copy import deepcopy
from collections import deque
R,C =map(int,input().split())
originalL = []
q = deque([])
for i in range(R):
    temp = list(map(int,input().split()))
    originalL.append(temp)

comb = list(combinations([i for i in range(R*C)],3))
dR = [-1,1,0,0]
dC = [0,0,-1,1]
result = 0

def bfs(): #전파 후 안전지대 개수 세기
    global result
    cnt=0
    L = deepcopy(originalL)
    for i in range(R):
        for j in range(C):
            if L[i][j] == 2:
                q.append((i, j))
    while q :
        r,c = q.popleft()
        for i in range(4):
            nextR = r+dR[i]
            nextC = c +dC[i]
            if 0 <= nextC < C and 0 <= nextR < R :
                if L[nextR][nextC] == 0:
                    L[nextR][nextC]=3
                    q.append((nextR,nextC))
    for i in range(R):
        for j in range(C):
            if L[i][j]==0:
                cnt+=1
    result = max(result,cnt)
    # for i in range(R):
    #     for j in range(L):
    #         if L[i][j]==3 :
    #             L[i][j]=0

    return result

for a, b, c in comb:
    c1 = a % C
    r1 = a // C

    c2 = b % C
    r2 = b // C

    c3 = c % C
    r3 = c // C

    if originalL[r1][c1] == 0 and originalL[r2][c2] == 0 and originalL[r3][c3] == 0:
        originalL[r1][c1] = 1
        originalL[r2][c2] = 1
        originalL[r3][c3] = 1
        bfs() #안전지대의 개수
        originalL[r1][c1] = 0
        originalL[r2][c2] = 0
        originalL[r3][c3] = 0

print(result)

#
#
#
#
#
#
#
#
#


# Imports
import pandas as pd
import numpy as np

from sklearn.cross_validation import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import precision_recall_curve
from sklearn.preprocessing import LabelEncoder
import matplotlib.pyplot as plt

# Retrieve Data Set
df = pd.read_csv('http://www.dataminingconsultant.com/data/churn.txt')

# Some Prepreocessing
df.columns = [c.lower().replace(' ', '_').replace('?', '').replace("'", "") for c in df.columns]

state_encoder = LabelEncoder()
df.state = state_encoder.fit_transform(df.state)

del df['phone']

binary_columns = ['intl_plan', 'vmail_plan', 'churn']
for col in binary_columns:
    df[col] = df[col].map({
            'no': 0
        ,   'False.': 0
        ,   'yes': 1
        ,   'True.': 1
    })

# Build the classifier and get the predictions
clf = RandomForestClassifier(n_estimators=50, oob_score=True)
test_size_percent = 0.1

signals = df[[c for c in df.columns if c != 'churn']]
labels = df['churn']

train_signals, test_signals, train_labels, test_labels = train_test_split(signals, labels, test_size=test_size_percent)
clf.fit(train_signals, train_labels)
predictions = clf.predict_proba(test_signals)[:,1]

precision, recall, thresholds = precision_recall_curve(test_labels, predictions)
thresholds = np.append(thresholds, 1)

queue_rate = []
for threshold in thresholds:
    queue_rate.append((predictions >= threshold).mean())

plt.plot(thresholds, precision, color= 'grey')
plt.plot(thresholds, recall, color= 'red')
plt.plot(thresholds, queue_rate, color= 'blue')

leg = plt.legend(('precision', 'recall', 'queue_rate'), frameon=True)
leg.get_frame().set_edgecolor('k')
plt.xlabel('threshold')
plt.ylabel('%')
plt.show()


import time
import numpy as np
import cvxopt

def loadData(fileName):             # 预处理数据，分为-1和1的二分类
    features = []
    labels = []
    fr = open(fileName, 'r')
    for line in fr.readlines():
        curLine = line.strip().split(',')
        if int(curLine[0]) == 0:
            labels.append(1)
        else:
            labels.append(-1)
        features.append([int(num) / 255 for num in curLine[1:]])
    return features, labels

class SVM(object):
    def __init__(self):
        pass

    def _init_parameters_(self, features, labels):              # 初始化参数
        self.m = len(features[0])   # 获取样本的维度(784)
        self.P = np.identity(self.m + 1)   # 目标函数中的二次部分，我们在最后把b也算作一个变量
        self.P[self.m][self.m] = 0            # 将变量b初始化为0
        self.P = cvxopt.matrix(self.P)   # 转换为矩阵形式
        self.q = cvxopt.matrix([0.0] * (self.m + 1))   # 目标函数中的一次部分

    def train(self, features, labels):
        # 约束条件中的不等式
        self._init_parameters_(features, labels)
        G = []
        for i in range(self.m):
            G.append([-features[j][i] * labels[j] for j in range(len(features))])
        G.append([-labels[i] * 1.0 for i in range(len(labels))])
        G = cvxopt.matrix(G)
        h = cvxopt.matrix([-1.0] * (len(labels)))

        # 将参数传递给solvers.qp，返回最优解
        cs = cvxopt.solvers
        result = cs.qp(self.P, self.q, G, h)
        results = result['x']
        w = results[:-1]
        b = results[-1]
        return w, b

    def _predict_(self, w, b, features):
        if (np.dot(w.T, features) + b) >= 0:
            return 1
        else:
            return -1

    def predict(self, w, b, features, labels):
        # 错误值计数
        errorCnt = 0
        # 循环遍历测试集中的每一个样本
        for i in range(len(features)):
            # 获取预测值
            predict = self._predict_(w, b, features[i])
            # 与答案进行比较
            if predict != labels[i]:
                # 若错误  错误值计数加1
                errorCnt += 1
        # 返回准确率
        return 1 - (errorCnt / len(features))

if __name__ == '__main__':
    time_1 = time.time()
    # 获取训练集
    print('start read train data')
    train_features, train_labels = loadData('../data/mnist_train.csv')
    time_2 = time.time()
    print('read train data cost ', time_2 - time_1, ' seconds', '\n')

    # 获取测试集
    print('start read test data')
    test_features, test_labels = loadData('../data/mnist_test.csv')
    time_3 = time.time()
    print('read test data cost ', time_3 - time_2, ' seconds', '\n')

    # 计算最优解w*,b*3
    print('start train')
    svm = SVM()
    w, b = svm.train(train_features[:6000], train_labels[:6000])
    time_4 = time.time()
    print('train cost ', time_4 - time_3, ' seconds', '\n')

    # 对训练得到的最优解进行测试
    print('start predict')
    accuracy = svm.predict(w, b, test_features[:3000], test_labels[:3000])
    time_5 = time.time()
    # 显示正确率
    print ("The accruacy socre is ", accuracy)
    print('predicting cost ', time_5 - time_4, ' seconds', '\n')
    # 显示用时时长
    print('All cost', time_5 - time_1, ' seconds')


import json

fhand = open('moorse.json', 'r')

hello = json.load(fhand)
takein = 'hjc'
while takein != '*':
    takein = input('Enter something here(type *, if you wanna quit): ')
    takein = takein.lower()
    if takein == '*':
        break

    for n in range(len(takein)):
        if takein[n] in hello:
            print(hello[takein[n]], end = ' ')

        elif takein[n] == ' ':
            print('/', end = ' ')

    print('\n\n')


#!python3
from datetime import datetime
import subprocess
import time
import logging

##########
#Startup
##########

print('Input target URL / IP!')
target=input()
print('Pinging to '+target)

##########
#setup logging
##########
logging.basicConfig(filename=target+'.log',format='%(asctime)s - %(message)s',level=logging.INFO)

##########
#Process reference
##########
#output = pingProc.stdout
#outErr = pingProc.stderr
#TraceRoute output
#tOutput = traceProc.stdout
#tOutErr = traceProc.stderr

#print(output,outErr)
#print(tOutput,tOutErr)

##########
#The Loop!
##########

currentTime=datetime.now().hour
print('Hour:',currentTime)

while True:

    time.sleep(1)

    if currentTime != datetime.now().hour:
        currentTime=datetime.now().hour
        print('\nHour:',currentTime)

    pingProc = subprocess.run(["ping",target,"-n","1"],encoding='utf-8',stdout=subprocess.PIPE,stderr=subprocess.PIPE)

    if 'Request timed out.' in pingProc.stdout:
        logging.warning('Ping to '+target+' failed!')
        print('!',flush=True, end="")
        #traceProc = subprocess.run(["tracert",target],encoding='utf-8',stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        #logging.warning(traceProc.stdout)
        #print('!')

    else:
        print('.',flush=True, end="")
        time.sleep(3)
        #print('.')


## below code is expected to start 2 processes on different gpu's and with a parameter server placed in the cpu.
###### The code trains the neural network with 2 hidden layers of size 4096 and ann output layer of 256

import multiprocessing
import time
import tensorflow as tf
from tensorflow.contrib.training import HParams
import os
import psutil
import numpy as np
from tensorflow.python.client import device_lib

cluster_spec ={"ps": ["localhost:2223"
                      ],
    "worker": [
        "localhost:2224",
        "localhost:2225"]}

cluster = tf.train.ClusterSpec(cluster_spec)
#loss = tf.random_normal([1],dtype=tf.float32)
#print(cluster.task_address())
#X = tf.constant(np.random.random_sample(size=(128,256)), dtype=tf.float32)
#labls=tf.constant([1,0,0,1,0,1,0,1,0,1],shape=[10,1],dtype=tf.float32)

def dumb_input_fn():

    x = tf.random_normal([128,256], dtype=tf.float32)
    y = tf.random_normal([128,256], dtype=tf.float32)

    return [x,y]

output_dir = 'tfprojects/output_dir_debug'
#tf.logging.set_verbosity(tf.logging.INFO)  # enables training error print out during training
def model_fn(features,labels):
    # This is is reeeeally slow way to square numbers.
    outputs = tf.layers.dense(
                    inputs = features,
                    units = 4096)
    outputs = tf.layers.dense(
                    inputs = outputs,
                    units = 4096)
    outputs = tf.layers.dense(
                    inputs = outputs,
                    units = 256)

    loss = tf.losses.mean_squared_error(outputs, labels)

    train_op = tf.contrib.layers.optimize_loss(
              loss, None, optimizer='Adam',
                        learning_rate = .0001)

    predictions = {"predictions":tf.identity(outputs,name = 'predictions')}
    return predictions, loss, train_op

def worker(device):
    params = HParams(cluster=cluster,
                     job_name = device[0],
                     task_index = device[1])

    if device[0]=='worker':
        # allow each worker to see only 1 of the 4 GPUS
        os.environ["CUDA_VISIBLE_DEVICES"]=str(params.task_index)

    else:
        # hide all 4 GPUS from ps
        os.environ["CUDA_VISIBLE_DEVICES"]=''

    Workrs(output_dir, params)

class _LoggerHook():
    ## create before run and after run functions that could be used to call certain parameters
    def begin(self):
        self._time=time.time()

    def before_run(self, run_context):
        self._time = time.time()
        return tf.train.SessionRunArgs(loss)  # Asks for loss value.

    def after_run(self, run_context, run_values):
        self._time = time.time()
        duration = time.time() - self._start_time
        loss_value = run_values.results

        format_str = ('loss = %.2f')
        print (format_str % (loss_value))

## The Workrs processes is triggered from the main function. T
def Workrs(output_dir,d={}):
    cluster = d.cluster
    job_name = d.job_name
    task_index = d.task_index
    # tf.train.Server creates a server cluster with the local host address as mentioned in cluster_Spec in tensorflow
    server= tf.train.Server(d.cluster,job_name = d.job_name,task_index=d.task_index)
    print("Task IndeX:",d.task_index)

    if job_name == "ps":
        ##try and wait for all the wokers to finish their tasks.
        ## Tensorflow does not stop the server process on PS to stop. This needs to be manually done by the code
        ## Have to setup a Tensorflow queue which is written to by Worker process once they finish training and read by PS to stop its session from running.
        server.join()
    elif job_name == "worker":
        #tf.train.replica_device_setter is used to automatically assign available devices for the specific task index
        with tf.device(tf.train.replica_device_setter(
                worker_device="/job:worker/replica:0/task:%d" % task_index,
                cluster=cluster)):

            X, labls = dumb_input_fn()
            _,_,train_op=model_fn(X,labls,None,d)

        global_step = tf.contrib.framework.get_or_create_global_step()
        step = 0
        start_time = time.time()
        # These are hooks that are to be used to stop training after a certain global step value has been reached.
        hooks = tf.train.StopAtStepHook(last_step=10)
        # MonitoredTrainingSession sets up the tensorflow threads and queues.
        with tf.train.MonitoredTrainingSession(master=server.target,config=tf.ConfigProto(log_device_placement=True),
                                                   is_chief=(d.task_index == 0),
                                                   checkpoint_dir=output_dir,
                                                   hooks=[hooks]) as mon_sess:

            #ckpt = tf.train.get_checkpoint_state(output_dir)

            while not mon_sess.should_stop():

                _ = mon_sess.run(train_op)
                if step % 5 == 0:
                    print("Step:", step, 10 / (time.time() - start_time), 'steps/sec')
                    print("Proc:",)
                    start_time = time.time()

                step += 1

#def get_available_gpus():
#    local_device_protos = device_lib.list_local_devices()
#    return [x.name for x in local_device_protos if x.device_type == 'GPU']
## Test the multiprocessing on a single cpu vs 4 on the local machine
#print("There are %d CPUs on this machine" % multiprocessing.cpu_count())
#print("There are  CPUs on this machine:" , get_available_gpus())
#pool = multiprocessing.Pool(processes=4)
#start_time = time.time()
#results = [pool.apply_async(cube, args=(x,)) for x in range(17,25)]
#out = [p.get() for p in results]
#print(out)
#start_time = time.time()
#pool.close()
#pool.join()

if __name__ == '__main__':

    logger = multiprocessing.log_to_stderr()
    result = multiprocessing.Queue()
    processes = []
    devices = [['ps', 0],
               ['worker', 0],
               ['worker', 1],
               ]

    for i in (devices):
        start_time = time.time()
        proc = multiprocessing.Process(target=worker,args=(i,))
        processes.append(proc)
        proc.start()

    for p in processes:
        p.join()

    print("time taken = %d" % (start_time - time.time()))

    results = [result.get() for p in processes]
#process_names = [proc.get_cpu_times() for proc in psutil.Process()]


import pandas as pd
import folium
from geopy.geocoders import ArcGIS
nom = ArcGIS()

df = pd.read_csv("supermarkets.csv")
df['location']=df.Address+','+df.City+','+df.State+','+df.Country

add= []
coordinates=[]
lat=[]
long=[]

for item in range(0,6):
    add.append(nom.geocode(df.location[item]))

for item in range(0,6):
    lat.append(add[item].latitude)

for item in range(0,6):
    long.append(add[item].longitude)

map = folium.Map(location=[37.756648011392286,37.757819005175406],zoom_start=6,tiles='Stamen Terrain')

fg = folium.FeatureGroup(name = 'My Map')
for item in range(0,6):
    fg.add_child(folium.Marker(location=[lat[item],long[item]],popup='location no- %s'%item,icon=folium.Icon(color='green')))
    map.add_child(fg)

map.save("customer_map.html")


from functools import wraps
from time import sleep

from telegram import ChatAction

from parameters import CAP

def get_si(number: float, type="s", size=3):
    if type == "s":
        sufs = {
            0: "",
            1: " k",
            2: " M",
            3: " G",
            4: " T",
            5: " P",
            6: " E",
            7: " Z",
            8: " Y",
        }
        exp = 0
        while number // 10 ** (exp * size):
            exp += 1
        exp -= 1 if exp else 0
        if exp <= len(sufs) - 1:
            suf = sufs[exp]
        else:
            exp_temp = exp - (len(sufs) - 1)
            suf = " {}{}".format("A" * (1 + exp_temp // 26), chr(65 + exp_temp % 26))
        return "{:.2f}{}".format(number / 10 ** (exp * size), suf).rstrip("0").rstrip(".").replace(".", "\.")
    elif type == "'":
        return "{:,}".format(int(number)).replace(",", "'")
    elif type == "f":
        return "{:,.2f}".format(number).replace(",", "'").replace(".", "\.")
    else:
        return number

def send_typing_action(func):
    """Sends typing action while processing func command."""

    @wraps(func)
    def command_func(update, context, *args, **kwargs):
        context.bot.send_chat_action(chat_id=update.effective_message.chat_id, action=ChatAction.TYPING)
        sleep(len(update.message.text) // 20)  # This is based on my max WPM hehe.
        return func(update, context, *args, **kwargs)

    return command_func

def power_10(number: int) -> int:
    min_cap = 10 ** (len(str(int(CAP))) - 1)
    try:
        return min(10 ** (len(str(int(number))) - 1), min_cap)
    except OverflowError as e:
        return min_cap

def sanitize_for_markdown(text: str) -> str:
    to_escape = ["_", "*", "[", "]", "(", ")", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!"]
    for char in to_escape:
        text = text.replace(char, "\{}".format(char))
    return text


# -*- coding: utf-8 -*-
import scrapy
import json
import time
from random import choice

from KOL.configure.graphqlQuery import *
from KOL.utils.myredis import RedisClient
from KOL.items import TestCommentUserItem

class TestgraphqlSpider(scrapy.Spider):
    name = 'testGraphql'

    redis_id_set_name = "KuaiShouPrincipalId"
    redisClient = RedisClient('10.8.26.105', 6379, 1)
    #redisClient = RedisClient('10.8.26.26', 6379, 1)

    #principalId = '3xy424mqbve5h82'
    url = "https://live.kuaishou.com/graphql"

    headers = {
        'accept': '*/*',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'content-type': 'application/json',
        'Host': 'live.kuaishou.com',
        'Origin': 'https://live.kuaishou.com',
    }

    '''headers = {
        'content-type': 'application/json',
    }'''

    didList = ['web_398544336a2a4c89bafe36006a1c399d',
               'web_d54ea5e1190a41e481809b9cd17f92aa',
               'web_5a63ef3c91c34b8e853c91b68d92208b',]

    def __init__(self, userId='xiena666', *args, **kwargs):
        super(TestgraphqlSpider, self).__init__(*args, **kwargs)
        #self.idSet = self.redisClient.smembers(self.redis_id_set_name)
        self.userId = userId

    def getCookie(self):
        tempCookie = {}
        tempCookie['did'] = choice(self.didList)
        return tempCookie

    def start_requests(self):
        id = self.userId
        print("cur id:")
        print(id)
        feedQuery = publicFeedsQuery
        feedQuery['variables']['principalId'] = id
        feedQuery['variables']['pcursor'] = '0'
        time.sleep(3)
        yield scrapy.Request(self.url, headers=self.headers, body=json.dumps(feedQuery),
                             method='POST', callback=self.parsePublicFeeds, meta={'bodyJson': feedQuery},
                             cookies=self.getCookie()
                             )
        '''for id in self.idSet:
            print("cur id:")
            print(id)
            feedQuery = publicFeedsQuery
            feedQuery['variables']['principalId'] = str(id, encoding='utf-8')
            feedQuery['variables']['pcursor'] = '0'
            time.sleep(3)
            yield scrapy.Request(self.url, headers=self.headers, body=json.dumps(feedQuery),
                                 method='POST', callback=self.parsePublicFeeds, meta={'bodyJson': feedQuery},
                                 cookies=self.getCookie()
                                 )'''

    def parsePublicFeeds(self, response):
        feedQuery = response.meta['bodyJson']
        if response.status != 200:
            print('get url error: ' + feedQuery)
            return
        rltJson = json.loads(response.text)
        flag = False
        if 'data' in rltJson:
            if 'publicFeeds' in rltJson['data']:
                flag = True
        if not flag:
            print('parse public feeds json error: ')
            print(rltJson)
            return
        pcursor = rltJson['data']['publicFeeds']['pcursor']
        feedList = rltJson['data']['publicFeeds']['list']
        for feed in feedList:
            photoId = feed['photoId']
            print('photoId:' + photoId)
            commentQuery = commentListQuery
            commentQuery['variables']['pcursor'] = '0'
            commentQuery['variables']['photoId'] = photoId
            time.sleep(2)
            yield scrapy.Request(self.url, headers=self.headers, body=json.dumps(commentQuery),
                                 method='POST', callback=self.parseCommentByPhotoId, meta={'bodyJson': commentQuery},
                                 cookies=self.getCookie()
                                 )
        if pcursor != 'no_more':
            feedQuery['variables']['pcursor'] = pcursor
            time.sleep(2)
            yield scrapy.Request(self.url, headers=self.headers, body=json.dumps(feedQuery),
                                 method='POST', callback=self.parsePublicFeeds, meta={'bodyJson': feedQuery},
                                 cookies=self.getCookie()
                                 )

    def parseCommentByPhotoId(self, response):
        commentQuery = response.meta['bodyJson']
        if response.status != 200:
            print('get url error: ' + commentQuery)
            return
        rltJson = json.loads(response.text)
        flag = False
        if 'data' in rltJson:
            if 'shortVideoCommentList' in rltJson['data']:
                flag = True
        if not flag:
            print('parse comment list json error: ')
            print(rltJson)
            return
        pcursor = rltJson['data']['shortVideoCommentList']['pcursor']
        commentList = rltJson['data']['shortVideoCommentList']['commentList']
        for comment in commentList:
            commentUser = TestCommentUserItem()
            if 'authorId' in comment:
                commentUser['authorId'] = comment['authorId']
            else:
                continue
            if 'authorName' in comment:
                commentUser['authorName'] = comment['authorName']
            else:
                commentUser['authorName'] = 'null'
            if 'headurl' in comment:
                commentUser['headurl'] = comment['headurl']
            else:
                commentUser['headurl'] = 'null'
            if 'authorEid' in comment:
                commentUser['authorEid'] = comment['authorEid']
            else:
                continue
            if self.redisClient.sismember(self.redis_id_set_name, commentUser['authorEid']):
                print('item exist')
                continue
            else:
                self.redisClient.sadd(self.redis_id_set_name, commentUser['authorEid'])
                print('get new item: ' + commentUser['authorEid'])
                yield commentUser
        if pcursor != 'no_more':
            commentQuery['variables']['pcursor'] = pcursor
            time.sleep(2)
            yield scrapy.Request(self.url, headers=self.headers, body=json.dumps(commentQuery),
                                 method='POST', callback=self.parseCommentByPhotoId, meta={'bodyJson': commentQuery},
                                 cookies=self.getCookie()
                                 )

    def close(self):
        self.redisClient.close()


import cv2
import pyautogui
import time
import numpy as np
import copy
from scipy import ndimage, signal
from scipy import linalg as la
import matplotlib.image as mpimg

def cannyEdgeDetection(image):
    removed_noise = cv2.GaussianBlur(image, (5, 5), 0)

    edges = np.uint8(removed_noise)  # Otherwise Canny detector gives depth error
    canny_edge_detected = cv2.Canny(edges, 100, 100)

    return canny_edge_detected, removed_noise

def minEigenValueDetection(gray_image, src_color):
    eigenValues = cv2.cornerEigenValsAndVecs(gray_image, 3, 3)

    corners = []

    corners_inserted_image = np.empty(gray_image.shape, dtype=np.float32)
    corners_inserted_image.fill(0)

    for i in range(gray_image.shape[0]):
        for j in range(gray_image.shape[1]):

            lambda_1 = eigenValues[i, j, 0]
            lambda_2 = eigenValues[i, j, 1]

            if lambda_1 > 0 and lambda_2 > 0:
                if lambda_2 > 0.09:
                    corners_inserted_image[i, j] = 255
                    corners.append((j, i))
                    src_color = cv2.circle(src_color, (j, i), 2, (0,255,0))

    return corners_inserted_image, src_color, corners

def sobel_filter(image):

    image = image.astype(np.float)

    kX = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype=np.float)
    kY = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], dtype=np.float)

    gx = signal.convolve2d(image, kX, mode='same', boundary='symm', fillvalue=0)
    gy = signal.convolve2d(image, kY, mode='same', boundary='symm', fillvalue=0)

    g = np.sqrt(gx * gx + gy * gy)
    g *= 255.0 / np.max(g)

    return g

def isFullScreenCheck(image):
    firstPixelValue = image[0, 0]

    if firstPixelValue[0] == 114 and firstPixelValue[1] == 111 and firstPixelValue[2] == 255:
        return True

    return False

def takeScreenshot(fileName, durationOfSleep = 5):                #takes shot of screen after invoking that function in 5 seconds
    time.sleep(durationOfSleep)

    myScreenshot = pyautogui.screenshot()
    myScreenshot.save(fileName)

def giveTheGameArea(image, destinationSize):         # if the game is not full screen call that method for getting game area from screenshot
    horizontalFrameConstant = 0.664
    verticalFrameConstant = 0.664

    image = cv2.resize(image, destinationSize)

    height = int(image.shape[0])
    width = int(image.shape[1])

    tempX = 0
    tempY = 0
    firstSquareIsFindFlag = False

    for i in range(height):
        if firstSquareIsFindFlag:
            break
        for j in range(width):
            pixel = image[i, j]
            if pixel[0] == 114 and pixel[1] == 111 and pixel[2] == 255:
                tempX = j
                tempY = i
                firstSquareIsFindFlag = True
                break

    firstSquarePixCoordinate = (tempX, tempY)
    print(firstSquarePixCoordinate)

    horizontalLength = int(width*horizontalFrameConstant)
    verticalLenght = int(height*horizontalFrameConstant)

    crop_img = image[firstSquarePixCoordinate[1]:firstSquarePixCoordinate[1]+verticalLenght, firstSquarePixCoordinate[0]:firstSquarePixCoordinate[0]+horizontalLength]
    return crop_img

if __name__ == "__main__":

#region PREPARATIONS

    destFileName = "screenshot.png"
    destSize     = (600, 400)

    takeScreenshot(destFileName, 3)

    image        = cv2.imread(destFileName)
    isFullScreen = isFullScreenCheck(image)

    if isFullScreen:
        image = cv2.resize(image, destSize)
    else:
        image = giveTheGameArea(image, destSize)
        image = cv2.resize(image, destSize)

    gray_scale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

#endregion

#region PART1 - SOBEL FILTER

    sobel_filtered_image = sobel_filter(gray_scale_image)

#endregion

#region PART2 - CANNY EDGE DETECTOR
    canny_edge_detected, removed_noise = cannyEdgeDetection(sobel_filtered_image)

#endregion

#region PART3 - MIN EIGENVALUE CORNER DETECTOR
    _, corner_detected, _ = minEigenValueDetection(canny_edge_detected, image)
#endregion

    cv2.imshow("1- Captured Image", image)
    cv2.imshow("2- Gray Scale Image", gray_scale_image)
    cv2.imshow("3- Sobel Filtered Image", sobel_filtered_image)
    cv2.imshow("4- Gaussian Smoothed Image", removed_noise)
    cv2.imshow("5- Canny Detected Image", canny_edge_detected)
    cv2.imshow("6- Corner Detected Image", corner_detected)

    cv2.waitKey()


"""
Support for Control4 devices

For more details about this component, please refer to the documentation at
https://github.com/r3pi/homeassistant-control4
"""

import logging
import voluptuous as vol

from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession

from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)

CONF_URL = 'url'
CONF_PROXY = 'proxy'

CONFIG_SCHEMA = vol.Schema({
    DOMAIN: vol.Schema({
        vol.Required(CONF_URL): cv.url,
        vol.Optional(CONF_PROXY, default=None): cv.url
    })
}, extra=vol.ALLOW_EXTRA)

DATA_CONTROL4 = 'control4'
DATA_CONTROL4_CONFIG = 'control4_config'

REQUIREMENTS = ['python-control4-lite===0.1.15']

async def async_setup(hass, config):
    """Setup Control4 Controller"""

    _LOGGER.debug('async_setup: %s', str(config))

    if DOMAIN not in config:
        return

    _LOGGER.debug('async_setup has config')

    from control4 import Control4

    conf = config[DOMAIN]

    # May need to avoid aviod 'session=async_get_clientsession(hass)' here,
    # because Control4 seems to require force_close -- TBD
    control4 = Control4(url=conf['url'], session=async_get_clientsession(hass), proxy=conf['proxy'])

    hass.data[DATA_CONTROL4_CONFIG] = conf
    hass.data[DATA_CONTROL4] = Control4Device(hass, conf, control4)

    return True

class Control4Device:
    """Structure Control4 functions for hass."""

    def __init__(self, hass, conf, control4):
        """Init Control4 Devices"""
        self._hass = hass
        self._conf = conf
        self._control4 = control4

    @property
    def control4(self):
        return self._control4


from django.db import models
from edc_base.model_managers import HistoricalRecords
from edc_base.model_mixins import BaseUuidModel
from edc_base.model_validators import datetime_not_future
from edc_base.sites import SiteModelMixin
from edc_base.utils import get_utcnow
from edc_constants.choices import YES_NO
from edc_identifier.model_mixins import NonUniqueSubjectIdentifierFieldMixin
from edc_protocol.validators import datetime_not_before_study_start
from edc_search.model_mixins import SearchSlugManager

from .eligibility import PregWomenEligibility
from .model_mixins import SearchSlugModelMixin
from ..identifiers import ScreeningIdentifier

class ScreeningPregWomenManager(SearchSlugManager, models.Manager):

    def get_by_natural_key(self, eligibility_identifier):
        return self.get(screening_identifier=eligibility_identifier)

class ScreeningPregWomen(NonUniqueSubjectIdentifierFieldMixin, SiteModelMixin,
                         SearchSlugModelMixin, BaseUuidModel):
    identifier_cls = ScreeningIdentifier

    screening_identifier = models.CharField(
        verbose_name='Eligibility Identifier',
        max_length=36,
        blank=True,
        null=True,
        unique=True)

    history = HistoricalRecords()

    objects = ScreeningPregWomenManager()

    def __str__(self):
        return f'{self.screening_identifier}'

    def save(self, *args, **kwargs):
        if not self.screening_identifier:
            self.screening_identifier = self.identifier_cls().identifier

        super().save(*args, **kwargs)

    def get_search_slug_fields(self):
        fields = super().get_search_slug_fields()
        fields.append('screening_identifier')
        return fields

    class Meta:
        app_label = 'flourish_caregiver'
        verbose_name = 'Screening for Newly Enrolled Pregnant Women'
        verbose_name_plural = 'Screening for Newly Enrolled Pregnant Women'

class ScreeningPregWomenInline(BaseUuidModel):
    mother_screening = models.ForeignKey(
        ScreeningPregWomen,
        on_delete=models.PROTECT)

    child_subject_identifier = models.CharField(
        verbose_name='Child Subject Identifier',
        max_length=36,
        blank=True,
        null=True,
        unique=True)

    report_datetime = models.DateTimeField(
        verbose_name="Report Date and Time",
        default=get_utcnow,
        validators=[
            datetime_not_before_study_start,
            datetime_not_future],
        help_text='Date and time of assessing eligibility')

    hiv_testing = models.CharField(
        verbose_name=('If HIV status not known, are you willing to undergo HIV'
                      ' testing and counseling?'),
        choices=YES_NO,
        max_length=3)

    breastfeed_intent = models.CharField(
        verbose_name='Do you intend on breastfeeding your infant?',
        choices=YES_NO,
        max_length=3)

    ineligibility = models.TextField(
        verbose_name='Reason not eligible',
        max_length=150,
        null=True,
        editable=False)

    is_eligible = models.BooleanField(
        default=False,
        editable=False)

    is_consented = models.BooleanField(
        default=False,
        editable=False)

    def save(self, *args, **kwargs):
        eligibility_criteria = PregWomenEligibility(
            self.hiv_testing, self.breastfeed_intent)
        self.is_eligible = eligibility_criteria.is_eligible
        self.ineligibility = eligibility_criteria.error_message
        super().save(*args, **kwargs)

    def __str__(self):
        return (f'{self.mother_screening.screening_identifier}'
                f' {self.child_subject_identifier}')

    class Meta:
        app_label = 'flourish_caregiver'
        verbose_name = 'Screening Pregnant Women Inline'
        verbose_name_plural = 'Screening Pregnant Women Inlines'


from kafka import KafkaConsumer, KafkaProducer
from threading import Thread, Event
from six.moves.queue import LifoQueue
import json
import requests
import time
import numpy as np
import os
import subprocess
from qtpy.QtCore import Slot, Qt
from pydm.data_plugins.plugin import PyDMPlugin, PyDMConnection

import logging

# Silence extremely noisy kafka logs
logger = logging.getLogger("kafka")
logger.setLevel("WARNING")

# TODO: Implement new signals...
# Configuration change
# Status change
# Send actions acknowledge
# Send actions disable

ALARMS = {
    "OK": 0,
    "MINOR": 1,
    "MAJOR": 2,
    "INVALID": 3,
    "DISCONNECTED": 4,
    "MINOR_ACK": 5,
    "MAJOR_ACK": 6,
    "UNDEFINED": 7,
    "UNDEFINED_ACK": 8
}

class Connection(PyDMConnection):
    """
    Connection plugin for the kafka alarm datasource.

    """

    def __init__(self, channel, address, protocol=None, parent=None):
        # type: (channel: _, address: str, protocal: str, parent: _)
        super(Connection, self).__init__(channel, address, protocol, parent)
        self.address = address
        self._update_queue = LifoQueue(maxsize=1)
        self._exit_event = Event()
        self._configuration = channel.alarm_configuration

        kafka_url = os.getenv("KAFKA_URL")
        alarm_configuration = channel.alarm_configuration

        try:
            self.consumer = KafkaConsumer(
                alarm_configuration,
                bootstrap_servers=[kafka_url],
                enable_auto_commit=False,
                key_deserializer=lambda x: x.decode('utf-8')
            )

            self.producer = KafkaProducer(bootstrap_servers=[kafka_url], value_serializer=lambda v: json.dumps(v).encode('utf-8'), key_serializer=lambda v: v.encode('utf-8'))

            if self.consumer.bootstrap_connected():

                self.connection_state_signal.emit(True)

                # initialize so can seek to beginning
                while not self.consumer._client.poll(): continue

                self.consumer.seek_to_beginning()

                self._monitor_thread = Thread(
                    target=self._monitor,
                )
                self._monitor_thread.start()

        except Exception as e:
            print(e)

        self.connected = True
        self.add_listener(channel)

    def _monitor(self, delay=0.1):
        for message in self.consumer:
            if self._exit_event.is_set():
                break

            if message.key == "state:/{}".format(self.address):
                value = json.loads(message.value.decode("utf-8"))
                self.send_new_severity(value["severity"])

                if value.get("message"):
                    self.send_new_value(value["message"])

    def close(self):
        self._exit_event.set()
        self._monitor_thread.join()
        self.send_connection_state(False)

    def send_new_value(self, value):
        self.new_value_signal[str].emit(value)

    def send_new_severity(self, value):
        self.new_severity_signal.emit(ALARMS[value])

    def send_connection_state(self, conn):
        self.connection_state_signal.emit(conn)

    def add_listener(self, channel):
        super(Connection, self).add_listener(channel)
        self.send_connection_state(conn=True)
        channel.value_signal[bool].connect(self.put_value, Qt.QueuedConnection)

    def acknowledge(self):
        self.producer.send(self._configuration, key = "command:/{}".format(self.address), value={"user": "pydm", "host": "", "command": "acknowledge"})

    def unacknowledge(self):
        self.producer.send(self._configuration, key = "command:/{}".format(self.address), value={"user": "pydm", "host": "", "command": "unacknowledge"})

    def disable(self):
        pass

    def put_value(self, value):
        if value:
            self.acknowledge()
        else:
            self.unacknowledge()

class AlarmPlugin(PyDMPlugin):
    protocol = "alarm"
    connection_class = Connection


import json
import click
from enum import Enum

from flask import Flask

app = Flask(__name__)

json_resume = 'resume.jjson'

class ApplicationError(Exception):
    code = 'default'
    def __init__(self, message):
        self.message = message

class JsonSyntaxError(ApplicationError):
    code = 'json.syntax.error'

class ResumeFileNotFound(ApplicationError):
    code = 'resume.file.not.found'

class Chapters(Enum):
    PERSONAL = 'personal'
    EXPERIENCE = 'experience'
    EDUCATION = 'education'
    ALL = 'all'

def get_resume_json(chapter: Chapters):
    try:
        with open(json_resume, 'r') as f:
            data = json.loads(f.read())
    except json.decoder.JSONDecodeError as e:
        raise JsonSyntaxError('JOSN resume syntax error: ' + str(e))
    except FileNotFoundError as e:
        raise ResumeFileNotFound(f'File {json_resume} does not exist.')

    rv = data if Chapters(chapter) == Chapters.ALL else data.get(chapter.value)
    return rv or {}

@app.errorhandler(404)
def handle_not_found_error(error):
    response = {
        'message': 'Resource not found',
        'error_code': '404.not.found'
    }
    return response, 404

@app.after_request
def add_status(response):
    content_type = response.headers.get('Content-Type')
    if content_type != 'application/json':
        return response

    data = json.loads(response.data)

    enhanced_data = {
        'status': 'success' if not data.get('error_code') else 'failed',
        'data': data.get('data', {}),
        'error_code': data.get('error_code', None),
        'message': data.get('message', None)
    }
    response.data = json.dumps(enhanced_data)
    if enhanced_data.get('error_code') and response.status.startswith('2'):
        response.status = 500
    return response

def api_error_catcher(func):
    def wrapper(*args, **kwargs):
        try:
            rv = func(*args, **kwargs)
        except ApplicationError as e:
            return {
                'error_code': e.code,
                'message': e.message
            }
        return rv
    return wrapper

@app.route("/personal", methods=['GET'], endpoint='get_personal')
@api_error_catcher
def get_personal():
    return {'data': get_resume_json(Chapters.PERSONAL)}

@app.route("/experience", methods=['GET'], endpoint='get_experience')
@api_error_catcher
def get_experience():
    return {'data': get_resume_json(Chapters.EXPERIENCE)}

@app.route("/education", methods=['GET'], endpoint='get_education')
@api_error_catcher
def get_education():
    return {'data': get_resume_json(Chapters.EDUCATION)}

@app.cli.command()
@click.argument('chapter', type=Chapters)
def get_chapter(chapter: Chapters):
    """Get a chapter of the resume.

    Example: flask get-chapter experience

    CHAPTER is the part of the resume that you want returned: personal,
    education, experience, all.
    """
    click.echo(json.dumps(get_resume_json(chapter), indent=4))

@app.cli.command()
@click.option('--personal', is_flag=True, help='Set flag to get the personal chapter')
@click.option('--experience', is_flag=True, help='Set flag to get the experience chapter')
@click.option('--education', is_flag=True, help='Set flag to get the education chapter')
def compose_resume(personal, experience, education):
    """Add the chapters by setting the flags that interest you.

    Example: flask compose-resume --education --personal
    """

    resume = {}
    mapping = {
        Chapters.PERSONAL: personal,
        Chapters.EXPERIENCE: experience,
        Chapters.EDUCATION: education,
    }
    try:
        for chapter in filter(lambda x: mapping[x], mapping):
            resume[chapter.value] = get_resume_json(Chapters(chapter))
    except ApplicationError as e:
        raise click.ClickException(e.message)

    click.echo(json.dumps(resume, indent=4))


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

# In[25]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.ticker as ticker
import matplotlib.ticker as plticker
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

# In[26]:

ucl_data = pd.read_csv('ucl_data.csv')
ucl_data.head()

# In[27]:

winner = []
for i in range (len(ucl_data['Team'])):
    if ucl_data ['Goalf'][i] > ucl_data['Goala'][i]:
        winner.append(ucl_data['Team'][i])
    else:
        winner.append('other')
ucl_data['winning'] = winner

ucl_data.head()

# In[28]:

Homeg = []
Away = []
for i in range (len(ucl_data['Team'])):
    if ucl_data ['Home'][i] == 1:
        Homeg.append(ucl_data['Team'][i])
        Away.append('Other')
    else:
        Homeg.append('Other')
        Away.append(ucl_data['Team'][i])
ucl_data['Homeg'] = Homeg
ucl_data['Away'] = Away

ucl_data.head()

# In[29]:

matches_data = ucl_data
matches_data

# In[30]:

matches_data = matches_data.reset_index(drop=True)
matches_data.loc[matches_data.winning == matches_data.Homeg,'winning']=2
matches_data.loc[matches_data.winning == matches_data.Away,'winning']=1
matches_data.loc[matches_data.winning == 'other','winning']=0

matches_data

# In[31]:

matches_data = matches_data.drop(['Team'], axis=1)
final = pd.get_dummies(matches_data, prefix=['Homeg', 'Away'], columns=['Homeg', 'Away'])

X = final.drop(['winning'], axis=1)
y = final["winning"]
y = y.astype('int')

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=42)

# In[32]:

final.head()

# In[33]:

result_model = LogisticRegression()
result_model.fit(X_train, y_train)
score = result_model.score(X_train, y_train)
score2 = result_model.score(X_test, y_test)

print("Training set accuracy: ", '%.3f'%(score))
print("Test set accuracy: ", '%.3f'%(score2))

# In[34]:

ranking = pd.read_csv('ranking.csv')
Rounds = pd.read_csv('Rounds.csv')

pred_set = []

# In[35]:

Rounds.insert(1, 'first_position', Rounds['Homeg'].map(ranking.set_index('Team')['position']))
Rounds.insert(2, 'second_position', Rounds['Away'].map(ranking.set_index('Team')['position']))

Rounds.tail()

# In[36]:

for index, row in Rounds.iterrows():
    if row['first_position'] < row['second_position']:
        pred_set.append({'Homeg': row['Homeg'], 'Away': row['Away'], 'winning': None})
    else:
        pred_set.append({'Homeg': row['Away'], 'Away': row['Homeg'], 'winning': None})

pred_set = pd.DataFrame(pred_set)
backup_pred_set = pred_set

pred_set.head()

# In[37]:

pred_set = pd.get_dummies(pred_set, prefix=['Homeg', 'Away'], columns=['Homeg', 'Away'])

missing_cols = set(final.columns) - set(pred_set.columns)
for c in missing_cols:
    pred_set[c] = 0
pred_set = pred_set[final.columns]

pred_set = pred_set.drop(['winning'], axis=1)

pred_set.head()

# In[38]:

predictions = result_model.predict(pred_set)
count = 1
for i in range(Rounds.shape[0]):
    num = 1
    print("Match" +str(count)+ ": " + backup_pred_set.iloc[i, 1] + " VS " + backup_pred_set.iloc[i, 0])
    if predictions[i] == 1:
        print("Winner: " + backup_pred_set.iloc[i, 1])
    elif predictions[i] == 0:
       # print("Penalties")
        if '%.3f'%(result_model.predict_proba(pred_set)[i][2]) > '%.3f'%(result_model.predict_proba(pred_set)[i][0]):
            print( backup_pred_set.iloc[i, 1] + ' winning: ', '%.3f'%(result_model.predict_proba(pred_set)[i][2]))
        else:
            print( "Winner: "+backup_pred_set.iloc[i, 0] )
        num = 0
    elif predictions[i] == 2:
        print("Winner: " + backup_pred_set.iloc[i, 0])
    print("")
    count = count+1

# In[ ]:


import sys
import time
import parse as p
from problems import none, some, many, few, alternate
import graph_visualizer as gv

should_visualize = False
results_file = None
log_file = None

def main(argv):
    global should_visualize
    global results_file
    global log_file

    results_file = open("results.txt", "w")
    results_file.write("Instance name, n, A, F, M, N, S\n")

    log_file = open("log.txt", "w")

    if len(argv) == 0:
        print_instructions()

    for i, arg in enumerate(argv):
        if i+1 > len(argv):
            print(f"Missing argument after flag {argv}")
            sys.exit(2)

        if arg == '--visualize':
            should_visualize = True
            continue
        elif arg == '--help':
            print_instructions()
        elif arg == '--file':
            file = argv[i+1]
            print(f"File: {file}")

            graph, red_keys,s,t, is_directed, has_no_incoming_edges = p.parse_file(file)
            delegate_problem(graph, red_keys,s,t, is_directed,file, has_no_incoming_edges)
            break
        elif arg == '--files':
            files = argv[i+1].split(",")
            print(f"File: {files}")
            for file in files:
                graph, red_keys,s,t, is_directed, has_no_incoming_edges = p.parse_file(file)
                delegate_problem(graph, red_keys,s,t, is_directed,file, has_no_incoming_edges)
            break
        elif arg == '--all':
            print(f"All:\n")
            for file in p.find_all_files():
                print(f"-- {file} --")
                graph, red_keys, s, t, is_directed, has_no_incoming_edges = p.parse_file(file)
                delegate_problem(graph, red_keys, s, t, is_directed,file, has_no_incoming_edges)
            break
        else:
            print("Invalid option")
            sys.exit(2)

    results_file.close()
    log_file.close()

def print_instructions():
    print("\n< ======================= RED SCARE FLAGS ======================= >\n")
    print(f"Provide a flag to run program: python --... --...\n")
    print("E.g.")
    print("\t--visualize (to visualize the graph)")
    print("\t--file { file_name } (provide the file name without the path")
    print("\t--files { file_name_1 } { ... } { file_name_n } (multiple files)")
    print("\t--all (run all implementations on files with n >= 500)\n")
    print("< =============================================================== >\n")
    sys.exit(2)
# Test cases: bht,common-1-5757,common-2-5757,gnm-5000-10000-0,gnm-5000-10000-1,dodecahedron,grid-50-0,grid-50-1,grid-50-2,increase-n-500-1,increase-n-500-2,increase-n-500-3,p3,rusty-1-5757,rusty-2-5757,ski-level20-1,ski-level20-2,ski-level20-3,small-world-50-0,small-world-50-1,wall-n-10000,wall-p-10000,wall-z-10000

def delegate_problem(graph, red_keys, s, t, is_directed, instance_name, has_no_directed_edges):
    if alternate.bfs(graph, s, t) is None:
        print_no_path(instance_name, s, t)
        return

    a_string, A = wrap_check_problem(lambda: alternate.check_alternate_problem(graph, red_keys,s,t), "Alternate", instance_name)
    print(a_string)

    f_string, F = wrap_check_problem(lambda: few.check_few_problem(graph, red_keys, s, t), "Few", instance_name)
    print(f_string)

    m_string, M = wrap_check_problem(lambda: many.check_many_problem(graph, red_keys, s, t,is_directed, has_no_directed_edges), "Many", instance_name)
    print(m_string)

    n_string, N = wrap_check_problem(lambda: none.check_none_problem(graph, red_keys, s, t), "None", instance_name)
    print(n_string)

    s_string, S = wrap_check_problem(lambda: some.check_some_problem(graph, red_keys, s, t, is_directed, has_no_directed_edges), "Some", instance_name)
    print(s_string)

    # build strings
    header_for_print = f"---- Instance: {instance_name}, n: {len(graph)} ----\n"
    print_string = header_for_print + a_string + f_string + m_string + n_string + s_string  + "\n"
    result_string = f"{instance_name}, {len(graph)}, {A}, {F}, {M}, {N}, {S}\n"

    results_file.write(result_string)
    log_file.write(print_string)
    print(print_string)

    if should_visualize:
        visualize(graph, red_keys, s, t, is_directed)

def visualize(graph, red_keys, s, t, is_directed):
    gv.visualize(graph, red_keys, s, t, is_directed)

def wrap_check_problem(function, problem_name, instance_name):
    start_time = time.time()
    problem = function()
    elapsed_time = printTimeTaken(start_time, problem_name[0], instance_name)
    problem_string = f"{problem_name}: {problem} with elapsed time: {elapsed_time}\n"
    return problem_string, problem

def printTimeTaken(start_time, problem, instance_name):
    end_time = (time.time() - start_time)
    if end_time < 1:
        return f"{problem} took: {round(end_time*1000,2)}ms for {instance_name}"
    else:
        return f"{problem} took: {round(end_time,2)}s for {instance_name}"

def print_no_path(instance_name, s, t):
    no_path = f"No path from '{s}' to '{t}'"
    result_string = f"{instance_name}: {no_path}\n"
    results_file.write(result_string)
    print_string = f"---- Instance: {instance_name}, {no_path}\n"
    log_file.write(print_string)
    print(print_string)

if __name__ == "__main__":
    sys.path.insert(1, 'problems/')
    main(sys.argv[1:])


import re

from aiogram import Router
from aiogram.filters import Command, CommandStart, Text
from aiogram.types import Message, CallbackQuery
from sqlalchemy import select, delete
from sqlalchemy.sql.expression import and_
from keyboards.bookmarks import create_bookmarks_keyboard, create_edit_keyboard
from keyboards.books import create_books_keyboard
from keyboards.pagination import create_pagination_keyboard, create_contents_keyboard
from lexicon.lexicon_en import LEXICON
from models.methods import get_user
from models.models import User, Bookmark, Book
from services.book_handing import prepare_book
from filters.filters import DelCallbackFactory, IsDigitCallbackData, BookCallbackFactory

router = Router()

#
@router.message(CommandStart())
async def process_start_command(message: Message, **kwargs):
    user_id = message.from_user.id
    session = kwargs['session_maker']
    user = await get_user(user_id, session)
    if not user:
        user = User(user_id=user_id)
        async with session() as session:
            session.add(user)
            await session.commit()
        await message.answer('You have been successfully registered')
    await message.answer(LEXICON['/start'])

@router.message(Command(commands=['help']))
async def process_help_command(message: Message):
    await message.answer(LEXICON['/help'])

@router.message(Command(commands=['books']))
@router.callback_query(Text(text=['books']))
async def process_books_command(response: Message | CallbackQuery, **kwargs):
    session = kwargs['session_maker']
    async with session() as session:
        books = await session.execute(select(Book))
        books = books.scalars().all()
    if isinstance(response, Message):
        await response.answer(text=LEXICON['/books'], reply_markup=create_books_keyboard(books))
    else:
        await response.message.edit_text(text=LEXICON['/books'], reply_markup=create_books_keyboard(books))

@router.callback_query(lambda cb: cb.data.startswith('book_'))
async def get_necessary_book(callback: CallbackQuery, **kwargs):
    title = re.sub(r'book_', '', callback.data)
    session = kwargs['session_maker']
    async with session() as session:
        book_dict = await prepare_book(title, session)
        total_pages = list(book_dict.keys())[-1]
        user = await session.get(User, callback.from_user.id)
        user.contents_page = 0
        if user.active_book == '' or user.active_book != title:
            user.page = 1
            user.active_book = title
            current_page = user.page
            session.add(user)
            text = book_dict[current_page]
        else:
            current_page = user.page
            text = book_dict[current_page]
        await session.commit()
    await callback.message.edit_text(text=text,
                                     reply_markup=create_pagination_keyboard('backward',
                                                                             f'{current_page} / {total_pages}',
                                                                             'forward'))

@router.callback_query(Text(text=['forward']))
async def process_backward_command(callback: CallbackQuery, **kwargs):
    session = kwargs['session_maker']
    async with session() as session:
        user = await session.get(User, callback.from_user.id)
        book_dict = await prepare_book(user.active_book, session)
        total_pages = list(book_dict.keys())[-1]
        if user.page < int(total_pages):
            user.page += 1
            current_page = user.page
            text = book_dict[current_page]
            session.add(user)
            await session.commit()
            await callback.message.edit_text(text=text,
                                             reply_markup=create_pagination_keyboard('backward',
                                                                                f'{current_page} / {total_pages}',
                                                                                'forward'))
        else:
            await callback.answer(text=LEXICON['max_page'])

@router.callback_query(Text(text=['backward']))
async def process_backward_command(callback: CallbackQuery, **kwargs):
    session = kwargs['session_maker']
    async with session() as session:
        user = await session.get(User, callback.from_user.id)
        book_dict = await prepare_book(user.active_book, session)
        total_pages = list(book_dict.keys())[-1]
        if user.page > 1:
            user.page -= 1
            current_page = user.page
            text = book_dict[current_page]
            session.add(user)
            await session.commit()
            await callback.message.edit_text(text=text,
                                             reply_markup=create_pagination_keyboard('backward',
                                                                                f'{current_page} / {total_pages}',
                                                                                'forward'))
        else:
            await callback.answer(text=LEXICON['min_page'])

@router.callback_query(Text(text=['contents']))
async def show_contents(callback: CallbackQuery, **kwargs):
    session = kwargs['session_maker']
    async with session() as session:
        user = await session.get(User, callback.from_user.id)
        book_dict = await prepare_book(user.active_book, session)
        await callback.message.edit_text(text=LEXICON['contents'],
                                         reply_markup=create_contents_keyboard(user.active_book, user.contents_page,
                                                                               book_dict))

@router.callback_query(IsDigitCallbackData())
async def get_necessary_page(callback: CallbackQuery, **kwargs):
    session = kwargs['session_maker']
    async with session() as session:
        user = await session.get(User, callback.from_user.id)
        user.page = current_page = int(callback.data)
        book_dict = await prepare_book(user.active_book, session)
        session.add(user)
        await session.commit()
        total_pages = list(book_dict.keys())[-1]
        text = book_dict[current_page]
    await callback.message.edit_text(text=text,
                                     reply_markup=create_pagination_keyboard('backward',
                                                                             f'{current_page} / {total_pages}',
                                                                             'forward'))

@router.callback_query(Text(text=['backward_contents']))
async def process_backward_contents(callback: CallbackQuery, **kwargs):
    session = kwargs['session_maker']
    async with session() as session:
        user = await session.get(User, callback.from_user.id)
        book_dict = await prepare_book(user.active_book, session)
        if user.contents_page >= 15:
            user.contents_page -= 15
            session.add(user)
            await callback.message.edit_text(text=LEXICON['contents'],
                                             reply_markup=create_contents_keyboard(user.active_book,
                                                                                user.contents_page, book_dict))
            await session.commit()
        else:
            await callback.answer(text=LEXICON['min_page'])

@router.callback_query(Text(text=['forward_contents']))
async def process_forward_contents(callback: CallbackQuery, **kwargs):
    session = kwargs['session_maker']
    async with session() as session:
        user = await session.get(User, callback.from_user.id)
        book_dict = await prepare_book(user.active_book, session)
        if user.contents_page <= list(book_dict.keys())[-1] - 15:
            user.contents_page += 15
            session.add(user)
            await callback.message.edit_text(text=LEXICON['contents'],
                                             reply_markup=create_contents_keyboard(user.active_book,
                                                                                user.contents_page, book_dict))
            await session.commit()
        else:
            await callback.answer(text=LEXICON['max_page'])

@router.message(Command(commands=['beginning']))
async def process_beginning_command(message: Message, **kwargs):
    session = kwargs['session_maker']
    async with session() as session:
        user = await session.get(User, message.from_user.id)
        if user.active_book:
            book_dict = await prepare_book(user.active_book, session)
            total_pages = list(book_dict.keys())[-1]
            user.page = 1
            session.add(user)
            await message.answer(text=book_dict[1],
                                 reply_markup=create_pagination_keyboard('backward',
                                                                         f'{user.page} / {total_pages}',
                                                                         'forward'))
            await session.commit()
        else:
            await message.answer(LEXICON['None_page'])

@router.message(Command(commands=['continue']))
@router.callback_query(Text(text='continue'))
async def process_continue_command(response: Message | CallbackQuery, **kwargs):
    session = kwargs['session_maker']
    async with session() as session:
        user = await session.get(User, response.from_user.id)
        if user.active_book:
            book_dict = await prepare_book(user.active_book, session)
            total_pages = list(book_dict.keys())[-1]
            if isinstance(response, Message):
                await response.answer(text=book_dict[1],
                                      reply_markup=create_pagination_keyboard('backward',
                                                                              f'{user.page} / {total_pages}',
                                                                              'forward'))
            else:
                await response.message.edit_text(text=book_dict[1],
                                                 reply_markup=create_pagination_keyboard('backward',
                                                                                f'{user.page} / {total_pages}',
                                                                                'forward'))
        else:
            if isinstance(response, Message):
                await response.answer(LEXICON['None_page'])
            else:
                await response.answer()
                await response.message.answer(LEXICON['None_page'])

@router.callback_query(Text(text=['add_to_bookmarks']))
async def add_to_bookmarks(callback: CallbackQuery, **kwargs):
    session = kwargs['session_maker']
    async with session() as session:
        user = await session.get(User, callback.from_user.id)
        bookmark = await session.execute(
            select(Bookmark).filter(
                Bookmark.book == user.active_book,
                Bookmark.page == user.page,
                Bookmark.user_id == user.user_id
            )
        )
        bookmark = bookmark.one_or_none()
        if not bookmark:
            bookmark = Bookmark(user_id=user.user_id,
                                user=user,
                                book=user.active_book,
                                page=user.page)
            session.add(bookmark)
            await session.commit()
            await callback.answer('The bookmark was created successfully')
        else:
            await callback.answer('The bookmark was created earlier')

@router.message(Command(commands=['bookmarks']))
@router.callback_query(Text(text='bookmarks'))
async def get_bookmarks_list(response: Message | CallbackQuery, **kwargs):
    session = kwargs['session_maker']
    async with session() as session:
        bookmarks = await session.execute(select(Bookmark).where(Bookmark.user_id == response.from_user.id))
        bookmarks = bookmarks.scalars().all()
        if bookmarks:
            if isinstance(response, Message):
                await response.answer(text=LEXICON['/bookmarks'],
                                      reply_markup=create_bookmarks_keyboard(bookmarks))
            else:
                await response.message.edit_text(text=LEXICON['/bookmarks'],
                                                 reply_markup=create_bookmarks_keyboard(bookmarks))
        else:
            if isinstance(response, Message):
                await response.answer(LEXICON['no_bookmarks'])
            else:
                await response.message.edit_text(LEXICON['no_bookmarks'])

@router.callback_query(Text(text='edit_bookmarks'))
async def process_edit_press(callback: CallbackQuery, **kwargs):
    session = kwargs['session_maker']
    async with session() as session:
        bookmarks = await session.execute(select(Bookmark).where(Bookmark.user_id == callback.from_user.id))
        bookmarks = bookmarks.scalars().all()
        await callback.message.edit_text(text=LEXICON['edit_bookmarks'],
                                         reply_markup=create_edit_keyboard(bookmarks))

@router.callback_query(DelCallbackFactory.filter())
async def process_del_bookmark(callback: CallbackQuery, callback_data: DelCallbackFactory, **kwargs):
    session = kwargs['session_maker']
    async with session() as session:
        await session.execute(delete(Bookmark).where(
            and_(
                Bookmark.book == callback_data.book,
                Bookmark.page == int(callback_data.page)
            )
        ))
        bookmarks = await session.execute(select(Bookmark).where(Bookmark.user_id == callback.from_user.id))
        bookmarks = bookmarks.scalars().all()
        await callback.message.edit_text(text=LEXICON['edit_bookmarks'],
                                         reply_markup=create_edit_keyboard(bookmarks))
        await session.commit()

@router.callback_query(BookCallbackFactory.filter())
async def view_necessary_bookmark(callback: CallbackQuery, callback_data: DelCallbackFactory, **kwargs):
    session = kwargs['session_maker']
    async with session() as session:
        user = await session.get(User, callback.from_user.id)
        user.page = callback_data.page
        user.active_book = callback_data.book
        book_dict = await prepare_book(user.active_book, session)
        total_pages = list(book_dict.keys())[-1]
        session.add(user)
        await callback.message.edit_text(text=book_dict[callback_data.page],
                                         reply_markup=create_pagination_keyboard('backward',
                                                                                f'{user.page} / {total_pages}',
                                                                                'forward'))
        await session.commit()


import discord
from discord import app_commands
import random
from dis_token import *

token = Enigma

intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)

SEED_ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
SEED_LENGTH = 16

def generate_character_map(seed):
    random.seed(seed)
    character_map = list(SEED_ALPHABET)
    random.shuffle(character_map)
    return character_map

def encrypt(message, seed):
    character_map = generate_character_map(seed)
    encrypted_message = ""

    for char in message:
        if char in SEED_ALPHABET:
            index = SEED_ALPHABET.index(char)
            encrypted_message += character_map[index]
        else:
            encrypted_message += char

    return encrypted_message

def decrypt(message, seed):
    character_map = generate_character_map(seed)
    decrypted_message = ""

    for char in message:
        if char in SEED_ALPHABET:
            index = character_map.index(char)
            decrypted_message += SEED_ALPHABET[index]
        else:
            decrypted_message += char

    return decrypted_message

@tree.command(name="encrypt", description="Encrypts a message with a seed")
async def encrypt_command(interaction, seed: str, message: str):
    result = encrypt(message, seed)
    await interaction.response.send_message(result)

@tree.command(name="decrypt", description="Decrypts a message with a seed")
async def decrypt_command(interaction, seed: str, message: str):
    result = decrypt(message, seed)
    await interaction.response.send_message(result, ephemeral=True)

@client.event
async def on_ready():
    await tree.sync()
    print("Ready!")
    await client.change_presence(activity=discord.Game(name="/help"))

client.run(token)


"""
This module is used to Plot the prediction
to demonstrate model performance. This method
is used within the notebook.
"""

import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt

def plotting_trend(stock, df_one_step, df_multi_step, prediction_start_date):
    """
    Plotting the predicted values.

    Args:
        stock (str): stock name
        df_one_step (pd.DataFrame): next day prediction one day at a time
        df_multi_step (pd.DataFrame): multiple days prediction in future all at the same time, using
                                      predicted values as an input to predict further day value
        prediction_start_date (dt.datetime): prediction start date for multi-step forecasting

    """
    df_one_step = df_one_step.drop(df_one_step.index[0])
    fig, axs = plt.subplots(2, 1, figsize=(20, 23))  # 2 rows, 1 column
    axs[0].plot(df_one_step.index, df_one_step['Predicted'], label="Predicted")
    axs[0].plot(df_one_step.index, df_one_step['Actual'], label="Actual")

    axs[0].set_title(
        f"{stock} Actual trend vs Predicted trend based on updating features on a daily basis to predict next day Price",
        fontsize=20, color="darkblue")
    axs[0].set_xlabel("Date", fontsize=25, color="black")
    axs[0].set_ylabel("Benchmarked Price", fontsize=25, color="black")
    axs[0].tick_params(axis='both', labelsize=20)

    axs[0].set_frame_on(True)
    axs[0].spines['top'].set_visible(True)
    axs[0].spines['right'].set_visible(True)
    axs[0].spines['bottom'].set_visible(True)
    axs[0].spines['left'].set_visible(True)

    axs[0].grid(b=True, color="aqua", alpha=0.5, linestyle='-.')
    axs[0].legend(loc='upper left', prop={'size': 25})

    axs[1].plot(df_multi_step.index, df_multi_step['Predicted Adj Close'], label="Predicted")
    axs[1].plot(df_multi_step.index, df_multi_step['Adj Close'], label="Actual")

    axs[1].set_title(f"{stock} Actual trend comparison with Predicted trend based on Multi-Step forecasting ",
                     fontsize=20, color="darkblue")
    axs[1].set_xlabel("Date", fontsize=25, color="black")
    axs[1].set_ylabel("Stock Price", fontsize=25, color="black")
    axs[1].tick_params(axis='both', labelsize=20)

    axs[1].set_frame_on(True)
    axs[1].spines['top'].set_visible(True)
    axs[1].spines['right'].set_visible(True)
    axs[1].spines['bottom'].set_visible(True)
    axs[1].spines['left'].set_visible(True)

    axs[1].grid(b=True, color="aqua", alpha=0.5, linestyle='-.')
    axs[1].legend(loc='upper left', prop={'size': 25})
    axs[1].axvline(x=prediction_start_date, color="r", linestyle="--", label="Prediction Date")
    text_box_date = prediction_start_date - dt.timedelta(days=8)
    axs[1].text(text_box_date, 0.1, 'Prediction Date', rotation=90, transform=axs[1].get_xaxis_text1_transform(0)[0],
                color="darkblue", fontsize=20)


import collections
import datetime
from collections import Counter

import discord
from discord import app_commands
from discord.ext import commands

STATS_PIPELINE = [{
    "$facet": {
        "getTop10Commands": [{
            "$group": {
                "_id": "$command",
                "count": {
                    "$sum": 1
                }
            }
        }, {
            "$sort": {
                "count": -1
            }
        }, {
            "$limit": 10
        }],
        "totalCommandCount": [{
            "$group": {
                "_id": None,
                "count": {
                    "$sum": 1
                }
            }
        }]
    }
}]

class Statistics(commands.GroupCog, name="statistics"):
    """Bot statistics"""

    def __init__(self, bot):
        self.bot = bot
        self.counter = Counter()
        self.db = self.bot.database.db.statistics

    @app_commands.checks.cooldown(1, 10, key=lambda i: i.user.id)
    @app_commands.command(name="user")
    @app_commands.describe(reveal="Post the response as a publicly "
                           "visible message. Defaults to False")
    async def statistics_user(self,
                              interaction: discord.Interaction,
                              reveal: bool = False):
        """Statistics of the user"""
        await interaction.response.defer(ephemeral=not reveal)
        data = discord.Embed(description="Command usage "
                             f"statistics of {interaction.user.mention}",
                             color=self.bot.color)
        match = [{"$match": {"author": interaction.user.id}}]
        facets = await self.db.commands.aggregate(match +
                                                  STATS_PIPELINE).to_list(None)
        facets = facets[0]
        data = await self.generate_embed(interaction, data, facets, rank=False)
        await interaction.followup.send(embed=data)

    @app_commands.command(name="server")
    @app_commands.checks.cooldown(1, 60, key=lambda i: i.guild_id)
    @app_commands.guild_only()
    async def statistics_guild(self, interaction: discord.Interaction):
        """Statistics of this server"""
        await interaction.response.defer()
        data = discord.Embed(description="Command usage "
                             f"statistics of {interaction.guild.name}",
                             color=self.bot.color)
        match = [{"$match": {"guild": interaction.guild.id}}]
        facets = await self.db.commands.aggregate(match +
                                                  STATS_PIPELINE).to_list(None)
        facets = facets[0]
        data = await self.generate_embed(interaction, data, facets, rank=False)
        await interaction.followup.send(embed=data)

    @app_commands.checks.cooldown(1, 3600, key=lambda i: i.user.id)
    @app_commands.command(name="global")
    async def statistics_total(self, interaction: discord.Interaction):
        """Total stats of the bot's commands

        Only available to server owner"""
        await interaction.response.defer()
        data = discord.Embed(description="Global command usage statistics.",
                             color=self.bot.color)
        facets = await self.db.commands.aggregate(STATS_PIPELINE).to_list(None)
        facets = facets[0]
        data = await self.generate_embed(interaction, data, facets, rank=False)
        await interaction.followup.send(embed=data)

    async def get_commands_stats(self, cursor, search):
        """Returns ordered dict of commands from cursor
        and search string in DB"""
        commands = {}
        async for stat in cursor:
            if stat[search] in commands:
                commands[stat[search]] += 1
            else:
                commands[stat[search]] = 1
        ordered_commands = collections.OrderedDict(
            sorted(commands.items(), key=lambda x: x[1], reverse=True))
        return ordered_commands

    def calc_percentage(self, ordered_commands, total):
        """Generates ordered dict of percentages of
        used commands from ordered_commands"""
        percentages = {}
        for doc in ordered_commands:
            percentages[doc["_id"]] = round(100 / total * doc["count"])
        ordered_percentages = collections.OrderedDict(
            sorted(percentages.items(), key=lambda x: x[1], reverse=True))
        return ordered_percentages

    async def generate_embed(self, ctx, embed, facets, *, rank=True):
        # Get data
        total_amount = facets["totalCommandCount"][0]["count"]
        ordered_commands = facets["getTop10Commands"]
        percentages = self.calc_percentage(ordered_commands, total_amount)
        embed.add_field(name="Total commands used",
                        value=str(total_amount),
                        inline=False)
        output = self.generate_commands(ordered_commands)
        embed.add_field(name="Most used commands", value=output, inline=False)
        output = self.generate_diagram(percentages)
        embed.add_field(name="Diagram", value=output, inline=False)

        return embed

    def generate_commands(self, ordered_commands):
        """Returns the 10 most used commands from ordered_commands"""
        seq = [k["_id"] for k in ordered_commands]
        longest = len(max(seq, key=len))
        if longest < 7:
            longest = 7
        output = [
            "COMMAND{}COUNT".format(" " * (longest - 4)),
            "--------{}|-----".format("-" * (longest - 6))
        ]
        counter = 0
        for doc in ordered_commands:
            if counter > 9:
                break
            output.append("{} {} | {}".format(
                doc["_id"].upper(), " " * (longest - len(doc["_id"])),
                doc["count"]))
            counter += 1
        output.append("--------{}------".format(
            "-" * (longest - len("command") + 2)))
        output = "```ml\n{}```".format("\n".join(output))
        return output

    def generate_diagram(self, percentages):
        """Generates string of ASCII bar out of ordered_dict of percentages"""
        counter = 0
        output = "```\n"
        for k, v in percentages.items():
            if counter < 5:
                bar_count = round(v / 5)
                for emoji in range(bar_count):
                    output += "▓"
                tab_count = 20 - bar_count
                for tab in range(tab_count):
                    output += "░"
                output += " {0}% used {1}\n".format(v, k)
                counter += 1
        return output + "```"

    async def generate_ranking(self, ctx, ranking):
        """Returns the first 5 users that used the most commands"""
        counter = 0
        output = ""
        for k, v in ranking.items():
            if counter < 5:
                counter += 1
                user = await self.bot.fetch_user(k)
                if user is None:
                    user = "Unknown"
                output += "{}. | {} | used {} commands\n".format(
                    counter, user, v)
        return output

    def get_bot_uptime(self, *, brief=False):
        now = datetime.datetime.utcnow()
        delta = now - self.bot.uptime
        hours, remainder = divmod(int(delta.total_seconds()), 3600)
        minutes, seconds = divmod(remainder, 60)
        days, hours = divmod(hours, 24)

        if not brief:
            if days:
                fmt = '{d} days, {h} hours, {m} minutes, and {s} seconds'
            else:
                fmt = '{h} hours, {m} minutes, and {s} seconds'
        else:
            fmt = '{h}h {m}m {s}s'
            if days:
                fmt = '{d}d ' + fmt

        return fmt.format(d=days, h=hours, m=minutes, s=seconds)

    @commands.Cog.listener()
    async def on_message(self, message):
        self.counter["messages"] += 1

    @commands.Cog.listener()
    async def on_interaction(self, interaction: discord.Interaction):
        if not interaction.command:
            return
        self.counter["invoked_commands"] += 1
        guild = interaction.guild.id if interaction.guild else None
        channel = interaction.channel.id if interaction.channel else None
        doc = {
            "author": interaction.user.id,
            "guild": guild,
            "channel": channel,
            "command": interaction.command.qualified_name,
            "timestamp": interaction.created_at
        }
        await self.db.commands.insert_one(doc)

async def setup(bot):
    await bot.add_cog(Statistics(bot))


import logging
from collections import namedtuple
from unittest.mock import ANY, Mock, PropertyMock, patch

from django.test import TestCase
from django.urls import reverse

from api.models import Status, Tenant

class StatusModelTest(TestCase):
    """Tests against the status functions."""

    @classmethod
    def setUpClass(cls):
        """Test Class setup."""
        # remove filters on logging
        logging.disable(logging.NOTSET)
        cls.status_info = Status()

    @classmethod
    def tearDownClass(cls):
        """Test Class teardown."""
        # restore filters on logging
        logging.disable(logging.CRITICAL)

    def setUp(self):
        """Create test case setup."""
        super().setUp()
        t, created = Tenant.objects.get_or_create(tenant_name="public")
        if created:
            t.ready = True
            t.save()

    @patch("os.environ")
    def test_commit_with_env(self, mock_os):
        """Test the commit method via environment."""
        expected = "buildnum"
        mock_os.get.return_value = expected
        result = self.status_info.commit
        self.assertEqual(result, expected)

    @patch("subprocess.run")
    @patch("api.status.model.os.environ")
    def test_commit_with_subprocess(self, mock_os, mock_subprocess):
        """Test the commit method via subprocess."""
        expected = "buildnum"
        run = Mock()
        run.stdout = b"buildnum"
        mock_subprocess.return_value = run
        mock_os.get.return_value = None
        result = self.status_info.commit
        self.assertEqual(result, expected)

class StatusViewTest(TestCase):
    """Tests the status view."""

    def test_status_endpoint(self):
        """Test the status endpoint."""
        url = reverse("server-status")
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)


import os
import cv2
import numpy as np

from datetime import datetime
from uuid import uuid4

from PIL import Image
from telegram import Bot

from utils.landmarks_detector import LandmarksDetector
from utils.face_alignment import image_align

from pix2pix import Pix2pixModels

def handle_file_upload(update, context):
    print(f'handle new image {datetime.now().isoformat()}')

    chat_id = update.message.chat.id
    bot: Bot = context.bot

    file_path = f"pictures/{uuid4()}.jpeg"
    with open(file_path, "wb") as fd:
        context.bot.get_file(update.message.photo[-1]).download(out=fd)

    aligned_image_files_path = process_input_image(file_path)

    if not aligned_image_files_path:
        bot.send_message(chat_id=chat_id, text="I'm sorry, but I did not find any faces on the image")

    for aligned_image_file in aligned_image_files_path:

        for output_image in Pix2pixModels.inference(aligned_image_file):
            opencv_output_image = cv2.cvtColor(np.array(output_image), cv2.COLOR_RGB2BGR)
            resized_image = cv2.resize(opencv_output_image, (512, 512))

            output_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB)
            output_image = Image.fromarray(output_image)

            webp_file_path = f"pictures/{uuid4()}.webp"
            output_image.save(webp_file_path, "webp")

            with open(webp_file_path, "rb") as fd:
                bot.send_sticker(chat_id, fd)

            os.remove(webp_file_path) # clear

        os.remove(aligned_image_file)

def process_input_image(input_file_path):
    landmarks_detector = LandmarksDetector()
    input_image = Image.open(input_file_path)

    output_files = []
    for face_landmarks in landmarks_detector.get_landmarks(input_file_path):
        output_image = image_align(input_image, face_landmarks, 256)
        output_file_path = f'pictures/{uuid4()}.jpeg'
        output_image.save(output_file_path, 'jpeg')
        output_files.append(output_file_path)

    return output_files


import pandas as pd
import numpy as np

from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler

from train import train_model

def k_fold_train_model(X: pd.DataFrame, y: pd.DataFrame, k: int, model: str, verbose: bool = True):
    all_train_acc = []
    all_test_acc = []

    # Choose only numeric columns
    X = X.select_dtypes(include=['number'])

    # Split to k folds
    kf = KFold(n_splits=k, shuffle=True, random_state=42)
    for train_index, test_index in kf.split(X):
        X_train, X_test = X.iloc[train_index], X.iloc[test_index]
        y_train, y_test = y.iloc[train_index], y.iloc[test_index]

        scaler = StandardScaler()
        scaler.fit(X_train)
        X_train = scaler.transform(X_train)
        X_test = scaler.transform(X_test)

        # Replace NaN with 0
        X_train[np.isnan(X_train)] = 0
        X_test[np.isnan(X_test)] = 0

        X_train = pd.DataFrame(X_train, columns=X.columns)
        X_test = pd.DataFrame(X_test, columns=X.columns)

        train_acc, test_acc, _ = train_model(X_train, X_test, y_train, y_test, model, verbose=verbose)

        all_train_acc.append(train_acc)
        all_test_acc.append(test_acc)

    if verbose:
        print(f'Model: {model}, Average training accuracy: {np.mean(all_train_acc)}, Average test accuracy: {np.mean(all_test_acc)}')

        print("Entire training set training:")
    scaler = StandardScaler()
    scaler.fit(X)
    all = scaler.transform(X)
    all[np.isnan(all)] = 0

    X = pd.DataFrame(all, columns=X.columns)
    _, _, clf = train_model(X, X, y, y, model, verbose=verbose)

    return np.mean(all_train_acc), np.mean(all_test_acc), clf