# -*- coding: utf-8 -*
from __future__ import unicode_literals, print_function
import django

django.setup()

from django.db import transaction
import sys
import os
from basics.models import *
from dictionary_functions import simple_dictionary, multi_option_dictionary, multi_option_dictionary_v_always_l, word_pair_dictionary
from standard_word_filter import l_part_of_speech_dics, create_part_of_speech_strings_with_persons_and_wiki_titles
from download_plenarprotokolle import download_latest_plenarprotokolle, get_last_pdf_legnum_docnum
from fun_with_xml import transform_pdf
from commons import clean_text_add_spaces, get_d_gen_pos_freq
from analyze_statements import create_file_of_general_word_freqs, create_file_of_speaker_function_party_stmts_counts, get_l_significant_words, map_words_to_texts
from basics.views_helper import get_l_speaker_distances
from standard_word_filter import get_standardized_word_pairs_from_text

d_first_names = simple_dictionary('first_names.txt')
d_last_names = multi_option_dictionary('last_names_short_full.txt')
d_nouns, d_adjectives, d_verbs, d_partizip_i = l_part_of_speech_dics()
d_wiki_titles = multi_option_dictionary_v_always_l('wiki_titles_with_synonyms_first_word_full_form_reduced.txt')
set_no_first_first_names = {'Bundeskanzlerin', 'Kanzlerin', u'Präsidentin', 'Ministerin', 'Umweltministerin',
                            'Abgeordnete', 'Familienministerin', 'Finanzministerin', 'Bildungsministerin',
                            'Verkehrsministerin', 'Arbeitsministerin', 'Außenministerin', 'Premierministerin',
                            'Verteidigungsministerin', 'Innenministerin', u'Ministerpräsidentin', 'Bundeskanzler',
                            'Kanzler', u'Präsident', 'Minister', 'Umweltminister', 'Abgeordneter', 'Familienminister',
                            'Finanzminister', 'Bildungsminister', 'Verkehrsminister', 'Arbeitsminister',
                            'Außenminister', 'Premierminister', 'Verteidigungsminister', 'Innenminister',
                            u'Ministerpräsident', 'Nur', 'Premierminister', 'Premierministerin', 'Herr', 'Frau',
                            'Vizepräsident', 'Vizepräsidentin', 'Kollege', 'Kollegin'}

def make_functions_gender_neutral(function):
    function += ' '
    function = function.replace('im Bundesministerium', 'bei dem Bundesminister').replace('Staatssekretär ', 'Staatssekretär/in ').replace('Staatssekretärin', 'Staatssekretär/in').replace('räsident', 'räsident/in').replace('räsident/inin', 'räsident/in').replace('eauftragter', 'eauftragte/r').replace('eauftragte ', 'eauftragte/r ').replace('Senatorin', 'Senator/in').replace('Senator ', 'Senator/in ').replace('anzler ', 'anzler/in ').replace('anzlerin', 'anzler/in').replace('Chef ', 'Chef/in ')

    function = function.replace('inister ', 'inister/in ').replace('inisterin', 'inister/in').replace('bei der', 'bei der/bei dem').replace('beim', 'bei der/bei dem')

    return function.strip().replace('tärbei', 'tär bei').replace('BMAS', ' Bundesminister/in für Arbeit und Soziales').replace('Bundestags', 'Bundestages').replace('/in.', '/in').replace('Baden Württemberg', 'Baden-Württemberg')

def standardize_functions(function):

    function = function.replace(u'Staatsminister/in für Europa', u'Staatsminister/in für Europa im Auswärtigen Amt')

    l_functions_standard_function = [
        ([u'Beauftragte/r der Bundesregierung für die Belange behinderter Menschen',
         u'Beauftragte/r der Bundesregierung für die Belange der Behinderten',
         u'Beauftragte/r der Bundesregierung für die Belange von Menschen mit Behinderungen'],
        u'Beauftragte/r der Bundesregierung für die Belange von Menschen mit Behinderungen'),
        ([u'für Arbeit und Soziales', u'für Arbeit und Sozialordnung'], u'für Arbeit und Soziales'),
        ([u'für Bildung', u'Bundesminister/in für Bildung und Forschung', u'für Bildung und Wissenschaft'], u'für Bildung und Forschung'),
        ([u'für Ernährung und Landwirtschaft', u'für Ernährung'], u'für Ernährung und Landwirtschaft'),
        ([u'für Familie', u'für Familie und Senioren'], u'für Familie'),
        ([u'für wirtschaftliche Zusammenarbeit', u'für wirtschaftliche Zusammenarbeit und Entwicklung'], u'für wirtschaftliche Zusammenarbeit und Entwicklung')
        ]

    return function

def get_document_fields(document_path, stmt_block_no):

    d_doc = {}
    d_word_pairs = word_pair_dictionary()

    import codecs

    with codecs.open(document_path, 'r', encoding="utf-8") as the_file:

        l_lines = the_file.readlines()
        l_lines = [line.strip() for line in l_lines if line and line != u'\n']

    file_name = document_path.split('/')[-1]

    d_doc['date'] = l_lines[0]
    d_doc['legislative_period'] = file_name[:2]
    d_doc['document_id'] = int(file_name.split('.')[0])
    d_doc['url'] = ''.join(['http://dip21.bundestag.de/dip21/btp/', str(d_doc['legislative_period']), '/', str(d_doc['document_id']), '.pdf'])
    print(d_doc['url'])

    last_speaker = ''
    for order_id, line in enumerate(l_lines[1:]):
        d_doc[order_id] = {}
        try:
            d_doc[order_id]['page'] = int(line.split('+page')[1])
        except IndexError:
            print(line)
            return 6

        raw_speaker = line.split('|')[1].replace('speaker', '')

        speaker = raw_speaker.split('(')[0].split(',')[0]
        d_doc[order_id]['speaker'] = speaker
        if speaker != last_speaker:
            stmt_block_no += 1
        d_doc[order_id]['stmt_block_no'] = stmt_block_no

        try:
            if raw_speaker.count('(') == 2 and not "(spricht von seinem Platz aus)" in raw_speaker:
                d_doc[order_id]['party'] = raw_speaker.split('(')[2].replace(')', '')
            else:
                d_doc[order_id]['party'] = raw_speaker.split('(')[1].replace(')', '')
            if "BÜNDNIS 90/" in d_doc[order_id]['party']:
                d_doc[order_id]['party'] = "BÜNDNIS 90/DIE GRÜNEN"
            elif d_doc[order_id]['party'] in ["Niedersachsen", "Thüringen", "Sachsen", "Rheinland-Pfalz", "Hamburg", "Brandenburg", "Schleswig-Holstein", "Baden-Württemberg", "Nordrhein-Westfalen", "Berlin", "Bremen", "Bayern", "Saarland", "Mecklenburg-Vorpommern", "Sachsen-Anhalt", "Hessen", "Baden Württemberg"]:
                d_doc[order_id]['party'] = ''
        except IndexError:
            d_doc[order_id]['party'] = ''

        d_doc[order_id]['party'] = d_doc[order_id]['party'].strip().replace('DIE LINKE.', 'DIE LINKE')
        if d_doc[order_id]['party'] in ['CSU', 'CSU/CSU']:
            d_doc[order_id]['party'] = 'CDU/CSU'
        if d_doc[order_id]['party'] in ['PDS', 'PDS/LL', 'PDS/Linke Liste', 'LINKE', 'DIE LINKE']:
            d_doc[order_id]['party'] = 'DIE LINKE'

        try:
            if u'Parl. Staatssekretär' in raw_speaker:
                function = u'Parl. Staatssekretär' + raw_speaker.split(u', Parl. Staatssekretär')[1]
                d_doc[order_id]['speaker'] = d_doc[order_id]['speaker'].split(u'Parl. Staatssekretär')[0].replace(',', '').strip()
            else:
                function = raw_speaker.split(',')[1]
        except IndexError:

            d_doc[order_id]['function'] = ''

            first_word = raw_speaker.split()[0]
            if 'räsident' in first_word or 'Senator' in first_word:
                function = first_word
                d_doc[order_id]['speaker'] = d_doc[order_id]['speaker'].replace(first_word, '').strip()
                d_doc[order_id]['party'] = ''
            else:
                function = 'Abgeordnete/r'
        if 'minister' in d_doc[order_id]['party']:
            d_doc[order_id]['function'] = d_doc[order_id]['party']
            d_doc[order_id]['party'] = ''
        d_doc[order_id]['function'] = make_functions_gender_neutral(function).strip().replace('Parl. Staatssekretär/in bei der/bei dem Bundesminister/in der Justiz und Verbraucherschutz', 'Parl. Staatssekretär/in bei der/bei dem Bundesminister/in der Justiz und für Verbraucherschutz').replace('Bundesministerium ', 'Bundesminister/in ').replace(u'für Justiz', u'der Justiz').replace(u'Bundesminister/in für Verteidigung', u'Bundesminister/in der Verteidigung').replace('))', ')').replace('Parlamentarischer', 'Parl.').replace(u'Bundesminister/in Wirtschaft und Technologie', u'Bundesminister/in für Wirtschaft und Technologie')

        text = line.split('|')[-1].split('+page')[0]
        d_doc[order_id]['text'] = text
        d_doc[order_id]['str_word_pairs'] = get_standardized_word_pairs_from_text(text, d_word_pairs)
        d_doc[order_id]['cleaned_text'] = clean_text_add_spaces(text)
        d_doc[order_id]['str_adjectives'], d_doc[order_id]['str_nouns'], d_doc[order_id]['str_verbs'], d_doc[order_id]['str_partizip_i'], d_doc[order_id]['str_other_words'], d_doc[order_id]['str_persons'], d_doc[order_id]['str_titles'], d_doc[order_id]['no_unique_words'] = create_part_of_speech_strings_with_persons_and_wiki_titles(text, d_nouns, d_verbs, d_adjectives, d_partizip_i, d_first_names, d_last_names, set_no_first_first_names, d_wiki_titles)
    return d_doc, stmt_block_no

def get_txt_file_path(txt_directory, leg_num, docnum):

    file_path = '/'.join([txt_directory, str(leg_num).zfill(2), ''.join([str(leg_num).zfill(2), str(docnum).zfill(3), '.txt'])])

    if not os.path.exists(file_path):
        return False

    return file_path

def get_latest_leg_period_doc_num_in_db():
    try:
        latest_document_in_db = list(Document.objects.all().order_by("document_id"))[-1]

    except IndexError:
        return 12, 0
    latest_document_in_db_id = str(latest_document_in_db.document_id)
    docnum = latest_document_in_db_id[2:]
    leg_num = latest_document_in_db_id[:2]

    return int(leg_num), int(docnum)

def txts_to_db(l_txt_file_paths, stmt_block_no):

    for file_path in l_txt_file_paths:

        print('trying to put file to db:', file_path)

        d_doc, stmt_block_no = get_document_fields(file_path, stmt_block_no)

        # print(d_doc.get('document_id'))
        [doc, __] = Document.objects.get_or_create(
            legislative_period=d_doc.get("legislative_period", -1),
            document_id=d_doc.get("document_id", -1),
            date=d_doc.get("date", None).replace("/", "-"),
            url=d_doc.get("url", None)
        )

        with transaction.atomic():
            for order_id in d_doc:
                #print(order_id)
                try:
                    int(order_id)
                except ValueError:
                    continue

                e = d_doc[order_id]

                [party,__] = Party.objects.get_or_create(
                    name="unknown",
                    abbrev=e.get("party"),
                    significant_words='',
                )

                [func, __] = Function.objects.get_or_create(
                    name=e.get('function'),
                )
                # print(party, func)
                [speaker, __] = Speaker.objects.get_or_create(
                    name=e.get("speaker"),
                    party=party,
                    function=func,
                    significant_words=''
                )

                [stmt_block_no0, __] = StmtBlock.objects.get_or_create(
                    stmt_block_no=e.get('stmt_block_no'),
                    speaker=speaker,
                    document=doc,
                )

                RegularStatement.objects.create(
                    text=e.get("text"),
                    str_adjectives=e.get('str_adjectives'),
                    str_nouns=e.get('str_nouns'),
                    str_verbs=e.get('str_verbs'),
                    str_partizip_i=e.get('str_partizip_i'),
                    str_other_words=e.get('str_other_words'),
                    str_persons=e.get('str_persons'),
                    str_titles=e.get('str_titles'),
                    str_word_pairs=e.get('str_word_pairs'),
                    order_id=order_id,
                    document=doc,
                    speaker=speaker,
                    page=e.get('page'),
                    cleaned_text=e.get('cleaned_text'),
                    stmt_block_no=stmt_block_no0,
                    no_unique_words=e.get('no_unique_words'),
                )

def add_statistics():

    Statistics.objects.create(
        no_plenarprotokolle=Document.objects.all().count(),
        no_speakers=Speaker.objects.all().count(),
        no_functions=Function.objects.all().count(),
        no_parties=Party.objects.all().count(),
    )

def add_significant_words_to_speaker():

    d_gen_pos_freq_dic = get_d_gen_pos_freq()

    for speaker in Speaker.objects.all():
        # print(speaker.name)
        stmts = RegularStatement.objects.filter(speaker=speaker)
        speaker.significant_words = '|'.join(get_l_significant_words(stmts, d_gen_pos_freq_dic))
        speaker.save(update_fields=["significant_words"])

def add_word_pairs_to_statements():
    print(add_word_pairs_to_statements)
    for leg_period in xrange(12, 19):
        print(leg_period)
        stmts = RegularStatement.objects.filter(**{'document__legislative_period': leg_period})
        for stmt in stmts:
            text = stmt.text
            set_word_pairs = get_standardized_word_pairs_from_text(text)

            stmt.str_word_pairs = '|'.join(set_word_pairs)
            #print(stmt.str_word_pairs)
            stmt.save(update_fields=["str_word_pairs"])

#add_word_pairs_to_statements()

'''def mark_statement_relevance():

    for stmt in RegularStatement.objects.all():
        if (u'Präsident' in stmt.speaker.function or u'Vizepräsident' in stmt.speaker.function) and not 'Bundesrat' in stmt.speaker.function:
            for i in ['a)', 'b)', 'c)', 'd)', 'e)', 'f)', 'g)']
            if 'usschuss' in stmt or stmt.text.count('Dr.') > 4:
                stmt.relevant = False
                stmt.save(update_fields=["relevant"])
                continue
        stmt.relevant = False
        stmt.save(update_fields=["relevant"])'''

def add_significant_words_to_parties():

    d_gen_pos_freq_dic = get_d_gen_pos_freq()
    for party in Party.objects.all():
        # print(speaker.name)
        stmts = RegularStatement.objects.filter(speaker__party=party)
        party.significant_words = '|'.join(get_l_significant_words(stmts, d_gen_pos_freq_dic))
        party.save(update_fields=["significant_words"])

def add_data_suggested_party_to_speaker():

    for speaker in Speaker.objects.all():
        l_no_shared_words_speaker, l_normalized_shares_parties = get_l_speaker_distances(speaker)
        if l_normalized_shares_parties:
            speaker.data_suggested_party = l_normalized_shares_parties[0][1]
            # print(speaker, l_normalized_shares_parties)
            speaker.save(update_fields=["data_suggested_party"])

def update_database(transform_pdfs, update_stats, pdf_transformation_start):

    txt_directory = os.path.dirname(os.path.realpath(sys.argv[0])).replace('federalday/tools',
                                                                   'app/txts/plenarprotokolle')

    new_documents_downloaded = False
    if download_latest_plenarprotokolle():
        new_documents_downloaded = True
    latest_pdf_legnum, latest_pdf_docnum = get_last_pdf_legnum_docnum()
    latest_leg_num_in_db, latest_docnum_in_db = get_latest_leg_period_doc_num_in_db()
    print(latest_leg_num_in_db, latest_docnum_in_db)

    leg_nums_to_db = xrange(latest_leg_num_in_db, latest_pdf_legnum + 1)
    # print(leg_nums_to_db, 99999999)
    stmt_block_no = 0
    for leg_num in leg_nums_to_db:
        l_txt_file_paths = []
        # print(leg_num, latest_leg_num_in_db, 'leg_num')
        if leg_num == latest_leg_num_in_db:
            # print(777)
            start_docnum = latest_docnum_in_db + 1
        else:
            # print(888)
            start_docnum = 1

        for docnum in xrange(start_docnum, 400):
            # print(docnum, leg_num, 'docnum989898')
            pdf_file_name = ''.join([str(leg_num).zfill(2), str(docnum).zfill(3), '.pdf'])

            if transform_pdfs and str(leg_num) + str(docnum).zfill(3) > pdf_transformation_start:
                print('working on pdf', pdf_file_name)
                if transform_pdf(pdf_file_name):
                    pass
                else:
                    break

            file_path = get_txt_file_path(txt_directory, leg_num, docnum)
            if not file_path:
                break
            # print(file_path, 33333)
            l_txt_file_paths.append(file_path)
        # print(l_txt_file_paths)
        #print(l_txt_file_paths)
        txts_to_db(l_txt_file_paths, stmt_block_no)

    if transform_pdfs or new_documents_downloaded or update_stats:

        print('doing statistics')
        add_statistics()
        print('creating file of general word freqs')
        create_file_of_general_word_freqs()
        print('creating file of speaker func party stmt counts')
        create_file_of_speaker_function_party_stmts_counts()
        print('adding significant words to speaker')
        add_significant_words_to_speaker()
        print('adding data suggested party to speaker')
        add_data_suggested_party_to_speaker()
        print('deleting saved search data')
        PickleCache.objects.all().delete()
        print('mapping words to texts')
        map_words_to_texts()

#update_database(False, True, '0')

#add_data_suggested_party_to_speaker()

#add_significant_words_to_parties()

#PickleCache.objects.all().delete()
#print(7)

def delete_all_tables():
    from django.db import connection
    with connection.cursor() as cursor:
        for table in ['basics_document', 'basics_function', 'basics_party', 'basics_picklecache', 'basics_regularstatement', 'basics_speaker', 'basics_statistics', 'basics_stmtblock', 'basics_wordmap', 'django_admin_log', 'django_content_type', 'django_migrations', 'django_session', 'auth_group', 'auth_group_permissions', 'auth_permission', 'auth_user', 'auth_user_groups', 'auth_user_user_permissions']:
            cursor.execute("DROP TABLE  IF EXISTS %s CASCADE" % table)

#delete_all_tables()
#print(get_latest_leg_period_doc_num_in_db())


import gulp
import os, sys, random
from colored import fg, bg, attr

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.express as px

import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error
# ase API
from ase import Atoms
# quippy API
from quippy.potential import Potential

print()
print(f'{fg(15)} {bg(5)} Visualisation {attr(0)}')
print()
print()

binwidth = 0.05
sig2 = 0.005

Arg = sys.argv
EIGVEC = Arg[1]
STEP = int(Arg[2])
RANK_from = int(Arg[3])
RANK_to = int(Arg[4])
Breath = Arg[5]
cutoff= float(Arg[6])
sparse = int(Arg[7])

EIGVEC = EIGVEC.split()
_EIGVEC = '-'.join(EIGVEC)

wd_name = f'GAP_{_EIGVEC}_{STEP}_{RANK_from}-{RANK_to}_{Breath}_{cutoff}_{sparse}'

GULP = gulp.GULP(STEP, EIGVEC, SP='set')

cwd = os.getcwd()
wd = [os.path.join(cwd, x) for x in os.listdir('./') if os.path.isdir(x) and wd_name in x]
try:
    full_wd = wd[0]
except IndexError:
    print("WARNING: Probably you didn't run {gap_1} and {gap_2}")
    sys.exit()

ext_fpath = os.path.join(full_wd, 'ext_movie.xyz')
FIT_dir_path = os.path.join(full_wd, 'FIT')
FIT_dir_path = os.path.join(full_wd, 'FIT')

Training_xyz_path = os.path.join(FIT_dir_path, 'Training_set.xyz')
Valid_xyz_path = os.path.join(FIT_dir_path, 'Valid_set.xyz')

#############################################################
# prepare dimer curve data using the Interatomic  potential #
#############################################################
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)

print(f"{fg(1)} Al-F GAP pairwise interaction {attr(0)}")
dimers = [Atoms("AlF", positions=[[0,0,0], [x, 0,0]]) for x in np.arange(0.0, 6.02, 0.02)] #int(cutoff)+1.02, 0.02)]
print(full_wd)
pot = Potential('IP GAP', param_filename='%s/FIT/GAP.xml' % (full_wd))
dimer_curve = []
for dim in dimers:
    dim.set_calculator(pot)
    dimer_curve.append(dim.get_potential_energy())
x_axis = np.array([dim.positions[1,0] for dim in dimers])
y_axis = np.array(dimer_curve)
data = np.c_[x_axis, y_axis]
output_list = data.tolist()
df = pd.DataFrame (output_list, columns = ['r', 'Al-F(GAP)'])
del dim, dimers, dimer_curve, x_axis, y_axis, data, output_list

# Al-F scaled to match with scaled F-F
dimers_AlF_scaled = [Atoms("AlF", positions=[[0,0,0], [round(x/np.sqrt(3), 3),0,0]]) for x in np.arange(0.0, 6.02, 0.02)]
dimer_curve_AlF_scaled = []
for dim_AlF_scaled in dimers_AlF_scaled:
    dim_AlF_scaled.set_calculator(pot)
    dimer_curve_AlF_scaled.append(dim_AlF_scaled.get_potential_energy())
x_axis_AlF_scaled = np.array([dim_AlF_scaled.positions[1,0] for dim_AlF_scaled in dimers_AlF_scaled])
y_axis_AlF_scaled = np.array(dimer_curve_AlF_scaled)
data_AlF_scaled = np.c_[x_axis_AlF_scaled, y_axis_AlF_scaled]
output_list_AlF_scaled = data_AlF_scaled.tolist()
df_AlF_scaled = pd.DataFrame (output_list_AlF_scaled, columns = ['r_scaled', 'Al-F(GAP)_scaled'])
del dim_AlF_scaled, dimers_AlF_scaled, dimer_curve_AlF_scaled, x_axis_AlF_scaled, y_axis_AlF_scaled, data_AlF_scaled, output_list_AlF_scaled
print()

print(f"{fg(3)} Al-Al GAP  pairwise interaction {attr(0)}")
dimers = [Atoms("AlAl", positions=[[0,0,0], [x, 0,0]]) for x in np.arange(0.0, 6.02, 0.02)]
dimer_curve = []
for dim in dimers:
    dim.set_calculator(pot)
    dimer_curve.append(dim.get_potential_energy())
x_axis = np.array([dim.positions[1,0] for dim in dimers])
y_axis = np.array(dimer_curve)
data = np.c_[x_axis, y_axis]
output_list = data.tolist()
dff = pd.DataFrame (output_list, columns = ['x2', 'Al-Al(GAP)'])
del dimers, dimer_curve, x_axis, y_axis, data, output_list
print()

print(f"{fg(2)} F-F GAP pairwise interaction {attr(0)}")
dimers = [Atoms("FF", positions=[[0,0,0], [x,0,0]]) for x in np.arange(0.0, 6.02, 0.02)]
dimer_curve = []
for dim in dimers:
    dim.set_calculator(pot)
    dimer_curve.append(dim.get_potential_energy())
x_axis = np.array([dim.positions[1,0] for dim in dimers])
y_axis = np.array(dimer_curve)
data = np.c_[x_axis, y_axis]
output_list = data.tolist()
dfff = pd.DataFrame (output_list, columns = ['x3', 'F-F(GAP)'])
del dimers, dimer_curve, y_axis, data, output_list
print()

df = df.join(dff)
df = df.join(dfff)
df = df.join(df_AlF_scaled)
df.drop(columns=['x2', 'x3'], inplace=True)
del dff, dfff, df_AlF_scaled

print('\n\n\nPlotting the pairwise interaction energy as a function of interatomic distances...\n\n\n')

###################################################
# calculating Interatomic distance and binning it #
###################################################
with open(Training_xyz_path, 'r') as f:
    lines = f.readlines()
no_of_atoms = int(lines[0])
lines = [x for x in lines if len(x) > 10 and 'Properties' not in x][:-2]

coordinates = []
ID = []
for i in lines:
    line = [float(x) for x in i.split()[1:]][:3]
    coordinates += line
    ID.append( i.split(' ')[0])
del lines

From = []
To = []
with open(ext_fpath, 'r') as f:
    lines = f.readlines()
for numi, i in enumerate(lines):
    if len(i) <= 10:
        From.append(numi)
        To.append(numi)

To.append(len(lines))
To = To[1:]

block = {From[i]: To[i] for i in range(len(From))}
del From, To
keys_list = list(block.keys())  # shuffle() wants a list
random.shuffle(keys_list)       # randomize the order of the keys

nkeys_80 = int(1.0 * len(keys_list))  # how many keys does 80% equal
keys_80 = keys_list[:nkeys_80]
keys_20 = keys_list[nkeys_80:]
del nkeys_80

train_80 = {k: block[k] for k in keys_80}
valid_20 = {k: block[k] for k in keys_20}
del keys_80, keys_20

coordinates = np.asarray(coordinates)
coordinates = np.reshape(coordinates, (len(train_80.keys()), no_of_atoms, 3))
ID = np.asarray(ID)
ID = np.reshape(ID, (len(train_80.keys()), no_of_atoms))

all_het_dist = []
all_homo_dist = []

for numi, i in enumerate(coordinates):
    nbins, npairs_all, npairs_het, npairs_homo, all_dist, het_dist, homo_dist, opdata = GULP.RDF(numi+1, no_of_atoms, i, ID[numi], binwidth, sig2)
    all_het_dist += het_dist
    all_homo_dist += homo_dist

#################################
# Plotting Born-Mayer, GAP, RDF #
#################################
# E vs r
fig = go.FigureWidget()

# Generated Born-Mayer potential
def BM(x):
    return 3760*np.exp(-x/0.222)

def deriv_BM(x):
    return -1880000*np.exp(-500*x/111)/111

def buck4(x):           # 2.73154 Å F-F distance
    if x.all() < 2.0:
        return 1127.7*np.exp(-x/0.2753)
    elif 2.0 <= x.all() < 2.726:
        return -3.976*x**5 + 49.0486*x4 -241.8573*x**3+ 597.2668*x**2 -741.117*x + 371.2706
    elif 2.726 <= x.all() < 3.031:
        return -0.361*x**3 +  3.2362*x**2 -9.6271*x +9.4816
    elif x.all() >= 3.031:
        return -15.83/P3035**6

# Born-Mayer Al-F potential
BM_color = 'rgb(10, 120, 24)'
df['Al-F(BM)'] = BM(x_axis)

trace1 = fig.add_scatter(x=x_axis, y=BM(x_axis), mode='lines', name='Al-F Born-Mayer',\
        line = dict(shape='linear', color=BM_color, dash='dot')
        )
# Buckingham 4-region F-F potential
df['F-F(buck4)'] = buck4(x_axis)
df.to_csv(f'{full_wd}/GAP_pot_tabulated.csv', index=False)

trace2 = fig.add_scatter(x=x_axis, y=buck4(x_axis), mode='lines', name='F-F Buck4',\
        line = dict(shape='linear', color='firebrick', dash='dot')
        )

#####################
### GAP potential ###
#####################
# Al-F
trace3 = fig.add_scatter(x=df['r'], y=df[df.columns[1]], mode='lines', name='Al-F GAP potential',\
        line = dict(shape='linear', color = BM_color))#, secondary_y=False,)

## Al-F (same results but the data points' x-axis is match with F-F scaled)
#trace_ = fig.add_scatter(x=df['r_scaled'], y=df['Al-F(GAP)_scaled'], mode='lines', name='Al-F GAP potential_2',\
#        line=dict(shape='linear', color=BM_color))

# F-F original
trace4 = fig.add_scatter(x=df['r'], y=df['F-F(GAP)'], mode='lines', name='F-F GAP potential',\
line = dict(shape = 'linear', color = 'firebrick')) # , secondary_y=False,)

# F-F scaled
scaling = df['r'].tolist()
scaling = [round(x/np.sqrt(3), 3) for x in scaling]
trace5 = fig.add_scatter(x=df['r_scaled'], y=df['F-F(GAP)'], mode='lines', name='F-F GAP potential_scaling', visible='legendonly')
df.drop(columns=['Al-F(BM)', 'Al-Al(GAP)'], inplace=True)

# Al-F + F-F
df['GAP_sum'] = df['F-F(GAP)'] + df['Al-F(GAP)_scaled']
df.to_csv('df_gap_sum.csv')
trace6 = fig.add_scatter(x=df['r_scaled'], y=df['GAP_sum'], mode='lines', name='sum(GAP)', visible='legendonly')

## MSE (± 0.5 from the equilibrium interatomic distnace)
from_point = list(df['r']).index([x for x in df['r'] if 1.57705-0.5 < x][0])
to_point = list(df['r']).index([x for x in df['r'] if x < 1.57705+0.5][-1])
MSE_catan = round(mean_squared_error(df['Al-F(GAP)'][from_point:to_point], BM(x_axis)[from_point:to_point], squared=False), 4)
from_point_a = list(df['r']).index([x for x in df['r'] if 2.73154-0.5 < x < 2.73154+0.5][0])
to_point_a = list(df['r']).index([x for x in df['r'] if x < 2.73154+0.5][-1])
MSE_an = round(mean_squared_error(df['F-F(GAP)'][from_point_a:to_point_a], buck4(x_axis)[from_point_a:to_point_a], squared=False), 4)
with open(f'{wd_name}/MSE.txt', 'w') as f:
    f.write(str(MSE_catan))
    f.write('\n')
    f.write(str(MSE_an))
print(f'RMSE(cat-an): {MSE_catan}')
print(f'RMSE(an-an): {MSE_an}')

# Histogram above the potential figure (uppger panel)
trace7 = fig.add_histogram(x=all_het_dist, xbins=dict(start=0, end=6, size=0.005), marker_color=BM_color,\
                            name='Number of hetero species interatomic distance', yaxis='y2')
trace8 = fig.add_histogram(x=all_homo_dist, xbins=dict(start=0, end=6, size=0.005), marker_color='firebrick',\
                            name='Number of homo species interatomic distance', yaxis='y2')

fig.layout = dict(xaxis=dict(domain=[0, 0.8], showgrid=False, zeroline=False, title='Interatomic distance / Å'),
                  yaxis=dict(domain=[0, 0.8], range=[-20, 50], showgrid=False, zeroline=True, title='Potential energy / eV'),
                  margin=dict(l=80, r=80, t=80, b=150),
                  width=1800,
                  height=800,
                  hovermode='closest',
                  bargap=0.8,
                  xaxis2=dict(domain=[0.85, 1], showgrid=False, zeroline=False, ),
                  yaxis2=dict(domain=[0.85, 1], showgrid=False, zeroline=False, title='Count'),
                  font=dict(size=20),
                  )

# Transparent vertical region where from the shortest to longest interatomic distance covered by the training structure
fig.add_vrect(
    x0=min(all_het_dist) ,x1=max(all_het_dist) ,
    fillcolor=BM_color, opacity=0.4,
    layer="below", line_width=1,)

fig.add_vrect(
    x0=min(all_homo_dist) ,x1=max(all_homo_dist) ,
    fillcolor='firebrick', opacity=0.4,
    layer="below", line_width=1,)

# Vertial-dash line to show equilibrium interatomic distances for Al-F, F-F
fig.add_vline(x=1.57705, line_width=2, line_dash="dash", line_color=BM_color)
fig.add_vline(x=2.73154, line_width=2, line_dash="dash", line_color="firebrick")
fig.add_annotation(
            #size=15,
            x=0.6,
            y=0.7,
            text=f"RMSE(Al-F) = {MSE_catan}
 RMSE(F-F) = {MSE_an}",
            xanchor='left',
            xref="paper",
            yref="paper")

#fig.add_annotation(
#            #size=15,
#            x=0,
#            y=-0.25,
#            text=f"RMSE = {MSE_catan} (at nearest distance > 1.57 Å \
#between V(GAP[Al-F]) and V(BM[Al-F])
 RMSE = {MSE_an} \
#(at nearest distance > 2.7 Å between V(GAP[F-F]) and V(buck4[F-F])",
#            xanchor='left',
#            xref="paper",
#            yref="paper")

fig.write_html(f'./{wd_name}/plot.html')

print(f'\n\n\n The plot (./{wd_name}/plot.html) is saved -- Good luck!\n\n\n')


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

# 예제 내용
# * 기본 위젯을 사용하여 기본 창을 생성
# * '창 이름'을 변경한다.

import sys

from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QSplitter
from PyQt5.QtWidgets import QTextEdit
from PyQt5.QtWidgets import QVBoxLayout

from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt

__author__ = "Deokyu Lim <hong18s@gmail.com>"

class Form(QWidget):
    """
    만들고자 하는 프로그램의 기본이 되는 창 또는 폼 위젯.
    본 위젯 위에 다른 위젯을 올려서 모양을 만든다.

    QWidget을 상속받아서 필요한 메소드를 작성.
    """

    def __init__(self):
        """
        보통 __init__ (생성자)에서 필요한 것들을 다를 위젯들을 선언해줘도 되지만 init_widget을 따로 만들어서 호출한다.
        """
        QWidget.__init__(self, flags=Qt.Widget)

        self.te_1 = QTextEdit()
        self.te_2 = QTextEdit()
        self.te_3 = QTextEdit()
        self.split_1 = QSplitter()
        self.split_2 = QSplitter()
        self.vbox = QVBoxLayout()
        self.container_vbox = QVBoxLayout()
        self.init_widget()

    def init_widget(self):
        """
        현재 위젯의 모양등을 초기화
        """
        self.setWindowTitle("Hello World")
        self.split_1.addWidget(self.te_1)
        self.split_1.addWidget(self.te_2)
        self.container_vbox.addWidget(self.split_1)

        self.split_2.setOrientation(Qt.Vertical)
        self.split_2.addWidget(self.split_1)
        self.split_2.addWidget(self.te_3)

        self.vbox.addWidget(self.split_2)
        self.setLayout(self.vbox)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = Form()
    form.show()
    exit(app.exec_())


try:
  from .. import unzip_requirements
except ImportError:
  pass
from gevent import monkey
monkey.patch_all()
import json
import boto3
import invokust
from locust import HttpLocust, TaskSet, task
import os

class TaskPostWithBodyAndHeaders(TaskSet):
    @task()
    def post_method(self):
        self.client.post(path,
                         data=body,
                         headers=headers)

class TaskPostWithBody(TaskSet):
    @task()
    def post_method(self):
        self.client.post(path,
                         data=body)

class TaskPostWithHeaders(TaskSet):
    @task()
    def post_method(self):
        self.client.post(path,
                         headers=headers)

class TaskPost(TaskSet):
    @task()
    def post_method(self):
        self.client.post(path)

class TaskGetWithBodyAndHeaders(TaskSet):
    @task()
    def get_method(self):
        self.client.get(path,
                        data=body,
                        headers=headers)

class TaskGetWithBody(TaskSet):
    @task()
    def get_method(self):
        self.client.get(path,
                        data=body)

class TaskGetWithHeaders(TaskSet):
    @task()
    def get_method(self):
        self.client.get(path,
                        headers=headers)

class TaskGet(TaskSet):
    @task()
    def get_method(self):
        self.client.get(path)

class TestLogicGetWithBodyAndHeaders(HttpLocust):
    task_set = TaskGetWithBodyAndHeaders

class TestLogicGetWithBody(HttpLocust):
    task_set = TaskGetWithBody

class TestLogicGetWithHeaders(HttpLocust):
    task_set = TaskGetWithHeaders

class TestLogicGet(HttpLocust):
    task_set = TaskGet

class TestLogicPostWithBodyAndHeaders(HttpLocust):
    task_set = TaskPostWithBodyAndHeaders

class TestLogicPostWithBody(HttpLocust):
    task_set = TaskPostWithBody

class TestLogicPostWithHeaders(HttpLocust):
    task_set = TaskPostWithHeaders

class TestLogicPost(HttpLocust):
    task_set = TaskPost

def push_not_available_if_not_float(value):
    try:
        temp = float(value)
    except Exception as e:
        temp = float(0)
    return temp

def start(event, context):
    print('Processing SQS trigger for notify.')
    client = boto3.client('sqs')

    print('Get the current Queue URL.')
    currentQueueArn = str(event["Records"][0]["eventSourceARN"])
    response = client.get_queue_url(
        QueueName=currentQueueArn.split(":")[5],
        QueueOwnerAWSAccountId=currentQueueArn.split(":")[4]
    )
    queueUrl = response["QueueUrl"]
    print('Managed to fetch the current queue url -> {}'.format(queueUrl))

    message = json.loads(event["Records"][0]["body"])
    url = str(message["url"])
    numClients = int(message["numClients"])
    hatchRate = int(message["hatchRate"])
    runTime = str(message["runTime"])
    requestId = str(message["requestId"])
    maxThreads = int(message["maxThreads"])
    threadCount = str(message["threadCount"])
    method = str(message["method"])

    global path
    global body
    global headers
    global request_name

    path = str(message["path"])
    body = str(message["body"])
    headers = str(message["headers"])
    request_name = "{}_{}".format(method, path)

    print('Removing message from queue.')
    try:
        client.delete_message(
            QueueUrl=queueUrl,
            ReceiptHandle=event['Records'][0]['receiptHandle']
        )
    except Exception as e:
        print("Message was already deleted or \"receiptHandle\" is invalid.")
        pass

    print('Starting shell subprocess for loadtest with threadCount {}.'.format(threadCount))

    # Set up the load test settings
    if method == "POST":
        if body == "{}" and headers == "{}":
            settings = invokust.create_settings(
                classes=[TestLogicPost],
                host=url,
                num_clients=numClients,
                hatch_rate=hatchRate,
                run_time=runTime
            )

        if body == "{}" and headers != "{}":
            settings = invokust.create_settings(
                classes=[TestLogicPostWithHeaders],
                host=url,
                num_clients=numClients,
                hatch_rate=hatchRate,
                run_time=runTime
            )

        if body != "{}" and headers == "{}":
            settings = invokust.create_settings(
                classes=[TestLogicPostWithBody],
                host=url,
                num_clients=numClients,
                hatch_rate=hatchRate,
                run_time=runTime
            )

        if body != "{}" and headers != "{}":
            settings = invokust.create_settings(
                classes=[TestLogicPostWithBodyAndHeaders],
                host=url,
                num_clients=numClients,
                hatch_rate=hatchRate,
                run_time=runTime
            )

    if method == "GET":
        if body == "{}" and headers == "{}":
            settings = invokust.create_settings(
                classes=[TestLogicGet],
                host=url,
                num_clients=numClients,
                hatch_rate=hatchRate,
                run_time=runTime
            )

        if body == "{}" and headers != "{}":
            settings = invokust.create_settings(
                classes=[TestLogicGetWithHeaders],
                host=url,
                num_clients=numClients,
                hatch_rate=hatchRate,
                run_time=runTime
            )

        if body != "{}" and headers == "{}":
            settings = invokust.create_settings(
                classes=[TestLogicGetWithBody],
                host=url,
                num_clients=numClients,
                hatch_rate=hatchRate,
                run_time=runTime
            )

        if body != "{}" and headers != "{}":
            settings = invokust.create_settings(
                classes=[TestLogicGetWithBodyAndHeaders],
                host=url,
                num_clients=numClients,
                hatch_rate=hatchRate,
                run_time=runTime
            )

    # Start the load test
    try:
        loadtest = invokust.LocustLoadTest(settings)
        loadtest.run()
    except Exception as e:
        print('Encountered error when running loadtest with threadCount {}. Error = {}'.format(
            threadCount, str(e)))

    stats = loadtest.stats()

    print("Finished tests.")
    responseValue={
        "input": {
            "url": url,
            "numClients": numClients,
            "hatchRate": hatchRate,
            "runTime": runTime,
            "maxThreads": maxThreads,
            "requestId": requestId,
            "threadCount": threadCount,
            "method": method,
            "path": path
        },
        "stats": stats
    }

    queue = os.environ.get("PostResultSqsUrl")
    client.send_message_batch(
        QueueUrl=queue,
        Entries=[{
            "Id": threadCount,
            "MessageBody": json.dumps(responseValue),
            "DelaySeconds": 0
        }]
    )

    return "Done"


import os
import sys
import time
import json
import socket
import threading
from tkinter import Tk, Menu, Listbox, Label, Scrollbar, Entry, StringVar, SINGLE, END, CENTER, ACTIVE, LEFT, N, S, E, W, TclError
from tkinter.ttk import Button, Style
from tkinter.messagebox import showinfo, showerror, askquestion
from tkinter.filedialog import askopenfilename, asksaveasfilename

from playsound import playsound

from eth import ETHConnection

MY_NAME = ''
OTHER_IP = ''
IS_SERVER = False
EXITED = False

def threaded(fn):
    def wrapper(*args, **kwargs):
        thread = threading.Thread(target=fn, args=args, kwargs=kwargs)
        thread.start()
        return thread
    return wrapper

class SetupWindow(Tk):
    """
    Little GUI for setting user's name, the partner's ip and that
    the PC is a server or not.
    """

    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)

        try:  # Set icon
            self.iconbitmap('icon.ico')
        except Exception:
            pass

        self.wm_title('TinyChat')
        self.resizable(False, False)
        try:
            self.eval('tk::PlaceWindow %s center' % self.winfo_pathname(self.winfo_id()))
        except TclError:
            pass

        self.name = 'anonymous'
        self.other_ip = socket.gethostbyname(socket.gethostname())
        self.is_server = True

        # Read last config from file
        if os.path.isfile('config.json'):
            with open('config.json', 'r') as file:
                config = json.load(file)
                self.name = config['name']
                self.other_ip = config['other_ip']
                self.is_server = config['is_server']

        self.name_label = Label(self, text='Name')
        self.other_ip_label = Label(self, text='IP')
        self.server_label = Label(self, text='Server')

        self.name_entry = Entry(self, textvariable=StringVar(), bd=1, width=30, bg='white', fg='black')
        self.other_ip_entry = Entry(self, textvariable=StringVar(), bd=1, width=30, bg='white', fg='black')
        self.server_button = Button(self, text='Yes' if self.is_server else 'No', width=22, command=lambda: self.set_is_server())
        self.apply_button = Button(self, text='Start', width=7, command=self.apply)

        self.name_entry.config(textvariable=StringVar(self, self.name))
        self.other_ip_entry.config(textvariable=StringVar(self, self.other_ip))
        self.other_ip_entry.config(state='disabled' if self.is_server else 'normal')

        self.name_label.grid(padx=1, pady=2, row=0, column=0)
        self.name_entry.grid(padx=1, pady=2, row=0, column=1)
        self.other_ip_label.grid(padx=1, pady=2, row=1, column=0)
        self.other_ip_entry.grid(padx=1, pady=2, row=1, column=1)
        self.server_label.grid(padx=1, pady=2, row=2, column=0)
        self.server_button.grid(padx=1, pady=2, row=2, column=1)
        self.apply_button.grid(padx=1, pady=5, row=3, column=1)

        self.mainloop()

    def set_is_server(self):
        self.is_server = not self.is_server
        self.server_button.config(text='Yes' if self.is_server else 'No')
        self.other_ip_entry.config(state='disabled' if self.is_server else 'normal')

    def apply(self):
        global MY_NAME, OTHER_IP, IS_SERVER

        if self.name_entry.get() and self.other_ip_entry.get():
            MY_NAME = self.name_entry.get()
            OTHER_IP = self.other_ip_entry.get()
            IS_SERVER = self.server_button.cget('text') == 'Yes'

            config = {
                'name': MY_NAME,
                'other_ip': OTHER_IP,
                'is_server': IS_SERVER
            }
            with open('config.json', 'w+') as file:
                json.dump(config, file)

            self.destroy()

class TinyChat(Tk):

    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)

        try:  # Set icon
            self.iconbitmap('icon.ico')
        except Exception:
            pass

        self.wm_title('TinyChat')
        self.resizable(False, False)

        try:
            self.eval('tk::PlaceWindow %s center' % self.winfo_pathname(self.winfo_id()))
        except TclError:
            pass

        self.bind('<Key>', self.keypress)

        self.sound_played_for_notification = False
        self.new_notification = False
        self.line_width = 42

        self.msg_list = Listbox(self, selectmode=SINGLE, height=10, width=self.line_width, borderwidth=0)
        self.msg_scb = Scrollbar(self, orient='vertical')
        self.msg_scb.grid(padx=0, pady=0, row=0, column=1, sticky=N + S)
        self.msg_list.config(yscrollcommand=self.msg_scb.set)
        self.msg_scb.config(command=self.msg_list.yview)

        self.entry_box = Entry(self, textvariable=StringVar(), bd=0, width=self.line_width, bg='white', fg='black')
        self.entry_button = Button(self, text='→', width=3, command=self.send_msg)

        self.msg_list.grid(padx=0, pady=0, row=0, column=0)
        self.entry_box.grid(padx=0, pady=0, row=1, column=0)
        self.entry_button.grid(padx=0, pady=0, row=1, column=1)

        # Connect on the ethernet port
        self.ETH = ETHConnection(OTHER_IP, IS_SERVER)

        # self.msg_recv_thread = Thread(target=self.recv_msg)
        # self.msg_recv_thread.start()
        # self.notifications_thread = Thread(target=self.notifications)
        # self.notifications_thread.start()
        self.msg_recv_thread = self.recv_msg()
        self.notifications_thread = self.notifications()

        self.wm_attributes('-topmost', 1)
        self.bind('<FocusIn>', self.handle_focus)
        self.protocol('WM_DELETE_WINDOW', self.on_closing)
        self.mainloop()

    def send_msg(self):
        global MY_NAME, OTHER_IP, IS_SERVER
        msg_to_send = self.entry_box.get()
        self.entry_box.delete(0, END)

        if msg_to_send:
            if msg_to_send[0] == '/':  # Commands
                cmd = msg_to_send.split(' ')

                if cmd[0] == '/?':
                    self.msg_list.insert(END, ' ')
                    self.msg_list.insert(END, 'HELP:')
                    self.msg_list.insert(END, '/setname NAME - Sets your name for the given NAME.')
                    self.msg_list.insert(END, '/re - Tries to reconnect.')
                    self.msg_list.insert(END, '/ip IP - Sets the partner\'s ip to IP.')
                    self.msg_list.insert(END, ' ')
                if cmd[0] == '/setname':
                    MY_NAME = cmd[1].strip()
                if cmd[0] == '/re':
                    self.ETH.reconnect()
                if cmd[0] == '/ip':
                    OTHER_IP = cmd[1].strip()

                return

            msg_to_send = MY_NAME + ': ' + msg_to_send
            self.add_msg(msg_to_send)

            try:
                self.ETH.send(msg_to_send)
            except:
                self.msg_list.insert(END, 'There is noone on the other end  ¯\\_(ツ)_/¯.')

                # Try to reestabilish connection
                self.ETH.reconnect()
                self.msg_recv_thread = self.recv_msg()
                self.msg_recv_thread.start()

    def add_msg(self, msg):
        if len(msg) > self.line_width:
            for i in range(int(len(msg) / self.line_width) + 1):
                self.msg_list.insert(END, msg[i * self.line_width:(i + 1) * self.line_width])
        else:
            self.msg_list.insert(END, msg)

        self.msg_list.yview_moveto('1')  # Scroll down to the last message

    def handle_focus(self, e):
        self.new_notification = False

    @threaded
    def recv_msg(self):
        global EXITED
        while not EXITED:
            time.sleep(0.1)

            if not self.ETH:
                continue

            data = self.ETH.recv(1024)
            if data:
                self.add_msg(data)
                self.new_notification = True
                self.sound_played_for_notification = False

    @threaded
    def notifications(self):
        global EXITED
        while not EXITED:
            time.sleep(0.1)

            if self.new_notification:
                if not self.sound_played_for_notification:
                    playsound('audio/notification.wav')
                    self.sound_played_for_notification = True

    def on_closing(self):
        """
        if self.ETH.is_connected:
            self.ETH.send(MY_NAME + ' has left the chat.')
            self.ETH.close()
        """
        global EXITED
        EXITED = True
        self.notifications_thread.join()
        self.msg_recv_thread.join()
        self.destroy()

    def keypress(self, event):
        # print(repr(event.char))
        # Turned off because \x08 == Ctrl + h and \x08 == Backspace
        # if event.char == '\x08':  # Ctrl + h
            # self.help()

        if event.char == '\r':
            self.send_msg()
            self.msg_list.yview_moveto('1')  # Scroll down to the last message

if __name__ == '__main__':
    SetupWindow()

    if MY_NAME:
        TinyChat()


from django.utils.functional import lazy

from mock import patch

from django_browserid import helpers
from django_browserid.tests import JSON_STRING, TestCase

def _lazy_request_args():
    return {'siteName': 'asdf'}
lazy_request_args = lazy(_lazy_request_args, dict)

class BrowserIDInfoTests(TestCase):
    def setUp(self):
        patcher = patch('django_browserid.helpers.render_to_string')
        self.addCleanup(patcher.stop)
        self.render_to_string = patcher.start()

    def test_defaults(self):
        with self.settings(BROWSERID_REQUEST_ARGS={'foo': 'bar', 'baz': 1}):
            output = helpers.browserid_info()

        self.assertEqual(output, self.render_to_string.return_value)
        expected_info = JSON_STRING({
            'loginUrl': '/browserid/login/',
            'logoutUrl': '/browserid/logout/',
            'csrfUrl': '/browserid/csrf/',
            'requestArgs': {'foo': 'bar', 'baz': 1},
        })
        self.render_to_string.assert_called_with('browserid/info.html', {'info': expected_info})

    def test_lazy_request_args(self):
        with self.settings(BROWSERID_REQUEST_ARGS=lazy_request_args()):
            output = helpers.browserid_info()

        self.assertEqual(output, self.render_to_string.return_value)
        expected_info = JSON_STRING({
            'loginUrl': '/browserid/login/',
            'logoutUrl': '/browserid/logout/',
            'csrfUrl': '/browserid/csrf/',
            'requestArgs': {'siteName': 'asdf'},
        })
        self.render_to_string.assert_called_with('browserid/info.html', {'info': expected_info})

class BrowserIDJSTests(TestCase):
    def test_basic(self):
        output = helpers.browserid_js()
        self.assertHTMLEqual(output, """

        """)

    def test_no_shim(self):
        output = helpers.browserid_js(include_shim=False)
        self.assertHTMLEqual(output, """

        """)

    def test_custom_shim(self):
        with self.settings(BROWSERID_SHIM='http://example.com/test.js'):
            output = helpers.browserid_js()
        self.assertHTMLEqual(output, """

        """)

    def test_autologin_email(self):
        """
        If BROWSERID_AUTOLOGIN_ENABLED is True, do not include the shim
        and include the autologin mock script.
        """
        with self.settings(BROWSERID_AUTOLOGIN_ENABLED=True):
            output = helpers.browserid_js()
            self.assertHTMLEqual(output, """

            """)

class BrowserIDCSSTests(TestCase):
    def test_basic(self):
        output = helpers.browserid_css()
        self.assertHTMLEqual(output, """
            <link rel="stylesheet" href="static/browserid/persona-buttons.css" />
        """)

class BrowserIDButtonTests(TestCase):
    def test_basic(self):
        button = helpers.browserid_button(text='asdf', next='1234', link_class='fake-button',
                                          href="/test", attrs={'target': '_blank'})
        self.assertHTMLEqual(button, """

                asdf

        """)

    def test_json_attrs(self):
        button = helpers.browserid_button(text='qwer', next='5678', link_class='fake-button',
                                          attrs='{"target": "_blank"}')
        self.assertHTMLEqual(button, """

                qwer

        """)

class BrowserIDLoginTests(TestCase):
    def test_login_class(self):
        button = helpers.browserid_login(link_class='go button')
        self.assertHTMLEqual(button, """

                Sign in

        """)

    def test_default_class(self):
        """
        If no class is provided, it should default to
        'browserid-login persona-button'.
        """
        button = helpers.browserid_login()
        self.assertHTMLEqual(button, """

                Sign in

        """)

    def test_color_class(self):
        button = helpers.browserid_login(color='dark')
        self.assertHTMLEqual(button, """

                Sign in

        """)

    def test_color_custom_class(self):
        """
        If using a color and a custom link class, persona-button should
        be added to the link class.
        """
        button = helpers.browserid_login(link_class='go button', color='dark')
        self.assertHTMLEqual(button, """

                Sign in

        """)

    def test_next(self):
        button = helpers.browserid_login(next='/foo/bar')
        self.assertHTMLEqual(button, """

                Sign in

        """)

class BrowserIDLogoutTests(TestCase):
    def test_logout_class(self):
        button = helpers.browserid_logout(link_class='go button')
        self.assertHTMLEqual(button, """

                Sign out

        """)

    def test_next(self):
        button = helpers.browserid_logout(next='/foo/bar')
        self.assertHTMLEqual(button, """

                Sign out

        """)


import json
import requests
import time
import urllib
from datetime import datetime
import pytz

def greetMsg():
    IST = pytz.timezone('Asia/Kolkata')
    datetime_object = datetime.now(IST)
    hour = datetime_object.hour
    if(hour > 0 and hour < 12):
        return 'Good Morning'
    elif(hour >= 12 and hour < 18):
        return 'Good Afternoon'
    elif hour >= 18 and hour < 22:
        return 'Good Evening'
    else:
        return 'I am sleepy, Good Night!!!'

with open('secrets.json') as f:
    data = json.load(f)

# Output: {'name': 'Bob', 'languages': ['English', 'Fench']}
# print(data)

TOKEN = data["key"]
URL = "https://api.telegram.org/bot{}/".format(TOKEN)

greetings = ["Hi", "Hello", "Hey"]

def get_url(url):
    response = requests.get(url)
    content = response.content.decode("utf8")
    return content

def get_json_from_url(url):
    content = get_url(url)
    js = json.loads(content)
    return js

def get_updates(offset=None):
    url = URL + "getUpdates?timeout=100"
    if offset:
        url += "&offset={}".format(offset)
    js = get_json_from_url(url)
    return js

def get_last_update_id(updates):
    update_ids = []
    for update in updates["result"]:
        update_ids.append(int(update["update_id"]))
    return max(update_ids)

def echo_all(updates):
    for update in updates["result"]:
        try:
            msgR = update["message"]["text"]
            value = [1 for item in greetings if(item in msgR)]
            if(1 in value):
                firstname = update["message"]["from"]["first_name"]
                message = "Hi {0},\n{1}".format(firstname, greetMsg())
                text = message
            else:
                text = update["message"]["text"]

            chat = update["message"]["chat"]["id"]
            send_message(text, chat)
        except Exception as e:
            print(e)

def get_last_chat_id_and_text(updates):
    num_updates = len(updates["result"])
    last_update = num_updates - 1
    text = updates["result"][last_update]["message"]["text"]
    chat_id = updates["result"][last_update]["message"]["chat"]["id"]
    return (text, chat_id)

def send_message(text, chat_id):
    text = urllib.parse.quote_plus(text)
    url = URL + "sendMessage?text={}&chat_id={}".format(text, chat_id)
    get_url(url)

def main():
    last_update_id = None
    while True:
        updates = get_updates(last_update_id)
        if len(updates["result"]) > 0:
            last_update_id = get_last_update_id(updates) + 1
            echo_all(updates)
        time.sleep(0.5)

if __name__ == '__main__':
    main()


import youtube_dl

print("Colocar o link de uma playlist ou o nome de uma musica do youtube")
url = input()

ydl_opts = {
        'format' : 'bestaudio/best',
        'keepvideo': False,
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }]
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download([url])


import argparse
import cv2
import numpy as np
import random

def addConstant(image_file_name='data/test.png', length=50, num=5):
    image = cv2.imread(image_file_name, 1)
    modifiedImage = np.empty(
        [len(image),  len(image[0]), 3], dtype=np.uint8)
    for i in range(len(image)):
        for j in range(len(image[0])):
            for k in range(3):
                modifiedImage[i][j][k] = image[i][j][k]
    for i in range(num):
        startX = random.randint(0, len(image) - length)
        startY = random.randint(0, len(image) - length)
        addConstantOnce(modifiedImage, length, startX, startY)
    cv2.imwrite("saved/constant.png", (modifiedImage))

def addConstantOnce(modifiedImage, length, startX, startY):
    for i in range(startX, startX + length):
        for j in range(startY, startY + length):
            for k in range(3):
                modifiedImage[i][j][k] = 0

    return modifiedImage

addConstant(num=20)


from openai import OpenAI
from decouple import config

def get_openai_response(input_text):
    openai_api_key = config('OPENAI_API_KEY')

    client = OpenAI(api_key=openai_api_key)

    response = client.chat.completions.create(
        model="gpt-3.5-turbo-1106",
        messages=[
            {"role": "system", "content": "HealthPoint a diagnosis chatbot that also give precautions and only answers medical or health questions"},
            {"role": "user", "content": input_text}
        ]
    )
    return response.choices[0].message.content


import requests
from bs4 import BeautifulSoup

def scrape(url):
    try:
        res = requests.get(url)
        soup = BeautifulSoup(res.text, features="lxml")
    except:
        soup = ''

    return soup

def get_data(url):
    soup = scrape(url)
    try:
        company_name = soup.select('.fn')
        company_name = company_name[0].text
    except AttributeError:
        company_name = ''

    try:
        company_code_tag = soup.find('td', text='Įmonės kodas')
        company_code = company_code_tag.find_next_sibling('td')
        company_code = company_code.text.strip()
    except AttributeError:
        company_code = ''

    try:
        company_address_elem = soup.find('td', {'itemprop': 'address'})
        company_address = company_address_elem.get_text(strip=True)
    except AttributeError:
        company_address = ''

    try:
        company_boss_tag = soup.find('td', text='Vadovas')
        company_boss = company_boss_tag.find_next_sibling('td')
        company_boss = company_boss.text.strip()
    except AttributeError:
        company_boss = ''

    extra_info = ''

    return [company_name, company_code, company_address, company_boss,
            extra_info]


# -*- coding: utf-8 -*-
"""
Created on Fri Mar  5 00:49:27 2021

@author: Tarandeep
"""

import PySimpleGUI as sg
import json

# create layout

def gui_method():

    bold_method = ('Arial', 12, 'bold')

    layout_gui = [
        [
            sg.Text(
                "This form requires files listed below to calculate model that returns the lowest distance.", font=bold_method)
        ],
        [
            sg.Text("File locations left blank will use default values.")
        ],
        [
            sg.Text("Select Excel Layout", size=(30, 1)),
            sg.In(size=(25, 1), enable_events=True, key="layout"),
            sg.FileBrowse(),
        ],
        [
            sg.Text("Select Specifications File", size=(30, 1)),
            sg.In(size=(25, 1), enable_events=True, key="specs"),
            sg.FileBrowse(),
        ],
        [
            sg.Text("Select Sales Data File", size=(30, 1)),
            sg.In(size=(25, 1), enable_events=True, key="salesData"),
            sg.FileBrowse(),
        ],
        [
            sg.Text("Select Store Orders", size=(30, 1)),
            sg.In(size=(25, 1), enable_events=True, key="storeOrder"),
            sg.FileBrowse(),
        ],
        [
            sg.Text("Select Order Lines Location", size=(30, 1)),
            sg.In(size=(25, 1), enable_events=True, key="orderLinesLocation"),
            sg.FolderBrowse(),
        ],
        [
            sg.Text("Export File Location", size=(30, 1)),
            sg.In(size=(25, 1), enable_events=True, key="exportLocation"),
            sg.FolderBrowse(),
        ],
        [
            sg.Text("What range of weeks would you like to look at?",
                    font=bold_method)
        ],
        [
            sg.Text("Starting Week", size=(30, 1)),
            sg.InputText(size=(5, 1), key="startWeek")
        ],
        [
            sg.Text("Ending Week", size=(30, 1)),
            sg.InputText(size=(5, 1), key="endWeek")
        ],
        [
            sg.Text("Select store order date to analyze:", font=bold_method)
        ],
        [
            sg.Text("Month", size=(10, 1)),
            sg.InputText(size=(5, 1), key="salesMonth"),
            sg.Text("Day", size=(10, 1)),
            sg.InputText(size=(5, 1), key="salesDay"),
            sg.Text("Year", size=(10, 1)),
            sg.InputText(size=(5, 1), key="salesYear")
        ],
        [
            sg.Checkbox("Would you like to compute Space Allocation Model? If not, previous results will be used.",
                        default=True, key="spaceAllocate"),
        ],
        [
            sg.Text("Space Allocation Parameters:", font=bold_method)
        ],
        [
            sg.Text("Max Spaces per SKU", size=(30, 1)),
            sg.InputText(size=(5, 1), key="maxSpaces")
        ],
        [
            sg.Text(
                "Aisle Declaration (ex. 1st row, 22nd row, 50th row) ", font=bold_method)
        ],
        [
            sg.Text("Top Aisle", size=(10, 1)),
            sg.InputText(size=(5, 1), key="aisleTop"),
            sg.Text("Cross Aisle", size=(10, 1)),
            sg.InputText(size=(5, 1), key="aisleMiddle"),
            sg.Text("Bottom Aisle", size=(10, 1)),
            sg.InputText(size=(5, 1), key="aisleBottom")
        ],
        [
            sg.Text("Determine SKU List to Calculate", font=bold_method)
        ],
        [
            sg.Checkbox("Random", default=True, key="random"),
            sg.Checkbox("COI", default=True, key="coi"),
            sg.Checkbox("Weight", default=True, key="weight"),
            sg.Checkbox("ABC (across)", default=True, key="across"),
            sg.Checkbox("ABC (vertical)", default=True, key="vertical")
        ],
        [
            sg.Submit(), sg.Cancel()
        ]
    ]

    window = sg.Window("Metro SKU Assignment Tool").Layout(layout_gui)
    while True:
        event, values = window.Read()  # Run the window until an "event" is triggered
        if event == "Submit":
            window.close()
            return values
        elif event is None or event == "Cancel":
            window.close()
            return None


from django.shortcuts import render,redirect
from django.http import HttpResponse, JsonResponse
from django.template import loader
# from administracion.models import Producto, Varietal

def hola_tienda(request):
    return HttpResponse('Bienvenido a la Tienda')

def index(request):
   template = loader.get_template('tienda/index.html')
   context={'titulo':'Inicio'}
   return HttpResponse(template.render(context,request))

# def indexp(request):
#     template_name="index.html"
#     varietales=Varietal.objects.filter(activo=True)
#     productos=Producto.objects.filter(activo=True)
#     context ={"productos":productos, "varietales":varietales}
#     return render(request,template_name,context)

# def index(request):
#    template = loader.get_template('tienda/index.html')
#    varietales=Varietal.objects.filter(activo=True)
#    productos=Producto.objects.filter(activo=True)
#    context={'titulo':'Inicio','producto':productos,'varietal':varietales}
#    return HttpResponse(template.render(context,request))

# def producto(request):
#    template = loader.get_template('tienda/productos.html')
#    context={'titulo':'Producto'}
#    return HttpResponse(template.render(context,request))

def index(request):
    if(request.method=='GET'):
        titulo = 'Titulo cuando accedo por GET'
    else:
        titulo = 'Titulo cuando accedo por otro método'
    parametro_uno = request.GET.get('param')

    listado_cursos = [
        {'nombre': 'Diseño UX/UI',
         'descripcion': 'Diseño' ,
         'categoria': 'Diseño',
         },
         {'nombre':'Fullstack Java',
         'descripcion': 'Curso FullStack',
         'categoria': 'Programacion',
         },
         {'nombre': 'Big Data',
         'descripcion': 'test',
         'categoria': 'Analisis de Datos',
         },
         {'nombre': 'Big Data Avanzada',
         'descripcion': 'test',
         'categoria': 'Analisis de Datos',
         },
    ]

    return render(request,'tienda/index.html',
                  {'titulo':titulo,
                   'parametro_uno':parametro_uno,
                   'cursos':listado_cursos
                  })

#Create your views here.

def api_productos(request):
   productos = [{
       'nombre': 'D.V. catena - tinto histórico 2019',
       'varietal': 'Blend',
       'portada': 'https://vinotecaligier.com/media/catalog/product/cache/1/image/500x500/9df78eab33525d08d6e5fb8d27136e95/b/e/be72477.jpg',
       'bodega': 'catena zapata',
       'precio': '3500'
   },{
       'nombre': 'White Bones',
       'varietal': 'Chardonnay',
       'portada': 'https://vinotecaligier.com/media/catalog/product/cache/1/small_image/430x320/9df78eab33525d08d6e5fb8d27136e95/b/e/be76371_base.jpg',
       'bodega': 'catena zapata',
       'precio': '42200'
   },{
       'nombre': 'River',
       'varietal': 'Malbec',
       'portada': 'https://vinotecaligier.com/media/catalog/product/cache/1/image/500x500/9df78eab33525d08d6e5fb8d27136e95/b/e/be72477.jpg',
       'bodega': 'catena zapata',
       'precio': '45600'
    },{
       'nombre': 'Saint Felicien',
       'varietal': 'Rose',
       'portada': 'https://http2.mlstatic.com/D_NQ_NP_601614-MLA52995055033_122022-V.jpg',
       'bodega': 'catena zapata',
       'precio': '9300'
    },{
       'nombre': 'Saint Felicien',
       'varietal': 'Sauvignon Blanc',
       'portada': 'https://cepadevinos.com/wp-content/uploads/2017/07/Saint_Felicien_Sauvignon_Blanc_qbs3es.jpg',
       'bodega': 'catena zapata',
       'precio': '42200'
    },{
       'nombre': 'Nicasia Vineyard Doux',
       'varietal': 'Espumante',
       'portada': 'https://www.espaciovino.com.ar/media/default/0001/65/thumb_64247_default_big.jpeg',
       'bodega': 'catena zapata',
       'precio': '42200'
    },{
       'nombre': 'Paraiso',
       'varietal': 'Blend',
       'portada': 'https://cepadevinos.com/wp-content/uploads/2017/07/Saint_Felicien_Sauvignon_Blanc_qbs3es.jpg',
       'bodega': 'Luigi Bosca',
       'precio': '42200'
    },{
       'nombre': 'Los Nobles',
       'varietal': 'Chardonnay',
       'portada': 'https://vinotecaligier.com/media/catalog/product/cache/1/image/500x500/9df78eab33525d08d6e5fb8d27136e95/b/e/be72477.jpg',
       'bodega': 'Luigi Bosca',
       'precio': '42200'
    },]
   response = {'status':'Ok', 'code':200, 'message':'Listado de productos', 'data':productos}
   return JsonResponse(response,safe=False)

def productos(request):
    return render(request,'tienda/productos.html')


import pandas as pd
from flask import Flask, render_template
from datetime import date

month = date.today().strftime("%b-%Y")
app = Flask(__name__)
app.config['STATIC_FOLDER'] = 'static'

@app.route('/')
def scoreboard():
    df = pd.read_csv(f"{month}_Summary.csv")
    limited_data = df.head(25).to_dict(orient='records')  # First 15 rows
    full_data = df.to_dict(orient='records')  # All rows
    return render_template('scoreboard.html', limited_data=limited_data, full_data=full_data)


import cmath
import math
from scipy.fftpack import fft, ifft

class FourierTransform:
    count = 0

    @staticmethod
    def fft_convolution(self, sig1, sig2):
        self.count += len(sig1)
        return list(map(lambda x, y: x * y, sig1, sig2))

    @staticmethod
    def fft_correlation(self, sig1, sig2):
        self.count += len(sig1)
        return list(map(lambda x, y: x.conjugate() * y, sig1, sig2))

    @staticmethod
    def _fft(self, _in_values, direction):
        _out_values = self._fft_(self, _in_values, direction)

        if direction == -1:
            n = len(_in_values)
            for i in range(n):
                _out_values[i] /= n

        if direction == 1:
            return fft(_in_values)
        else:
            return ifft(_in_values)

    @staticmethod
    def _fft_(self, _values, direction):
        n = len(_values)

        if n == 1:
            return _values

        _values_even = [complex(0, 0)] * (int(n / 2))
        _values_odd = [complex(0, 0)] * (int(n / 2))

        for i in range(n):
            if i % 2 == 0:
                _values_even[int(i / 2)] = _values[i]
            else:
                _values_odd[int(i / 2)] = _values[i]

        _b_even = self._fft_(self, _values_even, direction)
        _b_odd = self._fft_(self, _values_odd, direction)

        w_n = complex(cmath.exp(direction * (-1) * 2 * cmath.pi * complex(0, 1) / n))
        w = 1
        y = [complex(0, 0)] * n

        for i in range(int(n / 2)):
            y[i] = _b_even[i] + _b_odd[i] * w
            y[i + int(n / 2)] = _b_even[i] - _b_odd[i] * w
            w *= w_n
            self.count += 1

        return y

    @staticmethod
    def _fft_reorder(self, _data, _len):
        if _len <= 2:
            return
        for x in range(_len):
            r = self.rev_next(x, _len)
            if r > x:
                temp = _data[x]
                _data[x] = _data[r]
                _data[r] = temp

        return _data

    @staticmethod
    def rev_next(x, n):
        step = math.log2(n)
        r = 0
        while step != 0:
            r <<= 1
            r += (x & 1)
            x >>= 1
            step -= 1

        return r


#!/usr/bin/python
#coding=utf-8
"""
*文件说明:bak_check.py
*作者:高小调
*创建时间:2017年08月16日 星期三 09时37分38秒
*开发环境:Kali Linux/Python v2.7.13
"""
import sys
sys.path.append("..")
from lib import Downloader
import urlparse
def get_parent_paths(path):
    paths = []
    if not path or path[0] != '/':
        #path为None 或 path为/
        return paths
    paths.append(path)
    tmp = path
    if path[-1] == '/':
        #path 为 /a/b/c/d/e/
        tmp = path[:-1] #tmp = /a/b/c/d/e

    while tmp:  #tmp = /a/b/c/d/e
        tmp = tmp[:tmp.rfind('/')+1]   #tmp = /a/b/c/d/
        paths.append(tmp);
        tmp = tmp[:-1]                      #tmp = /a/b/c/d
    return paths

DIR_PROBE_EXTS = ['.tar.gz', '.zip', '.rar', '.tar.bz2']
FILE_PROBE_EXTS = ['.bak', '.swp', '.1']

def run(url):
    urlobj = urlparse.urlparse(url)
    paths = get_parent_paths(urlobj.path)
    web_paths = []
    for path in paths:
        if path == '/':
            #网站根目录
            for ext in DIR_PROBE_EXTS:
                url = "%s://%s%s%s" % (urlobj.scheme,urlobj.netloc,path,urlobj.netloc+ext)
                web_paths.append(url)
        else:
            if path[-1] == '/':
                #网站子目录
                for ext in DIR_PROBE_EXTS:
                    url = "%s://%s%s%s" % (urlobj.scheme,urlobj.netloc,path[:-1],ext)
                    web_paths.append(url)
            else:
                #非目录
                for ext in FILE_PROBE_EXTS:
                    url = "%s://%s%s%s"%(urlobj.scheme,urlobj.netloc,path,ext)
                    web_paths.append(url)
    downloader = Downloader.Downloader()
    found = False
    for path in web_paths:
        #print("[bak_check]:%s"%path)
        if((downloader.get(path) is not None)):
            found = True
            print("[bak_check]:bak found!%s"%path)
    if found is False:
        print ("[bak_check]:not found!")


from django.http import HttpResponse,Http404
from django.shortcuts import render,redirect
from .models import FoodStall,Review
from .forms import UserRegisterForm,ReviewForm
from django.contrib import messages
from datetime import date

# Home page
def index(request):
    return render(request,"yummyNUS/index.html")

#Stalls Page
def stalls(request):
    context = { 'stalls' : FoodStall.objects.all()}
    return render(request,"yummyNUS/stalls.html",context)

#Navigation Page
def navigate(request):
    return render(request,"yummyNUS/navi.html")

#Login Page
def login(request):
    return render(request,"yummyNUS/login.html")

#SignUp Page
def signup(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            messages.success(request,f'Account created for {username}!')
            return redirect('login')
    else:
        form = UserRegisterForm()
    return render(request,"yummyNUS/signup.html",{'form':form})

#individual stall
def stall(request,stall_id):
    try:
        stall =FoodStall.objects.get(id = stall_id)
    except FoodStall.DoesNotExist:
      raise Http404("Foodstall does not exist")
    if request.method == 'POST':

        form = ReviewForm(request.POST)
        form.foodstall_id = stall_id

        if form.is_valid():
            name = form.cleaned_data.get('Name')
            rating = form.cleaned_data.get('Rating')
            comment = form.cleaned_data.get('Comment')
            rev = Review(Name=name,Rating = rating,Comment = comment)
            rev.save()
            stall.review.add(rev)
            stall.save()
            context= {'stall':stall, 'menus': stall.menus.all(), 'form':form, 'reviews':stall.review.all()}
            return render(request,'yummyNUS/stall.html',context)
    else:

        form = ReviewForm()

    context= {'stall':stall, 'menus': stall.menus.all(), 'form':form, 'reviews':stall.review.all()}
    return render(request,'yummyNUS/stall.html',context)


#!/usr/bin/python
__author__ = 'ar'

import sys
import cv2
import numpy as np
import boto3

def getImageFromS3Object(s3Object):
    img_array = np.asarray(bytearray(s3Object['Body'].read()), dtype=np.uint8)
    ret=cv2.imdecode(img_array, cv2.CV_LOAD_IMAGE_UNCHANGED)
    return ret

########################
if __name__=='__main__':
    s3=boto3.resource('s3')
    s3b=s3.Bucket('imlab')
    for oo in s3b.objects.all():
        print(oo)
        o1=s3b.Object(oo.key).get()
        winName="win"+oo.key
        try:
            cv2.imshow(winName, getImageFromS3Object(o1))
        except:
            pass
    cv2.waitKey()


from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render, redirect

from django.views import View, generic
from .models import Course, Opinion, Lesson, Contact
from users.models import User

class HomeView(View):
    def get(self, request, *args, **kwargs):
        courses = Course.objects.all()
        teachers = User.objects.filter(role="TEACHER")
        opinions = Opinion.objects.all()
        return render(request, 'index.html', context={
                                                "courses": courses,
                                                "teachers": teachers,
                                                "opinions": opinions,
                                                })

class CourseDetailView(View):
    def get(self, request, pk, *args, **kwargs):
        try:
            course = Course.objects.get(pk=pk)
        except:
            return redirect('home')
        if request.user.is_authenticated:
            enroll = True if request.user in course.students.all() else False
        else:
            enroll = False
        return render(request, 'detail.html', context={"course": course, 'enroll': enroll})

class CourseListView(generic.ListView):
    model = Course
    queryset = Course.objects.all()
    template_name = 'courses.html'

    # def get_queryset(self):
    #     user = self.request.user
    #     print(user)
    #     object_list = Course.objects.filter(students=user)
    #     print(object_list)
    #     return object_list

class AboutView(View):
    def get(self, request, *args, **kwargs):
        return render(request, 'about.html')

class ContactView(View):
    def get(self, request, *args, **kwargs):
        return render(request, 'contact.html')

    def post(self, request):
        n = request.POST.get('name')
        e = request.POST.get('email')
        s = request.POST.get('subject')
        m = request.POST.get('message')
        try:
            Contact.objects.create(name=n, email=e, subject=s, message=m)
        except:
            pass
        return redirect('home')

class EnrollCourseView(LoginRequiredMixin, View):
    def get(self, request, pk):
        try:
            course = Course.objects.get(pk=pk)
        except:
            return redirect('home')

        course.students.add(request.user)
        if request.user.is_authenticated:
            enroll = True if request.user in course.students.all() else False
        else:
            enroll = False

        return render(request, 'detail.html', context={"course": course, 'enroll': enroll})

    def post(self, request, pk):
        id=request.POST.get('id')
        try:
            course = Course.objects.get(id=id)
        except:
            return redirect('home')

        course.students.add(request.user)
        if request.user.is_authenticated:
            enroll = True if request.user in course.students.all() else False
        else:
            enroll = False

        return render(request, 'detail.html', context={"course": course, 'enroll': enroll})

class LessonsListView(LoginRequiredMixin, View):

    def get(self, request, pk, *args, **kwargs):
        lesson_id = self.request.GET.get('lesson', None)
        print(lesson_id)
        try:
            course = Course.objects.get(pk=pk)
        except:
            return redirect('home')
        object_list = Lesson.objects.filter(course_id=pk)

        if lesson_id:
            lesson = Lesson.objects.filter(id=lesson_id).first()
        else:
            lesson = Lesson.objects.filter(course=course, number=1)

        return render(request, 'course-lessons.html', {'lessons': object_list, 'course': course, 'lesson': lesson})


import cv2
import numpy as np

def imRead(loc,mode):
    type=''
    if(mode=='Gray'):
        type=cv2.IMREAD_GRAYSCALE
    elif(mode=='Color'):
        type=cv2.IMREAD_ANYCOLOR
    else:
        type=cv2.IMREAD_ANYCOLOR

    return cv2.imread(loc,type)

def subMat(img, rect):
    x=rect[0]
    y=rect[1]
    width=rect[2]
    height=rect[3]
    crop=img[y:y+height,x:x+width]
    return crop

def coorTemplateMatch(img,tmp,threshold):
    res = cv2.matchTemplate(img,tmp,cv2.TM_CCOEFF_NORMED)
    minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(res)
    loc = np.where(res >= threshold) #좌표값 따기
    w, h = tmp.shape[::-1]

    for pt in zip(*loc[::-1]):
        cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2) #박스표시하기
        result_X=pt[0]
        result_Y=pt[1]
        return result_X,result_Y,img

def bTemplateMatch(img,tmp,threshold):
    try:
        res = cv2.matchTemplate(img,tmp,cv2.TM_CCOEFF_NORMED)
    except:
        tmp=cv2.cvtColor(tmp,cv2.COLOR_RGB2GRAY)
        res = cv2.matchTemplate(img,tmp,cv2.TM_CCOEFF_NORMED)

    minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(res)
    threshold = 0.8
    #loc = np.where(res >= threshold) #좌표값 따기
    #for pt in zip(*loc[::-1]):
    #cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2) #박스표시하기
    return True if maxVal>threshold else False

def Resize(img,mul):
    resize=cv2.resize(img, dsize=(0, 0), fx=mul, fy=mul, interpolation=cv2.INTER_LINEAR)
    return resize

def Sharpening(img,mode,arg):
    sharpening_mask1 = np.array([[-1, -1, -1], [-1, arg, -1], [-1, -1, -1]])
    sharpening_mask2 = np.array([[0, -1, 0], [-1, arg, -1], [0, -1, 0]])
    sharpening_out1 = cv2.filter2D(img, -1, sharpening_mask1)
    sharpening_out2 = cv2.filter2D(img, -1, sharpening_mask2)
    if mode==1:
        return sharpening_out1
    else:
        return sharpening_out2


#! /usr/bin/env python3
import argparse
import random
import numpy as np
import torch
import os
import cv2
from tqdm import tqdm
from datasets import collate_fn, CorrespondencesDataset
from utils import (compute_pose_error, pose_auc, estimate_pose_norm_kpts, estimate_pose_from_E)

from model import CLNet
from config import get_config, print_usage

torch.set_grad_enabled(False)
torch.manual_seed(0)

def test(opt, thr=1e-4, use_ransac=True):
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    print('Running inference on device \"{}\"'.format(device))

    test_dataset = CorrespondencesDataset(opt.data_te, opt)

    test_loader = torch.utils.data.DataLoader(
            test_dataset, batch_size=1, shuffle=False,
            num_workers=8, pin_memory=True, collate_fn=collate_fn)

    model = CLNet(opt)
    checkpoint = torch.load(opt.model_path, map_location=torch.device('cpu'))

    state_dict = {}

    '''Load a parallelly trained model'''
    for key in checkpoint['state_dict'].keys():
        key_new = key.split('module')[1][1:]
        state_dict[key_new] = checkpoint['state_dict'][key]

    '''Load a model trained on a single GPU'''
#    state_dict = checkpoint['state_dict']

    model.load_state_dict(state_dict)
    model.cuda()
    model.eval()

    err_ts, err_Rs, precisions, matching_scores, num_corrects = [], [], [], [], []
    for idx, test_data in enumerate(tqdm(test_loader)):
        xs = test_data['xs'].to(device)
        ys = test_data['ys'].to(device)

        _, _, e_hat, y_hat = model(xs, ys)

        mkpts0 = xs.squeeze()[:, :2].cpu().detach().numpy()
        mkpts1 = xs.squeeze()[:, 2:].cpu().detach().numpy()

        mask = y_hat.squeeze().cpu().detach().numpy() < thr
        mask_kp0 = mkpts0[mask]
        mask_kp1 = mkpts1[mask]

        if use_ransac == True:
            file_name = '/aucs.txt'
            ret = estimate_pose_norm_kpts(mask_kp0, mask_kp1)
        else:
            file_name = '/aucs_DLT.txt'
            e_hat = e_hat[-1].view(3, 3).cpu().detach().numpy()

            ret = estimate_pose_from_E(mkpts0, mkpts1, mask, e_hat)

        if ret is None:
            err_t, err_R = np.inf, np.inf
        else:
            R, t, inliers = ret
            R_gt, t_gt = test_data['Rs'], test_data['ts']
            T_0to1 = torch.cat([R_gt.squeeze(), t_gt.squeeze().unsqueeze(-1)], dim=-1).numpy()
            err_t, err_R = compute_pose_error(T_0to1, R, t)

        err_ts.append(err_t)
        err_Rs.append(err_R)

    # Write the evaluation results to disk.
    out_eval = {'error_t': err_ts,
                'error_R': err_Rs
                }

    pose_errors = []
    for idx in range(len(out_eval['error_t'])):
        pose_error = np.maximum(out_eval['error_t'][idx], out_eval['error_R'][idx])
        pose_errors.append(pose_error)

    thresholds = [5, 10, 20]
    aucs = pose_auc(pose_errors, thresholds)
    aucs = [100.*yy for yy in aucs]

    print('Evaluation Results (mean over {} pairs):'.format(len(test_loader)))
    print('AUC@5\t AUC@10\t AUC@20\t')
    print('{:.2f}\t {:.2f}\t {:.2f}\t'.format(aucs[0], aucs[1], aucs[2]))

    np.savetxt(opt.output_dir + file_name, np.asarray(aucs))
    return np.asarray(aucs)

if __name__ == '__main__':
    opt, unparsed = get_config()

    if not os.path.exists(opt.output_dir):
        os.makedirs(opt.output_dir)

    aucs = test(opt, thr=opt.thr, use_ransac=opt.use_ransac)