from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.http import JsonResponse
from django.views.generic import ListView

from core.models import Category, Product
from .models import Favorite

@login_required(login_url='login')
def save_favorite(request):
    """
    This function is used to register a product in the Favorite table.
    The user must be logged in to register a product.
    This function interacts with an AJAX script.
    """
    product_code = request.POST['product_code']
    product_selected = Product.objects.get(code=product_code)

    name_substitute_for = request.POST['product_sub_for']
    code_substitute_for = [p.code for p in Product.objects.filter(name__iexact=name_substitute_for)][0]

    user = request.user

    Favorite(name=product_selected.name,
             code=product_selected.code,
             nutrition_grade=product_selected.nutrition_grade,
             code_substitute_for=code_substitute_for,
             name_substitute_for=name_substitute_for,
             url=product_selected.url,
             category=product_selected.category,
             user=user).save()

    response_data = {
        'status': 'ok'
    }
    return JsonResponse(response_data)

@method_decorator(login_required, name='dispatch')
class FavoriteList(ListView):
    """
    This view retrieves the user's registered products
    and displays them using the template favorite-list.html.
    The user must be logged in to access his registered products.
    """
    model = Favorite
    context_object_name = "favorite_list"
    template_name = "favorite-list.html"

    def get_queryset(self):
        user = self.request.user
        return Favorite.objects.filter(nutrition_grade="good").order_by('nutrition_grade')


import argparse
import logging
import pickle
import sys
from io import StringIO
from os import getenv
from os.path import abspath, join

import pandas as pd  # type: ignore
import xgboost as xgb  # type: ignore

from preprocess import preprocess

random_state = 42
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

target_columns = [
    "incident",
]

# Work around for a SageMaker path issue
# (see https://github.com/aws/sagemaker-python-sdk/issues/648)
# WARNING - removing this may cause the submission process to fail
if abspath("/opt/ml/code") not in sys.path:
    sys.path.append(abspath("/opt/ml/code"))

def train(args):
    """Train

    Your model code goes here.
    """
    logger.info("calling training function")

    # If you require any particular preprocessing to create features then this
    # *must* be contained in the preprocessing function for the Unearthed pipeline
    # apply it to the private data
    df = preprocess(join(args.data_dir, "public.csv.gz"))

    # the target variable is incident
    y_train = df[target_columns]
    logger.info(f"training target shape is {y_train.shape}")
    X_train = df.drop(columns=target_columns)
    logger.info(f"training input shape is {X_train.shape}")

    model = xgb.XGBClassifier()
    print(model.fit(X_train, y_train))

    # save the model to disk
    save_model(model, args.model_dir)

def save_model(model, model_dir):
    """Save model to a binary file.

    This function must write the model to disk in a format that can
    be loaded from the model_fn.

    WARNING - modifying this function may cause the submission process to fail.
    """
    logger.info(f"saving model to {model_dir}")
    with open(join(model_dir, "model.pkl"), "wb") as model_file:
        pickle.dump(model, model_file)

def model_fn(model_dir):
    """Load model from binary file.

    This function loads the model from disk. It is called by SageMaker.

    WARNING - modifying this function may case the submission process to fail.
    """
    logger.info("loading model")
    with open(join(model_dir, "model.pkl"), "rb") as file:
        return pickle.load(file)

def input_fn(input_data, content_type):
    """Take request data and de-serialize the data into an object for prediction.

    In the Unearthed submission pipeline the data is passed as "text/csv". This
    function reads the CSV into a Pandas dataframe ready to be passed to the model.

    WARNING - modifying this function may cause the submission process to fail.
    """
    logger.info("receiving preprocessed input")

    # this call must result in a dataframe or nparray that matches your model
    input = pd.read_csv(StringIO(input_data), index_col=0, parse_dates=True)
    logger.info(f"preprocessed input has shape {input.shape}")
    return input

if __name__ == "__main__":
    """Training Main

    The main function is called by both Unearthed's SageMaker pipeline and the
    Unearthed CLI's "unearthed train" command.

    WARNING - modifying this function may cause the submission process to fail.

    The main function must call preprocess, arrange th
    """
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--model_dir", type=str, default=getenv("SM_MODEL_DIR", "/opt/ml/models")
    )
    parser.add_argument(
        "--data_dir",
        type=str,
        default=getenv("SM_CHANNEL_TRAINING", "/opt/ml/input/data/training"),
    )
    train(parser.parse_args())


import numpy as np

class NativeBayes:
    def N(self, x, mu, sigma):
        p = 1 / sigma / np.sqrt(2 * np.pi) * np.exp(-(x - mu) ** 2 / 2 / sigma ** 2)
        return p

    def fit(self, X, y):
        X1 = X[y == 1]  # 找到所有正样本
        X2 = X[y == 0]  # 找到所有负样本

        # 分别计算负样本的均值和方差:
        self.mu_positive = np.mean(X1, axis=0)
        self.std_positive = np.std(X1, axis=0)
        # 分别计算正样本的均值和方差:
        self.mu_negative = np.mean(X2, axis=0)
        self.std_negative = np.std(X2, axis=0)
        # 计算p(y==1)
        self.py_positive = len(y[y == 1]) / len(y)
        self.py_negative = len(y[y == 0]) / len(y)

    def predict_prob(self, X):
        prb = []

        for a in X:
            # 假设有k个特征:
            p_xk_on_positive = []
            p_xk_on_negative = []
            p_xk = []
            for k, x in enumerate(a):
                # 样本a的第k个特征,值为x
                """
                P(x1, x2) = P(x1)*P(x2)
                P(x1) = P(x1|y=0)P(y=0) + P(x1|y=1)P(y=1) 全概率公式
                P(x2) = P(x2|y=0)P(y=0) + P(x2|y=1)P(y=1) 全概率公式
                """
                p_xk_on_positive.append(self.N(x, self.mu_positive[k], self.std_positive[k]))
                p_xk_on_negative.append(self.N(x, self.mu_negative[k], self.std_negative[k]))

                """
                P(y|x1,x2) = P(x1|y)*P(x2|y)*P(y) / P(x1,x2)
                """
                p_xk.append(p_xk_on_negative[k] * self.py_negative + p_xk_on_positive[k] * self.py_positive)
            p_positive_on_x = np.multiply.reduce(p_xk_on_positive) * self.py_positive / np.multiply.reduce(p_xk)
            p_negative_on_x = np.multiply.reduce(p_xk_on_negative) * self.py_negative / np.multiply.reduce(p_xk)
            prb.append([p_negative_on_x, p_positive_on_x])
        return np.array(prb)

    def predict(self, X):
        prob = self.predict_prob(X)
        return np.argmax(prob, axis=1)

# 计算预测精度:
def accurency(y_pred, y_real):
    return 1 - np.sum(abs(y_pred - y_real))/len(y_pred)

from sklearn.datasets import make_moons, make_circles, make_classification
import matplotlib.pyplot as plt

# 获取数据
X, y = make_circles(noise=0.2, factor=0.5, random_state=1)
#X, y = make_moons(noise=0.2, random_state=1)
#X, y = make_classification()
# print(X[:5])
# print(y[:5])

# 模型训练及预测:
clf = NativeBayes()
clf.fit(X, y)
y_pred = clf.predict(X)
print('模型精度为:%0.4f' % accurency(y_pred, y))

new_X = np.array([[0.5, 0.5]])
print(clf.predict(new_X))

import matplotlib as mpl
# 调整图片风格
mpl.style.use('fivethirtyeight')
# 定义xy网格，用于绘制等值线图
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1),
                     np.arange(y_min, y_max, 0.1))

# 预测可能性
Z = clf.predict_prob(np.c_[xx.ravel(), yy.ravel()])[:, 1]
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=.8)
# plt.show()
# 绘制散点图
plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k')
plt.title("GaussianNaiveBayes")
plt.axis("equal")
plt.show()


# Python 2 and 3 compatability
from __future__ import division, unicode_literals, print_function

# Common Imports
import os
import numpy as np

# ML Imports
from sklearn.svm import SVC
from sklearn.base import BaseEstimator
from sklearn import datasets

# Graph Imports
import matplotlib.pyplot as plt
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12

# Declare Functions
def save_fig(fig_id, tight_layout=True):
    if not os.path.exists('images'):
        os.makedirs('images')
    path = os.path.join(PROJECT_ROOT_DIR, 'images', fig_id + '.png')
    print("Saving figure", fig_id)
    if tight_layout:
        plt.tight_layout()
    plt.savefig(path, format="png", dpi=300)

def plot_svc_decision_boundary(svm_clf, xmin, xmax):
    w = svm_clf.coef_[0]
    b = svm_clf.intercept_[0]

    # w0*x0 + w1*x1 + b = 0
    # x1 = -w0/w1 * x0 - b/w1
    x0 = np.linspace(xmin, xmax, 200)
    decision_boundary = -w[0] / w[1] * x0 - b / w[1]

    margin = 1 / w[1]
    gutter_up = decision_boundary + margin
    gutter_down = decision_boundary - margin

    svs = svm_clf.support_vectors_
    plt.scatter(svs[:, 0], svs[:, 1], s=180, facecolors='#FFAAAA')
    plt.plot(x0, decision_boundary, "k-", linewidth=-2)
    plt.plot(x0, gutter_up, "k--", linewidth=2)
    plt.plot(x0, gutter_down, "k--", linewidth=2)

# Directory settings
PROJECT_ROOT_DIR = '.'

# Declare classes
class MyLinearSVC(BaseEstimator):
    def __init__(self,
                 C=1,
                 eta0=1,
                 eta_d=10000,
                 n_epochs=1000,
                 random_state=None):
        self.C = C
        self.eta0 = eta0
        self.n_epochs = n_epochs
        self.random_state = random_state
        self.eta_d = eta_d

    def eta(self, epoch):
        return self.eta0 / (epoch + self.eta_d)

    def fit(self, X, y):
        # Random Init
        if self.random_state:
            np.random.seed(self.random_state)
        w = np.random.randn(X.shape[1], 1)  # n feat weights
        b = 0

        # m = len(X)
        t = y * 2 - 1  # -1 if t==0, +1 if t ==1
        X_t = X * t
        self.Js = []

        # Training
        for epoch in range(self.n_epochs):
            support_vectors_idx = (X_t.dot(w) + t * b < 1).ravel()
            X_t_sv = X_t[support_vectors_idx]
            t_sv = t[support_vectors_idx]

            J = 1 / 2 * np.sum(w * w) + self.C * (
                np.sum(1 - X_t_sv.dot(w)) - b * np.sum(t_sv))
            self.Js.append(J)

            w_gradient_vector = w - self.C * np.sum(
                X_t_sv, axis=0).reshape(-1, 1)
            b_derivative = -C * np.sum(t_sv)

            w = w - self.eta(epoch) * w_gradient_vector
            b = b - self.eta(epoch) * b_derivative

        self.intercept_ = np.array([b])
        self.coef_ = np.array([w])
        support_vectors_idx = (X_t.dot(w) + t * b < 1).ravel()
        self.support_vectors_ = X[support_vectors_idx]
        return self

    def decision_function(self, X):
        return X.dot(self.coef_[0]) + self.intercept_[0]

    def predict(self, X):
        return (self.decision_function(X) >= 0).astype(np.float64)

# Create Datasets
iris = datasets.load_iris()
X = iris["data"][:, (2, 3)]  # (petal lenght.cm, petal width.cm)
y = (iris["target"] == 2).astype(np.float64).reshape(-1, 1)  # Iris-Verginica

# Fit model
C = 2
svm_clf = MyLinearSVC(
    C=C, eta0=10, eta_d=1000, n_epochs=60000, random_state=42)
svm_clf.fit(X, y)
# svm_clf.predict(np.array([[5, 2], 4, 1]))

# Plot learning curve
plt.plot(range(svm_clf.n_epochs), svm_clf.Js)
plt.axis([0, svm_clf.n_epochs, 0, 100])
save_fig('l-SVC_BGD_learning_curve')
plt.show()

# Intercept and Coefficients
print(svm_clf.intercept_, svm_clf.coef_)

# Compare to sk_base
svm_clf2 = SVC(kernel="linear", C=C)
svm_clf2.fit(X, y.ravel())
print(svm_clf2.intercept_, svm_clf2.coef_)

# Plot SVMs
yr = y.ravel()
plt.figure(figsize=(12, 3.2))
plt.subplot(121)
plt.plot(X[:, 0][yr == 1], X[:, 1][yr == 1], "g^", label="Iris-Virginica")
plt.plot(
    X[:, 0][yr == 0], X[:, 1][yr == 0], "bs", label="Not Iris-Virginica")
plot_svc_decision_boundary(svm_clf, 4, 6)
plt.xlabel("Petal length", fontsize=14)
plt.ylabel("Petal width", fontsize=14)
plt.title("MyLinearSVC", fontsize=14)
plt.axis([4, 6, 0.8, 2.8])

plt.subplot(122)
plt.plot(X[:, 0][yr == 1], X[:, 1][yr == 1], "g^")
plt.plot(X[:, 0][yr == 0], X[:, 1][yr == 0], "bs")
plot_svc_decision_boundary(svm_clf2, 4, 6)
plt.xlabel("Petal length", fontsize=14)
plt.title("SVC", fontsize=14)
plt.axis([4, 6, 0.8, 2.8])
save_fig("my_svc_comparison")
plt.show()


import cv2
import sys
from os import listdir

#Get classifier to detect faces\
face_classifier = cv2.CascadeClassifier('Haarcascades/haarcascade_frontalface_default.xml')
#Get image names
data_path = 'D:/Image Processing Tutorial/Practices/amsrf/face_database/'

#LBPH Face recognizer
model = cv2.face.LBPHFaceRecognizer_create()
model.read("trained_model.yml")

#Eigen face recognizer
# model = cv2.face.EigenFaceRecognizer_create()
# model.read("trained_data_eigen.yml")

#Fisher face recognizer
# model = cv2.face.FisherFaceRecognizer_create()
# model.read("trained_data_fisher.yml")

noFaceMsg = ""

def face_detector(img):
    global noFaceMsg
    # Convert image to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_classifier.detectMultiScale(
        gray,
        scaleFactor=1.3,
        minNeighbors=8,
        minSize=(30, 30)
    )

    if faces is ():
        noFaceMsg = "Couldn't detect any faces"
        return img, []

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,255),2)
        roi = gray[y:y+h, x:x+w]
        roi = cv2.resize(roi, (300, 300))

    return img, roi

def getNames(folder_path):
    seen = set()
    for files in listdir(folder_path):
        temp = files.split("_")
        #print(temp)
        if temp[0] not in seen:
            #Labels.append(temp[1].split(".")[0])
            Labels.append(temp[0])

            IDs.append(temp[0])
            seen.add(temp[0])

def removeDuplicates(values):
    output = []
    seen = set()
    for value in values:
        if value not in seen:
            output.append(value)
            seen.add(value)
    return output

def recognizePerson(grayFace):
    # Pass face to prediction model
    # "results" comprises of a tuple containing the label and the confidence value
    results = model.predict(grayFace)

    #print("line68")
    #print(""+str(results[1]))
    #print("line70")
    #print("{}, {}".format(results[0], results[1]))
    if results[1] < 500:
        confidence = int( 100 * (1 - (results[1])/500) )
        #print("cf = ".format(confidence))
        #print(format(results[0])+"test")
        #print("line76")
        recognizedName = ""
        for i, ids in enumerate(IDs):
            #print(str(results[0])+", "+ids)
            if int(results[0]) == int(ids):
                recognizedName = Labels[i]
                #print(recognizedName)
                break
            else:
                recognizedName = "Unknown"
        #print("line86")
        #print("{}, {}".format(results[0], results[1]))
        return results[0], confidence
    else:
        return results[0],results[1]

if __name__ == '__main__':

    Labels = []
    IDs = []

    getNames(data_path)
    # Labels = removeDuplicates(Labels)
    # IDs = removeDuplicates(IDs)
    # print(Labels)
    # print(IDs)

    # Open Webcam
    cap = cv2.VideoCapture(0 + cv2.CAP_DSHOW)

    while True:

        ret, frame = cap.read()

        if not ret:
            continue

        image, face = face_detector(frame)

        if face == []:
            cv2.putText(image, noFaceMsg, (100, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
            cv2.imshow('Face Recognition', image )
            #continue
        else:
            try:

                recognizedName, confidence = recognizePerson(face)
                #print("line122")
                display_string = format(confidence) + '% Confident'
                cv2.putText(image, display_string, (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (255,120,150), 2)
                #print("Line125")
                if confidence > 85:
                    #print("Line127")
                    cv2.putText(image, "Recognized {0}".format(recognizedName), (50, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
                    #print("Line129")
                    cv2.imshow('Face Recognition', image )
                else:
                    cv2.putText(image, "Unknown person detected", (50, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
                    cv2.imshow('Face Recognition', image )

            except:
                #cv2.putText(image, "No Face Found", (220, 120) , cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
                #cv2.putText(image, "Couldn't Recognize", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0,0,255), 2)
                #cv2.imshow('Face Recognition', image )
                print("Unexpected error: ", sys.exc_info()[0])
                #raise
                break

        if cv2.waitKey(1) == 13: #13 is the Enter Key
            break

    cap.release()
    cv2.destroyAllWindows()


from django.urls import path
from . import views

urlpatterns = [
    path("", views.Home, name="home"),
    path('matchesps', views.matches_per_season_charts, name='matches_ps'),
    path('winsperseason', views.wins_per_season_charts, name='wins_ps'),
    path('extraruns', views.extra_runs_charts, name='extra_runs'),
    path('economy', views.economy_charts, name='economy'),
    path('batting_average', views.batting_average_charts, name='batting_avg'),
    path("json/matchesps/", views.matches_per_season, name="matches"),
    path("json/winsperseason/", views.wins_per_season, name="wins"),
    path("json/extraruns/", views.extra_runs, name="extra"),
    path("json/economy/", views.economy, name="econ"),
    path("json/batting_average/", views.batting_average, name="batting_average"),

]


from sqlalchemy.orm import Session

from .models import Message

class RecorderMiddleare:

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

    def process_input(self, input_event):
        m = Message(
            user_id=input_event.user_id,
            channel_id=input_event.channel,
            message=input_event.message,
        )
        session = Session(bind=self.conn)
        session.add(m)
        session.commit()


from django.shortcuts import render, redirect
from django.views.generic import View
from django.http import JsonResponse
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from . import config
from django.db.models import Q
from django.core.exceptions import ObjectDoesNotExist
from textblob import TextBlob

import os

import sys
sys.path.insert(1, "/home/Aiasem/mysite/chat_bot/")
import find_sim

from .models import ArtGallery, InfoGuide

import telebot
from telebot import types

image_names_kz = []
image_names_ru = []
image_names_eng = []
image_codes = []

def index(request):
    pass
    global image_names_kz, image_names_ru, image_names_eng, image_codes

    # find_sim.dataset_process()
    return render(request, "chat_bot/index.html", {'test': image_names_eng})

language_used = 'en'
result = ""
TOKEN = config.token
bot = telebot.TeleBot(TOKEN, threaded=False)

for q in ArtGallery.objects.all():
    image_names_kz.append(q.image_title_kz)
    image_names_ru.append(q.image_title_ru)
    image_names_eng.append(q.image_title_eng)
    image_codes.append(str(q.unique_code))
# так как вебхук уже подключен будем ждать новую информацию в TelegramBotView

class TelegramBotView(View):
    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return super(TelegramBotView, self).dispatch(request, *args, **kwargs)

    def post(self, request):
        bot.process_new_updates([telebot.types.Update.de_json(request.body.decode('utf-8'))]) # тут мы уже получили новую инфу, эти изменения обновляем в боте
        return JsonResponse({'code': 'accepted'})

# КОД БОТА

@bot.message_handler(commands=['start'])
def handle_start(message):
    user_id = message.from_user.id
    startObj = InfoGuide.objects.get(command_title = message.text.replace('/',""))
    text_eng = startObj.eng
    probel = '_'* 45
    text_ru = startObj.ru
    text_kz = startObj.kz
    text_fin = text_eng + "\n" + probel + "\n" + text_ru + "\n" + probel + "\n" +text_kz

    bot.send_message(user_id, text_fin )

    # bot.send_message(user_id, "HIIIII" )

@bot.message_handler(commands=['help','imagename', 'camera', 'uniquecode'])
def get_help_info(message):
    user_id = message.from_user.id
    text = message.text.replace('/',"")
    bot.send_message(user_id, command_text(text))

@bot.message_handler(commands=['language'])
def handle_language(message):

    user_id = message.from_user.id

    eng_button = types.InlineKeyboardButton(text="English", callback_data="en")
    ru_button = types.InlineKeyboardButton(text="Русский", callback_data="ru")
    kz_button = types.InlineKeyboardButton(text="Қазақша", callback_data="kk")
    keyboard = types.InlineKeyboardMarkup()
    keyboard.add(eng_button)
    keyboard.add(ru_button)
    keyboard.add(kz_button)

    bot.send_message(user_id, "Please choose the language:", reply_markup=keyboard)

@bot.message_handler(content_types=['photo']) # если отправили фото
def handle_image(message):

    global result
    user_id = message.from_user.id

    try:
        file_info = bot.get_file(message.photo[len(message.photo)-1].file_id)
        downloaded_file = bot.download_file(file_info.file_path)
        src = find_sim.query_image
        bot.send_message(user_id, command_text("wait"))
        with open(src, 'wb') as new_file:
            new_file.write(downloaded_file)

        base = os.path.basename(find_sim.calculation())

        # bot.send_message(user_id, base)
        if(base == ""):
            text = command_text("error")
            bot.send_message(user_id, text)
        else:
            result = os.path.splitext(base)[0]
            result = result.replace('_'," ")
            result = ArtGallery.objects.get(image_title_eng = result)

            text = command_text("info")
            bot.send_message(user_id, text , reply_markup = gen_markup())

    except Exception:
        bot.send_message(user_id, command_text("error"))

@bot.message_handler(content_types=['voice'])
def handle_voice(message):
    global language_used, result
    global image_names_kz, image_names_ru, image_names_eng, image_codes

    error = False
    user_id = message.from_user.id

    try:
        file_info = bot.get_file(message.voice.file_id)
        downloaded_file = bot.download_file(file_info.file_path)
        src = find_sim.query_voice
        with open(src, 'wb') as new_file:
            new_file.write(downloaded_file)
        res = find_sim.voice_proccess(language_used)
    except Exception:
        bot.send_message(user_id, command_text("error"))
    try:
        res = res.replace(' ',"").lower()

        if res.isdigit():
            result = ArtGallery.objects.get(unique_code = res)
            text = command_text("info")
            bot.send_message(user_id, text , reply_markup = gen_markup())
            error = False
        else:
            for (b, c, d) in zip(image_names_eng, image_names_kz, image_names_ru):
                x = b.replace(" ", "").lower()
                y = c.replace(" ", "").lower()
                z = d.replace(" ", "").lower()

                if( (x in res) or (y in res) or (z in res) ):
                    result = ArtGallery.objects.get(
                        Q(image_title_eng__contains = b)  |
                        Q(image_title_kz__contains = c) |
                        Q(image_title_ru__contains = d)
                    )
                    text = command_text("info")
                    bot.send_message(user_id, text , reply_markup = gen_markup())
    except Exception:
        bot.send_message(user_id, command_text("error"))

@bot.message_handler(content_types=['text']) # обычные сообщения
def handle_text(message):
    global language_used, result
    global image_names_kz, image_names_ru, image_names_eng, image_codes
    user_id = message.from_user.id
    #lang = TextBlob(text)
    try:
        res = message.text.replace(' ',"").lower()
        if res.isdigit():
            result = ArtGallery.objects.get(unique_code = res)
            text = command_text("info")
            bot.send_message(user_id, text , reply_markup = gen_markup())
            error = False
        else:
            for (b, c, d) in zip(image_names_eng, image_names_kz, image_names_ru):
                x = b.replace(" ", "").lower()
                y = c.replace(" ", "").lower()
                z = d.replace(" ", "").lower()

                if( (x in res) or (y in res) or (z in res) ):
                    result = ArtGallery.objects.get(
                        Q(image_title_eng__contains = b)  |
                        Q(image_title_kz__contains = c) |
                        Q(image_title_ru__contains = d)
                    )
                    text = command_text("info")
                    bot.send_message(user_id, text , reply_markup = gen_markup())
        #             error = False
        #         else:
        #             error = True
        # if error:
        #     bot.send_message(user_id, command_text("error"))

    except Exception:
        bot.send_message(user_id, command_text("error"))

def gen_markup():

    keyboard = types.InlineKeyboardMarkup()

    audio_button = types.InlineKeyboardButton(text="Audio", callback_data="audio")
    text_button = types.InlineKeyboardButton(text="Text", callback_data="text")

    keyboard.row(audio_button, text_button)

    return keyboard

def command_text(text):
    global language_used
    text = InfoGuide.objects.get(command_title = text)
    if(language_used == "en"):
        text = text.eng
    elif(language_used == "kk"):
        text = text.kz
    elif(language_used == "ru"):
        text = text.ru
    return text

def audio_lang():
    global result,language_used
    if(language_used == "kk"):
        data = result.audio_kz
    elif(language_used == "ru"):
        data = result.audio_ru
    elif(language_used == "en"):
        data = result.audio_eng
    return data

def text_lang():
    global result, language_used
    if(language_used == "kk"):
        data = result.text_kz
    elif(language_used == "ru"):
        data = result.text_ru
    elif(language_used == "en"):
        data = result.text_eng
    return data

@bot.callback_query_handler(func=lambda call: True)
def query_handler(call):
    global result
    global language_used

    user_id = call.from_user.id

    if call.data == 'en':
        language_used = "en"
        # command_text("language")
        bot.answer_callback_query(call.id,"OK")

    if call.data == "ru":
        language_used = "ru"
        bot.answer_callback_query(call.id,"OK")

    if call.data == "kk":
        language_used = "kk"
        bot.answer_callback_query(call.id,"OK")

    if call.data == "audio":
        bot.send_voice(user_id, audio_lang())
        result = ""

    if call.data == "text":
        bot.send_message(user_id, text_lang())
        result = ""


# 쓰레드 (Thread)

# threading 모듈
# 파이썬에서 쓰레드를 실행하기 위해서는,
# threading 모듈의 threading.Thread() 함수를 호출하여 Thread 객체를 얻은 후 Thread 객체의 start() 메서드를 호출하면 된다.
# 서브쓰레드는 함수 혹은 메서드를 실행하는데, 일반적인 구현방식으로 (1) 쓰레드가 실행할 함수 혹은 메서드를 작성하거나
# 또는 (2) threading.Thread 로부터 파생된 파생클래스를 작성하여 사용하는 방식 등이 있다.

import threading

def sum(low, high):
    total = 0
    for i in range(low, high):
        total += i
    print(__name__,"  Subthread 실행 결과 => ",low,"~",high ,"total", total)

t = threading.Thread(target=sum, args=(1, 100000))
t.start()

print(__name__,"  Main Thread  1")

# python -m pip install --upgrade pip   =>  도스창에서 pip 설치한다.
# python -m pip install requests

# 예제(A)

import  requests
import  time

def getHtml(url):
    resp = requests.get(url)
    time.sleep(1)
    print(url, len(resp.text), ' chars')

t1 = threading.Thread(target=getHtml, args=('http://google.com',))
t1.start()

print(__name__,"  Main Thread  2")

# *****************************************************************
# python -m pip install --upgrade pip   =>  도스창에서 pip 설치한다.
# python -m pip install requests
# C:\Users\USER\AppData\Local\Programs\Python\Python36-32\Scripts폴더에 설치 된거 확인

# 예제(B)
import threading, requests, time

class HtmlGetter (threading.Thread):
    def __init__(self, url):
        threading.Thread.__init__(self)
        self.url = url

    def run(self):
        resp = requests.get(self.url)
        time.sleep(1)
        print(self.url, len(resp.text), ' chars')

t = HtmlGetter('http://google.com')
t.start()

print("*************************************************************")

import threading, requests, time

def getHtml(url):
    resp = requests.get(url)
    time.sleep(1)
    print(__name__,url, len(resp.text), ' chars')

# 데몬 쓰레드
t1 = threading.Thread(target=getHtml, args=('http://google.com',))
t1.daemon = True
t1.start()

print("*************************************************************")


import gym
import numpy as np
import imageio
import os
import sys
from pyglet.window import key

from data import LEFT, RIGHT, GO, ACTIONS

samples_each_classes = 200

def action_to_id(a):
    if all(a == [-1, 0, 0]): return LEFT
    elif all(a == [1, 0, 0]): return RIGHT
    else:
        return GO

if __name__=='__main__':

    if len(sys.argv) < 2:
        sys.exit("Usage : python record_dataset.py path")

    env = gym.make('CarRacing-v0').env
    env.reset()

    folder = sys.argv[1]
    images = os.path.join(folder, "images")
    labels = os.path.join(folder, "labels.txt")
    os.makedirs(images, exist_ok=True)

    a = np.array([0.0, 0.0, 0.0])

    def key_press(k, mod):
        global restart
        if k==key.LEFT:  a[0] = -1.0
        if k==key.RIGHT: a[0] = +1.0
        if k==key.UP:    a[1] = +1.0
        if k==key.DOWN:  a[2] = +0.8   # set 1.0 for wheels to block to zero rotation
    def key_release(k, mod):
        if k==key.LEFT  and a[0]==-1.0: a[0] = 0
        if k==key.RIGHT and a[0]==+1.0: a[0] = 0
        if k==key.UP:    a[1] = 0
        if k==key.DOWN:  a[2] = 0

    env.viewer.window.on_key_press = key_press
    env.viewer.window.on_key_release = key_release
    env.reset()
    for i in range(100):
        env.step([0, 0, 0])
        env.render()

    file_labels = open(labels, 'w')
    samples_saved = {a: 0 for a in ACTIONS}

    i = 0
    while True:
        s, r, done, info = env.step(a)
        action_id = action_to_id(a)
        if samples_saved[action_id] < samples_each_classes:
            samples_saved[action_id] += 1
            samples_each_classes
            imageio.imwrite(os.path.join(folder, 'images', 'img-%s.jpg' % i ), s)
            file_labels.write('%s %s\n' % ('img-%s.jpg' % i, action_id))
            file_labels.flush()
            i += 1
            print(samples_saved)
        env.render()
    env.close()


# !/usr/bin/env python3
# -*- coding:utf-8 -*-
import jieba
import json

# 讀取 json 的程式
def jsonTextReader(jsonFilePath):
    with open(jsonFilePath, 'r', encoding='utf-8') as f:
        jsonFile = json.load(f)
        return jsonFile['text']

# 將字串轉為「句子」列表的程式
def text2Sentence(inputSTR):
    # replace '...' and '…' with ''
    unused = ['...', '…']
    for sep in unused:
        inputSTR = inputSTR.replace(sep, '')

    # replace '，', '、', '。', '「', '」' with separator '\n'
    separators = ['，', '、', '。', '「', '」']
    for sep in separators:
        inputSTR = inputSTR.replace(sep, '\n')

    # if ',' is not located in number, replace it with '\n'
    currentIndex = 0
    while True:
        # find next ','
        currentIndex = inputSTR.find(',', currentIndex)

        # ',' not find
        if currentIndex == -1:
            break

        # ',' is located in numbers
        if str.isdigit(inputSTR[currentIndex - 1]) and str.isdigit(inputSTR[currentIndex + 1]):
            currentIndex += 1
            continue

        inputSTR = inputSTR[:currentIndex] + '\n' + inputSTR[currentIndex + 1:]
        currentIndex += 1

    return inputSTR.strip('\n').split('\n')

def cutSentence(strList):
    resultList = []
    for s in strList:
        resultList.append("/".join(jieba.cut(s, cut_all=False)))
    return resultList

if __name__ == "__main__":
    # 設定要讀取的 news.json 路徑
    newsJsonPath = './example/news.json'

    # 將 news.json 利用 [讀取 json] 的程式打開
    text = jsonTextReader(newsJsonPath)

    # 將讀出來的內容字串傳給 [將字串轉為「句子」 列表」]的程式，存為 newsLIST
    newsLIST = text2Sentence(text)

    print("\n".join(cutSentence(newsLIST)))


import boto3
from boto3.dynamodb.conditions import Key, Attr

dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table("library")

def lambda_handler(event, context):
    query = event.get("query", "all")
    #Without filter
    if query == "all":
        response = table.scan()
        data = response["Items"]
    else:
    #With filter

        response = table.query(
            KeyConditionExpression=Key('books').eq(query)
            )
        data = response["Items"]

    return "\n".join([str(value) for value in data])

def main():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("-a", dest="command",  required=False, type=str, help="Command")
    parser.add_argument("-q", dest="query",    required=False, type=str, help="Query")
    args = parser.parse_args()

    if args.command == "fetch":
        event = {
            "query": args.query
        }
        response = lambda_handler(event, None)
        print(response)
    else:
        print(f"{args.command} not mapped")

if __name__ == "__main__":
    main()


import numpy as np
import cv2
import DataOperation
import Setting
import matplotlib.pyplot as plt

images_A_paths = DataOperation.get_image_paths(Setting.IMAGE_PATH_A)
images_B_paths = DataOperation.get_image_paths(Setting.IMAGE_PATH_B)
print('Number of images_A is {}, number of images_B is {}'.format(len(images_A_paths), len(images_B_paths)))

A_Images = DataOperation.load_images(images_A_paths[:3])
B_Images = DataOperation.load_images(images_B_paths[:3])

figure = np.concatenate([A_Images, B_Images], axis=0)
print(figure.shape)
figure = figure.reshape((2, 3) + figure.shape[1:])
print(figure.shape)
figure = DataOperation.stack_image(figure)
print(figure.shape)
plt.imshow(cv2.cvtColor(figure, cv2.COLOR_RGB2BGR))
plt.show()


from django import forms
from .models import Todo
from friendship.models import Friend
from datetimewidget.widgets import DateTimeWidget
from django.contrib.auth.models import User

class TodoForm(forms.ModelForm):

    class Meta:
        model = Todo
        fields = (
            'title',
            'text',
            'due_date',
            'shared_user',
            'completed',
        )
        widgets = {
            'title': forms.TextInput(attrs={'placeholder': 'Title'}),
            'text': forms.Textarea(attrs={'placeholder': 'Detailed description', 'rows': 4}),
            'due_date': DateTimeWidget(attrs={'id': "yourdatetimeid", 'placeholder': "Due Date"}, usel10n=True, bootstrap_version=3),
            'shared_user': forms.SelectMultiple()
        }


from collections import OrderedDict
import os
from argparse import Namespace
from typing import List, Tuple

import pkg_resources
import torch
from tqdm import tqdm
from hrnet_pose import models
from hrnet_pose.config import cfg, update_config
from hrnet_pose.core.inference import get_final_preds
from PIL import Image
from torchvision import transforms

INPUT_SIZE = 256, 192
IMAGE_SIZE = 176, 256
TRESHOLD = 0.15
SCALE_FACTOR = 4

DEFAULT_ARGS = Namespace(
    cfg=pkg_resources.resource_filename(
        'hrnet_pose', 'yaml/coco/hrnet/w48_256x192_adam_lr1e-3.yaml'),
    dataDir='.',
    logDir='.',
    modelDir='.',
    prevModelDir='.'
)

def transform_preds(p):
    # rotate -90 deg and scale to image dimensions
    p[..., 1] = (INPUT_SIZE[1] - 1 - p[..., 1]*SCALE_FACTOR) * IMAGE_SIZE[0] // INPUT_SIZE[1]
    p[..., 0] = p[..., 0] * SCALE_FACTOR * IMAGE_SIZE[1] // INPUT_SIZE[0]

class PoseEstimator:

    def __init__(self, args, use_cuda):
        update_config(cfg, args)
        self.model = models.pose_hrnet.get_pose_net(cfg, False)
        if use_cuda:
            self.model = self.model.cuda()
            self.model.load_state_dict(torch.load(cfg.TEST.MODEL_FILE), strict=False)
        else:
            self.model.load_state_dict(torch.load(cfg.TEST.MODEL_FILE, map_location=torch.device('cpu')), strict=False)
        self.model.eval()
        self.use_cuda = use_cuda
        self.transform = transforms.Compose([
            transforms.Resize(INPUT_SIZE),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
            transforms.Lambda(lambda t: torch.rot90(t, dims=(1, 2)))
        ])

    def infer(self, image: Image):
        """Takes an input RGB image <= INPUT_SIZE and returns an (17 x 2)-array"""
        batch = self.transform(image).unsqueeze(0)
        if self.use_cuda:
            batch = batch.cuda()
        with torch.no_grad():
            output = self.model(batch).detach().cpu().numpy()
        preds, maxvals = get_final_preds(output)
        transform_preds(preds)
        points = preds[0].astype(int)
        maxvals = maxvals[0]
        invalid = maxvals[..., 0] < TRESHOLD
        points[invalid] = -1
        return points

    def infer_batch(self, images):
        batch = torch.stack([self.transform(image) for image in images])
        if self.use_cuda:
            batch = batch.cuda()
        with torch.no_grad():
            output = self.model(batch).detach().cpu().numpy()
        preds, maxvals = get_final_preds(output)
        transform_preds(preds)
        points = preds.astype(int)
        invalid = maxvals[..., 0] < TRESHOLD
        points[invalid] = -1
        return points

if __name__ == '__main__':
    # most important parameters
    import sys
    input_folder, output_path = sys.argv[1:]
    pose_estimator = 'assets/pretrains/pose_hrnet_w48_256x192.pth'

    batch_size = 128

    args = DEFAULT_ARGS
    args.opts = ['TEST.MODEL_FILE', pose_estimator]

    model = PoseEstimator(args, use_cuda=True)

    with open(output_path, 'w') as result_file:
        processed_names = set()
        print('name:keypoints_y:keypoints_x', file=result_file)
        batch = OrderedDict()
        for image_name in tqdm(os.listdir(input_folder)):
            if '.ipynb_checkpoints' in image_name:
                continue

            image = Image.open(os.path.join(input_folder, image_name))
            batch[image_name] = image
            if len(batch) >= batch_size:
                for iname, pose_cords in zip(batch.keys(), model.infer_batch(list(batch.values()))):
                    print("%s: %s: %s" % (iname, list(pose_cords[:, 0]), list(
                        pose_cords[:, 1])), file=result_file)
                result_file.flush()
                batch = OrderedDict()
        if batch:
            for iname, pose_cords in zip(batch.keys(), model.infer_batch(list(batch.values()))):
                print("%s: %s: %s" % (iname, list(pose_cords[:, 0]), list(
                    pose_cords[:, 1])), file=result_file)
            result_file.flush()


import logistic_regression as lr
import importMNIST
import numpy as np
from sklearn.linear_model import LogisticRegression
from scipy.optimize import minimize
import time
from prettytable import PrettyTable

def lr_model(num_runs):

    deg = [[1,0,0], [1,1,0]]

    mean_acc = []
    median_acc = []
    var_acc = []

    for degrees in deg:
        print (degrees)
        compare_accuracy = []
        for i in range(num_runs):
            train_x, train_y, test_x, test_y= importMNIST.get_MNIST()

            model = lr.logistic(degrees, train_x, train_y, 10, 9)
            model.train(tol=1e-4)

            _, accuracy = model.accuracy(test_x,test_y)

            compare_accuracy.append(accuracy)

        mean_acc.append(np.mean(compare_accuracy))
        median_acc.append(np.mean(compare_accuracy))
        var_acc.append(np.var(compare_accuracy))

    t = PrettyTable(['Model', 'Mean', 'Median', 'Variance'])
    for i in range(len(deg)):
        t.add_row([deg[i], mean_acc[i], median_acc[i], var_acc[i]])
    print (t)

def skl_model(num_runs):

    deg = [[1, 1, 0]]

    mean_acc = []
    median_acc = []
    var_acc = []
    compare_accuracy = []

    for i in range(num_runs):
        print (i)
        train_x, train_y, test_x, test_y= importMNIST.get_MNIST()

        model = LogisticRegression(solver='lbfgs', multi_class='multinomial').fit(train_x,train_y)
        accuracy = model.score(test_x,test_y)
        compare_accuracy.append(accuracy)

    mean_acc.append(np.mean(compare_accuracy))
    median_acc.append(np.mean(compare_accuracy))
    var_acc.append(np.var(compare_accuracy))

    t = PrettyTable(['Model', 'Mean', 'Median', 'Variance'])
    for i in range(len(deg)):
        t.add_row([deg[i], mean_acc[i], median_acc[i], var_acc[i]])
    print (t)

def compare_optimisers():

    compare_optimisers = []
    model_coeffs = []

    train_x, train_y, test_x, test_y= importMNIST.get_MNIST()

    #terms = [constant, linear, quadratic]
    degrees = [1, 1, 0]
    '''
    model = lr.logistic(degrees, train_x, train_y, 10, 9)
    model.train(tol=1e-4)
    _, accuracy = model.accuracy(test_x,test_y)
    compare_optimisers.append(accuracy)
    model_coeffs.append(model.B_mat)
    '''

    model_sk = lr.logistic(degrees, train_x, train_y, 10, 9)

def grad_updates(xvals, model):
    pass

if __name__ == "__main__":

    ##################
    # Compare functions
    ##################
    compare_optimisers()
    #skl_model(5)


# -*- coding: utf-8 -*-
"""raw packet data

:mod:`pcapkit.protocols.raw` contains
:class:`~pcapkit.protocols.raw.Raw` only, which implements
extractor for unknown protocol, and constructs a
:class:`~pcapkit.protocols.protocol.Protocol` like object.

"""
import io

from pcapkit.corekit.infoclass import Info
from pcapkit.corekit.protochain import ProtoChain
from pcapkit.protocols.null import NoPayload
from pcapkit.protocols.protocol import Protocol
from pcapkit.utilities.exceptions import UnsupportedCall

__all__ = ['Raw']

class Raw(Protocol):
    """This class implements universal unknown protocol."""

    ##########################################################################
    # Properties.
    ##########################################################################

    # name of current protocol
    @property
    def name(self):
        """Name of current protocol.

        :rtype: Literal['Unknown']
        """
        return 'Unknown'

    # header length of current protocol
    @property
    def length(self):
        """Header length of current protocol.

        Raises:
            UnsupportedCall: This protocol doesn't support :attr:`length`.

        """
        raise UnsupportedCall(f"{self.__class__.__name__!r} object has no attribute 'length'")

    # name of next layer protocol
    @property
    def protocol(self):
        """Name of next layer protocol.

        Raises:
            UnsupportedCall: This protocol doesn't support :attr:`protocol`.

        """
        raise UnsupportedCall(f"{self.__class__.__name__!r} object has no attribute 'protocol'")

    ##########################################################################
    # Methods.
    ##########################################################################

    def read(self, length=None, *, error=None, **kwargs):  # pylint: disable=arguments-differ,unused-argument
        """Read raw packet data.

        Args:
            length (Optional[int]): Length of packet data.

        Keyword Args:
            error (Optional[str]): Parsing errors if any.
            **kwargs: Arbitrary keyword arguments.

        Returns:
            DataType_Raw: The parsed packet data.

        """
        if length is None:
            length = len(self)

        raw = dict(
            packet=self._read_fileng(length),
            error=error or None,
        )

        return raw

    def make(self, **kwargs):
        """Make raw packet data.

        Keyword Args:
            packet (bytes): Raw packet data.
            **kwargs: Arbitrary keyword arguments.

        Returns:
            bytes: Constructed packet data.

        """
        return bytes(kwargs.get('packet'))

    ##########################################################################
    # Data models.
    ##########################################################################

    def __post_init__(self, file, length=None, *, error=None, **kwargs):  # pylint: disable=arguments-differ
        """Post initialisation hook.

        Args:
            file (io.BytesIO): Source packet stream.
            length (Optional[int]): Length of packet data.

        Keyword Args:
            error (Optional[str]): Parsing errors if any (for parsing).
            **kwargs: Arbitrary keyword arguments.

        Would :mod:`pcapkit` encounter malformed packets, the original parsing
        error message will be provided as in ``error``.

        See Also:
            For construction argument, please refer to :meth:`make`.

        """
        if file is None:
            _data = self.make(**kwargs)
        else:
            _data = file.read(length)

        #: bytes: Raw packet data.
        self._data = _data
        #: io.BytesIO: Source packet stream.
        self._file = io.BytesIO(self._data)
        #: pcapkit.corekit.infoclass.Info: Parsed packet data.
        self._info = Info(self.read(length, error=error, **kwargs))

        #: pcapkit.protocols.null.NoPayload: Next layer (no payload).
        self._next = NoPayload()
        #: pcapkit.corekit.protochain.ProtoChain: Protocol chain from current layer.
        self._protos = ProtoChain(self.__class__, self.alias)

    @classmethod
    def __index__(cls):
        """Numeral registry index of the protocol.

        Raises:
            UnsupportedCall: This protocol has no registry entry.

        """
        raise UnsupportedCall(f'{cls.__name__!r} object cannot be interpreted as an integer')


import numpy as np
import matplotlib.pyplot as plt

# Hide axes
# ax.xaxis.set_visible(False)
# ax.yaxis.set_visible(False)

showing = False

def showTable(q):
    global showing
    fig, ax = plt.subplots()
    fig.patch.set_visible(False)
    ax.axis('off')
    labels = [i for i in range(len(q))]
    cols = [i for i in range(len(q[0]))]
    plt.table(cellText=q, rowLabels=labels, colLabels=cols, loc='center')
    plt.pause(0.05)
    if not showing:
        plt.show()
        showing = True

saved = False

def saveTable(q):
    global saved
    if saved:
        return
    f = open('table.csv', 'w')

    for st in q:
        for a in st:
            f.write("%f," % a)
        f.write("\n")

    f.close()
    saved = True


import logging

from rest_framework.generics import GenericAPIView
from rest_framework.response import Response

from .models import ChineseMetadata, ChineseWord
from .serializers import ChineseWordsSerializer

LOG = logging.getLogger(__name__)

class RecordWords(GenericAPIView):
  serializer_class = ChineseWordsSerializer

  def post(self, request, format=None):
    metadata = ChineseMetadata(**{
      'native': request.POST.get('native') == 'true',
      'gender': request.POST.get('gender')
    })
    metadata.save()

    for word in request.FILES:
      # import ipdb
      # ipdb.set_trace()
      _word = ChineseWord(word=word, wave=request.FILES.get(word).read(), metadata=metadata)
      _word.save()

    return Response(status=204)
record_words = RecordWords.as_view()


#import pandas as pd
#import numpy as np
#import datetime as dt
from keras import backend as K
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM, SimpleRNN
from pandas import Series
from keras.regularizers import l2
import numpy as np

class FNN():
    def __init__(self, units = [5], dropout=None, layers=1, regularizer=None, epochs=1000, batch_size=256, weights=None):
        self.batch_size = batch_size
        self.time_steps = 1
        self.num_features = 1
        self.epochs = epochs
        self.layers = layers
        self.units = units
        self.activation = ["tanh", "linear"]
        self.regularizer = regularizer
        self.dropout = dropout
        self.model = self._create(batch_size=self.batch_size, weights=weights)

    def _create(self, batch_size=1, weights=None):
        model = Sequential()
        for n in range(self.layers):
            model.add(Dense( int(self.units[0]),
                input_shape=(50,),
                activation=self.activation[0],
                kernel_regularizer = l2(0.01) if self.regularizer == "l2" else None,
                ))
        model.add(Dropout(self.dropout))
        model.add(Dense(1, activation=self.activation[-1]))

        if(weights is not None):
            model.set_weights(weights)

        model.compile(loss='mean_squared_error', optimizer='adam')
        return model

    def window(self, x, ws=50):
        x = x.reshape(-1)
        xx=[]
        for n in range(len(x) - ws):
            xx+=[x[n:n+ws]]
        return np.array(xx)

    def train(self, x, y, x_val, y_val):
        x = self.window(x, ws=50)
        y = y.reshape(-1)[50:]
        bs1 = int(x.shape[0]/self.batch_size) * self.batch_size
        x = x[:bs1]
        y = y[:bs1]
        for n in range( self.epochs ):
            if (n%10) is 0:
                print("Training step ", n)
            self.model.fit(x, y, batch_size=self.batch_size, shuffle=False, epochs=1, verbose=0)
            #self.model.reset_states()
        return self

    def _prediction_model(self):
        old_weights = self.model.get_weights()
        return self._create(batch_size=1, weights=old_weights)

    def single_forecast(self, x):
        x = self.window(x, ws=50)
        model = self._prediction_model()
        #model.reset_states()
        return model.predict(x, batch_size=1).reshape(-1)

    def multistep_forecast(self, x, horizon=24):
        x = self.window(x, ws=50)

        predictions={}
        for i in range(horizon):
            predictions[i] = []

        model = self._prediction_model()
        for t_0 in x:
            current_window = t_0
            predictions[0].append(model.predict(np.array([current_window]), batch_size=1)[0][0])
            for t_n in range(1, horizon):
                current_window = np.append( current_window[1:], predictions[t_n-1][-1]  )
                y = model.predict( np.array([current_window]), batch_size=1)
                predictions[t_n].append(y[0][0])
        return predictions

    def forecast(self, x, horizon=6): # SKIP?
        model = self._prediction_model()
        results = []

        model.reset_states()
        #return Series(model.predict(x, batch_size=1).reshape(-1))
        """
        for t_0 in range(1, len(x)+1):
            predictions = []
            model.reset_states()
            last_prediction = model.predict(x[:t_0], batch_size=1)[-1].reshape(1,1,1)

            #predictions.append(last_prediction)
            #for t_n in range(horizon):
            #    last_prediction = model.predict(predictions[-1], batch_size=1 )
            #    predictions.append(last_prediction)
            #results.append(predictions)
        return results
        """


from typing import Any

try:
    import torch

    HAVE_TORCH = 1
except:
    HAVE_TORCH = 0

import pudb.var_view as vv

def torch_stringifier_fn(value: Any) -> str:
    if not HAVE_TORCH:
        # Fall back to default stringifier

        return vv.default_stringifier(value)

    if isinstance(value, torch.nn.Module):
        device: str = str(next(value.parameters()).device)
        params: int = sum([p.numel() for p in value.parameters() if p.requires_grad])
        rep: str = value.__repr__() if len(value.__repr__()) < 55 else type(
            value
        ).__name__

        return "{}[{}] Params: {}".format(rep, device, params)
    elif isinstance(value, torch.Tensor):
        return "{}[{}][{}] {}".format(
            type(value).__name__,
            str(value.dtype).replace("torch.", ""),
            str(value.device),
            str(list(value.shape)),
        )
    else:
        # Fall back to default stringifier

        return vv.default_stringifier(value)


import logging
import pandas as pd
from map_model import Evaluation_map
from models.user2vec import User2Vec
import numpy as np

_logger = logging.getLogger(__name__)
logging.basicConfig(
    level=logging.INFO, format="%(asctime)s\t%(levelname)s\t%(name)s\t%(message)s"
)

PRIVATE_KAY_PATH = "../../../bigquery.json"
READ_ADDAR_PATH = "models/adar_df.csv"
READ_JACCARD_PATH = "models/jaccard_df.csv"
USER2VEC_KV_PATH = "../models/user2vec.kv"
USER2VEC_ADJACENCY_PATH = "../models/adjacency_matrix.pkl"

def evaluation_adar():
    _logger.info("First is adar index")
    recommendation_df = pd.read_csv(READ_ADDAR_PATH).astype(str)
    _logger.info("Begginning to caluculate adar`s map score")
    adar_evaluation = Evaluation_map(TODAY_CONNECTIONS_DF, recommendation_df, _logger)
    adar_evaluation.main()
    adar_score = adar_evaluation.caluculate_map()
    del adar_evaluation  # メモリの節約
    del recommendation_df
    return adar_score

def evaluation_jaccard():
    _logger.info("Second is jaccard coefficient")
    recommendation_df = pd.read_csv(READ_JACCARD_PATH).astype(str)
    _logger.info("Begginning to caluculate jaccard`s map score")
    jaccard_evaluation = Evaluation_map(
        TODAY_CONNECTIONS_DF, recommendation_df, _logger
    )
    jaccard_evaluation.main()
    jaccard_score = jaccard_evaluation.caluculate_map()
    del recommendation_df
    del jaccard_evaluation
    return jaccard_score

def evaluation_deepwalk():
    _logger.info("Third is deepwalk")
    user2vec = User2Vec.load(USER2VEC_KV_PATH, USER2VEC_ADJACENCY_PATH)
    recommendation_df = pd.DataFrame([], columns=["from", "to", "score"])
    for id in np.unique(TODAY_CONNECTIONS_DF.owner_id.values):
        try:
            recommendation_list = user2vec._kv.most_similar(positive=str(id), topn=10)
            for to_tuple in recommendation_list:
                to_list = list(to_tuple)
                to_list.insert(0, str(id))
                to_series = pd.Series(to_list, index=["from", "to", "score"])
                recommendation_df = recommendation_df.append(
                    to_series, ignore_index=True
                )
        except KeyError:
            continue
    _logger.info("Begginning to caluculate deepwalk`s map score")
    deep_evaluation = Evaluation_map(TODAY_CONNECTIONS_DF, recommendation_df, _logger)
    deep_evaluation.main()
    deepwalk_score = deep_evaluation.caluculate_map()
    del recommendation_df
    del deep_evaluation
    return deepwalk_score

# global variable
_logger.info("Reading new connections")
TODAY_CONNECTIONS_DF = reading_today_connections()

def main():
    score = {}

    score["AdarIndex"] = evaluation_adar()
    score["JaccardCoefficient"] = evaluation_jaccard()
    score["DeepWalk"] = evaluation_deepwalk()

    print(score)

if __name__ == "__main__":
    main()


"""
This file contains views that render a specific page for the gui.
"""

from django.shortcuts import redirect, render, reverse
from django.conf import settings
from django.contrib import messages
from django.views.generic import (View, ListView)
from djangohelpers.views import FilterByQueryParamsMixin
from braces.views import LoginRequiredMixin
from rest_framework.authtoken.models import Token

# import models
from userextensions.models import (UserRecent, UserFavorite)

# import forms
from userextensions.forms import UserPreferenceForm

class ListRecents(LoginRequiredMixin, FilterByQueryParamsMixin, ListView):
    """ Displays a list of urls the user has recently visited, rendered in a paginated, searchable, sortable bootstrap
    table. This view is filterable via query parameters. Includes links to delete individual entries.
    """
    base_template = settings.BASE_TEMPLATE
    template = "handyhelpers/generic/generic_list.html"

    def get(self, request, *args, **kwargs):
        context = dict()
        self.queryset = UserRecent.objects.filter(user=request.user).order_by('-updated_at')
        context['base_template'] = self.base_template
        context['queryset'] = self.filter_by_query_params()
        context['title'] = "Recents"
        context['sub_title'] = request.user.username
        context['table'] = "userextensions/table/table_recents.htm"
        return render(request, self.template, context=context)

class ListFavorites(LoginRequiredMixin, FilterByQueryParamsMixin, ListView):
    """ Displays a list of urls user has set as favorites, rendered in a paginated, searchable, sortable bootstrap
    table. This view is filterable via query parameters. Includes links to delete individual entries. """
    base_template = settings.BASE_TEMPLATE
    template = "handyhelpers/generic/generic_list.html"

    def get(self, request, *args, **kwargs):
        context = dict()
        self.queryset = UserFavorite.objects.filter(user=request.user).order_by('-updated_at')
        context['base_template'] = self.base_template
        context['queryset'] = self.filter_by_query_params()
        context['title'] = "Favorites"
        context['sub_title'] = request.user.username
        context['table'] = "userextensions/table/table_favorites.htm"
        return render(request, self.template, context=context)

class DetailUser(LoginRequiredMixin, View):
    """ Displays user details, including group configuration, API token, and configuration for theme, start page, and
    recents count. Includes link to refresh API token and modal form to edit user preferences. """
    base_template = settings.BASE_TEMPLATE
    template = "userextensions/detail/detail_user.html"

    def get(self, request):
        context = dict()
        # include user preference form
        form_data_user_preferences = dict()
        form_data_user_preferences['form'] = UserPreferenceForm(request.POST or None, instance=request.user.preference)
        form_data_user_preferences['action'] = "Update"
        form_data_user_preferences['action_url'] = reverse('userextensions:detail_user')
        form_data_user_preferences['title'] = "<b>Update Preferences: </b><small> {} </small>".format(request.user)
        form_data_user_preferences['modal_name'] = "update_user_preferences"
        context['form_data_user_preferences'] = form_data_user_preferences

        context['user'] = request.user
        context['token'] = str(Token.objects.get_or_create(user=request.user)[0])
        context['groups'] = sorted([i.name for i in request.user.groups.all()])
        context['base_template'] = self.base_template
        return render(request, self.template, context=context)

    def post(self, request):
        redirect_url = request.META.get('HTTP_REFERER')
        form = UserPreferenceForm(request.POST or None, instance=request.user.preference)
        if form.is_valid():
            form.save()
            messages.add_message(request, messages.ERROR, "Preferences updated!", extra_tags='alert-info', )
            return redirect(redirect_url)
        else:
            for error in form.errors:
                messages.add_message(request, messages.ERROR, "Input error: {}".format(error),
                                     extra_tags='alert-danger', )
            return self.get(request)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#######################
import cv2
import numpy as np
import matplotlib.pyplot as plt

I = cv2.imread('./imagenes/film_2/000819.jpg',cv2.IMREAD_GRAYSCALE)

I = np.float32(I)

'''
for i in range(I.shape[0]):
	for j in range(I.shape[1]):
		if I[i,j] < 200:
			I[i,j] = 0
'''
_,I = cv2.threshold(I,200,255,cv2.THRESH_BINARY)
kernel = np.ones((5,5),np.uint8)
I = cv2.erode(I,kernel,iterations = 2)
kernel = np.ones((4,4),np.uint8)
I = cv2.dilate(I,kernel, iterations = 2)

#plt.figure()
#plt.imshow(I, cmap = 'gray')

'''
######################################

_,threshold = cv2.threshold(I,200,255,cv2.THRESH_BINARY)
plt.figure()
plt.imshow(threshold, cmap = 'gray')

_,contours,_ = cv2.findContours(threshold, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

img2 = np.zeros(I.shape)
#img2 = img.copy()*0

for cnt in contours:
	rect = cv2.minAreaRect(cnt)
	# box = vertices del rect sent antihorario arranca
	# esquina superior derecha
	box = np.int0(cv2.boxPoints(rect))
	#filtro por area y long de los lados
	h,w = box[3,0]-box[0,0], box[3,1]-box[2,1]
	#print(h)
	if (int(h*w) > 500) and (h>30):
		cv2.drawContours(img2,[box],0,(255),3)
######################################

'''

corners = cv2.goodFeaturesToTrack(I,40,0.01,50)
corners = np.int0(corners)

for corner in corners:
	x, y = corner.ravel()
	cv2.circle(I, (x,y), 3, 150, -1)

plt.figure()
plt.imshow(I,cmap='gray')
plt.show()


# -*- coding: utf-8 -*-
"""
Created on Thu Jun 11 10:08:29 2015

Module to test and demonstrate the use of execution_gui

@author: albert
"""
from __future__ import absolute_import
import logging
import sys
import yaml
from wx import App
from time import sleep
from threading import Thread
from jetplex import __version__ as jetplex_version
from jetplex import __version__ as jetplex_revision
from jetplex import Fixture
from jetplex import Procedure
from jetplex import Execution
from jetplex import log_yaml_file
from jetplex import log_data
from jetplexwxgui.utils import get_path
from jetplexwxgui.progress_gui import ProgressFrame

# import wx

# from wx import __version__ as wx_version
# logging.info('wx version: %s', wx_version)

# from yaml import __version__ as yaml_version
# logging.info('pyYaml version: %s', yaml_version)

# from autopep8 import __version__ as autopep8_version
# logging.info('autopep8 version: %s', autopep8_version)

try:
    from _version import __version__ as jetplexwxgui_version
except ImportError:
    jetplexwxgui_version = "UNKNOWN"
try:
    from _version import __revision__ as jetplexwxgui_revision
except ImportError:
    jetplexwxgui_revision = "UNKNOWN"

YAML_FILE = get_path('testsample.yaml')
FX_FILE = get_path('fx.yaml')
FUNCS_TXT = """\
main branch:
- branch 1:
  - {get one: 1.0}
  - {get three: self.get_three}
- branch 2:
  - {get three: self.get_three}
  - {root path: 'self.step.path_list()[0]'}
  - {get fixture var: self.abc}
  - branch 3

branch 3:
  - {set var: self.set_var}
  - {get var: self.get_var}
"""
FUNCS = yaml.load(FUNCS_TXT)

class MyProcedure(Procedure):
    """demonstrate sequence"""
    def __init__(self, *args, **kwargs):
        """initialize"""
        super(MyProcedure, self).__init__(*args, **kwargs)
        self.var = None

    def get_three(self):
        """return three"""
        sleep(1)
        return 3.0

class MyExecution(Execution):
    def __init__(self, *args, **kwargs):
        """initialize"""
        super(MyExecution, self).__init__(*args, **kwargs)

    def functions(self):
        return {
            'step 1': (self.my_step, 1.0),
            'step 2': (self.my_step, 2.0),
            'step 3': (self.my_step, 3.0),
            'branch 3': [
                {'set var': (self.set_var, 5.0)},
                {'get var': self.get_var}
                ],
            'main branch': [
                {
                    'branch 1': [
                        {'get one': 1.0},
                        {'get three': self.get_three}
                        ]
                    },
                {
                    'branch 2': [
                        {'get three': self.get_three},
                        {'root path': self.step.path_list()[0]},
                        {'get fixture var': self.abc},
                        ]
                    }
                ]
            }

    def my_step(self, arg):
        sleep(1)
        return arg

class MyFixture(Fixture):
    """demonstrate fixture"""
    def __init__(self, *args, **kwargs):
        """initialize"""
        super(MyFixture, self).__init__(*args, **kwargs)

    def set_var(self, value):
        """set the var, which can be read by get_var"""
        logging.info('set_var @: %s', self.name)
        setattr(self, 'var', value)
        sleep(1)

    def get_var(self):
        """read var"""
        logging.info('get_var @: %s', self.name)
        sleep(1)
        return getattr(self, 'var')

def _main():
    """is the main function to execute when the module is executed"""
    # Setup logging
    logging.basicConfig(level=logging.INFO)
    logging.getLogger().setLevel(logging.INFO)
    # log version info
    logging.info('python version: %s', sys.version)
    logging.info('jetplex version: %s', jetplex_version)
    logging.info('jetplex revision: %s', jetplex_revision)
    logging.info('jetplexwxgui version: %s', jetplexwxgui_version)
    logging.info('jetplexwxgui revision: %s', jetplexwxgui_revision)
    # Log the functions
    log_data(FUNCS)
    # Log the fixture file
    log_yaml_file(FX_FILE)
    # Get fixture instance
    my_fixture = MyFixture(name='my fixture', yaml_file=FX_FILE)
    # get sequence specification
    name = 'pn11full'  # 'main branch'
    my_procedure = MyProcedure(name=name, yaml_file=YAML_FILE)
    # run the test to get sequence instance
    my_execution = MyExecution(my_procedure)
    thread = Thread(
        target=my_execution.run,
        args=(
            my_fixture,
            'myserialnumber',
            'mypartnumber',
            'mypartrevision',
            'mytesttype',
            )
        )
    thread.start()
    app = App(False)
    frame = ProgressFrame(my_execution)
    frame.Show()
#    test_db = TestDatabase()
#    print 'test database size:', len(test_db)
    app.MainLoop()
#    thread.join()
    # yaml_data = my_execution.get_y()
    # log_yaml(yaml_data, 'testinstance')

if __name__ == "__main__":
    _main()


import pygame
import sys

def run_game():
    pygame.init()

    screen=pygame.display.set_mode((1800,1200))

    image = pygame.image.load('images/ship.bmp')

    image_rect=image.get_rect()
    screen_rect=screen.get_rect()

    image_rect.centerx=screen_rect.centerx
    image_rect.bottom=screen_rect.bottom/2.0

    bg_color=(240,123,54)

    pygame.display.set_caption("Image on center")

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

            screen.fill(bg_color)
            screen.blit(image,image_rect)

            pygame.display.flip()

run_game()


import numpy as np
from math import pi, sin, cos, radians
from scipy.interpolate import interp1d
from scipy.signal.windows import hann
import matplotlib.pyplot as plt

from beagle_firmware import BeagleFirmware

# These paramters are expected to be mostly constant, fixed by competition
channel_depth = 5
source_depth = 4.5
fs = 96000
fc = 30000
T = 0.004  # pulse width, ms
c = 1480.  #speed of sound

# signal threshold value. Once this is exceeded we begin beamforming
p2_thresh = .025

# these will change all the time, and can be played with (within reason)
num_channels = 3
channel_coord = np.array([0, c / (2 * fc), c / fc])
receiver_depth = 4.5
receiver_range = 1  # range to channel at 0 coordinate point
receiver_bearing = -30 # bearing, degrees. Between -90 and 90

# add some noise to the signal. We can't anticipate what the tank will sound
# like in the background, so this is a coarse approximation of background
# interference
noise_level = 1e-5  # white noise variance. play around with this number

# each receiver will have a slighly different coordinates based on bearing and
# distance
dx = receiver_range + channel_coord * sin(radians(receiver_bearing))
dy = receiver_range + channel_coord * cos(radians(receiver_bearing))
rr = np.sqrt(dx ** 2 + dy ** 2)

# simulate first 3 in-plane arrivals
r_dir = np.sqrt(rr ** 2 + (receiver_depth - source_depth) ** 2)
r_surf = np.sqrt(rr ** 2 + (receiver_depth + source_depth) ** 2)
r_bottom = np.sqrt(rr ** 2 + (2 * channel_depth - source_depth) ** 2)

# time of arrival
time_dir = r_dir / c
time_surf = r_surf / c
time_bottom = r_bottom / c

# time axis of simulated recorded signal
taxis = np.arange(2 ** 13) / fs
taxis += time_dir[0] - 300 / fs  # small offset for plotting purposes

# transmitted signal
tsig = np.arange(np.ceil(T * fs)) / fs
sig_xmitt = np.sin(2 * pi * fc * tsig)
sig_xmitt *= hann(tsig.size)
# create an interpolator, this allows for sub sample timing of arrivals
sig_ier = interp1d(tsig, sig_xmitt, kind=3, bounds_error=False, fill_value=0.)

# create time series as a sum of all arrivals
x_sig = np.zeros((taxis.size, num_channels), dtype=np.float_)
x_sig += sig_ier(taxis[:, None] - time_dir) / r_dir
x_sig -= sig_ier(taxis[:, None] - time_surf) / r_surf
x_sig -= sig_ier(taxis[:, None] - time_bottom) / r_bottom

# create some noise on all channels
x_sig += np.random.randn(*x_sig.shape) * np.sqrt(noise_level)

nbd = BeagleFirmware(fc)

# compute output on each channel as though it came from the hydrophones
starti = 0
buffer_size = nbd.buffer_size
num_buffer = int(np.floor(x_sig.shape[0] / buffer_size))
buff_i = np.arange(num_buffer) * buffer_size

out_data = []
for i in buff_i:
    p_atfc = nbd.process(x_sig[i: i + buffer_size, :])
    out_data.append(np.concatenate(p_atfc))

out_data = np.concatenate(out_data)

# scale the data to V peak
out_data = np.sqrt(2) * np.sqrt(2 * np.abs(out_data) ** 2 / np.sum(nbd.window) ** 2)
win_time = np.arange(out_data.shape[0]) * nbd.num_step / fs + taxis[0]

fig, ax = plt.subplots()
ax.plot(taxis * 1e3, x_sig)
ax.plot([-1e4, 1e4], [1, 1], color='C3', label='clipping threshold')
ax.plot([-1e4, 1e4], [-1, -1], color='C3')
ax.grid()
ax.legend()
ax.set_xlim(taxis[0] * 1e3, taxis[-1] * 1e3)
ax.set_xlabel('time, (ms)')
ax.set_ylabel('pressure')
ax.set_title('simulated pressure arrival')

fig, ax = plt.subplots()
ax.plot(win_time * 1e3, out_data)
ax.plot([-1e4, 1e4], [p2_thresh, p2_thresh], 'k', label='detection threshold')
ax.plot([-1e4, 1e4], [1, 1], color='C3', label='clipping threshold')
ax.grid()
ax.legend()
ax.set_xlim(win_time[0] * 1e3, win_time[-1] * 1e3)
ax.set_xlabel('time, (ms)')
ax.set_ylabel('$|p|^2$')
ax.set_title('arrival energy at {:d} kHz, time series'.format(int(fc / 1e3)))

plt.show(block=False)


from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

def montecarlo(datos1,  datos2, alfa, N):
    '''Recibe un arreglo de datos y retorna un
    intervalo de confianza al 100*(1-alfa)% usando
    una simulacion de montecarlo'''

    flujo1 = datos1[0]
    error1 = datos1[1]
    flujo2 = datos2[0]
    error2 = datos2[1]
    a = np.zeros(N)
    b = np.zeros(N)
    for j in range(N):
        aux = np.random.normal(0, 1, size=len(flujo1))
        muestra1 = flujo1 + error1 * aux
        muestra2 = flujo2 + error2 * aux
        b[i], a[i] = np.polyfit(muestra1, muestra2, 1)
    b = np.sort(b)
    a = np.sort(a)
    liminfa = a[int(N * alfa)]
    limsupa = a[int(N * (1 - alfa))]
    liminfb = b[int(N * alfa)]
    limsupb = b[int(N * (1 - alfa))]
    return liminfa, limsupa, liminfb, limsupb

datos = (3.631 *
         np.loadtxt('data/DR9Q.dat', usecols=(80, 81, 82, 83)))
a = datos[:, 0]
b = datos[:, 1]
c = datos[:, 2]
d = datos[:, 3]
datosI = np.array([a, b])
datosZ = np.array([c, d])
polyfit = np.polyfit(datosI[0], datosZ[0], 1)
y = np.zeros(len(datosI[0]))
k = 0
for i in datosI[0]:
    y[k] = polyfit[0] * i + polyfit[1]
    k += 1
fig3 = plt.figure(3)
fig3.clf()
plt.plot(datosI[0], y, 'b-')
plt.plot(datosI[0], datosZ[0], 'r*')
plt.xlabel('Flujo banda I ($1e-6 \ Jy$)')
plt.ylabel(r'Flujo banda Z ($1e-6 \ Jy$)')
plt.title(r'Grafico flujo banda I vs flujo banda Z')
plt.grid(True)

plt.show()

alfa = 0.05
N = 10000
liminfa, limsupa, liminfb, limsupb = montecarlo(datosI, datosZ, alfa, N)
print('Parametro A = ')
print(polyfit[0])
print('Parametro B = ')
print(polyfit[1])
print('El limite inferior para  A ')
print(str(liminfa))
print('limite suprior')
print(limsupa)
print('El limite inferior para  B ')
print(liminfb)
print('limite suprior')
print(limsupb)


"""
Edit a ticket.

Edit a SoftLayer ticket. The edit method is two-fold. You may either edit a ticket itself,
add an update to a ticket, attach up to two files to a ticket, or perform all of these tasks.
The SoftLayer API ignores changes made to the userEditableFlag and accountId properties.
You may not assign a ticket to a user that your API account does not have access to.
You may not enter a custom title for standard support tickets, buy may do so when editing an administrative ticket.
Finally, you may not close a ticket using this method. Please contact SoftLayer if you need a ticket closed.
If you need to only add an update to a ticket then please use the addUpdate method in this service.
Likewise if you need to only attach a file to a ticket then use the addAttachedFile method.
The edit method exists as a convenience if you need to perform all these tasks at once.

Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Ticket/edit
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Ticket

License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""

import SoftLayer
import pprint

# Your SoftLayer user-name.
USERNAME = 'set me'
# Generate one at https://manage.softlayer.com/Administrative/apiKeychain
API_KEY = 'set me'

# The ticket id used to edited.
ticketId = 21528750
# The value used as example for editing the ticket.
subjectId = 1002
# The value used as example for editing the ticket.
content = 'Editing ticket subjectId(i.e. the group to which it belongs)'

# Build a skeleton SoftLayer_Ticket
# containing the ticket specification values.
ticket = {
    'subjectId': subjectId
}

# Declare the API client
client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
ticketService = client['SoftLayer_Ticket']

try:
    response = ticketService.edit(ticket, content, id=ticketId)
    pprint.pprint(response)
except SoftLayer.SoftLayerAPIError as e:
    print("Unable to edit the ticket ID: %s \nFault code: %s \nFault string: %s"
        % (str(ticketId), e.faultCode, e.faultString))


import pdb
import time
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
import torch.utils.model_zoo as model_zoo
import torch.optim as optim
from torch.autograd import Variable
import numpy as np
from src.codebase.utils.utils import log
from src.codebase_pytorch.utils.hooks import print_outputs, print_grads
from src.codebase_pytorch.utils.scheduler import ReduceLROnPlateau
from src.codebase_pytorch.utils.scheduler import LambdaLR
from src.codebase_pytorch.models.model import Model

__all__ = ['SqueezeNet', 'squeezenet1_0', 'squeezenet1_1']

model_urls = {
    'squeezenet1_0': 'https://download.pytorch.org/models/squeezenet1_0-a815701f.pth',
    'squeezenet1_1': 'https://download.pytorch.org/models/squeezenet1_1-f364aa15.pth',
}

model_paths = {
    'squeezenet1_0': '/n/regal/rush_lab/awang/models/squeezenet_v1_0.ckpt',
    'squeezenet1_1': '/n/regal/rush_lab/awang/models/squeezenet_v1_1.ckpt'
}

class Fire(nn.Module):

    def __init__(self, inplanes, squeeze_planes,
                 expand1x1_planes, expand3x3_planes):
        super(Fire, self).__init__()
        self.inplanes = inplanes
        self.squeeze = nn.Conv2d(inplanes, squeeze_planes, kernel_size=1)
        self.squeeze_activation = nn.ReLU(inplace=True)
        self.expand1x1 = nn.Conv2d(squeeze_planes, expand1x1_planes,
                                   kernel_size=1)
        self.expand1x1_activation = nn.ReLU(inplace=True)
        self.expand3x3 = nn.Conv2d(squeeze_planes, expand3x3_planes,
                                   kernel_size=3, padding=1)
        self.expand3x3_activation = nn.ReLU(inplace=True)

    def forward(self, x):
        x = self.squeeze_activation(self.squeeze(x))
        return torch.cat([
            self.expand1x1_activation(self.expand1x1(x)),
            self.expand3x3_activation(self.expand3x3(x))
        ], 1)

class SqueezeNet(Model):

    def __init__(self, version=1.0, num_classes=1000, use_cuda=0):
        super(SqueezeNet, self).__init__()
        if version not in [1.0, 1.1]:
            raise ValueError("Unsupported SqueezeNet version {version}:"
                             "1.0 or 1.1 expected".format(version=version))
        self.num_classes = num_classes
        self.use_cuda = use_cuda

        if version == 1.0:
            self.features = nn.Sequential(
                nn.Conv2d(3, 96, kernel_size=7, stride=2),
                nn.ReLU(inplace=True),
                nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                Fire(96, 16, 64, 64),
                Fire(128, 16, 64, 64),
                Fire(128, 32, 128, 128),
                nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                Fire(256, 32, 128, 128),
                Fire(256, 48, 192, 192),
                Fire(384, 48, 192, 192),
                Fire(384, 64, 256, 256),
                nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                Fire(512, 64, 256, 256),
            )
        else:
            self.features = nn.Sequential(
                nn.Conv2d(3, 64, kernel_size=3, stride=2),
                nn.ReLU(inplace=True),
                nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                Fire(64, 16, 64, 64),
                Fire(128, 16, 64, 64),
                nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                Fire(128, 32, 128, 128),
                Fire(256, 32, 128, 128),
                nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                Fire(256, 48, 192, 192),
                Fire(384, 48, 192, 192),
                Fire(384, 64, 256, 256),
                Fire(512, 64, 256, 256),
            )
        # Final convolution is initialized differently form the rest
        final_conv = nn.Conv2d(512, self.num_classes, kernel_size=1)
        self.classifier = nn.Sequential(
            nn.Dropout(p=0.5),
            final_conv,
            nn.ReLU(inplace=True),
            nn.AvgPool2d(13)
        )

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                if m is final_conv:
                    init.normal(m.weight.data, mean=0.0, std=0.01)
                else:
                    init.kaiming_uniform(m.weight.data)
                if m.bias is not None:
                    m.bias.data.zero_()

    def forward(self, x):
        x = self.features(x)
        x = self.classifier(x)
        return x.view(x.size(0), self.num_classes)

    def train_model(self, args, tr_data, val_data, fh):
        '''
        Train the model according to the parameters specified in args.
        '''
        self.train()
        lr = args.lr
        if args.optimizer == 'sgd':
            optimizer = optim.SGD(self.parameters(), lr=lr, momentum=args.momentum, weight_decay=args.weight_decay)
            log(fh, "\tOptimizing with SGD with learning rate %.3f" % lr)
        elif args.optimizer == 'adam':
            optimizer = optim.Adam(self.parameters(), lr=lr)
            log(fh, "\tOptimizing with adam with learning rate %.3f" % lr)
        elif args.optimizer == 'adagrad':
            optimizer = optim.Adagrad(self.parameters(), lr=lr,
                    weight_decay=args.weight_decay)
            log(fh, "\tOptimizing with adagrad with learning rate %.3f" % lr)
        else:
            raise NotImplementedError
        scheduler = ReduceLROnPlateau(optimizer,
                'max', factor=.5, patience=3, threshold=1e-1)
        #lr_lambda = lambda epoch: (1 - float(epoch) / args.n_epochs)
        #scheduler = LambdaLR(optimizer, lr_lambda)

        if args.load_model_from:
            self.load_state_dict(torch.load(args.load_model_from))

        start_time = time.time()
        val_loss, val_acc = self.evaluate(val_data)
        best_loss, best_acc = val_loss, val_acc
        last_acc = val_acc
        log(fh, "\tInitial val loss: %.3f, \tval acc: %.2f \t(%.3f s)" %
                (val_loss, val_acc, time.time() - start_time))
        if args.save_model_to:
            torch.save(self.state_dict(), args.save_model_to)
            log(fh, "\t\tSaved model to %s" % args.save_model_to)

        for epoch in xrange(args.n_epochs):
            log(fh, "\tEpoch %d, \tlearning rate: %.3f" % (epoch+1, scheduler.get_lr()[0]))
            total_loss, total_correct = 0., 0.
            start_time = time.time()
            for batch_idx in xrange(tr_data.n_batches):
                ins, targs = tr_data[batch_idx]
                if self.use_cuda:
                    ins, targs = ins.cuda(), targs.cuda()
                ins, targs = Variable(ins), Variable(targs)
                optimizer.zero_grad()
                outs = self(ins)
                loss = F.cross_entropy(outs, targs)
                total_loss += loss.data[0]
                preds = outs.data.max(1)[1]
                total_correct += preds.eq(targs.data).cpu().sum()
                loss.backward()
                optimizer.step()

            val_loss, val_acc = self.evaluate(val_data)
            log(fh, "\t\tTraining loss: %.3f \taccuracy: %.2f"
                    % (total_loss / tr_data.n_batches,
                        100. * total_correct / tr_data.n_ins))
            log(fh, "\t\tVal loss: %.3f \taccuracy: %.2f \t(%.3f s)"
                    % (val_loss, val_acc, time.time()-start_time))
            if val_acc > best_acc:
                if args.save_model_to:
                    torch.save(self.state_dict(), args.save_model_to)
                    log(fh, "\t\tSaved model to %s" % args.save_model_to)
                best_acc = val_acc
            if abs(last_acc - val_acc) < .01:
                #pdb.set_trace()
                #self.final_conv.register_forward_hook(print_outputs)
                #self.final_conv.register_backward_hook(print_grads)
                pass
            last_acc = val_acc
            scheduler.step(epoch)

        if args.save_model_to:
            self.load_state_dict(torch.load(args.save_model_to))
        _, val_acc = self.evaluate(val_data)
        log(fh, "\tFinished training in %.3f s, \tBest validation accuracy: %.2f" %
                (time.time() - start_time, val_acc))

    def evaluate(self, data):
        '''
        Evaluate model on data, usually either validation or test
        '''
        self.eval()
        total_loss, total_correct = 0., 0.
        for batch_idx in xrange(data.n_batches):
            ins, targs = data[batch_idx]
            if self.use_cuda:
                ins, targs = ins.cuda(), targs.cuda()
            ins, targs = Variable(ins, volatile=True), Variable(targs)
            outs = self(ins)
            total_loss += F.cross_entropy(outs, targs, size_average=False).data[0]
            preds = outs.data.max(1)[1]
            total_correct += preds.eq(targs.data).cpu().sum()
        return total_loss / data.n_ins, \
                100. * total_correct / data.n_ins

    def predict(self, data):
        '''
        Get predictions for data
        '''
        self.eval()
        predictions = []
        for batch_idx in xrange(data.n_batches):
            ins, targs = data[batch_idx]
            if self.use_cuda:
                ins, targs = ins.cuda(), targs.cuda()
            ins, targs = Variable(ins, volatile=True), Variable(targs)
            outs = self(ins)
            predictions.append(outs.data.max(1)[1].cpu().numpy())
        return np.vstack(predictions)

    def get_gradient(self, ins, targs):
        self.eval()
        if self.use_cuda:
            ins, targs = ins.cuda(), targs.cuda()
        ins, targs = Variable(ins, requires_grad=True), Variable(targs)
        outs = self(ins)
        loss = F.cross_entropy(outs, targs)
        loss.backward(retain_variables=True)
        return ins.grad.data.cpu().numpy()

def squeezenet1_0(pretrained=False, **kwargs):
    r"""SqueezeNet model architecture from the `"SqueezeNet: AlexNet-level
    accuracy with 50x fewer parameters and <0.5MB model size"
    <https://arxiv.org/abs/1602.07360>`_ paper.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = SqueezeNet(version=1.0, **kwargs)
    if pretrained:
        #model.load_state_dict(model_zoo.load_url(model_urls['squeezenet1_0']))
        model.load_state_dict(torch.load(model_paths['squeezenet1_0']))
    return model

def squeezenet1_1(pretrained=False, **kwargs):
    r"""SqueezeNet 1.1 model from the `official SqueezeNet repo
    <https://github.com/DeepScale/SqueezeNet/tree/master/SqueezeNet_v1.1>`_.
    SqueezeNet 1.1 has 2.4x less computation and slightly fewer parameters
    than SqueezeNet 1.0, without sacrificing accuracy.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = SqueezeNet(version=1.1, **kwargs)
    if pretrained:
        #model.load_state_dict(model_zoo.load_url(model_urls['squeezenet1_1']))
        model.load_state_dict(torch.load(model_paths['squeezenet1_1']))
    return model