# import chat_bot
# if __name__ == "__main__":
#     chat_bot.do_something()

# import chat_bot, calculator
# if __name__== "__main__":
#     userInput = int(input("""
#     Enter: 1 for Calculator
#            2 for Chatbot
#     """))
#     if userInput == 1:
#         calculator.calculate_something()
#     else:
#         chat_bot.do_something()

import math
from functions import exercise

# if __name__== "__main__":
#     print(exercise.max_of_three_numbers())

# if __name__== "__main__":
#    print(exercise.sum_of_numbers_in_a_list())

# if __name__== "__main__":
#    exercise.multiply_numbers_in_a_list()

# if __name__== "__main__":
#    (exercise.reverse_string())

# if __name__== "__main__":
#    exercise.calculate_factorial_of_a_number()

# if __name__ == "__main__":
#     exercise.find_number_in_range()

# if __name__ == "__main__":
#     exercise.calculate_case_of_letter("i am a BOY")

# if __name__ == "__main__":
#     exercise.sum_of_numbers_in_a_list([2,3,4])

if __name__== "__main__":
    while True:
        prompt=int (input("Enter from numbers 1 to 10: "))
        if prompt==1:
            exercise.max_of_three_numbers()
        elif prompt==2:
            exercise.sum_of_numbers_in_a_list()
        elif prompt==3:
            exercise.multiply_numbers_in_a_list()
        elif prompt==4:
            exercise.reverse_string()
        elif prompt==5:
            exercise.calculate_factorial_of_a_number()
        elif prompt==6:
            exercise.find_number_in_range()
        elif prompt==7:
            exercise.calculate_case_of_letter()
        elif prompt==8:
            exercise.unique_list()
        elif prompt==9:
            print(exercise.check_ifprime())
        elif prompt==10:
            (exercise.is_even_list())
        else:
            print("invalid number")


import FWCore.ParameterSet.Config as cms

#from DQMServices.Core.DQM_cfg import *
from DQMServices.Core.DQMStore_cfi import *

DQM = cms.Service("DQM",
                  debug = cms.untracked.bool(False),
                  publishFrequency = cms.untracked.double(5.0),
                  collectorPort = cms.untracked.int32(9090),
                  collectorHost = cms.untracked.string('dqm-prod-local.cms'),
                  filter = cms.untracked.string('')
                  )

from DQMServices.Components.DQMEnvironment_cfi import *

dqmSaver.convention = 'Online'
dqmSaver.referenceHandling = 'all'
dqmSaver.dirName = '/home/dqmprolocal/output'
dqmSaver.producer = 'DQM'
dqmSaver.saveByTime = 1
dqmSaver.saveByLumiSection = -1
dqmSaver.saveByMinute = 8
dqmSaver.saveByRun = 1
dqmSaver.saveAtJobEnd = False

#RunType, and Runkey selection from RCMS
import sys
from FWCore.ParameterSet.VarParsing import VarParsing
from dqmPythonTypes import *

runParameters = VarParsing ('analysis')
runParameters.register ('runtype',
  'pp_run',
  VarParsing.multiplicity.singleton,
  VarParsing.varType.string,
  "Type of Run in CMS")

runParameters.register ('runkey',
  'pp_run',
  VarParsing.multiplicity.singleton,
  VarParsing.varType.string,
  "Run Keys of CMS")

# Fix to allow scram to compile
#if len(sys.argv) > 1:
#  runParameters.parseArguments()

runType = RunType(['pp_run','cosmic_run','hi_run','hpu_run'])
if not runParameters.runkey.strip():
  runParameters.runkey = 'pp_run'

runType.setRunType(runParameters.runkey.strip())


import matplotlib.pyplot as plt

def plot(batch_size, model_type, hidden_dimensions):
    losses_save_path = f"data/{model_type}_{'-'.join([str(x) for x in hidden_dimensions])}_batch-{batch_size}_losses.csv"
    train_losses = []
    test_losses = []
    with open(losses_save_path, "r") as file:
        for line in file.readlines()[1:]:
            train_losses += [[float(string) for string in line.split(",")][:3]]
            test_losses += [[float(string) for string in line.split(",")][3:]]

    # plt.plot([x for x, _, _ in train_losses], label="train_total_loss")
    # plt.plot([x for x, _, _ in test_losses], label="test_total_loss")
    plt.plot([x for _, x, _ in train_losses], label="Train MSE loss")
    plt.plot([x for _, x, _ in test_losses], label="Test MSE loss")
    if model_type == "vae":
        plt.plot([x for _, _, x in train_losses], label="Train KLD loss", linestyle="dashed")
        plt.plot([x for _, _, x in test_losses], label="Test KLD loss", linestyle="dashed")
    if model_type == "simple":
        name = "Autoencoder"
    if model_type == "vae":
        name = "Variational autoencoder"
    plt.title(f"{name}, layers {hidden_dimensions}, batch_size {batch_size}")
    plt.xlabel("epochs")
    plt.legend(loc='upper left')
    plt.ylim(0, 37)
    plt.savefig(f"data/{model_type}_{'-'.join([str(x) for x in hidden_dimensions])}_batch-{batch_size}_losses.png")
    plt.close()

model_type = "simple"
hidden_dimensions = [1024, 64]
batch_size = 128
plot(batch_size, model_type, hidden_dimensions)

model_type = "simple"
hidden_dimensions = [256, 256, 256, 256, 4]
batch_size = 8
plot(batch_size, model_type, hidden_dimensions)

model_type = "vae"
hidden_dimensions = [256, 256, 256, 256, 8]
batch_size = 8
plot(batch_size, model_type, hidden_dimensions)


# -*- coding: utf-8 -*-
'''
Logistic Regression
'''
import numpy as np

# 使用梯度下降法
def LR_GD(X, Y, lam=0):
    print("Gradient Descent lam=" + str(lam) + "...")
    n, m = X.shape[0], X.shape[1]
    # 初始化
    W = np.ones((m, 1))
    # 步长 正则
    alpha = 0.5
    # 最大迭代次数
    iterations = 0
    while iterations < 500:
        # 估计类别和实际类别误差
        E = Y - sigmoid(np.dot(X, W))
        # 梯度
        gradient = np.dot(X.T, E)
        # 梯度上升
        W = W + alpha / n * gradient + lam / n * W
        iterations += 1
        if not iterations % 100:
            error = 1 / n * np.sqrt(np.linalg.norm(E, 2))
            print(iterations, error)
    return W

# 牛顿法
# 　在似然函数前加负号, 则求似然函数最小化
def LR_Newtons(X, Y, lam=0):
    print("Newtons Method lam=" + str(lam) + "...")
    # 获取维度信息
    n, m = X.shape[0], X.shape[1]
    # 初始化
    W = np.zeros((m, 1))
    # 正则项系数
    # 最大迭代次数
    iterations = 0
    while iterations < 500:
        # logistic function
        logistic = sigmoid(np.dot(X, W))
        # 一阶导
        first_der = -np.dot(X.T, Y - logistic)
        # 获得对角线为 p(1-p)的对角矩阵
        A = np.dot(logistic, 1 - logistic.T)
        A = np.multiply(np.eye(A.shape[0]), A)
        # 　海森矩阵　X'AX
        hession = np.dot(np.dot(X.T, A), X)
        # 更新
        W = W - 1 / n * np.dot(np.linalg.inv(hession), first_der) + lam / n * W
        iterations += 1
        if not iterations % 100:
            error = 1 / n * np.sqrt(np.linalg.norm(Y - logistic, 2))
            print(iterations, error)
    return W

# logistic function
def sigmoid(XW):
    return 1.0 / (1 + np.exp(-XW))

# softmax 多分类逻辑回归
# dataset Mnist Iris
# n个样本, m个属性(包括补齐的1), k个类别
def multi_LR(X, Y):
    n, m, k = X.shape[0], X.shape[1], Y.shape[1]
    # 初始化权重 (m, k)
    W = np.ones((m, k))
    # 步长
    alpha = 0.5
    lam = 0.00001
    # 迭代次数
    iterations = 0
    error = 1
    while iterations < 300:
        # softmax, 求每个样本分别属于每个类别的概率
        softmax_ = softmax(np.dot(X, W))
        # 梯度
        gradient = np.dot(X.T, (softmax_ - Y))
        # 更新W, 除以n归一化, lam正则化
        W = W - alpha / n * gradient + lam / n * W
        # 预测结果与实际结果的差值
        iterations += 1
        if not iterations % 50:
            error = 1 / n * np.sqrt(np.linalg.norm(softmax_ - Y, 2))
            print(iterations, error)

    return W

'''
@:param
    样本数n, 特征数m, 类别数k
    X (n, m)
    W (m, k)
    XW (n, k),
@:return
    (n, k), 每个样本属于每一类的概率

'''

def softmax(XW):
    # 　注意对数据进行归一化, 避免指数溢出
    prob = np.exp(XW)
    # 求每个样本属于各个类别的概率
    # 横向求和并扩展
    sum_prob = np.sum(prob, axis=1).reshape((prob.shape[0], 1)).repeat(XW.shape[1], axis=1)
    return prob / sum_prob


from proteus import Domain, Context
from proteus.mprans import SpatialTools as st
from proteus import Gauges as ga
from proteus import WaveTools as wt
from math import *
import numpy as np
from proteus.mprans import BodyDynamics as bd

opts=Context.Options([
    # predefined test cases
    ("water_level", 0.325, "Height of free surface above bottom"),
    # Geometry
    ('Lgen', 1.0, 'Genaration zone in terms of wave lengths'),
    ('Labs', 1.0, 'Absorption zone in terms of wave lengths'),
    ('Ls', 1.0, 'Length of domain from genZone to the front toe of rubble mound in terms of wave lengths'),
    ('Lend', 1.0, 'Length of domain from absZone to the back toe of rubble mound in terms of wave lengths'),
    # waves
    ('wave', True, 'Enable wave generation'),
    ('waveType', 'Fenton', 'Wavetype for regular waves, Linear or Fenton'),
    ("wave_period", 1.30, "Period of the waves"),
    ("wave_height", 0.167, "Height of the waves"),
    ('wavelength', 2.121, 'Wavelength only if Fenton is activated'),
    ('Ycoeff', [0.21107604, 0.07318902, 0.02782228, 0.01234846, 0.00618291, 0.00346483, 0.00227917, 0.00194241], 'Ycoeff only if Fenton is activated'),
    ('Bcoeff', [0.23112932, 0.03504843, 0.00431442, 0.00036993, 0.00004245, 0.00001877, 0.00000776, 0.00000196], 'Bcoeff only if Fenton is activated'),
    ('Nf', 8 ,'Number of frequency components for fenton waves'),
    ('meanVelocity', [ 0., 0., 0.],'Velocity used for currents'),
    ('phi0', 0.0 ,'Initial phase for waves'),
    ('Uwind', [0.0, 0.0, 0.0], 'Set air velocity'),
    ('fast', True ,'Switches ON fast cosh approximation'),
    # rubble mound
    ('porousMedia', True, 'Enable porus media region'),
    ("hs", 0.175, "Height of the breakwater"),
    ("slope1", 1./3., "Slope1 of the breakwater"),
    ("slope2", 1./2., "Slope2 of the breakwater"),
    ('porosity', 0.4, "Porosity of the medium"),
    ('d50', 0.030, "Mean diameter of the medium"),
    ('d15', None, "15% grading curve diameter of the medium"),
    ('Resistance', 'Shih', 'Ergun or Engelund or Shih'),
    # soil foundation
    ("springs", True, "Switch on/off soil module"),
    ("Kx", 541553.2, "Horizontal stiffness in Pa"),
    ("Ky", 582633.7, "Vertical stiffness in Pa"),
    ("Krot", 16246.6, "Rotational stiffness in N"),
    ("Cx", 1694.2, "Damping factor in Pa s "),
    ("Cy", 1757.32, "Damping factor in Pa s "),
    ("Crot", 69.61, "Rotational damping factor in N s "),
    # caisson
    ("caisson2D", True, "Switch on/off caisson2D"),
    ('dimx', 0.300, 'X-dimension of the caisson2D'),
    ('dimy', 0.385, 'Y-dimension of the caisson2D'),
    ('width', 1.0, 'Z-dimension of the caisson2D'),
    ('mass', 64.8/0.4, 'Mass of the caisson2D [kg]'),
    ('caissonBC', 'FreeSlip', 'caisson2D boundaries: NoSlip or FreeSlip'),
    ("rotation", False, "Initial position for free oscillation"),
    ("friction", True, "Switch on/off friction module for sliding"),
    ("overturning", True, "Switch on/off overturning module"),
    ("m_static", 0.500, "Static friction factor between caisson2D and rubble mound"),
    ("m_dynamic", 0.500, "Dynamic friction factor between caisson2D and rubble mound"),
    ('scheme', 'Runge_Kutta', 'Numerical scheme applied to solve motion calculation (Runge_Kutta or Central_Difference)'),
    # numerical options
    ("GenZone", True, 'Turn on generation zone at left side'),
    ("AbsZone", True, 'Turn on absorption zone at right side'),
    ("refinement_level", 0.0,"he=walength/refinement_level"),
    ("he", 0.05,"he=walength/refinement_level"),
    ("cfl", 0.450 ,"Target cfl"),
    ("duration", 20., "Durarion of the simulation"),
    ("freezeLevelSet", True, "No motion to the levelset"),
    ("useVF", 1.0, "For density and viscosity smoothing"),
    ('movingDomain', True, "Moving domain and mesh option"),
    ('conservativeFlux', True,'Fix post-processing velocity bug for porous interface'),
    ])

# ----- DOMAIN ----- #

domain = Domain.PlanarStraightLineGraphDomain()

# ----- WAVE CONDITIONS ----- #
period=opts.wave_period

omega=2*np.pi/opts.wave_period

waterLevel=opts.water_level

waveDir=np.array([1, 0., 0.])
mwl=waterLevel #coordinate of the initial mean level of water surface

waveHeight=opts.wave_height

inflowHeightMean=waterLevel
inflowVelocityMean =np.array([0.,0.,0.])
windVelocity = np.array([0.,0.,0.])

# ----- Phisical constants ----- #

rho_0=998.2
nu_0 =1.004e-6
rho_1=1.205
nu_1 =1.500e-5
sigma_01=0.0
g =np.array([0.,-9.8,0.])
gAbs=sqrt(sum(g**2))

# ----- WAVE input ----- #

if opts.wave == True:
    waveinput = wt.MonochromaticWaves(period=period,
                                  waveHeight=waveHeight,
                                  mwl=mwl,
                                  depth=waterLevel,
                                  g=g,
                                  waveDir=waveDir,
                                  wavelength=opts.wavelength,       # used by fenton waves
                                  waveType=opts.waveType,
                                  Ycoeff=np.array(opts.Ycoeff),     # used by fenton waves
                                  Bcoeff=np.array(opts.Bcoeff),     # used by fenton waves
                                  Nf=opts.Nf,                       # used by fenton waves
                                  meanVelocity = np.array(opts.meanVelocity),
                                  phi0 = opts.phi0,
                                  fast = opts.fast,
                                      )

#---------Domain Dimension

nd = 2
wl = waveinput.wavelength

#---------MESH SIZE
if opts.he == 0.0:
    he = wl/opts.refinement_level
else:
    he = opts.he

####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
# ----- SHAPES ----- #
####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################

if opts.caisson2D:
    L_leftSpo  = opts.Lgen*wl
    L_rightSpo = opts.Labs*wl

    hs=opts.hs
    slope1=opts.slope1
    slope2=opts.slope2

#-caisson2D
    dimx=opts.dimx
    dimy=opts.dimy
    b=dimx

#-Tank
    x1=L_leftSpo
    x2=x1+opts.Ls*wl
    x3=x2+(hs/slope1)

    xc1=x3+0.20
    xc2=xc1+b
    yc1=yc2=hs

    x4=xc2+0.20
    x5=x4+(hs/slope2)
    x6=x5+opts.Lend*wl
    x7=x6+L_rightSpo
    tank_dim = [x7, 1.0]

    boundaryOrientations = {'y-': np.array([0., -1.,0.]),
                            'x+': np.array([+1., 0.,0.]),
                            'y+': np.array([0., +1.,0.]),
                            'x-': np.array([-1., 0.,0.]),
                            'sponge': None,
                            'porousLayer': None,
                            'moving_porousLayer': None,
                           }
    boundaryTags = {'y-' : 1,
                    'x+' : 2,
                    'y+' : 3,
                    'x-' : 4,
                    'sponge' : 5,
                    'porousLayer' : 6,
                    'moving_porousLayer' : 7,
                       }

else:
    L_leftSpo  = opts.Lgen*wl
    L_rightSpo = opts.Labs*wl

    #-Tank
    x1=L_leftSpo
    x2=x1+opts.Ls*wl
    x3=x2+L_rightSpo

    tank_dim = [x3, 1.0]

    boundaryOrientations = {'y-': np.array([0., -1.,0.]),
                            'x+': np.array([+1., 0.,0.]),
                            'y+': np.array([0., +1.,0.]),
                            'x-': np.array([-1., 0.,0.]),
                            'sponge': None,
                           }
    boundaryTags = {'y-': 1,
                    'x+': 2,
                    'y+': 3,
                    'x-': 4,
                    'sponge': 5,
                       }

##############################################################################################################################################################################################################
# caisson2D
############################################################################################################################################################################################################

if opts.caisson2D:
    dimx=dimx
    dimy=dimy
    dim=(dimx,dimy)
    coords=[xc1+b/2., hs+dimy/2.] # For bodyDimensions and barycenter
    VCG=dim[1]/2.                 # For barycenter
    width=opts.width                     # The 3rd dimension
    mass=opts.mass #kg
    volume=float(dimx*dimy*width)
    density=float(mass/volume) #kg/m3
    I=mass*(dimx**2.+dimy**2.)/12.
    # It=(dimx**2.+dimy**2.)/12.

# --- Shape properties setup
    caisson = st.Rectangle(domain, dim=dim, coords=coords)
    caisson.vertices[0][0]=xc1
    caisson.vertices[0][1]=yc1
    caisson.vertices[1][0]=xc2
    caisson.vertices[1][1]=yc2

# --- Body properties setup
    caisson2D = bd.CaissonBody(shape=caisson, substeps=20)
    free_x=(0.0, 0.0, 0.0) # Translational DOFs
    free_r=(0.0, 0.0, 0.0) # Rotational DOFs
    m_static=opts.m_static # Static friction
    m_dynamic=opts.m_dynamic # Dynamic friction
    if opts.movingDomain==True:
        free_x=(1.0, 1.0, 0.0) # Translational DOFs
        if opts.overturning==True:
            free_r=(0.0, 0.0, 1.0) # Rotational DOFs
    caisson2D.setMass(mass)
    caisson2D.setConstraints(free_x=free_x, free_r=free_r)
    caisson2D.setFriction(friction=opts.friction, m_static=m_static, m_dynamic=m_dynamic,
                          tolerance=he/(float(10**6)), grainSize=opts.d50)
    overturning=opts.overturning
    caisson2D.setOverturning(overturning)
    if opts.rotation==True: # Initial position for free oscillation
        caisson2D.rotate(rotation)
    caisson2D.It= I/caisson2D.mass/width
    caisson2D.setNumericalScheme(scheme=opts.scheme)
    caisson2D.setRecordValues(filename='caisson2D', all_values=True)

##############################################################################################################################################################################################################
# Tank
#########################################################################################################################################################################################################

if opts.caisson2D==False:

    vertices=[[0.0, 0.0],#0
              [x1,  0.0],#1
              [x2, 0.0], #2
              [x3,  0.0 ],#3
              [x3,  tank_dim[1] ],#4
              [x2,  tank_dim[1] ],#5
              [x1,  tank_dim[1] ],#6
              [0.0,  tank_dim[1] ],#7
              ]

    vertexFlags=np.array([1, 1, 1, 1,
                          3, 3, 3, 3,
                         ])

    segments=[[0,1],
              [1,2],
              [2,3],
              [3,4],
              [4,5],
              [5,6],
              [6,7],
              [7,0],

              [1,6],
              [2,5],
             ]

    segmentFlags=np.array([1, 1, 1,
                           2, 3, 3, 3, 4,
                           5, 5,
                          ])

    regions = [ [ 0.90*x1 , 0.10*tank_dim[1] ],
            [ 0.90*x2 , 0.90*tank_dim[1] ],
            [ 0.95*x3 , 0.95*tank_dim[1] ] ]

    regionFlags=np.array([1, 2, 3])

else:

    vertices=[[0.0, 0.0],#0
              [x1,  0.0],#1
              [x2, 0.0], #2
              [x3,  hs ],#3
              [x4,  hs ],#4
              [x5,  0.0],#5
              [x6,  0.0],#6
              [x7,  0.0],#7
              [x7,    tank_dim[1]],#8
              [x6,    tank_dim[1]],#9
              [x1,    tank_dim[1]],#10
              [0.0,   tank_dim[1]],#11
              [xc1, yc1],#12
              [xc2, yc2],#13
              ]

    vertexFlags=np.array([1, 1, 1,
                          6, 6,
                          1, 1, 1,
                          3, 3, 3, 3,
                          7, 7,
                         ])

    segments=[[0,1],
              [1,2],
              [2,3],

              [4,5],
              [5,6],
              [6,7],
              [7,8],
              [8,9],
              [9,10],
              [10,11],
              [11,0],

              [2,5],
              [1,10],
              [6,9],
              [3,12],
              [13,4],
             ]

    segmentFlags=np.array([1, 1,
                           6, 6,
                           1, 1,
                           2, 3, 3, 3, 4,
                           1,
                           5, 5,
                           7, 7,
                          ])

    regions = [ [ 0.90*x1 , 0.10*tank_dim[1] ],
            [ 0.90*x2 , 0.90*tank_dim[1] ],
            [ xc1 , 0.50*hs ],
            [ 0.95*x7 , 0.95*tank_dim[1] ] ]

    regionFlags=np.array([1, 2, 3, 4])

tank = st.CustomShape(domain, vertices=vertices, vertexFlags=vertexFlags,
                      segments=segments, segmentFlags=segmentFlags,
                      regions=regions, regionFlags=regionFlags,
                      boundaryTags=boundaryTags, boundaryOrientations=boundaryOrientations)

##################################################################################################################################################################################################################
# POROUS MEDIA
##################################################################################################################################################################################################################

porosity=opts.porosity
voidFrac=1.0-porosity

d50=opts.d50
if d50==None:
    d15=opts.d15
else:
    d15=d50/1.2

#----- SHIH
if opts.Resistance=='Shih':
    term1=3.12*(10**-3.)
    term2=(gAbs/(nu_0**2.))**(2./3.)
    term3=(d15**2.)
    Alpha1=1684+term1*term2*term3 #Shih
    Alpha=Alpha1*nu_0*(voidFrac**2)/((porosity**3)*(d15**2))

    term1=-5.10*(10**-3.)
    term2=(gAbs/(nu_0**2.))**(1./3.)
    term3=(d15)
    Beta1=1.72+1.57*exp(term1*term2*term3) #Shih
    Beta=Beta1*voidFrac/((porosity**3)*d15)

#----- ERGUN
if opts.Resistance=='Ergun':
    Alpha1=150 #Ergun
    Beta1=1.75 #Ergun
    Alpha=Alpha1*nu_0*(voidFrac**2)/((porosity**3)*(d15**2))
    Beta=Beta1*voidFrac/((porosity**3)*d15)

#----- ENGELUND
if opts.Resistance=='Engelund':
    Alpha1=360 #Ergun
    Beta1=3.6 #Ergun
    Alpha=Alpha1*nu_0*(voidFrac**3)/((porosity**2)*(d15**2))
    Beta=Beta1*voidFrac/((porosity**3)*d15)

#Proteus scale in viscosity, so i need to divide alpha and beta by nu_0
dragAlpha=(porosity**2)*Alpha/nu_0
dragBeta=(porosity**3)*Beta/nu_0

#----- Spring setup

springs=opts.springs
Kx = opts.Kx
Ky = opts.Ky
Krot = opts.Krot
Cx = opts.Cx
Cy = opts.Cy
Crot = opts.Crot

if opts.caisson2D:
    caisson2D.setSprings(springs, Kx, Ky, Krot, Cx, Cy, Crot)

#############################################################################################################################################################################################################################################################################################################################################################################################
# ----- BOUNDARY CONDITIONS ----- #
#############################################################################################################################################################################################################################################################################################################################################################################################

if opts.caisson2D:
    # Caisson boundaries
    for bc in caisson.BC_list:
        if opts.caissonBC == 'FreeSlip':
            bc.setFreeSlip()
        if opts.caissonBC == 'NoSlip':
            bc.setNoSlip()

# Tank Boundaries
tank.BC['y+'].setAtmosphere()
tank.BC['x-'].setUnsteadyTwoPhaseVelocityInlet(wave=waveinput, vert_axis=1, smoothing=3.0*he)
tank.BC['y-'].setFreeSlip()
tank.BC['x+'].setFreeSlip()
tank.BC['sponge'].setNonMaterial()

if opts.caisson2D:
    # Porous media buondaries
    tank.BC['porousLayer'].reset()
    tank.BC['moving_porousLayer'].reset()

    # Moving Mesh Options
    if opts.movingDomain==True:
        for tb in [tank.BC['x+'], tank.BC['x-'], tank.BC['y+'], tank.BC['y-'], tank.BC['sponge'], tank.BC['porousLayer']]:
            tb.hx_dirichlet.uOfXT= lambda x, t: 0.0
            tb.hy_dirichlet.uOfXT= lambda x, t: 0.0
            tb.hz_dirichlet.uOfXT= lambda x, t: 0.0
            tb.u_stress.uOfXT=None
            tb.v_stress.uOfXT=None
            tb.w_stress.uOfXT=None
        ms=tank.BC['moving_porousLayer']
        ms.hx_dirichlet.uOfXT= None
        ms.hy_dirichlet.uOfXT= None
        ms.hz_dirichlet.uOfXT= lambda x, t: 0.0
        ms.u_stress.uOfXT=None
        ms.v_stress.uOfXT=None
        ms.w_stress.uOfXT=None

########################################################################################################################################################################################################################################################################################################################################################
# -----  GENERATION ZONE & ABSORPTION ZONE  ----- #
########################################################################################################################################################################################################################################################################################################################################################

# Waves and Generation zone
if opts.GenZone and opts.wave:
    tank.setGenerationZones(flags=1, epsFact_solid=float(L_leftSpo/2.),
                        orientation=[1., 0.], center=(float(L_leftSpo/2.), 0., 0.),
                        waves=waveinput, smoothing=3.0*he, dragAlpha=10.*omega/nu_0)

# Only Generation zone
elif opts.GenZone:
    tank.setAbsorptionZones(flags=1, epsFact_solid=float(L_leftSpo/2.),
                        orientation=[1., 0.], center=(float(L_leftSpo/2.), 0., 0.),
                        dragAlpha=10.*omega/nu_0)

# Porous zone
if opts.porousMedia:
    tank.setPorousZones(flags=3,
                    dragAlpha=dragAlpha, dragBeta=dragBeta,
                    porosity=porosity,)

# Absorption zone
if opts.AbsZone:
    if opts.caisson2D:
        tank.setAbsorptionZones(flags=4, epsFact_solid=float(L_rightSpo/2.),
                        orientation=[-1., 0.], center=(float(tank_dim[0]-L_rightSpo/2.), 0., 0.),
                        dragAlpha=10.*omega/nu_0)
    else:
        tank.setAbsorptionZones(flags=3, epsFact_solid=float(L_rightSpo/2.),
                        orientation=[-1., 0.], center=(float(tank_dim[0]-L_rightSpo/2.), 0., 0.),
                        dragAlpha=10.*omega/nu_0)

############################################################################################################################################################################
# ----- Output Gauges ----- #
############################################################################################################################################################################
T = opts.duration

gauge_dx=0.25
tank_dim_x=int(tank_dim[0])
nprobes=int(tank_dim_x/gauge_dx)+1
probes=np.linspace(0., tank_dim_x, nprobes)
PG=[]
if opts.caisson2D:
    zProbes=hs*0.5
else:
    zProbes=opts.water_level*0.5
for i in probes:
    PG.append((i, zProbes, 0.),)

if opts.caisson2D:
    gauge_dy=0.01
    tol=np.array([1*(10**-5),1*(10**-5),0.])
    i_point_f=np.array([caisson.vertices[0][0],caisson.vertices[0][1],0.])
    i_point_f += -tol #to avoid floating point error
    i_point_b=np.array([caisson.vertices[1][0],caisson.vertices[1][1],0.])
    i_point_b += tol #to avoid floating point error
    yProbes = np.linspace(i_point_f[1],i_point_f[1]+dimy, int(dimy/gauge_dy)+1)
    LG1=[]
    LG2=[]
    for j in yProbes:
        LG1.append((i_point_f[0],j,0.),)
        LG2.append((i_point_b[0],j,0.),)

#point_output=ga.PointGauges(gauges=((('p'),PG),
#                                 ),
#                          activeTime = (0., T),
#                          sampleRate=0.,
#                         fileName='point_gauges.csv')

#loadingsGauges=ga.PointGauges(gauges=((('p'),LG1),
#                                      (('p'),LG2),
#                                 ),
#                          activeTime = (0., T),
#                          sampleRate=0.,
#                          fileName='loadingsGauges.csv')

levelset_output=ga.PointGauges(gauges=((('phi',),PG),
                                 ),
                          activeTime = (0., T),
                          sampleRate=0.,
                          fileName='levelset_gauges.csv')

######################################################################################################################################################################################################################
# Numerical Options and other parameters #
######################################################################################################################################################################################################################

he = he
domain.MeshOptions.he = he

from math import *
from proteus import MeshTools, AuxiliaryVariables
import numpy
import proteus.MeshTools
from proteus import Domain
from proteus.Profiling import logEvent
from proteus.default_n import *
from proteus.ctransportCoefficients import smoothedHeaviside
from proteus.ctransportCoefficients import smoothedHeaviside_integral

st.assembleDomain(domain)

#----------------------------------------------------
# Time stepping and velocity
#----------------------------------------------------
weak_bc_penalty_constant = 10.0/nu_0 #100
dt_fixed = 1
dt_init = min(0.1*dt_fixed,0.001)
T = T
nDTout= int(round(T/dt_fixed))
runCFL = opts.cfl

#----------------------------------------------------
#  Discretization -- input options
#----------------------------------------------------

checkMass=False
applyCorrection=True
applyRedistancing=True
freezeLevelSet=opts.freezeLevelSet
useOnlyVF = False # if TRUE  proteus uses only these modules --> twp_navier_stokes_p + twp_navier_stokes_n
                  #                                              vof_p + vof_n
movingDomain=opts.movingDomain
useRANS = 0 # 0 -- None
            # 1 -- K-Epsilon
            # 2 -- K-Omega, 1998
            # 3 -- K-Omega, 1988

genMesh=True

# By DEFAULT on the other files.py -->  fullNewtonFlag = True
#                                       multilevelNonlinearSolver & levelNonlinearSolver == NonlinearSolvers.Newton

useOldPETSc=False # if TRUE  --> multilevelLinearSolver & levelLinearSolver == LinearSolvers.PETSc
                  # if FALSE --> multilevelLinearSolver & levelLinearSolver == LinearSolvers.KSP_petsc4py

useSuperlu = False #if TRUE --> multilevelLinearSolver & levelLinearSolver == LinearSolvers.LU

spaceOrder = 1
useHex     = False # used for discretization, if 1.0 --> CubeGaussQuadrature
                   #                          ELSE   --> SimplexGaussQuadrature

useRBLES   = 0.0 # multiplied with subGridError
useMetrics = 1.0 # if 1.0 --> use of user's parameters as (ns_shockCapturingFactor, ns_lag_shockCapturing, ecc ...)
useVF = opts.useVF # used in the smoothing functions as (1.0-useVF)*smoothedHeaviside(eps_rho,phi) + useVF*fmin(1.0,fmax(0.0,vf))

# Input checks
if spaceOrder not in [1,2]:
    print("INVALID: spaceOrder" + spaceOrder)
    sys.exit()

if useRBLES not in [0.0, 1.0]:
    print("INVALID: useRBLES" + useRBLES)
    sys.exit()

if useMetrics not in [0.0, 1.0]:
    print("INVALID: useMetrics")
    sys.exit()

#  Discretization
nd = 2
if spaceOrder == 1:
    hFactor=1.0
    if useHex:
        basis=C0_AffineLinearOnCubeWithNodalBasis
        elementQuadrature = CubeGaussQuadrature(nd,3)
        elementBoundaryQuadrature = CubeGaussQuadrature(nd-1,3)
    else:
        basis=C0_AffineLinearOnSimplexWithNodalBasis
        elementQuadrature = SimplexGaussQuadrature(nd,3)
        elementBoundaryQuadrature = SimplexGaussQuadrature(nd-1,3)
        #elementBoundaryQuadrature = SimplexLobattoQuadrature(nd-1,1)
elif spaceOrder == 2:
    hFactor=0.5
    if useHex:
        basis=C0_AffineLagrangeOnCubeWithNodalBasis
        elementQuadrature = CubeGaussQuadrature(nd,4)
        elementBoundaryQuadrature = CubeGaussQuadrature(nd-1,4)
    else:
        basis=C0_AffineQuadraticOnSimplexWithNodalBasis
        elementQuadrature = SimplexGaussQuadrature(nd,4)
        elementBoundaryQuadrature = SimplexGaussQuadrature(nd-1,4)

# Numerical parameters
ns_forceStrongDirichlet = False
backgroundDiffusionFactor=0.01
if useMetrics:
    ns_shockCapturingFactor  = 0.5 # magnifies numerical viscosity in NS (smoothening velocity fields)
    ns_lag_shockCapturing = True # lagging numerical viscosity speedsup Newton but destabilzes the solution
    ns_lag_subgridError = True # less nonlinear but less stable
    ls_shockCapturingFactor  = 0.5 # numerical diffusion of level set (smoothening phi)
    ls_lag_shockCapturing = True # less nonlinear but less stable
    ls_sc_uref  = 1.0 # reference gradient in numerical solution (higher=more diffusion)
    ls_sc_beta  = 1.5 # 1 is fully nonlinear, 2 is linear
    vof_shockCapturingFactor = 0.5 # numerical diffusion of level set (smoothening volume of fraction)
    vof_lag_shockCapturing = True # less nonlinear but less stable
    vof_sc_uref = 1.0
    vof_sc_beta = 1.5
    rd_shockCapturingFactor  = 0.5
    rd_lag_shockCapturing = False
    epsFact_density    = 3.0 # control width of water/air transition zone
    epsFact_viscosity  = epsFact_curvature  = epsFact_vof = epsFact_consrv_heaviside = epsFact_consrv_dirac = ecH = epsFact_density
    epsFact_redistance = 0.33
    epsFact_consrv_diffusion = 1.0 # affects smoothing diffusion in mass conservation
    redist_Newton = True
    kappa_shockCapturingFactor = 0.5
    kappa_lag_shockCapturing = True # False
    kappa_sc_uref = 1.0
    kappa_sc_beta = 1.5
    dissipation_shockCapturingFactor = 0.5
    dissipation_lag_shockCapturing = True # False
    dissipation_sc_uref = 1.0
    dissipation_sc_beta = 1.5
else:
    ns_shockCapturingFactor  = 0.9
    ns_lag_shockCapturing = True
    ns_lag_subgridError = True
    ls_shockCapturingFactor  = 0.9
    ls_lag_shockCapturing = True
    ls_sc_uref  = 1.0
    ls_sc_beta  = 1.0
    vof_shockCapturingFactor = 0.9
    vof_lag_shockCapturing = True
    vof_sc_uref  = 1.0
    vof_sc_beta  = 1.0
    rd_shockCapturingFactor  = 0.9
    rd_lag_shockCapturing = False
    epsFact_density    = 1.5
    epsFact_viscosity  = epsFact_curvature  = epsFact_vof = epsFact_consrv_heaviside = epsFact_consrv_dirac = epsFact_density
    epsFact_redistance = 0.33
    epsFact_consrv_diffusion = 10.0
    redist_Newton = False
    kappa_shockCapturingFactor = 0.9
    kappa_lag_shockCapturing = True#False
    kappa_sc_uref  = 1.0
    kappa_sc_beta  = 1.0
    dissipation_shockCapturingFactor = 0.9
    dissipation_lag_shockCapturing = True#False
    dissipation_sc_uref  = 1.0
    dissipation_sc_beta  = 1.0

ns_nl_atol_res = max(1.0e-12,0.001*domain.MeshOptions.he**2)
vof_nl_atol_res = max(1.0e-12,0.001*domain.MeshOptions.he**2)
ls_nl_atol_res = max(1.0e-12,0.001*domain.MeshOptions.he**2)
mcorr_nl_atol_res = max(1.0e-12,0.0001*domain.MeshOptions.he**2)
rd_nl_atol_res = max(1.0e-12,0.01*domain.MeshOptions.he)
kappa_nl_atol_res = max(1.0e-12,0.001*domain.MeshOptions.he**2)
dissipation_nl_atol_res = max(1.0e-12,0.001*domain.MeshOptions.he**2)
mesh_nl_atol_res = max(1.0e-12,0.001*domain.MeshOptions.he**2)

#turbulence
ns_closure=0 #1-classic smagorinsky, 2-dynamic smagorinsky, 3 -- k-epsilon, 4 -- k-omega

if useRANS == 1:
    ns_closure = 3
elif useRANS >= 2:
    ns_closure == 4

# Initial condition
waterLine_x = 2*tank_dim[0]
waterLine_z = waterLevel

def waveHeight(x,t):
    waterDepth = waveinput.eta(x, t) + waveinput.mwl
    return waterDepth

def wavePhi(x,t):
    [nd-1]- waveHeight(x,t)

def waveVF(x,t):
    return smoothedHeaviside(epsFact_consrv_heaviside*he,wavePhi(x,t))

def signedDistance(x):
    phi_x = x[0]-waterLine_x
    phi_z = x[nd-1]-waterLine_z

    if phi_x < 0.0:
        if phi_z < 0.0:
            return max(phi_x,phi_z)
        else:
            return phi_z
    else:
        if phi_z < 0.0:
            return phi_x
        else:
            return sqrt(phi_x**2 + phi_z**2)


from collections import defaultdict

def printSolution(x):
    print(f"The solution is: {x}")

def main():

    file = open("input.txt", "r")

    detected = {
        "children": 3,
        "cats": 7,
        "samoyeds": 2,
        "pomeranians": 3,
        "akitas": 0,
        "vizslas": 0,
        "goldfish": 5,
        "trees": 3,
        "cars": 2,
        "perfumes": 1,
    }

    aunts = {}
    matches = defaultdict(int)
    for line in file:
        line = line.strip().lstrip("Sue ")
        line = line.replace(":", "").replace(",", "")
        line = line.split()

        aunt = int(line.pop(0))
        aunts[aunt] = {}
        while line:
            k, v = line.pop(0), line.pop(0)
            aunts[aunt][k] = int(v)

    for aunt, details in aunts.items():
        for k, v in details.items():
            if k in detected and detected[k] == v:
                matches[aunt] += 1

    printSolution(max(matches, key=matches.get))

if __name__ == "__main__":
    main()


""" Contains Ros OrderState classes """

import uuid

class OrderState(object):
    """ A Ros Order State """
    def __init__(self):
        self.uuid = ""
        self.type = 0
        self.status = 0
        self.state = 0

    @classmethod
    def CreateObjectRosMsg(cls, msg):
        os = OrderState()
        try:
            os.uuid = str(uuid.UUID(bytes=msg.order_id.uuid))
            # print(os.uuid)
            os.status = msg.order_status
            os.type = msg.order_type
            os.state = msg.order_state

        except:
            return None
        return os

class rosOrderStatus():
    UNKNOWN = 0
    STARTED = 10
    ONGOING = 20
    FINISHED = 30
    WAITING = 40
    ERROR = 255

class rosOrderType():
    UNKNOWN = 0
    MOVE_ORDER = 10
    TRANSPORT_ORDER_STEP = 20
    TRANSPORT_ORDER = 30

class rosMoveOrderStates():
    MOVE_ORDER_UNAVAILABLE = 10
    MOVE_ORDER_START = 11
    MOVE_ORDER_ONGOING = 12
    MOVE_ORDER_FINISHED = 13
    MOVE_ORDER_ERROR = 14

class rosTransportOrderStepStates():
    TOS_MOVE_ORDER_START = 20
    TOS_MOVE_ORDER_ONGOING = 21
    TOS_MOVE_ORDER_FINISHED = 22
    TOS_MOVE_ORDER_ERROR = 23
    TOS_ACTION_START = 24
    TOS_ACTION_ONGOING = 25
    TOS_ACTION_FINISHED = 26
    TOS_ACTION_ERROR = 27

class rosTransportOrderStates():
    # Transport order
    TO_LOAD_MOVE_ORDER_START = 40
    TO_LOAD_MOVE_ORDER_ONGOING = 41
    TO_LOAD_MOVE_ORDER_FINISHED = 42
    TO_LOAD_MOVE_ORDER_ERROR = 43
    TO_LOAD_ACTION_START = 44
    TO_LOAD_ACTION_ONGOING = 45
    TO_LOAD_ACTION_FINISHED = 46
    TO_LOAD_ACTION_ERROR = 47
    TO_UNLOAD_MOVE_ORDER_START = 48
    TO_UNLOAD_MOVE_ORDER_ONGOING = 49
    TO_UNLOAD_MOVE_ORDER_FINISHED = 50
    TO_UNLOAD_MOVE_ORDER_ERROR = 51
    TO_UNLOAD_ACTION_START = 52
    TO_UNLOAD_ACTION_ONGOING = 53
    TO_UNLOAD_ACTION_FINISHED = 54
    TO_UNLOAD_ACTION_ERROR = 55


from SpatialCluster.utils.data_format import numpy_data_format, position_data_format
from SpatialCluster.utils.get_areas import get_areas
from minisom import MiniSom
import numpy as np

def SOM_Clustering(features_X, features_position, som_shape = (2,2), sigma=0.5, learning_rate=0.5, num_iterations = 100):
    features_X = numpy_data_format(features_X)
    features_position = position_data_format(features_position)
    som = MiniSom(som_shape[0], som_shape[1], features_X.shape[1], sigma=sigma, learning_rate=learning_rate)
    som.train(features_X, num_iterations)
    winner_coordinates = np.array([som.winner(x) for x in features_X]).T
    clusters = np.ravel_multi_index(winner_coordinates, som_shape)
    points = list(zip(features_position.lon, features_position.lat))
    areas_to_points = get_areas(clusters, points)
    return areas_to_points, clusters


#!/usr/bin/python

from mininet.net import Mininet
from mininet.node import Host, RemoteController
from mininet.node import OVSKernelSwitch
from mininet.log import setLogLevel, info
from lib.controller import POXBridge as AmiController
from mininet.cli import CLI

def AMINetwork():

    net = Mininet( topo=None,
                   build=False,
                   ipBase='10.0.0.0/8')

    info('*** Adding controller \n')
    c1 = net.addController('c1',
                           controller=AmiController,
                           #protocol='tcp',
                           port=6633)
    c2 = RemoteController('c2', ip='127.0.0.1', port=6635)
    #net.addController(c2)

    info( '*** Add switches\n')
    s1 = net.addSwitch('s1', cls=OVSKernelSwitch)
    s3 = net.addSwitch('s3', cls=OVSKernelSwitch)
    s2 = net.addSwitch('s2', cls=OVSKernelSwitch)

    info( '*** Add hosts\n')
    h1 = net.addHost('h1', cls=Host, ip='10.0.0.10', defaultRoute=None)
    h2 = net.addHost('h2', cls=Host, ip='10.0.0.20', defaultRoute=None)
    h3 = net.addHost('h3', cls=Host, ip='10.0.0.30', defaultRoute=None)
    h4 = net.addHost('h4', cls=Host, ip='10.0.0.31', defaultRoute=None)

    info('*** Add links\n')
    net.addLink(h1, s1)
    net.addLink(s1, s2)
    net.addLink(h2, s2)
    net.addLink(s2, s3)
    net.addLink(h3, s3)
    net.addLink(h4, s3)

    info( '*** Starting network\n')
    net.build()
    # info ('*** Starting controllers\n')
    for controller in net.controllers:
        controller.start()

    # info('*** Starting switches\n')
    net.get('s1').start([c1])
    net.get('s3').start([c1])
    net.get('s2').start([c1])

    info('*** Post configure switches and hosts\n')
    #net.pingAll()
    CLI(net)
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    AMINetwork()


#! /usr/bin/env python3

leeftijd = int(input("wat is uw leeftijd? "))
aantal = int(input("hoeveel bewoners? "))
huur = int(input("wat is uw huurbedrag? "))
inkomen = int(input("wat is uw inkomen? "))
vermogen = int(input("wat is uw vermogen? "))
huurtoeslag=False;
aantalinkomenvermogen = (aantal == 1 and inkomen <= 22000 and vermogen <=21000) or (aantal>1 and inkomen <300000 and vermogen <= 42000)
if leeftijd < 23:
    if huur >= 231 and huur <= 409 and aantalinkomenvermogen:
            huurtoeslag = True
elif huur >=231 and huur <= 710 and aantalinkomenvermogen:
    huurtoeslag = True
if huurtoeslag:
    print("U heeft wel recht op huurtoeslag")
else:
    print("U heeft geen recht op huurtoeslag")


from time import sleep
from pubsub import pub
from smart_bar import SmartBar
from order_manager import OrderManager
from threading import Thread
import requests

class App:
    def run(self):
        self.setup()
        self.loop()

    def setup(self):
        print("Create App")
        requests.get('http://smart-bar-app.herokuapp.com/api/orders/delete_all')

        pub.subscribe(self.notify, pub.ALL_TOPICS)

        self.order_manager = OrderManager()
        self.smart_bar = SmartBar()

        order_manager_thread = Thread(target = self.order_manager.run, daemon = True)
        order_manager_thread.start()

    def loop(self):
        order = {}

        try:
            while True:
                if not self.smart_bar.isProcessing() and bool(order):
                    pub.sendMessage('order-creating', status='creating')
                    self.smart_bar.processDrink(order["drink"])
                    requests.get('http://smart-bar-app.herokuapp.com/api/orders/delete_all')
                    order = {}
                elif self.smart_bar.isProcessing() == False:
                    order = self.getNewOrder()

                if (self.order_manager.cancel):
                    print(self.order_manager.cancel)
                    self.smart_bar.stop()
                    self.order_manager.cancel = False

                sleep(0.1)

            print("Done making orders")
        except KeyboardInterrupt:
            print ("\nCtrl-C pressed.")
            self.smart_bar.stop()

    def notify(self, topicObj=pub.AUTO_TOPIC, **msgData):
        status = False

        if bool(msgData) and msgData['status']:
            status = msgData['status']

        self.order_manager.queueUpdateOrder(topicObj.getName(), status)
        print('topic "%s": %s' % (topicObj.getName(), msgData))

    def getNewOrder(self):
        return self.order_manager.getLatestOrder()

app = App()
app.run()


from salesforce_ocapi.auth import CommerceCloudClientSession
import requests

session = CommerceCloudClientSession(
    client_id="ADDCLIENTID",
    client_secret="ADDSECRET",
    instance="https://production-eu01-example.demandware.net",
)

categories = ["CHLBAKTEA", "BAKBRDBRN"]

def get_category_information(catalogid: str, categoryid: str):
    url = f"{session.instance}/s/-/dw/data/v20_4/catalogs/{catalogid}/categories/{categoryid}"
    headers = {"Authorization": session.AuthHeader()}
    response = requests.request("GET", url, headers=headers)
    if response.ok:
        return response.json()
    else:
        return response.status_code

for category in categories:
    print(get_category_information("master-catalog", category))


# https://leetcode.com/problems/populating-next-right-pointers-in-each-node/

from collections import deque

from TreeNode import TreeNode as Node
from TreeNode import list_to_treenodes as ltt

def add_next_prop(treenode):
    if not treenode:
        return
    treenode.next = None
    add_next_prop(treenode.left)
    add_next_prop(treenode.right)
    return treenode

class Solution:
    def connect(self, root: Node) -> Node:
        if not root or not root.left or not root.right:
            return root

        count, lvc = 1, 3
        stack = deque([root.left, root.right])
        while stack:
            node1 = stack.popleft()
            if node1.left:
                stack.append(node1.left)
                stack.append(node1.right)
            count += 1
            if count != lvc and stack:
                node1.next = stack[0]
            else:
                lvc = lvc * 2 + 1

        return root

    def connect(self, root):
        p = root
        n1 = n2 = root.left if root else None
        while n2:
            if n2 != p.left:
                n2.next = p.left
            else:
                n2.next = p.right
                p = p.next
            n2 = n2.next
            if not p:
                p, n1, n2 = n1, n1.left, n1.left
        return root

s = Solution()
root = add_next_prop(ltt("[1,2,3,4,5,6,7]"))
s.connect(root)
print(root)

leetcode/Populating Next Right Pointers in Each Node.py


import numpy as np
import os
from PIL import Image
import numpy as np
import torch
import gc

def load_one(i):
    # ../
    im_directory = "data/images/"
    mask_directory = "data/masks/"
    f_im = os.path.join(im_directory, str(i) + ".jpg")
    f_mask = os.path.join(mask_directory, str(i) + ".jpg")
    im = Image.open(f_im)
    mask = Image.open(f_mask)
    im_data = im.getdata()
    mask_data = mask.convert("L").getdata()

    im.close()
    mask.close()

    # extract data into numpy array
    im_data = np.array(im_data).T
    mask_data = np.array(mask_data).T

    # get data into right shape and concat image data to final data array
    data_im = np.array([im_data.reshape(3, 256, 256)])
    data_mask = np.array([mask_data.reshape(1, 256, 256)])
    gc.collect()
    return torch.tensor(data_im).float(), torch.tensor(data_mask).float()

class DataLoader:
    def __init__(self, set_size, train_size, batch_size, test_batch_size=100):
        self.set = np.arange(set_size)
        self.trainingset = []
        self.testset = []
        self.set_size = set_size
        self.test_batch_size = test_batch_size

        self.batch_size = batch_size

        self.train_size = train_size
        self.trainingset = np.random.randint(set_size, size=train_size)

        self.test_size = self.set_size - self.train_size
        self.batch_counter = 0
        self.test_counter = 0
        print(os.getcwd())
        self.testset = list(set(self.set).difference(self.trainingset))

        self.load_all_counter = 0

    def start_new_epoch(self):
        self.batch_counter = 0
        np.random.shuffle(self.trainingset)

    def epoch_finished(self):
        return self.batch_counter >= int(self.train_size / self.batch_size)

    def trainloader(self):
        batch = self.trainingset[
            (self.batch_counter * self.batch_size) : (
                (self.batch_counter + 1) * self.batch_size
            )
        ]
        self.batch_counter += 1
        return self.batchloader(batch=batch)

    def testdata_loaded(self):
        if self.test_counter >= int(self.test_size / self.test_batch_size):
            self.test_counter = 0
            return True

        return False

    def testloader(self):
        batch = self.testset[
            (self.test_counter * self.test_batch_size) : (
                (self.test_counter + 1) * self.test_batch_size
            )
        ]
        self.test_counter += 1
        return self.batchloader(batch=batch)

    def batchloader(self, batchsize=None, batch=None):
        im_directory = "data/images/"
        mask_directory = "data/masks/"
        # if batch is given iterate throught batch indices
        if batchsize == None and any(batch) != None:
            j = 0
            for i in batch:
                # open image
                f_im = im_directory + str(i) + ".jpg"
                f_mask = mask_directory + str(i) + ".jpg"
                with Image.open(f_im) as im:
                    im_data = np.array(im.getdata()).T
                with Image.open(f_mask) as mask:
                    mask_data = np.array(mask.convert("L").getdata()).T / 255
                # im = Image.open(f_im)
                # mask = Image.open(f_mask)
                # mask = mask.convert("L")
                # extract data into numpy array

                # get data into right shape and concat image data to final data array
                if j == 0:
                    data_im = np.array([im_data.reshape(3, 256, 256)])
                    data_mask = np.array([mask_data.reshape(1, 256, 256)])
                    j += 1

                else:
                    im_data = im_data.reshape(1, 3, 256, 256)
                    data_im = np.concatenate((data_im, im_data))

                    mask_data = mask_data.reshape(1, 1, 256, 256)
                    data_mask = np.concatenate((data_mask, mask_data))
                # im.close()
                # mask.close()
                gc.collect()
            return torch.tensor(data_im).float(), torch.tensor(data_mask).float()

        if batchsize != None and any(batch) == None:
            print("WIRD DOCH BENUTZT")
            # if batch is not given generate random batch of size batchsize
            batch = np.random.randint(5108, size=batchsize)
            j = 0
            for i in batch:
                # open image
                f_im = os.path.join(im_directory, str(i) + ".jpg")
                f_mask = os.path.join(mask_directory, str(i) + ".jpg")
                im = Image.open(f_im)
                mask = Image.open(f_mask)
                mask = mask.convert("L")

                # extract data into numpy array
                im_data = np.array(im.getdata()).T
                mask_data = np.array(mask.getdata()).T

                # get data into right shape and concat image data to final data array
                if j == 0:

                    data_im = np.array([im_data.reshape(3, 256, 256)])
                    data_mask = np.array([mask_data.reshape(1, 256, 256)])
                    j += 1

                else:
                    im_data = im_data.reshape(1, 3, 256, 256)
                    data_im = np.concatenate((data_im, im_data))

                    mask_data = mask_data.reshape(1, 1, 256, 256)
                    data_mask = np.concatenate((data_mask, mask_data))
                im.close()
                mask.close()
                gc.collect()
            return torch.tensor(data_im).float(), torch.tensor(data_mask).float()

        if batchsize == None and batch == None:
            # if batchsize and batch are None raise Exception
            raise Exception("Weder batchsize noch batch deklariert!")


import pytorch_lightning as pl
from . import tools

class ForexDataModule(pl.LightningDataModule):

    def __init__(self, window_size, batch_size, label_size, filenames):
        super().__init__()
        self.window_size = window_size
        self.batch_size = batch_size
        self.label_size = label_size
        self.filenames = filenames

    def prepare_data(self):

        price_data, price_labels = tools.build_dataset(
            *self.filenames,
            window_size=self.window_size,
            label_size=self.label_size
        )
        self.train_loader, self.val_loader = tools.split_and_create_loaders(price_data, price_labels, self.batch_size)

    def train_dataloader(self):
        return self.train_loader

    def val_dataloader(self):
        return self.val_loader


#Translation means shifting an image in x and y axis or also the
#combinations of both x and y axis

import cv2 as cv
import numpy as np
img = cv.imread("C:/Users/vijay/Desktop/photo.jpg")

#We need to create a transalation method with img,x,y as parameters
def translate(img,x,y):
    #We need to create a translation matrix
    transMat = np.float32([[1,0,x],[0,1,y]])
    #We also need dimenions of the image
    dimensions = (img.shape[1], img.shape[0])
    return cv.warpAffine(img, transMat, dimensions)

#Values are
# -x --> left
# -y --> up
# x --> right
# y --> down

translated = translate(img,100,150)
cv.imshow("Image",translated)
cv.waitKey(0)
cv.destroyAllWindows()


"""
Created 06/03/2018
@author: Will Wilkinson
"""

import numpy as np

class EqualRectangularBandwidth:
    def __init__(self, N, low_lim, high_lim, freqRangePerBin=50):
        # make cutoffs evenly spaced on an erb scale
        self.N = N
        self.low_lim = low_lim
        self.high_lim = high_lim
        self.freqRangePerBin = freqRangePerBin

        erb_low = self.freq2erb(self.low_lim)
        erb_high = self.freq2erb(self.high_lim)
        erb_lims = np.linspace(erb_low, erb_high, self.N + 2)
        self.cutoffs = self.erb2freq(erb_lims)
        self.filters = self.make_filters()

    def freq2erb(self, freq_Hz):
        n_erb = 9.265 * np.log(1 + np.divide(freq_Hz, 24.7 * 9.265))
        return n_erb

    def erb2freq(self, n_erb):
        freq_Hz = 24.7 * 9.265 * (np.exp(np.divide(n_erb, 9.265)) - 1)
        return freq_Hz

    def make_filters(self):
        cos_filts = []
        for k in range(self.N):
            l_k = self.cutoffs[k]
            h_k = self.cutoffs[k + 2]  # adjacent filters overlap by 50%
            # impose minimum 100hz
            if h_k - l_k < 100:
                h_k = l_k + 100

            l_nfftind = (int)(l_k / self.freqRangePerBin) + 1
            h_nfftind = (int)(h_k / self.freqRangePerBin)

            avg = (self.freq2erb(l_k) + self.freq2erb(h_k)) / 2
            rnge = self.freq2erb(h_k) - self.freq2erb(l_k)

            kth_filter_array = []
            for i in range(l_nfftind, h_nfftind + 1):
                kth_filter_array.append(
                    np.cos(
                        (self.freq2erb(self.freqRangePerBin * i) - avg) / rnge * np.pi
                    )
                )
            cos_filts.append(((l_nfftind, h_nfftind + 1), np.array(kth_filter_array)))
        return np.array(cos_filts, dtype=object)


import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

# %matplotlib inline

def f(x, y, z, regla):
    return regla & (1 << (x << 2 | y << 1 | z))

def ac1(regla=135, E0="azar", alto=100, ancho=100):
    fig = plt.figure(figsize=(alto / 10, ancho / 10))
    E = np.zeros((alto, ancho), dtype=np.bool_)
    if E0 == "central":
        E[0, int(ancho / 2)] = 1  # 1 central
    elif E0[0] in ['0', '1']:
        ancho = len(E0)
        E = np.zeros((alto, ancho), dtype=np.bool_)
        for r in range(256):
            E[0, :] = np.array(list(E0))
    else:
        E[0, :] = np.round(np.random.rand(1, ancho))  # renglon aleatorio
    for i in range(1, alto):
        for j in range(1, ancho - 1):
            E[i, j] = f(E[i - 1, j - 1], E[i - 1, j], E[i - 1, j + 1], regla)
        E[i, 0] = f(E[i - 1, ancho - 1], E[i - 1, 0], E[i - 1, 1], regla)  # ahorra tiempo separar estos 2 casos
        E[i, ancho - 1] = f(E[i - 1, ancho - 2], E[i - 1, ancho - 1], E[i - 1, 0],
                            regla)  # no deben estar en el ciclo interno pues no depemden de j (pasa de 15s a 9s)
    plt.imshow(E, cmap=cm.Blues)
    plt.show()

ac1(115, "azar", alto=90)
#ac1(90, "central", alto=500, ancho=1000)
#ac1(94, "01110000000000001111100001111")
#ac1(alto=100, ancho=1000)


# File: heat_pump_adoption_modelling/pipeline/supervised_model/hp_status_prediction.py
"""
Predict the HP status of a household using a supervised learning model.
"""

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

from heat_pump_adoption_modelling import PROJECT_DIR, get_yaml_config, Path
from heat_pump_adoption_modelling.pipeline.preprocessing import feature_engineering
from heat_pump_adoption_modelling.pipeline.supervised_model.utils import plotting_utils

import pandas as pd
import numpy as np
from sklearn import svm

import re

from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import MinMaxScaler
from sklearn.decomposition import PCA

from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDClassifier

from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_validate
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_score

from heat_pump_adoption_modelling import PROJECT_DIR, get_yaml_config, Path

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

# Load config file
config = get_yaml_config(
    Path(str(PROJECT_DIR) + "/heat_pump_adoption_modelling/config/base.yaml")
)

FIG_PATH = PROJECT_DIR / config["SUPERVISED_MODEL_FIG_PATH"]
SUPERVISED_MODEL_OUTPUT = str(PROJECT_DIR) + config["SUPERVISED_MODEL_OUTPUT"]
IDENTIFIER = config["IDENTIFIER"]

prepr_pipeline = Pipeline(
    [
        ("imputer", SimpleImputer(missing_values=np.nan, strategy="median")),
        ("min_max_scaler", MinMaxScaler()),
        ("pca", PCA(n_components=0.9, random_state=42)),
    ]
)

prepr_pipeline_no_pca = Pipeline(
    [
        ("imputer", SimpleImputer(missing_values=np.nan, strategy="median")),
        ("min_max_scaler", MinMaxScaler()),
    ]
)

best_params = {
    "Linear Support Vector Classifier":  # { "alpha": 0.0001,"epsilon": 0.0001,
    # "penalty": "l2","tol": 0.001,}
    {
        "alpha": 0.0001,
        "epsilon": 0.0001,
        "penalty": "elasticnet",
        "tol": 0.0005,
        "max_iter": 5000,
    }
}

model_dict = {
    "Logistic Regression": LogisticRegression(random_state=42),
    "Linear Support Vector Classifier": SGDClassifier(random_state=42),
    "Support Vector Classifier": svm.SVC(probability=True, random_state=42),
}

def get_HP_status_changes(df):
    """For properties with multiple entries, get heat pump status at first
    and last entry time.

    Parameters
    ----------
    df: pandas.DataFrame
        Dataframe for not de-duplicated EPC data.

    Return
    ---------
    df_with_hp_status: pandas.DataFrame
        Dataframe with added future heat pump status."""

    # Get properties with multiple entries
    multiple_entry_epcs = df.loc[df["N_ENTRIES_BUILD_ID"] >= 2]

    # Get latest and first EPC entry
    latest_epc = feature_engineering.filter_by_year(
        multiple_entry_epcs, IDENTIFIER, None, selection="latest entry"
    )
    first_epc = feature_engineering.filter_by_year(
        multiple_entry_epcs, IDENTIFIER, None, selection="first entry"
    )

    # Heat pump status now / in the past
    latest_epc.rename(columns={"HP_INSTALLED": "NOW_HP"}, inplace=True)
    first_epc.rename(columns={"HP_INSTALLED": "PAST_HP"}, inplace=True)

    # Merging before/after
    before_after_status = pd.merge(
        latest_epc[["NOW_HP", IDENTIFIER]],
        first_epc[["PAST_HP", IDENTIFIER]],
        on=[IDENTIFIER],
    )

    # Heat pump was added in the meantime
    before_after_status["HP_ADDED"] = (before_after_status["NOW_HP"]) & (
        ~before_after_status["PAST_HP"]
    )

    # Get future heat pump status
    df_with_hp_status = pd.merge(
        first_epc,
        before_after_status[[IDENTIFIER, "HP_ADDED"]],
        on=[IDENTIFIER],
    )

    # Exclude samples that had a HP from the very start
    df_with_hp_status = df_with_hp_status.loc[(df_with_hp_status["PAST_HP"] == False)]

    # Drop unnecesary columns
    df_with_hp_status = df_with_hp_status.drop(columns=["PAST_HP"])

    return df_with_hp_status

def balance_set(X, target_variable, false_ratio=0.9):
    """Balance the training set.
    If false ratio set to 0.9, then 90% of the training data
    will have "False/No HP Installed" as a label.

    Parameters
    ----------
    X: pandas.DataFrame
        Training set.

    target_variable : str
        Variable/feature that is going to be predicted.

    false_ratio : float, default=0.9
        When re-balancing the set, use the false_ratio
        to determine the amount of False labels.

    Return
    ---------
    X: pandas.DataFrame
        Re-balanced training set."""

    multiplier = false_ratio / (1 - false_ratio)

    # Seperate samples with and without heat pumps
    X_true = X.loc[X[target_variable] == True]
    X_false = X.loc[X[target_variable] == False]

    # Shuffle and adjust size
    X_false = X_false.sample(frac=1)

    # Get the appropriate amount of "false" samples
    X_false = X_false[: int(X_true.shape[0] * multiplier)]

    # Concatenate "true" and "false" samples
    X = pd.concat([X_true, X_false], axis=0)

    # Reshuffle
    X = X.sample(frac=1)

    return X

def get_data_with_labels(
    df, version="Future HP Status", drop_features=[], balanced_set=True
):
    """Get the training data and labels (X, y) for training the models.

    Parameters
    ----------
    df: pandas.DataFrame
        Dataframe with features.

    version : str, default='Future HP Status'
        Target variable.
        Options: Future HP Status, Current HP Status

    drop_features : list, default=[]
        Features to discard.

    balanced_set : bool, default=True
        Balance the training set with HP/no HP samples.

    Return
    ---------
    X : pandas.Dataframe
        Training data.

    y : pandas.Dataframe
        Labels / ground truth."""

    df = df.sample(frac=1)

    # For the current status, get latest EPC entry only
    if version == "Current HP Status":

        X = feature_engineering.filter_by_year(
            df, IDENTIFIER, None, selection="latest entry"
        )
        target_variable = "HP_INSTALLED"
        drop_features += ["HEATING_SYSTEM", "MAINHEAT_DESCRIPTION"]

    # For future status, use changed heat pump status data
    elif version == "Future HP Status":

        X = get_HP_status_changes(df)
        target_variable = "HP_ADDED"

    else:
        raise IOError("Version '{}' is unknown.".format(version))

    # Drop columns that are all NaN (not expected)
    X = X.dropna(axis="columns", how="all")

    # Balance the feature set
    if balanced_set:
        X = balance_set(X, target_variable)

    X.reset_index(inplace=True, drop=True)

    # Select the label / ground truth
    y = X[target_variable]

    # Remove unneccesary features, including target variables
    for col in [target_variable] + drop_features:
        if col in X.columns:
            del X[col]

    return X, y

def train_and_evaluate(
    model_name, X_train, y_train, X_test, y_test, target_variable, feature_names, cv=5
):
    """
    Train and evaluate growth prediction model.

    Parameters
    ----------
    model_name: str
        Model to train and evaluate.

    X_train: np.array
        Training data.

    y_train: np.array
        Solutions for training data.

    X_test: np.array
        Test data.

    y_test: np.array
        Solutions for test data.

    target_variable : str
        Target variable to predict,
        e.g. HP_ADDED, HP_INSTALLED

    feature_names: list
        Feature names for training features.

    cv : int, default=5
        Number of cross validations, by default 5-fold.

    Return
    ---------
    model : sklearn model
        Trained model.
    """

    model = model_dict[model_name]

    if model in best_params.keys():
        model.set_params(**best_params[model_name])
    model.fit(X_train, y_train)

    pred_train = model.predict(X_train)
    pred_test = model.predict(X_test)

    print("Model Name:", model_name)

    # Plot the confusion matrix for training set
    plotting_utils.plot_confusion_matrix(
        y_train, pred_train, ["No HP", "HP"], "Training set"
    )

    # Plot the confusion matrix for validation set
    plotting_utils.plot_confusion_matrix(
        y_test, pred_test, ["No HP", "HP"], "Validation set"
    )

    # Print Accuracies
    print(
        "Accuracy train: {}%".format(
            np.round(accuracy_score(y_train, pred_train) * 100), 2
        )
    )
    print(
        "Accuracy test:   {}%".format(
            np.round(accuracy_score(y_test, pred_test) * 100), 2
        )
    )

    scores = cross_validate(
        model,
        X_train,
        y_train,
        scoring=("accuracy", "f1", "recall", "precision"),
        return_train_score=True,
        cv=cv,
    )

    print()
    print("{}-fold Cross Validation: Train \n---------\n".format(cv))
    print("Accuracy:", round(scores["train_accuracy"].mean(), 2))
    print("F1 Score:", round(scores["train_f1"].mean(), 2))
    print("Recall:", round(scores["train_recall"].mean(), 2))
    print("Precision:", round(scores["train_precision"].mean(), 2))
    print()
    print("{}-fold Cross Validation: Test \n---------\n".format(cv))
    print("Accuracy:", round(scores["test_accuracy"].mean(), 2))
    print("F1 Score:", round(scores["test_f1"].mean(), 2))
    print("Recall:", round(scores["test_recall"].mean(), 2))
    print("Precision:", round(scores["test_precision"].mean(), 2))
    print()
    print("-------------------------------")

    return model

def predict_heat_pump_status(
    X, y, target_variable="HP_Installed", save_predictions=False
):
    """Predict the heat pump status for household.

    Parameters
    ----------
    X: pandas.DataFrame
        Feature data.

    y: pandas.DataFrame
        Labels / ground truth.

    target_variables : str, default="HP_Installed"
        Target variables to predict.

    save_predictions : boolean, default=False
        Save the predictions, errors and other information for error analysis.

    Return: None"""

    # Save original training data (with all columns, e.g. POSTCODE, target variables)
    if save_predictions:
        original_df = X.copy()

    # Remove unnecessary columns
    for feat in X.columns:
        if feat.startswith("POSTCODE"):
            del X[feat]

    # Print the number of samples and features
    print("Number of samples:", X.shape[0])
    print("Number of features:", X.shape[1])
    print()

    trained_models = {}
    feature_names = X.columns
    y = np.array(y)

    # Apply preprocesisng pipeline
    X_prep = prepr_pipeline.fit_transform(X)

    # Split into train and test sets
    X_train, X_test, y_train, y_test, indices_train, _ = train_test_split(
        X_prep, y, list(X.index), test_size=0.1, random_state=42, stratify=y
    )

    X["training set"] = False
    X.loc[indices_train, "training set"] = True

    # Mark training samples
    if save_predictions:
        original_df["training set"] = False
        original_df.loc[indices_train, "training set"] = True

    # For each model train, make predictions and evaluate
    for model in model_dict.keys():
        trained_model = train_and_evaluate(
            model, X_train, y_train, X_test, y_test, "HP Installed", feature_names
        )

        trained_models[model] = trained_model

        if save_predictions:

            data_with_label_and_pred = original_df.copy()

            predictions = trained_model.predict(X_prep)

            # Get probabilities for target classes
            if model == "Logistic Regression":
                data_with_label_and_pred["proba 1"] = trained_model.predict_proba(
                    X_prep
                )[0][0]
                data_with_label_and_pred["proba 2"] = trained_model.predict_proba(
                    X_prep
                )[0][1]

            elif model == "Support Vector Classifier":

                data_with_label_and_pred["proba 1"] = trained_model.predict_proba(
                    X_prep
                )[:, 0]
                data_with_label_and_pred["proba 2"] = trained_model.predict_proba(
                    X_prep
                )[:, 1]
            else:
                pass

            # Save predictions, errors and ground truths for later error analysis
            data_with_label_and_pred["ground truth"] = y
            data_with_label_and_pred["prediction"] = predictions

            data_with_label_and_pred["error"] = (
                data_with_label_and_pred["prediction"]
                != data_with_label_and_pred["ground truth"]
            )

            output_filename = "household_based_predictions_with_{}.csv".format(
                re.sub(" ", "_", model).lower()
            )

            print(
                "Output saved to {}".format(SUPERVISED_MODEL_OUTPUT + output_filename)
            )
            data_with_label_and_pred.to_csv(
                SUPERVISED_MODEL_OUTPUT + output_filename, index=False
            )

    return trained_models


import re
import scrapy
from scrapy.loader import ItemLoader
from ..items import UuttarabankItem
from itemloaders.processors import TakeFirst
import datetime

pattern = r'(\xa0)?'
base = 'https://www.uttarabank-bd.com/index.php/home/news/{}'

class UuttarabankSpider(scrapy.Spider):
	now = datetime.datetime.now()
	year = now.year
	name = 'uttarabank'
	start_urls = [base.format(year)]

	def parse(self, response):
		yield response.follow(response.url, self.parse_post, dont_filter=True)

		if self.year > 2016:
			self.year -= 1
			yield response.follow(base.format(self.year), self.parse)

	def parse_post(self, response):
		articles = response.xpath('//div[@class="tabscontent"]//div[@class="row"]')
		length = len(articles)

		for index in range(length):
			item = ItemLoader(item=UuttarabankItem(), response=response)
			item.default_output_processor = TakeFirst()

			date = response.xpath(f'(//span[@style="color:#ff0000; font-size:12px;"])[{index + 1}]//text()').get()
			title = response.xpath(f'(//div[@class="floatleft col75"]/span[@title="Project Details"])[{index + 1}]//text()').get()
			content = response.xpath(f'(//div[@class="eleven columns row"])[{index + 1}]//text()').getall()
			content = [p.strip() for p in content if p.strip()]
			content = re.sub(pattern, "",' '.join(content))

			item.add_value('title', title)
			item.add_value('link', response.url)
			item.add_value('content', content)
			item.add_value('date', date)

			yield item.load_item()


'''
@author: bighouse
'''
from _collections import defaultdict
from ngram import NgramSequenceClassifier, Classifier, choose_max

class InterpolationClassifier(Classifier):

    def __init__(self, n, weights, verbose = False, cached = False):
        self.cached = cached
        if self.cached :
            self.cache = {}

        self.weights = weights
        self.n = n
        self.verbose = verbose
        self.k = len(weights)
        self.classifiers = []

        for i in range(self.k) :
            self.classifiers.append(NgramSequenceClassifier(n - i))

    def train(self, data, label):
        for i in xrange(self.k) :
            self.classifiers[i].train(data[i:], label)

    def prepare(self):
        pass

    def get_prob(self, denominator, classifier, label, current_ngram):
        model = classifier.models[label]
        numerator = classifier.get_prior_probabability_numerator(model, current_ngram[:-1])
        prior = numerator / denominator
        return model.conditional_probability(current_ngram) * prior

    def add_terms_of_submodel(self, probas, current_ngram, classifier, weight) :
            denominator = classifier.get_prior_probabability_denominator(current_ngram[:-1])
                # If the prior has never been seen
            if denominator == 0 :
                return 0.0
            else:
                for label in classifier.models :
                    frequency = self.get_prob(denominator, classifier, label, current_ngram)
                    probas[label] +=  frequency * weight

    def classify_suffix(self, probas, data, idx):
        for j in xrange(self.k):
            weight = self.weights[j]
            classifier = self.classifiers[j]
            subsequence = data[idx - self.n + j : idx + 1]
            self.add_terms_of_submodel(probas, subsequence, classifier, weight)
        return probas

    def classify(self, data):
        d_length = len(data)
        if d_length < self.n + 1 :
            return 'NONE'
        predictions = []
        for i in xrange(self.n, d_length) :
            probas = defaultdict(lambda : 0.0)
            probas = self.classify_suffix(probas, data, i)
            predictions.append(probas)
        result = choose_max(predictions)
        if len(result) == 0 :
            return "NONE"
        return result[0][0]


#!/bin/env python3

from os import path

from setuptools import setup, find_packages

here = path.abspath(path.dirname(__file__))
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='borgmacator',
    version='0.0.1',
    description='A GNOME AppIndicator for Borgmatic',
    long_description=long_description,
    long_description_content_type='text/markdown',
    url='https://github.com/N-Coder/borgmacator',
    author='Niko Fink',
    author_email='borgmacator@niko.fink.bayern',
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Environment :: X11 Applications :: Gnome',
        'Operating System :: Unix',
        'Intended Audience :: End Users/Desktop',
        'Topic :: Desktop Environment :: Gnome',
        'Topic :: System :: Archiving :: Backup',
        'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
        'Programming Language :: Python :: 3 :: Only',
    ],
    keywords='borg borgmatic backup appindicator indicator gnome',
    packages=find_packages(),
    python_requires='>=3.5, <4',
    install_requires=['PyGObject', 'pystemd', 'python-dateutil', 'requests', 'sh', 'appdirs'],
    entry_points={
        'console_scripts': [
            'borgmacator=borgmacator.main:main',
            'borgmacator-autostart=borgmacator.install:install',
            'borgmacator-restart=borgmacator.install:restart',
        ],
    },
    zip_safe=False,
    package_data={
        'borgmacator': ["img/*.svg"],
    }
)


from pymol import cmd, stored
import numpy as np
import matplotlib.cm as cm
from matplotlib.colors import Normalize
cmap = cm.seismic
norm = Normalize(vmin=-1, vmax=1)
cmap2 = cm.PRGn
norm = Normalize(vmin=-1, vmax=1)

def zero_residues(sel1,offset=0,chains=0):
        """
        """
        offset = int(offset)

        # variable to store the offset
        stored.first = None
        # get the names of the proteins in the selection

        names = ['(model %s and (%s))' % (p, sel1)
                        for p in cmd.get_object_list('(' + sel1 + ')')]

        if int (chains):
                names = ['(%s and chain %s)' % (p, chain)
                                for p in names
                                for chain in cmd.get_chains(p)]

        # for each name shown
        for p in names:
                # get this offset
                ok = cmd.iterate("first %s and polymer and n. CA" % p,"stored.first=resv")
                # don't waste time if we don't have to
                if not ok or stored.first == offset:
                        continue;
                # reassign the residue numbers
                cmd.alter("%s" % p, "resi=str(int(resi)-%s)" % str(int(stored.first)-offset))
                # update pymol

        cmd.rebuild()

def stucture_attribution(pdb_id, attributions_path, pdb_path, flag=False):
    '''
    '''
    data = np.load(attributions_path)
    attributions = data['data']
    kernels = data['kernels']
    labels = data['labels']
    offsets = data['offsets']
    ind = np.where(labels == pdb_id)
    #attribution = attributions[ind][0][:,-1]
    if flag:
        a_ = data["all_"][0]
        a_[a_<=0] = 0.0
        attribution = data['all_'][0] + data['all_'][1]
        attribution[attribution<=0] = 0.0
        attribution = a_ * attribution
    else:
        a_ = data["all_"][1]
        a_[a_<=0] = 0.0
        attribution = data['all_'][0] + data['all_'][1]
        attribution[attribution<=0] = 0.0
        attribution = a_ * attribution

    # Load PDB
    cmd.load(pdb_path+pdb_id[:-2]+'.pdb')
    cmd.split_chains(pdb_id[:-2])
    for name in cmd.get_names('objects', 0, '(all)'):
        if not name.endswith(pdb_id[-1].upper()) and name.startswith(pdb_id[:4]):
            cmd.delete(name)
        else: zero_residues(name)
    cmd.reset()

    cmd.color('white', pdb_id)
    for i, _ in enumerate(attribution):
        #if flag: _ = _ *-1
        cmd.select('toBecolored', pdb_id+ ' and res ' + str(i+offsets[ind][0]))
        cmd.set_color('saliency'+str(i)+pdb_id, list(cmap(norm(_)))[:3])
        #else: cmd.set_color('saliency'+str(i)+pdb_id, list(cmap2(norm(_)))[:3])
        cmd.color('saliency'+str(i)+pdb_id, 'toBecolored')

    cmd.select('selected', pdb_id)
    #cmd.show('mesh', 'selected')
    #cmd.show('sticks', 'selected')
    cmd.deselect()

def kernels(pdb_id, data_path, pdb_path, flag=False):
    '''
    '''
    data = np.load(data_path)
    kernels = data['kernels']
    labels = data['labels']
    offsets = data['offsets']
    ind = np.where(labels == pdb_id)
    kernels = kernels[ind][0][0]

    # Load PDB
    cmd.load(pdb_path+pdb_id[:-2]+'.pdb')
    cmd.split_chains(pdb_id[:-2])
    for name in cmd.get_names('objects', 0, '(all)'):
        if not name.endswith(pdb_id[-1].upper()) and name.startswith(pdb_id[:4]):
            cmd.delete(name)
        else: zero_residues(name)
    cmd.reset()

    pdb_id2 = pdb_id+'kernel'
    cmd.create(pdb_id2, pdb_id)
    cmd.delete(pdb_id)

    '''
    cmd.hide('everything',pdb_id)
    cmd.show_as('lines', pdb_id + ' and (name ca or name c or name n)')
    cmd.set('line_width', 5)
    cmd.set_bond('line_width', 5,  pdb_id + ' and (name ca or name c or name n)')
    cmd.show('spheres', pdb_id + ' and name ca')
    cmd.set('sphere_transparency', 0.0, pdb_id + ' and name ca')
    cmd.set('sphere_scale', 0.5, pdb_id + ' and name ca')
    '''

    cmd.hide('everything',pdb_id2)
    cmd.show('spheres', pdb_id2 + ' and name ca')
    cmd.set('sphere_transparency', 0.9, pdb_id2 + ' and name ca')

    for i, _ in enumerate(kernels):
        print(_)
        cmd.set('sphere_scale', _/(1.7*2), pdb_id2+ ' and res ' + str(i+offsets[ind][0])+ ' and name ca')
    cmd.deselect()

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

# Paths
data_path = '../../out/krashras_graph_new/interpret/attributions.npz'
pdb_path = '../../data/KrasHras/pdb/'

# Load Attribution
kras = ['5tb5_c', '4dsn_a', '3gft_f', '5v6v_b', '4luc_b',
             '4m22_a', '4lv6_b', '4epy_a', '4lrw_b', '4epx_a']
             #'4m1w_a', '5uqw_a', '4pzy_b', '4m21_a', '5us4_a',
             #'5f2e_a', '4q03_a', '4lyh_c', '4m1o_b', '4pzz_a']
hras = ['1p2v_a', '4urz_r', '2x1v_a', '3lo5_c', '2quz_a',
            '3kud_a', '1aa9_a', '1plk_a', '4k81_d', '5wdq_a']
            #'1iaq_a', '1xd2_a', '3i3s_r', '4efl_a', '4l9w_a',
            #'3lo5_a', '5b2z_a', '1nvv_q', '4efm_a', '3l8z_a']

cmd.reinitialize()
cmd.bg_color('black')
for _ in kras:
    #kernels(_, data_path, pdb_path)
    stucture_attribution(_, data_path, pdb_path)
for _ in hras:
    #kernels(_, data_path, pdb_path)
    stucture_attribution(_, data_path, pdb_path, True)

# Align and Translate
for _ in kras[1:]:
    cmd.align(_,  kras[0])
for _ in hras:
    cmd.align(_,  kras[0])
cmd.orient()

for i, _ in enumerate(kras):
    cmd.rotate([1.0,0.0,0.0],60,selection=_[:-1]+_[-1].upper())
    cmd.translate([0,50*0,0],selection=_[:-1]+_[-1].upper())

for i, _ in enumerate(hras):
    cmd.rotate([1.0,0.0,0.0],60,selection=_[:-1]+_[-1].upper())
    cmd.translate([-60,50*0,00],selection=_[:-1]+_[-1].upper())

cmd.reset()


import subprocess, os
# spam = '''
# godot --export "Windows Desktop" builds/win/SwingWing

# '''
# butler push directory user/game:channel
def read_config():
    out = []
    with open('export_presets.cfg') as f:
        text = f.read()
        start = 0
        while True:
            idx = text.find("\nname=", start)
            if idx == -1:
                return out
            start = idx + len("\nname=")
            end = text.find("\n", start)
            name = text[start+1:end-1]
            idx = text.find("\nexport_path=", start)
            start = idx + len("\nexport_path=")
            end = text.find("\n", start)
            export_path = text[start+1:end-1]
            out.append((name, export_path,))

def deploy():
    targets = read_config()
    lookup = {
        "Windows Desktop": "win",
        "Linux/X11": "linux",
        "Mac OSX": "osx",
        "HTML5": "html5"
    }
    for name, path in targets:
        comm = ["godot", "--export", name, path]
        #subprocess.run(comm)
        path, _ = os.path.split(path)
        channel = lookup[name]
        comm = f"butler push {path} etmm/swing-wing-redux:{channel}".split()
        subprocess.run(comm)

if __name__ == "__main__":
    deploy()


from flask import Flask, render_template, request
import pickle
from pandas import DataFrame
from sklearn.base import BaseEstimator
from sklearn.pipeline import Pipeline
from feature_engine.encoding import OneHotEncoder
from feature_engine.wrappers import SklearnTransformerWrapper
from sklearn.preprocessing import MinMaxScaler

###########################################################################################
#preprocessing pipeline
cat_features=['Fuel_Type', 'Seller_Type', 'Transmission']

pipe = Pipeline(steps=[

    ('one hot encoder', OneHotEncoder(variables=cat_features)),

    ('min max scaler', SklearnTransformerWrapper( variables = ['Kms_Driven'],
                                                 transformer=MinMaxScaler(feature_range=(0, 100) ) ) )

])
###########################################################################################
with open("pipe.pkl", "rb") as f:
	preprocessor = pickle.load(f)

with open("best_model.pkl", "rb") as f:
    model = pickle.load(f)

def predict_price(ex):
    columns = ['Year','Present_Price','Kms_Driven','Fuel_Type','Seller_Type', 'Transmission','Owner']
    df = DataFrame([ex], columns=columns)
    x = preprocessor.transform(df)
    y = model.predict(x)
    return round(y[0],2)
###################################################################################
app = Flask(__name__)

######################################################
@app.route("/", methods=['GET'])
def home():
   return render_template("home.html")

#######################################################
@app.route("/result", methods = ["GET","POST"])
def predict():
	if request.method == 'POST':
		Year = int(request.form.get('year'))
		Year = 2021 - Year
		Present_Price = float(request.form.get('pr_price'))
		Kms_Driven = float(request.form.get('kms'))
		Fuel_Type = request.form.get('fuel')
		Seller_Type = request.form.get('seller')
		Transmission = request.form.get('trans')
		Owner = int(request.form.get('owner'))

		x = [Year, Present_Price, Kms_Driven, Fuel_Type, Seller_Type, Transmission, Owner]
		y = predict_price(x)

		return render_template("result.html", price = y)
	else:
		return render_template("home.html")

#######################################################
if __name__ == '__main__':
	app.run(debug=True)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Rhythmic feature extraction"""

import numpy as np

from .. import util

from ..core.audio import autocorrelate
from ..core.spectrum import stft
from ..util.exceptions import ParameterError
from ..filters import get_window

__all__ = ["tempogram", "fourier_tempogram"]

# -- Rhythmic features -- #
def tempogram(
    y=None,
    sr=22050,
    onset_envelope=None,
    hop_length=512,
    win_length=384,
    center=True,
    window="hann",
    norm=np.inf,
):
    """Compute the tempogram: local autocorrelation of the onset strength envelope. [#]_

    .. [#] Grosche, Peter, Meinard Müller, and Frank Kurth.
        "Cyclic tempogram - A mid-level tempo representation for music signals."
        ICASSP, 2010.

    Parameters
    ----------
    y : np.ndarray [shape=(n,)] or None
        Audio time series.

    sr : number > 0 [scalar]
        sampling rate of ``y``

    onset_envelope : np.ndarray [shape=(n,) or (m, n)] or None
        Optional pre-computed onset strength envelope as provided by
        `librosa.onset.onset_strength`.

        If multi-dimensional, tempograms are computed independently for each
        band (first dimension).

    hop_length : int > 0
        number of audio samples between successive onset measurements

    win_length : int > 0
        length of the onset autocorrelation window (in frames/onset measurements)
        The default settings (384) corresponds to ``384 * hop_length / sr ~= 8.9s``.

    center : bool
        If `True`, onset autocorrelation windows are centered.
        If `False`, windows are left-aligned.

    window : string, function, number, tuple, or np.ndarray [shape=(win_length,)]
        A window specification as in `stft`.

    norm : {np.inf, -np.inf, 0, float > 0, None}
        Normalization mode.  Set to `None` to disable normalization.

    Returns
    -------
    tempogram : np.ndarray [shape=(win_length, n) or (m, win_length, n)]
        Localized autocorrelation of the onset strength envelope.

        If given multi-band input (``onset_envelope.shape==(m,n)``) then
        ``tempogram[i]`` is the tempogram of ``onset_envelope[i]``.

    Raises
    ------
    ParameterError
        if neither ``y`` nor ``onset_envelope`` are provided

        if ``win_length < 1``

    See Also
    --------
    fourier_tempogram
    librosa.onset.onset_strength
    librosa.util.normalize
    librosa.stft

    Examples
    --------
    >>> # Compute local onset autocorrelation
    >>> y, sr = librosa.load(librosa.ex('nutcracker'), duration=30)
    >>> hop_length = 512
    >>> oenv = librosa.onset.onset_strength(y=y, sr=sr, hop_length=hop_length)
    >>> tempogram = librosa.feature.tempogram(onset_envelope=oenv, sr=sr,
    ...                                       hop_length=hop_length)
    >>> # Compute global onset autocorrelation
    >>> ac_global = librosa.autocorrelate(oenv, max_size=tempogram.shape[0])
    >>> ac_global = librosa.util.normalize(ac_global)
    >>> # Estimate the global tempo for display purposes
    >>> tempo = librosa.beat.tempo(onset_envelope=oenv, sr=sr,
    ...                            hop_length=hop_length)[0]

    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots(nrows=4, figsize=(10, 10))
    >>> times = librosa.times_like(oenv, sr=sr, hop_length=hop_length)
    >>> ax[0].plot(times, oenv, label='Onset strength')
    >>> ax[0].label_outer()
    >>> ax[0].legend(frameon=True)
    >>> librosa.display.specshow(tempogram, sr=sr, hop_length=hop_length,
    >>>                          x_axis='time', y_axis='tempo', cmap='magma',
    ...                          ax=ax[1])
    >>> ax[1].axhline(tempo, color='w', linestyle='--', alpha=1,
    ...             label='Estimated tempo={:g}'.format(tempo))
    >>> ax[1].legend(loc='upper right')
    >>> ax[1].set(title='Tempogram')
    >>> x = np.linspace(0, tempogram.shape[0] * float(hop_length) / sr,
    ...                 num=tempogram.shape[0])
    >>> ax[2].plot(x, np.mean(tempogram, axis=1), label='Mean local autocorrelation')
    >>> ax[2].plot(x, ac_global, '--', alpha=0.75, label='Global autocorrelation')
    >>> ax[2].set(xlabel='Lag (seconds)')
    >>> ax[2].legend(frameon=True)
    >>> freqs = librosa.tempo_frequencies(tempogram.shape[0], hop_length=hop_length, sr=sr)
    >>> ax[3].semilogx(freqs[1:], np.mean(tempogram[1:], axis=1),
    ...              label='Mean local autocorrelation', basex=2)
    >>> ax[3].semilogx(freqs[1:], ac_global[1:], '--', alpha=0.75,
    ...              label='Global autocorrelation', basex=2)
    >>> ax[3].axvline(tempo, color='black', linestyle='--', alpha=.8,
    ...             label='Estimated tempo={:g}'.format(tempo))
    >>> ax[3].legend(frameon=True)
    >>> ax[3].set(xlabel='BPM')
    >>> ax[3].grid(True)
    """

    from ..onset import onset_strength

    if win_length < 1:
        raise ParameterError("win_length must be a positive integer")

    ac_window = get_window(window, win_length, fftbins=True)

    if onset_envelope is None:
        if y is None:
            raise ParameterError("Either y or onset_envelope must be provided")

        onset_envelope = onset_strength(y=y, sr=sr, hop_length=hop_length)

    else:
        # Force row-contiguity to avoid framing errors below
        onset_envelope = np.ascontiguousarray(onset_envelope)

    if onset_envelope.ndim > 1:
        # If we have multi-band input, iterate over rows
        return np.asarray(
            [
                tempogram(
                    onset_envelope=oe_subband,
                    hop_length=hop_length,
                    win_length=win_length,
                    center=center,
                    window=window,
                    norm=norm,
                )
                for oe_subband in onset_envelope
            ]
        )

    # Center the autocorrelation windows
    n = len(onset_envelope)

    if center:
        onset_envelope = np.pad(
            onset_envelope, int(win_length // 2), mode="linear_ramp", end_values=[0, 0]
        )

    # Carve onset envelope into frames
    odf_frame = util.frame(onset_envelope, frame_length=win_length, hop_length=1)

    # Truncate to the length of the original signal
    if center:
        odf_frame = odf_frame[:, :n]

    # Window, autocorrelate, and normalize
    return util.normalize(
        autocorrelate(odf_frame * ac_window[:, np.newaxis], axis=0), norm=norm, axis=0
    )

def fourier_tempogram(
    y=None,
    sr=22050,
    onset_envelope=None,
    hop_length=512,
    win_length=384,
    center=True,
    window="hann",
):
    """Compute the Fourier tempogram: the short-time Fourier transform of the
    onset strength envelope. [#]_

    .. [#] Grosche, Peter, Meinard Müller, and Frank Kurth.
        "Cyclic tempogram - A mid-level tempo representation for music signals."
        ICASSP, 2010.

    Parameters
    ----------
    y : np.ndarray [shape=(n,)] or None
        Audio time series.

    sr : number > 0 [scalar]
        sampling rate of ``y``

    onset_envelope : np.ndarray [shape=(n,)] or None
        Optional pre-computed onset strength envelope as provided by
        ``librosa.onset.onset_strength``.

    hop_length : int > 0
        number of audio samples between successive onset measurements

    win_length : int > 0
        length of the onset window (in frames/onset measurements)
        The default settings (384) corresponds to ``384 * hop_length / sr ~= 8.9s``.

    center : bool
        If `True`, onset windows are centered.
        If `False`, windows are left-aligned.

    window : string, function, number, tuple, or np.ndarray [shape=(win_length,)]
        A window specification as in `stft`.

    Returns
    -------
    tempogram : np.ndarray [shape=(win_length // 2 + 1, n)]
        Complex short-time Fourier transform of the onset envelope.

    Raises
    ------
    ParameterError
        if neither ``y`` nor ``onset_envelope`` are provided

        if ``win_length < 1``

    See Also
    --------
    tempogram
    librosa.onset.onset_strength
    librosa.util.normalize
    librosa.stft

    Examples
    --------
    >>> # Compute local onset autocorrelation
    >>> y, sr = librosa.load(librosa.ex('nutcracker'))
    >>> hop_length = 512
    >>> oenv = librosa.onset.onset_strength(y=y, sr=sr, hop_length=hop_length)
    >>> tempogram = librosa.feature.fourier_tempogram(onset_envelope=oenv, sr=sr,
    ...                                               hop_length=hop_length)
    >>> # Compute the auto-correlation tempogram, unnormalized to make comparison easier
    >>> ac_tempogram = librosa.feature.tempogram(onset_envelope=oenv, sr=sr,
    ...                                          hop_length=hop_length, norm=None)

    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots(nrows=3, sharex=True)
    >>> ax[0].plot(librosa.times_like(oenv), oenv, label='Onset strength')
    >>> ax[0].legend(frameon=True)
    >>> ax[0].label_outer()
    >>> librosa.display.specshow(np.abs(tempogram), sr=sr, hop_length=hop_length,
    >>>                          x_axis='time', y_axis='fourier_tempo', cmap='magma',
    ...                          ax=ax[1])
    >>> ax[1].set(title='Fourier tempogram')
    >>> ax[1].label_outer()
    >>> librosa.display.specshow(ac_tempogram, sr=sr, hop_length=hop_length,
    >>>                          x_axis='time', y_axis='tempo', cmap='magma',
    ...                          ax=ax[2])
    >>> ax[2].set(title='Autocorrelation tempogram')
    """

    from ..onset import onset_strength

    if win_length < 1:
        raise ParameterError("win_length must be a positive integer")

    if onset_envelope is None:
        if y is None:
            raise ParameterError("Either y or onset_envelope must be provided")

        onset_envelope = onset_strength(y=y, sr=sr, hop_length=hop_length)

    else:
        # Force row-contiguity to avoid framing errors below
        onset_envelope = np.ascontiguousarray(onset_envelope)

    # Generate the short-time Fourier transform
    return stft(
        onset_envelope, n_fft=win_length, hop_length=1, center=center, window=window
    )