import urllib.parse

import bs4
import copy
import hashlib
import os.path

import traceback
import WebRequest

from WebMirror.processor.ProcessorBase import PageProcessor
# import TextScrape.SiteArchiver

import common.util.urlFuncs as urlFuncs
import WebMirror.processor.ProcessorUtils.gDocParse as gdp

# import TextScrape.RelinkLookup
# import TextScrape.RELINKABLE as RELINKABLE

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

class GdocPageProcessor(PageProcessor):

	wanted_mimetypes = ['text/html']
	want_priority    = 90

	@staticmethod
	def wantsUrl(url):
		return urlFuncs.isGdocUrl(url)[0]

	loggerPath = "Main.Text.GdocPageProcessor"

	def __init__(self, pageUrl, pgContent, loggerPath, relinkable, scannedDomains=None, tlds=None, **kwargs):
		self.loggerPath = loggerPath+".GDocExtract"
		self.pageUrl    = pageUrl

		self._relinkDomains = set()
		for url in relinkable:
			self._relinkDomains.add(url)

		self._tld            = set()
		self._scannedDomains = set()

		# Tell the path filtering mechanism that we can fetch google doc files
		# Not switchable, since not fetching google docs content from a google docs page
		# wouldn't work too well.
		self._scannedDomains.add('https://docs.google.com/document/')
		self._scannedDomains.add('https://docs.google.com/spreadsheets/')
		self._scannedDomains.add('https://drive.google.com/folderview')
		self._scannedDomains.add('https://drive.google.com/open')

		if not scannedDomains:
			scannedDomains = []
		if not tlds:
			tlds = []

		# Build the filtering structures for checking outgoing links.
		for tld in tlds:
			self._tld.add(tld)

		if isinstance(scannedDomains, (set, list)):
			for url in scannedDomains:
				self.installBaseUrl(url)
		else:
			self.installBaseUrl(scannedDomains)

		# File mapping LUT
		self.fMap = {}

	def installBaseUrl(self, url):
		# print("Inserting ", url)
		netloc = urllib.parse.urlsplit(url.lower()).netloc
		if not netloc:
			raise ValueError("One of the scanned domains collapsed down to an empty string: '%s'!" % url)

		# Generate the possible wordpress netloc values.
		if 'wordpress.com' in netloc:
			subdomain, mainDomain, tld = netloc.rsplit(".")[-3:]

			self._scannedDomains.add("www.{sub}.{main}.{tld}".format(sub=subdomain, main=mainDomain, tld=tld))
			self._scannedDomains.add("{sub}.{main}.{tld}".format(sub=subdomain, main=mainDomain, tld=tld))
			self._scannedDomains.add("www.{sub}.files.{main}.{tld}".format(sub=subdomain, main=mainDomain, tld=tld))
			self._scannedDomains.add("{sub}.files.{main}.{tld}".format(sub=subdomain, main=mainDomain, tld=tld))

		# Blogspot is annoying and sometimes a single site is spread over several tlds. *.com, *.sg, etc...
		if 'blogspot.' in netloc:
			subdomain, mainDomain, tld = netloc.rsplit(".")[-3:]
			self._tld.add(tld)
			for tld in self._tld:
				self._scannedDomains.add("www.{sub}.{main}.{tld}".format(sub=subdomain, main=mainDomain, tld=tld))
				self._scannedDomains.add("{sub}.{main}.{tld}".format(sub=subdomain, main=mainDomain, tld=tld))

		if 'sites.google.com/site/' in url:
			self._scannedDomains.add(url)

		elif 'google.' in netloc:
			self.log.info("Skipping URL: '%s'", url)

		else:

			base, tld = netloc.rsplit(".", 1)
			self._tld.add(tld)
			for tld in self._tld:
				self._scannedDomains.add("{main}.{tld}".format(main=base, tld=tld))
				# print(self._scannedDomains)

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

	def processGdocResources(self, resources):

		# Expected format of tuples in ret:
		# fName, mimeType, content, fHash
		ret = []

		for fName, mimeType, content in resources:
			m = hashlib.md5()
			m.update(content)
			fHash = m.hexdigest()

			pseudoUrl = "gdoc-"+fHash

			self.fMap[fName] = fHash

			fName = os.path.split(fName)[-1]

			self.log.info("Resource = '%s', '%s', '%s'", fName, mimeType, pseudoUrl)
			if mimeType in ["image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/svg+xml", "image/vnd.djvu"]:
				self.log.info("Processing resource '%s' as an image file. (mimetype: %s)", fName, mimeType)
				ret.append((fName, mimeType, content, pseudoUrl))
			elif mimeType in ["application/octet-stream"]:
				self.log.info("Processing '%s' as an binary file.", fName)
				ret.append((fName, mimeType, content, pseudoUrl))
			else:
				self.log.warn("Unknown MIME Type? '%s', FileName: '%s'", mimeType, fName)

		if len(resources) == 0:
			self.log.info("File had no resource content!")

		return ret

	def cleanGdocPage(self, soup, url):
		# doc = readability.readability.Document(str(soup))
		title = self.extractTitle(soup, url)

		for span in soup.find_all("span"):
			span.unwrap()

		for style in soup.find_all('style'):
			style.decompose()

		for tag in soup.find_all(attrs = {'class' : True}):
			del tag['class']

		return title, soup

	# Hook so plugins can modify the internal URLs as part of the relinking process
	def preprocessGdocReaderUrl(self, inUrl):
		if inUrl.lower().endswith("/preview"):
			inUrl = inUrl[:-len("/preview")]

		return inUrl

	def convertToGdocReaderImage(self, srcUrl):

		itemHash = None
		for rscEnd in self.fMap:
			if srcUrl.endswith(rscEnd):
				itemHash = self.fMap[rscEnd]

		# if srcUrl in self.fMap:
		# 	url = self.fMap[srcUrl]
		# elif any([fUrl in url for fUrl in self.fMap]):
		# 	print('wat')
		# 	raise ValueError("Unknown image URL! = '%s'" % url)
		if not itemHash:
			raise ValueError("Unknown image URL! = '%s' (hash '%s')" % (srcUrl, itemHash))

		url = '/books/render?mdsum=%s' % urllib.parse.quote(itemHash)

		return url

	def processGdocPage(self, url, content):
		dummy_fName, content = content
		soup = WebRequest.as_soup(content)
		urlFuncs.canonizeUrls(soup, url)

		pgTitle, soup = self.cleanGdocPage(soup, url)

		plainLinks = self.extractLinks(soup, url)
		self.log.info("Page title = '%s'", pgTitle)
		soup = self.relink(soup, imRelink=self.convertToGdocReaderImage)

		url = self.preprocessGdocReaderUrl(url)
		url = urlFuncs.trimGDocUrl(url)
		# Since the content we're extracting will be embedded into another page, we want to
		# strip out the <body> and <html> tags. `unwrap()`  replaces the soup with the contents of the
		# tag it's called on. We end up with just the contents of the <body> tag.
		soup.body.unwrap()
		pgBody = soup.prettify()

		# No image links, since they're served as resource files in a google doc
		imageLinks = []
		return plainLinks, imageLinks, pgTitle, pgBody
		# self.updateDbEntry(url=url, title=pgTitle, contents=pgBody, mimetype='text/html', dlstate=2)

	def retreiveGoogleDoc(self, url):

		self.log.info("Should fetch google doc at '%s'", url)
		doc = gdp.GDocExtractor(url)

		attempts = 0

		mainPage = None
		while 1:
			attempts += 1
			try:
				mainPage, resources = doc.extract()
			except TypeError:
				self.log.critical('Extracting item failed!')
				for line in traceback.format_exc().strip().split("\n"):
					self.log.critical(line.strip())

					raise urlFuncs.CannotAccessGDocException("Cannot access google doc! Is it protected?")

			if mainPage:
				break
			if attempts > 3:
				raise TextScrape.SiteArchiver.DownloadException

		resources = self.processGdocResources(resources)

		return self.processGdocPage(url, mainPage) + (resources, )

	# Process a Google-Doc resource page.
	# This call does a set of operations to permute and clean a google doc page.
	def extractContent(self):

		plainLinks, imageLinks, pgTitle, pgBody, resources = self.retreiveGoogleDoc(self.pageUrl)

		ret = {}
		ret['plainLinks'] = plainLinks
		ret['rsrcLinks']  = imageLinks
		ret['title']      = pgTitle
		ret['contents']   = pgBody

		ret['resources'] = resources

		return ret

def test():
	print("Test mode!")
	import WebRequest
	import logSetup
	logSetup.initLogging()

	wg = WebRequest.WebGetRobust()
	# content = wg.getpage('http://www.arstechnica.com')
	scraper = GdocPageProcessor('https://docs.google.com/document/d/1atXMtCutHRpcHwSRS5UyMAC58_gQjMPR2dDVn1LCD3E', 'Main.Test', 'testinating')
	print(scraper)
	extr, rsc = scraper.extractContent()
	print('Plain Links:')
	for link in extr['plainLinks']:
		print(link)
	print()
	print()
	print('Resource files:')
	# for link in extr['rsrcLinks']:
	# 	print(link)

	for fName, mimeType, content, pseudoUrl in rsc:
		print(fName, mimeType, pseudoUrl)
	# print(extr['contents'])

if __name__ == "__main__":
	test()


# Import packages

from pylab import *
import numpy as numpy
#
#===============================================================================
# Plot multiple flux-hardness graphs for different flux levels for comparison
# (and light curve if wanted)
#===============================================================================

#   File routes
route           	= "/disks/raid/raid1/xray/raid/sdc1g08/NetData/\
Ngc5548/lightcurve"
energysoftroute  	= "/refined/"
energyhardroute  	= "/refined/"

# file names
energysoftfname 	= "NGC5548_lcurve_3_gti_0.5-2keV.qdp"
energyhardfname		= "NGC5548_lcurve_3_gti_2-10keV.qdp"

# create file routes
energysoftlocation 		= route+energysoftroute+energysoftfname
energyhardlocation 		= route+energyhardroute+energyhardfname

# Function to calculate hardness and error

def hardnessflux(fmin,fmax):

	# read data into 2-D arrays
	start = 0

	softtime    = numpy.array([])
	softflux    = numpy.array([])
	softfluxerr = numpy.array([])
	hardtime    = numpy.array([])
	hardflux    = numpy.array([])
	hardfluxerr = numpy.array([])

	energysoftin= open(energysoftlocation, 'r')
	energyhardin= open(energyhardlocation, 'r')

	hdata = energyhardin.readlines()

	for line in energysoftin:

			scurrtime, scurrflux, scurrfluxerr = line.split()

			if start >= 1:

				hcurrtime, hcurrflux, hcurrfluxerr = hdata[start].split()

				totflux = float(scurrflux) + float(hcurrflux)

				if totflux >= fmin and totflux <= fmax:

					softtime    = numpy.append(softtime, float(scurrtime))
					softflux    = numpy.append(softflux, float(scurrflux))
					softfluxerr = numpy.append(softfluxerr, float(scurrfluxerr))
					hardtime    = numpy.append(hardtime, float(hcurrtime))
					hardflux    = numpy.append(hardflux, float(hcurrflux))
					hardfluxerr = numpy.append(hardfluxerr, float(hcurrfluxerr))

			start += 1

	energyhardin.close()
	energysoftin.close()

	hardness    = hardflux/softflux
	hardnesserr = hardness*(numpy.sqrt((hardfluxerr/hardflux)**2 + \
			(softfluxerr/softflux)**2))

	# Calculate modified hardness and error (H - S / H + S)

	modhardness = (hardflux-softflux)/(hardflux+softflux)
	sumerr = sqrt(hardfluxerr**2 + softfluxerr**2)
	modhardnesserr = modhardness*(numpy.sqrt(\
	(sumerr/(hardflux-softflux))**2 + (sumerr/(hardflux+softflux))**2 ) )

	# sort modified hardnesses and soft flux according to hard flux

	modfluxhard = [[],[],[],[],[],[]]
	start = 0

	for value in range(len(modhardness)):
		# if it isn't the first value...
		if start !=0:
			for comp in range(len(modfluxhard[0])): # for each value
							# from the bottom up...
				if hardflux[value] <= modfluxhard[0][comp]:# add
						     						 # below value if less/equal
					modfluxhard[0].insert(comp,hardflux[value])
					modfluxhard[1].insert(comp,hardfluxerr[value])
					modfluxhard[2].insert(comp,softflux[value])
					modfluxhard[3].insert(comp,softfluxerr[value])
					modfluxhard[4].insert(comp,modhardness[value])
					modfluxhard[5].insert(comp,modhardnesserr[value])
					break
				if comp == len(modfluxhard[0]) -1:  # add after last
								 					# element if > than all
					modfluxhard[0].append(hardflux[value])
					modfluxhard[1].append(hardfluxerr[value])
					modfluxhard[2].append(softflux[value])
					modfluxhard[3].append(softfluxerr[value])
					modfluxhard[4].append(modhardness[value])
					modfluxhard[5].append(modhardnesserr[value])

		# if it is the first value, just fill that stuff in
		if start == 0:
			modfluxhard[0].append(hardflux[value])
			modfluxhard[1].append(hardfluxerr[value])
			modfluxhard[2].append(softflux[value])
			modfluxhard[3].append(softfluxerr[value])
			modfluxhard[4].append(modhardness[value])
			modfluxhard[5].append(modhardnesserr[value])
			start = 1

	totalflux = hardflux + softflux

	return modfluxhard, totalflux, hardtime

# ======= PLOTS TO MAKE ========================================================

fluxes = [[0,1],	# tmin1,tmax1
		 [1,2],	# tmin2,tmax2
		 [2,3],		# tmin3,tmax3
		 [3,5]]			# tmin4,tmax4

modfluxhard1, totalflux1, time1 = hardnessflux(fluxes[0][0],fluxes[0][1])
modfluxhard2, totalflux2, time2 = hardnessflux(fluxes[1][0],fluxes[1][1])
modfluxhard3, totalflux3, time3 = hardnessflux(fluxes[2][0],fluxes[2][1])
modfluxhard4, totalflux4, time4 = hardnessflux(fluxes[3][0],fluxes[3][1])

errors = False
single = True
lightcurves = True

# ==============================================================================

# lightcurves

if lightcurves:
	nplots = 3
else:
	nplots = 2

# plot with pylab

if single:

	if errors:

		subplot(nplots,1,1)
		title("hard flux v hardness")
		p1 = errorbar(modfluxhard1[0],modfluxhard1[4],yerr=modfluxhard1[5])
		p2 = errorbar(modfluxhard2[0],modfluxhard2[4],yerr=modfluxhard2[5])
		p3 = errorbar(modfluxhard3[0],modfluxhard3[4],yerr=modfluxhard3[5])
		p4 = errorbar(modfluxhard4[0],modfluxhard4[4],yerr=modfluxhard4[5])
		legend([p1,p2,p3,p4],  ["MJD: {0}".format(fluxes[0]),
								"MJD: {0}".format(fluxes[1]),
								"MJD: {0}".format(fluxes[2]),
								"MJD: {0}".format(fluxes[3]),])
		subplot(nplots,1,2)
		title("hard flux v soft flux")
		p1 = errorbar(modfluxhard1[0],modfluxhard1[2],xerr=modfluxhard1[1]\
					,yerr=modfluxhard1[3])
		p2 = errorbar(modfluxhard2[0],modfluxhard2[2],xerr=modfluxhard2[1]\
				,yerr=modfluxhard2[3])
		p3 = errorbar(modfluxhard3[0],modfluxhard3[2],xerr=modfluxhard3[1]\
				,yerr=modfluxhard3[3])
		p4 = errorbar(modfluxhard4[0],modfluxhard4[2],xerr=modfluxhard4[1]\
				,yerr=modfluxhard4[3])
		legend([p1,p2,p3,p4],  ["MJD: {0}".format(fluxes[0]),
								"MJD: {0}".format(fluxes[1]),
								"MJD: {0}".format(fluxes[2]),
								"MJD: {0}".format(fluxes[3]),])
	else:

		subplot(nplots,1,1)
		title("hard flux v hardness")
		p1 = scatter(modfluxhard1[0],modfluxhard1[4], color = "blue")
		p2 = scatter(modfluxhard2[0],modfluxhard2[4], color = "red")
		p3 = scatter(modfluxhard3[0],modfluxhard3[4], color = "green")
		p4 = scatter(modfluxhard4[0],modfluxhard4[4], color = "purple")
		legend([p1,p2,p3,p4],  ["MJD: {0}".format(fluxes[0]),
								"MJD: {0}".format(fluxes[1]),
								"MJD: {0}".format(fluxes[2]),
								"MJD: {0}".format(fluxes[3]),])

		subplot(nplots,1,2)
		title("hard flux v soft flux")
		p1 = scatter(modfluxhard1[0],modfluxhard1[2], color = "blue")
		p2 = scatter(modfluxhard2[0],modfluxhard2[2], color = "red")
		p3 = scatter(modfluxhard3[0],modfluxhard3[2], color = "green")
		p4 = scatter(modfluxhard4[0],modfluxhard4[2], color = "purple")
		legend([p1,p2,p3,p4],  ["MJD: {0}".format(fluxes[0]),
								"MJD: {0}".format(fluxes[1]),
								"MJD: {0}".format(fluxes[2]),
								"MJD: {0}".format(fluxes[3]),])

	if lightcurves:
		subplot(nplots,1,3)
		title("Lightcurve")
		p1 = scatter(time1,totalflux1, color = "blue")
		p2 = scatter(time2,totalflux2, color = "red")
		p3 = scatter(time3,totalflux3, color = "green")
		p4 = scatter(time4,totalflux4, color = "purple")
		legend([p1,p2,p3,p4],  ["MJD: {0}".format(fluxes[0]),
								"MJD: {0}".format(fluxes[1]),
								"MJD: {0}".format(fluxes[2]),
								"MJD: {0}".format(fluxes[3]),])
else:

	if errors:

		subplot(nplots+2,2,1)
		title("hard flux v hardness")
		errorbar(modfluxhard1[0],modfluxhard1[4],yerr=modfluxhard1[5])
		subplot(nplots+2,2,2)
		title("hard flux v soft flux")
		errorbar(modfluxhard1[0],modfluxhard1[2],xerr=modfluxhard1[1]\
					,yerr=modfluxhard1[3])
		subplot(nplots+2,2,3)
		errorbar(modfluxhard2[0],modfluxhard2[4],yerr=modfluxhard2[5])
		subplot(nplots+2,2,4)
		errorbar(modfluxhard2[0],modfluxhard2[2],xerr=modfluxhard2[1]\
				,yerr=modfluxhard2[3])
		subplot(nplots+2,2,5)
		errorbar(modfluxhard3[0],modfluxhard3[4],yerr=modfluxhard3[5])
		subplot(nplots+2,2,6)
		errorbar(modfluxhard3[0],modfluxhard3[2],xerr=modfluxhard3[1]\
				,yerr=modfluxhard3[3])
		subplot(nplots+2,2,7)
		errorbar(modfluxhard4[0],modfluxhard4[4],yerr=modfluxhard4[5])
		subplot(nplots+2,2,8)
		errorbar(modfluxhard4[0],modfluxhard4[2],xerr=modfluxhard4[1]\
				,yerr=modfluxhard4[3])

	else:

		subplot(nplots+2,2,1)
		title("hard flux v hardness")
		scatter(modfluxhard1[0],modfluxhard1[4])
		subplot(nplots+2,2,2)
		title("hard flux v soft flux")
		scatter(modfluxhard1[0],modfluxhard1[2])
		subplot(nplots+2,2,3)
		scatter(modfluxhard2[0],modfluxhard2[4])
		subplot(nplots+2,2,4)
		scatter(modfluxhard2[0],modfluxhard2[2])
		subplot(nplots+2,2,5)
		scatter(modfluxhard3[0],modfluxhard3[4])
		subplot(nplots+2,2,6)
		scatter(modfluxhard3[0],modfluxhard3[2])
		subplot(nplots+2,2,7)
		scatter(modfluxhard4[0],modfluxhard4[4])
		subplot(nplots+2,2,8)
		scatter(modfluxhard4[0],modfluxhard4[2])

	if lightcurves:
		subplot(nplots+2,1,3)
		title("Lightcurve")
		p1 = scatter(time1,totalflux1, color = "blue")
		p2 = scatter(time2,totalflux2, color = "red")
		p3 = scatter(time3,totalflux3, color = "green")
		p4 = scatter(time4,totalflux4, color = "purple")
		legend([p1,p2,p3,p4],  ["MJD: {0}".format(fluxes[0]),
								"MJD: {0}".format(fluxes[1]),
								"MJD: {0}".format(fluxes[2]),
								"MJD: {0}".format(fluxes[3]),])

show()


import pygame
import random

RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

class Snake:
    should_grow = False
    can_move = True
    def __init__(self, position,world_size,color=GREEN,size=16):
        self.body = [position]
        direction = random.choice([[0,size],[size,0],[0,-size],[-size,0]])
        self.vy = direction[0]
        self.vx = direction[1]
        self.color = color
        self.size = size
        self.world_size = world_size

    def new_pos(self):
        y = self.body[0][0]+self.vy
        x = self.body[0][1]+self.vx
        if y < 0:
            y = self.world_size-self.size
        elif y>= self.world_size:
            y = 0

        if x < 0:
            x = self.world_size-self.size
        elif x>= self.world_size:
            x = 0

        return (y,x)

    def update(self):
        if self.should_grow:
            self.body = [self.new_pos()]+self.body
            self.should_grow = False
        else:
            self.body = [self.new_pos()]+self.body[:-1]

    def changeMovement(self,option):
        if self.can_move:
            if option == 0 and self.vx != self.size:
                self.vx = -self.size
                self.vy = 0
                can_move = False
            elif option == 1 and self.vy != self.size:
                self.vx = 0
                self.vy = -self.size
                can_move = False
            elif option == 2 and self.vx != -self.size:
                self.vx = self.size
                self.vy = 0
                can_move = False
            elif option ==3 and self.vy != -self.size:
                self.vx = 0
                self.vy = self.size
                can_move = False

    def allow_move(self):
        self.can_move = True

    def check_collision_self(self):
        if self.body[0] in self.body[1:]:
            return True
        return False

    def check_collision_food(self, food_pos):
        if self.body[0] == food_pos:
            self.grow()
            return True
        return False

    def check_collision_snake(self, snake):
        if self.body[0] in snake.body[1:]:
            return True
        if self.body[0] == snake.body[0] and not (self.vx == -snake.vx or self.vy == -snake.vy):
            return True
        return False

    def grow(self):
        self.should_grow = True

    def draw(self,surface):
        for element in self.body:
            pygame.draw.rect(surface,self.color, pygame.Rect(element[1],element[0],self.size,self.size))

class Food():
    def __init__(self, world_size,snake_body,size=16):
        self.N = world_size//size
        self.size=size
        self.init_position(snake_body)
        self.color = RED

    def init_position(self, snake_body):
        positions = []
        for i in range(self.N):
            for j in range(self.N):
                pos = (i,j)
                if pos not in snake_body:
                    positions += [pos]
        position = random.choice(positions)
        self.position = (position[0]*self.size,position[1]*self.size)

    def update(self):
        return

    def draw(self, surface):
        pygame.draw.rect(surface,self.color, pygame.Rect(self.position[1],self.position[0],self.size,self.size))

if __name__ == "__main__":

    successes, failures = pygame.init()

    size = 32
    world_size = size*22

    screen = pygame.display.set_mode((world_size, world_size))  # Notice the tuple! It's not 2 arguments.
    clock = pygame.time.Clock()
    FPS = 15  # This variable will define how many frames we update per second.

    snake1 = Snake((world_size-size*2, size),world_size,size=size)
    snake2 = Snake((size, world_size-size*2),world_size,color=BLUE,size=size)
    food = Food(world_size,snake1.body+snake2.body,size=size)

    elements = [snake1, snake2,food]

    while True:
        clock.tick(FPS)

        snake1.allow_move()
        snake2.allow_move()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    snake1.changeMovement(0)
                elif event.key == pygame.K_w:
                    snake1.changeMovement(1)
                elif event.key == pygame.K_d:
                    snake1.changeMovement(2)
                elif event.key == pygame.K_s:
                    snake1.changeMovement(3)

                elif event.key == pygame.K_LEFT:
                    snake2.changeMovement(0)
                elif event.key == pygame.K_UP:
                    snake2.changeMovement(1)
                elif event.key == pygame.K_RIGHT:
                    snake2.changeMovement(2)
                elif event.key == pygame.K_DOWN:
                    snake2.changeMovement(3)

        screen.fill(BLACK)

        # update
        for el in elements:
            el.update()

        # check collision
        if snake1.check_collision_self():
            break
        if snake1.check_collision_food(food.position):
            food.init_position(snake1.body+snake2.body)
        if snake1.check_collision_snake(snake2):
            print("blue snake wins")
            break

        if snake2.check_collision_self():
            break
        if snake2.check_collision_food(food.position):
            food.init_position(snake1.body+snake2.body)
        if snake2.check_collision_snake(snake1):
            print("green snake wins")
            break

        # draw
        for el in elements:
            el.draw(screen)

        pygame.display.update()  # Or 'pygame.display.flip()'.


import json
import os
import tempfile
from abc import ABC
from typing import Union

import numpy as np
import pytest
from elasticsearch import NotFoundError

try:
    import torch  # noqa: F401
    from torch import Tensor, nn  # noqa: F401
    from transformers import PretrainedConfig, elasticsearch_model_id  # noqa: F401

    from eland.ml.pytorch import (  # noqa: F401
        NlpBertTokenizationConfig,
        NlpTrainedModelConfig,
        PyTorchModel,
        TraceableModel,
        task_type_from_model_config,
    )
    from eland.ml.pytorch.nlp_ml_model import (
        NerInferenceOptions,
        TextClassificationInferenceOptions,
        TextEmbeddingInferenceOptions,
    )

    TracedModelTypes = Union[  # noqa: F401
        torch.nn.Module,
        torch.ScriptModule,
        torch.jit.ScriptModule,
        torch.jit.TopLevelTracedModule,
    ]

    HAS_PYTORCH = True
except ImportError:
    HAS_PYTORCH = False

from tests import ES_TEST_CLIENT, ES_VERSION

pytestmark = [
    pytest.mark.skipif(
        ES_VERSION < (8, 0, 0),
        reason="This test requires at least Elasticsearch version 8.0.0",
    ),
    pytest.mark.skipif(
        not HAS_PYTORCH, reason="This test requires 'pytorch' package to run"
    ),
]

TEST_BERT_VOCAB = [
    "Elastic",
    "##search",
    "is",
    "fun",
    "my",
    "little",
    "red",
    "car",
    "God",
    "##zilla",
    ".",
    ",",
    "[CLS]",
    "[SEP]",
    "[MASK]",
    "[PAD]",
    "[UNK]",
    "day",
    "Pancake",
    "with",
    "?",
]

NER_LABELS = [
    "O",
    "B_MISC",
    "I_MISC",
    "B_PER",
    "I_PER",
    "B_ORG",
    "I_ORG",
    "B_LOC",
    "I_LOC",
]

TEXT_CLASSIFICATION_LABELS = ["foo", "bar", "baz"]

if not HAS_PYTORCH:
    pytest.skip("This test requires 'pytorch' package to run", allow_module_level=True)

class TestTraceableModel(TraceableModel, ABC):
    def __init__(self, model: nn.Module):
        super().__init__(model)

    def _trace(self) -> TracedModelTypes:
        input_ids = torch.tensor(np.array(range(0, len(TEST_BERT_VOCAB))))
        attention_mask = torch.tensor([1] * len(TEST_BERT_VOCAB))
        token_type_ids = torch.tensor([0] * len(TEST_BERT_VOCAB))
        position_ids = torch.arange(len(TEST_BERT_VOCAB), dtype=torch.long)
        return torch.jit.trace(
            self._model,
            (
                input_ids,
                attention_mask,
                token_type_ids,
                position_ids,
            ),
        )

    def sample_output(self) -> torch.Tensor:
        input_ids = torch.tensor(np.array(range(0, len(TEST_BERT_VOCAB))))
        attention_mask = torch.tensor([1] * len(TEST_BERT_VOCAB))
        token_type_ids = torch.tensor([0] * len(TEST_BERT_VOCAB))
        position_ids = torch.arange(len(TEST_BERT_VOCAB), dtype=torch.long)
        return self._model(input_ids, attention_mask, token_type_ids, position_ids)

class NerModule(nn.Module):
    def forward(
        self,
        input_ids: Tensor,
        _attention_mask: Tensor,
        _token_type_ids: Tensor,
        _position_ids: Tensor,
    ) -> Tensor:
        """Wrap the input and output to conform to the native process interface."""
        outside = [0] * len(NER_LABELS)
        outside[0] = 1
        person = [0] * len(NER_LABELS)
        person[3] = 1
        person[4] = 1
        result = [outside for _t in np.array(input_ids.data)]
        result[1] = person
        result[2] = person
        return torch.tensor([result], dtype=torch.float)

class EmbeddingModule(nn.Module):
    def forward(
        self,
        _input_ids: Tensor,
        _attention_mask: Tensor,
        _token_type_ids: Tensor,
        _position_ids: Tensor,
    ) -> Tensor:
        """Wrap the input and output to conform to the native process interface."""
        result = [0] * 512
        return torch.tensor([result], dtype=torch.float)

class TextClassificationModule(nn.Module):
    def forward(
        self,
        _input_ids: Tensor,
        _attention_mask: Tensor,
        _token_type_ids: Tensor,
        _position_ids: Tensor,
    ) -> Tensor:
        # foo, bar, baz are the classification labels
        result = [0, 1.0, 0]
        return torch.tensor([result], dtype=torch.float)

MODELS_TO_TEST = [
    (
        "ner",
        TestTraceableModel(model=NerModule()),
        NlpTrainedModelConfig(
            description="test ner model",
            inference_config=NerInferenceOptions(
                tokenization=NlpBertTokenizationConfig(),
                classification_labels=NER_LABELS,
            ),
        ),
        "Godzilla Pancake Elasticsearch is fun.",
        "[Godzilla](PER&Godzilla) Pancake Elasticsearch is fun.",
    ),
    (
        "embedding",
        TestTraceableModel(model=EmbeddingModule()),
        NlpTrainedModelConfig(
            description="test text_embedding model",
            inference_config=TextEmbeddingInferenceOptions(
                tokenization=NlpBertTokenizationConfig()
            ),
        ),
        "Godzilla Pancake Elasticsearch is fun.",
        [0] * 512,
    ),
    (
        "text_classification",
        TestTraceableModel(model=TextClassificationModule()),
        NlpTrainedModelConfig(
            description="test text_classification model",
            inference_config=TextClassificationInferenceOptions(
                tokenization=NlpBertTokenizationConfig(),
                classification_labels=TEXT_CLASSIFICATION_LABELS,
            ),
        ),
        "Godzilla Pancake Elasticsearch is fun.",
        "bar",
    ),
]

AUTO_TASK_RESULTS = [
    ("any_bert", "BERTMaskedLM", None, "fill_mask"),
    ("any_roberta", "RoBERTaMaskedLM", None, "fill_mask"),
    ("sentence-transformers/any_bert", "BERTMaskedLM", None, "text_embedding"),
    ("sentence-transformers/any_roberta", "RoBERTaMaskedLM", None, "text_embedding"),
    ("sentence-transformers/mpnet", "MPNetMaskedLM", None, "text_embedding"),
    ("sentence-transformers/any_bert", "BertModel", None, "text_embedding"),
    ("anynermodel", "BERTForTokenClassification", None, "ner"),
    ("anynermodel", "MPNetForTokenClassification", None, "ner"),
    ("anynermodel", "RoBERTaForTokenClassification", None, "ner"),
    ("anynermodel", "BERTForQuestionAnswering", None, "question_answering"),
    ("anynermodel", "MPNetForQuestionAnswering", None, "question_answering"),
    ("anynermodel", "RoBERTaForQuestionAnswering", None, "question_answering"),
    ("aqaModel", "DPRQuestionEncoder", None, "text_embedding"),
    ("aqaModel", "DPRContextEncoder", None, "text_embedding"),
    (
        "any_bert",
        "BERTForSequenceClassification",
        ["foo", "bar", "baz"],
        "text_classification",
    ),
    (
        "any_bert",
        "BERTForSequenceClassification",
        ["contradiction", "neutral", "entailment"],
        "zero_shot_classification",
    ),
    (
        "any_bert",
        "BERTForSequenceClassification",
        ["CONTRADICTION", "NEUTRAL", "ENTAILMENT"],
        "zero_shot_classification",
    ),
    ("any_bert", "SomeUnknownType", None, None),
]

@pytest.fixture(scope="function", autouse=True)
def setup_and_tear_down():
    ES_TEST_CLIENT.cluster.put_settings(
        body={"transient": {"logger.org.elasticsearch.xpack.ml": "DEBUG"}}
    )
    yield
    for (
        model_id,
        _,
        _,
        _,
        _,
    ) in MODELS_TO_TEST:
        model = PyTorchModel(ES_TEST_CLIENT, model_id.replace("/", "__").lower()[:64])
        try:
            model.stop()
            model.delete()
        except NotFoundError:
            pass

def upload_model_and_start_deployment(
    tmp_dir: str, model: TraceableModel, config: NlpTrainedModelConfig, model_id: str
):
    print("Loading HuggingFace transformer tokenizer and model")
    model_path = model.save(tmp_dir)
    vocab_path = os.path.join(tmp_dir, "vocabulary.json")
    with open(vocab_path, "w") as outfile:
        json.dump({"vocabulary": TEST_BERT_VOCAB}, outfile)
    ptm = PyTorchModel(ES_TEST_CLIENT, model_id)
    try:
        ptm.stop()
        ptm.delete()
    except NotFoundError:
        pass
    print(f"Importing model: {ptm.model_id}")
    ptm.import_model(
        model_path=model_path, config_path=None, vocab_path=vocab_path, config=config
    )
    ptm.start()
    return ptm

class TestPytorchModelUpload:
    @pytest.mark.parametrize("model_id,model,config,input,prediction", MODELS_TO_TEST)
    def test_model_upload(self, model_id, model, config, input, prediction):
        with tempfile.TemporaryDirectory() as tmp_dir:
            ptm = upload_model_and_start_deployment(tmp_dir, model, config, model_id)
            result = ptm.infer(docs=[{"text_field": input}])
            assert result.get("predicted_value") is not None
            assert result["predicted_value"] == prediction

    @pytest.mark.parametrize(
        "model_id,architecture,labels,expected_task", AUTO_TASK_RESULTS
    )
    def test_auto_task_type(self, model_id, architecture, labels, expected_task):
        config = (
            PretrainedConfig(
                name_or_path=model_id,
                architectures=[architecture],
                label2id=dict(zip(labels, range(len(labels)))),
                id2label=dict(zip(range(len(labels)), labels)),
            )
            if labels
            else PretrainedConfig(
                name_or_path=model_id,
                architectures=[architecture],
            )
        )
        assert task_type_from_model_config(model_config=config) == expected_task

    def test_elasticsearch_model_id(self):
        model_id_from_path = elasticsearch_model_id(r"/foo/bar")
        assert model_id_from_path == "foo_bar"

        model_id_from_path = elasticsearch_model_id(r"\BAZ\with space")
        assert model_id_from_path == "baz__with__space"

        model_id_from_path = elasticsearch_model_id(r"/foo/with tab\tback\slash")
        assert model_id_from_path == "foo__with__space__back__slash"

        long_id_left_truncated = elasticsearch_model_id(
            "/foo/bar/64charactersoftext64charactersoftext64charactersoftext64charofte"
        )
        assert (
            long_id_left_truncated
            == "64charactersoftext64charactersoftext64charactersoftext64charofte"
        )


import tweepy

consumer_key = 'xxxx'
consumer_secret = 'xxxx'
access_token = 'xxxx-xxxx'
access_token_secret = 'xxxx'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

DT_tweets = api.user_timeline('realDonaldTrump')

tweets = []
for tweet in DT_tweets:
	tweets.append({'time':tweet.created_at, 'text':tweet.text})

print(tweets)


from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from datetime import datetime, timedelta

chrome_driver_path = "C:\\Development\\chromedriver.exe"
chrome_service = Service(executable_path=chrome_driver_path)
driver = webdriver.Chrome(service=chrome_service)

driver.get('http://orteil.dashnet.org/experiments/cookie/')

cookie = driver.find_element(By.ID, "cookie")

game_end_time = datetime.now() + timedelta(minutes=5)
chronometer_end_time = datetime.now() + timedelta(seconds=1)
while datetime.now() < game_end_time:
    cookie.click()

    if datetime.now() >= chronometer_end_time:
        chronometer_end_time = datetime.now() + timedelta(seconds=3)  # Update 5 second timer.

        # Get items fom store again with updated prices.
        store = driver.find_elements(By.CSS_SELECTOR, "#store b")[:-1]
        store_prices = [int(item.text.split("- ")[-1].replace(",", "")) for item in store][::-1]

        store_clickable_items = driver.find_elements(By.CSS_SELECTOR, "#store div")[:-1]
        store_clickable_items = store_clickable_items[::-1]

        for idx, item_price in enumerate(store_prices):
            money = int(driver.find_element(By.ID, "money").text.replace(",", ""))
            if item_price <= money:
                store_clickable_items[idx].click()
                break

cookies_per_second = driver.find_element(By.ID, "cps").text
print(cookies_per_second)
driver.quit()


def extractWateriftWordpressCom(item):
	'''
	Parser for 'waterift.wordpress.com'
	'''

	vol, chp, frag, postfix = extractVolChapterFragmentPostfix(item['title'])
	if not (chp or vol) or "preview" in item['title'].lower():
		return None

	tagmap = [
		('Noire de Plaisir',                                 'Noire de Plaisir ~ Pleasure Training of the Fallen Vampire Princess~',                      'translated'),
		('I Have The Only Ero Knowledge In The World',       'I Have The Only Ero Knowledge In The World, So I Decided To Cum Inside Pretty Girls',       'translated'),
		('Loiterous', 'Loiterous',                'oel'),
	]

	for tagname, name, tl_type in tagmap:
		if tagname in item['tags']:
			return buildReleaseMessageWithType(item, name, vol, chp, frag=frag, postfix=postfix, tl_type=tl_type)

	return False


from collections import deque
from math import ceil

def solution(progresses, speeds):
    answer = []
    queue = deque()
    for idx, progress in enumerate(progresses):
        tmp = 0
        remain_work = 100 - progress
        required_day = ceil(remain_work / speeds[idx])

        if not queue:
            queue.append(required_day)
        else:
            while queue:
                if queue[0] < required_day:
                    tmp += 1
                    queue.popleft()
                else:
                    break
            queue.append(required_day)
        if tmp != 0:
            answer.append(tmp)
    if queue:
        answer.append(len(queue))

    return answer

if __name__ == "__main__":
    print(solution( [5, 5, 5], speeds=[21, 25, 20]))


from itertools import combinations
n = int(input())

f = [list(map(int, input().split())) for _ in range(n)]
p = [list(map(int, input().split())) for _ in range(n)]

li = [i for i in range(10)]
pro_count = -float('inf')

for i in range(1,11):
    iteration = list(combinations(li, i))
    for j in iteration:
        profit = 0
        for l in range(n):
            count = 0
            for k in j:
                if f[l][k]==1:
                    count += 1
            profit += p[l][count]
        pro_count = max(pro_count, profit)

print(pro_count)


import os
import socket
import boto3
import pytest
from moto import mock_elbv2
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import src.index as index
from unittest.mock import patch

@mock_elbv2
@patch('socket.gethostbyname_ex')
def test_handler(mock_gethostbyname_ex):
    # Define your environment variables
    os.environ['RDS_ENDPOINT'] = "rds-endpoint"
    os.environ['RDS_PORT'] = "1433"
    os.environ['AWS_DEFAULT_REGION'] = 'us-east-1'

    # Mock the RDS endpoint
    mock_gethostbyname_ex.return_value = ('', [], ['10.0.2.46', '10.0.2.47'])

    # Create an elbv2 client and create a target group
    client = boto3.client('elbv2')
    response = client.create_target_group(
        Name='my-targets',
        Protocol='TCP',
        Port=1433,
        VpcId='vpc-3ac0fb5f',
        HealthCheckProtocol='TCP',
        HealthCheckPort='1433'
    )
    assert response['ResponseMetadata']['HTTPStatusCode'] == 200

    os.environ['TARGET_GROUP_ARN'] = response['TargetGroups'][0]['TargetGroupArn']

    # Register targets
    response = client.register_targets(
        TargetGroupArn=os.environ['TARGET_GROUP_ARN'],
        Targets=[
            {
                'Id': '10.0.2.44',
                'Port': 1433
            },
            {
                'Id': '10.0.2.45',
                'Port': 1433
            },
        ]
    )
    assert response['ResponseMetadata']['HTTPStatusCode'] == 200

    # Mock an event and context, call the handler
    event = {}  # Mock event
    context = {}  # Mock context
    index.handler(event, context)

    # Check that the old targets were deregistered and new targets registered
    response = client.describe_target_health(TargetGroupArn=os.environ['TARGET_GROUP_ARN'])
    target_ips = [target['Target']['Id'] for target in response['TargetHealthDescriptions']]
    assert '10.0.2.44' not in target_ips
    assert '10.0.2.45' not in target_ips
    assert '10.0.2.46' in target_ips
    assert '10.0.2.47' in target_ips


import os
import time
import unittest
import logging
import tempfile
import logging_plugins
import logging_plugins.cli as cli

class TestCheckLogDump(unittest.TestCase):
    level = logging.DEBUG

    def setUp(self):
        self.logger = logging.getLogger()
        self.logger.setLevel(self.level)
        self.handler = self._make_handler()
        self.logger.handlers = []
        self.logger.addHandler(self.handler)

    def _make_handler(self):
        self.filename = tempfile.mktemp()
        return logging_plugins.DumpOnSignalCounterHandler(
            filename=self.filename, format="json"
        )

    def test_without_rules(self):
        ret = cli.check_log_dump(os.getpid(), self.filename)
        assert ret == 0

    def test_unmodified_file(self):
        ret = cli.check_log_dump(os.getpid(), self.filename + "foobar")
        assert ret == 2

    def test_rule_count(self):
        ret = cli.check_log_dump(os.getpid(), self.filename, "SIGUSR2", "info count gt 2")
        assert ret == 3
        self.logger.info("Foo")
        self.logger.info("Bar")
        self.logger.info("Three")
        ret = cli.check_log_dump(os.getpid(), self.filename, "SIGUSR2", "info count gt 2", "any count lt 1")
        assert ret == 4

    def test_rule_last_record(self):
        self.logger.info("Foo")
        ret = cli.check_log_dump(os.getpid(), self.filename, "SIGUSR2", "info last_record gt -2")
        assert ret == 0
        time.sleep(1)
        ret = cli.check_log_dump(os.getpid(), self.filename, "SIGUSR2", "any last_record gt -1.5")
        assert ret == 3


x = int(input())
y = int(input())

if ((-1000 <= x <= 1000 and x!=0) and (-1000 <= y <= 1000 and y!=0)):
    if (x > 0 and y > 0):
        print("1")
    elif (x < 0 and y > 0):
        print("2")
    elif (x < 0 and y < 0):
        print("3")
    else:
        print("4")
else:
    print("0이 아닌 -1000에서 1000 사이의 값을 입력하세요")


def valueEqualtoIndex(arr):
  res = []

  for i in range(len(arr)):
    if arr[i] == i+1:
      res.append(arr[i])

  return res

# print(valueEqualtoIndex([15, 2, 45, 12, 7]))

# print(max([1, 2, 3, 4, 45, 1]))


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

from keras_uncertainty.losses import regression_gaussian_nll_loss, regression_gaussian_beta_nll_loss
import numpy as np
import matplotlib.pyplot as plt

from keras.layers import Dense, Input, Flatten

import tensorflow as tf

from tensorflow import keras
from tensorflow.keras.models import Model

import keras_uncertainty.backend as K
from keras_uncertainty.models import TwoHeadStochasticRegressor, StochasticRegressor
from keras_uncertainty.layers import DropConnectDense, StochasticDropout

tf.compat.v1.disable_eager_execution()

# Losses commonly used in uncertainty quantification and probabilistic forecasting

def regression_gaussian_nll_loss(variance_tensor, epsilon=1e-8, variance_logits=False):
    """
        Gaussian negative log-likelihood for regression, with variance estimated by the model.
        This function returns a keras regression loss, given a symbolic tensor for the sigma square output of the model.
        The training model should return the mean, while the testing/prediction model should return the mean and variance.
    """
    def nll(y_true, y_pred):
        #if variance_logits:
        #    variance_tensor = K.exp(variance_tensor)

        return 0.5 * K.mean(K.log(variance_tensor + epsilon) + K.square(y_true - y_pred) / (variance_tensor + epsilon))

    return nll

def train_standard_model(x_train, y_train):
    """

    :param x_train:
    :param y_train:
    :return:
    """

    prob = 0.05
    inp = Input(shape=(1,))
    x = DropConnectDense(32, activation="relu", prob=prob)(inp)
    x = DropConnectDense(32, activation="relu", prob=prob)(x)
    mean = DropConnectDense(1, activation="linear")(x)
    var = DropConnectDense(1, activation="softplus")(x)

    train_model = Model(inp, mean)
    pred_model = Model(inp, [mean, var])

    opt = keras.optimizers.Adam(learning_rate=0.1)

    train_model.compile(loss=regression_gaussian_nll_loss(var), optimizer=opt)
    train_model.fit(x_train, y_train, verbose=2, epochs=100)

    return train_model, pred_model

def evaluate_model(domain, pred_model):
    # model taken from given repository: returns the mean and std (two heads)
    model = TwoHeadStochasticRegressor(pred_model)

    # predict on the same model 20 times and disentangle the uncertainties
    # where aleatoric uncertainty = mean of the variances from each predicted model (previously trained)
    # and epistemic uncertainty = variance of the means from each predicted model (previously trained)
    mean_pred, epi_std, ale_std = model.predict(domain, 20, disentangle_uncertainty=True)

    return mean_pred, epi_std, ale_std

# generating big and small datasets
X = np.clip(np.random.normal(0.0, 1.0, 100).reshape(-1,1), -3, 3)

# let us generate a grid to check how models fit the data
x_grid = np.linspace(-5, 5, 100).reshape(-1,1)

# defining the function - noisy
# function adapted from : https://www.kaggle.com/code/gdmarmerola/risk-and-uncertainty-in-deep-learning/notebook
noise = lambda x: (x**2)/10
target_toy = lambda x: (x + 0.3*np.sin(2*np.pi*(x + noise(x)) +
                        0.3*np.sin(4*np.pi*(x + noise(x)) +
                        noise(x) - 0.5)))

# defining the function - no noise
target_toy_noiseless = lambda x: (x + 0.3*np.sin(2*np.pi*(x)) + 0.3*np.sin(4*np.pi*(x)) - 0.5)

# runnning the target
y = np.array([target_toy(e) for e in X])
y_noiseless = np.array([target_toy_noiseless(e) for e in x_grid])

# check the toy data
plt.figure(figsize=[12,6], dpi=200)

# return trained model and predictive model
train_model, pred_model =train_standard_model(X, y)

# predict the model on data and return epistemic and aleatoric noise
predicted_mean, epi, ale = evaluate_model(x_grid, pred_model)

# reshape the vectors
y_pred_mean = predicted_mean.reshape((-1,))
y_epi = epi.reshape((-1,))
y_ale =ale.reshape((-1,))

# predictive uncertainty is the sum of epistemic and aleatoric
y_pred_std = y_epi + y_ale

#  calculate lower and upper bounds
y_pred_up_1 = y_pred_mean + y_pred_std
y_pred_down_1 = y_pred_mean - y_pred_std

plt.figure(figsize=[12,6], dpi=200)

# plot aleatoric uncertainty with the predicted mean
plt.plot(X, y, 'kx', label='Toy data', alpha=0.5, markersize=5)
plt.plot(x_grid, predicted_mean, label='neural net fit', color='tomato', alpha=0.8)
plt.fill_between (x_grid.reshape(1,-1)[0], y_pred_mean-y_ale, y_pred_mean+y_ale, alpha=0.2, label="Aleatoric Uncertainty", color='orange')
plt.title('Neural network fit for median expected value with aleatoric uncertainty')
plt.xlabel('$x$'); plt.ylabel('$y$')
plt.xlim(-3.5,3.5); plt.ylim(-5, 3)
plt.legend()
plt.show()

# plot epistemic uncertainty with the predicted mean
plt.plot(X, y, 'kx', label='Toy data', alpha=0.5, markersize=5)
plt.plot(x_grid, predicted_mean, label='neural net fit', color='tomato', alpha=0.8)
plt.fill_between (x_grid.reshape(1,-1)[0], y_pred_mean-y_epi, y_pred_mean+y_epi, alpha=0.2, label="Epistemic Uncertainty", color='orange')
plt.title('Neural network fit for median expected value with epistemic uncertainty')
plt.xlabel('$x$'); plt.ylabel('$y$')
plt.xlim(-3.5,3.5); plt.ylim(-5, 3)
plt.legend()
plt.show()

# plot predictive uncertainty with the predicted mean
plt.plot(X, y, 'kx', label='Toy data', alpha=0.5, markersize=5)
plt.plot(x_grid, predicted_mean, label='neural net fit', color='tomato', alpha=0.8)
plt.fill_between (x_grid.reshape(1,-1)[0], y_pred_mean-y_pred_std , y_pred_mean+y_pred_std , alpha=0.2, label="Predictive Uncertainty", color='orange')
plt.title('Neural network fit for median expected value with predictive uncertainty')
plt.xlabel('$x$'); plt.ylabel('$y$')
plt.xlim(-3.5,3.5); plt.ylim(-5, 3)
plt.legend()
plt.show()


from locale import normalize
from typing import Union
import torchaudio
import torch
import librosa
from omegaconf import DictConfig, OmegaConf
import logging
import os
import numpy as np

logger = logging.getLogger(__name__)

class AudioProcessor:
    def __init__(self,
                 config : DictConfig,
                 custom_feature_extractor : callable = None):
        self.config = config
        logger.info("Init AudioProcessor with parameters: " +
                    str(OmegaConf.to_container(self.config)))

        if custom_feature_extractor is not None:
            assert callable(custom_feature_extractor)
            logger.info("Overriding configuration with custom feature extractor")
            self.feature_extractor = custom_feature_extractor

        valid_features = ['spectrogram', 'melspectrogram', 'cqt', 'chroma_stft']
        if self.config.feature not in valid_features:
            raise ValueError("Invalid Feature: "+ str(self.config.feature))

        if self.config.feature == 'spectrogram':
            if self.config.loader == "torchaudio":
                self.feature_extractor = torchaudio.transforms.Spectrogram(
                    n_fft=self.config.spectrogram_params.n_fft,
                    pad_mode="reflect", # TODO: add config
                    center=True,  # TODO: add config
                    win_length=self.config.spectrogram_params.win_length,
                    hop_length=self.config.spectrogram_params.hop_length
                )
        elif self.config.feature == 'melspectrogram':
            if self.config.loader == "torchaudio":
                self.feature_extractor = torchaudio.transforms.MelSpectrogram(
                    sample_rate=self.config.sr,
                    center=True, # TODO: add config
                    n_fft=self.config.melspectrogram_params.n_fft,
                    win_length=self.config.melspectrogram_params.win_length,
                    hop_length=self.config.melspectrogram_params.hop_length,
                    n_mels=self.config.melspectrogram_params.n_mels,
                    f_min=self.config.melspectrogram_params.f_min,
                    f_max=self.config.melspectrogram_params.f_max
                )
        elif self.config.feature == 'cqt':
            def _cqt(x):
                return np.abs(
                    librosa.cqt(
                        np.array(x),
                        sr=self.config.cqt_params.sr,
                        hop_length=self.config.cqt_params.hop_length,
                        n_bins=self.config.cqt_params.n_bins,
                        bins_per_octave=self.config.cqt_params.bins_per_octave
                    )
                )
            self.feature_extractor = _cqt
        elif self.config.feature == 'chroma_stft':
            def _chroma_stft(x):
                return np.abs(
                    librosa.feature.chroma_stft(
                        np.array(x),
                        sr=self.config.chroma_stft_params.sr,
                        n_fft=self.config.chroma_stft_params.n_fft,
                        n_chroma=self.config.chroma_stft_params.n_chroma,
                        hop_length=self.config.chroma_stft_params.hop_length
                    )
                )
            self.feature_extractor = _chroma_stft
        else:
            raise NotImplementedError

    def wav2feature(self, x) -> Union[np.ndarray, torch.Tensor]:
        feature = self.feature_extractor(x)
        return feature

    def get_feature_from_audio_path(
        self, path: Union[os.PathLike, str]
    ) -> Union[np.ndarray, torch.Tensor]:
        return self.wav2feature(self.load_wav(path))

    def get_feature_from_audio(
        self, wav: Union[np.ndarray, torch.Tensor]
    ) -> Union[np.ndarray, torch.Tensor]:
        return self.wav2feature(wav)

    def load_wav(
        self,
        path: Union[os.PathLike, str],
        start_sec: float = None,
        duration_sec: float = None
    ) -> torch.Tensor:
        if self.config.loader == "torchaudio":
            if self.config.normalize:
                raise NotImplementedError
            if start_sec is not None or duration_sec is not None:
                sr = torchaudio.info(path).sample_rate
                wav, sr = torchaudio.load(
                    path,
                    normalize=True,  # TODO: add config
                    frame_offset=int(start_sec*sr),
                    num_frames=int(duration_sec*sr)
                )
                wav = torch.mean(wav, dim=0) # TODO: add config and move to transforms
            else:
                wav, sr = torchaudio.load(
                    path,
                    normalize=True,  # TODO: add config
                )
            if self.config.resample and sr != self.config.sr:
                resample = torchaudio.transforms.Resample(sr, self.config.sr) # TODO: add config and move to transforms
                wav = resample(wav)
        elif self.config.loader == "librosa":
            wav, sr = librosa.load(
                path,
                mono=True,
                sr=self.config.sr if self.config.resample else None,
                offset=start_sec,
                duration=duration_sec
            )
            if self.config.normalize:
                wav = librosa.util.normalize(wav)
            wav = torch.from_numpy(wav)
        else:
            raise NotImplementedError
        return wav


# 8. Reimplement the class of the preceding exercise in Java,
# JavaScript, Python, C#, or C++ (your choice). How much shorter
# is the Scala class?

class Car:

    def __init__(self, manufacturer, model_name, model_year = None, license_plate = None):
        self.manufacturer = manufacturer
        self.model_name = model_name
        self.model_year = model_year if model_year else -1
        self.license_plate = license_plate if license_plate else ''

if __name__ == '__main__':

    c1 = Car(manufacturer='Ford', model_name='F-150')
    c2 = Car(manufacturer='KIA', model_name='Sorento', model_year=2015)
    c3 = Car(manufacturer='Honda', model_name='Civic', license_plate='HSL2323')
    c4 = Car('Tesla', 'M3', 2023, 'GB7SD8')

    assert c1.model_year == -1
    assert c2.license_plate == ''
    assert c3.model_year == -1


import os, json, boto3, base64
from jsonschema import validate
from boto3.dynamodb.conditions import Key
from datetime import datetime, timezone

chats_table = boto3.resource('dynamodb').Table(os.environ.get('DDB_CHATS'))
advertisements_table = boto3.resource('dynamodb').Table(os.environ.get('DDB_AD'))

with open('schemas/message_post.json','r') as file:
    schema_post = json.load(file)

def get_chat(event, context):
    try:
        title = event['pathParameters']['title'].replace('%20', ' ')
    except KeyError:
        return {"statusCode": 400, "body": "No se ha indicado anuncio"}

    ann = advertisements_table.query(KeyConditionExpression=Key('title').eq(title)).get("Items")[0]
    if not ann:
        return {"statusCode": 404, "body": f"No se ha encontrado el anuncio {title}"}

    if 'user' in event['pathParameters']:
        owner = ann['owner']
        user = event['requestContext']['authorizer']['claims']['email']
        chat_id = title+'/'+event['pathParameters']['user']
        if not (user == owner or user == event['pathParameters']['user']):
            return {"statusCode": 403, "body": f"Usuario no autorizado en el chat"}
    else:
        chat_id = title

    chat = chats_table.query(KeyConditionExpression=Key('chat_id').eq(chat_id))
    messages = [{'fecha_hora': x['fecha_hora'], 'user_id': x['user_id'], 'message': x['message']} for x in chat.get("Items", [])]
    if not messages:
        return {"statusCode": 404, "body": "No messages found"}
    else:
        return {"statusCode": 200, "body": json.dumps(messages)}

def send_message(event, context):

    user = event['requestContext']['authorizer']['claims']['email']
    try:
        title = event['pathParameters']['title'].replace('%20', ' ')
    except KeyError:
        return {"statusCode": 400, "body": "No se ha indicado anuncio"}

    ann = advertisements_table.query(KeyConditionExpression=Key('title').eq(title)).get("Items")[0]
    if not ann:
        return {"statusCode": 404, "body": f"No se ha encontrado el anuncio {title}"}

    owner = ann['owner']

    if 'user' in event['pathParameters']:
        chat_id = title+'/'+event['pathParameters']['user']
        if not (user == owner or user == event['pathParameters']['user']):
            return {"statusCode": 403, "body": f"Usuario no autorizado en el chat"}
    else:
        chat_id = title

    message = json.loads(event.get('body', '{}'))
    if not message:
        return {"statusCode": 400, "body": "No se ha indicado body"}

    validate(instance=message, schema=schema_post)

    item = {
            'chat_id': chat_id,
            'fecha_hora': datetime.utcnow().replace(tzinfo=timezone.utc).isoformat(),
            'user_id': user,
            'message': message['message'],
        }
    chats_table.put_item(Item=item)
    return {"statusCode": 201, "body": f"Posted in {chat_id}" }


from turtle import position
from django.db import models
from django.contrib.auth.models import User

class job(models.Model):
    """
    - Position name
    - Text Description
    - Age Criteria
    - Salary
    - No. of openings
    """
    position_name =models.CharField(max_length=100)
    text_description = models.TextField()
    min_age =models.IntegerField()
    max_age =models.IntegerField()
    salary =models.IntegerField()
    no_openongs =models.IntegerField()
    create= models.ForeignKey(User, on_delete= models.CASCADE)

    def __str__(self):
        return self.position_name


import os
import sys
sys.path.insert(0, '../sim')
import sim_analyze_hmm_bw
sys.path.insert(0, '..')
import global_params as gp

def gen_input_file(sequence_fn, fn, tag, rep):
    """
    Initial mode:
    0) Build a new model.
    1) Load a pre-existing model.
    2) Exit.
    Choose an option:
    0
    Input the basic file info path name:
    (note: see README for file format)

    basic-info.txt
    Input the number of trees or states for this HMM:
    6

    Input the parental trees file path name:
    (note: see README for file format)

    parental.trees

    Input the gene genealogies file path name:
    (note: see README for file format)

    gene.trees

    Input outgroup taxon name, or empty string for no outgroup taxon:

    OUTGROUP
    Empty working directory:
    ./d
    Input non-zero substitution model rates in format <AG> <AC> <AT> <GC> <GT>:
    0.964011 0.192217 0.270592 0.0806232 0.174283
    Input non-zero substitution model base frequencies in format <A> <G> <C> <T>:
    0.1953 0.3077 0.3008 0.1962

    Across-row switching frequency gamma:
    .1
    Hidden state switching frequency ratio term input file:
    switching-frequency-ratio-terms

    Operate mode:
    0) Run Viterbi
    1) Learn Model using Baum Welch.
    3) Learn Model using a multivariate optimization heuristic that incorporates Brent's method
    4) Exit
    Choose an option:
    0

    Path to your output file:
    initial.viterbi.sequence
    Which observation sequence would you like to use?
    0) Reuse previously read sequence.
    1) Load a new observation sequence.
    Choose an option:
    1
    Keep or discard parsimony-uninformative sites? true for keep, false for discard:
    false
    Input observation sequence file path name :
    sequence_nogaps.fasta
    Input alignment length: 86706
    Alignment length after optional filter step: 8194
    Building Psy Array
    100% Done!

    Total: 29 MiB; Free: 22 MiB; --> Used: 7 MiB.
    Max: 454 MiB.
    Begin computing Viterbi's algorithm : FORWARD PART
    100% DONE!
    Enlarging... capacity = 10000
    Begin computing Viterbi's algorithm : BACKWARD PART
    100% DONE!
    Input HMM Viterbi log likelihood: |-21.866065387121736|
    Computing input HMM log likelihood for input sequences...
    Computing input HMM log likelihood for input sequences DONE.
    Input HMM log likelihood: |-21.310316817591303|
    Operate mode:
    0) Run Viterbi
    1) Learn Model using Baum Welch.
    3) Learn Model using a multivariate optimization heuristic that incorporates Brent's method
    4) Exit
    Choose an option:

    Which observation sequence would you like to use?
    0) Reuse previously read sequence.
    1) Load a new observation sequence.
    Choose an option:
    Input parental-branch-length-parameter-to-edge map filename:
    Input parental-branch-length-parameter strict inequalities filename:
    Input length-parameter-set-constraints filename:
    Input checkpoint file to restore from, or empty line for no restore:
    Output posterior decoding probabilities file:
    Output Viterbi-optimal hidden state sequence file:
    Output model likelihoods file:
    Output file with optimized model parameter values:
    Initial search settings vector <setting 1> <setting 2> ... <setting s>, where <setting s> is one of CURRENT, DEFAULT, RANDOM

    Input parental-branch-length-parameter-to-edge map filename:
    Input parental-branch-length-parameter strict inequalities filename:
    Input length-parameter-set-constraints filename:
    Input checkpoint file to restore from, or empty line for no restore:
    Output posterior decoding probabilities file:
    Output Viterbi-optimal hidden state sequence file:
    Output model likelihoods file:
    Output file with optimized model parameter values:
    Initial search settings vector <setting 1> <setting 2> ... <setting s>, where <setting s> is one of CURRENT, DEFAULT, RANDOM
    Enable optimization flag vector <enable parental tree optimization flag> <enable gene genealogy optimization flag> <enable switching frequency optimization flag> <enable substitution model optimization flag>
    0) Run Viterbi
    1) Learn Model using Baum Welch.
    3) Learn Model using a multivariate optimization heuristic that incorporates Brent's method
    4) Exit
    Choose an option:
    """

    initial_mode = '0' # build new model
    basic_info_fn = 'basic-info.txt'
    num_states = '6' # num parental trees * num genealogies
    parental_trees_fn = 'parental.trees'
    gene_trees_fn = 'gene.trees'
    outgroup_name = 'OUTGROUP'
    working_dir = '../../results/sim/phylo-hmm/working/' + tag # don't need one for every rep because we do one at a time
    os.mkdir(working_dir)
    substitution_rates = '1 1 1 1 1' # <AG> <AC> <AT> <GC> <GT>
    base_frequencies = '0.25 0.25 0.25 0.25' # <A> <G> <C> <T>
    parental_tree_switching_freq = '.1'
    gene_tree_switching_fn = 'switching-frequency-ratio-terms'
    operate_mode = '0' # run viterbi
    # operate_mode = '1' # learn with Baum-Welch
    output_file_path = '../../results/sim/phylo-hmm/initial.viterbi.sequence.' + tag + '.' + rep
    observation_sequence_option = '1' # read new sequence
    keep_uninformative_sites = 'true' # referred to as "optional filter step" later
    #sequence_fn = 'sequence.fasta'
    operate_mode_2 = '3' # learn with "a multivariate optimization heuristic that incorporates Brent's method"
    observation_sequence_option_2 = '0' # reuse previously read sequence
    length_params_fn = 'length-parameters'
    length_params_inequality_constraints_fn = 'length-parameter-inequality-constraints'
    length_params_constraint_sets_fn = 'length-parameter-constraint-sets'
    restore_fn = '' # blank for no file
    output_posterior_decoding_fn = '../../results/sim/phylo-hmm/optimized.posterior.decoding.probabilities.' + tag + '.' + rep
    output_viterbi_optimized_fn = '../../results/sim/phylo-hmm/optimized.viterbi.sequence.'  + tag + '.' + rep
    output_model_likelihoods_fn = '../../results/sim/phylo-hmm/optimized.model.likelihoods.'  + tag + '.' + rep
    output_optimized_params_fn = '../../results/sim/phylo-hmm/optimized.model.parameters.' + tag + '.' + rep
    initial_search_settings = 'CURRENT DEFAULT'
    enable_optimization = 'true true true true' # <enable parental tree optimization flag> <enable gene genealogy optimization flag> <enable switching frequency optimization flag> <enable substitution model optimization flag>
    operate_mode_3='4' # exit

    f = open(fn, 'w')
    f.write(initial_mode + '\n' + \
                basic_info_fn + '\n' + \
                num_states + '\n' + \
                parental_trees_fn + '\n' + \
                gene_trees_fn + '\n' + \
                outgroup_name + '\n' + \
                working_dir + '\n' + \
                substitution_rates + '\n' + \
                base_frequencies + '\n' + \
                parental_tree_switching_freq + '\n' + \
                gene_tree_switching_fn + '\n' + \
                operate_mode + '\n' + \
                output_file_path + '\n' + \
                observation_sequence_option + '\n' + \
                keep_uninformative_sites + '\n' + \
                sequence_fn + '\n' + \
                operate_mode_2 + '\n' + \
                observation_sequence_option_2 + '\n' + \
                length_params_fn + '\n' + \
                length_params_inequality_constraints_fn + '\n' + \
                length_params_constraint_sets_fn + '\n' + \
                restore_fn + '\n' + \
                output_posterior_decoding_fn + '\n' + \
                output_viterbi_optimized_fn + '\n' + \
                output_model_likelihoods_fn + '\n' + \
                output_optimized_params_fn + '\n' + \
                initial_search_settings + '\n' + \
                enable_optimization + '\n' + \
                operate_mode_3)


from ase.build import fcc111, add_adsorbate
from ase.visualize import view
from ase import Atoms
from ase.constraints import FixAtoms
from ase.constraints import FixBondLength
from ase.calculators.emt import EMT
from ase.io import read
from ase.optimize import *
from catlearn.optimize.mlmin import MLMin

"""
    Structure relaxation using constraints. CO on an Al(111) surface.
    Fixed bond length constraint.
    Benchmark using MLMin, GPMin, LBFGS and FIRE.
"""

# 1. Build Atoms Object.
###############################################################################

# Setup calculator:
calc = EMT()

# Create CO molecule:
d = 1.1
co = Atoms('CO', positions=[(0, 0, 0), (0, 0, d)])

# Create slab:
slab = fcc111('Al', size=(2,2,3), vacuum=10.0)
slab = fcc111('Al', size=(2,2,3))

# Add CO on the slab:
add_adsorbate(slab, co, 2., 'bridge')
slab.center(vacuum=10.0, axis=2)

# Set constraints:
c1 = FixAtoms(indices=[atom.index for atom in slab if atom.symbol == 'Al'])
c2 = FixBondLength(12, 13)
slab.set_constraint([c1, c2])

atoms = slab.copy()

# 2. Benchmark.
###############################################################################

# 2.A. Optimize structure using MLMin (CatLearn).
initial_mlmin = atoms.copy()
initial_mlmin.set_calculator(calc)
mlmin_opt = MLMin(initial_mlmin, trajectory='results_catlearn.traj')
mlmin_opt.run(fmax=0.01, kernel='SQE')

final_atoms = read('results_catlearn.traj', ':')

# 2.B Optimize using GPMin.
initial_gpmin = atoms.copy()
initial_gpmin.set_calculator(calc)
gpmin_opt = GPMin(initial_gpmin, trajectory='results_gpmin.traj',
                  update_hyperparams=True)
gpmin_opt.run(fmax=0.01)

# 2.C Optimize using LBFGS.
initial_lbfgs = atoms.copy()
initial_lbfgs.set_calculator(calc)
lbfgs_opt = BFGS(initial_lbfgs, trajectory='results_lbfgs.traj')
lbfgs_opt.run(fmax=0.01)

# 2.D Optimize using FIRE.
initial_fire = atoms.copy()
initial_fire.set_calculator(calc)
fire_opt = FIRE(initial_fire, trajectory='results_fire.traj')
fire_opt.run(fmax=0.01)

# 3. Summary of the results:
###############################################################################

print('\n Summary of the results:\n ------------------------------------')

fire_results = read('results_fire.traj', ':')
print('Number of function evaluations using FIRE:',
      len(fire_results))

lbfgs_results = read('results_lbfgs.traj', ':')
print('Number of function evaluations using LBFGS:',
      len(lbfgs_results))

gpmin_results = read('results_gpmin.traj', ':')
print('Number of function evaluations using GPMin:', gpmin_opt.function_calls)

catlearn_results = read('results_catlearn.traj', ':')
print('Number of function evaluations using MLMin (CatLearn):',
      len(catlearn_results))


balance = 0

def deposit(balance):
    amount = float(input("Enter amount to be Deposited: "))
    balance += amount
    print("\n Amount Deposited:", amount)

def withdraw(balance):
    amount = float(input("Enter amount to be Withdrawn: "))
    if balance >= amount:
        balance -= amount
        print("\n You Withdrew:", amount)
    else:
        print("\n Insufficient balance  ")

def display(balance):
    print("\n Net Available Balance=", balance)

dp = deposit()
print(dp(balance))

wd = withdraw()
print(wd(balance))

d = display()
print(d(balance))


import matplotlib.pyplot as plt

testAccuracy = []
trainAccuracy = []
iterations = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]

trainSize = [39, 79, 119, 159, 199, 239, 279, 319, 359]

varied = 'variedIteration'
algorithms = ['RHC', 'SA', 'GA']
dataset = ['train', 'test']
figure = 0

fig1 = plt.figure(1)
fig1.suptitle("Train Accuracy vs. Iterations (Overall)")
fig2 = plt.figure(2)
fig2.suptitle("Test Accuracy vs. Iterations (Overall)")

for algo in algorithms:
    sourceFileTrain = '../Assignment2/ABAGAIL/src/opt/test/myTests/outputData/' + str(varied) + '/' + str(
        algo) + '/' + str(algo) + '_' + '' + 'trainAccuracy_70.txt'
    sourceFileTest = '../Assignment2/ABAGAIL/src/opt/test/myTests/outputData/' + str(varied) + '/' + str(
        algo) + '/' + str(algo) + '_' + '' + 'testAccuracy_70.txt'
    with open(sourceFileTrain) as f:
        data = f.read()
    accuracy = [float(value) for value in data.split("\n") if value]

    plt.figure(1)
    plt.plot(iterations, accuracy, label=algo, lw=2.0)
   # plt.axis([np.min(iterations), np.max(iterations), 0, 100])

    with open(sourceFileTest) as f:
        data = f.read()
    accuracy = [float(value) for value in data.split("\n") if value]

    plt.figure(2)
    plt.plot(iterations, accuracy, label=algo, lw=2.0)
    #plt.axis([np.min(iterations), np.max(iterations), 0, 100])

sourceFileTrain = '../Assignment2/ABAGAIL/src/opt/test/myTests/outputData/' + 'backpropTrain.txt'
sourceFileTest = '../Assignment2/ABAGAIL/src/opt/test/myTests/outputData/' + 'backpropTest.txt'
with open(sourceFileTrain) as f:
    data = f.read()
trainAccuracy = [float(value)*100 for value in data.split("\n") if value]
plt.figure(1)
plt.plot(iterations, trainAccuracy, label="backpropagation", lw=2.0)

plt.xlabel("iteration")
plt.ylabel("Classification Accuracy")
plt.legend(loc='lower right')

with open(sourceFileTest) as f:
    data = f.read()
testAccuracy = [float(value)*100 for value in data.split("\n") if value]

plt.figure(2)
plt.plot(iterations, testAccuracy, label="backpropagation", lw=2.0)

plt.xlabel("iteration")
plt.ylabel("Classification Accuracy")
plt.legend(loc='lower right')

fig1.savefig(
    '../Assignment2/ABAGAIL/src/opt/test/myTests/outputGraphs/' + "trainAccuracy" + '_' + 'overall' + '_' + "_plot" + '.png',
    dpi=300)
fig2.savefig(
    '../Assignment2/ABAGAIL/src/opt/test/myTests/outputGraphs/' + 'testAccuracy' + '_' + 'overall' + '_' + "_plot" + '.png',
    dpi=300)
# plt.show()


def anagram(str1, str2):

    str1 = str1.replace(' ','').lower()
    str2 = str2.replace(' ','').lower()

    counts = {}

    for st in str1:
        if st in counts:
            counts[st] += 1
        else:
            counts[st] = 1

    for st in str2:
        if st in counts:
            counts[st] -= 1
        else:
            counts[st] = 1

    for count in counts:
        if counts[count] != 0:
            return False

    return True

if __name__ == '__main__':
    str1 = input()
    str2 = input()

    print(anagram(str1, str2))


from django.urls import path, include
from . import views

urlpatterns = [
    path('lista_autos/', views.lista_autos, name="lista_autos"),
    path('lista_motos/', views.lista_moto, name="lista_motos"),
    path('detalle_moto/<int:id>/', views.detalle_moto, name="detalle_moto"),
    path('detalle_auto/<int:id>/', views.detalle_auto, name="detalle_auto"),
    path('buscar_auto/', views.busquedaAuto, name="buscar_auto")
]


# link: https://github.com/uci-cbcl/NoduleNet
import sys
sys.path.append('./')
from pylung.annotation import *
from pylung.utils import *
from tqdm import tqdm
import sys
import nrrd
import SimpleITK as sitk
import cv2
import glob
import matplotlib.pyplot as plt

def load_itk_image(filename):
    """Return img array and [z,y,x]-ordered origin and spacing
    """

    itkimage = sitk.ReadImage(filename)
    numpyImage = sitk.GetArrayFromImage(itkimage)

    numpyOrigin = np.array(list(reversed(itkimage.GetOrigin())))
    numpySpacing = np.array(list(reversed(itkimage.GetSpacing())))

    return numpyImage, numpyOrigin, numpySpacing

def get_pos(img_mask):
    index = []
    for i in range(len(img_mask)):
        if 1 in img_mask[i]:
            index.append(i)
    return index

def nodule_mask(luna_path, ctr_path):

    img, origin, spacing = load_itk_image(luna_path)
    ctr_arrs = np.load(ctr_path, allow_pickle=True)

    mask = np.zeros(img.shape)
    for ctr_arr in ctr_arrs:
        z_origin = origin[0]
        z_spacing = spacing[0]
        ctr_arr = np.array(ctr_arr)
        ctr_arr[:, 0] = np.absolute(ctr_arr[:, 0] - z_origin) / z_spacing
        ctr_arr = ctr_arr.astype(np.int32)

        for z in np.unique(ctr_arr[:, 0]):
            ctr = ctr_arr[ctr_arr[:, 0] == z][:, [2, 1]]
            ctr = np.array([ctr], dtype=np.int32)
            mask[z] = cv2.fillPoly(mask[z], ctr, color=(1,) * 1)

    index = get_pos(mask)

    return mask, index

if __name__ == "__main__":
    luna_path = "../../../luna16/LUNA16/subset1/1.3.6.1.4.1.14519.5.2.1.6279.6001.287966244644280690737019247886.mhd"
    ctr_path = "../../../luna16/annotation/mask_test/1.3.6.1.4.1.14519.5.2.1.6279.6001.287966244644280690737019247886.npy"
    nodule_mask(luna_path, ctr_path)


import requests
from youtube_dl import YoutubeDL

# By Feh's

YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist': 'True'}

def search(arg):
    with YoutubeDL(YDL_OPTIONS) as ydl:
        try:
            requests.get(arg)
        except:
            video = ydl.extract_info(f"ytsearch:{arg}", download=False)['entries'][0]
        else:
            video = ydl.extract_info(arg, download=False)

    return video['webpage_url']

info = search(input('Nome da música: ').replace(' ', '+'))

print(info)

#By Ota's

import youtube_dl

ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download([info])


# 4. Write a python program to init default values for user object using __init__ method.

# creating a class
class User:
    name = None      # class variable
    roll = None

    def __init__(self):
        self.name = "Rishabh"         # instance variable
        self.roll = 21
        print("Name=", self.name)
        print("Roll=", self.roll)

# creating an object
obj = User()


import dataclasses
import logging
import os
import sys
from dataclasses import dataclass, field
from typing import Callable, Dict, Optional

import numpy as np

from transformers import AutoConfig, AutoModelForSequenceClassification, AutoTokenizer, EvalPrediction, GlueDataset
from transformers import GlueDataTrainingArguments as DataTrainingArguments
from transformers import (
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    #glue_compute_metrics,
    glue_output_modes,
    glue_tasks_num_labels,
    set_seed,
)
from metrics import glue_compute_metrics

logger = logging.getLogger(__name__)

@dataclass
class ModelArguments:
    """
    Arguments pertaining to which model/config/tokenizer we are going to fine-tune from.
    """

    model_name_or_path: str = field(
        metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}
    )
    config_name: Optional[str] = field(
        default=None, metadata={"help": "Pretrained config name or path if not the same as model_name"}
    )
    tokenizer_name: Optional[str] = field(
        default=None, metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}
    )
    cache_dir: Optional[str] = field(
        default=None, metadata={"help": "Where do you want to store the pretrained models downloaded from s3"}
    )
    warmup_proportion: Optional[float] = field(
        default=0.1, metadata={"help": "Proportion of training to perform linear learning rate warmup for. E.g., 0.1 = 10% of training."}
    )

def main():
    # See all possible arguments in src/transformers/training_args.py
    # or by passing the --help flag to this script.
    # We now keep distinct sets of args, for a cleaner separation of concerns.

    parser = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments))

    if len(sys.argv) == 2 and sys.argv[1].endswith(".json"):
        # If we pass only one argument to the script and it's the path to a json file,
        # let's parse it to get our arguments.
        model_args, data_args, training_args = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]))
    else:
        model_args, data_args, training_args = parser.parse_args_into_dataclasses()

    if (
        os.path.exists(training_args.output_dir)
        and os.listdir(training_args.output_dir)
        and training_args.do_train
        and not training_args.overwrite_output_dir
    ):
        raise ValueError(
            f"Output directory ({training_args.output_dir}) already exists and is not empty. Use --overwrite_output_dir to overcome."
        )

    # Setup logging
    logging.basicConfig(
        format="%(asctime)s - %(levelname)s - %(name)s -   %(message)s",
        datefmt="%m/%d/%Y %H:%M:%S",
        level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN,
    )
    logger.warning(
        "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s",
        training_args.local_rank,
        training_args.device,
        training_args.n_gpu,
        bool(training_args.local_rank != -1),
        training_args.fp16,
    )

    # Set seed
    set_seed(training_args.seed)

    try:
        num_labels = glue_tasks_num_labels[data_args.task_name]
        output_mode = glue_output_modes[data_args.task_name]
    except KeyError:
        raise ValueError("Task not found: %s" % (data_args.task_name))

    # Load tokenizer
    tokenizer = AutoTokenizer.from_pretrained(
        model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path,
        cache_dir=model_args.cache_dir,
    )

    # Get datasets
    train_dataset = (
        GlueDataset(data_args, tokenizer=tokenizer, cache_dir=model_args.cache_dir) if training_args.do_train else None
    )
    eval_dataset = (
        GlueDataset(data_args, tokenizer=tokenizer, mode="dev", cache_dir=model_args.cache_dir)
        if training_args.do_eval
        else None
    )
    test_dataset = (
        GlueDataset(data_args, tokenizer=tokenizer, mode="test", cache_dir=model_args.cache_dir)
        if training_args.do_predict
        else None
    )

    # Load pretrained model
    # Distributed training:
    # The .from_pretrained methods guarantee that only one local process can concurrently
    # download model & vocab.

    # Currently, this code do not support distributed training.
    if training_args.do_train:
        training_args.warmup_steps = int(model_args.warmup_proportion * (len(train_dataset) / training_args.per_device_train_batch_size) * training_args.num_train_epochs)
    training_args_weight_decay = 0.01
    logger.info("Training/evaluation parameters %s", training_args)

    config = AutoConfig.from_pretrained(
        model_args.config_name if model_args.config_name else model_args.model_name_or_path,
        num_labels=num_labels,
        finetuning_task=data_args.task_name,
        cache_dir=model_args.cache_dir,
    )
    model = AutoModelForSequenceClassification.from_pretrained(
        model_args.model_name_or_path,
        from_tf=bool(".ckpt" in model_args.model_name_or_path),
        config=config,
        cache_dir=model_args.cache_dir,
    )

    def build_compute_metrics_fn(task_name: str) -> Callable[[EvalPrediction], Dict]:
        def compute_metrics_fn(p: EvalPrediction):
            if output_mode == "classification":
                preds = np.argmax(p.predictions, axis=1)
            elif output_mode == "regression":
                preds = np.squeeze(p.predictions)
            return glue_compute_metrics(task_name, preds, p.label_ids)

        return compute_metrics_fn

    # Initialize our Trainer
    trainer = Trainer(
        model=model,
        args=training_args,
        train_dataset=train_dataset,
        eval_dataset=eval_dataset,
        compute_metrics=build_compute_metrics_fn(data_args.task_name),
    )

    # Training
    if training_args.do_train:
        trainer.train(
            model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path) else None
        )
        trainer.save_model()
        # For convenience, we also re-save the tokenizer to the same directory,
        # so that you can share your model easily on huggingface.co/models =)
        if trainer.is_world_master():
            tokenizer.save_pretrained(training_args.output_dir)

    # Evaluation
    eval_results = {}
    if training_args.do_eval:
        logger.info("*** Evaluate ***")

        # Loop to handle MNLI double evaluation (matched, mis-matched)
        eval_datasets = [eval_dataset]
        if data_args.task_name == "mnli":
            mnli_mm_data_args = dataclasses.replace(data_args, task_name="mnli-mm")
            eval_datasets.append(
                GlueDataset(mnli_mm_data_args, tokenizer=tokenizer, mode="dev", cache_dir=model_args.cache_dir)
            )

        for eval_dataset in eval_datasets:
            trainer.compute_metrics = build_compute_metrics_fn(eval_dataset.args.task_name)
            eval_result = trainer.evaluate(eval_dataset=eval_dataset)

            output_eval_file = os.path.join(
                training_args.output_dir, f"eval_results_{eval_dataset.args.task_name}.txt"
            )
            if trainer.is_world_master():
                with open(output_eval_file, "w") as writer:
                    logger.info("***** Eval results {} *****".format(eval_dataset.args.task_name))
                    for key, value in eval_result.items():
                        logger.info("  %s = %s", key, value)
                        writer.write("%s = %s\n" % (key, value))

            eval_results.update(eval_result)

    if training_args.do_predict:
        logging.info("*** Test ***")
        test_datasets = [test_dataset]
        if data_args.task_name == "mnli":
            mnli_mm_data_args = dataclasses.replace(data_args, task_name="mnli-mm")
            test_datasets.append(
                GlueDataset(mnli_mm_data_args, tokenizer=tokenizer, mode="test", cache_dir=model_args.cache_dir)
            )

        for test_dataset in test_datasets:
            predictions = trainer.predict(test_dataset=test_dataset).predictions
            if output_mode == "classification":
                predictions = np.argmax(predictions, axis=1)

            output_test_file = os.path.join(
                training_args.output_dir,
                f"test_results.txt"
                #f"test_results_{test_dataset.args.task_name}.txt"
            )
            if trainer.is_world_master():
                with open(output_test_file, "w") as writer:
                    logger.info("***** Test results {} *****".format(test_dataset.args.task_name))
                    writer.write("index\tprediction\n")
                    for index, item in enumerate(predictions):
                        if output_mode == "regression":
                            writer.write("%d\t%3.3f\n" % (index, item))
                        else:
                            item = test_dataset.get_labels()[item]
                            writer.write("%d\t%s\n" % (index, item))
    return eval_results

def _mp_fn(index):
    # For xla_spawn (TPUs)
    main()

if __name__ == "__main__":
    main()


# ---------- IMPORT ------------
import logging
import json
import datetime
import matplotlib
import matplotlib.pyplot as plt
# ------------------------------

# ---------- INPUT -------------
path_source = "C:/Users/Lukas/Desktop/goalAchiever.json"
# ------------------------------

# ---------- OUTPUT ------------

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

# ---------- CONFIG ------------
t = 50
# ------------------------------

# ---------- INITIAL -----------
logging.basicConfig(format='%(asctime)s [%(levelname)s] - %(message)s', datefmt='%d-%m-%y %H:%M:%S', level=logging.INFO)
datetimeFormat = "%Y-%m-%d"
list_of_datetimes = []
values = []
# ------------------------------

logging.info("Accessing plot data ...")
with open(path_source, "r") as fp:
    plotdata = json.load(fp)

for entry in plotdata[str(t)]:
    list_of_datetimes.append(datetime.datetime.strptime(entry, datetimeFormat).date())
    values.append(plotdata[str(t)][entry])

logging.info("Done. (1/2)")

logging.info("Creating plot ...")

dates = matplotlib.dates.date2num(list_of_datetimes)
matplotlib.pyplot.plot_date(dates, values, '-')
plt.axvline(x=datetime.datetime.strptime("2016-05-19", datetimeFormat).date(), color='#D3685D', label="Design change")
plt.xlabel("Time")
plt.ylabel("Share of users having a streak > " + str(t))
plt.legend()
plt.show()

logging.info("Done. (2/2)")


import torch
from torch import nn
import torch.nn.functional as F

from scipy.optimize import linear_sum_assignment
from utils.box_ops import generalized_iou, box_cxcywh_xyxy

class HungarianMatcher(nn.Module):
  def __init__(self, cost_class = 1, cost_bbox = 1, cost_giou = 1):
    super(HungarianMatcher, self).__init__()

    self.cost_class = cost_class
    self.cost_bbox = cost_bbox
    self.cost_giou = cost_giou

  def forward(self, preds, targets):
    """
    Preds is a dictonary that contains following keys:
      pred_class: [B, N, num_class]: Predicted class logits
      pred_bbox: [B, N, 4]: Predicted bouding boxes

    Target is a list of dictonary with following keys
      class: [B, N]: True labels
      bbox: [B, N, 4]: True bounding boxes
    """

    bs, num_queries = preds['pred_class'].shape[:2]

    # [B, N, num_class] -> [B*N, num_class]
    pred_class = preds['pred_class'].flatten(0, 1).softmax(-1)
    pred_bbox = preds['pred_bbox'].flatten(0, 1)

    tgt_ids = torch.cat([v['classes'] for v in targets])
    tgt_bbox = torch.cat([v['bboxes'] for v in targets])

    cost_class = -pred_class[:, tgt_ids]

    cost_bbox = torch.cdist(pred_bbox, tgt_bbox, p=1)

    cost_giou = -generalized_iou(box_cxcywh_xyxy(pred_bbox), box_cxcywh_xyxy(tgt_bbox))

    C = self.cost_class * cost_class + self.cost_bbox * cost_bbox + self.cost_giou * cost_giou

    C = C.view(bs, num_queries, -1).detach().cpu()

    sizes = [len(v['bboxes']) for v in targets]

    indices = []
    for i, c in enumerate(C.split(sizes, -1)):
      indices.append(linear_sum_assignment(c[i]))

    # indices = [linear_sum_assignment(c[i] for i, c in enumerate(C.split(sizes, -1)))]
    return [(torch.as_tensor(i, dtype=torch.int32), torch.as_tensor(j, dtype=torch.int32)) for i, j in indices]

class Criterion(nn.Module):
  def __init__(self, matcher, num_classes, eos_coef):
    super(Criterion, self).__init__()

    self.matcher = matcher
    self.num_classes = num_classes
    self.eos_coef = eos_coef
    empty_weight = torch.ones(self.num_classes + 1, device=device)
    empty_weight[-1] = self.eos_coef
    self.register_buffer('empty_weight', empty_weight)

  def _get_src_permutation_idx(self, indices):
    batch_idx = torch.cat([torch.full_like(src, i) for i, (src, _) in enumerate(indices)])
    src_idx = torch.cat([src for (src, _) in indices])
    return batch_idx, src_idx

  def _get_tgt_pernuration_idx(self, indices):
    batch_idx = torch.cat([torch.full_like(tgt, i) for i, (_, tgt) in enumerate(indices)])
    tgt_idx = torch.cat([tgt for (_, tgt) in indices])
    return batch_idx, tgt_idx

  def get_label_loss(self, outputs, targets, indices):
    pred_class = outputs['pred_class']
    idx = self._get_src_permutation_idx(indices)

    targets_class_o = torch.cat([t['classes'][J] for t, (_, J) in zip(targets, indices)])
    target_classes = torch.full(pred_class.shape[:2], self.num_classes, dtype=torch.int64, device=pred_class.device)

    target_classes[idx] = targets_class_o

    loss_ce = F.cross_entropy(pred_class.transpose(1, 2), target_classes, self.empty_weight)

    return loss_ce

  def get_bbox_loss(self, outputs, targets, indices, num_bboxes):

    idx = self._get_src_permutation_idx(indices)
    pred_bbox = pred_bbox = outputs['pred_bbox'][idx]
    tgt_bbox = torch.cat([t['bboxes'][i] for t, (_, i) in zip(targets, indices)], dim=0)

    loss_bbox = F.l1_loss(pred_bbox, tgt_bbox, reduction='none').sum() / num_bboxes
    giou_bbox = 1 - torch.diag(generalized_iou(
        box_cxcywh_xyxy(pred_bbox),
        box_cxcywh_xyxy(tgt_bbox)
    ))

    giou_loss = giou_bbox.sum() / num_bboxes
    return loss_bbox, giou_loss

  def forward(self, outputs, targets):
    """
    Arguments:
    Outputs is a dictonary containing:
      pred_class: [B, num_queries, num_classes] prediction for class
      pred_bbox: [B, num_queries, 4] prediction for bbox

    targets is a list of dictonary with elements having following keys:
      classes: [B, N] true labels
      bboxes: [B, N, 4] True bounding boxes
    """

    indices = self.matcher(outputs, targets)
    num_bboxes = sum(len(v['bboxes']) for v in targets)
    num_bboxes = torch.as_tensor([num_bboxes], dtype=torch.float, device=outputs['pred_class'].device)

    losses = {}
    l1_bbox_loss, giou_bbox = self.get_bbox_loss(outputs, targets, indices, num_bboxes)

    losses['label_loss'] = self.get_label_loss(outputs, targets, indices)
    losses['l1_loss'] = l1_bbox_loss
    losses['giou_loss'] = giou_bbox

    return losses