# -*- coding:utf-8 -*-
# import re
# sys.path.append('..')

from time import sleep
import unittest
import lib.public_functions as pubfuc
import multiprocessing
from appium import webdriver
import testcase.QSStarlight_behavior as QSStarlight_behavior
import logging

logger = logging.getLogger('autolog')

class QSStarlightTest(unittest.TestCase):

    def setUp(self):
        # 在当前目录，新建mail.txt，文件第一行存放设备列表，第二行存放roomid
        with open('mail.txt', 'r') as f:
            info = f.readlines()
        devicelist = info[0].rstrip().split(',')
        logger.info(f'devicelist: {devicelist}')
        self.device_name = devicelist
        self.roomid = info[1].split('#')[0].rstrip()
        self.sd = pubfuc.StartDriver(devicelist)
        self.proc_list = []

        pubfuc.cleannodeproc()
        for i in range(len(self.sd.devicelist)):
            self.proc_list.append(multiprocessing.Process(target=self.sd.startappiumserver, args=(i,)))

        for pro in self.proc_list:
            pro.start()

        for pro in self.proc_list:
            pro.join()

        while len(self.sd.getnodeprocpid()) < len(devicelist):
            sleep(1)

        logger.info(self.sd.getnodeprocpid())

        self.driverlist = []

        for i in range(len(self.sd.devicelist)):
            logger.info(i)
            desire_caps = self.sd.realdevice[i]
            if 'bundleId' in desire_caps:
                desire_caps['bundleId'] = 'com.powerinfo.iLiveQSStarLight'  # bootstrapA50
                # desire_caps['waitForQuiescence'] = 'false'
            else:
                desire_caps['appPackage'] = 'com.powerinfo.iLiveQSStarLight'
                desire_caps['appActivity'] = 'com.powerinfo.iLiveQSStarLight.StarLightLoginSettingActivity'
            driver = webdriver.Remote(f"http://localhost:{self.sd.aport[i]}/wd/hub", self.sd.realdevice[i])
            self.driverlist.append(driver)

        logger.info(self.driverlist)

    def test_001参与者多次加入离开房间(self):
        logger.info('test_001参与者多次加入离开房间')
        procs = []
        pool = multiprocessing.Pool(processes=len(self.driverlist))
        for driver in self.driverlist:
            index = self.driverlist.index(driver)
            proc = pool.apply_async(QSStarlight_behavior.joinandleaveroom, (driver, self.roomid, self.device_name[index], ))
            procs.append(proc)
        for i in procs:
            i.get()
        for i in procs:
            i.wait()

    def test_002参与者多次切后台(self):
        logger.info('test_003参与者多次切后台')
        procs = []
        pool = multiprocessing.Pool(processes=len(self.driverlist))
        for driver in self.driverlist:
            index = self.driverlist.index(driver)
            proc = pool.apply_async(QSStarlight_behavior.backapp, (driver, self.roomid, self.device_name[index], ))
            procs.append(proc)
        for i in procs:
            i.get()
        for i in procs:
            i.wait()

    def test_004多次切换摄像头(self):
        logger.info('test_007多次切换摄像头')
        procs = []
        pool = multiprocessing.Pool(processes=len(self.driverlist))
        for driver in self.driverlist:
            index = self.driverlist.index(driver)
            proc = pool.apply_async(QSStarlight_behavior.switchcamera, (driver, self.roomid, self.device_name[index],))
            procs.append(proc)
        for i in procs:
            i.get()
        for i in procs:
            i.wait()

    def test_005参与者多次锁屏(self):
        logger.info('test_004参与者多次创建房间')
        procs = []
        pool = multiprocessing.Pool(processes=len(self.driverlist))
        for driver in self.driverlist:
            index = self.driverlist.index(driver)
            proc = pool.apply_async(QSStarlight_behavior.lockscreen, (driver, self.roomid, self.device_name[index], ))
            procs.append(proc)
        for i in procs:
            i.get()
        for i in procs:
            i.wait()

    def test_006参与者切后台(self):
        logger.info('test_006参与者切后台')
        driver_zhubo, driver_fubo = self.driverlist[0], self.driverlist[1]
        devicedriverinfo = driver_zhubo.desired_capabilities  # 获取正在运行的设备的参数设置
        elementinfo, deviceid = pubfuc.getiteminfo(devicedriverinfo, 'bootstrap')
        QSStarlight_behavior.startnewroom(driver_zhubo, elementinfo)
        size = driver_fubo.get_window_size()
        print(size)
        if 'desired' not in driver_fubo.desired_capabilities:
            driver_fubo.execute_script("mobile: dragFromToForDuration",
                                  {'fromX': 100, 'fromY': size['height'] / 2, 'toX': 100,
                                   'toY': size['height'] - 100, 'duration': 2})
            print(2)
        else:
            driver_fubo.swipe(30, size['height'] / 2, 30, size['height'] - 100, 200)
        page_info = driver_fubo.page_source
        print(page_info)
        import re
        room_id = re.findall('RoomI.+name=', page_info)
        print(room_id)
        # room_id = '1'
        # a50_behavior.backapp(driver_fubo, self.roomid, self.device_name[1])

    def tearDown(self):
        logger.info('test运行完成')
        # quite the device driver
        # pass
        for driver in self.driverlist:
            driver.quit()
        # for proc in self.proc_list:
            # print(proc.is_alive())
            # proc.terminate()


import cv2
import config

haar_faces = cv2.CascadeClassifier(config.get("haar_cascades_file"))

def detect_multi(image):
    """Return bounds (x, y, width, height) of detected face in grayscale image.
    If no face or more than one face are detected, None is returned.
    """
    faces = haar_faces.detectMultiScale(image,
                                        scaleFactor=config.get("haar_scale_factor"),
                                        minNeighbors=config.get("haar_min_neighbors"),
                                        minSize=tuple(config.get("haar_min_size")),
                                        flags=cv2.CASCADE_SCALE_IMAGE)
    if len(faces) > 0:
        return faces
    else:
        return None

def detect_single(image):
    faces = detect_single(image)

    if faces is not None:
        return faces[0]
    else:
        return None

def crop(image, x, y, w, h):
    """Crop box defined by x, y (upper left corner) and w, h (width and height)
    to an image with the same aspect ratio as the face training data.  Might
    return a smaller crop if the box is near the edge of the image.
    """
    crop_height = int((config.get("face_height") / float(config.get("face_width"))) * w)
    midy = y + h / 2
    y1 = max(0, midy - crop_height / 2)
    y2 = min(image.shape[0] - 1, midy + crop_height / 2)
    return image[y1:y2, x:x + w]

def resize(image):
    """Resize a face image to the proper size for training and detection.
    """
    return cv2.resize(image, (config.get("face_width"), config.get("face_height")), interpolation=cv2.INTER_LANCZOS4)


import pandas as pd
import numpy as np
import sklearn
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_squared_error
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score, f1_score

df = pd.read_csv("FlightDelays.csv")      				#reads the flight delay csv file
df["DEP_DELAY"] = df["DEP_TIME"] - df["CRS_DEP_TIME"]
df["Flight Status"] = np.where(df["Flight Status"] == "ontime",0 ,1)

x1 = pd.get_dummies(df.ORIGIN)
x2 = pd.get_dummies(df.DEST)
x3 = pd.get_dummies(df.CARRIER)
x4 = pd.get_dummies(df.FL_NUM)
x6 = pd.get_dummies(df.Weather)
x7 = pd.get_dummies(df.TAIL_NUM)
x8 = pd.get_dummies(df.DAY_WEEK)
x9 = pd.get_dummies(df.DAY_OF_MONTH)
x10 = df[["DISTANCE","DEP_DELAY"]]

x10 = x10.apply(lambda x: (x - x.min(axis=0))/(x.max(axis=0) - x.min(axis=0)))

x = pd.concat([x1,x2,x3,x4,x6,x7,x8,x9,x10], axis=1)
y = df[["Flight Status"]]

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4)
model = LogisticRegression()
model.fit(x_train, y_train)

y_pred = model.predict(x_test)
acc = model.score(x_test,y_test)
RMSE = mean_squared_error(y_pred,y_test)
cm = confusion_matrix(y_test,y_pred)
f1 = f1_score(y_test, y_pred)

print("Accuracy of the model is : ",acc)
print("RMSE value is : ",RMSE)
print("f1_score is : ",f1)
print("confusin matrix for the model is : ",cm)


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

from django.conf import settings
from django.core.files import File

from collections import defaultdict
import traceback
import codecs
import os
import time
import logging

from analyse.models import MappingedFile
import util

errlog = logging.getLogger('daserr')

TMP_PATH = settings.MY_TMP_PATH

def third_to_source(third_file, source_files, member):
    third_fields = third_file.fields.split(",")
    third_index = [i for i,j in enumerate(third_fields) if j]
    third_res = defaultdict(str)
    skip_lines = third_file.skip_lines
    third_heads = ''

    for line in util.iter_txt(settings.MEDIA_ROOT + third_file.file.name):
        try:
            line = line.strip().split(third_file.splitor)
            if skip_lines > 0:
                if not third_heads:
                    third_heads = line[len(third_index):]
                    skip_lines -= 1
                continue
            key = []
            for index in third_index:
                key.append(line[index].strip())
            third_res['|'.join(key)]=third_file.splitor.join(line[len(third_index):]).strip()
        except:
            errlog.error('读取第三方匹配文件出错：' + third_file.file.name)
    for source_file in source_files.select_related('custom__analyst_custom'):
        source_fields = source_file.fields.split(',')
        source_index = []
        source_file_name = os.path.basename(str(source_file))
        tmp_file = source_file_name[:-4] + '_' + third_file.file_from + source_file_name[-4:]
        tmp_file_crypt = source_file_name[:-4] + '_' + third_file.file_from + '_crypt' + source_file_name[-4:]

        #crypt_fun = {}
        try:
            for fields in third_fields:
                source_index.append(source_fields.index(fields))
            #if fields == 'id':
                #crypt_fun[source_fields.index(fields)] = util.crypt_id
            #elif fields == 'cell':
                #crypt_fun[source_fields.index(fields)] = util.crypt_cell
            #elif fields == 'mail':
                #crypt_fun[source_fields.index(fields)] = util.crypt_mail
            #else:
                #continue
        except:
            errlog.error(traceback.format_exc())
            continue

        timestamp = time.strftime('%Y%m%d-%H%M')

        fh = codecs.open(TMP_PATH + timestamp + tmp_file, 'w', encoding='utf8')
        cfh = codecs.open(TMP_PATH + timestamp + tmp_file_crypt, 'w', encoding='utf8')
        fh.write('openid,' + ','.join(source_file.fields.split(',') + third_heads) + '\n')
        cfh.write('openid,' +  ','.join(source_file.fields.split(',') + third_heads) + '\n')

        line_num = 0
        openid_prex = '%05d' % source_file.id
        skip_lines = source_file.skip_lines
        for line in util.iter_txt(settings.MEDIA_ROOT + source_file.filename.name):
            try:
                line = line.strip().split(source_file.splitor)
                line_num += 1
                if skip_lines > 0:
                    skip_lines -= 1
                    continue
                key = []
                for i in source_index:
                    key.append(line[i])
                if not third_res['|'.join(key)]:
                    continue
                tmp = [openid_prex + ('%07d' % line_num)] + line + [third_res['|'.join(key)]]
                fh.write(','.join(tmp) + '\n')
                ll = util.crypt_line(','.join(line), source_file.fields, ',')
                tmp = [openid_prex + ('%07d' % line_num)] + ll.split(',') + [third_res['|'.join(key)]]
                cfh.write(','.join(tmp) + '\n')
            except:
                errlog.error('合并第三方结果出错：', source_file.filename.name)
                errlog.error(traceback.format_exc())
        fh.close()
        cfh.close()
        try:
            mf = MappingedFile()
            mf.source_file = source_file
            mf.customer = source_file.custom
            mf.parent_file = third_file
            mf.file_size = os.path.getsize(TMP_PATH + timestamp + tmp_file)
            mf.file_from = third_file.file_from
            mf.is_third_file = True
            mf.is_crypt = False
            mf.file.save('_'.join([os.path.splitext(tmp_file)[0], timestamp]) + '.txt',
                    File(open(TMP_PATH + timestamp + tmp_file)))
            mf.can_down.add(member)
            mf.can_down.add(source_file.custom)
            mf.save()

            mf = MappingedFile()
            mf.source_file = source_file
            mf.customer = source_file.custom
            mf.parent_file = third_file
            mf.file_size = os.path.getsize(TMP_PATH + timestamp + tmp_file_crypt)
            mf.file_from = third_file.file_from
            mf.is_third_file = True
            mf.is_crypt = True
            mf.file.save('_'.join([os.path.splitext(tmp_file_crypt)[0], timestamp]) + '.txt',
                    File(open(TMP_PATH + timestamp + tmp_file_crypt)))
            mf.can_down.add(member)
            mf.can_down.add(source_file.custom.analyst_custom)
            mf.save()
        except:
            errlog.error(traceback.format_exc())
        finally:
            util.try_delete_file(TMP_PATH + timestamp + tmp_file)
            util.try_delete_file(TMP_PATH + timestamp + tmp_file_crypt)


"""
Calculate Depreciation Rates (calc_rates):
-------------------------------------------------------------------------------
Last updated: 4/29/2016.

This module provides functions for calculating economic and tax depreciation
rates. All of the data is processed into NAICS trees by other modules, and
taken in as input of these functions.
"""
# Packages:
import os.path
import numpy as np
import pandas as pd
import xlrd
import ipdb
# Directories:
_CUR_DIR = os.path.dirname(__file__)
_MAIN_DIR = os.path.dirname(_CUR_DIR)
_DATA_DIR = os.path.join(_MAIN_DIR, 'data')
_DEPR_DIR = os.path.join(_DATA_DIR, 'depreciation_rates')
# Importing custom modules:
import naics_processing as naics
import constants as cst
# Full file paths:
_ECON_DEPR_IN_PATH = os.path.join(_DEPR_DIR, "Economic Depreciation Rates.csv")
_TAX_DEPR_IN_PATH = os.path.join(_DEPR_DIR, "BEA_IRS_Crosswalk.csv")

def calc_depr_rates(asset_tree, inv_tree, land_tree):
    # Opening file containing depreciation rates by asset type:
    depr_econ = pd.read_csv(_ECON_DEPR_IN_PATH)
    depr_econ = depr_econ.fillna(0)
    econ_assets = depr_econ["Asset"]
    econ_rates = depr_econ["Economic Depreciation Rate"]
    #
    types = ["All", "Corp", "Non-Corp"]
    #Initialize tree for depreciation rates:
    depr_tree = naics.generate_tree()
    depr_tree.append_all(df_nm="Economic", df_cols=types)
    #Makes a list of all the assets
    asset_list = asset_tree.enum_inds[0].data.dfs['Corp'].columns
    asset_list = asset_list.values.tolist()
    #Runs three times, once for all the assets, once for the corporate assets, and once for non-corporate assets
    for i in types:
        #Iterates over every industry in the tree
        for j in xrange(0, len(depr_tree.enum_inds)):
            asset_depreciation = 0
            total_depreciation = 0
            #grabs the assets for the industry
            asset_df = asset_tree.enum_inds[j].data.dfs[i]
            #Iterates over each asset in the industry
            for k in xrange(0, len(asset_list)):
                #calculates the sum of all the depreciation in the industry, multiplying the amount of each asset by its corresponding depreciation rate
                asset_depreciation += (asset_df.iloc[0,k] * econ_rates[k])
            #calculates the total capital stock in the industry
            tot_assets = sum(asset_tree.enum_inds[j].data.dfs[i].iloc[0,:])
            tot_inv = inv_tree.enum_inds[j].data.dfs["Inventories"][i][0]
            tot_land = land_tree.enum_inds[j].data.dfs["Land"][i][0]
            total_capital_stock = tot_assets + tot_inv + tot_land
            if(total_capital_stock != 0):
                #calculates the weighted average depreciation rate for assets only (can be changed to include inventories and land)
                depr_tree.enum_inds[j].data.dfs['Economic'][i].iloc[0] = asset_depreciation / tot_assets
            else:
                depr_tree.enum_inds[j].data.dfs['Economic'][i].iloc[0] = 0

    return depr_tree

def calc_tax_depr_rates(asset_tree, inv_tree, land_tree):
    #
    tax_data = pd.read_csv(_TAX_DEPR_IN_PATH).fillna(0)
    tax_assets = tax_data["Asset Type"]
    # Real Interest Rate:
    r = .05
    #
    tax_mthds = {"GDS 200%": 2.0, "GDS 150%": 1.5, "GDS SL": 1.0, "ADS SL": 1.0}
    tax_cols = {'Asset','Tax_Depreciation_Rate'}
    tax_rates = pd.DataFrame(np.zeros((len(tax_assets),len(tax_cols))), columns = tax_cols)
    tax_rates['Asset'] = tax_assets

    # Compute the tax depreciation rates:
    for i in xrange(0, len(tax_assets)):
        tax_method = tax_data['Method'][i]
        tax_system = tax_data['System'][i]
        tax_life = tax_data[tax_system][i]
        tax_b = tax_mthds[tax_method]
        tax_beta = tax_b / tax_life
        if(tax_method == 'GDS 200%' or tax_method == 'GDS 150%'):
            tax_star = tax_life * (1 - (1/tax_b))
            tax_z = (((tax_beta/(tax_beta+r))* (1-np.exp(-1*(tax_beta+r)*tax_star)))
                      + ((np.exp(-1*tax_beta*tax_star)/((tax_life-tax_star)*r))* (np.exp(-1*r*tax_star)-np.exp(-1*r*tax_life))))
            tax_rates.iloc[i,0] = r/((1/tax_z)-1)
        else:
            tax_z = ((1-np.exp(-1*r*tax_life)) / (r*tax_life))
            tax_rates.iloc[i,0] = (tax_z * r) / (1 + r - tax_z)
    #
    types = ["All", "Corp", "Non-Corp"]
    # Initialize tax depreciation rates tree:
    depr_tree = naics.generate_tree()
    depr_tree.append_all(df_nm="Tax", df_cols=types)
    #
    asset_list = asset_tree.enum_inds[0].data.dfs['Corp'].columns
    asset_list = asset_list.values.tolist()
    for i in types:
        #Iterates over every industry in the tree
        for j in xrange(0, len(depr_tree.enum_inds)):
            asset_depreciation = 0
            total_depreciation = 0
            #grabs the assets for the industry
            asset_df = asset_tree.enum_inds[j].data.dfs[i]
            #Iterates over each asset in the industry
            for k in xrange(0, len(asset_list)):
                #calculates the sum of all the depreciation in the industry, multiplying the amount of each asset by its corresponding depreciation rate
                asset_depreciation += (asset_df.iloc[0,k] * tax_rates['Tax_Depreciation_Rate'][k])

            #calculates the total capital stock in the industry
            tot_assets = sum(asset_tree.enum_inds[j].data.dfs[i].iloc[0,:])
            tot_inv = inv_tree.enum_inds[j].data.dfs["Inventories"][i][0]
            tot_land = land_tree.enum_inds[j].data.dfs["Land"][i][0]
            total_capital_stock = tot_assets + tot_inv + tot_land
            if(total_capital_stock != 0):
                #calculates the weighted average depreciation rate for assets only (can be changed to include inventories and land)
                depr_tree.enum_inds[j].data.dfs['Tax'][i].iloc[0] = asset_depreciation / tot_assets
            else:
                depr_tree.enum_inds[j].data.dfs['Tax'][i].iloc[0] = 0

    return depr_tree


#!/bin/env python

from flask import Flask
import redis

app = Flask(__name__)

kv = redis.Redis(host="redis", decode_responses=True)

@app.route("/")
@app.route("/index")
def base():
    ## we want to refresh this each time we refresh the page incase it's
    ## not ready on first startup.
    viewer0 = kv.hgetall("viewer0")
    notebookurl = "http://localhost/notebook"
    neuroglancerurl = f"http://localhost/nglancer/v/{viewer0['token']}/"

    ## this just bruteforce dumps html into a return output instead of a polished
    # template but should be fine as a demo.
    return f"""

        <title>Home Page - Neuroglancer</title>

neuroglancercv

Links:

         Nueroglancer Interface

         iPython Notebooks

"""

    # return "PNI Neuroglancer and Cloudvolume demo program"

if __name__ == "__main__":
    app.run(host="0.0.0.0")


class Conta:

    def __init__(self, nome="", conta="", agencia = '', saldo=0):
        self.nome = nome
        self.conta = conta
        self.agencia = agencia
        self.saldo = saldo

    def alterarNomeConta(self):
         self.nome = input('Digite o novo nome do titular:\n')
         self.menu()

    def deposito(self):
        deposito = float(input('Informe o valor do depósito:\n'))
        self.saldo = self.saldo + deposito
        self.menu()

    def saque(self):
        novoSaque = float(input('Informe o valor do saque:\n'))
        self.saldo = self.saldo - novoSaque
        self.menu()

    def consulta(self):
        print('Nome do titular: ' + self.nome)
        print('Número da conta: ' + self.conta)
        print('Número da agencia: ' + self.agencia)
        print('Saldo atual: ' + str(self.saldo))
        print('\n')
        voltaMenu = input("Digite 'r' para retornar ao menu principal:\n")
        if(voltaMenu == 'r'):
            self.menu()
        else:
            print('Opção inválida, digite novamente.')
            self.consulta()

    def menu(self):
        print('Escolha uma das opções para começar:\n')
        print('1 - Alterar nome da conta\n')
        print('2 - Realizar saque\n')
        print('3 - Realizar depósito\n')
        print('4 - Extrato\n')
        print('5 - Consultar detalhes da conta\n')
        print('6- Voltar ao menu inicial\n')
        resposta = input('Opção:\n')
        if(resposta == '1'):
          self.alterarNomeConta()
        elif(resposta == '2'):
          self.saque()
        elif(resposta == '3'):
          self.deposito()
        elif(resposta == '4'):
          print('Seu saldo é de: ' + str(self.saldo))
          self.menu()
        elif(resposta == '5'):
          self.consulta()
        elif(resposta == '6'):
          banco.menuBanco()

class Banco:
    listaDeContas = []

    def iniciaConta(self):
        nome = input('Digite o nome do titular:\n')
        agencia = input ('Por favor, informe a agencia: \n')
        conta = input('Digite o número da conta:\n')
        novaConta = Conta(nome, conta, agencia)
        self.listaDeContas.append(novaConta)
        self.menuBanco()

    def acessarConta(self):
        conta = input('Por favor, entre com o número da conta:\n')
        for conta in self.listaDeContas:
            if(conta == conta.conta):
                conta.menu()

    def excluirConta(self):
        conta = input('Por favor, entre com o número da conta:\n')
        for conta in self.listaDeContas:
            if(conta == conta.conta):
                print(conta.nome)
                print(conta.conta)
                resposta = input('Tem certeza da exclusão? [s/n]\n')
                if(resposta == 's'):
                    self.listaDeContas.remove(conta)
                elif(resposta == 'n'):
                    self.menuBanco()
                else:
                    print('Opção inválida!')
                    self.excluirConta()

    def menuBanco(self):

        print('Escolha uma das opções para começar:\n')
        print('1 - Criar uma conta\n')
        print('2 - Acessar uma conta \n')
        print('3 - Excluir uma conta \n')
        resposta = input('Opção:\n')
        if(resposta == '1'):
            self.iniciaConta()
        elif(resposta == '2'):
            self.acessarConta()
        elif(resposta == '3'):
            self.excluirConta()

banco = Banco()
banco.menuBanco()


#!/usr/bin/env python3
import argparse
import os
import re
import subprocess
import tinify

class Settings:
    debug = False

def run_cmd(cmd_args: list, cwd=None):
    cmd = " ".join(cmd_args)
    if Settings.debug:
        print(f"running:\n{cmd}")
    subprocess.run(cmd_args, check=True, cwd=cwd)

def main():
    parser = argparse.ArgumentParser(
        description="""Given an image file, such as,
            img.png
        convert to jpg and make tiny:
            img.tiny.jpg
    """
    )
    parser.add_argument(
        "--debug",
        dest="debug",
        action="store_true",
        help="Set debug mode output.",
    )
    parser.add_argument(
        "--keep-original",
        dest="keep",
        action="store_true",
        help="Do not delete original file.",
    )
    parser.add_argument(
        "file",
        type=str,
        help="input img file",
        default=None,
    )
    args = parser.parse_args()
    f_source = args.file
    is_jpg = False
    rx_jpg = re.compile(r"\.(jpg|jpeg)$", re.I)

    if args.debug:
        Settings.debug = True

    if not os.path.isfile(f_source):
        parser.error(f"file '{args.file}' does not exist.")

    is_jpg = True if re.search(rx_jpg, f_source) else False

    if re.search(r"\.tiny\.jpg$", f_source):
        print(f"already tiny: {args.file}")
        return

    source_fn = os.path.basename(f_source)

    source_abs_path = os.path.abspath(f_source)
    cwd = os.path.dirname(source_abs_path)

    no_ext_fn = re.sub(r"(\.[^\.]+)$", "", source_fn)
    # jpg_fn = f"{no_ext_fn}.jpg"
    jpg_abs_path = os.path.join(cwd, f"{no_ext_fn}.jpg")
    tiny_abs_path = os.path.join(cwd, f"{no_ext_fn}.tiny.jpg")

    if not is_jpg and not os.path.isfile(jpg_abs_path):
        print(f"convert to jpg: {source_fn}")
        # convert to jpg with imagemagick
        cmd_args = ["convert", source_fn, jpg_abs_path]
        run_cmd(cmd_args, cwd)
        source_fn = jpg_abs_path

    if os.path.isfile(tiny_abs_path):
        print(f"already exists: {tiny_abs_path}")
        return

    tinify.key = os.environ.get("TINY_JPG_API_KEY")
    print(f"tinify: {source_fn}")
    source = tinify.from_file(source_fn)
    source.to_file(tiny_abs_path)

    if not args.keep:
        if os.path.isfile(tiny_abs_path):
            print(f"rm: {args.file}")
            os.remove(args.file)
        else:
            print(f"ERROR, no tiny file: {tiny_abs_path}")

if __name__ == "__main__":
    main()


#even-odd finding in a list in python

list=[23,4,5,6,78,90,33]

even=[]
odd=[]

for i in list:
    if i%2==0:
        even.append(i)
    else:
        odd.append(i)

print(min(even))
print(max(odd))


from spack import *
import os
class R(Package):
    """R is 'GNU S', a freely available language and environment for
       statistical computing and graphics which provides a wide va
       riety of statistical and graphical techniques: linear and
       nonlinear modelling, statistical tests, time series analysis,
       classification, clustering, etc. Please consult the R project
       homepage for further information."""
    homepage = "http://www.example.com"
    url      = "http://cran.cnr.berkeley.edu/src/base/R-3/R-3.1.2.tar.gz"

    version('3.1.2', '3af29ec06704cbd08d4ba8d69250ae74')

    depends_on("readline %gcc@4.9.2")
    depends_on("jpeg %gcc@4.9.2")
    depends_on("cairo %gcc@4.9.2")

    def install(self, spec, prefix):
        readline = spec["readline"]
        os.environ['CFLAGS'] = "-O2 -I%s" % readline.prefix.include
        os.environ['CXXFLAGS'] = "-O2"
        os.environ['FFLAGS'] = "-O2"
        os.environ["LDFLAGS"] = "-L%s  -lreadline -lncurses" % readline.prefix.lib
        configure("--prefix=%s" % prefix,
                  "--enable-R-shlib",
                  "--enable-BLAS-shlib",
                  "--with-readline=%s" % readline.prefix,
                  "--with-cairo=%s" % spec["cairo"].prefix,
                  "--with-libpng=%s" % spec["libpng"].prefix,)
        make()
        make("install")


from tkinter import *
from tkinter import ttk
root = Tk()

#orient 'VERTICAL' or 'HORIZONTAL'
#length = # of pixels
progressbar = ttk.Progressbar (root, orient = HORIZONTAL, length = 200)
progressbar.pack()
#update progress
# If you cannot determine how much more time might remain for an operation,
#then you should use the indeterminate mode with the progress bar will just
#show the activity is still taking place without specifying the time that
#remains.
progressbar.config(mode = "indeterminate")
progressbar.start()
progressbar.stop()

#If you know how many steps the operation your tracking will take, and can
#calculate its progress, then using the determinate mode will allow you to
#manually update the value of the progress bar to represent how many steps are
#left.
progressbar.config(mode = "determinate", maximum = 11.0, value = 4.2)
progressbar.config(value = 8.0)
#increment by 1
progressbar.step()
progressbar.step(5)
value = DoubleVar()
progressbar.config(variable = value)
scale = ttk.Scale(root, orient = HORIZONTAL, length = 400, variable = value,
                  from_ = 0.0, to = 11.0)
scale.pack()
scale.set(4.2)
print (scale.get())

Python/Lynda Videos/Python GUI Development with Tkinter/Chapter 03/06_InputtingValuesAndDisplayingStatusWithTheScaleAndProgressbarWidgets.py


# -*- coding: utf-8 -*-
import requests
import traceback
from datetime import datetime

def crossTaskUpload(values, serverIP):
    try:
        turl = "http://%s:8080/ba/addWeather.action"%(serverIP)
        #values = {'comments': comments}
        msgSession = requests.Session()
        r = msgSession.post(turl, data=values)
        print(r.text)
    except Exception as e:
        tstr = traceback.format_exc()
        print(tstr)

def crossTaskUploadTest():
    serverIP = '127.0.0.1'
    values={'siteId':1,'timeUtc':datetime.now(),'weather':'clear','temperature':0,'humidity':0,'cloudRatio':0,'windSpeed':0,'rainfall':0}
    crossTaskUpload(values, serverIP)

if __name__ == "__main__":

    crossTaskUploadTest()


from random import randint
from gamefunctions import gameVars

def winorlose(status):
	# print("called win or lose", status, "\n")
	print("You", status, "! Would you like to play again?")
	choice = input ("Y / N")

	if choice == "Y" or choice == "y":
			global player_lives
			global computer_lives
			global player
			global computer
			gameVars.player_lives = 5
			gameVars.computer_lives = 5
			gameVars.player = False
			gameVars.computer = gameVars.choices[randint(0,2)]

	elif choice == "N" or choice == "n":
			print("You chose quit. Better luck next time!")
			exit()
	else:
			print("Make a valid choice. Yes or No!")
			winorlose(status)


# Michael O'Regan 05/May/2019
# http://interactivepython.org/courselib/static/pythonds/SortSearch/TheMergeSort.html

import time
import statistics
import numpy as np                              # importing numpy as np

np.random.seed(1)                                # seeding random on seed 1 so that all the arrays are the same

a = np.random.randint(200000, size=100)          # array of size 100
b = np.random.randint(200000, size=250)          # array of size 250
c = np.random.randint(200000, size=500)          # array of size 500
d = np.random.randint(200000, size=750)          # array of size 750
e = np.random.randint(200000, size=1000)         # array of size 1000
f = np.random.randint(200000, size=2500)         # array of size 2500
g = np.random.randint(200000, size=5000)         # array of size 5000
h = np.random.randint(200000, size=7500)         # array of size 7500
i = np.random.randint(200000, size=10000)        # array of size 10000
j = np.random.randint(200000, size=15000)        # array of size 15000
k = np.random.randint(200000, size=20000)        # array of size 20000
l = np.random.randint(200000, size=30000)        # array of size 30000
m = np.random.randint(200000, size=50000)        # array of size 50000

def mergeSort(alist):
    if len(alist)>1:
        mid = len(alist)//2
        lefthalf = alist[:mid]
        righthalf = alist[mid:]

        mergeSort(lefthalf)
        mergeSort(righthalf)

        i=0
        j=0
        k=0
        while i < len(lefthalf) and j < len(righthalf):
            if lefthalf[i] < righthalf[j]:
                alist[k]=lefthalf[i]
                i=i+1
            else:
                alist[k]=righthalf[j]
                j=j+1
            k=k+1

        while i < len(lefthalf):
            alist[k]=lefthalf[i]
            i=i+1
            k=k+1

        while j < len(righthalf):
            alist[k]=righthalf[j]
            j=j+1
            k=k+1

num_runs = 10                                       # set the benchamrk number of runs at 10
for r in range(num_runs):                           # looping the timing of the algorithm for num_runs
    np.random.seed(1)                               # setting the random seed (1)
    a = np.random.randint(200000, size=100)         # array of size 100
    start_time = time.time_ns()                     # setting start time to the time now in nanoseconds, https://docs.python.org/3/library/time.html#time.time
    mergeSort(a)                                   # calling algorithm for array a
    end_time = time.time_ns()                       # setting end time to the time now in nanoseconds
    time_elapsed = (end_time - start_time)*10**-6   # setting time elapsed to the difference of start and end to get the length of the running time of the algorithm
    resultsa = []                                   # setting results a to an empty array to fill with results
    resultsa.append(time_elapsed)                   # append the results array ie fill the results array with results of the runs as per num runs
ra = round(statistics.mean(resultsa),3)             # setting ra to the mean of results rounded to 3 decimal places https://www.geeksforgeeks.org/python-statistics-mean-function/
for r in range(num_runs):
    np.random.seed(1)
    b = np.random.randint(200000, size=250)          # array of size 250
    start_time = time.time_ns()
    mergeSort(b)                                    # calling algorithm for array b
    end_time = time.time_ns()
    time_elapsed = (end_time - start_time)*10**-6
    resultsb = []
    resultsb.append(time_elapsed)
rb = round(statistics.mean(resultsb),3)              # setting rb to the mean of results rounded to 3 decimal places
for r in range(num_runs):
    np.random.seed(1)
    c = np.random.randint(200000, size=500)          # array of size 500
    start_time = time.time_ns()
    mergeSort(c)                                    # calling algorithm for array c
    end_time = time.time_ns()
    time_elapsed = (end_time - start_time)*10**-6
    resultsc = []
    resultsc.append(time_elapsed)
rc = round(statistics.mean(resultsc),3)              # setting rc to the mean of results rounded to 3 decimal places
for r in range(num_runs):
    np.random.seed(1)
    d = np.random.randint(200000, size=750)          # array of size 750
    start_time = time.time_ns()
    mergeSort(d)                                    # calling algorithm for array d
    end_time = time.time_ns()
    time_elapsed = (end_time - start_time)*10**-6
    resultsd = []
    resultsd.append(time_elapsed)
rd = round(statistics.mean(resultsd),3)              # setting rd to the mean of results rounded to 3 decimal places
for r in range(num_runs):
    np.random.seed(1)
    e = np.random.randint(200000, size=1000)         # array of size 1000
    start_time = time.time_ns()
    mergeSort(e)                                    # calling algorithm for array e
    end_time = time.time_ns()
    time_elapsed = (end_time - start_time)*10**-6
    resultse = []
    resultse.append(time_elapsed)
re = round(statistics.mean(resultse),3)              # setting re to the mean of results rounded to 3 decimal places
for r in range(num_runs):
    np.random.seed(1)
    f = np.random.randint(200000, size=2500)         # array of size 2500
    start_time = time.time_ns()
    mergeSort(f)                                    # calling algorithm for array f
    end_time = time.time_ns()
    resultsf = []
    time_elapsed = (end_time - start_time)*10**-6
    resultsf.append(time_elapsed)
rf = round(statistics.mean(resultsf),3)              # setting rf to the mean of results rounded to 3 decimal places
for r in range(num_runs):
    np.random.seed(1)
    g = np.random.randint(200000, size=5000)         # array of size 5000
    start_time = time.time_ns()
    mergeSort(g)                                    # calling algorithm for array g
    end_time = time.time_ns()
    time_elapsed = (end_time - start_time)*10**-6
    resultsg = []
    resultsg.append(time_elapsed)
rg = round(statistics.mean(resultsg),3)              # setting rg to the mean of results rounded to 3 decimal places
for r in range(num_runs):
    np.random.seed(1)
    h = np.random.randint(200000, size=7500)         # array of size 7500
    start_time = time.time_ns()
    mergeSort(h)                                    # calling algorithm for array h
    end_time = time.time_ns()
    time_elapsed = (end_time - start_time)*10**-6
    resultsh = []
    resultsh.append(time_elapsed)
rh = round(statistics.mean(resultsh),3)              # setting rg to the mean of results rounded to 3 decimal places
for r in range(num_runs):
    np.random.seed(1)
    i = np.random.randint(200000, size=10000)        # array of size 10000
    start_time = time.time_ns()
    mergeSort(i)                                    # calling algorithm for array i
    end_time = time.time_ns()
    time_elapsed = (end_time - start_time)*10**-6
    resultsi = []
    resultsi.append(time_elapsed)
ri = round(statistics.mean(resultsi),3)              # setting ri to the mean of results rounded to 3 decimal places
for r in range(num_runs):
    np.random.seed(1)
    j = np.random.randint(200000, size=15000)        # array of size 15000
    start_time = time.time_ns()
    mergeSort(j)                                    # calling algorithm for array j
    end_time = time.time_ns()
    time_elapsed = (end_time - start_time)*10**-6
    resultsj = []
    resultsj.append(time_elapsed)
rj = round(statistics.mean(resultsj),3)              # setting rj to the mean of results rounded to 3 decimal places
for r in range(num_runs):
    np.random.seed(1)
    k = np.random.randint(200000, size=20000)        # array of size 20000
    start_time = time.time_ns()
    mergeSort(k)                                    # calling algorithm for array k
    end_time = time.time_ns()
    time_elapsed = (end_time - start_time)*10**-6
    resultsk = []
    resultsk.append(time_elapsed)
rk = round(statistics.mean(resultsk),3)              # setting rk to the mean of results rounded to 3 decimal places
for r in range(num_runs):
    np.random.seed(1)
    l = np.random.randint(200000, size=30000)        # array of size 30000
    start_time = time.time_ns()
    mergeSort(l)                                    # calling algorithm for array l
    end_time = time.time_ns()
    time_elapsed = (end_time - start_time)*10**-6
    resultsl = []
    resultsl.append(time_elapsed)
rl = round(statistics.mean(resultsl),3)              # setting rl to the mean of results rounded to 3 decimal places
for r in range(num_runs):
    np.random.seed(1)
    m = np.random.randint(200000, size=50000)        # array of size 50000
    start_time = time.time_ns()
    mergeSort(m)                                    # calling algorithm for array m
    end_time = time.time_ns()
    time_elapsed = (end_time - start_time)*10**-6
    resultsm = []
    resultsm.append(time_elapsed)
rm = round(statistics.mean(resultsm),3)              # setting rl to the mean of results rounded to 3 decimal places

mergebench = [ra, rb, rc, rd, re, rf, rg, rh, ri, rj, rk, rl, rm] # setting mergebench to an array of the results of timings for sorting arrays a-m


# speeding.py
# This program determines if you are speeding, and what you owe.

def main():
    base    = 50
    mphOver = 5
    over90  = 200

    limit = float(input('Enter speed limit (mph): '))
    speed = float(input('Enter clocked speed (mph): '))

    if speed <= limit:
        print('That speed was legal.')
    else:
        speedOver = speed - limit
        fine = base + (mphOver * speedOver)
        if speed > 90:
            fine += 200
        print('That speed was illegal. Total fine: ${:.2f}'.format(fine))

if __name__ == '__main__':
    main()
    input("Press any key to continue . . . ")


import numpy as np
import random
import scipy, argparse, os, sys, csv, io, time
import scipy.io as sio
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, Dataset
from sklearn.metrics import accuracy_score,precision_score
import re
from torch.utils.tensorboard import SummaryWriter
import heapq

from src.data_reader import data_reader, get_vec_mat
from src.count_tools import macro_acc
import DeVise_test_GZSL

import warnings
import resource
rlimit = resource.getrlimit(resource.RLIMIT_NOFILE)
resource.setrlimit(resource.RLIMIT_NOFILE, (2048, rlimit[1]))
warnings.filterwarnings('ignore')  # "error", "ignore", "always", "default", "module" or "once"
# /home/gyx/ZSL/data/ImageNet/Res101_Features/StandardSplit
#############
### model ###
#############
input_dims=2048
output_dims=100
p=0.5 #drop out

#############
### train ###
#############
batch_size=64
lr=1e-3
wds=1e-5
epoch_num=100
GPU = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
random_seed=12345

class devise(nn.Module):
    def __init__(self):
        super(devise,self).__init__()
        self.model=nn.Sequential(nn.BatchNorm1d(input_dims),
                         nn.Dropout(p),
                         nn.Linear(in_features=input_dims, out_features=2048, bias=True),
                         nn.ReLU(),
                         nn.BatchNorm1d(2048),
                         nn.Dropout(p),
                         nn.Linear(in_features=2048, out_features=output_dims, bias=True))
    def forward(self, x):
        x=self.model(x)
        return x

def train(core_model,folder_dir_train,folder_dir_eval,loss_fn):
    output_folder = './save_model'
    os.makedirs(output_folder, exist_ok=True)
    tr_img = data_reader(folder_dir_train)
    tr = DataLoader(tr_img, batch_size=batch_size, shuffle=True, num_workers=8)
    optimizer_tag = optim.Adam(core_model.parameters(), lr=lr,weight_decay=wds)
    print('using {} as criterion'.format(loss_fn))
    te_img = data_reader(folder_dir_eval)  # test
    te = DataLoader(te_img, batch_size=50, num_workers=8)  # test
    #get vec mat
    vec_mat=get_vec_mat()
    pth = folder_dir_train
    dirs_seen = os.listdir(pth)
    pth=folder_dir_eval
    vec_m=[] #seen matrix of training set
    ids=[]  # id of 19 seen classes
    for f in dirs_seen:#for seen class
        id = int(re.sub("\D", "", f))
        ids.append(id)
        idx = id - 1
        vec_m.append(vec_mat[idx])

    vec_m=np.array(vec_m)
    print("size of vec mat seen:", vec_m.shape) # 19,500
    vec_m=vec_m.transpose()

    best_acc_te_1 ,best_acc_te_2,best_acc_te_5  = 0, 0, 0
    writer = SummaryWriter()
    for epoch in range(epoch_num):
        core_model.train()
        loss_total = 0
        real_label=[]
        pre_label_1=[]
        pre_label_2 = [] #hit 2
        pre_label_5 = [] #hit 5
        print('train begin:')
        for i, (x, y) in enumerate(tr, 1):
            vec_y, tag_y = y #vec  tag
            x = x.to(GPU,dtype=torch.float)
            vec_y = vec_y.to(GPU, dtype=torch.float)
            core_model.zero_grad()
            #print(type(x))
            y_pred = core_model(x)
            loss = loss_fn(y_pred, vec_y)
            loss.backward()
            optimizer_tag.step()
            ### pre whether correct? ###
            #
            real_label.extend(tag_y) # batch_size
            #
            sz=len(tag_y)
            y_pred_cpu=y_pred.cpu().detach().numpy()
            tt = np.dot(y_pred_cpu,vec_m) # judge by dot Multiplication
            for n in range(sz):
                e = heapq.nlargest(5, range(len(tt[n])), tt[n].take)
                ii=0
                while ii<5:
                    if(ids[e[ii]]==tag_y[n]):
                        break
                    ii+=1
                pre_label_1.append(ids[e[0]])
                pre_label_2.append(ids[e[0]])
                pre_label_5.append(ids[e[0]])
                if(ii<=1):
                    pre_label_2[-1] = tag_y[n]
                    pre_label_5[-1] = tag_y[n]
                elif(ii<=4):
                    pre_label_5[-1] = tag_y[n]

            loss_total+=loss.item()
        acc_1 = macro_acc(real_label, pre_label_1) # hit 1
        acc_2 = macro_acc(real_label, pre_label_2)  # hit 2
        acc_5 = macro_acc(real_label, pre_label_5)  # hit 5
        print('Epoch {:4d}/{:4d} total_loss:{:06.5f} macro_acc_1: {:04.2f}% macro_acc_2: {:04.2f}% macro_acc_5: {:04.2f}%'.format(epoch,epoch_num, loss_total,acc_1*100,acc_2*100,acc_5*100))
        #print('total time: {:>5.2f}s'.format(time.time() - epoch_st))
        writer.add_scalars('macro_acc',
                           {'hit_1': acc_1,
                            'hit_2': acc_2,
                            'hit_5': acc_5},
                           epoch)
        writer.add_scalar('loss', loss_total, epoch)

        #test
        #print('eval begin:')
        combine_dir = "./ZSL_DATA/test_2/combine"
        acc_test_1,acc_test_2,acc_test_5,loss_total_test=DeVise_test_GZSL.dtest(te,core_model,combine_dir,GPU,loss_fn)
        #print(
        #    'test macro_prec_1: {:04.2f}% macro_acc_2: {:04.2f}% macro_acc_5: {:04.2f}%'.format(acc_test_1 * 100,
        #                                                                                acc_test_2 * 100,
        #                                                                                acc_test_5 * 100))
        writer.add_scalars('test macro_acc',
                           {'hit_1': acc_test_1,
                            'hit_2': acc_test_2,
                            'hit_5': acc_test_5},
                           epoch)
        writer.add_scalar('test loss', loss_total_test, epoch)
        if epoch>5 and (acc_test_1>best_acc_te_1 or acc_test_2>best_acc_te_2 or acc_test_5>best_acc_te_5):
            ans="_"
            if acc_test_1>best_acc_te_1:
                best_acc_te_1 = acc_test_1
                ans+="hit1_"
                ans+=str(acc_test_1*100)[:5]
                ans+="%"
            if acc_test_2>best_acc_te_2:
                best_acc_te_2 = acc_test_2
                ans+="hit2_"
                ans+=str(acc_test_2*100)[:5]
                ans += "%"
            if acc_test_5>best_acc_te_5:
                best_acc_te_5 = acc_test_5
                ans+="hit5_"
                ans+=str(acc_test_5*100)[:5]
                ans += "%"
            # './save_model'
            torch.save(core_model.state_dict(),os.path.join(output_folder, str(round(time.time())) + 'epoch' + str(epoch) +ans+ '.pkl'))

        # each 10 epoch save
        elif(epoch>0 and epoch % 10 ==0):
            torch.save(core_model.state_dict(), os.path.join(output_folder, str(round(time.time()))+'epoch'+str(epoch) + '.pkl'))

    writer.close()
    print("best_acc_te_1:",best_acc_te_1)
    print("best_acc_te_2:", best_acc_te_2)
    print("best_acc_te_5:", best_acc_te_5)

if __name__ == "__main__":
    random.seed(random_seed)
    np.random.seed(random_seed)
    torch.manual_seed(random_seed)
    torch.cuda.manual_seed(random_seed)

    core_model=devise().to(GPU)
    folder_dir_train="./ZSL_DATA/train_seen_2"
    folder_dir_eval="./ZSL_DATA/test_2/unseen"
    loss_fn=nn.MSELoss()
    train(core_model,folder_dir_train,folder_dir_eval,loss_fn)


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

import itertools
import ray
import sys
import time
from absl import app
from absl import flags

FLAGS = flags.FLAGS
flags.DEFINE_integer('num_actors', 1, 'Number of actors.')
flags.DEFINE_integer('num_requests', 10000, 'Number of requests.')

@ray.remote
class RPCActor(object):
    def __init__(self):
        pass

    def serve(self, req):
        ret = do_work.remote(req)
        return [ret]

@ray.remote
class NoOpActor(object):
    def __init__(self):
        pass

    def serve(self, req):
        return req

@ray.remote
def do_work(req):
    return req

def run_rpc_actors():
    rpc_actors = []
    for i in range(0, FLAGS.num_actors):
        rpc_actors.append(RPCActor.remote())
    rets = []
    start_time = time.time()
    rets = [rpc_actors[i % FLAGS.num_actors].serve.remote(i)
            for i in range(0, FLAGS.num_requests, 1)]
    ray.get(list(itertools.chain(*ray.get(rets))))
    duration = time.time() - start_time
    print('Duration RPC actors {}'.format(duration))

def run_no_op_actors():
    no_op_actors = []
    for i in range(0, FLAGS.num_actors):
        no_op_actors.append(NoOpActor.remote())
    start_time = time.time()
    rets = [no_op_actors[i % FLAGS.num_actors].serve.remote(i)
            for i in range(0, FLAGS.num_requests, 1)]
    ray.get(rets)
    duration = time.time() - start_time
    print('Duration no op actors {}'.format(duration))

def run_tasks():
    start_time = time.time()
    rets = [do_work.remote(i) for i in range(0, FLAGS.num_requests, 1)]
    ray.get(rets)
    duration = time.time() - start_time
    print('Duration tasks {}'.format(duration))

def main(argv):
    ray.init(redirect_output=True)
    run_rpc_actors()
    run_no_op_actors()
    run_tasks()

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


import subprocess

class GetVersion:
    GIT_COMMANDS = {
        "get_tag_list": "git tag -l",
    }

    @staticmethod
    def run_shell_command(command, shell=True, stdout=subprocess.PIPE):
        result = subprocess.run(command, shell=shell, stdout=stdout)
        return result

    def get_versions_list(self):
        git_tags_list = self.run_shell_command(__class__.GIT_COMMANDS["get_tag_list"])
        v_list = []
        for v in git_tags_list.stdout.decode('utf-8').splitlines():
            v_list.append(v)
        return v_list

    def get_last_version(self, v_list=None):
        if v_list is None:
            v_list = self.get_versions_list()
        return v_list[-1] if v_list else "No versions yet!"

    def write_or_create_version_file(self, path_to_file):
        last_v = self.get_last_version()
        with open(path_to_file, 'w') as f:
            f.write(last_v)

if __name__ == '__main__':
    getv = GetVersion()
    # return all version list
    list_versions = getv.get_versions_list()
    print("Version list %s" % list_versions)


from mud.mapper import Mapper
from mud.entities.object import Object
from mud.entities.character import Character
from mud.entities.actor import Actor
from settings.directions import DIRECTIONS

def look_command(actor, game, *args, **kwargs):
    room = actor.get_room()

    title_line = "{}{{x {} ({}) ({}) [Room {}] [ID {}]".format(
        "{B" + room.name + "{x",
        "{R[{WNOLOOT{R]{x",
        room.area_id,
        "{Wbuilding{x",
        room.id,
        room.uid,
    )

    output = []
    output.append(title_line)

    for index, line in enumerate(room.description):

        if index == 0:
            line = "  " + line

        output.append(line)

    output.append('')

    exits = []
    doors = []
    secrets = []

    def format_exits(exits):
        if not exits:
            return "none"
        return " ".join([exit["colored_name"] for exit in exits])

    for exit_id, exit in DIRECTIONS.items():
        room_exit = room.get_exit(exit_id)

        if not room_exit:
            continue

        if room_exit.has_flag("door") and room_exit.has_flag("closed"):
            if room_exit.has_flag("secret"):
                secrets.append(exit)
            else:
                doors.append(exit)
        else:
            exits.append(exit)

    exit_options = [
        ("Exits", exits),
        ("Doors", doors),
    ]
    # FIXME Add immortality check.
    if True:
        exit_options.append(("Secret", secrets))

    exit_line_parts = []
    for label, options in exit_options:
        exit_line_parts.append("{{x[{{G{}{{g:{{x {}{{x]".format(
            label,
            format_exits(options)
        ))

    output.append('   '.join(exit_line_parts))

    for cls in [Object, Actor, Character]:
        for other in cls.query_by_room_uid(actor.room_uid, game=game):
            if not actor.can_see(other) or other == actor:
                continue

            line = str(other.format_room_flags_to(actor))

            # Only add space if there's a set of flags.
            if line:
                line += ' '

            if type(other) is Object:
                line = '     ' + line

            line += str(other.format_room_name_to(actor))

            output.append(line)

    # Generate and prepend minimap.
    prefix_with_minimap = True

    if prefix_with_minimap:
        map_width = 15
        map_height = 7
        map_pad_width = 2

        map = Mapper.generate_map(actor=actor, border=True, width=map_width, height=map_height)
        map_lines = map.get_lines()

        minimapped_output = []

        for y in range(0, max(len(map_lines), len(output))):
            prefix_line = ' ' * (map_width + map_pad_width)
            if y < len(map_lines):
                prefix_line = map_lines[y] + (' ' * map_pad_width)

            output_line = output[y] if y < len(output) else ''

            minimapped_output.append(prefix_line + output_line)

        output = minimapped_output

    for line in output:
        actor.echo(line)


# 环形链表2
# 方法一：快慢指针
class Solution(object):
    def detectCycle(self, head):
        fast, slow = head, head
        while True:
            if not (fast and fast.next): return
            fast, slow = fast.next.next, slow.next
            if fast == slow: break
        fast = head
        while fast != slow:
            fast, slow = fast.next, slow.next
        return fast

def detectCycle(head):
    try:
        fast = head.next
        slow = head
        while fast is not slow:
            fast = fast.next.next
            slow = slow.next
    except:
        # if there is an exception, we reach the end and there is no cycle
        return None

    # since fast starts at head.next, we need to move slow one step forward
    slow = slow.next
    while head is not slow:
        head = head.next
        slow = slow.next

    return head
# 方法二：哈希表
# 首先，我们分配一个 Set 去保存所有的列表节点。我们逐一遍历列表，检查当前节点是否出现过，如果节点已经出现过，
# 那么一定形成了环且它是环的入口。否则如果有其他点是环的入口，我们应该先访问到其他节点而不是这个节点。其他情况，
# 没有成环则直接返回 null 。算法会在遍历有限个节点后终止，这是因为输入列表会被分成两类：成环的和不成环的。
# 一个不成欢的列表在遍历完所有节点后会到达 null - 即链表的最后一个元素后停止。
# 一个成环列表可以想象成是一个不成环列表将最后一个 null 元素换成环的入口。如果 while 循环终止，
# 我们返回 null 因为我们已经将所有的节点遍历了一遍且没有遇到重复的节点，这种情况下，列表是不成环的。
# 对于循环列表， while 循环永远不会停止，但在某个节点上， if 条件会被满足并导致函数的退出。

def detectCycle(head):
    visited = set()

    node = head
    while node is not None:
        if node in visited:
            return node
        else:
            visited.add(node)
            node = node.next.next

        return None


#!/usr/bin/env python

import numpy as np

import resampy

from nose.tools import raises

def make_tone(freq, sr, duration):
    return np.sin(2 * np.pi * freq / sr * np.arange(sr * duration))

def make_sweep(freq, sr, duration):

    return np.sin(np.cumsum(2 * np.pi * np.logspace(np.log2(2.0 / sr),
                                                    np.log2(float(freq) / sr),
                                                    num=duration*sr, base=2.0)))

def test_quality_sine():

    def __test(sr_orig, sr_new, fil, rms, x, y):

        y_pred = resampy.resample(x, sr_orig, sr_new, filter=fil)

        idx = slice(sr_new // 2, - sr_new//2)

        err = np.mean(np.abs(y[idx] - y_pred[idx]))
        assert err <= rms, '{:g} > {:g}'.format(err, rms)

    FREQ = 512.0
    DURATION = 2.0

    for (sr_orig, sr_new) in [ (44100, 22050), (22050, 44100) ]:
        x = make_tone(FREQ, sr_orig, DURATION)
        y = make_tone(FREQ, sr_new, DURATION)

        for (fil, rms) in [('sinc_window', 1e-6),
                           ('kaiser_fast', 1e-4),
                           ('kaiser_best', 1e-7)]:
            yield __test, sr_orig, sr_new, fil, rms, x, y

def test_quality_sweep():

    def __test(sr_orig, sr_new, fil, rms, x, y):

        y_pred = resampy.resample(x, sr_orig, sr_new, filter=fil)

        idx = slice(sr_new // 2, - sr_new//2)

        err = np.mean(np.abs(y[idx] - y_pred[idx]))
        assert err <= rms, '{:g} > {:g}'.format(err, rms)

    FREQ = 8192
    DURATION = 5.0

    for (sr_orig, sr_new) in [ (44100, 22050), (22050, 44100) ]:
        x = make_sweep(FREQ, sr_orig, DURATION)
        y = make_sweep(FREQ, sr_new, DURATION)

        for (fil, rms) in [('sinc_window', 1e-1),
                           ('kaiser_fast', 1e-1),
                           ('kaiser_best', 1e-1)]:
            yield __test, sr_orig, sr_new, fil, rms, x, y


import os
import sys
import json
import torch
import logging

import traceback
from tqdm import tqdm
from . import loader_utils
from .loader_utils import load_dataset, flat_rank_pos, limit_scope_length, stemming
from ..constant import BOS_WORD, EOS_WORD
from torch.utils.data import Dataset

logger = logging.getLogger()

# -------------------------------------------------------------------------------------------
# preprocess label for bert2gram training
# -------------------------------------------------------------------------------------------
def limit_phrase_length(positions, max_phrase_words):
    filter_positions = [pos for pos in positions if (pos[1]-pos[0]+1) <= max_phrase_words]
    return filter_positions

def get_ngram_label(valid_length, start_end_pos, max_phrase_words):
    # flatten, rank, filter overlap for answer positions

    sorted_positions = flat_rank_pos(start_end_pos)
    filter_positions = limit_phrase_length(sorted_positions, max_phrase_words)

    if len(filter_positions) != len(sorted_positions):
        overlen_flag = True
    else:
        overlen_flag = False

    s_label, e_label = [], []
    for s, e in filter_positions:
        if e < valid_length:
            s_label.append(s)
            e_label.append(e)
        else:
            break

    assert len(s_label) == len(e_label)
    return {'s_label':s_label, 'e_label': e_label, 'overlen_flag':overlen_flag}

def prepare_data_for_bert2gram(examples, max_token, max_phrase_words, mode, pretrain_model):
    logger.info('start preparing data for %s2Gram ...'%pretrain_model)
    overlen_num = 0
    new_examples = []
    for idx, ex in enumerate(tqdm(examples)):

        if len(ex['tokens']) < max_token:
            max_word = max_token
        else:
            max_word = ex['tok_to_orig_index'][max_token-1] + 1

        new_ex = {}
        new_ex['url'] = ex['url']
        new_ex['tokens'] = ex['tokens'][:max_token]
        new_ex['valid_mask'] = ex['valid_mask'][:max_token]
        new_ex['doc_words'] = ex['doc_words'][:max_word]
        assert len(new_ex['tokens']) == len(new_ex['valid_mask'])
        assert sum(new_ex['valid_mask']) == len(new_ex['doc_words'])

        if mode == 'train':
            parameter = {'valid_length': len(new_ex['doc_words']),
                         'start_end_pos': ex['start_end_pos'],
                         'max_phrase_words':max_phrase_words}
            # ------------------------------------------------
            label_dict = get_ngram_label(**parameter)
            if label_dict['overlen_flag']:
                overlen_num += 1

            if not label_dict['s_label']:
                continue

            new_ex['s_label'] = label_dict['s_label']
            new_ex['e_label'] = label_dict['e_label']

        new_examples.append(new_ex)
    logger.info('Delete Overlen Keyphrase (length > 5): %d (overlap / total = %.2f'
                %(overlen_num, float(overlen_num / len(examples) * 100)) + '%)')
    return new_examples

def reload_cached_dataset(cached_dataset_dir, dataset_class, name, pretrain_model):
    logger.info("start Reloading %s2gram %s %s cached dataset ..." %(pretrain_model, dataset_class, name))
    filename = os.path.join(cached_dataset_dir, "%s2gram.cached.%s.%s.json" % (pretrain_model, dataset_class, name))

    examples = []
    with open(filename, "r", encoding="utf-8") as f:
        for l in tqdm(f):
            examples.append(json.loads(l))
    f.close()
    logger.info("success loaded %s %s data : %d " %(dataset_class, name, len(examples)))
    return examples

def save_cached_dataset(cached_examples, dataset_name, mode, pretrain_model):
    logger.info("start saving %s2gram %s %s cached dataset ..." %(pretrain_model, dataset_name, mode))
    cached_dataset_dir = "./Cached_Datasets"

    if not os.path.exists(cached_dataset_dir):
        os.mkdir(cached_dataset_dir)

    filename = os.path.join(cached_dataset_dir, "%s2gram.cached.%s.%s.json" % (pretrain_model, dataset_name, mode))
    with open(filename, 'w', encoding='utf-8') as f_pred:
        for idx, ex in enumerate(tqdm(cached_examples)):
            f_pred.write("{}\n".format(json.dumps(ex)))
        f_pred.close()
    logger.info("successfully saved %s2gram %s %s cached dataset to %s" %(pretrain_model, dataset_name, mode, filename))

# -------------------------------------------------------------------------------------------
# build dataset for bert2gram
# -------------------------------------------------------------------------------------------
class build_bert2gram_dataset(Dataset):
    ''' build datasets for train & eval '''
    def __init__(self, args, examples, dataset_name, tokenizer, max_token, max_phrase_words,
                 mode, preprocess_folder, cached_dataset_dir=None, local_rank=-1):

        # --------------------------------------------------------------------------------------------
        self.sep_token_extra = False
        self.pretrain_model = "bert"
        if "roberta" in args.model_class:
            self.sep_token_extra = True
            self.pretrain_model = "roberta"

        params = {'examples': examples, 'max_token': max_token, 'max_phrase_words':max_phrase_words,
                  'mode':mode, "pretrain_model":self.pretrain_model}

        cached_examples = prepare_data_for_bert2gram(**params)

        # del cache
        examples.clear()

        # Rank cost too much time to preprocess `train_dataset`, so we want to save it
        if local_rank in [-1, 0] and mode == "train":
            save_cached_dataset(cached_examples, dataset_name, mode, self.pretrain_model)

        self.examples = cached_examples
        self.mode = mode
        self.tokenizer = tokenizer
        self.max_phrase_words = max_phrase_words

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

    def __getitem__(self, index):
        return convert_examples_to_features(index, self.examples[index], self.tokenizer, self.max_phrase_words, self.mode, self.sep_token_extra)

def convert_examples_to_features(index, ex, tokenizer, max_phrase_words, mode, sep_token_extra=False):
    ''' convert each batch data to tensor ; add [CLS] [SEP] tokens ;'''

    src_tokens = [BOS_WORD] + ex['tokens'] + [EOS_WORD]
    valid_ids = [0] + ex['valid_mask'] + [0]

    if sep_token_extra:
        src_tokens = src_tokens + [EOS_WORD]
        valid_ids = valid_ids + [0]

    src_tensor = torch.LongTensor(tokenizer.convert_tokens_to_ids(src_tokens))
    valid_mask = torch.LongTensor(valid_ids)
    orig_doc_len = sum(valid_ids)

    if mode == 'train':
        s_label = ex['s_label']
        e_label = ex['e_label']
        return index, src_tensor, valid_mask, s_label, e_label, orig_doc_len, max_phrase_words

    else:
        return index, src_tensor, valid_mask, orig_doc_len, max_phrase_words

def batchify_Bert2Gram_features_for_train(batch):
    ''' train dataloader & eval dataloader .'''

    ids = [ex[0] for ex in batch]
    docs = [ex[1] for ex in batch]
    valid_mask = [ex[2] for ex in batch]
    s_label_list = [ex[3] for ex in batch]
    e_label_list = [ex[4] for ex in batch]
    doc_word_lens = [ex[5] for ex in batch]
    max_phrase_words = [ex[6] for ex in batch][0]

    bert_output_dim = 768
    max_word_len = max([word_len for word_len in doc_word_lens]) # word-level

    # ---------------------------------------------------------------
    # [1] [2] src tokens tensor
    doc_max_length = max([d.size(0) for d in docs])
    input_ids = torch.LongTensor(len(docs), doc_max_length).zero_()
    input_mask = torch.LongTensor(len(docs), doc_max_length).zero_()

    for i, d in enumerate(docs):
        input_ids[i, :d.size(0)].copy_(d)
        input_mask[i, :d.size(0)].fill_(1)

    # ---------------------------------------------------------------
    # [3] valid mask tensor
    valid_max_length = max([v.size(0) for v in valid_mask])
    valid_ids = torch.LongTensor(len(valid_mask), valid_max_length).zero_()
    for i, v in enumerate(valid_mask):
        valid_ids[i, :v.size(0)].copy_(v)

    # ---------------------------------------------------------------
    # [4] active mask : for n-gram
    max_ngram_length = sum([max_word_len-n for n in range(max_phrase_words)])
    active_mask = torch.LongTensor(len(docs), max_ngram_length).zero_()

    for batch_i, word_len in enumerate(doc_word_lens):
        pad_len = max_word_len - word_len

        batch_mask = []
        for n in range(max_phrase_words):
            ngram_len = word_len - n
            if ngram_len > 0:
                gram_list = [1 for _ in range(ngram_len)] + [0 for _ in range(pad_len)]
            else:
                gram_list = [0 for _ in range(max_word_len-n)]
            batch_mask.extend(gram_list)
        active_mask[batch_i].copy_(torch.LongTensor(batch_mask))

    # -------------------------------------------------------------------
    # [5] label : for n-gram
    # 1. empty label list
    label_list = []
    for _ in range(len(docs)):
        batch_label = []
        for n in range(max_phrase_words):
            batch_label.append(torch.LongTensor([0 for _ in range(max_word_len-n)]))
        label_list.append(batch_label)

    # 2. valid label list
    for batch_i in range(len(docs)):
        for s, e in zip(s_label_list[batch_i], e_label_list[batch_i]):
            gram = e-s
            label_list[batch_i][gram][s] = 1

    # 3. label tensor
    ngram_label = torch.LongTensor(len(docs), max_ngram_length).zero_()
    for batch_i, label in enumerate(label_list):
        ngram_label[batch_i].copy_(torch.cat(label))

    # 4. valid output
    valid_output = torch.zeros(len(docs), max_word_len, bert_output_dim)
    return input_ids, input_mask, valid_ids, active_mask, valid_output, ngram_label, ids

def batchify_Bert2Gram_features_for_test(batch):
    ''' test dataloader for Dev & Public_Valid.'''

    ids = [ex[0] for ex in batch]
    docs = [ex[1] for ex in batch]
    valid_mask = [ex[2] for ex in batch]
    doc_word_lens = [ex[3] for ex in batch]
    max_phrase_words = [ex[4] for ex in batch][0]

    bert_output_dim = 768
    max_word_len = max([word_len for word_len in doc_word_lens]) # word-level

    # ---------------------------------------------------------------
    # [1] [2] src tokens tensor
    doc_max_length = max([d.size(0) for d in docs])
    input_ids = torch.LongTensor(len(docs), doc_max_length).zero_()
    input_mask = torch.LongTensor(len(docs), doc_max_length).zero_()

    for i, d in enumerate(docs):
        input_ids[i, :d.size(0)].copy_(d)
        input_mask[i, :d.size(0)].fill_(1)

    # ---------------------------------------------------------------
    # [3] valid mask tensor
    valid_max_length = max([v.size(0) for v in valid_mask])
    valid_ids = torch.LongTensor(len(valid_mask), valid_max_length).zero_()
    for i, v in enumerate(valid_mask):
        valid_ids[i, :v.size(0)].copy_(v)

    # ---------------------------------------------------------------
    # [4] active mask : for n-gram
    max_ngram_length = sum([max_word_len-n for n in range(max_phrase_words)])
    active_mask = torch.LongTensor(len(docs), max_ngram_length).zero_()

    for batch_i, word_len in enumerate(doc_word_lens):
        pad_len = max_word_len - word_len

        batch_mask = []
        for n in range(max_phrase_words):
            ngram_len = word_len - n
            if ngram_len > 0:
                gram_list = [1 for _ in range(ngram_len)] + [0 for _ in range(pad_len)]
            else:
                gram_list = [0 for _ in range(max_word_len-n)]
            batch_mask.extend(gram_list)
        active_mask[batch_i].copy_(torch.LongTensor(batch_mask))

    # ---------------------------------------------------------------
    # [5] valid output
    valid_output = torch.zeros(len(docs), max_word_len, bert_output_dim)
    return input_ids, input_mask, valid_ids, active_mask, valid_output, doc_word_lens, ids


# William Leighton Dawson
# Center for Cyber Security
# New York University - Abu Dhabi
# Implementation of the NIST Randomness Test Suite
# 2. Frequency Test within a Block

from helpers import proportion, split_list, LengthError
from cephes import igam
from sys import stderr

def igamc(a, x):
  return 1 - igam(a, x)

def BlockFrequency(e, M=10):
    M = 10
    ce = list(e)
    ce = ce[:len(ce) - (len(ce) % M)]
    n = len(ce)
    N = n / M
    run_test = True
    outstr = "Frequency Test within a Block:\n"
    outstr += "n = %d\n" % n
    outstr += "M = %d\n" % M
    outstr += "N = %d\n" % N
    if n < 100:
        run_test = False
    else:
        Pi_l = split_list(ce, M, proportion)
        Chi2_obs = 4 * M * sum([pow((i - 0.5), 2) for i in Pi_l])
        p = igamc(N / 2.0, Chi2_obs / 2.0)
        outstr = "Frequency Test within a Block:\n"
        outstr += "n = %d\n" % n
        outstr += "M = %d\n" % M
        outstr += "N = %d\n" % N
        outstr += "Chi^2_obs = %.6f\n" % Chi2_obs
        outstr += "P-value = %.6f\n" % p
    if not run_test:
        outstr += 'Test not run: pre-test condition not met: '
        outstr += 'n >= 100\n'
    open("./test_results/BlockFrequency.txt", "w").write(outstr)
    if run_test:
        if p < 0.01:
            return False
    return True


# Front matter
##############
import os
from os import fdopen, remove
from tempfile import mkstemp
from shutil import move
import glob
import re
import time
import pandas as pd
import numpy as np
from scipy import constants
from scipy.optimize import curve_fit, fsolve
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator
from matplotlib import gridspec
from scipy.interpolate import spline
import math
import seaborn as sns

matplotlib.rc('xtick', labelsize=16)
matplotlib.rc('ytick', labelsize=16)

rc = {'lines.linewidth': 1,
      'axes.labelsize': 20,
      'axes.titlesize': 20,
      'legend.fontsize': 26,
      'xtick.direction': u'in',
      'ytick.direction': u'in'}
sns.set_style('ticks', rc=rc)

start_time = time.time()

# Input file paths and info
###########################

pdf_filename = 'pdf_results_Debye.csv'

# Functions
###########

# Gaussian function
def Gaussian(x,amplitude,mean,sigma):
	return amplitude*np.exp(-(x-mean)**2/(2*sigma**2))

# Lorentzian function
def Lorentzian(x,amplitude,mean,sigma):
    return amplitude*(sigma/2)**2/((x-mean)**2+(sigma/2)**2)

# Gaussian function with an amplitude of 1 (not normalized)
def Gaussian1(x,mean,sigma):
	return np.exp(-(x-mean)**2/(2*sigma**2))

# Lorentzian function with an amplitude of 1 (not normalized)
def Lorentzian1(x,mean,sigma):
    return (sigma/2)**2/((x-mean)**2+(sigma/2)**2)

# Pseudo-Voigt function
def pseudoVoigt(x,amplitude,mean,sigma,shape):
    return amplitude*( shape*Lorentzian1(x,mean,sigma) +
         (1-shape)*Gaussian1(x,mean,sigma) )

# Function for an asymmetric Gaussian peak
def asymGaussian(x,amplitude,mean,sigmaLeft,sigmaRight):
	# Piecewise function
	y = np.zeros(len(x)) # Framework for adding pieces of function
	y += Gaussian(x,amplitude,mean,sigmaLeft) * (x <= mean) # Left side
	y += Gaussian(x,amplitude,mean,sigmaRight) * (x > mean) # Right side
	return y

# Function for an asymmetric Lorentzian peak
def asymLorentzian(x,amplitude,mean,sigmaLeft,sigmaRight):
	# Piecewise function
	y = np.zeros(len(x)) # Framework for adding pieces of function
	y += Lorentzian(x,amplitude,mean,sigmaLeft) * (x <= mean) # Left side
	y += Lorentzian(x,amplitude,mean,sigmaRight) * (x > mean) # Right side
	return y

# Function for an asymmetric pseudo-Voigt peak
def asymPseudoVoigt(x,amplitude,mean,sigmaLeft,shapeLeft,sigmaRight,shapeRight):
	# Piecewise function
	y = np.zeros(len(x)) # Framework for adding pieces of function
	y += pseudoVoigt(x,amplitude,mean,sigmaLeft,shapeLeft) * (x <= mean) # Left side
	y += pseudoVoigt(x,amplitude,mean,sigmaRight,shapeRight) * (x > mean) # Right side
	return y

# Wolfgang's function
def WSfunction(x,amplitude,mean,sigmaLeft,shapeLeft,sigmaRight,shapeRight):
	# Piecewise function
	y = np.zeros(len(x)) # Framework for adding pieces of function
	y += amplitude*2**(-np.abs((x-mean)/sigmaLeft)**shapeLeft) * (x <= mean) # Left side
	y += amplitude*2**(-np.abs((x-mean)/sigmaRight)**shapeRight) * (x > mean) # Right side
	return y

# Plot Debye pdf
def plot_vD_pdf(binned_df,Loren_opt,aLoren_opt,folder):

	fig, (ax0) = plt.subplots(nrows = 1, ncols=1, figsize=(16,4))

	# Plot vD pdf data
	ax0.plot(binned_df['Bin Midpoint'],binned_df['Prob'],'.')
	# ax0.plot(binned_df['Bin Midpoint'],Gaussian(binned_df['Bin Midpoint'],*Gauss_opt),
	# 	color='red')
	ax0.plot(binned_df['Bin Midpoint'],Lorentzian(binned_df['Bin Midpoint'],*Loren_opt),
		color='orange')
 	# ax0.plot(binned_df['Bin Midpoint'],pseudoVoigt(binned_df['Bin Midpoint'],*pVoigt_opt),
	# 	color='green')
	# ax0.plot(binned_df['Bin Midpoint'],asymGaussian(binned_df['Bin Midpoint'],*aGauss_opt),
	# 	color='blue')
	ax0.plot(binned_df['Bin Midpoint'],asymLorentzian(binned_df['Bin Midpoint'],*aLoren_opt),
		color='purple')
	# ax0.plot(binned_df['Bin Midpoint'],asymPseudoVoigt(binned_df['Bin Midpoint'],*apVoigt_opt),
	# 	color='gray')
	# ax0.plot(binned_df['Bin Midpoint'],WSfunction(binned_df['Bin Midpoint'],*WS_opt),
	# 	color='red')

	plt.tight_layout()
	fig.savefig('test_plots/vD_pdf_'+folder+'.pdf', format='pdf')
	plt.close()

# Fit asymmetric peak to pdf data
#################################

# Find the filepath of all .res NRIXS files in phox directories
pdf_path_list = [filepath for filepath in glob.glob('*/'+pdf_filename)]

# Prep lists, dictionaries
folder_list = []
vD_pdf_dict = dict()

# Collect folders, indices, paths, and input values
for pdf_path in pdf_path_list:
	folder = re.findall('([A-Za-z0-9_]+)/'+pdf_filename,pdf_path)[0]

	binned_df = pd.read_csv(pdf_path)

	# Parameter bounds
	amp_min = 0.1
	amp_max = 30
	mean_min = 100
	mean_max = 20000
	sigma_min = 1
	sigma_max = 1000
	shape_min = 0
	shape_max = 1

	# Initial parameter guess
	amp_guess = 1
	mean_guess = np.average(binned_df['Bin Midpoint'],weights = binned_df['Prob'])
	sigma_guess = 50
	shape_guess = 0.5

	print('Fitting peak to '+folder)

	# Fit Gaussian peak to pdf
	Gauss_opt,cov = curve_fit(Gaussian,binned_df['Bin Midpoint'],binned_df['Prob'],
		p0=[amp_guess,mean_guess,sigma_guess],
		bounds = ([amp_min,mean_min,sigma_min],
			[amp_max,mean_max,sigma_max]))
	print(Gauss_opt)

	# Fit Lorentzian peak to pdf
	Loren_opt,cov = curve_fit(Lorentzian,binned_df['Bin Midpoint'],binned_df['Prob'],
		p0=[amp_guess,mean_guess,sigma_guess],
		bounds = ([amp_min,mean_min,sigma_min],
			[amp_max,mean_max,sigma_max]))
	print(Loren_opt)

	# # Fit pseudo-Voigt peak to pdf
	# pVoigt_opt,cov = curve_fit(pseudoVoigt,binned_df['Bin Midpoint'],binned_df['Prob'],
	# 	p0=[amp_guess,mean_guess,sigma_guess,shape_guess],
	# 	bounds = ([amp_min,mean_min,sigma_min,shape_min],
	# 		[amp_max,mean_max,sigma_max,shape_max]))

	# # Fit asymmetric Gaussian peak to pdf
	# aGauss_opt,cov = curve_fit(asymGaussian,binned_df['Bin Midpoint'],binned_df['Prob'],
	# 	p0=[amp_guess,mean_guess,sigma_guess,sigma_guess],
	# 	bounds = ([amp_min,mean_min,sigma_min,sigma_min],
	# 		[amp_max,mean_max,sigma_max,sigma_max]))

	# Fit asymmetric Lorentzian peak to pdf
	aLoren_opt,cov = curve_fit(asymLorentzian,binned_df['Bin Midpoint'],binned_df['Prob'],
		p0=[amp_guess,mean_guess,sigma_guess,sigma_guess],
		bounds = ([amp_min,mean_min,sigma_min,sigma_min],
			[amp_max,mean_max,sigma_max,sigma_max]))
	print(aLoren_opt)

	# # Fit asymmetric pseudo-Voigt peak to pdf
	# apVoigt_opt,cov = curve_fit(asymPseudoVoigt,binned_df['Bin Midpoint'],binned_df['Prob'],
	# 	p0=[amp_guess,mean_guess,sigma_guess,shape_guess,sigma_guess,shape_guess],
	# 	bounds = ([amp_min,mean_min,sigma_min,shape_min,sigma_min,shape_min],
	# 		[amp_max,mean_max,sigma_max,shape_max,sigma_max,shape_max]))

	# # Fit Wolfgang's function to pdf
	# WS_opt,cov = curve_fit(WSfunction,binned_df['Bin Midpoint'],binned_df['Prob'],
	# 	p0=[amp_guess,mean_guess,sigma_guess,shape_guess,sigma_guess,shape_guess],
	# 	bounds = ([amp_min,mean_min,sigma_min,shape_min,sigma_min,shape_min],
	# 		[amp_max,mean_max,sigma_max,shape_max,sigma_max,shape_max]))

	# Create plot of pdf
	plot_vD_pdf(binned_df,Loren_opt,aLoren_opt,folder)


from pytube import Playlist
from pytube import YouTube

path = '/home/stilgar/Escritorio/Clot/Free/YotubePy/musica'

try:
    playlist = Playlist('https://www.youtube.com/playlist?list=PL2Szhg9E4zGKv96T9wuIUl6uj9ozte9Yc')

    for i in playlist:
        yt = YouTube(i)
        yt.streams.get_audio_only().download(output_path=path, filename=yt.title)
        print('YouTube video audio downloaded successfully')

except Exception as e:
    print(e)


def apaga_repetidos (s):
    s = s.lower()
    print(s)
    li = []
    for i in range (len(s)):
        li.append(s[i])
    for a in range(len(li)):
        b = a + 1
        while b < len(li):
            if li[a] == li[b] and li[a] != '*':
                li[b] == '*'
            b += 1
    n = ''.join(li)
    return n


from sklearn.metrics import silhouette_samples, silhouette_score
from scipy.cluster.hierarchy import linkage, dendrogram
from scipy.spatial.distance import pdist, squareform

import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np

import csv
import os

def get_cluster_colors(cluster_labels):
  colors = list()
  color_map = {
    0:'red',
    1:'orange',
    2:'yellow',
    3:'green',
    4:'blue',
    5:'indigo',
    6:'purple',
    7:'black'
  }
  for i in cluster_labels:
    colors.append(color_map[i])

  return colors

def get_cluster_labels(header, clusters):
  labels = list()
  for congressman_id in header:
    for i in range(len(clusters)):
      if congressman_id in clusters[i]:
        labels.append(i)
        break

  return np.array(labels)

def distance_matrix(method):
  distance = list()
  header = None

  with open('distance-{}.csv'.format(method), 'r') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
      if not header:
        header = row[1:]
      else:
        distance.append(row[1:])

  return distance, header

def _silhouette(clusters, method, k, series_type):
  fig, ax = plt.subplots()

  matrix, header = distance_matrix(method)
  cluster_labels = get_cluster_labels(header, clusters)

  # The silhouette_score gives the average value for all the samples.
  # This gives a perspective into the density and separation of the formed
  # clusters
  silhouette_avg = silhouette_score(matrix, cluster_labels, metric='precomputed')

  # Compute the silhouette scores for each sample
  sample_silhouette_values = silhouette_samples(matrix, cluster_labels, metric='precomputed')

  y_lower = 10
  n_clusters = len(clusters)
  for i in range(n_clusters):
    # The 1st subplot is the silhouette plot
    # The silhouette coefficient can range from -1, 1 but in this example all
    # lie within [-0.1, 1]
    ax.set_xlim([-0.1, 1])
    # The (n_clusters+1)*10 is for inserting blank space between silhouette
    # plots of individual clusters, to demarcate them clearly.
    ax.set_ylim([0, len(matrix) + (n_clusters + 1) * 10])

    # Aggregate the silhouette scores for samples belonging to
    # cluster i, and sort them
    ith_cluster_silhouette_values =  \
      sample_silhouette_values[cluster_labels == i]

    ith_cluster_silhouette_values.sort()

    size_cluster_i = ith_cluster_silhouette_values.shape[0]
    y_upper = y_lower + size_cluster_i

    color = cm.spectral(float(i) / n_clusters)
    ax.fill_betweenx(np.arange(y_lower, y_upper),
                      0, ith_cluster_silhouette_values,
                      facecolor=color, edgecolor=color, alpha=0.7)

    # Label the silhouette plots with their cluster numbers at the middle
    ax.text(-0.05, y_lower + 0.5 * size_cluster_i, str(i))

    # Compute the new y_lower for next plot
    y_lower = y_upper + 10  # 10 for the 0 samples

  ax.set_title("The silhouette plot for the various clusters.")
  ax.set_xlabel("The silhouette coefficient values")
  ax.set_ylabel("Cluster label")

  # The vertical line for average silhouette score of all the values
  ax.axvline(x=silhouette_avg, color="red", linestyle="--")

  ax.set_yticks([])  # Clear the yaxis labels / ticks
  ax.set_xticks([-0.1, 0, 0.2, 0.4, 0.6, 0.8, 1])

  directory = '../img/{}/silhouette/'.format(series_type)
  if not os.path.exists(directory):
    os.makedirs(directory)

  fig.savefig('../img/{}/silhouette/{}_k{}.png'.format(series_type, method, k), bbox_inches='tight')
  plt.close(fig)

def _dendrogram(clusters, method, k, series_type):
  fig, ax = plt.subplots()

  matrix, header = distance_matrix()
  cluster_labels = get_cluster_labels(header, clusters)

  matrix = np.array(matrix)
  float_matrix = matrix.astype(np.float)
  linkage_matrix = linkage(squareform(float_matrix))

  dg = dendrogram(linkage_matrix, no_plot=True)
  dg_labels = list()
  for idx in dg['leaves']:
    dg_labels.append(cluster_labels[idx])

  dg = dendrogram(linkage_matrix, leaf_font_size=8,
    truncate_mode='lastp', p=10,
    show_contracted=True)

  directory = '../img/{}/dendrogram/'.format(series_type)
  if not os.path.exists(directory):
    os.makedirs(directory)

  fig.savefig('../img/{}/dendrogram/{}.png'.format(series_type, method), bbox_inches='tight')
  plt.close(fig)


import io
import re
from setuptools import setup

with io.open("README.rst", "rt", encoding="utf8") as f:
    readme = f.read()

setup(
    name="Expense Tracker",
    version='0.0.1',
    url="https://github.com/mujahedalaqqad/expense_tracker/",
    project_urls={
        "Documentation": "https://github.com/mujahedalaqqad/expense_tracker/wiki/",
        "Code": "https://github.com/mujahedalaqqad/expense_tracker",
        "Issue tracker": "https://github.com/mujahedalaqqad/expense_tracker/issues",
    },
    license="BSD",
    author="Mujahed Alaqqad",
    author_email="mujahed.alaqqad@outlook.com",
    maintainer="Mujahed Alaqqad",
    maintainer_email="mujahed.alaqqad@outlook.com",
    description="A command line application to manage your daily expenses",
    long_description=readme,
    packages=["tracker", "tests"],
    include_package_data=True,
    python_requires=">=3.6",
    install_requires=['Click', 'beautifultable'],
    classifiers=[
        "Development Status :: 3 - Alpha",
        "Intended Audience :: Developers",
        "License :: OSI Approved :: BSD License",
        "Operating System :: OS Independent",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7",
    ],
)


from __future__ import division
from svg.path import Line,CubicBezier
from andysSVGpathTools import pathT2tseg, segDerivative
from numpy import isclose
from andysmod import poly_roots
from warnings import warn

def isPointOutwardOfSeg(pt,seg):
    #Let c(t) = seg.point(t). If <pt-c(t),c'(t)>=0 has a solution 0<=t<=1, then this the normal leaving seg at t will intersect with pt.
    q0,q1= pt.real,pt.imag
    if isinstance(seg,Line):
        a0,a1=seg.start.real,seg.end.real
        b0,b1=seg.start.imag,seg.end.imag
        denom = a0**2 - 2*a0*a1 + a1**2 + b0**2 - 2*b0*b1 + b1**2
        if denom:
            return [(a0**2 - a0*a1 + b0**2 - b0*b1 - (a0 - a1)*q0 - (b0 - b1)*q1)/(a0**2 - 2*a0*a1 + a1**2 + b0**2 - 2*b0*b1 + b1**2)]
        else:
            return []
    elif isinstance(seg,CubicBezier):
        a0,a1,a2,a3 = seg.start.real,seg.control1.real,seg.control2.real,seg.end.real
        b0,b1,b2,b3 = seg.start.imag,seg.control1.imag,seg.control2.imag,seg.end.imag
        deg0 = 3*a0**2 - 3*a0*a1 + 3*b0**2 - 3*b0*b1 - 3*(a0 - a1)*q0 - 3*(b0 - b1)*q1
        deg1 = -15*a0**2 + 30*a0*a1 - 9*a1**2 - 6*a0*a2 - 15*b0**2 + 30*b0*b1 - 9*b1**2 - 6*b0*b2 + 6*(a0 - 2*a1 + a2)*q0 + 6*(b0 - 2*b1 + b2)*q1
        deg2 = 30*a0**2 - 90*a0*a1 + 54*a1**2 + 9*(4*a0 - 3*a1)*a2 - 3*a0*a3 + 30*b0**2 - 90*b0*b1 + 54*b1**2 + 9*(4*b0 - 3*b1)*b2 - 3*b0*b3 - 3*(a0 - 3*a1 + 3*a2 - a3)*q0 - 3*(b0 - 3*b1 + 3*b2 - b3)*q1
        deg3 = -30*a0**2 + 120*a0*a1 - 108*a1**2 - 36*(2*a0 - 3*a1)*a2 - 18*a2**2 + 12*(a0 - a1)*a3 - 30*b0**2 + 120*b0*b1 - 108*b1**2 - 36*(2*b0 - 3*b1)*b2 - 18*b2**2 + 12*(b0 - b1)*b3
        deg4 = 15*a0**2 - 75*a0*a1 + 90*a1**2 + 15*(4*a0 - 9*a1)*a2 + 45*a2**2 - 15*(a0 - 2*a1 + a2)*a3 + 15*b0**2 - 75*b0*b1 + 90*b1**2 + 15*(4*b0 - 9*b1)*b2 + 45*b2**2 - 15*(b0 - 2*b1 + b2)*b3
        deg5 = -3*a0**2 + 18*a0*a1 - 27*a1**2 - 18*(a0 - 3*a1)*a2 - 27*a2**2 + 6*(a0 - 3*a1 + 3*a2)*a3 - 3*a3**2 - 3*b0**2 + 18*b0*b1 - 27*b1**2 - 18*(b0 - 3*b1)*b2 - 27*b2**2 + 6*(b0 - 3*b1 + 3*b2)*b3 - 3*b3**2
        inner_prod_coeffs = (deg5,deg4,deg3,deg2,deg1,deg0)
        outward_condition = lambda t: 1j*(pt-seg.point(t))/segDerivative(seg,t)>0
        cond = lambda t: isclose(t.imag,0) and 0<=t.real<=1 and outward_condition(t)
        inward_tvals = poly_roots(inner_prod_coeffs,condition=cond)
        return [t.real for t in inward_tvals]
    else:
        raise Exception("Second argument must be line or CubicBezier.")
def isPointOutwardOfPath(pt,path,justone=False):
#returns a list of (seg_idx,t) tuples s.t. the outward normal to seg at t intersects pt.
#if justone=True, then this list will be of length 1
    inward_segt_pairs = []
    for idx,seg in enumerate(path):
        tvals = isPointOutwardOfSeg(pt,seg)
        if justone and len(tvals):
            return [(idx,tvals[0])]
        for t in tvals:
            inward_segt_pairs.append((idx,t))
    return inward_segt_pairs
def invTransect(T,sortedRingList,OM,warnifnotunique=True):
    '''
    Finds a transect that ends at T.  In the case there are more than one, if
    warnifnotunique=True, user will be warned, but this may slow down transect
    generation.
    Output: list of tuples (pt,ring_idx,seg_idx,t)
    '''
    cur_ring = sortedRingList[-1]
    test_idx = len(sortedRingList)-1
    init_t,init_seg = pathT2tseg(cur_ring.path,T)
    init_seg_idx = cur_ring.path.index(init_seg)
    transect_info = [(cur_ring.point(T),len(sortedRingList)-1,init_seg_idx,init_t)]
    cur_pt = transect_info[-1][0]
    while test_idx>=0:
        test_idx-=1
        test_ring = sortedRingList[test_idx]
        if OM(cur_ring,test_ring)>0:
            if warnifnotunique:
                inward_segt_list = isPointOutwardOfPath(cur_pt,test_ring.path,justone=False)
                if inward_segt_list == []:
                    continue
                if len(inward_segt_list)>1:
                    warn("The transect ending at T=%s is likely not unique."%T)
                seg_idx,t = inward_segt_list[0]
            else:
                inward_segt = isPointOutwardOfPath(cur_pt,test_ring.path,justone=True)
                if inward_segt == []:
                    continue
                seg_idx,t = inward_segt[0]
            transect_info.append(test_ring.path[seg_idx].point(t),test_idx,seg_idx,t)
            cur_ring = sortedRingList[test_idx]
            cur_pt = transect_info[-1][0]

        #Erroneous Termination
        if test_idx <= 0 and sortedRingList.index(cur_ring) !=0:
            raise Exception("Something went wrong finding inverse transect.")


import pydantic
import pytest
from rusty_results import Result, Ok
import json

class Model(pydantic.BaseModel):
    result_value: Result[int, str]

TEST_MODEL_SERIALIZED = '{"result_value": {"Ok": 10}}'

def test_serialize():
    model = Model(result_value=Ok(10))
    assert model.json() == TEST_MODEL_SERIALIZED

def test_deserialize():
    model = Model(**json.loads(TEST_MODEL_SERIALIZED))
    assert model == Model(result_value=Ok(10))

def test_deserialize_fails():
    wrong_values = [
        10,
        {"foo": 10},
    ]
    with pytest.raises(pydantic.ValidationError):
        for value in wrong_values:
            Model(foo=value)

def test_deserialize_wrong_value_raises():
    class Model(pydantic.BaseModel):
        optional_value: Result[str, str]
    with pytest.raises(pydantic.ValidationError):
        Model(optional_value=Ok((1, 2)))

def test_deserialize_wrong_number_of_values():
    with pytest.raises(pydantic.ValidationError):
        Model(optional_value={"Result": "foo", "Bar": "Baz"})

def test_deserialize_inner_is_none():
    with pytest.raises(pydantic.ValidationError):
        Model(optional_value={"Result": None})


import os
import csv
import random

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

import tensorflow as tf
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers import Dense, Activation, Flatten, Dropout, Lambda, Cropping2D, ELU
from keras.callbacks import EarlyStopping, TensorBoard, ModelCheckpoint
from keras.layers.normalization import BatchNormalization
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from sklearn.preprocessing import StandardScaler

def model_just_output():
    model = Sequential()
    model.add(Flatten(input_shape=(160, 320, 3)))
    model.add(Dense(1))
    return model

def model_simple():
    model = Sequential()
    model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(160, 320, 3)))
    model.add(Flatten())
    model.add(Dense(1))
    return model

def model_like_nvidia():
    model = Sequential()
    model.add(Lambda(lambda x: (x / 255.0) - 0.5, input_shape=(64, 64, 3)))

    model.add(Convolution2D(24, 5, 5, subsample=(2, 2), border_mode="same"))
    model.add(ELU())
    model.add(MaxPooling2D((2, 2), border_mode="same"))
    model.add(BatchNormalization())

    model.add(Convolution2D(36, 5, 5, subsample=(1, 1), border_mode="valid"))
    model.add(ELU())
    model.add(Dropout(.4))
    model.add(MaxPooling2D((2, 2), border_mode="valid"))
    model.add(BatchNormalization())

    model.add(Convolution2D(64, 3, 3, subsample=(1, 1), border_mode="valid"))
    model.add(ELU())
    model.add(Dropout(.4))
    model.add(MaxPooling2D((2, 2), border_mode="valid"))
    model.add(BatchNormalization())

    model.add(Flatten())

    model.add(Dense(1164))
    model.add(Dropout(.3))
    model.add(ELU())

    model.add(Dense(100))
    model.add(ELU())

    model.add(Dense(50))
    model.add(ELU())

    model.add(Dense(10))
    model.add(ELU())

    model.add(Dense(1))

    model.summary()
    return model

from keras.applications.vgg16 import VGG16
from keras.layers import AveragePooling2D
from keras.regularizers import l2
from keras.layers import Input
from keras.models import Model
def model_like_vgg16():
    input_image = Input(shape=(64, 64, 3))
    base_model = VGG16(input_tensor=input_image, include_top=False)

    for layer in base_model.layers[:-3]:
        layer.trainable = False

    x = base_model.get_layer("block5_conv3").output
    x = AveragePooling2D((2, 2))(x)
    x = Dropout(0.5)(x)
    x = BatchNormalization()(x)
    x = Dropout(0.5)(x)
    x = Flatten()(x)
    x = Dense(4096, activation="elu", W_regularizer=l2(0.01))(x)
    x = Dropout(0.5)(x)
    x = Dense(2048, activation="elu", W_regularizer=l2(0.01))(x)
    x = Dense(2048, activation="elu", W_regularizer=l2(0.01))(x)
    x = Dense(1, activation="linear")(x)
    model = Model(inputs=base_model.input, outputs=x)
    model.summary()

    return model

def augment_brightness_camera_images(image):
    '''
    :param image: Input image
    :return: output image with reduced brightness
    '''

    # convert to HSV so that its easy to adjust brightness
    image1 = cv2.cvtColor(image,cv2.COLOR_RGB2HSV)

    # randomly generate the brightness reduction factor
    # Add a constant so that it prevents the image from being completely dark
    random_bright = .25+np.random.uniform()

    # Apply the brightness reduction to the V channel
    image1[:,:,2] = image1[:,:,2]*random_bright

    # convert to RBG again
    image1 = cv2.cvtColor(image1,cv2.COLOR_HSV2RGB)
    return image1

def generator(samples, batch_size=32):
    num_samples = len(samples)
    use_extra_image = True
    dir_data = "data"
    while 1: # Loop forever so the generator never terminates
        shuffle(samples)
        for offset in range(0, num_samples, batch_size):
            batch_samples = samples[offset:offset+batch_size]

            images = []
            angles = []
            for batch_sample in batch_samples:
                idx = 0
                if use_extra_image:
                    idx = random.randint(0, 2)

                current_path = os.path.join(os.path.dirname(__file__), dir_data, "IMG")
                image = cv2.imread(os.path.join(current_path, os.path.split(batch_sample[idx])[-1]))
                image = cv2.resize(image[55:135, :], (64, 64))
                image.astype(np.float32)

                measurement = float(batch_sample[3])

                if idx == 1:  # left
                    measurement += 0.25
                elif idx == 2:  # right
                    measurement -= 0.25

                if random.randrange(0, 2) == 1:
                    image = cv2.flip(image, 1)
                    measurement = -measurement

                images.append(image)
                angles.append(measurement)

            # trim image to only see section with road
            X_train = np.array(images)
            y_train = np.array(angles)
            yield shuffle(X_train, y_train)

def train_by_generator():
    lines = []
    dir_data = "data"
    fpath = 'weights.{epoch:02d}--{loss:.2f}-{val_loss:.2f}.h5'
    cp_cb = ModelCheckpoint(filepath=fpath, monitor='val_loss', verbose=1, save_best_only=True, mode='auto')
    es_cb = EarlyStopping(monitor='val_loss', patience=2, verbose=1, mode='auto')

    samples = []
    with open(os.path.join(os.path.dirname(__file__), dir_data, "driving_log.csv")) as csvfile:
        reader = csv.reader(csvfile)
        for line in reader:
            samples.append(line)
    samples = samples[1:]
    random.shuffle(samples)

    train_samples, validation_samples = train_test_split(samples, test_size=0.2)

    # model.fit(X_train, y_train, validation_split=0.2, shuffle=True, nb_epoch=20, callbacks=[cp_cb, es_cb])

    # compile and train the model using the generator function
    train_generator = generator(train_samples, batch_size=32)
    validation_generator = generator(validation_samples, batch_size=32)

    model = model_vgg_based()
    model.compile(loss='mse', optimizer='adam')

    # Preprocess incoming data, centered around zero with small standard deviation
    history_object = model.fit_generator(train_generator, samples_per_epoch=len(train_samples),
                                         validation_data=validation_generator, nb_val_samples=len(validation_samples),
                                         nb_epoch=20, verbose=1, callbacks=[cp_cb, es_cb])

    ### print the keys contained in the history object
    print(history_object.history.keys())

    ### plot the training and validation loss for each epoch
    plt.plot(history_object.history['loss'])
    plt.plot(history_object.history['val_loss'])
    plt.title('model mean squared error loss')
    plt.ylabel('mean squared error loss')
    plt.xlabel('epoch')
    plt.legend(['training set', 'validation set'], loc='upper right')
    plt.show()

def train_by_once():
    lines = []
    dir_data = "data"
    fpath = 'weights.{epoch:02d}--{loss:.2f}-{val_loss:.2f}.h5'
    cp_cb = ModelCheckpoint(filepath=fpath, monitor='val_loss', verbose=1, save_best_only=True, mode='auto')
    es_cb = EarlyStopping(monitor='val_loss', patience=1, verbose=1, mode='auto')
    use_extra_image = True

    with open(os.path.join(os.path.dirname(__file__), dir_data, "driving_log.csv")) as csvfile:
        reader = csv.reader(csvfile)
        for line in reader:
            lines.append(line)
    lines = lines[1:]
    random.shuffle(lines)

    images = []
    measurements = []
    for line in lines:
        measurement = float(line[3])
        # We have very many zero steerling sample, so I will push out them from training data.
        # if measurement == 0.0 and random.randrange(0, 2) != 0:
        #    continue

        current_path = os.path.join(os.path.dirname(__file__), dir_data, "IMG")

        idx = 0
        if use_extra_image:
            idx = random.randint(0, 2)

        image = cv2.imread(os.path.join(current_path, os.path.split(line[idx])[-1]))
        image = cv2.resize(image[55:135, :], (64, 64))
        image.astype(np.float32)

        if idx == 1: # left
            measurement += 0.25
        elif idx == 2: # right
            measurement -= 0.25

        if random.randrange(0, 2) == 1:
            image = cv2.flip(image, 1)
            measurement = -measurement

        image = augment_brightness_camera_images(image)

        images.append(image)
        measurements.append(measurement)

    X_train = np.array(images)
    y_train = np.array(measurements)

    model = model_like_vgg16()
    #model = model_like_nvidia()
    from keras.utils.vis_utils import plot_model
    plot_model(model, to_file='model.png', show_shapes=True)
    model.compile(loss='mse', optimizer='adam')

    history_object = model.fit(X_train, y_train, validation_split=0.2, shuffle=True, nb_epoch=30, callbacks=[cp_cb, es_cb], batch_size=32)

    model.save("model.h5")

    ### print the keys contained in the history object
    print(history_object.history.keys())

    ### plot the training and validation loss for each epoch
    plt.plot(history_object.history['loss'])
    plt.plot(history_object.history['val_loss'])
    plt.title('model mean squared error loss')
    plt.ylabel('mean squared error loss')
    plt.xlabel('epoch')
    plt.legend(['training set', 'validation set'], loc='upper right')
    plt.savefig("training_result.png")
    plt.show()

    plt.hist(measurements, bins=20)
    plt.savefig("input_data.png")
    plt.show()

if __name__ == '__main__':
    train_by_once()


import os
# Mostrar si es un color primario

#input
color=(os.sys.argv[1])
color_primarios=True
while(color_primarios):
    color=input("Ingrese el color: (rojo/negro/Marron/azul/celeste/amarillo/anaranjado/violeta:")
    color_primarios=(color != "rojo" and color != "azul" and color!= "amarillo")

#fin del bucle
print("la respuesta es un color primario:",color)
print("Fin del bucle")