#==================================================
#===== SUB FUNCTION - vavc
# main_v? anpr_v? datalogger_v? vavc_v?
#==================================================

#==================================================
#===== yolov5 reference
# https://github.com/ultralytics/yolov5
# Python>=3.6.0 is required with all requirements.txt installed including PyTorch>=1.7:
#==================================================

#==================================================
#===== centernet reference
# https://medium.com/visionwizard/centernet-objects-as-points-a-comprehensive-guide-2ed9993c48bc
# https://bluehorn07.github.io/2021/06/03/CenterNet-tutorial-windows.html
# 211207 Setup Guide.txt
#==================================================

#==================================================
# HISTORY
#==================================================
# Parameter:
# 1. detection = [bbox, conf, class]
#===== 13/12/2021
# 1. study vavc
#==================================================
# PENDING
#==================================================
# 1.
#==================================================

#--------------------------------------------------
TSRT = False
draw_bbox_vehicle = False
draw_bbox_wheel = False
draw_bbox_plate = False
put_class_at_bottom = True
center_bbox = False

import os
import cv2
import numpy as np
import queue
import time
#import multiprocessing as mp

from configparser import ConfigParser
from scipy import ndimage
from scipy.ndimage import interpolation as inter  # def correct_skew
from collections import namedtuple  # def bb_intersection_over_union

import torch
import pandas as pd
from utils.torch_utils import select_device, time_sync
from models.experimental import attempt_load
from utils.general import set_logging, check_img_size, non_max_suppression, scale_coords, xyxy2xywh
from numpy import random
from utils.plots import Annotator

#==================================================
# vehicle classes
toll_dataset_vehicle_classes = [
	'class1_lightVehicle',
	'class2_mediumVehicle',
	'class3_heavyVehicle',
	'class4_taxi',
	'class5_bus' #,'class_motocycle'
	]

#==================================================
# variable
data_list = []
single_wheel = []
double_wheel = []
license_plate_taxi = []
license_plate = []
predicted_vehicle_data = []

#--------------------------------------------------
# Projection Profile Method to determine OCR image skew angle correction
def correct_skew(image, delta=1, limit=5):
	def determine_score(arr, angle):
		data = inter.rotate(arr, angle, reshape=False, order=0)
		histogram = np.sum(data, axis=1)
		score = np.sum((histogram[1:] - histogram[:-1]) ** 2)
		return histogram, score

	gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
	thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

	scores = []
	angles = np.arange(-limit, limit+delta, delta)
	for angle in angles:
		histogram, score = determine_score(thresh, angle)
		scores.append(score)

	best_angle = angles[scores.index(max(scores))]

	(h, w) = image.shape[:2]
	center = (w // 2, h // 2)
	M = cv2.getRotationMatrix2D(center, best_angle, 1.0)
	rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)

	#return best_angle, rotated
	return rotated

#--------------------------------------------------
# letterbox is convert image aspect ratio to deep learning model square base in stride without distort and fill remaining part to gray.
def letterbox(img, new_shape=(640,640), color=(114,114,114), auto=True, scaleFill=False, scaleup=True, stride=32):
	# Resize and pad image while meeting stride-multiple constraints
	shape = img.shape[:2]  # current shape [height, width]
#	print(f"org img.shape: {img.shape[:2][1]}, {img.shape[:2][0]}")
	if isinstance(new_shape, int):
		new_shape = (new_shape, new_shape)

	# Scale ratio (new / old)
	r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
	if not scaleup:  # only scale down, do not scale up (for better test mAP)
		r = min(r, 1.0)

	# Compute padding
	ratio = r, r  # width, height ratios
	new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
#	print(f"new_unpad: {new_unpad[0]}, {new_unpad[1]}")
	dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]  # wh padding
	if auto:  # minimum rectangle
		dw, dh = np.mod(dw, stride), np.mod(dh, stride)  # wh padding
	elif scaleFill:  # stretch
		dw, dh = 0.0, 0.0
		new_unpad = (new_shape[1], new_shape[0])
		ratio = new_shape[1] / shape[1], new_shape[0] / shape[0]  # width, height ratios

#	print(f"dw, dh: {dw}, {dh}")
	dw /= 2  # divide padding into 2 sides
	dh /= 2

	if shape[::-1] != new_unpad:  # resize
		img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
	top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
	left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
	img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)  # add border

#	print(f"border img.shape: {img.shape[:2][1]}, {img.shape[:2][0]}")
	return img, ratio, (dw, dh)

#--------------------------------------------------
# boundingBox = [xmin, ymin, xmax, ymax]
def paintBoundingBox(img, boundingBox, color=(255,0,0), label=None):
	boundingBox = np.array(boundingBox, int)
	if label:
		# cv2.putText(img, str(label), (boundingBox[0],boundingBox[3]-4), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
		cv2.putText(img, str(label), (boundingBox[0],boundingBox[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 0), 3) 	#make text border as black
		cv2.putText(img, str(label), (boundingBox[0],boundingBox[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)

	img = cv2.rectangle(img, (boundingBox[0],boundingBox[1]), (boundingBox[2],boundingBox[3]), color, 2)
	bboxCenterX = int((boundingBox[0] + boundingBox[2]) / 2)
	bboxCenterY = int((boundingBox[1] + boundingBox[3]) / 2)
	if center_bbox is True:
		cv2.circle(img, (bboxCenterX, bboxCenterY), 4, color, thickness=-1)
	return

#--------------------------------------------------
# non-maximum suppression is to remove redundant bounding boxes in object detection
def NMS(boxes, confidence_score, class_name, overlapThresh=0.4):
	#print(boxes)
	# if there are no boxes, return an empty list
	if len(boxes) == 0:
		return []
	# if the bounding boxes integers, convert them to floats -- this is important since we'll be doing a bunch of divisions
	if boxes.dtype.kind == "i":
		boxes = boxes.astype("float")
	#print(boxes)

	# initialize the list of picked indexes
	pick = []

	# grab the coordinates of the bounding boxes
	x1 = boxes[:,0]  # x coordinate of the top-left corner
	y1 = boxes[:,1]  # y coordinate of the top-left corner
	x2 = boxes[:,2]  # x coordinate of the bottom-right corner
	y2 = boxes[:,3]  # y coordinate of the bottom-right corner
	#print(x1)
	#print(y1)
	#print(x2)
	#print(y2)

	# compute the area of the bounding boxes and sort the bounding boxes by the bottom-right y-coordinate of the bounding box
	areas = (x2 - x1 + 1) * (y2 - y1 + 1)
	#print(areas)
	idxs = np.argsort(y2)
	#print(idxs)

	# keep looping while some indexes still remain in the indexes list
	while len(idxs) > 0:
		# grab the last index in the indexes list and add the index value to the list of picked indexes
		last = len(idxs) - 1
		i = idxs[last]
		pick.append(i)
	#	print(pick)
		# find the largest (x, y) coordinates for the start of the bounding box and the smallest (x, y) coordinates for the end of the bounding box
		xx1 = np.maximum(x1[i], x1[idxs[:last]])
		yy1 = np.maximum(y1[i], y1[idxs[:last]])
		xx2 = np.minimum(x2[i], x2[idxs[:last]])
		yy2 = np.minimum(y2[i], y2[idxs[:last]])
		# compute the width and height of the bounding box
		w = np.maximum(0, xx2 - xx1 + 1)
		h = np.maximum(0, yy2 - yy1 + 1)
		# compute the ratio of overlap
		overlap = (w * h) / areas[idxs[:last]]
		# delete all indexes from the index list that have
		idxs = np.delete(idxs, np.concatenate(([last], np.where(overlap > overlapThresh)[0])))
	# return only the bounding boxes that were picked using the integer data type
	return boxes[pick].astype("int")

#--------------------------------------------------
def updateDetectionClass(detected_vehicle, single_wheel, double_wheel, license_plate_taxi):
	if len(license_plate_taxi) > 0:
		detected_vehicle[2] = 'class4_taxi'
		print('Change to class 4')
	if detected_vehicle[2] == 'class2_mediumVehicle':
		if len(double_wheel) <= 1:
			detected_vehicle[2] = 'class2_mediumVehicle'
		else:
			detected_vehicle[2] = 'class3_heavyVehicle'
			print('Change to class 3 ')

	if detected_vehicle[2] == 'class1_lightVehicle':
		try:
			if len(single_wheel) == 2 and len(double_wheel) == 0:
				detected_vehicle[2] = 'class1_lightVehicle'
		except:
			pass

	return detected_vehicle

#--------------------------------------------------
def vehicleDetection(model_vavc, half, device, vavc_frame, imgROI):
	frame_raw = vavc_frame.copy()
	#--------------------------------------------------
	model = model_vavc
	# # Load model
	# #	stride = int(model.stride.max())  # model stride=32
	# stride = 32
	# imgsz = 640
	# imgsz = check_img_size(imgsz, s=stride)  # check img_size
	# if half:
	# 	model.half()  # to FP16 to engine

	# Get names and colors
	names = model.module.names if hasattr(model, 'module') else model.names
	colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]

	# Padded resize
	if(TSRT):
		stride = max(int(model.stride), 32)  # model stride=32
	else:
		stride = 32
	imgsz = [640,640]
	imgsz = check_img_size(imgsz, s=stride)  # check img_size
	img = letterbox(vavc_frame, imgsz, stride=stride, auto=False)[0] #to engine

	# img = letterbox(vavc_frame, imgsz, stride=stride)[0]

	# Process image
	#t1 = time_sync()
	img = img[:, :, ::-1].transpose(2, 0, 1)  # img[:, :, ::-1] is BGR to RGB, transpose(2, 0, 1) is HWC to CHW
	img = np.ascontiguousarray(img)
	img = torch.from_numpy(img).to(device)
	img = img.half() if half else img.float()  # uint8 to fp16/32
	img /= 255.0  # 0 - 255 to 0.0 - 1.0
	if img.ndimension() == 3:
		img = img.unsqueeze(0)
	#t2 = time_sync()

	# Inference
	if(TSRT):
		pred = model(img, augment=False)
	else:
		pred = model(img, augment=False)[0]		#to engine
	#t3 = time_sync()
	# NMS
	conf_thres = 0.45
	iou_thres = 0.5
	pred = non_max_suppression(pred, conf_thres, iou_thres)
	#t4 = time_sync()
	#--------------------------------------------------

	det_bbox_list = []
	det_conf_list = []
	det_cls_list = []
	# Process detections
	annotator = Annotator(vavc_frame, line_width=3, pil=not ascii)
	for i, det in enumerate(pred):  # detections per image
		gn = torch.tensor(vavc_frame.shape)[[1, 0, 1, 0]]  # normalization gain whwh
		if len(det):
			# Rescale boxes from img_size to vavc_frame size
			det[:, :4] = scale_coords(img.shape[2:], det[:, :4], vavc_frame.shape).round()
			# Write results
			for *xyxy, conf, cls in reversed(det):
				#print(f"{names[int(cls)]}, {float(conf):.3f}")
				if conf > 0.45:
					# annotator.box_label(xyxy, label=f"{names[int(cls)]}, {float(conf):.3f}", color=colors[int(cls)])

					det_bbox_list.append([int(xyxy[0]), int(xyxy[1]), int(xyxy[2]), int(xyxy[3])])  # [xmin, ymin, xmax, ymax]
					det_conf_list.append(round(float(conf),2))  # [conf]
					det_cls_list.append(names[int(cls)])  # [class]

	'''
	if 'license_plate' in det_cls_list:

		print([i for i in det_cls_list if i == 'license_plate'])
		det_cls_list_i = [det_cls_list.index(i) for i in det_cls_list if i == 'license_plate']
		print(int(det_cls_list_i[0]))
		print(det_bbox_list[int(det_cls_list_i[0])])'''

	#print(f"det_bbox_list:\n{det_bbox_list}")
	#print(f"det_conf_list:\n{det_conf_list}")
	#print(f"det_cls_list:\n{det_cls_list}")

	crop_img = None
	crop_conf = 'no plate detect'
	if not det_bbox_list:  # no class detect
		#print("no class detect")
		return vavc_frame, 'no class detect', crop_img, crop_conf, "", "", "", "", ""

	#--------------------------------------------------
	# vehicle class filter
	data_list = []  # [[0,0,0,0, 0.0, '']]
	ctr_y_data_list = 0
	for index in range(len(det_cls_list)):
		if det_cls_list[index] in toll_dataset_vehicle_classes:  # filter toll_vehicle class (class0 - class6)
			ctr_x = int((det_bbox_list[index][0] + det_bbox_list[index][2]) / 2)
			ctr_y = int((det_bbox_list[index][1] + det_bbox_list[index][3]) / 2)
			#print(ctr_x, ctr_y)
			if imgROI[ctr_y, ctr_x] > 127:  # in detection roi range
				# select front vehicle
				if not data_list:
					data_list.clear()
					data_list.append(det_bbox_list[index])
					data_list.append(det_conf_list[index])
					data_list.append(det_cls_list[index])
					ctr_y_data_list = ctr_y
				elif ctr_y > ctr_y_data_list:
					data_list.clear()
					data_list.append(det_bbox_list[index])
					data_list.append(det_conf_list[index])
					data_list.append(det_cls_list[index])
					ctr_y_data_list = ctr_y
	if data_list:
		# print(f"data_list:\n{data_list}")
		if draw_bbox_vehicle:
			# paintBoundingBox(vavc_frame, data_list[0], label=f'{data_list[2]} {data_list[1]:.2f}', color=colors[names.index(data_list[2])])
			paintBoundingBox(vavc_frame, data_list[0], label=f'{data_list[2]} {data_list[1]:.2f}', color=(255,255,0))

	#--------------------------------------------------
	# wheel filter
	single_wheel.clear()
	double_wheel.clear()
	license_plate_taxi.clear()
	predicted_vehicle_data = None
	if len(data_list) > 0:
		for index in range(len(det_cls_list)):
			if det_cls_list[index] not in toll_dataset_vehicle_classes:  # filter not toll_vehicle class
				ctr_x = int((det_bbox_list[index][0] + det_bbox_list[index][2]) / 2)
				ctr_y = int((det_bbox_list[index][1] + det_bbox_list[index][3]) / 2)
				#print(ctr_x, ctr_y)
				if(ctr_x > int(data_list[0][0]))and(ctr_y > int(data_list[0][1]))and(ctr_x < int(data_list[0][2]))and(ctr_y < int(data_list[0][3])):  # in vehicle roi range
					if det_cls_list[index] == 'singleWheel':
						# single_wheel.append(True)
						single_wheel.append(det_bbox_list[index])
					elif det_cls_list[index] == 'doubleWheel':
						# double_wheel.append(True)
						double_wheel.append(det_bbox_list[index])
					elif det_cls_list[index] == 'license_plate_taxi':
						# license_plate_taxi.append(True)
						license_plate_taxi.append(det_bbox_list[index])
					elif det_cls_list[index] == 'license_plate':
						# license_plate.append(True)
						# license_plate.append(det_bbox_list[index])
						crop_conf = det_conf_list[index]
						x, y, w, h = int(det_bbox_list[index][0]), int(det_bbox_list[index][1]), int(det_bbox_list[index][2] - det_bbox_list[index][0]), int(det_bbox_list[index][3] - det_bbox_list[index][1])
						crop_img = frame_raw[max(y-3,0):min(y+h+3,frame_raw.shape[0]), max(x-3,0):min(x+w+3,frame_raw.shape[1])]
						try:
							crop_img = correct_skew(crop_img)
						except:
							print("ERROR: correct_skew.")
					if draw_bbox_wheel and det_cls_list[index] != 'license_plate':
						# paintBoundingBox(vavc_frame, det_bbox_list[index], label=f'{det_cls_list[index]} {det_conf_list[index]:.2f}', color=colors[names.index(det_cls_list[index])])
						paintBoundingBox(vavc_frame, det_bbox_list[index], label=f'{det_cls_list[index]} {det_conf_list[index]:.2f}', color=(255,255,0))

					if draw_bbox_plate and det_cls_list[index] == 'license_plate':
						# paintBoundingBox(vavc_frame, det_bbox_list[index], label=f'{det_cls_list[index]} {det_conf_list[index]:.2f}', color=colors[names.index(det_cls_list[index])])
						paintBoundingBox(vavc_frame, det_bbox_list[index], label=f'{det_cls_list[index]} {det_conf_list[index]:.2f}', color=(255,255,0))

		predicted_vehicle_data = updateDetectionClass(data_list[0], single_wheel, double_wheel, license_plate_taxi)
		if put_class_at_bottom is True:
			cv2.putText(vavc_frame, "Class: " + str(data_list[2]), (70,672), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 0), 4, cv2.LINE_AA)

		# if draw_bbox_vehicle:
		# print("predicted_vehicle_data", predicted_vehicle_data)
		# print("crop_conf", crop_conf)
		# 	print("data_list[0]",data_list[0])
		# 	print("data_list[1]",data_list[1])
		# 	print("data_list[2]",data_list[2])
		# 	print("predicted_vehicle_data : ", predicted_vehicle_data)
		# 	print("single_wheel :", single_wheel)
		# 	print("double_wheel :", double_wheel)
		# 	print("license_plate_taxi : ", license_plate_taxi)
		# 	print("license_plate : ", license_plate)
			#cv2.putText(vavc_frame, "CLASS: " + str(data_list[2]), (128,672), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 255, 255), 2, cv2.LINE_AA)
		#for xml
		return vavc_frame, predicted_vehicle_data, crop_img, crop_conf, data_list, single_wheel, double_wheel, license_plate_taxi, license_plate
	return vavc_frame, predicted_vehicle_data, crop_img, crop_conf, data_list, single_wheel, double_wheel, license_plate_taxi, license_plate

		# return vavc_frame, predicted_vehicle_data, crop_img, crop_conf
	# return vavc_frame, 'no class detect', crop_img, crop_conf


import xlrd #Module used to read data from excel
import sys
from pylab import plot,show,scatter,legend,ylabel,xlabel,subplot,tight_layout # Part of Lib Matplotlib used to plot graph
from scipy.optimize import curve_fit # Module used for curve fitting
from scipy.interpolate import interp1d
import numpy as np #For Data Analysis
from math import log #Importing log function from Math
from tkinter import * #Tkinter, GUI Library for Python
from models import *
from tkinter import filedialog

# r = row
# c = column
# updatelistbx(fl): updates listbox with new genes from file fl or returns an error if invalid fl passed

#nextclicks = 0 #For Next Button in identwind function
datalist = []
# correspodence = {'xvals' : [[]....],
#                 'samples' : [[]...],
#                 'yvalues' : [[]....]
#                 'intrap'  : [[[sample1 for gene1]....]..[file 2]....]}
correspodence = {'xvals':[[0.00015241579, 0.00045724737, 0.00137174211, 0.00411522633, 0.01234567901, 0.03703703703, 0.11111111111, 0.333333333, 1.0]],
                 'samples': [],
                 'yvalues': []}
firstrow = 0
intens10_rn: int = 39 #Intensity reporter 10 row number in sheet
intens1_rn: int = 30 #Intensity reporter 1 row number in sheet
models = {'Quadratic':quad, 'Cubic':cubic,'5PL':fivepl} # Dictional to relate relevant strings to their corresponding
                                                        # functions

def selectfile():
    global nextclicks
    nextclicks = 0
    fl = filedialog.askopenfilename(initialdir = "C:/Users/parsh/Desktop/Protien Data", title = "Select a file",
                                    filetypes= (("Excel Files", "*.xlsx"),("CSV Files", "*.csv")))
    extractdata(fl)

def extractdata(fl):
    try:
        wb = xlrd.open_workbook(fl) ## Load the excel workbook
        global sheet
        sheet = wb.sheet_by_index(0) # Load the First sheet in the workbook -- probably want to be user defined
        data = [[sheet.cell_value(r,c) for c in range(sheet.ncols)] for r in range(sheet.nrows)] # List of lists --
        # with first list corresponding to first row
        datalist.append(data)
        identwind()
    except:
        messagebox.showerror(title="Error", message="Invalid or No File Selected!")

## ********************** Top Level Windows for Choosing Ident, X/Y Points *************
# identwind() creates a toplevel window to select an identifier
def identwind():
    top = Toplevel(root)
    top.title("Select an identifier")
    newframe = Frame(top)
    chooseidentlab = Label(newframe, text = "Choose Identifier: ")
    identlist = []
    for c in range(len(datalist[0][firstrow])):
        identlist.append(str(c) + ". " + datalist[0][firstrow][c])
    global chosen
    chosen = StringVar()
    chosen.set(identlist[0])
    identifiers = OptionMenu(newframe,chosen,*identlist)
    nextbt = Button(newframe,text = "Next",command = lambda: choosesamples(top))
    chooseidentlab.pack()
    identifiers.pack()
    nextbt.pack()
    newframe.pack()
    # global nextclicks
    # while(nextclicks == 0): #nextclicks traces the user's first Next Click
    #                         # As soon as the user makes the click, the top is destroyed
    #     top.update() # updates to keep waiting till the user makes the click event
    # nextclicks = 0

def addyvals(listbox,lst):
    selected = listbox.curselection()
    for item in selected:
        lst.append(int(listbox.get(item).split(".")[0]))
    for item in selected:
        listbox.delete(item)

def addsamples(listbox, lst):
    selected = listbox.curselection()
    for item in selected:
        lst.append(int(listbox.get(item).split(".")[0]))
    for item in selected:
        listbox.delete(item)

def choosesamples(oldtop):
    # global nextclicks
    # nextclicks += 1
    oldtop.destroy()
    top2 = Toplevel(root)
    top2.title("Select Samples")
    frame2 = Frame(top2)
    allyvalslab = Label(frame2, text = "All Values: ")
    allyvals = Listbox(frame2, selectmode=SINGLE)
    for col in range(intens1_rn,intens10_rn+1):
        allyvals.insert(END, str(col) + ". " + datalist[-1][firstrow][col])
    allyvals.pack(side=LEFT)
    yvalues = []
    samples = []
    addtoselectedbt = Button(frame2,text = "Add to Y Values", command = lambda: addyvals(allyvals,yvalues))
    addtosamplesbt = Button(frame2, text = "Add to Samples", command = lambda: addsamples(allyvals,samples))
    addtosamplesbt.pack()
    addtoselectedbt.pack()
    frame2.pack()
    while (len(allyvals.get(0,END)) != 0):
        top2.update()
    correspodence['yvalues'].append(yvalues)
    correspodence['samples'].append(samples)
    #choosexvals(top2)
    updatelistbx()
    print(correspodence)

def addx(lst, clicks, corx):
    lst.append(corx.get())
    clicks[0] += 1

def choosexvals(oldtop):
    oldtop.destroy
    xvals = []
    for item in correspodence['yvalues'][-1]:
        newtop = Toplevel(root)
        corx = DoubleVar()
        inputlbl = Label(newtop, text = "Enter corresponding X value for : " + str(item))
        inputbx = Entry(newtop,textvariable = corx)
        clicks = [0]
        nextbt = Button(newtop, text = "Next", command = lambda: addx(xvals, clicks, corx))
        inputlbl.pack()
        inputbx.pack()
        nextbt.pack()
        while(clicks[0] == 0):
            newtop.update()
        newtop.destroy()
    correspodence['xvals'].append(xvals)
    updatelistbx()

    # Choose range for columns for y values:
    # newframe2 = Frame(top2)
    # choosestartlab = Label(newframe2,text = "Enter Starting Column: ")
    # startcoln = IntVar()
    # endcoln = IntVar()
    # startent = Entry(newframe2, textvariable = startcoln)
    # startent.delete(0,END)
    # startent.insert(0,intens1_rn)
    # choosestartlab.pack()
    # startent.pack()
    # chooseendlab = Label(newframe2, text="Enter Last Column: ")
    # endent = Entry(newframe2, textvariable=endcoln)
    # endent.delete(0, END)
    # endent.insert(0, intens1_rn)
    # chooseendlab.pack()
    # endent.pack()
    # newframe2.pack()

# When resetting the program:
#       * Make nextclicks = 0
def updatelistbx():
    listbox.delete(0,END)
    global identifier_coln  # putting 0 because first letter is a number corresponding
    identifier_coln = int((chosen.get())[0])   # to chosen identifier's column
    global allgenes
    allgenes = genelistcreator(datalist)
    for row in range(1,len(allgenes)):
        listbox.insert(END, str(row) + ". " + str(allgenes[row]))

    # for row in range(1, sheet.nrows): #Ignoring zero because first row is just titles
    #     listbox.insert(END, str(row) + ". " + str(data[row][identifier_coln]))

def genelistcreator(datalist):
    allident = []
    for data in datalist:
        for row in data:
            allident.append(row[identifier_coln])

    allident = list(dict.fromkeys(allident)) #a way of removing duplicates from the list
    return allident

# selected_genes returns the identifier selected from the listbox
def select_genes():
    clicked_items = listbox.curselection()
    r = clicked_items[0] + 1 #adding 1 because the row seleced corresponds to row+1 in allgenes
    return allgenes[r]

# searchwhatrow(identifier) returns a list of row numbers corresponding to the identifier  in each
#                           data (if it exists) in datalist else if it doesn't exist, it says NA(means Not available)
def searchwhatrow(identifier):
    rows = []
    for datano in range(len(datalist)):
        rows.append('NA')
        for row in range(1,len(datalist[datano])):
            if (datalist[datano][row][identifier_coln] == identifier):
                rows[datano] = int(row)
                break

    return rows

def xycreator(improws,xlst,ylst):
    #correspodent['xvals'][datano] gives x vals defined by the user for that specific data
    for datano in range(len(improws)):
        x = [] #list of x vals for that certain gene in that certain file to be plotted
        y = [] #list of y vals for that certain gene in that certain file to be plotted
        #datalist[datano][improws[datano]] is actaully the data we are going to be playing with - this corresponds to the gene selected
        # by user to be graphed ---- only if improws[datano] is not 'NA'
        #datalist[datano] gives the whole data
        #correspodence['yvalues'][datano] contains all the colnos required for y vals
        if (improws[datano] != 'NA'):
            ycols = correspodence['yvalues'][datano]
            xvalues = correspodence['xvals'][datano]
            for index in range(len(ycols)):
                yvalue = datalist[datano][improws[datano]][ycols[index]]
                xvalue = xvalues[index]
                if (yvalue > 0):
                    y.append(yvalue)
                    x.append(xvalue)

        xlst.append(x)
        ylst.append(y)
    print(xlst)
    print(ylst)

xfit = np.arange(0.0,1.01,0.01)
def show_graph():

    improws = searchwhatrow(select_genes()) #important rows contains row numbers for the certain identifier selected in each data file
                                            # so 1st item in improws corresponds to row number in first file for that identifier
    listofxvals,listofyvals = [],[]
    #Adding all the intensities greater than zero to the y list - This ensures that zero intensities are not counted

    xycreator(improws,listofxvals,listofyvals)
    # listofxvals = [] # Contains xvalues for all files in the form of lists
    # for index in range(len(datalist)):
    #     listofxvals.append(correspodence['xvals'][index])

    # for index in range(len(datalist)):
    #     y = []
    #     for c in correspodence['yvalues'][index]: #Needs to be changed when intensities become user defined
    #         yvalue = datalist[index][improws[index]][c]
    #         if yvalue > 0:
    #             y.append(yvalue)
    #         else:
    #             listofxvals[index].pop(len(y))
    #     listofyvals.append(y)

    # if onoff.get() == 1 :
    #     for index in range(len(listofyvals)):
    #         listofyvals[index] = list(map(log, listofyvals[index]))

    #         ylabel('Log of Recorder Intensities')
    # else:
    #      ylabel('Recorder Intensities')

    # What to do when a gene doesn't have any name in the excel sheet
    for index in range(len(listofxvals)):
        if len(listofxvals[index]) == 0:
            messagebox.showerror(title = 'Warning', message = "The item selected doesn't exist in File no. {} or all of the Y values are 0".format(index + 1))
        else:
            try:
                popt,pcov = curve_fit(models[clicked.get()],listofxvals[index],listofyvals[index], maxfev = 10000)
                subplot(1, len(listofxvals), index+1)
                xlabel('Absolute Values')
                plot(xfit, models[clicked.get()](xfit, *popt))
                #label=datalist[index][improws[index]][identifier_coln])
                scatter(listofxvals[index], listofyvals[index])
                print(popt)

            except:
                messagebox.showerror(title = 'Error', message = "Insufficient non-zero Y points for the curvefitting! ")

    tight_layout()
    show()

            #messagebox.showerror(title = "Error", message = "Insufficient non-zero Y points for the curvefitting! ")
# intrap(ident) performs intrap on
def inter(ident,label):
    improws = searchwhatrow(ident)
    listofxvals,listofyvals = [],[]
    xycreator(improws,listofxvals,listofyvals)
    global finalresult
    finalresult = []
    for datano in range(len(datalist)):
        interlistforfile = []
        if len(listofyvals[datano]) != 0:
            try:
                popt,pcov = curve_fit(models[clicked.get()],listofxvals[datano],listofyvals[datano], maxfev = 10000)
                global xfit
                fittedyvals = models[clicked.get()](xfit, *popt)
                interfunc = interp1d(fittedyvals,xfit)
            except:
                for samplecol in correspodence['samples'][datano]:
                    interlistforfile.append('MNC') #Model not compatible
                continue
            impdata = datalist[datano][improws[datano]]
            for samplecol in correspodence['samples'][datano]:
                try:
                    interlistforfile.append(float(interfunc(impdata[samplecol])))
                except:
                    interlistforfile.append('NIR')
        else:
            for samplecol in correspodence['samples'][datano]:
                interlistforfile.append('NA')
        finalresult.append(interlistforfile)
    label['text'] = (str(finalresult))
    print(finalresult)

# show_graph first calls the function select_genes to see what genes have been selected by user
#            and then this function loops through all the corresponding data and produces a graph
# def show_graph():
#     for r in select_genes():
#        r += 1 #Increment r(row no) by 1 because the least r is 0 which would correspond to row 1 in spreadsheet
#     y = [] # Initiating y to be an empty list
#
#     #Adding all the intensities greater than zero to the y list - This ensures that zero intensities are not counted
#     for c in range(intens10_rn,intens1_rn,-1):
#          if data[r][c] > 0:
#              y.append(data[r][c])
#
#     if onoff.get() == 1 :
#          y = list(map(log,y))
#          ylabel('Log of Recorder Intensities')
#     else:
#          ylabel('Recorder Intensities')
#     x = []
#     count = 1
#     dilfact = 1/3  # Dilution Factor
#     for i in y:
#          x.append(count)
#          count *= dilfact
#     # What to do when a gene doesn't have any name in the excel sheet
#     if len(y) > 0:
#         try:
#             popt,pcov = curve_fit(models[clicked.get()],x,y, maxfev = 10000)
#             plot(xfit, models[clicked.get()](xfit, *popt), label=data[r][identifier_coln])
#             scatter(x, y)
#             xlabel('Absolute Values')
#             legend()
#             show()
#         except:
#             messagebox.showerror(title = "Error", message = "Insufficient non-zero Y points for the curvefitting! ")
#
#     else:
#      messagebox.showerror(title = "Error", message = "No Y Points!")
## GUI STUFF ##

root = Tk()
root.title('Protien Graphing Software')
root.geometry("500x500")
uploadfilebt = Button(root,text = "Load File", command = selectfile).pack()
frame = Frame(root, height=500, width=500)
#************DROPDOWN BOX FOR MODEL************
clicked = StringVar()
clicked.set("Cubic")
droplabel = Label(root, text="Choose Model for Curve-fitting: ").pack()
drop = OptionMenu(root, clicked, "Quadratic", "Cubic", "5PL")
drop.pack()
#***********************************************
#****************Scrollbar and Listbox**********
scroll = Scrollbar(frame)
scroll.pack(side=RIGHT, fill=Y)
listbox = Listbox(frame, yscrollcommand=scroll.set, selectmode=SINGLE)
listbox.pack(side=LEFT)
scroll.config(command=listbox.yview)
showgraphbt = Button(frame, text="Show Graph", command=show_graph)
showgraphbt.pack(side=BOTTOM)
interpolatebt = Button(frame, text = "Interpolate", command = lambda: inter(select_genes(),resultlabel))
interpolatebt.pack(side = BOTTOM)
t = StringVar()
resultlabel = Label(frame, text = t)
resultlabel.pack(side = BOTTOM)
#***********************************************
#***************Log Transform Checkbutton*******
onoff = IntVar()
logtrans = Checkbutton(frame,text = "Enable Log Transform", variable = onoff)
logtrans.deselect()
logtrans.pack()
frame.pack(side=LEFT)
root.mainloop()


import os
import time
import random
import threading
from queue import Queue

from bulb import Bulb

BULBS = 5
TECHNITIANS = 2

def bulb_functioning(bulb):
    while True:
        if random.randint(0, 100) < 40:
            bulb.break_bulb()
            manager_queue.put(bulb)
            manager_event.set()
            manager_event.clear()
            bulb.event.wait()
        else:
            brightness = random.randint(0, 100)
            color = random.choice([
                "white",
                "red",
                "green",
                "blue",
                "yellow",
                "purple",
                "orange",
                "pink",
                "cyan",
                "magenta",
                "lime",
                "brown",
                "olive",
                "grey",
                "silver",
                "black",
            ])
            bulb.update_indicators(brightness, color)

        time.sleep(random.randint(3, 5))

def technician_fix_bulb(technician_id, bulb):
    bulb.fix_bulb(technician_id)
    bulb.event.set()
    bulb.event.clear()
    available_technitians.put(technician_id)

def manager():
    while True:
        manager_event.wait()
        while not manager_queue.empty():
            if not available_technitians.empty():
                threading.Thread(
                    target=technician_fix_bulb,
                    args=(
                        available_technitians.get(),
                        manager_queue.get()
                    ),
                    daemon=True
                ).start()

if __name__ == "__main__":
    if not os.path.exists("./digital-twins"):
        os.makedirs("./digital-twins")

    manager_thread = threading.Thread(target=manager, daemon=True)
    manager_queue = Queue()
    manager_event = threading.Event()

    manager_thread.start()

    available_technitians = Queue()

    for i in range(TECHNITIANS):
        available_technitians.put(i + 1)

    bulbs_ids = ["bulb-" + str(i + 1) for i in range(BULBS)]
    bulbs = [Bulb(bulb) for bulb in bulbs_ids]

    bulb_threads = []

    for bulb in bulbs:
        bulb_thread = threading.Thread(
            target=bulb_functioning, args=(bulb,), daemon=True)
        bulb_threads.append(bulb_thread)

    for bulb_thread in bulb_threads:
        bulb_thread.start()
        time.sleep(.5)

    for bulb_thread in bulb_threads:
        bulb_thread.join()


import torch
import numpy as np
# import os
from tqdm import tqdm
# import pandas as pd

# from networks import load_net
# from datasets import load_ds_info
# from utils import get_features_logits_labels

# import torch
# import numpy as np
from sklearn.base import BaseEstimator, RegressorMixin
from sklearn.model_selection import StratifiedKFold, GridSearchCV
from sklearn.metrics import make_scorer

from utils.uncertainty_measures import get_ece

def CCAC(method):
    skf = StratifiedKFold(n_splits=3, shuffle=True, random_state=0)
    calibrator = CCACbase(method, device='cuda')
    lambdas = [5, 10, 15, 20, 25, 50]
    gscv = GridSearchCV(calibrator,
                        param_grid={'lambda_1':  lambdas,'lambda_2': lambdas},
                        cv=skf,
                        scoring=make_scorer(get_ece_cv, greater_is_better=False),
                        verbose=0, refit=True,
                        n_jobs=3,error_score='raise')
    return gscv

def get_ece_cv(labels_true, logits_pred):
    ground_truth = (logits_pred[:,:-1].argmax(dim=1) == labels_true).cpu().numpy()
    softmaxes = torch.nn.Softmax(dim=1)(logits_pred)
    prob_top1 = softmaxes[:,:-1].max(dim=1).values
    prob_misclassified = softmaxes[:,-1]
    confidence = 1-torch.sqrt((1-prob_top1)*prob_misclassified)
    return get_ece(confidence.numpy(), ground_truth, 15)*100

class LossCCAC(torch.nn.Module):
    def __init__(self, lambda_1=0, lambda_2=0, num_classes=10):
        super(LossCCAC, self).__init__()
        self.num_classes = num_classes
        self.lambda_1 = lambda_1
        self.lambda_2 = lambda_2

    def forward(self, logits, labels, ground_truth):
        labels_ccac = labels.clone()
        labels_ccac[torch.logical_not(ground_truth)] = self.num_classes
        softmaxes = torch.nn.Softmax(dim=1)(logits)
        loss_1 = -torch.log(softmaxes[np.arange(len(labels_ccac))[ground_truth],labels_ccac[ground_truth]]).sum()
        loss_2 = -self.lambda_1*torch.log(1-softmaxes[ground_truth,-1]).sum()
        loss_3 = -self.lambda_2*torch.log(softmaxes[torch.logical_not(ground_truth),-1]).sum()
        loss = (loss_1+loss_2+loss_3)/len(logits)
        return loss

class ModelCCAC(torch.nn.Module):
    def __init__(self, num_classes=10):
        super(ModelCCAC, self).__init__()
        if num_classes==10:
            self.model = torch.nn.Sequential(
                torch.nn.Linear(10, 50),
                torch.nn.ReLU(),
                torch.nn.Linear(50, 20),
                torch.nn.ReLU(),
                torch.nn.Linear(20, 11),
            )
        elif num_classes==100:
            self.model = torch.nn.Sequential(
                torch.nn.Linear(100, 100),
                torch.nn.ReLU(),
                torch.nn.Linear(100, 101),
            )

    def forward(self, x):
        x = self.model(x)
        return x

class ModelCCACS(torch.nn.Module):
    def __init__(self, num_classes=10):
        super(ModelCCACS, self).__init__()
        if num_classes==10:
            self.model = torch.nn.Sequential(
                torch.nn.Linear(10, 50),
                torch.nn.ReLU(),
                torch.nn.Linear(50, 20),
                torch.nn.ReLU(),
                torch.nn.Linear(20, 1),
            )
        elif num_classes==100:
            self.model = torch.nn.Sequential(
                torch.nn.Linear(100, 100),
                torch.nn.ReLU(),
                torch.nn.Linear(100, 1),
            )
        self.temperature = torch.nn.Parameter(torch.ones(1, requires_grad=True))

    def forward(self, x):
        return torch.hstack([x/self.temperature,self.model(x)])

class CCACbase(BaseEstimator, RegressorMixin):
    def __init__(self, method, lambda_1 = 0.0, lambda_2 = 0.0, epoch_check=5, max_epochs = 150, batch_size=128,
                 lr = 1e-2, random_state = 15, device='cpu'):

        self.method = method
        self.lambda_1 = lambda_1
        self.lambda_2 = lambda_2
        self.epoch_check = epoch_check
        self.max_epochs = max_epochs
        self.batch_size = batch_size
        self.lr = lr
        self.random_state = random_state
        self.device = device
        self.patience = 15
        self.min_delta = 0.0

        # Setting random seed
        torch.manual_seed(random_state)
        np.random.seed(random_state)

    def fit(self, logits, labels, verbose = False):
        if verbose:
            tqdm.write(f'Fitting lambda_1={self.lambda_1:1.2f}, lambda_2={self.lambda_2:1.2f}')

        train_ds = torch.utils.data.TensorDataset(logits, labels)
        train_set_size = int(len(train_ds) * 0.8)
        valid_set_size = len(train_ds) - train_set_size
        train_ds, val_ds = torch.utils.data.random_split(train_ds, [train_set_size, valid_set_size])

        train_dataloader = torch.utils.data.DataLoader(train_ds, batch_size=self.batch_size, shuffle=True, num_workers=4, multiprocessing_context='fork')
        logits_val = logits[np.array(val_ds.indices)]
        labels_val = labels[np.array(val_ds.indices)]
        # Initialize model
        if self.method == 'CCAC':
            self.model = ModelCCAC(num_classes=logits.shape[1])
            best_model = ModelCCAC(num_classes=logits.shape[1])
        elif self.method == 'CCAC-S':
            self.model = ModelCCACS(num_classes=logits.shape[1])
            best_model = ModelCCACS(num_classes=logits.shape[1])
        # Initialize the loss function
        loss_fn = LossCCAC(lambda_1=self.lambda_1, lambda_2=self.lambda_2)
        # Initialize optimizer
        optimizer = torch.optim.SGD(self.model.parameters(), lr=1e-2)
        lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[75, 125])

        self.model.to(self.device)
        self.model.train()
        best_loss = None
        best_ece = 100.0

        patience_counter = 0
        for epoch in range(self.max_epochs):
            if patience_counter == self.patience:
                break
            loss_epoch = []
            for batch, (logits, labels) in enumerate(train_dataloader):
                logits = logits.to(self.device)
                labels = labels.to(self.device)
                # Compute prediction and loss
                new_logits = self.model(logits)
                ground_truth = (logits.argmax(dim=1) == labels).cpu()
                loss = loss_fn(new_logits, labels, ground_truth)
                # Backpropagation
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                loss_epoch.append(loss.item())

            lr_scheduler.step()
            # Early Stopping if for the last patience epochs the loss has not decreased by at least min_delta
            loss = np.mean(loss_epoch)
            if best_loss is None:
                best_loss = loss
            else:
                if loss-best_loss  < -self.min_delta:
                    patience_counter = 0
                    best_loss = loss
                else:
                    patience_counter += 1

            ground_truth = (self.model(logits_val.to(self.device))[:,:-1].argmax(dim=1) == labels_val.to(self.device)).cpu().numpy()
            with torch.no_grad():
                confidences = self.predict_proba(logits_val.to(self.device)).cpu().max(dim=1).values.numpy()
                ece_val = get_ece(confidences, ground_truth, 15)*100

            if ece_val < best_ece:
                best_ece = ece_val
                best_model.load_state_dict(self.model.state_dict())
            if verbose:
                if epoch % 25 == 0:
                    print(f"Loss: {loss:>7f} ECE (val):{ece_val:1.2f} [{epoch:>3d}/{self.max_epochs:>3d}] [{patience_counter:>3d}/{self.patience:>3d}]")

        self.model.eval()
        self.model.cpu()
        self.model = best_model.eval().cpu()

        return self

    def predict_proba(self, inputs):
        with torch.no_grad():
            softmaxes = torch.nn.Softmax(dim=1)(self.model(inputs))
            prob_top1 = softmaxes[:,:-1].max(dim=1).values
            prob_misclassified = softmaxes[:,-1]
            temp = torch.sqrt(softmaxes[:,:-1]*(1-prob_misclassified.unsqueeze(1)))
            probs = temp*((1-temp.max(dim=1).values)/(temp.sum(dim=1)-temp.max(dim=1).values)).unsqueeze(1)
            probs[np.arange(len(temp)),temp.argmax(dim=1)] = temp.max(dim=1).values
            return probs

    def predict(self, inputs):
        with torch.no_grad():
            return self.model(inputs)


import numpy as np

from constants.namespace import *

SYSTEM = S_OPEN_2
PERCENTAGE_DEPLETION = 85

# First recovery point is 16 which is immediately after 15% depletion
# 76.5 is 90% of whatever is left after 15% depletion
RECOVERY_POINTS = [20, 30, 50, 76.5, 90, 100]  # Get data at these points

# Initialization and some base constants and their ranges
RANGE_HILL_COEFFICIENT = [0.5, 1, 2]

# We used log scale because for larger multiplication factor lipid levels
# will saturate in the MM type kinetics
RANGE_MULTIPLICATION_FACTOR = np.logspace(np.log10(1), np.log10(10), 15)
# We used log scale because concentrations are near 1
RANGE_CARRY = np.logspace(np.log10(0.1), np.log10(10), num=15)

RANGE_FEED_TYPE = [FEEDBACK_POSITIVE, FEEDBACK_NEGATIVE]
RANGE_SUBSTRATE = [I_PMPI, I_PI4P, I_PIP2, I_DAG, I_PMPA, I_ERPA, I_CDPDAG,
                   I_ERPI]
RANGE_ENZYMES = [E_PITP, E_PI4K, E_PIP5K, E_PLC, E_DAGK, E_LAZA, E_PATP, E_CDS,
                 E_PIS, E_SINK, E_SOURCE]

COLORS_PRIMARY = ["#CDDC39", "#E91E63", "#9C27B0", "#673AB7",
                  "#3F51B5", "#2196F3", "#03A9F4", "#00BCD4", "#009688",
                  "#4CAF50", "#F44336",
                  "#8BC34A"]

COLORS_SECONDARY = ["#F0F4C3", "#F8BBD0", "#E1BEE7", "#D1C4E9",
                    "#C5CAE9", "#BBDEFB", "#B3E5FC", "#B2EBF2", "#B2DFDB",
                    "#C8E6C9", "#FFCDD2",
                    "#DCEDC8"]


import socket, struct
from scapy.all import *
from netfilterqueue import NetfilterQueue

class PacketChk(object):
	__l2list = ('ether', '8021q', '8023sap', '8023snap')
	__l3list = ('arp', 'ip', 'ipv6')
	__l4list = ('tcp', 'udp', 'sctp', 'icmp', 'icmpv6', 'ah', 'ahv6', 'esp', 'espv6')
	__l2map = {	'ether':	('Ether',),
			'8021q':	('Ether',),
			'8023sap':	('Dot3', 'LLC'),
			'8023snap':	('Dot3', 'LLC', 'SNAP') }
	__l3map = {	'arp':	'ARP',
			'ip':	'IP',
			'ipv6':	'IPv6' }
	__l4map = {	'tcp':		'TCP',
			'udp':		'UDP',
			'sctp':		'SCTP',
			'icmp':		'ICMP',
			'icmpv6':	None }
	__skmap = {	'arp':	socket.AF_INET,
			'ip':	socket.AF_INET,
			'ipv6':	socket.AF_INET6 }

	def __init__(self,pkttype):
		l2proto = None; l3proto = None; l4proto = None
		for key in pkttype.split("-"):
			if key in PacketChk.__l2list: l2proto = key
			if key in PacketChk.__l3list: l3proto = key
			if key in PacketChk.__l4list: l4proto = key
		self.l2class = (); self.l3class = None; self.l4class = None
		if l2proto: self.l2class = PacketChk.__l2map.get(l2proto)
		if l3proto: self.l3class = PacketChk.__l3map.get(l3proto)
		if l4proto: self.l4class = PacketChk.__l4map.get(l4proto)
		self.sktype = socket.AF_INET
		if l3proto: self.sktype = PacketChk.__skmap.get(l3proto)

	##############################
	# get packet info by nfqueue
	##############################
	def get_nfqueues(self,nfq_num,pkt_num,accept):
		############################################################
		# https://bugzilla.redhat.com/show_bug.cgi?id=1304959
		############################################################
		self.nfqueue = NetfilterQueue(af=self.sktype)
		self.accept = accept
		self.nfqueue.bind(int(nfq_num), self.callback_nfqueue, int(pkt_num))

	def get_nfqueue(self):
		self.nfqueue.run()
		# unbind() will cause segment fault on rhel7, need debugging
		# self.nfqueue.unbind()

	def callback_nfqueue(self,pkt):
		self.packet = getattr(sys.modules[__name__], self.l3class)(_pkt=pkt.get_payload())
		# It seems that scapy can not parse IPv6-SCTP packet normally,
		# so specify SCTP class manually.
		if self.l3class == 'IPv6' and self.packet[IPv6].nh == 132:
			content = pkt.get_payload()
			ipv6_content = content[0:40]
			sctp_content = content[40:]
			self.packet = IPv6(_pkt=ipv6_content)/SCTP(_pkt=sctp_content)
		self.packet.show()
		if self.accept == True:
			pkt.accept()
		else:
			pkt.drop()

	##############################
	# match packet type
	##############################
	def match_pkttype(self):
		for l2class in self.l2class:
			if not getattr(sys.modules[__name__], l2class) in self.packet:
				raise Exception("l2proto is not matched!")
		if self.l3class:
			if not getattr(sys.modules[__name__], self.l3class) in self.packet:
				raise Exception("l3proto is not matched!")
		if self.l4class:
			if not getattr(sys.modules[__name__], self.l4class) in self.packet:
				raise Exception("l4proto is not matched!")

	##############################
	# match specified fielf for the packet
	##############################
	__pktmap = {	'--indev':		( None,		'indev' ),
			'--outdev':		( None,		'outdev' ),
			'--physindev':		( None,		'physindev' ),
			'--physoutdev':		( None,		'physoutdev' ),
			'--src-mac':		( 'Ether',	'src' ),
			'--dst-mac':		( 'Ether',	'dst' ),
			'--arp-op':		( 'ARP',	'op' ),
			'--arp-ptype':		( 'ARP',	'ptype' ),
			'--arp-hwtype':		( 'ARP',	'hwtype' ),
			'--arp-plen':		( 'ARP',	'plen' ),
			'--arp-hwlen':		( 'ARP',	'hwlen' ),
			'--arp-psrc':		( 'ARP',	'psrc' ),
			'--arp-pdst':		( 'ARP',	'pdst' ),
			'--arp-hwsrc':		( 'ARP',	'hwsrc' ),
			'--arp-hwdst':		( 'ARP',	'hwdst' ),
			'--src-ip':		( 'IP|IPv6',	'src' ),
			'--dst-ip':		( 'IP|IPv6',	'dst' ),
			'--tos':		( 'IP',		'tos' ),
			'--ttl':		( 'IP',		'ttl' ),
			'--proto':		( 'IP',		'proto' ),
			'--flag':		( 'IP',		'flags' ),
			'--flags':		( 'IP',		'flags' ),
			'--hl':			( 'IPv6',	'hlim' ),
			'--nh':			( 'IPv6',	'nh' ),
			'--src-port':		( 'TCP|UDP',	'sport' ),
			'--dst-port':		( 'TCP|UDP',	'dport' ),
			'--tcp-flag':		( 'TCP',	'flags' ),
			'--tcp-flags':		( 'TCP',	'flags' ),
			'--tcp-option':		( 'TCP',	'options' ),
			'--tcp-options':	( 'TCP',	'options' ),
			'--icmp-type':		( 'ICMP',	'type' ),
			'--icmp-code':		( 'ICMP',	'code' ),
			'--icmp6-type':		( 'IPv6',	'type' ),
			'--icmp6-code':		( 'IPv6',	'code' ),
			'--payload':		( 'Raw',	'load' ),
			'--payload-len':	( 'Raw',	'load' ) }

	__ipflags_bitmap = FlagsField("flags", 0x0, 3, ["MF","DF","evil"])
	__tcpflags_bitmap = FlagsField("flags",	0x2, 8, "FSRPAUEC")

	def match_packet(self,key,value):
		# get classname and fieldname
		classname = PacketChk.__pktmap[key][0]
		fieldname = PacketChk.__pktmap[key][1]
		if classname == 'IP|IPv6':
			classname = self.l3class
		if classname == 'TCP|UDP':
			classname = self.l4class
		# get title and pktvalue
		if classname == None:
			title = fieldname
			pktvalue = getattr(self.packet, fieldname)
		else:
			title = '[%s].%s' % (classname, fieldname)
			pktvalue = getattr(self.packet[getattr(sys.modules[__name__], classname)], fieldname)
		# do the value match
		if key == '--flag':
			expvalue = PacketChk.__ipflags_bitmap.any2i(None, value)
			if expvalue | pktvalue == pktvalue:
				return 0
			pktvalue = PacketChk.__ipflags_bitmap.i2repr(None, pktvalue)
		elif key == '--flags':
			expvalue = PacketChk.__ipflags_bitmap.any2i(None, value)
			if expvalue == pktvalue:
				return 0
			pktvalue = PacketChk.__ipflags_bitmap.i2repr(None, pktvalue)
		elif key == '--tcp-flag':
			expvalue = PacketChk.__tcpflags_bitmap.any2i(None, value)
			if expvalue | pktvalue == pktvalue:
				return 0
			pktvalue = PacketChk.__tcpflags_bitmap.i2repr(None, pktvalue)
		elif key == '--tcp-flags':
			expvalue = PacketChk.__tcpflags_bitmap.any2i(None, value)
			if expvalue == pktvalue:
				return 0
			pktvalue = PacketChk.__tcpflags_bitmap.i2repr(None, pktvalue)
		elif key == '--tcp-option':
			if type(value) == str:
				for option in pktvalue:
					if value == option[0]:
						return 0
			if type(value) == tuple:
				for option in pktvalue:
					if value[0] == option[0]:
						if type(option[1])(value[1]) == option[1]:
							return 0
		elif key == '--payload-len':
			if len(pktvalue) == int(value):
				return 0
			pktvalue = len(pktvalue)
		elif value.count("-") != 0:
			values = value.split("-")
			if type(pktvalue)(values[0]) <= pktvalue <= type(pktvalue)(values[1]):
				return 0
		else:
			if value.startswith('0x'):
				value = int(value, 16)
			if pktvalue == type(pktvalue)(value):
				return 0
		raise Exception("%s: expected_value=%s, actual_value=%s, not matched!" % (title, value, pktvalue))


# -*- coding: utf-8 -*-
"""
Created on Wed Mar 25 18:02:47 2020

@author: Nikitha
"""
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
style.use('ggplot')

fig = plt.figure()
axis1 = fig.add_subplot(1,1,1)

def anima(i):

    pull = open('twitter-out.txt', 'r').read()
    print(pull)
    lines = pull.split('\n')

    xlist = []
    ylist = []

    x = 0
    y = 0

    for line in lines:

        x += 1
        if line == 'positive':
            y += 1
        elif line =='negative':
            y += -1
        else:
            y += 0

            xlist.append(x)
            ylist.append(y)

    axis1.clear()
    axis1.plot(xlist,ylist)

animate = animation.FuncAnimation(fig, anima, interval = 1000)
plt.show()


import multiprocessing as mp
import numpy as np
import sys
import torch
import openai
import asyncio
import jsonlines
import random
import soundfile as sf
import os
from pathlib import Path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from pypinyin import lazy_pinyin, Style
from listen_chats import listen
from Wav2Lip.inference import sync_lip, load_model
from NeMo.nemo.collections.tts.models import HifiGanModel, FastPitchModel
from transformers import AutoTokenizer, AutoModelForMaskedLM

class AiStreamer:
    def __init__(self, api_key, args):
        self.api_key = api_key
        self.args = args
        self.wav2lip_model = None
        self.tokenizer = None
        self.bert_model = None
        self.vocoder_model_pt = None
        self.spec_gen_model = None
        self.frontend = None
        self.am_predictor = None
        self.voc_predictor = None
        self.content_id = 0  # 当前音视频文件生成后的名称（编号），根据编号进行播放

        # 初始化预设video路径
        self.not_talking_videos_source_path = os.path.join(os.path.join(self.args.video_source, self.args.streamer),
                                                           'not_talking_source')
        self.talking_videos_source_path = os.path.join(os.path.join(self.args.video_source, self.args.streamer),
                                                       'talking_source')
        self.sync_result_path = os.path.join(os.path.join(self.args.video_source, self.args.streamer),
                                             'sync_result')

        # 建立输入的通信队列
        self.normal_chats = mp.Queue()  # 普通弹幕队列
        self.sc_chats = mp.Queue()  # sc留言队列
        self.questions = mp.Queue()  # 结果队列,格式为(question: result_avi path})

    def listen_chats(self):
        """实时监听直播间的普通弹幕和付费SC留言"""
        asyncio.run(listen(self.args.room_id, self.normal_chats, self.sc_chats))

    def read_jsonl(self):
        """
        读取jsonl文件内容
        :returns
            contents: [list]
        """
        jsonl_name = self.args.streamer + '.jsonl'
        jsonl_path = os.path.join(self.args.text_input, jsonl_name)  # jsonl文件的相对路径
        with open(jsonl_path, 'r+', encoding='utf8') as f:
            contents = []
            for items in jsonlines.Reader(f):
                contents.append(items)
        return contents

    def write_jsonl(self, question):
        """写入jsonl文件"""
        contents = self.read_jsonl()
        contents[-1]["content"] = question  # 写入新问题
        jsonl_name = self.args.streamer + '.jsonl'
        jsonl_path = os.path.join(self.args.text_input, jsonl_name)  # jsonl文件的相对路径
        with open(jsonl_path, 'w+', encoding='utf8') as f:
            for content in contents:
                f.write(str(content).replace('\'', '\"') + '\n')

    def get_inputs_from_typing(self, question):
        """替代音频输入，将问题写入jsonl文件中"""
        self.write_jsonl(question)

    def transcribe_audio(self):
        """对指定音频进行语音识别"""
        audio_file_path = os.path.join(os.path.join(self.args.audio_input, self.args.streamer), 'audio_input.m4a')
        audio_file = open(audio_file_path, 'rb')
        transcript = openai.Audio.transcribe("whisper-1", audio_file)
        sentences = transcript['text'].split(',')
        sentences[0] = self.args.streamer  # 因为识别不准所以统一替换成指定的streamer名字
        transcript = ','.join(sentences)
        return transcript

    def generate_text(self):
        """从Inputs路径下的对应jsonl文件中读取用户的提问, 根据不同模型调用不同API并返回答案"""
        # openai.api_key = self.api_key
        # # 区分3.5模型和其他老模型，因为接口调用方式不一样
        # if self.args.model_name == 'gpt-3.5-turbo':
        #     massages = self.read_jsonl()
        #     result = openai.ChatCompletion.create(
        #         model=self.args.model_name,
        #         messages=massages,
        #         temperature=0.4,
        #         top_p=1,
        #         n=1,
        #         presence_penalty=0.2,
        #         frequency_penalty=0.2
        #     )
        #     answer = result['choices'][0]['message']['content']
        #     print(f'回答：{answer}')
        answer = "对啊，好像我还能没看到上面中国主播啊或者网红啊去过对马岛，我可能也算是第一个去对马岛的了吧，到时候深度解密一下对马岛到底什么样儿啊，也没去过对马岛第一次去挺紧张的"
        return answer

    @ staticmethod
    def find_model(model_dir):
        """寻找model_dir下后缀在check_list的文件的文件"""
        path = ""
        check_list = ['pt', 'pth']
        for file in os.listdir(model_dir):
            suffix = file.split('.')[-1]
            if suffix in check_list:
                path = model_dir / file
                print(path)
        return path

    def text_preprocess(self, raw_text):
        punc_e2c = {',': '，',
                    '.': '。',
                    '?': '？',
                    '、': '，'}
        raw_text="".join([punc_e2c[i] if i in punc_e2c else i for i in raw_text])  # 按照punc_e2c对应关系转换中英文标点符号
        text = raw_text.replace(" ","").lower()

        with torch.no_grad():
            inputs = self.tokenizer("".join(text), return_tensors='pt')
            for i in inputs:
                inputs[i] = inputs[i].to(self.args.device)
            res = self.bert_model(**inputs, output_hidden_states=True)
            res = torch.cat(res['hidden_states'][-3:-2], -1)[0].cpu().numpy()

        initials = lazy_pinyin(raw_text, neutral_tone_with_five=False, style=Style.INITIALS, strict=False)
        finals = lazy_pinyin(raw_text, neutral_tone_with_five=False, style=Style.FINALS_TONE3)

        _vecs = []
        _text = []
        for _o in zip(zip(initials, finals), list(raw_text), res[1:-1]):
            _o, _c, _vec = _o
            if _o[0] != _o[1] and _o[0] != '':
                _text.extend(['@'+i for i in _o])
                _vecs.extend([_vec]*2)
            elif _o[0] != _o[1] and _o[0] == '':
                _text.append('@'+_o[1])
                _vecs.append(_vec)
            else:
                _text.extend(list(_o[0]))
                _vecs.extend([_vec]*len(_o[0]))

        _vecs = np.stack([res[0]] + _vecs + [res[-1]])
        bert = torch.tensor(_vecs.T[None]).to(self.args.device)
        phoneme = " ".join(_text)

        return bert, phoneme

    def generate_audio(self, input_text):
        """paddlespeech本地推理"""
        output_dir = Path(self.args.audio_output)
        output_dir.mkdir(parents=True, exist_ok=True)

        # 开始推理
        bert_feats, phoneme = self.text_preprocess(raw_text=input_text)
        with torch.no_grad():
            parsed = self.spec_gen_model.parse(str_input=phoneme, normalize=False)
            res = self.spec_gen_model.generate_spectrogram(
                tokens=parsed, pace=1,
                bert_feats=bert_feats,
            )
            spectrogram = res
            audio = self.vocoder_model_pt.convert_spectrogram_to_audio(spec=spectrogram)

        spectrogram = spectrogram.to('cpu').numpy()[0]
        audio = audio.to('cpu').numpy()

        # 保存文件
        wav_id = str(self.content_id) + ".wav"
        sf.write(output_dir / wav_id, audio, samplerate=22050)
        print(f'TTS数据生成完毕...')

    def generate_video(self):
        # 随机挑选一个视频进行合成
        talking_videos_source_list = self.load_video(self.talking_videos_source_path)
        face_index = random.randrange(len(talking_videos_source_list))
        face = talking_videos_source_list[face_index]
        sync_lip(ckpt=self.wav2lip_model,
                 face=face,
                 audio_input=os.path.join(self.args.audio_output, str(self.content_id) + '.wav'),
                 outfile=os.path.join(self.sync_result_path, str(self.content_id) + '.avi'))
        print(f'video数据生成完毕...')

    @staticmethod
    def load_video(video_path):
        """创建进程队列保存VideoCapture对象"""
        video_list = []
        for file_name in os.listdir(video_path):
            video = os.path.join(video_path, file_name)
            video_list.append(video)  # 先把所有文件的cap对象都读取到列表内
        return video_list

    def load_next_video(self):
        """给出主窗口下一个待播放的视频地址"""
        not_talking_video_list = self.load_video(self.not_talking_videos_source_path)
        next_video_index = random.randrange(len(not_talking_video_list))
        next_video = not_talking_video_list[next_video_index]
        return next_video

    def generate_answer(self, question):
        """从Inputs路径下的对应jsonl文件中读取用户的提问, 根据不同模型调用不同API并返回答案"""
        # 修改问题格式
        uname, msg = question[0], question[1]
        processed_question = uname + '问：' + msg

        # 生成回答
        self.content_id += 1  # 确认编号
        self.get_inputs_from_typing(question=processed_question)  # 将问题写入jsonl中的message模板
        text_answer = self.generate_text()
        self.generate_audio(input_text=text_answer)
        self.generate_video()
        self.questions.put((processed_question, os.path.join(self.sync_result_path, str(self.content_id) + '.avi')))  # 队列存入回答

    def processing_chats(self):
        """预加载模型并监听输入队列"""
        # 预加载lip_sync model
        if self.wav2lip_model is None:
            self.wav2lip_model = load_model(self.args.wav2lip_model)

        # 预加载TTS模型
        if self.tokenizer is None:
            self.tokenizer = AutoTokenizer.from_pretrained("hfl/chinese-roberta-wwm-ext-large")
        if self.bert_model is None:
            self.bert_model = AutoModelForMaskedLM.from_pretrained("hfl/chinese-roberta-wwm-ext-large").to(self.args.device).eval()
        if self.vocoder_model_pt is None:
            self.vocoder_model_pt = HifiGanModel.load_from_checkpoint(checkpoint_path=self.args.hifigan).to(self.args.device).eval()
        if self.spec_gen_model is None:
            self.spec_gen_model = FastPitchModel.load_from_checkpoint(checkpoint_path=self.args.fs2).to(self.args.device).eval()

        # 主循环
        while True:
            if not self.sc_chats.empty():  # 优先读取sc弹幕的留言
                sc_question = self.sc_chats.get()
                print(f'SC留言:{sc_question}')
                self.generate_answer(question=sc_question)
            if not self.normal_chats.empty() and self.sc_chats.empty():  # 如果sc留言队列为空则开始处理普通弹幕
                normal_question = self.normal_chats.get()
                print(f'普通留言:{normal_question}')
                self.generate_answer(question=normal_question)

    def start_stream(self):
        """使用进程启动各个模块对消息列表进行监听"""
        # 启动 listen_chats 进程
        listen_chats_process = mp.Process(target=self.listen_chats)
        listen_chats_process.start()

        # 启动 processing_chats 进程
        processing_chats_process = mp.Process(target=self.processing_chats)
        processing_chats_process.start()


def on_bluetooth_connected():
    basic.show_string("C")
bluetooth.on_bluetooth_connected(on_bluetooth_connected)

def on_bluetooth_disconnected():
    basic.show_string("D")
bluetooth.on_bluetooth_disconnected(on_bluetooth_disconnected)

def on_button_pressed_a():
    basic.show_string("A")
input.on_button_pressed(Button.A, on_button_pressed_a)

def on_logo_pressed():
    basic.show_number(5)
    basic.pause(1000)
    basic.show_number(4)
    basic.pause(1000)
    basic.show_number(3)
    basic.pause(1000)
    basic.show_number(2)
    basic.pause(1000)
    basic.show_number(1)
    music.set_built_in_speaker_enabled(True)
    basic.show_icon(IconNames.YES)
    for index in range(4):
        music.play_melody("C C G G A A G - ", 120)
        music.play_melody("F F E E D D C - ", 120)
input.on_logo_event(TouchButtonEvent.PRESSED, on_logo_pressed)

def on_button_pressed_b():
    music.set_built_in_speaker_enabled(True)
    basic.show_icon(IconNames.YES)
input.on_button_pressed(Button.B, on_button_pressed_b)

basic.show_icon(IconNames.HEART)
music.set_built_in_speaker_enabled(False)
basic.show_icon(IconNames.NO)
bluetooth.start_accelerometer_service()
bluetooth.start_button_service()
bluetooth.start_io_pin_service()
bluetooth.start_led_service()
bluetooth.start_magnetometer_service()
bluetooth.start_temperature_service()

def on_forever():
    basic.show_leds("""
        . . # . .
        # . . . #
        . . # . .
        # . # . #
        # # # # #
        """)
    basic.show_leds("""
        # . . . #
        . . # . .
        . . # . .
        # . # . #
        # # # # #
        """)
basic.forever(on_forever)


#https://algospot.com/judge/problem/read/ASYMTILING

"""def solve(pos):
    if pos == n: return 1
    elif pos > n : return 0
    if cache[pos] != -1: return cache[pos]
    cache[pos] = (solve(pos+1) + solve(pos+2)) % 1000000007
    return cache[pos]

for _ in range(int(input())):
    n = int(input())
    cache = [-1] * (n+1)
    cache[n] = 1
    total = solve(0)
    if n %2:
        m = n//2+1
        res = (total + 1000000007 - cache[m]) % 1000000007
        print(total - cache[m])
    else:
        m = cache[n//2]
        m2 = cache[n//2+1]
        res = (total + 1000000007 - m - m2) % 1000000007
        print(res)"""

def solve(remain):
    if remain == 0: return 1
    elif remain < 0: return 0
    if cache[remain] != -1: return cache[remain]
    cache[remain] = (solve(remain-1) + solve(remain-2)) % 1000000007
    return cache[remain]
for _ in range(int(input())):
    n = int(input())
    cache = [-1] * (n+1)
    total = solve(n)
    if n % 2 :
        res = (total + 1000000007 - solve(n//2)) % 1000000007
    else:
        t1= solve(n//2)
        t2 = solve(n//2-1)
        res = (total + 1000000007 - solve(n//2) - solve(n//2-1)) % 1000000007
    print(res)


import wikipediaapi
import tkinter as tk
import webbrowser

def search_wikipedia():
    #If the language is not selected, we display an error message
    if lang.get() == '':
        output_text.delete('1.0', tk.END)
        output_text.insert('1.0', 'It could be easier if you choose a language.')
        return

    # Get the input text from the entry field
    input_text = entry.get()

    # Use the Wikipedia API to search for the input text
    wiki = wikipediaapi.Wikipedia(lang.get())
    page = wiki.page(input_text)

    # If the page exists, display its content in the text box
    if page.exists():
        output_text.delete('1.0', tk.END)
        output_text.insert('1.0', page.text)
    # If the page does not exist, display an error message
    else:
        output_text.delete('1.0', tk.END)
        output_text.insert('1.0', 'Page not found.')

def copy_text():
    text = output_text.get('1.0', tk.END)
    truncated_text = text[:3999] # We truncate the text to 3999 characters, because discord has a limit of 4000 characters
    window.clipboard_clear()
    window.clipboard_append(text)
    # We change the text of the copy button to indicate that the text has been copied
    copy_button.config(text='Copied!')

def github():
    webbrowser.open('https://github.com/Claquettes/Claquipedia')

# Create the GUI window
window = tk.Tk()
window.title('Claquipedia - Wikipedia search bot')

# We change the color of the window,and of the text
window.configure(bg='#24273a')
window.option_add('*Font', 'Helvetica 12')
# we change the color of the buttons and input
window.option_add('*Button.Background', '#24273a')
window.option_add('*Button.Foreground', 'white')
window.option_add('*Entry.Background', '#24273a')
window.option_add('*Entry.Foreground', 'white')
window.option_add('*Label.Background', '#24273a')
window.option_add('*Label.Foreground', 'white')
window.option_add('*Text.Background', '#24273a')
window.option_add('*Text.Foreground', 'white')
# We change the color of the output text
window.option_add('*Text.Background', '#24273a')
window.option_add('*Text.Foreground', 'white')
# We change the color of the scrollbar, and of the options menu
window.option_add('*Scrollbar.Background', '#24273a')
window.option_add('*Scrollbar.Foreground', 'white')
window.option_add('*TCombobox.Background', '#24273a')
window.option_add('*TCombobox.Foreground', 'white')
# We change the color of the menu
window.option_add('*Menu.Background', '#24273a')
window.option_add('*Menu.Foreground', 'white')

lang = tk.StringVar()
# We add a label to indicate the purpose of the menu
lang_label = tk.Label(window, text='Choose a language:')
lang_label.grid(row=0, column=0, columnspan=2)
# We create an option menu to choose the language
lang_menu = tk.OptionMenu(window, lang, 'en', 'fr', 'es', 'de', 'it', 'pt', 'ru', 'ja', 'zh', 'ar', 'hi', 'ko', 'tr', 'pl', 'zh-tw')
lang_menu.grid(row=0, column=2, columnspan=2)

# Create the input label and entry field
input_label = tk.Label(window, text='Input a word:')
input_label.grid(row=3, column=0)
entry = tk.Entry(window)
entry.grid(row=3, column=1)

# Create the search button
search_button = tk.Button(window, text='Search', command=search_wikipedia)
search_button.grid(row=4, column=0, columnspan=2)

# Create the copy button
copy_button = tk.Button(window, text='Copy', command=copy_text)
copy_button.grid(row=4, column=2)

# Create the github button, that opens the github page of the project
github_button = tk.Button(window, text='Github', command=github)
github_button.grid(row=5, column=2)

# Create the output label and text box
output_label = tk.Label(window, text='Wikipedia page content:')
output_label.grid(row=5, column=0)
#we delete every character that is after the 3999th character
output_text = tk.Text(window, height=10, width=50)

output_text.grid(row=5, column=0, columnspan=2)
# we add padding to the text box
output_text.config(padx=10, pady=10)

# Start the GUI loop
window.mainloop()


from webbrowser import Chrome
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.yatra.com/flights")
depart_from = driver.find_element_by_id("BE_flight_origin_city")
depart_from.click()
time.sleep(3)

depart_from.send_keys("New Delhi")
depart_from.send_keys(Keys.ENTER)
time.sleep(3)

going_to = driver.find_element_by_xpath("//input[@id='BE_flight_arrival_city']")
going_to.send_keys("New")
time.sleep(3)
search_results = driver.find_elements(By.XPATH, "//div[@class='viewport']//div[1]/li")
print(len(search_results))

for results in search_results:
    print(results.text)
    if "New York (jfk)" in results.text:
        results.click()
        time.sleep(3)
        break

driver.quit()


import argparse
import datetime
import os
import re
import sys
from typing import Sequence

EXTENSIONS = [".py"]
COPYRIGHT_REGEX_STR = r"Copyright.*(?P<till>\d{4}) (?P<holder>.*)"
COPYRIGHT_REGEX = re.compile(COPYRIGHT_REGEX_STR)
THIS_YEAR = datetime.datetime.now().year

EXCLUDED_FILES = ["docs/conf.py"]

def check_file(file: str) -> int:
    """Run the check on single file."""
    ret_val = 0
    if not os.path.isfile(file):
        print(f"'{file}' doesn't exist anymore")
        return 0
    with open(file) as f:
        content = f.read()
    copyrights = COPYRIGHT_REGEX.findall(content)
    for cp_instance in copyrights:
        cp_year = int(cp_instance[0])
        if cp_year == THIS_YEAR:
            break
    else:
        print(f"File: '{file}' doesn't have {THIS_YEAR} Copyright")
        ret_val = 1
    return ret_val

def check_files(files: Sequence[str]) -> int:
    """Run the check on a list of files."""
    ret_val = 0
    for file in files:
        if file in EXCLUDED_FILES:
            continue
        _, extension = os.path.splitext(file)
        if extension in EXTENSIONS:
            ret_val += check_file(file)
    return ret_val

def main(argv: Sequence[str] = None) -> int:
    """Main function."""
    parser = argparse.ArgumentParser(
        description="""Check whether "files" have the current year in Copyright."""
    )
    parser.add_argument("files", nargs="*", help="Files to analyze")
    args = parser.parse_args(argv)

    return check_files(args.files)

if __name__ == "__main__":
    sys.exit(main())


from setuptools import find_packages, setup

exec(open("getml/version.py").read())

long_description = """
    getML (https://getml.com) is a software for automated machine learning
    (AutoML) with a special focus on feature learning for relational data
    and time series.

    This is the official python client for the getML engine.

    Documentation and more details at https://docs.getml.com/{}
""".format(
    __version__
)

setup(
    name="getml",
    version=__version__,
    author="getML",
    author_email="support@getml.com",
    license_files=("LICENSE",),
    license="Elastic License 2.0 (ELv2)",
    url="https://docs.getml.com/" + __version__,
    download_url="https://github.com/getml/getml-python-api",
    description="Python API for getML",
    long_description=long_description,
    packages=find_packages(),
    package_data={
        "getml": [
            "py.typed",
            "utilities/templates/*.jinja2",
            ".getML/**/*",
            ".getML/**/**/*",
            ".getML/**/**/**/*",
            ".getML/**/**/**/**/*",
        ]
    },
    python_requires=">=3.8.0",
    install_requires=[
        "pandas",
        "pyarrow>=7.0,<7.1",
        "numpy>=1.22",
        "jinja2",
        "scipy",
    ],
    setup_requires=["pytest-runner"],
    tests_require=["pytest"],
    keywords=["AutoML", "feature learning"],
    classifiers=[
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.8",
        "Topic :: Scientific/Engineering :: Artificial Intelligence",
        "Intended Audience :: Education",
        "Intended Audience :: Science/Research",
        "Intended Audience :: Financial and Insurance Industry",
        "Intended Audience :: Telecommunications Industry",
        "Intended Audience :: Information Technology",
        "Intended Audience :: Manufacturing",
        "Intended Audience :: Healthcare Industry",
        "Operating System :: OS Independent",
    ],
)


from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

from googlecloudsdk.command_lib.compute.routers.nats.rules import flags
from googlecloudsdk.core import exceptions as core_exceptions
import six

class ActiveIpsRequiredError(core_exceptions.Error):
  """Raised when active ranges are not specified for Public NAT."""

  def __init__(self):
    msg = '--source-nat-active-ips is required for Public NAT.'
    super(ActiveIpsRequiredError, self).__init__(msg)

class ActiveIpsNotSupportedError(core_exceptions.Error):
  """Raised when active IPs are specified for Private NAT."""

  def __init__(self):
    msg = '--source-nat-active-ips is not supported for Private NAT.'
    super(ActiveIpsNotSupportedError, self).__init__(msg)

class ActiveRangesRequiredError(core_exceptions.Error):
  """Raised when active ranges are not specified for Private NAT."""

  def __init__(self):
    msg = '--source-nat-active-ranges is required for Private NAT.'
    super(ActiveRangesRequiredError, self).__init__(msg)

class ActiveRangesNotSupportedError(core_exceptions.Error):
  """Raised when active ranges are specified for Public NAT."""

  def __init__(self):
    msg = '--source-nat-active-ranges is not supported for Public NAT.'
    super(ActiveRangesNotSupportedError, self).__init__(msg)

class DrainIpsNotSupportedError(core_exceptions.Error):
  """Raised when drain IPs are specified for Private NAT."""

  def __init__(self):
    msg = '--source-nat-drain-ips is not supported for Private NAT.'
    super(DrainIpsNotSupportedError, self).__init__(msg)

class DrainRangesNotSupportedError(core_exceptions.Error):
  """Raised when drain ranges are specified for Public NAT."""

  def __init__(self):
    msg = '--source-nat-drain-ranges is not supported for Public NAT.'
    super(DrainRangesNotSupportedError, self).__init__(msg)

def _IsPrivateNat(nat, compute_holder):
  return nat.type == (
      compute_holder.client.messages.RouterNat.TypeValueValuesEnum.PRIVATE)

def CreateRuleMessage(args, compute_holder, nat, with_private_nat=False):
  """Creates a Rule message from the specified arguments."""
  if with_private_nat:
    rule = compute_holder.client.messages.RouterNatRule(
        ruleNumber=args.rule_number,
        match=args.match,
        action=compute_holder.client.messages.RouterNatRuleAction())
    is_private_nat = _IsPrivateNat(nat, compute_holder)
    if args.source_nat_active_ips:
      if is_private_nat:
        raise ActiveIpsNotSupportedError()
      rule.action.sourceNatActiveIps = [
          six.text_type(ip)
          for ip in flags.ACTIVE_IPS_ARG_REQUIRED.ResolveAsResource(
              args, compute_holder.resources)
      ]
    elif not is_private_nat:
      raise ActiveIpsRequiredError()

    if args.source_nat_active_ranges:
      if not is_private_nat:
        raise ActiveRangesNotSupportedError()
      rule.action.sourceNatActiveRanges = [
          six.text_type(subnet)
          for subnet in flags.ACTIVE_RANGES_ARG.ResolveAsResource(
              args, compute_holder.resources)
      ]
    elif is_private_nat:
      raise ActiveRangesRequiredError()

    return rule

  active_ips = [
      six.text_type(ip)
      for ip in flags.ACTIVE_IPS_ARG_REQUIRED.ResolveAsResource(
          args, compute_holder.resources)
  ]
  return compute_holder.client.messages.RouterNatRule(
      ruleNumber=args.rule_number,
      match=args.match,
      action=compute_holder.client.messages.RouterNatRuleAction(
          sourceNatActiveIps=active_ips))

class RuleNotFoundError(core_exceptions.Error):
  """Raised when a Rule is not found."""

  def __init__(self, rule_number):
    msg = 'Rule `{0}` not found'.format(rule_number)
    super(RuleNotFoundError, self).__init__(msg)

def FindRuleOrRaise(nat, rule_number):
  """Returns the Rule with the given rule_number in the given NAT."""
  for rule in nat.rules:
    if rule.ruleNumber == rule_number:
      return rule
  raise RuleNotFoundError(rule_number)

def UpdateRuleMessage(rule, args, compute_holder, nat, with_private_nat=False):
  """Updates a Rule message from the specified arguments."""
  if args.match:
    rule.match = args.match
  is_private_nat = with_private_nat and _IsPrivateNat(nat, compute_holder)
  if args.source_nat_active_ips:
    if is_private_nat:
      raise ActiveIpsNotSupportedError()
    rule.action.sourceNatActiveIps = [
        six.text_type(ip)
        for ip in flags.ACTIVE_IPS_ARG_OPTIONAL.ResolveAsResource(
            args, compute_holder.resources)
    ]
  if args.source_nat_drain_ips:
    if is_private_nat:
      raise DrainIpsNotSupportedError()
    rule.action.sourceNatDrainIps = [
        six.text_type(ip) for ip in flags.DRAIN_IPS_ARG.ResolveAsResource(
            args, compute_holder.resources)
    ]
  elif args.clear_source_nat_drain_ips:
    rule.action.sourceNatDrainIps = []

  if not with_private_nat:
    return

  if args.source_nat_active_ranges:
    if not is_private_nat:
      raise ActiveRangesNotSupportedError()
    rule.action.sourceNatActiveRanges = [
        six.text_type(subnet)
        for subnet in flags.ACTIVE_RANGES_ARG.ResolveAsResource(
            args, compute_holder.resources)
    ]
  if args.source_nat_drain_ranges:
    if not is_private_nat:
      raise DrainRangesNotSupportedError()
    rule.action.sourceNatDrainRanges = [
        six.text_type(subnet)
        for subnet in flags.DRAIN_RANGES_ARG.ResolveAsResource(
            args, compute_holder.resources)
    ]
  elif args.clear_source_nat_drain_ranges:
    rule.action.sourceNatDrainRanges = []


#identifiers comment

#a name is called an identifier

name="Luminar Technolab" #name is a variable

#def add(num1,num2):
 #   return num1+num2
#add function name
#num1,num2 variable name

#class person:
  #  pass

#person class name

#identifiers variable,function,class

#input and output function

#to print messahe in console we can use print()
#for reading valus through console we can use input()

print("hello world")

#variables?

num=2
#variables are used to represent a memorylocation            10245|0010|

#name1="luminar technolab"  #variable name should not start with numerical value

name="luminar technolab"  #there is no length lmit
print("Company name =",name)

location="Kakkanad"

#Our company Luminar Technolab located in Kakkanad

print("Our company",name,"located in",location)

#myself yourname is years old

myname="Vishal Raju"  #Stringvale
age=23

print("Myself",myname,"is",age,"years old")


import sys

class MinHeap:
    ''' MinHeap implementation that uses graph edges.
    Each graph edge is in the format [current vertex, destination vertex, weight]
    e.g. [1,2,3] is the edge from vertex 1 to vertex 2 with weight 3'''
    def __init__(self, heap=[]):
        if heap == []:
            # storing the heap size here
            self.heap = [0]
        else:
            self.heap = [0] + heap
            i = len(self.heap) - 1
            self.heap[0] = i # get the size of the heap
            i = i // 2 # start of parent of last node
            while i > 0:
                self.heapify(i)
                i -= 1

    def is_empty(self):
        '''returns a boolean that indicates if the heap is empty
        >>>heap = MinHeap([1,2,3])
        >>>heap.is_empty
        False
        >>>heap = MinHeap()
        >>>heap.is_empty
        True
        '''
        return self.heap[0] == 0

    def __str__(self):
        return str(self.heap[1:]) + " Heap size: {}".format(self.heap[0])

    def insert(self, val):
        ''' inserts a value into the heap at the correct position
        @param val -> the value that is to be inserted
        In this implementation the value must be a graph edge'''
        self.heap.append(val)
        self.heap[0] += 1
        index = self.heap[0] # since we are storing the length
        while (index != 1 and val[2] < self.heap[index//2][2] ):
            # store as a temp value
            temp = self.heap[index//2]
            self.heap[index//2] = val
            self.heap[index] = temp
            index = index // 2

    def extract_min(self):
        '''Extracts and returns the minimum element in the heap and then
        heapifies starting at index 1. In this implementation the minimum
        element is the element with the least edge weight stored in index 2
        >>>heap = MinHeap([[1,2,3], [3,2,1]])
        [3,2,1]
        '''
        min_value = self.heap[1]
        if self.heap[0] > 1:
            self.heap[1] = self.heap.pop() # get the value from the right-most leaf
            self.heap[0] -= 1
            self.heapify(1) # heapify at index 1
        else:
            min_value = self.heap.pop()
            # decrease heap size
            self.heap[0] -= 1
        return min_value

    def heapify(self, i):
        '''Calls heapifies the heap starting at index i
        @param i -> the index at which to heapify'''
        heap_size = self.heap[0]
        left = 2*i
        right = 2*i + 1
        min_value = i
        # make sure that left and right are in the limits of the heap array
        if (left <= heap_size and self.heap[left][2] < self.heap[i][2]):
            min_value = left
        if (right <= heap_size and
            self.heap[right][2] < self.heap[min_value][2]):
            min_value = right
        if (min_value != i):
            temp = self.heap[min_value]
            self.heap[min_value] = self.heap[i]
            self.heap[i] = temp
            self.heapify(min_value)

def mst(n, edges):
    '''function that returns sum of MST edges using kruskals algorithm
    @param n -> number of vertices in the graph
    @param edges -> array with edges in the graph
    @returns -> the sum of the edges in the MST'''
    # create a visited array
    rsum = 0
    clusters = [0] * (n + 1)
    heap = MinHeap()
    # insert into heap
    for edge in edges:
        heap.insert(edge)
    for i in range(1, n+1):
        clusters[i] = [i]
    while not heap.is_empty():
        edge = heap.extract_min()
        vert_1, vert_2 = edge[0], edge[1]
        if clusters[vert_1] != clusters[vert_2]:
            rsum += edge[2]
            new_clust = clusters[vert_1] + clusters[vert_2]
            for vertex in new_clust:
                clusters[vertex] = new_clust
    return rsum

if __name__ == "__main__":
    n = 4
    edges = [[1, 2, 1], [3, 2, 150], [4, 3, 99], [1, 4, 100], [3, 1, 200]]
    result = mst(n, edges)
    print(edges)
    print(result)


# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import numpy as np
import torch
import sys

total = 5000
exemplar_size = (1, 3, 127, 127)
search_size = (1, 9, 255, 255)

class ExperimentPerformance(object):
    def __init__(self):
        super(ExperimentPerformance, self).__init__()

    def run(self, savepath_e, savepath_s):
        for i in range(total):
            exemplar_input = torch.randn(exemplar_size)
            exemplar_input = np.array(exemplar_input).astype(np.float32)
            exemplar_name = "exemplar{}".format(i)
            exemplar_path = os.path.join(savepath_e, exemplar_name + ".bin")
            exemplar_input.tofile(exemplar_path)

            search_input = torch.randn(search_size)
            search_input = np.array(search_input).astype(np.float32)
            search_name = "search{}".format(i)
            search_path = os.path.join(savepath_s, search_name + ".bin")
            search_input.tofile(search_path)

if __name__ == "__main__":
    save_path_e = sys.argv[1]
    save_path_s = sys.argv[2]
    if not os.path.exists(save_path_e):
        os.makedirs(save_path_e)
    if not os.path.exists(save_path_s):
        os.makedirs(save_path_s)
    e = ExperimentPerformance()
    e.run(save_path_e, save_path_s)
    print("Data For Performance Ready.")


import requests
import csv
# importing flask
from flask import Flask, render_template

# importing pandas module
import pandas as pd

response = requests.get('https://api.github.com/search/repositories?q=fun+python&s=stars')
jsonout=response.json()

repo_id=jsonout["items"][0]["id"]
print(jsonout["items"][0]["id"])

print(jsonout["items"][0]["full_name"])
print(jsonout["items"][0]["url"])

#Saving to dataframe
#df = pd.DataFrame(columns=["repo_id"])

outdata =[]
csvheader=["full_name","url","star"]
for x in jsonout['items']:
    listing =[x["full_name"],x["html_url"],x["stargazers_count"]]
    outdata.append(listing)
df=pd.DataFrame(outdata)
print(df)


from django.conf import settings
from django.db.models.signals import post_save
from django.utils.text import slugify
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.db.models import Sum
from django.urls import reverse
from datetime import date

class User(AbstractUser):
    is_taker = models.BooleanField(default=True)
    is_master = models.BooleanField(default=False)
    is_project_manager = models.BooleanField(default=False)
    email = models.EmailField('email address', max_length=254, unique=True)

    def __str__(self):
        return self.username

class Profile(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    name = models.CharField(
        max_length=255, help_text="Your full name")
    image = models.ImageField(
        default='profile_default.jpg', upload_to='profile_pics')
    slug = models.SlugField(max_length=30, unique=True)
    about = models.TextField(default="A brief description about me")
    joined = models.DateTimeField("Date Joined", auto_now_add=True)
    wallet_address = models.CharField(
        max_length=35, help_text="Pi wallet address")
    telegram_id = models.CharField(max_length=100, null=True, blank=True, default="username",
                                   help_text="please input your correct telegram username to connect with friends, fellow quiz takers, and quiz masters")

    def __str__(self):
        return self.user.get_username()

class Payment(models.Model):
    transaction_id = models.CharField(max_length=50)
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             on_delete=models.SET_NULL, blank=True, null=True)
    amount = models.FloatField()
    timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.user.username

def post_user_created_signal(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

post_save.connect(post_user_created_signal, sender=User)


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

# In[2]:

# Importing required libraries for this Project.

import pandas as pd
import numpy as np
import seaborn as sns

import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import matplotlib
plt.style.use('ggplot')
from matplotlib.pyplot import figure

get_ipython().run_line_magic('matplotlib', 'inline')
matplotlib.rcParams['figure.figsize'] = (12,8)

pd.options.mode.chained_assignment = None

# Now need to read the data file from system

df = pd.read_csv(r'D:\movies2.csv')

# In[42]:

# Now lets look at the data
df

# In[46]:

# Serching for Missing data
for col in df.columns:
    pct_missing=np.mean(df[col].isnull())
    print(col,pct_missing)

# In[48]:

# Data Types of the column

df.dtypes

# In[57]:

# Creating another row of  year  as 'Correctyear'. we can see some of the year's from release date and year are no matching

df['correctyear']=df['released'].astype(str).str[6:10]
df

# In[64]:

pd.set_option('display.max_rows',None)
df.sort_values(by='gross' ,ascending=False)

# In[65]:

# Drop any duplictes

df.drop_duplicates()

# In[36]:

# Plot Bugget vs gross using seabord
plt.scatter(x=df['budget'], y=df['gross'], alpha=0.5)
plt.title('Budget vs Gross Earnings')
plt.xlabel('Gross Earnings')
plt.ylabel('Budget for Film')
plt.show()

sns.regplot(x="gross", y="budget",data=df, line_kws={"color":"blue"})

# In[13]:

# Plot Score vs gross using seabord
sns.regplot(x="score", y="gross", data=df , line_kws={"color":"black"})

# In[14]:

# Correlation Matrix between all numeric columns

df.corr()
# default  correlation method is pearson

# In[18]:

# Visualization corelation

correlation_matrix = df.corr()
sns.heatmap(correlation_matrix, annot = True)
plt.title("Correlation Feature for Numeric Features")
plt.xlabel("Movie features")
plt.ylabel("Movie features")
plt.show()

# ### High Corelation between Gross and Budget (0.71)

# In[25]:

df_numerized=df
for col_name in df_numerized.columns:
    if(df_numerized[col_name].dtype =='object'):
        df_numerized[col_name] = df_numerized[col_name].astype('category')
        df_numerized[col_name]=df_numerized[col_name].cat.codes

df_numerized

# In[27]:

df_numerized.corr()

# In[28]:

correlation_matrix = df_numerized.corr()

sns.heatmap(correlation_matrix, annot = True)

plt.title("Correlation matrix for Movies")

plt.xlabel("Movie features")

plt.ylabel("Movie features")

plt.show()

# In[44]:

pd.set_option('display.max_rows',None)
correlation_mat=df_numerized.corr()
corr_pairs=correlation_mat.unstack()
corr_pairs

# In[43]:

pd.set_option('display.max_rows',None)
sorted_pairs=corr_pairs.sort_values()
sorted_pairs

# In[42]:

pd.set_option('display.max_rows',None)
high_corr=sorted_pairs[(sorted_pairs)>.05]
high_corr

# ###  Votes and Budget have the higest corellation to gross earnings

# In[4]:

genre_gross=df.groupby(['genre'])['gross'].sum()
genre_gross=genre_gross.to_frame()
genre_gross.reset_index(inplace=True)

# In[5]:

fig = plt.figure(figsize=(18,8))
plt.bar(genre_gross['genre'],genre_gross['gross'])
plt.title("Genre vs Gross",size=30)
plt.xlabel("Genres",size=15)
plt.ylabel("Amount",size=15)
plt.show()

# In[6]:

sns.pairplot(df,diag_kind='hist')

# In[7]:

for col in df.columns:
    if df[col].dtype !="object":
        sns.distplot(df[col],bins=20)
        plt.show()

# In[ ]:

# In[ ]:

# In[ ]:

# In[ ]:

# In[ ]:

# In[ ]:

# In[ ]:

# In[ ]:

# In[ ]:

# In[ ]:

# In[ ]:

# In[ ]:

# In[ ]:

# In[ ]:


import contextlib
import paddle

from .opcode_transform.transform import eval_frame_callback
from .opcode_transform import convert
from .symbolic_trace import SymbolicTraceContext
from .proxy_tensor import clear_runtime_proxytensor

def to_static(func, with_log=True):
    convert.LOG_FLAG = with_log
    def wrapped(*args, **kw):
        with Dy2staticGuard(eval_frame_callback):
            func(*args, **kw)
    return wrapped

@contextlib.contextmanager
def Dy2staticGuard(callback):
    with SymbolicTraceContext() as ctx:
        clear_runtime_proxytensor()
        paddle.fluid.core.set_eval_frame(callback)
        yield
        paddle.fluid.core.set_eval_frame(None)
        SymbolicTraceContext().start_compile()


import sys
import time
from api.get_async_video_job import UAICensorGetAsyncVideoJobApi
from api.utils import gen_signature, load_config

# usage: python query_async_video_job.py config_file='censor-config.json' job_id

if __name__ == '__main__':
    # job_id = sys.argv[1]
    # if job_id == '':
    #     raise ValueError('usage: python query_async_video_job.py job_id')
    if len(sys.argv) == 3:
        config_file = sys.argv[1]
        job_id = sys.argv[2]
    else:
        config_file = 'censor-config.json'
        job_id = sys.argv[1]
    timestamp = int(time.time())
    public_key, private_key, resource_id = load_config(config_file)
    signature = gen_signature(config_file=config_file,timestamp=timestamp, job_id=job_id)
    caller = UAICensorGetAsyncVideoJobApi(signature, public_key, resource_id,
                                             timestamp, job_id=job_id)
    caller.call_api()


# Face Detection
import cv2

# ອ່ານຮູບພາບ
img = cv2.imread('images/boy-girl.jpg')

# ອ່ານ File ສຳຫຼັບ Classification
face_cascade = cv2.CascadeClassifier('detect/haarcascade_frontalface_default.xml')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# ຈຳແນກໃບໜ້າ
scaleFactory = 1.1
minNeighber = 3
face_detect = face_cascade.detectMultiScale(
    gray_img, scaleFactory, minNeighber)

# ສະແດງຕຳແໜ່ງທີ່ພົບໃບໜ້າ
for (x, y, w, h) in face_detect:
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), thickness=5)

# ສະແດງຮູບພາບ
cv2.imshow('Orginal', img)
cv2.imshow('Result', gray_img)
cv2.waitKey(0)
cv2.destroyAllWindows()