"""
This module provides a set of mix-ins to be used throughout PolicyBrain models.
To read more about Django model mix-ins, check out the following links:
https://docs.djangoproject.com/en/2.0/topics/db/models/#abstract-base-classes
http://blog.kevinastone.com/django-model-behaviors.html
"""

from django.db import models
from . import param_formatters

class Fieldable(models.Model):
    """
    Mix-in for providing logic around formatting raw GUI input fields
    """

    class Meta:
        abstract = True

    def set_fields(self, upstream_objs, nonparam_fields=None):
        """
        Parse raw fields
            1. Only keep fields that user specifies
            2. Map TB names to TC names
            3. Do more specific type checking--in particular, check if
               field is the type that Tax-Calculator expects from this param
        """
        default_data = {}
        for obj in upstream_objs:
            dd = obj.default_data(start_year=self.start_year,
                                                     metadata=True)
            default_data.update(dd)

        gui_field_inputs, failed_lookups = param_formatters.parse_fields(
            self.raw_gui_field_inputs,
            default_data
        )

        if failed_lookups:
            # distinct elements
            potential_failed_lookups = set(failed_lookups)
            # only keep parameters that used to be in the upstream package
            set_failed_lookups = potential_failed_lookups - nonparam_fields
            if self.deprecated_fields is None:
                self.deprecated_fields = []
            # drop parameters that we already know are deprecated
            set_failed_lookups.difference_update(self.deprecated_fields)
            self.deprecated_fields += list(set_failed_lookups)

        self.gui_field_inputs = gui_field_inputs

    def pop_extra_errors(self, errors_warnings):
        """
        Removes errors on extra parameters
        """
        for project in errors_warnings:
            for action in ['warnings', 'errors']:
                params = list(errors_warnings[project][action].keys())
                for param in params:
                    if param not in self.raw_gui_field_inputs:
                        errors_warnings[project][action].pop(param)

    def get_model_specs(self):
        """
        Stub to remind that this part of the API is needed
        """
        raise NotImplementedError()

class DataSourceable(models.Model):
    """
    Mix-in for providing data_source field and methods that access it
    """

    class Meta:
        abstract = True

    # data source for model
    data_source = models.CharField(
        default="PUF",
        blank=True,
        null=True,
        max_length=20)

    @property
    def use_puf_not_cps(self):
        # which file to use, front-end not yet implemented
        if self.data_source == 'PUF':
            return True
        else:
            return False


import numpy as np
from neuron import h, gui
from module.simulation import real_morphology_model_2
from module.probability import ParameterSet, RandomVariable
from module.noise import inv_cov_mat
from module.protocol_test import run_protocol_simulations_c
import tables as tb
from functools import  partial
import os
import time

startTime = time.time()

p_names = ['Ra', 'gpas', 'ffact']
p_res = [30, 30, 30]  # Parameters resolution
p_range = [[50., 200.], [0.00002, 0.0005], [1., 5.]]
p_mean = [100., 0.0001, 2.]     # Fixed prior mean
p_std = [100., 0.0003, 2.]      # Fixed prior std

dt = 0.1
samples_num = 15000
noise_rep = 45      # How many repetition while params are fixed
fixed_param_num = 1  # The number of fixed parameters sampled from prior
batch_size = 30000     # Set it to "None" to compute the whole parameter space in one stock

path_target_traces = "/Users/admin/GD/PROJECTS/SPE/data/160526-C2-row-dt0.1-1.5s-corrected.txt"
path_neuron_model = 'load_file("/Users/admin/PROJECTS/SPE/parameter-inference/exp/exp_160526/load_new_passive.hoc")'
path_stimulus = "/Users/admin/PROJECTS/SPE/parameter-inference/exp/exp_160526/new_stim-dt0.1-1.5sec.txt"
working_path = "/Users/admin/PROJECTS/SPE/parameter-inference/exp/exp_160526"

# ----------------------
# Construct noise model
# ----------------------
def aut_corr_func(x):
    return A*np.exp(-x/T1)*np.cos(2*np.pi/T2*x+phi)

# fitted model parameters:
A, T1, T2, phi = [ 2.79388925e-02,  3.91934291e+02, 9.54439765e+02, -9.99502661e+01]

# def aut_corr_func(x):
#     return p[0]*np.exp(-np.abs(x)/p[1])*np.cos(2*np.pi/p[2]*np.abs(x))

# ---------------------
# Load target traces
# ---------------------
target_traces = np.loadtxt(path_target_traces)
# target_traces = target_traces.T
target_traces = target_traces.reshape((1, noise_rep, samples_num))
print((target_traces.shape))

# -----------------------------
# --- Load NEURON morphology
# -----------------------------
h(path_neuron_model)
# Set the appropriate "nseg"
for sec in h.allsec():
    sec.Ra = 100
h('forall {nseg = int((L/(0.1*lambda_f(100))+.9)/2)*2 + 1}')  # If Ra_max = 105 dend.nseg = 21 and soma.nseg = 1

# Set up initial parameters (in this case only 1)
initial_params = [{'Ra': None, 'gpas': None, 'ffact': None}]

t_vec = np.linspace(0, (samples_num-1)*dt, samples_num)

# --------------------------------------
# Construct or load invcovmat
# ---------------------------------------
invcovmat_path = None  # Set to None if covmat is to be computed
if invcovmat_path is None:
    print("Constructing covmat and invcovmat...")
    covmat, invcovmat = inv_cov_mat(aut_corr_func, t_vec)
    print("Inverse covariance matrix is loaded to memory!")
    print((invcovmat.shape))
    # np.savetxt("invcovmat.txt", invcovmat)
    # print("Inverse covmat is saved, next time you can load it.")
else:
    print("Loading invcovmat from given file")
    invcovmat = np.loadtxt(invcovmat_path)

# Set up parameters using prior information about them (fix the range we are assuming the true parameter)
prior_params = []
for idx, item in enumerate(p_names):
    prior_params.append(RandomVariable(name=item, range_min=p_range[idx][0], range_max=p_range[idx][1],
                                       resolution=p_res[idx], sigma=p_std[idx], mean=p_mean[idx]))

prior_set = ParameterSet(*prior_params)
prior_set.batch_len = batch_size
if batch_size != None:
    prior_set.isBatch = True
else:
    prior_set.isBatch = False
prior_set.create_batch()

# Save parameter informations
# Create database for data
database = tb.open_file(os.path.join(working_path, "paramsetup.hdf5"), mode="w")

# Save param initialization
param_init = []
for param in prior_set.params:
    param_init.append(param.get_init())
param_init = np.array(param_init, dtype=str)

database.create_array(database.root, "params_init",
                      title="Parameter space initializer. True value is about to set up!",
                      atom=tb.Atom.from_dtype(param_init.dtype),
                      shape=param_init.shape, obj=param_init)

# Save fixed params and target_traces
fixed_p = np.ndarray(shape=(len(initial_params), len(prior_set.params)))
for idx, item in enumerate(initial_params):
    for i, param in enumerate(prior_set.params):
        fixed_p[idx, i] = item[param.name]

database.create_array(database.root, "fixed_params",
                      title="True value for each parameter in given simulation",
                      atom=tb.Atom.from_dtype(fixed_p.dtype),
                      shape=fixed_p.shape, obj=fixed_p)

database.flush()
print("Parameter space initialization data saved to disk")
print(database)
database.close()

# RUN INFERENCE --------------------------------------------------------------------------------------------

# Load stimulus
s = np.loadtxt(path_stimulus)
s = s[:-1]
print(("stimulus len: ", s.shape))
model = partial(real_morphology_model_2, stim=s)

if __name__ == '__main__':
    run_protocol_simulations_c(model=model, target_traces=target_traces, inv_covmat=invcovmat, param_set=prior_set,
                               working_path=working_path)

runningTime = (time.time()-startTime)/60
print(("\n\nThe script was running for %f minutes" % runningTime))


import csv

from django.http import HttpResponse
from django.views.generic import View, TemplateView
from django_filters.views import FilterView
from django.core.cache import cache

from currency.filters import RateFilter
from currency.models import Rate
from currency import model_choices as mch
from currency.utils import genetere_rate_cache_key

class LatestRates(FilterView):
    filterset_class = RateFilter
    queryset = Rate.objects.all()
    template_name = 'rate.html'
    paginate_by = 10
    ordering = ['-created']
    context_object_name = 'rates'

    def get_context_data(self, *args, **kwargs):
        from urllib.parse import urlencode
        context = super().get_context_data(*args, **kwargs)

        query_params = dict(self.request.GET.items())
        if 'page' in query_params:
            del query_params['page']
        context['query_params'] = urlencode(query_params)

        return context

    # def get_paginate_by(self, queryset):
    #     super().get_paginate_by()

    # @property
    # def paginate_by(self):
    #     paginate = self.request.GET.get('paginate-by')
    #     return paginate

# Create your views here
class RateCSV(View):
    HEADERS = [
        'id',
        'created',
        'currency',
        'buy',
        'sale',
        'source',
    ]

    def get(self, request):
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename="rates.csv"'
        writer = csv.writer(response)

        writer.writerow(self.HEADERS)

        for rate in Rate.objects.all().iterator():
            row = [
                getattr(rate, f'get_{attr}_display')()
                if hasattr(rate, f'get_{attr}_display') else getattr(rate, attr)
                for attr in self.HEADERS
            ]

            writer.writerow(row)
            # writer.writerow(map(str, [
            #     rate.id,
            #     rate.created,
            #     rate.get_currency_display(),
            #     rate.buy,
            #     rate.sale,
            #     # rate.get_source_display(),
            # ]))

        return response

class LatestRate(TemplateView):
    template_name = 'latest-rates.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # context['rates'] = Rate.objects.filter(course=mch.SR_PRIVAT, currency=mch.CURR_USD).last()
        # rates = {
        #     'privatBank': [Rate.objects.filter(source=mch.SR_PRIVAT, currency=mch.CURR_USD).last(),
        #                    Rate.objects.filter(source=mch.SR_PRIVAT, currency=mch.CURR_USD).last()],
        #     'MonoBank': [Rate.objects.filter(source=mch.SR_PRIVAT, currency=mch.CURR_USD).last(),
        #                  Rate.objects.filter(source=mch.SR_PRIVAT, currency=mch.CURR_USD).last()],

        rates = []
        for bank in mch.SOURCE_CHOICES:
            source = bank[0]
            for curr in mch.CURRENCY_CHOICES:
                currency = curr[0]
                cache_key = genetere_rate_cache_key(source, currency)

                rate = cache.get(cache_key)
                if rate is None:
                    rate = Rate.objects.filter(source=source, currency=currency).order_by('created').last()
                    if rate:
                        rate_dict = {
                            'currency': rate.currency,
                            'source': rate.source,
                            'sale': rate.sale,
                            'buy': rate.buy,
                            'created': rate.created,
                        }
                        rates.append(rate_dict)
                        cache.set(cache_key, rate_dict, 60 * 15)  # 15 minutes
                        # cache.set(cache_key, rate_dict, 5)  # 5 seconds
                else:
                    rates.append(rate)

            context['rates'] = rates
            # Rate.objects.filter(source=mch.SR_PRIVAT, currency=mch.CURR_USD).order_by('-created')[0]
            return context

'''
source PrivatBank - latest USD, latest UER
source MonoBank - latest USD, latest UER
'''


import json
from sqlalchemy import create_engine, desc
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import sessionmaker
from flask_login import current_user
import re
from werkzeug.security import generate_password_hash
import socket
import datetime
from common.system import SysLog, User, AuditTrace
from common.MESLogger import MESLogger
from database.db_operate import DB_URL
from common.system import User

engine = create_engine(DB_URL, max_overflow=0,  # 超过连接池大小外最多创建的连接
                       pool_size=5,  # 连接池大小
                       pool_timeout=30,  # 池中没有线程最多等待的时间，否则报错
                       pool_recycle=-1,  # 多久之后对线程池中的线程进行一次连接的回收（重置）
                       echo=True
                       )
conn = engine.connect()
Session = sessionmaker(bind=engine)
db_session = Session()

from sqlalchemy import MetaData

metadata = MetaData()
from sqlalchemy import Table

Base = automap_base()
Base.prepare(engine, reflect=True)

logger = MESLogger('./logs', 'log')

# 插入日志OperationType OperationContent OperationDate UserName ComputerName IP
def insertSyslog(operationType, operationContent, userName):
    try:
        if operationType == None: operationType = ""
        if operationContent == None:
            operationContent = ""
        else:
            operationContent = str(operationContent)
        if userName == None: userName = ""
        ComputerName = socket.gethostname()
        db_session.add(
            SysLog(OperationType=operationType, OperationContent=operationContent,
                   OperationDate=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), UserName=userName,
                   ComputerName=ComputerName, IP=socket.gethostbyname(ComputerName)))
        db_session.commit()
        db_session.close()
    except Exception as e:
        db_session.rollback()
        print(e)
        logger.error(e)

def insert(data):
    '''
    :param data: 需要添加的数据
    :return:
    '''
    if isinstance(data, dict) and len(data) > 0:
        try:
            tableName = str(data.get("tableName"))
            obj = Base.classes.get(tableName.lower())
            ss = obj()
            for key in data:

                if key != "ID" and key != "tableName" and key != "id":
                    if key == "Password":
                        setattr(ss, key, generate_password_hash(data['Password']))
                        if tableName == "User":
                            setattr(ss, "Creater", current_user.Name)
                    elif key == "WorkNumber":
                        ocal = db_session.query(User).filter(User.WorkNumber == data['WorkNumber']).first()
                        if ocal != None:
                            return "工号重复，请重新录入！"
                        else:
                            setattr(ss, key, data['WorkNumber'])
                    else:
                        setattr(ss, key, data[key])
            db_session.add(ss)
            aud = AuditTrace()
            aud.TableName = tableName
            aud.Operation = current_user.Name + " 对表" + tableName + "添加一条数据！"
            aud.DeitalMSG = "用户：" + current_user.Name + " 对表" + tableName + "添加一条数据！"
            aud.ReviseDate = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            aud.User = current_user.Name
            db_session.add(aud)
            db_session.commit()
            db_session.close()
            return {"code": "200", "message": "添加成功"}
        except Exception as e:
            print(e)
            db_session.rollback()
            logger.error(e)
            insertSyslog("error", "%s数据添加报错：" % tableName + str(e), current_user.Name)
            return {"code": "500", "message": "请求错误", "data": "%s数据添加报错：" % tableName + str(e)}

def delete(data):
    '''
    :param data: 要删除的数据
    :return:
    '''
    try:
        tableName = str(data.get("tableName"))
        jsonstr = json.dumps(data.to_dict())
        if len(jsonstr) > 10:
            jstr = data.get("delete_data")
            jsonnumber = re.findall(r"\d+\.?\d*", jstr)
            for key in jsonnumber:
                try:
                    sql = "delete from " + "" + tableName + " where ID = " + str(key)
                    db_session.execute(sql)
                    aud = AuditTrace()
                    aud.TableName = tableName
                    aud.Operation = current_user.Name + " 对表" + tableName + "中的ID为" + key + "的数据做了删除操作！"
                    aud.DeitalMSG = "用户：" + current_user.Name + " 对表" + tableName + "中的ID为" + key + "的数据做了删除操作！"
                    aud.ReviseDate = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                    aud.User = current_user.Name
                    db_session.add(aud)
                    db_session.commit()
                    db_session.close()
                except Exception as ee:
                    print(ee)
                    db_session.rollback()
                    insertSyslog("error", "删除户ID为" + str(id) + "报错Error：" + str(ee), current_user.Name)
                    return {"code": "500", "message": "请求错误", "data": "删除户ID为" + str(id) + "报错Error：" + str(ee)}
            return {"code": "200", "message": "删除成功"}
    except Exception as e:
        db_session.rollback()
        logger.error(e)
        insertSyslog("error", "%s数据删除报错：" % tableName + str(e), current_user.Name)
        return {"code": "500", "message": "请求错误", "data": "%s数据删除报错：" % tableName + str(e)}

def update(data):
    '''
    :param data: 更新数据
    :return:
    '''
    if isinstance(data, dict) and len(data) > 0:
        try:
            tableName = str(data.get("tableName"))
            obj = Base.classes.get(tableName.lower())
            ss = obj()
            ID = data.get('ID')
            oclass = db_session.query(obj).filter_by(ID=int(data.get('ID'))).first()
            if oclass:
                for key in data:
                    if hasattr(oclass, key) and key != 'ID' and key != 'tableName' and key != "id" and key != "Creater":
                        if key == "Password":
                            setattr(oclass, key, generate_password_hash(data['Password']))
                        elif key == "WorkNumber":
                            ocal = db_session.query(User).filter(User.WorkNumber == data['WorkNumber']).first()
                            if ocal != None:
                                if oclass.WorkNumber != data['WorkNumber']:
                                    return "工号重复，请重新录入！"
                            else:
                                setattr(oclass, key, data['WorkNumber'])
                        else:
                            setattr(oclass, key, data[key])
                db_session.add(oclass)
                aud = AuditTrace()
                aud.TableName = tableName
                aud.Operation = current_user.Name + " 对表" + tableName + "的数据做了更新操作！"
                aud.DeitalMSG = "用户：" + current_user.Name + " 对表" + tableName + "ID为：" + ID + "做了更新操作"
                aud.ReviseDate = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                aud.User = current_user.Name
                db_session.add(aud)
                db_session.commit()
                db_session.close()
                return {"code": "200", "message": "修改成功"}
            else:
                return {"code": "200", "message": "修改成功", "data": "当前记录不存在"}
        except Exception as e:
            db_session.rollback()
            logger.error(e)
            insertSyslog("error", "%s数据更新报错：" % tableName + str(e), current_user.Name)
            return {"code": "500", "message": "请求错误", "data": "%s数据更新报错：" % tableName + str(e)}

def select(data):
    '''
    :param tablename: 查询表
    :param pages: 页数
    :param rowsnumber: 一页多少行
    :return:
    '''
    try:
        tableName = data.get("tableName")
        pages = data.get("offset")
        if pages == None or pages == "":
            pages = ""
        else:
            rowsnumber = int(data.get("limit"))
            pages = int(data.get("offset")) * rowsnumber
        newTable = Table(tableName, metadata, autoload=True, autoload_with=engine)
        columns = ""
        for column in newTable.columns:
            if columns == "":
                columns = str(column).split(".")[1]
            else:
                columns = columns + "," + str(column).split(".")[1]
        params = ""
        searchModes = data.get("searchModes")
        for key in data.keys():
            if key != "offset" and key != "limit" and key != "tableName" and key != "":
                if searchModes == None or searchModes == "":  # 模糊查询
                    if key != "searchModes":
                        if params == "":
                            params = key + " like '%" + data[key] + "%'"
                        else:
                            params = params + " AND " + key + " like '%" + data[key] + "%'"
                else:  # 精确查询
                    if key != "searchModes":
                        if params == "":
                            params = key + " = '" + data[key] + "'"
                        else:
                            params = params + " AND " + key + " = '" + data[key] + "'"
        if pages == "":
            if params == "":
                sql = "select " + columns + " from " + tableName + " ORDER BY ID DESC"
                sqlcount = "select count(ID) from " + tableName
            else:
                sql = "select " + columns + " from " + tableName + " where " + params + " ORDER BY ID DESC"
                sqlcount = "select count(ID) from " + tableName + " where " + params
        else:
            if params == "":
                sql = "select " + columns + " from " + tableName + "  ORDER BY ID DESC LIMIT " + str(pages) + "," + str(
                    rowsnumber)
                sqlcount = "select count(ID) from " + tableName
            else:
                sql = "select " + columns + " from " + tableName + " where " + params + "ORDER BY ID DESC LIMIT " + str(
                    pages) + "," + str(rowsnumber)
                sqlcount = "select count(ID) from " + tableName + " where " + params
        re = db_session.execute(sql).fetchall()
        recount = db_session.execute(sqlcount).fetchall()
        db_session.close()
        dict_list = []
        y = 0
        for i in re:
            # y = y + 1
            # if y == 1:
            #     continue
            # dir = {}
            # j = 0
            # for column in newTable.columns:
            #     if isinstance(i[j], datetime.datetime) == True:
            #         dir[str(column).split(".")[1]] = datetime.datetime.strftime(i[j],'%Y-%m-%d %H:%M:%S')
            #     else:
            #         dir[str(column).split(".")[1]] = i[j]
            #     j = j+1
            # dict_list.append(dir)
            dir = {}
            column_list = columns.split(",")
            for column in column_list:
                if isinstance(i[column], datetime.datetime) == True:
                    dir[column] = datetime.datetime.strftime(i[column], '%Y-%m-%d %H:%M:%S')
                else:
                    dir[column] = i[column]
            dict_list.append(dir)
        return {"code": "200", "message": "请求成功", "data": {"total": recount[0][0], "rows": dict_list}}
    except Exception as e:
        print(e)
        db_session.rollback()
        logger.error(e)
        insertSyslog("error", "查询报错Error：" + str(e), current_user.Name)
        return {"code": "500", "message": "请求错误", "data": "查询报错Error：" + str(e)}


import json

from app.main import bp
from app.models import Hieroglyph

from flask import render_template, request, current_app, url_for
from flask_login import login_required

def create_model_generator(model):
    for i, h in enumerate(model):
        model[i].onyomi = ', '.join(json.loads(h.onyomi))
        model[i].kunyomi = ', '.join(json.loads(h.kunyomi))
        model[i].translation = ', '.join(json.loads(h.translation))
    model_gen = ((model[n: n + 4], model[n:])[n + 4 > len(model)] for n in range(0, len(model), 4))
    return model_gen

@bp.route('/')
@bp.route('/index')
@login_required
def index():
    page = request.args.get('page', 1, type=int)
    model = Hieroglyph.query.filter_by(level=1).paginate(page, current_app.config['HIEROGLYPHS_PER_PAGE'])
    next_url = (None, url_for('main.index', page=model.next_num))[model.has_next]
    prev_url = (None, url_for('main.index', page=model.prev_num))[model.has_prev]
    return render_template('main/index.html', model=create_model_generator(model.items),
                           next_page=next_url, prev_page=prev_url)


# -*-coding=utf-8-*-

# @Time : 2019/6/20 17:54
# @File : semaphore_demo.py
import threading,time,datetime,random
# class MyThread(threading.Thread):

# 	def run(self):
# 		if semaphore.acquire():
# 			print(datetime.datetime.now().strftime('%H:%M:%S'),self.name)
# 			time.sleep(2)
# 			semaphore.release()

# if __name__=='__main__':
# 	semaphore = threading.Semaphore(5)
# 	threads=[]

# 	for i in range(100):
# 		threads.append(MyThread())

# 	for t in threads:
# 		t.start()

class MyThread(threading.Thread):

	def run(self):
		# if semaphore.acquire():
		# threading.Thread.
		time.sleep(random.random())
		print(datetime.datetime.now().strftime('%H:%M:%S'),self.name)
			# semaphore.release()

if __name__=='__main__':
	threads=[]

	for i in range(100):
		threads.append(MyThread())

	for t in threads:
		t.start()

	for t in threads:
		t.join()

	print('Done')


from PIL import Image, ImageDraw, ImageFont, ImageFilter
import random
import glob
import numpy as np
import os
import cv2
import argparse
import pickle
from argparse import RawDescriptionHelpFormatter
import fnmatch
import json
import  shutil

import traceback
import copy

'''
1. 从文字库随机选择10个字符/从文字库中随机抽出字符(构建字典：ID=>汉字)
2. 生成图片/生成字体(白底黑体)图像,背景图像,之后将字体图像帖和到背景图像中
    a.生成单个字体图像(包含多个字符)
        对图像进行旋转,模糊,腐蚀处理
        对图像进行等比例缩放
        查找包含字体的最小矩形(去除空余部分,使字体图像最小)
    b.贴合到背景图像的固定位置(背景图像从背景文件中选取):数组比大小,取小值
3. 随机使用函数
'''

# 从文字库中随机选择n个字符

def sto_choice_from_info_str(info_str, quantity=10):
    start = random.randint(0, len(info_str)-11)
    end = start + 10
    random_word = info_str[start:end]
    return random_word

def random_word_color():
    # 目前是黑色,更改这个函数还会改变字的颜色
    font_color_choice = [[54,54,54],[54,54,54],[105,105,105]]
    font_color = random.choice(font_color_choice)
    noise = np.array([random.randint(0,10),random.randint(0,10),random.randint(0,10)])
    font_color = (np.array(font_color) + noise).tolist()
    return tuple(font_color)

# 生成一张图片
def create_an_image(bground_path, width, height):
    bground_list = os.listdir(bground_path)
    bground_choice = random.choice(bground_list)
    bground = Image.open(bground_path+bground_choice)
    x, y = random.randint(0,bground.size[0]-width), random.randint(0, bground.size[1]-height)
    bground = bground.crop((x, y, x+width, y+height))
    return bground

# 模糊函数
def darken_func(image):
    #.SMOOTH
    #.SMOOTH_MORE
    #.GaussianBlur(radius=2 or 1)
    # .MedianFilter(size=3)
    # 随机选取模糊参数
    filter_ = random.choice(
                            [ImageFilter.SMOOTH,
                            ImageFilter.SMOOTH_MORE,
                            ImageFilter.GaussianBlur(radius=1.3)]
                            )
    image = image.filter(filter_)

    return image

# 旋转函数/cv2仿射里有旋转
def rad(x):
    return x * np.pi / 180
def rotate_func(img):
    rotate = np.random.randint(100,300)
    img = img.rotate(rad(rotate),expand=True)
    return img

# 噪声函数
def random_noise_func(img):
    img = np.asarray(img)
    img.flags.writeable =True
    for k in range(100):
        i = int(np.random.random()*img.shape[1])
        j = int(np.random.random()*img.shape[0])
        img[j,i,0] = random.randint(0,127)
        img[j,i,1] = random.randint(0,127)
        img[j,i,2] = random.randint(0,127)
    img = Image.fromarray(img)
    return img
def add_erode(img):
    img = np.asarray(img)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(2,2))
    erode_img = cv2.erode(img,kernel=kernel)
    img = Image.fromarray(erode_img)
    return img
def add_dilate(img):
    img = np.asarray(img)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
    dilate_img = cv2.dilate(img, kernel=kernel)
    img = Image.fromarray(dilate_img)
    return img

# 字体拉伸函数

def stretching_func(img):
    type = np.random.choice([0,1,2,3])
    w, h = img.size
    if type == 1 :
        img= img.resize((int(w*1.2),int(h)))

    elif type == 2:
        img = img.resize((int(w),int(h*1.2)))

    elif type == 3:
        img = img.resize((int(w*1.2),int(h*1.2)))
    else:
        return img
    return img

# def stretching_func(img):
#     img = np.asarray(img)
#     w,h,_ = img.shape
#     fov = 42
#     # 镜头与图像间的距离，21为半可视角，算z的距离是为了保证在此可视角度下恰好显示整幅图像
#     z = np.sqrt(w ** 2 + h ** 2) / 2 / np.tan(rad(fov / 2))
#     # 齐次变换矩阵
#     rx = np.array([[1, 0, 0, 0],
#                    [0, np.cos(rad(random.random()*10)), -np.sin(rad(random.random()*10)), 0],
#                    [0, -np.sin(rad(random.random()*10)), np.cos(rad(random.random()*10)), 0, ],
#                    [0, 0, 0, 1]], np.float32)
#
#     ry = np.array([[np.cos(rad(random.random()*10)), 0, np.sin(rad(random.random()*10)), 0],
#                    [0, 1, 0, 0],
#                    [-np.sin(rad(random.random()*10)), 0, np.cos(rad(random.random()*10)), 0, ],
#                    [0, 0, 0, 1]], np.float32)
#
#     rz = np.array([[np.cos(rad(random.random()*10)), np.sin(rad(random.random()*10)), 0, 0],
#                    [-np.sin(rad(random.random()*10)), np.cos(rad(random.random()*10)), 0, 0],
#                    [0, 0, 1, 0],
#                    [0, 0, 0, 1]], np.float32)
#
#     r = rx.dot(ry).dot(rz)
#
#     # 四对点的生成
#     pcenter = np.array([h / 2, w / 2, 0, 0], np.float32)
#
#     p1 = np.array([0, 0, 0, 0], np.float32) - pcenter
#     p2 = np.array([w, 0, 0, 0], np.float32) - pcenter
#     p3 = np.array([0, h, 0, 0], np.float32) - pcenter
#     p4 = np.array([w, h, 0, 0], np.float32) - pcenter
#
#     dst1 = r.dot(p1)
#     dst2 = r.dot(p2)
#     dst3 = r.dot(p3)
#     dst4 = r.dot(p4)
#
#     list_dst = [dst1, dst2, dst3, dst4]
#
#     org = np.array([[0, 0],
#                     [w, 0],
#                     [0, h],
#                     [w, h]], np.float32)
#
#     dst = np.zeros((4, 2), np.float32)
#
#     # 投影至成像平面
#     for i in range(4):
#         dst[i, 0] = list_dst[i][0] * z / (z - list_dst[i][2]) + pcenter[0]
#         dst[i, 1] = list_dst[i][1] * z / (z - list_dst[i][2]) + pcenter[1]
#
#     warpR = cv2.getPerspectiveTransform(org, dst)
#     output = cv2.warpPerspective(img, warpR, (h, w))
#
#     # martrix = cv2.getRotationMatrix2D((cols/2,rows/2),10,1.1)
#     # output = cv2.warpAffine(img,martrix,(cols,rows))
#     img = Image.fromarray(output)
#     return img

# 随机选取文字贴合起始的坐标, 根据背景的尺寸和字体的大小选择
def random_x_y(bground_size, font_size):
    width, height = bground_size
    # 为防止文字溢出图片，x，y要预留宽高
    x = random.randint(0, width-font_size*10)
    y = random.randint(0, int((height-font_size)/2))
    return x, y

def random_font_size():
    font_size = random.randint(24,27)
    return font_size

def random_font(font_path):
    font_list = os.listdir(font_path)
    random_font = random.choice(font_list)
    return font_path + random_font
def creat_img(bg_path,info_str,font_path):
    random_word = sto_choice_from_info_str(info_str, 10)
    # 生成一张背景图片，已经剪裁好，宽高为32*280
    font_size = random_font_size()
    raw_image = create_an_image(bg_path, font_size * 10 + 10, font_size + 10)

    # 随机选取字体大小

    # 随机选取字体
    font_name = random_font(font_path)
    # 随机选取字体颜色
    font_color = random_word_color()
    # 随机选取文字贴合的坐标 x,y
    draw_x, draw_y = random_x_y(raw_image.size, font_size)

    # 将文本贴到背景图片
    font = ImageFont.truetype(font_name, font_size)

    draw = ImageDraw.Draw(raw_image)
    draw.text((draw_x, draw_y), random_word, fill=font_color, font=font)
    return raw_image,random_word
# 选取作用函数
def random_choice_in_process_func(img):
    type = [0,1,2,3,4,5,]
    type =np.random.choice(type,size=np.random.randint(0,6))
    img = random_noise_func(img)
    erode_or_dilate = False
    for i_type in type:
        if i_type == 0 :
            img = random_noise_func(img)
        if i_type == 1 :
            img = rotate_func(img)
        if i_type == 2 :
            img = darken_func(img)
        if i_type == 3:
            img = stretching_func(img)
        if i_type == 4 and erode_or_dilate == False:
            img = add_dilate(img)
            erode_or_dilate=True
        if i_type ==5 and erode_or_dilate == False:
            img = add_erode(img)
        else:
            img = img
    return img

def main(save_path, num, info_str):
    if not os.path.exists(save_path):
        os.makedirs(save_path)
    raw_image,random_word= creat_img(bg_path='./background/',info_str=info_str,font_path='./font/')
    # 随机选取10个字符
    # random_word = sto_choice_from_info_str(info_str, 10)
    # # 生成一张背景图片，已经剪裁好，宽高为32*280
    # font_size = random_font_size()
    # raw_image = create_an_image('./background/', font_size*10+10, font_size+10)
    #
    # # 随机选取字体大小
    #
    # # 随机选取字体
    # font_name = random_font('./font/')
    # # 随机选取字体颜色
    # font_color = random_word_color()
    # # 随机选取文字贴合的坐标 x,y
    # draw_x, draw_y = random_x_y(raw_image.size, font_size)
    #
    # # 将文本贴到背景图片
    # font = ImageFont.truetype(font_name, font_size)
    #
    # draw = ImageDraw.Draw(raw_image)
    # draw.text((draw_x, draw_y), random_word, fill=font_color, font=font)

    # raw_image = rotate_func(raw_image)
    # raw_image = stretching_func(raw_image)
    # # raw_image = stretching_func(raw_image)
    # raw_image = add_erode(raw_image)
    # raw_image = add_dilate(raw_image)
    #
    # raw_image = random_noise_func(raw_image)
    raw_image = random_choice_in_process_func(raw_image)

    # 保存文本信息和对应图片名称
    raw_image.save(os.path.join(save_path, random_word+'_'+str(num)+'.png'))

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Process some integers.')
    parser.add_argument('--input', '--i', type=str, default="./data/demo_input.txt",
                        help='input Chinese words list file path')
    parser.add_argument('--output', '--o', type=str, default="./out/train/",
                        help='output image files directory')
    parser.add_argument('--num', '--n', type=int, default=1000,
                        help='number of output files')
    args = parser.parse_args()

    # open file
    file_name = args.input
    output_path = args.output
    total = args.num
    with open(file_name, 'r', encoding='utf-8') as input_file:
        info_list = [part.strip().replace('\t', '') for part in input_file.readlines()]
        info_str = ''.join(info_list)

    for num in range(0, total):
        main(output_path, num, info_str)
        if num % 1000 == 0:
            print('[%d/%d]'%(num,total))


from flask import Flask

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Restaurant, Base, MenuItem

engine = create_engine('sqlite:///restaurantmenu.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()

app = Flask(__name__)

@app.route('/')
@app.route('/restaurants/<int:restaurant_id>/')
def restaurantMenu(restaurant_id):
	restaurant = session.query(Restaurant).first()
	items = session.query(MenuItem).filter_by(restaurant_id = restaurant.id).all()
	s = ""
	for item in items:
		s += "%s </br> %s </br> %s </br> </br>" % (item.name, item.price, item.description)
	return s

if __name__ == '__main__':
	app.debug = True
	app.run(host = '0.0.0.0', port = 5000)


"""Initial migration.

Revision ID: b3ec8f39b084
Revises:
Create Date: 2021-01-14 00:32:09.399304

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'b3ec8f39b084'
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('category',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('name', sa.String(), nullable=False),
                    sa.PrimaryKeyConstraint('id'),
                    sa.UniqueConstraint('name')
                    )
    op.create_table('user',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('name', sa.String(length=80), nullable=False),
                    sa.Column('description', sa.String(), nullable=True),
                    sa.Column('email', sa.String(length=120), nullable=False),
                    sa.Column('create_at', sa.String(), nullable=True),
                    sa.Column('password', sa.String(
                        length=1024), nullable=False),
                    sa.Column('user_type', sa.String(
                        length=50), nullable=True),
                    sa.PrimaryKeyConstraint('id'),
                    sa.UniqueConstraint('email')
                    )
    op.create_table('news',
                    sa.Column('id', sa.Integer(), nullable=False),
                    sa.Column('title', sa.String(length=1024), nullable=False),
                    sa.Column('subtitle', sa.Text(), nullable=True),
                    sa.Column('content', sa.Text(), nullable=False),
                    sa.Column('upvotes', sa.Integer(), nullable=True),
                    sa.Column('downvotes', sa.Integer(), nullable=True),
                    sa.Column('create_at', sa.String(), nullable=True),
                    sa.Column('approved', sa.Boolean(), nullable=True),
                    sa.Column('author', sa.Integer(), nullable=True),
                    sa.ForeignKeyConstraint(['author'], ['user.id'], ),
                    sa.PrimaryKeyConstraint('id'),
                    sa.UniqueConstraint('content'),
                    sa.UniqueConstraint('title')
                    )
    op.create_table('news_category',
                    sa.Column('news_id', sa.Integer(), nullable=True),
                    sa.Column('category_id', sa.Integer(), nullable=True),
                    sa.ForeignKeyConstraint(
                        ['category_id'], ['category.id'], ),
                    sa.ForeignKeyConstraint(['news_id'], ['news.id'], )
                    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('news_category')
    op.drop_table('news')
    op.drop_table('user')
    op.drop_table('category')
    # ### end Alembic commands ###


"""This module fetches rating counts for single or multiple ISBNs to
test the goodreads API.

A goodreads API key is required. It can be specified in the function
call, or will be loaded from the GOODREADS_API_KEY environment
variable if no key was specified.
"""

import os
import requests

class MissingApiKeyException(Exception):
    """Raised when attempting to use the goodreads API functions
    without providing an API key
    """
    pass

def get_review_count(isbn, api_key=None):
    """Accepts a single ISBN number as a string, and gets review counts
    from the goodreads API.

    A goodreads API key is required. It can be specified in the function
    call, or will be loaded from the GOODREADS_API_KEY environment
    variable if no key was specified.
    """
    return get_counts_from_api(isbn, api_key)

def get_review_counts(isbns, api_key=None):
    """Accepts a list of ISBN numbers as strings, and gets review counts
    for each from the goodreads API.

    A goodreads API key is required. It can be specified in the function
    call, or will be loaded from the GOODREADS_API_KEY environment
    variable no key was specified.
    """
    return get_counts_from_api(','.join(isbns), api_key)

def get_counts_from_api(isbns, api_key):
    """Given a comma separated list of ISBN numbers as a single string,
    calls out to the goodreads API for review counts and returns the
    resulting JSON data.
    """
    if not api_key:
        api_key = os.getenv('GOODREADS_API_KEY')
    if not api_key:
        raise MissingApiKeyException(
            "No API key was provided in the function call "
            "or found in the environment. Try setting "
            "GOODREADS_API_KEY.")
    request_url = "https://www.goodreads.com/book/review_counts.json"
    print("Checking ISBN(s):", isbns)
    res = requests.get(request_url,
                       params={"key": api_key, "isbns": isbns}
                       )
    return res.json()['books']

if __name__ == '__main__':
    # Arbitrarily selected example ISBNs for testing purposes, these
    # don't all correspond to matching editions of the series
    HOBBIT_ISBN = "0345339681"
    FELLOWSHIP_ISBN = "0618346252"
    TWO_TOWERS_ISBN = "0618346260"
    RETURN_OF_THE_KING_ISBN = "0345339738"
    print("\nTest single ISBN:")
    print(get_review_count(HOBBIT_ISBN))
    print("\nTest multiple ISBNs:")
    print(get_review_counts([FELLOWSHIP_ISBN, TWO_TOWERS_ISBN,
                           RETURN_OF_THE_KING_ISBN]))


###Реализация парсинга из excel###
from openpyxl import load_workbook

wb = load_workbook('C:\XXXX\ИТМО\НИР\Задача№3_Мат_описание\lineranalysis\InputData.xlsx', data_only=True)  # Грузим эксель, первый аргумент - название файла эксель
#Целевая функция
data_for_calculating = []  # Создаем массив для данных
if 'ObjFunction' in wb.sheetnames:  # Проверяем есть ли данный лист в экселе
    ws = wb['ObjFunction']  # Присваиваем переменной ws нужный лист
    for cell in ws[2]:  # Пробегаемся по каждой ячейке во второй строке
        if cell.value == 0:
            break
        # Грузим в массив данные по каждой ячейке
        data_for_calculating.append(cell.value)
else:
    print('List Numbers does not exists')

#Правая сторона ограничений ежемесячного объема производства (тонн)
data_for_calculating1 = []  # Создаем массив для данных
if 'Restrictions' in wb.sheetnames:  # Проверяем есть ли данный лист в экселе
    ws = wb['Restrictions']  # Присваиваем переменной ws нужный лист
    for cell in ws[2]:  # Пробегаемся по каждой ячейке во второй строке
        if cell.value == 0:
            break
        # Грузим в массив данные по каждой ячейке
        data_for_calculating1.append(cell.value)
else:
    print('List Numbers does not exists')

#Границы каждой переменной
data_for_calculating2 = []  # Создаем массив для данных
if 'Boudaries' in wb.sheetnames:  # Проверяем есть ли данный лист в экселе
    ws = wb['Boudaries']  # Присваиваем переменной ws нужный лист
    for cell in ws[2]:  # Пробегаемся по каждой ячейке во второй строке
        if cell.value == 0:
            break
        # Грузим в массив данные по каждой ячейке
        data_for_calculating2.append(cell.value)
else:
    print('List Numbers does not exists')

###Реализация симплекс-метода###
from scipy.optimize import linprog
#linprog решает только задачи минимизации

obj = data_for_calculating[0:7]

#Левая сторона ограничений
lhs_ineq = [[ -1, 0, 0, 0, 0, 0, 0],  #-x1: ежемесячный объем производства (тонн)
            [0, -1, 0, 0, 0, 0, 0],  #-x2: ежемесячный объем производства (тонн)
            [ 0, 0, -1, 0, 0, 0, 0],  #-x3: ежемесячный объем производства (тонн)
            [ 0, 0, 0, -1, 0, 0, 0],  #-x4: ежемесячный объем производства (тонн)
            [0, 0, 0, 0, -1, 0, 0],  #-x5: ежемесячный объем производства (тонн)
            [ 0, 0, 0, 0, 0, -1, 0],  #-x6: ежемесячный объем производства (тонн)
            [ 0, 0, 0, 0, 0, 0, -1], #-x7: ежемесячный объем производства (тонн)
            [ 1, 1, 1, 1, 1, 1, 1] #x1+x2+x3+x4+x5+x6+x7
            ]

#Правая сторона ограничений ежемесячного объема производства (тонн)
rhs_ineq = data_for_calculating1[0:8]

#Определение границ каждой переменной
bnd = [(-8000000, 80000), #x1
       (-8000000, 72000), #x2
       (-8000000, 78000), #x3
       (-8000000, 73000), #x4
       (-8000000, 79000), #x5
       (-8000000,67000), #x6
       (-8000000, 40000) #x7
       ]

#obj содержит коэффициенты целевой функции
#lhs_ineq и rhs_ineq содержат коэффициенты из ограничений-неравенств для x.

opt = linprog(c=obj, A_ub=lhs_ineq, b_ub=rhs_ineq, bounds=bnd, method="simplex")
print(opt)


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

def harris(image):
    dst1 = cv2.GaussianBlur(image, (5, 5), 0)
    gray = cv2.cvtColor(dst1, cv2.COLOR_BGR2GRAY)

    cv2.imshow("gray", gray)
    ret, binary = cv2.threshold(gray, 0,255,  cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
    cv2.imshow("binary", binary)

    kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT, (23, 23))
    erode = cv2.erode(binary, kernel=kernel1)
    cv2.imshow("erode", erode)

    kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
    dilate = cv2.dilate(erode, kernel=kernel2)
    cv2.imshow("dilate", dilate)

    corners = cv2.goodFeaturesToTrack(dilate,4,0.1,135)
    corners = np.int0(corners)
    print(corners)
    for i in corners:
        x,y = i.ravel()
        cv2.circle(image,(x,y),3,255,-1)
    cv2.imshow("image", image)

    '''dilate1 = np.float32(dilate)
    img = cv2.cornerHarris(dilate1, 2, 23, 0.04)

    image[dst > 0.01 * dst.max()] = [0, 0 , 255]
    while True:
        cv2.imshow('Corner', image)
        if cv2.waitKey() & 0xff == ord('q'):
            break  '''

    '''dilate = np.float32(dilate)    #将gray转化为float32的输入图像 blocksize=2，ksize=3
    dst = cv2.cornerHarris(dilate,2,3,0.04)

     #result is dilated for marking the corners, not important
    dst = cv2.dilate(dst,None)

    # Threshold for an optimal value, it may vary depending on the image
    #将img图像中检测到的角点涂上红色
    image[dst>0.01*dst.max()]=[0,0,255]

    cv2.imshow('cornerHarris',image) '''

    pts1 = np.float32(corners)
    pts2 = np.float32([[0,330 ], [0, 0], [550, 330], [550, 0]])
    M = cv2.getPerspectiveTransform(pts1, pts2)

    # 进行透视变换

    img = cv2.warpPerspective(image, M, (550, 330))
    cv2.imshow("img", img)
    return img

capture = cv2.VideoCapture(0)
while(True):
    ret, frame = capture.read()
    frame = cv2.flip(frame, 1)
    img1 = harris(frame)
    img2 = harris(frame)
    img3 = harris(frame)
    img4 = harris(frame)
    cv2.imshow("img1",img1)
    cv2.imshow("img1",img1)
    cv2.imshow("img1",img1)
    cv2.imshow("img1",img1)

    harris(frame)
    if cv2.waitKey(10)==27:
        break

capture.release()
#cv2.destroyAllWindows()


from django.shortcuts import render, get_object_or_404, redirect, Http404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from .models import Post
from .forms import PostForm
from django.contrib import messages

def post_home(request):
    query_list = Post.objects.order_by('-timestamp')

    query = request.GET.get('q')
    if query:
        query_list = query_list.filter(title__icontains=query)

    paginator = Paginator(query_list, 5)

    page = request.GET.get('page')

    try:
        query_set = paginator.page(page)
    except PageNotAnInteger:
        query_set = paginator.page(1)
    except EmptyPage:
        query_set = paginator.page(paginator.num_pages)
    context = {'list': query_set}
    return render(request, 'posts/post_home.html', context)

def post_create(request):
    if not request.user.is_staff and not request.user.is_superuser:
        raise Http404("you are not a staff or superuser")
    form = PostForm()
    context = {'form': form}
    if request.method == 'POST':
        form = PostForm(request.POST, request.FILES or None)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.user = request.user
            instance.save()
            messages.success(request, 'successfully created')
            return redirect('posts:detail', instance.slug)

        else:
            messages.error(request, ' not successfully created')
            context = {'error_message': 'invalid form input'}
            return render(request, 'posts/post_create.html', context)
    else:
        return render(request, 'posts/post_create.html', context)

def post_detail(request, post_title_slug):
    instance = get_object_or_404(Post, slug=post_title_slug)
    context = {'instance': instance}
    return render(request, 'posts/post_detail.html', context)

def post_update(request, post_title_slug):
    if not request.user.is_staff and not request.user.is_superuser:
        raise Http404("You are not a staff or superuser")
    instance = get_object_or_404(Post, slug=post_title_slug)
    author = False

    if request.method == 'POST':

        form = PostForm(request.POST or None, request.FILES, instance=instance)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.save()
            messages.success(request, 'successfully updated')
            return redirect('posts:detail', instance.slug)
        context = {'instance': instance,
                   'form': form}

        return render(request, 'posts/post_update.html', context)
    else:
        form = PostForm(instance=instance)
        context={'form': form}
        return render(request, 'posts/post_update.html', context)

def post_delete(request, post_title_slug):
    if not request.user.is_staff and not request.user.is_superuser:
        raise Http404("you are not a staff or superuser")
    instance = get_object_or_404(Post, slug=post_title_slug)
    instance.delete()
    messages.success(request, 'deleted')
    return redirect('posts:home')


from setuptools import setup, find_packages

version = '1.0b4'

setup(name='collective.googleanalytics',
      version=version,
      description="Tools for pulling statistics from Google Analytics.",
      long_description=open("README.txt").read() + "\n" +
                       open("CHANGES.txt").read(),
      # Get more strings from http://www.python.org/pypi?%3Aaction=list_classifiers
      classifiers=[
        "Framework :: Plone",
        "Programming Language :: Python",
        "Topic :: Software Development :: Libraries :: Python Modules",
        "Topic :: Internet :: Log Analysis",
        "Topic :: Internet :: WWW/HTTP :: Site Management",
        "Development Status :: 3 - Alpha",
        ],
      keywords='Google Analytics Plone statistics portlet integration',
      author='Matt Yoder',
      author_email='mattyoder@onenw.org',
      url='http://svn.plone.org/svn/collective/collective.googleanalytics/trunk',
      license='GPL',
      packages=find_packages(exclude=['ez_setup']),
      namespace_packages=['collective'],
      include_package_data=True,
      zip_safe=False,
      install_requires=[
          'setuptools',
          # -*- Extra requirements: -*-
          'gdata>=2.0.4',
      ],
      entry_points="""
      [z3c.autoinclude.plugin]
      target = plone
      """
      )


import torch
import numpy as np

from torch import nn
from torch.optim import Adam, SGD, RMSprop
from torch.nn.utils.clip_grad import clip_grad_norm_

from .latent_temp_crf_rl import LatentTemplateCRFRL
from .ftmodel import FTModel

import torch
import traceback
from torch import autograd

class LatentTemplateCRFRLModel(FTModel):

  def __init__(self, config):
    super(LatentTemplateCRFRLModel, self).__init__()
    self.model = LatentTemplateCRFRL(config)
    self.optimizer = Adam(self.model.parameters(), lr=config.learning_rate)

    self.z_tau_init = config.z_tau_init
    self.x_lambd_start_epoch = config.x_lambd_start_epoch
    self.x_lambd_increase_interval = config.x_lambd_increase_interval
    self.max_grad_norm = config.max_grad_norm
    self.num_sample_nll = config.num_sample_nll
    self.num_sample_rl = config.num_sample_rl
    self.post_process_start_epoch = config.post_process_start_epoch
    self.dataset = config.dataset
    self.device = config.device
    self.temp_rank_strategy = config.temp_rank_strategy

    self.task = config.task
    return

  def train_step(self, batch, n_iter, ei, bi, schedule_params, debug=False):
    model = self.model

    # tau annealing
    tau = self.z_tau_init - n_iter * schedule_params['tau_decrease_interval']

    # x_lambd annealing
    if(ei < self.x_lambd_start_epoch):
      x_lambd = 0.
    else:
      x_lambd = ((ei - self.x_lambd_start_epoch) *\
        schedule_params['train_num_batches'] + bi)
      x_lambd *= self.x_lambd_increase_interval
      if(x_lambd >= 1.): x_lambd = 1.

    post_process = ei >= self.post_process_start_epoch
    if(self.dataset == 'e2e'):
      sentences = torch.from_numpy(batch['sent_dlex']).to(self.device)
    elif(self.dataset == 'mscoco'):
      sentences = torch.from_numpy(batch['sentences']).to(self.device)
    else:
      raise NotImplementedError('dataset %s not implemented' % self.dataset)

    # DEBUG
    # with GuruMeditation():
    model.zero_grad()
    loss, out_dict = model(
      keys=torch.from_numpy(batch['keys']).to(self.device),
      vals=torch.from_numpy(batch['vals']).to(self.device),
      sentences=sentences,
      sent_lens=torch.from_numpy(batch['sent_lens']).to(self.device),
      tau=tau,
      x_lambd=x_lambd,
      num_sample=self.num_sample_rl,
      post_process=post_process,
      debug=debug,
      return_grad=False
      )
    loss.backward()
    clip_grad_norm_(model.parameters(), self.max_grad_norm)
    self.optimizer.step()

    out_dict['tau'] = tau
    out_dict['x_lambd'] = x_lambd
    return out_dict

  def inspect_grad(self, batch, n_iter, ei, bi, schedule_params):
    model = self.model

    # tau annealing
    tau = self.z_tau_init - n_iter * schedule_params['tau_decrease_interval']

    # x_lambd annealing
    if(ei < self.x_lambd_start_epoch):
      x_lambd = 0.
    else:
      x_lambd = ((ei - self.x_lambd_start_epoch) *\
        schedule_params['train_num_batches'] + bi)
      x_lambd *= self.x_lambd_increase_interval
      if(x_lambd >= 1.): x_lambd = 1.

    post_process = ei >= self.post_process_start_epoch
    if(self.dataset == 'e2e'):
      sentences = torch.from_numpy(batch['sent_dlex']).to(self.device)
    elif(self.dataset == 'mscoco'):
      sentences = torch.from_numpy(batch['sentences']).to(self.device)
    else:
      raise NotImplementedError('dataset %s not implemented' % self.dataset)

    debug = False

    model.zero_grad()
    loss, out_dict = model(
      keys=torch.from_numpy(batch['keys']).to(self.device),
      vals=torch.from_numpy(batch['vals']).to(self.device),
      sentences=sentences,
      sent_lens=torch.from_numpy(batch['sent_lens']).to(self.device),
      tau=tau,
      x_lambd=x_lambd,
      num_sample=self.num_sample_rl,
      post_process=post_process,
      debug=debug,
      return_grad=True
      )

    retain_keys = [
      'g_seq_mean', 'g_seq_std', 'g_seq_r',
      'g_step_m_mean', 'g_step_m_std', 'g_step_m_r',
      'g_step_bt_mean', 'g_step_bt_std', 'g_step_bt_r',
      'g_step_ut_mean', 'g_step_ut_std', 'g_step_ut_r',
      'log_p_score',
      'reward_seq', 'learning_signal_seq',
      'reward_step_m', 'learning_signal_step_m',
      'reward_step_bt', 'learning_signal_step_bt',
      'reward_step_ut', 'learning_signal_step_ut',
      ]
    out_dict_ = {}
    for k in retain_keys:
      out_dict_[k] = out_dict[k]
    out_dict = out_dict_

    print('score func grad, seq level:   mean: %.4g, std: %.4g, r: %.4g' %
      (out_dict['g_seq_mean'], out_dict['g_seq_std'], out_dict['g_seq_r']))
    print('g_step_m:                     mean: %.4g, std: %.4g, r: %.4g' %
      (out_dict['g_step_m_mean'], out_dict['g_step_m_std'],
      out_dict['g_step_m_r']))
    print('g_step_bt:                    mean: %.4g, std: %.4g, r: %.4g' %
      (out_dict['g_step_bt_mean'], out_dict['g_step_bt_std'],
      out_dict['g_step_bt_r']))
    print('g_step_ut:                    mean: %.4g, std: %.4g, r: %.4g' %
      (out_dict['g_step_ut_mean'], out_dict['g_step_ut_std'],
      out_dict['g_step_ut_r']))

    print('log reconstruction: %.4g' % out_dict['log_p_score'])
    print('score func seq,             reward: %.4g, learning_sig:%.4g'%
      (out_dict['reward_seq'], out_dict['learning_signal_seq']))
    print('score func step marginal,   reward: %.4g, learning_sig:%.4g'%
      (out_dict['reward_step_m'], out_dict['learning_signal_step_m']))
    print('score fstep biased trans,   reward: %.4g, learning_sig:%.4g'%
      (out_dict['reward_step_bt'], out_dict['learning_signal_step_bt']))
    print('score fstep unbiased trans, reward: %.4g, learning_sig:%.4g'%
      (out_dict['reward_step_ut'], out_dict['learning_signal_step_ut']))
    return out_dict

  def valid_step(self, template_manager, batch, n_iter, ei, bi,
    mode='dev', dataset=None, schedule_params=None):
    model = self.model

    # tau annealing
    tau = self.z_tau_init - n_iter * schedule_params['tau_decrease_interval']

    # x_lambd annealing
    if(ei < self.x_lambd_start_epoch):
      x_lambd = 0.
    else:
      x_lambd = ((ei - self.x_lambd_start_epoch) *\
        schedule_params['train_num_batches'] + bi)
      x_lambd *= self.x_lambd_increase_interval
      if(x_lambd >= 1.): x_lambd = 1.

    if(self.dataset == 'e2e'):
      batch_c, batch = batch
    else: batch_c = batch

    post_process = ei >= self.post_process_start_epoch
    if(self.dataset == 'e2e'):
      sentences = torch.from_numpy(batch_c['sent_dlex']).to(self.device)
    elif(self.dataset == 'mscoco'):
      sentences = torch.from_numpy(batch_c['sentences']).to(self.device)
    else:
      raise NotImplementedError('dataset %s not implemented' % self.dataset)

    with torch.no_grad():
      out_dict = {}

      # likelihood evaluation
      if(self.task == 'density'):
        out_dict_ = model.infer_marginal(
          keys=torch.from_numpy(batch_c['keys']).to(self.device),
          vals=torch.from_numpy(batch_c['vals']).to(self.device),
          sentences=sentences,
          sent_lens=torch.from_numpy(batch_c['sent_lens']).to(self.device),
          x_lambd=x_lambd,
          num_sample=self.num_sample_nll)
      elif(self.task == 'generation'):
        # TBC
        pass
      out_dict.update(out_dict_)

    return out_dict


# uncompyle6 version 3.7.4
# Python bytecode 3.5 (3351)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /home/www.django/searchwally/codenerix/management/commands/colors.py
# Compiled at: 2017-11-28 06:03:36
# Size of source mod 2**32: 2163 bytes
import os
try:
    from subprocess import getstatusoutput
    pythoncmd = 'python3'
except:
    from commands import getstatusoutput
    pythoncmd = 'python2'

from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from codenerix.lib.debugger import Debugger
from codenerix.lib.colors import colors

class Command(BaseCommand, Debugger):
    help = 'Show colors for Debugger'

    def handle(self, *args, **options):
        self.set_name('CODENERIX')
        self.set_debug()
        keys = []
        for key in colors.keys():
            keys.append((colors[key][0], colors[key][1], key))

        keys.sort()
        for color in keys:
            simplebit, subcolor = colors[color[2]]
            print('{0:1d}:{1:02d} - \x1b[{2:1d};{3:02d}m{4:<14s}\x1b[1;00m{5:<s}'.format(simplebit, subcolor, simplebit, subcolor, color[2], color[2]))
            appname = settings.ROOT_URLCONF.split('.')[0]
            basedir = settings.BASE_DIR
            appdir = os.path.abspath('{}/{}'.format(basedir, appname))
            status, output = getstatusoutput("find {}/ -type f -name '*.pyc' -delete".format(appdir))
            if status:
                raise CommandError(output)


from bs4 import BeautifulSoup
import urllib2
import zipfile

# Set up a url connection with the census page
# which contains all of the 2013 1 Year data
pums_2013 = "http://www2.census.gov/acs2013_1yr/pums/"
pums_html = urllib2.urlopen(pums_2013)
pums_soup = BeautifulSoup(pums_html)

# Create a list of all the .zip links for
# which we need to download
zip_links = []
p_or_h = ['csv_h', 'csv_p']
for link in pums_soup.findAll('a'):
	filename = link.get_text()

	# Only pull the links where they represent
	# the household and person pums for a specific state.
	# NOT the entire U.S.A
	if any(x in filename for x in p_or_h):
		if filename not in ['csv_hus.zip', 'csv_pus.zip']:
			zip_links.append(filename)

# Create the output directory if it doesn't already exist
output_folder = "/home/lee/Dropbox/research/midas/pums_2013/"

if not os.path.isdir(output_folder):
	os.mkdir(output_folder)

for zipfile in zip_links:
	url = urllib2.urlopen(pums_2013 + zipfile)
	content = url.read()

	# Remove the surrounding characters from the
	# filename and save it in the output
	zip_save = zipfile.replace('csv_', '')

	with open(output_folder + zip_save, 'w') as file:
		file.write(content)


import sys
sys.path.insert(0, 'learning/')
from aspect_importance import dict_movie_aspect
from dictify_ratings import tuple_dict_from_ratings
import pickle
import json
import operator
import pandas as pd
import numpy as np

def film_strength(user_id, film_id, films, ratings, all_actors, all_directors, all_genres, all_similarities, testing_users_cold_start_for_user, movies_genres, movies_directors, movies_actors):
	framework = dict()
	framework['att'] = []
	framework['supp'] = []
	framework['neut'] = []
	framework['genres'] = []
	framework['directors'] = []
	framework['actors'] = []
	framework['films'] = []

	nSimUsers = 20 # number of similar users to use

	users_actors_prefs = testing_users_cold_start_for_user["actors"][user_id]
	users_directors_prefs = testing_users_cold_start_for_user["directors"][user_id]
	users_genres_prefs = testing_users_cold_start_for_user["genres"][user_id]
	similarities_for_new_user = testing_users_cold_start_for_user["sims"]

	# change similarity to only include the 20 (?) most similar users and the current user
	simsSorted = sorted(similarities_for_new_user, key = operator.itemgetter(1), reverse = True)

	# add a similarity of 1 from this user to this user
	sims = simsSorted[:nSimUsers]
	#sims.append((user_id,1))

	film = films[film_id]

	# mu constants for this user
	MUR = 0.3
	MUG = 0.3
	MUA = 0.5
	MUD = 0.2

	# take an average of each of the the genre's average ratings
	nGenres = 0
	dGenres = 0
	if type(film['genre']) is str:
		film['genre'] = [film['genre']]
	for genre in film['genre']:

		aspect_value =  movies_genres[genre].to_dict()
		movie_ids_with_aspect_value = [k.split("_")[0] for k,v in aspect_value.items() if v == 1]

		# get the average rating for each film of that genre and take an average
		nGenre = 0
		dGenre = 0
		for genrefilm in movie_ids_with_aspect_value:
			#if (genrefilm, user_id) in ratings.keys():
			#	avg_rat = ((ratings[(genrefilm, user_id)] - 1) / 2)-1
			#else:
			avg_rat = average_rating(sims, genrefilm, ratings)

			if avg_rat:
				dGenre += avg_rat
				nGenre += 1

		if nGenre > 0:
			avGenre = dGenre / nGenre
			cmbGenre = ((((users_genres_prefs[genre]- 1) / 2)-1) + (MUR*avGenre)) / (1+MUR)

		else:
			cmbGenre = (((users_genres_prefs[genre]- 1) / 2)-1)

		framework['genres'].append((genre, cmbGenre))

		dGenres += cmbGenre
		nGenres += 1

	if nGenres > 0:
		avgGenreRating = dGenres / nGenres
	else:
		avgGenreRating = 0

	# take an average of each of the the actor's average ratings
	nActors = 0
	dActors = 0
	if type(film['actors']) is str:
		film['actors'] = [film['actors']]
	for actor in film['actors']:

		aspect_value =  movies_actors[actor].to_dict()
		movie_ids_with_aspect_value = [k.split("_")[0] for k,v in aspect_value.items() if v == 1]

		# get the average rating for each film with that actor and take an average
		nActor = 0
		dActor = 0
		for actorfilm in movie_ids_with_aspect_value:

			#if (actorfilm, user_id) in ratings.keys():
			#	avg_rat = ((ratings[(actorfilm, user_id)] - 1) / 2)-1
			#else:
			avg_rat = average_rating(sims, actorfilm, ratings)

			if avg_rat:
				dActor += avg_rat
				nActor += 1

		if nActor > 0:
			# dActors += (((users_actors_prefs[actor]- 1) / 2)-1) * dActor / nActor
			avActor = dActor / nActor
			cmbActor = ((((users_actors_prefs[actor]- 1) / 2)-1) + (MUR*avActor)) / (1+MUR)
		else:
			cmbActor = (((users_actors_prefs[actor]- 1) / 2)-1)

		framework['actors'].append((actor, cmbActor))

		dActors += cmbActor
		nActors += 1

	if nActors > 0:
		avgActorRating = dActors / nActors
	else:
		avgActorRating = 0

	# take an average of each of the the director's average ratings
	nDirectors = 0
	dDirectors = 0
	if type(film['director']) is str:
		film['director'] = [film['director']]
	for director in film['director']:

		aspect_value =  movies_directors[director].to_dict()
		movie_ids_with_aspect_value = [k.split("_")[0] for k,v in aspect_value.items() if v == 1]

		# get the average rating for each film with that actor and take an average
		nDirector = 0
		dDirector = 0
		for directorfilm in movie_ids_with_aspect_value:

			#if (directorfilm, user_id) in ratings.keys():
			#	avg_rat = ((ratings[(directorfilm, user_id)] - 1) / 2)-1
			#else:
			avg_rat = average_rating(sims, directorfilm, ratings)

			if avg_rat:
				dDirector += avg_rat
				nDirector += 1

		if nDirector > 0:
			# dDirectors += (((users_directors_prefs[director]- 1) / 2)-1) * dDirector / nDirector
			#dDirectors += dDirector / nDirector
			avDirector = dDirector / nDirector
			cmbDirector = ((((users_directors_prefs[director]- 1) / 2)-1) + (MUR*avDirector)) / (1+MUR)
		else:
			cmbDirector = (((users_directors_prefs[director]- 1) / 2)-1)

		framework['directors'].append((director, cmbDirector))

		dDirectors += cmbDirector
		nDirectors += 1

	if nDirectors > 0:
		avgDirectorRating = dDirectors / nDirectors
	else:
		avgDirectorRating = 0

	# calculates the item strength
	avg_rat = average_rating(sims, film_id, ratings)

	if avg_rat is None:
		item_strength = ((MUG * avgGenreRating) + (MUA * avgActorRating)+ (MUD * avgDirectorRating)) / (MUG + MUA + MUD)
	else:
		item_strength = ((MUR * avg_rat) + (MUG * avgGenreRating) + (MUA * avgActorRating)+ (MUD * avgDirectorRating)) / (MUR + MUG + MUA + MUD)

	film_strength = (((item_strength + 1)*2)+1)

	framework['films'].append((film_id, film_strength))

	for (genre, strength) in framework['genres']:
		if strength < 0:
			framework['att'].append((genre, film_id))
		elif strength > 0:
			framework['supp'].append((genre, film_id))
		else:
			framework['neut'].append((genre, film_id))

	for (director, strength) in framework['directors']:
		if strength < 0:
			framework['att'].append((director, film_id))
		elif strength > 0:
			framework['supp'].append((director, film_id))
		else:
			framework['neut'].append((director, film_id))

	for (actor, strength) in framework['actors']:
		if strength < 0:
			framework['att'].append((actor, film_id))
		elif strength > 0:
			framework['supp'].append((actor, film_id))
		else:
			framework['neut'].append((actor, film_id))

	framework_file = "%s_%s.json" % (user_id, film_id)
	with open(framework_file, 'w') as f:
		json.dump(framework, f, indent = 4, ensure_ascii = False)

	return film_strength

def average_rating(sims, film_id, ratings):
	# counts and totals for each type of aspect
	nRatings = 0
	dRatings = 0

	# cycles through each of the similar users
	for sim in sims:

		user_id = sim[0]
		similarity = sim[1]

		# if a rating exists by this user on the film
		if (film_id, user_id) in ratings.keys():
			user_rating = ratings[(film_id, user_id)]

			scaled_rating = ((user_rating - 1) / 2)-1
			# print(user_rating)
			# print(scaled_rating)
			dRatings += scaled_rating * similarity
			nRatings += 1

	if nRatings == 0:
		avg_rat = None
	else:
		avg_rat = dRatings / nRatings

	return avg_rat

if __name__ == "__main__":

	ids = ['tt0289992','tt0338013','tt0308644','tt0162222','tt0362227','tt0203009','tt0373926','tt0264464','tt0315733','tt0327056']
	paper_ids = ['tt0264464', 'tt0203009']
	sample_users = ['1824033', '1409354', '2435946', '553658', '608915', '1753259', '2615678', '1443524', '771174']

	paper_films = pickle.load(open("db/paper_films.pkl", "rb"))
	paper_ratings = pickle.load(open("db/paper_ratings.pkl", "rb"))
	preferences = pickle.load(open("arg/preference_genre_False_normalized_10.pkl","rb"))

	films = dict()
	ratings = dict()

	for i in ids:
		films[i] = paper_films[i]
		ratings[i] = paper_ratings[i]

	movies_watched = viewed_matrix(ratings, films, sample_users)
	print (pd.DataFrame.from_dict(movies_watched, orient='index').to_string())

	df = pd.DataFrame.from_dict(movies_watched, orient='index')
	df = df.replace(np.nan, 0)
	print (df.to_string())

	movies_genres = dict_movie_aspect(films, "genre")
	movies_genres = pd.DataFrame.from_dict(movies_genres, dtype='int64', orient='index')
	movies_genres = movies_genres.replace(np.nan, 0)

	movies_directors = dict_movie_aspect(films, "director")
	movies_directors = pd.DataFrame.from_dict(movies_directors, dtype='int64', orient='index')
	movies_directors = movies_directors.replace(np.nan, 0)

	movies_actors = dict_movie_aspect(films, "actors")
	movies_actors = pd.DataFrame.from_dict(movies_actors, dtype='int64', orient='index')
	movies_actors = movies_actors.replace(np.nan, 0)

	all_actors = pickle.load(open("arg/preference_actors_False_normalized_10.pkl", "rb"))
	all_directors = pickle.load(open("arg/preference_director_False_normalized_10.pkl", "rb"))
	all_genres = pickle.load(open("arg/preference_genre_False_normalized_10.pkl", "rb"))
	all_similarities = pickle.load(open("arg/similarity_False_normalized_10.pkl", "rb"))

	ratings = tuple_dict_from_ratings(ratings)


import cv2
import tensorflow as tf
import os
import keras as keras
import numpy as np
import pickle
import time
import random
from skimage import data, color
from skimage.transform import rescale, rotate
from skimage.util import random_noise
# COMMMENT OUT WHEN FINDING BALLCENTERS

cap = cv2.VideoCapture('final_FIFACut.mp4')
ret, frame = cap.read()

IMG_SIZE = 25
CIRCLE_RADIUS = 5

second_frame_count = 1

ballPosition = pickle.load(open("ballPosition.pickle","rb"))

while(cap.isOpened()):
    if (second_frame_count == 1500):
         break

    ret, frameRead = cap.read()
    frame = random_noise(frameRead, mode='gaussian', seed=None, clip=True)

    blank_image = np.zeros((674,1200,3), np.uint8)

    cv2.circle(blank_image,(ballPosition[second_frame_count - 1][0],ballPosition[second_frame_count - 1][1]), CIRCLE_RADIUS, (0,255,255), -1)
    print(second_frame_count)
    start = time.time()
    if ballPosition[second_frame_count - 1][0] < IMG_SIZE and ballPosition[second_frame_count - 1][1] < IMG_SIZE:
        end = time.time()
        print(end - start)
        second_frame_count = second_frame_count + 1
        continue
    for i in range(1,5000):
        # print("frame: "+str(second_frame_count))
        # print("iteration "+str(i))
        width = random.randint(1, 1150)
        height = random.randint(1, 624)
        rotation = random.randint(1, 180)
        scale = random.uniform(0.5, 1)

        randompiece_frameWithCircle = blank_image[height:height+IMG_SIZE, width:width+IMG_SIZE]
        randompiece_frameToData = frame[height:height+IMG_SIZE, width:width+IMG_SIZE]

        rotation_frameWithCircle = rotate(randompiece_frameWithCircle, angle=rotation, mode='reflect')
        scale_frameWithCircle = rescale(rotation_frameWithCircle, scale=scale, mode='constant')

        rotation_frameToData = rotate(randompiece_frameToData, angle=rotation, mode='reflect')
        scale_frameToData = rescale(rotation_frameToData, scale=scale, mode='constant')
        # frameWithCircle = keras.preprocessing.image.ImageDataGenerator(featurewise_center=False, samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, zca_whitening=False, zca_epsilon=1e-06, rotation_range=45, width_shift_range=1200, height_shift_range=674, brightness_range=None, shear_range=0.0, zoom_range=[0.025,0.025], channel_shift_range=0.0, fill_mode='nearest', cval=0.0, horizontal_flip=False, vertical_flip=False, rescale=None, preprocessing_function=None, data_format=None, validation_split=0.0, dtype=None).random_transform(blank_image,seed=i)
        # frameToData = keras.preprocessing.image.ImageDataGenerator(featurewise_center=False, samplewise_center=False, featurewise_std_normalization=False, samplewise_std_normalization=False, zca_whitening=False, zca_epsilon=1e-06, rotation_range=45, width_shift_range=1200, height_shift_range=674, brightness_range=None, shear_range=0.0, zoom_range=[0.025,0.025], channel_shift_range=0.0, fill_mode='nearest', cval=0.0, horizontal_flip=False, vertical_flip=False, rescale=None, preprocessing_function=None, data_format=None, validation_split=0.0, dtype=None).random_transform(frame,seed=i)
        frameWithCircle = cv2.resize(scale_frameWithCircle, (IMG_SIZE,IMG_SIZE),interpolation = cv2.INTER_AREA)
        frameToData = cv2.resize(scale_frameToData, (IMG_SIZE,IMG_SIZE),interpolation = cv2.INTER_AREA)

        count = np.count_nonzero(frameWithCircle)

        if(count > 250):
            print(count)
            name = str(second_frame_count)+"_"+str(i)+".png"
            frameToData = frameToData * 255
            image_to_save = frameToData.astype('uint8')
            cv2.imwrite(os.path.join("C:/Users/emilh/Desktop/keras_bachelor/FifaBall/",name), image_to_save )
        else:
            random_number = random.uniform(0, 1)
            if random_number > 1 - 1 / 1000:
                 name = str(second_frame_count)+"_"+str(i)+".png"
                 frameToData = frameToData * 255
                 image_to_save = frameToData.astype('uint8')
                 cv2.imwrite(os.path.join("C:/Users/emilh/Desktop/keras_bachelor/FifaNotBall/",name), image_to_save )

    end = time.time()
    print(end - start)
    second_frame_count = second_frame_count + 1

cap.release()
cv2.destroyAllWindows()


import os
import pickle as pk

import matplotlib as mpl

mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import torch
from scipy.misc import imresize
from torchvision import transforms, datasets

log_dir = 'logs'

def trainloader(batch_size):
    dset = trainloader_helper()
    train_loader = torch.utils.data.DataLoader(dset, batch_size=batch_size, shuffle=True, num_workers=6)
    return train_loader

def trainloader_helper():
    data_dir = '/data/milatmp1/considib/img_align_celebA/celebA/'
    resized_dir = '/data/milatmp1/considib/resized_celebA/'
    if not os.path.isdir(resized_dir):
        _preprocess_celeb(data_dir, resized_dir)

    transform = transforms.Compose([
        # transforms.Resize(64),
        transforms.ToTensor(),
        transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
    ])

    data_dir = '/data/milatmp1/considib/resized_celebA'
    dset = datasets.ImageFolder(data_dir, transform)
    print("Loaded {} images for training".format(len(dset)))
    return dset

def _preprocess_celeb(root, save_root):
    resize_size = 64

    if not os.path.isdir(save_root):
        os.mkdir(save_root)
    if not os.path.isdir(save_root + 'celebA'):
        os.mkdir(save_root + 'celebA')
    img_list = os.listdir(root)

    # ten_percent = len(img_list) // 10
    print(len(img_list))

    for i in range(len(img_list)):
        img = plt.imread(root + img_list[i])
        img = imresize(img, (resize_size, resize_size))
        plt.imsave(fname=save_root + 'celebA/' + img_list[i], arr=img)

        if (i % 1000) == 0:
            print('%d images complete' % i)

def save(discriminator, generator, epoch=0):
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)
    gen_model_name = "{0}_{1}_G.pkl".format(generator.model_name, epoch)
    disc_model_name = "{0}_{1}_D.pkl".format(discriminator.model_name, epoch)
    torch.save(generator.state_dict(), os.path.join(log_dir, gen_model_name))
    torch.save(discriminator.state_dict(), os.path.join(log_dir, disc_model_name))

def save_losses(minibatch_disc_losses, minibatch_gen_losses, model_name):
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)

    fname1 = model_name + '_minibatch_disc_losses.pk'
    fname2 = model_name + '_minibatch_gen_losses.pk'

    with open(os.path.join(log_dir, fname1), "wb") as f:
        pk.dump(minibatch_disc_losses, f)
    with open(os.path.join(log_dir, fname2), "wb") as f:
        pk.dump(minibatch_gen_losses, f)

def plot_result(G, fixed_noise, num_epoch, fig_size=(8, 8)):
    G.eval()
    fixed_noise = fixed_noise.cuda()
    generate_images = G(fixed_noise)
    G.train()

    n_rows = n_cols = 8
    fig, axes = plt.subplots(n_rows, n_cols, figsize=fig_size)

    for ax, img in zip(axes.flatten(), generate_images):
        ax.axis('off')
        ax.set_adjustable('box-forced')
        img = (((img - img.min()) * 255) / (img.max() - img.min())).cpu().data.numpy().transpose(1, 2, 0).astype(
            np.uint8)
        ax.imshow(img, cmap=None, aspect='equal')
    plt.subplots_adjust(wspace=0, hspace=0)
    title = 'Epoch {0}'.format(num_epoch)
    fig.text(0.5, 0.04, title, ha='center')

    filename = "{0}_epoch_{1}.png".format(G.model_name, num_epoch)
    if not os.path.exists(log_dir):
        os.mkdir(log_dir)
    plt.savefig(os.path.join(log_dir, filename))
    plt.close()


import numpy as np
import pandas as pd
import json
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.cm as cmap
import matplotlib.ticker as mticker
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.ticker import ScalarFormatter
import matplotlib.gridspec as gridspec
import itertools
from pnptransport import utils
import os

output_folder = r'G:\Shared drives\FenningLab2\Projects\PVRD1\Simulations\Sentaurus PID\results\analysis\finite_source'
data_root = r'G:\Shared drives\FenningLab2\Projects\PVRD1\Simulations\Sentaurus PID\results\3D'
input_folder = r'G:\My Drive\Research\PVRD1\Manuscripts\Device_Simulations_draft\images\sentaurus_dsf'
ml_simulations_csv = r'G:\My Drive\Research\PVRD1\Manuscripts\Device_Simulations_draft\simulations\inputs_20201028\ofat_db.csv'

sentaurus_db_csv = r'sentaurus_pid_dsf.csv'
literature_db_csv = r'literature_pid_dsf.csv'
batch_analysis_folder = 'batch_analysis_rfr_t'

conditions = {
    'efield': 1E4,
    'S': 1E10,
    'k': 1E-4,
    'h': 1E-12
}

if __name__ == '__main__':
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    input_files_df = pd.read_csv(os.path.join(input_folder, sentaurus_db_csv))
    literature_files_df = pd.read_csv(os.path.join(input_folder, literature_db_csv))
    ofat_df = pd.read_csv(os.path.join(ml_simulations_csv))

    # ML dataroot
    ml_data_root = os.path.join(os.path.dirname(ml_simulations_csv), batch_analysis_folder)
    print(ml_data_root)

    # print(ofat_df.columns)
    with open('plotstyle.json', 'r') as f:
        mpl.rcParams.update(json.load(f)['defaultPlotStyle'])

    # Define the color palette
    cm = cmap.get_cmap('rainbow_r')

    nplots = len(input_files_df)  # + len(literature_files_df)
    normalize = mpl.colors.Normalize(vmin=0, vmax=nplots)
    plot_colors = [cm(normalize(i)) for i in range(nplots)]
    plot_marker = itertools.cycle(('o', 's', '^', 'v', '>', '<', 'd', 'p', '*'))

    fig = plt.figure(1)
    fig.set_size_inches(8.5, 3.5, forward=True)
    fig.subplots_adjust(hspace=0.1, wspace=0.35)
    gs0 = gridspec.GridSpec(ncols=1, nrows=1, figure=fig, width_ratios=[1])
    gs00 = gridspec.GridSpecFromSubplotSpec(nrows=1, ncols=1, subplot_spec=gs0[0])
    ax = fig.add_subplot(gs00[0, 0])

    # Plot Sentaurus results
    for i, r in input_files_df.iterrows():
        dsf = float(r['DSF'])
        lbl = r'$\mathregular{{D_{{SF}}}} = {0}$'.format(utils.latex_order_of_magnitude(dsf))
        efficiency_csv = os.path.join(
            data_root, r['folder'], 'jv_plots', 'efficiency_results.csv'
        )
        efficiency_df: pd.DataFrame = pd.read_csv(efficiency_csv)

        power = np.array(efficiency_df['pd_mpp (mW/cm2)'])
        p0 = power[0]
        idx = np.round(power, decimals=0) < np.round(p0, decimals=0)
        time_h = np.array(efficiency_df['time (s)'].values) / 3600.

        if len(power) > 0:
            ax.plot(
                time_h, power / power[0], color=plot_colors[i],  # marker=next(plot_marker),
                label=lbl, ls='-', fillstyle='none', zorder=i
            )

    cm = cmap.get_cmap('rainbow_r')

    nplots = len(literature_files_df)  # + len(literature_files_df)
    normalize = mpl.colors.Normalize(vmin=0, vmax=nplots)
    plot_colors = [cm(normalize(i)) for i in range(nplots)]
    plot_marker = itertools.cycle(('o', 's', '^', 'v', '>', '<', 'd', 'p', '*'))

    for i, lf in literature_files_df.iterrows():
        fn = lf['file']
        time_units = lf['time_units']
        label = lf['label']
        if lf['type'] == 'power':
            column_names = ['time', 'power']
        else:
            column_names = ['time', 'Rsh']
        lit_df = pd.read_csv(fn, skiprows=0, header=0, names=column_names, index_col=False)

        if time_units == 'min':
            time_lf = lit_df['time'].to_numpy() / 60
        elif time_units == 's':
            time_lf = lit_df['time'].to_numpy() / 3600
        elif time_units == 'h':
            time_lf = lit_df['time'].to_numpy()
        else:  # Assume hours
            time_lf = lit_df['time'].to_numpy()

        # t_max = max(time_lf.max(), t_max)

        if lf['type'] == 'power':
            normalized_power = lit_df['power']
            ax.plot(
                time_lf, normalized_power, fillstyle='none',
                color=plot_colors[i], label=label,
                marker=next(plot_marker), zorder=i + len(input_files_df),
                ls='--', lw=1, mew=1.5
            )

            t_interp = np.linspace(np.amin(time_lf), np.amax(time_lf), num=200)
            # f_p_interp = interpolate.interp1d(time_lf, normalized_power, kind='linear')
            # metric_interp = f_p_interp(t_interp)
            # idx_5 = (np.abs(metric_interp - 0.95)).argmin()
            # fail_time_5[i] = t_interp[idx_5]

    ax.set_xlabel('Time (hr)')
    ax.set_ylabel('Normalized Power')
    ax.set_xlim(0, 96.)
    ax.set_ylim(0.8, 1.05)

    xfmt = ScalarFormatter(useMathText=True)
    xfmt.set_powerlimits((-3, 3))

    ax.xaxis.set_major_formatter(xfmt)
    ax.xaxis.set_major_locator(mticker.MaxNLocator(6, prune=None))
    ax.xaxis.set_minor_locator(mticker.AutoMinorLocator(2))

    ax.yaxis.set_major_formatter(xfmt)
    ax.yaxis.set_major_locator(mticker.MaxNLocator(5, prune=None))
    ax.yaxis.set_minor_locator(mticker.AutoMinorLocator(2))

    leg = ax.legend(bbox_to_anchor=(1.1, 1.0), loc='upper left', borderaxespad=0., ncol=2)

    # Plot Sentaurus vs ML time series
    nplots = len(input_files_df)  # + len(literature_files_df)
    normalize = mpl.colors.Normalize(vmin=0, vmax=nplots)
    plot_colors = [cm(normalize(i)) for i in range(nplots)]
    plot_marker = itertools.cycle(('o', 's', '^', 'v', '>', '<', 'd', 'p', '*'))

    fig_ml = plt.figure(2)
    fig_ml.set_size_inches(8.5, 3.5, forward=True)
    fig_ml.subplots_adjust(hspace=0.1, wspace=0.35)
    gs0_ml = gridspec.GridSpec(ncols=1, nrows=1, figure=fig_ml, width_ratios=[1])
    gs00_ml = gridspec.GridSpecFromSubplotSpec(nrows=1, ncols=1, subplot_spec=gs0_ml[0])
    ax_ml = fig_ml.add_subplot(gs00_ml[0, 0])

    for i, r in input_files_df.iterrows():
        dsf = float(r['DSF'])
        lbl = r'$\mathregular{{D_{{SF}}}} = {0}$'.format(utils.latex_order_of_magnitude(dsf))
        efficiency_csv = os.path.join(
            data_root, r['folder'], 'jv_plots', 'efficiency_results.csv'
        )
        efficiency_df: pd.DataFrame = pd.read_csv(efficiency_csv)

        power = np.array(efficiency_df['pd_mpp (mW/cm2)'])
        p0 = power[0]
        idx = np.round(power, decimals=0) < np.round(p0, decimals=0)
        time_h = np.array(efficiency_df['time (s)'].values) / 3600.

        # Find the corresponding ML time series
        q = '`sigma_s (cm^-2)` == {0} & '.format(conditions['S'])
        q += '`zeta (1/s)` ==  {0} & '.format(conditions['k'])
        q += '`D_SF (cm^2/s)` == {0} & '.format(dsf)
        q += '`E (V/cm)` == {0} & '.format(conditions['efield'])
        q += '`h (cm/s)` == {0}'.format(conditions['h'])
        ml_dsf_df = ofat_df.query(q).reset_index().head(1)
        ml_pid_csv = ml_dsf_df['config file'].values
        if len(ml_pid_csv) > 0:
            file_tag = os.path.splitext(ml_pid_csv[0])[0] + '_simulated_pid.csv'
            path_to_ml_csv = os.path.join(ml_data_root, file_tag)
            if os.path.exists(path_to_ml_csv):
                print(path_to_ml_csv)
                ml_simulated_pid_df = pd.read_csv(path_to_ml_csv)
                time_ml = np.array(ml_simulated_pid_df['time (s)']) / 3600
                mpp_ml = np.array(ml_simulated_pid_df['Pmpp (mW/cm^2)'])
                lbl = r'$\mathregular{{D_{{SF}}}} = {0}$, ML'.format(utils.latex_order_of_magnitude(dsf))
                ax_ml.plot(
                    time_ml, mpp_ml / mpp_ml[0], color=plot_colors[i],  #marker=next(plot_marker),
                    label=lbl, ls='--', fillstyle='none', zorder=i
                )
        if len(power) > 0:
            ax_ml.plot(
                time_h, power / power[0], color=plot_colors[i],  # marker=next(plot_marker),
                label=lbl, ls='-', fillstyle='none', zorder=i+nplots
            )

        # print(file_tag)

    ax_ml.set_xlabel('Time (hr)')
    ax_ml.set_ylabel('Normalized Power')
    ax_ml.set_xlim(0, 96.)
    # ax_ml.set_ylim(0.8, 1.05)

    # ax_ml.set_yscale('log')

    xfmt = ScalarFormatter(useMathText=True)
    xfmt.set_powerlimits((-3, 3))

    ax_ml.xaxis.set_major_formatter(xfmt)
    ax_ml.xaxis.set_major_locator(mticker.MaxNLocator(6, prune=None))
    ax_ml.xaxis.set_minor_locator(mticker.AutoMinorLocator(2))

    # ax_ml.yaxis.set_major_formatter(xfmt)
    # ax_ml.yaxis.set_major_locator(mticker.MaxNLocator(5, prune=None))
    # ax_ml.yaxis.set_minor_locator(mticker.AutoMinorLocator(2))

    leg2 = ax_ml.legend(bbox_to_anchor=(1.1, 1.0), loc='upper left', borderaxespad=0., ncol=2)

    fig.tight_layout()
    fig_ml.tight_layout()
    fig.savefig(os.path.join(output_folder, 'failure_time_dsf.svg'), dpi=600)
    fig.savefig(os.path.join(output_folder, 'failure_time_dsf.png'), dpi=600)

    fig_ml.savefig(os.path.join(output_folder, 'failure_time_dsf_ml.svg'), dpi=600)
    fig_ml.savefig(os.path.join(output_folder, 'failure_time_dsf_ml.png'), dpi=600)
    plt.show()


import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from utils import init_params

class Learner(nn.Module):
    def __init__(self, nway=5, base_lr=1e-2):
        super(Learner, self).__init__()
        self.conv1 = nn.Conv2d(3,32,3,1,0)
        self.relu1 = nn.ReLU()
        self.bn1 = nn.BatchNorm2d(32)
        self.maxpool1 = nn.MaxPool2d(2,2,0)

        self.conv2 = nn.Conv2d(32,32,3,1,0)
        self.relu2 = nn.ReLU()
        self.bn2 = nn.BatchNorm2d(32)
        self.maxpool2 = nn.MaxPool2d(2,2,0)

        self.conv3 = nn.Conv2d(32,32,3,1,0)
        self.relu3 = nn.ReLU()
        self.bn3 = nn.BatchNorm2d(32)
        self.maxpool3 = nn.MaxPool2d(2,2,0)

        self.conv4 = nn.Conv2d(32,32,3,1,0)
        self.relu4 = nn.ReLU()
        self.bn4 = nn.BatchNorm2d(32)
        self.maxpool4 = nn.MaxPool2d(2,1,0)

        self.fc = nn.Linear(800, 5)

        init_params(self)

        self.base_lr = base_lr

    def forward(self, x):
        x = self.bn1(self.relu1(self.conv1(x)))
        x = self.maxpool1(x)
        x = self.bn2(self.relu2(self.conv2(x)))
        x = self.maxpool2(x)
        x = self.bn3(self.relu3(self.conv3(x)))
        x = self.maxpool3(x)
        x = self.bn4(self.relu4(self.conv4(x)))
        x = self.maxpool4(x)
        x = x.view(x.shape[0],-1)
        x = self.fc(x)
        return x

    def _adapt(self, weights, x, y):
        with torch.enable_grad():
            pred = self._forward_external_weight(weights, x)
            loss = F.cross_entropy(pred, y)
            grad = torch.autograd.grad(loss, weights)

            weights = list(map(lambda p : p[1] - self.base_lr * p[0], zip(grad, weights)))
        return weights

    def _forward_external_weight(self, weights, x, bn_training=True):
        idx = 0
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                x = F.conv2d(x, weights[idx], weights[idx+1], m.stride, m.padding)
                idx += 2
            elif isinstance(m, nn.BatchNorm2d):
                x = F.batch_norm(x, m.running_mean, m.running_var, weights[idx], weights[idx+1], bn_training)
                idx += 2
            elif isinstance(m, nn.Linear):
                x = x.view(x.shape[0], -1)
                x = F.linear(x, weights[idx], weights[idx+1])
                idx += 2
            elif isinstance(m, nn.ReLU):
                x = F.relu(x)
            elif isinstance(m, nn.MaxPool2d):
                x = F.max_pool2d(x, m.kernel_size, m.stride, m.padding)
        return x


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

from __future__ import division, print_function, absolute_import
from matplotlib import pyplot as plt
import os
from timeit import time
import warnings
import sys
import cv2
import numpy as np
from PIL import Image
from yolo import YOLO
import pandas as pd
from deep_sort import preprocessing
from deep_sort import nn_matching
from deep_sort.detection import Detection
from deep_sort.tracker import Tracker
from tools import generate_detections as gdet
from deep_sort.detection import Detection as ddet

warnings.filterwarnings('ignore')
def findbbx(frame, frame_idx, df):
    '''

    :param frame: number of frame
    :param df: dataframe which saves the det
    :return: a list of bbox[x,y,w,h]
    '''
    bbx = []
    while True:
        if frame_idx >= df.shape[0]:
            break
        if df['frame'][frame_idx] != frame:
            break
        t, l, w, h = df['t'][frame_idx] , df['l'][frame_idx] , df['w'][frame_idx] , df['h'][frame_idx]
        bbx.append([t, l, w, h])

        frame_idx += 1
    #print(bbx)
    return bbx, frame_idx

def test():
    MOT_path = r'E:\projects\deep_sort_yolov3\data\MOT17/'
    img_path = '1.jpg'
    frame = cv2.imread(img_path)
    print(frame)
    bbox = [343.8498840332031,821.2205810546875,125.63116455078125,258.70751953125]
    cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[0])+int(bbox[2]), int(bbox[1])+int(bbox[3])), (255, 0, 0), 2)
    cv2.imshow(' ', frame)
    cv2.waitKey()
    cv2.destroyAllWindows()

def main(yolo):
    MOT_path = '/home/wangshuai/projects/data/CVPR19/test/CVPR19-06'
    # Definition of the parameters
    max_cosine_distance = 0.70
    nn_budget = None
    nms_max_overlap = 1.0
    # load det
    det_df = pd.read_csv(MOT_path+'/det/det_csv.csv')
    # deep_sort
    model_filename = 'model_data/mars-small128.pb'
    encoder = gdet.create_box_encoder(model_filename, batch_size=1)
    # det_df_idx
    det_df_idx = 0
    metric = nn_matching.NearestNeighborDistanceMetric("ad_cosine", max_cosine_distance, nn_budget)
    tracker = Tracker(metric, n_init=3, max_age=30)

    writeVideo_flag = False

    if writeVideo_flag:
        # Define the codec and create VideoWriter object
        w = 1920
        h = 734
        fourcc = cv2.VideoWriter_fourcc(*'MJPG')
        out = cv2.VideoWriter('output.avi', fourcc, 15, (w, h))
        list_file = open('detection.txt', 'w')
    frame_index = 0
    imgs = os.listdir(MOT_path+'/img1')
    fps = 0.0
    imgs.sort()
    print(imgs)
    for img_path in imgs:
        if '.jpg' not in img_path:
            continue
        print('---------------------------------', frame_index + 1, '------------------')
        print(img_path)

        frame = cv2.imread(MOT_path + '/img1/' + img_path)
        # while True:
        #   ret, frame = video_capture.read()  # frame shape 640*480*3
        #  if ret != True:
        #     break
        t1 = time.time()

        image = Image.fromarray(frame)
        image = Image.fromarray(frame[..., ::-1])  # bgr to rgb
        boxs,det_df_idx = findbbx(frame_index+1,det_df_idx, det_df)
        # print("box_num",len(boxs))
        features = encoder(frame, boxs)

        # score to 1.0 here).
        detections = [Detection(bbox, 1.0, feature) for bbox, feature in zip(boxs, features)]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap, scores)
        detections = [detections[i] for i in indices]

        # Call the tracker
        tracker.predict()
        matches = tracker.update(detections)

        cv2.putText(frame, 'frame: ' + str(frame_index + 1), (30, 30), 0, 5e-3 * 200, (0, 255, 0), 2)

        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            bbox = track.to_tlbr()
            # cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2)  # 白色
            cv2.putText(frame, str(track.track_id), (int(bbox[0]), int(bbox[1])), 0, 5e-3 * 200, (0, 255, 0),
                        2)  # 绿色的字 + ' (' + str(bbox[0]) + ',' + str(bbox[1]) + ')'

        for i, det in enumerate(detections):
            bbox = det.to_tlbr()
            cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 0, 0), 2)

       # cv2.imshow('', frame)

        if writeVideo_flag:
            # save a frame

            out.write(frame)
            frame_index = frame_index + 1
            # print('frame',frame_index)
            # print('check', matches)
            # print(len(tracker.tracks))
            for track_id, det_idx in matches:
                bbox = detections[det_idx].tlwh
                # print('(',track_id,det_idx,')',end=' ')
                id = track_id
                list_file.write(str(frame_index) + ',' + str(id) + ',' + str(bbox[0]) + ',' + str(bbox[1]) + ',' + str(
                    bbox[2]) + ',' + str(bbox[3]) + ',1,-1,-1')
                list_file.write('\n')
            # list_file.write(str(frame_index)+' ')
            # if len(boxs) != 0:
            #     for i in range(0,len(boxs)):
            #         list_file.write(str(boxs[i][0]) + ' '+str(boxs[i][1]) + ' '+str(boxs[i][2]) + ' '+str(boxs[i][3]) + ' ')
        # list_file.write('\n')

        fps = (fps + (1. / (time.time() - t1))) / 2
        print("fps= %f" % (fps))

        # Press Q to stop!
        # if cv2.waitKey(1) & 0xFF == ord('q'):
        #     break

    if writeVideo_flag:
        out.release()
        list_file.close()
    #cv2.destroyAllWindows()

if __name__ == '__main__':
    #test()
    main(YOLO())