"""Transition matrix module."""

from typing import TypeVar, Iterable, Optional

from cellrank import logging as logg
from cellrank.ul._docs import d, inject_docs
from cellrank.tl.kernels import VelocityKernel, ConnectivityKernel
from cellrank.tl.kernels._base_kernel import KernelExpression
from cellrank.tl.kernels._velocity_kernel import BackwardMode, VelocityMode
from cellrank.tl.kernels._velocity_schemes import Scheme

AnnData = TypeVar("AnnData")

@inject_docs(m=VelocityMode, b=BackwardMode, s=Scheme)  # don't swap the order
@d.dedent
def transition_matrix(
    adata: AnnData,
    backward: bool = False,
    vkey: str = "velocity",
    xkey: str = "Ms",
    conn_key: str = "connectivities",
    gene_subset: Optional[Iterable] = None,
    mode: str = VelocityMode.DETERMINISTIC.s,
    backward_mode: str = BackwardMode.TRANSPOSE.s,
    scheme: str = Scheme.CORRELATION.s,
    softmax_scale: Optional[float] = None,
    weight_connectivities: Optional[float] = 0.2,
    density_normalize: bool = True,
    key: Optional[str] = None,
    **kwargs,
) -> KernelExpression:
    """
    Compute a transition matrix based on a combination of RNA Velocity and transcriptomic or spatial similarity.

    To learn more about the way in which the transition matrices are computed, see
    :class:`cellrank.tl.kernels.VelocityKernel` for the velocity-based transition matrix and
    :class:`cellrank.tl.kernels.ConnectivityKernel` for the similarity-based transition matrix.

    Parameters
    ----------
    %(adata)s
    %(backward)s
    vkey
        Key from ``adata.layers`` to access the velocities.
    xkey
        Key in ``adata.layers`` where expected gene expression counts are stored.
    conn_key
        Key in :attr:`anndata.AnnData.obsp` to obtain the connectivity matrix, describing cell-cell similarity. Only
        used when ``weight_connectivities > 0``.
    gene_subset
        List of genes to be used to compute transition probabilities.
        By default, genes from ``adata.var['velocity_genes']`` are used.
    %(velocity_mode)s
    %(velocity_backward_mode_high_lvl)s
    %(velocity_scheme)s
    %(softmax_scale)s
    weight_connectivities
        Weight given to similarities as opposed to velocities. Must be in `[0, 1]`.
    density_normalize
        Whether to use density correction when computing the transition probabilities based on similarities.
        Density correction is done as by [Haghverdi16]_.
    %(write_to_adata.parameters)s
    kwargs
        Keyword arguments for :meth:`cellrank.tl.kernels.VelocityKernel.compute_transition_matrix`.

    Returns
    -------
    :class:`cellrank.tl.KernelExpression`
        A kernel expression object containing the computed transition matrix.

        %(write_to_adata)s
    """

    def compute_velocity_kernel() -> VelocityKernel:
        return VelocityKernel(
            adata, backward=backward, vkey=vkey, xkey=xkey, gene_subset=gene_subset
        ).compute_transition_matrix(
            softmax_scale=softmax_scale,
            mode=mode,
            backward_mode=backward_mode,
            scheme=scheme,
            **kwargs,
        )

    if weight_connectivities is not None:
        if 0 < weight_connectivities < 1:
            vk = compute_velocity_kernel()
            logg.info(
                f"Using a connectivity kernel with weight `{weight_connectivities}`"
            )
            ck = ConnectivityKernel(
                adata, backward=backward, conn_key=conn_key
            ).compute_transition_matrix(density_normalize=density_normalize)
            final = (
                (1 - weight_connectivities) * vk + weight_connectivities * ck
            ).compute_transition_matrix()
        elif weight_connectivities == 0:
            final = compute_velocity_kernel()
        elif weight_connectivities == 1:
            final = ConnectivityKernel(
                adata,
                backward=backward,
                conn_key=conn_key,
            ).compute_transition_matrix(density_normalize=density_normalize)
        else:
            raise ValueError(
                f"Parameter `weight_connectivities` must be in range `[0, 1]`, found `{weight_connectivities}`."
            )
    else:
        final = vk

    final.write_to_adata(key=key)

    return final


import numpy as np

from bokeh.models import CustomJS, Slider, ColumnDataSource, WidgetBox
from bokeh.plotting import figure, output_file, show
from bokeh.layouts import layout

from scipy.integrate import odeint

tools = 'pan'

def lorenz():
    sigma = 10
    rho = 28
    beta = 8.0 / 3
    theta = 3 * np.pi / 4

    def lor(xyz, t):
        x, y, z = xyz
        x_dot = sigma * (y - x)
        y_dot = x * rho - x * z - y
        z_dot = x * y - beta * z
        return [x_dot, y_dot, z_dot]

    initial = (-10, -7, 35)
    t = np.arange(0, 500, 0.006)
    solution = odeint(lor, initial, t)
    x = solution[:, 0]
    y = solution[:, 1]
    z = solution[:, 2]
    xprime = np.cos(theta) * x - np.sin(theta) * y
    colors = ["#C6DBEF", "#9ECAE1", "#6BAED6", "#4292C6", "#2171B5", "#08519C", "#08306B"]
    p = figure(title="lorenz example", tools='', toolbar_location='above', responsive='box')
    p.multi_line(np.array_split(xprime, 7), np.array_split(z, 7), line_color=colors, line_alpha=0.8, line_width=1.5)

    return p

output_file('large_lorenz.html')
show(layout([[lorenz()], [lorenz()], [lorenz()]]))


from dis import dis
from genericpath import exists
import numpy as np
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
import PIL.Image, PIL.ImageTk
from shutil import copyfile
import time, os
import cv2
import torch
import torchvision.transforms as tt
import torch.nn as nn
import torch.nn.functional as F

face_classifier = cv2.CascadeClassifier("./models/haarcascade_frontalface_default.xml")
model_state = torch.load("./models/emotion_detection_model_state.pth", map_location=torch.device('cpu'))
class_labels = ['Angry', 'Disgust', 'Fear', 'Happy', 'Neutral', 'Sad', 'Surprise']

def conv_block(in_channels, out_channels, pool=False):
    layers = [nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
              nn.BatchNorm2d(out_channels),
              nn.ELU(inplace=True)]
    if pool: layers.append(nn.MaxPool2d(2))
    return nn.Sequential(*layers)

class ResNet(nn.Module):
    def __init__(self, in_channels, num_classes):
        super().__init__()

        self.conv1 = conv_block(in_channels, 128)
        self.conv2 = conv_block(128, 128, pool=True)
        self.res1 = nn.Sequential(conv_block(128, 128), conv_block(128, 128))
        self.drop1 = nn.Dropout(0.5)

        self.conv3 = conv_block(128, 256)
        self.conv4 = conv_block(256, 256, pool=True)
        self.res2 = nn.Sequential(conv_block(256, 256), conv_block(256, 256))
        self.drop2 = nn.Dropout(0.5)

        self.conv5 = conv_block(256, 512)
        self.conv6 = conv_block(512, 512, pool=True)
        self.res3 = nn.Sequential(conv_block(512, 512), conv_block(512, 512))
        self.drop3 = nn.Dropout(0.5)

        self.classifier = nn.Sequential(nn.MaxPool2d(6),
                                        nn.Flatten(),
                                        nn.Linear(512, num_classes))

    def forward(self, xb):
        out = self.conv1(xb)
        out = self.conv2(out)
        out = self.res1(out) + out
        out = self.drop1(out)

        out = self.conv3(out)
        out = self.conv4(out)
        out = self.res2(out) + out
        out = self.drop2(out)

        out = self.conv5(out)
        out = self.conv6(out)
        out = self.res3(out) + out
        out = self.drop3(out)

        return self.classifier(out)

def clf_model():
    model = ResNet(1, len(class_labels))
    model.load_state_dict(model_state)
    return model

########################################################################
class videoGUI:

    def __init__(self, window, window_title):

        self.window = window
        self.window.geometry("1080x930")
        self.window.title(window_title)

        self.snap = False
        self.on_video = False
        self.real_time = False
        self.pause = False   # Parameter that controls pause button
        self.record_face_status = False   # Parameter that controls face detection and cropping face

        # After it is called once, the show_frame method will be automatically called every delay (milliseconds)
        self.delay = 40   # ms [1000 ms / 25 frames = 40 ms / frame]

        self.cascPath = "haarcascade_frontalface_default.xml"   # OpenCV face detector
        self.faceCascade = cv2.CascadeClassifier(self.cascPath)

        self.result_video = 'output.mp4'   # filename to save output video file
        self.result_frame_size = (200, 200)  # Final frame size to save video file

        # Define the codec and create VideoWriter object, 'avi' works fine with DIVX codec
        self.fourcc = cv2.VideoWriter_fourcc(*'DIVX')

        ##### GUI Design #####
        top_frame = Frame(self.window)
        top_frame.pack(side=TOP, pady=5)

        bottom_frame = Frame(self.window)
        bottom_frame.pack(side=BOTTOM, pady=5)

        # Create a canvas that can fit the above video source size
        self.canvas = Canvas(top_frame)
        self.canvas.pack()
        self.canvas.config(width = 970, height = 640)
        self.canvas.create_rectangle(0, 0, 970, 640, outline="black", fill="grey")
        self.myimage = PIL.ImageTk.PhotoImage(file='emotions2.png')
        self.canvas.create_image(485, 320, image=self.myimage, anchor='c')

        # Select Button
        self.btn_select=Button(bottom_frame, text="Select video file", width=15, command=self.open_file, bg='skyBlue', fg='black', padx=15, pady = 15, borderwidth=4, relief="solid")
        self.btn_select.grid(row=0, column=2, padx=15, pady = 15)

        # Play Button
        self.btn_play=Button(bottom_frame, text="Play", width=15, state=DISABLED, command=self.play_video, bg='green', fg="black", padx=15, pady = 15, borderwidth=4, relief="solid")
        self.btn_play.grid(row=0, column=4, padx=15, pady = 15)

        # Pause Button
        self.btn_pause=Button(bottom_frame, text="Pause", width=15, state=DISABLED, command=self.pause_video, bg='red', fg="black", padx = 15, pady = 15, borderwidth=4, relief="solid")
        self.btn_pause.grid(row=0, column=5, padx=15, pady = 15)

        # Face Detection Label
        self.btn_real_time_detection = Button(bottom_frame, text="Real Time Detection", width=20, command=self.start_real_time, bg="lightGreen", fg="black", padx=15, pady = 15, borderwidth=4, relief="solid")
        self.btn_real_time_detection.grid(row=0, column=0, padx=15, pady = 15)

        # Snapshot Button
        self.btn_snapshot=Button(bottom_frame, text="Snapshot", width=15, state=DISABLED, command=self.snapshot, fg="black", padx=15, pady = 15, borderwidth=4, relief="solid")
        self.btn_snapshot.grid(row=0, column=6, padx=(15,0), pady = 15)

        # Status bar
        self.status = Label(bottom_frame, text='Snapshot feature is only for realtime detection and in one execution only do one type of detection!!', bd=1, relief=SUNKEN, anchor=W)  # anchor is for text inside Label
        self.status.grid(row=1, columnspan= 5, sticky=E+W, padx=15, pady = 15)  # side is for Label location in Window

        self.window.mainloop()

    def start_real_time(self):
        if (self.on_video):
            self.cap.release()
            cv2.destroyAllWindows()
            self.on_video = False
        self.real_time = True
        self.real_cap = cv2.VideoCapture(0)
        self.btn_play['state'] = NORMAL
        self.play_video()

    def open_file(self):
        if self.real_time:
            self.real_time = False
            self.real_cap.release()
            cv2.destroyAllWindows()

        self.on_video = True
        self.pause = False

        self.filename = filedialog.askopenfilename(title="Select file", filetypes=(("MP4 files", "*.mp4"),
                                                                                ("AVI files", "*.avi")))
        print("\n Filename : ", self.filename, "\n")

        # Open the video file
        self.cap = cv2.VideoCapture(self.filename)
        self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 970)
        self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 640)

        self.btn_play['state'] = NORMAL

    def get_frame(self):   # get only one frame

        try:

            if self.cap.isOpened():
                ret, frame = self.cap.read()

                if ret:
                    # Return a boolean success flag and the current frame (color map is BGR)
                    return (ret, frame)
                else:
                    return (ret, None)

            else:
                raise ValueError("Unable to open video file : ", self.filename)

        except:
            messagebox.showerror(title='Video file not found', message='Please select a video file.')

    def play_video(self):
        model = clf_model()
        self.btn_pause['state'] = NORMAL
        self.btn_snapshot['state'] = NORMAL

        # Get a frame from the video source, and go to the next frame automatically
        if self.real_time:
            ret, frame = self.real_cap.read()
        else:
            ret, frame = self.get_frame()

        if ret:

            #####################################################

            frame = cv2.flip(frame, 1)
            labels = []
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            faces = face_classifier.detectMultiScale(gray, 1.3, 5)

            for (x, y, w, h) in faces:
                cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
                roi_gray = gray[y : y + h, x : x + w]
                roi_gray = cv2.resize(roi_gray, (48, 48), interpolation=cv2.INTER_AREA)

                if np.sum([roi_gray]) != 0:
                    roi = tt.functional.to_pil_image(roi_gray)
                    roi = tt.functional.to_grayscale(roi)
                    roi = tt.ToTensor()(roi).unsqueeze(0)

                    # make a prediction on the ROI
                    tensor = model(roi)
                    pred = torch.max(tensor, dim=1)[1].tolist()
                    label = class_labels[pred[0]]

                    label_position = (x, y)
                    cv2.putText(
                    frame,
                    label,
                    label_position,
                    cv2.FONT_HERSHEY_COMPLEX,
                    2,
                    (0, 255, 0),
                    3,
                    )
                else:
                    cv2.putText(
                    frame,
                    "No Face Found",
                    (20, 60),
                    cv2.FONT_HERSHEY_COMPLEX,
                    2,
                    (0, 255, 0),
                    3,
                    )

            #####################################################
            frame = cv2.resize(frame, (970, 640))
            self.photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)))   # convert BGR into RGB color map

            self.canvas.create_image(0, 0, image = self.photo, anchor = NW)

        after_id = self.window.after(self.delay, self.play_video)

        if self.snap:
            cv2.imwrite("frame-" + time.strftime("%d-%m-%Y-%H-%M-%S") + ".png", frame)
            self.snap = False

        if self.pause:
            self.window.after_cancel(after_id)
            self.pause = False

    def pause_video(self):
        self.pause = True
        # self.status['text'] = 'Video paused'
        # print('I am in pause function : ', self.pause)

    def snapshot(self):
        self.snap = True

    # Release the video source when the object is destroyed
    def __del__(self):
        if self.on_video:
            if self.cap.isOpened():
                self.cap.release()

##### End Class #####

# Create a window and pass it to GUI Class
videoGUI(Tk(), "Emotion Detector")


import logging

import pytest
import requests

from AutoApi.wenxin.contact.token import Weixin
from AutoApi.wenxin.contact.utils import Utils

class TestDepartment:

    @pytest.mark.parametrize("name", ["因缘研究院", "福利研究院", "天文研究院"])
    def test_create(self, name, token):
        data = {
            "name": name+Utils.udid(),
            "parentid": 4,
            "order": 1,

        }
        r = requests.post("https://qyapi.weixin.qq.com/cgi-bin/department/create",
                      params = {"access_token": token}, json=data
                      ).json()

        logging.debug(r)
        assert r["errcode"] == 0

    def test_create_order(self, token):
        data = {
            "name": "信用研发中心",
            "parentid": 2,
            "order": 3,

        }
        r = requests.post("https://qyapi.weixin.qq.com/cgi-bin/department/create",
                          params={"access_token": token}, json=data
                          ).json()

        logging.debug(r)


# -*- ecoding: utf-8 -*-
# @ModuleName: dcn
# @Author: wk
# @Email: 306178200@qq.com
# @Time: 2022/6/11 12:56 PM
from torch import nn
import torch
from ..layers import EmbeddingLayer, MLP_Layer, CrossNet
from ..utils import get_dnn_input_dim, get_linear_input, get_feature_num
from ..base_model import BaseModel
class DCN(BaseModel):
    def __init__(self,
                 embedding_dim=32,
                 dnn_hidden_units=[64, 64, 64],
                 loss_fun = 'torch.nn.BCELoss()',
                 crossing_layers = 3,
                 enc_dict=None):
        super(DCN, self).__init__(enc_dict,embedding_dim)

        self.dnn_hidden_units = dnn_hidden_units
        self.loss_fun = eval(loss_fun)
        self.enc_dict = enc_dict

        self.num_sparse, self.num_dense = get_feature_num(self.enc_dict)
        input_dim = self.num_sparse * self.embedding_dim + self.num_dense
        self.crossnet = CrossNet(input_dim, crossing_layers)

        self.fc = nn.Linear(input_dim, 1)

        self.apply(self._init_weights)

    def forward(self, data,is_training=True):
        feature_emb = self.embedding_layer(data)
        dense_input = get_linear_input(self.enc_dict, data)
        flat_feature_emb = feature_emb.flatten(start_dim=1)
        cross_out = self.crossnet(torch.cat([flat_feature_emb, dense_input],dim=1))
        y_pred = self.fc(cross_out).sigmoid()

        if is_training:
            loss = self.loss_fun(y_pred.squeeze(-1),data['label'])
            output_dict = {'pred':y_pred,'loss':loss}
        else:
            output_dict = {'pred':y_pred}
        return output_dict


import sys
import os
sys.path.insert(1, os.path.join(sys.path[0], '.'))

import itertools
import numpy as np
import activation_function
from layer import InputLayer, OutputLayer, Layer

class NetworkBaseClass:
    ID_NONE = -1
    _ID_generator = itertools.count(ID_NONE + 1)
    @staticmethod
    def GenerateID():
        """
        Generate a unique ID
        """
        return next(LayerBaseClass._ID_generator)

    def __init__(self):
        self._ID = NeutronBaseClass.GenerateID()

    @property
    def ID(self):
        return self._ID

    def Train(self, input, output, **training_params):
        raise NotImplementedError("Must implement Train().")

    def GetOutput(self, input):
        """
        Get the output of the network.
        === INPUT ===
        input:  a numpy ndarray; the input signal.
                NOTE: must conform to the sepcification of the input of
                      the network.
        """
        raise NotImplementedError("Must implement GetOutput().")

class FullyConnectedFeedforwardNetwork(NetworkBaseClass):
    def __init__(self, layer_sizes, af_type_str="sigmoid"):
        """
        === INPUT ===
        layer_sizes:    an numpy array; each element is an integer representing
                the number of neurons in that layer.
                NOTE: the first and the last elements define the number of input
                      and output signals, respectively.
        af_type_str:    a string; the type of the activation function to use.
                NOTE: see activation_function.Build() for details.
        """
        if (len(layer_sizes) < 2):
            raise RuntimeError("Must define at least 2 layers.")

        af = activation_function.Build(af_type_str)

        input_count = layer_sizes[0]
        self._input_layer = InputLayer(input_count)
        last_layer = self._input_layer
        for i in range(1, len(layer_sizes)):
            layer = Layer(layer_sizes[i], input_count, af)
            last_layer.SetNextLayer(layer)

            # roll forward
            input_count = layer_sizes[i]
            last_layer = layer

    def Train(self, batch, initial_rho):
        """
        === INPUT ===
        batch: a list of (x, y); the training input and output
        """
        for (x, y) in batch:
            #rho = initial_rho / (i + 1.0)
            rho = initial_rho
            self._input_layer.Train(x, y, rho)

        # debug: collect all biases
        #bias = []
        #layer = self._input_layer._next_layer
        #while (layer is not None):
        #    bias.append(layer._bias)
        #    layer = layer._next_layer
        #print("self._bias is \n{}".format(bias))
        #input("press any key to continue")

    def GetOutput(self, input):
        """
        === INPUT ===
        """
        raw_output = self._input_layer.Feedforward(input)

        return np.asarray(raw_output)

class SimpleFeedforwardNetwork(NetworkBaseClass):
    def __init__(self, layer_sizes):
        """
        layer_sizes: a list; number of neurons in each layer.
        """
        self._af = activation_function.Build("sigmoid")
        self._num_layers = len(layer_sizes)
        # note the first layer is input layer and doesn't need bias / weight
        self._bias = [np.random.normal(0.0, 1.0, (x, 1)) \
                for x in layer_sizes[1:]]
        self._weight = [np.random.normal(0.0, 1.0/np.sqrt(y), (x, y)) \
                for x, y in zip(layer_sizes[1:], layer_sizes[:-1])]

    def GetOutput(self, x):
        a = x # activation of the input layer is just the input
        for (b, w) in zip(self._bias, self._weight):
            z = b + np.dot(w, a)
            a = self._af.GetValue(z)
        return a

    def Train(self, batch, rho):
        """
        batch: a list of (x, y); the training input and output
        """
        # gradient of cost w.r.t. bias and weight
        nabla_b = [np.zeros(b.shape) for b in self._bias]   # partial C / partial b
        nabla_w = [np.zeros(w.shape) for w in self._weight] # partial C / partial w

        for (x, y) in batch:
            #print("y is \n{}".format(y))
            delta_nabla_b = [np.zeros(b.shape) for b in self._bias]
            delta_nabla_w = [np.zeros(w.shape) for w in self._weight]
            a = x
            all_a = [a]
            all_z = []
            ##### forward #####
            for (b, w) in zip(self._bias, self._weight):
                z = b + np.dot(w, a)
                a = self._af.GetValue(z)
                all_z.append(z)
                all_a.append(a)

            ##### at the end #####
            e = y - all_a[-1] # difference between expected y and last-layer activation
            # quadratic cost
            dC_over_dz = -e * self._af.GetDerivative(z[-1])
            # cross-entropy cost
            #dC_over_dz = -e
            delta_nabla_b[-1] = dC_over_dz
            delta_nabla_w[-1] = np.dot(dC_over_dz, all_a[-2].T)

            ##### backward #####
            for l in range(2, self._num_layers):
                dC_over_da = np.dot(self._weight[-l+1].T, dC_over_dz)
                dC_over_dz = dC_over_da * self._af.GetDerivative(all_z[-l])
                delta_nabla_b[-l] = dC_over_dz
                delta_nabla_w[-l] = np.dot(dC_over_dz, all_a[-l-1].T)
                #print("dC_over_da is \n{}".format(dC_over_da))
                #print("dC_over_dz is \n{}".format(dC_over_dz))
            #print("all_a is \n{}".format(all_a))
            #print("all_z is \n{}".format(all_z))
            #print("delta_nabla_w is \n{}".format(delta_nabla_w))
            #print("delta_nabla_b is \n{}".format(delta_nabla_b))

            nabla_b = [nb + dnb \
                    for (nb, dnb) in zip(nabla_b, delta_nabla_b)]
            nabla_w = [nw + dnw \
                    for (nw, dnw) in zip(nabla_w, delta_nabla_w)]

        # gradient descent
        n = len(batch)
        self._weight = [w - rho * dw / n \
                for (w, dw) in zip(self._weight, nabla_w)]

        self._bias = [b - rho * db / n \
                for (b, db) in zip(self._bias, nabla_b)]
        #print("nabla_w is \n{}".format(nabla_w))
        #print("nabla_b is \n{}".format(nabla_b))
        #print("self._weight is \n{}".format(self._weight))
        #print("self._bias is \n{}".format(self._bias))
        #input("press any key to continue")

if (__name__ == "__main__"):
    pass


import os

from flask import Flask, render_template
from flask_socketio import SocketIO, emit

app = Flask(__name__)
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
socketio = SocketIO(app)

@app.route("/")
def index():
    return render_template("flack.html")

@socketio.on("send message")
def sendMessage(data):
    channelData = data
    emit("sendingMessage",channelData,broadcast=True)

if __name__ == "__main__":
    socketio.run(app)


from django.contrib import admin
from django.urls import path
from .views.task import *
from .views.user import *

urlpatterns = [
    #Login and logout
    path("",home,name="home"),
    path("login",login_user,name="login"),
    path("logout",logout_user,name="logout"),
    #CRUD Task
    path("tasks/",list_tasks, name= "tasks"),
    path("task_register",create_task,name="register"),
    path("edit/<int:id>",editTask,name="edit"),
    path("delete/<int:id>",destroyTask,name="delete"),
    #Register User
    path("user_register", register_user,name ='user_register'),
]


from keras import backend as K
from keras.models import Model
from keras.layers import Conv2D, Input, Conv2DTranspose, merge ,UpSampling2D
from keras.optimizers import SGD, adam
from keras.callbacks import ReduceLROnPlateau,ModelCheckpoint
import prepare_data as pd
import pandas
import numpy
import cv2

scale = 2

def PSNRLoss(y_true, y_pred):
    return -10. * K.log(K.mean(K.square(y_pred - y_true))) / K.log(10.)

def model_SRCNN():
    _input = Input(shape=(None, None, 1), name='input')
    SRCNN = UpSampling2D(size=(scale, scale), data_format=None)(_input)
    SRCNN = Conv2D(filters=64, kernel_size=(9, 9), padding='same', activation='relu')(SRCNN)
    SRCNN = Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='relu')(SRCNN)
    out = Conv2D(filters=1, kernel_size=(5, 5),padding='same', activation='linear')(SRCNN)
    model = Model(input=_input, output=out)
    return model

def SRCNN_train():
    SRCNN = model_SRCNN()
    SRCNN.compile(optimizer=adam(lr=0.0003), loss='mse',metrics=[PSNRLoss])
    print (SRCNN.summary())

    data, label = pd.read_training_data("drive/SuperResolution/train.h5")

    val_data, val_label = pd.read_training_data("drive/SuperResolution/val.h5")

    reduce_lr = ReduceLROnPlateau(monitor='val_acc', factor=0.9, patience=25,
                                  min_lr=0.000001, verbose=1)
    checkpoint = ModelCheckpoint("drive/SuperResolution/SRCNNx2_model.h5", monitor='val_loss', verbose=1, save_best_only=True,
                                 save_weights_only=False, mode='min')
    callbacks_list = [reduce_lr,checkpoint]
    SRCNN.fit(data, label, batch_size=64, validation_data=(val_data, val_label),
                               callbacks=callbacks_list, shuffle=True, nb_epoch=200, verbose=1)
#    SRCNN.save_weights("drive/SuperResolution/SRCNN_final.h5")

def SRCNN_predict():
    IMG_NAME = "image5/img.jpg"
    INPUT_NAME = "image5/input.jpg"
    OUTPUT_NAME = "image5/SRCNN_pre.jpg"

    label = cv2.imread(IMG_NAME)
    shape = label.shape

    img = cv2.resize(label, (int(shape[1] / scale), int(shape[0] / scale)), cv2.INTER_CUBIC)
    cv2.imwrite(INPUT_NAME, img)

    SRCNN = model_SRCNN()
    SRCNN.load_weights("result_code/SRCNNx2_model.h5")

    img = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
    Y = numpy.zeros((1, img.shape[0], img.shape[1], 1))
    Y[0, :, :, 0] = img[:, :, 0].astype(float) / 255.
    img = cv2.cvtColor(label, cv2.COLOR_BGR2YCrCb)

    pre = SRCNN.predict(Y, batch_size=1) * 255.
    pre[pre[:] > 255] = 255
    pre = numpy.uint8(pre)
    img[:, :, 0] = pre[0, :, :, 0]
    img = cv2.cvtColor(img, cv2.COLOR_YCrCb2BGR)
    cv2.imwrite(OUTPUT_NAME, img)

    # psnr calculation:
    im1 = cv2.imread(IMG_NAME, cv2.IMREAD_COLOR)
    im1 = cv2.cvtColor(im1, cv2.COLOR_BGR2YCrCb)
    im2 = cv2.imread(INPUT_NAME, cv2.IMREAD_COLOR)
    im2 = cv2.cvtColor(im2, cv2.COLOR_BGR2YCrCb)
    im2 = cv2.resize(im2, (img.shape[1], img.shape[0]))
    cv2.imwrite("image5/Bicubic.jpg", cv2.cvtColor(im2, cv2.COLOR_YCrCb2BGR))
    im3 = cv2.imread(OUTPUT_NAME, cv2.IMREAD_COLOR)
    im3 = cv2.cvtColor(im3, cv2.COLOR_BGR2YCrCb)

    print ("Bicubic:")
    print (cv2.PSNR(im1[:, :, 0], im2[:, :, 0]))
    print ("SRCNN:")
    print (cv2.PSNR(im1[:, :, 0], im3[:, :, 0]))

##*******************************************************************************************************************//
if __name__ == "__main__":
#    SRCNN_train()
     SRCNN_predict()
    #vilization_and_show()


# Import necessary libraries
from wordcloud import WordCloud
import matplotlib.pyplot as plt

# Text data
def choose_file():
    text = input("filename: ")
    with open(text,'r') as f:
        text = f.read()
        return text

# Generate word cloud
def customize_wordcloud():
    bg_color = input("background color: ")
    font = int(input("font size: "))
    wordcloud = WordCloud(background_color=bg_color, max_font_size=font).generate(choose_file())
    return wordcloud

# Save word cloud
def save_cloud():
    file_name = input("enter file name to save: ")
    return file_name

# Plot word cloud
plt.imshow(customize_wordcloud(), interpolation='bilinear')
plt.axis("off")
plt.savefig(save_cloud())
plt.show()


import  scipy.optimize
from scipy.optimize import linprog
import numpy as np
import gc
import time
def LP_solver(T, data, replica_num):
    start = time.clock()
    disk = len(T)
    task = len(data)

    print("LP_solver-C-initing")
    c = [0] * task * replica_num
    # for i in range(0, disk * task):
    #     c.append(0)
    c.append(1)

    print("LP_solver-Aeq-initing")
    Aeq_li = list()
    d = 0
    for i in range(0, task):
        # eq = list()
        eq = [0] * replica_num * i + [1] * replica_num + [0] * replica_num * (task - i - 1) + [0]

        # print("Aeq:", i, eq)
        Aeq_li.append(eq)
        # gc.collect()

    print("LP_solver-beq-initing")
    beq = [1]*task

    tasks =list()
    index = 0
    for da in data:
        task_li = [0] * disk
        for its in da:
            if index != 0:
                task_li[its] = index
            else:
                task_li[its] = -1
            index = index + 1

        tasks.append(task_li)

    # print(tasks)
    print("LP_solver-A-initing")
    A_li = list()
    d = 0
    for i in range(0, disk):
        eq = [0]*task*replica_num + [-1]

        for ta in tasks:
            if ta[i] != 0 and ta[i] != -1:
                eq[ta[i]] = T[i]
            elif ta[i] == -1:
                eq[0] = T[i]

        A_li.append(eq)
        # print("A:", i, eq)
        # print("A:", i)
        # gc.collect()

    print("LP_solver-b-initing")
    b = list()
    for i in range(0, disk):
        b.append(0)

    print("LP_solver-bound-initing")
    bounds1 = [(0, 1)] * task * replica_num +[(0, None)]

    init_end = time.clock()
    print(init_end - start, "seconds init time ")
    print("LP-SOLOING")
    gc.collect()

    res = linprog(c, A_ub=A_li, b_ub=b, A_eq=Aeq_li, b_eq=beq, bounds=tuple(bounds1))
    end = time.clock()
    print(end - init_end, "seconds LP time ")
    # print(res)
    return res, tasks
# tasks 是二位数组 每一行为一个task的，参数的序号


''' Language parser for Python '''

from lizard import CodeReader

class PythonReader(CodeReader):

    ext = ['py']
    conditions = set(['if', 'for', 'while', 'and', 'or',
                      'elif', 'except', 'finally'])

    def __init__(self, context):
        super(PythonReader, self).__init__(context)
        self._state = self._global
        self.function_stack = []
        self.current_indent = 0
        self.leading_space = True

    @staticmethod
    def generate_tokens(source_code, _=None):
        return CodeReader.generate_tokens(
            source_code,
            r"|\'\'\'.*?\'\'\'" + r'|\"\"\".*?\"\"\"')

    def preprocess(self, tokens):
        for token in tokens:
            if token != '\n':
                if self.leading_space:
                    if token.isspace():
                        self.current_indent = len(token.replace('\t', ' ' * 8))
                    else:
                        if not token.startswith('#'):
                            self._close_functions()
                        self.leading_space = False
            else:
                self.leading_space = True
                self.current_indent = 0
            if not token.isspace() or token == '\n':
                yield token

    @staticmethod
    def get_comment_from_token(token):
        if token.startswith("#"):
            return token[1:]

    def _global(self, token):
        if token == 'def':
            self._state = self._function

    def _function(self, token):
        if token != '(':
            self.function_stack.append(self.context.current_function)
            self.context.start_new_function(token)
            self.context.current_function.indent = self.current_indent
        else:
            self._state = self._dec

    def _dec(self, token):
        if token == ')':
            self._state = self._state_colon
        else:
            self.context.parameter(token)
            return
        self.context.add_to_long_function_name(" " + token)

    def _state_colon(self, token):
        self._state = self._state_first_line if token == ':' else self._global

    def _state_first_line(self, token):
        self._state = self._global
        if token.startswith('"""') or token.startswith("'''"):
            self.context.add_nloc(-token.count('\n') - 1)
        self._global(token)

    def eof(self):
        self.current_indent = 0
        self._close_functions()

    def _close_functions(self):
        while self.context.current_function.indent >= self.current_indent:
            endline = self.context.current_function.end_line
            self.context.end_of_function()
            self.context.current_function = self.function_stack.pop()
            self.context.current_function.end_line = endline


import unittest
import os
from dotenv import load_dotenv

import nlpaug.augmenter.audio as naa
from nlpaug.util import AudioLoader

class TestSpeed(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        env_config_path = os.path.abspath(os.path.join(
            os.path.dirname(__file__), '..', '..', '..', '.env'))
        load_dotenv(env_config_path)
        # https://freewavesamples.com/yamaha-v50-rock-beat-120-bpm
        cls.sample_wav_file = os.path.join(
            os.environ.get("TEST_DIR"), 'res', 'audio', 'Yamaha-V50-Rock-Beat-120bpm.wav'
        )
        cls.audio, cls.sampling_rate = AudioLoader.load_audio(cls.sample_wav_file)

    def test_substitute(self):
        for _ in range(10):
            aug = naa.SpeedAug()
            aug.model.stateless = False
            augmented_audio = aug.augment(self.audio)

            if aug.model.aug_factor < 1:
                self.assertGreater(len(augmented_audio), len(self.audio))
            else:
                self.assertLess(len(augmented_audio), len(self.audio))

    def test_coverage(self):
        zone = (0.3, 0.7)
        coverage = 0.1

        for _ in range(10):
            aug = naa.SpeedAug(zone=zone, coverage=coverage)
            aug.model.stateless = False
            aug.augment(self.audio)

            if aug.model.aug_factor < 1:
                self.assertGreater(len(aug.model.aug_data), len(self.audio[aug.model.start_pos:aug.model.end_pos]))
            else:
                self.assertLess(len(aug.model.aug_data), len(self.audio[aug.model.start_pos:aug.model.end_pos]))

    def test_zone(self):
        zone = (0, 1)
        coverage = 1.

        for _ in range(10):
            aug = naa.SpeedAug(zone=zone, coverage=coverage)
            aug.model.stateless = False
            aug.augment(self.audio)

            if aug.model.aug_factor < 1:
                self.assertGreater(len(aug.model.aug_data), len(self.audio[aug.model.start_pos:aug.model.end_pos]))
            else:
                self.assertLess(len(aug.model.aug_data), len(self.audio[aug.model.start_pos:aug.model.end_pos]))


import pandas as pd
import numpy as np
import time
import matplotlib.pyplot as plt
import seaborn as sns
from tree.base import DecisionTree
from metrics import *

np.random.seed(42)
num_average_time = 100

# Learn DTs
# ...
#
# Function to calculate average time (and std) taken by fit() and predict() for different N and P for 4 different cases of DTs
# ...
# Function to plot the results
# ..
# Function to create fake data (take inspiration from usage.py)
# ...
# ..other functions

def learndata(N,M,category): # create fake dataset for four cases
  if(category=="DisDis"):
    X = pd.DataFrame({i:pd.Series(np.random.randint(M, size = N), dtype="category") for i in range(5)})
    y = pd.Series(np.random.randint(M, size = N),  dtype="category")
  elif(category=="ReDis"):
    X = pd.DataFrame(np.random.randn(N, M))
    y = pd.Series(np.random.randint(M, size = N), dtype="category")
  elif(category=="ReRe"):
    X = pd.DataFrame(np.random.randn(N, M))
    y = pd.Series(np.random.randn(N))
  else:
    X = pd.DataFrame({i:pd.Series(np.random.randint(M, size = N), dtype="category") for i in range(5)})
    y = pd.Series(np.random.randn(N))
  return X,y

def caltime(category):
  np.random.seed(42)
  N = np.random.randint(1,150,size=3)
  M = np.random.randint(1,50,size=3)

  d={}
  p={}
  for n in N:
    for m in M:
      X,y=learndata(n,m,category)
      for criteria in ['information_gain', 'gini_index']:
        Dtree = DecisionTree(criterion=criteria,max_depth=4) #Split based on Inf. Gain
        start=time.time()
        Dtree.fit(X, y)
        end=time.time()
        st2=time.time()
        Dtree.predict(X)
        en2=time.time()
        if m in d.keys():
          d[m].append((n,end-start))
        else:
          d[m]=[(n,end-start)]
        if m in p.keys():
          p[m].append((n,en2-st2))
        else:
          p[m]=[(n,en2-st2)]

  fig,ax=plt.subplots(1,len(d))
  j=0
  for i in d.keys():
    x,y=zip(*d[i])
    ax[j].plot(x,y)
    ax[j].set_xlabel(f"N at attributes={i}")
    ax[j].set_ylabel("time")
    j+=1
  plt.subplots_adjust(wspace=4,hspace=4)
  fig.suptitle(f"{category}, fit time")
  plt.show()
  fig,ax=plt.subplots(1,len(p))
  j=0
  for i in p.keys():
    x,y=zip(*p[i])
    ax[j].plot(x,y)
    ax[j].set_xlabel(f"N at attributes={i}")
    ax[j].set_ylabel("time")
    j+=1
  plt.subplots_adjust(wspace=4,hspace=4)
  fig.suptitle(f"{category}, prediction time")
  plt.show()

#caltime("DisDis")
#caltime("ReDis")
caltime("DisRe")
#caltime("ReRe")


from iot.DB import DBSingleton
db= DBSingleton.get_instance()
db.connectDB()

from iot.models import DevicesLog
from iot.serializers import DevicesLogSerializer
from rest_framework.response import  Response
from rest_framework import status
from rest_framework.decorators import APIView

import datetime
from mongoengine import *

class DevicesLogViewSet(APIView):
    def get(self, request):
        logs = DevicesLog.objects.all()
        logs_serializer = DevicesLogSerializer(logs, many=True)
        return Response(logs_serializer.data)

class DevicesLogSearch(APIView):

    def __verify_date(self, fdate, ldate):
       if fdate > ldate:
           return False
       else:
           return True

    def get_object(self, roomID, fdate, ldate, deviceID):
        d1= fdate.split("-")
        d2= ldate.split("-")
        date1= datetime.date(int(d1[0]), int(d1[1]), int(d1[2]))
        date2= datetime.date(int(d2[0]), int(d2[1]), int(d2[2]))
        print(date1)
        print(date2)
        if self.__verify_date(date1, date2):
            logs=[]
            if deviceID != None:
                logs= DevicesLog.objects(  Q(deviceId= deviceID)
                                        & Q(atRoom= roomID) )
            else:
                logs= DevicesLog.objects(atRoom= roomID)
            res=[]
            for log in logs:
                if log._date_changed.date()>=date1 and log._date_changed.date()<=date2:
                    res.append(log)
            return res
        else:
            return []

    def get(self, request, paras):
        paralst= paras.split("+")
        roomID= paralst[0]
        fday= paralst[1]
        lday= paralst[2]
        deviceID= paralst[3] if (len(paralst) == 4) else None
        logs= self.get_object(roomID, fday, lday, deviceID)
        logs_serializer= DevicesLogSerializer(logs, many=True)
        return Response(logs_serializer.data)


import pytest

from calvin.runtime.north.plugins.port import queue
from calvin.runtime.north.calvin_token import Token
from calvin.runtime.north.plugins.port.queue.common import QueueEmpty
from calvin.runtime.north.plugins.port.queue.test.test_collect_unordered import TestCollectUnorderedFIFO
pytest_unittest = pytest.mark.unittest

class DummyPort(object):
    pass

def create_port(routing="collect-unordered"):
    port = DummyPort()
    port.properties = {'routing': routing, "direction": "in"}
    return queue.get(port)

def unwrap(data):
    return data.value.items()[0]

@pytest_unittest
class TestCollectUnorderedFIFOTagged(TestCollectUnorderedFIFO):

    def setUp(self):
        self.inport = create_port(routing="collect-tagged")

    def testType(self):
        queue_type = self.inport.queue_type
        self.assertEqual(queue_type, "collect:tagged")

    def testSerialize(self):
        self.setup_writers(3)
        for i in [1,2,3]:
            for j in [0,3]:
                self.inport.write(Token("data-%d" % (i+j)), "writer-%d" % i)
        for i in [1,2,3]:
            self.inport.peek(None)
        state = self.inport._state()
        port = create_port(routing="collect-tagged")
        port._set_state(state)
        for i in [1,2,3]:
            tag, data = unwrap(port.peek(None))
            self.assertEqual(tag, "writer-%d" % i)
            self.assertEqual(data, "data-%d" % (i+3))

    def testPeek_Normal(self):
        self.setup_writers(3)
        for i in [1,2,3]:
            for j in [0,3]:
                self.inport.write(Token("data-%d" % (i+j)), "writer-%d" % i)
        for i in [1,2,3]:
            tag, data = unwrap(self.inport.peek(None))
            self.assertEqual(tag, "writer-%d" % i)
            self.assertEqual(data, "data-%d" % i)
        for i in [1,2,3]:
            tag, data = unwrap(self.inport.peek(None))
            self.assertEqual(tag, "writer-%d" % i)
            self.assertEqual(data, "data-%d" % (i+3))

    def testCancel(self):
        self.setup_writers(3)
        for i in [1,2,3]:
            for j in [0,3]:
                self.inport.write(Token("data-%d" % (i+j)), "writer-%d" % i)
        data_1 = {}
        for i in [1,2,3]:
            tag, data = unwrap(self.inport.peek(None))
            data_1.setdefault(tag, []).append(data)

        self.inport.cancel(None)
        data_2 = {}
        try :
            while True:
                tag, data = unwrap(self.inport.peek(None))
                data_2.setdefault(tag, []).append(data)
        except QueueEmpty:
            pass
        for tag, data in data_1.items():
            self.assertEqual(data, data_2[tag][:len(data)])

    def testCommit(self):
        self.setup_writers(3)
        for i in [1,2,3]:
            for j in [0,3]:
                self.inport.write(Token("data-%d" % (i+j)), "writer-%d" % i)
        values = {}
        for i in [1,2,3,4,5,6]:
            tag, data = unwrap(self.inport.peek(None))
            values.setdefault(tag, []).append(data)
        self.inport.commit(None)
        # should be empty now
        with self.assertRaises(QueueEmpty):
            self.inport.peek(None)
        for tag, data in values.items():
            for i in [1,2,3]:
                if tag == "writer-%d" % i:
                    self.assertEqual(["data-%d" % d for d in [i, i+3]], data)


import argparse
import logging
import re

def update_version(version : str, file : str):
  openedFile = open(file, "r")
  fileContent = openedFile.read()
  regex = r'^(\s*\"version\": )(\"[\d.]*\"),$'
  newFileContent = ''
  print(fileContent.splitlines()
  )
  for f in fileContent.splitlines():
    match = re.fullmatch(regex, f)
    if match != None:
      newFileContent += match.group(1) + f'"{version}",'
    else:
      newFileContent += f
    newFileContent += '\n'
  openedFile.close()
  openedFile = open(file, "w")
  openedFile.write(newFileContent)
  openedFile.close()

def build_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    parser.add_argument(nargs=2,
                        action='store',
                        dest='update_version',
                        help='Apply regex to replace the current version of app.json expo pattern file'
                        )
    return parser.parse_args()

if __name__ == '__main__':
    args = build_args()
    if args.update_version:
        print("entrou")
        update_version(args.update_version[0], args.update_version[1])
        print("saiu")


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

from collections import defaultdict
import random

import numpy as np

from ray.rllib.env.async_vector_env import _DUMMY_AGENT_ID

class MultiAgentEpisode(object):
    """Tracks the current state of a (possibly multi-agent) episode.

    The APIs in this class should be considered experimental, but we should
    avoid changing things for the sake of changing them since users may
    depend on them for custom metrics or advanced algorithms.

    Attributes:
        new_batch_builder (func): Create a new MultiAgentSampleBatchBuilder.
        add_extra_batch (func): Return a built MultiAgentBatch to the sampler.
        batch_builder (obj): Batch builder for the current episode.
        total_reward (float): Summed reward across all agents in this episode.
        length (int): Length of this episode.
        episode_id (int): Unique id identifying this trajectory.
        agent_rewards (dict): Summed rewards broken down by agent.
        custom_metrics (dict): Dict where the you can add custom metrics.
        user_data (dict): Dict that you can use for temporary storage.

    Use case 1: Model-based rollouts in multi-agent:
        A custom compute_actions() function in a policy graph can inspect the
        current episode state and perform a number of rollouts based on the
        policies and state of other agents in the environment.

    Use case 2: Returning extra rollouts data.
        The model rollouts can be returned back to the sampler by calling:

        >>> batch = episode.new_batch_builder()
        >>> for each transition:
               batch.add_values(...)  # see sampler for usage
        >>> episode.extra_batches.add(batch.build_and_reset())
    """

    def __init__(self, policies, policy_mapping_fn, batch_builder_factory,
                 extra_batch_callback):
        self.new_batch_builder = batch_builder_factory
        self.add_extra_batch = extra_batch_callback
        self.batch_builder = batch_builder_factory()
        self.total_reward = 0.0
        self.length = 0
        self.episode_id = random.randrange(2e9)
        self.agent_rewards = defaultdict(float)
        self.custom_metrics = {}
        self.user_data = {}
        self._policies = policies
        self._policy_mapping_fn = policy_mapping_fn
        self._agent_to_policy = {}
        self._agent_to_rnn_state = {}
        self._agent_to_last_obs = {}
        self._agent_to_last_action = {}
        self._agent_to_last_pi_info = {}
        self._agent_to_prev_action = {}
        self._agent_reward_history = defaultdict(list)

    def policy_for(self, agent_id=_DUMMY_AGENT_ID):
        """Returns the policy graph for the specified agent.

        If the agent is new, the policy mapping fn will be called to bind the
        agent to a policy for the duration of the episode.
        """

        if agent_id not in self._agent_to_policy:
            self._agent_to_policy[agent_id] = self._policy_mapping_fn(agent_id)
        return self._agent_to_policy[agent_id]

    def last_observation_for(self, agent_id=_DUMMY_AGENT_ID):
        """Returns the last observation for the specified agent."""

        return self._agent_to_last_obs.get(agent_id)

    def last_action_for(self, agent_id=_DUMMY_AGENT_ID):
        """Returns the last action for the specified agent, or zeros."""

        if agent_id in self._agent_to_last_action:
            return _flatten_action(self._agent_to_last_action[agent_id])
        else:
            policy = self._policies[self.policy_for(agent_id)]
            flat = _flatten_action(policy.action_space.sample())
            return np.zeros_like(flat)

    def prev_action_for(self, agent_id=_DUMMY_AGENT_ID):
        """Returns the previous action for the specified agent."""

        if agent_id in self._agent_to_prev_action:
            return _flatten_action(self._agent_to_prev_action[agent_id])
        else:
            # We're at t=0, so return all zeros.
            return np.zeros_like(self.last_action_for(agent_id))

    def prev_reward_for(self, agent_id=_DUMMY_AGENT_ID):
        """Returns the previous reward for the specified agent."""

        history = self._agent_reward_history[agent_id]
        if len(history) >= 2:
            return history[-2]
        else:
            # We're at t=0, so there is no previous reward, just return zero.
            return 0.0

    def rnn_state_for(self, agent_id=_DUMMY_AGENT_ID):
        """Returns the last RNN state for the specified agent."""

        if agent_id not in self._agent_to_rnn_state:
            policy = self._policies[self.policy_for(agent_id)]
            self._agent_to_rnn_state[agent_id] = policy.get_initial_state()
        return self._agent_to_rnn_state[agent_id]

    def last_pi_info_for(self, agent_id=_DUMMY_AGENT_ID):
        """Returns the last info object for the specified agent."""

        return self._agent_to_last_pi_info[agent_id]

    def _add_agent_rewards(self, reward_dict):
        for agent_id, reward in reward_dict.items():
            if reward is not None:
                self.agent_rewards[agent_id,
                                   self.policy_for(agent_id)] += reward
                self.total_reward += reward
                self._agent_reward_history[agent_id].append(reward)

    def _set_rnn_state(self, agent_id, rnn_state):
        self._agent_to_rnn_state[agent_id] = rnn_state

    def _set_last_observation(self, agent_id, obs):
        self._agent_to_last_obs[agent_id] = obs

    def _set_last_action(self, agent_id, action):
        self._agent_to_last_action[agent_id] = action

    def _set_last_pi_info(self, agent_id, pi_info):
        self._agent_to_last_pi_info[agent_id] = pi_info

def _flatten_action(action):
    # Concatenate tuple actions
    if isinstance(action, list) or isinstance(action, tuple):
        expanded = []
        for a in action:
            if not hasattr(a, "shape") or len(a.shape) == 0:
                expanded.append(np.expand_dims(a, 1))
            else:
                expanded.append(a)
        action = np.concatenate(expanded, axis=0).flatten()
    return action


from PyQt5.QtWidgets import QMainWindow, QTableWidgetItem
from PyQt5.QtGui import QPainter, QPen, QFont, QColor
from PyQt5.QtCore import Qt, QPoint

from view.forms import ExplainForm

class ExplainView(QMainWindow):
    TABLE_LABLE_TEMPLATE = "
{model_name}
"
    COLUMN_STEP_NAME = "i, step"

    def __init__(self):
        super(ExplainView, self).__init__()
        self.ui = ExplainForm(self)
        self.init_UI()

    def init_UI(self):
        self._hide_interface()

    def _hide_interface(self):
        pass

    def set_table_data(self, data: dict, model_name: str):
        rows_count = len(data.keys())
        data_values = list(data.values())
        columns_count = len(data_values[0]) + 1

        columns_names = [self.COLUMN_STEP_NAME]
        columns_names.extend(list(data_values[0].keys()))

        self.ui.ExplainTableWidget.setRowCount(rows_count)
        self.ui.ExplainTableWidget.setColumnCount(columns_count)
        self.ui.ExplainTableWidget.setHorizontalHeaderLabels(columns_names)
        self.ui.ExplainTableWidget.verticalHeader().setVisible(False)

        self.ui.PaintModelLabel.setAlignment(Qt.AlignCenter)
        self.ui.PaintModelLabel.setText(self.TABLE_LABLE_TEMPLATE.format(model_name=model_name))

        for irow in range(rows_count):
            for icol, col in enumerate(columns_names):
                if col == self.COLUMN_STEP_NAME:
                    item = QTableWidgetItem(str(irow))
                else:
                    item = QTableWidgetItem(str(data_values[irow][col]))
                self.ui.ExplainTableWidget.setItem(irow, icol, item)


from __future__ import absolute_import, print_function

import cPickle, os, os.path, Queue, random, re, sys, traceback, threading, time, warnings
from thread import get_ident
from itertools import count

import logging
NOTSET = logging.NOTSET
DEBUG = logging.DEBUG
INFO = logging.INFO
WARNING = logging.WARNING
ERROR = logging.ERROR
CRITICAL = logging.CRITICAL

import pony
from pony import options
from pony.utils import current_timestamp, restore_escapes, localbase

try: process_id = os.getpid()
except AttributeError: process_id = 0 # in GAE

if pony.MODE.startswith('GAE-'): LOG_TO_SQLITE = False
elif options.LOG_TO_SQLITE is not None: LOG_TO_SQLITE = options.LOG_TO_SQLITE
else: LOG_TO_SQLITE = pony.MODE in ('CHERRYPY', 'INTERACTIVE', 'FCGI-FLUP')

logging.basicConfig(level=options.LOGGING_LEVEL or INFO, format='%(message)s')
root_logger = logging.root

pony_logger = logging.getLogger('pony')
pony_logger.setLevel(options.LOGGING_PONY_LEVEL or WARNING)

warnings_logger = logging.getLogger('warnings')
prev_showwarning = warnings.showwarning
def showwarning(message, category, filename, lineno, file=None, line=None):
    text = warnings.formatwarning(message, category, filename, lineno)
    warnings_logger.warning(text)
warnings.showwarning = showwarning

def log(*args, **record):
    if args:
        record['type'] = args[0]
        if len(args) > 1:
            record['text'] = args[1]
            assert len(args) == 2
    record.setdefault('type', 'unknown')
    for field in 'user', 'text':
        value = record.get(field)
        # if isinstance(value, str): record[field] = value.decode('utf-8', 'replace')
    level = record.get('severity') or INFO
    type = record.get('type', '')
    if pony_logger.level <= level and root_logger.level <= level and not type.startswith('logging:'):
        prefix = record.get('prefix', '')
        text = record.get('text', '')
        if type != 'exception': message = prefix + text
        else:
            message = record['traceback']
            if text.startswith('SyntaxError: '):
                message = message.decode('utf-8', 'replace').encode('cp1251')
        message = restore_escapes(message)
        if message: pony_logger.log(level, message)
    if LOG_TO_SQLITE:
        record['timestamp'] = current_timestamp()
        record['process_id'] = process_id
        record['thread_id'] = local.thread_id
        record.setdefault('trans_id', None)
        queue.put(record) # record can be modified inside LoggerThread

def log_exc():
    log(type='exception', prefix='Exception: ', severity=WARNING,
        text=traceback.format_exception_only(*sys.exc_info()[:2])[-1][:-1], traceback=traceback.format_exc())

sql_re = re.compile('^\s*(\w+)')

def log_sql(sql, params=()):
    command = (sql_re.match(sql).group(1) or '?').upper()
    log(type='SQL:'+command, prefix='SQL: ', text=sql, severity=DEBUG, params=params)

hdr_list = '''
    ACTUAL_SERVER_PROTOCOL
    AUTH_TYPE
    HTTP_ACCEPT
    HTTP_ACCEPT_CHARSET
    HTTP_ACCEPT_ENCODING
    HTTP_ACCEPT_LANGUAGE
    HTTP_CONNECTION
    HTTP_COOKIE
    HTTP_HOST
    HTTP_KEEP_ALIVE
    HTTP_USER_AGENT
    PATH_INFO
    QUERY_STRING
    REMOTE_ADDR
    REMOTE_PORT
    REQUEST_METHOD
    SCRIPT_NAME
    SERVER_NAME
    SERVER_PORT
    SERVER_PROTOCOL
    SERVER_SOFTWARE
    wsgi.url_scheme
    '''.split()

hdr_dict1 = dict((header, i) for i, header in enumerate(hdr_list))
hdr_dict2 = dict(enumerate(hdr_list))

def compress_record(record):
    type = record['type']
    if type.startswith('HTTP:') and type[5].isupper():
        get = hdr_dict1.get
        headers = dict((get(header, header), value) for (header, value) in record['headers'].items())
        record['headers'] = headers

def decompress_record(record):
    type = record['type']
    if type.startswith('HTTP:') and type[5].isupper():
        get = hdr_dict2.get
        headers = dict((get(header, header), value) for (header, value) in record['headers'].items())
        record['headers'] = headers

if LOG_TO_SQLITE:
    class PonyHandler(logging.Handler):
        def emit(handler, record):
            if record.name == 'pony': return
            if record.exc_info:
                if not record.exc_text: record.exc_text = logging._defaultFormatter.formatException(record.exc_info)
                kwargs = {'exc_text': record.exc_text}
            else: kwargs = {}
            log(type='logging:%s' % record.name, text=record.getMessage(),
                severity=record.levelno, module=record.module, lineno=record.lineno, **kwargs)
    pony_handler = PonyHandler()
    logging.root.addHandler(pony_handler)

    queue = Queue.Queue()

    class Local(localbase):
        def __init__(local):
            local.thread_id = get_ident()
            local.lock = threading.Lock()
            local.lock.acquire()

    local = Local()

    def search_log(max_count=100, start_from=None, criteria=None, params=()):
        criteria = criteria or '1=1'
        params = list(params)
        if start_from is not None:
            params.append(start_from)
            if max_count > 0: criteria += ' and id < ?'
            else: criteria += ' and id > ?'
        direction = 'desc' if max_count > 0 else ''
        params.append(abs(max_count))
        sql = 'select * from log where %s order by id %s limit ?' % (criteria, direction)
        result = []
        queue.put((sql, params, result, local.lock))
        local.lock.acquire()
        if result and isinstance(result[0], Exception): raise result[0]
        return result

    def get_logfile_name():
        # This function returns relative path, if possible.
        # It is workaround for bug in SQLite
        # (Problems with unicode symbols in directory name)
        if pony.MAIN_FILE is None: return ':memory:'
        root, ext = os.path.splitext(pony.MAIN_FILE)
        if pony.MODE != 'MOD_WSGI': root = os.path.basename(root)
        return root + '-log.sqlite'

    sql_create = """
    create table if not exists log (
        id           integer primary key, -- autoincremented row id
        timestamp    timestamp not null,
        type         text not null,       -- for example HTTP:GET or SQL:SELECT
        severity     integer,
        process_id   integer  not null,
        thread_id    integer  not null,
        trans_id     integer,             -- reserved for future use; must be NULL
        user         text,                -- current user login
        text         text,                -- url, sql query, debug message, etc.
        pickle_data  binary               -- all other data in pickled form
        );
    create index if not exists index_log_timestamp on log (timestamp, type);
    """

    sql_columns = '''
    id timestamp type severity process_id thread_id trans_id user text
    '''.split()

    question_marks = ', '.join(['?'] * (len(sql_columns) + 1))
    sql_insert = 'insert into log values (%s)' % question_marks

    class LoggerThread(threading.Thread):
        def __init__(thread):
            threading.Thread.__init__(thread, name="LoggerThread")
            thread.setDaemon(True)
        def run(thread):
            import sqlite3 as sqlite
            global OperationalError
            OperationalError = sqlite.OperationalError
            con = thread.connection = sqlite.connect(get_logfile_name())
            try:
                con.execute("PRAGMA synchronous = OFF;")
                con.executescript(sql_create)
                con.commit()
                while True:
                    x = queue.get()
                    if x is None: break
                    elif not isinstance(x, dict): thread.execute_query(*x)
                    else:
                        records = [ x ]
                        while True:
                            try: x = queue.get_nowait()
                            except Queue.Empty: break
                            if not isinstance(x, dict): break
                            records.append(x)
                        thread.save_records(records)
                        if x is None: break
                        elif not isinstance(x, dict): thread.execute_query(*x)
            finally:
                con.close()
        def execute_query(thread, sql, params, result, lock):
            try:
                try: cursor = thread.connection.execute(sql, params)
                except Exception as e:
                    result.append(e); return
                for row in cursor:
                    record = cPickle.loads(str(row[-1]).decode('zip'))
                    for i, name in enumerate(sql_columns): record[name] = row[i]
                    decompress_record(record)
                    result.append(record)
            finally:
                lock.release()
                thread.connection.rollback()
        def save_records(thread, records):
            rows = []
            for record in records:
                record.pop('prefix', None)
                compress_record(record)
                row = [ record.pop(name, None) for name in sql_columns ]
                row.append(buffer(cPickle.dumps(record, 2).encode('zip')))
                rows.append(row)
            con = thread.connection
            while True:
                try:
                    con.executemany(sql_insert, rows)
                    con.commit()
                except OperationalError:
                    con.rollback()
                    time.sleep(random.random())
                else: break

    @pony.on_shutdown
    def do_shutdown():
        log(type='Log:shutdown', severity=INFO)
        queue.put(None)
        logger_thread.join()

    logger_thread = LoggerThread()
    logger_thread.start()
    log(type='Log:start', severity=INFO)


# -*- coding: utf-8 -*-
import scrapy
from selenium import webdriver
from ..items import WangyiproItem
class WangyiSpider(scrapy.Spider):
    name = 'wangyi'
    # allowed_domains = ['www.xx.com']
    start_urls = ['http://war.163.com/']
    def __init__(self):
        self.bro = webdriver.Chrome(executable_path='D:\迅雷下载\开发工具\chromedriver1.exe')

    def detail_parse(self,response):
        item = response.meta['item']
        content = response.xpath('//*[@id="endText"]/p[4]/text()').extract_first()
        item['content'] = content

        yield item
    def parse(self, response):
        div_list = response.xpath('//div[@class="data_row news_article clearfix "]')
        for div in div_list:
            # while enumerate(div_list) == 10:
            #     break
            item = WangyiproItem()

            title = div.xpath('.//div/div[1]/h3/a/text()').extract_first()
            print(title)
            detail_url = div.xpath('.//div/div[1]/h3/a/@href').extract_first()
            item['title'] = title

            yield scrapy.Request(url=detail_url,callback=self.detail_parse,meta={'item':item})
    def closed(self,spider):
        print('关闭浏览器对象')
        self.bro.quit()


import requests
import sys
# SO, lets create a function to access the API
# Also lets create a function to find pairs of players
# Finally create a summoning function

def accessAPI():
    # Gets a request summoning
    # returns the list of NBA Players
    response = requests.get('https://mach-eight.uc.r.appspot.com/')
    data = response.json()
    return data["values"]

def findPairs(listOfPlayers, height: int):
    # Navigate the list of players
    # Take current player, go to each next player after him
    # sum both players, if sum of heights is equal to value, add pair to return list
    # repeat for each player after selected player
    i = 0
    result = []
    while i < len(listOfPlayers) - 1:
        j = i + 1
        while j < len(listOfPlayers):
            player_a = listOfPlayers[i]
            player_b = listOfPlayers[j]
            value = int(player_a["h_in"]) + int(player_b["h_in"])
            if value == height:
                result.append({
                    "player_a": player_a,
                    "player_b": player_b
                })
            j = j + 1
        i = i + 1
    if len(result) > 0:
        return result
    else:
        return "No matches found"

if __name__ == "__main__":  # pragma: no cover
    if len(sys.argv) < 2:
        print("::Error:: height is required. Use python nba_finder.py _height_")
    else:
        players = accessAPI()
        expected_height = int(sys.argv[1])
        print(f"Getting players with height of {expected_height}:", findPairs(players, expected_height))


import matplotlib.pyplot as plt
import matplotlib
import numpy as np
matplotlib.rcParams.update({'font.size':14, 'font.family':'sans-serif'})
fig,(ax2,ax) = plt.subplots(2,1,sharex=True)
colors = ['C0','C1']

with open('./data/result.data','rb') as fp:
    for cn,state_size in enumerate([2,4]):
        Ep = np.load(fp)
        Ave = np.load(fp)
        Std = np.load(fp)
        ax.plot(Ep,Ave[:,0],color=colors[cn],label=r'$N_T=%s$'%state_size)
        ax.fill_between(Ep,Ave[:,0]-Std[:,0],Ave[:,0]+Std[:,0], color=colors[cn],alpha=0.2)
        ax2.plot(Ep,Ave[:,0],color=colors[cn],label=r'$N_T=%s$'%state_size)
        ax2.fill_between(Ep,Ave[:,0]-Std[:,0],Ave[:,0]+Std[:,0], color=colors[cn],alpha=0.2)
y0 = 162.10051016264254
ax.plot([Ep[0],Ep[-1]],[y0,y0],'k--')
ax.text(50,y0+1,r'$\Delta A_\mathrm{II}$')
ax2.plot([Ep[0],Ep[-1]],[y0,y0],'k--')
ax2.text(50,y0+1,r'$\Delta A_\mathrm{II}$')

y0 = 17.22748822645095
ax.plot([Ep[0],Ep[-1]],[y0,y0],'k--')
ax.text(50,y0-5,r'$\Delta A_\mathrm{IV}$')
ax2.plot([Ep[0],Ep[-1]],[y0,y0],'k--')
ax2.text(50,y0-5,r'$\Delta A_\mathrm{IV}$')
ax.set_ylim((-5,40))
ax2.set_ylim((150,180))
ax.set_xlabel('Epoch')
ax.set_ylabel('Error (degree)')
ax2.legend()

ax2.spines['bottom'].set_visible(False)
ax.spines['top'].set_visible(False)
ax2.xaxis.tick_top()
ax2.tick_params(labeltop=False)  # don't put tick labels at the top
ax.xaxis.tick_bottom()

d = .015  # how big to make the diagonal lines in axes coordinates
# arguments to pass to plot, just so we don't keep repeating them
kwargs = dict(transform=ax2.transAxes, color='k', clip_on=False)
ax2.plot((-d, +d), (-d, +d), **kwargs)        # top-left diagonal
ax2.plot((1 - d, 1 + d), (-d, +d), **kwargs)  # top-right diagonal

kwargs.update(transform=ax.transAxes)  # switch to the bottom axes
ax.plot((-d, +d), (1 - d, 1 + d), **kwargs)  # bottom-left diagonal
ax.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)  # bottom-right diagonal
ax.yaxis.set_label_coords(-0.1,1.1)

plt.show()


import argparse

def parse():
    parser = argparse.ArgumentParser(description="Parse and play card games.")
    parser.add_argument("game", help="the name of the game")
    parser.add_argument("--log", nargs="?", const="output.txt", metavar="FILENAME", help="logs the output of the game")
    parser.add_argument('--debug', dest='debug', action='store_true', help="starts the game in debug mode")
    parser.set_defaults(debug=False)

    return parser.parse_args()


import csv
import cv2
import numpy as np
from keras.models import Sequential
from keras.layers import Flatten, Dense, Lambda, Cropping2D, BatchNormalization, Activation, Dropout
from keras.layers.convolutional import Conv2D
from scipy import ndimage

# 1) Read lines from driving_log.csv file

lines = []

print('Opening .csv file...')

with open('/home/workspace/CarND-Behavioral-Cloning-P3/recorded_data/driving_log.csv') as csvfile:
    reader = csv.reader(csvfile)
    for line in reader:
        lines.append(line)

# hint: use the following line to delete column titles in sample data
# del lines[0]

# 2) Read images and corresponding angles based on file path

images = []
measurements = []

print('Reading images...')

for line in lines:
        # iterate through lines in csv file
        steering_center = float(line[3])
        correction = 0.2    # angle correction for left and right images
        steering_left = steering_center + correction
        steering_right = steering_center - correction
        steering_angles = (steering_center, steering_left, steering_right)

        for pos in range(3):
            # iterate through left-center-right images
            source_path = line[pos]
            filename = source_path.split('/')[-1]
            current_path = '/home/workspace/CarND-Behavioral-Cloning-P3/recorded_data/IMG/' + filename

            image = ndimage.imread(current_path)
            images.append(image)

            measurement = steering_angles[pos]
            measurements.append(measurement)

            # hint: use following lines for augmentation by horizontal flipping
            # image_flipped = np.fliplr(image)
            # images.append(image_flipped)
            # measurement_flipped = -measurement
            # measurements.append(measurement_flipped)

X_train = np.array(images)
y_train = np.array(measurements)

# 3) Neural network model

model = Sequential()

# Crop & normalize
model.add(Cropping2D(cropping=((100,25),(0,0)), input_shape=(160,320,3)))
model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(45,320,3)))

# Convolutional neural network
model.add(Conv2D(filters=24, kernel_size=(5, 5), strides=(2, 2), padding='same'))
model.add(BatchNormalization())
model.add(Activation('elu'))

model.add(Conv2D(filters=36, kernel_size=(5, 5), strides=(2, 2), padding='same'))
model.add(BatchNormalization())
model.add(Activation('elu'))

model.add(Conv2D(filters=48, kernel_size=(5, 5), strides=(2, 2), padding='same'))
model.add(BatchNormalization())
model.add(Activation('elu'))

model.add(Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), padding='same'))
model.add(BatchNormalization())
model.add(Activation('elu'))

model.add(Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), padding='same'))
model.add(BatchNormalization())
model.add(Activation('elu'))

model.add(Flatten())

model.add(Dense(1164))
model.add(Dropout(0.5))

model.add(Dense(100))
model.add(BatchNormalization())
model.add(Activation('elu'))
model.add(Dropout(0.5))

model.add(Dense(50))
model.add(BatchNormalization())
model.add(Activation('elu'))

model.add(Dense(10))
model.add(BatchNormalization())
model.add(Activation('elu'))

model.add(Dense(1))

# 4) Compile, train and save model

print('Compiling...')
model.compile(loss='mse', optimizer='adam')

print('Training model...')
model.fit(X_train, y_train, validation_split=0.05, shuffle=True, nb_epoch=10)

print('Saving model...')
model.save('model.h5')


import argparse
import os
import random
import shutil
from math import atan, pi, sqrt

import numpy as np
import open3d.ml as _ml3d
import open3d.ml.torch as ml3d
from open3d._ml3d import metrics
from open3d._ml3d.datasets.utils import BEVBox3D
from open3d.ml import datasets

def parse_args():
    parser = argparse.ArgumentParser(
        description='Distributed Object Removal Attacks for Kitti Scene')

    parser.add_argument('--model', help="Object Detection model\
                                                PointPillars or \
                                                PointRCNN (requires CUDA)",
                        default='PointPillars')

    parser.add_argument('--dataset_path', help='Path to Pristine Cloud Dataset',
                        default='./sample_datasets/KITTI')

    parser.add_argument('--dataset_split', help='Training / Testing / Validation',
                        default='training')

    parser.add_argument('--frame', help="Frame wanted to Analyse \
                                        eg. 0: 000035 1: 000134 2: 000200",
                        default=0, type=int)

    parser.add_argument('--save_pcd', help="0: Don't Save Spoofed PCD \
                                            1: Save Spoofed PCD",
                        default=0, type=int, choices=[0, 1])

    parser.add_argument('--save_path', help="Path to Save Spoofed PCD. \
                                             Also, copies GT labels & Calib",
                        default='./sample_datasets/Adv_KITTI')

    parser.add_argument('--config_path', help="Path to Configuration File")

    parser.add_argument('--ckpt_path', help="Path to pretrained model")

    parser.add_argument('--device', help="Specify device (cpu / gpu / cuda)",
                        default='cpu')

    args, _ = parser.parse_known_args()

    return args

args = parse_args()

if args.config_path is not None:
    cfg_path = args.config_path
else:
    cfg_path = "configs/" + args.model.lower() + ".yml"
if args.ckpt_path is not None:
    ckpt_path = args.ckpt_path
else:
    ckpt_path = "./pretrained_ckpt/" + args.model.lower() + ".pth"

cfg = _ml3d.utils.Config.load_from_file(cfg_path)
model_ = _ml3d.utils.get_module("model", args.model, "torch")
model = model_(device=args.device, **cfg.model)
pipeline = ml3d.pipelines.ObjectDetection(model, device=args.device,
                                            **cfg.pipeline)
pipeline.load_ckpt(ckpt_path=ckpt_path)

dataset = ml3d.datasets.KITTI(args.dataset_path)
training_dataset = dataset.get_split(args.dataset_split)
frame_ = dataset.get_split_list(args.dataset_split)[args.frame]
frame = frame_.replace(args.dataset_path + \
                        "/training/velodyne/", '').replace('.bin', '')
print("\nKitti Data Frame: " + str(frame))
data = training_dataset.get_data(args.frame)

def get_linear_distance(point, ref):
    """
    Calculates linear distance for 3D points
    Args:
        point [x1, y1, z1]
        ref [x0, y0 , z0]
    Return:
        distance (float)
    """
    dist = sqrt((point[0]-ref[0])**2 + (point[1]-ref[1])**2 +
                 (point[2]-ref[2])**2)
    return dist

def get_linear_distance_list(box, ref):
    """
    Get a list of distances from list of boxes
    Args:
        box: list of boxes with centre coords [x,y,z]
        ref: reference point [x,y,z]
    Return:
        list: list of distances
    """

    list = []
    for i in box:
        dist = get_linear_distance(i, ref)
        list.append(dist)
    return list

def get_points_in_boxes(pc, boxes, box_info):
    """
    Get a list of points in each box
    Args:
        pc: point cloud space as np.array
        boxes: list of boxes as o3d.BEVBox3d
        box_info: list of boxes as [x,y,z,w,h,l,r]
    Return:
        list: list of np.arrays
    """
    is_points_in_box = datasets.utils.operations.points_in_box(
        pc, box_info)
    list_ = []
    for i, box in enumerate(boxes):
        pts_in_box = pc[is_points_in_box[:, i]]
        list_.append(pts_in_box)
    return list_

def get_pts_in_horizontal_view_range(target_box, target_box_pts):
    """
    Gets Points within a 10º Horizontal Angle (Azimuth angle)
    Args:
        target_box: box info [x,y,z,w,h,l,r]
        target_box_pts: np.array of points within bounding box
    Return:
        count: number of points within horizontal angle
        possible_attack_points: nx4 np.array of points within angle
    """
    count=0
    possible_attack_pts = []
    for i in target_box_pts:
        if (-pi/36) < (atan(i[1]/i[0]) -
                        atan(target_box[1]/target_box[0])) < (pi/36):
            possible_attack_pts.append(i)
            count+=1
    return count, possible_attack_pts

def get_spoofed_points(pos_attack_pts, data_pts, num):
    """
    ORA - Randomly removes specified 'num' of points
    Args:
        pos_attack_pts: nx4 np.array of points to remove
        data_pts: nx4 np.array of points for reference
        num: number of points to remove (int)
    Return:
        num: number of points removed
        spoofed_pts: poisioned pcd (nx4 np.array)
        pos_attack_pts: delete from original and returned
    """
    # If number of possible attack points is less than 'num'
    if len(pos_attack_pts) < num:
        num = len(pos_attack_pts)

    # Get random nx4 np.array of points to remove
    attack_pts = []
    for i in range(num):
        idx = random.randrange(len(pos_attack_pts))
        attack_pts.append(pos_attack_pts[idx])
        pos_attack_pts = np.delete(pos_attack_pts, idx, 0)
    attack_pts = np.array(attack_pts)

    # Get Index of Points to remove
    pos_list = []
    for i in range(len(attack_pts)):
        pos = np.where(np.all(data_pts == attack_pts[i], axis=1))[0]
        pos_list.extend(pos)
    pos_list.sort()

    spoofed_pts = data_pts

    # Remove all indices of Points
    for i in range(len(pos_list)):
        if i == 0:
            spoofed_pts = np.delete(spoofed_pts, pos_list[i], 0)
        else:
            spoofed_pts = np.delete(spoofed_pts, (pos_list[i] - i), 0)

    return num, spoofed_pts, pos_attack_pts

def get_pred_box_id(iou, target):
    """
    Get Index of Box which Intersects Target Box
    Args:
        iou: List of np.arrays containing IoU data
        target (int): index of target box
    Return:
        if found:
            i: Index of Maximum IoU
            max_list[target]: (float) Maximum IoU value with Box
        else:
            -1, -1 (no value found)
    """

    max_list = np.amax(iou, axis=0)
    for i in range(len(iou)):
            if iou[i][target] == max_list[target]:
                return i, max_list[target]
    return -1, -1

def get_prediction_info(data, idx, gt):
    """
    Runs Object Detector, and gets information on boxes, its confidence score and IoU with GT
    Args:
        data: Dictionary of [nx4] np.array of points and calibration
        idx: Index of GT box targeted
        gt: Ground Truth Bounding boxes as Dictionary
    Return:
        bboxes: List of BEVBox3D objects
        info_: Dictionary of IoU and Score for each Targeted Box
    """
    bboxes = pipeline.run_inference(data)[0]
    box = []
    box.extend(BEVBox3D.to_dicts(b) for b in [bboxes])
    iou = metrics.iou_3d(box[0]['bbox'].astype(np.float32),
                         gt[0]['bbox'].astype(np.float32))

    info_ = {}
    for i in idx:
        info_[i] = {}
        id_, iou_val = get_pred_box_id(iou, i)
        info_[i]['iou'] = iou_val
        if iou_val == 0:
            info_[i]['score'] = 0
        else:
            info_[i]['score'] = box[0]['score'][id_]

    return bboxes, info_

def save_spoofed_pcd(pts):
    """
    Function to save spoofed data
    Args:
        pts: point cloud data as np.array
    """
    print("\nSaving Spoofed PCD Data ...")
    # Get Number of Files in Save Path (eg. Adv_Kitti) Velodyne Dir
    dataset_ = ml3d.datasets.KITTI(args.save_path)
    file_ = dataset_.get_split_list(args.dataset_split)
    idx = len(file_)

    # Save Spoofed PCD in .bin file
    bin_path = args.save_path + "/" + args.dataset_split + "/velodyne/"
    bin_file_name = "{:04d}.bin".format(idx)
    bin_file_path = os.path.join(bin_path, bin_file_name)
    pts.tofile(bin_file_path)

    # Copy Relevant Label_2 File
    label_2_path = frame_.replace('velodyne', 'label_2').replace('.bin', '.txt')
    spoofed_label_path = bin_file_path.replace('velodyne', 'label_2').replace('.bin', '.txt')
    shutil.copy2(label_2_path, spoofed_label_path)

    # Copy Relevant Calib File Path
    calib_path = label_2_path.replace('label_2', 'calib')
    spoofed_calib_path = spoofed_label_path.replace('label_2','calib')
    shutil.copy2(calib_path, spoofed_calib_path)

    print("Saved: ")
    print(" PCD data: " + bin_file_path)
    print(" GT labels: " + spoofed_label_path)
    print(" Calibration: " + spoofed_calib_path)
    print("")
    return

def main():
    # Define Reference Box at [x,y,z], size[w,h,l], [yaw], [label], [score]
    reference_box = BEVBox3D([0, 0, 0], [2,2,3], pi/2,'Car',-1)
    ref_box_xyzwhlr = reference_box.to_xyzwhlr()

    # Ground Truth Information
    gt = []
    gt.extend([BEVBox3D.to_dicts(b) for b in [data['bounding_boxes']]])
    bbox = data['bounding_boxes']
    gt_xyzwhlr = [box.to_xyzwhlr() for box in bbox]
    distance_list = get_linear_distance_list(gt_xyzwhlr, ref_box_xyzwhlr)
    pts_in_box = get_points_in_boxes(data['point'],
                                              data['bounding_boxes'],
                                              gt_xyzwhlr)
    print(("=================== Ground Truth ==================="))
    print(("Box ID" + "{:>8}".format("Label") +
            "{:>22}".format("Distance (m)") +
            "{:>15}".format("No. of Points")))
    for i in range(len(data['bounding_boxes'])):
        print(("  {:<8}".format(str(i)) +
                "{:<10}".format(gt[0]['label'][i]) +
                "{:>11.1f}".format(distance_list[i]) +
                "{:>14.0f}".format(len(pts_in_box[i]))))

    # Get Possible Attack Points within 10º Horizontal Angle
    num_pts = {}
    pos_attack_pts = []
    for i in range(len(bbox)):
        cnt, pts_ = get_pts_in_horizontal_view_range(gt_xyzwhlr[i],
                                                        pts_in_box[i])
        num_pts[i] = cnt
        pos_attack_pts.append(pts_)
    # Sort by Least to Most Number of Points
    asc_num_ = {k: v for k, v in sorted(num_pts.items(),
                            key=lambda item: item[1])}

    # Get Spoofed Points and Record Number of Removed Points for Each Target Box
    spoofed_pts = np.array(data['point'])
    total_spoofed = 0
    count_ = {}
    for keys in asc_num_.keys():
        count_[keys] = np.array(0)
    while total_spoofed < 200:
        for keys in asc_num_.keys():
            if len(pos_attack_pts[keys]) > 40:
                spoof_num = 40
            else:
                spoof_num = len(pos_attack_pts[keys])
            if total_spoofed + spoof_num >= 200:
                spoof_num = 200 - total_spoofed
            (num_, spoofed_pts,
                pos_attack_pts[keys]) = get_spoofed_points(pos_attack_pts[keys],
                                                            spoofed_pts,
                                                            spoof_num)
            count_[keys] = count_[keys] + num_
            total_spoofed+=spoof_num

    # For Each GT-Box, Record If Points were Removed
    target_idx = []
    for i in count_.keys():
        if count_[i] > 0:
            target_idx.append(i)
    target_idx.sort()

    # Print IoU and Confidence of Targeted Boxes before D-ORA
    print("\nBefore D-ORA: ")
    pred_boxes, pred_info = get_prediction_info(data, target_idx, gt)
    for i in pred_info.keys():
        if pred_info[i]['score'] == 0:
            print("Box ID: " + str(i) + " was not detected")
        else:
            print(("Box ID: " + str(i) + " was detected with IoU: " +
                   "{:.3f}".format(pred_info[i]['iou']) +  ", Conf: " +
                   "{:.3f}".format(pred_info[i]['score'])))

    #Print IoU and Confidence of Targeted Boxes after D-ORA
    print("\nD-ORA targeted Box ID(s): " + str(target_idx))
    spoofed_data = {
        'point': spoofed_pts,
        'calib': data['calib']
    }
    spoofed_boxes, spoofed_info = get_prediction_info(spoofed_data,
                                                        target_idx, gt)
    for i in spoofed_info.keys():
        if spoofed_info[i]['score'] == 0:
            print(("After removing " + str(count_[i]) +
                    " points from Box ID: " + str(i) +
                    ", was not detected"))
        else:
            print("After removing " + str(count_[i]) +
                    " points from Box ID: " + str(i) +
                    ", was detected with IoU: " +
                    "{:.3f}".format(spoofed_info[i]['iou']) +
                    ", Conf: " + "{:.3f}".format(spoofed_info[i]['score']))

    # Save PCD
    if args.save_pcd == 1:
        save_spoofed_pcd(spoofed_pts)

    return

if __name__ == '__main__':
    main()


from django.shortcuts import render, redirect
from django.contrib.auth import logout as auth_logout
from .forms import *
from django.views.generic import *
from .models import *
from django.contrib.auth.models import AnonymousUser
from django.http import HttpResponseRedirect
from django.conf import settings
from django.views.generic.base import TemplateView
from django.contrib.auth.models import User
import datetime
import django.contrib.auth
from django.db.models import Q

def login(request):
    if request.user.is_authenticated:
        return redirect('home')
    else:
        return render(request, 'main/login.html')

def home(request):
    if request.user.is_authenticated:
        if request.method == 'POST':
            searchedState = request.POST['inputState']
            searchedWord = request.POST['input']
            if searchedState == 'Title':
                searchedList = Listing.objects.all().filter(book_title__icontains=searchedWord)
            elif searchedState == 'Author':
                searchedList = Listing.objects.all().filter(author__icontains=searchedWord)
            elif searchedState == 'Course':
                searchedList = Listing.objects.all().filter(book_course__icontains=searchedWord)
            elif searchedState == 'ISBN':
                searchedList = Listing.objects.all().filter(isbn=searchedWord)
            return render(request, 'main/home.html', {'searchedList': searchedList, 'searched': True})

        else:
            searchedList = Listing.objects.all()
            return render(request, 'main/home.html', {'searchedList': searchedList, 'searched': False})
    else:
        return redirect('/login')

def profile(request):
    # Profile.objects.filter(user=request.user).update(phone_number=request.POST["phoneNumber"])
    if request.user.is_authenticated:
        if request.method == 'POST':
            if 'phoneNumber' in request.POST:
                if(len(request.POST['phoneNumber']) == 10):
                    Profile.objects.filter(user=request.user).update(phone_number=request.POST["phoneNumber"])
            else:
                uploaded_file = request.FILES['document']
                store = FileSystemStorage('textexchange/uploads/'+str(request.user))
                store.save(uploaded_file.name, uploaded_file)
                Profile.objects.filter(user=request.user).update(profileImage=str(request.user)+'/'+str(uploaded_file))
        userid = request.user
        phonenumber = Profile.objects.get(user=request.user).phone_number
        if Profile.objects.get(user=request.user).profileImage:
            profileImage = Profile.objects.get(user=request.user).profileImage.url
        else:
            profileImage = "https://www.indiaspora.org/wp-content/uploads/2018/10/image-not-available.jpg"
        return render(request, 'main/profile.html', {'user': userid, 'phonenumber': phonenumber, 'profileImage': profileImage})
    else:
        return redirect('/login')

def sell(request):
    if request.user.is_authenticated:
        return render(request, 'main/sell.html')
    else:
        return redirect('/login')

# need to fix redirect
def listing(request):
    if request.user.is_authenticated:
        if request.method == 'POST':
            # form = ListingForm(request.POST, request.FILES)
            # # print(form)
            # if form.is_valid():
            #     print("working")
            #     newform = form.save(commit=False)
            #     newform.userid = Profile.objects.get(user=request.user)
            #     newform.save()
            #     return redirect('/sell/')
            listing = Listing()
            # listing.book_image = request.FILES['book_image']
            listing.seller_name = request.POST['seller_name']
            listing.book_title = request.POST['book_title']
            listing.author = request.POST['author']
            listing.isbn = request.POST['isbn']
            listing.book_course = request.POST['book_course']
            listing.price = request.POST['price']
            listing.book_description = request.POST['book_description']
            listing.userid = Profile.objects.get(user=request.user)
            listing.save()
            if 'book_image' in request.FILES:
                uploaded_file = request.FILES['book_image']
                store = FileSystemStorage('textexchange/uploads/textbook/'+str(request.user))
                store.save(uploaded_file.name, uploaded_file)
                Listing.objects.filter(book_title=request.POST['book_title'], userid=Profile.objects.get(user=request.user)).update(book_image='textbook/'+str(request.user)+'/'+str(uploaded_file))
            return redirect('/sell')
        # form = ListingForm()
        return render(request, 'main/listing.html')
    else:
        return redirect('/login')

# Gets to the payment page
class PaymentView(TemplateView):
    template_name = 'main/listings.html'
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['key'] = settings.RAVE_PUBLIC_KEY
        context['price'] = 1
        return context

# Page to indicate successful payment
class paymentSuccess(TemplateView):
    template_name = 'main/paymentSuccess.html'

# displays the listings that a user created
class DisplayUserListings(ListView):
    template_name = 'main/sell.html'

    def get(self, request, *args, **kwargs):
        # checks to see if user is logged in before trying to access Profile
        if(request.user.is_authenticated):
            form = ListingForm()
            id = Profile.objects.get(user=request.user)
            listings = Listing.objects.all().filter(userid=id)
            args = {'form': form, 'listings': listings}
            return render(request, self.template_name, args,)
        else:
            return redirect('/login')

# displays the listings to remove
class DisplayListingsRemove(ListView):
    template_name = 'main/removeListing.html'

    def get(self, request, *args, **kwargs):
        # checks to see if user is logged in before trying to access Profile
        if(request.user.is_authenticated):
            # deletes a listing
            if (request.GET.get('DeleteButton')):
                id = Profile.objects.get(user=request.user)
                Listing.objects.filter(book_title = request.GET.get('DeleteButton'), userid=id).delete()
                return redirect('/removeListing/')
            form = ListingForm()
            id = Profile.objects.get(user=request.user)
            listings = Listing.objects.all().filter(userid=id)
            args = {'form': form, 'listings': listings}
            return render(request, self.template_name, args,)
        else:
            return redirect('/login')

# displays listing on individual page

class ListingList(ListView):
    model = Listing

def listingdetail(request,id):
    if request.user.is_authenticated:
        listing = Listing.objects.get(id = id)
        return render(request, 'main/listings.html', {'listing': listing})
    else:
        return redirect('/login')

def message(request):
    if request.user.is_authenticated:
        if request.method == 'POST':
            form = ListingForm(request.POST)
            if form.is_valid():
                newform = form.save(commit=False)
                newform.userid = Profile.objects.get(user=request.user)
                newform.save()
                return redirect('/sell/')
        form = ListingForm()
        return render(request, 'main/listing.html', {'form': form})
    else:
        return redirect('/login')

class InboxView(View):
    def get(self, request, *args, **kwargs):
        args = {}
        if (request.user.is_authenticated):
            people = Map.objects.all().filter(Q(user=request.user) | Q(send=request.user)).order_by('-pub_date')
            num_results = Map.objects.all().filter(Q(user=request.user) | Q(send=request.user)).order_by('-pub_date').count()
            if(num_results!=0):
                args = {'people': people, 'user':request.user}
            return render(request, 'main/inbox.html', args)
        else:
            return redirect('/login')

    def post(self, request):
        if (request.user.is_authenticated):
            other = User.objects.all().filter(username=request.POST['rec'])
            #other = User.objects.get(username=request.POST['rec'])
            o = other.last()
            if(o):
                m = Message.objects.all().filter(Q(sender=request.user, recipient=o) | Q(sender=o, recipient=request.user)).order_by('-pub_date')
                args = {'m': m, 'o': o}
                return render(request, 'main/conversation.html', args)
            return render(request, 'main/message.html')
        else:
            return redirect('/login')

class SendMessageView(View):
    def get(self, request):
        args = {}
        if (request.user.is_authenticated):
            people = Map.objects.all().filter(user=request.user).order_by('-pub_date');
            args = {'people': people}
            return render(request, 'main/message.html', args, )
        else:
            return redirect('/login')

    #When I submit it will do the post
    def post(self, request):
        args = {}
        if (request.user.is_authenticated):
            other = User.objects.all().filter(username=request.POST['recipient'])
            o = other.last()
            if (o):
                sh = Map.objects.all().filter(Q(user=request.user, send = o) | Q(send=request.user, user = o))
                s = sh.first()
                if (s):
                    s.pub_date = django.utils.timezone.now()
                    s.save()
                else:
                    map = Map.objects.create(user=request.user, send=o, pub_date=django.utils.timezone.now())
                    map.save()
                mess = Message.objects.create(sender=request.user, recipient=o, message=request.POST['textbox'],
                                              pub_date=django.utils.timezone.now())
                mess.save()
                # m = Message.objects.all().filter(recipient=o)
                m = Message.objects.all().filter(Q(sender=request.user, recipient=o) | Q(sender=o, recipient=request.user)).order_by('-pub_date')
                args = {"other": o, 'm': m}
            else:
                b = bool(True)
                args = {"boolean" : b}
            return render(request, 'main/message.html', args, )
        else:
            return redirect('/login')

def message(request):
    args = {}
    if (request.user.is_authenticated):
        other = User.objects.all().filter(username=request.POST['recipient'])
        o = other.first()
        if(o):
            sh = Map.objects.all().filter(Q(user=request.user, send = o) | Q(send=request.user, user = o))
            s = sh.first()
            if(s):
                s.pub_date=django.utils.timezone.now()
                s.save()
            else:
                maps = Map.objects.create(user=request.user,send=o,pub_date=django.utils.timezone.now())
                maps.save()
            mess = Message.objects.create(sender=request.user, recipient=o, message=request.POST['textbox'],
                                             pub_date=django.utils.timezone.now())
            mess.save()
            # m = Message.objects.all().filter(recipient=o)
            m = Message.objects.all().filter(Q(sender=request.user, recipient=o) | Q(sender=o, recipient=request.user)).order_by('-pub_date')
            args = {"other": o, 'm': m}
        return render(request, 'main/message.html', args, )
    else:
        return redirect('/login')