from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
import json
# import datetime
from datetime import datetime
from .models import Departments,Roles
from str2bool import str2bool
from django.http import JsonResponse
import ast
from django.core.paginator import Paginator

# Create your views here.

@csrf_exempt
def adding_departments(request):
    status = ''
    message = ''
    status_code = ''

    try:
        requestData = json.loads(request.body)
    except:
        requestData = request.POST

    try:
        departments_name = requestData.get(u"departments_name", None)
        added_by = requestData.get(u"added_by", None)
        added_date = datetime.now()

        # [{"name":"Android","status":"true"}, {"name":"Web","status":"true"}]
        departments_name = ast.literal_eval(str(departments_name))

        try:
            for i in departments_name:
            #for i in jintu
                Departments.objects.get_or_create(department_name=(i['name']),\
                                                defaults={'status': str2bool(i['status']), 'added_by': added_by,
                                                            'added_datetime': added_date})
                #checking department_name is already exist
                status = "Success"
                message = "Successfully Departments created"
                status_code = 200
        except Exception as e:
            import traceback
            traceback.print_exc()
            status = "Failed"
            message = str(e)
            status_code = 400

    except Exception as e:
        status = "Failed"
        message = str(e)
        status_code = 400
    return JsonResponse({"status": status, "message": message, "status_code": status_code})

@csrf_exempt
def getting_departments(request):
    status = ''
    message = ''
    status_code = ''
    total_page = ''
    index_page = ''
    json_data = []
    data = dict()
    try:
        requestData = json.loads(request.body)
    except:
        requestData = request.POST
    try:
        page = requestData.get(u"page", None)
        total_departments = Departments.objects.all()

        if total_departments.exists():
            departments_paginator = Paginator(total_departments, 10)
            if int(page) > 0:
                pag_num = departments_paginator.get_page(page)
                n_deppg = str(pag_num)[1:-1]
                n = [int(i) for i in n_deppg.split() if i.isdigit()]
                total_page = str(n[1])

                if int(page) <= int(total_page):
                    for obj in pag_num:
                        data1 = {"primary_key": obj.primary_key, "department_name": obj.department_name,\
                                 "status": obj.status,\
                                 "added_by": obj.added_by, "added_datetime": obj.added_datetime,\
                                 "updated_datetime": obj.updated_datetime}
                        json_data.append(data1)

                    index_page = str(n[0])
                    status = "Success"
                    message = "Successfully received the departments"
                    status_code = 200
                else:
                    status = "Failed"
                    message = "Total department pages across the limit"
                    status_code = 409
            else:
                status = "Failed"
                message = "Total department pages beyond the limit"
                status_code = 409
    except Exception as e:
        status = "Failed"
        message = str(e)
        status_code = 404
    return JsonResponse({"status": status, "data": json_data, "message": message, \
                         "status_code": status_code, "total_page": total_page, "index_page": index_page})

###updating departments
@csrf_exempt
def updating_departments(request):
    status = ''
    message = ''
    status_code = ''

    try:
        requestData = json.loads(request.body)
    except:
        requestData = request.POST

    try:
        primary_key = requestData.get(u"primary_key", None)
        new_department_name = requestData.get(u"new_department_name", None)
        new_status = requestData.get(u"new_status", None)
        updated_datetime = datetime.now()

        try:
            get_departments = Departments.objects.get(primary_key=primary_key)

            if new_department_name != None and new_department_name != "":
                check_department = Departments.objects.filter(department_name__iexact=new_department_name)
                                                            #(department_name__iexact = "jintu")

                if check_department.exists() == False:
                    #jintu in not there
                    get_departments.department_name = new_department_name
                    get_departments.status = str2bool(new_status)
                    get_departments.updated_datetime = updated_datetime
                    get_departments.save()

                    status = "Success"
                    message = "Department details updated successfully"
                    status_code = 200
                else:
                    status = "Failed"
                    message = "Department name already exists"
                    status_code = 409
            else:
                status = "Failed"
                message = "Department name cannot be empty"
                status_code = 403
        except Exception as e:
            status = "Failed"
            message = str(e)
            status_code = 400
    except Exception as e:
        status = "Failed"
        message = str(e)
        status_code = 400
    return JsonResponse({"status": status, "message": message, "status_code": status_code})

###adding roles
@csrf_exempt
def adding_roles(request):
    status = ''
    message = ''
    status_code = ''

    try:
        requestData = json.loads(request.body)
    except:
        requestData = request.POST

    try:
        department_name = requestData.get(u"department_name", None)
        role_name = requestData.get(u"role_name", None)
        added_by = requestData.get(u"added_by", None)
        added_date = datetime.now()

        # [{"name":"Role","status":"true"}, {"name":"Role1","status":"true"}]
        role_name = ast.literal_eval(str(role_name))
        try:
            departments = Departments.objects.get(primary_key=department_name)
            for i in role_name:
                Roles.objects.get_or_create(department_name=departments, \
                                            role_name=(i['name']), \
                                            defaults={'status': str2bool(i['status']), 'added_by': added_by,
                                                      'added_datetime': added_date})
                status = "Success"
                message = "Roles was created successfully"
                status_code = 200
        except Exception as e:
            status = "Failed"
            message = str(e)
            status_code = 404
    except Exception as e:
        status = "Failed"
        message = str(e)
        status_code = 404
    return JsonResponse({"status": status, "message": message, "status_code": status_code})

###getting roles
@csrf_exempt
def getting_roles(request):
    status = ''
    message = ''
    status_code = ''
    index_page = ''
    total_page = ''
    json_data = []
    data = dict()

    try:
        requestData = json.loads(request.body)
    except:
        requestData = request.POST
    try:
        page = requestData.get(u"page", None)
        department_name = requestData.get(u"department_name")

        departments = Departments.objects.get(primary_key=department_name)
        total_roles = Roles.objects.filter(department_name=departments)

        if total_roles.exists():
            roles_paginator = Paginator(total_roles, 10)
            if int(page) > 0:
                pag_num = roles_paginator.get_page(page) #page = 1,2,3,4... whatever
                n_rolepg = str(pag_num)[1:-1]
                # print("n_rolepg:"n_rolepg) -> o/p n_rolepg: input_page of total page
                n = [int(i) for i in n_rolepg.split() if i.isdigit()]
                total_page = str(n[1])

                if int(page) <= int(total_page):
                    for obj in pag_num:
                        data1 = {"primary_key": obj.primary_key,\
                                 "role_name": obj.role_name, "status": obj.status, "added_by": obj.added_by,\
                                 "added_datetime": obj.added_datetime, "updated_datetime": obj.updated_datetime}
                        json_data.append(data1)

                    index_page = str(n[0])
                    status = "Success"
                    message = "Successfully received the roles"
                    status_code = 200
                else:
                    status = "Failed"

                    message = "Total roles pages across the limit"
                    status_code = 409
            else:
                status = "Failed"
                message = "Total roles pages beyond the limit"
                status_code = 409

    except Exception as e:
        status = "Failed"
        message = str(e)
        status_code = 400
    return JsonResponse({"status": status, "data": json_data, "message": message, \
                         "status_code": status_code, "total_page": total_page, "index_page": index_page})

###update the roles
@csrf_exempt
def updating_roles(request):
    status = ''
    message = ''
    status_code = ''
    try:
        requestData = json.loads(request.body)
    except:
        requestData = request.POST
    try:
        primary_key = requestData.get(u"primary_key", None)
        department_name = requestData.get(u"department_name", None)
        new_role_name = requestData.get(u"new_role_name", None)
        new_status = requestData.get(u"status", None)
        updated_datetime = datetime.now()

        try:
            departments = Departments.objects.get(primary_key=department_name)
            get_roles = Roles.objects.get(primary_key=primary_key, department_name=departments)

            if new_role_name != None and new_role_name != "":
                check_role = Roles.objects.filter(role_name__iexact=new_role_name) #check the new_role name is exist or not
                if check_role.exists() == False: #if exist is FAlse value . mean not exist the new role name . It will update with new value
                    get_roles.role_name = new_role_name
                    get_roles.status = str2bool(new_status)
                    get_roles.updated_datetime = updated_datetime
                    get_roles.save()

                    status = "Success"
                    message = "Successfully update the roles"
                    status_code = 200
                else:
                    status = "Failed"
                    message = "Roles name already exists"
                    status_code = 409
            else:
                status = "Failed"
                message = "Roles name can not be empty"
                status_code = 403

        except Exception as e:
            status = "Failed"
            message = str(e)
            status_code = 400
    except Exception as e:
        status = "Failed"
        message = str(e)
        status_code = 400
    return JsonResponse({"status": status, "message": message, "status_code": status_code})


from rest_framework.exceptions import NotAcceptable

class NumberTranslationService:

    MAX_LIMIT_ERROR = 'Número fora do limite válido'
    ZERO = 'zero'
    CEM = 'cem'

    UNITS = {
        '1': 'um',
        '2': 'dois',
        '3': 'três',
        '4': 'quatro',
        '5': 'cinco',
        '6': 'seis',
        '7': 'sete',
        '8': 'oito',
        '9': 'nove'
    }

    TEN_TO_NINETEEN = {
        '10': 'dez',
        '11': 'onze',
        '12': 'doze',
        '13': 'treze',
        '14': 'quatorze',
        '15': 'quinze',
        '16': 'dezesseis',
        '17': 'dezessete',
        '18': 'dezoito',
        '19': 'dezenove'
    }

    DOZENS = {
        '2': 'vinte',
        '3': 'trinta',
        '4': 'quarenta',
        '5': 'cinquenta',
        '6': 'sessenta',
        '7': 'setenta',
        '8': 'oitenta',
        '9': 'noventa'
    }

    HUNDREDS = {
        '2': 'duzentos',
        '3': 'trezentos',
        '4': 'quatrocentos',
        '5': 'quinhentos',
        '6': 'seiscentos',
        '7': 'setecentos',
        '8': 'oitocentos',
        '9': 'novecentos'
    }

    THOUSANDS = {
        '1': 'mil',
        '2': 'dois mil',
        '3': 'três mil',
        '4': 'quatro mil',
        '5': 'cinco mil',
        '6': 'seis mil',
        '7': 'sete mil',
        '8': 'oito mil',
        '9': 'nove mil'
    }

    def get_number_in_portuguese(self, number, result=""):
        """
        Given a number, returns it extension name in portuguese

        :param number: a valid number between -99999 and 99999
        :param result: the final result as string
        :return: result
        """
        number_as_str = str(number)

        # Check if the first char is a "-" sign
        first_position = number_as_str[0]
        if "-" == first_position:
            result = "menos"
            # Removes the negative sign from number
            return self.get_number_in_portuguese(number=number_as_str[1::], result=result)

        number_len = len(number_as_str)

        if number_len > 1 and self._is_zero_sequence(number_as_str):
            # the rest of the number ends in a zero sequence
            return result.strip()

        if first_position == '0':
            if number_len > 1:
                # Cut off the leading zero
                return self.get_number_in_portuguese(number=number_as_str[1::], result=result)
            if not result or result == '-':
                # The number is zero
                return self.ZERO

        if number_len > 5:
            # Out of range
            raise NotAcceptable(detail=self.MAX_LIMIT_ERROR)

        if number_len == 5:
            # Extract the dozen-thounsands
            first_two_positions = number_as_str[0] + number_as_str[1]
            result = ' '.join([result, self._get_two_digits_number_in_extension(first_two_positions), 'mil'])

            if self._is_zero_sequence(number_as_str[2::]):
                # Number ends in a zero sequence
                return result.strip()
            result = ' '.join([result, 'e'])

            return self.get_number_in_portuguese(number=number_as_str[2::], result=result)

        if number_len == 4:
            result = ' '.join([result, self.THOUSANDS[first_position]])

            if self._is_zero_sequence(number_as_str[1::]):
                # Number ends in a zero sequence
                return result.strip()
            result = ' '.join([result, 'e'])

            return self.get_number_in_portuguese(number=number_as_str[1::], result=result)

        if number_len == 3:
            is_following_zeros = self._is_zero_sequence(number_as_str[1::])

            if first_position == '1':
                # Number ends in 1xx
                if is_following_zeros:
                    # Number is 100
                    result = ' '.join([result, self.CEM])
                    return result.strip()
                result = ' '.join([result, 'cento e'])
                return self.get_number_in_portuguese(number=number_as_str[1::], result=result)
            result = ' '.join([result, self.HUNDREDS[first_position]])
            if is_following_zeros:
                # Number ends in a zero sequence
                return result.strip()
            result = ' '.join([result, 'e'])
            return self.get_number_in_portuguese(number=number_as_str[1::], result=result)

        if number_len == 2:
            result = ' '.join([result, self._get_two_digits_number_in_extension(number_as_str)])
            return result.strip()

        if number_len == 1:
            result = ' '.join([result, self.UNITS[number_as_str]])

        return result.strip()

    def _get_two_digits_number_in_extension(self, numeric_string):
        first_position = numeric_string[0]

        if first_position == '1':
            # Number is 10-19
            return self.TEN_TO_NINETEEN[numeric_string]

        # Number is 20-99
        result = self.DOZENS[first_position]
        second_position = numeric_string[1]
        if second_position == '0':
            # Number has two digitis, ending in zero
            return result
        # Number has two digits, ending in a number 1-9
        return ' e '.join([result, self.UNITS[second_position]])

    def _is_zero_sequence(self, numeric_string):
        """ Returns true if numeric_string is composed by zeros """
        for number in numeric_string:
            if number != '0':
                return False
        return True


#!/usr/bin/python3

import sys
import math
import numpy as np
from pca import pca
from gaussian import gaussian

if len(sys.argv)!=7:
  print('Usage: %s <trdata> <trlabels> <ks> <ds> <%%trper> <%%dvper>' % sys.argv[0]);
  sys.exit(1);

X= np.load(sys.argv[1])['X'];
xl=np.load(sys.argv[2])['xl'];
ks=np.fromstring(sys.argv[3],dtype=float,sep=' ');
ds=np.fromstring(sys.argv[4],dtype=int,sep=' ');
trper=int(sys.argv[5]);
dvper=int(sys.argv[6]);

N=X.shape[0];
np.random.seed(23); perm=np.random.permutation(N);
X=X[perm]; xl=xl[perm];

# Selecting a subset for train and dev sets
Ntr=round(trper/100*N);
Xtr=X[:Ntr]; xltr=xl[:Ntr];
Ndv=round(dvper/100*N);
Xdv=X[N-Ndv:]; xldv=xl[N-Ndv:];

#1. Calcular pca con xtr
[m,Mvect] = pca(Xtr);

print('Dimension\tAlpha\tError');
print('-----------\t-------\t----');

for d in ds:
  for k in ks:
    #2.1 proyectar los datos de xtr a ks dimensiones
    Xtr_p = (Xtr - m).dot(Mvect[:,1:d]);
    #2.2 proyectar los datos de xdr a ks dimensiones
    Xdv_p = (Xdv - m).dot(Mvect[:,1:d]);
    #2.3 medir error con la funcion gauss
    error = gaussian(Xtr_p, xltr, Xdv_p, xldv,k); #error con gaussian
    error = round(error,2);
    print('{}\t\t{}\t{}'.format(d,k, error));


from datetime import datetime, timedelta

import shippo
from tests.helper import create_mock_international_transaction, ShippoTestCase, DUMMY_PICKUP, shippo_vcr

class PickupTests(ShippoTestCase):
    request_client = shippo.http_client.RequestsClient

    @shippo_vcr.use_cassette(cassette_library_dir="tests/fixtures/pickup")
    def test_create(self):
        transaction, carrier_account = create_mock_international_transaction()
        PICKUP = DUMMY_PICKUP
        PICKUP["carrier_account"] = carrier_account
        PICKUP["transactions"] = [transaction.object_id]
        pickupTimeStart = datetime.now() + timedelta(hours=1)
        pickupTimeEnd = pickupTimeStart + timedelta(days=1)
        PICKUP["pickupTimeStart"] = pickupTimeStart.isoformat() + "Z"
        PICKUP["pickupTimeEnd"] = pickupTimeEnd.isoformat() + "Z"
        try:
            pickup = shippo.Pickup.create(**PICKUP)
        except shippo.error.InvalidRequestError:
            self.skipTest("Invalid request error")
        else:
            self.assertEqual(pickup.status, "SUCCESS")


import os

from pydantic import BaseSettings

class Settings(BaseSettings):
    """Provisioner settings."""

    EVENTING_HOST: str
    CREATE_EVENT_TRIGGER_ENDPOINT: str = "event-triggers"

    class Config:
        """Meta config."""

        env_file = (
            os.environ.get("MAVIS_CONFIG")  # check if the path specified.
            if os.environ.get("MAVIS_CONFIG") is not None
            else "./.env"
        )
        env_file_encoding = "utf-8"
        case_sensitive = True

provisioner_settings = Settings()  # type: ignore


from typing import Mapping

import sympy
import sympy.parsing.sympy_parser

class SingularityDetectionException(Exception):
    """
    Thrown in case an error occurs while detecting singularities.
    """
    pass

class SingularityDetection:
    r"""Singularity detection for generated propagator matrix.

    Some ordinary differential equations (ODEs) can be solved analytically: an expression for the solution can be readily derived by algebraic manipulation. This allows us to formulate an "exact integrator", that yields the next state of the system given the current state and the timestep Δt, to floating point (machine) precision [1]_.

    In some cases, an ODE is analytically tractable, but vulnerable to an edge case condition in the generated propagator matrices. Consider the following example: Let the system of ODEs be given by

    .. math::

       y' = A \cdot y

    Then the propagator matrix for a timestep :math:`\Delta t` is

    .. math::

       P = \exp(A \cdot \Delta t)

    which we can use to advance the system

    .. math::

       y(t + \Delta t) = P \cdot y(t)

    If :math:`A` is of the form:

    .. math::

       \begin{bmatrix}
       -a & 0  & 0\\
       1  & -a & 0\\
       0  & 1  & -b
       \end{bmatrix}

    Then the generated propagator matrix contains denominators that include the factor :math:`a - b`. When the parameters are chosen such that :math:`a = b`, a singularity (division by zero fault) occurs. However, the singularity is readily avoided if we assume that :math:`a = b` before generating the propagator, i.e. we start out with the matrix

    .. math::

       \begin{bmatrix}
       -a & 0  & 0\\
       1  & -a & 0\\
       0  & 1  & -a
       \end{bmatrix}

    The resulting propagator contains no singularities.

    This class detects the potential occurrence of such singularities (potential division by zero) in the generated propagator matrix, which occur under certain choices of parameter values. These choices are reported as "conditions" by the ``find_singularities()`` function.

    References
    ----------

    .. [1] Stefan Rotter, Markus Diesmann. Exact digital simulation of time-invariant linear systems with applications to neuronal modeling. Neurobiologie und Biophysik, Institut für Biologie III, Universität Freiburg, Freiburg, Germany Biol. Cybern. 81, 381-402 (1999)
    """

    @staticmethod
    def _is_matrix_defined_under_substitution(A: sympy.Matrix, cond: Mapping) -> bool:
        r"""
        Function to check if a matrix is defined (i.e. does not contain NaN or infinity) after we perform a given set of subsitutions.

        Parameters
        ----------
        A : sympy.Matrix
            input matrix
        cond : Mapping
            mapping from expression that is to be subsituted, to expression to put in its place
        """
        for val in sympy.flatten(A):
            for expr, subs_expr in cond.items():
                if sympy.simplify(val.subs(expr, subs_expr)) in [sympy.nan, sympy.zoo, sympy.oo]:
                    return False

        return True

    @staticmethod
    def _flatten_conditions(cond):
        r"""
        Return a list with conditions in the form of dictionaries
        """
        lst = []
        for i in range(len(cond)):
            if cond[i] not in lst:
                lst.append(cond[i])

        return lst

    @staticmethod
    def _filter_valid_conditions(cond, A: sympy.Matrix):
        filt_cond = []
        for i in range(len(cond)):  # looping over conditions
            if SingularityDetection._is_matrix_defined_under_substitution(A, cond[i]):
                filt_cond.append(cond[i])

        return filt_cond

    @staticmethod
    def _generate_singularity_conditions(A: sympy.Matrix):
        r"""
        The function solve returns a list where each element is a dictionary. And each dictionary entry (condition: expression) corresponds to a condition at which that expression goes to zero.
        If the expression is quadratic, like let's say "x**2-1" then the function 'solve() returns two dictionaries in a list. each dictionary corresponds to one solution.
        We are then collecting these lists in our own list called 'condition'.
        """
        conditions = []
        for expr in sympy.flatten(A):
            for subexpr in sympy.preorder_traversal(expr):  # traversing through the tree
                if isinstance(subexpr, sympy.Pow) and subexpr.args[1] < 0:  # find expressions of the form 1/x, which is encoded in sympy as x^-1
                    denom = subexpr.args[0]  # extracting the denominator
                    cond = sympy.solve(denom, denom.free_symbols, dict=True)  # ``cond`` here is a list of all those conditions at which the denominator goes to zero
                    if cond not in conditions:
                        conditions.extend(cond)

        return conditions

    @staticmethod
    def find_singularities(P: sympy.Matrix, A: sympy.Matrix):
        r"""Find singularities in the propagator matrix :math:`P` given the system matrix :math:`A`.

        Parameters
        ----------
        P : sympy.Matrix
            propagator matrix to check for singularities
        A : sympy.Matrix
            system matrix
        """
        try:
            conditions = SingularityDetection._generate_singularity_conditions(P)
            conditions = SingularityDetection._flatten_conditions(conditions)  # makes a list of conditions with each condition in the form of a dict
            conditions = SingularityDetection._filter_valid_conditions(conditions, A)  # filters out the invalid conditions (invalid means those for which A is not defined)
        except Exception as e:
            raise SingularityDetectionException()

        return conditions


import gym
from gym import error, spaces, utils
from gym.utils import seeding
import numpy as np
import pandas as pd
import torch

class BatteryEnv(gym.Env):
    def __init__(self, df, Eess=1, SOCmax=1, SOCmin=0, nu=0.9, PowStBy=0):
        self.df = df

        self.Eess = Eess #MWh
        self.SOC = SOCmin #a percentage of capacity
        self.SOCmin = SOCmin
        self.SOCmax = SOCmax
        self.steps = len(self.df)
        self.T = 1 #1 hour
        self.actions = np.zeros(self.steps) #stores historical actions that I take
        self.nu= nu #efficiency
        self.PowStBy = PowStBy
        self.alpha = 0 #apparently this needs to be updated every episodes

        self.reward_total = 0
        self.step = 0
        self.scaling_factor = 1
        # Actions of the format Buy x% of capacity, Sell x% of capacity, Hold, etc.
        self.action_space = spaces.Discrete( 5 )

        lows_array = np.zeros([1,25])
        highs_array = np.ones([1,25])
        lows_array[0] = self.SOCmin
        highs_array = highs_array*(max(self.df['Hourly_USD_per_MWh'])+1)

        highs_array[:,0] = self.SOCmax
        # Prices contains the energy values

        self.observation_space = spaces.Box(low=lows_array, high=highs_array, dtype=np.float16)

    def _next_obs(self):
        prices = self.df.Hourly_USD_per_MWh[self.step:self.step+24].to_numpy()
        done = self.step >= len(self.df)-25
        obs = torch.from_numpy(np.expand_dims(np.append([self.SOC],[prices]), 0)).float()
        # print("STEP: ", self.step, "\n", "obs: ", obs)
        return done, obs

    def eff(self):
        return self.nu

    def deg(self):
        return self.alpha

    def _take_action(self, action, ct):
        '''
        ct = current price of electricity
        action = integer between 0 and 4 inclusive

        Power is in units MW
        '''
        action_type = ACTION_LOOKUP[action]
        #if Power is negative, it's charging. If Power is positive, it's discharging
        Pow_high = min((self.SOC - self.SOCmin)*self.Eess/(self.eff()*self.T), self.Eess/2)
        Pow_low = max((self.SOC - self.SOCmax)*self.Eess/(self.eff()*self.T), -self.Eess/2)
        flag = 0
        if Pow_high < Pow_low: #if the bounds aren't possible -- which shouldnt ever come up
            Pow = 0
        else:
            if action_type == "dischargefull":
                Pow = self.Eess/2
                if not(Pow_low <= Pow <= Pow_high):
                    if Pow > Pow_high:
                        Pow = Pow_high
                        flag = 1
                    elif Pow < Pow_low:
                        Pow = Pow_low
            elif action_type == "dischargehalf":
                Pow = self.Eess/4
                if not(Pow_low <= Pow <= Pow_high):
                    if Pow > Pow_high:
                        Pow = Pow_high
                        flag = 1
                    elif Pow < Pow_low:
                        Pow = Pow_low
            elif action_type == "chargefull":
                Pow = -self.Eess/2
                if not(Pow_low <= Pow <= Pow_high):
                    if Pow > Pow_high:
                        Pow = Pow_high
                    elif Pow < Pow_low:
                        Pow = Pow_low
            elif action_type == "chargehalf":
                Pow = -self.Eess/4
                if not(Pow_low <= Pow <= Pow_high):
                    if Pow > Pow_high:
                        Pow = Pow_high
                    elif Pow < Pow_low:
                        Pow = Pow_low
        if action_type == "rest":
            SOC_dif = 1/self.Eess*self.T*self.PowStBy
            self.SOC -= SOC_dif
            Rt = 0
            self.Pow = 0
        else:
            self.SOC -= 1/self.Eess*self.T*self.eff()*Pow #charging when power is negative
            P_max = self.Eess/2
            # print(ct, Pow, self.alpha,P_max+0.0001)
            Rt = self.scaling_factor*((ct*Pow - self.deg()*abs(Pow))/P_max) + ((flag==1)* -5)
            self.profit += ct*Pow - self.deg()*abs(Pow)
            self.Pow = Pow
        self.step += 1
        return Rt

    def take_step(self, action, current_price):
        self.actions[self.step] = action
        reward = self._take_action(action, current_price)
        self.reward_total += reward
        done, observation = self._next_obs()
        return observation, reward, done

    def reset(self):
        self.SOC = self.SOCmin #a percentage of capacity
        self.actions = np.zeros(self.steps)
        self.step = 0
        self.profit = 0
        self.reward_total = 0
        self.Pow = 0

    def render(self, mode='human'):
        print("SOC:", self.SOC, "action:", self.actions[self.step-1], "profit: ", self.profit, "price", self.df.Hourly_USD_per_MWh[self.step])
        return(self.profit, self.SOCmax, self.reward_total, self.SOC, self.Pow, self.df.Hourly_USD_per_MWh[self.step])

ACTION_LOOKUP = {
    0: "dischargefull", #500 kW -- max(lowerbound, -500)
    1: "dischargehalf", #250 kW
    2: "rest", #0 kW
    3: "chargehalf", #-250kW
    4: "chargefull" #-500 kW
}


#!/usr/bin/python2.7

from __future__ import print_function
import Tkinter
import tkFileDialog
from collections import OrderedDict
from time import asctime
import sys

class GUIParameter(object):

    def __init__(
        self,
        name,
        widget_type,
        info=None,
        default=None
    ):
        self.name = name
        self.info = info
        self.widget_type = widget_type
        self.default = default
        self.widget = None
        self.value = None

    def __str__(self):
        return "{} = {}".format(
            self.name,
            self.value.get()
        )

class GUI(object):

    def __init__(self, title, parameters, externalRunCommand):
        sys.stderr = self
        sys.stdout = self
        self.root = Tkinter.Tk()
        self.root.winfo_toplevel().title(title)
        self.parameters = OrderedDict(
            (parameter.name, parameter) for parameter in parameters
        )
        self.externalRunCommand = externalRunCommand
        self.createForm()
        self.root.mainloop()

    def createForm(self):
        self.setCustomWidgets()
        self.setDefaultButtons()
        self.setDisplay()

    def setCustomWidgets(self):
        for i, parameter in enumerate(self.parameters.values()):
            self.setCustomWidget(i, parameter)

    def setDefaultButtons(self):
        for i, (name, command) in enumerate(
            [
                # ('Check Parameters', self.checkParameterCommand),
                ('Load Parameters', self.loadParameterCommand),
                ('Save Parameters', self.saveParameterCommand),
                ('Run', self.runCommand),
            ]
        ):
            self.setDefaultButton(name, command, len(self.parameters) + i)

    def setCustomWidget(self, row, parameter):
        Tkinter.Label(
            self.root,
            text=parameter.name
        ).grid(row=row, sticky='w')
        if parameter.widget_type == Tkinter.Entry:
            value = Tkinter.StringVar()
            widget = parameter.widget_type(
                self.root,
                textvariable=value
            )
        elif parameter.widget_type == Tkinter.Checkbutton:
            value = Tkinter.IntVar()
            widget = parameter.widget_type(
                self.root,
                variable=value
            )
        if parameter.default is not None:
            value.set(parameter.default)
        widget.grid(row=row, column=1, sticky='w', )
        info_button = Tkinter.Button(
            self.root,
            text='Info',
            command=lambda info=parameter.info: print(info)
        )
        info_button.grid(row=row, column=2)
        parameter.widget = widget
        parameter.value = value

    def setDefaultButton(self, name, command, row):
        default_button = Tkinter.Button(
            self.root,
            text=name,
            command=command
        )
        default_button.grid(
            row=row,
            column=0,
            columnspan=3,
            sticky='we'
        )

    # def checkParameterCommand(self):
    #     self.triggerActive(False)
    #     # TODO
    #     print("Not implemented")
    #     self.triggerActive(True)

    def loadParameterCommand(self):
        self.triggerActive(False)
        file_name = tkFileDialog.askopenfilename(
            parent=self.root,
            title='Select parameter file'
        )
        if file_name:
            with open(file_name, "rb") as infile:
                for line in infile:
                    if not line.startswith("#") and line.strip():
                        name, value = line.split(" = ")
                        self.parameters[name].value.set(value.rstrip())
            print(
                "Succesfully loaded parameters from {}".format(
                    file_name
                )
            )
        self.triggerActive(True)

    def saveParameterCommand(self):
        self.triggerActive(False)
        file_name = tkFileDialog.asksaveasfilename(
            parent=self.root,
            title='Select parameter file'
        )
        if file_name:
            with open(file_name, "wb") as outfile:
                for parameter in self.parameters.values():
                    outfile.write("{}\n".format(parameter))
            print(
                "Succesfully saved parameters to {}".format(
                    file_name
                )
            )
        self.triggerActive(True)

    def runCommand(self):
        self.triggerActive(False)
        self.externalRunCommand(self)
        self.triggerActive(True)

    def triggerActive(self, active):
        if active:
            state = "normal"
        else:
            state = "disabled"
        for widget in self.root.winfo_children():
            widget.config(state=state)

    def setDisplay(self):
        self.display_indent = 0
        self.display = Tkinter.Text(self.root)
        self.display.grid(
            row=0,
            column=3,
            # rowspan=len(self.parameters) + 4
            rowspan=len(self.parameters) + 3,
            sticky="ns"
        )
        self.display.config(state="disabled")

    def write(self, text):
        self.display.config(state="normal")
        # text = text.rstrip()
        # time = asctime()
        # space = 80 - len(time) - 3
        # chunks = len(text) / space + 1
        # if len(text) % space == 0:
        #     chunks -= 1
        # for i in xrange(chunks):
        #     self.display.insert(
        #         Tkinter.END,
        #         "{} > {}\n".format(
        #             time,
        #             text[space * i: space * (i + 1)]
        #         )
        #     )
        self.display.insert(Tkinter.END, text)
        self.display.update()
        self.display.config(state="disabled")
        # indent_space = "  " * self.display_indent
        # self.display.config(state="normal")
        # self.display.insert(
        #     Tkinter.END,
        #     "{} > {}{}\n".format(asctime(), indent_space, text)
        # )
        # self.display.update()
        # self.display.config(state="disabled")
        # self.display_indent += indent


import numpy as np
import matplotlib.pyplot as plt
from DPendulum import Pendulum

def s1(y):
    """Transform onto s1"""
    s1_y = []
    for val in y:
        v = val % (2*np.pi)
        if v > np.pi:
            v = v - 2*np.pi
        s1_y.append(v)
    return s1_y

def pi_jumps(y):
    """Return list of places where line between i -> i+1 should be removed."""
    no_line = []
    a = (y + np.pi) // (2*np.pi) / np.pi
    for i in range(len(a)-1):
        if a[i] != a[i+1]:
            no_line.append(i)
    return no_line

def plot_minus_jumps(y1, y2):
    index = [i for i in range(len(y1)-1, -1, -1)]
    discont  = set(pi_jumps(y1) + pi_jumps(y2))
    s_y1 = s1(y1)
    s_y2 = s1(y2)
    discont = list(discont)
    discont.sort()
    separated_points = []
    while index:
        run = [[], []]
        while True:
            j = index.pop()
            run[0].append(s_y1[j])
            run[1].append(s_y2[j])
            if j in discont or j == len(y1)-1:
                separated_points.append(run)
                break
    for r in range(len(separated_points)):
        plt.plot(separated_points[r][0], separated_points[r][1], color = 'b')

def twod_projection(pendulum, *, i=0, j=2):
    y = pendulum.sol()
    label = [r'$\theta_1$', 'r$\omega_1$', 'theta2', 'theta2dot']
    plt.figure(figsize=(15,5))
    a = plt.subplot(1, 3, 1)
    plt.subplots_adjust(wspace=0.5)
    plt.plot(y[i], y[j])
    plt.xlabel(label[i])
    plt.ylabel(label[j])
    b = plt.subplot(1, 3, 2)
    plt.plot(s1(y[i]), s1(y[j]))
    plt.xlabel(label[i])
    plt.ylabel(label[j])
    c = plt.subplot(1, 3, 3)
    plot_minus_jumps(y[i], y[j])
    plt.xlabel(label[i])
    plt.ylabel(label[j])
    plt.show()

def poincare(pendulum, *, var=0, varpos=0, s=True, time=0):
    label = [r'$\theta_1$', r'$\dot{\theta_1}$', r'$\theta_2$', r'$\dot{\theta_2}$']
    label.pop(var)
    y = np.array(pendulum.sol())
    y[0] = s1(y[0])
    y[2] = s1(y[2])
    indices = []
    for i in range(len(y[var]) - 1):
        if y[var][i] < varpos and y[var][i+1] > varpos:
            indices.append(i)
    y = y.T
    points = []
    if indices:
        for i in indices:
            x1, x2 = list(y[i]), list(y[i+1])
            w1, w2 = x1.pop(var), x2.pop(var)
            points.append([(x2[i]*w2 - x1[i]*w1)/(w2-w1) for i in range(3)])
        print(points)
        points = np.array(points).T

        fig = plt.figure(figsize=(15,5))
        a = plt.subplot(1, 3, 1, projection='3d')
        plt.subplots_adjust(wspace=0.5)
        a.scatter3D(points[0], points[1], points[2], s=0.3, c="#00008B")
        a.set_xlabel(label[0])
        a.set_ylabel(label[1])
        a.set_zlabel(label[2])
        b = plt.subplot(1, 3, 2)
        b.scatter(points[0], points[2], s=0.3, c="blue")
        b.set_xlabel(label[0], fontsize='12')
        b.set_ylabel(label[2], fontsize='12')
        c = plt.subplot(1, 3, 3)
        plt.scatter(points[1], points[2], s=0.5, c="#00008B")
        plt.xlabel(label[1], fontsize='12')
        plt.ylabel(label[2], fontsize='12')
        fig.tight_layout
        plt.show()
    else:
        print('No crosses')
        return y

x = 4
m2 = (1-x**2)**2 / (4*x**2)

y0 = [0, 0, np.pi/100, 0]
pendulum = Pendulum(theta1=y0[0], z1=y0[1], theta2=y0[2], z2=y0[3], tmax=300,
                    m2=m2, y0=y0)
poincare(pendulum)


import pickle
import datetime
import game
from sklearn.neural_network import MLPRegressor
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
#
#  rate a board with scikit-learn.neural_net
#
#
class HitBoardEvaluator:
    def __init__(self, color):
        if color == game.Game.white:
            self.regressor = pickle.load(open("trained_net_white_500_runs_10_4_hidden_nodes.p", "rb"))
        else:
            self.regressor = pickle.load(open("trained_net_red_500_runs_10_4_hidden_nodes.p", "rb"))

        """
        file = open("boards_n_values_35mb.p", "rb")
        X = pickle.load(file)
        y = pickle.load(file)

        X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)

        #print(len(X), len(y), X[0], y[0])

        for i in range(len(y)):
            print(X[i])
        for i in range(len(y)):
            val = (( (int) (y[i]*100))/100 )
            print(val)

        print(len(X))
        """

        #self.regressor = MLPRegressor(solver='adam', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1, max_iter=1000)
        #print("Training the net.")
        #self.regressor.fit(X_train, y_train)
        #print("Training finished. Score on test set:", self.regressor.score(X_test, y_test))

    def getBoardRating(self, tokens):
        return self.regressor.predict([tokens])

class WinningColorBoardEvaluator:
    def __init__(self):
        self.regressor = pickle.load(open("trained_net_305000_logistic.p", "rb"))

    def getBoardRating(self, tokens):
        return self.regressor.predict([tokens])

class OnlineBoardEvaluator:
    def __init__(self, netFilename):
        try:
            print("Trying to load neural net:", netFilename +".p")
            self.regressor = pickle.load(open(netFilename +".p", "rb"))
            print("Loading was successful.")
        except:
            print("Creating new neural net.")
            self.regressor = MLPRegressor(solver='adam', alpha=1e-5, activation="logistic", random_state=1, max_iter=1000000,)

        self.filenameBase = netFilename

        self.updates = 0
        self.saveEveryXth = 1

    def getBoardRating(self, tokens):
        return self.regressor.predict([tokens])

    def learnBatch(self, boards, whiteWins, redWins):
        X = boards
        pWhiteWins = [whiteWins[i]/(whiteWins[i] + redWins[i]) for i in range(len(X))]
        pRedWins =  [redWins[i]/(whiteWins[i] + redWins[i]) for i in range(len(X))]
        y = [pWhiteWins[i] - pRedWins[i] for i in range(len(X))]

        #X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
        #self.regressor.fit(X_train, y_train)
        #print("net score:", self.regressor.score(X_test, y_test))

        self.regressor.partial_fit(X,y)
        self.updates +=1
        if (self.updates >= self.saveEveryXth):
            print("Saving net.")
            dateString = str(datetime.datetime.now())
            pickle.dump(self.regressor, open(self.filenameBase +"_"+ dateString +".p", "xb"))
            pickle.dump(self.regressor, open(self.filenameBase +".p", "wb"))
            self.updates = 0
            print("saving a sample batch.")
            batchFile = open("training_batch_"+ str(self.saveEveryXth) +"_"+ dateString + "_"+ str(len(boards))  +".p", "xb")
            pickle.dump(X, batchFile)
            pickle.dump(y, batchFile)
            pickle.dump(whiteWins, batchFile)
            pickle.dump(redWins, batchFile)
            batchFile.close()

            """
            fig, axes = plt.subplots(8, 8)
            # use global min / max to ensure all weights are shown on the same scale
            vmin, vmax = self.regressor.coefs_[1].min(), self.regressor.coefs_[0].max()
            for coef, ax in zip(self.regressor.coefs_[1].T, axes.ravel()):
                ax.matshow(coef.reshape(28,1), cmap=plt.cm.gray, vmin=.5 * vmin,
                        vmax=.5 * vmax)
                ax.set_xticks(())
                ax.set_yticks(())

            #plt.show()
            plt.savefig("graph_"+ dateString +".png")
            plt.close()
            """


""" Implementation of Ghostnet """
from collections import namedtuple
import tensorflow as tf
# from GhostNetModule import ConvBlock
from models.decmodel_components.GhostNetModule import GhostModule as MyConv
from models.decmodel_components.GhostNetModule import MyDepthConv as DepthConv
from dec.dec_config import ACTIVATION, ATTENTION
from models.decmodel_components.convblock import ConvBatchNormRelu as CBR
from models.decmodel_components.squeeze_layers import Squeeze_excitation_layer as SElayer

kernel_initializer = tf.contrib.layers.variance_scaling_initializer(2.0)

Conv = namedtuple('Conv', ['kernel', 'stride', 'depth', 'factor', 'se'])
Bottleneck = namedtuple('Bottleneck', ['kernel', 'stride', 'depth', 'factor', 'se'])

_CONV_DEFS_0 = [
    Conv(kernel=[3, 3], stride=2, depth=16, factor=1, se=0),
    Bottleneck(kernel=[3, 3], stride=1, depth=16, factor=1, se=0),

    Bottleneck(kernel=[3, 3], stride=2, depth=24, factor=48 / 16, se=0),
    Bottleneck(kernel=[3, 3], stride=1, depth=24, factor=72 / 24, se=0),

    Bottleneck(kernel=[5, 5], stride=2, depth=40, factor=72 / 24, se=1),
    Bottleneck(kernel=[5, 5], stride=1, depth=40, factor=120 / 40, se=1),

    Bottleneck(kernel=[3, 3], stride=2, depth=80, factor=240 / 40, se=0),
    Bottleneck(kernel=[3, 3], stride=1, depth=80, factor=200 / 80, se=0),
    Bottleneck(kernel=[3, 3], stride=1, depth=80, factor=184 / 80, se=0),
    Bottleneck(kernel=[3, 3], stride=1, depth=80, factor=184 / 80, se=0),

    Bottleneck(kernel=[3, 3], stride=1, depth=112, factor=480 / 80, se=1),
    Bottleneck(kernel=[3, 3], stride=1, depth=112, factor=672 / 112, se=1),
    Bottleneck(kernel=[5, 5], stride=2, depth=160, factor=672 / 112, se=1),

    Bottleneck(kernel=[5, 5], stride=1, depth=160, factor=960 / 160, se=0),
    Bottleneck(kernel=[5, 5], stride=1, depth=160, factor=960 / 160, se=1),
    Bottleneck(kernel=[5, 5], stride=1, depth=160, factor=960 / 160, se=0),
    Bottleneck(kernel=[5, 5], stride=1, depth=160, factor=960 / 160, se=1),

]

def shape2d(a):
    """
    Ensure a 2D shape.

    Args:
        a: a int or tuple/list of length 2

    Returns:
        list: of length 2. if ``a`` is a int, return ``[a, a]``.
    """
    if type(a) == int:
        return [a, a]
    if isinstance(a, (list, tuple)):
        assert len(a) == 2
        return list(a)
    raise RuntimeError("Illegal shape: {}".format(a))

def ghostnet_base(inputs,
                  mode,
                  data_format,
                  min_depth=8,
                  depth_multiplier=1.0,
                  depth=1.0,
                  conv_defs=None,
                  output_stride=None,
                  dw_code=None,
                  ratio_code=None,
                  se=1,
                  scope=None,
                  is_training=False,
                  momentum=0.9):

    """ By adjusting depth_multiplier can change the depth of network """
    if data_format == 'channels_first':
        axis = 1
        inputs = tf.transpose(inputs, [0, 3, 1, 2])
    else:
        axis = -1

    output_layers = []

    def depth(d):
        d = max(int(d * depth_multiplier), min_depth)
        d = round(d / 4) * 4
        return d

    # Used to find thinned depths for each layer.
    if depth_multiplier <= 0:
        raise ValueError('depth_multiplier is not greater than zero.')

    if conv_defs is None:
        conv_defs = _CONV_DEFS_0

    if dw_code is None or len(dw_code) < len(conv_defs):
        dw_code = [3] * len(conv_defs)
    print('dw_code', dw_code)

    if ratio_code is None or len(ratio_code) < len(conv_defs):
        ratio_code = [2] * len(conv_defs)
    print('ratio_code', ratio_code)

    se_code = [x.se for x in conv_defs]
    print('se_code', se_code)

    if output_stride is not None and output_stride not in [8, 16, 32]:
        raise ValueError('Only allowed output_stride values are 8, 16, 32.')

    with tf.variable_scope(scope, 'MobilenetV2', [inputs]):

        """
        The current_stride variable keeps track of the output stride of the
        activations, i.e., the running product of convolution strides up to the
        current network layer. This allows us to invoke atrous convolution
        whenever applying the next convolution would result in the activations
        having output stride larger than the target output_stride.
        """
        current_stride = 1
        # The atrous convolution rate parameter.
        rate = 1
        net = inputs
        in_depth = 3
        gi = 0

        for i, conv_def in enumerate(conv_defs):
            layer_stride = conv_def.stride
            current_stride *= conv_def.stride
            if layer_stride != 1:
                output_layers.append(net)

            if isinstance(conv_def, Conv):
                # net = ConvBlock(net, depth(conv_def.depth), conv_def.kernel, stride=conv_def.stride,
                #                 name='ConvBlock_{}'.format(i), is_training=is_training, activation=ACTIVATION)
                net = CBR(net, depth(conv_def.depth), conv_def.kernel[0], strides=conv_def.stride, training=is_training,
                          momentum=momentum, mode=mode, name='ConvBlock_{}'.format(i), padding='same', data_format=data_format,
                          activation=ACTIVATION, bn=True, use_bias=False)

            # Bottleneck block.
            elif isinstance(conv_def, Bottleneck):
                # Stride > 1 or different depth: no residual part.
                if layer_stride == 1 and in_depth == conv_def.depth:
                    res = net
                else:
                    res = DepthConv(net, conv_def.kernel, stride=layer_stride, data_format=data_format, name='Bottleneck_block_{}_shortcut_dw'.format(i))
                    res = tf.layers.batch_normalization(res, training=is_training, name='Bottleneck_block_{}_shortcut_dw_BN'.format(i), axis=axis)

                    res = CBR(res, depth(conv_def.depth), 1, 1, training=is_training, momentum=momentum, mode=mode,
                              name='Bottleneck_block_{}_shortcut_1x1'.format(i), padding='same',
                              data_format=data_format, activation=ACTIVATION, bn=True, use_bias=False)

                # Increase depth with 1x1 conv.
                net = MyConv('Bottleneck_block_{}_up_pointwise'.format(i), net, depth(in_depth * conv_def.factor), 1,
                             dw_code[gi], ratio_code[gi], mode=mode, strides=1, data_format=data_format, use_bias=False, is_training=is_training, activation=ACTIVATION, momentum=momentum)

                # Depthwise conv2d.
                if layer_stride > 1:
                    net = DepthConv(net, conv_def.kernel, stride=layer_stride, data_format=data_format, name='Bottleneck_block_{}_depthwise'.format(i))
                    net = tf.layers.batch_normalization(net, training=is_training, name='Bottleneck_block_{}_depthwise_BN'.format(i), axis=axis)

                # SE
                if se_code[i] > 0 and se > 0:
                    if ATTENTION == 'se':
                        # net = SELayer(net, depth(in_depth * conv_def.factor), 4)
                        net = SElayer(net, depth(in_depth * conv_def.factor), depth(in_depth * conv_def.factor)//4, "se_{}".format(i), data_format=data_format)
                # Downscale 1x1 conv.
                net = MyConv('Bottleneck_block_{}_down_pointwise'.format(i), net, depth(conv_def.depth), 1, dw_code[gi],
                             ratio_code[gi], mode=mode, strides=1, data_format=data_format, use_bias=False, is_training=is_training, activation=ACTIVATION, momentum=momentum)

                gi += 1

                # Residual connection?
                net = tf.add(res, net, name='Bottleneck_block_{}_Add'.format(i)) if res is not None else net

            in_depth = conv_def.depth
            # Final end point?
        output_layers.pop(0)
        output_layers.append(net)
        return output_layers


from flask import Flask, render_template, request
from flask_restful import Resource, Api, reqparse
import pandas as pd
import requests as req
import random
import string

app = Flask(__name__)
api = Api(app)

@app.route('/login', methods=['GET', 'POST'])
def login_required():
    if request.method == 'POST':
        tokens = pd.read_csv('users.csv')
        alphabets = string.ascii_letters
        numbers = string.digits
        pasw = alphabets + numbers

        token = ''
        for _ in range(15):
            token += random.choice(pasw)

        data = pd.DataFrame({
            'token': token
        }, index=[1])
        tokens = tokens.append(data, ignore_index=True)
        tokens.to_csv('users.csv', index=False)
        print(token)
        return render_template('login.html', token=token)

    return render_template('login.html')

@app.route('/home', methods=['GET', 'POST'])
def home():
    api_url = 'https://172.31.39.248:8000/users'
    if request.method == 'POST':
        if request.form.get('Activate') == 'Activate':
            token = request.form.get('token')
            country = request.form.get('vpn')
            print(country)
            operation = 'enable'
            params = {
                'token': token,
                'country': country,
                'operation': operation
            }
            response = req.put(url=api_url, json=params)
            print(response.status_code)

        if request.form.get('Deactivate') == 'Deactivate':
            token = request.form.get('token')
            country = request.form.get('vpn')
            operation = 'disable'
            params = {
                'token': token,
                'country': country,
                'operation': operation
            }
            response = req.put(url=api_url, json=params)
            print(response.status_code)
        return render_template('home.html')

    if request.method == 'GET':
        return render_template('home.html')

class Users(Resource):

    @staticmethod
    def get():
        parser = reqparse.RequestParser()
        parser.add_argument('token', required=True)
        args = parser.parse_args()
        data = pd.read_csv('users.csv')
        if args['token'] in list(data['token']):
            return data[data['token'] == args['token']].to_dict('records'), 200

        else:
            return {
                       'message': 'Token is Invalid.'
                   }, 404

    @staticmethod
    def post():
        parser = reqparse.RequestParser()
        parser.add_argument('token', required=True)
        parser.add_argument('country', required=True)
        parser.add_argument('operation', required=True)
        args = parser.parse_args()

        data = pd.read_csv('users.csv')

        for token in list(data['token']):
            if args['token'] in token:
                return {
                           'message': f"'{args['token']}' already exists."
                       }, 409
        else:
            new_data = pd.DataFrame({
                'token': [args['token']],
                'country': [args['country']],
                'operation': [args['operation']],
            })
            data = data.append(new_data, ignore_index=True)
            data.to_csv('users.csv', index=False)
            return 200

    @staticmethod
    def put():
        parser = reqparse.RequestParser()
        parser.add_argument('token', required=True)
        parser.add_argument('country', required=True)
        parser.add_argument('operation', required=True)
        args = parser.parse_args()

        data = pd.read_csv('users.csv')

        if args['token'] in list(data['token']):
            data.loc[data['token'] == args['token'], 'country'] = args['country']
            data.loc[data['token'] == args['token'], 'operation'] = args['operation']

            # save back to CSV
            data.to_csv('users.csv', index=False)
            return 200

        else:
            return {
                       'message': f"'{args['token']}' not found."
                   }, 404

    @staticmethod
    def delete():
        parser = reqparse.RequestParser()
        parser.add_argument('token', required=True)
        args = parser.parse_args()

        data = pd.read_csv('users.csv')

        for token in list(data['token']):
            if args['token'] in token:
                data = data[data['token'] != args['token']]

                data.to_csv('users.csv', index=False)
                return 200

        else:
            return {
                       'message': f"'{args['token']}' not found."
                   }, 404

api.add_resource(Users, '/users')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)


from GPano import *
import GPano
import csv,itertools
import pandas as pd
import multiprocessing as mp
import database_ops as db_ops

def downloadImg(lon,lat,saved_path):
    gpano = GPano.GPano()
    degrees = [90,270]
    # saved_path = "J:\Research\Trees\west_trees"
    data = gpano.getPanoJsonfrmLonat(lon, lat)
    # print(data)
    car_pano = data.get("Location")
    car_lat = car_pano.get("lat")
    car_lon = car_pano.get("lng")
    # print(car_lon)
    # angle = gpano.getDegreeOfTwoLonlat(lat,lon,float(car_lat),float(car_lon))
    angle = gpano.getDegreeOfTwoLonlat(float(car_lat), float(car_lon), lat, lon)  #  see from float(car_lat), float(car_lon) to lon/lat

    panoId = data["Location"]["panoId"]
    # ---------------------------------------------------------------------------
    projection = data.get("Projection")
    pano_yaw_deg = float(projection.get("pano_yaw_deg"))
    print('angle, pano_yaw_deg: ', angle, pano_yaw_deg)
    # degrees.append(angle)

    find_min = [abs(pano_yaw_deg + deg - angle) % 360 for deg in degrees ]
    find_min2 = []
    for f in find_min:
        if f > 180:
            find_min2.append(abs(f-360))
        else:
            find_min2.append(f)

    index = find_min2.index(min(find_min2))
    # print(data)
    yaw = degrees[index]+pano_yaw_deg
    yaw = yaw % 360
    # if yaw > 360:
    #     yaw = yaw - 360
    # if yaw < 0:
    #     yaw = yaw + 360
    # print(yaw)
    # getDegreeOfTwoLonlat
    image, jpg_name = gpano.getImagefrmAngle(float(car_lon),float(car_lat), saved_path,yaw=yaw,prefix=panoId)
    url = gpano.getGSV_url_frm_lonlat(float(car_lon),float(car_lat), heading=yaw)
    print("Google street view URL:", url)
    print("PanoID:", panoId)
    # print(image)
    # print(jpg_name)
    # ---------------------------------------------------------------------------

#Tree long lat positions as input

# downloadImg(-75.14474465,39.98868215)

def downloadImgs(lon_lat_list, saved_path):
    while len(lon_lat_list) > 0:
        try:
            lon, lat = lon_lat_list.pop(0)
            downloadImg(lon,lat, saved_path)
        except Exception as e:
            print("Error in downloadImgs():", e)
            continue

def test_downloadImgs_mp(lon_lst_csv= r"K:\OneDrive_NJIT\OneDrive - NJIT\Research\Trees\datasets\NewYorkCity\NYC_Trees20k.csv", saved_path= r"K:\Research\Trees\NewYorkCity_test\google_street_images", Process_cnt = 10):
    print("lon_lst_csv: ", lon_lst_csv)
    lon_lats = pd.read_csv(lon_lst_csv)

    mid = int(len(lon_lats)/2)

    print(len(lon_lats))
    pool = mp.Pool(processes=Process_cnt)
    lon_lats_mp = mp.Manager().list()

    for idx, lonlat in lon_lats[: ].iterrows():
        lon = lonlat['POINT_X']
        lat = lonlat['POINT_Y']
        lon_lats_mp.append((lon, lat))

    for i in range(Process_cnt):
        pool.apply_async(downloadImgs, args=(lon_lats_mp, saved_path))
    pool.close()
    pool.join()

    print("Done.")

def readCSV(n):
    with open(r"K:\OneDrive_NJIT\OneDrive - NJIT\Research\Trees\datasets\NewYorkCity\NYC_Trees20k.csv") as csv_file:
        i = 0
        # csv_reader = csv.reader(csv_file, delimiter=',')
        for row in itertools.islice(csv.DictReader(csv_file), 0,n):

            try:

                print(i, row['POINT_X'], row['POINT_Y'])
                downloadImg(row["POINT_X"],row['POINT_Y'],n)
                # each_row = frozenset(row)
                # data.append(each_row)
                i += 1
            except Exception as e:
                print("Error in downloadImg():", e)

            # for item in each_row:
            #     itemSet.add(frozenset([item]))
# readCSV(19951)

if __name__ == "__main__":
    test_downloadImgs_mp()


from models.transactions import Transaction
from models.category import Category
import pdb
from models.merchant import Merchant
from models.category import Category
from models.profile import Profile
import repositories.merchant_repository as merchant_repository
import repositories.category_repository as category_repository
import repositories.transaction_repositiory as transaction_repository
import repositories.profile_repository as profile_repository

merchant_repository.delete_all()
category_repository.delete_all()
transaction_repository.delete_all()

import datetime
x = datetime.datetime.now()

profile1= Profile(100, 90)
profile_repository.save(profile1)

profile2= Profile(120,80)
profile_repository.save(profile2)

merchant1 = Merchant("Tesco")
merchant_repository.save(merchant1)

merchant2 = Merchant("Sainsburys")
merchant_repository.save(merchant2)

merchant3 = Merchant("Amazon")
merchant_repository.save(merchant3)

category1 = Category("Food", 100)
category_repository.save(category1)

category2 = Category("Entertainment", 50)
category_repository.save(category2)

transaction1 = Transaction(merchant1, category1, "Weekly food shop", 50, x)
transaction_repository.save(transaction1)

transaction2 = Transaction(merchant3, category2, "Purchased a film", 1.99, x)
transaction_repository.save(transaction2)

transaction3 = Transaction(merchant1, category1, "Top up shop", 5, x)
transaction_repository.save(transaction3)

transaction4 = Transaction(merchant2, category2, "video game", 45.99, x)
transaction_repository.save(transaction4)

pdb.set_trace()


from math import pi, sin, cos
from opentrons.util.linal import solve, add_z, apply_transform, solve_attitude
from numpy.linalg import inv
import numpy as np

def test_solve() -> None:
    theta = pi / 3.0  # 60 deg
    scale = 2.0

    expected = [
        (cos(0), sin(0)),
        (cos(pi / 2), sin(pi / 2)),
        (cos(pi), sin(pi)),
    ]

    actual = [
        (cos(theta) * scale + 0.5, sin(theta) * scale + 0.25),
        (cos(theta + pi / 2) * scale + 0.5, sin(theta + pi / 2) * scale + 0.25),
        (cos(theta + pi) * scale + 0.5, sin(theta + pi) * scale + 0.25),
    ]

    X = solve(expected, actual)

    expected2 = np.array(
        [cos(theta + pi / 2) * scale + 0.5, sin(theta + pi / 2) * scale + 0.25, 1]
    )
    result = np.dot(X, np.array([[0], [1], [1]])).transpose()  # type: ignore[no-untyped-call]

    assert np.isclose(expected2, result).all()

def test_add_z() -> None:
    x = 5
    y = 10
    z = 20

    xy_array = np.array([[1, 0, x], [0, 1, y], [0, 0, 1]])

    expected = np.array([[1, 0, 0, x], [0, 1, 0, y], [0, 0, 1, z], [0, 0, 0, 1]])

    result = add_z(xy_array, z)
    assert (result == expected).all()

def test_apply_transform() -> None:
    x = 1
    y = 2
    z = 3

    x_delta = 0.1
    y_delta = 0.2

    e = ((1, 1, 3), (2, 2, 2), (1, 2, 1))
    a = (
        (1 + x_delta, 1 + y_delta, 1.1),
        (2 + x_delta, 2 + y_delta, 2.2),
        (1 + x_delta, 2 + y_delta, 1.1),
    )
    transform = solve_attitude(e, a)

    expected = (round(x - x_delta, 2), round(y - y_delta, 2), round(z))

    result = apply_transform(inv(transform), (1, 2, 3))  # type: ignore[no-untyped-call]
    assert np.isclose(result, expected, atol=0.1).all()


#!/usr/bin/env python3
"""Discord app."""
import asyncio
import curses
import os
import subprocess

import discord
import requests

import theme as thm
import ui as UI

class Client(discord.Client):
    """Wrapper around discord.Client class."""

    def __init__(self, callback, theme):
        super(Client, self).__init__()
        self.callback = callback
        self.theme = theme
        self.ui = None

    def set_ui(self, ui):
        """UI setter."""
        self.ui = ui

    def chan(self, message):
        """Return the wrapper.Channel object of a message."""
        if self.ui is None:
            return None
        if message.channel.is_private:
            server = self.ui.servers[0]
        else:
            server = self.ui.servers.get(message.server.id)
        if server is None:
            return None
        return server[message.channel.id]

    async def on_message(self, message):
        """Add message to channel and handle mentions."""
        # Download avatar to cache
        asyncio.get_event_loop().run_in_executor(None, download_avatar, message.author)

        chan = self.chan(message)
        if chan is None:
            return
        print(message.author.name, message.content, file=open('log', 'a'))
        chan.add_message(message)
        if chan == self.ui.channel:
            self.ui.pad.redraw()
        else:
            mentioned = False
            # Direct mentions
            if any([member for member in message.mentions if member.id == self.user.id]):
                mentioned = True
            # Role mentions
            if message.server is not None:
                roles = message.server.get_member(self.user.id).roles
                if any([r in roles for r in message.role_mentions]):
                    mentioned = True
            # @everyone / @here mentions
            if '@everyone' in message.content or '@here' in message.content:
                mentioned = True
            # Direct messages
            if message.channel.is_private:
                mentioned = True
            if mentioned and message.author.id != self.user.id:
                chan.mentions += 1
                # Desktop notifications (Linux only)
                subprocess.Popen(['notify-send',
                                  '-i', '/tmp/%s.png' % message.author.avatar,
                                  message.author.name, message.clean_content])
            # If we're currently focusing the server in which the message happened
            if self.ui.server is not None and ((message.server is None and self.ui.server.id == 0) or
                                               (message.server is not None and message.server.id == self.ui.server.id)):
                self.ui.chan.redraw()
            self.ui.serv.redraw()
            chan.unread = True
        self.ui.draw()
        self.ui.refresh()

    async def on_message_delete(self, message):
        """Delete message from channel."""
        chan = self.chan(message)
        if chan is None:
            return
        chan.delete_message(message)
        if chan == self.ui.channel:
            self.ui.pad.redraw()
            self.ui.draw()
            self.ui.refresh()

    async def on_message_edit(self, before, after):
        """Edit message in channel."""
        chan = self.chan(before)
        if chan is None:
            return
        chan.edit_message(before, after)
        if chan == self.ui.channel:
            self.ui.pad.redraw()
            self.ui.draw()
            self.ui.refresh()

    async def on_ready(self):
        """Discord client initialization, main function wrapper."""
        window = None
        try:
            window = curses.initscr()
            curses.noecho()         # Don't echo input
            curses.raw()            # Catch Ctrl+S, Ctrl+Z and such
            window.keypad(True)     # Catch Ctrl+Down as one key
            curses.start_color()    # Custom colors
            await self.callback(window, thm.Theme(self.theme), self)
        finally:
            if window is not None:
                window.keypad(False)
                curses.echo()
                curses.nocbreak()
                curses.endwin()
            await self.logout()

def download_avatar(user):
    """Download a user's avatar to /tmp/ if not already done."""
    filename = '/tmp/%s.png' % user.avatar
    if os.path.exists(filename):
        return
    url = user.avatar_url or user.default_avatar_url
    url = url.replace('.webp', '.png')
    file = requests.get(url)
    open(filename, 'wb').write(file.content)

def getch(window):
    """Return the key pressed."""
    key = -1
    while key == -1:
        try:
            key = window.getch()
        except KeyboardInterrupt:
            key = 3
    return key

async def main(window, theme, client):
    """Main function."""
    curses.use_default_colors()
    curses.curs_set(0)

    theme.refresh_layout(window)

    key = -1

    win = UI.UI(window, theme, client)
    await win.update()
    client.set_ui(win)

    while key != 17:  # Ctrl+Q
        if key != -1:
            win.press(key)
        await win.do()  # Queue
        win.draw()
        win.refresh()
        # Blocking for a key press, but letting discord.py run
        key = await asyncio.get_event_loop().run_in_executor(None, getch, window)

def _run():
    token = open('token').read().strip()
    client = Client(main, 'theme.toml')
    try:
        client.run(token, bot=False)
    except KeyboardInterrupt:
        pass

if __name__ == '__main__':
    # Set environment variable
    if os.environ.get('TERM') != 'xterm-256color':
        print('Setting $TERM to `xterm-256color`')
        os.environ['TERM'] = 'xterm-256color'
    # Set terminal title
    print('\33]0;DiscordZ\a', end='')
    _run()


# -*- coding: utf-8 -*-
"""
Created on Thu Dec 08 01:44:04 2016

@author: Ben
"""

try:
    from mpi4py import MPI
    comm = MPI.COMM_WORLD
    nprocs = comm.size
    myrank = comm.rank
except ImportError:
    print('MPI not found')
    myrank = 0
    nprocs = 1

import src
import pdb
import numpy as np
import time
import sys, os
from langreth import *
import shutil
from util import *
from functions import *
import integration
from matsubara import *
from plotting import *
import time
from itertools import product

def main():

    if myrank==0:
        time0 = time.time()
        print(' ')
        print('nprocs = ',nprocs)

    Nkx = 1
    Nky = 1
    k2p, k2i, i2k = init_k2p_k2i_i2k(Nkx, Nky, nprocs, myrank)
    kpp = np.count_nonzero(k2p==myrank)

    beta = 10.0
    ARPES = False
    pump = 0
    g2 = None
    omega = None
    tmax = 1.0
    #dt_fine = 0.001
    dt_fine = 0.001
    order = 6
    ntau = 800

    #nts = [10, 50, 100, 500]

    nts = [100]

    diffs = {}
    diffs['nts'] = nts
    diffs['U']  = []
    diffs['M']  = []
    diffs['IR'] = []
    diffs['R']  = []
    diffs['L']  = []

    delta = 0.3
    omega = 0.2
    V = 0.5
    norb = 2

    def H(kx, ky, t):
        #Bx = 2.0*V*np.cos(2.0*omega*t)
        #By = 2.0*V*np.sin(2.0*omega*t)
        #Bz = 2.0*delta
        #return 0.5*np.array([[Bz, Bx-1j*By], [Bx+1j*By, -Bz]], dtype=complex)
        return np.array([[delta, V*np.exp(-2.0*1j*omega*t)], [V*np.exp(+2.0*1j*omega*t), -delta]], dtype=complex)

    def compute_time_dependent_G0(H, myrank, Nkx, Nky, ARPES, kpp, k2p, k2i, tmax, nt, beta, ntau, norb, pump):
        # check how much slower this is than computing G0 using U(t,t')

        norb = np.shape(H(0,0,0))[0]
        def H0(kx, ky, t): return np.zeros([norb,norb])
        constants = (myrank, Nkx, Nky, ARPES, kpp, k2p, k2i, tmax, nt, beta, ntau, norb, pump)
        UksR, UksI, eks, fks, Rs, Ht = init_Uks(H0, dt_fine, *constants, version='higher order')
        G0M_ref = compute_G0M(0, 0, UksI, eks, fks, Rs, *constants)
        G0_ref  = compute_G0R(0, 0, UksR, UksI, eks, fks, Rs, *constants)

        dt = 1.0*tmax/(nt-1)

        G0M = compute_G00M(0, 0, *constants)
        G0  = compute_G00R(0, 0, *constants)

        print('test G00')
        differences(G0_ref, G0)

        '''
        G00M = compute_G00M(0, 0, *constants)
        G00  = compute_G00R(0, 0, G0M, *constants)

        print('shpae G0M', np.shape(G0M.M))
        print('shape G00M', np.shape(G00M.M))

        #for (i,j) in product(range(norb), repeat=2):
        #    plt(np.linspace(0,beta,ntau), [G0M.M[:,i,j].imag, G00M.M[:,i,j].imag], 'G00M %d %d'%(i,j))
        #    plt(np.linspace(0,beta,ntau), [G0M.M[:,i,j].real, G00M.M[:,i,j].real], 'G00M %d %d'%(i,j))

        print('diffs')
        print(dist(G0M.M, G00M.M))
        print(dist(G0.R, G00.R))
        print(dist(G0.L, G00.L))
        print(dist(G0.IR, G00.IR))
        exit()
        '''

        GM = matsubara(beta, ntau, norb, -1)
        SigmaM  = matsubara(beta, ntau, norb, -1)
        SigmaM.deltaM = H(0, 0, 0)
        integrator.dyson_matsubara(G0M, SigmaM, GM)

        # check if SigmaM is the same as before

        G = langreth(norb, nt, tmax, ntau, beta, -1)
        Sigma = langreth(norb, nt, tmax, ntau, beta, -1)
        for it in range(nt):
            Sigma.deltaR[it] = H(0, 0, it*dt)
        integrator.dyson_langreth(G0M, SigmaM, G0, Sigma, G)

        return GM, G

    for nt in nts:

        constants = (myrank, Nkx, Nky, ARPES, kpp, k2p, k2i, tmax, nt, beta, ntau, norb, pump)

        integrator = integration.integrator(6, nt, beta, ntau)

        #---------------------------------------------------------
        # compute U(t,t') exactly and the corresponding G0

        #Uexact = np.array([expm(-1j*H(0,0,0)*t) for t in ts])

        ts = np.linspace(0, tmax, nt)
        _, UksI, eks, fks, Rs, _ = init_Uks(H, dt_fine, *constants)

        Omega = sqrt((delta-omega)**2 + V**2)
        Uexact = np.zeros([1, nt, norb, norb], dtype=np.complex128)
        Uexact[0, :, 0, 0] = np.exp(-1j*omega*ts)*(np.cos(Omega*ts) - 1j*(delta-omega)/Omega*np.sin(Omega*ts))
        Uexact[0, :, 0, 1] = -1j*V/Omega*np.exp(-1j*omega*ts)*np.sin(Omega*ts)
        Uexact[0, :, 1, 0] = -1j*V/Omega*np.exp(1j*omega*ts)*np.sin(Omega*ts)
        Uexact[0, :, 1, 1] = np.exp(1j*omega*ts)*(np.cos(Omega*ts) + 1j*(delta-omega)/Omega*np.sin(Omega*ts))

        print('check unitary ')
        p = np.einsum('tba,tbc->tac', np.conj(Uexact[0,:]), Uexact[0,:])
        print(dist(p, np.einsum('t,ab->tab', np.ones(nt), np.diag(np.ones(norb)))))

        GMexact = compute_G0M(0, 0, UksI, eks, fks, Rs, *constants)
        Gexact = compute_G0R(0, 0, Uexact, UksI, eks, fks, Rs, *constants)

        #---------------------------------------------------------
        # compute G0 computed with U(t,t') via integration
        UksR, UksI, eks, fks, Rs, _ = init_Uks(H, dt_fine, *constants, version='higher order')
        G0M = compute_G0M(0, 0, UksI, eks, fks, Rs, *constants)
        G0  = compute_G0R(0, 0, UksR, UksI, eks, fks, Rs, *constants)

        # test for U(t,t')
        d = dist(Uexact, UksR)
        print('diff Uexact UksR', d)
        print("done computing G0 using U(t,t')")
        diffs['U'].append(d)

        #---------------------------------------------------------
        # compute non-interacting G for the norb x norb problem
        # we compute this by solving Dyson's equation with the time-dependent hamiltonian as the selfenergy

        GdysonM, Gdyson = compute_time_dependent_G0(H, *constants)
        print('done computing G0 via Dyson equation')

        '''
        ts = np.linspace(0,tmax,nt)
        Uexact = np.array([expm(-1j*H(0,0,0)*t) for t in ts])
        print('diff Uexact UksR', dist(Uexact, UksR[0]))
        for (i,j) in product(range(2), repeat=2):
            plt(ts, [UksR[0,:,i,j].real, Uexact[:,i,j].real], 'real part %d %d'%(i,j))
            plt(ts, [UksR[0,:,i,j].imag, Uexact[:,i,j].imag], 'imag part %d %d'%(i,j))
        exit()
        '''

        #for (i,j) in product(range(norb), repeat=2):
        #    plt(np.linspace(0, beta, ntau), [G0.M[:,i,j].imag, GdysonM.M[:,i,j].imag], 'G0M')

        '''
        for (i,j) in product(range(norb), repeat=2):
            im([Gexact.L[:,i,:,j].real, G0.L[:,i,:,j].real, Gdyson.L[:,i,:,j].real], [0,tmax,0,tmax], 'G0 real L %d %d'%(i,j))
            im([Gexact.L[:,i,:,j].imag, G0.L[:,i,:,j].imag, Gdyson.L[:,i,:,j].imag], [0,tmax,0,tmax], 'G0 imag L %d %d'%(i,j))
        '''

        print('differences between G0 and Gexact')
        differences(G0, Gexact)

        print('differences between Gdyson and Gexact')
        differences(Gdyson, Gexact)

        '''
        for (i,j) in product(range(norb), repeat=2):
            print('i j %d %d'%(i,j))
            im([G0.R[:,i,:,j].imag, Gexact.R[:,i,:,j].imag], [0,tmax,0,tmax], 'R imag')
            im([G0.R[:,i,:,j].real, Gexact.R[:,i,:,j].real], [0,tmax,0,tmax], 'R real')
        '''

    plt_diffs(diffs)

    if 'MPI' in sys.modules:
        MPI.Finalize()

if __name__=='__main__':
    main()
    print('\nPASSED TEST')


import scipy.io, os, random, sys
import numpy as np
import pandas as pd
import torch
import torchvision.datasets
import torchvision.transforms as transforms
import torch.nn.functional as F
from skimage.transform import resize

class Data_Loader:
    def __init__(self):
        return

    def load_dataset(self, dataset_name, true_label=1):
        if dataset_name == "cifar10":
            return self.load_CIFAR10(true_label)
        if dataset_name == "thyroid":
            return self.load_THYROID()
        if dataset_name == "stl10":
            return self.load_STL10(true_label)

    def normalize_img_dataset(self, dataset, mean=1):
        return 2*(dataset/255.)-mean

    def normalize_tabular_dataset(self, x_train, normal_samples, anomal_samples):
        means = x_train.mean(0)
        stds = x_train.std(0)
        stds[stds==0] = 1

        def normalize(xs, mean, stds):
            return np.array([(x-mean)/stds for x in xs])

        x_train = normalize(x_train, means, stds)
        normal_samples = normalize(normal_samples, means, stds)
        anomal_samples = normalize(anomal_samples, means, stds)
        return x_train, normal_samples, anomal_samples

    def load_CIFAR10(self, true_label):
        folder_path = "./data"
        if not os.path.exists(folder_path):
            os.mkdir(folder_path)

        train_dataset = torchvision.datasets.CIFAR10(folder_path, train=True, download=True)
        x_train = np.array(train_dataset.data)
        y_train = np.array(train_dataset.targets)

        test_dataset = torchvision.datasets.CIFAR10(folder_path, train=False, download=True)
        x_test = np.array(test_dataset.data)
        y_test = np.array(test_dataset.targets)

        x_train = x_train[np.where(y_train==true_label)]
        x_train = self.normalize_img_dataset(np.asarray(x_train, dtype="float32"))
        x_test = self.normalize_img_dataset(np.asarray(x_test, dtype="float32"))
        print("Normalized CIFAR dataset is loaded.")
        return x_train, x_test, y_test

    def load_STL10(self, true_label, few=16):
        folder_path = "./data"
        if not os.path.exists(folder_path):
            os.mkdir(folder_path)

        torch.manual_seed(0)
        random.seed(0)
        classes = list(range(10))

        train_dataset = torchvision.datasets.STL10(folder_path, split="train", download=True, transform=transforms.ToTensor())
        selected_index_list = []
        for cls in classes:
            cls_index_list = [i for i in range(len(train_dataset)) if train_dataset[i][1] == cls]
            random.shuffle(cls_index_list)
            selected_index_list.extend(cls_index_list[:few])

        train_dataset = torch.utils.data.Subset(train_dataset, selected_index_list)
        x_train = np.array([train_dataset[i][0].numpy() for i in range(len(train_dataset))])
        y_train = np.array([train_dataset[i][1] for i in range(len(train_dataset))])

        test_dataset = torchvision.datasets.STL10(folder_path, split="test", download=True, transform=transforms.ToTensor())
        x_test = np.array([test_dataset[i][0].numpy() for i in range(len(test_dataset))])
        y_test = np.array([test_dataset[i][1] for i in range(len(test_dataset))])

        x_train = x_train.transpose((0, 2, 3, 1))
        x_test = x_test.transpose((0, 2, 3, 1))

        def resize_dataset(dataset):
            resized = np.zeros((dataset.shape[0], 32, 32, 3))
            for i in range(dataset.shape[0]):
                resized[i] = resize(dataset[i], (32,32))
            return resized

        x_train = resize_dataset(x_train)
        x_test = resize_dataset(x_test)

        x_train = x_train[np.where(y_train==true_label)]
        x_train = self.normalize_img_dataset(np.asarray(x_train, dtype="float32"))
        x_test = self.normalize_img_dataset(np.asarray(x_test, dtype="float32"))
        print("Normalized STL10 dataset is loaded.")
        return x_train, x_test, y_test

    def load_THYROID(self):
        data = scipy.io.loadmat("data/thyroid.mat")
        samples = data['X']
        labels = ((data['y']).astype(np.int32)).reshape(-1)

        normal_samples = samples[labels == 0]
        anomal_samples = samples[labels == 1]

        n_train = len(normal_samples) // 2
        x_train = normal_samples[:n_train]

        normal_test = normal_samples[n_train:]
        anomal_test = anomal_samples
        return self.normalize_tabular_dataset(x_train, normal_test, anomal_test)


from django import forms
from yt_app.models import Tournament, Place, Game, Team, Match
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError

class AddTournamentForm(forms.Form):
    name = forms.CharField()
    description = forms.CharField()
    place = forms.ModelChoiceField(queryset=Place.objects.all())
    game = forms.ModelChoiceField(queryset=Game.objects.all())

class AddPlaceForm(forms.Form):
    name = forms.CharField()
    description = forms.CharField()
    address = forms.CharField()

class AddGameForm(forms.Form):
    game_type = forms.ChoiceField(choices=Game.G_TYPES)
    name = forms.CharField()
    description = forms.CharField()

class AddTeamForm(forms.Form):
    name = forms.CharField()
    tournaments = forms.ModelChoiceField(queryset=Tournament.objects.all())
    players = forms.ModelMultipleChoiceField(queryset=User.objects.all())

class AddMatchForm(forms.Form):
    name = forms.CharField()
    team1 = forms.ModelChoiceField(queryset=Team.objects.all())
    team2 = forms.ModelChoiceField(queryset=Team.objects.all())
    tournament = forms.ModelChoiceField(queryset=Tournament.objects.all())

    def clean(self):
        cleaned_data = super().clean()

        team1 = cleaned_data.get('team1')
        team2 = cleaned_data.get('team2')

        if team1 == team2:
            raise forms.ValidationError('Teams have to be different')

class AddDuelForm(forms.Form):
    match_field = forms.ModelChoiceField(queryset=Match.objects.all(), label='Match')

class UpdateTeamForm(forms.Form):
    team = forms.ModelChoiceField(queryset=Team.objects.all())
    tournaments_ch= forms.ModelMultipleChoiceField(queryset=Tournament.objects.all(), label='Change tournaments')
    players_ch = forms.ModelMultipleChoiceField(queryset=User.objects.all(), label='Change players')


import logging
from ssl import SSLError

import requests

from models.company_analytic_response import CompanyAnalyticResponse
from models.enums.analytic_result_type_enum import AnalyticResultType
from agents.validators.site.base.validator_site_simple_base import ValidatorSiteSimpleBase

class ValidatorSSL(ValidatorSiteSimpleBase):

    def __init__(self) -> None:
        super().__init__()

    def handle(self, request: CompanyAnalyticResponse = CompanyAnalyticResponse()) -> CompanyAnalyticResponse:
        logging.info("ValidatorSSL")
        request.ssl = AnalyticResultType.REPROVED

        if self.valid_ssl():
            request.ssl = AnalyticResultType.APPROVED

        if self._next_handler:
            return self._next_handler.handle(request)

        return request

    def valid_ssl(self):
        try:
            try:
                if not self.url:
                    return False

                response = requests.get(self.url)

                if not response.url.lower().startswith('https'):
                    raise SSLError()

            except SSLError as e:
                logging.error(e)
                return False
            else:
                return True

        except Exception as e:
            logging.error(e)
            return False


import itertools
import numpy as np
import mxnet as mx
from mxnet.test_utils import *

curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
sys.path.insert(0, os.path.join(curr_path, '../unittest'))
from common import with_seed

# * GroupAdaGrad
class PyGroupAdaGrad(mx.optimizer.Optimizer):
    """The python reference of Group AdaGrad optimizer.

    Parameters
    ----------
    eps: float, optional
        Small value to avoid division by 0.

    """

    def __init__(self, eps=1e-5, **kwargs):
        super(PyGroupAdaGrad, self).__init__(**kwargs)
        self.float_stable_eps = eps

    def create_state(self, index, weight):
        assert len(weight.shape) == 2
        history = mx.nd.zeros(
            (weight.shape[0], 1), weight.context, stype=weight.stype)
        return history

    def update(self, index, weight, grad, state):
        self._update_count(index)
        lr = self._get_lr(index)
        wd = self._get_wd(index)
        assert wd == 0

        history = state
        grad = grad * self.rescale_grad
        if self.clip_gradient is not None:
            grad = mx.nd.clip(grad, -self.clip_gradient, self.clip_gradient)
        history[:] += mx.nd.mean(mx.nd.square(grad), axis=1, keepdims=True)
        div = lr * grad / mx.nd.sqrt(history + self.float_stable_eps)
        weight[:] -= div

def test_group_adagrad():
    mx.random.seed(0)
    opt1 = PyGroupAdaGrad
    opt2 = mx.optimizer.contrib.GroupAdaGrad
    shape = (3, 4)
    eps_options = [{}, {'eps': 1e-8}]
    cg_options = [{}, {'clip_gradient': 0.4}, {'clip_gradient': 0.5}]
    rg_options = [{}, {'rescale_grad': 0.14}, {'rescale_grad': 0.8}]
    for dtype in [np.float32]:
        for options in itertools.product(eps_options, cg_options, rg_options):
            kwarg = dict(wd=0.0)
            for option in options:
                kwarg.update(option)
            compare_optimizer(
                opt1(**kwarg),
                opt2(**kwarg),
                shape,
                dtype,
                compare_states=False)
            compare_optimizer(
                opt1(**kwarg),
                opt2(**kwarg),
                shape,
                dtype,
                w_stype='row_sparse',
                g_stype='row_sparse',
                compare_states=False)
            compare_optimizer(
                opt1(**kwarg),
                opt2(**kwarg),
                shape,
                dtype,
                g_stype='row_sparse',
                compare_states=False)

@with_seed()
def test_adamw():
    def get_refs(m, v, weight, grad_rescale, beta1, beta2, lr, eta, wd, epsilon, clip_grad=-1):
        if clip_grad >= 0:
            grad_rescale = mx.nd.clip(grad_rescale, -clip_grad, clip_grad)

        mean_ref = beta1*m + (1-beta1)*grad_rescale
        v_ref = beta2*v + (1-beta2)*(grad_rescale**2)
        weight_ref = weight - eta * (lr * mean_ref / (v_ref.sqrt() + epsilon) + weight * wd)
        return mean_ref, v_ref, weight_ref

    def run_adamw_test(nElem=1, aggregate=False):
        aggregate = aggregate or nElem > 1
        rescale_factor = 10
        eta, lr, wd, epsilon = 1, 1, 0.1, 1e-8
        beta1, beta2 = 0.9, 0.999
        clip_gradient = np.random.uniform(rescale_factor, rescale_factor)
        weight, grad, m, v, etas, lrs, wds, weight_ref = [], [], [], [], [], [], [], []
        for i in range(nElem):
            shape = (np.random.randint(3, high=10), np.random.randint(3, high=10))
            weight.append(mx.nd.random.uniform(shape=shape))
            grad.append(mx.nd.random.uniform(-1.0, 1.0, shape=shape))
            m.append(mx.nd.random.uniform(shape=shape))
            v.append(mx.nd.random.uniform(shape=shape))
            etas.append(eta - 1 / np.random.uniform(9, 10))
            lrs.append(lr - 1 / np.random.uniform(9, 10))
            wds.append(wd - 1 / np.random.uniform(95, 105))
            weight_ref.append(weight[i].copy())

        if aggregate:
            kwargs = {'etas': etas, 'lrs': lrs, 'wds': wds}
        else:
            kwargs = {'eta': etas[0], 'lr': lrs[0], 'wd': wds[0]}

        kwargs.update([('epsilon', epsilon), ('beta1', beta1), ('beta2', beta2), ('clip_gradient', clip_gradient)])

        # Test 1: Update is skipped for rescale = nan scalar
        rescale_grad = mx.nd.array([rescale_factor])
        tested_grad = [rescale_grad * 0, rescale_grad * np.nan, rescale_grad * np.inf]
        tested_rescaled_grad = [np.nan]
        tested_rescaled_grad.extend(tested_grad)

        for rescaled_grad in tested_rescaled_grad:
            if aggregate:
                mx.nd.contrib.multi_adamw_update(weight, grad, m, v,
                                                 rescaled_grad, out=weight, **kwargs)
            else:
                mx.nd.contrib.adamw_update(weight[0], grad[0], m[0], v[0],
                                           rescaled_grad, out=weight[0], **kwargs)

            # weights should remain unchanged
            for j in range(nElem):
                assert_almost_equal(weight_ref[j], weight[j])

        # Test 2: Same as Test 1 for multi-precision update
        weight_fp16, grad_fp16, weight_fp16_refs = [], [], []
        for i in range(nElem):
            weight_fp16.append(weight[i].astype('float16'))
            grad_fp16.append(grad[i].astype('float16'))
            weight_fp16_refs.append(weight_fp16[i].copy())

        for rescaled_grad in tested_grad:
            if aggregate:
                mx.nd.contrib.multi_mp_adamw_update(weight_fp16, grad_fp16, m, v, weight,
                                                    rescaled_grad, out=weight_fp16, **kwargs)
            else:
                mx.nd.contrib.mp_adamw_update(weight_fp16[0], grad_fp16[0], m[0], v[0], weight[0],
                                              rescaled_grad, out=weight_fp16[0], **kwargs)

            # weights should remain unchanged
            for i in range(nElem):
                assert_almost_equal(weight_ref[i], weight[i])
                assert_almost_equal(weight_fp16_refs[i], weight_fp16[i])

        # Test 3: Reference normal update
        grad_rescale, weight_test, m_refs, v_refs, weight_refs = [], [], [], [], []
        for i in range(nElem):
            grad_rescale.append(rescale_grad * grad[i])
            m_ref, v_ref, weight_ref = get_refs(m[i], v[i], weight[i], grad_rescale[i], beta1, beta2, lrs[i], etas[i], wds[i], epsilon, clip_gradient)
            m_refs.append(m_ref)
            v_refs.append(v_ref)
            weight_refs.append(weight_ref)
            weight_test.append(weight[i].copy())

        # op normal update
        if aggregate:
            mx.nd.contrib.multi_adamw_update(weight_test, grad, m, v,
                                             rescale_grad, out=weight_test, **kwargs)
        else:
            mx.nd.contrib.adamw_update(weight_test[0], grad[0], m[0], v[0],
                                       rescale_grad, out=weight_test[0], **kwargs)

        # Compare results
        atol = 1e-4 if aggregate else 1e-5
        rtol = 1e-4 if aggregate else None
        for i in range(nElem):
            assert_almost_equal(weight_refs[i], weight_test[i], rtol=rtol, atol=atol)
            assert_almost_equal(m_refs[i], m[i], rtol=rtol, atol=atol)
            assert_almost_equal(v_refs[i], v[i], atol=atol)

        # Test 4: Reference normal multi-precision update
        grad_rescale, m_refs, v_refs, weight_refs, weight_fp16_refs = [], [], [], [], []
        for i in range(nElem):
            grad_rescale.append(rescale_grad * grad_fp16[i].astype('float32'))
            m_ref, v_ref, weight_ref = get_refs(m[i], v[i], weight[i], grad_rescale[i], beta1, beta2, lrs[i], etas[i], wds[i], epsilon, clip_gradient)
            m_refs.append(m_ref)
            v_refs.append(v_ref)
            weight_refs.append(weight_ref)
            weight_fp16_refs.append(weight_ref.astype('float16'))

        # op normal multi-precision update
        if aggregate:
            mx.nd.contrib.multi_mp_adamw_update(weight_fp16, grad_fp16, m, v, weight,
                                                rescale_grad, out=weight_fp16, **kwargs)
        else:
            mx.nd.contrib.mp_adamw_update(weight_fp16[0], grad_fp16[0], m[0], v[0], weight[0],
                                          rescale_grad, out=weight_fp16[0], **kwargs)

        # Compare results
        for i in range(nElem):
            assert_almost_equal(m_refs[i], m[i], rtol=rtol, atol=atol)
            assert_almost_equal(v_refs[i], v[i], atol=atol)
            assert_almost_equal(weight_refs[i], weight[i], rtol=rtol, atol=atol)
            assert_almost_equal(weight_fp16_refs[i], weight_fp16[i], rtol=1e-3, atol=atol)

    # Testing aggregated Adam update for one element
    run_adamw_test(1, aggregate=True)

    # Testing Adam update, if nElem = 0, OR
    #         aggregated Adam update, if nElem > 0
    for nElem in range(6):
        run_adamw_test(nElem+1)

if __name__ == '__main__':
    import nose
    nose.runmodule()


# re package to use regular expressions
import re
# pypdf to extract data from pdf
import pypdf
# tempfile to create temporary files
import tempfile

incidents = []
fp = tempfile.TemporaryFile()

# extractincidents function to extract incidents
def extractincidents(data):
	#print("***Extraction Started***")
	fp.write(data)
	fp.seek(0)
	reader = pypdf.PdfReader(fp)
	page = len(reader.pages)

	for p in range(0, page):
		temp = []
		page_data = reader.pages[p].extract_text()
		line = ""
		dualline = 0
		for i in range(0, len(page_data)):
			if (page_data[i] == '\n'):

				if(line == 'Date / Time Incident Number Location Nature Incident ORI'):
					pass
				else:
					#print(line)
					s1 = ""
					ori = ""
					word_arr = line.split(" ")
					#print(word_arr[0])
					#print(word_arr[1])
					#print(word_arr[2])
					for i in range(3,len(word_arr)):
						if (word_arr[i] == "OK0140200" or word_arr[i] == "EMSSTAT" or word_arr[i] == "14005" or word_arr[i]=="14009"):
							dualline = 0
							break
						else:
							dualline = 1

					if dualline == 0:
						for i in range(3,len(word_arr)):
							if (word_arr[i] == "OK0140200" or word_arr[i] == "EMSSTAT" or word_arr[i] == "14005" or word_arr[i]=="14009"):
								#print(word_arr[i])
								ori = word_arr[i]

							else:
								s1 += word_arr[i] + " "

						pattern = r"\b(?:[A-Z][a-z]+|911 Call Nature Unknown|MVA With Injuries|MVA Non Injury|COP.*)\b"

						match = re.findall(pattern,s1)

						s2 = ""

						if len(match) == 0 and len(s1.strip()) == 0:
							s2 = "Nan"
							s1 = "Nan"
						elif len(match) == 0:
							s2 = "Nan"
						elif len(s1.strip()) == 0:
							s1 = "Nan"
						else:
							if match[0] == "911 Call Nature Unknown" or match[0] =="MVA With Injuries" or match[0] == "MVA Non Injury":
								s2 = match[0]
							else:
								s2 = s1.split(match[0],1)[1]

								if s2.strip().startswith("/"):
									s2 = match[0] + s2.strip()
								else:
									s2 = match[0] + " " + s2.strip()

						#print(s2)
						#print('subtract this :')
						#print(s1,'--',s2)
						if s1 == "Nan":
							lock = "Nan"
						else:
							lock = s1.replace(s2,"")
						#print(lock)
						temp.append(word_arr[0]+" "+word_arr[1])
						temp.append(word_arr[2])
						temp.append(lock)
						temp.append(s2)
						temp.append(ori)
						incidents.append([temp])

					if dualline == 1:
						dualline = 0
						continue

				temp = []
				line = str()
			else:
				line += page_data[i]

	return incidents