from cProfile import label
from tkinter import *
import os.path

class Programa:

    def __init__(self):
        self.title = "Login Uyeda Industrial de mexico"
        self.icon = './img/UYEDA.png'
        self.icon_alt = './21-tkinter/img/UYEDA.png'
        self.size = "650x500"
        self.resizable = False

    def cargar(self):
        #crear la ventana raiz
        ventana = Tk()
        self.ventana = ventana

        #titulo e la ventana
        ventana.title(self.title)

        #comprobar si existe un archivo
        ruta_icono = os.path.abspath(self.icon)

        #Icono de la ventana
        #ventana.iconbitmap(ruta_icono)

        if not os.path.isfile(ruta_icono):
            ruta_icono = os.path.abspath(self.icon_alt)

        #mostrar texto en el programa
        texto = Label(ventana, text=ruta_icono)
        texto.pack()

        #cambio en el tamaño de la ventana
        ventana.geometry(self.size)

        #bloquear el tamaño de la ventana
        if self.resizable:
            ventana.resizable(1,1)
        else:
            ventana.resizable(0,0)

    def addtexto(self, dato):
        texto = Label(self.ventana, text=dato)
        texto.pack()

    def mostrar(self):
        #arrancar y mostrar la ventana hasta que se cierre
        self.ventana.mainloop()

# Instanciar programa
programa = Programa()
programa.cargar()
programa.addtexto("**************************************")
programa.addtexto("Uyeda Industrial de Mexico")
programa.addtexto("Ingresa al Sistema")
programa.addtexto("Cotizaciones")
programa.addtexto("**************************************")
programa.mostrar()


"""
A simple baseline for a question answering system.
"""
import os
from pathlib import Path
import yaml
import openai
import gradio as gr
from annoy import AnnoyIndex
from PIL import Image
from transformers import BlipProcessor, BlipForConditionalGeneration
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.environ['OPENAI_API_KEY']

# --- load pre-processed chunks --- #
with open(Path(__file__).resolve().parent / "openai27052023.yaml", 'r') as f:
    paper = yaml.safe_load(f)
sentences = paper['sentences']

# --- embed chunks --- #
print("embedding chunks...")
embeddings = [
    r['embedding']
    for r in openai.Embedding.create(input=sentences, model='text-embedding-ada-002')['data']
]

# --- index embeddings for efficient search (using Spotify's annoy)--- #
hidden_size = len(embeddings[0])
index = AnnoyIndex(hidden_size, 'angular')  #  "angular" =  cosine
for i, e in enumerate(embeddings):
    index.add_item(i , e)
index.build(10)  # build 10 trees for efficient search

# --- iteratively answer questions (retrieve & generate) --- #

processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")

def run_on_cpu(image:str=None):
    if image is None:
        image = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
    # conditional image captioning
    text = "a photography of"
    inputs = processor(image, text, return_tensors="pt")
    out = model.generate(**inputs)
    print(processor.decode(out[0], skip_special_tokens=True))
    # >>> a photography of a woman and her dog
    # unconditional image captioning
    inputs = processor(image, return_tensors="pt")
    out = model.generate(**inputs)
    caption = processor.decode(out[0], skip_special_tokens=True)
    print(caption)
    # a woman sitting on the beach with her dog
    return caption

def respond(query, image, chat_history):
    if image is not None:
        caption = run_on_cpu(image)
        prompt = f"""
        The following is the description of an image:
        {caption}
        """
    embedding =  openai.Embedding.create(input = [query], model='text-embedding-ada-002')['data'][0]['embedding']
    # get nearest neighbors by vectors
    indices, distances = index.get_nns_by_vector(embedding,
                                                n=3,  # return top 3
                                                include_distances=True)
    results =  [
        (sentences[i], d)
        for i, d in zip(indices, distances)
    ]
    # with this, generate an answer
    excerpts = [res[0] for res in results]
    excerpts = '\n'.join([f'[{i}]. {excerpt}' for i, excerpt in enumerate(excerpts, start=1)])
    prompt = prompt + f"""
    user query:
    {query}

    title of the paper:
    {paper['title']}

    excerpts:
    {excerpts}
    ---
    given the excerpts from the paper above, answer the user query.
    In your answer, make sure to cite the excerpts by its number wherever appropriate.
    Note, however, that the excerpts may not be relevant to the user query.
    """
    gpt_role = """
    You are a human who speaks Korean and interacts with users like a exaggerate comedian.
    Don't let users know you're an AI.
    Never give out your any personal information.
    """
    chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo",
                                                    messages=[
                                                        {"role": "system", "content": gpt_role},
                                                        {"role": "user", "content": prompt}
                                                    ])
    answer = chat_completion.choices[0].message.content
    chat_history.append((query, answer))
    return "", None, chat_history

with gr.Blocks(css='style.css', theme=gr.themes.Soft()) as demo:
    DESCRIPTION = "# Chatbot"
    gr.Markdown(DESCRIPTION)
    with gr.Group(elem_id="gr-container"):
        chatbot = gr.Chatbot().style(height=768)
        msg = gr.Textbox()
        image = gr.Image().style(height=512)
        clear = gr.ClearButton([msg, chatbot, image])
        msg.submit(respond, [msg, image, chatbot], [msg, image, chatbot])

demo.launch()


import pytest
from dbt.tests.util import run_dbt, check_relations_equal

from dbt.tests.adapter.simple_copy.fixtures import (
    _PROPERTIES__SCHEMA_YML,
    _SEEDS__SEED_INITIAL,
    _MODELS__ADVANCED_INCREMENTAL,
    _MODELS__COMPOUND_SORT,
    _MODELS__DISABLED,
    _MODELS__EMPTY,
    _MODELS_GET_AND_REF_UPPERCASE,
    _MODELS__INCREMENTAL,
    _MODELS__INTERLEAVED_SORT,
    _MODELS__MATERIALIZED,
    _MODELS__VIEW_MODEL,
)

class TestSimpleCopyUppercase:
    @pytest.fixture(scope="class")
    def dbt_profile_target(self):
        return {
            "type": "postgres",
            "threads": 4,
            "host": "localhost",
            "port": 5432,
            "user": "root",
            "pass": "password",
            "dbname": "dbtMixedCase",
        }

    @pytest.fixture(scope="class")
    def models(self):
        return {
            "ADVANCED_INCREMENTAL.sql": _MODELS__ADVANCED_INCREMENTAL,
            "COMPOUND_SORT.sql": _MODELS__COMPOUND_SORT,
            "DISABLED.sql": _MODELS__DISABLED,
            "EMPTY.sql": _MODELS__EMPTY,
            "GET_AND_REF.sql": _MODELS_GET_AND_REF_UPPERCASE,
            "INCREMENTAL.sql": _MODELS__INCREMENTAL,
            "INTERLEAVED_SORT.sql": _MODELS__INTERLEAVED_SORT,
            "MATERIALIZED.sql": _MODELS__MATERIALIZED,
            "VIEW_MODEL.sql": _MODELS__VIEW_MODEL,
        }

    @pytest.fixture(scope="class")
    def properties(self):
        return {
            "schema.yml": _PROPERTIES__SCHEMA_YML,
        }

    @pytest.fixture(scope="class")
    def seeds(self):
        return {"seed.csv": _SEEDS__SEED_INITIAL}

    def test_simple_copy_uppercase(self, project):

        # Load the seed file and check that it worked
        results = run_dbt(["seed"])
        assert len(results) == 1

        # Run the project and ensure that all the models loaded
        results = run_dbt()
        assert len(results) == 7

        check_relations_equal(
            project.adapter, ["seed", "VIEW_MODEL", "INCREMENTAL", "MATERIALIZED", "GET_AND_REF"]
        )


import json
import signal
import sys
import shutil
import os
import subprocess
import requests
from steam import guard
from PyQt5 import QtWidgets, QtGui, QtCore

import PyUIs
import ConfirmationHandler
import AccountHandler
import Common

if not(sys.version_info.major == 3 and sys.version_info.minor >= 6):
    raise SystemExit('ERROR: Requires python >= 3.6')

class Empty(object):
    pass

def code_update(sa, code_box, code_bar):
    time = code_bar.value() - 1
    if time == 0:
        code_box.setText(sa.get_code())
        code_box.setAlignment(QtCore.Qt.AlignCenter)
        time = 30 - (sa.get_time() % 30)
    code_bar.setValue(time)

def restart():
    if getattr(sys, 'frozen', False):
        os.execl(sys.executable, sys.executable)
    else:
        os.execl(sys.executable, sys.executable, os.path.abspath(__file__))

def open_path(path):
    if sys.platform in ['windows', 'win32']:
        subprocess.Popen(['explorer', '/select', path])
    elif sys.platform == "darwin":
        subprocess.Popen(["open", "-R", path])
    else:
        subprocess.Popen(["xdg-open", path])

def save_mafiles(sa=None):
    with open(os.path.join(mafiles_folder_path, 'manifest.json'), 'w') as manifest_file:
        json.dump(manifest, manifest_file)
    if sa:
        with open(os.path.join(mafiles_folder_path, mafile_name), 'w') as mafile:
            json.dump(sa.secrets, mafile)

def refresh_session_handler():
    pass

def backup_codes_popup(sa):
    if not sa.backend:
        mwa = AccountHandler.get_mobilewebauth(sa)
        if not mwa:
            return
        sa.backend = mwa
    try:
        sa.create_emergency_codes()
        endfunc = Empty()
        endfunc.endfunc = False
        code_dialog = QtWidgets.QDialog()
        code_ui = PyUIs.PhoneDialog.Ui_Dialog()
        code_ui.setupUi(code_dialog)
        code_ui.buttonBox.rejected.connect(lambda: setattr(endfunc, 'endfunc', True))
        code_dialog.exec_()
        if endfunc.endfunc:
            return
        codes = sa.create_emergency_codes(code_ui.codeBox.text())
        codes = '\n'.join(codes)
    except guard.SteamAuthenticatorError as e:
        Common.error_popup(str(e))
        return
    if len(codes) > 0:
        bcodes_dialog = QtWidgets.QDialog()
        bcodes_ui = PyUIs.BackupCodesCreatedDialog.Ui_Dialog()
        bcodes_ui.setupUi(bcodes_dialog)
        bcodes_ui.copyButton.clicked.connect(lambda: (bcodes_ui.codeBox.selectAll(), bcodes_ui.codeBox.copy()))
        bcodes_ui.codeBox.setText(codes)
        bcodes_dialog.exec_()
    else:
        Common.error_popup('No codes were generated or invalid code', 'Warning:')

def backup_codes_delete(sa):
    if not sa.backend:
        mwa = AccountHandler.get_mobilewebauth(sa)
        if not mwa:
            return
        sa.backend = mwa
    endfunc = Empty()
    endfunc.endfunc = False
    bcodes_dialog = QtWidgets.QDialog()
    bcodes_ui = PyUIs.BackupCodesDeleteDialog.Ui_Dialog()
    bcodes_ui.setupUi(bcodes_dialog)
    bcodes_ui.buttonBox.rejected.connect(lambda: setattr(endfunc, 'endfunc', True))
    bcodes_dialog.exec_()
    if endfunc.endfunc:
        return
    try:
        sa.destroy_emergency_codes()
    except guard.SteamAuthenticatorError as e:
        Common.error_popup(str(e))

def test_mafiles(path, entry=False):
    try:
        manifest_file = open(os.path.join(path, 'manifest.json'))
    except IOError:
        return False
    test_manifest = json.load(manifest_file)
    if entry:
        try:
            with open(os.path.join(path, test_manifest['entries'][entry]['filename'])) as maf_file:
                maf = json.loads(maf_file.read())
            sa = guard.SteamAuthenticator(secrets=maf)
            sa.get_code()
        except (IOError, json.decoder.JSONDecodeError, guard.SteamAuthenticatorError):
            manifest_file.close()
            return False
        manifest_file.close()
        return True
    else:
        valid_entries = []
        for i in test_manifest['entries']:
            try:
                with open(os.path.join(path, i['filename'])) as maf_file:
                    maf = json.loads(maf_file.read())
                sa = guard.SteamAuthenticator(secrets=maf)
                sa.get_code()
                valid_entries.append(i)
            except (IOError, json.decoder.JSONDecodeError, guard.SteamAuthenticatorError):
                continue
        manifest_file.close()
        return valid_entries

def set_autoaccept(timer, sa, trades, markets):
    if trades or markets:
        timer.timeout.connect(lambda: accept_all(sa, trades, markets, False), QtCore.Qt.QueuedConnection)
        timer.start()
    else:
        timer.stop()

def accept_all(sa, trades=True, markets=True, others=True):
    AccountHandler.refresh_session(sa)
    confs = ConfirmationHandler.fetch_confirmations(sa)
    for i in range(len(confs)):
        if (not trades) and (confs[i].type == 2):
            del confs[i]
        if (not markets) and (confs[i].type == 3):
            del confs[i]
        if (not others) and not (confs[i].type in [2, 3]):
            del confs[i]
    if len(confs) == 0:
        return True
    return ConfirmationHandler.confirm_multi(sa, confs, 'allow')

def open_conf_dialog(sa):
    if not AccountHandler.refresh_session(sa):
        return
    info = Empty()
    info.index = 0
    info.confs = ConfirmationHandler.fetch_confirmations(sa)
    if len(info.confs) == 0:
        Common.error_popup('Nothing to confirm.', '  ')
        main_ui.confListButton.setText('Confirmations')
        return
    conf_dialog = QtWidgets.QDialog()
    conf_ui = PyUIs.ConfirmationDialog.Ui_Dialog()
    conf_ui.setupUi(conf_dialog)
    default_pixmap = QtGui.QPixmap(':/icons/confirmation_placeholder.png')

    def load_info():
        if len(info.confs) == 0:
            conf_dialog.hide()
            conf_dialog.close()
            conf_dialog.deleteLater()
            Common.error_popup('Nothing to confirm.', '  ')
            main_ui.confListButton.setText('Confirmations')
            return
        while True:
            try:
                conf = info.confs[info.index]
                break
            except IndexError:
                info.index -= 1
        conf_ui.titleLabel.setText(conf.description)
        conf_ui.infoLabel.setText('{0}\nTime: {1}\nID: {2}\nType: {3}'
                                  .format(conf.sub_description, conf.time, conf.id, conf.type_str))
        if conf.icon_url:
            pixmap = QtGui.QPixmap()
            pixmap.loadFromData(requests.get(conf.icon_url).content)
            conf_ui.iconLabel.setPixmap(pixmap)
        else:
            conf_ui.iconLabel.setPixmap(default_pixmap)
        conf_ui.backButton.setDisabled(info.index == 0)
        conf_ui.nextButton.setDisabled(info.index == (len(info.confs) - 1))

    def accept():
        AccountHandler.refresh_session(sa)
        if not info.confs[info.index].accept(sa):
            Common.error_popup('Failed to accept confirmation.')
        info.confs = ConfirmationHandler.fetch_confirmations(sa)
        load_info()

    def deny():
        AccountHandler.refresh_session(sa)
        if not info.confs[info.index].deny(sa):
            Common.error_popup('Failed to accept confirmation.')
        info.confs = ConfirmationHandler.fetch_confirmations(sa)
        load_info()

    def refresh_confs():
        AccountHandler.refresh_session(sa)
        info.confs = ConfirmationHandler.fetch_confirmations(sa)
        load_info()

    load_info()
    conf_ui.refreshButton.clicked.connect(refresh_confs)
    conf_ui.nextButton.clicked.connect(lambda: (setattr(info, 'index', ((info.index + 1) if info.index <
                                                                        (len(info.confs) - 1) else info.index)),
                                                load_info()))
    conf_ui.backButton.clicked.connect(lambda: (setattr(info, 'index', ((info.index - 1) if info.index > 0
                                                                        else info.index)), load_info()))
    conf_ui.acceptButton.clicked.connect(accept)
    conf_ui.denyButton.clicked.connect(deny)
    conf_dialog.exec_()

def add_authenticator():
    endfunc = Empty()
    endfunc.endfunc = False
    mwa = AccountHandler.get_mobilewebauth()
    if not mwa:
        return
    sa = guard.SteamAuthenticator(backend=mwa)
    if not sa.has_phone_number():
        code_dialog = QtWidgets.QDialog()
        code_ui = PyUIs.PhoneDialog.Ui_Dialog()
        code_ui.setupUi(code_dialog)
        code_ui.buttonBox.rejected.connect(lambda: setattr(endfunc, 'endfunc', True))
        code_dialog.setWindowTitle('Phone number')
        code_ui.actionBox.setText('This account is missing a phone number. Type yours below to add it.\n'
                                  'Eg. +1 123-456-7890')
        code_dialog.exec_()
        if endfunc.endfunc:
            return
        if sa.add_phone_number(code_ui.codeBox.text().replace('-', '')):
            code_dialog = QtWidgets.QDialog()
            code_ui = PyUIs.PhoneDialog.Ui_Dialog()
            code_ui.setupUi(code_dialog)
            code_ui.buttonBox.rejected.connect(lambda: setattr(endfunc, 'endfunc', True))
            code_dialog.exec_()
            if endfunc.endfunc:
                return
            if not sa.confirm_phone_number(code_ui.codeBox.text()):
                Common.error_popup('Failed to confirm phone number')
                return
        else:
            Common.error_popup('Failed to add phone number.')
            return
    try:
        sa.add()
    except guard.SteamAuthenticatorError as e:
        if 'DuplicateRequest' in str(e):
            code_dialog = QtWidgets.QDialog()
            code_ui = PyUIs.PhoneDialog.Ui_Dialog()
            code_ui.setupUi(code_dialog)
            code_ui.buttonBox.rejected.connect(lambda: setattr(endfunc, 'endfunc', True))
            code_dialog.setWindowTitle('Remove old authenticator')
            code_ui.actionBox.setText('There is already an authenticator associated with this account.'
                                      ' Enter its revocation code to remove it.')
            code_dialog.exec_()
            if endfunc.endfunc:
                return
            sa.secrets = {'revocation_code': code_ui.codeBox.text()}
            sa.revocation_code = code_ui.codeBox.text()
            try:
                sa.remove()
                sa.add()
            except guard.SteamAuthenticatorError as e:
                Common.error_popup(str(e))
                return
        else:
            Common.error_popup(e)
            return
    if os.path.isdir(mafiles_folder_path):
        if any('maFile' in x for x in os.listdir(mafiles_folder_path)) or 'manifest.json'\
                in os.listdir(mafiles_folder_path):
            Common.error_popup('The maFiles folder in the app folder is not empty.\nPlease remove it.')
            return
        else:
            shutil.rmtree(mafiles_folder_path)
    os.mkdir(mafiles_folder_path)
    with open(os.path.join(mafiles_folder_path, mwa.steam_id + '.maFile'), 'w') as maf:
        maf.write(json.dumps(sa.secrets))
    with open(os.path.join(mafiles_folder_path, 'manifest.json'), 'w') as manifest_file:
        manifest_file.write(json.dumps(
            {'periodic_checking': False, 'first_run': False, 'encrypted': False, 'periodic_checking_interval': 5,
             'periodic_checking_checkall': False, 'auto_confirm_market_transactions': False,
             'entries': [{'steamid': mwa.steam_id, 'encryption_iv': None, 'encryption_salt': None,
                         'filename': mwa.steam_id + '.maFile'}], 'auto_confirm_trades': False}))
    Common.error_popup('This is your revocation code. Write it down physically and keep it. You will need it in case'
                       ' you lose your authenticator.', sa.secrets['revocation_code'])
    code_dialog = QtWidgets.QDialog()
    code_ui = PyUIs.PhoneDialog.Ui_Dialog()
    code_ui.setupUi(code_dialog)
    code_ui.buttonBox.rejected.connect(lambda: setattr(endfunc, 'endfunc', True))
    while True:
        code_dialog.exec_()
        if endfunc.endfunc:
            return
        try:
            sa.finalize(code_ui.codeBox.text())
            break
        except guard.SteamAuthenticatorError as e:
            code_ui.msgBox.setText(str(e))

def remove_authenticator(sa):
    if not sa.backend:
        mwa = AccountHandler.get_mobilewebauth(sa)
        if not mwa:
            return
        sa.backend = mwa
    endfunc = Empty()
    endfunc.endfunc = False
    code_dialog = QtWidgets.QDialog()
    code_ui = PyUIs.PhoneDialog.Ui_Dialog()
    code_ui.setupUi(code_dialog)
    code_ui.buttonBox.rejected.connect(lambda: setattr(endfunc, 'endfunc', True))
    code_dialog.setWindowTitle('Remove authenticator')
    code_ui.actionBox.setText('Type \'yes\' into the box below to remove your\nauthenticator.')
    code_ui.msgBox.setText('Note that you will receive a 15-day\ntrade hold upon deactivating your authenticator.')
    for i in code_ui.buttonBox.buttons():
        if code_ui.buttonBox.buttonRole(i) == QtWidgets.QDialogButtonBox.AcceptRole:
            i.setEnabled(False)
    code_ui.codeBox.textChanged.connect(lambda x: [(b.setEnabled(x.lower() == 'yes')
                                                    if code_ui.buttonBox.buttonRole(b) ==
                                                    QtWidgets.QDialogButtonBox.AcceptRole else None)
                                                   for b in code_ui.buttonBox.buttons()])
    code_dialog.exec_()
    if endfunc.endfunc:
        return
    try:
        sa.remove()
    except guard.SteamAuthenticatorError as e:
        Common.error_popup(str(e))
        return
    os.remove(os.path.join(mafiles_folder_path, mafile_name))
    del manifest['entries'][manifest_entry_index]
    save_mafiles()
    restart()

def copy_mafiles():
    while True:
        file_dialog = QtWidgets.QFileDialog()
        f = str(file_dialog.getExistingDirectory(caption='Select your maFiles folder.'))
        if f == '':
            break
        if not test_mafiles(f):
            Common.error_popup('The selected folder does not contain valid maFiles.')
            continue
        if os.path.isdir(mafiles_folder_path):
            if any('maFile' in x for x in os.listdir(mafiles_folder_path)) or 'manifest.json'\
                    in os.listdir(mafiles_folder_path):
                Common.error_popup('The maFiles folder at {} is not empty.\nPlease remove it.'
                                   .format(mafiles_folder_path))
                continue
            else:
                shutil.rmtree(mafiles_folder_path)
        shutil.copytree(f, mafiles_folder_path)
        break

def app_load():
    global mafiles_folder_path, mafile_name, manifest_entry_index, manifest

    base_path = os.path.dirname(os.path.abspath(sys.executable)) if getattr(sys, 'frozen', False)\
        else os.path.dirname(os.path.abspath(__file__))
    if test_mafiles(os.path.join(base_path, 'maFiles')):
        mafiles_folder_path = os.path.join(base_path, 'maFiles')
    elif test_mafiles(os.path.expanduser(os.path.join('~', '.maFiles'))) and '--dbg' not in sys.argv:
        mafiles_folder_path = os.path.expanduser(os.path.join('~', '.maFiles'))
    else:
        mafiles_folder_path = os.path.join(base_path, 'maFiles') if os.path.basename(os.path.normpath(base_path)) ==\
                                      'PySteamAuth' else os.path.expanduser(os.path.join('~', '.maFiles'))
    while True:
        try:
            with open(os.path.join(mafiles_folder_path, 'manifest.json')) as manifest_file:
                manifest = json.loads(manifest_file.read())  # TODO add encryption support
            valid_entries = test_mafiles(mafiles_folder_path)
            if len(valid_entries) == 0:
                raise ValueError('No valid Manifest Entries found!')
            manifest_entry_index = 0
            if len(manifest['entries']) > 1:
                if ('selected_account' in manifest) and manifest['selected_account'] < len(manifest['entries']):
                    manifest_entry_index = manifest['selected_account']
                else:
                    ac_dialog = QtWidgets.QDialog()
                    ac_ui = PyUIs.AccountChooserDialog.Ui_Dialog()
                    ac_ui.setupUi(ac_dialog)
                    for i in valid_entries:
                        try:
                            entry = [None, str(i['steamid']), i['filename']]
                            with open(os.path.join(mafiles_folder_path, i['filename'])) as ma_file:
                                entry[0] = json.load(ma_file)['account_name']
                            ac_ui.accountSelectList.addTopLevelItem(QtWidgets.QTreeWidgetItem(entry))
                        except (json.JSONDecodeError, IOError):
                            continue
                    # noinspection PyUnresolvedReferences
                    ac_dialog.rejected.connect(sys.exit)
                    ac_ui.accountSelectList.itemSelectionChanged.connect(
                        lambda: ac_ui.buttonBox.setDisabled(len(ac_ui.accountSelectList.selectedItems()) != 1))
                    ac_dialog.exec_()
                    manifest_entry_index = ac_ui.accountSelectList.selectedIndexes()[0].row()
                    manifest['selected_account'] = manifest_entry_index
            mafile_name = manifest['entries'][manifest_entry_index]['filename']
            with open(os.path.join(mafiles_folder_path, mafile_name), 'r+') as ma_file:
                maf = json.load(ma_file)
                if 'device_id' not in maf:
                    maf['device_id'] = guard.generate_device_id(maf['steamid'])
                    ma_file.seek(0)
                    ma_file.write(json.dumps(maf))
                    ma_file.truncate()
            if not test_mafiles(mafiles_folder_path, manifest_entry_index):
                raise IOError()
            break
        except (IOError, ValueError, TypeError, IndexError, KeyError) as e:
            if os.path.isdir(mafiles_folder_path):
                if any('maFile' in x for x in os.listdir(mafiles_folder_path)) or 'manifest.json'\
                        in os.listdir(mafiles_folder_path):
                    Common.error_popup('Failed to load maFile: ' + str(e))
            setup_dialog = QtWidgets.QDialog()
            setup_ui = PyUIs.SetupDialog.Ui_Dialog()
            setup_ui.setupUi(setup_dialog)
            setup_ui.setupButton.clicked.connect(lambda: (setup_dialog.accept(), add_authenticator()))
            setup_ui.importButton.clicked.connect(lambda: (copy_mafiles(), setup_dialog.accept()))
            setup_ui.quitButton.clicked.connect(sys.exit)
            setup_dialog.exec_()
    sa = guard.SteamAuthenticator(maf)
    main_window.setWindowTitle('PySteamAuth - ' + sa.secrets['account_name'])
    main_ui.codeBox.setText(sa.get_code())
    main_ui.codeBox.setAlignment(QtCore.Qt.AlignCenter)
    main_ui.copyButton.clicked.connect(lambda: (main_ui.codeBox.selectAll(), main_ui.codeBox.copy()))
    main_ui.codeTimeBar.setTextVisible(False)
    main_ui.codeTimeBar.valueChanged.connect(main_ui.codeTimeBar.repaint)
    main_ui.tradeCheckBox.setChecked(manifest['auto_confirm_trades'])
    main_ui.marketCheckBox.setChecked(manifest['auto_confirm_market_transactions'])
    main_ui.confAllButton.clicked.connect(lambda: accept_all(sa))
    main_ui.confListButton.clicked.connect(lambda: open_conf_dialog(sa))
    main_ui.removeButton.clicked.connect(lambda: remove_authenticator(sa))
    main_ui.createBCodesButton.clicked.connect(lambda: backup_codes_popup(sa))
    main_ui.removeBCodesButton.clicked.connect(lambda: backup_codes_delete(sa))
    main_ui.actionOpen_Current_maFile.triggered.connect(lambda c: open_path(os.path.join(mafiles_folder_path,
                                                                                mafile_name)))
    main_ui.actionSwitch.triggered.connect(lambda c: (manifest.pop('selected_account'), save_mafiles(sa),
                                                      restart()))

    code_timer = QtCore.QTimer(main_window)
    code_timer.setInterval(1000)
    code_timer.timeout.connect(lambda: code_update(sa, main_ui.codeBox, main_ui.codeTimeBar))
    main_ui.codeTimeBar.setValue(30 - (sa.get_time() % 30))
    code_timer.start()

    aa_timer = QtCore.QTimer(main_window)
    aa_timer.setInterval(5000)
    set_autoaccept(aa_timer, sa, main_ui.tradeCheckBox.isChecked(), main_ui.marketCheckBox.isChecked())
    main_ui.tradeCheckBox.stateChanged.connect(lambda: set_autoaccept(aa_timer, sa, main_ui.tradeCheckBox.isChecked(),
                                                                      main_ui.marketCheckBox.isChecked()))
    main_ui.marketCheckBox.stateChanged.connect(lambda: set_autoaccept(aa_timer, sa, main_ui.tradeCheckBox.isChecked(),
                                                                       main_ui.marketCheckBox.isChecked()))

    main_window.show()
    main_window.raise_()

    save_mafiles(sa)

def main(argv):  # TODO debug menubar actions
    global app, main_window, main_ui

    sys.argv = argv

    signal.signal(signal.SIGINT, lambda x, y: app.exit(0))
    signal.signal(signal.SIGTERM, lambda x, y: app.exit(0))

    app = QtWidgets.QApplication(argv)
    if '--test' in argv:
        sys.exit()
        # QtCore.QTimer.singleShot(3000, app.quit)
    main_window = QtWidgets.QMainWindow()
    main_ui = PyUIs.MainWindow.Ui_MainWindow()
    main_ui.setupUi(main_window)
    QtCore.QTimer.singleShot(0, app_load)
    app.exec_()

if __name__ == '__main__':
    try:
        main(sys.argv)
    except KeyboardInterrupt:
        sys.exit()


from django import template
from django.core.exceptions import ObjectDoesNotExist
from goals.models import Activity

register = template.Library()

def date_activity(id):
    activity_list = []
    try:
        activities = Activity.objects.filter(date=id)
    except ObjectDoesNotExist:
        activities = None
    for activity in activities:
        activity_list.append(activity.description)
    return activity_list
register.filter('date_activity', date_activity)


from __future__ import unicode_literals
from __future__ import absolute_import

import os
import sys
import logging

log = logging.getLogger(__name__)

__version__ = '0.1.0'

def _(msg):
    """
    Temporal but valid translation function.
    """
    log.warning('_({}) called before configuring l10n'.format(msg))
    return msg

def find_localedir(localedir):
    """
    Custom function to find the locales message catalog in a multi-platform
    application.
    """
    import sys
    import os

    if localedir is None:
        if sys.platform.startswith('linux'):
            import gettext
            return gettext.bindtextdomain('messages')

        return find_localedir('locale')

    if os.path.isabs(localedir):
        return localedir

    def parent(path):
        return os.path.normpath(
            os.path.dirname(os.path.abspath(os.path.realpath(path)))
        )

    if sys.platform.startswith('win') and \
       getattr(sys, 'frozen', '') in ('dll', 'console_exe', 'windows_exe'):
        root = parent(sys.executable)
    else:
        import inspect
        root = parent(inspect.getfile(inspect.stack()[-1][0]))

    return os.path.join(root, localedir)

def l10n(domain=None, localedir=None):
    """
    Setup localization.

    :param str domain: The name of the translation domain (your application
     name). If None is provided (the default) the default domain is used as
     determined by gettext.textdomain() which usually is `messages`.

    :param str localedir: Path to the location of the binary `.mo` messages.

     Lookup path will then be `[prefix]/[locale]/LC_MESSAGES/[domain].mo`.

     The prefix will be determined as follow:

     1. If `localedir` is an absolute path as determined by
        :py:func:`os.path.isabs` then use the path as prefix for the lookup.

     2. If `localedir` is a relative path then determine the prefix as follow:

        1. If on Windows and in a freezed environment:

           `dirname([sys.executable])` + prefix

        2. Anything else:

           `dirname([calling_script])` + prefix

           Where `calling_script` is determined by inspecting the stack and
           retrieving the location of the calling script.

     3. If `localedir` is `None` (the default), then use the default system
        locale directory.

        1. On Linux:

           `[sys.prefix]/share/locale`

        2. Anything else, like Windows, which doesn't have a default folder,
           same as calling with `localedir` as a relative path `locale`.
    """
    import locale
    import traceback

    # Set LANG environment variable if unset
    # This is required by the gettext module to work on Windows
    os_windows = sys.platform.startswith('win')
    if os_windows:
        if os.getenv('LANG') is None:
            lang, enc = locale.getdefaultlocale()
            os.environ['LANG'] = lang

    # Set locale
    try:
        current = locale.setlocale(locale.LC_ALL, '')
        log.info('Locale set to {}'.format(current))
    except Exception:
        log.error('Unable to set user locale')
        log.error(traceback.format_exc())
        return _

    # Determine text domain and localedir
    import gettext

    if domain is None:
        domain = gettext.textdomain()

    localedir = find_localedir(localedir)

    # List modules to call
    modules = []
    if os_windows:
        try:
            import ctypes
            if not hasattr(l10n, 'libintl'):
                l10n.libintl = ctypes.cdll.LoadLibrary('intl.dll')
            modules.append(l10n.libintl)
        except Exception:
            log.error('Unable to load libintl')
            log.error(traceback.format_exc())
    modules.append(locale)
    modules.append(gettext)

    for module in modules:
        try:
            if hasattr(module, 'bindtextdomain'):
                module.bindtextdomain(domain, localedir)
            # Not sure about this call and encoding
            # Also, should we set the encoding here or just use lgettext
            # if hasattr(module, 'bind_textdomain_codeset'):
            #     module.bind_textdomain_codeset(domain, 'UTF-8')
            if hasattr(module, 'textdomain'):
                module.textdomain(domain)
        except Exception:
            log.error('Unable to bind text domain in module {}'.format(module))
            log.error(traceback.format_exc())

    global _
    _ = gettext.gettext

    return gettext.gettext

__all__ = ['_', 'l10n']


# To Run Arabic OCR
# python ArabicOCR.py <featureMethod> <classifier>
# featureMethod: StatisticalFeatures - NewGeometricFeatures
# classifier: SVM

TRAINING_DATASET = './Letters-Dataset-Generator/LettersDataset'
# TRAINING_DATASET = './Dataset/scanned'
TESTING_DATASET = './Dataset/Testing'

from importlib import import_module
import argparse
from tqdm import tqdm
import timeit
import glob
import cv2
import numpy as np
import os
from Preprocessing.Lines import LineSegmentation
from Preprocessing.Words import WordSegmentation
from Preprocessing.Characters import CharacterSegmentation
from Classification.TextLabeling import get_labels, getCharFromLabel
# from Preprocessing.PreprocessingTrain import get_dataset

import h5py
hdf5_dir = "PreprocessingOutput/1000-2000/"
def get_dataset(chars_file, labels_file):
    cfile = h5py.File(hdf5_dir + chars_file, "r+")
    imgs = []
    for img in cfile.keys():
        words = []
        for word in cfile[img].keys():
            word_1 = []
            for char in cfile[img][word].keys():
                word_1 += [np.array(cfile[img][word][char])]
            words += [word_1]
        imgs += [words]

    lfile = h5py.File(hdf5_dir + labels_file, "r+")
    labels = []
    for img in lfile.keys():
        label_img = []
        for word in lfile[img].keys():
            label_1 = []
            for label in lfile[img][word].keys():
                label_1 += [np.array(lfile[img][word][label])]
            label_img += [label_1]
        labels += [label_img]

    return imgs, labels

import re
def atoi(text):
    return int(text) if text.isdigit() else text

def natural_keys(text):
    return [ atoi(c) for c in re.split(r'(\d+)', text) ]

# checking if string contains list element
def contains(string, str_list):
    res = [ele for ele in str_list if(ele in string)]
    return bool(res)

def readImages(folder, trainTest = 0):
    images = []
    folders = [f for f in glob.glob(folder + "**/*", recursive=True)]
    y_vals = []
    filesNames = []
    for folder in folders:
        img_files = [img_file for img_file in glob.glob(folder + "**/*.png", recursive=True)]
        for i in range(len(img_files)):
            filesNames += [img_files[i]]
            img = cv2.imread(img_files[i])
            images.append(img)
            if trainTest == 0:
                label = int(img_files[i][img_files[i].index("label_") + len("label_"):img_files[i].index("_size")])
                y_vals.append(label)
    return images, y_vals, filesNames

def readImagesInFolder(folder):
    images = []
    folders = [f for f in glob.glob(folder + "**/*", recursive=True)]
    y_vals = []
    filesNames = []
    image_count = 0
    for img in folders:
        # only read first image
        if image_count == 4:
            return images, y_vals, filesNames
        filesNames += [img[img.index("scanned/") + len("scanned/"):]]
        image_count += 1
    return images, y_vals, filesNames

def imagePreprocessing(img):
    # Segment paragraph into lines
    lines = LineSegmentation(img, saveResults=False)

    # Segment lines into words
    words = []
    for i in range(len(lines)):
        words.extend(WordSegmentation(lines[i], lineNumber = i, saveResults=False))

    characters = []
    # Segment words into characters
    for word in words:
        characters.append(CharacterSegmentation(np.array(word, dtype=np.uint8)))

    return characters # [[[, , , characters], , , words] , , , lines]

if __name__ == "__main__":
    # Read arguments in order
    parser = argparse.ArgumentParser("Train Module")
    parser.add_argument("features")
    parser.add_argument("classifier")
    args = parser.parse_args() # Parse the arguments written by the user in the commandline

    # Import Modules
    #################

    # Import Features Type
    featuresModule = import_module('FeatureExtraction.' + args.features) # Dynamically load the features module
    featuresClass = getattr(featuresModule, args.features)
    features = featuresClass()

    # Import classifier Type
    classifierModule = import_module('Classification.' + args.classifier) # Dynamically load the classifier module
    classifierClass = getattr(classifierModule, args.classifier)
    classifier = classifierClass(features.featuresNumber)
    ###########################

    mode = int(input("1.Train\n2.Test existing Model\n"))

    if mode == 1:
        # set start time
        start_time = timeit.default_timer()

        #trainingImages, classifier.y_vals, __ = readImages(TRAINING_DATASET, trainTest = 0)

        if TRAINING_DATASET == './Dataset/scanned':
            # os.listdir("Dataset/scanned/")
            print("Reading Dataset")

            trainingImages = []
            imagesNames = []

            # for i in tqdm(sorted(glob.glob(TRAINING_DATASET + "*/*.png"),  key=natural_keys)):
            #     trainingImages += [cv2.imread(i)]
            #     imagesNames += [i[:-4]]

            print('-----------------------------')
            print("Preprocessing and feature Extraction Phase")

            processedCharacters = 0
            ignoredWords = 0
            processedWords = 0
            segmented = None

            skippedImages = 0
            TotalImages = 0

            for i in tqdm(sorted(glob.glob(TRAINING_DATASET + "*/*.png"),  key=natural_keys)[:2]):
                image = cv2.imread(i)

                textFileName = i[:-4].replace('scanned','text')
                textWords = open(textFileName+'.txt', encoding='utf-8').read().replace('\n',' ').split(' ')

                textWords = [item for item in textWords if item != '']

                segmented = imagePreprocessing(image) # Get characters of image
                # print("segmented", textFileName)

                # [[[, , , characters], , , words] , , , lines]
                double_char = "لا"
                segmentedWords = len(segmented)

                TotalImages += 1
                # print("Text: ", len(textWords), "Segmented: ", len(segmented))
                if len(textWords) > segmentedWords:
                    skippedImages += 1
                    continue
                faultyWordSegmented = False
                if len(textWords) < segmentedWords:
                    print("FAULTY IMAGE")
                    faultyWordSegmented = True

                for wordIndex in range(len(segmented)):

                    word = segmented[wordIndex]

                    correspondingTextWord = textWords[0]

                    # get count of occurances of "lam-alf" in word
                    occurances_count = correspondingTextWord.count(double_char)

                    text_length = len(correspondingTextWord)

                    # treat every "lam-alf" as one character
                    text_length -= occurances_count

                    # print("NOW Processing: ", correspondingTextWord, "text_length = ", text_length, " occurances_count = ", occurances_count, ' len(word) = ', len(word) )

                    if len(word) != text_length: # segmented characters != word characters
                        if faultyWordSegmented and wordIndex + 1 < len(segmented) and occurances_count > 0 and  text_length == len(word) + len(segmented[wordIndex+1]):
                            # There is لا that is causing a problem
                            correspondingTextWord = correspondingTextWord[:len(word)+1]
                            print("correspondingTextWord = ", correspondingTextWord)
                            textWords[0] = textWords[0][len(word)+1:]
                            processedWords += 1
                        else:
                            ignoredWords += 1
                            del textWords[0]
                            continue
                    else:
                        processedWords += 1
                        del textWords[0]

                    classifier.y_vals.extend(get_labels(correspondingTextWord))
                    for char in word:
                        processedCharacters += 1
                        #print('Currently processing image '+filesNames[0]+' line #', segmented.index(line), ' word #', line.index(word),' char #', word.index(char))
                        currentCharFeature = features.getFeatures(char, showResults = False, black_background=True)
                        classifier.x_vals.append(currentCharFeature) #cv2.resize(char, (100,60))

            print("processedCharacters = ", processedCharacters, "Characters from text = ", len(classifier.y_vals))
            print("ignoredWords = ", ignoredWords, " processedWords = ", processedWords)
            print("skipped Images = ", skippedImages, " (out of ", TotalImages,")")
            print('-----------------------------')
        else:
            # trainingImages, classifier.y_vals, filesNames = readImages(TRAINING_DATASET, 0)
            print("Loading dataset")
            trainingImages, labels = get_dataset('chars_101.h5', 'labels_101.h5')
            print("Finished Loading dataset")
            # Get Features
            # for i in tqdm(range(len(trainingImages))):
            for i in range(len(trainingImages)):
                print(i)
                for j in range(len(trainingImages[i])):
                    if (len(trainingImages[i][j]) == len(labels[i][j])):
                        for k in range(len(trainingImages[i][j])):
                            image = np.array(trainingImages[i][j][k])
                            classifier.x_vals.append(features.getFeatures(image, False))
                            classifier.y_vals.append(labels[i][j][k])

        # Train classifer
        print('Training Phase')
        print('-----------------------------')
        classifier.train()

        # for i in tqdm(sorted(glob.glob(TESTING_DATASET + "*/*.png"))):
        #     image = cv2.imread(i)
        #     textFileName = i[:-4]+'.txt'#.replace('scanned','text')
        #     segmented = imagePreprocessing(image) # Get characters of image
        #     print(len(segmented))
        #     # [[[, , , characters], , , words] , , , lines]
        #     f = open(textFileName,'w')
        #     for word in segmented:
        #         for char in word:
        #             currentCharFeature = features.getFeatures(char, False)
        #             classificationResult = classifier.getResult([currentCharFeature])
        #             # char = 'أ'
        #             char = getCharFromLabel(classificationResult)
        #             f.write(char)
        #         f.write(' ')
        #         # f.write('\n')
        #     f.close()

        # Test Model
        print('Testing Phase')
        print('-----------------------------')
        classifier.test()

        # Calculate and print total runtime
        print('Runtime: ', (timeit.default_timer() - start_time)/60)

        # Save Model
        print('Model Saved as ' +'Models/'+ args.classifier+'-'+args.features+'.sav')
        classifier.saveModel('Models/'+args.classifier+'-'+args.features)

    else:
        # modelFileName = input("Model filename:")
        print('Loading Model')
        print('-----------------------------')

        classifier.loadModel('Models/'+args.classifier+'-'+args.features)

        print('Load Dataset Phase')
        print('-----------------------------')
        # trainingImages, __ , filesNames = readImages(TESTING_DATASET, trainTest = 1)
        # print(filesNames)

        print('Processing')
        print('-----------------------------')

        for i in tqdm(sorted(glob.glob(TESTING_DATASET + "*/*.png"))):
            image = cv2.imread(i)
            textFileName = i[:-4]+'.txt'#.replace('scanned','text')
            segmented = imagePreprocessing(image) # Get characters of image
            print(len(segmented))
            # [[[, , , characters], , , words] , , , lines]
            f = open(textFileName,'wb+')
            for word in segmented:
                for char in word:
                    currentCharFeature = features.getFeatures(char, False)
                    classificationResult = classifier.getResult([currentCharFeature])
                    # char = 'أ'
                    char = getCharFromLabel(classificationResult)
                    f.write(char.encode('utf8'))
                f.write(' '.encode('utf8'))
                # f.write('\n')
            f.close()
            # filesNames.pop(0)


import cv2
import numpy as np
import operator
import os

MIN_CONTOUR_AREA = 100
RESIZED_IMAGE_WIDTH = 20
RESIZED_IMAGE_HEIGHT = 30

class ContourWithData:
    npaContour = None
    boundingRect = None
    intRectX = 0
    intRectY = 0
    intRectWidth = 0
    intRectHeight = 0
    fltArea = 0.0

    def calculateRectTopLeftPointAndWidthAndHeight(self):
        [intX, intY, intWidth, intHeight] = self.boundingRect
        self.intRectX = intX
        self.intRectY = intY
        self.intRectWidth = intWidth
        self.intRectHeight = intHeight

    def checkIfContourIsValid(self):
        if self.fltArea < MIN_CONTOUR_AREA: return False
        return True

def main():
    allContoursWithData = []
    validContoursWithData = []

    try:
        npaClassifications = np.loadtxt("classifications.txt", np.float32)
    except:
        print("Gagal membuka file Classifications.txt, Close Program\n")
        os.system("pause")
        return

    try:
        npaFlattenedImages = np.loadtxt("flattened_images.txt", np.float32)
    except:
        print("Gagal membuka file Flattened_Images.txt, Close Program\n")
        os.system("pause")
        return

    npaClassifications = npaClassifications.reshape((npaClassifications.size, 1))

    kNearest = cv2.ml.KNearest_create()

    kNearest.train(npaFlattenedImages, cv2.ml.ROW_SAMPLE, npaClassifications)

    ImageTesting = cv2.imread("Data_Training/test_5.jpg")

    if ImageTesting is None:
        print("Gagal mengimport file citra \n\n")
        os.system("pause")
        return

    ImageGray = cv2.cvtColor(ImageTesting, cv2.COLOR_BGR2GRAY)
    ImageBlurred = cv2.GaussianBlur(ImageGray, (5, 5), 0)

    ImageThresh = cv2.adaptiveThreshold(ImageBlurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)

    ImageThreshCopy = ImageThresh.copy()

    npaContours, npaHierarchy = cv2.findContours(ImageThreshCopy,
                                                 cv2.RETR_EXTERNAL,
                                                 cv2.CHAIN_APPROX_SIMPLE)

    for npaContour in npaContours:
        contourWithData = ContourWithData()
        contourWithData.npaContour = npaContour
        contourWithData.boundingRect = cv2.boundingRect(contourWithData.npaContour)
        contourWithData.calculateRectTopLeftPointAndWidthAndHeight()
        contourWithData.fltArea = cv2.contourArea(contourWithData.npaContour)
        allContoursWithData.append(contourWithData)

    for contourWithData in allContoursWithData:
        if contourWithData.checkIfContourIsValid():
            validContoursWithData.append(contourWithData)

    validContoursWithData.sort(key=operator.attrgetter("intRectX"))

    strFinalString = ""

    for contourWithData in validContoursWithData:
        cv2.rectangle(ImageTesting, (contourWithData.intRectX, contourWithData.intRectY), (contourWithData.intRectX + contourWithData.intRectWidth,
                      contourWithData.intRectY + contourWithData.intRectHeight), (0, 255, 0), 2)

        imgROI = ImageThresh[contourWithData.intRectY: contourWithData.intRectY + contourWithData.intRectHeight, contourWithData.intRectX:
                 contourWithData.intRectX + contourWithData.intRectWidth]

        imgROIResized = cv2.resize(imgROI, (RESIZED_IMAGE_WIDTH, RESIZED_IMAGE_HEIGHT))

        npaROIResized = imgROIResized.reshape((1, RESIZED_IMAGE_WIDTH * RESIZED_IMAGE_HEIGHT))

        npaROIResized = np.float32(npaROIResized)

        retval, npaResults, neigh_resp, dists = kNearest.findNearest(npaROIResized, k=1)

        strCurrentChar = str(chr(int(npaResults[0][0])))

        strFinalString = strFinalString + strCurrentChar

    print("\n" + "Karakter yang di Kenali Adalah : " + strFinalString + "\n")

    cv2.imshow("Citra Test", ImageTesting)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    return

if __name__ == "__main__":
    main()


from __future__ import division

import numpy as np
from root_pandas import read_root

def get_event_number(config):
    """ Compute the total number of events contained in the base tuples of
    a given configuration.

    Parameters
    ----------
    config : dictionary
        expected to contain the keys
            - 'filepath'
            - 'files'
            - 'pandas_kwargs'
    """
    files = [config['filepath'] + f for f in config['files']]
    df = read_root(files, key=config['pandas_kwargs']['key'],
                   columns=['SigYield_sw', 'nCandidate'])
    return df[df.nCandidate == 0].SigYield_sw.sum()

def d2_score(y_score, sample_weight=None):
    """ Compute <D^2> = <(1 - 2*omega)^2> where omega is either the per-event
    mistag estimate or the per event probability of the tag being correct.

    Parameters
    ----------
    y_score : array-like, shape=(n_samples,)
        omega or p(correct) values

    sample_weight : array-like, shape=(n_samples,), optional, default: None
        Weights. If set to None, all weights will be set to 1

    Returns
    -------
    score : float
        D squared
    """
    if sample_weight is None:
        sample_weight = np.ones_like(y_score)
    D2s = (1 - 2 * y_score)**2

    # This seems to return an unexpected nan value from time to time
    # return np.average(D2s, weights=sample_weight)
    return np.sum(sample_weight * D2s) / np.sum(sample_weight)

def tagging_power_score(df, config, total_event_number=None,
                        selected_event_number=None, efficiency=None,
                        etas='etas'):
    """ Compute per event tagging power with selection efficiency

    Parameters
    ----------
    df : pandas DataFrame
        a pandas DataFrame, expected to contain the columns
            - eta_column (default 'etas', optional, see etas)
            - 'target'
            - a weight branch (defined in the config object)
    config : dictionary
        a configuration object expected to contain the keys
            - all keys required for get_event_number
            - 'sorting_feature' used to select a single tagging particle
    etas : str or array, optional, default='etas'
        name of the column used as mistag prediction or list of eta values
    total_event_number : float, optional, default=None
        provide the total number of events in the base dataset for df.
        If this is none, the number will be extracted from the provided config
        file

    Returns
    -------
    score : float
        tagging power
    """
    if 'runNumber' not in df.index.names:
        df.set_index(['runNumber', 'eventNumber', '__array_index'], inplace=True)

    if type(etas) == str:
        etas = df[etas]

    sorting_feature = config['sorting_feature']
    grouped = df.groupby(['runNumber', 'eventNumber'], sort=False)
    idxmax = grouped[sorting_feature].idxmax()
    max_df = df.loc[idxmax]
    efficiency = (efficiency
                  or ((selected_event_number or max_df.SigYield_sw.sum())
                      / (total_event_number or get_event_number(config))
                      )
                  )
    return ((max_df.SigYield_sw * (1 - 2 * etas)**2).sum()
            / max_df.SigYield_sw.sum() * efficiency)


"""Harmony URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.contrib import admin
from rest_framework import routers
from stations.views import FavouriteViewSet, MostPlayedViewSet, Genre, StationDataViewSet, autocomplete, ModifyStationViewSet
from base.views import LanguageViewSet
router = routers.DefaultRouter()
router.register(r'station', StationDataViewSet, base_name='station')
router.register(r'languages', LanguageViewSet, base_name='languages')
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^api/', include(router.urls)),
    url(r'^api/favourite/$', FavouriteViewSet),
    url(r'^api/mostplayed/$', MostPlayedViewSet),
    url(r'^api/genre/$', Genre),
    url(r'^api/station/autocomplete', autocomplete),
    url(r'^api/updatestation/$', ModifyStationViewSet)
]


import numpy as np
from mtcnn import MTCNN
import cv2
from numpy import expand_dims
from PIL import Image
from sklearn.preprocessing import Normalizer, LabelEncoder

def detect_face(filename, detector):
	img = cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2RGB)
	results = detector.detect_faces(img)
	x1, y1, width, height = results[0]['box']
	x1, y1 = abs(x1), abs(y1)
	x2, y2 = x1 + width, y1 + height
	face = img[y1:y2, x1:x2]
	return face

def get_embeddings(face_pixels, model):
	#center the pixel
	new_width  = 160
	new_height = 160
	face_pixels = np.array(Image.fromarray(face_pixels).resize((new_width, new_height), Image.ANTIALIAS))
	face_pixels = face_pixels.astype('float32')
	mean, std = face_pixels.mean(), face_pixels.std()
	face_pixels = (face_pixels - mean) / std
	samples = expand_dims(face_pixels, axis=0)
	samples.shape
	# make prediction to get embedding
	yhat = model.predict(samples)
	return yhat[0]

def normalizer(trainX, testX):
	in_encoder = Normalizer(norm='l2')
	trainX = in_encoder.transform(trainX)
	testX = in_encoder.transform(testX)
	return trainX, testX

def normalizer(trainX):
	in_encoder = Normalizer(norm='l2')
	trainX = in_encoder.transform(trainX)
	return trainX

def labeller(trainy):
	out_encoder = LabelEncoder()
	out_encoder.fit(trainy)
	trainy = out_encoder.transform(trainy)
	return trainy

def labeller(trainy, testy):
	out_encoder = LabelEncoder()
	out_encoder.fit(trainy)
	trainy = out_encoder.transform(trainy)
	testy = out_encoder.transform(testy)
	return trainy, testy


import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split

from layers import *
from model import MLP

import os

# データを用意
X, Y = fetch_openml('mnist_784', version=1, data_home="./data/", return_X_y=True)

X = (X / 255.).to_numpy()
Y = Y.to_numpy().astype("int")

# 訓練・テストデータに分割
train_x, test_x, train_y, test_y = train_test_split(X, Y, test_size=0.2, random_state=2)
train_y = np.eye(10)[train_y].astype(np.int32)
test_y = np.eye(10)[test_y].astype(np.int32)
train_n = train_x.shape[0]
test_n = test_x.shape[0]

# モデルの構築
in_dim = 784 # 入力の次元
hid1_dim = 1024 # 一つ目の隠れ層の次元
hid2_dim = 512 # 二つ目の隠れ層の次元
out_dim = 10 # 出力の次元
use_dropout = True # ドロップアウトを行うかどうか
dropout_ratio = 0.2 # ドロップアウトの割合
lr = 1 # 学習率
optimizer = SGD(lr) # オプティマイザ
# optimizer = Adam(0.0005)
model = MLP([Linear(  in_dim, hid1_dim, ReLU, use_dropout=use_dropout, dropout_ratio=dropout_ratio),
             Linear(hid1_dim, hid2_dim, ReLU, use_dropout=use_dropout, dropout_ratio=dropout_ratio),
             Linear(hid2_dim,  out_dim, Softmax)], optimizer)

# 学習
n_epoch = 100 # エポックの数
batchsize = 100 # バッチサイズ

train_loss = []
train_acc = []
test_loss = []
test_acc = []

for epoch in range(n_epoch):
    print('epoch %d | ' % (epoch+1), end="")

    # 訓練
    sum_loss = 0
    pred_y = []
    perm = np.random.permutation(train_n)

    for i in range(0, train_n, batchsize):
        x = train_x[perm[i: i+batchsize]]
        t = train_y[perm[i: i+batchsize]]
        sum_loss += model.train(x, t) * len(x)
        # model.y には， (N, 10)の形で，画像が0~9の各数字のどれに分類されるかの事後確率が入っている
        # そこで，最も大きい値をもつインデックスを取得することで，識別結果を得ることができる
        pred_y.extend(np.argmax(model.y, axis=1))

    loss = sum_loss / train_n

    # accuracy : 予測結果を1-hot表現に変換し，正解との要素積の和を取ることで，正解数を計算できる．
    accuracy = np.sum(np.eye(10)[pred_y] * train_y[perm]) / train_n
    train_loss += [loss]
    train_acc += [accuracy]
    print('Train loss %.3f, accuracy %.4f | ' %(loss, accuracy), end="")

    # テスト
    sum_loss = 0
    pred_y = []

    for i in range(0, test_n, batchsize):
        x = test_x[i: i+batchsize]
        t = test_y[i: i+batchsize]

        sum_loss += model.test(x, t) * len(x)
        pred_y.extend(np.argmax(model.y, axis=1))

    loss = sum_loss / test_n
    accuracy = np.sum(np.eye(10)[pred_y] * test_y) / test_n
    test_loss += [loss]
    test_acc += [accuracy]
    print('Test loss %.3f, accuracy %.4f' %(loss, accuracy))

epochs = np.arange(1, n_epoch+1)

if not os.path.isdir('graph'):
    os.mkdir('graph')

plt.figure()
plt.plot(epochs, train_loss, label="train", color='tab:blue')
am = np.argmin(train_loss)
plt.plot(epochs[am], train_loss[am], color='tab:blue', marker='x')
plt.text(epochs[am], train_loss[am]-0.005, '%.3f' % train_loss[am], horizontalalignment="center", verticalalignment="top")

plt.plot(epochs, test_loss, label="test", color='tab:orange')
am = np.argmin(test_loss)
plt.plot(epochs[am], test_loss[am], color='tab:orange', marker='x')
plt.text(epochs[am], test_loss[am]+0.005, '%.3f' % test_loss[am], horizontalalignment="center", verticalalignment="bottom")

plt.legend()
plt.grid()
plt.xlabel("epoch")
plt.ylabel("loss")
plt.savefig('./graph/loss.png')

plt.figure()
plt.plot(epochs, train_acc, label="train")
am = np.argmax(train_acc)
plt.plot(epochs[am], train_acc[am], color='tab:blue', marker='x')
plt.text(epochs[am], train_acc[am]+0.001, '%.4f' % train_acc[am], horizontalalignment="center", verticalalignment="bottom")

plt.plot(epochs, test_acc, label="test")
am = np.argmax(test_acc)
plt.plot(epochs[am], test_acc[am], color='tab:orange', marker='x')
plt.text(epochs[am], test_acc[am]-0.001, '%.4f' % test_acc[am], horizontalalignment="center", verticalalignment="top")

plt.legend()
plt.grid()
plt.xlabel("epoch")
plt.ylabel("accuracy")
plt.savefig('./graph/accuracy.png')


raw_data = [line for line in open('Inputs/Day08Input.txt', 'r').read().splitlines()]
input_data = [[''.join(sorted(num)) for num in line.split(' ')] for line in [data.split(' | ')[0] for data in raw_data]]
for i in range(len(input_data)):
    input_data[i] = sorted(input_data[i], key=len)
output_data = [[num for num in line.split(' ')] for line in [data.split(' | ')[1] for data in raw_data]]

answer_one = 0

for line in output_data:
    for num in line:
        length = len(num)
        if length in [2, 3, 4, 7]:  # 1, 7, 4 or 8
            answer_one += 1

print(f'Answer 1: {answer_one}')  # 519

def symmetric_difference(x: str, y: str) -> str:
    return ''.join((set(x) - set(y)).union(set(y) - set(x)))

def get_0_6_9_c_d_e(line: list, one: str, four: str):
    eight = 'abcdefg'
    for i in range(len(line)):
        if len(line[i]) == 6:
            if symmetric_difference(line[i], eight) in list(one):
                six = line[i]
                c = symmetric_difference(line[i], eight)
            elif symmetric_difference(line[i], eight) in list(four):
                d = symmetric_difference(line[i], eight)
                zero = line[i]
            else:
                e = symmetric_difference(line[i], eight)
                nine = line[i]
    return zero, six, nine, c, d, e

def decode_input(line: list) -> dict[str, int]:
    assert len(line) == 10
    digits = {0: None,
              1: line[0],
              2: None,
              3: None,
              4: line[2],
              5: None,
              6: None,
              7: line[1],
              8: line[-1],
              9: None,
              }
    segments = {'a': symmetric_difference(digits[1], digits[7]),
                'b': None,
                'c': None,
                'd': None,
                'e': None,
                'f': None,
                'g': None,
                }
    digits[0], digits[6], digits[9], segments['c'], segments['d'], segments['e'] = get_0_6_9_c_d_e(line,
                                                                                digits[1],
                                                                                digits[4])
    segments['f'] = symmetric_difference(digits[1], segments['c'])
    segments['b'] = symmetric_difference(digits[4], segments['c'] + segments['d'] + segments['f'])
    segments['g'] = symmetric_difference(digits[0],
                                         segments['a'] + segments['b'] + segments['c'] + segments['e'] + segments['f'])
    digits[2] = ''.join(sorted(segments['a'] + segments['c'] + segments['d'] + segments['e'] + segments['g']))
    digits[3] = ''.join(sorted(segments['a'] + segments['c'] + segments['d'] + segments['f'] + segments['g']))
    digits[5] = ''.join(sorted(segments['a'] + segments['b'] + segments['d'] + segments['f'] + segments['g']))
    return {value: key for key, value in digits.items()}  # https://stackoverflow.com/a/483833

def decode_output(digits: dict[str, int], output: list[str]) -> int:
    result = ''
    for num in output:
        result += str(digits[''.join(sorted(list(num)))])
    return int(result)

assert len(input_data) == len(output_data)

answer_two = 0
for i in range(len(input_data)):
    answer_two += decode_output(decode_input(input_data[i]), output_data[i])

print(f'Answer 2: {answer_two}')


import requests as req
import pprint as pp
import pandas as pd
import sys, json, pymongo
from pymongo import MongoClient
import os
import csv

##Connecting to Database
conn = 'mongodb://localhost:27017'
client = pymongo.MongoClient(conn)
# Declare the database
db = client.Donor_Data
# Declare the collection
collection = db.projects

# print(collection.count())

results = db.projects.find()
for result in results:
    print(result)

url = "https://api.donorschoose.org/common/json_feed.html?APIKey=DONORSCHOOSE&newSince=1483256682&max=1000"

# Who was the director of the movie Aliens?
data = req.get(url).json()
post_id = collection.insert_many(data['proposals'])
data

# In[58]:

cursor = collection.find({},
    {'_id':0,'id': 1, 'title': 1, 'percentFunded': 1, 'numDonors': 1,'costToComplete':1,'numStudents':1,
     'totalPrice':1,'teacherId':1,'schoolTypes.id':1,'schoolName':1,'city':1,'zip':1,
     'state':1,'latitude':1,'longitude':1,'expirationDate':1,'fundingStatus':1,'gradeLevel.name':1,'povertyType.label':1,
     'subject.name':1})

flattened_records = []
for data_rec in cursor:
   #print(data_rec['gradeLevel']['name'])
    flattened_record = {
            'id': data_rec['id'],
            'title': data_rec['title'],
            'percentFunded' : data_rec['percentFunded'] if 'percentFunded' in data_rec.keys() else '',
            'numDonors': data_rec['numDonors'] if 'numDonors' in data_rec.keys() else '',
            'costToComplete' : data_rec['costToComplete'] if 'costToComplete' in data_rec.keys() else '',
            'numStudents': data_rec['numStudents'] if 'numStudents' in data_rec.keys() else '',
            'totalPrice' : data_rec['totalPrice'] if 'totalPrice' in data_rec.keys() else '',
            'schoolName': data_rec['schoolName'] if 'schoolName' in data_rec.keys() else '',
            'zip' : data_rec['zip'] if 'zip' in data_rec.keys() else '',
            'state': data_rec['state'] if 'state' in data_rec.keys() else '',
            'latitude' : data_rec['latitude'] if 'latitude' in data_rec.keys() else '',
            'longitude': data_rec['longitude'] if 'longitude' in data_rec.keys() else '',
            'expirationDate' : data_rec['expirationDate'] if 'expirationDate' in data_rec.keys() else '',
            'fundingStatus': data_rec['fundingStatus'] if 'fundingStatus' in data_rec.keys() else '',
            'gradelevel':data_rec['gradeLevel']['name'] if 'gradeLevel' in data_rec.keys() else '',
            'povertyType':data_rec['povertyType']['label'] if 'povertyType' in data_rec.keys() else '',
            'subject':data_rec['subject']['name'] if 'subject' in data_rec.keys() else ''

        }
    flattened_records.append(flattened_record)

#print(flattened_records)
# # Step 2: Iterate through the list of flattened records and write them to the csv file
with open('stack_039.csv', 'w') as outfile:
    fields = ['_id', 'id', 'title', 'percentFunded','numDonors','costToComplete','numStudents','totalPrice' ,'schoolName','zip','state'
        ,'latitude' ,'longitude','expirationDate' ,'fundingStatus','gradelevel','povertyType','subject']
    write = csv.DictWriter(outfile, fieldnames=fields)
    write.writeheader()
    for flattened_record in flattened_records:
        write.writerow(flattened_record)

# In[59]:

# print("id,title")
# cursor = collection.find()

# while (cursor.hasNext()) {
#     jsonObject = cursor.next();
#     print(jsonObject._id.valueOf() + "," + jsonObject.id + ",\"" + jsonObject.title +"\"")
# }


import random
def chat():
    print("Hey there! I am Robot 2.0 at your service")

    responses = {
        "my name is": "Hello {name}, How are you?",
        "hi": "Hello, my name is Hiesenberg",
        "what is your name?": "I am a bot created by Heisenberg. You can call me crazy!",
        "how are you?": "I'm doing good. How about you?",
        "sorry": ["It's alright", "It's OK, never mind"],
        "i am fine": "Great to hear that. How can I help you?",
        "good": "Nice to hear that. How can I help you? :)",
        "age?": "I'm a computer program, dude. Seriously, you are asking me this?",
        "what do you want?": "Make me an offer I can't refuse",
        "created?": ["Raghav created me using Python's NLTK library", "Top secret ;)"],
        "location?": "Indore, Madhya Pradesh",
        "how is your health": "I'm a computer program, so I'm always healthy",
        "sports": "I'm a very big fan of Football",
        "who is": ["Messi", "Ronaldo", "Rooney"],
        "who is": "Brad Pitt",
        "i am looking for online guides and courses to learn data science, can you suggest?": "Crazy_Tech has many great articles with step-by-step explanations along with code. You can explore",
        "quit": "Thank you for using our intelligence services"
    }

    while True:
        user_input = input("> ")
        found_response = False

        for pattern, response in responses.items():
            if pattern in user_input.lower():
                if "{name}" in response:
                    name = user_input.split("is", 1)[1].strip()
                    response = response.format(name=name)
                print(response)
                found_response = True
                break

        if not found_response:
            print("Sorry, I didn't understand that. Can you please rephrase?")

        if user_input.lower() == "quit":
            break

if __name__ == "__main__":
    chat()


#!/usr/bin/env python

import argparse
import json
import os
import pathlib
import tqdm

def load_val_ids(path):
    with open(path, 'r') as fin:
        paths = [line.strip() for line in fin.readlines()]
    ids = [int(line.split('_')[-1].split('.')[0]) for line in paths]
    return ids

def split_annotations(annotation_dir, val_ids):
    with open(annotation_dir / 'instances_train2017.json', 'r') as fin:
        train_data = json.load(fin)
    with open(annotation_dir / 'instances_val2017.json', 'r') as fin:
        val_data = json.load(fin)
    annotations = train_data['annotations'] + val_data['annotations']
    images = train_data['images'] + val_data['images']

    train_annotations = []
    val_annotations = []
    for annotation in tqdm.tqdm(annotations):
        if annotation['image_id'] in val_ids:
            val_annotations.append(annotation)
        else:
            train_annotations.append(annotation)

    train_images = []
    val_images = []
    for image in tqdm.tqdm(images):
        if image['id'] in val_ids:
            val_images.append(image)
        else:
            train_images.append(image)

    train_data = {
        'images': train_images,
        'annotations': train_annotations,
        'categories': train_data['categories'],
    }
    val_data = {
        'images': val_images,
        'annotations': val_annotations,
        'categories': val_data['categories'],
    }
    return train_data, val_data

def split_image_paths(coco2017_dir, val_ids):
    train_paths = sorted((coco2017_dir / 'train2017').glob('*'))
    val_paths = sorted((coco2017_dir / 'val2017').glob('*'))
    paths = train_paths + val_paths

    train_paths = []
    val_paths = []
    for path in tqdm.tqdm(paths):
        image_id = int(path.name.split('.')[0])
        if image_id in val_ids:
            val_paths.append(path)
        else:
            train_paths.append(path)
    return train_paths, val_paths

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--val_list', type=str, required=True)
    parser.add_argument('--coco2017_dir', type=str, required=True)
    parser.add_argument('--outdir', type=str, required=True)
    args = parser.parse_args()

    val_list_path = pathlib.Path(args.val_list)
    val_ids = load_val_ids(val_list_path)

    outdir = pathlib.Path(args.outdir)
    outdir.mkdir(exist_ok=True, parents=True)

    coco2017_dir = pathlib.Path(args.coco2017_dir)
    train_data, val_data = split_annotations(coco2017_dir / 'annotations',
                                             val_ids)
    train_paths, val_paths = split_image_paths(coco2017_dir, val_ids)

    annotation_dir = outdir / 'annotations'
    annotation_dir.mkdir(exist_ok=True, parents=True)
    with open(annotation_dir / 'instances_train2014.json', 'w') as fout:
        json.dump(train_data, fout)
    with open(annotation_dir / 'instances_val2014.json', 'w') as fout:
        json.dump(val_data, fout)

    train_image_dir = outdir / 'train2014'
    val_image_dir = outdir / 'val2014'
    train_image_dir.mkdir(exist_ok=True, parents=True)
    val_image_dir.mkdir(exist_ok=True, parents=True)
    for path in train_paths:
        os.symlink(path, train_image_dir / path.name)
    for path in val_paths:
        os.symlink(path, val_image_dir / path.name)

if __name__ == '__main__':
    main()


# 프로그래머스 Lv1 - 내적
# 문제링크: https://programmers.co.kr/learn/courses/30/lessons/70128

# 정확성 테스트 케이스 9개 중 9개 성공
# 총점 100.0

def solution(a,b):
    answer = 0
    n = len(a) # 리스트의 길이 a를 n에 대입
    for i in range(n): # i부터 n까지
        answer += a[i]*b[i]
        #a와 b의 i원소들의 곱한 값을 answer에 더하기
    return answer

a = [1,2,3,4]
b = [-3,-1,0,2]

print (solution(a,b))
# 출력 결과: 3


## rtaudio testing
import time
import rtaudio as rt
import struct
import numpy as np
import matplotlib.pyplot as pl
pl.ion()

class Player():
    def __init__(self):
        output_device = 'Apple Inc.: Built-in Output'
        output_device_idx = None

        dac = rt.RtAudio()
        n_devices = dac.getDeviceCount()
        for i in range(n_devices):
            info = dac.getDeviceInfo(i)
            #print(info)
            if info['name'] == output_device:
                output_device_idx = i

        self.buffer_size = 256

        oParams = {'deviceId': output_device_idx, 'nChannels': 2, 'firstChannel': 0}
        dac.openStream(oParams, {}, 44100, self.buffer_size, self.callback)
        dac.startStream()

        self._idx = 0

        t0 = time.time()
        while True:
            if time.time() - t0 > 2:
                break

        try:
            dac.stopStream()
        except:
            pass
        if dac.isStreamOpen():
            dac.closeStream()

    def callback(self, out_buffer, *args):
        for i in range(self.buffer_size):
            samp = np.sin(2*np.pi*440*self._idx/44100)
            struct.pack_into('f', out_buffer, 4*i, samp)
            self._idx += 1

class Recorder():
    def __init__(self):
        input_device = 'Apple Inc.: Built-in Microphone'

        dac = rt.RtAudio()
        input_device_idx = None
        n_devices = dac.getDeviceCount()
        for i in range(n_devices):
            info = dac.getDeviceInfo(i)
            #print(info)
            if info['name'] == input_device:
                input_device_idx = i
        print(input_device_idx)

        self.buffer_size = 512

        oParams = {'deviceId': input_device_idx, 'nChannels': 1, 'firstChannel': 0}
        dac.openStream({}, oParams, 48000, self.buffer_size, self.callback)
        dac.startStream()

        self.data = []

        t0 = time.time()
        while True:
            if time.time() - t0 > 2:
                break

        try:
            dac.stopStream()
        except:
            pass
        if dac.isStreamOpen():
            dac.closeStream()

    def callback(self, rw_buffer, r_buffer):
        #rw = rw_buffer[:]
        #r = r_buffer[:]

        n_frames = int(len(r_buffer) / 4)
        r = struct.unpack(n_frames*'f', r_buffer)
        self.data.append(r)

if __name__ == '__main__':
    #r = Recorder()
    #d = np.array(r.data, dtype=np.float32)
    #np.save('test', d.ravel())

    p = Player()
##


import numpy as np
from PIL import Image
from sklearn import svm
import os
import pickle

import load_pickle_database
from numpy import int

[dataset_read,label_read] = load_pickle_database.load_images_labels("/Users/yiliu/Box Sync/Stanford/Classes/2017-2018 Fall/CS 229 Machine Learning/Project/CAPTCHA Database/Simple single letter dataset/50000_single_letter.p")

#Split train set and test set
nTrainPrcntg = 0.7
nTrain = int(nTrainPrcntg*dataset_read.shape[0])
trainData = dataset_read[0:nTrain,:]
trainLabel = label_read[0:nTrain,]
testData = dataset_read[nTrain:,:]
testLabel = label_read[nTrain:,]

#Train model
multiClassSVM = svm.LinearSVC(loss = 'squared_hinge', penalty = 'l2',C=1/5000 ,max_iter=8000,verbose=1) #define kernel? default should be rbf-kernel
multiClassSVM.fit(trainData,trainLabel)
saveFile = "/Users/yiliu/Box Sync/Stanford/Classes/2017-2018 Fall/CS 229 Machine Learning/Project/CAPTCHA Database/Simple single letter dataset/SVM_Model.p"
pickle.dump(multiClassSVM,open(os.path.join(saveFile),'wb')) #Save model
print("Model Saved")
# print(trainData.shape)

#Load prediction dataset
loadPath = "/Users/yiliu/Box Sync/Stanford/Classes/2017-2018 Fall/CS 229 Machine Learning/Project/CAPTCHA Database/Simple single letter dataset/prediction_data.p"
[predData,predLabel] = pickle.load(open(loadPath, 'rb'))

#Training accuracy
confidenceTrain = multiClassSVM.decision_function(trainData)
predictionTrain = np.argmax(confidenceTrain, axis=1)
print(trainLabel.shape)
comparisonTrain = np.equal(predictionTrain,trainLabel)
accuracyTrain = sum(comparisonTrain)/trainLabel.size
print('Train accuracy:')
print(accuracyTrain)

#Test accuracy
confidenceTest = multiClassSVM.decision_function(testData)
predictionTest = np.argmax(confidenceTest, axis=1)
comparisonTest = np.equal(predictionTest,testLabel)
accuracyTest = sum(comparisonTest)/testLabel.size
print('Test accuracy:')
print(accuracyTest)

#Make predictions
confidencePred = multiClassSVM.decision_function(predData)
predictionPred = np.argmax(confidencePred, axis=1)

#Prediction accuracy
temp = 0
iteration = 0
for x in range(0,10):
    iteration = iteration + 1
    if predictionPred[x] == ord(predLabel[x,])-65:
        temp = temp+1
print('Correct number:')
print(temp)

accuracyPred = temp/predLabel.size
print('Prediction accuracy:')
print(accuracyPred)


import os

from pyven.exceptions.exception import PyvenException
import pyven.constants

from pyven.items.item import Item

from pyven.logging.logger import Logger

class Artifact(Item):

	def __init__(self, company, name, config, version, repo, to_retrieve, publish, file):
		super(Artifact, self).__init__(company, name, config, version, repo, to_retrieve, publish)
		self.file = file

	def type(self):
		return 'artifact'

	def basename(self):
		if self.file is not None:
			return os.path.basename(self.file)
		raise PyvenException('Unknown artifact location : ' + self.format_name())

	def check_generation(self, artifacts_checker):
		if not os.path.isfile(self.file):
			msg = ['Artifact not found : ' + self.format_name(),\
					'Expected location : ' + self.file]
			artifacts_checker.errors.append(msg)
			Logger.get().error(msg[0])
			Logger.get().error(msg[1])
			return False
		return True

	def check_version(self, artifacts_checker):
		if pyven.constants.PLATFORM == 'windows' and not self.to_retrieve:
			from win32api import GetFileVersionInfo, LOWORD, HIWORD
			expected_version = self.version.split('.')
			try:
				if self.file.endswith('.exe') or self.file.endswith('.dll'):
					info = GetFileVersionInfo(self.file, '\\')
					ms = info['FileVersionMS']
					ls = info['FileVersionLS']
					actual_version = [str(i) for i in [HIWORD(ms), LOWORD(ms), HIWORD(ls), LOWORD(ls)]]
					if len(expected_version) > len(actual_version):
						msg = ['Artifact version too short : ' + self.format_name(),\
								'Expected version : ' + self.version,\
								'Found version    : ' + '.'.join(actual_version)]
						artifacts_checker.errors.append(msg)
						Logger.get().error(msg[0])
						Logger.get().error(msg[1])
						Logger.get().error(msg[2])
						return False
					for idx, expected in enumerate(expected_version):
						if actual_version[idx] != expected:
							msg = ['Invalid artifact version : ' + self.format_name(),\
									'Expected version : ' + self.version,\
									'Found version    : ' + '.'.join(actual_version)]
							artifacts_checker.errors.append(msg)
							Logger.get().error(msg[0])
							Logger.get().error(msg[1])
							Logger.get().error(msg[2])
							return False
				return True
			except:
				if self.version not in ['0', '0.0', '0.0.0', '0.0.0.0']:
					msg = ['Artifact version not found : ' + self.format_name(), 'Expected version : ' + self.version]
					artifacts_checker.errors.append(msg)
					Logger.get().error(msg[0])
					Logger.get().error(msg[1])
					return False
				return True
		else:
			return True

	def check(self, artifacts_checker):
		if (self.check_generation(artifacts_checker)):
			return self.check_version(artifacts_checker)
		return False


import scapy.all as scapy
from scapy.layers import http # import module for filtering http requests
import argparse

def get_arguments():
    parser = argparse.ArgumentParser()
    parser.add_argument("-i","--iface",dest="interface",help="Specify interface to sniff data")
    value = parser.parse_args()
    if not value.interface:
        print("[-]Specify an interface")
    else:
        return value

#this method sniffs data on given interface , prn is for callback function and store = False indicates not to store in memory
def sniff(interface):
    scapy.sniff(iface=interface,store=False,prn=process_sniffed)

#this method captures the http requests "host" is the domain and "path" is the resource or web page
def get_url(packet):
    return packet[http.HTTPRequest].Host + packet[http.HTTPRequest].Path

#this method captures post requests/login information
def get_credentials(packet):
    if packet.haslayer(scapy.Raw):
        load = packet[scapy.Raw].load
        keywords=["username","login","email","user","password","pass"]
        for keyword in keywords:
            if keyword in load:
                return load

#this method is a call back function for scapy.sniff and it processes the packet captured
def process_sniffed(packet):
    if packet.haslayer(http.HTTPRequest):
        url = get_url(packet)
        print("[+]Captured HTTP Request :"+url)

    credentials =get_credentials(packet)
    if credentials:
        print("\n[+]Captured Credentials :"+credentials+"\n")

value = get_arguments()
sniff(value.interface)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('profile', '0024_market'),
    ]

    operations = [
        migrations.CreateModel(
            name='Follow',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('company', models.ForeignKey(to='profile.Company')),
            ],
        ),
        migrations.AddField(
            model_name='posting',
            name='started',
            field=models.BooleanField(default=False),
        ),
        migrations.AddField(
            model_name='follow',
            name='posting',
            field=models.ForeignKey(to='profile.Posting'),
        ),
        migrations.AddField(
            model_name='follow',
            name='student',
            field=models.ForeignKey(to='profile.Student'),
        ),
    ]


from unittest2 import TestCase

from datetime import datetime, timedelta
import securitybot.util as util

class VarTest(TestCase):
    def test_hours(self):
        assert util.OPENING_HOUR < util.CLOSING_HOUR, 'Closing hour must be after opening hour.'

class NamedTupleTest(TestCase):
    def test_empty(self):
        tup = util.tuple_builder()
        assert tup.answer is None
        assert tup.text == ''

    def test_full(self):
        tup = util.tuple_builder(True, 'Yes')
        assert tup.answer is True
        assert tup.text == 'Yes'

class BusinessHoursTest(TestCase):
    def test_weekday(self):
        '''Test business hours during a weekday.'''
        # 18 July 2016 is a Monday. If this changes, please contact the IERS.
        morning = datetime(year=2016, month=7, day=18, hour=util.OPENING_HOUR,
                           tzinfo=util.LOCAL_TZ)
        assert util.during_business_hours(morning)
        noon = datetime(year=2016, month=7, day=18, hour=12, tzinfo=util.LOCAL_TZ)
        assert util.during_business_hours(noon), \
            'This may fail if noon is no longer during business hours.'
        afternoon = datetime(year=2016, month=7, day=18, hour=util.CLOSING_HOUR - 1,
                             minute=59, second=59, tzinfo=util.LOCAL_TZ)
        assert util.during_business_hours(afternoon)

        breakfast = datetime(year=2016, month=7, day=18, hour=util.OPENING_HOUR - 1, minute=59,
                             second=59, tzinfo=util.LOCAL_TZ)
        assert not util.during_business_hours(breakfast)
        supper = datetime(year=2016, month=7, day=18, hour=util.CLOSING_HOUR,
                          tzinfo=util.LOCAL_TZ)
        assert not util.during_business_hours(supper)

    def test_weekend(self):
        '''Test "business hours" during a weekend.'''
        # As such, 17 July 2016 is a Sunday.
        sunday_morning = datetime(year=2016, month=7, day=17, hour=util.OPENING_HOUR,
                                  tzinfo=util.LOCAL_TZ)
        assert not util.during_business_hours(sunday_morning)

class ExpirationTimeTest(TestCase):
    def test_same_day(self):
        '''Test time delta within the same day.'''
        date = datetime(year=2016, month=7, day=18, hour=util.OPENING_HOUR, tzinfo=util.LOCAL_TZ)
        td = timedelta(hours=((util.CLOSING_HOUR - util.OPENING_HOUR) % 24) / 2)
        after = date + td
        assert util.get_expiration_time(date, td) == after

    def test_next_weekday(self):
        '''Test time delta overnight.'''
        date = datetime(year=2016, month=7, day=18, hour=util.CLOSING_HOUR - 1,
                        tzinfo=util.LOCAL_TZ)
        next_date = datetime(year=2016, month=7, day=19, hour=util.OPENING_HOUR + 1,
                             tzinfo=util.LOCAL_TZ)
        assert util.get_expiration_time(date, timedelta(hours=2)) == next_date

    def test_edge_weekday(self):
        '''Test time delta overnight just barely within range.'''
        date = datetime(year=2016, month=7, day=18, hour=util.CLOSING_HOUR - 1, minute=59,
                        second=59, tzinfo=util.LOCAL_TZ)
        td = timedelta(seconds=1)
        after = datetime(year=2016, month=7, day=19, hour=util.OPENING_HOUR,
                         tzinfo=util.LOCAL_TZ)
        assert util.get_expiration_time(date, td) == after

    def test_next_weekend(self):
        '''Test time delta over a weekend.'''
        date = datetime(year=2016, month=7, day=15, hour=util.CLOSING_HOUR - 1,
                        tzinfo=util.LOCAL_TZ)
        next_date = datetime(year=2016, month=7, day=18, hour=util.OPENING_HOUR + 1,
                             tzinfo=util.LOCAL_TZ)
        assert util.get_expiration_time(date, timedelta(hours=2)) == next_date

    def test_edge_weekend(self):
        '''Test time delta over a weekend just barely within range.'''
        date = datetime(year=2016, month=7, day=15, hour=util.CLOSING_HOUR - 1, minute=59,
                        second=59, tzinfo=util.LOCAL_TZ)
        td = timedelta(seconds=1)
        after = datetime(year=2016, month=7, day=18, hour=util.OPENING_HOUR,
                         tzinfo=util.LOCAL_TZ)
        assert util.get_expiration_time(date, td) == after


from struct import pack, unpack
import sys
import os
import time
# import socks
from bjdns import geventsocks
import json
from gevent.server import DatagramServer
import re
from gevent import socket
# from gevent import monkey
# monkey.patch_socket()

server = None
google_ip = '64.233.162.83'
cdn_list = {}
google = {}
ad = {}
cache = {}
listen_addr = ()
dns_cn_addr = ()
dns_foreign_addr = ()

def inlist(name, dict_):
	name = name.split('.')
	name.reverse()
	for i in range(1,len(name)+1):
		name_ = list(reversed([ name[j] for j in range(i) ]))
		name_ = '.' + '.'.join(name_)
		if dict_.get(name_):
			return True
	else:
		return False

def get_data(data,cdn=0):
	# data = pack('>H', len(data)) + data
	# s    = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s    = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
	# s.settimeout(2)
	s.sendto(data, dns_cn_addr)
	data = s.recv(512)
	return data
	# s.connect(('114.114.114.114', 53))
	# s.send(data)
	# res  = s.recv(512)
	# return res[2:]

def get_data_by_tcp(data):
	# print('get_data_by_tcp')
	data = pack('>H', len(data)) + data
	# s    = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	# s = geventsocks.socksocket()
	s = socket.socket()
	geventsocks.connect(s, dns_foreign_addr)
	s.send(data)
	# res = b''
	# for buf in iter(lambda:s.recv(1024),b''):
	# 	print('buf: ', buf)
	res = s.recv(512)
	length = unpack('>H', res[:2])[0]
	# print(length)
	if length <= len(res) - 2:
		pass
	else:
		while len(res) - 2 < length:
			res += s.recv(512)
	s.close()
	# print('get_data_by_tcp over')
	return res[2:]

def make_data(data, ip):
	#data即请求包
	(id, flags, quests,
	 answers, author, addition) = unpack('>HHHHHH', data[0:12])
	flags_new   = 33152
	answers_new = 1
	res         = pack('>HHHHHH', id, flags_new, quests,
					   answers_new, author, addition)

	res        += data[12:]

	dns_answer  = {
		'name':49164,
		'type':1,
		'classify':1,
		'ttl':600,
		'datalength':4
		}
	res        += pack('>HHHLH', dns_answer['name'], dns_answer['type'],
					  dns_answer['classify'], dns_answer['ttl'],
					  dns_answer['datalength'])

	ip          = ip.split('.')
	ip_bytes    = pack('BBBB', int(ip[0]), int(ip[1]),
					int(ip[2]), int(ip[3]))
	res        += ip_bytes

	return res

def get_ip_from_resp(res, data_len):
	res = res[data_len:]
	p = re.compile(b'\xc0.\x00\x01\x00\x01')
	# try:
	res = p.split(res)[1]
	# except IndexError:
	# 	raise Exception('get ip fail')
	# 	return
	ip_bytes   = unpack('BBBB',res[6:10])
	ip         =  '.'.join( [ str(i) for i in ip_bytes ] )
	return ip

# def get_ip(data, name):
# 	resp = get_data_by_tcp(data)
# 	ip = get_ip_from_resp(resp, len(data))
# 	return ip

def eva(data, client):
	list_iter = iter(data[13:])
	name      = ''
	for bit in iter(lambda: next(list_iter), 0):
		name += '.' if bit < 32 else chr(bit)

	type = unpack('>H',data[14+len(name):16+len(name)])
	type = type[0]
	if not type:
		server.sendto(get_data(data), client)
		return

	if name in cache:
		ip = cache[name]
		print(client[0],
			  '[{}]'.format(time.strftime('%Y-%m-%d %H:%M:%S')),
			  '[cache]', name, ip)
		server.sendto(make_data(data, ip), client)
		if inlist(name, cdn_list):
			res = get_data(data,cdn=1)
			ip_new = get_ip_from_resp(res, len(data))
		else:
			resp = get_data_by_tcp(data)
			ip_new = get_ip_from_resp(resp, len(data))
		if ip != ip_new:
			cache[name] = ip_new

	elif inlist(name, ad):
		ip = '127.0.0.1'
		print( client[0],
			   '[{}]'.format(time.strftime('%Y-%m-%d %H:%M:%S')),
			   '[ad]', name, ip)
		server.sendto(make_data(data, ip), client)

	elif inlist(name, google):
		ip = google_ip
		print( client[0],
			   '[{}]'.format(time.strftime('%Y-%m-%d %H:%M:%S')),
			   '[google]', name, ip)
		server.sendto(make_data(data, ip), client)

	elif inlist(name, cdn_list):
		print(client[0],
			  '[{}]'.format(time.strftime('%Y-%m-%d %H:%M:%S')),
			  '[cdn]', name,)
		# try:
		res = get_data(data,cdn=1)
		# except socket.timeout as e:
		# 	print(e)
		# 	return
		server.sendto(res, client)
		ip = get_ip_from_resp(res, len(data))
		cache[name] = ip

	else:
		# ip = get_ip(data, name)
		resp = get_data_by_tcp(data)
		ip = get_ip_from_resp(resp, len(data))
		server.sendto(make_data(data,ip), client)
		print(client[0],
			  '[{}]'.format(time.strftime('%Y-%m-%d %H:%M:%S')),
			  name, ip)
		cache[name] = ip

def main():
	global server, google_ip, cache, cdn_list, google, ad
	global dns_cn_addr, dns_foreign_addr

	cdn_list = { x:True for x in open('bjdns/cdnlist.txt','r').read().split('\n') if x}

	google = { x:True for x in open('bjdns/google.txt','r').read().split('\n') if x}
	ad = { x:True for x in open('bjdns/ad.txt','r').read().split('\n') if x} if os.path.isfile('ad.txt') else {}

	if len(sys.argv) >= 2:
		json_dir = sys.argv[1]
	else:
		json_dir = 'bjdns/bjdns.json'
	json_str = open(json_dir).read()
	json_dict = json.loads(json_str)
	ss_ip, ss_port = json_dict['socks5_server'].split(':')
	listen_addr = (json_dict['listen_ip'], json_dict['listen_port'])
	dns_cn_addr = (json_dict['dns_cn_ip'], json_dict['dns_cn_port'])
	dns_foreign_addr = (json_dict['dns_foreign_ip'], json_dict['dns_foreign_port'])
	geventsocks.set_default_proxy(ss_ip, int(ss_port))

	if os.name == 'nt':
		# adem()
		# exit()

		import threading
		from tkinter import Tk, Menu#,messagebox

		def adem_thread(s):
			while 1:
				try:
					data, client = s.recvfrom(512)
					threading.Thread(target=eva,args=(data, client,)).start()
				except ConnectionResetError:
					pass

		def menu_func(event, x, y):
			if event == 'WM_RBUTTONDOWN':	# Right click tray icon, pop up menu
				menu.tk_popup(x, y)

		def quit():
			root.quit()
			root.destroy()
			sys.exit()

		s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
		s.bind( listen_addr )

		root = Tk()
		root.tk.call('package', 'require', 'Winico')
		icon = root.tk.call('winico', 'createfrom', os.path.join(os.getcwd(), 'py.ico'))	# New icon resources
		root.tk.call('winico', 'taskbar', 'add', icon,
					 '-callback', (root.register(menu_func), '%m', '%x', '%y'),
					 '-pos',0,
					 '-text','bjdns')
		menu = Menu(root, tearoff=0)
		menu.add_command(label='退出', command=quit)

		root.withdraw()
		t = threading.Thread(target=adem_thread, args=(s,))
		t.setDaemon(True)
		t.start()
		root.mainloop()

	else:
		server = DatagramServer(listen_addr, eva)
		server.serve_forever()

if __name__ == '__main__':
	main()


import cv2
import numpy as np

def nothing(*arg):
    pass

if __name__ == '__main__':
    # cap = cv2.VideoCapture(0)
    cap = cv2.VideoCapture("C:/Users/xiaoi/Pictures/Camera Roll/001.mp4")
    cap.set(3, 3840)
    cap.set(4, 2160)
    image = cv2.imread("image/WIN_20191107_11_18_12_Pro.jpg", 0)
    image = cv2.resize(image, (960, 540))

    # 膨胀
    r = 1
    max_r = 100
    cv2.namedWindow("dilate", 1)
    cv2.createTrackbar("r", "dilate", r, max_r, nothing)
    while 1:
        ok, frame = cap.read()
        image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        image = cv2.resize(image, (960, 540))
        d = image
        r = cv2.getTrackbarPos("r", "dilate")
        s = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * r + 1, 2 * r + 1))

        # d = cv2.erode(d, s)  # 腐蚀
        # d = cv2.dilate(d, s)  # 膨胀
        #
        # # 开运算
        # d = cv2.erode(d, s)
        # d = cv2.dilate(d, s)
        #
        # # # 闭运算
        # d = cv2.dilate(d, s)
        # d = cv2.erode(d, s)

        # # 顶帽运算
        # d = cv2.erode(d, s)
        # d = cv2.dilate(d, s)
        # d = image-d

        # 底帽运算
        # d = cv2.dilate(d, s)
        # d = cv2.erode(d, s)
        # d = image - d

        # 形态学边缘检测（形态学梯度）
        d = cv2.erode(d, s)
        d = cv2.dilate(image, s) - d

        cv2.imshow("dilate", d)
        if cv2.waitKey(5) == 27:
            break
    cv2.destroyAllWindows()