import torch
from torch import nn
from .operation import Operation, OP_COV_KRON, OP_GRAM_HADAMARD
from .operation import BASIC_OPS, OP_COV_UNIT_WISE, OP_COV_UNIT_WISE_INV, OP_COV_DIAG, OP_COV_DIAG_INV, OP_GRAM_DIRECT, OP_BATCH_GRADS

class LayerNorm(Operation):
    """
    module.weight: normalized_shape
    module.bias: normalized_shape

    Argument shapes
    in_data: n x normalized_shape
    out_grads: n x normalized_shape

    normalized_shape: f[0] x f[1] x ... x f[-1]
    """
    supported_operations = set(
        BASIC_OPS + [OP_COV_UNIT_WISE, OP_COV_UNIT_WISE_INV, OP_COV_DIAG, OP_COV_DIAG_INV, OP_GRAM_DIRECT, OP_BATCH_GRADS])

    @staticmethod
    def preprocess_in_data(module, in_data, out_data):
        # restore normalized input
        in_data_norm = (out_data - module.bias).div(module.weight)
        in_data = in_data_norm
        # n x * x norm_shape -> n x norm_shape
        norm_shape_len = len(module.weight.shape)
        in_data_shape_len = len(in_data.shape)
        if norm_shape_len < in_data_shape_len - 1:
            in_data = in_data.flatten(end_dim=-norm_shape_len - 1)
        return in_data

    @staticmethod
    def preprocess_out_grads(module, out_grads):
        # n x * x norm_shape -> n x norm_shape
        norm_shape_len = len(module.weight.shape)
        out_grads_shape_len = len(out_grads.shape)
        if norm_shape_len < out_grads_shape_len - 1:
            out_grads = out_grads.flatten(end_dim=-norm_shape_len - 1)
        return out_grads

    @staticmethod
    def batch_grads_weight(
        module: nn.Module, in_data: torch.Tensor, out_grads: torch.Tensor
    ):
        return in_data.mul(out_grads)  # n x normalized_shape

    @staticmethod
    def batch_grads_bias(module, out_grads):
        return out_grads

    @staticmethod
    def grad_weight(module: nn.Module, in_data: torch.Tensor, out_grads: torch.Tensor):
        return in_data.mul(out_grads).sum(dim=0)

    @staticmethod
    def grad_bias(module: nn.Module, out_grads: torch.Tensor):
        return out_grads.sum(dim=0)

    @staticmethod
    def cov_diag_weight(module, in_data, out_grads):
        grads = in_data.mul(out_grads)
        return grads.mul(grads).sum(dim=0)  # normalized_shape

    @staticmethod
    def cov_diag_bias(module, out_grads):
        return out_grads.mul(out_grads).sum(dim=0)  # normalized_shape

    @staticmethod
    def cov_unit_wise(module, in_data, out_grads):
        n_features = in_data.flatten(start_dim=1).shape[1]  # (f[0] x f[1] x ... x f[-1])
        grads_w = in_data.mul(out_grads)  # n x normalized_shape
        grads_b = out_grads  # n x normalized_shape
        cov_ww = (grads_w ** 2).sum(0).flatten()  # n_features x 1
        cov_bb = (grads_b ** 2).sum(0).flatten()  # n_features x 1
        cov_wb = (grads_w * grads_b).sum(0).flatten()  # n_features x 1
        blocks = torch.vstack([cov_ww, cov_wb, cov_wb, cov_bb]).reshape(2, 2, n_features).transpose(0, 2)
        return blocks  # n_features x 2 x 2

    @staticmethod
    def cov_kron_A(module, in_data):
        raise ValueError(
            f'{OP_COV_KRON} operation is not supported in LayerNorm.'
        )

    @staticmethod
    def cov_kron_B(module, out_grads):
        raise ValueError(
            f'{OP_COV_KRON} operation is not supported in LayerNorm.'
        )

    @staticmethod
    def gram_A(module, in_data1, in_data2):
        raise ValueError(
            f'{OP_GRAM_HADAMARD} operation is not supported in LayerNorm.'
        )

    @staticmethod
    def gram_B(module, out_grads1, out_grads2):
        raise ValueError(
            f'{OP_GRAM_HADAMARD} operation is not supported in LayerNorm.'
        )


import torch
import gym
import matplotlib.pyplot as plt
import torch.nn.functional as F
import copy
from tqdm import tqdm
import numpy as np
import torch
import collections
import random

num_episodes = 500
hidden_dim = 128
gamma = 0.98
lmbda = 0.95
critic_lr = 1e-2
kl_constraint = 0.0005
alpha = 0.5
device = torch.device("cuda") if torch.cuda.is_available() else torch.device(
    "cpu")

from torch.utils.tensorboard import SummaryWriter

writer = SummaryWriter("D:\PycharmProjects\Franka\experiment\Acrobot-v1\Trpo")

def compute_advantage(gamma, lmbda, td_delta):
    td_delta = td_delta.detach().numpy()
    advantage_list = []
    advantage = 0.0
    for delta in td_delta[::-1]:
        advantage = gamma * lmbda * advantage + delta
        advantage_list.append(advantage)
    advantage_list.reverse()
    return torch.tensor(advantage_list, dtype=torch.float)

class PolicyNet(torch.nn.Module):
    def __init__(self, state_dim, hidden_dim, action_dim):
        super(PolicyNet, self).__init__()
        self.fc1 = torch.nn.Linear(state_dim, hidden_dim)
        self.fc2 = torch.nn.Linear(hidden_dim, action_dim)

    def forward(self, x):
        x = F.relu(self.fc1(x))
        return F.softmax(self.fc2(x), dim=1)

class ValueNet(torch.nn.Module):
    def __init__(self, state_dim, hidden_dim):
        super(ValueNet, self).__init__()
        self.fc1 = torch.nn.Linear(state_dim, hidden_dim)
        self.fc2 = torch.nn.Linear(hidden_dim, 1)

    def forward(self, x):
        x = F.relu(self.fc1(x))
        return self.fc2(x)

class TRPO:
    """ TRPO算法 """

    def __init__(self, hidden_dim, state_space, action_space, lmbda,
                 kl_constraint, alpha, critic_lr, gamma, device):
        state_dim = state_space.shape[0]
        action_dim = action_space.n
        # 策略网络参数不需要优化器更新
        self.actor = PolicyNet(state_dim, hidden_dim, action_dim).to(device)
        self.critic = ValueNet(state_dim, hidden_dim).to(device)
        self.critic_optimizer = torch.optim.Adam(self.critic.parameters(),
                                                 lr=critic_lr)
        self.gamma = gamma
        self.lmbda = lmbda  # GAE参数
        self.kl_constraint = kl_constraint  # KL距离最大限制
        self.alpha = alpha  # 线性搜索参数
        self.device = device

    def take_action(self, state):
        state = torch.tensor([state], dtype=torch.float).to(self.device)
        probs = self.actor(state)
        action_dist = torch.distributions.Categorical(probs)
        action = action_dist.sample()
        return action.item()

    def hessian_matrix_vector_product(self, states, old_action_dists, vector):
        # 计算黑塞矩阵和一个向量的乘积
        new_action_dists = torch.distributions.Categorical(self.actor(states))
        kl = torch.mean(
            torch.distributions.kl.kl_divergence(old_action_dists,
                                                 new_action_dists))  # 计算平均KL距离
        kl_grad = torch.autograd.grad(kl,
                                      self.actor.parameters(),
                                      create_graph=True)
        kl_grad_vector = torch.cat([grad.view(-1) for grad in kl_grad])
        # KL距离的梯度先和向量进行点积运算
        kl_grad_vector_product = torch.dot(kl_grad_vector, vector)
        grad2 = torch.autograd.grad(kl_grad_vector_product,
                                    self.actor.parameters())
        grad2_vector = torch.cat([grad.view(-1) for grad in grad2])
        return grad2_vector

    def conjugate_gradient(self, grad, states, old_action_dists):  # 共轭梯度法求解方程
        x = torch.zeros_like(grad)
        r = grad.clone()
        p = grad.clone()
        rdotr = torch.dot(r, r)
        for i in range(10):  # 共轭梯度主循环
            Hp = self.hessian_matrix_vector_product(states, old_action_dists,
                                                    p)
            alpha = rdotr / torch.dot(p, Hp)
            x += alpha * p
            r -= alpha * Hp
            new_rdotr = torch.dot(r, r)
            if new_rdotr < 1e-10:
                break
            beta = new_rdotr / rdotr
            p = r + beta * p
            rdotr = new_rdotr
        return x

    def compute_surrogate_obj(self, states, actions, advantage, old_log_probs,
                              actor):  # 计算策略目标
        log_probs = torch.log(actor(states).gather(1, actions))
        ratio = torch.exp(log_probs - old_log_probs)
        return torch.mean(ratio * advantage)

    def line_search(self, states, actions, advantage, old_log_probs,
                    old_action_dists, max_vec):  # 线性搜索
        old_para = torch.nn.utils.convert_parameters.parameters_to_vector(
            self.actor.parameters())
        old_obj = self.compute_surrogate_obj(states, actions, advantage,
                                             old_log_probs, self.actor)
        for i in range(15):  # 线性搜索主循环
            coef = self.alpha ** i
            new_para = old_para + coef * max_vec
            new_actor = copy.deepcopy(self.actor)
            torch.nn.utils.convert_parameters.vector_to_parameters(
                new_para, new_actor.parameters())
            new_action_dists = torch.distributions.Categorical(
                new_actor(states))
            kl_div = torch.mean(
                torch.distributions.kl.kl_divergence(old_action_dists,
                                                     new_action_dists))
            new_obj = self.compute_surrogate_obj(states, actions, advantage,
                                                 old_log_probs, new_actor)
            if new_obj > old_obj and kl_div < self.kl_constraint:
                return new_para
        return old_para

    def policy_learn(self, states, actions, old_action_dists, old_log_probs,
                     advantage):  # 更新策略函数
        surrogate_obj = self.compute_surrogate_obj(states, actions, advantage,
                                                   old_log_probs, self.actor)
        grads = torch.autograd.grad(surrogate_obj, self.actor.parameters())
        obj_grad = torch.cat([grad.view(-1) for grad in grads]).detach()
        # 用共轭梯度法计算x = H^(-1)g
        descent_direction = self.conjugate_gradient(obj_grad, states,
                                                    old_action_dists)

        Hd = self.hessian_matrix_vector_product(states, old_action_dists,
                                                descent_direction)
        max_coef = torch.sqrt(2 * self.kl_constraint /
                              (torch.dot(descent_direction, Hd) + 1e-8))
        new_para = self.line_search(states, actions, advantage, old_log_probs,
                                    old_action_dists,
                                    descent_direction * max_coef)  # 线性搜索
        torch.nn.utils.convert_parameters.vector_to_parameters(
            new_para, self.actor.parameters())  # 用线性搜索后的参数更新策略

    def update(self, transition_dict,i_episode, i):
        states = torch.tensor(transition_dict['states'],
                              dtype=torch.float).to(self.device)
        actions = torch.tensor(transition_dict['actions']).view(-1, 1).to(
            self.device)
        rewards = torch.tensor(transition_dict['rewards'],
                               dtype=torch.float).view(-1, 1).to(self.device)
        next_states = torch.tensor(transition_dict['next_states'],
                                   dtype=torch.float).to(self.device)
        dones = torch.tensor(transition_dict['dones'],
                             dtype=torch.float).view(-1, 1).to(self.device)
        td_target = rewards + self.gamma * self.critic(next_states) * (1 -
                                                                       dones)
        td_delta = td_target - self.critic(states)
        advantage = compute_advantage(self.gamma, self.lmbda,
                                      td_delta.cpu()).to(self.device)
        old_log_probs = torch.log(self.actor(states).gather(1,
                                                            actions)).detach()
        old_action_dists = torch.distributions.Categorical(
            self.actor(states).detach())
        critic_loss = torch.mean(
            F.mse_loss(self.critic(states), td_target.detach()))
        self.critic_optimizer.zero_grad()
        critic_loss.backward()
        self.critic_optimizer.step()  # 更新价值函数
        # 更新策略函数
        self.policy_learn(states, actions, old_action_dists, old_log_probs,
                          advantage)
        if (i_episode + 1) % 10 == 0:
            episode = (int)(num_episodes / 10 * i + i_episode + 1)
            writer.add_scalar("A2C-CriticLoss", critic_loss, episode)

class ReplayBuffer:
    def __init__(self, capacity):
        self.buffer = collections.deque(maxlen=capacity)

    def add(self, state, action, reward, next_state, done):
        self.buffer.append((state, action, reward, next_state, done))

    def sample(self, batch_size):
        transitions = random.sample(self.buffer, batch_size)
        state, action, reward, next_state, done = zip(*transitions)
        return np.array(state), action, reward, np.array(next_state), done

    def size(self):
        return len(self.buffer)

def moving_average(a, window_size):
    cumulative_sum = np.cumsum(np.insert(a, 0, 0))
    middle = (cumulative_sum[window_size:] - cumulative_sum[:-window_size]) / window_size
    r = np.arange(1, window_size - 1, 2)
    begin = np.cumsum(a[:window_size - 1])[::2] / r
    end = (np.cumsum(a[:-window_size:-1])[::2] / r)[::-1]
    return np.concatenate((begin, middle, end))

def train_on_policy_agent(env, agent, num_episodes):
    return_list = []
    for i in range(10):
        with tqdm(total=int(num_episodes / 10), desc='Iteration %d' % i) as pbar:
            for i_episode in range(int(num_episodes / 10)):
                episode_return = 0
                transition_dict = {'states': [], 'actions': [], 'next_states': [], 'rewards': [], 'dones': []}
                state = env.reset()
                done = False
                while not done:
                    action = agent.take_action(state)
                    next_state, reward, done, _ = env.step(action)
                    transition_dict['states'].append(state)
                    transition_dict['actions'].append(action)
                    transition_dict['next_states'].append(next_state)
                    transition_dict['rewards'].append(reward)
                    transition_dict['dones'].append(done)
                    state = next_state
                    episode_return += reward
                return_list.append(episode_return)
                agent.update(transition_dict,i_episode,i)
                if (i_episode + 1) % 10 == 0:
                    pbar.set_postfix({'episode': '%d' % (num_episodes / 10 * i + i_episode + 1),
                                      'return': '%.3f' % np.mean(return_list[-10:])})
                    writer.add_scalar('ten episodes average rewards', np.mean(return_list[-10:]),
                                      (int)(num_episodes / 10 * i + i_episode + 1))
                pbar.update(1)
    return return_list

def train_off_policy_agent(env, agent, num_episodes, replay_buffer, minimal_size, batch_size):
    return_list = []
    for i in range(10):
        with tqdm(total=int(num_episodes / 10), desc='Iteration %d' % i) as pbar:
            for i_episode in range(int(num_episodes / 10)):
                episode_return = 0
                state = env.reset()
                done = False
                while not done:
                    action = agent.take_action(state)
                    next_state, reward, done, _ = env.step(action)
                    replay_buffer.add(state, action, reward, next_state, done)
                    state = next_state
                    episode_return += reward
                    if replay_buffer.size() > minimal_size:
                        b_s, b_a, b_r, b_ns, b_d = replay_buffer.sample(batch_size)
                        transition_dict = {'states': b_s, 'actions': b_a, 'next_states': b_ns, 'rewards': b_r,
                                           'dones': b_d}
                        agent.update(transition_dict,i_episode,i)
                return_list.append(episode_return)
                if (i_episode + 1) % 10 == 0:
                    pbar.set_postfix({'episode': '%d' % (num_episodes / 10 * i + i_episode + 1),
                                      'return': '%.3f' % np.mean(return_list[-10:])})
                    writer.add_scalar('ten episodes average rewards', np.mean(return_list[-10:]),
                                      (int)(num_episodes / 10 * i + i_episode + 1))
                pbar.update(1)
    return return_list

def compute_advantage(gamma, lmbda, td_delta):
    td_delta = td_delta.detach().numpy()
    advantage_list = []
    advantage = 0.0
    for delta in td_delta[::-1]:
        advantage = gamma * lmbda * advantage + delta
        advantage_list.append(advantage)
    advantage_list.reverse()
    return torch.tensor(advantage_list, dtype=torch.float)

if __name__ == "__main__":
    env_name = 'Acrobot-v1'
    env = gym.make(env_name)
    env.seed(0)
    torch.manual_seed(0)
    agent = TRPO(hidden_dim, env.observation_space, env.action_space, lmbda,
                 kl_constraint, alpha, critic_lr, gamma, device)
    train_on_policy_agent(env, agent, num_episodes)


import os
import argparse
from getpass import getpass

import requests

def main():
    user = CommandLineParser().parse()
    API(user).parse()

class API:
    def __init__(self, user):
        self.target = "http://localhost:8080/"
        self.user = user
        self.header = self._create_header()

    def parse(self):
        projects = self._parse_projects()
        for project in projects:
            if project != "lifecycle":
                self._delete_project(project)
        users = self._parse_users()
        for user in users:
            if user != self.user:
                self._delete_user(user)

    def _parse_projects(self):
        projects = []
        localhost_project_dict = self._get_projects()
        for project in localhost_project_dict:
            projects.append(project["name"])
        return projects

    def _parse_users(self):
        users = []
        localhost_user_dict = self._get_users()
        for user in localhost_user_dict:
            users.append(user["email"])
        return users

    @staticmethod
    def _create_header():
        token = getpass("Token: ")
        return {"Authorization": "Bearer " + token}

    def _delete_project(self, project_name):
        response = requests.delete(f"{self.target}access/projects/{project_name}", headers=self.header)
        response.raise_for_status()

    def _delete_user(self, user_email):
        response = requests.delete(f"{self.target}access/users/{user_email}", headers=self.header)
        response.raise_for_status()

    def _get_projects(self):
        response = requests.get(f"{self.target}access/projects", headers=self.header).json()
        return response

    def _get_users(self):
        response = requests.get(f"{self.target}access/users", headers=self.header).json()
        return response

class CommandLineParser:
    def __init__(self):
        self.parser = CommandLineInterface()

    def parse(self):
        su = self._parse_su()
        return su

    def _parse_su(self):
        return self.parser.get_argument("user")

class CommandLineInterface:
    def __init__(self):
        parser = self._create_argument_parser()
        self.arguments = parser.parse_args()

    @staticmethod
    def _create_argument_parser():
        """
        Creation of the Command Line Arguments, and whenever they should be displayed as
        "required" or not.
        """
        parser = argparse.ArgumentParser(
            prog="Reset localhost projects and users",
            description="Utilitarian script for developers that want to reset their localhost instance to just Project "
                        "'Lifecycle' and a singular user if they supply -u. Does not affect Profiles."
        )
        required = parser.add_argument_group("Required arguments")
        required.add_argument(
            "-u",
            "--user",
            type=str,
            required=True,
            help="The single user that is registered on the localhost that needs to remain."
        )
        return parser

    def get_argument(self, argument_key):
        """
        Small getter function for CLI, including some error handling.
        """
        if argument_key in self.arguments:
            return getattr(self.arguments, argument_key)
        else:
            raise KeyError(f"Argument {argument_key} invalid, not found in CLI arguments.")

if __name__ == "__main__":
    main()


# LeetCode. Yandex Task List. 1493. Longest Subarray of 1's After Deleting One Element

'''

1493. Longest Subarray of 1's After Deleting One Element
Medium

Given a binary array nums, you should delete one element from it.

Return the size of the longest non-empty subarray containing only 1's in the resulting array. Return 0 if there is no such subarray.

Example 1:

Input: nums = [1,1,0,1]
Output: 3
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.

Example 2:

Input: nums = [0,1,1,1,0,1,1,0,1]
Output: 5
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].

Example 3:

Input: nums = [1,1,1]
Output: 2
Explanation: You must delete one element.

Example 4:

Input: nums = [1,1,0,0,1,1,1,0,1]
Output: 4

Example 5:

Input: nums = [0,0,0]
Output: 0

Constraints:

    1 <= nums.length <= 105
    nums[i] is either 0 or 1.

Accepted
30,851
Submissions
51,601

Related Topics
Math, Dynamic Programming, Sliding Window

Similar Questions
-

Hide Hint 1
Maintain a sliding window where there is at most one zero on it.

Intuition
There are only 3 cases that we need to take care of:
(1) only zeros in nums
(2) only ones in nums
(3) others

Complexity
Time O(N)
Space O(N)

Solution

def longestSubarray(self, nums: List[int]) -> int:
	# We get the group's key and length first, e.g. [0,1,1,1,0,1,1,0,1] -> [[0 , 1], [1, 3], [0, 1], [1, 2], [0, 1], [1, 1]]
	groupby = [ [k, len(list(i))] for k, i in itertools.groupby(nums)]

	if len(groupby) == 1:
		# case(1) only zeros in nums
		if groupby[0][0] == 0:
			return 0
		# case(2) only ones in nums
		elif groupby[0][0] == 1:
			return groupby[0][1] - 1
	else:
		# case(3) others
		res = max([times for i,times in groupby if i==1])
		for i in range(1, len(groupby)-1):
			# if both sides have ones and are separated by only 1 zero
			if groupby[i][0] == 0 and groupby[i][1] == 1:
				res = max(res, groupby[i-1][1] + groupby[i+1][1])
		return res

Similar Sliding Window Problems
1156. Swap For Longest Repeated Character Substring

'''

import math
import time
import collections
from typing import List
import numpy as np
import random as rnd
import itertools as it
from collections import defaultdict, Counter
import re
from functools import reduce
from functools import lru_cache
from bisect import bisect, bisect_left

class Solution:
    def longestSubarray(self, nums: List[int]) -> int:
        subArrSizesList = list()
        firstSubArrSize = 0
        secondSubArrSize = 0
        n = len(nums)
        if nums.count(0) == 0:
            return n-1
        for i in range(0, n):
            if nums[i] != 0:
                firstSubArrSize += 1
            elif nums[i] == 0:
                for j in range(i+1, n):
                    if nums[j] != 0:
                        secondSubArrSize += 1
                    else:
                        break
                subArrSizesList.append(firstSubArrSize + secondSubArrSize)
                firstSubArrSize = 0
                secondSubArrSize = 0
        return max(subArrSizesList)

if __name__ == "__main__":
    #
    start = time.perf_counter()
    solution = Solution()
    print(solution.twoSum(nums = [2 ,7 ,11 ,15], target = 9))
    end = time.perf_counter()
    print(f"test 1: {end - start:10.6f} sec")
    #
    start2 = time.perf_counter()
    print(solution.twoSum(nums = [3 ,2 ,4], target = 6))
    end2 = time.perf_counter()
    print(f"test 2: {end2 - start2:10.6f} sec")
    #
    start3 = time.perf_counter()
    print(solution.twoSum(nums = [3 ,3], target = 6))
    end3 = time.perf_counter()
    print(f"test 3: {end3 - start3:10.6f} sec")
    #
    start4 = time.perf_counter()
    print(solution.isIsomorphic("badc", "baba"))
    end4 = time.perf_counter()
    print(f"test 4: {end4 - start4:10.6f} sec")
    #
    start5 = time.perf_counter()
    print(solution.isIsomorphic("abab", "baba"))
    end5 = time.perf_counter()
    print(f"test 5: {end5 - start5:10.6f} sec")
    #
    start6 = time.perf_counter()
    print(solution.firstMissingPositive([1, 1]))
    end6 = time.perf_counter()
    print(f"test 6: {end6 - start6:10.6f} sec")


import pygame

#배경그림 설정값
screen = pygame.display.set_mode((700, 700))
bg_image1 = pygame.image.load("images/bg.jpg")
bg_image1 = pygame.transform.scale(bg_image1, (700,700))
bg_image2 = pygame.transform.flip(bg_image1, True, False)
bg_image3 = pygame.transform.flip(bg_image1, False, True) #처음그림 대칭시킨 그림
bg_image4 = pygame.transform.flip(bg_image1, True, True)

class Background:
    def __init__(self,screen):
        width, height = screen.get_size()
        self.image1 = bg_image1
        self.image2 = bg_image2
        self.image3 = bg_image3
        self.image4 = bg_image4

        self.pos1 = [0, 0]
        self.pos2 = [width, 0]
        self.pos3 = [0, height]
        self.pos4 = [width, height]
        self.pos5 = [-width, 0]
        self.pos6 = [0, -height]
        self.pos7 = [-width, -height]
        self.pos8 = [width, -height]
        self.pos9 = [-width, height]

        self.to = [0,0]
    #배경 이동
    def goto(self, x, y):
        self.to[0] += x
        self.to[1] += y

    #배경 이동속도 제어
    def update(self, dt, screen):
        width, height = screen.get_size()
        self.pos1[0] = (self.pos1[0] + dt * self.to[0] * 0.05)
        self.pos1[1] = (self.pos1[1] + dt * self.to[1] * 0.05)

        self.pos2[0] = (self.pos2[0] + dt * self.to[0] * 0.05)
        self.pos2[1] = (self.pos2[1] + dt * self.to[1] * 0.05)

        self.pos3[0] = (self.pos3[0] + dt * self.to[0] * 0.05)
        self.pos3[1] = (self.pos3[1] + dt * self.to[1] * 0.05)

        self.pos4[0] = (self.pos4[0] + dt * self.to[0] * 0.05)
        self.pos4[1] = (self.pos4[1] + dt * self.to[1] * 0.05)

        self.pos5[0] = (self.pos5[0] + dt * self.to[0] * 0.05)
        self.pos5[1] = (self.pos5[1] + dt * self.to[1] * 0.05)

        self.pos6[0] = (self.pos6[0] + dt * self.to[0] * 0.05)
        self.pos6[1] = (self.pos6[1] + dt * self.to[1] * 0.05)

        self.pos7[0] = (self.pos7[0] + dt * self.to[0] * 0.05)
        self.pos7[1] = (self.pos7[1] + dt * self.to[1] * 0.05)

        self.pos8[0] = (self.pos8[0] + dt * self.to[0] * 0.05)
        self.pos8[1] = (self.pos8[1] + dt * self.to[1] * 0.05)

        self.pos9[0] = (self.pos9[0] + dt * self.to[0] * 0.05)
        self.pos9[1] = (self.pos9[1] + dt * self.to[1] * 0.05)

Python Learning/[프로젝트] 총알 피하기 게임 Upgrade!/code/background.py


from __future__ import unicode_literals

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models

from django_changeset.models.mixins import RevisionModelMixin

def forwards_func(apps, schema_editor):
    RevisionModelMixin.set_enabled(False)
    db_alias = schema_editor.connection.alias

    User = apps.get_model('auth', 'User')
    Task = apps.get_model('projects', 'Task')
    TaskAssignedUser = apps.get_model('projects', 'TaskAssignedUser')

    # iterate over all tasks
    for task in Task.objects.using(db_alias).all():
        TaskAssignedUser.objects.using(db_alias).create(
            task=task,
            assigned_user=task.assigned_user
        )

    RevisionModelMixin.set_enabled(True)

def reverse_func(apps, schema_editor):
    db_alias = schema_editor.connection.alias

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('projects', '0056_task_assigned_users'),
    ]

    operations = [
        migrations.RunPython(forwards_func, reverse_func),
    ]


import threading
import math
from collections import deque
import time
import random
from datetime import datetime
import paho.mqtt.client as mqtt
import numpy as np

MQTT_SERVER = 'localhost'

MQTT_PENDULUM_STATE = 'pendulum_state_info'
MQTT_MOTOR_STATE = 'motor_state_info'

sub_topic_list = [MQTT_PENDULUM_STATE, MQTT_MOTOR_STATE]

self_env = None

class Env:
    def __init__(self):
        global self_env

        self_env = self

        self.state_space_shape = (6,)
        self.action_space_shape = (11,)

        self.last_pendulum_time = 0
        self.last_motor_time = 0

        self.pendulum_info_buffer = deque(maxlen=10)
        self.motor_info_buffer = deque(maxlen=10)
        self.pendulum_info = []
        self.motor_info = []
        self.last_pendulum_angle = 0
        self.last_motor_angle = 0

        self.sub = mqtt.Client(client_id="env_sub", transport="TCP")
        self.sub.on_connect = self.__on_connect
        self.sub.on_message = self.__on_message
        self.sub.connect(MQTT_SERVER, 1883, 60)

        s = threading.Thread(target=self.__sub, args=(self.sub,))
        s.daemon = True
        s.start()

        self.motor_speed = 0
        self.steps = 0
        self.done = False
        self.reward = 0
        self.rewards = []
        self.info = None

    @staticmethod
    def __on_connect(client, userdata, flags, rc):
        print("mqtt broker connected with result code " + str(rc))
        client.subscribe(topic=MQTT_PENDULUM_STATE)
        client.subscribe(topic=MQTT_MOTOR_STATE)

    @staticmethod
    def __sub(sub):
        try:
            print("Sub thread started!")
            sub.loop_forever()
        except KeyboardInterrupt as e:
            print("Sub thread Interrupted: {0}.format(0)")
            sub.unsubscribe(sub_topic_list)

    @staticmethod
    def __on_message(client, userdata, msg):
        if msg.topic == MQTT_PENDULUM_STATE:
            pendulum_info = str(msg.payload.decode("utf-8")).split('|')
            pendulum_info[1] = float(pendulum_info[1])

            pendulum_step_time = pendulum_info[1] - self_env.last_pendulum_time

            print("|| pendulum_angle: {0} || pendulum_step_time: {1:.4f} ||".format(
                pendulum_info[0], pendulum_step_time
            ))

            self_env.last_pendulum_time = pendulum_info[1]

        if msg.topic == MQTT_MOTOR_STATE:
            motor_info = str(msg.payload.decode("utf-8")).split('|')
            motor_info[1] = float(motor_info[1])

            motor_step_time = motor_info[1] - self_env.last_motor_time

            print("|| motor_angle: {0} || motor_step_time: {1:.4f} || ".format(
                motor_info[0], motor_step_time
            ))

            self_env.last_motor_time = motor_info[1]

if __name__ == "__main__":
    env = Env()

    while True:
        time.sleep(1)


'''Lv.2 [1차] 뉴스 클러스터링'''

# 문제 : https://school.programmers.co.kr/learn/courses/30/lessons/17677

# 풀이 1
def solution(str1, str2):
    # 1. s1 전처리
    s1 = []
    for i in range(len(str1)-1):
        element = str1[i:i+2]
        if element.isalpha():
            s1.append(element.lower())
    #print(s1)

    # 2. s2 전처리
    s2 = []
    for i in range(len(str2)-1):
        element = str2[i:i+2]
        if element.isalpha():
            s2.append(element.lower())
    #print(s2)

    # 3. 교집합원소개수, 합집합원소개수 계산
    cnt_gyo = 0
    for element in s1:
        if element in s2:
            cnt_gyo += 1
            s2[s2.index(element)] = ''
    cnt_hap = len(s1) + len(s2) - cnt_gyo

    # 4. J 구하기
    if cnt_hap == 0:
        return 65536
    else:
        return int(cnt_gyo / cnt_hap * 65536)

# 풀이 2
def solution(str1, str2):
    # 1. s1 전처리
    s1 = [str1[i:i+2].lower() for i in range(len(str1)-1) if str1[i:i+2].isalpha()]
    s2 = [str2[i:i+2].lower() for i in range(len(str2)-1) if str2[i:i+2].isalpha()]

    # 2. 교집합원소개수, 합집합원소개수 계산
    gyo = set(str1) & set(str2)
    cnt_gyo = sum([min(str1.count(gg), str2.count(gg)) for gg in gyo])
    cnt_hap = len(s1) + len(s2) - cnt_gyo

    # 4. J 구하기
    if cnt_hap == 0:
        return 65536
    else:
        return int(cnt_gyo / cnt_hap * 65536)

# 풀이 3
import re
import math
def solution(str1, str2):
    str1 = [str1[i:i+2].lower() for i in range(0, len(str1)-1) if not re.findall('[^a-zA-Z]+', str1[i:i+2])]
    str2 = [str2[i:i+2].lower() for i in range(0, len(str2)-1) if not re.findall('[^a-zA-Z]+', str2[i:i+2])]

    gyo = set(str1) & set(str2)
    hap = set(str1) | set(str2)

    if len(hap) == 0 :
        return 65536

    cnt_gyo = sum([min(str1.count(gg), str2.count(gg)) for gg in gyo])
    cnt_hap = sum([max(str1.count(hh), str2.count(hh)) for hh in hap])

    return math.floor((cnt_gyo/cnt_hap)*65536)

# 풀이 4
from collections import Counter
def solution(str1, str2):
    s1 = [str1[i:i+2].lower() for i in range(len(str1)-1) if str1[i:i+2].isalpha()]
    s2 = [str2[i:i+2].lower() for i in range(len(str2)-1) if str2[i:i+2].isalpha()]

    if not s1 and not s2:
        return 65536

    c1 = Counter(s1)
    c2 = Counter(s2)
    gyo = c1&c2
    hap = c1|c2
    cnt_gyo = sum(gyo.values())
    cnt_hap = sum(hap.values())

    answer = int(cnt_gyo / cnt_hap * 65536)

    return answer

# 풀이 5
from collections import Counter
def solution(str1, str2):
    s1 = [str1[i:i+2].lower() for i in range(len(str1)-1) if str1[i:i+2].isalpha()]
    s2 = [str2[i:i+2].lower() for i in range(len(str2)-1) if str2[i:i+2].isalpha()]
    if not s1 and not s2: return 65536
    return int(float(sum((Counter(s1)&Counter(s2)).values()))/float(sum((Counter(s1)|Counter(s2)).values())) * 65536)

[1차] 뉴스 클러스터링.py


from rest_framework.permissions import BasePermission, SAFE_METHODS
from core.utils import response_payload

class IsAdmin(BasePermission):
    message = response_payload(
        success=False,
        message="Only admins can perform this action",
    )

    def has_permission(self, request, view):
        return request.user.is_superuser

class IsTeacher(BasePermission):
    message = response_payload(
        success=False,
        message="Only teachers can perform this action",
    )

    def has_permission(self, request, view):
        return request.user.role == 'teacher'

class IsTeacherOfThisClassroom(BasePermission):
    message = response_payload(
        success=False,
        message="Only teachers of this classroom can perform this action",
    )

    def has_permission(self, request, view):
        classroom_id = view.kwargs['classroom_id']
        # This works because of the related_name='classrooms' in Classroom model.
        # Only teacher has 'classrooms' attribute, not the students.
        return request.user.classrooms.filter(classroom_id=classroom_id).exists()

class IsStudent(BasePermission):
    message = response_payload(
        success=False,
        message="Only students can perform this action",
    )

    def has_permission(self, request, view):
        return request.user.role == 'student'

class IsStudentOfThisClassroom(BasePermission):
    message = response_payload(
        success=False,
        message="Only students of this classroom can perform this action",
    )

    def has_permission(self, request, view):
        classroom_id = view.kwargs['classroom_id']
        # This works because of the related_name='enrollments' in Enrollment model.
        # Only student has 'enrollments' attribute, not the teachers.
        return request.user.enrollments.filter(classroom_id=classroom_id).exists()


import cv2
import numpy as np

cap = cv2.VideoCapture(0)
screen = np.zeros((800, 800, 3), np.uint8)

while True:
    re, frame = cap.read()
    frame = cv2.resize(frame, None, fx=0.5, fy=0.5)
    screen[0:480, 0:640] = frame

    cv2.imshow('screen', screen)
    cv2.imshow('frame', frame)
    print(type(frame[0, 0][0]))
    print(cv2.getWindowProperty('screen', cv2.WND_PROP_VISIBLE))
    if cv2.waitKey(1) & 0xff == ord('q'):
        break

cv2.destroyAllWindows()
cap.release()


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

from termcolor import colored
import socket

def fanc1():
    color_a = colored("[+] ", 'green')
    print("~"*50)
    host = input(color_a + "Host --> ")
    port = int(input(color_a + "Port --> "))
    print("~"*50)

    scan = socket.socket()

    color_b = colored("[!] ", 'red')
    color_c = colored("[!] ", 'yellow')

    try:
        scan.connect((host, port))
    except socket.error:
        print(color_b + "Port -- ", port, " -- [CLOSED]")
    else:
        print(color_c + "Port -- ", port, " -- [OPEN]")

def fanc2():
    color_a = colored("[+] ", 'green')
    color_b = colored("[!] ", 'red')
    color_c = colored("[!] ", 'yellow')

    host = input(color_a + "Host --> ")
    print("\n")
    port = []

    for i in port:
        try:
            scan = socket.socket()
            scan.settimeout(0.5)
            scan.connect((host, i))
        except socket.error:
            print(color_b + "Port -- ", i, " -- [CLOSED]")
        else:
            print(color_c + "Port -- ", i, " -- [OPEN]")

print("~"*50)

print("\t[1] --- сканировать отделный порт")
print("\t[2] --- сканировать список")

print("~"*50, "\n")
text_a = input("[scan]--> ")

if text_a == "1":
    fanc1()
elif text_a == "2":
    fanc2()
else:
    print(colored("Параметр введен не правильно!", 'red'))


import socket
import json
import rsa
from datetime import datetime

now = datetime.now()

current_time = now.strftime("%H:%M:%S")

UDP_IP = "127.0.0.1"
UDP_PORT = 5005

pesan = input("Masukkan Pesan Anda \t: ")

print(f"Target IP \t\t: {UDP_IP}")
print(f"Target Port \t\t: {UDP_PORT}")
# print(f"Pesan \t\t\t: {pesan}")

# Membuat Socket UDP
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
msg = {
    "user" : "Josua",
    "pesan" : pesan,
    "waktu"  : current_time
    }

x = "Halooo"

publicKey, privateKey = rsa.newkeys(512)

encMsg = rsa.encrypt(x.encode(), publicKey)
print(f"Pesan \t\t\t: {encMsg}")
# Mengirim pesan ke server
sock.sendto(encMsg, (UDP_IP, UDP_PORT))


import os
import shutil
import sys
import unittest

from monty.serialization import loadfn

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
__package__ = "auto_test"

from dpgen.auto_test.Lammps import Lammps
from dpgen.auto_test.lib.lammps import inter_deepmd

from .context import setUpModule  # noqa: F401

class TestLammps(unittest.TestCase):
    def setUp(self):
        self.jdata = {
            "structures": ["confs/std-fcc"],
            "interaction": {
                "type": "deepmd",
                "model": "lammps_input/frozen_model.pb",
                "deepmd_version": "1.1.0",
                "type_map": {"Al": 0},
            },
            "relaxation": {
                "cal_type": "relaxation",
                "cal_setting": {
                    "relax_pos": True,
                    "relax_shape": True,
                    "relax_vol": True,
                },
            },
        }

        self.equi_path = "confs/std-fcc/relaxation/relax_task"
        self.source_path = "equi/lammps"

        if not os.path.exists(self.equi_path):
            os.makedirs(self.equi_path)

        if not os.path.isfile(os.path.join(self.equi_path, "POSCAR")):
            shutil.copy(
                os.path.join(self.source_path, "Al-fcc.vasp"),
                os.path.join("confs/std-fcc", "POSCAR"),
            )

        self.confs = self.jdata["structures"]
        self.inter_param = self.jdata["interaction"]
        self.relax_param = self.jdata["relaxation"]
        self.Lammps = Lammps(
            self.inter_param, os.path.join(self.source_path, "Al-fcc.vasp")
        )

    def tearDown(self):
        if os.path.exists("confs/std-fcc/relaxation"):
            shutil.rmtree("confs/std-fcc/relaxation")

    def test_set_inter_type_func(self):
        self.Lammps.set_inter_type_func()
        self.assertEqual(inter_deepmd, self.Lammps.inter_func)

    def test_set_model_param(self):
        self.Lammps.set_model_param()
        model_param = {
            "model_name": ["frozen_model.pb"],
            "param_type": {"Al": 0},
            "deepmd_version": "1.1.0",
        }
        self.assertEqual(model_param, self.Lammps.model_param)

    def test_make_potential_files(self):
        cwd = os.getcwd()
        abs_equi_path = os.path.abspath(self.equi_path)
        self.Lammps.make_potential_files(abs_equi_path)
        self.assertTrue(os.path.islink(os.path.join(self.equi_path, "frozen_model.pb")))
        self.assertTrue(os.path.isfile(os.path.join(self.equi_path, "inter.json")))
        ret = loadfn(os.path.join(self.equi_path, "inter.json"))
        self.assertTrue(self.inter_param, ret)
        os.chdir(cwd)

    def test_make_input_file(self):
        cwd = os.getcwd()
        abs_equi_path = os.path.abspath("confs/std-fcc/relaxation/relax_task")
        shutil.copy(
            os.path.join("confs/std-fcc", "POSCAR"),
            os.path.join(self.equi_path, "POSCAR"),
        )
        self.Lammps.make_input_file(abs_equi_path, "relaxation", self.relax_param)
        self.assertTrue(os.path.isfile(os.path.join(abs_equi_path, "conf.lmp")))
        self.assertTrue(os.path.islink(os.path.join(abs_equi_path, "in.lammps")))
        self.assertTrue(os.path.isfile(os.path.join(abs_equi_path, "task.json")))

    def test_forward_common_files(self):
        fc_files = ["in.lammps", "frozen_model.pb"]
        self.assertEqual(self.Lammps.forward_common_files(), fc_files)

    def test_backward_files(self):
        backward_files = ["log.lammps", "outlog", "dump.relax"]
        self.assertEqual(self.Lammps.backward_files(), backward_files)


from fractions import Fraction as fract

def solution(pegs):
    print("solution_final -> pegs: ", pegs)
    lenPegs = len(pegs)
    if (lenPegs < 2):
        return [-1, -1]

    isEven = True if (lenPegs % 2 == 0) else False
    x = (- pegs[0] + pegs[lenPegs -1]) if isEven else (- pegs[0] - pegs[lenPegs - 1])

    if (lenPegs > 2):
        for i in range(1, lenPegs - 1):
            x += 2 * (-1)**(i+1) * pegs[i]

    G = []
    G.append(fract(2 * (float(x)/3 if isEven else x)).limit_denominator())
    radius = G[0]

    if ((radius / 2 > (pegs[-1] - pegs[-2])) or (radius < 2)):
        return [-1, -1]

    for index in range(0, lenPegs - 1):
            dist = pegs[index + 1] - pegs[index]
            nextRadius = fract(dist - radius).limit_denominator()
            if (radius < 1 or nextRadius < 1):
                return [-1, -1]
            else:
                radius = nextRadius
                G.append(radius)

    return [G[0].numerator, G[0].denominator]

def solution2(pegs):
    print("solution2 -> pegs: ", pegs)
    lenPegs = len(pegs)
    if (lenPegs < 2):
        return [-1, -1]

    isEven = True if (lenPegs % 2 == 0) else False
    x = (- pegs[0] + pegs[lenPegs -1]) if isEven else (- pegs[0] - pegs[lenPegs - 1])

    if (lenPegs > 2):
        for i in range(1, lenPegs - 1):
            x += 2 * (-1)**(i+1) * pegs[i]

    G = []
    G.append(fract(2 * (float(x)/3 if isEven else x)).limit_denominator())

    radius = G[0]
    for index in range(0, lenPegs - 2):
            dist = pegs[index + 1] - pegs[index]
            nextRadius = dist - radius
            if (radius < 1 or nextRadius < 1):
                return [-1, -1]
            else:
                radius = nextRadius

    return [G[0].numerator, G[0].denominator]

def solution1(pegs):
    print("solution1 -> pegs: ", pegs)
    lenP = len(pegs)

    if ((not pegs) or lenP == 1):
        return [-1, -1]

    a = pegs[0]
    gradient = -1
    for peg in pegs:
        a += 2 * peg * gradient
        gradient *= -1
    a += pegs[lenP - 1] * gradient
    a *= 2
    b = 3 if (lenP % 2 == 0) else 1

    if ((b == 3) and (a % b == 0)):
        a /= b
        b = 1

    prevRadius = float(float(a) / float(b))
    for i in range(0, lenP - 2):
        w = int(pegs[i+1] - pegs[i])
        if ((prevRadius < 1) or (prevRadius > (w - 1))):
            return [-1, -1]

    return [int(a), int(b)]

from random import sample

assert((solution([4, 30, 50])) == [12, 1])
print(solution([1, 11]))
print(solution([4.5, 30.5, 50.5]))
print(solution([10, 50.77, 70, 90.5, 115, 150]))
print(solution([4, 9, 17, 31, 40]))
print(solution([4, 30, 50]))
assert((solution1([4, 30, 50])) == [12, 1])
print(solution1([1, 11]))
print(solution1([4.5, 30.5, 50.5]))
print(solution1([10, 50.77, 70, 90.5, 115, 150]))
print(solution1([4, 9, 17, 31, 40]))
print(solution1([4, 30, 50]))
assert((solution2([4, 30, 50])) == [12, 1])
print(solution2([1, 11]))
print(solution2([4.5, 30.5, 50.5]))
print(solution2([10, 50.77, 70, 90.5, 115, 150]))
print(solution2([4, 9, 17, 31, 40]))
print(solution2([4, 30, 50]))

for i in range(1, 150):
    a = sample([x for x in range(1, 10000)], k=(sample([x for x in range(2, 21)], k=1)[0]))
    a.sort()
    print(solution(a))
    print(solution1(a))
    print(solution2(a))

gearing up for destruction/test-solution5.py


import labrad
from matplotlib import pyplot
#get access to servers
cxn = labrad.connect()
dv = cxn.data_vault

'This is phase oscillation due to AC Stark shift for local detection experiment'

dv.cd(['','Experiments','RamseyDephaseScanDuration','2013Apr05','1545_16'])
dv.open(1)
data = dv.get().asarray
x = data[:,0]
y = data[:,1]
figure = pyplot.figure()
figure.clf()
pyplot.plot(x, y,'o-')

pyplot.title('AC Stark Shift', fontsize = 40)
pyplot.ylim([0,1])
pyplot.xlabel(r'Interaction Time ( $\mu s$ )', fontsize = 32)
pyplot.ylabel('Excitation percentage', fontsize = 32)
pyplot.tick_params('both', labelsize = 20)
pyplot.show()


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

# ## Data Exploration

# In[ ]:

import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt

# In[ ]:

rawData = pd.read_csv("../../../input/aljarah_xAPI-Edu-Data/xAPI-Edu-Data.csv")

# In[ ]:

rawData.tail().T

# In[ ]:

rawData.columns

# In[ ]:

rawData.info()

# In[ ]:

import numpy as np

np.where(pd.isnull(rawData))

# In[ ]:

rawData.describe()

# ###  Catagorical variables

# In[ ]:

cat_Vars = ['gender', 'NationalITy', 'PlaceofBirth', 'StageID','Relation', 'GradeID', 'Topic', 'Semester', 'StudentAbsenceDays']

# In[ ]:

fig = plt.figure(figsize=(20, 30))
fig.subplots_adjust(hspace=.3, wspace=0.2)

for i in range(1,len(cat_Vars)+1,1):
    ax = fig.add_subplot(5, 2, i,)
    sns.countplot(rawData[cat_Vars[i-1]])
    ax.xaxis.label.set_size(20)
    plt.setp(ax.get_xticklabels(), rotation=30, horizontalalignment='right')
    total = float(len(rawData))
    for p in ax.patches:
        height = p.get_height()
        ax.text(p.get_x()+p.get_width()/2.,height + 5,'{:1.1f}%'.format(100 * height/total),ha="center")

# #### Based on the the above plots several observations can be made:
#     -Majority of students are male
#     -Most of the students are from either Kuwait or Jordan
#     -Majority of students are middleschool or lower

# ### Numerical Variables

# In[ ]:

num_Vars = [ 'raisedhands','VisITedResources', 'AnnouncementsView', 'Discussion', 'Class']

# fig = plt.figure(figsize=(10, 10))
# for i in range(1,len(num_Vars)+1,1):
#     ax = fig.add_subplot(2, 2, i)
#     sns.distplot(rawData[num_Vars[i-1]],kde= None)

# In[ ]:

# In general these features represent the level of student particapation in their respective courses.
# Cleary the students with low grades tend to have lower level of class particapation.

# ## Lets try to figure out which feauters are most important (if any) for predicting students' academic perfomance.

# Lets predict student perfomance using scikit random forest classifier

# In[ ]:

target = rawData.pop('Class') # target

# Drop the features not relevant to the student perfomance
rawData.drop(rawData[['ParentAnsweringSurvey', 'ParentschoolSatisfaction']], axis=1, inplace=True)

X = pd.get_dummies(rawData) # get numeric dummy variables for categorical data

# In[ ]:

from sklearn import preprocessing

le = preprocessing.LabelEncoder()
y = le.fit_transform(target) # encode target labels with a value

# In[ ]:

from sklearn.cross_validation import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.40) # split data set into train and set
X_train.shape, X_test.shape

# ### Cross-Validation to determine the best model parameter values

# In[ ]:

from sklearn.grid_search import GridSearchCV
from sklearn.ensemble import RandomForestClassifier

rfc = RandomForestClassifier(n_jobs=-1,max_features= 'sqrt' ,n_estimators=50, oob_score = True)

param_grid ={'n_estimators': [50, 100, 200],'max_features': ['auto', 'sqrt', 'log2'],'min_samples_leaf' : [1, 5,10, 50],}

CV_rfc = GridSearchCV(estimator=rfc, param_grid=param_grid, cv= 5, refit=True)
CV_rfc.fit(X_train, y_train)
CV_rfc.best_params_

# In[ ]:

#CV_rfc.grid_scores_

# ### Use best fit model parameters to make predictions on test set

# In[ ]:

from sklearn.metrics import accuracy_score

rfc = RandomForestClassifier(n_jobs=-1,max_features= 'sqrt' ,n_estimators=100, oob_score = True)
rfc.fit(X_train, y_train)
pred = rfc.predict(X_test)
accuracy_score(y_test, pred)

# ### Plot feature importance

# In[ ]:

feature_importance = pd.DataFrame(rfc.feature_importances_, index=X_train.columns, columns=["Importance"])
feature_importance.head(8)

# In[ ]:

feature_importance.sort('Importance', ascending=False, inplace=True)

# In[ ]:

fig = plt.figure(figsize=(25, 10))
ax = sns.barplot(feature_importance.index, feature_importance['Importance'])
fig.add_axes(ax)
plt.setp(ax.get_xticklabels(), rotation=30, horizontalalignment='right')[1]

# ### Quick test with xgboost classifier

# In[ ]:

import xgboost as xgb

xg_train = xgb.DMatrix(X_train, label=y_train)
xg_test = xgb.DMatrix(X_test, label=y_test)

# In[ ]:

param = {}

# use softmax multi-class classification
param['objective'] = 'multi:softmax'

# set xgboost parameter values
param['eta'] = 0.1
param['max_depth'] = 9
param['silent'] = 1
param['nthread'] = 3
param['num_class'] = 3

num_round = 100
bst = xgb.train(param, xg_train, num_round)

# get prediction
pred = bst.predict( xg_test ).astype(int)
accuracy_score(y_test, pred)

# In[ ]:

xgb.plot_importance(bst, height=0.5)

# The features related to the student participation in the class are important in accurate prediction of the student
# performance in any given course. (This is specific to the data set and the model we are using in this analysis.)
# This can be further tested by studying the variation in acuracy obtained by removing the important featues one by one from the model.

# In[ ]:


# @time 2022/6/16 10:46
# @Author howell
# @File test_auto_ui.PY
import time
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.webdriver import WebDriver
import uiautomator2 as u2

# from hamcrest import *

capabilities = {}
# Android平台测试
capabilities['platformName'] = 'Android'
# 测试手机版本为5.0
capabilities['platformVersion'] = '7.1.2'
capabilities['deviceName'] = '127.0.0.1:21503'
# 系统手机中的联系人app的包名   adb shell "dumpsys window | grep mCurrentFocus"
capabilities['appPackage'] = 'com.smjcs.app'
# 系统手机中的联系人app的主入口activity
capabilities['appActivity'] = 'io.dcloud.PandoraEntryActivity'
capabilities['unicodeKeyboard'] = True
capabilities['resetKeyboard'] = False
capabilities['noReset'] = True
capabilities['automationName'] = 'uiautomator2'
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', capabilities)
driver.implicitly_wait(10)

def test_click():
    x = driver.get_window_size()['width']
    y = driver.get_window_size()['height']
    time.sleep(2)
    click_y = int(y / 5 * 3 - 30 - 3)
    driver.tap([(x * 0.09, click_y)], 500)
    for i in range(50):
        # 选择分类
        el = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,
                                 'new UiSelector().className(\"android.view.View\").textContains(\"测试标签\")')
        el.click()
        # 选择规格
        el = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,
                                 'new UiSelector().className(\"android.view.View\").textContains(\"选规格\")')
        el.click()
        time.sleep(1)
        driver.tap([(x * 0.5, y - 30)], 500)
        # 结算
        driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,
                            'new UiSelector().className(\"android.view.View\").textContains(\"结算\")').click()
        try:
            # 提交订单
            driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,
                                'new UiSelector().className(\"android.view.View\").textContains(\"提交订单\")').click()
        except Exception as e:
            print(e)
            # name = str(i) + "error.png"
            # driver.save_screenshot(name)
            # 结算
            driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,
                                'new UiSelector().className(\"android.view.View\").textContains(\"结算\")').click()
            # 提交订单
            el2 = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,
                                      'new UiSelector().className(\"android.view.View\").textContains(\"提交订单\")')
            el2.click()

        # driver.find_element_by_android_uiautomator(
        #     'new UiSelector().className(\"android.view.View\").textContains(\"结算\")').click()
        # 支付
        driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,
                            'new UiSelector().className(\"android.view.View\").textContains(\"确认支付\")').click()
        # 返回首页
        driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,
                            'new UiSelector().className(\"android.view.View\").textContains(\"返回首页\")').click()

if __name__ == '__main__':
    test_click(5)


import getpass
import os
from tkinter import *
import tkinter as tk
from tkinter import IntVar, StringVar, filedialog
from tkinter.simpledialog import askstring
from pystray import MenuItem as item
import pystray
from PIL import Image
from downloadOrganizer import File_Handler
from win32api import GetSystemMetrics

global runner

width = int ( GetSystemMetrics(0) / 2 )
height = int ( GetSystemMetrics(1) / 1.8 )

class GUI(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title('Make your Download-Folder great again!')
        self.geometry(f'{width}x{height}')
        self.iconbitmap('./img/icon.ico')
        self.download_folder = runner.get_download_directory()
        self.destination_folder = runner.get_destination_directory()
        self.categories_dict = runner.get_categories_extensions()
        self.interval = runner.get_interval()
        self.protocol("WM_DELETE_WINDOW", self.hide_window)
        self.grid_columnconfigure(2, weight=1)
        self.setup_gui()
        if runner.get_autorun() == True:
            runner.start()
    def update_values(self, current_cat):
        to_add = askstring(f'Add extension', f'Add an extension to category: "{current_cat}"')
        print(f'add {to_add} to {current_cat}')
    def index_categories(self, grid):
        row = 0
        for k, v in runner.get_categories_extensions().items():
            tk.Label(grid, text=f'{k}:', font=('Arial', int ( 10 ), 'bold')).grid(column=0, row=row, padx=(20, 0), pady=(0, 1), sticky='w')
            if v is not None:
                values = ''
                for i in v:
                    if values == '':
                        values = i
                    else:
                        values += f', {i}'
                tk.Label(grid, text=f'{values},', font=('Arial', int ( 10 ), 'bold')).grid(column=1, row=row, padx=(20, 0), pady=(0, 1), sticky='w')
            else:
                tk.Label(grid, text=f'none', font=('Arial', int ( 10 ), 'italic')).grid(column=1, row=row, padx=(20, 0), pady=(0, 1), sticky='w')
            row += 1
    def activate_autostart(self):
        file_path = os.path.dirname(os.path.realpath(__file__))
        ext = os.fsdecode(os.path.realpath(__file__))
        USER_NAME = getpass.getuser()
        bat_path = r'C:/Users/%s/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup' % USER_NAME
        print(ext)
        with open(bat_path + '/' + "DownloadsOrganizer.bat", "w+") as bat_file:
            bat_file.write(f'start "" "{file_path}{ext}"')

        try:
            if os.path.exists(f'{file_path}/autostart'):
                return
            f = open("autostart", "x")
            f.close()
        except:
            tk.messagebox.showinfo(title='Error', message='Error at setting up autostart.')

    def deactivate_autostart(self):
        file_path = os.path.dirname(os.path.realpath(__file__))
        USER_NAME = getpass.getuser()
        bat_path = r'C:/Users/%s/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup' % USER_NAME
        if os.path.exists(bat_path + '/' + "DownloadsOrganizer.bat"):
            os.remove(bat_path + '/' + "DownloadsOrganizer.bat")

        try:
            if os.path.exists(f'{file_path}/autostart'):
                os.remove(f'{file_path}/autostart')
        except:
            tk.messagebox.showinfo(title='Error', message='Error at removing autostart.')
    def done(self):
        runner.set_download_directory(self.download_folder)
        runner.set_destination_directory(self.destination_folder)
    def save(self, interval, autostart):
        self.interval = interval
        runner.set_interval(interval)
        if autostart:
            self.activate_autostart()
        else:
            self.deactivate_autostart()
        self.done()
        tk.messagebox.showinfo(title='Saved', message='Saved all settings. Click on \'start\' to start the program.')
    def start(self):
        runner.start()
        tk.messagebox.showinfo(title='Starting...', message='Started the program.')
    def stop(self):
        runner.stop()
        tk.messagebox.showinfo(title='Stopping...', message='Successfully stopped the program.')
    def init_settings(self, grid):
        interval_lbl = tk.Label(grid, text='Interval: ', font=('Arial', int ( 10 ), 'bold'))
        interval_lbl.grid(column=0, row=0, padx=(20, 0))

        placeholder_spinbox = StringVar(self)
        placeholder_spinbox.set(int(self.interval))
        spinbox = tk.Spinbox(grid, from_=1, to=1000, width=5, textvariable=placeholder_spinbox)
        spinbox.grid(column=1, row=0, padx=(5, 0))

        is_autostart = tk.IntVar()
        opt_autostart = tk.Checkbutton(grid, text='Autostart', variable=is_autostart)
        opt_autostart.grid(row=0, column=2, padx=(20,0))

        save_btn = tk.Button(grid, text='Save', command=lambda : self.save(spinbox.get(), is_autostart.get()))
        save_btn.grid(column=0, row=1, sticky='w', padx=(20,0), pady=(10,0))

        start_btn = tk.Button(grid, text='Start', command=self.start)
        start_btn.grid(column=1, row=1, sticky='w', padx=(40,0), pady=(10,0))

        stop_btn = tk.Button(grid, text='Stop', command=self.stop)
        stop_btn.grid(column=2, row=1, sticky='w', padx=(20,0), pady=(10,0))
    def select_download_folder(self, lbl):
        self.download_folder = filedialog.askdirectory()
        lbl['text'] = f'{self.download_folder}'
    def select_destination_folder(self, lbl):
        self.destination_folder = filedialog.askdirectory()
        lbl['text'] = f'{self.destination_folder}'
    def help(self):
        tk.messagebox.showinfo(title='Extensions', message='Please navigate to the installation folder and find the \'settings.ini\' file.\nTo add a category, append a new line at the bottom. To add an extension, append them to the according line, separated by a comma. Please find the included examples.')
    def quit_window(self, icon, item):
        icon.stop()
        runner.stop()
        self.destroy()
    def show_window(self, icon, item):
        icon.stop()
        self.after(0, self.deiconify())
    def hide_window(self):
        self.withdraw()
        image = Image.open('./img/icon.ico')
        menu = (item('Show', self.show_window), item('Quit', self.quit_window))
        icon=pystray.Icon('Downloadfolder Organizer', image, 'Organizer', menu)
        icon.run()
    def setup_gui(self):
        # --------- MAIN GRID ---------
        grid = tk.Frame(self, bd=2)
        grid.pack(fill='x')

        # --------- FIRST ROW ---------
        grid1 = tk.Frame(grid, bd=2)
        grid1.grid(column=0, row=0, padx=0, pady=(10, 5), sticky='w')

        grid1.grid_columnconfigure(1, weight=1)

        foldersetting_lbl = tk.Label(grid1, text='Folder-Settings', font=('Arial', 20, 'bold'))
        foldersetting_lbl.grid(column=0, row=0, padx=20, sticky='w')

        downloadfolder_lbl = tk.Label(grid1, text='Select your Download-Folder...', font=('Arial', int ( 10 ), 'bold'))
        downloadfolder_lbl.grid(column=0, row=1, padx=20, pady=(10,0), sticky='w')
        current_downloadfolder = tk.Label(grid1, text=f'Auswahl:  {self.download_folder}', font=('Arial', int ( 10 ), 'italic'))
        current_downloadfolder.grid(column=0, row=2, padx=20, sticky='w')
        select_download_button = tk.Button(grid1, text='Select...', width=10, command=lambda:self.select_download_folder(current_downloadfolder))
        select_download_button.grid(column=1, row=2)

        destinationfolder_lbl = tk.Label(grid1, text='Select your Destination-Folder...', font=('Arial', int ( 10 ), 'bold'))
        destinationfolder_lbl.grid(column=0, row=3, padx=20, pady=(10,0), sticky='w')
        current_destinationfolder = tk.Label(grid1, text=f'Auswahl:  {self.destination_folder}', font=('Arial', int ( 10 ), 'italic'))
        current_destinationfolder.grid(column=0, row=4, padx=20, sticky='w')
        select_destination_button = tk.Button(grid1, text='Select...', width=10, command=lambda:self.select_destination_folder(current_destinationfolder))
        select_destination_button.grid(column=1, row=4)

        # --------- SECOND ROW ---------
        grid2 = tk.Frame(grid, bd=2)
        grid2.grid(column=0, row=1, padx=0, sticky='w')

        extensionsetting_lbl = tk.Label(grid2, text='Extensions', font=('Arial', int ( 20 ), 'bold'))
        extensionsetting_lbl.grid(column=0, row=0, padx=20, pady=(10, 0), sticky='w')

        extensionsetting_btn = tk.Button(grid2, text='?', width=5, command=self.help)
        extensionsetting_btn.grid(column=1, row=0, padx=20, pady=(10, 0))

        sub_grid2 = tk.Frame(grid2, bd=2)
        sub_grid2.grid(column=0, row=1, padx=0, sticky='w')

        self.index_categories(sub_grid2)

        # --------- THIRD ROW ---------
        grid3 = tk.Frame(grid, bd=2)
        grid3.grid(column=0, row=2, padx=0, sticky='w')

        miscsettings_lbl = tk.Label(grid3, text='Settings', font=('Arial', int ( 20 ), 'bold'))
        miscsettings_lbl.grid(column=0, row=0, padx=20, pady=(10, 0), sticky='w')

        sub_grid3 = tk.Frame(grid3, bd=2)
        sub_grid3.grid(column=0, row=1, padx=0, sticky='w')

        self.init_settings(sub_grid3)

if __name__ == '__main__':
    runner = File_Handler()
    root = GUI()
    root.mainloop()


from imutils import face_utils
import dlib
import cv2 as cv

path = "shape_predictor_68_face_landmarks.dat"

img = cv.imread("images/dt.jpg")

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(path)

gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

# get faces in image
rect = detector(gray, 0)

# for each detected face, find the landmarks
for i, face in enumerate(rect):
    shape = predictor(gray, face)
    shape = face_utils.shape_to_np(shape)

    # Draw on our image, all the found coordinate points (x,y)
    counter = 0
    for (x, y) in shape:
        counter += 1
        cv.circle(img, (x, y), 2, (0, 255, 0), -1)

    print(counter)
    # write the result
    cv.imwrite('face_features{}.png'.format(i), img)


from django import forms
from django.core import validators

class FormArticle(forms.Form):
    title = forms.CharField(
        label="Titulo",
        max_length=20,
        required=True,
        widget=forms.TextInput(
            attrs={  # html attributes
                'placeholder': 'Mete el titulo',
                'class': 'title_form_article'
            }
        ),
        validators=[
            validators.MinLengthValidator(4, 'El titulo es demasiado corto'),
            validators.RegexValidator('^[A-Za-z0-9]*$', 'El titulo esta mal formado', 'invalid_title')
            # validation characters
        ]
    )

    content = forms.CharField(
        label="Contenido",
        widget=forms.Textarea,
        validators=[
            validators.MaxLengthValidator(50, 'Te has pasado, has puesto mucho texto')
        ]
    )
    content.widget.attrs.update({  # html attributes
        'placeholder': 'Mete el contenido',
        'class': 'content_form_article',
        'id': 'contenido_form'
    })

    public_options = [
        (1, 'si'), (0, 'no')
    ]
    public = forms.TypedChoiceField(
        choices=public_options,
        label='¿Publicado?'
    )


import numpy as np
import time
import sys
from pathlib import Path
import logging
import os

class Logger(object):
    def __init__(self, outfile):
        self.terminal = sys.stdout
        self.log = open(outfile, "w")
        sys.stdout = self

    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)

    def flush(self):
        self.terminal.flush()

class AverageMeter:
    def __init__(self, *keys):
        self.__data = dict()
        for k in keys:
            self.__data[k] = [0.0, 0]

    def add(self, dict):
        for k, v in dict.items():
            if k not in self.__data:
                self.__data[k] = [0.0, 0]
            self.__data[k][0] += v
            self.__data[k][1] += 1

    def get(self, *keys):
        if len(keys) == 1:
            return self.__data[keys[0]][0] / self.__data[keys[0]][1]
        else:
            v_list = [self.__data[k][0] / self.__data[k][1] for k in keys]
            return tuple(v_list)

    def pop(self, key=None):
        if key is None:
            for k in self.__data.keys():
                self.__data[k] = [0.0, 0]
        else:
            v = self.get(key)
            self.__data[key] = [0.0, 0]
            return v

class Timer:
    def __init__(self, starting_msg = None):
        self.start = time.time()
        self.stage_start = self.start

        if starting_msg is not None:
            print(starting_msg, time.ctime(time.time()))

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        return

    def update_progress(self, progress):
        self.elapsed = time.time() - self.start
        self.est_total = self.elapsed / progress
        self.est_remaining = self.est_total - self.elapsed
        self.est_finish = int(self.start + self.est_total)

    def str_estimated_complete(self):
        return str(time.ctime(self.est_finish))

    def get_stage_elapsed(self):
        return time.time() - self.stage_start

    def reset_stage(self):
        self.stage_start = time.time()

    def lapse(self):
        out = time.time() - self.stage_start
        self.stage_start = time.time()
        return out

def to_one_hot(sparse_integers, maximum_val=None, dtype=np.bool):

    if maximum_val is None:
        maximum_val = np.max(sparse_integers) + 1

    src_shape = sparse_integers.shape

    flat_src = np.reshape(sparse_integers, [-1])
    src_size = flat_src.shape[0]

    one_hot = np.zeros((maximum_val, src_size), dtype)
    one_hot[flat_src, np.arange(src_size)] = 1

    one_hot = np.reshape(one_hot, [maximum_val] + list(src_shape))

    return one_hot

def create_logger(args, root_output_dir='outputs', log_output_dir='logs'):
    root_output_dir = Path(root_output_dir)
    # set up logger
    if not root_output_dir.exists():
        print('=> creating {}'.format(root_output_dir))
        root_output_dir.mkdir()

    final_output_dir = root_output_dir /\
                       '{}_{}'.format(args.cam_network.split('.')[-1], args.irn_network.split('.')[-1])

    print('=> creating {}'.format(final_output_dir))
    final_output_dir.mkdir(parents=True, exist_ok=True)

    time_str = time.strftime('%Y-%m-%d-%H-%M')

    tensorboard_log_dir = Path(log_output_dir) / \
                          '{}_{}'.format(args.cam_network.split('.')[-1], args.irn_network.split('.')[-1]) / \
                          time_str
    print('=> creating {}'.format(tensorboard_log_dir))
    tensorboard_log_dir.mkdir(parents=True, exist_ok=True)

    args.log_name = str(tensorboard_log_dir)

    cam_weight_path = final_output_dir / Path('cam_weight.pth')
    args.cam_weights_name = str(cam_weight_path)
    aff_weight_path = final_output_dir / Path('aff_weight.pth')
    args.irn_weights_name = str(aff_weight_path)
    args.cam_out_dir = str(final_output_dir / Path('cam'))
    args.ir_label_out_dir = str(final_output_dir / Path('aff_label'))
    args.sem_seg_out_dir = str(final_output_dir / Path('sem_seg'))
    args.ins_seg_out_dir = str(final_output_dir / Path('ins_seg'))
    os.makedirs(args.cam_out_dir, exist_ok=True)
    os.makedirs(args.ir_label_out_dir, exist_ok=True)
    os.makedirs(args.sem_seg_out_dir, exist_ok=True)
    os.makedirs(args.ins_seg_out_dir, exist_ok=True)


# -*- coding: utf-8 -*-
from typing import Any, Dict
from googleapiclient import discovery
from matos_gcp_provider.lib import factory
from matos_gcp_provider.lib.base_provider import BaseProvider

class serviceAccount(BaseProvider):
    """GCP service account class

    Args:
        BaseProvider (Class): Base provider class
    """

    def __init__(self, resource: Dict, **kwargs) -> None:
        """
        Construct service acounts service
        """
        self.resource = resource
        self.resource_type = "iam.googleapis.com/ServiceAccount"
        self.project_id = resource.pop("project_id")
        super().__init__(**kwargs)

    def get_inventory(self) -> Any:
        """
        Service discovery
        """
        service_accounts = []
        resources = self._get_assets(self.project_id, self.resource_type)
        service = discovery.build("iam", "v1", credentials=self.credentials)
        request = service.projects().serviceAccounts()
        for resource in resources:
            service_accounts.append(
                {
                    "type": "serviceAccount",
                    **resource.get("resource").get("data"),
                    "getPolicy": request.getIamPolicy(
                        resource=resource.get("resource").get("data").get("name")
                    ).execute(),
                    **request.keys()
                    .list(name=resource.get("resource").get("data").get("name"))
                    .execute(),
                }
            )
        return service_accounts

def register() -> Any:
    """Register plugins type"""
    factory.register("serviceAccount", serviceAccount)


import cv2
import numpy as np
from scipy.spatial import distance
from supervision.detection.annotate import BoxAnnotator
from supervision.detection.core import Detections
from ultralytics import YOLO

class DetectorTracker:
    def __init__(self):
        self.model = YOLO('yolov8n.pt')
        self.class_names_dict = self.model.model.names
        self.box_annotator = BoxAnnotator()
        self.point = ()
        self.tracking = False
        self.track_id = None

    def __clickEvent(self, event, x, y, flags, param):
        if event == cv2.EVENT_LBUTTONDOWN:
            self.point = (x, y)

    def __select_box(self, bboxes):
        if len(bboxes) != 0:
            x, y = self.point
            min_distance = None
            bbox_track = []

            for bbox in bboxes:
                x1, y1, x2, y2 = bbox
                if (x1 <= x <= x2) and (y1 <= y <= y2):
                    center = ((x2 + x1) / 2, (y2 + y1) / 2)
                    dist = distance.euclidean(center, self.point)
                    if min_distance:
                        if dist < min_distance:
                            min_distance = dist
                            bbox_track = bbox
                    else:
                        min_distance = dist
                        bbox_track = bbox
            return bbox_track
        else:
            return []

    def run(self):
        cap = cv2.VideoCapture(0)
        cv2.namedWindow('frame')
        cv2.setMouseCallback('frame', self.__clickEvent)
        tracker_id = None
        while True:
            ret, frame = cap.read()
            if ret is False:
                break

            results = self.model.track(frame, tracker="botsort.yaml", persist=True)[0]

            xyxy = results.boxes.xyxy.cpu().numpy()
            confidence = results.boxes.conf.cpu().numpy()
            class_id = results.boxes.cls.cpu().numpy().astype(int)

            if results.boxes.id is not None:
                tracker_id = results.boxes.id.cpu().numpy().astype(int)
            else:
                tracker_id = None

            if self.point != ():
                box_track = self.__select_box(bboxes=detections.xyxy)
                idx = np.where(detections.xyxy == box_track)[0][0]

                self.track_id = tracker_id[idx]
                self.tracking = True
                self.point = ()

            if self.tracking:
                idx = np.where(tracker_id == self.track_id)
                if len(idx[0]) != 0:
                    idx = idx[0][0]
                    xyxy = np.array([xyxy[idx]])
                    confidence = np.array([confidence[idx]])
                    tracker_id = np.array([tracker_id[idx]])
                    class_id = np.array([class_id[idx]])
                else:
                    xyxy = np.empty((0, 4), 'float64')
                    confidence = np.array([], 'float64')
                    tracker_id = np.array([], 'int')
                    class_id = np.array([], 'int')

            detections = Detections(xyxy=xyxy,
                                    confidence=confidence,
                                    class_id=class_id,
                                    tracker_id=tracker_id)

            labels = [
                f"{tracker_id}: {self.class_names_dict[class_id]} {confidence:0.2f}"
                for _, _, confidence, class_id, tracker_id
                in detections
            ]

            frame = self.box_annotator.annotate(scene=frame,
                                                detections=detections,
                                                labels=labels)

            cv2.imshow('frame', frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        cap.release()
        cv2.destroyAllWindows()

if __name__ == '__main__':
    det_track = DetectorTracker()
    det_track.run()


from datetime import datetime
from typing import List, Optional, Tuple, cast

import pkg_resources
import structlog
from flask import Flask
from flask_restful import Resource
from gevent.pywsgi import WSGIServer
from prometheus_client import make_wsgi_app
from werkzeug.exceptions import NotFound
from werkzeug.middleware.dispatcher import DispatcherMiddleware

from monitoring_service import metrics
from monitoring_service.constants import API_PATH, DEFAULT_INFO_MESSAGE
from monitoring_service.service import MonitoringService
from raiden_libs.api import ApiWithErrorHandler
from raiden_libs.utils import to_checksum_address

log = structlog.get_logger(__name__)

class MSResource(Resource):
    def __init__(self, monitoring_service: MonitoringService, api: "MSApi"):
        self.monitoring_service = monitoring_service
        self.service_token_address = (
            self.monitoring_service.context.user_deposit_contract.functions.token().call()
        )
        self.api = api

class InfoResource(MSResource):
    version = pkg_resources.get_distribution("raiden-services").version
    contracts_version = pkg_resources.get_distribution("raiden-contracts").version

    def get(self) -> Tuple[dict, int]:
        info = {
            "price_info": self.api.monitoring_service.context.min_reward,
            "network_info": {
                "chain_id": self.monitoring_service.chain_id,
                "token_network_registry_address": to_checksum_address(
                    self.monitoring_service.context.ms_state.blockchain_state.token_network_registry_address  # noqa
                ),
                "user_deposit_address": to_checksum_address(
                    self.monitoring_service.context.user_deposit_contract.address
                ),
                "service_token_address": to_checksum_address(self.service_token_address),
                "confirmed_block": {
                    "number": self.monitoring_service.context.ms_state.blockchain_state.latest_committed_block  # noqa
                },
            },
            "version": self.version,
            "contracts_version": self.contracts_version,
            "operator": self.api.operator,
            "message": self.api.info_message,
            "UTC": datetime.utcnow().isoformat(),
        }
        return info, 200

class InfoResource2(MSResource):
    version = pkg_resources.get_distribution("raiden-services").version
    contracts_version = pkg_resources.get_distribution("raiden-contracts").version

    def get(self) -> Tuple[dict, int]:
        info = {
            "price_info": str(self.api.monitoring_service.context.min_reward),
            "network_info": {
                "chain_id": self.monitoring_service.chain_id,
                "token_network_registry_address": to_checksum_address(
                    self.monitoring_service.context.ms_state.blockchain_state.token_network_registry_address  # noqa
                ),
                "user_deposit_address": to_checksum_address(
                    self.monitoring_service.context.user_deposit_contract.address
                ),
                "service_token_address": to_checksum_address(self.service_token_address),
                "confirmed_block": {
                    "number": str(
                        self.monitoring_service.context.ms_state.blockchain_state.latest_committed_block  # noqa
                    )
                },
            },
            "version": self.version,
            "contracts_version": self.contracts_version,
            "operator": self.api.operator,
            "message": self.api.info_message,
            "UTC": datetime.utcnow().isoformat(),
        }
        return info, 200

class MSApi:
    def __init__(
        self,
        monitoring_service: MonitoringService,
        operator: str,
        info_message: str = DEFAULT_INFO_MESSAGE,
    ) -> None:
        flask_app = Flask(__name__)
        self.api = ApiWithErrorHandler(flask_app)

        # Add the metrics prometheus app
        self.flask_app = DispatcherMiddleware(
            NotFound(),
            {
                "/metrics": make_wsgi_app(registry=metrics.REGISTRY),
                API_PATH: flask_app.wsgi_app,
            },
        )

        self.rest_server: Optional[WSGIServer] = None

        self.monitoring_service = monitoring_service
        self.operator = operator
        self.info_message = info_message

        resources: List[Tuple[str, Resource, str]] = [
            ("/v1/info", cast(Resource, InfoResource), "info"),
            ("/v2/info", cast(Resource, InfoResource2), "info2"),
        ]

        for endpoint_url, resource, endpoint in resources:
            self.api.add_resource(
                resource,
                endpoint_url,
                resource_class_kwargs={"monitoring_service": monitoring_service, "api": self},
                endpoint=endpoint,
            )

    def run(self, host: str, port: int) -> None:
        self.rest_server = WSGIServer((host, port), self.flask_app)
        self.rest_server.start()

        log.info("Running endpoint", endpoint=f"http://{host}:{port}")

    def stop(self) -> None:
        if self.rest_server:
            self.rest_server.stop()


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

import os
import time
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPRegressor
from toy_example import measure_time, load_from_csv, build_rating_matrix, make_submission
from learning_set_creation import create_learning_matrices2
import numpy as np
import random
from sklearn.metrics import mean_squared_error

def y_check(y):
    for i in range(len(y)):
        if y[i] > 5:
            y[i] = 5
        if y[i] < 0:
            y[i] = 0
    return y

add_noise = lambda x: x + random.uniform(-x/10, x/10)
v_noise = np.vectorize(add_noise)

if __name__ == '__main__':
    prefix = 'data/'

    # ------------------------------- Learning ------------------------------- #
    # Load training data
    training_user_movie_pairs = load_from_csv(os.path.join(prefix,
                                                           'data_train.csv'))
    training_labels = load_from_csv(os.path.join(prefix, 'output_train.csv'))
    user_movie_rating_triplets = np.hstack((training_user_movie_pairs,
                                            training_labels.reshape((-1, 1))))
    # Build the learning matrix
    rating_matrix = build_rating_matrix(user_movie_rating_triplets)
    X = create_learning_matrices2(rating_matrix, training_user_movie_pairs)

    # Build the model
    y = training_labels
    X_ls2, X_vs, y_ls2, y_vs = train_test_split(X, y, train_size=.8, random_state=20)
    # Creation of the learning and validation set.
    X_untouch = X_ls2.copy()
    scaler = StandardScaler()
    start = time.time()
    models = []
    n_estimator = 50
    y_pred = np.zeros((n_estimator,y_vs.shape[0]))
    with measure_time("Bagging training"):
        for i in range(n_estimator):
            # Training
            model = MLPRegressor(hidden_layer_sizes=(100, 50, 50), random_state=i)
            X_ls2 = v_noise(X_ls2)
            scaler.fit(X_ls2)
            X_ls2=scaler.transform(X_ls2)
            model.fit(X_ls2, y_ls2)
            models.append((model, scaler))
            X_t = scaler.transform(X_vs, copy=True)
            y_pred[i, :] = model.predict(X_t)
            print(model.predict(X_t))
            X_ls2 = X_untouch.copy()

    means = np.mean(y_pred, axis=0)
    print(mean_squared_error(y_vs, means))

    # ------------------------------ Prediction ------------------------------ #
    # Load test data
    test_user_movie_pairs = load_from_csv(os.path.join(prefix, 'data_test.csv'))

    # # Build the prediction matrix
    X_ts = create_learning_matrices2(rating_matrix, test_user_movie_pairs)
    X_ts = scaler.transform(X_ts)

    # Predict
    print(X_ts.shape)
    y_pred = np.zeros((n_estimator, X_ts.shape[0]))
    for i in range(n_estimator):
        y_pred[i, :] = models[i][0].predict(models[i][1].transform(X_ts))
    y_pred = np.mean(y_pred, axis=0)
    print(y_pred)

    y_pred = y_check(y_pred)
    # Making the submission file
    fname = make_submission(y_pred, test_user_movie_pairs, 'NLP_bagging' )
    print('Submission file "{}" successfully written'.format(fname))


import sys, os
import logging, traceback
import pandas as pd
import re
import collections

logging.basicConfig(
    filename=snakemake.log[0],
    level=logging.INFO,
    format="%(asctime)s %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
)

logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())

def handle_exception(exc_type, exc_value, exc_traceback):
    if issubclass(exc_type, KeyboardInterrupt):
        sys.__excepthook__(exc_type, exc_value, exc_traceback)
        return

    logger.error(
        "".join(
            [
                "Uncaught exception: ",
                *traceback.format_exception(exc_type, exc_value, exc_traceback),
            ]
        )
    )

# Install exception handler
sys.excepthook = handle_exception

sectors_common_aggreg = {
    sheet_name: pd.read_excel(
        snakemake.config["sectors_common_aggreg"], sheet_name=sheet_name, index_col=0
    )
    for sheet_name in [
        "eora26_without_reexport_to_common_aggreg",
        "euregio_to_common_aggreg",
        "exiobase_full_to_common_aggreg",
        "icio2021_reworked_to_common_aggreg",
    ]
}

regions_common_aggreg = {
    sheet_name: pd.read_excel(
        snakemake.config["regions_common_aggreg"], sheet_name=sheet_name, index_col=0
    )
    for sheet_name in [
        "eora26_without_reexport_to_common_aggreg",
        "euregio_to_common_aggreg",
        "exiobase_full_to_common_aggreg",
        "icio2021_reworked_to_common_aggreg",
    ]
}

def load_regions_aggreg(mrio_name):
    if "eora26" in mrio_name:
        return regions_common_aggreg["eora26_without_reexport_to_common_aggreg"]
    elif "euregio" in mrio_name:
        return regions_common_aggreg["euregio_to_common_aggreg"]
    elif "exiobase" in mrio_name:
        return regions_common_aggreg["exiobase_full_to_common_aggreg"]
    elif "oecd" in mrio_name:
        return regions_common_aggreg["icio2021_reworked_to_common_aggreg"]
    else:
        raise ValueError(f"Invalid MRIO name: {mrio_name}")

def load_sectors_aggreg(mrio_name):
    if "eora26" in mrio_name:
        return sectors_common_aggreg["eora26_without_reexport_to_common_aggreg"]
    elif "euregio" in mrio_name:
        return sectors_common_aggreg["euregio_to_common_aggreg"]
    elif "exiobase" in mrio_name:
        return sectors_common_aggreg["exiobase_full_to_common_aggreg"]
    elif "oecd" in mrio_name:
        return sectors_common_aggreg["icio2021_reworked_to_common_aggreg"]
    else:
        raise ValueError(f"Invalid MRIO name: {mrio_name}")

def aggreg_df(df, mrio_name):
    regions_aggreg = load_regions_aggreg(mrio_name)
    _df = df.copy()
    regions_aggreg = load_regions_aggreg(mrio_name)
    _df.rename(regions_aggreg["new region"].to_dict(), axis=1, level=0, inplace=True)
    _df = _df.groupby(["region", "sector"], axis=1).sum()
    sectors_aggreg = load_sectors_aggreg(mrio_name)
    _df.rename(sectors_aggreg["new sector"].to_dict(), axis=1, level=1, inplace=True)
    _df = _df.groupby(["region", "sector"], axis=1).sum()
    return _df

def common_monetary_factor(df, mrio_basename):
    _df = df.copy()
    _df = _df * (snakemake.config["monetary_factor"][mrio_basename] / 10**6)
    return _df

def create_result_dict(inputs):
    xp_regex = re.compile(
        r"results/simulations/mrio~(?P<mrio_basename>[\w]+)_(?P<year>\d\d\d\d)(?:_ixi)?/sectors_scenario~(?P<sectors_scenario>[^/]+)/recovery_scenario~(?P<recovery_scenario>[^/]+)/flood_scenario~(?P<flood_scenario>[^/]+)/order~(?P<order>[^/]+)_psi~(?P<psi>[^/]+)_base_alpha~(?P<base_alpha>[^/_]+)_max_alpha~(?P<max_alpha>[^/_]+)_tau_alpha~(?P<tau_alpha>[^/]+)"
    )
    res_dict = {}
    for key, val in inputs.items():
        for xp_file in val:
            match = xp_regex.match(xp_file)
            if match:
                df = pd.read_parquet(xp_file)
                df = aggreg_df(df, match.group("mrio_basename"))
                df = common_monetary_factor(df, match.group("mrio_basename"))
                res_dict[
                    (
                        key,
                        match.group("mrio_basename"),
                        match.group("year"),
                        match.group("sectors_scenario"),
                        match.group("recovery_scenario"),
                        match.group("flood_scenario"),
                        match.group("order"),
                        match.group("psi"),
                        match.group("base_alpha"),
                        match.group("max_alpha"),
                        match.group("tau_alpha"),
                    )
                ] = df
            else:
                ValueError(
                    f"Unmatched file, verify regex ?\n {val} to match with {xp_regex}"
                )

    return res_dict

logger.info(f"Starting creation of all results dictionary.")
res = create_result_dict(snakemake.input)
logger.info(f"Saving to {snakemake.output[0]}")
pd.concat(
    res,
    keys=res.keys(),
    names=[
        "variable",
        "mrio",
        "mrio year",
        "sectors_sce",
        "recovery_sce",
        "flood_sce",
        "order type",
        "psi",
        "alpha_base",
        "alpha_max",
        "alpha_tau",
    ],
).to_parquet(snakemake.output[0])