from django.urls import path
from django.conf.urls import url,include
from home import views

app_name='home'

urlpatterns = [
    url(r'^$',views.UserLogin,name='userlogin'),
    url(r'^index',views.Index,name='index'),
    url(r'^dash',views.Dashboard,name='dashboard'),
    url(r'^users/',views.Users,name='users'),
    url(r'^saveusers/',views.SaveUsers,name='saveUsers'),
    url(r'^kyc/',views.KycData,name='kyc'),
    url(r'^tradeapproval/',views.TradeApprove,name='tradeApproval'),
    url(r'^tradeapprove/',views.Approve,name='approveTrade'),
    url(r'^tradesave/',views.SaveApproval,name='save'),
    url(r'^tradeapproved/',views.TradeApproved,name='tradeApproved'),
    url(r'^predocuments/',views.PreDocuments,name='preDocuments'),
    url(r'^prepayments/',views.Prepayments,name='prepayments'),
    url(r'^prepaymenthelper/',views.PrepaymentsHelper,name='prepaymentsHelper'),
    url(r'^saveprepay/',views.SavePayments,name='savepre'),
    url(r'^salespurchase/',views.SalesPurchase,name='salesPurchase'),
    url(r'^savesalespurchase/',views.SaveSalesPurchase,name='saveSalesPurchase'),
    url(r'^salespurchasehelper/',views.SalesPurchaseHelper,name='helperSalesPurchase'),

    url(r'^extras/',views.ExtraCosts,name='extraCost'),
    url(r'^saveextras/',views.SaveExtraCost,name='saveExtras'),
    url(r'^finance/',views.PaymentFinance,name='paymentFinance'),
    url(r'^savefinance/',views.SaveFinance,name='savepaymentFinance'),
    url(r'^financehelper/',views.PaymentFinanceHelper,name='helperpaymentFinance'),
    url(r'^disputes/',views.Dispute,name='disputes'),
    url(r'^savedisputes/',views.SaveDisputes,name='savedispute'),
    url(r'^pl/',views.PLBook,name='pl'),
    url(r'^plsave/',views.SavePLBook,name='savepl'),
    url(r'^inventory/',views.Inventory,name='inventory'),
    url(r'^stocks/',views.Stock,name='stocks'),
    url(r'^savestocks/',views.SaveStock,name='savestocks'),
    url(r'^savekyc/',views.SaveKyc,name='savekyc'),
    url(r'^editkyc/',views.EditKyc,name='editkyc'),
    url(r'^fetchcompany/',views.FetchCompany,name='fetchComp'),
    url(r'^getdata1/',views.GetDataSales,name='getsales'),
    url(r'^getdata2/',views.GetDataPurchase,name='getpurchase'),
    url(r'^getdata/',views.GetDataInventory,name='getInventoryData'),
    url(r'^savedata/',views.SaveInventoryRecord,name='saveInventoryData'),
    url(r'^data/',views.GetProduct,name='getProductData'),
    url(r'^log/',views.LogFile,name='log'),

    url(r'^trace/',views.GetProductTrace,name='getproducttrace'),
    url(r'^getextra/',views.ExtrasHelper,name='extrashelper'),

#new --------------------------------------
    url(r'^payablemore/',views.PayableMore, name='getpayablemore'),
    url(r'^receivablemore/',views.ReceivableMore, name='getreceivablemore'),
    url(r'^bar/',views.GetPieData, name='getpiedata'),
    url(r'^salesbill/',views.GetSalesBill, name='getsalesbill'),

    url(r'^getstockproduct/',views.GetStockProduct, name='getStockProduct'),

    # ---------------------edit-------------------------------
    url(r'^editapproval/',views.EditTradeApproval,name='editTradeApproval'),
    url(r'^editapproved/',views.EditTradeApproved,name='editTradeApproved'),
    url(r'^editdoc/',views.EditDoc,name='editTradeDoc'),
    url(r'^editprepay/',views.EditPrePayments,name='editPrePayments'),
    url(r'^salespurchaseedit/',views.EditSalesAndPurchase,name='editSalesPurchase'),
    url(r'^editextras/',views.EditExtras,name='editExtras'),
    url(r'^editfinance/',views.EditPurchaseAndFinance,name='editFinance'),
    url(r'^editDisputes/',views.EditDisputes,name='editDisputes'),
    url(r'^editinventory/',views.EditInventory,name='editInventory'),
    url(r'^editstock/',views.EditStock,name='editStock'),
    url(r'^edituser/',views.EditUsers,name='edituser'),
     # editpl
    url(r'^editpl/',views.EditPL,name='editPL'),
# alter value -----------------------
    url(r'^alterapproval/',views.AlterTradeApproval,name='alterTradeApproval'),
    url(r'^alterapproved/',views.AlterTradeApproved,name='alterTradeApproved'),
    url(r'^alterprepay/',views.AlterPrePayments,name='alterPrePayments'),
    url(r'^altersalespurchase/',views.AlterSalesPurchase,name='alterSalesPurchase'),
    url(r'^alterextras/',views.AlterExtras,name='alterExtras'),
    url(r'^alterfinance/',views.AlterFinance,name='alterFinance'),
    url(r'^alterstock/',views.AlterStock,name='alterStock'),
    url(r'^alterkyc/',views.AlterKyc,name='alterKyc'),
    url(r'^delkyc/',views.DelKyc,name='delKyc'),
    url(r'^alteruser/',views.AlterUser,name='alteruser'),
    url(r'^deluser/',views.DelUser,name='delUser'),
    url(r'^alterpl/',views.AlterPL,name='alterPL'),
    url(r'^amend/',views.AmendValues,name='amendValues'),
    url(r'^deletestock/',views.DeleteStockJournal,name='deleteStockJournal'),

    #filters ....................................................................
    url(r'^filterapproval/',views.GetTradeApproval,name='gettradeapproval'),
    url(r'^filterapproved/',views.GetTradeApproved,name='gettradeapproved'),
    url(r'^filterpre/',views.GetPresale,name='getpresale'),
    url(r'^filterprepay/',views.GetPrepayment,name='getprepayment'),
    url(r'^filtersalespurchase/',views.GetSalesPurchase,name='getsalespurchase'),
    url(r'^filterextra/',views.GetExtras,name='getextras'),
    url(r'^filterfinance/',views.GetFinance,name='getfinance'),

    url(r'^qtygetapproved/',views.GetQtyApprovedDetailPurchase,name='qtyapproveddetailpurchase'),
    url(r'^qtygetapprovedsale/',views.GetQtyApprovedDetailSales,name='qtyapproveddetailsales'),
    url(r'^qtygetapprovedhelp/',views.GetQtyApprovedDetailPurchaseHelper,name='qtyapproveddetailpurchasehelper'),
    url(r'^qtygetapprovedsalehelp/',views.GetQtyApprovedDetailSalesHelper,name='qtyapproveddetailsaleshelper'),

    url(r'^filterpl/',views.FilterPL,name='filterp&l'),
    url(r'^filterp/',views.FilterP,name='filterpurchase'),
    url(r'^filters/',views.FilterS,name='filtersales'),
    # qtyapproveddetail

    url(r'^financedate/',views.FinanceDate,name='financeDate'),
    url(r'^spdate/',views.SPDate,name='spDate'),
    url(r'^approvedfilter/',views.ApprovedFilter,name='filterapproved'),
    url(r'^approvesp/',views.ApproveSP,name='approveSalesPurchase'),

      #--------------------excels-------------------------------------------------------
    url(r'^exportapproval/(?P<approval>\w*)/',views.ExportApproval,name='exportApproval'),
    url(r'^exportapproved/(?P<approved>\w*)/',views.ExportApproved,name='exportApproved'),
    url(r'^exportapprovedfilter/(?P<year>\w*)/(?P<month>\w*)/',views.ExportApprovedFilter,name='exportApprovedFilter'),
    url(r'^exportdoc/',views.ExportDoc,name='exportDoc'),
    url(r'^exportpre/',views.ExportPre,name='exportPrepayment'),
    url(r'^exportsp/',views.ExportSP,name='exportSalesPurchase'),
    url(r'^exportextras/',views.ExportExtras,name='exportExtras'),
    url(r'^exportfinance/(?P<approval>\w*)/',views.ExportFinance,name='exportFinance'),
    url(r'^exportfinancefilter/(?P<year>\w*)/(?P<month>\w*)/',views.ExportFinanceFilter,name='exportFinanceFilter'),
    url(r'^exportprofit/',views.ExportProfit,name='exportProfit'),
    url(r'^exportfilterprofit/(?P<year>\w*)/(?P<month>\w*)/',views.ExportFilterProfit,name='exportFilterProfit'),
    url(r'^exportkyc/',views.ExportKyc,name='exportKyc'),
    url(r'^exportsales/',views.ExportSales,name='exportSales'),
    url(r'^exportsalesfilter/(?P<year>\w*)/(?P<month>\w*)/',views.ExportSalesFilter,name='exportSalesFilter'),
    url(r'^exportpurchase/',views.ExportPurchase,name='exportPurchase'),
    url(r'^exportpurchasefilter/(?P<year>\w*)/(?P<month>\w*)/',views.ExportPurchaseFilter,name='exportPurchaseFilter'),
    url(r'^exportinv/',views.ExportInventory,name='exportInventory'),

    # kyc approval
    url(r'^approvekyc1/',views.ApproveKyc1,name='approveKyc1'),
    url(r'^approvekyc2/',views.ApproveKyc2,name='approveKyc2'),

    # url(r'^dashexportfinance/',views.ExportDashFinance,name='dueFinanceExport'),
    url(r'^checkfiance/',views.CheckFinance,name='checkfinance'),
    # url(r'^dashexportpre/',views.ExportDashPre,name='duePreExport'),)
    url(r'^checkpre/',views.CheckPre,name='checkpre'),
# delete------------------------------------------------------------------------------
    url(r'^delapproval/',views.DelApproval,name='delApproval'),
    url(r'^delapproved/',views.DelApproved,name='delApproved'),
    url(r'^delpre/',views.DelPrepayment,name='delPrepayment'),
    url(r'^delsp/',views.DelSP,name='delSP'),
    url(r'^delextra/',views.DelExtra,name='delExtra'),
    url(r'^delfinance/',views.DelFinance,name='delFinance'),
    url(r'^delpnl/',views.DelPnL,name='delPl'),

    url(r'^getrate',views.GetRate,name='get_rate'),
]


import discord
from discord.ext import commands
import psycopg2.extensions as sql
from typing import Iterable
from utils.sql_fetcher import SqlFetcher
from utils.utils import decode_mention

class CourseMentions:
	def __init__(
		self, conn: sql.connection, sql_fetcher: SqlFetcher, bot: commands.Bot
	) -> None:
		self.conn = conn
		self.sql_fetcher = sql_fetcher
		self.bot = bot

	def get_channel_full_name(self, channel: str) -> str:
		"""Searches the database for the course name given the channel id"""
		query = self.sql_fetcher["search_category_name.sql"]
		with self.conn as conn:
			with conn.cursor() as cursor:
				cursor.execute(query, {"channel": channel})
				row = cursor.fetchone()
		if row is not None:
			# return full matched category name from database
			return row[0]
		else:
			# return the channel name instead
			ch = self.bot.get_channel(int(channel))
			return ch.name if ch is not None else None

	def map_channel_mention(self, word: str):
		"""given a word in a string, return the channel name
		if it is a channel mention, otherwise return the original word"""
		mention_type, channel_id = decode_mention(word)
		# convert mention to full name if word is a mention
		if mention_type == "channel":
			return self.get_channel_full_name(channel_id)
		# not a channel mention
		return word

	def replace_channel_mentions(self, text: str):
		return " ".join(
			map(
				self.map_channel_mention,
				text.replace("<", " <").replace(">", "> ").split(),
			)
		)


"""

    Description: This file is where you set and change ports,
    set collection rates, etc

"""

import os, datetime

DEBUG = True

SITE_NAME = ""
BASE_DIR = os.getcwd()
DATA_DIR = os.path.join(BASE_DIR, '../../data')

#Uncomment the sensors that you are collecting from and add
#appropriate ports ALSO!!! add to the list of active sensors

SENSORS = {

           'TEMPERATURE': [
                           [ 'NAME'     , 'temperature'],
                           [ 'FREQ', '20'], #read every 120 sec
                           [ 'PERIOD', '30'], #for 30 seconds
                           [ 'WRITE_FILE', os.path.join(DATA_DIR,'temperature')],
                           ['INITIAL_COLLECTION_TIME',datetime.datetime.utcnow()],
                          ],

              'PRESSURE': [
                            [ 'NAME'     , 'pressure'],
                            [ 'FREQ', '20'], #read every 120 sec
                            [ 'PERIOD', '30'], #for 30 seconds
                            [ 'WRITE_FILE', os.path.join(DATA_DIR,'pressure')],
                            ['INITIAL_COLLECTION_TIME',datetime.datetime.utcnow()],
                           ],

          'SOLAR_RADIATION': [
                            ['NAME', 'solar_radiation'],
                            ['FREQ', '20'],
                            ['PERIOD','30'],
                            ['WRITE_FILE',os.path.join(DATA_DIR,'solar_radiation')],
                            ['INITIAL_COLLECTION_TIME',datetime.datetime.utcnow()],
                           ]

}


import cv2
import numpy as np
import math
from skimage import morphology, img_as_float, img_as_ubyte

def neibPoint(img, x, y, qt):
	i = -1;
	j = -1;
	neibQt = -1
	if img[x, y] == 255:
		for i in range(-1, 2):
			for j in range(-1, 2):
				if img[(x + i), (y + j)] == 255:
					neibQt += 1
	if neibQt == qt and qt > 0:
		return True
	elif neibQt == -qt and qt < 0:
		return True
	else:
		return False

def searchPath(img1, img2, x, y, distance):
	a = x;
	b = y;
	img2[a, b] = 100
	for i in range (0, distance):
		for m in range(-1, 2):
			for n in range(-1, 2):
				if img1[a+m, b+n] == 255:
					if img2[a+m, b+n] == 0:
						a = a+m
						b = b+n
						img2[a, b] = 100
					elif img2[a+m, b+n] == 255:
						return False
	return True

def minutiaeExtraction(s):
	file = open(s + '_coord.txt', 'w')

	# imgrgb = cv2.imread(s + '_skel.bmp', 1)
	img = cv2.imread(s + '_skel.bmp', 0)
	height, width = img.shape
	size = np.size(img)

	ctrl1 = np.zeros(img.shape,np.uint8)
	ctrl2 = np.zeros(img.shape,np.uint8)

	for i in range(1, height-1):
		for j in range(1, width-1):
			if neibPoint(img, i, j, 3):
				ctrl1[i, j] = 255
				# imgrgb[i-1, j-1] = [0, 0, 255]
				# imgrgb[i-1, j] = [0, 0, 255]
				# imgrgb[i-1, j+1] = [0, 0, 255]
				# imgrgb[i, j-1] = [0, 0, 255]
				# imgrgb[i, j+1] = [0, 0, 255]
				# imgrgb[i+1, j-1] = [0, 0, 255]
				# imgrgb[i+1, j] = [0, 0, 255]
				# imgrgb[i+1, j+1] = [0, 0, 255]

	for i in range(1, height-1):
		for j in range(1, width-1):
			if neibPoint(img, i, j, -1):
				ctrl2[i, j] = 255
				# imgrgb[i-1, j-1] = [0, 255, 0]
				# imgrgb[i-1, j] = [0, 255, 0]
				# imgrgb[i-1, j+1] = [0, 255, 0]
				# imgrgb[i, j-1] = [0, 255, 0]
				# imgrgb[i, j+1] = [0, 255, 0]
				# imgrgb[i+1, j-1] = [0, 255, 0]
				# imgrgb[i+1, j] = [0, 255, 0]
				# imgrgb[i+1, j+1] = [0, 255, 0]

	# Minutiaes 1: bifurcacoes, ou seja, pontos que possuem 3 pontos adjacentes
	# Minutiaes 2: finais de linha, ou seja, que possuem apenas 1 ponto adjacente
	# cv2.imwrite(s + "_mn_1_2.bmp", imgrgb)
	cv2.imwrite(s + "_mn_1_only.bmp", ctrl1)
	cv2.imwrite(s + "_mn_2_only.bmp", ctrl2)

	# Borda falsa - para evitar falha de segmentacao e variantes
	for i in range(0, height):
		img[i, 0] = 0
		img[i, width-1] = 0
	for i in range(0, width):
		img[0, i] = 0
		img[height-1, i] = 0

	ctrl3 = ctrl1.copy()

	# Retiradas de "bifurcacoes" unidas
	for i in range(1, height-1):
		for j in range(1, width-1):
			if ctrl3[i][j] == 255:
				for k in range(-30, 31):
					for l in range(-30, 31):
						if i+k < height and j+l < width:
							ctrl3[i+k][j+l] = 0
				ctrl3[i][j] = 255

	# cv2.imwrite(s + "_mn_1_clean.bmp", ctrl3)

	# # Remocao de bifurcacoes falsas
	# for i in range(1, height-1):
	#	for j in range(1, width-1):
	#	if ctrl1[i, j] == 255:
	#		count = 0
	#	    for a in range(-1, 2):
	#		for b in range(-1, 2):
	#		    if img[i+a, j+b] == 255:
	#			ctrl4 = ctrl1.copy()
	#			ctrl4[i, j] = 200
	#			if searchPath(img, ctrl4, i+a, j+b) == False:
	#			    count += 1
	#	    if count < 2:
	#		ctrl3[i, j] = 0

	# Minutiaes escolhidas
	cv2.imwrite(s + "_mn_1_selection.bmp", ctrl3)

	for i in range(0, height):
		for j in range(0, width):
			if img[i, j] == 255:
				if ctrl3[i, j] == 0:
					ctrl3[i, j] = 40

	# Minutiaes com a digital no fundo
	# cv2.imwrite(s + "_mn_1_selection2.bmp", ctrl3)

	ctrl3 = ctrl2.copy()

	# Inicios e terminos de linha
	for i in range(1, height-1):
		for j in range(1, width-1):
			if ctrl2[i, j] == 255:
				count = 0
				for a in range(-1, 2):
					for b in range(-1, 2):
						if img[i+a, j+b] == 255:
							ctrl4 = ctrl2.copy()
							ctrl4[i, j] = 200
							if not searchPath(img, ctrl4, i+a, j+b, 50):
								count += 1
				if count >= 1:
					ctrl3[i, j] = 0

	# Retiradas de terminais unidos
	for i in range(1, height-1):
		for j in range(1, width-1):
			if ctrl3[i][j] == 255:
				for k in range(-20, 21):
					for l in range(-20, 21):
						if i+k < height and j+l < width:
							ctrl3[i+k][j+l] = 0
				ctrl3[i][j] = 255

	# Minutiaes escolhidas
	cv2.imwrite(s + "_mn_2_selection.bmp", ctrl3)

	for i in range(0, height):
		for j in range(0, width):
			if img[i, j] == 255:
				if ctrl3[i, j] == 0:
					ctrl3[i, j] = 40

	# Minutiaes com a digital no fundo
	# cv2.imwrite(s + "_mn_2_selection2.bmp", ctrl3)

	# Calculo do core
	img = cv2.imread(s + "_mn_1_selection.bmp", 0)
	tX = 0
	tY = 0
	qt = 0
	for i in range(0, height):
		for j in range(0, width):
			if img[i, j] == 255:
				tX += i
				tY += j
				qt += 1

	img = np.zeros(img.shape,np.uint8)
	tX /= qt
	tY /= qt

	img[tX, tY] = 255

	# Escrita em arquivo
	file.write(str(tX)+','+str(tY)+'\n')
	cv2.imwrite(s + "_core.bmp", img)

	# Escolha entre todas as minutiae

	img1 = cv2.imread(s + "_mn_1_selection.bmp", 0)
	img2 = cv2.imread(s + "_mn_2_selection.bmp", 0)
	img3 = img1
	img3 = np.zeros(img1.shape,np.uint8)

	for i in range(0, height):
		for j in range(0, width):
			if img1[i][j] == 255 or img2[i][j] == 255:
				if math.sqrt( math.pow(abs(i - tX), 2) + math.pow(abs(j - tY), 2)) < 170:
					img3[i][j] = 255

	# Retiradas de minutiaes unidos
	for i in range(1, height-1):
		for j in range(1, width-1):
			if img3[i][j] == 255:
				for k in range(-20, 21):
					for l in range(-20, 21):
						if i+k < height and j+l < width:
							img3[i+k][j+l] = 0
				img3[i][j] = 255
				if img1[i][j] == 255:
					file.write(str(i)+','+str(j)+',1\n')
				else:
					file.write(str(i)+','+str(j)+',0\n')

	cv2.imwrite(s + "_minutiae.bmp", img3)
	file.close()

minutiaeExtraction('101_1')
minutiaeExtraction('101_2')
minutiaeExtraction('101_3')
minutiaeExtraction('101_4')
minutiaeExtraction('101_5')
minutiaeExtraction('101_6')
minutiaeExtraction('102_1')
minutiaeExtraction('102_2')
minutiaeExtraction('102_3')
minutiaeExtraction('102_4')


import cflib.crtp
import time
import numpy as np
import cv2
import time
from matplotlib import pyplot as plt
from distance_to_camera import DistanceCalculator
from controle_cachorrinho import PositioningControl
import threading
import urllib.request as urllib

from cflib.crazyflie import Crazyflie
from cflib.crazyflie.syncCrazyflie import SyncCrazyflie
from cflib.positioning.motion_commander import MotionCommander
from cflib.crazyflie.swarm import CachedCfFactory
from cflib.crazyflie.swarm import Swarm
from cflib.crazyflie.log import LogConfig

cflib.crtp.init_drivers(enable_debug_driver=False)
factory = CachedCfFactory(rw_cache='./cache')
URI3 = 'radio://0/40/2M/E7E7E7E7E4'
cf = Crazyflie(rw_cache='./cache')
sync = SyncCrazyflie(URI3, cf=cf)
sync.open_link()
mc = MotionCommander(sync)
mc.take_off()
time.sleep(3)

calculator = DistanceCalculator()

controller = PositioningControl(320,20,27,2)
cap = cv2.VideoCapture(1)
CenterLimitH = [305, 335]
cont = 0

while(cap.isOpened()):
    ret, frame = cap.read()

    if(frame is None):
        break

    if(cont%15==0):
        thread = threading.Thread(target=calculator.processImage, args=(frame, ))
        thread.setDaemon(True)
        thread.start()
        thread.join()

    if(cont%30==0):
        calculator.mediaDistance()

    calculator.writeDistance(frame)
    cv2.imshow('frame',frame)
    markerCenter = calculator.center

    if(mc.isRunning):
        mc.stop()

    if(calculator.markerFound):
        #Se o marcador não está no centro, envia comando para virar o drone
        #na direção do marcador
        if(controller.offCenter(markerCenter)):
            if(markerCenter < controller.center):
                mc.turn_left(1)
            else:
                mc.turn_right(1)

        #Se o marcador não está na distância correta, envia commando
        #para aproximar ou afasta-se
        if(controller.offDistance(calculator.distance_x)):
            if(calculator.distance_x > controller.center):
                mc.start_linear_motion(0.1, 0.0, 0.0)
            else:
                mc.start_linear_motion(-0.1, 0.0, 0.0)
        mc.setIsRunning(True)

    # if(self.alpha != 0):
    #     if (self.alpha > 10):
    #         mc.left(0.1)
    #         mc.setIsRunning(True)
    #     elif(self.alpha < 10):
    #         mc.right(0.1)
    #         mc.setIsRunning(True)

    cont += 1
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

cv2.waitKey()
cv2.destroyAllWindows()

mc.land()
sync.close_link()


##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## Created by: speedinghzl02
## updated by: RainbowSecret
## Microsoft Research
## yuyua@microsoft.com
## Copyright (c) 2018
##
## This source code is licensed under the MIT-style license found in the
## LICENSE file in the root directory of this source tree
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

import torch.nn as nn
from torch.nn import functional as F
import math
import torch.utils.model_zoo as model_zoo
import torch
import os
import sys
import numpy as np
from torch.autograd import Variable
import functools
import torchvision.models as mo
res18=mo.resnet18()
affine_par = True

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
sys.path.append(os.path.join(BASE_DIR, '../utils'))
sys.path.append(os.path.join(BASE_DIR, '../utils'))
# sys.path.append('/inplace_abn')
from utils.resnet_block import conv3x3, Bottleneck,BasicBlock
from utils.psp_block import PSPModule
from utils.modules import InPlaceABN, InPlaceABNSync
BatchNorm2d = functools.partial(InPlaceABNSync, activation='none')

class Net(nn.Module):
    def __init__(self,student, teacher, criterion_l,criterion_s,criterion_k,args):
        super(Net, self).__init__()
        self.student = student
        self.teacher = teacher
        self.criterion_l = criterion_l
        self.criterion_k = criterion_k
        self.criterion_s = criterion_s
        self.args = args

    def inference(self,image,upsample):
        self.student.eval()
        _, _, w, h = image.size()
        preds = self.student(image)
        if upsample:
            scale_pred = F.upsample(input=preds[0], size=(w, h), mode='bilinear', align_corners=True)
        else:
            scale_pred = preds[0]
        return scale_pred

    def cal_loss(self,s_image,t_image,label):

        preds = self.student(s_image)
        with torch.no_grad():
            soft = self.teacher(t_image)
        loss = 0
        loss_k = 0
        loss_l = 0
        loss_s = 0
        if 'label' in self.args.sd_mode:
            loss_l = self.args.weight_l * self.criterion_l(preds, label)
            loss = loss + loss_l

        if 'kd' in self.args.sd_mode:
            loss_k = self.args.weight_k * self.criterion_k(preds, soft)

            loss = loss + loss_k
        if 'sd' in self.args.sd_mode:
            loss_s = self.args.weight_s * self.criterion_s(preds, soft)
            loss = loss + loss_s
        return loss

    def forward(self,s_image,t_image,label,upsample,mode):
        if mode =='train':
            ret = self.cal_loss(s_image,t_image,label)
        elif mode == 'test':
            ret = self. inference(s_image,upsample)

        return ret


"""empty message

Revision ID: 4a80166a46ec
Revises: 7f234c96d8f4
Create Date: 2017-05-24 13:49:31.910594

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '4a80166a46ec'
down_revision = '7f234c96d8f4'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('subject', sa.Column('dataset_id', sa.Integer(), nullable=False))
    op.create_foreign_key(None, 'subject', 'dataset', ['dataset_id'], ['id'])
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'subject', type_='foreignkey')
    op.drop_column('subject', 'dataset_id')
    # ### end Alembic commands ###


#!/usr/bin/env python
import cv2
#import cv2.cv as cv
import numpy as np
#from cv2 import *

image_path="plate6.png"
carsample=cv2.imread(image_path)

# convert into grayscale
gray_carsample=cv2.cvtColor(carsample, cv2.COLOR_BGR2GRAY)

# blur the image
blur=cv2.GaussianBlur(gray_carsample,(5,5),0)

# find the sobel gradient. use the kernel size to be 3
sobelx=cv2.Sobel(blur, cv2.CV_8U, 1, 0, ksize=3)

#Otsu thresholding
_,th2=cv2.threshold(sobelx, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)

#Morphological Closing
se=cv2.getStructuringElement(cv2.MORPH_RECT,(23,2))
closing=cv2.morphologyEx(th2, cv2.MORPH_CLOSE, se)

contours,_=cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

for cnt in contours:
    rect=cv2.minAreaRect(cnt)
    box=cv2.cv.BoxPoints(rect)
    box=np.int0(box)
    cv2.drawContours(carsample, [box], 0, (0,255,0),2)

#validate a contour. We validate by estimating a rough area and aspect ratio check.
def validate(cnt):
    rect=cv2.minAreaRect(cnt)
    box=cv2.cv.BoxPoints(rect)
    box=np.int0(box)
    output=False
    width=rect[1][0]
    height=rect[1][1]
    if ((width!=0) & (height!=0)):
        if (((height/width>3) & (height>width)) | ((width/height>3) & (width>height))):
            if ((height*width<16000) & (height*width>3000)):
                output=True
    return output
#Lets draw validated contours with red.
for cnt in contours:
    if validate(cnt):
        rect=cv2.minAreaRect(cnt)
        box=cv2.cv.BoxPoints(rect)
        box=np.int0(box)
        cv2.drawContours(carsample, [box], 0, (0,0,255),2)

# defining a function doing this will come handy.
def generate_seeds(centre, width, height):
    minsize=int(min(width, height))
    seed=[None]*10
    for i in range(10):
        random_integer1=np.random.randint(1000)
        random_integer2=np.random.randint(1000)
        seed[i]=(centre[0]+random_integer1%int(minsize/2)-int(minsize/2),centre[1]+random_integer2%int(minsize/2)-int(minsize/2))
    return seed

#masks are nothing but those floodfilled images per seed.
def generate_mask(image, seed_point):
    h=carsample.shape[0]
    w=carsample.shape[1]
    #OpenCV wants its mask to be exactly two pixels greater than the source image.
    mask=np.zeros((h+2, w+2), np.uint8)
    #We choose a color difference of (50,50,50). Thats a guess from my side.
    lodiff=50
    updiff=50
    connectivity=4
    newmaskval=255
    flags=connectivity+(newmaskval<<8)+cv2.cv.CV_FLOODFILL_FIXED_RANGE+cv2.cv.CV_FLOODFILL_MASK_ONLY
    _=cv2.floodFill(image, mask, seed_point, (255, 0, 0),
                    (lodiff, lodiff, lodiff), (updiff, updiff, updiff), flags)
    return mask

# we will need a fresh copy of the image so as to draw masks.
carsample_mask=cv2.imread(image_path)

# for viewing the different masks later
mask_list=[]

for cnt in contours:
    if validate(cnt):
        rect=cv2.minAreaRect(cnt)
        centre=(int(rect[0][0]), int(rect[0][1]))
        width=rect[1][0]
        height=rect[1][1]
        seeds=generate_seeds(centre, width, height)

        #now for each seed, we generate a mask
        for seed in seeds:
            # plot a tiny circle at the present seed.
            cv2.circle(carsample, seed, 1, (0,0,255), -1)
            # generate mask corresponding to the current seed.
            mask=generate_mask(carsample_mask, seed)
            mask_list.append(mask)

cv2.imwrite('gray.jpg',gray_carsample)
cv2.imwrite('blur.jpg',blur)
cv2.imwrite('sobelx.jpg',sobelx)
cv2.imwrite('thresholding.jpg',th2)
cv2.imwrite('contours.jpg', carsample)
cv2.imwrite('test2.jpg',mask_list[2])
cv2.imwrite('test3.jpg',mask_list[3])
cv2.imwrite('test4.jpg',mask_list[4])
cv2.imwrite('test5.jpg',mask_list[5])
cv2.imwrite('test6.jpg',mask_list[6])
cv2.imwrite('test7.jpg',mask_list[7])
cv2.imwrite('test8.jpg',mask_list[8])
cv2.imwrite('test9.jpg',mask_list[9])


#!/usr/bin/env python3

import sys
import warnings

if not sys.warnoptions:
    warnings.simplefilter("ignore")

import keras
from keras.applications.nasnet import preprocess_input, NASNetLarge

#from keras_applications.resnet import ResNet50, ResNet101, ResNet152
from keras.applications.resnet50 import ResNet50

from keras.layers import Dense, Conv2D, BatchNormalization, Activation
from keras.layers import AveragePooling2D, Input, Flatten
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint, LearningRateScheduler
from keras.callbacks import ReduceLROnPlateau
from keras.preprocessing.image import ImageDataGenerator
from keras import backend as K
from keras.models import Model
from keras.datasets import cifar100
import numpy as np
import os

# Training parameters
batch_size = 32
epochs = 200
num_classes = 10

class KerasModel():
    '''Create, train and predict with a Keras neural network.
    '''

    def __init__(self,
                 model_spec='ResNet20',
                 include_top=True,
                 weights='imagenet',
                 input_tensor=None,
                 pooling='max',
                 classes=1000):

        self.model_spec = model
        self.include_top=include_top
        self.weights = weights
        self.input_tensor = input_tensor
        self.pooling = pooling
        self.classes = classes

        if not self.include_top:
            self.input_shape = (224, 224, 3)
        else:
            self.input_shape = None

        self.model_options = {
            'ResNet50': ResNet50,
            'ResNet101': ResNet101,
            'ResNet152': ResNet152,
            'NASNetLarge': NASNetLarge,
        }

        self.model = self.model_options[self.model_spec]

    def build_model(self):

        self.model = self.model(include_top=self.include_top,
                                weights=self.weights,
                                input_tensor=self.input_tensor,
                                pooling=self.pooling)

    def train_model(self):

        pass

    def predict(self):

        pass

resnet50 = ResNet50(include_top=True,
                    weights='imagenet',
                    input_tensor=None,
                    input_shape=None,
                    pooling=None,
                    classes=1000)

(x_train, y_train), (x_test, y_test) = cifar100.load_data()


from __future__ import print_function
import logging
import sys

import grpc

import hellonerd_pb2
import hellonerd_pb2_grpc

def run():
    try:
        uname = sys.argv[1]
        server = sys.argv[2]
        port = sys.argv[3]
        detonate = sys.argv [4]
    except:
        detonate = "no"
        print('usage: python client.py username ip  port  --normal/--detonate ')
        sys.exit(0)

    with grpc.insecure_channel(server+':'+port) as channel:
        stub = hellonerd_pb2_grpc.HelloNerdStub(channel)
        if (detonate == '--detonate'):
            while True:
                response = stub.SayHello(hellonerd_pb2.HelloRequest(name=uname))
                print ("Nerd Says : " + response.message)
        response = stub.SayHello(hellonerd_pb2.HelloRequest(name=uname))
        print("Nerd Says: " + response.message)

if __name__ == '__main__':
    logging.basicConfig()
    run()


import asyncio, arpeggio, discord, hjson, os, pprint, re, sys
from ithkuil.morphology.words import Factory
from ithkuil.morphology.exceptions import AnalysisException

SUB = str.maketrans("0123456789", "₀₁₂₃₄₅₆₇₈₉")
SUP = str.maketrans("0123456789", "⁰¹²³⁴⁵⁶⁷⁸⁹")

with open('lexicon.hjson') as f: lexicon = hjson.load(f)
with open('suffixes.hjson') as f: suffixes = hjson.load(f)
with open('biases.hjson') as f: biases = hjson.load(f)

def normalize(s):
    s = s.replace('\N{left single quotation mark}', "'")
    s = s.replace('\N{right single quotation mark}', "'")
    s = s.replace('\N{left double quotation mark}', '"')
    s = s.replace('\N{right double quotation mark}', '"')
    s = s.replace('\N{modifier letter acute accent}', '\N{acute accent}')
    s = s.replace('\N{modifier letter grave accent}', '\N{grave accent}')
    s = s.replace('\N{modifier letter small h}', 'h')
    s = re.sub('ẍ|ẋ|x̌', 'xh', s)
    s = re.sub('ḑ|đ|ḏ', 'dh', s)
    s = re.sub('ı', 'i', s)
    return s

def fix_parens(s):
    return s.replace(') (', ', ')

def lexicon_lookup(root, designation, stem_and_pattern):
    d = 0 if designation == 'IFL' else 1
    p = int(stem_and_pattern[1]) - 1
    s = int(stem_and_pattern[3]) - 1
    return lexicon_lookup_(root, d, p, s)

def lexicon_lookup_(root, d, p, s):
    print('lexicon_lookup_', root, d, p, s)
    dps = ''
    if (d, p, s) != (0, 0, 0):
        dps = ('-%s-P%dS%d' % (['IFL', 'FML'][d], p + 1, s + 1)).translate(SUB)

    root = normalize(root)
    if root not in lexicon:
        return "%s%s" % (root, dps)
    entry = lexicon[root]
    if isinstance(entry, str):
        return "%s%s" % (entry, dps)

    if isinstance(entry[0], str):
        command = entry[0]
        if command.startswith('@'):
            # Template
            res = lexicon_lookup_(command, d, p, s)
            if command == '@kh' and '/' in entry[1]:
                res = res.replace('@ relation (dominant)', entry[1].split('/')[0].strip())
                res = res.replace('@ relation (passive)', entry[1].split('/')[1].strip())
            elif len(entry) > 2:
                for i in range(1, len(entry)):
                    res = res.replace('@' + str(i), entry[i])
                return res
            return res.replace('@', entry[1])
        elif command.startswith('+'):
            # Addendum
            res = lexicon_lookup_(command[1:], d, p, s)
            res = fix_parens(res + ' (' + entry[1] + ')')
            return res
        else:
            raise ValueError('Invalid command: ' + command)

    x = entry[d]
    if isinstance(x, str):
        res = lexicon_lookup_(root, 0, p, s)
        res = fix_parens(res + ' (' + x + ')')
        return res

    x = entry[d][p]
    if isinstance(x, str):
        res = lexicon_lookup_(root, d, 0, s)
        res = fix_parens(res + ' (' + x + ')')
        return res

    return entry[d][p][s]

defaults = 'UNI DEL CSL NRM M EXS OBL STA UNFRAMED MNO FAC CTX PRC ASR PPS CNF NEU'.split()

def nice_level(deg, typ):
    return '= > < OPT MIN SPL IFR ≥ ≤'.split()[deg-1] + {1: 'ᵣ', 2: 'ₐ', 3: '₃'}[typ]

def nice_suffix(s, full_names=False, t3_adjunct=False):
    if t3_adjunct:
        return nice_gloss(s['v3c_adjunct'], full_names)

    code = s['code']
    typ = int(s['degree'][5])
    deg = int(s['degree'][-1])
    if code == 'LVL':
        return nice_level(deg, typ)

    fill_at = None
    if re.match('MD..', code):
        fill_at = suffixes[code]
        code = 'MD'

    typs = {1: '', 2: '₂', 3: '₃'}[typ]
    if code in suffixes:
        result = suffixes[code][deg-1].join('‘’') + typs
        if fill_at: result = result.replace('@', fill_at)
        return result

    degs = str(deg).translate(SUP)
    return code + typs + degs

def nice_code(key, full_names=False):
    code = key['code']
    if code[:3] in biases and full_names:
         return '*(%s)*' % (biases[code[:3]]['+' in code])
    elif code.startswith('CMP'):
        p = {
            '1': 'previously less',
            '2': 'previously more',
            '3': 'still less',
            '4': 'still more',
            '5': 'now less',
            '6': 'now more',
            '7': 'previously equal',
            '8': 'previous level unknown'
        }[code[3]]
        q = {
            'A': 'but still low',
            'B': 'but now high',
            'C': 'now also high',
        }[code[4]]
        if 'now' in p: q = q.replace('now ', '')
        return '%s[%s; %s]' % (code, p, q)
    levels = 'EQU SUR DFC OPT MIN SPL IFR SPQ SBE'.split()
    if code[:3] in levels:
        return nice_level(levels.index(code[:3]) + 1, 'ra'.index(code[3:]) + 1)
    return key['name'].lower() if full_names else key['code']

def nice_gloss(word, full_names=False):
    word = normalize(word.lower().strip('.,?!'))

    try:
        parse = Factory.parseWord(word)
        desc = parse.fullDescription()
        descType = desc['type']

        if desc['type'] == 'Bias adjunct':
            code = desc['Bias']['code']
            return nice_code(desc['Bias'], full_names)

        if desc['type'] == 'Personal adjunct':
            tags = []
            for k in desc['categories']:
                if k in desc and desc[k]['code'] not in defaults:
                    tags.append(nice_code(desc[k], full_names))
            return '-'.join(tags)

        if desc['type'] != 'Formative':
            tags = []
            for k in desc['categories']:
                if k in desc and desc[k]['code'] not in defaults:
                    tags.append(nice_code(desc[k], full_names))
            return desc['type'] + ': ' + '-'.join(tags)

        tags = []
        root = desc.pop('Root')
        designation = desc.pop('Designation')['code']
        stem_and_pattern = desc.pop('Stem and Pattern', {'code': 'P1S1'})['code']
        lex = lexicon_lookup(root, designation, stem_and_pattern)

        ir = None
        if 'Incorporated root' in desc:
            i_root = desc.pop('Incorporated root')
            i_designation = desc.pop('Designation (inc)')['code']
            i_stem_and_pattern = desc.pop('Stem and Pattern (inc)', {'code': 'P1S1'})['code']
            i_lex = lexicon_lookup(i_root, i_designation, i_stem_and_pattern)
            inc_tags = []
            for k in desc['categories']:
                if k.endswith('(inc)') and k in desc:
                    key = desc.pop(k)
                    if key['code'] not in defaults:
                        inc_tags.append(nice_code(key, full_names))
            ir = '-'.join(["*%s*" % i_lex] + inc_tags)

        for k in desc['categories']:
            if k in desc and desc[k]['code'] not in defaults:
                tags.append(nice_code(desc[k], full_names))
        res = '-'.join(["*%s*" % lex] + tags)
        if ir:
            res += '-[%s]' % ir
        if 'suffixes' in desc:
            t3_adjunct = all(s['degree'][5] == '3' for s in desc['suffixes'])
            res += ' + ' + ' + '.join(nice_suffix(s, full_names, t3_adjunct) for s in desc['suffixes'])

        return res
    except arpeggio.NoMatch as e:
        return "Couldn't parse."
    except AnalysisException as e:
        return str(e)

client = discord.Client()

def bot_result(message_content):
    cmd, *words = message_content.split()
    full_names = 'full' in cmd

    if len(words) == 1:
        return nice_gloss(words[0], full_names)
    else:
        return '\n'.join(['**__Gloss:__**'] + ['**%s**: %s' % (word.lower().strip('.,?!'), nice_gloss(word, full_names)) for word in words])

@client.event
async def on_ready():
    print('Running Discord bot:', client.user)

@client.event
async def on_message(message):
    say = lambda s: message.channel.send(s)
    if message.author == client.user: return
    if message.content.startswith('!gloss'):
        await say(bot_result(message.content))

if __name__ == '__main__':
    if sys.argv[1:]:
        print(bot_result(' '.join(sys.argv[1:])))
    else:
        client.run(os.getenv('ULAMTON_TOKEN'))


# pca로 만든 컬럼 모델에 집어넣을 수 있다.
# input_dim = 154로 모델을 만드시오

import numpy as np
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Input
from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split

from tensorflow.keras.datasets import mnist
(x_train,y_train),(x_test,y_test)=mnist.load_data()

print(x_train.shape) # (60,000,28,28)
print(y_train.shape) # (60,000,)
print(x_test.shape)  # (10,000,28,28)
print(y_test.shape)  # (10,000,)

# 데이터 전처리
x_train=x_train.reshape(60000,784).astype('float32')/255
x_test=x_test.reshape(10000,784).astype('float32')/255

X = np.append(x_train,x_test,axis=0)

pca = PCA(n_components=154)
X = pca.fit_transform(X)

print(X.shape) #(70,000, 154)

# autoencoder니까 one.hot.encoding할 필요 없음
x2_train = X[:60000]
x2_test = X[60000:]

print(x2_train.shape)
print(x2_test.shape)

input_img = Input(shape=(154,))
encoded = Dense(32, activation='relu')(input_img)
# 중간 노드의 개수 파악
# 특성을 추출하고 차원을 줄이는 것(pca)
decoded = Dense(154, activation='sigmoid')(encoded) # 0부터 1사이니까(정규화시켜서), sigmoid써준다. # 다중분류에서도 sigmoid써줄 수 있음

autoencoder = Model(input_img, decoded)

autoencoder.summary()

autoencoder.compile(optimizer='adam',loss='binary_crossentropy',metrics=['acc'])
# autoencoder.compile(optimizer='adam',loss='mse')

autoencoder.fit(x2_train, x2_train, epochs=50, batch_size=50, validation_split=0.2) # 앞뒤가 똑같은 오토인코더~~

decoded_imgs = autoencoder.predict(x2_test) # predict

loss, acc = autoencoder.evaluate(x2_test,decoded_imgs, batch_size=50)

print("loss:",loss)
print("acc:",acc)

"""
loss: 3.1720477634906217e-05
acc: 0.9996083
"""


# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2021/6/29 19:08
# @Author  : Gene Jiang
# @File    : learning_jmespath.py
# @Description:

import jmespath

b = {'a': {'b': {'c': {'d': 'value'}}}}

result = jmespath.search("a.b.c.d", b)
print(result)

local_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list_result = jmespath.search('[0:3]', local_list)
print(list_result)


from transformers import AlbertTokenizer, AlbertForSequenceClassification, AdamW, get_linear_schedule_with_warmup
# from transformers import BertTokenizer, BertForSequenceClassification, AdamW, get_linear_schedule_with_warmup

from torch.utils.data import TensorDataset, random_split
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler
from imblearn.over_sampling import RandomOverSampler
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score, confusion_matrix
from sklearn.dummy import DummyClassifier

from collections import defaultdict, Counter

import torch
import gc
import pandas as pd
import numpy as np

import time
import datetime
import random

device = torch.device("cuda:6")

def format_time(elapsed):
    '''
    Takes a time in seconds and returns a string hh:mm:ss
    '''
    elapsed_rounded = int(round((elapsed)))
    return str(datetime.timedelta(seconds=elapsed_rounded))

def run(input_sen, output, testloaders, logit_df, seed=42):
    ## META VARIABLES
    random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    np.random.seed(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

    epochs = 20
    batch_size = 32

    # Train-Val-Test Split
    X_train, X_test, y_train, y_test, _, test_idx = train_test_split(input_sen, output, np.arange(len(input_sen)), test_size=0.2)
    y_majority = max(Counter(y_test).values()) / len(y_test)
    assert((input_sen[test_idx] == X_test).all())
    assert((output[test_idx] == y_test).all())
    X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.125)

    ros = RandomOverSampler()
    X_train, y_train = ros.fit_resample(X_train.reshape(-1, 1), y_train)

    # Tokenization & Dataloading
    tokenizer = AlbertTokenizer.from_pretrained('albert-base-v2')

    #Train
    encoding = tokenizer(list(map(lambda x: x[0], X_train)), return_tensors='pt', padding=True, truncation=True, max_length=128).to(device)
    input_ids = encoding['input_ids']
    attention_masks = encoding['attention_mask']
    labels = torch.tensor(y_train).unsqueeze(1).to(device)
    train_dataset = TensorDataset(input_ids, attention_masks, labels)
    train_dataloader = DataLoader(train_dataset, sampler=RandomSampler(train_dataset), batch_size=batch_size)

    #Val
    encoding = tokenizer(list(X_val), return_tensors='pt', padding=True, truncation=True, max_length=128).to(device)
    input_ids = encoding['input_ids']
    attention_masks = encoding['attention_mask']
    labels = torch.tensor(y_val).unsqueeze(1).to(device)
    val_dataset = TensorDataset(input_ids, attention_masks, labels)
    validation_dataloader = DataLoader(val_dataset, sampler=RandomSampler(val_dataset), batch_size=batch_size)

    #Test
    encoding = tokenizer(list(X_test), return_tensors='pt', padding=True, truncation=True, max_length=128).to(device)
    input_ids = encoding['input_ids']
    attention_masks = encoding['attention_mask']
    labels = torch.tensor(y_test).unsqueeze(1).to(device)
    test_dataset = TensorDataset(input_ids, attention_masks, labels)
    prediction_sampler = SequentialSampler(test_dataset)
    test_dataloader = DataLoader(test_dataset, sampler=prediction_sampler, batch_size=batch_size)

    model = AlbertForSequenceClassification.from_pretrained('albert-base-v2', num_labels = 3).to(device)
    optimizer = AdamW(filter(lambda p: p.requires_grad, model.parameters()), lr=2e-5)
    scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=0, num_training_steps=len(train_dataloader) * epochs)

    total_t0 = time.time()
    best_state_dict = None
    best_val = 0
    for epoch_i in range(epochs):

        # ========================================
        #               Training
        # ========================================

        print("")
        print('======== Epoch {:} / {:} ========'.format(epoch_i + 1, epochs))
        print('Training...')

        # Measure how long the training epoch takes.
        t0 = time.time()
        total_train_loss = 0

        model.train()

        predictions_train = np.array([])
        true_label_train = np.array([])
        for step, batch in enumerate(train_dataloader):

            # Progress update every 50 batches.
            if step % 50 == 0 and not step == 0:
                elapsed = format_time(time.time() - t0)
                print('  Batch {:>5,}  of  {:>5,}.    Elapsed: {:}.'.format(step, len(train_dataloader), elapsed))

            b_input_ids = batch[0].to(device)
            b_input_mask = batch[1].to(device)
            b_labels = batch[2].to(device)

            model.zero_grad()
            loss, logits = model(b_input_ids,
                                token_type_ids=None,
                                attention_mask=b_input_mask,
                                labels=b_labels)

            total_train_loss += loss.item() * b_labels.shape[0]
            loss.backward()
            optimizer.step()
            scheduler.step()

            gc.collect()

            logits = logits.detach().cpu().numpy()
            label_ids = b_labels.to('cpu').numpy()

            predictions_train = np.append(predictions_train, np.argmax(logits, axis=1).flatten())
            true_label_train = np.append(true_label_train, label_ids)

        # Calculate the average loss over all of the batches.
        accuracy_train = np.sum(predictions_train == true_label_train) / true_label_train.shape[0]
        f1_macro_train = f1_score(true_label_train, predictions_train, average='macro')
        f1_micro_train = f1_score(true_label_train, predictions_train, average='micro')
        print("\n  Training Accuracy: {0:.2f}".format(accuracy_train))
        print("  Training F1-MACRO: {0:.2f}".format(f1_macro_train))
        print("  Training F1-MICRO: {0:.2f}".format(f1_micro_train))

        avg_train_loss = total_train_loss / true_label_train.shape[0]
        training_time = format_time(time.time() - t0)
        print("  Average training loss: {0:.2f}".format(avg_train_loss))
        print("  Training epcoh took: {:}".format(training_time))

        # ========================================
        #               Validation
        # ========================================

        print("\nRunning Validation...")

        t0 = time.time()
        model.eval()

        total_val_loss = 0
        predictions_val = np.array([])
        true_label_val = np.array([])
        for batch in validation_dataloader:
            b_input_ids = batch[0].to(device)
            b_input_mask = batch[1].to(device)
            b_labels = batch[2].to(device)

            with torch.no_grad():
                (loss, logits) = model(b_input_ids,
                                    token_type_ids=None,
                                    attention_mask=b_input_mask,
                                    labels=b_labels)

            total_val_loss += loss.item() * b_labels.shape[0]

            logits = logits.detach().cpu().numpy()
            label_ids = b_labels.to('cpu').numpy()

            predictions_val = np.append(predictions_val, np.argmax(logits, axis=1).flatten())
            true_label_val = np.append(true_label_val, label_ids)

        accuracy_val = np.sum(predictions_val == true_label_val) / true_label_val.shape[0]
        f1_macro_val = f1_score(true_label_val, predictions_val, average='macro')
        f1_micro_val = f1_score(true_label_val, predictions_val, average='micro')
        print("  Accuracy: {0:.2f}".format(accuracy_val))
        print("  F1-MACRO: {0:.2f}".format(f1_macro_val))
        print("  F1-MICRO: {0:.2f}".format(f1_micro_val))

        performance_metric = f1_macro_val

        if performance_metric > best_val:
            print("Best Model Updated.")
            best_val = performance_metric
            best_state_dict = model.state_dict()

        avg_val_loss = total_val_loss / true_label_val.shape[0]
        validation_time = format_time(time.time() - t0)
        print("  Validation Loss: {0:.2f}".format(avg_val_loss))
        print("  Validation took: {:}".format(validation_time))

    print("\nTraining complete!")
    print("Total training took {:} (h:mm:ss)".format(format_time(time.time()-total_t0)))

    # ========================================
    #               Test
    # ========================================

    model.load_state_dict(best_state_dict)
    model.eval()

    # testloaders['indomain'] = (test_dataloader, None, None)
    test_measures = defaultdict(dict)

    for name, content in testloaders.items():
        testloader, sentences, labels = content
        predictions_test = np.array([])
        true_label_test = np.array([])
        logit_stack = np.empty((0,3))

        for batch in testloader:
            batch = tuple(t.to(device) for t in batch)
            b_input_ids, b_input_mask, b_labels = batch

            with torch.no_grad():
                outputs = model(b_input_ids, token_type_ids=None,
                                attention_mask=b_input_mask)

            logits = outputs[0]
            logits = logits.detach().cpu().numpy()
            label_ids = b_labels.to('cpu').numpy()

            predictions_test = np.append(predictions_test, np.argmax(logits, axis=1).flatten())
            true_label_test = np.append(true_label_test, label_ids)

            if name == 'indomain':
                logit_stack = np.vstack((logit_stack, logits))

        if name == 'indomain':
            for cnt, idx in enumerate(test_idx):
                logit_df.iloc[idx,:] += logit_stack[cnt,:]
            easy_examples = test_idx[predictions_test == true_label_test]
            hard_examples = test_idx[predictions_test != true_label_test]

        best_accr = np.sum(predictions_test == true_label_test) / true_label_test.shape[0]
        best_macro_f1 = f1_score(true_label_test, predictions_test, average='macro')
        print(name + ":  Test Accuracy: {0:.2f}".format(best_accr))
        print(name + ":  Test F1-MACRO: {0:.2f}".format(best_macro_f1))

        test_measures[name]['accr'] = best_accr
        test_measures[name]['f1'] = best_macro_f1

    # ========================================
    #              Dummy Test
    # ========================================
        if name == 'indomain':
            dummy_clf = DummyClassifier(strategy="uniform")
            dummy_clf.fit(X_train, y_train)
            predictions_dummy = dummy_clf.predict(X_test)

            dummy_accr = np.sum(predictions_dummy == y_test) / y_test.shape[0]
            dummy_macro_f1 = f1_score(y_test, predictions_dummy, average='macro')
            print("  Dummy Accuracy: {0:.2f}".format(dummy_accr))
            print("  Dummy F1-MACRO: {0:.2f}".format(dummy_macro_f1))
        else:
            dummy_clf = DummyClassifier(strategy="uniform")
            dummy_clf.fit(sentences, labels)
            predictions_dummy = dummy_clf.predict(sentences)

            dummy_accr = np.sum(predictions_dummy == labels) / labels.shape[0]
            dummy_macro_f1 = f1_score(labels, predictions_dummy, average='macro')
            print("  Dummy Accuracy: {0:.2f}".format(dummy_accr))
            print("  Dummy F1-MACRO: {0:.2f}".format(dummy_macro_f1))
        test_measures[name]['dummy_accr'] = dummy_accr
        test_measures[name]['dummy_f1'] = dummy_macro_f1

    return {
        'seed': seed,
        # 'best_accr': test_measures['indomain']['accr'],
        # 'best_f1': test_measures['indomain']['f1'],
        # 'dummy_accr': test_measures['indomain']['dummy_accr'],
        # 'dummy_f1': test_measures['indomain']['dummy_f1'],
        'best_accr_sw': test_measures['ood_sw']['accr'],
        'best_f1_sw': test_measures['ood_sw']['f1'],
        'dummy_accr_sw': test_measures['ood_sw']['dummy_accr'],
        'dummy_f1_sw': test_measures['ood_sw']['dummy_f1'],
        'best_accr_bs': test_measures['ood_bs']['accr'],
        'best_f1_bs': test_measures['ood_bs']['f1'],
        'dummy_accr_bs': test_measures['ood_bs']['dummy_accr'],
        'dummy_f1_bs': test_measures['ood_bs']['dummy_f1'],
        # 'majority': y_majority
    }

def run_many_times(file_path):
    run_time = 50
    start_seed = 150
    result_record = pd.DataFrame([], columns=['seed', 'best_accr', 'best_f1', \
        'dummy_accr', 'dummy_f1', 'best_accr_sw', 'best_f1_sw',\
        'dummy_accr_sw', 'dummy_f1_sw', 'best_accr_bs', 'best_f1_bs',\
        # 'best_accr_mmatch', 'best_f1_mmatch', 'best_accr_mismatch', 'best_f1_mismatch',\
        'dummy_accr_bs', 'dummy_f1_bs'])
    testloaders = {
        'ood_sw': load_test_data('./data/valid_data_cond1.csv'),
        'ood_bs': load_test_data('./data/valid_data_baseline.csv')
    }

    sentences, labels = load_data(file_path)
    logit_df = pd.DataFrame(0, index =np.arange(len(sentences)), columns=[0,1,2])
    easy_and_hard_df = pd.DataFrame(0, index =np.arange(len(sentences)), columns=[0,1])
    for i in range(run_time):
        result_df = run(sentences, labels, testloaders, logit_df, seed=start_seed+i)
        # for idx in easy_examples:
        #     easy_and_hard_df.iloc[idx,0] += 1
        # for idx in hard_examples:
        #     easy_and_hard_df.iloc[idx,1] += 1

        result_record = result_record.append(result_df, ignore_index=True)
        result_record.to_csv('./output/result_multiart_ood_{0}.csv'.format(start_seed))
        # result_record.to_pickle('./output/result_multiart.pkl')
        # logit_df.to_csv('./output/result_multiart_logit_ood_{0}.csv'.format(start_seed))
        # easy_and_hard_df.to_csv('./output/result_multiart_eandh_ood_{0}.csv'.format(start_seed))
        print("RUNTIME is", i)

def load_test_data(file_path, batch_size=32, tsv=False):

    df = pd.read_csv(file_path)
    sentences = df.h.values
    raw_labels = df.label.values

    tokenizer = AlbertTokenizer.from_pretrained('albert-base-v2')
    encoding = tokenizer(list(sentences), return_tensors='pt', padding=True, truncation=True, max_length=128).to(device)

    input_ids = encoding['input_ids']
    attention_masks = encoding['attention_mask']
    labels = torch.tensor(raw_labels).unsqueeze(1).to(device)
    test_dataset = TensorDataset(input_ids, attention_masks, labels)

    # Dataloading
    prediction_sampler = SequentialSampler(test_dataset)
    test_dataloader = DataLoader(test_dataset, sampler=prediction_sampler, batch_size=batch_size)

    return (test_dataloader, sentences, raw_labels)

def load_data(file_path):
    # base_df = pd.read_csv('data/valid_data_multiart.csv')
    df = pd.read_csv(file_path)

    sentences = df.h.values
    labels = df.label.values
    return sentences, labels

def main():
    file_path = 'data/valid_data_multiart.csv'
    run_many_times(file_path)

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
# Copyright (c) 2018-2021, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
from typing import Tuple, Union

from wetterdienst.exceptions import InvalidEnumeration, InvalidParameter
from wetterdienst.metadata.period import Period
from wetterdienst.metadata.resolution import Resolution
from wetterdienst.provider.dwd.observation.metadata import (
    DwdObservationDataset,
    DwdObservationParameter,
)
from wetterdienst.provider.dwd.observation.metadata.dataset import (
    RESOLUTION_DATASET_MAPPING,
)
from wetterdienst.provider.dwd.observation.metadata.parameter import (
    PARAMETER_TO_DATASET_MAPPING,
)
from wetterdienst.util.enumeration import parse_enumeration_from_template

def create_parameter_to_dataset_combination(
    parameter: Union[DwdObservationParameter, DwdObservationDataset],
    resolution: Resolution,
) -> Tuple[
    Union[DwdObservationParameter, DwdObservationDataset],
    DwdObservationDataset,
]:
    """Function to create a mapping from a requested parameter to a provided parameter
    set which has to be downloaded first to extract the parameter from it"""
    try:
        parameter_ = parse_enumeration_from_template(
            parameter, DwdObservationParameter[resolution.name]
        )

        parameter = PARAMETER_TO_DATASET_MAPPING[resolution][parameter_]

        return parameter, parse_enumeration_from_template(
            parameter.__class__.__name__, DwdObservationDataset
        )
    except (KeyError, InvalidEnumeration):
        try:
            parameter_set = parse_enumeration_from_template(
                parameter, DwdObservationDataset
            )

            return parameter_set, parameter_set
        except InvalidEnumeration:
            raise InvalidParameter(
                f"parameter {parameter} could not be parsed for "
                f"time resolution {resolution}"
            )

def check_dwd_observations_dataset(
    dataset: DwdObservationDataset,
    resolution: Resolution,
    period: Period,
) -> bool:
    """
    Function to check for element (alternative name) and if existing return it
    Differs from foldername e.g. air_temperature -> tu
    """
    check = RESOLUTION_DATASET_MAPPING.get(resolution, {}).get(dataset, [])

    if period not in check:
        return False

    return True


from IPython.display import display, Image
from random import random, randint
import string
import math

print("Все верно!")

def test(ex, *args):
    if ex == '1.1':

        if ( isinstance(args[0], float) and isinstance(args[1], int) and isinstance(args[2], int) ):
            if ( int(args[0]+args[1]) == args[2]):
                print("Все верно!")
                return Image(url='https://www.meme-arsenal.com/memes/22e0fbcffd5b78ef953409238a031c2b.jpg', width=250, height=250)
            else:
                print("dest должна равняться ark + pizza \nПример: dest = int(ark + pizza)")
                return Image(url='https://www.irishtimes.com/polopoly_fs/1.2809144.1475080968!/image/image.jpg_gen/derivatives/box_620_330/image.jpg'\
                         ,width=250, height=250 )
        else:
            print("Перечитай задание=) и попробуй снова.")
            return Image(url='https://www.irishtimes.com/polopoly_fs/1.2809144.1475080968!/image/image.jpg_gen/derivatives/box_620_330/image.jpg'\
                         ,width=250,height=250)

    if ex == '2.1':
        print(f"Debug: l_result_1 == {args[0]}, l_result_2 == {args[1]}, l_result_3 == {args[2]}, l_result_4 == {args[3]}\n")

        if( args[0] == True and args[1] == False and args[2] == True and args[3] == False ):
            print("Все верно!")
            print("Хорошая работа=)")
            return Image(url='https://media.giphy.com/media/cQQd1c5xFas4U/giphy.gif', width=500, height=500)

        else:
            print("Не верно, проверь Debug: строку.")
            return Image(url='https://media.giphy.com/media/9Y5BbDSkSTiY8/giphy.gif', width=500, height=500)

    if ex == '3.1':

        if ( (args[0] > 900) and (args[1] == 12) ) or ( (args[0] < 50) and (args[1] == 1) ) or ( (args[0] >= 55) and (args[1] == 15) ):
            print("Все верно!")
            print("Хорошая работа=)")
            return Image(url='https://media.giphy.com/media/cQQd1c5xFas4U/giphy.gif', width=500, height=500)

        else:
            print("Неверно  =((((")
            return Image(url='https://media.giphy.com/media/9Y5BbDSkSTiY8/giphy.gif', width=500, height=500)


from django.shortcuts import render, get_object_or_404,HttpResponseRedirect
from django.http import HttpResponseBadRequest
from django.views.generic.edit import CreateView, DeleteView
from django.contrib.auth.decorators import login_required
from .mixins import CreaSezioneMixin
from .models import Discussione,Post,Sezione
from .forms import AggiungiRisposta,FormDiscussione

# Create your views here.

class CreaSezione(CreaSezioneMixin,CreateView):
    model = Sezione
    fields = "__all__"
    template_name = "forum/crea_sezione.html"
    success_url = "/"

def show_sezione(request, pk):
    sezione = get_object_or_404(Sezione, pk=pk)
    discussioni = Discussione.objects.filter(sezione_appartenenza = sezione).order_by("data")
    context = {"sezione":sezione, "discussioni": discussioni}
    return render(request, "forum/show_sezione.html", context)

@login_required
def crea_discussione(request,pk):
    sezione = get_object_or_404(Sezione, pk = pk)
    if request.method == "POST":
        form = FormDiscussione(request.POST)
        if form.is_valid():
            discussione = form.save(commit=False)
            discussione.sezione_appartenenza = sezione
            discussione.creatore = request.user
            discussione.save()
            primo_post = Post.objects.create(
            discussione_appartenenza = discussione,
            creatore = request.user , testo = form.cleaned_data["contenuto"])
            return HttpResponseRedirect(discussione.get_absolute_url())

    else:
        form = FormDiscussione()
    context = {"form": form}
    return render(request, "forum/crea_discussione.html", context)

def show_discussione(request, pk):
    discussione = get_object_or_404(Discussione, pk=pk)
    posts = Post.objects.filter(discussione_appartenenza = discussione)
    form_risposta = AggiungiRisposta()
    context = {"discussione": discussione , "posts": posts, "form_risposta": form_risposta}
    return render(request, "forum/show_discussione.html", context)

def aggiungi_risposta(request, pk):
    discussione = get_object_or_404(Discussione, pk=pk)
    if request.method == "POST":
        form = AggiungiRisposta(request.POST)
        if form.is_valid():
            form.save(commit=False)
            form.instance.discussione_appartenenza = discussione
            form.instance.creatore = request.user
            form.save()
            return HttpResponseRedirect(discussione.get_absolute_url())
    else:
        return HttpResponseBadRequest()

class CancellaPost(DeleteView):
    model = Post
    success_url = "/"


import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import preprocessing
import pylab

df = pd.read_excel("C:\\Users\\Avinash\\Desktop\\Assignments\\Hierarchical_clustering\\EastWestAirlines.xlsx", sheet_name = 'data')

df.info() #To check details of the dataset

#To rename column names
Airline = df.rename(columns={'Award?':'Award'},inplace=True)
Airline = df.rename(columns={'ID#':'ID'},inplace=True)

#To drop unwanted columns
Airline = df.drop(["ID","Award", "Qual_miles","cc2_miles","cc3_miles"], axis=1)
Airline

Airline.shape # To check rows and columns
Airline.columns #To check column names of dataset
Airline.duplicated().sum() #To check the number of duplicate rows
Airline.drop_duplicates(keep=False,inplace=True)

#There are no duplicate rows present in the dataset
#-------------------------------------------------------------------------------------------
Airline.isna().sum()#To Find missing values in the columns
#There is no missing values in the dataset
Airline.isnull().sum()
#-------------------------------------------------------------------------------------------

#a=Airline["cc3_miles"].nunique() #To count unique values in the column

plt.boxplot(Airline.Balance)
'''
#------------------------------------------------------------------------------------
#To check the count of outliers in each column
Q1 = Airline.quantile(0.25)
Q3 = Airline.quantile(0.75)
IQR = Q3 - Q1
count=((Airline < (Q1 - 1.5 * IQR)) | (Airline > (Q3 + 1.5 * IQR))).sum()
count
#-------------------------------------------------------------------------------------
'''
df_winsorize = Airline.copy(deep=True)
stats.mstats.winsorize(a=df_winsorize['Balance'], limits=(0, 0.07), inplace=True)
df_winsorize.boxplot(column=['Balance'])

df_winsorize = Airline.copy(deep=True)
stats.mstats.winsorize(a=df_winsorize['Bonus_miles'], limits=(0, 0.07), inplace=True)
df_winsorize.boxplot(column=['Bonus_miles'])

df_winsorize = Airline.copy(deep=True)
stats.mstats.winsorize(a=df_winsorize['Bonus_trans'], limits=(0, 0.02), inplace=True)
df_winsorize.boxplot(column=['Bonus_trans'])

df_winsorize = Airline.copy(deep=True)
stats.mstats.winsorize(a=df_winsorize['Flight_miles_12mo'], limits=(0, 0.15), inplace=True)
df_winsorize.boxplot(column=['Flight_miles_12mo'])

############### 1. Remove (let's trimm the dataset) ################
# Trimming Technique
# let's flag the outliers in the data set
#---------------------------------------------------------------------------------------
'''IQR = Airline['Flight_miles_12mo'].quantile(0.75) - Airline['Flight_miles_12mo'].quantile(0.25)
IQR
lower_limit = Airline['Flight_miles_12mo'].quantile(0.25) - (IQR * 1.5)
lower_limit
upper_limit = df['Flight_miles_12mo'].quantile(0.75) + (IQR * 1.5)
upper_limit
outliers_df = np.where(Airline['Flight_miles_12mo'] > upper_limit, True, np.where(Airline['Flight_miles_12mo'] < lower_limit, True, False))
df_trimmed = Airline.loc[~(outliers_df), ]
Airline.shape, df_trimmed.shape

# let's explore outliers in the trimmed dataset
sns.boxplot(df_trimmed.Flight_miles_12mo);plt.title('Boxplot');plt.show()
#--------------------------------------------------------------------------------------
'''

##############################################
#pd.get_dummies(Airline) #To create dummy variables
#############################################

'''
#Transformation
stats.probplot(df.Balance, dist="norm",plot=pylab)
stats.probplot(df.Qual_miles, dist="norm",plot=pylab)
stats.probplot(df.Bonus_miles, dist="norm",plot=pylab)
stats.probplot(df.Bonus_trans, dist="norm",plot=pylab)
stats.probplot(df.Flight_miles_12mo, dist="norm",plot=pylab)
stats.probplot(df.Days_since_enroll, dist="norm",plot=pylab)

#To convert to normal by applying suitable function
stats.probplot(np.log(df.Balance),dist="norm",plot=pylab)
stats.probplot(np.log(df.Qual_miles),dist="norm",plot=pylab)
stats.probplot(np.log(df.Bonus_miles),dist="norm",plot=pylab)
stats.probplot(np.log(df.Bonus_trans),dist="norm",plot=pylab)
stats.probplot(np.log(df.Flight_miles_12mo),dist="norm",plot=pylab)
stats.probplot(np.log(df.Days_since_enroll),dist="norm",plot=pylab)
#sqrt,exp,log,reciprocal
'''

'''
#To standardise the data
list(Airline)
standardize = preprocessing.StandardScaler()
standardize_array = standardize.fit_transform(df)
df_standard = pd.DataFrame(standardize_array, columns=list(Airline))
df_standard
'''
#To normalise the data
normalize = preprocessing.MinMaxScaler()
normalize_array = normalize.fit_transform(Airline)
df_normal = pd.DataFrame(normalize_array,columns=list(Airline))
df_normal

from scipy.cluster.hierarchy import linkage
import scipy.cluster.hierarchy as sch

z = linkage(df_normal, method = "complete", metric = "euclidean")

# Dendrogram
plt.figure(figsize=(15, 8));plt.title('Hierarchical Clustering Dendrogram');plt.xlabel('Index');plt.ylabel('Distance')
sch.dendrogram(z,
    leaf_rotation = 0,  # rotates the x axis labels
    leaf_font_size = 10 # font size for the x axis labels
)
plt.show()

from sklearn.cluster import AgglomerativeClustering

h_complete = AgglomerativeClustering(n_clusters = 4, linkage = 'complete', affinity = "euclidean").fit(df_normal)
h_complete.labels_

cluster_labels = pd.Series(h_complete.labels_)

Airline['clust'] = cluster_labels # creating a new column and assigning it to new column

Airline = Airline.iloc[:, [7,0,1,2,3,4,5,6]]
Airline.head()

# Aggregate mean of each cluster
Airline=Airline.iloc[:, 1:].groupby(Airline.clust).mean()

Airline.to_excel("EastWestAirlines.xlsx", encoding = "utf-8")

import os
os.getcwd()


"""raspPinball hardware plateform"""

import logging
import asyncio
import time

from mpf.platforms.rasppinball.keypad import Keypad

from mpf.devices.driver import ConfiguredHwDriver
from mpf.core.platform import MatrixLightsPlatform, LedPlatform, SwitchPlatform, DriverPlatform
from mpf.platforms.interfaces.switch_platform_interface import SwitchPlatformInterface
from mpf.platforms.interfaces.driver_platform_interface import DriverPlatformInterface
from mpf.platforms.interfaces.rgb_led_platform_interface import RGBLEDPlatformInterface
from mpf.platforms.base_serial_communicator import BaseSerialCommunicator

from neopixel import * # ok sur raspberry
#from neopixel import neopixel #don't find it on raspberry

#class HardwarePlatform(MatrixLightsPlatform, LedPlatform, SwitchPlatform, DriverPlatform):
class HardwarePlatform(SwitchPlatform, DriverPlatform, LedPlatform):
    """Platform class for the raspPinball hardware.

    Args:
        machine: The main ``MachineController`` instance.

    """

    okey = None
    osw = None
    fake_keys = [" ", "H", "AH", "H", " ", "M", " ", "I", "J", "I", " ", "J", "I", " ", "J", "K", " ",  "N", " ", "M", "H", "H"]
    fake_idx = 0
    fake_cnt = 0

    def __init__(self, machine):
        """Initialise raspPinball platform."""
        super(HardwarePlatform, self).__init__(machine)
        self.log = logging.getLogger('RASPPINBALL')
        self.log.info("Configuring raspPinball hardware.")
        self._poll_task = None
        self.strips = dict()
        self.switches = dict()
        self.drivers = dict()
        self.leds = dict()
        #self.serial_connections = dict()
        self.communicator = None

    def __repr__(self):
        """Return string representation."""
        return '<Platform.raspPinball>'

    def initialize(self):
        """Initialise connections to raspPinball hardware."""
        self.log.info("Initialize raspPinball hardware.")

        self.config = self.machine.config['rasppinball']
        self.machine.config_validator.validate_config("rasppinball", self.config)
        print("***************************")
        print(self.config)
        #self.machine_type = (
        #    self.machine.config['hardware']['driverboards'].lower())

        self._connect_to_hardware()

        #  keypad
        self._kp = Keypad()
        self.old_key = ""
        self.key = ""
        #  leds
        self.init_strips()

    def stop(self):
        # TODO: send a stop to arduino
        pass

    def init_strips(self):
        """read strips config and init objects"""
        #!!161126:VG:init_strips
        # read only one for now...
        #self.machine.config_validator.validate_config("rasp_strip_leds", rasp_strip_leds)
        #strip_config = self.config
        #self.strip = neopixel.Adafruit_NeoPixel(
        #    strip_config['count'], strip_config['pin'], strip_config['freq'], strip_config['dma'],
        #    strip_config['invert'], strip_config['brightness'])

        self.strip = Adafruit_NeoPixel(64, 18, 800000, 5, False, 255)
        # Intialize the library (must be called once before other functions).
        self.strip.begin()
        #self.strips[strip_name] = self.strip
        self.strip.updated = False

    def fake_keypad(self):
        c = self.fake_keys[self.fake_idx]

        if c == ' ':
            max = 800
        else:
            max = 10

        self.fake_cnt += 1
        if self.fake_cnt > max:
            self.fake_cnt = 0
            self.fake_idx += 1

        try:
            c = self.fake_keys[self.fake_idx]
        except:
            c = " "
            self.fake_idx = 0

        return c

    def update_kb(self):
        s = self._kp.getKeys()
        #self.key = ""
        #s = self.fake_keypad()
        #if len(s) > 0:
        #    self.log.info(s)
        #    print("KB:", s)
        if s != self.old_key:
            #print("Keys:%s" % s)

            #   disable sw
            for num, sw in self.switches.items():
                if (num in self.old_key) and (not num in s):
                    #print ("%s OFF" % num)
                    self.machine.switch_controller.process_switch_by_num(sw.number, state=0, platform=self, logical=False)

            for num, sw in self.switches.items():
                if (not num in self.old_key) and (num in s):
                    #print ("%s ON" % num)
                    self.machine.switch_controller.process_switch_by_num(sw.number, state=1, platform=self, logical=False)

            self.old_key = s

    def tick(self, dt):
        """check with tick..."""
        del dt
        self.update_kb()

        #if self.strip.updated:
        #    self.strip.updated = False
        self.strip.show()

        #  resent frame not acked by Arduino
        self.communicator.resent_frames()

    def get_hw_switch_states(self):
        """Get initial hardware switch states."""
        hw_states = dict()
        #k = self._kp.keypad()
        k = ""
        for number, sw in self.switches.items():
            if number == k:
                hw_states[number] = 1
            else:
                hw_states[number] = 0
        return hw_states

    def _get_pulse_ms_value(self, coil):
        if coil.config['pulse_ms']:
            return coil.config['pulse_ms']
        else:
            # use mpf default_pulse_ms
            return self.machine.config['mpf']['default_pulse_ms']

    def configure_switch(self, config: dict):
        """Configure a switch.

        Args:
            config: Config dict.
        """
        #print(config)
        number = config['number']
        self.log.debug("configure_switch(%s)" % (number))
        switch = RASPSwitch(config, number)
        self.switches[number] = switch
        return switch

    #def configure_driver(self, config):
    def configure_driver(self, config: dict):
        """Configure a driver.

        Args:
            config: Config dict.
        """
        #print(config)
        number = config['number']
        self.log.debug("configure_driver(%s)" % (number))
        driver = RASPDriver(config, number, self)
        self.drivers[number] = driver
        return driver

    def configure_led(self, config, channels):
        """Subclass this method in a platform module to configure an LED.

        This method should return a reference to the LED's platform interface
        object which will be called to access the hardware.

        Args:
            channels (int): Number of channels (typically 3 for RGB).
            config (dict): Config of LED.

        """
        number = config['number']
        self.log.debug("configure_led(%s)" % number)
        #strip = self.strips[0]
        strip = self.strip
        led = RASPLed(config, number, strip)
        self.leds[number] = led
        return led

    def clear_hw_rule(self, switch, coil):
        """Clear a hardware rule.

        This is used if you want to remove the linkage between a switch and
        some driver activity. For example, if you wanted to disable your
        flippers (so that a player pushing the flipper buttons wouldn't cause
        the flippers to flip), you'd call this method with your flipper button
        as the *sw_num*.

        """
        self.log.info("clear_hw_rule(coil=%s sw=%s)" %
                       (coil.hw_driver.number, switch.hw_switch.number))
        self.communicator.rule_clear(coil.hw_driver.number, switch.hw_switch.number)

    def set_pulse_on_hit_rule(self, enable_switch, coil):
        """Set pulse on hit rule on driver.

        Pulses a driver when a switch is hit. When the switch is released the pulse continues. Typically used for
        autofire coils such as pop bumpers.
        """
        self.log.info("set_pulse_on_hit_rule(coil=%s sw=%s)" %
                       (coil.hw_driver.number, enable_switch.hw_switch.number))
        self.communicator.rule_add(1, coil.hw_driver.number, enable_switch.hw_switch.number,
                                   duration=self._get_pulse_ms_value(coil))

    def set_pulse_on_hit_and_release_rule(self, enable_switch, coil):
        """Set pulse on hit and release rule to driver.

        Pulses a driver when a switch is hit. When the switch is released the pulse is canceled. Typically used on
        the main coil for dual coil flippers without eos switch.
        """
        self.log.info("set_pulse_on_hit_and_release_rule(coil=%s sw=%s)" %
                       (coil.hw_driver.number, enable_switch.hw_switch.number))
        self.communicator.rule_add(2, coil.hw_driver.number, enable_switch.hw_switch.number,
                                   duration=self._get_pulse_ms_value(coil))

    def set_pulse_on_hit_and_enable_and_release_rule(self, enable_switch, coil):
        """Set pulse on hit and enable and relase rule on driver.

        Pulses a driver when a switch is hit. Then enables the driver (may be with pwm). When the switch is released
        the pulse is canceled and the driver gets disabled. Typically used for single coil flippers.
        """
        self.log.info("set_pulse_on_hit_and_enable_and_release_rule(coil=%s sw=%s)" %
                       (coil.hw_driver.number, enable_switch.hw_switch.number))
        self.communicator.rule_add(3, coil.hw_driver.number, enable_switch.hw_switch.number,
                                   duration=self._get_pulse_ms_value(coil))

    def set_pulse_on_hit_and_enable_and_release_and_disable_rule(self, enable_switch, disable_switch, coil):
        """Set pulse on hit and enable and release and disable rule on driver.

    Pulses a driver when a switch is hit. Then enables the driver (may be with pwm). When the switch is released
        the pulse is canceled and the driver gets disabled. When the second disable_switch is hit the pulse is canceled
        and the driver gets disabled. Typically used on the main coil for dual coil flippers with eos switch.
        """
        self.log.info("set_pulse_on_hit_and_enable_and_release_and_disable_rule(coil=%s sw=%s dis_sw=%s)" %
                       (coil.hw_driver.number, enable_switch.hw_switch.number, disable_switch.hw_switch.number))
        self.communicator.rule_add(4, coil.hw_driver.number, enable_switch.hw_switch.number, disable_sw_id=disable_switch.hw_switch.number,
                                   duration=self._get_pulse_ms_value(coil))

    def _connect_to_hardware(self):
        """Connect to each port from the config.

        This process will cause the connection threads to figure out which processor they've connected to
        and to register themselves.
        """
        if False:  # !!!TEMP:need to validate config...
            if len(self.config['ports']) > 1:
                self.log.fatal("only one slave com port is supported")
            if len(self.config['ports']) == 0:
                self.log.warning("no communication port setted!")
                return
            port = self.config['ports'][0]
            self.communicator = RaspSerialCommunicator(
                platform=self, port=port,
                baud=self.config['baud'])
        self.communicator = RaspSerialCommunicator(
            platform=self, port='/dev/ttyAMA0',
            baud=115200)

    def process_received_message(self, msg: str):
        """Send an incoming message from the FAST controller to the proper method for servicing.

        Args:
            msg: messaged which was received
        """
        all = msg.split(":")
        if len(all) < 2:
          self.log.warning("Recv bad formated cmd", msg)
          return
        cmd, all_param = all[:2]
        params = all_param.split(";")

        self.strip.setPixelColorRGB(0, 0, 0, 0)
        if cmd == "":
            pass
        elif cmd == "SWU":      # switch update
            sw_id = params[0]
            sw_state = int(params[1])
            self.machine.switch_controller.process_switch_by_num(sw_id, state=sw_state, platform=self, logical=False)
            self.strip.setPixelColorRGB(0, 0, 0, 0xff)  # blue
        elif cmd == "DBG":      # debug message
            self.log.debug("RECV:%s" % msg)
        elif cmd == "INF":      # debug message
            self.log.info("RECV:%s" % msg)
        elif cmd == "WRN":  # warning message
            self.log.warning("RECV:%s" % msg)
            self.strip.setPixelColorRGB(0, 0xff, 0xff, 0)  # yellow
        elif cmd == "ERR":  # warning message
            self.log.error("RECV:%s" % msg)
            self.strip.setPixelColorRGB(0, 0xff, 0, 0)  # red
        elif cmd == "TCK": # arduino is alive !
            self.log.debug("TCK ok:%d" % int(params[0]))
        elif cmd == "ACK": # ack of frame
            self.communicator.ack_frame(int(params[0]), params[1] == "OK")
            self.strip.setPixelColorRGB(0, 0, 0xff, 0)  # green
        else:
            self.log.warning("RECV:UNKNOWN FRAME: [%s]" % msg)
        l = len(self.communicator.frames)
        #TODO: self.machine['frame_cnt'] = l
        self.strip.show()
        self.machine.events.post_async('raspberry_frame_count', frame_cnt=l, frames=self.communicator.frames)

class RASPDriver(DriverPlatformInterface):

    def __init__(self, config, number, platform):
        """Initialise driver."""
        super().__init__(config, number)
        self.platform = platform
        self.log = logging.getLogger('RASPDriver')
        self.log.info("Driver Settings for %s", self.number)

    def disable(self, coil):
        """Disable the driver."""
        self.log.info("RASPDriver.Disable(%s %s)" % (coil.config['label'], coil.hw_driver.number))
        self.platform.communicator.driver_disable(coil.hw_driver.number)
        pass

    def enable(self, coil):
        """Enable this driver, which means it's held "on" indefinitely until it's explicitly disabled."""
        self.log.info("RASPDriver.Enable(%s %s)" % (coil.config['label'], coil.hw_driver.number))
        self.platform.communicator.driver_enable(coil.hw_driver.number)
        pass

    def get_board_name(self):
        """Return the name of the board of this driver."""
        pass

    def pulse(self, coil, milliseconds):
        """Pulse a driver.

        Pulse this driver for a pre-determined amount of time, after which
        this driver is turned off automatically. Note that on most platforms,
        pulse times are a max of 255ms. (Beyond that MPF will send separate
        enable() and disable() commands.

        Args:
            milliseconds: The number of ms to pulse this driver for. You should
                raise a ValueError if the value is out of range for your
                platform.

        Returns:
            A integer of the actual time this driver is going to be pulsed for.
            MPF uses this for timing in certain situations to make sure too
            many drivers aren't activated at once.

        """
        self.log.info("RASPDriver.Pulse(%s %s, %d ms)" %
                       (coil.config['label'], coil.hw_driver.number, milliseconds))
        self.platform.communicator.driver_pulse(coil.hw_driver.number, milliseconds)
        return milliseconds

class RASPSwitch(SwitchPlatformInterface):

    def __init__(self, config, number):
        """Initialise switch."""
        super().__init__(config, number)
        self.log = logging.getLogger('RASPSwitch')
        #self.notify_on_nondebounce = notify_on_nondebounce
        #self.hw_rules = {"closed_debounced": [],
        #                 "closed_nondebounced": [],
        #                 "open_debounced": [],
        #                 "open_nondebounced": []}

class RASPLed(RGBLEDPlatformInterface):

    def __init__(self, config, number, strip):
        """Initialise led."""
        self.number = number
        self.current_color = '000000'
        self.log = logging.getLogger('RASPLed')
        self.strip = strip

    def color(self, color):
        """Set the LED to the specified color.

        Args:
            color: a list of int colors. one for each channel.

        Returns:
            None
        """
        #self._color = color
        new_color = "{0}{1}{2}".format(hex(int(color[0]))[2:].zfill(2),
                                       hex(int(color[1]))[2:].zfill(2),
                                       hex(int(color[2]))[2:].zfill(2))
        #self.log.info("RASPLes.color(%s : %s -> %s)" % (self.number, color, new_color))
        #print("color(%s -> %s)" % (self.number, new_color))
        try:
            self.current_color = new_color
            #self.strip.setPixelColor(int(self.number), self.current_color)
            self.strip.setPixelColorRGB(int(self.number), color[0], color[1], color[2])

            self.strip.updated = True
        except Exception as e:
            self.log.error("led update error" + str(e))

class RaspSerialCommunicator(BaseSerialCommunicator):
    """Protocol implementation to the Arduino"""

    def __init__(self, platform, port, baud):
        """Initialise communicator. """
        self.frame_nb = 0
        self.received_msg = ''
        self.frames = {}
        super().__init__(platform, port, baud)

    def _parse_msg(self, msg):
        """Parse a message.

        Msg may be partial.
        Args:
            msg: Bytes of the message (part) received.
        """
        try:
            self.received_msg += msg.decode()
        except:
            self.log.warning("invalid parse frame '%s'" % msg)

        while True:
            pos = self.received_msg.find('\r')
            if pos == -1: # no full msg
                break
            m = self.received_msg[:pos].strip()
            if not len(m):
                break
            self.platform.process_received_message(m)
            self.received_msg = self.received_msg[pos + 1:]

    @asyncio.coroutine
    def _identify_connection(self):
        """Initialise and identify connection."""
        pass #nothing to identify...
        #raise NotImplementedError("Implement!")

    def __send_frame(self, frame_nb, msg):
        """send a frame, store id and date it"""
        if frame_nb in self.frames:
            retry  = self.frames[frame_nb]['retry'] + 1
        else:
            retry = 0
        if retry > 5:
            self.log.error('SEND:too many retry (%d) for frame "%s"' % (retry, msg))
            self.frames.pop(frame_nb)
            return
        self.frames[frame_nb] = {'msg': msg, 'time': time.time(), 'retry': retry}
        s = "!%d:%s\n" % (self.frame_nb, msg)
        self.log.info('SEND:%s' % s)
        self.send(s.encode())

    def __send_msg(self, msg):
        """send a new frame"""
        self.frame_nb += 1
        self.__send_frame(self.frame_nb, msg)

    def ack_frame(self, frame_nb, result):
        """an ack has been received, delete the accodring frame in buffer"""
        #!!170514:VG:Remove the frame only if ACK OK
        if frame_nb in self.frames:
            if not result:
                self.log.error("ACK frame error '%s'" % self.frames[frame_nb])
            else:
                self.frames.pop(frame_nb)

    def resent_frames(self):
        """resent all frame not acked after a timeout of 250ms"""
        try:
            for k,f in self.frames.items():
                if time.time() - f['time'] > 0.500:
                    self.log.warning("resend frame %d:%s" % (k, f['msg']))
                    self.__send_frame(k, f['msg'])
        except RuntimeError:
            pass  # dictionary changed size during iteration

    def rule_clear(self, coil_pin, enable_sw_id):
        msg = "RC:%s:%s" % (coil_pin, enable_sw_id)
        self.__send_msg(msg)

    def rule_add(self, hwrule_type, coil_pin, enable_sw_id='0', disable_sw_id='0', duration=10):
        msg = "RA:%d:%s:%s:%s:%d" % (hwrule_type, coil_pin, enable_sw_id, disable_sw_id, duration)
        self.__send_msg(msg)

    def driver_pulse(self, coil_pin, duration):
        #!!170418:VG:Add duration
        msg = "DP:%s:%d" % (coil_pin, duration)
        self.__send_msg(msg)

    def driver_enable(self, coil_pin):
        msg = "DE:%s" % (coil_pin)
        self.__send_msg(msg)

    def driver_disable(self, coil_pin):
        msg = "DD:%s" % (coil_pin)
        self.__send_msg(msg)


import torch
import torch.nn as nn
from auto_gradient import Variable

torch.manual_seed(777)

idx2char=['h','i','e','l','o']
x_data=[[0,1,0,2,3,3]]
x_one_hot=[[
    [1,0,0,0,0],
    [0,1,0,0,0],
    [1,0,0,0,0],
    [0,0,1,0,0],
    [0,0,0,1,0],
    [0,0,0,1,0]
]]

y_data=[1,0,2,3,3,4]

inputs=Variable(torch.Tensor(x_one_hot))
labels=Variable(torch.LongTensor(y_data))

num_classes=5
input_size=5
hidden_size=5
batch_size=1
sequence_length=6
num_layers=1

class RNN(nn.Module):
    def __init__(self,num_classes,input_size,hidden_size,num_layers):
        super(RNN,self).__init__()

        self.num_classes=num_classes
        self.num_layers=num_layers
        self.input_size=input_size
        self.hidden_size=hidden_size
        self.sequence_length=sequence_length

        self.rnn=nn.RNN(input_size=5,hidden_size=5,batch_first=True)

    def forward(self,x):
        h_0=Variable(torch.zeros(self.num_layers,x.size(0),self.hidden_size))
        x.view(x.size(0),self.sequence_length,self.input_size)
        out,_=self.rnn(x,h_0)
        return out.view(-1,num_classes)

rnn=RNN(num_classes,input_size,hidden_size,num_layers)
print(rnn)

criterion=torch.nn.CrossEntropyLoss()
optimizer=torch.optim.Adam(rnn.parameters(),lr=0.1)

for epoch in range(100):
    outputs=rnn(inputs)
    optimizer.zero_grad()
    loss=criterion(outputs,labels)

    loss.backward()
    optimizer.step()

    _,idx=outputs.max(1)
    idx=idx.data.numpy()
    result_str=[idx2char[c] for c in idx.squeeze()]
    print('epoch : %d,loss: %1.3f'%(epoch+1,loss.data[0]))
    print('Predicted string:',''.join(result_str))

print('Learning finished')


# -*- coding: utf-8 -*-
'''
project module
'''
from flask import Blueprint
from flask import request, render_template, redirect, url_for, abort, session, flash
from flask_login import login_required, current_user
from sqlalchemy import desc

from database import db
from models import Project, ProjectMeta, RecordSchema, Record, RecordMeta
from utils import genShortTag, generateEtag  # , print_log

import json

app = Blueprint("project", __name__, template_folder="templates", static_folder="static", url_prefix="/project")

@app.route("/")
@login_required
def index():
    projects = db.session.query(Project).filter(Project.owner == current_user._id).all()
    return render_template("list.html", projects=projects)

@app.route("/create", methods=["GET", "POST"])
@login_required
def create():
    if request.method == "POST":
        project_name = request.form.get("name")
        project_description = request.form.get("description")

        project = Project()
        project.name = project_name
        project.description = project_description
        project.owner = current_user._id
        project.tag = genShortTag()
        project._etag = generateEtag()

        db.session.add(project)
        db.session.commit()

        return redirect(url_for("project.index"))
    else:
        return render_template("create.html")

@app.route("/<tag>")
@login_required
def main(tag):
    project = db.session.query(Project).filter(Project.tag == tag).first()
    session["current_project"] = tag

    if project is None:
        abort(404)

    return render_template("main.html", project=project)

@app.route("/<tag>/record_schema", methods=["GET", "POST"])
@login_required
def recordSchema(tag):
    project = db.session.query(Project).filter(Project.tag == tag).first()
    if request.method == "GET":
        recordSchema = db.session.query(RecordSchema).filter(RecordSchema.project_id == project._id, RecordSchema.active == 1).first()
        if recordSchema is not None:
            form = json.loads(recordSchema.fields)
        else:
            form = None
        return render_template("record_schema.html", form=form)
    else:
        field_labels = request.form.getlist('field_label')
        field_types = request.form.getlist('field_type')
        field_names = request.form.getlist('field_name')
        options = request.form.getlist('options')
        status = request.form.getlist('status')

        fields = []

        for i, label in enumerate(field_labels):
            row = dict()
            row['order'] = i
            row['label'] = label
            if field_names[i] == '':
                name = genShortTag()
                row['name'] = name
            else:
                row['name'] = field_names[i]
            row['type'] = field_types[i]
            row['options'] = [x.strip() for x in options[i].split(',')]
            row['status'] = status[i]
            fields.append(row)

        form = db.session.query(RecordSchema).filter(RecordSchema.project_id == project._id, RecordSchema.active == 1).order_by(desc(RecordSchema.revision)).first()

        if form is None:
            recordSchema = RecordSchema()
            recordSchema.project_id = project._id
            recordSchema.revision = 1
            recordSchema.active = 1
            recordSchema.fields = json.dumps(fields)
            recordSchema._etag = generateEtag()
        else:
            recordSchema = RecordSchema()
            recordSchema.project_id = project._id
            recordSchema.revision = form.revision + 1
            recordSchema.active = 1
            form.active = 0
            recordSchema.fields = json.dumps(fields)
            recordSchema._etag = generateEtag()

        db.session.add(recordSchema)
        db.session.commit()

        return redirect(url_for("project.recordSchema", tag=tag))

@app.route("/<tag>/add_record", methods=["POST"])
@login_required
def add_record(tag):
    project = db.session.query(Project).filter(Project.tag == tag).first()
    schema = db.session.query(RecordSchema).filter(RecordSchema.project_id == project._id, RecordSchema.active == 1).order_by(desc(RecordSchema.revision)).first()
    record = Record()
    record.schema_id = schema._id
    record.project_id = project._id
    record._etag = generateEtag()
    db.session.add(record)
    db.session.commit()

    form = json.loads(schema.fields)

    for k, v in request.form.items():
        if k == "comment":
            record.comment = v
        else:
            record_meta = RecordMeta()
            record_meta.record_id = record._id
            record_meta.meta_key = k
            record_meta.meta_value = v
            record_meta._etag = generateEtag()

            for field in form:
                if field["name"] == k:
                    record_meta.order = field["order"]

            db.session.add(record_meta)

    db.session.commit()
    return redirect(url_for("project.recordSchema", tag=tag))

@app.route("/<tag>/record")
def record(tag):
    project = db.session.query(Project).filter(Project.tag == tag).first()
    schema = db.session.query(RecordSchema).filter(RecordSchema.project_id == project._id, RecordSchema.active == 1).order_by(desc(RecordSchema.revision)).first()
    records = db.session.query(Record).filter(Record.project_id == project._id).all()
    return render_template("record.html", records=records)

@app.route("/<tag>/record/<record_id>/delete", methods=["POST"])
def delete_record_item(tag, record_id):
    record = db.session.query(Record).filter(Record._id == record_id).first()
    for meta in record.meta:
        db.session.delete(meta)

    db.session.delete(record)
    db.session.commit()
    return redirect(url_for("project.record", tag=tag))

@app.route("/<tag>/record/<record_id>/edit", methods=["GET", "POST"])
def edit_record(tag, record_id):
    if request.method == "GET":
        record = db.session.query(Record).filter(Record._id == record_id).first()
        schema = db.session.query(RecordSchema).filter(RecordSchema._id == record.schema_id).first()
        if schema is not None:
            form = json.loads(schema.fields)
        else:
            form = None
        return render_template("record_edit.html", form=form, record=record)
    else:
        record = db.session.query(Record).filter(Record._id == record_id).first()
        schema = db.session.query(RecordSchema).filter(RecordSchema._id == record.schema_id).first()

        form = json.loads(schema.fields)

        record.comment = request.form.get("comment")
        for meta in record.meta:
            meta.meta_value = request.form.get(meta.meta_key)

            for field in form:
                if field["name"] == meta.meta_key:
                    meta.order = field["order"]

        db.session.commit()
        return redirect(url_for("project.record", tag=tag))

@app.route("/<tag>/settings", methods=["GET", "POST"])
def settings(tag):
    project = db.session.query(Project).filter(Project.tag == tag).first()
    if request.method == "GET":
        page_id = ""
        page_type = ""
        for meta in project.meta:
            if meta.meta_key == "page_id":
                page_id = meta.meta_value
            elif meta.meta_key == "page_type":
                page_type = meta.meta_value

        return render_template("settings.html", page_id=page_id, page_type=page_type)
    else:
        project = db.session.query(Project).filter(Project.tag == tag).first()
        page_id = request.form.get("page_id")
        page_type = request.form.get("page_type")

        if page_type != "" and db.session.query(ProjectMeta).filter(ProjectMeta.meta_key == "page_id", ProjectMeta.meta_value == page_id).first() is not None:
            flash("페이지 ID가 중복됩니다. 다른 ID를 사용하세요.")
            return redirect(url_for("project.settings", tag=tag))

        if page_type != "":
            page_id_meta = ProjectMeta()
            page_id_meta.project_id = project._id
            page_id_meta.meta_key = "page_id"
            page_id_meta.meta_value = page_id
            page_id_meta._etag = generateEtag()
            db.session.add(page_id_meta)

            page_type_meta = ProjectMeta()
            page_type_meta.project_id = project._id
            page_type_meta.meta_key = "page_type"
            page_type_meta.meta_value = page_type
            page_type_meta._etag = generateEtag()
            db.session.add(page_type_meta)
            db.session.commit()

        return redirect(url_for("project.settings", tag=tag))


import flask
from flask import request, Flask, render_template
import pickle
from pickle import load
import numpy as np

app = Flask(__name__)

@app.route('/')
def index():
	return render_template('index.html')

filename = 'model.pkl'
clf = load(open(filename, 'rb'))

@app.route('/predict', methods=['POST'])
def predict():
	if request.method == "POST":
		pregnancies = int(request.form['pregnancies'])
		glucose = int(request.form['glucose'])
		blood_pressure = int(request.form['blood_pressure'])
		skin_thickness = int(request.form['skin_thickness'])
		insulin = int(request.form['insulin'])
		bmi = float(request.form['bmi'])
		diabetes_pedigree_function = float(request.form['diabetes_pedigree_function'])
		age = int(request.form['age'])

		data = np.array([[pregnancies,glucose,blood_pressure, skin_thickness, insulin, bmi, diabetes_pedigree_function,age]])
		my_prediction = clf.predict(data)
		return render_template('result.html', prediction=my_prediction)

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