"""Runs commands to produce convolved predicted counts map in current directory."""
import numpy as np
from astropy.coordinates import Angle
from astropy.units import Quantity
from astropy.io import fits
from astropy.wcs import WCS
from gammapy.cube import SkyCube, compute_npred_cube
from gammapy.datasets import FermiVelaRegion
from gammapy.irf import EnergyDependentTablePSF
from gammapy.utils.energy import EnergyBounds

__all__ = ['prepare_images']

def prepare_images():
    # Read in data
    fermi_vela = FermiVelaRegion()
    background_file = FermiVelaRegion.filenames()['diffuse_model']
    exposure_file = FermiVelaRegion.filenames()['exposure_cube']
    counts_file = FermiVelaRegion.filenames()['counts_cube']
    background_model = SkyCube.read(background_file, format='fermi-background')
    exposure_cube = SkyCube.read(exposure_file, format='fermi-exposure')

    # Re-project background cube
    repro_bg_cube = background_model.reproject(exposure_cube)

    # Define energy band required for output
    energies = EnergyBounds([10, 500], 'GeV')

    # Compute the predicted counts cube
    npred_cube = compute_npred_cube(repro_bg_cube, exposure_cube, energies,
                                    integral_resolution=5)

    # Convolve with Energy-dependent Fermi LAT PSF
    psf = EnergyDependentTablePSF.read(FermiVelaRegion.filenames()['psf'])
    kernels = psf.kernels(npred_cube)
    convolved_npred_cube = npred_cube.convolve(kernels, mode='reflect')

    # Counts data
    counts_cube = SkyCube.read(counts_file, format='fermi-counts')
    counts_cube = counts_cube.reproject(npred_cube)

    counts = counts_cube.data[0]
    model = convolved_npred_cube.data[0]

    # Load Fermi tools gtmodel background-only result
    gtmodel = fits.open(FermiVelaRegion.filenames()['background_image'])[0].data.astype(float)

    # Ratio for the two background images
    ratio = np.nan_to_num(model / gtmodel)

    # Header is required for plotting, so returned here
    wcs = npred_cube.wcs
    header = wcs.to_header()
    return model, gtmodel, ratio, counts, header


import os
import cv2
import random
import numpy as np
import matplotlib.pyplot as plt
import math

##### Part 1: image preprossessing #####

def rgb2gray(img):
    """
    5 points
    Convert a colour image greyscale
    Use (R,G,B)=(0.299, 0.587, 0.114) as the weights for red, green and blue channels respectively
    :param img: numpy.ndarray (dtype: np.uint8)
    :return gray_image: numpy.ndarray (dtype:np.uint8)
    """
    if len(img.shape) != 3:
        print('RGB Image should have 3 channels')
        return

    ###Your code here###
    img_gray = img[:,:,0]*0.299 + img[:,:,1]*0.587 + img[:,:,2]*0.114
    ###
    return img_gray

def convolute(img, kernal):
    y, x = img.shape
    img_pad_zeros = pad_zeros(img,1,1,1,1)

    kernal_flip = np.flipud(np.fliplr(kernal))
    m, n = kernal_flip.shape

    new_img = np.zeros((y,x))

    for i in range(y):
            for j in range(x):
                new_img[i][j] = np.sum(img_pad_zeros[i:i+m, j:j+m]*kernal_flip)
    return new_img

def gray2grad(img):
    """
    5 points
    Estimate the gradient map from the grayscale images by convolving with Sobel filters (horizontal and vertical gradients) and Sobel-like filters (gradients oriented at 45 and 135 degrees)
    The coefficients of Sobel filters are provided in the code below.
    :param img: numpy.ndarray
    :return img_grad_h: horizontal gradient map. numpy.ndarray
    :return img_grad_v: vertical gradient map. numpy.ndarray
    :return img_grad_d1: diagonal gradient map 1. numpy.ndarray
    :return img_grad_d2: diagonal gradient map 2. numpy.ndarray
    """
    sobelh = np.array([[-1, 0, 1],
                       [-2, 0, 2],
                       [-1, 0, 1]], dtype = float)
    sobelv = np.array([[-1, -2, -1],
                       [0, 0, 0],
                       [1, 2, 1]], dtype = float)
    sobeld1 = np.array([[-2, -1, 0],
                        [-1, 0, 1],
                        [0,  1, 2]], dtype = float)
    sobeld2 = np.array([[0, -1, -2],
                        [1, 0, -1],
                        [2, 1, 0]], dtype = float)

    ###Your code here####
    img_grad_h = convolute(img, sobelh)
    img_grad_v = convolute(img, sobelv)
    img_grad_d1 = convolute(img, sobeld1)
    img_grad_d2 = convolute(img, sobeld2)
    ###
    return img_grad_h, img_grad_v, img_grad_d1, img_grad_d2

def pad_zeros(img, pad_height_bef, pad_height_aft, pad_width_bef, pad_width_aft):
    """
    5 points
    Add a border of zeros around the input images so that the output size will match the input size after a convolution or cross-correlation operation.
    e.g., given matrix [[1]] with pad_height_bef=1, pad_height_aft=2, pad_width_bef=3 and pad_width_aft=4, obtains:
    [[0 0 0 0 0 0 0 0]
    [0 0 0 1 0 0 0 0]
    [0 0 0 0 0 0 0 0]
    [0 0 0 0 0 0 0 0]]
    :param img: numpy.ndarray
    :param pad_height_bef: int
    :param pad_height_aft: int
    :param pad_width_bef: int
    :param pad_width_aft: int
    :return img_pad: numpy.ndarray. dtype is the same as the input img.
    """
    height, width = img.shape[:2]
    new_height, new_width = (height + pad_height_bef + pad_height_aft), (width + pad_width_bef + pad_width_aft)
    img_pad = np.zeros((new_height, new_width)) if len(img.shape) == 2 else np.zeros((new_height, new_width, img.shape[2]))

    ###Your code here###
    if len(img.shape) == 2:
        img_pad[pad_height_bef:pad_height_bef+height, pad_width_bef:pad_width_bef+width] = img
    else:
        img_pad[pad_height_bef:pad_height_bef+height, pad_width_bef:pad_width_bef+width,:] = img
    img_pad = img_pad.astype(img.dtype)
    ###
    return img_pad

##### Part 2: Normalized Cross Correlation #####
def normalized_cross_correlation(img, template):
    """
    10 points.
    Implement the cross-correlation operation in a naive 6 nested for-loops.
    The 6 loops include the height, width, channel of the output and height, width and channel of the template.
    :param img: numpy.ndarray.
    :param template: numpy.ndarray.
    :return response: numpy.ndarray. dtype: float
    """
    Hi, Wi = img.shape[:2]
    Hk, Wk = template.shape[:2]
    Ho = Hi - Hk + 1
    Wo = Wi - Wk + 1

    ###Your code here###
    template = template.astype(float)
    sum_template = np.sum(template)

    if len(template.shape) == 3:
        for k in range(template.shape[2]):
            template[:,:,k] /= sum_template
    else:
        template /= sum_template

    template_mag = np.sqrt(np.sum(template*template))

    img = img.astype(float)

    response = np.zeros((Ho, Wo))
    for i in range(Ho):
        for j in range(Wo):
            window = img[i:i+Hk, j:j+Wk, :]
            window_mag = np.sqrt(np.sum(window*window))
            for k in range(Hk):
                for l in range(Wk):
                    if len(template.shape) >= 3:
                        for m in range(template.shape[2]):
                            response[i,j] += template[k,l,m]*window[k,l,m]
                    else:
                        response[i,j] += template[k,l]*window[k,l]
            response[i,j] /= (window_mag*template_mag)
    ###
    return response

#Normalize a RGB kernel so that the sum of each color channel is 1
def normalized_a_kernel(kernel):
    kernel = kernel.astype(float)

    sum_kernel = np.sum(kernel)

    kernel /= np.abs(sum_kernel)
    return kernel

def normalized_cross_correlation_fast(img, template):
    """
    10 points.
    Implement the cross correlation with 3 nested for-loops.
    The for-loop over the template is replaced with the element-wise multiplication between the kernel and the image regions.
    :param img: numpy.ndarray
    :param template: numpy.ndarray
    :return response: numpy.ndarray. dtype: float
    """
    Hi, Wi = img.shape[:2]
    Hk, Wk = template.shape[:2]
    Ho = Hi - Hk + 1
    Wo = Wi - Wk + 1

    ###Your code here###
    img = img.astype(float)
    template = normalized_a_kernel(template)
    template_mag = np.sqrt(np.sum(template*template))

    response = np.zeros((Ho, Wo))
    for i in range(Ho):
        for j in range(Wo):
            # three other loops are replaced with element-wise matrix multiplication
            window = img[i:i+Hk, j:j+Wk]
            window_mag = np.sqrt(np.sum(window*window))
            response[i, j] = np.sum(template*window) / (window_mag*template_mag)
    ###
    return response

def im2col(x, kernel):
    kernel_shape = kernel.shape[0]
    rows = []

    for row in range(x.shape[0] - kernel_shape + 1):
        for col in range(x.shape[1] - kernel_shape + 1):
            window = x[row: row + kernel_shape, col: col + kernel_shape]
            rows.append(window.flatten())

    return np.array(rows)

def color_im2col(img, kernel):
    if (len(img.shape) == 2 and len(kernel.shape) == 2):
        return im2col(img, kernel)

    if (len(img.shape) > 2):
        matrix_list = []
        for i in range(img.shape[2]):
            matrix_list.append(im2col(img[:,:,i], kernel))
        return np.hstack(matrix_list)

def reshape_template(kernel):
    if (len(kernel.shape) == 2):
        return kernel.flatten()
    else:
        row_list = []
        for i in range(kernel.shape[2]):
            row_list.append(kernel[:,:,i].flatten())
        return np.hstack(row_list)

def normalized_cross_correlation_matrix(img, template):
    """
    10 points.
    Converts cross-correlation into a matrix multiplication operation to leverage optimized matrix operations.
    Please check the detailed instructions in the pdf file.
    :param img: numpy.ndarray
    :param template: numpy.ndarray
    :return response: numpy.ndarray. dtype: float
    """
    Hi, Wi = img.shape[:2]
    Hk, Wk = template.shape[:2]
    Ho = Hi - Hk + 1
    Wo = Wi - Wk + 1

    ###Your code here###
    img = img.astype(float)
    img_matrix = color_im2col(img, template)

    # Normalize the magnitude of the template
    template = normalized_a_kernel(template)

    #Reshape the template into a column
    template_matrix = reshape_template(template)

    #Multiple the image matrix and the template column
    reshaped_output = np.matmul(img_matrix,template_matrix)

    #Compute the magnitude of each window from the image matrix
    img_window_mag = np.sum(img_matrix**2, axis=1)
    img_window_mag = np.sqrt(img_window_mag)

    #Compute the magnitude of template
    template_mag = np.sqrt(np.sum(template*template))

    #Scale the window magnitude with the maginitude of the template
    img_window_mag *= template_mag

    #Compute the final response and reshape it
    reshaped_output_normalized = reshaped_output / img_window_mag
    response = reshaped_output_normalized.reshape((Ho, Wo))
    ###
    return response

##### Part 3: Non-maximum Suppression #####

def non_max_suppression(response, suppress_range, threshold=None):
    """
    10 points
    Implement the non-maximum suppression for translation symmetry detection
    The general approach for non-maximum suppression is as follows:
	1. Set a threshold τ; values in X<τ will not be considered.  Set X<τ to 0.
    2. While there are non-zero values in X
        a. Find the global maximum in X and record the coordinates as a local maximum.
        b. Set a small window of size w×w points centered on the found maximum to 0.
	3. Return all recorded coordinates as the local maximum.
    :param response: numpy.ndarray, output from the normalized cross correlation
    :param suppress_range: a tuple of two ints (H_range, W_range).
                           the points around the local maximum point within this range are set as 0. In this case, there are 2*H_range*2*W_range points including the local maxima are set to 0
    :param threshold: int, points with value less than the threshold are set to 0
    :return res: a sparse response map which has the same shape as response
    """
    ###Your code here###
    if threshold == None:
        threshold = 0

    res = np.where(response > threshold, response, 0)

    while np.max(res) > 0:
        rows, columns = np.where(res == np.max(res))
        x = rows[0]
        y = columns[0]
        for j in range(-suppress_range[0], suppress_range[0]):
            for k in range(-suppress_range[1], suppress_range[1]):
                if ((x + j) in range(res.shape[0])) and ((y+k) in range(res.shape[1])):
                    res[x + j, y + k] = 0
        res[x,y] = -1

    res = np.where(res == -1, 1, 0)
    ###
    return res

##### Part 4: Question And Answer #####

def mean_subtracted_kernel(kernel):
    kernel = kernel.astype(float)
    mean_kernel = np.zeros(kernel.shape[2])
    for i in range(kernel.shape[2]):
        mean_kernel[i] = np.sum(kernel[:,:,i])
    mean_kernel = mean_kernel / (kernel.shape[0] * kernel.shape[1])

    new_kernel = kernel
    for i in range(kernel.shape[2]):
        new_kernel[:,:,i] = new_kernel[:,:,i] - mean_kernel[i]
    return new_kernel

def mean_img(img, Hk, Ho, Wk, Wo):
    mean_windows = np.zeros((Ho, Wo, img.shape[2]))
    for i in range(Ho):
        for j in range(Wo):
            window = img[i:i+Hk, j:j+Wk, :]
            mean_window = np.zeros(img.shape[2])
            for k in range(img.shape[2]):
                mean_window[k] = np.sum(window[:,:,k])
            mean_window /= (Hk * Wk)
            mean_windows[i,j] = mean_window
    return mean_windows

def normalized_cross_correlation_ms(img, template):
    """
    10 points
    Please implement mean-subtracted cross correlation which corresponds to OpenCV TM_CCOEFF_NORMED.
    For simplicty, use the "fast" version.
    :param img: numpy.ndarray
    :param template: numpy.ndarray
    :return response: numpy.ndarray. dtype: float
    """
    Hi, Wi = img.shape[:2]
    Hk, Wk = template.shape[:2]
    Ho = Hi - Hk + 1
    Wo = Wi - Wk + 1

    ###Your code here###
    mean_template = normalized_a_kernel(template)
    mean_template = mean_subtracted_kernel(mean_template)
    template_mag = np.sqrt(np.sum(mean_template*mean_template))

    pad_height_bef, pad_height_aft = template.shape[0] // 2 - (1 if template.shape[0] % 2 == 0 else 0), template.shape[0] // 2
    pad_width_bef, pad_width_aft = template.shape[1] // 2 - (1 if template.shape[1] % 2 == 0 else 0), template.shape[1] // 2

    img = img.astype(float)
    mean_windows = mean_img(img, Hk, Ho, Wk, Wo)

    response = np.zeros((Ho, Wo))
    for i in range(Ho):
        for j in range(Wo):
            window = img[i:i+Hk, j:j+Wk, :]
            window = window - mean_windows[i,j]
            window_mag = np.sqrt(np.sum(window*window))

            response[i, j] = np.sum(mean_template*window) / (window_mag*template_mag)
    ###
    return response

###############################################
"""Helper functions: You should not have to touch the following functions.
"""
def read_img(filename):
    '''
    Read HxWxC image from the given filename
    :return img: numpy.ndarray, size (H, W, C) for RGB. The value is between [0, 255].
    '''
    img = cv2.imread(filename)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    return img

def show_imgs(imgs, titles=None):
    '''
    Display a list of images in the notebook cell.
    :param imgs: a list of images or a single image
    '''
    if isinstance(imgs, list) and len(imgs) != 1:
        n = len(imgs)
        fig, axs = plt.subplots(1, n, figsize=(15,15))
        for i in range(n):
            axs[i].imshow(imgs[i], cmap='gray' if len(imgs[i].shape) == 2 else None)
            if titles is not None:
                axs[i].set_title(titles[i])
    else:
        img = imgs[0] if (isinstance(imgs, list) and len(imgs) == 1) else imgs
        plt.figure()
        plt.imshow(img, cmap='gray' if len(img.shape) == 2 else None)

def show_img_with_squares(response, img_ori=None, rec_shape=None):
    '''
    Draw small red rectangles of size defined by rec_shape around the non-zero points in the image.
    Display the rectangles and the image with rectangles in the notebook cell.
    :param response: numpy.ndarray. The input response should be a very sparse image with most of points as 0.
                     The response map is from the non-maximum suppression.
    :param img_ori: numpy.ndarray. The original image where response is computed from
    :param rec_shape: a tuple of 2 ints. The size of the red rectangles.
    '''
    response = response.copy()
    if img_ori is not None:
        img_ori = img_ori.copy()
    H, W = response.shape[:2]
    if rec_shape is None:
        h_rec, w_rec = 25, 25
    else:
        h_rec, w_rec = rec_shape

    xs, ys = response.nonzero()
    for x, y in zip(xs, ys):
        response = cv2.rectangle(response, (y - h_rec//2, x - w_rec//2), (y + h_rec//2, x + w_rec//2), (255, 0, 0), 2)
        if img_ori is not None:
            img_ori = cv2.rectangle(img_ori, (y - h_rec//2, x - w_rec//2), (y + h_rec//2, x + w_rec//2), (0, 255, 0), 2)

    if img_ori is not None:
        show_imgs([response, img_ori])
    else:
        show_imgs(response)


#importing library
from sklearn import svm, datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#from mlxtend.plotting import plot_decision_regions

#import dataset
df = pd.read_csv("Iris.csv")
df.head()

#divide dataset into: train, test, X, Y
y = df[['Species']]
X = df[['SepalLengthCm', 'SepalWidthCm']]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

print(X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)

#creating svm classification object
model_lin = svm.SVC(kernel='linear', C=1, gamma=1)

model_rbf_c1 = svm.SVC(kernel='rbf', C=1, gamma=1)
model_rbf_c2 = svm.SVC(kernel='rbf', C=10, gamma=1)
model_rbf_c3 = svm.SVC(kernel='rbf', C=100, gamma=1)

model_rbf_g1 = svm.SVC(kernel='rbf', C=1, gamma=0.5)
model_rbf_g2 = svm.SVC(kernel='rbf', C=1, gamma=1)
model_rbf_g3 = svm.SVC(kernel='rbf', C=1, gamma=10)

#training model using training set
model_lin.fit(X_train, y_train)

model_rbf_c1.fit(X_train, y_train)
model_rbf_c2.fit(X_train, y_train)
model_rbf_c3.fit(X_train, y_train)

model_rbf_g1.fit(X_train, y_train)
model_rbf_g2.fit(X_train, y_train)
model_rbf_g3.fit(X_train, y_train)

#checking score
model_lin.score(X_train, y_train)

model_rbf_c1.score(X_train, y_train)
model_rbf_c2.score(X_train, y_train)
model_rbf_c3.score(X_train, y_train)

model_rbf_g1.score(X_train, y_train)
model_rbf_g2.score(X_train, y_train)
model_rbf_g3.score(X_train, y_train)

#predict output
y_pred_lin = model_lin.predict(X_test)

y_pred_rbf_c1 = model_rbf_c1.predict(X_test)
y_pred_rbf_c2 = model_rbf_c2.predict(X_test)
y_pred_rbf_c3 = model_rbf_c3.predict(X_test)

y_pred_rbf_g1 = model_rbf_g1.predict(X_test)
y_pred_rbf_g2 = model_rbf_g2.predict(X_test)
y_pred_rbf_g3 = model_rbf_g3.predict(X_test)

#printing accuracy
print('The linear accuracy is: ', accuracy_score(y_pred_lin, y_test))
print(confusion_matrix(y_test, y_pred_lin))
print(classification_report(y_test, y_pred_lin))

print('\nThe RBF (C=1, G=1) accuracy is: ', accuracy_score(y_pred_rbf_c1, y_test))
print(confusion_matrix(y_test, y_pred_rbf_c1))
print(classification_report(y_test, y_pred_rbf_c1))

print('\nThe RBF (C=10, G=1) accuracy is: ', accuracy_score(y_pred_rbf_c2, y_test))
print('The RBF (C=100, G=1) accuracy is: ', accuracy_score(y_pred_rbf_c3, y_test))

print('\nThe RBF (C=1, G=0.5) accuracy is: ', accuracy_score(y_pred_rbf_g1, y_test))
print('The RBF (C=1, G=1) accuracy is: ', accuracy_score(y_pred_rbf_g2, y_test))
print('The RBF (C=1, G=10) accuracy is: ', accuracy_score(y_pred_rbf_g3, y_test))

def make_meshgrid(x, y, h=.02):
    """Create a mesh of points to plot in

    Parameters
    ----------
    x: data to base x-axis meshgrid on
    y: data to base y-axis meshgrid on
    h: stepsize for meshgrid, optional

    Returns
    -------
    xx, yy : ndarray
    """
    x_min, x_max = x.min() - 1, x.max() + 1
    y_min, y_max = y.min() - 1, y.max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))
    return xx, yy

def plot_contours(ax, clf, xx, yy, **params):
    """Plot the decision boundaries for a classifier.

    Parameters
    ----------
    ax: matplotlib axes object
    clf: a classifier
    xx: meshgrid ndarray
    yy: meshgrid ndarray
    params: dictionary of params to pass to contourf, optional
    """
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    out = ax.contourf(xx, yy, Z, **params)
    return out

# import some data to play with
iris = datasets.load_iris()
# Take the first two features. We could avoid this by using a two-dim dataset
X = iris.data[:, :2]
y = iris.target

# we create an instance of SVM and fit out data. We do not scale our
# data since we want to plot the support vectors
C = 1.0  # SVM regularization parameter
models = (svm.SVC(kernel='linear', C=C),
          svm.LinearSVC(C=C),
          svm.SVC(kernel='rbf', gamma=0.7, C=C),
          svm.SVC(kernel='poly', degree=3, C=C))
models = (clf.fit(X, y) for clf in models)

# title for the plots
titles = ('SVC with linear kernel',
          'LinearSVC (linear kernel)',
          'SVC with RBF kernel',
          'SVC with polynomial (degree 3) kernel')

# Set-up 2x2 grid for plotting.
fig, sub = plt.subplots(2, 2)
plt.subplots_adjust(wspace=0.4, hspace=0.4)

X0, X1 = X[:, 0], X[:, 1]
xx, yy = make_meshgrid(X0, X1)

for clf, title, ax in zip(models, titles, sub.flatten()):
    plot_contours(ax, clf, xx, yy,
                  cmap=plt.cm.coolwarm, alpha=0.8)
    ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')
    ax.set_xlim(xx.min(), xx.max())
    ax.set_ylim(yy.min(), yy.max())
    ax.set_xlabel('Sepal length')
    ax.set_ylabel('Sepal width')
    ax.set_xticks(())
    ax.set_yticks(())
    ax.set_title(title)

plt.show()


#!/usr/bin/env python3
"""
Alarm navigation menu
Tested on windows 10 (Not working at the moment)
Ubuntu 18.04
mac high sierra and Mojave
"""
import datetime
import time
import os

class AlarmMenu:
    """Alarm menu"""

    def __init__(self, alarm_app):
        self.app = alarm_app

    def menu(self):
        """Run menu for user selection input"""
        choice = input("""\
    Alarm Clock APP
    Please choose an option:
    1) Run alarm without date specification
    2) Choose a single day of the week
    3) Turn alarm on during Weekdays
    4) Turn alarm on during Weekends
    9) Load saved settings
    0) Settings
    > """)
        choice_dict = {
            "1": self.quick_start,
            "2": self.normal_mode,
            "3": self.weekday_mode,
            "4": self.weekend_mode,
            "9": self.load_settings,
            "0": self.settings
        }
        if choice_dict.get(choice) is None:
            print("\n\t{} is an Invalid input\n".format(choice))
            self.menu()
        else:
            choice_dict[choice]()

    def quick_start(self):
        """Run alarm app with only alarm time input"""
        self.run_sch()

    def normal_mode(self):
        """
        Select which day or days of the week to run alarm
        Alarm time input also required
        """
        date_choice = self.get_dates()
        self.run_sch(date_choice)

    def weekday_mode(self):
        """
        Run alarm app on weekdays only
        Alarm time input required
        """
        _weekday = [1, 2, 3, 4, 5]
        self.run_sch(_weekday)

    def weekend_mode(self):
        """
        Run alarm app on weekends only
        Alarm time input required
        """
        _weekend = [6, 7]
        self.run_sch(_weekend)

    def load_settings(self):
        """
        Load saved settings
        Schedule, time, ringtone, and volume settings can be saved
        at settings menu
        """
        if self.file_exist() is True and self.file_is_empty() is False:
            with open("settings.txt", "r") as ftxt:
                data = ftxt.read().split("\n")
                if data[0] != "None":
                    date_choice = [int(i) for i in data[0]]
                else:
                    date_choice = None
                if data[1] != "None":
                    time_choice = data[1]
                else:
                    time_choice = None
                if data[2] != "None":
                    ringtone_choice = data[2]
                else:
                    ringtone_choice = None
                if data[3] != "None":
                    volume_choice = data[3]
                else:
                    volume_choice = None
                self.run_sch(choice=date_choice, al_time=time_choice,
                             ringtone=ringtone_choice, vol=volume_choice)
        else:
            print("\n\tNo settings saved!"
                  " Please first set settings at settings menu\n")
            self.menu()

    def settings(self):
        """Settings menu"""
        choice = input("""\
    Select setting options
    1) set schedule settings
    2) set alarm time settings
    3) set ringtone settings
    4) set alarm volume settings
    5) view saved settings
    0) return to main menu
    > """)
        choice_dict = {
            "1": self._set_schedule,
            "2": self._set_time,
            "3": self._set_ringtone,
            "4": self._set_volume,
            "5": self._view_settings,
            "0": self.menu,
        }
        if choice_dict.get(choice) is None:
            print("\n\tInvalid input")
            self.settings()
        else:
            choice_dict[choice]()

    def run_sch(self, choice=None, al_time=None, ringtone=None, vol=None):
        """
        Runs alarm app according to input schedule
        App will sleep until alarm time to prevent uneccessary CPU usage
        to run in the background silently
        """
        alarm_time = self.get_alarm_time(al_time)
        print("\n\tAlarm is ON\n")
        try:
            while True:
                while alarm_time != self.current_time():
                    time.sleep(1)
                if choice is None:
                    self._run_sch(ringtone, vol)
                else:
                    for i in choice:
                        if i == self.current_date().isoweekday() and \
                           alarm_time == self.current_time():
                            self._run_sch(ringtone, vol)
        except KeyboardInterrupt:
            print("\n\tAlarm has been turned OFF")

    def _run_sch(self, ringtone, vol):
        """
        run schedule
        Sleep is implemented if user attempted KeyboardInterrupt
        during alarm time to prevent alarmrm from continuously ringing
        """
        self.app(ringtone, vol)
        print("\n\tAlarm is still ON\n\tPress ctrl + c again to turn off!")
        time.sleep(60)

    @staticmethod
    def current_date():
        """Get current date"""
        return datetime.date.today()

    @staticmethod
    def current_time():
        """Get current time"""
        return str(datetime.datetime.today())[-15:-10]

    def get_alarm_time(self, al_time=None):
        """Set alarm time"""
        if al_time is None:
            hour, minute = None, None
        else:
            hour = al_time[:-3]
            minute = al_time[3:]
        return self._get_hour(hour) + ":" + self._get_minute(minute)

    def _get_hour(self, hour):
        """
        hour must be in 24 hour format
        hour must have 2 digits (e.g.: 1 -> 01)
        """
        # list of values from 00 to 23, append 0 if single digit
        hour_format = ["0"+str(i) if i < 10 else str(i) for i in range(0, 24)]
        if hour is None:
            hour = input(" Please set the alarm hour (xx) time: ")
        if hour not in hour_format:
            print("\n\tInvalid hour input (only 00 to 23 are acceptable)\n")
            return self._get_hour(None)
        return hour

    def _get_minute(self, minute):
        """minutes must have 2 digits (e.g.: 1 -> 01)"""
        # list of values from 00 to 59, append if single digit
        minute_format = ["0"+str(i) if i < 10 else str(i)
                         for i in range(0, 60)]
        if minute is None:
            minute = input(" Please set the alarm minute (xx) time: ")
        if minute not in minute_format:
            print("\n\tInvalid minute input (only 00 to 59 are acceptable)\n")
            return self._get_minute(None)
        return minute

    def get_dates(self, choice=None):
        """Set a day or days to turn alarm on"""
        if choice is None:
            choice = input("""\
     Which day or days do you want the alarm to be turned on?
     Monday    : 1
     Tuesday   : 2
     Wednesday : 3
     Thursday  : 4
     Friday    : 5
     Saturday  : 6
     Sunday    : 7
     > """)
        return self._get_dates(choice)

    def _get_dates(self, choice):
        """get dates main function"""
        date_dict = {
            "1": 1,
            "2": 2,
            "3": 3,
            "4": 4,
            "5": 5,
            "6": 6,
            "7": 7
        }
        date_choice = []
        for i in choice:
            if date_dict.get(i) is None:
                print("\n\tInvalid input\n")
                return self.get_dates(choice=None)
            date_choice.append(int(i))
        return date_choice

    @staticmethod
    def _initial_write(_dates="None", _time="None",
                       _ringtone="None", _volume="None"):
        """Create settings.txt and initialize inputs as None"""
        with open("settings.txt", "w") as ftxt:
            ftxt.write(_dates + "\n")
            ftxt.write(_time + "\n")
            ftxt.write(_ringtone + "\n")
            ftxt.write(_volume + "\n")

    @staticmethod
    def _overwrite(line_num, _data):
        """
        Overwrite saved settings in settings.txt
        for loop is used to rewrite each line in the settings.txt
        """
        with open("settings.txt", "r+") as ftxt:
            new_data = ftxt.read().split("\n")
            new_data[line_num] = _data
            ftxt.seek(0)
            for i in range(4):
                ftxt.write(new_data[i] + "\n")

    def _set_schedule(self):
        """Set, write, and save schedule settings to settings.txt file"""
        _dates = "".join([str(i) for i in self.get_dates()])
        if self.file_exist() is True:
            if self.file_is_empty() is False:
                self._overwrite(0, _dates)
                return self.settings()
        self._initial_write(_dates=_dates)
        return self.settings()

    def _set_time(self):
        """Set, write, and save alarm time setting to settings.txt file"""
        _time = self.get_alarm_time()
        if self.file_exist() is True:
            if self.file_is_empty() is False:
                self._overwrite(1, _time)
                return self.settings()
        self._initial_write(_time=_time)
        return self.settings()

    def _set_ringtone(self):
        """Set, write, and save ringtone setting to settings.txt file"""
        _path = input("""\
    Key in file path
    e.g.: 'alarm-volume-control/audio/sample.mp3'
    > """)
        if self.file_exist() is True:
            if self.file_is_empty() is False:
                self._overwrite(2, _path)
                return self.settings()
        self._initial_write(_ringtone=_path)
        return self.settings()

    def _set_volume(self):
        """Set, write, and save alarm volume setting to settings.txt file"""
        _vol = input("Alarm volume level: 0 to 100\n> ")
        if self.file_exist() is True:
            if self.file_is_empty() is False:
                self._overwrite(3, _vol)
                return self.settings()
        self._initial_write(_volume=_vol)
        return self.settings()

    def _view_settings(self):
        """
        View currently saved settings
        Currently, only 4 settings can be implemented
        therefore in the for loop, range is set to (4)
        to display all saved settings
        """
        if self.file_exist() is True:
            if self.file_is_empty() is False:
                for i in range(4):
                    self._read(i)
                return self.settings()
        print("\n\tNo settings saved\n")
        return self.settings()

    @staticmethod
    def _read(line_num):
        """Read settings.txt file"""
        with open("settings.txt", "r") as ftxt:
            lines = ftxt.readlines()
            ftxt.seek(0)
            for i, line in enumerate(lines):
                if i == line_num:
                    print(line, end="")

    @staticmethod
    def file_is_empty():
        """Check if text file is empty"""
        return os.stat("settings.txt").st_size == 0

    @staticmethod
    def file_exist():
        """Check if file exists"""
        return os.path.isfile("settings.txt")


from selenium.webdriver.common.by import By

class AdministrationLocators(object):

    overview = (By.LINK_TEXT, "Overview")
    projects = (By.LINK_TEXT, "Projects")
    view_projects = (By.LINK_TEXT, "View projects")
    add_project = (By.LINK_TEXT, "Add a new project")
    users_roles = (By.LINK_TEXT, "Users & Roles")
    forget_user = (By.LINK_TEXT, "Forget this user")

    banner = (By.CLASS_NAME, "empty-with-explanation")
    banner_title = (By.CLASS_NAME, "empty-title")
    banner_body = (By.CLASS_NAME, "empty-body")


import numpy as np
from sklearn.tree import DecisionTreeClassifier

class TreeDiscretizer:
    """A DecisionTreeClassifier of which the leaf indices are used as cluster indices.

    Parameters
    ----------
    X_train : array-like, shape (n_samples, n_features), floats
        The feature matrix of the training set.

    y_train : array-like, shape (n_samples), nonnegative ints
        The labels of X_train.

    J : int
        The maximum number of desired clusters.

    criterion : string, optional
        The split criterion of the underlying DecisionTreeClassifier, 'gini' by default.

    seed : nonnegative int, optional
        This seed for the random number generator makes random splits reproducible.
    """
    def __init__(self, X_train, y_train, J, criterion = 'gini', seed = None):
        self.classifier = DecisionTreeClassifier(
          max_leaf_nodes = J,
          criterion = criterion,
          random_state = seed
        )
        self.classifier.fit(X_train, y_train)
        x_train = self.classifier.apply(X_train)
        self.indexmap = dict(zip(np.unique(x_train), range(len(np.unique(x_train)))))
    def discretize(self, X):
        x_raw = self.classifier.apply(X) # return the raw leaf indices of X
        return np.array([ self.indexmap[x] for x in x_raw ])

def fit_pdf(x, bins = None, normalize = True):
    """Estimate the discrete probability density function (pdf) g of the observed values x.

    Parameters
    ----------
    x : array-like, shape (n_samples,), nonnegative ints
        The indices of the J observed clusters.

    bins : array-like, shape (J,), nonnegative ints, optional
        The J indices of the observed clusters, i.e. the unique values of x.

    normalize : boolean, optional
        True, if the result g should be normalized to a probability density function.
        Otherwise, the integer counts of each bin are returned.

    Returns
    ----------
    g : array-like, shape (J,), floats
       The empirical discrete pdf of the observed values x.
    """
    if bins is None:
        bins = range(np.min(x), np.max(x)+1)
    bincounts = np.bincount(x, minlength = np.max(bins)+1)[bins]
    return normalizepdf(bincounts) if normalize else bincounts


from pylatex import Document, Section, Subsection, Tabular, MultiColumn,\
    MultiRow, NoEscape
from pylatex.math import Math
from collections import OrderedDict
import copy
import numpy as np

from modelguidedattacks.results import build_full_results_dict

def result_to_str(result, long=False):
    if result is None or np.isinf(result) or np.isnan(result):
        return "-"
    elif long:
        return f"{result:.4f}"
    else:
        return f"{result:.2f}"

"""
results top level will be keyed by K
next level will be binary search steps
next level will be keyed by iterations
next level will be keyed by method
"""

only_mean = False
model_name = "resnet50"
results = build_full_results_dict(model_name)

model_to_tex = {
    "resnet50": "Resnet-50",
    "densenet121": "Densenet121",
    "deit_small": "DeiT-S",
    "vit_base": "ViT$_{B}$"
}

# Preprocess all results and select bests
for top_k, bs_dict in results.items():
    for num_bs, iter_dict in bs_dict.items():
        for num_iter, method_dict in iter_dict.items():
            metric_bests = {}
            metrics_compared = {}

            for method_name, method_results in method_dict.items():
                for metric_name, metric_value in method_results.items():
                    reduction_func = max if "ASR" in metric_name else min

                    if metric_name not in metric_bests:
                        metric_bests[metric_name] = 0. if reduction_func is max else np.Infinity
                        metrics_compared[metric_name] = 0

                    if metric_value is not None:
                        metric_bests[metric_name] = reduction_func(metric_bests[metric_name], metric_value)
                        metrics_compared[metric_name] += 1

            for method_name, method_results in method_dict.items():
                for metric_name, metric_value in method_results.items():
                    method_results[metric_name] = result_to_str(metric_value, "inf" in metric_name or "ASR" in metric_name)

                    if metric_value is not None and np.allclose(metric_value, metric_bests[metric_name]) \
                        and metrics_compared[metric_name] > 1:
                        method_results[metric_name] = rf"\textbf{{ {method_results[metric_name]} }}"

method_tex = {
    "cwk": r"CW^K",
    "ad": r"AD",
    "cvxproj": r"\textbf{QuadAttac$K$}"
}

doc = Document("multirow")

protocol_cols = 1
attack_method_cols = 1
best_cols = 4
mean_cols = 4
worst_cols = 4

if only_mean:
    col_widths = [protocol_cols, attack_method_cols, mean_cols]
else:
    col_widths = [protocol_cols, attack_method_cols, best_cols, mean_cols, worst_cols]

total_cols = sum(col_widths)
tabular_string = "|"

for w in col_widths:
    tabular_string += "l" * w + "|"

table1 = Tabular(tabular_string)
table1.add_hline()
table1.add_row((MultiColumn(total_cols, align='|c|', data=NoEscape(model_to_tex[model_name])),))
table1.add_hline()

if only_mean:
    table1.add_row((
        MultiRow(2, data="Protocol"),
        MultiRow(2, data="Attack Method"),
        MultiColumn(mean_cols, align="|c|", data="Mean"),
    ))
else:
    table1.add_row((
        MultiRow(2, data="Protocol"),
        MultiRow(2, data="Attack Method"),
        MultiColumn(best_cols, align="|c|", data="Best"),
        MultiColumn(mean_cols, align="|c|", data="Mean"),
        MultiColumn(worst_cols, align="|c|", data="Worst"),
    ))

table1.add_hline(start=protocol_cols + attack_method_cols + 1)

num_result_colums = 1 if only_mean else 3
table1.add_row("",
               "",
               *(NoEscape(r"ASR$\uparrow$"),
               NoEscape(r"$\ell_1 \downarrow$"),
               NoEscape(r"$\ell_2 \downarrow$"),
               NoEscape(r"$\ell_{\infty} \downarrow$"))*num_result_colums
               )

table1.add_hline()

for top_k, bs_dict in results.items():

    total_results = 0
    # Count total results
    for _, iter_dict in bs_dict.items():
        for num_iter, method_dict in iter_dict.items():
            total_results += len(method_dict)

    top_k_latex_obj = MultiRow(total_results, data=f"Top-{top_k}")

    shown_topk_obj = False

    for bs_steps, iter_dict in bs_dict.items():
        for num_iter, method_dict in iter_dict.items():
            for method_name, method_results in method_dict.items():
                first_obj = top_k_latex_obj if not shown_topk_obj else ""
                shown_topk_obj = True

                row_results = []

                reduction_names = ["mean"] if only_mean else ["best", "mean", "worst"]
                for reduction in reduction_names:
                    for metric in ["ASR", "L1", "L2", "L_inf"]:
                        result_key = f"{metric}_{reduction}"
                        long_result = "inf" in metric
                        row_results.append(
                            NoEscape(method_results[result_key])
                            )

                table1.add_row(
                    first_obj,
                    NoEscape("$" + method_tex[method_name] +
                            f"_{{{bs_steps}x{num_iter}}}$"),
                    *row_results
                )

            table1.add_hline(start=protocol_cols + 1)

    table1.add_hline()

# doc.append(table1)

print(table1.dumps())


from flask import Blueprint, render_template, redirect, url_for, request, jsonify
from flask_login import login_required, current_user
from datetime import datetime
from src import db
from src.models import User, Tweet, Like, Comment
from src.tweets.forms import CreateTweetForm, CreateCommentForm
import markdown2
import bleach

tweets = Blueprint('tweets', __name__)

@tweets.route("/tweet/create", methods=['GET', 'POST'])
@login_required
def tweet_create():
    form = CreateTweetForm()
    if form.validate_on_submit():
        time = datetime.utcnow()
        markdown = markdown2.markdown(form.textbody.data)
        bleached_markdown = bleach.clean(markdown, tags=['b', 'i', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'abbr', 'acronym', 'p', 'code', 'blockquote', 'table', 'tr', 'td', 'br', 'ol', 'li', 'pre', 'div', 'span', 'em', 'strong', 'cite', 'col', 'colgroup', 'datalist', 'dd', 'dt', 'dl', 'q', 's', 'small', 'sub', 'sup', 'td', 'tbody', 'th', 'thead', 'u'])
        tweet = Tweet(identifier=Tweet.get_identifier(), textbody_source=form.textbody.data, textbody_markdown=bleached_markdown, author=current_user, created_utc=time, is_nsfw=form.is_nsfw.data)
        db.session.add(tweet)
        db.session.commit()
        return redirect(url_for('main.home'))
    return render_template('tweets/tweet_create.html', title='Create a tweet', form=form)

@tweets.route("/tweet/<string:ident>")
@login_required
def tweet_show(ident):
    tweet = Tweet.query.filter_by(identifier=ident).first_or_404()
    comments = tweet.comments.filter(Comment.parent == None).all()
    return render_template('tweets/tweet.html', tweet=tweet, comments=comments, showCreateComment=True)

@tweets.route("/tweet/<string:ident>/edit", methods=['GET', 'POST'])
@login_required
def tweet_edit(ident):
    tweet = Tweet.query.filter_by(identifier=ident).first_or_404()
    if current_user.id != tweet.userid:
        abort(403)
    form = CreateTweetForm()
    if form.validate_on_submit():
        tweet.textbody_source = form.textbody.data
        tweet.textbody_markdown = markdown2.markdown(form.textbody.data)
        edited_time = datetime.utcnow()
        tweet.edited_utc = edited_time
        tweet.is_edited = True
        tweet.is_nsfw = form.is_nsfw.data
        db.session.commit()
        return redirect(url_for('main.home'))
    elif request.method == 'GET':
        form.textbody.data = tweet.textbody_source
        form.is_nsfw.data = tweet.is_nsfw
    return render_template('tweets/tweet_create.html', form=form, is_editing=True)

@tweets.route("/tweet/like", methods=['POST'])
def tweet_like():
    if not current_user.is_authenticated:
        response = {"statuscode": -1, "status": "Permission denied"}
        return jsonify(response)

    tweet_ident = request.json['ident']
    tweet = Tweet.query.filter_by(identifier=tweet_ident).first()
    if not tweet:
        response = {"statuscode": -1, "status": "Tweet not found"}
    else:
        if current_user.has_liked_tweet(tweet):
            current_user.unlike_tweet(tweet)
            response = {"statuscode": 0, "status": "Liked"}
        else:
            current_user.like_tweet(tweet)
            response = {"statuscode": 0, "status": "Unliked"}
        db.session.commit()
    return jsonify(response)

@tweets.route("/tweet/sticky", methods=['POST'])
@login_required
def tweet_sticky():
    if not current_user.is_authenticated:
        response = {"statuscode": -1, "status": "Permission denied"}
        return jsonify(response)

    tweet_ident = request.json['ident']
    tweet = Tweet.query.filter_by(identifier=tweet_ident).first()
    if tweet.userid == current_user.id:
        tweet.stickied = not tweet.stickied
        db.session.commit()
        response = {"statuscode": 0}
        return jsonify(response)
    else:
        response = {"statuscode": -1, "status": "Cannot sticky/unsticky the tweet"}
        return jsonify(response)
    response = {"statuscode": -1}
    return jsonify(response)

@tweets.route("/tweet/<string:ident>/comment/create", methods=['GET', 'POST'])
@login_required
def comment_create(ident):
    form = CreateCommentForm()
    tweet = Tweet.query.filter_by(identifier=ident).first_or_404()
    if form.validate_on_submit():
        comment = Comment(textbody=form.textbody.data, identifier=Comment.get_identifier(), tweet=tweet, author=current_user, recipient=tweet.author)
        comment.save()
        tweet.author.add_notification('unread_notifs_count', tweet.author.new_notifs())
        db.session.commit()
        return redirect(url_for('tweets.tweet_show', ident=ident))
    return render_template('tweets/comment_create.html', form=form)

@tweets.route("/comment/<string:ident>/replies", methods=['GET'])
@login_required
def comment_replies(ident):
    comment = Comment.query.filter_by(identifier=ident).first_or_404()
    return render_template('tweets/replies.html', comment=comment)

@tweets.route("/comment/<string:ident>/reply", methods=['GET', 'POST'])
@login_required
def comment_reply(ident):
    form = CreateCommentForm()
    p_comment = Comment.query.filter_by(identifier=ident).first_or_404()
    if form.validate_on_submit():
        comment = Comment(textbody=form.textbody.data, identifier=Comment.get_identifier(), tweet=p_comment.tweet, author=current_user, recipient=p_comment.tweet.author, parent=p_comment)
        comment.save()
        p_comment.author.add_notification('unread_notifs_count', p_comment.author.new_notifs())
        db.session.commit()
        return redirect(url_for('tweets.comment_replies', ident=p_comment.identifier))
    return render_template('tweets/comment_create.html', form=form, is_reply=True)


# Importación de las librerías necesarias para este file
import pandas as pd
from tensorflow.keras import Model
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense

from importlib.machinery import SourceFileLoader
mp = SourceFileLoader("umap_", "venvIA/lib/python3.9/site-packages/umap/umap_.py").load_module()

#Clase autoencoder
class AutoEncoders(Model):

  def __init__(self, output_units, dims):

    super().__init__()
    self.encoder = Sequential( #Codificar
        [
          Dense(dims, activation="relu"),
          Dense(dims, activation="relu"),
          Dense(dims+1, activation="relu")
        ]
    )

    self.decoder = Sequential( #Decodificar
        [
          Dense(dims, activation="relu"),
          Dense(dims, activation="relu"),
          Dense(output_units, activation="sigmoid")
        ]
    )

  def call(self, inputs): #Llama al autoencoder

    encoded = self.encoder(inputs)
    decoded = self.decoder(encoded)
    return decoded

'''
Función: get_MPL_encoding

Descripción: Crea el autoencoder y codifica los datos

Parametros: data (array de numpy), dims (int)

Retorna: reduced_df (dataframe de pandas)
'''
def get_MLP_encoding(data, dims):

  data = pd.DataFrame(data)

  auto_encoder = AutoEncoders(len(data.columns), dims)

  auto_encoder.compile(loss='mae', metrics=['mae'], optimizer='adam')

  history = auto_encoder.fit(data, data, epochs=15, batch_size=32, verbose=0)

  encoder_layer = auto_encoder.get_layer('sequential')
  reduced_df = pd.DataFrame(encoder_layer.predict(data))
  reduced_df = reduced_df.add_prefix('feature_')

  return reduced_df

'''
Función: umap2d3d

Descripción: Aplica el algoritmo UMAP a los datos para dejarlos en 2d

Parametros: data (array de numpy), dims (int)

Retorna: embedding (dataframe de pandas)
'''
def umap2d3d(data, dims):
  reducer = mp.UMAP(n_components=dims)
  embedding = reducer.fit_transform(data)

  return embedding


import threading
import numpy as np
import json
import sys
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, QtCore
from pyqtgraph.Point import Point
import traceback
from slicer_constants import MIN_X, MAX_X, POS, SET_SPOTS, SET_REGION, PLAYBACK_MARKER, TIME_IN_HISTORY

spots = [{POS: [0, 0]}, {POS: [2, 1]}]
region = None
scatter_plot = None
# playback_marker_scatter = None

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    sys.stderr.flush()

def update():
    global region
    region.setZValue(10)
    min_x, max_x = region.getRegion()
    print(json.dumps({MIN_X: min_x, MAX_X: max_x}))
    sys.stdout.flush()

def set_spots(spots_json):
    global spots
    spots = spots_json
    scatter_plot.setData(spots)

def set_region(region_json):
    global spots
    global region
    min_x = region_json[MIN_X]
    max_x = region_json[MAX_X]
    if not np.isfinite(min_x): min_x = min(spot[POS][0] for spot in spots)
    if not np.isfinite(max_x): max_x = max(spot[POS][0] for spot in spots)

    region.setRegion([min_x, max_x])

def set_playback_marker(playback_marker_json):
    global playback_marker
    global view_box
    # This shit makes the gui freeze after a while for some reason

    # time_in_history = playback_marker_json[TIME_IN_HISTORY]
    # if time_in_history < -1000:
    #     return
    # min_x, max_x = region.getRegion()
    # time_in_history = min(max_x, max(min_x, time_in_history))
    # playback_spots = [ {POS: (time_in_history, i)} for i in range(7) ]
    # playback_marker_scatter.setData(playback_spots)
    # eprint(time_in_history)
    # playback_marker.setPos((time_in_history,0))
    # eprint (playback_marker_json)

def read_input():
    try:
        while True:
            try:
                line = input()
            except EOFError as e:
                return
            message = json.loads(line)
            if SET_SPOTS in message:
                set_spots(message[SET_SPOTS])
            elif SET_REGION in message:
                set_region(message[SET_REGION])
            elif PLAYBACK_MARKER in message:
                set_playback_marker(message[PLAYBACK_MARKER])
            else:
                eprint('bad message: ', message)
    except Exception as e:
        traceback.print_exc()
        sys.stderr.flush()
        os.exit(-1)

class NonFocusStealingGraphicsWindow(pg.GraphicsWindow):
    def show(self):
        self.setAttribute(98) # Qt::WA_ShowWithoutActivating
        super().show()

def main():
    global region
    global scatter_plot
    global view_box

    # window layout
    app = QtGui.QApplication([])
    win = NonFocusStealingGraphicsWindow(title='Slicer')
    win.setGeometry(0, 660, 600, 380)

    label = pg.LabelItem(justify='right')
    win.addItem(label)
    view_box = win.addPlot(row=1, col=0)

    region = pg.LinearRegionItem()
    region.setZValue(10)
    region.sigRegionChanged.connect(update)
    # Add the LinearRegionItem to the ViewBox, but tell the ViewBox to exclude this
    # item when doing auto-range calculations.
    view_box.addItem(region, ignoreBounds=True)

    # pg.dbg()
    scatter_plot = pg.ScatterPlotItem(size=10, pen=pg.mkPen(None), brush=pg.mkBrush(255, 255, 255, 120))
    set_spots(spots)
    view_box.addItem(scatter_plot)

    # playback_marker = pg.InfiniteLine(pos=(0,0), angle=30)
    # view_box.addItem(playback_marker)
    # global playback_marker_scatter
    # playback_marker_scatter = pg.ScatterPlotItem(size=10, pen=pg.mkPen(None), brush=pg.mkBrush(255, 255, 255, 120))
    # view_box.addItem(playback_marker_scatter)

    threading.Thread(target=read_input, daemon=True).start()

    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    main()


import numpy as np
import cv2
import os

face_cascade = cv2.CascadeClassifier('C:\\Users\\Kash\\AppData\\Local\\Programs\\Python\\Python36-32\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('C:\\Kash\\AppData\\Local\\Programs\\Python\\Python36-32\\Lib\\site-packages\\cv2\\data\\haarcascade_eye.xml')
face_id = 3
cap = cv2.VideoCapture(0)
count = 0
while True:
    ret, img = cap.read()
    img = cv2.flip(img, 1)  # flip video image vertically
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        count += 1

        # Save the captured image into the datasets folder
        cv2.imwrite("F:\\Jarvis\\dataset\\User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y + h, x:x + w])

        cv2.imshow('image', img)

    k = cv2.waitKey(100) & 0xff  # Press 'ESC' for exiting video
    if k == 27:
        break
    elif count >= 100:
        break

# Do a bit of cleanup
print("\n [INFO] Exiting Program and cleanup stuff")
cap.release()
cv2.destroyAllWindows()


'''
采集代理
'''
import telnetlib

from utils import gather

def get_proxies():
    url, proxies = "https://www.kuaidaili.com/free/inha/{page}/", []

    for i in range(1, 4):
        etree = gather.get_html_to_etree(url.format(page=i))
        ips, ports = etree.xpath('//td[@data-title="IP"]/text()'), etree.xpath('//td[@data-title="PORT"]/text()')
        for i in range(0, len(ips)):
            try:
                print('检测{ip}:{port}'.format(ip=ips[i], port=ports[i]))
                telnetlib.Telnet(ips[i], port=ports[i], timeout=5)
            except BaseException:
                print('{ip}:{port}超时'.format(ip=ips[i], port=ports[i]))
            else:
                proxie = {
                    'http': 'http://{ip}:{port}'.format(ip=ips[i], port=ports[i]),
                    'https': 'http://{ip}:{port}'.format(ip=ips[i], port=ports[i]),
                }
                proxies.append(proxie)
    return proxies


import json
import os
import time
from datetime import date, datetime
from typing import Tuple

import boto3
import pandas as pd
from botocore.exceptions import ClientError

s3 = boto3.resource('s3')
dynamodb = boto3.resource('dynamodb')

history_table_name = os.environ['DOG_HISTORY_TABLE_NAME']
summary_table_name = os.environ['DOG_SUMMARY_TABLE_NAME']

def get_dogs(dog_list_json: dict) -> Tuple[dict, date]:
    json_packet = json.loads(dog_list_json)
    dogs_json = json_packet['dogs']
    count_date = datetime.utcfromtimestamp(json_packet['report_date'])
    # Set UTC time to noon, so the local date portion will be on the same day for US timezones.
    count_date = count_date.replace(hour=12, minute=0, second=0, microsecond=0)

    return dogs_json, count_date

def get_dog_dataframe(dogs_json: dict, count_date: date) -> pd.DataFrame:
    dogs_df = pd.DataFrame(dogs_json)
    dogs_df['record_date'] = count_date
    dogs_df['name'] = dogs_df.index
    dogs_df.set_index(['id', 'record_date'], inplace=True)
    dogs_df.index.set_names(['animal_id', 'record_date'], inplace=True)
    return dogs_df

def write_dogs_to_daily_table(dogs_df: pd.DataFrame) -> pd.DataFrame:
    daily_table = dynamodb.Table(history_table_name)
    dogs_flat = pd.DataFrame(dogs_df.to_records())
    dogs_flat['record_date'] = dogs_flat.record_date.apply(lambda rd: int(rd.timestamp()))
    dogs_dict = dogs_flat.to_dict(orient='index')
    count = 1
    for rec in dogs_dict.values():
        daily_table.put_item(Item=rec)
        if count % 50 == 0:
            time.sleep(1)
        count += 1
    return dogs_flat

def write_dogs_to_summary_table(dogs_df: pd.DataFrame):
    summary_table = dynamodb.Table(summary_table_name)
    dog_summary_df = dogs_df.copy()
    dog_summary_df.sort_values(['animal_id', 'record_date'], inplace=True)
    dog_summary_df.drop(
        ['location', 'holder', 'bite', 'kc', 'level_note', 'stress', 'stress_note', 'team', 'diet', 'dbs', 'notes'],
        axis=1, inplace=True)
    dog_summary_df.drop_duplicates(subset=['animal_id'], keep='last', inplace=True)
    dogs_summary_dict = dog_summary_df.to_dict(orient='index')

    count = 1
    for rec in dogs_summary_dict.values():
        try:
            summary_entry = summary_table.get_item(
                Key={
                    'animal_id': rec['animal_id']
                }
            )
            if summary_entry.get('Item') is None:
                rec['days_on_campus'] = 1
                summary_table.put_item(Item=rec)
            else:
                response = summary_table.update_item(
                    Key={
                        'animal_id': rec['animal_id']
                    },
                    UpdateExpression="SET #level = :dbs_level, \
                                       #name = :animal_name, \
                                       weight = :weight, \
                                       record_date = :rec_date, \
                                       age = :age \
                                       ADD days_on_campus :val",
                    ExpressionAttributeNames={
                        '#level': 'Level',
                        '#name': 'name'
                    },
                    ExpressionAttributeValues={
                        ':dbs_level': rec['Level'],
                        ':val': 1,
                        ':animal_name': rec['name'],
                        ':weight': rec['weight'],
                        ':rec_date': rec['record_date'],
                        ':age': rec['age']
                    },
                    ConditionExpression=":rec_date > record_date",
                    ReturnValues='UPDATED_NEW'
                )
        except ClientError as e:
            print(e)
            break
        if count % 30 == 0:
            time.sleep(1)
        count += 1

def lambda_handler(event, context):
    bucket = event['Records'][0]['s3']['bucket']['name']
    file = event['Records'][0]['s3']['object']['key']
    print(bucket, file)
    dog_list = s3.Object(bucket, file)
    dog_list_json = dog_list.get()['Body'].read().decode('utf-8')
    dogs, report_time = get_dogs(dog_list_json)
    dogs_df = get_dog_dataframe(dogs, report_time)
    dog_history_df = write_dogs_to_daily_table(dogs_df)
    write_dogs_to_summary_table(dog_history_df)

    return {
        'statusCode': 200,
        'body': json.dumps(f'Data loaded to {history_table_name} and {summary_table_name} tables')
    }


"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.urls import re_path
from users import views

app_name = 'users'

urlpatterns = [
    re_path(r'^register/$', views.register, name='register'),
    re_path(r'^login/$', views.login, name='login'),
    re_path(r'^user/(?P<pk>\d+)/profile/$', views.profile, name='profile'),
    re_path(r'^user/(?P<pk>\d+)/profile/update/$', views.profile_update, name='profile_update'),
    # re_path(r'^user/(?P<pk>\d+)/pwdchange/$', views.pwd_change, name='pwd_change'),
    # re_path(r'^logout/$', views.logout, name='logout'),
]


import torch
from torch.nn import Linear
import torch.nn.functional as F

class LipschitzLinear(Linear):
    def __init__(self, lipschitz: float, projection, in_features: int, out_features: int, bias: bool = True):

        super().__init__(in_features, out_features, bias)

        self.lipschitz = lipschitz
        self.projection = projection

    def forward(self, x):
        lipschitz_weight = self.projection(self.weight, self.lipschitz)
        return F.linear(x, lipschitz_weight, self.bias)


from Board import *
import pygame

class Graphics:
    def __init__(self):
        self.window_size = [600, 600]
        self.square_size = self.window_size[0] // 8
        self.piece_size = self.square_size // 2
        self.screen = pygame.display.set_mode((self.window_size[0], self.window_size[1]))
        self.background = pygame.image.load('resources/board.png')

    def update_graphics(self, board):
        self.screen.fill(BLACK)
        self.screen.blit(self.background, self.background.get_rect())
        self.draw_board(board)
        pygame.display.update()

    def draw_board(self, board):
        for checker in board.get_ai_checkers():
            pygame.draw.circle(self.screen, RED, self.get_pixel_pos(checker.pos()), self.piece_size)
        for checker in board.get_player_checkers():
            pygame.draw.circle(self.screen, BLUE, self.get_pixel_pos(checker.pos()), self.piece_size)

    def get_pixel_pos(self, idx):
        return self.square_size * (idx % 8) + self.piece_size, self.square_size * (idx // 8) + self.piece_size


from django.db import models
from django.db.models import Q
from permissions.models import Permission
from people.models import Group as ResearchGroup
from finance.models import ExpenseCode

class ReimbursementQuerySet(models.QuerySet):
    pass
    # User dependent Querysets
    # =========================================================================

    def owned_by(self, user):
        """
        Filters the Queryset to objects owned by the User or where
        he is referenced in a Foreign Key.

        This is by default what everyone sees if they have no permissions.
        """
        return self.filter( Q(created_by=user) | Q(person__auth_user=user) ).distinct()

    def managed_by(self, user, required_codenames, default=None):
        """
        Filters the Queryset to objects the user is allowed to manage
        given his Authorization Group profiles.

        Uses the RankedPermissions table.
        """
        if default is None:
            default = self.none()

        if user.is_superuser:
            return self

        ranked_permissions = Permission.objects.filter_by_user_and_auth_permissions(
            user, self.model, required_codenames)

        if ranked_permissions.exists():
            # check if the user has permissions to all registers
            if ranked_permissions.filter(researchgroup=None).exists():
                return self
            else:
                # check which expense codes the user has permissions to
                researchgroups = [p.researchgroup for p in ranked_permissions]
                auth_groups   = [g.groupdjango for g in researchgroups]
                expenses_with_access = ExpenseCode.objects.filter(
                    project__costcenter__group__in=auth_groups
                )

                # The owner of the reimbursements or the manager of the expense codes can access
                return self.filter(
                    Q(expenses__expensecode__in=expenses_with_access) |
                    Q(created_by=user) |
                    Q(person__auth_user=user)
                )

        return default.distinct()

    # PyForms Querysets

    def list_permissions(self, user):

        return self.managed_by(
            user,
            ['can_approve_reimbursements', 'manage_reimbursements', 'receive_approved_notifications'],
            default=self.owned_by(user)
        )

    def has_add_permissions(self, user):
        return True

    def has_view_permissions(self, user):
        # view_permission is useless because we let people see
        # their own objects via list_permissions
        return self.list_permissions(user)

    def has_update_permissions(self, user):

        # it was created or belongs to the user.
        if self.filter(
                Q(status="draft") &
                (
                    Q(created_by=user) | Q(person__auth_user=user)
                )
            ).exists():
            return True

        if self.filter(
                status__in=['draft', 'pending', 'printed', 'approved', 'rejected']
            ).managed_by( user, ['manage_reimbursements'] ).exists():
            return True

        if self.filter( status__in=['submitted'] ).managed_by( user, ['can_approve_reimbursements'] ).exists():
            return True

        return False

    def has_remove_permissions(self, user):
        """
        Only reimbursements in draft or pending can be removed.
        """
        return self.filter(
            Q(status__in=["draft", "pending"]) & ( Q(created_by=user) | Q(person__auth_user=user) )
        )

    # =========================================================================


from torch.autograd import Variable as Vb
import torch.nn as nn
from utils.utils import *
from modules.layers.vgg import Vgg19
from torchvision import transforms as trn
from utils.ops import resample

preprocess = trn.Compose([
    trn.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])

mean = Vb(torch.FloatTensor([0.485, 0.456, 0.406])).view([1, 3, 1, 1])
std = Vb(torch.FloatTensor([0.229, 0.224, 0.225])).view([1, 3, 1, 1])

def normalize(x):
    x = (x+1) / 2
    gpu_id = x.get_device()
    return (x - mean.cuda(gpu_id))/std.cuda(gpu_id)

class PerceptualLoss(nn.Module):
    def __init__(self, train_params):
        super(PerceptualLoss, self).__init__()
        self.vgg19 = Vgg19()
        self.train_params = train_params
        self.criterion = nn.L1Loss()

    @staticmethod
    def compute_gram(x):
        b, ch, h, w = x.size()
        f = x.view(b, ch, w * h)
        f_t = f.transpose(1, 2)
        g = f.bmm(f_t) / (h * w * ch)

        return g

    def forward(self, gt, fake):
        out_dict = {}
        content_loss = 0.0
        style_loss = 0.0
        weights = [1.0, 1.0, 1.0, 1.0, 1.0]
        for i in range(self.train_params["num_predicted_frames"]):
            gt_frame = gt[:, :, i, ...]
            fake_frame = fake[:, :, i, ...]
            x_vgg, y_vgg = self.vgg19(gt_frame), self.vgg19(fake_frame)

            if self.train_params["loss_weights"].get("style", 0) > 0:
                style_loss += \
                    self.criterion(self.compute_gram(x_vgg['relu2_2'].detach()), self.compute_gram(y_vgg['relu2_2']))
                style_loss += \
                    self.criterion(self.compute_gram(x_vgg['relu3_4'].detach()), self.compute_gram(y_vgg['relu3_4']))
                style_loss += \
                    self.criterion(self.compute_gram(x_vgg['relu4_4'].detach()), self.compute_gram(y_vgg['relu4_4']))
                style_loss += \
                    self.criterion(self.compute_gram(x_vgg['relu5_2'].detach()), self.compute_gram(y_vgg['relu5_2']))

            if self.train_params["loss_weights"].get("perceptual", 0) > 0:
                content_loss += weights[0] * self.criterion(x_vgg['relu1_1'].detach(), y_vgg['relu1_1'])
                content_loss += weights[1] * self.criterion(x_vgg['relu2_1'].detach(), y_vgg['relu2_1'])
                content_loss += weights[2] * self.criterion(x_vgg['relu3_1'].detach(), y_vgg['relu3_1'])
                content_loss += weights[3] * self.criterion(x_vgg['relu4_1'].detach(), y_vgg['relu4_1'])
                content_loss += weights[4] * self.criterion(x_vgg['relu5_1'].detach(), y_vgg['relu5_1'])
        if content_loss > 0:
            out_dict["perceptual"] = content_loss / self.train_params["num_predicted_frames"]
        if style_loss > 0:
            out_dict["style"] = style_loss / self.train_params["num_predicted_frames"]
        return out_dict

class SmoothLoss(nn.Module):
    def __init__(self):
        super(SmoothLoss, self).__init__()

    @staticmethod
    def gradient_x(img):
        gx = (img[:, :, :-1, :] - img[:, :, 1:, :])
        return gx

    @staticmethod
    def gradient_y(img):
        gy = (img[:, :, :, :-1] - img[:, :, :, 1:])
        return gy

    def compute_smooth_loss(self, flow_x, img):
        flow_gradients_x = self.gradient_x(flow_x)
        flow_gradients_y = self.gradient_y(flow_x)

        image_gradients_x = self.gradient_x(img)
        image_gradients_y = self.gradient_y(img)

        weights_x = torch.exp(-torch.mean(torch.abs(image_gradients_x), 1, True))
        weights_y = torch.exp(-torch.mean(torch.abs(image_gradients_y), 1, True))

        smoothness_x = flow_gradients_x * weights_x
        smoothness_y = flow_gradients_y * weights_y

        return torch.mean(torch.abs(smoothness_x)) + torch.mean(torch.abs(smoothness_y))

    def compute_flow_smooth_loss(self, flow, img):
        smoothness = 0
        _, c, h, w = flow.size()
        # flow = torch.cat([flow[:, 0:1, :, :] * ((w - 1.0) / 2.0), flow[:, 1:2, :, :] * ((h - 1.0) / 2.0)], dim=1)
        for i in range(2):
            smoothness += self.compute_smooth_loss(flow[:, i:i+1, :, :], img)
        return smoothness/2

    def forward(self, flow, image):
        return self.compute_flow_smooth_loss(torch.cat(torch.unbind(flow, dim=2), dim=0),
                                             torch.cat(torch.unbind(image, dim=2), dim=0))

class FlowConsistLoss(nn.Module):

    def __init__(self, train_params):
        super(FlowConsistLoss, self).__init__()
        self.train_params = train_params

    @staticmethod
    def _flowconsist(flow, flowback, mask_fw=None, mask_bw=None):
        if mask_fw is not None:
            nextloss = (mask_fw * torch.abs(resample(flowback, flow) + flow)).mean()
            prevloss = (mask_bw * torch.abs(resample(flow, flowback) + flowback)).mean()
        else:
            nextloss = torch.abs(resample(flowback, flow) + flow).mean()
            prevloss = torch.abs(resample(flow, flowback) + flowback).mean()
        return prevloss + nextloss

    def forward(self, flow, flowback, mask_fw=None, mask_bw=None):
        if mask_bw is not None:
            flowcon = self._flowconsist(torch.cat(torch.unbind(flow, dim=2), dim=0),
                                        torch.cat(torch.unbind(flowback, dim=2), dim=0),
                                        mask_fw=torch.cat(torch.unbind(mask_fw, dim=2), dim=0),
                                        mask_bw=torch.cat(torch.unbind(mask_bw, dim=2), dim=0))
        else:
            flowcon = self._flowconsist(torch.cat(torch.unbind(flow, dim=2), dim=0),
                                        torch.cat(torch.unbind(flowback, dim=2), dim=0))
        return flowcon * self.train_params["num_predicted_frames"]

class KLLoss(nn.Module):
    def __init__(self):
        super(KLLoss, self).__init__()

    def forward(self, mu, logvar):
        KLD = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
        return KLD / mu.numel()

class SSIMLoss(nn.Module):
    def __init__(self):
        super(SSIMLoss, self).__init__()

    @staticmethod
    def ssim(x, y):
        c1 = 0.01 ** 2
        c2 = 0.03 ** 2

        mu_x = F.avg_pool2d(x, 3, 1)
        mu_y = F.avg_pool2d(y, 3, 1)

        sigma_x = F.avg_pool2d(x ** 2, 3, 1) - mu_x ** 2
        sigma_y = F.avg_pool2d(y ** 2, 3, 1) - mu_y ** 2
        sigma_xy = F.avg_pool2d(x * y, 3, 1) - mu_x * mu_y

        ssim_n = (2 * mu_x * mu_y + c1) * (2 * sigma_xy + c2)
        ssim_d = (mu_x ** 2 + mu_y ** 2 + c1) * (sigma_x + sigma_y + c2)

        ssim = ssim_n / ssim_d

        return torch.clamp((1 - ssim) / 2, 0, 1).mean()

    def forward(self, x, y):
        sim = self.ssim(torch.cat(torch.unbind(x, dim=2), dim=0), torch.cat(torch.unbind(y, dim=2), dim=0))
        return sim

class L1MaskedLoss(nn.Module):
    def __init__(self):
        super(L1MaskedLoss, self).__init__()

    def forward(self, source, target, mask=None):
        if mask is not None:
            mask = mask.expand_as(source)
            return F.l1_loss(source * mask, target * mask)
        else:
            return F.l1_loss(source, target)

class TrainingLosses(nn.Module):
    def __init__(self, train_params, model_params):
        super(TrainingLosses, self).__init__()
        self.train_params = train_params
        self.model_params = model_params
        if self.train_params["loss_weights"]["perceptual"] > 0:
            self.perceptual_loss = PerceptualLoss(train_params)
        self.flow_consist = FlowConsistLoss(self.train_params)
        self.smooth_loss = SmoothLoss()
        self.kl_loss = KLLoss()
        self.ssim_loss = SSIMLoss()
        self.l1_masked_loss = L1MaskedLoss()

    def forward(self, data, frames, bw_optical_flows, fw_optical_flows, bw_occlusion_masks, fw_occlusion_masks,
                generated, tracking_gnn):
        loss_dict = {}
        source_frame = frames[:, :, self.train_params["num_input_frames"] - 1, ...]
        target_frames = frames[:, :, self.train_params["num_input_frames"]:, ...]
        '''flowloss'''

        loss_dict["flow_reconstruction"] = self.l1_masked_loss(generated["dense_motion_bw"], bw_optical_flows,
                                                               bw_occlusion_masks)
        if fw_optical_flows is not None:
            loss_dict["flow_reconstruction"] += self.l1_masked_loss(generated["dense_motion_fw"],
                                                                    fw_optical_flows, fw_occlusion_masks)
            loss_dict["flowcon"] = self.flow_consist(generated["dense_motion_fw"], generated["dense_motion_bw"],
                                                     generated["occlusion_fw"], generated["occlusion_bw"])
        warped_frames = torch.cat([torch.unsqueeze(resample(source_frame,
                                                            generated["dense_motion_bw"][:, :, i, ...]),
                                                   2) for i in range(self.train_params["num_predicted_frames"])], 2)
        loss_dict["warped"] = self.l1_masked_loss(warped_frames, target_frames)
        if self.train_params["loss_weights"]["flow_smooth"] > 0:
            loss_dict["flow_smooth"] = self.smooth_loss(generated["dense_motion_bw"], target_frames)
            if fw_optical_flows is not None:
                loss_dict["flow_smooth"] += self.smooth_loss(generated["dense_motion_fw"],
                                                   torch.unsqueeze(source_frame, 2).repeat(1, 1, target_frames.shape[2],
                                                                                1, 1))
        '''kldloss'''
        loss_dict["kl"] = self.kl_loss(generated["mu"], generated["logvar"])
        '''Image Similarity loss'''
        loss_dict["ssim"] = self.ssim_loss(generated["generated"], target_frames)
        loss_dict["reconstruction"] = self.l1_masked_loss(generated["generated"], target_frames)
        '''vgg loss'''
        loss_dict.update(self.perceptual_loss(target_frames,
                                              generated["generated"]))
        '''mask loss'''
        loss_dict["occlusion_bw"] = self.l1_masked_loss(bw_occlusion_masks, generated["occlusion_bw"])
        if fw_optical_flows is not None:
            loss_dict["occlusion_fw"] = self.l1_masked_loss(fw_occlusion_masks, generated["occlusion_fw"])
        scale = 0
        rotation = 0
        translation = 0
        for t in range(self.train_params["num_predicted_frames"]):
            translation += self.l1_masked_loss(generated[f"theta_{t}"][:, 2], tracking_gnn.targets_theta[:, t][:, 2])
            translation += self.l1_masked_loss(generated[f"theta_{t}"][:, 5], tracking_gnn.targets_theta[:, t][:, 5])
            scale += self.l1_masked_loss(generated[f"theta_{t}"][:, 0], tracking_gnn.targets_theta[:, t][:, 0])
            scale += self.l1_masked_loss(generated[f"theta_{t}"][:, 4], tracking_gnn.targets_theta[:, t][:, 4])
            rotation += self.l1_masked_loss(generated[f"theta_{t}"][:, 1], tracking_gnn.targets_theta[:, t][:, 1])
            rotation += self.l1_masked_loss(generated[f"theta_{t}"][:, 3], tracking_gnn.targets_theta[:, t][:, 3])
        loss_dict["translation"] = loss_dict.get("translation", 0) + isnan(translation)
        loss_dict["scale"] = loss_dict.get("scale", 0) + isnan(scale)
        loss_dict["rotation"] = loss_dict.get("rotation", 0) + isnan(rotation)

        return loss_dict


import nltk

"""list of words to remove meaningless words"""

words = set(nltk.corpus.words.words())

def meaningful_check(word):
    word = word.lower()
    word_length = len(word.split(" "))
    if((word in words and word_length==1) or word_length>1):
        return True
    else:
        return False


import pygame
import numpy as np
from random import *
from pygame.draw import *

pygame.init()

FPS = 10
screen = pygame.display.set_mode((450, 650))

def clouds(surface, n):
    """
    Draws n pairs of dark and light transparent clouds

    :param surface: Surface to draw clouds on
    :param n: Number of pairs of clouds to draw

    """
    for i in range(n):
        # TODO: blur clouds
        LIGHT = (255, 255, 255, randint(10, 120))
        DARK  = ( 37,  37,  37, randint(10, 150))
        ellipse(surface, LIGHT, (randint(-100,350), randint(-100, 275), randint(100,500), randint(10,30)))
        ellipse(surface,  DARK, (randint(-100,350), randint(-100, 275), randint(100,500), randint(10,30)))

def ellipse_series(surface, color, x_left, y_top, width, height, scale = 1):
    """
    Draws a sequence of ellipses with given colors and positions on the same surface

    :param surface: Surface to draw ellipes on
    :param x_left:  Array of the X coordinates of the leftmost points of the ellipses
    :param y_top:   Array of the Y coordinates of the highest points of the ellipses
    :param width:   Width array
    :param height:  Height array
    :param scale:   Scale factor of each ellipse

    """
    for c, x, y, w, h in zip(color, x_left, y_top, width, height):
        ellipse(surface, c, (x * scale, y * scale, w * scale, h * scale))

def spaceship(x, y, scale = 1):
    """
    Draws spaceship from the point (x, y)

    :param x: X coordinate of a top left corner of a drawing surface
    :param y: Y coordinate of a top left corner of a drawing surface
    :param scale: Scale of the drawing

    """

    # Main surface
    spaceship_surface = pygame.Surface((100 * scale, 100 * scale), pygame.SRCALPHA)

    # Colors
    WHITE      = (255, 255, 255)
    LIGHT      = (200, 200, 200, 150)
    LIGHT_GRAY = (200, 200, 200)
    DARK_GRAY  = (100, 100, 100)

    # Trianglular transparent ray of the spaceship
    vertices = [(10, 90), (50, 20), (90, 90)]
    polygon(spaceship_surface, LIGHT, [(x * scale, y * scale) for x, y in vertices])

    # Fuselage shape
    color  = [DARK_GRAY, LIGHT_GRAY]
    x_left = [10,        20]
    y_top  = [0,         0]
    width  = [90,        70]
    height = [30,        20]
    ellipse_series(spaceship_surface, color, x_left, y_top, width, height, scale)

    # Rivets on the fuselage
    color  = [WHITE] * 6
    x_left = [15, 28, 44, 87, 74, 58]
    y_top  = [13, 19, 22, 13, 19, 22]
    width  = [8] * 6
    height = [6] * 6
    ellipse_series(spaceship_surface, color, x_left, y_top, width, height, scale)

    # Adding surface to the drawing
    screen.blit(spaceship_surface, (x,y))

def alien(x, y, scale = 1, flip = False):
    """
    Draws a scalable alien which can be mirrored along Y axis

    :param flip: True if alien should be mirrored, False else
    :param x: X coordinate of the top left corner of the drawing surface
    :param y: Y coordinate or the top left corner of the drawing surface
    :param k: Scale of the alien

    """

    # Main surface
    alien = pygame.Surface((100 * scale, 200 * scale), pygame.SRCALPHA)

    # Colors
    SKIN  = (255, 255, 200)
    WHITE = (255, 255, 255)
    BLACK = (  0,   0,   0)
    RED   = (255,   0,   0)
    GREEN = ( 50, 170,   0)
    BROWN = ( 64,   4,   4)

    # Body and legs
    color  = [SKIN] * 7
    x_left = [24,  15,  40,  12,  42,   0,  47]
    y_top  = [95, 136, 145, 155, 165, 172, 182]
    width  = [25,  16,  16,  10,  10,  16,  16]
    height = [65,  25,  25,  25,  25,  16,  16]
    ellipse_series(alien, color, x_left, y_top, width, height, scale)

    # Hands
    color  = [SKIN] * 6
    x_left = [  7,  50,   7,  62, 12, 42]
    y_top  = [110, 110, 120, 110, 95, 97]
    width  = [ 12,  12,   7,  20, 16, 16]
    height = [ 10,  10,   9,   8, 16, 16]
    ellipse_series(alien, color, x_left, y_top, width, height, scale)

    # Horns; first 4 ellipses are the left horn, last 5 are another one
    color  = [SKIN] * 9
    x_left = [0,  5,  10, 15, 81, 73, 63, 52, 55]
    y_top  = [37, 47, 53, 60, 45, 45, 48, 60, 58]
    width  = [15, 10,  8,  5, 12,  7,  8,  6,  8]
    height = [10,  7,  9,  9, 15,  6,  8,  8,  4]
    ellipse_series(alien, color, x_left, y_top, width, height, scale)

    # Head
    color = [SKIN] * 6
    x_left = [12, 47, 15, 20, 25, 30]
    y_top  = [67, 67, 65, 75, 80, 90]
    width  = [16, 16, 50, 40, 30, 20]
    height = [16, 16, 15, 15, 15, 20]
    ellipse_series(alien, color, x_left, y_top, width, height, scale)

    # Eyes
    color    = [BLACK, BLACK, WHITE, WHITE]
    x_center = [30, 50, 33, 52]
    y_center = [76, 77, 79, 79]
    radius   = [7,   6,  2,  2]
    for c, _x, _y, r in zip(color, x_center, y_center, radius):
        # names _x and _y are chosen so not to confuse with params
        circle(alien, c, (_x * scale, _y * scale), r * scale)

    # Apple
    color  = [RED, GREEN]
    x_left = [72, 82]
    y_top  = [92, 77]
    width  = [20,  3]
    height = [20, 10]
    ellipse_series(alien, color, x_left, y_top, width, height, scale)

    pygame.draw.arc(alien, BROWN,
        (77 * scale, 87 * scale, 10 * scale, 10 * scale),
        -np.pi / 8, 5 * np.pi / 8, 2)

    # Mirroring the alien if needed
    if flip:
        alien = pygame.transform.flip(alien, True, False)

    # Adding surface to the drawing
    screen.blit(alien, (x,y))

def background():
    """
    Draws background with clouds

    """
    # Background
    SKY    = ( 0, 34, 43)
    GROUND = (34, 43,  0)
    rect(screen, GROUND, (0, 325, 450, 325))
    rect(screen,    SKY, (0,   0, 450, 325))
    circle(screen, (240, 240, 230), (300, 150), 60)

    # Clouds
    clouds_surface = pygame.Surface((450, 650), pygame.SRCALPHA)
    clouds(clouds_surface, 20)
    screen.blit(clouds_surface, (0, 0))

def picture():
    """
    Draws spaceships, aliens and the background with clouds

    """
    background()

    spaceship(200, 300, scale = 0.5)
    spaceship(300, 280, scale = 1)
    spaceship(  0, 225, scale = 2)

    alien(100, 280, scale = 0.4, flip = True)
    alien( 50, 330, scale = 0.4, flip = True)
    alien(140, 330, scale = 0.4)
    alien(100, 380, scale = 0.8, flip = True)
    alien(250, 320, scale = 1.5)

picture()

pygame.display.update()
clock = pygame.time.Clock()
finished = False

while not finished:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            finished = True
    pygame.display.update()

pygame.quit()


import asyncio
import datetime
import pandas as pd
from collections import defaultdict
from urllib.parse import urlencode
import json
from pandas import DataFrame
from Util.RequestUtil import get_html
from config import ALL_CODE

class BaiduIndex:
    def __init__(self, startdate, enddate, city, key_word):
        self.start_date = datetime.datetime.strptime(startdate, '%Y-%m-%d')
        self.end_date = datetime.datetime.strptime(enddate, '%Y-%m-%d')
        self.area = ALL_CODE.get(city, 0)
        self.key_words = key_word
        self.result = {keyword: defaultdict(list) for keyword in self.key_words}
        self._all_cate = ['all', 'pc', 'wise']
        self._headers = {
            'Cookie': ''
        }

    async def gen_date(self, start, end):
        """

        :param start:
        :param end:
        :return:
        """
        paramter = {
            'startDate': start,
            'endDate': end,
            'area': self.area,
            'word': self.key_words,

        }
        url = 'http://index.baidu.com/api/SearchApi/index?' + urlencode(paramter)
        html = await get_html(url, self._headers)
        datas = json.loads(html)
        uniqueid = datas['data']['uniqid']
        encrypt_datas = []
        for single_data in datas['data']['userIndexes']:
            encrypt_datas.append(single_data)
        return encrypt_datas, uniqueid

    async def gen_key(self, uniqueid):
        """

        :param uniqueid:
        :return:
        """
        url = 'http://index.baidu.com/Interface/api/ptbk?uniqid=%s' % uniqueid
        html = await get_html(url, header=self._headers)
        datas = json.loads(html)
        return datas['data']

    def trans_data(self, key, data):
        """

        :param key:
        :param data:
        :return:
        """
        a = key
        i = data
        n = {}
        s = []
        for o in range(len(a) // 2):
            n[a[o]] = a[len(a) // 2 + o]
        for r in range(len(data)):
            s.append(n[i[r]])
        return ''.join(s).split(',')

    def parse_save_data(self, data):
        """
        转化保存数据
        """
        data_dic_reshape = {}
        for cate in self._all_cate:
            data_dic_reshape[cate] = data[cate]['data']
        time_series = pd.date_range(start=self.start_date, end=self.end_date)
        data_df = DataFrame(data_dic_reshape, index=time_series)
        csv_name = './{}_指数_{}--{}.csv'.format(self.key_words, self.start_date.date(), self.end_date.date())
        data_df.to_csv(path_or_buf=csv_name, mode='w+')

    async def main(self):
        encrypt_datas, uniqid = await self.gen_date(self.start_date, self.end_date)
        key = await self.gen_key(uniqid)
        for encrypt_data in encrypt_datas:
            for kind in self._all_cate:
                encrypt_data[kind]['data'] = self.trans_data(key, encrypt_data[kind]['data'])
            self.parse_save_data(encrypt_data)

if __name__ == '__main__':
    baidu = BaiduIndex('2018-4-20', '2018-10-20', '', '王俊凯')
    loop = asyncio.get_event_loop()
    loop.run_until_complete(baidu.main())


import argparse
import pytest
from src.dataset import create_dataset

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='test_module')
    parser.add_argument('--batch_size', default=64, type=int, metavar='N', help='train batchsize (default: 256)')
    parser.add_argument('--target', type=str, default='GPU', choices=['GPU', 'Ascend'])
    parser.add_argument('--data_url', default='data path', type=str)
    args = parser.parse_args()

    train_dataset = create_dataset(dataset_path=args.data_url, do_train=True,
                                       repeat_num=1, batch_size=args.batch_size, target=args.target)
    assert train_dataset.get_batch_size() == args.batch_size


import cv2

def processImage(image):
    return image

# get the image from the video camera
capture = cv2.VideoCapture(0)

while True:
    ret, frame = capture.read()
    result = processImage(frame)
    cv2.imshow('Whiteboard Viewer', result)
    if cv2.waitKey(1) == 27: # Escape
        break

# release resources
capture.release()
cv2.destroyAllWindows()


__author__ = 'junwang'
from geopy.geocoders import Nominatim
import csv
# creat a list to hold address
address = []
coordinate = []
listOfRow = []
geolocator = Nominatim()
with open('DOF__Condominium_Comparable_Rental_Income___Manhattan___FY_2009_2010.csv', 'r') as f:
    reader = csv.DictReader(f)
    for row in reader:
        address.append(row['MANHATTAN CONDOMINIUM PROPERTY Address'] + ' NY')

for ad in address:
    location = geolocator.geocode(ad)
    coordinate.append(str(location.latitude) + '，' + str(location.longitude))
with open('DOF__Condominium_Comparable_Rental_Income___Manhattan___FY_2009_2010.csv', 'r') as f2:
    reader = csv.reader(f2)
    for row2 in reader:
        listOfRow.append(row2)
    listOfRow[0].append('Coordinate')
    for i in range(0, len(coordinate)):
        listOfRow[i + 1].append(coordinate[i])
with open('DOF__Condominium_Comparable_Rental_Income___Manhattan___FY_2009_2010_With_Coordinate.csv', 'w') as w:
    writer = csv.writer(w, delimiter=',')
    writer.writerows(listOfRow)


#!/usr/bin/python3
"""
Script that takes in a letter, sends a POST request to a specified
URL with the letter as a parameter, and handles the response
"""

from sys import argv
import requests

if __name__ == "__main__":

    url = "http://0.0.0.0:5000/search_user"
    if len(argv) > 1:
        q = {'q': argv[1]}
    else:
        q = {'q': ''}
    res = requests.post(url, data=q)
    try:
        body = res.json()
        if len(body) == 0:
            print("No result")
        else:
            print("[{}] {}".format(body["id"], body["name"]))
    except ValueError:
        print("Not a valid JSON")


from ... rest import Rest
from ... rest import api_register
from ... rest import api_callback
from .. utils import get_parent_dn
from . import ManagedObject

import logging

# module level logging
logger = logging.getLogger(__name__)

@api_register(parent="fabric", path="mo/mgmtRsMgmtBD")
class mgmtRsMgmtBD(ManagedObject):

    META_ACCESS = ManagedObject.append_meta_access({
        "namespace":"mgmtRsMgmtBD",
    })

    META = ManagedObject.append_meta({
        "tDn": {},
        "parent": {
            "type": str,
            "description": "parent mgmtInB dn for this object"
        },
    })

    @staticmethod
    @api_callback("before_create")
    def before_create(data):
        """ set parent to mgmtInB based on dn """
        data["parent"] = get_parent_dn(data["dn"])
        return data


#! /usr/bin/env python

import os
from flask import Flask, flash, render_template

app = Flask(__name__)

PORT = int(os.environ.get("PORT", '5000'))
DEBUG = os.environ.get('DEBUG', True)

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

app.debug = DEBUG
app.run(host='0.0.0.0', port=PORT)