""" This script receives notifications from Terraform Cloud workspaces
    and automatically saves the latest state file from that workspace
    to a corresponding S3 bucket. """

import base64
import hashlib
import hmac
import json
import os
import boto3
from botocore.exceptions import ClientError
import requests

REGION = os.getenv("REGION", None)
S3_BUCKET = os.getenv("S3_BUCKET", None)
SALT_PATH = os.getenv("SALT_PATH", None)
TFC_TOKEN_PATH = os.getenv("TFC_TOKEN_PATH", None)
VAULT_SECRET_FILE = os.getenv("VAULT_SECRET_FILE", None)

SAVE_STATES = {'applied'}

# Initialize boto3 client at global scope for connection reuse
session = boto3.Session(region_name=REGION)
ssm = session.client('ssm')
s3 = boto3.resource('s3')

def lambda_handler(event, context):
    """ Handle the incoming requests """
    print(event)
    # first we need to authenticate the message by verifying the hash
    message = bytes(event['body'], 'utf-8')
    salt = bytes(ssm.get_parameter(Name=SALT_PATH, WithDecryption=True)[
        'Parameter']['Value'], 'utf-8')
    hash = hmac.new(salt, message, hashlib.sha512)
    if hash.hexdigest() == event['headers']['X-Tfe-Notification-Signature']:
        # HMAC verified
        if event['httpMethod'] == "POST":
            return post(event)
        return get()
    return 'Invalid HMAC'

def get():
    """ Handle a GET request """
    return {
        "statusCode": 200,
        "headers": {
            "Content-Type": "application/json",
            "Access-Control-Allow-Origin": "*"
        },
        "body": "I'm here!"
    }

def post(event):
    """ Handle a POST request """
    payload = json.loads(event['body'])
    post_response = "I'm here!"

    if payload and 'run_status' in payload['notifications'][0]:
        body = payload['notifications'][0]
        if body['run_status'] in SAVE_STATES:
            print("Run status indicates save the state file.")
            if payload['workspace_name']:
                if VAULT_SECRET_FILE:
                    vault_secret_file = open(VAULT_SECRET_FILE)
                    vault_secret = json.load(vault_secret_file)
                    tfc_api_token = vault_secret['data']['token']
                else:
                    tfc_api_token = bytes(ssm.get_parameter(
                        Name=TFC_TOKEN_PATH, WithDecryption=True)['Parameter']['Value'], 'utf-8')
                    tfc_api_token = tfc_api_token.decode("utf-8")

                state_api_url = 'https://app.terraform.io/api/v2/workspaces/' + \
                    payload['workspace_id'] + '/current-state-version'

                tfc_headers = {'Authorization': 'Bearer ' + tfc_api_token,
                               'Content-Type': 'application/vnd.api+json'}

                state_api_response = requests.get(
                    state_api_url, headers=tfc_headers)

                state_response_payload = json.loads(state_api_response.text)

                archivist_url = state_response_payload['data']['attributes'][
                    'hosted-state-download-url']

                archivist_response = requests.get(archivist_url)

                encoded_state = archivist_response.text.encode('utf-8')

                state_md5 = base64.b64encode(hashlib.md5(
                    encoded_state).digest()).decode('utf-8')

                try:
                    s3_response = s3.Bucket(S3_BUCKET).put_object(
                        Key=payload['workspace_name'], Body=encoded_state, ContentMD5=state_md5)
                    print("State file saved: ", s3_response)
                except ClientError as error:
                    print(error)

    return {
        "statusCode": 200,
        "body": json.dumps(post_response)
    }


import tkinter as tk
import CalculateDimension as calc
from PIL import ImageTk, Image

dest = ""
compo = ""

compo_list = []

def checkfloat(string):
    try:
        float(string)
        return True
    except ValueError:
        return False

def inject_dest():
    if checkfloat(textbox_dest.get()):
        global dest
        dest = textbox_dest.get()
    else:
        print("Errore: inserire un numero")
        textbox_dest.delete(0, tk.END)
        textbox_dest.insert(0, "Errore: inserire un numero")

def checkcompofloat(string, delimiter = "-"):
    parts = string.split(delimiter)
    for part in parts:
        if not part.isdigit():
            return False
    return True

def inject_compo():
    if checkcompofloat(textbox_compo.get()):
        global compo
        compo = textbox_compo.get()
    else:
        print("Errore: inserire una composizione valida")
        textbox_compo.delete(0, tk.END)
        textbox_compo.insert(0, "Errore: inserire una composizione valida")

def load_image():
    # Load the image file
    image = Image.open("./images/test.png")
    image = image.resize((720, 600))  # Resize the image to fit in the window
    photo = ImageTk.PhotoImage(image)

    # Set the image on the label widget
    label.configure(image=photo)
    label.image = photo  # Keep a reference to the photo to avoid garbage collection

def new_windows():
    inject_dest()
    new_window = tk.Toplevel(window)
    new_window.title("Composizione")
    new_window.geometry("300x300")

    max_found = 0;
    best_compo = "4-5-6-7-6-5-4"

    if float(dest) >= 30 and float(dest)  < 40:
        compo_list = ["5-6-7-8-7-6-5", "6-7-8-9-8-7-6", "7-8-9-10-9-8-7"]
    elif float(dest)  >= 40 and float(dest)  < 50:
        compo_list = ["6-7-8-9-8-7-6", "5-6-7-8-7-6-5", "4-5-6-5-4"]
    elif float(dest)  >= 50 and float(dest)  < 70:
        compo_list = ["4-5-6-5-4", "4-5-6-7-6-5-4", "3-4-5-4-3"]
    elif float(dest)  >= 60:
        compo_list = ["3-4-5-4-3", "3-4-3"]

    for i in range(0, len(compo_list)):
        rows_array = [float(i) for i in compo_list[i].split('-')]
        if(len(rows_array) % 2 == 1):
            n_rows = len(rows_array)
            radius = float(dest)
            radius = radius / 20
            #Call the function to calculate the dimension of the hexagon
            total = calc.Calculate(radius, n_rows, rows_array, 9999)
            total_tubes_1 = sum([int(i) for i in compo_list[i].split('-')]) * total
            if total_tubes_1 > max_found:
                max_found = total_tubes_1
                best_compo = compo_list[i]
    label_best_compo = tk.Label(new_window, text="Miglior composizione: " + best_compo).place(x=10, y=30)

def startcomputation():
    inject_dest()
    inject_compo()
    if dest == "" or compo == "":
        print("Errore: inserire tutti i dati")
    else:
        rows_array = [float(i) for i in compo.split('-')]
        if(len(rows_array) % 2 == 1):
            n_rows = len(rows_array)
            radius = float(dest)
            radius = radius / 20

            #Call the function to calculate the dimension of the hexagon
            total = calc.Calculate(radius, n_rows, rows_array, 9999)
            label_total.config(text="Total tubes: " + str(sum([int(i) for i in compo.split('-')]) * total))
            load_image()

window = tk.Tk()
window.title("My GUI")

window.geometry("1000x700")
# Create an empty label to hold the image
label = tk.Label(window)
label.place(x=300, y=0)

label_dest = tk.Label(window, text="Diametro esterno:")
textbox_dest = tk.Entry(window)

label_compo = tk.Label(window, text="Composizione:")
textbox_compo = tk.Entry(window)

button_start = tk.Button(window, text="Start", command=startcomputation)
button_test = tk.Button(window, text="Test", command=new_windows)

window.resizable(False, False)

label_total = tk.Label(window, text="Total tubes: ")
label_total.place(x=10, y=210)

label_dest.place(x=10, y=30)
textbox_dest.place(x=10, y=50)

label_compo.place(x=10, y=100)
textbox_compo.place(x=10, y=120)

button_start.place(x=10, y=150)
button_test.place(x=10, y=180)

window.mainloop()


import logging

import tkinter as tk
from tkinter import ttk
from controllers.main_controller import MainController

from gui import custom_components

from utils.constants import const
from pkmn.gen_factory import current_gen_info

logger = logging.getLogger(__name__)

class RouteSearch(ttk.Frame):
    def __init__(self, controller:MainController, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._controller:MainController = controller

        self.padx = 5
        self.pady = 1

        self._filter_vals = []
        self._filter_components = []

        num_filters_per_row = 5
        for cur_idx, event_type in enumerate(const.ROUTE_EVENT_TYPES):
            cur_row = cur_idx // num_filters_per_row
            cur_col = (cur_idx % num_filters_per_row) * 2

            cur_checkbox = custom_components.CheckboxLabel(self, text=event_type, toggle_command=self.curry_filter_callback(event_type))
            cur_checkbox.grid(row=cur_row, column=cur_col, padx=self.padx, pady=self.pady, sticky=tk.EW)
            self._filter_components.append(cur_checkbox)

        row_idx = len(const.ROUTE_EVENT_TYPES) + 1
        self.reset_button = custom_components.SimpleButton(self, text="Reset All Filters", command=self.reset_all_filters)
        self.reset_button.grid(row=row_idx, column=0, padx=self.padx, pady=self.pady, sticky=tk.W)

        self.search_label = tk.Label(self, text="Search:")
        self.search_label.grid(row=row_idx, column=2, padx=self.padx, pady=self.pady, sticky=tk.W)

        self.search_val = custom_components.SimpleEntry(self, callback=self.search_callback)
        self.search_val.grid(row=row_idx, column=4, padx=self.padx, pady=self.pady, sticky=tk.EW, columnspan=3)

    def reset_all_filters(self, *args, **kwargs):
        for cur_checkbox in self._filter_components:
            cur_checkbox:custom_components.CheckboxLabel
            if cur_checkbox.is_checked():
                cur_checkbox.toggle_checked()

        self.search_val.set("")

    def search_callback(self, *args, **kwargs):
        self._controller.set_route_search(self.search_val.get())

    def curry_filter_callback(self, string_val):
        def inner(*args, **kwargs):
            if string_val in self._filter_vals:
                self._filter_vals.remove(string_val)
            else:
                self._filter_vals.append(string_val)

            self._controller.set_route_filter_types(self._filter_vals)

        return inner


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

class AVAM_block(nn.Module):
    """Audio-Visual Attention Mechanism.

    Reference: https://arxiv.org/pdf/2203.05178.pdf

    Attributes
    ----------
    in_channels: int, optional (default=3)
        Input number of channels.
    out_channels: int, optional (default=1)
        Output number of channels.
        """
    def __init__(self):
        """Initialize module."""
        super(AVAM_block,self).__init__()
        self.convmax = nn.Conv2d(in_channels=2,out_channels=1,kernel_size=7,padding=3)
        self.convavg = nn.Conv2d(in_channels=2,out_channels=1,kernel_size=7,padding=3)
        self.finalconv = nn.Conv2d(in_channels=2,out_channels=1,kernel_size=7,padding=3)

    def forward(self,image, audio):
        """Process input batch.

        image: Tensor
            Input image tensor.
        audio: Tensor
            Input audio tensor.

        Returns
        -------
        Tensor
            Attention tensor of image and audio.
        """
        F_MAX = torch.cat([
            torch.max(image,axis=1)[0].unsqueeze(dim=1),
            torch.max(audio,axis=1)[0].unsqueeze(dim=1)],dim=1)
        F_AVG = torch.cat([
            torch.mean(image,axis=1).unsqueeze(dim=1),
            torch.mean(audio,axis=1).unsqueeze(dim=1)],dim=1)
        F_1 = torch.cat([
            F.sigmoid(self.convavg(F_AVG)),
            F.sigmoid(self.convmax(F_MAX))],axis=1)
        M = F.sigmoid(self.finalconv(F_1))
        F_att = image*M
        return F_att

class ResBlock(nn.Module):
    """ResidualBlock.

    Reference: https://arxiv.org/pdf/1512.03385.pdf

    Parameters
    ----------
    in_channels: int, optional (default=2)
        Input number of channels.
    out_channels: int, optional (default=1)
        Output number of channels.
    downsample: bool, optional (default=False)
        If to use downsample layer.
    """
    def __init__(self, in_channels, out_channels, downsample):
        """Initialize module."""
        super().__init__()
        if downsample:
            self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=2, padding=1)
            self.shortcut = nn.Sequential(
                nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=2),
                nn.BatchNorm2d(out_channels)
            )
        else:
            self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1)
            self.shortcut = nn.Sequential()

        self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1)
        self.bn1 = nn.BatchNorm2d(out_channels)
        self.bn2 = nn.BatchNorm2d(out_channels)

    def forward(self, x):
        """Process input batch.

        Parameters
        ----------
        x: Tensor
            Input batch.

        Returns
        -------
        Tensor
            Processed input batch.
        """
        shortcut = self.shortcut(x)
        x = nn.ReLU()(self.bn1(self.conv1(x)))
        x = nn.ReLU()(self.bn2(self.conv2(x)))
        x = x + shortcut
        return nn.ReLU()(x)

class MultimodalModel(nn.Module):
    """MultimodalModel.

    Reference: https://arxiv.org/pdf/2203.05178.pdf

    Parameters
    ----------
    in_channels (int) - количество входных каналов
    audio_size (int) - размер изображения спектрограммы
    classify (bool) - использование полносвязного слоя для классификации
    return_patch (bool) - использование патча 2х2 на выходе (PatchGan Discriminator)

    Forward Pass:
    Input: x (tuple: (img, audio_up, audio_d))
    Output: out (int): класс (дипфейк/нет) if classify == True else out (torch.Tensor [B,2,2]) if return_patch == True else torch.Tensor [B,256,3,3]

    """
    def __init__(self,in_channels,audio_size_default = 96,classify=True,return_patch = False):
        """Initialize module."""
        super(MultimodalModel,self).__init__()
        self.audio_size_default = audio_size_default
        self.classify = classify
        self.return_patch = return_patch
        self.avam1 = AVAM_block()
        self.avam2 = AVAM_block()
        self.avam3 = AVAM_block()
        self.avam4 = AVAM_block()
        self.avam5 = AVAM_block()

        self.resblock1 = ResBlock(in_channels,16,downsample=True)
        self.resblock2 = ResBlock(16,32,downsample=True)
        self.resblock3 = ResBlock(64,64,downsample=True)
        self.resblock4 = ResBlock(128,128,downsample=True)
        self.resblock5 = ResBlock(256,256,downsample=True)

        if self.return_patch:
            self.patch_output = nn.Sequential(*[nn.Conv2d(256,1,kernel_size=2),
                                                nn.Sigmoid()])
        self.upsampled_audio1 = ResBlock(3,16,downsample=True)
        self.upsampled_audio2 = ResBlock(16,32,downsample=True)
        self.upsampled_audio3 = ResBlock(32,64,downsample=True)
        self.upsampled_audio4 = ResBlock(64,128,downsample=True)

        if self.audio_size_default==384:
            self.default_audio = ResBlock(3,16,downsample=True)
            self.default_audio0 = ResBlock(16,16,downsample=True)
            self.default_audio1 = ResBlock(16,32,downsample=True)

        elif self.audio_size_default==768:
            self.default_audio_inp = ResBlock(3,16,downsample=True)
            self.default_audio = ResBlock(16,16,downsample=True)
            self.default_audio0 = ResBlock(16,16,downsample=True)
            self.default_audio1 = ResBlock(16,32,downsample=True)

        self.default_audio2 = ResBlock(32,32,downsample=True)
        self.default_audio3 = ResBlock(32,64,downsample=True)
        self.default_audio4 = ResBlock(64,128,downsample=True)

        self.classifier = nn.Sequential(*[nn.Flatten(),nn.Linear(256*9,1000,bias=False),
                                          nn.ReLU(),nn.Dropout(p=0.5),
                                          nn.Linear(1000,100,bias=False),
                                          nn.ReLU(),nn.Dropout(p=0.5),
                                          nn.Linear(100,1,bias=False),
                                          nn.Sigmoid()])

    def forward(self,x):
        print(x[0].shape)
        img,audio_up,audio_d = x
        if self.audio_size_default==384:
            audio_d = self.default_audio(audio_d)
            audio_d = self.default_audio0(audio_d)
        elif self.audio_size_default==768:
            audio_d = self.default_audio_inp(audio_d)
            audio_d = self.default_audio(audio_d)
            audio_d = self.default_audio0(audio_d)

        avam = self.resblock1(self.avam1(img,audio_up))
        audio_up = self.upsampled_audio1(audio_up)
        audio_d = self.default_audio1(audio_d)
        print(avam.shape)

        avam = self.resblock2(self.avam2(avam,audio_up))
        audio_up = self.upsampled_audio2(audio_up)
        audio_d = self.default_audio2(audio_d)

        avam = torch.cat([avam,audio_d],dim=1)
        avam = self.resblock3(self.avam3(avam,audio_up))
        audio_up = self.upsampled_audio3(audio_up)
        audio_d = self.default_audio3(audio_d)

        avam = torch.cat([avam,audio_d],dim=1)
        avam = self.resblock4(self.avam4(avam,audio_up))
        audio_up = self.upsampled_audio4(audio_up)
        audio_d = self.default_audio4(audio_d)

        avam = torch.cat([avam,audio_d],dim=1)
        avam = self.resblock5(self.avam5(avam,audio_up))
        # if self.classify and self.patch_output:

        if self.classify:
            return self.classifier(avam)
        if self.return_patch:
            return self.patch_output(avam)

        return avam


"""
The implementation for the script that provides the command-line interface (CLI)
"""
import os, sys, errno, json
from io import StringIO
from argparse import ArgumentParser
from ..validate import ExtValidator
from ..validate import ValidationError, SchemaError, RefResolutionError
from ..validate import MissingSchemaDocument
from ..schemaloader import SchemaLoader, schemaLoader_for_schemas

description = \
"""validate one or more JSON documents against their schemas"""

epilog=None

def define_opts(progname=None):

    parser = ArgumentParser(progname, None, description, epilog)
    parser.add_argument('files', metavar='FILE', type=str, nargs='+',
                        help="files to validate")
    parser.add_argument('-S', '--doc-schema', type=str, dest='docschema',
                        metavar='URI',
                        help="the URI of the schema to assume for the document"
                            +"as a whole (overriding the internal id)")
    parser.add_argument('-L', '--schema-location', type=str, dest='loc',
                        metavar='DIR_OR_FILE', default=None,
                        help="Either a directory containing cached schemas "
                             +"or a schema location file")
    parser.add_argument('-g', '--ignore-extensions', action='store_true',
                        dest='minimal',
                        help="Ignore any extension declarations when "
                            +"validating")
    parser.add_argument('-C', '--strict', action='store_true',
                        dest='strict',
                        help="Fail if an extensions schema cannot be loaded "
                            +"(otherwise, ignore unresolvable extensions)")
    parser.add_argument('-q', '--quiet', action='store_true',
                        help="suppress messages explaining why documents are "
                            +"invalid; only short success/failure message for "
                            +"each file is printed.")
    parser.add_argument('-s', '--silent', action='store_true',
                        help="suppress all output; the exit code indicates "
                            +"if any of the files are invalid.")
    parser.add_argument('-v', '--verbose', action='store_true',
                        help="provide additional messages; useful for "
                            +"troubleshooting")
    parser.add_argument('-e', '--load-enhanced-schemas', action='store_true',
                        dest="loadejs",
                        help="load schemas needed to validate EJS schema  "
                            +"documents (can be overridden by -L).")
    parser.add_argument('-M', '--mongodb-safe', action='store_const',
                        dest="epfx", const='_', default='$',
                        help="use a MongoDB-safe convention for special "
                            +"validation properties, starting them with _ "
                            +"instead of a $")
    parser.add_argument('--val-prop-prefix', action='store', dest="epfx",
                        metavar='PRE', type=str,
                        help="expect the special validation properties in the "
                            +"instance documents to start with the prefix given "
                            +"by PRE (default: '$')")

    return parser

# Exit codes
INVALID   = 1    # one or more input files are invalid
BADSCHEMA = 2    # problem found with one or more schemas (including missing)
BADINPUTS = 3    # bad inputs provided (including files not found)

UNEXPECTED = 10

class Runner(object):

    def __init__(self, progname=None, optsfunc=None, out=sys.stdout,
                 err=sys.stderr, qopt='quiet', sopt='silent'):
        self._parser = optsfunc(progname)
        self.opts = None
        self._q = qopt
        self._s = sopt
        self.err = err
        self.out = out

    @property
    def prog(self):
        return self._parser.prog

    def execute(self, args=None):
        """
        execute the script by parsing the command line arguments and calling
        the run() method.

        :return int:  the code to exit with
        """
        self.opts = self._parser.parse_args(args)
        try:
            return self.run()
        except Exception as ex:
            return self.fail(UNEXPECTED, "Unexpected exception: " + str(ex))

    def run(self):
        return 0

    def fail(self, exitcode, message):
        """
        print a failure message and return the given exit code
        """
        self.complain(message)
        return exitcode

    def complain(self, message):
        if (hasattr(self.opts, self._q) and getattr(self.opts, self._q)) or \
           (hasattr(self.opts, self._q) and getattr(self.opts, self._q)):
            return

        if self.prog:
            self.err.write(self.prog)
            self.err.write(": ")
        self.err.write(message)
        self.err.write('\n')
        self.err.flush()

    def advise(self, message):
        if hasattr(self.opts, self._q) and getattr(self.opts, self._q):
            return

        self.err.write(message)
        self.err.write('\n')
        self.err.flush()

    def tell(self, message):
        if hasattr(self.opts, self._s) and getattr(self.opts, self._s):
            return

        self.out.write(message)
        self.out.write('\n')
        self.out.flush()

class Validate(Runner):
    def __init__(self, progname=None, out=sys.stdout, err=sys.stderr):
        Runner.__init__(self, progname, define_opts, out, err)

    def run(self):
        """
        execute the validate script.

        Command line arguments are parsed from sys.argv.
        """
        if self.opts.silent:
            self.opts.quiet = False
        if self.opts.quiet:
            self.opts.verbose = False

        loader = (self.opts.loadejs and schemaLoader_for_schemas()) or None

        if self.opts.loc:
            if not os.path.exists(self.opts.loc):
                return self.fail(BADINPUTS,
                                 self.opts.loc + ": schema file/dir not found")

            if os.path.isdir(self.opts.loc):
                ldr = SchemaLoader.from_directory(self.opts.loc)
            else:
                ldr = SchemaLoader.from_location_file(self.opts.loc)

            if loader:
                loader.copy_locations_from(ldr)
            else:
                loader = ldr

        val = ExtValidator(loader, ejsprefix=self.opts.epfx)

        doc = None
        anyinvalid = False
        badschema = False
        for filename in self.opts.files:
            try:
                if not os.path.exists(filename):
                    self.complain(filename + ": file not found.")
                    continue
                with open(filename) as fd:
                    doc = json.load(fd)

                val.validate(doc, self.opts.minimal, self.opts.strict,
                             self.opts.docschema)

                if not self.opts.silent:
                    self.tell("{0}: valid!".format(os.path.basename(filename)))
            except (ValidationError, SchemaError, RefResolutionError) as ex:
                f = os.path.basename(filename)
                self.advise("{0}:".format(f))
                if isinstance(ex, MissingSchemaDocument):
                    self.advise("Warning: "+str(ex))
                    if self.opts.verbose:
                        mfd = StringIO()
                        mfd.write("Cached schemas available:")
                        for sid in loader.iterURIs():
                            mfd.write("\n   ")
                            mfd.write(sid)
                        self.advise(mfd.getvalue())

                elif isinstance(ex, RefResolutionError):
                    self.advise("Unable to resolve reference in schema: "+
                                str(ex))
                else:
                    self.advise(str(ex))
                self.tell("{0}: not valid.".format(f))
                anyinvalid = True
                if isinstance(ex, (SchemaError, RefResolutionError)):
                    badschema = True

        return (badschema and BADSCHEMA) or (anyinvalid and INVALID) or 0


import sys
import time
import numpy as np
from gensim.models import KeyedVectors, FastText
from tool_models.sdgp_model.Sdgp.sdgp_tools.ltp4_complete import Ltp4
import torch

class FastTextTool(object):
    def __init__(self, vector_path=None, model_path=None):
        self.vector_path = vector_path
        self.model_path = model_path
        self.FASTTEXT = None
        if self.vector_path:
            t1 = time.time()
            self.FASTTEXT = KeyedVectors.load_word2vec_format(self.vector_path, binary=False)
            t2 = time.time()
            print('KeyedVectors 加载耗时：', t2 - t1)
        if self.model_path:
            t1 = time.time()
            self.FASTTEXT = FastText.load_fasttext_format(self.model_path).wv
            t2 = time.time()
            print('FastText 加载耗时：', t2 - t1, 's')
        self.ltp_tool = Ltp4()

    def find_longest_sub_token(self, token):
        for index in range(len(token), 0, -1):
            if token[:index] in self.FASTTEXT:
                return index
        return -1

    def token_seperate_for_OOV(self, token):
        tokens = []
        rest_token = token
        while len(rest_token) > 0:
            longest_sub_token_length = self.find_longest_sub_token(rest_token)
            if longest_sub_token_length == -1:
                break
            else:
                tokens.append(rest_token[:longest_sub_token_length])
                rest_token = rest_token[longest_sub_token_length:]
        if len(rest_token) > 0:
            print('OOV token 处理不了:', rest_token)
            sys.exit(1)

        return tokens

    def encode(self, text):
        analyse_out = self.ltp_tool.analyze(text)[0]
        words = analyse_out['words']
        word_vectors = []
        for word in words:
            if word in self.FASTTEXT:
                word_vectors.append(self.FASTTEXT[word])
            else:
                tokens = self.token_seperate_for_OOV(word)
                for token in tokens:
                    word_vectors.append(self.FASTTEXT[token])
        embedding = np.mean(word_vectors, axis=0)
        embedding = torch.tensor(embedding)
        return embedding

    def compute_similarity(self, text1, text2):
        embedding1 = self.encode(text1)
        embedding2 = self.encode(text2)
        similarity_score = torch.cosine_similarity(embedding1, embedding2, dim=0)

        return similarity_score

def test_some_samples():
    model_path = 'fasttext/cc/cc.zh.300.bin'
    fasttext = FastTextTool(vector_path=None, model_path=model_path)

if __name__ == '__main__':
    test_some_samples()


from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.decorators import login_required
from .models import Empresa, Refeicao, Alimento
from .forms import  EmpresaForm, RefeicaoForm, AlimentoForm
from django.contrib.auth.models import User

@login_required
def usuarios_new(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        User.objects.create_user(form.data['username'], None, form.data['password1']    )
        return redirect('usuario_list')
    return render(request, "cadastrar_usuario.html", {"form": UserCreationForm() })

@login_required
def usuarios_list(request):
    usuarios = User.objects.all()
    return render(request, 'usuario.html', {'usuarios': usuarios})
@login_required
def usuarios_delete(request, id):
    usuario = get_object_or_404(Usuario, pk=id)
    form = UsuarioForm(request.POST or None, instance=usuario)

    if request.method == 'POST':
        usuario.delete()
        return redirect('usuario_list')
    return render(request, 'usuario_delete_confirm.html', {'usuario': usuario})
@login_required
def empresas_new(request):
    form = EmpresaForm(request.POST or None)

    if form.is_valid():
        form.save()
        return redirect('empresa_list')
    return render(request, 'cadastrar_empresa.html', {'form': form})

@login_required
def empresas_update(request, id):
    empresa = get_object_or_404(Empresa, pk=id)
    form = EmpresaForm(request.POST or None, instance=empresa)

    if form.is_valid():
        form.save()
        return redirect('empresa_list')
    return render(request, 'cadastrar_empresa.html', {'form': form})

@login_required
def empresas_delete(request, id):
    empresa = get_object_or_404(Empresa, pk=id)
    form = EmpresaForm(request.POST or None, instance=empresa)

    if request.method == 'POST':
        empresa.delete()
        return redirect('empresa_list')
    return render(request, 'empresa_delete_confirm.html', {'empresa': empresa})

@login_required
def empresas_list(request):
    empresas = Empresa.objects.all()
    return render(request, 'empresa.html', {'empresas': empresas})

@login_required
def alimentos_new(request):
    form = AlimentoForm(request.POST or None)

    if form.is_valid():
        form.save()
        return redirect('alimento_list')
    return render(request, 'cadastrar_alimento.html', {'form': form})

@login_required
def alimentos_list(request):
    alimentos = Alimento.objects.all()
    return render(request, 'alimento.html', {'alimentos': alimentos})

@login_required
def alimentos_update(request, id):
    alimento = get_object_or_404(Alimento, pk=id)
    form = AlimentoForm(request.POST or None, instance=alimento)

    if form.is_valid():
        form.save()
        return redirect('alimento_list')
    return render(request, 'cadastrar_alimento.html', {'form': form})

@login_required
def alimentos_delete(request, id):
    alimento = get_object_or_404(Alimento, pk=id)
    form = AlimentoForm(request.POST or None, instance=alimento)

    if request.method == 'POST':
        alimento.delete()
        return redirect('alimento_list')
    return render(request, 'alimento_delete_confirm.html', {'alimento': alimento})

@login_required
def refeicaos_new(request):
    form = RefeicaoForm(request.POST or None)

    if form.is_valid():
        form.save()
        return redirect('refeicao_list')
    return render(request, 'cadastrar_refeicao.html', {'form': form})

@login_required
def refeicaos_list(request):
    refeicaos = Refeicao.objects.all()
    return render(request, 'refeicao.html', {'refeicaos': refeicaos})

@login_required
def refeicaos_update(request, id):
    refeicao = get_object_or_404(Refeicao, pk=id)
    form = RefeicaoForm(request.POST or None, instance=refeicao)

    if form.is_valid():
        form.save()
        return redirect('refeicao_list')
    return render(request, 'cadastrar_refeicao.html', {'form': form})

@login_required
def refeicaos_delete(request, id):
    refeicao = get_object_or_404(Refeicao, pk=id)
    form = RefeicaoForm(request.POST or None, instance=refeicao)

    if request.method == 'POST':
        refeicao.delete()
        return redirect('refeicao_list')
    return render(request, 'refeicao_delete_confirm.html', {'refeicao': refeicao})


"""
--- Day 11: Space Police ---

On the way to Jupiter, you're pulled over by the Space Police.

"Attention, unmarked spacecraft! You are in violation of Space Law! All spacecraft must
 have a clearly visible registration identifier! You have 24 hours to comply or be sent
  to Space Jail!"

Not wanting to be sent to Space Jail, you radio back to the Elves on Earth for help.
 Although it takes almost three hours for their reply signal to reach you, they send
  instructions for how to power up the emergency hull painting robot and even provide a
   small Intcode program (your puzzle input) that will cause it to paint your ship
    appropriately.

There's just one problem: you don't have an emergency hull painting robot.

You'll need to build a new emergency hull painting robot. The robot needs to be able to
 move around on the grid of square panels on the side of your ship, detect the color of
  its current panel, and paint its current panel black or white. (All of the panels are
   currently black.)

The Intcode program will serve as the brain of the robot. The program uses input
 instructions to access the robot's camera: provide 0 if the robot is over a black
  panel or 1 if the robot is over a white panel. Then, the program will output two
   values:

First, it will output a value indicating the color to paint the panel the robot is over:
 0 means to paint the panel black, and 1 means to paint the panel white.
Second, it will output a value indicating the direction the robot should turn: 0 means
 it should turn left 90 degrees, and 1 means it should turn right 90 degrees.
After the robot turns, it should always move forward exactly one panel. The robot
 starts facing up.

The robot will continue running for a while like this and halt when it is finished
 drawing. Do not restart the Intcode computer inside the robot during this process.

For example, suppose the robot is about to start running. Drawing black panels as .,
 white panels as #, and the robot pointing the direction it is facing (< ^ > v), the
  initial state and region near the robot looks like this:

.....
.....
..^..
.....
.....
The panel under the robot (not visible here because a ^ is shown instead) is also black,
 and so any input instructions at this point should be provided 0. Suppose the robot
  eventually outputs 1 (paint white) and then 0 (turn left). After taking these actions
   and moving forward one panel, the region now looks like this:

.....
.....
.<#..
.....
.....
Input instructions should still be provided 0. Next, the robot might output 0 (paint
 black) and then 0 (turn left):

.....
.....
..#..
.v...
.....
After more outputs (1,0, 1,0):

.....
.....
..^..
.##..
.....
The robot is now back where it started, but because it is now on a white panel, input
 instructions should be provided 1. After several more outputs (0,1, 1,0, 1,0), the
  area looks like this:

.....
..<#.
...#.
.##..
.....
Before you deploy the robot, you should probably have an estimate of the area it will
 cover: specifically, you need to know the number of panels it paints at least once,
  regardless of color. In the example above, the robot painted 6 panels at least once.
   (It painted its starting panel twice, but that panel is still only counted once; it
    also never painted the panel it ended on.)

Build a new emergency hull painting robot and run the Intcode program on it. How many
 panels does it paint at least once?

--- Part Two ---

You're not sure what it's trying to paint, but it's definitely not a registration
 identifier. The Space Police are getting impatient.

Checking your external ship cameras again, you notice a white panel marked "emergency
 hull painting robot starting panel". The rest of the panels are still black, but it
  looks like the robot was expecting to start on a white panel, not a black one.

Based on the Space Law Space Brochure that the Space Police attached to one of your
 windows, a valid registration identifier is always eight capital letters. After
  starting the robot on a single white panel instead, what registration identifier does
   it paint on your hull?
"""

import numpy as np
from enum import Enum
from typing import List

class OpCode(Enum):
    STOP = 99

class InitMode(Enum):
    NORMAL = 0
    REPRISE = 1

class Intcode:
    """Class for IntCode"""

    def __init__(self, instructions: np.array):
        self.orig_instructions = instructions
        # Default input is 0
        self.input = [0]
        self.reset()

    def reset(self):
        self.index = 0
        self.base = 0
        self.is_first_exec = 0
        self.ops = np.zeros(shape=(10000,), dtype=np.int64)
        self.ops[: len(self.orig_instructions)] = self.orig_instructions

    # In normal mode, reset after assigning inputs
    # In reprise mode, only assign inputs
    def init_mode(self, init_mode: InitMode, input_values: List[int]):
        self.input = input_values
        if init_mode == InitMode.NORMAL:
            self.reset()
        elif init_mode == InitMode.REPRISE:
            if self.is_first_exec == 0:
                # Provide full input
                self.input = input_values
            else:
                # Only provide input signal -- pop the setting
                self.input.pop()
        else:
            raise Exception(f"Init Mode {init_mode} not supported")

    def get_params(self, modes, num: int):
        params = []
        for mode, value in zip(modes, range(self.index + 1, self.index + num)):
            if mode == "2":
                params.append(self.ops[value] + self.base)
            elif mode == "1":
                params.append(value)
            elif mode == "0":
                params.append(self.ops[value])
            else:
                raise Exception(f"Mode {mode} is not supported.")

        return params

    def run(self, print_mode=False):
        while True:
            # Fetch instruction -- split in op and modes
            op = self.ops[self.index] % 100
            modes = list(reversed(f"{self.ops[self.index] // 100:03d}"))

            # Stop if stop op is reached or input is OpCode.STOP
            if op == 99:
                return OpCode.STOP

            # Execute instructions
            if op == 1:
                values = self.get_params(modes, 4)
                self.ops[values[2]] = self.ops[values[0]] + self.ops[values[1]]
                self.index += 4
            elif op == 2:
                values = self.get_params(modes, 4)
                self.ops[values[2]] = self.ops[values[0]] * self.ops[values[1]]
                self.index += 4
            elif op == 3:
                values = self.get_params(modes, 2)
                self.ops[values[0]] = self.input.pop()
                self.index += 2
            elif op == 4:
                values = self.get_params(modes, 2)
                # Update is_first_run status
                self.is_first_exec = 1
                self.index += 2

                if print_mode:
                    print(self.ops[values[0]])
                else:
                    return self.ops[values[0]]

            elif op == 5:
                values = self.get_params(modes, 3)
                if self.ops[values[0]] != 0:
                    self.index = self.ops[values[1]]
                else:
                    self.index += 3
            elif op == 6:
                values = self.get_params(modes, 3)
                if self.ops[values[0]] == 0:
                    self.index = self.ops[values[1]]
                else:
                    self.index += 3
            elif op == 7:
                values = self.get_params(modes, 4)
                self.ops[values[2]] = (
                    1 if self.ops[values[0]] < self.ops[values[1]] else 0
                )
                self.index += 4
            elif op == 8:
                values = self.get_params(modes, 4)
                self.ops[values[2]] = (
                    1 if self.ops[values[0]] == self.ops[values[1]] else 0
                )
                self.index += 4
            elif op == 9:
                values = self.get_params(modes, 2)
                self.base += self.ops[values[0]]
                self.index += 2

            else:
                return -1

def colour_and_turn(intcode: Intcode, grid: np.array):
    """Convenience function to implement the colour&turn loop"""
    # Initialize position, direction and rotation arrays
    grid_size, _ = grid.shape
    pos = np.array([grid_size // 2, grid_size // 2])
    direction = np.array([0, 1])
    R = np.array([[0, -1], [1, 0]])

    # Keep track of visited positions
    set_visited = set()
    # Set the initial turn command to 0
    turn = 0
    while turn != OpCode.STOP:
        # Run in reprise mode -- repeat the input twice to be consistent with the
        #  reprise mode implementation
        x, y = pos
        in_color = grid[x, y]
        intcode.init_mode(InitMode.REPRISE, [in_color, in_color])
        # Fetch color
        color = intcode.run()
        if color == OpCode.STOP:
            break
        # Update color
        grid[x, y] = color
        # Add to visited positions
        set_visited.add((x, y))
        # Fetch turn
        turn = intcode.run()
        # Update direction
        if turn == 0:
            direction = R.dot(direction)
        elif turn == 1:
            direction = -R.dot(direction)
        else:
            raise Exception(f"Turn {turn} is not recognized.")
        # Update grid
        pos = pos + direction

    return set_visited, grid

def main():
    input_array = np.loadtxt("2019/11/input.txt", delimiter=",", dtype=np.int64)
    instructions = input_array.copy()

    # Init intcode
    intcode = Intcode(instructions)

    # Initialize grid -- part 1
    grid = np.zeros(shape=(1001, 1001), dtype=np.int64)

    set_visited, _ = colour_and_turn(intcode, grid)

    print(f"Result of part 1: {len(set_visited)}")

    # Initialize grid -- part 2
    intcode.reset()
    grid = np.zeros(shape=(1001, 1001), dtype=np.int64)
    grid[500, 500] = 1

    set_visited, grid = colour_and_turn(intcode, grid)
    # Find first and last non-zero row and column
    zero_rows = np.where(grid.sum(axis=1) != 0)[0]
    zero_cols = np.where(grid.sum(axis=0) != 0)[0]
    grid = grid[
        zero_rows[0] - 1 : zero_rows[-1] + 2, zero_cols[0] - 1 : zero_cols[-1] + 2
    ]
    # Get final image
    map_to_pixel = {0: " ", 1: "#"}
    decoded = ""
    for row in np.rot90(grid):
        decoded += "".join(map(map_to_pixel.get, row)) + "\n"
    print("Result of part 2: ")
    print("")
    print(f"{decoded}")

if __name__ == "__main__":
    import cProfile

    cProfile.run("main()")


import json

path = "/Users/luke/Desktop/Code/progressive_technicalities/src/main/resources/assets/progressivetechnicalities/models/item/"

while True:
	name = input("name? ")

	data = {
		"parent": "item/generated",
		"textures": {
			"layer0": "progressivetechnicalities:items/" + name
		}
	}

	with open(path + name + ".json", "w") as f:
		f.write(json.dumps(data, indent=4))


import unittest

from selenium.webdriver import ActionChains

from config import browser
from locators.draggable_locators import DRAGGABLE_URL, SOURCE_ELEMENT

class DraggablePage(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = browser()
        cls.driver.get(DRAGGABLE_URL)

    def test_draggable(self):
        source_element = self.driver.find_element(*SOURCE_ELEMENT)
        current_location = source_element.location

        action = ActionChains(self.driver)
        action.drag_and_drop_by_offset(source_element, 150, 200).perform()

        location = source_element.location

        self.assertNotEqual(current_location, location)

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

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


import argparse

from molecular_ml.evaluation.mpn_ffd_eval import eval_model as evalmpn
from molecular_ml.evaluation.attention_lstm_eval import eval_model as evallstm
from molecular_ml.utils.io import read_cfg

def parse_opt():
    parser = argparse.ArgumentParser(description=".")
    parser.add_argument('model_path', type=str, required = True, help='weight path of the model')
    parser.add_argument("val_data_path", help="dataset path with labels", type=str, required = True)
    parser.add_argument("--config_file", default="", help="path to config file", type=str)

    args = parser.parse_args()
    return args

def evaluate_model(model_path: str, val_data_path: str, config_file: str=""):
    #Read config file
    cfg = read_cfg(config_file)
    if cfg.MODEL.name == "attention_lstm":
        evallstm(model_path, val_data_path, config_file)
    elif cfg.MODEL.name == "mpn_ffd":
        evalmpn(model_path, val_data_path, config_file)

if __name__ == "__main__":
    args = parse_opt()
    evaluate_model(args.model_path,
                   args.val_data_path,
                   args.config_file)


from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import JSONResponse

from app.api.account_routes import router as account_route
from app.api.session_routes import router as session_route
from app.api.chat_routes import router as chat_route
from app.api.collect_routes import router as collect_route
from app.api.feedback_routes import router as feedback_route
from app.api.role_routes import router as role_route
from app.api.file_routes import router as file_route
from app.config import Config
from app.core.exceptions import UserAccessDeniedException
from app.core.logging import logging
from app.models.response import ApiResponse

app = FastAPI()

# Enables CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(account_route, prefix=f"{Config.API_PREFIX}/v1")
app.include_router(session_route, prefix=f"{Config.API_PREFIX}/v1")
app.include_router(chat_route, prefix=f"{Config.API_PREFIX}/v1")
app.include_router(collect_route, prefix=f"{Config.API_PREFIX}/v1")
app.include_router(feedback_route, prefix=f"{Config.API_PREFIX}/v1")
app.include_router(role_route, prefix=f"{Config.API_PREFIX}/v1")
app.include_router(file_route, prefix=f"{Config.API_PREFIX}/v1")

@app.exception_handler(Exception)
async def conflict_error_handler(_, exc: Exception):
    """全局异常处理"""
    logging.error(exc)
    # 返回状态码仍为200，exc的错误信息放到ApiResponse中以json格式方式返回并且可以跨域访问
    return JSONResponse(
        headers={
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "*",
        },
        content=ApiResponse(code="500", status="FAILED", message=str(exc)).__dict__,
    )

# UserAccessDeniedException异常处理状态码为403
@app.exception_handler(UserAccessDeniedException)
async def user_access_denied_error_handler(_, exc: UserAccessDeniedException):
    """全局异常处理"""
    logging.error(exc)
    # 返回状态码仍为200，exc的错误信息放到ApiResponse中以json格式方式返回并且可以跨域访问
    return JSONResponse(
        headers={
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "*",
        },
        content=ApiResponse(code="403", status="FAILED", message=str(exc)).__dict__,
    )


from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name="home"),
    path('detalle/<int:categoria_id>/', views.detalle, name='detalle'),
    path('contacto/', views.contacto, name="contacto"),
    path('checkout/', views.checkout, name="checkout"),
    path('cart/', views.cart, name="cart"),
    path('productos/<int:categoria_id>/', views.productos_por_categoria, name='productos_por_categoria'),
    path('cargar-productos/', views.cargar_productos_desde_excel, name='cargar_productos'),
    path('cargar-categorias/', views.cargar_categorias_desde_excel, name='cargar_categorias'),
    path('cargar-unidades/', views.cargar_unidades_desde_excel, name='cargar_unidades'),
    path('cargar-tamanos/', views.cargar_tamanos_desde_excel, name='cargar_tamanos'),
    path('cargar-regiones/', views.cargar_regiones_desde_excel, name='cargar_regiones'),
    path('cargar-ciudades/', views.cargar_ciudades_desde_excel, name='cargar_ciudades'),
    path('cargar-comunas/', views.cargar_comunas_desde_excel, name='cargar_comunas'),
    path('creararticulo/', views.creararticulo, name="creararticulo"),
    path('crear/', views.crear, name="crear"),
    path('nuevo_producto/', views.nuevo_producto, name="nuevo_producto"),
]


from pysnmp.hlapi import *
from data.config import get_sig_ctrl_interface_params

def snmpSet(OID, Value):
    """
    :Author:
        Aschkan Omidvar <aschkan@ufl.edu>
    :Date:
        Jan-2017
    """
    assert type(OID) == str
    errorIndication, errorStatus, errorIndex, varBinds = next(
        setCmd(SnmpEngine(),
               CommunityData('public', mpModel=0),  # snmp v1. delete mpModel for v2c),
               UdpTransportTarget(('169.254.91.71', 161)),
               ContextData(),
               ObjectType(ObjectIdentity(str(OID)), Integer(Value))))

    if errorIndication:
        print(errorIndication)
    elif errorStatus:
        print('%s at %s' % (errorStatus.prettyPrint(),
                            errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
    else:
        for varBind in varBinds:
            print(' = '.join([x.prettyPrint() for x in varBind]))

def snmpTranslate(List):
    """
    Example: 2^^3 phase translation breakdown:

    - Bit 7 = Ring number = (ringControlGroupNumber * 8)
    - Bit 6 = Ring number = (ringControlGroupNumber * 8) - 1
    - Bit 5 = Ring number = (ringControlGroupNumber * 8) - 2
    - Bit 4 = Ring number = (ringControlGroupNumber * 8) - 3
    - Bit 3 = Ring number = (ringControlGroupNumber * 8) - 4
    - Bit 2 = Ring number = (ringControlGroupNumber * 8) - 5
    - Bit 1 = Ring number = (ringControlGroupNumber * 8) - 6
    - Bit 0 = Ring number = (ringControlGroupNumber * 8) - 7

    :Author:
        Aschkan Omidvar <aschkan@ufl.edu>
    :Date:
        Jan-2017

    .. note::
        This module translates the phase numbers in a given list into snmp legible
        integers according to NTCIP 1202. The code encripts the list of the phases
        into a binary string and then parses it to an snmp int value.

    """

    Power = max(List)
    Binary = ''
    for i in range(Power):
        if i + 1 in List:
            Binary += '1'
        else:
            Binary += '0'
    revB = Binary[::-1]
    snmpCode = int(revB, 2)
    return snmpCode

def snmpOmit(List):
    """
    :Author:
        Aschkan Omidvar <aschkan@ufl.edu>
    :Date:
        Jan-2017
    .. note::
        This module transforms the bit matrix values for OID
        enterprise::1206.4.2.1.1.5.1.2.1 to the corresponding phase number and
        omit it. Hold is a command that causes omission of a selected phase.
    """
    assert type(List) == list  #
    OmitTemp = sorted(List)
    if List == [0]:
        snmpTerminate()
    else:
        snmpSet('1.3.6.1.4.1.1206.4.2.1.1.5.1.2.1', snmpTranslate(OmitTemp))

def snmpHold(List):
    """
    :Author:
        Aschkan Omidvar <aschkan@ufl.edu>
    :Date:
        Jan-2017

    .. note::
        This module transforms the bit matrix values for OID
        enterprise::1206.4.2.1.1.5.1.4.1 to the corresponding phase number and
        hold it. Hold is a command that retains the existing Green interval.
    """
    assert type(List) == list  #
    HoldTemp = sorted(List)
    if List == [0]:
        snmpTerminate()
    else:
        snmpSet('1.3.6.1.4.1.1206.4.2.1.1.5.1.4.1', snmpTranslate(HoldTemp))

def snmpForceOff(List):
    """
    :Author:
        Aschkan Omidvar <aschkan@ufl.edu>
    :Date:
        Jan-2017

    .. note::
        This module transforms the bit matrix values for OID
        enterprise::1206.4.2.1.1.5.1.5.1 to the corresponding phase number and
        Force Off it. Force off is A command to force the termination of the green
        interval in the actuated mode or Walk Hold in the nonactuated mode of the
        associated phase. Termination is subject to the presence of a serviceable
        conflicting call. The Force Off function shall not be effective during the
        timing of the Initial,Walk, or Pedestrian Clearance. The Force Off shall
        only be effective as long as the condition is sustained. If a phase
        specific Force Off is applied, the Force Off shall not prevent the start
        of green for that phase
    """
    assert type(List) == list
    ForceOffTemp = sorted(List)
    if List == [0]:
        snmpTerminate()
    else:
        snmpSet('1.3.6.1.4.1.1206.4.2.1.1.5.1.5.1', snmpTranslate(ForceOffTemp))

def snmpVehCall(List):
    """
    :Author:
        Aschkan Omidvar <aschkan@ufl.edu>
    :Date:
        Jan-2017

    .. note::
        This module transforms the bit matrix values for OID
        enterprise::1206.4.2.1.1.5.1.6.1 to the corresponding phase number and
        call a vehicle on it.
    """
    assert type(List) == list
    VehCallTemp = sorted(List)
    if List == [0]:
        snmpTerminate()
    else:
        snmpSet('1.3.6.1.4.1.1206.4.2.1.1.5.1.6.1', snmpTranslate(VehCallTemp))

def snmpTerminate():
    """
    :Author:
        Aschkan Omidvar <aschkan@ufl.edu>
    :Date:
        Jan-2017

    .. note::
        This module terminates all the commands and resets the signal controller to
        the default mode (actauted)
    """
    snmpSet('1.3.6.1.4.1.1206.4.2.1.1.5.1.2.1', 0)
    snmpSet('1.3.6.1.4.1.1206.4.2.1.1.5.1.4.1', 0)
    snmpSet('1.3.6.1.4.1.1206.4.2.1.1.5.1.5.1', 0)
    snmpSet('1.3.6.1.4.1.1206.4.2.1.1.5.1.6.1', 0)

def snmp_phase_ctrl(Phase, inter_name):
    """
    :Author:
        Aschkan Omidvar <aschkan@ufl.edu>
    :Date:
        Jan-2017

    .. note::
        Send command to ASC
    """
    num_phase, al, non, non_conflict = get_sig_ctrl_interface_params(inter_name)

    snmpHold(list(al))
    snmpHold(list(non))

    for p in range(len(non_conflict)):
        if Phase in non_conflict[p]:
            snmpVehCall(non_conflict[p])
            snmpOmit([i for i in al if i not in non_conflict[p]])

# Quickstart Test
# snmp_phase_ctrl(4)


import sqlite3, datetime, random
from flask import *

app = Flask(__name__)
app.config["PERMANENT_SESSION_LIFETIME"] = datetime.timedelta(minutes=10)
app.secret_key = 'haley-duddjwebserver'

def sql_check():
    try:
        db = sqlite3.connect("./data/Haley.db")
        SQL = db.cursor()
        return db, SQL
    except:
        return False, False

def eng_String():
    db, SQL = sql_check()
    if db == False: return False
    try:
        SQL.execute('SELECT engA1, engA2, engA3 FROM eng_String WHERE num_id = 1')
        result = SQL.fetchone()
    except Exception as e:
        print(e)
        return False
    result_A1 = str(random.sample((str(result[0]).replace(',', '')).split(), len((str(result[0]).replace(',', '')).split()))).replace("[", '').replace("'", '').replace("]", '')
    result_A2 = str(random.sample((str(result[1]).replace(',', '')).split(), len((str(result[1]).replace(',', '')).split()))).replace("[", '').replace("'", '').replace("]", '')
    result_A3 = str(random.sample((str(result[2]).replace(',', '')).split(), len((str(result[2]).replace(',', '')).split()))).replace("[", '').replace("'", '').replace("]", '')
    print(result_A1, result_A2, result_A3)
    return result_A1, result_A2, result_A3

def eng_check(id_num: int = None, text: str = None):
    if text is None or id_num is None: return False
    db, SQL = sql_check()
    if db == False: return False
    try:
        SQL.execute('SELECT engA1, engA2, engA3 FROM eng_String WHERE num_id = 1')
        result = SQL.fetchone()
    except:
        return False
    if result[id_num] == text:
        return True
    else:
        return False

@app.route('/', methods = ['POST', 'GET'])
def home():
    if request.method == 'POST':
        session['username'] = request.form['username']
        session['usernumber'] = request.form['classnum']
        if session['username'] == 'admin' and session['usernumber'] == '040728':
            return redirect(url_for('admin'))
        return redirect(url_for('home'))
    elif request.method == 'GET':
        if 'username' in session:  # session안에 username이 있으면 로그인
            return redirect(url_for('input_test'))
    return render_template('/home.html') # 로그인이 안될 경우

@app.route('/input-test', methods = ['POST', 'GET'])
def input_test():
    if request.method == "GET":
        if 'username' not in session:
            return redirect(url_for('home'))
        print(eng_String())
        eng_Q1, eng_Q2, eng_Q3 = eng_String()
        if eng_String() == False:
            return render_template('/result_page.html', _result = '오류가 발생 하였습니다. 데이터')
        return render_template('/inputs_web.html', eng_Q1 = eng_Q1, eng_Q2 = eng_Q2, eng_Q3 = eng_Q3)
    elif request.method == 'POST':
        engQ1, engQ2, engQ3 = request.form['engQ1'], request.form['engQ2'], request.form['engQ3']
        if eng_check(0, engQ1) == True: view_resultQ1 = '정답'
        else: view_resultQ1 = '오답'
        if eng_check(1, engQ2) == True: view_resultQ2 = '정답'
        else: view_resultQ2 = '오답'
        if eng_check(2, engQ3) == True: view_resultQ3 = '정답'
        else: view_resultQ3 = '오답'
        return f'{engQ1} - {view_resultQ1}
{engQ2} - {view_resultQ2}
{engQ3} - {view_resultQ3}'

@app.route('/admin-page')
def admin():
    if request.method == "GET":
        if 'username' not in session:
            return render_template('/home.html')
        if session['username'] != 'admin' and session['usernumber'] != '040728':
            if 'username' in session:
                return redirect(url_for('input_test'))
            return redirect(url_for('home'))
        return render_template('/admin_page.html')

@app.route('/eng-stringinputjsonload', methods = ['POST'])
def eng_stringinputjsonload():
    if request.method == 'POST':
        db, SQL = sql_check()
        if db == False: return render_template('/result_page.html', _result = '오류가 발생 하였습니다.
데이터 불러오는 중 에러발생')
        engA1, engA2, engA3 = request.form['engA1'], request.form['engA2'], request.form['engA3']
        try:
            SQL.execute(f'UPDATE eng_String SET engA1 = "{engA1}" WHERE num_id = 1')
            SQL.execute(f'UPDATE eng_String SET engA2 = "{engA2}" WHERE num_id = 1')
            SQL.execute(f'UPDATE eng_String SET engA3 = "{engA3}" WHERE num_id = 1')
            db.commit()
            _result = '성공적으로 수정이 완료 되었습니다.'
        except Exception as e:
            _result = f'일시적인 서버 오류로 실행이 최소 되었습니다.\n{e}'
        return render_template('/result_page.html', _result = _result)

@app.route('/test-result/<result>')
def test_result(result):
    str(result).replace('<>', '
')
    return render_template('/result_page.html', _result = result)

@app.route('/logout')
def logout():
    session.pop('username', None)
    session.pop('usernumber', None)
    return redirect(url_for('home'))

app.run(host='0.0.0.0', port=2022)


import postman

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

import os.path
from appsettings import AlphaStockSettings

__settings_path = './alphastock_settings.json'
settings = AlphaStockSettings()
if not os.path.exists(__settings_path):
    print(f"새로운 설정 파일 생성, '{__settings_path}'")
    settings.create_settings(__settings_path)
    exit()
else:
    settings.load_settings(__settings_path)

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

import codecs

def build_auth_mail(email, token):
    message = __auth_message.replace('%email%', email).replace('%token%', token)
    return message

def build_welcome_mail(email, token):
    message = __welcome_message.replace('%email%', email).replace('%token%', token)
    return message

__auth_file_path = './auth.html'
__welcome_file_path = './welcome.html'
__auth_message = codecs.open(__auth_file_path, 'r', 'utf-8').read()
__welcome_message = codecs.open(__welcome_file_path, 'r', 'utf-8').read()

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

import requests

def check_recaptcha(user_response):
    payload = { 'secret': settings.recaptcha_secret, 'response': user_response }
    response = requests.post(__url, data = payload).json()
    return response['success']

__url = 'https://www.google.com/recaptcha/api/siteverify'

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

from flask import Flask, render_template, redirect, url_for, request, jsonify
from validate_email import validate_email

app = Flask(__name__)
project_nickname = '주가예측 알리미'

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/favorite/<email>/<token>')
def favorite(email, token):
    if email is None or email.isspace() or not validate_email(email):
        print('올바른 이메일의 형식이 아님', email)
        return 'Credential을 확인할 수 없습니다.'

    if token is None or token.isspace() or len(token) != 8:
        print('올바른 토큰의 형식이 아님', token)
        return 'Credential을 확인할 수 없습니다.'

    subscriber = postman.Subscriber(email)
    if token != subscriber.token:
        print('토큰이 일치하지 않음', token)
        return 'Credential을 확인할 수 없습니다.'

    if not postman.sub_manager.check_subscribe(email):
        print('구독중이 아닌 이메일', email)
        return '구독자만 이용할 수 있습니다.'

    # favorites = { 'data': postman.db_manager.select_favorite_stock_ids(subscriber) }
    favorites = postman.db_manager.select_favorite_stock_ids(subscriber)

    return render_template('favorite.html', email = email, token = token, stock_table = postman.stock_table, favorites = favorites)

@app.route('/api/favorite', methods = ['POST'])
def save_favorite():
    email = request.form['email']
    token = request.form['token']
    favorites = request.form.getlist('favorites[]')

    if email is None or email.isspace() or not validate_email(email):
        print('올바른 이메일의 형식이 아님', email)
        return 'Credential을 확인할 수 없습니다.'

    if token is None or token.isspace() or len(token) != 8:
        print('올바른 토큰의 형식이 아님', token)
        return 'Credential을 확인할 수 없습니다.'

    subscriber = postman.Subscriber(email)
    if token != subscriber.token:
        print('토큰이 일치하지 않음', token)
        return 'Credential을 확인할 수 없습니다.'

    if not postman.sub_manager.check_subscribe(email):
        print('구독중이 아닌 이메일', email)
        return '구독자만 이용할 수 있습니다.'

    postman.db_manager.delete_all_favorites(subscriber)
    postman.db_manager.insert_favorites(subscriber, favorites)

    return '저장되었습니다.'

@app.route('/api/subscribe', methods = ['POST'])
def subscribe():
    email = request.form['email']
    grecaptcha_response = request.form['grecaptcha_response']

    if grecaptcha_response is None or not check_recaptcha(grecaptcha_response):
        print('reCAPTCHA가 만료됨')
        return 'reCAPTCHA가 만료되었습니다.\n다시 시도해주세요.'

    if email is None or email.isspace() or not validate_email(email):
        print('올바른 이메일의 형식이 아님', email)
        return f"'{email}'\n올바른 형식의 이메일이 아닙니다."

    if postman.sub_manager.check_subscribe(email):
        print('이미 구독중인 이메일', email)
        return f"'{email}'\n이미 구독중인 이메일입니다."

    subscriber = postman.Subscriber(email)
    postman.sub_manager.add_to_auth(subscriber)
    subject = f'{project_nickname} 구독 인증 메일입니다'
    body = build_auth_mail(subscriber.email, subscriber.token)

    gmail_sender = postman.GmailSender(postman.settings.gmail_account, postman.settings.gmail_password, project_nickname)
    gmail_sender.send_mail([email], subject, body, True)

    return f"'{email}'\n구독을 위한 인증메일이 발송되었습니다.\n이메일을 확인해주세요."

@app.route('/api/unsubscribe/<email>/<token>')
def unsubscribe(email, token):
    if email is None or email.isspace() or not validate_email(email):
        print('올바른 이메일의 형식이 아님', email)
        return f"'{email}'\n올바른 형식의 이메일이 아닙니다."

    if token is None or token.isspace() or len(token) != 8:
        print('올바른 토큰의 형식이 아님', token)
        return '구독 해지 실패'

    if not postman.sub_manager.check_subscribe(email):
        print('구독중이 아닌 이메일 주소', email)
        return f"'{email}'\n구독중이 아닌 이메일입니다."

    postman.sub_manager.unsubscribe(email, token)

    return f"'{email}'\n구독 해지"

@app.route('/api/auth/<email>/<token>')
def auth(email, token):
    if email is None or email.isspace() or not validate_email(email):
        print('올바른 이메일의 형식이 아님', email)
        return '인증 실패'

    if token is None or token.isspace() or len(token) != 8:
        print('올바른 토큰의 형식이 아님', token)
        return '인증 실패'

    if postman.sub_manager.check_subscribe(email):
        print('이미 구독중인 이메일', email)
        return '인증 실패'

    if not postman.sub_manager.auth(email, token):
        print('인증 실패', email, token)
        return '인증 실패'

    postman.sub_manager.subscribe(email)
    subject = f'[{project_nickname}] 관심 종목을 선택해주세요'
    body = build_welcome_mail(email, token)

    gmail_sender = postman.GmailSender(postman.settings.gmail_account, postman.settings.gmail_password, project_nickname)
    gmail_sender.send_mail([email], subject, body, True)

    return '이메일 인증이 완료되었습니다.\n이메일을 확인해 주세요.'

@app.route('/elements')
def elements():
    return render_template('elements.html')

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

if __name__ == '__main__':
    app.run()


import numpy as np
import h5py

class Recorder:
    def __init__(self):
        self.memory = []

    def add(self, chunk):
        # if len(self.memory) > 0:
        #     assert self.memory[-1].shape[0] == chunk.shape[0]
        self.memory.append(chunk)

    def clear(self):
        self.memory = []

    def save(self, config_recorder):
        with h5py.File(str(config_recorder.save_path) + '.h5', 'w') as file:
            if len(self.memory) > 0:
                stacked_data = np.concatenate(self.memory, axis=0)
                file['raw_data'] = stacked_data[:, config_recorder.channel_index]
            else:
                empty_shape = (0, config_recorder.dataset_width)
                file.create_dataset('raw_data', empty_shape)

            file.attrs['fs'] = config_recorder.fs
            file.attrs['channel_bads'] = np.asarray(config_recorder.channel_bads)
            file.attrs['channel_names'] = '|'.join(config_recorder.channel_names)

            #
            # file.create_dataset('channel_bads', data=np.asarray(config_recorder.channel_bads))
            # file.create_dataset('channel_names', data=np.asarray(config_recorder.channel_names))
            # file.create_dataset('fs', data=np.array(config_recorder.fs))


import numpy as np
import torch
import torch.nn.functional as F
import torch.utils.data as tdata
from conformal import ConformalModelLogits

def ensemble_logits(list_logits, weights):
    probs = torch.zeros_like(list_logits[0].tensors[0])  # 1000 classes in Imagenet.
    labels = list_logits[0].tensors[1]
    for i in range(len(list_logits)):
        probs += weights[i] * F.softmax(list_logits[i].tensors[0], dim=1)
    logits = torch.log(probs)

    # Construct the dataset
    dataset_logits = tdata.TensorDataset(logits, labels)
    return dataset_logits

def LACPredictionSets(logits_cal, logits_val, alpha, bsz, temp_scale=True):
    # logits_cal, logits_val= split2(logits, n_data_conf, len(logits)-n_data_conf) # A new random split for every trial
    n_data_conf = len(logits_cal)
    # Prepare the loaders
    # ground truth locations
    gt_locs_cal = np.array([np.where(np.argsort(x[0]).flip(dims=(0,)) == x[1])[0][0] for x in logits_cal])
    gt_locs_val = np.array([np.where(np.argsort(x[0]).flip(dims=(0,)) == x[1])[0][0] for x in logits_val])
    # calibrate using a dummy conformal model
    loader_cal = torch.utils.data.DataLoader(logits_cal, batch_size=bsz, shuffle=False, pin_memory=True)
    if temp_scale:
        conformal_model = ConformalModelLogits(None, loader_cal, alpha=alpha, naive=True, batch_size=bsz)
        T = conformal_model.T.item()
    else:
        T = 1.0
    scores_cal = 1-np.array([np.sort(torch.softmax(logits_cal[i][0]/T, dim=0))[::-1][gt_locs_cal[i]] for i in range(len(logits_cal))])
    scores_val = 1-np.array([np.sort(torch.softmax(logits_val[i][0]/T, dim=0))[::-1][gt_locs_val[i]] for i in range(len(logits_val))])
    q = np.quantile(scores_cal, np.ceil((n_data_conf+1) * (1-alpha))/n_data_conf)
    top1_avg = (gt_locs_val == 0).mean()
    top5_avg = (gt_locs_val < 5).mean()
    cvg_avg = ( scores_val < q).mean()
    sz_avg = np.array([ ( (1-torch.softmax(logits_val[i][0]/T, dim=0)) < q).sum() for i in range(len(logits_val)) ]).mean()
    return top1_avg, top5_avg, cvg_avg, sz_avg


"""TensorFlow utilities and data structures for rapid prototyping.

This module is inspired by:
.. _Structuring Your TensorFlow Models by Danijar Hafner / CC BY-SA 3.0:
   https://danijar.com/structuring-your-tensorflow-models/
.. _Patterns for Fast Prototyping with TensorFlow by Danijar Hafner / CC BY-SA 3.0:
   https://danijar.com/patterns-for-fast-prototyping-with-tensorflow/
"""

__license__ = "MIT License"
__copyright__ = "Copyright (c) 2019 Piotr Januszewski"

import functools

class AttrDict(dict):
    """Defines an attribute dictionary that allows the user to address keys as
    if they were attributes.

    `AttrDict` inherits from `dict`, so it can be used everywhere where `dict` is expected.
    The __init__ constructs an empty `AttrDict` or recursively initialises an `AttrDict`
    from a (maybe nested) `dict`.

    Args:
        other (dict): Either dictionary (can be nested) to initialise an `AttrDict` from
        or None to construct an empty `AttrDict`.
    """

    def __init__(self, other=None):
        super().__init__()

        if other is not None:
            for key, value in other.items():
                # It is easier to ask for forgiveness, than to ask for permission
                try:
                    self[key] = AttrDict(value)
                except AttributeError:
                    self[key] = value

    def nested_update(self, other):
        """Updates the values in this and the nested `AttrDict`s.

        Args:
            other (dict): Dictionary (can be nested) to update the `AttrDict` from.
        """

        for key, value in other.items():
            # It is easier to ask for forgiveness, than to ask for permission
            try:
                self[key].nested_update(value)
            except AttributeError:
                self[key] = value

        return self

    __getattr__ = dict.__getitem__
    __setattr__ = dict.__setitem__

def attrdict_from_json(data):
    """Constructs an AttrDict from JSON formatted string.

    Args:
        data (str): JSON formatted string.
    """

    from json import loads
    return AttrDict(loads(data))

def attrdict_from_yaml(data):
    """Constructs an AttrDict from YAML formatted string.

    Args:
        data (str): YAML formatted string.
    """

    from yaml import safe_load
    return AttrDict(safe_load(data))

def lazy_property_with_scope(*args, scope_name=None, **kwargs):
    """Property decorator on steroids.

    Property decorator which additionally defines TensorFlow variable scope
    and adds lazy loading.

    This decorator can be used without parentheses if no arguments are provided.

    Args:
        scope_name (str): The scope name. If `None` decorated function's name is
                          used as the scope name.
        *args: Passed to `tf.variable_scope`.
        **kwargs: Passed to `tf.variable_scope`.
    """

    def define_scope(function, scope_name, *args, **kwargs):
        """Decorator for properties that define TensorFlow operations.

        The decorated function will only be executed once. Subsequent calls to
        it will directly return the result so that operations are added to
        the graph only once. The operations added by the function live within
        a tf.variable_scope(). If this decorator is used with arguments, they
        will be forwarded to the variable scope. The scope name defaults to
        the name of the decorated function.

        Args:
            scope_name (str): The scope name. If `None` decorated function's
                              name is used as the scope name.
            *args: Passed to `tf.variable_scope`.
            **kwargs: Passed to `tf.variable_scope`.
        """

        from tensorflow import variable_scope

        attribute = '_cache_' + function.__name__
        name = scope_name or function.__name__

        @property
        @functools.wraps(function)
        def decorator(self):
            if not hasattr(self, attribute):
                with variable_scope(name, *args, **kwargs):
                    setattr(self, attribute, function(self))
            return getattr(self, attribute)
        return decorator

    if len(args) == 1 and len(kwargs) == 0 and callable(args[0]):
        return define_scope(args[0], scope_name=scope_name)
    return lambda function: define_scope(function, scope_name=scope_name,
                                         *args, **kwargs)

def share_variables(function):
    """Decorator for methods that define TensorFlow operations.

    The decorated method is wrapped with tf.make_template() so that it does
    variable sharing. The tf.make_template() will only be executed once.
    Subsequent calls to the decorated function will reuse the template so that
    operations are added to the graph only once.
    """

    from tensorflow import make_template

    attribute = '_cache_' + function.__name__

    @functools.wraps(function)
    def decorator(self, *args, **kwargs):
        if not hasattr(self, attribute):
            setattr(self, attribute,
                    make_template(function.__name__,
                                  function,
                                  create_scope_now_=True))
        return getattr(self, attribute)(self, *args, **kwargs)
    return decorator


# PyTorch imports
import torch
from torch.utils.data import Dataset, DataLoader, Subset
from torch.utils.data import random_split
import torchvision
from torchvision import transforms

# python imports
import os
import numpy as np

# additional libraries
import pytorch_lightning as pl

# custom imports
from data_management.data_tools import DefaultDataset

class FashionMNISTDataModule(pl.LightningDataModule):
    """PyTorch-Lightning datamodule class for the FashionMNIST dataset."""
    def __init__(self, data_dir, batch_size, Nsamples=None, valid_frac=None,
                 random_state=42, transform=None, test_transform=None ):
        super().__init__()
        self.data_dir   = data_dir      # directory to contain this and other eventual datasets
        self.batch_size = batch_size    # training batch size
        self.Nsamples   = Nsamples      # number of samples to load from the train dataset
        self.frac       = valid_frac    # fraction of samples to use as validation set
        self.seed       = random_state

        # set up transformations
        if transform is None:
            self.transform = transforms.ToTensor()
        else:
            self.transform = transform

        if test_transform is None:
            self.test_transform = transforms.ToTensor()
        else:
            self.test_transform = test_transform

        ### label names
        self.label_names = ['T-shirt/top','Trouser','Pullover','Dress','Coat'      ,
                            'Sandal'     ,'Shirt'  ,'Sneaker' ,'Bag'  ,'Ankle boot']

    def prepare_data(self):
        ### train dataset
        self.full = torchvision.datasets.FashionMNIST(self.data_dir,
                                                      train    = True,
                                                      download = True, ##
                                                     )
        ### test dataset
        self.test_data = torchvision.datasets.FashionMNIST(self.data_dir,
                                                           train     = False,
                                                           download  = True, ##
                                                           transform = self.test_transform,
                                                          )

    def setup(self, stage=None):
        # eventually restricting to Nsamples
        if self.Nsamples is not None:
            self.full = Subset(self.full, np.random.choice(range(self.full.__len__()), size=self.Nsamples))

        # split into train and validation
        split_tr  = round( self.full.__len__()*(1-self.frac) )
        split_val = round( self.full.__len__()*self.frac )

        train, val = random_split(self.full,
                                  [split_tr, split_val],
                                  generator=torch.Generator().manual_seed(self.seed),
                                 )
        # add transforms
        self.train_data = DefaultDataset(data=train, transform=self.transform     )
        self.val_data   = DefaultDataset(data=val  , transform=self.test_transform)

    def train_dataloader(self):
        return DataLoader(self.train_data,
                          batch_size = self.batch_size,
                          shuffle    = True,
                          pin_memory = True,
                         )

    def val_dataloader(self):
        return DataLoader(self.val_data,
                          batch_size = self.batch_size,
                          shuffle    = False,
                          pin_memory = True,
                         )

    def test_dataloader(self):
        return DataLoader(self.test_data,
                          batch_size = 500, #self.batch_size,
                          shuffle    = False,
                          pin_memory = True,
                         )

    def predict_dataloader(self):
        return DataLoader(self.test_data,
                          batch_size = 500, #self.batch_size,
                          shuffle    = False,
                          pin_memory = True,
                         )

    def get_label_names(self):
        return self.label_names

    def get_sample_size(self):
        return torch.Size([1,28,28])


"""Utils for Zinnia's tests"""
import cStringIO
from xmlrpclib import Transport

from django.test.client import Client

class TestTransport(Transport):
    """Handles connections to XML-RPC server
    through Django test client."""

    def __init__(self, *args, **kwargs):
        Transport.__init__(self, *args, **kwargs)
        self.client = Client()

    def request(self, host, handler, request_body, verbose=0):
        self.verbose = verbose
        response = self.client.post(handler,
                                    request_body,
                                    content_type="text/xml")
        res = cStringIO.StringIO(response.content)
        res.seek(0)
        return self.parse_response(res)


from . import Entity
from PIL import Image
import pygame

class Boss(Entity):
    def __init__(self, pos):
        self.alive = True
        super().__init__(pos)
        self.img = Image.open('assets/animations/boss.png')
        self.img = self.img.resize((self.img.width*4, self.img.height*4), 0)
        self.img.save('AAA.png')
        self.imgs = pygame.image.load('AAA.png')

    def update(self, player):
        if player.bar >= 800:
            self.alive = False
            player.bar = 0

    def draw(self, win):
        win.blit(self.imgs, (self.x-(self.img.width//2),
                 self.y-(self.img.height//2)))


import numpy as np
import os
import sys
sys.path.append('~/utils')
from glob import glob
import h5py

from sketching_piano_expression.utils.parse_utils import *

def make_onset_based_all(x_data, out, same_onset_ind=None):
    '''
    get all notes in each onset
    '''
    start, end = same_onset_ind
    same_onset = np.argmax(x_data[:,start:end], axis=-1)
    new_out = list()
    is_onset = [out[0]]
    for i in range(1, x_data.shape[0]):
        o = same_onset[i]
        if o == 0:
            new_out.append(is_onset)
            is_onset = [out[i]]
        elif o == 1:
            is_onset.append(out[i])
    new_out.append(is_onset)
    return new_out

def make_onset_based_pick(x_data, out, same_onset_ind=None):
    '''
    get only the lowest note for each onset
    '''
    start, end = same_onset_ind
    same_onset = np.argmax(x_data[:,start:end], axis=-1)
    new_out = list()
    for i in range(x_data.shape[0]):
        o = same_onset[i]
        if o == 0:
            new_out.append(out[i])
        elif o == 1:
            continue
    return np.asarray(new_out)

def make_align_matrix(x_data, roll, same_onset_ind=None, rev=False):
    align_mat = np.zeros([x_data.shape[0], roll.shape[0]])
    start, end = same_onset_ind
    same_onset = np.argmax(x_data[:,start:end], axis=-1)
    ind = -1
    if rev == False:
        for i in range(x_data.shape[0]):
            o = same_onset[i]
            if o == 0: # if not same onset:
                ind += 1
                align_mat[i, ind] = 1
            elif o == 1:
                align_mat[i, ind] = 1
        # print(ind)
    elif rev == True:
        prev_o = 0
        for i in reversed(range(x_data.shape[0])):
            o = same_onset[i]
            if o == 1:
                if prev_o == 0: # diff onset
                    ind += 1
                    align_mat[i, ind] = 1
                    # print("o: 1 / prev_o: 0 --> {}th".format(i))
                elif prev_o == 1: # same onset
                    align_mat[i, ind] = 1
            elif o == 0:
                if prev_o == 0: # diff onset
                    ind += 1
                    align_mat[i, ind] = 1
                    # print("o: 0 / prev_o: 0 --> {}th".format(i))
                elif prev_o == 1: # same onset
                    align_mat[i, ind] = 1
            prev_o = o
        align_mat = np.flip(align_mat, 0)
    return align_mat.astype(np.float32)

def make_notenum_onehot(m_data):
    '''
    shape = [note-num, onset-num]
    '''
    note_num_seq = np.sum(m_data, axis=0)
    seq_expand = np.matmul(note_num_seq, m_data.T)
    # make onehot
    notenum_onehot = np.zeros([m_data.shape[0], 11])
    for i, s in enumerate(seq_expand):
        assert s > 0
        notenum_onehot[i,int(s-1)] = 1

    return notenum_onehot

def get_vertical_position(x_data, same_onset_ind=None):
    v_pos = np.zeros([len(x_data), 11])
    start, end = same_onset_ind
    for i, d in enumerate(x_data):
        same_onset = np.argmax(d[start:end])
        if same_onset == 0:
            pos = 0
            v_pos[i, pos] = 1
        elif same_onset == 1:
            pos += 1
            v_pos[i, pos] = 1
    return v_pos

def make_pianoroll_x(x_data, same_onset_ind=None):
    roll = np.zeros([88,len(x_data)])
    start, end = same_onset_ind
    col = -1
    for i, d in enumerate(x_data):
        pitch = np.argmax(d[start-88:start])
        same_onset = np.argmax(d[start:end])
        if same_onset == 0: # start
            col += 1
            roll[pitch,col] = 1
            # print(i)
        elif same_onset == 1: # cont
            roll[pitch,col] = 1
    # erase empty rolls
    for j in reversed(range(len(x_data))):
        if np.sum(roll[:,j]) > 0:
            break
    roll = np.transpose(roll[:,:j+1])
    return roll

def note_to_onset_ind(inp, same_onset_ind):
    onset_ind = list()
    start, end = same_onset_ind
    for n, each_note in enumerate(inp):
        if np.argmax(each_note[start:end]) == 0: # same onset feature
            onset_ind.append(n)
    onset_ind.append(n+1)
    assert onset_ind[-1] == len(inp)
    return onset_ind

def main(dirname, same_onset_ind=[110,112]):
    print("Saving batches...")

    # dirname = './features'
    groups = sorted(glob(os.path.join(dirname, "*/"))) # train/val/test
    maxlen, hop = 16, 4

    all_batches = list()
    all_num = 0
    for group in groups: # train/val/test
        datapath = os.path.join(group, 'raw')
        savepath = os.path.join(group, 'batch')
        if not os.path.exists(savepath):
            os.makedirs(savepath)
        categs = sorted(glob(os.path.join(datapath, '*/')))

        for categ in categs:
            c_name = categ.split('/')[-2]
            pieces = sorted(glob(os.path.join(categ, '*/')))

            for piece in pieces:
                p_name = piece.split('/')[-2]
                cond_ = np.load(os.path.join(piece, 'cond.npy'), allow_pickle=True) # xml
                cond_ = sorted(cond_, key=lambda x: x[0]) # same order with score
                tempo = cond_[0][1][0] # BPM
                inps = sorted(glob(os.path.join(piece, 'inp.*.npy'))) # score
                oups = sorted(glob(os.path.join(piece, 'oup.*.npy'))) # perform
                if len(inps) < 4:
                    print(c_name, p_name, len(inps), tempo)
                    all_num += len(inps)

                for inp_path, oup_path in zip(inps, oups):

                    pl_name = os.path.basename(oup_path).split('.')[-2]
                    inp_ = np.load(inp_path, allow_pickle=True)
                    oup_ = np.load(oup_path, allow_pickle=True)
                    inp_ = sorted(inp_, key=lambda x: x[0])
                    oup_ = sorted(oup_, key=lambda x: x[0]) # same order with score midi
                    if not len(inp_) == len(oup_) == len(cond_):
                        print(c_name, p_name, pl_name)
                        raise AssertionError

                    # augmentation
                    '''
                    cond: tempo(1), beat(12), dynamics(6), staff(2), downbeat(2),
                        time_sig(num)(12), time_sig(denom)(12), key_sig(12)
                    '''
                    cond = np.asarray([c[1] for c in cond_])
                    inp = np.asarray([i[1] for i in inp_])
                    t_aug = [1]
                    d_aug = [1]

                    print("{}: {}: {}".format(c_name, p_name, pl_name), end="\r")

                    for t in t_aug: # augment tempo
                        for d in d_aug: # augment dynamics
                            in_batch = inp
                            out_batch = np.asarray([
                                [o[1][0]*d, o[1][1]*t,
                                o[1][2]*t, o[1][3]*t] for o in oup_]) # , o[1][4]*t

                            onset_ind = note_to_onset_ind(in_batch, same_onset_ind=same_onset_ind)
                            t_ind = ind2str(int(t*100), 3)
                            d_ind = ind2str(int(d*100), 3)

                            # save batch
                            num = 1
                            for b in range(0, len(onset_ind)-maxlen, hop): # onset-based

                                b_ind = ind2str(num, 4)

                                # onset_based
                                onset_in_range = onset_ind[b:b+maxlen+1]
                                minind = onset_in_range[0]
                                maxind = onset_in_range[-1]

                                in_ = in_batch[minind:maxind]
                                out_ = out_batch[minind:maxind]
                                con_ = cond[minind:maxind]

                                in2_ = make_pianoroll_x(in_, same_onset_ind=same_onset_ind)
                                if len(in2_) < 4:
                                    continue
                                in3_ = make_align_matrix(in_, in2_, same_onset_ind=same_onset_ind) # note2chord
                                in4_ = make_notenum_onehot(in3_) # NumInChord
                                in5_ = get_vertical_position(in_, same_onset_ind=same_onset_ind) # PositionInChord

                                assert in_.shape[0] == in3_.shape[0] == in4_.shape[0]
                                assert in2_.shape[0] == in3_.shape[1]

                                in_ = np.concatenate([in_, in4_, in5_], axis=-1)

                                # save batch
                                savename_x = os.path.join(savepath, '{}.{}.{}.batch_x.{}.t{}_d{}.npy'.format(
                                    c_name.lower(), p_name.lower(), pl_name, b_ind, t_ind, d_ind))
                                savename_m = os.path.join(savepath, '{}.{}.{}.batch_m.{}.t{}_d{}.npy'.format(
                                    c_name.lower(), p_name.lower(), pl_name, b_ind, t_ind, d_ind))
                                savename_y = os.path.join(savepath, '{}.{}.{}.batch_y.{}.t{}_d{}.npy'.format(
                                    c_name.lower(), p_name.lower(), pl_name, b_ind, t_ind, d_ind))
                                savename_c = os.path.join(savepath, '{}.{}.{}.batch_cond.{}.t{}_d{}.npy'.format(
                                    c_name.lower(), p_name.lower(), pl_name, b_ind, t_ind, d_ind))

                                assert len(in_) == len(out_) == len(con_)

                                np.save(savename_x, in_)
                                np.save(savename_m, in3_)
                                np.save(savename_y, out_)
                                np.save(savename_c, con_)

                                print("saved batches for {} {}: player {} (aug: {}/{}) --> inp{} / oup{} / cond{}.     ".format(
                                    c_name, p_name, pl_name, t, d, in_.shape, out_.shape, con_.shape), end='\r')
                                num += 1
                        print("saved batches for {} {}: player {}".format(c_name, p_name, pl_name))

def corrupt_to_onset(x, y, stat=np.mean, same_onset_ind=None):
    y_onset = make_onset_based_all(x, y, same_onset_ind=same_onset_ind)
    y_onset_ = list()
    for each_onset in y_onset:
        each = np.asarray(each_onset)
        _features = stat(each, axis=0)
        y_onset_.append(_features)
    return np.asarray(y_onset_)

def create_h5_dataset(dataset='train', savepath='./data/data_samples'): # save npy files into one hdf5 dataset
    batch_path = "./data/data_samples/features/{}/batch".format(dataset)
    # load filenames
    x_path = [np.string_(x) for x in sorted(glob(os.path.join(batch_path, "*.batch_x.*.t100_d100.npy")))]
    m_path = [np.string_(m) for m in sorted(glob(os.path.join(batch_path, "*.batch_m.*.t100_d100.npy")))]
    y_path = [np.string_(y) for y in sorted(glob(os.path.join(batch_path, "*.batch_y.*.t100_d100.npy")))]
    c_path = [np.string_(c) for c in sorted(glob(os.path.join(batch_path, "*.batch_cond.*.t100_d100.npy")))]

    # save h5py dataset
    f = h5py.File(os.path.join(savepath, "{}.h5".format(dataset)), "w")
    dt = h5py.special_dtype(vlen=str) # save data as string type
    f.create_dataset("x", data=x_path, dtype=dt)
    f.create_dataset("m", data=m_path, dtype=dt)
    f.create_dataset("y", data=y_path, dtype=dt)
    f.create_dataset("c", data=c_path, dtype=dt)
    f.close()

if __name__ == "__main__":
    import logging, sys, argparse
    logging.basicConfig(stream=sys.stdout, level=logging.INFO)
    logger = logging

    parser = argparse.ArgumentParser(description='Process some integers.')
    parser.add_argument('--input_dir', type=str, default='./data/data_samples/features',
                        help='input directory (subdirectories are train/val/test)')

    args = parser.parse_args()

    main(dirname=args.input_dir)


from tensorflow import keras
from keras.preprocessing.image import ImageDataGenerator, load_img,img_to_array
from keras.models import Sequential, Model
from keras.layers import Dense,Flatten,Dropout,Input
from keras.layers.convolutional import Conv2D,MaxPooling2D
import keras.layers as layers
from keras.applications import MobileNetV2
from keras.applications.mobilenet_v2 import decode_predictions, preprocess_input
from keras.models import load_model
from keras import backend as K
import cv2
import numpy as np

train_datagen = ImageDataGenerator(
    rescale=1.0/255.0,
    shear_range = 0.3,
    zoom_range = 0.3,
    horizontal_flip=True,
    preprocessing_function=preprocess_input
)

train_flow = train_datagen.flow_from_directory(
    './data/train/', #replace this path to your own dataset
    target_size=(224,224),
    batch_size=32,
    class_mode='categorical'
)

test_datagen = ImageDataGenerator(
    rescale=1.0/255.0,
    horizontal_flip=False
)

test_flow = test_datagen.flow_from_directory(
    './data/test/',
    target_size=(224,224),
    batch_size=32,
    class_mode='categorical'
)

conv_base = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
conv_base.trainable = False
model = keras.Sequential()
model.add(conv_base)
model.add(layers.GlobalAveragePooling2D())
model.add(layers.Flatten())
model.add(Dropout(0.25))

model.add(layers.Dense(100, activation='relu'))
model.add(layers.BatchNormalization())
model.add(layers.Dense(52, activation='relu'))
model.add(layers.BatchNormalization())
model.add(layers.Dense(26, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

model.fit_generator(train_flow, epochs=10)

model.save('./saved_model/MobileNetV2')
scores = model.evaluate(test_flow)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))


import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

path = "ex2.txt"

data = pd.read_csv(path,names=['size','bedrooms','price'])

#   特征归一化。 这个对于pandas来说很简单
data = (data - data.mean()) / data.std()
data.insert(0,'ones',1)
print(data.head())

cols = data.shape[1]
m = data.shape[0]
x = data.iloc[:,0:cols-1]
y = data.iloc[:,cols-1:cols]

def computeCost(x,y,theta):
    inner = np.power((x * theta.T - y), 2)
    return np.sum(inner) / (2 * m)

x = np.matrix(x)
y = np.matrix(y)
print(x.shape[1])
theta = np.matrix(np.zeros(x.shape[1]))  # theta就是一个（1,cols-1）矩阵
print(theta)

def gradientDescentMulti(x, y, theta, alpha, iters):
    temp = np.zeros(theta.shape[1])
    cost = np.zeros(iters)    #cost.shape = (iters)而不是（1，iters),是向量

    for i in range(iters):
        temp = theta - (alpha / m) * (x * theta.T - y).T * x
        theta = temp

        cost[i] = computeCost(x, y, theta)  #如果cost.shape=(1,iters),此处应为cost[0,i]

    return theta,cost

alpha = 0.01
iters = 1000
final_theta , cost = gradientDescentMulti(x,y,theta,alpha,iters)
final_cost = computeCost(x,y,final_theta)
print(f'final_theta={final_theta}')
print(f'final_cost={final_cost}')
print(len(cost))

fig, ax = plt.subplots(figsize=(8,5))
ax.plot(np.arange(iters), cost,'r')
ax.set_xlabel('Iterations')
ax.set_ylabel('Cost')
ax.set_title('Error vs. Training Epoch')
plt.show()


from langchain.embeddings import HuggingFaceEmbeddings
from langchain.retrievers import TimeWeightedVectorStoreRetriever
from langchain.vectorstores import FAISS
from langchain.docstore import InMemoryDocstore
from langchain.schema import Document
from typing import Any, Dict, List, Optional, Tuple
import faiss
import datetime
import os
import _pickle as cPickle
import bz2

#model_name = "intfloat/e5-base-v2"
model_name = "models/e5-base-v2"
model_kwargs = {'device': 'cpu'}
EMBEDDING_MODEL = HuggingFaceEmbeddings(
    model_name=model_name,
    model_kwargs=model_kwargs)
# Initialize the vectorstore as empty
embedding_size = 768

def _get_hours_passed(time: datetime, ref_time: datetime) -> float:
    """Get the hours passed between two datetime objects."""
    return (time - ref_time).total_seconds() / 3600

class TimeWeightedVectorStoreRetriever_custom(TimeWeightedVectorStoreRetriever):
    def _get_combined_score(
            self,
            document: Document,
            vector_relevance: Optional[float],
            current_time: datetime,
        ) -> float:
        """Return the combined score for a document."""
        hours_passed = _get_hours_passed(
            current_time,
            document.metadata["last_accessed_at"],
        )
        #Note: We can change the above 'last_accessed_at' above to 'created_at' to rank memory based on when it was created (rather than when it was last accessed in the Langchain default implementation)
        #https://github.com/hwchase17/langchain/blob/85dae78548ed0c11db06e9154c7eb4236a1ee246/langchain/retrievers/time_weighted_retriever.py#L119

        score = (1.0 - self.decay_rate) ** hours_passed
        # print(f'score contributed by time: {score}')
        for key in self.other_score_keys:
            if key in document.metadata:
                if key != 'importance':
                  score += document.metadata[key]
                else:
                  score += int(document.metadata[key])/10.
                #   print(f'score contributed by importance: {int(document.metadata[key])/10.}')

        if vector_relevance is not None:
            score += vector_relevance
            # print(f'score contributed by vector relevance: {vector_relevance}')

        # print(f'total score: {score}')
        # print('------------')
        return score

def prepare_memory_object(timestamp, lastAccess, vector, importance):
  memoryObject = {
        "timestamp": timestamp,
        "lastAccess": lastAccess,
        "vector": vector,
        "importance": importance
    }
  return memoryObject

def initialize_vector_memory():
  index = faiss.IndexFlatL2(embedding_size)
  vectorstore = FAISS(EMBEDDING_MODEL.embed_query, index, InMemoryDocstore({}), {})
  retriever = TimeWeightedVectorStoreRetriever_custom(vectorstore=vectorstore, other_score_keys = ['importance'] , decay_rate=.01, k=1)
  return retriever

def load_vector_memory(root = '/content/vector_mems', mem_name = 'dialogue'):
  path = os.path.join(root, mem_name) + '.pbz2'
  assert os.path.exists(path)
  data = bz2.BZ2File(path, "rb")
  retriever = cPickle.load(data)
  return retriever

def save_vector_memory(retriever, root = '/content/vector_mems', mem_name = 'dialogue'):
  path = os.path.join(root, mem_name) + '.pbz2'
  with bz2.BZ2File(path, "w") as f:
    cPickle.dump(retriever, f)
  return

def retrieve(query, retriever, top_k = 3):
  retriever.k = top_k
  retrieved_docs = retriever.get_relevant_documents(query)
  retrieved_docs_str = [('- ' + doc.page_content) for doc in retrieved_docs]
  return '\n'.join(retrieved_docs_str)