# -*- coding: utf-8 -*-
import os

import pytest
from alchy.model import extend_declarative_base

from intertwine import IntertwineModel, create_app, intertwine_db
from intertwine.trackable import Trackable
from config import ToxConfig

@pytest.fixture(scope="module")
def options():
    """Capture configuration data and validate data is available"""
    options = {
        'host': '127.0.0.1',
        'port': 5000,
        'config': ToxConfig(),
    }

    return options

# TODO: Update for alchy
TESTDB = 'test-sqlite.db'
TESTDB_FOLDER = os.path.dirname(os.path.realpath(__file__))
TESTDB_PATH = os.path.join(TESTDB_FOLDER, TESTDB)
TEST_DATABASE_URI = 'sqlite:///' + TESTDB_PATH

class PyTestConfig(ToxConfig):
    SQLALCHEMY_DATABASE_URI = TEST_DATABASE_URI
    # SQLALCHEMY_TRACK_MODIFICATIONS = False

@pytest.fixture(scope='function')
def app(request):
    """Create session-wide test `Flask` application"""
    app = create_app(__name__, PyTestConfig)

    # Establish an application context before running the tests.
    ctx = app.app_context()
    ctx.push()

    def teardown():
        Trackable.clear_all()
        ctx.pop()

    # Teardown application context at the end of usage (session)
    request.addfinalizer(teardown)
    return app

@pytest.fixture(scope='function')
def db(app, request):
    """Create session-wide test database"""
    if os.path.exists(TESTDB_PATH):
        os.unlink(TESTDB_PATH)

    intertwine_db.app = app
    intertwine_db.create_all()

    def teardown():
        Trackable.clear_all()
        intertwine_db.drop_all()
        if os.path.exists(TESTDB_PATH):
            os.unlink(TESTDB_PATH)

    request.addfinalizer(teardown)
    return intertwine_db

@pytest.fixture(scope='function')
def session(db, request):
    """Create new database session for a test"""
    connection = db.engine.connect()
    transaction = connection.begin()

    options = dict(bind=connection, binds={})
    session = db.create_scoped_session(options=options)

    extend_declarative_base(IntertwineModel, session=session)

    db.session = session

    def teardown():
        Trackable.clear_all()
        transaction.rollback()
        connection.close()
        session.remove()

    request.addfinalizer(teardown)
    assert session is not None
    return session

@pytest.fixture(scope='function')
def client(app, request):
    """Create test client"""
    app.config['TESTING'] = True
    client = app.test_client()

    def teardown():
        app.config['TESTING'] = False

    request.addfinalizer(teardown)
    return client

@pytest.fixture(scope='function')
def caching(app, request):
    """Enable caching of model instances"""
    with Trackable.caching():
        yield


# ENGR2050_09_jasayles.py
# Ethan Sayles
# April 6, 2017
import numpy as np
from matplotlib import pyplot as plt
from scipy import stats as st

#Class to plot the points and best fit lines of data
class Plot():
    def __init__(self, file):
        self.file = file
    def  read_data(self):           #Function to read the data into two lists
        x = []
        y = []
        f = open(self.file, 'r')
        for line in f:
            pari = line.split(",")
            x.append(float(pari[0]))
            y.append(float(pari[1]))
        f.close()
        self.data = [x, y]
    def show(self):                 #Function to show the data points, and the best fit lines
        self.read_data()
        y = self.data[1]
        x = self.data[0]
        Xs = np.arange(0, max(x), .1)

        line_data = st.linregress(x, y)     #Finding the best fit line using scipy
        m1 = line_data[0]
        b1 = line_data[1]
        r_squared = line_data[2] ** 2

        plt.plot(y, x,  'ro',  label = 'Data Points')                                     #Plotting the data points and the best fit lines
        plt.plot(m1 * Xs + b1 , Xs,label = "R_squared = %f" %(r_squared))
        #plt.plot(m2*Xs+b2, Xs, label = "Numpy linalg best fit line")
        plt.title("Best Fit Line",  fontsize = 20)
        plt.xlabel("x",  fontsize=10)
        plt.ylabel("y",  fontsize=10)
        print ("The coefficient of determination for the blue function is %f" %(r_squared))
        plt.legend()
        plt.show()

#Conditional to check for test cases
if __name__ == '__main__':
    f ="ENGR2050_qz2-1_6.dat"
    plot = Plot(f)
    plot.show()


import torch
import unittest

from tests.disc.testing_base import DiscTestCase

class TestDiscBatchNorm(DiscTestCase):
    def _test_batchnorm(self, batchnorm_func, inp_test_data=None):
        if inp_test_data is not None:
            test_data = inp_test_data
        else:
            test_data = torch.randn([20, 16, 50, 100], device=self.device)
        if (isinstance(test_data, torch.Tensor)):
            test_data = (test_data.to(self.device),)
        self._test_cvt_to_disc(batchnorm_func, test_data)

    def test_batchnorm1d(self):
        batchnorm = torch.nn.BatchNorm1d(16).eval()
        self._test_batchnorm(batchnorm, torch.randn([20, 16, 60], device=self.device))
        batchnorm = torch.nn.BatchNorm1d(16, affine=False).eval()
        self._test_batchnorm(batchnorm, torch.randn([20, 16, 60], device=self.device))

    def test_batchnorm2d(self):
        batchnorm = torch.nn.BatchNorm2d(16).eval()
        self._test_batchnorm(batchnorm)
        batchnorm = torch.nn.BatchNorm2d(16, affine=False).eval()
        self._test_batchnorm(batchnorm)

    def test_batchnorm3d(self):
        batchnorm = torch.nn.BatchNorm3d(16).eval()
        self._test_batchnorm(batchnorm, torch.randn([20, 16, 60, 50, 100], device=self.device))
        batchnorm = torch.nn.BatchNorm3d(16, affine=False).eval()
        self._test_batchnorm(batchnorm, torch.randn([20, 16, 60, 50, 100], device=self.device))

    def test_functional_batchnorm(self):
        @torch.jit.script
        def batchnorm_func(x, running_mean, running_var, weight, bias):
            out_y = torch.nn.functional.batch_norm(x, running_mean, running_var, weight, bias, training=False)
            return out_y

        channel = 16
        x = torch.ones([20, channel], device=self.device)
        running_mean = torch.randn(channel, device=self.device)
        running_var = torch.randn(channel, device=self.device)
        weight = torch.randn(channel, device=self.device)
        bias = torch.randn(channel, device=self.device)
        with torch.no_grad():
            self._test_batchnorm(batchnorm_func, (x, running_mean, running_var, weight, bias))

if __name__ == "__main__":
    unittest.main()


import requests
import json

gqry = "gremlin=g.V().has('vertex_label','Version').has('pname',within('io.vertx:vertx-core')).in('has_dependency').values('sid')"
payload = {'gremlin':gqry}
url = 'http://bayesian-gremlin-http-saket-graph.dev.rdu2c.fabric8.io/'

r = requests.post(url,data=json.dumps(payload))

sids = r.json()['result']['data']

sidslen = len(sids)

strsids = '\',\''.join(map(str, sids))
strsids = strsids.replace('\n',"")
gqry1 = "g.V().has('vertex_label','Stack').has('sid',within('"+strsids+"')).out('has_dependency').values('pname').count().map(union(identity(),constant("+str(sidslen)+")).fold(1){a,b -> b/a})"

payload = {'gremlin':gqry1}
url = 'http://bayesian-gremlin-http-saket-graph.dev.rdu2c.fabric8.io/'

r = requests.post(url,data=json.dumps(payload))
print(r.text)


import pygame
import math
import random

#set window
pygame.init()
WIDTH, HEIGHT = 700, 500

window = pygame.display.set_mode((WIDTH, HEIGHT))

pygame.display.set_caption(("HangMan"))

#loading images

images = []

for i in range(7):
  image = pygame.image.load(str(i) + ".png")
  images.append(image)

#Loading and processing the word dictionary
a_file = open("dictionary.txt", "r")
list_of_lists = []
for line in a_file:
  stripped_line = line.strip()
  line_list = stripped_line. split()
  line_up = [x.upper() for x in line_list]
  list_of_lists. append(line_up)
a_file. close()

wordlist = [''.join(i) for i in list_of_lists]
#words = str(wordlist)

#colors
Grey = (125,125,125)
BLACK = (0,0,0)
WHITE = (255,255,255)

#fonts
LETTER_FONT = pygame.font.SysFont('ariel', 40)
WORD_FONT = pygame.font.SysFont('ariel', 60)

# Game variables
Hangman_Status=0
#words = ['HELLO', 'DARKNESS', 'MY', 'OLD', 'FRIEND']
#print(words)
word = random.choice(wordlist)
guessed = []

FPS = 60
clock = pygame.time.Clock()

#buttons
RADIUS = 20
GAP = 15
letters = []
startx = round((WIDTH - (RADIUS * 2 + GAP) * 13) / 2)
starty = 400
A = 65
for i in range(26):
    x = startx + GAP * 2 + ((RADIUS * 2 + GAP) * (i % 13))
    y = starty + ((i // 13) * (GAP + RADIUS * 2))
    letters.append([x,y, chr( A + i ), True])

#drawing

def draw():
  window.fill(Grey)

  #draw word
  display_word = ""
  for letter in word:
    if letter in guessed:
      display_word += letter + " "
    else:
      display_word +="_ "
  text = WORD_FONT.render(display_word,1,BLACK)
  window.blit(text, (180,310))

  #draw buttons
  for letter in letters:
    x,y,ltr,IsVisible =letter
    if IsVisible:
      pygame.draw.circle(window, BLACK, (x,y), RADIUS, 3)
      text = LETTER_FONT.render(ltr, 1, BLACK)
      window.blit(text, (x - text.get_width()/2, y - text.get_height()/2))

  window.blit(images[Hangman_Status], (180,70))
  pygame.display.update()

run = True
#show message
def display_text(message):
  window.fill(WHITE)
  text = WORD_FONT.render(message,1,BLACK)
  window.blit(text,(WIDTH/2 - text.get_width()/2 , HEIGHT/2 - text.get_height()/2))
  pygame.display.update()
  pygame.time.delay(2000)

while run:
  clock.tick(FPS)
  draw()

  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      run = False
    if event.type == pygame.MOUSEBUTTONDOWN:
      mouse_x,mouse_y = pygame.mouse.get_pos()
      for letter in letters:
        x,y,ltr,IsVisible = letter
        if IsVisible:
          distance = math.sqrt((x - mouse_x)**2 + (y - mouse_y)**2)
          if distance < RADIUS:
            letter[3] = False
            guessed.append(ltr)
            if ltr not in word:
              Hangman_Status += 1

  won = True

  for letter in word:
    if letter not in guessed:
      won = False
      break
  if won:
    display_text("YOU WON")
    break

  if Hangman_Status == 6:
    display_text("YOU LOST")
    break

pygame.quit()


# Importing built-in modules
from tkinter import *
from PIL import ImageTk
import pygame as p
import sys

# Importing self-defined modules
from Chessopoly import *
import Engine

p.init()

# BACKGROUND MUSICS

def win_music():
      '''Plays victpry.mp3 on victory of a team'''

      p.mixer.init()

      # Loading Music
      p.mixer.music.load("audio\\victory.mp3")
      p.mixer.music.set_volume(0.6)

      # Playing Music
      p.mixer.music.play()

def loop_music():
      '''Plays loop.mp3 on loop until game stops.'''

      p.mixer.init()

      # Loading Music
      p.mixer.music.load("audio\\loop.mp3")
      p.mixer.music.set_volume(0.3)

      # Playing Music
      p.mixer.music.play()

def load_images(rec_width, rec_height):
      '''Loads images in a dictionary.'''

      global images
      images = {}

      # All available characters
      characters = ['BL1', 'BP1', 'BL2', 'BP2', 'BR', 'BH',
                    'BS1', 'BS2', 'BS3', 'BS4', 'BS5', 'BS6',
                    'CL1', 'CP1', 'CL2', 'CP2', 'CR', 'CH',
                    'CS1', 'CS2', 'CS3', 'CS4', 'CS5', 'CS6', 'Sp']

      for character in characters:

            # Loading Images
            load_img = p.image.load(f'images\\{character}.png')
            images[character] = p.transform.scale(load_img, (rec_width, rec_height))

def draw_board_characs(screen, gs, rec_width, rec_height, rows, columns, rec_click, allowed_turns):
      '''Draws the characters, board and lines on the board.'''

      # For the board
      draw_board(screen, rec_width, rec_height, rows, columns)

      # For grid on board
      draw_lines(screen, rec_width, rec_height, rows, columns)

      # For highlights
      highlight_squares(screen, gs, allowed_turns, rec_click, rec_height, rec_width)

      # For the characters
      draw_characs(screen, gs.board, rec_width, rec_height, rows, columns)

def draw_board(screen, rec_width, rec_height, rows, columns):
      '''Draws the board on the screen'''

      # RGB Value of Light Golden
      color = (249, 221, 100)

      for row in range(rows):
            for col in range(columns):

                  # To draw rectangles on board
                  p.draw.rect(screen, color, p.Rect(col*rec_width, row*rec_height, rec_width, rec_height))

def highlight_squares(screen, gs, allowed_turns, rec_click, rec_height, rec_width):
      '''Highlights the piece selected & its possible moves'''

      # The click record isn't empty
      if rec_click != ():

            # assigning row no. & col. no.
            row, col = rec_click

            # proper piece selection check
            if gs.board[col][row][0] == ('B' if gs.black_turn else 'C'):

                  # Creating surface to highlight
                  surf = p.Surface((rec_width, rec_height))

                  # transparency
                  surf.set_alpha(100)

                  # fill color
                  surf.fill(p.Color('blue'))

                  screen.blit(surf, (rec_width*col, row*rec_height))

                  # fill color for moves
                  surf.fill(p.Color('red'))

                  # finding the clicked turn
                  for turn in allowed_turns:

                        # When found
                        if turn.first_row == row and turn.first_col == col:

                              screen.blit(surf, (turn.sec_col*rec_width, turn.sec_row*rec_height))

def draw_characs(screen, board, rec_width, rec_height, rows, columns):
      '''Draws the characters on the board'''

      for col in range(columns):
            for row in range(rows):

                  # Character's name
                  character = board[col][row]

                  if character != '__':

                        # Displaying characters
                        screen.blit(images[character], p.Rect(col*rec_width, row*rec_height, rec_width, rec_height))

def draw_lines(screen, rec_width, rec_height, rows, columns):
      '''Draws the lines on the board'''

      # Color of lines
      color = p.Color('Black')

      for row in range(rows):
            for col in range(columns):
                  # Line under a rectangle
                  p.draw.line(screen, color, (0, (row+1)*rec_height), ((col+1)*rec_width, (row+1)*rec_height), 2)

                  # Line on the right side of rectangle
                  p.draw.line(screen, color, ((col+1)*rec_width, 0), ((col+1)*rec_width, (row+1)*rec_height), 2)

def animate_turns(turn, screen, board, clock, rec_width, rec_height, rows, columns):
      '''Animates the pieces' movements'''

      # difference of position after turn
      rows_moved = turn.sec_row - turn.first_row
      cols_moved = turn.sec_col - turn.first_col

      # frames per Square movement
      frames_pr_sq = 5

      # total number of frames moved
      total_frames = (abs(rows_moved) + abs(cols_moved)) * frames_pr_sq

      # displaying each frame in frames
      for frame in range(total_frames + 1):

            # to keep track of animation
            row, col = (turn.first_row + rows_moved * frame / total_frames, turn.first_col + cols_moved * frame / total_frames)

            # For the board
            draw_board(screen, rec_width, rec_height, rows, columns)

            # For the characters
            draw_characs(screen, board, rec_width, rec_height, rows, columns)

            # erasing the moved piece
            color = (249, 221, 100)
            sec_square = p.Rect(rec_width*turn.sec_col, rec_height*turn.sec_row, rec_width, rec_height)
            p.draw.rect(screen, color, sec_square)

            # drawing captured piece if there
            if turn.turn_to != '__':
                  screen.blit(images[turn.turn_to], sec_square)

            # For grid on board
            draw_lines(screen, rec_width, rec_height, rows, columns)

            # drawing moving piece
            screen.blit(images[turn.turn_from], p.Rect(rec_width*col, rec_height*row, rec_width, rec_height))

            # updating display
            p.display.flip()
            clock.tick(80)

def turn_of(screen, turn_black):
      '''Draws the turn indicator on the board. Tells which team is up for next turn.'''

      p.init()
      p.font.init()
      # Initializing & Defining font
      font = p.font.SysFont("Brush Script Std", 30)

      if turn_black:
            txt = font.render(('Turn: Team Black'), True, (0, 0, 0), '#ffffff')
      else:
            txt = font.render(('Turn: Team Cyan '), True, (0, 0, 0), '#ffffff')

      # Draws the text
      try:
            screen.blit(txt, (50, 620))
            p.display.flip()

      except:
            pass

def result(team1, team2):
      '''Compares the teams & Displays the winners of both the teams.'''

      global win_window

      # Closing the main board
      p.quit()

      # Specifying details of the Result Window
      win_window = Tk()
      win_music()
      win_window.title('Results - CHESSOPOLY')
      win_window.geometry('880x614')
      win_window.resizable(False, False)
      win_window.iconbitmap('images\\icon.ico')

      # Background Music
      win = ImageTk.PhotoImage(file = 'images\\result.png')
      win_bg = Label(win_window, image = win)
      win_bg.place(x = 0, y = 0, relwidth = 1, relheight = 1)

      # If Black Teams wins
      if team1 > team2:
            txt = f'''Team Black: {team1} Points.
Team Cyan: {team2} Points.

TEAM BLACK wins the CHESSOPOLY!

Congratulations!'''

      # If Cyan Team wins
      elif team1 < team2:
            txt = f'''Team Black: {team1} Points.
Team Cyan: {team2} Points.

TEAM CYAN wins the CHESSOPOLY!

Congratulations!'''

      # If there's a tie.
      else:
            txt = f'''Team Black: {team1} Points.
Team Cyan: {team2} Points.

There was a tie between the two teams.'''

      # Displaying message & Back to Menu Button
      lbl = Label(win_window, text = txt, bg = '#a28938', fg = '#88001b', font = ('Brush Script MT', 16))
      lbl.place(relx = 0.5, rely = 0.5, anchor = CENTER)

      menu_btn = Button(win_window, text = 'Menu', command = destroyed, bg = '#7e6b26', fg = '#e7d789', font = ('Brush Script MT', 16))
      menu_btn.place(relx = 0.5, rely = 0.8, anchor = CENTER)

      win_window.mainloop()

def destroyed():
      '''Closes the results window & goes back to main menu.'''

      p.mixer.quit()
      win_window.destroy()
      menu_func()

def rules1():
      '''Shows a window with rules of the game.'''

      global rules

      # Specifying Rules window details
      rules = Tk()
      rules.title('Rules - CHESSOPOLY')
      rules.geometry('880x613')
      rules.resizable(False, False)
      rules.iconbitmap('images\\icon.ico')

      # Background Image
      bgd=ImageTk.PhotoImage(file='images\\rules.png')
      bgd_img=Label(rules, image=bgd).place(x=0, y=0)

      # Basic Rules
      rules_hd='Rules:'
      rules_txt='''Every character has some property in the beginning of the game.
When rector gets captured, total assets of both the teams are compared.
The team with higher number of assets will be the winner of the game.
For more detail, read 'README' file.
'''

      # Property Details
      prop_hd='Properties:'
      prop_txt='''Rector --- NUST
HODs --- SEECS & NBS
Professors --- Office # 412, 405, 420, 408
Lab Engineers ---  Labs 1, 2, 3, 4
Students --- No property.'''

      # Moves' Details
      move_hd='Moves:'
      move_txt='''Rector can 5 steps in any direction
HOD can move only one step in any direction
Professor can move 5 steps diagonally
Lab Engineer mmakes an L on board when moved
Student can move 2 steps forward on first move,
1 step on other moves & captures diagonally'''

      # LABELS FOR EACH HEADING & PARAGRAPH

      # Rules
      label1 = Label(rules, text = rules_txt, bg = '#a28938', fg = 'black', font = ('Brush Script MT', 11))
      label1.place(relx = 0.5, rely = 0.42, anchor = CENTER)

      label2 = Label(rules, text = rules_hd, bg = '#a28938', fg = 'black', font = ('Brush Script MT', 14, 'bold'))
      label2.place(relx = 0.33, rely = 0.32, anchor = CENTER)

      # Properties
      label3 = Label(rules, text = prop_txt, bg = '#a28938', fg = 'black', font = ('Brush Script MT', 12))
      label3.place(relx = 0.53, rely = 0.58, anchor = CENTER)

      label4 = Label(rules, text = prop_hd, bg = '#a28938', fg = 'black', font = ('Brush Script MT', 14, 'bold'))
      label4.place(relx = 0.3, rely = 0.5, anchor = CENTER)

      # Moves
      label5 = Label(rules, text = move_txt, bg = '#a28938', fg = 'black', font = ('Brush Script MT', 12))
      label5.place(relx = 0.5, rely = 0.79, anchor = CENTER)

      label6 = Label(rules, text = move_hd, bg = '#a28938', fg = 'black', font = ('Brush Script MT', 14, 'bold'))
      label6.place(relx = 0.29, rely = 0.7, anchor = CENTER)

      # Back to menu
      back_btn=Button(rules, text = 'BACK', command = back_to_menu, bg = '#7e6b26', fg = 'black', font = ('Brush Script MT', 16, 'bold'))
      back_btn.place(relx = 0.5, rely = 0.91, relwidth = 0.1, relheight = 0.05, anchor = CENTER)

      rules.mainloop()

def back_to_menu():
      '''Closes the Rules window & goes back to main menu'''

      rules.destroy()
      menu_func()


from django.urls import path
from . import views

urlpatterns = [
	path('', views.index, name = 'index'),
	path('panel/', views.panel, name = 'panel'),
	path('delete/<int:email_id>/', views.delete, name = 'delete'),
	path('sendemail/', views.send_mails, name = 'sendemail'),
	path('info/', views.info, name = 'info')
]


import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=PC-TP3-02;'
                      'Database=BR_RK7_MAIN;'
                      'Trusted_Connection=yes;')

print("Connected")

cursor = conn.cursor()
cursor.execute("select cast(cast(sqlquery as varbinary(max)) as nvarchar(max)) as sqlquery from PLG_IR_DATASETS where code = 126")

for row in cursor:
    ts = row[0]
    s = str(ts)
    s = s.replace(":USE_ALT_LANG","/*:USE_ALT_LANG*/0")

    tmpc = conn.cursor()
    tmpc.execute("select guidstring from restaurants where code = 26")
    for tr in tmpc:
        s = s.replace(":RESTAURANT","'"+str(tr[0])+"'")

    tmpc.execute("select guidstring from classificatorgroups where sifr = 10 and numingroup = 0")
    for tr in tmpc:
        s = s.replace(":CLASS","'"+str(tr[0]+"'"))

    tmpc.close

    s = s.replace(":date1","'2001-01-01'")
    s = s.replace(":date2","'2020-01-01'")

print(s)

cursor.execute(s)
for row in cursor:
    print(row)

cursor.close
conn.close


import tkinter as tk
import yahoo_fin.stock_info as si
from yahoo_fin import options
import os
import tkinter.font as tkFont

# HEIGHT will be the height of the frame upon which elements
# will be built
HEIGHT = 350
# WIDTH will be the width of the frame upon which elements
# will be built
WIDTH = 500
# base_folder uses os to create a path to the folder this file is in
base_folder = os.path.dirname(__file__)
# image_path searches the base folder for the blueback.gif file which
# is to be the background
image_path = os.path.join(base_folder, 'blueback.gif')

# get_stock consumes a string representing the ticker of some stock
# and produces a statement based on the percentage difference
# between the market value of the stock and the intrinsic value
# calculated with the pe ratio method
def get_stock(entry):
    try:
        # fetches the live market price of the stock with the given
        # ticker using yahoo finance
        market = si.get_live_price(entry)
        # obtains a quote table for the stock with the given ticker
        # containing all relevant stock data
        quote_table = si.get_quote_table(entry, dict_result=False)
        # retriees the price to earnings ratio from the quote table
        pe = quote_table.iat[13,1]
        # retrieves the earnings per share value from the quote table
        eps = quote_table.iat[7,1]
        # calculates the intrinsic value of the stock with the given
        # ticker using the pe ratio and the earnings per share
        ivalue1 = pe * eps
        # produces the difference between the intrinsic value and the
        # market value, both rounded to two decimal points
        diff1 = round(ivalue1, 2) - round(market, 2)
        # calculates the percent difference between the intrinsic value
        # and market value by dividing the actual difference by the
        # market value
        diff1percent = diff1 / market

        if diff1percent >= 20:
            statement = "Estimated Intrinsic Value: " + str(round(ivalue1, 2))
            statement += "\nCurrent Market Value: " + str(round(market, 2))
            statement += "\nInteresting! " + str(entry)
            statement += " looks like a very strong buy."
            statement += "\nYou should definitely look into making an immediate purchase."
        if diff1percent >= 10:
            statement = "Estimated Intrinsic Value: " + str(round(ivalue1, 2))
            statement += "\nCurrent Market Value: " + str(round(market, 2))
            statement += "\nIntriguing. " + str(entry) + " could be a strong buy,"
            statement += " you\nshould definitely consider making a purchase."
        if diff1percent >= 5:
            statement = "Estimated Intrinsic Value: " + str(round(ivalue1, 2))
            statement += "\nCurrent Market Value: " + str(round(market, 2))
            statement += "\n" + str(entry) + " is definitely trending upwards."
            statement +="\nKeep an eye on it and be on the\nlookout for"
            statemetn += " potential buying opportunities."
        if diff1percent > 0:
            statement = "Estimated Intrinsic Value: " + str(round(ivalue1, 2))
            statement += "\nCurrent Market Value: " + str(round(market, 2))
            statement += "\n" + str(entry) + " seems to be trending in the"
            statement += " right direction, but it's too close to call right now."
            statement += "\nHold off on buying until more definitive data is available."
        if diff1percent == 0:
            statement = "Estimated Intrinsic Value: " + str(round(ivalue1, 2))
            statement += "\nCurrent Market Value: " + str(round(market, 2))
            statement += "\n" + str(entry) + " is trading at approximately"
            statement += " its intrinsic value. Hold off on making a purchase,"
            statement += " as \nthere isn't much value to be found here."
        if diff1percent < 0:
            statement = "Estimated Intrinsic Value: " + str(round(ivalue1, 2))
            statement += "\nCurrent Market Value: " + str(round(market, 2))
            statement += "\n" + str(entry) + " looks to be on the downswing."
            statement += " Don't establish\na position immediately, but keep an"
            statement += " eye out\nfor potential buying opportunities in the future."
        else:
            statement = "There is not enough data at the moment to come\n to a"
            statement += " conclusive valuation for " + str(entry)
            statement += ". Please check back\nlater when our database is updated."
    # an error will only occur if the given ticker does not exist, thus
    # this is the only error that must be handled
    except:
        statement = "The ticker you entered does not exist. Please try again."
    update1 = statement.replace('{', '')
    update2 = update1.replace('}', '')
    # updates the text in the label element with our updated statement
    label['text'] = update2

root = tk.Tk()
# makes the window size static
root.resizable(False, False)

# produces a blank canvas upon which elements can be added and positioned
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

# the font style for the text that will appear displaying the service name
titlestyle=tkFont.Font(family= "Lucida Grande", size=40)

# makes the background photo found previously an element that can be added
background_image = tk.PhotoImage(file=image_path)

# creates a label element with background_image as its image
background_label = tk.Label(root, image=background_image)

# places the background label such that it takes up the entire canvas
background_label.place(x=0, y=0, relwidth=1, relheight=1)

# creates a frame element with a given background colour and border width 5
frame = tk.Frame(root, bg='#2575C6', bd=5)

# places the frame such that it is centred at 50% of the width of the canvas
# and 27.5% of the height of the canvas, and has a width equal to 75% of
# the width of the canvas and a height of 10% of the height of the canvas
frame.place(relx=0.5, rely=0.275, relwidth=0.75, relheight=0.1, anchor='n')

# creates a frame element that will contain the service title
upper_frame = tk.Frame(root, bg='#003366', bd=10)

# places the frame such that it is centred at 50% of the width and 7% of the height,
# and has a width equal to 60% of the canvas and 20% of the height of the canvas
upper_frame.place(relx=0.5, rely=0.07, relwidth=0.6, relheight=0.2, anchor='n')

# creates a label element containing the string "BlueChip" with font being the
# previously defined titlestyle
upper_label = tk.Label(upper_frame, fg="#ffffff", bg="#003366", text="BlueChip", font=titlestyle)

# places the label in the upper_frame element such that it has identical width and height
# to upper_frame
upper_label.place(relwidth=1, relheight=1)

# creates a frame element that will contain details related to the creator of this
# service (me!)
name_frame = tk.Frame(root, bg='#003366', bd=10)

# places the frame such that it is centred at 85% of the canvas width and the highest
# point of the canvas height while having width equal to 30% of the canvas width
# and height equal to 15% of the canvas height
name_frame.place(relx=0.85, rely=0, relwidth=0.3, relheight=0.15, anchor='n')

# creates a label element that contains details pertaining to the creator of the service
# that will exist within the name_frame frame
name_label = tk.Label(name_frame, fg="#ffffff", bg="#003366", text="Designed by Rocky Wang\nr533wang@uwaterloo.ca")

# places the label element in the name_frame frame with height and width both equal to 100%
# of the frame
name_label.place(relwidth=1, relheight=1)

# creates an entry element that will consume the user input with a given font size and colour
entry = tk.Entry(frame, fg="#16589B", font=40)

# places the entry element into the frame element with width equal to 57.5% of the frame and
# 100% of the frame height
entry.place(relwidth=0.575, relheight=1)

# creates a button element that, when pressed will submit the user input as the entry element
# for get_stock
button = tk.Button(frame, text="Submit", font=1, fg='#FFFFFF', bg='#16589B', command=lambda: get_stock(entry.get()))

# places the button element in the frame element centred at 60% of the frame width with height
# equal to 100% of the frame height and width equal to 40% of the frame width
button.place(relx=0.6, relheight=1, relwidth=0.4)

# creates a frame element with a given background colour and border width
lower_frame = tk.Frame(root, bg='#2575C6', bd=10)

# places the lower_frame element centered at 50% of the canvas width and 40% of the canvas height
# with width equal to 75% of the canvas width and height equal to 55% of the canvas height
lower_frame.place(relx=0.5, rely=0.4, relwidth=0.75, relheight=0.55, anchor='n')

# creates a label element that will be used to display the welcome message to users and
# to display the response to user inputs to the user
label = tk.Label(lower_frame, fg="#16589B", text="Welcome to the BlueChip stock advisory system. \nEnter the ticker"
+ " for the stock you'd like assessed,\nand an evaluation will appear in this box.")

# places the response label element in the lower_frame element with width equal to 100% of the
# lower_frame width and height equal to 100% of the lower_frame
label.place(relwidth=1, relheight=1)

root.mainloop()


from tools.engine import freeze_bn, custom_voc_evaluate
from utils.backbone_utils import resnet_fpn_backbone
from faster_rcnn import FasterRCNN, FastRCNNPredictor
from faster_polar_rcnn import FasterPolarRCNN
import _utils
from datasets.data import get_dataset
from run import get_transform
from polar_net import PolarNet

import torch
from thop import profile
from copy import deepcopy

def get_model_info(model, tsize):
    device = 'cuda'
    stride = 64
    img = torch.zeros((1, 3, stride, stride), device=device) #next(model.parameters()).device
    flops, params = profile(deepcopy(model.to(device)), inputs=(img,), verbose=False)
    params /= 1e6
    flops /= 1e9
    flops *= tsize[0] * tsize[1] / stride / stride * 2  # Gflops
    info = "Params: {:.2f}M, Gflops: {:.2f}".format(params, flops)
    return info

if __name__ == '__main__':

    use_attn = 0
    use_polar = 1
    mtype = 'v4.0.1c'
    _mtype = 'v4c'
    tail = '_w_neg'
    # use_new_v = 1
    model_name = "r50_use_attn=%d_use_polar=%d%s" % (use_attn, use_polar, mtype)
    model_path = "results/polar%s_r50%s/best.pth" % (mtype, tail)
    CLASSES = {"__background__", "nGEC", "AGC"}
    val_csvname = "test_w_ood.csv"
    # val_csvname = "val.csv"
    val_batch_size = 1
    num_workers = 4
    torch.cuda.set_device(2)
    device = torch.device("cuda")
    print("====Loading data====")
    dataset_val = get_dataset(val_csvname,
                              datatype="val",
                              transform=get_transform(train=False))
    dataloader_val = torch.utils.data.DataLoader(
        dataset_val, batch_size=val_batch_size, shuffle=False,
        num_workers=num_workers,
        collate_fn=_utils.collate_fn
    )

    print("====Loading model====")
    backbone = resnet_fpn_backbone("resnet50", False, use_attn=use_attn)
    if use_polar == 1:
        polar_net = PolarNet(mtype=_mtype)
        model = FasterPolarRCNN(backbone, polar_net, mtype=_mtype, num_classes=len(CLASSES))
    else:
        model = FasterRCNN(backbone, num_classes=len(CLASSES))

    in_features = model.roi_heads.box_predictor.cls_score.in_features
    model.roi_heads.box_predictor = FastRCNNPredictor(in_features,
                                                      len(CLASSES))
    model.load_state_dict(torch.load(model_path, map_location='cpu'))
    model.to(device)

    model.apply(freeze_bn)
    print(get_model_info(model, (1024, 1024)))

    val_mAP = custom_voc_evaluate(
        model, dataloader_val, device=device,
        gt_csv_path=val_csvname,
        cls_csv_path="temp/test_%s_%s_cls.csv" % (model_name, val_csvname),
        loc_csv_path="temp/test_%s_%s_loc.csv" % (model_name, val_csvname)
    )

    print(val_mAP)


import requests

from pyrogram import Filters, Message

from rosebot import BOT
from rosebot.helpers import ReplyCheck

def define_word_ud(word):
    def get_data(word):
        r = requests.get(
            "http://api.urbandictionary.com/v0/define?term={}".format(word)
        )
        if r.status_code == 200:
            data = r.json()
            return data

    def get_definitions(data):
        definitions = []
        for definition in data["list"]:
            definitions.append(
                "{}\t{}\t{}".format(
                    definition["word"], definition["definition"], definition["example"]
                )
            )
        return definitions

    def get_five_results(definitions):
        five_definitions = ""
        for i in range(5):
            definition = definitions[i].split("\t")
            word = definition[0]
            definition_ = definition[1]
            example = definition[2]
            five_definitions = "{}\n**{}**\n{}\n\nExample:\n{}\n\n".format(
                five_definitions, word, definition_, example
            )
        return five_definitions

    data = get_data(word)
    if not data:
        return "Found nothing on Urban Dictionary"
    else:
        definitions = get_definitions(data)
        five_definitions = get_five_results(definitions)
        return five_definitions

@BOT.on_message(Filters.command("ud", "!"))
def post_ud(bot: BOT, message: Message):
    message_text = message.text.replace("!ud ", "")
    text = define_word_ud(message_text)
    BOT.send_message(
        chat_id=message.chat.id,
        text=text[:4096],
        disable_notification=True,
        reply_to_message_id=ReplyCheck(message),
    )
    if text[4096:]:
        BOT.send_message(
            chat_id=message.chat.id, text=text[4096:], disable_notification=True
        )
    if message.from_user.is_self:
        message.delete()


from __future__ import print_function

import warnings
from time import gmtime, strftime
import traceback
warnings.simplefilter("ignore", UserWarning)  # cuDNN warning

import logging
import formatting

logger_RNN = logging.getLogger('audioSR')
logger_RNN.setLevel(logging.DEBUG)
FORMAT = '[$BOLD%(filename)s$RESET:%(lineno)d][%(levelname)-5s]: %(message)s '
formatter = logging.Formatter(formatting.formatter_message(FORMAT, False))
formatter2 = logging.Formatter('%(asctime)s - %(name)-5s - %(levelname)-10s - (%(filename)s:%(lineno)d): %(message)s')

# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger_RNN.addHandler(ch)

# File logger: see below META VARIABLES

import time
program_start_time = time.time()

print("\n * Importing libraries...")
from general_tools import *
from RNN_tools_lstm import *

##### SCRIPT META VARIABLES #####
VERBOSE = True

# batch sizes: see just above training loop
num_epochs = 20

nbMFCCs = 39 # num of features to use -> see 'utils.py' in convertToPkl under processDatabase
nbPhonemes = 39  # number output neurons

# for each dataset type as key this dictionary contains as value a list of all the network architectures that need to be trained for this dataset
MANY_N_HIDDEN_LISTS = {}
MANY_N_HIDDEN_LISTS['TIMIT'] = [[256,256]]
    # [8, 8], [8, 8, 8], [8, 8, 8, 8], [8, 8, 8, 8, 8, 8, 8, 8],
    #  [32, 32], [32, 32, 32], [32, 32, 32, 32],
    #  [64, 64], [64, 64, 64], [64, 64, 64, 64],
    #  [256, 256], [256, 256, 256], [256, 256, 256, 256],
    #  [512, 512], [512, 512, 512], [512, 512, 512, 512]]

MANY_N_HIDDEN_LISTS['TCDTIMIT'] = [[256,256]]#[32,32],[64,64],[256,256]]#,[512,512]]
# combined is TIMIT and TCDTIMIT put together
MANY_N_HIDDEN_LISTS['combined'] = [[256,256]]#[32, 32], [64, 64], [256, 256]]#, [512, 512]]

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

BIDIRECTIONAL = True
ADD_DENSE_LAYERS = False

justTest =  True

# this sets up parameters for training/evaluation of networks.
# it has lots of nested for loops so that you can train lots of different networks automatically.
# Just set the parameters
def main():
    #global  justTest, withNoise, noiseTypes, ratio_dBs

    # Choose which datasets to run
    datasets = ["combined"]  #"TCDTIMIT", combined"

    # choose which data to use as test set
    test_datasets = {}
    test_datasets['TIMIT'] = ["TIMIT"]
    test_datasets['TCDTIMIT'] = ["TCDTIMIT"]
    test_datasets['combined'] = ["TIMIT", "TCDTIMIT"]

    ROUND_PARAMS = False

    # use noise for the test set
    withNoise = False
    noiseTypes = ['white', 'voices']
    ratio_dBs = [0, -3, -5, -10]

    # for all datasets, train or test. If test, loop over test_datasets, noises, etc
    for dataset in datasets:
        for N_HIDDEN_LIST in MANY_N_HIDDEN_LISTS[dataset]:
            ##### BUIDING MODEL #####
            if N_HIDDEN_LIST[0] > 128:      batch_sizes = [64, 32, 16, 8, 4]
            elif N_HIDDEN_LIST[0] > 64:     batch_sizes = [128, 64, 32, 16, 8, 4]
            else:                           batch_sizes = [256, 128, 64, 32, 16, 8, 4]

            if justTest:
                for test_dataset in test_datasets[dataset]:
                    for batch_size in batch_sizes:
                        try:
                            network, loadParamsSuccess, model_save, batch_size, fh = setupNetwork(dataset, test_dataset, N_HIDDEN_LIST, batch_size, ROUND_PARAMS)
                            if withNoise:
                                for noiseType in noiseTypes:
                                    for ratio_dB in ratio_dBs:
                                        datasetFiles = loadData(dataset, test_dataset, withNoise, noiseType, ratio_dB)
                                        trainNetwork(network=network, loadParamsSuccess=loadParamsSuccess,
                                                     model_save=model_save, batch_size=batch_size, datasetFiles=datasetFiles,
                                                     withNoise=withNoise, noiseType=noiseType, ratio_dB=ratio_dB, fh=fh)
                            else:
                                datasetFiles = loadData(dataset, test_dataset)
                                trainNetwork(network=network, loadParamsSuccess=loadParamsSuccess,
                                             model_save=model_save, batch_size=batch_size, datasetFiles=datasetFiles, fh=fh)
                            break;
                        except:
                            print('caught this error: ' + traceback.format_exc());
                            logger_RNN.info("batch size too large; trying again with lower batch size")
                            pass  # just try again with the next batch_size

            else: #just train, no noise or special test datasets
                test_dataset = dataset
                for batch_size in batch_sizes:
                    try:
                        network, loadParamsSuccess, model_save, batch_size, fh = setupNetwork(dataset, test_dataset, N_HIDDEN_LIST, batch_size)
                        datasetFiles = loadData(dataset, test_dataset)
                        trainNetwork(network=network, loadParamsSuccess=loadParamsSuccess,
                                     model_save=model_save, batch_size=batch_size, datasetFiles=datasetFiles, fh = fh)
                        break
                    except:
                        print('caught this error: ' + traceback.format_exc());
                        logger_RNN.info("batch size too large; trying again with lower batch size")
                        pass  # just try again with the next batch_size

    logger_RNN.info("\n* Done")
    logger_RNN.info('Total time: %s',time.time() - program_start_time)

# this generates the correct path based on the chosen parameters, and gets the train/val/test data
def loadData(dataset, test_dataset, withNoise=False, noiseType=None, ratio_dB=None):
    root = os.path.expanduser("~/TCDTIMIT/audioSR/")
    dataDir = root + dataset + "/binary" + str(nbPhonemes) + os.sep + dataset  # output dir from datasetToPkl.py
    data_path = os.path.join(dataDir, dataset + '_' + str(nbMFCCs) + '_ch.pkl');
    if justTest:
        if withNoise:
            test_dataDir = root + test_dataset + "/binary" + str(nbPhonemes) + "_" + noiseType + \
                           os.sep + "ratio" + str(ratio_dB) + os.sep + test_dataset
        else:
            test_dataDir = root + test_dataset + "/binary" + str(nbPhonemes) + os.sep + test_dataset

        test_data_path = os.path.join(test_dataDir, test_dataset + '_' + str(nbMFCCs) + '_ch.pkl');

    logger_RNN.info('  data source: %s', data_path)

    dataset = unpickle(data_path)
    X_train, y_train, valid_frames_train, X_val, y_val, valid_frames_val, X_test, y_test, valid_frames_test = dataset

    # if justtest, you can use another dataset than the one used for training for evaluation
    if justTest:
        logger_RNN.info("  test data source: %s", test_data_path)
        if withNoise:
            X_test, y_test, valid_frames_test = unpickle(test_data_path)
        else:
            _, _, _, _, _, _, X_test, y_test, valid_frames_test = unpickle(test_data_path)

    datasetFiles = [X_train, y_train, valid_frames_train, X_val, y_val, valid_frames_val, X_test, y_test,
                    valid_frames_test]
    # Print some information
    debug = False
    if debug:
        logger_RNN.info("\n* Data information")
        logger_RNN.info('X train')
        logger_RNN.info('  %s %s', type(X_train), len(X_train))
        logger_RNN.info('  %s %s', type(X_train[0]), X_train[0].shape)
        logger_RNN.info('  %s %s', type(X_train[0][0]), X_train[0][0].shape)
        logger_RNN.info('  %s', type(X_train[0][0][0]))

        logger_RNN.info('y train')
        logger_RNN.info('  %s %s', type(y_train), len(y_train))
        logger_RNN.info('  %s %s', type(y_train[0]), y_train[0].shape)
        logger_RNN.info('  %s %s', type(y_train[0][0]), y_train[0][0].shape)

        logger_RNN.info('valid_frames train')
        logger_RNN.info('  %s %s', type(valid_frames_train), len(valid_frames_train))
        logger_RNN.info('  %s %s', type(valid_frames_train[0]), valid_frames_train[0].shape)
        logger_RNN.info('  %s %s', type(valid_frames_train[0][0]), valid_frames_train[0][0].shape)

    return datasetFiles

# this builds the chosen network architecture, loads network weights and compiles the functions
def setupNetwork(dataset, test_dataset, N_HIDDEN_LIST, batch_size, ROUND_PARAMS=False):
    root = os.path.expanduser("~/TCDTIMIT/audioSR/")
    store_dir = root + dataset + "/results"
    if not os.path.exists(store_dir): os.makedirs(store_dir)

    model_name = str(len(N_HIDDEN_LIST)) + "_LSTMLayer" + '_'.join([str(layer) for layer in N_HIDDEN_LIST]) \
                 + "_nbMFCC" + str(nbMFCCs) + ("_bidirectional" if BIDIRECTIONAL else "_unidirectional") + \
                 ("_withDenseLayers" if ADD_DENSE_LAYERS else "") + "_" + dataset

    # model parameters and network_training_info
    model_load = os.path.join(store_dir, model_name + ".npz")
    model_save = os.path.join(store_dir, model_name)

    # log file
    logFile = store_dir + os.sep + model_name + '.log'
    if os.path.exists(logFile):
        fh = logging.FileHandler(logFile)  # append to existing log
    else:
        fh = logging.FileHandler(logFile, 'w')  # create new logFile
    fh.setLevel(logging.DEBUG)
    fh.setFormatter(formatter)
    logger_RNN.addHandler(fh)
    #############################################################

    logger_RNN.info("\n\n\n\n STARTING NEW TRAINING SESSION AT " + strftime("%Y-%m-%d %H:%M:%S", gmtime()))

    ##### IMPORTING DATA #####

    logger_RNN.info('  model target: %s', model_save + '.npz')

    logger_RNN.info('\n* Building network using batch size: %s...', batch_size)
    RNN_network = NeuralNetwork('RNN', None, batch_size=batch_size,
                                num_features=nbMFCCs, n_hidden_list=N_HIDDEN_LIST,
                                num_output_units=nbPhonemes,
                                bidirectional=BIDIRECTIONAL, addDenseLayers=ADD_DENSE_LAYERS,
                                debug=False,
                                dataset=dataset, test_dataset=test_dataset)

    # print number of parameters
    nb_params = lasagne.layers.count_params(RNN_network.network_lout_batch)
    logger_RNN.info(" Number of parameters of this network: %s", nb_params)

    # Try to load stored model
    logger_RNN.info(' Network built. Trying to load stored model: %s', model_load)
    if justTest and ROUND_PARAMS:
        success = RNN_network.load_model(model_load, roundParams=True)
    else:
        success = RNN_network.load_model(model_load)

    RNN_network.loadPreviousResults(model_save)

    ##### COMPILING FUNCTIONS #####
    logger_RNN.info("\n* Compiling functions ...")
    RNN_network.build_functions(train=True, debug=False)

    return RNN_network, success, model_save, batch_size, fh

# this takes the prepared data, built network and some parameters, and trains/evaluates the network
def trainNetwork(network, loadParamsSuccess, model_save, batch_size, datasetFiles, withNoise=False, noiseType='white', ratio_dB=0, fh=None):
    # Decaying LR
    LR_start = 0.015
    if loadParamsSuccess == 0: LR_start = LR_start / 10.0
    logger_RNN.info("LR_start = %s", str(LR_start))
    LR_fin = 0.0000001
    logger_RNN.info("LR_fin = %s", str(LR_fin))
    # LR_decay = (LR_fin / LR_start) ** (1. / num_epochs)  # each epoch, LR := LR * LR_decay
    LR_decay = 0.5
    logger_RNN.info("LR_decay = %s", str(LR_decay))

    ##### TRAINING #####
    logger_RNN.info("\n* Training ...")
    network.train(datasetFiles, model_save, num_epochs=num_epochs,
                      batch_size=batch_size, LR_start=LR_start, LR_decay=LR_decay,
                      compute_confusion=True, justTest=justTest, debug=False,
                  withNoise=withNoise, noiseType=noiseType, ratio_dB=ratio_dB)

    fh.close()
    logger_RNN.removeHandler(fh)

if __name__ == "__main__":
    main()


from gi.repository import MyPaint

def brushsetting_from_str(setting_str):
    return getattr(MyPaint.BrushSetting, "SETTING_" + setting_str.upper())

def set_base_value(brush, setting_str, value):
    setting = brushsetting_from_str(setting_str)
    return brush.set_base_value(setting, value)

def get_base_value(brush, setting_str):
    setting = brushsetting_from_str(setting_str)
    return brush.get_base_value(setting)

def set_base_color(brush, color_hsv):
    color_h, color_s, color_v = color_hsv
    set_base_value(brush, "color_h", color_h)
    set_base_value(brush, "color_s", color_s)
    set_base_value(brush, "color_v", color_v)

def get_base_color(brush):
    color_h = get_base_value(brush, "color_h")
    color_s = get_base_value(brush, "color_s")
    color_v = get_base_value(brush, "color_v")
    return color_h, color_s, color_v

__test__ = dict(allem="""

These are auxiliary functions to simplify the API provided by GObject
introspection.

>>> setting = brushsetting_from_str("color_h")
>>> setting == MyPaint.BrushSetting.SETTING_COLOR_H
True

>>> brush = MyPaint.Brush()
>>> set_base_value(brush, "radius_logarithmic", 12.0)
>>> get_base_value(brush, "radius_logarithmic")
12.0

>>> set_base_color(brush, (0.0, 1.0, 0.5))
>>> get_base_color(brush)
(0.0, 1.0, 0.5)

>>> get_base_value(brush, "color_s")
1.0

>>> get_base_value(brush, "color_v")
0.5

""")

if __name__ == '__main__':
    import doctest
    doctest.testmod()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 30 08:37:41 2023

@author: admin
"""
import copy,random,pygame

class Aliens(object):
    def __init__(self, k,openSpaces,row,column, pixel_w, pixel_h):
        self.k=k
        self.openSpaces = openSpaces
        self.alienSpaces = random.sample(self.openSpaces,k)
        self.row=row
        self.column=column
        self.h = row
        self.w = column
        self.pixel_w = pixel_w
        self.pixel_h = pixel_h
        self.xscale = pixel_w / column
        self.yscale = pixel_h / row

    def moveAlien(self, screen, x_offset, y_offset):

        def checkForOutOfBound(x,y,row,col):
            if x>=row or x<0 or y<0 or y>=col:
                return True
            return False

        moves=[[0,1],[1,0],[0,-1],[-1,0],[0,0]]
        random.shuffle(self.alienSpaces)
        aliensCopy=copy.deepcopy(self.alienSpaces)
        for alien in aliensCopy:
            #print(alien)
            random.shuffle(moves)
            for move in moves:
                neighbourRow = alien[0]+move[0]
                neighbourCol = alien[1]+move[1]
                res = checkForOutOfBound(neighbourRow,neighbourCol,self.row,self.column)
                if [neighbourRow,neighbourCol] not in self.alienSpaces and res == False and [neighbourRow,neighbourCol] in self.openSpaces:
                    self.alienSpaces.remove(alien)
                    self.alienSpaces.append([neighbourRow,neighbourCol])
                    break
        print("alienspaces",self.alienSpaces)
        for x,y in self.alienSpaces:
            bx = y * self.xscale
            by = x * self.yscale
            pygame.draw.rect(screen, (255, 5, 5), pygame.Rect(bx + x_offset, by + y_offset, self.xscale, self.yscale))


import json

class Asset(object):

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def serialize(self):
        return json.dumps(self, default=lambda o: o.__dict__)

if __name__ == '__main__':
    a = Asset('lee', 100.80)
    print(a.serialize())


import numpy as np

import torch
import torch.nn.functional
import torchvision.transforms

import PSMNet.models

# global preprocessing transform (from PSMNet Test_img.py)
_normal_mean_var = {'mean': [0.485, 0.456, 0.406],
                    'std': [0.229, 0.224, 0.225]}
_inference_transform = \
   torchvision.transforms.Compose([torchvision.transforms.ToTensor(),
                                   torchvision.transforms.Normalize(**_normal_mean_var)])

class PyramidalStereoMatchingNet:

    def __init__(self, psmnet_pretrained_weights, use_cuda, max_disp=192):
        self.psmnet = PSMNet.models.stackhourglass(maxdisp=max_disp)
        if use_cuda:
            state_dict = torch.load(psmnet_pretrained_weights)
        else:
            state_dict = torch.load(psmnet_pretrained_weights, map_location=torch.device('cpu'))

        self.psmnet = torch.nn.DataParallel(self.psmnet, device_ids=[0]) # required to be before load_state_dict()
        self.psmnet.load_state_dict(state_dict['state_dict'])
        self.psmnet.eval() # prep for inference
        if use_cuda:
            self.psmnet.cuda()
        else:
            self.psmnet.cpu()
    # /__init__()

    def _preprocess(self, imgL_in, imgR_in):

        # following steps from PSMNet Test_img.py
        imgL = _inference_transform(imgL_in)
        imgR = _inference_transform(imgR_in)

        # pad to width and height to 16 times
        top_pad = (16 - (imgL.shape[1] % 16)) % 16
        right_pad = (16 - (imgL.shape[2] % 16)) % 16

        imgL = torch.nn.functional.pad(imgL,(0,right_pad, top_pad,0)).unsqueeze(0)
        imgR = torch.nn.functional.pad(imgR,(0,right_pad, top_pad,0)).unsqueeze(0)

        return imgL, imgR
    # /_preprocess()

    def disparityField(self, left_img, right_img, use_cuda):
        psmnet_left_img, psmnet_right_img = \
            self._preprocess(left_img, right_img)
        if use_cuda:
            psmnet_left_img.cuda()
            psmnet_right_img.cuda()
        with torch.no_grad():
            disparity_field = self.psmnet(psmnet_left_img, psmnet_right_img)

        disparity_field = torch.squeeze(disparity_field)
        disparity_field_cpu = disparity_field.data.cpu().numpy()
        print('\tmin disp = {:.2f}, max disp = {:.2f}'.format(np.min(disparity_field_cpu),
                                                              np.max(disparity_field_cpu)))
        return disparity_field_cpu
    # /disparityField()

# /class PyramidalStereoMatching


from tensorflow.keras.preprocessing.image import ImageDataGenerator

import pandas as pd
import numpy as np

from data_loaders.utils import _getLabels
from utils.paths import getDataDirectory

import types

def getData(dataset_name, test_dataset_name, label_set, get_batches_with_fake_waves_fn, dim=256):
    train_data = _getLabels(label_set, "train")
    validation_data = _getLabels(label_set, "validation")
    test_data = _getLabels(label_set, "test")

    data_path = getDataDirectory(dataset_name)
    test_data_path = getDataDirectory(test_dataset_name)

    train_datagen = ImageDataGenerator()
    test_datagen = ImageDataGenerator()

    train = train_datagen.flow_from_dataframe(
        train_data,
        directory=data_path,
        x_col="filename", y_col=["left", "top", "right", "bottom"],
        target_size=(dim, dim),
        batch_size=32,
        color_mode = "rgb",
        class_mode="raw",
        shuffle=True,
    )
    val = test_datagen.flow_from_dataframe(
        validation_data,
        directory=test_data_path,
        x_col="filename", y_col=["left", "top", "right", "bottom"],
        target_size=(dim, dim),
        batch_size=1,
        color_mode = "rgb",
        class_mode="raw",
        shuffle=False
    )
    test = test_datagen.flow_from_dataframe(
        test_data,
        directory=test_data_path,
        x_col="filename", y_col=["left", "top", "right", "bottom"],
        target_size=(dim, dim),
        batch_size=1,
        color_mode = "rgb",
        class_mode="raw",
        shuffle=False
    )
    train._get_batches_of_transformed_samples = types.MethodType( get_batches_with_fake_waves_fn, train ) # replace method for loading train batches
    return train, val, test


import json
import time
import base64
import xml.etree.ElementTree as ET
from pymisp import MISPEvent, MISPObject

misperrors = {'error': 'Error'}
moduleinfo = {'version': 1, 'author': 'Christian Studer',
              'description': 'Import from GoAML',
              'module-type': ['import']}
moduleconfig = []
mispattributes = {'inputSource': ['file'], 'output': ['MISP objects'],
                  'format': 'misp_standard'}

t_from_objects = {'nodes': ['from_person', 'from_account', 'from_entity'],
                  'leaves': ['from_funds_code', 'from_country']}
t_to_objects = {'nodes': ['to_person', 'to_account', 'to_entity'],
                'leaves': ['to_funds_code', 'to_country']}
t_person_objects = {'nodes': ['addresses'],
                    'leaves': ['first_name', 'middle_name', 'last_name', 'gender', 'title', 'mothers_name', 'birthdate',
                               'passport_number', 'passport_country', 'id_number', 'birth_place', 'alias', 'nationality1']}
t_account_objects = {'nodes': ['signatory'],
                     'leaves': ['institution_name', 'institution_code', 'swift', 'branch', 'non_banking_insitution',
                                'account', 'currency_code', 'account_name', 'iban', 'client_number', 'opened', 'closed',
                                'personal_account_type', 'balance', 'date_balance', 'status_code', 'beneficiary',
                                'beneficiary_comment', 'comments']}
entity_objects = {'nodes': ['addresses'],
                  'leaves': ['name', 'commercial_name', 'incorporation_legal_form', 'incorporation_number', 'business', 'phone']}

goAMLobjects = {'report': {'nodes': ['reporting_person', 'location'],
                           'leaves': ['rentity_id', 'submission_code', 'report_code', 'submission_date', 'currency_code_local']},
                'reporting_person': {'nodes': ['addresses'], 'leaves': ['first_name', 'middle_name', 'last_name', 'title']},
                'location': {'nodes': [], 'leaves': ['address_type', 'address', 'city', 'zip', 'country_code', 'state']},
                'transaction': {'nodes': ['t_from', 't_from_my_client', 't_to', 't_to_my_client'],
                                'leaves': ['transactionnumber', 'transaction_location', 'date_transaction',
                                           'transmode_code', 'amount_local']},
                't_from': t_from_objects, 't_from_my_client': t_from_objects,
                't_to': t_to_objects, 't_to_my_client': t_to_objects,
                'addresses': {'nodes': ['address'], 'leaves': []},
                'address': {'nodes': [], 'leaves': ['address_type', 'address', 'city', 'zip', 'country_code', 'state']},
                'from_person': t_person_objects, 'to_person': t_person_objects, 't_person': t_person_objects,
                'from_account': t_account_objects, 'to_account': t_account_objects,
                'signatory': {'nodes': ['t_person'], 'leaves': []},
                'from_entity': entity_objects, 'to_entity': entity_objects,
                }

t_account_mapping = {'misp_name': 'bank-account', 'institution_name': 'institution-name', 'institution_code': 'institution-code',
                     'iban': 'iban', 'swift': 'swift', 'branch': 'branch', 'non_banking_institution': 'non-bank-institution',
                     'account': 'account', 'currency_code': 'currency-code', 'account_name': 'account-name',
                     'client_number': 'client-number', 'personal_account_type': 'personal-account-type', 'opened': 'opened',
                     'closed': 'closed', 'balance': 'balance', 'status_code': 'status-code', 'beneficiary': 'beneficiary',
                     'beneficiary_comment': 'beneficiary-comment', 'comments': 'comments'}

t_person_mapping = {'misp_name': 'person', 'comments': 'text', 'first_name': 'first-name', 'middle_name': 'middle-name',
                    'last_name': 'last-name', 'title': 'title', 'mothers_name': 'mothers-name', 'alias': 'alias',
                    'birthdate': 'date-of-birth', 'birth_place': 'place-of-birth', 'gender': 'gender', 'nationality1': 'nationality',
                    'passport_number': 'passport-number', 'passport_country': 'passport-country', 'ssn': 'social-security-number',
                    'id_number': 'identity-card-number'}

location_mapping = {'misp_name': 'geolocation', 'city': 'city', 'state': 'region', 'country_code': 'country', 'address': 'address',
                    'zip': 'zipcode'}

t_entity_mapping = {'misp_name': 'legal-entity', 'name': 'name', 'business': 'business', 'commercial_name': 'commercial-name',
                    'phone': 'phone-number', 'incorporation_legal_form': 'legal-form', 'incorporation_number': 'registration-number'}

goAMLmapping = {'from_account': t_account_mapping, 'to_account': t_account_mapping, 't_person': t_person_mapping,
                'from_person': t_person_mapping, 'to_person': t_person_mapping, 'reporting_person': t_person_mapping,
                'from_entity': t_entity_mapping, 'to_entity': t_entity_mapping,
                'location': location_mapping, 'address': location_mapping,
                'transaction': {'misp_name': 'transaction', 'transactionnumber': 'transaction-number', 'date_transaction': 'date',
                                'transaction_location': 'location', 'transmode_code': 'transmode-code', 'amount_local': 'amount',
                                'transmode_comment': 'transmode-comment', 'date_posting': 'date-posting', 'teller': 'teller',
                                'authorized': 'authorized', 'transaction_description': 'text'}}

nodes_to_ignore = ['addresses', 'signatory']
relationship_to_keep = ['signatory', 't_from', 't_from_my_client', 't_to', 't_to_my_client', 'address']

class GoAmlParser():
    def __init__(self):
        self.misp_event = MISPEvent()

    def read_xml(self, data):
        self.tree = ET.fromstring(data)

    def parse_xml(self):
        self.first_itteration()
        for t in self.tree.findall('transaction'):
            self.itterate(t, 'transaction')

    def first_itteration(self):
        submission_date = self.tree.find('submission_date').text.split('+')[0]
        self.misp_event.timestamp = int(time.mktime(time.strptime(submission_date, "%Y-%m-%dT%H:%M:%S")))
        for node in goAMLobjects['report']['nodes']:
            element = self.tree.find(node)
            if element is not None:
                self.itterate(element, element.tag)

    def itterate(self, tree, aml_type, referencing_uuid=None, relationship_type=None):
        objects = goAMLobjects[aml_type]
        referenced_uuid = referencing_uuid
        rel = relationship_type
        if aml_type not in nodes_to_ignore:
            try:
                mapping = goAMLmapping[aml_type]
                misp_object = MISPObject(name=mapping['misp_name'])
                for leaf in objects['leaves']:
                    element = tree.find(leaf)
                    if element is not None:
                        object_relation = mapping[element.tag]
                        attribute = {'object_relation': object_relation, 'value': element.text}
                        misp_object.add_attribute(**attribute)
                if aml_type == 'transaction':
                    for node in objects['nodes']:
                        element = tree.find(node)
                        if element is not None:
                            self.fill_transaction(element, element.tag, misp_object)
                self.misp_event.add_object(misp_object)
                last_object = self.misp_event.objects[-1]
                referenced_uuid = last_object.uuid
                if referencing_uuid and relationship_type:
                    referencing_object = self.misp_event.get_object_by_uuid(referencing_uuid)
                    referencing_object.add_reference(referenced_uuid, rel, None, **last_object)
            except KeyError:
                pass
        for node in objects['nodes']:
            element = tree.find(node)
            if element is not None:
                tag = element.tag
                if tag in relationship_to_keep:
                    rel = tag[2:] if tag.startswith('t_') else tag
                self.itterate(element, element.tag, referencing_uuid=referenced_uuid, relationship_type=rel)

    @staticmethod
    def fill_transaction(element, tag, misp_object):
        if 't_from' in tag:
            from_funds = element.find('from_funds_code').text
            from_funds_attribute = {'object_relation': 'from-funds-code', 'value': from_funds}
            misp_object.add_attribute(**from_funds_attribute)
            from_country = element.find('from_country').text
            from_country_attribute = {'object_relation': 'from-country', 'value': from_country}
            misp_object.add_attribute(**from_country_attribute)
        if 't_to' in tag:
            to_funds = element.find('to_funds_code').text
            to_funds_attribute = {'object_relation': 'to-funds-code', 'value': to_funds}
            misp_object.add_attribute(**to_funds_attribute)
            to_country = element.find('to_country').text
            to_country_attribute = {'object_relation': 'to-country', 'value': to_country}
            misp_object.add_attribute(**to_country_attribute)

def handler(q=False):
    if q is False:
        return False
    request = json.loads(q)
    if request.get('data'):
        data = base64.b64decode(request['data']).decode('utf-8')
    else:
        misperrors['error'] = "Unsupported attributes type"
        return misperrors
    aml_parser = GoAmlParser()
    try:
        aml_parser.read_xml(data)
    except Exception:
        misperrors['error'] = "Impossible to read XML data"
        return misperrors
    aml_parser.parse_xml()
    r = {'results': {'Object': [obj.to_json() for obj in aml_parser.misp_event.objects]}}
    return r

def introspection():
    return mispattributes

def version():
    moduleinfo['config'] = moduleconfig
    return moduleinfo


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov  6 15:00:41 2023

@author: maggie clarke

Step #1
Append task data files, perform tSSS & movement compensation
"""
import mne
import os
import os.path as op
from mne.preprocessing import maxwell_filter, compute_average_dev_head_t
from mne.chpi import (compute_chpi_amplitudes, compute_chpi_locs,
                      compute_head_pos, write_head_pos)

####### set these  before running #############################################
path = '/home/maggie/data/'

# setup subject name ### change this for each subject
s = 'vml_meg_015'

# set to True if headpos has not already been calculated & saved
calc_headpos = True
###############################################################################

# read in system specific cross-talk and fine calibration files
ct = op.join(path, 'ct_sparse.fif')
fc = op.join(path, 'sss_cal.dat')

# append task runs & save output
raw = mne.io.Raw(op.join(path, 'VML', '%s' %s,
                         '%s_1_raw.fif' %s), preload=True)
raw2 = mne.io.Raw(op.join(path, 'VML', '%s' %s,
                         '%s_2_raw.fif' %s), preload=True)
raw.append([raw, raw2])
raw.save(op.join(path, 'VML', '%s' %s, '%s_all_raw.fif' %s))
# delete old raw objects for memory
del raw, raw2

# read in raw data
raw = mne.io.Raw(op.join(path, 'VML', '%s' %s,
                         '%s_all_raw.fif' %s), preload=True)

# make a copy of raw and apply a lowpass filter (55 Hz) for vizualization
raw_filtered = raw.copy().load_data().filter(l_freq=None, h_freq=55.)

# plot raw data and visually inspect for bad channels
raw_filtered.plot()
# delete filtered raw object
del raw_filtered

# set bad channels in data structure - get these from
# runsheet OR visual inspection
raw.info['bads'] = ['MEG1213']

# setup for movement compensation by extracting coil info
if calc_headpos == True:
    amps = compute_chpi_amplitudes(raw)
    locs = compute_chpi_locs(raw.info, amps)
    pos = compute_head_pos(raw.info, locs)
    # save pos file (computing takes awhile)
    write_head_pos(op.join(path, 'VML', '%s' %s, '%s_all_pos.fif' %s), pos)
else:
    pos = mne.chpi.read_head_pos(op.join(path, 'VML', '%s' %s,
                                         '%s_all_pos.fif' %s))

# get initial head position and calculate average position (across time)
orig_head_dev_t = mne.transforms.invert_transform(raw.info["dev_head_t"])
avg_head_dev_t = mne.transforms.invert_transform(compute_average_dev_head_t
                                                     (raw, pos))

# plot head positions over time (green=initial, red=average)
if not os.path.exists(op.join(path, 'VML', '%s' %s, 'figures')):
    os.makedirs(op.join(path, 'VML', '%s' %s, 'figures'))

fig = mne.viz.plot_head_positions(pos)
for ax, val, val_ori in zip(
    fig.axes[::2],
    avg_head_dev_t["trans"][:3, 3],
    orig_head_dev_t["trans"][:3, 3],
):
    ax.axhline(1000 * val, color="r")
    ax.axhline(1000 * val_ori, color="g")
fig.savefig(op.join(path, 'VML', '%s' %s, 'figures', '%s_att_all_headpos' %s))

# use average position for movement compensation destination
destination = (raw.info['dev_head_t']['trans'][:3, 3])

# perform tSSS - we don't need to apply eSSS for event-related data
tsss_mc = maxwell_filter(raw, calibration=fc, cross_talk=ct,
                         st_duration=6, head_pos=pos,
                         destination=destination, st_correlation=0.98)

# save the tSSS processed file for next steps
tsss_mc.save(op.join(path, 'VML', '%s' %s, '%s_all_tsss.fif' %s))


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

# In[1]:

#jaiiii sri ram

# In[2]:

#steps
'''
1.input raw data
2.tokenizing
3.textcleaning
   - remove punctuations
   - remove stopwords
   - stemming/lemmatization
4.vectorization
5.modelling
6.accuracy and final model
'''

# # BUSINESS PROBLEM UNDERSTANDING

# In[3]:

#Performing sentimental analysis on the input data and saying whether the feedback of the students is positive or not
#application - applying on the feedback data of the students on the lecturer and analying their data instead of manually reading each and every feedback

# In[4]:

import pandas as pd
import seaborn as sns
import matplotlib as plt

# In[5]:

df = pd.read_csv("C:\\Users\\Sricharan Reddy\\Downloads\\feedback dataset.csv")

# In[6]:

df.head()

# In[7]:

df.info()

# In[8]:

#here the first column is a text column in which the feedback is written and sentiment is the output column of dataset

# In[9]:

import nltk
import re

# In[10]:

from nltk.corpus import stopwords
from nltk.stem import PorterStemmer

# In[11]:

ps = PorterStemmer()

# In[12]:

corpus = []
for i in range(len(df)):
    rp = re.sub("[^a-zA-Z]"," ",df['text'][i])
    rp = rp.lower()
    rp = rp.split()
    rp = [ps.stem(word) for word in rp if not  word in set(stopwords.words('english'))]
    rp = " ".join(rp)
    corpus.append(rp)

# In[13]:

corpus[:5]

# In[14]:

#working fine here we have done removing punctuations , stopwords , stemming lessgooo

# In[15]:

from sklearn.feature_extraction.text import CountVectorizer

# In[16]:

cv = CountVectorizer()

# In[17]:

x = cv.fit_transform(corpus).toarray()

# In[18]:

y = df['sentiment']

# In[19]:

from sklearn.model_selection import train_test_split

# In[20]:

x_train , x_test , y_train , y_test = train_test_split(x,y,test_size=0.2,random_state = 9)

# In[ ]:

# # Modelling

# ## 1.Naive Bayes classifier

# In[21]:

from sklearn.naive_bayes import MultinomialNB

# In[22]:

nbmodel = MultinomialNB()

# In[23]:

nbmodel.fit(x_train,y_train)

# In[24]:

y_pred_train  = nbmodel.predict(x_train)

# In[25]:

y_pred_test = nbmodel.predict(x_test)

# In[26]:

from sklearn.metrics import accuracy_score

# In[27]:

accuracy_score(y_train,y_pred_train)

# In[28]:

accuracy_score(y_test,y_pred_test)

# In[29]:

nbmodel.score(x_test,y_test)

# ## 2.XGBOOST ALGORITHM

# In[30]:

from xgboost import XGBClassifier

# In[31]:

model = XGBClassifier()
model.fit(x_train,y_train)

# In[32]:

y_pred_train  = model.predict(x_train)
y_pred_test = model.predict(x_test)

# In[33]:

from sklearn.metrics import accuracy_score

# In[34]:

accuracy_score(y_train,y_pred_train)

# In[35]:

accuracy_score(y_test,y_pred_test)

# In[36]:

model.score(x_test,y_test)

# # 3.Logistic regression

# In[37]:

from sklearn.linear_model import LogisticRegression

# In[38]:

model = LogisticRegression()
model.fit(x_train,y_train)

# In[39]:

y_pred_train  = model.predict(x_train)
y_pred_test = model.predict(x_test)

# In[40]:

print(accuracy_score(y_train,y_pred_train))
print(accuracy_score(y_test,y_pred_test))

# In[41]:

model.score(x_test,y_test)

# # To make a new prediction : -

# In[42]:

#for predicting for a new input you have to do all the steps of the preprocessing before model u can't just give it directly

# In[59]:

new_feedback = "teaching is excellent and the way of explanation is very good "

# In[60]:

#new_feedback = "teaching is not good the way of explanation is bad and ambiguious "

# In[61]:

corpus = []

# In[62]:

rp = re.sub("[^a-zA-Z]"," ",new_feedback)
rp = rp.lower()
rp = rp.split()
rp = [ps.stem(word) for word in rp if not  word in set(stopwords.words('english'))]
rp = " ".join(rp)
corpus.append(rp)

# In[63]:

x_new = cv.transform(corpus).toarray()

# In[64]:

x_new

# In[65]:

ans = model.predict(x_new)

# In[66]:

if(ans==1):
    print("Positive opinion")
else:
    print("Negative opinion")

# In[67]:

#scr

# In[ ]:


#Python 2.x program to transcribe an Audio file
import speech_recognition as sr
from gtts import gTTS
import os
AUDIO_FILE = ("Recording.wav")

# use the audio file as the audio source

r = sr.Recognizer()

with sr.AudioFile(AUDIO_FILE) as source:
	#reads the audio file. Here we use record instead of
	#listen
	audio = r.record(source)

try:
    x=r.recognize_google(audio)
    print("The audio file contains: " + x)

except sr.UnknownValueError:
	print("Google Speech Recognition could not understand audio")

except sr.RequestError as e:
	print("Could not request results from Google Speech Recognition service; {0}".format(e))

language='en'
myobj = gTTS(text=x, lang=language, slow=False)
myobj.save("welcome.wav")

# Playing the converted file
os.system("mpg321 recording.wav")


from pathlib import Path
import pandas as pd

import nltk
from nltk.corpus import stopwords
from nltk.stem.wordnet import WordNetLemmatizer
import string

# Importing Gensim
import gensim
from gensim import corpora

# to suppress warnings
from warnings import filterwarnings
filterwarnings('ignore')

proc_data_dir = Path('Prepared data')
proc_data_dir = Path('Raw data')
data = pd.read_excel(proc_data_dir / 'training_data_AlixPartners_Val_Dist.xlsx')

corpus = list(data['Paragraph'])
corpus = list(set(corpus))
############## Pre-proc
# stop loss words
stop = set(stopwords.words('english'))

# punctuation
exclude = set(string.punctuation)

# lemmatization
lemma = WordNetLemmatizer()

# One function for all the steps:
def clean(doc):

    # convert text into lower case + split into words
    stop_free = " ".join([i for i in doc.lower().split() if i not in stop])

    # remove any stop words present
    punc_free = ''.join(ch for ch in stop_free if ch not in exclude)

    # remove punctuations + normalize the text
    normalized = " ".join(lemma.lemmatize(word) for word in punc_free.split())
    return normalized

# clean data stored in a new list
clean_corpus = [clean(doc).split() for doc in corpus]

dict_ = corpora.Dictionary(clean_corpus)
doc_term_matrix = [dict_.doc2bow(i) for i in clean_corpus]

Lda = gensim.models.ldamodel.LdaModel
ldamodel = Lda(doc_term_matrix, num_topics=10, id2word = dict_, passes=1, random_state=0, eval_every=None)
ldamodel.print_topics()
print(ldamodel.print_topics(num_topics=10, num_words=5))

count = 0
for i in ldamodel[doc_term_matrix]:
    print("doc : ",count,i)
    count += 1


##
# CPCantalapiedra 2021

from io import StringIO
import math
import multiprocessing
import sys

import numpy as np
import pandas as pd

from pastml.acr import pastml_pipeline, acr, _validate_input
from pastml.ml import MPPA
from pastml.models.f81_like import F81

##
## ACR functions
##

##
# Compute ACR
def compute_acr_probs(tree, data, columns, threads, data_sep, id_index, verbose = False):
    name_column = None
    root_date = None
    prediction_method = MPPA
    model = F81

    if verbose == True: print("Computing ACR", file=sys.stderr)

    #
    # Validate input

    roots, columns, column2states, name_column, age_label, parameters, rates = \
    _validate_input(tree, columns, name_column, data, data_sep, id_index,
                    root_date, copy_only=False, parameters=None, rates=0)

    forced_joint = False
    reoptimise = False
    smoothing = False
    resolve_polytomies = False
    frequency_smoothing = False

    #
    # Compute ACR

    acr_results = acr(forest=roots, columns=columns, column2states=column2states,
                      prediction_method=prediction_method, model=model, column2parameters=parameters,
                      column2rates=rates,
                      force_joint=forced_joint, threads=threads, reoptimise=reoptimise, tau=None if smoothing else 0,
                      resolve_polytomies=resolve_polytomies, frequency_smoothing=frequency_smoothing)

    # marginal probabilities is a <class 'pandas.core.frame.DataFrame'>
    marginal_probs = acr_results[0]['marginal_probabilities']
    # print(marginal_probs, file=sys.stderr)

    return marginal_probs

##
# Computes ACR and remove tips
def get_acr_probs(tree, data_df, columns, threads, data_sep, id_index, verbose = False):
    # create file-like object from dataframe
    s_buf = StringIO()
    data_df.to_csv(s_buf)
    s_buf.seek(0)

    # compute ACR
    marginal_probs = compute_acr_probs(tree, s_buf, columns, threads, data_sep, id_index, verbose)

    # remove ACR probabilities of leaves
    marginal_probs = marginal_probs.drop(data_df.index)

    return marginal_probs

##
## Entropies functions
##

def compute_entropies(marginal_probs, verbose = False):

    if verbose == True: print("Computing entropies", file=sys.stderr)

    k = len(marginal_probs.columns)

    for col in marginal_probs:
        marginal_probs.loc[marginal_probs[col]>1/k, col] = marginal_probs.loc[marginal_probs[col]>1/k, col] / (1 - k) - 1 / (1 - k)

    entropies = marginal_probs.sum(axis=1)

    np.random.seed(25)
    entropies[entropies == 0] = entropies[entropies == 0] + np.random.rand(1)/10000
    entropies[entropies == 1] = entropies[entropies == 1] - np.random.rand(1)/10000

    return entropies

##
## MCMC functions
##

def lpalpha(alpha, beta, x, lambda0):
    return len(x) * (math.lgamma(alpha+beta) - math.lgamma(alpha)) - alpha * (lambda0 - sum(np.log(x)))

def mhalpha(alpha, beta, x, lambda0, se):
    a0 = alpha
    a1 = None
    r = None

    while r is None:
        a1 = math.exp(np.random.normal(math.log(a0), se))
        lpa1 = lpalpha(a1, beta, x, lambda0)
        lpa0 = lpalpha(a0, beta, x, lambda0)
        try:
            r = min(1, math.exp(lpa1 - lpa0))
        except OverflowError:
            if lpa1 >= lpa0:
                r = 1
            else:
                r = float('-inf')

    if np.random.rand(1) < r:
        pass # return a1
    else:
        a1 = a0 # return a0

    return a1

def lpbeta(alpha, beta, x, lambda0):
    return len(x) * (math.lgamma(alpha+beta) - math.lgamma(beta)) - beta * (lambda0 - sum(np.log(1-x)))

def mhbeta(alpha, beta, x, lambda0, se):
    b0 = beta
    b1 = None
    r = None

    while r is None:
        b1 = math.exp(np.random.normal(math.log(b0), se))
        lpb1 = lpbeta(alpha, b1, x, lambda0)
        lpb0 = lpbeta(alpha, b0, x, lambda0)
        try:
            r = min(1, math.exp(lpb1 - lpb0))
        except OverflowError:
            if lpb1 >= lpb0:
                r = 1
            else:
                r = float('-inf')

    if np.random.rand(1) < r:
        pass # return b1
    else:
        b1 = b0 # return b0

    return b1

def compute_mcmc_parallel(args):
    alpha, beta, x, lambda0, se, sim, thin, burn = args
    return compute_mcmc(alpha, beta, x, lambda0, se, sim, thin, burn)

def compute_mcmc(alpha, beta, x, lambda0, se, sim, thin, burn):
    gibbs = []
    # define steps to simulate
    gibbs_range = range(burn, sim+1, thin)
    # print(f"Gibbs sampling: {gibbs_range[0]}, {gibbs_range[1]}, ..., {gibbs_range[-1]}", file=sys.stderr)

    # burn
    for i in range(1, burn):
        alpha = mhalpha(alpha, beta, x, lambda0, se)
        beta = mhbeta(alpha, beta, x, lambda0, se)

    # chain
    for i in range(burn, sim+1):
        alpha = mhalpha(alpha, beta, x, lambda0, se)
        beta = mhbeta(alpha, beta, x, lambda0, se)

        if i % thin == 0:
            gibbs.append((alpha, beta))

    # print(f"Gibbs sampling: {gibbs[0]}, {gibbs[1]}, ..., {gibbs[-1]}", file=sys.stderr)

    return gibbs

##
## Compute delta
##

def compute_delta(entropies, threads, verbose = False):
    delta = None

    # Based on
    # https://github.com/mrborges23/delta_statistic/blob/master/code.R

    lambda0 = 0.1   #rate parameter of the proposal
    se      = 0.5   #standard deviation of the proposal
    sim     = 10000 #number of iterations
    thin    = 10    #we kept only each 10th iterate
    burn    = 100   #100 iterates are burned-in
    mcmc_reps = 2   # number of times MCMC is run

    if verbose == True: print("Computing delta", file=sys.stderr)

    threads = threads if threads < 2 else mcmc_reps

    # Single thread
    if threads == 1:
        divs = [b/a for (a, b) in compute_mcmc(np.random.exponential(1), np.random.exponential(1), entropies, lambda0, se, sim, thin, burn)]
        divs.extend([b/a for (a, b) in compute_mcmc(np.random.exponential(1), np.random.exponential(1), entropies, lambda0, se, sim, thin, burn)])

    # Multiprocess
    else:

        def mcmc_args(reps):
            for rep in range(1, reps+1):
                yield (np.random.exponential(1), np.random.exponential(1), entropies, lambda0, se, sim, thin, burn)

        divs = None
        with multiprocessing.Pool(processes = threads) as pool:
            vals = pool.imap_unordered(compute_mcmc_parallel, mcmc_args(mcmc_reps))
            divs = [b/a for sublist in vals for (a, b) in sublist]

    # Compute delta
    if divs is None or len(divs) <= 0:
        raise Exception("Could not compute MCMC alpha-beta values.")
    else:
        # mean of all beta/alpha divisions
        delta = sum(divs)/len(divs)

    return delta

##
## p-value functions
##

##
def compute_random_delta_parallel(args):
    tree, df, columns, threads, data_sep, id_index, verbose = args
    return compute_random_delta(tree, df, columns, threads, data_sep, id_index, verbose)

##
def compute_random_delta(tree, df, columns, threads, data_sep, id_index, verbose):
    # random shuffle of input data
    shuffled_df = df.sample(frac = 1, random_state = np.random.RandomState())
    shuffled_df.index = df.index

    # compute ACR
    marginal_probs = get_acr_probs(tree, shuffled_df, columns, threads, data_sep, id_index, verbose)

    # Entropies
    entropies = compute_entropies(marginal_probs, verbose)

    # Delta
    pval_delta = compute_delta(entropies, threads, verbose)

    return pval_delta

##
def compute_pvalue(delta, pval_reps, tree, df, columns, threads, data_sep, id_index, verbose):
    p_value = None

    def compute_random_delta_args(reps):
        threads = 1
        for rep in range(1, reps+1):
            yield (tree, df, columns, threads, data_sep, id_index, verbose)

    # obtain random deltas
    random_deltas = []
    with multiprocessing.Pool(processes = threads) as pool:
        random_deltas = pool.imap_unordered(compute_random_delta_parallel, compute_random_delta_args(pval_reps))
        random_deltas = [rd for rd in random_deltas] # parse iterator
        # p-value
        p_value = sum([1 for rd in random_deltas if rd > delta]) / len(random_deltas)

    return p_value

##
## API
##

def compute_delta_pvalue(tree, data, columns, threads, pval_reps, data_sep, id_index, verbose = False):
    delta = p_value = None

    ##
    ## Parse data to File-like object
    ## (needed to shuffle the data and be able to use it
    ## for pastml ACR function which uses pandas read_csv function)
    if verbose == True: print("Parsing input data", file=sys.stderr)

    df = pd.read_csv(data, sep=data_sep, index_col=id_index, header=0, dtype=str)

    ##
    ## Ancestral Character Reconstruction

    marginal_probs = get_acr_probs(tree, df, columns, threads, data_sep, id_index, verbose)

    ##
    ## Compute entropies
    # Based on
    # https://github.com/mrborges23/delta_statistic/blob/master/code.R

    entropies = compute_entropies(marginal_probs, verbose)

    ##
    ## Compute delta
    ## Based on
    # https://github.com/mrborges23/delta_statistic/blob/master/code.R
    delta = compute_delta(entropies, threads, verbose)

    if verbose == True: print(f"delta statistic: {delta}", file=sys.stderr)

    ##
    ## Compute p-value

    if pval_reps > 0:

        if verbose == True: print(f"Computing p-value: {pval_reps} repetitions", file=sys.stderr)

        p_value = compute_pvalue(delta, pval_reps, tree, df, columns, threads, data_sep, id_index, verbose)

        if verbose == True: print(f"p-value: {p_value}", file=sys.stderr)

    return (delta, p_value)

##
## MAIN
##

if '__main__' == __name__:

    ##
    ## Arguments

    tree = sys.argv[1]
    data = sys.argv[2]
    columns = sys.argv[3].split(",")
    if len(sys.argv) > 4:
        threads = int(sys.argv[4])
    else:
        threads = 0

    if len(sys.argv) > 5:
        pval_reps = int(sys.argv[5])
    else:
        pval_reps = 0

    if len(sys.argv) > 6:
        data_sep = sys.argv[6]
    else:
        data_sep = ","

    if len(sys.argv) > 7:
        id_index = int(sys.argv[7])
    else:
        id_index = 0

    threads = threads if threads > 0 else multiprocessing.cpu_count()

    delta, p_value = compute_delta_pvalue(tree, data, columns, threads, pval_reps, data_sep, id_index, verbose = True)

    print(f"delta = {delta}")
    if p_value is not None:
        print(f"p-value = {p_value}")

    print("Finished.", file=sys.stderr)

## END


#-*-coding:utf-8-*-
# 游戏主程序

from gui.graph import *
from pygame.locals import *
import pygame
from sys import exit

from tools.game import Game
from gui.graph import Graph
from gui.lost import Lost
from gui.win import Win
from gui.welcome import Welcome

row=4# 行数
gb=Game(row,True)
gb.start_test()
gr=Graph(row)
gr.start_graph('2048')

we=Welcome(gr.screen)
we.get_welcome()

# 随机生成数和位置并初始化
n=gb.get_rand_num()
v=gb.get_rand_vector()
gb.change_num(v,n)
gr.background()
box=gb.get_box()
gr.playbox(box)
gr.flash()

win_number=2048

end=0# 结束因子，0为未结束，1为输，2为赢

while end==0 or end==2:
    d=None
    for event in pygame.event.get():
        if event.type==QUIT:
            exit()
        elif event.type==KEYDOWN:
                if event.key==K_ESCAPE:
                    exit()
                elif event.key==K_UP:
                    d='u'
                elif event.key==K_DOWN:
                    d='d'
                elif event.key==K_LEFT:
                    d='l'
                elif event.key==K_RIGHT:
                    d='r'

    if d:
        # 移动
        gb.move(d)
        gr.playbox(gb.get_box())
        gr.flash()
        n=gb.get_rand_num()
        v=gb.get_rand_vector()
        if v:
            gb.change_num(v,n)
        else:
            end=1

        max=gb.get_max()# 判断最大值
        # 赢时的画面
        if max==win_number and end==0:
            win=Win(gr.screen)
            win.get_win()
            end=2

    gr.playbox(gb.get_box())
    gr.flash()

# 输时的画面
if end==1:
    lost=Lost(gr.screen)
    lost.get_lost()

gb.end_test()


from typing import List
from fastapi import Depends, Request, APIRouter
from pydantic import BaseModel
from sqlalchemy.orm import Session
from auth.database import User
from models.models import *
from fastapi.templating import Jinja2Templates

templates = Jinja2Templates(directory="templates")
router = APIRouter()

@router.get('/')
def select_all_templates(request: Request, db: Session = Depends(get_db)):
    html_templates = db.query(HTMLtemplate).all()
    return templates.TemplateResponse("main_page.html", {"request": request, 'templates': html_templates})
    #return html_templates

@router.get("/post/{name}")
async def get_post_by_name(request: Request, name: str, db: Session = Depends(get_db)):
    post = db.query(HTMLtemplate).filter(HTMLtemplate.title == name)
    #return {"post": post.__str__()}
    return templates.TemplateResponse("main_page.html", {"request": request, 'templates': post, 'message': 'Шаблоны по вашему запросу:'})

@router.get("/posts/{{ tag_title }}")
def get_posts_on_tag(tag_title: str, db: Session = Depends(get_db)):
    posts = db.query(HTMLtemplate).join(HTMLtemplate.tags).filter(Tag.title == tag_title).all()
    return posts

@router.delete('/post_delete')
def delete_template(db: Session = Depends(get_db), title: str = 'post name'):
    post = db.query(HTMLtemplate).filter(HTMLtemplate.title == title).first()
    if post:
        db.delete(post)
        db.commit()
        return f'Пост "{ title }" успешно удалён'
    else:
        return f'Пост "{ title }" не найден ('

class HTMLtemplateCreate(BaseModel):
    title: str
    text: str
    tag_ids: List[int]

@router.post("/htmltemplates")
def create_htmltemplate(htmltemplate_data: HTMLtemplateCreate, db: Session = Depends(get_db)):
    tag_ids = htmltemplate_data.tag_ids
    htmltemplate = HTMLtemplate(title=htmltemplate_data.title, text=htmltemplate_data.text)

    # берём юзера и теги для добавления
    tags = db.query(Tag).filter(Tag.id.in_(tag_ids)).all()
    author = db.query(User).all()

    # добавляем связь тегов и юзера с постом
    htmltemplate.tags = tags
    htmltemplate.author = author

    # Добавление объекта в сессию и сохранение изменений
    db.add(htmltemplate)
    db.commit()
    db.refresh(htmltemplate)

    #return {"message": "HTMLtemplate created successfully"}
    return htmltemplate


from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier
da=datasets.load_iris()
print(da.keys())
print(da.DESCR)
s1=da.data
s2=da.target
model=KNeighborsClassifier()
model.fit(s1,s2)
y1=model.predict([[4,5,6,7]])
print(y1)


from PIL import Image
#import skimage as sk
#from skimage import io as sk_io
#import cv2

import os
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import image as mp_image

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.withdraw()

#file_path = filedialog.askopenfilename()
file_path = filedialog.askopenfilenames()

print(file_path)

images = []

for file in file_path:
  print(file)
  pil_image = Image.open(file)
  images.append(pil_image)

# Set up a figure of an appropriate size
fig = plt.figure(figsize=(12, 12))

image_num = 0
num_images = len(images)
# loop through the images
for image_idx in range(num_images):
    # Keep an incrementing count of each image
    a=fig.add_subplot(1, num_images, image_idx+1)
    # Add the image to the plot
    image_plot = plt.imshow(images[image_idx])
    # Add a caption with the folder name
    a.set_title("Image " + str(image_idx+1))

# Show the plot
plt.show()

image_num = 0
num_images = len(images)
# loop through the images
for image_idx in range(num_images):
    # Keep an incrementing count of each image
    a=fig.add_subplot(1, num_images, image_idx+1)
    # Add the image to the plot
    image_plot = plt.imshow(images[image_idx])
    # Add a caption with the folder name
    a.set_title("Image " + str(image_idx+1))
plt.show()