import pygame as pg
import sys
sys.path.insert(1,'C:\\Users\\karim\\OneDrive\\Documents\\Code\\PYTHON\\trans-auto')

from components.IconButton2 import IconButton2

class Map :
    def __init__(self,screen,x,y,w,h,connectionToDatabase):

        self.connectionToDatabase = connectionToDatabase
        self.screen = screen
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.offsetX = w/2
        self.offsetY = h/2
        self.offsetXTemp = 0
        self.offsetYTemp = 0
        self.zoomCard = 1

        self.fontP = pg.font.Font(None, 15)

        self.ZoomIn = IconButton2(w - 90,
                                h - 50,
                                40,
                                40,
                                icon = "plus.png",
                                bgColor=pg.Color('#848F8B'),
                                border_thikness=1,
                                border_radius=10,
                                nextFunc=self.ZoomInFunc)
        self.ZoomOut = IconButton2(w - 40,
                                h - 50,
                                40,
                                40,
                                icon = "minus.png",
                                bgColor=pg.Color('#848F8B'),
                                border_thikness=1,
                                border_radius=10,
                                nextFunc=self.ZoomOutFunc)

        self.Cards = self.connectionToDatabase.ReadAllRFID()
        self.Links = self.connectionToDatabase.ReadAllLINK()

        self.graphicCards = {}
        self.graphicLinks = {}
        for card in self.Cards:
            self.graphicCards[card['id']] = {
                                                'id':card['id'],
                                                'code':card['code'],
                                                'x': card['x'],
                                                'y': (self.h-card['y']),
                                            }
        for link in self.Links:
            self.graphicLinks[link['id']] = {
                                                'id': link['id'],
                                                'id1': link['first_end'],
                                                'id2': link['second_end'],
                                            }
        self.frame = pg.Rect(x, y, w, h)

        #! MAP MOVE AND ZOOM VARIABLE
        self.down = False
        self.up = True
        self.positionDown = (0,0)
        self.zoom = 1

        # for l in self.graphicLinks :
        #     print(l)
        # for l in self.graphicCards :
        #     print(l)

        # print(self.graphicLinks)

    def ZoomInFunc(self):
        if  self.zoom < 4:
            self.zoom += 0.1

    def ZoomOutFunc(self):
        if  self.zoom >0.1:
            self.zoom -= 0.1

    def update(self):
        self.Cards = self.connectionToDatabase.ReadAllRFID()
        self.Links = self.connectionToDatabase.ReadAllLINK()
        for card in self.Cards:
            self.graphicCards[card['id']] = {
                                                'id':card['id'],
                                                'code':card['code'],
                                                'x': card['x'],
                                                'y': (self.h-card['y']),
                                            }
        for link in self.Links:
            self.graphicLinks[link['id']] = {
                                                'id': link['id'],
                                                'id1': link['first_end'],
                                                'id2': link['second_end'],
                                            }

    def draw(self , ListOfRobots):

        pg.draw.rect(self.screen, "#ffffff", self.frame,
                     0, 0)
        for link in self.Links:
            x1 = int(self.graphicCards[link['first_end']]['x'] * self.zoom + self.offsetX)
            y1 = int((self.graphicCards[link['first_end']]['y']- self.h) * self.zoom + self.h - self.offsetY)
            x2 = int(self.graphicCards[link['second_end']]['x'] * self.zoom + self.offsetX)
            y2 = int((self.graphicCards[link['second_end']]['y'] - self.h) * self.zoom + self.h - self.offsetY)
            pg.draw.line(self.screen,'#000000',(x1,y1),(x2,y2),int(2 * self.zoom))

        for card in self.Cards:
            x = self.graphicCards[card['id']]['x'] * self.zoom + self.offsetX
            y = (self.graphicCards[card['id']]['y'] - self.h) * self.zoom + self.h - self.offsetY
            pg.draw.circle(self.screen,'#dd0000',(x,y),int(5*self.zoom))
            self.screen.blit(
                self.fontP.render(
                                    '{} ({}, {})'.format(card['id'], card['x'], card['y']),
                                    True,
                                    pg.Color('#000000')
                                ),
                (x + 10 , y + 10)
            )
        for robot in ListOfRobots :
            if robot.position :
                x = robot.position[0] * self.zoom + self.offsetX
                y = -1 * robot.position[1] * self.zoom + self.h - self.offsetY
                pg.draw.circle(self.screen, '#00dd00', (x,y), int(4 * self.zoom))

        self.ZoomIn.draw(self.screen)
        self.ZoomOut.draw(self.screen)

    def handleEvent(self,event):
        # print("type : ",event.type,event.type == pg.KEYDOWN)
        # print("zoom : " ,self.zoom)
        self.ZoomIn.handleEvent(event)
        self.ZoomOut.handleEvent(event)
        if event.type == pg.KEYDOWN:
            # print("key",event.key)
            keys = pg.key.get_pressed()  #checking pressed keys
            if  (keys[pg.K_PLUS] or keys[pg.K_KP_PLUS] ) and self.zoom < 4:
                self.zoom += 0.1
            elif (keys[pg.K_MINUS] or keys[pg.K_KP_MINUS] ) and self.zoom > 0.2:
                self.zoom -= 0.1
            else:
                pass
                # Re-render the text.

        if event.type == pg.MOUSEBUTTONDOWN:
            if self.frame.collidepoint(event.pos):
                self.down = True
                self.up = False
                self.positionDown = event.pos
                self.offsetXTemp = self.offsetX
                self.offsetYTemp = self.offsetY

        if event.type == pg.MOUSEBUTTONUP:
            if self.frame.collidepoint(event.pos):
                self.down = False
                self.up = True
                self.offsetXTemp = self.offsetX
                self.offsetYTemp = self.offsetY

        if event.type == pg.MOUSEMOTION and self.down and not self.up :
            self.offsetX = self.offsetXTemp - (self.positionDown[0] - event.pos[0])
            self.offsetY = self.offsetYTemp + (self.positionDown[1] - event.pos[1])


import pygame

"""
* Checks for the arithmetic of winning points,
* games, and the match.
"""

def check_point_over(stats, ball, paddles, scoreboard):
    # Ball hits players middle wall
    if ball.rect.left > ball.screen_rect.right:
        stats.enemy_score += 1
        check_win(stats)
        reset_objects(scoreboard, ball, paddles)

    # Ball hits enemy middle wall
    elif ball.rect.right < ball.screen_rect.left:
        stats.player_score += 1
        check_win(stats)
        reset_objects(scoreboard, ball, paddles)

    elif ball.rect.bottom < ball.screen_rect.top or ball.rect.top > ball.screen_rect.bottom:
        # Ball hits players top or bottom walls
        if ball.rect.centerx > ball.screen_rect.centerx:
            stats.enemy_score += 1
            check_win(stats)
            reset_objects(scoreboard, ball, paddles)

        # Ball hits enemy top or bottom walls
        else:
            stats.player_score += 1
            check_win(stats)
            reset_objects(scoreboard, ball, paddles)

def reset_objects(scoreboard, ball, paddles):
    scoreboard.initialize_score_display()
    ball.reset()
    for paddle in paddles:
        paddle.reset()

def check_win(stats):
    # Sound files to play if a game or match is lost/won
    player_lose_wav = pygame.mixer.Sound('Sounds/YouLose.ogg')
    player_win_wav = pygame.mixer.Sound('Sounds/YouWin.ogg')
    player_wingame_wav = pygame.mixer.Sound('Sounds/MarioWin.ogg')
    player_losegame_wav = pygame.mixer.Sound('Sounds/MarioLose.ogg')

    if stats.player_score >= 11 and stats.player_score >= (stats.enemy_score + 2):
        stats.player_wins += 1
        stats.player_score = 0
        stats.enemy_score = 0
        player_win_wav.play(0)
    elif stats.enemy_score >= 11 and stats.enemy_score >= (stats.player_score + 2):
        stats.enemy_wins += 1
        stats.player_score = 0
        stats.enemy_score = 0
        player_lose_wav.play(0)

    if stats.player_wins >= 3 and stats.player_wins >= (stats.enemy_wins + 2):
        stats.game_active = False
        stats.play_again = True
        player_wingame_wav.play(2)

    elif stats.enemy_wins >= 3 and stats.enemy_wins >= (stats.player_wins + 2):
        stats.game_active = False
        stats.play_again = True
        player_losegame_wav.play(0)

def update_all(ball, all_paddles, player_paddles, enemy_paddles):
    ball.update(all_paddles)
    for paddle in player_paddles:
        paddle.update()
    for paddle in enemy_paddles:
        paddle.update(ball)

def draw_all(ball, all_paddles):
    ball.draw()
    for paddle in all_paddles:
        paddle.draw()


from vecutil import *
from plotting import *
_browser = None

def getitem(l,d):
    return l.lines[d]

def setitem(l,d,val):
    l.lines[d] = val

class Graph:
	"""docstring for Foo"""
	def __init__(self):
		self.lines = {}
		self.points = {}

	__getitem__ = getitem

	__setitem__ = setitem

	def add_point(self, pt, name,color=None):
		self.points[name] = {'pt':pt, 'color':color}

	def add_line(self, l, name, color=None):
		""" add a line to the lines
		    input: l is a list of two tuples or lists ---> [(1,3), (6,2)] or [[1,3], [6,2]
		"""
		if len(l) == 2:
			self.lines[name] = {'line':l, 'color':color}
		else:
			raise ValueError

	def show(self, browser=None):
	    scalar = 200./10
	    # origin = (210, 210)
	    dot_size = 3
	    origin = 210
	    hpath = create_temp('.html')
	    with open(hpath, 'w') as h:
	        h.writelines(
	            ['<!DOCTYPE html>\n'
	            ,'\n'
	            ,'<title>plot</title>\n'
	            ,'\n'
	            ,'\n'
	            ,'<svg height="420" width=420 xmlns="http://www.w3.org/2000/svg">\n'
	            ,'<line x1="0" y1="210" x2="420" y2="210"'
	            ,'style="stroke:rgb(0,0,0);stroke-width:2"/>\n'
	            ,'<line x1="210" y1="0" x2="210" y2="420"'
	            ,'style="stroke:rgb(0,0,0);stroke-width:2"/>\n'])
	        for line in self.lines:
	            l = self.lines[line]
	            x1, y1 = vec2list(l['line'][0])
	            x2, y2 = vec2list(l['line'][1])
	            h.writelines(['<line x1="%d" y1="%d" x2="%d" y2="%d" style="stroke:%s;stroke-width:2"/>\n'
	                          % (origin+x1*scalar,origin-y1*scalar,origin+x2*scalar,origin-y2*scalar,l['color'])])
	        for pts in self.points:
	            pt = self.points[pts]
	            x,y = vec2list(pt['pt'])
	            h.writelines(['<circle cx="%d" cy="%d" r="%d" fill="%s"/>\n'
	                        % (origin+scalar*x,origin-scalar*y,dot_size,pt['color'])])
	        h.writelines(['</svg>\n\n'])
	    if browser is None:
	        browser = _browser
	    webbrowser.get(browser).open('file://%s' % hpath)

# class Lines:
# 	def __init__(self):
# 		self.lines = {}

# 	__getitem__ = getitem

# 	__setitem__ = setitem

# 	def __iter__(self):
# 		return self.lines.__iter__()

# 	def add_line(self, l, name):
# 		""" add a line to the lines
# 		    input: l is a list of two tuples or lists ---> [(1,3), (6,2)] or [[1,3], [6,2]
# 		"""
# 		if len(l) == 2:
# 			self.lines[name] = l
# 		else:
# 			raise ValueError


import tensorflow as tf
import re
import io
import unicodedata

def unicode_to_ascii(s):
  return ''.join(c for c in unicodedata.normalize('NFD', s)
      if unicodedata.category(c) != 'Mn')

def preprocess_sentence(w):
  w = re.sub(r"([?.!,¿])", r" \1 ", w)
  w = re.sub(r'[" "]+', " ", w)

  # replacing everything with space except (a-z, A-Z, ".", "?", "!", ",")
  w = re.sub(r"[^a-zA-Z가-힣?.!,¿]+", " ", w)
  w = w.strip()

  # adding a start and an end token to the sentence
  # so that the model know when to start and stop predicting.
  w = '<start> ' + w + ' <end>'
  return w

def create_dataset(path, num_examples):
  lines = io.open(path, encoding='UTF-8').read().strip().split('\n')
  word_pairs = [[preprocess_sentence(w) for w in l.split('\t')[:2]]  for l in lines[:num_examples]]
  print('word_pairs', word_pairs)

  return zip(*word_pairs)

def tokenize(lang):
    lang_tokenizer = tf.keras.preprocessing.text.Tokenizer(filters='')
    lang_tokenizer.fit_on_texts(lang)
    tensor = lang_tokenizer.texts_to_sequences(lang)
    tensor = tf.keras.preprocessing.sequence.pad_sequences(tensor, padding='post')
    return tensor, lang_tokenizer

def load_dataset(path, num_examples=None):
    # creating cleaned input, output pairs
    targ_lang, inp_lang = create_dataset(path, num_examples)

    input_tensor, inp_lang_tokenizer = tokenize(inp_lang)
    target_tensor, targ_lang_tokenizer = tokenize(targ_lang)

    return input_tensor, target_tensor, inp_lang_tokenizer, targ_lang_tokenizer


import Tkinter
import tkFileDialog
import ImageTk

COLORS = 'black', 'red', 'green', 'blue'

def main():
    global X, Y, hold, speed, blink, pause, screen, timer
    X, Y = 0, 0
    hold = []
    speed = 128
    blink = None
    pause = None
    root = Tkinter.Tk()
    root.withdraw()
    root.resizable(False, False)
    root.title('Tiled Video Player')
    root.bind('<Escape>', do_pause)
    root.bind('-', slow)
    root.bind('=', fast)
    image = get_image()
    screen = Tkinter.Canvas(root, width=320, height=240, background='red', highlightthickness=0)
    screen.bind('<Motion>', motion)
    screen.bind('<ButtonPress-1>', press)
    screen.bind('<ButtonRelease-1>', release)
    screen.bind('<Button-3>', delete)
    screen.pack()
    screen.create_image(800, 600, image=image)
    timer = screen.after(100, update)
    root.deiconify()
    root.mainloop()

def slow(event):
    global speed, timer
    if speed < 1024:
        speed <<= 1
        if timer is not None:
            screen.after_cancel(timer)
            timer = screen.after(speed, update)
    show_speed(True)

def fast(event):
    global speed, timer
    if speed > 2:
        speed >>= 1
        if timer is not None:
            screen.after_cancel(timer)
            timer = screen.after(speed, update)
    show_speed(True)

def get_image():
    try:
        filename = tkFileDialog.askopenfilename(title='Tiled Video Player')
        assert filename
        image = ImageTk.PhotoImage(file=filename)
        assert (1600, 1200) == (image.width(), image.height())
        return image
    except:
        raise SystemExit

def motion(event):
    if hold:
        hold.extend([event.x, event.y])
        event.widget.create_line(hold[-4:], fill='red', width=2, tag='TEMP')

def press(event):
    global hold
    hold = [event.x, event.y]

def release(event):
    global hold
    if len(hold) > 2:
        event.widget.delete('TEMP')
        event.widget.create_line(hold, fill='red', width=2, tag='LINE', smooth=True)
    hold = []

def delete(event):
    event.widget.delete('LINE')

def do_pause(event):
    global timer, pause
    if timer is None:
        timer = screen.after(speed, update)
        screen.delete('PAUSE')
        screen.after_cancel(pause)
        pause = None
    else:
        timer = screen.after_cancel(timer)
        show_pause(True)

def show_pause(start=False):
    global P_COLOR, pause
    if start:
        P_COLOR = 0
        if pause is not None:
            screen.after_cancel(pause)
    else:
        P_COLOR += 1
        P_COLOR %= len(COLORS)
    screen.delete('PAUSE')
    fill = COLORS[P_COLOR]
    screen.create_text(295, 10, fill=fill, text='PAUSE', tag='PAUSE')
    pause = screen.after(500, show_pause)

def show_speed(start=False):
    global FPS_COLOR, blink
    if start:
        FPS_COLOR = 0
        if blink is not None:
            screen.after_cancel(blink)
    else:
        FPS_COLOR += 1
    screen.delete('FPS')
    if FPS_COLOR < len(COLORS):
        fill = COLORS[FPS_COLOR]
        text = '%s FPS' % int(1000.0 / speed + 0.5)
        screen.create_text(25, 10, fill=fill, text=text, tag='FPS')
        blink = screen.after(500, show_speed)
    else:
        blink = None

def update():
    global X, Y, timer
    timer = screen.after(speed, update)
    X += 1
    if X == 5:
        X = 0
        Y += 1
        if Y == 5:
            Y = 0
            screen.move(1, 1280, 960)
        else:
            screen.move(1, 1280, -240)
    else:
        screen.move(1, -320, 0)

if __name__ == '__main__':
    main()

Python 2.X/ZERO/GUI/TV Player/TV Player v3.pyw


import sqlalchemy
import pandas as pd
print("リスト3.22:DBからDateFrameへの取り込み")
db_url = "sqlite:///iris.db"
sql = "SELECT * FROM iris"
engine = sqlalchemy.create_engine(db_url)
iris_data = pd.read_sql(sql, engine)
print("条件なし")
print(iris_data)
print("加工済みデータをDateFrameに")
sql = """SELECT * FROM iris WHERE Species = '{0}' AND "Petal Width" >= {1}""".format("versicolor", 1.5)
versicolor = pd.read_sql(sql, engine)
versicolor
print("条件指定:種(Species)　versicolor, 花弁の長さ(Petal Width)1.5以上")
print(versicolor)


#!/usr/bin/python3

# Yet Another Simple Site Generator - MIT License by Kevin Froman (https://ChaosWebs.net)

# Usage: Simply enter the top half of your site in header.html, bottom half in footer.html.
# Then put your page content in the source folder with a .html extension.
# Just run the script to generate your site :)

import glob, os

print("Generating Site...")

header = open('header.html', 'r').read()
footer = open('footer.html', 'r').read()

for file in glob.glob("source/*.html"):

	sourceFile = open(file, 'r')
	fileContents = sourceFile.read()

	generatedFile = open('output/' + file.replace('source/', ''), 'w')

	generatedFile.write(header + fileContents + footer)

	generatedFile.close()

sourceFile.close()

print("Site generated.")


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

import sys
from os import listdir
import glob

filelist = glob.glob("./filter_use/*.pdb")

ofile = open("s_pdb_hbplus.bash","w")
for filename in filelist:
    pos = filename.rfind("/")
    name = filename[pos+1:-4]
    print >> ofile, "hbplus  ../filter_use/"+name+".pdb"

ofile.close()


from KratosMultiphysics import *
import python_process
from math import *

class aux_object:
    def __init__(self, t, compiled_function, variable ):
        self.t = t
        self.compiled_function = compiled_function
        self.variable = variable

    def f(self,node):
        x = node.X
        y = node.Y
        z = node.Z
        t = self.t
        node.SetSolutionStepValue(self.variable,0, eval(self.compiled_function) )

    def freturn(self,node):
        x = node.X
        y = node.Y
        z = node.Z
        t = self.t
        return eval(self.compiled_function)

class aux_object_cpp_callback:
    def __init__(self, compiled_function ):
        self.compiled_function = compiled_function

    def f(self,x,y,z,t):
        return eval(self.compiled_function)
        #return 0

##all the processes python processes should be derived from "python_process"
class ApplyCustomFunctionProcess(Process):
    def __init__(self, model_part, mesh_id, variable_name, interval, is_fixed, function_string, free_outside_of_interval=True):
        #python_process.PythonProcess.__init__(self)
        Process.__init__(self)
        self.model_part = model_part
        self.variable = globals().get(variable_name)
        self.mesh = model_part.GetMesh(mesh_id)
        #self.table = model_part.GetTable(table_id)
        self.interval = interval #expected to be a vector of size 2
        self.is_fixed = is_fixed
        self.free_outside_of_interval = free_outside_of_interval
        self.function_string = function_string

        ##compile function function_string
        self.compiled_function = compile(self.function_string, '', 'eval', optimize=2) #here we precompile the expression so that then it is much faster to execute it

        #construct a variable_utils object to speedup fixing
        self.variable_utils = VariableUtils()

        self.tmp = aux_object_cpp_callback(self.compiled_function)
        self.cpp_apply_function_utility = PythonGenericFunctionUtility(self.variable, self.mesh.Nodes, self.tmp )

        print("finished construction of ApplyCustomFunctionProcess Process")

    def function(self, x,y,z,t):
        return eval(self.compiled_function) #this one is MUCH FASTER!
        #return eval(self.function_string)

    def ExecuteInitializeSolutionStep(self):
        current_time = self.model_part.ProcessInfo[TIME]

        if(current_time > self.interval[0] and  current_time<self.interval[1]):
            self.variable_utils.ApplyFixity(self.variable, self.is_fixed, self.mesh.Nodes)

            #create and pass around a list
            #tmp = aux_object(current_time, self.compiled_function, self.variable)
            #self.variable_utils.ApplyVector(self.variable, Vector( list(map(tmp.freturn, self.mesh.Nodes)) ),  self.mesh.Nodes)

            #other option - also faster than the first for larger examples
            #tmp = aux_object(current_time, self.compiled_function, self.variable)
            #any(map(tmp.f, self.mesh.Nodes))

            #faster for larger examples
            #for node in self.mesh.Nodes:
                #node.SetSolutionStepValue(self.variable,0,    self.function(node.X,node.Y,node.Z, current_time)    )

            #ultimate version. calling custom defined function from c++
            self.cpp_apply_function_utility.ApplyFunction(current_time)

    def ExecuteFinalizeSolutionStep(self):
        if self.free_outside_of_interval:
            current_time = self.model_part.ProcessInfo[TIME]

            if(current_time > self.interval[0] and  current_time<self.interval[1]):

                #here we free all of the nodes in the mesh
                fix = False
                self.variable_utils.ApplyFixity(self.variable, fix, self.mesh.Nodes)

def Factory(settings, Model):
    params = settings["parameters"]

    if(settings["process_name"] == "ApplyCustomFunctionProcess"):
        model_part = Model.get(  params.get( "model_part_name", "not found!!" ) , "model part not found" )
        mesh_id = params["mesh_id"]
        variable_name = params["variable_name"]
        is_fixed = params["is_fixed"]
        #table_id = params["table_id"]
        interval = params["interval"]
        free_outside_of_interval = params["free_outside_of_interval"]
        function_string = params["f(x,y,z,t)="]

        if(len(interval) != 2):
            raise Exception( "interval size is expected to be two, while we got interval = "+str(interval));

        #here we could eventually sanitize the function

        return ApplyCustomFunctionProcess(model_part, mesh_id, variable_name, interval, is_fixed, function_string, free_outside_of_interval)
    else:
        raise Exception("trying to construct a Process with the wrong name!")


# http://stackoverflow.com/questions/34649152/neural-network-backpropagation-algorithm-not-working-in-python
import numpy as np
import math
import matplotlib.pyplot as plt

class Neuralnet:
    def __init__(self, neurons, activation):
        self.weights = []
        self.inputs = []
        self.outputs = []
        self.errors = []
        self.rate = 0.5
        self.activation = activation    #sigmoid or tanh

        self.neurons = neurons
        self.L = len(self.neurons)      #number of layers

        eps = 0.12    # range for uniform distribution   -eps..+eps
        for layer in range(len(neurons)-1):
            self.weights.append(np.random.uniform(-eps,eps,size=(neurons[layer+1], neurons[layer]+1)))

    ###################################################################################################
    def train(self, X, Y, iter_count):

        m = X.shape[0] # get batch size

        for layer in range(self.L):
            self.inputs.append(np.empty([m, self.neurons[layer]])) # initialize layers
            self.errors.append(np.empty([m, self.neurons[layer]])) # ??????

            if (layer < self.L -1):
                self.outputs.append(np.empty([m, self.neurons[layer]+1])) # is bias added????
            else:
                self.outputs.append(np.empty([m, self.neurons[layer]]))

        #accumulate the cost function
        J_history = np.zeros([iter_count, 1])

        for i in range(iter_count):

            self.feedforward(X)

            J = self.cost(Y, self.outputs[self.L-1])
            J_history[i, 0] = J

            self.backpropagate(Y)

        #plot the cost function to check the descent
        plt.plot(J_history)
        plt.show()

    ###################################################################################################
    def cost(self, Y, H):
        J = np.sum(np.sum(np.power((Y - H), 2), axis=0))/(2*m)
        return J

    ###################################################################################################
    def feedforward(self, X):

        m = X.shape[0] # get size of training batch

        self.outputs[0] = np.concatenate(  (np.ones([m, 1]),   X),   axis=1) # inputs and bias of the input layer

        for i in range(1, self.L): # for all the layers except the input layer
            self.inputs[i] = np.dot( self.outputs[i-1], self.weights[i-1].T  ) # calculate the input of the layer by multiplying the weights to the outputs of previous layer

            # then calculate the output of the current layer by using the activation function
            if (self.activation == 'sigmoid'):
                output_temp = self.sigmoid(self.inputs[i])
            elif (self.activation == 'tanh'):
                output_temp = np.tanh(self.inputs[i])

            if (i < self.L - 1): # for any layer before the last layer
                self.outputs[i] = np.concatenate(  (np.ones([m, 1]),   output_temp),   axis=1) # fill the output matrix with biases
            else: # for the last layer
                self.outputs[i] = output_temp # don't use biases and only give the output

    ###################################################################################################
    def backpropagate(self, Y):

        self.errors[self.L-1] = self.outputs[self.L-1] - Y # calculate the errors of last layer

        for i in range(self.L - 2, 0, -1):
            # calculate the error of each layer using the errors of the layer after it
            if (self.activation == 'sigmoid'):
                self.errors[i] = np.dot(  self.errors[i+1],   self.weights[i][:, 1:]  ) *  self.sigmoid_prime(self.inputs[i])
            elif (self.activation == 'tanh'):
                self.errors[i] = np.dot(  self.errors[i+1],   self.weights[i][:, 1:]  ) *  (1 - self.outputs[i][:, 1:]*self.outputs[i][:, 1:])

        # update the weights using the calculated errors
        for i in range(0, self.L-1):
            grad = np.dot(self.errors[i+1].T, self.outputs[i]) / m
            self.weights[i] = self.weights[i] - self.rate*grad

    ###################################################################################################
    def sigmoid(self, z):
        s = 1.0/(1.0 + np.exp(-z))
        return s

    ###################################################################################################
    def sigmoid_prime(self, z):
        s = self.sigmoid(z)*(1 - self.sigmoid(z))
        return s

    ###################################################################################################
    def predict(self, X, weights):

        m = X.shape[0];

        self.inputs = []
        self.outputs = []
        self.weights = weights

        for layer in range(self.L):
            self.inputs.append(np.empty([m, self.neurons[layer]]))

            if (layer < self.L -1):
                self.outputs.append(np.empty([m, self.neurons[layer]+1]))
            else:
                self.outputs.append(np.empty([m, self.neurons[layer]]))

        self.feedforward(X)

        return self.outputs[self.L-1]

###################################################################################################
#                MAIN PART

activation1 = 'sigmoid'     # the input should be scaled into [ 0..1]
activation2 = 'tanh'        # the input should be scaled into [-1..1]

activation = activation1

net = Neuralnet([1, 6, 1], activation) # structure of the NN and its activation function

##########################################################################################
#                TRAINING

m = 1000 #size of the training set
X = np.linspace(0, 4*math.pi, num = m).reshape(m, 1); # input training set

Y = np.sin(X) # target

kx = 0.1 # noise parameter
noise = (2.0*np.random.uniform(0, kx, m) - kx).reshape(m, 1)
Y = Y + noise # noisy target

# scaling of the target depending on the activation function
if (activation == 'sigmoid'):
    Y_scaled = (Y/(1+kx) + 1)/2.0
elif (activation == 'tanh'):
    Y_scaled = Y/(1+kx)

# number of the iteration for the training stage
iter_count = 20000
net.train(X, Y_scaled, iter_count) #training

# gained weights
trained_weights = net.weights

##########################################################################################
#                 PREDICTION

m_new = 40 #size of the prediction set
X_new = np.linspace(0, 4*math.pi, num = m_new).reshape(m_new, 1);

Y_new = net.predict(X_new, trained_weights) # prediction

#rescaling of the result
if (activation == 'sigmoid'):
    Y_new = (2.0*Y_new - 1.0) * (1+kx)
elif (activation == 'tanh'):
    Y_new = Y_new * (1+kx)

# visualization
plt.plot(X, Y)
plt.plot(X_new, Y_new, 'ro')
plt.draw()

raw_input('press any key to exit')


from musiclang.core.score import *

class Progression:
    def __init__(self, *degrees):
        self.base_progression = [d.copy() for d in degrees]
        self.progression = list([d.copy() for d in degrees])
        self.score = None
        self.pattern_ = None
        self.rythm_ = None

    def __iter__(self):
        return self.progression

    def __call__(self, note=C5):
        self.score = None
        if self.rythm_ is not None:
            print(len(self.pattern_))
            print(len(self.rythm_))
            for d, r in zip(self.pattern_, self.rythm_):
                print(r)
                self.score += d.augment(r)
        else:
            for d in self.pattern_:
                self.score += d
        self.progression = self.base_progression
        return self.score+(note.note-C5.note)

    def pattern(self, items):
        # List of list or list of ints
        progression = []
        for idx, s in enumerate(self.progression):
            progression.append(s.pattern(*items))
        self.pattern_ = progression
        return self

class Degrees:
    def __init__(self, scale):
        self.scale = scale

    def __call__(self, number):
        start_note = self.scale(number)
        return self.scale(number) * self.scale(number+2) * self.scale(number+4) * self.scale(number+6)

class CustomDegrees:
    def __init__(self, degrees):
        self.degrees = degrees
    def __call__(self, number):
        return self.degrees[number]


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 13 15:56:03 2021

@author: jzimmer1
"""

import os
import re
import json

# === === Helper functions === ===
def write_json(data, filename):
    with open(filename,"w") as f:
        json.dump(data,f)
    return None
def get_json(filename):
    with open(filename) as f:
        jsonobj = json.load(f)
    return jsonobj

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

def delete_extras(filename):
    pathname = filename
    filename = filename.split("/")[-1]
    #filename = filename.split(".")[0]
    #print(filename)
    edit = "?action=edit"
    source = "?action=source"
    if filename.endswith(edit) or filename.endswith(source):
        print("deleting ", filename)
        os.remove(pathname)
    else:
        pass
    return None

def foldercont(foldername):
    allfiles = os.listdir(foldername)
    return allfiles

def compare_folders():
    attemptfiles = foldercont("tvtropes.org-attempt/pmwiki/pmwiki.php/Literature")
    attemptname = "tvtropes.org-attempt/pmwiki/pmwiki.php/Literature/"
    files = foldercont("tvtropes.org-plain/pmwiki/pmwiki.php/Literature")
    filesname = "tvtropes.org-plain/pmwiki/pmwiki.php/Literature/"
    thirdfiles = foldercont("tvtropes.org-third/pmwiki/pmwiki.php/Literature")
    thirdname = "tvtropes.org-third/pmwiki/pmwiki.php/Literature/"
    # for f in thirdfiles:
    #     delete_extras("tvtropes.org-third/pmwiki/pmwiki.php/Literature/" + f)
    # thirdfiles = foldercont("tvtropes.org-third/pmwiki/pmwiki.php/Literature")
    # different_files = []
    # filelist = []
    # for f in attemptfiles:
    #     if f not in files and f not in thirdfiles:
    #         different_files.append(f)
    #         filelist += ["In attempt only: tvtropes.org-attempt/pmwiki/pmwiki.php/Literature",f]
    # for f in files:
    #     if f not in attemptfiles and f not in thirdfiles:
    #         different_files.append(f)
    #         filelist += ["In plain only: tvtropes.org-plain/pmwiki/pmwiki.php/Literature",f]
    # for f in thirdfiles:
    #     if f not in files and f not in attemptfiles:
    #         different_files.append(f)
    #         filelist += ["In third only: tvtropes.org-third/pmwiki/pmwiki.php/Literature",f]
    # write_json(filelist, "difference_in_paths_3_Lit_wget_results_and.json")
    os.mkdir("Literature-Union")
    unionname = "Literature-Union/"
    unionfiles = list(set(attemptfiles + files + thirdfiles))
    write_json(unionfiles, "union_of_3_Lit_wget_results.json")
    for f in unionfiles:
        if f in thirdfiles:
            os.rename(thirdname+f, unionname+f)
        elif f in files:
            os.rename(filesname+f, unionname+f)
        elif f in attemptfiles:
            os.rename(attemptname+f, unionname+f)
        else:
            print("where did this come from: ",f)
    return None

compare_folders()

Literature-Union/Scraping Literature 3 times/delete_extra_files_literature.py


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

#统计
dict_log = {}
f = open('www_access_20140823.log')
for i in f.readlines():
    tmp_list = i.split()
    tulpe_key =  (tmp_list[0],tmp_list[6],tmp_list[8])
    dict_log[tulpe_key] = dict_log.get(tulpe_key,0)+1
f.close()

#翻转
new_dict = {}
for key,val in dict_log.items():
    new_dict.setdefault(val,[])
    new_dict[val].append(key)

key_arr = new_dict.keys()
res = """
"

		NO
		srt
		NUM

"""
#前10名
count = 1
while count <11:
    max_count = max(key_arr)
    for i in new_dict[max_count]:
        str = '\t
\n\t\t%s	\n\t\t%s	\n\t\t%s	\n\t
' %(count,i,max_count)
        res += str
    key_arr.remove(max_count)
    count = count +len(new_dict[max_count])
#生成文件
f = open('web.html','w+')
for i in res:
    f.writelines(i)
f.close()


"""
Weights for PySAL Network Module
"""

__author__ = "Sergio Rey <sjsrey@gmail.com>, Jay Laura <jlaura@asu.edu>"

from itertools import combinations
import numpy as np
import pysal as ps
from util import threshold_distance, edge_length, knn_distance

def w_links(wed):
    """
    Generate Weights object for links in a WED

    Parameters
    ----------
    wed: PySAL Winged Edged Data Structure

    Returns

    ps.W(neighbors): PySAL Weights Dict
    """
    nodes = wed.node_edge.keys()
    neighbors = {}
    for node in nodes:
        lnks = wed.enum_links_node(node)
        # put i,j s.t. i < j
        lnks = [tuple(sorted(lnk)) for lnk in lnks]
        for comb in combinations(range(len(lnks)), 2):
            l, r = comb
            if lnks[l] not in neighbors:
                neighbors[lnks[l]] = []
            neighbors[lnks[l]].append(lnks[r])
            if lnks[r] not in neighbors:
                neighbors[lnks[r]] = []
            neighbors[lnks[r]].append(lnks[l])
    return ps.W(neighbors)

def w_distance(wed, threshold, cost=None, alpha=-1.0, binary=True, ids=None):
    '''
    Generate a Weights object based on a threshold
     distance using a WED

    Parameters
    ----------
    wed: PySAL Winged Edged Data Structure
    distance: float network threshold distance for neighbor membership
    cost: defaults to length, can be any cost dicationary {(edge): cost}

    Returns
    -------
    ps.W(neighbors): PySAL Weights Dict
    '''

    if cost is None:
        cost = edge_length(wed)
    if ids:
        ids = np.array(ids)
    else:
        ids = np.arange(len(wed.node_list))
    neighbors = {}
    if binary is True:
        for node in wed.node_list:
            near, pred = threshold_distance(wed, cost, node, threshold)
            neighbors[ids[node]] = near
        return ps.W(neighbors, None, ids)
    elif binary is False:
        weights = {}
        for node in wed.node_list:
            wt = []
            near, pred = threshold_distance(wed, cost, node, threshold)
            near.remove(node)
            neighbors[ids[node]] = near
            for end in near:
                path = [end]
                previous = pred[end]
                while previous != node:
                    path.append(previous)
                    end = previous
                    previous = pred[end]
                path.append(node)
                cum_cost = 0
                for p in range(len(path) - 1):
                    cum_cost += cost[(path[p], path[p + 1])]
                wt.append(cum_cost ** alpha)
            weights[ids[node]] = wt
        return ps.W(neighbors, weights, ids)

def w_knn(wed, n, cost=None, ids=None):
    '''
    Generate w Weights object based on the k-nearest
     network neighbors.

    Parameters
    ----------
    wed: PySAL Winged Edged Data Structure
    n: integer number of neighbors for each node
    cost: defaults to length, can be any cost dictionary

    Returns
    -------
    ps.W(neighbors): PySAL Weights Dict
    '''

    if cost is None:
        cost = edge_length(wed)
    if ids:
        ids = np.array(ids)
    else:
        ids = np.arange(len(wed.node_list))
    neighbors = {}
    for node in wed.node_list:
        neighbors[node] = knn_distance(wed, cost, node, n=n)
    return ps.W(neighbors, id_order=ids)


def swapFileData():
    fileName=input("Enter file name: ")
    wordCount=0
    file = open("data/names.txt", "r")
    for i in file :
        words=i.split()
        wordCount=wordCount+len(words)
    print("number of words : ")
    print(wordCount)
swapFileData()


from tests.base import BaseTestCase
from nose.plugins.attrib import attr

from shiftcontent.fields import Text

@attr('fields', 'field_text')
class TextTest(BaseTestCase):

    def test_instantiate_field(self):
        """ Instantiating text field """
        field = Text()
        field.set(None)
        self.assertIsInstance(field, Text)

    def test_get_value(self):
        """ Getting value from text field """
        value = 'i am field value'
        field = Text(value)
        self.assertEquals(value, field.get())
        self.assertTrue(type(field.get()) is str)

    def test_get_db_representation(self):
        """ Getting db representation of text field value """
        value = 'i am field value'
        field = Text(value)
        self.assertEquals(value, field.to_db())
        self.assertTrue(type(field.to_db()) is str)

    def test_populate_from_db(self):
        """ Populating value from db representation for text field"""
        value = 'i am field value'
        field = Text()
        field.from_db(value)
        self.assertEquals(value, field.get())
        self.assertTrue(type(field.to_db()) is str)

    def test_get_json_representation(self):
        """ Getting json representation of text field value """
        value = 'i am field value'
        field = Text(value)
        self.assertEquals(value, field.to_json())
        self.assertTrue(type(field.to_json()) is str)

    def test_populate_from_json(self):
        """ Populating value from json representation for text field"""
        value = 'i am field value'
        field = Text()
        field.from_json(value)
        self.assertEquals(value, field.get())
        self.assertTrue(type(field.to_db()) is str)

    def test_get_search_representation(self):
        """ Getting search representation of text field value """
        value = 'i am field value'
        field = Text(value)
        self.assertEquals(value, field.to_search())
        self.assertTrue(type(field.to_search()) is str)

    def test_get_search_mapping(self):
        """ Getting search index data type for the text field """
        field = Text()
        mapping = field.search_mapping()
        self.assertEquals('text', mapping['type'])


import sys
sys.path.append("/Users/rahuldhakecha/RajGroup/SD/WebFrom/")
from Connections.AWSMySQL import AWSMySQLConn
from datetime import datetime as dt
from datetime import date
import pandas as pd
import time
import re
pd.set_option("display.max_columns", None, "display.max_rows", None)

connection = AWSMySQLConn()

lead_status = ['OPEN', 'CONTACTED', 'VISITED', 'ENQUIRY', 'OFFER', 'WON', 'CLOSE', 'HOLD', 'REGRET', 'LETTER']
lead_status.remove('LETTER')
print(lead_status)

def add():
    print("Hello")
    return 9

def rectify_commp_location(comp_location, order_key):
    count = comp_location.count('\\')
    if count == 1:
        new_loc = comp_location[:0] + '\\\\\\' + comp_location[0:7] + '\\\\' + comp_location[7:13] + '\\\\' + comp_location[13:36] + '\\\\' + comp_location[36:45] + '\\\\' + comp_location[45:59] + '\\\\' + comp_location[59:]
        connection.execute("UPDATE RajElectricalsOrdersNew SET comp_location='{}' where "
                           "order_key='{}'".format(new_loc, order_key))
    else:
        new_loc = comp_location
    print(new_loc)

if __name__ == '__main__':
    value = connection.execute_query(
        "select order_key, order_date, po_no, project_description, scope_of_work, client_name, "
        "client_location, order_no, file_no, order_status, project_incharge, project_value,"
        " remarks, comp_location from RajElectricalsOrdersNew;")
    temp = value['client_name'].apply(lambda x:x.lower())
    print(temp.str.contains('arden'.lower()))
    # value['comp_location'] = value.apply(lambda row: rectify_commp_location(row['comp_location'], row['order_key']), axis=1)
    # connection.execute_query("select email from users where user_name='{}'".format('AnkitRibadiya')).iloc[0]['email']
    # print(str(dt.now().year))


import os
import os.path
import glob
# import data.move_read_test as dm
# import torch
# from torch.utils import data
import cv2
import numpy as np
import tensorflow as tf
from keras.preprocessing import image
import keras
from collections import Counter
import csv
import sys
import time

model = keras.models.load_model(os.path.join('checkpoints', 'Afterwashpart3.018-3.38.hdf5'))

dict_club = {}
with open('dic.csv')as f:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        dict_club[row[0]] = row[1]

def Eval_Video(mode=None,mcp=None):

    data = get_video()
    total=0
    error_list=[]
    predict_list=[]
    print(data)

    for i in range(len(data)):
        current_video=data[i].split(os.path.sep)[8].split('.')[0]
        predict_videoname=data[i].split(os.path.sep)[8]
        print(current_video)
        pic_path=glob.glob(os.path.join('/home/sk49/workspace/dataset/QIYI_FACE/Test/face_crop','*jpg'))
        final_list=[]
        if(len(pic_path)!=0):
            for x in pic_path:
                if(x.find(current_video) != -1):
                   final_list.append(x)
            if(len(final_list)==0):
                error_list.append(current_video)
            else:
                total+=1
                print(final_list)
                print(time.time())
                predict = Load_predict(final_list)
                predict_list.append([predict_videoname,predict])
                print(time.time())

    with open('predict.csv','w',newline='') as f1:
        writer1 = csv.writer(f1)
        writer1.writerows(predict_list)

    with open('error_video.csv', 'w') as f:
        writer = csv.writer(f)
        writer.writerows(error_list)

    print("总计："+str(total)+'  未识别视频'+str(len(error_list)))

def Load_predict(final_list=None):
    # basemodel=torch.load('')
    # MCP=torch.load('')
    result=[]
    max=0
    for x in final_list:
        img = image.load_img(x, target_size=(224, 224))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x=x/255.0
        preds = model.predict(x)
        predict=np.argmax(preds)
        result.append(predict)

    count=Counter(result).most_common(1)
    max , _ = count[0]

    return list(dict_club.keys())[list(dict_club.values()).index(str(max))]

def get_video(version='01'):
    video_list=[]
    for x in glob.glob(os.path.join('/home/sk49/workspace/dataset/character_recog_iqiyi/test/IQIYI_VID_TEST','*mp4')):
        video_list.append(x)

    # return video_list[start_num:end_num+1]
    return video_list

if __name__ == '__main__':
    # argv = sys.argv
    # print(argv)
    # start_num = 0
    # end_num =172834   #(172835)
    # start_num = int(argv[1])
    # end_num = int(argv[2])
    # print("start:", start_num, "end", end_num)
    Eval_Video()


#! /usr/bin/env python
# s3_list_buckets.py
"""
Lists all keys in the specified bucket, along with each key's metadata.
Saves it all into a sqlite3 database.
"""

import os
import sqlite3
import sys
from boto.s3.connection import S3Connection

def list_bucket(bucket_name, conn):
    create_db_if_necessary(conn)
    s3 = S3Connection()
    bucket = s3.get_bucket(bucket_name)
    for key in bucket.list():
        pk, saved = add_to_db(conn, bucket, key)
        status = 'Inserted'
        if saved == False:
            status = 'Exists - Not Updated'
        print("{0:08d}  {1}  {2}".format(pk, key.name, status))

def add_to_db(conn, bucket, key):
    pk = existing_record_id(conn, key)
    if pk:
        return pk, False  # No need to process this again
    statement = """insert into s3_keys
    (bucket, name, cache_control, content_type, etag,
    last_modified, storage_class, size)
    values (?,?,?,?,?,?,?,?)"""
    c = conn.cursor()
    c.execute(statement, (key.bucket.name, key.name,
                          key.cache_control, key.content_type,
                          key.etag.replace('"', ''),
                          key.last_modified, key.storage_class,
                          key.size))
    conn.commit()
    pk = c.lastrowid
    full_key = bucket.get_key(key.name)
    for k,v in full_key.metadata.iteritems():
        statement = """insert into s3_meta (key_id, name, value)
        values (?,?,?)"""
        conn.execute(statement, (pk, k, v))
        conn.commit()
    c.close()
    return pk, True

def existing_record_id(conn, key):
    exists = "select id from s3_keys where name=? and etag=? and bucket=?"
    c = conn.cursor()
    c.execute(exists, (key.name, key.etag.replace('"', ''), key.bucket.name))
    row = c.fetchone()
    if row and len(row) > 0:
        return row[0]
    return None

def create_db_if_necessary(conn):
    query = """SELECT name FROM sqlite_master WHERE type='table'
    AND name='s3_keys'"""
    c = conn.cursor()
    c.execute(query)
    row = c.fetchone()
    if not row or len(row) < 1:
        print("Creating table s3_keys")
        statement = """create table s3_keys(
        id integer primary key autoincrement,
        bucket text, name text, cache_control text,
        content_type text,
        etag text, last_modified datetime,
        storage_class text, size int)"""
        conn.execute(statement)
        conn.commit()

        print("Creating index ix_name_etag_bucket on s3_keys")
        statement = """create index ix_name_etag_bucket on
        s3_keys(name, etag, bucket)"""
        conn.execute(statement)
        conn.commit()

        print("Creating table s3_meta")
        statement = "create table s3_meta(key_id, name, value)"
        conn.execute(statement)
        conn.commit()
    c.close()

if __name__ == "__main__":
    if not os.path.exists('db'):
        os.mkdir('db')
    conn = sqlite3.connect('db/aptrust_s3.db')
    list_bucket('aptrust.preservation.storage', conn)
    list_bucket('aptrust.preservation.oregon', conn)
    conn.close()


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

"""
@Author: yangwenhao
@Contact: 874681044@qq.com
@Software: PyCharm
@File: extract_xvector_egs.py
@Time: 2019/12/10 下午10:32
@Overview: Exctract speakers vectors for kaldi PLDA.
"""
from __future__ import print_function

import argparse
import os
import time

import numpy as np
import torch
import torch._utils
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torchvision.transforms as transforms
# from Process_Data.voxceleb2_wav_reader import voxceleb2_list_reader
from Process_Data.KaldiDataset import KaldiExtractDataset
from kaldi_io import read_mat
from tqdm import tqdm

from Define_Model.model import PairwiseDistance
from Process_Data.audio_processing import toMFB, totensor, truncatedinput, read_audio, ConcateVarInput
from TrainAndTest.common_func import create_model

# Version conflict

try:
    torch._utils._rebuild_tensor_v2
except AttributeError:
    def _rebuild_tensor_v2(storage, storage_offset, size, stride, requires_grad, backward_hooks):
        tensor = torch._utils._rebuild_tensor(storage, storage_offset, size, stride)
        tensor.requires_grad = requires_grad
        tensor._backward_hooks = backward_hooks
        return tensor

    torch._utils._rebuild_tensor_v2 = _rebuild_tensor_v2
import warnings

warnings.filterwarnings("ignore")

# Training settings
parser = argparse.ArgumentParser(description='Extract x-vector for plda')
# Model options
parser.add_argument('--enroll-dir', type=str, help='path to dataset')
parser.add_argument('--train-spk-a', type=int, metavar='NJOB', help='num of job')
parser.add_argument('--train-spk-b', type=int, metavar='NJOB', help='num of job')

parser.add_argument('--test-dir', type=str, help='path to voxceleb1 test dataset')
parser.add_argument('--sitw-dir', type=str, help='path to voxceleb1 test dataset')

parser.add_argument('--trials', type=str, default='trials', help='path to voxceleb1 test dataset')
parser.add_argument('--remove-vad', action='store_true', default=False, help='using Cosine similarity')
parser.add_argument('--extract', action='store_true', default=True, help='need to make mfb file')
parser.add_argument('--extract-path', type=str, help='need to make mfb file')

parser.add_argument('--nj', default=8, type=int, metavar='NJOB', help='num of job')
parser.add_argument('--feat-format', type=str, default='kaldi', choices=['kaldi', 'npy'],
                    help='number of jobs to make feats (default: 10)')

parser.add_argument('--check-path', default='Data/checkpoint/GradResNet8/vox1/spect_egs/soft_dp25',
                    help='folder to output model checkpoints')
parser.add_argument('--save-init', action='store_true', default=True, help='need to make mfb file')
parser.add_argument('--resume',
                    default='Data/checkpoint/GradResNet8/vox1/spect_egs/soft_dp25/checkpoint_10.pth', type=str,
                    metavar='PATH',
                    help='path to latest checkpoint (default: none)')

parser.add_argument('--start-epoch', default=1, type=int, metavar='N',
                    help='manual epoch number (useful on restarts)')
parser.add_argument('--epochs', type=int, default=20, metavar='E',
                    help='number of epochs to train (default: 10)')
parser.add_argument('--scheduler', default='multi', type=str,
                    metavar='SCH', help='The optimizer to use (default: Adagrad)')
parser.add_argument('--gamma', default=0.75, type=float,
                    metavar='GAMMA', help='The optimizer to use (default: Adagrad)')
parser.add_argument('--milestones', default='10,15', type=str,
                    metavar='MIL', help='The optimizer to use (default: Adagrad)')
parser.add_argument('--min-softmax-epoch', type=int, default=40, metavar='MINEPOCH',
                    help='minimum epoch for initial parameter using softmax (default: 2')
parser.add_argument('--veri-pairs', type=int, default=20000, metavar='VP',
                    help='number of epochs to train (default: 10)')

# Training options
# Model options
parser.add_argument('--model', type=str, help='path to voxceleb1 test dataset')
parser.add_argument('--resnet-size', default=8, type=int,
                    metavar='RES', help='The channels of convs layers)')
parser.add_argument('--filter', type=str, default='None', help='replace batchnorm with instance norm')
parser.add_argument('--transform', type=str, default='None', help='add a transform layer after embedding layer')

parser.add_argument('--vad', action='store_true', default=False, help='vad layers')
parser.add_argument('--inception', action='store_true', default=False, help='multi size conv layer')
parser.add_argument('--inst-norm', action='store_true', default=False, help='batchnorm with instance norm')
parser.add_argument('--input-norm', type=str, default='Mean', help='batchnorm with instance norm')
parser.add_argument('--encoder-type', type=str, default='SAP', help='path to voxceleb1 test dataset')
parser.add_argument('--channels', default='64,128,256', type=str,
                    metavar='CHA', help='The channels of convs layers)')
parser.add_argument('--feat-dim', default=64, type=int, metavar='N', help='acoustic feature dimension')
parser.add_argument('--input-dim', default=257, type=int, metavar='N', help='acoustic feature dimension')
parser.add_argument('--accu-steps', default=1, type=int, metavar='N', help='manual epoch number (useful on restarts)')

parser.add_argument('--alpha', default=12, type=float, metavar='FEAT', help='acoustic feature dimension')
parser.add_argument('--kernel-size', default='5,5', type=str, metavar='KE', help='kernel size of conv filters')
parser.add_argument('--padding', default='', type=str, metavar='KE', help='padding size of conv filters')
parser.add_argument('--stride', default='2', type=str, metavar='ST', help='stride size of conv filters')
parser.add_argument('--fast', action='store_true', default=False, help='max pooling for fast')

parser.add_argument('--cos-sim', action='store_true', default=False, help='using Cosine similarity')
parser.add_argument('--avg-size', type=int, default=4, metavar='ES', help='Dimensionality of the embedding')
parser.add_argument('--time-dim', default=2, type=int, metavar='FEAT', help='acoustic feature dimension')
parser.add_argument('--embedding-size', type=int, default=128, metavar='ES',
                    help='Dimensionality of the embedding')
parser.add_argument('--batch-size', type=int, default=128, metavar='BS',
                    help='input batch size for training (default: 128)')
parser.add_argument('--input-per-spks', type=int, default=224, metavar='IPFT',
                    help='input sample per file for testing (default: 8)')
parser.add_argument('--num-valid', type=int, default=5, metavar='IPFT',
                    help='input sample per file for testing (default: 8)')
parser.add_argument('--test-input-per-file', type=int, default=4, metavar='IPFT',
                    help='input sample per file for testing (default: 8)')
parser.add_argument('--test-batch-size', type=int, default=4, metavar='BST',
                    help='input batch size for testing (default: 64)')
parser.add_argument('--dropout-p', type=float, default=0.25, metavar='BST',
                    help='input batch size for testing (default: 64)')

# loss configure
parser.add_argument('--loss-type', type=str, default='soft', choices=['soft', 'asoft', 'center', 'amsoft',
                                                                      'wasse', 'mulcenter', 'arcsoft'],
                    help='path to voxceleb1 test dataset')
parser.add_argument('--num-center', type=int, default=2, help='the num of source classes')
parser.add_argument('--source-cls', type=int, default=1951,
                    help='the num of source classes')

parser.add_argument('--finetune', action='store_true', default=False,
                    help='using Cosine similarity')
parser.add_argument('--loss-ratio', type=float, default=0.1, metavar='LOSSRATIO',
                    help='the ratio softmax loss - triplet loss (default: 2.0')

# args for additive margin-softmax
parser.add_argument('--margin', type=float, default=0.3, metavar='MARGIN',
                    help='the margin value for the angualr softmax loss function (default: 3.0')
parser.add_argument('--s', type=float, default=15, metavar='S',
                    help='the margin value for the angualr softmax loss function (default: 3.0')

# args for a-softmax
parser.add_argument('--m', type=int, default=3, metavar='M',
                    help='the margin value for the angualr softmax loss function (default: 3.0')
parser.add_argument('--lambda-min', type=int, default=5, metavar='S',
                    help='random seed (default: 0)')
parser.add_argument('--lambda-max', type=float, default=1000, metavar='S',
                    help='random seed (default: 0)')

parser.add_argument('--lr', type=float, default=0.1, metavar='LR', help='learning rate (default: 0.125)')
parser.add_argument('--lr-decay', default=0, type=float, metavar='LRD',
                    help='learning rate decay ratio (default: 1e-4')
parser.add_argument('--weight-decay', default=5e-4, type=float,
                    metavar='WEI', help='weight decay (default: 0.0)')
parser.add_argument('--momentum', default=0.9, type=float,
                    metavar='MOM', help='momentum for sgd (default: 0.9)')
parser.add_argument('--dampening', default=0, type=float,
                    metavar='DAM', help='dampening for sgd (default: 0.0)')
parser.add_argument('--optimizer', default='sgd', type=str,
                    metavar='OPT', help='The optimizer to use (default: Adagrad)')
parser.add_argument('--grad-clip', default=10., type=float,
                    help='momentum for sgd (default: 0.9)')
# Device options
parser.add_argument('--no-cuda', action='store_true', default=False,
                    help='enables CUDA training')
parser.add_argument('--gpu-id', default='0', type=str,
                    help='id(s) for CUDA_VISIBLE_DEVICES')
parser.add_argument('--seed', type=int, default=123456, metavar='S',
                    help='random seed (default: 0)')
parser.add_argument('--log-interval', type=int, default=10, metavar='LI',
                    help='how many batches to wait before logging training status')

parser.add_argument('--acoustic-feature', choices=['fbank', 'spectrogram', 'mfcc'], default='fbank',
                    help='choose the acoustic features type.')
parser.add_argument('--makemfb', action='store_true', default=False,
                    help='need to make mfb file')
parser.add_argument('--makespec', action='store_true', default=False,
                    help='need to make spectrograms file')

args = parser.parse_args()

# Set the device to use by setting CUDA_VISIBLE_DEVICES env variable in
# order to prevent any memory allocation on unused GPUs
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_id

args.cuda = not args.no_cuda and torch.cuda.is_available()
np.random.seed(args.seed)
torch.manual_seed(args.seed)

if args.cuda:
    cudnn.benchmark = True

# create logger
# Define visulaize SummaryWriter instance

kwargs = {'num_workers': 12, 'pin_memory': True} if args.cuda else {}
l2_dist = nn.CosineSimilarity(dim=1, eps=1e-6) if args.cos_sim else PairwiseDistance(2)

if args.acoustic_feature == 'fbank':
    transform = transforms.Compose([
        ConcateVarInput(),
    ])
    transform_T = transforms.Compose([
        ConcateVarInput(),
    ])
    file_loader = read_mat
else:
    transform = transforms.Compose([
        truncatedinput(),
        toMFB(),
        totensor(),
        # tonormal()
    ])
    file_loader = read_audio

# pdb.set_trace()
enroll_dir = KaldiExtractDataset(dir=args.enroll_dir, filer_loader=file_loader, transform=transform)
test_dir = KaldiExtractDataset(dir=args.test_dir, filer_loader=file_loader, transform=transform)

# test_dir = ScriptTestDataset(dir=args.test_dir, loader=file_loader, transform=transform_T)

def extract(data_loader, model, set_id, extract_path):
    model.eval()
    # uids, xvector = [], []
    if not os.path.exists(extract_path):
        os.makedirs(extract_path)

    if not os.path.exists(extract_path + '/npy_vectors'):
        os.makedirs(extract_path + '/npy_vectors')

    pbar = tqdm(enumerate(data_loader))

    with open(extract_path + '/utt2vec', 'w') as scp:

        with torch.no_grad():
            batch_data = torch.tensor([]).reshape(0, 1, 300, 81)
            tmp_len = [0]
            tmp_uids = []
            for batch_idx, (data, uid) in pbar:

                vec_shape = data.shape
                # print(data.shape)
                if vec_shape[1] != 1:
                    data = data.reshape(vec_shape[0] * vec_shape[1], 1, vec_shape[2], vec_shape[3])
                # print(data.shape)
                if len(batch_data) < args.batch_size and batch_idx + 1 < len(data_loader.dataset):

                    batch_data = torch.cat((batch_data, data), dim=0)
                    tmp_len.append(tmp_len[-1] + len(data))
                    tmp_uids.append(uid[0])
                else:
                    # print(tmp_len)
                    batch_data = batch_data.cuda()
                    _, feats = model(batch_data)
                    if torch.isnan(feats).int().sum() > 0:
                        print("Nan detected!")

                    feats = feats.data.cpu().numpy()

                    for i in range(len(tmp_uids)):
                        this_feat = feats[tmp_len[i]:tmp_len[i + 1]]
                        xvector_path = os.path.join(extract_path, 'npy_vectors', tmp_uids[i] + '.npy')

                        np.save(xvector_path, this_feat)
                        scp.write(tmp_uids[i] + " " + xvector_path + '\n')

                    batch_data = torch.tensor([]).reshape(0, 1, 300, 81)
                    tmp_len = [0]
                    tmp_uids = []

                if batch_idx % args.log_interval == 0:
                    pbar.set_description(
                        'Extract {}: [{:8d}/{:8d} ({:3.0f}%)] '.format(
                            set_id,
                            batch_idx,
                            len(data_loader.dataset),
                            100. * batch_idx / len(data_loader)))
                # break

def main():
    # Views the training images and displays the distance on anchor-negative and anchor-positive

    # print the experiment configuration
    print('\nCurrent time is\33[91m {}\33[0m.'.format(str(time.asctime())))
    print('Parsed options: {}'.format(vars(args)))
    print('Number of Speakers A: {}\n'.format(args.train_spk_a))
    print('Number of Speakers B: {}\n'.format(args.train_spk_b))

    # instantiate model and initialize weights
    kernel_size = args.kernel_size.split(',')
    kernel_size = [int(x) for x in kernel_size]
    if args.padding == '':
        padding = [int((x - 1) / 2) for x in kernel_size]
    else:
        padding = args.padding.split(',')
        padding = [int(x) for x in padding]

    kernel_size = tuple(kernel_size)
    padding = tuple(padding)
    stride = args.stride.split(',')
    stride = [int(x) for x in stride]

    channels = args.channels.split(',')
    channels = [int(x) for x in channels]

    model_kwargs = {'input_dim': args.input_dim, 'feat_dim': args.feat_dim, 'kernel_size': kernel_size,
                    'filter': args.filter, 'inst_norm': args.inst_norm, 'input_norm': args.input_norm,
                    'stride': stride, 'fast': args.fast, 'avg_size': args.avg_size, 'time_dim': args.time_dim,
                    'padding': padding, 'encoder_type': args.encoder_type, 'vad': args.vad,
                    'transform': args.transform, 'embedding_size': args.embedding_size, 'ince': args.inception,
                    'resnet_size': args.resnet_size, 'num_classes_a': args.train_spk_a,
                    'num_classes_b': args.train_spk_b,
                    'channels': channels, 'alpha': args.alpha, 'dropout_p': args.dropout_p}

    print('Model options: {}'.format(model_kwargs))
    model = create_model(args.model, **model_kwargs)

    if args.cuda:
        model.cuda()

    if os.path.isfile(args.resume):
        # print('=> loading checkpoint {}'.format(resume))
        checkpoint = torch.load(args.resume)
        filtered = {k: v for k, v in checkpoint['state_dict'].items() if 'num_batches_tracked' not in k}
        model_dict = model.state_dict()
        model_dict.update(filtered)
        model.load_state_dict(model_dict)

    else:
        raise Exception('=> no checkpoint found at {}'.format(args.resume))

    # Extract enroll set vectors
    # train_loader = torch.utils.data.DataLoader(train_dir, batch_size=args.batch_size, shuffle=False, **kwargs)
    enroll_loader = torch.utils.data.DataLoader(enroll_dir, batch_size=1, shuffle=False, **kwargs)
    extract(enroll_loader, model, set_id='enroll', extract_path=args.extract_path + '/x_vector/enrollled')

    # Extract test set vectors
    test_loader = torch.utils.data.DataLoader(test_dir, batch_size=1, shuffle=False, **kwargs)
    extract(test_loader, model, set_id='test', extract_path=args.extract_path + '/x_vector/notenrollled')

    print('Extract x-vector completed for train and test in %s!\n' % (args.extract_path + '/x_vector'))

if __name__ == '__main__':
    main()


#-*- coding: utf-8 -*-

def binary_search(target, data):
    data = sorted(data)
    start = 0
    end = len(data) - 1

    while start<=end:
        mid = (start+end)//2
        if data[mid] == target:
            return mid
        elif data[mid] < target:
            start = mid + 1
        else:
            end = mid - 1
    return None

if __name__ == '__main__':
    li = [i ** 2 for i in range(11)]
    target = 9
    idx = binary_search(target, li)
    if idx:
        print(li[idx])
    else:
        print("Not found target {}".format(target))


from pathlib import Path
from typing import Optional

import typer

admin_app = typer.Typer()

@admin_app.command("initialize-database")
def initialize_database(database_uri: str):
    """
    Initialize a SQL database for use by Tiled.
    """
    from sqlalchemy import create_engine

    from ..database.core import (
        REQUIRED_REVISION,
        UninitializedDatabase,
        check_database,
        initialize_database,
    )

    engine = create_engine(database_uri)
    redacted_url = engine.url._replace(password="[redacted]")
    try:
        check_database(engine)
    except UninitializedDatabase:
        # Create tables and stamp (alembic) revision.
        typer.echo(
            f"Database {redacted_url} is new. Creating tables and marking revision {REQUIRED_REVISION}.",
            err=True,
        )
        initialize_database(engine)
        typer.echo("Database initialized.", err=True)
    else:
        typer.echo(f"Database at {redacted_url} is already initialized.", err=True)
        raise typer.Abort()

@admin_app.command("upgrade-database")
def upgrade_database(
    database_uri: str,
    revision: Optional[str] = typer.Argument(
        None,
        help="The ID of a revision to upgrade to. By default, upgrade to the latest one.",
    ),
):
    """
    Upgrade the database schema to the latest version.
    """
    from sqlalchemy import create_engine

    from ..database.core import get_current_revision, upgrade

    engine = create_engine(database_uri)
    redacted_url = engine.url._replace(password="[redacted]")
    current_revision = get_current_revision(engine)
    if current_revision is None:
        # Create tables and stamp (alembic) revision.
        typer.echo(
            f"Database {redacted_url} has not been initialized. Use `tiled admin initialize-database`.",
            err=True,
        )
        raise typer.Abort()
    upgrade(engine, revision or "head")

@admin_app.command("downgrade-database")
def downgrade_database(
    database_uri: str,
    revision: str = typer.Argument(..., help="The ID of a revision to downgrade to."),
):
    """
    Upgrade the database schema to the latest version.
    """
    from sqlalchemy import create_engine

    from ..database.core import downgrade, get_current_revision

    engine = create_engine(database_uri)
    redacted_url = engine.url._replace(password="[redacted]")
    current_revision = get_current_revision(engine)
    if current_revision is None:
        # Create tables and stamp (alembic) revision.
        typer.echo(
            f"Database {redacted_url} has not been initialized. Use `tiled admin initialize-database`.",
            err=True,
        )
        raise typer.Abort()
    downgrade(engine, revision)

@admin_app.command("check-config")
def check_config(
    config_path: Path = typer.Argument(
        None,
        help=(
            "Path to a config file or directory of config files. "
            "If None, check environment variable TILED_CONFIG. "
            "If that is unset, try default location ./config.yml."
        ),
    ),
):
    "Check configuration file for syntax and validation errors."
    import os

    from ..config import parse_configs

    config_path = config_path or os.getenv("TILED_CONFIG", "config.yml")
    try:
        parse_configs(config_path)
    except Exception as err:
        typer.echo(str(err), err=True)
        raise typer.Exit(1)
    typer.echo("No errors found in configuration.")


import matplotlib.pyplotaspltimportnumpyasn
from topolar import topolar
from skimage.data import chelsea
from scipy.ndimage.interpolation import geometric_transform

def topolar(img,order=1):
    max_radius = 0.5*np.linalg.norm(img.shape)

    def transform(coords):
        theta = 2*np.pi*coords[1]/(img.shape[1]-1.)
        radius = max_radius*coords[0]/img.shape[0]
        i = 0.5*img.shape[0]-radius*np.sin(theta)
        j = radius*np.cos(theta)+0.5*img.shape[1]
        return i,j

    polar = geometric_transform(img, transform, order=order)
    rads = max_radius*np.linspace(0,1,img.shape[0])
    angs = np.linspace(0,2*np.pi,img.shape[1])
    return polar, (rads,angs)

img = chelsea()[...,0]/255.
pol, (rads, angs) = topolar(img)
fig,ax = plt.subplots(2, 1, figsize=(6.8))


# coding: utf-8

# In[1]:

#Pull in Packages
get_ipython().run_line_magic('matplotlib', 'inline')
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#From Scikit Learn
from sklearn import preprocessing
from sklearn.model_selection  import train_test_split, cross_val_score, KFold
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.metrics import roc_curve, auc, confusion_matrix, classification_report

# In[2]:

#Change Directory
import os
path="/Users/emmydoo19/Desktop/BIA6303/"
os.chdir(path)

# In[3]:

#Pulling in Churn Data
Churn=pd.read_csv("Churn_Calls.csv")
Churn.head(10)

# In[4]:

# designate target variable name
targetName = 'churn'
targetSeries = Churn[targetName]
#remove target from current location and insert in column 0
del Churn[targetName]
Churn.insert(0, targetName, targetSeries)
#reprint dataframe and see target is in position 0
Churn.head(10)
Churn.shape

# In[5]:

#EDA
Churn.info()

#Checking to see the data types, colums, and shape of the data.

# In[6]:

Churn.describe()
#Checking the Count, Mean, St Dev, Min, Max, and Quartiles.
#Good quick info to grab and decide if I want to normalize or not.

# In[7]:

Churn.dtypes

#Need to be coginizant of the fact I'm working with Int, Float, and Objects.
#Will have to do some transformation on the ojects to run through any models.

# In[8]:

groupby = Churn.groupby(targetName)
targetEDA=groupby[targetName].aggregate(len)
plt.figure()
targetEDA.plot(kind='bar', grid=False)
plt.axhline(0, color='k')

#Did a quick plot to visualize the data, can see its an unbalanced data set and only 15% of the time does churn happen.
#If the model doesnt do better than 0.85% accuracy the we are better off guessing.

# In[9]:

#Dummy Variables and replacing Null Values

from sklearn import preprocessing
le_dep = preprocessing.LabelEncoder()
Churn['churn'] = le_dep.fit_transform(Churn['churn'])

for col in Churn.columns[1:]:
	attName = col
	dType = Churn[col].dtype
	missing = pd.isnull(Churn[col]).any()
	# discretize (create dummies)
	if dType == object:
		Churn = pd.concat([Churn, pd.get_dummies(Churn[col], prefix=col)], axis=1)
		del Churn[attName]

Median=Churn.median()
Churn = Churn.fillna(Median)

Churn.shape
Churn.head(10)

# In[10]:

#Train Test Split

features_train, features_test, target_train, target_test = train_test_split(
    Churn.iloc[:,1:].values, Churn.iloc[:,0].values, test_size=0.30, random_state=0)

# In[11]:

#check shape of the new data split out

print(features_test.shape)
print(features_train.shape)
print(target_test.shape)
print(target_train.shape)

# In[12]:

#Decision Tree train model.

from sklearn import tree
clf_dt = tree.DecisionTreeClassifier()

#Fit clf to the training data
clf_dt = clf_dt.fit(features_train, target_train)
#Predict clf DT model again test data
target_predicted_dt = clf_dt.predict(features_test)

# In[13]:

#Confusion Matrix, Accuracy Score, and other metrics
print("DT Accuracy Score", accuracy_score(target_test, target_predicted_dt))
print(classification_report(target_test, target_predicted_dt))
print(confusion_matrix(target_test, target_predicted_dt))

# In[14]:

#Change Gini to Entropy in Desicion Tree

from sklearn import tree
clf_dt2 = tree.DecisionTreeClassifier(criterion='entropy')
clf_dt2 = clf_dt2.fit(features_train, target_train)
target_predicted_dt2 = clf_dt2.predict(features_test)

# In[15]:

#Confusion Matrix, Accuracy Score, and other metrics
print("DT Accuracy Score", accuracy_score(target_test, target_predicted_dt2))
print(classification_report(target_test, target_predicted_dt2))
print(confusion_matrix(target_test, target_predicted_dt2))

# In[16]:

#Change Splitter from best to random best

from sklearn import tree
clf_dt3 = tree.DecisionTreeClassifier(splitter='random')
clf_dt3 = clf_dt3.fit(features_train, target_train)
target_predicted_dt3 = clf_dt3.predict(features_test)

# In[17]:

#Confusion Matrix, Accuracy Score, and other metrics
print("DT Accuracy Score", accuracy_score(target_test, target_predicted_dt3))
print(classification_report(target_test, target_predicted_dt3))
print(confusion_matrix(target_test, target_predicted_dt3))

# In[18]:

#Cross Validation for original desicion tree
scores = cross_val_score(clf_dt, features_train, target_train, cv=10)
print("Cross Validation Score for each K",scores)
scores.mean()

# In[19]:

#Knn Model

from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier(n_neighbors=3)
model = model.fit(features_train, target_train)
predicted_model = model.predict(features_test)

# In[20]:

print("KNN Accuracy Score", accuracy_score(target_test, predicted_model))
print(classification_report(target_test, predicted_model))
print(confusion_matrix(target_test, predicted_model))

# In[26]:

#Cross Validation KNN
KNNscores = cross_val_score(model, features_train, target_train, cv=10)
print("Cross Validation Score for each K",scores)
KNNscores.mean()

#KNN Scores stayed about the same as the orginal model.
#Probably isnt the best model to run for this data set if both run pretty poorly with mariginal changes and CV

# In[29]:

#RF Model
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators= 500)
rf.fit(features_train, target_train)
# test random forest model
target_predicted_rf = rf.predict(features_test)
#print accuracy_score(target_test, target_predicted_rf)
target_names = [" No", "Yes"]
print(classification_report(target_test, target_predicted_rf, target_names=target_names))
print("RF Accuracy", accuracy_score(target_test, target_predicted_rf))

# In[31]:

#Cross Validation for RF Forest Model
#Cross Validation RF
RFscores = cross_val_score(rf, features_train, target_train, cv=10)
print("Cross Validation Score for RF",scores)
RFscores.mean()


from orm_connection.orm_session import MysqlSvr
from orm_connection.kbl_basketball import BleagueNblBasketballStage, BleagueNblBasketballSeason

spx_dev_session = MysqlSvr.get('spider_zl')
years_list = [2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
id = 1
for year in years_list:
    sport_id = 2
    season = str(year) + '-' + str(year + 1)
    season_id = \
        spx_dev_session.query(BleagueNblBasketballSeason).filter(BleagueNblBasketballSeason.season == season).all()[
            0].id
    name_zh = season + '赛季'
    has_player_stats = 1
    has_team_stats = 1
    mode = 5
    data = {
        'id': int(id),
        'season_id': int(season_id),
        'sport_id': int(sport_id),
        'name_zh': str(name_zh),
        'mode': int(mode),
    }
    BleagueNblBasketballStage.upsert(
        spx_dev_session,
        'id',
        data
    )
    id += 1


import csv
import os
import shutil
from time import time

mimic_data_path = "/home/mattyws/Documentos/mimic/data/"
csv_file_path = mimic_data_path+"csv/"
files = [
    # 'OUTPUTEVENTS', 'CHARTEVENTS', 'PROCEDURES_ICD', 'MICROBIOLOGYEVENTS',
    # 'LABEVENTS', 'DIAGNOSES_ICD', 'NOTEEVENTS', 'PRESCRIPTIONS', 'CPTEVENTS',
    'INPUTEVENTS_CV', 'INPUTEVENTS_MV'
]

def writeBuffer(writer, buffer):
    for data in buffer:
        writer.writerow(data)

def closeFilePointer(files_dict):
    for key in files_dict.keys():
        files_dict[key]['file'].close()

for file in files:
    print("========================================= {} ====================================================".format(file))
    data_path = mimic_data_path+file
    if not os.path.exists(data_path):
        os.mkdir(data_path)
    else:
        start = time()
        print("Deleting created directory -----------------")
        shutil.rmtree(data_path)
        end = time()
        print("Took {} seconds to delete the directory.".format(end-start))
        os.mkdir(data_path)
    with open(csv_file_path+file+'.csv', 'r') as csv_source_file:
        reader = csv.DictReader(csv_source_file)
        buffer = []
        print("Iteration on file -----------------")
        start = time()
        total_rows = 0
        empty_admission = 0
        files_dict = dict()
        for row in reader:
            total_rows += 1
            if total_rows % 100000 == 0:
                print("{} total rows processed".format(total_rows))
            if len(row['HADM_ID']) == 0:
                empty_admission += 1
            #Write buffer
            file_path = data_path+"/{}_{}.csv".format(file, row["HADM_ID"])
            if row["HADM_ID"] not in files_dict.keys():
                files_dict[row["HADM_ID"]] = dict()
                files_dict[row["HADM_ID"]]['file'] = open(file_path, 'w')
                files_dict[row["HADM_ID"]]['csv'] = csv.DictWriter(open(file_path, 'w'), row.keys())
                files_dict[row["HADM_ID"]]['csv'].writeheader()
            files_dict[row["HADM_ID"]]['csv'].writerow(row)
            # if not os.path.exists(file):
            #     with open(file_path, 'w+') as csv_new_file:
            #         writer = csv.DictWriter(csv_new_file, row.keys())
            #         writer.writeheader()
            #         writer.writerow(row)
            # else:
            #     with open(file_path, "a+") as csv_existed_file:
            #         writer = csv.DictWriter(csv_existed_file, row.keys())
            #         writer.writerow(row)
        end = time()
        print("Took {} seconds to process file. Total rows processed {}, and {} rows with empty admission id.".
              format(end-start, total_rows, empty_admission))
        closeFilePointer(files_dict)


from bottle import request, response, route, run, static_file
from io import BytesIO
import json
from qrcode import make
from werewolf import game

game = game()

@route('/')
def index():
	ip = request.environ.get('REMOTE_ADDR')
	if game.state is game.GAME_INACTIVE:
		if game.host_player is None:
			if 'start' in request.query:
				game.host_player = ip
				game.state = game.GAME_SETUP
				filename = 'setup.html'
			else:
				filename = 'start.html'
		return static_file(filename, root='static/')

@route('/roles/')
def roles():
	return json.dumps(game.roles.keys())

@route('/presets/')
def presets():
	presets = {}
	for (name, roles) in game.presets.iteritems():
		presets[name] = []
		for role in roles:
			presets[name].append({str(role): role.__dict__.values()})
	return json.dumps(presets)

@route('/qr.jpg')
def qr():
	out = BytesIO()
	qr = make('http://%s' % request.get_header('host'))
	response.set_header('Content-type', 'image/jpeg')
	qr.save(out, format='JPEG')
	return out.getvalue()

run(host='0.0.0.0', port=8082)


import re

email = input('Informe seu email: ')
padrao = r'[a-z0-9.]+@[a-z0-9]+\.[a-z]*'

resultado = re.findall(padrao, email)

if resultado is not None and len(resultado) > 0:
    print(resultado)
else:
    print('\033[91mPadrao não validado\033[0m')

telefone = input('Informe seu telefone: ')
padrao = r'\(\d{2}\) \d{4,5}-\d{4}|\d{4,5}-\d{4}|\d{2} \d{4,5}-\d{4}'

resultado = re.findall(padrao, telefone)

if resultado is not None and len(resultado) > 0:
    print(re.sub('[()-]|\s', '', telefone))
else:
    print('\033[91mPadrao não validado\033[0m')

cpf = input('Digite seu cpf: ')
padrao = r'\d{3}\.\d{3}\.\d{3}-\d{2}|^\d{11}$|\d{3}\s\d{3}\s\d{3}-\d{2}|\d{3}\.\d{3}\.\d{3}\/\d{2}'

resultado = re.findall(padrao, cpf)
print(resultado)

#MAGENTA = '\033[95m'
#CYAN = '\033[96m'
#BLUE = '\033[94m'
#DARKCYAN = '\033[36m'
#GREEN = '\033[92m'
#YELLOW = '\033[93m'
#RED = '\033[91m'
#BOLD = '\033[1m'
#UNDERLINE = '\033[4m'
#END = '\033[0m'
#https://raccoon.ninja/pt/dev-pt/tabela-de-cores-ansi-python/


"""
Main interface module to use pyEPR.

Contains code to conenct to Ansys and to analyze HFSS files using the EPR method.

This module handles the micowave part of the analysis and conenction to

Further contains code to be able to do autogenerated reports,

Copyright Zlatko Minev, Zaki Leghtas, and the pyEPR team
2015, 2016, 2017, 2018, 2019, 2020
"""

from __future__ import print_function  # Python 2.7 and 3 compatibility

import sys
from pathlib import Path

import pandas as pd

from . import Dict, ansys, config, logger
from .toolbox.pythonic import get_instance_vars

class ProjectInfo(object):
    """
    Class containing options and information about the manipulation and analysis in HFSS.

    Junction info:
    -----------------------
        self.junctions : OrderedDict()

        A Josephson tunnel junction has to have its parameters specified here for the analysis.
        Each junction is given a name and is specified by a dictionary.
        It has the following properties:

            `Lj_variable`: Name of HFSS variable that specifies junction inductance Lj defined
                        on the boundary condition in HFSS. DO NOT USE Global names that start
                        with $.
            `rect`:     Name of HFSS rectangle on which lumped boundary condition is specified.
            `line`:     Name of HFSS polyline which spans the length of the recntalge.
                        Used to define the voltage across the junction.
                        Used to define the current orientation for each junction.
                        Used to define sign of ZPF.
            `length`:   Length in HFSS of the junction rectangle and line
                        (specified in meters). You can use epr.parse_units('100um')
            `Cj_variable` : Experimental. Optional.
                        Name of HFSS variable that specifies junction inductance Cj defined
                        on the boundary condition in HFSS. DO NOT USE Global names that start
                        with $.

        Example definition:

        ..code-block python

            # Define a single junction
            pinfo = ProjectInfo('')
            pinfo.junctions['j1'] = {'Lj_variable' : 'Lj1',
                         'rect'        : 'JJrect1',
                         'line'        : 'JJline1',
                         'length'      : parse_units('50um')} # Length is in meters

            # Specify multiple junctions in HFSS model
            n_junctions = 5
            for i in range(1, 1+n_junctions):
                pinfo.junctions[f'j{i}'] = {'Lj_variable' : f'Lj{i}',
                                            'rect'        : f'JJrect{i}',
                                            'line'        : f'JJline{i}',
                                            'length'      : parse_units('50um')}

    HFSS app connection settings
    -----------------------
        project_path  : str
            Directory path to the hfss project file. Should be the directory, not the file.
            default = None: Assumes the project is open, and thus gets the project based
            on `project_name`
        project_name  : str,  None
            Name of the project within the project_path. "None" will get the current active one.
        design_name   : str,  None
            Name of the design within the project. "None" will get the current active one.
        setup_name    : str,  None
            Name of the setup within the design. "None" will get the current active one.

    Additional init setting:
    -----------------------
        do_connect : True by default. Connect to HFSS

    HFSS desgin settings
    -----------------------
    describe junction parameters
        junc_rects    = None
            Name of junction rectangles in HFSS
        junc_lines    = None
            Name of lines in HFSS used to define the current orientation for each junction
        junc_LJ_names = None
            Name of junction inductance variables in HFSS.
            Note, DO NOT USE Global names that start with $.
        junc_lens     = None
            Junciton rect. length, measured in meters.

    """

    class _Dissipative:
        # TODO: remove and turn to dict

        def __init__(self):
            self.dielectrics_bulk = None
            self.dielectric_surfaces = None
            self.dielectric_MA_surfaces = None
            self.dielectric_MS_surfaces = None
            self.dielectric_SA_surfaces = None
            self.resistive_surfaces = None
            self.seams = None

    def __init__(self, project_path=None, project_name=None, design_name=None,
                 setup_name=None, do_connect=True):

        # Path: format path correctly to system convention
        self.project_path = str(Path(project_path)) \
            if not (project_path is None) else None
        self.project_name = project_name
        self.design_name = design_name
        self.setup_name = setup_name

        # HFSS desgin: describe junction parameters
        # TODO: introduce modal labels
        self.junctions = Dict()  # See above for help
        self.ports = Dict()

        # Dissipative HFSS volumes and surfaces
        self.dissipative = self._Dissipative()
        self.options = config.ansys

        # Conected to HFSS variable
        self.app = None
        self.desktop = None
        self.project = None
        self.design = None
        self.setup = None

        if do_connect:
            self.connect()

    _Forbidden = ['app', 'design', 'desktop', 'project',
                  'dissipative', 'setup', '_Forbidden', 'junctions']

    def save(self):
        '''
        Return a dicitonary to save
        '''
        return dict(
            pinfo=pd.Series(get_instance_vars(self, self._Forbidden)),
            dissip=pd.Series(get_instance_vars(self.dissipative)),
            options=pd.Series(get_instance_vars(self.options)),
            junctions=pd.DataFrame(self.junctions),
            ports=pd.DataFrame(self.ports),
        )

    def connect(self):
        """
        Connect to Ansys Desktop.
        """
        logger.info('Connecting to Ansys Desktop API...')

        self.app, self.desktop, self.project = ansys.load_ansys_project(
            self.project_name, self.project_path)
        self.project_name = self.project.name
        self.project_path = self.project.get_path()

        # Design
        if self.design_name is None:
            self.design = self.project.get_active_design()
            self.design_name = self.design.name
            logger.info(f'\tOpened active design\n\
\tDesign:    {self.design_name} [Solution type: {self.design.solution_type}]')
        else:

            try:
                self.design = self.project.get_design(self.design_name)
                logger.info(f'\tOpened active design\n\
\tDesign:    {self.design_name} [Solution type: {self.design.solution_type}]')

            except Exception as e:
                _traceback = sys.exc_info()[2]
                logger.error(f"Original error \N{loudly crying face}: {e}\n")
                raise(Exception(' Did you provide the correct design name?\
                    Failed to pull up design. \N{loudly crying face}').with_traceback(_traceback))

        # Setup
        try:
            setup_names = self.design.get_setup_names()

            if len(setup_names) == 0:
                logger.warning('\tNo design setup detected.')
                if self.design.solution_type == 'Eigenmode':
                    logger.warning('\tCreating eigenmode default setup one.')
                    setup = self.design.create_em_setup()
                    self.setup_name = setup.name
                elif self.design.solution_type == 'DrivenModal':
                    setup = self.design.create_dm_setup()  # adding a driven modal design
                    self.setup_name = setup.name
            else:
                if self.setup_name not in setup_names:
                    logger.warning('\tUser specified design setup does not exist. Utilizing the default satup.')
                    self.setup_name = setup_names[0]

            # get the actual setup if there is one
            self.get_setup(self.setup_name)

        except Exception as e:

            _traceback = sys.exc_info()[2]
            logger.error(f"Original error \N{loudly crying face}: {e}\n")
            raise Exception(' Did you provide the correct setup name?\
                        Failed to pull up setup. \N{loudly crying face}').with_traceback(_traceback)

        # Finalize
        self.project_name = self.project.name
        self.design_name = self.design.name

        logger.info(
            '\tConnection to Ansys established successfully. \N{grinning face} \n')

        return self

    def get_setup(self, name):
        """
        Connects to a specific setup for the design.
        Sets  self.setup and self.setup_name.
        If Name is
        """
        if name is None:
            return None
        else:
            self.setup = self.design.get_setup(name=self.setup_name)
            if self.setup is None:
                logger.error(f"Could not retrieve setup: {self.setup_name}\n \
                               Did you give the right name? Does it exist?")

            self.setup_name = self.setup.name
            logger.info(
                f'\tOpened setup `{self.setup_name}`  ({type(self.setup)})')
            return self.setup

    def check_connected(self):
        """Checks if fully connected including setup
        """
        return\
            (self.setup is not None) and\
            (self.design is not None) and\
            (self.project is not None) and\
            (self.desktop is not None) and\
            (self.app is not None)

    def disconnect(self):
        '''
        Disconnect from existing HFSS design.
        '''
        assert self.check_connected() is True,\
            "It does not appear that you have connected to HFSS yet.\
            Use the connect()  method. \N{nauseated face}"
        self.project.release()
        self.desktop.release()
        self.app.release()
        ansys.release()

    # UTILITY FUNCTIONS

    def get_dm(self):
        '''
        Utility shortcut function to get the design and modeler.

        .. code-block:: python
            oDesign, oModeler = projec.get_dm()
        '''
        return self.design, self.design.modeler

    def get_all_variables_names(self):
        """Returns array of all project and local design names."""
        return self.project.get_variable_names() + self.design.get_variable_names()

    def get_all_object_names(self):
        """Returns array of strings"""
        o_objects = []
        for s in ["Non Model", "Solids", "Unclassified", "Sheets", "Lines"]:
            o_objects += self.design.modeler.get_objects_in_group(s)
        return o_objects

    def validate_junction_info(self):
        """ Validate that the user has put in the junction info correctly.
        Do no also forget to check the length of the rectangles/line of
        the junction if you change it.
        """

        all_variables_names = self.get_all_variables_names()
        all_object_names = self.get_all_object_names()

        for jjnm, jj in self.junctions.items():

            assert jj['Lj_variable'] in all_variables_names,\
                """pyEPR ProjectInfo user error found \N{face with medical mask}:
                Seems like for junction `%s` you specified a design or project
                variable for `Lj_variable` that does not exist in HFSS by the name:
                 `%s` """ % (jjnm, jj['Lj_variable'])

            for name in ['rect', 'line']:

                assert jj[name] in all_object_names, \
                    """pyEPR ProjectInfo user error found \N{face with medical mask}:
                    Seems like for junction `%s` you specified a %s that does not exist
                    in HFSS by the name: `%s` """ % (jjnm, name, jj[name])

        # TODO: Check the length of the rectangle


from django.contrib import admin
from django.urls import path, include
from django.urls.conf import re_path
from rest_framework.routers import DefaultRouter
from authapp.views import UserModelViewSet
from todoapp.views import *
from rest_framework.authtoken import views
from rest_framework.permissions import AllowAny
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from graphene_django.views import GraphQLView

router = DefaultRouter()
router.register('user', UserModelViewSet)
router.register('todo', TodoModelViewSet)
router.register('project', ProjectModelViewSet)
router.register('project-users', ProjectUsersViewSet)

schema_view = get_schema_view(
    openapi.Info(
        title="Library",
        default_version='0.1',
        description="Documentation to out project",
        contact=openapi.Contact(email="admin@admin.local"),
        license=openapi.License(name="MIT License"),
    ),
    public=True,
    permission_classes=[AllowAny],
)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api-auth/', include('rest_framework.urls')),
    # path('api/', include(router.urls)),
    path('api-token-auth/', views.obtain_auth_token),
    path('api/<str:version>/', include(router.urls)),
    path('swagger/', schema_view.with_ui('swagger',
         cache_timeout=0), name='schema-swagger-ui'),
    re_path('^swagger(?P<format>\.json|\.yaml)$',
            schema_view.without_ui(cache_timeout=0), name='schema-json'),
    path('redoc/', schema_view.with_ui('redoc', cache_timeout=0),
         name='schema-redoc'),
    path("graphql/", GraphQLView.as_view(graphiql=True)),
]


#Assignment: Functions Intermediate I

# Update Values in Dictionaries and Lists
x = [ [5,2,3], [10,8,9] ]
students = [
    {'first_name':  'Michael', 'last_name' : 'Jordan'},
    {'first_name' : 'John', 'last_name' : 'Rosales'}
]
sports_directory = {
    'basketball' : ['Kobe', 'Jordan', 'James', 'Curry'],
    'soccer' : ['Messi', 'Ronaldo', 'Rooney']
}
z = [ {'x': 10, 'y': 20} ]

x[1][0] = 15
print(x)

students[0]['last_name'] = "Bryant"
print(students)

sports_directory['soccer'][0] = 'Andres'
print( sports_directory['soccer'])

z[0]['y'] = 30
print(z)

# Iterate Through a List of Dictionaries
students = [
        {'first_name':  'Michael', 'last_name' : 'Jordan'},
        {'first_name' : 'John', 'last_name' : 'Rosales'},
        {'first_name' : 'Mark', 'last_name' : 'Guillen'},
        {'first_name' : 'KB', 'last_name' : 'Tonel'}
    ]

def iterate_dictionary(list):
    for i in range(0, len(list)):
        output = ""
        for key,val in list[i].items():
            output += f" {key} - {val},"
        print(output)

iterate_dictionary(students)
# should output: (it's okay if each key-value pair ends up on 2 separate lines;
# bonus to get them to appear exactly as below!)
# first_name - Michael, last_name - Jordan
# first_name - John, last_name - Rosales
# first_name - Mark, last_name - Guillen
# first_name - KB, last_name - Tonel

# # Get Values From a List of Dictionaries
# Michael
# John
# Mark
# KB

# Jordan
# Rosales
# Guillen
# Tonel

#Iterate Through a Dictionary with List Values
dojo = {
'locations': ['San Jose', 'Seattle', 'Dallas', 'Chicago', 'Tulsa', 'DC', 'Burbank'],
'instructors': ['Michael', 'Amy', 'Eduardo', 'Josh', 'Graham', 'Patrick', 'Minh', 'Devon']
}

def print_info(dict):
    for key,val in dict.items():
        print(f"{len(val)} {key.upper()}")
        for i in range(0, len(val)):
            print(val[i])
print_info(dojo)
# output:
# 7 LOCATIONS
# San Jose
# Seattle
# Dallas
# Chicago
# Tulsa
# DC
# Burbank

# 8 INSTRUCTORS
# Michael
# Amy
# Eduardo
# Josh
# Graham
# Patrick
# Minh
# Devon