from django.shortcuts import render, redirect, HttpResponse
from app.forms import ProdutosForm
from app.models import Produto, Empresa
import json

def home(request, pk):
    data = {}
    data['db'] = Produto.objects.filter(empresa_id=pk)
    return render(request, 'indexProducts.html', data)

def form(request, pk):
    data = {}
    data['form'] = ProdutosForm()
    data["empresa_id"] = pk
    return render(request, 'productsForm.html', data)

def create(request, pk):
    empresa = Empresa.objects.get(pk=pk)
    produto = Produto(empresa=empresa, nome=request.POST.get('nome'),
                      codigo=request.POST.get('codigo'),
                      descricao=request.POST.get('descricao'),
                      marca=request.POST.get('marca'),
                      valor=request.POST.get('valor')
                      )
    produto.save()
    return redirect('homeproduto', pk)

def view(request, pk):
    data = {}
    data['db'] = Produto.objects.get(pk=pk)
    return render(request, 'viewProducts.html', data)

def edit(request, pk):
    data = {}
    data['db'] = Produto.objects.get(pk=pk)
    data['form'] = ProdutosForm(instance=data['db'])
    return render(request, 'productsForm.html', data)

def update(request, pk):
    data = {}
    data['db'] = Produto.objects.get(pk=pk)
    form = ProdutosForm(request.POST or None, instance=data['db'])
    if form.is_valid():
        form.save()
        return redirect('home')

def delete(request, pk):
    db = Produto.objects.get(pk=pk)
    db.delete()
    return redirect('home')


import numpy as np
import starfile
import pandas as pd
import argparse
import traceback

parser = argparse.ArgumentParser(description='')
parser.add_argument('--particles', type=str, required=True, help='Input particles.star file')
#parser.add_argument('--tomoname', type=str, required=True, help='Specify tomogram name')
#parser.add_argument('--binfactor', type=float, required=True, help='Specify the bin factor')
args = parser.parse_args()

def find_points_within_distance(a_points, b_points, c_points, distance_cutoff):
    # Convert point arrays to numpy arrays
    a_points = np.array(a_points)
    b_points = np.array(b_points)
    c_points = np.array(c_points)

    # Calculate distances between each point of type c and points of type a
    distances_a = np.linalg.norm(a_points[:, np.newaxis, :] - c_points, axis=2)

    """ for i in range(2):
        for j in range(3):
            v = a_points[j] - c_points[i]
            print(np.sqrt(np.sum(v*v))) """

    # Find indices where distances are within the cutoff for points of type a
    indices_a_within_cutoff = np.any(distances_a < distance_cutoff, axis=1)

    # Calculate distances between each point of type c and points of type b
    distances_b = np.linalg.norm(b_points[:, np.newaxis, :] - c_points, axis=2)

    # Find indices where distances are within the cutoff for points of type b
    indices_b_within_cutoff = np.any(distances_b < distance_cutoff, axis=1)

    # Extract points of type a and b within the cutoff
    points_a_within_cutoff = a_points[indices_a_within_cutoff]
    points_b_within_cutoff = b_points[indices_b_within_cutoff]

    # Combine indices to find c points near both a and b
    #indices_near_both_a_b = np.logical_or(indices_a_within_cutoff, indices_b_within_cutoff)

    # Extract c points near both a and b
    # c_points_near_a = c_points[indices_a_within_cutoff]
    # c_points_near_b = c_points[indices_b_within_cutoff]
    # c_points_near_a_b = np.concatenate((c_points_near_a, c_points_near_b), axis=0)

    # return points_a_within_cutoff, points_b_within_cutoff#, c_points_near_a_b
    return indices_a_within_cutoff, indices_b_within_cutoff

def select_by_tomoname(df, tname):
    classlist = df["rlnClassNumber"]
    xyz_cls1 = []
    xyz_cls2 = []
    xyz_cls4 = []
    rtp_cls1 = []
    rtp_cls2 = []
    rtp_cls4 = []

    for i, _ in enumerate(classlist):
        if df["rlnTomoName"][i] == tname:
            if df["rlnClassNumber"][i] == 1:
                xyz_cls1.append([df["rlnCoordinateX"][i], df["rlnCoordinateY"][i], df["rlnCoordinateZ"][i]])
                rtp_cls1.append([df["rlnAngleRot"][i], df["rlnAngleTilt"][i], df["rlnAnglePsi"][i]])
            if df["rlnClassNumber"][i] == 2:
                xyz_cls2.append([df["rlnCoordinateX"][i], df["rlnCoordinateY"][i], df["rlnCoordinateZ"][i]])
                rtp_cls2.append([df["rlnAngleRot"][i], df["rlnAngleTilt"][i], df["rlnAnglePsi"][i]])
            if df["rlnClassNumber"][i] == 4:
                xyz_cls4.append([df["rlnCoordinateX"][i], df["rlnCoordinateY"][i], df["rlnCoordinateZ"][i]])
                rtp_cls4.append([df["rlnAngleRot"][i], df["rlnAngleTilt"][i], df["rlnAnglePsi"][i]])

    # output class 2 points for sanity check

    dfx = pd.DataFrame()
    xx = [row[0] for row in xyz_cls2]
    dfx["rlnTomoName"] = [tname for _ in range(len(xx))]
    dfx["rlnCoordinateX"] = [row[0] for row in xyz_cls2]
    dfx["rlnCoordinateY"] = [row[1] for row in xyz_cls2]
    dfx["rlnCoordinateZ"] = [row[2] for row in xyz_cls2]
    dfx["rlnClassNumber"] = [2 for _ in range(len(xx))]

    outfilename = 'cls2_particles_%s.star' % tname
    starfile.write(dfx, outfilename, overwrite=True)

    distance_cutoff = 500. # Angstroms

    a_points = np.array(xyz_cls1, dtype=float)
    b_points = np.array(xyz_cls4, dtype=float)
    c_points = np.array(xyz_cls2, dtype=float)

    a_points_rtp = np.array(rtp_cls1, dtype=float)
    b_points_rtp = np.array(rtp_cls4, dtype=float)

    indices_a_within_cutoff, indices_b_within_cutoff = find_points_within_distance(
        a_points=a_points,
        b_points=b_points,
        c_points=c_points,
        distance_cutoff=distance_cutoff)

    points_a_within_cutoff = a_points[indices_a_within_cutoff]
    points_b_within_cutoff = b_points[indices_b_within_cutoff]

    a_points_rtp_within_cutoff = a_points_rtp[indices_a_within_cutoff]
    b_points_rtp_within_cutoff = b_points_rtp[indices_b_within_cutoff]

    tma = []
    if 0 < len(points_a_within_cutoff):
        xa = points_a_within_cutoff[:, 0]
        ya = points_a_within_cutoff[:, 1]
        za = points_a_within_cutoff[:, 2]
        ra = a_points_rtp_within_cutoff[:, 0]
        ta = a_points_rtp_within_cutoff[:, 1]
        pa = a_points_rtp_within_cutoff[:, 2]
        tma = [tname for _ in range(len(xa))]
        cls1_number = [1 for _ in range(len(xa))]

    tmb = []
    if 0 < len(points_b_within_cutoff):
        xb = points_b_within_cutoff[:, 0]
        yb = points_b_within_cutoff[:, 1]
        zb = points_b_within_cutoff[:, 2]
        rb = b_points_rtp_within_cutoff[:, 0]
        tb = b_points_rtp_within_cutoff[:, 1]
        pb = b_points_rtp_within_cutoff[:, 2]
        tmb = [tname for _ in range(len(xb))]
        cls4_number = [4 for _ in range(len(xb))]

    # Output those particles into a star file
    try:
        if 0 < len(tma) and 0 < len(tmb):
            tomoname = tma + tmb
            cls_number = cls1_number + cls4_number
            x = xa.tolist() + xb.tolist()
            y = ya.tolist() + yb.tolist()
            z = za.tolist() + zb.tolist()
            rot = ra.tolist() + rb.tolist()
            tilt = ta.tolist() + tb.tolist()
            psi = pa.tolist() + pb.tolist()
        elif 0 < len(tma) and not (0 < len(tmb)):
            tomoname = tma
            cls_number = cls1_number
            x = xa.tolist()
            y = ya.tolist()
            z = za.tolist()
            rot = ra.tolist()
            tilt = ta.tolist()
            psi = pa.tolist()
        elif not (0 < len(tma)) and 0 < len(tmb):
            tomoname = tmb
            cls_number = cls4_number
            x = xb.tolist()
            y = yb.tolist()
            z = zb.tolist()
            rot = rb.tolist()
            tilt = tb.tolist()
            psi = pb.tolist()
    except:
        print(traceback.exit())

    return x, y, z, rot, tilt, psi, cls_number, tomoname

# Example usage:
#a_points = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
#b_points = np.array([[10, 11, 12], [13, 14, 15], [16, 17, 18]])
#c_points = np.array([[19, 20, 21], [22, 23, 24]])

#distance_cutoff = 20.0

#points_a_within_cutoff, points_b_within_cutoff = find_points_within_distance(a_points, b_points, c_points, distance_cutoff)

#print("Points of type a within cutoff:", points_a_within_cutoff)
#print("Points of type b within cutoff:", points_b_within_cutoff)

# Reading from particles.star file into classes
df = starfile.read(args.particles)

uniq_tnames = set(df["rlnTomoName"])
xlist = []
ylist = []
zlist = []
rotlist = []
tiltlist = []
psilist = []
clslist = []
tomolist = []
for tname in uniq_tnames:
    x, y, z, rot, tilt, psi, cls_number, tomoname = select_by_tomoname(df, tname)

    df2 = pd.DataFrame()
    df2["rlnTomoName"] = tomoname
    df2["rlnCoordinateX"] = x
    df2["rlnCoordinateY"] = y
    df2["rlnCoordinateZ"] = z
    df2["rlnAngleRot"] = rot
    df2["rlnAngleTilt"] = tilt
    df2["rlnAnglePsi"] = psi
    df2["rlnClassNumber"] = cls_number

    outfilename = 'neighbor_particles_%s.star' % tname
    starfile.write(df2, outfilename, overwrite=True)

    xlist += x
    ylist += y
    zlist += z
    rotlist += rot
    tiltlist += tilt
    psilist += psi
    clslist += cls_number
    tomolist += tomoname

# Output all particles
df3 = pd.DataFrame()
df3["rlnTomoName"] = tomolist
df3["rlnCoordinateX"] = xlist
df3["rlnCoordinateY"] = ylist
df3["rlnCoordinateZ"] = zlist
df3["rlnAngleRot"] = rotlist
df3["rlnAngleTilt"] = tiltlist
df3["rlnAnglePsi"] = psilist
df3["rlnClassNumber"] = clslist

outfilename = 'neighbor_particles.star'
starfile.write(df3, outfilename, overwrite=True)


import sys
from PyQt5 import QtCore
import matplotlib.pyplot as plt
import numpy as np
import JDataObject as Jdata
from matplotlib.colors import LinearSegmentedColormap
from PyQt5.QtWidgets import QApplication

# ==============================================================
# Global variable to declare
vmin = 10.                                  # vmin_rho
vmax = 4000.                                 # vmax_rho
vsmin = 1.                                  # vmin_rho
vsmax = 20.                                 # vmax_rho
fig, ((ax, ax2), (ax3, ax4)) = plt.subplots(2, 2)
update = True
line_direction = 'north'
# Head to bottom of script to identify file path
# =============================================================

def file_changed(path):
    """
    method for updating plot when file changes

    """
    print('File Changed: %s' % path)
    #  ============================================================
    # define the file required for import
    fileName = path
    ax.clear()
    ax2.clear()
    ax3.clear()
    ax4.clear()
    injection_location_x = []
    injection_location_y = []
    xp_rho = []
    yp_rho = []
    xp_mx = []
    yp_mx = []
    val = []
    val2 = []
    id1_rho = []
    id2_rho = []
    id1_mx = []
    id2_mx = []
    xp_rho_rev = []
    yp_rho_rev = []
    xp_mx_rev = []
    yp_mx_rev = []
    val_rev = []
    val2_rev = []
    id1_rho_rev = []
    id2_rho_rev = []
    id1_mx_rev = []
    id2_mx_rev = []
    injection = []
    # =================================================================
    # Code Start
    patch = Jdata.loadDias(fileName)               # loads data
    print(len(patch.readings))
    # calculated mid-pt data points
    for src in range(len(patch.readings)):
        injection_location_x.append(patch.readings[src].Idp.Tx1)
        injection_location_y.append(25.0)
        injection.append(patch.readings[src].MemNumber)
        for rx in range(len(patch.readings[src].Vdp)):
            if line_direction == 'east':
                patch.readings[src].Vdp[rx].direction = False

            if patch.readings[src].Vdp[rx].flagRho == "Accept":
                try:
                    if (patch.readings[src].Idp.Tx1 <
                            patch.readings[src].Vdp[rx].Rx1):
                        xp_rho.append(
                            patch.readings[src].Vdp[rx].getXplotpoint(
                                patch.readings[src].Idp))
                        yp_rho.append(
                            patch.readings[src].Vdp[rx].getZplotpoint(
                                patch.readings[src].Idp))
                        val.append(
                            np.abs(patch.readings[src].Vdp[rx].Rho))
                        id1_rho.append(
                            patch.readings[src].Vdp[rx].Rx1File[:2])
                        id2_rho.append(
                            patch.readings[src].Vdp[rx].Rx2File[:2])
                    elif (patch.readings[src].Idp.Tx1 >
                            patch.readings[src].Vdp[rx].Rx1):
                        xp_rho_rev.append(
                            patch.readings[src].Vdp[rx].getXplotpoint(
                                patch.readings[src].Idp))
                        yp_rho_rev.append(
                            patch.readings[src].Vdp[rx].getZplotpoint(
                                patch.readings[src].Idp))
                        val_rev.append(
                            np.abs(patch.readings[src].Vdp[rx].Rho))
                        id1_rho_rev.append(
                            patch.readings[src].Vdp[rx].Rx1File[:2])
                        id2_rho_rev.append(
                            patch.readings[src].Vdp[rx].Rx2File[:2])
                except:
                    print("something wrong with file loading")
                    pass
            if patch.readings[src].Vdp[rx].flagMx == "Accept":
                try:
                    if (patch.readings[src].Idp.Tx1 <
                            patch.readings[src].Vdp[rx].Rx1):
                        xp_mx.append(
                            patch.readings[src].Vdp[rx].getXplotpoint(
                                patch.readings[src].Idp))
                        yp_mx.append(
                            patch.readings[src].Vdp[rx].getZplotpoint(
                                patch.readings[src].Idp))
                        val2.append(
                            np.abs(patch.readings[src].Vdp[rx].Mx))
                        id1_mx.append(
                            patch.readings[src].Vdp[rx].Rx1File[:2])
                        id2_mx.append(
                            patch.readings[src].Vdp[rx].Rx2File[:2])
                    elif (patch.readings[src].Idp.Tx1 >
                            patch.readings[src].Vdp[rx].Rx1):
                        xp_mx_rev.append(
                            patch.readings[src].Vdp[rx].getXplotpoint(
                                patch.readings[src].Idp))
                        yp_mx_rev.append(
                            patch.readings[src].Vdp[rx].getZplotpoint(
                                patch.readings[src].Idp))
                        val2_rev.append(
                            np.abs(patch.readings[src].Vdp[rx].Mx))
                        id1_mx_rev.append(
                            patch.readings[src].Vdp[rx].Rx1File[:2])
                        id2_mx_rev.append(
                            patch.readings[src].Vdp[rx].Rx2File[:2])
                except:
                    print("something wrong with file loading")
                    pass

    # convert to numpy
    midx = np.asarray(xp_rho)
    midz = np.asarray(yp_rho)
    midx_rev = np.asarray(xp_rho_rev)
    midz_rev = np.asarray(yp_rho_rev)
    injection_location_y = np.asarray(injection_location_y)
    injection_location_x = np.asarray(injection_location_x)
    # check which data to plot
    name = 'custom_div_cmap'
    pcolorOpts = {}
    custom_map = LinearSegmentedColormap.from_list(name=name,
                                                   colors=['aqua',
                                                           [0, 0.85, 1, 1],
                                                           [0.1, 1, 0.1, 1],
                                                           'yellow',
                                                           [1, 0.7, 0, 1],
                                                           [1, 0.2, 0.2, 1],
                                                           [0.95, 0.9, 1, 1]],
                                                   N=200)
    # ==========================================================
    # Plot forward direction Resistivity
    ax.axes.set_title("Apparent Resistivity (ohm-m)", y=1.14)
    try:
        ph = ax.scatter(xp_rho, yp_rho, c=val, cmap=custom_map,
                        vmin=vmin, vmax=vmax, clim=(vmin, vmax))
        #  plotting dipole info text
        for i, txt in enumerate(val):
            idizzle = id1_rho[i] + "-" + id2_rho[i]
            ax.annotate(idizzle, (midx[i], midz[i]), size=6)
        # Initiate color bar if needed
        if update:
            cbar = plt.colorbar(ph, ax=ax,
                                format="%.0f", fraction=0.04,
                                orientation="vertical")

    except ValueError:
        print("no data to plot in forward Rho")
    # =============================================================
    # Plot reverse direction Resistivity
    try:
        ph_reverse = ax3.scatter(xp_rho_rev, yp_rho_rev, c=val_rev,
                                 cmap=custom_map,
                                 vmin=vmin, vmax=vmax, clim=(vmin, vmax))
        #  plotting dipole info text
        for i, txt in enumerate(val_rev):
            idizzle2 = id1_rho_rev[i] + "-" + id2_rho_rev[i]
            ax3.annotate(idizzle2, (midx_rev[i], midz_rev[i]), size=6)
        # Initiate color bar if needed
        if update:
            cbar3 = plt.colorbar(ph_reverse, ax=ax3,
                                 format="%.0f", fraction=0.04,
                                 orientation="vertical")

    except ValueError:
        print("no data to plot in reverse Rho")
    #  ===========================================================
    #  plot the injection text
    for i in range(len(patch.readings)):
        ax.annotate(int(injection[i]), (injection_location_x[i],
                    injection_location_y[i]), size=6)
        ax2.annotate(int(injection[i]), (injection_location_x[i],
                     injection_location_y[i]), size=6)
        ax3.annotate(int(injection[i]), (injection_location_x[i],
                     injection_location_y[i]), size=6)
        ax4.annotate(int(injection[i]), (injection_location_x[i],
                     injection_location_y[i]), size=6)

    #  ===========================================================
    #  Plot forward direction Chargeability
    name = 'custom_div_cmap1'
    ax2.axes.set_title("Apparent Chargeability (mV/V)", y=1.11)
    try:
        ph2 = ax2.scatter(xp_mx, yp_mx, c=val2, cmap=custom_map,
                          vmin=vsmin, vmax=vsmax, clim=(vmin, vmax))
        #  plot dipole info text
        for i, txt in enumerate(val2):
            idizzle = id1_mx[i] + "-" + id2_mx[i]
            ax2.annotate(idizzle, (midx[i], midz[i]), size=6)
        # Initiate colorbar if needed
        if update:
            cbar2 = plt.colorbar(ph2, ax=ax2,
                                 format="%.0f", fraction=0.04,
                                 orientation="vertical")

    except ValueError:
            print("no data to plot in forward Mx")

    #  =========================================================
    #  Plot reverse direction Chargeability
    try:
        ph4 = ax4.scatter(xp_mx_rev, yp_mx_rev, c=val2_rev, cmap=custom_map,
                          vmin=vsmin, vmax=vsmax, clim=(vmin, vmax))
        #  plot dipole info text
        for i, txt in enumerate(val2_rev):
            idizzle2 = id1_mx_rev[i] + "-" + id2_mx_rev[i]
            ax4.annotate(idizzle2, (midx_rev[i], midz_rev[i]), size=6)
        # Initiate colorbar if needed
        if update:
            cbar4 = plt.colorbar(ph4, ax=ax4,
                                 format="%.0f", fraction=0.04,
                                 orientation="vertical")

    except ValueError:
            print("no data to plot in reverse Mx")

    # ============================================================
    #  adjust plotting axis for new data as it comes in
    try:
        ax.axes.get_yaxis().set_visible(False)
        ax.axes.set_ylim(np.min(midz) - 50, np.max(midz) + 100)
        ax.axes.set_xlim(np.min(midx) - 250, np.max(midx) + 100)
        ax.xaxis.tick_top()
        ax.xaxis.set_label_position('top')
        ax.tick_params(labelsize=8)
        ax2.axes.get_yaxis().set_visible(False)
        ax2.axes.set_ylim(np.min(midz) - 50, np.max(midz) + 100)
        ax2.axes.set_xlim(np.min(midx) - 250, np.max(midx) + 100)
        ax2.xaxis.tick_top()
        ax2.xaxis.set_label_position('top')
    except ValueError:
        pass
    try:
        ax2.tick_params(labelsize=8)
        ax3.axes.get_yaxis().set_visible(False)
        ax3.axes.set_ylim(np.min(midz_rev) - 50, np.max(midz_rev) + 100)
        ax3.axes.set_xlim(np.min(midx_rev) - 250, np.max(midx_rev) + 100)
        ax3.xaxis.tick_top()
        ax3.xaxis.set_label_position('top')
        ax3.tick_params(labelsize=8)
        ax4.axes.get_yaxis().set_visible(False)
        ax4.axes.set_ylim(np.min(midz_rev) - 50, np.max(midz_rev) + 100)
        ax4.axes.set_xlim(np.min(midx_rev) - 250, np.max(midx_rev) + 100)
        ax4.xaxis.tick_top()
        ax4.xaxis.set_label_position('top')
        ax4.tick_params(labelsize=8)
    except ValueError:
        pass
    plt.draw()
# ================================================================

# ==================================================================
# Running the Application
app = QApplication(sys.argv)

paths = ["C:\\Users\\johnk\\devProjects\\Python\\sectionplots\\L4250.dat"]
# "C:\Users\johnk\devProjects\Python\sectionplots\L0L1A.dat"
try:
    file_changed(paths[0])
    update = False
except:
    print("no file ready for plotting")

fs_watcher = QtCore.QFileSystemWatcher(paths)
# fs_watcher.directoryChanged.connect(directory_changed)
fs_watcher.fileChanged.connect(file_changed)
plt.show()

sys.exit(app.exec_())


from collections import deque

import numpy as np
import carla

PID_PARAMS = [
        dict(K_P=0.5,  K_I=4.0, K_D=0.1,  fps=20, n=40),
        dict(K_P=1.5,  K_I=3.0, K_D=0.1,  fps=20, n=40),
        dict(K_P=0.75,  K_I=2.0, K_D=0.2, fps=20, n=30),
        dict(K_P=1.0,  K_I=3.0, K_D=0.05, fps=20, n=80),
        dict(K_P=1.25,  K_I=3.0, K_D=0.05,  fps=20, n=80),
        ]

class PIDController(object):
    def __init__(self, K_P=1.0, K_I=0.0, K_D=0.0, fps=10, n=20, **kwargs):
        print(K_P, K_I, K_D, fps, n, **kwargs)

        self._K_P = K_P
        self._K_I = K_I
        self._K_D = K_D

        self._dt = 1.0 / fps
        self._n = n
        self._window = deque(maxlen=self._n)

    def step(self, error):
        self._window.append(error)

        if len(self._window) >= 2:
            integral = np.mean(self._window) * self._dt
            derivative = (self._window[-1] - self._window[-2]) / self._dt
        else:
            integral = 0.0
            derivative = 0.0

        return self._K_P * error + self._K_I * integral + self._K_D * derivative

class Controller(object):
    def __init__(self, pid):
        self.brake_threshold = 0.5

        self.turn_control = PIDController(**PID_PARAMS[pid])
        self.speed_control = PIDController(K_P=1.0, K_I=1.0, K_D=0.5, fps=20, n=100)

    def postprocess(self, steer, throttle, brake):
        control = carla.VehicleControl()
        control.steer = np.clip(steer, -1.0, 1.0)
        control.throttle = np.clip(throttle, 0.0, 1.0)
        control.brake = np.clip(brake, 0.0, 1.0)
        control.manual_gear_shift = False

        return control

    def run_step(self, target, target_speed, speed, viz=None):
        target_speed = np.clip(target_speed, 0.0, 7.5)
        delta = np.clip(target_speed - speed, 0.0, 1)
        theta = np.radians(90 - np.degrees(np.arctan2(target[1], target[0])))

        steer = self.turn_control.step(theta)
        throttle = self.speed_control.step(delta)
        brake = 0.0

        self.speed = speed
        self.target_speed = target_speed

        # Slow or stop.
        if target_speed < self.brake_threshold:
            steer = 0.0
            throttle = 0.0
            brake = 0.5

        control = self.postprocess(steer, throttle, brake)

        if viz:
            viz.controller_draw(speed, target_speed, control)

        return control


"""
Basic example of BST
"""

from __future__ import annotations
from dataclasses import dataclass
import random

@dataclass
class Node:
    value: int
    left: Node = None
    right: Node = None

@dataclass
class Tree:
    root: Node = None

    def insert(self, value: int) -> Node:
        """Inserts new node to the tree."""
        node = Node(value)

        if self.root is None:
            self.root = node
            return
        else:
            current = self.root

        while current:
            if node.value < current.value:
                if current.left is None:
                    current.left = node
                    break
                else:
                    current = current.left
            elif current.value < node.value:
                if current.right is None:
                    current.right = node
                    break
                else:
                    current = current.right
        return node

    @property
    def min(self) -> Node:
        """Finds minimum node"""
        current = self.root
        while current.left:
            current = current.left
        return current

    @property
    def max(self) -> Node:
        """Finds node with max value"""
        current = self.root
        while current.right:
            current = current.right
        return current

    def find(self, value: int) -> Node:
        """Finds node by value."""
        current = self.root
        while current:
            if current.value == value:
                return current
            current = current.left if value < current.value else current.right
        return current

    def in_order(self, node: Node) -> Node:
        """
        Process left sub tree
        Current tree
        Process right sub tree
        """
        if node.left:
            for n in self.in_order(node.left):
                yield n
        yield node
        if node.right:
            for n in self.in_order(node.right):
                yield n

    def __iter__(self) -> Tree:
        self.current = self.in_order(self.root)
        return self

    def __next__(self) -> Node:
        return next(self.current)

    def __repr__(self) -> str:
        """In-order tree print"""
        trace = []
        for n in self.in_order(self.root):
            trace.append(n.value)

        return "->".join(map(str, trace))

if __name__ == "__main__":
    MAX_VAL = 10 ** 3
    NODES = 20
    random.seed(42)
    root_value = random.randint(0, MAX_VAL)
    tree = Tree(Node(root_value))
    for i in random.sample(range(MAX_VAL), NODES):
        tree.insert(i)

    print(f"Root value: {tree.root.value}")
    print(f"min tree value {tree.min.value}")
    print(f"max tree value {tree.max.value}")

    print(tree)

    for node in tree:
        print(node.value)

    # this value is not part of the tree
    node = tree.find(256)
    assert node is None
    node = tree.find(250)
    assert node.value == 250

    print("DONE")


#This program calculates and plots a mean-variance function for a portfolio
#The idea is based upon the work of Markowitz

from matplotlib import pyplot as plt
from matplotlib.pyplot import xlabel, ylabel
from math import *

def calc(m1, s1, m2, s2, r12):
    l = []
    mpf = 0
    spf = 0
    minvar = 0
    omega = 0
    #Caclulates all the mean-variance points ((y, x) points) for each omega += 0.01 starting from 0
    for i in range(100):
        mpf = omega*m1+(1-omega)*m2
        spf = (omega*omega*s1*s1)+(1-omega)*(1-omega)*s2*s2+2*omega*(1-omega)*s1*s2*r12
        spf = sqrt(spf)
        minvar = 2*omega*s1*s1-s2*s2+(2-4*omega)*s1*s2*r12
        l.append((spf, mpf, omega, minvar))
        omega += 0.01

    return l

g = calc(0.04, 0.03, 0.06, 0.05, 0.2)
x_val = [x[0] for x in g]
y_val = [x[1] for x in g]
plt.plot(x_val, y_val)
plt.ylabel("Mean", fontsize = 14, color = "red")
plt.xlabel("Variance", fontsize = 14, color = "red")
plt.show()


"""Splits the raw data output from mturk into a test and validation set appropriate for the model eval and test sets.

For the diabetes data, there's a strange character that causes line breaks in the final data, but I can't seem to get
rid of it... After processing the diabetes data, I went through and removed these characters manually...
"""
import argparse
import json

import numpy as np

np.random.seed(0)
parser = argparse.ArgumentParser()
parser.add_argument('--raw_data_location', type=str, required=True)
parser.add_argument('--dataset', type=str, required=True)
parser.add_argument('--acceptance_threshold', type=float, default=3.0)
args = parser.parse_args()

ACCEPTANCE_THRESHOLD = args.acceptance_threshold

with open(args.raw_data_location, 'r') as file:
    data = json.load(file)
    data_dict = json.loads(data)

original_sentence_hit_ids = list(data_dict.keys())
assert len(original_sentence_hit_ids) == 50, f"Original set has len {len(original_sentence_hit_ids)}!"
test_set_keys = original_sentence_hit_ids
assert len(test_set_keys) == 50, f"Test set has len {len(test_set_keys)}!"

def build_test_suite_string(hit_ids):
    return_string = ""
    for hid in hit_ids:
        hit = data_dict[hid]
        parse = hit["question_parse"]
        for revision_hit_id in hit["revisions"]:
            revision_scores = hit["revisions"][revision_hit_id]["scores"]
            revision_text = hit["revisions"][revision_hit_id]["revised_text"]
            scores = np.array(revision_scores)
            if np.mean(scores) >= ACCEPTANCE_THRESHOLD:
                if revision_text in return_string:
                    continue
                revision_text = revision_text.replace("\n", "")
                revision_text = revision_text.replace("\r", "")
                parse = parse.replace("\n", "")
                parse = parse.replace("\r", "")

                # resolve annotation errors
                parse = parse.replace("previous filter", "previousfilter")
                parse = parse.replace("score accuracy test", "score accuracy")
                parse = parse.replace("greater equal to", "greater equal than")

                return_string += revision_text + "\n"
                return_string += parse + "\n\n"
    while return_string.endswith("\n"):
        return_string = return_string[:-len("\n")]
    return return_string

def save(data_to_save, filename):
    with open(filename, 'w') as file_to_write:
        file_to_write.write(data_to_save)

test_string = build_test_suite_string(test_set_keys)
split_test_string = test_string.split('\n\n')
print(f"Test set has {len(split_test_string)} items")

test_file_name = f"{args.dataset}_test_suite.txt"
eval_file_name = f"{args.dataset}_eval_suite.txt"

save(test_string, test_file_name)


from flask import Flask, render_template, flash, request, redirect
app = Flask (__name__)
app.secret_key = 'secret key'

users={
    "mr admin": "admin",
    "ms admin": "admin",
    "mr user": "user",
    "ms user": "user"
}

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == 'POST':
        if users[request.form['username']] != request.form['password']:
            return render_template('invalid.html')
        else:
            return redirect ('/cabinet')
    else:
        return render_template('login.html')

@app.route("/cabinet")
def cabinet():
    return render_template('cabinet.html')

if __name__ == "__main__":
    app.run(host='localhost', port=5000, debug=True)


# pytorch
import torch

import os
import numpy as np
import cv2

class YOLOOutput2BB():
    """
    grid_scale: The grid size of the model output.
    x_scale: The x scale of input.
    y_scale: The y scale of input.
    z_scale: The z scale of input.
    """
    def __init__(self, grid_scale, x_scale, y_scale, z_scale, conf_thresh, device):
        self.grid_scale = grid_scale
        self.x_scale = x_scale
        self.st_center_x = x_scale / 2
        self.y_scale = y_scale
        self.st_center_y = y_scale / 2
        self.z_scale = z_scale

        self.conf_thresh = conf_thresh
        self.device = device

    def __call__(self, model_output):
        conf_tensor = (model_output[:, 0] > self.conf_thresh).nonzero()
        onehot = torch.zeros((model_output.size()[0], self.grid_scale, self.grid_scale, self.grid_scale),
                             dtype=torch.float32).zero_().to(self.device)
        onehot[conf_tensor[:, 0], conf_tensor[:, 1], conf_tensor[:, 2], conf_tensor[:, 3]] = 1

        # xyz to grid scale
        model_output[conf_tensor[:, 0], 1, conf_tensor[:, 1], conf_tensor[:, 2], conf_tensor[:, 3]] += conf_tensor[:, 1]
        model_output[conf_tensor[:, 0], 2, conf_tensor[:, 1], conf_tensor[:, 2], conf_tensor[:, 3]] += conf_tensor[:, 2]
        model_output[conf_tensor[:, 0], 3, conf_tensor[:, 1], conf_tensor[:, 2], conf_tensor[:, 3]] += conf_tensor[:, 3]
        # xyz whd
        x = (model_output[:, 1]) * onehot
        x = ((x[x > 0] / self.grid_scale) * self.x_scale) - self.st_center_x
        y = model_output[:, 2] * onehot
        y = ((y[y > 0] / self.grid_scale) * self.y_scale) - self.st_center_y
        z = model_output[:, 3] * onehot
        z = ((z[z > 0] / self.grid_scale) * self.z_scale)
        w = model_output[:, 4] * onehot
        w = w[w > 0] * self.x_scale
        h = model_output[:, 5] * onehot
        h = h[h > 0] * self.y_scale
        d = model_output[:, 6] * onehot
        d = d[d > 0] * self.z_scale

        # xxyyzz
        x1 = (x - (w/2)).unsqueeze(0)
        x2 = (x + (w/2)).unsqueeze(0)
        y1 = (y - (h/2)).unsqueeze(0)
        y2 = (y + (h/2)).unsqueeze(0)
        z1 = (z - (d/2)).unsqueeze(0)
        z2 = (z + (d/2)).unsqueeze(0)
        # print('x1', x1)
        # print('x2', x2)
        # print('y1', y1)
        # print('y2', y2)
        # print('z1', z1)
        # print('z2', z2)
        bb = torch.cat((x1, x2, y1, y2, z1, z2), dim=0)
        bb = torch.transpose(bb, dim0=0, dim1=1)
        # print(bb)

        return bb

class SaveData():
    def __init__(self, color_save_flag, depth_save_flag, txt_save_flag, save_dir):
        """
        Currently, this function is needed to only 1 frame as input.
        :param color_save_flag: Flag of saving color image
        :param depth_save_flag: Flag of saving depth image
        :param txt_save_flag: Flag of saving the txt of bounding boxes.
        :param save_dir:
        """
        os.makedirs(save_dir + '/color', exist_ok=True)
        os.makedirs(save_dir + '/depth', exist_ok=True)
        os.makedirs(save_dir + '/bb', exist_ok=True)

        self.color_flag = color_save_flag
        self.depth_flag = depth_save_flag
        self.txt_flag = txt_save_flag
        self.save_dir = save_dir

    def __call__(self, save_name, color=None, depth=None, bb=None):
        if self.color_flag is True and color is None:
            raise Exception('SaveData Error: color_save_flag is True, but there is no color input.')
        elif self.depth_flag is True and depth is None:
            raise Exception('SaveData Error: depth_save_flag is True, but there is no depth input.')
        elif self.txt_flag is True and bb is None:
            raise Exception('SaveData Error: txt_save_flag is True, but there is no bb input.')
        else:
            if self.color_flag is True:
                self.color_save(color=color, name=save_name)
            if self.depth_flag is True:
                self.depth_save(depth=depth, name=save_name)
            if self.txt_flag is True:
                self.bb_save(bb=bb, name=save_name)

    def color_save(self, color, name):
        cv2.imwrite(filename=self.save_dir + '/color/' + name + '.png', img=color)

    def depth_save(self, depth, name):
        cv2.imwrite(filename=self.save_dir + '/depth/' + name + '.png', img=depth)

    def bb_save(self, bb, name):
        bb = bb.to("cpu").numpy()
        np.savetxt(self.save_dir + '/bb/' + name + '.txt', bb)


from __future__ import annotations

from datetime import timedelta
from unittest.mock import patch

import pytest

from airflow import AirflowException, example_dags as example_dags_module
from airflow.models import DagBag
from airflow.models.dagcode import DagCode

# To move it to a shared module.
from airflow.utils.file import open_maybe_zipped
from airflow.utils.session import create_session
from tests.test_utils.db import clear_db_dag_code

def make_example_dags(module):
    """Loads DAGs from a module for test."""
    dagbag = DagBag(module.__path__[0])
    return dagbag.dags

class TestDagCode:
    """Unit tests for DagCode."""

    def setup_method(self):
        clear_db_dag_code()

    def teardown_method(self):
        clear_db_dag_code()

    def _write_two_example_dags(self):
        example_dags = make_example_dags(example_dags_module)
        bash_dag = example_dags["example_bash_operator"]
        DagCode(bash_dag.fileloc).sync_to_db()
        xcom_dag = example_dags["example_xcom"]
        DagCode(xcom_dag.fileloc).sync_to_db()
        return [bash_dag, xcom_dag]

    def _write_example_dags(self):
        example_dags = make_example_dags(example_dags_module)
        for dag in example_dags.values():
            dag.sync_to_db()
        return example_dags

    def test_sync_to_db(self):
        """Dg code can be written into database."""
        example_dags = self._write_example_dags()

        self._compare_example_dags(example_dags)

    def test_bulk_sync_to_db(self):
        """Dg code can be bulk written into database."""
        example_dags = make_example_dags(example_dags_module)
        files = [dag.fileloc for dag in example_dags.values()]
        with create_session() as session:
            DagCode.bulk_sync_to_db(files, session=session)
            session.commit()

        self._compare_example_dags(example_dags)

    def test_bulk_sync_to_db_half_files(self):
        """Dg code can be bulk written into database."""
        example_dags = make_example_dags(example_dags_module)
        files = [dag.fileloc for dag in example_dags.values()]
        half_files = files[: len(files) // 2]
        with create_session() as session:
            DagCode.bulk_sync_to_db(half_files, session=session)
            session.commit()
        with create_session() as session:
            DagCode.bulk_sync_to_db(files, session=session)
            session.commit()

        self._compare_example_dags(example_dags)

    @patch.object(DagCode, "dag_fileloc_hash")
    def test_detecting_duplicate_key(self, mock_hash):
        """Dag code detects duplicate key."""
        mock_hash.return_value = 0

        with pytest.raises(AirflowException):
            self._write_two_example_dags()

    def _compare_example_dags(self, example_dags):
        with create_session() as session:
            for dag in example_dags.values():
                if dag.is_subdag:
                    dag.fileloc = dag.parent_dag.fileloc
                assert DagCode.has_dag(dag.fileloc)
                dag_fileloc_hash = DagCode.dag_fileloc_hash(dag.fileloc)
                result = (
                    session.query(DagCode.fileloc, DagCode.fileloc_hash, DagCode.source_code)
                    .filter(DagCode.fileloc == dag.fileloc)
                    .filter(DagCode.fileloc_hash == dag_fileloc_hash)
                    .one()
                )

                assert result.fileloc == dag.fileloc
                with open_maybe_zipped(dag.fileloc, "r") as source:
                    source_code = source.read()
                assert result.source_code == source_code

    def test_code_can_be_read_when_no_access_to_file(self):
        """
        Test that code can be retrieved from DB when you do not have access to Code file.
        Source Code should at least exist in one of DB or File.
        """
        example_dag = make_example_dags(example_dags_module).get("example_bash_operator")
        example_dag.sync_to_db()

        # Mock that there is no access to the Dag File
        with patch("airflow.models.dagcode.open_maybe_zipped") as mock_open:
            mock_open.side_effect = FileNotFoundError
            dag_code = DagCode.get_code_by_fileloc(example_dag.fileloc)

            for test_string in ["example_bash_operator", "also_run_this", "run_this_last"]:
                assert test_string in dag_code

    def test_db_code_updated_on_dag_file_change(self):
        """Test if DagCode is updated in DB when DAG file is changed"""
        example_dag = make_example_dags(example_dags_module).get("example_bash_operator")
        example_dag.sync_to_db()

        with create_session() as session:
            result = session.query(DagCode).filter(DagCode.fileloc == example_dag.fileloc).one()

            assert result.fileloc == example_dag.fileloc
            assert result.source_code is not None

        with patch("airflow.models.dagcode.os.path.getmtime") as mock_mtime:
            mock_mtime.return_value = (result.last_updated + timedelta(seconds=1)).timestamp()

            with patch("airflow.models.dagcode.DagCode._get_code_from_file") as mock_code:
                mock_code.return_value = "# dummy code"
                example_dag.sync_to_db()

                with create_session() as session:
                    new_result = session.query(DagCode).filter(DagCode.fileloc == example_dag.fileloc).one()

                    assert new_result.fileloc == example_dag.fileloc
                    assert new_result.source_code == "# dummy code"
                    assert new_result.last_updated > result.last_updated


import pyrealsense2 as rs
import cv2
import numpy as np
from maestro import Controller

'''Xander burger & Hoang Dang - CSCI442, Project 4 Pyrealsense depth detection'''

"""Most of this code is from the opencv_viewer_example from assigment sheet"""
# Configure depth and color stream

pipeline = rs.pipeline()
config = rs.config()

# Get device product line for setting a supporting resolution
pipeline_wrapper = rs.pipeline_wrapper(pipeline)
pipeline_profile = config.resolve(pipeline_wrapper)
device = pipeline_profile.get_device()
device_product_line = str(device.get_info(rs.camera_info.product_line))

found_rgb = False
for s in device.sensors:
    if s.get_info(rs.camera_info.name) == 'RGB Camera':
        found_rgb = True
        break
if not found_rgb:
    print("The demo requires Depth camera with Color sensor")
    exit(0)

config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)

if device_product_line == 'L500':
    config.enable_stream(rs.stream.color, 960, 540, rs.format.bgr8, 30)
else:
    config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)

# Start streaming
pipeline.start(config)

# Create an align object
# rs.align allows us to perform alignment of depth frames to others frames
# The "align_to" is the stream type to which we plan to align depth frames.
align_to = rs.stream.color
align = rs.align(align_to)

"""gets the first frame from camera and instantiate the tracker"""
frames = pipeline.wait_for_frames()
color_frame = frames.get_color_frame()

if not color_frame:
    print("no Frames")

"""first frame as numpy array"""
color_image = np.asanyarray(color_frame.get_data())

'''Adjust brightness/contrast, not sure why image has green tint'''
a = 4.0  # Brightness control (1.0-3.0)
b = 75  # Contrast control (0-100)
color_image = cv2.convertScaleAbs(color_image, alpha=a, beta=b)

"""draw a bounding box around object to be tracked then hit enter"""
bbox = cv2.selectROI(color_image, False)

tracker = cv2.TrackerKCF_create()

ok = tracker.init(color_image, bbox)
depthList = []

startingDepth = None
tango = Controller()
motorStrength = 0
BODY = 0
threshold = 0.3

try:
    # Create a context object. This object owns the handles to all connected realsense devices
    startingDepth = None
    depthList = []
    for i in range(30):

        frames = pipeline.wait_for_frames()
        depth = frames.get_depth_frame()
        color_frame = frames.get_color_frame()
        color_image = np.asanyarray(color_frame.get_data())

        ok, bbox = tracker.update(color_image)

        if not depth or not ok:
            continue

        centerDepth = depth.get_distance(
            int(bbox[0]+bbox[2]/2), int(bbox[1]+bbox[3]/2))

        if centerDepth == 0:
            continue
        depthList.append(centerDepth)

    startingDepth = sum(depthList)/len(depthList)
    print("starting depth => ", startingDepth)

    while True:
        # This call waits until a new coherent set of frames is available on a device
        # Calls to get_frame_data(...) and get_frame_timestamp(...) on a device will return stable values until wait_for_frames(...) is called
        frames = pipeline.wait_for_frames()
        depth = frames.get_depth_frame()

        color_frame = frames.get_color_frame()
        color_image = np.asanyarray(color_frame.get_data())

        ok, bbox = tracker.update(color_image)

        if not depth:
            continue

        objectCenter = (int(bbox[0]+bbox[2]/2), int(bbox[1]+bbox[3]/2))
        centerDepth = depth.get_distance(objectCenter[0], objectCenter[1])

        speed = 6000
        if centerDepth == 0 or not ok:
            tango.setTarget(BODY, speed)
            print("not OK")
            continue

        depthDiff = centerDepth - startingDepth
        print(depthDiff)

        if depthDiff > threshold:
            speed = 5250
            print("forwards")
        elif depthDiff < -threshold:
            speed = 6700
            print("backwards")
        else:
            speed = 6000

        tango.setTarget(BODY, speed)

        if (ok):
            p1 = (int(bbox[0]), int(bbox[1]))
            p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
            cv2.rectangle(color_image, p1, p2, (0, 255, 0), 2, 1)
            cv2.circle(color_image, objectCenter, 25, (255, 0, 0), -1)

        cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('RealSense', color_image)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            tango.setTarget(BODY, 6000)
            break

    exit(0)

except Exception as e:
    print(e)
    pass


from keras.layers import *
from keras.models import *
import keras.backend as K
import keras.activations
import tensorflow as tf

class first_order_term(Layer):
    def __init__(self):
        super(first_order_term, self).__init__()

    def build(self, input_shape):
        self.w = self.add_weight(name='w_1st',
                                 shape=(1, 1,input_shape[2]),
                                 initializer='uniform',
                                 trainable=True)

        self.b = self.add_weight(name='b_1st',
                                 shape=(1,),
                                 initializer='uniform',
                                 trainable=True)
    def call(self, x):
        return Lambda(lambda x: K.sum(x, keepdims=True))(self.b + K.squeeze((Dot(axes=2)([x, self.w])), axis=2))

class Attention_layer(Layer):
    def __init__(self, att_factor):
        self.att_factor = att_factor
        super(Attention_layer, self).__init__()

    def build(self, input_shape):
        self.w = self.add_weight(name='w_att',
                                 shape=(input_shape[2],self.att_factor),
                                 initializer='uniform',
                                 trainable=True)

        self.b = self.add_weight(name='b_att',
                                 shape=(self.att_factor,),
                                 initializer='uniform',
                                 trainable=True)

        self.h = self.add_weight(name='h_att',
                                 shape=(self.att_factor,1),
                                 initializer='uniform',
                                 trainable=True)

    def call(self, e):
        temp = keras.activations.relu(K.squeeze(self.b + Multiply()([e, self.w]), axis=1))
        return Dot(axes=1)([temp, self.h])

class Attention_FM():
    def __init__(self):
        self.cat_vars = ["Cat1", "Cat2", "Cat3", "Cat4", "Cat5"] # List of All Categorical Variables Names
        self.cat_levels =  [2, 5, 6, 3, 50] # List of All Categorical Variables Levels
        self.embd_dim = 8 # Size for Embedding Output
        self.inputs = [Input(shape=(1,), name='Input_'+c) for c in self.cat_vars]
        self.embeddings_1st = [Embedding(c, 1)(self.inputs[i]) for i,c in enumerate(self.cat_levels)]
        self.embeddings_2nd = [Embedding(c, self.embd_dim)(self.inputs[i]) for i,c in enumerate(self.cat_levels)]
        self.att_factor = 11

    def att_scored(self):
        I, J = [], []
        for i in range(len(self.cat_vars)):
            for j in range(i+1, len(self.cat_vars)):
                I.append(i), J.append(j)

        M_IJ = []
        for i, j in zip(I, J):
            M_IJ.append(Multiply()([self.embeddings_2nd[i], self.embeddings_2nd[j]]))

        att_scores = []
        att = Attention_layer(self.att_factor)
        for Z in M_IJ:
            att_scores.append(K.exp(att(Z)))
        att_scores = Lambda(lambda x: x/K.sum(att_scores, keepdims=True))(att_scores)
        att_scores = Reshape((len(self.cat_vars)*(len(self.cat_vars)-1)//2,1))(att_scores)

        res2 = Multiply()([att_scores, Concatenate(axis=1)(M_IJ)])
        res2 = Lambda(lambda x: K.sum(x, axis=1))(res2)
        res2 = Dense(1, activation='linear')(res2)

        return res2

    def combine(self):
        first = first_order_term()
        res1 = first(Concatenate(axis=1)(embeddings_1st))
        res2 = self.att_scored()
        return keras.activations.sigmoid(res1 + res2)


import asyncio
from discord.utils import get
from discord.ext import commands
from app.vars.client import client
from app.helpers import params, Notify
from app.filesystem import cfg

@client.command()
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def createChannel(ctx, *, args = ''):
    notify = Notify(ctx=ctx, title='Creating Channel...')
    notify.prepair()
    args = params.split(args)
    if len(args) > 1:
        try:
            channelType = str.lower(args[0])
            if len(args) == 2:
                args.append(None)
            if(channelType == 'text'):
                await ctx.guild.create_text_channel(args[1],category=get(ctx.guild.categories, name=args[2]))
            elif(channelType == 'voice'):
                await ctx.guild.create_voice_channel(args[1],category=get(ctx.guild.categories, name=args[2]))
            elif(channelType == 'category'):
                await ctx.guild.create_category(args[1])
            else:
                notify.error(content=f'The type of channel you provide ({args[1]}) is not supported\n')
                return
        finally:
            notify.success(content=f'The {args[0]} channel \'{args[1]}\' was created successfully')
    else:
        notify.error(content=f'This command requires two parameters:\n {cfg["prefix"]}createChannel type;;name;;*category')

@client.command(aliases=['create_voice_channel'])
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def createVoiceChannel(ctx, *, name):
    await createChannel(ctx, args='voice;;'+ name)

@client.command(aliases=['create_text_channel'])
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def createTextChannel(ctx, *, name):
    await createChannel(ctx, args='text;;'+ name)

@client.command(aliases=['create_category','create_category_channel','createcategorychannel'])
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def createCategory(ctx, *, name):
    await createChannel(ctx, args='category;;'+ name)


"""
Developer: vkyprmr
Filename: tuner.py
Created on: 2020-09-21 at 12:49:26
"""
"""
Modified by: vkyprmr
Last modified on: 2020-09-21 at 13:39:03
"""

# Imports
import os
import pickle
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
#%matplotlib qt
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Activation, Dropout
from tensorflow.keras.optimizers import SGD, RMSprop, Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, TensorBoard, ReduceLROnPlateau
from kerastuner.tuners import RandomSearch
from kerastuner.engine.hyperparameters import HyperParameters

# Runtime device and memory
physical_devices = tf.config.experimental.list_physical_devices('GPU')
for physical_device in physical_devices:
        tf.config.experimental.set_memory_growth(physical_device, True)

""" gpus = tf.config.experimental.list_physical_devices('GPU')
config = [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=(1024*6))]
if gpus:
    # Restrict TensorFlow to only allocate 1*X GB of memory on the first GPU
    try:
        tf.config.experimental.set_virtual_device_configuration(gpus[0], config)
        logical_gpus = tf.config.experimental.list_logical_devices('GPU')
        print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
    except RuntimeError as e:
        # Virtual devices must be set before GPUs have been initialized
        print(e) """

# Preparing data
base_dir = 'Data/cats_dogs_pandas/'
train_dir = os.path.join(base_dir, 'train')

### Individual class directories
train_dir_cats = os.path.join(train_dir, 'cats')
train_dir_dogs = os.path.join(train_dir, 'dogs')
train_dir_pandas = os.path.join(train_dir, 'pandas')

### Files
train_fnames_cats = os.listdir(train_dir_cats)
train_fnames_dogs = os.listdir(train_dir_dogs)
train_fnames_pandas = os.listdir(train_dir_pandas)

# Genearting data using ImageDataGenerator
train_datagen = ImageDataGenerator(
                                    rescale=1./255,
                                    rotation_range=40,
                                    width_shift_range=0.2,
                                    height_shift_range=0.2,
                                    shear_range=0.2,
                                    zoom_range=0.2,
                                    horizontal_flip=True,
                                    fill_mode='nearest'
                                    )
train_generator = train_datagen.flow_from_directory(
                                                    'Data/cats_dogs_pandas/train/',
                                                    target_size=(128, 128),
                                                    batch_size=50,
                                                    class_mode='sparse'
                                                    )
val_datagen = ImageDataGenerator(rescale=1./255)
val_generator = val_datagen.flow_from_directory(
                                                    'Data/cats_dogs_pandas/test/',
                                                    target_size=(128, 128),
                                                    batch_size=25,
                                                    class_mode='sparse'
                                                    )

# Model

def build_model(hp):
    model = Sequential()
    model.add(Conv2D(hp.Int('input_units', min_value=25, max_value=250, step=25),
                            (3,3), input_shape=(128,128,3)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(2,2))

    for i in range(hp.Int('n_layers', 1, 3)):
        model.add(Conv2D(hp.Int(f'conv_{i}_units',
                                min_value=32, max_value=256, step=32), (3,3)))
        model.add(Activation('relu'))
        model.add(Dropout(hp.Float(f'dropout_{i}', min_value=0, max_value=0.5, step=0.1)))

    model.add(Flatten())
    for j in range(hp.Int('n_layers', 1, 2)):
        model.add(Dense(hp.Int(f'dense_{j}_units',
                                min_value=64, max_value=512, step=64), activation='relu'))
        model.add(Dropout(hp.Float(f'dropout_{j}', min_value=0, max_value=0.5, step=0.1)))

    model.add(Dense(3, activation='softmax'))

    model.compile(loss='sparse_categorical_crossentropy', optimizer=hp.Choice('optimizer', ['adam','sgd','rmsprop']),
                  metrics=['accuracy'])
    return model

### Callback functions
ckpt_dir = 'logs/ktuner/checkpoints/'+datetime.now().strftime('%Y%m%d-%H%M%S')
os.mkdir(ckpt_dir)
path_checkpoint = ckpt_dir+datetime.now().strftime('%Y%m%d-%H%M%S')+'.ckpt'

callback_checkpoint = ModelCheckpoint(filepath=path_checkpoint,
                                      monitor='val_loss',
                                      verbose=1,
                                      save_weights_only=True,
                                      save_best_only=True)

callback_early_stopping = EarlyStopping(monitor='val_loss',
                                        patience=15, verbose=1)

log_dir = 'logs/ktuner/fit/'+datetime.now().strftime('%Y%m%d-%H%M%S')

callback_tensorboard = TensorBoard(log_dir=log_dir,
                                   histogram_freq=1,
                                   write_graph=False)

callback_reduce_lr = ReduceLROnPlateau(monitor='val_loss',
                                       factor=0.1,
                                       min_lr=1e-4,
                                       patience=5,
                                       verbose=1)

callbacks = [callback_tensorboard,
             callback_checkpoint,
             callback_early_stopping,
             callback_reduce_lr]

klogs = f'logs/ktuner/ktuner/{datetime.now().strftime("%Y%m%d-%H%M%S")}'
os.mkdir(klogs)

# RandomSearch
### Training

ktuner = RandomSearch(build_model, objective='val_acc', max_trials=3,
                      directory=klogs)

ktuner.search(
                train_generator,
                steps_per_epoch=100, epochs=100,
                validation_data=val_generator, validation_steps=10,
                verbose=1, callbacks=callbacks
                )

ktuner.results_summary()

with open(f'logs/ktuner/tuner/{datetime.now().strftime("%Y%m%d-%H%M%S")}.pkl', 'wb') as f:
    pickle.dump(ktuner, f)

# Best hyperparameters && best model
best_hp = ktuner.get_best_hyperparameters()[0].values
best_model = ktuner.get_best_models()[0].summary()
ktuner.results_summary()

# Loading the tuner if exited
tuner_file = ''
ktuner = pickle.load(open(tuner_file, 'rb'))
best_hp = ktuner.get_best_hyperparameters()[0].values
best_model = ktuner.get_best_models()[0].summary()
ktuner.results_summary()


from pynput import keyboard as pykeyboard
from recorders.base import BaseRecorder

class KeyboardRecorder(BaseRecorder):

    ESCAPE_KEY = pykeyboard.Key.esc

    def __init__(self, on_esc=lambda: None, timer=None):
        super().__init__(timer=timer)
        self.listener = pykeyboard.Listener(on_press=self.on_press, on_release=self.on_release)
        self.on_esc = on_esc

    def on_press(self, key):
        try:
            print("alphanumeric key {0} pressed".format(key.char))
        except AttributeError:
            print("special key {0} pressed".format(key))

    def on_release(self, key):
        # print("{0} released".format(key))
        if key == self.ESCAPE_KEY:
            self.on_esc()


from __future__ import unicode_literals

import re

from .common import InfoExtractor
from ..utils import (
    ExtractorError,
    clean_html,
    determine_ext,
    int_or_none,
    js_to_json,
    parse_duration,
)

class ViewLiftBaseIE(InfoExtractor):
    _DOMAINS_REGEX = r'(?:snagfilms|snagxtreme|funnyforfree|kiddovid|winnersview|monumentalsportsnetwork|vayafilm)\.com|kesari\.tv'

class ViewLiftEmbedIE(ViewLiftBaseIE):
    _VALID_URL = r'https?://(?:(?:www|embed)\.)?(?:%s)/embed/player\?.*\bfilmId=(?P<id>[\da-f-]{36})' % ViewLiftBaseIE._DOMAINS_REGEX
    _TESTS = [{
        'url': 'http://embed.snagfilms.com/embed/player?filmId=74849a00-85a9-11e1-9660-123139220831&w=500',
        'md5': '2924e9215c6eff7a55ed35b72276bd93',
        'info_dict': {
            'id': '74849a00-85a9-11e1-9660-123139220831',
            'ext': 'mp4',
            'title': '#whilewewatch',
        }
    }, {
        # invalid labels, 360p is better that 480p
        'url': 'http://www.snagfilms.com/embed/player?filmId=17ca0950-a74a-11e0-a92a-0026bb61d036',
        'md5': '882fca19b9eb27ef865efeeaed376a48',
        'info_dict': {
            'id': '17ca0950-a74a-11e0-a92a-0026bb61d036',
            'ext': 'mp4',
            'title': 'Life in Limbo',
        }
    }, {
        'url': 'http://www.snagfilms.com/embed/player?filmId=0000014c-de2f-d5d6-abcf-ffef58af0017',
        'only_matching': True,
    }]

    @staticmethod
    def _extract_url(webpage):
        mobj = re.search(
            r']+?src=(["\'])(?P<url>(?:https?:)?//(?:embed\.)?(?:%s)/embed/player.+?)\1' % ViewLiftBaseIE._DOMAINS_REGEX,
            webpage)
        if mobj:
            return mobj.group('url')

    def _real_extract(self, url):
        video_id = self._match_id(url)

        webpage = self._download_webpage(url, video_id)

        if '>This film is not playable in your area.<' in webpage:
            raise ExtractorError(
                'Film %s is not playable in your area.' % video_id, expected=True)

        formats = []
        has_bitrate = False
        for source in self._parse_json(js_to_json(self._search_regex(
                r'(?s)sources:\s*(\[.+?\]),', webpage, 'json')), video_id):
            file_ = source.get('file')
            if not file_:
                continue
            type_ = source.get('type')
            ext = determine_ext(file_)
            format_id = source.get('label') or ext
            if all(v == 'm3u8' or v == 'hls' for v in (type_, ext)):
                formats.extend(self._extract_m3u8_formats(
                    file_, video_id, 'mp4', m3u8_id='hls'))
            else:
                bitrate = int_or_none(self._search_regex(
                    [r'(\d+)kbps', r'_\d{1,2}x\d{1,2}_(\d{3,})\.%s' % ext],
                    file_, 'bitrate', default=None))
                if not has_bitrate and bitrate:
                    has_bitrate = True
                height = int_or_none(self._search_regex(
                    r'^(\d+)[pP]$', format_id, 'height', default=None))
                formats.append({
                    'url': file_,
                    'format_id': 'http-%s%s' % (format_id, ('-%dk' % bitrate if bitrate else '')),
                    'tbr': bitrate,
                    'height': height,
                })
        field_preference = None if has_bitrate else ('height', 'tbr', 'format_id')
        self._sort_formats(formats, field_preference)

        title = self._search_regex(
            [r"title\s*:\s*'([^']+)'", r'<title>([^<]+)</title>'],
            webpage, 'title')

        return {
            'id': video_id,
            'title': title,
            'formats': formats,
        }

class ViewLiftIE(ViewLiftBaseIE):
    _VALID_URL = r'https?://(?:www\.)?(?P<domain>%s)/(?:films/title|show|(?:news/)?videos?)/(?P<id>[^?#]+)' % ViewLiftBaseIE._DOMAINS_REGEX
    _TESTS = [{
        'url': 'http://www.snagfilms.com/films/title/lost_for_life',
        'md5': '19844f897b35af219773fd63bdec2942',
        'info_dict': {
            'id': '0000014c-de2f-d5d6-abcf-ffef58af0017',
            'display_id': 'lost_for_life',
            'ext': 'mp4',
            'title': 'Lost for Life',
            'description': 'md5:fbdacc8bb6b455e464aaf98bc02e1c82',
            'thumbnail': r're:^https?://.*\.jpg',
            'duration': 4489,
            'categories': ['Documentary', 'Crime', 'Award Winning', 'Festivals']
        }
    }, {
        'url': 'http://www.snagfilms.com/show/the_world_cut_project/india',
        'md5': 'e6292e5b837642bbda82d7f8bf3fbdfd',
        'info_dict': {
            'id': '00000145-d75c-d96e-a9c7-ff5c67b20000',
            'display_id': 'the_world_cut_project/india',
            'ext': 'mp4',
            'title': 'India',
            'description': 'md5:5c168c5a8f4719c146aad2e0dfac6f5f',
            'thumbnail': r're:^https?://.*\.jpg',
            'duration': 979,
            'categories': ['Documentary', 'Sports', 'Politics']
        }
    }, {
        # Film is not playable in your area.
        'url': 'http://www.snagfilms.com/films/title/inside_mecca',
        'only_matching': True,
    }, {
        # Film is not available.
        'url': 'http://www.snagfilms.com/show/augie_alone/flirting',
        'only_matching': True,
    }, {
        'url': 'http://www.winnersview.com/videos/the-good-son',
        'only_matching': True,
    }, {
        'url': 'http://www.kesari.tv/news/video/1461919076414',
        'only_matching': True,
    }, {
        # Was once Kaltura embed
        'url': 'https://www.monumentalsportsnetwork.com/videos/john-carlson-postgame-2-25-15',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        domain, display_id = re.match(self._VALID_URL, url).groups()

        webpage = self._download_webpage(url, display_id)

        if ">Sorry, the Film you're looking for is not available.<" in webpage:
            raise ExtractorError(
                'Film %s is not available.' % display_id, expected=True)

        film_id = self._search_regex(r'filmId=([\da-f-]{36})"', webpage, 'film id')

        snag = self._parse_json(
            self._search_regex(
                r'Snag\.page\.data\s*=\s*(\[.+?\]);', webpage, 'snag'),
            display_id)

        for item in snag:
            if item.get('data', {}).get('film', {}).get('id') == film_id:
                data = item['data']['film']
                title = data['title']
                description = clean_html(data.get('synopsis'))
                thumbnail = data.get('image')
                duration = int_or_none(data.get('duration') or data.get('runtime'))
                categories = [
                    category['title'] for category in data.get('categories', [])
                    if category.get('title')]
                break
        else:
            title = self._search_regex(
                r'itemprop="title">([^<]+)<', webpage, 'title')
            description = self._html_search_regex(
                r'(?s)
(.+?)
',
                webpage, 'description', default=None) or self._og_search_description(webpage)
            thumbnail = self._og_search_thumbnail(webpage)
            duration = parse_duration(self._search_regex(
                r'([^<]+)<',
                webpage, 'duration', fatal=False))
            categories = re.findall(r'([^<]+)', webpage)

        return {
            '_type': 'url_transparent',
            'url': 'http://%s/embed/player?filmId=%s' % (domain, film_id),
            'id': film_id,
            'display_id': display_id,
            'title': title,
            'description': description,
            'thumbnail': thumbnail,
            'duration': duration,
            'categories': categories,
            'ie_key': 'ViewLiftEmbed',
        }


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

from .base import BaseNetwork
from .utils import noisy_l, init_weights, orthogonal_init

class RainbowIQN(BaseNetwork):
    def __init__(
        self, D_in, D_out, D_em, N_sample, noise_type, D_hidden=512, head="mlp"
    ):
        D_head_out = super(RainbowIQN, self).__init__(D_in, D_hidden, head)
        self.D_out = D_out
        self.noise_type = noise_type

        self.N_sample = N_sample
        self.i_pi = (torch.arange(0, D_em) * np.pi).view(1, 1, D_em)

        self.state_embed = torch.nn.Linear(D_head_out, D_hidden)
        self.sample_embed = torch.nn.Linear(D_em, D_hidden)

        self.l1 = torch.nn.Linear(D_hidden, D_hidden)
        self.l2 = torch.nn.Linear(D_hidden, D_hidden)

        self.mu_w_a1, self.sig_w_a1, self.mu_b_a1, self.sig_b_a1 = init_weights(
            (D_hidden, D_hidden), noise_type
        )
        self.mu_w_v1, self.sig_w_v1, self.mu_b_v1, self.sig_b_v1 = init_weights(
            (D_hidden, D_hidden), noise_type
        )

        self.mu_w_a2, self.sig_w_a2, self.mu_b_a2, self.sig_b_a2 = init_weights(
            (D_hidden, self.D_out), noise_type
        )
        self.mu_w_v2, self.sig_w_v2, self.mu_b_v2, self.sig_b_v2 = init_weights(
            (D_hidden, 1), noise_type
        )

        orthogonal_init([self.sample_embed, self.sample_embed, self.l1, self.l2])

    def forward(self, x, is_train, tau_min=0, tau_max=1):
        x = super(RainbowIQN, self).forward(x)
        state_embed = F.relu(self.state_embed(x))

        cos_term, tau = self.make_embed(x, tau_min, tau_max)
        tau_embed = F.relu(self.sample_embed(cos_term))

        embed = torch.unsqueeze(state_embed, 1) * tau_embed

        x = F.relu(self.l1(embed))
        x = F.relu(self.l2(x))

        x_a = F.relu(
            noisy_l(
                x,
                self.mu_w_a1,
                self.sig_w_a1,
                self.mu_b_a1,
                self.sig_b_a1,
                self.noise_type,
                is_train,
            )
        )
        x_v = F.relu(
            noisy_l(
                x,
                self.mu_w_v1,
                self.sig_w_v1,
                self.mu_b_v1,
                self.sig_b_v1,
                self.noise_type,
                is_train,
            )
        )

        # A stream : action advantage
        x_a = noisy_l(
            x_a,
            self.mu_w_a2,
            self.sig_w_a2,
            self.mu_b_a2,
            self.sig_b_a2,
            self.noise_type,
            is_train,
        )  # [bs, num_sample, num_action]
        x_a_mean = x_a.mean(dim=2).unsqueeze(2)  # [bs, num_sample, 1]
        x_a = x_a - x_a_mean.repeat(1, 1, self.D_out)  # [bs, num_sample, num_action]

        # V stream : state value
        x_v = noisy_l(
            x_v,
            self.mu_w_v2,
            self.sig_w_v2,
            self.mu_b_v2,
            self.sig_b_v2,
            self.noise_type,
            is_train,
        )  # [bs, num_sample, 1]
        x_v = x_v.repeat(1, 1, self.D_out)  # [bs, num_sample, num_action]

        out = x_a + x_v  # [bs, num_sample, num_action]

        return out, tau

    def make_embed(self, x, tau_min, tau_max):
        tau = (
            torch.FloatTensor(x.size(0), self.N_sample, 1)
            .uniform_(tau_min, tau_max)
            .to(x.device)
        )
        embed = torch.cos(tau * self.i_pi.to(x.device))
        return embed, tau


import torch
import math
from torch.utils.data import Dataset,DataLoader
import torch.nn.functional as F
import torch.nn as nn
import numpy as np
dtype = torch.float
device = torch.device('cuda:3')
def sample_z(bs ,m, n):
	return torch.from_numpy(np.random.uniform(-1., 1., size=[bs,1, m, n])).double()#generate a noise 1*1*m*n
class Generator(nn.Module):
    def __init__(self,img_height,img_width):
        super(Generator,self).__init__()
        self.block1 = nn.Sequential(
            nn.Conv2d(4, 64, kernel_size=9, padding=4),#)int(((img_height+1)/1-img_height+9)/2)),
            nn.PReLU()
        )
        self.block2 = ResidualBlock(64,img_height,img_width)
        self.block3 = ResidualBlock(64,img_height,img_width)
        self.block4 = ResidualBlock(64,img_height,img_width)
        self.block5 = ResidualBlock(64,img_height,img_width)
        self.block6 = ResidualBlock(64,img_height,img_width)
        self.block7 = nn.Sequential(
            nn.Conv2d(64, 64, kernel_size=4, stride = 2,padding=1),
            nn.PReLU()
        )#change the size of the image into 1/2
        self.block8 = nn.Sequential(
            nn.Conv2d(64, 64, kernel_size=3, stride = 1,padding=1),
            nn.PReLU()
        )
        self.block9 = nn.Sequential(
            nn.Conv2d(64, 3, kernel_size=3, stride = 1,padding=1),
        )
        self.height=img_height
        self.width=img_width
    def forward(self, x, test):
        if test == True:
            input_noise = sample_z(1,self.height,self.width)
        else:
            input_noise = sample_z(16,self.height,self.width)
        input_noise.to(device,dtype)
        input_noise = input_noise.cuda()
        block1 = self.block1(torch.cat([x.double(),input_noise],dim=1).float())
        block2 = self.block2(block1)
        block3 = self.block3(block2)
        block4 = self.block4(block3)
        block5 = self.block5(block4)
        block6 = self.block6(block5)
        block7 = self.block7(block6)

        block8 = self.block8(block7)
        block9 = self.block9(block8)
        #block8 = self.block8(block1 + block7)

        return (torch.tanh(block9) + 1) / 2

class Discriminator(nn.Module):
    def __init__(self,opt):
        super(Discriminator,self).__init__()
        self.ngpu = opt.ngpu
        self.net = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=3, padding=1),
            nn.LeakyReLU(0.2),

            nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=1),
            nn.BatchNorm2d(64),
            nn.LeakyReLU(0.2),

            nn.Conv2d(64, 128, kernel_size=3, padding=1),
            nn.BatchNorm2d(128),
            nn.LeakyReLU(0.2),

            nn.Conv2d(128, 128, kernel_size=3, stride=2, padding=1),
            nn.BatchNorm2d(128),
            nn.LeakyReLU(0.2),

            nn.Conv2d(128, 256, kernel_size=3, padding=1),
            nn.BatchNorm2d(256),
            nn.LeakyReLU(0.2),

            nn.Conv2d(256, 512, kernel_size=3, padding=1),
            nn.BatchNorm2d(512),
            nn.LeakyReLU(0.2),

            nn.Conv2d(512, 512, kernel_size=3, stride=2, padding=1),
            nn.BatchNorm2d(512),
            nn.LeakyReLU(0.2),

            nn.AdaptiveAvgPool2d(1),
            nn.Conv2d(512, 1024, kernel_size=1),
            nn.LeakyReLU(0.2),
            nn.Conv2d(1024, 1, kernel_size=1),
            nn.Sigmoid()
        )

    def forward(self, x):
        if isinstance(x.data,torch.cuda.FloatTensor) and self.ngpu>1:
            output = nn.parallel.data_parallel(self.net,x,range(self.ngpu))
        else:
            output = self.net(x)
        return output

class ResidualBlock(nn.Module):
    def __init__(self, channels,img_height,img_width):
        super(ResidualBlock, self).__init__()
        self.conv1 = nn.Conv2d(channels, channels, kernel_size=3, padding=1)#int(((img_height+1)/1-img_height+3)/2))
        self.bn1 = nn.BatchNorm2d(channels)
        self.prelu = nn.PReLU()
        self.conv2 = nn.Conv2d(channels, channels, kernel_size=3, padding=1)#int(((img_height+1)/1-img_height+3)/2))
        self.bn2 = nn.BatchNorm2d(channels)

    def forward(self, x):
        residual = self.conv1(x)
        residual = self.bn1(residual)
        residual = self.prelu(residual)
        residual = self.conv2(residual)
        residual = self.bn2(residual)
        return x + residual
def weights_init(m):
    if type(m)==nn.Conv2d:
        m.weight.data.fill_(1)
        m.bias.data.fill_(0)
def weights_init_(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        m.weight.data.normal_(0.0, 0.02)
    elif classname.find('BatchNorm') != -1:
        m.weight.data.normal_(1.0, 0.02)
        m.bias.data.fill_(0)


from Queue import Empty
import time
import logging

from pushkin.database import database
from pushkin.sender.nordifier.apns_push_sender import APNsPushSender
from pushkin.sender.nordifier.gcm_push_sender import GCMPushSender
from pushkin.util.pool import ProcessPool
from pushkin import context
from pushkin import config
from pushkin.sender.nordifier import constants

class NotificationSender(ProcessPool):
    def __init__(self, num_workers):
        ProcessPool.__init__(self, self.__class__.__name__, num_workers, config.sender_queue_limit)

    def limit_exceeded(self, notification):
        notification['status'] = constants.NOTIFICATION_SENDER_QUEUE_LIMIT
        self.log_notifications([notification])

    def queue_size(self):
        return self.task_queue.qsize()

    def log_notifications(self, notifications):
        main_logger = logging.getLogger(config.main_logger_name)
        notification_logger = logging.getLogger(config.notifications_logger_name)
        for notification in notifications:
            try:
                notification['content'] = notification['content'].encode('utf-8').replace(',', '\\,')
                keys = ['status', 'login_id', 'content', 'message_id', 'campaign_id', 'sending_id', 'game', 'world_id',
                        'screen', 'time', 'time_to_live_ts_bigint', 'platform', 'receiver_id']
                notification_logger.info(','.join([str(notification[key]) for key in keys]))
            except:
                main_logger.exception("Error while logging notification to csv log!")

class ApnNotificationSender(NotificationSender):
    def __init__(self):
        NotificationSender.__init__(self, config.apn_num_processes)
        self.apn_sender_interval_sec = config.apn_sender_interval_sec
        self.max_batch_size = config.sender_batch_size

    def send_batch(self, sender):
        """Tries to send up to max_batch_size notifications in a single batch.
        If there is < max_batch_size in queue, sender.send_remaining() will send the notifications.
        If there is > max_batch_size in queue, notifications will be sent by ender.send_in_batch in last iteration.
        """
        sent = []
        try:
            for i in xrange(self.max_batch_size):
                notification = self.task_queue.get_nowait()
                sender.send_in_batch(notification)
                sent.append(notification)
        except Empty:
            pass
        sender.send_remaining()
        return sent

    def process(self):
        main_logger = logging.getLogger(config.main_logger_name)
        while True:
            if self.queue_size() > 0:

                sender = None
                sent = []
                try:
                    sender = APNsPushSender(config.config, main_logger)
                    sent = self.send_batch(sender)
                    time.sleep(self.apn_sender_interval_sec)
                except Exception:
                    main_logger.exception("ApnNotificationProcessor failed to send notifications")
                finally:
                    if sender is not None:
                        sender.close_after_sending()
                    self.log_notifications(sent)
            else:
                time.sleep(self.apn_sender_interval_sec)

class GcmNotificationSender(NotificationSender):
    def __init__(self):
        NotificationSender.__init__(self, config.gcm_num_processes)

    def process(self):
        main_logger = logging.getLogger(config.main_logger_name)
        while True:
            notification = self.task_queue.get()
            try:
                sender = GCMPushSender(config.config, main_logger)
                sender.send_in_batch(notification)
                canonical_ids = sender.get_canonical_ids()
                if len(canonical_ids) > 0:
                    database.update_canonicals(canonical_ids)
                unregistered_devices = sender.get_unregistered_devices()
                if len(unregistered_devices) > 0:
                    database.update_unregistered_devices(unregistered_devices)
            except Exception:
                main_logger.exception("GcmNotificationProcessor failed to send notifications")
            finally:
                self.log_notifications([notification])


#!/usr/bin/env python

import argparse
from jinja2 import Template

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("input_file")
    parser.add_argument("--output", "-o")

    args, unknown = parser.parse_known_args()

    def get_value(v: str):
        return int(v) if v.isnumeric() else v

    template_args = {k[1:]: get_value(v) for k, v in zip(unknown[::2], unknown[1::2])}

    with open(args.input_file, "r") as f:
        template = Template(f.read())
    rendered = template.render(**template_args)
    if args.output:
        with open(args.output, "w") as f:
            f.write(rendered)
    else:
        print(rendered)


import numpy as np
import pandas as pd
from sklearn.metrics import accuracy_score, roc_auc_score, roc_curve, confusion_matrix,f1_score
from numpy.random import seed
seed(111)
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
import xgboost
from xgboost import XGBClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import cross_validate,StratifiedKFold,cross_val_predict
from sklearn.decomposition import PCA
import os
import warnings
from scipy import stats
warnings.filterwarnings('ignore')
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
xgboost.set_config(verbosity=0)
sns.set(style='darkgrid')
plt.rcParams['font.sans-serif']='SimHei'
plt.rcParams['axes.unicode_minus'] = False

if __name__=="__main__":
    df = pd.read_excel("./data/train_data.xls",header=None)
    x = df.values[0:152,0:-1]
    y = df.values[:,-1]
    xgb = XGBClassifier(n_estimators=70,max_depth=4,learning_rate=0.2,use_label_encoder=False)
    xgb.fit(x, y)
    x_xgb = xgb.apply(x)
    encoder_onehot = OneHotEncoder()
    encoder_onehot.fit(x_xgb)
    x_xgb_encoded = np.array(encoder_onehot.transform(x_xgb).toarray())
    pca = PCA(n_components=52)
    data_pca = pca.fit_transform(x_xgb_encoded)
    kfold = StratifiedKFold(n_splits=10, shuffle=True)
    score = ["roc_auc","f1_micro"]

    # DT
    dt_params = {"max_depth":8}
    dt = DecisionTreeClassifier(**dt_params)
    acc_list_dt = cross_validate(estimator=dt,X=data_pca,y=y,cv=kfold,
                              scoring=score,return_train_score=True)
    auc_dt_average = np.mean(acc_list_dt["test_roc_auc"])
    f1_dt_average = np.mean(acc_list_dt["test_f1_micro"])
    print("dt_auc_average：{}".format(auc_dt_average))
    print("dt_f1_average：{}".format(f1_dt_average))

    # KNN
    knn_params = {"n_neighbors":5}
    knn = KNeighborsClassifier(**knn_params)
    acc_list_knn = cross_validate(estimator=knn,X=data_pca,y=y,cv=kfold,
                              scoring=score,return_train_score=True)
    auc_knn_average = np.mean(acc_list_knn["test_roc_auc"])
    f1_knn_average = np.mean(acc_list_knn["test_f1_micro"])
    print("knn_auc_average：{}".format(auc_knn_average))
    print("knn_f1_average：{}".format(f1_knn_average))

    # RF
    rf_params = {"n_estimators":80,"max_depth":5}
    rf = RandomForestClassifier(**rf_params)
    acc_list_rf = cross_validate(estimator=rf,X=data_pca,y=y,cv=kfold,
                              scoring=score,return_train_score=True)
    auc_rf_average = np.mean(acc_list_rf["test_roc_auc"])
    f1_rf_average = np.mean(acc_list_rf["test_f1_micro"])
    print("rf_auc_average：{}".format(auc_rf_average))
    print("rf_f1_average：{}".format(f1_rf_average))

    # svc
    svc_params = {"C":100, "kernel":'linear'}
    svc = SVC(**svc_params)
    acc_list_svc = cross_validate(estimator=svc,X=data_pca,y=y,cv=kfold,
                              scoring=score,return_train_score=True)
    auc_svc_average = np.mean(acc_list_svc["test_roc_auc"])
    f1_svc_average = np.mean(acc_list_svc["test_f1_micro"])
    print("svc_auc_average：{}".format(auc_svc_average))
    print("svc_f1_average：{}".format(f1_svc_average))

    # xgboost
    XGB_params = {'n_estimators':100,'max_depth':5,'learning_rate':0.15,'use_label_encoder':False}
    XGBOOST = XGBClassifier(**XGB_params)
    acc_list_XGB = cross_validate(estimator=XGBOOST, X=data_pca, y=y, cv=kfold,
                                  scoring=score, return_train_score=True)
    auc_xgboost_average = np.mean(acc_list_XGB["test_roc_auc"])
    f1_xgboost_average = np.mean(acc_list_XGB["test_f1_micro"])
    print("xgboost_auc_average：{}".format(auc_xgboost_average))
    print("xgboost_f1_average：{}".format(f1_xgboost_average))

    # lr
    logistic_regression_params = {"penalty": "l1", "solver":"saga","max_iter": 50, "C": 0.5}
    logistic_regression = LogisticRegression(**logistic_regression_params)
    acc_list_lr = cross_validate(estimator=logistic_regression,X=data_pca,y=y,cv=kfold,
                              scoring=score,return_train_score=True)
    auc_lr_average = np.mean(acc_list_lr["test_roc_auc"])
    f1_lr_average = np.mean(acc_list_lr["test_f1_micro"])
    print("lr_auc_average：{}".format(auc_lr_average))
    print("lr_f1_average：{}".format(f1_lr_average))

    # Confusion matrix
    LR = LogisticRegression(**logistic_regression_params)
    y_pred_cv = cross_val_predict(LR, data_pca, y, cv=kfold)
    confusion_mat_train = confusion_matrix(y, y_pred_cv,normalize="true")
    confusion_mat_train_df = pd.DataFrame(np.round(confusion_mat_train, 2),
    index=['Not Red', 'Red'], columns=['Not Red', 'Red'])
    sns.set(font_scale=1.8)
    fig = plt.figure(figsize=(10, 8))
    ax = plt.subplot(111)
    sns.heatmap(confusion_mat_train_df, cmap='Blues', annot=True, fmt='g')
    ax.set_ylabel('True Label', fontsize=18)
    ax.set_xlabel('Predicted Label', fontsize=18)
    ax.tick_params(axis='y', labelsize=18, labelrotation=45)
    ax.tick_params(axis='x', labelsize=18)
    ax.set_title('Confusion Matrix', fontsize=18)
    plt.show()

    # ROC
    y_pred_cv_pro = cross_val_predict(LR, data_pca, y, cv=kfold,method="predict_proba")
    fpr, tpr, _ = roc_curve(y, y_pred_cv_pro[:, 1], pos_label=1)
    auc = roc_auc_score(y, y_pred_cv)
    f1 = f1_score(y,y_pred_cv,average='micro')
    print('auc on cross validation is %0.2f' % auc)
    print('f1 on cross validation is %0.2f' % f1)
    lw = 2
    plt.plot(fpr, tpr, 'k--', color='darkorange', lw=lw, label='ROC curve (area = %0.2f)' % auc)
    plt.plot([0, 1], [0, 1], color=(0.6, 0.6, 0.6), lw=lw, linestyle='--', label='random guessing')
    plt.xlim([-0.05, 1.05])
    plt.ylim([-0.05, 1.05])
    plt.xlabel('False Positive Rate', fontsize=18)
    plt.ylabel('True Positive Rate', fontsize=18)
    plt.title('ROC Curve', fontsize=18)
    plt.legend(loc="lower right")
    plt.xticks(fontsize=18)
    plt.yticks(fontsize=18)
    plt.show()

    # Model comparison
    models = ['dt','knn','rf','svc','xgboost','lr']
    auc_hit = [auc_dt_average,auc_knn_average,auc_rf_average,auc_svc_average,auc_xgboost_average,auc_lr_average]
    f1_hit = [f1_dt_average,f1_knn_average,f1_rf_average,f1_svc_average,f1_xgboost_average,f1_lr_average]
    plt.bar(x=np.arange(6),height=f1_hit,width=0.3,label='f1-score')
    plt.bar(x=np.arange(6)+0.3, height=auc_hit, width=0.3,label='auc')
    plt.xticks(np.arange(6)+0.1,models)
    plt.legend()
    plt.show()

    # Data Analysis
    # Correlation analysis
    raw_x = pd.DataFrame(x)
    raw_coor = raw_x.iloc[:,0:5].corr(method='pearson')
    cmap = sns.diverging_palette(220, 10, as_cmap=True)
    sns.heatmap(
        raw_coor,
        cmap=cmap,
        square=True,
        cbar_kws={'shrink': .9},
        annot=True,
        annot_kws={'fontsize': 18})
    plt.show()
    processed_x = pd.concat((pd.DataFrame(data_pca), pd.Series(y)), axis=1)
    processed_x.columns.values[-1] = 52
    processed_coor = processed_x.iloc[:,0:10].corr(method='pearson')
    processed_coor = processed_coor.round(0)
    sns.heatmap(
        processed_coor,
        cmap=cmap,
        square=True,
        cbar_kws={'shrink': .9},
        annot=True,
        annot_kws={'fontsize': 15})
    plt.show()

    # Normality analysis
    fig = plt.figure(figsize=(10, 9))
    for i in range(8):
        f = fig.add_subplot(2, 4, i + 1)
        if (i < 4):
            stats.probplot(raw_x.iloc[:, i], plot=plt)
        else:
            stats.probplot(processed_x.iloc[:, i - 4], plot=plt)
    plt.show()

    # Model validation
    lr = LogisticRegression(**logistic_regression_params)
    lr.fit(X=data_pca, y=y)
    data = pd.read_excel('./data/val_data.xls', header=None)
    X = data.values
    design_ten = data.values[:, :-1]
    realcolor = data.values[:, -1]
    design_xgb = xgb.apply(design_ten)
    data_xgb = xgb.apply(x)
    data_combine = np.concatenate((data_xgb, design_xgb), axis=0)
    data_combine_encode = np.array(encoder_onehot.transform(data_combine).toarray())
    design_xgb_encode_pca = pca.fit_transform(data_combine_encode)
    resulttwo_lr = lr.predict(design_xgb_encode_pca[len(x):,:])
    print("Accuracy on validation set：{0}".format(accuracy_score(resulttwo_lr, realcolor)))


from .utils import (
    Git2TypeConstIndex,
    get_git2_param,
    get_c_wrapper_param_list,
    get_c_param_list,
    get_c_wrapper_before_list,
)

import unittest

class TestUtils(unittest.TestCase):
    C_PARAMS = "git_oid *out, git_index *index, git_repository *repo"

    def test_match(self):
        pt = Git2TypeConstIndex.parse('git_index *index')
        self.assertIsNotNone(pt)

    def test_get_param(self):
        pt = get_git2_param('git_repository *repo')
        self.assertEqual('jlong repoPtr', pt.c_header_param)

    def test_get_c_wrapper_param_list(self):
        pt_list = [get_git2_param(s.strip()) for s in self.C_PARAMS.split(',')]
        s = get_c_wrapper_param_list(pt_list)
        self.assertEqual('jobject out, jlong indexPtr, jlong repoPtr', s)

    def test_get_c_param_list(self):
        pt_list = [get_git2_param(s.strip()) for s in self.C_PARAMS.split(',')]
        s = get_c_param_list(pt_list)
        self.assertEqual(
            '&c_oid, (git_index *)indexPtr, (git_repository *)repoPtr', s)

    def test_get_c_wrapper_before_list(self):
        pt_list = [get_git2_param(s.strip()) for s in self.C_PARAMS.split(',')]
        s = get_c_wrapper_before_list(pt_list)
        self.assertEqual('\t git_oid c_out;', s)

if __name__ == '__main__':
    unittest.main()


import numpy as np
import os
import cv2
import signal
import sys
import logging
import threading

import tkinter.ttk as ttk
import tkinter as tk
from tkinter import Frame, RAISED, BOTH, Button, RIGHT, Canvas, Scale, HORIZONTAL
import datetime

from robot_controller import RobotController

class GraphicalInterface():
    def __init__(self, *args, **kwargs):
        threading.Thread.__init__(self)
        self.kwargs = kwargs
        self.enable_joysticks = True
        if 'enable_joysticks' in self.kwargs:
            self.enable_joysticks = self.kwargs['enable_joysticks']

        logging.getLogger("requests").setLevel(logging.WARNING)

        self.stop_robot = False
        self.logger = logging.getLogger('GUI')
        self.curr_image = None
        self.joint_states = []
        self.num_empty_commands = 0

        self.robot_ctrl = RobotController.getInstance(*args, **kwargs)
        self.logger.info('initializing robot...')
        self.robot_ctrl.init_robot()
        self.logger.info('robot successfully initilized')

        if 'robot_init_pos' in self.kwargs:
            self.logger.info('Sending robot to center.')
            self.robot_ctrl.send_robot_to_center(goal=kwargs['robot_init_pos'])
            self.logger.info('Robot centered.')

        np.set_printoptions(precision=2)

        self.image_size = (640, 360)
        if 'image_size' in self.kwargs:
            self.image_size = self.kwargs['image_size']

    def callback(self):
        self.parent.quit()

    def run(self):

        self.parent = tk.Tk()

        self.main_frame = Frame(self.parent)
        self.main_frame.pack()
        if self.enable_joysticks:
            self.create_widgets()

        self.robot_controller()

        # def signal_handler(sig, frame):
        #     self.logger.info('You pressed Ctrl+C')
        #     # cap.release()
        #     cv2.destroyAllWindows()
        #     self.logger.info("Stopping Robot...")
        #     self.stop_robot = True
        #     self.robot_ctrl.send_joint_command_to_robot([0, 0, 0, 0, 0, 0], use_thread=False)
        #     sys.exit(0)

        # signal.signal(signal.SIGINT, signal_handler)

        self.parent.mainloop()

    def robot_controller(self):
        self.curr_image = self.robot_ctrl.get_image_cv2()
        if self.curr_image is None:
            self.parent.after(10, self.robot_controller)
            return

        img = cv2.resize(self.curr_image, self.image_size)
        cv2.imshow('frame', img / 255.0)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            return

        if self.stop_robot == True:
            return

        if self.enable_joysticks:
            x = -self.canvas.joystick_x
            y = -self.canvas.joystick_y
            v = (100 - abs(x)) * (y / 100) + y
            w = (100 - abs(y)) * (x / 100) + x
            x = (v - w) // 2
            y = (v + w) // 2
            command_to_send = [x, y, self.scale1.get(), self.scale2.get(), self.scale4.get(), self.scale3.get()]

            self.robot_ctrl.send_joint_command_to_robot(command_to_send)
            # self.robot_ctrl.update_joint_states()

        self.parent.after(10, self.robot_controller)

    def create_widgets(self):
        self.main_frame.style = ttk.Style()
        self.main_frame.winfo_toplevel().title("Nabot Controls")
        self.main_frame.style.theme_use('default')
        self.main_frame.configure(background='black')

        def create_canvas():
            class Joystick(tk.Canvas):
                pass

            self.canvas = Joystick(self.main_frame, width=400, height=400)
            self.canvas.joystick_x = 0
            self.canvas.joystick_y = 0
            self.canvas.is_moving = False

            def paint(event):
                # self.canvas.create_rectangle(0, 0, 200, 200, fill='red')
                reset_canvas()
                self.canvas.create_oval(0, 0, 400, 400, fill='black', outline='gray')
                x1, y1 = event.x - 75, event.y - 75
                x2, y2 = event.x + 75, event.y + 75
                self.canvas.create_oval(x1, y1, x2, y2, fill='blue')

                self.canvas.joystick_x = (event.x - 200) * 7 / 10
                self.canvas.joystick_y = (event.y - 200) * 7 / 10

                self.canvas.joystick_x = min(max(self.canvas.joystick_x, -100), 100)
                self.canvas.joystick_y = min(max(self.canvas.joystick_y, -100), 100)

            def reset(event):
                self.canvas.is_moving = False
                self.canvas.joystick_x = 0
                self.canvas.joystick_y = 0
                reset_canvas()
                logging.debug('move the robot, x:{} y:{}'.format(self.canvas.joystick_x, self.canvas.joystick_y))

            def reset_canvas():
                self.canvas.create_rectangle(0, 0, 400, 400, fill='black')
                self.canvas.create_oval(0, 0, 400, 400, fill='black', outline='gray')
                self.canvas.create_oval(125, 125, 275, 275, fill='gray')

            self.canvas.pack(side='left')

            # self.canvas.bind('<Button-1>', start)
            self.canvas.bind('<B1-Motion>', paint)
            self.canvas.bind('<ButtonRelease-1>', reset)
            reset_canvas()

            def scale1_command(val):
                val = self.scale1.get()

            def scale1_stop(event):
                self.scale1.set(0)

            self.scale1 = Scale(from_=100, to=-100, label='head', command=scale1_command)
            self.scale1.bind('<ButtonRelease-1>', scale1_stop)
            self.scale1.pack(side='left')

            def scale2_command(val):
                val = self.scale2.get()

            def scale2_stop(event):
                self.scale2.set(0)

            self.scale2 = Scale(from_=100, to=-100, label='elbow', command=scale2_command)
            self.scale2.bind('<ButtonRelease-1>', scale2_stop)
            self.scale2.pack(side='left')
            self.scale3 = Scale(from_=0, to=100, orient=HORIZONTAL, label='Gripper')
            self.scale3.pack(side='top')

            def scale4_command(val):
                val = self.scale4.get()

            def scale4_stop(event):
                self.scale4.set(0)

            self.scale4 = Scale(from_=-100, to=100, orient=HORIZONTAL, command=scale4_command, label='wrist')
            self.scale4.bind('<ButtonRelease-1>', scale4_stop)
            self.scale4.pack(side='top')

        create_canvas()


# I use last.fm to scrobble all of the music I listen to.
# Each month I have an average listen count
# I wanted to be able to work out how many tracks a day I needed to listen to,
# To allow me to achieve a particular play count average.

import math
import calendar
from datetime import date

today = date.today()
d1 = today.strftime("%d/%m/%Y")
d2 = today.strftime("%d")
d3 = today.strftime("%m")
d4 = today.strftime("%Y")

print("Today's date =", d1)
print("The current day =", d2)
print("the current month =", d3)
print("The current year is", d4)

today = d2
today = int(today)

monthNow = d3
monthNow = str(monthNow)

if monthNow != "10" or monthNow != "11" or monthNow != "12":
    monthNow = str(monthNow[1])
else:
    monthNow = str(monthNow)
#print(monthNow)

yearNow = d4
yearNow = int(yearNow)
#print(calendar.isleap(yearNow))

if calendar.isleap(yearNow):
    print("This is a leap year.")
else:
    print("This year is not a leap year")

days1 = 31
days2 = 30
days3 = 28
days4 = 29
monthLength = 0

# stage 1 - work out the length of the month
if monthNow == str(2) and calendar.isleap(yearNow):
    monthLength = days4
elif monthNow == str(2) and False == calendar.isleap(yearNow):
    monthLength = days3

elif monthNow == str(1) or monthNow == str(3) or monthNow == str(5) or monthNow == str(7) or monthNow == str(8) \
        or monthNow == str(10) or monthNow == str(12):
    monthLength = days1
# elif monthNow == str(4) or monthNow == str(6) or monthNow == str(9) or monthNow == str(11):
else:
    monthLength = days2
print("This month is", monthLength, "days long.")

scrobbles = int(input("How many scrobbles have you listened to so far this month? "))
average = scrobbles / today
rounded = math.floor(average)
print("Your current play average is: ", rounded)

# #Uses the data from the current average to ask what the target is and
# #works out how many tracks need to be listened to
target = int(input("What is your target average? "))
scrobblesNeeded = target * monthLength
print("To achieve that you have to listen to", scrobblesNeeded, "tracks")
scrobblesToGet = scrobblesNeeded - scrobbles
print("You need to listen to another", scrobblesToGet, "tracks")

# if daysLeft is 0, this prevents a ZeroDivisionError
daysLeft = monthLength - today
print("There are", daysLeft, "days left this month.")

if daysLeft != 0:
    daily = scrobblesToGet / daysLeft
    daily = math.floor(daily)
    print("You need to listen to about", daily, "tracks a day.")
    print("Thank you for using the Average Scrobble calculator.")
else:
    print("Thank you for using the Average Scrobble calculator.")
print("End")