"""
Пожалуйста, приступайте к этой задаче после того, как вы сделали и получили ревью ко всем остальным задачам
в этом репозитории. Она значительно сложнее.

Есть набор сообщений из чата в следующем формате:

```
messages = [
    {
        "id": "efadb781-9b04-4aad-9afe-e79faef8cffb",
        "sent_at": datetime.datetime(2022, 10, 11, 23, 11, 11, 721),
        "sent_by": 46,  # id пользователя-отправителя
        "reply_for": "7b22ae19-6c58-443e-b138-e22784878581",  # id сообщение, на которое это сообщение является ответом (может быть None)
        "seen_by": [26, 91, 71], # идентификаторы пользователей, которые видели это сообщение
        "text": "А когда ревью будет?",
    }
]
```

Так же есть функция `generate_chat_history`, которая вернёт список из большого количества таких сообщений.
Установите библиотеку lorem, чтобы она работала.

Нужно:
1. Вывести айди пользователя, который написал больше всех сообщений.
2. Вывести айди пользователя, на сообщения которого больше всего отвечали.
3. Вывести айди пользователей, сообщения которых видело больше всего уникальных пользователей.
4. Определить, когда в чате больше всего сообщений: утром (до 12 часов), днём (12-18 часов) или вечером (после 18 часов).
5. Вывести идентификаторы сообщений, который стали началом для самых длинных тредов (цепочек ответов).

Весь код стоит разбить на логические части с помощью функций.
"""
import random
import uuid
import datetime
from pprint import pprint
from collections import defaultdict

import lorem

def generate_chat_history():
    messages_amount = random.randint(200, 1000)
    users_ids = list(
        {random.randint(1, 10000) for _ in range(random.randint(5, 20))}
    )
    sent_at = datetime.datetime.now() - datetime.timedelta(days=100)
    messages = []
    for _ in range(messages_amount):
        sent_at += datetime.timedelta(minutes=random.randint(0, 240))
        messages.append({
            "id": uuid.uuid4(),
            "sent_at": sent_at,
            "sent_by": random.choice(users_ids),
            "reply_for": random.choice(
                [
                    None,
                    (
                        random.choice([m["id"] for m in messages])
                        if messages else None
                    ),
                ],
            ),
            "seen_by": random.sample(users_ids,
                                     random.randint(1, len(users_ids))),
            "text": lorem.sentence(),
        })
    return messages

def find_id_user_most_messages(messages: list) -> int:
    messages_per_user = defaultdict(int)
    for message in messages:
        messages_per_user[message['sent_by']] += 1

    max_messages = 0
    user_ids_with_max_messages = 0
    for key, value in messages_per_user.items():
        if value > max_messages:
            max_messages = value
            user_ids_with_max_messages = [key]
        elif value == max_messages:
            user_ids_with_max_messages.append(key)

    return user_ids_with_max_messages

def find_id_user_most_messages_replies(messages: list) -> int:
    replies_per_message = defaultdict(int)
    for message in messages:
        if message['reply_for'] is None:
            continue
        replies_per_message[message['reply_for']] += 1

    most_replied_messages = set()
    most_replied_count = 0
    for key, value in replies_per_message.items():
        if value > most_replied_count:
            most_replied_count = value
            most_replied_messages = {key}
        elif value == most_replied_count:
            most_replied_messages.add(key)

    most_replied_users = []
    for message in messages:
        if message['id'] in most_replied_messages:
            most_replied_users.append(message['sent_by'])

    return most_replied_users

def find_id_user_most_see_messages(messages: list) -> list:
    users = defaultdict(set)

    for message in messages:
        if users.get(message['sent_by']) is None:
            users[message['sent_by']] = set(message['seen_by'])
        else:
            users[message['sent_by']] = users[message['sent_by']].union(message['seen_by'])

    most_see_message_user = []
    max_len_seen_by = 0
    for key, value in users.items():
        if len(value) > max_len_seen_by:
            most_see_message_user = []
            max_len_seen_by = len(value)
            most_see_message_user.append(key)
        elif len(value) == max_len_seen_by:
            most_see_message_user.append(key)

    return most_see_message_user

def when_most_messages(messages: list) -> str:
    count_morning = 0
    count_day = 0
    count_evening = 0

    for message in messages:
        time = message['sent_at']
        time = time.time()
        if datetime.time(0, 0, 0) < time < datetime.time(12, 0, 0):
            count_morning += 1
        elif datetime.time(12, 0, 0) <= time <= datetime.time(18, 0, 0):
            count_day += 1
        else:
            count_evening += 1

    if count_morning > count_day and count_morning > count_evening:
        return 'Утром'
    elif count_day > count_evening:
        return 'Днем'
    else:
        return 'Вечером'

# вспомогательная функция для нахождения id сообщения отправителя
def find_id_message(messages: list, id_message) -> str:
    for message in messages:
        if message['id'] == id_message:
            return message['reply_for']

def find_id_messages_which_have_most_threads(messages: list) -> list:
    dict_result = defaultdict(int)
    for message in messages:
        if message['reply_for'] is None:
            continue
        else:
            id_message = message['reply_for']
            count_threads = 0

            while True:
                count_threads += 1
                id_message_find = find_id_message(messages, id_message)
                if id_message_find is None:
                    break
                else:
                    id_message = id_message_find
            dict_result[id_message] = count_threads

    id_message = []
    max_value = 0
    for key, value in dict_result.items():
        if value > max_value:
            max_value = value
            id_message.clear()
            id_message.append(key)
        elif value == max_value:
            id_message.append(key)
    return id_message

if __name__ == "__main__":
    # pprint(generate_chat_history())
    print(find_id_user_most_messages(generate_chat_history()))
    print(find_id_user_most_messages_replies(generate_chat_history()))
    print(find_id_user_most_see_messages(generate_chat_history()))
    print(when_most_messages(generate_chat_history()))
    print(find_id_messages_which_have_most_threads(generate_chat_history()))


from rest_framework import serializers

from .models import (
    Product,
    ProductImage,
    Size,
    Category
)

class CategoryListSerializer(serializers.HyperlinkedModelSerializer):
    url = serializers.HyperlinkedIdentityField(
        view_name='products:category-detail-view',
        lookup_field='slug'
    )

    class Meta:
        model = Category
        fields = (
            'id',
            'title',
            'url',
        )

class CategoryDetailSerializer(CategoryListSerializer):
    products = serializers.SerializerMethodField()

    class Meta:
        model = Category
        fields = (
            'id',
            'title',
            'products',
        )

    def get_products(self, obj):
        # The source of the SSL context override
        return ProductListSerializer(obj.product_set.all(), many=True, context=self.context).data

class ProductListSerializer(serializers.HyperlinkedModelSerializer):
    url = serializers.HyperlinkedIdentityField(
        view_name='products:product-detail-view', lookup_field='slug')

    class Meta:
        model = Product
        fields = (
            'id',
            'slug',
            'title',
            'price',
            'image',
            'url',
        )

class ProductDetailSerializer(ProductListSerializer):
    sizes = serializers.SerializerMethodField()
    productImages = serializers.SerializerMethodField()
    categories = CategoryListSerializer(many=True)

    class Meta:
        model = Product
        fields = (
            'id',
            'title',
            'price',
            'image',
            'slug',
            'categories',
            'sizes',
            'description',
            'productImages',
        )

    def get_sizes(self, obj):
        return SizeSerializer(obj.size_set.all(), many=True).data

    def get_productImages(self, obj):
        return ProductImageSerializer(
            obj.productimage_set.all(),
            many=True
        ).data

class SizeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Size
        fields = (
            'id',
            'size',
            'slug',
            'stock',
        )

class ProductImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProductImage
        fields = (
            'id',
            'image',
        )


import random
from itertools import zip_longest
from typing import List

from config import MuZeroConfig
from game.game import AbstractGame
import _pickle as cPickle
import os
import numpy as  np

class ReplayBuffer(object):

    def __init__(self, config: MuZeroConfig, fighter):
        self.window_size = config.window_size
        self.batch_size = config.batch_size
        self.buffer = []
        self.loaded_games = []
        self.current_games = []
        self.memory_path = config.memory_path
        self.fighter = fighter

    def save_game(self, game):
        if sum([len(i.root_values) for i in self.buffer])  > self.window_size:
            self.buffer.pop(0)
        if game.player1_historic_network == True:
            game.game_player1_priority = 0
            game.player1_priorities = list(np.full(len(game.root_values), 0))
        else:
            game.game_player1_priority = 1e3*len(game.root_values)
            game.player1_priorities = list(np.full(len(game.root_values), 1e3))
            player1_zero_move_idx = [i for i, j in enumerate(game.child_visits) if j[0][0] == 1.]
            for idx in player1_zero_move_idx:
                game.player1_priorities[idx] = 0
        if game.player2_historic_network == True:
            game.game_player2_priority = 0
            game.player2_priorities = list(np.full(len(game.root_values), 0))
        else:
            game.game_player2_priority = 1e3*len(game.root_values)
            game.player2_priorities = list(np.full(len(game.root_values), 1e3))
            player2_zero_move_idx = [i for i, j in enumerate(game.child_visits) if j[1][0] == 1.]
            for idx in player2_zero_move_idx:
                game.player2_priorities[idx] = 0
        self.buffer.append(game)

    def update_buffer(self):
        new_files = [f for f in os.listdir(self.memory_path) if f not in self.loaded_games]
        new_files = [f for f in new_files if (f.split('_')[-1][:-4] == self.fighter) | (f.split('_')[-2] == self.fighter)]
        new_files.sort(key = lambda x: int(x.split('_')[1]))
        if len(new_files) > self.window_size // 1100:
            self.loaded_games = self.loaded_games + new_files[:-self.window_size // 1100]
            new_files = new_files[-self.window_size // 1100:]
        if len(new_files) != 0:
            for new_file in new_files:
                with open(os.path.join(self.memory_path,new_file), 'rb') as game_file:
                    game = cPickle.load(game_file)
                self.save_game(game)
                self.loaded_games.append(new_file)
                if sum([len(i.root_values) for i in self.buffer]) > self.window_size:
                    self.current_games.pop(0)
                self.current_games.append(new_file)

    def sample_batch(self, num_unroll_steps: int, unroll_step_size : int, td_steps: int, fighter):
        # Generate some sample of data to train on
        games = self.sample_games(fighter)
        game_pos = [(g, self.sample_position(self.buffer[g], fighter), 'player1' if self.buffer[g].player1 == fighter else 'player2') for g in games]
        game_data = [(self.buffer[g].make_image(i, p), [action.index for action in [j[int(p[-1]) - 1] for j in self.buffer[g].history[i:i + num_unroll_steps]]],
                      self.buffer[g].make_target(i, num_unroll_steps, unroll_step_size, td_steps, p))
                     for (g, i, p) in game_pos]
        sample_weights = [self.buffer[g].player1_priorities[i] if p == 'player1' else self.buffer[g].player2_priorities[i] for (g, i, p) in game_pos]
        game_weights = [self.buffer[g].game_player1_priority if p == 'player1' else self.buffer[g].game_player2_priority for (g, i, p) in game_pos]
        weight_batch = 1 / (np.array(sample_weights) * np.array(game_weights))
        weight_batch = weight_batch / np.max(weight_batch)
        # Pre-process the batch
        image_batch, actions_time_batch, targets_batch = zip(*game_data)
        targets_init_batch, *targets_time_batch = zip(*targets_batch)
        actions_time_batch = list(zip_longest(*actions_time_batch, fillvalue=0))

        # Building batch of valid actions and a dynamic mask for hidden representations during BPTT

        batch = image_batch, targets_init_batch, targets_time_batch, actions_time_batch
        return batch, game_pos, weight_batch**0.4

    def sample_games(self, fighter) -> List[AbstractGame]:
        # Sample game from buffer either uniformly or according to some priority.
        game_probs = np.array([game.game_player1_priority if game.player1 == fighter else game.game_player2_priority for game in self.buffer])
        game_probs /= np.sum(game_probs)
        return np.random.choice(len(self.buffer), size=self.batch_size, p = game_probs)

    def sample_position(self, game: AbstractGame, fighter) -> int:
        # Sample position from game either uniformly or according to some priority.
        if game.player1 == fighter:
            pos_probs = game.player1_priorities / sum(game.player1_priorities)
        if game.player2 == fighter:
            pos_probs = game.player2_priorities / sum(game.player2_priorities)
        return np.random.choice(len(pos_probs), p=pos_probs)

    def sample_position_value_bias(self, game: AbstractGame) -> int:
        # Sample position from game either uniformly or according to some priority.
        history = [i.index for i in game.history]
        counts = np.bincount(history)
        common = np.argmax(counts)
        above_avg = [i[0] for i in np.argwhere(history==common)]
        below_avg = [i[0] for i in np.argwhere(history!=common)]
        if random.randint(0,5) != 5:
            return np.random.choice(below_avg)
        else:
            return np.random.choice(above_avg)
    def update_priorities(self, priorities, idx_info, fighter):
        for i in range(len(idx_info)):
            game_id, game_pos, _ = idx_info[i]
            priority = priorities[i,:]
            start_idx = game_pos

            if self.buffer[game_id].player1 == fighter:
                end_idx = min(game_pos+len(priority), len(self.buffer[game_id].player1_priorities))
                self.buffer[game_id].player1_priorities[start_idx:end_idx] = priority[:end_idx-start_idx]
                self.buffer[game_id].game_player1_priority = np.mean(self.buffer[game_id].player1_priorities) * len(self.buffer[game_id].root_values)
            if self.buffer[game_id].player2 == fighter:
                end_idx = min(game_pos+len(priority), len(self.buffer[game_id].player2_priorities))
                self.buffer[game_id].player2_priorities[start_idx:end_idx] = priority[:end_idx-start_idx]
                self.buffer[game_id].game_player2_priority = np.mean(self.buffer[game_id].player2_priorities) * len(self.buffer[game_id].root_values)


import numpy as np
import run as  r
from sklearn.gaussian_process.kernels import ABCMeta, Matern, ConstantKernel, Exponentiation, ExpSineSquared, Hyperparameter, KernelOperator, \
    NormalizedKernelMixin, PairwiseKernel, RationalQuadratic, StationaryKernelMixin, RBF, CompoundKernel, DotProduct, Product, GenericKernelMixin, WhiteKernel, \
    Kernel, Sum

'''
[id]
112

[name]
GaussianProcessRegressor

[input]
x_train	训练集	训练集标签数据集	二维数组	必须	定数
y_train	测试集	测试集数据集	二维数组	必须	定数
x_test	训练集标签	训练集标签标签	一维数组	必须	定数
y_test	测试集标签	测试集标签	一维数组	必须	定数
kernel	内核	默认为None,指定GP协方差函数的内核。如果传递了None，则默认使用内核'1.0 * RBF(1.0)'。请注意，内核的超参数在拟合过程中已优化,可选字符串	字符串	不必须	定数
alpha	alpha	默认为1e-10,拟合期间将值添加到内核矩阵的对角线。较大的值对应于观测结果中增加的噪声水平。通过确保计算值形成正定矩阵，这也可以防止拟合期间出现潜在的数值问题。如果传递了数组，则该数组必须具有与用于拟合的数据相同的条目数，并且用作与数据点有关的噪声水平。请注意，这等效于添加c = alpha的WhiteKernel。直接允许将噪声级别指定为参数主要是为了方便和与Ridge保持一致,可选数组,浮点数	字符串	不必须	定数
optimizer	optimizer	默认为'fmin_l_bfgs_b',可以是内部支持的用于优化kernel 's parameters, specified by a string, or an externally defined optimizer passed as a callable. Per default, the ' L-BFGS-B ' algorithm from scipy.optimize.minimize is used. If None is passed, the kernel' s参数的优化器之一。可用的内部优化器是：: 'fmin_l_bfgs_b,可选'fmin_l_bfgs_b'	字符串	不必须	定数
n_restarts_optimizer	重新启动次数	默认为0,用于查找内核初始参数的优化程序的重新启动次数，以及从允许的theta值空间中随机抽取的theta采样对数均匀性中剩余的参数(如果有的话)。如果大于0，则所有边界必须是有限的。请注意，n_restarts_optimizer == 0表示执行了一次运行,可选整数	整数	不必须	定数
normalize_y	normalize_y	默认为False,无论目标值y是否被归一化，目标值的均值和方差分别设置为等于0和1。对于使用零均值，单位方差先验的情况，建议使用此方法。注意，在此实现中，在报告GP预测之前，将规范化反转,可选布尔值	布尔值	不必须	定数
copy_X_train	copy_X_train	默认为True,如果为True，则训练数据的永久副本存储在对象中。否则，仅存储对训练数据的引用，如果对数据进行外部修改，则可能导致预测更改,可选布尔值	布尔值	不必须	定数
random_state	随机种子	默认为None,确定用于初始化中心的随机数生成。在多个函数调用之间传递int以获得可重复的结果,可选整数	整数	不必须	定数

[output]
train_predict	预测	训练集预测结果	一维数组(数值)
test_predict	预测	测试集预测结果	一维数组(数值)
train_score	正确率	训练集预测结果的正确率	数字
test_score	正确率	测试集预测结果的正确率	数字
X_train_	X_train_	训练数据的特征向量或其他表示形式(预测也需要)	二维数组
y_train_	y_train_	训练数据中的目标值(预测也需要)	一维数组
L_	L_	'X_train_'中内核的下三角Cholesky分解	二维数组
kernel_	kernel_	用于预测的内核。内核的结构与作为参数传递的内核相同，但具有优化的超参数	字符串
alpha_	alpha	核空间中训练数据点的对偶系数	一维数组
log_marginal_likelihood_value_	对数边际可能性	'self.kernel_.theta'的对数边际可能性	浮点数

[outline]

[describe]
高斯过程回归(GPR)。
该实现基于Rasmussen和Williams提出的高斯机器学习过程算法(GPML)的算法2.1。
除了标准的scikit-learn估计器API外，GaussianProcessRegressor：*允许进行预测而无需事先拟合(基于GP优先级)*提供其他方法sample_y(X)，该方法评估在给定输入下从GPR(优先级或后验)中提取的样本*公开了一个方法log_marginal_likelihood(theta)，该方法可在外部用于其他选择超参数的方式，例如通过马尔可夫链蒙特卡洛。

'''

def main(x_train, y_train, x_test, y_test,
         kernel=None, alpha=1e-10, optimizer="fmin_l_bfgs_b", n_restarts_optimizer=0, normalize_y=False, copy_X_train=True, random_state=None
         ):
    if type(x_train) is str:
        x_train = eval(x_train)
    if type(y_train) is str:
        y_train = eval(y_train)
    if type(x_test) is str:
        x_test = eval(x_test)
    if type(y_test) is str:
        y_test = eval(y_test)
    if type(kernel) is str:
        kernel = eval(kernel)
    if type(alpha) is str:
        alpha = eval(alpha)
    if type(n_restarts_optimizer) is str:
        n_restarts_optimizer = eval(n_restarts_optimizer)
    if type(normalize_y) is str:
        normalize_y = eval(normalize_y)
    if type(copy_X_train) is str:
        copy_X_train = eval(copy_X_train)
    if type(random_state) is str:
        random_state = eval(random_state)

    return r.run(x_train=x_train, y_train=y_train, x_test=x_test, y_test=y_test, kernel=kernel,
                 alpha=alpha,
                 optimizer=optimizer,
                 n_restarts_optimizer=n_restarts_optimizer,
                 normalize_y=normalize_y,
                 copy_X_train=copy_X_train,
                 random_state=random_state)

if __name__ == '__main__':
    import numpy as np
    import json

    array = np.loadtxt('D:\\123_2.csv', delimiter=',')
    array = array[0:20, :]

    y = array[:, -1].tolist()
    x = np.delete(array, -1, axis=1).tolist()
    array = array.tolist()
    back = main(x, y, x, y)

    print(back)
    for i in back:
        print(i + ":" + str(back[i]))

    json.dumps(back)


import torch
import math
from torch import nn

import torch.nn.functional as F

from transformers.activations import get_activation

from .utils import init_weights

def _mask(logits, mask):
    return mask * logits - 1e3 * (1 - mask)

# VarMisuse -----------------------------------------------------------------

class _LocRepairPointerHead(nn.Module):

    def __init__(self, config):
        super().__init__()

        self.dense = nn.Linear(config.hidden_size, config.hidden_size)
        self.prediction = nn.Linear(config.hidden_size, 2)

        self.apply(init_weights)

    def forward(self, input_states):
        hidden = self.dense(input_states)
        hidden = get_activation("gelu")(hidden)
        logits = self.prediction(hidden)
        logits = logits.transpose(2, 1)
        return logits

class VarMisuseBaseModel(nn.Module):

    def __init__(self, config, encoder):
        super().__init__()

        self.config = config
        self.encoder = encoder

    @torch.no_grad()
    def score(self, logits, labels):
        probs = nn.Softmax(dim = 2)(logits)

        # Location metrics
        loc_predict = probs[:, 0, :]
        loc_labels =  labels[:, 0, :]

        locate = loc_predict.argmax(dim=1)
        locate = torch.nn.functional.one_hot(locate, num_classes=loc_predict.shape[1]).float()
        locate_acc = (locate * loc_labels).sum(dim=1)

        buggy_labels = 1 - loc_labels[:, 0]

        # Buggy classification
        false_alarms = 1 - ((1 - buggy_labels)*locate_acc).sum() / ((1 - buggy_labels).sum() + 1e-9)
        bug_acc      = (buggy_labels * locate_acc).sum() / (buggy_labels.sum() + 1e-9)

        # Classification
        cls_predict = loc_predict[:, 0].round()
        cls_labels  = loc_labels[:, 0]
        cls_acc = (cls_predict * cls_labels).mean() + ((1 - cls_predict) * buggy_labels).mean()

        #Repair pointer
        rep_probs = probs[:, 1, :]
        rep_labels = labels[:, 1, :]

        target_probs   = (rep_labels * rep_probs).sum(dim=-1)
        target_predict = target_probs.round()
        target_acc = (target_predict * buggy_labels).sum() / (1e-9 + buggy_labels.sum())

        joint_acc = (buggy_labels * locate_acc * target_predict).sum() / (1e-9 + buggy_labels.sum())

        return {
            "classification_acc": cls_acc.item(),
            "localization_acc": locate_acc.mean().item(),
            "bug_acc": bug_acc.item(),
            "false_alarm_rate": false_alarms.item(),
            "repair_acc": target_acc.item(),
            "loc_repair_acc": joint_acc.item(),
            "avg_prediction": cls_predict.mean().item()
        }

    def loc_repair_acc(self, tokens, position_ids = None, labels = None):
        pass

    def forward(self, tokens, token_mask = None, position_ids = None, labels = None):

        prediction = self.loc_repair_logits(tokens, position_ids, labels)

        # Mask prediction
        if token_mask is not None:
            token_mask = token_mask.float().unsqueeze(1).expand_as(prediction)
            prediction = _mask(prediction, token_mask)

        # Calculate a loss if necessary
        if labels is not None:
            log_probs = nn.LogSoftmax(dim=2)(prediction)
            norm = labels.sum(dim=-1, keepdim = True)

            per_token_loss = (-labels * log_probs) / (norm + 1e-9)
            per_example_loss = per_token_loss.sum(dim=-1)

            per_task_loss = per_example_loss.mean(dim = 0)
            return per_task_loss.sum(), prediction

        return prediction

class VarMisuseModel(VarMisuseBaseModel):

    def __init__(self, config, encoder):
        super().__init__(config, encoder)

        self.head = _LocRepairPointerHead(config)

    def loc_repair_logits(self, tokens, position_ids = None, labels = None):
        attention_mask = tokens.sum(dim=2).clamp_(0, 1)

        encoding, _ = self.encoder(
            tokens = tokens,
            attention_mask = attention_mask.bool(),
            position_ids = position_ids
        )

        return self.head(encoding)

# General model that works with inner repairs and localization --------------------------------

class _LocateHead(nn.Module):

    def __init__(self, config):
        super().__init__()

        self.ffn_in  = nn.Linear(2 * config.hidden_size, config.hidden_size)
        self.ffn_out = nn.Linear(config.hidden_size, 1)

        self.apply(init_weights)

    def forward(self, context_embed, token_embed, token_mask = None, labels = None):
        assert context_embed.shape[1] == token_embed.shape[1]

        # Localization prediction --------------------------------

        diff_vector = token_embed - context_embed
        diff_vector = torch.cat([context_embed, diff_vector], dim = 2)
        hidden = self.ffn_in(diff_vector)
        hidden = nn.Tanh()(hidden)
        hidden = self.ffn_out(hidden)
        hidden = hidden.squeeze(-1)

        if token_mask is not None: hidden = _mask(hidden, token_mask)

        # Loss calculation ---------------------------------------

        if labels is not None:
            locate_labels = labels[:, 0, :]
            log_probs = nn.LogSoftmax(dim=1)(hidden)
            loss = (-locate_labels * log_probs).sum(dim=1)

            return loss.mean(), hidden

        return None, hidden

class _RepairHead(nn.Module):

    def __init__(self, config):
        super().__init__()
        self.config = config

        if config.decoder_vocab_size > 0: # We have a target vocab
            self.decoder = nn.Linear(config.hidden_size, config.decoder_vocab_size, bias = False)
            self.apply(init_weights)

    def forward(self, error_embed, context_embed, token_mask = None, labels = None, target_labels = None):

        # Compute a local pointer --------------------------------

        repair_logits = torch.bmm(error_embed.unsqueeze(1), context_embed.transpose(2, 1)).squeeze()
        repair_logits /= math.sqrt(error_embed.shape[1])

        if len(repair_logits.shape) < 2:
            repair_logits = repair_logits.unsqueeze(0)

        if token_mask is not None and not self.config.token_annotate:
            repair_logits = _mask(repair_logits, token_mask)

        if labels is not None:
            repair_labels = labels[:, 1, :]

        # Compute a global vocab index ---------------------------

        if hasattr(self, "decoder"):
            decoder_logits = self.decoder(error_embed)
            repair_logits  = torch.cat([repair_logits, decoder_logits], dim = 1)

            if labels is not None and target_labels is not None:
                ohe_labels = F.one_hot(target_labels, num_classes=self.config.decoder_vocab_size)
                ohe_labels[:, 0] = 0
                repair_labels = torch.cat([repair_labels, ohe_labels], dim = 1)

        # Loss computation ---------------------------------------

        if labels is not None:
            repair_log_probs = nn.LogSoftmax(dim = 1)(repair_logits)
            norm = repair_labels.sum(dim = -1).clamp_(0, 1)

            # Collect log probs
            # log sum_(t_i = w)(P(t_i)) = log sum_(t_i = w)(exp log P(t_i))
            #      = LSE(log P(t_i))
            repair_log_probs = _mask(repair_log_probs, repair_labels)
            per_example_loss = -norm * torch.logsumexp(repair_log_probs, dim = 1)

            return per_example_loss.mean(), repair_logits

        return None, repair_logits

class LocateRepairModel(nn.Module):

    def __init__(self, config, encoder):
        super().__init__()

        self.config = config
        self.encoder = encoder

        self.locate_head = _LocateHead(config)
        self.repair_head = _RepairHead(config)

    @torch.no_grad()
    def score(self, logits, labels):
        locate_logits, repair_logits = logits

        # Score for localization
        loc_predict = nn.Softmax(dim = 1)(locate_logits)
        loc_labels =  labels[:, 0, :]

        locate = loc_predict.argmax(dim=1)
        locate = torch.nn.functional.one_hot(locate, num_classes=loc_predict.shape[1]).float()
        locate_acc = (locate * loc_labels).sum(dim=1)

        buggy_labels = 1 - loc_labels[:, 0]

        # Buggy classification
        false_alarms = 1 - ((1 - buggy_labels)*locate_acc).sum() / ((1 - buggy_labels).sum() + 1e-9)
        bug_acc      = (buggy_labels * locate_acc).sum() / (buggy_labels.sum() + 1e-9)

        # Classification
        cls_predict = loc_predict[:, 0].round()
        cls_labels  = loc_labels[:, 0]
        cls_acc = (cls_predict * cls_labels).mean() + ((1 - cls_predict) * buggy_labels).mean()

        # Repair scores
        rep_probs = nn.Softmax(dim = 1)(repair_logits)
        rep_labels = labels[:, 1, :]

        if rep_probs.shape[1] != rep_labels.shape[1]:
            target_labels = labels[:, 2, :]
            target_labels = target_labels[loc_labels.bool()]
            ohe_labels = F.one_hot(target_labels, num_classes=self.config.decoder_vocab_size)
            ohe_labels[:, 0] = 0
            rep_labels = torch.cat([rep_labels, ohe_labels], dim = 1)

        target_probs   = (rep_labels * rep_probs).sum(dim=-1)
        target_predict = target_probs.round()
        target_acc = (target_predict * buggy_labels).sum() / (1e-9 + buggy_labels.sum())

        joint_acc = (buggy_labels * locate_acc * target_predict).sum() / (1e-9 + buggy_labels.sum())

        return {
            "classification_acc": cls_acc.item(),
            "localization_acc": locate_acc.mean().item(),
            "bug_acc": bug_acc.item(),
            "false_alarm_rate": false_alarms.item(),
            "repair_acc": target_acc.item(),
            "loc_repair_acc": joint_acc.item(),
            "avg_prediction": cls_predict.mean().item()
        }

    def forward(self, tokens, token_mask = None, position_ids = None, labels = None):

        attention_mask = tokens.sum(dim=2).clamp_(0, 1)

        context_embed, token_embed = self.encoder(
            tokens = tokens,
            attention_mask = attention_mask.bool(),
            position_ids = position_ids,
            token_type_ids = token_mask if self.config.token_annotate else None,
        )

        locate_loss, locate_logits = self.locate_head(context_embed,
                                                        token_embed,
                                                        token_mask,
                                                        labels)

        # Either use the gold localization or the predicted to get the error position

        error_repair_labels = None

        if labels is not None: # We are training
            locate_mask = labels[:, 0, :].bool()

            if self.config.decoder_vocab_size > 0:
                assert labels.shape[1] >= 2, "If a target vocabulary is specified we expect that target labels are provided."

                error_repair_labels = labels[:, 2, :]
                error_repair_labels = error_repair_labels[locate_mask]

        else: # We are at inference
            locate = locate_logits.argmax(dim=1)
            locate_mask = F.one_hot(locate, num_classes=tokens.shape[1]).bool()

        error_hidden = context_embed[locate_mask]

        # ----------------------------------------------------------------

        repair_loss, repair_logits = self.repair_head(
            error_hidden,
            context_embed,
            token_mask,
            labels,
            error_repair_labels
        )

        if labels is not None:
            return locate_loss + repair_loss, (locate_logits, repair_logits)

        return (locate_logits, repair_logits)

# Masked repair ----------------------------------------------------------------

class MaskedRepairModel(nn.Module):

    def __init__(self, config, encoder):
        super().__init__()

        self.config = config
        self.encoder = encoder
        self.repair_head = _RepairHead(config)

    @torch.no_grad()
    def score(self, repair_logits, labels):

        # Repair mask
        loc_labels =  labels[:, 0, :]
        buggy_labels = 1 - loc_labels[:, 0]

        # Repair scores
        rep_probs = nn.Softmax(dim = 1)(repair_logits)
        rep_labels = labels[:, 1, :]

        if rep_probs.shape[1] != rep_labels.shape[1]:
            target_labels = labels[:, 2, :]
            target_labels = target_labels[loc_labels.bool()]
            ohe_labels = F.one_hot(target_labels, num_classes=self.config.decoder_vocab_size)
            ohe_labels[:, 1] = 0
            rep_labels = torch.cat([rep_labels, ohe_labels], dim = 1)

        target_probs   = (rep_labels * rep_probs).sum(dim=-1)
        target_predict = target_probs.round()
        target_acc = (target_predict * buggy_labels).sum() / (1e-9 + buggy_labels.sum())

        return {
            "repair_acc": target_acc.item()
        }

    def forward(self, tokens, token_mask = None, position_ids = None, labels = None, repair_mask = None):

        attention_mask = tokens.sum(dim=2).clamp_(0, 1)

        context_embed, _ = self.encoder(
            tokens = tokens,
            attention_mask = attention_mask.bool(),
            position_ids = position_ids,
            token_type_ids = token_mask if self.config.token_annotate else None,
        )

        # Either use the gold localization or the predicted to get the error position

        error_repair_labels = None

        if labels is not None: # We are training
            locate_mask = labels[:, 0, :].bool()

            if self.training and self.config.decoder_vocab_size > 0:
                assert labels.shape[1] >= 2, "If a target vocabulary is specified we expect that target labels are provided."

                error_repair_labels = labels[:, 2, :]
                error_repair_labels = error_repair_labels[locate_mask]

        else: # We are at inference
            if repair_mask is None:
                raise ValueError("Location labels are required to identify mask position.")
            locate_mask = repair_mask.bool()

        error_hidden = context_embed[locate_mask]

        # ----------------------------------------------------------------

        repair_loss, repair_logits = self.repair_head(
            error_hidden,
            context_embed,
            token_mask,
            labels,
            error_repair_labels
        )

        if labels is not None:
            return repair_loss, repair_logits

        return repair_logits


import re
import logging
from datetime import datetime, timezone

__all__ = ('datetime_to_ns',)

logger = logging.getLogger('aionationstates')

class DataClassWithId:
    def __eq__(self, other):
        # Ids in NS are pretty much always not globally unique.
        if type(self) is not type(other):
            return NotImplemented
        return self.id == other.id

    def __hash__(self):
        return hash((self.id,))

    def __repr__(self):
        return f'<{self.__class__.__name__} id={self.id}>'

def normalize(identifier):
    identifier = identifier.lower().replace(' ', '_')
    if not re.match('^[a-z0-9_-]+$', identifier):
        raise ValueError(f'provided identifier {identifier} contains invalid'
                         ' characters.')
    return identifier

def banner_url(id):
    return f'https://www.nationstates.net/images/banners/{id}.jpg'

def timestamp(line):
    return datetime.utcfromtimestamp(int(line))

def utc_seconds(datetime_):
    return int(datetime_.replace(tzinfo=timezone.utc).timestamp())

def unscramble_encoding(text):
    """This is a workaround for a bug in the NS server-side code.
    (This entire lib is, honestly.)

    Specifically, somewhere in the process W-1252 encoded text is
    wrongly interpreted to be ISO-8859-1, resulting in *some* characters
    being deterministically unintentionally replaced with useless to the
    user Unicode control chars.

    This is a very common problem.  Common enough, in fact, to be
    accounted for in the HTML treatment of Character References as
    defined by the specification.  Well, it is technically a parse
    error, but nobody really cares since the correct, expected character
    is returned.  For this reason, the bug is not present (or at least
    not visible) on the NS web interface, and only shows itself when
    dealing with the API.

    Interestingly enough, these characters are not always serialized as
    NCRs, in the dispatch CDATA they are represented literally, meaning
    that even modifying the XML parser to include a bit of HTML leniency
    would not be enough.  Not that anyone would do that regardless.

    This function reverses the process, substiuting the unprintable mess
    returned by NS for the Unicode characters it must have originated
    from.

    It's a bit ugly, but gets the job done.
    """
    return text.translate(unscramble_table)

unscramble_table = str.maketrans({
    '\u0080': '\N{EURO SIGN}',
    '\u0082': '\N{SINGLE LOW-9 QUOTATION MARK}',
    '\u0083': '\N{LATIN SMALL LETTER F WITH HOOK}',
    '\u0084': '\N{DOUBLE LOW-9 QUOTATION MARK}',
    '\u0085': '\N{HORIZONTAL ELLIPSIS}',
    '\u0086': '\N{DAGGER}',
    '\u0087': '\N{DOUBLE DAGGER}',
    '\u0088': '\N{MODIFIER LETTER CIRCUMFLEX ACCENT}',
    '\u0089': '\N{PER MILLE SIGN}',
    '\u008A': '\N{LATIN CAPITAL LETTER S WITH CARON}',
    '\u008B': '\N{SINGLE LEFT-POINTING ANGLE QUOTATION MARK}',
    '\u008C': '\N{LATIN CAPITAL LIGATURE OE}',
    '\u008E': '\N{LATIN CAPITAL LETTER Z WITH CARON}',
    '\u0091': '\N{LEFT SINGLE QUOTATION MARK}',
    '\u0092': '\N{RIGHT SINGLE QUOTATION MARK}',
    '\u0093': '\N{LEFT DOUBLE QUOTATION MARK}',
    '\u0094': '\N{RIGHT DOUBLE QUOTATION MARK}',
    '\u0095': '\N{BULLET}',
    '\u0096': '\N{EN DASH}',
    '\u0097': '\N{EM DASH}',
    '\u0098': '\N{SMALL TILDE}',
    '\u0099': '\N{TRADE MARK SIGN}',
    '\u009A': '\N{LATIN SMALL LETTER S WITH CARON}',
    '\u009B': '\N{SINGLE RIGHT-POINTING ANGLE QUOTATION MARK}',
    '\u009C': '\N{LATIN SMALL LIGATURE OE}',
    '\u009E': '\N{LATIN SMALL LETTER Z WITH CARON}',
    '\u009F': '\N{LATIN CAPITAL LETTER Y WITH DIAERESIS}',
})

class aobject:
    """Inheriting this class allows you to define an async __init__.

    Code shamelessly ripped from StackOverflow.

    Before getting angry at me for abusing python features, remind
    yourself that all async/await code is already an abuse of generators
    and embrace the simple truth that practicality beats purity.
    """
    async def __new__(cls, *a, **kw):
        instance = super().__new__(cls)
        await instance.__init__(*a, **kw)
        return instance

    async def __init__(self):
        pass

def actually_synchronous(async_function):
    def wrapper(*args, **kwargs):
        coro_object = async_function(*args, **kwargs)
        try:
            coro_object.send(None)
        except StopIteration as e:
            return e.value
        else:
            raise TypeError("the function supplied isn't actually synchronous")
    return wrapper

async def alist(asyncgen):
    return [item async for item in asyncgen]

def datetime_to_ns(then):
    """Transform a :any:`datetime.datetime` into a NationStates-style
    string.

    For example "6 days ago", "105 minutes ago", etc.
    """
    if then == datetime(1970, 1, 1, 0, 0):
        return 'Antiquity'

    now = datetime.utcnow()
    delta = now - then
    seconds = delta.total_seconds()

    # There's gotta be a better way to do this...
    years,   seconds = divmod(seconds, 60*60*24*365)
    days,    seconds = divmod(seconds, 60*60*24)
    hours,   seconds = divmod(seconds, 60*60)
    minutes, seconds = divmod(seconds, 60)
    years   = int(years)
    days    = int(days)
    hours   = int(hours)
    minutes = int(minutes)
    seconds = round(seconds)

    if years > 1:
        if days > 1:
            return f'{years} years {days} days ago'
        elif days == 1:
            return '{years} years 1 day ago'
        return '{years} years ago'
    if years == 1:
        if days > 1:
            return f'1 year {days} days ago'
        elif days == 1:
            return '1 year 1 day ago'
        return '1 year ago'

    if days > 3:
        return f'{days} days ago'
    if days > 1:
        if hours > 1:
            return f'{days} days {hours} hours ago'
        elif hours == 1:
            return f'{days} days 1 hour ago'
        return f'{days} days ago'
    if days == 1:
        if hours > 1:
            return f'1 day {hours} hours ago'
        elif hours == 1:
            return '1 day 1 hour ago'
        return '1 day ago'

    if hours > 1:
        return f'{hours} hours ago'
    if hours == 1:
        return f'{minutes + 60} minutes ago'

    if minutes > 1:
        return f'{minutes} minutes ago'
    if minutes == 1:
        return '1 minute ago'

    return 'Seconds ago'


from videos_freeze_analyzer import VideosFreezeAnalyzer
from video_valid_points_list_generator import dowload_url
from video_valid_points_list_generator import VideoValidPointsListGeneratorFfmpeg
from video_freeze_analyzer import VideoFreezeAnalyzer

import json

def main(urls):
    files = []
    for url in urls:
        files.append(dowload_url(url))

    videos_list =[]
    for file_name in files:
        video_valid_list = VideoValidPointsListGeneratorFfmpeg(file_name).generate_valid_points_list()
        videos_list.append(VideoFreezeAnalyzer().analyze(video_valid_list))

    videos_output = VideosFreezeAnalyzer(videos_list).analyze()
    results = json.dumps(videos_output, indent=4)
    print(results)

if __name__ == '__main__':
    urls = ["https://storage.googleapis.com/hiring_process_data/freeze_frame_input_a.mp4",
            "https://storage.googleapis.com/hiring_process_data/freeze_frame_input_b.mp4",
            "https://storage.googleapis.com/hiring_process_data/freeze_frame_input_c.mp4"]

    main(urls)


import pandas as pd
from dotenv import load_dotenv
import os

# load env
load_dotenv()

# load dataset
url = "https://raw.githubusercontent.com/erijmo/3690/main/healthcare_dataset.csv"
df = pd.read_csv(url)

# set api key
api_key = os.getenv("OPENAI_API_KEY")

def get_healthcare_response(user_input, user_name, df):
    # search for keyword in user input
    for column in df.columns:
        if column.lower() in user_input:
            response = f"{user_name}, your {column.lower()} is {df[column].iloc[0]}"
            return response

    # if no keyword located, ask for clarification
    return "I'm sorry, I couldn't understand your request. Can you please provide more details?"

# prompt response
print("HealthcareBot: Hello! I'm your HealthcareBot. May I know your name, please?")

while True:
    user_name = input("User: ")

    # check if the user's name is in the system
    if user_name.lower() in df["Name"].str.lower().values:
        print(f"HealthcareBot: Thank you, {user_name}! How can I assist you today?")
        break
    else:
        print("HealthcareBot: I'm sorry, but I couldn't find your name in the system. Please try again.")

# user interaction loop
while True:
    user_input = input("User: ")

    # check if any exit-related keywords are present in the user input
    if any(keyword in user_input.lower() for keyword in ['exit', 'bye', 'quit']):
        print("HealthcareBot: Goodbye! If you have more questions, feel free to ask.")
        break

    response = get_healthcare_response(user_input, user_name, df)
    if response:
        print("HealthcareBot:", response)


from thumbor.utils import logger

try:
    import cv2  # noqa
    import numpy as np  # noqa

    CV_AVAILABLE = True
except ImportError:
    CV_AVAILABLE = False

class BaseDetector:
    def __init__(self, context, index, detectors):
        self.context = context
        self.index = index
        self.detectors = detectors

    def verify_cv(self) -> bool:
        if CV_AVAILABLE:
            return True

        logger.error(
            "OpenCV (cv2) is not available for thumbor. "
            "thumbor.detectors.local_detector.CascadeLoaderDetector "
            "can't be executed. Skipping..."
        )

        return False

    async def detect(self):
        raise NotImplementedError()

    async def next(self):
        if not self.detectors or self.index > len(self.detectors) - 2:
            return

        next_detector = self.detectors[self.index + 1](
            self.context, self.index + 1, self.detectors
        )
        await next_detector.detect()


class Node:
    def __init__(self, data):
        self.data = data
        self.nref = None
        self.pref = None

class Queue:
    def __init__(self):
        self.start = None
        self.end = None

    def is_empty(self):
        return self.start is None

    def pop(self):
        if self.is_empty():
            return None
        else:
            val = self.start.data
            if self.start == self.end:
                self.start = None
                self.end = None
            else:
                self.start = self.start.nref
                self.start.pref = None
            return val

    def push(self, val):
        new_node = Node(val)
        if self.is_empty():
            self.start = new_node
            self.end = new_node
        else:
            new_node.pref = self.end
            self.end.nref = new_node
            self.end = new_node

    def insert(self, n, val):
        if n == 0:
            new_node = Node(val)
            new_node.nref = self.start
            self.start.pref = new_node
            self.start = new_node
        else:
            current = self.start
            for i in range(n-1):
                current = current.nref
                if current is None:
                    raise IndexError("Index out of range")
            new_node = Node(val)
            new_node.nref = current.nref
            new_node.pref = current
            if current.nref is not None:
                current.nref.pref = new_node
            current.nref = new_node

    def print(self):
        current = self.start
        while current is not None:
            print(current.data, end=' ')
            current = current.nref


import sys

def qtm(seq):
    result = 0
    for move in seq:
        if move.startswith("("):
            result += 0 # this line does nothing, but I added it for clarity that we weight AUF as 0
        elif "2" in move:
            result += 2
        else:
            result += 1
    return result

def htm(seq):
    result = 0
    for move in seq:
        if move.startswith("("):
            result += 0 # this line does nothing, but I added it for clarity that we weight AUF as 0
        else:
            result += 1
    return result

eval_dict = {
    "R": 1,
    "R'": 1,
    "R2": 1.5,

    "L": 1.1,
    "L'": 1.1,
    "L2": 1.6,

    "U": 1,
    "U'": 1,
    "U2": 1.3,

    "D": 1.1,
    "D'": 1.1,
    "D2": 1.9,

    "F": 1.6,
    "F'": 1.6,
    "F2": 1.6,

    "B": 2,
    "B'": 2.2,
    "B2": 3.5,
}

def eval_sequence(seq):
    result = 0
    for move in seq:
        if move.startswith("("):
            result += 0 # this line does nothing, but I added it for clarity that we weight AUF as 0
        else:
            result += eval_dict[move]
    return result

if __name__ == '__main__' :
    if len(sys.argv) == 2:
        file_to_process = sys.argv[1]
    else:
        print("please provide a file as an argument")
        exit(1)

    all_sequences = []
    with open(file_to_process, "r", encoding="UTF-8") as f:
        for line in f:
            line = line.strip()
            if line and not line.startswith("INFO"):
                seq = line.split()
                #remove U.
                if seq[0].startswith("U"):
                    seq[0] = "(%s)" % seq[0]
                if seq[-1].startswith("U"):
                    seq[-1] = "(%s)" % seq[-1]

                all_sequences.append([seq, eval_sequence(seq)])
    for (alg, score) in sorted(all_sequences, key = lambda x:x[1]):
        print("score=%.2f qtm=%d htm=%d\n%s" % (score, qtm(alg), htm(alg), " ".join(alg)))


import socket
import tkinter as tk
from tkinter import *
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to the server
host = '10.0.65.12'
port = 5556
s.connect((host, port))

print('Connected to the server')

name = input("What's your name: ")
print(name)

s.send(str.encode(name))

flag = s.recv(1042).decode()

if flag:
    root = Tk()
    root.title("21 Dares")
    canvas = Canvas(width=800,height=250)
    root.geometry("800x600")
    para="Basically, a online multiplayer game where players will take turns saying numbers 1,2 or\n 	 3 and it will add up to the main thing, the person whose number reaches 21 has to do a dare.\n Dare will ask that person to turn on the webcam and the other players will ask that person to \ndo a dare. In 3 mins that person has to do the dare. "
    label_head = tk.Label(root, text="Welcome To 21 Dares",font=('Arial,',40),foreground='gold3')
    label_head.pack()
    canvas.create_rectangle(140,70,310,240,outline ="black",fill ="white",width = 2)
    canvas.create_rectangle(330,70,500,240,outline ="black",fill ="white",width = 2)
    canvas.create_rectangle(520,70,690,240,outline ="black",fill ="white",width = 2)
    canvas.pack()
    def start_game():
        root.destroy()
        root2 = Tk()
        root2.title("21 Dares")
        root2.geometry("1500x800")
        w= root2.winfo_screenwidth()
        h= root2.winfo_screenheight()
        canvs2 = tk.Canvas(root2, height=900, width=1500)
        count_bx = canvs2.create_rectangle(600,50,850,300,fill="DarkSeaGreen1")
        ply1 = canvs2.create_oval(600,350,850,600,fill="lavender blush1")
        ply1_lb = canvs2.create_text(725,475,text="0",font=('Calibri',90))
        ply2 = canvs2.create_oval(900,350,1150,600,fill="lavender blush1")
        ply2_lb = canvs2.create_text(1025,475,text="0",font=('Calibri',90))
        ply3 = canvs2.create_oval(300,350,550,600,fill="lavender blush1")
        ply3_lb = canvs2.create_text(425,475,text="0",font=('Calibri',90))

        lb_count = canvs2.create_text(720,180,text="0",font=('Calibri',90))
        bt_1=tk.Button(root2,text="1",font=('Calibri',25),foreground='black',background="light goldenrod yellow")
        bt_1.place(x=420, y=700)
        bt_2=tk.Button(root2,text="2",font=('Calibri',25),foreground='black',background="light goldenrod yellow")
        bt_2.place(x=720, y=700)
        bt_3=tk.Button(root2,text="3",font=('Calibri',25),foreground='black',background="light goldenrod yellow")
        bt_3.place(x=1020, y=700)

        canvs2.pack()
        root2.mainloop()

    label_desp = tk.Label(root,text=para,font=('Arial',20),foreground='dark olive green')
    label_desp.pack(padx=5,pady=120)
    start_button=tk.Button(root,text="Start the game",font=('Calibri',35),foreground='sandy brown',command = start_game)
    start_button.pack(padx=5,pady=20)

    root.mainloop()

# Close the socket
s.close()


import torch
from torch.optim import SGD
import torch.nn.functional as F
from sklearn.metrics import accuracy_score

from models_torch.FFM import FFM_Layer
from utils.load_data import load_criteo_data

if __name__ == '__main__':
    (X_train, y_train), (X_test, y_test), feature_info = load_criteo_data('dataset/criteo_sample.csv',
                                                                          sparse_return='category')
    X_train = torch.tensor(X_train, dtype=torch.float32)
    X_test = torch.tensor(X_test, dtype=torch.float32)
    y_train = torch.tensor(y_train, dtype=torch.float32)
    y_test = torch.tensor(y_test, dtype=torch.float32)
    # 参数
    k = 8
    n_epoch = 10
    lr = 0.01
    # 初始化
    model = FFM_Layer(dense_features=feature_info['dense_feature'], sparse_features=feature_info['sparse_feature'],
                      sparse_feature_dim=feature_info['max_one_hot_dim'], k=k)
    optim = SGD(lr=lr, params=model.parameters(), weight_decay=1e-4)
    criterion = F.binary_cross_entropy
    # 训练模型
    for epoch in range(n_epoch):
        model.train()
        logits = torch.reshape(model(X_train), (-1,))
        loss = criterion(logits, y_train)
        # 更新权重
        optim.zero_grad()   # 清除累计梯度
        loss.backward()
        optim.step()
        if epoch % 1 == 0 and epoch:
            print('epoch: {}, loss: {}'.format(epoch, loss))

    # 模型评估
    model.eval()
    with torch.no_grad():
        pred = torch.reshape(model(X_test), (-1,))
        loss = criterion(pred, y_test)
        pred = [1 if x > 0.5 else 0 for x in pred]
        print('acc: {}, loss: {}'.format(accuracy_score(y_test, pred), loss))


import subprocess
from multiprocessing import Pool
import os
import numpy as np
import sys

def Thread(arg):
    print(arg)
    file = open('output/' + str(0) + '.log', 'w')
    subprocess.call(arg, shell=True, stdout=file)

def main():
    seed = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    batch = np.array([10, 50, 100, 200])
    # batch = batch.repeat(9)
    batch = np.tile(batch, 9)
    hidden = np.array([25, 50, 100])
    hidden = hidden.repeat(4)
    hidden = np.tile(hidden, 3)
    optim = {0: 'adam', 1: 'adagrad', 2: 'adadelta', 3: 'sgd'}
    op_idx = np.array([0, 1, 2, 3])
    op_idx = op_idx.repeat(12)
    lr = np.array([0.1, 0.01, 0.001])
    ed_pass = np.array([4, 8, 10])

    idx = [x for x in range(36)]

    arglist = []
    st = int(sys.argv[1])
    print(st)
    end = int(sys.argv[2])
    print(end)

    for i in range(st, end):
        opt_st = optim[op_idx[i]]
        pcmd = "python dt_pl_parser.py  --train data/wsj10_tr --tag_num 1 --hidden " + str(
            hidden[i]) + " " + "--batch " + str(
            batch[i]) + " " + "--optim " + opt_st + " " + "--do_eval --use_trigram " + "--sample_idx " + str(idx[i])
        arglist.append(pcmd)
        print(pcmd)

    p = Pool(4)
    p.map(Thread, arglist, chunksize=1)
    p.close()
    p.join()

if __name__ == '__main__':
    main()


#!/usr/bin/env python

import rospy
import smach
from mavros_msgs.msg import WaypointList
from std_msgs.msg import Bool,String
from PrintColours import *
#from aerialcore_common.srv import ConfigMission, ConfigMissionResponse

#
# def mission_callback(req):
#     rospy.loginfo(" /mission/new service was called")
#     return ConfigMissionResponse(success=True)

class GCSConnection(smach.State):
    def __init__(self,autopilot,uav_id):
        smach.State.__init__(
            self, outcomes=['mission_upload', 'shutdown'])
        #rospy.Service('uav_2/mission/new', ConfigMission, mission_callback)
        self.autopilot = autopilot
        self.uav_id = uav_id

    def execute(self, ud):
        rospy.loginfo('[GCSconnection] - GCSconnection state')
        #rospy.set_param("/uav_{}_sm/autopilot".format(self.uav_id),"GCSconnection") changed
        airframe_pub = rospy.Publisher("/uav_{}_sm/com/airframe_type".format(self.uav_id), String, queue_size=10)

        mission_state_pub = rospy.Publisher("/uav_{}_sm/com/mission_state".format(self.uav_id), String, queue_size=10)
        if self.autopilot == "px4":
            airframe = self.autopilot + "/vtol"
        if self.autopilot == "dji":
            airframe = self.autopilot + "/M210"

        # transition to gcs_connection state
        while not rospy.is_shutdown():
            airframe_pub.publish(airframe)
            mission_state_pub.publish("uav_{} connected to the GCS".format(self.uav_id))
            if self.autopilot == "px4":
                waypointList_msg = rospy.wait_for_message("/uav_{}/mavros/mission/waypoints".format(self.uav_id), WaypointList)# modify
                rospy.loginfo(CBLUE+"There are %d waypoints in the mission"+CEND, len(waypointList_msg.waypoints))
                if len(waypointList_msg.waypoints) > 0:
                    rospy.loginfo("Vehicle with PX4 with MISSION LOADED has %d waypoints", len(waypointList_msg.waypoints))
                    return 'mission_upload'
                else:
                    rospy.loginfo("Vehicle with PX4 have no mission loaded")
                    rospy.sleep(1)
            elif self.autopilot == "dji":
                rospy.loginfo(CBLUE+"Vehicle with DJI"+CEND)
                cmd_msg = rospy.wait_for_message("/uav_{}/dji_sm/upload_mission".format(self.uav_id), Bool)
                if cmd_msg.data == True:
                    rospy.loginfo(CBLUE+"Vehicle with DJI has mission loaded"+CEND)
                    return 'mission_upload'
            # if (True):# Modify
            #     rospy.sleep(2)
            #     return 'mission_upload'

        return 'shutdown'


import os
import sys
import time
from functools import partial
from multiprocessing import Pool
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('window-size=1920,1080')
chrome_options.add_argument('start-maximised')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

def scroll_to_page_end_n_times(browser, s, page_load_wait_seconds):
  for _ in range(s):
    print(f'ScrollHeight before scrolling: {browser.execute_script("return document.documentElement.scrollHeight")}')
    browser.execute_script('window.scrollTo(0, document.body.scrollHeight);')
    print(f'Scrolled, waiting for {page_load_wait_seconds} seconds to load page')
    time.sleep(page_load_wait_seconds)
    print(f'ScrollHeight after scrolling: {browser.execute_script("return document.documentElement.scrollHeight")}')
  return

def links_collection(main_page_link, num_links, start_at_link_num, scroll_limit, page_load_wait_seconds, element_load_wait_seconds):
  print('Working in background...')

  n = 0
  s = 0
  link_num = start_at_link_num
  links_dict = {}

  chrome_driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
  with chrome_driver as browser:
    browser.get(main_page_link)
    while n < num_links:
      try:
        link_path = '/html/body/div[1]/div/div[1]/div/div[3]/div/div/div/div[1]/div[1]/div[2]/div/div/div/div/div/div/div[' + str(
            link_num) + ']/a/div[1]/div/div/div/div/div[2]/div[1]/div/div/div/div[2]/span/span/object/a'
        page = WebDriverWait(browser, element_load_wait_seconds).until(EC.visibility_of_element_located((By.XPATH, link_path)))
        event_url_long = page.get_attribute('href')
        event_url_cutoff = event_url_long[event_url_long.find('events/') + 7:].find('/')
        event_url = event_url_long[:event_url_long.find('events/') + 7 + event_url_cutoff + 1]
        event_title = page.text
        print(f'Fetching link for event: {event_title}')
        links_dict[event_title + str(n)] = event_url
        link_num += 1
        n += 1
      except Exception as e:
        # print(f'links_collection exception:\n{e}')
        if s < scroll_limit:
          s += 1
          scroll_to_page_end_n_times(browser, s, page_load_wait_seconds)
        else:
          break
  print(f'\nNumber of links: {str(len(links_dict.items()))}')
  return links_dict

def get_location(browser, element_load_wait_seconds):
  try:
    link_path = '/html/body/div[1]/div/div[1]/div/div[3]/div/div/div/div[1]/div[1]/div[2]/div/div/div[1]/div[1]/div[2]/div/div[2]/div/div[1]/div/div/div[3]'
    page = WebDriverWait(browser, element_load_wait_seconds).until(EC.visibility_of_element_located((By.XPATH, link_path)))
    e_location = page.text
  except Exception as e:
    e_location = 'n/a'
    #print(f'get_location exception:\n{e}')
  return e_location

def get_datetime(browser, element_load_wait_seconds):
  try:
    link_path = '/html/body/div[1]/div/div[1]/div/div[3]/div/div/div/div[1]/div[1]/div[2]/div/div/div[1]/div[1]/div[2]/div/div[2]/div/div[1]/div/div/div[1]/span'
    page = WebDriverWait(browser, element_load_wait_seconds).until(EC.visibility_of_element_located((By.XPATH, link_path)))
    e_datetime_str = page.text

  except Exception as e:
    e_datetime_str = 'n/a'
    # print(f'get_datetime exception:\n{e}')

  return e_datetime_str

def get_host_and_num_people_responded(browser, element_load_wait_seconds):
  try:
    link_path = '/html/body/div[1]/div/div[1]/div/div[3]/div/div/div/div[1]/div[1]/div[2]/div/div/div[2]/div/div/div/div/div[1]/div/div/div'
    page = WebDriverWait(browser, element_load_wait_seconds).until(EC.visibility_of_element_located((By.XPATH, link_path)))

    children = page.find_elements(By.XPATH, './child::*')
    e_host = ''
    e_num_people_responded = ''
    for child in children:
      textContent = child.text
      if 'Event by' in textContent:
        e_host = textContent[textContent.find('Event by') + 9:]
      elif 'people responded' in textContent:
        e_num_people_responded = textContent[:textContent.find('people responded')].strip()
      elif 'person responded' in textContent:
        e_num_people_responded = textContent[:textContent.find('person responded')].strip()
      else:
        pass

    if e_host == '':
      e_host = 'n/a'
    if e_num_people_responded == '':
      e_num_people_responded = 'n/a'

  except Exception as e:
    e_host = 'n/a'
    e_num_people_responded = 'n/a'
    # print(f'get_host_and_num_people_responded exception:\n{e}')
  return e_host, e_num_people_responded

def get_description(browser, element_load_wait_seconds):
  try:
    link_path = '/html/body/div[1]/div/div[1]/div/div[3]/div/div/div/div[1]/div[1]/div[2]/div/div/div[2]/div/div/div/div/div[1]/div/div/div/div[last()]/div/span'
    page = WebDriverWait(browser, element_load_wait_seconds).until(EC.visibility_of_element_located((By.XPATH, link_path)))

    children = page.find_elements(By.XPATH, './child::*')
    for child in children:
      try:
        see_more_btn = child.find_element(By.XPATH, "./div[@role='button']")
        see_more_btn.click()
      except:
        pass

    children = page.find_elements(By.XPATH, './child::*')
    e_description = ''
    for child in children:
      e_description += child.text
      e_description += '\n'

    if 'See less' in e_description:
      e_description = e_description[:e_description.find(' See less')]
    elif 'See more' in e_description:
      e_description = e_description[:e_description.find('... See more')]
    else:
      pass

    if e_description == '':
      e_description = 'n/a'

  except Exception as e:
    e_description = 'n/a'
    # print(f'get_description exception:\n{e}')
  return e_description

def get_image_url(browser, element_load_wait_seconds):
  try:
    link_path = '/html/body/div[1]/div/div[1]/div/div[3]/div/div/div/div[1]/div[1]/div[2]/div/div/div[1]/div[1]/div[1]/div/div/div[2]/div/a/div/div/div/div/div/img'
    page = WebDriverWait(browser, element_load_wait_seconds).until(EC.visibility_of_element_located((By.XPATH, link_path)))
    img_path = page.get_attribute('src')
  except Exception as ignore:
    try:
      link_path = '/html/body/div[1]/div/div[1]/div/div[3]/div/div/div/div[1]/div[1]/div[2]/div/div/div[1]/div[1]/div[1]/div/div/div[2]/div/a/div/div/div/div/img'
      page = WebDriverWait(browser, element_load_wait_seconds).until(EC.visibility_of_element_located((By.XPATH, link_path)))
      img_path = page.get_attribute('src')
    except Exception as e:
      img_path = 'n/a'
      # print(f'get_image_url exception:\n{e}')
  return img_path

def crawl_links(element_load_wait_seconds, current_link):
  e_location = 'n/a'
  e_datetime = 'n/a'
  e_host = 'n/a'
  e_num_people_responded = 'n/a'
  e_description = 'n/a'
  img_path= 'n/a'
  try:
    chrome_driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
    with chrome_driver as browser:
      print(f'Crawling on: {current_link}')

      # Using Selenium
      browser.get(current_link)

      e_location = get_location(browser, element_load_wait_seconds)
      e_datetime = get_datetime(browser, element_load_wait_seconds)
      e_host, e_num_people_responded = get_host_and_num_people_responded(browser, element_load_wait_seconds)
      e_description = get_description(browser, element_load_wait_seconds)
      img_path = get_image_url(browser, element_load_wait_seconds)

  except Exception as e:
    print(f'crawl_links exception:\n{e}')  # if the links are not found in a page, print exception

  return e_location, e_host, e_num_people_responded, e_datetime, e_description, img_path

def main(dict):
  num_links = dict['num_links']
  start_at_link_num = dict['start_at_link_num']
  scroll_limit = dict['scroll_limit']
  page_load_wait_seconds = dict['page_load_wait_seconds']
  element_load_wait_seconds = dict['element_load_wait_seconds']
  event_search_keyword = dict['event_search_keyword']
  main_page_link = f'https://www.facebook.com/events/search/?q={event_search_keyword}'
  pool_size = dict['pool_size']

  links_dict = links_collection(main_page_link, num_links, start_at_link_num, scroll_limit, page_load_wait_seconds, element_load_wait_seconds)

  print('\nInitiating scraping...')
  #pool = Pool(processes=pool_size)  # creates pool of n processes at a time
  #func = partial(crawl_links, element_load_wait_seconds)
  #e_details_list = pool.map(func, list(links_dict.values()))  # maps the function crawl_links (with arg element_load_wait_seconds) with the links_dict.items() input
  e_details_list = [crawl_links(element_load_wait_seconds, link) for link in list(links_dict.values())]

  return_dict = { 'payload' : [] }
  e_details_labels = ['location', 'host', 'numPeopleResponded', 'datetime', 'details', 'imgPath']
  for (e_name, e_link), e_details in zip(links_dict.items(), e_details_list):
    event_dict = {}
    event_dict['link'] = e_link
    event_dict['name'] = e_name[:-1]
    for e_detail_item_label, e_detail_item  in zip(e_details_labels, e_details):
        event_dict[e_detail_item_label] = e_detail_item
    return_dict['payload'].append(event_dict)

    return return_dict


#Fall2019W7
#C-Elevator Trouble

def failMsg():
    print("use the stairs")

def main():
    params = input().split()
    f = int(params[0])
    s = int(params[1])
    g = int(params[2])
    u = int(params[3])
    d = int(params[4])
    floorDif = g - s
    curr = s
    buttonPresses = 0
    if (floorDif % 2 == 1 and u % 2 == 0 and d % 2 == 0):
        failMsg()
    #incomplete

#use breadth first search
#queues or dictionaries??


from flask import Flask, g, render_template, request, send_from_directory, url_for
import sqlite3, os, datetime

from werkzeug.utils import redirect, secure_filename

SITENAME = 'SaLeeMas - PicShare'
# Définir le dossier dans lequel les photos
# vont petre uploadés
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
app = Flask(__name__)
# Attention, à préciser le répertoire local !
DATABASE = 'app.db'
# On définit une variable globale qui rendra
# nos fichiers accesssibles même via les templates
# récupéré de la doc FLASK
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

# Se connecter à la DB (code récupéré de la doc FLASK)
def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = sqlite3.connect(DATABASE)
    return db

# La fonction pour spécifier les types de fichier autorisés
def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

# La route de la homepage
@app.route("/", methods=["GET", "POST"])
def show_pictures():
    db = get_db()
    print("All categories")
    if request.method == 'GET':
        categories = db.execute("SELECT name from categories order by id")
        pictures = db.execute("SELECT id, title, filename \
                               from pictures order by upload_date desc")
        # print(pictures.fetchall())
        return render_template("index.html",
                all_pictures=pictures, all_categories=categories)

# La route de la homepage avec la categorie name en argument
@app.route("/<category>", methods=["GET", "POST"])
def show_category_pictures(category):
    db = get_db()
    if request.method == 'GET':
        # print("Chosen category", category)
        categories = db.execute("SELECT name from categories order by id")
        if category:
            print("category", category)

            pictures = db.execute("SELECT pictures.id, title, filename \
                                   from pictures left join categories \
                                   on category_id = categories.id \
                                   where categories.name = (?) \
                                   order by upload_date desc", [category])
            # print(pictures.fetchall())
            return render_template("index.html",
                                    all_pictures=pictures,
                                    all_categories=categories,
                                    chosen_category=category)

# La route du chemin d'accès à l'image à renvoyer, avec
# le nom du répertoire "uploads/", suivi du nom du fichier image
@app.route('/uploads/<filename>')
def download_file(filename):
    print("send_from_directory",
          send_from_directory(app.config["UPLOAD_FOLDER"], filename))
    return send_from_directory(app.config["UPLOAD_FOLDER"], filename)

# La route de la page upload
@app.route("/upload",  methods=["GET", "POST"])

def upload():
    db = get_db()
    categories_cursor = db.execute("select name from categories order by id;")
    categories_name = categories_cursor.fetchall()
    print("I am the result of your query: ", categories_name)
    list_of_categories = []
    for category in categories_name:
        name = category[0]
        list_of_categories.append(name)
    print("i am the list of cat : ", list_of_categories)
    if request.method == 'POST':
        file = request.files['file']
        print("I am the files.filename : ", file.filename)
        if allowed_file(file.filename):
            filename = secure_filename(file.filename) #  c'est le path
            title = request.form.get("title")
            description = request.form.get("description")
            print(description, " - ", request.form.get("description"))
            category = request.form.get("category")
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            upload_date = datetime.datetime.now()
            # print("datetime.datetime.now()", datetime.datetime.now())
            # sauvegarder le fichier dans la DB
            db = get_db()
            if category:
                cursor1 = db.execute("SELECT id from categories \
                                where name = ?", [category])
                category_id = cursor1.fetchone()
                # print(category_id[0])
            db.execute("INSERT into pictures (title, filename, upload_date, category_id, description) \
                                      values (?    ,     ?   ,       ?    ,      ?     ,      ?     )",
                        [title, filename, upload_date, int(category_id[0]), description])
            # # vérifier si le titre existe déjà
            # cursor_title = db.execute(
            # "SELECT id, title FROM pictures WHERE title = (?)", [title])
            # print("I'am the cursor: ", cursor_title)
            # # On l'enregistre dans une variable et on l'affiche
            # # avec fetchone, si le résultat n'est pas None on retourne 404
            # title_request = cursor_title.fetchone()
            # print("hey I'm the request title ", title_request)
            # if title_request is not None: abort(404)
            db.commit()
            return render_template("picture_uploaded.html")
    return render_template('upload.html', list_of_categories=list_of_categories)

#  La route de la page picture
@app.route("/picture/<picture_id>", methods=["GET", "POST"])
def picture_id(picture_id):
    if picture_id and request.method == 'POST':
        comment = request.form.get("comment")
        # print("I am the comment :", comment)
        comment_date = datetime.datetime.now()

        # print("datetime.datetime.now()", comment_date)
        # sauvegarder le fichier dans la DB
        db = get_db()
        db.execute("INSERT into comments (comment, comment_date, picture_id) \
                                  values (?      ,        ?    , ?)",
                    [comment, comment_date, picture_id])
        db.commit()
    if picture_id and request.method == 'GET':
        # print("I am the id of the chosen picture :", picture_id)
        db = get_db()
        pictures = db.execute("SELECT title, filename, upload_date, description, categories.name \
                               from pictures inner join categories \
                               on category_id = categories.id \
                               where pictures.id = (?)", [picture_id])
        # print(pictures)
        comments = db.execute("SELECT comment, comment_date \
                               from comments inner join pictures \
                               on picture_id = pictures.id \
                               where pictures.id = (?) \
                               order by comment_date desc", [picture_id])
        # print(comments)
        return render_template("picture.html",
                                all_pictures=pictures,
                                all_comments=comments)
    # print("not picture_id")
    return redirect("/picture/" + picture_id)

if __name__ == "__main__":
    app.run(debug=True)


import json
import sqlite3
from urllib import response
from fastapi.testclient import TestClient
import time
import pytest

from main import app, conn, c
from models import AtualizarFilme, AtualizarPlaneta, Filme, Planeta, Excluido, InserirPlaneta

client = TestClient(app)

# def test_create_schema():
#     c.executescript("""

# BEGIN TRANSACTION;
# DROP TABLE IF EXISTS "Filme";
# CREATE TABLE IF NOT EXISTS "Filme" (
# 	"id"	INTEGER NOT NULL,
# 	"Nome"	TEXT NOT NULL,
# 	"Data_de_lancamento"	TEXT NOT NULL,
# 	"Excluido"	INTEGER NOT NULL,
# 	PRIMARY KEY("id")
# );
# DROP TABLE IF EXISTS "Planeta";
# CREATE TABLE IF NOT EXISTS "Planeta" (
# 	"id"	INTEGER NOT NULL,
# 	"Nome"	TEXT NOT NULL,
# 	"Clima"	TEXT NOT NULL,
# 	"Diametro"	INTEGER NOT NULL,
# 	"Populacao"	INTEGER NOT NULL,
# 	"Excluido"	INTEGER NOT NULL,
# 	PRIMARY KEY("id")
# );
# DROP TABLE IF EXISTS "Planeta_Apareceu_Filme";
# CREATE TABLE IF NOT EXISTS "Planeta_Apareceu_Filme" (
# 	"id"	INTEGER NOT NULL UNIQUE,
# 	"PlanetaID"	INTEGER NOT NULL,
# 	"FilmeID"	INTEGER NOT NULL,
# 	"Excluido"	INTEGER NOT NULL,
# 	PRIMARY KEY("id" AUTOINCREMENT)
# );
# INSERT INTO "Filme" VALUES (1,'A morte do jedi','2020-04-23 10:20:30.400000+02:30',0);
# INSERT INTO "Filme" VALUES (2,'O jedi não morreu','2021-04-23 10:20:30.400000+02:30',0);
# INSERT INTO "Filme" VALUES (3,'O jedi nunca morreu','1970-01-01 00:33:41+00:00',0);
# INSERT INTO "Filme" VALUES (4,'Ou será que morreu?','1970-01-01 00:33:41+00:00',0);
# INSERT INTO "Filme" VALUES (5,'Não morreu, eu sabia!','2032-04-23 10:20:30.400000+02:30',0);
# INSERT INTO "Planeta" VALUES (1,'Marte','vento',55,66,0);
# INSERT INTO "Planeta" VALUES (2,'Marte 2','vento',10000,564612,0);
# INSERT INTO "Planeta" VALUES (3,'Planeta Voador','string',787878,152314856,0);
# INSERT INTO "Planeta" VALUES (5,'Nao lembro','murky',5489645,5164,0);
# INSERT INTO "Planeta" VALUES (6,'Planetoide','string',48654,1,1);
# INSERT INTO "Planeta_Apareceu_Filme" VALUES (1,1,1,0);
# INSERT INTO "Planeta_Apareceu_Filme" VALUES (2,1,2,0);
# INSERT INTO "Planeta_Apareceu_Filme" VALUES (3,2,2,0);
# INSERT INTO "Planeta_Apareceu_Filme" VALUES (4,6,1,1);
# INSERT INTO "Planeta_Apareceu_Filme" VALUES (5,6,2,1);
# INSERT INTO "Planeta_Apareceu_Filme" VALUES (10,6,3,1);
# INSERT INTO "Planeta_Apareceu_Filme" VALUES (11,6,4,1);
# INSERT INTO "Planeta_Apareceu_Filme" VALUES (12,6,5,1);
# COMMIT;

# """)

def test_read_root():
    response = client.get('/')
    assert response.status_code == 200
    assert response.json() == {'Hello,': 'World!'}

def test_read_planets_error_without_bool():
    response = client.get('/api/v1/planets')
    #Sem ?show_deleted=true
    assert response.status_code == 422

def test_read_planets_deleted_true():
    response = client.get('/api/v1/planets?show_deleted=true')
    assert response.status_code == 200
    print(type(response.json()))
#     assert response.json() == [
#   {
#     "id": 1,
#     "Nome": "Marte",
#     "Clima": "vento",
#     "Diametro": 55,
#     "Populacao": 66,
#     "Excluido": 0,
#     "Filmes_em_que_apareceu": [
#       1,
#       2
#     ]
#   },
#   {
#     "id": 2,
#     "Nome": "Marte",
#     "Clima": "vento",
#     "Diametro": 55,
#     "Populacao": 66,
#     "Excluido": 0,
#     "Filmes_em_que_apareceu": [
#       2
#     ]
#   },
#   {
#     "id": 3,
#     "Nome": "sexomaluco",
#     "Clima": "string",
#     "Diametro": 0,
#     "Populacao": 0,
#     "Excluido": 1,
#     "Filmes_em_que_apareceu": []
#   },
#   {
#     "id": 5,
#     "Nome": "string",
#     "Clima": "murky",
#     "Diametro": 0,
#     "Populacao": 0,
#     "Excluido": 1,
#     "Filmes_em_que_apareceu": []
#   },
#   {
#     "id": 6,
#     "Nome": "string",
#     "Clima": "string",
#     "Diametro": 0,
#     "Populacao": 0,
#     "Excluido": 1,
#     "Filmes_em_que_apareceu": [
#       1,
#       2,
#       3,
#       4,
#       5
#     ]
#   }
# ]

def test_read_planets_deleted_false():
    response = client.get('/api/v1/planets?show_deleted=false')
    assert response.status_code == 200

def test_read_planet():
    response = client.get('/api/v1/planets/1')
    assert response.status_code == 200
    assert response.json() == {
                            "id": 1,
                            "Nome": "Marte",
                            "Clima": "vento",
                            "Diametro": 55,
                            "Populacao": 66,
                            "Excluido": 0,
                            "Filmes_em_que_apareceu": [
                                1,
                                2
                            ]
                            }

def test_create_planet_movie_doesnt_exist():
    json={
            "id": 61,
            "Nome": "string",
            "Diametro": 0,
            "Populacao": 0,
            "FilmesID": [
            0
            ],
            "Excluido": 1
        }
    response = client.post('/api/v1/planets',
    json=json
    )
    assert response.status_code == 400
    #assert response.json() == {"detail": 'Pelo menos um dos filmes inseridos não existe',}

# def test_create_planet():
#     json={
#             "id": 44,
#             "Nome": "teste",
#             "Diametro": 0,
#             "Populacao": 0,
#             "FilmesID": [
#             1
#             ],
#             "Excluido": 0
#         }
#     response = client.post('/api/v1/planets',
#     json=json
#     )
#     assert response.status_code == 200


import keras.backend as K
import tensorflow as tf
import cv2
import imageio
import numpy as np

def square_sum(x):
    return K.sum(K.square(x), axis=-1, keepdims=True)

def euclSq(x):
    x, y = x
    x = K.batch_flatten(x)
    y = K.batch_flatten(y)
    return square_sum(x - y)

def l2_normalize(x):
    inv_sqrt = 1. / K.sqrt(K.maximum(square_sum(x), 1e-6))
    return x * inv_sqrt

def gram_matrix(x):
    filters = x.shape[3]
    size = x.shape[1]
    V = K.reshape(x, (-1, size * size, 1, filters))
    V = K.permute_dimensions(V, (0, 3, 2, 1))
    VT = K.permute_dimensions(V, (0, 2, 1, 3))
    return K.sum(V * VT, axis=3)

def triplet_loss(x):
    return K.maximum(x[0] - x[1] + 1, 0)

def gram(x):
    m, n = map(int, x.shape[2:])
    G = gram_matrix(x)
    return G / (4 * m**2 * n**2)

def get_image(filepath):
    with open(filepath, 'rb') as f:
        img = imageio.imread(f)
    img = crop_resize(img)
    return np.clip(img / 255, 0, 1)

def crop_resize(img):
    height, width = img.shape[:2]

    if height > width:
        center = height // 2
        up = center - width // 2
        down = center + width // 2
        img = img[up:down, :, :]
    elif height < width:
        center = width // 2
        left = center - height // 2
        right = center + height // 2
        img = img[:, left:right, :]
    img = cv2.resize(img, (256, 256), cv2.INTER_LANCZOS4)
    return img


from mpl_toolkits.mplot3d import axes3d

import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
import csv

from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from mpl_toolkits.mplot3d import Axes3D

import plotly.graph_objects as go
import plotly.express as px

import publico as func

pd.options.mode.chained_assignment = None  # default='warn'

from dateutil import parser

def MediaFileRede(res_select, interval_time=5):

    res_select.drop_duplicates(subset=None, keep="first", inplace=True)

    # cria campos
    res_select['Timer2']  =  0
    res_select['Media2']  =  0.0

    velo_total = 0.0
    count=0
    timer_atual = 0.0
    timer_ant = 0.0
    elapset_atual= 0.0
    elapset_cumulativo = 0.0

    count_timer=interval_time

    for index, row in res_select.iterrows():

        timer_atual  = row['Tempo']

        if (timer_ant!=0.0):
            elapset_atual = float(row['Tempo']) - float(timer_ant)

            # print(abs(elapset_atual))

            elapset_cumulativo+=float(elapset_atual)

            if ((elapset_cumulativo >= interval_time)):
                # print('Chegou')
                # break
                media_velo = velo_total / count
                res_select.at[index,"Media2"] = media_velo
                res_select.at[index,"Timer2"] = count_timer

                elapset_cumulativo=0.0
                timer_ant = 0.0
                velo_total=0.0
                media_velo=0.0
                count=0

                count_timer+=interval_time

        if (timer_atual != timer_ant):
            timer_ant = timer_atual

        velo_total = velo_total + row['Download']
        count+=1

    # remove zeros
    res_select = res_select[(res_select['Timer2']!=0) & (res_select['Timer2']<=280) & (res_select['Media2']<300) ]

    return res_select

EXP="70"

print("Loading Dataframe...")
# BASELINE GERAL ***************************************************
df1 = pd.read_csv("../repositorio/" + EXP + "/REDE_GERAL.csv")

df1['Download']  =  df1['Download'].astype(float)
df1['Upload']  =  df1['Upload'].astype(float)
df1['Tempo']  =  df1['Tempo'].astype(float)
df1['Source'] = "BASELINE"

# df1_filtro = df1.loc[(df1['Bytes'] > 0)]
df1_select = df1[['Download', 'Source', 'Tempo']]
df1_select = MediaFileRede(df1_select)

# *************************************************************************

# BASELINE 1TO 2 **********************************************************
df2 = pd.read_csv("../repositorio/" + EXP + "/REDE_BASELINE_1TO2.csv")

df2['Download']  =  df2['Download'].astype(float)
df2['Upload']  =  df2['Upload'].astype(float)
# df2['Duracao']  =  df2['Duracao'].astype(float)
df2['Tempo']  =  df2['Tempo'].astype(float)
# df2['Bytes']  =  df2['Bytes'].astype(float)
df2['Source'] = "1TO2"

# df4_filtro = 7df4.loc[(df4['Bytes'] > 0)]
df2_select = df2[['Download', 'Source', 'Tempo']]
df2_select = MediaFileRede(df2_select)
#********************************************************************
print("Loading Dataframe...")

# BASELINE RANDOM **********************************************************
df3 = pd.read_csv("../repositorio/" + EXP + "/REDE_BASELINE_RANDOM.csv")

df3['Download']  =  df3['Download'].astype(float)
df3['Upload']  =  df3['Upload'].astype(float)
# df3['Duracao']  =  df3['Duracao'].astype(float)
df3['Tempo']  =  df3['Tempo'].astype(float)
# df3['Bytes']  =  df3['Bytes'].astype(float)
df3['Source'] = "RAND"

# df4_filtro = df4.loc[(df4['Bytes'] > 0)]
df3_select = df3[['Download', 'Source', 'Tempo']]
df3_select = MediaFileRede(df3_select)
#********************************************************************
print("Loading Dataframe...")

# BASELINE THRESHOLD **********************************************************
df4 = pd.read_csv("../repositorio/" + EXP + "/REDE_BASELINE_THRESHOLD.csv")

df4['Download']  =  df4['Download'].astype(float)
df4['Upload']  =  df4['Upload'].astype(float)
# df4['Duracao']  =  df4['Duracao'].astype(float)
df4['Tempo']  =  df4['Tempo'].astype(float)
# df4['Bytes']  =  df4['Bytes'].astype(float)
df4['Source'] = "LIM-5"

# df4_filtro = df4.loc[(df4['Bytes'] > 0)]
df4_select = df4[['Download', 'Source', 'Tempo']]
df4_select = MediaFileRede(df4_select)
#********************************************************************
print("Loading Dataframe...")

# DBSCAN **********************************************************
df5 = pd.read_csv("../repositorio/" + EXP + "/REDE_DBSCAN.csv")

df5['Download']  =  df5['Download'].astype(float)
df5['Upload']  =  df5['Upload'].astype(float)
df5['Tempo']  =  df5['Tempo'].astype(float)
df5['Source'] = "DBSCAN"

# df1_filtro = df1.loc[(df1['Bytes'] > 0)]
df5_select =df5[['Download', 'Source', 'Tempo']]
df5_select = MediaFileRede(df5_select)

#********************************************************************

# # # DBSCAN FILTER  **********************************************************
# # df6 = pd.read_csv("../repositorio/" + EXP + "/REDE_DBSCAN_FILTER.csv")

# # df6['Download']  =  df6['Download'].astype(float)
# # df6['Upload']  =  df6['Upload'].astype(float)
# # df6['Duracao']  =  df6['Duracao'].astype(float)
# # df6['STime']  =  df6['STime'].astype(float)
# # df6['Bytes']  =  df6['Bytes'].astype(float)
# # df6['Source'] = "DBSCAN - FILTER"

# # df6_filtro = df6.loc[(df6['Bytes'] > 0)]
# # df6_select = df6_filtro[['Upload','Bytes','Source', 'STime','Duracao']]
# # df6_select = MediaFileRede(df6_select)
# # #********************************************************************

# XMEANS **********************************************************
df7 = pd.read_csv("../repositorio/" + EXP + "/REDE_XMEANS.csv")

df7['Download']  =  df7['Download'].astype(float)
df7['Upload']  =  df7['Upload'].astype(float)
# df7['Duracao']  =  df7['Duracao'].astype(float)
df7['Tempo']  =  df7['Tempo'].astype(float)
# df7['Bytes']  =  df7['Bytes'].astype(float)
df7['Source'] = "XMEANS"

# df1_filtro = df1.loc[(df1['Bytes'] > 0)]
df7_select =df7[['Download', 'Source', 'Tempo']]
df7_select = MediaFileRede(df7_select)
#********************************************************************

print("Loading Chart...")

# res = pd.concat([df1_select,df5_select,df7_select], sort=False)
# res = pd.concat([df1_select,df2_select,df3_select,df4_select, df5_select,df7_select], sort=False)
res = pd.concat([df1_select,df2_select,df3_select,df4_select], sort=False)

fig = px.line(res.reset_index(), x="Timer2", y="Media2", color="Source", title='Network Traffic').for_each_trace(lambda t: t.update(name=t.name.replace("Source=","")))

# https://plotly.com/python/axes/
# https://plotly.com/python/line-charts/

# fig.update_layout(
#     # title = "AnaliseAlgorithms ",
#     yaxis = dict(
#     #   range=[0,9],
#     #   tick0=0, dtick=2.5,
#       title_text='Upload Rate',
#     ),
#     xaxis = dict(
#       title_text='Normalized Simulation Time (i)',
#     ),
# )

fig.update_layout(
    # title = "AnaliseAlgorithms ",
    yaxis = dict(
    #   # range=[0,9],
    #   tick0=0, dtick=5,
      title_text='Network Traffic',
    ),
    font=dict(size=16),
    xaxis = dict(
      title_text='Normalized Simulation Time (t)',
    ),
    # plot_bgcolor='rgba(0,1,0,0)' # 76 64=todos
    legend=dict(
      x=0.76,
      y=1.1,
      font=dict(size=16),
      orientation='h'
      ),
      # annotations=[dict(
      #   xref='paper',
      #   yref='paper',
      # )
      # ]
)

fig.show()


import math

n = int(input())
for _ in range(n):
  line = input()
  k = int(math.sqrt(len(line)))
  chunks = [line[i:i+k] for i in range(0, len(line), k)]
  s = ""
  for j in reversed(range(k)):
    for chunk in chunks:
      s += chunk[j]
  print(s)


import asyncio
import traceback
from neptune_py.skeleton.skeleton import NeptuneServiceSkeleton
from neptune_py.skeleton.messager import (
    NeptuneWriterBaseAbstract, NeptuneMessageType
)

import struct
import collections

class TLV:
    _format = '!HI'
    meta_size = struct.calcsize(_format)
    tlv = collections.namedtuple('tlv_tuple', 'tag length')
    MagicTag = 13

    @classmethod
    def pack(cls, tag, data):
        return struct.pack(cls._format, tag, len(data)) + data

    @classmethod
    def unpack(cls, data):
        if len(data) < cls.meta_size:
            return None
        tag, length = struct.unpack(cls._format, data)
        return cls.tlv(tag=tag, length=length)

class TlvWriter(NeptuneWriterBaseAbstract):
    def __init__(self, writer):
        super().__init__()
        self.writer = writer
        self.closed = False

    def write(self, message):
        self.writer.write(TLV.pack(TLV.MagicTag, message))

    def close(self):
        if self.closed:
            return
        self.closed = True

        if self.writer.can_write_eof():
            self.writer.write_eof()
        else:
            self.writer.close()

class NeptuneTlvBase(NeptuneServiceSkeleton):

    def __init__(self, host, port, messager_manager, name=None):
        super().__init__(name)
        self.host = host
        self.port = port
        self.messager_manager = messager_manager
        self.messager_id = 0

    async def connection_handler(self, reader, writer):
        peername = writer.get_extra_info("peername")
        self.get_logger().debug(f'{peername} connected')

        messager_id = self.messager_id
        tlv_writer = TlvWriter(writer)
        self.messager_manager.on_connected(messager_id, tlv_writer)
        self.messager_id += 1

        try:
            while True:
                meta = await reader.readexactly(TLV.meta_size)
                tlv = TLV.unpack(meta)
                # print(tlv)
                data = await reader.readexactly(tlv.length)
                self.messager_manager.on_message(messager_id, data)
        except asyncio.IncompleteReadError as e:
            if e.partial:
                # empty data indicates peer closed the connection, otherwise the data
                # is illegal.
                self.get_logger().debug(f'{peername} illegal data')
        except Exception as e:
            self.get_logger().error(traceback.format_exc())
        finally:
            self.get_logger().debug(f'{peername} closed')
            self.messager_manager.on_disconnected(messager_id)
            writer.close()
            await writer.wait_closed()

    def init(self):
        self.get_logger().debug(f'init {self.__class__.__name__} {self.name}')

    async def finish(self):
        self.get_logger().debug(f'stopping {self.__class__.__name__} {self.name}...')

class NeptuneTlvService(NeptuneTlvBase):
    """
    tlv message server
    """
    async def logic(self):
        # https://docs.python.org/3.6/library/asyncio-protocol.html
        # 'Changed in version 3.6: The socket option TCP_NODELAY is now set by default.'
        server = await asyncio.start_server(self.connection_handler, self.host, self.port)
        async with server:
            self.get_logger().debug(f'NeptuneTlvService {self.name} starts to server')
            await server.serve_forever()

class NeptuneTlvClient(NeptuneTlvBase):
    """
    tlv message client
    """
    async def logic(self):
        reader, writer = await asyncio.open_connection(self.host, self.port)
        await self.connection_handler(reader, writer)


# 9 - Crie uma lista contendo 5 nomes e adicione esta lista dentro da lista gerada no exercício 4

import random

lista1 = ['Maria', 'João', 'Marcio', 'Marta', 'Ana']
lista2 = []
contador = 0

while contador < 10:
    n = random.randint(10, 1580)
    lista2.append(n)
    contador += 1

lista2.append(lista1)
print(lista2)


import socket
from time import sleep

TCP_IP = '192.168.1.103'
TCP_PORT = 5005
BUFFER_SIZE = 40  # Normally 1024, but we want fast response

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)

c1 = ""
c2 = ""

print("INICIA SERVER")

conn, addr = s.accept()
print('Connection address:', addr)
while 1:
    print("RECIBE")
    data = conn.recv(BUFFER_SIZE)
    longitud = len(data)
    if longitud > 0:
        c1 = data[0:5]
    if longitud > 5:
        c2 = data[5:10]

    print("received data:", data)

    if c1 == b'B_ACE' or c2 == b'B_ACE':
        print("Hay que acelerar!")

    if c1 == b'B_RET' or c2 == b'B_RET':
        print("Hay que frenar!")

    if c1 == b'EXIT':
        print("CIERRA CONEXION")
        conn.close()
        break
    else:
        c1 = ""
        c2 = ""

        # data = bytes(str(BUFFER_SIZE))
        conn.send(data)  # echo
        sleep(3)


from utility import classifier as cls
import numpy as np

import random

# action space 中的最后一个动作为终止

# 自己构建的环境
class MyEnv:
    def __init__(self, state_size, max, data, classifier):
        self.state_size = state_size
        self.action_size = state_size + 1  # 包含一个终止动作
        self.max = max  # 最多选取max个特征，超出直接终止
        self.data = data
        self.classifier = classifier
        self.dict = {}

        self.reset()

    def random_action(self):
        while True:
            action = random.randint(0, self.action_size - 1)
            if action == self.action_size - 1 or self.state[action] == 0:
                break
        return action

    def step(self, action_index):
        if action_index == self.action_size - 1:  # 终止
            self.done = True
        else:
            self.state[action_index] = 1
            self.count += 1
            if self.count == self.max_count:  # 已经到达选择数量上线
                self.done = True

            # reward 默认为0
            # if current_count>self.max:
            #     reward = self.max - current_count
            # else:
        reward = self.get_reward()
        if reward == -1:
            # print("no flag")
            reward = cls.get_reward(self.state, self.classifier, self.data, self.max_count)
            self.add_dict(reward)

        # reward = random.random()*100
        return np.array(self.state), reward, self.done

    def reset(self):
        self.state = [0 for _ in range(self.state_size)]
        self.max_count = min(self.max, self.state_size)  # 最大特征数
        self.count = 0  # 当前已经选取的特征数
        self.done = False
        return np.array(self.state)

    def render(self):
        print("This is me: {}".format(self.state))

    def get_reward(self):
        temp = [str(x) for x in self.state]
        temp = '.'.join(temp)
        reward = self.dict.get(temp, -1)
        return reward

    def add_dict(self, reward):
        temp = [str(x) for x in self.state]
        temp = '.'.join(temp)
        self.dict[temp] = reward


from pyhpecfm import fabric
from lib.actions import HpecfmBaseAction

class fabricIpLookup(HpecfmBaseAction):
    def run(self):
        cfm_fabrics = fabric.get_fabric_ip_networks(self.client)
        if isinstance(cfm_fabrics, list):
            fabric_data = []
            # Loop through cfm_fabrics and process IPZ
            for fabip in cfm_fabrics:
                desc = fabip['description']
                if desc == '':
                    desc = 'HPE Composable Fabric'
                out ={
                        'u_desc':desc,
                        'u_fabu_uid':fabip['fabric_uuid'],
                        'u_name':fabip['name'],
                        'u_mode':fabip['mode'],
                        'u_sub_address':fabip['subnet']['address'],
                        'u_mask_prefix':fabip['subnet']['prefix_length']
                      }
                fabric_data.append(out)

            return (True, fabric_data)
        return (False, switches)


from pydoc import tempfilepager
from PIL import Image
import numpy
import cv2

slot_1_box = (905, 215, 930, 235)
slot_2_box = (933, 215, 958, 235)
slot_3_box = (961, 215, 986, 235)
slots_poss = (slot_1_box, slot_2_box, slot_3_box)

def get_crop(_source, _box):
    return Image.open(_source).convert('RGB').crop(_box)  # .save(tmp_path)

def calculate(image1, image2):
    image1 = cv2.cvtColor(numpy.asarray(image1), cv2.COLOR_RGB2BGR)
    image2 = cv2.cvtColor(numpy.asarray(image2), cv2.COLOR_RGB2BGR)
    hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])
    hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0])
    # 计算直方图的重合度
    degree = 0
    for i in range(len(hist1)):
        if hist1[i] != hist2[i]:
            degree = degree + \
                (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i]))
        else:
            degree = degree + 1
    degree = degree / len(hist1)
    return degree

def classify_hist_with_split(image1, image2, size=(256, 256)):
    # image1 = Image.open(image1)
    image2 = Image.open(image2)
    # 将图像resize后，分离为RGB三个通道，再计算每个通道的相似值
    image1 = cv2.cvtColor(numpy.asarray(image1), cv2.COLOR_RGB2BGR)
    image2 = cv2.cvtColor(numpy.asarray(image2), cv2.COLOR_RGB2BGR)
    image1 = cv2.resize(image1, size)
    image2 = cv2.resize(image2, size)
    sub_image1 = cv2.split(image1)
    sub_image2 = cv2.split(image2)
    sub_data = 0
    for im1, im2 in zip(sub_image1, sub_image2):
        sub_data += calculate(im1, im2)
    sub_data = sub_data / 3

    return sub_data

class analyzer():
    def __init__(self,sourcepath,slotpath):
        self.sourcepath = sourcepath
        self.slotpath = slotpath
        pass

    def analyze(self, img):

        source_path = self.sourcepath + img

        res = [0 for _ in range(len(slots_poss))]

        for i in range(len(slots_poss)):
            img1_path = get_crop(source_path, slots_poss[i])
            for level in range(4):
                img2_path = self.slotpath + 'slot_lv' + str(level + 1)+'.png'
                result = classify_hist_with_split(img1_path, img2_path)
                if result[0] > 0.8:
                    res[i] = (level+1)
                # print(img + str(level) + "相似度为：" + "%.2f%%" % (result * 100))
        # print(img, res)

        # Image.open(source_path).crop((905, 215, 986, 235)
                                    #  ).save(tmppath + str(res) + "-" + img)

        return res


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 技术支持：dwz.cn/qkEfX1u0 项目实战讨论QQ群6089740 144081101
# CreateDate: 2019-12-29

def fib2(n):
    if n < 2:  # base case
        return n
    return fib2(n - 2) + fib2(n - 1)  # recursive case

if __name__ == "__main__":
    print(fib2(5))
    print(fib2(10))