import re
import math
import pynbs
import struct
import os
import argparse

_map = 'a_b[cd~e;f1gh2i3jk4l5m6no7p8qr9s0t"uv,w.xy?z'

def getFloat(f):
    return struct.pack('<f', f)

def getInt(n):
    return struct.pack('<i', n)

def getShort(n):
    return struct.pack('<h', n)

def getByte(b):
    return bytes([b])

def convert(string):
    with open("out.mcs", 'wb') as f:
        notes = []
        ticking = True
        tick = 0
        for i in string:
            if i == '(':
                ticking = False
            elif i == ')':
                ticking = True
            else:
                try:
                    notes.append({
                        'time': tick,
                        'instrument': 0,
                        'key': _map.index(i.lower())+48
                    })
                except:
                    pass
            if ticking:
                tick += 1

        notes.sort(key=lambda note: note.get('time'))

        multiplier = 5

        # Magic
        f.write(bytes('MCSQ', encoding='utf-8'))
        # Version
        f.write(getShort(1))
        # Tempo Multiplier
        f.write(getFloat(multiplier))
        # Number of notes
        f.write(getInt(len(notes)))

        length = 0
        for i in range(len(notes)):
            note = notes[i]
            try:
                length = note['time'] - notes[i-1]['time']
            except:
                length = 1

            if length < 0:
                length = 0

            f.write(getShort(length))                       # Note tick
            # Note instrument
            f.write(getByte(note['instrument']))
            f.write(getByte(note['key']))

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('string', type=str,
                        help='String')
    args = parser.parse_args()

    convert(args.string)


import selenium

from fixtures import *
from ajax import *

import time
import pytest
URL = 'http://www.yelp.com/biz/lollys-cafe-new-bedford'
InvalidURL = 'http://www.tutorialspoint.com/python/list_len.htm'

@pytest.mark.webtest
def test_setup_selenium_with_invalid_url(setup_selenium):
    driver = setup_selenium
    driver.get("http://localhost:9999")
    elem = driver.find_element_by_id("searchbar-text-field")
    elem.send_keys(InvalidURL)
    elem.submit()
    time.sleep(0.5)

    error = driver.find_element_by_id("exception").text
    assert 'Invalid URL' in error
    #keys_point = driver.find_element_by_id("results-list").text
    #assert len(keys_point) == 0

@pytest.mark.webtest
def test_setup_selenium_test_if_URL_in_database(setup_selenium):
    driver = setup_selenium
    driver.get("http://localhost:9999")
    elem = driver.find_element_by_id("searchbar-text-field")
    elem.send_keys(URL)
    elem.submit()
    time.sleep(0.5)

    # Precondition
    error = driver.find_element_by_id('exception').text
    assert 'please wait' in error.lower()

    # Spin wait for key points
    for i in range(60):
        key_points = driver.find_elements_by_class_name("key-point")
        if len(key_points) > 0:
            break
        time.sleep(1)

    # Post condition
    assert len(key_points) == 3

#spin wait after delay function is added

@pytest.mark.webtest
def test_setup_selenium_with_url(setup_selenium):
    driver = setup_selenium
    driver.get("http://localhost:9999")
    elem = driver.find_element_by_id("searchbar-text-field")
    elem.send_keys(URL)
    elem.submit()
    time.sleep(0.5)

    # Doesn't ask user to wait
    with pytest.raises(selenium.common.exceptions.NoSuchElementException):
        error = driver.find_element_by_id('exception')

    #elem.send_keys(Keys.RETURN)
    #assert "No results found." not in driver.page_source
    key_points = driver.find_elements_by_class_name("key-point")
    assert len(key_points) == 3


from unittest import TestCase, main
import sys
# ZODB imports
import transaction
from ZODB.FileStorage import FileStorage
from ZODB import DB

from bokeep.book import BoKeepBookSet
from bokeep.util import ends_with_commit

from tests.test_transaction_and_module import Type1Transaction
from test_bokeep_book import BoKeepWithBookSetup, TESTBOOK

class BoKeepBasicTest(BoKeepWithBookSetup):
    def setUp(self):
        BoKeepWithBookSetup.setUp(self)
        self.trans_key = self.test_book_1.insert_transaction(
            Type1Transaction(None) )
        transaction.get().commit()

    def test_interesting_sequence(self):
        self.assertEquals( self.test_book_1.book_name, TESTBOOK )

        @ends_with_commit
        def simple_tests(book, key):
            trans = book.get_transaction(key)
            self.assertEquals( trans.data, "blah" )
            trans.data = "ha"
            self.assertEquals( trans.data, "ha" )
            trans.reset_data()
            self.assertEquals( trans.data, "blah" )
            trans.append_data(" shit")
            self.assertEquals( trans.data, "blah shit" )

        simple_tests(self.test_book_1, self.trans_key)

        @ends_with_commit
        def after_commit_read(book, trans_key):
            trans = book.get_transaction(trans_key)
            self.assertEquals( trans.data, "blah shit" )

        after_commit_read(self.test_book_1, self.trans_key)

        self.test_book_1.remove_transaction(self.trans_key)
        transaction.get().commit()

if __name__ == "__main__":
    main()


# _*_ coding:utf-8 _*_
__author__ = 'meto'
__date__ = '2018/10/30 18:04'

import sys

sys.path.append('G:/study/python3selenium3')
from selenium import webdriver
import time
import random
from PIL import Image
from util.rk import RClient
from Imooc_selenium.find_element import FindElement

class RegisterFunction(object):

    def __init__(self, url,i):
        self.driver = self.get_driver(url,i)

    # 获取driver并打开url
    def get_driver(self, url,i):
        if i == 0:
            driver = webdriver.Chrome()
        elif i == 1:
            driver = webdriver.Firefox()
        else:
            # driver = webdriver.Edge()
            pass
        driver.get(url)
        driver.maximize_window()
        return driver

    # 输入用户信息
    def send_user_info(self, key, data):
        self.get_user_element(key).send_keys(data)

    # 定位用户信息，获取element
    def get_user_element(self, key):
        find_element = FindElement(self.driver)
        user_element = find_element.get_element(key)
        return user_element

    # 获取随机数
    def get_range_user(self):
        user_info = ''.join(random.sample('1234567890abcdefghijklmnopqrstuvwxyz', 8))
        return user_info

    # 获取图片
    def get_code_image(self, file_name):
        self.driver.save_screenshot(file_name)
        code_element = self.get_user_element("code_image")
        # print(code_element.location)
        left = code_element.location['x']
        top = code_element.location['y']
        right = code_element.size['width'] + left
        height = code_element.size['height'] + top
        im = Image.open(file_name)
        img = im.crop((left, top, right, height))
        img.save(file_name)

    # 获取验证码
    def code_online(self, file_name):
        self.get_code_image(file_name)
        rc = RClient('meto001', 'agv10086', '115087', 'ac9b1b069fcc4f03a3108d16d5f67662')
        im = open(file_name, 'rb').read()
        key = rc.rk_create(im, 3050)['Result']
        return key

    def main(self):
        user_name_info = self.get_range_user()
        user_email = user_name_info + "@163.com"
        file_name = "G:/study/python3selenium3/Image/test.png"
        # code_text = self.code_online(file_name)
        self.send_user_info('user_email', user_email)
        self.send_user_info('user_name', user_name_info)
        self.send_user_info('password', '111111')
        # print(code_text)
        # self.send_user_info('code_text', code_text)
        self.get_user_element('register_button').click()
        code_error = self.get_user_element('code_text_error')

        if code_error is None:
            print('注册成功')
        else:
            self.driver.save_screenshot('G:/study/python3selenium3/Image/code_error.png')
        time.sleep(10)
        self.driver.close()

if __name__ == '__main__':
    for i in range(3):
        register_function = RegisterFunction('http://www.5itest.cn/register',i)
        register_function.main()


from APIOpenBD import APIOpenBD
from ElasticsearchWrapper import ElasticsearchWrapper
from flask import Flask, render_template, request, jsonify
import json
from jinja2 import Template, Environment, FileSystemLoader

import re

from jinja2 import evalcontextfilter, Markup, escape

_paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}')

class CustomFlask(Flask):
    '''
    テンプレートのデリミタがVue.jsと競合するので、Flask側でデリミタを別の文字に変更する
	参照：https://muunyblue.github.io/0b7acbba52fb92b2e9c818f7f56bac99.html
    '''
    jinja_options = Flask.jinja_options.copy()
    jinja_options.update(dict(
		block_start_string='(%',
		block_end_string='%)',
		variable_start_string='((',
		variable_end_string='))',
		comment_start_string='(#',
		comment_end_string='#)',
    ))

    template_filter = Flask.template_filter

app = CustomFlask(__name__)
#app = Flask(__name__)

# @app.template_filter()
# @evalcontextfilter
@app.template_filter('nl2br')
def nl2br(eval_ctx, value):
    result = u'\n\n'.join(u'
%s
' % p.replace('\n', '
\n') \
        for p in _paragraph_re.split(escape(value)))
    if eval_ctx.autoescape:
        result = Markup(result)
    return result

@app.template_filter('linebreaksbr')
def linebreaksbr(arg):
    return arg.replace("\n", "
\n")

# env = Environment(loader=FileSystemLoader('.'), trim_blocks=False)

# env.filters['linebreaksbr'] = linebreaksbr
# template = env.get_template('index.html')

# env.filters.update({
# 	"linebreaksbr": jinja2_custom_filters.linebreaksbr,
# })

app.config['JSON_AS_ASCII'] = False    # jsonifyで日本語が文字化けする場合の対処

@app.route("/")
def index():
	'''
	画面
	'''
	return render_template("index.html")
	# return template.render("index.html")

@app.route("/get")
def get():
	'''
	ISBNに対応する書籍情報の取得
	'''
	# パラメータからISBNコードを取得
	isbn = request.args.get('isbn', default=None)
	# 必要な情報を取得する
	json_data = APIOpenBD().get_json(isbn) if isbn else {}
	# dict型をJSON型のレスポンスに変換
	response = jsonify(json_data)

	return response

@app.route("/regist")
def regist():
	'''
	ISBNに対応する書籍情報を取得して、Elasticsearchに登録
	'''
	# パラメータからISBNコードを取得
	isbn = request.args.get('isbn', default=None).replace('-','')
	# 必要な情報を取得する
	json_data = APIOpenBD().get_json(isbn) if isbn else {}

	if json_data == None:
		json_data = {}

	if len(json_data) > 0:
		# Elasticsearch
		es = ElasticsearchWrapper('openbd', 'openbd-index')
		# 追加
		es.insert_one(json_data)

	# dict型をJSON型のレスポンスに変換
	response = jsonify(json_data)

	return response

@app.route("/search")
def search():
	'''
	検索
	'''
	# パラメータからISBNコードを取得
	isbn = request.args.get('isbn', default=None)
	title = request.args.get('title', default=None)
	author = request.args.get('author', default=None)
	publisher = request.args.get('publisher', default=None)
	publishedDate = request.args.get('publishedDate', default=None)
	description = request.args.get('description', default=None)

	# 検索の項目名、項目値のDictionary
	items = {}
	if isbn != None:
		items['isbn'] = isbn.replace('-','')
	if title != None:
		items['title'] = title
	if author != None:
		items['authors'] = author
	if publisher != None:
		items['publisher'] = publisher
	if publishedDate != None:
		items['publishedDate'] = publishedDate
	if description != None:
		items['description'] = description

	# Elasticsearch
	es = ElasticsearchWrapper('openbd', 'openbd-index')
	# 検索
	json_data = es.search_and(items)

	# dict型をJSON型のレスポンスに変換
	response = jsonify(json_data)

	return response

@app.route("/searchall")
def search_all():
	'''
	一覧
	'''
	# Elasticsearch
	es = ElasticsearchWrapper('openbd', 'openbd-index')
	# 検索
	json_data = es.search_all(1000)

	# dict型をJSON型のレスポンスに変換
	response = jsonify(json_data)

	return response

if __name__ == "__main__":
    app.run(debug=False, host='0.0.0.0', port=8080)


import sys
sys.path.append("../")

from NLG_BRAZILIAN_PORTUGUESE.geracao_funcoes_por_ordem.ordem_palavra.pal_nominais import *
import argparse

if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='Retorna pronomes no caso reto')
    parser.add_argument('pessoa_da_interlocucao', type=str)
    parser.add_argument('genero', type=str)
    parser.add_argument('numero', type=str)
    parser.add_argument('morfologia_do_pronome', type=str)
    args = parser.parse_args()

    print(realizacao_pronominal_casoreto(args.pessoa_da_interlocucao,
                                         args.genero, args.numero, args.morfologia_do_pronome))

# EX.:
#
# realizacao_pronominal_casoreto("não_interlocutor", "feminino", "singular",'morfologia_terceira_pessoa')
#
# realizacao_pronominal_casoreto("ouvinte", "feminino", "singular",'padrão', 'morfologia_terceira_pessoa)

# python3 main_pal_nominais.py não_interlocutor feminino singular morfologia_terceira_pessoa
# python3 main_pal_nominais.py ouvinte none singular morfologia_terceira_pessoa
# python3 main_pal_nominais.py ouvinte none singular padrão


# simple NN with skorch
# that does balanced dataset at training time
#
from collections import OrderedDict

import numpy as np

import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data
import torch.nn.functional as F
import skorch
from sklearn.model_selection import StratifiedKFold

def make_ff_nn(
        input_size,
        num_hidden_layers,
        hidden_size,
        activation,
        dropout,
):
    """
    Wrapper function to assemble feed forward NN using pytorch sequential
    """
    modules = [
        ("input_layer", nn.Linear(input_size, hidden_size)),
        ("input_drop_out", nn.Dropout(p=dropout)),
    ]
    for ix in range(num_hidden_layers - 1):
        l = nn.Linear(hidden_size, hidden_size)
        modules.append(("hidden_layer_{}".format(ix), l))
        if activation == "logistic":
            a = nn.Sigmoid()
        elif activation == "tanh":
            a = nn.Tanh()
        elif activation == "relu":
            a = nn.ReLU()
        else:
            raise ValueError("No such activation function defined")
        modules.append(("activation_{}".format(ix), a))
        if dropout > 0:
            modules.append(("drop_out_{}".format(ix), nn.Dropout(p=dropout)))
    # output layer
    modules.append(("output_layer", nn.Linear(hidden_size, 1)))
    modules.append(("output_fun", nn.Sigmoid()))
    return nn.Sequential(OrderedDict(modules))

class FeedForwardNN(nn.Module):
    """
    Simple feed forward network with dropout
    """
    def __init__(
            self,
            input_size,
            num_hidden_layers=3,
            hidden_size=100,
            activation="logistic",
            dropout=0.5,
    ):
        super().__init__()
        self.model = make_ff_nn(
            input_size,
            num_hidden_layers,
            hidden_size,
            activation,
            dropout,
        )

    def forward(self, X):
        return self.model(X)

def to_float32(elem):
    """
    Need to cast X or y to float32
    """
    return elem.astype(np.float32)

class PatchedNeuralNetClassifier(skorch.NeuralNetClassifier):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def fit(self, X, y):
        # conveniences
        if X.dtype != np.float32:
            X = to_float32(X)

        if y.dtype != np.float32:
            y = to_float32(y)

        # to align with sklearn expectations
        self.classes_ = np.array([False, True])
        super().fit(X, y)

    def predict_proba(self, X):
        prob_true = super().predict_proba(X)
        prob_false = 1.0 - prob_true
        return np.hstack((prob_false, prob_true))

    def predict(self, X):
        prob_true = super().predict_proba(X)
        return prob_true.flatten() > 0.5

class WeightedBCELoss(nn.Module):
    def __init__(self, weight=None):
        super().__init__()
        self.weight = None

    def forward(self, _input, target):
        # compute weights automatically
        if self.weight is None:
            num_pos = target.sum()
            num_neg = (1 - target).sum()
            # need to match dimensions
            weight = torch.tensor([1.0, num_neg / num_pos],
                                  dtype=torch.float32)
            weights = weight[target.to(torch.long)].reshape(-1, 1)
            weights = weights.to(_input.device)
        return F.binary_cross_entropy(
            _input,
            target.reshape(-1, 1),
            weight=weights,
        )

def get_nn():
    """
    Wrapper to produce model
    """
    net = PatchedNeuralNetClassifier(
        FeedForwardNN,
        optimizer=optim.Adam,
        criterion=WeightedBCELoss,
        # use all data in each batch, so that
        # the weighted loss makes sense...
        batch_size=-1,
        max_epochs=5,
        # early stopping
        callbacks=[
            ('early-stopping',
             skorch.callbacks.EarlyStopping(
                 monitor='valid_loss',
                 patience=3,
                 threshold=0.0001,
                 threshold_mode='rel',
                 lower_is_better=True,
             )),
        ],
        device="cuda",
    )
    return net


# -*- coding: utf-8 -*-
import numpy as np
np.set_printoptions(threshold='nan')
import os
from os import walk
import os.path as osPath
import math
import csv
from PIL import Image, ImageDraw, ImageFont, ImageColor

def generateRandomRule():
    myRule = ""
    for i in range(0,32):
         myRule += str(random.randint(0,1))
    return myRule

def remap(value, minInput, maxInput, minOutput, maxOutput):
    if minInput < maxInput:
        value = maxInput if value > maxInput else value
        value = minInput if value < minInput else value
        inputSpan = maxInput - minInput
        outputSpan = maxOutput - minOutput
        scaledThrust = float(value - minInput) / float(inputSpan)
        return minOutput + (scaledThrust * outputSpan)
    else:
        return minOutput

def getInspArrayRangeZero(inArLenght):
    rulesCount = pow(2, inArLenght)
    returnArray = []
    arRange = range(0, rulesCount)
    for i in arRange:
        returnArray.append(0)
    return returnArray

def getInspArraySeqence(inArLenght):
    rulesCount = pow(2, inArLenght)
    arRange = range(0, rulesCount)
    binSeqDiv = [pow(2, x) for x in range(0,inArLenght)]
    binSeqDiv.reverse()
    myArray = [[math.trunc(x / y) % 2 for x in arRange] for y in binSeqDiv]
    myArray = map(list, zip(*myArray))
    return myArray

def returnOne(inNum):
    if inNum > 1:
        return 1
    else:
        return inNum
def loadLayers(inDirPath):
    myDir = []
    truePixs = [[]]
    returnImages = []
    returnData = []
    lastImgSize = 0
    for i in inDirPath:
        myImgColors = []
        myImgs = []
        if os.path.isdir(i):
            myDir = os.listdir(i)
            myDir.sort()
#        print myDir
        if len(myDir) != 0:
            for j in myDir:
                lastImg = Image.open(i + "/" + j)
                lastImg.convert("1")
                lastImg.tobitmap(lastImg)
                lastImgSize = lastImg.size
                myImgColors.append(lastImg.getcolors())
                myImgs.append(lastImg)
#                print "LAST LOADED IMAGE: " + j
#                print "colors = rs = " + str(lastImg.getcolors())
        else:
            return []
        splitString = i.split("_")
        ruleNum = splitString[len(splitString)-2]
        returnData.append([myImgColors, lastImgSize[0], lastImgSize[1], ruleNum, myImgs])
    return returnData

def loadDirs(inDirPath, inRange):
    myDirs = []
    f =[]
    returnData = []
    liWidth = 0
    liHeight = 0
    if os.path.isdir(inDirPath):
        myDirs = os.listdir(inDirPath)
        myDirs.sort()
    if inRange[0] <= len(myDirs):
        startLayer = inRange[0]
        print ("startLayer: " +str(startLayer) + " total dirs " + str(len(myDirs)))
    else:
        return []
    if inRange[len(inRange)-1] <= len(myDirs):
        endLayer = len(inRange)-1
    elif inRange[len(inRange)-1] > len(myDirs):
        endLayer = len(myDirs) - 1
    else:
        return []
    for (path, dirnames, filenames) in walk(inDirPath):
        f.extend(dirnames)
        break
    for j, s in enumerate(f):
        f[j] = inDirPath + s
#    print myDirs[: inRange[len(inRange)-1]]
#    print f[inRange[0]:inRange[len(inRange)-1]]
    f.sort()
#    print f[inRange[0]:inRange[len(inRange)-1]]
    print("endLayer: " + str(endLayer))
    return f[inRange[0]: (inRange[len(inRange)-1])+1]

def getCross(inRules, inInspArraySequence, inWidth, inLogData):
    print("inRules")
    print(inRules)
    dirPath = r"/storage/emulated/0/CA/_moje pokusy/cross_test.png"
    recSize = inWidth / (len(inRules)*4+1.0)
    crossSize = recSize * 3
    gap = recSize
    fontSize = int(crossSize*0.8)
    crossOffset0Vert = fontSize + int(recSize)
    crossOffset0=0
    crossOffset1=recSize
    crossOffset2=recSize*2
    crossOffset3=crossSize
    text2Offset = crossOffset0Vert + int(crossOffset3) + int(recSize)+30
    imHeight = text2Offset + fontSize + int(recSize)
    fadeValue = 0.18
    returnArray = []
    myHSLcol = "hsl(0,100%,50%)"
    myFirstOcurences = inLogData[0]
    myOcurencesCount = inLogData[1]
    maxOcurenceValue = max(myOcurencesCount)
    scaleCount = 10
    scaleStep = maxOcurenceValue / scaleCount
    if scaleStep > 0:
        scaleRange = range(0,maxOcurenceValue,scaleStep)
    else:
        scaleRange = range(0,10,1)
    font = ImageFont.truetype("raavi.ttf", fontSize)
    im = Image.new('RGB', (inWidth,imHeight), (0, 0, 0))
    imD = ImageDraw.Draw(im)
    for i, s in enumerate(inInspArraySequence):
        fillColor0 = int(s[0]*(255*fadeValue+((1-fadeValue)*int(inRules[i])*255)))
#        frameColor = int(255*fadeValue+((1-fadeValue)*int(inRules[i])*255))
        frameColor = int(255*fadeValue)
        imD.rectangle([((crossSize*i+gap*i)+crossOffset1,crossOffset0Vert), (int((crossSize*i+gap*i)+crossOffset2), int(crossOffset0Vert+crossOffset1))], (fillColor0, fillColor0,fillColor0),(frameColor,frameColor,frameColor))
        returnArray.append(int((crossSize*i+gap*i)+crossOffset1))
        fillColor0 = int(s[1]*(255*fadeValue+((1-fadeValue)*int(inRules[i])*255)))
        imD.rectangle([((crossSize*i+gap*i)+crossOffset0,crossOffset0Vert + crossOffset1), ((crossSize*i+gap*i)+crossOffset1,crossOffset0Vert + crossOffset2)], (fillColor0, fillColor0,fillColor0),(frameColor,frameColor,frameColor))
        fillColor0 = int(s[2]*(255*fadeValue+((1-fadeValue)*int(inRules[i])*255)))
        imD.rectangle([((crossSize*i+gap*i)+crossOffset1,crossOffset0Vert + crossOffset1), ((crossSize*i+gap*i)+crossOffset2, crossOffset0Vert + crossOffset2)], (fillColor0, fillColor0,fillColor0),(frameColor,frameColor,frameColor))
        fillColor0 = int(s[3]*(255*fadeValue+((1-fadeValue)*int(inRules[i])*255)))
        imD.rectangle([((crossSize*i+gap*i)+crossOffset2,crossOffset0Vert + crossOffset1), ((crossSize*i+gap*i)+crossOffset3, crossOffset0Vert + crossOffset2)], (fillColor0, fillColor0,fillColor0),(frameColor,frameColor,frameColor))
        fillColor0 = int(s[4]*(255*fadeValue+((1-fadeValue)*int(inRules[i])*255)))
        imD.rectangle([((crossSize*i+gap*i)+crossOffset1,crossOffset0Vert + crossOffset2), ((crossSize*i+gap*i)+crossOffset2, crossOffset0Vert + crossOffset3)], (fillColor0, fillColor0,fillColor0),(frameColor,frameColor,frameColor))
        imD.text((int((crossSize*i+gap*i)+crossOffset1), text2Offset - fontSize),inRules[i],font=font, fill=(255,255,255,255))
        imD.text((int((crossSize*i+gap*i)+crossOffset1), 0), str(i),font=font, fill=(255,255,255,255))
        myHSLcol = "hsl(" + str(int(remap(myOcurencesCount[i], 0, maxOcurenceValue, 0, 185))) + ",100%, " + str(int(remap(myOcurencesCount[i], 0, maxOcurenceValue, 30, 70))) + "%)"
        imD.text((int((crossSize*i+gap*i)+crossOffset1), text2Offset - fontSize*2), str(myFirstOcurences[i]),font=font, fill=myHSLcol)
        textXpos = 3
        for s in scaleRange:
            myHSLcol = "hsl(" + str(int(remap(s, 0, maxOcurenceValue, 0, 185))) + ",100%, " + str(int(remap(s, 0, maxOcurenceValue, 30, 70))) + "%)"
            imD.text((textXpos, text2Offset), str(s),font=font, fill=myHSLcol)
            myTextSize = imD.textsize(str(s),font=font)
            textXpos += (myTextSize[0] + 15)
#    im.save(dirPath)
    return im

def createColorAnalyze(inColData, inSavePath, logReadDir):
    returnArray = []
    for i, imgs in enumerate(inColData):
        textHeight = 10
        resX = imgs[1]
        resY = imgs[2]
        columns = max(math.trunc(math.sqrt(len(imgs[4]))),20)
        maxValue = resX * resY
        nResX = resX * columns
        nResY = 600
        myValues =[]
        mySavePath = inSavePath + str(int(imgs[3],2)) + "_" + imgs[3] + "_" + str(resX) + "x" + str(resY) +"_analyse.png"
        mySavePathBig = inSavePath + str(int(imgs[3],2)) + "_" + imgs[3] + "_" + str(resX) + "x" + str(resY) +"_analyse_big.png"
        logReadPath = logReadDir +imgs[3] + "_" + str(resX) + "x" + str(resY) + ".csv"
        myLogsData = readCSV_Log(logReadPath)
        crossIm = getCross(imgs[3], getInspArraySeqence(5), nResX, myLogsData)
        pColXStart = 0
        pRowYStart = nResY + textHeight*10 + crossIm.size[1]
        mySAnal = Image.new("RGB",(nResX,nResY))
        myBAnal = Image.new("RGB",(columns * resX,nResY + textHeight*10 + int(len(imgs[4])/columns*resY + crossIm.size[1])),(0,0,0))
        myFont = ImageFont.truetype("raavi.ttf", textHeight)
        myFont2 = ImageFont.truetype("arial.ttf", textHeight)
 #+ textHeight + int(math.ceil(len(imgs[4])/columns))))
        mySAnalDraw = ImageDraw.Draw(mySAnal)
        myBAnalDraw = ImageDraw.Draw(myBAnal)
        myImgs = imgs[4]
        for j, cols in enumerate(imgs[0]):
            blackValue = 0
            whiteValue = 0
            bX = nResX / float(len(imgs[0]))
            pColX = pColXStart + ((j % columns)*resX)
            pRowY =	pRowYStart + math.trunc(j/columns)*resY
            for k, vals in enumerate(cols):
                if vals[1] == 0:
                    blackValue = vals[0]
                elif vals[1] == 255:
                    whiteValue = vals[0]
            myValues.append((pColX, pRowY))
            mySAnalDraw.rectangle([bX*j, 0, (bX*j)+bX, remap(whiteValue, 0, maxValue, 0, nResY)], (255,255,255))
            myBAnal.paste(myImgs[j], (pColX, pRowY))
        returnArray.append(myValues)
        myBAnal.paste(mySAnal, (0,0))
        myBAnal.paste(crossIm, (0,mySAnal.size[1]))
        myBAnalDraw.text((3,nResY+crossIm.size[1]), "RULE BINAR CODE: " + imgs[3],font=myFont2)
        myBAnalDraw.text((3,nResY+crossIm.size[1] + textHeight), "GENERATION COUNT: " + str(len(imgs[0])),font=myFont2)
        if len(myLogsData) != 0:
            myFirstOcurences = myLogsData[0]
            myOcurencesCount = myLogsData[1]
            myBAnalDraw.text((3,nResY+crossIm.size[1] + textHeight*2), "GENERATION OF FIRST OCURENCE: " + str(myFirstOcurences),font=myFont2)
            myBAnalDraw.text((3,nResY+crossIm.size[1] + textHeight*3), "OCURENCE COUNT: " + str(myOcurencesCount),font=myFont2)
        mySAnal.save(mySavePath)
        myBAnal.save(mySavePathBig)
    return returnArray

def readCSV_Log(inPath):
    returnArray = []
    inspArrayRange = getInspArrayRangeZero(5)
    firstOcure = getInspArrayRangeZero(5)
    if osPath.isfile(inPath) == True:
        print("Log File Exists: " + inPath)
        f = open(inPath, "r")
        with f:
            reader = csv.reader(f)
            layNum = 0
#            myRows = []
#            myCounts = []
            for i, row in enumerate(reader):
                if len(row) != 1 and len(row) >= 4:
#                    myRows.append(row)
                    myInspMatrixNum = int(row[3])
#                    myCounts.append(myInspMatrixNum)
                    myCount = inspArrayRange[myInspMatrixNum]
                    if myCount == 0:
                        firstOcure[myInspMatrixNum] = layNum + 1
                    myCount += 1
                    inspArrayRange[myInspMatrixNum] = myCount
                else:
#                    returnArray.append(myCounts)
                    layNum = layNum + 1
#                    myRows = []
#                    myCounts = []
    else:
        print("Log File Doesn't Exist: ")
    return [firstOcure, inspArrayRange]

def run(inDirPath):
    myDirs = loadDirs(inDirPath, range(0, 40))
    for i in myDirs:
        print(i)
    myData = loadLayers(myDirs)
    print("\n")
    print("myData: ")
    print("myDataLen " + str(len(myData)))
    for i, d in enumerate(myData):
        print("DIR_NUMBER: " + str(i))
        print("resX: " + str(d[1]))
        print("resY: " + str(d[2]))
        print("ruleNumber: ")
        print(d[4])
#        print myDirs[i]
        a=0
        for j in d[0]:
            print(str(a))
            print(j)
            a += 1
    myAnalyzeData = createColorAnalyze(myData, savePath, logReadDir)
    for i in myAnalyzeData:
        print(i)

def getInvertedDigit(inRule, inPos):
    returnString = ""
    listString = list(inRule)
    if listString[inPos] == '0':
        listString[inPos] = '1'
    elif listString[inPos] == '1':
        listString[inPos] = '0'
    return ''.join(listString)

dirPath = r"/storage/emulated/0/CA/_moje pokusy/CELULAR_AUTOMAT-2D/zaloha/z/"
savePath = r"/storage/emulated/0/CA/_moje pokusy/CELULAR_AUTOMAT-2D/analyse/interesting2/"
logReadDir = r"/storage/emulated/0/CA/_moje pokusy/CELULAR_AUTOMAT-2D/CSV_log/"

resolutionX = 51
resolutionY = 51
layersCount = 100
run(dirPath)


from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from operators.sas_to_redshift import SASfileToRedshiftOperator
from operators.copy_to_redshift import S3ToRedshiftOperator
from operators.load_fact import LoadFactOperator
from operators.clean_tables import CleanTablesOperator
from operators.data_quality import DataQualityOperator
from helpers.tables import Tables
from helpers.sql_queries import SqlQueries
from airflow.models import Variable

# Config
s3_bucket_name = Variable.get("s3_bucket")
aws_iam_arn = Variable.get("aws_iam_arn")

default_args = {
    'owner': 'hg',
    'start_date': datetime(2022, 2, 28),
    'depends_on_past': False,
    'retries': 1,
    'retry_delay': timedelta(minutes=5),
    'catchup': False,
    'email_on_retry': False,
}

dag = DAG('elt_dag',
          default_args=default_args,
          description='Load and transform data in Redshift with Airflow',
          schedule_interval='@monthly',
          )

start_operator = DummyOperator(task_id='Begin_execution',  dag=dag)
end_operator = DummyOperator(task_id='End_execution',  dag=dag)

# Copy tables from SAS file
for table in Tables.sas_tables:
    load_sas_table = SASfileToRedshiftOperator(
        task_id='loading_table_{}_from_sas_file'.format(
            table.get('table_name')),
        dag=dag,
        aws_credentials_id="aws_credentials",
        redshift_conn_id="redshift",
        s3_bucket=s3_bucket_name,
        s3_key="I94_SAS_Labels_Descriptions.SAS",
        aws_region="us-east-1",
        parse_value=table.get('parse_string'),
        end_string=table.get('end_string'),
        table_name=table.get('table_name'),
        table_columns=table.get('table_columns'),
    )

    # Perform quality checks
    run_quality_checks = DataQualityOperator(
        task_id='Run_data_quality_checks_on_{}'.format(
            table.get('table_name')),
        dag=dag,
        redshift_conn_id="redshift",
        table_name=table.get('table_name'),
        dq_test_list=table.get('dq_checks')
    )

    start_operator >> load_sas_table >> run_quality_checks >> end_operator

# Copy tables from csv and parquet
for table in Tables.csv_parq_tables:
    load_from_csv_parq = S3ToRedshiftOperator(
        task_id='loading_table_{}'.format(table.get('table_name')),
        dag=dag,
        redshift_conn_id="redshift",
        aws_iam_arn=aws_iam_arn,
        aws_region="us-east-1",
        table_name=table.get('table_name'),
        file_type=table.get('file_type'),
        s3_bucket=s3_bucket_name,
        s3_key=table.get('key'),
        extra_params=table.get('extra_params')
    )

    # Perform quality checks
    run_quality_checks = DataQualityOperator(
        task_id='Run_data_quality_checks_on_{}'.format(
            table.get('table_name')),
        dag=dag,
        redshift_conn_id="redshift",
        table_name=table.get('table_name'),
        dq_test_list=table.get('dq_checks')

    )
    start_operator >> load_from_csv_parq >> run_quality_checks >> end_operator

# Copy tables staging table airport code, world temp, us city demo
for table in Tables.csv_stg_tables:
    load_clean_from_csv_parq = S3ToRedshiftOperator(
        task_id='loading_table_{}'.format(table.get('table_name')),
        dag=dag,
        redshift_conn_id="redshift",
        aws_iam_arn=aws_iam_arn,
        aws_region="us-east-1",
        table_name=table.get('table_name'),
        file_type=table.get('file_type'),
        s3_bucket=s3_bucket_name,
        s3_key=table.get('key'),
        extra_params=table.get('extra_params')
    )

#     # Clean data
    clean_stg_table = CleanTablesOperator(
        task_id='clean_table_{}'.format(table.get('table_name')),
        dag=dag,
        redshift_conn_id="redshift",
        table=table.get('table_name'),
        sql_stmt=SqlQueries.clean_sql.get(table.get('table_name'))
    )

    # Perform quality checks
    run_quality_checks = DataQualityOperator(
        task_id='Run_data_quality_checks_on_{}'.format(
            table.get('table_name')),
        dag=dag,
        redshift_conn_id="redshift",
        table_name=table.get('table_name'),
        dq_test_list=table.get('dq_checks')
    )

    start_operator >> load_clean_from_csv_parq >> clean_stg_table
    clean_stg_table >> run_quality_checks >> end_operator

load_immigration_stg_table = S3ToRedshiftOperator(
    task_id='loading_table_{}'.format(
        Tables.parq_immigration.get('table_name')),
    dag=dag,
    redshift_conn_id="redshift",
    aws_iam_arn=aws_iam_arn,
    aws_region="us-east-1",
    table_name=Tables.parq_immigration.get('table_name'),
    file_type=Tables.parq_immigration.get('file_type'),
    s3_bucket=s3_bucket_name,
    s3_key=Tables.parq_immigration.get('key'),
    extra_params=Tables.parq_immigration.get('extra_params')
)

# clean data and load immigration fact table from immigration staging table
load_clean_immigration_table = LoadFactOperator(
    task_id='load_public.immigration_fact_table',
    dag=dag,
    redshift_conn_id="redshift",
    table_name="public.immigration",
    sql_stmt=SqlQueries.clean_load_immigration
)

run_quality_checks = DataQualityOperator(
    task_id='Run_data_quality_checks_on_{}'.format(
        Tables.parq_immigration.get('table_name')),
    dag=dag,
    redshift_conn_id="redshift",
    table_name=Tables.parq_immigration.get('table_name'),
    dq_test_list=Tables.parq_immigration.get('dq_checks')
)

start_operator >> load_immigration_stg_table >> load_clean_immigration_table
load_clean_immigration_table >> run_quality_checks >> end_operator


import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

@pytest.fixture()
def set_up():
    chrome_options = Options()
    chrome_options.add_argument("--disable-extensions")
    chrome_options.add_argument("--no-sandbox")
    pathToChrome = '/Users/bpyronn/Downloads/chromedriver'
    driver = webdriver.Chrome(executable_path=pathToChrome, chrome_options=chrome_options)
    driver.implicitly_wait(10)
    driver.maximize_window()
    driver.get("https://www.nespresso.com/ch/en/home")
    yield driver
    driver.close()
    driver.quit()


from django.urls import path
from . import views
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import views as auth_view

urlpatterns = [
    path('',views.login_page,name='Home'),
    #path('/accounts', views.uses, name="users"),
    path('users/', views.users, name='users.html'),
    path('users/users.html', views.users, name='users.html'),
    path('users/machines.html', views.MachineVision, name='machines.html'),
    path('users/report.html', views.IP_Camera, name='report.html'),
    path('users/image_tagging.html', views.Downloads, name='image_tagging.html'),
    path('users/machines/add_machine',views.add_machine,name="add_machine"),
    path('',views.logot,name="logout")

    # path('login/',views.login_page)

    # path('/accounts',views.home, name="Login Page")
    # # path('register/', views.register, name='register'),
    # path('profile/', views.profile, name='profile'),
    # path('login/', auth_view.LoginView.as_view(template_name='users/login.html'), name="login"),
    # path('logout/', auth_view.LogoutView.as_view(template_name='users/logout.html'), name="logout"),
]


import pandas as pd
from pathlib import Path
from sandu.data_types import SensitivityInput
import app.controllers.agents.sensitivity_analysis.sensitivity_model_inventory as inventory
from app.core.settings import DATA_PATH_LIVE
import json
import numpy as np
from typing import List, Tuple

"""
Parse Ensemble Time Series Format (ents) data sets to make sandu SensitivityInput objects for specified quantities
in quantities_of_interest which is defined in sensitivity_inventory.json.

The SensitivityInput objects are saved to json files which may be loaded into and used by the python package sandu.

If you have not used sandu before it will be helpful to consult the examples, at https://github.com/ErikRZH/sandu
"""

def output_file_name(ents_in: str, index: str) -> str:
    """ Returns the name and path to file output_i.csv given i.

    Args:
        ents_in: String containing the location of a dataset in ensemble time series format.
        index: The index i used to identify the model evaluation.

    Returns:
        output_file_name: The name and path to file output_i.csv.
    """
    output_file_name = ents_in / Path("output_" + str(index) + ".csv")
    return output_file_name

def make_dataframe(ents_in: str, quantities_of_interest: List[dict]) -> pd.DataFrame:
    """Gives a pandas dataframe of the type used by the sandu library for sensitivity analysis given a ents structure.

    Args:
        ents_in: String containing the location of a dataset in time series ensemble format.
        quantities_of_interest: List containing dictionaries with information needed to make SensitivityInput objects.
           One dictionary/entry corresponds to one quantity of interest.
            Dictionaries have keys,
            name: Name of the quantity, the SensitivtiyInput object is saved as: name_input.json
            mean: Column containing the mean of the quantity.
            variance: Column containing the variance of the quantity.

    Returns:
        df: dataframe containing parameters-quantity_of_interests_mean/variance
    """
    df = pd.read_csv(ents_in / "parameters.csv")
    df_temp = pd.read_csv(ents_in / "output_metadata.csv")
    desc = df_temp["description"]
    time_name = df_temp.loc[df_temp["description"] == "time_unit"]["output"].at[0]
    for quantity in quantities_of_interest:
        df[quantity["mean"]] = np.nan
        df[quantity["mean"]] = df[quantity["mean"]].astype(object)
        df[quantity["variance"]] = np.nan
        df[quantity["variance"]] = df[quantity["variance"]].astype(object)
        for i in df["index"]:
            # Sort the output time series by day and add them in the appropriate columns
            df.at[i, quantity["mean"]] = pd.read_csv(output_file_name(ents_in, i)).sort_values(time_name)[
                quantity["mean"]].tolist()
            df.at[i, quantity["variance"]] = pd.read_csv(output_file_name(ents_in, i)).sort_values(time_name)[
                quantity["variance"]].tolist()
    return df

def get_parameters_and_bounds(ents_in: str) -> Tuple[list, list]:
    """ Returns two lists needed by sandu, one containing the names and one the bounds of model parameters.

    Args:
        ents_in: String containing the location of a dataset in time series ensemble format.

    Returns:
        parameters: List of the names of the parameters, as required by sandu.
        bounds: List containing the lower and upper bounds of the parameters as [lower, upper],
            in the same order as the parameters list.
    """
    df = pd.read_csv(ents_in / "parameters_metadata.csv")
    parameters = df["parameter"].to_list()
    bounds = get_bounds(ents_in, parameters)
    return parameters, bounds

def get_bounds(ents_in: str, parameters):
    """ Returns a list containing the bounds of model parameters.

    Args:
        ents_in: String containing the location of a dataset in time series ensemble format.
        parameters: List of the names of the parameters, as required by sandu.

    Returns:
        bounds: List containing the lower and upper bounds of the parameters as [lower, upper],
            in the same order as the parameters list.
    """
    df = pd.read_csv(ents_in / "parameters.csv")
    bounds = []
    for parameter in parameters:
        upper_bound = df[parameter].max().item()
        lower_bound = df[parameter].min().item()
        bounds_entry = [lower_bound, upper_bound] if upper_bound != lower_bound else [upper_bound]
        bounds.append(bounds_entry)
    return bounds

def ents_to_sandu_agent():
    model_list = inventory.get_sensitivity_models()
    for model in model_list:
        # filepath to folder containing an Ensemble Time Series (ents) format dataset.
        folder = Path(DATA_PATH_LIVE)
        relative_location = Path("ents_format_datasets") / Path(model["name"])
        location = folder / relative_location

        # List containing dictionaries with information needed to make SensitivityInput objects.
        quantities_of_interest = model["quantities_of_interest"]

        df = make_dataframe(location, quantities_of_interest)
        parameters, bounds = get_parameters_and_bounds(location)

        for quantity in quantities_of_interest:
            filename = "models/sensitivity/" + model["name"] + "/" + quantity["name"] + "_raw.json"
            output_file = str(folder / filename)
            # make directory if it does not already exist
            parentfolder = Path(output_file).parents[0]
            parentfolder.mkdir(parents=True, exist_ok=True)  # Create directory for file if not already present

            new_sensitivity_input = SensitivityInput(df.to_json(index=False, orient="split"), parameters, bounds,
                                                     quantity["mean"], quantity["variance"])
            with open(output_file, "w", encoding="utf-8") as f:
                json.dump(new_sensitivity_input.__dict__, f, ensure_ascii=False, indent=4)

            # Open and load every object to validate it
            with open(output_file, "r") as read_file:
                x = json.load(read_file, object_hook=lambda d: SensitivityInput(**d))


#!/usr/bin/python
# Classification (U)

"""Program:  repair_db.py

    Description:  Unit testing of repair_db in mongo_db_admin.py.

    Usage:
        test/unit/mongo_db_admin/repair_db.py

    Arguments:

"""

# Libraries and Global Variables

# Standard
import sys
import os

if sys.version_info < (2, 7):
    import unittest2 as unittest
else:
    import unittest

# Third-party
import mock

# Local
sys.path.append(os.getcwd())
import mongo_db_admin
import version

__version__ = version.__version__

def run_repair():

    """Method:  run_repair

    Description:  Stub holder for run_repair function.

    Arguments:

    """

    return True

class Server(object):

    """Class:  Server

    Description:  Class stub holder for mongo_class.Server class.

    Methods:
        __init__

    """

    def __init__(self):

        """Method:  __init__

        Description:  Class initialization.

        Arguments:

        """

        pass

class UnitTest(unittest.TestCase):

    """Class:  UnitTest

    Description:  Class which is a representation of a unit testing.

    Methods:
        setUp
        test_repair_db2
        test_repair_db

    """

    def setUp(self):

        """Function:  setUp

        Description:  Initialization for unit testing.

        Arguments:

        """

        self.server = Server()
        self.args_array = {"-R": "Optionsetting"}

    @mock.patch("mongo_db_admin.process_request")
    def test_repair_db2(self, mock_process):

        """Function:  test_repair_db2

        Description:  Test repair_db function.

        Arguments:

        """

        mock_process.return_value = (True, "Error Message")

        self.assertEqual(
            mongo_db_admin.repair_db(
                self.server, self.args_array), (True, "Error Message"))

    @mock.patch("mongo_db_admin.process_request")
    def test_repair_db(self, mock_process):

        """Function:  test_repair_db

        Description:  Test repair_db function.

        Arguments:

        """

        mock_process.return_value = (False, None)

        self.assertEqual(
            mongo_db_admin.repair_db(
                self.server, self.args_array), (False, None))

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


# ***** BEGIN GPL LICENSE BLOCK *****
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ***** END GPL LICENCE BLOCK *****

import os
import time
import bpy
from bpy.app.handlers import persistent
from VisIt.lib import render_utils

@persistent
def post_handler(frame):
    context = bpy.context

class RenderAllCamsOperator(bpy.types.Operator):
    bl_idname = "vis_it.render_all_cams"
    bl_label = "Render all cams"
    bl_description = "Render all cameras in a scene as separate shots"
    bl_options = {'REGISTER', 'BLOCKING'}

    frame_padding: bpy.props.IntProperty(
        name="Frame padding",
        default=1,
        min=1,
        step=1,
    )

    def __init__(self):
        self.timer = None

    @classmethod
    def poll(cls, context):
        return context.area.type == 'VIEW_3D'

    def invoke(self, context, event):
        wm = context.window_manager
        self.timer = wm.event_timer_add(1, window=context.window)
        wm.modal_handler_add(self)
        return {'RUNNING_MODAL'}

    def modal(self, context, event):
        if event.type == 'ESC':
            self.cancel(context)
            return {'CANCELLED'}

        if event.type == 'TIMER':
            bpy.app.handlers.render_post.append(post_handler)

            wm = context.window_manager
            scene = context.scene
            previz_render_path = scene.render_dir
            scene_render_path = scene.render.filepath

            if len(previz_render_path.strip()) == 0:
                self.report({'ERROR'}, "Please choose a render directory")
                return {'CANCELLED'}

            cams = render_utils.get_scene_cameras(scene)
            override = {}

            for cam in cams:
                scene.camera = cam
                filename = f"{cam.name}_{'#' * self.frame_padding}"
                scene.render.filepath = os.path.join(previz_render_path, scene.name, cam.name, filename)

                # bpy.ops.render.view_show('INVOKE_DEFAULT')
                bpy.ops.render.render(animation=True)
                # bpy.ops.render.view_cancel('INVOKE_DEFAULT')

            scene.render.filepath = scene_render_path
            bpy.app.handlers.render_post.remove(post_handler)

            return {'FINISHED'}

        return {'PASS_THROUGH'}

    def cancel(self, context):
        wm = context.window_manager
        wm.event_timer_remove(self.timer)


import os
import re
from pathlib import Path

import pandas as pd
from dotenv import load_dotenv

BASE_DIR = Path(__file__).resolve().parent.parent
load_dotenv(dotenv_path='.env')

PANDAS_TO_JSON_PARAMS = {'force_ascii': False, 'orient': 'records'}

def clean_bank_name(bank_name):
    try:
        p = re.compile(r'[農|漁]會')
        pos = p.search(bank_name).end()
        return bank_name[:pos]
    except Exception as e:
        return None

def make_love_code_map():
    url = 'https://quality.data.gov.tw/dq_download_csv.php?nid=31868&md5_url=4d4f544023dda33ac54a3adca1aec4c9'
    df_raw = pd.read_csv(url, dtype={'捐贈碼': str})
    df_all = df_raw[['受捐贈機關或團體名稱', '捐贈碼']].rename(columns={
        '受捐贈機關或團體名稱': 'name',
        '捐贈碼': 'code'
    })
    df_all.to_json('tw_love_code_mapping.json', **PANDAS_TO_JSON_PARAMS)

def make_tw_bank_map():
    post_office_url = 'https://quality.data.gov.tw/dq_download_csv.php?nid=5950&md5_url=21483be4e8097acbd4f1e2f24e1bb3c1'
    bank_url = 'https://www.fisc.com.tw/TC/OPENDATA/R2_Location.csv'
    post_csv = pd.read_csv(post_office_url, dtype={'六碼郵遞區號': str})
    bank_csv = pd.read_csv(bank_url, dtype={'銀行代號': str, '分支機構代號': str})

    df_post = post_csv[post_csv['六碼郵遞區號'].notna()]
    df_bank = bank_csv[bank_csv['銀行代號'].notna()]
    bank_primary = df_bank[df_bank['分支機構代號'].notna()]
    # cleanup 金融機構名稱
    bank_primary.loc[bank_primary['金融機構名稱'].str.contains('[農|漁]會')].apply(
        lambda x: clean_bank_name(x))

    banks = bank_primary[['銀行代號', '分支機構代號', '金融機構名稱', '分支機構名稱', '地址']].rename(
        columns={
            '銀行代號': 'code',
            '分支機構代號': 'branch_code',
            '金融機構名稱': 'name',
            '分支機構名稱': 'branch_name',
            '地址': 'address'
        })
    posts = df_post.copy()
    posts = posts[['電腦局號', '局名', '縣市', '鄉鎮市區', '地址']]
    posts.rename(columns={
        '電腦局號': 'branch_code',
        '局名': 'branch_name'
    },
                 inplace=True)
    posts['address'] = posts['縣市'] + posts['鄉鎮市區'] + posts['地址']
    posts.drop(columns=['縣市', '鄉鎮市區', '地址'], inplace=True)
    posts['code'] = '700'
    posts['name'] = '中華郵政股份有限公司'

    df_all = pd.concat([posts, banks])
    df_all.to_json('tw_bank_code_mapping.json', **PANDAS_TO_JSON_PARAMS)

if __name__ == '__main__':
    make_tw_bank_map()
    make_love_code_map()


import requests
import datetime
from db import connect

file = open("API_KEY.txt","r")
API_KEY = file.read()

SYMBOL = 'VALE3'
OUTPUTSIZE = 'compact'  # compact: last 100 data points; full: last 20 years

def get_result():
    r = requests.get(
        'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=' + SYMBOL +
        '.SA&outputsize=' + OUTPUTSIZE + '&apikey={}'.format(API_KEY))

    if r.status_code == 200:
        data = r.json()
        data = data['Time Series (Daily)']

        result = []
        for d, p in data.items():
            if float(p['4. close']) == 0:
                continue

            date = datetime.datetime.strptime(d, '%Y-%m-%d')
            data_row = [date, float(p['1. open']), float(p['2. high']), float(p['3. low']), float(p['4. close']),
                        int(p['5. volume'])]
            result.append(data_row)

        return result

def import_data():
    con = connect()
    cursor = con.cursor()

    for item in get_result():
        sql = "INSERT INTO time_series_daily (symbol, date, open, high, low, close, volume) VALUES " \
              "(%s, %s, %s, %s, %s, %s, %s)"
        values = (SYMBOL, item[0], item[1], item[2], item[3], item[4], item[5])
        cursor.execute(sql, values)
        con.commit()
    con.close()

import_data()

file.close()
print("Dados importados com sucesso!")


import math
import random
import statistics
import numpy as np

#define points in spherical coordinates
# r is constant (1)
# azimuth (theta), where +X = 0deg
# elevation, ranges from -pi/2 to pi/2 (-90deg to +90deg)

def sph_random_distribution(point_qty, r):
    '''
    This function returns a list of points on a sphere
    Inputs:
        desired number of points
        radius of sphere
    Output:
        list of points
    '''

    point_list = []
    for i in range(point_qty):
        azimuth     = random.random() * 2*math.pi
        elevation   = (random.random()-.5) * math.pi
        point = (r, azimuth, elevation)
        point_list.append(point)
    return point_list

def central_angle(coord1, coord2):
    '''
    this function returns the central angle (in radians) between two points on a sphere
    the max return value is pi/2 = 180deg
    '''
    (r1, az1, el1), (r2, az2, el2) = coord1, coord2
    return np.arccos( (np.sin(az1)*np.sin(az2)) + np.cos(az1)*np.cos(az2)*np.cos(el1-el2) )

def fitness(list_of_points):
    #initialize list of central angles
    central_angle_list = []
    for coordinate in list_of_points[:-1]:
        #for each coordinate, calculate the central angle to every coordinate after it in the list and record value
        #don't compare the last coordinate in the list to anything
        #and don't compare a coordinate to itself
        for index in range(list_of_points.index(coordinate) + 1, len(list_of_points)):
            central_angle_list.append( central_angle(coordinate, list_of_points[index]) )
    #calculate values from list
    CAmin, CAmax, CAavg, CAstdev = min(central_angle_list), max(central_angle_list), sum(central_angle_list)/len(central_angle_list), statistics.stdev(central_angle_list)
    # return (CAmin, CAmax, CAavg)
    return CAstdev

def generate_population(population_size):
    #Generate pool of random distributions
    distribution_pool = []
    for i in range(population_size): distribution_pool.append(sph_random_distribution(number_of_points, unit_radius))
    return distribution_pool

def evaluate_population(distribution_pool):
    #Get fitness of each distribution and sort accordingly
    population_fitness=[]
    for distribution in distribution_pool: population_fitness.append( (fitness(distribution), distribution) )
    sorted_population = sorted(population_fitness, key=lambda x:x[0])

    # for member in sorted_population: print(member[0])
    return sorted_population

if __name__ == "__main__" :

    '''
    1) Create random distributions of points on sphere
    2) Calculate error function (standard deviation)
        for each distribution: get min, max, average central angle between points
    3) Optimize until convergence (standard deviation falls below thresh)

    '''

    number_of_points    = 4
    unit_radius         = 1
    population_size     = 10

    #initialize
    population = generate_population(population_size) #generate random population

    sorted_population = evaluate_population(population) #order population according to fitness
    print (len(sorted_population))


from django.shortcuts import render
import requests
from rest_framework import status
from rest_framework.parsers import MultiPartParser, FormParser
from rest_framework.response import Response
import json
from rest_framework.views import APIView
from api.serializers import  ImageSerializer, Base64ImageSerializer
from PIL import Image
from api.models import UploadImage
import torch
from torchvision import transforms
import pickle
from django.conf import settings
import os
import base64
from datetime import datetime
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
BASE_DIR = settings.BASE_DIR
MEDIA_ROOT = settings.MEDIA_ROOT

#inference transformation
inference_transformer = transforms.Compose([
                        transforms.Resize((224,224)),
                        transforms.ToTensor(),
                        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                                 std=[0.229, 0.224, 0.225])
])
mapping_file_path = os.path.join(BASE_DIR,'api','code_to_breed.pickle')
with open(mapping_file_path,'rb') as f:
    code_to_breed = pickle.load(f)

#load model
model_file_path = os.path.join(BASE_DIR,'api','complete_model.pt')
model = torch.load(model_file_path,map_location='cpu')

#pytorch inference
def run_inference(model,image_path,inference_transformer,code_to_breed):
    model.eval()
    image = Image.open(image_path)
    transformed_image = inference_transformer(image)
    if torch.cuda.is_available():
        model = model.cuda()
        transformed_image = transformed_image.cuda()
    transformed_image = transformed_image.unsqueeze(0)
    output = model(transformed_image).cpu()
    score,pred = torch.max(output,1)
    print(code_to_breed)
    print(output)
    print(pred)

    return score.data[0], code_to_breed[int(pred.cpu())]

# Create your views here.

def prepare_path(BASE_DIR,image_path):
    final_path = os.path.join(BASE_DIR,'dog_breed')
    image_path = image_path.split("/")
    for pt in image_path:
        final_path = os.path.join(final_path,pt)
    return final_path

class ImageView(APIView):
    parser_classes = [MultiPartParser,FormParser]

    serializer_class = ImageSerializer

    def post(self,request):
        serializer = self.serializer_class(data=request.data)
        if serializer.is_valid():
            serializer.save()
            image = serializer.data.get('image')
            try:
                image_path = prepare_path(BASE_DIR,image)
            except:
                 return Response({"Bad Request":"Invalid Image"},status=status.HTTP_400_BAD_REQUEST)

            score, pred = run_inference(model,image_path,inference_transformer,code_to_breed)
            os.remove(image_path)
            return Response({"Score":score, "Prediction":pred},status=status.HTTP_200_OK)
        return Response({"Bad Request":"Invalid Image"},status=status.HTTP_400_BAD_REQUEST)

class Base64ImageView(APIView):
    # parser_classes = [MultiPartParser,FormParser]
    serializer_class = Base64ImageSerializer

    def post(self,request):
        serializer = self.serializer_class(data=request.data)

        if serializer.is_valid():
            image_string = serializer.data.get('base64string')
            image_string = bytes(image_string,'utf-8')
            image = base64.decodestring(image_string + b'===')
            name = datetime.now().strftime('%d_%m_%Y_%H_%M_%S') + '.jpg'
            image_path = os.path.join(MEDIA_ROOT,'images',name)

            with open(image_path,'wb') as f:
                f.write(image)

            score, pred = run_inference(model,image_path,inference_transformer,code_to_breed)
            print(score,pred)
            os.remove(image_path)
            return Response({"Score":score, "Prediction":pred},status=status.HTTP_200_OK)

        return Response({"Bad Request":"Invalid Image"},status=status.HTTP_400_BAD_REQUEST)


import os,sys
import cv2
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from keras.models import Sequential, Model, load_model
from keras.layers import *
from keras.callbacks import *
from keras.utils import np_utils
from keras.optimizers import SGD, Adam
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt

def read_images_labels(path,i):
    for file in os.listdir(path):
        abs_path = os.path.abspath(os.path.join(path, file))
        if os.path.isdir(abs_path):
            i+=1
            temp = os.path.split(abs_path)[-1]
            name.append(temp)
            read_images_labels(abs_path,i)
            amount = int(len(os.listdir(path)))
            sys.stdout.write('\r'+'>'*(i)+' '*(amount-i)+'[%s%%]'%(i*100/amount)+temp)
        else:
            if file.endswith('.jpg'):
                image=cv2.resize(cv2.imread(abs_path),(128,128))
                images.append(image)
                labels.append(i-1)
    return images, labels, name

def read_images(path):
    images=[]
    for i in range(25):
        image=cv2.resize(cv2.imread(path+str(i+1)+'.jpg'),(128,128))
        images.append(image)
    images=np.array(images,dtype=np.float32)/255
    return images

def transform(listdir,label,lenSIZE):
    label_str=[]
    for i in range (lenSIZE):
        temp=listdir[label[i]]
        label_str.append(temp)
    return label_str

images = read_images('D:/GitHub/ACV/dataset/mingxang/test/')
model = load_model('./weight/0244_30-0.75.hdf5')

predict = model.predict_classes(images, verbose=1)
print(predict)
label_str=transform(np.loadtxt('name.txt',dtype='str'),predict,images.shape[0])

df = pd.DataFrame({"character":label_str})
df.index = np.arange(1, len(df) + 1)
df.index.names = ['id']
df.to_csv('./result/test.csv')


import itertools as it
#https://jmduke.com/posts/a-gentle-introduction-to-itertools/
letters = ['a', 'b', 'c', 'd', 'e', 'f']
booleans = [1, 0, 1, 0, 0, 1]
numbers = [23, 20, 44, 32, 7, 12]
decimals = [0.1, 0.7, 0.4, 0.4, 0.5]

#print list(it.chain(letters, booleans, decimals))
# print(*array, sep='')
print(*it.chain(letters, booleans, decimals))

# https://www.tutorialspoint.com/python3/python_lists.html
list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7]

print ("list1[0]: ", list1[0])
print ("list2[1:5]: ", list2[1:5])

data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
print(*it.accumulate(data, max))

print(*it.combinations('ABCD', 2))
# print(*it.permutations('1234567', 7))

matr = {}
matr[(0,0)] = 0
matr[(0,1)] = 15
matr[(0,2)] = 5
matr[(0,3)] = 16

matr[(1,0)] = 10
matr[(1,1)] = 0
matr[(1,2)] = 12
matr[(1,3)] = 11

matr[(2,0)] = 13
matr[(2,1)] = 9
matr[(2,2)] = 0
matr[(2,3)] = 8

matr[(3,0)] = 6
matr[(3,1)] = 14
matr[(3,2)] = 8
matr[(3,3)] = 0

print(matr[(0,0)])

# def davy_accumulate(iterable):
#     return matr[(int(elt1), int(elt2))]

def davy_accumulate(iterable):
    'Return running totals'
    # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
    # accumulate([1,2,3,4,5], operator.nul)  --> 1 2 6 24 120
    it = iter(iterable)
    try:
        total = next(it)
    except StopIteration:
        return
    yield total
    elt_prec="0"
    total0=0
    for element in it:

        dist = matr[(int(elt_prec),int(element))]
        total0 = total0+dist
        elt_prec=element
        yield total0

min_all=10000000000000000000001
min_trouve=10000000000000000000001
#for i in it.permutations('1234567', 7):
for i in it.permutations('123', 3):
    a=tuple('0')+i+tuple('0')
    print ("a :", a)
    response = list(davy_accumulate(a))
    min_all=min(min_all,response[4])
    if response[4] < min_trouve: #Si a est positif
        min_trouve=response[4]
        chemin_trouve=a

    # print(*davy_accumulate(a))
print ("min_trouve :", min_trouve)

print ("chemin_trouve :", chemin_trouve)


### 20190430
### resolute binvox to lower resolution

import os
import numpy as np
import binvox_rw
import argparse
import time

def log_error(error_log, f):
    """
    log error to a file
    :param error_log: path to error log. ends with ".txt"
    :param f: obj file name
    :return:
    """
    with open(error_log, "a+") as e:
        e.write(f)
    return

def dilate(volume, target_dim, thres):
    """
    dilate a higher dimension volume to lower dimension
    :param volume: 3D numpy array
    :param target_dim: target dimension
    :param thres: threshold of how many voxel fills count as one. float between 0 and 1
    :return: 3D numpy array of shape target_dim ** 3
    """
    src_dim = volume.shape[0]
    cube_dim = int(src_dim / target_dim)  # dimension of the small cube
    thres_num = int(cube_dim ** 3 * thres)

    newv = np.empty([target_dim, target_dim, target_dim])   # empty voxel to fill
    for x in range(target_dim):
        sx = cube_dim * x   # starting x
        ex = cube_dim * (x+1)  # ending x, not included in indexing
        for y in range(target_dim):
            sy = cube_dim * y   # starting y
            ey = cube_dim * (y+1)  # ending y
            for z in range(target_dim):
                sz = cube_dim * z   # starting z
                ez = cube_dim * (z+1)  # ending z
                cube = volume[sx:ex, sy:ey, sz:ez]   # sub cube of data
                if np.sum(cube) >= thres_num:
                    newv[x, y, z] = 1
                else:
                    newv[x, y, z] = 0
    return newv

def main(args):
    input_dir = args.input_dir
    output_dir = args.output_dir
    log_dir = args.log_dir
    error_log = os.path.join(log_dir, "error.txt")
    target_dim = 64

    allf = os.listdir(input_dir)
    start = time.time()
    for i, f in enumerate(allf):
        if (i % 1000 == 0):
            print("Now checking the {0}-th file. Elapsed time {1:.2f} min.".format(i, (time.time() - start) / 60))
        with open(os.path.join(input_dir, f), "rb") as b:
            model = binvox_rw.read_as_3d_array(b)
            volume = np.asarray(model.data*1, dtype=np.float32)
        # print("the shape of volume")
        # print(volume.shape)
        if np.sum(volume) == 0:
            print("Empty voxel! Logging...")
            log_error(error_log, f)
            continue
        else:
            newv = dilate(volume, target_dim, 0.5)
            outpath = os.path.join(output_dir, f.split(".")[0]+".npy")
            np.save(outpath, newv)
    return

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    # dir parameters
    parser.add_argument('--output_dir', type=str, default="../output",
                        help='output path')
    parser.add_argument('--input_dir', type=str, default='../input',
                        help='input path')
    parser.add_argument('--log_dir', type=str, default='/log/',
                        help='for tensorboard log path save in output_dir + log_dir')

    args = parser.parse_args()
    main(args)


from itertools import accumulate
from typing import AbstractSet

from .parse import coalesce, is_word, lower
from .types import Context, ContextualEdit, Edit

def _match(lhs: bool, existing: str, insertion: str) -> str:
    existing, insertion = lower(existing), lower(insertion)
    if lhs:
        for match in reversed(tuple(accumulate(insertion))):
            if match == existing[-len(match) :]:
                return match
        else:
            return ""
    else:
        for match in reversed(tuple(accumulate(reversed(insertion)))):
            if match == existing[: len(match) :]:
                return match
        else:
            return ""

def trans(line_before: str, line_after: str, edit: Edit) -> ContextualEdit:
    l_match = _match(True, existing=line_before, insertion=edit.new_text)
    rest = edit.new_text[len(l_match) :]
    r_match = _match(False, existing=line_after, insertion=rest)
    c_edit = ContextualEdit(
        new_text=edit.new_text,
        new_prefix=edit.new_text,
        old_prefix=line_before[-len(l_match) :] if l_match else "",
        old_suffix=line_after[: len(r_match)],
    )
    return c_edit

def trans_adjusted(
    unifying_chars: AbstractSet[str], ctx: Context, edit: Edit
) -> ContextualEdit:
    c_edit = trans(line_before=ctx.line_before, line_after=ctx.line_after, edit=edit)
    new_syms = len(tuple(coalesce(edit.new_text, unifying_chars=unifying_chars)))
    simple_before = (
        ctx.words_before
        if is_word(c_edit.new_text[:1], unifying_chars=unifying_chars)
        else ctx.syms_before
    )
    simple_after = (
        ctx.words_after
        if is_word(c_edit.new_text[-1:], unifying_chars=unifying_chars)
        else ""
    )

    old_prefix = simple_before if new_syms <= 1 else c_edit.old_prefix or simple_before
    old_suffix = simple_after if new_syms <= 1 else c_edit.old_suffix or simple_after

    adjusted = ContextualEdit(
        new_text=c_edit.new_text,
        new_prefix=c_edit.new_prefix,
        old_prefix=old_prefix,
        old_suffix=old_suffix,
    )
    return adjusted

def expand_tabs(context: Context, text: str) -> str:
    new_text = (
        text.expandtabs(context.tabstop)
        if context.expandtab
        else text.replace(" " * context.tabstop, "\t")
    )
    return new_text


from flask import Flask, render_template, request, jsonify, g
from jinja_maker import save

app = Flask(__name__)

widget_list = [
    "Frame",
    "ComboBox",
    "ScrollableFrame",
    "Textbox",
    "Button",
    "Label",
    "Entry",
    "OptionMenu",
    "SegmentedButton",
    "Switch",
    "CheckBox",
    "RadioButton",
    "Slider"
]

x = 0
y = 0
height = 100
width = 100
all = {}
selected = {"name": "", "x": 0, "y": 0, "height": 0, "width": 0, "type": "", "spec": {}}

def get_all_widget():
    all_widget = {}
    for w in all:
        all_widget[w] = get_position(w)
    return all_widget

def add_widget(widget):
    name = widget.name
    value = widget
    all[name] = value

def delete_widget(widget_name):
    del all[widget_name]

def get_size():
    return x, y, height, width

def set_sizing(**kwargs):
    global x, y, height, width
    for arg in kwargs:
        if arg == "x":
            x = kwargs[arg]
        elif arg == "y":
            y = kwargs[arg]
        elif arg == "height":
            height = kwargs[arg]
        elif arg == "width":
            width = kwargs[arg]
        else:
            print("error")

def get_final():
    full_dict = {}
    for widget in all:
        wid_class = all[widget]
        coord = wid_class.coordinate
        full_dict[widget] = {'type': wid_class.type, 'width': coord["width"], 'height': coord["height"],
                             'y': coord["y"], 'x': coord["x"]}
    return full_dict

def get_position(name):
    widget = all[name]
    old_x, old_y, h, w = widget.get_relative('x', 'y', 'height', 'width')
    new_x = (old_x / 100) * width
    new_y = (old_y / 100) * height
    new_h = (h / 100) * height
    new_w = (w / 100) * width

    return {"x": new_x, "y": new_y, "height": new_h, "width": new_w}

class Widget:
    def __init__(self, name, coordinate, widget_type="Frame"):
        self.name = name
        self.coordinate = coordinate
        self.type = widget_type

    def get_name(self):
        return self.name

    def get_relative(self, *args):
        if args:
            to_return = []
            for request_arg in args:
                try:
                    to_return.append(self.coordinate[request_arg])
                except:
                    print(
                        f"Warning: Exception occurred for argument :{request_arg}."
                        f" Valid argument are: x, y, height, width")
            return to_return
        else:
            return self.coordinate

@app.route('/')
def index():
    return render_template('index.html', all_widget=get_all_widget(), drop_values=widget_list, selected=selected)

@app.route('/save_all')
def save_all():
    final_widgets = get_final()
    # TODO ask for a path to save the template
    save("/Users/theomathieu/Downloads", final_widgets)  # CHANGE PATH
    return render_template('index.html', all_widget=get_all_widget(), drop_values=widget_list, selected=selected)

@app.route('/new_widget', methods=['POST'])
def new_widget():
    data = request.json
    test = Widget(data["name"], data["coords"])
    add_widget(test)
    return jsonify(success=True)

@app.route('/resize', methods=['POST'])
def resize():
    data = request.json
    set_sizing(**data)
    return jsonify(success=True)

@app.route('/select_widget', methods=['POST'])
def select_widget():
    data = request.json
    selected["name"] = data["name"]
    coords = get_position(selected["name"])
    selected["height"] = int(coords["height"])
    selected["width"] = int(coords["width"])
    selected["x"] = int(coords["x"])
    selected["y"] = int(coords["y"])
    selected["type"] = all[selected["name"]].type
    return jsonify(success=True)

@app.route('/delete', methods=['POST'])
def delete():
    data = request.json
    name = data["name"]
    delete_widget(name)
    global selected
    selected = {"name": "", "x": 0, "y": 0, "height": 0, "width": 0, "type": "", "spec": {}}
    return jsonify(success=True)

@app.route('/drop_choice', methods=['POST'])
def drop_choice():
    data = request.json
    type = data["type"]
    global selected
    selected["type"] = type
    all[selected["name"]].type = type
    return jsonify(success=True)

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


# Django libs
from django.shortcuts import render
from django.conf import settings
from django.http import StreamingHttpResponse

#Libraries of ourselves
from .form import ImageForm
from .models import Image
from .camera import VideoCamera
from .constants import BASE_URL, MARKET, SA, SA_D, ST_D, HEADER
from .utilities import egmap, process_json, predict_image, model

# Standard Libraries
import json
import logging

# 3rd party libraries
import requests as rq
import PIL
import numpy as np

res = ""  #result that we will pass to the result page

logger = logging.getLogger(__name__)

# Create your views here.

def home(request):
    return render(request, 'home.html', context={})

def form(request):
    global SA
    global model
    modified_image = Image()
    result_dic = {}
    genre=""
    img=""

    if request.method=="POST":
        form=ImageForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            form.save()
            logger.debug("Image saved")
            obj=form.instance
            logger.info(f"obj is {obj}")
            name_image= obj.image.name
            test_image = obj.image
            target_image = PIL.Image.open(str(settings.BASE_DIR) + obj.image.url)
            logger.info(f"{type(target_image)}")
            image_array = np.array(target_image)
            image_file, x1 = predict_image(image_array, name_image)
            logger.info(f"Image file type: {type(image_file)}")
            modified_image.image = image_file
            logger.debug("next step")
            modified_image.save()
            genre = egmap(x1)
            logger.info(f"genre is {genre}")
            if genre != '':
                SA = SA_D[genre]
                ST = ST_D[genre]
            url = BASE_URL+'?limit=14&market='+MARKET+'&seed_artists='+SA+'&seed_genres='+genre+'&seed_tracks='+ST
            r = rq.get(url, headers=HEADER)
            logger.info(f"{r.status_code}")

            if r.json() and r.status_code == 200:
                json_text = json.loads(r.text)
                result_dic = process_json(json_text["tracks"])
            else:
                logger.warning("Bad Request")

            logger.info(f"result is {result_dic}")

            return render(request, "predict.html", {"obj":obj, "prediction":x1, "modified_image":modified_image, "result_dic": result_dic})

    else:
        form = ImageForm()
        logger.debug("It came into else")
        img = Image.objects.last()
        logger.info(f"Image is {img}")

    return render(request, "predict.html", {"img":img,"form":form})

# def gen(camera):
#     while True:
#         frame = cam.get_frame()
#         # print(frame)
#         m_image, lab =predict_video(frame, "result")
#         print("This is in gen")
#         SA = SA_D[lab]
#         ST = ST_D[lab]
#         print(lab)
#         # m_image = cv2.cvtColor(m_image, cv2.COLOR_RGB2BGR)
#         ret, m_image = cv2.imencode('.jpg', m_image)
#         m_image = m_image.tobytes()
#         yield(b'--frame\r\n'
#               b'Content-Type: image/jpeg\r\n\r\n' + m_image + b'\r\n\r\n')

def gen(camera):
    while True:
        frame = camera.get_frame()
        yield(b'--frame\r\n'
              b'Content-Type: image/jpeg\r\n\r\n' +
              frame + b'\r\n\r\n'
        )

def livefeed(request):
    try:
        return StreamingHttpResponse(gen(()), content_type="multipart/x-mixed-replace;boundary=frame")
    except Exception as e:  # This is bad! replace it with proper handling
        logger.error(e)

def showlive(request):
    submitb = request.POST.get('submit')
    logger.info(submitb)
    context = {'submitb':submitb} if submitb else {'submitb': None}
    return render(request, 'live.html', context)

def liveres(request):
    logger.debug("This is live res")
    genre = egmap(res)
    logger.info("genre is {}".format(genre))
    if genre != '':
        SA = SA_D[genre]
        ST = ST_D[genre]
    url = BASE_URL+'?limit=7&market='+MARKET+'&seed_artists='+SA+'&seed_genres='+genre+'&seed_tracks='+ST
    r = rq.get(url, headers=HEADER)
    if r.status_code == 200 and r.json():
       json_text = json.loads(r.text)
       result_dic = process_json(json_text["tracks"])
    context = {'result_dic': result_dic, 'emotion': res}
    return render(request, 'liveres.html', context)


## For mobile devices ##

from google.cloud import storage
import tensorflow as tf
from PIL import Image
import numpy as np

BUCKET = "boom155" ##google cloud bucket name
CLASS_NAMES = ["Early Blight","Late Blight","Healthy"]

model = None

def download_blob(bucket_name,source_blob_name,destination_file_name):
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(source_blob_name)
    blob.download_to_filename(destination_file_name)

## predict function uploaded on the cloud which will be called everytime when someone uses the application
def predict(request):
    global model
    if model is None:
        download_blob(
            BUCKET,
            "models/mymodel_for_mobile.h5",
            "/tmp/mymodel_for_mobile.h5",
        )
        model = tf.keras.models.load_model("/tmp/mymodel_for_mobile.h5")

    image = request.files["file"]

    ## To resize every image file sent by the user/client to 256*256 dimensions
    image = np.array(Image.open(image).convert("RGB").resize((256,256)))
    img_array = tf.expand_dims(image,0)
    print(img_array)
    predictions = model.predict(img_array)
    print("Predictions:", predictions)
    index = np.argmax(predictions)
    print("index",index)
    predicted_class = CLASS_NAMES[index]
    confidence = round(100 * (np.max(predictions)), 2)

    return {"class": predicted_class, "confidence": confidence}


from tqdm             import tqdm
from autograd         import grad, hessian, jacobian
from copy             import copy
import autograd.numpy as np

from libs.operators        import positive_definite
from libs.line_search      import *

class QuasiNewton:
    def __init__(self, func, initialX, interval=[-1e15, 1e15], ftol=1e-6, maxIters=1e3, maxItersLS=200):
        self.costFunc   = func
        self.gradFunc   = grad(self.evaluate)
        self.hessFunc   = hessian(self.evaluate)
        self.maxIters   = int(maxIters)
        self.maxItersLS = maxItersLS
        self.interval   = interval
        self.fevals     = 0
        self.ftol       = ftol

        self.x_is_matrix= False

        if np.ndim(initialX) > 1:
            self.x_is_matrix = True
            initialX = np.squeeze(initialX)

        self.xLen = np.shape(initialX)[0]
        self.direction  = np.zeros((maxIters, self.xLen))
        self.x          = np.zeros((maxIters, self.xLen))
        self.x[0] = initialX

        self.gradient   = np.zeros((self.maxIters, self.xLen))
        self.S          = np.zeros((self.maxIters, self.xLen, self.xLen))

        self.epsilon1 = self.ftol
        self.k = 0
        self.m = 0
        self.rho = 0.1
        self.sigma = 0.7
        self.tau = 0.1
        self.chi = 0.75
        self.m_hat = 600
        self.epsilon2 = 1e-10

        self.S[0] = np.eye(self.xLen)

    def evaluate(self, x):
        if self.x_is_matrix:
            x = np.reshape(x, (self.xLen,1))
        result = self.costFunc(x)
        self.fevals += 1
        return result

    def line_search(self, x):
        def funcLS(alpha):
            return self.evaluate(x + alpha*self.direction[self.iter])

        lineSearch = FibonacciSearch(funcLS, self.interval, ftol=self.ftol, maxIters=self.maxItersLS)
        self.alpha[self.iter] = lineSearch.optimize()
        self.xLS = self.x[self.iter] + self.alpha[self.iter]*self.direction[self.iter]
        return self.xLS

    def get_S(self):
        pass

    # def get_direction(self, x):
    #     if self.k > 0:
    #         self.gradient[self.k] = self.gradFunc(self.x[self.k])
    #         self.S[self.k]        = self.get_S()
    #
    #     direction = -(self.S[self.k] @ self.gradient[self.k])
    #     return direction

    def get_alpha0(self):
        if np.abs(self.fLGrad) > self.epsilon2:
            self.alpha0 = -2*self.delta_f0/self.fLGrad
        else:
            self.alpha0 = 1

        if (self.alpha0 <= 0) or (self.alpha0 > 1):
            self.alpha0 = 1
        return self.alpha0

    def optimize(self):
        self.fevals = 0
        ## Step 1
        self.f0 = self.evaluate(self.x[0])
        self.gradient[0] = self.gradFunc(self.x[0])
        self.m += 2
        self.f_00 = copy(self.f0)
        self.delta_f0 = copy(self.f0)

        while self.k < self.maxIters-1:
            # print("Iter: ", self.k)
            self.escape = False
            ## Step 2
            self.direction[self.k] = -(self.S[self.k] @ self.gradient[self.k])
            self.alphaL = 0
            self.alphaU = 1e99
            self.fL = copy(self.f0)
            self.fLGrad = (self.gradFunc(self.x[self.k] + self.alphaL*self.direction[self.k]).T)@self.direction[self.k]
            self.alpha0 = self.get_alpha0()

            while self.escape == False:
                ## Step 3
                self.deltaK = self.alpha0*self.direction[self.k]
                self.f0 = self.evaluate(self.x[self.k] + self.deltaK)
                self.m += 1

                ## Step 4
                cond1 = self.f0 > self.fL + self.rho*(self.alpha0 - self.alphaL)*self.fLGrad
                cond2 = np.abs(self.fL - self.f0) > self.epsilon2
                if cond1 and cond2:
                    self.alphaU = self.alpha0
                    self.alpha0_hat = self.alphaL + ((self.alpha0 - self.alphaL)**2)*self.fLGrad/(2*(self.fL - self.f0 + (self.alpha0 - self.alphaL)*self.fLGrad))

                    self.alpha0L_hat = self.alphaL + self.tau*(self.alphaU - self.alphaL)
                    if self.alpha0_hat < self.alpha0L_hat:
                        self.alpha0_hat = self.alpha0L_hat

                    self.alpha0U_hat = self.alphaU - self.tau*(self.alphaU - self.alphaL)
                    if self.alpha0_hat > self.alpha0U_hat:
                        self.alpha0_hat = self.alpha0U_hat

                    self.alpha0 = self.alpha0_hat
                    # Go to Step 3
                else:
                    ## Step 5
                    self.f0Grad = (self.gradFunc(self.x[self.k] + self.alpha0*self.direction[self.k]).T)@self.direction[self.k]
                    self.m += 1

                    ## Step 6
                    cond1 = self.f0Grad < self.sigma*self.fLGrad
                    cond2 = np.abs(self.fL - self.f0) > self.epsilon2
                    if cond1 and cond2:
                        deltaAlpha0 = (self.alpha0 - self.alphaL)*self.f0Grad/(self.fLGrad - self.f0Grad)
                        if deltaAlpha0 <= 0:
                            self.alpha0_hat = 2*self.alpha0
                        else:
                            self.alpha0_hat = self.alpha0 + deltaAlpha0

                        self.alpha0U_hat = self.alpha0 + self.chi*(self.alphaU - self.alpha0)
                        if self.alpha0_hat > self.alpha0U_hat:
                            self.alpha0_hat = self.alpha0U_hat

                        self.alphaL = self.alpha0
                        self.alpha0 = self.alpha0_hat
                        self.fL = self.f0
                        self.fLGrad = self.f0Grad
                        # Go to Step 3
                    else:
                        ## Step 7
                        self.x[self.k+1] = self.x[self.k] + self.deltaK
                        self.delta_f0 = self.f_00 - self.f0
                        # Stopping condition
                        if np.linalg.norm(self.deltaK, ord=2) < self.epsilon1 and np.linalg.norm(self.delta_f0) < self.epsilon1:
                            print("Stopping conditions reached. Algorithm terminating.")
                            self.xOpt = self.x[self.k+1]
                            return self.xOpt, self.costFunc(self.xOpt), self.fevals
                        self.f_00 = self.f0

                        ## Step 8
                        self.gradient[self.k+1] = self.gradFunc(self.x[self.k+1])
                        self.gamma = self.gradient[self.k+1] - self.gradient[self.k]

                        self.D = self.deltaK.T@self.gamma
                        if self.D <= 0:
                            self.S[self.k+1] = np.eye(self.xLen)
                        else:
                            self.S[self.k+1] = self.get_S()
                        self.k += 1
                        self.escape == True
                        # Go to Step 2
        print("\nAlgorithm did not converge.")
        self.xOpt = self.x[self.k]
        return self.xOpt, self.costFunc(self.xOpt), self.fevals

class QuasiNewtonDFP(QuasiNewton):
    def get_S(self):
        arg1 = (self.deltaK @ np.transpose(self.deltaK))/(np.transpose(self.deltaK) @ self.gamma)
        arg2 = (self.S[self.k]@self.gamma)@(self.gamma.T@self.S[self.k])/(self.gamma.T@self.S[self.k]@self.gamma)

        self.S[self.k+1] = self.S[self.k] + arg1 - arg2
        return self.S[self.k+1]

class QuasiNewtonBFGS(QuasiNewton):
    def get_S(self):
        arg1 = (1 + ((self.gamma.T@self.S[self.k])@self.gamma)/(self.gamma.T@self.deltaK))*((self.deltaK@self.deltaK.T)/self.gamma.T@self.deltaK)
        arg2 = (self.deltaK@(self.gamma.T@self.S[self.k]) + self.S[self.k]@self.gamma@self.deltaK.T)/(self.gamma.T@self.deltaK)

        self.S[self.k+1] = self.S[self.k] + arg1 - arg2
        return self.S[self.k+1]


import logging
import datetime
from pymodbus.client import ModbusTcpClient

from sqlalchemy import (
    table,
    column,
    insert,
    MetaData,
    Integer,
    DateTime,
    Boolean,
    Column,
    String,
    Table,
)

class ModbusException(Exception):
    pass

def create_table(conn, table_name, schema: str = "public"):
    meta = MetaData(conn)
    dbtable = Table(
        table_name,
        meta,
        Column("query_time", DateTime(timezone=True)),
        Column("ip", String),
        Column("port", Integer),
        Column("register_address", Integer),
        Column("value", Integer),
        Column("create_date", DateTime(timezone=True)),
        Column("is_valid", Boolean),
        Column("unit", Integer),
        schema=schema,
    )

    dbtable.create(conn, checkfirst=True)

def get_table(table_name, schema: str = "public"):
    return table(
        table_name,
        column("query_time", DateTime(timezone=True)),
        column("ip", String),
        column("port", Integer),
        column("register_address", Integer),
        column("value", Integer),
        column("create_date", DateTime(timezone=True)),
        column("is_valid", Boolean),
        column("unit", Integer),
        schema=schema,
    )

def read_modbus(ip, port, type, register_address, count, slave, timeout=20):
    client = ModbusTcpClient(
        ip, timeout=timeout, RetryOnEmpty=True, retries=3, port=port
    )
    logging.info("getting registers from inverter")
    if type == "holding":
        registries = client.read_holding_registers(
            register_address, count=count, slave=slave
        )
    elif type == "input":
        registries = client.read_input_registers(
            register_address, count=count, slave=slave
        )
    else:
        raise NotImplementedError("type {} is not implemented".format(type))

    if registries.isError():
        logging.error(registries)
        raise ModbusException(str(registries))

    return registries.registers

def main_read_store(
    conn,
    table,
    ip,
    port,
    type,
    modbus_tuples,
    schema: str,
):
    dbtable = get_table(table, schema=schema)

    for unit, register_address, count in modbus_tuples:
        try:
            registries = read_modbus(
                ip,
                port,
                type,
                register_address,
                count,
                unit,
            )
            query_time = datetime.datetime.now(datetime.timezone.utc)

            for offset, register in enumerate(registries):
                insert_statement = insert(dbtable).values(
                    query_time=query_time,
                    ip=ip,
                    port=port,
                    register_address=register_address + offset,
                    value=register,
                    create_date=query_time,
                    is_valid=True,
                    unit=unit,
                )

                result = conn.execute(insert_statement)
        except ModbusException as e:
            logging.error(e)

            insert_statement = insert(dbtable).values(
                query_time=query_time,
                ip=ip,
                port=port,
                register_address=register_address,
                value=None,
                create_date=query_time,
                is_valid=False,
                unit=unit,
            )

            result = conn.execute(insert_statement)

    return 0


import logging

from odoo import fields, models

_logger = logging.getLogger(__name__)

class AddressAssociateToAddressAux(models.TransientModel):
    _description = 'Address Associate to Address (Aux)'
    _name = 'clv.address.associate_to_address_aux'

    def _default_address_ids(self):
        return self._context.get('active_ids')
    address_ids = fields.Many2many(
        comodel_name='clv.address',
        relation='clv_address_associate_to_address_aux_rel',
        string='Addresses',
        default=_default_address_ids
    )

    create_new_address_aux = fields.Boolean(
        string='Create new Address (Aux)',
        default=True,
        readonly=False
    )

    # @api.multi
    def _reopen_form(self):
        self.ensure_one()
        action = {
            'type': 'ir.actions.act_window',
            'res_model': self._name,
            'res_id': self.id,
            'view_type': 'form',
            'view_mode': 'form',
            'target': 'new',
        }
        return action

    # @api.multi
    def do_address_associate_to_address_aux(self):
        self.ensure_one()

        address_count = 0
        for address in self.address_ids:

            address_count += 1

            _logger.info(u'%s %s %s', '>>>>>', address_count, address.name)

            AddressAux = self.env['clv.address_aux']
            address_aux = AddressAux.search([
                ('related_address_id', '=', address.id),
            ])
            _logger.info(u'%s %s %s', '>>>>>>>>>>', 'address_aux_id:', address_aux.id)

            if address_aux.id is not False:

                new_address_aux = address_aux

            else:

                if self.create_new_address_aux:

                    values = {}
                    values['street_name'] = address.street_name

                    _logger.info(u'%s %s %s', '>>>>>>>>>>', 'values:', values)
                    new_address_aux = AddressAux.create(values)
                    _logger.info(u'%s %s %s', '>>>>>>>>>>', 'new_address_aux:', new_address_aux)

                    values = {}
                    values['related_address_id'] = address.id
                    _logger.info(u'%s %s %s', '>>>>>>>>>>', 'values:', values)
                    new_address_aux.write(values)

                    new_address_aux.do_address_aux_get_related_address_data()

        if address_count == 1:

            action = {
                'type': 'ir.actions.act_window',
                'name': 'Addresses (Aux)',
                'res_model': 'clv.address_aux',
                'res_id': new_address_aux.id,
                'view_type': 'form',
                'view_mode': 'tree,kanban,form',
                'target': 'current',
                'context': {'search_default_name': new_address_aux.name},
            }

        else:

            action = {
                'type': 'ir.actions.act_window',
                'name': 'Addresses (Aux)',
                'res_model': 'clv.address_aux',
                'view_type': 'form',
                'view_mode': 'tree,kanban,form',
                'target': 'current',
            }

        return action
        # return True


import abc

from prettytable import PrettyTable

class MonitorMixinBase(object):
  """
  Base class for MonitorMixin. Each subclass will be a mixin for a particular
  algorithm.

  All arguments, variables, and methods in monitor mixin classes should be
  prefixed with "mm" (to avoid collision with the classes they mix in to).
  """
  __metaclass__ = abc.ABCMeta

  def __init__(self, *args, **kwargs):
    """
    Note: If you set the kwarg "mmName", then pretty-printing of traces and
          metrics will include the name you specify as a tag before every title.
    """
    self.mmName = kwargs.get("mmName")
    if "mmName" in kwargs:
      del kwargs["mmName"]

    super(MonitorMixinBase, self).__init__(*args, **kwargs)

    # Mapping from key (string) => trace (Trace)
    self._mmTraces = None
    self._mmData = None
    self.mmClearHistory()

  def mmClearHistory(self):
    """
    Clears the stored history.
    """
    self._mmTraces = {}
    self._mmData = {}

  @staticmethod
  def mmPrettyPrintTraces(traces, breakOnResets=None):
    """
    Returns pretty-printed table of traces.

    @param traces (list) Traces to print in table
    @param breakOnResets (BoolsTrace) Trace of resets to break table on

    @return (string) Pretty-printed table of traces.
    """
    assert len(traces) > 0, "No traces found"
    table = PrettyTable(["#"] + [trace.prettyPrintTitle() for trace in traces])

    for i in xrange(len(traces[0].data)):
      if breakOnResets and breakOnResets.data[i]:
        table.add_row(["<reset>"] * (len(traces) + 1))
      table.add_row([i] +
        [trace.prettyPrintDatum(trace.data[i]) for trace in traces])

    return table.get_string().encode("utf-8")

  @staticmethod
  def mmPrettyPrintMetrics(metrics):
    """
    Returns pretty-printed table of metrics.

    @param metrics (list) Traces to print in table

    @return (string) Pretty-printed table of metrics.
    """
    assert len(metrics) > 0, "No metrics found"
    table = PrettyTable(["Metric",
                         "min", "max", "sum", "mean", "standard deviation"])

    for metric in metrics:
      table.add_row([metric.prettyPrintTitle(),
                     metric.min,
                     metric.max,
                     metric.sum,
                     metric.mean,
                     metric.standardDeviation])

    return table.get_string().encode("utf-8")

  def mmGetDefaultTraces(self, verbosity=1):
    """
    Returns list of default traces. (To be overridden.)

    @param verbosity (int) Verbosity level

    @return (list) Default traces
    """
    return []

  def mmGetDefaultMetrics(self, verbosity=1):
    """
    Returns list of default metrics. (To be overridden.)

    @param verbosity (int) Verbosity level

    @return (list) Default metrics
    """
    return []


import numpy as np
import cv2

pic = np.zeros((500, 500, 3), dtype =  'uint8') # Crea una imagen 500x500 fondo negro de int 8

font = cv2.FONT_HERSHEY_COMPLEX
cv2.putText(pic, 'Angel', (100, 100), font, 3, (255,255,255), 4, cv2.LINE_8)

cv2.imshow('Dark', pic)
cv2.waitKey(0)
cv2.destroyAllWindows()