import numpy as np
import matplotlib.pyplot as plt

X = ['int8', 'a=1','a=2','a=3','a=4','a=5', 'a=6']
speedup = [
    1.0,
    1.681470079,
    1.73761439,
    1.988074266,
    2.0586388,
    2.13651083,
    2.284971772]

acc = [
    73.36,
    73.358,
    73.334,
    73.35,
    72.59,
    72.462,
    72.186
]

fig, ax1 = plt.subplots()
X_axis = np.arange(len(X))

ax1.bar(X_axis - 0.2, speedup, 0.4, label = 'Speedup', color='tomato',linewidth=1,)

plt.xticks(X_axis, X)
ax1.set_xlabel("Config")
ax1.set_ylabel("Speedup")
ax1.legend()

ax2 = ax1.twinx()
ax2.set_ylabel("Top-1 Acc")
ax2.bar(X_axis + 0.2, acc, 0.4, label = 'Acc', color='deepskyblue',linewidth=1,)
ax2.legend()
# plt.show()

plt.savefig('combined_results.png')
plt.savefig('combined_results.pdf', bbox_inches='tight', transparent=True)


def find_special_trafo(type2_invariants,ker2,P4):
    """
    INPUT: type2_invariants =[A,B,C,E] defining the hyperelliptic curve
    y**2 = (x**2-1)(x**2-A)(Ex**2-Bx+C)
    the 4-torsion kernel ker 4
    OUTPUT: [alpha,beta,gamma,delta,epsilon] defining the coordinate transformation
    from Proposition 4.1 to a Type-1 equation
    [Ap,Bp,Cp,Ep] coeffcients of the Type-1 equation
    """

    [A,B,C,E] = type2_invariants;
    [g10,g11,g12] = ker2[0][:3]
    [g20,g21,g22] = ker2[1][:3]

    # proceed as in Proof of Proposition 4.5
    assert g10 == -g11-1  #check that indeed 1 is a root of g1
    alpha1 = 1
    alpha2 = g10

    """
    either -alpha1 or -alpha2 are a root of g2, which we call beta1,
    the other root is called gamma1
    : beta2 and gamma2 are the roots of (Ex**2-Bx+C)
    Note: We directly apply the transformation t1: x -> (x-alpha2)/(x-alpha1)
    """
    lc = (alpha1-alpha2)*(alpha1-alpha2);
    den_aux = 1/((g21+g21)*g11)
    assert g20 == g21 - alpha1
    beta1 = -g11*g21*g11*den_aux;
    beta2 = (g21-alpha1+alpha2)*g11*(den_aux+den_aux);
    gamma1 = -(alpha2+alpha2)*g21*(den_aux+den_aux);
    gamma2den = B+E*(g21-2);
    gamma2 = (B+E*(g21+g11))/gamma2den;
    lc = (lc+lc)*g21*g11*gamma2den;

    # Transformation (x-alpha2)/(x-alpha1) is applied  to the 4-torsion point P
    [a0,a1,a2,b0,b1] = P4;
    mu = a1*b0-a0*b1;
    deni = 1/((a1 + 1) + a0);
    a0p =  (alpha2*(a1 + alpha2) + a0)*deni;
    a1p =  (a1*g11 - (g10 + a0+ g10 + a0))*deni;
    aux1 = (deni*(alpha2 - 1))**2;
    b0p =  aux1 * (((b1*alpha2+b0) + (b0+b0)*alpha2) + mu*(2+alpha2+a1) - a0*b0);
    b1p =  aux1 * (a0*b0 - (b1 + b0+b0+b0) - (3 + a1)*mu);

    # compute sqrt(beta1*beta2) as explained in Corollary 2.8
    beta12 = beta1*beta2;
    invsqbeta12 = (beta12*b0p*b0p + lc*(a0p-beta12)*(-a1p-beta1-beta2)*a0p*a0p)/(b0p*(b1p*a0p-a1p*b0p)*beta12+lc*((a0p-beta12)*a0p)**2);
    assert invsqbeta12**2 == 1/beta12, "sqrt is incorrect"

    # final transformation
    alpha = invsqbeta12;
    beta = -invsqbeta12*alpha2;
    gamma = 1;
    delta = -alpha1;
    epsilon =  (invsqbeta12*(alpha2-alpha1))**3;
    #auxiliary values to apply the transformation more efficiently
    al2 = alpha*alpha
    al2be = al2*beta
    al2de = al2*delta
    al3 = al2*alpha
    albe = alpha*beta
    alde = alpha*delta
    be2 = beta*beta
    bede = beta*delta
    de2 = delta*delta
    trafo = [alpha,beta,gamma,delta,epsilon,al2,al2be,al2de,al3,albe,alde,be2,bede,de2]

    Ap = (beta1+beta2)*invsqbeta12;
    Bp = (gamma1+gamma2)*invsqbeta12;
    Cp = gamma1*gamma2*invsqbeta12*invsqbeta12;
    Ep = 1/(lc*alpha);
    type1_invariants = [Ap,Bp,Cp,Ep]

    return type1_invariants, trafo

def apply_Mumford_transformation(P,trafo):
    """
    Transformation of point P in Mumford coordinates.
    On the level of curves, this cooresponds to
    x -> (alpha*x + beta)/(gamma*x + delta)
    y -> epsilon*y/(gamma*x + delta)**3
    Note: we assume gamma = 1
    And get some auxiliary information, e.g alde = alpha*delta
    """
    [al,be,ga,de,ep,al2,al2be,al2de,al3,albe,alde,be2,bede,de2] = trafo;

    [a0,a1,a2,b0,b1] = P;

    if a2 == 0:
        print("not implemented");
    else:
        adeni = 1/(-a0 + de*a1 - de2)
        a1p = (al*(a0+a0) + (-alde - be)*a1 + (bede+bede))*adeni
        a0p = (-al2*a0 + albe*a1 - be2)*adeni
        b33 = -b0 + b1*de
        b22 =  al*(b0+b0+b0) - (be + alde+alde)*b1
        b11 = -al2*(b0+b0+b0) + (al2de + albe+albe)*b1
        b00 = al3*b0 - al2be*b1
        b1p = (a0p-a1p*a1p)*b33 + a1p*b22  - b11
        b0p = a0p*(-a1p*b33 + b22) - b00
    return [a0p,a1p,1,b0p,b1p]

def invariant_type2_type1(type2_invariants):
    #from type 2 invariants to type 1
    [A,B,C,E] = type2_invariants
    denom_aux = 1/((1-A)*(E-B+C))
    return [-(2+A+A)*(E-B+C)*denom_aux, (C+C-E-E)*(1-A)*denom_aux, (E+B+C)*(1-A)*denom_aux, (1-A)*(E-B+C)]

def invariant_type1_type2(type1_invariants):
    #from type 1 invariants to type 2
    [A,B,C,E] = type1_invariants
    term = -E*(A-2)
    return [(A+2)/(A-2), 2*term*(C-1),term*(B+C+1), term*(-B+C+1)]

def apply_trafo_type2_type1(P):
    #clearly, this special transformation can be evaluated more efficiently
    alpha = 1
    beta = -1
    gamma = 1
    delta = 1
    epsilon = 2
    trafo = [alpha,beta,gamma,delta,epsilon,1,-1,1,1,-1,1,1,-1,1]

    return apply_Mumford_transformation(P,trafo)

def apply_trafo_type1_type2(P):

    [a0,a1,a2,b0,b1] = P;

    if a2 == 0:
        print("not implemented");
    else:
        adeni = 1/(-a0 - a1 - 1)
        a1p = ((a0+a0)  - 2)*adeni
        a0p = (-a0 + a1 - 1)*adeni
        b33 = -b0 - b1
        b22 =  (b0+b0+b0) + b1
        b11 = - (b0+b0+b0) + b1
        b00 = b0 - b1
        b1p = (a0p-a1p*a1p)*b33 + a1p*b22  - b11
        b0p = a0p*(-a1p*b33 + b22) - b00
    return [a0p,a1p,1,b0p,b1p]

def type1_isogeny(type1_invariants):
    """
    compute the codomain invariants
    """
    [A,B,C,E] = type1_invariants
    if C == 1:
        print("codomain is product of elliptic curves.")
    denom_aux = 1/(1-C)
    Ap = C
    Bp = E + E
    Cp = (B-A*C)*E*denom_aux
    Ep = (A-B)*E*denom_aux

    return [Ap,Bp,Cp,Ep]

def auxiliary_products(type1_invariants):
    [A,B,C,E] = type1_invariants
    AB = A*B
    AC = A*C
    B2 = B*B
    BC = B*C
    C2 = C*C
    CE = C*E
    return [AB,AC,B2,BC,C2,CE]

def auxiliary_doubling(type2_invariants):
    [A,B,C,E] = type2_invariants
    t1 = (A+1)*E - C
    AC = A*C
    return [t1,AC]

def apply_type1_isogeny(P, type1_invariants,aux_iso):
    # Formula from Theorem 4.7

    [A,B,C,E] = type1_invariants
    [AB,AC,B2,BC,C2,CE] = aux_iso

    [a0,a1,a2,b0,b1] = P
    #computing monomials
    a02 = a0*a0
    a0a1 = a0*a1
    a12 = a1*a1
    a0b0 = a0*b0
    a0b1 = a0*b1
    a1b0 = a1*b0
    a12b0 = a12*b0
    a02a1b0 = a02*a1b0
    a0a12b0 = a0*a12b0
    a13b0 = a12*a1b0
    a02b1 = a02*b1
    a03b1 = a0*a02b1
    a02a1b1 = a0a1*a0b1
    a0a12b1 = a0b1*a12
    a02b0 = a02*b0
    a0a1b0 = a0a1*b0
    a0a1b1 = a0a1*b1
    a021b1 = a02*b1
    b02 = b0*b0

    a00 = C2*(a02+a12) + BC*(a0a1+a1) + (B2 - C2 - C2)*a0 + C2
    a11 = (C2 - C)*(a0a1+a0a1+a1+a1) + (BC - B)*(a0+a0+a0+a0)
    a22 = -(C+C)*(a02+a12+1) - (BC + B)*(a0a1+a1) + (-B2 + C2 + C2 - C -C + 2)*(a0+a0)
    a33 = (1-C)*(a0a1+a0a1+a1+a1) + (B - BC)*(a0+a0+a0+a0)
    a44 = a02 + B*(a0a1+a1) + a12 + (B2 - 2)*a0 + 1
    b00 = C*(a02a1b0+a13b0-a03b1-a0a12b1- a0a1b0-a0a1b0+a02b1+a02b1+a1b0-a0b1) + B*(a0a12b0 - a02a1b1-a02b0+a0b0) + AC*(a02b0+a12b0-a0a1b1-a0b0) + AB*(a0a1b0-a02b1)
    b11 = C*(a0a12b0+a0a12b0-a02a1b1-a02a1b1-a02b0+a12b0+b0) + (2)*(a02a1b1 - a0a12b0+a02b0-a0b0) + AC*(a0a1b0+a0a1b0-a02b1-a0b1+a1b0) + A*(-a0a1b0-a0a1b0+a02b1+a02b1) + B*(a0a1b0-a02b1+a0b1) + AB*a0b0
    b22 = - a02a1b0 - a13b0 + a03b1  + a0a12b1 - a1b0 - a0b1 + B*(-a0a12b0+a02a1b1+a02b0-a0b0) + A*(-a02b0-a12b0+a0a1b1-a0b0) + AB*(-a0a1b0+a02b1) + C*(a0a1b0+a0a1b0-a02b1-a02b1+a0b1+a0b1)  + AC*(a0b0+a0b0)
    b33 = - a02b0 - a12b0 + a0b0 + a0b0 - b0 + B*(-a0a1b0+a02b1-a0b1) + A*(-a02b1-a1b0+a0b1) - AB*a0b0
    bden =  (1 - a0) * (b02 - b1*(a1b0-a0b1));
    bden2 = bden*bden

    # Reduction Step
    lam = E*(B-A)*bden2 - (C-1)*b33*b33;
    #assert lam*a00 != 0, "edge case not implemented"

    a00lam = a00*lam
    den_aux = 1/(a00lam*bden)
    den = den_aux*bden
    a0p = (CE*bden2*(AC-B)+ (1-C)*b00*b00)*a44*den;
    a1p = (((2-C-C)*(CE*bden2 + b00*b11))*a44-a11*a0p*lam)*den;
    bdeni = den_aux*a00lam;
    b1p = (-b11+b22*a1p+b33*(a0p-a1p*a1p))*bdeni;
    b0p = (-b00+(b22-b33*a1p)*a0p)*bdeni;

    return [a0p,a1p,1,b0p,b1p]


import os
import requests

# This is a script you keep to yourself and run locally each time
# you want to create/update your application's commands

# global commands are cached and only update every hour
# url = f"https://discord.com/api/v8/applications/{APP_ID}/commands"

# while guild commands update instantly
# they"re much better for testing

BOT_TOKEN = os.environ["DISCORD_BOT_TOKEN"]
APP_ID = os.environ["DISCORD_APP_ID"]
GUILD_ID = os.environ["DISCORD_GUILD_ID"]

url = f"https://discord.com/api/v8/applications/{APP_ID}/guilds/{GUILD_ID}/commands"

json = {
  "name": "support",
  "type": 1,
  "description": "Ask for the very best of technical support",
  "options": [{
    "name": "question",
    "description": "Ask your question",
    "type": 3,
    "required": True
  }]
}

response = requests.post(url, headers={
  "Authorization": f"Bot {BOT_TOKEN}"
}, json=json)

print(response.json())


import string
import re

def main():
    plate = input("Plate: ")
    if is_valid(plate):
        print("Valid")
    else:
        print("Invalid")

def is_valid(s):
    if (
        character_limit(s)
        and two_letters(s)
        and not_zero_first(s)
        and punctuation_check(s)
        and middle_numbers(s)
        ):
        return True

def character_limit(chars):
    # max 6 min 2 characters
    if (len(chars) <= 6) and (len(chars) >= 2):
        return True

def two_letters(two):
    # must start with two letters
    if two[0].isalpha():
        if two[1].isalpha():
            return True

def not_zero_first(plate):
    # The first number used cannot be a ‘0’
    if plate.isalpha():
        return True
    elif plate.isalnum():
        i = [x.isnumeric() for x in plate].index(True)
        #return True
        #if i == range(0,9):
        if int(plate[i]) != 0:
            return True
    else:
        return False

def punctuation_check(plate_chars):
    # “No periods, spaces, or punctuation marks are allowed.”
    if plate_chars.isalnum():
        return True

def middle_numbers(end):
    # Numbers cannot be used in the middle of a plate; they must come at the end.
    # Validate if string has numbers
    if end.isalpha():
        return True

            #  If string has numbers, confirm there are no letters after numbers
            # numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
    elif end.isalnum():
        list = [char for char in end]
        any_numbers = False
        for i in list:
            if i.isdigit():
                if i=='0' and not any_numbers:
                    return False
                any_numbers = True
            elif i.isalpha():
                if any_numbers:
                    return False
            else:
                return False
        return True

    # if a number has a letter after it = bad
    #if x.index[i].isdigit() and x.index[i+1].isalpha():
    #    return False

main()


"""
@author: Li Xi
@file: sensitive.py
@time: 2019/11/3 14:19
@desc: 敏感检测
用字典的方法检测文本中的敏感词。
使用方法见文件最下方，目前的做法是将敏感词用特定字符mask掉。
"""
import os

from tools.search import DFASearch

class Sensitive(DFASearch):
    def mask(self, content, mask_with="*"):
        result = self.search(content)
        for x in result:
            content = content.replace(x, mask_with * len(x))
        return content

if __name__ == "__main__":
    sen = Sensitive()
    content = "hello sexy baby"
    content_with_mask = sen.mask(content)  # mask掉敏感词
    print(content_with_mask)
    # hello ***y baby


import cv2
import numpy as np
'''
bilinear interpolation
'''

def bilinear_interpolation(img_src, height_dst, width_dst):
    h_src, w_src, ch_src = img_src.shape
    img_dst = np.zeros([height_dst, width_dst, ch_src], dtype=img_src.dtype)
    ratio_h = h_src / height_dst
    ratio_w = w_src / width_dst
    for i in range(ch_src):
        for j in range(height_dst):
            for k in range(width_dst):
                x_src = (k + 0.5) * ratio_w
                y_src = (j + 0.5) * ratio_h
                x1_src, y1_src = int(np.floor(x_src)), int(np.floor(y_src))
                x2_src, y2_src = min(x1_src + 1, w_src - 1), min(y1_src + 1, h_src - 1)

                # Matrix operation
                x_diff = np.array([x2_src - x_src, x_src - x1_src]).reshape(1,2)
                y_diff = np.array([y2_src - y_src, y_src - y1_src]).reshape(2,1)
                f_Q11 = img_src[y1_src, x1_src, i]
                f_Q12 = img_src[y2_src, x1_src, i]
                f_Q21 = img_src[y1_src, x2_src, i]
                f_Q22 = img_src[y2_src, x2_src, i]
                f_Q = np.array([[f_Q11, f_Q12],[f_Q21, f_Q22]])
                img_dst[j, k, i] = x_diff.dot(f_Q).dot(y_diff).item(0)

                # General operation
                # r1 = (x2_src - x_src) * img_src[y1_src, x1_src, i] + (x_src - x1_src) * img_src[y1_src, x2_src, i]
                # r2 = (x2_src - x_src) * img_src[y2_src, x1_src, i] + (x_src - x1_src) * img_src[y2_src, x2_src, i]
                # img_dst[j, k, i] = np.floor((y2_src - y_src) * r1 + (y_src - y1_src) * r2)

    return img_dst

img_src = cv2.imread("../lenna.png")
img_dst = bilinear_interpolation(img_src, 1024, 1024)
cv2.imshow("Original imagge", img_src)
cv2.imshow("Bilinear interpolation image", img_dst)
cv2.imwrite("Upsampling by the bilinear interpolation.png", img_dst)
cv2.waitKey()


# Constraints:
# - assumes moment of inertia about center of gravity axis is idealized as a point mass MOI
# - assumes chassis torsional stiffness is infinite (front roll = rear roll)
# - step input is given as a slip angle of 8 deg for both front tires. This can be improved or changed.

import csv
import math

from tireFit import tireFit
from base import base
from kinematics import kinematics

class transient_response(base):
    def __init__(self):
        pass

    def getFittingFunction(self, file, pressureCriteria):
        tireClient = tireFit(file)
        return tireClient.get_fit_object(pressureCriteria)

    def getLateralForce(self, fitFunc, slipangle, camber, normalforce):
        tireClient = tireFit('')
        return tireClient.get_lateral_force(fitFunc, slipangle, camber, normalforce)
    def getWheelRates(self, constants, front_or_rear):
        if (front_or_rear == 'FRONT'):
            tirerate = constants['FTSR']
            wheelrate = constants['FSSR']*constants['FMR']**2
            return ((1.0/tirerate + 1.0/wheelrate)**(-1))
        elif (front_or_rear == 'REAR'):
            tirerate = constants['RTSR']
            wheelrate = constants['RSSR']*constants['RMR']**2
            return ((1.0/tirerate + 1.0/wheelrate)**(-1))

    def getRollStiffness(self, constants, front_or_rear, track):
        # roll stiffness is torque/deg of roll. Assume +- 1 inch displacement on each wheel.
        # F = 1in*wheelrate on each wheel. Torque = F*track
        # angle = arctan(2in/track)
        wheelrate = self.getWheelRates(constants, front_or_rear)
        angle = math.degrees(math.atan(2.0/track))
        return (12*wheelrate/angle)

    def interpolate(self, xvals, yvals, x):
        for i in range(0, len(xvals)-1):
            if (x >= xvals[i] and x <= xvals[i+1]):
                x0 = xvals[i]
                x1 = xvals[i+1]
                y0 = yvals[i]
                y1 = yvals[i+1]
                return ((x - x0)/(x1 - x0) * (y1 - y0) + y0)
        # throw error if point isn't within the data set
        raise ValueError("ERROR: Trying to interpolate a point outside the data range.")

    def getCamberFromNormalForce(self, curves, normal_force, nominal_normal_force):
        dF = nominal_normal_force - normal_force
        return self.interpolate(curves[0], curves[1], dF)

    def getRCHfromNormalForce(self, curves, normal_force, nominal_normal_force):
        dF = nominal_normal_force - normal_force
        return self.interpolate(curves[0], curves[2], dF)

    def calculateSettlingTime(self, dt, dyawdtlist):
        maxyawgrad = max(dyawdtlist)
        for i in range(0, len(dyawdtlist)):
            if (dyawdtlist[i]/maxyawgrad < 0.01): # if yaw gradient is less than 1% of max yaw gradient
                converged = True
                for j in range(i, len(dyawdtlist)): # check that remaining steps do not exceed 1%
                    if (abs(dyawdtlist[j])/abs(maxyawgrad) > 0.01):
                        converged = False
                if (converged):
                    return(dt*i/100.0)

    def calculateOvershoot(self, dyawdtlist):
        maxyawgrad = max(dyawdtlist)
        minyawgrad = min(dyawdtlist)
        if (minyawgrad > 0.00):
            return (0.00)
        else:
            return (-minyawgrad/maxyawgrad*100.0)

    def calculate_response_time(self, tireFile, suspensionFile):
        dt = 0.1 # time step
        const_slip_angle = 8 # step input for front
        con_tol_yawgrad = 0.1 # convergence tolerance for yaw gradient

        pressureCriteria = ['P', 9.5, 10.5]
        fitFunc = self.getFittingFunction(tireFile, pressureCriteria)
        constants = self.read_transient_csv(suspensionFile)

        print('')
        print('')
        print('Reading suspension points (suspension_points.csv) and tire fitting function (TireFit/lateral_tire_fitting_function.csv).')

        # right and up are positive from the driver's view

        # ------------- Constants ----------------
        front_track = constants['FTCP'][1] * 2
        rear_track = constants['RTCP'][1] * 2

        wheelbase = constants['RTCP'][0] - constants['FTCP'][0]
        h_cog = constants['COG']
        longitudinal_MOI = 0.5*(constants['FSM'] + constants['RSM'])*h_cog*h_cog

        FR_ss_normal_force = constants['FUM'] + constants['FSM']
        FL_ss_normal_force = constants['FUM'] + constants['FSM']
        RR_ss_normal_force = constants['RUM'] + constants['RSM']
        RL_ss_normal_force = constants['RUM'] + constants['RSM']

        total_mass = 2*(RR_ss_normal_force + FR_ss_normal_force)
        rear_weight_bias = RR_ss_normal_force/(RR_ss_normal_force + FR_ss_normal_force)

        # --------------------------------------

        # ------------ Get curves --------------
        # get camber and RCH curves
        kinematicsObj = kinematics()
        frontcurves, rearcurves = kinematicsObj.main(suspensionFile, 3, .05)
        print(min(rearcurves[0]), max(rearcurves[0]))
        print(min(frontcurves[0]), max(frontcurves[0]))
        frontwheelrate = self.getWheelRates(constants, 'FRONT')
        rearwheelrate = self.getWheelRates(constants, 'REAR')
        print(frontwheelrate)
        print(rearwheelrate)

        # transform curves as a function of delta z to delta normal force
        dF = [x * frontwheelrate for x in frontcurves[0]] #scale dZ to dF
        frontcurves[0] = dF

        dF = [x * rearwheelrate for x in rearcurves[0]]
        rearcurves[0] = dF
        print(min(rearcurves[0]), max(rearcurves[0]))
        print(min(frontcurves[0]), max(frontcurves[0]))

        #--------------------------------------

        # ------- Initial conditions -----------
        yaw = 0
        dyaw_dt = 0
        roll = 0
        droll_dt = 0
        d2roll_dt2 = 0

        FR_lateral_force = 0
        FL_lateral_force = 0
        RR_lateral_force = 0
        RL_lateral_force = 0

        FR_normal_force = FR_ss_normal_force
        FL_normal_force = FL_ss_normal_force
        RR_normal_force = RR_ss_normal_force
        RL_normal_force = RL_ss_normal_force
        print(FR_normal_force, FL_normal_force, RR_normal_force, RL_normal_force)
        # --------------------------------------

        # ------- Calculate coefficients -------

        front_roll_stiffness = self.getRollStiffness(constants, 'FRONT', front_track) + 12.0*constants['FARS']
        rear_roll_stiffness =  self.getRollStiffness(constants, 'REAR', front_track) + 12.0*constants['RARS']

        # find critical damping coefficients. gamma_crit = sqrt(k*m). k = roll stiffness . m = longitudinal moment of inertia
        front_crit_damping_coeff = math.sqrt(front_roll_stiffness*longitudinal_MOI)
        rear_crit_damping_coeff = math.sqrt(rear_roll_stiffness*longitudinal_MOI)

        # set damping coefficients = damping_ratio*crit_damping_coeff
        front_gamma = constants['FDR']*front_crit_damping_coeff
        rear_gamma = constants['RDR']*rear_crit_damping_coeff

        print(front_roll_stiffness, rear_roll_stiffness, front_crit_damping_coeff, rear_crit_damping_coeff, front_gamma, rear_gamma)
        # --------------------------------------

        # Open output file for writing
        results_yaw      = []
        results_dyawdt   = []
        results_roll     = []
        csvfile = open('output.csv', 'w')
        writer = csv.writer(csvfile, delimiter=',')
        writer.writerow(['t', 'YawGradient', 'Roll', 'FL normal force', 'FR normal force', 'RL normal force', 'RR normal force'])

        print('Starting simulation...')

        for i in range(0, 1000):
            # get new roll center heights and cambers from the normal forces
            front_RCH = .5*self.getRCHfromNormalForce(frontcurves, FR_normal_force, FR_ss_normal_force) + .5*self.getRCHfromNormalForce(frontcurves, FL_normal_force, FL_ss_normal_force)
            rear_RCH = .5*self.getRCHfromNormalForce(rearcurves, RR_normal_force, RR_ss_normal_force) + .5*self.getRCHfromNormalForce(rearcurves, RL_normal_force, RL_ss_normal_force)
            frontleft_camber = self.getCamberFromNormalForce(frontcurves, FL_normal_force, FL_ss_normal_force)
            frontright_camber = self.getCamberFromNormalForce(frontcurves, FR_normal_force, FR_ss_normal_force)
            rearleft_camber = self.getCamberFromNormalForce(rearcurves, RL_normal_force, RL_ss_normal_force)
            rearright_camber = self.getCamberFromNormalForce(rearcurves, RR_normal_force, RR_ss_normal_force)

            # deltaF = roll (deg)  * roll_stiffness(ft-lbs/deg) / track (in) * (12in/ft)
            deltaF = roll*front_roll_stiffness*(h_cog - front_RCH)/h_cog/front_track
            print(deltaF)
            # deltaF is split between the two wheels
            FR_normal_force = FR_ss_normal_force + (0.5*deltaF)
            FL_normal_force = FL_ss_normal_force - (0.5*deltaF)

            # Repeat for rear
            deltaF = roll*rear_roll_stiffness*(h_cog - rear_RCH)/h_cog/rear_track
            RR_normal_force = RR_ss_normal_force + (0.5*deltaF)
            RL_normal_force = RR_ss_normal_force - (0.5*deltaF)

            #check if normal force goes negative. If it does, set it to 0
            if (FR_normal_force < 0):
                FR_normal_force = 0
            if (FL_normal_force < 0):
                FL_normal_force = 0
            if (RR_normal_force < 0):
                RR_normal_force = 0
            if (RL_normal_force < 0):
                RL_normal_force = 0

            # calculate lateral forces using tire fit function for a given slip angle, camber, and normal force
            FR_lateral_force = self.getLateralForce(fitFunc, const_slip_angle, frontright_camber, FR_normal_force)
            FL_lateral_force = self.getLateralForce(fitFunc, const_slip_angle, -frontleft_camber, FL_normal_force)
            RR_lateral_force = self.getLateralForce(fitFunc, yaw - constants['RT'], rearright_camber, RR_normal_force)
            RL_lateral_force = self.getLateralForce(fitFunc, yaw + constants['RT'], -rearleft_camber, RL_normal_force)

            print(FR_lateral_force, 'fr', FR_normal_force)
            print(FL_lateral_force, 'fl', FL_normal_force)
            print(RR_lateral_force, 'rr', RR_normal_force)
            print(RL_lateral_force, 'rl', RL_normal_force)

            # calculate total front and rear lateral forces
            front_force = FR_lateral_force - FL_lateral_force
            rear_force = RR_lateral_force - RL_lateral_force

            # calculate roll torque as a function of the lateral forces and their moment arms with damping in the yz plane
            front_roll_torque = front_force*(h_cog - front_RCH) - front_gamma*droll_dt
            rear_roll_torque = rear_force*(h_cog - rear_RCH) - rear_gamma*droll_dt

            # 2nd derivative of roll is the roll torque divided by moment of inertia
            d2roll_dt2 = (front_roll_torque + rear_roll_torque)/longitudinal_MOI

            # yaw torque is the difference in torques between the front lateral force and rear lateral force on the yaw center (assumed to be center of gravity) in xy plane
            yawtorque = front_force*(1 - rear_weight_bias)*wheelbase - rear_force*rear_weight_bias*wheelbase

            # derivative of yaw is the yaw torque divided by the polar moment of inertia. (Not 2nd derivative because we consider yaw to be in the car's reference frame)
            dyaw_dt = 57.296*yawtorque/constants['PMOI'] #57.296 rad/deg

            # Step in time
            yaw += dt*dyaw_dt
            roll += dt*droll_dt
            droll_dt += dt*d2roll_dt2

            results_yaw.append(yaw)
            results_dyawdt.append(-dyaw_dt)
            results_roll.append(roll)

            #write solution
            if (i%10 == 0):
                writer.writerow([i/100.*dt, -dyaw_dt, roll, FL_normal_force, FR_normal_force, RL_normal_force, RR_normal_force])

        err = abs(max(results_dyawdt[-100:], key = abs)) # max absolute value of yaw gradient in last 100 iterations
        if (err < con_tol_yawgrad):
            print('Solution converged to yaw gradient tolerance: ' + str(err))
        else:
            print('Solution did not converge to specified yaw gradient tolerance. Results may be inaccurate. Yaw gradient deviation in last 100 steps: '+ str(err))

        print('')
        print('Results:')
        print('Transient settling time                  :       %.3f' % self.calculateSettlingTime(dt, results_dyawdt) + ' s') #find response time to reach less than 1% of max yaw gradient
        print('Negative yaw-gradient overshoot (%% of max gradient) :       %.1f' % self.calculateOvershoot(results_dyawdt) + ' %')
        print('Steady state yaw                 :       %.1f' % (sum(results_yaw[-100:])/100.0) + ' deg') #average yaw in last 100 iterations
        print('Steady state roll                    :       %.1f' % (sum(results_roll[-100:])/100.0) + ' deg') #average roll in last 100 iterations

if __name__ == "__main__":
    obj = transient_response()
    tireFile = 'A1965run18.csv'
    suspensionFile = 'suspension_points.csv'
    obj.calculate_response_time(tireFile, suspensionFile)


# -*- coding: utf-8 -*-
"""
Created on Fri Jun 12 09:38:21 2020

@author: juand
"""
import ee,datetime

def get_forest_mask(date_start_detection,yearly_mask,optical_mask,sar_mask,sar_tmp_mask):
    """
    Returns a forest mask, computed as the negation of the addition of all available rasterized non-forest masks:
        - Acummulated 2019 deforestation (PRODES/INPE), hidrology (INPE), non forest (INPE) and varzea mask (INPE)
        - Ongoing optical deforestation warnings (DETER), until date_start_detection
        - DETERSAR CR2 warnings until the actual run
        - DETERSAR_CR2 warnings computed on the actual run
    Args:
        date_start_detection (ee.Date): Date of the start of SAR detection. Used to mask out any posterior optical deforestation data.
        yearly_mask
        optical_mask (STR): Current rasterized mask of DETER. Should have unique band representing the number of elapsed days from 2018-01-01.
                            Use 'precompute_deforest_mask' to compute this image from DETER polygons.
        sar_mask (STR):    Accumulated detections of DETERSAR
        sar_tmp_mask (ee.FeatureCollection): Feature Collection of the CR2 polygons detected during the current run
    Returns:
        ee.Image: mask representing forested pixels.

    """
    deforestationMaskRaster=make1sFromMask(ee.Image(yearly_mask)).unmask(0,False)
    days_from_1jan2018=date_start_detection.difference(ee.Date('2018-01-01'),'day')
    if optical_mask==None:
        deforestationMaskRaster_DETER=ee.Image(0)
    else:
        deforestationMaskRaster_DETER=ee.Image(optical_mask).lt(ee.Image(days_from_1jan2018)).unmask(0,False)
    if sar_mask==None:
        deforestationMaskRaster_DETERSAR=ee.Image(0)
    else:
        deforestationMaskRaster_DETERSAR=make1sFromMask(ee.Image(sar_mask)).unmask(0,False)
    deforestationMaskRaster_DETERSAR_TMP=ee.Image(sar_tmp_mask.map(lambda ft:ft.set('desm',1)).reduceToImage(['desm'],'first')).unmask(0,False)
    deforestationMask=deforestationMaskRaster.Or(deforestationMaskRaster_DETER).Or(deforestationMaskRaster_DETERSAR).Or(deforestationMaskRaster_DETERSAR_TMP)
    return deforestationMask.Not()

def make1sFromMask(img):
    return img.selfMask().add(1).divide(img.add(1))

def precompute_deforest_mask(warning_vector_asset="users/juandb/DETER_SAR_PRODUCAO/deter-amz-public-2020Mai26",output_asset="users/juandb/DETER_SAR_PRODUCAO/deter_amz_public_2020Mai26_CR_raster"):
    DETER_CR = ee.FeatureCollection(warning_vector_asset) \
                .filterMetadata("CLASSNAME","equals","DESMATAMENTO_CR")
    DETER_CR_VEG = ee.FeatureCollection(warning_vector_asset) \
                .filterMetadata("CLASSNAME","equals","DESMATAMENTO_VEG")
    def add_date(ft):return ft.set("days_from_1jan2018",ee.Date(ft.get("VIEW_DATE")).difference(ee.Date('2018-01-01'),'day'))
    DETER=DETER_CR.merge(DETER_CR_VEG).map(add_date).filterMetadata("days_from_1jan2018","greater_than",212)
    print (DETER.size().getInfo())
    deforestationMaskRaster_DETER=DETER.reduceToImage(["days_from_1jan2018"],ee.Reducer.mean()).rename('days_from_2018').toInt16()
    print(deforestationMaskRaster_DETER.getInfo())
    task=ee.batch.Export.image.toAsset(image=deforestationMaskRaster_DETER,description="export_forest_mask",assetId=output_asset,scale=20,maxPixels=3400123231231)
    return task

def rasterize_deforestation_polygons(pols_asset="users/juandb/DETER_SAR_PRODUCAO/DETERSAR_CR2_TRANSMITIDOS_4AREAS_ACUMULADO",output_asset="users/juandb/DETER_SAR_PRODUCAO/DETERSAR_CR2_TRANSMITIDOS_4AREAS_ACUMULADO_raster"):
    raster=ee.FeatureCollection(pols_asset).map(lambda ft:ft.set('desm',1)).reduceToImage(['desm'],'first')
    task=ee.batch.Export.image.toAsset(image=raster,description="rasterize",assetId=output_asset,scale=20,maxPixels=3400123231231)
    return task

if __name__== '__main__':
    task16=rasterize_deforestation_polygons()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Author rsg
#
import os
import ujson
import logging
import argparse
import subprocess

def get_config():
    with open("config.json", mode="r", encoding="utf8") as config_file:
        config = config_file.read()
        config_dict = ujson.loads(config)

    return config_dict

def init_logger(path):
    """初始化日志记录器

    :param path: 日志路径
    :return:
        logger
    """

    logger = logging.getLogger("file_dispatcher")
    logger.setLevel(level=logging.INFO)
    file_handler = logging.FileHandler(filename=path, mode="a",
                                       encoding="utf8")
    logger.addHandler(file_handler)
    formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    file_handler.setFormatter(formatter)

    return logger

class PathParser(object):
    def __init__(self, path):
        self._path = path

    @property
    def extend(self):
        root, ext = os.path.splitext(self._path)
        return ext

    @property
    def path(self):
        return self._path

    @property
    def filename(self):
        return os.path.split(self._path)[-1]

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("path", help="file path.", action="store")
    args = parser.parse_args()
    config = get_config()
    logger = init_logger(config["log_path"])

    if not os.path.exists(args.path):
        # logger.info("file path {path} does not exists.".format(path=args.path))
        exit(0)

    path_parser = PathParser(args.path)
    dest_path = config["destination"].get(path_parser.extend)
    if not dest_path:
        logger.info(
            "There is no destination for {path}".format(path=path_parser.path))
        exit(0)

    if os.path.exists(os.path.join(dest_path, path_parser.filename)):
        logger.info("The destination have same name file. {filename}".format(
            filename=path_parser.filename))
        exit(0)

    subprocess.call(["mv", path_parser.path, dest_path])
    logger.info("Moved {origin} to {dest}".format(origin=path_parser.path,
                                                  dest=dest_path))


from django.template import loader
from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from services.EventsTypeService import EventsTypeService
from django.views.decorators.http import require_http_methods
from django.views.decorators.csrf import csrf_protect
from django.contrib import messages
from .forms import EventsTypeForm
import datetime

@require_http_methods(['GET'])
def index(request):

    events_type_service = EventsTypeService()

    events_type = events_type_service.getEventsType()

    context = {
        'events_type' : events_type,
        'titulo' : 'titulo'
    }

    return render(request, 'Admin/Events_type/index_events_type.html', context)

@require_http_methods(['GET'])
def create_index(request):

    context = {
        'titulo' : 'titulo'
    }

    return render(request, 'Admin/Events_type/new_events_type.html', context)

@require_http_methods(['POST'])
@csrf_protect
def create_eventstype(request):

    form = EventsTypeForm(request.POST)

    context = {
        'titulo' : 'titulo'
    }

    insert_data = {}

    if not form.is_valid():

        errors =  form.errors.as_data()

        for error in errors:

            validation_instance = errors[error]

            for instance_error in validation_instance:

                for error_message in instance_error:

                    messages.error(request,(error_message))

        return render(request,'Admin/Events_type/new_events_type.html',context)

    else:

        insert_data["name"] = request.POST.get('name')

        insert_data["description"] = request.POST.get('description')

        insert_data["status"] = 1

        events_type_service = EventsTypeService()

        events_type_service.create(insert_data)

        return HttpResponseRedirect(reverse('events_type:index'))

@require_http_methods(['GET'])
def update_index(request,type_id):

    events_type_service = EventsTypeService()

    filters = {
        'id' : type_id
    }

    event_type   =  events_type_service.filter(filters)

    context = {
        'titulo' : 'titulo',
        'event_type' : event_type
    }

    return render(request,'Admin/Events_type/edit_events_type.html',context)

@require_http_methods(['POST'])
@csrf_protect
def update_eventstype(request,type_id):

    insert_data = {}

    if request.POST["name"]:
        insert_data["name"] = request.POST["name"]

    if request.POST["description"]:
        insert_data["description"] = request.POST["description"]

    if request.POST["status"]:
        insert_data["status"] = request.POST["status"]

    events_type_service = EventsTypeService()

    id_type = int('0' + type_id)

    events_type_service.update(id_type,insert_data)

    return HttpResponseRedirect(reverse('events_type:index'))


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

from sqlalchemy import Column, Integer, String, ForeignKey, Text, TIMESTAMP
from sqlalchemy.orm import relationship, backref
from doyou.database import Base

class Location(Base):
  """
  地域字典表
  """
  __tablename__ = 'location'

  id = Column(Integer, nullable=False, primary_key=True)
  name = Column(String(128), nullable=False)

  def __init__(self, id=None, name=None):
    self.id = id
    self.name = name

  def __repr__(self):
    return "<Location('%s', '%s')>" % (str(self.id), self.name)


alist=[4,2,3,1]
alist_len=len(alist)
alist_min=min(alist)
sorted_alist=sorted(alist)
print(alist)
print(sorted_alist)
aset={4,2,3,1}
aset_len=len(aset)
aset_min=min(aset)
adict={'a':1,'b':2,'c':3}
adict_len=len(adict)
adict_min=min(adict)
print(adict_min)

list3=3 in alist
set3=3 in aset
dict3=3 in adict
dictc='c' in adict
print(list3)
print(set3)
print(dict3)
print(dictc)

Functions, Methods for Container Data Types project.py


import numpy as np
from pyqtgraph.parametertree import Parameter, ParameterTree, ParameterItem, registerParameterType
from pyqtgraph.parametertree import types as pTypes
from HdfDoc import HdfDoc
from GateDocView import GateDocView
from Event import Event

class AscanDocView(pTypes.GroupParameter):
    def __init__(self, gate_docview, name, hdf_doc: HdfDoc, a_scan, **opts):
        super().__init__(name=name)
        self.hdf_doc = hdf_doc
        self.a_scan = a_scan
        self.signal_ascanUpdatedEvent = None
        self.gate_docview: GateDocView = gate_docview
        self.gate_docview.ij_change_event += self.on_update_ij_pos
        self.ascan_changed_event = Event()

        self.fwf_roi = self.get_fwfroi_default_location()

    def set_ascan(self, a_scan):
        self.a_scan = a_scan
        self.ascan_changed_event(self.a_scan)

    def on_update_ij_pos(self, i_indx, j_indx):
        a_scan = self.hdf_doc.get_a_scan(i_indx, j_indx)
        self.set_ascan(a_scan)

    def get_fwfroi_default_location(self):
        signal_len = len(self.a_scan)
        left = signal_len / 10
        top = 0.5 * np.max(self.a_scan)
        bottom = 0.5 * np.min(self.a_scan)
        width = left
        height = top - bottom
        return {'left': left, 'bottom': bottom, 'width': width, 'height': height}

    def set_fwf_roi(self, left, bottom, width, height):
        self.fwf_roi = {'left': left, 'bottom': bottom, 'width': width, 'height': height}
        self.gate_docview.set_fwf_arr(left, bottom, width, height)

    def get_fwf_roi(self):
        return self.fwf_roi

    def save_ascan_to_txt_file(self, file_name):
        t_min = self.gate_docview.get_tmin_param().value()
        t_max = self.gate_docview.get_tmax_param().value()
        np.savetxt(file_name, self.a_scan[t_min:t_max])


from __future__ import unicode_literals
import youtube_dl
import os
import numpy as np
import librosa
from scipy.io import wavfile
#import multiprocessing as mp
#import multiprocessing_logging
#import logging.handlers
#worker_args = [ytid, ts_start, ts_end, data_dir, ffmpeg_path, ffprobe_path]
#    pool.apply_async(partial(segment_mp_worker, **ffmpeg_cfg), worker_args)

MAXINT16 = np.iinfo(np.int16).max

extensionsToCheck = ['.m4a', '.webm', '.part', '.wav', '.ytdl']

#yt_embed_url_form = "http://www.youtube.com/embed/{video_id}?start={start_sec}&end={end_sec}"
yt_full_url_form = "http://www.youtube.com/watch?v={video_id}"
# yt_embed_url_form = "http://www.youtube.com/v/{video_id}?version=3&start={start_sec}&end={end_sec}&autoplay=0&hl=en_US&rel=0"

class MyLogger(object):
    def debug(self, msg):
        pass

    def warning(self, msg):
        pass

    def error(self, msg):
        print(msg)

def my_hook(d):
    if d['status'] == 'finished':
        print('Done downloading, now converting ...')

def download_youtube_wav(video_id,
                        raw_dir=None,
                        short_raw_dir=None,
                        start_sec=0,
                        duration=10,
                        sample_rate=16000):

    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'wav',
            'preferredquality': '192',
        }],
        'logger': MyLogger(),
        'progress_hooks': [my_hook],
    }

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        #if start_sec is not None and end_sec is not None:
        # ydl.download(
        #     [
        #         yt_embed_url_form.format(
        #             video_id=video_id,
        #             start_sec=int(start_sec),
        #             end_sec=int(end_sec)
        #         )
        #     ]
        # )
        # else:
        try:
            ydl.download(
                [yt_full_url_form.format(video_id=video_id)]
            )
        except:
            print(video_id, 'not available for download anymore.')

    save_full_raw = raw_dir is not None and isinstance(raw_dir, str)
    save_short_raw = short_raw_dir is not None and isinstance(short_raw_dir, str)
    video_title = None

    for filename in os.listdir('.'):
        if video_id in filename:
            if '.wav' in filename:
                video_title = filename[:-16]
                if save_short_raw:
                    new_short_raw_name = os.path.join(
                        short_raw_dir,
                        filename[-15:]
                    )

                    print(
                        video_title
                        ,
                        '\n\tshort clip saved in:\n\t\t',
                        os.path.join(*new_short_raw_name.split('/')[-3:])
                    )

                    # Load and downsample audio to sample_rate
                    # audio is a 1D time series of the sound
                    # can also use (audio, fs) = soundfile.read(audio_path)
                    (audio, fs) = librosa.load(
                        filename,
                        sr=sample_rate,
                        offset=start_sec,
                        duration=duration
                    )

                    # Store downsampled 10sec clip under data/short_raw/
                    wavfile.write(
                        filename=new_short_raw_name,
                        rate=sample_rate,
                        data=(audio * MAXINT16).astype(np.int16)
                    )

                if save_full_raw:
                    new_raw_name = os.path.join(
                        raw_dir,
                        filename[-15:]
                    )
                    print(
                        video_title,
                        '\n\tfull clip saved in:\n\t\t',
                        os.path.join(*new_raw_name.split('/')[-3:])
                    )
                    os.rename(filename, new_raw_name)

                else:
                    os.remove(filename)
            elif any(ext in filename for ext in extensionsToCheck):
                os.remove(filename)

    return video_title

#with wave.open(video_id + '.wav') as wf:

# youtube-dl -o "%(id)s.wav" -f 'bestaudio/best' --audio-format wav BaW_jenozKc


from __future__ import print_function
import numpy as np
np.random.seed(1337)  # for reproducibility
import h5py
import datetime
import numpy as np
import keras.backend as K
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Layer, Activation, BatchNormalization
from keras.optimizers import SGD, Adam, RMSprop
from keras.callbacks import LearningRateScheduler
from keras.utils import np_utils
import keras
import tensorflow as tf
import time

from binary_ops import binary_tanh as binary_tanh_op
from binary_ops import binary_tanh1
from binary_layers import BinaryDense, BinaryDense1, BinaryDense2, BinaryDense3, Clip, BinaryConv2D
#############################################################################################
from keras.models import load_model

tf.compat.v1.disable_eager_execution()

class DropoutNoScale(Dropout):
    '''Keras Dropout does scale the input in training phase, which is undesirable here.
    '''
    def call(self, inputs, training=None):
        if 0. < self.rate < 1.:
            noise_shape = self._get_noise_shape(inputs)

            def dropped_inputs():
                return K.dropout(inputs, self.rate, noise_shape,
                                 seed=self.seed) * (1 - self.rate)
            return K.in_train_phase(dropped_inputs, inputs,
                                    training=training)
        return inputs

def binary_tanh(x):
    return binary_tanh_op(x)

##########################################
o0=[3,10,13,25,28,55,69,71,101,126,136]
o1=[2,5,14,29,31,37,39,40,46,51]
o2=[1,35,38,43,47,72,77,82,106,119]
o3=[18,30,32,44,51,63,68,76,87,90]
o4=[4,6,19,24,27,33,42,48,49,56]
o5=[8,15,23,45,52,53,59,102,120,127]
o6=[11,21,22,50,54,66,81,88,91,98]
o7=[0,17,26,34,36,41,60,64,70,75]
o8=[61,84,110,128,134,146,177,179,181,184]
o9=[7,9,12,16,20,58,62,73,78,92]

model = np.load("weight_retrain15.npy")
############middle layer output#################
inp = model.input                                           # input placeholder
outputs = [layer.output for layer in model.layers]          # all layer outputs
functor = K.function([inp]+ [K.learning_phase()], outputs) # evaluation function
functor1 = K.function([inp], outputs )

## Testing 1

#for i in range(10):
#    test = np.random.random(X_test[1].shape)[np.newaxis,...]
#layer_outs = functor([X_test[0], 1.]) #with dropout
#    layer_outs = functor([X_test[1][np.newaxis,...], 0.]) #without dropout
#    print("###################")
#print
 #   h=layer_outs[o1[i]][0][0]
 #   for j in range(10):
  #      diff=h-layer_outs[1][0][j]
  #      print(h)

#########################################################################33
#import matplotlib.pyplot as plt
#plt.plot()
#plt.plot(history.history['val_acc'])
#plt.title('model accuracy')
#plt.ylabel('accuracy')
#plt.xlabel('epoch')
#plt.legend(['test'], loc='upper left')
#plt.show()

#plt.plot(history.history['val_loss'])
#plt.title('model loss')
#plt.xlabel('epoch')
#plt.legend(['test'], loc='upper left')
#plt.show()

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

#print(layer_outs)

for i in range (10):
## Testing 1
    test = np.random.random(X_test[o0[i]].shape)
##layer_outs = functor([X_test[0], 1.]) #with dropout
    layer_outs1 = functor1([X_test[o0[i]][np.newaxis,...], 0.]) #without dropout
    print(0)
    print(layer_outs1[10])

for i in range (10):
## Testing 1
    test = np.random.random(X_test[o1[i]].shape)
##layer_outs = functor([X_test[0], 1.]) #with dropout
    layer_outs1 = functor1([X_test[o1[i]][np.newaxis,...], 0.]) #without dropout
    print(1)
    print(layer_outs1[10])

for i in range (10):
    test2 = np.random.random(X_test[o2[i]].shape)
##layer_outs = functor([X_test[0], 1.]) #with dropout
    layer_outs2 = functor1([X_test[o2[i]][np.newaxis,...], 0.]) #without dropout
    print(2)
    print(layer_outs2[10])

for i in range (10):
    test3 = np.random.random(X_test[o3[i]].shape)
##layer_outs = functor([X_test[0], 1.]) #with dropout
    layer_outs3 = functor1([X_test[o3[i]][np.newaxis,...], 0.]) #without dropout
    print(3)
    print(layer_outs3[10])

for i in range(10):
    test4 = np.random.random(X_test[o4[i]].shape)
##layer_outs = functor([X_test[0], 1.]) #with dropout
    layer_outs4 = functor1([X_test[o4[i]][np.newaxis,...], 0.]) #without dropout
    print(4)
    print(layer_outs4[10])

for i in range(10):
    test5 = np.random.random(X_test[o5[i]].shape)
##layer_outs = functor([X_test[0], 1.]) #with dropout
    layer_outs5 = functor1([X_test[o5[i]][np.newaxis,...], 0.]) #without dropout
    print(5)
    print(layer_outs5[10])

for i in range(10):
    test6 = np.random.random(X_test[o6[i]].shape)
##layer_outs = functor([X_test[0], 1.]) #with dropout
    layer_outs6 = functor1([X_test[o6[i]][np.newaxis,...], 0.]) #without dropout
    print(6)
    print(layer_outs6[10])

for i in range(10):
    test7 = np.random.random(X_test[o7[i]].shape)
##layer_outs = functor([X_test[0], 1.]) #with dropout
    layer_outs7 = functor1([X_test[o7[i]][np.newaxis,...], 0.]) #without dropout
    print(7)
    print(layer_outs7[10])

for i in range(10):
    test8 = np.random.random(X_test[o8[i]].shape)
##layer_outs = functor([X_test[0], 1.]) #with dropout
    layer_outs8 = functor1([X_test[o8[i]][np.newaxis,...], 0.]) #without dropout
    print(8)
    print(layer_outs8[10])

for i in range(10):
    test9 = np.random.random(X_test[o9[i]].shape)
##layer_outs = functor([X_test[0], 1.]) #with dropout
    layer_outs9 = functor1([X_test[o9[i]][np.newaxis,...], 0.]) #without dropout
    print(9)
    print(layer_outs9[10])

#print(layer_outs2)
#print(layer_outs[10][0][1])
#print(layer_outs[3][0])
#print(layer_outs[4][0])


# -*- coding: utf-8 -*-
###############################################################################
#    License, author and contributors information in:                         #
#    __manifest__.py file at the root folder of this module.                  #
###############################################################################

from odoo import models, fields, api, _
from odoo.exceptions import UserError, ValidationError
from datetime import datetime

class Holidays(models.Model):
    _inherit = "hr.holidays"

    year = fields.Many2one('hr.year',string='Year',required=True,)
    carry_leave = fields.Boolean('Is Carry leave')
    leave_balance = fields.Float('Remaining Leave',compute='_compute_leave_balance')

    _sql_constraints = [
        ('date_check', "CHECK ( number_of_days_temp IS NOT NULL )", "The number of days must be not null."),
    ]

    @api.depends('date_from', 'date_to','holiday_status_id','employee_id')
    def _compute_leave_balance(self):
        """ Helper to compute the remaining leaves for the current employees
        :returns dict where the key is the employee id, and the value is the remain leaves
        """
        if self.employee_id and self.holiday_status_id and self.year:
            sql_query = """
                SELECT
                    sum(h.number_of_days) AS days
                FROM
                    hr_holidays h
                    join hr_holidays_status s ON (s.id= %s)
                WHERE
                    h.state in ('deputy','validate','validate1','confirm') AND
                    s.id=h.holiday_status_id AND
                    h.employee_id = %s AND
                    h.year = %s
                GROUP BY h.employee_id"""
            params = (self.holiday_status_id.id,self.employee_id.id,self.year.id,)
            self.env.cr.execute(sql_query, params)
            results = self.env.cr.fetchone()
            self.leave_balance = results[0] if results else 0.00
        else:
            self.leave_balance = 0.00

    @api.constrains('leave_balance','number_of_days_temp')
    def _check_leave_balance(self):
        """ Blocking if there is no sufficient leave for selected year """
        if self.type == 'remove' and not self.holiday_status_id.limit:
            if self.number_of_days_temp > self.leave_balance:
                raise ValidationError(_('The number of remaining leaves is not sufficient for this leave type.'))

    @api.onchange('date_from', 'date_to')
    def _default_year(self):
        """ Automating year form Duration, It will get the year from Durations when both years are same.
        Onchange validation for year is using if the year is not matching."""
        date_from = self.date_from
        date_to = self.date_to
        year = False
        if date_from and date_to:
            from_year  = datetime.strptime(date_from, '%Y-%m-%d %H:%M:%S').year
            from_to  = datetime.strptime(date_to, '%Y-%m-%d %H:%M:%S').year
            if from_year == from_to:
                now = from_year or from_to
                year = self.env['hr.year'].search([('year','=',now)],limit=1).id
            if from_year != from_to:
                raise ValidationError(_('The Start and End date has to be in the same year. Please apply for a separate leave for the next year'))
        if self.type == 'remove':
            self.year = year

    @api.constrains('date_from', 'date_to')
    def _check_date(self):
        """ Checking Duration Years in same year """
        if self.type == 'remove':
            for holiday in self:
                date_from  = datetime.strptime(holiday.date_from, '%Y-%m-%d %H:%M:%S').year or False
                date_to  = datetime.strptime(holiday.date_to, '%Y-%m-%d %H:%M:%S').year or False
                if date_from != date_to:
                    raise ValidationError(_('The Start and End date has to be in the same year. Please apply for a separate leave for the next year'))
        return super(Holidays,self)._check_date()

class HolidaysType(models.Model):
    _inherit = "hr.holidays.status"

    is_annual_leave = fields.Boolean('Is Annual Leave')
    max_carry_allowed = fields.Float('Max Carry Allowed')

    @api.constrains('is_annual_leave')
    def onchange_is_annual_leave(self):
        annual_leave_ids = []
        for status in self.env['hr.holidays.status'].search([]):
            for ids in status:
                if ids.is_annual_leave:
                    annual_leave_ids.append(ids.is_annual_leave)
        if len(annual_leave_ids) > len(set(annual_leave_ids)):
            raise ValidationError('Annual Leave is already Mapped for another Leave Type!')


from keras.utils import np_utils
import joblib
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import sys

def get_data_label(fea):
	if fea == 'all':
		fea = 'seq+str+dyn'

	data = np.load('data/' + fea + '/test_data_1d.npy', allow_pickle=True).astype(float)
	label = np.load('data/' + fea + '/test_label_1d.npy').astype(float)

	label = np_utils.to_categorical(label)

	return data, label

def predict(data,fea):
	if fea == 'all':
		fea = 'seq+str+dyn'

	rf_model = joblib.load(fea + '/rf.pkl')
	pred_prob = rf_model.predict_proba(data)

	return pred_prob

if __name__ == '__main__':
	fea = 'all'

	data, label = get_data_label(fea)
	pred_prob = predict(data, fea)
	pred_label = np.argmax(pred_prob, axis=1)

	print(pred_label)


#!/usr/bin/env python
from datetime import datetime
from pathlib import Path

import click
from dateutil.relativedelta import relativedelta
import pandas as pd

from utilities import download_stats
from utilities.xdev_repos import XdevRepos

def update_dataframe(df1, df2):
    """Update df1 with the contents of df2

    This operation:

    - returns df1 if df2 is None,
    - adds new columns in df1 (initialized to 0), if columns in
      df2 exist that are not already in df1,
    - overwrites rows in df1 with rows from df2, or adds rows
      in df1 from df2 if the year-month index doesn't exist
    """
    if df2 is None:
        return df1

    for col in df2:
        if col not in df1:
            df1[col] = 0

    for row in df2.itertuples():
        idx = row.Index
        vals = {}
        for k, v in row._asdict().items():
            if k == 'Index':
                continue
            if k[0] == '_':
                k = df2.columns[int(k[1:])-1]
            vals[k] = v
        df1.loc[idx] = vals

def validate_source(ctx, param, value):
    if value == 'all':
        return 'pypi', 'conda'
    else:
        return (value,)

@click.command()
@click.option(
    '-s',
    '--sources',
    type=click.Choice(['pypi', 'conda', 'all'], case_sensitive=False),
    default='all',
    callback=validate_source,
    help='Download statistics data from given source',
)
@click.option(
    '-n',
    '--last',
    type=click.IntRange(1, 12, clamp=True),
    default=3,
    help='Download data for the last n months',
)
@click.option(
    '-a',
    '--all',
    default=False,
    is_flag=True,
    help='Download all data, not just recent data',
)
def main(sources, last, all):
    repos = XdevRepos()

    for source in sources:
        packages = repos.packages(source=source)
        method = getattr(download_stats, f'get_{source}_stats')

        statsfile = 'data' / Path(f'{source}_stats.csv')
        if statsfile.exists():
            print(f'Reading {source} statistics file...')
            df = pd.read_csv(statsfile)
            df['month'] = pd.to_datetime(df.month).dt.to_period('M')
            df = df.set_index('month')
        else:
            print(f'Initializing {source} statistics...')
            df = pd.DataFrame()
            df.index = pd.period_range(
                start='2019-01',
                periods=0,
                freq='M',
                name='month',
            )

        if all:
            print(f'Downloading all {source} statistics...')
            df_ = method(packages=packages)
            update_dataframe(df, df_)

        else:
            print(f'Updating {source} statistics for last {last} months...')
            now = datetime.now()
            for i in range(last-1, -1, -1):
                dt = now - relativedelta(months=i)
                year, month = dt.year, dt.month

                print(f'  Updating month {year:04d}-{month:02d}')
                df_ = method(packages=packages, year=year, month=month)
                update_dataframe(df, df_)

        df.to_csv(statsfile)
        print('Done.')

if __name__ == '__main__':
    main()


import wave

# Class for reading .wav file

class AudioReader:
    def __init__(self, filename: str):

        if not isinstance(filename, str):
            raise TypeError("Filename must be of string type")

        self.filename = filename
        self.f_samp = 0
        self.num_samp = 0
        self.signal = []
        self.read_audio_from_file()

    def read_audio_from_file(self):
        with wave.open(self.filename, 'r') as wav:
            self.f_samp = wav.getframerate()
            self.num_samp = wav.getnframes()
            self.signal = wav.readframes(self.num_samp)


#!/usr/bin/env python3

#Cunren Liang, 22-MAR-2018

#this command can run multiple times for a stack
#example command
#../../code/s1_select_ion.py -dir . -sn 34/38.5 -nr 5

import os
import sys
import glob
import shutil
import zipfile
import argparse
import datetime
import numpy as np
import xml.etree.ElementTree as ET

class sentinelSLC(object):
    """
        A Class representing the SLCs
    """
    def __init__(self, safe_file, orbit_file=None):
        self.safe_file = safe_file
        self.orbit_file = orbit_file

    def get_datetime(self):
        datefmt = "%Y%m%dT%H%M%S"
        safe = os.path.basename(self.safe_file)
        fields = safe.split('_')
        self.platform = fields[0]
        self.start_date_time = datetime.datetime.strptime(fields[5], datefmt)
        self.stop_date_time = datetime.datetime.strptime(fields[6], datefmt)
        self.date = (datetime.datetime.date(self.start_date_time)).isoformat().replace('-','')

    def get_param(self):
        #speed of light (m/s)
        c = 299792458.0

        #1. processing software version
        zf = zipfile.ZipFile(self.safe_file, 'r')
        manifest = [item for item in zf.namelist() if '.SAFE/manifest.safe' in item][0]
        xmlstr = zf.read(manifest)
        root = ET.fromstring(xmlstr)
        elem = root.find('.//metadataObject[@ID="processing"]')

        #setup namespace
        nsp = "{http://www.esa.int/safe/sentinel-1.0}"
        rdict = elem.find('.//xmlData/' + nsp + 'processing/' + nsp + 'facility').attrib
        self.proc_site = rdict['site'] +', '+ rdict['country']

        rdict = elem.find('.//xmlData/' + nsp + 'processing/' + nsp + 'facility/' + nsp + 'software').attrib
        #ver = rdict['name'] + ' ' + rdict['version']
        self.proc_version = rdict['version']

        #2. start ranges
        anna = sorted([item for item in zf.namelist() if '.SAFE/annotation/s1' in item])
        #dual polarization. for the same swath, the slant ranges of two polarizations should be the same.
        if len(anna) == 6:
            anna = anna[1:6:2]

        startingRange = []
        for k in range(3):
            xmlstr = zf.read(anna[k])
            root = ET.fromstring(xmlstr)
            startingRange.append(
                float(root.find('imageAnnotation/imageInformation/slantRangeTime').text)*c/2.0
                )

        self.startingRanges = startingRange

        #3. snwe
        map_overlay = [item for item in zf.namelist() if '.SAFE/preview/map-overlay.kml' in item][0]
        xmlstr = zf.read(map_overlay)
        xmlstr = xmlstr.decode('utf-8')
        start = '<coordinates>'
        end = '</coordinates>'
        pnts = xmlstr[xmlstr.find(start)+len(start):xmlstr.find(end)].split()

        lats=[]
        lons=[]
        for pnt in pnts:
           lons.append(float(pnt.split(',')[0]))
           lats.append(float(pnt.split(',')[1]))
        self.snwe=[min(lats),max(lats),min(lons),max(lons)]

def get_safe_from_group(group):

    safes = []
    ngroup = len(group)
    for i in range(ngroup):
        ngroupi = len(group[i])
        for j in range(ngroupi):
            safes.append(group[i][j].safe_file)

    return safes

def print_group(group):
    '''print group parameters
    '''
    print()
    print('slice                                                                    no   ver         IW1 (m)           IW2 (m)           IW3 (m)')
    print('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')

    for i in range(len(group)):
        for j in range(len(group[i])):
            print_stuff = '%s %3d  %s  '%(os.path.basename(group[i][j].safe_file), i+1, group[i][j].proc_version)
            print_stuff += "{} {} {}".format(group[i][j].startingRanges[0], group[i][j].startingRanges[1], group[i][j].startingRanges[2])
            print(print_stuff)
        print()

def get_group(dir0):
    '''
    this routine group the slices, each group is an acquisition
    the returned result is a list (acquistions sorted by starting time) containing a number of lists (slices sorted by starting time)
    '''
    #sort by starting time
    zips = sorted(glob.glob(os.path.join(dir0, 'S1*_IW_SLC_*.zip')), key=lambda x: x.split('_')[-5], reverse=False)
    nzips = len(zips)

    group = []
    for i in range(nzips):
        safeObj=sentinelSLC(zips[i])
        safeObj.get_datetime()
        safeObj.get_param()

        datefmt = "%Y%m%dT%H%M%S"
        fields = zips[i].split('_')
        tbef = datetime.datetime.strptime(fields[-5], datefmt)
        taft = datetime.datetime.strptime(fields[-4], datefmt)

        if i == 0:
            #create new group
            tbef0 = tbef
            group0 = []

        #S-1A is capable of operating up to 25 min per orbit [21]
        #Yague-Martinez et al., "Interferometric Processing of Sentinel-1 TOPS Data,"
        #S1A/B revisit time is 6 days, here we use 1 day to check if from the same orbit
        if np.absolute((tbef - tbef0).total_seconds()) < 24 * 3600:
            group0.append(safeObj)
        else:
            group.append(group0)
            #create new group
            tbef0 = tbef
            group0 = []
            group0.append(safeObj)

        if i == nzips - 1:
            group.append(group0)

    return group

def check_redundancy(group, threshold=1):
    '''
    threshold: time difference threshold in seconds between two slices in second.
    this routine checks, for a slice, if there are multiple ones.
    '''

    print('\nchecking different copies of same slice')

    safe_removed_indices = []
    ngroup = len(group)
    for i in range(ngroup):
        ngroupi = len(group[i])
        if ngroupi == 1:
            continue
        else:
            for j in range(ngroupi-1):
                for k in range(j+1, ngroupi):
                    if np.absolute((group[i][j].start_date_time - group[i][k].start_date_time).total_seconds()) < threshold and \
                       np.absolute((group[i][j].stop_date_time - group[i][k].stop_date_time).total_seconds()) < threshold:

                        #determine which one to be removed
                        j_version = False
                        k_version = False
                        for l in range(ngroupi):
                            if l != j and l != k:
                                if group[i][j].proc_version == group[i][l].proc_version:
                                    j_version = True
                                if group[i][k].proc_version == group[i][l].proc_version:
                                    k_version = True
                        if j_version == k_version:
                            safe_removed_index = [i, j]
                        else:
                            if j_version == False and k_version == True:
                                safe_removed_index = [i, j]
                            else:
                                safe_removed_index = [i, k]
                        safe_removed_indices.append(safe_removed_index)

                        print('in acquistion {}, the following two slices are the same:'.format(i+1))
                        if safe_removed_index == [i, j]:
                            print(os.path.basename(group[i][j].safe_file) + ' (not to be used)')
                            print(os.path.basename(group[i][k].safe_file))
                        else:
                            print(os.path.basename(group[i][j].safe_file))
                            print(os.path.basename(group[i][k].safe_file) + ' (not to be used)')

    #remove redundant slices
    if safe_removed_indices != []:
        group_new = []
        for i in range(ngroup):
            ngroupi = len(group[i])
            group_new.append([group[i][j] for j in range(ngroupi) if [i,j] not in safe_removed_indices])

        print('slices removed:')
        for index in safe_removed_indices:
            print('%s %3d'%(os.path.basename(group[index[0]][index[1]].safe_file), index[0]+1))

    else:
        group_new = group
        print('no slices removed')

    return group_new

def check_version(group):
    print('\nchecking different slice versions of an acquisition')

    acquistion_removed_indices = []
    ngroup = len(group)
    for i in range(ngroup):
        ngroupi = len(group[i])
        for j in range(ngroupi):
            if group[i][0].proc_version != group[i][j].proc_version:
                print('different slice versions found in acquistion {}'.format(i+1))
                acquistion_removed_indices.append(i)
                break

    #remove acquistions
    if acquistion_removed_indices != []:
        group_new = [group[i] for i in range(ngroup) if i not in acquistion_removed_indices]
        print('acquistions removed:')
        for i in acquistion_removed_indices:
            for j in range(len(group[i])):
                print('%s %3d'%(os.path.basename(group[i][j].safe_file), i+1))
    else:
        group_new = group
        print('no acquistions removed')

    return group_new

def check_gap(group):
    print('\nchecking gaps in an acquistion')

    acquistion_removed_indices = []
    ngroup = len(group)
    for i in range(ngroup):
        ngroupi = len(group[i])
        if ngroupi == 1:
            continue
        else:
            for j in range(0, ngroupi-1):
                if (group[i][j+1].start_date_time - group[i][j].stop_date_time).total_seconds() > 0:
                    acquistion_removed_indices.append(i)
                    break

    #remove acquistions
    if acquistion_removed_indices != []:
        group_new = [group[i] for i in range(ngroup) if i not in acquistion_removed_indices]
        print('acquistions removed:')
        for i in acquistion_removed_indices:
            for j in range(len(group[i])):
                print('%s %3d'%(os.path.basename(group[i][j].safe_file), i+1))
    else:
        group_new = group
        print('no acquistions removed')

    return group_new

def acquistion_snwe(groupi):
    '''return snwe of an acquisition consisting a number of slices'''
    s = min([x.snwe[0] for x in groupi])
    n = max([x.snwe[1] for x in groupi])
    w = min([x.snwe[2] for x in groupi])
    e = max([x.snwe[3] for x in groupi])

    return [s, n, w, e]

def overlap(group):
    '''return snwe of the overlap of all acquistions'''

    s = max([(acquistion_snwe(x))[0] for x in group])
    n = min([(acquistion_snwe(x))[1] for x in group])
    w = max([(acquistion_snwe(x))[2] for x in group])
    e = min([(acquistion_snwe(x))[3] for x in group])

    if s >= n or w >= e:
       #raise Exception('no overlap among the acquistions')
       print('WARNING: there is no overlap among the acquistions, snwe: {}'.format([s, n, w, e]))

    return [s, n, w, e]

def check_aoi(group, s, n):
    '''
    check each group to see if it fully covers [s, n], if not remove the acquistion
    s: south bound
    n: north bound
    '''

    print('\nchecking if each acquistion fully covers user specifed south/north bound [{}, {}]'.format(s, n))

    acquistion_removed_indices = []
    ngroup = len(group)
    for i in range(ngroup):
        snwe = acquistion_snwe(group[i])
        if not (snwe[0] <= s and snwe[1] >= n):
            acquistion_removed_indices.append(i)

    #remove acquistions
    if acquistion_removed_indices != []:
        group_new = [group[i] for i in range(ngroup) if i not in acquistion_removed_indices]
        print('acquistions removed:')
        for i in acquistion_removed_indices:
            for j in range(len(group[i])):
                print('%s %3d'%(os.path.basename(group[i][j].safe_file), i+1))
    else:
        group_new = group
        print('no acquistions removed')

    return group_new

def check_different_starting_ranges(group):
    '''
    checking if there are different starting ranges in each acquistion.
    '''

    print('\nchecking if there are different starting ranges in each acquistion')

    acquistion_removed_indices = []
    ngroup = len(group)
    for i in range(ngroup):
        ngroupi = len(group[i])
        for j in range(1, ngroupi):
            if group[i][0].startingRanges != group[i][j].startingRanges:
                acquistion_removed_indices.append(i)
                #print('++++++++++++++{} {}'.format(group[i][0].safe_file, group[i][j].safe_file))
                break

    #remove acquistions
    if acquistion_removed_indices != []:
        group_new = [group[i] for i in range(ngroup) if i not in acquistion_removed_indices]
        print('acquistions removed:')
        for i in acquistion_removed_indices:
            for j in range(len(group[i])):
                print('%s %3d'%(os.path.basename(group[i][j].safe_file), i+1))
    else:
        group_new = group
        print('no acquistions removed')

    return group_new

def check_small_number_of_acquisitions_with_same_starting_ranges(group, threshold=1):
    '''
    for the same subswath starting ranges,
    if the number of acquistions < threshold, remove these acquistions
    '''

    print('\nchecking small-number of acquistions with same starting ranges')

    ngroup = len(group)

    starting_ranges = [x[0].startingRanges for x in group]

    #get unique starting_ranges
    starting_ranges_unique = []
    for i in range(ngroup):
        if starting_ranges[i] not in starting_ranges_unique:
            starting_ranges_unique.append(starting_ranges[i])

    #get number of acquistions for each unique starting ranges
    ngroup_unique = len(starting_ranges_unique)
    starting_ranges_unique_number = [0 for i in range(ngroup_unique)]
    for k in range(ngroup_unique):
        for i in range(ngroup):
            if starting_ranges_unique[k] == starting_ranges[i]:
                starting_ranges_unique_number[k] += 1

    #get starting ranges to be removed (number of acquistions < threshold)
    starting_ranges_removed = []
    for k in range(ngroup_unique):
        if starting_ranges_unique_number[k] < threshold:
            starting_ranges_removed.append(starting_ranges_unique[k])

    #remove acquistions
    if starting_ranges_removed != []:
        group_new = [group[i] for i in range(ngroup) if group[i][0].startingRanges not in starting_ranges_removed]

        print('acquistions removed:')
        for i in range(ngroup):
            if group[i][0].startingRanges in starting_ranges_removed:
                for j in range(len(group[i])):
                    print('%s %3d'%(os.path.basename(group[i][j].safe_file), i+1))

    else:
        group_new = group
        print('no acquistions removed')

    return group_new

def cmdLineParse():
    '''
    Command line parser.
    '''

    parser = argparse.ArgumentParser( description='select Sentinel-1A/B acquistions good for ionosphere correction. not used slices are moved to folder: not_used')
    parser.add_argument('-dir', dest='dir', type=str, required=True,
            help = 'directory containing the "S1*_IW_SLC_*.zip" files')
    parser.add_argument('-sn', dest='sn', type=str, required=True,
            help='south/north bound of area of interest, format: south/north')
    parser.add_argument('-nr', dest='nr', type=int, default=10,
            help = 'minimum number of acquisitions for same starting ranges. default: 10')

    if len(sys.argv) <= 1:
        print('')
        parser.print_help()
        sys.exit(1)
    else:
        return parser.parse_args()

if __name__ == '__main__':

    inps = cmdLineParse()
    s,n=[float(x) for x in inps.sn.split('/')]

    #group the slices
    group = get_group(inps.dir)
    safes_all = get_safe_from_group(group)

    #print overlap of group
    #print('overlap among acquisitions: {}'.format(overlap(group)))

    #print group before removing slices/acquistions
    print_group(group)

    #do checks and remove the slices/acquisitions
    group = check_redundancy(group, threshold=1)
    group = check_version(group)
    group = check_gap(group)
    group = check_aoi(group, s, n)
    group = check_different_starting_ranges(group)
    group = check_small_number_of_acquisitions_with_same_starting_ranges(group, threshold=inps.nr)

    #print group after removing slices/acquistions
    print_group(group)

    #move slices that are not used to 'not_used'
    safes_used = get_safe_from_group(group)
    not_used_dir = os.path.join(inps.dir, 'not_used')
    os.makedirs(not_used_dir, exist_ok=True)
    for safe in safes_all:
        if safe not in safes_used:
            shutil.move(safe, not_used_dir)


__author__ = "Erik Seetao, Hayk Hovhannisyan"

import numpy as np
import matplotlib.pyplot as plt
import cv2
#import tensorflow as tf
import os

import pickle

from skimage.measure import find_contours
#from sklearn.model_selection import train_test_split

class HandsDataLoader:
    def __init__(self,path,newdirectory):
        self.path = path
        self.width = 120 #height is 240, so plus minus width on both sides gives 240 as square image
        self.newdirectory = newdirectory
        self.label2intb = {'palm'       : 0,
                            'l'          : 1,
                            'fist'       : 2,
                            'fist_moved' : 3,
                            'thumb'      : 4,
                            'index'      : 5,
                            'ok'         : 6,
                            'palm_moved' : 7,
                            'c'          : 8,
                            'down'       : 9
                            }

    def crop_img(self, mid_point, image):
        '''
        Checks if image needs padding, then crops image to 240x240
        '''

        #padding image
        if(mid_point + self.width > 640):
            print('right padding...')
            extra_r = mid_point + self.width - 640
            pad_r   = np.zeros((240, extra_r))
            new_im = np.concatenate((image[:,mid_point-self.width:],pad_r),axis  =1)

        elif(mid_point - self.width < 0):
            print('left padding...')
            extra_l = self.width - mid_point

            pad_l   = np.zeros((240,extra_l))
            new_im  = np.concatenate((pad_l,image[:,:mid_point+self.width]),axis  =1)
        #image does not need to be padded
        else:
            new_im = image[:,mid_point-self.width:mid_point+self.width]

        return new_im

    def rotate_image(self, image, angle):
        '''
        Rotates the image counterclockwise by angle in degrees
        '''
        image_center = tuple(np.array(image.shape[1::-1]) / 2)
        rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
        result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)

        return result

    def process_img(self,aug=False):
        '''
        Data preprocessing on images and saves them to appropriate dataset folders
        '''

        print("list dir is", os.listdir(self.path+'/leapGestRecog')) #prints out user list

        output_im = []
        output_lb = []

        for person in os.listdir(self.path+'/leapGestRecog'):
            user_path = self.path+'/leapGestRecog/' + person + '/'
            print("userpath is:",user_path)
            print("person is:",person)

            for gesture in os.listdir(user_path):
                gesture_path = user_path + gesture
                print("gesture path is:",gesture_path)

                img_counter = 1
                class_counter = 1

                #checkpoint, used for processing actual pngs
                checkpoint_path = self.path + '/' + self.newdirectory + '/' + person + '/' + gesture
                if len(os.listdir(checkpoint_path)) == 200: #if processed 200 images
                    print("already finished processing gesture ",gesture," for user ",person)
                    continue
                else:
                    for img in os.listdir(gesture_path):

                        print("processing... ", gesture_path+'/'+img) #is absolute path of image

                        r = plt.imread(gesture_path + '/' + img)

                        contours = find_contours(r, 0.35) #find hand

                        # #plotting contours
                        # fig, ax = plt.subplots()
                        # ax.imshow(r, interpolation='nearest', cmap=plt.cm.gray)

                        # for n, contour in enumerate(contours):
                        #     ax.plot(contour[:, 1], contour[:, 0], linewidth=2)
                        # ax.axis('image')
                        # ax.set_xticks([])
                        # ax.set_yticks([])
                        # #plt.show()
                        # ################

                        #find midpoint of hand
                        max_val = contours[0][:,1].max()
                        min_val = contours[0][:,1].min()
                        mid_point = int((max_val+min_val)/2)

                        # #to find vertical, only needed for crosshairs plot
                        # vmax_val = contours[0][:,0].max()
                        # vmin_val = contours[0][:,0].min()
                        # vmid_point = int((vmax_val+vmin_val)/2)

                        # # prints marker in center of hand
                        # #plt.figure()
                        # plt.imshow(r,cmap=plt.cm.gray)
                        # plt.plot(mid_point,vmid_point, marker = "+", color = 'r')
                        # plt.show()

                        cropped_img = self.crop_img(mid_point,r)
                        downsampled_img = cropped_img[::2,::2]
                        # print("downsampled img shape is", downsampled_img.shape)
                        # plt.figure()
                        # plt.imshow(downsampled_img)
                        # plt.show()
                        mirrored_img = np.flip(downsampled_img,axis=1)

                        output_im.append(mirrored_img)
                        output_lb.append(self.label2intb[gesture[3:]])

                        # class_index = str(class_counter).zfill(2)
                        # new_dir_save = self.path + '/' + self.newdirectory + '/' + person + '/' + gesture + '/'

                        # if aug == False:
                        #     img_index = str(img_counter).zfill(4)
                        #     plt.imshow(downsampled_img, cmap=plt.cm.gray)
                        #     plt.savefig(new_dir_save + "frame_" + person + "_" + class_index + "_" + img_index + ".png")
                        # if aug == True:
                        #     img_index = str(img_counter+200).zfill(4)
                        #     plt.imshow(mirrored_img, cmap=plt.cm.gray)
                        #     plt.savefig(new_dir_save + "frame_" + person + "_" + class_index + "_" + img_index + ".png")
                        # plt.close()

                        img_counter += 1
                class_counter += 1
        print("pickling files...")

        if not os.path.exists(self.newdirectory):
            print("Output file does not exist. Creating new file...")
            os.makedirs(self.newdirectory)

        M = 5000
        for i in range(0,4): #from 20k/5k
            begin = M*i
            end   = M*(i+1)
            print("begin and end:",begin,end)
            temp = output_im[begin:end]
            out = self.path + '/' + self.newdirectory + '/' + "output_im_" + str(i) +".p"
            #out  = base_path_out + "output_im_" + str(i) +".p"
            pickle.dump(temp,open( out, "wb" ) )

    def prep_folder(self):
        '''
        Check if new directory exists to save images in, otherwise builds the dataset files
        '''
        if not os.path.exists(self.newdirectory):
            os.makedirs(self.newdirectory)

            users = ['00','01','02','03','04','05','06','07','08','09']
            gestures = ['01_palm','02_l','03_fist','04_fist_moved','05_thumb','06_index','07_ok','08_palm_moved','09_c','10_down']
            #print(self.path + '/' + self.newdirectory + '/')
            for user in users:
                os.makedirs(os.path.join(self.path + '/' + self.newdirectory + '/', user))
                for gesture in gestures:
                    os.makedirs(os.path.join(self.path + '/' + self.newdirectory + '/' + user + '/', gesture))

if __name__ == '__main__':

    path = os.path.dirname(os.path.abspath(__file__)) #/Users/eseetao/Documents/Code
    #print(path)

    data = HandsDataLoader(path,"leapGestMirror")
    #data.prep_folder()

    #data.process_img(aug=False)