# from bunch import Bunch
import logging

import numpy as np
import pandas as pd
from sklearn.base import TransformerMixin
from sklearn.impute import SimpleImputer
from sklearn.impute._base import _BaseImputer
from sklearn.utils.validation import check_array, check_is_fitted

import tigerml.core.dataframe as td
from tigerml.core.utils import (
    DictObject,
    flatten_list,
    get_cat_cols,
    get_num_cols,
)

from .scripts.Mice_Impute import MiceImputer

_LOGGER = logging.getLogger(__name__)

def _check_X(X, columns=None):
    """Change array to DataFrame."""
    if isinstance(X, (pd.DataFrame, td.DataFrame)):
        return X
    elif isinstance(X, (np.ndarray)):
        if columns is None:
            return pd.DataFrame(X)
        else:
            return pd.DataFrame(X, columns=columns)
    elif isinstance(X, (pd.Series, td.Series)):
        return pd.DataFrame(X, columns=X.name)
    elif isinstance(X, (list, tuple)):
        X = np.array(X)
        return pd.DataFrame(X, columns=[str(i) for i in range(X.shape[1])])
    elif hasattr(X, ("__array__")):
        data = X.__array__()
        return pd.DataFrame(data, columns=[str(i) for i in range(data.shape[1])])
    return X

class Imputer(_BaseImputer, TransformerMixin):
    """Imputer class.

    Parameters
    ----------
    num_impute_method: str, default median
        numerical imputation method, {'mean', 'median', 'mode'}
    cat_impute_method: str, default mode
        categorical imputation method, {'mode', 'constant'}
    constant_value: str, default missing
        categorical constant to be used when the cat_impute_method is constant

    Attributes
    ----------
    imputed_data: pd.DataFrame
        imputed data frame
    imputation_summary: dict
        imputations summary with no of missing and
        method used for imputation for each column

    Examples
    --------
    >>> import numpy as np
    >>> from tigerml.core.preprocessing.imputer import Imputer
    >>> imp = Imputer()
    >>> imp.fit([[7, 2, 3], [4, np.nan, 6], [10, 5, 9]])
    Imputer()
    >>> X = [[np.nan, 2, 3], [4, np.nan, 6], [10, np.nan, 9]]
    >>> print(imp.transform(X))
    [[ 7.   2.   3. ]
     [ 4.   3.5  6. ]
     [10.   3.5  9. ]]
    """

    MEAN = "mean"
    MEDIAN = "median"
    MODE = "mode"
    CONSTANT = "constant"
    REGRESSION = "regression"

    METHODS = DictObject(
        {
            MEAN: "mean",
            MEDIAN: "median",
            MODE: "mode",
            CONSTANT: "constant",
            REGRESSION: "regression",
        }
    )

    def __init__(
        self,
        missing_values=np.nan,
        add_indicator=False,
        num_impute_method="median",
        cat_impute_method="mode",
        constant_value="missing",
    ):
        if (
            num_impute_method not in self.METHODS
            or cat_impute_method not in self.METHODS
        ):
            raise Exception(
                "Supported imputation methods: {}".format(self.METHODS.keys())
            )
        super().__init__(missing_values=missing_values, add_indicator=add_indicator)
        self.num_impute_method = num_impute_method
        self.cat_impute_method = cat_impute_method
        self.constant_value = constant_value

    @property
    def num_cols(self):
        """Returns list of numeric columns."""
        return list(set(get_num_cols(self.data_)) - set(self.drop_cols_))

    @property
    def cat_cols(self):
        """Returns list of categorical columns."""
        return list(set(get_cat_cols(self.data_)) - set(self.drop_cols_))

    def add_imputation_rule(
        self,
        cols,
        impute_method,
        constant_value="missing",
        ind_cols=[],
        ind_impute_method=None,
    ):
        """Adds imputation rule."""
        cols_already_applied = [
            col
            for col in cols
            if any([col in rule["cols"] for rule in self.imputation_rules_])
        ]
        if cols_already_applied:
            raise Exception(
                "Imputation rule already applied " "for {}".format(cols_already_applied)
            )
        if [col for col in cols if col not in self.data_]:
            raise Exception(
                "Columns not present in data - {}".format(
                    [col for col in cols if col not in self.data_]
                )
            )
        if impute_method not in self.METHODS:
            raise Exception(
                "Supported imputation methods: {}".format(self.METHODS.keys())
            )
        self.imputation_rules_.append(
            {
                "cols": cols,
                "impute_method": impute_method,
                "ind_cols": ind_cols,
                "ind_impute_method": ind_impute_method,
                "constant_value": constant_value,
            }
        )
        return self

    def fit(self, data, y=None):
        """Fits imputer."""
        cols = None
        if hasattr(data, "columns"):
            cols = data.columns.tolist()
        check_array(
            data,
            accept_large_sparse=False,
            dtype=object,
            force_all_finite="allow-nan",
            copy=True,
        )
        data = _check_X(data, cols)
        self.features_ = data.columns.tolist()
        self.drop_cols_ = []
        self.imputation_rules_ = []
        self.imputation_summary_ = {}
        self.imputer_ = {}
        self.imputed_data_ = data
        self.data_ = data
        imputed_cols = []
        from tigerml.core.utils import compute_if_dask

        for col in self.imputed_data_.columns:
            if compute_if_dask(
                self.imputed_data_[col].isna().sum() == len(self.imputed_data_)
            ):
                self.imputed_data_ = self.imputed_data_.drop(col, axis=1)
                self.drop_cols_.append(col)
                _LOGGER.info(
                    "All the values of {} are missing. "
                    "Dropping the column.".format(col)
                )
                self.imputation_summary_.update(
                    {
                        col: {
                            "no_of_missing": "All values",
                            "method": "Dropped the column",
                        }
                    }
                )

        if len(self.imputation_rules_) > 0:
            for method in self.imputation_rules_:
                if method["impute_method"] == "regression":
                    if not method["ind_impute_method"] is None:
                        impute_method = method["ind_impute_method"]
                    else:
                        impute_method = self.num_impute_method
                    if len(method["ind_cols"]) > 0:
                        df = pd.DataFrame(self.data_[method["ind_cols"]])
                    else:
                        df = self.data_[self.num_cols]
                    for col in method["cols"]:
                        self.imputer_[col] = self.impute_by_regression(
                            self.data_[col], df, impute_method
                        )
                else:
                    impute_method = method["impute_method"]
                    cols = method["cols"]
                    if all([col in self.num_cols for col in cols]):
                        for col in cols:
                            self.imputer_[col] = self.impute_num(
                                self.data_[col], impute_method=impute_method
                            )
                    elif all([col in self.cat_cols for col in cols]):
                        for col in cols:
                            self.imputer_[col] = self.impute_cat(
                                self.data_[col],
                                impute_method=impute_method,
                                missing_str=method["constant_value"],
                            )
                    elif impute_method == "mode":
                        for col in cols:
                            if col in self.num_cols:
                                self.imputer_[col] = self.impute_num(
                                    self.data_[col], impute_method=impute_method
                                )
                            else:
                                self.imputer_[col] = self.impute_cat(
                                    self.data_[col],
                                    impute_method=impute_method,
                                    missing_str=method["constant_value"],
                                )
                    else:
                        raise Exception(
                            "numeric and categorical columns cannot "
                            "be imputed together with {}".format(impute_method)
                        )

        imputed_cols += list(
            set(flatten_list([rule["cols"] for rule in self.imputation_rules_]))
        )
        for col in set(self.num_cols) - set(imputed_cols):
            self.imputation_rules_.append(
                {
                    "cols": [col],
                    "impute_method": self.num_impute_method,
                    "ind_cols": None,
                    "ind_impute_method": None,
                    "constant_value": None,
                }
            )
            self.imputer_[col] = self.impute_num(
                self.data_[col], impute_method=self.num_impute_method
            )
        for col in set(self.cat_cols) - set(imputed_cols):
            self.imputation_rules_.append(
                {
                    "cols": [col],
                    "impute_method": self.cat_impute_method,
                    "ind_cols": None,
                    "ind_impute_method": None,
                    "constant_value": self.constant_value,
                }
            )
            self.imputer_[col] = self.impute_cat(
                self.data_[col],
                impute_method=self.cat_impute_method,
                missing_str=self.constant_value,
            )
        self.is_fitted_ = True

        non_imputed_cols = set(self.imputed_data_.columns) - set(self.imputer_.keys())
        if len(non_imputed_cols) > 0:
            for col in non_imputed_cols:
                no_of_missing = self.imputed_data_[col].isnull().sum().sum()
                _LOGGER.info(
                    "Column '{}' is skipped for imputation as its "
                    "not recognised as either numerical or categorical "
                    "data type.".format(col)
                )
                self.imputation_summary_.update(
                    {
                        col: {
                            "no_of_missing": no_of_missing,
                            "imputed with": "Not Imputed",
                        }
                    }
                )
        return self

    def transform(self, data):
        """Returns imputed data values."""
        cols = None
        if hasattr(data, "columns"):
            cols = data.columns.tolist()
        check_is_fitted(self, "is_fitted_")
        check_array(
            data, accept_large_sparse=False, dtype=object, force_all_finite="allow-nan"
        )
        data = _check_X(data, cols)
        if data.shape[1] != self.data_.shape[1]:
            raise ValueError(
                "The number of features {} in transform is different "
                "from the number of features {} in fit.".format(
                    data.shape[1], self.data_.shape[1]
                )
            )
        data_ = data.copy()
        imputed_data_ = data.copy()
        cols = list(self.imputer_.keys())
        for col in cols:
            if col not in self.drop_cols_:
                no_of_missing = data_[col].isnull().sum().sum()
                impute_method = [
                    rule for rule in self.imputation_rules_ if col in rule["cols"]
                ][0]["impute_method"]
                imputed_data_[col] = self.imputer_[col].transform(
                    data_[col].to_numpy().reshape(-1, 1)
                )
                self.imputation_summary_.update(
                    {
                        col: {
                            "no_of_missing": no_of_missing,
                            "imputed with": impute_method,
                        }
                    }
                )
        return imputed_data_.values

    def _more_tags(self):
        """Returns tag to allow nan for string and 2d array dtypes."""
        return {"allow_nan": True, "X_types": ["2darray", "string"]}

    def get_imputed_data(self):
        """Returns imputed data."""
        return self.imputed_data_

    def get_feature_names(self):
        """Returns list of feature names."""
        return self.features_

    def get_imputation_method(self, col_name):
        """Returns imputation method."""
        if [rule for rule in self.imputation_rules_ if col_name in rule["cols"]]:
            return [
                rule for rule in self.imputation_rules_ if col_name in rule["cols"]
            ][0]["impute_method"]
        else:
            return None

    @staticmethod
    def impute_num(feature, impute_method="mean", constant_value=None):
        """Imputing numerical data.

        This method will impute the missing values in a continuous variable.
        This transformation will impute in inplace.

        Parameters
        ----------
          feature : str
            Name of the numeric variable to be encoded.
          impute_method : int
            Default is 'mean'. Other options include - 'mode' and 'median'.df[]

        Returns
        -------
           fitted object : SimpleImputer fitted python object
        """
        if isinstance(feature, (pd.DataFrame, pd.Series, td.DataFrame, td.Series)):
            feature = feature.to_numpy()
        elif isinstance(feature, (np.ndarray)):
            pass
        else:
            raise ValueError("Pass either numpy or pd or td data")
        if impute_method == "median":
            imp = SimpleImputer(strategy="median")
            imp.fit(feature.reshape(-1, 1))
        elif impute_method == "mode":
            imp = SimpleImputer(strategy="most_frequent")
            imp.fit(feature.reshape(-1, 1))
        elif impute_method == "mean":
            imp = SimpleImputer(strategy="mean")
            imp.fit(feature.reshape(-1, 1))
        else:
            raise Exception(
                'Supported values for "impute_method" are - mean, median, mode'
            )
        return imp

    @staticmethod
    def impute_cat(feature, impute_method="constant", missing_str="missing"):
        """Impute Categorical data.

        This method will impute the missing values in a categorical variable.
        This transformation will impute in inplace.

        Parameters
        ----------
          feature : str
            Name of the category variable to be encoded.
          impute_method : str
            Default is 'constant'. Other options include - 'mode'.
          missing_str : str
            Default is 'missing'. Value that you
            want impute in the missing values.

        Returns
        -------
           fitted object : SimpleImputer fitted python object
        """
        assert str(feature.dtype) == "category" or not np.issubdtype(
            feature.dtype, np.number
        ), (
            "feature is not Categorical, imputeMissingValCat "
            "can impute only Categorical variables"
        )

        if isinstance(feature, (pd.DataFrame, pd.Series, td.DataFrame, td.Series)):
            feature = feature.to_numpy()
        elif isinstance(feature, (np.ndarray)):
            pass
        else:
            raise ValueError("Pass either numpy or pd or td data")

        if impute_method == "constant":
            imp = SimpleImputer(strategy="constant", fill_value=missing_str)
            imp.fit(feature.reshape(-1, 1))
        elif impute_method == "mode":
            imp = SimpleImputer(strategy="most_frequent")
            imp.fit(feature.reshape(-1, 1))
        else:
            raise Exception('Incorrect input. Should be either "constant" or "mode".')
        return imp

    @staticmethod
    def impute_by_regression(target, df, impute_method="mean"):
        """Impute by regression.

        This method will impute the missing values of the target variable
        by fitting linear/logistic equation.
        Unlike MICE, this method will create single equation for the
        target variable.
        This equations are either linear or logistic equations.

        Parameters
        ----------
          target : str
            variable for which imputation has to be performed.
          group : list
            independent variables with which missing values have to be estimated.
          impute_method : str
            Default is 'Mean'. Strategy on how to impute missing
            values in independent variables of the equation.

        Return
        ------
           fitted object : SimpleImputer fitted python object
        """
        if target.name in df.columns:
            df = df[~target.name]
        reg_imp = MiceImputer(seed_strategy=impute_method, target=target.name, group=[])
        reg_imp.fit(pd.concat([df, target], axis=0))
        return reg_imp


from .modules import ThreadCreator
from .utils import emit

class Customer(ThreadCreator):
    target = 'customer'
    def initialize(self, name):
        def customer_func():
            self.trigger('enter', name=name)
            # Do not block thread activity.
            can_seat = self.seat_semaphore.acquire(blocking=False)
            if not can_seat:
            # Exit the thread because the seat is full.
                self.trigger('full', name=name)
            else:
                # Signal barber that a customer has been added into waiting queue.
                self.customer_queue.put(name)
                self.customer_semaphore.release()
                self.trigger('waiting', name=name)
                self.barber_semaphore.acquire()
                self.trigger('serving', name=name)
        return customer_func


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

import wave

import pyaudio

FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 50
WAVE_OUTPUT_FILENAME = "teste.wav"

audio = pyaudio.PyAudio()

def main():
    # começa a gravação

    stream = audio.open(format=FORMAT, channels=CHANNELS,
                        rate=RATE, input=True,
                        frames_per_buffer=CHUNK)
    frames = []

    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        data = stream.read(CHUNK)
        frames.append(data)

    # termina a gravação

    stream.stop_stream()
    stream.close()
    audio.terminate()

    waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
    waveFile.setnchannels(CHANNELS)
    waveFile.setsampwidth(audio.get_sample_size(FORMAT))
    waveFile.setframerate(RATE)
    waveFile.writeframes(b''.join(frames))
    waveFile.close()

if __name__ == "__main__":
    main()


#!/usr/bin/env python

"""
This script looks into a working directory and
runs/checks/removes any jobs inside that directory.
If a job(jsonlike) is found but no jobdir present, script will submit to queue.
If a job is found and a jobdir is present:
  - if results are found, job was successful.
      - copy and upload log file (redundant but might be useful to have at toplevel)
      - tar, copy and upload results
      - tar and copy results to local results dir
      - remove job/log/results from working directory.
  - if no results found:
    - if job log is "done", job failed.
      - copy and upload log file
      - copy and upload job file
      - tar and copy results directory to local results dir for debug
      - remove job/log/results from working directory
    - if job log is not "done", job is still running.
      - copy and upload current progress (log)
"""
import argparse
import os
import glob
import shutil
import errno
import subprocess
from qtools import Submitter
import tarfile
import boto3
import botocore
import logging
import time
import sys
import re

import download_rawdata as dr

__version__ = '0.0.1'

PROFILE = 'ncrcrg.u19@gmail.com'

def make_tarfile(output_filename, source_dir, logger):
    """
    Generates tar.gz file from source_dir.

    :param output_filename: basestring
        typically ${SRC}.tar.gz
    :param source_dir: basestring
        the directory to be tarballed
    :param logger: logging.Logger()
        Logger object
    :return:
    """
    logger.info("Making tar file (may take awhile..)")
    if not os.path.exists(output_filename):
        logger.info("{} doesn't exist, generating tar file.".format(output_filename))
        with tarfile.open(output_filename, "w:gz") as tar:
            tar.add(source_dir, arcname=os.path.basename(source_dir))
    else:
        logger.info("{} exists, will not overwrite.".format(output_filename))
    logger.info("Done writing tar file ({})".format(output_filename))
    logger.info("Sleeping for 15s.")
    time.sleep(15)

def remove(to_remove, logger):
    """
    Removes a directory or file using shutil or os.

    :param to_remove: basestring
        the directory/file to remove
    :return:
    """
    try:
        logger.info("Removing {}".format(to_remove))
        shutil.rmtree(to_remove)
    except OSError:
        logger.info("Removing {}".format(to_remove))
        os.remove(to_remove)

def remove_file(to_remove, logger):
    """
    Remove a single file using os.remove().

    :param to_remove: basestring
    :param logger:
    :return:
    """

    logger.info("Removing {}".format(to_remove))
    if os.path.exists(to_remove):
        os.remove(to_remove)
    else:
        logger.warning("Warning, {} does not exist (and so will not remove)".format(to_remove))
    time.sleep(1)

def main():
    """
    Main program.
    """
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--project_dir",
        required=False,
        type=str,
        default="/projects/ps-yeolab4/NCRCRG"
    )
    parser.add_argument(
        "--user_dir",
        required=False,
        type=str,
        default="home"
    )
    parser.add_argument(
        "--results_dir",
        required=False,
        type=str,
        default="results_dir"
    )
    parser.add_argument(
        "--log_dir",
        required=False,
        type=str,
        default="logs"
    )
    parser.add_argument(
        "--bucket",
        required=False,
        type=str,
        default="u19"
    )

    # Process args
    args = parser.parse_args()
    project_dir = args.project_dir
    bucket = args.bucket
    work_dir = args.work_dir
    results_dir = args.results_dir

    log_dir = os.path.join(project_dir, args.log_dir)
    user_dir = os.path.join(project_dir, args.user_dir)

    logger = logging.getLogger('status')
    logger.setLevel(logging.INFO)

    ih = logging.FileHandler(os.path.join(log_dir, dr.append_date('status.txt')))
    ih.setLevel(logging.INFO)
    logger.addHandler(ih)

    eh = logging.FileHandler(os.path.join(log_dir, dr.append_date('status.err')))
    eh.setLevel(logging.ERROR)
    logger.addHandler(eh)

    wh = logging.FileHandler(os.path.join(log_dir, dr.append_date('status.txt')))
    wh.setLevel(logging.WARNING)
    logger.addHandler(wh)

    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    ih.setFormatter(formatter)
    eh.setFormatter(formatter)
    logger.info("starting program")

    users = glob.glob(os.path.join(user_dir, '*'))
    for user in users:
        user_work_dir = os.path.join(user, work_dir)
        user_results_dir = os.path.join(user, results_dir)

if __name__ == "__main__":
    main()


import os
from flask import Flask,session,flash, render_template,redirect, url_for, json, request,jsonify
import random
from flaskext.mysql import MySQL
from flask_cors import CORS, cross_origin
from werkzeug.utils import secure_filename
#from flask_debugtoolbar import DebugToolbarExtension

app = Flask(__name__,static_folder='static', static_url_path='/static')

APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = os.path.join(APP_ROOT,'static/img/cats')
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

CORS(app, origins='*')
mysql = MySQL(app)
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'bfc1f7492709c0'
app.config['MYSQL_DATABASE_PASSWORD'] = '42121e97'
app.config['MYSQL_DATABASE_DB'] = 'ad_1fde3f7e20bc37f'
app.config['MYSQL_DATABASE_HOST'] = 'us-cdbr-iron-east-03.cleardb.net'
mysql.init_app(app)

#FUNÇÃO PARA VERIFICAR SE O ARQUIVO É VALIDO
def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
#-------------------------------------------------------------------------------------------------------------
#FUNÇÃO PARA LISTAR TODAS AS CATEGORIAS
def lista_categorias():
	con=mysql.connect()
	cursor=con.cursor()
	cursor.execute("SELECT nome from categorias")
	categorias=cursor.fetchall()
	cursor.close()
	return categorias
#-------------------------------------------------------------------------------------------------------------
def lista_anuncios():
	con=mysql.connect()
	cursor=con.cursor()
	cursor.execute("SELECT categoria,expiracao,texto_tras,imagem_frente FROM anuncios order by expiracao ASC")
	anuncios=cursor.fetchall()
	cursor.close()
	con.close()
	return anuncios
#--------------------------------------------------------------------------------------------------------------
# Rota Index
@app.route('/')
def index():
	if session.get('logado') == True:
		return redirect(url_for("admin"))
	return render_template("login.html")
#-------------------------------------------------------------------------------------------------------------
# Rota Logout
@app.route('/logout')
def logout():
    session.pop('logado', None)
    return redirect(url_for('index'))
#-------------------------------------------------------------------------------------------------------------
# Rota Login
@app.route('/login',methods=['POST'])
def login():
	usr=str(request.form['user'])
	psw=str(request.form['password'])
	con=mysql.connect()
	cursor=con.cursor()
	cursor.execute("SELECT usuario,senha from usuarios WHERE usuario ='"+usr+"'")
	user=cursor.fetchall()
	con.close()

	if len(user) is 1:
		if (str(user[0][1])==psw):
			session['logado']=True #Setando a sessão
			return redirect(url_for("admin"))
		else:
			return "falhou"
	else:
		return "Usuario invalido"
#-------------------------------------------------------------------------------------------------------------
#Rota página Admin
@app.route('/admin')
def admin():
	if session.get('logado') != True:
		return redirect(url_for("index"))
	categorias = lista_categorias()
	return render_template("admin.html",categorias=categorias)
#-------------------------------------------------------------------------------------------------------------
#Rota lista anuncio
@app.route('/listar-anuncio')
def listanunc():
	if session.get('logado') != True:
		return redirect(url_for("index"))
	anuncios = lista_anuncios()
	return render_template("listar-anuncio.html",anuncios=anuncios)
#-------------------------------------------------------------------------------------------------------------
#Rota lista cliente
@app.route('/listar-cliente')
def listacliente():
	if session.get('logado') != True:
		return redirect(url_for("index"))
	return render_template("listar-cliente.html")
#-------------------------------------------------------------------------------------------------------------
#Rota lista categoria
@app.route('/listar-categoria')
def listacategoria():
	if session.get('logado') != True:
		return redirect(url_for("index"))
	categorias = lista_categorias()
	return render_template("listar-categoria.html", categorias=categorias)

#-------------------------------------------------------------------------------------------------------------
#Função cadastrar cliente
@app.route('/cadcliente',methods=['POST'])
def cadcliente():
	razao=str(request.form['razao'])
	cnpj=str(request.form['cnpj'])
	tel=str(request.form['telefone'])
	email=str(request.form['email'])
	resp=str(request.form['responsavel'])
	con=mysql.connect()
	cursor=con.cursor()
	try:
		cursor.callproc('cadcliente',(razao,cnpj,tel,email,resp))
		con.commit()
		flash('Cliente inserido com sucesso!')
	except:
		con.rollback()
		flash('Cliente não inserido!')
	cursor.close()
	con.close()

	return redirect(url_for("admin"))
#-------------------------------------------------------------------------------------------------------------
#FUNÇÃO CADASTRAR ANUNCIOS
@app.route('/cadanuncio',methods=['POST'])
def cadanuncio():
	categoria=str(request.form['categoria'])
	expiracao=request.form['expiracao']
	descricao = str(request.form['descricao'])
	img=request.files['imagem']
	name=str(random.randrange(1,1000000))+".png"
	img.save(os.path.join(app.config['UPLOAD_FOLDER'],name))
	con=mysql.connect()
	cursor=con.cursor()
	try:
		cursor.callproc('cadanuncio',(categoria,expiracao,descricao,name))
		con.commit()
		flash('Anuncio inserido com sucesso!')
	except:
		con.rollback()
		flash('Anuncio não inserido!')
	cursor.close()
	con.close()

	return redirect(url_for("admin"))

#------------------------------------------------------------------------------------------------------------
#Função cadastrar categoria
@app.route('/cadcat',methods=['POST'])
def cadcat():
	cate=request.form['cat']
	con=mysql.connect()
	cursor=con.cursor()
	try:
		cursor.callproc('cadcat',[cate,])
		con.commit()
		flash('Categoria inserida com sucesso!')
	except (ValueError, KeyError, TypeError) as error:
	    con.rollback()
	    flash(error)
	cursor.close()
	con.close()

	return redirect(url_for("admin"))
#-------------------------------------------------------------------------------------------------------------
@app.route('/tst')
def viewteste():
	return render_template("teste2.html")
#-------------------------------------------------------------------------------------------------------------
@app.route('/teste',methods=['POST'])
def testeimg():
	img=request.files['imagem']
	name=str(random.randrange(1,1000000))+".png"
	img.save(os.path.join(app.config['UPLOAD_FOLDER'],name))
	teste = print(img)
	return "blablabla"
#-------------------------------------------------------------------------------------------------------------
#API retorna os anuncios da HOME
@app.route('/api/home')
def home():
	tipo=request.args.get("tipo")
	con=mysql.connect()
	cursor=con.cursor()
	cursor.callproc('ListaIndex',(tipo,))
	ret = cursor.fetchall()
	anuncios=[]
	for anuncio in ret:
		i={
			'id':anuncio[0],
			'imagem':anuncio[1],
			'texto':anuncio[2],
			'categoria':anuncio[3]
		}
		anuncios.append(i)
	return jsonify(anuncios)
#-------------------------------------------------------------------------------------------------------------
# API Retorna os anuncios da categoria
@app.route('/api/list', methods=['GET'])
def list():

	cat = request.args.get("categoria")
	con=mysql.connect()
	cursor=con.cursor()
	cursor.callproc('ListaAnuncios',(cat,))
	categorias = cursor.fetchall()
	anuncios=[]
	for anuncio in categorias:
		i={
			'id':anuncio[0],
			'imagem':anuncio[1],
			'texto':anuncio[2],
			'categoria':anuncio[3]
		}
		anuncios.append(i)
	return jsonify(anuncios)
#-------------------------------------------------------------------------------------------------------------
# API Retorna todas as categorias
@app.route('/api/categorias',methods=['POST'])
def list_cats():
	con=mysql.connect()
	cursor=con.cursor()
	cursor.callproc('ListaCategorias')
	list_categorias = cursor.fetchall()
	cat=[]
	for nome in list_categorias:
		i={
			'nome':nome[0]
		}
		cat.append(i)
	return jsonify(cat)
#-------------------------------------------------------------------------------------------------------------

if __name__ == '__main__':
	app.config['SECRET_KEY'] = "koakoakoqews"
	port = int(os.environ.get('PORT', 5000))
	#app.debug= True
	#toolbar = DebugToolbarExtension(app)
	app.run(host='127.0.0.1', port=port)  #usar se for testar localmente
	#app.run(host='0.0.0.0', port=port)	#usar no herokuc


from wordcloud import WordCloud
import matplotlib.pyplot as plt
import jieba

path_txt = 'file.txt'
f = open(path_txt, 'r', encoding='UTF-8').read()

cut_text = " ".join(jieba.cut(f))

wordcloud = WordCloud(
    font_path="/Users/Jason/Library/Fonts/msyh.ttf",
    background_color="white", width=1000, height=880).generate(cut_text)

plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()


import unittest #Встроенная библиотека
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

import logging
logging.basicConfig(format='%(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO,
                    filename='test.log'
                    )

class LoginAvito(unittest.TestCase): #класс наследую от Юниттест (способ сообщения что это тест)
    def setUp(self):
        self.driver = webdriver.Chrome('/Users/bladerrunner/projects/autotests/chromedriver 2') # открывает браузер перед тестом
        self.driver.get('https://www.avito.ru')

    def test_open_login_form(self):
        self.driver.find_element_by_link_text('Вход и регистрация').click()
        self.login_field = self.driver.find_element_by_name('login')
        self.login_field.send_keys('Byyy')
        self.login_field = self.driver.find_element_by_name('password')
        self.login_field.send_keys('Byyy')
        self.login_field.send_keys(Keys.ENTER)

if __name__ == '__main__':
    unittest.main()


from collections import namedtuple

SensorInfo = namedtuple("SensorInfo", ['value', 'is_accumulated'])

def is_dev_accepted(name, disallowed_prefixes, allowed_prefixes):
    dev_ok = True

    if disallowed_prefixes is not None:
        dev_ok = all(not name.startswith(prefix)
                     for prefix in disallowed_prefixes)

    if dev_ok and allowed_prefixes is not None:
        dev_ok = any(name.startswith(prefix)
                     for prefix in allowed_prefixes)

    return dev_ok

def delta(func, only_upd=True):
    prev = {}
    while True:
        for dev_name, vals in func():
            if dev_name not in prev:
                prev[dev_name] = {}
                for name, (val, _) in vals.items():
                    prev[dev_name][name] = val
            else:
                dev_prev = prev[dev_name]
                res = {}
                for stat_name, (val, accum_val) in vals.items():
                    if accum_val:
                        if stat_name in dev_prev:
                            delta = int(val) - int(dev_prev[stat_name])
                            if not only_upd or 0 != delta:
                                res[stat_name] = str(delta)
                        dev_prev[stat_name] = val
                    elif not only_upd or '0' != val:
                        res[stat_name] = val

                if only_upd and len(res) == 0:
                    continue
                yield dev_name, res
        yield None, None


from osgeo import gdal, osr
import ogr
import utm
import pdb

class OutOfRangeError(Exception):
    pass

def find_roi(img_file, lat, lon, zone):
    """
    Find pixel which corresponds to lat and lon in image.

    Args:
        img_file: full path to georeferenced image file
        lat, lon: float, location to find
        zone: utm zone in which the image is projected

    Returns:
        x, y: location in pixel space of the lat lon provided
    """
    dataset = gdal.Open(img_file)   # open image
    geotransform = dataset.GetGeoTransform()   # get data transform

    # change lat_lon to same projection
    l_x, l_y, l_zone, l_zone_let = utm.from_latlon(lat, lon)

    if zone != l_zone:
        l_x, l_y = convert_utm_zones(l_x, l_y, l_zone, zone)

    # calculate pixel locations: http://www.gdal.org/gdal_datamodel.html
    x = int((l_x - geotransform[0]) / geotransform[1])   # latitude
    y = int((l_y - geotransform[3]) / geotransform[5])   # longitude

    return x, y

def convert_utm_zones(x, y, zone_from, zone_to):
    """
    Convert lat/lon to appropriate utm zone.

    Args:
        x, y: lat and lon, projected in zone_from
        zone_from: inital utm projection zone
        zone_to: final utm projection zone

    Returns:
        x, y: lat and lon, projected in zone_to
    """

    # Spatial Reference System
    input_epsg = int(float('326' + str(zone_from)))
    output_epsg = int(float('326' + str(zone_to)))

    # create a geometry from coordinates
    point = ogr.Geometry(ogr.wkbPoint)
    point.AddPoint(x, y)

    # create coordinate transformation
    in_spatial_ref = osr.SpatialReference()
    in_spatial_ref.ImportFromEPSG(input_epsg)

    out_spatial_ref = osr.SpatialReference()
    out_spatial_ref.ImportFromEPSG(output_epsg)

    coord_trans = osr.CoordinateTransformation(in_spatial_ref, out_spatial_ref)

    # transform point
    point.Transform(coord_trans)

    return point.GetX(), point.GetY()

def dc_avg(filename, poi):
    dataset = gdal.Open(filename)   # open image
    image = dataset.ReadAsArray()

    c, r = poi

    roi = image[r-1:r+2, c-1:c+2]

    return roi.mean()


from django.db import models
from django.utils import timezone

# Create your models here.

class Contacto(models.Model):
    nombre = models.CharField(max_length=100)
    email = models.EmailField()
    asunto = models.CharField(max_length=150)
    mensaje = models.TextField()
    fecha = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.nombre

    class Meta:
        db_table = "contacto"
        verbose_name = "contacto"
        verbose_name_plural = "contactos"
        ordering = ["id"]


import cv2

#crop
def crop(x,y,w,h,margin,img_width,img_height):
	xmin = int(x-w*margin)
	xmax = int(x+w*margin)
	ymin = int(y-h*margin)
	ymax = int(y+h*margin)
	if xmin<0:
		xmin = 0
	if ymin<0:
		ymin = 0
	if xmax>img_width:
		xmax = img_width
	if ymax>img_height:
		ymax = img_height
	return xmin,xmax,ymin,ymax

#display result
def show_results(img,results, img_width, img_height):
	img_cp = img.copy()
	print('len results:',len(results))
	img_cp1 = cv2.cvtColor(img_cp, cv2.COLOR_BGR2RGB)
	for i in range(len(results)):
		x = int(results[i][1])
		y = int(results[i][2])
		w = int(results[i][3])//2
		h = int(results[i][4])//2

		xmin,xmax,ymin,ymax=crop(x,y,w,h,1.0,img_width,img_height)

		cv2.rectangle(img_cp1,(xmin,ymin),(xmax,ymax),(0,255,0), 30)
		cv2.rectangle(img_cp1,(xmin,ymin-20),(xmax,ymin),(125,125,125),-1)
		cv2.putText(img_cp1,results[i][0] + ' : %.2f' % results[i][5],(xmin+5,ymin-7),cv2.FONT_HERSHEY_SIMPLEX,7,(255,0, 0), 15 , cv2.LINE_AA)
	return img_cp1 #cv2.imshow('YoloKerasHandDetection',img_cp)


# ADW, 07 June 2016
# Run this inside the directory with the images

import glob
import datetime as dt
import numpy as np
from matplotlib import pyplot as plt

files = sorted(glob.glob('*.jpg'))
f0 = files[0]

timestamps = []
unixtimes = []

# For the "strptime" function for working with time stamps,
# see http://www.tutorialspoint.com/python/time_strptime.htm
start_time = dt.datetime.strptime(files[0], "Img%Y-%m-%d %H.%M.%S.jpg")
seconds_since_start = []

for fi in files:
  timestamp = dt.datetime.strptime(fi, "Img%Y-%m-%d %H.%M.%S.jpg")
  timestamps.append(timestamp)
  # Writing this as an integer instead of timedelta with total_seconds
  seconds_since_start.append( (timestamp - start_time).total_seconds() )
seconds_since_start = np.array(seconds_since_start)

# Plot the results

plt.plot(seconds_since_start, 'k.');
plt.xlabel('Image number (sequential from start)', fontsize=16)
plt.ylabel('Real time since start of experiment [seconds]', fontsize=16)

plt.show()

# ... after this, it will be a matter of looking for legitimate breaks in time
# that are related to pauses in the experiment (e.g., laser scans)
# and replacing them with a number of seconds elapsed that comes
# immediately after those time-stamps that precede the laser scans.

# Also, using this method of working through the time stamps allows you to
# delete pictures before you sort them -- because it just reads the
# time stamps. Just so long as you don't create a big break in time without
# remembering that it was you who made it!


from abc import ABC, abstractmethod
from functools import partial
from weakref import WeakKeyDictionary

from typing import Type, TypeVar, Callable, Any, Tuple, Union

from venom import Message
from venom.exceptions import ValidationError
from venom.fields import FieldDescriptor
from venom.message import fields, merge_into
from venom.validation._validators import min_length, max_length, pattern, min_items, max_items, minimum, maximum
from .schema import Schema

M = TypeVar('M', bound=Message)

def _get_field_schema(field: FieldDescriptor) -> Schema:
    return merge_into(Schema(), field.schema or Schema())

class _MessageValidator(ABC):
    __msg_validators = WeakKeyDictionary()

    def __new__(cls, msg: Type[Message], **kwargs):
        try:
            return cls.__msg_validators[msg]
        except KeyError:
            pass

        instance = super(_MessageValidator, cls).__new__(cls)
        instance.__init__(msg, **kwargs)
        return instance

    def __init__(self, msg: Type[M], *, register: bool = True):
        if register:
            self.__msg_validators[msg] = self
        self._message_cls = msg

    @abstractmethod
    def validate(self, instance: M) -> None:
        raise NotImplementedError

class MessageValidator(_MessageValidator):
    def __init__(self, msg: Type[M]):
        super().__init__(msg)

        # TODO only create validators where the schema is not empty

        field_schemas = ((field, _get_field_schema(field)) for field in fields(msg))
        self._field_validators = tuple((field.name, _FieldValidator(field, schema))
                                       for field, schema in field_schemas
                                       if schema != Schema())

    def validate(self, message: M) -> None:
        for name, validator in self._field_validators:
            try:
                validator.validate(message.get(name))
            except ValidationError as v:
                v.path.insert(0, name)
                raise v

_ValidationFunction = Callable[[Any], None]

def _repeat(validations: Tuple[_ValidationFunction], instance: Union[list, dict]):
    for index, value in enumerate(instance):
        try:
            for validate in validations:
                validate(value)
        except ValidationError as v:
            v.path.insert(0, index)
            raise v

class _FieldValidator(object):
    __slots__ = ('_validations',)

    def __init__(self, field: FieldDescriptor, schema: Schema):
        self._validations = self._get_validations(field, schema)

    @classmethod
    def _get_validations(cls, field: FieldDescriptor, schema: Schema) -> Tuple[_ValidationFunction, ...]:
        validations = ()

        if schema == Schema():
            return ()

        if issubclass(field.type, Message):
            validator = MessageValidator(field.type)
            return validator.validate,
        elif field.type in (str, bytes):
            if schema.min_length > 0:
                validations += partial(min_length, schema),
            if schema.max_length is not None:
                validations += partial(max_length, schema),
            if schema.pattern:
                validations += partial(pattern, schema),
        elif field.type in (int, float):
            if schema.minimum is not None:
                validations += partial(minimum, schema),
            if schema.maximum is not None:
                validations += partial(maximum, schema),
        else:
            raise NotImplementedError

        if field.repeated:
            value_validations = validations
            validations = ()

            if field.key_type:
                raise NotImplementedError
            else:
                if schema.min_items > 0:
                    validations += partial(min_items, schema),
                if schema.max_items is not None:
                    validations += partial(max_items, schema),

            if value_validations:
                validations += partial(_repeat, value_validations),
        return validations

    def validate(self, instance: Any):
        for validate in self._validations:
            validate(instance)


import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

import numpy as np
from keras.models import load_model
from keras.preprocessing import sequence
from keras.preprocessing.text import Tokenizer

import joblib as jl
import re

model = load_model('./models/text9_cnn_model.h5')

genre = 9
maxLen = 40

filepath = r'./input_text9/'
# filepath = r'./input_text3/'

def rm(text):

	rm = re.compile(r'\n')
	return rm.sub('', text)

def readFile(path):

	questions = []
	labels = []
	file_list = []

	for file in os.listdir(path):
		file_list.append(path + file)

	counter_question = 0
	counter_file = 0
	for file_name in file_list:
		with open(file_name, encoding='utf-8') as f:
			while 1:
				line = f.readline()
				if not line:
					counter_file += 1
					break;
				questions.append(rm("".join(line)))
				labels.append(counter_file)
				counter_question += 1

	return questions, labels, counter_question

def preprocessing(question):

	with open("./models/tokenizer9", "rb") as handler:
		tokenizer = jl.load(handler)

	#x_train_seq = tokenizer.texts_to_sequences(train_texts)
	question = tokenizer.texts_to_sequences(question)
	#x_train = sequence.pad_sequences(x_train_seq, maxlen=30)
	question = sequence.pad_sequences(question, maxlen=maxLen)

	return question

questions, labels, num_questions = readFile(r'./input_text9/')

questions_array = preprocessing(questions)

result = model.predict(questions_array)

result_array = np.argmax(result, axis=1)

# 输出所有预测结果
# for index in range(num_questions):
# 	print(questions[index], result_array[index])

# 错误语句输出
counter_failed = 0
for index in range(num_questions):
	if labels[index] != result_array[index]:
		counter_failed += 1
		print(questions[index], ', 预测分类为: ', result_array[index], ', 正确分类为: ', labels[index])

print('总共测试 ', num_questions, ' 个样本, ', counter_failed, ' 个样本预测失败, 正确率为: ', (num_questions - counter_failed) / num_questions)


from itertools import combinations
from math import prod

with open('input24.txt') as f:
    data = sorted(map(int, list(f)), reverse=True)

def quantum(groups: int) -> int:
    groupweight = sum(data) // groups
    minlen = 1
    while sum(data[:minlen]) < groupweight:
        minlen += 1
    found = False
    min_qe = float('+inf')
    while not found:
        for c in combinations(data, minlen):
            weight = sum(c)
            if weight == groupweight:
                found = True
                qe = prod(c)
                if qe < min_qe:
                    min_qe = qe
        if not found:
            minlen += 1
    return min_qe

print(quantum(3), quantum(4))


import os
import sqlite3
import flask

app = flask.Flask(__name__)

def get_db():
    db = sqlite3.connect('db.sqlite3')
    db.row_factory = sqlite3.Row
    return db

def create_db():
    db = get_db()
    db.execute('CREATE TABLE row ' + \
               '(id INTEGER PRIMARY KEY AUTOINCREMENT,' + \
               'message TEXT NOT NULL)')
    db.close()

if not os.path.isfile('db.sqlite3'):
    create_db()

@app.route('/')
def home():
    db = get_db()
    rows = get_db().execute('SELECT * FROM row').fetchall()
    db.close()
    return flask.render_template('template.html', rows = rows)

@app.route('/add/')
def add():
    db = get_db()
    db.execute('INSERT INTO row(message) VALUES(?)', ('My Message',))
    db.commit()
    db.close()
    return flask.redirect(flask.url_for('home'))

@app.route('/clear/')
def clear():
    db = get_db()
    db.execute('DELETE FROM row')
    db.commit()
    db.close()
    return flask.redirect(flask.url_for('home'))

if __name__ == '__main__':
    app.run(port=12345, debug=True)


from sqlalchemy import Column, String, Table, BigInteger
from models import Base

customerprofile = Table('CUSTOMERPROFILE', Base.metadata,
                        Column('id', BigInteger, primary_key=True),
                        Column('mobileUnionID', BigInteger),
                        Column('fiveUnionID', BigInteger),
                        Column('name', String(120)),
                        Column('certNo', String(50)),
                        Column('mobilePhone', String(30))
                        )


from django.urls import path

from . import views

# app_name = "team_manager"

urlpatterns = [
    path('', views.UitslagView.as_view(), name='index'),
    path('uitslag/', views.UitslagView.as_view(), name='uitslag'),
    path('uitslag/<race>/', views.uitslag_race, name='uitslag_race'),
    path('stand/',views.StandView.as_view(), name='stand'),
    path('spelerteam/',views.SpelerTeamView.as_view(), name='spelerteam'),
]


"""
Creates a mosaic of an image out of instances of the same image.
"""

from PIL import Image

import settings

def averageImageColor(image):
    """
    Calculates the averge color of an image.

    Args:
        image: A PIL image object.
    Returns:
        A (r, g, b) tuple that contains the average color of the image.
    """
    histogram = image.histogram()

    # Split the histogram into colors
    red = histogram[0:256]
    green = histogram[256:256 * 2]
    blue = histogram[256 * 2:256 * 3]

    # Determine averages of the channels
    averageRed = sum(channel * weight for channel, weight in enumerate(red)) / sum(red)
    averageGreen = sum(channel * weight for channel, weight in enumerate(green)) / sum(green)
    averageBlue = sum(channel * weight for channel, weight in enumerate(blue)) / sum(blue)

    return (averageRed, averageGreen, averageBlue)

def imageIntensity(image):
    """
    Calculates the intensity of an image.

    Args:
        image: A PIL image object.
    Returns:
        The intensity of the image, from 0 to 255.
    """
    color = averageImageColor(image)
    intensity = (0.2989 * color[0]) + (0.5870 * color[1]) + (0.1140 * color[2])
    return intensity

def coloredImage(image, color, shade):
    """
    Modifies an image to express a color more vibrantly.

    Args:
        image: A PIL image object that will be modified.
        color: The color that the image will express more vibrantly.
        shade: If true, then the image will get darker; otherwise, it will get lighter.
    Returns:
        A PIL image object that has been modified.
    """
    pixels = list(image.getdata())

    # Get the color distances of each pixel from the specified color
    distances = []
    for pixel in pixels:
        colorDistance = (
                         ((color[0] - pixel[0]) ** 2) +
                         ((color[1] - pixel[1]) ** 2) +
                         ((color[2] - pixel[2]) ** 2)
                        ) ** 0.5
        distances.append(colorDistance)

    # Find range of distances; change distances to percentages
    maxDistance = max(distances)
    minDistance = min(distances)
    percentDistances = []
    for distance in distances:
        percent = (distance - minDistance) / float((maxDistance - minDistance))
        percentDistances.append(percent)

    # Shade or tint pixels according to their percent distance
    # If the image is on average dark, shade; otherwise, tint
    modifiedPixels = []
    for pixel, percent in zip(pixels, percentDistances):
        if shade:
            modified = tuple([int(round(color * percent)) for color in pixel])
        else:
            modified = tuple([int(round(((255 - color) * percent) + color)) for color in pixel])
        modifiedPixels.append(modified)

    # Create new image with modified pixels
    newImage = Image.new("RGB", image.size)
    newImage.putdata(modifiedPixels)
    return newImage

def mosaic(sourcePath, savePath, tileWidth, mosaicColumns, shade):
    """
    Saves a mosaic of a source image, made up of source image tiles.

    Args:
        sourcePath: The location of the source image.
        savePath: The location where the final mosaic will be saved to.
        tileWidth:  The width of the tiles in pixels which make up the mosaic.
        mosaicColumns: The number of times a tile repeats horizontally.
        shade: If true, then instances will get darker; otherwise, they will get lighter.
    """
    source = Image.open(sourcePath)
    sourceDimensions = source.size
    sourceWidth = sourceDimensions[0]
    sourceHeight = sourceDimensions[1]

    # Determine mosaic dimensions
    mosaicColumns = mosaicColumns
    tileWidth = tileWidth
    mosaicWidth = tileWidth * mosaicColumns

    mosaicRows = int(round(mosaicColumns * (sourceHeight / float(sourceWidth))))
    tileHeight = int(round(tileWidth * (sourceHeight / float(sourceWidth))))
    mosaicHeight = tileHeight * mosaicRows

    tileTemplate = source.resize((tileWidth, tileHeight))

    # Determine the colors of each tile
    sourceTileWidth = sourceWidth / mosaicColumns
    sourceTileHeight = sourceHeight / mosaicRows
    tileColors = []
    for row in range(mosaicRows):
        rowColors = []
        upper = row * sourceTileHeight
        lower = upper + sourceTileHeight
        for column in range(mosaicColumns):
            left = column * sourceTileWidth
            right = left + sourceTileWidth
            tile = source.crop((left, upper, right, lower))
            rowColors.append(averageImageColor(tile))
        tileColors.append(rowColors)

    # Determine if tiles should be shaded or tinted based on source intensity
    if shade is None:
        if imageIntensity(tileTemplate) < 255.0 / 2.0:
            shade = True
        else:
            shade = False

    # Place tiles corresponding to average colors onto mosaic
    mosaic = Image.new("RGB", (mosaicWidth, mosaicHeight))
    for rowNumber, row in enumerate(tileColors):
        upper = rowNumber * tileHeight
        for columnNumber, color in enumerate(row):
            left = columnNumber * tileWidth
            tile = coloredImage(tileTemplate, color, shade)
            mosaic.paste(tile, (left, upper))
    mosaic.save(savePath, "JPEG")

if __name__ == '__main__':
    mosaic(settings.sourcePath, settings.savePath, settings.tileWidth, settings.mosaicColumns, settings.shade)


    # Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from collections import deque

import argparse
import os
import sys
import time
import cv2

import numpy as np
import tensorflow as tf

def load_graph(model_file):
  graph = tf.Graph()
  graph_def = tf.GraphDef()

  with open(model_file, "rb") as f:
    graph_def.ParseFromString(f.read())
  with graph.as_default():
    tf.import_graph_def(graph_def)

  return graph

def read_tensor_from_image_file(file_name, input_height=299, input_width=299,
				input_mean=0, input_std=255):
  input_name = "file_reader"
  output_name = "normalized"
  file_reader = tf.read_file(file_name, input_name)
  if file_name.endswith(".png"):
    image_reader = tf.image.decode_png(file_reader, channels = 3,
                                       name='png_reader')
  elif file_name.endswith(".gif"):
    image_reader = tf.squeeze(tf.image.decode_gif(file_reader,
                                                  name='gif_reader'))
  elif file_name.endswith(".bmp"):
    image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader')
  else:
    image_reader = tf.image.decode_jpeg(file_reader, channels = 3,
                                        name='jpeg_reader')
  float_caster = tf.cast(image_reader, tf.float32)
  dims_expander = tf.expand_dims(float_caster, 0);
  resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width])
  normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
  sess = tf.Session()
  result = sess.run(normalized)

  return result

def load_labels(label_file):
  label = []
  proto_as_ascii_lines = tf.gfile.GFile(label_file).readlines()
  for l in proto_as_ascii_lines:
    label.append(l.rstrip())
  return label

if __name__ == "__main__":
  file_name = "tf_files/videos"
  model_file = "tf_files/retrained_graph.pb"
  label_file = "tf_files/retrained_labels.txt"
  output = "output"
  input_height = 299
  input_width = 299
  input_mean = 128
  input_std = 128
  input_layer = "Mul"
  output_layer = "final_result"

  parser = argparse.ArgumentParser()
  parser.add_argument("--image", help="image to be processed")
  parser.add_argument("--graph", help="graph/model to be executed")
  parser.add_argument("--labels", help="name of file containing labels")
  parser.add_argument("--output", help="name of dir to write output to")
  parser.add_argument("--input_height", type=int, help="input height")
  parser.add_argument("--input_width", type=int, help="input width")
  parser.add_argument("--input_mean", type=int, help="input mean")
  parser.add_argument("--input_std", type=int, help="input std")
  parser.add_argument("--input_layer", help="name of input layer")
  parser.add_argument("--output_layer", help="name of output layer")
  args = parser.parse_args()

  if args.graph:
    model_file = args.graph
  if args.output:
    output = args.output
  if args.image:
    file_name = args.image
  if args.labels:
    label_file = args.labels
  if args.input_height:
    input_height = args.input_height
  if args.input_width:
    input_width = args.input_width
  if args.input_mean:
    input_mean = args.input_mean
  if args.input_std:
    input_std = args.input_std
  if args.input_layer:
    input_layer = args.input_layer
  if args.output_layer:
    output_layer = args.output_layer

  graph = load_graph(model_file)

  for video_name in os.listdir(file_name):

      #custom variables for smoothing
      smooth_lst = deque(maxlen=10)
      smooth_var = 0

      #custom variables for counting chews
      chew_count = 0
      toggle = True

      #timing dictionary
      times = {}

      video_capture = cv2.VideoCapture(file_name+"/"+video_name)
      while (video_capture.isOpened()):

          ret,frame = video_capture.read()
          frameId = video_capture.get(1) #current frame number
          if ((frameId % 2) ==0 and (ret == True)):

              cv2.imwrite(filename="screens/"+"alpha.jpg", img=frame);
              image_data = "screens/alpha.jpg"
              t = read_tensor_from_image_file(image_data,
                                              input_height=input_height,
                                              input_width=input_width,
                                              input_mean=input_mean,
                                              input_std=input_std)

              input_name = "import/" + input_layer
              output_name = "import/" + output_layer
              input_operation = graph.get_operation_by_name(input_name);
              output_operation = graph.get_operation_by_name(output_name);

              with tf.Session(graph=graph) as sess:
                #start = time.time()
                results = sess.run(output_operation.outputs[0],
                                  {input_operation.outputs[0]: t})
                #end=time.time()
              results = np.squeeze(results)

              top_k = results.argsort()[-5:][::-1]
              labels = load_labels(label_file)

              print (labels[0], results[0])

              if (labels[0]=="chew" and results[0]>0.99):
                smooth_lst.append("1")
              else:
                smooth_lst.append("0")

              numlist = [int(x) for x in smooth_lst]
              print (numlist)

              new_frame = cv2.resize(frame, (960,540))

              if (sum(numlist)>=7 and toggle == True):

                  toggle = False
                  print ("HERE WE GOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO!!")
                  print (frameId)
                  chew_count += 1

                  #populate dict with times of chews, time.time works on Unix and Windows but time.clock doesnt
                  times[chew_count] = frameId
                  print (times)
                  #print (numlist)

              if (sum(numlist)==0):
                  toggle = True

              if (toggle == False):
                   #superimpose "Chew" text on output frame
                   font = cv2.FONT_HERSHEY_SIMPLEX
                   cv2.putText(new_frame,'Chew',(10,60), font, 2,(124,252,0),2,cv2.LINE_AA)

              img_path = "D:/Jpegs"
              cv2.imwrite(img_path+"/"+str(frameId)+video_name[:-4]+".jpg", new_frame)
              #cv2.imshow("image", new_frame)
              #cv2.waitKey(30)

          if (ret == False):
              break

      if (chew_count>1):
          #access the last chew frame and subtract when the first chew frame occurred
          time = (times[chew_count]-times[1])/25
          print ("Time for 1st chew: " + str(times[1]))
          print ("This is the chewcount" + str(chew_count))
          print ("Time for last chew: " + str(times[chew_count]))

          chew_rate = time/(chew_count-1)

          #save chew_rates to files
          filepath = output + "/" + video_name[:-4]

          f = open(filepath+".txt", "w+")
          f.write(str(chew_rate))
          f.close()
          f= open(output+"/all.txt", "a+")
          f.write(str(chew_rate)+"\n")
          f.close()

          print (chew_rate)

      else:
          print ("Minimum of two chews needs to occur to calculate rate!")

          #print (smooth_lst)
      print ("Chews: " + str(chew_count))
      print (times)

      #empty out dictionary
      times.clear()

      video_capture.release()
      cv2.destroyAllWindows()


import json as j

class Locales:

    def __init__(self, file=None):
        self.file_name = file
        self.lang = None
        self.messages = None
        self.languages = None

        if self.file_name:
            self.open_doc(file)

    @staticmethod
    def example(file_name=None):
        if not file_name:
            file_name = "example.json"

        with open(file_name, 'w', encoding='utf-8') as f:
            data = {
                "languages": {
                    "ru": "Русский", "eng": "English", "sp": "Español"
                },

                "messages": {
                    "welcome": {
                        "ru": "Добро пожаловать!",
                        "eng": "Welcome!"
                    },
                    "goodbye": {
                        "ru": "Пока",
                        "eng": "Goodbye",
                        "sp": "Adiós"
                    }
                }

            }
            j.dump(data, f, indent=2, ensure_ascii=False)

    def open_doc(self, file):
        with open(file, 'r', encoding='utf-8') as f:
            d = j.load(f)

        if "languages" not in d:
            raise KeyError("You need to initialize languages in json body")

        self.languages = d["languages"]
        self.lang = list(self.languages.keys())[0]

        if "messages" not in d:
            raise KeyError("You need to initialize messages in json body")

        self.messages = d["messages"]

        m_l = set()
        [[m_l.add(l) for l in [*x.keys()]] for x in [*d["messages"].values()]]

        for key in m_l:
            if key not in d["languages"]:
                raise ValueError('Key "{}" should be in "languages"'.format(key))

    def get(self, message, language=None):
        if message not in self.messages:
            raise KeyError('"{}" not found in "messages"'.format(message))

        if language:
            if language not in self.languages:
                raise KeyError('"{}" not found in "languages"'.format(language))
            return self.messages[message][language]
        else:
            return self.messages[message][self.lang]

    def get_all(self, message):
        if message not in self.messages:
            raise KeyError('"{}" not found in "messages"'.format(message))

        return self.messages[message]

    def get_by_name(self, name):
        n = [*self.languages.keys()]
        d_n = [*self.languages.values()]

        if name not in d_n:
            raise ValueError('"{}" not found as Value in "languages"'.format(name))

        i = d_n.index(name)

        return n[i]

    def set_default_lang(self, language):
        if language not in self.languages:
            raise KeyError('"{}" not found in "languages"'.format(language))

        self.lang = language


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

import os
import sys
import numpy as np
import torch
import random
from torch.autograd import Variable
from torch.nn.parameter import Parameter
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from tqdm import tqdm

from dde.common.cmd_args import cmd_args
from dde.common.pytorch_initializer import weights_init

class MLPEnergy(nn.Module):
    def __init__(self, input_dim, hidden_dim, depth, output_dim=1, act_out = None):
        super(MLPEnergy, self).__init__()
        self.act_out = act_out
        self.output_dim = output_dim
        self.depth = depth
        list_w = []
        if self.depth > 1:
            list_w.append( nn.Linear(input_dim, hidden_dim) )
            for i in range(self.depth - 2):
                list_w.append( nn.Linear(hidden_dim, hidden_dim) )

            list_w.append( nn.Linear(hidden_dim, self.output_dim) )
        else:
            list_w.append( nn.Linear(input_dim, self.output_dim) )

        self.list_w = nn.ModuleList(list_w)

        weights_init(self)

    def forward(self, x, mask = None):
        x = x.view(x.size()[0], -1)
        for i in range(self.depth):
            if i + 1 == self.depth and cmd_args.fix_phi:
                x = x.detach()
            h = self.list_w[i](x)
            if i + 1 < self.depth:
                x = F.relu(h)
            else:
                x = h

        if self.act_out is not None:
            out = self.act_out(x)
        else:
            out = x
        if mask is not None:
            assert mask.size()[0] == out.size()[0] and mask.size()[1] == self.output_dim
            s = F.torch.sum(mask * out, dim=1, keepdim=True)
            return s
        return out

class CondMLPEnergy(nn.Module):
    def __init__(self, x_dim, y_dim, hidden_dim, depth, output_dim=1, act_out = None):
        super(CondMLPEnergy, self).__init__()
        self.act_out = act_out
        self.output_dim = output_dim
        self.depth = depth
        input_dim = 2 * x_dim
        self.y2h = nn.Linear(y_dim, x_dim)
        list_w = []
        if self.depth > 1:
            list_w.append( nn.Linear(input_dim, hidden_dim) )
            for i in range(self.depth - 2):
                list_w.append( nn.Linear(hidden_dim, hidden_dim) )

            list_w.append( nn.Linear(hidden_dim, self.output_dim) )
        else:
            list_w.append( nn.Linear(input_dim, self.output_dim) )

        self.list_w = nn.ModuleList(list_w)

        weights_init(self)

    def forward(self, x, y):
        x = x.view(x.size()[0], -1)
        hy = self.y2h(y)
        x = torch.cat([x, hy], dim=-1)
        for i in range(self.depth):
            if i + 1 == self.depth and cmd_args.fix_phi:
                x = x.detach()
            h = self.list_w[i](x)
            if i + 1 < self.depth:
                x = F.relu(h)
            else:
                x = h

        if self.act_out is not None:
            out = self.act_out(x)
        else:
            out = x
        return out

def get_gamma(X, bandwidth):
    x_norm = torch.sum(X ** 2, dim=1, keepdim=True)
    x_t = torch.transpose(X, 0, 1)
    x_norm_t = x_norm.view(1, -1)
    t = x_norm + x_norm_t - 2.0 * torch.matmul(X, x_t)
    dist2 = F.relu(Variable(t)).detach().data

    d = dist2.cpu().numpy()
    d = d[np.isfinite(d)]
    d = d[d > 0]
    median_dist2 = float(np.median(d))
    print('median_dist2:', median_dist2)
    gamma = 0.5 / median_dist2 / bandwidth
    return gamma

def get_kernel_mat(x, landmarks, gamma):
    feat_dim = x.shape[1]
    batch_size = x.shape[0]
    x = x.view(1, -1).repeat(landmarks.shape[0], 1)  # db_size, bsize x feat_dim
    d = (x - landmarks.repeat(1, batch_size)) ** 2
    d_list = torch.split(d, feat_dim, dim=1)
    d = torch.sum(torch.cat(d_list, dim=0), dim=1)

    # get kernel matrix
    k = torch.exp(d * -gamma)
    k = k.view(batch_size, -1)
    return k

class CondKernelExpFamily(nn.Module):
    def __init__(self, X, Y, bd_x, bd_y):
        super(CondKernelExpFamily, self).__init__()
        self.X = torch.Tensor(X).contiguous()
        self.Y = torch.Tensor(Y).contiguous()
        self.basis_x = np.copy(X)
        self.basis_y = np.copy(Y)
        self.gamma_x = get_gamma(self.X, bd_x)
        self.gamma_y = get_gamma(self.Y, bd_y)
        print('gamma_x:', self.gamma_x)
        print('gamma_y:', self.gamma_y)
        if cmd_args.ctx == 'gpu':
            self.X = self.X.cuda()
            self.Y = self.Y.cuda()
        self.X = Variable(self.X)
        self.Y = Variable(self.Y)
        self.db_size = self.X.shape[0]
        self.alpha = Parameter(torch.Tensor(self.db_size, 1).normal_(0, 0.1))

        self.kxx = get_kernel_mat(self.X, self.X, self.gamma_x)
        self.kyy = get_kernel_mat(self.Y, self.Y, self.gamma_y)
        self.kxxyy = self.kxx * self.kyy
        self.kxxyy = self.kxxyy.detach()

    def get_norm(self):
        if cmd_args.l2 <= 0:
            return 0.0
        at = torch.transpose(self.alpha, 0, 1)
        t = torch.matmul(at, self.kxxyy)
        norm2 = torch.matmul(t, self.alpha)
        return cmd_args.l2 * norm2

    def forward(self, x, y):
        kx = get_kernel_mat(x, self.X, self.gamma_x)
        ky = get_kernel_mat(y, self.Y, self.gamma_y)

        kxxyy = kx * ky
        scores = torch.matmul(kxxyy, self.alpha)
        return scores

class KernelExpFamily(nn.Module):
    def __init__(self, dataset, bandwidth, bias=False):
        super(KernelExpFamily, self).__init__()
        self.has_bias = bias
        self.X = torch.tensor(dataset, dtype=torch.float32)
        assert bandwidth > 0
        self.gamma = get_gamma(self.X, bandwidth)
        self.bd = None
        print('gamma:', self.gamma)
        # else:
        #     self.bd = Parameter(torch.tensor(1.0 / np.abs(bandwidth), dtype=torch.float32))

        self.db_size = self.X.shape[0]
        self.alpha = Parameter(torch.Tensor(self.db_size, 1).normal_(0, 0.1))
        self.bias = Parameter(torch.Tensor(1).normal_(0, 0.1))
        if cmd_args.ctx == 'gpu':
            self.X = self.X.cuda()

    def forward(self, x):
        if self.bd is None:
            gamma = self.gamma
        else:
            gamma = 0.5 / self.median_dist2 * self.bd
        k = get_kernel_mat(x, self.X, gamma)
        scores = torch.matmul(k, self.alpha)
        if self.has_bias:
            scores += self.bias
        return scores

def log_sum_exp(vec, dim, keepdim):
    vec = torch.sum(torch.exp(vec), dim=dim, keepdim=keepdim)
    return vec


"""
Test nimare.meta.cbma.ale (ALE/SCALE meta-analytic algorithms).
"""
import pytest

import nimare
from nimare.meta.cbma import ale
from nimare.correct import FWECorrector

def test_ale():
    """
    Smoke test for ALE
    """
    meta = ale.ALE()
    res = meta.fit(pytest.cbma_testdata1)
    assert isinstance(res, nimare.base.MetaResult)
    corr = FWECorrector(method='bonferroni')
    cres = corr.transform(res)
    assert isinstance(cres, nimare.base.MetaResult)
    corr = FWECorrector(method='permutation', voxel_thresh=0.001,
                        n_iters=5, n_cores=1)
    cres = corr.transform(meta.results)
    assert isinstance(cres, nimare.base.MetaResult)

def test_ale_subtraction():
    """
    Smoke test for ALE
    """
    meta1 = ale.ALE()
    res1 = meta1.fit(pytest.cbma_testdata1)

    meta2 = ale.ALE()
    res2 = meta2.fit(pytest.cbma_testdata1)

    corr = FWECorrector(method='permutation', voxel_thresh=0.001,
                        n_iters=5, n_cores=1)
    cres1 = corr.transform(res1)
    cres2 = corr.transform(res2)

    sub_meta = ale.ALESubtraction()
    sub_meta.fit(
        meta1, meta2,
        image1=cres1.get_map('logp_level-cluster_corr-FWE_method-permutation', return_type='image'),
        image2=cres2.get_map('logp_level-cluster_corr-FWE_method-permutation', return_type='image'))
    assert isinstance(sub_meta.results, nimare.base.MetaResult)


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

#importing data from c++ code
data = pd.read_csv('rk6.txt', names=['x', 'y_rk6', 'y_exact', 'diff_rk6'])

#Defining Points
numPoints=np.array([10, 100, 1000, 10000])
x0=0.0; xf=5.
h = (xf-x0)/numPoints

#Defining mean difference with c++ data
diff_all = np.array([np.mean(np.array(data.iloc[0:10,3])), np.mean(np.array(data.iloc[11:110,3])),
	np.mean(np.array(data.iloc[111:1110,3])), np.mean(np.array(data.iloc[1111:11110,3]))])

#ploting data
fig = plt.figure(figsize=(8,4))
ax0 = fig.add_subplot(131); ax1 = fig.add_subplot(132)
ax2 = fig.add_subplot(133)

#first subplot
ax0.plot(data.iloc[111:1110,0], data.iloc[111:1110,1], 'm-', label='rk6 sol')
ax0.plot(data.iloc[111:1110,0], data.iloc[111:1110,2], 'c--', label='exact sol')
ax0.set_xlabel('x'); ax0.set_ylabel('y')
ax0.legend()

#second subplot
ax1.semilogy(data.iloc[111:1110,0], data.iloc[111:1110,3], 'm-', label='rk6 diff')
ax1.set_xlabel('x'); ax1.set_ylabel(r'log|$y_{exact}$ - $y_{num}$|')
ax1.legend()

#third subplot
ax2.plot(h, diff_all, 'm-')
ax2.set_xlabel('h'); ax2.set_ylabel('mean difference')

plt.tight_layout()
plt.show()


import os
import sys
import cv2
import torch
import random
import sys_utils
import nn_utils

import numpy as np
import torch.nn as nn
import data_provider

from sys_utils import _single_instance_logger as logger
from PIL import Image

# 格式定义：
# pixel_annotations：       像素为单位的标注，格式是[left, top, right, bottom]，绝对位置标注
# normalize_annotations:    归一化后的标注0-1，除以图像宽高，格式是[cx, cy, width, height]

# 总结：
# 1.hsv增广没做v
# 2.exif信息没用到
# 3.collate_fn函数没测试
# 4.随机翻转v

class Dataset:
    def __init__(self, augment, image_size, provider, batch_size=None, max_stride=32):
        self.augment = augment
        self.image_size = image_size
        self.border_fill_value = 114
        self.provider = provider

        # 获取所有标注的尺寸信息，基于像素宽高
        # all_labeled_information is   **[ jpeg_file, normalize_annotations, [image_width, image_height] ]**
        # normalize_annotations is **[ [cx, cy, width, height, class] ] ndarray**
        all_annotation_sizes = []
        for item in self.provider.all_labeled_information:
            normalize_annotations = item[1]
            image_width, image_height = item[2]
            max_line = max(image_width, image_height)
            scale_width = image_size * image_width / max_line
            scale_height = image_size * image_height / max_line

            annotation_size = normalize_annotations[:, [2, 3]] * [scale_width, scale_height]
            all_annotation_sizes.append(annotation_size)
        self.all_annotation_sizes = np.concatenate(all_annotation_sizes, axis=0)

        if not self.augment:
            # build rectangular
            batch_shapes_wh, batch_index_list, sorted_index = self.build_rectangular(image_size, batch_size, max_stride, provider.all_labeled_information)

            # 得到批次的形状，以及根据索引重新编排咱们的数据
            self.batch_shapes_wh    = batch_shapes_wh
            self.batch_index_list   = batch_index_list
            self.provider.all_labeled_information = [self.provider.all_labeled_information[i] for i in sorted_index]

    def build_rectangular(self, image_size, batch_size, max_stride, all_labeled_information, pad=0.5):
        '''
        Rectangular Training  https://github.com/ultralytics/yolov3/issues/232
        说明：
            构建基于矩形训练的信息。其含义有：
            1. dataset不能够使用shuffle的方式进行数据获取
            2. 对于所有图像集合S(image, size[Nx2, w h])，训练批次为B
            3. 排序S，基于size的宽高比，得到I
            4. 使得每个批次获取的图像，其宽高比都是接近的。因此可以直接对所有图进行最小填充，使得宽高一样作为输出。这样我们就得到一批一样大小的图进行迭代了
                但是每个批次间，可能会不一样
        '''

        # all_labeled_information is   **[ jpeg_file, normalize_annotations, [image_width, image_height] ]**
        datas = all_labeled_information
        total_images = len(datas)

        # 比如有10个图，batch=3
        # 得到的是：0001112223
        batch_index_list = np.floor(np.arange(total_images) / batch_size).astype(np.int32)

        # 没轮迭代所需要的批次数
        number_of_batches = batch_index_list[-1] + 1

        # 提取所有的宽高尺寸
        shapes_wh = np.array(list(map(lambda x: x[2], datas)), dtype=np.float32)

        # 计算高宽比
        aspect_ratio = shapes_wh[:, 1] / shapes_wh[:, 0]

        # 排序并得到索引
        index_of_image_based_aspect_ratio = aspect_ratio.argsort()

        # 重新编序宽高比
        aspect_ratio = aspect_ratio[index_of_image_based_aspect_ratio]

        # 计算每个shape基于image_size的宽高比
        batch_shapes_wh = [[1, 1]] * number_of_batches
        for ibatch in range(number_of_batches):

            # 提取属于这个batch的所有宽高比（横纵比）
            aspect_ratio_of_ibatch = aspect_ratio[batch_index_list == ibatch]

            # 计算最小和最大的高宽比
            min_aspect_ratio, max_aspect_ratio = aspect_ratio_of_ibatch.min(), aspect_ratio_of_ibatch.max()

            if max_aspect_ratio < 1:
                # 如果这个批次的图像全都是，宽度小于高度的。说明长边是高度方向
                # 长边设置为图像大小，短边则用宽高比
                batch_shapes_wh[ibatch] = [1, max_aspect_ratio]
            elif min_aspect_ratio > 1:
                # 如果这个批次的图像全都是，宽度大于高度的
                batch_shapes_wh[ibatch] = [1 / min_aspect_ratio, 1]

        batch_shapes_wh = np.ceil(np.array(batch_shapes_wh) * image_size / max_stride + pad).astype(np.int) * max_stride
        return batch_shapes_wh, batch_index_list, index_of_image_based_aspect_ratio

    def __len__(self):
        return len(self.provider)

    def __getitem__(self, image_indice):
        if self.augment:
            image, normalize_annotations = self.load_mosaic(image_indice)
            restore_info = None

            # 应用hsv增广
            image = self.hsv_augment(image)

            # 一定概率应用随机水平翻转
            if random.random() < 0.5:
                image, normalize_annotations = self.horizontal_flip(image, normalize_annotations)
        else:
            batch_index = self.batch_index_list[image_indice]
            batch_width, batch_height = self.batch_shapes_wh[batch_index]
            image, normalize_annotations, restore_info = self.load_center_affine(image_indice, batch_width, batch_height)

        num_targets = len(normalize_annotations)

        # BGR to RGB
        bgr_image = image
        image = np.ascontiguousarray(bgr_image[..., ::-1].transpose(2, 0, 1))

        # normalize
        image = image / np.array([255.0], dtype=np.float32)

        # image_id, class_index, cx, cy, width, height
        output_annotations = np.zeros((num_targets, 6), dtype=np.float32)
        output_annotations[:, 1:] = normalize_annotations[:, [4, 0, 1, 2, 3]]
        return torch.from_numpy(image), bgr_image, torch.from_numpy(output_annotations), normalize_annotations, restore_info

    def load_center_affine(self, image_indice, output_width, output_height):
        '''
        加载图像，采用中心对齐的方式
        返回值：
            image, normalize_annotations
        '''

        image, normalize_annotations, (width, height), (origin_width, origin_height), scale = self.load_image_with_uniform_scale(image_indice)
        #nn_utils.draw_norm_bboxes(image, normalize_annotations, color=(0, 0, 255), thickness=5)

        pad_width = output_width - width
        pad_height = output_height - height

        pad_left = pad_width // 2
        pad_right = pad_width - pad_left
        pad_top = pad_height // 2
        pad_bottom = pad_height - pad_top

        fill_value = (self.border_fill_value, self.border_fill_value, self.border_fill_value)
        image = cv2.copyMakeBorder(image, pad_top, pad_bottom, pad_left, pad_right, borderType=cv2.BORDER_CONSTANT, value=fill_value)

        # 框处理
        # (norm_cx * origin_width + pad_left) / self.image_size
        # norm_cx * origin_width / self.image_size + pad_left / self.image_size
        #        alpha =  origin_width / self.image_size
        #        beta =   pad_left / self.image_size
        #
        # norm_width * origin_width / self.image_size
        # norm_width * alpha

        x_alpha = width / output_width
        x_beta = pad_left / output_width
        y_alpha = height / output_height
        y_beta = pad_top / output_height

        # cx, cy
        normalize_annotations[:, [0, 1]] = normalize_annotations[:, [0, 1]] * [x_alpha, y_alpha] + [x_beta, y_beta]

        # width, height
        normalize_annotations[:, [2, 3]] = normalize_annotations[:, [2, 3]] * [x_alpha, y_alpha]
        return image, normalize_annotations, (pad_left, pad_top, origin_width, origin_height, scale)

    # def horizontal_flip(self, image, normalize_annotations):

    #     image = cv2.flip(image, 1)

    #     # cx, cy, width, height, class_index
    #     normalize_annotations[:, 0] = 1 - normalize_annotations[:, 0]
    #     return image, normalize_annotations
    def horizontal_flip(self, image, normalize_annotations):
        '''
        对图像和框进行水平翻转
        参数：
            image：提供图像
            normalize_annotations：提供归一化后的框信息，格式是[cx, cy, width, height, class_index]
        返回值：
            image, normalize_annotations
        '''

        # flipCode = 1 ，   水平，也就是x轴翻转
        # flipCode = 0，    垂直，也就是y轴翻转
        # flipCode = -1，   对角翻转，x和y都发生翻转
        image = cv2.flip(image, flipCode=1)
        #image = np.fliplr(image)
        normalize_annotations = normalize_annotations.copy()

        # cx, cy, width, height
        # 0-1
        # (image_width - 1) / image_width
        image_width = image.shape[1]  # Height, Width, Channel
        normalize_annotations[:, 0] = (image_width - 1) / image_width - normalize_annotations[:, 0]
        return image, normalize_annotations

    def hsv_augment(self, image, hue_gain=0.015, saturation_gain=0.7, value_gain=0.4):
        '''
        对图像进行HSV颜色空间增广
        参数：
            hue_gain:          色调增益，最终增益系数为  random(-1, +1) * hue_gain + 1
            saturation_gain:   饱和度增益，最终增益系数为  random(-1, +1) * saturation_gain + 1
            value_gain:        亮度增益，最终增益系数为  random(-1, +1) * value_gain + 1
        返回值：
            image
        '''

        # 把增益值修改为最终的增益系数
        hue_gain = np.random.uniform(-1, +1) * hue_gain + 1
        saturation_gain = np.random.uniform(-1, +1) * saturation_gain + 1
        value_gain = np.random.uniform(-1, +1) * value_gain + 1

        # 把图像转换为HSV后并分解为H、S、V，3个通道
        hue, saturation, value = cv2.split(cv2.cvtColor(image, cv2.COLOR_BGR2HSV))

        # cv2.COLOR_BGR2HSV       ->  相对压缩过的，可以说是有点损失的
        # cv2.COLOR_BGR2HSV_FULL  ->  完整的

        # hue        ->  值域 0 - 179
        # saturation ->  值域 0 - 255
        # value      ->  值域 0 - 255

        # LUT，look up table
        # table  -> [[10, 255, 7], [255, 0, 0], [0, 255, 0]]
        # index  -> [2, 0, 1]
        # value  -> [[0, 255, 0], [10, 255, 7], [255, 0, 0]]

        dtype = image.dtype
        lut_base = np.arange(0, 256)
        lut_hue = ((lut_base * hue_gain) % 180).astype(dtype)
        lut_saturation = np.clip(lut_base * saturation_gain, 0, 255).astype(dtype)
        lut_value = np.clip(lut_base * value_gain, 0, 255).astype(dtype)

        # cv2.LUT(index, lut)
        changed_hue = cv2.LUT(hue, lut_hue)
        changed_saturation = cv2.LUT(saturation, lut_saturation)
        changed_value = cv2.LUT(value, lut_value)

        image_hsv = cv2.merge((changed_hue, changed_saturation, changed_value))
        return cv2.cvtColor(image_hsv, cv2.COLOR_HSV2BGR, dst=image)

    def load_mosaic(self, image_indice):
        '''
        加载图像，并使用马赛克增广
            - 先把1个image_indice指定的图，和其他3个随机图，拼接为马赛克
            - 使用随机仿射变换，输出image_size大小
            - 移除无效的框
            - 恢复框为normalize
        返回值：
            image[self.image_size x self.image_size], normalize_annotations
        '''

        # 在image_size * 0.5到image_size * 1.5之间随机一个中心
        # 马赛克的第一步是拼接为一个大图，即image_size * 2, image_size * 2
        x_center = int(random.uniform(self.image_size * 0.5, self.image_size * 1.5))
        y_center = int(random.uniform(self.image_size * 0.5, self.image_size * 1.5))

        num_images = len(self.provider)
        all_image_indices = [image_indice] + [random.randint(0, num_images - 1) for _ in range(3)]

        #  img1,  img2
        #  img3,  img4
        alignment_corner_point = [
            [1, 1],   # img1的角点相对于其宽高尺寸的位置
            [0, 1],   # img2的角点相对于其宽高尺寸的位置
            [1, 0],   # img3的角点相对于其宽高尺寸的位置
            [0, 0]    # img4的角点相对于其宽高尺寸的位置
        ]

        merge_mosaic_image_size = self.image_size * 2
        merge_mosaic_image = np.full((merge_mosaic_image_size, merge_mosaic_image_size, 3), self.border_fill_value, dtype=np.uint8)
        merge_mosaic_pixel_annotations = []

        for index, (image_indice, (corner_point_x, corner_point_y)) in enumerate(zip(all_image_indices, alignment_corner_point)):

            image, normalize_annotations, (image_width, image_height), (origin_width, origin_height), scale = self.load_image_with_uniform_scale(image_indice)
            corner_point_x = corner_point_x * image_width
            corner_point_y = corner_point_y * image_height

            x_offset = x_center - corner_point_x   # 先加目标图的x，再减去image上的x
            y_offset = y_center - corner_point_y

            M = np.array([
                [1, 0, x_offset],
                [0, 1, y_offset]
            ], dtype=np.float32)

            cv2.warpAffine(image, M, (merge_mosaic_image_size, merge_mosaic_image_size),
                dst=merge_mosaic_image,
                borderMode=cv2.BORDER_TRANSPARENT,
                flags=cv2.INTER_NEAREST)

            # left_small = max(-x_offset, 0)
            # top_small = max(-y_offset, 0)
            # right_small = image_width - max((x_offset + image_width) - merge_mosaic_image_size, 0)
            # bottom_small = image_height - max((y_offset + image_height) - merge_mosaic_image_size, 0)
            # left_large = max(x_offset, 0)
            # top_large = max(y_offset, 0)
            # right_large = min(x_offset + image_width, merge_mosaic_image_size)
            # bottom_large = min(y_offset + image_height, merge_mosaic_image_size)
            # merge_mosaic_image[top_large:bottom_large, left_large:right_large] = image[top_small:bottom_small, left_small:right_small]

            # 把框转换为像素单位，并且是[left, top, right, bottom, class_index]格式的
            pixel_annotations = nn_utils.convert_to_pixel_annotation(normalize_annotations, image_width, image_height)

            # 把框进行平移
            pixel_annotations = pixel_annotations + [x_offset, y_offset, x_offset, y_offset, 0]

            # 把所有框合并
            merge_mosaic_pixel_annotations.append(pixel_annotations)

        # 所有框拼接为一个矩阵
        merge_mosaic_pixel_annotations = np.concatenate(merge_mosaic_pixel_annotations, axis=0)

        # 如果框越界了，需要限制到范围内，inplace操作
        np.clip(merge_mosaic_pixel_annotations[:, :4], a_min=0, a_max=merge_mosaic_image_size-1, out=merge_mosaic_pixel_annotations[:, :4])

        # 随机仿射变换
        scale = random.uniform(0.5, 1.5)

        # 随机仿射变化
        #  1. 进行缩放
        #  2. 将large图的中心移动到，目标图(small)的中心上
        #       - large -> 1280 x 1280
        #       - small ->  640 x 640
        # 也知道中心是：self.image_size, self.image_size，缩放系数是scale
        #    x * M00 + 0 * m01 + x_offset
        #    ix * M00 + x_offset  =  dx
        #       已知一个解，指的是中心点的解
        #       large.center.x * M00 + x_offset = small.center.x
        #       x_offset = small.center.x - large.center.x * M00
        #                = small.center.x - large.center.x * scale
        #                = image_size * 0.5 - image_size * scale
        #                = image_size * (0.5 - scale)
        #
        #    指定的中心，有什么样的定义：
        M = np.array([
            [scale, 0, self.image_size * (0.5 - scale)],
            [0, scale, self.image_size * (0.5 - scale)]
        ], dtype=np.float32)

        merge_mosaic_image = cv2.warpAffine(merge_mosaic_image, M,
            (self.image_size, self.image_size),
            flags=cv2.INTER_LINEAR,
            borderMode=cv2.BORDER_CONSTANT,
            borderValue=(self.border_fill_value, self.border_fill_value, self.border_fill_value))

        # 使用M矩阵对框进行变换，达到目标位置
        num_targets = len(merge_mosaic_pixel_annotations)
        output_normalize_annotations = np.zeros((0, 5))

        if num_targets > 0:
            # 映射标注框到目标图，使用M矩阵
            targets_temp = np.ones((num_targets * 2, 3))

            # 内存排布知识
            # N x 5
            # N x 4 -> left, top, right, bottom, left, top, right, bottom, left, top, right, bottom,
            #       -> reshape(N x 2, 2)
            #       -> left, top
            #       -> right, bottom
            #       -> left, top
            #       -> right, bottom
            # 把box标注信息变成一行一个点
            targets_temp[:, :2] = merge_mosaic_pixel_annotations[:, :4].reshape(num_targets * 2, 2)

            # targets_temp ->  2N x 3
            # M -> 2 x 3
            # output: 2N x 2,
            merge_projection_pixel_annotations = merge_mosaic_pixel_annotations.copy()
            merge_projection_pixel_annotations[:, :4] = (targets_temp @ M.T).reshape(num_targets, 4)

            # 处理框
            # 1. 裁切到图像范围
            # 2. 过滤掉无效的框
            np.clip(merge_projection_pixel_annotations[:, :4], a_min=0, a_max=self.image_size-1, out=merge_projection_pixel_annotations[:, :4])

            # 过滤无效的框
            projection_box_width = merge_projection_pixel_annotations[:, 2] - merge_projection_pixel_annotations[:, 0] + 1
            projection_box_height = merge_projection_pixel_annotations[:, 3] - merge_projection_pixel_annotations[:, 1] + 1
            original_box_width = merge_mosaic_pixel_annotations[:, 2] - merge_mosaic_pixel_annotations[:, 0] + 1
            original_box_height = merge_mosaic_pixel_annotations[:, 3] - merge_mosaic_pixel_annotations[:, 1] + 1

            area_projection = projection_box_width * projection_box_height
            area_original = original_box_width * original_box_height

            aspect_ratio = np.maximum(projection_box_width / (projection_box_height + 1e-6), projection_box_height / (projection_box_width + 1e-6))

            # 保留的条件分析
            # 1. 映射后的框，宽度必须大于2
            # 2. 映射后的框，高度必须大于2
            # 3. 裁切后的面积 / 裁切前的面积 > 0.2
            # 4. max(宽高比，高宽比) < 20
            keep_indices = (projection_box_width > 2) & \
                           (projection_box_height > 2) & \
                           (area_projection  / (area_original * scale + 1e-6) > 0.2) & \
                           (aspect_ratio < 20)
            merge_projection_pixel_annotations = merge_projection_pixel_annotations[keep_indices]
            output_normalize_annotations = nn_utils.convert_to_normalize_annotation(merge_projection_pixel_annotations, self.image_size, self.image_size)

        return merge_mosaic_image, output_normalize_annotations

    def load_image_with_uniform_scale(self, image_indice):
        '''
        加载图像，并进行长边等比缩放到self.image_size大小
        返回值：
            image, normalize_annotations, (image_resized_width, image_resized_height)
        '''
        image, normalize_annotations, (image_width, image_height) = self.provider[image_indice]
        to_image_size_ratio = self.image_size / max(image_width, image_height)

        if to_image_size_ratio != 1:

            # 如果不需要增广（评估阶段），并且缩放系数小于1，就使用效果比较好的插值方式
            if to_image_size_ratio < 1 and not self.augment:
                interp = cv2.INTER_AREA     # 速度慢，效果好，区域插值
            else:
                interp = cv2.INTER_LINEAR   # 速度快，效果也还ok，线性插值

            #image = cv2.resize(image, (0, 0), fx=to_image_size_ratio, fy=to_image_size_ratio, interpolation=interp)
            image = cv2.resize(image, (int(to_image_size_ratio * image_width), int(to_image_size_ratio * image_height)), interpolation=interp)

        image_resized_height, image_resized_width = image.shape[:2]
        return image, normalize_annotations, (image_resized_width, image_resized_height), (image_width, image_height), to_image_size_ratio

    @staticmethod
    def collate_fn(batch):
        '''
        属于dataset.__getitem__之后，dataloader获取数据之前
        获取数据之前，指：for batch_index, (images, labels) in enumerate(dataloader):
        在这里需要准备一个image_id

        因为这里预期返回的内容有：
        images[torch.FloatTensor]，normalize_annotations[Nx6][image_id, class_index, cx, cy, width, height], visual_info
        - visual_info指，给visdom用来显示的东西。返回batch中某一张图的信息，[image, annotations, image_id]
        '''
        # batch = [[image, annotations], [image, annotations]]
        images, original_images, normalize_annotations, original_normalize_annotations, restore_info = zip(*batch)
        for index, annotations in enumerate(normalize_annotations):
            annotations[:, 0] = index

        # 准备visual_info，用来显示的东西， image, annotations, image_id
        visual_image_id = random.randint(0, len(original_images)-1)
        visual_image = original_images[visual_image_id]
        visual_annotations = original_normalize_annotations[visual_image_id]
        visual_info = visual_image_id, visual_image, visual_annotations, restore_info

        normalize_annotations = torch.cat(normalize_annotations, dim=0)
        images = torch.stack(images, dim=0)
        return images, normalize_annotations, visual_info

if __name__ == "__main__":

    from tqdm import tqdm

    voc_provider = data_provider.VOCProvider("/data-rbd/wish/four_lesson/dataset/voc2007/VOCdevkitTest/VOC2007/")
    dataset = Dataset(False, 640, voc_provider, 32)

    for item in tqdm(range(len(dataset))):
        val = dataset[item]


import sys, time, telepot, unicodedata, urllib3, wikiquote, random
from telepot.loop import MessageLoop
from urllib.request import urlopen, Request #as uReq
from bs4 import BeautifulSoup as soup
#BeautifulSoup + Wikiquote part
def scrap():
    url = 'URL/'
    req = urlopen(url)
    page = req.read()
    req.close()
    page_soup = soup(page, "html.parser")
    containers = page_soup.find("span", {"id":"ajaxtemp"}).text.strip()
    norm_containers = unicodedata.normalize('NFKD', containers).encode('ascii', 'ignore')+ b"\n"
    the_quote = random.choice(wikiquote.quotes('Helen Keller')).encode('UTF-8')
    output = norm_containers +  the_quote
    return output
#Telepot part
def handle(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)
    print(content_type, chat_type, chat_id)

    if content_type == 'text':
        bot.sendMessage(chat_id, scrap())

TOKEN = 'WHATEVER BOT FATHER SAYS'

bot = telepot.Bot(TOKEN)
MessageLoop(bot, handle).run_as_thread()
print ('Listening ...')

# Keep the program running.
while 1:
    time.sleep(1)


import time
from scipy.spatial.transform import Rotation as R
import scipy
import numpy as np
import matplotlib.pyplot as plt
from utils import NOISE_FILENAME

class FTSensor:
    def __init__(self, robot):
        self.frequency = 1/500
        self.robot = robot
        self.noiseMean = 0
        self.noiseSTD = 1
        self.acceleration = []
        self.filename = NOISE_FILENAME

    def convertForceInBase2TCP(self,ftInBase):
        tcpPose = self.robot.getActualTCPPose()
        rot = R.from_rotvec(tcpPose[3:6])
        rMatrix = scipy.linalg.inv(rot.as_matrix())
        return rMatrix @ ftInBase[0:3]

    def saveNoiseValues(self):
        print("Saving",self.filename)
        with open(self.filename, 'w+') as file:
            string = str(self.noiseMean) + " " + str(self.noiseSTD) + " " + str(self.acceleration)[1:-1]
            file.write(string)

    def noiseFT(self, duration):
        print("Recording noise F/T sensor!")

        durTime = time.time()
        self.robot.zeroFtSensor()
        accelerations = []
        noiseMagnitudeOfForce = []
        while time.time() - durTime < duration:
            startTime = time.time()

            ftInBase = self.robot.getActualTCPForce()
            ftInTCP = self.convertForceInBase2TCP(ftInBase)

            noiseMagnitudeOfForce.append(np.linalg.norm(ftInTCP))

            accelerations.append(self.robot.getActualToolAccelerometer())

            diff = time.time() - startTime
            if(diff < self.frequency):
                time.sleep(self.frequency - diff)

        self.noiseMean = np.asarray(noiseMagnitudeOfForce).mean()
        self.noiseSTD = np.asarray(noiseMagnitudeOfForce).std()
        self.acceleration = np.mean(np.asarray(accelerations),axis=0)
        self.saveNoiseValues()
        print("Noise mean:", self.noiseMean, "Noise std:", self.noiseSTD,"Acceleration:",self.acceleration)


"""add comment

Revision ID: d088fb3bb7bd
Revises: dc67a31159b7
Create Date: 2016-08-02 15:25:02.523107

"""

# revision identifiers, used by Alembic.
revision = 'd088fb3bb7bd'
down_revision = 'dc67a31159b7'

from alembic import op
import sqlalchemy as sa

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('comments',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('content', sa.Text(), nullable=True),
    sa.Column('content_html', sa.Text(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.Column('disabled', sa.Boolean(), nullable=True),
    sa.Column('author_id', sa.Integer(), nullable=True),
    sa.Column('blog_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['author_id'], ['users.id'], ),
    sa.ForeignKeyConstraint(['blog_id'], ['blogs.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_comments_created_at'), 'comments', ['created_at'], unique=False)
    ### end Alembic commands ###

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_comments_created_at'), table_name='comments')
    op.drop_table('comments')
    ### end Alembic commands ###


from flask import Blueprint, send_file
import os

NAME = 'images'
page = Blueprint(NAME, __name__)

def initialize_app(app):
    app.register_blueprint(page, url_prefix="/")

@page.route("/")
def index():
    return send_file("static/index.html")

@page.route("/images/<image>")
def images(image):
    try:
        respond = send_file(os.path.join(os.path.abspath('.'), 'app/static/images/' + image))
    except FileNotFoundError:
        respond = send_file(os.path.join(os.path.abspath("."), "app/static/index.html"))
    return respond

@page.after_app_request
def after_request(response):
    if 'Access-Control-Allow-Origin' not in response.headers:
        response.headers.add('Access-Control-Allow-Origin', '*')
    return response