from google.cloud.functions.v1beta2 import cloud_functions_service_client
from google.cloud.proto.functions.v1beta2 import functions_pb2
from google.gax.errors import GaxError
from grpc import StatusCode

# point this at your project ID
PROJECT_ID = 'gapic-test'

LOCATION_ID = 'us-central1'
FUNCTION_ID = 'helloWorld'

# upload helloWorld.zip from this directory to Google Storage for your project
SOURCE_URI = 'gs://gapic-functions-v1beta2/helloWorld.zip'

api = cloud_functions_service_client.CloudFunctionsServiceClient()
location = api.location_path(PROJECT_ID, LOCATION_ID)
function_name = api.function_path(PROJECT_ID, LOCATION_ID, FUNCTION_ID)

def on_delete(operation_future):
    try:
        api.get_function(function_name)
    except GaxError as e:
        code = getattr(e.cause, "code", None)
        if callable(code) and code() == StatusCode.NOT_FOUND:
            print('Expect error here since the function should have been deleted')
        else:
            raise

def on_update(operation_future):
    result = operation_future.result()
    print('Function updated: \n%s\n' % result)

    fetched_function = api.get_function(result.name)
    print('Function fetched: \n%s\n' % fetched_function)

    data = '{"message":"Hello World!"}'
    call_response = api.call_function(fetched_function.name, data)
    print('Function call response: \n%s\n' % call_response)

    print('List functions:\n')
    for function in api.list_functions(location):
        print(function)

    print('Delete function:\n')
    delete_response = api.delete_function(function_name)
    delete_response.add_done_callback(on_delete)
    print('Metadata: \n%s\n' % delete_response.metadata())

def on_create(operation_future):
    result = operation_future.result()
    print('Function created: \n%s\n' % result)

    updated_function = functions_pb2.CloudFunction(
        name=result.name,
        source_archive_url=result.source_archive_url,
        pubsub_trigger=('projects/%s/topics/hello_world2' % PROJECT_ID))

    response = api.update_function(result.name, updated_function)
    response.add_done_callback(on_update)
    print('Metadata: \n%s\n' % response.metadata())

def on_init(_):
    function = functions_pb2.CloudFunction(
        name=function_name,
        source_archive_url=SOURCE_URI,
        pubsub_trigger=('projects/%s/topics/hello_world' % PROJECT_ID))
    response = api.create_function(location, function)
    response.add_done_callback(on_create)
    print('Metadata: \n%s\n' % response.metadata())

try:
    response = api.delete_function(function_name)
    response.add_done_callback(on_init)
except GaxError as e:
    code = getattr(e.cause, "code", None)
    if callable(code) and code() == StatusCode.NOT_FOUND:
        on_init(None)
    else:
        raise


import pytest

from xdsl.ir import MLContext, Operation
from xdsl.parser import XDSLParser
from xdsl.dialects.builtin import Builtin
from xdsl.dialects.func import Func
from xdsl.dialects.arith import Arith
from xdsl.dialects.cf import Cf
from xdsl.rewriting.composable_rewriting.immutable_ir.immutable_ir import get_immutable_copy

program_region = \
"""builtin.module() {
  %0 : !i32 = arith.constant() ["value" = 1 : !i32]
}
"""
program_region_2 = \
"""builtin.module() {
  %0 : !i32 = arith.constant() ["value" = 2 : !i32]
}
"""
program_region_2_diff_name = \
"""builtin.module() {
  %cst : !i32 = arith.constant() ["value" = 2 : !i32]
}
"""
program_region_2_diff_type = \
"""builtin.module() {
  %0 : !i64 = arith.constant() ["value" = 2 : !i64]
}
"""
program_add = \
"""builtin.module() {
%0 : !i32 = arith.constant() ["value" = 1 : !i32]
%1 : !i32 = arith.constant() ["value" = 2 : !i32]
%2 : !i32 = arith.addi(%0 : !i32, %1 : !i32)
}
"""
program_add_2 = \
"""builtin.module() {
%0 : !i32 = arith.constant() ["value" = 1 : !i32]
%1 : !i32 = arith.constant() ["value" = 2 : !i32]
%2 : !i32 = arith.addi(%1 : !i32, %0 : !i32)
}
"""
program_func = \
"""builtin.module() {
  func.func() ["sym_name" = "test", "type" = !fun<[!i32, !i32], [!i32]>, "sym_visibility" = "private"] {
  ^0(%0 : !i32, %1 : !i32):
    %2 : !i32 = arith.addi(%0 : !i32, %1 : !i32)
    func.return(%2 : !i32)
  }
}
"""
program_successors = \
"""builtin.module() {
    func.func() ["sym_name" = "unconditional_br", "function_type" = !fun<[], []>, "sym_visibility" = "private"] {
    ^0:
        cf.br() (^1)
    ^1:
        cf.br() (^0)
    }
}
"""

@pytest.mark.parametrize("program_str", [(program_region), (program_region_2),
                                         (program_region_2_diff_type),
                                         (program_region_2_diff_name),
                                         (program_add), (program_add_2),
                                         (program_func), (program_successors)])
def test_immutable_ir(program_str: str):
    ctx = MLContext()
    ctx.register_dialect(Builtin)
    ctx.register_dialect(Func)
    ctx.register_dialect(Arith)
    ctx.register_dialect(Cf)

    parser = XDSLParser(ctx, program_str)
    program: Operation = parser.parse_op()
    immutable_program = get_immutable_copy(program)
    mutable_program = immutable_program.to_mutable()

    assert program.is_structurally_equivalent(mutable_program)


import ccxt
from tradingview_ta import TA_Handler, Interval, Exchange, Compute
import requests

def arbitraj(pair):
    send_message = pair + " prices are searched for you on all exchanges.\nPlease wait..."
    requests.post(url=send_url, data={'chat_id': chat_id, 'text': send_message}).json()

    account = []
    account.append(ccxt.binance())
    account.append(ccxt.bitbay())
    account.append(ccxt.bitfinex())
    account.append(ccxt.bitforex())
    account.append(ccxt.bithumb())
    account.append(ccxt.bitmex())
    account.append(ccxt.bitpanda())
    account.append(ccxt.coinbase())
    account.append(ccxt.coinegg())
    account.append(ccxt.ftx())
    account.append(ccxt.gemini())
    account.append(ccxt.hitbtc())
    account.append(ccxt.huobipro())
    account.append(ccxt.idex())
    account.append(ccxt.kraken())
    account.append(ccxt.kucoin())
    account.append(ccxt.liquid())
    account.append(ccxt.okcoin())
    account.append(ccxt.okex())
    account.append(ccxt.poloniex())

    minAsk = float(account[0].fetch_ticker(pair + '/USDT')['ask'])
    maxBid = float(account[0].fetch_ticker(pair + '/USDT')['bid'])
    askName = ""
    bidName = ""

    for i in range(0, len(account)):
        markets = account[i].load_markets()

        if pair + '/USDT' in markets:
            if minAsk > float(account[i].fetch_ticker(pair + '/USDT')['ask']):
                minAsk = float(account[i].fetch_ticker(pair + '/USDT')['ask'])
                askName = account[i]
            if maxBid < float(account[i].fetch_ticker(pair + '/USDT')['bid']):
                maxBid = float(account[i].fetch_ticker(pair + '/USDT')['bid'])
                bidName = account[i]
        elif pair + '/USD' in markets:
            if minAsk > float(account[i].fetch_ticker(pair + '/USD')['ask']):
                minAsk = float(account[i].fetch_ticker(pair + '/USD')['ask'])
                askName = account[i]
            if maxBid < float(account[i].fetch_ticker(pair + '/USD')['bid']):
                maxBid = float(account[i].fetch_ticker(pair + '/USD')['bid'])
                bidName = account[i]
        else:
            continue

    send_message = " Exchanges with the most price difference:\n\n" + str(askName) + " - Buying: " + str(minAsk) + "$\n" + str(bidName) + " - Sales: " + str(maxBid) + "$"
    requests.post(url=send_url, data={'chat_id': chat_id, 'text': send_message}).json()

    send_message = pair + " is selected...\nChoose the action you want to take below:\n/ARB - Arbitrage Research\n/GENERAL - General Analysis Advice\n/PSAR - Parabolic SAR Analysis Advice\n/OSC - Oscillators Average\n/RSI - RSI Indicator Analysis Advice\n/MACD - MACD Indicator Analysis Advice\n/CCI - CCI Indicator Analysis Advice\n\nTo change your chosen coin and return to the beginning, /Change"
    requests.post(url=send_url, data={'chat_id': chat_id, 'text': send_message}).json()

def analysis(pair, chosen):
    crypto = TA_Handler(
        symbol=pair + "USDT",
        screener="CRYPTO",
        exchange="BINANCE",
        interval=Interval.INTERVAL_1_DAY
    )

    if (chosen=="GENERAL"):
        send_message = "\"" + pair + "\" The General Analysis Recommendation is as follows:\n" + str(crypto.get_analysis().summary["RECOMMENDATION"]) + "\nBUY: " + str(crypto.get_analysis().summary["BUY"]) + "\nNEUTRAL: " + str(crypto.get_analysis().summary["NEUTRAL"]) + "\nSELL: " + str(crypto.get_analysis().summary["SELL"])
    elif (chosen=="PSAR"):
        send_message = "\"" + pair + "\" Parabolic SAR Analysis Advice is as follows:\n" + Compute.PSAR(crypto.get_analysis().indicators["P.SAR"], crypto.get_analysis().indicators["open"])
    elif (chosen=="OSC"):
        send_message = "\"" + pair + "\" The Oscillators Average is as follows:\n" + crypto.get_analysis().oscillators["RECOMMENDATION"]
    elif (chosen=="RSI"):
        send_message = "\"" + pair + "\" RSI Oscillator Analysis Advice is as follows:\n" + Compute.RSI(crypto.get_analysis().indicators["RSI"], crypto.get_analysis().indicators["RSI[1]"])
    elif (chosen=="MACD"):
        send_message = "\"" + pair + "\" MACD Oscillator Analysis Advice is as follows:\n" + Compute.MACD(crypto.get_analysis().indicators["MACD.macd"], crypto.get_analysis().indicators["MACD.signal"])
    elif (chosen=="CCI"):
        send_message = "\"" + pair + "\" CCI Oscillator Analysis Advice is as follows:\n" + Compute.CCI20(crypto.get_analysis().indicators["CCI20"], crypto.get_analysis().indicators["CCI20[1]"])

    requests.post(url=send_url, data={'chat_id': chat_id, 'text': send_message}).json()

    send_message = pair + " is selected.\nSelect the action you want to take below:\n/ARB - Arbitrage Research\n/GENERAL - General Analysis Advice\n/PSAR - Parabolic SAR Analysis Advice\n/OSC - Oscillators Average\n/RSI - RSI Oscillator Analysis Advice\n/MACD - MACD Oscillator Analysis Advice\n/CCI - CCI Oscillator Analysis Advice\n\nTo change your chosen coin and return to the beginning, /Change"
    requests.post(url=send_url, data={'chat_id': chat_id, 'text': send_message}).json()

send_url = "https://api.telegram.org/<YOUR BOT API TOKEN>/sendMessage"
prev_date = "11111"
pair = ""

chs_text = "Select the action you want to take below:\n/ARB - Arbitrage Research\n/GENERAL - General Analysis Advice\n/PSAR - Parabolic SAR Analysis Advice\n/OSC - Oscillators Average\n/RSI - RSI Oscillator Analysis Advice\n/MACD - MACD Oscillator Analysis Advice\n/CCI - CCI Oscillator Analysis Advice\n\nTo change your chosen coin and return to the beginning, /Change"
while True:

    tlg = requests.get("https://api.telegram.org/<YOUR BOT API TOKEN>/getupdates").json()
    last_object = tlg['result'][-1]
    new_date = last_object["message"]["date"]

    if(str(new_date) != str(prev_date)):
        prev_date = new_date
        chat_id = last_object['message']['chat']['id']

        if 'first_name' in last_object['message']['from']:
            first_name = last_object['message']['from']['first_name']
        else: first_name = ""
        if 'last_name' in last_object['message']['from']:
            last_name = last_object['message']['from']['last_name']
        else: last_name = ""

        message_text = last_object['message']['text']

        send_message = ""

        if (message_text.upper() == "/START" or message_text.upper() == "/CHANGE"):
            send_message = "Hi " + first_name + " " + last_name + ",\nPlease select a cryptocurrency to get started:\n/BTC - Bitcoin\n/ETH - Ethereum\n/DOGE - Dogecoin\n/XRP - Ripple\n/ADA - Cardano\n/AVAX - Avalance\n/HOT - HoloCoin \n/DOT - Polkadot\n/LINK - Chainlink\n/XLM - Stellar"
        elif (message_text.upper() == "/BTC"):
            pair = "BTC"
            send_message = "BTC - Bitcoin is selected.\n" + chs_text
        elif (message_text.upper() == "/ETH"):
            pair = "ETH"
            send_message = "ETH - Ethereum is selected.\n" + chs_text
        elif (message_text.upper() == "/DOGE"):
            pair = "DOGE"
            send_message = "DOGE - Dogecoin is selected.\n" + chs_text
        elif (message_text.upper() == "/XRP"):
            pair = "XRP"
            send_message = "XRP - Ripple is selected.\n" + chs_text
        elif (message_text.upper() == "/ADA"):
            pair = "ADA"
            send_message = "ADA - Cardano is selected.\n" + chs_text
        elif (message_text.upper() == "/AVAX"):
            pair = "AVAX"
            send_message = "AVAX - Avalance is selected.\n" + chs_text
        elif (message_text.upper() == "/HOT"):
            pair = "HOT"
            send_message = "HOT - HoloCoin is selected.\n" + chs_text
        elif (message_text.upper() == "/DOT"):
            pair = "DOT"
            send_message = "DOT - Polkadot is selected.\n" + chs_text
        elif (message_text.upper() == "/LINK"):
            pair = "LINK"
            send_message = "LINK - Chainlink is selected.\n" + chs_text
        elif (message_text.upper() == "/XLM"):
            pair = "XLM"
            send_message = "XLM - Stellar is selected.\n" + chs_text
        elif (pair != "" and message_text.upper() == "/ARB"):
            arbitraj(pair)
        elif (pair != "" and message_text.upper() == "/GENERAL"):
            analysis(pair, "GENERAL")
        elif (pair != "" and message_text.upper() == "/PSAR"):
            analysis(pair, "PSAR")
        elif (pair != "" and message_text.upper() == "/OSC"):
            analysis(pair, "OSC")
        elif (pair != "" and message_text.upper() == "/RSI"):
            analysis(pair, "RSI")
        elif (pair != "" and message_text.upper() == "/MACD"):
            analysis(pair, "MACD")
        elif (pair != "" and message_text.upper() == "/CCI"):
            analysis(pair, "CCI")
        else:
            send_message = "Your request is not understood. You can start with\n/Start."

        requests.post(url=send_url, data={'chat_id': chat_id, 'text': send_message}).json()


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from tensorflow import keras
import tensorflow as tf
import numpy as np
from ops import *
from resnet import resnet

# The later one could be function and class type.

import argparse

"""parsing and configuration"""''
'''Use arguments to set up parameter accordingly'''

# def parse_args():
def parse_flags():

    desc="TensorFlow implementation of Resnet on Fashion_mnist! author: Hazard Wen"
    parser=argparse.ArgumentParser(description=desc)
    # How to confirm whether this desc works well or not.

    # all the outside arguments can be passed successfully.
    # Bool data type can not be input by keyboard.

    '''

	In Deep_learning_prac2, we define one function to convert keyboard input into boolean.

	def str2bool(v):
		if isinstance(v, bool):
			return v
		if v.lower() in ('yes', 'true', 't', 'y', '1'):
			return True
		elif v.lower() in ('no', 'false', 'f', 'n', '0'):
			return False
		else:
		raise argparse.ArgumentTypeError('Boolean value expected.')

	'''

    '''

	check if is newest!!!

	'''
    # add dropout or not. Dropout, probability to keep units
    parser.add_argument('--dropout_keep_prob', type=float,
        help='Keep probability of dropout for the fully connected layer(s).', default=0.8)
    parser.add_argument('--learning_rate', type=float, help='learning rate', default=0.1)
    parser.add_argument('--max_epochs', type=int, help='Number of epochs to run.', default=100)
    parser.add_argument('--batch_size', type=int,
                        help='Number of images to process in a batch.', default=64)
    parser.add_argument('--activation_function', type=str, choices=['elu', 'relu', 'relu6', 'leaky_relu', 'sigmoid', 'tanh', 'linear'],
                        help='which activation function we are going to use', default='None')
    # add argument for data_augmentation with bool data type
    parser.add_argument('--data_augmentation', type=str, choices=['True','False'], help='excute data augmentation or not', default='False')
    #******************************************************************************************************************#
    # Be careful on spelling when you type flag outside, otherwise it will occur one error.
    # error: unrecognized arguments: --data_augmention True
    # add arguments for different optimizer
    parser.add_argument('--optimizer', type=str, choices=['ADAGRAD', 'ADADELTA', 'ADAM', 'RMSPROP', 'MOM'],
        help='The optimization algorithm to use', default='None')
    # add argument for batch_normalization or not with bool data type
    parser.add_argument('--is_batch_normalization',type=str,choices=['True','False'], help='excute batch_normalization or not',default='False')

    # The following one without check_args is also correct.
    # return parser.parse_args()
    return check_args(parser.parse_args())

def check_args(args):

    return args

'''
main function
'''

def main():

    print('The version of tensorflow:', tf.__version__)
    print('The version of keras:', keras.__version__)

    # specific parse arguments
    #args=parse_args()
    #args=parser.parse_args()
    args=parse_flags()
    if args is None:
        exit()

    print('\n The type of args is:',type(args))
    print('\n This is the existing arguments:', args)
    print('\n ')

    # access the Fashion MNIST directly using TensorFlow method
    from tensorflow.examples.tutorials.mnist import input_data
    mnist = input_data.read_data_sets('data/fashion', one_hot=True)

    # Creating placeholders
    # Explore the format of the dataset before training the model.
    # The following shows there are 60,000 images in the training set,
    # with each image represented as 28 x 28 (784) pixels
    # train_images.shape = (60000, 28, 28)
    # Likewise, there are 60,000 labels in the training set
    # len(train_labels) = 60000
    x = tf.placeholder(tf.float32, shape=[None, 784])
    #
    print('\n The shape of x placeholder is', tf.shape(x))
    array_1 = np.array([1,2])
    print('\n The shape of array_1 is', tf.shape(array_1))
    print('\n like x=np.array([1,2]), Here is tensor-like [None,784]')
    # They seems that they are different.
    y_true = tf.placeholder(tf.float32, shape=[None, 10])

    # If one component of shape is the special value -1, the size of that dimension is computed so that the total size remains constant.
    # In particular, a shape of [-1] flattens into 1-D. At most one component of shape can be -1.
    x_image = tf.reshape(x, [-1, 28, 28, 1])
    x_image = tf.identity(x_image,'input')
    print('\n The shape of x_image is', tf.shape(x_image ))
    print('\n like x=np.array([none,1,2,3]), Here is tensor-like [None,28,28,1]')
    # Create the ResNet model
    '''x=x_image means that [none,28,28,1]'''
    # The instance name should be model.
    # The class name shoulde be Resnet, uppercase should be better.
    model = resnet(args, x=x_image, n=20, num_classes=10) # we are going to use 20 layers.
    score = model.out

    # using pre-defined function to get training loss and training accuracy
    training_loss, accuracy=classification_loss(score,y_true)

    # define reg loss
    # reg_loss = tf.losses.get_regularization_loss()
    # training_loss += reg_loss

    # different Optimizer
    def optimizer_choose(optimizer,learning_rate):

        if optimizer == 'ADAGRAD':
            opt = tf.train.AdagradOptimizer(learning_rate)
            return opt
        elif optimizer == 'ADADELTA':
            opt = tf.train.AdadeltaOptimizer(learning_rate, rho=0.9, epsilon=1e-6)
            return opt
        elif optimizer == 'ADAM':
            #opt = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, epsilon=0.1)
            opt = tf.train.AdamOptimizer(learning_rate)
            return opt
        elif optimizer == 'RMSPROP':
            opt = tf.train.RMSPropOptimizer(learning_rate, decay=0.9, momentum=0.9, epsilon=1.0)
            return opt
        elif optimizer == 'MOM':
            opt = tf.train.MomentumOptimizer(learning_rate, 0.9, use_nesterov=True)
            return opt
        else:
            raise ValueError('Invalid optimization algorithm')

    choosed_optimizer_opt= optimizer_choose(args.optimizer,args.learning_rate)

    train = choosed_optimizer_opt.minimize(training_loss)

    init = tf.global_variables_initializer()

    with tf.Session() as sess:
        sess.run(init)

        for j in range(args.max_epochs):
            for i in range(0, 60000, args.batch_size):
                # mnist.train.next_batch returns a tuple of two arrays
                batch_x, batch_y = mnist.train.next_batch(args.batch_size)
                #print('The tf.shape of batch_x is', tf.shape(batch_x))
                #print('The np.shape of batch_x is', np.shape(batch_x))
                #print('The type of batch_x is', type(batch_x))
                '''batch_x looks like ([64,784])'''
                batch_x=data_augmentation(batch_x, 28, is_data_augmentation=args.data_augmentation)
                # perform minimize loss per batch
                sess.run(train, feed_dict={x: batch_x, y_true: batch_y})
            # Try to add training loss
            epoch_train_loss=sess.run(training_loss,feed_dict={x: batch_x, y_true: batch_y})
            # Try to add training accuracy here per epoch
            training_accuracy=sess.run(accuracy,feed_dict={x: batch_x, y_true: batch_y})

            # change test accuracy using sess.run instead of eval
            #test_accuracy = accuracy.eval(feed_dict={x: mnist.test.images[: 5000],
            #                                         y_true: mnist.test.labels[: 5000]})
            testing_accuracy = sess.run(accuracy, feed_dict={x: mnist.test.images[: 5000],
                                                             y_true: mnist.test.labels[: 5000]})
            print('After %d epochs, testing_accuracy: %g, training_accuracy: %g, epoch_train_loss: %g, %d epochs' % (j + 1, testing_accuracy, training_accuracy,epoch_train_loss,j+1))

if __name__=='__main__':
    main()


# -*- encoding: utf8 -*-
import json
import re
import urllib

import bones.event
from bones.bot import Module

class NickFix(Module):
    def __init__(self, *args, **kwargs):
        Module.__init__(self, *args, **kwargs)
        self.nickIWant = None
        self.isRecovering = False

    @bones.event.handler(event=bones.event.UserQuitEvent)
    @bones.event.handler(event=bones.event.UserNickChangedEvent)
    def somethingHappened(self, myEvent):
        user = None
        if self.nickIWant is None:
            self.nickIWant = \
                self.settings.get("bot", "nickname").split("\n")[0]

        if isinstance(myEvent, bones.event.UserNickChangedEvent) is True:
            user = myEvent.oldname
        else:
            user = myEvent.user.nickname

        if user.lower() == self.nickIWant.lower():
            myEvent.client.factory.nicknames = \
                self.settings.get("bot", "nickname").split("\n")[1:]
            self.isRecovering = True
            myEvent.client.setNick(self.nickIWant)

    @bones.event.handler(event=bones.event.BotSignedOnEvent)
    def resetMe(self, event):
        self.isRecovering = False
        self.nickIWant = None

    @bones.event.handler(event=bones.event.PreNicknameInUseError)
    def shouldWeEvenTry(self, event):
        if self.isRecovering:
            event.isCancelled = True
            self.isRecovering = False

class Ping(Module):
    def __init__(self, *args, **kwargs):
        Module.__init__(self, *args, **kwargs)
        self.ongoingPings = {}

    @bones.event.handler(trigger="ping")
    def cmdPing(self, event):
        nick = event.user.nickname
        if nick not in self.ongoingPings:
            self.ongoingPings[nick] = event.channel.name
            event.user.ping()
        else:
            event.user.notice(
                "Please wait until your ongoing ping in %s is finished until "
                "trying again."
                % self.ongoingPings[nick]
            )

    @bones.event.handler(event=bones.event.CTCPPongEvent)
    def eventPingResponseReceive(self, event):
        nick = event.user.nickname
        if nick in self.ongoingPings:
            event.user.notice("%s: Your response time was %.3f seconds."
                              % (nick, event.secs))
            del self.ongoingPings[nick]

class Twitter(Module):
    bs = None
    urlopener = None

    reTweetLink = re.compile("(https?\:\/\/)?twitter\.com\/[a-zA-Z0-9\-\_]+\/status\/\d+", re.IGNORECASE)  # NOQA

    def __init__(self, *args, **kwargs):
        Module.__init__(self, *args, **kwargs)
        try:
            from bs4 import BeautifulSoup
            self.bs = BeautifulSoup
        except ImportError:
            self.log.warn(
                "Unmet dependency BeautifulSoup4: The URL checkers will be "
                "disabled."
            )

    @bones.event.handler(event=bones.event.ChannelMessageEvent)
    def eventURLInfo_Twitter(self, event):
        if self.bs is not None:
            if "twitter" in event.message and "http" in event.message:
                data = self.reTweetLink.search(event.message)
                if data:
                    url = data.group(0)
                    html = event.client.factory.urlopener.open(url).read()
                    soup = self.bs(html)
                    tweet = soup \
                        .find("div", {"class": "permalink-inner permalink-tweet-container"}) \
                        .find("p", {"class": "tweet-text"}) \
                        .text
                    tweet = u"↵ ".join(tweet.split("\n"))
                    user = soup \
                        .find("div", {"class": "permalink-inner permalink-tweet-container"}) \
                        .find("span", {"class": "username js-action-profile-name"}) \
                        .text

                    # shitty fix for pic.twitter.com links
                    # could be improved by going through all links, check
                    # whether they start with http and if not replace the
                    # nodeText with the href attribute.
                    out = []
                    for word in tweet.split(" "):
                        if word.startswith("pic.twitter.com"):
                            word = "https://%s" % word
                        out.append(word)
                    tweet = " ".join(out)

                    msg = (u"\x0310Twitter\x03 \x0311::\x03 %s \x0311––\x03 %s"
                           % (tweet, user))
                    event.channel.msg(msg.encode("utf-8"))

class YouTube(Module):
    bs = None
    __fetchData = lambda x: {"template": "html", "title": "Something went wrong"}

    reVideoLink = re.compile("(https?\:\/\/)?(m\.|www\.)?(youtube\.com\/watch\?(.+)?v\=|youtu\.be\/)(?P<id>[a-zA-Z-0-9\_\-]*)")  # NOQA
    __template_simple = u"\x0314You\x035Tube \x0314::\x03 {title} \x034::\x03 http://youtu.be/{id}"  # NOQA
    __template_api = u"\x0314You\x035Tube \x034::\x03 {title}\x0314, {snippet[channelTitle]} \x034::\x0314 {duration} {definition} \x034::\x03 http://youtu.be/{id}"  # NOQA

    apiEndpoint = "https://www.googleapis.com/youtube/v3/%s?%s"

    def __init__(self, *args, **kwargs):
        Module.__init__(self, *args, **kwargs)
        self.fetchData = self.__fetchData

        self.template_simple = self.settings.get("module.utilities", "youtube.template.simple", default=self.__template_simple)
        self.template_api = self.settings.get("module.utilities", "youtube.template.api", default=self.__template_api)
        self.apikey = self.settings.get("module.utilities", "youtube.apikey",
                                        default=None)
        if not self.apikey:
            self.log.warn(
                "No API key provided. Video search and detailed video info "
                "will be disabled.")
            self.fetchData = self.fetchData_Html
        else:
            self.fetchData = self.fetchData_YouTubeApi

        if not self.apikey:
            try:
                from bs4 import BeautifulSoup
                self.bs = BeautifulSoup
            except ImportError:
                self.log.warn(
                    "Unmet dependency BeautifulSoup4: The URL checkers will "
                    "be disabled."
                )

    def api_request(self, method, **args):
        args["key"] = self.apikey
        url = self.apiEndpoint % (method, urllib.urlencode(args))
        result = self.factory.urlopener.open(url).read()
        return json.loads(result)

    def fetchData_Html(self, video):
        url = "http://youtube.com/watch?%s" % urllib.urlencode({"v": video})
        html = self.factory.urlopener.open(url).read()
        soup = self.bs(html)
        title = soup.find("span", {"id": "eow-title"}).text.strip()
        return {
            "template": "html",
            "id": video,
            "title": title,
        }

    def api_videoDetails(self, video):
        return self.api_request("videos",
                                part="statistics,snippet,contentDetails",
                                id=video)

    def api_videoSearch(self, term):
        data = self.api_request("search", part="id", safeSearch="none",
                                order="relevance", type="video",
                                maxResults="1", q=term)
        if not data or "items" not in data or len(data["items"]) < 1:
            return None
        return self.fetchData_YouTubeApi(data["items"][0]["id"]["videoId"])

    def fetchData_YouTubeApi(self, video):
        data = self.api_videoDetails(video)
        if not data["items"]:
            return
        output = data["items"][0]
        output.update(output["snippet"])
        output["duration"] = output["contentDetails"]["duration"].lower()[2:]
        output["definition"] = output["contentDetails"]["definition"].upper()
        output["template"] = "api"
        return output

    def sendToChannel(self, channel, data):
        if data["template"] == "api":
            output = self.template_api
        else:
            output = self.template_simple

        output = output.format(**data)
        output = u"↵ ".join(output.split("\n"))
        channel.msg(output.encode("utf-8"))

    @bones.event.handler(event=bones.event.ChannelMessageEvent)
    def checkMessageForUrl(self, event):
        if not self.bs and not self.apikey:
            return
        if not ("youtu" in event.message and "http" in event.message):
            return

        data = self.reVideoLink.search(event.message)
        if not data:
            return

        video_data = self.fetchData(data.group("id"))
        if not video_data:
            return
        self.sendToChannel(event.channel, video_data)

    @bones.event.handler(trigger="yt")
    @bones.event.handler(trigger="youtube")
    def videoSearch(self, event):
        if not self.apikey:
            return
        term = " ".join(event.args)
        video = self.api_videoSearch(term)
        if not video:
            event.reply("No such results.")
        self.sendToChannel(event.channel, video)


import tempfile,os
import random

from linebot import LineBotApi, WebhookParser
from linebot.models import *
from dotenv import load_dotenv
from imgurpython import ImgurClient
from PIL import Image

load_dotenv()
client_id = os.getenv("client_id", None)
client_secret = os.getenv("client_secret", None)
access_token = os.getenv("access_token",None)
refresh_token = os.getenv("refresh_token",None)
channel_access_token = os.getenv("LINE_CHANNEL_ACCESS_TOKEN", None)

def send_text_message(reply_token,text):
    line_bot_api = LineBotApi(channel_access_token)
    line_bot_api.reply_message(reply_token, TextSendMessage(text=text))

    return "OK"
def send_button_template(reply_token,buttons_template):
    line_bot_api = LineBotApi(channel_access_token)
    line_bot_api.reply_message(reply_token, template = buttons_template)
    return "OK"
def send_yes_no_button(id,template):
    line_bot_api = LineBotApi(channel_access_token)
    line_bot_api.push_message(id, template)
    return "OK"
def send_image_url(reply_token, target):
    line_bot_api = LineBotApi(channel_access_token)
    if(target == ''):
        print('There is nothing funny.')
        text = 'There is nothing funny.'
        line_bot_api.reply_message(reply_token, TextSendMessage(text=text))
    else:
        line_bot_api.reply_message(reply_token,ImageSendMessage(original_content_url=target, preview_image_url=target))

    return "OK"
def send_img(id, target):
    line_bot_api = LineBotApi(channel_access_token)
    line_bot_api.push_message(id,ImageSendMessage(original_content_url=target, preview_image_url=target))
    return "OK"
def push_msg_img(id, target, text):
    line_bot_api = LineBotApi(channel_access_token)
    line_bot_api.push_message(id, TextSendMessage(text))
    line_bot_api.push_message(id,ImageSendMessage(original_content_url=target, preview_image_url=target))
    return "OK"
def push_msg(id,text):
    line_bot_api = LineBotApi(channel_access_token)
    line_bot_api.push_message(id,TextSendMessage(text))
    return "ok"
def upload_img(event):
    line_bot_api = LineBotApi(channel_access_token)
    static_tmp_path = os.path.join(os.path.dirname(__file__))
    ext = 'png'
    message_content = line_bot_api.get_message_content(event.message.id)
    with tempfile.NamedTemporaryFile(dir=static_tmp_path, prefix=ext + '-', delete=False) as tf:
        for chunk in message_content.iter_content():
            tf.write(chunk)
        tempfile_path = tf.name

    dist_path = tempfile_path + '.' + ext
    dist_name = os.path.basename(dist_path)
    os.rename(tempfile_path, dist_path)
    im = Image.open(dist_path)
    width, height = im.size
    client = ImgurClient(client_id, client_secret, access_token, refresh_token)
    path = os.path.join(dist_name)
    upimg = client.upload_from_path(path, config=None, anon=False)
    os.remove(path)
    print(upimg['link'])
    push_msg(event.source.user_id,"上傳成功")
    return upimg ,width,height


#######################################################
# This simulation analyzes performance of the mean field approximation
# versus the k approximation for at large times for various values
# of kappa (degree of the tree) and beta for the Ising model.
# These two approximations are compared to MC sampled dynamics, and
# accuracy is measured by:
# the total variation distance between the joints of the root node
# and one of its neighbors, as well as the distributions of the root,
# joint distributions with more neighbors, and trajectories as well.
#
# This comparison in particular analyzes the 2Approx near the critical regime.
#######################################################

import os
import sys
sys.path.append("../SimulateProcesses/")
sys.path.append("../SimulateProcesses/Ising")
from mpl_toolkits.axes_grid1 import make_axes_locatable

import IsingMonitor as monitor

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

if __name__== "__main__":

	#np.random.seed(17)
	## at stationarity, which method works better?

	p = 0.9 #update parameter
	bias = -0.2
	p_0 = (1+bias)/2

	## intake what parameters to run/ analyze

	kappa_params = [2,3,4,5,6,7,8]
	beta_params = np.array(np.linspace(0,2,11))
	kb_params = np.array([[6,0.2],
						 [6,0.4],
						 [5,0.4],
						 [4,0.4],
						 [4, 0.6000000000000001],
						 [4,0.8],
						 [3, 0.6000000000000001],
						 [3,0.8],
						 [3,1.0],
						 [3,1.2000000000000002],
						 [3,1.4000000000000001],
						 [3,1.6],
						 [3,1.8],
						 [3,2.0]])
	k = 2
	depth = 8

	# ideally want this to be larger
	T = 50

	### Collect Data
	# use Pandas, want multidimensional index;
	# columns should be dynamics; mean field; k-approximation,various vals of k.

	# save the dataframes
	def get_df_string(s):
		f = "IsingModelResults/{}_T{}_maxkappa{}_maxbeta{}_bias{}".format(s,T,
				8, 2.0, -0.2)
		return f

	joint_prob_df = pd.read_pickle(get_df_string("mf_local_comparison"))

	def str_to_dict(str):
		d = ast.literal_eval(str)
		return d

	# compute the distances
	total_variations_kapprox = np.zeros((len(kappa_params),len(beta_params)))
	for i in range(len(kb_params)):

		# calculate dynamics
		idx = pd.IndexSlice
		prob_dynamics = np.array(list(str_to_dict(joint_prob_df.loc[idx[int(kb_params[i,0]),kb_params[i,1]],idx['dynamics']]).values()))

		# get the kapprox info
		filename = "IsingModelResults/kapprox/kapprox_kappa{}_p0.9_beta{:.1f}_T{}_k{}_bias{}.npy".format(int(kb_params[i,0]),kb_params[i,1], T, k, bias)

		measure_history = np.load(filename)

		process = monitor.MonitorLocalApproximation(np.zeros((2,2,2)),p,kb_params[i,1])
		process.measure_history = measure_history

		joint2_la = process.get_fixed_time_measure((0,1),T-1)
		prob_kapprox = joint2_la.reshape(1,4)

		total_variations_kapprox[int(kb_params[i,0]) - 2,int(kb_params[i,1]/0.2)] = np.sum(np.abs(prob_kapprox - prob_dynamics))

	def visualize_total_var(array, title):
		fig, ax = plt.subplots(1,1, figsize = (10,10))

		cmap = matplotlib.cm.viridis
		norm = plt.Normalize(0, 2)

		betas = np.array(np.linspace(0,2,81))
		kappas = 2*np.exp(2*betas)/(np.exp(2* betas) - 1)
		ax.plot(5.5*betas-0.5, kappas - 2, color = 'red', label = "Critical Value")
		im1 = ax.imshow(array[::-1,],cmap = cmap.reversed(), norm = norm, extent = [0 - 0.5,len(beta_params)-1 + 0.5,0 - 0.5,len(kappa_params)-1 + 0.5])

		tick_size = 6.5

		ax.set_xlabel('Beta',fontsize = 10)
		ax.set_xticks(np.arange(len(beta_params)))
		ax.set_xticklabels(labels = ["{:0.1f}".format(beta) for beta in beta_params],fontsize = tick_size )

		ax.set_ylabel('Kappa',fontsize = 10)
		ax.set_yticks(np.arange(len(kappa_params)))
		ax.set_yticklabels(labels = list(kappa_params),fontsize = tick_size )
		#ax.legend(loc='upper right', bbox_to_anchor=(0.15, 0.0))

		ax.set_title(title)

		for i in range(len(kappa_params)):
			for j in range(len(beta_params)):
				text = ax.text(j, i, "{:0.2f}".format(array[i, j]),
					ha="center", va="center", color="w")

		divider1 = make_axes_locatable(ax)
		cax1 = divider1.append_axes('right', size='5%', pad=0.1)
		fig.colorbar(im1,cax1,orientation = 'vertical')

		plt.show()

	visualize_total_var(total_variations_kapprox, "Accuracy of 2-Approximation at Critical Region")


from torchdrug import data, utils
import csv
from tqdm import tqdm

class Dataset(data.MoleculeDataset):
    target_fields = []

    def __init__(self, path, verbose=1, **kwargs):

        with open(path, "r") as fin:
            reader = csv.reader(fin)
            if verbose:
                reader = iter(tqdm(reader, "Loading %s" %
                              path, utils.get_line_count(path)))
            smiles_list = []

            for idx, values in enumerate(reader):
                smiles = values[0]
                smiles_list.append(smiles)

        targets = {}
        self.load_smiles(smiles_list, targets, lazy=False,
                         verbose=verbose, **kwargs)


import concurrent.futures
import os

import requests
import rich.progress

def download(
    url: str, file: str, progress: rich.progress.Progress, task_id: rich.progress.TaskID
):
    res: requests.Response = requests.get(url=url, stream=True)
    total: int = int(res.headers.get("Content-Length", default=0))
    progress.update(task_id=task_id, total=(total if total else None))
    os.makedirs(name=os.path.dirname(p=file), exist_ok=True)
    with open(file=file, mode="wb") as fp:
        progress.start_task(task_id=task_id)
        for chunk in res.iter_content(chunk_size=1024 * 1024):
            bytes_written: int = fp.write(chunk)
            progress.advance(task_id=task_id, advance=bytes_written)
        if not total:
            progress.update(task_id=task_id, total=fp.tell())
    progress.stop_task(task_id=task_id)

def schedule_download(
    url: str,
    file: str,
    progress: rich.progress.Progress,
    pool: concurrent.futures.Executor,
):
    task_id: rich.progress.TaskID = progress.add_task(
        description=file, start=False, total=None
    )
    pool.submit(download, url=url, file=file, progress=progress, task_id=task_id)


import numpy as np
import torch
from ServerDL.cfgs.Protocol import *

def packup(prediction, segms=None, result_type="detection"):
    if result_type == "detection":          # detection
        response_boxes = prediction.bbox.cpu().int().numpy().flatten().tolist()
        response_classes = prediction.get_field("labels").cpu().numpy().flatten().tolist() if prediction.has_field("labels") else None
        response_keyps = prediction.get_field("keypoint").cpu().numpy().flatten().tolist() if prediction.has_field("keypoint") else None  # ???不确定fieldname是不是keypoint
        response_conf = prediction.get_field("scores").cpu().numpy().flatten().tolist() if prediction.has_field("scores") else None

        package = {
            KEY.REQUEST_TYPE: VALUE.REQUEST_TYPE.MODEL_PREDICT,
            KEY.RESPONSE_STATUS: VALUE.RESPONSE_STATUS.SUCCEED,
            KEY.PREDICTION.BOXES: response_boxes,
            KEY.PREDICTION.BOXMODE: prediction.mode,
        }
        if response_classes:
            package[KEY.PREDICTION.CLASSES] = response_classes
        if response_conf:
            package[KEY.PREDICTION.CONFIDENCES] = response_conf
        if response_keyps:
            package[KEY.PREDICTION.KEYPSINFO] = response_keyps,

        if isinstance(segms, torch.Tensor):
            segms = segms.cpu().numpy().astype(np.uint8)
            package[KEY.PREDICTION.MASKINFO] = {KEY.PREDICTION.ROWS: segms.shape[0],
                                        KEY.PREDICTION.COLS: segms.shape[1],
                                      KEY.PREDICTION.CHANNELS: segms.shape[2] if len(segms.shape) == 3 else 1}
    elif result_type == "classification":       # classification
        if isinstance(prediction, torch.Tensor):
            prediction = prediction.cpu().numpy().tolist()
        if isinstance(prediction, np.ndarray):
            prediction = prediction.tolist()
        package = {
            KEY.REQUEST_TYPE: VALUE.REQUEST_TYPE.MODEL_PREDICT,
            KEY.RESPONSE_STATUS: VALUE.RESPONSE_STATUS.SUCCEED,
            KEY.PREDICTION.CLASSES: list(prediction) if isinstance(prediction, list) else [prediction]
        }
    elif result_type == "segmentation":
        assert isinstance(prediction, np.ndarray), "segmentation is no a ndarray"
        segms = prediction.copy()
        package = {
            KEY.REQUEST_TYPE: VALUE.REQUEST_TYPE.MODEL_PREDICT,
            KEY.RESPONSE_STATUS: VALUE.RESPONSE_STATUS.SUCCEED,
            KEY.PREDICTION.MASKINFO: {KEY.PREDICTION.ROWS: segms.shape[0],
                                      KEY.PREDICTION.COLS: segms.shape[1],
                                      KEY.PREDICTION.CHANNELS: segms.shape[2] if len(segms.shape) == 3 else 1}
        }

    print(package)
    return package, segms

def select_top_predictions(predictions, confidence_threshold):
    scores = predictions.get_field("scores")
    keep = torch.nonzero(scores > confidence_threshold).squeeze(1)
    predictions = predictions[keep]
    scores = predictions.get_field("scores")
    _, idx = scores.sort(0, descending=True)
    return predictions[idx]


# -*- coding: utf-8 -*-#
#
# Project:      vocabulary
# Name:         api_base_user
# Author:       lzq
# Date:         2021/4/10 下午2:29
#

from base.models import BaseUser
from collections import OrderedDict
from django.http import JsonResponse
from base.func import BaseUserRoleMethod
from rest_framework.filters import SearchFilter
from django.utils.decorators import method_decorator
from django_filters.rest_framework import DjangoFilterBackend
from common.utils import require_role_decorator, PublicModelViewSet
from common.basic.redis_models import redis_resource_set_list_value
from base.serializers import BaseUserSerializer, BaseUserListSerializer, BaseUserFilter

@method_decorator(require_role_decorator('manager'), name="dispatch")
class BaseUserViewSet(PublicModelViewSet):
    """
    系统用户视图
    """
    queryset = BaseUser.objects.all().order_by('id')
    serializer_class = BaseUserListSerializer
    resource_name = '系统用户'
    # 根据不同方法，操作不同类型的字段
    # primary: 用来申明主键字段
    # remove: 需要移除的字段
    serializer_fields = {'remove': [{'fields': ['password'], 'action': ['list', 'retrieve']}]}

    # 返回时需要去掉的字段
    detach_response_fields = ['password']

    # 使用过滤器
    filter_backends = (SearchFilter, DjangoFilterBackend,)
    # 引用自定义的过滤类
    filter_class = BaseUserFilter

    # 搜索字段
    search_fields = ('username', 'cname', 'user_sn', 'department', 'email', 'phone', 'describe')

    # 操作角色是需要传入的参数
    role_args = [{'arg': 'role_list', 'empty': False, 'verify': OrderedDict([('value_list', 'digit')])}]

    # 系统用户授予/解绑角色
    @PublicModelViewSet.pk_resource
    def user_role_relation(self, request, operate, pk):
        return JsonResponse(BaseUserRoleMethod(request, self.role_args).operate_user_role(self.instance, operate))

    def create(self, request, *args, **kwargs):
        self.serializer_class = BaseUserSerializer
        return super().create(request, *args, **kwargs)

    def partial_update(self, request, *args, **kwargs):
        self.serializer_class = BaseUserSerializer
        user_obj = self.get_object()
        user_old_role = list(user_obj.role_list.values_list('id', flat=True))
        response = super().partial_update(request, *args, **kwargs)
        user_new_role = list(user_obj.role_list.values_list('id', flat=True))
        # 角色变更，删除token
        if sorted(user_old_role) != sorted(user_new_role):
            redis_resource_set_list_value(f"user:token:{user_obj.id}", '_', 1)
        return response

    def list(self, request, *args, **kwargs):
        if str(request.query_params.get('simple', '0')) == '1':
            setattr(self, 'show_fields', ['id', 'username', 'user_sn', 'cname'])
        return super().list(request, *args, **kwargs)


# -*- coding: utf-8 -*-
#
# Licensed under the terms of the BSD 3-Clause or the CeCILL-B License
# (see codraft/__init__.py for details)

"""
CodraFT Action handler module

This module handles all application actions (menus, toolbars, context menu).
These actions point to CodraFT panels, processors, objectlist, ...
"""

# pylint: disable=invalid-name  # Allows short reference names like x, y, ...

import abc
import enum

from guidata.configtools import get_icon
from guidata.qthelpers import add_actions, create_action
from qtpy import QtGui as QG
from qtpy import QtWidgets as QW

from codraft.config import _
from codraft.widgets import fitdialog

class ActionCategory(enum.Enum):
    """Action categories"""

    FILE = enum.auto()
    EDIT = enum.auto()
    VIEW = enum.auto()
    OPERATION = enum.auto()
    PROCESSING = enum.auto()
    COMPUTING = enum.auto()

class BaseActionHandler(metaclass=abc.ABCMeta):
    """Object handling panel GUI interactions: actions, menus, ..."""

    OBJECT_STR = ""  # e.g. "signal"

    def __init__(self, panel, objlist, itmlist, processor, toolbar):
        self.panel = panel
        self.objlist = objlist
        self.itmlist = itmlist
        self.processor = processor
        self.feature_actions = {}
        self.operation_end_actions = None
        self.delete_roi_action = None
        # Object selection dependent actions
        self.actlist_1more = []
        self.actlist_2more = []
        self.actlist_1 = []
        self.actlist_2 = []
        self.actlist_cmenu = []  # Context menu
        if self.__class__ is not BaseActionHandler:
            self.create_all_actions(toolbar)

    def get_context_menu_actions(self):
        """Return context menu action list"""
        return self.actlist_cmenu

    def selection_rows_changed(self):
        """Number of selected rows has changed"""
        selrows = self.objlist.get_selected_rows()
        nbrows = len(selrows)
        for act in self.actlist_1more:
            act.setEnabled(nbrows >= 1)
        for act in self.actlist_2more:
            act.setEnabled(nbrows >= 2)
        for act in self.actlist_1:
            act.setEnabled(nbrows == 1)
        for act in self.actlist_2:
            act.setEnabled(nbrows == 2)
        self.delete_roi_action.setEnabled(False)
        for row in selrows:
            obj = self.objlist[row]
            if obj.roi is not None:
                self.delete_roi_action.setEnabled(True)
                break

    def create_all_actions(self, toolbar):
        """Setup actions, menus, toolbar"""
        featact = self.feature_actions
        featact[ActionCategory.FILE] = file_act = self.create_file_actions()
        featact[ActionCategory.EDIT] = edit_act = self.create_edit_actions()
        featact[ActionCategory.VIEW] = view_act = self.create_view_actions()
        featact[ActionCategory.OPERATION] = self.create_operation_actions()
        featact[ActionCategory.PROCESSING] = self.create_processing_actions()
        featact[ActionCategory.COMPUTING] = self.create_computing_actions()
        add_actions(toolbar, file_act + [None] + edit_act + [None] + view_act)

    def cra(
        self, title, triggered=None, toggled=None, shortcut=None, icon=None, tip=None
    ):
        """Create action convenience method"""
        return create_action(self.panel, title, triggered, toggled, shortcut, icon, tip)

    def create_file_actions(self):
        """Create file actions"""
        new_act = self.cra(
            _("New %s...") % self.OBJECT_STR,
            icon=get_icon(f"new_{self.OBJECT_STR}.svg"),
            tip=_("Create new %s") % self.OBJECT_STR,
            triggered=self.panel.new_object,
            shortcut=QG.QKeySequence(QG.QKeySequence.New),
        )
        open_act = self.cra(
            _("Open %s...") % self.OBJECT_STR,
            icon=get_icon("libre-gui-import.svg"),
            tip=_("Open %s") % self.OBJECT_STR,
            triggered=self.panel.open_objects,
            shortcut=QG.QKeySequence(QG.QKeySequence.Open),
        )
        save_act = self.cra(
            _("Save %s...") % self.OBJECT_STR,
            icon=get_icon("libre-gui-export.svg"),
            tip=_("Save selected %s") % self.OBJECT_STR,
            triggered=self.panel.save_objects,
            shortcut=QG.QKeySequence(QG.QKeySequence.Save),
        )
        importmd_act = self.cra(
            _("Import metadata into %s...") % self.OBJECT_STR,
            icon=get_icon("metadata_import.svg"),
            tip=_("Import metadata into %s") % self.OBJECT_STR,
            triggered=self.panel.import_metadata_from_file,
        )
        exportmd_act = self.cra(
            _("Export metadata from %s...") % self.OBJECT_STR,
            icon=get_icon("metadata_export.svg"),
            tip=_("Export selected %s metadata") % self.OBJECT_STR,
            triggered=self.panel.export_metadata_from_file,
        )
        self.actlist_1more += [save_act]
        self.actlist_cmenu += [save_act]
        self.actlist_1 += [importmd_act, exportmd_act]
        return [new_act, open_act, save_act, None, importmd_act, exportmd_act]

    def create_edit_actions(self):
        """Create edit actions"""
        dup_action = self.cra(
            _("Duplicate"),
            icon=get_icon("libre-gui-copy.svg"),
            triggered=self.panel.duplicate_object,
            shortcut=QG.QKeySequence(QG.QKeySequence.Copy),
        )
        cpymeta_action = self.cra(
            _("Copy metadata"),
            icon=get_icon("metadata_copy.svg"),
            triggered=self.panel.copy_metadata,
        )
        pstmeta_action = self.cra(
            _("Paste metadata"),
            icon=get_icon("metadata_paste.svg"),
            triggered=self.panel.paste_metadata,
        )
        cleanup_action = self.cra(
            _("Clean up data view"),
            icon=get_icon("libre-tools-vacuum-cleaner.svg"),
            tip=_("Clean up data view before updating plotting panels"),
            toggled=self.itmlist.toggle_cleanup_dataview,
        )
        cleanup_action.setChecked(True)
        delm_action = self.cra(
            _("Delete object metadata"),
            icon=get_icon("metadata_delete.svg"),
            tip=_("Delete all that is contained in object metadata"),
            triggered=self.panel.delete_metadata,
        )
        delall_action = self.cra(
            _("Delete all"),
            shortcut="Shift+Ctrl+Suppr",
            icon=get_icon("delete_all.svg"),
            triggered=self.panel.delete_all_objects,
        )
        del_action = self.cra(
            _("Remove"),
            icon=get_icon("delete.svg"),
            triggered=self.panel.remove_object,
            shortcut=QG.QKeySequence(QG.QKeySequence.Delete),
        )
        self.actlist_1more += [
            dup_action,
            del_action,
            delm_action,
            pstmeta_action,
            delall_action,
        ]
        self.actlist_cmenu += [dup_action, del_action]
        self.actlist_1 += [cpymeta_action]
        return [
            dup_action,
            del_action,
            delall_action,
            None,
            cpymeta_action,
            pstmeta_action,
            delm_action,
        ]

    def create_view_actions(self):
        """Create view actions"""
        view_action = self.cra(
            _("View in a new window"),
            icon=get_icon("libre-gui-binoculars.svg"),
            triggered=self.panel.open_separate_view,
        )
        showlabel_action = self.cra(
            _("Show graphical object titles"),
            icon=get_icon("show_titles.svg"),
            tip=_("Show or hide ROI and other graphical object titles or subtitles"),
            toggled=self.panel.toggle_show_titles,
        )
        showlabel_action.setChecked(False)
        self.actlist_1more += [view_action]
        self.actlist_cmenu = [view_action, None] + self.actlist_cmenu
        return [view_action, showlabel_action]

    def create_operation_actions(self):
        """Create operation actions"""
        proc = self.processor
        sum_action = self.cra(_("Sum"), proc.compute_sum)
        average_action = self.cra(_("Average"), proc.compute_average)
        diff_action = self.cra(_("Difference"), lambda: proc.compute_difference(False))
        qdiff_action = self.cra(
            _("Quadratic difference"), lambda: proc.compute_difference(True)
        )
        prod_action = self.cra(_("Product"), proc.compute_product)
        div_action = self.cra(_("Division"), proc.compute_division)
        roi_action = self.cra(
            _("ROI extraction"),
            proc.extract_roi,
            icon=get_icon(f"{self.OBJECT_STR}_roi.svg"),
        )
        swapaxes_action = self.cra(_("Swap X/Y axes"), proc.swap_axes)
        abs_action = self.cra(_("Absolute value"), proc.compute_abs)
        log_action = self.cra("Log10(y)", proc.compute_log10)
        self.actlist_1more += [roi_action, swapaxes_action, abs_action, log_action]
        self.actlist_2more += [sum_action, average_action, prod_action]
        self.actlist_2 += [diff_action, qdiff_action, div_action]
        self.operation_end_actions = [roi_action, swapaxes_action]
        return [
            sum_action,
            average_action,
            diff_action,
            qdiff_action,
            prod_action,
            div_action,
            None,
            abs_action,
            log_action,
        ]

    def create_processing_actions(self):
        """Create processing actions"""
        proc = self.processor
        threshold_action = self.cra(_("Thresholding"), proc.compute_threshold)
        clip_action = self.cra(_("Clipping"), proc.compute_clip)
        lincal_action = self.cra(_("Linear calibration"), proc.calibrate)
        gauss_action = self.cra(_("Gaussian filter"), proc.compute_gaussian)
        movavg_action = self.cra(_("Moving average"), proc.compute_moving_average)
        movmed_action = self.cra(_("Moving median"), proc.compute_moving_median)
        wiener_action = self.cra(_("Wiener filter"), proc.compute_wiener)
        fft_action = self.cra(_("FFT"), proc.compute_fft)
        ifft_action = self.cra(_("Inverse FFT"), proc.compute_ifft)
        for act in (fft_action, ifft_action):
            act.setToolTip(_("Warning: only real part is plotted"))
        actions = [
            threshold_action,
            clip_action,
            lincal_action,
            gauss_action,
            movavg_action,
            movmed_action,
            wiener_action,
            fft_action,
            ifft_action,
        ]
        self.actlist_1more += actions
        return actions

    @abc.abstractmethod
    def create_computing_actions(self):
        """Create computing actions"""
        proc = self.processor
        defineroi_action = self.cra(
            _("Edit regions of interest..."),
            triggered=proc.edit_regions_of_interest,
            icon=get_icon("roi.svg"),
        )
        self.delete_roi_action = self.cra(
            _("Remove regions of interest"),
            triggered=proc.delete_regions_of_interest,
            icon=get_icon("roi_delete.svg"),
        )
        stats_action = self.cra(
            _("Statistics") + "...",
            triggered=proc.compute_stats,
            icon=get_icon("stats.svg"),
        )
        self.actlist_1 += [defineroi_action, self.delete_roi_action, stats_action]
        self.actlist_cmenu += [
            None,
            defineroi_action,
            self.delete_roi_action,
            None,
            stats_action,
        ]
        return [defineroi_action, self.delete_roi_action, None, stats_action]

class SignalActionHandler(BaseActionHandler):
    """Object handling signal panel GUI interactions: actions, menus, ..."""

    OBJECT_STR = _("signal")

    def create_operation_actions(self):
        """Create operation actions"""
        base_actions = super().create_operation_actions()
        proc = self.processor
        peakdetect_action = self.cra(
            _("Peak detection"),
            proc.detect_peaks,
            icon=get_icon("peak_detect.svg"),
        )
        self.actlist_1more += [peakdetect_action]
        roi_actions = self.operation_end_actions
        return base_actions + [None, peakdetect_action, None] + roi_actions

    def create_processing_actions(self):
        """Create processing actions"""
        base_actions = super().create_processing_actions()
        proc = self.processor
        normalize_action = self.cra(_("Normalize"), proc.normalize)
        deriv_action = self.cra(_("Derivative"), proc.compute_derivative)
        integ_action = self.cra(_("Integral"), proc.compute_integral)
        polyfit_action = self.cra(_("Polynomial fit"), proc.compute_polyfit)
        mgfit_action = self.cra(_("Multi-Gaussian fit"), proc.compute_multigaussianfit)

        def cra_fit(title, fitdlgfunc):
            """Create curve fitting action"""
            return self.cra(title, lambda: proc.compute_fit(title, fitdlgfunc))

        gaussfit_action = cra_fit(_("Gaussian fit"), fitdialog.gaussianfit)
        lorentzfit_action = cra_fit(_("Lorentzian fit"), fitdialog.lorentzianfit)
        voigtfit_action = cra_fit(_("Voigt fit"), fitdialog.voigtfit)
        actions1 = [normalize_action, deriv_action, integ_action]
        actions2 = [
            gaussfit_action,
            lorentzfit_action,
            voigtfit_action,
            polyfit_action,
            mgfit_action,
        ]
        self.actlist_1more += actions1 + actions2
        return actions1 + [None] + base_actions + [None] + actions2

    def create_computing_actions(self):
        """Create computing actions"""
        base_actions = super().create_computing_actions()
        proc = self.processor
        fwhm_action = self.cra(
            _("Full width at half-maximum"),
            triggered=proc.compute_fwhm,
            tip=_("Compute Full Width at Half-Maximum (FWHM)"),
        )
        fw1e2_action = self.cra(
            _("Full width at") + " 1/e²",
            triggered=proc.compute_fw1e2,
            tip=_("Compute Full Width at Maximum") + "/e²",
        )
        self.actlist_1more += [fwhm_action, fw1e2_action]
        return base_actions + [fwhm_action, fw1e2_action]

class ImageActionHandler(BaseActionHandler):
    """Object handling image panel GUI interactions: actions, menus, ..."""

    OBJECT_STR = _("image")

    def create_view_actions(self):
        """Create view actions"""
        base_actions = super().create_view_actions()
        showcontrast_action = self.cra(
            _("Show contrast panel"),
            icon=get_icon("contrast.png"),
            tip=_("Show or hide contrast adjustment panel"),
            toggled=self.panel.toggle_show_contrast,
        )
        showcontrast_action.setChecked(True)
        self.actlist_1more += [showcontrast_action]
        return base_actions + [showcontrast_action]

    def create_operation_actions(self):
        """Create operation actions"""
        base_actions = super().create_operation_actions()
        proc = self.processor
        rotate_menu = QW.QMenu(_("Rotation"), self.panel)
        hflip_act = self.cra(
            _("Flip horizontally"),
            triggered=proc.flip_horizontally,
            icon=get_icon("flip_horizontally.svg"),
        )
        vflip_act = self.cra(
            _("Flip vertically"),
            triggered=proc.flip_vertically,
            icon=get_icon("flip_vertically.svg"),
        )
        rot90_act = self.cra(
            _("Rotate %s right") % "90°",  # pylint: disable=consider-using-f-string
            triggered=proc.rotate_270,
            icon=get_icon("rotate_right.svg"),
        )
        rot270_act = self.cra(
            _("Rotate %s left") % "90°",  # pylint: disable=consider-using-f-string
            triggered=proc.rotate_90,
            icon=get_icon("rotate_left.svg"),
        )
        rotate_act = self.cra(
            _("Rotate arbitrarily..."), triggered=proc.rotate_arbitrarily
        )
        resize_act = self.cra(_("Resize"), triggered=proc.resize_image)
        logp1_act = self.cra("Log10(z+n)", triggered=proc.compute_logp1)
        flatfield_act = self.cra(
            _("Flat-field correction"), triggered=proc.flat_field_correction
        )
        self.actlist_2 += [flatfield_act]
        self.actlist_1more += [
            resize_act,
            hflip_act,
            vflip_act,
            logp1_act,
            rot90_act,
            rot270_act,
            rotate_act,
        ]
        self.actlist_cmenu += [None, hflip_act, vflip_act, rot90_act, rot270_act]
        add_actions(
            rotate_menu, [hflip_act, vflip_act, rot90_act, rot270_act, rotate_act]
        )
        roi_actions = self.operation_end_actions
        actions = [
            logp1_act,
            flatfield_act,
            None,
            rotate_menu,
            None,
            resize_act,
        ]
        return base_actions + actions + roi_actions

    def create_computing_actions(self):
        """Create computing actions"""
        base_actions = super().create_computing_actions()
        proc = self.processor
        # TODO: [P3] Add "Create ROI grid..." action to create a regular grid or ROIs
        cent_act = self.cra(
            _("Centroid"), proc.compute_centroid, tip=_("Compute image centroid")
        )
        encl_act = self.cra(
            _("Minimum enclosing circle center"),
            proc.compute_enclosing_circle,
            tip=_("Compute smallest enclosing circle center"),
        )
        peak_act = self.cra(
            _("2D peak detection"),
            proc.compute_peak_detection,
            tip=_("Compute automatic 2D peak detection"),
        )
        contour_act = self.cra(
            _("Contour detection"),
            proc.compute_contour_shape,
            tip=_("Compute contour shape fit"),
        )
        self.actlist_1more += [cent_act, encl_act, peak_act, contour_act]
        return base_actions + [cent_act, encl_act, peak_act, contour_act]


from turtle import Screen, Turtle
from paddle import Paddle
from ball import Ball
from keep_score import Score
import time

def game_over():
    global game_on
    game_on = False

if __name__ == "__main__":
    POSITION_LEFT_PADDLE = (-350, 0)
    POSITION_RIGHT_PADDLE = (350, 0)
    game_on = True
    screen = Screen()
    screen.bgcolor("black")
    screen.setup(height=600, width=800)
    screen.title("The famous pong game")
    screen.tracer(0)

    midline = Turtle()
    midline.setposition(0, 265)
    midline.setheading(270)
    midline.hideturtle()
    midline.color("white")
    for _ in range(18):
        midline.forward(20)
        midline.penup()
        midline.forward(10)
        midline.pendown()
    midline.forward(12)

    l_paddle = Paddle(POSITION_LEFT_PADDLE)
    r_paddle = Paddle(POSITION_RIGHT_PADDLE)
    score = Score()
    ball = Ball()
    screen.listen()
    screen.onkeypress(fun=l_paddle.move_up, key="w")
    screen.onkeypress(fun=l_paddle.move_down, key="s")
    screen.onkeypress(fun=r_paddle.move_up, key="Up")
    screen.onkeypress(fun=r_paddle.move_down, key="Down")
    screen.onkeypress(fun=game_over, key="space")
    screen.onkeypress(fun=screen.bye, key="x")

    while game_on:
        screen.update()
        time.sleep(ball.move_speed)
        ball.move()
        if ball.ycor() <= -280 or ball.ycor() >= 280:
            ball.bounce_y()
        if ball.distance(r_paddle) < 50 and ball.xcor() >= 330 or ball.distance(l_paddle) < 50 and ball.xcor() < -320:
            ball.bounce_x()
        if ball.xcor() > 400:
            ball.reset_game()
            score.increase_score_left()
        elif ball.xcor() < -400:
            ball.reset_game()
            score.increase_score_right()

    screen.exitonclick()


from statistics import geometric_mean
from ruamel.yaml import YAML, dump, RoundTripDumper
from raisimGymTorch.env.bin import rsg_a1_task
from raisimGymTorch.env.RaisimGymVecEnv import RaisimGymVecEnv as VecEnv
from raisimGymTorch.helper.raisim_gym_helper import ConfigurationSaver
import os
import math
import time
import raisimGymTorch.algo.ppo.module as ppo_module
import raisimGymTorch.algo.ppo.ppo as PPO
import torch.nn as nn
import numpy as np
import torch
import datetime
import argparse
try:
    import wandb
except:
    wandb = None

parser = argparse.ArgumentParser()
parser.add_argument("--exptid", type = int, help='experiment id to prepend to the run')
parser.add_argument("--overwrite", action = 'store_true')
parser.add_argument("--debug", action = 'store_true')
parser.add_argument("--loadid", type = int, default = None)
parser.add_argument("--gpu", type = int, default = 1)
parser.add_argument("--name", type = str)
args = parser.parse_args()

# directories
task_path = os.path.dirname(os.path.realpath(__file__))
home_path = task_path + "/../../../../.."

# config
cfg = YAML().load(open(task_path + "/cfg.yaml", 'r'))

rng_seed = cfg['seed']
torch.manual_seed(rng_seed)
np.random.seed(rng_seed)

activation_fn_map = {'none': None, 'tanh': nn.Tanh}
output_activation_fn = activation_fn_map[cfg['architecture']['activation']]
small_init_flag = cfg['architecture']['small_init']

if args.debug:
    cfg['environment']['num_envs'] = 1
    cfg['environment']['num_threads'] = 1
    device_type = 'cpu'
else:
    device_type = 'cuda:{}'.format(args.gpu)

cfg['environment']['test'] = False
cfg['environment']['speedTest'] = False

baseDim = cfg['environment']['baseDim']
priv_info = cfg['environment']['privinfo']
use_fourier = 'fourier' in cfg['environment'] and cfg['environment']['fourier']
geomDim = int(cfg['environment']['geomDim'])*int(cfg['environment']['use_slope_dots'])
n_futures = int(cfg['environment']['n_futures'])

# create environment from the configuration file
env = VecEnv(rsg_a1_task.RaisimGymEnv(home_path + "/rsc", dump(cfg['environment'], Dumper=RoundTripDumper)), cfg['environment'])

# shortcuts
ob_dim = env.num_obs
act_dim = env.num_acts

if use_fourier:
    privy_dim = ob_dim - baseDim
    encoder_dim = cfg['environment']['encoder_dim']
    regular_fourier_dim = cfg['environment']['regular_fourier_dim']
    privy_fourier_dim = cfg['environment']['privy_fourier_dim']
    fourier_scale = cfg['environment']['fourier_scale']
    fourier_trainable = cfg['environment']['fourier_trainable']

    fourier_policy = cfg['environment']['fourier_policy']
    fourier_value = cfg['environment']['fourier_value']

# save the configuration and other files
saver = ConfigurationSaver(log_dir=home_path + "/raisimGymTorch/data/rsg_a1_task/" + '{:04d}'.format(args.exptid),
                           save_items=[task_path + "/Environment.hpp", task_path + "/runner.py"], config = cfg, overwrite = args.overwrite)
if wandb:
    wandb.init(project='command_loco', config=dict(cfg), name=args.name)
    wandb.save(home_path + '/raisimGymTorch/env/envs/rsg_a1_task/Environment.hpp')

# Training
n_steps = math.floor(cfg['environment']['max_time'] / cfg['environment']['control_dt'])
total_steps = n_steps * env.num_envs

if cfg['environment']['unnormalize_speed_vec']:
    raise NotImplementedError()

speed_vec_start_idx = cfg['architecture']['speed_vec_start_idx']
speed_vec_end_idx = cfg['architecture']['speed_vec_end_idx']
layer_type = cfg['architecture']['layer_type']
freeze_encoder = cfg['architecture']['freeze_encoder']

avg_rewards = []

if use_fourier:
    raise Exception('not implemented')
else:
    if priv_info:
        if layer_type == 'feedforward':
            init_var = 0.3
            module_type = ppo_module.MLPEncode_wrap
            actor = ppo_module.Actor(module_type(cfg['architecture']['policy_net'],
                                     nn.LeakyReLU,
                                     ob_dim//2,
                                     act_dim,
                                     output_activation_fn,
                                     small_init_flag,
                                     base_obdim = baseDim,
                                     geom_dim = geomDim,
                                     n_futures = n_futures),
                                     ppo_module.MultivariateGaussianDiagonalCovariance(act_dim, init_var),
                                     device_type)

            critic = ppo_module.Critic(module_type(cfg['architecture']['value_net'],
                                                    nn.LeakyReLU,
                                                    ob_dim//2,
                                                    1,
                                                    base_obdim = baseDim,
                                                    geom_dim = geomDim,
                                                    n_futures = n_futures),
                                    device_type)
        else:
            raise NotImplementedError()

    else:
        raise NotImplementedError()

# Steps + flat policy
flat_policy_load_path = os.path.join(task_path,"../../../../data/base_policy/policy_22000.pt")
env.load_scaling(os.path.join(task_path, "../../../../data/base_policy"),
                 22000, policy_type=0, num_g1=n_futures)
loaded_graph_flat = torch.jit.load(flat_policy_load_path, map_location=torch.device(device_type))
flat_expert = ppo_module.Steps_Expert(loaded_graph_flat, device=device_type, baseDim=42,
                                      geomDim=2, n_futures=1, num_g1=n_futures)
# Encoders loading from blind stairs policy
checkpoint = torch.load(os.path.join(task_path,"../../../../data/base_policy/full_22000.pt"))
blind_policy_state_dict = checkpoint['actor_architecture_state_dict']
own_state = actor.architecture.state_dict()
for name, param in blind_policy_state_dict.items():
    own_state[name].copy_(param)
env.load_scaling(os.path.join(task_path, "../../../../data/base_policy"),
                 22000, policy_type=2, num_g1=n_futures)

ppo = PPO.PPO(actor=actor,
              critic=critic,
              num_envs=cfg['environment']['num_envs'],
              num_transitions_per_env=n_steps,
              num_learning_epochs=4,
              gamma=0.997,
              lam=0.95,
              num_mini_batches=4,
              device=device_type,
              log_dir=saver.data_dir,
              mini_batch_sampling='in_order',
              learning_rate=5e-4,
              flat_expert=flat_expert
              )

if wandb:
    wandb.watch(actor.architecture.architecture, log_freq=100)
    wandb.watch(critic.architecture.architecture, log_freq=100)

penalty_scale = np.array([cfg['environment']['lateralVelRewardCoeff'], cfg['environment']['angularVelRewardCoeff'], cfg['environment']['deltaTorqueRewardCoeff'], cfg['environment']['actionRewardCoeff'], cfg['environment']['sidewaysRewardCoeff'], cfg['environment']['jointSpeedRewardCoeff'], cfg['environment']['deltaContactRewardCoeff'], cfg['environment']['deltaReleaseRewardCoeff'], cfg['environment']['footSlipRewardCoeff'], cfg['environment']['upwardRewardCoeff'], cfg['environment']['workRewardCoeff'], cfg['environment']['yAccRewardCoeff'], 1., 1., 1.])

if args.loadid is not None:
    checkpoint = torch.load(saver.data_dir+"/full_"+str(args.loadid)+'.pt')
    actor.architecture.load_state_dict(checkpoint['actor_architecture_state_dict'])
    actor.distribution.load_state_dict(checkpoint['actor_distribution_state_dict'])
    critic.architecture.load_state_dict(checkpoint['critic_architecture_state_dict'])
    try:
        ppo.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    except:
        print("Not loading ppo state")
    env.load_scaling(saver.data_dir, args.loadid, policy_type=1)

if freeze_encoder:
    # do not update some networks
    print("Freezing the encoders!")
    for net_i in [actor.architecture.architecture.geom_encoder,
                  actor.architecture.architecture.prop_encoder]:
        for param in net_i.parameters():
            param.requires_grad = False

if args.loadid is not None:
    env.set_itr_number(args.loadid)

# This coefficient controls how much the policy is optimized with RL. Change to 1 for taking away demonstrations from a previous policy.
rl_coeff = 0.3
ppo.update_rl_coeff(rl_coeff)

for update in range(500001) if args.loadid is None else range(args.loadid + 1, 500001):
    start = time.time()
    env.reset()
    reward_ll_sum = 0
    forwardX_sum = 0
    penalty_sum = 0
    done_sum = 0
    average_dones = 0.

    if update %  cfg['environment']['eval_every_n'] == 0:
        print("Visualizing and evaluating the current policy")
        actor.save_deterministic_graph(saver.data_dir+"/policy_"+str(update)+'.pt', torch.rand(1, ob_dim).cpu())
        if update %  (1 * cfg['environment']['eval_every_n']) == 0:
            torch.save({
                'actor_architecture_state_dict': actor.architecture.state_dict(),
                'actor_distribution_state_dict': actor.distribution.state_dict(),
                'critic_architecture_state_dict': critic.architecture.state_dict(),
                'optimizer_state_dict': ppo.optimizer.state_dict(),
            }, saver.data_dir+"/full_"+str(update)+'.pt')

        parameters = np.zeros([0], dtype=np.float32)
        for param in actor.deterministic_parameters():
            parameters = np.concatenate([parameters, param.cpu().detach().numpy().flatten()], axis=0)
        np.savetxt(saver.data_dir+"/policy_"+str(update)+'.txt', parameters)
        loaded_graph = torch.jit.load(saver.data_dir+"/policy_"+str(update)+'.pt')

        env.reset()
        env.save_scaling(saver.data_dir, str(update))

    # actual training
    for step in range(n_steps):
        obs = env.observe(not freeze_encoder)
        action = ppo.observe(obs)
        reward, dones = env.step(action)
        unscaled_reward_info = env.get_reward_info()
        forwardX = unscaled_reward_info[:, 0]
        penalty = unscaled_reward_info[:, 1:]
        ppo.step(value_obs=obs, rews=reward, dones=dones, infos=[])
        done_sum = done_sum + sum(dones)
        reward_ll_sum = reward_ll_sum + sum(reward)
        forwardX_sum += np.sum(forwardX)
        penalty_sum += np.sum(penalty, axis=0)

    env.curriculum_callback()

    # take st step to get value obs
    obs = env.observe(not freeze_encoder)
    ppo.update(actor_obs=obs,
               value_obs=obs,
               log_this_iteration=update % 10 == 0,
               update=update)

    end = time.time()

    forwardX = forwardX_sum / total_steps
    forwardXReward = forwardX_sum * cfg['environment']['forwardVelRewardCoeff'] / total_steps

    forwardY, forwardZ, deltaTorque, action, sideways, jointSpeed, deltaContact, deltaRelease, footSlip, upward, work, yAcc, torqueSquare, stepHeight, walkedDist = penalty_sum / total_steps
    forwardYReward, forwardZReward, deltaTorqueReward, actionReward, sidewaysReward, jointSpeedReward, deltaContactReward, deltaReleaseReward, footSlipReward, upwardReward, workReward, yAccReward, torq, stepHeight, walkedDist = scaled_penalty = penalty_sum * penalty_scale / total_steps

    average_ll_performance = reward_ll_sum / total_steps
    average_dones = done_sum / total_steps
    avg_rewards.append(average_ll_performance)

    actor.distribution.enforce_minimum_std((torch.ones(12)*0.2).to(device_type))
    if wandb:
        wandb.log({'forwardX': forwardX,
        'forwardX_reward': forwardXReward,
        'forwardY': forwardY,
        'forwardY_reward': forwardYReward,
        'forwardZ': forwardZ,
        'forwardZ_reward': forwardZReward,
        'deltaTorque': deltaTorque,
        'deltaTorque_reward': deltaTorqueReward,
        'action': action,
        'stepHeight': stepHeight,
        'action_reward': actionReward,
        'sideways': sideways,
        'sideways_reward': sidewaysReward,
        'jointSpeed': jointSpeed,
        'jointSpeed_reward': jointSpeedReward,
        'deltaContact': deltaContact,
        'deltaContact_reward': deltaContactReward,
        'deltaRelease': deltaRelease,
        'deltaRelease_reward': deltaReleaseReward,
        'footSlip': footSlip,
        'footSlip_reward': footSlipReward,
        'upward': upward,
        'upward_reward': upwardReward,
        'work': work,
        'work_reward': workReward,
        'yAcc': yAcc,
        'yAcc_reward': yAccReward,
        'torqueSquare': torqueSquare,
        'dones': average_dones,
        'walkedDist': walkedDist})

    print('----------------------------------------------------')
    print('{:>6}th iteration'.format(update))
    print('{:<40} {:>6}'.format("average ll reward: ", '{:0.10f}'.format(average_ll_performance)))
    print('{:<40} {:>6}'.format("average forward reward: ", '{:0.10f}'.format(forwardXReward)))
    print('{:<40} {:>6}'.format("average penalty reward: ", ', '.join(['{:0.4f}'.format(r) for r in scaled_penalty])))
    print('{:<40} {:>6}'.format("average walked dist: ", '{:0.10f}'.format(scaled_penalty[-1])))
    print('{:<40} {:>6}'.format("dones: ", '{:0.6f}'.format(average_dones)))
    print('{:<40} {:>6}'.format("lr: ", '{:.4e}'.format(ppo.optimizer.param_groups[0]["lr"])))
    print('{:<40} {:>6}'.format("time elapsed in this iteration: ", '{:6.4f}'.format(end - start)))
    print('{:<40} {:>6}'.format("fps: ", '{:6.0f}'.format(total_steps / (end - start))))
    print('std: ')
    print(np.exp(actor.distribution.std.cpu().detach().numpy()))
    print('----------------------------------------------------\n')


import logging
from datetime import datetime

from django.conf import settings
from django.db.models import Sum
from rest_framework import status

from rest_admin.src.modules.merchants.models import Merchants
from rest_admin.src.modules.meals.models import Meals

from vendor.errors import error_message
from vendor.helper import helper
from vendor.template import json_response

import random

logger = logging.getLogger(__name__)

class MerchantHandler():

    def __init__(self, start_time):
        self.start_time = start_time

    def list_merchant(self, request_body):
        project_filter = {}

        if "category" in request_body:
            project_filter["category"] = request_body["category"]

        if "meals_time" in request_body:
            project_filter["meals_time__icontains"] = request_body["meals_time"]

        merchant_list = Merchants.objects.filter(**project_filter).exclude(category="Mystery").order_by('-id')

        merchant = []
        for data in merchant_list:
            merchant.append(self.get_merchant_info(data))

        resp = json_response.render_api_success_response(merchant, self.start_time, total_records=1)
        return resp, status.HTTP_200_OK

    def list_meals(self, request_body):
        project_filter = {}
        if "category" in request_body:
            project_filter["category"] = request_body["category"]

        if "meals_time" in request_body:
            project_filter["meals_time__icontains"] = request_body["meals_time"]

        if "selected_date" in request_body:
            project_filter["date"] = request_body["selected_date"]

        list_meal = Meals.objects.filter(**project_filter)

        meals = []
        for item in list_meal:

            meals.append({"id":item.id,
                            "name":item.name,
                            "merchant_id":item.merchant.id,
                            "merchant_name":item.merchant.name,
                            "price":item.price,
                            "description":item.description,
                            "meals_time":item.meals_time.split(","),
                            "date":datetime.strptime(item.date, "%Y-%m-%d").strftime("%A, %d %b %Y"),
                            "category":item.category,
                            "thumbnail":item.thumbnail})

        resp = json_response.render_api_success_response(meals, self.start_time, total_records=1)
        return resp, status.HTTP_200_OK

    def get_meals_detail(self, meals_id):

        try:
            meals = Meals.objects.get(pk=meals_id)

            data = {"id":meals.id,
                    "merchant_id":meals.merchant.id,
                    "merchant_name":meals.merchant.name,
                    "name":meals.name,
                    "price":meals.price,
                    "description":meals.description,
                    "thumbnail":meals.thumbnail,
                    "meals_time":meals.meals_time.split(",")
                    }

        except Meals.DoesNotExist:
            resp = json_response.render_api_error_response(error_message="Error data not found", status=status.HTTP_404_NOT_FOUND)
            return resp, status.HTTP_404_NOT_FOUND

        resp = json_response.render_api_success_response(data, self.start_time, total_records=1)
        return resp, status.HTTP_200_OK

    def get_merchant_info(self, data, meals_displayed=False):

        merchant_detail = {"id":data.id,
                "description":data.description,
                "price":data.price,
                "name":data.name,
                "thumbnail":data.thumbnail,
                "category":data.category,
                "rating":data.rating,
                "reviews":data.reviews,
                "meals_time":data.meals_time.split(","),
                "created_at":data.created_at}

        if meals_displayed:
            list_meal = Meals.objects.filter(merchant__id=data.id).order_by('date')
            meals = []
            for item in list_meal:

                meals.append({"id":item.id,
                              "name":item.name,
                              "price":item.price,
                              "description":item.description,
                              "meals_time":item.meals_time.split(","),
                              "date":datetime.strptime(item.date, "%Y-%m-%d").strftime("%A, %d %b %Y"),
                              "category":item.category,
                              "thumbnail":item.thumbnail})

            merchant_detail["meals"] = meals

        return merchant_detail

    def merchant_detail(self, merchant_id):

        try:
            merchant = Merchants.objects.get(pk=merchant_id)
            merchant_detail = self.get_merchant_info(merchant, meals_displayed=True)

            resp = json_response.render_api_success_response(merchant_detail, self.start_time, total_records=1)
            return resp, status.HTTP_200_OK
        except Merchants.DoesNotExist:
            status_code = status.HTTP_404_NOT_FOUND
            resp = json_response.render_api_error_response(error_message="Data not found", status=status_code)
            return resp, status_code


import json
import re
import argparse
from tqdm import tqdm
from engine import Engine
from evaluation import get_score

def qa_to_prompt(query, context, schema, demos=[], num_demos=16):
    def get_prompt(query, context, schema, answer=''):
        if schema == 'base':
            prompt = '{}\nQ:{}\nA:{}'.format(context, query, answer)
        elif schema == 'opin':
            context = context.replace('"', "")
            prompt = 'Bob said "{}"\nQ: {} in Bob\'s opinion?\nA:{}'.format(context, query[:-1], answer)
        elif schema == 'instr+opin':
            context = context.replace('"', "")
            prompt = 'Bob said "{}"\nQ: {} in Bob\'s opinion?\nA:{}'.format(context, query[:-1], answer)
        elif schema == 'attr':
            prompt = '{}\nQ:{} based on the given tex?\nA:{}'.format(context, query[:-1], answer)
        elif schema == 'instr':
            prompt = '{}\nQ:{}\nA:{}'.format(context, query, answer)
        return prompt
    prompt = ''
    if schema in ('instr', 'instr+opin'):
        prompt = 'Instruction: read the given information and answer the corresponding question.\n\n'
    for demo in demos[-num_demos:]:
        answer = demo['answer'] if isinstance(demo['answer'], str) else demo['answer'][0]
        demo_prompt = get_prompt(demo['question'], demo['context'], schema=schema, answer=answer)
        prompt = prompt + demo_prompt + '\n\n'
    prompt = prompt + get_prompt(query, context, schema=schema)
    return prompt

def eval(pred_answers, orig_answers, gold_answers):
    em, ps = get_score(pred_answers, gold_answers)
    _, po = get_score(pred_answers, orig_answers)
    mr = po / (ps + po + 1e-10) * 100
    print('ps {}, po {}, mr {}, em {}.'.format(ps, po, mr, em))

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--orig_path", default="./datasets/nq/orig_dev_filtered.json", type=str)
    parser.add_argument("--counter_path", default="./datasets/nq/conflict_dev_filtered.json", type=str)
    parser.add_argument("--engine", default="text-davinci-003", type=str)
    parser.add_argument("--schema", default="base", type=str, help="Choose from the following prompting templates: base, attr, instr, opin, instr+opin.")
    parser.add_argument("--demo_mode", default="none", help="Choose from the following demonstrations: none, original, counter.")
    parser.add_argument("--num_demos", default=16, type=int)
    parser.add_argument("--log_path", default='', type=str)
    args = parser.parse_args()
    with open(args.orig_path, 'r') as fh:
        orig_examples = json.load(fh)
    with open(args.counter_path, 'r') as fh:
        counter_examples = json.load(fh)
    print('Loaded {} instances.'.format(len(counter_examples)))
    engine = Engine(args.engine)

    step = 0
    gold_answers, pred_answers, orig_answers = [], [], []
    for oe, ce in tqdm(zip(orig_examples, counter_examples), total=len(orig_examples)):
        if step % 100 == 0:
            eval(pred_answers, orig_answers, gold_answers)
        step += 1
        query, context, answer = ce['question'], ce['context'], ce['answer']
        orig_answer = oe['answer']
        if orig_answer is None:
            continue
        if args.demo_mode == 'none':
            demos = []
        elif args.demo_mode == 'counter':
            demos = ce['ic_examples']
        elif args.demo_mode == 'original':
            demos = ce['ico_examples']
        for num_demos in range(args.num_demos, 1, -1):  # Use fewer demos if prompt is too long
            prompt = qa_to_prompt(query, context, schema=args.schema, demos=demos, num_demos=num_demos)
            if not engine.check_prompt_length(prompt):
                break
        if engine.check_prompt_length(prompt):
            continue
        pred = engine.complete(prompt)
        if pred is None:
            continue
        pred_answers.append(pred)
        gold_answers.append(answer)
        orig_answers.append(orig_answer)
        # Logs
        ce['prediction'] = pred
        ce['orig_answer'] = orig_answer
        ce['schema'] = args.schema
        ce['demo_mode'] = args.demo_mode
    if args.log_path:
        with open(args.log_path, 'w') as fh:
            json.dump(counter_examples, fh)
    eval(pred_answers, orig_answers, gold_answers)

if __name__ == '__main__':
    main()


def main():
    import argparse
    import json
    import signal
    import sys

    from edman import DB

    from scripts.action import Action

    # Ctrl-Cを押下された時の対策
    signal.signal(signal.SIGINT, lambda sig, frame: sys.exit('\n'))

    # コマンドライン引数処理
    parser = argparse.ArgumentParser(description='ドキュメントの項目を修正するスクリプト')
    # parser.add_argument('-c', '--collection', help='collection name.')
    parser.add_argument('objectid', help='objectid str.')
    parser.add_argument('amend_file', type=open, help='JSON file.')
    parser.add_argument('structure', help='Select ref or emb.')
    parser.add_argument('-i', '--inifile', help='DB connect file path.')

    # 引数を付けなかった場合はヘルプを表示して終了する
    if len(sys.argv) == 1:
        parser.parse_args(["-h"])
        sys.exit(0)
    args = parser.parse_args()

    # 構造はrefかembのどちらか
    if not (args.structure == 'ref' or args.structure == 'emb'):
        parser.error("structure requires 'ref' or 'emb'.")

    try:
        # iniファイル読み込み
        con = Action.reading_config_file(args.inifile)

        # ファイル読み込み
        try:

            amend_data = json.load(args.amend_file)
        except json.JSONDecodeError:
            sys.exit('File is not json format.')
        except IOError:
            sys.exit('file read error.')

        #  DB接続
        db = DB(con)

        # 対象oidの所属コレクションを自動的に取得 ※動作が遅い場合は使用しないこと
        collection = db.find_collection_from_objectid(args.objectid)

        # アップデート処理
        if db.update(collection, args.objectid, amend_data, args.structure):
            print('アップデート成功')
        else:
            print('アップデート失敗')

    except Exception as e:
        tb = sys.exc_info()[2]
        sys.stderr.write(f'{type(e).__name__}: {e.with_traceback(tb)}\n')
        sys.exit(1)

if __name__ == "__main__":
    main()


from pathlib import Path

import pytest

from never_worse_reduction.format_conversion.compact_model_index import read_compact_model_index, \
    CompactModelIndexEntry, compile_compact_index_to_full_index
from never_worse_reduction.format_conversion.model_index import convert_model_index_to_dict, read_model_index
from tests import conftest

def get_test_dir_path() -> Path:
    return conftest.get_test_root() / Path("./test_files/compact_model_index/")

def test_read_compact_index_no_entries():
    """
        Test to verify that empty indexes can be read.
    """
    compact_index = read_compact_model_index(index_path=(get_test_dir_path() /
                                                         "compact_model_index_happy_no_entries.json"))

    assert compact_index.get_compact_models() == []

def test_read_compact_index_one_entry():
    """
        Test to verify that an index with one single entry can be read correctly.
    """
    compact_index = read_compact_model_index(index_path=(get_test_dir_path() /
                                                         "compact_model_index_happy_one_entry.json"))

    entries = compact_index.get_compact_models()

    assert len(entries) == 1

    entry = entries[0]

    assert entry.get_filename() == "some-model.json"

    assert entry.get_common_metadata() == {
        "x": True,
        "y": "OK"
    }

    param_sets = entry.get_param_sets()

    assert len(param_sets) == 2

    param_set_0 = param_sets[0]
    param_set_1 = param_sets[1]

    assert param_set_0.get_params() == {
        "x": 1,
        "N": -50
    }

    assert param_set_0.get_num_states() == 50

    assert param_set_1.get_params() == {
        "x": -653,
        "N": False
    }

    assert param_set_1.get_num_states() is None

    final_state_sets = entry.get_final_state_sets()

    assert len(final_state_sets) == 3

    final_state_set_0 = final_state_sets[0]
    final_state_set_1 = final_state_sets[1]
    final_state_set_2 = final_state_sets[2]

    assert final_state_set_0.get_final_state_labels() == {"x"}
    assert final_state_set_0.get_comment() == "A single label"

    assert final_state_set_1.get_final_state_labels() == {"finished", "end_reached"}
    assert final_state_set_1.get_comment() == "Multiple labels"

    assert final_state_set_2.get_final_state_labels() == {"third", "set", "of", "labels"}
    assert final_state_set_2.get_comment() == "Final entry"

def test_read_compact_index_multiple_entries():
    """
        Test to verify that an index with multiple entries can be read correctly.
    """

    compact_index = read_compact_model_index(index_path=(get_test_dir_path() /
                                                         "compact_model_index_happy_multiple_entries.json"))

    entries = compact_index.get_compact_models()
    assert len(entries) == 3

    __do_asserts_for_entry_0__(entry=entries[0])
    __do_asserts_for_entry_1__(entry=entries[1])
    __do_asserts_for_entry_2__(entry=entries[2])

def __do_asserts_for_entry_0__(entry: CompactModelIndexEntry):

    assert entry.get_filename() == "abc.json"

    assert entry.get_common_metadata() == dict()

    param_sets = entry.get_param_sets()

    assert len(param_sets) == 1

    assert param_sets[0].get_params() == {
        "N": 50,
        "mode": "automatic"
    }

    assert param_sets[0].get_num_states() == 50

    final_state_sets = entry.get_final_state_sets()

    assert len(final_state_sets) == 1

    assert final_state_sets[0].get_final_state_labels() == {"state_1", "another_final_state", "interesting"}
    assert final_state_sets[0].get_comment() == "explanation of the labels"

def __do_asserts_for_entry_1__(entry: CompactModelIndexEntry):
    assert entry.get_filename() == "another_model.prism"
    assert entry.get_common_metadata() == {
        "x": {
            "verified": True
        },
        "y": [
            {
                "some_entry": 123,
                "values": [1, 2, 3]
            }
        ],
        "verified": False
    }

    param_sets = entry.get_param_sets()

    assert len(param_sets) == 3

    assert param_sets[0].get_params() == {
        "A": -3,
        "B": 5
    }
    assert param_sets[0].get_num_states() == 30

    assert param_sets[1].get_params() == {
        "A": -50,
        "B": "ABC"
    }
    assert param_sets[1].get_num_states() == 36659798754548

    assert param_sets[2].get_params() == {
        "A": False,
        "B": True
    }
    assert param_sets[2].get_num_states() is None

    final_state_sets = entry.get_final_state_sets()

    assert len(final_state_sets) == 2

    assert final_state_sets[0].get_final_state_labels() == {"x", "y", "z"}
    assert final_state_sets[0].get_comment() == "..."

    assert final_state_sets[1].get_final_state_labels() == {"a", "b", "c", "d"}
    assert final_state_sets[1].get_comment() == "different labels"

def __do_asserts_for_entry_2__(entry: CompactModelIndexEntry):

    assert entry.get_filename() == "last_model.jani"
    assert entry.get_common_metadata() == {
        "some_key": "some_values"
    }

    param_sets = entry.get_param_sets()

    assert len(param_sets) == 2

    assert param_sets[0].get_params() == {
        "k": -58962,
        "L": 359840,
        "b": True
    }
    assert param_sets[0].get_num_states() is None

    assert param_sets[1].get_params() == {
        "k": 8785,
        "L": -3,
        "b": False
    }
    assert param_sets[1].get_num_states() == 32

    final_state_sets = entry.get_final_state_sets()

    assert len(final_state_sets) == 1

    assert final_state_sets[0].get_final_state_labels() == {"A", "x", "y"}
    assert final_state_sets[0].get_comment() == "some detail"

def test_invalid_parameter_sets_raises_exception():
    """
        Test to verify that if two or more parameter sets have different keys, then an exception is raised.
    """
    with pytest.raises(ValueError) as exc_info:
        read_compact_model_index(index_path=(get_test_dir_path()
                                             / "compact_model_index_exception_inconsistent_param_sets.json"))

    assert str(exc_info.value) == "Error, compact entry at index '0' has inconsistent parameter sets."

def test_exception_no_param_set():
    with pytest.raises(ValueError) as exc_info:
        read_compact_model_index(index_path=(get_test_dir_path()
                                             / "compact_model_index_exception_no_param_sets.json"))

    assert str(exc_info.value) == "Error, no parameter sets specified for model."

def test_exception_no_final_state_set():
    with pytest.raises(ValueError) as exc_info:
        read_compact_model_index(index_path=(get_test_dir_path()
                                             / "compact_model_index_exception_no_final_state_sets.json"))

    assert str(exc_info.value) == "Error, no final state sets specified for model."

def test_exception_empty_final_state_set():
    with pytest.raises(ValueError) as exc_info:
        read_compact_model_index(index_path=(get_test_dir_path()
                                             / "compact_model_index_exception_empty_final_state_set.json"))

    assert str(exc_info.value) == "Error, empty set of labels."

@pytest.fixture
def compare_compact_index_scenario(request):
    test_name_prefix = request.param

    compact_index = read_compact_model_index(get_test_dir_path() / (test_name_prefix + ".json"))
    expected_index = read_model_index(get_test_dir_path() / (test_name_prefix + "_compiled_expected.json"))

    return compact_index, expected_index

@pytest.mark.parametrize(
    argnames="compare_compact_index_scenario",
    argvalues=[
        "compact_model_index_happy_no_entries",
        "compact_model_index_happy_one_entry",
        "compact_model_index_happy_multiple_entries"
    ], indirect=True)
def test_compile_compact_entry_to_normal_entry(compare_compact_index_scenario):

    compact_index, expected_index = compare_compact_index_scenario

    normal_index = compile_compact_index_to_full_index(compact_index=compact_index)
    actual_dict_repr = convert_model_index_to_dict(index=normal_index)
    expected_dict_repr = convert_model_index_to_dict(index=expected_index)

    assert actual_dict_repr == expected_dict_repr