import math

import matplotlib.pyplot as plt
import numpy as np

class LeastSquares:
    def __init__(self, a, b, n, m, func):
        if a >= b:
            raise Exception("Invalid a,b values (a is more than b)")

        self.n = n
        self.m = m
        self.__calculate_a(a, b, func)

    @staticmethod
    def __phi(x, i):
        return math.pow(x, i)

    def __phi_k_phi_j(self, k, j, x):
        res = 0
        for i in range(self.n + 1):
            res += self.__phi(x[i], k) * self.__phi(x[i], j)
        return res

    def __f_phi_j(self, func, j, x):
        res = 0
        for i in range(self.n + 1):
            res += func(x[i]) * self.__phi(x[i], j)
        return res

    def __calculate_a(self, a, b, func):
        x = np.linspace(a, b, self.n + 1)
        matrix = [[0.0 for _ in range(self.m + 1)] for _ in range(self.m + 1)]
        for j in range(self.m + 1):
            for k in range(self.m + 1):
                matrix[j][k] = self.__phi_k_phi_j(k, j, x)

        res = []
        for j in range(self.m + 1):
            res.append(self.__f_phi_j(func, j, x))

        self.a = np.linalg.solve(matrix, res)

    def calc(self, x):
        res = 0
        for i in range(self.m + 1):
            res += self.a[i] * self.__phi(x, i)
        return res

def f(x):
    return x ** 2

def main():
    a, b, n, m = (-2, 2, 4, 1)
    N = n * 10

    ls = LeastSquares(a, b, n, m, f)

    x = [i for i in np.linspace(a, b, N)]

    y = [f(i) for i in x]
    plt.plot(x, y, linestyle='dashed', color='green')

    y = [ls.calc(i) for i in x]
    plt.plot(x, y)

    plt.show()

main()


import os
import sys
import datetime
from math import *
import numpy as np
import numpy.ma as ma

import netCDF4

import bounders

#---------------------------------------------------
#Gross bound checks on .nc files, developed primarily from the sea ice (CICE6) output
#Robert Grumbine
#30 January 2020
#
#data file = argv[1] (input)
#control dictionary = argv[2] (input)
#bootstrapped dictionary = argv[3] (optional, may be written to if needed and present)
#---------------------------------------------------

errcount = int(0)

if (not os.path.exists(sys.argv[1]) ):
  print("failure to find ",sys.argv[1])
  exit(1)
else:
  model = netCDF4.Dataset(sys.argv[1], 'r')
  nx = len(model.dimensions['ni'])
  ny = len(model.dimensions['nj'])

  #rg q: is this universal across UFS? -- no.
  tlons = model.variables["TLON"][:,:]
  tlats = model.variables["TLAT"][:,:]

  #LAND = 0, #Ocean = 1
  try:
    tmask = model.variables["tmask"][:,:]
  except :
    tmask = np.zeros((ny, nx))
    tmask = 1.
  tarea = model.variables["tarea"][:,:]

  #Get the dictionary file, perhaps with bounds given
  try:
    fdic = open(sys.argv[2])
  except:
    print("could not find a dictionary file ",sys.argv[2])
    exit(1)

  try:
    flying_dictionary = open(sys.argv[3],"w")
    flyout = True
  except:
    #debug print("cannot write out to bootstrap dictionary file")
    flyout = False

  parmno = 0
  for line in fdic:
    words = line.split()
    parm = words[0]
    tmp = bounders.bounds(param=parm)
    try:
      temporary_grid = model.variables[parm][0,:,:]
    except:
      print(parm," not in data file")
      continue

    # find or bootstrap bounds -----------------
    tmp.set_bounds(temporary_grid, words, flyout, flying_dictionary)

    #Global tests -- test whether the test fails anywhere
    gfail = tmp.whether(temporary_grid)

    #Pointwise checks -- Show where (and which) test failed:
    if (gfail):
      errcount += tmp.where(temporary_grid, tlats, tlons, tmask, tarea)

    parmno += 1

#exit codes are bounded, while error counts are not
if (errcount == 0):
  exit(0)
else:
  print("errcount = ",errcount)
  exit(1)


import string

from pyspark.sql import SparkSession
from pyspark.sql.functions import to_date, col, date_add, expr

spark = SparkSession.builder.master("local").appName("dealingwithdate").getOrCreate()

df = spark.read.csv("dealing*", header=True, inferSchema=True)
df.show()
df.printSchema()

df1 = df.withColumn("date_new",to_date(col("RechargeDate").cast("string"),"yyyyMMdd"))
df1.printSchema()
df1.show()

# df2 = df1.withColumn("Extended_warrenty", date_add("date_new","RemainingDays"))
df1.select("*").withColumn("Expiry_date", expr("date_add(date_new,RemainingDays)")).show()
# df2.show()


from parser.base_parser import BaseParser
from parser import utility as util
from os import path
import os
import json
from parser.base_parser import write_parsed_records

class JSONParser(BaseParser):

    def __init__(self, trans_info):
        super().__init__(trans_info)
        self.trans_info = trans_info

    def parse(self, input_path):
        util.validate_path(input_path)
        if path.isdir(input_path):
            raise Exception("folder is giving inplace of file")
        else:
            return self.parse_file(input_path)

    def parse_file(self, input_path):
        output_file_url = self.trans_info["trans_meta"]["output_folder_path"]
        filename, file_extension = os.path.splitext(input_path)
        output_file_url = output_file_url if output_file_url + "/" + filename else util.get_output_path(
            input_path)
        output_list = []
        chunk_size = 10000
        if "chunk_size" in self.trans_info["trans_meta"]:
            chunk_size = int(self.trans_info["trans_meta"]["chunk_size"])
        with open(input_path, 'r') as json_file:
            for line in json_file:
                json_row = json.loads(line)
                transformed_row = super(JSONParser, self).transform(json_row)
                output_list.append(transformed_row)
                if len(output_list) == int(chunk_size):
                    write_parsed_records(output_file_url, output_list)
                    output_list.clear()
        write_parsed_records(output_file_url, output_list)


from modules import *
import PySimpleGUI as psg

init_json()

theme = psg.theme('DarkPurple')

label = psg.Text('Type in a to-do')
input_box = psg.InputText(tooltip="Enter Todo", size=32, key='task_name')
icons = ['./media/edit.png', './media/plus.png', './media/minus.png']
list_box = psg.Listbox(values=get_task_list(), key='Tasks', enable_events=True, size=[30, 10])
edit = psg.Button(image_source=icons[0], size=2, key='Edit')
complete = psg.Button(image_source=icons[2], key='Remove')
add = psg.Button(image_source=icons[1], key='Add')
last_update = psg.Text(f"Last update: {get_human_readable_time(get_dictionary()['last_update'])}", key='last_update')
buttons = [[edit], [complete]]
right_column_layout = [[list_box]]
left_column = psg.Column(right_column_layout)
right_column = psg.Column(buttons)

window = psg.Window('My Tasks App',
                    layout=[
                        [label],
                        [input_box, add],
                        [[left_column, right_column]],
                        [last_update]
                    ],
                    font=('Roboto', 20))

while True:
    event, value = window.read(timeout=1000)

    if psg.WINDOW_CLOSED or event == psg.WIN_CLOSED:
        break

    try:
        window['last_update'].update(f"Last update: {get_human_readable_time(get_dictionary()['last_update'])}")
    except KeyError:
        pass

    if event == 'Add':
        if len(input_box.get().strip()) > 1:
            print(value)
            add_task(value["task_name"])
            window['Tasks'].update(values=get_task_list())
        else:
            psg.popup("Enter to-do title first.")

    if event == 'Edit':
        try:
            edit_task(value['Tasks'][0], value['task_name'])
            window['Tasks'].update(values=get_task_list())
        except IndexError:
            psg.popup("Select to-do first.")

    if event == 'Remove':
        confirm_layout = [
            [psg.Text("Are you sure?")],
            [psg.Button("Yes"), psg.Button("No")]
        ]
        confirm_window = psg.Window("Confirmation", confirm_layout)
        while True:
            confirm_event, confirm_values = confirm_window.read()
            if confirm_event == psg.WINDOW_CLOSED or confirm_event == "No":
                break
            elif confirm_event == "Yes":
                print(f'removing {value}')
                remove_task(value['Tasks'][0])
                window['Tasks'].update(values=get_task_list())
                break

        confirm_window.close()

window.close()


import tensorflow as tf
import keras.backend as K

tf.reverse
sess = tf.Session()
input = tf.constant([[[1, 1, 1], [2, 2, 2]],
                     [[3, 3, 3], [4, 4, 4]],
                     [[5, 5, 5], [6, 6, 6]]])
data = tf.slice(input, [1, 0, 0], [3, 2, 3])
print(sess.run(data))


import tweepy
import json
from random import randint
from time import sleep

class composeMessage:
    def ph1(self, w1dic, w2dic):
        w1_lst = w1dic[randint(0, len(w1dic) - 1)]
        w2_lst = w2dic[randint(0, len(w2dic) - 1)]
        try:
            temp = w1_lst[2]
            temp = w2_lst[1]
            while w1_lst[1] == w2_lst[1]:
                print(w2_lst[0])
                w2_lst = w2dic[randint(0, len(w2dic) - 1)]
        except IndexError:
            pass
        word1 = w1_lst[0]
        word2 = w2_lst[0]
        if len(w1_lst) == 3:
            atcl = w1_lst[2]
        else:
            atcl = w1_lst[1]
        return 'You are ' + atcl + ' ' + word1 + ' ' + word2 + '.'
composeMessage = composeMessage()

def sleeptimer(secs):
    i = 0
    while i < secs:
        sleep(1)
        print(str(secs - i) + ' seconds remaining...')
        i += 1

user = True

tw_apiinfo = json.load(open('api_info.json'))

auth = tweepy.OAuthHandler(tw_apiinfo['ckey'], tw_apiinfo['csec'])
auth.set_access_token(tw_apiinfo['atkey'], tw_apiinfo['atsec'])

api = tweepy.API(auth, wait_on_rate_limit = True, wait_on_rate_limit_notify = True, compression = True)

while True:
    dictionary = json.load(open('dictionary.json'))
    users = json.load(open('accounts.json'))['account_list']
    w1dic = []
    w2dic = []
    for item in dictionary['words_l1']:
        w1dic.append([item[0], dictionary['words_l1'].index(item),item[2]])
        w2dic.append([item[1], dictionary['words_l1'].index(item)])
    for item in dictionary['words_l1_adj']:
        w1dic.append([item[0], item[1]])
    for item in dictionary['words_l1_noun']:
        w2dic.append([item])
    if user:
        message = '@' + users[randint(0, len(users) - 1)] + ' ' + composeMessage.ph1(w1dic, w2dic)
    else:
        message = composeMessage.ph1(w1dic, w2dic)
    try:
        api.update_status(message)
        print('Sent message "' + message + '"')
    except:
        print('Failed to update status')
        break
    sleeptimer(12 * 60 * 60) # 12hrs * 60mins * 60secs


import os

source_floder='/home/hjm/t/'
dest = '/home/hjm/t2.txt'

file_list = os.listdir(source_floder)
file_list = sorted(file_list)

obj = ""
for file in file_list:
    obj += file.strip('.jpg')+'\n'
    # print(obj)

with open(dest,'w') as w:
        w.write(obj)
w.close()


import pandas as pd
from ete3 import NCBITaxa
import sys
input_file = sys.argv[1]
output = open(sys.argv[2], "w")

#given a file with one taxID per row, output the complete taxonomy.
taxids = open(input_file).readlines()
lista=[]
for item in taxids:

    ncbi =NCBITaxa()
    lineage = ncbi.get_lineage(item)
    names = ncbi.get_taxid_translator(lineage)
    lista.append([names[taxid] for taxid in lineage])

data = pd.DataFrame(lista)
data.to_csv(output, sep="\t", index=False, header=None)


import re
import MeCab
from bs4 import BeautifulSoup
from urllib import request

main_url = "https://www.aozora.gr.jp/cards/000148/files/2371_13943.html"
stop_url = "http://svn.sourceforge.jp/svnroot/slothlib/CSharp/Version1/SlothLib/NLP/Filter/StopWord/word/Japanese.txt"

main_response = request.urlopen(main_url)
main_soup = BeautifulSoup(main_response)

stop_response = request.urlopen(stop_url)
stop_soup = BeautifulSoup(stop_response)

main_response.close()
stop_response.close()

main_text = main_soup.find("div", class_ = "main_text")
main_tags_delete = main_text.find_all(["rp", "rt"])

for tag in main_tags_delete:
    tag.decompose()

main_text = main_text.get_text()
main_text = re.sub(r"[\u3000 \n \r]", "", main_text)

stop_text = stop_soup.text
stoptext_list = stop_text.split("\r\n")
stoptext_list = [word for word in stoptext_list if word]

mecab_tagger = MeCab.Tagger("-Owakati")
main_text = mecab_tagger.parse(main_text)

main_text_list = main_text.split(" ")
result_text_list = list()

for split_text in main_text_list:
    if split_text not in stoptext_list:
        result_text_list.append(split_text)

print(result_text_list)


# python3

from collections import namedtuple

Bracket = namedtuple("Bracket", ["char", "position"])

def are_matching(left, right):
    return (left + right) in ["()", "[]", "{}"]

def find_mismatch(text):
    opening_brackets_stack = []
    for i, next in enumerate(text):
        if next in "([{":
            # Process opening bracket, write your code here
            opening_brackets_stack.append(Bracket(next, i))

        if next in ")]}":
            # Process closing bracket, write your code here
            if len(opening_brackets_stack) == 0:
                return i + 1
            else:
                top_char, _ = opening_brackets_stack.pop()
                if not are_matching(top_char, next):
                    # Found unmatched closing bracket without opening bracket
                    return i + 1

    if len(opening_brackets_stack) == 0:
        return "Success"
    else:
        # Find first unmatched opening bracket
        _, first_position = opening_brackets_stack[0]
        return first_position + 1

def main():
    text = input()
    mismatch = find_mismatch(text)
    # Printing answer, write your code here
    print(mismatch)

if __name__ == "__main__":
    main()


# author   : Johann-Mattis List
# email    : mattis.list@uni-marburg.de
# created  : 2015-05-05 16:04
# modified : 2015-05-05 16:04
"""
<++>
"""

__author__="Johann-Mattis List"
__date__="2015-05-05"

from lingpyd import *
from lingpyd.plugins.lpserver.lexibase import LexiBase

db = LexiBase('sinotibetan', dbase='sinotibetan.sqlite3',
        #url = 'http://tsv.lingpy.org/triples/sinotibetan.sqlite3'
        )

# get the concepts
concepts = [m[1] for m in csv2list('matches.tsv')]
# calculate coverage
corage = {}
ff = open('coverage.csv', 'w')
ff.write('DOCULECT\tPRESENT\tMISSING\tCOVERAGE\n')

# get concepts
new_concepts = [w[1] for w in csv2list('stdb.concepts.csv') if w[3] == 'NEW']
for t in db.taxa:
    d = db.get_dict(taxon=t)
    present = [k for k in d if k in concepts]
    missing = [k for k in concepts if k not in d]
    c = len(present)
    corage[t] = (c,c/len(concepts),missing)
    if c < 220:
        print(t,c)

        with open('taxa/'+t+'.csv', 'w') as f:
            f.write('NUMBER\tWORD\n')
            count = 1
            for i,m in enumerate(missing):
                if m in new_concepts:
                    f.write(str(count)+'\t'+m+'\n')
                    count += 1
    ff.write('{0}\t{1}\t{2}\t{3:.2f}\n'.format(
        t,
        c,
        len(missing),
        100 * c/len(concepts)))
ff.close()

db.output('tsv', filename='stdb-2015-05-06')


import json
import boto3
from botocore.exceptions import ClientError

credentialDurationInSec = 3600

def createRoleAlias(props):
    alias_role_name = str(props['IoTRoleAliasName'])
    token_role_arn = str(props['TokenRoleARN'])
    print('Role creation: requested', alias_role_name)

    try:
        client = boto3.client('iot')
        response = client.create_role_alias(
            roleAlias=alias_role_name,
            roleArn=token_role_arn,
            credentialDurationSeconds=credentialDurationInSec
        )
        print('Role creation: success', response)
        return response['roleAlias']
    except Exception as e:
        print("Role creation: fail", e)
        return None

def deleteRoleAlias(role_alias_name):
    print("Role deletion : requested", role_alias_name)

    try:
        client = boto3.client('iot')
        client.delete_role_alias(
            roleAlias=role_alias_name
        )
        print("Role deletion : success")
        return role_alias_name
    except Exception as e:
        print("Role deletion: ", e)
        return None

def handle(event, context):
    print('event-->', json.dumps(event))

    request_type = event['RequestType']
    physical_id = None

    if request_type == 'Create':
        physical_id = createRoleAlias(event['ResourceProperties'])
    elif request_type == 'Delete':
        physical_id = deleteRoleAlias(event['PhysicalResourceId'])
    elif request_type == 'Update':
        deleteRoleAlias(event['PhysicalResourceId'])
        physical_id = createRoleAlias(event['ResourceProperties'])

    if physical_id != None:
        return { 'PhysicalResourceId': physical_id }
    else:
        raise Exception('Fail to handl IoTRoleAlias')


"""
Run `tox -s (session-name)` to run a suite.
Run `tox` to run all suites.

If you are contributing, please run all suites and fix all
errors and warning before submitting a pull request.
"""
import os

import nox

@nox.session
def test(session):
    """
    Run all test files in the project.
    """
    if os.environ.get("DJANGO_SECRET_KEY") is None:
        session.error("""
Please set the DJANGO_SECRET_KEY variable
Use the following if you\'re just developing:
  export DJANGO_SECRET_KEY=$(openssl rand -hex 16)""")

    session.install("-r", "requirements/testing.txt")
    session.run(
        "coverage",
        "run",
        "-m",
        "pytest",
        env={"DJANGO_SETTINGS_MODULE": "config.settings.testing"},
    )
    session.run("coverage", "report", "-m")

@nox.session
def fix(session):
    """
    Recursively formats files specified in the `modules` tuple.
    black:  Standardizes all files to a strict style
    yapf:   Makes all files conform to PEP8
    """
    modules = ("dinedash", "config", "manage.py")
    session.install("black", "yapf")
    session.run("black", *modules)
    session.run("yapf", "-i", "--recursive", *modules)

@nox.session
def lint(session):
    """
    Recursively formats files specified in the `modules` tuple.
    flake8: Checks for PEP8 syntax errors
    pylint: Checks for stylistic and logical syntax errors
    """
    modules = ("dinedash", )
    session.install("flake8", "pylint")
    session.run("flake8", *modules)
    # To ignore Django errors, pylint_django needs Django and factory_boy
    session.install("pylint-django", "django", "factory_boy", "shortuuid")
    session.run("pylint", "--load-plugins", "pylint_django", *modules)


from unittest import TestCase, mock
from WarGame.deckofcard import DeckOfCard

class TestDeckOfCard(TestCase):

    def setUp(self):
        self.obj_deck = DeckOfCard()

    def test__init__(self):
        # 4 suit with one of each value [2,3,4,5,6,7,8,9,10,'Jack', 'Queen', 'King', 'Ace']
        # card structure is: ['suit','value']
        list_club = []
        list_heart = []
        list_diamond = []
        list_spade = []
        for i in self.obj_deck.cards:
            if i[0] == 'Heart':
                list_heart.append(i[1])
            if i[0] == 'Spade':
                list_spade.append(i[1])
            if i[0] == 'Diamond':
                list_diamond.append(i[1])
            if i[0] == 'Club':
                list_club.append(i[1])
        self.assertTrue(list_club == [2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King', 'Ace'])
        self.assertTrue(list_club == list_heart == list_spade == list_diamond)

    def test_cards_shuffle(self):
        self.obj_deck.cards = [['card1'], ['card2'], ['card3'], ['card4']]
        self.obj_deck.cards_shuffle()
        self.assertFalse(self.obj_deck.cards == [['card1'], ['card2'], ['card3'], ['card4']])

    def test_deal_one(self):
        self.obj_deck.cards = [['card1'], ['card2'], ['card3'], ['card4']]
        self.obj_deck.deal_one()
        self.assertTrue(len(self.obj_deck.cards) == 3)


"""
This is the solution for problem set 1 b for MIT 6.001
all the test point given is revealed correct
"""

# let the user do the input
annual_salary = int(input("please give us your annual salary:"))
portion_saved = float(input("please give us your planned portion of salary to save:"))
portion_of_semi_annual_salary_raise = float(input("please give us the portion for your semi-annual raise:"))
total_cost = int(input("please enter the price of your dream house:"))

#set some unchanged parameters
annually_interest_rate = 0.04
monthly_salary = annual_salary/12
current_saving = 0.0
portion_down_payment = 0.25
total_month = 0

#other parameters that need further calculation
down_payment = portion_down_payment*total_cost
monthly_saving = monthly_salary*portion_saved

#using an explicit loop to recursivly add the saving and month
while(current_saving<down_payment):
    total_month += 1
    if (total_month)%6 == 0:
        monthly_saving *= (1+portion_of_semi_annual_salary_raise)
    monthly_interest_revenue = current_saving * annually_interest_rate / 12
    current_saving += monthly_saving + monthly_interest_revenue

print(total_month+1)


from unittest import mock

from django.conf import settings
from django.template import Context, Template

from sitedata.templatetags.sitedata import fqdn_format
from sitedata.utils import SiteData

def test_url_fqdn():
    # Expect a FQDN to work for a given site in context
    template = (
        '{% load sitedata %}'
        '{% url_fqdn "search" %}'
    )
    request = mock.Mock()
    request.sitedata = SiteData(label='nextminingboom')
    rendered = Template(template).render(Context({'request': request}))
    assert rendered == 'http://www.nextminingboom.com/search/'

    # Expect a FQDN stored in an 'as' variable
    template = (
        '{% load sitedata %}'
        '{% url_fqdn "search" as foo %}'
        '{{foo}}'
    )
    request = mock.Mock()
    request.sitedata = SiteData(label='nextminingboom')
    rendered = Template(template).render(Context({'request': request}))
    assert rendered == 'http://www.nextminingboom.com/search/'

    # Expect a FQDN with fallback to the default site when no context
    template = (
        '{% load sitedata %}'
        '{% url_fqdn "search" sitedata="catalysthunter" %}'
    )
    rendered = Template(template).render(Context({}))
    assert rendered == 'http://catalysthunter.com/search/'

    # Expect a FQDN for the site specified in the tag params
    template = (
        '{% load sitedata %}'
        '{% url_fqdn "search" sitedata="nextinvestors" %}'
    )
    request = mock.Mock()
    request.sitedata = SiteData(label='catalysthunter')
    rendered = Template(template).render(Context({'request': request}))
    assert rendered == 'http://www.nextinvestors.com/search/'

def test_with_fqdn():
    # Expect FQDN for paths to work for a given site
    template = (
        '{% load sitedata %}'
        '{% with_fqdn "/foo/bar/" %}'
    )
    request = mock.Mock()
    request.sitedata = SiteData(label='nextminingboom')
    rendered = Template(template).render(Context({'request': request}))
    assert rendered == 'http://www.nextminingboom.com/foo/bar/'

    # Expect FQDN for paths to fallback to the default site
    template = (
        '{% load sitedata %}'
        '{% with_fqdn "/foo/bar/" %}'
    )
    rendered = Template(template).render(Context({}))
    assert rendered == 'http://catalysthunter.com/foo/bar/'

    # Expect FQDN for file paths
    template = (
        '{% load sitedata %}'
        '{% with_fqdn "/foo/bar/baz.jpg" %}'
    )
    rendered = Template(template).render(Context({}))
    assert rendered == 'http://catalysthunter.com/foo/bar/baz.jpg'

    # Expect FQDN for paths with no prefixed '/'
    template = (
        '{% load sitedata %}'
        '{% with_fqdn "foo/bar/" %}'
    )
    rendered = Template(template).render(Context({}))
    assert rendered == 'http://catalysthunter.com/foo/bar/'

def static_with_fqdn():
    """
    Expect the tag to use the default sitedata when no request
    context provided (catalysthunter.com)
    """
    template = (
        '{% load sitedata %}'
        '{% static_with_fqdn path %}'
    )
    asset_path = settings.SITEDATA['catalysthunter']['og_image']['path']
    rendered = Template(template).render(Context({'path': asset_path}))
    assert rendered == 'http://catalysthunter.com/static/{}'.format(asset_path)

def test_sitedata_title():
    template = (
        '{% load sitedata %}'
        '{% sitedata_title "Woo" %}'
    )
    request = mock.Mock()
    request.sitedata = SiteData(label='catalysthunter')
    rendered = Template(template).render(Context({'request': request}))
    assert rendered == request.sitedata.get_title('Woo')

def test_fqdn_format():
    # Normal case
    expected = 'http://example.com/foo/'
    actual = fqdn_format('http', 'example.com', '/foo/')
    assert expected == actual

    # No prefixed slash on path
    expected = 'http://example.com/foo/'
    actual = fqdn_format('http', 'example.com', 'foo/')
    assert expected == actual

    # Only use path if path is a FQDN
    expected = 'http://example.com/foo/'
    actual = fqdn_format('https', 'wrong.net', 'http://example.com/foo/')
    assert expected == actual


#suma y resta (+ -)
suma = 2+2
resta = 2-2

#Multiplicacion (* /)
multi = 2*2
division = 2/2 #(devuelve float)

#potenciacion (elevado a)
exponente = 12**5

#division baja (//)
division_baja=2//2 #devuelve entero redondeado hacia abajo

#modulo
modulo = 12%2

tipo_de_dato=type([1,2,3]) #type -> Nos muestrta el tipo de dato

print(tipo_de_dato)


def findQualities(mat, i,j):
	if i<0 or i>=n or j<0 or j>=m or mat[i][j] == 0:
		return 0
	else:
		return mat[i][j]

n,m = list(map(int, input().split()))
mat = []
for i in range(n):
	mat.append(list(map(int, input().split())))
rp = [(1,0), (-1,0), (0,1), (0,-1), (-1,-1), (1,-1), (-1,1), (1,1)]
b = [[0 for j in range(m)] for i in range(n)]
mat[0][0] = 0
maxq = -99999
for i in range(n):
	for j in range(m):
		if mat[i][j] == 1:
			nc = 0
			for p in rp:
				nc += findQualities(mat, i+p[0], j+p[1])
			# print(nc, '({},{})'.format(i+1, j+1))
			maxq = max(maxq, nc)
			b[i][j] = nc
brideFound = False
for i in range(len(b)):
	for j in range(len(b[0])):
		if b[i][j] == maxq:
			print('{}:{}:{}'.format(i+1,j+1,maxq))
			brideFound = True
			break
	if brideFound == True:
		break
if brideFound == False:
	print('No suitable girl found')


import uuid
from neuron import h
import warnings
from .guitools import make_callable

import logging
logging.basicConfig(level=logging.DEBUG, filename="mylog.txt")

make_browser_html = None

class Widget:
    def to_html(self):
        raise NotImplementedError()
    def mappings(self):
        return {}

class XValue(Widget):
    def __init__(self, prompt, variable, boolean_deflt, action, boolean_canrun, context):
        self.prompt = prompt
        if (isinstance(variable, str)):
            self.ptr = getattr(h, "_ref_" + variable)
        else:   #TODO: if not pointer
            self.ptr = variable
        self.uuid = uuid.uuid4().hex    # uuid4 because prob more secure than uuid1

        if boolean_deflt:
            # TODO add a checkbox that is checked when value is not at default
            # involves a state variable for checkbox and storing original value somewhere
            warnings.warn("Checkbox tied to xvalue not yet implemented.")
        self.callback = None
        if action is not None:
            self.uuid2 = uuid.uuid4().hex
            self.callback = make_callable(callback, context)
        if boolean_canrun:
            # TODO change the label to a button, callback when either button is pressed or value is changed
            warnings.warn("Xvalue callback button appearances not yet implemented.")

    def mappings(self):
        if self.callback:
            return{self.uuid: self.ptr, self.uuid2: self.callback}
        else:
            return {self.uuid: self.ptr}

    def to_html(self):
        if self.callback:
            return """<input type="number" data-variable="{}" data-onenter="{}"> {}""".format(self.uuid, self.uuid2, self.prompt)
        else:
            return """<input type="number" data-variable="{}"> {}""".format(self.uuid, self.prompt)

class XCheckBox(Widget):
    def __init__(self, prompt, state_variable, callback, context):
        self.prompt = prompt
        self.state_ref = state_variable
        self.callback = make_callable(callback, context)

        self.uuid = uuid.uuid4().hex
        self.uuid2 = uuid.uuid4().hex

    def mappings(self):
        if self.callback:
            return {self.uuid: self.state_ref, self.uuid2: self.callback}
        else:
            return {self.uuid: self.state_ref}

    def to_html(self):
        if self.callback:
            return """<input type="checkbox" data-variable="{}" data-onclick="{}"> {}""".format(self.uuid, self.uuid2, self.prompt)
        else:
            return """<input type="checkbox" data-variable="{}"> {}""".format(self.uuid, self.prompt)

class XStateButton(Widget):
    def __init__(self, prompt, state_variable, callback, context):
        self.prompt = prompt
        self.state_ref = state_variable
        self.callback = make_callable(callback, context)

        self.uuid = uuid.uuid4().hex
        self.uuid2 = uuid.uuid4().hex

    def mappings(self):
        if self.callback:
            return {self.uuid: self.state_ref, self.uuid2: self.callback}
        else:
            return {self.uuid: self.state_ref}

    def to_html(self):
        if self.callback:
            return """<button class="state" data-variable="{}" data-onclick="{}">{}</button>""".format(self.uuid, self.uuid2, self.prompt)
        else:
            return """<button class="state" data-variable="{}">{}</button>""".format(self.uuid, self.prompt)

class XButton(Widget):
    def __init__(self, prompt, callback, context):
        self.prompt = prompt
        self.callback = make_callable(callback, context)
        self.uuid = uuid.uuid4().hex

    def mappings(self):
        return {self.uuid: self.callback}

    def to_html(self):
        return """<button data-onclick="{}">{}</button>""".format(self.uuid, self.prompt)

class XLabel(Widget):
    def __init__(self, text):
        self.text = text

    def mappings(self):
        return {}

    def to_html(self):
        return "{}".format(self.text)

class XVarLabel(Widget):
    def __init__(self, strref):
        self.strref = strref
        self.uuid = uuid.uuid4().hex

    def mappings(self):
        return {self.uuid: self.strref}

    def to_html(self):
        return """ """.format(self.uuid)

class Container(Widget):
    def __init__(self):
        self.widgets = []
        self.orientation = 'vertical'

    def to_html(self):
        if self.orientation == 'horizontal':
            return '

' + '

'.join(widget.to_html() for widget in self.widgets) + '

'
        else:
            return '

' + '

'.join(widget.to_html() for widget in self.widgets) + '

'

    def add(self, item):
        self.widgets.append(item)

    def mappings(self):
        result = {}
        for widget in self.widgets:
            result.update(widget.mappings())
        return result

class Window(Container):
    def __init__(self, title):
        Container.__init__(self)
        self.title = title

class HBox(Container):
    def __init__(self):
        Container.__init__(self)
        self.orientation = 'horizontal'

    def intercept(self, value):
        if value:
            active_container.append(self)
            return 1
        elif active_container[-1] != self and not value:
            # do nothing
            pass
        else:
            active_container.pop()
            return 1

    def map(self):
        if not active_container:
            raise Exception('not currently supporting top-level HBox')

        if active_container[-1] == self:
            raise Exception('can only map to different container')

        active_container[-1].add(self)
        return 1

class VBox(Container):
    def __init__(self):
        Container.__init__(self)
        self.orientation = 'vertical'

    def intercept(self, value):
        if value:
            active_container.append(self)
            return 1
        elif active_container[-1] != self and not value:
            # do nothing
            pass
        else:
            active_container.pop()
            return 1

    def map(self):
        if not active_container:
            raise Exception('not currently supporting top-level VBox')

        if active_container[-1] == self:
            raise Exception('can only map to different container')

        active_container[-1].add(self)
        return 1

active_container = []

active_window = None

def xpanel(*args, context=None):
    global active_window
    if args and active_window is not None:
        raise Exception('not currently allowing nesting xpanels')
    if not args and active_window is None:
        raise Exception('no active xpanel')
    if args:
        active_window = Window(args[0])
        active_container.append(active_window)
    else:
        html = active_window.to_html()
        logging.debug("mappings: "+str(active_window.mappings()))
        make_browser_html(html, user_mappings=active_window.mappings(), title=active_window.title)
        active_window = None
    return 0

def xvalue(prompt, variable, boolean_deflt=None, action=None, boolean_canrun=None, context=None):
    active_container[-1].add(XValue(prompt, variable, boolean_deflt, action, boolean_canrun, context))
    return 0

def xcheckbox(prompt, state_variable, callback=None, context=None):
    active_container[-1].add(XCheckBox(prompt, state_variable, callback, context))
    return 0

def xstatebutton(prompt, state_variable, callback=None, context=None):
    active_container[-1].add(XStateButton(prompt, state_variable, callback, context))
    return 0

def xlabel(text, context=None):
    active_container[-1].add(XLabel(text))
    return 0

def xbutton(prompt, callback, context=None):
    active_container[-1].add(XButton(prompt, callback, context))
    return 0

def xvarlabel(strref, context=None):
    active_container[-1].add(XVarLabel(strref))
    return 0

class Graph(Widget):
    def __init__(self):
        self.labels = {}
        self.var_mappings = {}
        active_container[-1].add(self)

    def __repr__(self):
        return 'Graph'

    def mappings(self):
        return self.var_mappings

    def addvar(self, label, var=None):
        default_lab = False
        if var is None:
            var = label
            default_lab = True
        current_uuid = uuid.uuid4().hex
        if (isinstance(var, str)):  #retrieve cas
            spl = var.split('(')
            if len(spl) == 1:
                seg = 0.5   #default
            else:
                seg = float(spl[1][:-1])
            ptr = getattr(h.cas()(seg), "_ref_" + spl[0])
            self.var_mappings.update({current_uuid: ptr})
            if default_lab:
                label = spl[0]
        else:   #TODO: if not a ptr
            self.var_mappings.update({current_uuid: var})
        self.labels.update({current_uuid: label})
        logging.debug("added graph var: "+label)
        return 1

    def to_html(self):
        if not self.var_mappings:
            return '

'
        else:
            return """

""".format(";".join(self.var_mappings.keys()), ";".join(self.labels.values()))


'''
#Author: Abdul Azeez Omar
#Date: Feb 2018
#Purpose: A tool that will start with a given URL and extract all links from that webpage.
'''
from bs4 import BeautifulSoup
from urllib import request
import getopt, sys
from urllib.parse import urlparse
from symbol import except_clause

def getHelp():
    print('\nUsage: ' + sys.argv[0] + " targeSite -o save.txt\n")
    print("-h  to get help")
    print("-t --target to assign the target")
    print("-l --limit to limit the search range")
    print("-m --output only the URLs of pages within the domain and not broken")
    print("-o --to save the result in a file\n")
    exit(1)

# extract root domain of an url
def get_root_domain(url):
    r = urlparse(url)
    tmp = r.netloc
    parts = tmp.split('.')
    if len(parts) < 2:
        return None

    return parts[-2] + '.' + parts[-1]

# output report to stdout or file
def outputReport(fout, str):
    if fout != None:
        fout.write(str + '\n')
    else:
        print(str)

# request header
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
       'Accept-Encoding': 'none',
       'Accept-Language': 'en-US,en;q=0.8',
       'Connection': 'keep-alive'}

# process arguments
try:
    opts, args = getopt.getopt(sys.argv[1:], "mht:o:l:", ["help", "ofile=", "limit="])
except getopt.GetoptError:
    print('Wrong argument, use -h to get help')
    exit()

# flags for options
targeSite = None
output2File = False
outFileName = None
limitSearch = False
limitDomain = None
validLinksOnly = False

for name,value in opts:
    if name in ('-h', '--help'):
        getHelp()
    if name in ('-o', '--ofile'):
        output2File = True
        outFileName = value
    if name in ('-l', '--limit'):
        limitSearch = True
        limitDomain = value
    if name in ('-m'):
        validLinksOnly = True
    if name in ('-t'):
        targeSite = value
localLinks = []
foreignLinks = []
visited = []
broken = []

if targeSite == None:
    print('No target website is given, use -h to get help')
    exit(1)

# add the given URL to the local links collection
localLinks.append(targeSite)
# get root domain
rootDomain = get_root_domain(targeSite)

while len(localLinks) != 0:
    tmpLink = localLinks.pop()
    visited.append(tmpLink)
    req = request.Request(tmpLink, headers=hdr)
    try:
        response = request.urlopen(req)
    except:
        broken.append(tmpLink)
        continue
    # read html page
    html_doc = response.read()
    # parse the website
    soup = BeautifulSoup(html_doc, "html.parser")
    # get all links
    for link in soup.find_all('a'):
        newLink = link.get('href')
        newLink = str(newLink)
        if newLink.startswith('http') or newLink.startswith('https'):
            if get_root_domain(newLink) == rootDomain:
                if limitSearch:
                    # not visited local links
                    if limitDomain in newLink and newLink not in visited:
                        localLinks.append(newLink)
                elif newLink not in visited:
                    localLinks.append(newLink)
            # foreign link
            else:
                foreignLinks.append(newLink)

fout = None
if output2File:
    fout = open(outFileName, 'w')
# output report
if validLinksOnly:
    for l in visited:
        if l not in broken:
            outputReport(fout, l)
else:
    outputReport(fout, '\n\x1b[6;30;42m' + 'Local links: ' + '\x1b[0m')
    for l in visited:
        if l not in broken:
            outputReport(fout, l + '    OK')
        else:
            outputReport(fout, l + '    BROKEN')
    outputReport(fout, '\n\x1b[6;30;42m' + 'Foreign links: ' + '\x1b[0m')
    for l in foreignLinks:
        outputReport(fout, l)


import distutils.spawn
import logging
import unittest
from typing import Optional
import os

import easyprocess
from flask import url_for as flask_url_for
from flask_testing import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

from labonneboite.conf import settings
from labonneboite.web.app import app

@unittest.skipUnless('SELENIUM_IS_SETUP' in os.environ, 'Selenium database is not defined as setup')
class LbbSeleniumTestCase(LiveServerTestCase):
    """
    Sets up the environment for Selenium tests and exposes
    various properties.

    https://docs.python.org/2/library/unittest.html
    http://selenium-python.readthedocs.io/index.html
    https://pythonhosted.org/Flask-Testing/#testing-with-liveserver
    """

    # Note that this test case expects a working SQL and Elasticsearch database

    def create_app(self):
        # Override settings
        settings.API_ADRESSE_BASE_URL = 'https://api-adresse.data.gouv.fr'
        settings.API_DEPARTMENTS_URL = 'https://geo.api.gouv.fr/departements'

        # Random port generation
        app.config['LIVESERVER_PORT'] = 8943
        app.config['SERVER_NAME'] = None
        app.config['TESTING'] = True
        # Disable logging
        app.logger.setLevel(logging.CRITICAL)
        logging.getLogger('werkzeug').setLevel(logging.CRITICAL)
        logging.getLogger('easyprocess').setLevel(logging.CRITICAL)
        return app

    def setUp(self):
        super(LbbSeleniumTestCase, self).setUp()
        self.display = None
        try:
            from pyvirtualdisplay import Display
            display = Display(visible=0, size=(1600, 1600))
            display.start()
        except easyprocess.EasyProcessCheckInstalledError:
            # On some unreliable and exotic OS (such as Mac OS) installing a
            # headless display server such as Xvfb is highly non-trivial. In
            # those cases, we just run Chrome. It's fun and allows the user to
            # view the tests running in real time.
            print("Xvfb is not available. Running selenium tests in non-virtual display.")

        # Chromedriver is often in /usr/lib/chromium-browser/chromedriver
        chromedriver_path = distutils.spawn.find_executable('chromedriver')
        if not chromedriver_path:
            chromedriver_path = distutils.spawn.find_executable('chromedriver', path='/usr/lib/chromium-browser/')
        if not chromedriver_path:
            raise RuntimeError('Missing chromedriver executable. Did you install the chromium-chromedriver package?')

        # Configure logging
        capabilities = DesiredCapabilities.CHROME
        capabilities['loggingPrefs'] = {'browser': 'ALL'}

        chrome_options = Options()
        # chrome_options.add_argument("--disable-extensions")
        # chrome_options.add_argument("--disable-gpu")
        chrome_options.add_argument("--headless")

        self.driver = webdriver.Chrome(
            desired_capabilities=capabilities,
            executable_path=chromedriver_path,
            options=chrome_options,
        )

        # Ensure that the window size is large enough so that HTML elements won't overlap.
        self.driver.set_window_size(1600, 1200)

        # Implicitely wait at most 10 seconds when trying to select an element
        # that does not exist yet.
        self.driver.implicitly_wait(10)

    def tearDown(self):
        self.print_js_logs()
        super(LbbSeleniumTestCase, self).tearDown()
        self.driver.quit()
        if self.display:
            self.addCleanup(self.display.stop)

    def url_for(self, endpoint, **kwargs):
        """
        A small helper to generate a URL to the given endpoint in the context of `self.app_context`.
        """
        with app.app_context():
            url = flask_url_for(endpoint, **kwargs)
            return self.get_server_url() + url

    def print_js_logs(self):
        # Convenient utility to print client-side logs
        for entry in self.driver.get_log('browser'):
            print(entry)

    @property
    def focusElement(self):
        return self.driver.switch_to.active_element

    def assertElementIsFocus(self, element, msg: Optional[str] = None):
        assertion_func = self._getAssertEqualityFunc(self.focusElement, element)
        assertion_func(self.focusElement, element, msg=msg)

def url_has_changed(current_url):
    def check(driver):
        return driver.current_url != current_url
    return check


import nltk
from nltk.corpus import brown

govt_text = brown.words(categories='government')
fdist = dict(nltk.FreqDist([w.lower() for w in govt_text]))

models = 'wh'

for k in fdist.keys():
    if k.startswith(models):
        print(k, ':', fdist[k])


import logging
from typing import Any, Type

from pydantic import BaseModel, ValidationError

def try_valparse(
    raw_data: dict[str, Any], datamodel: Type[BaseModel]
) -> tuple[dict[str, Any], bool]:
    """
    Attempts data validation and type parsing using Pydantic models. Logs validation
    errors upon failure.

    Args:
        raw_data: raw data to validate
        datamodel: Pydantic Model to use for validation

    Returns:
        A tuple of either parsed or raw data, and a boolean indicating if
        validation and parsing were successful.
    """
    valparse = False
    try:
        parsed_data = datamodel.parse_obj(raw_data).dict()
        valparse = True
        logging.info(
            f"Validated and parsed raw data according to {datamodel.__name__} model"
        )
        return parsed_data, valparse
    except ValidationError as e:
        logging.error(
            f"Schema inconsistency detected between {datamodel.__name__} model. Unable"
            f" to validate and parse raw data:\n {e}"
        )
    return raw_data, valparse


import cv2
import imutils
from imutils import perspective
#from imutils.video import count_frames
import numpy as np
from matplotlib import pyplot as plt
import glob

#WIDTH, HEIGHT = 100, 100

class utils(object):

    def getSpotsCoordiantesFromImage(img, num_space) :
        #coordinate_lists has this format[ [(x1,y1), (x2,y2), (x3,y3), (x4,y4)], [], [] ]
        coordinate_lists = []
        spots_index_list = []
        for i in range(num_space):
            plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
            plt.xticks([]), plt.yticks([])
            #we need 4 points to get rectangle
            print("Please click 4 points for parking lot in clock direction", i)
            coordinate = plt.ginput(4)
            print("clicked points coordinate are ", coordinate)
            coordinate_lists.append(coordinate)
            spots_index_list.append(i)
        plt.close()
        #saveSpotsCoordinates(coordinate_lists)
        #saveSpotsIndex(spots_index_list)
        return coordinate_lists

    ''' rotaciona imagens '''
    def getRotateRect(img, cooridnate_lists, WIDTH = 100, HEIGHT = 100):
        #warped image list is the list with warper images
        warped_img_lists = []
        i = 0
        #every time we process one coordinates
        for coordinate in cooridnate_lists :
            warped = perspective.four_point_transform(img, coordinate)
            warped_resize = cv2.resize(warped, (WIDTH, HEIGHT), interpolation=cv2.INTER_CUBIC)

            # plt.imshow(warped, cmap = 'gray', interpolation = 'bicubic')
            # plt.xticks([]), plt.yticks([])
            # plt.show()
            cv2.imshow("resize %d"%i, warped_resize)

            warped_img_lists.append(warped_resize)

            i+=1
        return warped_img_lists

    def load_images_from_path(path):

        lista_imagens = []

        img_list = []
        for files in glob.glob(folderPath + "/*.jpg"):
            img = cv2.imread(files)
            img_list.append(img)
        return img_list


import os
import sys
import tarfile
import time
import ieugwaspy
import pandas as pd
from datetime import datetime

__version__ = "0.3.0"

# Versions of ontologies and the resulting search database
EFO_VERSION = "3.57.0"
UBERON_VERSION = "2023-07-25"
SEARCH_DB_VERSION = "0.10.0"

DATASET_NAME = "opengwas"
OUTPUT_DATABASE_FILEPATH = "../" + DATASET_NAME + "_search.db"

def delete_existing_resources():
    _delete_file("../resources/efo.db")
    _delete_file("../resources/efo_dbxrefs.tsv")
    _delete_file("../resources/efo_edges.tsv")
    _delete_file("../resources/efo_entailed_edges.tsv")
    _delete_file("../resources/efo_labels.tsv")
    _delete_file("../resources/efo_mappings_counts.tsv")
    _delete_file("../resources/opengwas_mappings.csv")
    _delete_file("../resources/opengwas_metadata.tsv")
    _delete_file("../resources/opengwas_references.tsv")
    _delete_file("../opengwas_search.db")

def _delete_file(file):
    if os.path.isfile(file):
        os.remove(file)

def get_version_info_table(metadata_timestamp):
    data = [("SearchDB", SEARCH_DB_VERSION),
            ("EFO", EFO_VERSION),
            ("UBERON", UBERON_VERSION),
            ("Metadata", metadata_timestamp)]
    df = pd.DataFrame(data, columns=["Resource", "Version"])
    return df

# Takes an optional argument that is an NCBI API KEY, which is used to query PubMed faster.
if __name__ == "__main__":
    delete_existing_resources()

    # Check if an NCBI API Key is provided
    if len(sys.argv) > 1:
        os.environ["NCBI_API_KEY"] = sys.argv[1]
        print(f"Using NCBI API Key: {os.environ.get('NCBI_API_KEY')}")
    else:
        print("NCBI API Key not provided—PubMed queries will be slower. Provide API Key as a parameter to this module.")

    # Fetch the OpenGWAS metadata directly from OpenGWAS using ieugwaspy package
    print("Downloading OpenGWAS metadata...")
    metadata_dict = ieugwaspy.gwasinfo()
    metadata_download_timestamp = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
    metadata_df = pd.DataFrame.from_dict(metadata_dict, orient="index")
    metadata_df = metadata_df[metadata_df["id"].str.contains("eqtl-a") == False]  # Remove eqtl records
    metadata_df["pmid"] = metadata_df["pmid"].astype(str).str.replace(".0", "", regex=False)  # remove '.0' from PMIDs
    metadata_df.to_csv("../resources/opengwas_metadata.tsv", index=False, sep="\t")

    version_info_df = get_version_info_table(metadata_timestamp=metadata_download_timestamp)

    # Build the database
    start = time.time()
    from build_database import build_database
    print("Building database...")
    build_database(dataset_name=DATASET_NAME,
                   metadata_df=metadata_df,
                   ontology_name="EFO",
                   ontology_url=f"http://www.ebi.ac.uk/efo/releases/v{EFO_VERSION}/efo.owl",
                   pmid_col="pmid",
                   resource_col="trait",
                   resource_id_col="id",
                   mapping_minimum_score=0.6,
                   include_cross_ontology_references_table=True,
                   output_database_filepath=OUTPUT_DATABASE_FILEPATH,
                   mapping_base_iris=("http://www.ebi.ac.uk/efo/", "http://purl.obolibrary.org/obo/MONDO",
                                      "http://purl.obolibrary.org/obo/HP", "http://www.orpha.net/ORDO",
                                      "http://purl.obolibrary.org/obo/DOID"),
                   additional_tables={"version_info": version_info_df},
                   additional_ontologies=["UBERON"])

    base_filename = os.path.basename(OUTPUT_DATABASE_FILEPATH)
    with tarfile.open(OUTPUT_DATABASE_FILEPATH + ".tar.xz", "w:xz") as tar:
        tar.add(OUTPUT_DATABASE_FILEPATH, arcname=base_filename)
    print(f"Finished building database ({time.time() - start:.1f} seconds)")


# This module defines functions that can be used to check whether OpenMP is
# available and if so what flags to use. To use this, import the
# add_openmp_flags_if_available function in a setup_package.py file where you
# are defining your extensions:
#
#     from astropy_helpers.openmp_helpers import add_openmp_flags_if_available
#
# then call it with a single extension as the only argument:
#
#     add_openmp_flags_if_available(extension)
#
# this will add the OpenMP flags if available.

from __future__ import absolute_import, print_function

import os
import sys
import glob
import tempfile
import subprocess

from distutils import log
from distutils.ccompiler import new_compiler
from distutils.sysconfig import customize_compiler
from distutils.errors import CompileError, LinkError

from .setup_helpers import get_compiler_option

__all__ = ['add_openmp_flags_if_available']

CCODE = """
#include <omp.h>
#include <stdio.h>
int main(void) {
  #pragma omp parallel
  printf("nthreads=%d\\n", omp_get_num_threads());
  return 0;
}
"""

def add_openmp_flags_if_available(extension):
    """
    Add OpenMP compilation flags, if available (if not a warning will be
    printed to the console and no flags will be added)

    Returns `True` if the flags were added, `False` otherwise.
    """

    ccompiler = new_compiler()
    customize_compiler(ccompiler)

    tmp_dir = tempfile.mkdtemp()

    start_dir = os.path.abspath('.')

    if get_compiler_option() == 'msvc':
        compile_flag = '-openmp'
        link_flag = ''
    else:
        compile_flag = '-fopenmp'
        link_flag = '-fopenmp'

    try:

        os.chdir(tmp_dir)

        with open('test_openmp.c', 'w') as f:
            f.write(CCODE)

        os.mkdir('objects')

        # Compile, link, and run test program
        ccompiler.compile(['test_openmp.c'], output_dir='objects', extra_postargs=[compile_flag])
        ccompiler.link_executable(glob.glob(os.path.join('objects', '*' + ccompiler.obj_extension)), 'test_openmp', extra_postargs=[link_flag])
        output = subprocess.check_output('./test_openmp').decode(sys.stdout.encoding or 'utf-8').splitlines()

        if 'nthreads=' in output[0]:
            nthreads = int(output[0].strip().split('=')[1])
            if len(output) == nthreads:
                using_openmp = True
            else:
                log.warn("Unexpected number of lines from output of test OpenMP "
                         "program (output was {0})".format(output))
                using_openmp = False
        else:
            log.warn("Unexpected output from test OpenMP "
                     "program (output was {0})".format(output))
            using_openmp = False

    except (CompileError, LinkError):

        using_openmp = False

    finally:

        os.chdir(start_dir)

    if using_openmp:
        log.info("Compiling Cython extension with OpenMP support")
        extension.extra_compile_args.append(compile_flag)
        extension.extra_link_args.append(link_flag)
    else:
        log.warn("Cannot compile Cython extension with OpenMP, reverting to non-parallel code")

    return using_openmp


import ast
import json
import os
import tempfile
import uuid
from time import gmtime, strftime

import boto3
from PIL import Image

from utility import logger
from handler.db_handler import get_face_group_data, save_face_group_data
from handler.es_handler import push_data_to_es

ES_HOST = "search-od-happy-face-5i3vty7clvmiz3cvgus3n6dfpa.us-east-1.es.amazonaws.com"
ES_INDEX_NAME = "test"
GROUP_PHOTOS = "group_snaps"
REGION_NAME = 'us-east-1'
DYNAMO_TBL_FACEGROUPS = 'FaceGroupsEdge'

rek_client = boto3.client('rekognition', region_name=REGION_NAME)
s3_client = boto3.client('s3', region_name=REGION_NAME)
s3 = boto3.resource('s3', region_name=REGION_NAME)
dynamo_client = boto3.client('dynamodb', region_name=REGION_NAME)
dynamodb = boto3.resource('dynamodb', REGION_NAME)

s3_bucket = 'image-recognition-edge'
bucket = s3.Bucket(s3_bucket)

tmp = "{}/happyface".format(tempfile.gettempdir())
x, y = 2592, 1944
similarity_q = 50.00

def process_image(tmp_path):
    logger.info("\nStarted processing photo {}".format(tmp_path))
    try:
        with open(tmp_path, "rb") as imageFile:
            f = imageFile.read()
            b = bytearray(f)
    except Exception as e:
        logger.error('Error occurred while opening image file: {}'.format(str(e)))
        raise Exception('Error occurred while opening image file: ', e)

    try:
        group_s3_path = "{}/{}".format(GROUP_PHOTOS, os.path.basename(tmp_path))
        logger.info("Uploading photo : " + group_s3_path)
        s3_client.upload_file(tmp_path, s3_bucket, group_s3_path, ExtraArgs={'ACL': 'public-read'})
        s3.ObjectAcl(s3_bucket, group_s3_path).put(ACL='public-read')
        logger.info("Photo uploaded")
        response = rek_client.detect_faces(
            Image={
                'Bytes': b
            },
            Attributes=[
                'ALL',
            ]
        )
        for index, image in enumerate(response['FaceDetails']):
            path = os.path.join(tmp, os.path.basename(tmp_path).rstrip("/"))
            img = Image.open(path)
            width, height, left, top = image['BoundingBox']['Width'], \
                                       image['BoundingBox']['Height'], \
                                       image['BoundingBox']['Left'], \
                                       image['BoundingBox']['Top']
            left, top, right, bottom = left * x, top * y, (left + width) * x, (height + top) * y
            cropped = img.crop((left, top, right, bottom))
            cropped = cropped.resize((100, 100), Image.ANTIALIAS)

            dir_path = os.path.join(tmp, os.path.basename(tmp_path).split(".")[0])

            if not os.path.exists(dir_path):
                os.mkdir(dir_path)
            thum_uuid = str(uuid.uuid4())
            thumbnail_path = os.path.join(dir_path, thum_uuid + ".jpeg")
            cropped.save(thumbnail_path)
            s3_thumbnail_path = "thumbnail/{}".format(
                os.path.basename(tmp_path).split(".")[0] + "_" + os.path.basename(thumbnail_path))

            happy_emotion = {'Type': 'HAPPY', 'Confidence': 0.0}
            for emotion in image['Emotions']:
                # if emotion['Type'] == 'HAPPY' and emotion['Confidence'] > 90:
                if emotion['Type'] == 'HAPPY':
                    happy_emotion = emotion

                    # upload thumbnail to s3
                    logger.info("Uploading thumbnail: " + s3_thumbnail_path)
                    s3_client.upload_file(thumbnail_path, s3_bucket, s3_thumbnail_path, ExtraArgs={'ACL': 'public-read'})
                    s3.ObjectAcl(s3_bucket, s3_thumbnail_path).put(ACL='public-read')
                    os.remove(thumbnail_path)
                    os.rmdir(dir_path)

                    # Compare with existing thumnails and store data in FaceGroups
                    compare_existing_thumbnails(s3_thumbnail_path, happy_emotion)

        logger.info("Completed processing photo {}\n".format(tmp_path))

    except Exception as e:
        logger.error('Error occurred while detecting faces: {}'.format(str(e)))
        raise Exception('Error occurred while detecting faces:', e)

def compare_existing_thumbnails(thumbnail_path, emotions):
    s3_thumbnail_path = os.path.join("https://s3.amazonaws.com", s3_bucket, thumbnail_path)
    face_group_result = get_face_group_data()
    now = str(strftime("%Y-%m-%d %H:%M:%S", gmtime()))

    if face_group_result['Items']:
        is_face_matched = False
        for i in face_group_result['Items']:
            json_str = json.dumps(i)
            resp_dict = json.loads(json_str)
            target_image_list = ast.literal_eval(resp_dict['image_list'])
            target_path = "thumbnail" + target_image_list[0]['path'].split("thumbnail")[1]
            if target_path != thumbnail_path:
                img_uuid = resp_dict['uuid']
                compare_response = compare_faces(thumbnail_path, target_path)
                if compare_response is not None:
                    if compare_response['FaceMatches']:
                        for dictionary in compare_response['FaceMatches']:
                            if float(dictionary['Similarity']) > similarity_q:
                                t_dict = dict(path=s3_thumbnail_path, emotions=emotions, date=now)
                                target_image_list.append(t_dict)
                                save_face_group_data(img_uuid, target_image_list)
                                push_data_to_es(img_uuid, [t_dict])
                                is_face_matched = True
                                break

        if not is_face_matched:
            img_uuid = str(uuid.uuid4())
            image_list = [dict(path=s3_thumbnail_path, emotions=emotions, date=now)]
            save_face_group_data(img_uuid, image_list)
            push_data_to_es(img_uuid, image_list)
    else:
        img_uuid = str(uuid.uuid4())
        image_list = [dict(path=s3_thumbnail_path, emotions=emotions, date=now)]
        save_face_group_data(img_uuid, image_list)
        push_data_to_es(img_uuid, image_list)

def compare_faces(src_image, target_image):
    # logger.info("src_image: ", src_image, "target_image: ", target_image)
    try:
        response = rek_client.compare_faces(
            SourceImage={
                'S3Object': {
                    'Bucket': s3_bucket,
                    'Name': src_image
                }
            },
            TargetImage={
                'S3Object': {
                    'Bucket': s3_bucket,
                    'Name': target_image
                }
            }
        )
        return response
    except Exception as e:
        logger.error('\nError occurred while comparing faces using rekognition : {}'.format(str(e)))
        logger.error('Error comparing images ({}) and ({}) '.format(src_image, target_image))
        # raise Exception('Error occurred while comparing faces using rekognition: ', e)
        pass

# process_image("C:/Users/anisaraf/Desktop/test/Image_20190306_121818.jpg")


# You won't directly use this class, but it will be used by the other classes.
# Unless you want to build the class yourself, you may use this

import torch.nn as nn

class CNNClassificationModel(nn.Module):
    """
    Base PyTorch class of the classification model with Encoder, Head for CNN
    """
    def forward(self, x):
        # 1D signal x => [BATCH, CHANNEL, TIME]
        # 1D spectrum x => [BATCH, FREQUENCY, TIME] (single channel)
        # 2D spectrum x => [BATCH, CHANNEL, FREQUENCY, TIME] (multi channel)
        # 2D image x => [BATCH, CHANNEL, HEIGHT, WIDTH]
        # 3D volumetric x => [BATCH, CHANNEL, HEIGHT, WIDTH, DEPTH]
        features = self.encoder(x) # => [BATCH, 512, ...]
        features_agg = self.globalpooling(features) # => [BATCH, 512, 1, (1, (1))]
        output = self.head(features_agg.view(x.shape[0],-1)) # => [BATCH, NUM_CLS]
        return output

class RNNClassificationModel(nn.Module):
    """
    Base PyTorch class of the classification model with Encoder, Head for RNN
    """
    def forward(self, x):
        # 1D signal x => [BATCH, CHANNEL, TIME]
        x = x.transpose(1,2)
        features, _ = self.encoder(x) # RNN will output feature and states
        output = self.head(features[:, -1, :])
        return output

class HFClassificationModel(nn.Module):
    """
    Base Hugginface-pytoch model wrapper class of the classification model
    """
    def __init__(self, model,
                 num_cls=None,
                 loss_fct=nn.CrossEntropyLoss()):
        super().__init__()
        self.net = model
        if 'num_cls' in model.__dict__.keys():
            self.num_cls = model.num_cls
        else:
            self.num_cls = num_cls
        assert self.num_cls is not None, "num_cls is not defined"
        self.loss_fct = loss_fct
    def forward(self, x, labels=None):
        logits = self.net(x)  # Forward pass the model
        if labels is not None:
            # logits => [BATCH, NUM_CLS]
            # labels => [BATCH]
            loss = self.loss_fct(logits.view(-1, self.num_cls), labels.view(-1))  # Calculate loss
        else:
            loss = None
        # return dictionary for hugginface trainer
        return {'loss':loss, 'logits':logits, 'hidden_states':None}

# Test the function
if __name__ == '__main__':
    import torch
    from fusionlab.classification import VGG16Classifier
    from fusionlab.classification import LSTMClassifier

    H = W = 224
    cout = 5
    inputs = torch.normal(0, 1, (1, 3, W))
    # Test CNNClassification
    model = VGG16Classifier(3, cout, spatial_dims=1)
    hf_model = HFClassificationModel(model, cout)
    output = hf_model(inputs)
    print(output['logits'].shape)
    assert list(output.keys()) == ['loss', 'logits', 'hidden_states']

    inputs = torch.normal(0, 1, (1, 3, H, W))
    # Test CNNClassification
    model = VGG16Classifier(3, cout, spatial_dims=2)
    hf_model = HFClassificationModel(model, cout)
    output = hf_model(inputs)
    print(output['logits'].shape)
    assert list(output.keys()) == ['loss', 'logits', 'hidden_states']

    inputs = torch.normal(0, 1, (1, 3, H))
    model = LSTMClassifier(3, cout)
    hf_model = HFClassificationModel(model, cout)
    output = hf_model(inputs)
    print(output['logits'].shape)
    assert list(output.keys()) == ['loss', 'logits', 'hidden_states']


import sys
import pynput
from pynput.keyboard import Key, Controller
from selenium import webdriver
import time

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import element_to_be_clickable
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
keyboard = Controller()
driver.implicitly_wait(3)
wait = WebDriverWait(driver, 10)
driver.get('http://google.com')
element = driver.find_element_by_name("q")
#element = wait.until()
#button = wait.until(element_to_be_clickable((By.NAME, "btnK")))
element.send_keys("python")
#button = driver.find_element_by_xpath(r"//*[@id='tsf']/div[2]/div[1]/div[2]/div[2]/div[2]/center/input[1]").click()
#button = driver.find_element_by_name("btnK").click()

element.send_keys(Keys.ENTER)
#time.sleep(2)
#element.send_keys(keyboard.press(Key.enter))

links = driver.find_elements_by_partial_link_text("python")
print(links)
print(type(links))
print(len(links))

links = driver.find_elements_by_partial_link_text("Download")
print (len(links))

for l in links:
    print(l.size)
    print(l.location)

action = webdriver.ActionChains(driver)

(webdriver.ActionChains(driver)
.move_to_element(drag) #define element
.key_down(Keys.CONTROL)
.click_and_hold()
.move_to_element(drop)
.release()
.key_up(Keys.CONTROL)
.perform() #run chain
)

#element.send_keys(keyboard.press(Key.enter))
#element.send_keys(keyboard.press("enter"))
#driver.find_element_by_id(_id)
time.sleep(10)
driver.quit()
#driver.close()

#driver = webdriver.Firefox(executable_path=r"C:\Users\Desktop\Drivers\geckodriver.exe")
#good practice is create separete folder for all drivers like - D:\Selenium\drivers.

'''Commands:
driver.get()
driver.quit()
driver.close()
driver.refresh()
driver.back()
driver.forward()
'''
'''css selectors
form#loginForm
input[name="username"]
#loginForm input[type="text"] - space here means that look in class LoginForm and child element input with text
check selector in the console - $$('*[name="btnK"')
'''


#-------------------------------------------------------------------------------
# bob: compiler.py
#
# Scheme compiler.
#
# Eli Bendersky (eliben@gmail.com)
# This code is in the public domain
#-------------------------------------------------------------------------------
from __future__ import print_function
from .utils import flatten, list_find_or_append
from .expr import *
from .bytecode import *
from .bobparser import BobParser

DEBUG = False

class CompiledLabel(object):
    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return str(self.name)

class CompiledProcedure(object):
    """ Represents a compiled procedure.

        A procedure contains arguments (list of symbol names), code (list
        of compiled instructions) and optionally a name (for debugging).
    """
    def __init__(self, args, code, name=''):
        self.args = args
        self.code = code
        self.name = name

    def __repr__(self, nesting=0):
        """ Obtain a string representation of a compiled procedure.
        """
        repr = ''
        prefix = ' ' * nesting

        repr += prefix + '----------\n'
        repr += prefix + 'Proc: ' + ('' if self.name is None else self.name) + '\n'
        repr += prefix + 'Args: ' + str(self.args) + '\n'

        for instr in self.code:
            if isinstance(instr, CompiledLabel):
                repr += prefix + '  %s:\n' % instr.name
            else:
                assert isinstance(instr, Instruction)
                repr += prefix + '    ' + '%-12s' % opcode2str(instr.opcode)
                if instr.arg is None:
                    repr += '\n'
                elif instr.opcode == OP_FUNCTION:
                    repr += '\n'
                    # Note: calling __repr__ directly in order to pass it the
                    # nesting (the repr() built-in doesn't pass on additional
                    # arguments)
                    #
                    repr += instr.arg.__repr__(nesting + 8)
                else:
                    if is_scheme_expr(instr.arg):
                        arg_repr = expr_repr(instr.arg)
                    else:
                        arg_repr = str(instr.arg)
                    repr += arg_repr + '\n'

        repr += prefix + '----------\n'
        return repr

class BobCompiler(object):
    """ A Scheme compiler.
    """
    class CompileError(Exception): pass

    def __init__(self):
        self.labelstate = 0

    def compile(self, exprlist):
        """ Compile a list of parsed expressions (what's returned by
            BobParser.parse) into a single argument-less CompiledProcedure
            with the instructions stored in its .code attribute.
        """
        compiled_exprs = self._comp_exprlist(exprlist)
        return CompiledProcedure(args=[], name='', code=compiled_exprs)

    def _make_label(self, prefix=None):
        prefix = prefix or 'LABEL'
        self.labelstate += 1
        return CompiledLabel('%s%s' % (prefix, self.labelstate))

    def _instr(self, opcode, arg=None):
        """ A helper creator for instructions.
        """
        return [Instruction(opcode, arg)]

    def _instr_seq(self, *args):
        """ Create a sequence (list) of instructions, where each argument can
            be either a single instruction or a sequence of instructions.
        """
        return list(flatten(args))

    def _comp(self, expr):
        """ Compile an expression.

            Always returns a (Python) list of instructions.
        """
        if DEBUG: print('~~~~ Comp called on %s [%s]' % (expr_repr(expr), type(expr)))

        if is_self_evaluating(expr):
            return self._instr(OP_CONST, expr)
        elif is_variable(expr):
            return self._instr(OP_LOADVAR, expr)
        elif is_quoted(expr):
            return self._instr(OP_CONST, text_of_quotation(expr))
        elif is_assignment(expr):
            return self._instr_seq(
                            self._comp(assignment_value(expr)),
                            self._instr(OP_STOREVAR, assignment_variable(expr)))
        elif is_definition(expr):
            return self._comp_definition(expr)
        elif is_if(expr):
            return self._comp_if(expr)
        elif is_cond(expr):
            return self._comp(convert_cond_to_ifs(expr))
        elif is_let(expr):
            return self._comp(convert_let_to_application(expr))
        elif is_lambda(expr):
            return self._comp_lambda(expr)
        elif is_begin(expr):
            return self._comp_begin(begin_actions(expr))
        elif is_application(expr):
            return self._comp_application(expr)
        else:
            raise self.CompileError("Unknown expression in COMPILE: %s" % expr)

    def _comp_lambda(self, expr):
        # The lambda parameters are in Scheme's nested Pair format. Convert
        # them into a normal Python list
        #
        args = expand_nested_pairs(lambda_parameters(expr))
        arglist = []

        # Some sanity checking: only symbol arguments are supported
        #
        for sym in args:
            if isinstance(sym, Symbol):
                arglist.append(sym.value)
            else:
                raise self.CompileError("Expected symbol in argument list, got: %s" % expr_repr(sym))

        # For the code - compile lambda body as a sequence and append a RETURN
        # instruction to the end
        #
        proc_code = self._instr_seq(self._comp_begin(lambda_body(expr)),
                                    self._instr(OP_RETURN))

        return self._instr( OP_FUNCTION,
                            CompiledProcedure(
                                args=arglist,
                                code=proc_code))

    def _comp_begin(self, exprs):
        # To compile a 'begin' we append the compiled versions of all the
        # expressions in it, with a POP instruction inserted after each one
        # except the last.
        #
        exprlist = expand_nested_pairs(exprs, recursive=False)
        return self._comp_exprlist(exprlist)

    def _comp_exprlist(self, exprlist):
        instr_pop_pairs = list(self._instr_seq(self._comp(expr), self._instr(OP_POP)) for expr in exprlist)
        instrs = self._instr_seq(*instr_pop_pairs)
        return instrs[:-1] if len(instrs) > 0 else instrs

    def _comp_definition(self, expr):
        compiled_val = self._comp(definition_value(expr))

        # If the value is a procedure (a lambda), assign its .name attribute
        # to the variable name (for debugging)
        #
        var = definition_variable(expr)
        if (    isinstance(compiled_val[-1], Instruction) and
                isinstance(compiled_val[-1].arg, CompiledProcedure)):
            compiled_val[-1].arg.name = var.value

        return self._instr_seq(compiled_val, self._instr(OP_DEFVAR, var))

    def _comp_if(self, expr):
        label_else = self._make_label()
        label_after_else = self._make_label()

        return self._instr_seq(
                        self._comp(if_predicate(expr)),
                        self._instr(OP_FJUMP, label_else),
                        self._comp(if_consequent(expr)),
                        self._instr(OP_JUMP, label_after_else),
                        [label_else],
                        self._comp(if_alternative(expr)),
                        [label_after_else])

    def _comp_application(self, expr):
        args = expand_nested_pairs(application_operands(expr), recursive=False)
        compiled_args = self._instr_seq(*[self._comp(arg) for arg in args])
        compiled_op = self._comp(application_operator(expr))
        return self._instr_seq(
                        compiled_args,
                        compiled_op,
                        self._instr(OP_CALL, len(args)))

class BobAssembler(object):
    def assemble(self, proc):
        """ Assemble a compiled procedure into a CodeObject
        """
        label_offsets = self._compute_label_offsets(proc)
        return self._assemble_to_code(proc, label_offsets)

    def _compute_label_offsets(self, proc):
        """ The first pass of the assembler - compute addresses (offsets) of all
            labels in the code. Return a dict mapping label name -> offset.
        """
        d = {}
        offset = 0
        for instr in proc.code:
            if isinstance(instr, CompiledLabel):
                d[instr.name] = offset
            else:
                offset += 1
        return d

    def _assemble_to_code(self, proc, label_offsets):
        """ The second and main pass of the assembler - translate the compiled
            procedure int a CodeObject.
        """
        co = CodeObject()
        co.name = proc.name
        co.args = proc.args

        for instr in proc.code:
            if isinstance(instr, CompiledLabel):
                continue
            if instr.opcode == OP_CONST:
                # Don't try to save space by folding similar pairs together
                # since this is against the semantics of Scheme: two different
                # Pairs, even with the same elements, must be distinct for eqv?
                #
                if isinstance(instr.arg, Pair):
                    co.constants.append(instr.arg)
                    arg = len(co.constants) - 1
                else:
                    arg = list_find_or_append(co.constants, instr.arg)
            elif instr.opcode in (OP_LOADVAR, OP_STOREVAR, OP_DEFVAR):
                arg = list_find_or_append(co.varnames, instr.arg.value)
            elif instr.opcode == OP_FUNCTION:
                # Recursively assemble the CompiledProcedure referred to by
                # this instruction and shove it into the constants list.
                #
                co.constants.append(self.assemble(instr.arg))
                arg = len(co.constants) - 1
            elif instr.opcode in (OP_FJUMP, OP_JUMP):
                arg = label_offsets[instr.arg.name]
            elif instr.opcode == OP_CALL:
                arg = instr.arg
            elif instr.opcode in (OP_POP, OP_RETURN):
                arg = None
            else:
                assert False, "Unexpected opcode %s" % instr.opcode

            co.code.append(Instruction(instr.opcode, arg))

        return co

def compile_code(code_str):
    """ Convenience function for compiling (& assembling) a string containing
        Scheme code into a code object.
    """
    parsed_exprs = BobParser().parse(code_str)
    compiled = BobCompiler().compile(parsed_exprs)

    return BobAssembler().assemble(compiled)


class Hyperparameters:
    #  Using the same hyperparameters from Josh's setup,
    #  probably will change a couple of these values to modify learning
    def __init__(self):
        self.gamma = 0.995
        self.tau = 0.005
        self.alpha = 0.001
        self.beta = 0.002
        self.lr = 1e-4
        self.ee_lr = 0.01
        self.epsilon = 1.0
        self.eps_min = 0.01
        self.eps_dec = 1e-5

        self.intn_epsilon = 1.0
        self.intn_eps_min = 0.01
        self.intn_eps_dec = 1e-5

        self.batch_size = 100
        self.mem_size = 100000
        self.replace_target_ctr = 1000
        self.failed = False
        self.failure_action = [0, 0, 1]

        self.noise = 0.1
        self.update_actor_iter = 2
        self.warmup = 1000
        self.time_step = 0

        self.min_max_action = 1

        #GNN
        self.hidden_channels = 32
        self.num_heads = 4


# Given the dictionary below:
artist = {
    "first": "Neil",
    "last": "Young",
}

# Declare a variable called full_name
# Access and assign the first & last dictionary keys into full_name as a single string with a space in-between
full_name = artist["first"] + " " + artist["last"]
print(full_name)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Date    : Feb-11-20 14:18
# @Author  : Kelly Hwong (you@example.org)
# @Link    : https://stackoverflow.com/questions/2828059/sorting-arrays-in-numpy-by-column

import os
import csv
import json
import numpy as np
import shutil
from tqdm import tqdm

def change_positive_column():
    with open("submission.csv", "r") as submission_origin:
        f_origin = csv.reader(submission_origin)
        headers = next(f_origin)
        new_rows = []
        for row in f_origin:
            # print(row)
            new_score = str(1 - float(row[1]))
            new_row = [row[0], new_score]
            new_rows.append(new_row)

    with open('submission_modified.csv', 'w') as submission_modified:
        f_modified = csv.writer(submission_modified)
        f_modified.writerow(headers)
        for row in new_rows:
            f_modified.writerow(row)

def read_data(filepath=""):
    """
    """
    data = []
    with open(filepath, "r") as csvfile:
        reader = csv.reader(csvfile)
        for i, row in enumerate(reader):
            if i == 0:
                continue
            # print("row:", row)
            if row[2] == "":
                row[2] = 0  # score, 没有就补0
            data.append([int(row[0]), float(row[1]), float(row[2])])
    data = np.asarray(data)
    data = data[data[:, 0].argsort()]  # 按照第一列排序
    return data

def read_TP(filepath="", threshold=0.5):
    """读取csv文件的概率
    csv文件第一列是文件名
    第二列是 sample 是 bad 类别的概率
    """
    tp = 0  # predict bad as indeed bad
    fn = 0  # predict good, actually bad
    with open(filepath, "r") as csvfile:
        reader = csv.reader(csvfile)
        for i, row in enumerate(reader):
            if i == 0:
                continue
            # print("row:", row)
            if float(row[1]) >= threshold:  # predict > threshold
                tp += 1
            else:
                fn += 1
    return tp, fn

def read_TN(filepath="", threshold=0.5):
    """读取csv文件的概率
    csv文件第一列是文件名
    第二列是 sample 是 bad 类别的概率
    """
    tn = 0  # predict good as indeed good
    fp = 0  # predict bad, actually good
    with open(filepath, "r") as csvfile:
        reader = csv.reader(csvfile)
        for i, row in enumerate(reader):
            if i == 0:
                continue
            # print("row:", row)
            if float(row[1]) < threshold:  # predict > threshold
                tn += 1
            else:
                fp += 1
    return tn, fp

def count_TP(filepath=""):
    count = 0
    with open(filepath, "r") as csvfile:
        reader = csv.reader(csvfile)
        for i, row in enumerate(reader):
            if i == 0:
                continue
            # print("row:", row)
            if float(row[1]) > 0.5:  # predict > threshold
                count += 1
    return count

def list_model_epochs(model_dir, model_type):
    _model_list = os.listdir(model_dir)
    model_list = []
    for m in _model_list:
        if "auc" in m:
            model_list.append(m.replace(model_type+".", "").replace(".h5", ""))
    epochs = [int(_.split('-')[0]) for _ in model_list]
    aucs = [float(_.split('-')[2]) for _ in model_list]
    return epochs, aucs

def unify_epoch():
    """ Change "ResNet56v2.85and001-auc-0.9811" to "ResNet56v2.086-auc-0.9811"
    Only run once!
    """
    """ Load Config """
    with open('config.json', 'r') as f:
        CONFIG = json.load(f)
    ROOT_PATH = CONFIG["ROOT_PATH"]
    MODEL_DIR = CONFIG["MODEL_DIR"]
    MODEL_DIR = os.path.join(ROOT_PATH, MODEL_DIR)

    os.chdir(MODEL_DIR)
    for filename in os.listdir(MODEL_DIR):
        if "and" in filename:
            # print(filename)
            i = filename.find("and")
            # print(filename[i-2:i], filename[i+3:i+6])
            start_epoch = int(filename[i-2:i])
            delta_epoch = int(filename[i+3:i+6])
            epoch = start_epoch + delta_epoch
            new_filename = filename[:i-2] + "%03d" % epoch + filename[i+6:]
            print(new_filename)
            input("ready to rename...")
            os.rename(filename, new_filename)

def create_csv():
    with open('config.json', 'r') as f:
        CONFIG = json.load(f)
    ROOT_PATH = CONFIG["ROOT_PATH"]
    MODEL_DIR = "models-simpleCNN-auc"
    MODEL_DIR = os.path.join(ROOT_PATH, MODEL_DIR)

    print(MODEL_DIR)

    _model_list = os.listdir(MODEL_DIR)
    model_list = []
    for m in _model_list:
        model_list.append(m.replace("model_", "").replace(".h5", ""))
    epochs = [int(_.split('-')[0])+17 for _ in model_list]
    acc = [float(_.split('-')[2]) for _ in model_list]

    print(epochs)
    print(acc)

    with open("./simpleCNN-epoch-auc.csv", "w", newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(["epoch", "auc", "score", "date"])
        for i, epoch in enumerate(epochs):
            writer.writerow([epoch, acc[i], "", "20200203"])

def transform_filetree():
    """
    原始train目录下，文件夹名为{label}，下面为文件名为{id}的训练图片
    把它转化为train-*格式文件夹
    """
    with open('config.json', 'r') as f:
        CONFIG = json.load(f)
    ROOT_PATH = CONFIG["ROOT_PATH"]

    TRAIN = os.path.join(ROOT_PATH, "data/train")
    TRAIN_GOOD = os.path.join(TRAIN, "good_0")
    TRAIN_BAD = os.path.join(TRAIN, "bad_1")

    good_sample_files = os.listdir(TRAIN_GOOD)
    size_good_sample = len(good_sample_files)
    mask = list(range(0, size_good_sample))
    np.random.shuffle(mask)
    val_split = int(0.2*size_good_sample)

    os.makedirs(os.path.join(TRAIN, "train/good_0"), exist_ok=True)
    os.makedirs(os.path.join(TRAIN, "val/good_0"), exist_ok=True)
    os.chdir(TRAIN_GOOD)
    for m in tqdm(mask[:val_split]):
        shutil.move(good_sample_files[m], os.path.join(TRAIN, "val/good_0"))
    for m in tqdm(mask[val_split:]):
        shutil.move(good_sample_files[m], os.path.join(TRAIN, "train/good_0"))

    bad_sample_files = os.listdir(TRAIN_BAD)
    size_bad_sample = len(bad_sample_files)
    mask = list(range(0, size_bad_sample))
    np.random.shuffle(mask)
    val_split = int(0.2*size_bad_sample)

    os.makedirs(os.path.join(TRAIN, "train/bad_1"), exist_ok=True)
    os.makedirs(os.path.join(TRAIN, "val/bad_1"), exist_ok=True)
    os.chdir(TRAIN_BAD)
    for m in tqdm(mask[:val_split]):
        shutil.move(bad_sample_files[m], os.path.join(TRAIN, "val/bad_1"))
    for m in tqdm(mask[val_split:]):
        shutil.move(bad_sample_files[m], os.path.join(TRAIN, "train/bad_1"))

def main():
    # create_csv()

    transform_filetree()
    return
    TRAIN = "D:\\DeepLearningData\\semi-conductor-image-classification-first\\data\\train"
    TRAIN_GOOD = os.path.join(TRAIN, "good_0")
    os.chdir(TRAIN_GOOD)
    os.rename('train-good_0-00025ab9befdfbf34891887c1f8d8b4f.jpg',
              '00025ab9befdfbf34891887c1f8d8b4f.jpg')

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-

from odoo import api, fields, models, _
import logging
from odoo.exceptions import ValidationError
from odoo.tools.profiler import profile

_logger = logging.getLogger(__name__)

class AccountMove(models.Model):
    _inherit = ['account.move']

    @api.depends('type')
    def _get_ncf_types_domain(self):
        for move in self:
            final_ncf_types = []
            ncf_types = move.env['ir.sequence'].search([('is_ncf', '=', True)])
            for ncf_type in ncf_types:
                if move.type in [move_type.code for move_type in ncf_type.move_type_ids]:
                    final_ncf_types.append(ncf_type.id)

            move.ncf_type_list = final_ncf_types

    def get_ncf(self):
        for move in self:
            #             _logger.info(f"Move's ncf_€type var -> {move.ncf_type}")
            sequence = move.ncf_type
            ncf = sequence.get_next_char(sequence.number_next_actual)
            return ncf

    def set_ncf(self):
        for move in self:
            ncf = self.env['ir.sequence'].next_by_code(move.ncf_type.code)
            return ncf

    @api.constrains('ncf')
    def _check_ncf_length(self):
        for move in self:
            if move.ncf:
                if len(move.ncf) in range(1, 11):
                    #                     _logger.info(f"NCF => {move.ncf}")
                    raise ValidationError('NCF is shorter than 11 characters')
                elif len(move.ncf) > 11:
                    #                     _logger.info(f"NCF => {move.ncf}")
                    raise ValidationError('NCF is longer than 11 characters')

    @api.constrains('ncf')
    def _check_unique_ncf(self):
        for move in self:
            if move.ncf:
                # Getting all the invoices (hopefully 1) with the same ncf
                invoices = self.env['account.move'].search(
                    [('ncf', '=', move.ncf)])

                if len(invoices) > 1:
                    error_text = f"NCF {move.ncf} is already taken by {len(invoices)-1} invoice(s).\n"

                    # Getting invoices attributes to show as a description in Exception window
                    for invoice in invoices[1:]:

                        inv_name = invoice.name if invoice.name != '/' else "Unposted invoice"
                        inv_partner = invoice.partner_id.name if invoice.partner_id.name else "No partner assigned"
                        inv_date = invoice.date if invoice.date else "No date assigned"

                        error_text += f"- {inv_name} | {inv_partner} | {inv_date}\n"
                    raise ValidationError(error_text)

    @api.constrains('ncf_type')
    def _check_available_ncf_type(self):
        for move in self:
            if move.ncf_type:
                if move.type not in [move_type.code for move_type in move.ncf_type.move_type_ids]:
                    raise ValidationError("NCF Type not valid")

    @api.onchange('ncf_type')
    def change_ncf(self):
        for move in self:
            if move.ncf_type:
                move.ncf = move.get_ncf()
            else:
                move.ncf = ''

    @api.model
    def create(self, values):
        """Override default Odoo create function and extend."""
        move = super(AccountMove, self).create(values)
        if move.ncf_type and (move.ncf == move.get_ncf()):
            move.set_ncf()
        return move

    def write(self, values):
        """Override default Odoo write function and extend."""
        for move in self:
            res = super(AccountMove, self).write(values)
            if move.ncf_type and (move.ncf == move.get_ncf()):
                move.set_ncf()
            return res

    def _reverse_move_vals(self, default_values, cancel=True):
        move_vals = super(AccountMove, self)._reverse_move_vals(default_values)

        move_vals['parent_move_id'] = move_vals['reversed_entry_id'] if 'reversed_entry_id' in move_vals else False

        move_vals['ncf'] = ''
        move_vals['ncf_type'] = self.env['ir.sequence'].search(
            [('code', '=', 'out_refund')])

        return move_vals

#     ===NCF Fields===
    ncf = fields.Char('NCF', default='', size=11)
    # Avaliable ncf types according to the move type
    ncf_type_list = fields.Many2many(
        'ir.sequence', compute=_get_ncf_types_domain)
    ncf_type = fields.Many2one('ir.sequence', string='NCF Type')
#     ===REVERSAL fields===
    parent_move_id = fields.Many2one('account.move', string='Parent Invoice',
                                     help="The move affected by the reversal.", domain="[('type','not in', ['in_refund', 'out_refund'])]")
    mod_ncf = fields.Char(
        'Modified NCF', related='parent_move_id.ncf', readonly=True)


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
df_train = pd.read_csv("../input/liverpool-ion-switching/train.csv")

train_time   = df_train["time"].values.reshape(-1,500000)
train_signal = df_train["signal"].values.reshape(-1,500000)
train_opench = df_train["open_channels"].values.reshape(-1,500000)
def create_axes_grid(numplots_x, numplots_y, plotsize_x=6, plotsize_y=3):
    fig, axes = plt.subplots(numplots_y, numplots_x)
    fig.set_size_inches(plotsize_x * numplots_x, plotsize_y * numplots_y)
    fig.subplots_adjust(wspace=0.05, hspace=0.05)
    return fig, axes

def set_axes(axes, use_grid=True, x_val = [0,100,10,5], y_val = [-50,50,10,5]):
    axes.grid(use_grid)
    axes.tick_params(which='both', direction='inout', top=True, right=True, labelbottom=True, labelleft=True)
    axes.set_xlim(x_val[0], x_val[1])
    axes.set_ylim(y_val[0], y_val[1])
    axes.set_xticks(np.linspace(x_val[0], x_val[1], np.around((x_val[1] - x_val[0]) / x_val[2] + 1).astype(int)))
    axes.set_xticks(np.linspace(x_val[0], x_val[1], np.around((x_val[1] - x_val[0]) / x_val[3] + 1).astype(int)), minor=True)
    axes.set_yticks(np.linspace(y_val[0], y_val[1], np.around((y_val[1] - y_val[0]) / y_val[2] + 1).astype(int)))
    axes.set_yticks(np.linspace(y_val[0], y_val[1], np.around((y_val[1] - y_val[0]) / y_val[3] + 1).astype(int)), minor=True)
fig, axes = create_axes_grid(1,2,20,5)
set_axes(axes[0], x_val=[0,500,50,10], y_val=[-5,15,5,1])
set_axes(axes[1], x_val=[0,500,50,10], y_val=[-1,11,1,1])

axes[0].set_title('training data')
axes[0].set_xticklabels('')
axes[1].set_xlabel('time / s')
axes[0].set_ylabel('signal')
axes[1].set_ylabel('open channels')

for i in range(10):
    if i in [0,2,3,4,5]:
        col = 'red'
    else:
        col = 'blue'
    axes[0].plot(train_time[i], train_signal[i], color=col, linewidth=0.1);
    axes[1].plot(train_time[i], train_opench[i], color=col, linewidth=0.1);
def markov_p(data):
    channel_range = np.unique(data)
    channel_bins = np.append(channel_range, 11)
    data_next = np.roll(data, -1)
    matrix = []
    for i in channel_range:
        current_row = np.histogram(data_next[data == i], bins=channel_bins)[0]
        current_row = current_row / np.sum(current_row)
        matrix.append(current_row)
    return np.array(matrix)
p03 = markov_p(train_opench[3])

fig, axes = create_axes_grid(1,1,10,5)
axes.set_title('Markov Transition Matrix P for sequence 3')
sns.heatmap(
    p03,
    annot=True, fmt='.3f', cmap='Blues', cbar=False,
    ax=axes, vmin=0, vmax=0.5, linewidths=2);
eig_values, eig_vectors = np.linalg.eig(np.transpose(p03))
print("Eigenvalues :", eig_values)
dist03 = eig_vectors[:,0] / np.sum(eig_vectors[:,0])
print("Probability distribution for sequence 3 :", dist03)
np.histogram(train_opench[3], bins=[0,1,2,3,4], density=True)[0]
data = train_signal[3]

fig, axes = create_axes_grid(1,1,10,10)
set_axes(axes, x_val=[-4,2,1,.1], y_val=[-4,2,1,.1])

axes.set_aspect('equal')
axes.scatter(np.roll(data,-1), data, s=.01);
data = train_signal[4]
data_true = train_opench[4]

fig, axes = create_axes_grid(1,1,10,10)
set_axes(axes, x_val=[-4,8,1,.1], y_val=[-4,8,1,.1])

axes.set_aspect('equal')
for i in range(11):
    axes.scatter(np.roll(data,-1)[data_true==i], data[data_true==i], s=.01);


import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from pytorch_transformers import BertConfig, BertTokenizer, BertForSequenceClassification

class BertForTextClassification(nn.Module):
    def __init__(self, pretrained_model_path, num_classes, hidden_size=768, num_hidden_layers=12, num_attention_heads=12, \
            intermediate_size=3072, hidden_act='gelu', hidden_dropout_prob=0.1, attention_probs_dropout_prob=0.1,
            max_position_embeddings=512, use_pretrained_model=False):
        super(BertForTextClassification, self).__init__()

        if(use_pretrained_model==True):
            print('Reloading pretrained models...')
            self.model = BertForSequenceClassification.from_pretrained(pretrained_model_path, num_labels=num_classes)
        else:
            print('Constructing new Bert by parameters...')
            tokenizer = BertTokenizer.from_pretrained(pretrained_model_path)
            vocab_size = len(tokenizer.ids_to_tokens)

            config = BertConfig(vocab_size, hidden_size, num_hidden_layers, num_attention_heads, \
                                intermediate_size, hidden_act, hidden_dropout_prob, \
                                attention_probs_dropout_prob, max_position_embeddings)
            self.model = BertForSequenceClassification(config)

    def forward(self, input_senquence, batch_size=None):
        '''
        Args:
        input_senquence: shape=[batch_size, max_time_step]

        Returns:
        logits: shape=[batch_size, num_labels]
        '''
        logits = self.model(input_senquence)[0]
        return logits


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 11 18:07:10 2018

@author: dzhong
"""

import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt

from networkx.drawing.nx_agraph import write_dot, graphviz_layout

#G = nx.DiGraph()
G = nx.MultiDiGraph()
G.add_node("0",fontsize=25,penwidth=3)

for i in range(1):
    G.add_nodes_from([1,2,4,8],fontsize=25,penwidth=3)
    G.add_nodes_from([3,5,9,6,10],fontsize=25,penwidth=3)
    G.add_nodes_from([7,11],fontsize=25,penwidth=3)

#   G.add_node("Process_%i" % i)
#   G.add_node("Grandchild_%i" % i)
#   G.add_node("Greatgrandchild_%i" % i)
    G.add_edge(0,1,label="seq:1", fontsize=20,penwidth=3)
    G.add_edge(0,2,label="seq:3", fontsize=20,penwidth=3)
    G.add_edge(0,4,label="seq:5", fontsize=20,penwidth=3)
    G.add_edge(0,8,label="seq:7", fontsize=20,penwidth=3)
    G.add_edge(0,10, color='blue', label="seq:4", fontsize=20,penwidth=3)
    #G.add_node(2,color='blue',style='filled',weight=0.5)
    G.add_edge(0,11,color='blue', label="seq:2", fontsize=20,penwidth=3)
    G.add_edge(0,4, color='blue', label="seq:8", fontsize=20,penwidth=3)
    #G.add_node(2,color='blue',style='filled',weight=0.5)
    G.add_edge(0,8, label="seq:6",color='blue', fontsize=20,penwidth=3)
    G.add_edges_from([(1, 3), (1, 5),(1, 9),(2, 6), (2, 10)],penwidth=3)
    G.add_edges_from([(3, 7), (3, 11)],penwidth=3)

 #   G.add_edge("0", "Process_%i" % i)
 #   G.add_edge("Process_%i" % i, "Grandchild_%i" % i)
#    G.add_edge("Grandchild_%i" % i, "Greatgrandchild_%i" % i)
for u,v,d in G.edges(data=True):
    d['weight']=30
    #d['color']='green'
    print(d)
# write dot file to use with graphviz
# run "dot -Tpng test.dot >test.png"
write_dot(G,'test.dot')

#run in shell: dot -Tpdf test.dot -o review_reorder_span.pdf


from memory_profiler import profile # may need to pip isntall memory_profiler
import collections

@profile # we can decorate ANY function to get a memory profile
def someFn():
    # here is a double-ended queue
    my_deq = collections.deque('98765432')
    # we can alter members of the queue
    my_deq.append('1')
    my_deq.appendleft('0')
    my_deq.pop() # remove from the right
    my_deq.popleft() # remove from the left
    print(my_deq)

if __name__ == '__main__':
    someFn()