# -*- coding: utf-8 -*-
import numpy as np
from abc import ABCMeta, abstractmethod
from sklearn.utils.extmath import randomized_svd
from sklearn.metrics.pairwise import rbf_kernel
from sklearn.model_selection import KFold
from sklearn.metrics.pairwise import euclidean_distances

class DRChangeRateEstimator(object):
    """ density ratio change rate estimator
    """
    MEAN_OPTION = {
        "feature_extraction": {
            "name": "mean",
            "param": {}
        }
    }
    SVD_OPTION = {
        "feature_extraction": {
            "name": "svd",
            "param": {
                "n_components": 2,
                "n_iter": 5
            }
        }
    }
    RuLSIF_OPTION = {
        "param": {
            "lambda": 0.1,
            "beta": 0.2,
            "gamma": 0.1,
            "validate": "none"
        }
    }

    def __init__(self,
                 sliding_window=3,
                 pside_len=4,
                 cside_len=4,
                 mergin=0,
                 trow_offset=0,
                 tcol_offset=0):
        """ init
        Args:
            sliding_window: int:
            pside_len: int(>0): past side length
            cside_len: int(>0): current side length
            mergin: int(<pside_len):
                mergin btween past side and current side
            offset: int(0=<t_offset<=cside_len): current time offset
        """
        self.sliding_window = sliding_window
        self.pside_len = pside_len
        self.cside_len = cside_len
        self.mergin = mergin
        self.trow_offset = trow_offset
        self.tcol_offset = tcol_offset
        self._estimation_method = None

    def build(self,
              estimation_method="von_mises_fisher",
              options=MEAN_OPTION):
        """ build
        Args:
            estimation_method: string:
                "von_mises_fisher":
                "RuLSIFitting":
        """
        if estimation_method == "von_mises_fisher":
            # set feature extractor
            extractor_options = options.get("feature_extraction", dict())
            name = extractor_options.get("name")
            params = extractor_options.get("param")
            if name == "mean":
                extractor = MeanVectorExtractor()
            elif name == "svd":
                extractor = SVDExtractor(
                    n_components=params["n_components"],
                    n_iter=params["n_iter"]
                )
            else:
                raise TypeError("unknown extractor {}".format(name))
            # set estimation method
            self._estimation_method = VonMisesFisherFitting(extractor)

        elif estimation_method == "RuLSIFitting":
            # set estimation method
            params = options.get("param")
            self._estimation_method = RuLSIFitting(
                params["gamma"], params["lambda"], params["beta"],
                params["validate"]
            )

        else:
            raise TypeError("unknown method name {}".format(estimation_method))

    def transform(self, X, destination="forward_backward"):
        """ transform
        Args:
            X: numpy array 1D: time series or other 1D features
               or
               numpy array 2D: time series subsequence or other 2D features
            destination: string:
                "forward":
                "backward":
                "forward_backward":
        Returns:
            change_rates: numpy array 1D:
        """
        nT = len(X)
        # check input data shape and convert them to 2D if they are 1D
        if len(X.shape) == 1:
            X = self.get_subsequence(X, self.sliding_window)
        elif len(X.shape) > 2:
            raise ValueError("input data shape should be 1 or 2d.")

        if X.shape[1] != self.sliding_window:
            raise ValueError("input shape does not match sliding window.")

        change_rates = np.zeros(nT)
        for t in xrange(nT):
            pX, cX = self._get_pcX(t, X)
            if pX is None or cX is None:
                change_rates[t] = None
            else:
                if destination == "forward":
                    change_rates[t] = self._estimation_method(pX, cX)
                elif destination == "backward":
                    if t+1 < nT:
                        change_rates[t+1] = self._estimation_method(cX, pX)
                else:
                    change_rates[t] += self._estimation_method(pX, cX)/2.0
                    if t+1 < nT:
                        change_rates[t+1] += self._estimation_method(cX, pX)/2.0

        return change_rates

    def _get_pcX(self, t, X):
        """ get past and current side X
        Args:
            t: int: time index of X
            X: numpy array 2D:
        Returns:
            pX: numpy array 2D:
            cX: numpy array 2D:
        """
        cindex = t - self.tcol_offset - self.trow_offset
        if cindex + self.cside_len > len(X) or cindex < 0:
            cX = None
        else:
            cX = X[cindex:cindex+self.cside_len]
        pindex = cindex - self.pside_len - self.mergin
        if pindex < 0:
            pX = None
        else:
            pX = X[pindex:pindex+self.pside_len]
        return pX, cX

    @staticmethod
    def get_subsequence(X, m):
        """ get subsequence
        Args:
            X: numpy array 1D:
            m: int: sliding_window
        Returns:
            subsequences: numpy array 2D:
        """
        n = len(X) - m + 1
        subsequences = np.zeros((n, m))
        for i in xrange(n):
            subsequences[i] = X[i:i+m]
        return subsequences

class VonMisesFisherFitting(object):
    """ Von Mises Fiser Method Fitting Class
    """
    def __init__(self, extractor):
        """ init
        """
        self._feature_extractor = extractor

    def __call__(self, pX, cX):
        """ call method
        Args:
            pX: numpy array 2D: past side subsequences
            cX: numpy array 2D: current side subsequences
        """
        return self.estimate(pX, cX)

    def estimate(self, pX, cX):
        """ change rate estimation method by using von mises fisher
            distribution
        Args:
            pX: numpy array 2D: past side subsequences
            cX: numpy array 2D: current side subsequences
        """
        # feature extraction from 2D matrix
        if self._feature_extractor is None:
            raise TypeError("Feature extractor is not assigned.")
        u = self._feature_extractor.extract(pX)
        q = self._feature_extractor.extract(cX)
        if len(u.shape) > 1:
            return (1. - np.linalg.norm((u.T).dot(q), 2))
        else:
            return (1. - (u.T).dot(q))

class RuLSIFitting(object):
    """ relative unconstrained least-squares importance fitting
        Reference
            * Anomaly Detection and Change Detection,
              T.Ide and M.Sugiyama, Kodansha Ltd.
            * Change-point detection in time-series data by relative
              density-ratio estimation, Song Liu, et.al.
              Neural Networks, volume 43, July 2013, Pages 72-83
    """
    def __init__(self, gamma, lam, beta, validate="none"):
        """ init
        Args:
            gamma: float: parameter of rbf kernel
            lam: float: parameter of regularization
            beta: float: parameter of relative pearson divergence (0~1)
            validate: string: times of validation
                "every": validate every change rate
                "once": validate only once data set
                "none": no validation (use initial parameters)
        """
        self.gamma = gamma
        self.lam = lam
        self.beta = beta
        self.validate = validate
        self.is_validated = False

        self.nfold = 5
        self.list_lambda = [
            0.0001, 0.001, 0.1, 1.0, 10.0
        ]
        self.list_sigma = [
            0.6, 0.8, 1.0, 1.2, 1.4
        ]

    def get_gammas(self, X, Y):
        """ get gammas
        Args:
            X: np array:
            Y: np array:
        Returns:
            gammas: np array:
                1/(2*sigma^2) where sigma is multipled by median distance
        """
        S = np.concatenate((X, Y))
        dists = euclidean_distances(S, S)
        med_dist = np.median(dists)
        sigmas = med_dist*np.array(self.list_sigma)
        gammas = np.array([1.0/(2.0*sigma*sigma) for sigma in sigmas])
        return gammas

    def __call__(self, pX, cX):
        """ call method
        Args:
            pX: numpy array 2D: past side subsequences
            cX: numpy array 2D: current side subsequences
        """
        return self.estimate(pX, cX)

    def estimate(self, pX, cX):
        """ change rate estimation method by using von mises fisher
            distribution
        Args:
            pX: numpy array 2D: past side subsequences
            cX: numpy array 2D: current side subsequences
        """
        # cross validation
        if(self.validate == "every" or
           (self.validate == "once" and self.is_validated is False)):
            list_gamma = self.get_gammas(pX, cX)
            self.cross_validation(
                pX, cX, self.nfold, self.list_lambda, list_gamma
            )

        # estimate change rate
        phi_x = rbf_kernel(pX, pX, self.gamma)
        phi_y = rbf_kernel(pX, cX, self.gamma)
        theta = self.calc_theta(phi_x, phi_y, self.lam, self.beta)
        if theta is None:
            return None
        rPE = self.calc_pe(theta, phi_x, phi_y)

        return rPE

    def calc_theta(self, phi_x, phi_y, lam, beta):
        """ calculate theta
        Args:
            phi_x:
            phi_y:
            lam:
            beta:
        Returns:
            theta:
        """
        nx = phi_x.shape[1]
        ny = phi_y.shape[1]
        h = phi_x.mean(axis=1)
        G_1 = beta/nx*self._product_sum(phi_x, phi_x)
        G_2 = (1.0 - beta)/ny*self._product_sum(phi_y, phi_y)
        G_beta = G_1 + G_2 + lam*np.eye(len(G_1))

        if np.linalg.matrix_rank(G_beta) != len(G_beta):
            return None
        theta = np.linalg.solve(G_beta, h)
        return theta

    def calc_pe(self, theta, phi_x, phi_y):
        """ calculate pearson divergence
        Args:
            theta:
            phi_x:
            phi_y:
        Returns:
            :
        """
        g_x = theta.dot(phi_x)
        g_y = theta.dot(phi_y)
        term1 = -1.0*self.beta/2.0*(g_x**2).mean()
        term2 = -1.0*(1.0-self.beta)/2.0*(g_y**2).mean()
        term3 = g_x.mean()

        return term1 + term2 + term3 - 1.0/2.0

    def cross_validation(self,
                         X,
                         Y,
                         nfold,
                         list_lambda,
                         list_gamma,
                         use_flat=False):
        """ cross validation
        Args:
            X:
            Y:
            nfold:
            list_lambda:
            list_gamma:
        """
        kf = KFold(n_splits=nfold)
        if use_flat:
            nX = X.flatten()
            nY = Y.flatten()
        else:
            nX = X
            nY = Y

        cv_score = np.zeros((len(list_gamma), len(list_lambda)))
        for ig, test_gamma in enumerate(list_gamma):
            phi_x = rbf_kernel(nX, nX, test_gamma)
            phi_y = rbf_kernel(nX, nY, test_gamma)
            cv_score_k = np.zeros((nfold, len(list_lambda)))
            for k, (ix, iy) in enumerate(zip(kf.split(phi_x.T),
                                             kf.split(phi_y.T))):
                train_ix, test_ix = ix
                train_iy, test_iy = iy
                for il, test_lambda in enumerate(list_lambda):
                    theta_k = self.calc_theta(phi_x[:, train_ix],
                                              phi_y[:, train_iy],
                                              test_lambda,
                                              self.beta)
                    J_k = self.calc_pe(
                        theta_k, phi_x[:, test_ix], phi_y[:, test_iy]
                    )
                    cv_score_k[k, il] = J_k
            cv_score[ig] = cv_score_k.mean(axis=0)
        answer_gi = cv_score.argmin(axis=0)[0]
        answer_li = cv_score.argmin(axis=1)[0]
        self.gamma = list_gamma[answer_gi]
        self.lam = list_lambda[answer_li]
        self.is_validated = True

    @staticmethod
    def _product_sum(phi1, phi2):
        return phi1.dot(phi2.T)

class Extractor(object):
    """ Extractor Base
    """
    __metaclass__ = ABCMeta

    def __call__(self, X):
        """ call
        Args:
            X: numpy array 2D:
        """
        return self.extract(X)

    @abstractmethod
    def extract(self, X):
        """ extract
        Args:
            X: numpy array 2D:
        """
        pass

class MeanVectorExtractor(Extractor):
    """ Mean Vector Extractor Class
    """
    def extract(self, X):
        """ extract
        Args:
            X: numpy array 2D:
        """
        m = X.sum(axis=0)/len(X)
        return m/np.sqrt(m.dot(m))

class SVDExtractor(Extractor):
    """ Singlar Value Decomposition Extractor Class
    """
    def __init__(self, n_components=2, n_iter=5):
        """ init
        """
        self.n_components = n_components
        self.n_iter = n_iter

    def extract(self, X):
        """ extract
        Args:
            X: numpy array 2D:
        """
        U, Sigma, VT = randomized_svd(
            X, n_components=self.n_components, n_iter=self.n_iter
        )
        return U


import discord
from discord import app_commands
import random
import os

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

client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)

class MyClient(discord.Client):
    def __init__(self, *, intents: discord.Intents):
        super().__init__(intents=intents)
        self.tree = app_commands.CommandTree(self)

    async def setup_hook(self):
        await self.tree.sync()

intents = discord.Intents.default()
client = MyClient(intents=intents)

class ButtonView(discord.ui.View):
    @discord.ui.button(label='Start group roll!', style=discord.ButtonStyle.primary)
    async def roll(self, interaction: discord.Interaction, button: discord.ui.Button):
        final_message = await interaction.message.fetch()
        await rollEndFunc(final_message)
        button.disabled = True
        button.label = "Roll Complete!"
        button.style = discord.ButtonStyle.secondary
        await interaction.response.edit_message(view=self)

def init_insults():
    insults = []
    f = open("insults.txt", "r+")
    for l in f:
        insults.append(l)
    return insults

def init_flirt():
    pickup = []
    f = open("pickuplines.txt", "r+")
    for l in f:
        pickup.append(l)
    return pickup

async def randFunc(startNum, endNum):
    text = random.randint(startNum, endNum)
    print(text)
    return text

async def rollEndFunc(message):
    users = [ user async for user in message.reactions[0].users()]

    results = []
    for u in range(0, len(users)):
        if (users[u] != client.user):
            val = random.randint(0,99)
            results.append((users[u].mention, val))
            await message.channel.send(users[u].mention + " Rolled a " + str(val))

    winner = ("",0)
    for fin in range(0, len(results)):
        if (results[fin][1] > winner[1]):
            winner = results[fin]
    print("winner", winner)
    await message.channel.send(winner[0] + " Wins with a " + str(winner[1]))

async def rollFunc(message):
    parts = message.split('d')
    message = "You rolled: "
    print(parts)
    total = 0
    for i in range(int(parts[0])):
        result = random.randint(1, int(parts[1]))
        message += str(result)
        total += result
        if (i + 1 != int(parts[0])):
            message +=","

    if (int(parts[0]) > 1):
        message += "\n Total of {0}".format(total)
    return message

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.tree.command()
async def vm(interaction: discord.Interaction):
    """performs vicous mockery"""
    insult = insults[random.randint(0, len(insults))]
    await interaction.response.send_message(f'{insult}')

@client.tree.command()
async def flirt(interaction: discord.Interaction):
    """Provides a flirticious thing to say"""
    pickup_line = flirts[random.randint(0, len(flirts))]
    await interaction.response.send_message(f'{pickup_line}')

@client.tree.command(name="random")
@app_commands.describe(
    startnum='start value',
    endnum='end value',
)
async def randnum(interaction: discord.Interaction, startnum: int=0, endnum: int=100):
    """random number gen"""
    res = await randFunc(startnum, endnum)
    await interaction.response.send_message(res)

@client.tree.command()
@app_commands.describe(
    die="the amount of dice you want rolled. ie: 2d20, 1d6, 3d100"
)
async def roll(interaction: discord.Interaction, die: str='1d20'):
    """rolls given dice"""
    res = await rollFunc(die)
    await interaction.response.send_message(res)

@client.tree.command()
async def grouproll(interaction: discord.Interaction):
    await interaction.response.send_message("Here's your rolls", view = ButtonView())
    resp = await interaction.original_response()
    await resp.add_reaction("ðŸŽ²")

insults = init_insults()
flirts = init_flirt()
client.run(os.getenv('TOKEN'))


from rtlsdr import RtlSdr

#import matplotlib.pyplot as plt
import asyncio

import numpy as np

async def streaming():

    sdr = RtlSdr(serial_number='00000001')

    sdr.sample_rate = 2.048e6
    #sdr.center_freq = 462.5625e6
    sdr.freq_correction = -4
    sdr.gain = "auto"
    sdr.center_freq = 433.5e6

    sdr.set_bandwidth(500e3)

    async for samples in sdr.stream():
        print(samples)

    await sdr.stop()

    sdr.close()

loop = asyncio.get_event_loop()
loop.run_until_complete(streaming())

def print_data(data,obj):
    #print(data)
    for x in range(len(data)):
        I = np.real(data[x])
        Q = np.imag(data[x])
        P_dBm = 10 * np.log10(10 * (np.power(I,2) + np.power(Q,2)))
        if(x%100 == 0):
            print(P_dBm)


import os

from .ansible_runner import AnsibleRunner
from keywords.exceptions import ProvisioningError

from keywords.utils import log_info

import argparse

# Install aws credentials onto machine in /root/.aws/credentials file.
# Currently used by awslogs, which is a cloudwatch logs forwarder.
#
# Don't use full aws credentials, create a keypair on the existing mobiletestkit user
# which has full cloudwatch permissions, but nothing else

def install_aws_credentials(cluster_config, aws_access_key_id, aws_secret_access_key):

    log_info("Installing aws credentials for cluster_config: {}".format(cluster_config))

    ansible_runner = AnsibleRunner(config=cluster_config)

    status = ansible_runner.run_ansible_playbook(
        "install-aws-credentials.yml",
        extra_vars={
            "aws_access_key_id": aws_access_key_id,
            "aws_secret_access_key": aws_secret_access_key,
        },
    )
    if status != 0:
        raise ProvisioningError("Failed to aws credentials")

if __name__ == "__main__":

    usage = "usage: python install_aws_credentials.py --aws-access-key-id AKIAIYOURKEY --aws-secret-access-key uaLqkyoursecret"

    parser = argparse.ArgumentParser()
    parser.add_argument("--aws-access-key-id", help="aws access key id (use limited key, see comments in code)", required=True)
    parser.add_argument("--aws-secret-access-key", help="aws secret access key", required=True)
    args = parser.parse_args()

    try:
        cluster_conf = os.environ["CLUSTER_CONFIG"]
    except KeyError as ke:
        print("Make sure CLUSTER_CONFIG is defined and pointing to the configuration you would like to provision", ke)
        raise KeyError("CLUSTER_CONFIG not defined. Unable to provision cluster.")

    install_aws_credentials(cluster_conf, args.aws_access_key_id, args.aws_secret_access_key)


# -*- coding: utf-8 -*--
'''
#=============================================================================
#     FileName: Excel2Plist.py
#         Desc: simple create plist file from excel
#       Author: bugcode
#        Email: bugcodingt@gmail.com
#     HomePage: bugcoding.net
#      Version: 0.0.1
#   LastChange: 2014-11-14 14:52:53
#=============================================================================
'''
#引入操作excel model
import xlrd
import sys
import os
import types
import getopt

class Excel2Plist(object):
    #excel 目录
    __excel_dirs = ""
    #plist 目录
    __plist_dirs = ""
    #标准题头xml
    __xml_title_ = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
    #docType 声明
    __doc_type_declare = "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
    #plist 前置标签
    __plist_pre_label = "<plist version=\"1.0\">"
    #plist 封闭标签
    __plist_closed_label = "</plist>"
    #字典前置与封闭标签
    __dict_pre_label = "<dict>"
    __dict_closed_label = "</dict>"
    #key
    __key_pre_label = "<key>"
    __key_closed_label = "</key>"
    #string
    __string_pre_label = "<string>"
    __string_closed_label = "</string>"

    #确定excel在哪个目录，plist放到哪个目录
    def __init__(self, _excel_dirs = "./excel", _plist_dirs = "./plist"):
        self.__excel_dirs = _excel_dirs
        self.__plist_dirs = _plist_dirs

    #操作单个excel文件，转换为plist文件
    def transExcel2Plist(self, _excel_name, _plist_name):
        #读取excel文件数据，_excel_name 指定的文件
        _excel_data = xlrd.open_workbook(_excel_name)
        #读取不到直接返回
        if _excel_data is None :
            return
        #获取一个工作表，默认获取第0个工作表
        _excel_table = _excel_data.sheets()[0]
        if _excel_table is None :
            return
        #创建plist文件
        _plist_fp = open(_plist_name, "w")
        if _plist_fp is None :
            return
        #写入公共信息
        _plist_fp.write(self.__xml_title_ + "\n" + self.__doc_type_declare + "\n" + self.__plist_pre_label + "\n")
        #写入字典前置标签
        _plist_fp.write("\t" + self.__dict_pre_label + "\n")

        #写入数据
        _excel_rows = _excel_table.nrows
        _excel_cols = _excel_table.ncols
        if _excel_rows == 0 or _excel_cols == 0:
            _plist_fp.close()
            os.remove(_plist_name)
            return
        #第一行数据是用作key的，所以总数据是读取出来的行从第一行开始

        for i in range(1, _excel_rows):

            #第一个key，根据类型来区分
            _key_info = _excel_table.row(i)[0].value
            if type(_key_info) == types.FloatType:
                _key_info = str(int(_key_info))
            else:
                _key_info = str(_key_info)
            _plist_fp.write("\t\t" + self.__key_pre_label + _key_info + self.__key_closed_label + "\n")
            #key所属下面是一个dict
            _plist_fp.write("\t\t" + self.__dict_pre_label + "\n")

            #加入每一列对应的数据
            for j in range(0, _excel_cols):

                _data_info = _excel_table.row(i)[j].value

                #对应的数据信息，如果是中文，做encode处理
                if type(_data_info) == types.FloatType:
                    if _data_info == int(_data_info):
                        _data_info = str(int(_data_info))
                    else:
                        _data_info = str("%.3f" % _data_info)
                else:
                    _data_info = _data_info.encode('utf-8')
                #如果某一个列没有值，则这列不生成结点
                if _data_info is None or  _data_info == "":
                    continue

                #写入key值
                _plist_fp.write("\t\t\t" + self.__key_pre_label + (_excel_table.row(0)[j].value) + self.__key_closed_label + "\n")
                #写入string
                _plist_fp.write("\t\t\t" + self.__string_pre_label)

                #写入这列具体的数据
                _plist_fp.write(_data_info)
                _plist_fp.write(self.__string_closed_label + "\n")

            _plist_fp.write("\t\t" + self.__dict_closed_label + "\n")

        #写完数据写入字符封闭标签
        _plist_fp.write("\t" + self.__dict_closed_label + "\n")
        #写入plist封闭标签
        _plist_fp.write(self.__plist_closed_label + "\n")
        _plist_fp.close()

    #设置目录
    def setSourceDir(self, _src_excel_dir, _dst_plist_dir):
        self.__excel_dirs = self.getpwd() + os.sep + _src_excel_dir + os.sep
        if not os.path.exists(self.__excel_dirs):
            print ("\033[1;31;40m%s\033[0m" % "[not found [excel] directory on current directory!]")
            sys.exit(0)
        self.__plist_dirs = self.getpwd() + os.sep + _dst_plist_dir + os.sep
        if not os.path.exists(self.__plist_dirs):
            os.makedirs(self.__plist_dirs)

    def getpwd(self):
        if os.getcwd() == sys.path[0]:  # 说明是直接运行py
            return os.getcwd()
        else:  # 说明是pyinstaller1.5打包成exe的文件在运行
            return sys.path[1]

    def check_extend_name(self, file_name, *extend_name):
        array = map(file_name.endswith, extend_name)
        return any(array)

    #遍历整个文件夹
    def traverseAllExcel2Plist(self):
        #遍历指定的excel文件
        cnt = 0
        for file in os.listdir(self.__excel_dirs):
            if not os.path.isdir(file):
                if self.check_extend_name(file, "xls", "xlsx"):
                    excel_name = self.__excel_dirs + os.sep + file
                    plist_name = self.__plist_dirs + os.sep + file.split(".")[0] + ".plist"
                    cnt += 1
                    self.transExcel2Plist(excel_name, plist_name)
                    print("\033[1;34;36m%s\033[0m \033[1;32;38m%s\033[0m" % ("transform " , "["+file+"]" + " >>> completed"))
        if cnt != 0:
            print ("Success [" + str(cnt) + "]" + " plist")
        else:
            print ("not found xls or xlsx file!")

excel2plist = Excel2Plist()
excel2plist.setSourceDir("excel", "plist")
#excel2plist.transExcel2Plist("nationList.xls", "nationList.plist")
excel2plist.traverseAllExcel2Plist()


import random
from tkinter import CENTER
import numpy as np
import matplotlib.pyplot as plt
from cmath import exp

WAVE_LENGTH = 0.232924707
### preset antenna coordinate, in metres ###
# ant_1 = np.array((-0.375, 0))
# ant_2 = np.array((-0.125, 0))
# ant_3 = np.array((0.125, 0))
# ant_4 = np.array((0.375, 0))
# pt_1 = np.array((0, 2))
# pt_1 = np.array((-0.3, 2)) # initial starting coordinate

# cross antenna set
ant_1 = np.array((0.875, 0))
ant_2 = np.array((1.125, 0))
ant_3 = np.array((0, 0.875))
ant_4 = np.array((0, 1.125))
pt_1 = np.array((1, 1))

### figure initialization ###
colors = ['crimson', 'magenta', 'darkslateblue', 'violet', 'blue', 'steelblue', 'cyan', 'turquoise', 'lime', 'green', 'yellowgreen', 'yellow', 'gold', 'goldenrod', 'orangered', 'darkred', 'gray']

def generate_random_pt(centre, radius):
    # square space
    new_pt = np.array((centre[0] + random.uniform(-radius, radius), centre[1] + random.uniform(-radius, radius)))

    # circle space
    # random_radius = random.uniform(0, radius)
    # random_angle = random.uniform(0, 2 * np.pi)
    # new_pt = np.array((centre[0] +  random_radius * np.cos(random_angle), centre[1] + random_radius * np.sin(random_angle)))

    return new_pt

def get_phasor(pt_tag):
    eu_distance = np.array((np.linalg.norm(pt_tag - ant_1), np.linalg.norm(pt_tag - ant_2), np.linalg.norm(pt_tag - ant_3), np.linalg.norm(pt_tag - ant_4)))
    return eu_distance * 4 * np.pi / WAVE_LENGTH

def phasor_normalized(phasor):
    phasor_temp = []
    for i in range(len(phasor)):
        phasor_temp.append(exp(-1j*(phasor[i] - phasor[0])))
    # phasor_normalized = np.matrix(phasor_temp) # matrix version
    phasor_normalized = np.array(phasor_temp) # array version
    return phasor_normalized

def cosine_similarity(complex_vec_1, complex_vec_2):
    # num = np.linalg.norm(np.dot(complex_vec_1, complex_vec_2.getH())) # used in matrix version, same result
    num = np.linalg.norm(np.dot(complex_vec_1, complex_vec_2.conjugate().T)) # same using abs or norm
    # print(np.dot(complex_vec_1, complex_vec_2.conjugate().T), np.dot(complex_vec_1.conjugate().T, complex_vec_2)) # differnet in complex part, but same in norm/abs
    den = np.linalg.norm(complex_vec_1) * np.linalg.norm(complex_vec_2)
    return num / den

def plot_candidates(pt_current, radius, num_candidates):
    for i in range(num_candidates):
        pt_temp = np.array((pt_current[0] + radius * np.cos(i * 2 * np.pi / num_candidates), pt_current[1] + radius * np.sin(i * 2 * np.pi / num_candidates)))
        plt.scatter(pt_temp[0], pt_temp[1], c=colors[i], s=200)
    plt.scatter(pt_current[0], pt_current[1], c=colors[16], s=200)

def candidate_generator(pt_current, pt_measured, radius, num_candidates):
    candidates = []
    result = []
    phasor_measured = get_phasor(pt_measured)

    for i in range(num_candidates):
        pt_temp = np.array((pt_current[0] + radius * np.cos(i * 2 * np.pi / num_candidates), pt_current[1] + radius * np.sin(i * 2 * np.pi / num_candidates))) # circumference

        # pt_temp = generate_random_pt(pt_current, radius)

        phasor_temp = get_phasor(pt_temp)
        sim_temp = cosine_similarity(phasor_normalized(phasor_measured), phasor_normalized(phasor_temp))

        candidates.append(pt_temp)
        result.append(sim_temp)

    # add itself (circle centre) as a candidate other than the circle
    ### if randomly generate candidates, no need to add itself
    pt_temp = pt_current
    phasor_temp = get_phasor(pt_temp)
    sim_temp = cosine_similarity(phasor_normalized(phasor_measured), phasor_normalized(phasor_temp))

    candidates.append(pt_temp)
    result.append(sim_temp)

    # 1, only returning the candidate with the biggest similarity
    idx = result.index(max(result))
    plt.scatter(pt_measured[0], pt_measured[1], c=colors[idx], s=5)
    # return candidates[idx]

    # [not working well] 2, weighted sum of all
    # result_normalize = [x / sum(result) for x in result]
    # pt_new = np.array((0, 0))
    # for i in range(len(result_normalize)):
    #     # print(candidates[i] * result_normalize[i])
    #     pt_new = pt_new + candidates[i] * result_normalize[i]
    # return pt_new

    # 3, weighted sum of outstanding some of them, by threshold (0.9, 0.95, etc.)
    # new_result = []
    # new_candidates = []
    # for i in range(len(result)):
    #     if result[i] > 0.95:
    #         new_result.append(result[i])
    #         new_candidates.append(candidates[i])

    # result_normalize = [x / sum(new_result) for x in new_result]
    # pt_new = np.array((0, 0))
    # for i in range(len(result_normalize)):
    #     # print(candidates[i] * result_normalize[i])
    #     pt_new = pt_new + new_candidates[i] * result_normalize[i]

    # return pt_new

    # 4, weighted sum of outstanding 2 some of them (preset number)
    # new_result = []
    # new_candidates = []
    # new_idx = []
    # for _ in range(2):
    #     max_result = max(result)
    #     idx = result.index(max_result)
    #     new_result.append(result[idx])
    #     new_candidates.append(candidates[idx])
    #     result[idx] = 0
    #     new_idx.append(idx)

    # # for temp_idx in new_idx:
    # #     plt.scatter(candidates[temp_idx][0], candidates[temp_idx][1], c=colors[temp_idx], s=400)

    # result_normalize = [x / sum(new_result) for x in new_result]
    # pt_new = np.array((0, 0))
    # for i in range(len(result_normalize)):
    #     # print(candidates[i] * result_normalize[i])
    #     pt_new = pt_new + new_candidates[i] * result_normalize[i]
    # # plt.scatter(pt_new[0], pt_new[1], c='red', s=20, label='synthesis')
    # return pt_new

    # 5, weighted sum of all, add some preset/heuristic factors on weights

def plot_hotspot():
    plt.figure(figsize=(8,8))
    plot_candidates(pt_1, 0.25, 16)

    for _ in range(5000):
        new_pt = generate_random_pt(pt_1, 0.25)
        # print(new_pt)
        candidate_generator(pt_1, new_pt, 0.25, 16)

    # plt.show()
    plt.savefig('cross.png')

##################################
###### phasor differenciate ######
##################################

def candidate_generator_df(pt_current, pt_measured, radius, num_candidates):
    candidates = []
    result = []
    phasor_current = get_phasor(pt_current)
    phasor_measured = get_phasor(pt_measured)

    for i in range(num_candidates):
        pt_temp = np.array((pt_current[0] + radius * np.cos(i * 2 * np.pi / num_candidates), pt_current[1] + radius * np.sin(i * 2 * np.pi / num_candidates)))
        phasor_temp = get_phasor(pt_temp)

        # sim_temp = cosine_similarity(phasor_normalized(phasor_measured), phasor_normalized(phasor_temp)) # direct distance/phase
        sim_temp = cosine_similarity(phasor_normalized(phasor_measured - phasor_current), phasor_normalized(phasor_temp - phasor_current)) # differenciate

        # print('-------------------------------')
        # print(phasor_current)

        candidates.append(pt_temp)
        result.append(sim_temp)

    # add itself as a candidate, other than the circle, totally 9 candidates
    pt_temp = pt_current
    phasor_temp = get_phasor(pt_temp)
    sim_temp = cosine_similarity(phasor_normalized(phasor_measured - phasor_current), phasor_normalized(phasor_temp - phasor_current))

    candidates.append(pt_temp)
    result.append(sim_temp)

    idx = result.index(max(result))
    # plt.scatter(pt_measured[0], pt_measured[1], c=colors[idx], s=5)
    return candidates[idx]

def plot_hotspot_df():
    plt.figure(figsize=(8,8))
    plot_candidates(pt_1, 5, 16)

    for _ in range(5000):
        new_pt = generate_random_pt(pt_1, 5)
        # print(new_pt)
        candidate_generator_df(pt_1, new_pt, 5, 16)

    plt.show()

#################################
###### complete simulation ######
#################################

def func1(x):
    return x**2

def func2(x):
    return np.sin(x * np.pi)

def plot_antenna_position():
    plt.scatter(ant_1[0], ant_1[1], c='green', s=10)
    plt.scatter(ant_2[0], ant_2[1], c='green', s=10)
    plt.scatter(ant_3[0], ant_3[1], c='green', s=10)
    plt.scatter(ant_4[0], ant_4[1], c='green', s=10)

def generate_trajectory():
    # generate a trajectory and sample at certain time-stamp
    ### type 1 ###
    # y = np.linspace(0, 1, 50) # len = 50, range = (0, 40)
    # x = func1(y)

    ### type 2 ###
    # x = np.linspace(0, 1, 50)
    # y = func1(x)

    ### type 3 ###
    # x = np.linspace(0, 1, 50) # related to samples number
    # y = func2(x)

    ### type 4 ###
    y = np.linspace(0, 1, 50) # related to samples number
    x = func2(y)

    # introducing noise
    # for i in range(len(x)):
    #     # x[i] = x[i] + random.uniform(-0.05, 0.05)
    #     y[i] = y[i] + random.uniform(-0.02, 0.02)

    return x - 0.3, y + 1

def simulation_process():
    X, Y = generate_trajectory()
    plt.figure(figsize=(12,12))
    plt.plot(X, Y, c='steelblue')
    plt.scatter(X, Y, c='red', s=5)
    # plot_antenna_position()

    current_pt = pt_1

    candidate_traj_x = [X[0]]
    candidate_traj_y = [Y[0]]
    # print(X)
    # print(Y)

    for i in range(len(X)-1):
        measure_pt = np.array((X[i+1], Y[i+1]))
        # print(measure_pt)
        print('-------------------')

        if i == 0:
            df = np.array((0, 0))
        else:
            df = np.array((X[i]-X[i-1], Y[i]-Y[i-1]))
        print(df)

        df_size = np.linalg.norm(df)
        print(df_size) # difference in distance, actually unknown, only phase can be obtained

        previewed_pt = current_pt + df # 1-order preview, linear interpolation

        # r should be heuristic, estimate velocity, 2 * df_size

        # temp_pt = candidate_generator(current_pt, measure_pt, 2 * df_size, 100) # 100 particles, searching area is 0.05m circle in distance
        temp_pt = candidate_generator(previewed_pt, measure_pt, 2 * df_size, 100) # 100 particles, searching area is 0.05m circle in distance

        matching_bound_x = [temp_pt[0], measure_pt[0]]
        matching_bound_y = [temp_pt[1], measure_pt[1]]
        plt.plot(matching_bound_x, matching_bound_y)

        # update current position
        current_pt = temp_pt

        # record selected candidates trajectory
        candidate_traj_x.append(temp_pt[0])
        candidate_traj_y.append(temp_pt[1])

    plt.plot(candidate_traj_x, candidate_traj_y, c='blue')

    # plt.show()
    plt.savefig('sin1_no-preview.png')

###################################
def synthesis_scope():
    plt.figure(figsize=(10,10))
    temp_pt = generate_random_pt(pt_1, 5)
    plt.scatter(temp_pt[0], temp_pt[1], c='blue', s=20, label='current ground truth')

    # candidate_generator(pt_1, temp_pt, 5, 16)
    # plt.legend()

if __name__ == '__main__':
    plot_hotspot()

    # plot_hotspot_df()

    # simulation_process()

"""
It can be seen that multiple of them approach to 0.99
[0.994074525122763, 0.9998240079916879, 0.9370650565663665, 0.7441692049198252, 0.592632403841898, 0.6532177701212211, 0.8721051411923184, 0.9953489177496571, 0.908985062713027]
candidates 1 2 8 have big values, but actually closer to candidate 1, candidate 2 has the biggest similarity
---if updated by direct replacement, candidate 2 will be chosen, [  3.53553391 103.53553391]
---if updated by synthesis, weighted sum as,
------synthesis all of them， [ 0.95284506 99.66851705]
------synthesis some bigger layer of them, distingush by threshold 0.9 [  3.1514341  100.30761284], or top 3 [  4.02254225 100.00529292]
---------why 3: 2 for mutual compensation, 1 for bias, seems to be the best
------synthesis with bonus
Draw a hotspot graph, proofing the possibility is not equal to all candidates

Draw all the candidate, draw the complete curve plot on the complete simulation, inspect whether the strip-skipping effect will happen.

230612:
The 'Monte Carlo' version:
It seems better. However, the further distance will still influence the accuracy of the matching.
It can alleviate the strip-skipping effect to some degree, but still exist.
The parameter 'radius' should be big enough to cover the biggest speed of possible tag movement.
Use the best 2 of them and synthesis have a quite good performance.
Searching space should be circle but not square.
Todo:
Number of candidates/particles. Trade-off.
Heuristic radius. Prepared for the possible velocity variation. Too big, noise and fluctuation. Too small, unable to track.
If not real-time, try to reduce the updating rate of rfid reader.

Knowing direction, use linear-interpolated-previewed pf. Divdided into simple and complex trajectory.
"""


from wikidata.client import Client
import json
import csv
from collections import defaultdict
import numpy as np
import pickle

with open('country_dict.json') as inp:
    lines = inp.read()
country_dict = json.loads(lines)
#except:
#    country_dict = {}
#try:
with open('country_dict_r.json') as inp:
    lines = inp.read()
country_dict_r = json.loads(lines)

c2code = {}
code2c = {}
with open("data/wikipedia-iso-country-codes.csv") as f:
    file= csv.DictReader(f, delimiter=',')
    for line in file:
        c2code[line['English short name lower case']] = line['Alpha-3 code']
        code2c[line['Alpha-3 code']] = line['English short name lower case']

def get_name(ent):
    if str(ent).count("\'") == 2:
        return str(ent)[str(ent).find("\'")+1:str(ent).rfind("\'")]
    elif str(ent).count('\"') == 2:
        return str(ent)[str(ent).find("\"")+1:str(ent).rfind("\"")]

#countries_to_check = ['Canada','Lithuania','Austria','German Confederation','Poland','Czech Republic']
countries_to_check = []

for LANG in ['bengali','arabic', 'english', 'finnish', 'indonesian', 'japanese', 'korean', 'russian', 'swahili', 'telugu', 'thai']:
    with open(f"../data/entities/tydiqa/tydiqa-dev-{LANG}.pickle", 'rb') as inp:
        lines = pickle.load(inp)

    client = Client()

    json_name = 'tydiqa_data/all_d.json'
    with open(json_name) as inp:
        jsonstr = inp.read()
    d = json.loads(jsonstr)
    #d = {}

    lang_entities = []
    countc = 0
    for k in lines:
        #print(k)
        try:
            l = k[0]['id']
        except:
            continue
        countc += 1
        #l = l.strip().split('\t')[0]
        lang_entities.append(l)
        to_check = False
        if l in d:
            codes_to_check = [country_dict_r[ccc] for ccc in countries_to_check]
            if 'country' in d[l]:
                if d[l]['country'] in codes_to_check:
                    to_check=True
            if 'citizen' in d[l]:
                if d[l]['citizen'] in codes_to_check:
                    to_check=True
            if 'born' in d[l]:
                if d[l]['born'] in codes_to_check:
                    to_check=True
            if 'died' in d[l]:
                if d[l]['died'] in codes_to_check:
                    to_check=True
        #for key in entity:
        #    print(str(key))
        if (l not in d) or to_check:
            try:
                entity = client.get(l, load=True)
            except:
                lang_entities.remove(l)
                continue
            #print(entity)
            name = get_name(entity)
            print(name)
            d[l] = {}
            d[l]['name'] = name

            for key in entity.keys():
                try:
                    if key.id == 'P31':
                        #print(f'instance of: {entity[key].id}')
                        #print(key.id, entity[key])
                        if entity[key].id == 'Q5':
                            #print('\thuman')
                            d[l]['instance'] = 'human'
                        elif entity[key].id == 'Q515':
                            #print('\tcity')
                            d[l]['instance'] = 'city'
                        elif entity[key].id == 'Q6256':
                            #print('\tcountry')
                            d[l]['instance'] = 'country'
                    if key.id in 'P27,P19,P20,P17,P1376,P625,P69,P276,P495,P159'.split(','):
                        #print(key.id, entity[key])
                        if key.id == 'P17':
                            #print(f'\tcountry: {entity[key].id}')
                            d[l]['country'] = entity[key].id
                        elif key.id == 'P20':
                            #print(f'\tdied in: {entity[key].id}')
                            d[l]['died'] = entity[key].id
                        elif key.id == 'P19':
                            #print(f'\tborn in: {entity[key].id}')
                            d[l]['born'] = entity[key].id
                        elif key.id == 'P27':
                            #print(f'\tcitizen of: {entity[key].id}')
                            d[l]['citizen'] = entity[key].id
                        elif key.id == 'P69':
                            #print(f'\teducated at: {entity[key].id}')
                            d[l]['educated'] = entity[key].id
                        elif key.id == 'P276':
                            #print(f'\tlocated at: {entity[key].id}')
                            d[l]['located'] = entity[key].id
                        elif key.id == 'P159':
                            #print(f'\theadquartered at: {entity[key].id}')
                            d[l]['headquartered'] = entity[key].id
                        elif key.id == 'P495':
                            #print(f'\toriginated from: {entity[key].id}')
                            d[l]['from'] = entity[key].id
                        elif key.id == 'P1376':
                            #print(f'\tcapital of: {entity[key].id}')
                            d[l]['capital'] = entity[key].id
                        elif key.id == 'P625':
                            #print(f'\tcoordinates: {entity[key]}')
                            temp = str(entity[key]).split('(')[1]
                            latitude = float(temp.split(',')[0])
                            longtidute = float(temp.split(',')[1])
                            d[l]['coordinates'] = (latitude,longtidute)
                except:
                    pass
        if countc == 100:
            countc = 0
            jsonString = json.dumps(d)
            json_name = f"tydiqa_data/all_d.json"
            with open(json_name, 'w') as op:
                op.write(jsonString)

    new_d = {}
    for l in lang_entities:
        try:
            new_d[l] = d[l]
        except:
            continue

    jsonString = json.dumps(d)
    json_name = f"tydiqa_data/all_d.json"
    with open(json_name, 'w') as op:
        op.write(jsonString)

    jsonString = json.dumps(new_d)
    json_name = f"tydiqa_data/{LANG}_d.json"
    with open(json_name, 'w') as op:
        op.write(jsonString)

    d = new_d

    print("************************")
    #except:
    #    country_dict_r = {}

    with open('place2country.json') as inp:
        lines = inp.read()
    place2country = json.loads(lines)
    #place2country = {}

    with open('id2geo.json') as inp:
        lines = inp.read()
    id2geo = json.loads(lines)
    #id2geo = {}

    count = 0
    count2 = 0
    for key in lang_entities:
        count2 += 1
        if key in id2geo:
            to_continue = True
            if 'country' in id2geo[key]:
                if id2geo[key]['country'] in countries_to_check:
                    to_continue=False
            if 'citizen' in id2geo[key]:
                if id2geo[key]['citizen'] in countries_to_check:
                    to_continue=False
            if 'born' in id2geo[key]:
                if id2geo[key]['born'] in countries_to_check:
                    to_continue=False
            if 'died' in id2geo[key]:
                if id2geo[key]['died'] in countries_to_check:
                    to_continue=False
            if to_continue:
                continue
        if 'country' in d[key]:
            print('>found country')
            if d[key]['country'] in country_dict:
                print(f"{key}\t{d[key]['name']}\tcountry:{country_dict[d[key]['country']]}")
                if key not in id2geo:
                    id2geo[key] = {}
                id2geo[key]['name'] = d[key]['name']
                id2geo[key]['country'] = country_dict[d[key]['country']]
            else:
                country = client.get(d[key]['country'], load=True)
                country_name = get_name(country)
                print(f"{key}\t{d[key]['name']}\tcountry:{country_name}")
                country_dict[country.id] = country_name
                country_dict_r[country_name] = country.id
                count += 1
                if key not in id2geo:
                    id2geo[key] = {}
                id2geo[key]['name'] = d[key]['name']
                id2geo[key]['country'] = country_name

        if 'citizen' in d[key]:
            print('>found citizen')
            if d[key]['citizen'] in country_dict:
                print(f"{key}\t{d[key]['name']}\tcitizen:{country_dict[d[key]['citizen']]}")
                if key not in id2geo:
                    id2geo[key] = {}
                id2geo[key]['name'] = d[key]['name']
                id2geo[key]['citizen'] = country_dict[d[key]['citizen']]
            else:
                place = client.get(d[key]['citizen'], load=True)
                country_name = get_name(place)
                print(f"{key}\t{d[key]['name']}\tcitizen:{country_name}")
                country_dict[place.id] = country_name
                country_dict_r[country_name] = place.id
                count += 1
                if key not in id2geo:
                    id2geo[key] = {}
                id2geo[key]['name'] = d[key]['name']
                id2geo[key]['citizen'] = country_name

        if 'born' in d[key]:
            print('>found born')
            if d[key]['born'] in place2country:
                print(f"{key}\t{d[key]['name']}\tborn:{place2country[d[key]['born']]['country']}")
                if key not in id2geo:
                    id2geo[key] = {}
                id2geo[key]['name'] = d[key]['name']
                id2geo[key]['born'] = place2country[d[key]['born']]['country']
            else:
                if d[key]['born']=='Q2042400':
                    print('')
                place = client.get(d[key]['born'], load=True)
                for key2 in place.keys():
                    if key2.id == 'P17':
                        try:
                            countryid = place[key2].id
                        except:
                            print(key2, key, d[key], place)
                            continue
                        if countryid in country_dict:
                            print(f"{key}\t{d[key]['name']}\tborn:{country_dict[countryid]}")
                            if key not in id2geo:
                                id2geo[key] = {}
                            id2geo[key]['name'] = d[key]['name']
                            id2geo[key]['born'] = country_dict[countryid]
                            place2country[d[key]['born']] = {}
                            place2country[d[key]['born']]['name'] = get_name(place)
                            place2country[d[key]['born']]['country'] = country_dict[countryid]
                        else:
                            country = client.get(countryid, load=True)
                            country_name = get_name(country)
                            print(f"{key}\t{d[key]['name']}\tborn:{country_name}")
                            country_dict[country.id] = country_name
                            country_dict_r[country_name] = country.id
                            place2country[d[key]['born']] = {}
                            place2country[d[key]['born']]['name'] = get_name(place)
                            place2country[d[key]['born']]['country'] = country_name
                            count += 1
                            if key not in id2geo:
                                id2geo[key] = {}
                            id2geo[key]['name'] = d[key]['name']
                            id2geo[key]['born'] = country_name

        if 'died' in d[key]:
            print('>found died')
            if d[key]['died'] in place2country:
                print(f"{key}\t{d[key]['name']}\tdied:{place2country[d[key]['died']]['country']}")
                if key not in id2geo:
                    id2geo[key] = {}
                id2geo[key]['name'] = d[key]['name']
                id2geo[key]['died'] = place2country[d[key]['died']]['country']
            else:
                place = client.get(d[key]['died'], load=True)
                for key2 in place.keys():
                    if key2.id == 'P17':
                        try:
                            countryid = place[key2].id
                        except:
                            print(key2, key, d[key], place)
                            continue
                        if countryid in country_dict:
                            print(f"{key}\t{d[key]['name']}\tdied:{country_dict[countryid]}")
                            if key not in id2geo:
                                id2geo[key] = {}
                            id2geo[key]['name'] = d[key]['name']
                            id2geo[key]['died'] = country_dict[countryid]
                            place2country[d[key]['died']] = {}
                            place2country[d[key]['died']]['name'] = get_name(place)
                            place2country[d[key]['died']]['country'] = country_dict[countryid]
                        else:
                            country = client.get(countryid, load=True)
                            country_name = get_name(country)
                            print(f"{key}\t{d[key]['name']}\tdied:{country_name}")
                            country_dict[country.id] = country_name
                            country_dict_r[country_name] = country.id
                            place2country[d[key]['died']] = {}
                            place2country[d[key]['died']]['name'] = get_name(place)
                            place2country[d[key]['died']]['country'] = country_name
                            count += 1
                            if key not in id2geo:
                                id2geo[key] = {}
                            id2geo[key]['name'] = d[key]['name']
                            id2geo[key]['died'] = country_name
        #print(count)
        if count == 50:
            jsonString = json.dumps(country_dict)
            with open("country_dict.json", 'w') as op:
                op.write(jsonString)

            jsonString = json.dumps(country_dict_r)
            with open("country_dict_r.json", 'w') as op:
                op.write(jsonString)

            jsonString = json.dumps(id2geo)
            with open("id2geo.json", 'w') as op:
                op.write(jsonString)

            jsonString = json.dumps(place2country)
            with open("place2country.json", 'w') as op:
                op.write(jsonString)
            count = 0
        if count2 == 50:
            jsonString = json.dumps(id2geo)
            with open("id2geo.json", 'w') as op:
                op.write(jsonString)
            count2 = 0

    jsonString = json.dumps(country_dict)
    with open("country_dict.json", 'w') as op:
        op.write(jsonString)

    jsonString = json.dumps(country_dict_r)
    with open("country_dict_r.json", 'w') as op:
        op.write(jsonString)

    jsonString = json.dumps(id2geo)
    with open("id2geo.json", 'w') as op:
        op.write(jsonString)

    jsonString = json.dumps(place2country)
    with open("place2country.json", 'w') as op:
        op.write(jsonString)

    from urllib.request import urlopen
    import json
    #with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    #    counties = json.load(response)

    #print(counties["features"][0])

    import pandas as pd

    # import plotly.express as px
    # import plotly.graph_objects as go

    country_list = list(c2code.keys())
    iso_list = [c2code[c] for c in country_list]
    print(len(country_list))
    print(country_list)
    print(iso_list)

    mapping = {}
    mapping['United States of America'] = 'United States'
    mapping["People's Republic of China"] = 'China'
    mapping["The Bahamas"] = 'Bahamas'
    mapping["Ivory Coast"] = "Côte d'Ivoire"
    mapping["Democratic Republic of the Congo"] = "D.R. Congo"
    mapping["Republic of the Congo"] = 'Congo'
    mapping["Dutch East Indies"] = 'Netherlands Antilles'
    mapping["Russian Republic"] = 'Russian Federation'
    mapping["The Gambia"] = 'Gambia'
    mapping["England"] = 'United Kingdom'
    mapping["Scotland"] = 'United Kingdom'
    mapping["Wales"] = 'United Kingdom'
    mapping["East Timor"] = 'Timor-Leste'
    mapping["North Macedonia"] = 'Macedonia'
    mapping["People's Republic of Angola"] = 'Angola'
    mapping["State of Palestine"] = 'Palestine'
    mapping["Federated States of Micronesia"] = 'Micronesia'
    mapping["Democratic Republic of Georgia"] = 'Georgia'
    mapping["Great Britain"] = 'United Kingdom'
    mapping["United Kingdom of Great Britain and Ireland"] = 'United Kingdom'
    mapping["Russia"] = 'Russian Federation'
    mapping["Laos"] = "Lao People's Democratic Republic"
    mapping["São Tomé and Príncipe"] = 'Sao Tome and Principe'
    mapping["Mongolian People's Republic"] = 'Mongolia'
    mapping["Sahrawi Arab Democratic Republic"] = 'Western Sahara'
    mapping["Serbia and Montenegro"] = 'Serbia'
    mapping["Somaliland"] = 'Somalia'
    mapping["Palestinian territories"] = 'Palestine'
    mapping["Northern Ireland"] = 'United Kingdom'
    mapping["Vatican City"] = 'Holy See'
    mapping["Czech REpublic"] = 'Czech Republic'
    mapping["AustriA"] = 'Austria'
    mapping["LIthuania"] = 'Lithuania'
    mapping[""] = ''
    mapping[""] = ''

    for key in country_dict_r:
        if key in country_list:
            print("found!", key)
            if key in c2code:
                print(f"\talso found ISO code: {c2code[key]}")
            else:
                print(f"\tdid not find ISO code: {key}")
        elif key in mapping:
            if mapping[key] in country_list:
                print("found!", mapping[key])
                if mapping[key] in c2code:
                    print(f"\talso found ISO code (map): {c2code[mapping[key]]}")
                else:
                    print(f"\tdid not find ISO code (map): {mapping[key]}")
        else:
            print("not!", key)

    dataset_counts = defaultdict(lambda:0)
    for key in lang_entities:
        if key in id2geo:
            name = id2geo[key]['name']
            if 'country' in id2geo[key] or 'citizen' in id2geo[key]:
                if 'country' in id2geo[key]:
                    country = id2geo[key]['country']
                else:
                    country = id2geo[key]['citizen']
                #print(country)
                if country in mapping:
                    country = mapping[country]
                if country in c2code:
                    code = c2code[country]
                    dataset_counts[code] += 1
            if 'born' in id2geo[key]:
                #print("BORN: ", id2geo[key]['born'])
                born = id2geo[key]['born']
                if born in mapping:
                    born = mapping[born]
                if born in c2code:
                    code = c2code[born]
                    dataset_counts[code] += 1
            if 'died' in id2geo[key]:
                #print("DIED: ", id2geo[key]['died'])
                died = id2geo[key]['died']
                if died in mapping:
                    died = mapping[died]
                if died in c2code:
                    code = c2code[died]
                    dataset_counts[code] += 1

    codes = list(code2c.keys())
    #print(codes)
    #logcounts = [np.log10(dataset_counts[c]) for c in codes]
    logcounts = []
    for c in codes:
        if dataset_counts[c]>0:
            logcounts.append(np.log10(dataset_counts[c]))
        else:
            logcounts.append(0)
    counts = [dataset_counts[c] for c in codes]
    sqrtcounts = np.sqrt(counts)
    #print(counts)
    names = [code2c[c] for c in codes]
    #print(names)
    texts = [f"{names[i]}<br>#entities: {counts[i]}" for i in range(len(codes))]

    df = pd.DataFrame({'iso_alpha': codes, 'country':names, 'counts':counts, 'log_counts':logcounts, 'text':texts, 'sqrt_counts':sqrtcounts})
    df.to_csv(f'tydiqa_count_csvs/tydiqa-{LANG}.csv')
    '''
    #fig = px.choropleth(df,
    fig = go.Figure(data=go.Choropleth(
            locations=df["iso_alpha"],
            z=df["log_counts"], # lifeExp is a column of gapminder
            text=df["text"], # column to add to hover information
            #colorscale="Viridis",
            colorscale="Reds",
            marker_line_color='black',
            marker_line_width=0.5,
            colorbar_title = 'log(#entities)',
            ))
    fig.update_layout(title_text=f'Geographic Coverage: TREx-{LANG}',
        geo=dict(
        showframe=True,
        showcoastlines=True,
        projection_type='natural earth'
    ))
    fig.write_image(f"plots/TREx-{LANG}.png")
    fig.show()
    '''

#================================================
    # fig = go.Figure(data=go.Scattergeo(
    #         locations=df["iso_alpha"],
    #         #size=df["log_counts"], # lifeExp is a column of gapminder
    #         text=df["text"], # column to add to hover information
    #         #colorscale="Viridis",
    #         mode = 'markers',
    #         marker_color = df['log_counts'],

    #         marker_size = df["log_counts"],
    #         #marker_sizemin = 6,
    #         marker_colorscale="Bluered",
    #         marker_sizeref = 0.08,
    #         #marker_line_color='black',
    #         #marker_line_width=0.5,
    #         marker_colorbar_title = 'log(#entities)',
    #         marker_colorbar_ticktext = [0,10,100,1000],
    #         ))
    # fig.update_layout(title_text=f'Geographic Coverage: tydiqa-{LANG}',
    #     geo=dict(
    #     showframe=True,
    #     showcoastlines=True,
    #     showcountries=True,
    #     projection_type='natural earth'
    # ))
    # fig.write_image(f"plots/tydiqa-{LANG}.scatter.png")
    # fig.show()
#=================================================


#! /usr/bin/env python
import json
import warnings
from glob import glob
from os import path

import datreant

def convert(folder):
    treants = glob(path.join(folder, 'Treant*'))
    if len(treants) == 0:
        warnings.warn("No treant found in folder: {}".format(folder))
        return
    elif len(treants) > 1:
        warnings.warn("Multiple treants found in folder: {}\n"
                      "    Skipping conversion".format(folder))
        return

    json_file = treants[0]
    with open(json_file) as fh:
        old = json.load(fh)
    datreant.Treant(folder, categories=old['categories'], tags=old['tags'])

def main():
    import argparse
    parser = argparse.ArgumentParser(
        description="Convert pre 1.0 Treant to new Treant",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument(
        "directories",
        metavar="DIRECTORY",
        nargs="+",
        help="one or more directories that are being processed")
    args = parser.parse_args()

    for dir in args.directories:
        convert(dir)

if __name__ == '__main__':
    main()


from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone

from redactor.fields import RedactorEditor

from insite_messages.models import Message

class MessageComposeForm(forms.Form):

  recipient = forms.ModelChoiceField(widget=forms.Select(attrs={
                        'class': 'ui fluid search dropdown',
                        'placeholder': _('recipient')}),
                      label=_('Recipient'),
                      queryset= User.objects.all())

  subject = forms.CharField(widget=forms.TextInput(attrs={
                        'placeholder': _('subject')}),
                      label=_('Subject'),
                      max_length=120)

  body = forms.CharField(widget=forms.Textarea(attrs={
                         'placeholder': _('message body'),
                         'cols': "20",
                         'rows': "5"}),
                        label=_('Message'),
                        max_length=500)

  def save(self, sender, parent=None):

    recipient = self.cleaned_data['recipient']
    try:
      recipient = User.objects.get(username=recipient.username)
    except User.DoesNotExist:
      raise forms.ValidationError(_('The following username is incorrect: %(username)s') % {
        'username': recipient_username
      })

    subject = self.cleaned_data['subject']
    body = self.cleaned_data['body']

    message = Message.objects.create_message(sender, recipient, subject, body)

    if parent is not None:
      message.parent = parent
      parent.replied_at = timezone.now()
      parent.save()

    message.save()

    return message

  class Meta:
    model = Message
    exclude = ['parent', 'sent_at', 'read_at', 'replied_at', 'sender_deleted_at', 'recipient_deleted_at']
    widgets = {
      'body': RedactorEditor()
    }


from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse,HttpResponse
import youtube_dl
from youtube_dl import DownloadError
import re
import uuid
import os
from youtube_search import YoutubeSearch
import json
@csrf_exempt
def search(request):
    query=request.POST.get("query")
    result=str(uuid.uuid1()).split('-')[0]+".mp3"
    ydl_opts = {
    'format': 'bestaudio/bestaudio',
    'postprocessors': [{'key': 'FFmpegExtractAudio',
               'preferredcodec': 'mp3',
               'preferredquality': '192'}],
    'outtmpl': 'media/'+result,
    'quiet': False,
    'noplaylist':True,
    'ignoreerrors':True,
    'max-downloads':1,
    }
    ydl = youtube_dl.YoutubeDL(ydl_opts)
    try:
        title=(ydl.extract_info(query,download=False))['title']
    except youtbe_dl.utils.ExtractorError:
        return JsonResponse({"INVALID":"This mp3 is not available for download by you tube"},safe=False)
    title=re.sub('[\W_]+', '', str(title))+".mp3"
    try:
        ydl.download([query])
    except  youtube_dl.utils.DownloadError:
        print('Something went wrong!')
    except  youtube_dl.utils.UnavailableVideoError:
        return JsonResponse({"INVALID":"Mp3 format is not available for this song."},safe=False)
    except  youtube_dl.utils.GeoRestrictedError:
        return JsonResponse({"INVALID":"Song is not available in your country."},safe=False)
    except  youtube_dl.utils.YoutubeDLError:
        return JsonResponse({"INVALID":"Some error occured. Try again please."},safe=False)
    return JsonResponse({"fn":result,"sn":title},safe=False)
def autosuggest(request):
    data = request.GET.get("term")
    results = YoutubeSearch(''+data, max_results=5).to_json()
    mimetype = 'application/json'
    return HttpResponse(json.dumps(results), mimetype)


import argparse

def parse_args():
    """
    Parses command line arguments for jobs.
    Available arguments: --recreate, --norefresh, --dryrun, --refreshonly
    Passing no arguments: default behavior
        Truncsert and Tableau Refresh
    --recreate: necessary if table structure changes
        Drop Create and Tableau Refresh
    --recreate --norefresh:
        Drop Create and don't perform Tableau Refresh
    --dryrun: View the full DDL, do not execute statement or refresh tableau
        Print full statement and return
    --refreshonly: Kick the tableau refresh
        Tableau Refresh only
    Examples:
    python3 jobs/debug__command_line_args.py --norefresh --recreate
    Namespace(dryrun=False, norefresh=True, recreate=True, refreshonly=False)
    python3 jobs/debug__command_line_args.py --norefresh
    Namespace(dryrun=False, norefresh=True, recreate=False, refreshonly=False)
    python3 jobs/debug__command_line_args.py
    Namespace(dryrun=False, norefresh=False, recreate=False, refreshonly=False)
    """

    parser = argparse.ArgumentParser()
    parser.add_argument("--recreate", help="recreate", action="store_true")
    parser.add_argument("--norefresh", help="no refresh", action="store_true")
    parser.add_argument("--dryrun", help="dryrun", action="store_true")
    parser.add_argument("--refreshonly", help="refreshonly", action="store_true")
    args = parser.parse_args()

    return args

if __name__ == '__main__':
    print(parse_args())


from sortedcontainers import SortedList

class Solution:
    def countFairPairs(self, nums: List[int], lower: int, upper: int) -> int:
        n = len(nums)
        bst = SortedList()
        res = 0
        for i in range(n):
            res += bst.bisect_right(upper - nums[i]) - bst.bisect_left(lower - nums[i])
            bst.add(nums[i])
        return res


import random
import sys
import art
from colorama import Fore, Style

# object for gold
class Treasure:
    def __init__(self, gold):
        self.gold = gold

    def set(self, gold):
        self.gold = gold

    def add(self, gold):
        self.gold += gold

    def add_treasure(self, treasure):
        self.add(treasure.gold)

# may not need this repr
    # def __repr__(self):
    #     return f'Treasure(gold={self.gold})'

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

# object for treasure coffers
class Coffer:
    def __init__(self, gold):
        self.gold = Treasure(gold)

    def loot(self, character):
        character.gold.add_treasure(self.gold)
        self.gold.set(0)
        print(f'You now have {player.gold} pieces of gold.' + Style.RESET_ALL)

# object for combat characters (player and enemy monsters)
class Character:
    def __init__(self, name, max_health, strength, cure):
        self.name = name
        self.max_health = max_health
        self.health = max_health
        self.strength = strength
        self.cure = cure
        self.gold = Treasure(0)

    def attack(self, enemy):
        damage_variance = random.randint(-5, 5)
        damage = self.strength + damage_variance
        print(Fore.MAGENTA + f'You swing your sword at the {enemy.name}!')
        print(f'You deal {damage} damage!' + Style.RESET_ALL)
        enemy.health -= damage
        enemy.health = max(0, enemy.health)
        print(f'The {enemy.name} now has {enemy.health} health.')

    def take_damage(self, enemy):
        damage_variance = random.randint(-5, 5)
        damage = enemy.strength + damage_variance
        self.health -= damage
        self.health = max(0, self.health)  # makes sure health >= 0
        print(Fore.RED + f'The {enemy.name} attacks you for {damage} damage.')
        print(f'You now have {player.health} health.' + Style.RESET_ALL)

    def heal(self):
        heal_variance = random.randint(-5, 5)
        heal_amount = self.cure + heal_variance
        self.health += heal_amount
        self.health = min(self.health, self.max_health)  # caps health at max
        print(Fore.GREEN
              + f'You cast a healing spell and recover {heal_amount} health!')
        print(f'You now have {player.health} health.' + Style.RESET_ALL)

# treasure coffer spawn function
def spawn_coffer():
    coffer = Coffer(random.randint(10, 30))
    with open('coffer.txt') as f:
        contents = f.read()
        print(contents)
    print('Do you want to ' + Fore.YELLOW + 'loot' + Style.RESET_ALL
          + ' the coffer, or ' + Fore.RED + 'ignore' + Style.RESET_ALL
          + ' it and continue?')
    while True:
        decision = input().lower()
        if decision == 'ignore':
            print('You ignore the coffer and continue on your way.')
            break
        elif decision == 'loot':
            print(Fore.YELLOW
                  + f'You find {coffer.gold} pieces of gold inside.')
            coffer.loot(player)
            global coffers_looted
            coffers_looted += 1
            break
        else:
            print('Can\'t do that. Try again!')

# treasure coffer trap/mimic spawn function
def spawn_mimic():
    enemy = Character('mimic', 100, 30, None)
    with open('coffer.txt') as f:
        contents = f.read()
        print(contents)
    print('Do you want to ' + Fore.YELLOW + 'loot' + Style.RESET_ALL
          + ' the coffer, or ' + Fore.RED + 'ignore' + Style.RESET_ALL
          + ' it and continue?')
    while True:
        decision = input().lower()
        if decision == 'ignore':
            print(Fore.GREEN
                  + 'Good choice. That was actually a mimic in disguise!')
            print('You get away safely!' + Style.RESET_ALL)
            break
        elif decision == 'loot':
            print(f'The {enemy.name} reveals itself!')
            print('It sheds its glittering disguise and attacks!')
            player.take_damage(enemy)
            print('What do you do?')
            global enemies_defeated
            players_turn = True

            while player.health > 0 and enemy.health > 0:
                if players_turn:
                    print('Do you ' + Fore.RED + 'attack' + Style.RESET_ALL
                          + f' the {enemy.name} or cast a ' + Fore.GREEN
                          + 'heal' + Style.RESET_ALL + ' spell?')
                    decision = input().lower()
                    if decision == 'heal':
                        player.heal()
                    elif decision == 'attack':
                        player.attack(enemy)
                    else:
                        print('Can\'t do that. Try again!')
                        continue
                else:
                    print(f'It\'s the {enemy.name}\'s turn.')
                    player.take_damage(enemy)

                players_turn = not players_turn

            if player.health <= 0:
                print(Fore.RED
                      + f'You have been defeated by the {enemy.name}.')
                game_recap()
            else:
                print(Fore.YELLOW
                      + f'You have successfully defeated the {enemy.name}!'
                      + Style.RESET_ALL)
                enemies_defeated += 1
            break
        else:
            print('Can\'t do that. Try again!')

# monster spawn function
def spawn_monster():
    enemy = Character('monster', 100, 30, None)
    with open('monster.txt') as f:
        contents = f.read()
        print(contents)
    print(f'You have {player.health} health.')
    print('What do you do?')
    global enemies_defeated
    players_turn = True

    while player.health > 0 and enemy.health > 0:
        if players_turn:
            print('Do you ' + Fore.RED + 'attack' + Style.RESET_ALL
                  + f' the {enemy.name} or cast a ' + Fore.GREEN
                  + 'heal' + Style.RESET_ALL + ' spell?')
            decision = input().lower()
            if decision == 'heal':
                player.heal()
            elif decision == 'attack':
                player.attack(enemy)
            else:
                print('Can\'t do that. Try again!')
                continue
        else:
            print(f'It\'s the {enemy.name}\'s turn.')
            player.take_damage(enemy)

        players_turn = not players_turn

    if player.health <= 0:
        print(Fore.RED + f'You have been defeated by the {enemy.name}.')
        game_recap()
    else:
        print(Fore.YELLOW + f'You have successfully defeated the {enemy.name}!'
              + Style.RESET_ALL)
        enemies_defeated += 1

# function to display tally of enemy and coffer encounters
def game_recap():
    print(Fore.CYAN + f'You encountered {enemies_encountered} enemies.')
    print(f'You defeated {enemies_defeated} of those enemies.')
    print(f'You encountered {coffers_encountered} coffers.')
    print(f'You looted {coffers_looted} coffers.' + Style.RESET_ALL)
    sys.exit()

# declaring global variables
player = Character('Adventurer', 100, 40, 50)
enemies_encountered = 0
enemies_defeated = 0
coffers_encountered = 0
coffers_looted = 0

# main game code
print('You arrive at the entrance of the caverns.')
print('Armed with your trusty sword and handful of spells,')
print('you venture bravely into the dark depths before you.')
art.aprint('sword6')
while True:
    print('Continue to the next area?')
    decision = input(Fore.GREEN + 'Y' + Style.RESET_ALL + ' or '
                     + Fore.RED + 'N? ' + Style.RESET_ALL).lower()
    if decision == 'n':
        print('Until next time!')
        game_recap()
    elif decision == 'y':
        encounter = random.randint(1, 10)
        if encounter >= 9:
            print(Fore.YELLOW + 'You discover a glittering treasure coffer!')
            print(Style.RESET_ALL)
            coffers_encountered += 1
            spawn_coffer()
        elif encounter >= 3:
            print(Fore.RED + 'You run into a monster!'
                  + Style.RESET_ALL)
            enemies_encountered += 1
            spawn_monster()
            coffer_drop = random.randint(1, 10)
            if coffer_drop >= 9:
                print(Fore.YELLOW + 'Huzzah! '
                      + 'The defeated monster drops a bonus treasure coffer!'
                      + Style.RESET_ALL)
                coffers_encountered += 1
                spawn_coffer()
            else:
                pass
        else:
            print(Fore.YELLOW
                  + 'You discover a suspicious looking treasure coffer.'
                  + Style.RESET_ALL)
            enemies_encountered += 1
            spawn_mimic()
    else:
        print('Can\'t do that. Try again!')


from typing import Dict, Optional, Tuple

import torch
import torch.nn.functional as F
from transformers.models.gpt2.modeling_gpt2 import GPT2LMHeadModel

from archai.nlp.models.hf_gpt2.config_hf_gpt2 import HfGPT2Config, HfGPT2FlexConfig
from archai.nlp.models.hf_gpt2.hf_gpt2_utils.gpt2_lm_head_model_flex import GPT2LMHeadModelFlex
from archai.nlp.models.model_base import ArchaiModel

class HfGPT2(ArchaiModel):
    """Huggingface's Open AI GPT-2 standard architecture.

    """

    def __init__(self, **kwargs) -> None:
        """Initializes the class by creating compatible configuration and model objects.

        """

        super().__init__()

        self.config = HfGPT2Config(**kwargs)
        self.model = GPT2LMHeadModel(self.config)

        if self.config.tie_weight:
            self.model.tie_weights()

    def forward(self,
                input_ids: torch.Tensor,
                labels: Optional[torch.Tensor] = None,
                mems: Optional[torch.Tensor] = None,
                past_key_values: Optional[torch.Tensor] = None,
                output_loss: Optional[bool] = True,
                output_prediction_scores: Optional[bool] = False
                ) -> Tuple[torch.Tensor, ...]:
        assert mems is None, 'HfGPT2 does not support memory (mems).'

        # Labels are the same as input_ids because they will be shifted inside the model
        # Causal attention mask is also created inside the model
        outputs = self.model(input_ids=input_ids,
                             labels=input_ids,
                             attention_mask=torch.ones_like(input_ids),
                             past_key_values=past_key_values)

        if output_loss:
            return (outputs.loss, None, None, outputs.past_key_values)

        if output_prediction_scores:
            # GPT-2 only outputs the logits, so they need to be converted with log_softmax
            return (None, F.log_softmax(outputs.logits, dim=-1), None, outputs.past_key_values)

    def get_params(self) -> Dict[str, int]:
        params = {}

        params['embedding'] = self.get_params_from_layer(['Embedding'])
        params['attention'] = self.get_params_from_layer(['GPT2Attention'])
        params['ff'] = self.get_params_from_layer(['GPT2MLP'])
        params['layer_norm'] = self.get_params_from_layer(['LayerNorm'])

        params['non_embedding'] = params['attention'] + params['ff'] + params['layer_norm']
        params['total'] = params['non_embedding'] + params['embedding']

        return params

class HfGPT2Flex(ArchaiModel):
    """Huggingface's Open AI GPT-2 flex-based architecture.

    Flex-based architectures allow different hyperparameters settings for each layer.

    """

    def __init__(self, **kwargs) -> None:
        """Initializes the class by creating compatible configuration and model objects.

        """

        super().__init__()

        self.config = HfGPT2FlexConfig(**kwargs)
        # assert all(self.config.n_head[0] == n_h for n_h in self.config.n_head), 'HfGPT2Flex does not support different `n_head`.'

        self.model = GPT2LMHeadModelFlex(self.config)

        if self.config.tie_weight:
            self.model.tie_weights()

    def forward(self,
                input_ids: torch.Tensor,
                labels: Optional[torch.Tensor] = None,
                mems: Optional[torch.Tensor] = None,
                past_key_values: Optional[torch.Tensor] = None,
                output_loss: Optional[bool] = True,
                output_prediction_scores: Optional[bool] = False
                ) -> Tuple[torch.Tensor, ...]:
        assert mems is None, 'HfGPT2Flex does not support memory (mems).'

        # Causal attention mask is created inside the model
        outputs = self.model(input_ids=input_ids,
                             labels=labels,
                             attention_mask=torch.ones_like(input_ids),
                             past_key_values=past_key_values,
                             output_loss=output_loss,
                             output_prediction_scores=output_prediction_scores)

        if output_loss:
            return (outputs.loss, None, None, outputs.past_key_values)

        if output_prediction_scores:
            return (None, outputs.logits, None, outputs.past_key_values)

    def get_params(self) -> Dict[str, int]:
        params = {}

        if self.config.div_val >= 1:
            # Positional embeddings are nn.Embedding layers, which overlap
            # when calculated together with AdaptiveEmbedding (have inner nn.Embedding layers)
            params['embedding'] = self.get_params_from_layer(['AdaptiveEmbedding']) + self.config.max_position_embeddings * self.config.hidden_size
        else:
            params['embedding'] = self.get_params_from_layer(['Embedding'])

        params['attention'] = self.get_params_from_layer(['GPT2AttentionFlex'])
        params['ff'] = self.get_params_from_layer(['GPT2MLPFlex'])
        params['layer_norm'] = self.get_params_from_layer(['LayerNorm'])

        params['non_embedding'] = params['attention'] + params['ff'] + params['layer_norm']
        params['total'] = params['non_embedding'] + params['embedding']

        return params


from pyrogram import Client, Filters, StopPropagation, InlineKeyboardButton, InlineKeyboardMarkup

@Client.on_message(Filters.command(["help"]))
async def start(client, message):
	alpha2 = InlineKeyboardMarkup([
		[InlineKeyboardButton("WhatsApp Group", url="https://chat.whatsapp.com/FOALYW6PMitLo9jntxEGqi")],
                [InlineKeyboardButton("Youtube Channel", url="https://youtube.com/channel/UC6ZgCl_f6YPxAsrzWhApPow")],
		 ])
	help_image = "https://telegra.ph/file/778e59fcf123197692cb0.jpg"
	await message.reply_photo(help_image,  caption="**Spooky Help** \n\n`ENG HELP...`\n**• Just Send your Youtube video url** \n**• And i will give Method list to select your choice**\n\n`SI HELP...`\n**• කොපි කරගත් Youtube ලින්කුව එවන්න.**\n**• එවිට ලැබෙන ලැයිස්තුවෙන් අවශ්‍ය ප්‍රමාණය හා මාදිලිය තෝරාදෙන්න.**\n\n\n**•This bot is fully free.**\n__• Don't pay anyone for Bots like this.__",reply_markup=alpha2)


import streamlit as st
import pandas as pd
import pickle
import numpy as np
from PIL import Image

model_landrace = pickle.load(open('model_landrace.sav', 'rb'))
model_yorkshire = pickle.load(open('model_yorkshire.sav', 'rb'))

st.title('Prediksi Angka Kelahiran Hewan Ternak Babi')
image = Image.open('img/pic.png')
st.image(image, width=300)

def predict_landrace(usia, bobot, penyakit, induk):
    result = model_landrace.predict([[0, bobot, usia, penyakit, induk]])

    return result

def predict_yorkshire(usia, bobot, penyakit, induk):
    result = model_yorkshire.predict([[0, bobot, usia, penyakit, induk]])

    return result

def main():
    jenis_option = st.selectbox("Pilih jenis babi", ["Landrace", "Yorkshire"])

    usia = st.slider('Usia (Bulan)', 8, 71, 8)
    bobot = st.slider('Bobot (Kg)', 80, 250, 80)
    chk_penyakit = st.selectbox('Penyakit', ('Ya', 'Tidak'))
    if chk_penyakit == 'Ya':
        penyakit = 1
    else:
        penyakit = 0
    induk = st.slider('Jumlah Indukan Yang Akan Melahirkan', 2, 4, 2)

    if st.button("Prediksi"):
        if jenis_option == "Landrace":
            result = predict_landrace(usia, bobot, penyakit, induk)
        elif jenis_option == "Yorkshire":
            result = predict_yorkshire(usia, bobot, penyakit, induk)

        prediksi = str(result).replace('[', '').replace(']', '')
        hasil = np.round(float(prediksi))
        hasil = int(hasil)
        st.write("Hasil prediksi:", hasil)
        st.write("Jadi untuk 2 minggu kedepan induk babi akan melahirkan sebanyak ", hasil, "ekor anak babi")
        st.write("Dan untuk per ekor indukan babi akan melahirkan sebanyak ", int(hasil/induk), "ekor anak babi")

if __name__ == "__main__":
    main()


from collections import OrderedDict

def lengthOfLongestSubstringKDistinct(s: str, k: int) -> int:
    n = len(s)
    if k == 0 or n == 0:
        return 0

    left = right = 0

    hash_m = OrderedDict()

    max_len = 1

    while right < n:
        character = s[right]
        if character in hash_m:
            del hash_m[character]
        hash_m[character] = right
        right += 1

        if len(hash_m) == k + 1:
            _, del_idx = hash_m.popitem(last=False)
            left = del_idx + 1
        max_len = max(max_len, right - left)
    return max_len


from copy import deepcopy

import pytest
from langchain.prompts import ChatPromptTemplate

from ix.chains.llm_chain import LLMChain, TEMPLATE_CLASSES
from ix.chains.loaders.prompts import create_message
from ix.chains.tests.mock_memory import MockMemory
from ix.chains.tests.test_config_loader import OPENAI_LLM, MOCK_MEMORY

PROMPT_TEMPLATE = {
    "class_path": "langchain.prompts.chat.ChatPromptTemplate",
    "config": {
        "messages": [
            {
                "role": "system",
                "template": "Say hello to {name} and answer user question",
                "partial_variables": {
                    "name": "test user",
                },
            },
            {
                "role": "user",
                "template": "Question: {user_input}",
                "input_variables": ["user_input"],
            },
            {"role": "assistant", "template": "Answer: ANSWER"},
        ],
    },
}

EXAMPLE_CONFIG = {
    "class_path": "ix.chains.llm_chain.LLMChain",
    "config": {
        "llm": OPENAI_LLM,
        "memory": MOCK_MEMORY,
        "prompt": PROMPT_TEMPLATE,
    },
}

@pytest.mark.django_db
class TestChatPromptTemplate:
    def test_create_message(self):
        message = {
            "role": "user",
            "template": "hello {name} i will answer {user_input}",
            "input_variables": ["user_input"],
            "partial_variables": {"name": "test user"},
        }

        result = create_message(message)

        assert isinstance(result, TEMPLATE_CLASSES["user"])
        assert result.prompt.partial_variables == {"name": "test user"}

    def test_from_config(self, load_chain):
        config = deepcopy(PROMPT_TEMPLATE)
        chain = load_chain(config)
        assert isinstance(chain, ChatPromptTemplate)
        assert len(chain.messages) == 3
        assert isinstance(chain.messages[0], TEMPLATE_CLASSES["system"])
        assert isinstance(chain.messages[1], TEMPLATE_CLASSES["user"])
        assert isinstance(chain.messages[2], TEMPLATE_CLASSES["assistant"])

@pytest.mark.django_db
class TestLLMChain:
    def test_from_config(self, load_chain, mock_openai_key):
        config = deepcopy(EXAMPLE_CONFIG)
        chain = load_chain(config)

        assert isinstance(chain, LLMChain)
        assert (
            chain.prompt.messages[0].prompt.partial_variables
            == EXAMPLE_CONFIG["config"]["prompt"]["config"]["messages"][0][
                "partial_variables"
            ]
        )
        assert chain.prompt.messages[1].prompt.partial_variables == {}
        assert isinstance(chain.memory, MockMemory)


import pygame
import sys
import random

# Initialisation de Pygame
pygame.init()

# Dimensions de l'écran
largeur, hauteur = 640, 480
taille_case = 20

# Couleurs
noir = (0, 0, 0)
blanc = (255, 255, 255)
vert = (0, 255, 0)

# Création de l'écran
ecran = pygame.display.set_mode((largeur, hauteur))
pygame.display.set_caption("Snake")

# Fonction pour afficher le message de fin de jeu
def message_fin(texte):
    police = pygame.font.Font(None, 36)
    texte_surface = police.render(texte, True, blanc)
    texte_rect = texte_surface.get_rect()
    texte_rect.center = (largeur // 2, hauteur // 2)
    ecran.blit(texte_surface, texte_rect)
    pygame.display.flip()
    pygame.time.wait(2000)

# Fonction principale du jeu
def jouer():
    # Initialisation du serpent
    serpent = [(3, 1), (3, 2), (3, 3)]
    direction = (1, 0)

    # Position initiale de la pomme
    pomme = (random.randint(0, (largeur // taille_case) - 1), random.randint(0, (hauteur // taille_case) - 1))

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    direction = (0, -1)
                elif event.key == pygame.K_DOWN:
                    direction = (0, 1)
                elif event.key == pygame.K_LEFT:
                    direction = (-1, 0)
                elif event.key == pygame.K_RIGHT:
                    direction = (1, 0)

        # Déplacer le serpent
        tête = (serpent[0][0] + direction[0], serpent[0][1] + direction[1])
        serpent.insert(0, tête)

        # Vérifier si le serpent mange la pomme
        if serpent[0] == pomme:
            pomme = (random.randint(0, (largeur // taille_case) - 1), random.randint(0, (hauteur // taille_case) - 1))
        else:
            serpent.pop()

        # Vérifier si le serpent a perdu
        if (serpent[0][0] < 0 or serpent[0][0] >= largeur // taille_case or
                serpent[0][1] < 0 or serpent[0][1] >= hauteur // taille_case or
                serpent[0] in serpent[1:]):
            message_fin("Game Over")
            jouer()

        # Effacer l'écran
        ecran.fill(noir)

        # Dessiner le serpent
        for segment in serpent:
            pygame.draw.rect(ecran, vert, (segment[0] * taille_case, segment[1] * taille_case, taille_case, taille_case))

        # Dessiner la pomme
        pygame.draw.rect(ecran, blanc, (pomme[0] * taille_case, pomme[1] * taille_case, taille_case, taille_case))

        # Mettre à jour l'écran
        pygame.display.flip()

        # Contrôler la vitesse du jeu
        pygame.time.Clock().tick(10)

# Lancer le jeu
jouer()


from django.shortcuts import render,HttpResponse
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from utils.pages import page
from jasset import models
from django.db.models import Q
from jasset.api.asset import DealResponseAsset
# Create your views here.

@login_required
def asset_list(request):
    asset_objs = models.Asset.objects.select_related()
    page_objs = page(request, asset_objs)
    return render(request, "jasset/asset_list.html", {"page_objs": page_objs})

@login_required
def host_list(request):
    host_objs = models.Host.objects.select_related()
    page_objs = page(request, host_objs)
    return render(request, "jasset/host_list.html", {"page_objs": page_objs})

@csrf_exempt
def asset_api(request):
    """处理客户端反馈回的数据"""
    deal_asset_obj=DealResponseAsset(request)
    deal_asset_obj.execute()

    return HttpResponse("OK")

@login_required
def asset_info(request,asset_id):

    host_obj=models.Host.objects.filter(asset_id=asset_id).first()
    try:
        record_objs=host_obj.asset.assetrecord.all().order_by("-create_time")
    except:
        record_objs=None

    return render(request,"jasset/asset_info.html",locals())

@login_required
def assetrecord_list(request):
    if request.method=="GET":
        assetrecord_objs=models.AssetRecord.objects.select_related().order_by("create_time")
        page_objs=page(request,assetrecord_objs,page_number=20)
    if request.method=="POST":
        host_ip=request.POST.get("host_ip")
        assetrecord_objs=models.AssetRecord.objects.filter(asset__host__host_ip=host_ip).select_related().order_by("create_time")
        page_objs=page(request,assetrecord_objs,page_number=20)
    return render(request,"jasset/asset_record_list.html",locals())

@login_required
def asseterrorlog_list(request):
    if request.method=="GET":
        asset_error_log_objs=models.ErrorLog.objects.select_related().order_by("create_time")
        page_objs=page(request,asset_error_log_objs,page_number=20)
    if request.method=="POST":
        host_ip=request.POST.get("host_ip")
        asset_error_log_objs=models.ErrorLog.objects.filter(asset__host__host_ip=host_ip).select_related().order_by("create_time")
        page_objs=page(request,asset_error_log_objs,page_number=20)
    return render(request,"jasset/asset_error_log_list.html",locals())