import unittest
from selenium import webdriver
from src.page.login_page import ShowAll_page
from time import sleep
from src.common import excel_data
from src.common.mylog import mylog
from src.common.page_url import url

class TestSaasShowAll(unittest.TestCase):
    def setUp(self):
        self.mylog = mylog()
        self.driver = webdriver.Chrome()
        self.url = url.login_page
        self.ShowAll_page = ShowAll_page(self.driver, self.url, 'SaaS Web')
        self.ShowAll_page.login_success()

    def test_selected(self):
        '''验证登录后默认选中'''
        try:
            status = self.driver.find_element_by_xpath(self.ShowAll_page.tab_loc).get_attribute("aria-current")
            self.assertEqual(status, 'true')
        except Exception as e:
                self.mylog.error('error_log ')
                self.ShowAll_page.img_screenshot('error_img')
                raise e

    def test_skipToSetting(self):
        '''验证hover并打开账号设置页面'''
        try:
            self.ShowAll_page.hover_it()
            self.ShowAll_page.click_setting()
            sleep(1)
            self.assertEqual(self.driver.current_url, url.setting_page)
        except Exception as e:
                self.mylog.error('error_log ')
                self.ShowAll_page.img_screenshot('error_img')
                raise e

    def test_skipToDetail(self):
        '''验证跳转点击查看button'''
        try:
            self.ShowAll_page.click_detail()
            sleep(1)
            self.assertIn(url.detail_page, self.driver.current_url)
        except Exception as e:
            self.mylog.error('error_log ')
            self.ShowAll_page.img_screenshot('error_img')
            raise e

    def test_logout(self):
        '''验证hover并退出登录'''
        try:
            self.ShowAll_page.hover_it()
            self.ShowAll_page.click_logout()
            sleep(1)
            self.driver.switch_to_alert().dismiss()
            self.assertEqual(self.driver.current_url, url.showAll_page)
        except Exception as e:
                self.mylog.error('error_log ')
                self.ShowAll_page.img_screenshot('error_img')
                raise e
        sleep(1)
        try:
            self.ShowAll_page.hover_it()
            self.ShowAll_page.click_logout()
            sleep(1)
            self.driver.switch_to_alert().accept()
            self.assertEqual(self.driver.current_url, url.login_page)
        except Exception as e:
                self.mylog.error('error_log ')
                self.ShowAll_page.img_screenshot('error_img')
                raise e

    def tearDown(self):
        self.driver.close()

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


import time
from copy import deepcopy
from typing import List, Tuple

import BuiltIns
from Datatypes import AbstractDataStructure
from util import processParen, splitWithoutParen, outString, smart_replace, smartUpdate, \
    processHead, Counter, match_type, find_package_end, unfold, deref, independent
from Match import MatchDictionary

# Query Class
class Query:
    """

    Query class represents queries. Queries are questions asked by the programmer and solved by the database that the programmer has provided.
    Queries can be simple, or they can include many logical gates (see more in solving techniques).

    """

    start = 0

    def __init__(self, interpreter):
        """
        initiates empty query (don't call this, use @classmethod Query.Create(...))

        :param interpreter: Interpreter
        """
        self.gateA = None
        self.gateB = None
        self.type = None
        self.cond: List[Tuple[Query, Query]] = []

        self.object = None
        self.action = None

        self.interpreter = interpreter

    # Create a new query
    @classmethod
    def create(cls, interpreter, query):
        """
        Main way of creating queries. Used class method and not __init__ because it calls itself recursively. While it is possible to use __init__ and call
        input recursively, this way is more aesthetically pleasing (in my opinion), because it allows the __init__ to behave as a private method behind the
        scenes.

        :param interpreter: Interpreter
        :param query: str
        :return: Query (None if illegal)
        """
        X = Query(interpreter)
        query = processParen(query)
        if not query:
            interpreter.raiseError("Error: Incomplete Parentheses")
            return
        X.input(query)
        return X

    # A function that defines the query - matches the type and recursively defines queries
    def input(self, query: str):
        """
        Main function that builds queries.
        Searches for logical gates (&, |, $, ~).
        Searches for package calls (G{...}(...))

        :param query: str
        :return: None
        """

        # looking for cuts
        searchForCuts = splitWithoutParen(query, "\\")
        if len(searchForCuts) != 1:
            self.gateA = Query.create(self.interpreter, searchForCuts[0])
            self.gateB = Query.create(self.interpreter, "\\".join(searchForCuts[1:]))
            self.type = '\\'
            return

        # looking for ands
        searchForAnd = splitWithoutParen(query, "&")
        if len(searchForAnd) != 1:
            self.gateA = Query.create(self.interpreter, searchForAnd[0])
            self.gateB = Query.create(self.interpreter, "&".join(searchForAnd[1:]))
            self.type = '&'
            return

        # looking for ors
        searchForOr = splitWithoutParen(query, "|")
        if len(searchForOr) != 1:
            self.gateA = Query.create(self.interpreter, searchForOr[0])
            self.gateB = Query.create(self.interpreter, "|".join(searchForOr[1:]))
            self.type = '|'
            return

        # looking for Xor
        searchForXor = splitWithoutParen(query, "$")
        if len(searchForXor) != 1:
            # print(searchForXor)
            self.gateA = Query.create(self.interpreter, searchForXor[0])
            self.gateB = Query.create(self.interpreter, "$".join(searchForXor[1:]))
            self.type = '$'
            return

        # looking for not
        searchForFilter = splitWithoutParen(query, "~")
        if len(searchForFilter) != 1:
            if len(searchForFilter) != 2 or searchForFilter[0] != '':
                self.interpreter.raiseError("Error: ~ must have 1 side!")
                return
            self.gateA = Query.create(self.interpreter, searchForFilter[1])
            self.type = "~"
            return

        # conditional statements
        if query[:4] == "cond":
            processed = processParen(query[5:-1])
            if not processed:
                self.interpreter.raiseError("Error: illegal cond statement")
                return
            self.type = "c"

            parts = splitWithoutParen(processed)
            for part in parts:
                trie = splitWithoutParen(part, ">")
                if len(trie) != 2:
                    self.interpreter.raiseError(f"Error: {part} illegal cond")
                    return
                cond, exe = trie
                cond = Query.create(self.interpreter, cond)
                exe = Query.create(self.interpreter, exe)
                self.cond.append((cond, exe))
            return

        # Changing a reference
        searchForReferenceDefinition = outString(query, ":=")
        if searchForReferenceDefinition:
            parts = query.split(":=")
            if len(parts) != 2:
                self.interpreter.raiseError(f"Error: Illegal pattern for changing a reference, '{query}'")
                return
            self.gateA, self.gateB = parts
            self.type = ":="
            return

        # Dereference
        if outString(query, "!") and len(splitWithoutParen(query, "!")) == 1:
            self.gateA = query
            self.type = "!"
            return

        # Folding
        if outString(query, "%") and len(splitWithoutParen(query, "%")) == 1 and not outString(query, ":"):
            self.gateA = query
            self.type = "%"
            return

        # case that package
        searchForPackage = outString(query, "}(")
        if searchForPackage:
            self.gateA = query
            self.type = 'pi'
            return

        # case that it is a simple clause
        self.gateA = query
        self.type = 'r'
        return

    # Given new information (in the form of a dictionary) change the query
    def add_new_info(self, info: dict):
        """
        Adding new information to a current query.
        Useful in And Gates where a solution to a previous query is used in a following query(s).

        :param info: dict (of translations between variables and atoms|lists|variables)
        :return: Query (updated)
        """
        Q = Query(self.interpreter)

        if self.type == 'r' or self.type == 'pi' or self.type == 'pc' or self.type == "%" or self.type == "!":
            # print(f"New Info is {info}")
            Q.gateA = smart_replace(self.gateA, info)
            Q.type = self.type
        elif self.type in ['&', '|', '$', '\\']:
            try:
                Q.gateA = self.gateA.add_new_info(info)
                Q.gateB = self.gateB.add_new_info(info)
                Q.type = self.type
            except AttributeError as e:
                print(type(Q), Q.type, Q.gateA, Q.gateB)
                raise e
        elif self.type == '~':
            Q.gateA = self.gateA.add_new_info(info)
            Q.type = self.type
        elif self.type == "c":
            for key, value in self.cond:
                key, value = key.add_new_info(info), value.add_new_info(info)
                Q.cond.append((key, value))
                Q.type = 'c'
        elif self.type == ":=":
            Q.gateA = smart_replace(self.gateA, info)
            Q.gateB = smart_replace(self.gateB, info)
            Q.type = ":="

        return Q

    # to string
    def __str__(self):
        """
        Query as string

        :return: str
        """
        if self.type == "r" or self.type == 'pi' or self.type == 'pc' or self.type == "%" or self.type == "!":
            return f"{self.gateA}"
        elif self.type == '&':
            return "(" + str(self.gateA) + ")" + "&" + "(" + str(self.gateB) + ")"
        elif self.type == '\\':
            return "(" + str(self.gateA) + ")" + "\\" + "(" + str(self.gateB) + ")"
        elif self.type == "c":
            s = ",".join([f"{cond} > {search}" for cond, search in self.cond])
            return f"cond({s})"
        elif self.type == "|":
            return "(" + str(self.gateA) + ")" + "|" + "(" + str(self.gateB) + ")"
        elif self.type == "~":
            return "~" + str(self.gateA)
        elif self.type == "$":
            return "(" + str(self.gateA) + ")" + "$" + "(" + str(self.gateB) + ")"
        elif self.type == ":=":
            return f"{self.gateA} := {self.gateB}"
        else:
            print(self.type)

    # Main function. Searches for query
    def search(self, depth):
        """
        Search. Uses generators (yield statements) to generate solutions for this query.
        Given type "r" looks for predicates
        Given type "~" looks to see that a predicate doesn't have a solution
        Given type "&" looks for both gates of query, uses the first to 'feed' the second
        Given type "\\" looks for left side once only, and then right size
        Given type "|" yields from first gate, and from second gate
        Given type "$" yields from first gate, if none yielded, yields from second gate
        Given type "pi", represents Package{param}(variables), searched packages
        Given type "%", unfolds (if possible) and searches normally ('r')
        Given type "c" conditional.

        :param depth: a Counter object, maximum recursion depth
        :return: a generator object that can generate solutions for this query, dict[str, str]
        """

        if depth.count < 0:
            return

        if self.interpreter.console_trace_on:
            print(f"Searching for {self.__str__()}, with accumulated depth of {depth}, type {self.type}")

        if self.interpreter.trace_on:
            self.interpreter.message(f"Searching for {self.__str__()}")
            yield "Print"

        # regular query
        if self.type == 'r':

            if not self.gateA:
                return

            if outString(self.gateA, ":"):
                data_name, _, second_part = self.gateA.partition(":")
                action, _, query_pat = second_part.partition("(")
                query_pat = query_pat[:-1]
                data_struct: AbstractDataStructure = self.interpreter.datasets.get(data_name, None) or self.interpreter.datahashes.get(data_name, None)
                if data_struct:
                    if action == "Insert":
                        yield from data_struct.insert(query_pat)
                    elif action == "Remove":
                        yield from data_struct.remove(query_pat)
                    elif action == "Lookup":
                        yield from data_struct.lookup(query_pat)
                    elif action == "Clear":
                        if query_pat == "":
                            yield from data_struct.clear()

                elif match_type(data_name) == "title":
                    t_name, _, t_pattern = data_name.partition("(")
                    start = ""
                    if f"{t_name}.metacall" in self.interpreter.predicates and action != "metacall":
                        start = f"{t_name}.metacall(" + data_name + "," + action + ",[" + query_pat + "])&"
                    class_prefix = f"{t_name}.{action}"
                    if class_prefix in self.interpreter.predicates:
                        class_string = start + f"{class_prefix}(" + data_name + (query_pat and "," + query_pat) + ")"
                        print(class_string)
                        class_action = Query.create(self.interpreter, class_string)
                        if class_action:
                            yield from class_action.search(depth)
                    else:
                        if f"{t_name}.call" in self.interpreter.predicates:
                            class_string = start + f"{t_name}.call(" + data_name + "," + action + ",[" + query_pat + "])"
                            yield from Query.create(self.interpreter, class_string).search(depth)

                elif f"{data_name}.new" in self.interpreter.predicates:
                    for construct_dict in Query.create(self.interpreter, f"{data_name}.new(?construct)").search(depth):
                        construct = construct_dict.get("?construct", None)
                        if construct:
                            m = MatchDictionary.match(self.interpreter, action, construct)
                            if m:
                                yield m[1]

                return

            query_name, _, query_pat = self.gateA.partition("(")
            query_pat = query_pat[:-1]

            for mac in self.interpreter.macros:
                if mac in query_pat:
                    flag = True
                    break
            else:
                flag = False

            if flag:
                query_pat = self.interpreter.macroSimplified(query_pat, depth)
                if query_pat is None:
                    return

            if query_name == "True":
                yield {}
                return

            if "{" in query_name:
                self.type = "pi"
                yield from self.search(depth)
                return

            # if predicate is variable
            if query_name[0] == "?":
                if query_pat == "":
                    return
                for predicate in self.interpreter.predicates.values():
                    sol_with_predicate = {query_name: predicate.name}
                    Q = Query.create(self.interpreter, smart_replace(self.__str__(), sol_with_predicate))
                    depth.sub(1)
                    for sol in Q.search(depth):
                        if type(sol) == str:
                            yield sol
                            continue
                        complete_sol = smartUpdate(sol_with_predicate, sol)
                        yield complete_sol
                return

            if query_name in self.interpreter.pythons:
                try:
                    yield from self.interpreter.pythons[query_name](query_pat)
                except Exception as e:
                    self.interpreter.raiseError(f"Python Error: {e}")
                return

            if query_name == "Time":
                parts = splitWithoutParen(query_pat)
                if len(parts) != 1:
                    return
                m = MatchDictionary.match(self.interpreter, parts[0], str((time.time() - Query.start).__round__(2)))
                if m:
                    yield m[1]
                return

            if query_name == "ClockInit":
                if query_pat != "":
                    return
                Query.start = time.time()
                yield {}
                return

            found = [False]
            for solution in BuiltIns.builtin(self.interpreter, query_name, query_pat, depth, found):
                yield solution
            if found[0]:
                return

            if query_name in self.interpreter.domains:
                domain = self.interpreter.domains[query_name]
                # print(f"Found the domain {domain.name}")
                for sol in domain.search(depth, query_pat):
                    # print(f"Found a solution {sol}")
                    m = MatchDictionary.match(self.interpreter, query_pat, smart_replace(domain.raw_vars, sol))
                    if m:
                        yield m[1]
                return

            query_name, _, query_pat = self.gateA.partition("(")
            query_pat = query_pat[:-1]

            predicate_match = self.interpreter.predicates.get(query_name, None)

            if predicate_match:
                for search, backward, forwards in predicate_match.match(query_pat):
                    if search == 1:
                        yield backward
                    else:
                        next_q = Query.create(self.interpreter, search)
                        depth.sub(1)
                        for solution in next_q.search(depth):

                            if solution == "Request":
                                yield "Request"
                                continue
                            if solution == "Print":
                                yield "Print"
                                continue

                            solution_as_dict = {}
                            for key in backward.keys():

                                if backward[key] in solution.keys():
                                    solution_as_dict[key] = solution[backward[key]]
                                else:
                                    solution_as_dict[key] = processHead(smart_replace(backward[key], solution))

                            yield solution_as_dict

        # filter clause
        elif self.type == "~":

            if not self.gateA:
                return

            for sol in self.gateA.search(depth):
                if sol != "Print" and sol != "Request":
                    return

            yield {}

        # cut clause
        elif self.type == "\\":

            if not self.gateA or not self.gateB:
                return

            try:
                gen = self.gateA.search(depth)
                p_solution = next(gen)
                while p_solution in ["Print", "Request"]:
                    yield p_solution
                    p_solution = next(gen)

                updated_gate_B = self.gateB.add_new_info(p_solution)

                for solution in updated_gate_B.search(depth):

                    if solution == "Request":
                        yield "Request"
                        continue

                    if solution == "Print":
                        yield "Print"
                        continue

                    q_solution = p_solution.copy()
                    q_solution = smartUpdate(q_solution, solution)
                    yield q_solution

            except StopIteration:
                pass
            finally:
                return

        # and clause
        elif self.type == "&":

            if not self.gateA or not self.gateB:
                return

            for p_solution in self.gateA.search(depth):

                if p_solution == "Request":
                    yield "Request"
                    continue
                if p_solution == "Print":
                    yield "Print"
                    continue

                updated_gate_B = self.gateB.add_new_info(p_solution)

                for solution in updated_gate_B.search(depth):
                    if solution == "Request":
                        yield "Request"
                        continue
                    if solution == "Print":
                        yield "Print"
                        continue

                    q_solution = p_solution.copy()
                    q_solution = smartUpdate(q_solution, solution)
                    yield q_solution

        # or clause
        elif self.type == '|':

            if not self.gateA or not self.gateB:
                return

            yield from self.gateA.search(depth)
            yield from self.gateB.search(depth)

        # Lazy Or
        elif self.type == "$":
            if not self.gateA or not self.gateB:
                return

            first_solutions = self.gateA.search(depth)
            found_in_first = False
            for sol in first_solutions:
                if sol != "Print" or sol != "Request":
                    found_in_first = True
                yield sol

            if not found_in_first:
                yield from self.gateB.search(depth)

        # Package with input
        elif self.type == 'pi':

            if not self.gateA:
                return

            X = find_package_end(self.gateA)
            if not X:
                return

            p_pred, q_inp = X
            q_inp = q_inp[1:-1]
            p_name, _, p_inp = p_pred.partition("{")
            p_inp = p_inp[:-1]

            if p_name not in self.interpreter.packages:
                return

            flag_p = False
            flag_q = False

            for macro in self.interpreter.macros:
                if macro in p_inp:
                    flag_p = True
                if macro in q_inp:
                    flag_q = True
                if flag_p and flag_q:
                    break

            if flag_p:
                p_inp = self.interpreter.macroSimplified(p_inp, depth)
            if flag_q:
                q_inp = self.interpreter.macroSimplified(q_inp, depth)

            package_match = self.interpreter.packages[p_name]

            for search, backward in package_match.match(p_inp, q_inp):
                if search == 1:
                    yield backward
                    continue

                new_query = Query.create(self.interpreter, search)

                depth.sub(1)

                for solution in new_query.search(depth):
                    solution_as_dict = {}
                    for key in backward:
                        solution_as_dict[key] = smart_replace(backward[key], solution)
                    yield solution_as_dict

        # Folding
        elif self.type == '%':

            if not self.gateA:
                return

            if outString(self.gateA, "){"):
                X = find_package_end(self.gateA)
                if not X:
                    return

                p_pred, q_inp = X
                q_inp = q_inp[1:-1]
                p_name, _, p_inp = p_pred.partition("{")
                p_inp = p_inp[:-1]

                if outString(p_inp, "%"):
                    p_inp = unfold(p_inp)
                if outString(q_inp, "%"):
                    q_inp = unfold(q_inp)

                if not p_inp or not q_inp:
                    return

                new_query = Query.create(self.interpreter, f"{p_name}{{{p_inp}}}({q_inp})")
                yield from new_query.search(depth)

            try:
                query_name, _, query_pat = self.gateA.partition("(")
                query_pat = query_pat[:-1]
            except ValueError:
                return

            query_pat = unfold(query_pat)
            if query_pat == False or query_pat is None:
                return
            new_query = Query.create(self.interpreter, f"{query_name}({query_pat})")
            print(new_query)
            if new_query:
                yield from new_query.search(depth)
            return

        # Conditional Statements
        elif self.type == 'c':
            for key, value in self.cond:
                if not key or not value:
                    continue
                try:
                    search = key.search(depth)
                    p_solution = next(search)
                    while p_solution in ["Print", "Request"]:
                        yield p_solution
                        p_solution = next(search)
                    for sol in value.add_new_info(p_solution).search(depth):
                        if sol in ["Print", "Request"]:
                            yield sol
                            continue
                        yield smartUpdate(p_solution, sol)
                    return
                except StopIteration:
                    continue

        # Dereference
        elif self.type == "!":

            if not self.gateA:
                return

            if outString(self.gateA, "){"):
                X = find_package_end(self.gateA)
                if not X:
                    return

                p_pred, q_inp = X
                q_inp = q_inp[1:-1]
                p_name, _, p_inp = p_pred.partition("{")
                p_inp = p_inp[:-1]

                if outString(p_inp, "%"):
                    p_inp = deref(self.interpreter, p_inp)
                if outString(q_inp, "%"):
                    q_inp = deref(self.interpreter, q_inp)

                if not p_inp or not q_inp:
                    return

                new_query = Query.create(self.interpreter, f"{p_name}{{{p_inp}}}({q_inp})")
                yield from new_query.search(depth)

            try:
                query_name, _, query_pat = self.gateA.partition("(")
                query_pat = query_pat[:-1]
            except ValueError:
                return

            query_pat = deref(self.interpreter, query_pat)
            if not query_pat:
                return
            new_query = Query.create(self.interpreter, f"{query_name}({query_pat})")
            if new_query:
                yield from new_query.search(depth)
            return

        # Setting Reference
        elif self.type == ':=':
            if self.gateA not in self.interpreter.references:
                self.interpreter.raiseError(f"Error: Trying to change the value of non-existent reference, '{self.gateA}'")
                return
            elif independent(self.gateB):
                self.interpreter.raiseError(f"Error: trying to change the value of a reference to a value containing variables, '{self.gateB}'")
                return
            self.gateB = deref(self.interpreter, self.gateB)
            if not self.gateB:
                return
            self.gateB = processHead(self.gateB)
            if not self.gateB:
                return
            self.interpreter.references[self.gateA] = self.gateB
            yield {}

        else:
            self.interpreter.raiseError(f"Error: Illegal Query")

if __name__ == "__main__":
    q = Query.create(None, "?x:=3")
    print(q)


import os
from flask import Flask
from flask import request
from flask import render_template
import urllib
import time
from datetime import datetime
import threading
import random

r_time_str = ''
num_threads = 5
thread_delay = 3
loadtest = 0

class InitiateLoad (threading.Thread):
 def run (self):
     global loadtest
     global num_threads
     global thread_delay
     loadtest = 1
     while (loadtest):
      for x in xrange ( num_threads ):
       MyThread().start()
      time.sleep(thread_delay)

class MyThread ( threading.Thread ):
 def run (self):
     global r_time_str
     global thread_delay
     url = 'http://helloflask25.cfapps.io/fast'
     time.sleep (random.random()*thread_delay)
     start = datetime.now()
     nf = urllib.urlopen(url)
     page = nf.read()
     end = datetime.now()
     r_time_str += str(start)[:-4] + ',' + str(page) + ',' + (str(end - start)[:-4])[5:] + '<br>'
     nf.close()

app = Flask(__name__)

@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
    return render_template('hello.html', name=name)

@app.route('/monitor/')
def monitor():
    return render_template('flot/ajax/index.html')

@app.route('/mon/')
def mon():
    return render_template('index.html')
@app.route('/data/')
def data():
    url = 'http://monitor25.cfapps.io/static/flot/examples/ajax/data-usa-gdp-growth.json'
    nf = urllib.urlopen(url)
    page = nf.read()
    nf.close()
    return page


import sys
sys.path.insert(0, '../common_python')
import tools
import pytest
import os, sys

def test_unit_lbann2_reload(cluster, exe, dirname):
    lbann2  = exe + '2'
    model_path = '{../../model_zoo/models/lenet_mnist/model_lenet_mnist.prototext,../../model_zoo/tests/model_lenet_mnist_lbann2ckpt.prototext}'
    command = tools.get_command(
        cluster=cluster, executable=lbann2, data_reader_name='mnist',
        data_filedir_ray='/p/gscratchr/brainusr/datasets/MNIST',
        dir_name=dirname,
        model_path=model_path,
        optimizer_name='sgd',
        num_epochs=2)
    return_code = os.system(command)
    if return_code != 0:
        sys.stderr.write('LBANN2 LeNet execution failed, exiting with error')
        sys.exit(1)

    os.system('mv lbann2_ckpt lbann2_nockpt')

    command = tools.get_command(
        cluster=cluster, executable=exe, num_nodes=1, num_processes=1,
        dir_name=dirname,
        data_filedir_ray='/p/gscratchr/brainusr/datasets/MNIST',
        data_reader_name='mnist', model_folder='tests',
        model_name='lenet_mnist_ckpt', num_epochs=2, optimizer_name='sgd')
    return_code_ckpt_1 = os.system(command)
    if return_code_ckpt_1 != 0:
        sys.stderr.write('LeNet (checkpoint) execution failed, exiting with error')
        sys.exit(1)

    command = tools.get_command(
        cluster=cluster, executable=lbann2, num_nodes=1, num_processes=1,
        dir_name=dirname,
        data_filedir_ray='/p/gscratchr/brainusr/datasets/MNIST',
        data_reader_name='mnist',
        model_path='../../model_zoo/tests/model_lenet_mnist_lbann2ckpt.prototext', num_epochs=2, optimizer_name='sgd', ckpt_dir='ckpt/')
    return_code_ckpt_2 = os.system(command)
    if return_code_ckpt_2 != 0:
        sys.stderr.write('LBANN2 LeNet weight reload failed, exiting with error')
        sys.exit(1)
    os.system('rm lbann2_ckpt/model0-epoch*')
    os.system('rm lbann2_nockpt/model0-epoch*')
    diff_test = os.system('diff -rq lbann2_ckpt/ lbann2_nockpt/')
    os.system('rm -rf ckpt')
    os.system('rm -rf lbann2_*')
    assert diff_test == 0


import unittest
from dependencies.python.fmlaas.model import JobBuilder
from dependencies.python.fmlaas.model import JobConfiguration
from dependencies.python.fmlaas.model import ExperimentBuilder
from dependencies.python.fmlaas.model import ProjectBuilder

class AbstractModelTestCase(unittest.TestCase):

    def _build_default_experiment(self):
        experiment = self._build_parameterized_experiment(1)

        experiment_json = {
            "ID" : "123dafasdf34sdfsdf_1",
            "jobs" : [],
            "current_job" : "NONE",
            "start_model" : {},
            "current_model" : {},
            "hyperparameters" : {},
            "is_active" : False
        }

        return experiment, experiment_json

    def _build_parameterized_experiment(self, id):
        builder = ExperimentBuilder()
        builder.id = "123dafasdf34sdfsdf_{}".format(id)

        return builder.build()

    def _build_job(self, id):
        builder = JobBuilder()
        builder.set_id("test_id_{}".format(id))
        builder.set_experiment_id("123dafasdf34sdfsdf")
        builder.set_project_id("fl_group_1232234")
        builder.set_devices(["123", "234"])
        configuration = JobConfiguration(2, 0, "RANDOM", [])
        builder.set_configuration(configuration.to_json())

        return builder.build()

    def _build_simple_project(self, id):
        builder = ProjectBuilder()
        builder.set_id("id_{}".format(id))
        builder.set_name("my_name")

        return builder.build()

    def _get_simple_project_json(self, id):
        return {
            'name': 'my_name',
            'ID': 'id_{}'.format(id),
            'devices': {},
            "experiments": {},
            "members": {},
            "billing": {}}


from PyQt5.QtCore import QSize
from ..ekmap_common import *
from ..ekmap_converter import eKConverter
from ..qgslayer_parser.symbol_layer_factory import SymbolLayerFactory
import json, hashlib, os.path

class SymbologyParser():

    # This is abstract method
    def parse(self, renderer, exporter):
        raise NotImplementedError("Please Implement this method")

    def _wrapSymbolLayer(self, symbol, exporter):
        styleLayers = []
        for symbolLayer in symbol.symbolLayers():
            styleParser = SymbolLayerFactory.getLayerParser(symbolLayer, exporter)
            if styleParser is not None:
                styles = styleParser.parse()
                # Case POINT
                if symbolLayer.type() == 0:
                    key = json.dumps(styles)
                    key = hashlib.md5(key.encode()).hexdigest()
                    sizeUnit = eKConverter.convertRenderUnitValueToName(symbol.sizeUnit())
                    size = eKConverter.convertUnitToPixel(symbol.size(), sizeUnit)
                    exportImagePath = TEMP_LOCATION + '/' + key + '.png'
                    symbol.exportImage(exportImagePath, 'png', QSize(size, size))
                    if os.path.isfile(exportImagePath):
                        exporter.externalGraphics.append(exportImagePath)
                        styleLayers.append({
                            'type': 'symbol',
                            'layout': {
                                'icon-image': key
                            }
                        })
                # Case LINE or POLYGON
                else:
                    styleLayers.extend(styleParser.parse())

        return styleLayers


from django.http import HttpResponseForbidden,HttpResponseRedirect
from django.urls import reverse
from django.views.generic import FormView
from django.views import View
from django.views.generic.detail import SingleObjectMixin
from django.shortcuts import render,get_object_or_404
from django.views.generic import (
	ListView,
	DetailView,
	CreateView,
	UpdateView,
	DeleteView,
	)
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.views.generic.edit import FormMixin
from django.views.generic.edit import ModelFormMixin
from .models import Campaign
from users.models import MyUser
from peak.models import Peak
from django.contrib import messages
from peak.forms import PeakForm
from django.http import JsonResponse

from user_activities.forms import CampaignEnroll
from django import template
from user_activities.models import Campaign_enrollment,Peak_annotations
import json
from django.http import Http404
import datetime
from .forms import Update_Campaign,Create_campaigns

# Create your views here.

#this will be the home page for one campaign to check the progress
#peaks etc, this can bee seen only by admin of each campaign

### TO DO :
	# 1) MAKE THIS ACCESIBLE ONLY TO LOGGIN USERS
	# 2) SHOW HERE THE CLICKED POST

def campaign(request):
	context = {
        'campaign': Campaign.objects.all()
    }
	return render(request,'campaign/campaign.html',context)

# class base viewd
class CampaignListView(ListView):
	model 								= Campaign
	template_name 						= 'campaign/campaign.html' # <app>/<model>_<viewtype>.html
	context_object_name 				= 'campaign'
	ordering 							= ['-date_posted']

	# making a queryset so on the campaign page will not be shown the campaigns the user has already enrolled
	def get_queryset(self):
		actualUser_enrolledTo = list(Campaign_enrollment.objects.filter(user_id = self.request.user.id).values())
		enrolledTo 			  = Campaign_enrollment.objects.filter(user_id = self.request.user.id)
		campaigns = Campaign.objects.exclude( id__in  = [x['campaign_id_id'] for x in actualUser_enrolledTo])
		return campaigns

	def get_context_data(self, **kwargs):
		#print(campaigns)
		context 						= super(CampaignListView, self).get_context_data(**kwargs)
		context["actualUser"] 			= self.request.user
		context["userHasEnrolled"]		= CheckUserEnroll(self.request.GET.get('id'), self.request.user.id)
		context["closed_campaigns"] 	= Campaign.objects.filter(status = 'Closed')
		context["actual_date"]  		= datetime.datetime.now().strftime("%Y-%m-%d")

		return context

	#to do here is to clear the form after submit
	def post(self, request, *args, **kwargs):
		# When the form is submitted, it will enter here
		self.object 					= None
		post 							= Campaign_enrollment()

		#get the campaign enrollment user ids
		#user_enrolled 		= CheckUserEnroll(request.user)
		user_enrolled_to 				= CheckUserEnroll(request.POST.get('id'), request.user )
		if user_enrolled_to:
			#print("is enrollment")
			messages.warning(request, 'You are already Enrolled')
			return HttpResponseRedirect(reverse('campaign-home'))
		else:
			#print("is not enrollment")
			post.campaign_id			= Campaign.objects.get(id = request.POST.get('id'))
			post.user_id   				= request.user
			post.save()
			# Whether the form validates or not, the view will be rendered by get()
			messages.success(request, 'You have Enrolled')
			return HttpResponseRedirect(reverse('campaign-detail', kwargs={'pk': request.POST.get('id')}))

# a view for individual campaign
# here i am sticking to django default template just to show the difference
class CampaignDetailView(UserPassesTestMixin,LoginRequiredMixin,FormMixin,DetailView):
	model 		 = Campaign
	form_class 	 = PeakForm

	def get_success_url(self):
		return reverse("peak-list", kwargs={"pk": self.object.id})

	def get_context_data(self, **kwargs):
		context 						= super(CampaignDetailView, self).get_context_data(**kwargs)
		context["form"] 				= self.get_form()
		context["actualUser"] 			= self.request.user
		context["userHasEnrolled"]		= CheckUserEnroll(self.object.id, self.request.user.id)
		peak_ids						= Peak.objects.filter(campaign_id = self.kwargs['pk'], status=1).values_list('id', flat=True)
		context["peaks_notannotate"] 	= Peak.objects.filter(campaign_id = self.kwargs['pk'], status=0).values()
		peaks							= Peak.objects.filter(campaign_id = self.kwargs['pk'], status=1).values()
		context["totalPeaksForCampaign"]= peaks.count()
		annotatedPeaks = []
		rejectedPeaks  = []
		new_annotations  = []
		allPeaks 	   = 0
		#find the total non annotated peaks
		peaks = list(peaks)
		incr = 0
		annotated_peaks = None
		rejected_peaks  = None
		peaks
		if peaks:
			#here we get all the annotations that the peaks of the campaigns have annotations
			annotated_peaks  = Peak_annotations.objects.filter(peak_id__in = peak_ids).values_list('peak_id').distinct()
			rejected_peaks   = Peak_annotations.objects.filter(peak_id__in = peak_ids,status=0).values('peak_id').distinct()
			for peak in peaks:
				temp  = Peak_annotations.objects.filter(peak_id = peak['id']).values('peak_id').distinct()
				temp2 = list(Peak_annotations.objects.filter(peak_id = peak['id'],status=0).values('peak_id').distinct())
				temp3 = list(Peak_annotations.objects.filter(peak_id = peak['id']).values())
				temp4 = Peak_annotations.objects.filter(peak_id = peak['id'],status ='').values('peak_id').distinct()
				#get all the peaks

				if temp:
					annotatedPeaks.append(temp)

				if temp2:
					rejectedPeaks.append(temp2)

				if temp3:
					allPeaks = temp3

				if temp4:
					new_annotations.append(temp4)
			if allPeaks:
				for i in allPeaks:
					for p2 in allPeaks:
						if i['peak_id_id'] == p2['peak_id_id']:
							if i['valued'] != p2['valued']:
								incr += 1
			context['notAnnotatedPeaks'] 	= context["totalPeaksForCampaign"]-len(annotatedPeaks)
			context['AnnotatedPeaks'] 		= len(annotatedPeaks)
			#list of peaks who have been annotated at least once.
			context['annotatedList'] 		= Peak.objects.filter(id__in = annotated_peaks).exclude(id__in = new_annotations).values()
			#list of peaks with at least one rejected annotatio
			context['rejectedList']			= Peak.objects.filter(id__in = rejected_peaks).values()
			#peaks who have new annotations
			context['new_annotations']      = Peak.objects.filter(id__in = new_annotations).values()
			#campaign peaks not annotated
			context["campaign_peaks"] 		= Peak.objects.filter(campaign_id = self.kwargs['pk'], status=1).exclude(id__in = annotated_peaks).values()
			#print(rejected_peaks)
			context['RejectedPeaks'] 		= len(rejectedPeaks)
			context['conflix']				= incr
		return context

	def post(self, request, *args, **kwargs):
		if self.request.is_ajax():
			mydata = json.loads(request.body)
			if request.method == 'POST' and mydata['action'] == 'getPeakDataForAdmin':
				#print(mydata['action'])
				peaks1 = list(Peak.objects.filter(id = mydata['peak_id']).values())
				annotation_evaluated  = list(Peak_annotations.objects.filter(peak_id = mydata['peak_id']).values())
				positive = 0
				negative = 0
				if annotation_evaluated:
					for i in annotation_evaluated:
						if i['valued'] == 1:
							positive += 1
						elif i['valued'] == 0:
							negative += 1
				print(negative)
				print(positive)
				data = {
					'peaks_json':peaks1,
					'annotations':annotation_evaluated,
					'positive':positive,
					'negative':negative
					}
				return JsonResponse({'peaks': data},content_type='application/json')
		else:
			if 'evaluateAnnotationForm' in request.POST:
				#print("okay we in")
				self.object 			= None
				annotation_peak 		= self.request.POST.get('hidden_peak_id')
				annotation 				= Peak_annotations.objects.get(id = annotation_peak)

				annotation.status 		= self.request.POST.get('evaluateAnnotation')
				campaign_id 			= self.request.POST.get('act_cpm')
				#annotation.valued 		= True
				#print(annotation.status)
				#post.campaign_id	= Campaign.objects.get(id = request.POST.get('id'))
				#post.user_id   	= request.user
				annotation.save()
				messages.success(request, 'You revied the annotaion')
				return HttpResponseRedirect(reverse('campaign-detail', kwargs={'pk': campaign_id}))
			elif 'addPeakForm' in request.POST:
				self.object = self.get_object()
				form = self.get_form()
				if form.is_valid():
					return self.form_valid(form)
				else:
					messages.warning(request,  form.errors)
					return HttpResponseRedirect(reverse('campaign-detail', kwargs={'pk': self.object.id}))
		return HttpResponseRedirect(reverse('campaign-detail', kwargs={'pk': self.object.id}))

	def test_func(self):
		if self.request.user.is_manager == True:
			return True
		return False

	def handle_no_permission(self):
		self.object = self.get_object()
		if self.raise_exception:
			raise PermissionDenied(self.get_permission_denied_message())
		return HttpResponseRedirect(reverse('peak-list', kwargs={'pk': self.object.id}))

	def form_valid(self,form):
		form.fields = self.object
		#form.instance.name gets the name of the tag field
		mydata 		= form.instance.fileJson
		mydataTemp	= json.load(mydata)
		mydataTemp 	= json.dumps(mydataTemp)
		mydata3 	= json.loads(mydataTemp)

		#status of the uploaded peak
		status 		= form.instance.status
		peak_exists = []
		i = 0
		for data in mydata3:
			#self.object gets here the Entity we are viweing
			form.instance.id 			= None
			form.instance.peak_id 		= data['id']
			form.instance.lat 			= data['latitude']
			form.instance.lon			= data['longitude']
			form.instance.alt			= data['elevation']
			form.instance.localize_names= data['localized_names']
			form.instance.provenance_org= data['provenance']
			form.instance.name 			= data['name']
			form.instance.campaign_id 	= self.object
			form.instance.status 		= status

			peak_check = Peak.objects.filter(peak_id = data['id'],campaign_id= self.object).values()
			if peak_check:
				#peak_exists = peak_exists + data
				print("exists")
			else:
				print("IN")
				i = i + 1
				form.save()

			print(peak_check)
		if i > 0:
			return super().form_valid(form)
		else:
			messages.error(self.request,"The file you tried to add has all the Peaks already registered ")
			return HttpResponseRedirect(reverse('campaign-detail', kwargs={'pk': self.object.id}))

# creatign a new campaign
class CampaignCreateView(UserPassesTestMixin,LoginRequiredMixin,CreateView):
	model = Campaign
	# passing the field for the form for each campaign
	#fields = ['name','status','start_date','end_date']
	form_class = Create_campaigns
	#checking if form is valid and we add the current user to the form
	def form_valid(self,form):
		#user_id is the id of the user we want to associate to the campaign
		form.instance.user_id = self.request.user
		return super().form_valid(form)

	def test_func(self):
		# getting the campaign that we are currently trying to update
		#the first parametter gets the  user
		if self.request.user.is_manager == True:
			return True
		return False

# creatign a new campaign
class CampaignUpdateView(LoginRequiredMixin,UserPassesTestMixin, UpdateView):
	model 		   		= Campaign
	template_name  		= 'campaign/campaign_update.html' # <app>/<model>_<viewtype>.html
	context_object_name = 'campaign-update'
	form_class 			= Update_Campaign
	# passing the field for the form for each campaign
	#fields = ['name','status','start_date','end_date']

	#checking if form is valid and we add the current user to the form
	def form_valid(self,form):
		#user_id is the id of the user we want to associate to the campaign
		form.instance.user_id = self.request.user
		return super().form_valid(form)

	def test_func(self):
		# getting the campaign that we are currently trying to update
		campaign = self.get_object()
		#the first parametter gets the  user
		if self.request.user == campaign.user_id:
			return True
		return False

	def test_func(self):
		# getting the campaign that we are currently trying to update
		#the first parametter gets the  user
		if self.request.user.is_manager == True:
			return True
		return False

class CampaignDeleteView(LoginRequiredMixin,UserPassesTestMixin,DeleteView):
	model = Campaign
	success_url = '/profile/'

	def test_func(self):
		# getting the campaign that we are currently trying to update
		campaign = self.get_object()
		#the first parametter gets the  user
		if self.request.user == campaign.user_id:
			return True
		return False

	def test_func(self):
		# getting the campaign that we are currently trying to update
		#the first parametter gets the  user
		if self.request.user.is_manager == True:
			return True
		return False

def CheckUserEnroll(campaign,user):
	hasEnrolled = Campaign_enrollment.objects.filter(campaign_id = campaign,user_id=user)
	if hasEnrolled:
		return hasEnrolled
	return False

register = template.Library()
@register.filter(name='ifUserEnrolled')
def ifUserEnrolled(value, arg):
    return True


from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from sqlalchemy.orm import join

from .models import Item, Comment
from .schemas import ItemCreate, CommentCreate

async def get_items_db(db: AsyncSession, skip: int = 0, limit: int = 100):
    result = await db.execute(select(Item).offset(skip).limit(limit))

    return result.scalars().all()

async def get_item_db(db: AsyncSession, item_id: int):
    result = await db.execute(select(Item).filter(Item.id == item_id))
    return result.scalars().first()

async def create_user_item(db: AsyncSession, item: ItemCreate, user_id: int):
    db_item = Item(**item.dict(), owner_id=user_id)
    db.add(db_item)
    try:
        await db.commit()
        await db.refresh(db_item)
    except IntegrityError as ex:
        await db.rollback()
    else:
        return db_item

async def create_comment_item(
        db: AsyncSession,
        comment: CommentCreate,
        user_id: int,
        item_id: int
):
    db_comment = Comment(**comment.dict(), owner_id=user_id, item_id=item_id)
    db.add(db_comment)
    try:
        await db.commit()
        await db.refresh(db_comment)
    except IntegrityError as ex:
        await db.rollback()
    else:
        return db_comment

async def get_comments_db(db: AsyncSession, item_id: int, skip: int = 0, limit: int = 100):
    result = await db.execute(select(Comment).filter(
        Comment.item_id == item_id
    ).offset(skip).limit(limit))

    return result.scalars().all()


import requests

from loguru import logger

host = "http://localhost:9999"

r = requests.get(f"{host}/train")
logger.info(r.text)

payload = [
    {"Age": 85, "Sex": "male", "Embarked": "S"},
    {"Age": 24, "Sex": "female", "Embarked": "C"},
    {"Age": 3, "Sex": "male", "Embarked": "C"},
    {"Age": 21, "Sex": "male", "Embarked": "S"},
]

r = requests.post(f"{host}/predict", json=payload)
logger.info(r.text)

#if you dont want to remove the model.comment the following code
r = requests.get(f"{host}/wipe")
logger.info(r.text)

"""
from urllib.parse import urlparse
import http.client as ht
def getStatus(ourl):
    try:
        url = urlparse(ourl)
        conn = ht.HTTPConnection(url.netloc)
        conn.request("HEAD", url.path)
        res = conn.getresponse()
        return res.status, ourl
    except:
        return "error", ourl
getStatus(ourl="http://127.0.0.1:5000/train")
"""


import torch
import os
import re
from pathlib import Path
from functools import partial
import esm
from torch.nn.utils.rnn import pad_sequence
from transformers import AlbertTokenizer, AutoModelForMaskedLM, logging
from tf_bind_transformer.cache_utils import cache_fn, run_once, md5_hash_fn

def exists(val):
    return val is not None

def map_values(fn, dictionary):
    return {k: fn(v) for k, v in dictionary.items()}

def to_device(t, *, device):
    return t.to(device)

def cast_tuple(t):
    return (t,) if not isinstance(t, tuple) else t

PROTEIN_EMBED_USE_CPU = os.getenv('PROTEIN_EMBED_USE_CPU', None) is not None

if PROTEIN_EMBED_USE_CPU:
    print('calculating protein embed only on cpu')

# global variables

GLOBAL_VARIABLES = {
    'model': None,
    'tokenizer': None
}

# general helper functions

def calc_protein_representations_with_subunits(proteins, get_repr_fn, *, device):
    representations = []

    for subunits in proteins:
        subunits = cast_tuple(subunits)
        subunits_representations = list(map(get_repr_fn, subunits))
        subunits_representations = list(map(partial(to_device, device = device), subunits_representations))
        subunits_representations = torch.cat(subunits_representations, dim = 0)
        representations.append(subunits_representations)

    lengths = [seq_repr.shape[0] for seq_repr in representations]
    masks = torch.arange(max(lengths), device = device)[None, :] <  torch.tensor(lengths, device = device)[:, None]
    padded_representations = pad_sequence(representations, batch_first = True)

    return padded_representations.to(device), masks.to(device)

# esm related functions

ESM_MAX_LENGTH = 1024
ESM_EMBED_DIM = 1280

INT_TO_AA_STR_MAP = {
    0: 'A',
    1: 'C',
    2: 'D',
    3: 'E',
    4: 'F',
    5: 'G',
    6: 'H',
    7: 'I',
    8: 'K',
    9: 'L',
    10: 'M',
    11: 'N',
    12: 'P',
    13: 'Q',
    14: 'R',
    15: 'S',
    16: 'T',
    17: 'V',
    18: 'W',
    19: 'Y',
    20: '_'
}

def tensor_to_aa_str(t):
    str_seqs = []
    for int_seq in t.unbind(dim = 0):
        str_seq = list(map(lambda t: INT_TO_AA_STR_MAP[t] if t != 20 else '', int_seq.tolist()))
        str_seqs.append(''.join(str_seq))
    return str_seqs

@run_once('init_esm')
def init_esm():
    model, alphabet = esm.pretrained.esm1b_t33_650M_UR50S()
    batch_converter = alphabet.get_batch_converter()

    if not PROTEIN_EMBED_USE_CPU:
        model = model.cuda()

    GLOBAL_VARIABLES['model'] = (model, batch_converter)

def get_single_esm_repr(protein_str):
    init_esm()
    model, batch_converter = GLOBAL_VARIABLES['model']

    data = [('protein', protein_str)]
    batch_labels, batch_strs, batch_tokens = batch_converter(data)

    if batch_tokens.shape[1] > ESM_MAX_LENGTH:
        print(f'warning max length protein esm: {protein_str}')

    batch_tokens = batch_tokens[:, :ESM_MAX_LENGTH]

    if not PROTEIN_EMBED_USE_CPU:
        batch_tokens = batch_tokens.cuda()

    with torch.no_grad():
        results = model(batch_tokens, repr_layers=[33])

    token_representations = results['representations'][33]
    representation = token_representations[0][1 : len(protein_str) + 1]
    return representation

def get_esm_repr(proteins, device):
    if isinstance(proteins, torch.Tensor):
        proteins = tensor_to_aa_str(proteins)

    get_protein_repr_fn = cache_fn(get_single_esm_repr, path = 'esm/proteins')

    return calc_protein_representations_with_subunits(proteins, get_protein_repr_fn, device = device)

# prot-albert 2048 context length

PROT_ALBERT_PATH = 'Rostlab/prot_albert'
PROT_ALBERT_DIM = 4096
PROT_ALBERT_MAX_LENGTH = 2048

def protein_str_with_spaces(protein_str):
    protein_str = re.sub(r"[UZOB]", 'X', protein_str)
    return ' '.join([*protein_str])

@run_once('init_prot_albert')
def init_prot_albert():
    GLOBAL_VARIABLES['tokenizer'] = AlbertTokenizer.from_pretrained(PROT_ALBERT_PATH, do_lower_case = False)
    model = AutoModelForMaskedLM.from_pretrained(PROT_ALBERT_PATH)

    if not PROTEIN_EMBED_USE_CPU:
        model = model.cuda()

    GLOBAL_VARIABLES['model'] = model

def get_single_prot_albert_repr(
    protein_str,
    max_length = PROT_ALBERT_MAX_LENGTH,
    hidden_state_index = -1
):
    init_prot_albert()
    model = GLOBAL_VARIABLES['model']
    tokenizer = GLOBAL_VARIABLES['tokenizer']

    encoding = tokenizer.batch_encode_plus(
        [protein_str_with_spaces(protein_str)],
        add_special_tokens = True,
        padding = True,
        truncation = True,
        max_length = max_length,
        return_attention_mask = True,
        return_tensors = 'pt'
    )

    if not PROTEIN_EMBED_USE_CPU:
        encoding = map_values(lambda t: t.cuda(), encoding)

    model.eval()
    with torch.no_grad():
        outputs = model(**encoding, output_hidden_states = True)

    hidden_state = outputs.hidden_states[hidden_state_index][0]
    return hidden_state

def get_prot_albert_repr(
    proteins,
    device,
    max_length = PROT_ALBERT_MAX_LENGTH,
    hidden_state_index = -1
):
    if isinstance(proteins, str):
        proteins = [proteins]

    if isinstance(proteins, torch.Tensor):
        proteins = tensor_to_aa_str(proteins)

    get_protein_repr_fn = cache_fn(get_single_prot_albert_repr, path = f'proteins/prot_albert')

    return calc_protein_representations_with_subunits(proteins, get_protein_repr_fn, device = device)

# alphafold2 functions

AF2_MAX_LENGTH = 2500
AF2_EMBEDDING_DIM = 384

AF2_DIRECTORY = os.getenv('TF_BIND_AF2_DIRECTORY', os.path.expanduser('~/.cache.tf.bind.transformer/.af2_embeddings'))
AF2_DIRECTORY_PATH = Path(AF2_DIRECTORY)

def get_single_alphafold2_repr(
    protein_str,
    max_length = AF2_MAX_LENGTH,
):
    md5 = md5_hash_fn(protein_str)
    embedding_path = AF2_DIRECTORY_PATH / f'{md5}.pt'
    assert embedding_path.exists(), f'af2 embedding not found for {protein_str}'

    tensor = torch.load(str(embedding_path))
    return tensor[:max_length]

def get_alphafold2_repr(
    proteins,
    device,
    max_length = AF2_MAX_LENGTH,
    **kwargs
):
    representations = []

    for subunits in proteins:
        subunits = cast_tuple(subunits)
        subunits = list(map(lambda t: get_single_alphafold2_repr(t, max_length = max_length), subunits))
        subunits = torch.cat(subunits, dim = 0)
        representations.append(subunits)

    lengths = [seq_repr.shape[0] for seq_repr in representations]
    masks = torch.arange(max(lengths), device = device)[None, :] <  torch.tensor(lengths, device = device)[:, None]
    padded_representations = pad_sequence(representations, batch_first = True)

    return padded_representations.to(device), masks.to(device)

# factory functions

PROTEIN_REPR_CONFIG = {
    'esm': {
        'dim': ESM_EMBED_DIM,
        'fn': get_esm_repr
    },
    'protalbert': {
        'dim': PROT_ALBERT_DIM,
        'fn': get_prot_albert_repr
    },
    'alphafold2': {
        'dim': AF2_EMBEDDING_DIM,
        'fn': get_alphafold2_repr
    }
}

def get_protein_embedder(name):
    allowed_protein_embedders = list(PROTEIN_REPR_CONFIG.keys())
    assert name in allowed_protein_embedders, f"must be one of {', '.join(allowed_protein_embedders)}"

    config = PROTEIN_REPR_CONFIG[name]
    return config


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

# In[1]:

# Implement by Zhe Chu, Jun 17,2021
# This python script file implement the THz Channel model introduced by
#A. Saeed, O. Gurbuz, A. O. Bicen and M. A. Akkas,
#"Variable-Bandwidth Model and Capacity Analysis for Aerial Communications in the Terahertz Band,"
#in IEEE Journal on Selected Areas in Communications, vol. 39, no. 6, pp. 1768-1784, June 2021, doi: 10.1109/JSAC.2021.3071831.

# In[2]:

import matplotlib.pyplot as plt

import numpy as np

import math
from matplotlib.pyplot import figure
import matplotlib.pylab as pylab

# In[3]:

# Our model focusing on altitude between 10km to 16km. US standard 1976 is our atmospheric model.
# Antenna transmission power = 37 dBm, gain = 80 dBi.
# band (0.75-10THz) in the paper; however, we only take 10% of the whole band.

# In[4]:

# band (0.75-10THz)
# 10km alitutde model
altitude = 10 #km

transmission_distance = [1,2,3,4,5,6,7,8,9,10] #km

capacity =[11.53, 3.5, 1.5, 1, 0.5, 0.3, 0.2, 0.1, 0.1, 0] #Tbps

zenith_angle = [0,30,60,90,120,150,180]

zenith_capacity = [14.1, 14, 13.5, 11.53, 10, 8.5, 8.1] # with distance = 1km

# In[5]:

# I divided it by 10 right here, sice the real bandwidth can be use is much smaller

capacity_copy = [element * 0.027 for element in capacity]

zenith_capacity_copy = [element * 0.027 for element in zenith_capacity]

capacity = capacity_copy

zenith_capacity = zenith_capacity_copy

# In[6]:

def estimate_data_at_0_distance(transmission_distance,capacity):

    if(transmission_distance[0]!=0):

        slop = (capacity[1] - capacity[0])/(transmission_distance[1]-transmission_distance[0])

        value_at_zero = capacity[0]-slop*transmission_distance[0]

        #print(slop,value_at_zero)

        transmission_distance.insert(0,0)

        capacity.insert(0,value_at_zero)

    return([transmission_distance,capacity])

# In[7]:

[transmission_distance,capacity] = estimate_data_at_0_distance(transmission_distance,capacity)

#print(transmission_distance)
#print(capacity)

# In[8]:

def plot_channel_capacity(altitude, transmission_distance,capacity,zenith_angle=[],zenith_capacity=[] ):

    params = {'legend.fontsize': 'x-large',
              'figure.figsize': (10, 5),
              'axes.labelsize': 'x-large',
              'axes.titlesize':'x-large',
              'xtick.labelsize':'x-large',
              'ytick.labelsize':'x-large'}
    pylab.rcParams.update(params)

    plt.rcParams.update({'font.size': 11})

    # =========channel capacity vs distance=========
    plot1 = plt.figure(1)

    plt.plot(transmission_distance, capacity)
    # naming the x axis
    plt.xlabel('Transmission Distance (km)')
    # naming the y axis
    plt.ylabel('Capacity(Tbps)')
    # giving a title to my graph
    title_name = 'Capacity(Tbps) vs Distance at Altitude '+str(altitude)+ ' km'

    plt.title(title_name)

    plt.ylim(0,)
    plt.xlim(1,)
    plt.grid()

    for a,b in zip(transmission_distance,capacity):
        plt.text(a, b, str(b))

    plt.xticks(np.arange(min(transmission_distance), max(transmission_distance)+1, 1.0))

    # =========channel capacity vs zenith angle===========
    if(len(zenith_capacity)!=0 and len(zenith_angle)!=0):

        plot1 = plt.figure(2)

        plt.plot(zenith_angle, zenith_capacity)
        # naming the x axis
        plt.xlabel('Zenith Angle (degrees)')
        # naming the y axis
        plt.ylabel('Capacity(Tbps)')
        # giving a title to my graph
        title_name = 'Capacity(Tbps) vs Zenith Angle (degrees) at Altitude '+str(altitude)+ ' km'

        plt.title(title_name)

        plt.ylim(zenith_capacity[-1]*0.9,zenith_capacity[0]*1.1)
        plt.xlim(0,180)
        plt.grid()

        for a,b in zip(zenith_angle, zenith_capacity):
            plt.text(a, b, str(b))

        plt.xticks(np.arange(min(zenith_angle), max(zenith_angle)+1,30))

        plt.show()

#plot_channel_capacity(altitude, transmission_distance,capacity,zenith_angle,zenith_capacity)

# In[9]:

def Reverse(lst):
    return [ele for ele in reversed(lst)]

# In[10]:

class channels:

    def __init__(self, transmission_distance, capacity, zenith_angle, zenith_capacity, altitude):

        self.transmission_distance = transmission_distance

        self.capacity = capacity

        self.zenith_angle = zenith_angle

        self.zenith_capacity = zenith_capacity

        self.altitude = altitude

# In[11]:

jet_channel =  channels(transmission_distance, capacity, zenith_angle, zenith_capacity, altitude)

# In[12]:

# 16km alitutde model
# UAV altitude data

# In[13]:

# 33.6 dBm, 80 dBi #ignor the minor changes in antenna dBm
# band (0.75-10THz)

altitude = 16 #km

transmission_distance = [1,2,3,4,5,6,7,8,9,10] #km

capacity =[34.65, 15, 7.5, 4, 3.5, 2, 1.5, 1, 0.5, 0] #Tbps

zenith_angle = [0,30,60,90,120,150,180]

zenith_capacity = [36.3, 36.2, 35.5, 34.65,33.4, 32.5, 32.18] # with distance = 1km

# In[14]:

# I divided it by 10 right here, sice the real bandwidth can be use is much smaller

capacity_copy = [element * 0.027 for element in capacity]

zenith_capacity_copy = [element * 0.027 for element in zenith_capacity]

capacity = capacity_copy

zenith_capacity = zenith_capacity_copy

# In[15]:

[transmission_distance,capacity] = estimate_data_at_0_distance(transmission_distance,capacity)

# In[16]:

#plot_channel_capacity(altitude, transmission_distance,capacity,zenith_angle,zenith_capacity)

# In[17]:

UAV_channel =  channels(transmission_distance, capacity, zenith_angle, zenith_capacity, altitude)

# In[18]:

#print(UAV_channel.zenith_capacity)
#print(jet_channel.zenith_capacity)

# In[19]:

# Accroding to fig 6 in paper, channel capacity is almost linearly increasing vs altitude increasing
# Hence, channel capacitance at each specific altitude can be estimated
# However, what we want to have is the channel capacitance for two nodes at different altitude.
# Capacitance vs Zenith Angle graphy provide us the vertical(altitude) direction information
# We want to estimate Capacitance vs Zenith Angle at each altitude

# In[20]:

def cap_vs_zenith_at_target_altitude(target_altitude,                                     low_altitude_channel_model = jet_channel,                                     high_altitude_channel_model = UAV_channel ):

    angles = low_altitude_channel_model.zenith_angle

    start_altitude = low_altitude_channel_model.altitude

    end_altitude = high_altitude_channel_model.altitude

    start_cap_vs_znith_curve = low_altitude_channel_model.zenith_capacity

    end_cap_vs_znith_curve   = high_altitude_channel_model.zenith_capacity

    target_altitude = target_altitude

    cap_vs_zenith_at_target_altitude = []

    for i in range(len(start_cap_vs_znith_curve)):

        bot_val = start_cap_vs_znith_curve[i]

        up_val = end_cap_vs_znith_curve[i]

        slope = (up_val-bot_val)/(end_altitude-start_altitude)

        #print(slope)

        new_val = bot_val + slope* (target_altitude - start_altitude)

        new_val = max(0,new_val)

        cap_vs_zenith_at_target_altitude.append(new_val)

    return(cap_vs_zenith_at_target_altitude)

# In[21]:

#test_cap_vs_zenith = cap_vs_zenith_at_target_altitude(14)

#print(test_cap_vs_zenith)

# In[22]:

#plot
'''
for test_altitude in range(jet_channel.altitude - 1, UAV_channel.altitude + 1):

    params = {'legend.fontsize': 'x-large',
              'figure.figsize': (30, 20),
              'axes.labelsize': 'x-large',
              'axes.titlesize':'x-large',
              'xtick.labelsize':'x-large',
              'ytick.labelsize':'x-large'}
    pylab.rcParams.update(params)

    plt.rcParams.update({'font.size': 22})

    plot1 = plt.figure(1)

    test_cap_vs_zenith = cap_vs_zenith_at_target_altitude(test_altitude)

    label = "Altitude = "+str(test_altitude)+"km"

    plt.plot(zenith_angle, test_cap_vs_zenith, label=label)

    for e in range(len(test_cap_vs_zenith)):

        test_cap_vs_zenith[e] = round(test_cap_vs_zenith[e],2)

    for a,b in zip(zenith_angle, test_cap_vs_zenith):
        plt.text(a, b, str(b))

    #print(test_cap_vs_zenith[0] - test_cap_vs_zenith[-1])

plt.grid()

plt.xticks(np.arange(min(zenith_angle), max(zenith_angle)+70,30))

plt.legend()

plt.show()
'''

# In[23]:

def find_channel_cap_vs_dsitance_at_specific_altitude(altitude,                                                      low_altitude_channel_model= jet_channel,                                                      high_altitude_channel_model = UAV_channel):

    bottom_cureve = low_altitude_channel_model.capacity

    upper_cureve  = high_altitude_channel_model.capacity

    start_altitude = low_altitude_channel_model.altitude

    end_altitude = high_altitude_channel_model.altitude

    alt_dif = end_altitude - start_altitude

    cureve_at_traget_altitude = []

    for i in range(len(bottom_cureve)):

        bottom_cap = bottom_cureve[i]

        upper_cap = upper_cureve[i]

        slop = (upper_cap - bottom_cap)/alt_dif

        estimate_cap = bottom_cap + slop*(altitude - start_altitude)

        cureve_at_traget_altitude.append(estimate_cap)

    return cureve_at_traget_altitude

# In[24]:

#test_curve = (find_channel_cap_vs_dsitance_at_specific_altitude(13))

#plot_channel_capacity(13 , transmission_distance,test_curve)

# In[25]:

# given X,Y data points, try to find y value when given a x

def estimate_value_by_cureve_slop(x_vals,y_vals,target_x):

    if(target_x<x_vals[0]):

        slop = (y_vals[1] - y_vals[0])/(x_vals[1] - x_vals[0])

        result = y_vals[0]-slop*(x_vals[0]-target_x)

        return result

    elif(target_x>x_vals[-1]):

        slop = (y_vals[-1] - y_vals[-2])/(x_vals[-1] - x_vals[-2])

        result = y_vals[-1]+slop*(target_x-x_vals[-1])

        return result

    i=0

    while(x_vals[i]<target_x):

        i+=1

    i-=1

    x1 = x_vals[i]
    x2 = x_vals[i+1]
    y1 = y_vals[i]
    y2 = y_vals[i+1]

    slop = (y2-y1)/(x2-x1)

    result = y1+slop*(target_x-x1)

    return result

# In[26]:

#test = estimate_value_by_cureve_slop(x_vals = transmission_distance,y_vals = test_curve,target_x = 11)

#print(test)

# In[27]:

# intput: node1/2 psoition = [x,y,z] or [x,y], the z will be set =10 for default
#output: channel capacitance in unit Tbps

def find_channel_capacity(node1_position,node2_position,                          low_altitude_channel_model= jet_channel,                          high_altitude_channel_model = UAV_channel):

    global zenith_angle

    global transmission_distance

    # set up default altitude if altitude information are not given
    default_altitude = 10

    if(len(node1_position) <3):
        node1_position.append(default_altitude)

    if(len(node2_position) <3):
        node2_position.append(default_altitude)

    # node 2 altitude must > node 1

    if(node2_position[2]<node1_position[2]):
        return find_channel_capacity(node2_position,node1_position,                                     low_altitude_channel_model,                                     high_altitude_channel_model)

    x_dif = node2_position[0] - node1_position[0]

    y_dif = node2_position[1] - node1_position[1]

    z_dif = node2_position[2] - node1_position[2]

    distance = (x_dif**2 + y_dif**2 + z_dif**2)**(1/2)

    #xy_plan_d = (x_dif**2 + y_dif**2)**(1/2)

    if(distance ==0):
        zenith_angle_between_nodes = 90
    else:
        zenith_angle_between_nodes = math.acos(z_dif/distance)/math.pi*180

    #print('distance between nodes: ',distance)
    #print('zenith angle between nodes: ',zenith_angle_between_nodes,'degree')

    #find lower altitude channel modle
    base_model_altitude = node1_position[2]

    lower_altitude_base_capacity_vs_distance_model = find_channel_cap_vs_dsitance_at_specific_altitude(base_model_altitude)

    base_capacitance = estimate_value_by_cureve_slop(x_vals = transmission_distance,y_vals = lower_altitude_base_capacity_vs_distance_model,target_x = distance)

    base_capacitance =max(base_capacitance,0)

    #print('without zenith angle, the capacitance is ',base_capacitance)

    # due to zenith angle, the acutal cureve will be shifted(up wards)
    cap_vs_zenith = cap_vs_zenith_at_target_altitude(base_model_altitude)

    zero_angle_val = estimate_value_by_cureve_slop(x_vals = zenith_angle,y_vals = cap_vs_zenith,target_x = 90)

    with_angle_val = estimate_value_by_cureve_slop(x_vals = zenith_angle,y_vals = cap_vs_zenith,target_x = zenith_angle_between_nodes)

    #print('when d=1km, without zenith, cap = ',zero_angle_val)
    #print('when d=1km, with zenith,  cap = ',with_angle_val)

    percentage_change = (with_angle_val-zero_angle_val)/zero_angle_val

    final_result = (1+percentage_change)*base_capacitance

    return final_result

# In[28]:

'''
node1_position = [0,0,16] #[0, 0, 0+10]

node2_position = [0,0,16] #[10, 1,3**(1/2)+10]

capacitance = find_channel_capacity(node1_position,node2_position)

print('channel capacitance is ',capacitance,'Tbps')
'''

# In[ ]:

# In[ ]:

# In[ ]:


from pathlib import Path
import shutil
import os
from tqdm import tqdm

def prepare_images_by_age( path_dataset="../datasets/ffhq_mini/images/", age_min=20, age_max=70):
    dataset_name = "age_"+str(age_min)+"_to_"+str(age_max)
    print(f'[INFO] Dataset name: {dataset_name}.')

    new_path = Path(path_dataset) / dataset_name
    try :
        new_path.mkdir(parents=True, exist_ok=False)
    except FileExistsError:
        print(f'[INFO] Dataset already created.')
    else:
        print(f'[INFO] Dataset is being created...')

    for filename in tqdm(os.listdir(path_dataset)):

        if filename.split('_')[0] != 'age':

            in_age_min = int(filename.split('_')[0]) >= age_min
            in_age_max = int(filename.split('_')[0]) <= age_max

            if  in_age_min and in_age_max :
                shutil.move(path_dataset+"/"+filename, str(new_path)+"/"+filename)

if __name__ == "__main__":
    prepare_images_by_age()


from flask import Flask, render_template, request
import pandas as pd
import numpy as np
import joblib

housing_df = pd.read_excel('C://Users//anike//Zep Analytics//Real Estate Analysis//MagicBricks Cleaned (Excel).xlsx')
cities_list = list(np.sort(housing_df['City'].unique()))
states_list = list(np.sort(housing_df['State/Territory'].unique()))
tiers_list = list(np.sort(housing_df['Tier'].unique()))
transaction_list = list(np.sort(housing_df['Transaction'].unique()))
furnishing_list = list(np.sort(housing_df['Furnishing'].unique()))
status_list = list(np.sort(housing_df['Status'].unique()))

app = Flask(__name__)

@app.route('/')
def home_page():
    prediction = "₹ 0.00"
    return render_template("index.html",
                           price=prediction,
                           cities=cities_list,
                           states=states_list,
                           tiers=tiers_list,
                           trans=transaction_list,
                           furnish=furnishing_list,
                           status=status_list)

@app.route('/predict', methods=["GET", "POST"])
def predict():
    if request.method == 'POST':
        input_df = pd.DataFrame(index=[0])
        input_df['Bedrooms'] = int(request.form.get('Bedrooms'))
        input_df['Bathroom'] = int(request.form.get('Bathrooms'))
        input_df['Area(sqft)'] = float(request.form.get('Area'))
        input_df['Latitude'] = float(request.form.get('Latitude'))
        input_df['Longitude'] = float(request.form.get('Longitude'))
        input_df['Price per sqft'] = float(request.form.get('Price per sqft'))
        for t in tiers_list:
            if t == request.form.get('Tier'):
                input_df['Tier_' + t] = 1
            else:
                input_df['Tier_' + t] = 0
        for s in states_list:
            if s == request.form.get('State/Territory'):
                input_df['State/Territory_' + s] = 1
            else:
                input_df['State/Territory_' + s] = 0
        for t in transaction_list:
            if t == request.form.get('Transaction'):
                input_df['Transaction_' + t] = 1
            else:
                input_df['Transaction_' + t] = 0
        for f in furnishing_list:
            if f == request.form.get('Furnishing'):
                input_df['Furnishing_' + f] = 1
            else:
                input_df['Furnishing_' + f] = 0
        for s in status_list:
            if s == request.form.get('Status'):
                input_df['Status_' + s] = 1
            else:
                input_df['Status_' + s] = 0

        #input_df.to_csv('Input_df.csv', index=False)
        model = joblib.load('GradientBoosting (Price Predictor).joblib')
        pred = np.round(model.predict(input_df)[0], 2)
        if pred > 100:
            prediction = '₹ ' + str(round(pred / 100, 2)) + ' Cr'
        else:
            prediction = '₹ ' + str(pred) + ' Lacs'

    return render_template("index.html",
                           price=prediction,
                           cities=cities_list,
                           states=states_list,
                           tiers=tiers_list,
                           trans=transaction_list,
                           furnish=furnishing_list,
                           status=status_list)

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


import airshare
import requests
import socket
import webbrowser

SERVER_ADDR = 'http://172.30.1.48:32456'

CODE = 'brad-macbook-pro'
PORT = 23456

local_ip_addr = socket.inet_ntoa(airshare.utils.get_local_ip_address())

data = {
    'code': CODE,
    'ip_addr': local_ip_addr,
    'port': PORT,
}

r = requests.post('%s/register' % (SERVER_ADDR,), data)

print(r.json())

try:
    webbrowser.open('%s/browse' % SERVER_ADDR)
except:
    print('%s/browse' % SERVER_ADDR)

airshare.receiver.receive_server(code=CODE, port=PORT)


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

def get_image_info(image):
	print(type(image))
	print(image.shape)
	print(image.size)
	print(image.dtype)
	print(np.array(image))

img2 = cv.imread('Rick and Morty.jpg')
img3 = cv.cvtColor(img2,cv.COLOR_BGR2RGB)
img = cv.imread('Rick and Morty.jpg',0)

cv.namedWindow("Rick and Morty", cv.WINDOW_NORMAL)
cv.imshow("Rick and Morty",img)

get_image_info(img)

k = cv.waitKey(0)
if k == ord("s"):
	cv.imwrite('Rick and Morty GRAY.jpg',img)
if k == 27:
	cv.destroyAllWindows()

plt.subplot(311), plt.imshow(img,cmap= "gray")
plt.title('GRAY Image'), plt.xticks([]),plt.yticks([])
plt.subplot(312), plt.imshow(img2)
plt.title('BGR Image'),plt.xticks([]),plt.yticks([])
plt.subplot(313), plt.imshow(img3)
plt.title('RGB Image'),plt.xticks([]),plt.yticks([])
plt.show()


from fastapi import APIRouter

from app.api import RouterTags

router = APIRouter(
    tags = [RouterTags.File],
    responses = { 404: { "detail": "not found" } }
)

####################################
# file serve request
# /home/foo/var.txt -> /files//home/foo/var.txt
####################################
@router.get("/files/{filepath:path}")
async def serve_file(filepath: str):
    return { filepath: filepath }


# do not forget to run the coupon project !!
import requests # need to install in settings
import json
import pytest

class RestObject:
    def __init__(self, d):
        self.__dict__ = d
    def __str__(self):
        result = ""
        for k, v in self.__dict__.items():
            result += k
            result += " : "
            result += str(v)
            result += '\n'
        return result

def test_rest_api_get_todos():
    resp = requests.get("https://jsonplaceholder.typicode.com/todos/1")
    d = json.loads(resp.content)
    t = RestObject(d)
    assert t.id == 1
    assert t.userId == 1
    assert t.title == 'delectus aut autem'
    assert t.completed == False

def test_rest_api_get_coupon():
    resp = requests.get("http://localhost:8080/coupon/1")
    d = json.loads(resp.content)
    t = RestObject(d)
    assert t.id == 1
    assert t.title == 'Caffe'
    # [{"title":"Caffe","id":1},{"title":"Movie VIP","id":2},{"title":"Sky jump","id":3}]


# !/usr/bin/env python
# -*-coding:utf-8-*-
from sqlite import sqlite_get_first, sqlite_first, sqlite_update
from web3 import Web3
from web3.middleware import geth_poa_middleware
from config import config
import sys
import os
from datetime import datetime

network = sys.argv[1]
w3 = Web3(Web3.WebsocketProvider(config('NODE', network + config('NODE', 'CHAIN')).format(config('MORALIS', "FIND_BLOCK")), websocket_timeout=900, websocket_kwargs={'max_size': 999999999}))
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
while True:
    try:
        latestBlock = w3.eth.getBlock('latest').number
        checkLastBlock = sqlite_first("bots", "WHERE network='{0}' AND status=1 ORDER BY block DESC".format(network))
        if checkLastBlock is None:
            blockNum = latestBlock
        else:
            blockNum = int(checkLastBlock['block']) + 1
        if latestBlock >= blockNum:
            checkFreeBot = sqlite_get_first("bots", "block", None)
            if checkFreeBot is not None:
                sqlite_update("bots", "block = {0}, network = '{1}', updated_at='{2}'".format(blockNum, network, datetime.now().strftime("%d/%m/%Y %H:%M:%S")), "bot", checkFreeBot['bot'])
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        print("error----------", e, exc_type, fname, exc_tb.tb_lineno)


import collections
import json
from unittest import mock

import pytest
from py_zipkin.storage import get_default_tracer
from py_zipkin.storage import Stack

from pyramid_zipkin import tween
from tests.acceptance.test_helper import MockTransport

DummyRequestContext = collections.namedtuple(
    'RequestContext',
    ['zipkin_context'],
)

@pytest.mark.parametrize('is_tracing', [True, False])
@mock.patch.object(get_default_tracer(), 'zipkin_span', autospec=True)
def test_zipkin_tween_sampling(
    mock_span,
    dummy_request,
    dummy_response,
    is_tracing,
):
    """
    We should enter py_zipkin context manager and
    generate a trace id regardless of whether we are sampling
    """
    dummy_request.registry.settings = {
        'zipkin.is_tracing': lambda _: is_tracing,
        'zipkin.transport_handler': MockTransport(),
    }
    handler = mock.Mock()
    handler.return_value = dummy_response

    assert tween.zipkin_tween(handler, None)(dummy_request) == dummy_response
    assert handler.call_count == 1
    assert mock_span.call_count == 1

@pytest.mark.parametrize(['set_callback', 'called'], [(False, 0), (True, 1)])
@pytest.mark.parametrize('is_tracing', [True, False])
@mock.patch.object(get_default_tracer(), 'zipkin_span', autospec=True)
def test_zipkin_tween_post_handler_hook(
    mock_span,
    dummy_request,
    dummy_response,
    is_tracing,
    set_callback,
    called,
):
    """
    We should invoke the post processor callback regardless of trace id
    or sampling
    """
    mock_post_handler_hook = mock.Mock()

    dummy_request.registry.settings = {
        'zipkin.is_tracing': lambda _: is_tracing,
        'zipkin.transport_handler': MockTransport(),
    }
    if set_callback:
        dummy_request.registry.settings['zipkin.post_handler_hook'] = \
            mock_post_handler_hook

    handler = mock.Mock()
    handler.return_value = dummy_response

    assert tween.zipkin_tween(handler, None)(dummy_request) == dummy_response
    assert handler.call_count == 1
    assert mock_span.call_count == 1
    assert mock_post_handler_hook.call_count == called
    if set_callback:
        mock_post_handler_hook.assert_called_once_with(
            dummy_request,
            dummy_response,
            mock_span.return_value.__enter__.return_value,
        )

@pytest.mark.parametrize(['set_callback', 'called'], [(False, 0), (True, 1)])
@pytest.mark.parametrize('is_tracing', [True])
def test_zipkin_tween_exception(
    get_request,
    dummy_response,
    is_tracing,
    set_callback,
    called,
):
    """
    If request processing has an exception set response_status_code to 500
    """

    mock_post_handler_hook = mock.Mock()
    transport = MockTransport()

    get_request.registry.settings = {
        'zipkin.is_tracing': lambda _: is_tracing,
        'zipkin.transport_handler': transport,
    }
    if set_callback:
        get_request.registry.settings['zipkin.post_handler_hook'] = \
            mock_post_handler_hook

    handler = mock.Mock(side_effect=Exception)

    try:
        tween.zipkin_tween(handler, dummy_response)(get_request)
        pytest.fail('exception was expected to be thrown!')
    except Exception:
        pass

    spans = transport.get_payloads()
    assert len(spans) == 1
    span = json.loads(spans[0])[0]
    span['tags']['response_status_code'] = '500'
    span['tags']['error.type'] = 'Exception'

    assert handler.call_count == 1
    assert mock_post_handler_hook.call_count == called

@pytest.mark.parametrize(['set_callback', 'called'], [(False, 0), (True, 1)])
@pytest.mark.parametrize('is_tracing', [True])
def test_zipkin_tween_no_exception(
    get_request,
    dummy_response,
    is_tracing,
    set_callback,
    called,
):
    mock_post_handler_hook = mock.Mock()
    transport = MockTransport()

    get_request.registry.settings = {
        'zipkin.is_tracing': lambda _: is_tracing,
        'zipkin.transport_handler': transport,
    }
    if set_callback:
        get_request.registry.settings['zipkin.post_handler_hook'] = \
            mock_post_handler_hook

    handler = mock.Mock()
    tween.zipkin_tween(handler, dummy_response)(get_request)

    spans = transport.get_payloads()
    assert len(spans) == 1
    span = json.loads(spans[0])[0]
    span['tags']['response_status_code'] = '200'

    assert handler.call_count == 1
    assert mock_post_handler_hook.call_count == called

def test_zipkin_tween_context_stack(
    dummy_request,
    dummy_response,
):
    old_context_stack = get_default_tracer()._context_stack
    dummy_request.registry.settings = {
        'zipkin.is_tracing': lambda _: False,
        'zipkin.transport_handler': MockTransport(),
        'zipkin.request_context': 'rctxstorage.zipkin_context',
    }

    context_stack = mock.Mock(spec=Stack)
    dummy_request.rctxstorage = DummyRequestContext(
        zipkin_context=context_stack,
    )

    handler = mock.Mock(return_value=dummy_response)
    response = tween.zipkin_tween(handler, None)(dummy_request)
    get_default_tracer()._context_stack = old_context_stack

    assert response == dummy_response

    assert context_stack.push.call_count == 1
    assert context_stack.pop.call_count == 1

def test_getattr_path():
    request_context = DummyRequestContext(zipkin_context=object())
    assert tween._getattr_path(request_context, 'zipkin_context.__class__')

    # missing attribute
    tween._getattr_path(request_context, 'zipkin_context.missing') is None

    # attribute is None
    mock_object = mock.Mock(nil=None)
    assert tween._getattr_path(mock_object, 'nil') is None

def test_logs_warning_if_using_function_as_transport(
    dummy_request,
    dummy_response,
):
    dummy_request.registry.settings = {
        'zipkin.is_tracing': lambda _: False,
        'zipkin.transport_handler': lambda x: None,
        'request_context': 'rctxstorage',
    }

    handler = mock.Mock(return_value=dummy_response)
    with pytest.deprecated_call():
        tween.zipkin_tween(handler, None)(dummy_request)


import cv2
import numpy as np
"""
OpenCV: 高斯滤波

高斯滤波：
    GaussianBlur(img, kernel, sigmaX, sigmaY...)
    sigma: 中心滤波延展的宽度，即到原点（中心点）的差距
高斯权重，越靠近锚点，权重越高，呈现正太分布
"""

img = cv2.imread(r'E:\PycharmProjects\funnyPython\opencv_py\data\imgs\boy.png')

dst = cv2.GaussianBlur(img, (5, 5), sigmaX=1,)

cv2.imshow('dst', dst)
cv2.imshow('img', img)
cv2.waitKey(0)


# https://docs.smith.langchain.com/evaluation/quickstart?ref=blog.langchain.dev

#%% CREATE A DATASET
from langsmith import Client

example_inputs = [
  "a rap battle between Atticus Finch and Cicero",
  "a rap battle between Barbie and Oppenheimer",
  "a Pythonic rap battle between two swallows: one European and one African",
  "a rap battle between Aubrey Plaza and Stephen Colbert",
]

client = Client()
dataset_name = "Rap Battle Dataset"

# Storing inputs in a dataset lets us
# run chains and LLMs over a shared set of examples.
dataset = client.create_dataset(
    dataset_name=dataset_name, description="Rap battle prompts.",
)
for input_prompt in example_inputs:
    # Each example must be unique and have inputs defined.
    # Outputs are optional
    client.create_example(
        inputs={"question": input_prompt},
        outputs=None,
        dataset_id=dataset.id,
    )

# %% DEFINE LLM

from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(temperature=0)

# %% EVALUATE LLM

from langchain.smith import RunEvalConfig, run_on_dataset

eval_config = RunEvalConfig(
  evaluators=[
    # You can specify an evaluator by name/enum.
    # In this case, the default criterion is "helpfulness"
    "criteria",
    # Or you can configure the evaluator
    RunEvalConfig.Criteria("harmfulness"),
    RunEvalConfig.Criteria("misogyny"),
    RunEvalConfig.Criteria(
      {"cliche": "Are the lyrics cliche? "
      "Respond Y if they are, N if they're entirely unique."}
      )
  ]
)
run_on_dataset(
    client=client,
    dataset_name=dataset_name,
    llm_or_chain_factory=llm,
    evaluation=eval_config,
    verbose=True,
)

# %%


from __future__ import division
import torch
import torch.nn as nn
import torch.nn.init as init
from torch.autograd import Function
from torch.autograd import Variable
from math import sqrt as sqrt
from itertools import product as product
import numpy as np
from utils.box_utils import decode, nms
from utils.config import Config

class Detect(Function):
    def __init__(self, num_classes, bkg_label, top_k, conf_thresh, nms_thresh):
        self.num_classes = num_classes
        self.background_label = bkg_label
        self.top_k = top_k
        self.nms_thresh = nms_thresh
        if nms_thresh <= 0:
            raise ValueError('nms_threshold must be non negative.')
        self.conf_thresh = conf_thresh
        self.variance = Config['variance']

    def forward(self, loc_data, conf_data, prior_data):
        loc_data = loc_data.cpu()
        conf_data = conf_data.cpu()
        num = loc_data.size(0)  # batch size
        num_priors = prior_data.size(0)
        output = torch.zeros(num, self.num_classes, self.top_k, 5)
        conf_preds = conf_data.view(num, num_priors,
                                    self.num_classes).transpose(2, 1)
        # 对每一张图片进行处理
        for i in range(num):
            # 对先验框解码获得预测框
            decoded_boxes = decode(loc_data[i], prior_data, self.variance)
            conf_scores = conf_preds[i].clone()

            for cl in range(1, self.num_classes):
                # 对每一类进行非极大抑制
                c_mask = conf_scores[cl].gt(self.conf_thresh)
                scores = conf_scores[cl][c_mask]
                if scores.size(0) == 0:
                    continue
                l_mask = c_mask.unsqueeze(1).expand_as(decoded_boxes)
                boxes = decoded_boxes[l_mask].view(-1, 4)
                # 进行非极大抑制
                ids, count = nms(boxes, scores, self.nms_thresh, self.top_k)
                output[i, cl, :count] = \
                    torch.cat((scores[ids[:count]].unsqueeze(1),
                               boxes[ids[:count]]), 1)
        flt = output.contiguous().view(num, -1, 5)
        _, idx = flt[:, :, 0].sort(1, descending=True)
        _, rank = idx.sort(1)
        flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)].fill_(0)
        return output

class PriorBox(object):
    def __init__(self, cfg):
        super(PriorBox, self).__init__()
        self.image_size = cfg['min_dim']
        self.num_priors = len(cfg['aspect_ratios'])
        self.variance = cfg['variance'] or [0.1]
        self.feature_maps = cfg['feature_maps']
        self.min_sizes = cfg['min_sizes']
        self.max_sizes = cfg['max_sizes']
        self.steps = cfg['steps']
        self.aspect_ratios = cfg['aspect_ratios']
        self.clip = cfg['clip']
        self.version = cfg['name']
        for v in self.variance:
            if v <= 0:
                raise ValueError('Variances must be greater than 0')

    def forward(self):
        mean = []
        for k, f in enumerate(self.feature_maps):
            x,y = np.meshgrid(np.arange(f),np.arange(f))
            x = x.reshape(-1)
            y = y.reshape(-1)
            for i, j in zip(y,x):
                f_k = self.image_size / self.steps[k]
                # 计算网格的中心
                cx = (j + 0.5) / f_k
                cy = (i + 0.5) / f_k

                # 求短边
                s_k = self.min_sizes[k]/self.image_size
                mean += [cx, cy, s_k, s_k]

                # 求长边
                s_k_prime = sqrt(s_k * (self.max_sizes[k]/self.image_size))
                mean += [cx, cy, s_k_prime, s_k_prime]

                # 获得长方形
                for ar in self.aspect_ratios[k]:
                    mean += [cx, cy, s_k*sqrt(ar), s_k/sqrt(ar)]
                    mean += [cx, cy, s_k/sqrt(ar), s_k*sqrt(ar)]
        # 获得所有的先验框
        output = torch.Tensor(mean).view(-1, 4)

        if self.clip:
            output.clamp_(max=1, min=0)
        return output

class L2Norm(nn.Module):
    def __init__(self,n_channels, scale):
        super(L2Norm,self).__init__()
        self.n_channels = n_channels
        self.gamma = scale or None
        self.eps = 1e-10
        self.weight = nn.Parameter(torch.Tensor(self.n_channels))
        self.reset_parameters()

    def reset_parameters(self):
        init.constant_(self.weight,self.gamma)

    def forward(self, x):
        norm = x.pow(2).sum(dim=1, keepdim=True).sqrt()+self.eps
        #x /= norm
        x = torch.div(x,norm)
        out = self.weight.unsqueeze(0).unsqueeze(2).unsqueeze(3).expand_as(x) * x
        return out


from django.shortcuts import render, redirect
from django.core.files.storage import FileSystemStorage
from django.views import View
from .excel import excel_to_db
import pandas as pd
from .models import Match
from django.core.paginator import Paginator
from django.shortcuts import render

def listing(request, matches):
    paginator = Paginator(matches, 10)
    page = request.GET.get('page')
    matches = paginator.get_page(page)
    return matches

def index(request):
    ctx = {}
    return render(request, 'index.html', ctx)

def add_match(request):
    return redirect('/admin/matches/match/add/')

def all_matches(request):
    ctx = {}
    matches = Match.objects.all()
    ctx['matches'] = listing(request, matches)
    return render(request, 'matches/all_matches.html', ctx)

def filters(request):
    ctx = {}
    if request.method == 'GET':
        ctx['matches'] = Match.objects.all()
        ctx['country'] = ''
        ctx['winner'] = ''
        return render(request, 'matches/filters.html', ctx)
    if request.method == 'POST':
        country = request.POST.get('country')
        winner = request.POST.get('winner')
        if country not in 'all':
            ctx['matches'] = Match.objects.filter(champ=country)
        if winner not in 'all':
            ctx['matches'] = Match.objects.filter(result=winner)
        if country not in 'all' and winner not in 'all':
            ctx['matches'] = Match.objects.filter(champ=country, result=winner)
        ctx['country'] = request.POST.get('country')
        ctx['winner'] = request.POST.get('winner')
        return render(request, 'matches/filters.html', ctx)

def load_excel(request):
    ctx = {}
    if request.POST.get('upload_btn') == 'Upload':
        uploaded_file = request.FILES['document']
        fs = FileSystemStorage()
        if fs.exists(uploaded_file.name):
            fs.delete(uploaded_file.name)
        fs.save(uploaded_file.name, uploaded_file)
        df = pd.read_excel(uploaded_file, sheet_name='all')
        excel_to_db(df)
    return render(request, 'matches/load_excel.html', ctx)

def delete(request):
    ctx = {}
    ctx['matches'] = Match.objects.all()
    if request.method == 'GET':
        Match.objects.all().delete()
    return render(request, 'matches/all_matches.html', ctx)


from wordcloud import WordCloud
import matplotlib.pyplot as plt
import nltk

pontos = ['?', '!', '.', ',', '&',' ', '(', ')', ':', ';']
def cloud(tt):
    text = ''
    lista_teste = []

    for words in tt:
        aux = nltk.word_tokenize(words)
        lista_teste = []
        for t in aux:
            if t not in pontos:
                text += t + " "

    wordcloud = WordCloud(background_color="black", width=1600, height=800).generate(text)
    fig, ax = plt.subplots(figsize=(10,6))
    ax.imshow(wordcloud, interpolation='bilinear')
    ax.set_axis_off()

    plt.imshow(wordcloud)
    wordcloud.to_file("wordcloud.png")


# Inspired by https://github.com/melvinkcx/graphql-utilities/blob/master/graphql_utilities/decorators.py

from functools import wraps

def one_time_middleware(resolve_func):
    """
    Make sure middleware is run only once,
    this is done by setting a flag in the `context` of `ResolverInfo`
    Example:
        class AuthenticationMiddleware:
            @one_time_middleware
            def resolve(self, next, root, info, *args, **kwargs):
                pass
    """
    @wraps(resolve_func)
    def wrapper(self, next, root, info, *args, **kwargs):
        decorator_name = "__{}_run__".format(self.__class__.__name__)

        if info.context is not None:
            if isinstance(info.context, dict) and not info.context.get(decorator_name, False):
                info.context[decorator_name] = True
                return resolve_func(self, next, root, info, *args, **kwargs)
            elif not isinstance(info.context, dict) and not getattr(info.context, decorator_name, False):
                setattr(info.context, decorator_name, True)
                return resolve_func(self, next, root, info, *args, **kwargs)

        return next(root, info, *args, **kwargs)

    return wrapper