from django.utils import six

from django.contrib.auth.models import User
from django.core import serializers
from django.http import JsonResponse
from django.shortcuts import render

from authsys.models import UserProfile
from orders.forms import CheckoutForm
from orders.models import ProductInBascket, ProductInOrder, Order

def bascket_adding(request):
    if not request.session.exists(request.session.session_key):
        request.session.create()
    session_key = request.session.session_key

    data = request.POST
    product_id = data.get('product_id')
    num = data.get('quantity')
    product_name = data.get('product_name')
    print(product_name, product_id , num , data.get('is_delete')== 'false')
    if data.get('is_delete') == 'true':

        ProductInBascket.objects.filter(id=product_id).update(is_active=False)
    else:

        new_product, created = ProductInBascket.objects.get_or_create(session_key=session_key, product_id=product_id, defaults={'number_of_products' : num});
        if not created:
            new_product.number_of_products += int(num)
            new_product.is_active = True
            new_product.save(force_update=True)
    return_dict = dict()
    total_prod_items = ProductInBascket.objects.filter(session_key=session_key, is_active=True)
    total_prod_num = total_prod_items.count()
    list_products = [{'price' : item.product.price, 'name' : item.product.name , 'quant' : item.number_of_products,'id' : item.id}
                     for item in total_prod_items]
    return_dict['total_prod_num'] = total_prod_num
    return_dict['total_prod_items'] = list_products
    return JsonResponse(return_dict)

def checkout(request):
    if not request.session.exists(request.session.session_key):
        request.session.create()
    session_key = request.session.session_key
    if request.method == "POST":
        form = CheckoutForm(request.POST or None)
        print(request.POST)
        if form.is_valid():

            product_queryset = dict(request.POST)
            name =product_queryset['name'][0]
            phone = product_queryset['phone'][0]
            adress = product_queryset['adress'][0]
            product_dict = {}
            for k, v in product_queryset.items():
                if k.isdigit():

                   product_dict[k] = v[0]

            user, created = User.objects.get_or_create(username=phone, defaults={ 'first_name': name })
            if created:
                user_profile = UserProfile.objects.create(user_id=user.id, adress=adress)

            order = Order.objects.create(user=user , customer_name=name , customer_phone=phone, status_order_id=3)
            for k , v in product_dict.items():

                product_in_bascket = ProductInBascket.objects.get(id=k)
                product_in_bascket.number_of_products = v
                product_in_bascket.order = order
                product_in_bascket.save(force_update=True)
                ProductInOrder.objects.create(product=product_in_bascket.product,  number_of_products=int(v),
                                              total_price=product_in_bascket.total_price, order=order)

        return JsonResponse({'bar' : 'foo'})
    products_in_bascket = ProductInBascket.objects.filter(session_key=session_key, is_active=True, order__isnull=True)
    total_price = 0
    for item in products_in_bascket:
        total_price+=item.total_price

    return render(request, 'orders/checkout.html', locals())


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
#==============================================================================
# SCRIPT TO VERIFY DATE FOR TIME SERIES
#==============================================================================

This script will verify date for time series and will filter when same date
are wrong (e.g. when a 2017 year is between 2016).

@author: fncsobral
@date  : 05/dec/2016
@mod1  : 16/jan/2017 - Arranging UTC time, and improving date filter.
@mod2  : 17/jan/2017 - Arranging index problem with variables DataFrame
#==============================================================================
"""

from datetime import datetime as dtm
import numpy as np
import time
import calendar

#==============================================================================
#
#==============================================================================
def ver_continuity(var_dtfr):
    '''
    Use the DataFrame of a specific data
    to get all the time information, generate a
    EPOCH date and then will organize the time
    sorted ascending, to verify if date is in
    the correct place.

    RETURN:
        - epc_smpl: dictionary that contains all the period of sampling. This
        group will be used to verify the amount of samples captured in that
        hour in GETTING_DATA.PY script.

    '''

    # Getting all the fields information to build epoch date.
    # 150000 is the visual limit that I think sensors were took off the water.
    nms   = ['YEAR', 'MONTH', 'DAY', 'HOUR', 'MIN', 'SEC']
    YEAR  = var_dtfr[nms[0]][:150000]
    MONTH = var_dtfr[nms[1]][:150000]
    DAY   = var_dtfr[nms[2]][:150000]
    HOUR  = var_dtfr[nms[3]][:150000]
    MIN   = var_dtfr[nms[4]][:150000]
    SEC   = var_dtfr[nms[5]][:150000]

    # Cutting all VAR_DTFR
    var_dtfr = var_dtfr[:150000]

    # Building epoch date vector.
    epc = list()
    for yy, mm, dd, HH, MM, SS in zip(YEAR, MONTH, DAY, HOUR, MIN, SEC):
        epc.append(calendar.timegm(dtm(yy, mm, dd, HH, MM, SS).timetuple()))

    # First evaluation, verifying if data is in the right position. If not,
    # data will be discarted. Better evaluation about data veracity just can be
    # made using some kind of metadata, what I don't have now and I expected
    # spreadsheet to be corrected offered to the users...
    idx = np.argsort(epc)

    # This part verifies if the time is placed at the right position. If not, it
    # is removed and another index verification will happen.
    TT = True
    while TT == True:
#        print('Idx length is:', len(idx))

        for ii, kk in enumerate(idx):
            if kk != ii:
#                print('Index ii is at wrong place at the original series.',
#                      'Indice ii = ', ii, ' Indice kk = ', kk)
                # Removing the element that is the wrong place.
                epc = np.delete(epc, ii)
                idx = np.argsort(epc)

                # Removing from VAR_DTFR
                var_dtfr = var_dtfr.drop(var_dtfr.index[ii])
                break

            elif ii == len(idx) - 1:
                print('LAST verification...')
                TT = False

            else:
                pass

    # Getting epoch sorted and with repeated numbers cut off. Sometimes at
    # time sampling, there are more than one data for the same time, and
    # those are removed here.   [NÃO SEI SE MANTENHO ISSO....17/JAN/2017]
    new_epc      = np.unique(epc)
    new_var_dtfr = var_dtfr.drop_duplicates(['YEAR', 'MONTH', 'DAY', 'HOUR', 'MIN', 'SEC'])

    # Current time: to cut date not realistic with the actual date (2017 date in
    # 2016 year). In epoch date.
    cur_time = time.mktime(time.localtime())

    # ----------------- List to stores index to be removed ------------------ #
    # Epoch date that represent future date (still not achieved in real life calendar)
    epc_future = list()

    # Epoch date that sampling does not started in HH:01:30
    epc_start  = list()

    # Dictionary that will contain the samples for each hour.
    epc_smpl   = dict()

    # List that stores the samples for each hour.
    tmsmpl     = list()

    # Loop over epc ascending sorted.
    for ii, ee in enumerate(new_epc):

        # Getting information of "ee", which is the iteration for all epoch
        # dates of sampling (already sorted as ascend and without repeated numbers)
        yy = dtm.utcfromtimestamp(ee).year
        mm = dtm.utcfromtimestamp(ee).month
        dd = dtm.utcfromtimestamp(ee).day
        HH = dtm.utcfromtimestamp(ee).hour

        # EPC_REF is the mark for the first moment of sampling, that is, HH:01:30
        # for each HH; the reference.
        epc_ref = calendar.timegm(dtm(yy, mm, dd, HH, 1, 30).timetuple())

        # If time is greater than localtime, will be removed.
        if ee > cur_time:
            # Storing index remove value later.
            epc_future.append(ii)

            # Removing index ii from DATAFRAME VAR_DTFR
            new_var_dtfr = new_var_dtfr.drop(new_var_dtfr.index[ii])

            print('Date out of range, need to be removed ', ii, dtm.fromtimestamp(ee))

        # Data between expected time sample 01m30s and 1m49s
        elif ee - epc_ref >= 0 and ee - epc_ref <= 19:
            tmsmpl.append(ii)
#            print('Time sample within the range of 30s and 49s.')

            # Verifying the next time. If it changed of HH (difference will be
            # greater than 19s), the appended values will be stored in dictionary
            # EPC_SMPL
            # Verifying if ii index will not exceed the NEW_EPC length.
            if ii+1 <= len(new_epc)-1:
                # GG is epoch date
                gg = new_epc[ii + 1]

                # If difference greater than 19 sec, store the epoch appended and
                # initiate another TMSMPL storing for the next hours.
                if gg - epc_ref > 19:
                    epc_smpl[str(epc_ref)] = tmsmpl
                    tmsmpl = list()
            else:
                pass

        # This part will get times that are within the time period of sampling
        # but started in other moment instead 1m30s.
        # The values are the indexes refered to NEW_EPC.
        else:
            epc_start.append(ii)

            # Removing index ii from DATAFRAME VAR_DTFR
            new_var_dtfr = new_var_dtfr.drop(new_var_dtfr.index[ii])

            print('Started in other time than 1m30s: ', dtm.utcfromtimestamp(ee))

    # Storing EPC_FUTURE and EPC_START inside EPC_SMPL
    epc_smpl['epc_start' ] = epc_start
    epc_smpl['epc_future'] = epc_future

    ###
    # Return dictionary with time samples.
    return epc_smpl, new_var_dtfr

#==============================================================================
# END
#==============================================================================


"""
Flat text button GUI element.
"""

from typing import Union, Tuple

import arcade
from arcade.arcade_types import Color
from .button_abstract import ButtonAbstract

class ClickableText(ButtonAbstract):
    def __init__(self, text, center_x, center_y,
                 text_color: Color,
                 text_color_mouse_over: Color = None,
                 text_color_mouse_press: Color = None,
                 font_size: float = 12,
                 font_name: Union[str, Tuple[str, ...]] = ('calibri', 'arial'),
                 ):
        super().__init__(center_x, center_y)

        if text_color_mouse_over is None:
            text_color_mouse_over = text_color

        if text_color_mouse_press is None:
            text_color_mouse_press = text_color

        text_image_normal = arcade.get_text_image(text=text, text_color=text_color, font_size=font_size, font_name=font_name)
        text_image_mouse_over = arcade.get_text_image(text=text, text_color=text_color_mouse_over, font_size=font_size, font_name=font_name)
        text_image_mouse_press = arcade.get_text_image(text=text, text_color=text_color_mouse_press, font_size=font_size, font_name=font_name)

        self.texture = arcade.Texture(image=text_image_normal, name=text)
        self.normal_texture = self.texture
        self.mouse_press_texture = arcade.Texture(image=text_image_mouse_press, name=text+"3")
        self.mouse_over_texture = arcade.Texture(image=text_image_mouse_over, name=text+"6")


import os
import json
import PySide.QtGui as QtGui
from PySide.QtCore import Signal

class JobWidget(QtGui.QWidget):

    rendererChanged = Signal(int)

    def __init__(self, renderer):
        super(JobWidget, self).__init__()
        self.setLayout(QtGui.QVBoxLayout())
        jobBox = QtGui.QGroupBox('Job Set')
        jobBoxLayout = QtGui.QGridLayout()
        jobBox.setLayout(jobBoxLayout)
        self.layout().addWidget(jobBox)
        self.layout().setContentsMargins(0, 0, 0, 0)
        jobBoxLayout.addWidget(QtGui.QLabel('Renderer:'), 0, 0)
        self.rendererBox = QtGui.QComboBox()
        self.setRendererDrop(renderer)
        self.rendererBox.currentIndexChanged.connect(self.emitRendererChangedSignal)
        jobBoxLayout.addWidget(self.rendererBox, 0, 1)
        jobBoxLayout.addWidget(QtGui.QLabel('Priority:'), 1, 0)
        self.priorityBox = QtGui.QSpinBox()
        self.priorityBox.setMinimum(1)
        self.priorityBox.setMaximum(10)
        self.priorityBox.setValue(5)
        self.priorityBox.setMaximumWidth(50)
        jobBoxLayout.addWidget(self.priorityBox, 1, 1)
        jobBoxLayout.addWidget(QtGui.QLabel('Dependent Job:'), 1, 2)
        self.dependJobLineEdit = QtGui.QLineEdit()
        jobBoxLayout.addWidget(self.dependJobLineEdit, 1, 3)
        self.dependJobLineEdit.setText('')
        jobBoxLayout.addWidget(QtGui.QLabel('Frame Split Mode:'), 2, 0)
        self.splitmodeDrop = QtGui.QComboBox()
        self.setSplitModeDrop()
        self.splitmodeDrop.activated[int].connect(self.splitModeSelected)
        jobBoxLayout.addWidget(self.splitmodeDrop, 2, 1)
        hlayout = QtGui.QHBoxLayout()
        hlayout.addItem(QtGui.QSpacerItem(10, 10, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum))
        hlayout.addWidget(QtGui.QLabel('Count:'))
        jobBoxLayout.addLayout(hlayout, 2, 2)
        self.countBox = QtGui.QSpinBox()
        self.countBox.setValue(5)
        jobBoxLayout.addWidget(self.countBox, 2, 3)
        self.countLabel = QtGui.QLabel('frames/chunk')
        jobBoxLayout.addWidget(self.countLabel, 2, 4)
        jobBoxLayout.addWidget(QtGui.QLabel('Client Pool:'), 3, 0)
        self.poolDrop = QtGui.QComboBox()
        self.setPoolsDrop()
        jobBoxLayout.addWidget(self.poolDrop, 3, 1)

    def emitRendererChangedSignal(self):
        self.rendererChanged.emit(int(self.rendererBox.currentIndex()))

    def setRendererDrop(self, renderer):
        if os.path.exists(os.environ['SHOT_SUBMIT_CONFIG']):
            jd = open(os.environ['SHOT_SUBMIT_CONFIG']).read()
            data = json.loads(jd)
            options = data['Renderers'][renderer]
            for option in options:
                self.rendererBox.addItem(option)

    def setPoolsDrop(self):
        if os.path.exists(os.environ['SHOT_SUBMIT_CONFIG']):
            jd = open(os.environ['SHOT_SUBMIT_CONFIG']).read()
            data = json.loads(jd)
            options = data['Pools']
            for option in options:
                self.poolDrop.addItem(option)

    def setSplitModeDrop(self):
        self.splitmodeDrop.addItem('No Splitting')
        self.splitmodeDrop.addItem('Split into X total pieces')
        self.splitmodeDrop.addItem('Split into X frames per chunk')
        self.splitmodeDrop.setCurrentIndex(2)

    def splitModeSelected(self, index):
        if index == 0:
            self.countLabel.setText('')
            self.countBox.setValue(0)
            self.countBox.setEnabled(False)
        elif index == 1:
            self.countBox.setEnabled(True)
            self.countBox.setValue(2)
            self.countLabel.setText('total pieces')
        else:
            self.countBox.setEnabled(True)
            self.countBox.setValue(5)
            self.countLabel.setText('frames/chunk')

    def getRenderer(self):
        return str(self.rendererBox.currentText())

    def getClientPools(self):
        return str(self.poolDrop.currentText())

    def getSplitMode(self):
        splitMode = str(self.splitmodeDrop.currentIndex())
        count = str(self.countBox.text())
        return '%s,%s' % (splitMode, count)

    def getDependentJob(self):
        return str(self.dependJobLineEdit.text())


import os.path
import docker
DOCKER_CLIENT = docker.from_env()
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))  # this is where the constants.py file should be
DOCKER_IMAGE_PATH = ROOT_PATH
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
MIN_SUBMISSION_INTERVAL_SECONDS = 10  # the minimum time a user must wait between submissions
MAX_TEST_RUN_SECONDS = 5  # the maximum time a Submission can run
TESTS_FOLDER_NAME = 'challenge_tests'  # the name of the folder which holds tests for challenges

CHALLENGES_APP_FOLDER_NAME = 'challenges'
GRADER_FILE_NAME = 'grader.py'  # the file which is sent over to the docker container and executed there

BASE_USER_ROLE_NAME = 'User'

RUSTLANG_NAME = 'Rust'
PYTHONLANG_NAME = 'Python'
CPPLANG_NAME = 'C++'
GOLANG_NAME = 'Go'
KOTLIN_NAME = 'Kotlin'
RUBY_NAME = 'Ruby'

SUBMISSION_MINIMUM_TIMED_OUT_PERCENTAGE = 40  # the minimum percentage of timed out tests a submission needs to have for the whole submission to be considered 'timed_out'

# Keys for the object returned by the grader's AsyncResult function
GRADER_TEST_RESULTS_RESULTS_KEY = 'results'
GRADER_TEST_RESULT_SUCCESS_KEY = 'success'
GRADER_TEST_RESULT_TIME_KEY = 'elapsed_seconds'
GRADER_TEST_RESULT_DESCRIPTION_KEY = 'description'
GRADER_TEST_RESULT_TRACEBACK_KEY = 'traceback'
GRADER_TEST_RESULT_ERROR_MESSAGE_KEY = 'error_message'
GRADER_COMPILE_FAILURE = 'COMPILATION FAILED'
GRADER_TEST_RESULT_TIMED_OUT_KEY = 'timed_out'

RUSTLANG_TIMEOUT_SECONDS = 5
RUSTLANG_ERROR_MESSAGE_SNIPPET = 'error: aborting due to previous error'
RUSTLANG_ERROR_MESSAGE_SNIPPET_2 = 'error: incorrect close delimiter'
RUSTLANG_UNFRIENDLY_ERROR_MESSAGE = "note: Run with `RUST_BACKTRACE=1`"  # error message that is of no interest to the user
RUSTLANG_FILE_EXTENSION = '.rs'
RUSTLANG_COMPILE_ARGS = ['rustc']
RUSTLANG_RUN_ARGS = []

CPP_TIMEOUT_SECONDS = 4
CPP_FILE_EXTENSION = '.cpp'
CPP_COMPILE_ARGS = ['g++', '-std=c++11', '-o']
CPP_RUN_ARGS = []

PYTHON_TIMEOUT_SECONDS = 5
PYTHON_FILE_EXTENSION = '.py'
PYTHON_RUN_COMMAND = 'python3'

RUBY_TIMEOUT_SECONDS = 5
RUBY_FILE_EXTENSION = '.rb'
RUBY_RUN_COMMAND = 'ruby'

GO_TIMEOUT_SECONDS = 4
GO_FILE_EXTENSION = '.go'
GO_COMPILE_ARGS = ['go', 'build']
GO_RUN_ARGS = []

KOTLIN_TIMEOUT_SECONDS = 4
KOTLIN_COMPILE_ARGS = ['kotlinc']
KOTLIN_FILE_EXTENSION = '.kt'
KOTLIN_RUN_ARGS = ['java', '-jar']


import pandas as pd
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import seaborn as sns

# Read data
df = pd.read_csv(r'Data/SC_expression.csv')
# Now treatments are rows and genes are columns

# Remove gene names row
#df = df.drop(['Unnamed: 0'])
#df = df.set_index('IFFABF').transpose()
df = df.set_index('Genes')
cols = list(df.columns)
treatments = []
for col in cols:
  treatments.append(col[:3])
df = df.T
# PCA magic
pca = PCA(n_components=2, svd_solver='full')
pca.fit(df)
T = pca.transform(df)
pc_df = pd.DataFrame(data=T, columns=['PC1', 'PC2'])
pc_df = pc_df.assign(treatments=treatments)
ax = sns.lmplot(x="PC1", y="PC2",
  data=pc_df,
  fit_reg=False,
  hue='treatments',
  legend=True,
  scatter_kws={"s": 80}) # specify the point size

print(pc_df)
plt.show()


# This is to find a relation between cluster mass and the average redshift at a given angular radii
# (c) mattchelldavis

from astropy.io import fits
import numpy as np

import matplotlib as mpl
mpl.use('Agg') # When I don't want to display graphs

import matplotlib.pyplot as plt
import kmeans_radec as kmrd
import sys
from scipy import integrate
import treecorr
import time
import scipy

from colossus.cosmology import cosmology as csmg
#csmg.setCosmology('WMAP9', {'flat':True,'Om0':0.2}) # setting cosmology in colossus

from colossus.halo import concentration as concen
# my other files
import error as err
import cosmology as cosmo
import ploting
import fitting_NFW

# <<--Important overarching variable Definitions-->>
# coorilation function
min_sep = 0.2
max_sep = 7
nbins = 10
n = 4
# data limits
clust_z_min = 0.1
clust_z_max = 0.33
gal_z_min = 0.4
# <<--End of line-->>

program_start = time.time()

# getting cluster and bacground galaxy data
clusters = fits.getdata('/calvin1/mattchell200/redmapper_catalogs/sdss/v5.10/dr8_run_redmapper_v5.10_lgt20_catalog.fit');

back_gal = fits.getdata('/calvin1/mattchell200/fits/sdss_galaxies_dr12_cut.fit')
# print clusters.dtype.names
# imposing limits on cluster data
print('n = {0}\nR_min = {1}\nR_max = {2}'.format(n, min_sep, max_sep))
print('cutting clusters...')
c = np.array([clusters['RA'].copy(),clusters['DEC'].copy(),clusters['Z_LAMBDA'].copy(),clusters['LAMBDA_CHISQ'].copy()]).T
c_rel = np.array([[clust[0],clust[1],clust[2],cosmo.easy_D_A(clust[2]),clust[3]] for clust in c if clust_z_min < clust[2] < clust_z_max and clust[3] > 20.]).T
n_clusters = c_rel[0].size
print("{0} clusters.".format(n_clusters))

# imposing limits on galaxy data
print('cutting galaxies...')
b_g = np.array([back_gal['RA'].copy(),back_gal['DEC'].copy(),back_gal['Z_NOQSO'].copy(),back_gal['i'].copy(),back_gal['g'].copy(),back_gal['r'].copy(),back_gal['i_color'].copy(),back_gal['g_color'].copy(),back_gal['r_color'].copy(),back_gal['i_psf'].copy(),back_gal['i_mod'].copy(),back_gal['i_fib2'].copy(),back_gal['z_mod'].copy(),back_gal['z_psf'].copy()]).T
b_g_rel = np.array([gal for gal in b_g if gal_z_min < gal[2]]).T
n_galaxies = b_g_rel[0].size
print("{0} galaxies.".format(n_galaxies))

'''
plt.plot(b_g_rel[3], b_g_rel[11], ',b')
plt.xlabel(r'$ i $-band Magnitudes')
plt.ylabel(r'$ i_{psf} $ Magnitudes')
plt.subplots_adjust(bottom=0.15, left=0.15)
plt.show()

b_g_rel_north = np.asarray([gal for gal in b_g_rel.T if 100 < gal[0] < 300]).T
b_g_rel_south = np.asarray([gal for gal in b_g_rel.T if 100 > gal[0] or gal[0] > 300]).T

plt.plot(b_g_rel_north[0],b_g_rel_north[1],"b,")
plt.plot(b_g_rel_south[0],b_g_rel_south[1],"r,")
plt.title("Galaxies in the North (blue) and South (red)")
plt.xlabel("RA")
plt.ylabel("Dec")
plt.xlim(0.,360.)
plt.ylim(-90.,90.)
plt.show()

plt.hist(c_rel[4],50,range=(15.,205.),normed=True,color="blue",alpha=0.3)
#plt.hist(b_g_rel_south[2],50,range=(0.2,1.2),normed=True,color="red",alpha=0.3)
plt.title("Galaxy Redshift Distribution in the North (blue) and South (red)")
plt.xlabel(r"$z$",fontsize=16)
plt.ylabel(r"Probability")
plt.yscale("log")
plt.show()
'''
# set probability distributions for calcualtion of expected mu value
fitting_NFW.set_p_of_zr(c_rel.copy(),20.,210.)
fitting_NFW.set_p_of_g(b_g_rel[2].copy(),gal_z_min)

# using treecorr corrilation function for average z at pysical seperation
print('finding correlation function...')

cat_c = treecorr.Catalog(ra=c_rel[0].copy(), dec=c_rel[1].copy(), r=c_rel[3].copy(), ra_units='deg', dec_units='deg')
nn = treecorr.NNCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins, metric='Rlens')

del_z_bar = np.zeros(nbins)
N = np.zeros(nbins)
cat_g = treecorr.Catalog(ra=b_g_rel[0].copy(), dec=b_g_rel[1].copy(), w=np.power(b_g_rel[2].copy(),n), ra_units='deg', dec_units='deg')
nn.process(cat_c,cat_g)

z_bar = nn.weight.copy()/nn.npairs.copy()
mean_z = np.average(np.power(b_g_rel[2],n))
print(mean_z, z_bar, n)
del_z_bar = z_bar-mean_z
N = nn.npairs.copy()
sep = nn.meanr.copy()
print(N)

# Calcualtion of error on the signal
Cov_mat = err.jack_knife(nn,b_g_rel[0:3].copy(),c_rel.copy(),mean_z,sep,nbins,n,ncen=70,read_from_file=False)
#Cov_mat = err.randoms_cluster_errors(n_clusters,clust_z_min,clust_z_max,nn,cat_g,mean_z,sep,nbins,read_from_file=False)
#Cov_mat = err.anylitical_error(b_g_rel[2],N)

# quick significance checker
'''
mean_2_z = np.average(np.power(b_g_rel[2],2.))
chi_2 = np.sum(np.power(del_z_bar,2.)*N/(mean_2_z-np.power(mean_z,2.)))
print("chi_2: {0}".format(chi_2))
chi_2_dist = lambda x: np.power(x,nbins/2.-1)*np.exp(-x/2.)/(np.power(2.,nbins/2.)*scipy.special.gamma(nbins/2.))
tot_prob = scipy.integrate.quad(chi_2_dist,0,chi_2)
print("total probability: {0}".format(tot_prob))
print("Significance: {0}".format(scipy.special.erfinv(tot_prob[0])*np.power(2,0.5)))
'''
print("fitting signal...")
M_0, var_M_0 = fitting_NFW.multiple_profiles(sep.copy(),del_z_bar.copy(),N.copy(),b_g_rel.copy(),c_rel.copy(),n,Cov_mat.copy())

r = np.logspace(np.log10(min_sep),np.log10(max_sep),30,endpoint=True)
model = []
model_p = []
model_m = []
M_p = M_0+np.power(var_M_0,0.5)
M_m = M_0-np.power(var_M_0,0.5)
quad_coeff = fitting_NFW.get_quad_coeff_for_d_del_z_d_Sig(b_g_rel.copy(),n)
for r_i in r:
    model.append(fitting_NFW.expect_delta_z_bar_of_Sig(M_0,r_i,quad_coeff))
    model_p.append(fitting_NFW.expect_delta_z_bar_of_Sig(M_p,r_i,quad_coeff))
    model_m.append(fitting_NFW.expect_delta_z_bar_of_Sig(M_m,r_i,quad_coeff))
model = np.asarray(model)
model_p = np.asarray(model_p)
model_m = np.asarray(model_m)

'''
# ploting signal to noise as a function of mode number
n = np.linspace(.5,5.,16,endpoint=True)
print(n)
sig_noise = []
signal = []
noise = []
for k in n:
    del_z_n_bar, sig = fitting.other_modes(b_g_rel,cat_c,nn,k)
    A_n, sig_A = fitting.fit_powr_law(sep,del_z_n_bar,sig)
    sig_noise.append(A_n/sig_A)
    signal.append(A_n)
    noise.append(sig_A)
s_n = np.asarray(sig_noise)
signal = np.asarray(signal)
noise = np.asarray(noise)

plt.plot(n,s_n)
plt.xlabel('n')
plt.ylabel('Signal/Noise')
plt.title('Checking Signal to Noise')
plt.show()

plt.plot(n,signal,'r')
plt.plot(n,noise,'b')
plt.xlabel('n')
plt.ylabel('Signal (red) and Noise (blue)')
plt.title('Signal And Noise')
plt.show()
'''

# ploting avg redshift as a function of r
old_sig = np.power((np.average(np.power(b_g_rel[2],2.))-np.power(np.average(b_g_rel[2]),2.))/N,0.5)
sig = np.power(np.array([Cov_mat[i,i] for i in range(sep.size)]),0.5)
print(sig/old_sig)
plt.figure(1,figsize=(7.5,7),dpi=80)
plt.axhline(y=0.0, xmin=0.0, xmax=1.0, linewidth=1, color='k')
data_graph = plt.errorbar(sep, del_z_bar, yerr=sig, fmt='o', color='b', capsize=3)
plt.fill_between(r, model_m, model_p, color='r', alpha=0.25)
model_graph, = plt.plot(r, model, 'r')
sig_label, = plt.fill(np.NaN, np.NaN, 'r', alpha=0.25)

#model_p_graph = plt.plot(r,model_p,'r',alpha=0.5)
#model_m_graph = plt.plot(r,model_m,'r',alpha=0.5)
if n == 1:
    plt.ylabel(r'$\Delta \bar{z}$', fontsize=14)
else:
    plt.ylabel(r'$\Delta \bar{z^{'+str(n)+'}}$', fontsize=14)

plt.xscale('log')
plt.xlim([min_sep, max_sep])
plt.xlabel(r'$R \ (Mpc)$')

plt.subplots_adjust(hspace=0.26,wspace=0.56,right=0.95,left=0.16,top=0.93,bottom=0.15)
plt.legend([data_graph, (model_graph, sig_label)], [r'$\Delta \bar{z}$ Signal', 'NSF model'])
#plt.suptitle('Redshift Signal vs. Physical Radius')
plt.savefig("auto/delta_z_bar_fit_NFW_{0}.png".format(n))
plt.close()

# ploting r*avg redshift as a function of r
plt.figure(1,figsize=(7.5,7),dpi=80)
plt.axhline(y=0.0, xmin=0.0, xmax=1.0, linewidth=1, color='k')
data_graph = plt.errorbar(sep, sep*del_z_bar, yerr=sep*sig, fmt='o', color='b', capsize=3)
plt.fill_between(r, r*model_m, r*model_p, color='r', alpha=0.25)
model_graph, = plt.plot(r, r*model, 'r')
sig_label, = plt.fill(np.NaN, np.NaN, 'r', alpha=0.25)

if n == 1:
    plt.ylabel(r'$R \Delta \bar{z} \ (Mpc)$', fontsize=14)
else:
    plt.ylabel(r'$R \Delta \bar{z^{'+str(n)+'}} \ (Mpc)$', fontsize=14)

plt.xscale('log')
plt.xlim([min_sep, max_sep])
plt.xlabel(r'$R \ (Mpc)$')

plt.subplots_adjust(hspace=0.26,wspace=0.56,right=0.95,left=0.16,top=0.93,bottom=0.15)
plt.legend([data_graph, (model_graph, sig_label)], [r'$\Delta \bar{z}$ Signal', 'NSF model'])
#plt.suptitle('Redshift Signal Times Radius vs. Physical Radius')
plt.savefig("auto/delta_z_bar_r_fit_NFW_{0}.png".format(n))
plt.close()

'''
ploting.histogram(b_g_rel[2],100,range=(0.,3.),color='r',xlabel='Redshift',ylabel='counts',title='Log of Background Galaxies Histogram',yscale='log')

ploting.plot_posit(b_g_rel[0],b_g_rel[1],color='r')
'''
program_end = time.time()
run_time = program_end-program_start

print("done")
print("total run time: "+str(run_time)+" sec")
print("             or "+str(run_time/60.)+" min")
print("             or "+str(run_time/3600.)+" hr\n")


import random
from argparse import ArgumentParser
from datetime import datetime
from pathlib import Path

import h5py
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from sklearn.metrics import classification_report, confusion_matrix

from commands import Command
from model import train_model, test_model
from math import floor

parser = ArgumentParser()

parser.add_argument("data_files", nargs="+", help="Data files to train on", default=[])
parser.add_argument("--output_file", "-o", type=str, help="Output file",
                    default=str(Path(f"./models/{datetime.now().strftime('%Y-%m-%d--%H_%M_%S')}--model")))
parser.add_argument("--epochs", "-e", type=int, help="Epochs to train for")
parser.add_argument("--steps_per_epoch", "-s", type=int, help="Steps/batches per epoch")
parser.add_argument("--sequence_length", "-sl", type=int, help="Size of the RNN sequence to train on", default=100)
parser.add_argument("--batch_size", "-b", type=int, help="Batch Size", default=32)
parser.add_argument("--validation_steps", "-vs", type=int, default=10, help="Validation steps per epoch (batches of "
                                                                            "batch_size sequence_size sequences to "
                                                                            "validate on)")
parser.add_argument("--test_split", "-t", type=float, default=0.1, help="Test split")

NOTHING_WEIGHT = 0.075

def random_segments(data, batch_size, segment_length: int, noise: bool):
    use_data = [d for d in data if d[0].shape[0] >= segment_length]
    while True:
        batch_features = []
        batch_labels = []
        weight = []
        for i in range(batch_size):
            features, labels = random.choice(use_data)
            start = random.randint(0, len(features) - (segment_length + 1))

            feature = features[start:start + segment_length]

            if noise:
                feature += np.random.normal(0, 0.2, feature.shape)

            batch_features.append(feature)
            l = labels[start:start + segment_length]
            batch_labels.append(l)
            # 0.1 if Nothing, else 1
            weight.append(l[:, 0] * -(1 - NOTHING_WEIGHT) + 1)

        yield np.stack(batch_features, axis=0), np.stack(batch_labels, axis=0), np.stack(weight, axis=0)

def all_segments(files, index: int, batch_size: int, segment_length: int):
    for f in files:
        data = f[index]
        i = 0
        while i < len(data) - segment_length:
            batch = []
            for j in range(batch_size):
                if i >= len(data) - segment_length:
                    break

                batch.append(data[i:i + segment_length])
                i += 20

            if len(batch) > 0:
                yield np.stack(batch, axis=0)

def plot_confusion_matrix(cm, target_names, title="Confusion Matrix"):
    cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

    fig, ax = plt.subplots()
    fig.set_size_inches(6, 6)
    fig.set_dpi(100)
    im = ax.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
    ax.figure.colorbar(im, ax=ax)
    # We want to show all ticks...
    ax.set(xticks=np.arange(cm.shape[1]),
           yticks=np.arange(cm.shape[0]),
           # ... and label them with the respective list entries
           xticklabels=target_names, yticklabels=target_names,
           title=title,
           ylabel='True label',
           xlabel='Predicted label')

    # Rotate the tick labels and set their alignment.
    plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
             rotation_mode="anchor")

    # Loop over data dimensions and create text annotations.
    fmt = '.2f'
    thresh = cm.max() / 2.
    for i in range(cm.shape[0]):
        for j in range(cm.shape[1]):
            ax.text(j, i, format(cm[i, j], fmt),
                    ha="center", va="center",
                    color="white" if cm[i, j] > thresh else "black")
    fig.tight_layout()

    plt.plot()
    plt.show()

def display_report(model, test_data, test_labels, name, args):
    target_names = [c.name for c in list(Command)]
    y_true = test_labels
    y_true = np.argmax(y_true, axis=-1).flatten()

    Y_pred = model.predict(test_data)
    y_pred = np.argmax(Y_pred, axis=-1).flatten()
    # y_pred = y_pred.flatten()
    print(f'{name} Classification Report')
    print(classification_report(y_true, y_pred, target_names=target_names))

    cm = confusion_matrix(y_true, y_pred)

    plot_confusion_matrix(cm, target_names, f"{name} Confusion Matrix")

def segment_data(all_data):
    last = 0
    new_data = []
    new_labels = []
    nothing_count = 0
    for d in all_data:
        data = d[0]
        labels = d[1]
        last = 0
        nothing_count = 0
        for i in range(0, len(data)):
            current = labels[i].argmax()
            # print("{}: {}".format(i, labels[i].argmax()))
            if(current != last):
                if(current != 0):
                    new_data.append(data[i:i+29])
                    new_labels.append(labels[i+5].tolist())
                    if(nothing_count == 0):
                        new_data.append(data[i-29:i])
                        new_labels.append(labels[i-5].tolist())
                    nothing_count = (nothing_count + 1) % 4
            last = current
    new_labels = np.array(new_labels)
    new_data = np.array(new_data)
    # print(new_labels)
    return new_data, new_labels

if __name__ == '__main__':

    if len(tf.config.list_physical_devices('GPU')) < 1:
        print("Warning: Not using GPU, this will be slow")

    args = parser.parse_args()

    data_files = [Path(d) for d in args.data_files]

    for d in data_files:
        if not d.exists():
            raise FileNotFoundError(f"Data file {d} does not exist")

    all_data = []

    for d in data_files:
        with h5py.File(str(d), 'r') as f:
            all_data.append((np.log10(f["features"][:]), f["labels"][:]))

    #print(all_data)
    #exit()
    new_data, new_labels = segment_data(all_data)
    total_count = new_data.shape[0]
    print(total_count)
    test_count = floor(total_count * 0.2)
    end_train = total_count - test_count
    indices = np.arange(new_data.shape[0])
    np.random.shuffle(indices)
    new_data = new_data[indices]
    new_labels = new_labels[indices]
    train_data = new_data[0:end_train]
    test_data = new_data[end_train:]
    train_labels = new_labels[0:end_train]
    test_labels = new_labels[end_train:]
    #print(train_data)
    #print(train_labels)

    # all_features = np.concatenate([d[0] for d in all_data], axis=0)
    #
    # f_mean = np.mean(all_features, axis=0)
    # f_std = np.std(all_features, axis=0)
    #
    # shift = 2
    # all_data = [((d[0] - f_mean) / f_std, np.concatenate([np.zeros((shift, 5)), d[1][:-shift, :]], axis=0)) for d in
    #             all_data]
    #
    # num_test = int(sum(d[0].shape[0] for d in all_data) * args.test_split)
    #
    # file_idx = 0
    # data_idx = 0
    # remaining = num_test
    #
    # train = []
    # test = []

    model = test_model()

    #print(train_data)
    #X_train_log = np.log10(train_data, out=np.zeros_like(train_data), where=(train_data!=0))
    #X_test_log = np.log10(test_data, out=np.zeros_like(test_data), where=(test_data!=0))
    #exit()
    X_train_log = train_data
    X_test_log = test_data

    hist = model.fit(X_train_log,train_labels,validation_data=(X_test_log,test_labels),epochs=12,batch_size=20)

    # Plot training & validation loss values
    plt.plot(hist.history['loss'])
    plt.plot(hist.history['val_loss'])
    plt.title('Model loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Test'], loc='upper left')
    plt.show()

    # Plot training & validation accuracy values
    plt.plot(hist.history['acc'])
    plt.plot(hist.history['acc_1'])
    plt.plot(hist.history['val_acc'])
    plt.plot(hist.history['val_acc_1'])
    plt.title('Model accuracy')
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['Train', "Weighted Train", 'Test', "Weighted Test"], loc='upper left')
    plt.show()

    out_path = Path(args.output_file)

    model.save(args.output_file)

    #np.save(out_path / "mean.npy", f_mean)
    #np.save(out_path / "std.npy", f_std)

    # just train
    display_report(model, X_train_log, train_labels, "Just Train", args)

    # just test
    display_report(model, X_test_log, test_labels, "Just Test", args)


from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import argparse
import os
import sys
from uuid import uuid4

from PH5WebService.PH5MetaQuery.station_args_container \
                                import StationArgsContainer
from PH5WebService.common import inventory_utils
from PH5WebService.common.custom_exceptions import ArgumentValidationError
from PH5WebService.common.log import sta_logger
from PH5WebService.PH5MetaQuery.write_ph5geocsv import inventory_to_ph5_geocsv
from PH5WebService.PH5MetaQuery.write_stationtxt import inventory_to_station_text
from PH5WebService.PH5MetaQuery.configuration import CONFIG
from ph5.core import ph5api
from ph5.clients.ph5tostationxml import run_ph5_to_stationxml

def parse_arguments():

    sentinel_dict = {}

    def _preprocess_sysargv(argv):
        inputs = []
        for arg in argv[1:]:
            # handles case where values contain --, otherwise they will
            # be interpreted as arguments.
            if '--,' in arg or ',--' in arg or arg == '--':
                sentinel = uuid4().hex
                key = '%s' % sentinel
                sentinel_dict[key] = arg
                inputs.append(sentinel)
            else:
                inputs.append(arg)
        return inputs

    def _postprocess_sysargv(v):
        if v in sentinel_dict:
            return sentinel_dict.get(v)
        else:
            return v

    # define command line arguments
    parser = argparse.ArgumentParser(description='Command line parser for '
                                                 'pic-ph5 station xml query.')
    # Parameter for handling POST request
    parser.add_argument('--STDIN',
                        help="For POST request, POST body "
                             "is passed in as stdin",
                        action="store_true")
    # channel options
    parser.add_argument('--network', '--net',
                        help="Select one or more network codes. Can be SEED "
                             "codes or data center defined codes. Accepts "
                             "wildcards and lists.")
    parser.add_argument('--reportnum', '--report',
                        help="Select one or more network codes. Can be SEED "
                             "codes or data center defined codes. Accepts "
                             "wildcards and lists.")
    parser.add_argument('--station', '--sta',
                        help="Select one or more SEED station codes. Accepts "
                             "wildcards and lists.")
    parser.add_argument('--location', '--loc',
                        help="Select one or more SEED location identifier. "
                        "Use -- for 'Blank' location IDs (ID's containing 2 "
                        "spaces). Accepts wildcards and lists.",
                        type=_postprocess_sysargv)
    parser.add_argument('--channel', '--cha',
                        help="Select one or more SEED channel codes. "
                             "Accepts wildcards and lists.")
    parser.add_argument('--arrayid', '--array',
                        help="Select one or more SEED channel codes. "
                             "Accepts wildcards and lists.")
    parser.add_argument('--receiver',
                        help="Select one or more receiver ids. "
                             "Accepts lists.")
    parser.add_argument('--component',
                        help="Select one or more component ids. Accepts "
                             "wildcards and lists.")

    # time constraints
    parser.add_argument('--starttime', '--start',
                        help="Limit to metadata describing channels operating "
                        "on or after the specified start time.")
    parser.add_argument('--endtime', '--end',
                        help="Limit to metadata describing channels operating "
                             "on or before the specified end time.")

    # rectangular geographic constraints
    parser.add_argument('--minlatitude', '--minlat', help='Southern boundary.')
    parser.add_argument('--maxlatitude', '--maxlat', help='Northern boundary.')
    parser.add_argument('--minlongitude', '--minlon', help='Western boundary.')
    parser.add_argument('--maxlongitude', '--maxlon', help='Eastern boundary.')

    # radial geographic constraints
    parser.add_argument("--latitude", "--lat", action="store",
                        help="Specify the central latitude point for a radial "
                             "geographic constraint.")
    parser.add_argument("--longitude", "--lon", action="store",
                        help="Specify the central longitude point for a "
                             "radial geographic constraint.")
    parser.add_argument("--minradius", action="store",
                        help="Specify minimum distance from the geographic "
                             "point defined by latitude and longitude.")
    parser.add_argument("--maxradius", action="store",
                        help="Specify maximum distance from the geographic "
                             "point defined by latitude and longitude.")

    # miscellaneous
    parser.add_argument('--level', default='station',
                        help="Specify level of detail using network, station, "
                        "channel,or response.")
    parser.add_argument('--format', default='xml',
                        help="Specify output format. Valid formats "
                             "include xml.")

    args = parser.parse_args(_preprocess_sysargv(sys.argv))
    return args

def parse_level(level, out_format):
    """
    Method for parsing PH5 Station Service request level
    :param level: The Station service request level (Network, Station,
        Channel, or Response) :type: str
    :returns: A validated request level string :type: str
    """
    level = level.upper()
    out_format = out_format.upper()
    if (level == 'RESPONSE' and out_format == 'TEXT') or \
            level not in ['NETWORK', 'STATION', 'CHANNEL', 'RESPONSE']:
        raise ArgumentValidationError("Invalid level - '%s'." % level)
    else:
        return level

def get_request_args(args_ns):
    request_list = []
    args_dict = vars(args_ns)
    if args_ns.STDIN:
        post_data = sys.stdin.read().splitlines()
        # add global parameter values
        args_dict.update({(k.strip(), v.strip()) for k, v in
                         (item.split('=') for item in post_data
                         if '=' in item)})
        # add network, station, location, channel, starttime, and
        # endtime query arguments
        post_req_list = []
        for request in list(item.strip() for item in post_data
                            if '=' not in item):
            items = request.split(' ')
            if len(items) == 6:
                post_req = {}
                post_req.update(args_dict)  # set defaults
                post_req.update({'network': items[0],
                                 'station': items[1],
                                 'location': items[2],
                                 'channel': items[3],
                                 'starttime': items[4],
                                 'endtime': items[5]})
                # starttime and endtime support '*' for http POST requests only
                if post_req.get('starttime') == "*":
                    post_req['starttime'] = None
                if post_req.get('endtime') == "*":
                    post_req['endtime'] = None
                post_req_list.append(post_req)
            else:
                continue  # skip bad request
        for request in post_req_list:
            sxml_arg_cont = StationArgsContainer(request)
            request_list.append(sxml_arg_cont)
    else:
        sxml_arg_cont = StationArgsContainer(args_dict)
        request_list.append(sxml_arg_cont)
    return request_list

def get_experiment_paths(data_dir=None):
    if not data_dir:
        data_dir = CONFIG.data_dir
    paths = []
    basepaths = data_dir.split(",")
    for basepath in basepaths:
        for dirName, _, fileList in os.walk(basepath):
            for fname in fileList:
                if fname == "master.ph5":
                    paths.append(dirName)
    return paths

def read_ph5_networks(data_dir, level):
    """
    Reads network level inventories for each PH5 network in the archive
    """
    sxml_arg_cont = StationArgsContainer({})
    sxml_arg_cont.set_level(level)
    sxml_arg_dict = sxml_arg_cont.__dict__

    exp_paths = get_experiment_paths(data_dir)
    for path in exp_paths:
        inv = get_stationxml_inv([sxml_arg_dict],
                                 level=level,
                                 paths=[path])

        yield inv

def get_stationxml_inv(sxml_arg_dict_list, out_format="XML",
                       level="RESPONSE", paths=None):
    uri = os.environ.get('REQUESTURL')
    if paths is None:
        paths = get_experiment_paths()
    inv = run_ph5_to_stationxml(paths,
                                nickname="master.ph5",
                                out_format=out_format,
                                level=level,
                                uri=uri,
                                args_dict_list=sxml_arg_dict_list)

    return inv

def group_requests_by_network(sxml_arg_cont_list):
    grouped_req_dict = {}
    for sxml_arg_cont in sxml_arg_cont_list:
        net_list = str(sxml_arg_cont.get_network_list())
        if net_list not in grouped_req_dict.keys():
            grouped_req_dict[net_list] = [sxml_arg_cont.as_dict()]
        elif net_list:
            grouped_req_dict[net_list].append(sxml_arg_cont.as_dict())
        else:
            return sxml_arg_cont_list
    return list(grouped_req_dict.values())

def exit_with_error(err_msg, error_code):
    sys.stderr.write(err_msg)
    exit(error_code)

def run_station_service():
    try:
        iris_custom_ns = "http://www.fdsn.org/xml/station/1/iris"
        inv_list = []
        # read user arguments
        args_ns = parse_arguments()
        requests = get_request_args(args_ns)  # get a list of requests
        for sxml_arg_cont_list in group_requests_by_network(requests):
            out_format = sxml_arg_cont_list[0].get('out_format')
            level = parse_level(sxml_arg_cont_list[0].get('level'), out_format)
            sxml_inv = get_stationxml_inv(
                            sxml_arg_cont_list,
                            'TEXT' if (out_format=='GEOCSV') else out_format,
                            level)
            if sxml_inv:
                inv_list.append(sxml_inv)
        inv = inventory_utils.combine_inventories(inv_list)
        if not inv:
            exit_with_error("Request resulted in no data.", 2)
        else:
            inv = inventory_utils.sort_inventory(inv)
            if out_format == 'TEXT':
                try:
                    for line in inventory_to_station_text(inv, level):
                        sys.stdout.write(line)
                except Exception as err:
                    exit_with_error(err.message, 1)
            elif out_format == 'GEOCSV':
                try:
                    for line in inventory_to_ph5_geocsv(inv, level):
                        sys.stdout.write(line)
                except Exception as err:
                    exit_with_error(err.message, 1)
            elif out_format == 'XML' or out_format == 'STATIONXML':
                inv.write(sys.stdout, format='STATIONXML',
                          nsmap={'iris': iris_custom_ns})
    except ArgumentValidationError as err:
        exit_with_error(err.message, 3)
    except Exception as err:
        sta_logger.error("Unexpected error - %s" % err.message)
        exit_with_error(err.message, 1)


from collections import OrderedDict

from django import template
from django.contrib.auth.models import Group

register = template.Library()

@register.filter(is_safe=True)
def dict_sort(value):
    if isinstance(value, dict):
        new_dict = OrderedDict()
        key_list = sorted(value.keys())
        for key in key_list:
            new_dict[key] = value[key]
        return new_dict
    elif isinstance(value, list):
        return sorted(value, key=lambda k: k[0])
    else:
        return value

@register.filter
def addstr(arg1, arg2):
    return str(arg1) + str(arg2)

@register.filter(name='has_group')
def has_group(user, group_name):
    group = Group.objects.get(name=group_name)
    return True if group in user.groups.all() else False


import pathlib
import uuid

import invoke
import toml

def _get_git_root(ctx):
    root = ctx.run('git rev-parse --show-toplevel', hide=True).stdout.strip()
    return pathlib.Path(root)

def _get_news_dir(ctx):
    return _get_git_root(ctx).joinpath('news')

def get_toml_file(ctx):
    return _get_git_root(ctx).joinpath('pyproject.toml')

def get_random():
    return str(uuid.uuid4())[:8]

@invoke.task()
def add(ctx, description, type_, issue=None):
    """Create a news entry.
    """
    toml_file = get_toml_file(ctx)
    tf = toml.load(toml_file.as_posix())
    allowed_types = tf.get('tool', {}).get('towncrier', {}).get('type', [])
    if allowed_types:
        allowed_types = [t.get('directory') for t in allowed_types]
    if not allowed_types or type_ not in allowed_types:
        print("[news.add] Invalid type or no types found: {0}".format(type_))
        print("Valid news types: {0}".format(' '.join(allowed_types)))
        return
    target_dir = _get_news_dir(ctx)
    existing_files = [
        f.stem.split('-')[0]
        for f in target_dir.glob('*.{0}'.format(type_))
        if f.stem.split('-')[0].isdigit()
    ]
    if not existing_files and not issue:
        nextfile = target_dir / '1-{0}.{1}'.format(get_random(), type_)
    elif issue:
        nextfile = target_dir / '{0}.{1}'.format(issue, type_)
    else:
        nextfile = '{0}-{1}.{2}'.format(
            int(sorted(existing_files).pop()) + 1,
            get_random(),
            type_,
        )
        nextfile = target_dir / nextfile
    print("[news.add] Adding newsfile {0} => [{1}] {2}".format(
        nextfile.name,
        type_.upper(),
        description,
    ))
    nextfile.write_text(description)

@invoke.task
def generate(ctx, draft=False):
    """Generate NEWS from fragments.
    """
    print("[news.generate] Generating NEWS")
    args = []
    if draft:
        args.append("--draft")
    ctx.run("towncrier {}".format(" ".join(args)))


from snownlp import sentiment
from snownlp import SnowNLP
import pandas as pd

text = pd.read_csv("test.csv")
text1=text.iloc[:,0]
#sentiment.train('negative.csv','positive.csv')
#sentiment.save('/Users/jasondennis/Desktop/graduate-design/NLP\ Analysis')
sentiment.load('/Users/jasondennis/Desktop/graduate-design/NLP\ Analysis')
senti=[SnowNLP(i).sentiments for i in text1] #遍历每条评论进行预测

newsenti=[]

for i in senti:
    if (i>=0.6):
        newsenti.append(1)
    else:
        newsenti.append(-1)
text['predict']=newsenti  #将新的预测标签增加为text的某一列，所以现在text的第0列为评论文本，第1列为实际标签，第2列为预测标签
counts=0
for j in range(len(text.iloc[:,0])): #遍历所有标签，将预测标签和实际标签进行比较，相同则判断正确。
    if text.iloc[j,2]==text.iloc[j,1]:
        counts+=1
print (float(counts)/float(len(text)))


#!/usr/bin/env python
# coding: utf-8

import numpy as np
import pandas as pd

import os
import itertools

import functools

import operator
import re

import scipy as sp

from scipy.sparse import csc_matrix
from scipy.sparse.linalg import svds, eigs

from scipy.linalg import toeplitz

import math

from matplotlib import pyplot as plt

from bs4 import BeautifulSoup
from time import time

import nolds
import skedm as edm
from scipy.signal import argrelextrema
import json
import measures1 as nolds1

class NumpyEncoder(json.JSONEncoder):
    """ Special json encoder for numpy types """
    def default(self, obj):
        if isinstance(obj, (np.int_, np.intc, np.intp, np.int8,
            np.int16, np.int32, np.int64, np.uint8,
            np.uint16, np.uint32, np.uint64)):
            return int(obj)
        elif isinstance(obj, (np.float_, np.float16, np.float32,
            np.float64)):
            return float(obj)
        elif isinstance(obj,(np.ndarray,)): #### This is the fix
            return obj.tolist()
        return json.JSONEncoder.default(self, obj)

import Complexity_entropy as complexity
from math import factorial

# In[2]:
print('Loading data...')

#Import Data Books
sb = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/Books/balls1000_t/sB.csv', delimiter=',')
vtb = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/Books/balls1000_t/vtB.csv', delimiter=',')
ub = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/Books/balls1000_t/uB.csv', delimiter=',')
wb = ub*sb

print('Loaded books...')

# #Import Data Wikipedia
# sw = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/Wikipedia/d200000s1000/sw.csv', delimiter=',')
# vtw = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/Wikipedia/d200000s1000/vtw.csv', delimiter=',')
# uw = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/Wikipedia/d200000s1000/uw.csv', delimiter=',')
# ww = uw*sw

# print('Loaded wikipedia...')

# #############################################################
# #Import Data Bot

sbot = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with books/new_0t/sb.csv', delimiter=',')
vtbot = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with books/new_0t/vtb.csv', delimiter=',')
ubot = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with books/new_0t/ub.csv', delimiter=',')
wbot = ubot*sbot

# # In[ ]:

# #Import Data Human

sh = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with books/new_0t/sh.csv', delimiter=',')
vth = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with books/new_0t/vth.csv', delimiter=',')
uh = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with books/new_0t/uh.csv', delimiter=',')
wh = uh*sh

# print('Loaded Comments with books dict...')
# #############################################################3
# #Import Data Bot

# sbotw = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with wikipedia/sb2.csv', delimiter=',')
# vtbotw = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with wikipedia/vtb2.csv', delimiter=',')
# ubotw = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with wikipedia/ub2.csv', delimiter=',')
# wbotw = ubotw*sbotw

# # In[ ]:

# #Import Data Human

# shw = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with wikipedia/sh2.csv', delimiter=',')
# vthw = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with wikipedia/vth2.csv', delimiter=',')
# uhw = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with wikipedia/uh2.csv', delimiter=',')
# whw = uhw*shw
# print('Loaded Comments with wiki dict...')
#############################################################3
# #Import Data Bot

# sbots = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with sentiment words/sb3.csv', delimiter=',')
# vtbots = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with sentiment words/vtb3.csv', delimiter=',')
# ubots = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with sentiment words/ub3.csv', delimiter=',')
# wbots = ubots*sbots

# # In[ ]:

# #Import Data Human

# shs = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with sentiment words/sh3.csv', delimiter=',')
# vths = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with sentiment words/vth3.csv', delimiter=',')
# uhs = np.genfromtxt(fname='/home/rfreyrebonilya/Spotthebot/Resultados/comments/with sentiment words/uh3.csv', delimiter=',')
# whs = uhs*shs
# print('Loaded Comments with PositiveSent dict...')

print('Dimension Books Dict:' ,vtb.shape)
# print('Dimension Wiki Dict:' ,vtw.shape)

# print('Dimension Bot book Dict:' ,vtbot.shape)
# print('Dimension Human book Dict:' ,vth.shape)

# print('Dimension Bot wiki Dict:' ,vtbotw.shape)
# print('Dimension Human wiki Dict:' ,vthw.shape)
# print('Dimension Bot sent Dict:' ,vtbots.shape)
# print('Dimension Human sent Dict:' ,vths.shape)

#############################################################
def reconst(lib,w,vt,k1,k):
    reconstruct = np.zeros(vt[0,:].shape)
    r=(np.array(range(k1,k))+1)*(-1)
    for l in r:
        reconstruct += vt[l,:]*w[lib,l]
    return reconstruct

# def plot_series(lib,w,vt,t): #t type of dict
#     columns = 3
#     rows = 4
#     tipe_dict={1:'book',
#           2:'wiki',
#           3:'bot_b',
#           4:'human_b',
#           5:'bot_w',
#           6:'human_w',
#           7:'bot_s',
#           8:'human_s'
#               }

#     tittle=tipe_dict[t]+str(lib)

#     fig, ax_array = plt.subplots(rows, columns,figsize=(25,20),squeeze=False)

#     index=0
#     #lib=1624

#     modes=[1,2,10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
#     for i,ax_row in enumerate(ax_array):

#         for j,axes in enumerate(ax_row):

#             #print(modes[index])
#             reconstruction=reconst(lib,w,vt,0,modes[index])

#             axes.plot(list(range(0,vt.shape[1])),reconstruction,color=".3",linewidth=1.5)
#             axes.set_title('SV used:{}'.format(modes[index]))
#             axes.set_yticklabels([])
#             axes.set_xticklabels([])
#             index=index+1
#     plt.savefig(os.path.join(os.getcwd(), tittle+'.pdf'))
# #plt.show()

# sample=list(range(0,10))

# print('Plotting samples...')
# for i in sample:

#     plot_series(sample[i],wb,vtb,1)

#     plot_series(sample[i],ww,vtw,2)

#     plot_series(sample[i],wbot,vtbot,3)
#     plot_series(sample[i],wh,vth,4)

#     plot_series(sample[i],wbotw,vtbotw,5)
#     plot_series(sample[i],whw,vthw,6)

#     plot_series(sample[i],wbots,vtbots,7)
#     plot_series(sample[i],whs,vths,8)

#############################################################
def phase_space(lib,w,vt,first_eig,last_eig, emb_dim,lag,order,lag1):

    #reconstruct the book/wiki/comment
    reconstruction=reconst(lib,w,vt,first_eig,last_eig)

    if np.sum(reconstruction)==0:
        probability=99999999999
        MPR=99999999999
        NSE=99999999999

    else:

        probability=complexity.ordinal_pat_prob(reconstruction, order, lag1)
        NSE=complexity.shannon_ent(probability,normalize=True)
        MPR=complexity.MPR_entropy(reconstruction, order, lag1)

    return probability,NSE,MPR

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

def generate_phase_info(w,vt,emb_dim,lag,order,lag1):
    size=100#w.shape[0]

    probability_=[]
    NSE_=[]
    MPR_=[]

    for i in range(size):
        probability,NSE,MPR=phase_space(i,w,vt,0,1000,emb_dim,lag,order,lag1)
        probability_.append(probability)
        NSE_.append(NSE)
        MPR_.append(MPR)

    return probability_,NSE_,MPR_

print('Processin Phase space...')

#emb_dim=20
#lag=1
#print('emb_dim:',emb_dim)
#print('lag:',lag)

#t0 = time()
#NSE_books,MPR_books=generate_phase_info(wb,vtb,emb_dim,lag,order,lag1)
#t1 = time()
#print('Time process books:',t1 - t0)

#generate_phase_info(ww,vtw,Lyapunov_wiki,MI_wiki,PhaseSpace_wiki)

# t0 = time()
# Lyapunov_bot_book,corr_bot_book,PhaseSpace_bot_book,probability_bot_book,NSE_bot_book,MPR_bot_book=generate_phase_info(wbot,vtbot,emb_dim,lag)

# t1 = time()
# print('Time process bot_books:',t1 - t0)

# t0 = time()
# Lyapunov_human_book,corr_human_book,PhaseSpace_human_book,probability_human_book,NSE_human_book,MPR_human_book=generate_phase_info(wh,vth,emb_dim,lag)
# t1 = time()
# print('Time process human_books:',t1 - t0)
#############################################################

print('Saving results...')

#np.savetxt("lyapunov_books.csv", Lyapunov_books, delimiter=",")
# np.savetxt("Lyapunov_bot_book.csv", Lyapunov_bot_book, delimiter=",")
# np.savetxt("Lyapunov_human_book.csv", Lyapunov_human_book, delimiter=",")

#np.savetxt("corr_books.csv", corr_books, delimiter=",")
# np.savetxt("corr_bot_book.csv", corr_bot_book, delimiter=",")
# np.savetxt("corr_human_book.csv", corr_human_book, delimiter=",")

#np.savetxt("NSE_books.csv", NSE_books, delimiter=",")
# np.savetxt("NSE_bot_book.csv", NSE_bot_book, delimiter=",")
# np.savetxt("NSE_human_book.csv", NSE_human_book, delimiter=",")

#np.savetxt("MPR_books.csv", MPR_books, delimiter=",")
# np.savetxt("MPR_bot_book.csv", MPR_bot_book, delimiter=",")
# np.savetxt("MPR_human_book.csv", MPR_human_book, delimiter=",")

#dumped = json.dumps(PhaseSpace_books, cls=NumpyEncoder)
#with open('PhaseSpace_books.txt','w') as myfile:
#    json.dump(dumped,myfile)

#dumped1 = json.dumps(PhaseSpace_bot_book, cls=NumpyEncoder)
# with open('PhaseSpace_bot_book.txt','w') as myfile:
#     json.dump(dumped1,myfile)

#dumped2 = json.dumps(PhaseSpace_human_book, cls=NumpyEncoder)
# with open('PhaseSpace_human_book.txt','w') as myfile:
#     json.dump(dumped2,myfile)

#############################################################
def plot_ps(PhaseSpace_,t): #t type of dict
    columns = 5
    rows = 10
    tipe_dict={1:'book',
          2:'wiki',
          3:'bot_b',
          4:'human_b',
          5:'bot_w',
          6:'human_w',
          7:'bot_s',
          8:'human_s'
              }

    tittle=tipe_dict[t]

    fig, ax_array = plt.subplots(rows, columns,figsize=(25,50),squeeze=False)

    index=0
    #lib=1624

    PhaseSpace_=np.asarray(PhaseSpace_)
    for i,ax_row in enumerate(ax_array):

        for j,axes in enumerate(ax_row):

            axes.plot(np.array(PhaseSpace_[index]).T[0],np.array(PhaseSpace_[index]).T[1])
            axes.set_title('Phase space sample:{}'.format(index))
            #axes.set_yticklabels([])
            #axes.set_xticklabels([])
            index=index+1
    plt.savefig(os.path.join(os.getcwd(), tittle+'.pdf'))
    plt.clf()

def plot_hist(lyap_,t,et):

    tipe_dict={1:'book',
          2:'wiki',
          3:'bot_b',
          4:'human_b',
          5:'bot_w',
          6:'human_w',
          7:'bot_s',
          8:'human_s'
              }

    est_dic={1:'lyapunov',
          2:'correlation'
              }

    tittle=tipe_dict[t]+'_'+est_dic[et]

    lyap_=np.array(lyap_)
    lyap_ = lyap_.astype('float')
    lyap_[lyap_ == 99999999999] = 'nan'
    lyap_ = lyap_[~np.isnan(lyap_)]
    plt.hist(lyap_, bins='auto')
    plt.title(r'Histogram')
    #plt.show()
    plt.savefig(os.path.join(os.getcwd(), tittle+'.pdf'))
    plt.clf()

def import_t(x):
    x=np.asarray(x)
    x = x.astype('float')
    x[x == 99999999999] = 'nan'
    x = x[~np.isnan(x)]
    return x

###################################################################################################
def D_max(p,order=3,dim=1):
    x = np.array(p)
    dim=x.ndim
    N=factorial(order)**dim

    q_0=(-1.0/(((N+1)/N)*np.log(N+1)-2*np.log(2*N)+np.log(N)))*2

    #P=ordinal_pat_prob(x, order, delay)
    P_e=complexity.equi_prob(order,dim)
    #H_P=complexity.shannon_ent(P,normalize=True)

    q_j=(complexity.shannon_ent(np.true_divide((x+P_e),2),normalize=False) - (complexity.shannon_ent(x,normalize=False))/2
        -(complexity.shannon_ent(P_e,normalize=False))/2)*q_0

    return q_j

def D_min(p,n,order=3,dim=1):
    N=factorial(order)**dim
    pe=1/N
    #pe=complexity.equi_prob(order,dim)
    Dmin=np.power(p-pe, 2)+(N-n-1)*np.power(((1-p)/(N-n-1))-pe, 2)+n*np.power(pe, 2)
    #Dmax=np.power(p-pe, 2).sum()

    return Dmin

def D_minenv(h,order=3,dim=1):
    N=factorial(order)**dim
    #pe=1/N
    #pe=equi_prob(order,dim)
    Dminenv=np.exp(-h*np.log(N))-(1/N)
    #Dminenv=np.power(2,-h*np.log2(N))-(1/N)
    #Dmax=np.power(p-pe, 2).sum()

    return Dminenv

def max_min_complexity_entropy(order=3,dim=1,n=0):
    N=factorial(order)**dim
    points=1000
    f_max=np.linspace(1/N,0.99,points)
    f_min=np.linspace(0.01,1/(N-n),points)
    #f=(1-fmax)/(N-1)
    H=[]
    Hmin=[]
    Dmax=[]
    Dminenv=[]
    Dmin=[]
    for fmax in f_max:
        f=np.concatenate((fmax, np.multiply(np.ones(N-1),((1-fmax)/(N-1)))), axis=None)
        #h=-(1/np.log(N))*((fmax*np.log(fmax))+(1-fmax)*np.log((1-fmax)/(N-1)))
        h=complexity.shannon_ent(f,normalize=True)
        H.append(h)
        #dmax=D_max(fmax,order,dim)
        dmax=D_max(f,order,dim)
        Dmax.append(dmax)
        dminenv=D_minenv(h,order,dim)
        Dminenv.append(dminenv)

    for fmin in f_min:
        #f=np.concatenate((fmax, np.ones(N-1)*((1-fmax)/(N-1))), axis=None)
        hmin=-(1/np.log(N))*((fmin*np.log(fmin))+(1-fmin)*np.log((1-fmin)/(N-n-1)))
        #h=shannon_ent(f,normalize=True)
        Hmin.append(hmin)
        dmin=D_min(fmin,n,order,dim)
        Dmin.append(dmin)

    H=np.asarray(H)
    Hmin=np.asarray(Hmin)

    Dmax=np.asarray(Dmax)
    Dminenv=np.asarray(Dminenv)
    Dmin=np.asarray(Dmin)

    Cmax=H*Dmax
    Cminenv=H*Dminenv
    Cmin=Hmin*Dmin

    return Cmax,Cmin,Cminenv,H,Hmin

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

def plot_complex_entrop(MPR_books,MPR_bot,MPR_human,NSE_books,NSE_bot,NSE_human,o,l):
#def plot_complex_entrop(MPR_books,NSE_books,o,l):

    MPR_books=import_t(MPR_books)
    MPR_bot=import_t(MPR_bot)
    MPR_human=import_t(MPR_human)
    NSE_books=import_t(NSE_books)
    NSE_bot=import_t(NSE_bot)
    NSE_human=import_t(NSE_human)

    Cmax,Cmin,Cminenv,H,Hmin=max_min_complexity_entropy(order=o,dim=1,n=0)

    tittle='Complexity_plane'+'_'+'order'+ str(o) + '_' + 'lag'+ str(l)

    plt.figure(figsize=(15, 10))

    #plt.plot(H,Cmax,'--k',markersize=0.2,label='C_max')
    #plt.plot(H,Cminenv,'--k',markersize=0.2,label='C_min')

    plt.plot(NSE_books,MPR_books,'ro',markersize=0.5,label='Books:'+'(min_MPR,max_MPR)=' + '('+ str("{0:.4f}".format(np.min(MPR_books))) + ','+str("{0:.4f}".format(np.max(MPR_books)))+')'
             +'/'+'(min_NSE,max_NSE)=' + '('+ str("{0:.4f}".format(np.min(NSE_books))) + ','+str("{0:.4f}".format(np.max(NSE_books)))+')')

    plt.plot(NSE_bot,MPR_bot,'b+',markersize=0.5,label='Bots:'+'(min_MPR,max_MPR)=' + '('+ str("{0:.4f}".format(np.min(MPR_bot))) + ','+str("{0:.4f}".format(np.max(MPR_bot)))+')'
             +'/'+'(min_NSE,max_NSE)=' + '('+ str("{0:.4f}".format(np.min(NSE_bot))) + ','+str("{0:.4f}".format(np.max(NSE_bot)))+')')

    plt.plot(NSE_human,MPR_human,'g^',markersize=0.5,label='Human:'+'(min_MPR,max_MPR)=' + '('+ str("{0:.4f}".format(np.min(MPR_human))) + ','+str("{0:.4f}".format(np.max(MPR_human)))+')'
             +'/'+'(min_NSE,max_NSE)=' + '('+ str("{0:.4f}".format(np.min(NSE_human))) + ','+str("{0:.4f}".format(np.max(NSE_human)))+')')
    plt.legend()
    plt.xlabel('Normalized Shannon Entropy')
    plt.ylabel('MPR - SC')
    #plt.show()
    plt.savefig(os.path.join(os.getcwd(), tittle+'.pdf'))
    plt.clf()

print('Plotting...')
#############################################################

#plot_complex_entrop(MPR_books,MPR_bot_book,MPR_human_book,NSE_books,NSE_bot_book,NSE_human_book)
#plot_complex_entrop(MPR_books,NSE_books)

#plot_hist(Lyapunov_books,1,1)
#plot_hist(Lyapunov_bot_book,3,1)
#plot_hist(Lyapunov_human_book,4,1)

#plot_hist(corr_books,1,2)
#plot_hist(corr_bot_book,3,2)
#plot_hist(corr_human_book,4,2)

#plot_ps(PhaseSpace_books,1)
# plot_ps(PhaseSpace_bot_book,3)
# plot_ps(PhaseSpace_human_book,4)

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

emb_dim=10
lag=1
orders=[3,4,5,6,7,8,9,10]
lags=[1,2,3,4,5]

for o in orders:
        for l in lags:
                probability_books,NSE_books,MPR_books=generate_phase_info(wb,vtb,emb_dim,lag,o,l)
                probability_bot_book,NSE_bot,MPR_bot=generate_phase_info(wbot,vtbot,emb_dim,lag,o,l)
                probability_human_book,NSE_human,MPR_human=generate_phase_info(wh,vth,emb_dim,lag,o,l)

                #plot_complex_entrop(MPR_books,NSE_books,o,l)
                plot_complex_entrop(MPR_books,MPR_bot,MPR_human,NSE_books,NSE_bot,NSE_human,o,l)


# implement blind regression model to estimate blank entries on the matrix
import math
from heapq import nsmallest
import pickle
import sys
import datetime
import time
import numpy as np

class BlindRegression():
	def __init__(self, data_matrix):
		self._data_matrix = data_matrix
		self._dimension = self._data_matrix.shape
		# calculate "N1(u)" and "N2(i)"
		print(str(self._dimension))
		self._pos_col_entries_set_list = []
		outfile=open('matrix.txt','w')
		for list in data_matrix:
			i=0
			for col in list:
				if i>0:
					outfile.write("\t")
				outfile.write("%s" % col)
				i+=1
			outfile.write("\n")

		outfile.close()
		for row in self._data_matrix:
			self._pos_col_entries_set_list.append(set([i for i in range(len(row)) if row[i] > 0]))

		self._pos_row_entries_set_list = []
		for col in self._data_matrix.T:
			self._pos_row_entries_set_list.append(set([i for i in range(len(col)) if col[i] > 0]))
			#print("self._pos_row_entries_set_list",str(self._pos_row_entries_set_list))

	def estimate_gaussian(self, u, i, beta=2, lam=1, num_points=20):

		if beta < 2:
			print('ERROR: Invalid beta')
			return None

		# calculate "N1(u,v)"
		n1uv = []
		for v in range(self._dimension[0]):
			n1uv.append(self._pos_col_entries_set_list[u].intersection(self._pos_col_entries_set_list[v]))

		# calculate "N2(i,j)"
		n2ij = []
		for j in range(self._dimension[1]):
			n2ij.append(self._pos_row_entries_set_list[i].intersection(self._pos_row_entries_set_list[j]))

		beta_u = set([v for v in self._pos_row_entries_set_list[i] if v != u and len(n1uv[v]) >= beta])
		beta_i = set([j for j in self._pos_col_entries_set_list[u] if j != i and len(n2ij[j]) >= beta])

		beta_ui = sorted([(v, j) for v in beta_u for j in beta_i if self._data_matrix[v][j] > 0], key=lambda x:x[1])

		if len(beta_ui) == 0:
			return None

		if len(beta_ui) > num_points:
			beta_ui = nsmallest(num_points, beta_ui, key=lambda x: abs(x[1]-i))

		print(beta_ui)

		estimate = 0
		total_weight = 0
		for (v, j) in beta_ui:
			s_uv_square = self._suv_square(u, v, n1uv[v])
			s_ij_square = self._sij_square(i, j, n2ij[j])
			w_vj = math.exp(-lam*min(s_uv_square, s_ij_square))
			estimate += w_vj*(self._data_matrix[u][j] + self._data_matrix[v][i] - self._data_matrix[v][j])
			total_weight += w_vj

		return estimate / total_weight

	def estimate_gaussian_cache(self, u, i_vec, beta=2, lam=1, num_points=20):

		if beta < 2:
			print('ERROR: Invalid beta')
			return None

		estimate_result = []
		print('i_vec', i_vec)
		sys.stdout.flush()
		# calculate "N1(u,v)"
		n1uv = []
		for v in range(self._dimension[0]):
			n1uv.append(self._pos_col_entries_set_list[u].intersection(self._pos_col_entries_set_list[v]))

		s_uv_square_cache = {}

		for i in i_vec:
			print(i)
			# calculate "N2(i,j)"
			n2ij = []
			for j in range(self._dimension[1]):
				n2ij.append(self._pos_row_entries_set_list[i].intersection(self._pos_row_entries_set_list[j]))

			beta_u = set([v for v in self._pos_row_entries_set_list[i] if v != u and len(n1uv[v]) >= beta])
			beta_i = set([j for j in self._pos_col_entries_set_list[u] if j != i and len(n2ij[j]) >= beta])

			beta_ui = sorted([(v, j) for v in beta_u for j in beta_i if self._data_matrix[v][j] > 0], key=lambda x: x[1])

			if len(beta_ui) == 0:
				estimate_result.append([i, None])
				continue

			if len(beta_ui) > num_points:
				beta_ui = nsmallest(num_points, beta_ui, key=lambda x: abs(x[1] - i))

			estimate = 0
			total_weight = 0
			for (v, j) in beta_ui:
				if v in s_uv_square_cache:
					s_uv_square = s_uv_square_cache[v]
				else:
					s_uv_square = self._suv_square(u, v, n1uv[v])
					s_uv_square_cache[v] = s_uv_square

				s_ij_square = self._sij_square(i, j, n2ij[j])

				w_vj = math.exp(-lam * min(s_uv_square, s_ij_square))
				estimate += w_vj * (self._data_matrix[u][j] + self._data_matrix[v][i] - self._data_matrix[v][j])
				total_weight += w_vj

			estimate_result.append((i, estimate/total_weight))

		return estimate_result

	def estimate_gaussian_cache_2D(self, u_vec, i_vec, beta=2, lam=1, num_points=20):
		if beta < 2:
			print('ERROR: Invalid beta')
			return None

		s_uv2_cache = {}
		s_ij2_cache = {}
		estimate_result = {}

		counter = 0
		for u in u_vec:
			counter += 1
			if counter % 100 == 0:
				current_ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
				print(counter, '/', len(u_vec), 'done', current_ts)
			estimate_result[u] = []
			for i in i_vec:
				# calculate "N1(u,v)"
				n1uv = []
				for v in range(self._dimension[0]):
					n1uv.append(self._pos_col_entries_set_list[u].intersection(self._pos_col_entries_set_list[v]))
				# calculate "N2(i,j)"
				n2ij = []
				for j in range(self._dimension[1]):
					n2ij.append(self._pos_row_entries_set_list[i].intersection(self._pos_row_entries_set_list[j]))

				beta_u = set([v for v in self._pos_row_entries_set_list[i] if v != u and len(n1uv[v]) >= beta])
				beta_i = set([j for j in self._pos_col_entries_set_list[u] if j != i and len(n2ij[j]) >= beta])

				beta_ui = sorted([(v, j) for v in beta_u for j in beta_i if self._data_matrix[v][j] > 0], key=lambda x: x[1])

				if len(beta_ui) == 0:
					estimate_result[u].append([i, None])
					continue

				if len(beta_ui) > num_points:
					beta_ui = nsmallest(num_points, beta_ui, key=lambda x: abs(x[1] - i))

				estimate = 0
				total_weight = 0
				for (v, j) in beta_ui:
					if (u, v) in s_uv2_cache:
						s_uv_square = s_uv2_cache[(u, v)]
					elif (v, u) in s_uv2_cache:
						s_uv_square = s_uv2_cache[(v, u)]
					else:
						s_uv_square = self._suv_square(u, v, n1uv[v])
						s_uv2_cache[(u, v)] = s_uv_square

					if (i, j) in s_ij2_cache:
						s_ij_square = s_ij2_cache[(i, j)]
					elif (j, i) in s_ij2_cache:
						s_ij_square = s_ij2_cache[(j, i)]
					else:
						s_ij_square = self._sij_square(i, j, n2ij[j])
						s_ij2_cache[(i, j)] = s_ij_square

					w_vj = math.exp(-lam * min(s_uv_square, s_ij_square))
					estimate += w_vj * (self._data_matrix[u][j] + self._data_matrix[v][i] - self._data_matrix[v][j])
					total_weight += w_vj

				estimate_result[u].append((i, estimate / total_weight))

		return estimate_result

	def estimate_gaussian_cache_2D_all(self, beta=2, lam=1, num_points=20):
		if beta < 2:
			print('ERROR: Invalid beta')
			return None

		s_uv2_cache = {}
		s_ij2_cache = {}
		estimate_result = -1*np.ones(shape=self._dimension)

		counter = 0
		print("Dimensions:",str(self._dimension[0]),str(self._dimension[1]))
		for row in range(self._dimension[0]):
			print("At row",row)
			counter += 1
			if counter % 10 == 0:
				current_ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
				print(counter, '/', self._dimension[0], 'done', current_ts)
			for col in range(self._dimension[1]):
				print("At col",col)
				# calculate "N1(u,v)"
				intersect_col_entries = []
				for v in range(self._dimension[0]):
					#print("Evaluating at row",v)
					intersect_col_entries.append(self._pos_col_entries_set_list[row].intersection(self._pos_col_entries_set_list[v]))
#					print("nonzer _entries is ",str(self._pos_col_entries_set_list[v]))
					#print("intersect_col_entries is ",str(intersect_col_entries[v]))
				# calculate "N2(i,j)"
				intersect_row_entries = []
				for j in range(self._dimension[1]):
					#print("Evaluating at col",j)
					intersect_row_entries.append(self._pos_row_entries_set_list[col].intersection(self._pos_row_entries_set_list[j]))
					#print("intersect_row_entries is ",str(sorted(intersect_row_entries[j])))

				best_row_neighbors = set([other_row for other_row in self._pos_row_entries_set_list[col] if other_row != row and len(intersect_col_entries[other_row]) >= beta])
				#print("beta",beta,"Beta_row",str(sorted(best_row_neighbors)))
				best_col_neighbors = set([other_col for other_col in self._pos_col_entries_set_list[row] if other_col != col and len(intersect_row_entries[other_col]) >= beta])
				#print("beta",beta,"Beta_col",str(sorted(best_col_neighbors)))
				#print("Beta_col",str(best_col_neighbors))

				best_neighbor_tuples = sorted([(v, j) for v in best_row_neighbors for j in best_col_neighbors if self._data_matrix[v][j] > 0], key=lambda x: x[1])

				#print("best neighbor tuples",str(best_neighbor_tuples))
				print("best_neighbor_tuples length",str(len(best_neighbor_tuples)))
				if len(best_neighbor_tuples) == 0:
					continue

				if len(best_neighbor_tuples) > num_points:
					best_neighbor_tuples = nsmallest(num_points, best_neighbor_tuples, key=lambda x: abs(x[1] - col))
				print("num_points",num_points,"length",str(len(best_neighbor_tuples)),"best_neighbor_tuples",str(best_neighbor_tuples))

				estimate = 0
				total_weight = 0
				print("Working on cache")
				for (v, j) in best_neighbor_tuples:
					if (row, v) in s_uv2_cache:
						s_uv_square = s_uv2_cache[(row, v)]
					elif (v, row) in s_uv2_cache:
						s_uv_square = s_uv2_cache[(v, row)]
					else:
						s_uv_square = self._suv_square(row, v, intersect_col_entries[v])
						s_uv2_cache[(row, v)] = s_uv_square

					if (col, j) in s_ij2_cache:
						s_ij_square = s_ij2_cache[(col, j)]
					elif (j, col) in s_ij2_cache:
						s_ij_square = s_ij2_cache[(j, col)]
					else:
						s_ij_square = self._sij_square(col, j, intersect_row_entries[j])
						s_ij2_cache[(col, j)] = s_ij_square

					w_vj = math.exp(-lam * min(s_uv_square, s_ij_square))
					estimate += w_vj * (self._data_matrix[row][j] + self._data_matrix[v][col] - self._data_matrix[v][j])
					total_weight += w_vj

				estimate_result[row][col] = estimate / total_weight
				print("Worked on cache")

		return estimate_result

	def _suv_square(self, u, v, uv_set):

		result = 0

		for i in uv_set:
			for j in uv_set:
				diff = (self._data_matrix[u][i] - self._data_matrix[v][i]) - (self._data_matrix[u][j] - self._data_matrix[v][j])
				result += math.pow(diff, 2)

		return result / (2*len(uv_set)*(len(uv_set)-1))

	def _sij_square(self, i, j, ij_set):

		result = 0

		for u in ij_set:
			for v in ij_set:
				diff = (self._data_matrix[u][i] - self._data_matrix[u][j]) - (self._data_matrix[v][i] - self._data_matrix[v][j])
				result += math.pow(diff, 2)

		return result / (2 * len(ij_set) * (len(ij_set) - 1))


# coding=utf-8
# Copyright 2021 The Edward2 Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Normalization layers.

## References:

[1] Takeru Miyato, Toshiki Kataoka, Masanori Koyama, Yuichi Yoshida.
    Spectral normalization for generative adversarial networks.
    In _International Conference on Learning Representations_, 2018.

[2] Farzan Farnia, Jesse M. Zhang, David Tse.
    Generalizable Adversarial Training via Spectral Normalization.
    In _International Conference on Learning Representations_, 2019.

[3] Stephan Hoyer.
    Better spectral norm for convolutions.
    https://nbviewer.jupyter.org/gist/shoyer/fa9a29fd0880e2e033d7696585978bfc
"""

from typing import Any, Callable, Mapping, Optional, Tuple

import flax.core
import flax.linen as nn
import jax
import jax.numpy as jnp
import numpy as np

# conventional Flax types
Array = Any
Dtype = Any
PRNGKey = Any
Shape = Tuple[int]

def _l2_normalize(x, eps=1e-12):
  return x * jax.lax.rsqrt(jnp.maximum(jnp.square(x).sum(), eps))

class SpectralNormalization(nn.Module):
  """Implements spectral normalization for linear layers.

  In Flax, parameters are immutable so we cannot modify the parameters of
  the input layer during the transformation. As a resolution, we will move
  all parameters of the input layer to this spectral normalization layer.
  During the transformation, we will modify the weight and call the input
  layer with the updated weight.
  For example, the pattern for parameters with a Dense input layer will be
    {"Dense": {"weight": ..., "bias": ...}}
  which matches the pattern in Flax as if "Dense" is a submodule of this
  spectral normalization layer. Name of the input layer can be customized
  using the `layer_name` attribute.

  Note that currently, the implementation with a customized
  ``kernel_apply_kwargs`` attribute only works for Flax's Dense, Conv, Embed
  (or their subclasses) input layers.

  Attributes:
    layer: a Flax layer to apply normalization to.
    iteration: the number of power iterations to estimate weight matrix's
      singular value.
    norm_multiplier: multiplicative constant to threshold the normalization.
      Usually under normalization, the singular value will converge to this
      value.
    u_init: initializer function for the first left singular vectors of the
      kernel.
    v_init: initializer function for the first right singular vectors of the
      kernel.
    kernel_apply_kwargs: updated keyword arguments to clone the input layer. The
      cloned layer represents the linear operator performed by the weight
      matrix. If not specified, that operator follows SN-GAN implementation
      (reference [1]). In particular, for Dense layers the default behavior is
      equivalent to using a cloned layer with no bias (by specifying
      ``kernel_apply_kwargs=dict(use_bias=False)``). With this customization, we
      can have the same implementation (inspried by reference [3]) for different
      interpretations of Conv layers. Also see SpectralNormalizationConv2D for
      an example of using this attribute.
    kernel_name: name of the kernel parameter of the input layer.
    layer_name: name of the input layer.
  """
  layer: nn.Module
  iteration: int = 1
  norm_multiplier: float = 0.95
  u_init: Callable[[PRNGKey, Shape, Dtype],
                   Array] = nn.initializers.normal(stddev=0.05)
  v_init: Callable[[PRNGKey, Shape, Dtype],
                   Array] = nn.initializers.normal(stddev=0.05)
  kernel_apply_kwargs: Optional[Mapping[str, Any]] = None
  # TODO(phandu): Allow users to provide a list of kernel names, so that
  # we can use SN-GAN interpretation for SeparableConv2D layers.
  kernel_name: str = "kernel"
  layer_name: Optional[str] = None

  def _get_singular_vectors(self, initializing, kernel_apply, in_shape, dtype):
    if initializing:
      rng_u = self.make_rng("params")
      rng_v = self.make_rng("params")
      # Interpret output shape (not that this does not cost any FLOPs).
      out_shape = jax.eval_shape(kernel_apply,
                                 jax.ShapeDtypeStruct(in_shape, dtype)).shape
    else:
      rng_u = rng_v = out_shape = None
    u = self.variable("spectral_stats", "u", self.u_init, rng_u, out_shape,
                      dtype)
    v = self.variable("spectral_stats", "v", self.v_init, rng_v, in_shape,
                      dtype)
    return u, v

  @nn.compact
  def __call__(self, inputs: Array, training: bool = True) -> Array:
    """Applies a linear transformation with spectral normalization to the inputs.

    Args:
      inputs: The nd-array to be transformed.
      training: Whether to perform power interations to update the singular
        value estimate.

    Returns:
      The transformed input.
    """
    layer_name = type(
        self.layer).__name__ if self.layer_name is None else self.layer_name
    params = self.param(layer_name,
                        lambda *args: self.layer.init(*args)["params"], inputs)
    w = params[self.kernel_name]

    if self.kernel_apply_kwargs is None:
      # By default, we use the implementation in SN-GAN.
      kernel_apply = lambda x: w.reshape(-1, w.shape[-1]) @ x
      in_shape = (np.prod(w.shape[:-1]),)
    else:
      # Otherwise, we extract the actual kernel transformation in the input
      # layer. This is useful for Conv2D spectral normalization in [2].
      kernel_apply = self.layer.clone(**self.kernel_apply_kwargs).bind(  # pylint: disable=not-a-mapping
          {"params": {
              self.kernel_name: w
          }})
      # Compute input shape of the kernel operator. This is correct for all
      # linear layers on Flax: Dense, Conv, Embed.
      in_shape = inputs.shape[-w.ndim + 1:-1] + w.shape[-2:-1]

    initializing = self.is_mutable_collection("params")
    u, v = self._get_singular_vectors(initializing, kernel_apply, in_shape,
                                      w.dtype)
    u_hat, v_hat = u.value, v.value
    u_, kernel_transpose = jax.vjp(kernel_apply, v_hat)
    if training and not initializing:
      # Run power iterations using autodiff approach inspired by [3].
      def scan_body(carry, _):
        u_hat, v_hat, u_ = carry
        v_, = kernel_transpose(u_hat)
        v_hat = _l2_normalize(v_)
        u_ = kernel_apply(v_hat)
        u_hat = _l2_normalize(u_)
        return (u_hat, v_hat, u_), None

      (u_hat, v_hat, u_), _ = jax.lax.scan(
          scan_body, (u_hat, v_hat, u_), None, length=self.iteration)
      u.value, v.value = u_hat, v_hat

    sigma = jnp.vdot(u_hat, u_)
    # Bound spectral norm by the `norm_multiplier`.
    sigma = jnp.maximum(sigma / self.norm_multiplier, 1.)
    w_hat = w / jax.lax.stop_gradient(sigma)
    self.sow("intermediates", "w", w_hat)

    # Update params.
    params = flax.core.unfreeze(params)
    params[self.kernel_name] = w_hat
    layer_params = flax.core.freeze({"params": params})
    return self.layer.apply(layer_params, inputs)

class SpectralNormalizationConv2D(SpectralNormalization):
  __doc__ = "Implements spectral normalization for Conv layers based on [2].\n" + "\n".join(
      SpectralNormalization.__doc__.split("\n")[1:])

  kernel_apply_kwargs: Mapping[str, Any] = flax.core.FrozenDict(
      feature_group_count=1, padding="SAME", use_bias=False)


import numpy as np
import cv2
import imutils
import mouse
from cv2 import imshow as p
cap = cv2.VideoCapture(0)

cap.set(3,1900)
cap.set(4,1080)

x,y = mouse.get_position()
while(True):

    ret, img = cap.read()
    img2 = img.copy()
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    lower_yellow = np.array([20,100,100])
    upper_yellow = np.array([30,255,255])

    lower_blue = np.array([110,50,50])
    upper_blue = np.array([130,255,255])

    mask_yellow = cv2.inRange(hsv, lower_yellow, upper_yellow)
    mask_blue = cv2.inRange(hsv, lower_blue, upper_blue)

    res = cv2.bitwise_and(img,img, mask= mask_yellow)
    imgray = cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)
    ret,thresh = cv2.threshold(imgray,127,255,0)
    _, contours, _= cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for c in contours:
        if len(c)>30:
            M = cv2.moments(c)
            if M["m00"] != 0:
                cX = int(M["m10"] / M["m00"])
                cY = int(M["m01"] / M["m00"])
                mouse.move(x-cX,cY-y,absolute = False,duration=0)
                x = cX
                y = cY
            else:
                cX, cY = mouse.get_position()

        else:
            x = x
            y = y

    res = cv2.bitwise_and(img,img, mask= mask_blue)
    imgray = cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)
    ret,thresh2 = cv2.threshold(imgray,127,255,0)
    _, contours, _= cv2.findContours(thresh2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cv2.imshow("",res)

##    but = mouse.is_pressed(button='left')
##    a = [len(c) for c in contours]
##    a.sort(reverse=True)
##    #print (a[0])
##    try:
##        if a[0] > 40:
##            if but:
##                pass
##            else:
##                mouse.press(button='left')
##        else:
##            mouse.release(button='left')
##    except:
##        pass

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()


from json import dumps, loads
from models.match import Match
from controller.combat_controller import CombatManagerController

from flask_restplus import reqparse

class MatchController:
    def __init__(self, request):
        self.request = request

    def new(self):
        """
        Creates a new match
        """
        parser = reqparse.RequestParser()
        parser.add_argument('name', required=True)
        parser.add_argument('events', action='append')
        parser.add_argument('description')
        parser.add_argument('campaign', required=True)

        parse_result = parser.parse_args(req=self.request)

        # Document.from_json() gets a string as an argument, so we need to use `json.dumps()` here
        match = Match.from_json(dumps(parse_result)).save()

        return "{}".format(match.id)

    @staticmethod
    def list():
        """
        Makes a query to list all matches
        """
        list_of_matches = list(
            map(lambda match: loads(match.to_json()), Match.objects.all()))
        return list_of_matches

    @staticmethod
    def get_element_detail(identifier):
        """
        Returns an match campaigning the given id
        """
        return Match.objects.get(id=identifier).to_json()

    def edit(self, identifier):
        """
        Edits an match given its id
        """
        match = Match.objects.get(id=identifier)

        parser = reqparse.RequestParser()
        parser.add_argument('name')
        parser.add_argument('events', action='append')
        parser.add_argument('description')
        parser.add_argument('campaign')

        parse_result = parser.parse_args(req=self.request)

        no_docs_updated = match.update(**parse_result)

        if no_docs_updated == 1:  # the row was updated successfully
            new_match = Match.objects.get(id=identifier)
            return loads(new_match.to_json())

    @staticmethod
    def delete(identifier):
        """
        Deletes an match given its id
        """
        target = Match.objects.get(id=identifier)
        target_data = loads(target.to_json())

        target.delete()

        return target_data

    def start_battle(self, identifier):

        controller = CombatManagerController(self.request)
        battle_id = controller.new()

        target = Match.objects.get(id=identifier)
        target.update(push__battles=battle_id)
        target = Match.objects.get(id=identifier)

        return(loads(target.to_json()))


# -*- coding: utf-8 -*-
# Copyright (C) Canux CHENG <canuxcheng@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""Base class for all Sharepoint 2013 plugins."""

import logging
import traceback
from monitoring.nagios.plugin import NagiosPluginHTTP

from powershell import XMLTable, XMLSerializedTable
from powershell.xml.exceptions import XMLValidityError

logger = logging.getLogger('plugin.base')

class PluginBase(NagiosPluginHTTP):
    """Base class for all Exchange plugins."""
    def __init__(self, *args, **kwargs):
        super(PluginBase, self).__init__(*args, **kwargs)
        self._alerts = {
            'warning': [],
            'critical': [],
        }
        self.have_criticals = False
        self.have_warnings = False

    def run(self):
        """Run the plugin."""
        try:
            self.main()
        except Exception:
            self.shortoutput = 'Unexpected plugin error ! Please investigate.'
            self.longoutput = traceback.format_exc().splitlines()
            self.unknown(self.output())

    def main(self):
        """Main entry point for the plugin."""
        raise NotImplementedError('Main entry point is not implemented !')

    def fetch_xml_table(self):
        """Helper to fetch the XML via HTTP and parse it."""
        response = self.http.get(self.options.path)

        try:
            xml_table = XMLTable(response.content)
            logger.debug('XML Table: %s', xml_table)
            return xml_table
        except XMLValidityError:
            try:
                xml_table = XMLSerializedTable(response.content)
                logger.debug('XML Serialized Table: %s', xml_table)
                return xml_table
            except XMLValidityError:
                self.shortoutput = 'XML format is not valid !'
                self.longoutput = traceback.format_exc().splitlines()
                self.critical(self.output())

    def add_critical_result(self, crit_result):
        """
        Add a critical result.

        Used in longoutput to show the result in a CRITICAL section.
        """
        self._alerts['critical'].append(crit_result)
        self.have_criticals = True

    def add_warning_result(self, warn_result):
        """
        Add a warning result.

        Used in longoutput to show the result in a WARNING section.
        """
        self._alerts['warning'].append(warn_result)
        self.have_warnings = True


import json
import urbackup_api
import sys
import time
import re
# Example: python3 urbackup-stats.py "127.0.0.1"

def worker(url, user, password):
    server = urbackup_api.urbackup_server(url, user, password)
    status = server.get_status()
    now = int(time.time())
    # Metrics definition
    vms = 0
    vms_fail = 0
    desktops = 0
    desktops_fail = 0
    laptops = 0
    laptops_fail = 0
    servers = 0
    servers_fail = 0
    backup_new = now
    backup_old = 0
    obj_total = len(status)
    obj_online = 0
    obj_nogroup = 0
    obj_active = 0
    obj_disabled = 0
    # Calculation
    for item in status:
        if "image_disabled" in item:
            obj_disabled += 1
        if item['delete_pending'] != 0:
            current = now - item["lastbackup_image"]
            if backup_new > current:
                backup_new = current
            if backup_old < current:
                backup_old = current
            if item["online"]:
                obj_online += 1
            if item["status"] == 3 or item["status"] == 4:
                obj_active += 1
            if re.search(r"^vms", item["groupname"]):
                vms += 1
                if not item["image_ok"]:
                    vms_fail += 1
            elif re.search(r"^desktops", item["groupname"]):
                desktops += 1
                if not item["image_ok"]:
                    desktops_fail += 1
            elif re.search(r"^laptops", item["groupname"]):
                laptops += 1
                if not item["image_ok"]:
                    laptops_fail += 1
            elif re.search(r"^servers", item["groupname"]):
                servers += 1
                if not item["image_ok"]:
                    servers_fail += 1
            else:
                obj_nogroup += 1
    print(json.dumps({"data": [{
        "{#URL}": url,
        "{#TOTAL}": obj_total,
        "{#ONLINE}": obj_online,
        "{#NOGROUP}": obj_nogroup,
        "{#ACTIVE}": obj_active,
        "{#DISABLED}": obj_disabled,
        "{#NEW}": backup_new,
        "{#OLD}": backup_old,
        "{#VMS}": vms,
        "{#DESKTOPS}": desktops,
        "{#LAPTOPS}": laptops,
        "{#SERVERS}": servers,
        "{#VMSFAIL}": vms_fail,
        "{#DESKTOPSFAIL}": desktops_fail,
        "{#LAPTOPSFAIL}": laptops_fail,
        "{#LAPTOPSFAIL}": laptops_fail,
        "{#SERVERSFAIL}": servers_fail
    }]}))

if __name__ == "__main__":
    worker("http://" + sys.argv[1] + ":55414/x", "user", "npu0H4Gk6r")


from django.shortcuts import render
from django import http
from polls.models import Poll

def poll_detail(request, poll_id):
    poll = Poll.objects.get(pk=poll_id)

    # Registra o voto passado via GET
    if request.GET:
        user = request.user
        if user not in poll.voted_users.all():
            index = int(request.GET['item'])
            poll.vote_for(index, user)

    context = {
        'poll': poll,
        'options': poll.items.all(),
    }
    return render(request, 'polls/detail.html', context)

UnB/11o semestre/DAS/Aulas com o Fabio/demos (calc e django demo)/django-demo/mysite/polls/views.py