import numpy as np
import gymnasium as gym
from matplotlib import animation
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
import env as maBanditWorld

class epsilon_greedy:
    #why does init need __? python needs it
    def __init__(self, n_actions, env, seed):
        self.env = env
        np.random.seed(seed)

        self.number_of_rows = 50
        self.table_of_avg_rewards = np.zeros((self.number_of_rows, n_actions))

        #self.average_reward = [0.0 for i in range(n_actions)]

        self.memory_of_each_pull = [0.0 for i in range(n_actions)]
        self.accumulated_rewards = [0.0 for i in range(n_actions)]
        self.arms_array = [i for i in range(n_actions)]
        self.history_of_pulls = []
        self.steps = []
        self.running_avg = []
        self.history_of_action_distributions = []
        self.running_regret = []
        self.optimal_avg_running_reward = []

        self.sigma_regret = 0
        self.sigma_sum = 0
        self.sigma_pulls = 0

        #find the true best arm
        self.r_dist = self.env.env.getRDist()

        #self.reward = self.env.env.getReward()

        self.array_with_mean_reward_of_each_arm = []
        for i in range(len(self.r_dist)):
            mean_reward_of_each_arm = np.mean(self.r_dist[i])
            self.array_with_mean_reward_of_each_arm.append(mean_reward_of_each_arm)

        self.best_arm = np.argmax(self.array_with_mean_reward_of_each_arm)
        self.mean_reward_of_best_arm = np.max(self.array_with_mean_reward_of_each_arm)

        #print(self.mean_reward_of_best_arm)

    #under the assumption that epsilon is between 0,1
    #under the assumptiong that decay_rate is between 0,1
    #take initial epsilon and then decrease it until we are exploiting near the end of episodes
    def epsilon_decay(self, step_count, decay_rate, epsilon):
        return epsilon * ((1 - decay_rate)**(step_count))

    def epsilon_greedy_policy(self, n_actions, epsilon):
        def policy(observation):
            #exploration; outputs even distribution of all actions then we can select
            if epsilon > np.random.rand(): #rand generates a float
                distribution = [1.0/n_actions for i in range(n_actions)]
                return distribution

            #exploitation: figuring out what the best action is and then making sure that it's a peak so the program picks it
            else:
                distribution = [0.0 for i in range(n_actions)]
                best_action_idx = np.argmax(self.table_of_avg_rewards[observation]) #gets the index of the largest value in accumulated_rewards
                #print(best_action_idx)
                distribution[best_action_idx] += 1.0
                return distribution
        return policy

    def train(self, num_episodes, decay_rate, epsilon, step_count):

        numActions = self.env.env.action_space.n

        for episode_idx in range(num_episodes):
            print("\nEpisode {}/{}".format(episode_idx + 1, num_episodes))

            observation = self.env.env.reset() #why 2 env?
            done = False
            time = 1

            while not done:
                for i in range(step_count):
                    #choose an action using greedy-epsilon policy

                    decayed_epsilon = self.epsilon_decay(step_count=i+1, decay_rate=decay_rate, epsilon=epsilon) #decays within episodes not out of
                    policy = self.epsilon_greedy_policy(n_actions=numActions, epsilon=decayed_epsilon) #epsilon itself outputs a linear regret curve
                    action_distribution = policy(observation)

                    action = np.random.choice(np.arange(len(action_distribution)), p=action_distribution)

                    next_observation, reward, done, _ = self.env.env.step(action)

                    #appending action to history of action distributions for plotting later
                    self.history_of_action_distributions.append(action_distribution)

                    #adding reward to its slot

                    self.accumulated_rewards[action] += reward

                    self.table_of_avg_rewards[observation][action] += reward

                    #when a specific arm is pulled, add that number to that index
                    self.memory_of_each_pull[action] += 1

                    #updating average_reward
                    self.table_of_avg_rewards[observation][action] = self.table_of_avg_rewards[observation][action]/self.memory_of_each_pull[action]

                    #self.average_reward[action] = self.accumulated_rewards[action]/self.memory_of_each_pull[action]

                    #updating sigma_pulls
                    self.sigma_pulls += 1

                    #updating sigma_sum
                    #reward can be negative
                    self.sigma_sum += reward

                    #updating memory
                    self.history_of_pulls.append(action)

                    #updating steps
                    self.steps.append(len(self.steps))

                    #updating running avg reward for each step
                    self.running_avg.append(self.sigma_sum/self.sigma_pulls)

                    # finds mean_reward so we can calculate regret
                    # mean_reward_of_selected_arm = np.mean(self.r_dist[action])

                    #print(self.table_of_avg_rewards[observation][action])
                    #print(self.table_of_avg_rewards[observation]) #should be printing out a row
                    #print(self.table_of_avg_rewards[:, action]) #should print out the col

                    mean_reward_of_selected_arm = np.mean(self.r_dist[action])
                    regret_of_step = self.mean_reward_of_best_arm - mean_reward_of_selected_arm
                    self.sigma_regret += regret_of_step

                    self.running_regret.append(self.sigma_regret)

                    #self.optimal_avg_running_reward.append(self.mean_reward_of_best_arm)

                    #debug
                    print("step {}/{}".format(i, step_count))
                    #print("decayed epsilon: " + str(decayed_epsilon))
                    #print("action distribution: " + str(action_distribution))
                    #print("History of action distrbution: " + str(self.history_of_action_distributions))
                    #print("Observation: " + str(observation))
                    #print("Sigma sum: " + str(self.sigma_sum))
                    #print("Reward: " + str(reward))
                    #print("best arm {} ___ Mean_reward_of_best_arm {} ".format(self.best_arm, self.mean_reward_of_best_arm))
                    #print("selected arm: {} ___ mean_reward_of_selected_arm {}".format(action, mean_reward_of_selected_arm))
                    #print("average reward array: \n" + str(self.average_reward))
                    #print(self.sigma_regret)
                    #print(self.running_regret)
                    #print(self.history_of_pulls)
                    #print("Regret2: " + str(np.max(self.env.env.getRDist())))

                    if done:
                        done = True
                    else:
                        observation = next_observation
                        time += 1

        return self.running_regret

    def plots(self):
        #normalizing bar graph
        for i in range(len(self.memory_of_each_pull)):
            self.memory_of_each_pull[i] = self.memory_of_each_pull[i]/self.sigma_pulls

        #bar graph
        #x will be 0,1,2,3,,4,5, num of arms; y will be num of pulls
        fig1 = plt.figure(figsize = (50,50))
        plt.bar(self.arms_array, self.memory_of_each_pull, color = 'blue', width =.4)
        plt.xlabel("Arm")
        plt.ylabel("Probability")
        plt.title("Probability per arm")

        #converging dots plot arm pulledv. steps
        #x will be steps; y will be num of arms
        fig2 = plt.figure(figsize = (50,50))
        plt.plot(self.steps, self.history_of_pulls)
        plt.xlabel("Time steps")
        plt.ylabel("Arm pulled")
        plt.title("Arm pulled at each time step")

        #optimal reward
        fig3 = plt.figure(figsize = (50,50))
        plt.plot(self.steps, self.optimal_avg_running_reward)
        plt.xlabel("Time steps")
        plt.ylabel("Optimal running avg reward")
        plt.title("Optimal average running reward")

        #continous plot timestepv. reward
        #x will be steps; y will be avg reward
        fig4 = plt.figure(figsize = (50,50))
        plt.plot(self.steps, self.running_avg)
        plt.xlabel("Time steps")
        plt.ylabel("Running Average")
        plt.title("Running average at each time step")

        #regret plot, shows us how close our reward is to actual max reward possible of that step
        #x wlil be steps; y will be regret of each step
        fig5 = plt.figure(figsize = (50,50))
        plt.plot(self.steps, self.running_regret)
        plt.xlabel("Time steps")
        plt.ylabel("Regret")
        plt.title("Time steps vs. Regret")

        plt.show()

        return fig1, fig2, fig4, fig5


"""
Preprocess_Fischbach.py

Ryan Fischbach
Dr. Khuri
CSC373
12/4/2020

Final Project: Scraping Tweets For Classification Of Stock Price

This script takes in the preprocessed_stocks.csv and preprocessed_tweets.csv files and generates visualizations and other EDA to better understand the data.

To run, type "Python eda_fischbach.py {absolute directory path containing CSVs}" into the CLI.
"""

# Import relevant libraries
import numpy as np
import pandas as pd
import sys
import os
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.dates import DateFormatter
import datetime
import sklearn

try:
    dir = sys.argv[1]

    if (not os.path.isdir(dir)):
        print(
            "Please enter a valid absolute path to the directory housing the csvs.")
        sys.exit(1)

except:
    print("Please enter a valid absolute path to the directory of the directory housing csvs after the .py file.")
    sys.exit(1)

try:
    os.chdir(dir)

    #Read in preprocessed csv data
    combinedTweets = pd.read_csv('preprocessed_tweets.csv')
    preprocessedStocks = pd.read_csv('preprocessed_stocks.csv')
except:
    print("CSV could not be read. Please enter a valid absolute path to the directory of purchases.csv and make sure the file is there..")
    sys.exit(1)

#Convert date format
combinedTweets['Date'] = pd.to_datetime(combinedTweets['Date'], dayfirst=True)
preprocessedStocks['Date'] = pd.to_datetime(preprocessedStocks['Date'], dayfirst=True)

# Import the wordcloud library
from wordcloud import WordCloud

# Join the different processed titles together.
long_string = ','.join(list(combinedTweets['Tweets'].values))
# Create a WordCloud object
wordcloud = WordCloud(background_color="white", max_words=5000, contour_width=3, contour_color='steelblue')
# Generate a word cloud
wordcloud.generate(long_string)
# Visualize the word cloud
wordcloud.to_image()

import matplotlib.dates as mdates
import matplotlib.cbook as cbook

years = mdates.YearLocator()   # every year
months = mdates.MonthLocator()  # every month
years_fmt = mdates.DateFormatter('%Y')

fig, ax = plt.subplots()
ax.plot('Date', 'Count', data=combinedTweets)

# format the ticks
ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(years_fmt)
ax.xaxis.set_minor_locator(months)

ax.set_xlabel('Date')
ax.set_ylabel('Number of Tweets')
ax.set_title('Number of Tweets on Date')

# format the coords message box
ax.format_xdata = mdates.DateFormatter('%Y-%m-%d')
ax.format_ydata = lambda x: '$%1.2f' % x  # format the price.
ax.grid(True)

# rotates and right aligns the x labels, and moves the bottom of the
# axes up to make room for them
fig.autofmt_xdate()

plt.show()

import matplotlib.dates as mdates
import matplotlib.cbook as cbook

fig, ax = plt.subplots()
ax.hist('Labels', data=preprocessedStocks)

# format the ticks
ax.set_xlabel('Label')
ax.set_ylabel('Number of Occurences')
ax.set_title('Number of Occurences of Each Label')
ax.set_xticklabels(['> 2% Change', '<= 2% Change'])

# format the coords message box
ax.set_xticks((0,1))
ax.grid(True)

# rotates and right aligns the x labels, and moves the bottom of the
# axes up to make room for them

plt.show()

import matplotlib.dates as mdates
import matplotlib.cbook as cbook

fig, ax = plt.subplots()
ax.hist('Sentiment', data=combinedTweets)

# format the ticks
ax.set_ylabel('Occurences')
ax.set_title('Number of Occurences of Each Sentiment')
ax.set_xticklabels(['Negative', 'Positive'])

# format the coords message box
ax.set_xticks((0,1))
ax.grid(True)

# rotates and right aligns the x labels, and moves the bottom of the
# axes up to make room for them

plt.show()

# Commented out IPython magic to ensure Python compatibility.
# Determine most popular words
from sklearn.feature_extraction.text import CountVectorizer
sns.set_style('whitegrid')
# %matplotlib inline
# Helper function
def plot_10_most_common_words(count_data, count_vectorizer):
    import matplotlib.pyplot as plt
    words = count_vectorizer.get_feature_names()
    total_counts = np.zeros(len(words))
    for t in count_data:
        total_counts+=t.toarray()[0]

    count_dict = (zip(words, total_counts))
    count_dict = sorted(count_dict, key=lambda x:x[1], reverse=True)[0:10]
    words = [w[0] for w in count_dict]
    counts = [w[1] for w in count_dict]
    x_pos = np.arange(len(words))

    plt.figure(2, figsize=(15, 15/1.6180))
    plt.subplot(title='10 most common words')
    sns.set_context("notebook", font_scale=1.25, rc={"lines.linewidth": 2.5})
    sns.barplot(x_pos, counts, palette='husl')
    plt.xticks(x_pos, words, rotation=90)
    plt.xlabel('words')
    plt.ylabel('counts')
    plt.show()
# Initialise the count vectorizer with the English stop words
count_vectorizer = CountVectorizer(stop_words='english')
# Fit and transform the processed titles
count_data = count_vectorizer.fit_transform(combinedTweets.Tweets)
# Visualise the 10 most common words
plot_10_most_common_words(count_data, count_vectorizer)

import warnings
warnings.simplefilter("ignore", DeprecationWarning)
# Load the LDA model from sk-learn
from sklearn.decomposition import LatentDirichletAllocation as LDA

# Helper function
def print_topics(model, count_vectorizer, n_top_words):
    words = count_vectorizer.get_feature_names()
    for topic_idx, topic in enumerate(model.components_):
        print("\nTopic #%d:" % topic_idx)
        print(" ".join([words[i]
                        for i in topic.argsort()[:-n_top_words - 1:-1]]))

# Tweak the two parameters below
number_topics = 5
number_words = 10
# Create and fit the LDA model
lda = LDA(n_components=number_topics, n_jobs=-1)
lda.fit(count_data)
# Print the topics found by the LDA model
print("Topics found via LDA:")
print_topics(lda, count_vectorizer, number_words)


#!/usr/bin/python

from botocore.session import Session

import re
import os
import sys
import botocore
import argparse
import jinja2

def striphtml(data):
    p = re.compile(r'<.*?>')
    return p.sub('', data)

def convert(name):
    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
    return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()

parser = argparse.ArgumentParser(description="Generate aws stackstorm actions")
parser.add_argument('-d', '--outputdir', default="actions", help="base output directory")
parser.add_argument('-s', '--service', default=None, help="service to generate actions for (eg s3)")
args = parser.parse_args()

outputdir = args.outputdir
myservice = args.service

# print "outputdir: %s" % outputdir
# print "myservice: %s" % myservice

try:
    os.stat(outputdir)
except:  # noqa: E722
    os.mkdir(outputdir)

templateLoader = jinja2.FileSystemLoader(searchpath="templates")
templateEnv = jinja2.Environment(loader=templateLoader)

session = Session()

if myservice is None:
    myservices = session.get_available_services()
else:
    myservices = [myservice]

for myservice in myservices:
    print("%s:" % myservice)

    try:
        mysrv = session.get_service_model(myservice)
    except botocore.exceptions.UnknownServiceError as e:
        print("\n%s\n" % e)
        sys.exit(1)

    for op in mysrv.operation_names:  # pylint: disable=not-an-iterable
        allvars = {}
        allvars['paramsreq'] = []
        allvars['params'] = []

        model = mysrv.operation_model(op)

        op = convert(op)

        print(" " + op)
        allvars['action'] = op
        allvars['name'] = myservice + "_" + op
        allvars['service'] = myservice

        if model.input_shape is None:
            continue

        members = model.input_shape.members  # pylint: disable=no-member

        smodel = model.service_model

        # print smodel._shape_resolver

        smembers = model.input_shape._shape_model['members']   # pylint: disable=no-member
        for sname, sdata in smembers.items():
            tmp = {}
            stype = smodel._shape_resolver._shape_map[sdata['shape']]['type']
            # blob defined in boto as bytes or seekable file-like object - not supported here
            if stype == "blob":
                stype = "string"
            if stype == "double":
                stype = "number"
            if stype == "long":
                stype = "integer"
            if stype == "structure":
                stype = "object"
            if stype == "map":
                stype = "object"
            if stype == "list":
                stype = "array"
            if stype == "timestamp":
                stype = "string"
            if stype == "float":
                stype = "number"
            tmp['name'] = sname
            tmp['type'] = stype
            if 'documentation' in sdata:
                tmp['description'] = striphtml(sdata['documentation'].rstrip().replace('"', "'"))
                tmp['description'] = striphtml(tmp['description'].replace("\\", "\\\\"))
            else:
                tmp['description'] = ''

            if sname in model.input_shape.required_members:   # pylint: disable=no-member
                allvars['paramsreq'].append(tmp)
            else:
                allvars['params'].append(tmp)

        actionyaml = outputdir + "/" + allvars['name'] + ".yaml"
        template = templateEnv.get_template('action_template.yaml.jinja')

        with open(actionyaml, 'w') as y:
            outputText = template.render(allvars).encode('utf8')  # pylint: disable=no-member
            y.write(outputText)


__author__ = 'rafek@google.com (Rafe Kaplan)'

import appengine_config

from protorpc.webapp import service_handlers

import tunes_db

def main():
  service_handlers.run_services(
    [('/music', tunes_db.MusicLibraryService),
    ])

if __name__ == '__main__':
  main()


#json
import json

#tkinter library
from   tkinter import *
import tkinter as tk

#pygame library
from pygame import *

#import python Image Library
from PIL import Image, ImageTk

#function declaration

def   quit_app():
      my_window.destroy()

      #button music
def play_music():
    mixer.init()
    mixer.music.load('punch.wav')
    mixer.music.play()

    while mixer.music.get_busy():
          time.Clock().tick(2)

def open_window():

                play_music()

                #my_window.withdraw()
                top=Toplevel()
                top.title('NeuFenster')
                top.geometry("%dx%d+0+0" % (width_value, height_value))
                button_1=Button(top,
                                text="Schließen",
                                font="Times 12",
                                pady=20,
                                padx=20,
                                bg='#40E0D0',
                                command=top.destroy)

                button_1.pack()

# Title Window
my_window=Tk()

#Outsourcing Image
photo=PhotoImage(file='C:\\Users\\moin\\PycharmProjects\\untitled1\\deer.png')

#creation of menubar/associating menubar with my_window instance
my_menubar=Menu(my_window)

#my_menubar.add_command(label='Menu')
my_dropdown_menu=Menu(my_menubar)
my_dropdown_menu.add_command(label='Start',command=open_window)
my_dropdown_menu.add_command(label='Stop',command=open_window)
my_dropdown_menu.add_command(label='Techno', command=open_window)
my_dropdown_menu.add_command(label='power',command=open_window)

my_menubar.add_cascade(label='Menu', menu=my_dropdown_menu)
my_menubar.add_command(label='Quit', command=quit_app)
my_window.config(menu=my_menubar)

# json file
Objektliste='''

{
"Objekt":
      [
      {
      "id": 1,
      "posx": 8,
      "posy": 9,
      "width": 200,
      "height":300,
      "textname": "Start",
      "img": "C:/Users/moin/Desktop/pr3/3.png",
      "color": "blue",
      "beep": "punch.wav",
      "exec": "shutdown.py",
      "answer": 2
      },

      {
      "id": 2,
      "posx": 8,
      "posy": 10,
      "width": 200,
      "height": 300,
      "textname": "Stop",
      "img": "C:/Users/moin/Desktop/pr3/3.png",
      "color": "blue",
      "beep": "punch.wav",
      "exec": "shutdown.py",
      "answer": 2
      },

      {
      "id": 3,
      "posx": 8,
      "posy": 11,
      "width": 200,
      "height": 300,
      "textname": "Techno",
      "img":"C:/Users/moin/Desktop/pr3/4.png",
      "color": "blue",
      "beep": "punch.wav",
      "exec": "shutdown.py",
      "answer": 4
      },

      {
      "id": 4,
      "posx": 8,
      "posy": 12,
      "width": 200,
      "height": 300,
      "textname": "Power",
      "img":  "C:/Users/moin/Desktop/pr3/5.png",
      "color": "blue",
      "beep": "punch.wav",
      "exec": "shutdown.py",
      "answer": 1
      }

      ]

}

'''

#Title of the window
my_window.title("Home Automation")
my_window.configure(background='white')

#full window preview/Vorschau
width_value=my_window.winfo_screenwidth()
height_value=my_window.winfo_screenwidth()

#canvas=Canvas(my_window, width=width_value, height=height_value)
#canvas.pack()
#canvas.create_image(0,0,anchor=NW, image=photo)

my_window.geometry("%dx%d+0+0" % (width_value, height_value))

frame1=tk.Frame(my_window)
frame1.pack(side=tk.TOP, fill=tk.X)

#frame2=tk.Frame(my_window)
#frame2.pack(side=tk.TOP, fill=tk.X)

#frame3=tk.Frame(my_window)
#frame3.pack(side=tk.TOP, fill=tk.X)

data=json.loads(Objektliste)
mycontainer=[]

#Creation of for loop/Erzeugt von for loop
for item in data['Objekt']:
            button  = item['id']
            buttonText = item['textname']
            colour=item['color']
            width=item['width']
            height= item['height']
            posx = item['posx']
            posy = item['posy']
            imagePath=item['img']
            cmd  = item['exec']

            #Creation of button

            myImage=PhotoImage(file=imagePath)

            mycontainer.append(myImage)

            button = Button( frame1,

                             compound=tk.TOP,
                             fg=colour,
                             width=width,
                             height=height,
                             bg="white",

                             font="Times 32",
                             relief="groove",
                             image=myImage,
                             text=buttonText,
                             pady=20,
                             padx=20,
                             command=open_window)

            button.grid(column=posy, row=posx)

            #print(button['text'])
# end of loop

my_window.mainloop()

Finalaufgabe von GUI Entwicklung-1st phase.py


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

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import sqlite3
from contextlib import closing
import scrapy
import json

class MsbrPipeline(object):

    def open_spider(self, spider):
        self.file = open('items.jl', 'w')

    def close_spider(self, spider):
        self.file.close()

    @classmethod
    def from_crawler(cls, crawler):
        return cls(crawler.settings.get('DBPATH'))

    def __init__(self, dbpath=None):
        self.conn = sqlite3.connect(dbpath)
        with closing(self.conn.cursor()) as cur:
            cur.execute(
                '''
                CREATE TABLE IF NOT EXISTS data23 (
                        product_name str NOT NULL,
                        product_price str NOT NULL,
                        product_link str DEFAULT 0,
                        image str,
                        type str
                        ) ;
                ''')

    def process_item(self, item, spider):
        print('product process item')
        line = json.dumps(dict(item)) + "\n"
        self.file.write(line)
        with closing(self.conn.cursor()) as cur:
            cur.execute(
                '''
                INSERT OR IGNORE
                INTO data23 (product_name, product_price, product_link, image, type)
                VALUES ( ?, ?, ?, ?, ? );
                ''',
                (item['product_name'], item['product_price'], item['product_link'],
                 item['image'], item['type'])
            )
            return item


from typing import Any, List, Optional
import discord
from discord.components import SelectOption
from discord.ext import commands
import logging
import logging.handlers
from discord.interactions import Interaction

from discord.utils import MISSING

logger = logging.getLogger('discord')
logger.setLevel(logging.INFO)
handler = logging.handlers.RotatingFileHandler(
    filename='discord.log',
    encoding='utf-8',
    maxBytes=32 * 1024 * 1024,  # 32 MiB
    backupCount=5,  # Rotate through 5 files
)
dt_fmt = '%Y-%m-%d %H:%M:%S'
formatter = logging.Formatter('[{asctime}] [{levelname:<8}] {name}: {message}', dt_fmt, style='{')
handler.setFormatter(formatter)
logger.addHandler(handler)

class Logger:
    def __init__(self) -> None:
        self.logger = logger

class cog(commands.Cog):
    def __init__(self,bot):
        self.bot = bot
        self.logger = logger

class buttonCore(discord.ui.View):
    def __init__(self, *, timeout: float | None = 180):
        super().__init__(timeout=timeout)
        self.value = None
        self.logger = logger

class menuCore(discord.ui.Select):
    def __init__(self, *, custom_id: str = ..., placeholder: str | None = None, min_values: int = 1, max_values: int = 1, options: List[SelectOption] = ..., disabled: bool = False, row: int | None = None) -> None:
        super().__init__(custom_id=custom_id, placeholder=placeholder, min_values=min_values, max_values=max_values, options=options, disabled=disabled, row=row)
        self.logger = logger

    async def callback(self, interaction: discord.Interaction): ...

class DropdownView(discord.ui.View):
    def __init__(self,menu:menuCore, *, custom_id: str = ..., placeholder: str | None = None, min_values: int = 1, max_values: int = 1, options: List[SelectOption] = ..., disabled: bool = False, row: int | None = None):
        super().__init__()
        # Adds the dropdown to our view object.
        self.add_item(menu(custom_id=custom_id, placeholder=placeholder, min_values=min_values, max_values=max_values, options=options, disabled=disabled, row=row))


###############################################################################
#                                                                             #
#    This program is free software: you can redistribute it and/or modify     #
#    it under the terms of the GNU General Public License as published by     #
#    the Free Software Foundation, either version 3 of the License, or        #
#    (at your option) any later version.                                      #
#                                                                             #
#    This program is distributed in the hope that it will be useful,          #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
#    GNU General Public License for more details.                             #
#                                                                             #
#    You should have received a copy of the GNU General Public License        #
#    along with this program. If not, see <http://www.gnu.org/licenses/>.     #
#                                                                             #
###############################################################################

import numpy as np

import matplotlib
import mpld3

from biolib.common import find_nearest

from refinem.plots.base_plot import BasePlot
from refinem.plots.mpld3_plugins import Tooltip

class GcPlots(BasePlot):
    """Create histogram and scatterplot showing GC distribution of scaffolds."""

    def __init__(self, options):
        """Initialize."""
        BasePlot.__init__(self, options)

    def data_pts(self, genome_scaffold_stats, mean_gc):
        """Get data points to plot.

        Parameters
        ----------
        genome_scaffold_stats : d[scaffold_id] -> namedtuple of scaffold stats
          Statistics for scaffolds in genome.

        Returns
        -------
        dict : d[scaffold_id] -> (x, y)
        """

        pts = {}
        for scaffold_id, stats in genome_scaffold_stats.items():
            pts[scaffold_id] = (stats.gc - mean_gc, stats.length / 1000.0)

        return pts

    def plot(self, genome_scaffold_stats, highlight_scaffold_ids, link_scaffold_ids, mean_gc, gc_dist, percentiles_to_plot):
        """Setup figure for plots.

        Parameters
        ----------
        genome_scaffold_stats : d[scaffold_id] -> namedtuple of scaffold stats
          Statistics for scaffolds in genome.
        highlight_scaffold_ids : d[scaffold_id] -> color
            Scaffolds in genome to highlight.
        link_scaffold_ids : list of scaffold pairs
            Pairs of scaffolds to link together.
        mean_gc : float
          Mean GC of genome.
        gc_dist : d[gc][length][percentile] -> critical value
          GC distribution.
        percentiles_to_plot : iterable
          Percentile values to mark on plot.
        """

        # Set size of figure
        self.fig.clear()

        mpld3.plugins.clear(self.fig)
        mpld3.plugins.connect(self.fig, mpld3.plugins.Reset(), mpld3.plugins.BoxZoom(), mpld3.plugins.Zoom())
        mpld3.plugins.connect(self.fig, mpld3.plugins.MousePosition(fontsize=12, fmt='.1f'))

        self.fig.set_size_inches(self.options.width, self.options.height)

        axes_hist = self.fig.add_subplot(121)
        axes_scatter = self.fig.add_subplot(122)

        self.plot_on_axes(self.fig, genome_scaffold_stats,
                          highlight_scaffold_ids,
                          link_scaffold_ids,
                          mean_gc, gc_dist, percentiles_to_plot,
                          axes_hist, axes_scatter, True)

        self.fig.tight_layout(pad=1, w_pad=1)
        self.draw()

    def plot_on_axes(self, figure,
                     genome_scaffold_stats,
                     highlight_scaffold_ids, link_scaffold_ids,
                     mean_gc, gc_dist, percentiles_to_plot,
                     axes_hist, axes_scatter, tooltip_plugin):
        """Create histogram and scatterplot.

        Parameters
        ----------
        figure : matplotlib.figure
          Figure on which to render axes.
        genome_scaffold_stats : d[scaffold_id] -> namedtuple of scaffold stats
          Statistics for scaffolds in genome.
        highlight_scaffold_ids : d[scaffold_id] -> color
            Scaffolds in genome to highlight.
        link_scaffold_ids : list of scaffold pairs
            Pairs of scaffolds to link together.
        mean_gc : float
          Mean GC of genome.
        gc_dist : d[gc][length][percentile] -> critical value
          GC distribution.
        percentiles_to_plot : iterable
          Percentile values to mark on plot.
        """

        # histogram plot
        if axes_hist:
            scaffold_gc = [stats.gc for stats in genome_scaffold_stats.values()]
            ylabel = '# scaffolds (out of %d)' % len(scaffold_gc)
            self.histogram(axes_hist, scaffold_gc, 20, 80, 2, '% GC', ylabel)

        # scatterplot
        xlabel = 'delta GC (mean = %.1f%%)' % mean_gc
        ylabel = 'Scaffold length (kbp)'

        pts = self.data_pts(genome_scaffold_stats, mean_gc)

        scatter, x_pts, y_pts, plot_labels = self.scatter(axes_scatter,
                                                             pts,
                                                             highlight_scaffold_ids,
                                                             link_scaffold_ids,
                                                             xlabel, ylabel)

        _, ymax = axes_scatter.get_ylim()
        xmin, xmax = axes_scatter.get_xlim()

        # draw vertical line at x=0
        axes_scatter.plot([0, 0], [0, ymax], linestyle='dashed', color=self.axes_colour, lw=1.0, zorder=0)

        # plot reference distributions
        closest_gc = find_nearest(list(gc_dist.keys()), mean_gc / 100)
        for percentile in percentiles_to_plot:
            # find closest distribution values
            temp_scaffold_len = list(gc_dist[closest_gc].keys())[0]
            d = gc_dist[closest_gc][temp_scaffold_len]
            gc_lower_bound_key = find_nearest(list(d.keys()), (100 - percentile) / 2.0)
            gc_upper_bound_key = find_nearest(list(d.keys()), (100 + percentile) / 2.0)

            xL = []
            xU = []
            y = []
            for window_size in gc_dist[closest_gc]:
                xL.append(gc_dist[closest_gc][window_size][gc_lower_bound_key] * 100)
                xU.append(gc_dist[closest_gc][window_size][gc_upper_bound_key] * 100)
                y.append(window_size / 1000.0)

            # sort by y-values
            sort_indexY = np.argsort(y)
            xL = np.array(xL)[sort_indexY]
            xU = np.array(xU)[sort_indexY]
            y = np.array(y)[sort_indexY]
            axes_scatter.plot(xL, y, 'r--', lw=1.0, zorder=0)
            axes_scatter.plot(xU, y, 'r--', lw=1.0, zorder=0)

        # ensure y-axis include zero and covers all sequences
        axes_scatter.set_ylim([0, ymax])

        # ensure x-axis is set appropriately for sequences
        axes_scatter.set_xlim([xmin, xmax])

        # tooltips plugin
        if tooltip_plugin:
            tooltip = Tooltip(scatter, labels=plot_labels, hoffset=5, voffset=-15)
            mpld3.plugins.connect(figure, tooltip)

        return scatter, x_pts, y_pts, self.plot_order(plot_labels)


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

import torch
from torch import nn

from ..layers import TextEncoder
from ..layers.decoders import get_decoder
from ..utils.misc import get_n_params
from ..vocabulary import Vocabulary
from ..utils.topology import Topology
from ..utils.ml_metrics import Loss
from ..utils.device import DEVICE
from ..utils.misc import pbar
from ..utils.data import sort_predictions
from ..datasets import MultimodalDataset
from ..metrics import Metric

logger = logging.getLogger('nmtpytorch')

class NMT(nn.Module):
    supports_beam_search = True

    def set_defaults(self):
        self.defaults = {
            'emb_dim': 128,             # Source and target embedding sizes
            'emb_maxnorm': None,        # Normalize embeddings l2 norm to 1
            'emb_gradscale': False,     # Scale embedding gradients w.r.t. batch frequency
            'enc_dim': 256,             # Encoder hidden size
            'enc_type': 'gru',          # Encoder type (gru|lstm)
            'enc_lnorm': False,         # Add layer-normalization to encoder output
            'enc_bidirectional': True,  # Whether the RNN encoder should be bidirectional
            'n_encoders': 1,            # Number of stacked encoders
            'dec_dim': 256,             # Decoder hidden size
            'dec_type': 'gru',          # Decoder type (gru|lstm)
            'dec_variant': 'cond',      # (cond|simplegru|vector)
            'dec_init': 'mean_ctx',     # How to initialize decoder (zero/mean_ctx/feats)
            'dec_init_size': None,      # feature vector dimensionality for
            'dec_init_activ': 'tanh',   # Decoder initialization activation func
                                        # dec_init == 'feats'
            'att_type': 'mlp',          # Attention type (mlp|dot)
            'att_temp': 1.,             # Attention temperature
            'att_activ': 'tanh',        # Attention non-linearity (all torch nonlins)
            'att_mlp_bias': False,      # Enables bias in attention mechanism
            'att_bottleneck': 'ctx',    # Bottleneck dimensionality (ctx|hid)
            'att_transform_ctx': True,  # Transform annotations before attention
            'att_ctx2hid': True,        # Add one last FC layer on top of the ctx
            'dropout_emb': 0,           # Simple dropout to source embeddings
            'dropout_ctx': 0,           # Simple dropout to source encodings
            'dropout_out': 0,           # Simple dropout to decoder output
            'dropout_enc': 0,           # Intra-encoder dropout if n_encoders > 1
            'tied_emb': False,          # Share embeddings: (False|2way|3way)
            'direction': None,          # Network directionality, i.e. en->de
            'max_len': 80,              # Reject sentences where 'bucket_by' length > 80
            'bucket_by': None,          # A key like 'en' to define w.r.t which dataset
                                        # the batches will be sorted
            'bucket_order': None,       # Curriculum: ascending/descending/None
            'sampler_type': 'bucket',   # bucket or approximate
            'sched_sampling': 0,        # Scheduled sampling ratio
            'short_list': 0,            # Short list vocabularies (0: disabled)
            'bos_type': 'emb',          # 'emb': default learned emb
            'bos_activ': None,          #
            'bos_dim': None,            #
            'out_logic': 'simple',      # 'simple' or 'deep' output
            'dec_inp_activ': None,      # Non-linearity for GRU2 input in dec
        }

    def __init__(self, opts):
        super().__init__()

        # opts -> config file sections {.model, .data, .vocabulary, .train}
        self.opts = opts

        # Vocabulary objects
        self.vocabs = {}

        # Each auxiliary loss should be stored inside this dictionary
        # in order to be taken into account by the mainloop for multi-tasking
        self.aux_loss = {}

        # Setup options
        self.opts.model = self.set_model_options(opts.model)

        # Parse topology & languages
        self.topology = Topology(self.opts.model['direction'])

        # Load vocabularies here
        for name, fname in self.opts.vocabulary.items():
            self.vocabs[name] = Vocabulary(fname, short_list=self.opts.model['short_list'])

        # Inherently non multi-lingual aware
        slangs = self.topology.get_src_langs()
        tlangs = self.topology.get_trg_langs()
        if slangs:
            self.sl = slangs[0]
            self.src_vocab = self.vocabs[self.sl]
            self.n_src_vocab = len(self.src_vocab)
        if tlangs:
            self.tl = tlangs[0]
            self.trg_vocab = self.vocabs[self.tl]
            self.n_trg_vocab = len(self.trg_vocab)
            # Need to be set for early-stop evaluation
            # NOTE: This should come from config or elsewhere
            self.val_refs = self.opts.data['val_set'][self.tl]

        # Check vocabulary sizes for 3way tying
        if self.opts.model.get('tied_emb', False) not in [False, '2way', '3way']:
            raise RuntimeError(
                "'{}' not recognized for tied_emb.".format(self.opts.model['tied_emb']))

        if self.opts.model.get('tied_emb', False) == '3way':
            assert self.n_src_vocab == self.n_trg_vocab, \
                "The vocabulary sizes do not match for 3way tied embeddings."

    def __repr__(self):
        s = super().__repr__() + '\n'
        for vocab in self.vocabs.values():
            s += "{}\n".format(vocab)
        s += "{}\n".format(get_n_params(self))
        return s

    def set_model_options(self, model_opts):
        self.set_defaults()
        for opt, value in model_opts.items():
            if opt in self.defaults:
                # Override defaults from config
                self.defaults[opt] = value
            else:
                logger.info('Warning: unused model option: {}'.format(opt))
        return self.defaults

    def reset_parameters(self):
        for name, param in self.named_parameters():
            # Skip 1-d biases and scalars
            if param.requires_grad and param.dim() > 1:
                nn.init.kaiming_normal_(param.data)
        # Reset padding embedding to 0
        if hasattr(self, 'enc') and hasattr(self.enc, 'emb'):
            with torch.no_grad():
                self.enc.emb.weight.data[0].fill_(0)

    def setup(self, is_train=True):
        """Sets up NN topology by creating the layers."""
        ########################
        # Create Textual Encoder
        ########################
        self.enc = TextEncoder(
            input_size=self.opts.model['emb_dim'],
            hidden_size=self.opts.model['enc_dim'],
            n_vocab=self.n_src_vocab,
            bidirectional=self.opts.model['enc_bidirectional'],
            rnn_type=self.opts.model['enc_type'],
            dropout_emb=self.opts.model['dropout_emb'],
            dropout_ctx=self.opts.model['dropout_ctx'],
            dropout_rnn=self.opts.model['dropout_enc'],
            num_layers=self.opts.model['n_encoders'],
            emb_maxnorm=self.opts.model['emb_maxnorm'],
            emb_gradscale=self.opts.model['emb_gradscale'],
            layer_norm=self.opts.model['enc_lnorm'])

        self.ctx_sizes = {str(self.sl): self.enc.ctx_size}

        ################
        # Create Decoder
        ################
        Decoder = get_decoder(self.opts.model['dec_variant'])
        self.dec = Decoder(
            input_size=self.opts.model['emb_dim'],
            hidden_size=self.opts.model['dec_dim'],
            n_vocab=self.n_trg_vocab,
            rnn_type=self.opts.model['dec_type'],
            ctx_size_dict=self.ctx_sizes,
            ctx_name=str(self.sl),
            tied_emb=self.opts.model['tied_emb'],
            dec_init=self.opts.model['dec_init'],
            dec_init_size=self.opts.model['dec_init_size'],
            dec_init_activ=self.opts.model['dec_init_activ'],
            att_type=self.opts.model['att_type'],
            att_temp=self.opts.model['att_temp'],
            att_activ=self.opts.model['att_activ'],
            att_ctx2hid=self.opts.model['att_ctx2hid'],
            transform_ctx=self.opts.model['att_transform_ctx'],
            mlp_bias=self.opts.model['att_mlp_bias'],
            att_bottleneck=self.opts.model['att_bottleneck'],
            dropout_out=self.opts.model['dropout_out'],
            emb_maxnorm=self.opts.model['emb_maxnorm'],
            emb_gradscale=self.opts.model['emb_gradscale'],
            sched_sample=self.opts.model['sched_sampling'],
            bos_type=self.opts.model['bos_type'],
            bos_dim=self.opts.model['bos_dim'],
            bos_activ=self.opts.model['bos_activ'],
            bos_bias=self.opts.model['bos_type'] == 'feats',
            out_logic=self.opts.model['out_logic'],
            dec_inp_activ=self.opts.model['dec_inp_activ'])

        # Share encoder and decoder weights
        if self.opts.model['tied_emb'] == '3way':
            self.enc.emb.weight = self.dec.emb.weight

    def load_data(self, split, batch_size, mode='train'):
        """Loads the requested dataset split."""
        self.dataset = MultimodalDataset(
            data=self.opts.data['{}_set'.format(split)],
            mode=mode, batch_size=batch_size,
            vocabs=self.vocabs, topology=self.topology,
            bucket_by=self.opts.model['bucket_by'],
            max_len=self.opts.model['max_len'],
            bucket_order=self.opts.model['bucket_order'],
            sampler_type=self.opts.model['sampler_type'])
        logger.info(self.dataset)
        return self.dataset

    def get_bos(self, batch_size):
        """Returns a representation for <bos> embeddings for decoding."""
        return torch.LongTensor(batch_size).fill_(self.trg_vocab['<bos>'])

    def encode(self, batch, **kwargs):
        """Encodes all inputs and returns a dictionary.

        Arguments:
            batch (dict): A batch of samples with keys designating the
                information sources.

        Returns:
            dict:
                A dictionary where keys are source modalities compatible
                with the data loader and the values are tuples where the
                elements are encodings and masks. The mask can be ``None``
                if the relevant modality does not require a mask.
        """
        d = {str(self.sl): self.enc(batch[self.sl])}
        if 'feats' in batch:
            d['feats'] = (batch['feats'], None)
        return d

    def forward(self, batch, **kwargs):
        """Computes the forward-pass of the network and returns batch loss.

        Arguments:
            batch (dict): A batch of samples with keys designating the source
                and target modalities.

        Returns:
            Tensor:
                A scalar loss normalized w.r.t batch size and token counts.
        """
        # Get loss dict
        result = self.dec(self.encode(batch), batch[self.tl])
        result['n_items'] = torch.nonzero(batch[self.tl][1:]).shape[0]
        return result

    def test_performance(self, data_loader, dump_file=None):
        """Computes test set loss over the given DataLoader instance."""
        loss = Loss()

        for batch in pbar(data_loader, unit='batch'):
            batch.device(DEVICE)
            out = self.forward(batch)
            loss.update(out['loss'], out['n_items'])

        return [
            Metric('LOSS', loss.get(), higher_better=False),
        ]

    def get_decoder(self, task_id=None):
        """Compatibility function for multi-tasking architectures."""
        return self.dec

    def register_tensorboard(self, handle):
        """Stores tensorboard hook for custom logging."""
        self.tboard = handle

    @staticmethod
    def beam_search(models, data_loader, task_id=None, beam_size=12, max_len=200,
                    lp_alpha=0., suppress_unk=False, n_best=False):
        """An efficient implementation for beam-search algorithm.

        Arguments:
            models (list of Model): Model instance(s) derived from `nn.Module`
                defining a set of methods. See `models/nmt.py`.
            data_loader (DataLoader): A ``DataLoader`` instance.
            task_id (str, optional): For multi-output models, this selects
                the decoder. (Default: None)
            beam_size (int, optional): The size of the beam. (Default: 12)
            max_len (int, optional): Maximum target length to stop beam-search
                if <eos> is still not generated. (Default: 200)
            lp_alpha (float, optional): If > 0, applies Google's length-penalty
                normalization instead of simple length normalization.
                lp: ((5 + |Y|)^lp_alpha / (5 + 1)^lp_alpha)
            suppress_unk (bool, optional): If `True`, suppresses the log-prob
                of <unk> token.
            n_best (bool, optional): If `True`, returns n-best list of the beam
                with the associated scores.

        Returns:
            list:
                A list of hypotheses in surface form.
        """
        def tile_ctx_dict(ctx_dict, idxs):
            """Returns dict of 3D tensors repeatedly indexed along the sample axis."""
            # 1st: tensor, 2nd optional mask
            return {
                k: (t[:, idxs], None if mask is None else mask[:, idxs])
                for k, (t, mask) in ctx_dict.items()
            }

        def check_context_ndims(ctx_dict):
            for name, (ctx, mask) in ctx_dict.items():
                assert ctx.dim() == 3, \
                    f"{name}'s 1st dim should always be a time dimension."

        # This is the batch-size requested by the user but with sorted
        # batches, efficient batch-size will be <= max_batch_size
        max_batch_size = data_loader.batch_sampler.batch_size
        k = beam_size
        inf = -1000
        results = []
        enc_args = {}

        if task_id is None:
            # For classical models that have single encoder, decoder and
            # target vocabulary
            decs = [m.dec for m in models]
            f_inits = [dec.f_init for dec in decs]
            f_nexts = [dec.f_next for dec in decs]
            vocab = models[0].trg_vocab
        else:
            # A specific input-output topology has been requested
            task = Topology(task_id)
            enc_args['enc_ids'] = task.srcs
            # For new multi-target models: select the first target decoder
            decs = [m.get_decoder(task.first_trg) for m in models]
            # Get the necessary init() and next() methods
            f_inits = [dec.f_init for dec in decs]
            f_nexts = [dec.f_next for dec in decs]
            # Get the corresponding vocabulary for the first target
            vocab = models[0].vocabs[task.first_trg]

        # Common parts
        encoders = [m.encode for m in models]
        unk = vocab['<unk>']
        eos = vocab['<eos>']
        n_vocab = len(vocab)

        # Tensorized beam that will shrink and grow up to max_batch_size
        beam_storage = torch.zeros(
            max_len, max_batch_size, k, dtype=torch.long, device=DEVICE)
        mask = torch.arange(max_batch_size * k, device=DEVICE)
        nll_storage = torch.zeros(max_batch_size, device=DEVICE)

        for batch in pbar(data_loader, unit='batch'):
            batch.device(DEVICE)

            # Always use the initial storage
            beam = beam_storage.narrow(1, 0, batch.size).zero_()

            # Mask to apply to pdxs.view(-1) to fix indices
            nk_mask = mask.narrow(0, 0, batch.size * k)

            # nll: batch_size x 1 (will get expanded further)
            nll = nll_storage.narrow(0, 0, batch.size).unsqueeze(1)

            # Tile indices to use in the loop to expand first dim
            tile = range(batch.size)

            # Encode source modalities
            ctx_dicts = [encode(batch, **enc_args) for encode in encoders]

            # Sanity check one of the context dictionaries for dimensions
            check_context_ndims(ctx_dicts[0])

            # Get initial decoder state (N*H)
            h_ts = [f_init(ctx_dict) for f_init, ctx_dict in zip(f_inits, ctx_dicts)]

            # we always have <bos> tokens except that the returned embeddings
            # may differ from one model to another.
            idxs = models[0].get_bos(batch.size).to(DEVICE)

            for tstep in range(max_len):
                # Select correct positions from source context
                ctx_dicts = [tile_ctx_dict(cd, tile) for cd in ctx_dicts]

                # Get log probabilities and next state
                # log_p: batch_size x vocab_size (t = 0)
                #        batch_size*beam_size x vocab_size (t > 0)
                # NOTE: get_emb does not exist in some models, fix this.
                log_ps, h_ts = zip(
                    *[f_next(cd, dec.get_emb(idxs, tstep), h_t[tile]) for
                      f_next, dec, cd, h_t in zip(f_nexts, decs, ctx_dicts, h_ts)])

                # Do the actual averaging of log-probabilities
                log_p = sum(log_ps).data

                if suppress_unk:
                    log_p[:, unk] = inf

                # Detect <eos>'d hyps
                idxs = (idxs == 2).nonzero()
                if idxs.numel():
                    if idxs.numel() == batch.size * k:
                        break
                    idxs.squeeze_(-1)
                    # Unfavor all candidates
                    log_p.index_fill_(0, idxs, inf)
                    # Favor <eos> so that it gets selected
                    log_p.view(-1).index_fill_(0, idxs * n_vocab + 2, 0)

                # Expand to 3D, cross-sum scores and reduce back to 2D
                # log_p: batch_size x vocab_size ( t = 0 )
                #   nll: batch_size x beam_size (x 1)
                # nll becomes: batch_size x beam_size*vocab_size here
                # Reduce (N, K*V) to k-best
                nll, beam[tstep] = nll.unsqueeze_(2).add(log_p.view(
                    batch.size, -1, n_vocab)).view(batch.size, -1).topk(
                        k, sorted=False, largest=True)

                # previous indices into the beam and current token indices
                pdxs = beam[tstep] / n_vocab
                beam[tstep].remainder_(n_vocab)
                idxs = beam[tstep].view(-1)

                # Compute correct previous indices
                # Mask is needed since we're in flattened regime
                tile = pdxs.view(-1) + (nk_mask / k) * (k if tstep else 1)

                if tstep > 0:
                    # Permute all hypothesis history according to new order
                    beam[:tstep] = beam[:tstep].gather(2, pdxs.repeat(tstep, 1, 1))

            # Put an explicit <eos> to make idxs_to_sent happy
            beam[max_len - 1] = eos

            # Find lengths by summing tokens not in (pad,bos,eos)
            len_penalty = beam.gt(2).float().sum(0).clamp(min=1)

            if lp_alpha > 0.:
                len_penalty = ((5 + len_penalty)**lp_alpha) / 6**lp_alpha

            # Apply length normalization
            nll.div_(len_penalty)

            if n_best:
                # each elem is sample, then candidate
                tbeam = beam.permute(1, 2, 0).to('cpu').tolist()
                scores = nll.to('cpu').tolist()
                results.extend(
                    [(vocab.list_of_idxs_to_sents(b), s) for b, s in zip(tbeam, scores)])
            else:
                # Get best-1 hypotheses
                top_hyps = nll.topk(1, sorted=False, largest=True)[1].squeeze(1)
                hyps = beam[:, range(batch.size), top_hyps].t().to('cpu')
                results.extend(vocab.list_of_idxs_to_sents(hyps.tolist()))

        # Recover order of the samples if necessary
        return sort_predictions(data_loader, results)


import chess
import chess.engine
import chess.pgn
import os
from decouple import config
import numpy as np

def fen_to_matrix(fen):
    piece_values = {"P": 1, "R": 5, "N": 3, "B": 3, "K": 100, "Q": 9, "p": -1, "r": -5, "n": -3, "b": -3, "k": -100,
                    "q": -9}
    matrix = [[0 for _ in range(8)] for _ in range(8)]
    records = fen.split()
    board = records[0]
    position_rows = board.split("/")
    contor = 0
    for position_row in position_rows:
        for potential_piece in position_row:
            if potential_piece.isalpha():
                matrix[int(contor / 8)][contor % 8] = piece_values[potential_piece] / piece_values["K"]
                contor += 1
            else:
                contor += int(potential_piece)
    return matrix

def fen_to_sparse_matrix6(fen):
    piece_map = {
        "P": [[0 for _ in range(8)] for _ in range(8)],
        "R": [[0 for _ in range(8)] for _ in range(8)],
        "N": [[0 for _ in range(8)] for _ in range(8)],
        "B": [[0 for _ in range(8)] for _ in range(8)],
        "K": [[0 for _ in range(8)] for _ in range(8)],
        "Q": [[0 for _ in range(8)] for _ in range(8)],
    }
    records = fen.split()
    board = records[0]
    position_rows = board.split("/")
    contor = 0
    for position_row in position_rows:
        for potential_piece in position_row:
            if potential_piece.isalpha():
                if potential_piece.isupper():
                    piece_map[potential_piece][int(contor / 8)][contor % 8] = 1
                else:
                    piece_map[potential_piece.upper()][int(contor / 8)][contor % 8] = -1
                contor += 1
            else:
                contor += int(potential_piece)
    return [piece_map["P"], piece_map["R"], piece_map["N"],
            piece_map["B"], piece_map["K"], piece_map["Q"]]

def fen_to_sparse_matrix12(fen):
    piece_map = {
        "P": [[0 for _ in range(8)] for _ in range(8)],
        "R": [[0 for _ in range(8)] for _ in range(8)],
        "N": [[0 for _ in range(8)] for _ in range(8)],
        "B": [[0 for _ in range(8)] for _ in range(8)],
        "K": [[0 for _ in range(8)] for _ in range(8)],
        "Q": [[0 for _ in range(8)] for _ in range(8)],
        "p": [[0 for _ in range(8)] for _ in range(8)],
        "r": [[0 for _ in range(8)] for _ in range(8)],
        "n": [[0 for _ in range(8)] for _ in range(8)],
        "b": [[0 for _ in range(8)] for _ in range(8)],
        "k": [[0 for _ in range(8)] for _ in range(8)],
        "q": [[0 for _ in range(8)] for _ in range(8)],
    }
    records = fen.split()
    board = records[0]
    who_moves=records[1]
    position_rows = board.split("/")
    contor = 0
    for position_row in position_rows:
        for potential_piece in position_row:
            if potential_piece.isalpha():
                piece_map[potential_piece][int(contor / 8)][contor % 8] = 1
                contor += 1
            else:
                contor += int(potential_piece)
    return [piece_map["P"], piece_map["R"], piece_map["N"],
            piece_map["B"], piece_map["K"], piece_map["Q"], piece_map["p"], piece_map["r"], piece_map["n"],
            piece_map["b"], piece_map["k"], piece_map["q"]]

def fen_to_sparse_matrix13(fen):
    piece_map = {
        "P": [[0 for _ in range(8)] for _ in range(8)],
        "R": [[0 for _ in range(8)] for _ in range(8)],
        "N": [[0 for _ in range(8)] for _ in range(8)],
        "B": [[0 for _ in range(8)] for _ in range(8)],
        "K": [[0 for _ in range(8)] for _ in range(8)],
        "Q": [[0 for _ in range(8)] for _ in range(8)],
        "p": [[0 for _ in range(8)] for _ in range(8)],
        "r": [[0 for _ in range(8)] for _ in range(8)],
        "n": [[0 for _ in range(8)] for _ in range(8)],
        "b": [[0 for _ in range(8)] for _ in range(8)],
        "k": [[0 for _ in range(8)] for _ in range(8)],
        "q": [[0 for _ in range(8)] for _ in range(8)],
    }
    records = fen.split()
    board = records[0]
    who_moves=records[1]
    position_rows = board.split("/")
    contor = 0
    for position_row in position_rows:
        for potential_piece in position_row:
            if potential_piece.isalpha():
                piece_map[potential_piece][int(contor / 8)][contor % 8] = 1
                contor += 1
            else:
                contor += int(potential_piece)
    color_to_move=np.zeros(shape=(8,8)) if who_moves == "b" else np.ones(shape=(8,8))
    return [color_to_move, piece_map["P"], piece_map["R"], piece_map["N"],
            piece_map["B"], piece_map["K"], piece_map["Q"], piece_map["p"], piece_map["r"], piece_map["n"],
            piece_map["b"], piece_map["k"], piece_map["q"]]

def getLines(inputFilePath):
    with open(inputFilePath, 'r') as file:
        lines=file.readlines()
    return lines

def getTrainDataDenseMatrix(inputFilePath):
    X_train = np.empty(shape=(0, 8, 8))
    Y_train = np.empty(shape=(0))
    with open(inputFilePath, 'r') as file:
        for line in file:
            line_elems = line.split(":")
            fen, evaluation = line_elems[0], line_elems[1]
            if "#" in evaluation:
                continue
            X_train = np.append(X_train, np.array([fen_to_matrix(fen)]), axis=0)
            Y_train = np.append(Y_train, np.array([int(evaluation)]), axis=0)
    return X_train, Y_train

def getTrainDataSparseMatrix6(inputFilePath):
    X_train = np.empty(shape=(0, 6, 8, 8))
    Y_train = np.empty(shape=(0))
    with open(inputFilePath, 'r') as file:
        for line in file:
            line_elems = line.split(":")
            fen, evaluation = line_elems[0], line_elems[1]
            if "#" in evaluation:
                continue
            X_train = np.append(X_train, np.array([fen_to_sparse_matrix6(fen)]), axis=0)
            Y_train = np.append(Y_train, np.array([int(evaluation)]), axis=0)
    return X_train, Y_train

def getTrainDataSparseMatrix12Classification(lines):
    X_train = np.zeros(shape=(len(lines), 12, 8, 8))
    Y_train = np.zeros(shape=(len(lines), 1))
    i = 0
    for idx, line in enumerate(lines):
        line = line.strip()
        line_elems = line.split(":")
        fen, evaluation = line_elems[0], line_elems[1]
        if "#" in evaluation:
            continue
        X_train[idx] = np.array(fen_to_sparse_matrix12(fen))
        if int(evaluation) < -100:
            Y_train[idx] = np.array([0])
        elif -100 < int(evaluation) < 100:
            Y_train[idx] = np.array([1])
        else:
            Y_train[idx] = np.array([2])
        i += 1
        if i % 10000 == 0:
            print("Gata inca 10000 suntem la : " + str(i))
    return X_train, Y_train

def save_train_data(fen_dict, batchPath):
    with open(batchPath + "/trainData", 'a') as file:
        for fen in fen_dict:
            file.write(str(fen) + ":" + str(fen_dict[fen]) + "\n")

def save_checkpoint(batchPath, idxPgn, idxGame):
    with open(batchPath + "/checkpoint.txt", "w") as file:
        file.write(str(idxPgn) + ":" + str(idxGame))

def read_checkpoint(batchPath):
    with open(batchPath + "/checkpoint.txt", "r") as file:
        idxGame, idxCheckpont = file.read().strip().split(":")
    return int(idxGame), int(idxCheckpont)

def create_labels(batchPath):
    idxGame, idxPgn = read_checkpoint(batchPath)

    engine = chess.engine.SimpleEngine.popen_uci("../../stockfish/stockfish.exe")
    for idx, pgn_file in enumerate(os.listdir(batchPath)):
        if ".txt" in pgn_file or idx < idxGame:
            continue
        pgn_file = batchPath + "/" + pgn_file
        pgn = open(pgn_file)
        fen_dict = dict()
        i = 1
        while True:
            game = chess.pgn.read_game(pgn)
            if game is None:
                if i % 10 != 0:
                    print("Saving...")
                    save_train_data(fen_dict, batchPath)
                    save_checkpoint(batchPath, idx, i)
                    fen_dict.clear()
                break
            if idx == idxGame and i <= idxPgn:
                i += 1
                continue
            board = game.board()
            for move in game.mainline_moves():
                board.push(move)
                info = engine.analyse(board, chess.engine.Limit(depth=15))
                fen_dict[board.fen()] = info['score'].white()
            print("Game: " + str(i) + " from " + pgn_file.split("/")[-1] + " ended!")
            if i % 10 == 0:
                print("Saving...")
                save_train_data(fen_dict, batchPath)
                save_checkpoint(batchPath, idx, i)
                fen_dict.clear()
            i += 1
        pgn.close()
    engine.quit()

def remove_duplicates(input_file, output_file):
    fens = set()
    evaluations = []
    i = 0
    with open(input_file, 'r') as file:
        for line in file:
            line_elems = line.split(":")
            fen, evaluation = line_elems[0], line_elems[1]
            if "#" in evaluation:
                continue
            if fen not in fens:
                fens.add(fen)
                evaluations.append(evaluation)
            i += 1
            if i % 10000 == 0:
                print("Gata inca 10000 suntem la : " + str(i))
    with open(output_file, 'w') as file:
        for fen, eval in zip(fens, evaluations):
            file.write(fen + ":" + str(eval))

def main():
    remove_duplicates("../trainData","../theData")

if __name__ == '__main__':
    main()


from django.contrib import admin

from simple_history.admin import SimpleHistoryAdmin

from .models import Foo, Bar

class FooModelAdmin(SimpleHistoryAdmin):
    list_display = ["name", "email", "phone"]

class BarModelAdmin(SimpleHistoryAdmin):
    list_display = ["foo", "kind", "get_value", "status"]

    actions = ["mark_changes"]

    def get_value(self, obj):
        return getattr(obj, obj.kind)

    def mark_changes(self, request, queryset):
        for obj in queryset:
            setattr(obj.foo, obj.kind, getattr(obj, obj.kind))
            obj.foo.save(update_fields=[obj.kind])

            obj.status = obj.ACCEPTED
            obj.save()

        self.message_user(request, "Ok")

admin.site.register(Foo, FooModelAdmin)
admin.site.register(Bar, BarModelAdmin)


#Purpose: Decode a steganographically coded image
#Author: Dewar P.

from PIL import Image

def hidden_num(pixel):

    r,g,b = pixel

    lsdr = r%10 *100
    lsdg = g%10 *10
    lsdb = b%10 *1

    return(lsdr+lsdg+lsdb)

def hidden_char(X):
    return(chr(X))

def decode(original_pic, N):
    piclist = original_pic.getdata()
    i = 0
    letter = ''
    message = ''

    while i < N:
        num = hidden_num(piclist[i])
        letter = hidden_char(num)
        message = message + letter

        i = i+1
    return message
##Your functions should be written above this line
##Don't modify any of the code below

def testfunctions():
    sample = [(241, 90, 147), (111 , 51, 31) ]
    if hidden_num(sample[0]) != 107:
        print("Your 'hidden_num' function has an error.")

    if hidden_char(107) != 'k':
        print("Your 'hidden_char' function has an error.")

    class dummy:
        def getdata(self):
            return [(241, 90, 147), (111 , 51, 31) ]

    if decode(dummy(), 2) != 'ko':
        print("Your 'decode' function has an error.")

    return

def main():
    print("\n\nTesting your functions.\n\n")
    testfunctions()
    print("\n\nReady to decode.\n\n")
    imgfile = "encoded.png"
    N = 654   # Remeber to set N: given from the decode function
    thepic = Image.open(imgfile)
    print(decode(thepic, N))
    return

###
main()


from django.urls import path
from . import views

app_name = 'math_chart'
urlpatterns = [
    path('', views.home, name='home'),
    path('my_chart_10/', views.my_chart_10, name='my_chart_10'),
    path('my_chart_20/', views.my_chart_20, name='my_chart_20'),
    path('b6_b6', views.b6_b6, name='b6_b6'),
    path('my_square', views.my_square, name='my_square'),
    path('my_sin', views.my_sin, name='my_sin'),
    path('my_cos', views.my_cos, name='my_cos'),
    path('my_tan', views.my_tan, name='my_tan'),
    path('trigonometry', views.trigonometry, name='trigonometry'),
]


from selenium.common.exceptions import NoSuchElementException, ElementClickInterceptedException
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
import time
import pandas as pd
from tqdm.auto import tqdm
from time import sleep
tqdm._instances.clear()

def get_jobs(keyword, num_jobs, path, slp_time, verbose):

    pbar = tqdm(total=num_jobs)
    options = webdriver.ChromeOptions()
    driver = webdriver.Chrome(executable_path=path, options=options)
    driver.set_window_size(1320, 1000)

    url = 'https://www.glassdoor.co.uk/Job/london-england-jobs-SRCH_IL.0,14_IC2671300.htm?suggestCount=0&suggestChosen=false&clickSource=searchBtn&typedKeyword=&typedLocation=London%252C%2520England&context=Jobs&dropdown=0'
    driver.get(url)

    # Close the pop ups
    time.sleep(slp_time)
    driver.find_element(By.CLASS_NAME, 'eigr9kq4').click()

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME,'modal_closeIcon-svg'))).click()

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'onetrust-accept-btn-handler'))).click()

    # Search for keyword
    key_search = driver.find_element(By.ID,'sc.keyword')
    key_search.send_keys(keyword)
    key_search.send_keys(Keys.RETURN)

    jobs = []
    while len(jobs)<num_jobs:
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'e1rrn5ka4')))
        time.sleep(slp_time)
        job_buttons = driver.find_elements(By.CLASS_NAME, 'e1rrn5ka4')
        for i in job_buttons:
            if len(jobs)>num_jobs:
                break
            time.sleep(slp_time)
            try:
                driver.find_element(By.CSS_SELECTOR, '#JDCol > div > div.css-17bh0pp.erj00if0 > button')
            except:
                pass
            try:
                driver.execute_script("arguments[0].click();", i)
            except:
                break
            time.sleep(slp_time)
            ## added this as getting error where the page doesnt load and throws an error page to manually retry search
            # This checks for the error first
            try:
                driver.find_element(By.CSS_SELECTOR, '#JDCol > div > div.css-17bh0pp.erj00if0 > button')
            except:
                pass
                #WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'e1tk4kwz1')))
            try:
                company = driver.find_element(By.CLASS_NAME, 'e1tk4kwz1').text
            except NoSuchElementException:
                company = -1
            try:
                location = driver.find_element(By.CLASS_NAME,'e1tk4kwz5').text
            except NoSuchElementException:
                location = -1
            try:
                job_title = driver.find_element(By.CLASS_NAME,'e1tk4kwz4').text
            except NoSuchElementException:
                job_title = -1
            try:
                salary = driver.find_element(By.XPATH,'//*[@id="MainCol"]/div[1]/ul/li[1]/div[2]/div[3]/div[1]/span').text
            except NoSuchElementException:
                salary = -1
            try:
                salary_estimate = driver.find_element(By.CLASS_NAME,'e2u4hf13').text
            except NoSuchElementException:
                salary_estimate = -1

            try:
                title = driver.find_elements(By.CLASS_NAME,'e1pvx6aw1')
                var = driver.find_elements(By.CLASS_NAME,'e1pvx6aw2')
                comp_info = {}
                for i,x in zip(title,var):
                    comp_info[i.text] = x.text
                try:
                    num_employees = comp_info['Size']
                except:
                    num_employees = -1
                try:
                    founded = comp_info['Founded']
                except:
                    founded = -1
                try:
                    company_type = comp_info['Type']
                except:
                    company_type = -1
                try:
                    company_industry = comp_info['Industry']
                except:
                    company_industry = -1
                try:
                    company_sector = comp_info['Sector']
                except:
                    company_sector = -1
                try:
                    company_revenue = comp_info['Revenue']
                except:
                    company_revenue = -1
            except:
                num_employees = -1
                founded = -1
                company_type = -1
                company_industry = -1
                company_sector = -1
                company_revenue = -1

            try:
                review = driver.find_elements(By.CLASS_NAME,'erz4gkm1')
                ratings = {}
                for i,e in list(zip(review[::2], review[1::2])):
                    ratings[i.text] = e.text
                try:
                    career_opp_rat = ratings['Career Opportunities']
                except:
                    career_opp_rat = -1
                try:
                    comp_rate = ratings['Comp & Benefits']
                except:
                    comp_rate = -1
                try:
                    culture_rate = ratings['Culture & Values']
                except:
                    culture_rate = -1
                try:
                    mngmt_rate = ratings['Senior Management']
                except:
                    mngmt_rate = -1
                try:
                    worklife_rate = ratings['Work/Life Balance']
                except:
                    worklife_rate = -1
            except:
                career_opp_rat = -1
                comp_rate = -1
                culture_rate = -1
                mngmt_rate = -1
                worklife_rate = -1
            try:
                WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#JobDescriptionContainer > div.css-t3xrds.e856ufb4'))).click()
                time.sleep(slp_time)
                description = driver.find_element(By.ID,'JobDescriptionContainer').text
            except:
                description = -1

            jobs.append({
            'Company' : company,
            'Job title' : job_title,
            'Location': location,
            'Salary': salary,
            'Salary estimate': salary_estimate,
            'Employees': num_employees,
            'Founded': founded,
            'Company type': company_type,
            'Company industry': company_industry,
            'Company sector': company_sector,
            'Company revenue': company_revenue,
            'Career opportunities': career_opp_rat,
            'Comp & Benefits': comp_rate,
            'Culture & Values': culture_rate,
            'Senior Management': mngmt_rate,
            'Work/Life Balance': worklife_rate,
            'Description': description
            })

            if verbose:
                print("company: {}".format(company))
                print("job_title: {}".format(job_title))
                print("location: {}".format(location))
                print("salary: {}".format(salary))
                print("salary_estimate: {}".format(salary_estimate))
                print("employees: {}".format(num_employees))
                print("founded: {}".format(founded))
                print("company_type: {}".format(company_type))
                print("company_industry: {}".format(company_industry))
                print("company_sector: {}".format(company_sector))
                print("company_revenue: {}".format(company_revenue))
                print("Career opportunities: {}".format(career_opp_rat))
                print("Comp & Benefits: {}".format(comp_rate))
                print("Culture & Values: {}".format(culture_rate))
                print("Senior Management: {}".format(mngmt_rate))
                print("Work/Life Balance: {}".format(worklife_rate))
            else:
                if len(jobs)%30==0:
                    pd.DataFrame(jobs).to_csv('TEMP_glassdoor_jobs.csv', index = False)
            pbar.update(1)
        try:
            WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#MainCol > div.tbl.fill.px.my.d-flex > div > div.pageContainer > button.nextButton.css-1hq9k8.e13qs2071 > span > svg'))).click()
        except:
            print("Scraping terminated before reaching target number of jobs. Needed {}, got {}.".format(num_jobs, len(jobs)))
            break
    return pd.DataFrame(jobs)


from tkinter import *
import pygame as py
import os

window = Tk()
window.title("Ton Player")
window.geometry("650x260")
window.config(bg="black")

py.init()
playstate = 0
mainb = PhotoImage(file="Icons/play.png")
pauseimage = PhotoImage(file="Icons/stop.png")
playimage = PhotoImage(file="Icons/play.png")

def playstop():
    global playstate
    if playstate == 0:
        song = songList.get(ACTIVE)
        py.mixer.music.load(f"Songs/{song}")
        py.mixer.music.play()
        playstate = 1
        mainButton.configure(image=pauseimage)
    elif playstate == 1:
        py.mixer.music.pause()
        playstate = 2
        mainButton.configure(image=playimage)
    else:
        py.mixer.music.unpause()
        playstate = 1
        mainButton.configure(image=pauseimage)

def nextSong():
    current = songList.curselection()
    next = songList.get(current[0] + 1)
    py.mixer.music.load(f"Songs/{next}")
    py.mixer.music.play(loops=0)
    songList.selection_clear(0, END)
    songList.activate(current[0] + 1)
    songList.selection_set(current[0] + 1)

def previousSong():
    current = songList.curselection()
    previous = songList.get(current[0]-1)
    py.mixer.music.load(f"Songs/{previous}")
    py.mixer.music.play(loops=0)
    songList.selection_clear(0, END)
    songList.activate(current[0] - 1)
    songList.selection_set(current[0] - 1)

# App Name
logo = PhotoImage(file="Icons/logo.png")
isim = Label(window, image=logo, text="Ton Player", bg="black", fg="#1db954", font="Helvetica 17", compound="left")
isim.pack()

# Song List
songList = Listbox(window, bg="black", fg="white", width=100, selectbackground="gray", highlightcolor="black", bd=1)
songList.pack()
for i in os.listdir("Songs"):
    if i.endswith(".mp3"):
        songList.insert(END, i)

tustakim = Frame(window)
tustakim.pack()

# Previous Button
previousb = PhotoImage(file="Icons/previous.png")
previousButton = Button(tustakim, bg="black", image=previousb, width=62, height=59, bd=0, command=previousSong)
previousButton.grid(row=0, column=0)

# Play Button
mainButton = Button(tustakim, bg="black", image=mainb, width=62, height=59, bd=0, command=playstop)
mainButton.grid(row=0, column=1)

# Next Button
skipb = PhotoImage(file="Icons/skip.png")
skipButton = Button(tustakim, bg="black", image=skipb, width=62, height=59, bd=0, command=nextSong)
skipButton.grid(row=0, column=2)

window.iconbitmap("Icons/logo.ico")
window.mainloop()


import torch
import torch.nn.functional as F

class Act(torch.nn.Module):
    def __init__(self, act: str, slope: float = 0.05) -> None:
        super(Act, self).__init__()
        self.act = act
        self.slope = slope
        self.shift = torch.log(torch.tensor(2.0)).item()

    def forward(self, input: torch.Tensor) -> torch.Tensor:
        if self.act == "relu":
            return F.relu(input)
        elif self.act == "leaky_relu":
            return F.leaky_relu(input)
        elif self.act == "sp":
            return F.softplus(input, beta=1)
        elif self.act == "leaky_sp":
            return F.softplus(input, beta=1) - self.slope * F.relu(-input)
        elif self.act == "elu":
            return F.elu(input, alpha=1)
        elif self.act == "leaky_elu":
            return F.elu(input, alpha=1) - self.slope * F.relu(-input)
        elif self.act == "ssp":
            return F.softplus(input, beta=1) - self.shift
        elif self.act == "leaky_ssp":
            return (
                F.softplus(input, beta=1)
                - self.slope * F.relu(-input)
                - self.shift
            )
        elif self.act == "tanh":
            return torch.tanh(input)
        elif self.act == "leaky_tanh":
            return torch.tanh(input) + self.slope * input
        elif self.act == "swish":
            return torch.sigmoid(input) * input
        else:
            raise RuntimeError(f"Undefined activation called {self.act}")


import os
import unittest

import numpy as np

import pydrake
from pydrake.attic.multibody import shapes
from pydrake.common.eigen_geometry import Isometry3

class TestShapes(unittest.TestCase):
    def test_api(self):
        box_size = [1., 2., 3.]
        radius = 0.1
        length = 0.2

        box = shapes.Box(size=box_size)
        self.assertTrue(np.allclose(box.size, box_size))
        self.assertEqual(box.getPoints().shape, (3, 8))
        self.assertEqual(len(box.getFaces()), 12)
        self.assertEqual(len(box.getFaces()[0]), 3)

        sphere = shapes.Sphere(radius=radius)
        self.assertEqual(sphere.radius, radius)
        self.assertEqual(sphere.getPoints().shape, (3, 1))
        with self.assertRaises(RuntimeError):
            sphere.getFaces()

        cylinder = shapes.Cylinder(radius=radius, length=length)
        self.assertEqual(cylinder.radius, radius)
        self.assertEqual(cylinder.length, length)

        capsule = shapes.Capsule(radius=radius, length=length)
        self.assertEqual(capsule.radius, radius)
        self.assertEqual(capsule.length, length)

        pts = np.tile(box_size, (10, 1)).T
        mesh_points = shapes.MeshPoints(pts)
        self.assertEqual(mesh_points.getPoints().shape, (3, 10))

        obj_mesh_path = os.path.join(
            pydrake.getDrakePath(), "examples/quadrotor/quadrotor_base.obj")
        obj_mesh_uri = "box_obj"
        mesh = shapes.Mesh(uri=obj_mesh_uri, resolved_filename=obj_mesh_path)
        self.assertTrue(np.allclose(mesh.scale, [1., 1., 1.]))
        self.assertEqual(mesh.uri, obj_mesh_uri)
        self.assertEqual(mesh.resolved_filename, obj_mesh_path)

    def test_visual_element_api(self):
        material_in = [0.3, 0.4, 0.5, 0.6]
        material_in_2 = [0.6, 0.7, 0.8, 0.9]
        box = shapes.Box(size=[1., 1., 1.])
        visual_element_np = shapes.VisualElement(box, np.eye(4), material_in)
        visual_element_isom = shapes.VisualElement(
            box, Isometry3.Identity(), material_in)
        self.assertTrue(np.allclose(visual_element_np.getMaterial(),
                                    material_in))
        visual_element_np.setMaterial(material_in_2)
        self.assertTrue(np.allclose(visual_element_np.getMaterial(),
                                    material_in_2))


# This example requires the 'members' privileged intent to use the Member converter.
# This example also requires the 'message_content' privileged intent to function.

import traceback
import typing

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.members = True
intents.message_content = True

bot = commands.Bot('!', intents=intents)

@bot.command()
async def userinfo(ctx: commands.Context, user: discord.User):
    # In the command signature above, you can see that the `user`
    # parameter is typehinted to `discord.User`. This means that
    # during command invocation we will attempt to convert
    # the value passed as `user` to a `discord.User` instance.
    # The documentation notes what can be converted, in the case of `discord.User`
    # you pass an ID, mention or username (discrim optional)
    # E.g. 80088516616269824, @Danny or Danny#0007

    # NOTE: typehinting acts as a converter within the `commands` framework only.
    # In standard Python, it is use for documentation and IDE assistance purposes.

    # If the conversion is successful, we will have a `discord.User` instance
    # and can do the following:
    user_id = user.id
    username = user.name
    avatar = user.display_avatar.url
    await ctx.send(f'User found: {user_id} -- {username}\n{avatar}')

@userinfo.error
async def userinfo_error(ctx: commands.Context, error: commands.CommandError):
    # If the conversion above fails for any reason, it will raise `commands.BadArgument`
    # so we handle this in this error handler:
    if isinstance(error, commands.BadArgument):
        return await ctx.send('Couldn\'t find that user.')
    # The default `on_command_error` will ignore errors from this command
    # because we made our own command-specific error handler,
    # so we need to log tracebacks ourselves.
    else:
        traceback.print_exception(type(error), error, error.__traceback__)

# Custom Converter here
class ChannelOrMemberConverter(commands.Converter):
    async def convert(self, ctx: commands.Context, argument: str):
        # In this example we have made a custom converter.
        # This checks if an input is convertible to a
        # `discord.Member` or `discord.TextChannel` instance from the
        # input the user has given us using the pre-existing converters
        # that the library provides.

        member_converter = commands.MemberConverter()
        try:
            # Try and convert to a Member instance.
            # If this fails, then an exception is raised.
            # Otherwise, we just return the converted member value.
            member = await member_converter.convert(ctx, argument)
        except commands.MemberNotFound:
            pass
        else:
            return member

        # Do the same for TextChannel...
        textchannel_converter = commands.TextChannelConverter()
        try:
            channel = await textchannel_converter.convert(ctx, argument)
        except commands.ChannelNotFound:
            pass
        else:
            return channel

        # If the value could not be converted we can raise an error
        # so our error handlers can deal with it in one place.
        # The error has to be CommandError derived, so BadArgument works fine here.
        raise commands.BadArgument(f'No Member or TextChannel could be converted from "{argument}"')

@bot.command()
async def notify(ctx: commands.Context, target: ChannelOrMemberConverter):
    # This command signature utilises the custom converter written above
    # What will happen during command invocation is that the `target` above will be passed to
    # the `argument` parameter of the `ChannelOrMemberConverter.convert` method and
    # the conversion will go through the process defined there.

    await target.send(f'Hello, {target.name}!')

@bot.command()
async def ignore(ctx: commands.Context, target: typing.Union[discord.Member, discord.TextChannel]):
    # This command signature utilises the `typing.Union` typehint.
    # The `commands` framework attempts a conversion of each type in this Union *in order*.
    # So, it will attempt to convert whatever is passed to `target` to a `discord.Member` instance.
    # If that fails, it will attempt to convert it to a `discord.TextChannel` instance.
    # See: https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html#typing-union
    # NOTE: If a Union typehint converter fails it will raise `commands.BadUnionArgument`
    # instead of `commands.BadArgument`.

    # To check the resulting type, `isinstance` is used
    if isinstance(target, discord.Member):
        await ctx.send(f'Member found: {target.mention}, adding them to the ignore list.')
    elif isinstance(target, discord.TextChannel):  # this could be an `else` but for completeness' sake.
        await ctx.send(f'Channel found: {target.mention}, adding it to the ignore list.')

# Built-in type converters.
@bot.command()
async def multiply(ctx: commands.Context, number: int, maybe: bool):
    # We want an `int` and a `bool` parameter here.
    # `bool` is a slightly special case, as shown here:
    # See: https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html#bool

    if maybe is True:
        return await ctx.send(number * 2)
    await ctx.send(number * 5)

bot.run('token')


from itertools import zip_longest, combinations
import json
import os
import warnings

import numpy as np

import tvm
from tvm import relay
from tvm import rpc

# from tvm.contrib.debugger import debug_runtime as graph_executor
from tvm.contrib import graph_executor
from tvm.relay.op.contrib import clml
from tvm.contrib import utils
from tvm import autotvm
from tvm.autotvm.measure import request_remote
from tvm.relay.expr_functor import ExprMutator, Call

"""Utils for adreno compute/schedules"""

import os
import tvm
import numpy as np
from tvm import relay
from tvm import autotvm
from tvm import rpc
from tvm.contrib import utils, ndk
from tvm.relay import testing
from tvm.relay.transform import recast
from tvm.contrib import graph_runtime
from tvm.runtime.vm import VirtualMachine
import json

NDK_CROSS_COMPILER = os.getenv("TVM_NDK_CC", "aarch64-linux-android-g++")

def get_cpu_op_count(mod):
    """Traverse graph counting ops offloaded to TVM."""

    class Counter(tvm.relay.ExprVisitor):
        def __init__(self):
            super().__init__()
            self.count = 0

        def visit_call(self, call):
            if isinstance(call.op, tvm.ir.Op):
                self.count += 1

            super().visit_call(call)

    c = Counter()
    c.visit(mod["main"])
    return c.count

def get_non_cpu_op_count(mod):
    """Traverse graph counting ops not offloaded to TVM."""

    class Counter(tvm.relay.ExprVisitor):
        def __init__(self):
            super().__init__()
            self.count = 0

        def visit_call(self, call):
            if not isinstance(call.op, tvm.ir.Op):
                self.count += 1

            super().visit_call(call)

    c = Counter()
    c.visit(mod["main"])
    return c.count

# build module run with opencl or clml target with graph executor
def build_and_run(
    remote,
    mod,
    params1,
    inputs,
    target="llvm",
    enable_clml=False,
    stat_file=None,
):
    if remote is None:
        target_host = "llvm"
    else:
        target_host = "llvm -mtriple=arm64-linux-android"

    if isinstance(mod, tvm.relay.expr.Call):
        mod = tvm.IRModule.from_expr(mod)

    with autotvm.apply_history_best(stat_file):
        with tvm.transform.PassContext(opt_level=3):
            if enable_clml:
                mod = clml.partition_for_clml(mod, params1)
            graph, lib, params = relay.build(
                mod, target_host=target_host, target=target, params=params1
            )

    if remote is None:
        ctx = tvm.opencl()
        m = graph_runtime.create(graph, lib, ctx)
    else:
        temp = utils.tempdir()
        dso_binary = "dev_lib_cl.so"
        dso_binary_path = temp.relpath(dso_binary)
        ctx = remote.cl(0)
        lib.export_library(dso_binary_path, fcompile=ndk.create_shared)
        remote.upload(dso_binary_path)
        rlib = remote.load_module(dso_binary)
        m = graph_runtime.create(graph, rlib, ctx)
    m.set_input(**params)
    m.set_input(**inputs)
    m.run()
    return m.get_output(0)

# build module run with opencl or clml target with vm executor
def build_and_run_vm(
    remote,
    mod,
    params1,
    inputs,
    target="llvm",
    enable_clml=False,
    stat_file=None,
):
    if remote is None:
        target_host = "llvm"
    else:
        target_host = "llvm -mtriple=arm64-linux-android"

    target_host = tvm.target.Target(target_host)
    target = tvm.target.Target(target, target_host)
    if isinstance(mod, relay.Function):
        module = tvm.IRModule({})
        module["main"] = mod
        mod = module
    elif isinstance(mod, tvm.relay.expr.Call):
        mod = tvm.IRModule.from_expr(mod)

    with autotvm.apply_history_best(stat_file):
        with tvm.transform.PassContext(opt_level=3):
            if enable_clml:
                mod = clml.partition_for_clml(mod, params1)
            vmc = relay.vm.compile(mod, target=target, params=params1)

    if remote is None:
        dev = tvm.opencl()
        vm = VirtualMachine(vmc, dev, "naive")
    else:
        temp = utils.tempdir()
        dso_binary = "dev_lib_cl.so"
        dso_binary_path = temp.relpath(dso_binary)
        dev = remote.cl(0)
        vmc.mod.export_library(dso_binary_path, cc=NDK_CROSS_COMPILER)
        remote.upload(dso_binary_path)
        rlib = remote.load_module(dso_binary)
        vm = VirtualMachine(rlib, dev, "naive")
    inputs_data = {}
    for key in inputs.keys():
        inputs_data[key] = tvm.nd.array(inputs[key], dev)
    for k, v in params1.items():
        inputs_data[k] = tvm.nd.array(v, dev)
    vm.set_input("main", **inputs_data)
    vm.invoke_stateful("main")
    out = vm.get_outputs()[0]

    return out

def extract_clml_modules(module):
    """Get the CLML module(s) from llvm module."""
    return list(filter(lambda mod: mod.type_key == "clml", module.get_lib().imported_modules))

def verify_codegen(
    remote,
    mod,
    params,
    known_good_codegen,
    target="llvm",
    num_clml_modules=1,
    tvm_ops=0,
):
    if remote is None:
        target_host = "llvm"
    else:
        target_host = "llvm -mtriple=arm64-linux-android"

    """Check clml codegen against a known good output."""
    if isinstance(mod, tvm.relay.expr.Call):
        mod = tvm.IRModule.from_expr(mod)
    with tvm.transform.PassContext(opt_level=3):
        mod = clml.partition_for_clml(mod, params)
        tvm_op_count = get_cpu_op_count(mod)
        assert tvm_op_count == tvm_ops, "Got {} TVM operators, expected {}".format(
            tvm_op_count, tvm_ops
        )
        partition_count = 0
        for global_var in mod.get_global_vars():
            if "clml" in global_var.name_hint:
                partition_count += 1

        assert (
            num_clml_modules == partition_count
        ), "Got {} Open CLML partitions, expected {}".format(partition_count, num_clml_modules)
    relay.backend.te_compiler.get().clear()

    module = relay.build(mod, target=target, target_host=target_host, params=params)
    clml_modules = extract_clml_modules(module)
    assert len(clml_modules) == num_clml_modules, (
        f"The number of CLML modules produced ({len(clml_modules)}) does not "
        f"match the expected value ({num_clml_modules})."
    )

    for mod in clml_modules:
        source = mod.get_source("json")
        codegen = json.loads(source)["nodes"]
        # remove input and const names as these cannot be predetermined
        for node in range(len(codegen)):
            if codegen[node]["op"] == "input" or codegen[node]["op"] == "const":
                codegen[node]["name"] = ""
        codegen_str = json.dumps(codegen, sort_keys=True, indent=2)
        known_good_codegen_str = json.dumps(known_good_codegen, sort_keys=True, indent=2)

        assert codegen_str == known_good_codegen_str, (
            f"The JSON produced by codegen does not match the expected result. \n"
            f"Actual={codegen_str} \n"
            f"Expected={known_good_codegen_str}"
        )