import numpy as np
from PIL import Image
from k_means_clustering import k_means

#Given an image, return the pixel RGB data as a 1D vector
def read_image_pixels(filename):
    im = Image.open(filename)
    pixels = im.load()
    RGB_vectors = []
    for i in range(im.size[0]):
        for j in range(im.size[1]):
            RGB_vectors.append(pixels[i,j])
    return np.asarray(RGB_vectors)

#Take an image, segment it, and output the segmented image
def write_clustered_image(infile,outfile, k):
    print("Importing Data...")
    data = read_image_pixels(infile)

    print("Clustering...")
    means, indicators = k_means(data,k,True)

    print("Segmenting...")
    segment(data,means,indicators)

    size = Image.open(infile).size
    img_array = reshape(data,size)

    out = Image.fromarray(img_array.astype('uint8'))
    out.save(outfile)

#Replace pixels in data with the means to which they are assigned
def segment(data,means,indicators):
    for n in range(len(data)):
        tmp = means[np.argmax(indicators[n])]
        data[n] = np.asarray([int(x) for x in tmp])

#Convert 1D vector to a 2D matrix that can be written to an image
def reshape(data, size):
    img_array = np.zeros((size[1],size[0],len(data[0])))
    for i in range(size[1]):
        for j in range(size[0]):
            img_array[i][j] = data[(i + j*size[1])]
    return img_array

#MAIN
if __name__ == '__main__':
    k=4
    #write_clustered_image("Images/ferrari.jpg","Images/ferrari_segmented_" + str(k)+ ".jpg",k)
    #write_clustered_image("Images/bugatti.jpg","Images/bugatti_segmented_" + str(k)+ ".jpg",k)
    #write_clustered_image("Images/tree.jpg","Images/tree_segmented_" + str(k)+ ".jpg",k)
    #write_clustered_image("Images/penguin.jpg","Images/penguin_segmented_" + str(k)+".jpg",k)
    #write_clustered_image("Images/droplets.jpg","Images/droplets_segmented_" + str(k)+".jpg",k)
    for k in range(2,8):
        write_clustered_image("Images/highland_park_flowers.jpeg","Images/highland_park_flowers_segmented_" + str(k) +".jpeg",k)


from typing import List, Dict, Optional
from collections import defaultdict

from SPARQLWrapper import SPARQLWrapper, JSON

from noisemon.tools.cache_to_redis import get_cacher
from noisemon.tools.retry_request import retry_request
from noisemon.settings import settings
from noisemon.logger import logger

redis_params = {
    "host": settings.REDIS_HOST,
    "port": settings.REDIS_PORT,
    "db": settings.REDIS_DB,
    "client_name": "wikidata_cache",
}
redis_cacher = get_cacher(redis_params, EXPIRE=60 * 60 * 24 * 7)
decorator = redis_cacher(key_argument_position=1)

class Wikidata:
    def __init__(self):
        self.sparql = SPARQLWrapper("http://query.wikidata.org/sparql")
        self.sparql.setReturnFormat(JSON)

    # @retry_request
    # @redis_cacher(key_argument_position=1)
    def lookup_companies_by_ticker(self, ticker: str) -> List:
        """
        Given string ticker returns a list of junk
        """
        raise Exception("Obsolete")

    # @redis_cacher(key_argument_position=1)
    @retry_request
    @decorator
    def lookup_entity_label_by_qid(self, qid) -> Optional[str]:
        """
        Given QID, lookup for name in russian (fallback to english)
        """
        logger.debug(f"Performing wikidata entities lookup by QID: {qid}")
        if "http" in qid:
            qid = qid.split("/")[-1]
        query = """
            SELECT DISTINCT ?label WHERE {
                wd:%s rdfs:label ?label FILTER (lang(?label)="ru" || lang(?label)="en").
            }
            LIMIT 1""" % (qid)

        self.sparql.setQuery(query)
        results = self.sparql.query().convert()

        if not results["results"]["bindings"]:
            return None
        return results["results"]["bindings"][0]["label"]["value"]

    # @redis_cacher
    @decorator
    @retry_request
    def lookup_aliases_by_ticker(self, ticker: str) -> Dict[str, List[str]]:
        """
        Given ticker, lookup for aliases and return em with respect to
        entity QID, e.g. Dict[QID, List of aliases (unique)]
        """
        logger.debug(f"Performing wikidata entity aliases lookup given ticker: {ticker}")
        query = """
        SELECT DISTINCT ?id ?idLabel ?alias WHERE {
            SERVICE wikibase:label { bd:serviceParam wikibase:language "ru,en". }
            VALUES ?ticker { "%s"}

            ?id p:P414 ?exchange.
            ?exchange pq:P249 ?ticker.
            ?id skos:altLabel ?alias .
        }
        LIMIT 150""" % (ticker,)

        res = defaultdict(set)

        self.sparql.setQuery(query)
        results = self.sparql.query().convert()

        for x in results["results"]["bindings"]:
            res[x["id"]["value"]].add(x["alias"]["value"])
        for x in results["results"]["bindings"]:
            res[x["id"]["value"]].add(x["idLabel"]["value"])

        res = {key: list(value) for key, value in res.items()}
        return res


# Tokenization of Data
# Developed by Rahul Goel
# Jan 2022

import pandas as pd
from pandas import json_normalize
import os
import sys
import numpy as np
import requests
import pyodbc
import json
import yaml
import csv
import warnings;

warnings.simplefilter('ignore')
import cx_Oracle
import logging

# Parameter info
credentials = yaml.safe_load(open('secrets_increment_ORCA.yaml'))
max_c = 0
file_dict = credentials['file-dev']

log_path = os.getcwd() + '/'

if os.path.exists(log_path) is False:
    os.mkdir(log_path)

FORMAT = '%(asctime)s:%(name)s:%(levelname)s - %(message)s'  # format to save errors, info etc., in log file
logging.basicConfig(filename=log_path + "/MDM_Tokenization_ORCA.log", format=FORMAT, level=logging.INFO,filemode="w")
logging.info("Started")

def get_data():
    import chardet
    with open(r'{}{}'.format(credentials['file-dev'][cred]['input_path'], credentials['file-dev'][cred]['filename']),
              'rb') as f:
        result = chardet.detect(f.read())  # or readline if the file is large
    try:
        logging.info("File Name :- {}".format(credentials['file-dev'][cred]['filename']))
        logging.info('File encoding: {}, condifence :{}'.format(result['encoding'],result['confidence']))
        df = pd.read_csv(r'{}{}'.format(credentials['file-dev'][cred]['input_path'],credentials['file-dev'][cred]['filename']),delimiter=credentials['file-dev'][cred]['delimiter'],
                         header=None,dtype=str,encoding=result['encoding'],quoting=csv.QUOTE_NONE, keep_default_na=False, na_values=[''])
        logging.info("Input file read Successfully :- Rows : {} | Columns : {}".format(df.shape[0],df.shape[1]))
        return df
    except Exception as e:
        logging.error(e)
    try:
        df = pd.read_csv(
            r'{}{}'.format(credentials['file-dev'][cred]['input_path'], credentials['file-dev'][cred]['filename']),
            delimiter=credentials['file-dev'][cred]['delimiter'],
            header=None, dtype=str, encoding='Windows-1252', quoting=csv.QUOTE_NONE, keep_default_na=False,
            na_values=[''])
        return df
    except Exception as e:
        logging.error(e)
    # raise Exception

def token_data_col(df, col_names, token_group_template_detail, token_dtype_detail):
    auto_json_data = df
    # auto_json_data['tokengroup'] = (['CB01' for p in range(len(auto_json_data))])
    # auto_json_data['tokentemplate'] = (['CBASCII01' for p in range(len(auto_json_data))])

    # token_group_template_detail index 0 :- tokengroup
    # token_group_template_detail index 1 :- tokentemplate

    auto_json_data['tokengroup'] = token_group_template_detail[0]
    auto_json_data['tokentemplate'] = token_group_template_detail[1]

    auto_json_data = auto_json_data[[col_names, 'tokengroup', 'tokentemplate']].dropna()
    if token_dtype_detail == 'int':
        auto_json_data[col_names] = auto_json_data[col_names].astype(str).str.replace("-", "").str.replace("\.0", "")
    if token_dtype_detail == 'str':
        auto_json_data[col_names] = auto_json_data[col_names].where(
            ~auto_json_data[col_names].astype(str).str.fullmatch(r"\s*"))
        auto_json_data[col_names] = auto_json_data[col_names].str.strip()
        auto_json_data[col_names] = auto_json_data[col_names].str.pad(20, side='right')
        auto_json_data[col_names] = auto_json_data[col_names].str[:20]

    auto_json_data.rename(columns={col_names: 'data'}, inplace=True)
    auto_json_data_dict = auto_json_data.to_json(orient='records')
    logging.info("API input dictionary generated")

    return auto_json_data, auto_json_data_dict

def token_generic(auto_json_dict_data):
    bearer_header = {
        "APP_ID": credentials['vts-expl']['APP_ID'],
        "APP_KEY": credentials['vts-expl']['APP_KEY'],
        "apiVersion": str(credentials['vts-expl']['apiVersion'])
    }

    def header_generation():
        try:
            headers = {
                'Content-Type': 'Application/json',
                'Authorization': 'Bearer {}'.format(
                    requests.post(credentials['vts-expl']['bearer-url'], headers=bearer_header).json()[
                        'access_token'])
            }
            return headers
        except Exception as e:
            logging.error("Issue Generating Bearer Token")
            logging.error(e)
            raise

    headers = header_generation()

    try:
        payload = auto_json_dict_data
        url = credentials['vts-expl']['token-url']
        response = requests.post(url, headers=headers, data=payload, verify=False)

        if response.status_code == 401:
            if "Unauthorized. Azure AD  Access token is missing or invalid" in response.json(strict=False)['message']:
                headers = header_generation()
                response = requests.post(url, headers=headers, data=payload, verify=False)

        # data = response.json()
        data = response.json(strict=False)
        temp = pd.DataFrame(data)
        logging.info("Token data generated")

        return temp
    except Exception as e:
        logging.error("Issue performing Tokenization with url")
        logging.error(e)
        raise

def chunker(seq, size):
    return (seq[pos:pos + size] for pos in range(0, len(seq), size))

try:
    for cred in range(len(credentials['file-dev'])):
        log_path = os.getcwd() + '/'

        if os.path.exists(log_path) is False:
            os.mkdir(log_path)

        FORMAT = '%(asctime)s:%(name)s:%(levelname)s - %(message)s'  # format to save errors, info etc., in log file
        logging.basicConfig(filename=log_path + "/" + credentials['file-dev'][cred]['filename'] + "_Tokenization.log",
                            format=FORMAT, level=logging.INFO, filemode="w")
        logging.info("Started")
        try:
            df3 = get_data()
            start_size = df3.shape[0]
        except:
            continue

        column_order = df3.columns
        arr = np.array(credentials['file-dev'][cred]['col_position'])
        arr = arr[(arr < len(column_order))]
        col_names_file = [int(i) - 1 for i in arr]
        token_group_template = {
            val: [credentials['file-dev'][cred]['tokengroup'][ind], credentials['file-dev'][cred]['tokentemplate'][ind]] for
            ind, val in enumerate(col_names_file)}
        token_dtype = {val: credentials['file-dev'][cred]['dtype'][ind] for ind, val in enumerate(col_names_file)}
        temp = pd.DataFrame()
        for col_name in col_names_file:
            logging.info("-" * 50)
            logging.info("Processing column {}".format(col_name))
            try:
                temp[col_name] = df3[col_name].str.strip()
                if len(temp[col_name].unique()) == 1 and temp[col_name].unique()[0] == '':
                    df3[str(col_name) + '_token'] = np.nan

                else:
                    df3[col_name] = df3[col_name].str.strip()
                    auto_json, auto_json_dict = token_data_col(df3, col_name, token_group_template[col_name],
                                                               token_dtype[col_name])
                    result = []

                    auto_json_unique = auto_json.drop_duplicates(subset=['data'])

                    if auto_json.shape[0] > 0:
                        chunk_count = 1
                        for i in chunker(auto_json_unique, 1000):
                            logging.info("Tokenizing Chunk {}".format(chunk_count))
                            i = i.to_json(orient='records')
                            array = i
                            auto_json_dict = array
                            result.append(token_generic(auto_json_dict))
                            chunk_count += 1

                        result = pd.concat(result).reset_index(drop=True).rename(
                            columns={'token': str(col_name) + '_token'})
                        result.drop(columns={'status'}, inplace=True)

                        result_join = auto_json_unique.reset_index().join(result).set_index('index')

                        auto_json[str(col_name) + '_token'] = auto_json['data'].map(
                            result_join.set_index('data')[str(col_name) + '_token'])

                        df3 = df3.join(auto_json[str(col_name) + '_token'].to_frame())
                    else:
                        df3[str(col_name) + '_token'] = np.nan
            except KeyError as e:
                print(e)

        df3.drop(columns={'tokengroup', 'tokentemplate'}, inplace=True)
        df3.drop(columns=col_names_file, inplace=True)
        for col_name in col_names_file:
            df3 = df3.rename(columns={str(col_name) + '_token': col_name})

        df3 = df3[column_order]
        if start_size == df3.shape[0]:
            logging.info('start & end size match')
            df3.to_csv(r'{}{}'.format(credentials['file-dev'][cred]['output_path'],
                                      credentials['file-dev'][cred]['filename']), sep='\x01', quoting=csv.QUOTE_NONE,
                       header=None, index=False
                       )
        else:
            logging.info('start & end size mismatch')
        # credentials['file-dev'][cred]['delimiter']

        logging.info("Output Data generated successfully :- Rows : {} | Columns : {}".format(df3.shape[0], df3.shape[1]))
        logging.info("Completed")
        logging.info("-" * 50)
        logging.info("-" * 50)
        print('File tokenisation done')
except Exception as e:
    logging.error(e)
print ('Run completed')
logging.info('Run completed')


from __future__ import unicode_literals

import time

from django.core.management.base import BaseCommand

from search.datasources.bookshops.frFR.dilicom import dilicomScraper
from search.models import Card

# py2/3
try:
    input = raw_input
except NameError:
    pass

class Command(BaseCommand):

    def add_arguments(self, parser):
        # parser.add_argument(
        #     '-V',
        #     dest='verbose',
        #     help='debug output.',
        # )
        pass

    def update_all(self):
        # All cards with an ISBN.
        cards = Card.objects.filter(isbn__isnull=False)
        self.stdout.write("Updating {} cards.".format(cards.count()))

        confirmation = True
        # confirmation = raw_input("Continue ? [Y/n]")
        if confirmation == "n":
            exit(0)

        isbns = cards.values_list("isbn", flat=True)  # [it.isbn for it in cards]

        # isbns = isbns[:100]  # DEBUG:
        # isbns = isbns[50:500]  # TODO:
        # isbns = isbns[430:500]  # TODO:
        dilicom_query = dilicomScraper.Scraper(*isbns)
        self.stdout.write("Searching all on Dilicom...")
        bklist, errors = dilicom_query.search()

        if len(bklist) != len(isbns):
            self.stdout.write("--- beware: the search results have not the same length that our query: {} vs {}".format(len(bklist), len(isbns)))
            # exit(1)

        cards_updated = []
        cards_not_updated = []
        count_ok = 0
        for i, card in enumerate(cards):
            bk = list(filter(lambda it: it['isbn'] == card.isbn, bklist))
            if bk:
                bk = bk[0]

            if not bk:
                # self.stdout.write("No matching result for card {}. Pass.".format(card.isbn))
                cards_not_updated.append(card)
                continue

            # self.stdout.write("{}: => {}".format(card.title, bk))

            # THE update.
            # same publisher?
            # print("--publishers: {} VS {}".format(bk.get('publishers'), " + ".join([it.name for it in card.publishers.all()])))

            pub_name = ""
            if bk.get('publishers'):
                pub_name = bk.get('publishers')[0]
            if not pub_name:
                print("Could not find the name of distributor for {}. Exiting.".format(bk.get('publishers')[0]))
                exit(1)

            print("* {} - updating {}".format(i, card.isbn))

            try:
                gln = bk.get('distributor_gln')
                if not gln:
                    print("Could not get distributor GLN from {}. Exiting.".format(bk))
                    exit(1)
                Card.update_from_dict(card, bk,
                                      distributor_gln=gln,
                                      publisher_name=pub_name)

                cards_updated.append(card)
                count_ok += 1
            except Exception as e:
                self.stdout.write(" ! failed for {}: {}".format(bk.get('isbn'), e))

            # Updating 4.000 cards is resource heavy.
            if i % 100 == 0:
                time.sleep(0.2)

        self.stdout.write("-------------------")
        self.stdout.write("Cards OK: {}.".format(count_ok))
        self.stdout.write("Cards updated:{}".format(len(cards_updated)))
        self.stdout.write("Cards not updated:{}".format(len(cards_not_updated)))
        # self.stdout.write("\n".join([it.isbn for it in cards_not_updated]))
        self.stdout.write("Done.")

    def handle(self, *args, **options):
        self.stdout.write("Go...")
        try:
            self.update_all()
        except KeyboardInterrupt:
            self.stdout.write("User abort.")


from time import sleep
from web3 import Web3
import tomllib
import json

with open("config.toml", "rb") as config_file:
    config = tomllib.load(config_file)
with open("abi.json", "r") as config_file:
    abi = json.load(config_file)

w3 = Web3(Web3.HTTPProvider(config["PRIVATE"]["WSS_APIKEY"]))
contract = w3.eth.contract(
    address=config["PUBLIC"]["marimo_address"], abi=abi)

def check_marimo():
    block = w3.eth.get_block(block_identifier="latest")
    base_fee_per_gas = getattr(block, "baseFeePerGas")
    gas_in_gwei = Web3.from_wei(base_fee_per_gas, "gwei")
    # check for gas price
    print(
        f"bh: {getattr(block, 'number')}")
    if gas_in_gwei > config["PUBLIC"]["gas_price"]:
        print(f"Gas too expensive. {gas_in_gwei} GWEI")
    else:
        # get all marimbo from PUBLIC_KEY acc and iterate over it
        marimos_owned = contract.functions.tokensOfOwner(
            config["PRIVATE"]["PUBLIC_KEY"]).call()
        for marimo_id in marimos_owned:
            marimo_life = contract.functions.getElapsedTimeFromLastWaterChanged(
                marimo_id).call()
            life_threshold = 60 * 60 * 24 * \
                (100 - config["PUBLIC"]["water_clarity"])
            # check for marimos life
            if marimo_life < life_threshold:
                print("Marimo life is good.")
            else:
                print(
                    f"Marimo life is {marimo_life / 60 / 60 / 24} days")
                nonce = w3.eth.get_transaction_count(
                    config["PRIVATE"]["PUBLIC_KEY"])
                marimo_txn = contract.functions.changeWater(marimo_id).build_transaction({
                    'chainId': 1,
                    'gas': 300000,
                    'maxFeePerGas': base_fee_per_gas + Web3.to_wei(1, "gwei"),
                    'maxPriorityFeePerGas': w3.to_wei('1', 'gwei'),
                    "nonce": nonce
                })
                signed_txn = w3.eth.account.sign_transaction(
                    marimo_txn,
                    private_key=config["PRIVATE"]["PRIVATE_KEY"]
                )
                w3.eth.send_raw_transaction(signed_txn.rawTransaction)
                print(signed_txn.hash)
                receipt = w3.eth.wait_for_transaction_receipt(signed_txn.hash)
                print(receipt)

if __name__ == '__main__':
    while True:
        check_marimo()
        sleep(5)


def program():
    print("please enter a word")
    word = input()

    print("Please select an option")
    print("""
    1.Display in a Box
    2.Display Lower-case
    3.Display Upper-case
    4.Display Mirrored
    5.Repeat
    """)

    option = int(input())

    if (option == 1):
        word_box(word)
    elif(option == 2):
        lower_case(word)
    elif(option == 3):
        upper_case(word)
    elif(option == 4):
        mirrored(word)
    elif(option == 5):
        repeat(word)
def word_box(word):
    print("*" * (len(word) + 10))
    print("*      ", word, "*")
    print("*" * (len(word) + 10))

def lower_case(word):
    print(word.lower())

def upper_case(word):
    print(word.upper())

def mirrored(word):
    reverse = ""
    for letter in word:
        reverse = letter + reverse
    print(reverse)

def repeat(word):
    print("How many times should i print the word")
    word_repeat = int(input())
    for number in range (0,word_repeat,1):
        if (number % 2 == 0):
            lower_case(word)
        else:
            upper_case(word)

program()


"""BUILD rules to construct a java agent.

All this is unfortunately necessary.  Some of the machinery is located at

  //java/com/google/monitoring/runtime/instrumentation/BUILD

but you should be able to use the java_agent_binary by itself; just
add yourself to the java_agent_builddef_users package group.
"""

# expand_template is present in a standard template in google3, but not in open-source.
def expand_template_impl(ctx):
    ctx.actions.expand_template(
        template = ctx.file.template,
        output = ctx.outputs.out,
        substitutions = ctx.attr.substitutions,
        is_executable = ctx.attr.is_executable,
    )

expand_template = rule(
    implementation = expand_template_impl,
    attrs = {
        "template": attr.label(mandatory = True, allow_single_file = True),
        "substitutions": attr.string_dict(mandatory = True),
        "out": attr.output(mandatory = True),
        "is_executable": attr.bool(default = False, mandatory = False),
    },
)

def java_agent_binary(
        library_name,
        deploy_jar_name,
        premain_class,
        srcs,
        deps,
        visibility = ["//visibility:private"],
        compatible_with = None,
        resources = None,
        extra_runtime_deps = [],
        **kwds):
    """Builddef to create a Java instrumentation agent cleanly.

    A large part of what this is doing is doing surgery on a java_binary
    so it won't, for example, interfere with build data or override
    existing classes in your application.

    Args:
         library_name: The name of the agent.
         deploy_jar_name: The name of the resulting deploy JAR.
         premain_class: The class to be run prior to main().  See the
           documentation for java.lang.instrument for more information.
      extra_runtime_deps: runtime_deps added to java_binary or java_test calls
    """

    simple_class_name = premain_class[premain_class.rfind(".") + 1:]

    package = premain_class[:premain_class.rfind(".")]

    path_to_bootstrap = premain_class.replace(".", "/") + "Bootstrap.class"

    bootstrap_file = simple_class_name + "Bootstrap.java"

    # See note in Bootstrap.java.in to explain this.
    expand_template(
        name = library_name + "_bootstrap",
        template = ":bootstrap",
        out = bootstrap_file,
        substitutions = {
            "CLASS_NAME": simple_class_name,
            "PACKAGE": package,
            "PATH_TO_CLASS": path_to_bootstrap,
            "PREMAIN_CLASS": premain_class,
            "GENERATOR": "allocation_instrumenter/builddefs.bzl",
        },
        compatible_with = compatible_with,
    )

    if srcs != []:
        native.java_library(
            name = library_name,
            srcs = srcs,
            visibility = visibility,
            deps = deps,
            compatible_with = compatible_with,
            **kwds
        )
    else:
        native.java_library(
            name = library_name,
            exports = deps,
            compatible_with = compatible_with,
            **kwds
        )

    native.java_binary(
        name = library_name + "_internal",
        srcs = [bootstrap_file],
        deploy_manifest_lines = [
            "Premain-Class: " + premain_class + "Bootstrap",
            "Can-Redefine-Classes: true",
            "Can-Retransform-Classes: true",
        ],
        main_class = "NotSuitableAsAMain",
        deps = [
            "@google_bazel_common//third_party/java/jsr250_annotations",
        ],
        runtime_deps = [":" + library_name] + extra_runtime_deps,
        visibility = ["//visibility:private"],
        compatible_with = compatible_with,
        resources = resources,
        **kwds
    )

    native.genrule(
        name = library_name + "_asm_rename",
        srcs = [library_name + "_internal_deploy.jar"],
        outs = [library_name + "_internal_2_deploy.jar"],
        cmd = "$(location @google_bazel_common//tools/jarjar) process $(location :jarjar_rules) '$<' '$@'",
        tools = [
            ":jarjar_rules",
            "@google_bazel_common//tools/jarjar",
        ],
        visibility = ["//visibility:private"],
        compatible_with = compatible_with,
        **kwds
    )

    native.genrule(
        name = library_name + "_jar_strip",
        srcs = [library_name + "_internal_2_deploy.jar"],
        outs = [deploy_jar_name],
        cmd = "absolutify() { [[ \"$$1\" =~ ^/ ]] && echo \"$$1\" || echo \"$$PWD/$$1\"; }; " +
              "JAR=`absolutify $(JAVABASE)/bin/jar`; " +
              "OUT=`absolutify $@`; " +
              "IN=`absolutify $<`; " +
              "TMPDIR=$$(mktemp -d $(@D)/tmp.XXX) && " +
              "cd $${TMPDIR} && " +
              "$${JAR} xf $${IN} &&" +
              "rm -rf $$(find . -name module-info\\.class) &&" +
              "$${JAR} cfm $${OUT} META-INF/MANIFEST.MF $$(find . -type f \\( -not -regex \".com/google/build/Data.class\" -and -not -regex \".*build-data\\.properties\" \\) )",
        toolchains = ["@bazel_tools//tools/jdk:current_host_java_runtime"],
        visibility = ["//visibility:public"],
        compatible_with = compatible_with,
        **kwds
    )


def letterCombinations(digits):
    dict={2:"abc",3:"def",4:"ghi",5:"jkl",6:"mno",7:"pqrs",8:"tuv",9:"wxyz"}
    if ("1" in digits) or (len(digits)==0):
        return []

    length=1
    for num in digits:
        num=int(num)
        length*=len(dict.get(num))
    #print("length={}".format(length))
    answer=[""]*length
    th=length
    for n in range(len(digits)):
        num=int(digits[n])
        th/=len(dict.get(num))
        #print("th={}".format(th))
        pos=0
        #th=3ならaaabbbcccみたいに並べる
        for i in range(length):
            #print("pos={}".format(pos))
            answer[i]+=dict.get(num)[pos]
            #print("answer={}".format(answer))
            if (i+1)%th==0:
                pos+=1
            if pos==len(dict.get(num)):
                pos=0
    return answer


class Solution(object):
    def calculateMinimumHP(self, dungeon):
        """
        :type dungeon: List[List[int]]
        :rtype: int
        """
        #dungeon = [[-2, -3, 3], [-5, -10, 1], [10, 30, -5]]
        m = len(dungeon)
        n = len(dungeon[0])
        dp = [[float('inf')] * (n+1) for _ in range(m+1)]
        dp[m][n-1] = 1
        for i in range(m-1,-1,-1):
            for j in range(n-1,-1,-1):
                # temp 表示要往 右或下 走，最少需要的生命值
                temp = min( (dp[i+1][j]) , (dp[i][j+1]))-dungeon[i][j]
                # 如果temp > 1,则为temp; 如果temp < 1, 因为当前点骑士必须最小具有1点生命值，
                # 所以dp[i][j] = 1
                dp[i][j] = max(temp, 1)
        return dp[0][0]


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

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

import numpy as np
import tensorflow as tf

#***************************************************************
def orthonormal_initializer(input_size, output_size):
  """"""

  if not tf.get_variable_scope().reuse:
    print(tf.get_variable_scope().name)
    I = np.eye(output_size)
    lr = .1
    eps = .05/(output_size + input_size)
    success = False
    while not success:
      Q = np.random.randn(input_size, output_size) / np.sqrt(output_size)
      for i in xrange(100):
        QTQmI = Q.T.dot(Q) - I
        loss = np.sum(QTQmI**2 / 2)
        Q2 = Q**2
        Q -= lr*Q.dot(QTQmI) / (np.abs(Q2 + Q2.sum(axis=0, keepdims=True) + Q2.sum(axis=1, keepdims=True) - 1) + eps)
        if np.isnan(Q[0,0]):
          lr /= 2
          break
      if np.isfinite(loss) and np.max(Q) < 1e6:
        success = True
      eps *= 2
    print('Orthogonal pretrainer loss: %.2e' % loss)
  else:
    Q = np.random.randn(input_size, output_size) / np.sqrt(output_size)
  return Q.astype(np.float32)

#===============================================================
def linear(inputs, output_size, n_splits=1, add_bias=True, initializer=None, moving_params=None):
  """"""

  # Prepare the input
  if not isinstance(inputs, (list, tuple)):
    inputs = [inputs]
  n_dims = len(inputs[0].get_shape().as_list())
  all_inputs = tf.concat(inputs, n_dims-1)
  input_size = all_inputs.get_shape().as_list()[-1]

  # Prepare the output
  output_size *= n_splits
  output_shape = []
  shape = tf.shape(all_inputs)
  for i in xrange(n_dims-1):
    output_shape.append(shape[i])
  output_shape.append(output_size)
  output_shape = tf.stack(output_shape)

  all_inputs = tf.reshape(all_inputs, [-1, input_size])
  with tf.variable_scope('Linear'):
    # Get the matrix
    if initializer is None and not tf.get_variable_scope().reuse:
      mat = orthonormal_initializer(input_size, output_size//n_splits)
      mat = np.concatenate([mat]*n_splits, axis=1)
      initializer = tf.constant_initializer(mat)
    matrix = tf.get_variable('Weights', [input_size, output_size], initializer=initializer)
    if moving_params is not None:
      matrix = moving_params.average(matrix)
    else:
      tf.add_to_collection('Weights', matrix)

    # Get the bias
    if add_bias:
      bias = tf.get_variable('Biases', [output_size], initializer=tf.zeros_initializer())
      if moving_params is not None:
        bias = moving_params.average(bias)
    else:
      bias = 0

    # Do the multiplication
    lin = tf.matmul(all_inputs, matrix) + bias
    lin = tf.reshape(lin, output_shape)
    if n_splits > 1:
      return tf.split(lin, n_splits, n_dims-1)
    else:
      return lin

#===============================================================
def bilinear(inputs1, inputs2, output_size, n_splits=1, add_bias1=True, add_bias2=True, initializer=None, moving_params=None):
  """"""

  # Prepare the input
  if not isinstance(inputs1, (list, tuple)):
    inputs1 = [inputs1]
  n_dims1 = len(inputs1[0].get_shape().as_list())
  all_inputs1 = tf.concat(inputs1, n_dims1-1)
  inputs1_size = all_inputs1.get_shape().as_list()[-1]
  inputs1_bucket_size = tf.shape(all_inputs1)[-2]

  if not isinstance(inputs2, (list, tuple)):
    inputs2 = [inputs2]
  n_dims2 = len(inputs2[0].get_shape().as_list())
  all_inputs2 = tf.concat(inputs2, n_dims2-1)
  inputs2_size = all_inputs2.get_shape().as_list()[-1]
  inputs2_bucket_size = tf.shape(all_inputs2)[-2]

  # Prepare the output
  output_size *= n_splits
  output_shape = []
  shape1 = tf.shape(all_inputs1)
  for i in xrange(n_dims1-1):
    output_shape.append(shape1[i])
  output_shape.append(output_size)
  output_shape.append(inputs2_bucket_size)
  output_shape = tf.stack(output_shape)

  all_inputs1 = tf.reshape(all_inputs1, tf.stack([-1, inputs1_bucket_size, inputs1_size]))
  if add_bias1:
    bias1 = tf.ones(tf.stack([tf.shape(all_inputs1)[0], inputs1_bucket_size, 1]))
    all_inputs1 = tf.concat([all_inputs1, bias1], 2)
    inputs1_size += 1
  all_inputs2 = tf.reshape(all_inputs2, tf.stack([-1, inputs2_bucket_size, inputs2_size]))
  if add_bias2:
    bias2 = tf.ones(tf.stack([tf.shape(all_inputs2)[0], inputs2_bucket_size, 1]))
    all_inputs2 = tf.concat([all_inputs2, bias2], 2)
    inputs2_size += 1
  with tf.variable_scope('Bilinear'):
    # Get the matrix
    if initializer is None and tf.get_variable_scope().reuse is None:
      mat = orthonormal_initializer(inputs1_size, inputs2_size)[:,None,:]
      mat = np.concatenate([mat]*output_size, axis=1)
    weights = tf.get_variable('Weights', [inputs1_size, output_size, inputs2_size], initializer=initializer)
    if moving_params is not None:
      weights = moving_params.average(weights)
    else:
      tf.add_to_collection('Weights', weights)

    # Do the multiplication
    # (bn x d) (d x rd) -> (bn x rd)
    lin = tf.matmul(tf.reshape(all_inputs1, [-1, inputs1_size]),
                        tf.reshape(weights, [inputs1_size, -1]))
    # (b x nr x d) (b x n x d)T -> (b x nr x n)
    bilin = tf.matmul(tf.reshape(lin, tf.stack([-1, inputs1_bucket_size*output_size, inputs2_size])),
                      all_inputs2, transpose_b=True)
    # (bn x r x n)
    bilin = tf.reshape(bilin, output_shape)

    if n_splits > 1:
      return tf.split(bilin, n_splits, n_dims-2)
    else:
      return bilin

#===============================================================
def convolutional(inputs, window_size, output_size, n_splits=1, add_bias=True, initializer=None, moving_params=None):
  """"""

  # Prepare the input
  if not isinstance(inputs, (list, tuple)):
    inputs = [inputs]
  n_dims = len(inputs[0].get_shape().as_list())
  all_inputs = tf.concat(inputs, n_dims-1)
  input_size = all_inputs.get_shape().as_list()[-1]
  bucket_size = tf.shape(all_inputs)[-2]

  # Prepare the output
  output_size *= n_splits
  output_shape = []
  shape = tf.shape(all_inputs)
  for i in xrange(n_dims-1):
    output_shape.append(shape[i])
  output_shape.append(output_size)
  output_shape = tf.stack(output_shape)

  all_inputs = tf.reshape(all_inputs, tf.stack([-1, bucket_size, input_size]))
  with tf.variable_scope('Convolutional'):
    # Get the matrix
    if initializer is None and not tf.get_variable_scope().reuse:
      mat = orthonormal_initializer(input_size*window_size, output_size//n_splits)
      mat = np.concatenate([mat]*n_splits, axis=1)
      mat = np.reshape(mat, [window_size, input_size, output_size])
      initializer = tf.constant_initializer(mat)
    matrix = tf.get_variable('Weights', [window_size, input_size, output_size], initializer=initializer)
    if moving_params is not None:
      matrix = moving_params.average(matrix)
    else:
      tf.add_to_collection('Weights', matrix)

    # Get the bias
    if add_bias:
      bias = tf.get_variable('Biases', [output_size], initializer=tf.zeros_initializer())
      if moving_params is not None:
        bias = moving_params.average(bias)
    else:
      bias = 0

    # Do the multiplication
    conv = tf.nn.conv1d(all_inputs, matrix, 1, 'SAME') + bias
    conv = tf.reshape(conv, output_shape)
    if n_splits > 1:
      return tf.split(conv, n_splits, n_dims-1)
    else:
      return conv

#===============================================================
def random_mask(prob, mask_shape, dtype=tf.float32):
  """"""

  rand = tf.random_uniform(mask_shape)
  ones = tf.ones(mask_shape, dtype=dtype)
  zeros = tf.zeros(mask_shape, dtype=dtype)
  prob = tf.ones(mask_shape) * prob
  return tf.where(rand < prob, ones, zeros)

#===============================================================
def random_where(prob, success, fail, keep_dims=None):
  """"""

  mask_shape = tf.shape(success)
  keep_dims = np.zeros(len(success.get_shape().as_list()), dtype=np.int32)
  keep_dims[dims] = np.int32(1)
  mask_shape = mask_shape ** keep_dims
  mask = random_mask(prob, mask_shape, dtype=success.dtype)
  return mask * success + (1-mask) * fail


#!/usr/bin/env python3

import math, itertools, functools, heapq
from collections import defaultdict, Counter
from typing import List, Set, Dict, Tuple

try:
    from _tree import *
    from _list import *
    from _util import *
except ImportError:
    pass

class LRUCache(object):
    # TODO: slow, improve
    def __init__(self, capacity):
        """
        :type capacity: int
        """
        self.cache = {}
        self.capacity = capacity
        self.queue = []

    def get(self, key):
        """
        :rtype: int
        """
        if key in self.cache:
            self.queue.remove(key)
            self.queue.insert(0, key)
            return self.cache[key]
        return -1

    def set(self, key, value):
        """
        :type key: int
        :type value: int
        :rtype: nothing
        """
        if key not in self.cache:
            if len(self.queue) < self.capacity:
                self.queue.insert(0, key)
            else:
                k = self.queue.pop()
                del self.cache[k]
                self.queue.insert(0, key)
        else:
            self.queue.remove(key)
            self.queue.insert(0, key)
        self.cache[key] = value

class LRUCacheV2:

    def __init__(self, capacity: int):
        self.capacity = capacity
        self.head = None
        self.tail = None
        self.map: Dict[int, DoubleListNode] = {}

    def _move_to_head(self, node: DoubleListNode):
        if self.head is node:  # head node
            return
        elif self.tail is node:  # tail node
            self.tail = node.prev
            if node.prev is not None:
                node.prev.next = None
        else:  # middle node
            node.prev.next = node.next
            node.next.prev = node.prev
        node.prev = None
        node.next = None
        self._insert_head(node)

    def _insert_head(self, node: DoubleListNode):
        if self.head is None or self.tail is None:
            self.head = node
            self.tail = node
        else:
            node.next = self.head
            self.head.prev = node
            self.head = node

    def get(self, key: int) -> int:
        if key not in self.map:
            return -1
        node = self.map[key]
        self._move_to_head(node)
        return node.val

    def put(self, key: int, value: int) -> None:
        if key in self.map:
            node = self.map[key]
            node.val = value
            self._move_to_head(node)
        else:
            node = DoubleListNode(key=key, val=value)
            self.map[key] = node
            self._insert_head(node)
            if len(self.map) > self.capacity:  # evict
                del self.map[self.tail.key]
                self.tail = self.tail.prev
        print(self.map)

class LRUCacheOfficial:

    def __init__(self, capacity: int):
        self.capacity = capacity
        self.head = DoubleListNode(-1, -1)
        self.tail = DoubleListNode(-1, -1)
        self.head.next = self.tail
        self.tail.prev = self.head
        self.map: Dict[int, DoubleListNode] = {}

    def _remove(self, node: DoubleListNode):
        node.prev.next = node.next
        node.next.prev = node.prev

    def _add(self, node: DoubleListNode):
        next_head = self.head.next
        next_head.prev = node
        node.next = next_head
        node.prev = self.head
        self.head.next = node

    def get(self, key: int) -> int:
        if key not in self.map:
            return -1
        node = self.map[key]
        self._remove(node)
        self._add(node)
        return node.val

    def put(self, key: int, value: int) -> None:
        if key in self.map:
            old_node = self.map[key]
            self._remove(old_node)

        node = DoubleListNode(key=key, val=value)
        self.map[key] = node
        self._add(node)

        if len(self.map) > self.capacity:
            node_to_delete = self.tail.prev
            self._remove(node_to_delete)
            del self.map[node_to_delete.key]

if __name__ == '__main__':
    lru = LRUCacheOfficial(capacity=2)
    lru.put(1, 1)
    lru.put(2, 2)
    print("%d==1" % lru.get(1))
    lru.put(3, 3)
    print("%d==-1" % lru.get(2))
    lru.put(4, 4)
    print("%d==-1" % lru.get(1))
    print("%d==3" % lru.get(3))
    print("%d==4" % lru.get(4))

    lru = LRUCacheOfficial(capacity=3)
    lru.put(1, 1)
    lru.put(2, 2)
    lru.put(3, 3)
    lru.put(4, 4)
    print("%d==4" % lru.get(4))
    print("%d==3" % lru.get(3))
    print("%d==2" % lru.get(2))
    print("%d==-1" % lru.get(1))
    lru.put(5, 5)
    print("%d==-1" % lru.get(1))
    print("%d==2" % lru.get(2))
    print("%d==3" % lru.get(3))
    print("%d==-1" % lru.get(4))
    print("%d==5" % lru.get(5))


# from django.shortcuts import render
from rest_framework.response import Response
from django.contrib import auth
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.models import User
from .models import Publication, File
from elsa_lab_django.settings import DOMAIN
import os
from os.path import splitext
import base64

from rest_framework import viewsets
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from rest_framework import status
from rest_framework_jwt.authentication import (
    BaseJSONWebTokenAuthentication, JSONWebTokenAuthentication
)

from .serializers import PublicationSerializer

def create_and_write_file(publication, file):
    path = './publications/publications_file/{}'.format(publication.title)
    # fetch the data of uploaded file
    file_data = file.pop('data').split(',')[-1]
    # encoding to utf-8
    file_data = bytes(file_data, encoding='utf8')
    # extract extension
    filename, extension = splitext(file.pop('title'))
    # local file path ( file name )
    local_path = path + '/' + publication.title + extension
    # write file
    with open(local_path, 'wb') as f:
        f.write(base64.decodebytes(file_data))
    file.pop('preview')
    file['url'] = DOMAIN + '/static/' + '/'.join(local_path.split('/')[3:])
    file['path'] = local_path
    file['publication'] = publication
    File.objects.create(**file)

class PublicationList(APIView, JSONWebTokenAuthentication):
    """
    API endpoint that allows users to be viewed or edited.
    """

    def get(self, request, format=None):
        publications = Publication.objects.all()
        serializer = PublicationSerializer(publications, many=True)
        return Response(serializer.data)

    # upload file data format: base64
    def post(self, request, format=None):
        serializer = PublicationSerializer(data=request.data)
        data = request.data
        file = request.data.pop('files')[0]
        if serializer.is_valid():
            path = './publications/publications_file/{}'.format(serializer.data['title'])
            if not os.path.exists(path):
                os.makedirs(path)

            # front-end only allow upload one file
            # file = data.pop('files')[0]
            # create Publication object
            p = Publication.objects.create(**data)
            # create and write file object
            create_and_write_file(p, file)

            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class PublicationDetail(APIView, JSONWebTokenAuthentication):
    """
    Retrieve, update or delete a snippet instance.
    """

    def get(self, request, publication_id, format=None):
        publication = Publication.objects.get(id=publication_id)
        serializer = PublicationSerializer(publication)
        return Response(serializer.data)

    def put(self, request, publication_id, format=None):
        publication = Publication.objects.get(id=publication_id)
        data = request.data
        file = data.pop('files')[0]
        serializer = PublicationSerializer(data=request.data)
        if serializer.is_valid():
            path = './publications/publications_file/{}'.format(serializer.data['title'])
            if not os.path.exists(path):
                os.makedirs(path)
            if hasattr(publication, 'file'):
                publication.file.delete()
            create_and_write_file(publication, file)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, publication_id, format=None):
        publication = Publication.objects.get(id=publication_id)
        publication.delete()
        if Publication.objects.filter(id=publication_id).exists():
            return Response(status=status.HTTP_400_BAD_REQUEST)
        return Response(status=status.HTTP_204_NO_CONTENT)

class FileDetail(APIView, JSONWebTokenAuthentication):
    def delete(self, request, publication_id, format=None):
        publication = Publication.objects.get(id=publication_id)
        publication.file.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


import socket, sys, struct, random, time

class InPacket():
    """
    Class that represents a singe incoming TCP packet
    """

    def parse(self):
        """
        Parses an incoming TCP/IP packet
        """
        header = struct.unpack('!HHLLBBHHH', self.packet[20:40])
        self.src_prt = header[0]
        self.dst_prt = header[1]
        self.seqn = header[2]
        self.ackn = header[3]
        self.offset = header[4]
        self.flags = header[5]
        self.flg_fin = (self.flags & 1)
        self.flg_syn = (self.flags & 2) >> 1
        self.flg_rst = (self.flags & 4) >> 2
        self.flg_psh = (self.flags & 8) >> 3
        self.flg_ack = (self.flags & 16) >> 4
        self.flg_urg = (self.flags & 32) >> 5
        self.window = header[6]
        self.chksum = header[7]
        self.urg_prt = header[8]
        self.header_size = 20 + (4 * (self.offset >> 4))
        self.data_size = len(self.packet) - self.header_size
        self.data = self.packet[self.header_size:]
        return self

    def __init__(self, packet):
        self.packet = packet[0]
        self.src_prt = 0
        self.dst_prt = 0
        self.seqn = 0
        self.ackn = 0
        self.offset = 0
        self.flags = 0
        self.flg_fin = 0
        self.flg_syn = 0
        self.flg_rst = 0
        self.flg_psh = 0
        self.flg_ack = 0
        self.flg_urg = 0
        self.window = 0
        self.chksum = 0
        self.urg_prt = 0
        self.header_size = 0
        self.data_size = 0
        self.data = 0

class OutPacket():
    """
    Class that represents a single outgoing TCP packet
    """

    def tcp_checksum(self, msg):
        """
        Computes the checksum of a single TCP/IP packet
        """
        s = 0
        for i in range(0, len(msg), 2):
            w = ord(msg[i]) + (ord(msg[i+1]) << 8 )
            s = s + w
        s = (s>>16) + (s & 0xffff);
        s = s + (s >> 16);
        s = ~s & 0xffff
        return s

    def ip(self):
        """
        Builds a single IP header with proper
        source and destination ip address
        """
        version = 4
        ihl = 5 # Internet Header Length
        tos = 0 # Type of Service
        tl = 0 # total length will be filled by kernel
        id = 54321
        flags = 0 # More fragments
        offset = 0
        ttl = 255
        protocol = socket.IPPROTO_TCP
        checksum = 0 # will be filled by kernel
        source = socket.inet_aton(self.srcip)
        destination = socket.inet_aton(self.dstip)
        ver_ihl = (version << 4) + ihl
        flags_offset = (flags << 13) + offset
        ip_header = struct.pack("!BBHHHBBH4s4s",
                                ver_ihl,
                                tos,
                                tl,
                                id,
                                flags_offset,
                                ttl,
                                protocol,
                                checksum,
                                source,
                                destination)
        return ip_header

    def tcp(self):
        """
        Builds a single TCP header with proper checksum
        and supports additional payload data
        """
        data_offset = (self.offset << 4) + 0
        flags = self.fin + (self.syn << 1) + (self.rst << 2) + (self.psh << 3) + (self.ack << 4) + (self.urg << 5)
        tcp_header = struct.pack('!HHLLBBHHH',
                                 self.srcp,
                                 self.dstp,
                                 self.seqn,
                                 self.ackn,
                                 data_offset,
                                 flags,
                                 self.window,
                                 self.checksum,
                                 self.urgp)
        #pseudo header fields
        source_ip = socket.inet_aton(self.srcip)
        destination_ip = socket.inet_aton(self.dstip)
        reserved = 0
        protocol = socket.IPPROTO_TCP
        total_length = len(tcp_header) + len(self.payload)
        # Pseudo header
        psh = struct.pack("!4s4sBBH",
                    source_ip,
                    destination_ip,
                    reserved,
                    protocol,
                    total_length)
        psh = psh + tcp_header + self.payload
        tcp_checksum = self.tcp_checksum(psh)
        tcp_header = struct.pack("!HHLLBBH",
                            self.srcp,
                            self.dstp,
                            self.seqn,
                            self.ackn,
                            data_offset,
                            flags,
                            self.window)
        tcp_header += struct.pack('H', tcp_checksum)
        tcp_header += struct.pack('!H', self.urgp)
        return tcp_header

    def packet(self):
        """
        Build a single TCP/IP header by combinging
        IP header, TCP header and payload
        """
        ip = self.ip()
        tcp = self.tcp()
        return ip + tcp + self.payload

    def __init__(self, sock, data=''):
        self.srcip = sock.src_ip
        self.dstip = sock.dst_ip
        self.srcp = sock.src_port
        self.dstp = sock.dst_port
        self.seqn = sock.seqn
        self.ackn = sock.ackn
        self.offset = 5 # Data offset: 5x4 = 20 bytes
        self.reserved = 0
        self.urg = 0
        self.ack = 0
        self.psh = 0
        self.rst = 0
        self.syn = 0
        self.fin = 0
        self.window = socket.htons(5840)
        self.checksum = 0
        self.urgp = 0
        self.data = 0

        if len(data) % 2 == 1:
            data += "0"
        self.payload = data

class RawSocket():
    """
    Class that represents a single socket, backed by raw sockets
    Attempts to mimick api of the built in socket class
    """

    def connect(self, domain, port):
        """
        Connects to the given domain by performing
        the TCP 3 way handshake
        """
        # set ip and port number
        self.dst_ip = socket.gethostbyname(domain)
        self.dst_port = port
        # send syn
        self.send_syn()
        # receive syn/ack
        self.recv_next()
        # send ack
        self.send_ack()

    def send_syn(self):
        """
        Sends a single syn packet
        """
        syn = OutPacket(self)
        syn.syn = 1
        self.socket.sendto(syn.packet(), (self.dst_ip, 0))

    def send_ack(self):
        """
        Sends a single ack packet
        """
        ack = OutPacket(self)
        ack.ack = 1
        self.socket.sendto(ack.packet(), (self.dst_ip, 0))

    def send_fin(self):
        """
        Sends a single fin packet
        """
        fin = OutPacket(self)
        fin.ack = 1
        fin.fin = 1
        self.socket.sendto(fin.packet(), (self.dst_ip, 0))

    def send(self, data):
        """
        Sends a packet with payload data, also known as a push
        """
        packet = OutPacket(self, data)
        packet.ack = 1
        packet.psh = 1
        self.socket.sendto(packet.packet(), (self.dst_ip, 0))
        self.recv_next()

    def recv_next(self, bytes=65565):
        """
        Reads in one packet and properly checks to make
        sure it is a packet intented for this program
        Also updates our syn and ack sequence numbers
        """
        start = time.time()
        time.clock()
        elapsed = 0
        while elapsed < 180:
            elapsed = time.time() - start
            packet = self.rsocket.recvfrom(65565)
            ip = packet[1][0]
            if ip == self.dst_ip:
                packet = InPacket(packet).parse()
                self.seqn = packet.ackn
                self.ackn = packet.seqn + packet.data_size + 1
                return packet

        raise Exception("Timeout")

    def recv(self, bytes=65565):
        """
        Recieves the body of the response and returns it as a string
        """
        data = ""
        while True:
            packet = self.recv_next()
            data += packet.data
            self.send_ack()
            if packet.flg_fin:
                return data

    def close(self):
        """
        Closes out the TCP connection
        """
        self.send_fin()
        self.recv_next()
        self.send_ack()

    def __init__(self):
        self.src_ip = socket.gethostbyname(socket.gethostname())
        self.src_port = random.randint(49152,65535)
        self.dst_ip = "127.0.0.1"
        self.dst_port = 80
        self.seqn = random.randint(200,9999)
        self.ackn = 0
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
        self.rsocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)

def rawsocket():
    """
    Return an instance of a single raw socket
    """
    return RawSocket()


# -*- python -*-

load("//tools/workspace:github.bzl", "github_archive")

def ignition_math_repository(
        name,
        mirrors = None):
    # When updating this commit, also remember to adjust the PROJECT_*
    # constants in ./package.BUILD.bazel to match the new version number.
    github_archive(
        name = name,
        repository = "ignitionrobotics/ign-math",
        commit = "ignition-math6_6.8.0",
        sha256 = "07c4fa9eeaa51fe2b4ee2855ea69bb16571d7e6c315b8b0726fc4383b64303a1",  # noqa
        build_file = "@drake//tools/workspace/ignition_math:package.BUILD.bazel",  # noqa
        mirrors = mirrors,
    )


from NeuralNet import NeuralNet, kerasmodel
import numpy as np
import tensorflow as tf

model = tf.keras.models.load_model('finalmodel.keras')
X_Test = np.linspace(0, 1.0, 100)
Y_Test = np.sin(X_Test*2*np.pi)
Y_Pred = model.predict(X_Test)
print(Y_Pred)
'''
minscore = 1e300
minparams = None
with open('dumpfile.dat', "r") as f:
    for line in f:
        col = line.split('|')
        parameters = np.array([float(x) for x in col[0].split()])
        score = float(col[1])
        if score < minscore:
            minscore = score
            minparams = parameters
            print(minscore)
print(minparams)
weights = model.get_weights()
nweights = 0
for i, layer in enumerate(weights):
    for j, x in np.ndenumerate(layer):
        weights[i][j] = minparams[nweights]
        nweights += 1
model.set_weights(weights)

'''

#A function which plots the data set and the function y = sin(x) using matplotlib.
def plot(X, Y, Y_Pred):
    import matplotlib.pyplot as plt
    plt.plot(X, Y, 'o')
    plt.plot(X, Y_Pred)
    plt.show()
#    plt.savefig('fig.png')

plot(X_Test, Y_Test, Y_Pred)


import re
import traceback

import requests
from bs4 import BeautifulSoup

HOME_URL = "http://ntucoder.net/Message"

comments = set()

url = HOME_URL

page = 1
while True:
    try:
        r = requests.get(url)
        soup = BeautifulSoup(r.content, 'lxml')
        for comment in soup.select('div.message-info > div'):
            txt = re.sub('^[^:]+:', '', comment.text.strip()).strip()
            if txt != '':
                comments.add(txt)
        url = "http://ntucoder.net{}".format(soup.select_one('#view-previous-con > a')['href'])
        page+= 1
    except:
        traceback.print_exc()
        break

print("Total pages = {}".format(page))

with open('ntucoder.txt', 'w', encoding='utf8') as fp:
    for x in comments:
        fp.write(x + '\n\n')


"""
list comprehensions
readble, expressive, and effective
"""
from math import factorial, sqrt
from pprint import pprint as pp

def prime(p):
    if p <2:
        return False
    for i in range(2, int(sqrt(p))+1):
        if p % i == 0:
            return False
    return True

def main():
    """
    test function
    :return:
    """
    words = 'today i am very happy to learn about list comprehensions'.split()
    #print(words)
    data = [] # empty list
    for word in words:
        #some analysis
        data.append(word)

    #filter data
    print(words)

    #task: find the length of the first 20 factorial numbers
    #factorial numbers
    length = []
    for x in range(20):
        #print(factorial(x))
        length.append(len(str(factorial(x))))

    print(length)

    #use a list comprehensoin: []
    info2 = [len(str(factorial(x))) for x in range(20)]
    #print(info2)
   # pp(info2)

    #set comprehensions: ()
    info3 = {len(str(factorial(x))) for x in range(100)}
    #pp(info3)

    #dictionary comprehensions
    nbateams = {"jazz": "slc","warriors":"oakland","lakers":"la", 'clippers':'la'}

    teamsnba = {city:mascot for mascot, city in nbateams.items()}
    pp(nbateams)
    pp(teamsnba)

    #filter predicates

    primenums = [x for x in range(10) if prime(x)]
    pp(len(primenums))

if __name__ == '__main__':
    main()
    exit(0)


import numpy as np
import sklearn.neighbors
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib.patches
import scipy
import re
import subprocess
import itertools

import pdb

###############################################################################
#
# SIMPLICIAL COMPLEXES
#
###############################################################################

def triangle_cliques(connections):
    """
    Returns all 3-cliques implied by the upper-triangular sparse connections
    matrix.
    """
    triangles = []
    for i in range(connections.shape[0]):
        adjacency = connections.getrow(i)
        # ignore self loops
        adjacency[0,i] = False
        neighbors = np.nonzero(adjacency)[1].tolist()
        while neighbors:
            j = neighbors.pop(0)
            for k in neighbors:
                if connections[j,k]:
                    triangles.append([i,j,k])

    return triangles

def kneighbors_2skeleton(data, k):
    """
    Builds the 2-skeleton of the data by connecting each point to its k nearest
    neighbors, then making triangles out of sets of 3 mutually-connected
    vertices. Returns a list of edges and a list of triangles.
    """
    # compute the k nearest neighbors
    knn = sklearn.neighbors.kneighbors_graph(data, k)
    # make the connections symmetric, and return only the upper triangle
    connections = scipy.sparse.triu((knn + knn.T).astype(bool)).tocsr()

    # find mutually-connected triangles
    triangles = triangle_cliques(connections)

    # get the list of edges
    u,v = np.nonzero(connections)
    edges = zip(u.tolist(), v.tolist())
    # remove self edges
    edges = [e for e in edges if e[0] != e[1]]

    return edges, triangles

def simplify_triangle_edges(edges, tris):
    simplified_edges = list(edges)
    for tri in tris:
        for p,q in itertools.combinations(tri, 2):
            if (p,q) in simplified_edges:
                simplified_edges.remove((p,q))
            if (q,p) in simplified_edges:
                simplified_edges.remove((q,p))
    return simplified_edges

def plot_2d_complex(data, edges, tris):
    """
    Plots a complex whose points are embedded in 2d Euclidean space.
    """
    # draw the points
    plt.scatter(data[:,0], data[:,1])
    # now draw each edge
    for u,v in edges:
        x = data[(u,v),0]
        y = data[(u,v),1]
        plt.plot(x, y, color='black', alpha=0.5)

    for u,v,w in tris:
        points = data[(u,v,w),:]
        poly = matplotlib.patches.Polygon(points, facecolor='red', alpha=0.2,
                color='red')
        plt.gca().add_patch(poly)

    plt.show()

###############################################################################
#
# GRAPH FUNCTIONS
#
###############################################################################

def geodesic_distance(plex, root=None):
    """
    Given a simplicial complex in the form of a networkx graph, computes the
    geodesic distance to each node from a given root node. If the root is not
    specified, an arbitrary extreme root is found by eccentricity. Returns a
    list of distances.

    Assumes the graph is connected.
    """
    if root is None:
        # compute the eccentricity of each node
        max_distances = nx.eccentricity(plex)
        root = max(max_distances.items(), key=lambda x: x[1])[0]

    # compute the distance from the root to each node
    distances = nx.shortest_path_length(plex, root)
    return [distances[v] for v in sorted(plex.nodes())]

###############################################################################
#
# FUNCTIONS ON REEB GRAPHS
#
###############################################################################

def is_regular(graph, node):
    """
    Determines whether a node in the graph is regular.
    A node is regular if:
        1) It is of degree 2.
        2) It is the only path between its neighbors.
    """
    if nx.degree(graph, node) != 2:
        return False

    # get the parents, of which there are two
    u,v = nx.neighbors(graph, node)

    n_simple_paths = 0
    for path in nx.all_simple_paths(graph, u, v):
        n_simple_paths += 1
        if n_simple_paths > 1:
            return False
    else:
        return True

def contract(reeb_graph):
    """
    Given an augmented reeb graph, contracts it, returning a graph where each
    edge has a "members" property listing the nodes it contains.
    """
    g = reeb_graph.copy()
    # give every edge the members property
    for u,v in g.edges_iter():
        g[u][v]['members'] = set()

    # now reduce each that is regular
    for node in g.nodes():
        if is_regular(g, node):
            # get the neighbors
            u,v = nx.neighbors(g, node)
            # link them
            g.add_edge(u,v)

            # record the members of the edges
            members = set([node])
            members = members | g.edge[u][node]['members']
            members = members | g.edge[v][node]['members']
            g.edge[u][v]['members'] = members

            # delete the original node
            g.remove_node(node)

    return g

###############################################################################
#
# RANDREEB INTERFACE
#
###############################################################################

def randreeb(heights, edges, tris):
    """
    Interfaces with the randreeb program to compute the reeb graph of a
    function defined on the 2-skeleton of a simplicial complex.
    """
    import os
    module_path = os.path.dirname(os.path.abspath(__file__))
    os.chdir(module_path)
    write_OFF("complex.off", heights, edges, tris)
    # call randreeb
    output = subprocess.check_output(["./randreeb/ReebGraph",
            "-z", "-a", "complex.off"])
    # read the output
    values, edges = read_OFF("out_aug.off")
    return edges

###############################################################################
#
# OFF FILE HANDLING
#
###############################################################################

def read_OFF(filepath):
    """
    Reads an OFF file, returning a list of values on the vertices, and a list
    of faces (edges are ignored).
    """
    with open(filepath,"r") as f:
        # read until we get to the first line containing the counts
        line = f.readline()
        pattern = "^(\d+) (\d+) (\d+)"
        matches = re.findall(pattern, line)
        while not matches:
            line = f.readline()
            matches = re.findall(pattern, line)
        n_vertices, n_faces, _ = [int(x) for x in matches[0]]

        # read the vertices in, taking only the last number of each line
        values = []
        for i in range(n_vertices):
            line = f.readline().strip()
            vertex = [float(x) for x in line.split()]
            values.append(vertex[-1])

        # read the faces in
        faces = []
        for i in range(n_faces):
            line = f.readline().strip()
            face = [int(x) for x in line.split()]
            dim = face[0]
            if face[-4:] == [1,0,0,1] or True:
                faces.append(face[1:dim+1])

        return values, faces

def write_OFF(filepath, values, edges, faces):
    """
    Writes an OFF file that is readable by the above.
    """
    with open(filepath,"w+") as f:
        f.write("OFF\n")
        f.write("{} {} 0\n".format(len(values), len(edges)+len(faces)))
        for v in values:
            f.write("0 0 {}\n".format(v))

        for x in edges+faces:
            f.write("{} ".format(len(x)))
            f.write(" ".join([str(y) for y in x]))
            f.write("\n")


# -*- coding: utf-8 -*-
# __author__ = 'qingmei'
# last modify 20180515

from biocluster.agent import Agent
from biocluster.tool import Tool
from biocluster.core.exceptions import OptionError
import os

class SnpeffGffcheckAgent(Agent):
    """
    参考基因组snpeff check ref.gff生成genes.gff
    """
    def __init__(self,parent):
        super(SnpeffGffcheckAgent,self).__init__(parent)
        options = [
            {"name": "refgff", "type": "string"},  # one col
        ]
        self.add_option(options)

    def check_options(self):
        if not self.option("refgff"):
            raise OptionError("请输入ref.gff", code="34505801") # 必须有

    def set_resource(self):
        """
        运行所需资源
        """
        self._cpu = 2
        self._memory = '2G'

    def end(self):
        super(SnpeffGffcheckAgent,self).end()
########

class SnpeffGffcheckTool(Tool):
    def __init__(self, config):
        super(SnpeffGffcheckTool, self).__init__(config)
        self.gffcheck_path = self.config.PACKAGE_DIR + "/wgs/gffcheck.pl"
        self.perl_path = 'miniconda2/bin/perl '

    def SnpeffGffcheck(self):
        """
        要重新写下！！！
        :return:
        """
        cmd = "{} {} -i {}  -o {}"\
            .format(self.perl_path, self.gffcheck_path, self.option("refgff"),
                    self.output_dir + "/genes.gff")
        self.logger.info(cmd)
        self.logger.info("开始进行SnpeffGffcheck")
        command = self.add_command("snpeffgffcheck", cmd).run()  # nranno必须小写，
        self.wait(command)
        if command.return_code == 0:
            self.logger.info("SnpeffGffcheck完成！")
        else:
            self.set_error("SnpeffGffcheck出错！", code="34505801")
            self.set_error("SnpeffGffcheck出错！", code="34505804")

    def run(self):
        super(SnpeffGffcheckTool, self).run()
        self.SnpeffGffcheck()
        self.end()


from pytesseract import pytesseract
from googletrans import Translator

t = Translator()
path =  r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image = r'img_name.png'

pytesseract.tesseract_cmd = path_to_tesseract

img = Image.open(image)
text = pytesseract.image_to_string(img)
after_translate = t.translate(text[:-1], dest="he")

print(after_translate.text[::-1]) ### you dont have to add the [::-1] i added it because vs prints hebrew reversed


class Solution:
    def getHint(self, secret: str, guess: str) -> str:
        x,y=0,0
        secret1 = list(secret)
        guess1 = list(guess)
        for i in range(len(secret1)):
            if secret1[i] == guess1[i]:
                x+=1
                secret1[i] = "A"
                guess1[i] = "B"
        for j in guess1:
            if j in secret1:
                y+=1
                secret1.remove(j)
        return ("%dA%dB" %(x,y))


"""
Simple Bot taken from the python-telegram-bot examples.
Deployed using heroku.
"""

import logging
import os
import random
import string

import telegram
from PIL import Image
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext

import secrets
import static

def get_random_alphanumeric_string(length):
    """ Get a string of random alphanumeric characters of specified length"""
    return ''.join((random.choice(string.ascii_letters + string.digits) for _ in range(length)))

TOKEN = os.environ['ENV_BOT_TOKEN']
PORT = int(os.environ.get('PORT', 5000))

# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)

logger = logging.getLogger(__name__)

# Define a few command handlers. These usually take the two arguments update and
# context. Error handlers also receive the raised TelegramError object in error.
def start(update, context):
    """Send a message when the command /start is issued."""
    update.message.reply_text(static.start_text, parse_mode=telegram.ParseMode.MARKDOWN_V2)

def help_nandler(update, context):
    """Send a message when the command /help is issued."""
    update.message.reply_text(static.commands_text, parse_mode=telegram.ParseMode.MARKDOWN_V2)

def coords(update, context):
    """Send the coordinates picture"""
    try:
        y, x = map(float, update.message.text.split()[1:])
        if y < static.min_h or x < static.min_w or y > static.max_h or x > static.max_w:
            raise ValueError
    except ValueError:
        update.message.reply_text('Incorrect coordinates specified')
        return

    background = Image.open('res/map.jpg', 'r')
    bg_w, bg_h = background.size
    fg_size = int(min(bg_w, bg_h) * 7 / 100)
    foreground = Image.open('res/loc.png', 'r').convert('RGBA').resize((fg_size, fg_size))

    offset = (int((x - static.min_w) / (static.max_w - static.min_w) * bg_w - fg_size / 2),
              int(bg_h - (y - static.min_h) / (static.max_h - static.min_h) * bg_h - fg_size))
    background.paste(foreground, offset, foreground)  # third parameter is alpha mask

    filename = get_random_alphanumeric_string(12)
    background.save(f'res/{filename}.png')
    update.message.reply_photo(photo=open(f'res/{filename}.png', 'rb'))
    os.remove(f'res/{filename}.png')

def location(update: Update, context: CallbackContext):
    user_location = update.message.location
    if static.min_w <= user_location.longitude <= static.max_w and \
            static.min_h <= user_location.latitude <= static.max_h:
        update.message.reply_text(f'You are on point! '
                                  f'Your location is: {user_location.longitude} {user_location.latitude}')
        update.message.text = f'/coords {user_location.latitude} {user_location.longitude}'
        coords(update, context)
    else:
        update.message.reply_text(f'You are not inside the map. '
                                  f'Your location however is: {user_location.longitude} {user_location.latitude}')

def error(update, context):
    """Log Errors caused by Updates."""
    logger.warning('Update "%s" caused error "%s"', update, context.error)

def main():
    """Start the bot."""
    # Create the Updater and pass it your bot's token.
    # Make sure to set use_context=True to use the new context based callbacks
    # Post version 12 this will no longer be necessary
    updater = Updater(TOKEN, use_context=True)

    # Get the dispatcher to register handlers
    dp = updater.dispatcher

    # on different commands - answer in Telegram
    dp.add_handler(CommandHandler("start", start))
    dp.add_handler(CommandHandler("help", help_nandler))
    dp.add_handler(CommandHandler("coords", coords))
    dp.add_handler(MessageHandler(Filters.location, location))

    # log all errors
    dp.add_error_handler(error)

    # Start the Bot
    # Use updater instead of poller
    updater.start_webhook(listen="0.0.0.0",
                          port=int(PORT),
                          url_path=TOKEN)
    updater.bot.setWebhook('https://ams-test-task.herokuapp.com/' + TOKEN)

    # Run the bot until you press Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()

if __name__ == '__main__':
    main()


import pandas as pd
from pixels.behaviours.reach import Events
from pixtools import rolling_bin

def get_rolling_bins(exp, units, al, ci_s, step, ci_e, bl_s, bl_e, increment):
    """
    get confidence intervals with rolling bins for different cell types.

    parameters:
    ===

    exp: Experiment class from pixels.

    units: a list of units of interest with their spike rates, selected from
    exp.select_units(). Currently 'pyramidal' & 'interneurons', but can be any
    categories.

    al: actionlabels.

    ci_s: start of confidence interval calculation.

    step: ci bin size.

    ci_e: end of confidence interval calculation.

    bl_s: start of ci baseline calculation.

    bl_e: end of ci baseline calculation.

    increment: overlap between bins.
    """
    if repr(units).count("cortex") == 0:
        cis = []
        for u in units:
            even_bins = exp.get_aligned_spike_rate_CI(
                al,
                Events.led_on,
                start=ci_s,
                step=step,
                end=ci_e,
                bl_start=bl_s,
                bl_end=bl_e,
                units=u,
            )
            odd_bins = exp.get_aligned_spike_rate_CI(
                al,
                Events.led_on,
                start=ci_s + increment,
                step=step,
                end=ci_e + increment,
                bl_start=bl_s,
                bl_end=bl_e,
                units=u,
            )
            cis_even = rolling_bin.rename_bin(
                df=even_bins, l=3, names=[0, 200, 400, 600, 800]
            )
            cis_odd = rolling_bin.rename_bin(
                df=odd_bins, l=3, names=[100, 300, 500, 700, 900]
            )
            cis.append((cis_even, cis_odd))

        # concat even & odd bins within each cell type category, then concat
        # categories to form the final df
        cis_u0 = pd.concat([cis[0][0], cis[0][1]], axis=1)
        cis_u1 = pd.concat([cis[1][0], cis[1][1]], axis=1)
        cis = pd.concat([cis_u0, cis_u1], axis=1, keys=[0, 1], names=["cell type"])

    else:
        even_bins = exp.get_aligned_spike_rate_CI(
            al,
            Events.led_on,
            start=ci_s,
            step=step,
            end=ci_e,
            bl_start=bl_s,
            bl_end=bl_e,
            units=units,
        )
        odd_bins = exp.get_aligned_spike_rate_CI(
            al,
            Events.led_on,
            start=ci_s + increment,
            step=step,
            end=ci_e + increment,
            bl_start=bl_s,
            bl_end=bl_e,
            units=units,
        )
        cis_even = rolling_bin.rename_bin(
            df=even_bins, l=3, names=[0, 200, 400, 600, 800]
        )
        cis_odd = rolling_bin.rename_bin(
            df=odd_bins, l=3, names=[100, 300, 500, 700, 900]
        )
        cis = pd.concat([cis_even, cis_odd], axis=1)

    return cis


## from ctypes import *
#ctypes.cdll.LoadLibrary('')
#lib1 = CDLL("deps/sparse_rrt/deps/trajopt/build/lib/libsco.so")
#lib2 = CDLL("deps/sparse_rrt/deps/trajopt/build/lib/libutils.so")

import sys
sys.path.append('/media/arclabdl1/HD1/YLmiao/mpc-mpnet-cuda-yinglong/deps/sparse_rrt-1')
sys.path.append('.')

from sparse_rrt.planners import SST
#from env.cartpole_obs import CartPoleObs
#from env.cartpole import CartPole
#from sparse_rrt.systems import standard_cpp_systems
from sparse_rrt import _sst_module
import numpy as np
import time
from plan_utility.line_line_cc import *
import pickle
from sparse_rrt import _deep_smp_module, _sst_module
import os
import matplotlib
matplotlib.use("Agg")
obs_list = []
LENGTH = 20.
width = 8.
near = width * 1.2
# convert from obs to point cloud
# load generated point cloud
obs_list_total = []
obc_list_total = []
for i in range(10):
    file = open('/media/arclabdl1/HD1/YLmiao/data/kinodynamic/car_obs/obs_%d.pkl' % (i), 'rb')
    obs_list_total.append(pickle.load(file))
    file = open('/media/arclabdl1/HD1/YLmiao/data/kinodynamic/car_obs/obc_%d.pkl' % (i), 'rb')
    obc_list_total.append(pickle.load(file))

#[(0, 932), (1, 935), (2, 923), (8, 141), (5,931), (7, 927)]
# (5,931), (6, 286)

for video_i in range(20):
    obs_idx = np.random.randint(low=5,high=10)
    p_idx = np.random.randint(low=800,high=1000)
    #p_idx = 900
    print('obs_idx: ')
    print(obs_idx)
    print('p_idx:')
    print(p_idx)

    # Create custom system
    #obs_list = [[-10., -3.],
    #            [0., 3.],
    #            [10, -3.]]
    obs_list = obs_list_total[obs_idx]
    obc_list = obc_list_total[obs_idx]

    params = {
        'solver_type' : "cem",
        'n_sample': 1024,
        'n_elite': 128,
        'n_t': 2,
        'max_it': 200,
        'converge_r': 1e-10,

        'dt': 2e-3,

        'mu_u': np.array([1., 1.]) ,
        'sigma_u': np.array([1., 0.5]),

        'mu_t': .5,
        'sigma_t': .5,
        't_max': 1.,

        'verbose': False,#True,#
        'step_size': 0.8,

        "goal_radius": 2.0,

        "sst_delta_near": .001,
        "sst_delta_drain": .0005,
        "goal_bias": 0.08,

        "width": 8,
        "hybrid": False,
        "hybrid_p": 0.0,

        "cost_samples": 1,
        "mpnet_weight_path":"/home/arclabdl1/YLmiao/kinodynamics/mpc-mpnet-cuda-yinglong/mpnet/exported/output/car_obs/mpnet_10k_external_small_model.pt",
        #"mpnet_weight_path":"mpnet/exported/output/cartpole_obs/mpnet_10k_external_v2_deep.pt",
        #"mpnet_weight_path":"mpnet/exported/output/cartpole_obs/mpnet_10k.pt",

        # "mpnet_weight_path":"mpnet/exported/output/cartpole_obs/mpnet_10k_nonorm.pt",
        # "mpnet_weight_path":"mpnet/exported/output/cartpole_obs/mpnet_subsample0.5_10k.pt",

        "cost_predictor_weight_path": "/home/arclabdl1/YLmiao/kinodynamics/mpc-mpnet-cuda-yinglong/mpnet/exported/output/cartpole_obs/cost_10k.pt",
        "cost_to_go_predictor_weight_path": "/home/arclabdl1/YLmiao/kinodynamics/mpc-mpnet-cuda-yinglong/mpnet/exported/output/cartpole_obs/cost_to_go_10k.pt",

        "refine": False,
        "using_one_step_cost": False,
        "refine_lr": 0,
        "refine_threshold": 0,
        "device_id": "cuda:2",

        "cost_reselection": False,
        "number_of_iterations": 5000,
        "weights_array": [1, 1.0, .5],

    }

    # load path
    path = np.load('/media/arclabdl1/HD1/YLmiao/mpc-mpnet-cuda-yinglong/results/cpp_full/car_obs/default/paths/path_%d_%d.npy' % (obs_idx, p_idx), allow_pickle=True)[0]

    sgs = open('/media/arclabdl1/HD1/YLmiao/data/kinodynamic/car_obs/%d/start_goal_%d.pkl' % (obs_idx, p_idx), 'rb')
    sgs = pickle.load(sgs)

    planner = _deep_smp_module.DSSTMPCWrapper(
        system_type='car_obs',
        solver_type="cem",
        start_state=np.array(path[0]),
    #             goal_state=np.array(ref_path[-1]),
        goal_state=np.array(sgs[-1]),
        goal_radius=params['goal_radius'],
        random_seed=0,
        sst_delta_near=params['sst_delta_near'],
        sst_delta_drain=params['sst_delta_drain'],
        obs_list=obs_list,
        width=params['width'],
        verbose=params['verbose'],
        mpnet_weight_path=params['mpnet_weight_path'],
        cost_predictor_weight_path=params['cost_predictor_weight_path'],
        cost_to_go_predictor_weight_path=params['cost_to_go_predictor_weight_path'],
        num_sample=params['cost_samples'],
        #np=params['n_problem'],
        ns=params['n_sample'], nt=params['n_t'], ne=params['n_elite'], max_it=params['max_it'],
        converge_r=params['converge_r'], mu_u=params['mu_u'], std_u=params['sigma_u'], mu_t=params['mu_t'],
        std_t=params['sigma_t'], t_max=params['t_max'], step_size=params['step_size'], integration_step=params['dt'],
        device_id=params['device_id'], refine_lr=params['refine_lr'],
        weights_array=params['weights_array'],
        obs_voxel_array=obc_list.reshape(-1)
    )

    def enforce_bounds(state):
        '''
        check if state satisfies the bound
        apply threshold to velocity and angle
        return a new state toward which the bound has been enforced
        '''
        WIDTH = 2.0
        LENGTH = 1.0

        STATE_X = 0
        STATE_Y = 1

        STATE_THETA = 2
        MIN_X = -25
        MAX_X = 25
        MIN_Y = -35
        MAX_Y = 35

        new_state = np.array(state)
        """
        if state[STATE_V] < MIN_V/30.:
            new_state[STATE_V] = MIN_V/30.
        elif state[STATE_V] > MAX_V/30.:
            new_state[STATE_V] = MAX_V/30.
        """
        if state[2] < -np.pi:
            new_state[2] += 2*np.pi
        elif state[2] > np.pi:
            new_state[2] -= 2*np.pi
        return new_state

    def overlap(b1corner,b1axis,b1orign,b1dx,b1dy,b2corner,b2axis,b2orign,b2dx,b2dy):
        # this only checks overlap of b1 w.r.t. b2
        # a complete check should do in both directions
        b2ds = [b2dx, b2dy]
        for a in range(0,2):
            t=b1corner[0][0]*b2axis[a][0]+b1corner[0][1]*b2axis[a][1] # project corner to the axis by inner product

            tMin = t
            tMax = t
            for c in range(1,4):
                t = b1corner[c][0]*b2axis[a][0]+b1corner[c][1]*b2axis[a][1] # project corner to the axis by inner product
                # find range by [tMin, tMax]
                if t < tMin:
                    tMin = t
                elif t > tMax:
                    tMax = t
            # since b2 the other corners (corner 1, 2, 3) are larger than b2orign (project of corner 0 to axis)
            # specifically, the range is [b2orign[i], b2orign[i]+size(i)] (of the projected point by dot product)
            # we only need to compare tMax with b2orign[i], and tMin with size(i)+b2orign[i]
            if ((tMin > (b2ds[a] + b2orign[a])) or (tMax < b2orign[a])):
                return False

        return True

    def IsInCollision(x, obc, obc_width=8.):
        car_width = 2.0
        car_len = 1.0
        width = 8.0
        WIDTH = car_width
        LENGTH = car_len
        STATE_X = 0
        STATE_Y = 1
        STATE_THETA = 2
        MIN_X = -25
        MAX_X = 25
        MIN_Y = -35
        MAX_Y = 35
        if x[0] < MIN_X or x[0] > MAX_X or x[1] < MIN_Y or x[1] > MAX_Y:
            return True

        robot_corner=np.zeros((4,2),dtype=np.float32)
        robot_axis=np.zeros((2,2),dtype=np.float32)
        robot_orign=np.zeros(2,dtype=np.float32)
        length=np.zeros(2,dtype=np.float32)
        X1=np.zeros(2,dtype=np.float32)
        Y1=np.zeros(2,dtype=np.float32)

        X1[0]=np.cos(x[STATE_THETA])*(WIDTH/2.0)
        X1[1]=-np.sin(x[STATE_THETA])*(WIDTH/2.0)
        Y1[0]=np.sin(x[STATE_THETA])*(LENGTH/2.0)
        Y1[1]=np.cos(x[STATE_THETA])*(LENGTH/2.0)

        for j in range(0,2):
            # order: (left-bottom, right-bottom, right-upper, left-upper)
            # assume angle (state_theta) is clockwise
            robot_corner[0][j]=x[j]-X1[j]-Y1[j]
            robot_corner[1][j]=x[j]+X1[j]-Y1[j]
            robot_corner[2][j]=x[j]+X1[j]+Y1[j]
            robot_corner[3][j]=x[j]-X1[j]+Y1[j]

            # axis: horizontal and vertical
            robot_axis[0][j] = robot_corner[1][j] - robot_corner[0][j]
            robot_axis[1][j] = robot_corner[3][j] - robot_corner[0][j]

        #print('robot corners:')
        #print(robot_corner)
        length[0]=np.sqrt(robot_axis[0][0]*robot_axis[0][0]+robot_axis[0][1]*robot_axis[0][1])
        length[1]=np.sqrt(robot_axis[1][0]*robot_axis[1][0]+robot_axis[1][1]*robot_axis[1][1])
        # normalize the axis
        for i in range(0,2):
            for j in range(0,2):
                robot_axis[i][j]=robot_axis[i][j]/float(length[i])

        # obtain the projection of the left-bottom corner to the axis, to obtain the minimal projection length
        robot_orign[0]=robot_corner[0][0]*robot_axis[0][0]+ robot_corner[0][1]*robot_axis[0][1]
        robot_orign[1]=robot_corner[0][0]*robot_axis[1][0]+ robot_corner[0][1]*robot_axis[1][1]
        #print('robot orign:')
        #print(robot_orign)
        for i in range(len(obc)):
            cf=True

            obs_corner=np.zeros((4,2),dtype=np.float32)
            obs_axis=np.zeros((2,2),dtype=np.float32)
            obs_orign=np.zeros(2,dtype=np.float32)
            length2=np.zeros(2,dtype=np.float32)

            for j in range(0,2):
                # order: (left-bottom, right-bottom, right-upper, left-upper)
                obs_corner[0][j] = obc[i][j]
                obs_corner[1][j] = obc[i][2+j]
                obs_corner[2][j] = obc[i][2*2+j]
                obs_corner[3][j] = obc[i][3*2+j]

                # horizontal axis and vertical
                obs_axis[0][j] = obs_corner[1][j] - obs_corner[0][j]
                obs_axis[1][j] = obs_corner[3][j] - obs_corner[0][j]
                #obs_axis[0][j] = obs_corner[3][j] - obs_corner[0][j]
                #obs_axis[1][j] = obs_corner[1][j] - obs_corner[0][j]

            length2[0]=np.sqrt(obs_axis[0][0]*obs_axis[0][0]+obs_axis[0][1]*obs_axis[0][1])
            length2[1]=np.sqrt(obs_axis[1][0]*obs_axis[1][0]+obs_axis[1][1]*obs_axis[1][1])

            # normalize the axis
            for i1 in range(0,2):
                for j1 in range(0,2):
                    obs_axis[i1][j1]=obs_axis[i1][j1]/float(length2[i1])

            # obtain the inner product of the left-bottom corner with the axis to obtain the minimal of projection value
            obs_orign[0]=obs_corner[0][0]*obs_axis[0][0]+ obs_corner[0][1]*obs_axis[0][1]  # dot product at 0-th corner
            obs_orign[1]=obs_corner[0][0]*obs_axis[1][0]+ obs_corner[0][1]*obs_axis[1][1]
            # do checking in both direction (b1 -> b2, b2 -> b1). If at least one shows not-overlapping, then it is not overlapping
            cf=overlap(robot_corner,robot_axis,robot_orign,car_width,car_len,obs_corner,obs_axis,obs_orign,width,width)
            cf=cf and overlap(obs_corner,obs_axis,obs_orign,width,width,robot_corner,robot_axis,robot_orign,car_width,car_len)
            if cf==True:
                return True
        return False

    def wrap_angle(x, system):
        circular = system.is_circular_topology()
        res = np.array(x)
        for i in range(len(x)):
            if circular[i]:
                # use our previously saved version
                res[i] = x[i] - np.floor(x[i] / (2*np.pi))*(2*np.pi)
                if res[i] > np.pi:
                    res[i] = res[i] - 2*np.pi
        return res

    from visual.visualizer import Visualizer

    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    import matplotlib as mpl
    import matplotlib.patches as patches
    from matplotlib.colors import LinearSegmentedColormap

    class CarVisualizer(Visualizer):
        def __init__(self, system, params):
            super(CarVisualizer, self).__init__(system, params)
            self.dt = 2
            self.fig = plt.gcf()
            self.fig.set_figheight(5)
            self.fig.set_figwidth(10)
            self.ax1 = plt.subplot(121)
            #self.ax2 = plt.subplot(122)
            self.ax2 = self.fig.add_subplot(122, projection='3d')

        def _init(self):
            # clear the current ax
            ax = self.ax1
            ax.clear()
            ax.set_xlim(-25, 25)
            ax.set_ylim(-35, 35)
            # add patches
            state = self.states[0]
            self.car = patches.Rectangle((state[0]-self.params['car_w']/2,state[1]-self.params['car_l']/2),\
                                           self.params['car_w'],self.params['car_l'],\
                                          linewidth=.5,edgecolor='blue',facecolor='blue')
            self.car_direction = patches.Arrow(state[0],state[1],\
                                               self.params['car_w']/2,0,\
                                               linewidth=1.0, edgecolor='yellow')
            self.recs = []
            self.recs.append(self.car)
            self.recs.append(self.car_direction)
            for i in range(len(self.obs)):
                x, y = self.obs[i]
                obs = patches.Rectangle((x-self.params['obs_w']/2,y-params['obs_h']/2),\
                                           self.params['obs_w'],self.params['obs_h'],\
                                          linewidth=.5,edgecolor='black',facecolor='black')
                self.recs.append(obs)
                ax.add_patch(obs)
            # transform pole according to state
            t = mpl.transforms.Affine2D().rotate_deg_around(state[0], state[1], \
                                                            -state[2]/np.pi * 180) + ax.transData
            self.car.set_transform(t)
            ax.add_patch(self.car)
            self.car_direction.set_transform(t)
            self.car_direction_patch = ax.add_patch(self.car_direction)

            # add goal patch
            state = self.states[-1]
            self.car_goal = patches.Rectangle((state[0]-self.params['car_w']/2,state[1]-self.params['car_l']/2),\
                                           self.params['car_w'],self.params['car_l'],\
                                          linewidth=.5,edgecolor='red',facecolor='red')
            self.recs.append(self.car_goal)
            # transform pole according to state
            t = mpl.transforms.Affine2D().rotate_deg_around(state[0], state[1], \
                                                            -state[2]/np.pi * 180) + ax.transData
            self.car_goal.set_transform(t)
            ax.add_patch(self.car_goal)

            # state
            state = self.states[0]
            ax = self.ax2
            ax.clear()
            ax.set_xlim3d(-25, 25)
            ax.set_ylim3d(-35,35)
            ax.set_zlim3d(-np.pi,np.pi)

            dx = 1
            dtheta = 0.1
            feasible_points = []
            infeasible_points = []
            imin = 0
            imax = int(2*25./dx)
            jmin = 0
            jmax = int(2*35./dx)
            zmin = 0
            zmax = int(2*np.pi/dtheta)

            """
            for i in range(imin, imax):
                for j in range(jmin, jmax):
                    for z in range(zmin, zmax):
                        x = np.array([dx*i-25, dx*j-35, 0., 0., dtheta*z-np.pi, 0., 0., 0.])
                        if IsInCollision(x, self.cc_obs):
                            infeasible_points.append(x)
                        else:
                            feasible_points.append(x)

            feasible_points = np.array(feasible_points)
            infeasible_points = np.array(infeasible_points)
            #ax.scatter(feasible_points[:,0], feasible_points[:,2], c='yellow')
            ax.scatter(infeasible_points[:,0], infeasible_points[:,1], infeasible_points[:,4], c='black')
            """

            scat_state = ax.scatter([state[0]], [state[1]], [state[2]], c='blue')
            self.recs.append(scat_state)

            state = self.states[-1]

            ax.scatter([state[0]], [state[1]], [state[2]], c='red', marker='*')

            # draw the goal region
            #ax = self.ax1
            # randomly sample several points

            return self.recs
        def _animate(self, i):
            ax = self.ax1
            ax.set_xlim(-25, 25)
            ax.set_ylim(-35, 35)
            state = self.states[i]
            self.recs[0].set_xy((state[0]-self.params['car_w']/2,state[1]-self.params['car_l']/2))
            t = mpl.transforms.Affine2D().rotate_deg_around(state[0], state[1], \
                                                            -state[2]/np.pi * 180) + ax.transData
            self.recs[0].set_transform(t)

            # modify car_direction
            self.car_direction_patch.remove()
            self.car_direction = patches.Arrow(state[0],state[1],\
                                               self.params['car_w']/2,0,\
                                          linewidth=1.0, edgecolor='yellow')
            self.recs[1] = self.car_direction
            t = mpl.transforms.Affine2D().rotate_deg_around(state[0], state[1], \
                                                            -state[2]/np.pi * 180) + ax.transData
            self.recs[1].set_transform(t)
            self.car_direction_patch = ax.add_patch(self.car_direction)

            # print location of cart
            ax = self.ax2
            ax.set_xlim3d(-25, 25)
            ax.set_ylim3d(-35,35)
            ax.set_zlim3d(-np.pi,np.pi)

            self.recs[-1]._offsets3d = ([state[0]], [state[1]], [state[2]])

            return self.recs

        def animate(self, states, actions, costs, obstacles):
            '''
            given a list of states, actions and obstacles, animate the robot
            '''

            new_obs_i = []
            obs_width = width
            for k in range(len(obstacles)):
                obs_pt = []
                obs_pt.append(obstacles[k][0]-obs_width/2)
                obs_pt.append(obstacles[k][1]-obs_width/2)
                obs_pt.append(obstacles[k][0]+obs_width/2)
                obs_pt.append(obstacles[k][1]-obs_width/2)
                obs_pt.append(obstacles[k][0]+obs_width/2)
                obs_pt.append(obstacles[k][1]+obs_width/2)
                obs_pt.append(obstacles[k][0]-obs_width/2)
                obs_pt.append(obstacles[k][1]+obs_width/2)
                new_obs_i.append(obs_pt)
            obs_i = new_obs_i
            self.cc_obs = obs_i

            # transform the waypoint states and actions into trajectory
            traj = []
            s = states[0]
            self.states = states
            self.obs = obstacles
            #self._init()
            #plt.show()

            for i in range(len(states)-1):
                print('state: %d, remaining: %d' % (i, len(states)-i))
                # connect from this state to next
                solution_u, solution_t = planner.steer_solution(states[i], states[i+1])
                #print('solution_u:')
                #print(solution_u)
                #print('solution_t:')
                #print(solution_t)

                #print('start:')
                #print(states[i])
                #print('s:')
                #print(s)
                s = states[i]

                for j in range(len(solution_u)):
                    action = solution_u[j]
                    num_steps = int(np.round(solution_t[j]/self.params['integration_step']))
                    for k in range(num_steps):
                        traj.append(np.array(s))
                        print("porpagating... j = %d, k = %d" % (j, k))
                        #print(s)
                        #print('st:')
                        #print(sT)
                        s = self.system(s, action, self.params['integration_step'])
                        if IsInCollision(s, obs_i):
                            print('collision state:')
                            print(s)
                            print('st:')
                            print(states[i+1])
                        assert not IsInCollision(s, obs_i)
                        s = enforce_bounds(s)

            traj = np.array(traj)
            print("animating...")
            # animate
            self.states = traj
            #print(traj)
            self.obs = obstacles
            print(len(self.states))
            self.total = len(self.states)
            ani = animation.FuncAnimation(self.fig, self._animate, range(0, len(self.states), 1),
                                          interval=self.dt, blit=True, init_func=self._init,
                                          repeat=True)
            return ani

    params = {}
    car_width = 2.0
    car_len = 1.0
    width = 8.0
    params['car_w'] = car_width
    params['car_l'] = car_len
    params['obs_w'] = width
    params['obs_h'] = width
    params['integration_step'] = 0.002

    #system = _sst_module.RallyCar()
    propagate_system = _sst_module.Car()
    cpp_propagator = _sst_module.SystemPropagator()
    dynamics = lambda x, u, t: cpp_propagator.propagate(propagate_system, x, u, t)

    vis = CarVisualizer(dynamics, params)
    states = path
    sgs[0] = wrap_angle(sgs[0], propagate_system)
    sgs[1] = wrap_angle(sgs[1], propagate_system)
    #print('states:')
    #print(states)
    anim = vis.animate(np.array(states), None, None, obs_list)
    #HTML(anim.to_html5_video())
    anim.save('car_obs_MPC_env%d_path%d.mp4' % (obs_idx, p_idx))


#程序文件名Pex7_5.py
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
import numpy as np
from numpy.linalg import norm
from scipy.interpolate import interp2d

z=np.loadtxt("Pdata7_5.txt")  #加载高程数据
x=np.arange(0,1500,100)
y=np.arange(1200,-100,-100)
f=interp2d(x, y, z, 'cubic')
xn=np.linspace(0,1400,141)
yn=np.linspace(0,1200,121)
zn=f(xn, yn)

m=len(xn); n=len(yn); s=0;
for i in np.arange(m-1):
    for j in np.arange(n-1):
        p1=np.array([xn[i],yn[j],zn[j,i]])
        p2=np.array([xn[i+1],yn[j],zn[j,i+1]])
        p3=np.array([xn[i+1],yn[j+1],zn[j+1,i+1]])
        p4=np.array([xn[i],yn[j+1],zn[j+1,i]])
        p12=norm(p1-p2); p23=norm(p3-p2); p13=norm(p3-p1);
        p14=norm(p4-p1); p34=norm(p4-p3);
        L1=(p12+p23+p13)/2;s1=np.sqrt(L1*(L1-p12)*(L1-p23)*(L1-p13));
        L2=(p13+p14+p34)/2; s2=np.sqrt(L2*(L2-p13)*(L2-p14)*(L2-p34));
        s=s+s1+s2;
print("区域的面积为：", s)

plt.rc('font',size=16); plt.rc('text',usetex=True)
plt.subplot(121); contr=plt.contour(xn,yn,zn); plt.clabel(contr)
plt.xlabel('$x$'); plt.ylabel('$y$',rotation=90)
ax=plt.subplot(122,projection='3d');
X,Y=np.meshgrid(xn,yn)
ax.plot_surface(X, Y, zn,cmap='viridis')
ax.set_xlabel('$x$'); ax.set_ylabel('$y$'); ax.set_zlabel('$z$')
plt.savefig('figure7_5.png',dpi=500); plt.show()


class Solution(object):
    def trap(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        max_left, max_right = [height[0]], [height[-1]]
        # from left
        for i in range(1, len(height)):
            max_left.append(max(max_left[i - 1], height[i]))
        for i in range(len(height) - 2, -1, -1):
            max_right.insert(0, max(max_right[0], height[i]))
        # print(max_left)
        # print(max_right)
        # print(height)

        ans = 0
        for i in range(len(height)):
            ans += min(max_left[i], max_right[i]) - height[i]
        return ans