from fenics import *

from numpy import where

import numpy as np

import matplotlib.pyplot as plt
import random as rnd
###############################################################################
from smt.sampling_methods import LHS
from emukit.model_wrappers.gpy_quadrature_wrappers import BaseGaussianProcessGPy, RBFGPy
from emukit.quadrature.kernels.integration_measures import IsotropicGaussianMeasure
from emukit.quadrature.kernels import QuadratureRBFIsoGaussMeasure
from emukit.quadrature.methods.vanilla_bq import VanillaBayesianQuadrature
import GPy
import matplotlib.pyplot as plt
from scipy.stats import truncnorm
import math
from emukit.core.loop.user_function import UserFunctionWrapper
import random as rnd
import seaborn as sns
from typing import Tuple, List, Optional
from scipy.stats import norm
import Gen_data3

# Optimization options for the form compiler
parameters["form_compiler"]["cpp_optimize"] = True
ffc_options = {"optimize": True, \
               "eliminate_zeros": True, \
               "precompute_basis_const": True, \
               "precompute_ip_const": True}

class HyperElastic():

    def __init__(self,data):#,loadSteps, delta, verbosity, saveSolution):

        self.data = data
        self.J = len(data)
        self.N_MC = 30
        self.sigmaf = 0.5

        #self.loadSteps = loadSteps
        #self.delta = delta

        # Create mesh and define function space
        self.mesh = UnitCubeMesh(9, 9, 12)
        #mesh = BoxMesh(Point(0.0, 0.0, 0.0), Point(1., 4., 2.), 10, 10, 10)
        self.V = VectorFunctionSpace(self.mesh, "Lagrange", 1)

        # Mark boundary subdomians
        self.left =  CompiledSubDomain("near(x[0], side) && on_boundary", side = 0.0)
        self.right = CompiledSubDomain("near(x[0], side) && on_boundary", side = 1.0)

        self.top =  CompiledSubDomain("near(x[2], side) && on_boundary", side = 1.0)
        self.bottom = CompiledSubDomain("near(x[2], side) && on_boundary", side = 0.0)
        self.c = Constant((0.0, 0.0, 0.0))
        self.bc_bottom = DirichletBC(self.V, self.c, self.bottom) #boundary for bottom face = fixed
        # Find dofs on top face
        ff = MeshFunction("size_t",self.mesh, self.mesh.topology().dim()-1, 0)
        self.top.mark(ff, 1)
        vv = Function(self.V)
        bc = DirichletBC(self.V, Constant((0.0, 0.0,1.0)), ff, 1)
        bc.apply(vv.vector())
        self.top_dofs = where(vv.vector()==1.0)[0]

    def apply(self,data,theta, delta, loadSteps, verbosity = False, saveSolution = False):

        # Define functions
        du = TrialFunction(self.V)            # Incremental displacement
        v  = TestFunction(self.V)             # Test function
        u  = Function(self.V)                 # Displacement from previous iteration
        B  = Constant((0.0, 0.0, 0.0))  # Body force per unit volume
        T  = Constant((0.0,  0.0, 0.0))  # Traction force on the boundary

        # Kinematics
        d = u.geometric_dimension()
        I = Identity(d)             # Identity tensor
        F = I + grad(u)             # Deformation gradient
        C = F.T*F                   # Right Cauchy-Green tensor

        Cinv = inv(C)

        # Invariants of deformation tensors
        Ic = tr(C)
        J  = det(F)

        # Composite part

        e0 = Constant((1.0, 0.0, 0.0))
        n0 = Constant((0.0, 0.0, 1.0))

        L = dot(e0,C * e0)
        calL = dot(n0, Cinv * n0)

        # Invariants of deformation tensors
        I1 = tr(C) # This is the first invariant of C
        I2 = 0.5*(tr(C)**2 - tr(dot(C,C)))
        J  = det(F)
        I1_bar = J**(-2/3)*I1
        I2_bar = J**(-4/3)*I2
        ####################################################
        # Elasticity parameters
        #E, nu = 0.000001, 0.3
        #G, K = E/(2*(1 + nu)), E*nu/((1 + nu)*(1 - 2*nu))

        #theta_0 = Constant(theta[0])
        #theta_1 = Constant(theta[1])
        #Data_Driven_Term = theta_0 * ((1.0 - calL)**2 + theta_1 * (1.0 - calL)**4)

        #Es = Constant(120.0)
        #phi0 = Constant(0.45)
        #dWdl = 0.5 * As * (calL - sqrt(calL))/((1/phi0 - sqrt(calL)/phia)**4) + 0.5 * G * (1 / calL**2 - 1/calL)

        # Stored strain energy density (compressible neo-Hookean model)
        #psi = (G/2)*(I1 - 3) - G*ln(J) + (K/2)*(ln(J))**2 + 0.25 * Es * phi0 * (L - ln(L)) + Data_Driven_Term

        ####################################################
        # Elasticity parameters
        E, nu = 10.0, 0.3
        mu, lmbda = Constant(E/(2*(1 + nu))), Constant(E*nu/((1 + nu)*(1 - 2*nu)))

        # Stored strain energy density (compressible neo-Hookean model)

        #theta = [10.0, 1.0]
        normal_compaction_model = 0.5 * theta[0] * (calL - ln(calL))

        Es, phi0 = 120., 0.45
        axial_model = 0.25 * Es * phi0 * (L - ln(L))

        psi = (mu/2)*(Ic - 3) - mu*ln(J) + (lmbda/2)*(ln(J))**2 + normal_compaction_model+axial_model
        ####################################################
        W=0
        #C_10 = theta[0]
        #C_01 = theta[1]

        #W = C_01*(I2_bar-3) + C_10*(I1_bar - 3)

        # Total potential energy
        #Pi = W*dx - dot(B, u)*dx - dot(T, u)*ds
        Pi = psi*dx - dot(B, u)*dx - dot(T, u)*ds

        # Compute first variation of Pi (directional derivative about u in the direction of v)
        F = derivative(Pi, u, v)

        # Compute Jacobian of F
        J = derivative(F, u, du)

        # Loading loop
        count=0
        misfit = np.zeros(loadSteps)
        for i in range(1, loadSteps + 1):

                    if(verbosity):
                        print("Load step - " + str(i))

                    # Update non-homogeneous boundary condition for current load step
                    r = Constant((i * delta, 0.0, 0.0))
                    bc_top = DirichletBC(self.V, r, self.top)
                    bcs = [self.bc_bottom, bc_top]

                    # Solve variational problem
                    solve(F == 0, u, bcs, J=J)

                    # Save solution in VTK format
                    if(saveSolution):
                        file = File("displacement_" + str(i) + ".pvd");
                        file << u;

                    # Output forces
                    y = assemble(F)
                    Force_top = 0
                    for i in self.top_dofs:
                        Force_top += y[i]

                    print(Force_top)
                    misfit[count] = (data[count] - Force_top)
                    count=count+1

        Phi=self.evaluateDensity(misfit)       #number 0 is nothing
        return np.exp(Phi)

    def evaluateDensity(self, misfit):
        return -0.5 * np.sum(misfit)**2 / (self.sigmaf**2)
##############################################################################################################
    def press_fem(self) -> Tuple[UserFunctionWrapper, List[Tuple[float, float]]]:

        integral_bound = [(1,15),(0.2,0.4)]
        return UserFunctionWrapper(self._press_fem), integral_bound

    def _press_fem(self, data,param:np.ndarray, delta,loadSteps, verbosity, saveSolution) -> np.ndarray:
        Likelihood=self.apply(data, param, delta,loadSteps, verbosity, saveSolution)
        return np.reshape(np.array(Likelihood),(-1,1))
##############################################################################################################
    def singleGP(self, data, delta,loadSteps, verbosity, saveSolution):
        #########################################
        #xlimits = np.array([[1,60], [0,10]])   #for polynomial Mooney Rivlin
        xlimits = np.array([[15,25], [0,5]])
        sampling = LHS(xlimits=xlimits)

        train_number =200
        theta = sampling(train_number)
        ########################################

        self.X_init=np.zeros((train_number,2))
        Y=[]
        for i in range(0,train_number):                #initial points to GP
                function_input=theta[i]
                self.X_init[i]=theta[i]
                Y_= np.reshape(user_function.f(data, function_input, delta,loadSteps, verbosity, saveSolution),(-1,1))
                Y.append(Y_)

        self.Y_init=np.reshape(np.array(Y),(-1,1))

        gpy_model = GPy.models.GPRegression(X=self.X_init, Y=self.Y_init,kernel=GPy.kern.Matern52(input_dim=self.X_init.shape[1], ARD=False))

        gpy_model.optimize(messages=True)
        gpy_model.optimize_restarts(num_restarts = 1)

        return gpy_model

    def trainAll(self, delta, loadSteps, verbosity, saveSolution):

        self.GPs=[]

        for j in range(0,self.J):
            GP=self.singleGP(self.data[j],delta,loadSteps,verbosity,saveSolution)
            self.GPs.append(GP)
        return self.GPs

#################################################################################################################
    def theta_given_phi(self, phi, theta):
        return norm.logpdf(theta[0], loc=phi[0], scale=phi[1]) + norm.logpdf(theta[1], loc=phi[2], scale=phi[3])

    def logLike(self,data, phi, delta,loadSteps, verbosity, saveSolution):
        ll = 0.0
        for j in range(self.J): # For each experiment
            tmp = 0.0
            for i in range(self.N_MC):
                theta =np.array( [np.random.normal(phi[0],phi[1]),np.random.normal(phi[2],phi[3])]) # na balw abs(theta)
                gp_theta=np.reshape(theta,(1,-1))
                part1 =self.GPs[j].predict(gp_theta)
                part2  = self.theta_given_phi(phi, theta)
                tmp+=float(part1[0])  *part2                        # den douleuei me part2

            if tmp<=0:
                tmp=0.01
            ll += np.log(tmp/self.N_MC)
        return ll

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

# Imagine this is your data!
data = [-0.6538075772610137,
        -1.3600898575757168,
        -2.12445260813641,
        -2.9531997892903594,
        -3.853423265511568,
        -4.833097824627032,
        -5.901174910748575,
        -7.067660481777995]

N=3 #number of experiments
#def genData(real_data):
#    data = np.array(real_data) + rnd.normalvariate(0,0.005)
    #stress=stress.tolist()
#    return data

Data_i=[]
#for l in range(N): # Generate Synthetic data for each experiment
#    Data_i.append(genData(data))
data_realization = Gen_data3.Generation_data()

for l in range(N): # Generate Synthetic data for each experiment
    #theta_real =np.array( [np.random.normal(phi_real[0],phi_real[1]),np.random.normal(phi_real[2],phi_real[3])])
    #Data_i.append(data_realization.apply(theta_real,1,5,True,True))
    Data_i.append(data_realization.apply())

loadSteps = 10
delta = 0.02
saveSolution = True
verbosity = True

myModel = HyperElastic(Data_i)

# Test
#phi[0]=[0.9,0.01,0.1,0.1]
#theta =np.array( [np.random.normal(phi[0][0],phi[0][1]),np.random.normal(phi[0][2],phi[0][3])])
#kappa_keepo=myModel.apply(data,theta, delta, loadSteps, verbosity, saveSolution)

user_function, integral_bounds = myModel.press_fem()
######################################################
#check fig
#m=myModel.singleGP(Data_i[0],delta,loadSteps, verbosity, saveSolution)
#from IPython.display import display
#display(m)
#fig = m.plot()
#GPy.plotting.show(fig, filename='basic_gp_regression_notebook')
#####################################################

tr=myModel.trainAll(delta,loadSteps,verbosity,saveSolution)
'''
######################################################################################################
ndraws = 3000  # number of draws from the distribution
phi=np.zeros((ndraws,4))
#theta=np.zeros((1,2))
prop_phi=np.zeros((ndraws,4))
sigma1_sq=np.zeros((ndraws,1))
sigma2_sq=np.zeros((ndraws,1))
a=[1]
sigma1_sq[0]=1
sigma1_sq_it=1
k=0
ni=0
m=1.1
a_star=0.60
acceptanceCount=0
phi[0]=[20,1,1,0.1]

#likelihood,theta[0]=myModel.apply(phi[0], 1, 5, True, True)
#logLikelihood=np.log(likelihood)
#theta =np.array( [np.random.normal(phi[0][0],phi[0][1]),np.random.normal(phi[0][2],phi[0][3])])
#theta=abs(theta)
#logLikelihood = myModel.log_likelihood(Data_i,phi[0],1,5,True,True)
logLikelihood = myModel.logLike(Data_i,phi[0],delta,loadSteps, verbosity, saveSolution)

for it in range(1,ndraws):
    prop_phi[it][0]=np.sqrt(1-sigma1_sq_it**2)*phi[it-1][0] + 5*sigma1_sq_it*float(truncnorm.rvs(-1,1,size=1))
    prop_phi[it][1]=np.sqrt(1-sigma1_sq_it**2)*phi[it-1][1] + sigma1_sq_it*float(truncnorm.rvs(-1,1,size=1))
    prop_phi[it][2]=np.sqrt(1-sigma1_sq_it**2)*phi[it-1][2] + 0.3*sigma1_sq_it*float(truncnorm.rvs(-1,1,size=1))
    prop_phi[it][3]=np.sqrt(1-sigma1_sq_it**2)*phi[it-1][3] + 0.03*sigma1_sq_it*float(truncnorm.rvs(-1,1,size=1))

    prop_phi[it]=abs(prop_phi[it])

    #############################
#update the adaptation
    if it>100*(ni+1):
        k=k+1
        ni=ni+1
        #update of sigma_sq
        log_sigma1_sq=np.log(sigma1_sq[k-1]) + m**(-k)*(np.mean(np.array([a]))-a_star)
        sigma1_sq[k]=np.exp(log_sigma1_sq)
        #if sigma1_sq[k]>1:
        #    sigma1_sq[k]=0.99
        sigma1_sq_it=float(sigma1_sq[k])

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

    #prop_likelihood,prop_theta=myModel.apply(prop_phi[it], 1, 5, True, True)
    #prop_logLikelihood=np.log(prop_likelihood)

    #prop_logLikelihood = myModel.log_likelihood(Data_i,prop_phi[it],1,5,True,True)
    prop_logLikelihood = myModel.logLike(Data_i,prop_phi[it],delta,loadSteps, verbosity, saveSolution)

    alpha= prop_logLikelihood -logLikelihood
    alpha = min(0,alpha)

    if     np.log(rnd.random())<alpha:

           acceptanceCount=acceptanceCount+1
           phi[it]=prop_phi[it]
           #theta[it]=prop_theta
           #Likelihood=prop_Likelihood
           logLikelihood=prop_logLikelihood
           a=a+[1]
    else:
           phi[it]=phi[it-1]
           a=a+[0]
           #theta[it] = theta[it-1]

'''


import os
import sys
import json

import numpy as np
import tensorflow as tf

"""Tensorflow distributed training example
This code is retrived from https://www.tensorflow.org/tutorials/distribute/multi_worker_with_keras
"""

def mnist_dataset(batch_size):
  (x_train, y_train), _ = tf.keras.datasets.mnist.load_data()
  # The `x` arrays are in uint8 and have values in the [0, 255] range.
  # You need to convert them to float32 with values in the [0, 1] range.
  x_train = x_train / np.float32(255)
  y_train = y_train.astype(np.int64)
  train_dataset = tf.data.Dataset.from_tensor_slices(
      (x_train, y_train)).shuffle(60000).repeat().batch(batch_size)
  return train_dataset

def build_and_compile_cnn_model():
  model = tf.keras.Sequential([
      tf.keras.layers.InputLayer(input_shape=(28, 28)),
      tf.keras.layers.Reshape(target_shape=(28, 28, 1)),
      tf.keras.layers.Conv2D(32, 3, activation='relu'),
      tf.keras.layers.Flatten(),
      tf.keras.layers.Dense(128, activation='relu'),
      tf.keras.layers.Dense(10)
  ])
  model.compile(
      loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
      optimizer=tf.keras.optimizers.SGD(learning_rate=0.001),
      metrics=['accuracy'])
  return model

def main():
    per_worker_batch_size = 64
    tf_config = json.loads(os.environ['TF_CONFIG'])
    num_workers = len(tf_config['cluster']['worker'])

    strategy = tf.distribute.MultiWorkerMirroredStrategy()

    global_batch_size = per_worker_batch_size * num_workers
    multi_worker_dataset = mnist_dataset(global_batch_size)

    with strategy.scope():
        # Model building/compiling need to be within `strategy.scope()`.
        multi_worker_model = build_and_compile_cnn_model()

    multi_worker_model.fit(multi_worker_dataset, epochs=3, steps_per_epoch=70)

if __name__ == "__main__":
    worker_rank = sys.argv[1]
    workers = [*map(lambda x: f"{x}:12345", sys.argv[2:])]
    os.environ["TF_CONFIG"] = json.dumps({
    'cluster': {
        'worker': workers
    },
    'task': {'type': 'worker', 'index': worker_rank}
    })

    print("Hello from Python")
    print(workers, worker_rank)
    print(os.environ["TF_CONFIG"])
    main()


from collections import *
from functools import reduce
import operator

def process_data(input):
    return [[int(x) for x in num] for num in input]

def build_basin(input):
    basin = {}
    for i in range(len(input)):
        for j in range(len(input[0])):
            pt = (i,j)
            n = []
            if i - 1 >= 0 and input[i - 1][j] != 9: n.append((i - 1, j))
            if i + 1 < len(input) and input[i + 1][j] != 9: n.append((i + 1, j))
            if j - 1 >= 0 and input[i][j-1] != 9: n.append((i, j - 1))
            if j + 1 < len(input[0]) and input[i][j + 1] != 9: n.append((i, j + 1))
            basin[pt] = n
    return basin

def part1(input):
    output = 0
    for i in range(len(input)):
        for j in range(len(input[0])):
            if (i < len(input) - 1 and input[i][j] >= input[i + 1][j]) or \
		    input[i][j] >= input[i - 1][j] or \
            j > 0 and input[i][j] >= input[i][j - 1] or \
            (j < len(input[0]) - 1 and input[i][j] >= input[i][j + 1]):
                continue
            output += input[i][j] + 1
    return output

def part2(input):
    sizes = []
    basin = build_basin(input)
    traversed = set()
    for x in basin.keys():
        if x in traversed:
            continue
        size = 0
        lst = [x]
        while lst:
            node = lst.pop(0)
            for next in basin[node]:
                if next not in traversed:
                    traversed.add(next)
                    lst.append(next)
                    size += 1
        sizes.append(size)
    sorted_sizes = sorted(sizes)[::-1][:3]
    return reduce(operator.mul, (sorted_sizes))

data = process_data(open("day9.txt", "r").read().splitlines())
print(part1(data))
print(part2(data))


import unittest

from trainer import main, save_model, ComponentArguments, OutputDestinations
import os
import yaml
import shutil

class TestTrainer(unittest.TestCase):
    def setUp(self) -> None:
        contents_folder = os.path.join(
            os.path.dirname(__file__), "test_contents"
        )
        output_folder = os.path.join(os.path.dirname(__file__), "output")

        def load_yaml(path: str) -> str:
            with open(path, "r") as f:
                d = yaml.load(f)
                return yaml.dump(d)

        parameters = load_yaml(
            os.path.join(contents_folder, "test_parameters.yml")
        )
        self.args = ComponentArguments(
            train_data_path=os.path.join(contents_folder, "train.csv"),
            val_data_path=os.path.join(contents_folder, "val.csv"),
            test_data_path=os.path.join(contents_folder, "test.csv"),
            suffix="_test",
            parameters=parameters,
        )
        self.out_dest = OutputDestinations(output_folder)

    def tearDown(self) -> None:
        shutil.rmtree(self.out_dest.trained_model)

    def test_main(self):
        model = main(args=self.args)
        save_model(model=model, out_dest=self.out_dest)


# -*- coding: utf-8 -*-
# this file is released under public domain and you can use without limitations

from HTMLParser import HTMLParser
from re import sub
from sys import stderr
from traceback import print_exc

class _DeHTMLParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.__text = []

    def handle_data(self, data):
        text = data.strip()
        if len(text) > 0:
            self.__text.append(text)

    def handle_starttag(self, tag, attrs):
        if tag == 'br':
            self.__text.append('\n')
        if tag == 'h2':
            self.__text.append('\n')
        if tag == 'td':
            if self.__text[-1] != '\n':
                self.__text.append('\t')

    def handle_endtag(self, tag):
        if tag == 'tr':
            self.__text.append('\n')

    def text(self):
        return ''.join(self.__text).strip()

def dehtml(text):
    try:
        parser = _DeHTMLParser()
        parser.feed(text)
        parser.close()
        return parser.text()
    except:
        print_exc(file=stderr)
        return text

def get_years():
    import json

    my_syllabus = []

    if 'Autodetect course' not in request.vars.course:
        course = db(db.course.course == request.vars.course).select(db.course.id).first()
        syllabi = db(db.subject_course.course == course.id).select(db.subject_course.year, distinct=True)
        my_syllabus = [syllabus.year.year for syllabus in syllabi]

    return json.dumps(my_syllabus)

def index():
    import urllib

    courses = db(db.course.id > 0).select(orderby=db.course.course)
    my_course = ['Autodetect course']
    for course in courses: my_course.append(course.course)

    my_syllabus = ['Autodetect syllabus']

    form = SQLFORM.factory(
        Field('grades', 'text', label='Grades', requires=IS_NOT_EMPTY()),
        Field('course', 'list:string', requires=IS_IN_SET(my_course,labels=my_course, zero=None))
    )
    year = TR(LABEL('Curriculum'),SELECT('Autodetect curriculum', _id='no_table_curriculum', _name='curriculum', value='Autodetect curriculum'))
    form[0].insert(-1,year)

    contactme = SQLFORM(db.comments)

    subjects = None
    info = None
    mygrades = None
    mysubjects = None
    year = None
    pes = None
    cwts = None
    ges = None
    non_ges = None
    course = None
    notes = None
    c_notes = None
    changelog = db(db.changelog.id > 0).select(orderby=~db.changelog.timestamp, limitby=(0,5))

    if request.vars.bookmark:
        text = dehtml(urllib.unquote(request.vars.bookmark))
        form.vars.grades = text
        info,mygrades = parse(text)
        if info != 'Error':
            mysubjects,year,pes,cwts,ges,non_ges,course,notes,c_notes = parse_subjects(mygrades, info, 'Autodetect course', 'Autodetect curriculum')

    if form.accepts(request.vars, session, formname='form_one'):

        info,mygrades = parse(form.vars.grades)

        if info != 'Error':
            mysubjects,year,pes,cwts,ges,non_ges,course,notes,c_notes = parse_subjects(mygrades, info, form.vars.course,
                                         form.vars.curriculum)

    if contactme.accepts(request.vars, session, formname='form_two'):
        response.flash = 'Thank you for posting.'

    return dict(form=form, info=info, mygrades=mygrades, mysubjects=mysubjects, contactme=contactme,year=year,
            pes=pes,cwts=cwts,ges=ges,non_ges=non_ges,course=course,changelog=changelog,notes=notes,c_notes=c_notes)

def parse_subjects(mygrades, info, mycourse, myyear):

    courses = db(db.course.id > 0).select(orderby=db.course.course)

    if mycourse == 'Autodetect course':
        course = db(db.course.course == info['course']).select().first()
        if course is None:
            if 'Master ' in info['course']:
                return 'not BS',None,None,None,None,None,None,None,None
            else: return 'not supported',None,None,None,None,None,None,None,None
    else: course = db(db.course.course == mycourse).select().first()

    if myyear == 'Autodetect curriculum':
        studno = int(info['studno'][:4])
        syllabi = db(db.subject_course.course == course.id).select(db.subject_course.year, distinct=True)
        my_syllabus = [syllabus.year.year for syllabus in syllabi]
        year = filter(lambda x: x <= studno, my_syllabus)

        if len(year):
            syllabus = db(db.syllabus.year == max(year)).select().first()
        else: return 'too old',None,None,None,None,None,None,None,None

    else:
        syllabus = db(db.syllabus.year == int(myyear)).select().first()

        if len(db((db.subject_course.course == course.id) & (db.subject_course.year == syllabus.id)).select()) == 0:
            return 'not supported',None,None,None,None,None,None,None,None

    majors = db((db.subject_course.course == course.id) & (db.subject_course.year == syllabus.id))._select(db.subject_course.subject)

    major_subjects = db(db.subject.id.belongs(majors)).select()
    require = db(db.subject_requirements.course.contains(course.id)).select()
    prereqs = db((db.prerequisites.year == syllabus.id) & (db.prerequisites.course.contains(course.id))).select()
    coreqs = db((db.corequisites.year == syllabus.id) & (db.corequisites.course.contains(course.id))).select()

    notes = db(db.notes.course.contains(course.id)).select().first()
    c_notes = db(db.notes_curriculum.course.contains(course.id)).select().first()
    ges_domain = db(db.ge_domain.id > 0)._select(db.ge_domain.ge)
    ah = db(db.domains.domain == 'AH').select().first()
    ssp = db(db.domains.domain == 'SSP').select().first()
    mst = db(db.domains.domain == 'MST').select().first()
    ges_ah = db(db.ge_domain.domain == ah.id).select()
    ges_ssp = db(db.ge_domain.domain == ssp.id).select()
    ges_mst = db(db.ge_domain.domain == mst.id).select()
    ges = db(db.subject.id.belongs(ges_domain)).select()

    phil = db(db.phil_stud.id > 0)._select(db.phil_stud.ge)
    ges_phil_stud = db(db.subject.id.belongs(phil)).select()

    eng = db(db.english.id > 0)._select(db.english.ge)
    ges_english = db(db.subject.id.belongs(eng)).select()

    taken_majors = []
    taken_pes = []
    taken_non_majors = []
    taken_cwts = []
    taken_msts = []
    taken_ahs = []
    taken_ssps = []
    taken_phil_stud = []
    taken_english = []

    taken_mst = []
    taken_ah = []
    taken_ssp = []
    taken_phil = []
    taken_eng = []

    for sem in mygrades:
        semester = sem['sem'].replace('Semester', 'Sem').replace('20', '').replace('First','1st').replace('Second', '2nd')
        for subject in sem['subject']:
            subject['grade'] = subject['grade'].strip()
            if subject['subject'] == 'Bio 1': subject['subject'] = 'BIO 1'   #Fixed for Bio 1
            if ' III' in subject['subject']: subject['subject'] = subject['subject'].replace('III', '3')
            if ' II' in subject['subject']: subject['subject'] = subject['subject'].replace('II', '2')
            if ' I' in subject['subject']: subject['subject'] = subject['subject'].replace('I', '1')
            if subject['subject'] == 'Comm 3 Eng': subject['subject'] = 'Comm 3'   #Fixed for Comm 3
            if subject['subject'] == 'Humanidades 1': subject['subject'] = 'Humad 1' #Fixed for Humad 1

            blank = subject['grade'] == ''
            fail = '5.00' in subject['grade']
            ng = subject['grade'] == 'NG'
            inc = subject['grade'] == 'INC'
            four = subject['grade'] == '4.00'
            drp = subject['grade'] == 'DRP'
            f = 'F' in subject['grade']

            if not blank and not drp and not fail and not ng and not inc and not four and not f:
                if len(major_subjects.find(lambda row: row.name == subject['subject'])) != 0:
                    taken_majors.append(subject['subject'])
                elif subject['unit'].find('(') != -1:
                    if 'PE' in subject['subject']:
                        taken_pes.append({'subject':subject['subject'], 'sem':semester})
                    elif 'CWTS' in subject['subject']:
                        taken_cwts.append({'subject':subject['subject'], 'sem':semester})
                elif len(ges.find(lambda row: row.name == subject['subject'])) != 0:
                    ge = ges_ah.find(lambda row: row.ge.name == subject['subject']).first()
                    if ge is not None:
                        taken_ahs.append({'subject':subject['subject'], 'sem':semester, 'title':ge.ge.title, 'description':ge.ge.description})
                        taken_ah.append(subject['subject'])

                    ge = ges_mst.find(lambda row: row.ge.name == subject['subject']).first()
                    if ge is not None:
                        taken_msts.append({'subject':subject['subject'], 'sem':semester, 'title':ge.ge.title, 'description':ge.ge.description})
                        taken_mst.append(subject['subject'])

                    ge = ges_ssp.find(lambda row: row.ge.name == subject['subject']).first()
                    if ge is not None:
                        taken_ssps.append({'subject':subject['subject'], 'sem':semester, 'title':ge.ge.title, 'description':ge.ge.description})
                        taken_ssp.append(subject['subject'])

                    if len(ges_phil_stud.find(lambda row: row.name == subject['subject'])) != 0:
                        taken_phil_stud.append({'subject':subject['subject'], 'sem':semester})
                        taken_phil.append(subject['subject'])

                    if len(ges_english.find(lambda row: row.name == subject['subject'])) != 0:
                        taken_english.append({'subject':subject['subject'], 'sem':semester})
                        taken_eng.append(subject['subject'])

                else:
                    taken_non_majors.append({'subject':subject['subject'], 'sem':semester})

    if ('Math 53' in taken_majors or 'Math 100' in taken_majors) and 'Math 17' not in taken_majors: #APE
        if len(major_subjects.find(lambda row:row.name == 'Math 17')) != 0:
            taken_majors.append('Math 17')
            if 'Math 14' in taken_majors: taken_majors.remove('Math 14')
            if 'Math 11' in taken_majors: taken_majors.remove('Math 11')

            for subject in taken_non_majors:
                if subject['subject'] == 'Math 14':
                    taken_non_majors.remove(subject)
                    break
            for subject in taken_non_majors:
                if subject['subject'] == 'Math 11':
                    taken_non_majors.remove(subject)
                    break
        else:
            taken_majors.append('Math 11')
            taken_majors.append('Math 14')

    if 'Math 17' in taken_majors and len(major_subjects.find(lambda row:row.name == 'Math 17')) == 0:
        taken_majors.remove('Math 17')
        taken_majors.append('Math 14')
        taken_majors.append('Math 11')

        for subject in taken_non_majors:
            if subject['subject'] == 'Math 17':
                taken_non_majors.remove(subject)
                break

    taken_ges = {'mst':taken_msts, 'ah':taken_ahs, 'ssp':taken_ssps, 'phil_stud':taken_phil_stud,
                    'english':taken_english, 'others':taken_non_majors}

    eligible = []
    not_taken_majors = db(~db.subject.name.belongs(taken_majors) & db.subject.id.belongs(majors)).select()

    _msts = db(db.subject.name.belongs(taken_mst))._select(db.subject.id)
    not_taken_msts =  db(~db.ge_domain.ge.belongs(_msts) & (db.ge_domain.domain == mst.id)).select()
    _ssps = db(db.subject.name.belongs(taken_ssp))._select(db.subject.id)
    not_taken_ssps =  db(~db.ge_domain.ge.belongs(_ssps) & (db.ge_domain.domain == ssp.id)).select()
    _ahs = db(db.subject.name.belongs(taken_ah))._select(db.subject.id)
    not_taken_ahs =  db(~db.ge_domain.ge.belongs(_ahs) & (db.ge_domain.domain == ah.id)).select()
    _phil = db(db.subject.name.belongs(taken_phil))._select(db.subject.id)
    not_taken_phils =  db(~db.phil_stud.ge.belongs(_phil)).select()
    _eng = db(db.subject.name.belongs(taken_eng))._select(db.subject.id)
    not_taken_eng =  db(~db.english.ge.belongs(_eng)).select()

    not_taken_ges = {'mst':not_taken_msts, 'ssp':not_taken_ssps, 'ah':not_taken_ahs, 'phil_stud':not_taken_phils, 'english':not_taken_eng}

    mysubjects = {}
    mysubjects['elig'] = []
    mysubjects['non_elig'] = []
    mysubjects['senior'] = []

    for subject in not_taken_majors:
        if len(require.find(lambda row: row.subject == subject.id)) == 1:
            req = require.find(lambda row: row.subject == subject.id).first()
            mysubjects['senior'].append({'subject':subject.name, 'requirements':req.requirements})
        elif len(prereqs.find(lambda row: row.subject == subject.id)) == 0:
            coreq = coreqs.find(lambda row: row.subject == subject.id)
            if len(coreq):
                core = []
                for co in coreq:
                    core.append(co.coreq.name)
                mysubjects['elig'].append({'subject':subject.name, 'coreq':', '.join(core)})
            else: mysubjects['elig'].append({'subject':subject.name, 'coreq':''})
        else:
            prereq_sub = []
            prereq = prereqs.find(lambda row: row.subject.id == subject.id)
            count = 0

            for subj in prereq:
                if subj.prereq.name not in taken_majors: prereq_sub.append(subj.prereq.name)
                else: count = count + 1

            if count == len(prereq):
                coreq = coreqs.find(lambda row: row.subject == subject.id)

                if len(coreq):
                    core = []
                    for co in coreq:
                        core.append(co.coreq.name)
                    mysubjects['elig'].append({'subject':subject.name, 'coreq':','.join(core)})
                else: mysubjects['elig'].append({'subject':subject.name, 'coreq':''})
            else:
                coreq = coreqs.find(lambda row: row.subject == subject.id)
                if len(coreq):
                    core = []
                    for co in coreq:
                        core.append(co.coreq.name)
                    mysubjects['non_elig'].append({'subject':subject.name, 'prereq':', '.join(prereq_sub), 'coreq':', '.join(core)})
                else: mysubjects['non_elig'].append({'subject':subject.name, 'prereq':', '.join(prereq_sub), 'coreq':''})

    return mysubjects, syllabus.year, taken_pes, taken_cwts, taken_ges, not_taken_ges, course.course, notes, c_notes

def parse(grades):
    index = grades.find('View Grades')
    if index == -1: return 'Error',None
    grades = grades[index:]
    lines = grades.splitlines()

    if 'Load another student' in lines[1]: del lines[1]

    if len(lines) < 5: return 'Error',None

    name, studno, course = lines[1:4]

    info = {}
    info['name'] = name
    info['studno'] = studno
    info['course'] = course

    mygrades = []

    more_than_one = 0
    for line in lines[5:]:
        if 'Summer' in line or 'Semester' in line:
            mygrades.append({'sem':line})
            mygrades[-1]['subject'] = []
        elif more_than_one:
            l = line.split('\t')
            if len(l) == 4:
                unit, grade = l[-3], l[-2]
                mygrades[-1]['subject'][-1]['unit'] = unit
                mygrades[-1]['subject'][-1]['grade'] = grade
                more_than_one = 0
        elif 'Class Code' in line: continue
        elif len(line) == 0: continue
        elif 'NOTICE' in line: break
        elif 'Unresolved INC or 4.00' in line: break
        else:
            l = line.split('\t')
            if len(l) == 3:
                subj = l[1][:l[1].rfind(' ')]
                mygrades[-1]['subject'].append({'subject':subj})
                more_than_one = 1
            elif len(l) == 6:
                subj,unit, grade = l[1][:l[1].strip().rfind(' ')],l[-3], l[-2]
                mygrades[-1]['subject'].append({'subject':subj,'unit':unit,'grade':grade})
    return info, mygrades


import sqlite3, time, os
from datetime import datetime as dt
from datetime import timedelta

class Query:
    def __init__(self):
        self.conn = sqlite3.connect('C:/Users/wbmar/OneDrive/Documents/python_projects/options/options.db')
        self.c = self.conn.cursor()

    def get_data(self, metric, start, end):
        start_unix = time.mktime(dt.strptime(start, "%Y-%m-%d").timetuple())
        end_unix = time.mktime(dt.strptime(end, "%Y-%m-%d").timetuple())

        self.c.execute(f"SELECT date,{metric} FROM gme WHERE date BETWEEN {start_unix} AND {end_unix}")
        data = self.c.fetchall()
        self.conn.close()

        unix_list = [item[0] for item in data]
        date_list = [dt.fromtimestamp(int(ts)) for ts in unix_list] # Converts Unix timestamp to local datetime
        last_list = [item[1] for item in data]

        return(date_list, last_list)

    def suggest_dates(self):
        self.c.execute("SELECT MAX(date) FROM gme")
        ts = self.c.fetchone()[0] + 86400 # Adds 1 day to unix time
        self.conn.close()

        start = dt.utcfromtimestamp(ts).strftime('%Y-%m-%d')
        end = (dt.today()-timedelta(days=1)).strftime('%Y-%m-%d')

        return start, end


from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
import streamlit as st
import numpy as np
import pandas as pd

class Swg(object):

    def __init__(self,data):

        self.data = data

    def scale(self):
        df = self.data
        #Find Lastrow
        lastrow = len(df.index)
        SWG_Data = pd.read_csv("Dataset/SWG_Dataset.csv")
        SWG_Data = SWG_Data.drop(columns=['Start time','End time','Severity'])

        # SWG_Data.set_axis([read_columns], axis='columns', inplace=True)

        #concat data and dataset for Scaler
        df = pd.concat([df, SWG_Data],axis=0, ignore_index=True)
        # st.table(df.head())
        scl = StandardScaler()

        read_columns = ["UPPER_BUS_PHASE_A","UPPER_BUS_PHASE_B","UPPER_BUS_PHASE_C",
                "LOWER_BUS_PHASE_A","LOWER_BUS_PHASE_B","LOWER_BUS_PHASE_C",
                "OUTGOING_PHASE_A","OUTGOING_PHASE_B","OUTGOING_PHASE_C",
                "UPPER_BUS_PD", "LOWER_BUS_PD", "SPOUT_PD", "OUTGOING_PD"]

        X = scl.fit_transform(df[read_columns].values)

        if df.shape[1] == 9:

            y = df.iloc[:,8].values
            return X,y
        else:
            X = pd.DataFrame(X).iloc[:lastrow]
            return X

    def acc(self,pred,true): return accuracy_score(true,pred)


from sys import stdin
import heapq
from itertools import chain

dy = (-1,0,1,0)
dx = (0,-1,0,1)

n = int(stdin.readline())

board = [list(map(int, stdin.readline().split())) for _ in range(n)]

heap = []
dp = [[1]*n for _ in range(n)]

for i in range(n):
    for j in range(n):
        heapq.heappush(heap, (board[i][j] * -1, i, j))

while heap:
    num, y, x = heapq.heappop(heap)

    for i in range(4):
        ny, nx = y + dy[i], x + dx[i]
        if 0 <= ny < n and 0 <= nx < n and board[ny][nx] > board[y][x]:
            dp[y][x] = max(dp[y][x], dp[ny][nx] + 1)

print(max(chain(*dp)))


#!/usr/bin/env python
from unittest import mock

import mozunit
import pytest

from mozperftest.environment import METRICS
from mozperftest.metrics.utils import metric_fields
from mozperftest.tests.support import BT_DATA, EXAMPLE_TEST, get_running_env, temp_file
from mozperftest.utils import silence

def setup_env(options):
    mach_cmd, metadata, env = get_running_env(**options)
    runs = []

    def _run_process(*args, **kw):
        runs.append((args, kw))

    mach_cmd.run_process = _run_process
    metrics = env.layers[METRICS]
    env.set_arg("tests", [EXAMPLE_TEST])
    metadata.add_result({"results": str(BT_DATA), "name": "browsertime"})
    return metrics, metadata, env

@pytest.mark.parametrize("no_filter", [True, False])
@mock.patch("mozperftest.metrics.notebookupload.PerftestNotebook")
def test_notebookupload_with_filter(notebook, no_filter):

    options = {
        "notebook-metrics": [],
        "notebook-prefix": "",
        "notebook": True,
        "notebook-analysis": ["scatterplot"],
        "notebook-analyze-strings": no_filter,
    }

    metrics, metadata, env = setup_env(options)

    with temp_file() as output:
        env.set_arg("output", output)
        with metrics as m, silence():
            m(metadata)

    if no_filter:
        args, kwargs = notebook.call_args_list[0]
        assert type(kwargs["data"][0]["data"][0]["value"]) == str
    else:
        for call in notebook.call_args_list:
            args, kwargs = call
            for a in args:
                for data_dict in a:
                    for data in data_dict["data"]:
                        assert type(data["value"]) in (int, float)

    notebook.assert_has_calls(
        [mock.call().post_to_iodide(["scatterplot"], start_local_server=True)]
    )

@pytest.mark.parametrize("stats", [False, True])
@mock.patch("mozperftest.metrics.notebookupload.PerftestNotebook")
def test_compare_to_success(notebook, stats):
    options = {
        "notebook-metrics": [metric_fields("firstPaint")],
        "notebook-prefix": "",
        "notebook-analysis": [],
        "notebook": True,
        "notebook-compare-to": [str(BT_DATA.parent)],
        "notebook-stats": stats,
    }

    metrics, metadata, env = setup_env(options)

    with temp_file() as output:
        env.set_arg("output", output)
        with metrics as m, silence():
            m(metadata)

    args, kwargs = notebook.call_args_list[0]

    if not stats:
        assert len(kwargs["data"]) == 2
        assert kwargs["data"][0]["name"] == "browsertime- newest run"
        assert kwargs["data"][1]["name"] == "browsertime-results"
    else:
        assert any("statistics" in element["subtest"] for element in kwargs["data"])

    notebook.assert_has_calls(
        [mock.call().post_to_iodide(["compare"], start_local_server=True)]
    )

@pytest.mark.parametrize("filepath", ["invalidPath", str(BT_DATA)])
@mock.patch("mozperftest.metrics.notebookupload.PerftestNotebook")
def test_compare_to_invalid_parameter(notebook, filepath):
    options = {
        "notebook-metrics": [metric_fields("firstPaint")],
        "notebook-prefix": "",
        "notebook-analysis": [],
        "notebook": True,
        "notebook-compare-to": [filepath],
    }

    metrics, metadata, env = setup_env(options)

    with pytest.raises(Exception) as einfo:
        with temp_file() as output:
            env.set_arg("output", output)
            with metrics as m, silence():
                m(metadata)

    if filepath == "invalidPath":
        assert "does not exist" in str(einfo.value)
    else:
        assert "not a directory" in str(einfo.value)

if __name__ == "__main__":
    mozunit.main()


from gzip import READ
from operator import truediv
from optparse import AmbiguousOptionError
import uuid
from utils.helper import *

REWARD_VALUE = 25.0
NORMAL = 0
REWARD = 1

from blockchainActions.Signature import *

class Tx:
    def __init__(self, type=NORMAL):

        self.type = type
        self.inputs = []
        self.outputs = []
        self.sigs = []
        self.reqd = []
        self.status = []
        self.userId = []
        self.txid = uuid.uuid1()

    def add_input(self, from_addr, amount):
        self.inputs.append((from_addr, amount))

    def add_output(self, to_addr, amount):
        self.outputs.append((to_addr, amount))

    def add_userId(self, id):
        self.userId.append(id)

    def add_reqd(self, addr):
        self.reqd.append(addr)

    def add_status(self, st):
        self.status.append(st)

    def sign(self, private):
        message = self.__gather()
        newsig = sign(message, private)
        self.sigs.append(newsig)

    def is_valid(self):
        if self.type == REWARD:
            if len(self.inputs) != 0 and len(self.outputs) != 1:
                return False
            return True

        else:
            total_in = 0
            total_out = 0
            message = self.__gather()
            for addr, amount in self.inputs:
                found = False
                for s in self.sigs:
                    if verify(message, s, addr):
                        found = True
                if not found:
                    # print ("No good sig found for " + str(message))
                    return False
                if amount < 0:
                    return False
                total_in = total_in + amount
            for addr in self.reqd:
                found = False
                for s in self.sigs:
                    if verify(message, s, addr):
                        found = True
                if not found:
                    return False
            for addr, amount in self.outputs:
                if amount < 0:
                    return False
                total_out = total_out + amount

            if total_out > total_in:
                # print("Outputs exceed inputs")
                return False
            return True

    def __gather(self):
        data = []
        data.append(self.inputs)
        data.append(self.outputs)
        data.append(self.reqd)
        data.append(self.status)
        return data

    def __repr__(self):
        repr_str = "INPUTS:\n"
        for addr, amt in self.inputs:
            repr_str = repr_str + str(amt) + " from " + get_user_name_by_pub_key(pbcKey=addr.decode('utf-8')) + "\n"
            # repr_str = repr_str + str(amt) + "from" + str(addr) + "\n"

        repr_str += "OUTPUTS:\n"
        for addr, amt in self.outputs:
            repr_str = repr_str + str(amt) + " to " + get_user_name_by_pub_key(pbcKey=addr.decode('utf-8')) + "\n"
            # repr_str = repr_str + str(amt) + "to" + str(addr) + "\n"

        repr_str += "EXTRA REQUIRED SIGNATURES:\n"
        for req_sig in self.reqd:
            repr_str = repr_str + str(req_sig) + "\n"

        repr_str += "SIGNATURES:\n"
        for sig in self.sigs:
            repr_str = repr_str + str(sig) + "\n"

        repr_str += "Created by userID:\n"
        for id in self.userId:
            repr_str = repr_str + str(id) + "\n"

        repr_str += "END\n"

        return repr_str


#!/usr/bin/python

import collections
import random
import heapq

class PriorityQueue:
    def __init__(self):
        self.elements = []

    def is_empty(self):
        return len(self.elements) == 0

    def put(self, item, priority):
        heapq.heappush(self.elements, (priority, item))

    def get(self):
        return heapq.heappop(self.elements)[1]

    def contains(self, item):
        return not self.is_empty() and item in list(zip(*self.elements))[1]

def dfs(grid):

    # do the search
    result = dfs_iterative(grid)
    result2 = bfs_iterative(grid)
    result3 = ucs_iterative(grid)

    print ("Depth First Search")
    # print expanded nodes
    print (len(result['visited']))
    for pos in result['visited']:
        print (str(pos[0]) + ' ' + str(pos[1]))

    print ("Breath First Search")
    print (len(result2['visited']))
    for pos in result2['visited']:
        print (str(pos[0]) + ' ' + str(pos[1]))

    print ("Uniform Cost Search")
    path = reconstruct_path(finish_state, result3)
    print (len(path)-1)  # distance is -1
    for pos in path:
        print (str(pos[0]) + ' ' + str(pos[1]))

    # print path
    #path = reconstruct_path(finish_state, result['came_from'])
    #print (len(path)-1)  # distance is -1
    #for pos in path:
        #print (str(pos[0]) + ' ' + str(pos[1]))

# ---------------------------------------------------------------------------
# Depth First Search
#
# * take element from the fringe
# * if it's the goal -> success
# * otherwise add all of it's neighbours that have not yet been
#     looked at to the fringe
#
# Store the path by recording where we came from.
#   See: http://www.redblobgames.com/pathfinding/a-star/introduction.html
#   Alternative: store not only nodes but also their parents in visited
# ---------------------------------------------------------------------------

def dfs_iterative(grid):
    global finish_state
    fringe = collections.deque()  # nodes under consideration
    came_from = {}  # our closed set & node parent information
    visited = []  # only needed for hackerrank - print expanded nodes at end

    # start state
    start = (11, 11)
    fringe.append(start)
    came_from[start] = None

    while len(fringe) > 0:
        current = fringe.pop()  # depth first
        visited.append(current)

        if is_goal(current, grid):  # done?
            finish_state = current
            print (finish_state)
            return {'visited': visited, 'came_from': came_from}

        # expand all possible moves from current
        neighbours = get_neighbours(current, grid)
        for next in neighbours:

            # don't expand (or explore) nodes twice
            if next not in came_from and next not in fringe:
                came_from[next] = current
                fringe.append(next)

def bfs_iterative(grid):
    global finish_state
    fringe = collections.deque()  # nodes under consideration
    came_from = {}  # our closed set & node parent information
    visited = []  # only needed for hackerrank - print expanded nodes at end

    # start state
    start = (11, 11)
    fringe.append(start)
    came_from[start] = None

    while len(fringe) > 0:
        current = fringe.popleft()
        visited.append(current)

        if is_goal(current, grid):  # done?
            finish_state = current
            print (finish_state)
            return {'visited': visited, 'came_from': came_from}

        # expand all possible moves from current
        neighbours = get_neighbours(current, grid)
        for next in neighbours:

            # don't expand (or explore) nodes twice
            if next not in came_from and next not in fringe:
                came_from[next] = current
                fringe.append(next)

def ucs_iterative(grid):
    global finish_state
    fringe = PriorityQueue()   # nodes under consideration
    came_from = {}  # our closed set & node parent information
    cost_so_far = {} # only needed for hackerrank - print expanded nodes at end

    # start state
    start = (11, 11)
    fringe.put(start, 0)
    cost_so_far[start] = 0
    came_from[start] = None

    while not fringe.is_empty() > 0:
        current = fringe.get()

        if is_goal(current, grid):  # done?
            finish_state = current
            return came_from

        # expand all possible moves from current
        neighbours = get_neighbours(current, grid)
        for next in neighbours:

            new_cost = cost_so_far[current] + cost_of_move(next, grid)

            # don't expand (or explore) nodes twice
            if (next not in came_from and not fringe.contains(next)) or (new_cost < cost_so_far[next]):
                came_from[next] = current
                cost_so_far[next] = new_cost
                fringe.put(next, new_cost)

def cost_of_move(pos, grid):
    return 0 if is_goal(pos, grid) else 1

# gets all neighbours for a position on the grid, only returns valid moves
def get_neighbours(pos, grid):
    (x, y) = pos
    step = grid[pos[0]][pos[1]]
    neighbours = [(x-step, y  ),  # up
                  (x,   y-step),  # left
                  (x,   y+step),  # right
                  (x+step, y  )]  # down
    neighbours = filter(lambda x: is_passable(x, grid), neighbours)
    return neighbours

def is_passable(pos, grid):
    x,y = pos[0],pos[1]
    return (x >= 0 and x < 21) and (y >= 0 and y < 21)

def is_goal(pos, grid):
    x,y = pos[0],pos[1]
    return (x == 0 and y == 0) or (x == 0 and y == 20) or (x == 20 and y == 0) or ( x == 20 and y == 20)

def is_start(pos, grid):
    x,y = pos[0],pos[1]
    return x == 11 and y == 11

# build path from start to goal
def reconstruct_path(goal, came_from):
    current = goal
    path = [current]
    while not is_start(current, grid):
        print (current)
        current = came_from[current]
        path.append(current)
    return path[::-1]  # reverse

# PREDEFINED TEMPLATE CODE -----------------------------------------
start_state = (11,11)
finish_state = (0,0)
grid = [[random.randint(1,9) for x in range(21)] for x in range(21)]

dfs(grid)


from django.shortcuts import render
from .models import *

def index(request):
    if 'total_spent' not in request.session:
        request.session['total_spent'] = 0
    if 'products_ordered' not in request.session:
        request.session['products_ordered'] = 0
    context = {
        "all_products": Product.objects.all()
    }
    return render(request, "store/index.html", context)

def checkout(request):
    request.session['checkout_quantity'] = int(request.POST.get('quantity'))

    request.session['total_spent'] += (float(request.POST['price']) * request.session['checkout_quantity'])
    request.session['products_ordered'] += request.session['checkout_quantity']

    quantity_from_form = int(request.POST["quantity"])
    price_from_form = float(request.POST["price"])
    total_charge = quantity_from_form * price_from_form

    context = {
        'product_price': price_from_form,
        'quantity': quantity_from_form,
        'total_charge': total_charge
    }
    print("Charging credit card...")
    Order.objects.create(quantity_ordered=quantity_from_form, total_price=total_charge)
    return render(request, "store/checkout.html", context)


from collections import deque
def solution(targets):
    answer = 0
    targets = deque(sorted(targets, key = lambda x : x[1]))
    tmp = targets.popleft()
    shot = tmp[1] - 0.5
    while targets:
        tmp = targets.popleft()
        if shot < tmp[0]:
            answer += 1
            shot = tmp[1] - 0.5
    return answer + (shot > tmp[0])


import argparse
import os
import sys
import time
sys.path.append(os.path.abspath('../..'))
import pandas
import json
import modules.io as io
import modules.vascular_data as sv
import vtk
import numpy as np

parser = argparse.ArgumentParser()
parser.add_argument('-config')
parser.add_argument('-tube_file')
parser.add_argument('-output_fn')
args = parser.parse_args()

cfg = io.load_json(args.config)

DIR          = cfg['dir']
MESH_LABELS  = cfg['mesh_labels']
GENS         = cfg['generations']
NUM_MODELS   = cfg['num_models']
SIM_NAME     = cfg['sim_name']
RESULTS_FILE = cfg['results_file']

LABS = cfg['labels']
LABELS = []
for i in range(cfg['start'], cfg['end'], cfg['incr']):
    for l in LABS:
        new_l = str(i)
        if len(new_l) == 1:
            new_l = "0000"+new_l
        if len(new_l) == 2:
            new_l = "000"+new_l
        if len(new_l) == 3:
            new_l = "00"+new_l
        if len(new_l) == 4:
            new_l = "0"+new_l
        LABELS.append(l+'_'+new_l)

TUBE_FILE = io.load_json(args.tube_file)
POINTS    = TUBE_FILE['points']
NORMALS   = TUBE_FILE['normals']
RADIUSES  = TUBE_FILE['radiuses']
N         = range(len(POINTS))

data = []

for gen in GENS:
    for mesh in MESH_LABELS:
        for nm in range(NUM_MODELS):
            vtu_fn = DIR+'/'+str(gen)+'/'+mesh+'/'+str(nm)+'/'+SIM_NAME+'/'+RESULTS_FILE
            print(vtu_fn)
            if not os.path.exists(vtu_fn): continue

            pd = sv.read_vtu(vtu_fn)

            for j,p,n,r in zip(N, POINTS, NORMALS, RADIUSES):
                surf = sv.clip_plane_rad(pd,p,n,r)

                #try:
                area = sv.vtk_integrate_pd_area(surf)
                length = sv.vtk_integrate_pd_boundary_length(surf)

                for l in LABELS:
                    print(l)
                    ints    = sv.vtk_integrate_pd_volume(surf, l)
                    ints_bd = sv.vtk_integrate_pd_boundary(surf, l)

                    for k in range(len(ints)):
                        q = l.split('_')[0]
                        t = int(l.split('_')[1])
                        c = k

                        d = {"generation":gen,
                            "mesh":mesh,
                            "model":nm,
                            "point":j,
                            "time":t,
                            "component":c,
                             "x":p[0], "y":p[1], "z":p[2],
                             "nx":n[0],"ny":n[1],"nz":n[2],
                             "radius_actual":np.sqrt(area/np.pi),
                             "radius_supplied":r,
                             "area":area,
                             "length":length
                             }

                        d[q] = ints[k]*1.0/area
                        d[q+'_boundary'] = ints_bd[k]*1.0/length

                        data.append(d)
                #except:
                    #print("failed vtu {} point {}".format(i,j))

df = pandas.DataFrame(data)
df.to_csv(args.output_fn)


from django.db import models

# Create your models here.
class Team(models.Model):
    """ Model class for Teams details   """

    team_id = models.IntegerField (primary_key=True)
    name = models.CharField (max_length=200)
    logo_uri = models.ImageField (upload_to='pics')
    club_state = models.CharField(max_length=200)

    def __str__(self):
        return self.name

class Player(models.Model):
    """ Model class for Player details   """

    player_id = models.IntegerField (primary_key=True)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    image_uri = models.ImageField(upload_to='pics')
    jersey_number = models.PositiveIntegerField()
    country = models.CharField(max_length=100)
    team_id = models.ForeignKey(Team,on_delete = models.CASCADE)

class Matches(models.Model):

    """ Model class for Match Details """

    match_id = models.IntegerField(primary_key=True)
    teams = models.ManyToManyField(Team)
    stadium = models.CharField(max_length=200)
    location = models.CharField(max_length=200)
    winner = models.CharField(max_length=200)
    date = models.DateTimeField(auto_now=True)

class PlayerHistory(models.Model):

    """ Model class for Player performance record  """

    id = models.IntegerField (primary_key=True)
    player_id = models.ForeignKey(Player, on_delete=models.CASCADE)
    no_of_matches = models.PositiveIntegerField()
    total_run = models.PositiveIntegerField()
    highest_score = models.PositiveIntegerField()
    no_of_fifties = models.PositiveIntegerField()
    no_of_hundreds = models.PositiveIntegerField()

class Points(models.Model):

    """Model class for Team Points"""

    id = models.IntegerField(primary_key=True)
    team = models.OneToOneField(Team, on_delete=models.CASCADE)
    points = models.PositiveIntegerField()


from pyspark.sql import SparkSession
from pyspark.ml.feature import VectorAssembler
from pyspark.ml.classification import RandomForestClassifier,LinearSVC
import ast
from pyspark.ml import Pipeline
from pyspark.sql import functions as F, types
from pyspark.ml.evaluation import MulticlassClassificationEvaluator
import pandas as pd

spark = SparkSession \
    .builder \
    .appName("toxicity_classification") \
    .getOrCreate()
sc = spark.sparkContext

dfTrain = spark.read.csv('Data/FeaturizedDataTraining.csv',header=True)
column_names = dfTrain.schema.names[1:]
dfTrain = dfTrain.select(*(F.col(c).cast("float").alias(c) for c in column_names))
dfTrain = dfTrain.withColumn("label", dfTrain["label"].cast(types.IntegerType()))
dfVal = spark.read.csv('Data/FeaturizedDataValidation.csv',header=True)
column_names = dfVal.schema.names[1:]
dfVal = dfVal.select(*(F.col(c).cast("float").alias(c) for c in column_names))
dfVal = dfVal.withColumn("label", dfVal["label"].cast(types.IntegerType()))

dfTrain = dfTrain.withColumn('label',(dfTrain['label']>0).cast('integer'))
dfVal = dfVal.withColumn('label',(dfVal['label']>0).cast('integer'))

#Creating the data frame containing the training data
#pandas_df = pd.read_csv('Data/FeaturizedDataTraining.csv')
#pandas_df = pd.read_csv('Data/FeaturizedDataTrainingSample.csv')
#dfTrain = spark.createDataFrame(pandas_df)

#Creating the data frame containing the testing data
#pandas_df = pd.read_csv('Data/FeaturizedDataValidation.csv')
#pandas_df = pd.read_csv('Data/FeaturizedDataValidationSample.csv')
#dfVal = spark.createDataFrame(pandas_df)

#Creating the data frame containing the examples
pandas_df = pd.read_csv('Data/Examples.csv')
dfExample = spark.createDataFrame(pandas_df)

column_names = dfExample.schema.names[1:501] #column_names is the same for al three dfs

vec_assemb = VectorAssembler(inputCols=column_names, outputCol="Vecfeatures")
vec_assemb.transform(dfTrain)

rf = RandomForestClassifier(labelCol="label", featuresCol="Vecfeatures", numTrees=100)
lsvc = LinearSVC(maxIter=10, regParam=0.1, featuresCol="Vecfeatures")
pipeline = Pipeline(stages=[vec_assemb,rf ])
ML_model = pipeline.fit(dfTrain)
predictions = ML_model.transform(dfVal)

#evaluator = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="weightedRecall")
#recall = evaluator.evaluate(predictions)

evaluator = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="accuracy")
accuracy = evaluator.evaluate(predictions)

TP = predictions.where(predictions.label==1).where(predictions.prediction==1).count()
TN = predictions.where(predictions.label==0).where(predictions.prediction==0).count()
FP = predictions.where(predictions.label==0).where(predictions.prediction==1).count()
FN = predictions.where(predictions.label==1).where(predictions.prediction==0).count()
precision = TP/(TP+FP)
recall = TP/(TP+FN)

print('accuracy',accuracy)
print('recall:',recall)
print('precision',precision)

sample_pred = ML_model.transform(dfExample)

sample_pred.select('sentence', 'prediction').show()
ML_model.save("SavedSVMModel_rf_nt_100")

# dfTrain = spark.read.csv('Data/FeaturizedDataTraining.csv',header=True)
# column_names = dfTrain.schema.names[1:]
# dfTrain = dfTrain.select(*(F.col(c).cast("float").alias(c) for c in column_names))
# dfTrain = dfTain.withColumn("label", dfTrain["label"].cast(types.IntegerType()))

# dfTain = spark.read.csv('Data/FeaturizedDataTraining.csv',header=True)
# column_names = dfTain.schema.names[1:]
# dfTain = dfTain.select(*(F.col(c).cast("float").alias(c) for c in column_names))
# dfTain = dfTain.withColumn("label", dfTain["label"].cast(types.IntegerType()))


#!/usr/bin/env python3

'''A command-line implementation of the Tower of Hanoi game.

Rates solutions based on number of moves.'''
import random
import os

from pyfiglet import figlet_format

board = [[],[],[]]
moves = 0
number_of_disks = 3
disk_character = "#"

####################
# Set Up Interface #
####################

def clear_screen():
    '''Clears the screen.'''
    os.system('cls' if os.name == 'nt' else 'clear')

def logo():
    '''Displays the game logo.'''
    clear_screen()
    print(figlet_format('Hanoi', font='whimsy'))

def press_enter():
    '''Pauses the game until the player presses enter.'''
    input("\nPress ENTER to continue. ")

#################
# Game Specific #
#################

def finished():
    '''Checks if the game has been solved.'''
    if len(board[2]) == number_of_disks:
        return True
    return False

def new_game():
    '''Sets up a new game.'''
    global moves, board
    moves = 0
    board = [list(range(number_of_disks,0,-1)),[],[]]
    return True

def game_loop():
    '''Play the game.'''
    while not finished():
        logo()
        draw_board()
        action = input("> ").lower()
        if action == "q" or action == "quit":
            clear_screen()
            break
        elif action == "help" or action == "h":
            logo()
            print("HELP")
            print("Input the moves you'd like to make in the format below:\n\nx > y\n \nX is the column you want to take a stone from and\ny is the column you want to place it onto.\n\n2 > 1 moves the top stone from column 2 to column 1.")
            print("\nQuit exits the game and help brings up this menu.")
            press_enter()
        else:
            try:
                x, y = action.split(",")
            except ValueError:
                continue
            try:
                x = int(x.strip())
                y = int(y.strip())
            except ValueError:
                continue
            if x in range(1,4) and y in range(1,4):
                move(x, y)
    if finished():
        logo()
        draw_board()
        draw_stars(score_moves(moves,number_of_disks))
        print("Congratulations! You solved the puzzle!")
        press_enter()
    return True

def score_moves(moves, n):
    '''Calculate scores depending on the number of moves.'''
    best_moves = 2**n - 1
    stars = 1
    proportion = (moves - best_moves) / best_moves
    if proportion <= 0.2:
        stars = 3
    elif proportion <= 0.4:
        stars = 2
    return stars

def move(from_column, to_column):
    '''Moves a disk from one column to another.'''
    global board, moves
    to_column -=1
    from_column -=1
    valid = True
    if len(board[from_column]) == 0:
        valid = False
    if valid and (len(board[to_column]) == 0 or board[from_column][-1] < board[to_column][-1]):
        print("valid)")
        moves += 1
        board[to_column].append(board[from_column].pop())
    else:
        print("That is not a valid move!")
        press_enter()
        return False
    return True

########
# Menu #
########

def show_menu():
    '''Shows the game menu.'''
    while True:
        logo()
        print("Play\nHelp\nQuit")
        action = input("> ").lower()
        if action == "q" or action == "quit":
            clear_screen()
            break
        elif action == "play" or action == "p":
            new_game()
            game_loop()
        elif action == "help" or action == "h":
            draw_help()

def draw_help():
    '''Shows the help page.'''
    logo()
    print("HELP")
    print("Input the moves you'd like to make in the format below:\n\nx,y\n \nX is the column you want to take a stone from and\ny is the column you want to place it onto.\n\nExample: 2,1 moves the top stone from column 2 to column 1.")
    print("\nQUIT exits the game.\nHELP brings up this menu.")
    press_enter()

###########
# Drawing #
###########

def draw_board():
    '''Draws the game display.'''
    #print(board)
    whole = number_of_disks * 2 + 2
    mid = whole//2
    rest = whole - mid+1
    display = "=="
    for x in range(3):
        display += "="*mid + str(x+1) + "="*rest
    display += "="
    for row in range(number_of_disks+1):
        newstr = ""
        for column in range(3):
            try:
                drawing = disk_character*board[column][row]
            except IndexError:
                drawing = "|"
            else:
                drawing += "|" + drawing
            newstr += "   " + drawing.center(number_of_disks*2+1)
        display = newstr + "\n" + display
    print("Move all of the disks onto the last peg.")
    print("\n"+display)
    if not finished():
        print("\nEnter your move.\nExample:\n1, 2 moves the top disk from column 1 to column 2.\n")
        print("QUIT to quit. HELP for help")

def draw_stars(stars):
    star = [
        "    .    ",
        " __.'.__ ",
        "'-.   .-'",
        "  /.'.\  ",
        "  '   '  "
    ]
    print("")
    for line in star:
        linetext = line;
        count = stars
        while count >= 2:
            linetext += " " + line;
            count -= 1
        linetext = linetext.center(40)
        print(linetext)

###################
# Run the Program #
###################

show_menu()


#!/usr/bin/env python3

from __future__ import print_function, division, absolute_import
from os import path

import docopt
import sys
import screed

__author__ = "lteasnail"

CLI_ARGS = """
USAGE:
alignment_summerise.py [options]

OPTIONS:
 -t STRING        String to count

counts the number of each base pair
"""

def GC_caluator(filename):
    GC_dict = {}
    for seq in screed.open(filename):
        seq_name = seq.name
        seq = seq.sequence
        length = len(seq)
        base_count = {}
        for base in seq:
            base_count[base] = base_count.get(base, 0) + 1
        parp = base_count['C'] + base_count['G']
        GC_content = parp/length
        GC_dict[seq_name] = GC_content
    return GC_dict

# If I am being run as a script...
if __name__ == '__main__':
    opts = docopt.docopt(CLI_ARGS)
    fasta_file = opts['-t']
    GC_diction = GC_caluator(fasta_file)
    max_GC = 0
    max_name = ''
    for key in GC_diction:
        if GC_diction[key] > max_GC:
            max_GC = GC_diction[key]
            max_name = key
    max_GC = GC_diction[max_name] * 100
    print("{} {}".format(max_name, max_GC), file=sys.stderr)


# -*- coding:utf-8 -*-
from absl import flags
import json
import math
import pickle as cPickle
import random
import sys

import numpy as np
import tensorflow as tf
import modeling, xlnet, model_utils
import sentencepiece as spm
from prepro_utils import preprocess_text, encode_ids
from tensorflow.contrib import rnn, crf
from tensorflow.contrib.crf import crf_log_likelihood
from tensorflow.contrib.layers.python.layers import initializers

from utils import f1_score, format_result, get_tags, format_tags, new_f1_score

# Model
flags.DEFINE_float("dropout", default=0.1,
      help="Dropout rate.")
flags.DEFINE_float("dropatt", default=0.1,
      help="Attention dropout rate.")
flags.DEFINE_integer("clamp_len", default=-1,
      help="Clamp length.")
flags.DEFINE_bool("use_bfloat16", default=False,
      help="Whether to use bfloat16.")

# Parameter initialization
flags.DEFINE_enum("init", default="normal",
                  enum_values=["normal", "uniform"],
                  help="Initialization method.")
flags.DEFINE_float("init_std", default=0.02,
                   help="Initialization std when init is normal.")
flags.DEFINE_float("init_range", default=0.1,
                   help="Initialization std when init is uniform.")

# TPUs and machines
flags.DEFINE_bool("use_tpu", default=False, help="whether to use TPU.")

# Training
flags.DEFINE_integer("train_batch_size", default=1,
                     help="batch size for training")
flags.DEFINE_integer("train_steps", default=8000,
                     help="Number of training steps")
flags.DEFINE_integer("warmup_steps", default=0, help="number of warmup steps")
flags.DEFINE_integer("save_steps", default=None,
                     help="Save the model for every save_steps. "
                     "If None, not to save any model.")
flags.DEFINE_integer("max_save", default=5,
                     help="Max number of checkpoints to save. "
                     "Use 0 to save all.")
flags.DEFINE_integer("shuffle_buffer", default=2048,
                     help="Buffer size used for shuffle.")

# Optimization
flags.DEFINE_float("learning_rate", default=3e-5, help="initial learning rate")
flags.DEFINE_float("min_lr_ratio", default=0.0,
                   help="min lr ratio for cos decay.")
flags.DEFINE_float("clip", default=1.0, help="Gradient clipping")
flags.DEFINE_float("weight_decay", default=0.00, help="Weight decay rate")
flags.DEFINE_float("adam_epsilon", default=1e-6, help="Adam epsilon")
flags.DEFINE_string("decay_method", default="poly", help="poly or cos")
flags.DEFINE_float("lr_layer_decay_rate", default=0.75,
                   help="Top layer: lr[L] = FLAGS.learning_rate."
                   "Lower layers: lr[l-1] = lr[l] * lr_layer_decay_rate.")

flags.DEFINE_string("data_dir", default="data/", help="tran, dev and test data dir", )
flags.DEFINE_string("xlnet_config", default="chinese_xlnet_base_L-12_H-768_A-12/xlnet_config.json", help="xlnet config file dir")
flags.DEFINE_string("init_checkpoint", default="chinese_xlnet_base_L-12_H-768_A-12/xlnet_model.ckpt", help="xlnet model init checkpoint")
flags.DEFINE_string("spm", default="chinese_xlnet_base_L-12_H-768_A-12/spiece.model", help="spiece model file")
flags.DEFINE_string("entry", default="train", help="operation")

FLAGS = flags.FLAGS

class Model():
    def __init__(self):
        self.nums_tags = 4
        self.lstm_dim = 128
        self.embedding_size = 50
        self.max_epoch = 10
        self.global_steps = tf.Variable(0, trainable=False)
        self.best_dev_f1 = tf.Variable(0.0, trainable=False)
        self.checkpoint_dir = "./model/"
        self.checkpoint_path = "./model/ner.ckpt"
        self.initializer = initializers.xavier_initializer()

        self.is_training = True if FLAGS.entry=="train" else False

    def __creat_model(self):

        # xlnet embbeding layer
        self._init_xlnet_placeholder()
        self.xlnet_layer()

        # bi-Lstm layer
        self.biLSTM_layer()

        # logits_layer
        self.logits_layer()

        # loss_layer
        self.loss_layer()

        # crf_layer
        self.crf_layer()

        # optimizer_layer
        self.xlnet_optimizer_layer()

    def _init_xlnet_placeholder(self):
        self.input_ids = tf.placeholder(
            dtype=tf.int32,
            shape=[None, None],
            name="xlnet_input_ids"
        )
        self.input_mask = tf.placeholder(
            dtype=tf.float32,
            shape=[None, None],
            name="xlnet_input_mask"
        )
        self.segment_ids = tf.placeholder(
            dtype=tf.int32,
            shape=[None, None],
            name="xlnet_segment_ids"
        )
        self.targets = tf.placeholder(
            dtype=tf.int32,
            shape=[None, None],
            name="xlnet_targets"
        )

        self.dropout = tf.placeholder(
            dtype=tf.float32,
            shape=None,
            name="xlnet_dropout"
        )
        used = tf.sign(tf.abs(self.input_ids))
        length = tf.reduce_sum(used, reduction_indices=1)
        self.length = tf.cast(length, tf.int32)
        self.nums_steps = tf.shape(self.input_ids)[-1]

    def xlnet_layer(self):
        xlnet_config = xlnet.XLNetConfig(json_path = FLAGS.xlnet_config)
        run_config = xlnet.create_run_config(self.is_training, True, FLAGS)

        xlnet_model = xlnet.XLNetModel(
            xlnet_config = xlnet_config,
            run_config = run_config,
            input_ids = self.input_ids,
            seg_ids = self.segment_ids,
            input_mask = self.input_mask)

        self.embedded = xlnet_model.get_sequence_output()
        self.model_inputs = tf.nn.dropout(
            self.embedded, self.dropout
        )

    def embedding_layer(self):
        with tf.variable_scope("embedding_layer") as scope:
            sqart3 = math.sqrt(3)

            self.embedding_matrix = tf.get_variable(
                name="embedding_matrix",
                shape=[self.input_size, self.embedding_size],
                initializer=self.initializer,
                dtype=tf.float32,
            )

            self.embedded = tf.nn.embedding_lookup(
                self.embedding_matrix, self.inputs
            )

            self.model_inputs = tf.nn.dropout(
                self.embedded, self.dropout
            )

    def biLSTM_layer(self):
        with tf.variable_scope("bi-LSTM") as scope:
            lstm_cell = {}
            for direction in ["forward", "backward"]:
                with tf.variable_scope(direction):
                    lstm_cell[direction] = rnn.LSTMCell(
                        num_units=self.lstm_dim,
                    )

            outputs, final_states = tf.nn.bidirectional_dynamic_rnn(
                cell_fw=lstm_cell['forward'],
                cell_bw=lstm_cell['backward'],
                inputs=self.model_inputs,
                sequence_length=self.length,
                dtype=tf.float32,
            )
            self.lstm_outputs = tf.concat(outputs, axis=2)

    def logits_layer(self):
        with tf.variable_scope("hidden"):
            w = tf.get_variable("W", shape=[self.lstm_dim*2, self.lstm_dim],
                                dtype=tf.float32, initializer=self.initializer
                                )
            b = tf.get_variable("b", shape=[self.lstm_dim], dtype=tf.float32,
                                initializer=self.initializer
                                )

            output = tf.reshape(self.lstm_outputs, shape=[-1, self.lstm_dim*2])
            hidden = tf.tanh(tf.nn.xw_plus_b(output, w, b))
            self.hidden = hidden

        with tf.variable_scope("logits"):
            w = tf.get_variable("W", shape=[self.lstm_dim, self.nums_tags],
                                initializer=self.initializer, dtype=tf.float32
                                )
            self.test_w = w
            b = tf.get_variable("b", shape=[self.nums_tags], dtype=tf.float32)
            self.test_b = b
            pred = tf.nn.xw_plus_b(hidden, w, b)
            self.logits = tf.reshape(
                pred, shape=[-1, self.nums_steps, self.nums_tags])

    def loss_layer(self):
        with tf.variable_scope("loss_layer"):
            logits = self.logits
            targets = self.targets

            self.trans = tf.get_variable(
                "transitions",
                shape=[self.nums_tags, self.nums_tags],
                initializer=self.initializer
            )

            log_likelihood, self.trans = crf_log_likelihood(
                inputs=logits,
                tag_indices=targets,
                transition_params=self.trans,
                sequence_lengths=self.length
            )
            self.loss = tf.reduce_mean(-log_likelihood)

    def crf_layer(self):
        # CRF decode, pred_ids 是一条最大概率的标注路径
        self.pred_ids, _ = crf.crf_decode(potentials=self.logits, transition_params=self.trans, sequence_length=self.length)

    def xlnet_optimizer_layer(self):
        correct_prediction = tf.equal(
            tf.argmax(self.logits, 2), tf.cast(self.targets, tf.int64))
        self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        FLAGS.train_steps = int(
            self.train_length / FLAGS.train_batch_size * self.max_epoch)
        FLAGS.warmup_steps = int(FLAGS.train_steps * 0.1)

        self.train_op, self.learning_rate, _ = model_utils.get_train_op(FLAGS, self.loss)
        self.saver = tf.train.Saver(tf.global_variables(), max_to_keep=5)

    def xlnet_step(self, sess, batch):
        try:
            ntokens, tag_ids, inputs_ids, segment_ids, input_mask = zip(*batch)
        except ValueError:
            print("ValueError: not enough values to unpack")
            return 0, 0, 0, 0, 0

        feed = {
            self.input_ids: inputs_ids,
            self.targets: tag_ids,
            self.segment_ids: segment_ids,
            self.input_mask: input_mask,
            self.dropout: 0.5
        }
        embedding, global_steps, loss, _, logits, acc, length = sess.run([self.embedded, self.global_steps, self.loss, self.train_op, self.logits, self.accuracy, self.length], feed_dict=feed)
        return global_steps, loss, logits, acc, length

    def train(self):
        from xlnet_data_utils import XLNetDataUtils
        sp_model = spm.SentencePieceProcessor()
        sp_model.Load(FLAGS.spm)

        self.train_data = XLNetDataUtils(sp_model, batch_size=8)
        self.dev_data = XLNetDataUtils(sp_model, batch_size=8)
        self.dev_batch = self.dev_data.iteration()

        data = {
            "batch_size": self.train_data.batch_size,
            "input_size": self.train_data.input_size,
            "vocab": self.train_data.vocab,
            "tag_map": self.train_data.tag_map,
        }

        f = open("model/data_map.pkl", "wb")
        cPickle.dump(data, f)
        f.close()
        self.batch_size = self.train_data.batch_size
        self.nums_tags = len(self.train_data.tag_map.keys())
        self.tag_map = self.train_data.tag_map
        self.train_length = len(self.train_data.data)

        # save vocab
        print("-"*50)
        print("train data:\t", self.train_length)
        print("nums of tags:\t", self.nums_tags)

        self.__creat_model()
        with tf.Session() as sess:
            with tf.device("/gpu:0"):
                ckpt = tf.train.get_checkpoint_state(self.checkpoint_dir)
                if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
                    print("restore model")
                    self.saver.restore(sess, ckpt.model_checkpoint_path)
                else:
                    sess.run(tf.global_variables_initializer())

                    tvars = tf.trainable_variables()
                    (assignment_map, initialized_variable_names) = model_utils.get_assignment_map_from_checkpoint(tvars, FLAGS.init_checkpoint)
                    tf.train.init_from_checkpoint(FLAGS.init_checkpoint, assignment_map)
                    for var in tvars:
                        init_string = ""
                        if var.name in initialized_variable_names:
                            init_string = ", *INIT_FROM_CKPT*"
                        print("  name = %s, shape = %s%s", var.name, var.shape,
                                        init_string)

                for i in range(self.max_epoch):
                    print("-"*50)
                    print("epoch {}".format(i))

                    steps = 0
                    for batch in self.train_data.get_batch():
                        steps += 1
                        global_steps, loss, logits, acc, length = self.xlnet_step(sess, batch)
                        if steps % 1 == 0:
                            print("[->] step {}/{}\tloss {:.2f}\tacc {:.2f}".format(
                                steps, len(self.train_data.batch_data), loss, acc))
                        if steps % 100 == 0:
                            self.xlnet_evaluate(sess, "ORG")
                            self.xlnet_evaluate(sess, "PER")
                            self.xlnet_evaluate(sess, "LOC")
                        if steps % 1000 == 0:
                            self.saver.save(sess, self.checkpoint_path)
                    self.saver.save(sess, self.checkpoint_path)

    def xlnet_evaluate(self, sess, tag):
        result = []
        trans = self.trans.eval()
        batch = self.dev_batch.__next__()

        ntokens, tag_ids, inputs_ids, segment_ids, input_mask = zip(*batch)
        feed = {
            self.input_ids: inputs_ids,
            self.segment_ids: segment_ids,
            self.targets: tag_ids,
            self.input_mask: input_mask,
            self.dropout: 1
        }
        pre_paths, acc, lengths = sess.run([self.pred_ids, self.accuracy, self.length], feed_dict=feed)

        tar_paths = tag_ids
        recall, precision, f1 = f1_score(tar_paths, pre_paths, tag, self.tag_map)
        best = self.best_dev_f1.eval()
        if f1 > best:
            print("\tnew best f1:")
            print("\trecall {:.2f}\t precision {:.2f}\t f1 {:.2f}".format(recall, precision, f1))
            tf.assign(self.best_dev_f1, f1).eval()

    def prepare_xlnet_pred_data(self, text):
        text.replace('…', '.')
        text.replace('℃', 'C')
        text_preprocessed = preprocess_text(text)
        input_ids = encode_ids(self.sp_model, text_preprocessed)
        input_mask = [1] * len(input_ids)
        segment_ids = [0] * len(input_ids)

        feed = {
            self.input_ids: [input_ids],
            self.segment_ids: [segment_ids],
            self.input_mask: [input_mask],
            self.dropout: 1
        }
        return feed

    def predict(self):
        f = open("model/data_map.pkl", "rb")
        maps = cPickle.load(f)
        f.close()
        self.batch_size = 1
        self.sp_model = spm.SentencePieceProcessor()
        self.sp_model.Load(FLAGS.spm)
        self.train_length = 10

        self.tag_map = maps.get("tag_map", {})
        self.nums_tags = len(self.tag_map.values())
        self.__creat_model()
        with tf.Session() as sess:
            ckpt = tf.train.get_checkpoint_state(self.checkpoint_dir)
            if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
                print("[->] restore model")
                self.saver.restore(sess, ckpt.model_checkpoint_path)
            else:
                print("[->] no model, initializing")
                sess.run(tf.global_variables_initializer())

            trans = self.trans.eval()
            while True:
                text = input(" > ")

                feed = self.prepare_xlnet_pred_data(text)

                paths, length = sess.run([self.pred_ids, self.length], feed_dict=feed)

                print(format_tags(paths[0], self.tag_map))
                org = get_tags(paths[0], "ORG", self.tag_map)
                org_entity = format_result(org, text, "ORG")
                per = get_tags(paths[0], "PER", self.tag_map)
                per_entity = format_result(per, text, "PER")
                loc = get_tags(paths[0], "LOC", self.tag_map)
                loc_entity = format_result(loc, text, "LOC")

                resp = org_entity["entities"] + per_entity["entities"] + loc_entity["entities"]
                print(json.dumps(resp, indent=2, ensure_ascii=False))

def main(_):
    model = Model()
    if FLAGS.entry == "train":
        model.train()
    elif FLAGS.entry == "predict":
        model.predict()

if __name__ == "__main__":
    tf.app.run()


"""Defines preprocessing filters which can be applied by preprocessing.get_data.

Kyle Roth. 2019-02-12.
"""

import librosa
import numpy as np
from scipy import fft
from scipy import signal

# used by mel function
_mel = librosa.filters.mel(16000, 2048, n_mels=80)  # linear transformation, (80,1025)

def mel(X, y):
    """Mel spectrogram preprocessing."""
    out = []
    for x in X:
        spectrum = np.log(np.abs(fft(x))[:len(x)//2])
        spectrum = signal.resample(spectrum, 1025)
        x_mel = np.dot(_mel, spectrum)
        out.append(x_mel)
    X = np.array(out)
    y = np.array(y)

    # remove NaNs
    is_nan = np.isnan(X).any(axis=1)
    X, y = X[~is_nan], y[~is_nan]

    return X, y

def resample(data, y):
    """Resample audio to 800 points."""
    out = []
    for thing in data:
        out.append(signal.resample(thing, 800))
    return np.array(out), y

def get_filter(name):  # pylint: disable=too-many-return-statements
    """Get the preprocessor specified by the name given.

    Args:
        name (str): name of model constructor to be called.
    Returns:
        (callable): model constructor to be called.
    """
    name = name.lower()
    if name == 'mel':
        return mel
    if name == 'resample':
        return resample
    raise ValueError("filter must be one of {mel, resample}")


import sqlite3
from tkinter import *

def createtable():
    conn = sqlite3.connect("Database.db")
    cur = conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS data(Id INTEGER PRIMARY KEY ,Date TEXT,Earning INTEGER,Exercise TEXT,Study TEXT,Diet TEXT,python TEXT)")
    conn.commit()
    conn.close()

def insert_data(Date,Earning,Exercise,Study,Diet,Python):
    conn = sqlite3.connect("Database.db")
    cur = conn.cursor()
    cur.execute("INSERT INTO data VALUES(NULL, ?,?,?,?,?,?)",(Date,Earning,Exercise,Study,Diet,Python))
    conn.commit()
    conn.close()

def view():
    conn = sqlite3.connect("Database.db")
    cur = conn.cursor()
    cur.execute("SELECT * FROM data")
    rows = cur.fetchall()
    conn.commit()
    conn.close()
    return rows

def delete(id):
    conn = sqlite3.connect("Database.db")
    cur = conn.cursor()
    cur.execute("DELETE FROM data WHERE Id = ?", (id,))
    conn.commit()
    conn.close()

def search(date='' , earnings='' , exercise='' , study='' , diet='' , python=''):
    conn = sqlite3.connect('Database.db')
    cur = conn.cursor()
    cur.execute("SELECT * FROM data WHERE Date=?  OR Earning=? OR Exercise=? OR Study=? OR Diet=? OR Python=?" , (date , earnings , exercise , study , diet , python))
    rows = cur.fetchall()
    conn.commit()
    conn.close()
    return rows

createtable()


import sys
sys.path.extend([
    "./",
])

import os
import torch
from tqdm import tqdm
import time
import numpy as np
import argparse
import umap
import yaml
import json
import pandas as pd
import re
import plotly.express as px
import seaborn as sns
import matplotlib.pyplot as plt

import src.custom_metrics.preparation as pp
from src.general_utils import util_data
from src.general_utils import util_path
from src.custom_metrics import features
from src.general_utils import util_general
from src.general_utils import util_reports

from src.data_util import Dataset_

def get_parser():

    parser = argparse.ArgumentParser()

    parser.add_argument("-cfg", "--cfg_file", type=str, default="./src/configs/")
    parser.add_argument('--source_dir', type=str, default='./reports/pneumoniamnist', help='Directory name to fake samples.') #/home/lorenzo/GAN-Ensembles/reports/
    parser.add_argument('--dataset_name', type=str, default='pneumoniamnist', choices=['pneumoniamnist'],  help='The name of dataset')
    parser.add_argument('--gpu_ids', type=str, default='1', help='gpu ids: e.g. 0  use -1 for CPU')
    parser.add_argument('--batch_size', type=int, default='64', help='Batch size')
    parser.add_argument('--gan_models', type=util_general.parse_comma_separated_list, default='ACGAN-Mod-ADC,SAGAN,ReACGAN-ADA', help='List of GANs to enable in the ensemble')
    parser.add_argument('--gan_steps', type=util_general.parse_comma_separated_list, default='100000', help='Iter or Iters to sample each GAN')
    parser.add_argument('--gan_steps_ensemble', type=util_general.parse_comma_separated_list, default='100000', help='Iter or Iters to sample each GAN')
    parser.add_argument('--gan_models_ensemble', type=util_general.parse_comma_separated_list, default='ACGAN-Mod-ADC,SAGAN', help='List of GANs to enable in the ensemble')
    parser.add_argument('--gan_steps_ensemble', type=util_general.parse_comma_separated_list, default='100000', help='Iter or Iters to sample each GAN')
    parser.add_argument('--gan_models_steps_ensemble', type=util_general.parse_comma_separated_list, default=None, help='Combinations models_steps.')
    parser.add_argument("--post_resizer", type=str, default="friendly", help="which resizer will you use to evaluate GANs in ['legacy', 'clean', 'friendly']")
    parser.add_argument('--eval_backbone', type=str, default='ResNet50_torch', help="[InceptionV3_torch, ResNet50_torch, SwAV_torch, resnet_ae_50_pneumoniamnist, disc_resnet_50_pneumoniamnist, cnn_resnet_50_pneumoniamnist")
    parser.add_argument('--n_samples', type=int, default='50000',  help='Total number of images generated by the ensemble.')
    parser.add_argument('--split', type=str, default='train', choices=['train', 'val'])

    return parser

# main
if __name__ == "__main__":

    parser = get_parser()
    args, unknown = parser.parse_known_args()

    with open(args.cfg_file) as file:
        cfg = yaml.load(file, Loader=yaml.FullLoader)

    # Directories.
    split= args.split
    source_dir = args.source_dir
    dataset_name = args.dataset_name
    samples_dir = os.path.join(source_dir, 'samples')
    cache_dir = os.path.join(source_dir, 'features')
    reports_dir = os.path.join(source_dir, 'umap_embeddings')
    util_path.create_dir(reports_dir)

    # Parameters.
    eval_backbone= args.eval_backbone
    post_resizer= args.post_resizer
    n_samples = args.n_samples
    batch_size = args.batch_size
    n_max_dim_red = np.inf

    # Device.
    gpu_ids = int(args.gpu_ids)
    print(f'gpu_ids: {gpu_ids}')

    if gpu_ids >= 0:
        # Check if available.
        if not torch.cuda.is_available():
            raise ValueError('GPU specified but not available.')
        device = torch.device('cuda:{}'.format(gpu_ids))
    else:
        device = torch.device('cpu')

    # GANs
    gan_models_steps = args.gan_models_steps
    if len(gan_models_steps) != 0:
        gan_models = [x.split('_')[0] for x in gan_models_steps]
        gan_steps = [x.split('_')[1] for x in gan_models_steps]
    else:
        gan_models = args.gan_models
        gan_steps = args.gan_steps
    gan_models_steps_ensemble = args.gan_models_steps_ensemble
    if len(gan_models_steps) != 0:
        gan_models_ensemble = [x.split('_')[0] for x in gan_models_steps_ensemble]
        gan_steps_ensemble = [x.split('_')[1] for x in gan_models_steps_ensemble]
    else:
        gan_models_ensemble = args.gan_models_ensemble
        gan_steps_ensemble = args.gan_steps_ensemble
    gan_aval = os.listdir(samples_dir)
    filename = f'umap_{split}_{eval_backbone}_{post_resizer}_{n_samples}'

    # Create the entire path for each gan.
    gan_folders = [os.path.join(root_dir, x, f'fake__{split}') for x in gan_aval if any(f'{gan_model}-train-' in x for gan_model in gan_models)]
    gan_folders = [os.path.join(x, f"step={y}") for x in gan_folders for y in gan_steps]

    gan_folders_ensemble = [os.path.join(samples_dir, x, 'fake') for x in gan_aval if any(y in x for y in gan_models_ensemble)]
    gan_folders_ensemble = [os.path.join(x, f"step={y}") for x in gan_folders_ensemble for y in gan_steps_ensemble]

    eval_model = pp.LoadEvalModel(
        eval_backbone=eval_backbone,
        post_resizer=post_resizer,
        device=device
    )
    tot_comb = len(gan_folders)

    real_dataset = Dataset_(data_name=cfg['DATA']['name'],
                            data_dir=cfg['DATA']['data_dir'],
                            train=True if split == 'train' else False,
                            crop_long_edge=cfg['PRE']['crop_long_edge'],
                            resize_size=cfg['PRE']['resize_size'],
                            resizer=cfg['PRE']['pre_resizer'],
                            random_flip=cfg['PRE']['apply_rflip'],
                            normalize=cfg['PRE']['normalize'],
                            cfgs=cfg)
    n_max_dim_red = min(n_max_dim_red, len(real_dataset))  # take the minimum between n_max_dim_red and the length of real dataset

    real_loader = torch.utils.data.DataLoader(real_dataset, batch_size=batch_size, shuffle=False)

    # real
    print('Load feature real.')
    stats1 = features.compute_feature(
        dataloader=real_loader,
        eval_model=eval_model,
        batch_size=batch_size,
        quantize=True,
        device=device,
        cache_dir=cache_dir + f'/real_{split}',
        capture_all=True,
        capture_mean_cov=True
    )
    feat_real = stats1.get_all()

    # Main cycle.
    feat_all = []
    y_all = []
    feat_all.append(feat_real)
    y_all.append(np.zeros(feat_real.shape[0], dtype=int))
    gan_names = {
       'real': 0
    }

    # Compute features for all possible GANs.
    with tqdm(total=tot_comb) as pbar:
        for idx, gan_sel in enumerate(gan_folders):

            print("\n")
            print(gan_sel)

            tik = time.time()
            # Dataset
            gan0_dataset = util_data.EnsembleDataset(folders=[gan_sel], weights=[1.0])

            # Dataloder
            dataloader0 = torch.utils.data.DataLoader(gan0_dataset, batch_size=batch_size, shuffle=False)

            # Compute the features representations.
            stats0 = features.compute_feature(
                dataloader=dataloader0,
                eval_model=eval_model,
                batch_size=batch_size,
                quantize=True,
                device=device,
                cache_dir=cache_dir + gan_sel.split('samples')[-1],
                max_items=n_samples,
                capture_all=True,
                capture_mean_cov=True
            )
            feat_synth = stats0.get_all()
            feat_synth = feat_synth[:n_max_dim_red]  # select only n_max_dim_red samples
            feat_all.append(feat_synth)
            y_all.append(np.full(feat_synth.shape[0], idx + 1, dtype=int))
            gan_names[gan_sel.split('samples')[-1]] = idx + 1

    with open(os.path.join(reports_dir, f'{filename}_gan_label_map.txt'), "w") as f:
        json.dump(gan_names, f, indent=4)

    feat_real = feat_all[0]
    feat_real = np.array(feat_real).astype(np.float32)
    y_real = y_all[0]
    y_real = np.array(y_real).astype(np.int32)

    feat_all_gans = feat_all[1:]
    feat_all_gans = np.concatenate(feat_all_gans, axis=0).astype(np.float32)
    y_all_gans = y_all[1:]
    y_all_gans = np.concatenate(y_all_gans, axis=0).astype(np.int32)

    feat_ensemble = [feat_all[gan_names.get(x.split('samples')[-1])] for x in gan_folders_ensemble]
    feat_ensemble = np.concatenate(feat_ensemble, axis=0).astype(np.float32)
    y_ensemble = [y_all[gan_names.get(x.split('samples')[-1])] for x in gan_folders_ensemble]
    y_ensemble = np.concatenate(y_ensemble, axis=0).astype(np.int32)

    # Fit umap.
    print('Fit umap.')
    reducer = umap.UMAP(random_state=42)
    output_name = filename + '_' + 'fit_real'
    transformer = reducer.fit(feat_real)

    # Project latent points.
    print('Project latent points.')
    embedding_real = transformer.transform(feat_real)
    embedding_all_gans = transformer.transform(feat_all_gans)
    embedding_ensemble = transformer.transform(feat_ensemble)

    # Create a dataframe from embeddings.
    df_real = pd.DataFrame(embedding_real, columns=['umap-1', 'umap-2'])
    df_real['exp'] = 'real'

    df_naive = pd.DataFrame(embedding_all_gans, columns=['umap-1', 'umap-2'])
    df_naive['exp'] = 'naive'

    df_DGE = pd.DataFrame(embedding_ensemble, columns=['umap-1', 'umap-2'])
    df_DGE['exp'] = 'DGE'

    # Concatenate all these dataframes
    df = pd.concat([df_real, df_naive, df_DGE], ignore_index=True)

    # Save to the disk.
    df.to_excel(os.path.join(reports_dir, f'{filename}.xlsx'), index=False)
    df_real.to_excel(os.path.join(reports_dir, f'{filename}_real.xlsx'), index=False)
    df_naive.to_excel(os.path.join(reports_dir, f'{filename}_naive.xlsx'), index=False)
    df_DGE.to_excel(os.path.join(reports_dir, f'{filename}_DGE.xlsx'), index=False)

    print('May the force be with you.')


#imports
import os
import cv2
import subprocess
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from register_user import register_user
from user_recognition import user_recognition
import RPi.GPIO as GPIO

#setup GPIO pins
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(11, GPIO.IN)
GPIO.setup(9, GPIO.OUT)

#start backend flask server

subprocess.Popen('python server.py', shell=True)

#start react app in full screen

os.chdir(r'../mirror_UI/frontend/')
subprocess.Popen('npm start', shell=True)
os.chdir(r'../../python_backend/')
time.sleep(5)
print('slept')

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver", chrome_options=chrome_options)
driver.get('http://localhost:3000/')
driver.fullscreen_window()

def detect_face():
    cam = cv2.VideoCapture(0)
    for i in range(10):
        check, frame = cam.read()
        user_name = user_recognition("people.json", frame)
        if(user_name != None):
            return user_name
        else:
            continue
    return None

#detect motion
while True:
    PIR = GPIO.input(11)
    if PIR == 1:
        face = detect_face()
        if (face != None):
            driver.get(f'http://localhost:3000/users/{face}')
            driver.fullscreen_window()
            GPIO.output(9, GPIO.HIGH)
            time.sleep(60)
        elif(face == None):
            driver.get(f'http://localhost:3000/')
            driver.fullscreen_window()
            GPIO.output(9, GPIO.LOW)

#scan faces