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

import numpy as np

import theano.tensor as T
from theano.tensor.nnet import conv
from theano.tensor.signal import downsample

from breze.arch.component import transfer as _transfer, loss as _loss
from breze.arch.construct.base import Layer
from breze.arch.util import lookup

class AffineNonlinear(Layer):

    @property
    def n_inpt(self):
        return self._n_inpt

    @property
    def n_output(self):
        return self._n_output

    def __init__(self, inpt, n_inpt, n_output, transfer='identity',
                 use_bias=True, declare=None, name=None):
        self.inpt = inpt
        self._n_inpt = n_inpt
        self._n_output = n_output
        self.transfer = transfer
        self.use_bias = use_bias
        super(AffineNonlinear, self).__init__(declare=declare, name=name)

    def _forward(self):
        self.weights = self.declare((self.n_inpt, self.n_output))

        self.output_in = T.dot(self.inpt, self.weights)

        if self.use_bias:
            self.bias = self.declare(self.n_output)
            self.output_in += self.bias

        f = lookup(self.transfer, _transfer)

        self.output = f(self.output_in)

class Split(Layer):

    def __init__(self, inpt, lengths, axis=1, declare=None, name=None):
        self.inpt = inpt
        self.lengths = lengths
        self.axis = axis
        super(Split, self).__init__(declare, name)

    def _forward(self):
        starts = [0] + np.add.accumulate(self.lengths).tolist()
        stops = starts[1:]
        starts = starts[:-1]

        self.outputs = [self.inpt[:, start:stop] for start, stop
                        in zip(starts, stops)]

class Concatenate(Layer):

    def __init__(self, inpts, axis=1, declare=None, name=None):
        self.inpts = inpts
        self.axis = axis
        super(Concatenate, self).__init__(declare, name)

    def _forward(self):
        concatenated = T.concatenate(self.inpts, self.axis)
        self.output = concatenated

class SupervisedLoss(Layer):

    def __init__(self, target, prediction, loss, comp_dim=1, imp_weight=None,
                 declare=None, name=None):
        self.target = target
        self.prediction = prediction
        self.loss_ident = loss

        self.imp_weight = imp_weight
        self.comp_dim = comp_dim

        super(SupervisedLoss, self).__init__(declare, name)

    def _forward(self):
        f_loss = lookup(self.loss_ident, _loss)

        self.coord_wise = f_loss(self.target, self.prediction)

        if self.imp_weight is not None:
            self.coord_wise *= self.imp_weight

        self.sample_wise = self.coord_wise.sum(self.comp_dim)

        self.total = self.sample_wise.mean()

class Conv2d(Layer):

    def __init__(self, inpt, inpt_height, inpt_width, n_inpt,
                 filter_height, filter_width,
                 n_output, transfer='identity',
                 n_samples=None,
                 subsample=(1, 1),
                 declare=None, name=None):
        self.inpt = inpt
        self.inpt_height = inpt_height
        self.inpt_width = inpt_width
        self.n_inpt = n_inpt

        self.filter_height = filter_height
        self.filter_width = filter_width

        self.n_output = n_output
        self.transfer = transfer
        self.n_samples = n_samples
        self.subsample = subsample

        # self.output_height, _ = divmod(inpt_height, filter_height)
        # self.output_width, _ = divmod(inpt_width, filter_width)
        self.output_height = (inpt_height - filter_height) / subsample[0] + 1
        self.output_width = (inpt_width - filter_width) / subsample[1] + 1

        if not self.output_height > 0:
            raise ValueError('inpt height smaller than filter height')
        if not self.output_width > 0:
            raise ValueError('inpt width smaller than filter width')

        super(Conv2d, self).__init__(declare=declare, name=name)

    def _forward(self):
        self.weights = self.declare((
            self.n_output, self.n_inpt,
            self.filter_height, self.filter_width))
        self.bias = self.declare((self.n_output,))

        self.output_in = conv.conv2d(
            self.inpt, self.weights,
            image_shape=(
                self.n_samples, self.n_inpt, self.inpt_height, self.inpt_width),
            subsample=self.subsample,
            border_mode='valid',
            )

        f = lookup(self.transfer, _transfer)
        self.output = f(self.output_in)

class MaxPool2d(Layer):

    def __init__(self, inpt, inpt_height, inpt_width, pool_height, pool_width,
                 n_output,
                 transfer='identity',
                 declare=None, name=None):
        self.inpt = inpt
        self.inpt_height = inpt_height
        self.inpt_width = inpt_width
        self.pool_height = pool_height
        self.pool_width = pool_width
        self.transfer = transfer

        self.output_height, _ = divmod(inpt_height, pool_height)
        self.output_width, _ = divmod(inpt_width, pool_width)

        if not self.output_height > 0:
            raise ValueError('inpt height smaller than pool height')
        if not self.output_width > 0:
            raise ValueError('inpt width smaller than pool width')

        self.n_output = n_output

        super(MaxPool2d, self).__init__(declare=declare, name=name)

    def _forward(self):
        self.output_in = downsample.max_pool_2d(
            input=self.inpt, ds=(self.pool_height, self.pool_width),
            ignore_border=True)

        f = lookup(self.transfer, _transfer)
        self.output = f(self.output_in)


"""
This contains the Content implementation for GitLab.
"""
from typing import Optional
from typing import Union
from urllib.parse import quote_plus

from IGitt.GitLab import GitLabMixin
from IGitt.GitLab import GitLabOAuthToken
from IGitt.GitLab import GitLabPrivateToken
from IGitt.Interfaces import delete
from IGitt.Interfaces import get
from IGitt.Interfaces import put
from IGitt.Interfaces.Content import Content

class GitLabContent(GitLabMixin, Content):
    """
    This class represents a content on GitHub
    """
    def __init__(self,  token: Union[GitLabOAuthToken, GitLabPrivateToken],
                 repository: str, path: str):
        self._token = token
        self._repository = repository
        self._url = ('/projects/' + quote_plus(repository) +
                     '/repository/files/' + path)

    def get_content(self, ref='master'):
        data = {
            'path': self._url,
            'ref': ref
        }
        self.data = get(token=self._token, url=self.url, params=data)

    def delete(self, message: str, branch: Optional[str]=None):
        """
        Deletes content

        :param message: The commit message for the deletion commit.
        :param branch:  The branch to delete this content from. Defaults to
                        `master`.
        """
        if branch is None:
            branch = 'master'

        data = {
            'file_path': self._url,
            'branch': branch,
            'commit_message': message
        }
        delete(self._token, url=self.url, params=data)

    def update(self, message: str, content: str, branch: Optional[str]=None):

        if branch is None:
            branch = 'master'

        data = {
            'file_path': self._url,
            'commit_message': message,
            'branch': branch,
            'content' : content
        }
        put(token=self._token, url=self.url, data=data)


# https://school.programmers.co.kr/learn/courses/30/lessons/118666

def solution(survey, choice):
    characters = (("R","T"),("C","F"),("J","M"),("A","N"))

    scores = {c1:0 for c2 in characters for c1 in c2}

    for s, c in zip(survey, choice):
        scores[s[0]] -= c-4
        scores[s[1]] += c-4

    answer = [c1 if scores[c1]>=scores[c2]else c2 for c1,c2 in characters]

    return ''.join(answer)

if __name__ == "__main__":
    param = {
        "survey": ["AN", "CF", "MJ", "RT", "NA"],
		"choice": [5, 3, 2, 7, 5]
    }
    result = "TCMA"
    print(solution(**param), result)


import numpy as np
from numpy.linalg import eigh, svd
from scipy.linalg import orth
from scipy.stats import ortho_group

from SH import spectral_hashing
from utilities import normalize_Z

def gram_schmidt_orthogonalization(U, num_vectors=1, random_state=42):
    """
    This function implements gram schmidt orthogonalization method
    to compute num_vectors new vectors that are orthogonal  to the
    columns of U. The function returns [U, U_hat] that U_hat has
    num_vectors columns.
    Inputs:
        U: a (N, dim) matrix
        num_vectors: how many times this orthogonalization have to execute
    Output:
        concatenation (column-wise) of U and num_vectors columns.
    """
    def project_v_on_u(v,u):
        """
        This operator projects the vector v orthogonally onto the line spanned by
        vector u. If u = 0, projection v on 0 is defined 0.
        """
        assert u.shape == v.shape
        assert len(u.shape) == 1
        if np.all(u==0):
            return u
        scalar = np.dot(v,u)/np.dot(u,u)
        return scalar*u

    N, dim = U.shape
    dtype = U.dtype
    R = np.random.RandomState(random_state)
    U_new = np.concatenate([U, np.zeros((N, num_vectors), dtype=dtype)], axis=1)
    U_hat = np.array(R.randn(N, num_vectors), dtype=dtype) # initialization
    for i in range (num_vectors):
        v = U_hat[:, i]
        for j in range(dim):
            v -= project_v_on_u(v, U_new[:, j])
        if np.any(v!=0):
            v /= np.linalg.norm(v)
        U_new[:, dim] = v
        dim += 1
    return U_new

def DGH(Z, K=16, rho=0.1, TB=300, TG=20, B=None, Y=None, random_state=42, verbose=1):
    """
    This function implements descrete graph hashing method (2014). For more
    details, see the corresponding paper.
    Inputs:
        Z: data-to-anchor mapping of train data. shape is (n_samples, n_anchors)
        K: Number of bits for each binary code
        rho:tuning parameter (see equation 3 of the paper)
        TB: iteration numbers of optimizing the B-subproblem
        TG: iteration numbers of optimizing the whole DGH problem
        B: initialization of B matrix. If None, DGH-I initialization is used
        Y: initialization of Y matrix. If None, DGH-I initialization is used
        random_state: used for initialization of gram schmidt orthogonalization
        verbose: 1 or 0. If 1, it prints some information about early convergence
                 and rank of JB matrix in Y-subproblem
    Outputs:
        B: learned binary codes. The values are [-1,1]. shape is (n_samples, K)
        Y: relaxed version of B (with real values). shape is (n_samples, K)
    """
    n_samples = Z.shape[0]
    eps = 1e-4 # controls convergence
    if Y is None:
        Y = spectral_hashing(Z, K=K, thresholding=None)
    if B is None:
        B = np.sign(Y)
    # normalize Z
    Z = normalize_Z(Z)

    def B_subproblem(B, Y, L, rho=0.1, TB=300):
        """
        signed gradient method (SGM) for B-subproblem (see the algorithm 1 of
        the paper).
        Note that L = Z.T@B is computed once in the objective function and we
            brought it here for efficency reasons.
        """
        for t in range(TB):
            B_conv = B.copy() # used to check convergence
            grad = 2*Z@L + rho*Y
            B = np.sign(np.where(np.abs(grad)>0, grad, B))
            # check convergence
            if np.all(B==B_conv)==True:
                if verbose:
                    print(f"B_subproblem converged early in {t+1} iteration")
                return B
        return B

    def Y_subproblem(B, tg, K=8, random_state=42):
        # tg is iteration number starting from 0
        B = B - np.mean(B, axis=0) # equivalent to JB (see paper)
        M = B.T@B
        eig_vals, V = eigh(M)
        eig_vals, V = eig_vals[::-1], V[:, ::-1]
        r = np.argmin((eig_vals/eig_vals[0])>eps) # rank of M
        if r==0:
            r = K
            if verbose:
                print(f"The rank of JB matrix is {r} (full-rank) in Y"
                      f" subproblem and in iteration {tg+1}")
        else:
            if verbose:
                print(f"The rank of JB matrix is {r} in Y subproblem and"
                      f" in iteration {tg+1}")
        sigma_inv = np.diag(1/np.sqrt(eig_vals[:r]))
        U = B@V[:,:r]@sigma_inv
        if r==K: # full-rank case
            return np.sqrt(n_samples)*(U@V.T)
        # If the M is not full rank, we must generate U_hat
        U = np.concatenate([U, np.ones((n_samples,1), dtype=U.dtype)], axis=1)
        U_new = gram_schmidt_orthogonalization(U, num_vectors=K-r,
                                               random_state=random_state)
        idx = np.setdiff1d(np.arange(K+1), r)
        return np.sqrt(n_samples)*(U_new[:, idx]@V.T)

    def objective_func(B, Y, rho=0.1):
        L = Z.T@B
        return np.trace(L.T@L) + rho*np.trace(B.T@Y), L

    Q_new, L = objective_func(B, Y, rho=rho)
    for tg in range(TG):
        Q_old = Q_new
        # B subproblem
        B = B_subproblem(B, Y, L, rho=rho, TB=TB)
        # Y subproblem
        Y = Y_subproblem(B, tg, K=K, random_state=random_state)
        # check convergence
        Q_new, L = objective_func(B, Y, rho=rho)
        if ((Q_new-Q_old)/Q_old)<eps:
            if verbose:
                print(f"The DGH problem converged early in {tg+1} iterations")
            return B, Y
    return B, Y

def SHSR(Z, K=16, n_iter=20, Q=None, random_state=42, verbose=1):
    """
    This function implements Spectral Hashing with Spectral Rotation (SHSR) method.
    For more details, see "Discrete Spectral Hashing for Efficient Similarity
    Retrieval" paper (section III).
    Inputs:
        Z: data-to-anchor mapping of train data. shape is (n_samples, n_anchors)
        K: Number of bits for each binary code
        n_iter: maximum number of iterations.
        Q: initialization of Q matrix. If None, A random orthogonal matrix is used
        random_state: used for initialization of Q (if it is None)
        verbose: 1 or 0. If 1, it prints some information about early convergence
    Outputs:
        B: learned binary codes. The values are [-1,1]. shape is (n_samples, K)
        F: output of spectral hashing method. shape is (n_samples, K)
        Q: learned orthogonal matrix. shape is (K, K)
    """
    n_samples = Z.shape[0]
    # apply spectral hashing
    F = spectral_hashing(Z, K=K, thresholding=None)
    Error = 1000 # used for convergence check

    def B_subproblem(F, Q):
        M = F@Q
        half =  n_samples//2
        Idx = np.argsort(-M, axis=0)
        B = np.zeros((n_samples, K), dtype=np.float32)
        for i in range(K):
            B[Idx[:half, i], i] = 1.
            B[Idx[half:, i], i] = -1.
        return B

    def Q_subproblem(F, B):
        G = B.T@F
        U, diag_mat, V_transpose = svd(G)
        V = V_transpose.T
        Q = V@U.T
        return Q

    # initialization of Q
    if Q is None:
        Q = ortho_group.rvs(K, random_state=random_state)
    assert Q.shape == (K, K)
    B = B_subproblem(F, Q)

    for it in range(n_iter):
        B_old = B
        Q = Q_subproblem(F, B)
        B = B_subproblem(F, Q)
        # convergence check
        Error = np.mean(np.abs(B-B_old))
        if verbose>0:
            print(f"Mean absolute error is {Error} after {it+1} iterations")
        if Error < 1e-4:
            if verbose>0:
                print(f"\nThe SHSR problem converged early in {it+1} iterations")
            return B, F, Q
    return B, F, Q

def DSH(Z, K=16, alpha=0.1, F=None, n_iter=30, Ng_iter=30, random_state=42, verbose=1):
    """
    This function implements DISCRETE SPECTRAL HASHING (DSH) method.
    For more details, see "Discrete Spectral Hashing for Efficient Similarity
    Retrieval" paper (section V).
    Inputs:
        Z: data-to-anchor mapping of train data. shape is (n_samples, n_anchors)
        K: Number of bits for each binary code
        alpha: regularization term for quantization loss
        F: initialization of F matrix. If None, A random orthogonal matrix is used
        n_iter: maximum number of iterations.
        Ng_iter: maximum number of iterations of Generalized Power method
        random_state: used for initialization of F
        verbose: 1 or 0. If 1, it prints some information about early convergence
    Outputs:
        B: learned binary codes. The values are [-1,1]. shape is (n_samples, K)
    """
    n_samples = Z.shape[0]
    # normalize Z
    Z = normalize_Z(Z)

    def F_subproblem(Z, B, F, Ng_iter):
        # this subprolem is solved using Generalized Power Iteration (GPI)4
        for it_g in range(Ng_iter):
            F_old = F
            M = 2*(Z@(Z.T@F)) + 2*alpha*B
            U, diag_mat, V_transpose = svd(M, full_matrices=False)
            F = U@(np.sqrt(n_samples)*V_transpose)
            Error = np.mean(np.abs(F-F_old))
            if Error < 1e-4:
                if verbose>0:
                    print("early convergence of Generalized Power Iteration "\
                              f"algorithm in {it_g+1} iteration")
                return F
        return F

    def B_subproblem(F):
        half =  n_samples//2
        Idx = np.argsort(-F, axis=0)
        B = np.zeros((n_samples, K), dtype=np.float32)
        for i in range(K):
            B[Idx[:half, i], i] = 1.
            B[Idx[half:, i], i] = -1.
        return B

    # F initialization
    if F is None:
        R = np.random.RandomState(random_state)
        F = np.float32(orth(R.rand(n_samples, K)))
        assert F.shape==(n_samples, K)
    #  initialization
    B = B_subproblem(F)
    # main loop
    for it in range(n_iter):
        if verbose>0:
            print(f"iteration {it+1}:")
        B_old = B
        F = F_subproblem(Z, B, F, Ng_iter)
        B = B_subproblem(F)
        # convergence check
        Error = np.mean(np.abs(B-B_old))
        if verbose>0:
            print(f"Mean absolute error is {Error}")
        if Error < 1e-4:
            if verbose>0:
                print(f"\nThe DSH problem converged early in {it+1} iterations")
            return B
    return B


import cv2
import mediapipe as mp

mp_draw = mp.solutions.drawing_utils
mp_hand= mp.solutions.hands

video=cv2.VideoCapture(0)
with mp_hand.Hands(min_detection_confidence=0.5,
 min_tracking_confidence=0.5) as hands:
 while True:
  ret,image = video.read()
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  image.flags.writeable=False
  results = hands.process(image)
  image.flags.writeable=True
  cv2.imshow("Frame",image)
  k = cv2.waitKey(1)
  if k == ord("q"):
      break
video.release()
cv2.destroyAllWindows()


from sys import stdin
n = stdin.readline()
N = sorted(map(int,stdin.readline().split()))
m = stdin.readline()
M = map(int, stdin.readline().split())
def binary_search(N, targetNum):
    start = 0
    end = len(N)-1 # index는 0부터 시작하기 때문
    while start <= end:
        midIndex = (start+end)//2 # 무한루프가 될 수 있어서 start+end로 함(범위가 계속 바뀌어야 하기 때문)
        indexValue = N[midIndex]
        if indexValue == targetNum:
            return 1
        elif indexValue < targetNum:
            start = midIndex + 1 # midIndex까지는 실행했으므로 +1 함
        elif indexValue > targetNum:
            end = midIndex - 1 # midIndex까지는 실행했으므로 -1 함
    return 0
for targetNum in M:
    print(binary_search(N, targetNum))


"""
This file implement PPO algorithm.

You need to implement `compute_loss` function.

-----
*2020-2021 Term 1, IERG 5350: Reinforcement Learning. Department of Information Engineering, The Chinese University of
Hong Kong. Course Instructor: Professor ZHOU Bolei. Assignment author: PENG Zhenghao, SUN Hao, ZHAN Xiaohang.*
"""
import numpy as np
import torch
import torch.nn.functional as F
import torch.optim as optim

from .base_trainer import BaseTrainer
from .buffer import MyPPORolloutStorage as PPORolloutStorage

from os.path import join as pjoin

class PPOConfig:
    """Not like previous assignment where we use a dict as config, here we
    build a class to represent config."""

    def __init__(self):
        # Common
        self.device = torch.device(
            "cuda:1" if torch.cuda.is_available() else "cpu")
        self.eval_freq = 100
        self.save_freq = 50
        self.log_freq = 10
        self.num_envs = 1     # dont't change

        # Sample
        self.num_steps = 450  # num_steps * num_envs = sample_batch_size
        self.state_dim = 128  # dont't change

        # Learning
        self.gamma = 0.95
        self.lr = 5e-4
        self.grad_norm_max = 50.0
        self.entropy_loss_weight = 0.01
        self.ppo_epoch = 10
        self.mini_batch_size = 256
        self.ppo_clip_param = 0.1
        self.USE_GAE = True
        self.gae_lambda = 0.9
        self.value_loss_weight = 1.0
        # self.value_loss_weight = 0.5

        self.project_root = "/home/jb/projects/Code/IERG5350/project/ierg5350_rl_course_project/"
        # self.encoder_ckpt = None
        self.encoder_ckpt = pjoin(self.project_root, "multimodal/runs/2020-12-12-13-19/ckpt_encoder_best.pth")

ppo_config = PPOConfig()

class PPOTrainer(BaseTrainer):
    def __init__(self, env, config):
        super(PPOTrainer, self).__init__(env, config)

        # There configs are only used in PPO
        self.num_sgd_steps = config.ppo_epoch
        self.mini_batch_size = config.mini_batch_size
        self.num_mini_batches = int(
            self.num_steps // self.mini_batch_size)
        self.clip_param = config.ppo_clip_param

    def setup_optimizer(self):
        self.optimizer = optim.Adam(self.model.parameters(), lr=self.lr, eps=1e-5)

    def setup_rollouts(self):
        act_dim = 1 if self.discrete else self.num_actions
        self.rollouts = PPORolloutStorage(self.num_steps, self.num_envs,
                                          [(224, 224, 3), (224, 224, 1), (6, 1)],
                                          act_dim, self.device,
                                          self.config.USE_GAE, self.config.gae_lambda)

    def compute_loss(self, sample):
        """Compute the loss of PPO"""
        observations_batch, actions_batch, return_batch, masks_batch, \
        old_action_log_probs_batch, adv_targ = sample

        assert old_action_log_probs_batch.shape == (self.mini_batch_size, 1)
        assert adv_targ.shape == (self.mini_batch_size, 1)
        assert return_batch.shape == (self.mini_batch_size, 1)

        values, action_log_probs, dist_entropy = self.evaluate_actions(
            observations_batch, actions_batch)

        assert values.shape == (self.mini_batch_size, 1)
        assert action_log_probs.shape == (self.mini_batch_size, 1)
        assert values.requires_grad
        assert action_log_probs.requires_grad
        assert dist_entropy.requires_grad

        # [TODO] Implement policy loss
        ratio = torch.exp(action_log_probs.float() - old_action_log_probs_batch.float())
        surr1 = ratio * adv_targ
        surr2 = torch.clamp(ratio, min=1.0 - self.clip_param, max=1.0 + self.clip_param) * adv_targ

        policy_loss = - torch.min(surr1, surr2).mean()

        # [TODO] Implement value loss
        # value_loss = F.mse_loss(return_batch, values)
        value_loss = 0.5 * (return_batch - values).pow(2).mean()

        # This is the total loss
        loss = policy_loss + self.config.value_loss_weight * value_loss - self.config.entropy_loss_weight * dist_entropy

        return loss, policy_loss, value_loss, dist_entropy

    def update(self, rollout):
        # Get the normalized advantages
        advantages = rollout.returns[:-1] - rollout.value_preds[:-1]
        advantages = (advantages - advantages.mean()) / (advantages.std() + 1e-5)

        value_loss_epoch = []
        policy_loss_epoch = []
        dist_entropy_epoch = []
        total_loss_epoch = []

        # Train for num_sgd_steps iterations (compared to A2C which only
        # train one iteration)
        for e in range(self.num_sgd_steps):
            data_generator = rollout.feed_forward_generator(advantages, self.mini_batch_size)

            for sample in data_generator:
                total_loss, policy_loss, value_loss, dist_entropy = self.compute_loss(sample)
                self.optimizer.zero_grad()
                total_loss.backward()
                torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.config.grad_norm_max)
                self.optimizer.step()

                value_loss_epoch.append(value_loss.item())
                policy_loss_epoch.append(policy_loss.item())
                total_loss_epoch.append(total_loss.item())
                dist_entropy_epoch.append(dist_entropy.item())

        return np.mean(policy_loss_epoch), np.mean(value_loss_epoch), \
               np.mean(dist_entropy_epoch), np.mean(total_loss_epoch)


import sys
from datetime import datetime, timezone

from loaders.loader_base import LoaderBase
from model.jobs import Job
from providers.gmail import Gmail

if __name__ == '__main__':
    start_time = float(sys.argv[1])
    email_body = ''
    jobs: [Job] = LoaderBase.get_jobs_since(datetime.fromtimestamp(start_time, timezone.utc))
    for job in jobs:
        job_desc = job.started.strftime('%Y-%m-%d %H:%M:%S') + ' ' \
                   + (job.completed.strftime('%Y-%m-%d %H:%M:%S') if job.completed else 'failed') + ' ' \
                   + str(job.job_info) + ' ' \
                   + job.provider.name + ':' + job.job_type.name + '\n'
        email_body += job_desc
    Gmail.send(subject='Daily Dose of Tariel', content=email_body)

    print('email_body: ' + email_body)


"""
.. module:: CDataSplitterKFold
   :synopsis: K-Fold splitting

.. moduleauthor:: Ambra Demontis <ambra.demontis@unica.it>
.. moduleauthor:: Marco Melis <marco.melis@unica.it>

"""
from sklearn.model_selection import KFold

from secml.array import CArray
from secml.data.splitter import CDataSplitter

class CDataSplitterKFold(CDataSplitter):
    """K-Folds dataset splitting.

    Provides train/test indices to split data in train and test sets.
    Split dataset into 'num_folds' consecutive folds (with shuffling).

    Each fold is then used a validation set once while the k - 1
    remaining fold form the training set.

    Parameters
    ----------
    num_folds : int, optional
        Number of folds to create. Default 3.
        This correspond to the size of tr_idx and ts_idx lists.
    random_state : int, RandomState instance or None, optional (default=None)
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, is the RandomState instance used by np.random.

    Attributes
    ----------
    class_type : 'kfold'

    Examples
    --------
    >>> from secml.data import CDataset
    >>> from secml.data.splitter import CDataSplitterKFold

    >>> ds = CDataset([[1,2],[3,4],[5,6]],[1,0,1])
    >>> kfold = CDataSplitterKFold(num_folds=3, random_state=0).compute_indices(ds)
    >>> print(kfold.num_folds)
    3
    >>> print(kfold.tr_idx)
    [CArray(2,)(dense: [0 1]), CArray(2,)(dense: [0 2]), CArray(2,)(dense: [1 2])]
    >>> print(kfold.ts_idx)
    [CArray(1,)(dense: [2]), CArray(1,)(dense: [1]), CArray(1,)(dense: [0])]

    """
    __class_type = 'kfold'

    def __init__(self, num_folds=3, random_state=None):

        super(CDataSplitterKFold, self).__init__(
            num_folds=num_folds, random_state=random_state)

    def compute_indices(self, dataset):
        """Compute training set and test set indices for each fold.

        Parameters
        ----------
        dataset : CDataset
            Dataset to split.

        Returns
        -------
        CDataSplitter
            Instance of the dataset splitter with tr/ts indices.

        """
        # Resetting indices
        self._tr_idx = []
        self._ts_idx = []

        sk_splitter = KFold(n_splits=self.num_folds,
                            shuffle=True,
                            random_state=self.random_state)

        # We take sklearn indices (iterators) and map to list of CArrays
        for train_index, test_index in \
                sk_splitter.split(dataset.X.get_data()):
            train_index = CArray(train_index)
            test_index = CArray(test_index)
            self._tr_idx.append(train_index)
            self._ts_idx.append(test_index)

        return self


import socket

# 1 -  using sockets without context manager

# Server
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("127.0.0.1", 10001))
sock.listen(socket.SOMAXCONN)

conn, addr = sock.accept()
while True:
    data = conn.recv(1024)
    if not data:
        break
    # process data
    print(data.decode("utf8"))

conn.close()
sock.close()

# Client # 01
sock1 = socket.socket()
sock1.connect(("127.0.0.1", 10001))
sock1.sendall("test ping_1".encode("utf8"))
sock1.close()

# Client # 02
sock2 = socket.create_connection(("127.0.0.1", 10001))
sock2.sendall("test ping_2".encode("utf8"))
sock2.close()

# 2 -  using sockets with context manager

# Server
with socket.socket() as sock3:
    sock3.bind(("", 10001))
    sock3.listen()

    while True:
        conn, addr = sock3.accept()
        with conn:
            while True:
                data = conn.recv(1024)
                if not data:
                    break
                print(data.decode("utf8"))

# Client
with socket.create_connection(("127.0.0.1", 10001), 5) as sock4:
    sock4.sendall("test ping_3".encode("utf8"))


import torch
import torchvision.transforms.functional as tfunc
from PIL import Image
from utils.gen_ops import make_square
import random
from albumentations.augmentations import ISONoise as alb_ISONoise
import numpy as np

DEFAULT_SIZE = 256
DEFAULT_INTERPOLATION_PIL = Image.BILINEAR
DEFAULT_INTERPOLATION = tfunc.InterpolationMode.BILINEAR
DEFAULT_ANGLE = 30

def square(image, size, stretch=False, interp=DEFAULT_INTERPOLATION_PIL):
    if stretch:
        image = image.resize((size, size), resample=interp)
    else:
        image = make_square(image, size, 'RGB')

    return image

class Square(torch.nn.Module):
    def __init__(self, size=DEFAULT_SIZE, stretch=False, interpolation=DEFAULT_INTERPOLATION):
        super(Square, self).__init__()
        self.size = size
        self.stretch = stretch
        self.interpolation = interpolation

    def __call__(self, image):
        return square(image, size=self.size, stretch=self.stretch, interp=self.interpolation)

class RandomScale(torch.nn.Module):
    def __init__(self, s_min=0.8, s_max=1.2, interpolation=DEFAULT_INTERPOLATION):
        super(RandomScale, self).__init__()
        self.min = s_min
        self.max = s_max
        self.interp = interpolation
        self.r1 = random.Random()

    def __call__(self, image):
        w, h = image.size

        min_dim = min(w, h)

        min_size = self.min * min_dim
        max_size = self.max * min_dim

        size = int(self.r1.uniform(min_size, max_size))

        return tfunc.resize(image, size=size, interpolation=self.interp)

class RandomCrop(torch.nn.Module):
    def __init__(self, size=DEFAULT_SIZE):
        super(RandomCrop, self).__init__()
        self.size = size
        self.r1 = random.Random()
        self.r2 = random.Random()
        self.sq = Square(size=size, stretch=False)

    def __call__(self, image):
        w, h = image.size

        min_dim = min(w, h)

        if min_dim <= self.size:
            image = self.sq(image)
            # w, h = image.size
        else:
            x = self.r1.randint(0, w - self.size) if w > self.size else 0
            y = self.r2.randint(0, h - self.size) if h > self.size else 0

            image = tfunc.crop(image, x, y, self.size, self.size)

        return image

class ISONoise(torch.nn.Module):
    def __init__(self, p=0.5):
        super(ISONoise, self).__init__()
        self.p = p
        self.r1 = random.Random()
        self.iso_noise = alb_ISONoise(always_apply=True)

    def __call__(self, image):
        r = self.r1.random()

        if r > self.p:
            return image

        image_np = np.array(image)
        augmented = self.iso_noise(image=image_np)['image']

        return Image.fromarray(augmented)


import torch
import gym
import Actor_critic_model
import Actor_critic_model_deep
import Actor_critic_model_extra_deep
import Test_PPO
import os
import matplotlib.pyplot as plt

have_path = False
#file_path = '/home/jacob/Documents/LunarLander/AlbinJacob/Saves_PPO/model_params_253.50440692121083' #Vanilla model with 1 layer
#file_path = '/home/jacob/Documents/LunarLander/AlbinJacob/Saves_PPO/model_params_251.6410028433216_DEEP' #"Deeper" model with 2 layers
#file_path = '/home/jacob/Documents/LunarLander/AlbinJacob/Saves_PPO/model_params_253.52047213033865_EXTRA_DEEP' #Even deeper model, with 5 layers
file_path = 'model_params_294.9360994385101_EXTRA_DEEP' #Deeper model, with 5 layers, run longer
env = gym.make('LunarLanderContinuous-v2')

do_render = True
hidden_size = 256
state_dim = env.observation_space.shape[0]
action_dim = env.action_space.shape[0]

if have_path:
    path = '/home/jacob/Documents/LunarLander/AlbinJacob/Saves_PPO/'
    file_list = os.listdir(path)
    file_list.sort()
    file_path = path + file_list[-1]
    print('Opening file path: ',file_path)
#model = Actor_critic_model.ActorCritic(state_dim,action_dim,hidden_size)
#model = Actor_critic_model_deep.ActorCriticDeep(state_dim,action_dim,hidden_size)
model = Actor_critic_model_extra_deep.ActorCriticXDeep(state_dim,action_dim,hidden_size)
print(model)
checkpoint = torch.load(file_path)
model.load_state_dict(checkpoint['model_state_dict'])
episodes = checkpoint['episodes']
loss = checkpoint['loss']
test_rewards = checkpoint['test_rewards']
test_rewards = test_rewards[1:]
#plots
max_mean_test_reward = round(max(test_rewards),2)
fig, ax = plt.subplots()
line, = ax.plot(test_rewards)
bbox_args = dict(boxstyle="round", fc="0.8")
arrow_args = dict(arrowstyle="->", connectionstyle="arc,angleA=0,armA=40,angleB=-90,armB=30,rad=7")
ax.annotate('Best reward: ' + str(max_mean_test_reward) , xy=(len(test_rewards)-1,max_mean_test_reward+1), xycoords='data', xytext=(-20, -100), textcoords='offset points', ha="right", va="top", bbox=bbox_args, arrowprops=arrow_args)
ax.set_ylabel('Mean test reward')
ax.set_xlabel('Episodes')
ax.set_title('Actor Critic with PPO Algorithm for Lunar Lander Continous')
ax.legend(['Mean test reward per episode'], loc = 'lower right')
fig.savefig('mean_reward_history.png')
plt.show()

plt.figure()
plt.plot(loss)
plt.ylabel('Mean loss over all environments')
plt.xlabel('Episodes')
plt.show()

#Run model
for i in range(20):
    #vid_name = "./vid" + str(i)
    #env = gym.wrappers.Monitor(env, vid_name,force=True) #To record
    reward = Test_PPO.run_test(env,model,do_render)
    print('Reward: ', reward)


import pytest

from aoc_08 import (
    Display,
    get_eight,
    get_four,
    get_one,
    get_seven,
    observations_len_5,
    part_one,
    part_two,
    read_file,
    solve_display,
)

def test_get_four():
    input = "be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb".split()
    result = get_four(input)

    assert result == "cgeb"

def test_get_one():
    input = "be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb".split()
    result = get_one(input)

    assert result == "be"

def test_get_seven():
    input = "be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb".split()
    result = get_seven(input)

    assert result == "edb"

def test_get_eight():
    input = "be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb".split()
    result = get_eight(input)

    assert result == "cfbegad"

def test_read_file():
    data = "data/08_test.data"
    result = read_file(data)

    observations = "be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb".split()
    readings = "fdgacbe cefdb cefbgd gcbe".split()

    assert result[0][0] == observations
    assert result[0][1] == readings

def test_part_one_with_test_data():
    data = "data/08_test.data"
    result = part_one(data)

    assert result == 26

def test_observations_len_5():
    data = "data/08_test.data"
    observations = read_file(data)

    result = observations_len_5(observations[0][0])
    expected = ["fdcge", "fecdb", "fabcd"]
    assert result == expected

def test_solve_display():
    observations = [
        "acedgfb",
        "cdfbe",
        "gcdfa",
        "fbcad",
        "dab",
        "cefabd",
        "cdfgeb",
        "eafb",
        "cagedb",
        "ab",
    ]
    readings = ["cdfeb", "fcadb", "cdfeb", "cdbaf"]

    result = solve_display([observations, readings])
    expected = {
        "a": "d",
        "b": "a",
        "c": "b",
        "d": "c",
        "e": "g",
        "f": "e",
        "g": "f",
    }
    assert result == expected

def test_solve_display_test_data_line_1():
    observations = "be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb".split()
    readings = ["fdgacbe", "cefdb", "cefbgd", "gcbe"]

    mapping = solve_display([observations, readings])
    display = Display(mapping)
    result = display.convert_to_int(readings)
    assert result == 8394

def test_solve_display_test_data_line_3():
    observations = "fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef ".split()
    readings = ["cg", "cg", "fdcagb",  "cbg"]

    mapping = solve_display([observations, readings])
    display = Display(mapping)
    result = display.convert_to_int(readings)
    assert result == 1197

def test_display_convert_to_int():
    readings = ["cdfeb", "fcadb", "cdfeb", "cdbaf"]
    mapping = {
        "a": "d",
        "b": "a",
        "c": "b",
        "d": "c",
        "e": "g",
        "f": "e",
        "g": "f",
    }
    display = Display(mapping)
    result = display.convert_to_int(readings)
    assert result == 5353

def test_part_two_with_test_data():
    filename = "data/08_test.data"
    result = part_two(filename)

    assert result == 61229

def test_part_two_with_problem_data():
    filename = "data/08.data"
    result = part_two(filename)

    assert result == 1070188


import cv2, time, dlib
import math
import importlib
from nicefacelib.src.faceformulas.EyeTracker import EyeTracker
from nicefacelib.src.faceformulas.SimpleGeometry import SimpleGeometry
from nicefacelib.src.utils.ConvertDlib import *
from nicefacelib.src.utils.DataUtils import *

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

faceCalculator = SimpleGeometry()
eyeTracker = EyeTracker()
eyeTracker.debugDrawEye = 1
eyeTracker.IsFindPupilEnabled = 1

files = ["testfaces\\face1.png", "testfaces\\face2.png"]

for path in files:
    frame = cv2.imread(path)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = detector(gray)
    frame = gray
    if (len(faces) > 0):

        face = faces[0]
        landmarks = predictor(gray, face)
        coordinates = ConvertToSimpleMatrix(landmarks)

        faceCalculator.Next68Coordinates(coordinates)
        frame = eyeTracker.CalculatePupils(frame, coordinates)

    recolor = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
    cv2.putText(recolor, "press any key to destroy all windows", (3,20), cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.5, color=(0,0,255))
    cv2.imshow(path, recolor)

cv2.waitKey()

cv2.destroyAllWindows()


import random, string

from django.shortcuts import render
from django.contrib.auth.decorators import login_required

from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from payments import get_payment_model, RedirectNeeded

@login_required
def create_ref_code(request):
    return ''.join(random.choices(string.ascii_lowercase + string.digits, k=20))

def payment_details(request, payment_id):
    payment = get_object_or_404(get_payment_model(), id=payment_id)
    try:
        form = payment.get_form(data=request.POST or None)
    except RedirectNeeded as redirect_to:
        return redirect(str(redirect_to))
    return render(request, 'pages/payment.html',{'form': form, 'payment': payment})

class PaymentsView(LoginRequiredMixin,View):
    form_class = PaymentsForm
    initial = {'key': 'value'}
    template_name = 'payment.html'

    def get(self, request, *args, **kwargs):
        try:
            form = self.form_class(initial=self.initial)
            order = Order.objects.get(user=request.user, ordered=False)
            context = {
                    'form': form,
                    'couponform': CouponForm(),
                    'shippingform': ShippingForm(),
                    'order': order,
                    # 'DISPLAY_COUPON_FORM': True
                }
            return render(request, self.template_name, context)
        except ObjectDoesNotExist:
            messages.info(request, "You do not have an active order.")
            return render(request, self.template_name, context)

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST, initial=self.initial)
        try:
            order = Order.objects.get(user=request.user, ordered=False)
            amount = int(order.get_total())
            if form.is_valid():
                billing_address_1 = form.cleaned_data['billing_address_1']
                billing_address_2 = form.cleaned_data['billing_address_2']
                billing_country_code = form.cleaned_data['billing_country_code']
                billing_city = form.cleaned_data['billing_city']
                billing_postcode = form.cleaned_data['billing_postcode']
                # TODO: add functionality for these fields
                # same_shipping_address = form.cleaned_data['same_shipping_address']
                # save_info = form.cleaned_data['save_info']
                billing_address = Address(
                    user = self.request.user,
                    street_address = billing_address_1,
                    apartment_address = billing_address_2,
                    country = billing_country_code,
                    zip = billing_postcode,
                    address_type = 'B'
                    )
                billing_address.save()
                order.billing_address = billing_address
                payments = Payments.objects.create(
                    user = request.user,
                    variant = 'default',
                    status = 'confirmed',
                    transaction_id = create_ref_code(request),
                    currency = 'usd',
                    total = amount,
                    billing_first_name = request.user.first_name,
                    billing_last_name = request.user.last_name,
                    billing_address_1 = billing_address_1,
                    billing_address_2 = billing_address_2,
                    billing_city = billing_city,
                    billing_postcode = billing_postcode,
                    billing_country_code = billing_country_code,
                    billing_email = request.user.email,
                    customer_ip_address = '127.0.0.1'
                    )
                payments.save()
                order.payments = payments
                # TODO: assign reference code
                order.ref_code = payments.transaction_id
                order.ordered = True
                order.save()
                order_items = order.items.all()
                order_items.update(ordered=True)
                for item in order_items:
                    item.save()

                messages.success(self.request, "Your order was successful!")
                return redirect('home')
        except ObjectDoesNotExist:
            messages.info(self.request, "You do not have an active order.")
            return redirect('order-summary')
        return render(request, self.template_name, {'form': form})


import collections
from operator import add

Patient = collections.namedtuple("Patient", "id name address phone_number")

BloodTest = collections.namedtuple("BloodTest", "id id_patient code date description result")

patient1=Patient(1, "Alice", "Pza L. Da Vinci 32", "02 2399")
patient2=Patient(2, "Bob", "Via Golgi 40", "02 23992")
bloodTest1=BloodTest(1,1,1, "17/2/2017", "Glucose",94)
bloodTest2=BloodTest(2,1,2, "17/2/2017", "Cholesterol",140)
bloodTest3=BloodTest(3,2,1, "16/2/2017", "Glucose",83)
bloodTest4=BloodTest(4,2,2, "16/2/2017", "Cholesterol",238)

patientsRDD=sc.parallelize([patient1,patient2])
testsRDD=sc.parallelize([bloodTest1,bloodTest2,bloodTest3,bloodTest4])

patientsDF=patientsRDD.toDF(["id", "name", "address", "phone_number"])
patientsDF.show()

testsDF=testsRDD.toDF(["id", "id_patient", "code", "date", "description", "result"])
testsDF.show()

filteredDF=testsDF.filter("description=='Cholesterol'" and testsDF.result>220)
filteredDF.collect()

joinDF=filteredDF.join(patientsDF,filteredDF.id_patient==patientsDF.id).select(filteredDF.id_patient,"result","address")
joinDF.collect()


"""
AUTHOR : HAIDER ALI
DATE   : JANUARY 16 2021
PURPOSE: TO CREATE A AI VOICE ASSISTANT IN PYTHON ..
"""

import pyttsx3
import datetime
import speech_recognition as sr  # pip instal speechRecognition
import wikipedia
import webbrowser
import os
import smtplib

# email_content = {'haider':'007harryjutt@gmail.com','ali':'007razajutt@gmail.com'}

engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
# print(voices[0].id)
engine.setProperty("voice", voices[0].id)

def speak(audio):

    engine.say(audio)
    engine.runAndWait()

def wishMe():
    """
    THIS FUNCTION WISH THE USER DEPEND ON TIME
    """
    hour = int(datetime.datetime.now().hour)
    if hour >= 0 and hour <= 12:
        print("GOOD MORNING ! ")
        speak("Good Morning")
    elif hour >= 13 and hour <= 17:
        print("GOOD AFTERNOON !")
        speak("Good Afternoon")
    else:
        print("GOOD EVENING !")
        speak("Good Evening")
    speak("I am David Sir Please tell me how may i help you ?")

def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening ....")
        r.pause_threshold = 1
        audio = r.listen(source)

    try:
        print("Recognizing ....")
        query = r.recognize_google(audio, language="en-in")
        print(f"User Said : {query}\n")
    except Exception as e:
        print("Say That Again Please ? ")
        # print(e)
        return "None"
    return query

if __name__ == "__main__":
    wishMe()
    # takeCommand()
    while True:
        query = takeCommand().lower()
        if 'wikipedia' in query:
            try:
                print("David : Searching Wikipedia ...\n")
                speak("Searching Wikipedia")
                query = query.replace('wikipedia','')
                results = wikipedia.summary(query,sentences=1)
                speak("Alright ... ")
                print("David : Acording To Wikipedia\n")
                speak("Acording To Wikipedia ...")
                print(results,'\n')
                speak(results)
            except Exception as e:
                print("David : No Results Found !!!")
                speak("No Results Found")
        elif 'open youtube' in query:
            webbrowser.open("youtube.com")
        elif 'open google' in query:
            webbrowser.open("google.com")
        elif 'open stack over flow' in query:
            webbrowser.open("stackoverflow.com")
        elif 'play music' in query:
            music_dir = r"F:\harry_programmes\Projects\David or Jarvis Projects AI\Fav songs"
            songs = os.listdir(music_dir)
            # print(time)
            os.startfile(os.path.join(music_dir,songs[0]))
        elif 'the time' in query:
            time = datetime.datetime.now().strftime("%H:%M:%S")
            print(time)
            speak(time)
        elif 'open code' in query:
            code_path="C:\\Users\\N.S COMPUTERS\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
            os.startfile(code_path)

        # elif "send email" in query:
        #     for name,adress in email_content.keys():
        #         if name in query:
        #             try:
        #                 print("David : What Should I Say ?\n")
        #                 speak("What Should I say")
        #                 content = takeCommand()
        #                 to = f"{adress}"
        #                 sendEmail(to,content)
        #                 print("Email Has Been Sent !\n")
        #                 speak("Email has been sent ")
        #             except Exception as e:
        #                 print(f"David : I am not able to send this e-mail")
        #                 speak("I am not able to send this email")

        elif 'what is your name ?' in query or 'author of this softwate' in query:
            speak("I Am Assistant Of Haider and My name is David")
            print(f"David : I Am Assistant Of Haider and My name is David \n")
        elif 'haider calculator' in query:
            cal_path=r"F:\harry_programmes\Projects\Calculator\build\exe.win-amd64-3.9\Calculator.exe"
            os.startfile(cal_path)
        elif "what you can do" in query:
            command = f"1 . Wikipedia\t2 . Youtube\t3 . Google\t4 . VS Code\t5 . Stackoverflow\n6 . Time\t7 . Calcultor Haider\t8 . Play Music\t9 . Email Send\n"

            speak("I Command On This Function For User ")
            print("I Command On This Function For User ...\n")
            print(command)
            speak(command)
        elif 'quite' in query or 'close' in query or 'stop' in query:
            exit()
        else:
            pass
        # else:
        #     print("I Can Not Able To Solve Your request ...")
        #     speak("I Can not able to solve your request")


import os
from collections import defaultdict
from unittest import TestCase

import torch
from transformers import AutoTokenizer, AutoConfig, RobertaTokenizer, RobertaForSequenceClassification

from acl.__data_prep import load_parscit_file, get_citation_context
from experiments.environment import get_env
from models.auto_modeling import AutoModelForMultiLabelSequenceClassification

class AutoModelingTest(TestCase):
    env = None

    def setUp(self) -> None:
        os.environ["WANDB_DISABLED"] = "true"
        os.environ["WANDB_WATCH"] = "false"
        self.env = get_env()

        self.cache_dir = '../data/transformers_cache'
        self.sample_text = ' '.join(['Hello world! '] * 10)
        self.num_labels = 5

    def forward_model(self, model_name_or_path, text, tokenizer_name=None):
        tokenizer = AutoTokenizer.from_pretrained(tokenizer_name if tokenizer_name else model_name_or_path, cache_dir=self.cache_dir)
        model_config = AutoConfig.from_pretrained(model_name_or_path, num_labels=self.num_labels, cache_dir=self.cache_dir)

        model = AutoModelForMultiLabelSequenceClassification.from_pretrained(model_name_or_path, config=model_config, cache_dir=self.cache_dir)

        model.eval()

        encodings = tokenizer.batch_encode_plus([text], return_tensors='pt')

        return model(encodings['input_ids']), model, tokenizer

    def test_bert_auto(self):
        model_name_or_path = self.env['bert_dir'] + '/bert-base-cased'
        out, model, tokenizer = self.forward_model(model_name_or_path, self.sample_text)

        print(out)
        print(type(model))

        print(model.config.max_position_embeddings)

    def test_distilbert_auto(self):
        model_name_or_path = self.env['bert_dir'] + '/distilbert-base-uncased'
        out, model, tokenizer = self.forward_model(model_name_or_path, self.sample_text)

        print(out)
        print(type(model))

        print(model.config.max_position_embeddings)

    def test_xlnet_auto(self):
        model_name_or_path = 'xlnet-base-cased'
        out, model, tokenizer = self.forward_model(model_name_or_path, self.sample_text)

        print(out)
        print(type(model))

        self.assertEqual(self.num_labels, out[0].shape[1])

        print(model.config.max_position_embeddings)
        print(tokenizer.model_max_length)

    def test_roberta_auto(self):
        model_name_or_path = 'roberta-base'
        out, model, tokenizer = self.forward_model(model_name_or_path, self.sample_text)

        print(out)
        print(type(model))

        self.assertEqual(self.num_labels, out[0].shape[1])

        print(model.roberta)
        print(model.config.max_position_embeddings)
        # model.save_pretrained(self.cache_dir)

    def test_roberta_manual(self):
        tokenizer = RobertaTokenizer.from_pretrained('roberta-base', cache_dir=self.cache_dir)
        model = RobertaForSequenceClassification.from_pretrained('roberta-base', cache_dir=self.cache_dir)

        encodings = tokenizer.batch_encode_plus(['foo bar'], return_tensors='pt')

        print(model(encodings['input_ids']))

    def test_longformer_auto(self):
        model_name_or_path = 'longformer-base-4096'
        out, model, tokenizer = self.forward_model(model_name_or_path, self.sample_text, 'roberta-base')

        print(out)
        print(type(model))

        self.assertEqual(self.num_labels, out[0].shape[1])

        print(model.roberta)
        print(model.config.max_position_embeddings)
        # model.save_pretrained(self.cache_dir)


import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt

# Argon parameters
eps = 0.2379
D = 0.2379
sigma = 3.405
re = pow(2,1/6)*sigma
a = 1.05

r = np.arange(3,10,0.00001)
lj = 4 * eps * ((np.power(sigma, 12) / np.power(r, 12)) - (np.power(sigma, 6) / np.power(r, 6)))
morse = D * ((np.exp(-2*a*(r-re))) - 2*(np.exp(-a*(r-re))))

plt.title("Plotting Morse and LJ Potential")
plt.ylim(-2*eps, +5*eps)
plt.ylabel('Energy')
plt.xlabel('Distance')
plt.text(8,0.7, 'a = 1.05', fontsize=11, bbox=dict(facecolor='red', alpha=0.3))
plt.plot(r,morse,'b', label='Morse Potential')
plt.plot(r,lj,'g', label='Lennard-Jones Potential')
plt.grid()
plt.legend()
plt.show()


"""Update Layer for GNN implementation."""
import tensorflow as _tf
from abc import ABC as _ABC, abstractmethod as _abstractmethod

from .input import UpdateInput
from .mlp import MLP

class UpdateLayer(_tf.keras.layers.Layer, _ABC):
    """Base Update Layer abstract class to be inherited by Update Layer implementations.

    Abstract class to define handling of Batched UpdateInput for update layer to perform Update.
    Child classes must implement the update method, which takes as argument a non-batched flattened
    UpdateInput.
    """
    def __init__(self, hidden_state_size=10, message_size=10, *args, **kwargs):
        """Update Layer abstract class constructor."""
        super(UpdateLayer, self).__init__(*args, **kwargs)
        self.hidden_state_size = hidden_state_size
        self.message_size = message_size

    def call(self, inputs: UpdateInput, training=None):
        # Flatten batch-nodes
        batch_size = _tf.shape(inputs.hidden)[0]
        num_nodes = _tf.shape(inputs.hidden)[1]
        flattened_size = batch_size * num_nodes
        messages = _tf.reshape(inputs.messages, [flattened_size, self.message_size])
        hidden = _tf.reshape(inputs.hidden, [flattened_size, self.hidden_state_size])
        hidden_initial = _tf.reshape(
            inputs.hidden_initial, [flattened_size, self.hidden_state_size])
        # Call update function
        new_hidden = self.update(
            UpdateInput(
                hidden=hidden,
                hidden_initial=hidden_initial,
                messages=messages,
            ), training=training
        )
        # Restore batches
        return _tf.reshape(new_hidden, [batch_size, num_nodes, self.hidden_state_size])

    @_abstractmethod
    def update(self, inputs: UpdateInput, training=None):
        """Update method to apply to a certain flattened UpdateInput."""
        pass

class FeedForwardUpdate(UpdateLayer):
    """Update function layer with a Feed Forward NN for GNN model."""

    def __init__(
        self, hidden_state_size=10, message_size=10, activation="relu", layer=None,
        num_layers=4, output_activation=None, units=50, *args, **kwargs
    ):
        super(FeedForwardUpdate, self).__init__(
            hidden_state_size=hidden_state_size, message_size=message_size, *args, **kwargs)
        self.mlp = MLP(
            activation=activation, layer=layer, name="update-ff-net", num_layers=num_layers,
            output_activation=output_activation, output_units=hidden_state_size, units=units,
            **kwargs)

    def build(self, input_shapes):
        build_shapes = input_shapes.hidden[-1] + input_shapes.messages[-1]
        self.mlp.build(_tf.TensorShape([None, build_shapes]))
        super(UpdateLayer, self).build([])

    def update(self, inputs: UpdateInput, training=None):
        hidden = inputs.hidden
        messages = inputs.messages
        _input = _tf.concat([hidden, messages], axis=-1)
        return self.mlp(_input, training=training)

class GRUUpdate(UpdateLayer):
    """Update function layer with GRU for GNN model."""

    def __init__(self, hidden_state_size=10, message_size=10, *args, **kwargs):
        super(GRUUpdate, self).__init__(
            hidden_state_size=hidden_state_size, message_size=message_size, *args, **kwargs)
        self.gru = _tf.keras.layers.GRUCell(units=hidden_state_size, name="update-gru")

    def update(self, inputs: UpdateInput, training=None):
        hidden = inputs.hidden
        messages = inputs.messages
        new_hidden, _ = self.gru(messages, states=hidden, training=training)
        return new_hidden


PLANNING = 0
COMPLETE = 1
UNROUTABLE = 2

class Route:
    def __init__(self, start, end, map):
        self.start = start
        self.end = end
        self.map = map
        self.route = None
        self.routeStatus = PLANNING
        router.requestRoute(self)

def h(loc, dest):
    # Use manhattan distances for the moment - probably suboptimal but will do
    return 1.01 * (abs(dest[0]-loc[0]) + abs(dest[1]-loc[1]))

def closer(o, p1, p2):
    d1 = h(o, p1)
    d2 = h(o, p2)
    return cmp(d1, d2)

class Node:
    def __init__(self, loc, g, parent, end, passable):
        self.loc = loc
        self.g = g
        self.parent = parent
        if end != None:
            self.h = h(loc, end)
        else:
            self.h = None
        self.passable = passable

    def f(self):
        return self.g + self.h

    @staticmethod
    def findLowest(openset):
        lowest = 1000000000
        lowestNode = None
        for n in openset:
            if n.f() < lowest:
                lowest = n.f()
                lowestNode = n
        return lowestNode

    @staticmethod
    def neighbours(nodemap, node, end):
        deltas = ( (1,0), (0,1), (-1,0), (0,-1) )
        results = set()
        (cx,cy) = node.loc
        (width, height) = ( len(nodemap), len(nodemap[0]) )
        for d in deltas:
            x = cx + d[0]
            y = cy + d[1]
            if x >= 0 and x < width and\
               y >= 0 and y < height and\
               nodemap[x][y].passable:
                   results.add( nodemap[x][y] )
            if end == (x,y):
                results.add( nodemap[x][y] )
        return results

class Router:
    def __init__(self):
        self.map = None
        self.nodemap = None

    # An a* routing algorithm
    # We use a second map of nodes (which I guess could really be part of the main map, but would
    # need resetting after every search, so unless performance is truly terrible, I'm not too
    # worried for now)
    def requestRoute(self, route):
        (startx, starty) = route.start
        open = set()
        closed = set()

        (width, height) = route.map.size
        map = route.map.lock()
        if self.nodemap == None:
            self.nodemap = []
            for x in range(width):
                self.nodemap.append([])
                for y in range(height):
                    self.nodemap[x].append(Node( (x,y), -1, None, route.end, map[x][y].isPassable()) )
        else:
            for x in range(len(self.nodemap)):
                for y in range(len(self.nodemap[x])):
                    self.nodemap[x][y].g = -1
                    self.nodemap[x][y].parent = None
                    self.nodemap[x][y].end = route.end
                    self.nodemap[x][y].passable = map[x][y].isPassable()

        route.map.unlock()

        self.nodemap[startx][starty].g = 0
        open.add(self.nodemap[startx][starty])

        lowest = Node.findLowest(open)
        if lowest == None:
            route.routeStatus = UNROUTABLE
            return

        while lowest.loc != route.end:
            current = lowest
            open.remove(current)
            closed.add(current)
            for neighbour in Node.neighbours(self.nodemap, current, route.end):
                cost = current.g + 1

                if neighbour in open and cost < neighbour.g:
                    open.remove(neighbour)
                if neighbour in closed and cost < neighbour.g:
                    closed.remove(neighbour)
                if neighbour not in open and neighbour not in closed:
                    neighbour.g = cost
                    open.add(neighbour)
                    neighbour.parent = current

            lowest = Node.findLowest(open)
            if lowest == None:
                route.routeStatus = UNROUTABLE
                return

        route.route = []
        loc = route.end
        while loc != route.start:
            route.route.append(loc)
            (x,y) = loc
            loc = self.nodemap[x][y].parent.loc

        route.route.reverse()
        route.routeStatus = COMPLETE

router = Router()


#!/bin/python3

import math
import os

# Complete the redJohn function below.
def redJohn(n):
    res = 0
    combos = 1
    for i in range(n // 4):
        combos += (math.factorial(n - 3 * (i + 1)) / (math.factorial(n - 4 * (i + 1)) * math.factorial(i + 1)))
    for j in range(2, int(combos + 1)):
        prime = True
        if (j % 2 == 0 and j > 2) or (j % 3 == 0 and j > 3):
            prime = False
        else:
            for k in range(5, 1 + round(j ** .5), 6):
                if j % k == 0 or j % (k + 2) == 0:
                    prime = False
                    break
        res += 1 if prime else 0
    return res

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t = int(input())

    for t_itr in range(t):
        n = int(input())

        result = redJohn(n)

        fptr.write(str(result) + '\n')

    fptr.close()

hackerrank/Algorithms/Red John is Back/solution.py


import cv2
import numpy as np
import torch
import torch.nn.functional as F

"""
[References]
https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio
https://en.wikipedia.org/wiki/Structural_similarity
"""

def mae(img1, img2):
    return np.mean(np.abs(img1.astype(float) - img2.astype(float)))

def mse(img1, img2):
    return np.mean(np.square(img1.astype(float) - img2.astype(float)))

def psnr(img1, img2):
    return 20*np.log10(255) - 10*np.log10(mse(img1, img2))

def ssim(img1: np.ndarray, img2: np.ndarray):
    x = torch.Tensor(img1.transpose(2, 0, 1).astype(float)).unsqueeze(0)
    y = torch.Tensor(img2.transpose(2, 0, 1).astype(float)).unsqueeze(0)

    x = F.pad(x, (1, 1, 1, 1), 'reflect')
    y = F.pad(y, (1, 1, 1, 1), 'reflect')

    mx = F.avg_pool2d(x, 3, 1)
    my = F.avg_pool2d(y, 3, 1)

    sx = F.avg_pool2d(x**2, 3, 1) - mx**2
    sy = F.avg_pool2d(y**2, 3, 1) - my**2
    sxy = F.avg_pool2d(x * y, 3, 1) - mx*my

    C1 = (0.01*255)**2
    C2 = (0.03*255)**2

    nom = (2*mx*my + C1) * (2*sxy + C2)
    den = (mx**2 + my**2 + C1) * (sx + sy + C2)
    ssim = nom / den

    return ssim.mean().item()

def main():
    image1 = cv2.imread('./data/images/river.png')
    image2 = cv2.imread('./data/images/river.jpg')
    image3 = cv2.imread('./data/images/park.png')

    print('image1 & image2')
    print("MAE  : %10.3f" % mae(image1, image2))
    print("MSE  : %10.3f" % mse(image1, image2))
    print("PSNR : %10.3f" % psnr(image1, image2))
    print("SSIM : %10.3f" % ssim(image1, image2))
    print()
    print('image1 & image3')
    print("MAE  : %10.3f" % mae(image1, image3))
    print("MSE  : %10.3f" % mse(image1, image3))
    print("PSNR : %10.3f" % psnr(image1, image3))
    print("SSIM : %10.3f" % ssim(image1, image3))

if __name__ == '__main__':
    main()


from pymongo import MongoClient

import config

class SignMongoDB(object):

    def __init__(self, username: str):
        self.client = MongoClient(
            host=config.MONGO_HOST,
            port=config.MONGO_PORT,
        )
        database = self.client["signweb"]
        if config.MONGO_USER_NAME is not None:
            database.authenticate(config.MONGO_USER_NAME, config.MONGO_USER_PASS)

        self.collection = database["users"]
        self.username = username
        if not self.collection.find_one({"username": username}):
            self.collection.insert_one({"username": username})

    def to_get_cookie(self) -> dict:
        """从数据库内取出cookie"""
        return self.collection.find_one({"username": self.username}, {"cookie": 1.0})['cookie']

    def to_save_cookie(self, cookie: dict):
        """保存cookie到数据库"""
        self.collection.update_one({"username": self.username}, {"$set": {"cookie": cookie}})

    def to_get_all_classid_and_courseid(self) -> list:
        """获取此用户所有的课程id"""
        cursor = self.collection.find_one({"username": self.username}, {"cclist": 1.0})
        try:
            return cursor['cclist']
        except:
            return []

    def to_save_all_classid_and_courseid(self, cclists: list):
        """保存此用户所有的课程id"""
        for cclist in cclists:
            self.collection.update_one({"username": self.username},
                                    {"$addToSet": {"cclist": cclist}})

    def to_save_istext_activeid(self, activeid: str):
        # 保存已签过到的任务id
       self.collection.update_one({"username": self.username}, {"$addToSet": {"activeid": activeid}})

    def to_get_istext_activeid(self) -> list:
        """获取前10个已签到activeid"""
        sort = [(u"_id", -1)]
        res = self.collection.find_one({"username": self.username}, {"activeid": 1.0}, sort=sort, limit=10)
        try:
            return res["activeid"]
        except:
            return []

    def set_test_data(self):
        self.collection.insert_one({"username": self.username, "cookie": 11111, "activeid": [1,2], "classid": [8,9], "courseid": [8,9]})

if __name__ == '__main__':
    s = SignMongoDB("222")
    # s.to_save_istext_activeid("123")


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

from numpy import arange, ravel, zeros, newaxis, array, where, greater, resize
from tkinter import Tk, Label, Frame
from PIL import Image, ImageTk
import sys

class Scene(Frame):

    def __init__(self, master, width=603, height=456):
        super().__init__(master, width=width, height=height)

        self.im = self.mandel = self.background = None

    def draw(self, LowX, HighX, LowY, HighY, maxiter=30):
        xx = arange(LowX, HighX, (HighX-LowX) / self['width'] * 2)
        yy = arange(HighY, LowY, (LowY-HighY) / self['height'] * 2) * 1j
        c = ravel(xx + yy[:, newaxis])
        z = zeros(c.shape, complex)
        output = resize(array(0,), c.shape)

        for iter in range(maxiter):
            print('iter:', iter)
            z = z * z + c
            finished = greater(abs(z), 2.0)
            c = where(finished, 0+0j, c)
            z = where(finished, 0+0j, z)
            output = where(finished, iter, output)

        # scale output a bit to make it brighter
        # output * output * 1000
        output = (output + (256 * output) + (256**2) * output) * 8
        self.mandel = output.tobytes()
        print('Size model:', len(self.mandel))

    def createImage(self):
        self.im = Image.new('RGB', (self['width']//2, self['height']//2))
        self.draw(-2.1, 0.7, -1.2, 1.2)

        # Size of image in bytes
        print('Image bytes:', len(self.im.tobytes('raw', 'RGBX', 0, -1)))
        self.im.frombytes(self.mandel, 'raw', 'RGBX', 0, -1)

    def createLabel(self):
        self.image = ImageTk.PhotoImage(self.im)
        self.background = Label(self.master, image=self.image)
        self.background.pack()

def main():
    window = Tk()
    window.title('Mandel Image')
    scene = Scene(window)
    scene.createImage()
    scene.createLabel()
    window.mainloop()

if __name__ == '__main__':
    main()


import paho.mqtt.client as mqtt
import logbook
import os
import sys

def on_connect(client, userdata, flags, rc):
    if rc:
        log.critical("failed to connect to {}, error code is {}. Aborting.".format(mqtt_server,rc))
        sys.exit()
    else:
        log.info("connected to {}".format(mqtt_server))
        client.subscribe("#")
        log.info("subscribed to # (all topics)")

def on_message(client, userdata, msg):
    log.info("{} {}".format(msg.topic,msg.payload.decode()))

# log to console
logbook.StreamHandler(sys.stdout).push_application()
log = logbook.Logger('log-mqtt')
log.level = logbook.DEBUG if os.environ.get('DEV') else logbook.INFO

mqtt_server = "mqtt.example.com"
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(mqtt_server, 1883, 60)
log.info("starting MQTT loop")
client.loop_forever()


from flask import Flask, render_template
from flask_wtf import FlaskForm
from wtforms import (SelectField,  IntegerField, RadioField, SubmitField)
from wtforms.validators import InputRequired
import pandas as pd

import pickle
rf_model = pickle.load(open('rf_classifier.pkl', 'rb'))

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'

@app.before_first_request
def startup():
    global rf_model
    rf_model = pickle.load(open('rf_classifier.pkl', 'rb'))

# model related variables
features = ['Pclass', 'Sex', 'Age', 'SibSp']

class InputForm(FlaskForm):
    sex = RadioField("Please select a sex:", choices=[(0,'Male'),(1,'Female')],
                     validators=[InputRequired()])
    age = IntegerField("What is your age:",
                       validators=[InputRequired()])
    pclass = SelectField("Which class are you in:",
                         choices=[(1, 'First'), (2, 'Second'), (3, 'Third')])

    sibsp = IntegerField("Number of travelers are in your party:",
                         validators=[InputRequired()])

    submit = SubmitField()

@app.route('/', methods=('GET', 'POST'))
def index():
    probability = 0
    form = InputForm()
    x_user = []
    if form.validate_on_submit():
        x_user = pd.DataFrame([[
            int(form.pclass.data),
            int(form.sex.data),
            form.age.data,
            form.sibsp.data]], columns = features)

        #score = rf_model.predict(x_user[features])
        probs =  rf_model.predict_proba(x_user[features])
        probability = (probs[0,1])*100
        #prob = (probs[0,1])*100

    return render_template('index.html',
                           x_user=x_user,
                           form=form,
                           prob = probability)

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


import pandas as pd
import numpy as np

# DATA URL SOURCES ###############################
eng_url     = 'https://andybek.com/pandas-eng'
state_url   = 'https://andybek.com/pandas-state'
party_url   = 'https://andybek.com/pandas-party'
liberal_url = 'https://andybek.com/pandas-liberal'
ivies_url   = 'https://andybek.com/pandas-ivies'
##################################################

eng = pd.read_csv(eng_url)
state = pd.read_csv(state_url)
party = pd.read_csv(party_url)
liberal = pd.read_csv(liberal_url)
ivies = pd.read_csv(ivies_url)

dfs = [state, eng, liberal, ivies, party]

schools = pd.concat(dfs).drop_duplicates(subset='School Name')

schools.reset_index(drop=True, inplace=True)

eng4 = eng.copy()
eng4['STEM'] = True

# Keep all columns ons both dfs
pd.concat([ivies, eng4], join='outer')

# Keep only columns common between the dfs
pd.concat([ivies, eng4], join='inner')

pandas/Snippets/multiple df/concat on different columns.py


# keras56_mnist_DNN.py

import numpy as np
import matplotlib.pyplot as plt

from tensorflow.keras.datasets import mnist

(x_train, y_train), (x_test,y_test) = mnist.load_data()

print(x_train.shape)  #(60000, 28, 28)

print(y_train.shape)  #(60000,)

print(x_test.shape)   #(10000, 28, 28)
print(y_test.shape)   #(10000,)

print(x_train[0].shape)   # (28, 28)

# plt.imshow(x_train[0], 'gray')
# plt.show()

# _________데이터 전처리 & 정규화 _________________

# from keras.utils import np_utils

# y_train = np_utils.to_categorical(y_train)
# y_test = np_utils.to_categorical(y_test)

# print(y_train.shape)  # (60000, 10) -> one hot encoding

# 비지도학습에서 x - x 이니까 one hot 할 필요 없음

x_train  = x_train.reshape(60000,784).astype('float32')/255.0
x_test  = x_test.reshape(10000,784).astype('float32')/255.0

X =np.append(x_train, x_test, axis =0)

print(X.shape) # (70000, 784)

from sklearn.decomposition import PCA

pca = PCA()
pca.fit(X)
cumsum = np.cumsum(pca.explained_variance_ratio_)
# print(cumsum)

best_n_components = np.argmax(cumsum >= 0.95) + 1

print(best_n_components) # 154


class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x < 0:
            return False

        strX = str(x)
        lenStr = len(strX)
        for idx in range (0, int (lenStr/2)):
            if strX[idx] != strX[lenStr - idx -1]:
                return False

        return True

#n=int(input("Enter number:"))
#temp=n
#rev=0
# reverse a number e.g., 129 -> 921
#while(n>0):
#    dig=n%10
#    rev=rev*10+dig
#    n=n//10
#if(temp==rev):
#    print("The number is a palindrome!")
#else:
#    print("The number isn't a palindrome!")


#!/usr/bin/env python
import rospy
from std_msgs.msg import Int32
from geometry_msgs.msg import PoseStamped, Pose
from styx_msgs.msg import TrafficLightArray, TrafficLight, TL_State
from styx_msgs.msg import Lane
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import tf
import cv2
import yaml
import math
import numpy as np
import tensorflow
from time import time
from std_msgs.msg import Header

STATE_COUNT_THRESHOLD = 3
GREEN = 2
RED = 1
YELLOW = 3

class TLDetector(object):
    def __init__(self):
        rospy.init_node('tl_detector')

        self.image_number = 0

        path = rospy.get_param("traffic_light_detector")

        #path = "/home/mikep/Documents/DNN-Tensorflow-Models/Traffic_Light/Simulator/frozen/frozen_inference_graph.pb"
        #path = "/home/mikep/Documents/DNN-Tensorflow-Models/Traffic_Light/Simulator/frozen/optimized.pb"

        self.detection_graph = tensorflow.Graph()
        with self.detection_graph.as_default():
            det_graph_def = tensorflow.GraphDef()
            with tensorflow.gfile.GFile(path, 'rb') as fid:
                serialized_graph = fid.read()
                det_graph_def.ParseFromString(serialized_graph)
                tensorflow.import_graph_def(det_graph_def, name='')

        # Definite input and output Tensors for detection_graph
        self.image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')
        # Each box represents a part of the image where a particular object was detected.
        self.detection_boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')
        # Each score represent how level of confidence for each of the objects.
        # Score is shown on the result image, together with the class label.
        self.detection_scores = self.detection_graph.get_tensor_by_name('detection_scores:0')
        self.detection_classes = self.detection_graph.get_tensor_by_name('detection_classes:0')
        self.num_detections = self.detection_graph.get_tensor_by_name('num_detections:0')

        self.sess = tensorflow.Session(graph=self.detection_graph)

        self.pose = None
        self.waypoints = []
        self.camera_image = None
        self.lights = []

        sub1 = rospy.Subscriber('/current_pose', PoseStamped, self.pose_cb)
        sub2 = rospy.Subscriber('/base_waypoints', Lane, self.waypoints_cb)

        '''
        /vehicle/traffic_lights provides you with the location of the traffic light in 3D map space and
        helps you acquire an accurate ground truth data source for the traffic light
        classifier by sending the current color state of all traffic lights in the
        simulator. When testing on the vehicle, the color state will not be available. You'll need to
        rely on the position of the light and the camera image to predict it.
        '''
        sub3 = rospy.Subscriber('/vehicle/traffic_lights', TrafficLightArray, self.traffic_cb)
        sub4 = rospy.Subscriber('/image_color', Image, self.image_cb)

        config_string = rospy.get_param("/traffic_light_config")
        self.config = yaml.load(config_string)

        self.bridge = CvBridge()
        #self.light_classifier = TLClassifier()
        #self.light_classifier = TL_SVM()
        self.listener = tf.TransformListener()

        self.state = TrafficLight.UNKNOWN
        self.last_state = TrafficLight.UNKNOWN
        self.last_wp = -1
        self.state_count = 0

        # The publisher of the next red traffic light waypoint index
        self.upcoming_red_light_pub = rospy.Publisher('/traffic_waypoint', Int32, queue_size=1)
        self.custom_state_pub = rospy.Publisher('/custom_light', TL_State, queue_size=1)

        rospy.spin()

    def pose_cb(self, msg):
        self.pose = msg

    def waypoints_cb(self, waypoints):
        self.waypoints = waypoints.waypoints

        stop_line_positions = self.config['stop_line_positions']
        self.stop_line_waypoints = []
        for stop_line_position in stop_line_positions:

            # Get position of this stop line
            pose = Pose()
            pose.position.x = stop_line_position[0]
            pose.position.y = stop_line_position[1]

            # Find the nearest waypoint to this stopline position
            closest_distance = float('inf')
            closest_waypoint = 0
            for i, waypoint in enumerate(self.waypoints):
                this_distance = self.distance_to_position(self.waypoints, i, pose.position)
                if this_distance < closest_distance:
                    closest_distance = this_distance
                    closest_waypoint = i
            self.stop_line_waypoints.append(closest_waypoint)

    def traffic_cb(self, msg):
        self.lights = msg.lights

    def image_cb(self, msg):
        """Identifies red lights in the incoming camera image and publishes the index
            of the waypoint closest to the red light's stop line to /traffic_waypoint

        Args:
            msg (Image): image from car-mounted camera

        """
        self.has_image = True
        self.camera_image = msg
        light_wp, state = self.process_traffic_lights()
        #print(light_wp)
        #print(state)
        #print()

        '''
        Publish upcoming red lights at camera frequency.
        Each predicted state has to occur `STATE_COUNT_THRESHOLD` number
        of times till we start using it. Otherwise the previous stable state is
        used.
        '''
        our_msg = TL_State()
        our_light = TrafficLight()
        our_light.header = Header()
        our_light.header.stamp = rospy.Time.now()
        our_light.header.frame_id = '/world'
        if self.state != state:
            self.state_count = 0
            self.state = state
        elif self.state_count >= STATE_COUNT_THRESHOLD:
            self.last_state = self.state
            light_wp = light_wp if (state == TrafficLight.RED or state == TrafficLight.YELLOW) else -1
            self.last_wp = light_wp
            self.upcoming_red_light_pub.publish(Int32(light_wp))

            our_msg.waypoint = light_wp
            our_light.state = state
            our_msg.light = our_light
            self.custom_state_pub.publish(our_msg)
        else:
            self.upcoming_red_light_pub.publish(Int32(self.last_wp))

            our_msg.waypoint = self.last_wp
            our_light.state = self.last_state
            our_msg.light = our_light
            self.custom_state_pub.publish(our_msg)
        self.state_count += 1

    def get_closest_waypoint(self, pose):
        """Identifies the closest path waypoint to the given position
        Args:
            pose (Pose): position to match a waypoint to

        Returns:
            int: index of the closest waypoint in self.waypoints

        """
        # Find the nearest waypoint
        closest_distance = float('inf')
        closest_waypoint = 0
        for i in range(len(self.waypoints)):
            this_distance = self.distance_to_position(self.waypoints, i, pose.position)
            if this_distance < closest_distance:
                closest_distance = this_distance
                closest_waypoint = i
        return closest_waypoint

    def get_closest_stop_line_waypoint(self, waypoints, car_position_waypoint, stop_line_positions):
        # Go through all stop_lines, and find their nearest waypoint
        if not hasattr(self, 'stop_line_waypoints'): return -1, 1000000
        else:

            # Log
            #rospy.loginfo("stop_line_waypoints:")
            #rospy.loginfo(stop_line_waypoints)

            # Now find the stop_line waypoint that closest to but not behind car_position_waypoint
            closest_stop_line_waypoint = 1000000
            closest_stop_line_index = -1
            for i, stop_line_waypoint in enumerate(self.stop_line_waypoints):
                if stop_line_waypoint > car_position_waypoint and stop_line_waypoint < closest_stop_line_waypoint:
                    closest_stop_line_waypoint = stop_line_waypoint
                    closest_stop_line_index = i

            # Return
            return closest_stop_line_index, closest_stop_line_waypoint

    def distance_to_position(self, waypoints, wp, position):
        calculate_distance = lambda a, b: math.sqrt((a.x-b.x)**2 + (a.y-b.y)**2  + (a.z-b.z)**2)
        distance = calculate_distance(waypoints[wp].pose.pose.position, position)
        return distance

    def get_light_state(self, traffic_light_state_truth):
        """Determines the current color of the traffic light

        Returns:
            int: ID of traffic light color (specified in styx_msgs/TrafficLight)

        """
        if not self.has_image:
            return False

        # Get classification
        cv_image = self.bridge.imgmsg_to_cv2(self.camera_image, "bgr8")
        cv_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB)

        height = cv_image.shape[0]
        width = cv_image.shape[1]

        boxes, scores, classes, num = self.sess.run([self.detection_boxes,
                                                     self.detection_scores,
                                                     self.detection_classes,
                                                     self.num_detections],
                                                    feed_dict={self.image_tensor: [cv_image]})

        new_boxes = []
        new_classes = []
        new_scores = []
        for i in range(num):
            if scores[0][i] > 0.5:
                xmin = int(boxes[0][i][0] * height)
                xmax = int(boxes[0][i][2] * height)
                ymin = int(boxes[0][i][1] * width)
                ymax = int(boxes[0][i][3] * width)
                box = [xmin, xmax, ymin, ymax]
                new_boxes.append(box)
                new_classes.append(classes[0][i])
                new_scores.append(scores[0][i])

        if len(new_classes) != 0:
            counts = np.bincount(new_classes)
            light = np.argmax(counts)
            if light == GREEN:
                return TrafficLight.GREEN
            elif light == RED:
                return TrafficLight.RED
            elif light == YELLOW:
                return TrafficLight.YELLOW
        else: return TrafficLight.UNKNOWN

    def process_traffic_lights(self):
        """Finds closest visible traffic light, if one exists, and determines its location and color

        Returns:
            int: index of waypoint closest to the upcoming stop line for a traffic light (-1 if none exists)
            int: ID of traffic light color (specified in styx_msgs/TrafficLight)

        """

        # Our closest visible traffic light starts off as none
        t1 = time()

        closest_traffic_light_waypoint = -1

        # Get the list of 'stop line' positions
        stop_line_positions = self.config['stop_line_positions']

        # Find which waypoint the car is closest to
        closest_stop_line_waypoint = -1
        traffic_light_state = TrafficLight.UNKNOWN
        if(self.pose):
            car_position_waypoint = self.get_closest_waypoint(self.pose.pose)

            # Find the closest traffic light based on stop positions
            index, closest_stop_line_waypoint = self.get_closest_stop_line_waypoint(self.waypoints, car_position_waypoint, stop_line_positions)

            # Get traffic light state ground truth from simulator provided lights list
            traffic_light_state_truth = TrafficLight.UNKNOWN
            if len(self.lights) > 0 and index >= 0 and index < len(self.lights):
                # Use matching index from stop_line_positions and hope that they align
                traffic_light_state_truth = self.lights[index].state

            # Get traffic light state from camera image
            traffic_light_state = self.get_light_state(traffic_light_state_truth)

            text = ""
            if traffic_light_state == 0: text = "Red."
            if traffic_light_state == 1: text = "Yellow."
            if traffic_light_state == 2: text = "Green."
            if traffic_light_state == 3: text = "Unknown."
            real = ""
            if traffic_light_state_truth == 0: real = "Red."
            if traffic_light_state_truth == 1: real = "Yellow."
            if traffic_light_state_truth == 2: real = "Green."
            if traffic_light_state_truth == 3: real = "Unknown."

        return closest_stop_line_waypoint, traffic_light_state

if __name__ == '__main__':
    try:
        TLDetector()
    except rospy.ROSInterruptException:
        rospy.logerr('Could not start traffic node.')


from sklearn.linear_model import SGDClassifier
import numpy as np
import os
from sklearn.externals import joblib

OPENAI_DIRECTORY=""
GLOVE_DIRECTORY="glove.twitter.27B/"
GLOVE_FILE="glove.twitter.27B.200d.txt"

def sgd():

    svm = SGDClassifier(loss='hinge')
    for i in range(0,250):
        print(i)
        X= np.load(OPENAI_DIRECTORY+"X"+ str(i)+".npy")
        Y = np.load(OPENAI_DIRECTORY+"Y" + str(i) + ".npy")
        shuffle_indices = np.random.permutation(np.arange(len(Y)))
        X = X[shuffle_indices]
        Y = Y[shuffle_indices]
        svm.partial_fit(X,Y,classes=np.unique(Y))
    joblib.dump(svm, 'openai.pkl')

def get_embeddings():
    embeddings = {}
    with open(os.path.join(GLOVE_DIRECTORY, GLOVE_FILE), 'r',encoding='utf8') as f:
        for line in f:
            values = line.split()
            word = values[0]
            coefs = np.asarray(values[1:], dtype='float32')
            embeddings[word] = coefs
    return embeddings

def embd_to_features(sentence,embedding):
    sum=np.zeros(200)
    i=0
    for w in sentence:
        if w in embedding:
            sum=sum+embedding[w]
            i=i+1
    scale=1/(i+1)
    return [scale*e for e in sum]

def glove():
    s = get_embeddings()
    positive_examples = list(open("train_pos_full.txt", "r").readlines())
    positive_examples = [s.strip() for s in positive_examples]  # -1000
    negative_examples = list(open("train_neg_full.txt", "r").readlines())
    negative_examples = [s.strip() for s in negative_examples]
    x = positive_examples + negative_examples
    positive_labels = [1 for _ in positive_examples]
    negative_labels = [0 for _ in negative_examples]
    y = np.concatenate([positive_labels, negative_labels], 0)
    shuffle_indices = np.random.permutation(np.arange(len(y)))
    X= x[shuffle_indices]
    Y = y[shuffle_indices]
    svm = SGDClassifier(loss='hinge')
    for i in range(0,250):
        X_i=[embd_to_features(e,s) for e in X[(i-1)*10000:i*10000]]
        Y_i = Y[(i - 1) * 10000:i * 10000]
        svm.partial_fit(X_i,Y_i,classes=np.unique(Y_i))
    joblib.dump(svm, 'glove.pkl')

sgd()
glove()


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

#from scipy import misc
from skimage.transform import resize
import imageio.core.util
import imageio
import os
import tensorflow as tf
import numpy as np
import facenet
import modules.detect_face
import random
from time import sleep

# =============================================================================
# Ignore WARNING:root
def silence_imageio_warning(*args, **kwargs):
    pass
imageio.core.util._precision_warn = silence_imageio_warning

print("[INFO] Starting program ...")
sleep(random.random())

# Input iamges: Directory with unaligned images
input_dir_name = "align_input"
dataset        = facenet.get_dataset(input_dir_name)

# Output Images: Directory with aligned face thumbnails
output_dir_name = "align_output"
output_dir      = os.path.expanduser(output_dir_name)
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

# add to result
output_dir_name2 = "result"
output_dir2      = os.path.expanduser(output_dir_name2)
if not os.path.exists(output_dir2):
    os.makedirs(output_dir2)

print("[INFO] Creating networks and loading parameters ...")
# Using GPUs|TensorFlow: Allowing GPU memory growth
# https://www.tensorflow.org/guide/using_gpu
with tf.Graph().as_default():
    config = tf.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.333
    session = tf.Session(config=config)
    with session.as_default():
        pnet, rnet, onet = modules.detect_face.create_mtcnn(session, None)

minsize     = 20                # minimum size of face
threshold   = [ 0.6, 0.7, 0.7 ] # three steps's threshold
factor      = 0.709             # scale factor
image_size  = 160   # Image size (height, width) in pixels
margin      = 0     # Margin for the crop around the bounding box (height, width) in pixels
# Detect and align multiple faces per image
detect_multiple_faces = False

# Add a random key to the filename to allow alignment using multiple processes
random_key = np.random.randint(0, high=99999)
bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)

with open(bounding_boxes_filename, "w") as text_file:
    nrof_images_total = 0
    nrof_successfully_aligned = 0
    for cls in dataset:
        output_class_dir = os.path.join(output_dir, cls.name)
        if not os.path.exists(output_class_dir):
            os.makedirs(output_class_dir)
#==============================================================add to result
        output_class_dir2 = os.path.join(output_dir2, cls.name)
        if not os.path.exists(output_class_dir2):
            os.makedirs(output_class_dir2)
#==============================================================
        for image_path in cls.image_paths:
            nrof_images_total += 1
            filename = os.path.splitext(os.path.split(image_path)[1])[0]
            output_filename = os.path.join(output_class_dir, filename+'.png')
            print("[INFO] %s ..." % image_path)
            if not os.path.exists(output_filename):
                try:
#                    img = misc.imread(image_path)
                    img = imageio.imread(image_path)
                except (IOError, ValueError, IndexError) as e:
                    errorMessage = '{}: {}'.format(image_path, e)
                    print("[INFO] %s ..." % errorMessage)
                else:
                    if img.ndim<2:
                        print('[INFO] Unable to align ... "%s"' % image_path)
                        text_file.write('%s\n' % (output_filename))
                        continue
                    if img.ndim == 2:
                        img = facenet.to_rgb(img)
                    img = img[:,:,0:3]

                    bounding_boxes, _ = modules.detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
                    nrof_faces = bounding_boxes.shape[0]
                    if nrof_faces>0:
                        det = bounding_boxes[:,0:4]
                        det_arr = []
                        img_size = np.asarray(img.shape)[0:2]
                        if nrof_faces>1:
                            bounding_box_size = (det[:,2]-det[:,0])*(det[:,3]-det[:,1])
                            img_center = img_size / 2
                            offsets = np.vstack([ (det[:,0]+det[:,2])/2-img_center[1], (det[:,1]+det[:,3])/2-img_center[0] ])
                            offset_dist_squared = np.sum(np.power(offsets,2.0),0)
                            index = np.argmax(bounding_box_size-offset_dist_squared*2.0) # some extra weight on the centering
                            det_arr.append(det[index,:])
                        else:
                            det_arr.append(np.squeeze(det))

                        for i, det in enumerate(det_arr):
                            det = np.squeeze(det)
                            bb = np.zeros(4, dtype=np.int32)
                            bb[0] = np.maximum(det[0]-margin/2, 0)
                            bb[1] = np.maximum(det[1]-margin/2, 0)
                            bb[2] = np.minimum(det[2]+margin/2, img_size[1])
                            bb[3] = np.minimum(det[3]+margin/2, img_size[0])
                            cropped = img[bb[1]:bb[3],bb[0]:bb[2],:]
#                            scaled = misc.imresize(cropped, (image_size, image_size), interp='bilinear')
                            scaled = resize(cropped, (image_size, image_size), order=1, mode='reflect',anti_aliasing=True)
                            nrof_successfully_aligned += 1
                            filename_base, file_extension = os.path.splitext(output_filename)
                            if detect_multiple_faces:
                                output_filename_n = "{}_{}{}".format(filename_base, i, file_extension)
                            else:
                                output_filename_n = "{}{}".format(filename_base, file_extension)
#                            misc.imsave(output_filename_n, scaled)
                            imageio.imwrite(output_filename_n, scaled)
                            text_file.write('%s %d %d %d %d\n' % (output_filename_n, bb[0], bb[1], bb[2], bb[3]))
                    else:
                        print('[INFO] Unable to align ..."%s"' % image_path)
                        text_file.write('%s\n' % (output_filename))

print('[INFO] Total number of images: %d' % nrof_images_total)
print('[INFO] Number of successfully aligned images: %d' % nrof_successfully_aligned)


from .api import AsyncYoutubeAPI
from typing import NamedTuple as _NamedTuple
from .exceptions import *
from . import utils

__title__ = "ayt-api"
__author__ = "Revnoplex"
__license__ = "MIT"
__copyright__ = "Copyright (c) 2022-2023 Revnoplex"
__version__ = "0.1.5"

class VersionInfo(_NamedTuple):
    major: int
    minor: int
    micro: int
    release_level: str
    serial: int

_matches = [("alpha", "a"), ("beta", "b"), ("candidate", "rc"), ("final", "")]

_suffix = __version__.split(".")[-1][1:]

_serial = ""
_release_letter = ""

for _char in _suffix:
    if _char.isdecimal():
        _serial += _char
    else:
        _release_letter += _char

_release_level = "".join([match[0] for match in _matches if _release_letter in match])

version_info = VersionInfo(major=int(__version__.split(".")[0]), minor=int(__version__.split(".")[1]),
                           micro=int(__version__.split(".")[2][0]), release_level=_release_level or "final",
                           serial=int(_serial or 0))


from django.shortcuts import render
from .models import Book

def all_books(request):
    """A view to show all books"""

    books = Book.objects.all()

    context = {
        'books': books,
    }

    return render(request, 'library/library.html', context)


names = ['abc','def','ghi']

# make the reverse in every list item

def reverse_list(x):

    reverse_item = []
    for i in range(len(names)):
        pop_items = names.pop()
        reverse_item.append(pop_items)

    return reverse_item

print(reverse_list(names))

words = ['abc','def','ghi']

def reverse_words(x):
    reverse_word_list = []

    for i in x:
        reverse_word_list.append(i[-1::-1])

    return reverse_word_list

# print(reverse_words(words))

# makes the reverse in every each item of the list
# def reverse_elements(x):
#     reverse = []
#     for i in x:
#         reverse.append(i[-1::-1])
#     return reverse

# print(reverse_elements(words))


import numpy as np
from matrizes import Matrizes
l = Matrizes()

class Tomografia:

    def densidade(self, a, b, converter):

        # Cria uma variável x0, que será o ponto inicial.

        x0 = []

        for i in range(len(a[0])):
            x0.append([0])

        # Execução da fórmula apresentada no artigo

        x = []

        for k in range(50): # Estrutura de repetição para aumentar a precisão dos resultados

            x.clear() # Limpa a lista para que somente o ultimo resultado seja salvo

            for i in range(len(a)): # Estrutura de repetição que resolve o sistema de n icógnitas e m equações usando projeção ortogonal

                # Conversão das listas para matrizes numpy

                x0 = np.asmatrix(x0)
                a[i] = np.matrix(a[i])

                if(i == 0): # caso esteja na primeira repetição, o algorítmo usará o x0 no lugar do x[i-1]

                    if((np.transpose(a[i]) * np.asmatrix(a[i]) ) != 0):
                        aux = x0 + a[i] * ( (b[i]-(np.transpose(a[i]) * x0)) / (np.transpose(a[i]) * np.asmatrix(a[i]) ) )
                        x.append(aux)

                    else:
                        x.append(0)

                else:
                    if((np.transpose(a[i]) * np.asmatrix(a[i]) ) != 0):

                        aux = x[i-1] + a[i] * ( (b[i]-(np.transpose(a[i]) * x[i-1])) / (np.transpose(a[i]) * np.asmatrix(a[i]) ) )
                        x.append(aux)

                    else:
                        x.append(0)

                if(i == (len(a)-1)):
                    x0 = x[i]

        # Transformando em uma fração e retornando o resultado x onde k = 12 e p = 50
        x = x[-1]

        if converter:
            max = np.matrix.max(x)
            for i in range(len(x)):
                x[i] = x[i]/max

        return x


def soma_numeros():
    """
    Faça um Programa que peça dois números e imprima a soma.
    :return: inteiro
    1 - Pedir para o usuário digitar um número e repetir o passo
    2 - somar os valores e colocar em uma variável
    3 - retornar o valor para a função
    4 - chamar a função
    """
    numero_1 = eval(input("Digite o primeiro valor: "))
    numero_2 = eval(input("Digite o segundo valor: "))
    soma = numero_1 + numero_2

    return print(f"A soma dos valores é {soma}")

soma_numeros()