#!/usr/bin/python
# IZ_1993.py
from __future__ import division
'''
    Copyright (C) 2014 Samuel J. Lasley, Russell A. Green, and
						Adrian Rodriguez-Marek

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without restriction,
    including without limitation the rights to use, copy, modify, merge,
    publish, distribute, sublicense, and/or sell copies of the Software,
    and to permit persons to whom the Software is furnished to do so,
    subject to the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
    BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
    ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
'''
#Edited at 9/14/2016
#import pdb
import numpy as np
import matplotlib.pyplot as plt
#straindef=np.logspace(-6,-0.004364805,num=10)

straindef = np.array([0.000001, 0.000003, 0.00001, 0.00003, 0.0001, 0.0003,
                      0.001, 0.003, 0.01, 0.99, 5])

def IZ_1993(gam=straindef, PI=0., sigm=100):
    ''' Returns the Ishibashi and Zhang (1993) values for G/Gmax
    and damping (decimal) for a given values of strain (as a decimal),
    Plasticity index, and initial mean effective confining stress (kPa).
    '''

    PI = float(PI)
    sigm = float(sigm)

    if PI == 0.:
        n = 0.0
    elif PI <= 15:
        n = 3.37e-6 * PI ** 1.404
    elif PI <= 70:
        n = 7e-7 * PI ** 1.976
    else:
        n = 2.7e-5 * PI ** 1.115

    K = 0.5 * (1 + np.tanh(np.log(((0.000102 + n) / gam) ** 0.492)))
    m = (0.272 * (1 - np.tanh(np.log((0.000556 / gam) ** 0.4))) *
                np.exp(-0.0145 * PI ** 1.3))
    Gratio = K * sigm ** m
    np.putmask(Gratio, Gratio>1, 1)
    #use np.copyto when updates to numpy 1.7 or use Gratio.clip(max=1)
    D = 0.1665 * (1 + np.exp(-0.0145 * PI ** 1.3)) * (0.586 * Gratio ** 2. -
                1.547 * Gratio + 1.)
    Dmin=0.1665 * (1 + np.exp(-0.0145 * PI ** 1.3)) * (0.586 * 1 ** 2. -
                1.547 * 1 + 1.)
    return (gam, Gratio, D, Dmin)

def DS_2001(gam=straindef, PI=0., sigm=100., OCR=1., soil=0, N=10., frq=1.,MRD_rand=0,epsilon=-10,Su=-1.0,Gmax=375000):
    '''
    Darendeli and Stokoe (2001) shear modulus reduction and damping curves.
    gam= shear strain (decimal)
    PI= Plasticity Index
    sigm = the initial mean effective confining stress (kPa)
    OCR = the overconsolidation ratio
    soil = the soil type indicator:
        0 for general
        1 for clean sand
        2 for sands with high fines contents
        3 for silts
        4 for clays
    #Mahdi's change
    MRD_rand = Randomizing MRD curves
        0 Use median MRD curve
        1 Generate a randmized MRD curve
    epsilon = The random number used in generating MRD realizations
        -10 The code produces a random number
        Input a two dimentional vector to specify it manually
    Su = shear strength of soil( To not correct NG for shear strenght input -1)
    Gmax = Maximum soil modulus
    #
    N = the number of cycles
    frq = the frequency of loading
    Dmintype = 1 for 'Default', 0 for 'Green'  (Suggest using Green's for now!!!!!)
                !!!Not Implemented !!!
    The output of the function is:
    gam = the same input shear strain as a decimal
    Gratio = G/Gmax = the shear modulus normalized by the small strain
        shear modulus.
    D = damping (decimal)
    '''
    PI = float(PI)
    sigm = float(sigm)
    OCR = float(OCR)
    N = float(N)
    frq = float(frq)
    soil = int(soil)
    MRD_rand = int(MRD_rand)
    Su=float(Su)
    Gmax=float(Gmax)
    phi1 = np.array([ 3.52E-02, 4.74E-02, 3.34E-02, 4.16E-02, 2.58E-02])
    phi2 = np.array([ 1.01E-03,-2.34E-03,-5.79E-05, 6.89E-04, 1.95E-03])
    phi3 = np.array([ 3.246E-01, 2.50E-01, 2.49E-01, 3.21E-01, 9.92E-02])
    phi4 = np.array([ 3.483E-01, 2.34E-01, 4.82E-01, 2.80E-01, 2.26E-01])
    phi5 = np.array([ 9.19E-01, 8.95E-01, 8.45E-01, 1.00E+00, 9.75E-01])
    phi6 = np.array([ 8.005E-01, 6.88E-01, 8.89E-01, 7.12E-01, 9.58E-01])
    phi7 = np.array([ 1.29E-02, 1.22E-02, 2.02E-02, 3.03E-03, 5.65E-03])
    phi8 = np.array([-1.069E-01, -1.00E-01,-1.00E-01,-1.00E-01,-1.00E-01])
    phi9 = np.array([-2.889E-01, -1.27E-01,-3.72E-01,-1.89E-01,-1.96E-01])
    phi10 = np.array([ 2.919E-01, 2.88E-01, 2.33E-01, 2.34E-01, 3.68E-01])
    phi11 = np.array([ 6.329E-01, 7.67E-01, 7.76E-01, 5.92E-01, 4.66E-01])
    phi12 = np.array([-5.66E-03, -2.83E-02,-2.94E-02,-7.67E-04, 2.23E-02])
    phi13 = -4.23
    phi14 = 3.62
    phi15 = -5.00
    phi16 = -2.50E-01

    # Calculate the reference shear strain:
    gamr = ((phi1[soil] + phi2[soil] * PI * OCR ** phi3[soil]) * (sigm / 101.325) ** phi4[soil])
    gam = gam * 100  # convert to percent

    # Calculate assorted variables:
    a = phi5[soil]
    b = phi11[soil] + phi12[soil] * np.log(N)
    C1 = -1.1143 * a ** 2 + 1.8618 * a + 0.2523
    C2 = 0.0805 * a ** 2 - 0.0710 * a - 0.0095
    C3 = -0.0005 * a ** 2 + 0.0002 * a + 0.0003
    # Calculate G/Gmax
    Gratio = 1. / (1. + (gam / gamr) ** a)

    Dmasinga1 = 100 / np.pi * (4 * (gam - gamr * np.log((gam + gamr)
                / gamr)) / (gam ** 2 / (gam + gamr)) - 2 )

    Dmasing = C1 * Dmasinga1 + C2 * Dmasinga1 ** 2 + C3 * Dmasinga1 ** 3

    Dmin = ((phi6[soil] + phi7[soil] * PI * OCR ** phi8[soil]) * (sigm / 101.3) ** phi9[soil] * (1 + phi10[soil] * np.log(frq)))

    D = b * (Gratio ** 0.1) * Dmasing + Dmin

    if MRD_rand == 1:
        ro=-0.5 # <--- This value is based on a sentence in darandali thesis
        sNG = np.exp(phi13)+np.sqrt(0.25/np.exp(phi14)-((Gratio-0.5)**2)/np.exp(phi14))
        sD = np.exp(phi15)+np.exp(phi16)*np.sqrt(D)
        sDmin=np.exp(phi15)+np.exp(phi16)*np.sqrt(Dmin)
        # Add varibility due to PI
        dDmas_dgr = 8.74/(gamr/100);
        PI_v=15 #<----
        Dmin_v= (phi7[soil]*OCR**phi7[soil]*(sigm/101)**(phi9[soil])*(1+phi10[soil]*np.log(frq)))*PI_v;
        gr_v=(phi2[soil]*OCR**phi3[soil])*(sigm/101)**phi4[soil]/100*PI_v;
        NG_gr=a/(4*(gamr/100))*gr_v;# Derivative of NG with respect to gr
        D2_v = abs(b*0.1*1.866*(-a/(4*(gamr/100)))*13.57+b*(1/2)**0.1*dDmas_dgr)*gr_v
        sD_Dmin =np.exp(phi15)+np.exp(phi16)*np.sqrt(Dmin);
        s2D2_gr = np.exp(phi16)**2*(b*(0.5)**0.1*13.57)+2*np.exp(phi15)*np.exp(phi16)*(np.sqrt(b*(0.5)**0.1*13.57+Dmin)-np.sqrt(Dmin))
        s2D = sD_Dmin**2+Dmin_v**2+((s2D2_gr+D2_v)/s2D2_gr)*(np.exp(phi16)**2*(D-Dmin)+2*np.exp(phi15)*np.exp(phi16)*(np.sqrt(D)-np.sqrt(Dmin)))
        sD=np.sqrt(s2D)
        sNG = np.exp(phi13)+np.sqrt(4*NG_gr**2+1/np.exp(phi14))*np.sqrt(0.25-((Gratio-0.5)**2))
        # Make the variability of G/Gmax before threshold strain zero
        gtf_r=np.array([0.000331314,0.000479492,0.000700539,0.00095242,0.001268053,0.001704959,0.00220124,0.002564315,0.00389172,0.00389172])
        PI_r=np.array([0,4.637925427,11.70840849,18.02339908,25.00172487,33.18696591,39.62171166,44.97718245,60,200])
        gtf=np.exp(np.interp(PI,PI_r,np.log(gtf_r)))
        ga=gtf*30
        z=np.ones(len(gam))
        for i in range(len(gam)):
            if gam[i]>=ga:
                z[i]=1
            elif gam[i]<gtf:
                z[i]=0
            else:
                z[i] = ((np.log(gam[i])-np.log(gtf))/(np.log(ga)-np.log(gtf)))

        sNG=z*sNG
        # Bring the variability and sigma from lognormal to normal distribution
        D_log = np.log(D/np.sqrt(1+sD**2/D**2))
        Dmin_log = np.log(Dmin/np.sqrt(1+sDmin**2/Dmin**2))
        Gratio_log = np.log(Gratio/np.sqrt(1+sNG**2/Gratio**2))
        sD_log = np.sqrt(np.log(1+sD**2/D**2))
        sDmin_log = np.sqrt(np.log(1+sDmin**2/Dmin**2))
        sNG_log = np.sqrt(np.log(1+sNG**2/Gratio**2))

        if epsilon==-10: #If no epsilon is assigned, make it. If it is assigned, just read them
            epsilon1=-5
            epsilon2=-5
            while abs(epsilon1)>=2 or abs(epsilon1)>=2:
                epsilon1 = np.random.normal(loc=0.0, scale=1.0)
                epsilon2 = np.random.normal(loc=0.0, scale=1.0)
                epsilon2=((1-ro**2)**0.5)*epsilon2+ro*epsilon1
        else:
            epsilon1=epsilon[0]
            epsilon2=epsilon[1]
        Gratio_n_log=Gratio_log+epsilon1*sNG_log
        D_n=np.exp(D_log+epsilon2*sD_log)
        Dmin_n=np.exp(Dmin_log+epsilon2*sDmin_log)

        D=D_n
        Dmin=Dmin_n
        D=np.minimum(D,15)
        Gratio = np.exp(Gratio_n_log)

        # If you have Su value correct the curve for it
        if Su!=-1.0:
            sSu = 0.5 #<----

            # Yee et al. (2013)
            g1_vector = [0.3, 0.1, 0.07, 0.05, 0.03, 0.01, 0.005,0.001]

            for g1 in g1_vector:

                condition = gam > g1

                A = np.extract(condition , gam)

                g_1 = A[0]

                A = np.extract(condition , Gratio)

                Gratio_1 = A[0]

                condition = gam<g1

                A = np.extract(condition , gam)

                g_2 = A[-1]

                A = np.extract(condition ,Gratio )

                Gratio_2 = A[-1]

                NG1=(Gratio_2*g_2 - Gratio_1*g_1) / (g_2 - g_1)

                tauf_n=np.exp(np.log(Su) + epsilon1 *sSu )

                gref=(tauf_n - (g_1/100)*Gratio_1*Gmax)/(NG1*Gmax)*100

                if gref>0 and NG1>0:
                    break

            count=0

            for g in gam:

                if g>g1:

                    Gratio[count] = (g_1*Gratio_1+np.sign(gref)*NG1*(g-g_1)/(1+(g-g_1)/np.absolute(gref)))/g

                count+=1

    elif Su != -1.0:
        # Yee et al. (2013)
        g1_vector = [0.3, 0.1, 0.07, 0.05, 0.03, 0.01, 0.005]

        for g1 in g1_vector:

            condition = gam > g1

            A = np.extract(condition , gam)

            g_1 = A[0]

            A = np.extract(condition , Gratio)

            Gratio_1 = A[0]

            condition = gam<g1

            A = np.extract(condition , gam)

            g_2 = A[-1]

            A = np.extract(condition ,Gratio )

            Gratio_2 = A[-1]

            NG1=(Gratio_2*g_2 - Gratio_1*g_1) / (g_2 - g_1)

            tauf_n=Su

            gref=(tauf_n - (g_1/100)*Gratio_1*Gmax)/(NG1*Gmax)*100

            if gref>0 and NG1>0:
                break

        count=0

        for g in gam:

            if g>g1:

                Gratio[count] = (g_1*Gratio_1+np.sign(gref)*NG1*(g-g_1)/(1+(g-g_1)/np.absolute(gref)))/g

            count+=1

    return gam / 100, Gratio, D/100 , Dmin/100

def Menq(gam = straindef, Cu = 7 , D50=1 , sigm=100.,N = 10., frq = 1.,MRD_rand=0,epsilon=-10,Su=-1,Gmax=375000):
    '''
    Usage: [NG,D ,sNG, sD] = Menq(g,so
    g = strain vector (enter a negative number to use default)
    unitless (e.g., not in percent)
    Cu   Coeff. of Uniformity
    D50 mean grain size diam (in mm)
    so  mean effective stress(in kPa)
    #Mahdi's change
    MRD_rand = Randomizing MRD curves
        0 Use median MRD curve
        1 Generate a randmized MRD curve
    epsilon = The random number used in generating MRD realizations
        -10 The code produces a random number
        Input a two dimentional vector to specify it manually
    Su = shear strength of soil( To not correct NG for shear strenght input -1)
    Gmax = Maximum soil modulus
    #
    The last two argument are optional (default is used otherwise)
    Ncyc: number of cycles of loading (default is 10)
    f: frequency (Hz) (default is 1 Hz)
    Output
    NG: normalized modulus reduction
    D: Damping curve
    sNG: Standard deviation of normalized curve
    sD: Standard deviation of damping curve
    '''

    gam=gam*100;
    pa=101.325;
    gamr = 0.12 * Cu ** (-0.6) * (sigm/pa) ** (0.5*Cu ** (-0.15));
    a = 0.86 + 0.1*np.log10(sigm/pa);
    Gratio=1/(1+(gam/gamr)**a);

    b=0.6329-0.0057*np.log(N);

    c1=-1.1143*a**2 + 1.8618*a + 0.2523;
    c2=0.0805* a**2 - 0.0710*a - 0.0095;
    c3=-0.0005*a**2 + 0.0002*a + 0.0003;
    Dmas_a1=(100/3.14*(4*((gam-gamr*np.log((gam+gamr)/gamr))/(gam**2/(gam+gamr)))-2));
    Dmas = c1*Dmas_a1 + c2*Dmas_a1**2 + c3*Dmas_a1**3;
    Dmin = 0.55*Cu**0.1*D50**(-0.3)*(sigm/pa)**(-0.05);

    #Dec 18,2015: change the coefficient of so/pa to -0.08 from -.05. this is
    #a discrepancy in Menq's dissertation
    #Dec 21, 2015: email from Menq
    #Dear Adrian,
    #Happy Holidays!
    # The value of -0.08 was the best fit for my data.  However, I was concerned that I might be over fitting the data. So, I rounded up the number to -0.05, and it worked just fine. The value of -0,05 was what I recommended.  I apologized for the writing.  It was painful for me to write in English.
    # Please note that most of the test specimens are air dried.   With added moisture, Dmin is expected to be higher.
    # Best regards,
    #
    #
    # Menq,

    F = b* Gratio**0.1
    D = F*Dmas + Dmin
    phi13 = -4.23
    phi14 = 3.62
    phi15 = -5.00
    phi16 = -2.50E-01
    if MRD_rand == 1:
        ro=-0.5
        sNG = np.exp(phi13)+np.sqrt(0.25/np.exp(phi14)-((Gratio-0.5)**2)/np.exp(phi14))
        sD = np.exp(phi15)+np.exp(phi16)*np.sqrt(D)
        sDmin=np.exp(phi15)+np.exp(phi16)*np.sqrt(Dmin)
        PI=0
        gtf_r=np.array([0.000331314,0.000479492,0.000700539,0.00095242,0.001268053,0.001704959,0.00220124,0.002564315,0.00389172,0.00389172])
        PI_r=np.array([0,4.637925427,11.70840849,18.02339908,25.00172487,33.18696591,39.62171166,44.97718245,60,200])
        gtf=np.exp(np.interp(PI,PI_r,np.log(gtf_r)))
        ga=gtf*30
        z=np.ones(len(gam))
        for i in range(len(gam)):
            if gam[i]>=ga:
                z[i]=1
            elif gam[i]<gtf:
                z[i]=0
            else:
                z[i] = ((np.log(gam[i])-np.log(gtf))/(np.log(ga)-np.log(gtf)))
        sNG=z*sNG

        D_log = np.log(D/np.sqrt(1+sD**2/D**2))
        Dmin_log = np.log(Dmin/np.sqrt(1+sDmin**2/Dmin**2))
        Gratio_log = np.log(Gratio/np.sqrt(1+sNG**2/Gratio**2))
        sD_log = np.sqrt(np.log(1+sD**2/D**2))
        sDmin_log = np.sqrt(np.log(1+sDmin**2/Dmin**2))
        sNG_log = np.sqrt(np.log(1+sNG**2/Gratio**2))
        if epsilon==-10:
            epsilon1=-5
            epsilon2=-5
            while abs(epsilon1)>=2 or abs(epsilon1)>=2:
                epsilon1 = np.random.normal(loc=0.0, scale=1.0)
                epsilon2 = np.random.normal(loc=0.0, scale=1.0)
                epsilon2=((1-ro**2)**0.5)*epsilon2+ro*epsilon1
        else:
            epsilon1=epsilon[0]
            epsilon2=epsilon[1]
        Gratio_n=np.exp(Gratio_log+epsilon1*sNG_log)
        D_n=np.exp(D_log+epsilon2*sD_log)
        Dmin_n=np.exp(Dmin_log+epsilon2*sDmin_log)

        D=D_n
        Gratio=Gratio_n
        Dmin=Dmin_n
        D=np.minimum(D,15)

    return gam / 100, Gratio, D/100 , Dmin/100

def PietPeat(gam= straindef, sigm = 100 ,N = 10., frq = 1.,MRD_rand=0,epsilon=-10,Su=-1,Gmax=375000):
    '''
       Piet fit to Peat data
       Usage: [NG,D ,sNG, sD] = Darendeli(g,PI,svo,Ncyc,f)
       Input
           g = strain vector (enter a negative number to use default)
               unitless (e.g., not in percent)
           PI?????
           so  mean effective stress(in kPa)
           OCR
           Ko: lateral earth pressure coeff. at rest
         The last two argument are optional (default is used otherwise)
           Ncyc: number of cycles of loading (default is 10)
           f: frequency (Hz) (default is 1 Hz)
       Output
           NG: normalized modulus reduction
           D: Damping curve
           sNG: Standard deviation of normalized curve
           sD: Standard deviation of damping curve
    '''

    #enter parameters of model, from Table 8.12 of Darendeli
    p=np.array([3.52E-02,1.01E-03,3.25E-01,3.48E-01,9.19E-01,8.01E-01 ,1.29E-02,-1.07E-01,-2.89E-01,2.92E-01,6.33E-01,-5.66E-03, -4.23,3.62,-5.00,-2.50E-01,5.62,2.78]);
    gam=gam

    pa = 101.3;

    a=0.776;

    gamr=(0.995*(sigm/pa)**0.694)/100;

    c1=-1.1143*a**2 + 1.8618*a + 0.2523;
    c2=0.0805* a**2 - 0.0710*a - 0.0095;
    c3=-0.0005*a**2 + 0.0002*a + 0.0003;

    Gratio=1/(1+(gam/gamr)**a);

    Dmas_a1=(100/3.14*(4*((gam-gamr*np.log((gam+gamr)/gamr))/(gam**2./(gam+gamr)))-2));
    Dmas = c1*Dmas_a1 + c2*Dmas_a1**2 + c3*Dmas_a1**3;
    Dmin =  2.512*(sigm/pa)**(-0.2889);
    F = 0.712* Gratio**0.1;
    # F=0.637;
    D = F*Dmas + Dmin
    phi13 = -4.23
    phi14 = 3.62
    phi15 = -5.00
    phi16 = -2.50E-01
    if MRD_rand == 1:
        PI=50
        ro=-0.5
        sNG = np.exp(phi13)+np.sqrt(0.25/np.exp(phi14)-((Gratio-0.5)**2)/np.exp(phi14))
        sD = np.exp(phi15)+np.exp(phi16)*np.sqrt(D)
        sDmin=np.exp(phi15)+np.exp(phi16)*np.sqrt(Dmin)
        # Make the variability of G/Gmax before threshold strain zero
        gtf_r=np.array([0.000331314,0.000479492,0.000700539,0.00095242,0.001268053,0.001704959,0.00220124,0.002564315,0.00389172,0.00389172])
        PI_r=np.array([0,4.637925427,11.70840849,18.02339908,25.00172487,33.18696591,39.62171166,44.97718245,60,200])
        gtf=np.exp(np.interp(PI,PI_r,np.log(gtf_r)))
        ga=gtf*30
        z=np.ones(len(gam))
        for i in range(len(gam)):
            if gam[i]*100>=ga:
                z[i]=1
            elif gam[i]*100<gtf:
                z[i]=0
            else:
                z[i] = ((np.log(gam[i]*100)-np.log(gtf))/(np.log(ga)-np.log(gtf)))
        sNG=z*sNG

        D_log = np.log(D/np.sqrt(1+sD**2/D**2))
        Dmin_log = np.log(Dmin/np.sqrt(1+sDmin**2/Dmin**2))
        Gratio_log = np.log(Gratio/np.sqrt(1+sNG**2/Gratio**2))
        sD_log = np.sqrt(np.log(1+sD**2/D**2))
        sDmin_log = np.sqrt(np.log(1+sDmin**2/Dmin**2))
        sNG_log = np.sqrt(np.log(1+sNG**2/Gratio**2))
        if epsilon==-10:
            epsilon1=-5
            epsilon2=-5
            while abs(epsilon1)>=2 or abs(epsilon1)>=2:
                epsilon1 = np.random.normal(loc=0.0, scale=1.0)
                epsilon2 = np.random.normal(loc=0.0, scale=1.0)
                epsilon2=((1-ro**2)**0.5)*epsilon2+ro*epsilon1
        else:
            epsilon1=epsilon[0]
            epsilon2=epsilon[1]
        Gratio_n=np.exp(Gratio_log+epsilon1*sNG_log)
        D_n=np.exp(D_log+epsilon2*sD_log)
        Dmin_n=np.exp(Dmin_log+epsilon2*sDmin_log)

        D=D_n
        Gratio=Gratio_n
        Dmin=Dmin_n
        D=np.minimum(D,15)

        # If you have Su value correct the curve for it
        if Su!=-1.0:

            sSu = 0.5 #<---
            # Yee et al. (2013)
            g1_vector = np.array([0.3, 0.1, 0.07, 0.05, 0.03, 0.01, 0.005]) / 100
            for g1 in g1_vector:

                condition = gam > g1

                A = np.extract(condition , gam)

                g_1 = A[0]

                A = np.extract(condition , Gratio)

                Gratio_1 = A[0]

                condition = gam<g1

                A = np.extract(condition , gam)

                g_2 = A[-1]

                A = np.extract(condition ,Gratio )

                Gratio_2 = A[-1]

                NG1=(Gratio_2*g_2 - Gratio_1*g_1) / (g_2 - g_1)

                tauf_n=np.exp(np.log(Su) + epsilon1 *sSu )

                gref=(tauf_n - (g_1/100)*Gratio_1*Gmax)/(NG1*Gmax)*100

                if gref>0 and NG1>0:

                    break

            count=0

            for g in gam:

                if g>g1:

                    Gratio[count] = (g_1*Gratio_1+np.sign(gref)*NG1*(g-g_1)/(1+(g-g_1)/np.absolute(gref)))/g

                count+=1

    elif Su != -1.0:

        # Yee et al. (2013)
        g1_vector =np.array([0.3, 0.1, 0.07, 0.05, 0.03, 0.01, 0.005]) / 100

        for g1 in g1_vector:

            condition = gam > g1

            A = np.extract(condition , gam)

            g_1 = A[0]

            A = np.extract(condition , Gratio)

            Gratio_1 = A[0]

            condition = gam<g1

            A = np.extract(condition , gam)

            g_2 = A[-1]

            A = np.extract(condition ,Gratio )

            Gratio_2 = A[-1]

            NG1=(Gratio_2*g_2 - Gratio_1*g_1) / (g_2 - g_1)

            tauf_n=Su

            gref=(tauf_n - (g_1/100)*Gratio_1*Gmax)/(NG1*Gmax)*100

            if gref>0 and NG1>0:
                break

        count=0

        for g in gam:

            if g>g1:
                Gratio[count] = (g_1*Gratio_1+np.sign(gref)*NG1*(g-g_1)/(1+(g-g_1)/np.absolute(gref)))/g

            count+=1
    return gam , Gratio, D/100 , Dmin/100

def Vucetic_Dobry (gam = straindef, PI=0):
    '''
    PI = Plasticity Index
    '''
    gam=gam*100
    if PI==0:
        gamref = np.array ([1.0e-4, 3.16e-4 ,1e-3, 3.16e-3, 1e-2, 3.16e-2, 1e-1, 3.16e-1, 1])
        Gref = np.array ([1 , 1 , 0.96 , 0.88 , 0.7 , 0.47 , 0.26 , 0.11 , 0.03])
        Dref = np.array ([1 , 1 , 1 , 3 , 5.4 , 9.8 , 15 , 20.3 , 24])
    elif PI==15:
        gamref = np.array ([1.0e-4, 3.16e-4 ,1e-3, 3.16e-3, 1e-2, 3.16e-2, 1e-1, 3.16e-1, 1])
        Gref = np.array ([1 , 1 , 0.99 , 0.94 , 0.81 , 0.64 , 0.41 , 0.22 , 0.1])
        Dref = np.array ([1 , 1 , 1 , 2.6 , 4.5 , 7.5 , 11.6 , 16 , 20])
    elif PI==30:
        gamref = np.array ([1.0e-4, 3.16e-4 ,1e-3, 3.16e-3, 1e-2, 3.16e-2, 1e-1, 3.16e-1, 1])
        Gref = np.array ([1 , 1 , 1 , 0.98 , 0.9 , 0.75 , 0.53 , 0.35 , 0.17])
        Dref = np.array ([1 , 1 , 1 , 2.1 , 3.8 , 5.9 , 8.8 , 12.5 , 16.9])
    elif PI==50:
        gamref = np.array ([1.0e-4, 3.16e-4 ,1e-3, 3.16e-3, 1e-2, 3.16e-2, 1e-1, 3.16e-1, 1])
        Gref = np.array ([1 , 1 , 1 , 1 , 0.95 , 0.84 , 0.67 , 0.47 , 0.25])
        Dref = np.array ([1 , 1 , 1 , 1.8 , 2.9 , 4.3 , 6.2 , 9.5 , 13.5])
    elif PI==100:
        gamref = np.array ([1.0e-4, 3.16e-4 ,1e-3, 3.16e-3, 1e-2, 3.16e-2, 1e-1, 3.16e-1, 1])
        Gref = np.array ([1 , 1 , 1 , 1 , 0.98 , 0.92 , 0.81 , 0.63 , 0.37])
        Dref = np.array ([1 , 1 , 1 , 1.5 , 2 , 2.9 , 4.1 , 6.2 , 9.8])
    elif PI==200:
        gamref = np.array ([1.0e-4, 3.16e-4 ,1e-3, 3.16e-3, 1e-2, 3.16e-2, 1e-1, 3.16e-1, 1])
        Gref = np.array ([1 , 1 , 1 , 1 , 1 , 0.96 , 0.89 , 0.75 , 0.53 ])
        Dref = np.array ([1 , 1 , 1.1 , 1.3 , 1.6 , 2.1 , 3 , 4.8 , 8.1 ])
    else:
        print('In Vucetic_Dobry model PI should be one of these values: 0,15, 30,50,100,200')
    Gratio = np.interp( np.log10(gam) , np.log10(gamref), Gref)
    D = np.interp( np.log10(gam) , np.log10(gamref), Dref)
    return gam / 100, Gratio, D/100

def Undamped_linear (gam = straindef):
    '''
    PI = Plasticity Index
    '''

    Gratio = np.ones(len(gam))
    D = np.zeros(len(gam))
    print(Gratio)
    return gam , Gratio, D/100


from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Q
from drf_yasg.utils import swagger_auto_schema
from rest_framework import permissions
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

from hotelcontent.models import Hotel, Rooms, Bookings, AgentReservation, User
from .functions import str_to_date, final_price_list, final_price
from .serializers import (
    HotelsSerializer,
    BookingSerializer,
    FilterHotelsSerializer,
    CancelBookingSerializer,
    CreateBookingSerializer,
    RateSerializer,
    RateFilterSerializer,
)

class HotelsView(APIView):
    """Endpoint for viewing available hotels"""

    @swagger_auto_schema(responses={200: HotelsSerializer})
    def get(self, request):
        hotels = Hotel.objects.all()
        serializer = HotelsSerializer(hotels, many=True)
        return Response({"hotels": serializer.data})

    @swagger_auto_schema(
        request_body=FilterHotelsSerializer, responses={200: HotelsSerializer}
    )
    def post(self, request):
        """Filters hotels by coordinates within a certain radius"""

        filter_hotels = []

        lat = request.data.get("latitude")
        long = request.data.get("longitude")
        rad = request.data.get("radius")

        for p in Hotel.objects.raw(
            "SELECT * FROM hotelcontent_hotel WHERE earth_distance(ll_to_earth(%s,%s),ll_to_earth(float8("
            "hotel_lat), float8(hotel_long))) / 1000 <= %s", [lat, long, rad]
        ):
            filter_hotels.append(p)

        serializer = HotelsSerializer(filter_hotels, many=True)

        return Response({"hotels in range " + str(rad) + "km": serializer.data})

class CancelBooking(APIView):
    """Endpoint for canceling active bookings"""

    permission_classes = [permissions.IsAuthenticated]

    @swagger_auto_schema(
        request_body=CancelBookingSerializer,
        responses={200: "This booking has been canceled"},
    )
    def post(self, request):
        """Cancel booking by id"""
        user_name = request.user.username
        user = User.objects.get(username=user_name)
        agent = AgentReservation.objects.get(agent=user)
        booking_id = request.data.get("booking_id")
        try:
            booking = Bookings.objects.get(id=booking_id, agent_reservation=agent)
            booking.booking_stat = False
            booking.save()
            return Response("This booking has been canceled", status=status.HTTP_200_OK)
        except ObjectDoesNotExist:
            return Response(
                "Error! Booking with this id doesn't exist!",
                status=status.HTTP_404_NOT_FOUND,
            )

class BookingView(APIView):
    """Endpoint for booking a rate"""

    permission_classes = [permissions.IsAuthenticated]

    @swagger_auto_schema(
        request_body=CreateBookingSerializer, responses={201: "Successfully booked"}
    )
    def post(self, request):
        """Book the rate by checkin, checkout date, room number and hotel"""
        agent_name = request.user.username
        agent_user = User.objects.get(username=agent_name)
        agent = AgentReservation.objects.get(agent=agent_user)

        start_date, end_date = str_to_date(request)

        if start_date == "error":
            return Response(
                "start_date and end_date must be not Empty",
                status=status.HTTP_400_BAD_REQUEST,
            )
        if start_date == "incorrect date":
            return Response(
                "Incorrect start_date or end_date", status=status.HTTP_400_BAD_REQUEST
            )

        room_num = int(request.data.get("room_number"))
        hotel = Hotel.objects.get(hotel_name=request.data.get("hotel"))
        room = Rooms.objects.get(room_number=room_num, hotel=hotel)

        if Rooms.objects.filter(
            Q(id=room.id)
            & Q(bookings__booking_stat=True)
            & (
                (
                    Q(bookings__checkin__lt=end_date)
                    & Q(bookings__checkout__gte=end_date)
                )
                | (
                    Q(bookings__checkin__lte=start_date)
                    & Q(bookings__checkout__gt=start_date)
                )
            )
        ).exists():
            return Response(
                "Not created, Booking is exist", status=status.HTTP_400_BAD_REQUEST
            )

        room = final_price(
            room=room, start_date=start_date, end_date=end_date, request=request
        )

        booking = Bookings(
            agent_reservation=agent,
            booking_stat=True,
            hotels=hotel,
            checkin=start_date,
            checkout=end_date,
            rate_price=room.room_price,
            room=room,
            room_number=room.room_number,
            hotel=hotel.hotel_name,
        )
        booking.save()
        return Response("Successfully created", status=status.HTTP_201_CREATED)

class RatesFilterDateView(APIView):
    """Endpoint for viewing available rates"""

    @swagger_auto_schema(
        request_body=RateFilterSerializer, responses={200: RateSerializer}
    )
    def post(self, request):
        """Filter available rates by checkin and checkout date"""
        start_date, end_date = str_to_date(request)

        free_rooms = list(
            Rooms.objects.exclude(
                Q(bookings__booking_stat=True)
                & (
                    (
                        Q(bookings__checkin__lt=end_date)
                        & Q(bookings__checkout__gte=end_date)
                    )
                    | (
                        Q(bookings__checkin__lte=start_date)
                        & Q(bookings__checkout__gt=start_date)
                    )
                )
            )
        )
        free_rooms = final_price_list(
            free_rooms=free_rooms,
            start_date=start_date,
            end_date=end_date,
            request=request,
        )
        serializer = RateSerializer(free_rooms, many=True)
        return Response({"rooms": serializer.data})

class MyBookingsView(APIView):
    """Endpoint for viewing available bookings"""

    permission_classes = [permissions.IsAuthenticated]

    @swagger_auto_schema(responses={200: BookingSerializer})
    def get(self, request):
        agent_name = request.user.username
        agent_object = User.objects.get(username=agent_name)
        agent = AgentReservation.objects.get(agent=agent_object)

        bookings = Bookings.objects.filter(agent_reservation=agent)
        serializer = BookingSerializer(bookings, many=True)
        return Response({"Bookings": serializer.data})


# -*- coding:ascii -*-
from mako import runtime, filters, cache
UNDEFINED = runtime.UNDEFINED
__M_dict_builtin = dict
__M_locals_builtin = locals
_magic_number = 10
_modified_time = 1425761005.751362
_enable_loop = True
_template_filename = '/Users/spencerlowe/PycharmProjects/chef-master/inventory/templates/shoppingCart.html'
_template_uri = 'shoppingCart.html'
_source_encoding = 'ascii'
import os, os.path, re
_exports = ['content', 'extra_links']

def _mako_get_namespace(context, name):
    try:
        return context.namespaces[(__name__, name)]
    except KeyError:
        _mako_generate_namespaces(context)
        return context.namespaces[(__name__, name)]
def _mako_generate_namespaces(context):
    pass
def _mako_inherit(template, context):
    _mako_generate_namespaces(context)
    return runtime._inherit_from(context, '/base_app/templates/base_ajax.htm', _template_uri)
def render_body(context,**pageargs):
    __M_caller = context.caller_stack._push_frame()
    try:
        __M_locals = __M_dict_builtin(pageargs=pageargs)
        STATIC_URL = context.get('STATIC_URL', UNDEFINED)
        def content():
            return render_content(context._locals(__M_locals))
        items = context.get('items', UNDEFINED)
        def extra_links():
            return render_extra_links(context._locals(__M_locals))
        __M_writer = context.writer()
        __M_writer('\n\n')
        __M_writer('\n\n')
        if 'parent' not in context._data or not hasattr(context._data['parent'], 'extra_links'):
            context['self'].extra_links(**pageargs)

        __M_writer('\n\n')
        if 'parent' not in context._data or not hasattr(context._data['parent'], 'content'):
            context['self'].content(**pageargs)

        return ''
    finally:
        context.caller_stack._pop_frame()

def render_content(context,**pageargs):
    __M_caller = context.caller_stack._push_frame()
    try:
        def content():
            return render_content(context)
        items = context.get('items', UNDEFINED)
        __M_writer = context.writer()
        __M_writer('\n\n
\n\n
\n
\n
\n\n                    \n                        Item Name\n                    	\n\n                    \n                        Quantity\n                    	\n                    \n                        Total Price\n                    	\n                    \n                        Remove\n                    	\n
\n
\n
\n\n        ')
        totalPrice = 0

        __M_writer('\n')
        for item in items:
            __M_writer('
\n                \n                    ')
            __M_writer(str( item.name ))
            __M_writer('\n                	\n                \n                    ')
            __M_writer(str( items[item] ))
            __M_writer('\n                	\n                \n                    $')
            __M_writer(str( item.price * items[item] ))
            __M_writer('\n                	\n                \n
\n                        <button class="btn btn-danger" data-id="')
            __M_writer(str( item.id ))
            __M_writer('" id="delete_item">Delete</button>\n
\n                	\n
\n            ')
            totalPrice += (item.price * items[item])

            __M_writer('\n')
        __M_writer('
\n                \n                    Total\n                	\n                	\n                \n                    $')
        __M_writer(str( totalPrice ))
        __M_writer('\n                	\n
\n
\n\n
\n
\n            \n                <button class="btn btn-warning" id="btn_purchase">Check Out</button>\n            \n
\n
\n\n')
        return ''
    finally:
        context.caller_stack._pop_frame()

def render_extra_links(context,**pageargs):
    __M_caller = context.caller_stack._push_frame()
    try:
        STATIC_URL = context.get('STATIC_URL', UNDEFINED)
        def extra_links():
            return render_extra_links(context)
        __M_writer = context.writer()
        __M_writer('\n    <link rel="stylesheet" type="text/css" href="')
        __M_writer(str( STATIC_URL ))
        __M_writer('homepage/styles/login.css">\n')
        return ''
    finally:
        context.caller_stack._pop_frame()

"""
__M_BEGIN_METADATA
{"filename": "/Users/spencerlowe/PycharmProjects/chef-master/inventory/templates/shoppingCart.html", "uri": "shoppingCart.html", "source_encoding": "ascii", "line_map": {"64": 40, "65": 41, "66": 42, "67": 44, "68": 44, "69": 47, "70": 47, "71": 50, "72": 50, "73": 54, "74": 54, "75": 58, "77": 58, "78": 60, "79": 66, "80": 66, "86": 11, "27": 0, "93": 11, "94": 12, "95": 12, "101": 95, "38": 7, "39": 9, "44": 13, "54": 15, "61": 15, "62": 40}}
__M_END_METADATA
"""


import argparse, sys, time, json
sys.path.append('/home/zhhao/audioST/instruct_speech_llama')
from transformers import AutoTokenizer, AutoModelForCausalLM
from tqdm import tqdm
import torch, transformers
from instruct_speech_new.eval.utils import disable_torch_init
from instruct_speech_new.model.model import SpeechLlamaForCausalLM, SpeechLlamaConfig
from instruct_speech_new.model.utils import KeywordsStoppingCriteria
from fairseq.data.audio.speech_to_text_dataset import _collate_frames
from instruct_speech_new.train.dataset import PromptSpeechToTextDatasetCreator, SpeechToTextDatasetItem
from instruct_speech_new import conversation as conversation_lib
from instruct_speech_new.conversation import SeparatorStyle

import os
import requests

import torch.nn.functional as F

IGNORE_INDEX = -100
DEFAULT_PAD_TOKEN = "[PAD]"
DEFAULT_EOS_TOKEN = "</s>"
DEFAULT_BOS_TOKEN = "<s>"
DEFAULT_UNK_TOKEN = "<unk>"
DEFAULT_SPEECH_TOKEN = "<speech>"
DEFAULT_SPEECH_PATCH_TOKEN = "<sp_patch>"
DEFAULT_SPEECH_START_TOKEN = "<sp_start>"
DEFAULT_SPEECH_END_TOKEN = "<sp_end>"

prompt_list = ['<speech_here> Start by converting the English audio into German written form.',]

def eval_model(args):
    load_type = torch.float16 # torch.float32
    # Model
    disable_torch_init()
    model_name = os.path.expanduser(args.model_name)
    tokenizer = transformers.AutoTokenizer.from_pretrained(
        args.model_name,
        padding_side="right",
        use_fast=False,
    )
    config = json.load(open(os.path.join(args.model_name, 'config.json')))
    config['large_model'] = True
    update_config = os.path.join(args.model_name, 'config_large.json')
    json.dump(config, open(update_config, 'w'), indent=2)
    model = SpeechLlamaForCausalLM.from_pretrained(args.model_name,
                                                   torch_dtype=load_type,
                                                   low_cpu_mem_usage=True,
                                                   device_map='auto',
                                                   config=update_config,).eval()
    if 'model.embed_tokens' in model.hf_device_map.keys():
        device_input = 'cuda:' + str(model.hf_device_map['model.embed_tokens'])
        device_output = 'cuda:' + str(model.hf_device_map['lm_head'])
    else:
        device_input = 'cuda'
        device_input = 'cuda'
    length_after_ssl, length_after_adp = model.model.initialize_speech_modules(
        speech_tower_path=model.config.speech_tower_path,
        speech_tower_type=None,
        len_adapter_channels=model.config.len_adapter_channels,
        len_adapter_kernel_sizes=model.config.len_adapter_kernel_sizes,
        ssl_fintuned=model.config.ssl_fintuned,
    )
    model.model.speech_tower.to(dtype=load_type, device=device_input)

    length_adapter_weights = torch.load(args.length_adapter_path, map_location='cpu')
    mlp_adapter_weights = torch.load(args.mlp_adapter_path, map_location='cpu')

    model.model.mm_length_adapter.load_state_dict(length_adapter_weights)
    model.model.mm_mlp_adapter.load_state_dict(mlp_adapter_weights)

    model.model.mm_length_adapter.to(dtype=load_type, device=device_input)
    model.model.mm_mlp_adapter.to(dtype=load_type, device=device_input)

    test_dataset = PromptSpeechToTextDatasetCreator.from_tsv(args.data_path, args.data_split)

    if not os.path.exists(os.path.join(args.result, args.data_split)):
        os.makedirs(os.path.join(args.result, args.data_split))

    ref_file = open(os.path.join(args.result, args.data_split, "ref"), "w")
    hyp_file = open(os.path.join(args.result, args.data_split, "hyp"), "w")
    conv = conversation_lib.default_conversation.copy()
    for test_data in tqdm(test_dataset):
        source, ref, id = test_data.source, test_data.target, test_data.id
        speech_batch = _collate_frames([source], is_audio_input=True)
        n_frames = torch.tensor([source.size(0)], dtype=torch.long)
        speech_lens = length_after_adp(length_after_ssl(n_frames))

        to_adds = [int(speech_len)*DEFAULT_SPEECH_PATCH_TOKEN for speech_len in speech_lens]
        to_adds = [DEFAULT_SPEECH_START_TOKEN + to_add + DEFAULT_SPEECH_END_TOKEN for to_add in to_adds]

        qs = prompt_list[0]
        before, after = qs.split('<speech_here>')
        mm_prompts = [before + to_add + after for to_add in to_adds]

        conv.messages = []
        conv.append_message(conv.roles[0], mm_prompts[0])
        conv.append_message(conv.roles[1], None)
        prompt_inputs = conv.get_prompt()
        inputs = tokenizer([prompt_inputs])
        input_ids = torch.as_tensor(inputs.input_ids).cuda()

        stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
        keywords = [stop_str]
        stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
        with torch.inference_mode():
            output_ids = model.generate(
                attention_mask=input_ids.ne(tokenizer.pad_token_id),
                input_ids=input_ids.to(device=device_input),
                speech_batch=speech_batch.to(dtype=load_type, device=device_input),
                src_lengths=n_frames.to(device=device_input),
                after_lens=speech_lens.to(device=device_input),
                #do_sample=True,
                #temperature=0.8,
                num_beams=args.beam,
                max_new_tokens=500,
                stopping_criteria=[stopping_criteria])
        input_token_len = input_ids.shape[1]
        outputs = tokenizer.batch_decode(output_ids[:, input_token_len:], skip_special_tokens=True)[0]
        outputs = outputs.strip()
        print(outputs)
        if outputs.endswith(stop_str):
            outputs = outputs[:-len(stop_str)]
        outputs = outputs.strip()
        print(f"{id} decode complete,\nref:{ref} \nhyp:{outputs}")
        print(f"{id}\t{ref}", file=ref_file)
        print(f"{id}\t{outputs}", file=hyp_file)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--model-name", type=str, default="facebook/opt-350m")
    parser.add_argument("--length-adapter-path", type=str, required=True)
    parser.add_argument("--mlp-adapter-path", type=str, required=True)
    parser.add_argument("--data-path", type=str, required=True)
    parser.add_argument("--data-split", type=str, required=True)
    parser.add_argument("--result", type=str, required=True)
    parser.add_argument("--beam", type=int, default=1)
    args = parser.parse_args()

    eval_model(args)


# -*- coding: utf8 -*-

class Utilisateur:                      # création de la classe "Utilisateur"

    def __init__(self, prenom, age):    # initialisation de la classe avec création des attributs
        self.user_name = prenom         # création de l'attribut "user_name" qui contiendra le prénom du client (ie de l'utilisateur)
        self.user_age = age             # création de l'attribut "user_age" qui contiendra l'âge du client (ie de l'utilisateur)

    def affich_nom_age(self):           # # création de la méthode "affich_nom_age" qui affichera le prénom et l'âge du client
        print(f"Je m'appelle {self.user_name} et j'ai {self.user_age} ans.")

first_client = Utilisateur("Pierre", 29)        # création d'une instance "Utilisateur" (un objet) appelé "first_client"
first_client.affich_nom_age()                   # utilisation de la méthode "affich_nom_age" qui permet d'afficher Prénom et age du client

second_client = Utilisateur("Sophie", 50)        # création d'un objet, de la classe "Utilisateur", appelé "second_client"
second_client.affich_nom_age()                   # utilisation de la méthode "affich_nom_age" qui permet d'afficher Prénom et age du client

print(first_client.__dict__)

Fonctionnels/Classes/02 Objet/Création d'un objet (attributs et méthode) (5).py


from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
import time
import pandas as pd

file_path = "resources/skin1.csv"

start = time.time()
df_raw = pd.read_csv(file_path, delimiter=',')
x_train = []
y_train = []

x_test = []
y_test = []

for i in range(len(df_raw)):
    if i < len(df_raw) * 0.8:
        x_train.append([df_raw['R'][i], df_raw['G'][i], df_raw['B'][i]])
        y_train.append(df_raw['SKIN'][i])
    else:
        x_test.append([df_raw['R'][i], df_raw['G'][i], df_raw['B'][i]])
        y_test.append(df_raw['SKIN'][i])

kn = KNeighborsClassifier(n_neighbors=5, n_jobs=4)
kn.fit(x_train, y_train)
kn_predict = kn.predict(x_test)
kn_acc_score = accuracy_score(y_test, kn_predict)
end = time.time()
print("Total time ", end - start)
print("Acurancy ", kn_acc_score)


import os
from typing import Set

from git import Repo

BACKEND_ALIASES = {
    "tf": "tensorflow",
    "torch": "torch",
    "implicit": "implicit",
    "lightfm": "lightfm",
    "xgb": "xgboost",
}
OTHER_MARKERS = {"unit", "integration", "examples", "datasets", "horovod", "transformers"}
SHARED_MODULES = {
    "/datasets/",
    "/models/config/",
    "/models/utils/",
    "/models/io.py",
}

COMPARE_BRANCH = os.environ.get("COMPARE_BRANCH", "main")

def get_changed_backends(compare_branch: str = COMPARE_BRANCH) -> Set[str]:
    """
    Check which backends need to be tested based on the changed files in the current branch.

    This function scans the current branch of a repository and identifies files that have changed.
    If a change is detected in a backend file, the corresponding backend alias is added to a set.
    If a change is detected in a shared file, all backend aliases are returned.

    Example usage::
        >>> get_changed_backends()
        {'backend1', 'backend2'}

    Returns
    -------
    set
        A set of backends that need to be tested. The backends are identified based on the
        changed files in the current branch of the repository.
    """

    if not compare_branch:
        compare_branch = COMPARE_BRANCH

    repo = Repo()

    commit = repo.head.commit  # Current branch last commit

    if compare_branch not in repo.branches:
        origin = repo.remotes.origin
        origin.fetch(compare_branch)
        ref = getattr(origin.refs, compare_branch)
        local_branch = repo.create_head(compare_branch, ref)
        local_branch.set_tracking_branch(ref)

    if compare_branch in repo.branches:
        comparison = repo.branches[compare_branch]
    else:
        raise ValueError("Could not find comparison branch")
    diffs = commit.diff(comparison)

    changed_files = set()
    for change_type in ["A", "D", "R", "M", "T"]:
        for diff in diffs.iter_change_type(change_type):
            if diff.a_path:
                changed_files.add(diff.a_path)
            if diff.b_path:
                changed_files.add(diff.b_path)

    untracked_files = {file for file in repo.untracked_files}

    # Use the git diff command to get unstaged changes
    unstaged_files = repo.git.diff(None, name_only=True).split()

    all_changed_files = changed_files.union(untracked_files, unstaged_files)

    changed_backends = set()
    for file in all_changed_files:
        try:
            # If shared file is updated, we need to run all backends
            for shared in SHARED_MODULES:
                if shared in file:
                    output = set(BACKEND_ALIASES.values())
                    output = output.union({"datasets", "horovod", "transformers"})

                    return output

            name = file.split("/")[2]
            if name in BACKEND_ALIASES:
                changed_backends.add(BACKEND_ALIASES[name])
        except IndexError:
            continue

    return changed_backends

def backend_has_changed(backend_name: str, compare_branch: str = COMPARE_BRANCH) -> bool:
    """
    Check if a specific backend needs to be tested based on the changed files in the current branch.

    This function utilizes the get_changed_backends function to check if a specific backend has
    experienced changes and hence requires testing.

    Parameters
    ----------
    backend_name : str
        The name of the backend to check for changes.

    Returns
    -------
    bool
        Returns True if the backend has changed and needs testing, False otherwise.

    """
    changed_backends = get_changed_backends(compare_branch)
    output: bool = False

    for backend in backend_name.split("|"):
        output |= backend in changed_backends

    return output


# Exercise 6.2
# The Ackermann function

# Guardian to make sure inputs are correct
def is_nonnegative_integer(n):
    return isinstance(n, int) and n>= 0

def ack(m,n):
    """Compute the Ackermann function of m and n
    m and n should be nonnegative integers
    """
    # Guardian to make sure m and n are nonnegative integers
    if not (is_nonnegative_integer(m) and is_nonnegative_integer(n)):
        print("m and n must be nonnegative integers.")
        return
    if m==0:
        return n+1
    elif m>0 and n==0:
        return ack(m-1,1)
    elif m>0 and n>0:
        return ack(m-1,ack(m,n-1))

print(ack(3,4)) #125
print(ack(3,6))
print(ack(3,7))
print(ack(3,8))
print(ack(3,9))
#print(ack(4,4)) #maximum recursion depth exceeded


import sys
input = sys.stdin.readline

def solution(lst):

    coins, dp = [], []
    for i in range(len(lst)):
        for _ in range(lst[i][1]):
            coins.append(lst[i][0])

    for i in range(2, sum(coins) // 2 + 1):
        dp[i]

    dp = [[-1] * (sum(coins) // 2 + 1) for _ in range(len(coins))]
    # return

for _ in range(3):
    N = int(input())
    lst = [list(map(int, input().split())) for _ in range(N)]
    print(solution(lst))


"""
## Introduction
This script shows an implementation of the Actor Critic method for the game Snake.
It is based very closely on the example of the Keras documentation
[Actor Critic Method](https://keras.io/examples/rl/actor_critic_cartpole/)
written by [Apoorv Nandan](https://twitter.com/NandanApoorv).

### Actor Critic Method
As an agent takes actions and moves through an environment, it learns to map
the observed state of the environment to two possible outputs:
1. Recommended action: A probability value for each action in the action space.
   The part of the agent responsible for this output is called the **actor**.
2. Estimated rewards in the future: Sum of all rewards it expects to receive in the
   future. The part of the agent responsible for this output is the **critic**.
Agent and Critic learn to perform their tasks, such that the recommended actions
from the actor maximize the rewards.

### References
- [Actor Critic Method](https://hal.inria.fr/hal-00840470/document)
"""
"""
## Setup
"""

import sys

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

from snake import Snake

# Configuration parameters for the whole setup
gamma = 0.99  # Discount factor for past rewards
max_steps_per_episode = 10000
env = Snake()  # Create the environment
eps = np.finfo(np.float32).eps.item()  # Smallest number such that 1.0 + eps != 1.0

"""
## Implement Actor Critic network
This network learns two functions:
1. Actor: This takes as input the state of our environment and returns a
probability value for each action in its action space.
2. Critic: This takes as input the state of our environment and returns
an estimate of total rewards in the future.
In our implementation, they share the initial layer.
"""

num_inputs = 7
num_actions = 3
num_hidden = 128

inputs = layers.Input(shape=(num_inputs,))
common = layers.Dense(num_hidden, activation="relu")(inputs)
action = layers.Dense(num_actions, activation="softmax")(common)
critic = layers.Dense(1)(common)

model = keras.Model(inputs=inputs, outputs=[action, critic])

"""
## Train
"""

optimizer = keras.optimizers.Adam(learning_rate=0.01)
huber_loss = keras.losses.Huber()
action_probs_history = []
critic_value_history = []
rewards_history = []
running_reward = 0
episode_count = 0

while True:  # Run until solved
    state = env.reset()
    episode_reward = 0
    with tf.GradientTape() as tape:
        for timestep in range(1, max_steps_per_episode):
            # Adding this line would show the attempts
            env.render()
            # of the agent in a pop up window.

            state = tf.convert_to_tensor(state)
            state = tf.expand_dims(state, 0)

            # Predict action probabilities and estimated future rewards
            # from environment state
            action_probs, critic_value = model(state)
            critic_value_history.append(critic_value[0, 0])

            # Sample action from action probability distribution
            action = np.random.choice(num_actions, p=np.squeeze(action_probs))
            action_probs_history.append(tf.math.log(action_probs[0, action]))

            # Apply the sampled action in our environment
            state, reward, done, _ = env.step(action)
            rewards_history.append(reward)
            episode_reward += reward

            if done:
                break

        # Update running reward to check condition for solving
        running_reward = 0.05 * episode_reward + (1 - 0.05) * running_reward

        # Calculate expected value from rewards
        # - At each timestep what was the total reward received after that timestep
        # - Rewards in the past are discounted by multiplying them with gamma
        # - These are the labels for our critic
        returns = []
        discounted_sum = 0
        for r in rewards_history[::-1]:
            discounted_sum = r + gamma * discounted_sum
            returns.insert(0, discounted_sum)

        # Normalize
        returns = np.array(returns)
        returns = (returns - np.mean(returns)) / (np.std(returns) + eps)
        returns = returns.tolist()

        # Calculating loss values to update our network
        history = zip(action_probs_history, critic_value_history, returns)
        actor_losses = []
        critic_losses = []
        for log_prob, value, ret in history:
            # At this point in history, the critic estimated that we would get a
            # total reward = `value` in the future. We took an action with log probability
            # of `log_prob` and ended up recieving a total reward = `ret`.
            # The actor must be updated so that it predicts an action that leads to
            # high rewards (compared to critic's estimate) with high probability.
            diff = ret - value
            actor_losses.append(-log_prob * diff)  # actor loss

            # The critic must be updated so that it predicts a better estimate of
            # the future rewards.
            critic_losses.append(
                huber_loss(tf.expand_dims(value, 0), tf.expand_dims(ret, 0))
            )

        # Backpropagation
        loss_value = sum(actor_losses) + sum(critic_losses)
        grads = tape.gradient(loss_value, model.trainable_variables)
        optimizer.apply_gradients(zip(grads, model.trainable_variables))

        # Clear the loss and reward history
        action_probs_history.clear()
        critic_value_history.clear()
        rewards_history.clear()

        if "--test" in sys.argv:
            sys.exit()

    # Log details
    episode_count += 1
    if episode_count % 10 == 0:
        template = "running reward: {:.2f} at episode {}"
        print(template.format(running_reward, episode_count))

    if running_reward > 20:  # Condition to consider the task solved
        print("Solved at episode {}!".format(episode_count))
        model.save("snakeAC.h5")
        break


# An array is monotonic if it is either monotone increasing or monotone decreasing.

# An array A is monotone increasing if for all i <= j, A[i] <= A[j].  An array A is
# monotone decreasing if for all i <= j, A[i] >= A[j].

# Return true if and only if the given array A is monotonic.

# Example 1:
# Input: [1,2,2,3]
# Output: true

# Example 2:
# Input: [6,5,4,4]
# Output: true

# Example 3:
# Input: [1,3,2]
# Output: false

# Example 4:
# Input: [1,2,4,5]
# Output: true

# Example 5:
# Input: [1,1,1]
# Output: true

# Note:
#     1 <= A.length <= 50000
#     -100000 <= A[i] <= 100000

# ----------------------------------------------
# Ideas:

# Considerations:

# Complexity: O(n) time, O(1) space
# ----------------------------------------------

class Solution:
    def isMonotonic(self, A):
        """
        :type A: List[int]
        :rtype: bool
        """
        known_dir = False
        increase = True
        for i in range(1, len(A), 1):
            diff = A[i] - A[i - 1]
            if diff == 0:
                continue
            elif not known_dir:
                increase = diff > 0
                known_dir = True
            elif (increase and diff < 0) or (not increase and diff > 0):
                return False
        return True


# chat/consumers.py
import json
from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer
from rest_framework import serializers
from ..models import Message, Room
from ..serializers import MessageSerializer

class ChatConsumer(WebsocketConsumer):
    def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name

        self.room, created = Room.objects.get_or_create(label=self.room_name)

        # Join room group
        async_to_sync(self.channel_layer.group_add)(
            self.room_group_name,
            self.channel_name
        )

        self.accept()

    def disconnect(self, close_code):
        # Leave room group
        async_to_sync(self.channel_layer.group_discard)(
            self.room_group_name,
            self.channel_name
        )

    # Receive message from WebSocket
    def receive(self, text_data):
        text_data_json = json.loads(text_data)
        get_all_messages = text_data_json.get("get_all_messages")

        if get_all_messages == True:
            async_to_sync(self.channel_layer.group_send)(
                self.room_group_name,
                {
                    'type': 'all_messages',
                }
            )
        else:
            # Save message to DB
            message = text_data_json['message']
            message_object = Message(room=self.room, message=message)
            message_object.save()

            # Send message to room group
            async_to_sync(self.channel_layer.group_send)(
                self.room_group_name,
                {
                    'type': 'chat_message',
                    'message': message
                }
            )

    # Receive request get all messages
    def get_all_messages(self):
        all_messages = Message.objects.filter(room=self.room)
        all_messages_data = MessageSerializer(all_messages, many=True).data
        return all_messages_data

    def all_messages(self, event):
        result_messages = {
            'all_messages': self.get_all_messages(),
            'send_all_messages': True
        }
        self.send(text_data=json.dumps(result_messages))

    # Receive message from room group
    def chat_message(self, event):
        message = event['message']

        # Send message to WebSocket
        self.send(text_data=json.dumps({
            'message': message
        }))


from django.urls import path
from . import views
from django.shortcuts import render

urlpatterns = [
    path('',views.admindash,name='admindash'),
    path('feedback',views.feedback,name='feedback'),
    path('reports',views.reports,name='reports'),
    path('finalreport',views.finalreport,name='finalreport'),
    path('Users',views.Users,name='Users'),
    path('addUser',views.addUser,name='addUser'),
    path('viewUser/<str:pk>',views.viewUser,name='viewUser'),
    path('approveUser/',views.approveUser,name='approveUser'),
    path('approveUserFunc/<str:pk>',views.approveUserFunc,name='approveUserFunc'),
    path('deleteUser/<str:pk>',views.deleteUser,name='deleteUser'),
    path('deleteJob/<str:pk>',views.deleteJob,name='deleteJob'),
    path('viewjobs',views.viewjobs,name='viewjobs'),
    path('viewjobdetails/<str:pk>',views.viewjobdetails,name='viewjobdetails'),
  ]


"""
Purpose: provides an example of using sklearn_gbm_wrapper module
using California housing dataset in which on numerical feature (Population)
is converted to categorical feature

Data created: 2018-06-28

Author: Alex Radnaev

"""

import sklearn.datasets as sklearn_ds
import sklearn_gbm_ots as gbm_ots
import pandas as pd

def population_mapper(x):
    if x < 1000:
        return '<1k'
    elif x < 2000:
        return '1k-2k'
    else:
        return '2k+'

def prepare_california_housing_dataframe(outcome):
    """Returns california housing dataset as a dataframe

    arguments:
        outcome - column name for the outcome"""
    housing_dataset = sklearn_ds.california_housing.fetch_california_housing()
    X = housing_dataset.data
    y = housing_dataset.target

    df_X = pd.DataFrame(X, columns=housing_dataset.feature_names)
    df_y = pd.DataFrame(y, columns=[outcome])
    df = pd.concat([df_X, df_y], axis=1)
    df['Population'] = df['Population'].apply(population_mapper)

    return df

def run_gbm():
    """Prepares California housing dataset and build gbm model

    Returns:
        fitted sklearn_gbm_wrapper.GBMwrapper object"""
    outcome = 'house price, 100k'

    df = prepare_california_housing_dataframe(outcome)

    housing_gbm = gbm_ots.GBMwrapper(
        df[:100],
        outcome,
        show_plots=False,
        random_state=2018)

    gbm_params = {
        'n_estimators': 300,
        'max_depth': 3,
        'max_features': 5,
        'min_samples_split': 10,
        'min_samples_leaf': 5,
        'learning_rate': 0.5,
        'loss': 'ls'}

    housing_gbm.build_model(params=gbm_params)

    return housing_gbm

def custom_plotting(gbm):
    """Demonstrates how to customize plots"""
    gbm.plot_output(
        ax_limits={'AveBedrms': {'xlim': [0.9, 1.2]}},
        figsize=(11, 11),
        absolute_yticks=False,
        absolute_yscale=False,
        file_prefix='relative_scale_relative_ticks')

    gbm.plot_output(
        ax_limits={'AveBedrms': {'xlim': [0.9, 1.2]}},
        figsize=(11, 11),
        absolute_yticks=True,
        absolute_yscale=False,
        file_prefix='relative_scale_abs_ticks')

    # gbm.plot_output(
    #     ax_limits={'AveBedrms': {'xlim': [0.9, 1.2]}},
    #     figsize=(11, 11),
    #     absolute_yticks=False,
    #     absolute_yscale=True,
    #     file_prefix='abs_scale_relative_ticks')

    # gbm.plot_output(
    #     ax_limits={'AveBedrms': {'xlim': [0.9, 1.2]}},
    #     figsize=(11, 11),
    #     absolute_yticks=True,
    #     absolute_yscale=True,
    #     file_prefix='abs_scale_abs_ticks')

def main():
    housing_gbm = run_gbm()
    custom_plotting(housing_gbm)

if __name__ == '__main__':
    main()


from aiogram import Dispatcher
from aiogram.dispatcher import FSMContext
from aiogram.types import (
    Message,
    CallbackQuery,
    InlineKeyboardButton,
    InlineKeyboardMarkup,
)

from bot import dp
from bot import bot
from app.middlewares import helpers
from app.states.department import Department
from app.database import departments as depart_db
from app.middlewares.checks import check_is_admin
from app.middlewares.state_check import state_check
from app.handlers.departments.depart_helper import *
from app.keyboards.inline.helper_buttons import back
from app.keyboards.inline import callback_datas as cd
from app.keyboards.inline import department_buttons as kb
from app.middlewares.helpers import call_chat_and_message

async def add_department(call: CallbackQuery, state: FSMContext):
    chat_id, message_id = await call_chat_and_message(call)
    await state_check(state)
    await Department.first()
    text = f"Введіть назву закладу"
    await bot.edit_message_text(
        chat_id=chat_id,
        message_id=message_id,
        text=text,
        reply_markup=kb.cancel_add_depart,
    )

async def depart_name(message: Message, state: FSMContext):
    name = message.text
    name = name.strip()
    depart_data = await state.get_data()
    if "name" in depart_data:
        await state.update_data(name=name)
        depart_data = await state.get_data()
        await Department.waiting_for_confirm.set()
        timetable = depart_data["timetable"]
        week = await string_week(timetable)
        text = await string_confirm(depart_data, week)
        await message.answer(text, reply_markup=kb.confinm_depart)
    else:
        await state.update_data(name=name)
        await Department.next()
        text = f"Введіть область в якій знаходиться заклад"
        await message.answer(text)

async def depart_region(message: Message, state: FSMContext):
    region = message.text
    region = region.strip()
    depart_data = await state.get_data()
    if "region" in depart_data:
        await state.update_data(region=region)
        depart_data = await state.get_data()
        await Department.waiting_for_confirm.set()
        timetable = depart_data["timetable"]
        week = await string_week(timetable)
        text = await string_confirm(depart_data, week)
        await message.answer(text, reply_markup=kb.confinm_depart)
    else:
        await state.update_data(region=region)
        await Department.next()
        text = f"Введіть місто у якому знаходиться заклад"
        await message.answer(text)

async def depart_city(message: Message, state: FSMContext):
    city = message.text
    city = city.strip()
    depart_data = await state.get_data()
    if "city" in depart_data:
        await state.update_data(city=city)
        depart_data = await state.get_data()
        await Department.waiting_for_confirm.set()
        timetable = depart_data["timetable"]
        week = await string_week(timetable)
        text = await string_confirm(depart_data, week)
        await message.answer(text, reply_markup=kb.confinm_depart)
    else:
        await state.update_data(city=city)
        await Department.next()
        text = "Тепер введіть адресу закладу"
        await message.answer(text)

async def depart_address(message: Message, state: FSMContext):
    address = message.text
    address = address.strip()
    depart_data = await state.get_data()
    if "address" in depart_data:
        await state.update_data(address=address)
        depart_data = await state.get_data()
        await Department.waiting_for_confirm.set()
        timetable = depart_data["timetable"]
        week = await string_week(timetable)
        text = await string_confirm(depart_data, week)
        await message.answer(text, reply_markup=kb.confinm_depart)
    else:
        await state.update_data(address=address)
        await Department.next()
        text = "Тепер введіть телефон/и закладу"
        await message.answer(text)

async def depart_phone(message: Message, state: FSMContext):
    phone = message.text
    phone = phone.strip()
    depart_data = await state.get_data()
    if "phone" in depart_data:
        await state.update_data(phone=phone)
        depart_data = await state.get_data()
        await Department.waiting_for_confirm.set()
        timetable = depart_data["timetable"]
        week = await string_week(timetable)
        text = await string_confirm(depart_data, week)
        await message.answer(text, reply_markup=kb.confinm_depart)
    else:
        await state.update_data(phone=phone)
        await Department.next()
        text = "Тепер введіть ID адміністратора закладу"
        await message.answer(text)

async def depart_admin(message: Message, state: FSMContext):
    admin = message.text
    admin = int(admin)
    depart_data = await state.get_data()
    if "admin" in depart_data:
        await state.update_data(admin=admin)
        depart_data = await state.get_data()
        await Department.waiting_for_confirm.set()
        timetable = depart_data["timetable"]
        week = await string_week(timetable)
        text = await string_confirm(depart_data, week)
        await message.answer(text, reply_markup=kb.confinm_depart)
    else:
        await state.update_data(admin=admin)
        await Department.next()
        text = (
            "Тепер введіть розклад закладу у порядку\n"
            "від понеділка до неділі наприклад:\n"
            "8:00-22:00|8:00-22:00|8:00-22:00|8:00-22:00|8:00-22:00|10:00-20:00|вихідний"
        )
        await message.answer(text)

async def depart_timetable(message: Message, state: FSMContext):
    try:
        timetable = message.text
        (
            monday,
            tuesday,
            wednesday,
            thursday,
            friday,
            saturday,
            sunday,
        ) = timetable.split("|")
        monday = monday.strip()
        tuesday = tuesday.strip()
        wednesday = wednesday.strip()
        thursday = thursday.strip()
        friday = friday.strip()
        friday = friday.strip()
        saturday = saturday.strip()
        sunday = sunday.strip()
        timetable = {
            "Понеділок": monday,
            "Вівторок": tuesday,
            "Середа": wednesday,
            "Четвер": thursday,
            "Пятниця": friday,
            "Субота": saturday,
            "Неділя": sunday,
        }
        depart_data = await state.get_data()
        if "timetable" in depart_data:
            await state.update_data(timetable=timetable)
            depart_data = await state.get_data()
            await Department.waiting_for_confirm.set()
        else:
            await state.update_data(timetable=timetable)
            depart_data = await state.get_data()
            await Department.next()
        week = await string_week(timetable)
        text = await string_confirm(depart_data, week)
        await message.answer(text, reply_markup=kb.confinm_depart)
    except:
        await message.answer(
            "Введіть розклад закладу у порядку\n"
            "від понеділка до неділі наприклад:\n"
            "8:00-22:00|8:00-22:00|8:00-22:00|8:00-22:00|8:00-22:00|10:00-20:00|вихідний"
        )
        return

async def confirm_department(call: CallbackQuery, state: FSMContext):
    chat_id, message_id = await call_chat_and_message(call)
    if call["data"] == "depart_confirm:Yes":
        depart_data = await state.get_data()
        name = depart_data["name"]
        region = depart_data["region"]
        city = depart_data["city"]
        address = depart_data["address"]
        phone = depart_data["phone"]
        admin = depart_data["admin"]
        timetable = depart_data["timetable"]
        dp.loop.create_task(depart_db.add_department(
            name, region, city, address, phone, timetable, admin
        ))
        edit_depart = InlineKeyboardMarkup()
        edit_depart.add(back("depart_list"))
        await call.message.edit_reply_markup(reply_markup=None)
        await call.message.answer(
            f'Заклад "{name}" було додано', reply_markup=edit_depart
        )
        await state_check(state)

    elif call["data"] == "depart_confirm:No":
        text = "Оберіть що треба редагувати:"
        await bot.edit_message_text(
            chat_id=chat_id,
            message_id=message_id,
            text=text,
            reply_markup=kb.add_edit_depart,
        )

    elif call["data"] == "depart_add_edit:name":
        await Department.waiting_for_name.set()
        text = "Введіть нову назву закладу"
        await bot.edit_message_text(chat_id=chat_id, message_id=message_id, text=text)

    elif call["data"] == "depart_add_edit:region":
        await Department.waiting_for_region.set()
        text = "Введіть нову область закладу"
        await bot.edit_message_text(chat_id=chat_id, message_id=message_id, text=text)

    elif call["data"] == "depart_add_edit:city":
        await Department.waiting_for_city.set()
        text = "Введіть нове місто закладу"
        await bot.edit_message_text(chat_id=chat_id, message_id=message_id, text=text)

    elif call["data"] == "depart_add_edit:address":
        await Department.waiting_for_address.set()
        text = "Введіть новий адрес закладу"
        await bot.edit_message_text(chat_id=chat_id, message_id=message_id, text=text)

    elif call["data"] == "depart_add_edit:phone":
        await Department.waiting_for_phone.set()
        text = "Введіть новий телефон закладу"
        await bot.edit_message_text(chat_id=chat_id, message_id=message_id, text=text)

    elif call["data"] == "depart_add_edit:timetable":
        await Department.waiting_for_timetable.set()
        text = "Введіть новий розклад закладу"
        await bot.edit_message_text(chat_id=chat_id, message_id=message_id, text=text)

async def cancel(call: CallbackQuery, state: FSMContext):
    chat_id, message_id = await call_chat_and_message(call)
    text = "Відкинути всі зміни?"
    await bot.edit_message_text(
        chat_id=chat_id,
        message_id=message_id,
        text=text,
        reply_markup=kb.confirm_or_fail_cancel,
    )

async def fail_cancel(call: CallbackQuery, state: FSMContext):
    chat_id, message_id = await call_chat_and_message(call)
    my_state = await state.get_state()
    if my_state == "Department:waiting_for_name":
        text = "Введіть назву закладу"
        await bot.edit_message_text(
            chat_id=chat_id, message_id=message_id, text=text, reply_markup=None
        )
    elif my_state == "Department:waiting_for_confirm":
        depart_data = await state.get_data()
        timetable = depart_data["timetable"]
        week = await string_week(timetable)
        text = await string_confirm(depart_data, week)
        await bot.edit_message_text(
            chat_id=chat_id,
            message_id=message_id,
            text=text,
            reply_markup=kb.confinm_depart,
        )

def register_handlers_add_department(dp: Dispatcher):
    dp.register_callback_query_handler(
        add_department, cd.depart_menu_callback.filter(value=["add"]), state="*"
    )
    dp.register_message_handler(depart_name, state=Department.waiting_for_name)
    dp.register_message_handler(depart_region, state=Department.waiting_for_region)
    dp.register_message_handler(depart_city, state=Department.waiting_for_city)
    dp.register_message_handler(depart_address, state=Department.waiting_for_address)
    dp.register_message_handler(depart_phone, state=Department.waiting_for_phone)
    dp.register_message_handler(depart_admin, state=Department.waiting_for_admin)
    dp.register_message_handler(
        depart_timetable, state=Department.waiting_for_timetable
    )
    dp.register_callback_query_handler(
        confirm_department,
        cd.depart_confirm_callback.filter(value=["Yes", "No"]),
        state=Department.waiting_for_confirm,
    )
    dp.register_callback_query_handler(
        confirm_department,
        cd.depart_add_edit_callback.filter(
            field=["name", "region", "city", "address", "phone", "timetable"]
        ),
        state=Department.waiting_for_confirm,
    )
    dp.register_callback_query_handler(
        cancel, cd.depart_confirm_callback.filter(value=["Cancel"]), state="*"
    )
    dp.register_callback_query_handler(
        fail_cancel, cd.depart_confirm_callback.filter(value=["Not"]), state="*"
    )


import os
import random
import shutil

import pytest
from helpers import copy_sample_files, random_string
from iiif_pipeline.derivatives import create_jp2
from iiif_pipeline.helpers import matching_files

FIXTURES_FILEPATH = os.path.join("fixtures", "tif")
SOURCES = [os.path.join("/", "source"),
           os.path.join("/", "source folder")]
DERIVATIVES = [os.path.join("/", "derivatives"),
               os.path.join("/", "derivatives folder")]
UUIDS = [random_string() for x in range(random.randint(2, 3))]
PAGE_COUNT = random.randint(1, 5)

def setup():
    """Sets up source directory for tests."""
    for SOURCE_DIR, DERIVATIVE_DIR in zip(SOURCES, DERIVATIVES):
        for d in [SOURCE_DIR, DERIVATIVE_DIR]:
            if os.path.isdir(d):
                shutil.rmtree(d)
        shutil.copytree(FIXTURES_FILEPATH, SOURCE_DIR)
        copy_sample_files(SOURCE_DIR, UUIDS, PAGE_COUNT, "tif")
        os.makedirs(DERIVATIVE_DIR)

def test_create_jp2():
    """Ensure the run method produces the expected number of files."""
    for SOURCE_DIR, DERIVATIVE_DIR in zip(SOURCES, DERIVATIVES):
        uuid = random.choice(UUIDS)
        tiff_files = matching_files(
            SOURCE_DIR,
            prefix=uuid,
            skip=False,
            prepend=True)
        create_jp2(tiff_files, uuid, DERIVATIVE_DIR)
        assert len(os.listdir(DERIVATIVE_DIR)) == PAGE_COUNT
        for f in os.listdir(DERIVATIVE_DIR):
            assert ".tif" not in f

def test_replace_jp2():
    """Ensure replacing of files is handled correctly.

    If files exist and the replace parameter is False, a FileExistsError should
    be raised.
    """
    for SOURCE_DIR, DERIVATIVE_DIR in zip(SOURCES, DERIVATIVES):
        uuid = random.choice(UUIDS)
        tiff_files = matching_files(
            SOURCE_DIR,
            prefix=uuid,
            skip=False,
            prepend=True)
        create_jp2(tiff_files, uuid, DERIVATIVE_DIR)
        with pytest.raises(FileExistsError):
            create_jp2(tiff_files, uuid, DERIVATIVE_DIR)
        create_jp2(tiff_files, uuid, DERIVATIVE_DIR, replace=True)

def teardown():
    """Removes test directories."""
    for SOURCE_DIR, DERIVATIVE_DIR in zip(SOURCES, DERIVATIVES):
        for d in [SOURCE_DIR, DERIVATIVE_DIR]:
            shutil.rmtree(d)


def triangle(row):
    colors = {"R":0, "G":1, "B":2, 0:"R", 1:"G", 2:"B"}
    def merge(a, b): return colors[(-colors[a]-colors[b])%3] # returns the result color by using modulo
    if len(row)==1: return row
    section=[3**i+1 for i in range(10,-1, -1)] # 3**n+1 thanks to the video, descending order
    for length in section: # try each section from bigger to smaller
        while len(row)>=length: # if row is bigger than section, process
            if len(row)==length: return merge(row[0], row[-1])
            row = [merge(row[i], row[i+length-1]) for i in range(len(row)-length+1)]
    return row[0]


import requests

# SpotifyClient is the class that interacts with the Spotify API endpoint to retrieve the
# data we need to make the necessary predictions
class SpotifyClient(object):
    def __init__(self, api_key, song_id, features):
        '''Initializing the class variables for the instance
        '''
        self.api_key = api_key
        self.song_id = song_id
        self.features = features

    def get_track_features(self):
        '''Method that will retrieve the audio features of the track and retuen it in a list.
        '''

        # Formatting the URL of the API endpoint we want to interact with
        url = f'https://api.spotify.com/v1/audio-features/{self.song_id}'

        # Making a request to the URL and recieving the response
        response = requests.get(
            url,
            headers={
                "Content-Type": "application/json",
                "Authorization": f'Bearer {self.api_key}'
            }
        )

        # Converting the response into a python dict
        response_json = response.json()

        value_list = []

        # Adding each of the features to the list
        for feature in self.features:
            try:
                value_list.append(response_json[feature])
            except:
                # Handling error where the access token is expired
                if 'error' in response_json.keys():
                    return None
                value_list.append(None)

        return value_list

    def get_track_analysis(self):
        '''Method that will retrieve the audio analysis and extract the necessary data
        '''

        # Formatting the URL of the API endpoint we want to interact with
        url = f'https://api.spotify.com/v1/audio-analysis/{self.song_id}'

        # Making a request to the URL and recieving the response
        response = requests.get(
            url,
            headers={
                "Content-Type": "application/json",
                "Authorization": f'Bearer {self.api_key}'
            }
        )

        # Converting the response into a python dict
        response_json = response.json()

        try:
            sections = response_json['sections']
            section = sections[2]
            chorus_hit = section['start']
        except:
            # Handling error where the access token is expired
            if 'error' in response_json.keys():
                return None
            sections = []
            chorus_hit = None

        return [chorus_hit, len(sections)]

    def get_track_artists(self):
        '''Method that will retrieve the artist of the track
        '''

        # Formatting the URL of the API endpoint that we want to interact with
        url = f'https://api.spotify.com/v1/tracks/{self.song_id}'

        # Making a request to the endpoint and recieving a response
        response = requests.get(
            url,
            headers={
                "Content-Type": "application/json",
                "Authorization": f'Bearer {self.api_key}'
            }
        )

        # Converting the response to a python dict
        response_json = response.json()

        try:
            artists = response_json['artists']
            artist = artists[0]
            artist_name = artist['name']
        except:
            # Handling error where the access token has expired
            if 'error' in response_json.keys():
                return None
            artist_name = None

        return [artist_name]


import sys

from pyspark.sql import DataFrame
from pyspark.sql.types import StructType, StructField, StringType, IntegerType

from pysparkexamples import spark, PROJECT_ROOT

JSON_FILE = f"{PROJECT_ROOT}/data/emp_details.json"
URI_SCHEME = "file://"
FILE_SCHEMA = StructType([
    StructField("userId", StringType(), True),
    StructField("jobTitle", StringType(), True),
    StructField("firstName", StringType(), True),
    StructField("lastName", StringType(), True),
    StructField("employeeCode", StringType(), True),
    StructField("region", StringType(), True),
    StructField("phoneNumber", StringType(), True),
    StructField("emailAddress", StringType(), True),
    StructField("salary", IntegerType(), True)
])

def read_json(uri_scheme: str = URI_SCHEME, file_name: str = JSON_FILE) -> DataFrame:
    """
    Function used to read json file
    it will read data/emp_details.json by default

    :param uri_scheme: uri scheme e.g. file, hdfs, gcs, s3
    :param file_name: absolute path of file to read
    :return: DataFrame object
    """
    # NOTE - for reading file from local system use UNIX like path string
    print(f"Reading JSON File: {JSON_FILE}")
    input_path = f"{uri_scheme}/{file_name}" if sys.platform == "win32" else f"{uri_scheme}{file_name}"
    return spark.read.json(input_path)

def write_json(df: DataFrame, output_dir: str) -> None:
    """
    Function used to write a DataFrame to json file

    :param df: Dataframe object
    :param output_dir: relative or absolute path of target file
    :return: None
    """
    print(f"Writing DataFrame to JSON file {output_dir}")
    df.write.json(output_dir, mode="overwrite")


# IMPORTING LIBS AND MODEL
import streamlit as st
import joblib

model = joblib.load('titanic_model_svm.sav')

# DATA INPUT
st.image('imgs/titanic.jpg')
st.title("Titanic - Prediction of survival")
name = st.text_input('Name')
last_name = st.text_input('Last Name')
title = st.selectbox('Title', ('Mrs', 'Miss', 'Master', 'Mr', 'Other'))
sex = st.radio("Sex", ('Male', 'Female'))
age = st.slider('Age', 0.0, 130.0, 25.0)
embarked = st.selectbox('Embarked in', ('Cherbourg', 'Queenstown', 'Southampton'))
psg_class = st.selectbox('Class', (1,2,3))
cabin = st.text_input('Cabin')
ticket = st.text_input('Ticket')
fare = st.slider('Fare', 0.0, 513.0, 15.0)
sibsp = st.number_input("Number of siblings/spouses aboard the Titanic", value=0, min_value=0, step=1)
parch = st.number_input("Number of parents/children aboard the Titanic", value=0, min_value=0, step=1)

# DATA TREATMENT
sex = 1 if sex == "Male" else 0
embarked = 0 if embarked == "Cherbourg" else 2 if embarked == "Southampton" else 1
isAlone = 1 if (sibsp + parch) == 0 else 0

if title == "Mr":
    title = 2
elif title == "Mrs":
    title = 3
elif title == "Miss":
    title = 1
elif title == "Master":
    title = 0
else:
    title = 4

if fare > 31.0:
    fare = 3
elif fare > 14.454:
    fare = 2
elif fare > 7.91:
    fare = 1
else:
    fare = 0

if age > 64.0:
    age = 4
elif age > 48.0:
    age = 3
elif age > 32.0:
    age = 2
elif age > 16.0:
    age = 1
else:
    age = 0

# PREDICTION
btn_predict = st.button("Predict")
if btn_predict:
    result = model.predict([[psg_class,sex,embarked,age,fare,title,isAlone]])
    st.subheader("The passenger is... ")
    result = "Survived" if result == 1 else "Died"
    st.write(result)


import pandas as pd
import matplotlib.pyplot as plt

def convert():
    pass

# Part 1
df = pd.read_csv('datasets/flights.csv'
                    , parse_dates={'Date':["Year","Month","DayofMonth"]}
                    , dtype={'UniqueCarrier':'category','Origin':'category','Dest':'category','DepTime':'str', 'ArrTime':'str','FlightNum':'str'})
df['DayOfWeek'] -= 1

print(df.head())

print(df.dtypes)
print(df.shape)

#part 2
print(df[(df['Date'].dt.month==1)&(df['Date'].dt.day==1)].shape)

#Part 3
print(df[(df['UniqueCarrier']=='UA')|(df['UniqueCarrier']=='AA')].shape)

#Part 4
delayed_df = df[(df['DepDelay']>60)]
print(round(delayed_df.shape[0]/df.shape[0]*100,2))

#Part 5
df['Speed'] = df['Distance']/df['AirTime']*60
print(df.head())

# Part 6
avg_delay_per_dest = df.groupby('Dest')['ArrDelay'].mean()
# print(avg_delay_per_dest)

# Part 7
print(avg_delay_per_dest.sort_values()[:6])
avg_delay_per_dest.plot(kind='bar')
# plt.show()

#Part 8
df = df.sort_values(by=['Date'])
counts = df.groupby('Date')['Date'].count().to_frame('FlightCount').reset_index()
df = df.merge(counts, on='Date')
print(df.head())

# Part 9
print(df.sort_values(by=['FlightCount','Date'],ascending=False)[:6])

# Part 10
df['cancel_and_div'] = df['Cancelled']+df['Diverted']
total_cancel_div = df.groupby(['UniqueCarrier'])['cancel_and_div'].sum().to_frame('Number')

dato_fc = df[['Date','FlightCount','UniqueCarrier']].drop_duplicates()

airlines_counts = dato_fc.groupby('UniqueCarrier').sum()

print(airlines_counts)
print(total_cancel_div)
final_df = airlines_counts.merge(total_cancel_div, left_index=True, right_index=True)
final_df['pct'] = final_df['Number']/final_df['FlightCount']*100
print(final_df)


import poloniex
import time
import datetime
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

def main():
    date, data = getDataPoloniex()
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(date, data)
    ax.set_title("BTC Price")
    ax.set_xlabel("Day")
    ax.set_ylabel("BTC Price[$]")
    plt.grid(fig)
    plt.show(fig)

def getDataPoloniex():
    polo = poloniex.Poloniex()
    polo.timeout = 2
    chartETH_BTC = polo.returnChartData('BTC_ETH', period=300, start=time.time() - 300 * 500, end=time.time())

    row_list = []
    for i in range(len(chartETH_BTC)):
        # date = datetime.datetime.fromtimestamp(chartETH_BTC[i]['date']).date()
        item = chartETH_BTC[i]
        dict = [ str(item['date']),  str(item['open']), str(item['high']),  str(item['low']), str(item['close']), str(item['volume'])]
        row_list.append(dict)
    print(row_list)
    df = pd.DataFrame(row_list)
    df.to_csv('polo.csv', index=False, header=False)

    # tmpDate = [chartETH_BTC[i]['date'] for i in range(len(chartETH_BTC))]
    # date = [datetime.datetime.fromtimestamp(tmpDate[i]).date() for i in range(len(tmpDate))]
    # data = [float(chartETH_BTC[i]['open']) for i in range(len(chartETH_BTC))]
    return

if __name__ == "__main__":
    getDataPoloniex()


import streamlit as st
import pandas as pd
from PIL import Image

st.set_page_config(layout='wide')

logo = Image.open('avengerslogo.png')
st.image(logo, width = 150)

df = pd.read_csv("charcters_stats.csv")

avengers = df[(df.Name == "Iron Man") | (df.Name == "Captain America") | (df.Name == "Spider-Man") | (df.Name == "Hulk") | (df.Name == "War Machine") | (df.Name == "Vision") | (df.Name == "Black Panther") | (df.Name == "Ant-Man")].reset_index(drop=True)

avenger_options = avengers['Name'].unique().tolist()
avenger = st.selectbox('Avengers', avenger_options,0)
row = avengers[avengers['Name']==avenger]

antMan = Image.open('antman.jpg')
blackPanther = Image.open('blackpanther.jpg')
captainAmerica = Image.open('captainamerica.jpg')
hulk = Image.open('hulk.jpg')
ironMan = Image.open('ironman.jpg')
spiderMan = Image.open('spiderman.jpg')
vision = Image.open('vision.jpg')
warMachine = Image.open('warmachine.jpg')

col1,col2,col3 = st.columns(3)
col2.metric('Stength',row.iloc[0]['Strength'])
col2.metric('Speed', row.iloc[0]['Speed'])
col3.metric('Power', row.iloc[0]['Power'])
col2.metric('Intelligence', row.iloc[0]['Intelligence'])
col3.metric('Durability', row.iloc[0]['Durability'])
col3.metric('Combat', row.iloc[0]['Combat'])
st.metric('Rating', row.iloc[:,2:7].mean(axis=1))
#put scatter plot here

if avenger == "Ant-Man":
    col1.image(antMan)
elif avenger == 'Black Panther':
    col1.image(blackPanther)
elif avenger == 'Captain America':
    col1.image(captainAmerica)
elif avenger == 'Hulk':
    col1.image(hulk)
elif avenger == 'Iron Man':
    col1.image(ironMan)
elif avenger == 'Spider-Man':
    col1.image(spiderMan)
elif avenger == 'Vision':
    col1.image(vision)
elif avenger == 'War Machine':
    col1.image(warMachine)