import collections

import cv2
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

class ImageAnalyzer:
    """
    Esta clase efectúa un recorrido exhaustivo de todas las imágenes que
    conforman el conjunto de datos ISIC 2019, cargándolas una a una y
    examinando sus propiedades.
    """
    def __init__(self, configuration):
        # Establecemos la semilla aleatoria a utilizar.
        self.random_seed = 1000

        # Establecemos el directorio de imágenes a utilizar.
        self.dir_images = configuration["IMAGES_DIR"]

        # Lectura del fichero que describe la clase a la que pertenece cada
        # imagen.
        self.df_images = pd.read_csv(configuration["CLASSES_FILE"])

    def images_report(self):
        """
        Esta función explora y ofrece un informe de las imágenes que se
        encuentran por debajo de un directorio raíz especificado. El informe
        contiene los siguientes elementos:

        - Rango dinámico (valores máximo y mínimo encontrados en sus matrices de
        canales). Cada imagen se obtiene como un array de Numpy con lo que
        podemos utilizar las funciones min() y max() para obtener la respuesta.
        - Tamaño de la imagen: altura x anchura.
        - Número de canales.
        """

        # Recorremos el listado de imágenes obtenido y vamos obteniendo
        # distintas métricas de cada una de ellas.
        range_min = 9999
        range_max = -1
        dimensions = {}
        channels = []
        for i in self.df_images.index:
            # Lectura de la imagen
            image = cv2.imread(
                self.dir_images + '/' + self.df_images["image"][i] + '.jpg')

            # Actualización del valor mínimo encontrado para el rango dinámico
            # global.
            if image.min() < range_min:
                range_min = image.min()

            # Actualización del valor máximo encontrado para el rango dinámico
            # global.
            if image.max() > range_max:
                range_max = image.max()

            # Actualización de la lista de dimensiones de imagen encontradas
            size_pair = (image.shape[0], image.shape[1])
            if size_pair in dimensions:
                classes_dict = dimensions.get(size_pair)
                class_key = self.df_images.columns[
                    np.argmax(self.df_images.loc[i][2:]) + 2]
                if class_key in classes_dict:
                    classes_dict.update(
                        {class_key: classes_dict.get(class_key) + 1})
                else:
                    classes_dict[class_key] = 1
                dimensions.update({size_pair: classes_dict})
            else:
                dimensions[size_pair] = {
                    self.df_images.columns[
                        np.argmax(self.df_images.loc[i][2:]) + 2]: 1
                }

            # Actualización del número de canales encontrado
            channels.append(image.shape[2])

        # Agrupamos y contamos las imágenes que tenemos por número de canales.
        count_channels = collections.Counter(channels)

        # Mostramos el rango dinámico encontrado para el dataset completo.
        print("Rango dinámico: {}-{}".format(range_min, range_max))

        # Mostramos cuántas imágenes tenemos por número de canales.
        print("Número de canales:")
        for j in count_channels.keys():
            print("  - {} -> {} imágenes ({})".format(
                str(j).rjust(7),
                count_channels.get(j),
                str("{:.2f}%".format(
                    count_channels.get(j) / len(self.df_images) * 100))
            ).rjust(6))

        # Mostramos cuántas imágenes tenemos por cada tamaño encontrado.
        print("Dimensiones:")
        for j in dimensions.keys():
            total_images = sum(dimensions.get(j).values())
            for k in dimensions.get(j):
                dimensions.get(j)[k] = "{:.2f}%".format(
                    dimensions.get(j)[k] * 100 / total_images)

            print("  - {}x{} -> {} imágenes ({}) (clases: {})".format(
                str(j[0]).rjust(4),
                str(j[1]).rjust(4),
                str(total_images).rjust(5),
                str("{:.2f}%".format(
                    total_images / len(self.df_images) * 100)).rjust(7),
                dimensions.get(j)
            ))

        # Finalmente mostramos el total de imágenes del conjunto de datos.
        print("Total imágenes del conjunto de entrenamiento:",
              len(self.df_images),
              "(100.00%)")

    def show_sample_images(self, n_images: int) -> None:
        """
        Cargamos y mostramos las primeras imágenes de cada clase usando OpenCV

        :param n_images: número de imágenes de muestra a mostrar.
        """

        for the_class in self.df_images.loc[:, "MEL":"SCC"].columns:
            print("Muestra de la clase " + the_class + ":")
            df_sample_class = \
                self.df_images.loc[self.df_images[the_class] == 1.0].sample(
                    n=n_images, random_state=self.random_seed)

            sample_images = []
            for i in df_sample_class.index:
                sample_images.append(cv2.imread(self.dir_images + "/" +
                                                df_sample_class["image"][i] +
                                                '.jpg'))

            plt.figure(figsize=(16, 10))
            for i in range(1, n_images + 1):
                plt.subplot(1, 5, i)
                plt.grid(False)
                plt.imshow(sample_images[i - 1])
            plt.show()

    def classes_graph(self) -> None:
        """
        Esta función produce un gráfico donde se puede ver la representación de
        cada clase en el conjunto de datos.
        """

        # Mostrar un extracto del contenido del fichero leído
        print(self.df_images)

        # Mostramos un gráfico con la distribución de clases del conjunto de
        # datos,
        # para ello definimos un eje Y con el número de imágenes por clase.
        y = self.df_images.loc[:, "MEL":"UNK"].sum()

        # El eje X con cada una de las clases de imágenes.
        x = self.df_images.columns[1:]

        # Ordenar los datos para apreciar mejor los posibles desequilibrios.
        y, x = zip(*sorted(zip(y, x)))

        # Gráfico que muestra los dos ejes definidos.
        fig = plt.figure()
        ax = fig.add_axes([0, 0, 1, 1])
        ax.bar(x, y)
        ax.set_ylabel("Número de imágenes")
        ax.set_xlabel("Clase")
        patches = ax.patches
        labels = [f"{i}" for i in y]
        for rect, label in zip(patches, labels):
            height = rect.get_height()
            ax.text(rect.get_x() + rect.get_width() / 2, height, label,
                    ha="center",
                    va="bottom")
        plt.show()


"""
Small modules for the structure of PointNet.
Author: Carlos Leo
Time:   2020/10/15 - 15:12
Version: 1.0
"""
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

class TNet(nn.Module):
    """
    T-Net which produce a transformation matrix to align input or feature space.

    k: the channel of the input
    """
    def __init__(self, k=3):
        super().__init__()
        self.k = k

        # shared mlp(64, 128, 1024)
        self.conv1 = nn.Conv1d(k, 64, 1)
        self.bn1 = nn.BatchNorm1d(64)
        self.conv2 = nn.Conv1d(64, 128, 1)
        self.bn2 = nn.BatchNorm1d(128)
        self.conv3 = nn.Conv1d(128, 1024, 1)
        self.bn3 = nn.BatchNorm1d(1024)

        # fully connected layer(512, 256, k*k)
        self.fc1 = nn.Linear(1024, 512)
        self.bn4 = nn.BatchNorm1d(512)
        self.fc2 = nn.Linear(512, 256)
        self.bn5 = nn.BatchNorm1d(256)
        self.fc3 = nn.Linear(256, k * k)

    def forward(self, x):
        x = F.relu(self.bn1(self.conv1(x)))
        x = F.relu(self.bn2(self.conv2(x)))
        x = F.relu(self.bn3(self.conv3(x)))

        # max symmetric function which change dims (B, C, N)
        # into dims (B, C)
        x = torch.max(x, dim=-1)[0]

        x = F.relu(self.bn4(self.fc1(x)))
        x = F.relu(self.bn5(self.fc2(x)))
        x = self.fc3(x)

        x = x.view(-1, self.k, self.k)

        eye = torch.eye(self.k)
        if x.is_cuda:
            eye = eye.cuda()
        eyes = eye.repeat(x.size()[0], 1, 1)
        x += eyes

        return x

def feature_transform_regularization(trans):
    """
    $L_{reg}=||I-AA^{T}||^2_F$, references the original implement

    :param trans: transformation matrix by T-Net in features
    :return:
    """
    d = trans.size()[1]
    I = torch.eye(d)[None, :, :]
    if trans.is_cuda:
        I = I.cuda()
    loss = torch.mean(torch.norm(torch.bmm(trans, trans.transpose(2, 1)) - I, dim=(1, 2)))
    return loss

def to_one_hots(y, categories):
    """
    Encode the labels into one-hot coding.

    :param y: labels for a batch data with size (B,)
    :param categories: total number of kinds for the label in the dataset
    :return: (B, categories)
    """
    y_ = torch.eye(categories)[y.data.cpu().numpy()]
    if y.is_cuda:
        y_ = y_.cuda()
    return y_

if __name__ == '__main__':

    def setup_seed(seed):
        """
        Set a random seed to generate same random numbers
        :param seed: random seed number
        :return:
        """
        torch.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)
        np.random.seed(seed)
        torch.backends.cudnn.deterministic = True

    setup_seed(20)

    # 随机生成数据测试T-Net
    x = torch.rand((10, 3, 1024))

    t_net = TNet()
    print(t_net(x).size())

    x2 = torch.rand((10, 64, 1024))
    t_net64 = TNet(k=64)
    print(t_net64(x2).size())


from sqlalchemy import create_engine, MetaData, select, and_, join, text
from sqlalchemy.orm import sessionmaker
from datetime import timedelta, datetime
from pytz import utc
from dotenv import load_dotenv, find_dotenv
from operator import itemgetter
from itertools import groupby
from sqlalchemy.dialects import postgresql
import os

# Load environment variables
load_dotenv(find_dotenv())

# Initialize connection and Session
database_url = os.getenv('POSTGRES_DB_URL')
engine = create_engine(database_url)
Session = sessionmaker(bind=engine)
session = Session()

meta = MetaData()
meta.reflect(bind=engine)

# Map tables
stays = meta.tables['stays']
hotels = meta.tables['hotels']

def fetch_stays(start_date, end_date, hotels_name_text = [], hotel_cities=[], hotel_countries=[], hotel_regions=[], award_categories=[], rate_filter=None):

    filter_conditions = [
        stays.c.check_in_date >= start_date,
        stays.c.check_out_date <= end_date,
        stays.c.duration == 1,
        stays.c.last_checked_time > datetime.now().astimezone(utc) - timedelta(hours=48),
        hotels.c.hotel_brand == 'hyatt'
    ]

    if [name for name in hotels_name_text if name]:
        filter_conditions.append(hotels.c.hotel_name.in_(hotels_name_text))
    if [name for name in hotel_cities if name]:
        filter_conditions.append(hotels.c.hotel_city.in_(hotel_cities))
    if [name for name in hotel_countries if name]:
        filter_conditions.append(hotels.c.hotel_country.in_(hotel_countries))
    if [name for name in hotel_regions if name]:
        filter_conditions.append(hotels.c.hotel_region.in_(hotel_regions))
    if [name for name in award_categories if name]:
        filter_conditions.append(hotels.c.award_category.in_(award_categories))
    if rate_filter == 'Standard':
        filter_conditions.append(stays.c.standard_rate > 0)
    if rate_filter == 'Premium':
        filter_conditions.append(stays.c.premium_rate > 0)

    j = join(stays, hotels, stays.c.hotel_id == hotels.c.hotel_id)

    stay_records = session.execute(
        select(*stays.c, *hotels.c).select_from(j).where(
            and_(*filter_conditions)
        ).group_by(*stays.c, *hotels.c)
        .order_by(hotels.c.hotel_name, stays.c.check_in_date)
    ).fetchall()

    return [record._mapping for record in stay_records]

def get_consecutive_stays(hotel_data, num_consecutive_days, rate_filter=None, max_points_budget=0):
    # Sort data by hotel code and check in date
    hotel_data.sort(key=lambda x: (x['hotel_code'], x['check_in_date']))

    # Group data by hotel code
    grouped_data = groupby(hotel_data, key=itemgetter('hotel_code'))

    result = []

    # Iterate over each group
    for hotel_code, group in grouped_data:
        stays = list(group)

        for i in range(len(stays) - num_consecutive_days + 1):
            result_stay = {}
            # Check if the dates are consecutive
            if all((stays[i + j + 1]['check_in_date'] - stays[i + j]['check_in_date'] == timedelta(days=1) for j in range(num_consecutive_days - 1))):

                standard_rate = 0
                premium_rate = 0

                if all(stay['premium_rate'] > 0 for stay in stays[i:i + num_consecutive_days]):
                    premium_rate = sum(stay['premium_rate'] for stay in stays[i:i + num_consecutive_days]) / num_consecutive_days
                if all(stay['standard_rate'] > 0 for stay in stays[i:i + num_consecutive_days]):
                    standard_rate = sum(stay['standard_rate'] for stay in stays[i:i + num_consecutive_days]) / num_consecutive_days

                if standard_rate == 0 and premium_rate == 0:
                    continue

                response_url, booking_url = build_url('Hyatt', hotel_code, stays[i]['check_in_date'].strftime('%Y-%m-%d'), (stays[i]['check_in_date'] + timedelta(days=num_consecutive_days)).strftime('%Y-%m-%d'), room_qty = 1, adults = 2, kids = 0)

                result_stay = {
                    'hotel_name': stays[i]['hotel_name'],
                    'hotel_code': stays[i]['hotel_code'],
                    'date_range_start': stays[i]['check_in_date'],
                    'date_range_end': stays[i]['check_in_date'] + timedelta(days=num_consecutive_days),
                    'last_checked': stays[i]["last_checked_time"],
                    'standard_rate': standard_rate,
                    'premium_rate': premium_rate,
                    'hotel_city': stays[i]["hotel_city"],
                    'hotel_province': stays[i]["hotel_province"],
                    'hotel_country': stays[i]["hotel_country"],
                    'hotel_region': stays[i]["hotel_region"],
                    'award_category': stays[i]["award_category"],
                    'booking_url': booking_url
                }

                if rate_filter == 'Standard':
                    if max_points_budget == 0 or (standard_rate <= max_points_budget and standard_rate != 0):
                        result.append(result_stay)
                elif rate_filter == 'Premium':
                    if max_points_budget == 0 or (premium_rate <= max_points_budget and premium_rate != 0):
                        result.append(result_stay)
                else:
                    if max_points_budget == 0 or (standard_rate <= max_points_budget and standard_rate != 0) or (premium_rate <= max_points_budget and premium_rate != 0):
                        result.append(result_stay)
    return result

def build_url(hotel_brand, hotel_code, checkin_date, checkout_date, room_qty = 1, adults = 2, kids = 0):
    base_url_dict = {
        'Hyatt': 'https://www.hyatt.com/shop/service/rooms/roomrates/'
    }
    search_base_url_dict = {
        'Hyatt': 'https://www.hyatt.com/shop/rooms/'
    }
    base_url = base_url_dict[hotel_brand] + hotel_code
    search_base_url = search_base_url_dict[hotel_brand] + hotel_code
    response_url = base_url + f'?spiritCode={hotel_code}&rooms={room_qty}&adults={adults}&checkinDate={checkin_date}&checkoutDate={checkout_date}&kids={kids}'
    search_url = search_base_url + f'?checkinDate={checkin_date}&checkoutDate={checkout_date}&rateFilter=woh'
    return response_url, search_url

def search_by_consecutive_nights(start_date, end_date, length_of_stay, hotel_name_text=[], hotel_city=[], hotel_country=[], hotel_region=[], award_category=[], rate_filter=None, max_points_budget=0):
    try:
        records = fetch_stays(start_date=start_date, end_date=end_date, hotels_name_text=hotel_name_text, hotel_cities=hotel_city, hotel_countries=hotel_country, hotel_regions=hotel_region, award_categories=award_category, rate_filter=rate_filter)
        # print(len(records))
        consecutive_stays = get_consecutive_stays(records, length_of_stay, rate_filter, max_points_budget)
        session.close()
        return consecutive_stays
    except Exception as e:
        session.close()
        return "Error in search_stays.py: %s", str(e)

# result = get_consecutive_stays_2(
#     start_date='2023-08-31',
#     end_date='2023-08-31',
#     hotel_cities=['New York', 'Paris'],
#     hotel_countries=['United States', 'France'],
#     award_categories=[],
#     length_of_stay='3',
#     rate_filter=[],
#     max_points_budget='0',
# )


# -*- coding: utf-8 -*-
"""
Created on Fri Jun  4 23:47:23 2021

@author: John
"""

import sys
import pandas as pd
from sqlalchemy import create_engine

def load_data(messages_filepath, categories_filepath):
    """Function to load data returns a dataframe"""

    messages = pd.read_csv(messages_filepath)
    categories = pd.read_csv(categories_filepath)

    # merge datasets
    df = pd.merge(messages, categories, on="id")

    return df

def clean_data(df):
    """Function to clean text data"""

    categories = df["categories"].str.split(";", expand=True)

    # select the first row of the categories dataframe
    row = categories.iloc[[1]]

    # remove last 2 characters of catergory names
    category_colnames = [category_name.split('-')[0] for category_name in row.values[0]]

    # rename the columns of `categories`
    categories.columns = category_colnames

    for column in categories:
        # set each value to be the last character of the string
        categories[column] = categories[column].astype(str).str[-1:]

        # convert column from string to numeric
        #categories[column] = pd.to_numeric(categories[column], downcast="integer")
        categories[column] = categories[column].astype(int)

    categories.drop(categories[categories['related'] > 1].index, inplace = True)
    # drop the original categories column from `df`
    df.drop("categories", axis=1, inplace=True)

    # concatenate the original dataframe with the new `categories` dataframe
    df = pd.concat([df, categories],join='inner',axis=1)

    # drop duplicates
    df=df.drop_duplicates(subset=['message'])

    return df

def save_data(df, database_filename):
    """save dataframe as sql database file"""
    engine = create_engine('sqlite:///' + database_filename)
    df.to_sql('DisasterResponse', engine, index=False,if_exists='replace')

def main():
    """Function to call all sub functions  to process and clean data."""

    if len(sys.argv) == 4:

        messages_filepath, categories_filepath, database_filepath = sys.argv[1:]

        print('Loading data...\n    MESSAGES: {}\n    CATEGORIES: {}'
              .format(messages_filepath, categories_filepath))
        df = load_data(messages_filepath, categories_filepath)

        print('Cleaning data...')
        df = clean_data(df)

        print('Saving data...\n    DATABASE: {}'.format(database_filepath))
        save_data(df, database_filepath)

        print('Cleaned data saved to database!')

    else:
        print('Please provide the filepaths of the messages and categories '\
              'datasets as the first and second argument respectively, as '\
              'well as the filepath of the database to save the cleaned data '\
              'to as the third argument. \n\nExample: python process_data.py '\
              'disaster_messages.csv disaster_categories.csv '\
              'DisasterResponse.db')

if __name__ == '__main__':
    main()


"zarr companion: benchmark Zarr companion file for band access"

import random

import zarr

from .utils import parse_args
from .benchmark_abc import BenchmarkABC, Verbosity

class ZarrCompanionBenchmark(BenchmarkABC, re_str=r'.*profile\.zarr'):
    "benchmark for profile converted to zarr"

    def __init__(self, path: str) -> None:
        self.file = path

    def task(self) -> None:
        z = zarr.open(self.file, mode='r')

        # Y,X,C Zarr dataset
        profile = z['0']

        y = random.randrange(profile.shape[0])
        x = random.randrange(profile.shape[1])

        band = profile[y, x, :]  # load it
        sum(band)  # do something with it

    def info(self, verbosity: Verbosity) -> str:
        info = f'Zarr Companion: {self.file}'

        if verbosity == Verbosity.VERBOSE:
            data = zarr.open(self.file, mode='r')['0']
            zarr_info = str(data.info)[:-1].split('\n')
            zarr_info = [item.split(':') for item in zarr_info]
            zarr_info = {l.strip(): r.strip() for (l, r) in zarr_info}

            info += (f'\n\tshape = {data.shape}, chunks = {data.chunks}, '
                     f'compressor = {data.compressor}, filters = {data.filters}')
            info += (f'\n\torder = {data.order}, size (mem) = {zarr_info["No. bytes"]}'
                     f', size (disk) {zarr_info["No. bytes stored"]}')

        return info

def _main():

    args = parse_args()

    for file in args.files:
        ZarrCompanionBenchmark(file).bench(
            Verbosity.VERBOSE, enable_gc=args.gc, number=args.number, repeat=args.repeat)

if __name__ == "__main__":
    _main()

"""
ZarrCompanionBenchmark (duration averaged on 1000 iterations, repeated 3 times.)
results: [2.404e-04, 2.301e-04, 2.302e-04]: 2.301e-04 s to 2.404e-04 s, 2.336e-04 s ± 5.912e-06 s
Zarr Companion: files/z-series/profile.zarr
        shape = (167, 439, 5), chunks = (84, 439, 5), compressor = None, filters = None
        order = C, size (mem) = 366565 (358.0K), size (disk) 369004 (360.4K)

ZarrCompanionBenchmark (duration averaged on 1000 iterations, repeated 3 times.)
results: [3.583e-04, 3.521e-04, 3.527e-04]: 3.521e-04 s to 3.583e-04 s, 3.544e-04 s ± 3.372e-06 s
Zarr Companion: files/test-channel-image/profile.zarr
        shape = (512, 512, 31), chunks = (128, 128, 16), compressor = None, filters = None
        order = C, size (mem) = 16252928 (15.5M), size (disk) 16777463 (16.0M)
"""


from typing import Any
import time
from functools import wraps

class StopwatchMaker(object):
    """
    Wrapper class that makes timing functions easier.
    If wrapped the function will be timed unless you pass in the keyword 'timed = False'.
    You can set the loading message with keyword 'loading_msg = ...' and end message with keyword 'end_msg = ...'.
    """

    def __init__(self, f):
        self._f = f

    def __call__(self, *args: Any, **kwds: Any) -> Any:
        timed: bool = True
        loading_msg: str = None
        end_msg: str = None

        if "timed" in kwds:
            timed = kwds.pop("timed")
        if "loading_msg" in kwds:
            loading_msg = kwds.pop("loading_msg")
        if "end_msg" in kwds:
            end_msg = kwds.pop("end_msg")

        if not timed:
            return self._f(*args, **kwds)

        if loading_msg:
            print(loading_msg)

        timer = time.time()
        ret = self._f(*args, **kwds)
        timer = time.time() - timer

        if end_msg:
            print(end_msg, end='')
        print(timer)
        return ret

class StopwatchMessageMaker:
    def __init__(self, loading_msg = None, end_msg = None) -> None:
        self._loading_msg = loading_msg
        self._end_msg = end_msg

    def __call__(self, f) -> any:
        lm = self._loading_msg
        em = self._end_msg

        class Stopwatch:
            def __init__(self, f) -> None:
                self._f = f

            def __call__(self, *args: Any, **kwds: Any) -> Any:

                if lm:
                    print(lm)

                timer = time.time()
                ret = self._f(*args, **kwds)  #check if you are acessing the right function
                timer = time.time() - timer

                if em:
                    print(em, end='')
                print(timer)
                return ret

        return Stopwatch(f)

# def stopwatch_factory(loading_msg: str = None, end_msg: str = None):
#     def timer(func, *params):
#         if loading_msg: print(loading_msg)

#         timer = time.time()
#         try:
#             ret = func(*params) if params and func else func() if func else None
#         except BaseException as e:
#             raise
#         timer = time.time() - timer

#         if end_msg: print(end_msg, end="")
#         print(f'{timer:^4}')
#         return ret

#     return timer

# @staticmethod
#     def timed(func: callable = lambda *args: None, *params,  loading_msg: str = None, end_msg: str = None) -> any:
#         """Prints out a loading_msg and calls the passed func with given params.
#         After the execution of given function prints out the end_msg followed by the time it took func to complete."""

#         if loading_msg: print(loading_msg)

#         timer = time.time()
#         try:
#             ret = func(*params) if params and func else func() if func else None
#         except BaseException as e:
#             raise
#         timer = time.time() - timer

#         if end_msg: print(end_msg, end="")
#         print(f'{timer:^4}')
#         return ret


import argparse
import time

import matplotlib.pyplot as plt
import torch
import torch.nn.functional as F
from matplotlib import rc

from gsoftmax.modules import Gspace2d

rc('font', **{'family': 'sans-serif'})
rc('text', usetex=True)
rc('text.latex', preamble=r'\usepackage{amsmath}')

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--device', type=str, help='device')

args = parser.parse_args()

def make_random_alpha(batch_size, h, w):
    alpha = torch.empty((batch_size, h, w)).uniform_(0, 1.)
    supp = torch.empty((batch_size, h, w)).uniform_(0, 1.) > 0.9
    alpha[~supp] = 0
    alpha /= alpha.view(batch_size, -1).sum(dim=1)[:, None, None]
    return alpha

def make_unidim_alpha(batch_size, h, w):
    alpha = torch.zeros((batch_size, h, w))
    alpha[:, h // 5, w // 5: (4 * w) // 5] = 1
    alpha[:, (4 * h) // 5, w // 5: (4 * w) // 5] = 1
    alpha[:, w // 5: (4 * w) // 5, w // 5] = 1
    alpha[:, w // 5: (4 * w) // 5, (4 * w) // 5] = 1
    alpha /= alpha.view(batch_size, -1).sum(dim=1)[:, None, None]
    return alpha

device = args.device
h, w = 32, 32
batch_size = 1
alpha = make_unidim_alpha(batch_size, h, w)
alpha += make_random_alpha(batch_size, h, w) / 5
alpha /= alpha.view(batch_size, -1).sum(dim=1)[:, None, None]
alpha = alpha[:, None]

alpha = alpha.to(device)

gspace = Gspace2d(h, w, sigma=3, max_iter=1000, tol=0,
                  method='lbfgs', logspace=True, verbose=20, )
metric_space = gspace.to(device)
alpha = alpha.to(device)

alpha = alpha.view(batch_size, -1)
v, f = metric_space.entropy_and_potential(alpha)
for i in range(1):
    t0 = time.perf_counter()
    v, proj = gspace.lse_and_softmax(f)
    elapsed = time.perf_counter() - t0
    print(f'Time: {elapsed:.2f}')

sm = F.softmax(f.view(batch_size, -1), dim=1).view(batch_size, 1, h, w)

alpha = alpha.view(batch_size, 1, h, w)
proj = proj.view(batch_size, 1, h, w)
f = f.view(batch_size, 1, h, w)

alpha = alpha.cpu().numpy()
proj = proj.cpu().numpy()
f = f.cpu().numpy()

fig, axes = plt.subplots(1, 4, figsize=(5, 1.2))
fig.subplots_adjust(left=0.0, right=1, top=1, bottom=0.2, wspace=0.02)
axes = axes.ravel()
axes[0].imshow(alpha[0, 0])

axes[0].annotate(r'Distribution $\alpha$', xy=(0.5, -0.1), ha='center',
                 va='top',
                 xycoords='axes fraction'
                 )
axes[1].imshow(-f[0, 0])
axes[1].annotate('Distance field \n' + r'$f = \nabla \Omega(\alpha)$',
                 va='top', xy=(0.5, -0.1), ha='center',
                 xycoords='axes fraction'
                 )
axes[2].imshow(proj[0, 0])
axes[2].annotate('Link function \n' + r'$\nabla \Omega^\star(f)$',
                 xy=(0.5, -0.1), ha='center',
                 va='top',
                 xycoords='axes fraction'
                 )
axes[3].imshow(sm[0, 0])
axes[3].annotate('Naive \n' + r'$\text{softmax}(f)$', xy=(0.5, -0.1),
                 ha='center',
                 va='top',
                 xycoords='axes fraction')
for ax in axes:
    ax.axis('off')
plt.savefig('link.pdf')


import logging

import numpy
try:
    from configparser import ConfigParser
except ImportError:
    from ConfigParser import ConfigParser

from .Rcvr68_92 import Rcvr68_92
from gbtcal.decode import getFitsForScan, getDcrDataDescriptors
from gbtcal.dcrtable import DcrTable

logger = logging.getLogger(__name__)

class Backend:

    def __init__(self, data, dcrHdu):
        self.dcrHdu = dcrHdu
        self.data = data
        self.descriptors = getDcrDataDescriptors(data)

        feedPolCombos = list(set([(f, p) for f, p, _, _ in self.descriptors]))
        self.channels = ["%s%s" % (f, p) for f, p in feedPolCombos]

        self.name = "DCR"

    def GetRawPower(self, feed, pol, phase):

        sigref, cal = phase

        mask = ((self.data['FEED'] == feed) &
                (numpy.char.rstrip(self.data['POLARIZE']) == pol) &
                (self.data['SIGREF'] == sigref) &
                (self.data['CAL'] == cal))

        return self.data[mask]['DATA'][0]

    def GetIntegrationTime(self):

        h = self.dcrHdu[0].header
        return h['DURATION']

    def GetIntegrationStartTimes(self):

        dataTab = self.dcrHdu[3].data
        return dataTab.field('TIMETAG')

    def GetPhases(self):
        phases = numpy.unique(self.data[['SIGREF', 'CAL']])
        return phases

    def GetNumPhases(self):

        return len(self.GetPhases())

class CalSeqScan:

    def __init__(self, projectPath, scanNum):
        # self.project = project
        # self.scan    = scan
        self.projectPath = projectPath
        self.scanNum = scanNum

        self.fitsMap = getFitsForScan(projectPath, scanNum)

        logger.debug("fits files %s", list(self.fitsMap.keys()))
        self.channels = []
        self.ports = []  # need ordered list to correspond with data columns
        self.SetBackend()
        self.SetReceiver()
        self.InitData()

    def SetBackend(self):

        dcrHdu = self.fitsMap['DCR']
        ifHdu = self.fitsMap['IF']
        data = DcrTable.read(dcrHdu, ifHdu)

        self.backend = Backend(data, dcrHdu)

    def getBackendName(self):
        return self.backend.name

    # def GetDataConnection(self):
        # return self.scan.getDataConnection()

    def GetReceiverCal(self):
        rcvrName = self.scan.getRcvrName()
        try:
            return self.project.getRcvrCalibration(rcvrName)
        except Exception:
            return None

    def SetReceiver(self):
        hdu = self.fitsMap['Rcvr68_92']
        self.receiver = Rcvr68_92(hdu, debug=True)

    def InitData(self):
        self.scanData = {}

    def getTwarm(self):
        """ Uses receiver class to get TWARM from receiver FITS file """
        if self.receiver:
            # TBF: for auto scan returns header value of twarm unless have dmjd arg
            return self.receiver.getTwarm()
        return 0

    def getTcold(self):
        """ Reads TCOLD from CalSeq.conf config file """
        ygorTelescope = "/home/sim"  # getConfigValue("/home/sim", "YGOR_TELESCOPE")
        filename = ygorTelescope + "/etc/config/CalSeq.conf"
        cp = ConfigParser()
        cp.read(filename)
        try:
            return float(cp.get("all_modes", "tcold"))
        except Exception as e:
            default = 50.0
            logger.warning("Caught exception %s", e)
            logger.warning("Returning default value %s", default)
            return default

    def getCalPos(self):
        return self.receiver.getCalpos()

    def isAuto(self):
        return self.receiver.isAuto()

    def getScanData(self):
        """ self.scanData is a dict of format {channel: (type, Raw Power data)}
            where:
            channel = 1X, 1Y, 2X, or 2Y
            type    = Vwarm, Vcold, Observing, Position2, or Position5
        """
        return self.scanData

    def getChannels(self):
        return self.channels

    def processCalseqScan(self):
        """ Read channel data and put in scanData dictionary
            with calibration wheel position metadata """

        if self.getBackendName() == "DCR":
            logger.debug("processCalseqScan for DCR")
            # Get DCR FITS DATA 'TIMETAG' column
            # (we only need this for auto mode,
            # but don't want to repeat this in loop)
            dmjds = self.backend.GetIntegrationStartTimes()

            for channel in self.backend.channels:
                for phase in self.backend.GetPhases():
                    logger.debug("channel, phase: %s, %s", channel, phase)
                    self.channels.append(channel)
                    feed = int(channel[0])
                    pol = channel[1]
                    # get the data for that channel
                    channelData = self.backend.GetRawPower(feed, pol, phase)

                    if self.isAuto():  # auto CalSeq
                        # This is the more complicated case;
                        # Have to parse channelData according to calpos
                        autoData = {}
                        tint = self.backend.GetIntegrationTime()
                        for idx, dmjd in enumerate(dmjds):
                            calPosition = self.receiver.getPosition(dmjd, tint)
                            if calPosition != "Unknown":
                                dataType = self.getDataType(calPosition, feed)
                                try:
                                    autoData[dataType].append(channelData[idx])
                                except KeyError:
                                    # start a list for this dataType
                                    autoData[dataType] = [channelData[idx]]
                        for datatype in list(autoData.keys()):
                            try:
                                self.scanData[channel].append(
                                    (datatype, numpy.array(autoData[datatype]))
                                )
                            except KeyError:
                                self.scanData[channel] = [
                                    (datatype, numpy.array(autoData[datatype]))
                                ]
                    else:  # manual CalSeq
                        # This is the simple case, all data is the same type
                        calPosition = self.getCalPos()
                        dataType = self.getDataType(calPosition, feed)
                        # Now put data in dict
                        self.scanData[channel] = (dataType, channelData)
        elif self.getBackendName() in ["Spectrometer", "Vegas"]:
            phase = 0

            for beam in self.backend.getBeams():
                for pol in self.backend.getPolarizations():
                    channel = str(beam) + pol[0]
                    self.channels.append(channel)
                    channelData = None
                    if self.isAuto():  # auto CalSeq
                        autoData = {}
                        for ifnum in self.backend.getIFNumbers():
                            # retrieve the needed Spectrometer class
                            bankIdx = self.backend.getBankIndex(
                                beam, pol, ifnum
                            )
                            bank = self.backend.getBank(bankIdx)

                            # Data for stationary wheel positions during scan
                            tint = bank.GetIntegrationTime()
                            dmjds = bank.GetIntegrationStartTimes()
                            for idx, dmjd in enumerate(dmjds):
                                calPosition = self.receiver.getPosition(
                                    dmjd, tint
                                )
                                if calPosition != "Unknown":
                                    dataType = self.getDataType(
                                        calPosition, beam
                                    )
                                    newData = self.backend.getRawPowerByValues(
                                        beam, pol, ifnum, idx, phase
                                    )
                                    if numpy.isnan(newData).any():
                                        continue
                                    try:
                                        autoData[dataType] = numpy.concatenate(
                                            (autoData[dataType], newData)
                                        )
                                    except KeyError:
                                        autoData[dataType] = newData
                        for datatype in list(autoData.keys()):
                            try:
                                self.scanData[channel].append(
                                    (datatype, autoData[datatype])
                                )
                            except KeyError:
                                self.scanData[channel] = [
                                    (datatype, autoData[datatype])
                                ]
                    else:  # manual CalSeq
                        for ifnum in self.backend.getIFNumbers():
                            for i in range(self.backend.getNumIntegrations()):
                                newData = self.backend.getRawPowerByValues(
                                    beam, pol, ifnum, i, phase
                                )
                                try:
                                    channelData = numpy.concatenate(
                                        (channelData, newData)
                                    )
                                except ValueError:
                                    channelData = newData

                        calPosition = self.getCalPos()
                        dataType = self.getDataType(calPosition, beam)
                        self.scanData[channel] = (dataType, channelData)

    def getDataType(self, calPos, feed):
        """Converts cal position Cold1/2 to Vwarm or Vcold depending on feed"""
        if "Cold" in calPos:
            # Convert calPos to key 'Vwarm' or 'Vcold'
            beamLoad = {"Cold1": ("Vcold", "Vwarm"),
                        "Cold2": ("Vwarm", "Vcold")}
            dataType = beamLoad[calPos][feed - 1]
        else:
            dataType = calPos
        return dataType

if __name__ == "__main__":
    projPath = "/home/gbtdata/AGBT16B_288_03"
    scanNum = 1
    scan = CalSeqScan(projPath, scanNum)
    logger.debug("scan: %s", scan)
    scan.processCalseqScan()


import pygame
from pygame import mixer

import numpy as np

from visualization.transition import Transition
from visualization.button import Button
from visualization.game import deck

from gym_environments.blackjack_env import BlackjackEnv

from agents.player_agent import Player
from agents.dealer_agent import Dealer
from agents.rl_agent import QLearningAgent

#declare other classes
transition_player = Transition()
dealer_bust_img = transition_player.images['dealer_bust']
player_bust_img = transition_player.images['player_bust']
robot_bust_img = transition_player.images['robot_bust']
dealer_turn_img = transition_player.images['dealer_turn']
dealer_wins_img = transition_player.images['dealer_wins']
player_wins_img = transition_player.images['player_wins']
robot_win_img = transition_player.images['robot_win']

env = BlackjackEnv()
observation = env.reset()

def main():
    initial_cards, player_sum, dealer_sum, ace = deck._deal_initial_cards()

    #declare agents
    dealer = Dealer(dealer_sum, initial_cards[2])
    player = Player(player_sum, [initial_cards[0], initial_cards[1]], ace)
    robot = QLearningAgent(env)

    pygame.init()
    mixer.init()
    robot.train(num_episodes=400000)
    screen = pygame.display.set_mode((640, 480), pygame.DOUBLEBUF | pygame.HWSURFACE | pygame.HWACCEL)

    screen_width = screen.get_width()
    screen_height = screen.get_height()
    pygame.display.set_caption('blackjack')
    pygame.display.set_icon(deck.cardBacks[1])

    #COLOR PALLATE
    DARK_BLUE = (43, 65, 98)
    BLUE_GREY = (56, 95, 113)
    OFF_WHITE = (245, 240, 246)
    LIGHT_BROWN = (215, 179, 119)
    DARK_BROWN = (143, 117, 79)

    running = True

    #BUTTONS
    def hit():
        player._move('hit')
        click.play()
    def stay():
        player._move('stay')
        click.play()
    def replay():
        global observation
        initial_cards, player_sum, dealer_sum, ace = deck._deal_initial_cards()

        player._reset(player_sum, [initial_cards[0], initial_cards[1]], ace)
        dealer._reset(dealer_sum, initial_cards[2])
        observation = env.reset()

        shuffle.play()
    buttons = []
    button_width = 100
    button_height = 40
    big_button_width = 400
    big_button_height = 100
    hit_button_x = ((screen_width - button_width) // 2) - button_width // 2 - 10
    hit_button_y = (screen_height - button_height) // 2
    stay_button_x = ((screen_width - button_width) // 2) + button_width // 2 + 10
    stay_button_y = (screen_height - button_height) // 2
    replay_button_x = screen_width // 2 - big_button_width // 2
    replay_button_y = screen_width // 2 - big_button_height - 10
    hit_button = Button(hit_button_x, hit_button_y, button_width, button_height, "hit", hit)
    stay_button = Button(stay_button_x, stay_button_y, button_width, button_height, "stay", stay)
    replay_button = Button(replay_button_x, replay_button_y, big_button_width, big_button_height, 'replay', replay)
    buttons.extend([hit_button, stay_button, replay_button])

    #TILE
    pattern_image = pygame.image.load("public/pattern.png")
    pattern_image = pygame.transform.scale(pattern_image, (pattern_image.get_width() / 6, pattern_image.get_height() / 6))
    pattern_width, pattern_height = pattern_image.get_size()

    #SOUND EFFECTS
    click = pygame.mixer.Sound('public/sounds/click_high.wav')
    shuffle = pygame.mixer.Sound('public/sounds/shuffle.mp3')
    pygame.mixer.music.load('public/sounds/ipanema.mp3')

    click.set_volume(1.8)
    shuffle.set_volume(1.8)
    pygame.mixer.music.set_volume(0.5)

    pygame.mixer.music.play(-1)

    #button visibility decider

    replay() #this is to cause the first game from playing itself
    while (running):
        dealer_cards = dealer.cards
        player_cards = player.cards
        robot_cards = env.cards

        # CARD VARIABLES
        padding = 10
        player_card_delay = 10 + (20 * (len(player_cards) - 1))
        dealer_card_delay = 10
        robot_card_delay = 10

        screen.fill(BLUE_GREY)
        for x in range(0, screen_width, pattern_width):
                for y in range(0, screen_height, pattern_height):
                    screen.blit(pattern_image, (x, y))

        #DEAL CARDS FOR DEALER & ROBOT?

        if (player.done):
            if (not env.round_done):
                global observation
                action = np.argmax(robot.q_table[observation])
                observation, _, _, _ = env.step(action, True)
        if (env.round_done):
            if (not dealer.stop):
                dealer._move('hit')

        # PYGAME EVENTS
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print(robot_cards)
                running = False
            for button in buttons:
                button.handle_event(event)

        # SHOW BUTTONS
        for button in buttons:
            if (not player.done):
                if (buttons.index(button) == 2):
                    button.set_visibility(False)
                else:
                    button.set_visibility(True)
                    button.draw(screen)
            else:
                if (buttons.index(button) != 2):
                    button.set_visibility(False)
                else:
                    if (dealer.stop):
                        button.draw(screen)
                        button.set_visibility(True)
                        screen.blit(player_bust_img, (0, 0))

        # SHOW DEALER CARDS
        for card in dealer_cards:
            screen.blit(card, (dealer_card_delay, 10))
            dealer_card_delay += 20
        if (len(dealer_cards) < 2):
            screen.blit(deck.cardBacks[0], (dealer_card_delay, 10))

        # SHOW PLAYER CARDS
        for card in player_cards:
            screen.blit(card, (screen_width - player_card_delay - card.get_width(), screen_height - padding - card.get_height()))
            player_card_delay -= 20

        #SHOW ROBOT CARDS
        for card in robot_cards:
            screen.blit(card, (robot_card_delay, screen_height - padding - card.get_height()))
            robot_card_delay += 20
        pygame.display.flip()

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
"""
Created on Fri Jul 21 00:48:27 2017

@author: DataScience
"""

# -*- coding: utf-8 -*-
"""
Created on Thu Jul 20 18:17:59 2017

@author: DataScience
"""

#生成上证50支股票，流动股东变化间隔的分布图

import cPickle as cp
import datetime as dt
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

shares = cp.load(open('shares.pkl'))
#res = pd.DataFrame()
res = []

for stock in list(shares.keys()):
    Holder_list = shares[stock]['holder_liqname']
    Time_list = shares[stock]['time']

    index = 0
    change_index = []

    for i in Holder_list:
        if index == 0:
            index += 1
            continue
        elif (i == Holder_list[index-1]) :
            index += 1
            continue
        else:
            change_index.append(index)
            index += 1

    changeT_list = []

    index = 0
    for j in Time_list:
        if index in change_index or index == 0:
            changeT_list.append(dt.datetime.strptime(j,"%Y-%m-%d").date())

        index += 1

    series = pd.Series(changeT_list)
    dif = series.diff()
    res_tmp = (dif[1:] / np.timedelta64(1, 'D')).astype(int)
   # res = pd.concat([res,res_tmp],axis = 1)
    res= res + list(res_tmp)
#==============================================================================
# res.columns = list(shares.keys())
# res.to_csv('SH50_liq_change.csv')
#==============================================================================
plt.hist(res,bins=40, histtype="stepfilled", color="#CD4F39")
plt.title('SH50_liq',fontsize=30 )
plt.xlabel('Gap',fontsize=20)
plt.ylabel('Num',fontsize=20)


# hello_world.py
from datetime import timedelta
from airflow import DAG
from airflow.utils.dates import days_ago
from airflow.operators.bash import BashOperator
from airflow.operators.python import PythonOperator
from airflow.operators.python_operator import PythonVirtualenvOperator

with DAG(
    dag_id="train_infer", # DAG의 식별자용 아이디입니다.
    description="train model and get inference", # DAG에 대해 설명합니다.
    start_date=days_ago(7), # DAG 정의 기준 2일 전부터 시작합니다.
    schedule_interval="0 0 * * *", # 매일 06:00에 실행합니다.
    tags=["model"],
) as dag:

    t0 = BashOperator(
    task_id="credential",
    bash_command='export GOOGLE_APPLICATION_CREDENTIALS="/opt/ml/wine/airflow/deep-theorem-391805-8dd50cc51ba5.json"',
    dag=dag,
    )

    t1 = BashOperator(
    task_id="preprocess",
    bash_command="python /opt/ml/wine/code/data/prepare_data.py",
    dag=dag,
    )

    t2 = BashOperator(
    task_id="train",
    bash_command="python /opt/ml/wine/Recbole/train.py --model_name DCN",
    dag=dag,
    )

    t3 = BashOperator(
    task_id="inference",
    bash_command="python /opt/ml/wine/Recbole/inference.py",
    dag=dag,
    )
    # 테스크 순서를 정합니다.
    # t1 실행 후 t2를 실행합니다.
    t0 >> t1 >> t2 >> t3


####################################################################################################
## A program producing pencil drawing from natural images (https://github.com/LarsPh/Pencil-Sketch).
## Copyright (c) 2019 Zhaorong Wang.
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, version 3.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
####################################################################################################

import numpy as np
import cv2
from scipy.ndimage.interpolation import rotate
from scipy.interpolate import UnivariateSpline
from scipy.signal import fftconvolve
from scipy.sparse import spdiags
from scipy.sparse.linalg import cg
from PIL import Image
import matplotlib.pyplot as plt
import sys
import os

#np.set_printoptions(threshold=sys.maxsize)
#np.set_printoptions(**opt)

#normalize the pixel valute to the range 0-255
def to255Int(A, c=255):
    return np.uint8((A - np.amin(A))/(np.amax(A)-np.amin(A))*c)

#edge detection
def gradEdges(S, method):
    #gradient edge detection, works poorly even after denoising
    #S = cv2.GaussianBlur(S, (7,7), 0)
    #S = cv2.bilateralFilter(S, 30, 30, 3)
    #Dx = np.column_stack((S[ : ,1: ], S[ : , -1]))-S
    #Dy = np.row_stack((S[1: , : ], S[-1, : ]))-S
    #G = np.sqrt(Dx**2+Dy**2)
    if(method == 1):
        #Laplacian edge detection, works better after bilateral filter
        #S = cv2.GaussianBlur(S, (7,7), 0)
        S = cv2.bilateralFilter(S, 30, 30, 3)
        G = cv2.Laplacian(S, cv2.CV_8U, ksize=5)
    else:
        #Canny edge detection, good performance on denoising, but more and thinner strokes
        #works better with ligher strokes, i.e. smaller value for darkerStroke
        (r, c) = np.shape(S)
        mean = np.sum(S)/(r*c)
        lower = mean*0.75
        upper = mean*1.25
        G = cv2.Canny(S, lower, upper)
    return G

#Draw strokes. Generate cross-like effect at junctions of strokes
def drawLines(G, nDirec, kernR, darkerStroke):
    (r, c) = np.shape(G)
    kernScale = int(min(r, c)/kernR)
    #generate line segments
    L = np.zeros((kernScale*2+1, kernScale*2+1, nDirec))
    L[ : , kernScale+1, 0] = 1
    for i in range(nDirec):
        L[ : , : ,i] = np.round(rotate(L[ : , : ,0], i/nDirec*180, reshape=False))
    #classification, fftconvolve save mem compared with convolve when N is large
    Gc = np.zeros((r, c, nDirec))
    for i in range(nDirec):
        Gc[ : , : ,i] = fftconvolve(G, L[ : , : ,i], mode="same")
    Index = np.argmax(Gc, axis=2)
    C = np.zeros((r, c, nDirec))
    for i in range(nDirec):
        C[ : , : ,i] = G*(Index == i)

    Sp = np.zeros((r, c))
    for i in range(nDirec):
        Sp += fftconvolve(C[ : , : ,i], L[ : , : ,i], mode="same")

    Sp = 1-(Sp - np.amin(Sp))/(np.amax(Sp)-np.amin(Sp))
    Sp = Sp ** darkerStroke
    return Sp

#Tone transformation
def transferTone(A, w1, w2, w3):
    A = to255Int(A)
    (r, c) = np.shape(A)
    p = np.zeros(256)
    inten = np.arange(0, 256)
    p1 = (1/9)*np.exp(-(255-inten)/9)
    p2 = np.zeros(256)
    p2[105:225] = 1/(225-105)
    p3 = (1/np.sqrt(2*np.pi*11))*np.exp(-(inten-90)**2/(11*11*2))
    p = w1*p1+w2*p2+w3*p3
    p /= np.sum(p)
    #plt.plot(inten, p, 'ro')
    #smoothen the curve
    fp = UnivariateSpline(inten, p)
    fp.set_smoothing_factor(0.000035)
    p = fp(inten)
    plt.plot(inten, p, 'go')
    p /= np.sum(p)
    cp = np.zeros(256)
    #calculate cunmulative density function
    for i in range(256):
        cp[i] = np.sum(p[0:i])
    cp /= cp[255]
    s = np.zeros(256)
    for i in range(r):
        for j in range(c):
            s[A[i, j]] += 1
    s /= r*c
    cs = np.zeros(256)
    for i in range(256):
        cs[i] = np.sum(s[0:i])
    #plt.plot(inten, s, 'o')

    Diff = np.zeros((256, 256))
    for i in range(256):
        for j in range(256):
            Diff[i, j] = np.abs(cs[i]-cp[j])
    #tonal mapping
    matching = np.zeros(256)
    for i in range(256):
        minIdx = 0
        minVal = Diff[i, 0]
        for j in range(256):
            if (Diff[i, j] < minVal):
                minVal = Diff[i, j]
                minIdx = j
        matching[i] = minIdx
    J = np.zeros((r,c))
    for i in range(r):
        for j in range(c):
            J[i, j] = matching[A[i, j]]

    t = np.zeros(256)
    for i in range(r):
        for j in range(c):
            t[np.uint8(J[i, j])] += 1
    t /= r*c
    plt.plot(inten, t, 'bo')
    #uncommnet to see the comparison on target(p) and mapped(t) histogram
    #plt.show()
    return to255Int(J)

#Transfer the texture so that it matches the tone image by sovling linear equation
def renderTexture(J, H1, lam):
    (r, c) = np.shape(J)
    (n, m) = np.shape(H1)
    #adjust the resolution of texture
    x = int(np.ceil(r/n))
    y = int(np.ceil(c/m))
    H2 = H1
    for i in range(y):
        H2 = np.hstack((H2, H1))
    H = H2
    for i in range(x):
        H = np.vstack((H, H2))
    H = H[ :r, :c]
    Image.fromarray(to255Int(H), "L").save("temp/texture_test1.jpg")

    epsilon = 0.0001
    H = np.float64(H)/255
    #prevent log(0)
    J = np.float64(J+epsilon)/255
    #transform to vector
    h = np.reshape(H, r*c)
    j = np.reshape(J, r*c)
    logh = np.log(h)
    logj = np.log(j)
    i = np.ones(r*c)
    #use methods for sparse matrix to prevent mem problems
    Dx = spdiags(np.array([-i,i]), np.array([0,1]), r*c, r*c)
    Dy = spdiags(np.array([-i,i]), np.array([0,c]), r*c, r*c)
    A = spdiags(logh*logh, 0, r*c, r*c)+lam*(Dx.transpose().dot(Dx)+Dy.transpose().dot(Dy))
    b = logh*logj
    #conjugate gradient
    beta, _ = cg(A, b)
    Beta = beta.reshape(r, c)
    T = H**Beta
    #elements of T have values between 0 to 1
    return T

#Combine the pencil strokes and tonal texture
def combineST(S, T):
    return to255Int(S) * T

#edgeDetctMthd: choose method for edge detection. 1 for bilateral filter + Laplacian, 2 for Canny
#kerndirN: value for line segments (kernal) with different directions
#kernScale: the scale for kernal would be min(w, h)/kerScale. w and h are the height and width of input image
#darkerStroke: larger value would make the stroke darker
#w1, w2, w3: weight of three distribution
#lam: lambda multiplied by the gradient of beta in formula (8) in final step (check the paper for details)
def pencilSketch(srcDir="source_images", txPath="textures/texture1.jpg", edgeDetctMthd=1, kerdireN=8, kernScale=200, darkerStroke=2, w1=0.52, w2=0.37, w3=0.11, lam=0.2):
    print("Generating images...")

    for fname in os.listdir(srcDir):
        im = Image.open(srcDir+"/"+fname)
        ImRGB = np.array(im)
        ImLUV = cv2.cvtColor(ImRGB, cv2.COLOR_RGB2Luv)
        #transform on L channal
        ImGs = ImLUV[ : , : ,0]
        if not os.path.exists("temp"):
            os.mkdir("temp")
        if not os.path.exists("output"):
            os.mkdir("output")
        Image.fromarray(ImGs, "L").save("temp/grayscale_"+fname)
        #print(np.shape(ImGs))

        G = gradEdges(ImGs, 1)
        Image.fromarray(to255Int(G), "L").save("temp/gradient_"+fname)
        Sp = drawLines(G, kerdireN, kernScale, darkerStroke)
        Image.fromarray(to255Int(Sp), "L").save("temp/lines_"+fname)
        J = transferTone(ImGs, w1, w2, w3)
        Image.fromarray(to255Int(J), "L").save("temp/tone_map_"+fname)
        texIm = Image.open(txPath).convert("L")
        H = np.array(texIm)
        T = renderTexture(J, H, lam)
        Image.fromarray(to255Int(T), "L").save("temp/pencil_texture_"+fname)
        R = combineST(Sp, T)
        Image.fromarray(to255Int(R), "L").save("output/pencil_sketch_"+fname)
        ImLUV[ : , : ,0] = to255Int(R)
        ImRGB = cv2.cvtColor(ImLUV, cv2.COLOR_Luv2RGB)
        Image.fromarray(ImRGB, "RGB").save("output/colored_pencil_sketch_"+fname)

    print("Finished generation")

pencilSketch()


#coding=utf-8
from django.conf.urls.defaults import *
#from view import current_datetime,hello,hours_ahead,template_datetime,dir_datetime,html_datetime,current_url_view_good,display_meta,model_meta,search_form
from book_sql import book_list
from linshi import Thanks,cs_disange,method_splitter,some_page_get,some_page_post,wcs_s
#from view import search,contact,setEmail,thanks

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
#admin.autodiscover()

urlpatterns = patterns('view',
                       ('^$','hello'),
                       ('^current_time/$','current_datetime'),
                       ('^current_time(\d{1,2})/$','hours_ahead'),
                       ('^template_datetime/$','template_datetime'),
                       ('^html_datetime/$','html_datetime'),
                       ('^dir_datetime/$','dir_datetime'),
                       ('^book_list/$',book_list),
                       (r'^admin/',include(admin.site.urls)),
                       ('^page/$','current_url_view_good'),
                       ('^meta/$','display_meta'),
                       ('^model_meta','model_meta'),
                       (r'^search-form/$','search_form'),
                       ('^search/$','search'),
                       ('^contact/$','contact'),
                       ('^setEmail/$','setEmail'),
                       ('^thanks/$','thanks'),
                       ('^linshi/(?P<thanks>\w+)/$',Thanks), #命名组传递参数
                       ('^(?P<thanks>xiao\w+)/$',cs_disange,{'name':'No Thanks.html'}), #使用第三个参数
                       ('^(?P<thanks>[^xiao]\w+)/$',cs_disange,{'name':'thanks.html'}), #使用第三个参数
                       ('^@@/$',cs_disange,{'name':'No Thanks.html','thanks':'suru cuowu'}),#伪造捕捉的URLconf值
                       ('^@somepage/$',method_splitter,{'GET':some_page_get,'POST':some_page_post}),
                       #('^wcs_s/$',wcs_s),
                       #('^current_time/',include('wcs_s'))

)


from django.contrib import admin
from django.urls import path
from api import views
from django.conf.urls.static import static
from django.conf import settings
from rest_framework_jwt.views import obtain_jwt_token
from knox import views as knox_views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.UserRegistration.as_view()),
    path('books/',views.ShopList.as_view()),
    path('books/<int:pk>/',views.ShopDetail.as_view()),
    path('addproduct/',views.ProductList.as_view()),
    path('viewproduct/<int:pk>/',views.ProductDetail.as_view()),
    path('viewproductdetails/<int:pk>/<int:productID>/',views.ProductDetailView.as_view()),
    path('cart/',views.AddToCart.as_view()),
    path('cartview/<int:pk>/',views.CartView.as_view()),
    path('cartviewss/<int:pk>/',views.updateQuantity.as_view()),
    path('register/', views.RegisterAPI.as_view(), name='register'),
    path('login', views.LoginAPI.as_view(), name='login'),
    path('logout', knox_views.LogoutView.as_view(), name='logout'),
    path('logoutall/', knox_views.LogoutAllView.as_view(), name='logoutall'),

]

if settings.DEBUG is True:
    urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)


from django.http import Http404
from django.shortcuts import render, redirect
from django.contrib import messages
from .forms import UserRegisterForm
from django.contrib.auth.decorators import login_required
from blog.models import Post
from .models import User
import datetime

# Create your views here.
def register(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            messages.success(request, f'Your account has been created. You can now login')
            return redirect('login')
    else:
        form = UserRegisterForm()
    return render(request, 'users/register.html', {'form': form})

@login_required
def profile(request, username):
    # current_user = request.user
    try:
        current_user_id = User.objects.get(username=username)
    except User.DoesNotExist:
        raise Http404("User Does Not Exist")
    profile_context = {
        "user_posts": Post.objects.filter(author=current_user_id),
        "queried_user": User.objects.get(username=username)
    }
    return render(request, 'users/profile.html', profile_context)

@login_required
def specific_blog(request, username, title):
    try:
        current_user_id = User.objects.get(username=username)
        post = Post.objects.get(title=title, author=current_user_id)
    except User.DoesNotExist:
        raise Http404("User Does Not Exist")
    context = {
            "user_post": post
        }
    return render(request, 'users/userblog.html', context)

@login_required
def addPost(request):
    if request.method == 'POST':
        if 'data' in request.POST:
            blog_title = request.POST['title']
            blog_data = request.POST['data']
            current_user = request.user
            # print(current_user)
            # print(type(current_user))
            # print(current_user.id)
            object = Post.objects.create(title=blog_title, content=blog_data, date_posted=datetime.datetime.now(), author=current_user)
            object.save()
            print("This Works and object saved!!!")
    context = {
        'message': 'Your Post has been Added !!! ',
        'posts': Post.objects.all(),
        'user': request.user
    }
    return render(request, 'blog/home.html', context)


from load_data import Data
import time
from collections import defaultdict
from model import *
from torch.optim.lr_scheduler import ExponentialLR
import argparse

class Experiment:
    def __init__(self, model_name, learning_rate=0.001, ent_vec_dim=200, rel_vec_dim=200,
                 num_iterations=100, batch_size=128, decay_rate=0., cuda = False, device=0,
                 input_dropout=0.3, hidden_dropout=0., hidden_dropout1=0.4, hidden_dropout2=0.5,
                 feature_map_dropout=0., in_channels=1, out_channels=32, filt_h=3, filt_w=3,
                 label_smoothing=0.):
        self.model_name = model_name
        self.learning_rate = learning_rate
        self.ent_vec_dim = ent_vec_dim
        self.rel_vec_dim = rel_vec_dim
        self.num_iterations = num_iterations
        self.batch_size = batch_size
        self.decay_rate = decay_rate
        self.label_smoothing = label_smoothing
        self.cuda = cuda
        self.device = device
        self.kwargs = {"input_dropout": input_dropout, "hidden_dropout": hidden_dropout,
                       "hidden_dropout1": hidden_dropout1, "hidden_dropout2": hidden_dropout2,
                       "feature_map_dropout": feature_map_dropout, "in_channels": in_channels,
                       "out_channels": out_channels, "filt_h": filt_h, "filt_w": filt_w}

    def get_data_idxs(self, data):
        # 把三元组中的实体和关系都转换为id
        data_idxs = [(self.entity_idxs[data[i][0]], self.relation_idxs[data[i][1]], self.entity_idxs[data[i][2]])
                     for i in range(len(data))]
        return data_idxs

    def get_er_vocab(self, data):
        # entity-relation词典，记录该头实体+关系对应的尾实体有哪些（可以用于filter-setting或者负采样）
        er_vocab = defaultdict(list)
        for triple in data:
            er_vocab[(triple[0], triple[1])].append(triple[2])
        return er_vocab

    def get_batch(self, er_vocab, er_vocab_pairs, idx):
        # 拿到1个batch_size个(头,关系)，对应拿到(尾)。由于reverse，就不存在head_batch和tail_batch的问题
        batch = er_vocab_pairs[idx:idx + self.batch_size]
        targets = np.zeros((len(batch), len(d.entities)))
        for idx, pair in enumerate(batch):
            targets[idx, er_vocab[pair]] = 1.  # 把该条(头,关系)的所有尾实体在target中标1
        targets = torch.FloatTensor(targets)
        if self.cuda:
            targets = targets.to(self.device)
        return np.array(batch), targets

    def evaluate(self, model, data):
        hits = []
        ranks = []
        for i in range(10):
            hits.append([])

        test_data_idxs = self.get_data_idxs(data)
        # print("待验证数据集三元组数(reverse): %d" % len(test_data_idxs))
        er_vocab = self.get_er_vocab(self.get_data_idxs(d.data))  # 数据集全部三元组的(头,关系)->尾 字典

        for i in range(0, len(test_data_idxs), self.batch_size):    # step = batch_size
            data_batch, _ = self.get_batch(er_vocab, test_data_idxs, i)  # data_batch是三元组
            e1_idx = torch.tensor(data_batch[:, 0])
            r_idx = torch.tensor(data_batch[:, 1])
            e2_idx = torch.tensor(data_batch[:, 2])
            if self.cuda:
                e1_idx = e1_idx.to(self.device)
                r_idx = r_idx.to(self.device)
                e2_idx = e2_idx.to(self.device)
            predictions = model.forward(e1_idx, r_idx)

            for j in range(data_batch.shape[0]):  # 针对data_batch的每一条三元组
                filt = er_vocab[(data_batch[j][0], data_batch[j][1])]
                target_value = predictions[j, e2_idx[j]].item()
                predictions[j, filt] = 0.0  # filter-setting，先把所有存在的尾置0，再将真尾设回预测值，避免其他存在尾的影响
                predictions[j, e2_idx[j]] = target_value

            sort_values, sort_idxs = torch.sort(predictions, dim=1, descending=True)  # sort_idxs是预测分值从高到低的实体id

            sort_idxs = sort_idxs.cpu().numpy()
            for j in range(data_batch.shape[0]):
                rank = np.where(sort_idxs[j] == e2_idx[j].item())[0][0]  # rank是真尾的sort序号
                ranks.append(rank + 1)  # 实际位次要+1

                for hits_level in range(10):
                    if rank <= hits_level:
                        hits[hits_level].append(1.0)
                    else:
                        hits[hits_level].append(0.0)

        print('MR:{:.6f}, MRR:{:.6f}'.format(np.mean(ranks), np.mean(1. / np.array(ranks))))
        print('Hits@1:{:.6f}, Hits@3:{:.6f}, Hit@10:{:.6f}'.format(np.mean(hits[0]), np.mean(hits[2]), np.mean(hits[9])))

    def train_and_eval(self):
        self.entity_idxs = {d.entities[i]: i for i in range(len(d.entities))}  # entity2id
        self.relation_idxs = {d.relations[i]: i for i in range(len(d.relations))}  # relation2id

        train_data_idxs = self.get_data_idxs(d.train_data)  # 把train_triples中的实体和关系都转换成id了，reverse过所以double了
        print("训练集三元组数(reverse): %d" % len(train_data_idxs))

        if model_name.lower() == "hype":
            model = HypE(d, self.ent_vec_dim, self.rel_vec_dim, self.device, **self.kwargs)
        elif model_name.lower() == "hyper":
            model = HypER(d, self.ent_vec_dim, self.rel_vec_dim, self.device, **self.kwargs)
        elif model_name.lower() == "distmult":
            model = DistMult(d, self.ent_vec_dim, self.rel_vec_dim, self.device, **self.kwargs)
        elif model_name.lower() == "conve":
            model = ConvE(d, self.ent_vec_dim, self.rel_vec_dim, self.device, **self.kwargs)
        elif model_name.lower() == "complex":
            model = ComplEx(d, self.ent_vec_dim, self.rel_vec_dim, self.device, **self.kwargs)
        elif model_name.lower() == "tucker":
            model = TuckER(d, self.ent_vec_dim, self.rel_vec_dim, self.device, **self.kwargs)

        if self.cuda:
            model.to(self.device)
        model.init()
        opt = torch.optim.Adam(model.parameters(), lr=self.learning_rate)
        # opt = torch.optim.Adagrad(model.parameters(), lr=self.learning_rate)

        if self.decay_rate:
            scheduler = ExponentialLR(opt, self.decay_rate)

        er_vocab = self.get_er_vocab(train_data_idxs)
        er_vocab_pairs = list(er_vocab.keys())  # 训练集里的(头,关系)

        print("开始训练...")
        for it in range(1, self.num_iterations + 1):
            start_train = time.time()
            model.train()
            losses = []
            np.random.shuffle(er_vocab_pairs)
            for j in range(0, len(er_vocab_pairs), self.batch_size):
                data_batch, targets = self.get_batch(er_vocab, er_vocab_pairs, j)  # data_batch是(头,关系), target把尾标1
                opt.zero_grad()
                e1_idx = torch.tensor(data_batch[:, 0])
                r_idx = torch.tensor(data_batch[:, 1])
                if self.cuda:
                    e1_idx = e1_idx.to(self.device)
                    r_idx = r_idx.to(self.device)
                predictions = model.forward(e1_idx, r_idx)  # 大小是[batch_size, num_entity]
                if self.label_smoothing:
                    targets = ((1.0 - self.label_smoothing) * targets) + (1.0 / targets.size(1))
                loss = model.loss(predictions, targets)
                loss.backward()
                opt.step()
                losses.append(loss.item())
            if self.decay_rate:
                scheduler.step()
            print("Epoch " + str(it) + ", time:" + str("%.2f" % (time.time() - start_train)) +
                  ", loss:" + str("%.8f" % np.mean(losses)))
            model.eval()
            with torch.no_grad():
                # print("验证集结果:", end='')
                # self.evaluate(model, d.valid_data)
                # print("训练集结果:", end='')
                # self.evaluate(model, d.train_data)
                if not it % 2:
                    print("测试集结果: ", end='')
                    start_test = time.time()
                    self.evaluate(model, d.test_data)
                    print("test time:" + str("%.2f" % (time.time() - start_test)))

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--model_name', type=str, default="ConvE", help='TuckER, HypER, HypE, ConvE, DistMult or ComplEx')
    # FB15k, FB15k-237, WN18 or WN18RR
    parser.add_argument("--dataset", type=str, default="WN18RR", help="数据集")
    parser.add_argument("--cuda", type=bool, default=True, help="use cuda (GPU)")
    parser.add_argument("--device", type=int, default=2)

    args = parser.parse_args()
    model_name = args.model_name
    print("模型: " + model_name)
    data_dir = "../data/%s/" % args.dataset     # 拼接形成数据集路径
    torch.backends.cudnn.deterministic = True
    seed = 20
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available:
        torch.cuda.manual_seed_all(seed)  # 消除模型训练的随机性，使结果可复现

    # 获取数据，reverse -> (t, r^-1, h)
    d = Data(data_dir=data_dir, reverse=True)     # valid和train也都读进来了，有txt文件就可以
    experiment = Experiment(model_name, num_iterations=200, batch_size=128, learning_rate=0.005,
                            decay_rate=1.0, ent_vec_dim=200, rel_vec_dim=200, cuda=args.cuda, device=args.device,
                            input_dropout=0.2, hidden_dropout=0.3, hidden_dropout1=0.45, hidden_dropout2=0.5,
                            feature_map_dropout=0.2, in_channels=1, out_channels=32, filt_h=1, filt_w=9,
                            label_smoothing=0.1)
    experiment.train_and_eval()


import requests
from bs4 import BeautifulSoup
from urllib.request import urlopen
from regex_search import searchExclude

def returnWeather(url="https://weather.theage.com.au/local-forecast/vic/melbourne"):
    '''
    Retrieves the weather from our specified url
    '''
    html = urlopen(url)
    soup = BeautifulSoup(html, 'html.parser')
    weather = soup.find('div', attrs={'class':'ff_current_temp'})
    weather = str(weather)
    weather = searchExclude('a-z=_"<> / \n °', weather) # strips all unnessacary characters so we can just get the numbers
    x=""
    for i in weather: # puts the string back together
        x+= i
    weather = x
    return weather


import socket
from select import *
import sys
from time import sleep
import bluetooth
import json
import SensorDeliverer as sd
import ActuatorManager as am

frontstr = "Client >> "

if __name__ == "__main__":
    file_path = 'config.json'
    with open(file_path, "r") as fj:
        fd = json.load(fj)
        HOST = fd['SERVER_IP']
        PORT_SENSOR = fd['SERVER_PORT_SENSOR']
        PORT_ACTUATOR = fd['SERVER_PORT_ACTUATOR']
        BT_ADDR = fd['BT_ADDR']
        BT_PORT = fd['BT_PORT']
        SYSTEM_ID = fd['SYSTEM_ID']

    print("System ID : ", SYSTEM_ID)
    print("System ID Type : ", type(SYSTEM_ID))
    print("Server Host Sensor : ", HOST, " | Port : ", PORT_SENSOR)
    print("Server Host Actuator : ", HOST, " | Port : ", PORT_ACTUATOR)
    print("Bluetooth Address : ", BT_ADDR, "Port : ", BT_PORT)

    loop = True

    while loop:
        # Bluetooth Socket
        try :
            print(frontstr, "Bluetooth connecting ... ", end="")
            bt_socket=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
            bt_socket.connect((BT_ADDR,BT_PORT))
            print("Success!!")

            sensor = sd.SensorDeliverer(bt_socket, HOST, PORT_SENSOR, SYSTEM_ID)
            actuator = am.ActuatorManager(bt_socket, HOST, PORT_ACTUATOR, SYSTEM_ID)

            sensor.start()
            actuator.start()

            sensor.join()
            actuator.join()
        except KeyboardInterrupt:
            print(frontstr, "ClientAgent is stopped.")
        except Exception as e :
            print(frontstr, "Error is occured : ", e)

        bt_socket.close()
        print(frontstr, "Bluetooth socket closed.")
        print(frontstr, "Sleep 5 sec for next connection.")
        sleep(5)


import numpy as np
import torch
from flask import Flask, jsonify, request

from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
from pydantic import BaseModel
from statistics import mode
import logging

app = Flask(__name__)

# class EntityIn(BaseModel):
#     texts : list

# class EntityOut(BaseModel):
#     label : str

@app.route("/",methods=['GET'])
def test():
    return jsonify({"Hello" : "World"})

def get_model():
    tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
    model = DistilBertForSequenceClassification.from_pretrained('prakhars/instagram_caption_classifier')
    return tokenizer, model

label_dict= {
14: 'Travel',
 0: 'Art and Culture',
 9: 'Music',
 4: 'Entertainment',
 10: 'Parenting',
 8: 'Health and Fitness',
 13: 'Sports',
 6: 'Food',
 2: 'Beauty, Fashion and Lifestyle',
 1: 'Automotive',
 11: 'Pets',
 5: 'Finance',
 12: 'Photography',
 3: 'DIY',
 7: 'Gaming'
 }

tokenizer, model = get_model()

@app.route('/predict/',methods=['POST'])

def read_root():

    try:
        texts = request.json['texts']
        encodings = tokenizer(texts, return_tensors="pt", padding=True, truncation=True)
        outputs = model(**encodings)
        predictions = outputs.logits.argmax(-1)
        out_codes = predictions.tolist()
        out_labels = {'labels':[]}
        most_common_label = {}
        for code in out_codes:
            out_labels['labels'].append(label_dict[code])
    except Exception as e:
        logging.error("Exception occured", exc_info=True)

    try :
        most_common_label['label'] = mode(out_labels['labels'])
    except:
        most_common_label['label'] = out_labels['labels'][0]

    return jsonify(most_common_label)

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


import os
import sys
import time
import serial
import logging

LOG = logging.getLogger(__name__)

# unless an explicit RADIORA_BRIDGE_TTY is specified to use, this searches by default across
# a set of common TTYs to find a RadioRA Classic RS232 module
DEFAULT_RADIORA_BRIDGE_TTY_TO_SEARCH = [
    '/dev/tty.usbserial-A501SGSU',
    '/dev/tty.usbserial-A501SGSV',
    '/dev/tty.usbserial-A501SGSW',
    '/dev/tty.usbserial-A501SGSX',
    '/dev/tty.usbserial-A501SGSY',
    '/dev/tty.usbserial-A501SGSZ',
    '/dev/tty.usbserial-A501SGT0',
    '/dev/tty.usbserial-A501SGT1',
    '/dev/tty.usbserial-A501SGT2',
    '/dev/tty.usbserial-A501SGT3',
    '/dev/ttyS0',         # Raspberry Pi mini UART GPIO
#    '/dev/ttyAMA0',       # Raspberry Pi GPIO pins 14/15 (pre-Bluetooth RPi 3)
    '/dev/serial0',       # RPi 3 serial port alias 1
    '/dev/serial1',       # RPi 3 serial port alias 2
    '/dev/tty.usbserial', # typical MacOS USB serial adapter
    '/dev/ttyUSB0',       # Linux USB serial 1
    '/dev/ttyUSB1'        # Linux USB serial 2
]

class RadioRASerial:

    def __init__(self):
        self.version = None
        self.tty_timeout = int(os.environ['RADIORA_BRIDGE_TTY_TIMEOUT']) if 'RADIORA_BRIDGE_TTY_TIMEOUT' in os.environ else 1
        ttys_to_search = DEFAULT_RADIORA_BRIDGE_TTY_TO_SEARCH

        # NOTE: RADIORA_BRIDGE_TTY environment variable overrides the default search paths
        if 'RADIORA_BRIDGE_TTY' in os.environ:
            tty_config = os.environ['RADIORA_BRIDGE_TTY']
            logging.info(">> RadioRA Classic device search paths overridden by env variable RADIORA_BRIDGE_TTY=" + tty_config)
            ttys_to_search = tty_config.split(',')

        self.__discover_radiora_serial__(ttys_to_search)

    def __discover_radiora_serial__(self, ttys_to_search):
        LOG.info(">> Discovering RadioRA device on serial interfaces: {}".format(', '.join(ttys_to_search)))
        for tty in ttys_to_search:
            try:
                if not os.path.exists(tty):
                    logging.info(">> Serial device {} does not exist, ignoring".format(tty))
                    continue

                LOG.warning('>> Testing tty {}'.format(tty))
                self.tty = tty
                self.serial = serial.Serial(tty,
                                            baudrate=9600, # 9600 baud is required by RA-RS232
                                            parity=serial.PARITY_NONE,
                                            stopbits=serial.STOPBITS_ONE,
                                            bytesize=serial.EIGHTBITS,
                                            dsrdtr=True, rtscts=True,
                                            timeout=self.tty_timeout)
                self.writeCommand('VERI')
                response = self.readData()

                # response = REV,M<Master Revision>,S<Slave Revision>, e.g. REV,M3.14,S1.01
                if ((response != None) and response.startswith('REV,')):
                    self.version = response.lstrip('REV,').rstrip('!')
                    LOG.warning('>> Found Lutron RadioRA Classic at {} (version={})'.format(self.tty, self.version))
                    break

                self.serial.close()
                self.serial = None
                self.tty = None

            except:
                LOG.error('Unexpected error: %s', sys.exc_info()[0])
                self.version = None

        if self.version == None:
            LOG.fatal("No RadioRA RS232 devices discovered at {}".format(', '.join(ttys_to_search)))
            raise RuntimeError("No RadioRA RS232 devices discovered at {}".format(', '.join(ttys_to_search)))

    def __repr__(self):
        return '<RadioRA Classic RS232: tty={}; version={}>'.format(self.tty, self.version)

    def _readline(self):
        eol = b'\r'
        line = bytearray()
        while True:
            c = self.serial.read(1)
            if c:
                if c == eol:
                    #line += '|'
                    break
                line += c
            else:
               break
        return bytes(line).decode('utf-8')

    def writeCommand(self, command):
        LOG.info('>> Serial write to {}: {}'.format(self.tty, command))
        self.serial.reset_input_buffer()
        self.serial.write((command + "\r\n").encode('utf-8'))

    # not the most efficient reading one byte at a time, but it is way faster than
    # waiting for a 1 or 2 second timeout on every read. This could be improved in future.
    def readData(self):
        start = time.time()
        result = self._readline()
        while self.serial.in_waiting:
            result = result + self._readline()
        end = time.time()

        LOG.info('>> Serial read ({1:.0f} ms): {0}'.format(result, 1000 * (end-start)))
        return result


#!/usr/bin/python3

import sys, json

def treatKey(obj, path, key):
    objKey = obj[key]
    objKeyType = type(objKey)
    value = objKey if (not objKeyType is dict and not objKeyType is list) else ''
    # used to be a print
    return (path + '/' + key, value)

def _finditem(obj, key, path = ''):
    objType = type(obj)
    results = []
    if objType is dict:
        keys = obj.keys()
        if key == '':
            for keyTmp in keys:
                results += [treatKey(obj, path, keyTmp)]
        elif key in keys:
            results += [treatKey(obj, path, key)]
        for keyTmp in keys:
            res = _finditem(obj[keyTmp], key, path + '/' + keyTmp)
            if res != []:
                results += res
    elif objType is list:
        objLen = len(obj)
        for objIndex in range(objLen):
            objEl = obj[objIndex]
            res = _finditem(objEl, key, path + '/' + str(objIndex))
            if res != []:
                results += res
    return results

filePath = sys.argv[1]
key = sys.argv[2] if len(sys.argv) >= 3 else ''
# `ytVariableName` could be for instance 'ytInitialPlayerResponse'
ytVariableName = sys.argv[3] if len(sys.argv) >= 4 else 'ytInitialData'

# if not found from key could search by value
# that way could find easily shortest path to get the value as sometimes the value is repeated multiple times

with open(filePath) as f:
    isJSON = f.read(1) == '{'

if not isJSON:
    with open(filePath) as f:
        content = f.read()
    newContent = '{' + content.split(ytVariableName + ' = {')[1].split('};')[0] + '}'
    with open(filePath, 'w') as f:
        f.write(newContent)

with open(filePath) as f:
    data = json.load(f)

with open(filePath, 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii = False, indent = 4)

pathValues = _finditem(data, key)
for path, value in pathValues:
    print(path, value)
    path = path[1:]
    path = path.replace('/', "']['")
    pathParts = path.split("][")
    pathPartsLen = len(pathParts)
    for pathPartsIndex in range(pathPartsLen):
        pathPart = pathParts[pathPartsIndex][1:-1]
        if pathPart.isdigit():
            pathParts[pathPartsIndex] = pathPart
    path = "][".join(pathParts)
    newPath = "['" + path + "']"
    print(newPath, value)


from datetime import datetime
import socket

address = ('localhost', 6789)
max_size = 1000 # Максимально число байтов, которое принимаем от клиента

print('Starting the server at', datetime.now())
print('Waiting for a client to call.')
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(address)
server.listen(5)  	# Максимальное кол-во клиентских соединений, будет выдавать отказ послежующим клиентам

client, addr = server.accept()
data = client.recv(max_size) # Установка значения Max кол-ва байтов

print('At', datetime.now(), client, 'said', data)
client.sendall(b'Are you talking to me?')
client.close()
server.close()


from PyQt5 import QtCore, QtGui, QtWidgets
import orderPage, askLogout
import sqlite3

class Ui_homePage(object):

    def askLogout(self):
        self.askLogoutDialog = QtWidgets.QDialog()
        self.askLogoutUi = askLogout.Ui_askLogoutDialog()
        self.askLogoutUi.setupUi(self.askLogoutDialog)
        self.askLogoutDialog.show()

    def getEmail(self,userid):
        _translate = QtCore.QCoreApplication.translate
        connDB = sqlite3.connect('washaDB/users.db')
        curDB = connDB.cursor()
        curDB.execute("SELECT * FROM users WHERE userID=:userID",
                    {'userID':userid})
        DBList = curDB.fetchall()
        DBTup = DBList[0]
        self.email = DBTup[1]
        self.profileButton.setText(_translate("homePage", self.email))

    def openOrders(self):
        self.orderWindow = QtWidgets.QMainWindow()
        self.orderUi = orderPage.Ui_orderWindow()
        self.orderUi.setupUi(self.orderWindow)
        self.orderWindow.show()
        self.orderUi.userID = self.userID
        self.orderUi.addressToTxtBox()

    def setupUi(self, homePage):
        self.userID = 0
        self.email = ''
        homePage.setObjectName("homePage")
        homePage.resize(362, 528)
        homePage.setMinimumSize(QtCore.QSize(362, 528))
        homePage.setMaximumSize(QtCore.QSize(362, 528))
        self.centralwidget = QtWidgets.QWidget(homePage)
        self.centralwidget.setObjectName("centralwidget")
        self.newOrderButton = QtWidgets.QPushButton(self.centralwidget, clicked= lambda: self.openOrders())
        self.newOrderButton.setGeometry(QtCore.QRect(50, 250, 271, 41))
        self.newOrderButton.setAutoDefault(False)
        self.newOrderButton.setDefault(False)
        self.newOrderButton.setFlat(False)
        self.newOrderButton.setObjectName("newOrderButton")
        self.showOrdersButton = QtWidgets.QPushButton(self.centralwidget)
        self.showOrdersButton.setGeometry(QtCore.QRect(50, 310, 271, 41))
        self.showOrdersButton.setObjectName("showOrdersButton")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(50, 130, 271, 71))
        font = QtGui.QFont()
        font.setFamily("Bahnschrift SemiBold")
        font.setPointSize(56)
        self.label.setFont(font)
        self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.profileButton = QtWidgets.QCommandLinkButton(self.centralwidget)
        self.profileButton.setGeometry(QtCore.QRect(10, 450, 228, 41))
        self.profileButton.setObjectName("profileButton")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget, clicked = lambda: self.askLogout())
        self.pushButton.setGeometry(QtCore.QRect(236, 450, 111, 41))
        self.pushButton.setObjectName("pushButton")
        homePage.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(homePage)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 362, 21))
        self.menubar.setObjectName("menubar")
        self.menuAbout = QtWidgets.QMenu(self.menubar)
        self.menuAbout.setObjectName("menuAbout")
        homePage.setMenuBar(self.menubar)
        self.actionAbout = QtWidgets.QAction(homePage)
        self.actionAbout.setObjectName("actionAbout")
        self.menuAbout.addAction(self.actionAbout)
        self.menubar.addAction(self.menuAbout.menuAction())

        self.retranslateUi(homePage)
        QtCore.QMetaObject.connectSlotsByName(homePage)

    def retranslateUi(self, homePage):
        _translate = QtCore.QCoreApplication.translate
        homePage.setWindowTitle(_translate("homePage", "Home Page"))
        homePage.setWindowIcon(QtGui.QIcon("icon.png"))
        self.newOrderButton.setText(_translate("homePage", "NEW ORDER"))
        self.showOrdersButton.setText(_translate("homePage", "SHOW ORDERS"))
        self.label.setText(_translate("homePage", "OLS"))
        self.profileButton.setText(_translate("homePage", "example@email.com"))
        self.pushButton.setText(_translate("homePage", "LOGOUT"))
        self.menuAbout.setTitle(_translate("homePage", "Info"))
        self.actionAbout.setText(_translate("homePage", "About"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    homePage = QtWidgets.QMainWindow()
    ui = Ui_homePage()
    ui.setupUi(homePage)
    homePage.show()
    sys.exit(app.exec_())


import numpy as np
import numpy.linalg as nplg
import scipy.sparse.linalg as spsplg
from cgs import conjugate_gradient_solve
from jacobi import jacobi_solve, jacobi_setup
from system_matrix import system_matrix as test_matrix

def test_solver(n=1000, N=3, method="lu", tol=1e-6):
    """Solves N nxn systems using {method} and compares with
    a direct solver (spsplg.solve())."""
    A = test_matrix(n)
    B = [np.random.rand(n) for _ in range(N)]

    if method == "lu":
        lu = spsplg.splu(A)
    elif method == "jacobi":
        Dinv, L_plus_U = jacobi_setup(A)

    for b in B:
        x_true = spsplg.spsolve(A, b)

        if method == "lu":
            x = lu.solve(b)
        elif method == "jacobi":
            x, n_iter = jacobi_solve(Dinv, L_plus_U, b, tol=tol)
        elif method == "cg":
            x, n_iter = conjugate_gradient_solve(A, b, tol=tol)

        if nplg.norm(x - x_true) / nplg.norm(x_true) > 10 * tol:
            print(f"Error! {method} yields an error larger than {10*tol:.2e}.")

    print(f"Method: {method}")
    if method in ["jacobi", "cg"]:
        print(f"Number of iterations: {n_iter}")
    print(f"x: {x.shape}")
    print()


import json
import requests
import folium
import pandas as pd
import numpy as np
from lxml import etree
from pandas.io.json import json_normalize
import matplotlib.cm as cm
import matplotlib.colors as colors
from geopy.geocoders import Nominatim

wikiData = requests.get('https://en.wikipedia.org/wiki/Boroughs_of_New_York_City')
html = etree.HTML(wikiData.text)
boroughTable = html.xpath('//table[@class="wikitable sortable"]/tbody/tr')

geolocator = Nominatim(user_agent="ny_explorer")

newyork_df_columns = ['Borough','Latitude','Longitude', 'Population', 'Density(Persons/Sq. Km)']
newyork_borough_df = pd.DataFrame(columns = newyork_df_columns)

for row in boroughTable[3:8]:
    rowElements = row.xpath('td')
    name = rowElements[0].xpath('div/b/a')[0].text.strip()

    population_str = rowElements[2].text.strip().split(',')
    population = int("".join(population_str))

    density_str = rowElements[8].text.strip().split(',')
    density = int("".join(density_str))

    location = geolocator.geocode(name)
    latitude = location.latitude
    longitude = location.longitude
    newyork_borough_df = newyork_borough_df.append({'Borough' : name,
                                                    'Latitude' : latitude,
                                                    'Longitude' : longitude,
                                                    'Population' : population,
                                                    'Density(Persons/Sq. Km)' : density}, ignore_index=True)

newyork_borough_df = newyork_borough_df.sort_values(by = 'Density(Persons/Sq. Km)',ascending = False)
newyork_borough_df = newyork_borough_df.reset_index(drop=True)

location = geolocator.geocode('New York City')
latitude = location.latitude
longitude = location.longitude
print('The geograpical coordinate of New York City are {}, {}.'.format(latitude, longitude))

# create map of New York using latitude and longitude values
map_newyork = folium.Map(location=[latitude, longitude], zoom_start=10)

# add markers to map
for lat, lng, borough in zip(newyork_borough_df['Latitude'], newyork_borough_df['Longitude'], newyork_borough_df['Borough']):
    label = '{}'.format(borough)
    label = folium.Popup(label, parse_html=True)
    folium.CircleMarker(
        [lat, lng],
        radius=5,
        popup=label,
        color='blue',
        fill=True,
        fill_color='#3186cc',
        fill_opacity=0.7,
        parse_html=False).add_to(map_newyork)

map_newyork.save('map.html')


# import pandas as pd
#
# from cfg import data_root
#
# save_file = "soybean_price.csv"
# save_file_path = data_root + save_file
#
# #初始化数据存储数组
# soybean_price_columns = ["product", "date", "market", "price", "unit"]
#
# # 给数据添加表头
# soybean_price_df = pd.read_csv(save_file_path, header=0)
# soybean_price_df.columns = soybean_price_columns
#
# soybean_price_df["date"] = pd.to_datetime(soybean_price_df["date"], format="%Y-%m-%d" )
#
# print(soybean_price_df.info())
# soybean_price_df.sort_values("date", inplace=True, ascending=True)
#
# # 重新将数据写入csv文件
# soybean_price_df.to_csv(save_file_path, index = False)

from selenium import webdriver

from cfg import chromedriver_path

driver = webdriver.Chrome(chromedriver_path)
driver.get("https://cn.investing.com/currencies/usd-cny-historical-data")
elem = driver.find_element_by_id("widgetFieldDateRange")
elem.click()

elem_start_date = driver.find_element_by_id("startDate")
elem_start_date.clear()
elem_start_date.send_keys("2014/01/01")

elem_end_date = driver.find_element_by_id("endDate")
elem_end_date.clear()
elem_end_date.send_keys("2018/07/18")

elem_in = driver.find_element_by_id("applyBtn")
elem_in.click()

#
2018/06/18 - 2018/07/18


from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from .models import Parties
from .serializers import PartiesSerializers
from rest_framework.parsers import JSONParser
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def parties_list(request):
    if request.method=='GET':
        parties = Parties.objects.all()
        serializer = PartiesSerializers(parties, many=True)
        return JsonResponse(serializer.data, safe=False)

    if request.method=='POST':
        parties = JSONParser().parse(request)
        serializer = PartiesSerializers(data=parties)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)

@csrf_exempt
def customers_list(request):
    customers = Parties.objects.filter(is_customer=True)
    serializer = PartiesSerializers(customers, many=True)
    return JsonResponse(serializer.data, safe=False)

@csrf_exempt
def vendors_list(request):
    vendors = Parties.objects.filter(is_vendor=True)
    serializer = PartiesSerializers(vendors, many=True)
    return JsonResponse(serializer.data, safe=False)


# pyright: reportMissingImports=false

import sys
from typing import Literal, TypeGuard
import fontforge
import psMat
import util
import properties as P
from datetime import datetime

if len(sys.argv) != 5:
    raise ValueError("Invalid argument")

def is_font_style(style: str) -> TypeGuard[Literal["Regular", "Bold", "Italic", "BoldItalic"]]:
    return style in ["Regular", "Bold", "Italic", "BoldItalic"]

FONT_EN_TTF = sys.argv[1]
FONT_JP_TTF = sys.argv[2]
if not is_font_style(sys.argv[3]):
    raise ValueError("Invalid font style")
else:
    FONT_STYLE = sys.argv[3]
BUILD_FILE = sys.argv[4]

def main() -> None:
    font = new_font()

    merge_en(font)
    merge_jp(font)

    if "Italic" in FONT_STYLE:
        make_italic(font)
        util.fix_all_glyph_points(font, round=True, addExtrema=True)
    else:
        util.fix_all_glyph_points(font, addExtrema=True)

    font.selection.all()
    font.autoHint()
    font.autoInstr()
    font.selection.none()

    util.font_into_file(font, BUILD_FILE)
    util.log("Generated:", BUILD_FILE)

def merge_en(font) -> None:
    en_font = fontforge.open(FONT_EN_TTF)
    en_font.encoding = P.ENCODING
    font.mergeFonts(en_font)
    en_font.close()
    util.log("Merged:", FONT_EN_TTF, "->", BUILD_FILE)

def merge_jp(font) -> None:
    jp_font = fontforge.open(FONT_JP_TTF)
    font.mergeFonts(jp_font)
    for glyph in jp_font.glyphs():
        unicode = glyph.unicode
        if unicode == -1:
            continue
        if glyph.altuni is not None:
            font[unicode].altuni = glyph.altuni
        font[unicode].unicode = unicode
    jp_font.close()
    util.log("Merged:", FONT_JP_TTF, "->", BUILD_FILE)

def make_italic(font) -> None:
    PI = 3.14159265358979323846
    rot_rad = -1 * P.ITALICANGLE * PI / 180
    transform_mat = psMat.skew(rot_rad)

    def selectMore(start, end=None):
        nonlocal font
        if end is None:
            font.selection.select(("more", "encoding"), start)
        else:
            font.selection.select(("more", "ranges", "encoding"), start, end)

    selectMore(0x21, 0x217f)
    selectMore(0x2460, 0x24ea)
    selectMore(0x2768, 0x277e)
    selectMore(0x27e6, 0x27eb)
    selectMore(0x2987, 0x2998)
    selectMore(0x2e18)
    selectMore(0x2e22, 0x2e2e)
    selectMore(0x2e8e, 0xffe5)
    selectMore(0x1f100)
    selectMore(0x20b9f, 0x2f920)
    # NOTE: After 0x110000, codepoint is defferent in Reguler and Bold.
    selectMore(".notdef", "uni301F.half")
    selectMore("acute.half", "zero.alt01")
    font.transform(transform_mat)
    font.selection.none()

def new_font():
    style_prop = P.STYLE_PROPERTY[FONT_STYLE]
    font = fontforge.font()
    font.ascent = P.ASCENT
    font.descent = P.DESCENT
    font.italicangle = P.ITALICANGLE if "Italic" in FONT_STYLE else 0
    font.upos = P.UNDERLINE_POS
    font.uwidth = P.UNDERLINE_HEIGHT
    font.familyname = P.FAMILY
    font.copyright = P.COPYRIGHT
    font.encoding = P.ENCODING
    font.fontname = P.FAMILY + "-" + FONT_STYLE
    font.fullname = P.FAMILY + " " + FONT_STYLE
    font.version = P.VERSION
    font.appendSFNTName(
        "English (US)",
        "SubFamily",
        "".join([" " + c if c.isupper() else c for c in FONT_STYLE]).lstrip()
    )
    font.appendSFNTName(
        "English (US)",
        "UniqueID",
        "; ".join(
            [
                f"FontForge {fontforge.version()}",
                P.FAMILY + " " + FONT_STYLE,
                P.VERSION,
                datetime.today().strftime("%F"),
            ]
        ),
    )

    font.gasp_version = 1
    font.gasp = (
        (65535,
         ('gridfit', 'antialias', 'symmetric-smoothing', 'gridfit+smoothing')),
    )

    font.weight = style_prop["weight"]
    font.os2_weight = style_prop["os2_weight"]
    font.os2_width = 5  # Medium (100%)
    font.os2_stylemap = style_prop["os2_stylemap"]
    font.os2_vendor = "2357"  # Me
    font.os2_panose = (  # https://monotype.github.io/panose/pan1.htm
        2,                            # Family Kind = 2-Latin: Text and Display
        11,                           # Serif Style = Nomal Sans
        style_prop["panose_weight"],  # Weight
        9,                            # Proportion = 9-Monospaced
        3,                            # Contrast = 3-Very Low
        2,                            # Stroke Variation = 2-No Variation
        2,                            # Arm Style = 2-Straight Arms/Horizontal
        style_prop['panose_letterform'],  # Letterform
        2,                            # Midline = 2-Standard/Trimmed
        4,                            # X-height = 4-Constant/Large
    )

    # typoascent, typodescent is generic version for above.
    # the `_add` version is for setting offsets.
    font.os2_typoascent = P.ASCENT
    font.os2_typodescent = -P.DESCENT
    font.os2_typoascent_add = 0
    font.os2_typodescent_add = 0

    # winascentwindescent is typoascent/typodescent for Windows.
    font.os2_winascent = P.ASCENT
    font.os2_windescent = P.DESCENT
    font.os2_winascent_add = 0
    font.os2_windescent_add = 0

    # winascentwindescent is typoascent/typodescent for macOS.
    font.hhea_ascent = P.ASCENT
    font.hhea_descent = -P.DESCENT
    font.hhea_ascent_add = 0
    font.hhea_descent_add = 0

    # linegap is for gap between lines.  The `hhea_` version is for macOS.
    font.os2_typolinegap = 0
    font.hhea_linegap = 0

    return font

if __name__ == "__main__":
    main()