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

import _init_paths

from tqdm import tqdm
import os
import sys
import cv2
import json
import copy
import numpy as np
import warnings

from opts import opts
from detector import Detector
from tools import tbd_utils, visualization

warnings.filterwarnings("ignore")

image_ext = ['jpg', 'jpeg', 'png', 'webp']
video_ext = ['mp4', 'mov', 'avi', 'mkv']
time_stats = ['tot', 'load', 'pre', 'net', 'dec', 'post', 'merge', 'display']

def demo(opt):
    os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpus_str
    opt.debug = max(opt.debug, 1)
    detector = Detector(opt)

    if opt.demo == 'webcam' or \
            opt.demo[opt.demo.rfind('.') + 1:].lower() in video_ext:
        is_video = True

        cam = cv2.VideoCapture(0 if opt.demo == 'webcam' else opt.demo)
    else:
        is_video = False

        if os.path.isdir(opt.demo):
            image_names = []
            ls = os.listdir(opt.demo)
            for file_name in sorted(ls):
                ext = file_name[file_name.rfind('.') + 1:].lower()
                if ext in image_ext:
                    image_names.append(os.path.join(opt.demo, file_name))
        else:
            image_names = [opt.demo]

    out = None
    out_name = opt.demo[opt.demo.rfind('/') + 1:]
    if opt.save_video:
        if not os.path.exists('../results'):
            os.mkdir('../results')

        fourcc = cv2.VideoWriter_fourcc('I', '4', '2', '0')
        save_video_dir = '../results/{}/videos/'.format(
            opt.exp_id + '_' + out_name)
        if not os.path.exists(save_video_dir):
            os.makedirs(save_video_dir, exist_ok=True)
        save_seq_path = os.path.join(save_video_dir, out_name)

        out = cv2.VideoWriter(save_seq_path+".avi", fourcc, opt.save_framerate, (
            opt.input_w, opt.input_h))

    if opt.debug < 5:
        detector.pause = False
    cnt = 0
    results = {}

    remain_frame = 30
    last_n_frames_bboxes = np.zeros(remain_frame, np.int32)

    save_video_dir = '../results/{}/records/'.format(
        opt.exp_id + '_' + out_name)
    if not os.path.exists(save_video_dir):
        os.makedirs(save_video_dir, exist_ok=True)
    results_mot_file_path = os.path.join(save_video_dir, out_name+".txt")

    while True:
        if is_video:
            _, img = cam.read()
            if img is None:
                save_and_exit(opt, out, results, out_name)
        else:
            if cnt < len(image_names):
                img = cv2.imread(image_names[cnt])
            else:

                avi_to_mp4 = "ffmpeg -i %s.avi %s.mp4" % (
                    save_seq_path, save_seq_path)
                del_avi_file = "rm %s.avi" % (save_seq_path)
                os.system(avi_to_mp4)
                os.system(del_avi_file)
                out.release()
                return

        cnt += 1

        if opt.resize_video:
            img = cv2.resize(img, (opt.input_w, opt.input_h))

        if cnt < opt.skip_first:
            continue

        if not opt.save_video:
            cv2.imshow('input', img)

        ret = detector.run(img)

        time_str = '[{}] frame {} |'.format(opt.demo.split("/")[-1], cnt)
        for stat in time_stats:
            time_str = time_str + '{} {:.3f}s |'.format(stat, ret[stat])
        print(time_str)

        results[cnt] = ret['results']

        save_img_dir = "../results/%s/imgs/" % (opt.exp_id + '_' + out_name)
        if not os.path.exists(save_img_dir):
            os.makedirs(save_img_dir, exist_ok=True)
        if opt.save_video:

            trackers = []
            for tracklet in results[cnt]:
                trackers.append(list(tracklet['bbox']) +
                                list([tracklet['score']]) +
                                list([tracklet['tracking_id']]))

            frame_id = cnt-1
            tbd_utils.SaveRecords(trackers, frame_id, results_mot_file_path)

            last_n_frames_bboxes[frame_id % remain_frame] = len(trackers)
            n_lines = last_n_frames_bboxes.sum()
            img = visualization.DrawTrackers(img, trackers, frame_id,
                                             n_lines, results_mot_file_path)

            out.write(img)

            if not is_video:
                cv2.imwrite(save_img_dir+'{}.jpg'.format(cnt), img)

        if cv2.waitKey(1) == 27:
            save_and_exit(opt, out, results, out_name)
            return
    save_and_exit(opt, out, results)

def save_and_exit(opt, out=None, results=None, out_name=''):
    if opt.save_results and (results is not None):
        save_dir = '../results/{}_results.json'.format(
            opt.exp_id + '_' + out_name)
        print('saving results to', save_dir)
        json.dump(_to_list(copy.deepcopy(results)),
                  open(save_dir, 'w'))
    if opt.save_video and out is not None:
        out.release()
    sys.exit(0)

def _to_list(results):
    for img_id in results:
        for t in range(len(results[img_id])):
            for k in results[img_id][t]:
                if isinstance(results[img_id][t][k], (np.ndarray, np.float32)):
                    results[img_id][t][k] = results[img_id][t][k].tolist()
    return results

if __name__ == '__main__':
    data_root = "/home/caoxiaoyan/MOT_benchmark/BEE20_v2"
    seq_list = os.listdir(data_root)
    bar = tqdm(seq_list)
    for seq_name in bar:
        bar.set_description("Processing:%s" % seq_name)
        if "beev2" in seq_name:
            opt = opts().init()
            opt.demo = os.path.join(data_root, seq_name)
            demo(opt)


import ndex.networkn as networkn
import re
import cns_model as cm

# def make_transform():
#     transform = {}
#     for old, new in predicate_translation.iteritems():
#         transform[unicode(old)] = unicode(new)
#     return transform

# def transform_predicate(predicate, transform):
#     if not predicate:
#         return "interactsWith"
#     for old, new in transform.iteritems():
#         if predicate == old:
#             return new
#     return predicate

wildtypesuffix = re.compile('_wt$', re.IGNORECASE)

def normalize_name(name):
    return wildtypesuffix.sub("", name)

def normalize(from_network):
    ont = cm.predicateOntology(cm.predicates)
    to_network = networkn.NdexGraph()
    to_network.set_name(from_network.get_name())
    node_id_map = {}
    for from_node_id, from_node_attr in from_network.nodes_iter(data=True):
        from_node_attr['name'] = normalize_name(from_node_attr['name'])
        to_node_id = to_network.add_new_node(from_node_attr['name'], from_node_attr)
        node_id_map[from_node_id] = to_node_id

    for from_source_id, from_target_id, source_edge_attr in from_network.edges_iter(data=True):
        to_source_id = node_id_map[from_source_id]
        to_target_id = node_id_map[from_target_id]
        from_predicate = source_edge_attr.get("interaction")
        to_predicate = from_predicate
        to_predicate_object = ont.translate_name(from_predicate)
        to_edge_attr = source_edge_attr.copy()
        if to_predicate_object:
            to_predicate = to_predicate_object.get_name()
            to_edge_attr['directed'] = to_predicate_object.directed
            to_edge_attr['contact'] = to_predicate_object.contact
            to_edge_attr['negative'] = to_predicate_object.negative

        to_network.add_edge_between(to_source_id,
                                    to_target_id,
                                    to_predicate,
                                    to_edge_attr)
    return to_network


import random
import os
import colorama
from colorama import Fore, Back, Style

colorama.init(autoreset=True)

def clear_terminal():
    """
    Clear terminal function
    """
    os.system('cls' if os.name == 'nt' else 'clear')

def get_valid_name():
    """
    Welcome message and input player name and store it
    """
    print('***  Welcome to BlackJack ***')
    while True:
        name = input(
            Fore.GREEN
            + Style.BRIGHT
            + "Please enter your name (min 3 characters no spaces): ")
        if name.isalpha() and len(name) >= 3:
            return name
        print(
            Fore.RED
            + Style.BRIGHT
            + "Invalid input. Name must contain at least 3 letters.")

def main():
    """
    Menu options to view instructions or start the game
    """
    while True:
        print("Please choose an option:")
        print("1. View Instructions")
        print("2. Start Game")

        choice = input("Enter the number of your choice: ")

        if choice == "1":
            clear_terminal()
            print("Instructions: \n")
            print("1. The player and dealer are both dealt two cards")
            print("2. The player will get both cards face up")
            print("3. Dealer will have one card hidden")
            print("4. The goal of the game is to get as close"
                  "to 21 without going over")
            print("5. Player will be given the option to Hit"
                  "or Stand during your turn")
            print("6. Once you are satisfied with your score"
                  "and stand then its the dealers turn")
            print("7. The dealer must hit and draw a card on"
                  "16 but stand on 17")
            print("8. If the dealer goes over 21 he loses and you win")
            print("9. If both yourself and the dealer do not"
                  "exceed 21 then the closest to 21 wins")
            print("10. Game can end in a tie if both scores are the same")
            print("11. Scores will be reported at the end of the game \n")

        elif choice == "2":
            clear_terminal()
            play_blackjack()
            break
        else:
            print(
                Fore.RED
                + Style.BRIGHT
                + "Invalid choice. Please enter a valid option (1 or 2).")

def get_card_value(card):
    """
    Apply value to cards
    """
    if card in ['J', 'Q', 'K']:
        return 10
    elif card == 'A':
        return 11
    else:
        return int(card)

def get_hand_value(hand):
    """
    Apply hand value
    """
    value = sum(get_card_value(card) for card in hand)
    num_aces = hand.count('A')
    while value > 21 and num_aces > 0:
        value -= 10
        num_aces -= 1
    return value

def print_hand(hand):
    print(" ".join(hand), "(Value:", get_hand_value(hand), ")")

def play_blackjack():
    """
    Black jack game wins losses list set to 0
    """
    wins = 0
    losses = 0

    while True:
        # Shuffle of deck prior to dealing

        deck = ['2', '3', '4', '5', '6', '7', '8', '9', '10',
                'J', 'Q', 'K', 'A'] * 4
        random.shuffle(deck)

        # Dealing of 2 cards to player and dealer

        player_hand = []
        dealer_hand = []
        player_hand.append(deck.pop())
        dealer_hand.append(deck.pop())
        player_hand.append(deck.pop())
        dealer_hand.append(deck.pop())

        # Final round once player has decided to stand or hit then
        # the remaining dealer card is shown and scores can then
        # be calculated
        print(Fore.GREEN + Style.BRIGHT + f"{username}'s hand:")
        print_hand(player_hand)
        print(
            Fore.MAGENTA
            + Style.BRIGHT
            + "Dealer's hand:")
        print(dealer_hand[0], "X")
        while get_hand_value(player_hand) < 21:
            action = input("Do you want to hit (h) or stand (s)? ").lower()
            if action in ['h', 'hit']:
                player_hand.append(deck.pop())
                print(Fore.GREEN + Style.BRIGHT + f"{username}'s hand:")
                print_hand(player_hand)
            elif action in ['s', 'stand']:
                print(Fore.GREEN + Style.BRIGHT + f"{username} stands.")
                break
            else:
                print(
                    Fore.RED
                    + Style.BRIGHT
                    + "Invalid input. Please enter 'h' or 's'.")

        # If player hand greater than 17 then dealer takes another card
        player_value = get_hand_value(player_hand)
        while get_hand_value(dealer_hand) < 17:
            dealer_hand.append(deck.pop())

        print(Fore.MAGENTA + Style.BRIGHT + "\nDealer's hand:")
        print_hand(dealer_hand)
        # Calculation to determine winner of game
        dealer_value = get_hand_value(dealer_hand)
        if player_value == 21:
            print(
                Fore.GREEN
                + Style.BRIGHT
                + "***Blackjack*** Congratulations, you win!")
            wins += 1
        elif player_value > 21 or (dealer_value <= 21 and
                                   dealer_value > player_value):
            print(Fore.RED + Style.BRIGHT + "Sorry, you lose!")
            losses += 1
        elif dealer_value > 21 or player_value > dealer_value:
            print(Fore.GREEN + Style.BRIGHT + "Congratulations, you win!")
            wins += 1
        else:
            print("It's a tie!")
        # Play Again Request
        play_again = input('Do you want to play again (y/n)? ').lower()
        if play_again != 'y':
            clear_terminal()
            print(
                Fore.YELLOW
                + Style.BRIGHT
                + f"{username}, you won {wins} games and lost {losses} games.")
            print(
                Fore.CYAN
                + Style.BRIGHT
                + f'Thank you for playing {username}')
            break
        else:
            clear_terminal()

if __name__ == "__main__":
    username = get_valid_name()
    print(f"Welcome to your game of BlackJack {username}!")
    main()


# -*- coding: utf-8 -*-
"""
Created on Fri Apr 26 00:15:36 2019

@author: Manyue
"""

import numpy as np
import sys
from gym.envs.toy_text import discrete

UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3

GOAL = 4
START = 20
SNAKE1 = 7
SNAKE2 = 17

eps = 0.25

class Robot_vs_snakes_world(discrete.DiscreteEnv):
    def __init__(self):
        self.shape = [5,5]
        nS = np.prod(self.shape)
        nA = 4
        MAX_Y = self.shape[0]
        MAX_X = self.shape[1]

        P = {}
        grid = np.arange(nS).reshape(self.shape)
        it = np.nditer(grid, flags = ['multi_index'])

        while not it.finished:
            s = it.iterindex
            y, x = it.multi_index
            P[s] = {a : [] for a in range(nA)}
            is_done = lambda s: s == GOAL

            if is_done(s):
                reward = 0.0
            elif s == SNAKE1 or s ==SNAKE2:
                reward = -15.0
            else:
                reward = -1.0

            if is_done(s):
                P[s][UP] = [(1.0, s, reward, True)]
                P[s][RIGHT] = [(1.0, s, reward, True)]
                P[s][DOWN] = [(1.0, s, reward, True)]
                P[s][LEFT] = [(1.0, s, reward, True)]
            else:
                ns_up = s if y==0 else s-MAX_X
                ns_right = s if x==(MAX_X - 1) else s+1
                ns_down = s if y==(MAX_Y - 1) else s+MAX_X
                ns_left = s if x==0 else s-1

                P[s][UP] = [(1 - (2*eps), ns_up, reward, is_done(ns_up)),
                 (eps, ns_right, reward, is_done(ns_right)),
                 (eps, ns_left, reward, is_done(ns_left))]

                P[s][RIGHT] = [(1 - (2*eps), ns_right, reward, is_done(ns_right)),
                 (eps, ns_up, reward, is_done(ns_up)),
                 (eps, ns_down, reward, is_done(ns_down))]

                P[s][DOWN] = [(1 - (2*eps), ns_down, reward, is_done(ns_down)),
                 (eps, ns_right, reward, is_done(ns_right)),
                 (eps, ns_left, reward, is_done(ns_left))]

                P[s][LEFT] = [(1 - (2*eps), ns_left, reward, is_done(ns_left)),
                 (eps, ns_up, reward, is_done(ns_up)),
                 (eps, ns_down, reward, is_done(ns_down))]

            it.iternext()
        isd = np.zeros(nS)
        isd[START] = 1.0

        self.P = P
        super(Robot_vs_snakes_world, self).__init__(nS, nA, P, isd)

    def _render(self):
        grid = np.arange(self.nS).reshape(self.shape)
        it = np.nditer(grid, flags = ['multi_index'])
        while not it.finished:
            s = it.iterindex
            y, x = it.multi_index

            if self.s == s:
                output = " R "
            elif s == GOAL:
                output = " G "
            elif s == SNAKE1 or s == SNAKE2:
                output = " S "
            else:
                output = " o "

            if x == 0:
                output = output.lstrip()
            if x == self.shape[1] - 1:
                output = output.rstrip()

            sys.stdout.write(output)

            if x == self.shape[1] - 1:
                sys.stdout.write("\n")

            it.iternext()

        sys.stdout.write("\n")

#env = Robot_vs_snakes_world()
#env.senv.step(DIR)
#env.p[state][action]

#This function is to return the value function, as well as the greedy policy function. Print out
#both the policy and the value function.
def value_iteration(env):

    V = np.zeros(env.nS)
    #..
    theta = 0.0001
    gama = 1
    def oneStep(state, V):
        Q_sa = np.zeros(env.nA)
        for i in range(env.nA):
            for next_S in env.P[state][i]:
                p, s_, r, _ = next_S
                Q_sa[i] += (p*(r+gama *V[s_]))
        return Q_sa

    while True:
        dta = 0
        for a in range(env.nS):
            MY = oneStep(a, V)
            bactval = np.max(MY)
            dta = max(dta,np.abs(bactval-V[a]))
            V[a] = bactval
        if dta < theta:
            break

    policy = np.zeros([env.nS, env.nA])
    for a in range(env.nS):
        MY = oneStep(a,V)
        bact = np.argmax(MY)
        policy[a,bact] = 1.0

    return policy, V

env = Robot_vs_snakes_world()
while env.s != GOAL:
    policy, V = value_iteration(env)
    env.step(np.argmax(policy[env.s]))
    env._render()


# Copyright [2022] Eduardo Zanetta

# A função deve receber um numero N >= 0 (deve validar o input para a função),
# e retornar o valor correspondente desse número na sequencia fibonnaci.
# EX. fib(0) = 0; fib(1) = 1; fib(2) = 1; fib(3) = 2; fib(5) = 5; fib(6) = 8.

# Criar uma função LINEAR que resolva fibonacci

"""
primeiros 10:

0 1 1 2 3 5 8 13 21 34  -> fib(n)
^ ^ ^ ^ ^ ^ ^ ^  ^  ^
0 1 2 3 4 5 6 7  8  9   -> n
"""
# lógica de fibonacci -> f(n) = f(n-1) + f(n-2)

while True:  # validação

	n = int(input("N: "))  # pega input

	if n >= 0:  # número correto
		break;  # sai do loop

	else:  # número incorreto
		print("N deve ser maior ou igual a 0.")  # mensagem de erro + repete loop

fib_arr = [0, 1]  # primeiros números da sequência (tratados separadamente)

if n < 2:
	print(f"fib({n}) = {n}")  # caso seja digitado 0 ou 1

else:
	for i in range(2, n + 1):  # começa em 2 pois envolve operações com i - 2, evitando o erro de "list index out of range"
		fib_arr.append((fib_arr[i - 1]) + (fib_arr[i - 2]))  # adiciona próximo número da sequência na lista

	print(f"fib({n}) = {fib_arr[n]}")  # output

	# print(fib_arr)  # debug


#
# usage: python k36.py {file name}
#

import sys
import collections
from k30 import load_mecab

def frequency_ranking(data):
    cd = collections.Counter([m['base'] for s in data for m in s])
    return [[k, v] for k,v in cd.most_common()]

if __name__ == '__main__':
    fn = sys.argv[1]
    data = load_mecab(fn)
    for v in frequency_ranking(data):
        print(v[0], v[1])


import random
from collections import deque

from PIL import Image

def printGrid(grid, n: int, m: int):
    img = Image.new('1', (n, m))
    pixels = img.load()
    for i in range(img.size[0]):
        for j in range(img.size[1]):
            pixels[i, j] = grid[i][j]
    img.save('maze.png')

def makeGrid(width, height):
    newgrid = [[0 for x in range(height)] for y in range(width)]
    for i in range(len(newgrid)):
        for j in range(len(newgrid[i])):
            if i == 0 or j == 0 or i == len(newgrid) - 1 or j == len(newgrid[0]) - 1:
                newgrid[i][j] = 1
    return newgrid

def populateGrid(grid, chance):
    for i in range(len(grid)):  # reminder to test with: for index, value in enumerate(grid)
        for j in range(len(grid[0])):
            if (random.randint(0, 100) <= chance):  # test with list comprehension instead??
                grid[i][j] = 1
    return grid

def automataIteration(grid, minCount, makePillars):
    new_grid = [row[:] for row in grid]
    for i in range(1, len(grid) - 1):
        for j in range(1, len(grid[0]) - 1):
            count = 0
            for k in range(-1, 2):
                for l in range(-1, 2):
                    if grid[i + k][j + l] == 1:
                        count += 1
            if count >= minCount or (count == 0 and makePillars == 1):
                new_grid[i][j] = 1
            else:
                new_grid[i][j] = 0
    return new_grid

def floodFindEmpty(grid, tries, goal):
    times_remade = 0
    percentage = 0

    while times_remade < tries and percentage < goal:
        copy_grid = [row[:] for row in grid]
        open_count = 0
        times_remade += 1
        unvisited = deque([])
        new_grid = [[1 for x in range(len(grid[0]))] for y in range(len(grid))]
        # find a random empty space, hope it's the biggest cave
        randx = random.randint(0, len(grid) - 1)
        randy = random.randint(0, len(grid[0]) - 1)
        while (grid[randx][randy] == 1):
            randx = random.randint(0, len(grid) - 1)
            randy = random.randint(0, len(grid[0]) - 1)
        unvisited.append([randx, randy])
        while len(unvisited) > 0:
            current = unvisited.popleft()
            new_grid[current[0]][current[1]] = 0
            for k in range(-1, 2):
                for l in range(-1, 2):
                    if current[0] + k >= 0 and current[0] + k < len(grid) and current[1] + l >= 0 and current[
                        1] + l < len(grid[0]):  # if we're not out of bounds
                        if copy_grid[current[0] + k][current[1] + l] == 0:  # if it's an empty space
                            copy_grid[current[0] + k][current[1] + l] = 2  # mark visited
                            open_count += 1
                            unvisited.append([current[0] + k, current[1] + l])
        percentage = open_count * 100 / (len(grid) * len(grid[0]))

    return new_grid, percentage

def main():
    #width = int(input("Enter the width: "))
    #height = int(input("Enter the height: "))
    # chance = 100 - int(input("Enter the percentage chance of randomly generating a wall: "))
    # count = int(input("Enter the min count of surrounding walls for the automata rules: "))
    width = 100
    height = 100
    iterations = 2
    chance = 40
    count = 5
    #iterations = int(input("Enter the number of regular iterations: "))
    #pillarIterations = int(input("Enter the number of pillar-generating iterations: "))
    floodTries = 5
    goalPercentage = 30  # above 30% seems to be a good target

    grid = makeGrid(width, height)

    grid = populateGrid(grid, chance)

    for i in range(iterations):
        grid = automataIteration(grid, count, 0)

    grid, percentage = floodFindEmpty(grid, floodTries, goalPercentage)

    # self reminder to try checking map size https://stackoverflow.com/questions/1331471/in-memory-size-of-a-python-structure

    print("")
    printGrid(grid, width, height)

def generate(width: int, height: int, iterations: int):

    chance = 40
    count = 5
    floodTries = 5
    goalPercentage = 30  # above 30% seems to be a good target

    grid = makeGrid(width, height)

    grid = populateGrid(grid, chance)

    for i in range(iterations):
        grid = automataIteration(grid, count, 0)

    grid, percentage = floodFindEmpty(grid, floodTries, goalPercentage)

    printGrid(grid, width, height)

if __name__ == "__main__":
    main()


import logging
import os
import subprocess
from typing import Dict, Sequence

from .color import Color
from .utils import colorful_print
from .utils import read_config

logger = logging.getLogger(__name__)

class Task:
    short_name: str
    full_name: str
    cmd: Sequence[str] = []
    tasks: Sequence['Task'] = []

class Runner:
    tasks: Dict[str, Task] = {}

    def __init__(self, config_file):
        self.__config_file = config_file
        self.__config_dir = os.path.dirname(config_file)
        self.__config = read_config(config_file)
        logger.debug('config: %s', self.__config)
        self.__config.setdefault('name', '')

        self.__flatten(self.__config, '')
        logger.debug('tasks: %s', self.tasks)

    def run(self, name: str = '') -> int:
        task = self.tasks.get(name) or \
               next((t for t in self.tasks.values() if name == t.short_name), None)

        if not task:
            available = {*self.tasks.keys()}
            raise Exception(f'Cannot find such a task: {name}, available: {available}')

        return self.__run_task(task)

    def __run_task(self, task: Task) -> int:
        name = task.short_name
        colorful_print('start to exec task(' + name + '):', Color.BLUE)

        if not task.tasks:
            cmd = task.cmd
            res = subprocess.run(
                cmd,
                shell=True,
                cwd=self.__config_dir
            )

            if res.returncode:
                colorful_print('some error occurred when execute task(' + name + ')', Color.RED)
            else:
                colorful_print('task(' + name + ') was executed successfully', Color.GREEN)

            return res.returncode
        else:
            for t in task.tasks:
                res = self.__run_task(t)
                if res:
                    return res

    def __flatten(self, task_obj, namespace: str):
        task = Task()
        task.short_name = task_obj['name']
        task.full_name = namespace + task_obj['name']
        task.cmd = task_obj.get('cmd', [])
        self.tasks[task.full_name] = task

        sub_task_obj = task_obj.get('tasks', [])
        if not sub_task_obj:
            return task

        next_ns = task.full_name + '.' if task.full_name else ''
        task.tasks = [self.__flatten(t, next_ns) for t in sub_task_obj]

        return task


'''kruskal's by fast union find for weighted undirected graphs
union by rank - O(logn), find - Path Compression - O(logn)  <two techniques complement each other>
'''
class Graph:
    def __init__(self,num_v):
        self.num_of_vertices = num_v
        self.edges = []
    def addEdge(self,edge):
        self.edges.append(edge)

    '''
    Let the subset {0, 1, .. 9} be represented as below and find() is called
for element 3.
              9
         /    |    \
        4     5      6
     /     \        /  \
    0        3     7    8
            /  \
           1    2

When find() is called for 3, we traverse up and find 9 as representative
of this subset. With path compression, we also make 3 as the child of 9 so
that when find() is called next time for 1, 2 or 3, the path to root is reduced.

               9
         /    /  \    \
        4    5    6     3
     /           /  \   /  \
    0           7    8  1   2

    '''

    def find(self,parents,node):
        if parents[node] != node:
            parents [node] = self.find(parents,parents[node])
        return parents[node]
    def fastunion(self,parents,ranks,n1,n2):
        '''get the parents of both nodes to union them'''
        node1 = self.find(parents,n1)
        node2 = self.find(parents,n2)
        '''get the ranks of parents of both nodes to compare them'''
        if ranks[node1] == ranks[node2]:
            parents[node1] = node2
            ranks[node2]+=1
        elif ranks[node1] > ranks[node2]:
            parents[node2] = node1
            ranks[node1]+=1
        else:
            parents[node1] = node2
            ranks[node2]+=1
    def kruskal(self):
        self.edges = sorted(self.edges,key=lambda x:x[2])
        i = 1
        e = 0
        parents = [i for i in range(self.num_of_vertices)]
        ranks = [1 for _ in range(self.num_of_vertices)]
        while i<self.num_of_vertices:
            edge = self.edges[e]
            e+=1
            '''get the parents of both nodes to union them'''
            p1 = self.find(parents,edge[0])
            p2 = self.find(parents,edge[1])
            '''print as favourable only if cycle is not formed - i.e not same parents'''
            if  p1!= p2:
                self.fastunion(parents,ranks,edge[0],edge[1])
                i+=1
                print(edge[0],"-->",edge[1],": size - ",edge[2])
#inputs
g = Graph(4)
g.addEdge([0, 1, 10])
g.addEdge([0, 2, 6])
g.addEdge([0, 3, 5])
g.addEdge([1, 3, 15])
g.addEdge([2, 3, 4])
g.kruskal()


import boto3
import sys

def main(stack_name = 'MyFirstVPC',file_path = '../CloudFormation/VPC-NotNAT.yml'):
#def main(stack_name, file_path):
    print("Starting stack creation")
    # open the test file from the current diectory
    template = open(file_path).read()
    # create the boto3 cloudformation client
    cloudformation = boto3.client("cloudformation")
    stacks = cloudformation.describe_stacks(stack_name)
    print(stacks)
    # Validate if stack exists
    waiter = cloudformation.get_waiter('stack_exists')
    print(waiter)
    if 'StackExist' in str(waiter):
        print("StackName '" + stack_name + "' already exist, aborting...")
    else:
        # create the new stack
        cloudformation.create_stack(StackName=stack_name,TemplateBody=template)
        # create the new waiter
        waiter = cloudformation.get_waiter('stack_create_complete')
        # wait until the stack state changes to "CREATE_COMPLETE"
        #    waiter.wait(StackName=stack_name)
        print("StackName '" + stack_name + "' was created sucessfully")

if __name__ == "__main__":
    #stack_name = sys.argv[1]
    #file_path = sys.argv[2]
    #main(stack_name, file_path)
    main()


#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2020/12/28 15:22
Desc: 国家金融与发展实验室-中国宏观杠杆率数据
http://114.115.232.154:8080/
"""
import pandas as pd

def macro_cnbs() -> pd.DataFrame:
    """
    国家金融与发展实验室-中国宏观杠杆率数据
    http://114.115.232.154:8080/
    :return: 中国宏观杠杆率数据
    :rtype: pandas.DataFrame
    """
    url = "http://114.115.232.154:8080/handler/download.ashx"
    temp_df = pd.read_excel(url, sheet_name="Data", header=0, skiprows=1, engine="openpyxl")
    temp_df["Period"] = pd.to_datetime(temp_df["Period"]).dt.strftime("%Y-%m")
    temp_df.dropna(axis=1, inplace=True)
    temp_df.columns = [
        "年份",
        "居民部门",
        "非金融企业部门",
        "政府部门",
        "中央政府",
        "地方政府",
        "实体经济部门",
        "金融部门资产方",
        "金融部门负债方",
    ]
    return temp_df

if __name__ == '__main__':
    macro_cnbs_df = macro_cnbs()
    print(macro_cnbs_df)


import re

from fragments import coverage, helper

# Extract score and message that use JavaGrading
def extract_java_grading_result(result, feedback_settings):
    # we have a feedback from JavaGrading
    # Fetch total for INGInious

    # WARNING : there could be multiple TOTAL in the stdout
    # So we must merge everything

    # Strips not useful things of JavaGrading

    # Display array of test suite results only if student didn't use prohib' instruction
    display = False if result.returncode == 2 else True

    result_string = result.stdout.replace("--- GRADE ---", "").replace("--- END GRADE ---", "")
    regex_strip = r"TOTAL \d*[.]?\d*\/\d*[.]?\d*"
    regex_strip2 = r"TOTAL WITHOUT IGNORED \d*[.]?\d*\/\d*[.]?\d*"

    # Remove match
    result_string = re.sub(regex_strip, '', result_string)
    result_string = re.sub(regex_strip2, '', result_string)

    regex = '\*\*TOTAL\*\*",,\*\*(\d*[.]?\d*\/\d*[.]?\d*)\*\*'
    matches = re.findall(regex, result.stdout)

    # convert everything in float
    converted_results = [
        [
            float(item)
            for item in match.split("/")
        ]
        for match in matches
    ]

    student_result, total_result = [sum(i) for i in zip(*converted_results)]

    return student_result / total_result, result_string if display \
        else feedback_settings["status_message"].get(result.returncode, "Uncommon Failure")

# Extract result from JaCoCo
def extract_jacoco_result(feedback_settings):
    coverage_stats = feedback_settings["coverage_stats"]
    # No coverage stats , cannot evaluate this
    if not coverage_stats:
        return 0.0, "NO COVERAGE CRITERIA WERE GIVEN"
    else:
        # Generate the xml report file
        gen_report = coverage.generate_coverage_report()
        print("GENERATING THE EXEC FILE : {}".format(gen_report))
        helper.run_command(gen_report)

        # extract stats
        coverage_result = coverage.extract_stats()
        filtered_coverage_result = [x for x in coverage_result if x["type"] in coverage_stats]
        print(filtered_coverage_result)

        # generate score and message

        covered = sum(x["covered"] for x in filtered_coverage_result)
        total = covered + sum(x["missed"] for x in filtered_coverage_result)

        msg = '\n'.join(
            [
                "{}:\t{}/{}".format(c["type"], c["covered"], c["covered"] + c["missed"])
                for c in filtered_coverage_result
            ]
        )

        # For security (if report gives 0 at total, don't try to apply division)
        ratio = covered / total if total > 0 else 0.0

        return ratio, msg


# Problem 463
# Date completed: 2019/09/18

# 672 ms
class Solution:
    def islandPerimeter(self, grid: List[List[int]]) -> int:
        if not grid: return 0

        h, w = len(grid), len(grid[0])
        islands = [[i,j] for i in range(h) for j in range(w) if grid[i][j]==1 ]

        self.perimeter = len(islands)*4

        def helper(i,j):
            if grid[i][j]==1: self.perimeter -= 1

        for [i,j] in islands:
            if i>0: helper(i-1,j)
            if j>0: helper(i,j-1)
            if i<h-1: helper(i+1,j)
            if j<w-1: helper(i,j+1)
            # print(i,j,self.perimeter)

        return self.perimeter


import numpy as np
import scipy.special as scs
import matplotlib.pyplot as plt

N=[5,50,500,5000]
a=13.37995764
b=12.13615216

def f(x,n):
    return x**(a+n-1)*(1-x)**(b-1)/scs.beta(a+n,b)

fig,ax=plt.subplots(nrows=2,ncols=2)
i=1
for n in N:
    plt.subplot(2,2,i)
    i+=1
    x=np.linspace(0,1,1000)
    f_x=f(x,n)
    p=0
    for j in range(500,999):
        p=p+0.001*f(x[j],n)
    plt.plot(x,f_x)
    plt.title('n='+str(n)+',  P(P>1/2)='+str(round(p,3)))

plt.show()


#   Code for the Assignment 1 of Digital Signal Processing
#   Authors: Gabriel Galeote-Checa & Anton Saikia

import matplotlib.pylab as plt
import numpy as np
import scipy.io.wavfile as wav
import wave
plt.rcParams.update({'font.size': 20})

fs, soundwave = wav.read ('original.wav') # Read sound signal

# Representation of Time Domain
t = np.linspace(0, len(soundwave)/fs, len(soundwave))  # time vector

# ---- Figure 1 ------
plt.figure(1)
plt.plot(t, soundwave)
plt.xlabel('Time (s)')
plt.ylabel('Magnitude')

# ---- Frequency Domain Calculations ----
fftSoundWave = np.fft.fft(soundwave)
f = np.linspace(0, fs, len(fftSoundWave)) # Full spectrum frequency range

fftSoundwaveHalf = fftSoundWave[:len(fftSoundWave) // 2]  # REMOVE  the half of the mirrored spectrum
fHalf = np.linspace(0, fs / 2, len(fftSoundwaveHalf)) # Half spectrum frequency range

# ---- Figure 2 ----
# FFT signal in linear and logarithmic axis
plt.figure(2)
plt.plot(fHalf, 20*np.log10(abs(fftSoundwaveHalf)))
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude (dB)')
plt.xscale('log')

# ---- Voice Processing ----
"""
Description:

The processing consists of two parts: amplification of the harmonic between 300 and 355 Hz and attenuation of the
harmonic between 400 and 460 Hz. The reason is to give same importance (magnitude range) to the first 4 harmonics
of the signal

We would need to convert from frequency to the exact sample of the array where we have to start operating. Therefore,
the conversion needs to be parsed to int obtaining the position of the fft signal where we apply the operations.

"""

fLow = 7000  # Hz
fHigh = fs/2  # Hz
fAmplifyL1 = 1700  # Hz
fAmplifyH1 = 2400  # Hz
fAmplifyL2 = 3400  # Hz
fAmplifyH2 = 3800  # Hz
a1 = 8  # amplification 1st harmonic
a2 = 8  # amplification 2nd harmonic

# Conversion from frequency to sample
fL = int((fLow / fs) * len(fftSoundWave))
fH = int((fHigh / fs) * len(fftSoundWave))

fAL1 = int((fAmplifyL1 / fs) * len(fftSoundWave))
fAH1 = int((fAmplifyH1 / fs) * len(fftSoundWave))

fAL2 = int((fAmplifyL2 / fs) * len(fftSoundWave))
fAH2 = int((fAmplifyH2 / fs) * len(fftSoundWave))

FSW = fftSoundWave  # pass fftSoundWave to other variable to not overlap the original signal

# Operations

FSW[fL:fH] = FSW[fL:fH] * 0
FSW[len(FSW) - fH:len(FSW) - fL] = FSW[len(FSW) - fH:len(FSW) - fL] * 0

FSW[fAL1:fAH1] = FSW[fAL1:fAH1] * a1
FSW[len(FSW) - fAH1:len(FSW) - fAL1] = FSW[len(FSW) - fAH1:len(FSW) - fAL1] * a1

FSW[fAL2:fAH2] = FSW[fAL2:fAH2] * a2
FSW[len(FSW) - fAH2:len(FSW) - fAL2] = FSW[len(FSW) - fAH2:len(FSW) - fAL2] * a2

# Reconstruction of the signal
timeFilteredSoundWave = np.fft.ifft(FSW)
timeFilteredSoundWave = np.real(timeFilteredSoundWave)

# ---- Figure 3 ----
# Represents the result signal in linear and logarithmic axis.
plt.figure(3)
plt.plot(fHalf, 20*np.log10(abs(FSW[:len(FSW) // 2])))
plt.xscale('log')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude (dB)')

# ---- Save Processed Sound Recording ----
f = wave.open(r"improved.wav", "wb")
# 1 channel with a sample width of 2 and frame rate of 2*fs which is standard
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(2*fs)
timeFilteredSoundWave=timeFilteredSoundWave.astype(int)
f.writeframes(timeFilteredSoundWave.tostring())
# --- EXTRA ----
# Here, the spectogram is presented to see the variation of frequencies with respect to the time domain signal.
# This representation is very useful to analyse the profile of the sound and the frequencies distribution
# along the time. Thus, frequency over time is represented.
plt.figure(4)
plt.subplot(311)
plt.plot(t, soundwave)
plt.xlabel('Time (s)')
plt.xlim([0, len(t)/fs])
plt.ylabel(' Magnitude')
plt.subplot(312)
powerSpectrum, frequenciesFound, time, imageAxis = plt.specgram(soundwave, Fs=fs)
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')
plt.subplot(313)
powerSpectrum, frequenciesFound, time, imageAxis = plt.specgram(timeFilteredSoundWave, Fs=fs)
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')

plt.show()


from dataclasses import dataclass

from odd_collector.adapters.druid.domain.column_type import ColumnType
from odd_collector_sdk.errors import MappingDataError

@dataclass
class ColumnStats:
    name: str
    type_signature: str
    data_type: str
    has_multiple_values: bool
    has_nulls: bool
    size: int
    cardinality: int
    min_value: any
    max_value: any
    error_message: any
    type: ColumnType = ColumnType.dimension

    @staticmethod
    def from_response(column_name: str, record: dict):
        try:
            # Return
            return ColumnStats(
                type_signature=record.get("typeSignature", None),
                data_type=record.get("type", None),
                has_multiple_values=record.get("hasMultipleValues", None),
                has_nulls=record.get("hasNulls", None),
                size=record.get("size", None),
                cardinality=record.get("cardinality", None),
                min_value=record.get("minValue", None),
                max_value=record.get("maxValue", None),
                error_message=record.get("errorMessage", None),
                name=column_name,
            )
        except Exception as e:
            # Throw
            raise MappingDataError(
                "Couldn't transform Druid result to Column model"
            ) from e


from contextlib import contextmanager
import logging
import sys
from threading import Lock
from time import sleep
from typing import Any, Iterator, Optional, Tuple, Union

import click
from flask import Flask, jsonify, request
from seleniumwire import webdriver # type: ignore
from selenium.common.exceptions import TimeoutException, InvalidArgumentException, UnexpectedAlertPresentException
from selenium.webdriver.firefox.options import Options
from werkzeug.wrappers import Response

View = Union[Response, str, Tuple[str, int]]

log = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger('werkzeug').disabled = True
logging.getLogger('seleniumwire.storage').disabled = True
logging.getLogger('seleniumwire.backend').disabled = True
logging.getLogger('seleniumwire.handler').disabled = True
logging.getLogger('seleniumwire.server').disabled = True

# Set up app
app = Flask(__name__)

class DriverPool:
    lock = Lock()
    retries = 4
    retry_wait = .5

    def __init__(self, n_drivers: int, headless: bool = True) -> None:
        options = Options()
        options.headless = headless
        options.set_preference('dom.webnotifications.enabled', False)
        self.drivers = [webdriver.Firefox(options=options) for _ in range(n_drivers)]

    @contextmanager
    def _get_free_driver(self) -> Iterator[Optional[webdriver.Firefox]]:
        for _ in range(self.retries):
            try:
                driver = self.drivers.pop()
                break
            except IndexError:
                sleep(self.retry_wait)
        else:
            driver = None

        try:
            yield driver
        finally:
            if driver is not None:
                self.drivers.append(driver)

    def get(self, url: str, identikey: str) -> str:
        with self._get_free_driver() as driver:
            if driver is None:
                return 'busy'

            # Load for 6 seconds at max. Why 6? Made it up. Everything should be <10ms, unless
            # someone put an infinite redirect in their XSS or something.
            driver.set_page_load_timeout(6)
            try:
                # Set the StudentIdentikey header for all outgoing requests
                def interceptor(request: Any) -> None:
                    request.headers['StudentIdentikey'] = identikey

                driver.request_interceptor = interceptor
                # Get the URL. This might fail if the previous student has an alert popup open, so
                # we catch that error and retry until it works. I don't think this is possible to
                # loop infinitely, but maybe. Hasn't happened yet.
                while True:
                    try:
                        driver.get(url)
                        break
                    except UnexpectedAlertPresentException:
                        pass
            except InvalidArgumentException:
                log.exception('Bad url')
                return 'The URL was invalid'
            except TimeoutException:
                log.exception('Timeout')
                driver.get('')
                return 'timeout'

            return 'success'

drivers: DriverPool = None # type: ignore

@app.route('/')
def index() -> View:
    return 'ok'

@app.route('/visit')
def visit() -> View:
    url = request.json.get('url') # type: ignore
    identikey = request.json.get('identikey') # type: ignore

    if not url or not identikey:
        log.error('Did not get URL or identikey')

        # This should never happen unless there is a bug somewhere
        return jsonify({ 'status': 'internal error (reach out to instructor)' }) # type: ignore

    log.info('{} requested URL: {}'.format(identikey, url))
    status = drivers.get(url, identikey)

    return jsonify({ 'status': status }) # type: ignore

@click.command()
@click.option('--debug', is_flag=True)
@click.option('--port', type=int, default=8082)
def main(debug: bool, port: int) -> None:
    global drivers
    if debug:
        # When running locally, a single browser with the UI enabled is fine
        log.setLevel(logging.DEBUG)
        drivers = DriverPool(1, headless=False)
    else:
        # When running in prod, use 5 headless browsers for concurrency
        drivers = DriverPool(5)

    log.info('Starting support server on http://127.0.0.1:{}'.format(port))

    app.run(threaded=True, debug=debug, port=port)

if __name__ == '__main__':
    main()


#about database
import sqlite3
import os
import glob
import re
from mutagen.easyid3 import EasyID3
from mutagen.mp4 import MP4
from mutagen.mp3 import MP3

def connect_db():
    HERE_DIR = os.path.dirname(os.path.abspath(__file__))

    dbpath = os.path.join(HERE_DIR, "musics.sqlite")

    # create db or connect
    conn = sqlite3.connect(dbpath, check_same_thread=False, isolation_level=None)
    cur = conn.cursor()
    return cur, conn

def create_db():
    cur=connect_db()[0]
    cur.execute(
        "CREATE TABLE musics(id INTEGER PRIMARY KEY AUTOINCREMENT, album STRING, title STRING, artist STRING, tracknumber INTEGER,total_tracknumber INTEGER, genre, file_place STRING UNIQUE, time_min INTEGER, time_sec INTEGER)"
    )

    cur.close()

# return parameter's extension example:m4a

def return_extension(file_path):
    file_extension = os.path.splitext(file_path)[1][1:]

    return file_extension

# read "music" folder
def read_musicfile():
    music_files=[]
    # read file_
    types=["m4a", "mp3"]

    for t in types:
        path_music = "./music/**/*."+t
        music_files=music_files+glob.glob(path_music, recursive=True)
    music_files = [f.replace("\\", "/") for f in music_files]

    return music_files

# insert music data in the database
def insert_musicdata(data_music):
    cur=connect_db()[0]
    conn=connect_db()[1]

    sql = "INSERT INTO musics(album, title, artist, tracknumber, total_tracknumber, genre, file_place, time_min, time_sec) values(?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT (file_place) DO NOTHING"
    data = [(
        data_music["album"],
        data_music["title"],
        data_music["artist"],
        data_music["tracknumber"],
        data_music["total_tracknumber"],
        data_music["genre"],
        data_music["file_place"],
        data_music["time_min"],
        data_music["time_sec"]
    )]
    cur.executemany(sql, data)
    conn.commit()

# get music data with mutagen

def get_musicdata_mp3(music_filepath):
    tags = EasyID3(music_filepath)
    music_time = MP3(music_filepath).info.length

    data_music = {"album": tags["album"][0],
                  "title": tags["title"][0],
                  "artist": tags["artist"][0],
                  # tracknumber is like 5/7
                  "tracknumber": tags["tracknumber"][0].split("/")[0],
                  "total_tracknumber": tags["tracknumber"][0].split("/")[1],

                  "genre": tags["genre"][0],
                  "file_place": music_filepath,
                  #time is sec
                  "time_min": int(music_time/60),
                  "time_sec": int(music_time % 60)
                  }

    return data_music

def get_musicdata_m4a(music_filepath):
    mp4 = MP4(music_filepath)
    music_time = mp4.info.length
    track_number = mp4.tags["trkn"]

    data_music = {"album": mp4.tags["\xa9alb"][0],
                  "title": mp4.tags["\xa9nam"][0],
                  "artist": mp4.tags["\xa9ART"][0],
                  # tracknumber is like 5/7
                  "tracknumber": track_number[0][0],
                  "total_tracknumber": track_number[0][1],

                  "genre": mp4.tags["\xa9gen"][0],
                  "file_place": music_filepath,
                  #time is sec
                  "time_min": int(music_time/60),
                  "time_sec": int(music_time % 60)
                  }

    return data_music

'''
https://compute-cucco.hatenablog.com/entry/2019/07/21/204512
'''

def get_count(table_name):
    cur=connect_db()[0]
    sql_count="SELECT COUNT (*) FROM {0}".format(table_name)
    #d is like (100, )
    d=cur.execute(sql_count).fetchone()
    cur.close()
    return d[0]

def get_one_column_data_list(table_name, column_name,target_column_index=0):
    #行：row number
    data_size=get_count(table_name)
    column_data_list=[None]*data_size

    sql_select="SELECT {0} FROM {1}".format(column_name,table_name)

    cur=connect_db()[0]
    d=cur.execute(sql_select)

    for index, row in enumerate(d):
        column_data_list[index]=row[target_column_index]
    cur.close()

    return column_data_list

#get all data list
#list in dictionary [{"title":...}, {"title":...}]
def get_one_row_data_list(table_name):
    row_data_dic=[]
    cur=connect_db()[0]

    for row_data_list in cur.execute(f"select * from {table_name}"):
        row_data_dic.append({
            "album":row_data_list[1],
            "title":row_data_list[2],
            "artist":row_data_list[3],
            "tracknumber":row_data_list[4],
            "total_tracknumber":row_data_list[5],
            "genre":row_data_list[6],
            "file_place":row_data_list[7],
            "time_min":row_data_list[8],
            "time_sec":row_data_list[9]
        })
    return row_data_dic


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

from openeye import *
from openeye.oechem import *
import networkx as nx
import itertools as itl
import scipy.spatial.distance as ssd
import multiprocessing

import numpy as np
import ase.io as aio
import ase.data as ad
import ase, os, sys, re, copy

import aqml.cheminfo as co
import aqml.cheminfo.core as cc

import aqml.cheminfo.math as cim
from aqml.cheminfo.molecule.elements import Elements
import aqml.cheminfo.molecule.core as cmc
import aqml.cheminfo.molecule.geometry as GM
import aqml.cheminfo.molecule.nbody as MB
import aqml.cheminfo.rdkit.resonance as crr
import aqml.cheminfo.oechem.core as coc

import aqml.cheminfo.rw.ctab as crc
from rdkit import Chem

# reference coordination number
#cnsr = {1:1, 3:1, 4:2, 5:3, 6:4, 7:3, 8:2, 9:1, 13:3, 14:4, 15:3, 16:2, 17:1, 35:1, 53:1}

T,F = True,False

#class Match(object):
#    def __init__(self, mols_q, mol_t, smiles=None):
#        if smiles is None:
#            smiles =
#        patt = smi2patt(smiles)
#
#    def filter(self,thresh):
#        matches = []
#        return icsr

dic_fmt = {'sdf': oechem.OEFormat_SDF, 'pdb': oechem.OEFormat_PDB, \
            'mol': oechem.OEFormat_MDL, 'xyz': oechem.OEFormat_XYZ}

def sdf2oem(sdf):
    ifs = oemolistream()
    assert ifs.SetFormat( dic_fmt[ sdf[-3:] ] )
    assert ifs.open(sdf)
    for m in ifs.GetOEGraphMols(): #.next()
        break
    return m

def pdb2oem(f):
    return sdf2oem(f)

def oem2can(oem, ImpHCount=F, rebuild=F):
    # ISOMERIC identical to Isotopes | AtomStereo | BondStereo | Canonical | AtomMaps | RGroups
    if ImpHCount:
        flavor = OESMILESFlag_ImpHCount | OESMILESFlag_Canonical
    else:
        flavor = OESMILESFlag_Canonical
    m = rebuild_m(oem) if rebuild else oem
    return OECreateSmiString(m, flavor)

def oem2smi(oem, ImpHCount=F, rebuild=F):
    return oem2can(oem, ImpHCount=ImpHCount, rebuild=rebuild)

def smi2oem(smi, addh=False):
    m = OEGraphMol()
    iok = OESmilesToMol(m,smi)
    #assert iok, '#ERROR: parsing SMILES failed!'
    if addh:
        OEAddExplicitHydrogens(m)
    else:
        OESuppressHydrogens(m,F,F,F)
    return iok, m

def rebuild_mol(m):
    mu = OEGraphMol()
    atoms = {}; icnt = 0
    for ai in m.GetAtoms():
        ia = ai.GetIdx()
        zi = ai.GetAtomicNum()
        if zi > 1:
            aiu = mu.NewAtom( zi )
            aiu.SetHyb( OEGetHybridization(ai) )
            aiu.SetImplicitHCount( ai.GetImplicitHCount() )
            atoms[ icnt ] = aiu
            icnt += 1
    for bi in m.GetBonds():
        p, q = bi.GetBgnIdx(), bi.GetEndIdx()
        biu = mu.NewBond( atoms[p], atoms[q], bi.GetOrder() )
    OEFindRingAtomsAndBonds(mu)
    OEAssignAromaticFlags(mu, OEAroModel_OpenEye)
    OECanonicalOrderAtoms(mu)
    OECanonicalOrderBonds(mu)
    return mu

def get_bom(m):
    na = m.NumAtoms()
    bom = np.zeros((na,na), dtype=int)
    for bi in m.GetBonds():
        i,j,boij = bi.GetBgnIdx(), bi.GetEndIdx(), bi.GetOrder()
        bom[i,j] = bom[j,i] = boij
    return bom

def get_coords(m):
    coords = []
    for ai in m.GetAtoms():
        coords_ai = m.GetCoords(ai)
        coords.append( coords_ai )
    return np.array(coords,dtype=float)

def vang(u,v):
    cost = np.dot(u,v)/(np.linalg.norm(u) * np.linalg.norm(v))
# sometimes, `cost might be 1.00000000002, then np.arccos(cost)
# does not exist!
    u = cost if abs(cost) <= 1 else 1.0
    return np.arccos( u )*180/np.pi

def prepare_protein(f):
    m = pdb2oem(f)
    OESuppressHydrogens(m, F, F, F)

    lig = oechem.OEGraphMol()
    prot = oechem.OEGraphMol()
    wat = oechem.OEGraphMol()
    other = oechem.OEGraphMol()
    assert oechem.OESplitMolComplex(lig, prot, wat, other, m)

    OERemoveFormalCharge(prot) # note that for quaternary amines (>[N+]<), charges retain
    OEAddExplicitHydrogens(prot)
    OESet3DHydrogenGeom(prot)
    f2 = f[:-4]+'-new.pdb'
    write(prot,f2)
    write(prot,f[:-4]+'-new.xyz')
    obj = StringM(f2)
    obj.check_valence_states()
    #obj.check_interatomic_distance()
    #assert iok

class XYZMol(object):
    """
    perceive BO from geom by OEChem
    """
    def __init__(self, fx):
        ifs = oemolistream()
        assert ifs.SetFormat( dic_fmt['xyz'] )
        assert ifs.open(fx)
        for mol in ifs.GetOEGraphMols(): #.next()
            break
        oechem.OEDetermineConnectivity(mol)
        oechem.OEFindRingAtomsAndBonds(mol)
        oechem.OEPerceiveBondOrders(mol)
        oechem.OEAssignImplicitHydrogens(mol)
        oechem.OEAssignFormalCharges(mol)
        self.mol = mol

    @property
    def g(self):
        return

class ConnMol(cmc.RawMol):
    """
    Mol with connectivity only
    No bond order perceived
    """
    def __init__(self, obj, ivdw=False, scale=1.0, iprt=F):
        cmc.RawMol.__init__(self, obj, ivdw=ivdw, scale=scale, iprt=iprt)

    def get_mol(self):
        return coc.newmol(self.zs, np.zeros(self.na), self.g, self.coords).mol

    @property
    def mol(self):
        if not hasattr(self, '_mol'):
            self._mol = self.get_mol()
        return self._mol

    def get_strained(self):
        return np.any( [ [ OEAtomIsInRingSize(ai, n) for n in [3,4,5,7] ] for ai in self.mol.GetAtoms() ] )

    @property
    def strained(self):
        if not hasattr(self, '_strained'):
            self._strained = self.get_strained()
        return self._strained

    @property
    def is_mcplx(self): # is mol complex?
        if not hasattr(self, '_imcplx'):
            self._imcplx = (not self.is_connected)
        return self._imcplx

class StringM(coc.newmol):

    """
    build molecule object with a string (SMILES or sdf file) as input
    """

    def __init__(self, obj, stereo=F, isotope=F, suppressH=F, ImpHCount=F, ds=None,\
                 pls=None, scale_vdw=1.2, resort=F, simple=F, debug=F, nprocs=1):

        self.suppressH = suppressH
        self.ImpHCount = ImpHCount
        self.debug = debug
        istat = T
        if isinstance(obj, str):
            string = obj
            if os.path.exists(string):
                if string.endswith( ('sdf','mol','pdb') ):
                    m = sdf2oem(string)
                    #print('######################')
                    if suppressH:
                        OESuppressHydrogens(m, F, F, F)
                else:
                    raise Exception('#ERROR: file type not supported')
                if suppressH:
                    OESuppressHydrogens(m, False, False, False)
            else: # presumably a SMILES string
                #print('------------------------')
                if ('@' in string) and (not stereo):
                    # now remove stereo and isotopes, otherwise we may
                    # encounter error " idx out of bound when calling get_bom()
                    istat, _m = smi2oem(string)
                    _s = OECreateSmiString(_m, OESMILESFlag_Canonical)
                else:
                    _s = string
                istat, m = smi2oem(_s)
                if istat and (not suppressH):
                    iok = OEAddExplicitHydrogens(m)
        elif isinstance(obj, oechem.OEGraphMol):
            m = obj
        else:
            raise Exception('#ERROR: input `string not supported')

        self.istat = istat
        if not istat:
            raise Exception('istat is False??')

        na = m.NumAtoms()
        _zs = []; chgs = []
        for ai in m.GetAtoms():
            _zs.append( ai.GetAtomicNum() )
            chgs.append( ai.GetFormalCharge() )
        chgs = np.array(chgs,dtype=int)
        zs = np.array(_zs,dtype=int)
        ias = np.arange(na)
        bom = get_bom(m)
        coords = get_coords(m)
        if resort and (1 in _zs):
            ih1 = _zs.index(1)
            if np.any(zs[ih1:] > 1):
                print(' ***** hydrogens were pushed to the end')
                newm = OEGraphMol()
                _ias = np.concatenate((ias[zs>1], ias[zs==1]))
                zs = zs[_ias]
                chgs = chgs[_ias]
                coords = coords[_ias]
                _bom = bom.copy()
                bom = _bom[_ias][:,_ias]
        self.zs = zs
        self.bom = bom
        self.coords = coords
        self.chgs = chgs

        coc.newmol.__init__(self, zs, chgs, bom, coords=coords, ds=ds, pls=pls, \
                         scale_vdw=scale_vdw, debug=debug, nprocs=nprocs)

    #@property
    #def newm(self):
    #    if not hasattr(self, '_newm'):
    #        self._newm = coc.newmol(self.zs, self.chgs, self.bom, coords=self.coords)
    #    return self._newm

class smiles_db(object):

    def __init__(self, obj, sort=T):
        if isinstance(obj,(tuple,list)):
            ss1 = obj
        elif isinstance(obj,str):
            assert os.path.exists(obj), '#ERROR: file does not exist!'
            ss1 = [ si.strip().split()[0] for si in open(obj).readlines() ]
        else:
            raise Exception('unknown input object!')
        nm = len(ss1)
        ims = np.arange(nm).astype(int)
        self.ims = ims
        nas = []
        ss2 = []
        iN5s = []
        for si in ss1:
             om = StringM(si, suppressH=F, simple=T)
             iasN5 = om.ias[ np.logical_and(om.zs==7, om.tvs==5) ]
             iN5 = F
             for iaN in iasN5:
                 _bosi = om.bom[iaN]
                 bosi = _bosi[_bosi>0]
                 bosi.sort()
                 bosi = bosi[::-1]
                 sbo = ''.join([ '%d'%bo for bo in bosi ])
                 if not (sbo=='32' or ((om.zs[om.bom[iaN]>0] == 8).sum() == 2)):
                     iN5 = T
                     break
             iN5s.append(iN5)
             ss2.append( oem2can(om.oem) ) # oechem can strings
             nas.append(om.nheav)
        nas = np.array(nas,dtype=int)
        iN5s = np.array(iN5s, dtype=bool)
        ns = np.unique(nas) #dtype=int)
        if sort:
            ss3 = []
            nms = []
            nsheav = []
            imap = []
            iN5s_new = []
            for ni in ns:
                idx = ims[ni==nas]
                ssi = np.array(ss2)[idx]
                irs = np.argsort(ssi)
                idx2 = idx[irs] #.sort()
                iN5s_new += list(iN5s[idx2])
                nmi = len(ssi)
                nms.append(nmi)
                nsheav += [ni]*nmi
                ss3 += list(ssi[irs])
                imap += list(idx2)
            print(' ** smiles were sorted by num_heav_atom and then name')
            imap = np.array(imap, dtype=int)
            iN5s = np.array(iN5s_new, dtype=bool)
        else:
            ss3 = ss2
            nsheav = nas
            nms = None
            imap = None
        self.smiles = ss3
        self.nsheav = nsheav
        self.imap = imap
        self.iN5s = iN5s
        self.nms = nms

def find_neighbors(g, ias):
    """
    get the neighbors of a list of atoms `ias
    """
    neibs = []
    na = g.shape[0]
    ias0 = np.arange(na)
    for ia in ias:
        for ja in ias0[ g[ia] > 0 ]:
            if ja not in ias:
                neibs.append( ja )
    return neibs


## Lenfest Biodiversity -IBA coverage
## coverage by Ecoregion
## coverage within MPAs by Ecoregion

import geopandas as gpd
import pandas as pd

## Ecoregion feature class
base_gdb =  r'V:\lenfest_nmsf_biodiversity\Data\lenfest_nmsf_base_prep.gdb'
ecoregion_base = gpd.read_file(f'{base_gdb}', layer='marine_ecoregions_final')
ecoregion_base["ER Area"] = (ecoregion_base["Shape_Area"] / (1000*1000))
ecoregion_base = ecoregion_base.drop(columns=["geometry","Shape_Length","Shape_Area"])

## CSVs to tables
all_output_csv = r'V:\lenfest_nmsf_biodiversity\Data\IBA\IBA_coverage_ecoregion.csv'
mpa_output_csv = r'V:\lenfest_nmsf_biodiversity\Data\IBA\IBA_coverage_MPA.csv'
fp_mpa_output_csv = r'V:\lenfest_nmsf_biodiversity\Data\IBA\IBA_coverage_FP_MPA.csv'

ecoregion_df = gpd.read_file(all_output_csv)
mpa_df = gpd.read_file(mpa_output_csv)
full_protect_mpa_df = gpd.read_file(fp_mpa_output_csv)

## rename area/percentage columns, drop columns
## drop columns and recalc below - checked that this matches the output from GPD intersect
ecoregion_df = ecoregion_df.apply(pd.to_numeric, errors='ignore')
ecoregion_df["IBA Area in ER"] = (ecoregion_df["iba_intersect_area"] / (1000*1000))
ecoregion_df = ecoregion_df.drop(columns=["field_1","geometry","NAME","Shape_Length_1","Shape_Area_1","Percent of Ecoregion Covered by IBA","iba_intersect_area"])

mpa_df = mpa_df.apply(pd.to_numeric, errors='ignore')
mpa_df["IBA Area in MPA"] = ( mpa_df["mpa_intersect_area"] / (1000*1000))
mpa_df = mpa_df.drop(columns=["field_1","geometry","NAME","Shape_Length_1","Shape_Area_1","mpa_intersect_area"])

full_protect_mpa_df = full_protect_mpa_df.apply(pd.to_numeric, errors='ignore')
full_protect_mpa_df["IBA Area in FP-MPA"] = (full_protect_mpa_df["fp_mpa_intersect_area"] / (1000*1000))
full_protect_mpa_df = full_protect_mpa_df.drop(columns=["field_1","geometry","NAME","Shape_Length_1","Shape_Area_1","fp_mpa_intersect_area"])

## join tables
summary_df = pd.merge(ecoregion_base, ecoregion_df, how="left", left_on="Ecoregion_ID", right_on="Ecoregion_ID")
summary_df = pd.merge(summary_df, mpa_df, how="left", left_on="Ecoregion_ID", right_on="Ecoregion_ID")
summary_df = pd.merge(summary_df, full_protect_mpa_df, how="left", left_on="Ecoregion_ID", right_on="Ecoregion_ID")

## calculate coverage of ecoregion feature in MPA networks
summary_df["PCT of ER Covered by IBA"] = (summary_df["IBA Area in ER"] / summary_df["ER Area"]) * 100
summary_df["PCT of ER IBA in MPA"] = (summary_df["IBA Area in MPA"] / summary_df["IBA Area in ER"]) * 100
summary_df["PCT of ER IBA in FP MPA"] = (summary_df["IBA Area in FP-MPA"] / summary_df["IBA Area in ER"]) * 100

## remove rows with no IBAs
summary_df = summary_df[summary_df['IBA Area in ER'].notna()]

## calculate summary row
summary_df = summary_df.append(
    {'NAME': 'All Ecoregions',
     'Ecoregion_ID': '0',
     'ER Area': summary_df["ER Area"].sum(),
     'IBA Area in ER': summary_df["IBA Area in ER"].sum(),
     'IBA Area in MPA': summary_df["IBA Area in MPA"].sum(),
     'IBA Area in FP-MPA': summary_df["IBA Area in FP-MPA"].sum(),
     'PCT of ER Covered by IBA': summary_df["IBA Area in ER"].sum() / summary_df["ER Area"].sum() * 100,
     'PCT of ER IBA in MPA': summary_df["IBA Area in MPA"].sum() / summary_df["IBA Area in ER"].sum() * 100,
     'PCT of ER IBA in FP MPA': summary_df["IBA Area in FP-MPA"].sum() / summary_df["IBA Area in ER"].sum() * 100
     },
    ignore_index = True)

## clean up table
summary_df.sort_values(by=['IBA Area in ER'], inplace=True, ascending=False)
summary_df.fillna("--", inplace=True)
summary_df.set_index('Ecoregion_ID', inplace=True)
pd.set_option('display.float_format', lambda x: '%.3f' % x)

## output to CSV
summary_df.to_csv(r'V:\lenfest_nmsf_biodiversity\Data\_Scripts\iba_coverage_ecoregion_MPA.csv')


import datetime

from django.test import TestCase, override_settings
from django.utils import timezone

from tests import factories

@override_settings(CACHES={
    'default': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
    }
})
class BaseApiTest(TestCase):
    @classmethod
    def setUpClass(cls):
        """
        setting up the following scenario
        ---------------------------------------------------
        |PROJ | USERS  |             TIME LOGS             |
        ---------------------------------------------------
            user_admin

        projA
            ├── userA1
            |     ├─────── 2022-02-01 11:00:00 - 2022-02-01 18:00 (7h)
            |     └─────── 2022-03-01 11:00:00 - None
            |
            └── userA2
                  └─────── 2022-02-01 11:00:00 - 2022-03-01 14:00 (3h)

        projB
            └── userB1
                  └─────── 2022-03-01 15:00:00 - None
        --------------
        """
        # create users
        cls.user_admin = factories.AdminFactory()
        cls.userA1 = factories.UserFactory(username='userA1')
        cls.userA2 = factories.UserFactory(username='userA2')
        cls.userB1 = factories.UserFactory(username='userB1')
        super(BaseApiTest, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        super(BaseApiTest, cls).tearDownClass()

    def setUp(self):
        # assign users and create projects

        # projects
        self.projA = factories.ProjectFactory(name='projA', creator=self.user_admin)
        self.projB = factories.ProjectFactory(name='projB', creator=self.user_admin)

        # userA1 to projA
        self.userA1_projA = factories.ProjectAssignmentFactory(
            user=self.userA1,
            project=self.projA,
        )
        # userA2 to projA
        self.userA2_projA = factories.ProjectAssignmentFactory(
            user=self.userA2,
            project=self.projA,
        )
        # userB1 to projB
        self.userB1_projB = factories.ProjectAssignmentFactory(
            user=self.userB1,
            project=self.projB
        )

        # logs of userA1 to projA
        self.tl_userA1_projA_7h = factories.TimeLogCompleteFactory7h(
            project_assignment=self.userA1_projA
        )
        self.tl_userA1_projA_3h = factories.TimeLogOnlyStartFactory(
            project_assignment=self.userA1_projA
        )
        # logs of userA2 to projA
        self.tl_userA2_projA_partial = factories.TimeLogCompleteFactory3h(
            project_assignment=self.userA2_projA
        )
        # logs of userB1 to projB
        self.tl_userB1_projB_partial = factories.TimeLogOnlyStartFactory(
            project_assignment=self.userB1_projB,
            start_time=datetime.datetime(
                year=2022,
                month=3,
                day=1,
                hour=15,
                minute=00,
                tzinfo=timezone.utc
            )
        )

        super(BaseApiTest, self).setUp()

    def tearDown(self):
        super(BaseApiTest, self).tearDown()


import copy
import math
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from lib.config import cfg

from layers.low_rank import LowRank
import blocks
import lib.utils as utils
from models.basic_model import BasicModel
from layers.positional_encoding import PositionalEncoding

import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import (TransformerEncoder, TransformerDecoder,
                      TransformerEncoderLayer, TransformerDecoderLayer)
from torch import Tensor

PAD_IDX=0
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

def generate_square_subsequent_mask(sz):
    mask = (torch.triu(torch.ones((sz, sz), device=DEVICE)) == 1).transpose(0, 1)
    mask = mask.float().masked_fill(mask == 0, float('-inf')).masked_fill(mask == 1, float(0.0))
    return mask

def create_src_mask(src, tgt=None, tgt_len=None):
    src_seq_len = src.shape[0]

    src_mask = torch.zeros((src_seq_len, src_seq_len), device=DEVICE).type(torch.bool)
    src_padding_mask = (src == PAD_IDX).transpose(0, 1)
    return src_mask, src_padding_mask

def create_tgt_mask(tgt):
    tgt_seq_len = tgt.shape[0]
    tgt_mask = generate_square_subsequent_mask(tgt_seq_len)
    B = tgt.shape[1]
    #no <bos> and <eos>, so custimize ....
    shift_tgt = torch.cat([tgt.new_ones(1,B), tgt[0:tgt_seq_len-1,:]], dim=0)
    tgt_padding_mask = ((tgt == PAD_IDX) & (shift_tgt == PAD_IDX)).transpose(0, 1)
    return tgt_mask, tgt_padding_mask

def create_tgt_padding_mask(tgt):
    tgt_seq_len = tgt.shape[0]
    B = tgt.shape[1]
    #no <bos> and <eos>, so custimize ....
    shift_tgt = torch.cat([tgt.new_ones(1,B), tgt[0:tgt_seq_len-1,:]], dim=0)
    tgt_padding_mask = ((tgt == PAD_IDX) & (shift_tgt == PAD_IDX)).transpose(0, 1)
    return tgt_padding_mask

class PositionalEncoding(nn.Module):
    def __init__(self, emb_size: int, dropout, maxlen: int = 5000):
        super(PositionalEncoding, self).__init__()
        den = torch.exp(- torch.arange(0, emb_size, 2) * math.log(10000) / emb_size)
        pos = torch.arange(0, maxlen).reshape(maxlen, 1)
        pos_embedding = torch.zeros((maxlen, emb_size))
        pos_embedding[:, 0::2] = torch.sin(pos * den)
        pos_embedding[:, 1::2] = torch.cos(pos * den)
        pos_embedding = pos_embedding.unsqueeze(-2)

        self.dropout = nn.Dropout(dropout)
        self.register_buffer('pos_embedding', pos_embedding)

    def forward(self, token_embedding: Tensor):
        return self.dropout(token_embedding +
                            self.pos_embedding[:token_embedding.size(0),:])

class PositionalEncodingTrainable(nn.Module):
    def __init__(self, emb_size: int, dropout, maxlen: int = 100):
        super(PositionalEncodingTrainable, self).__init__()
        den = torch.exp(- torch.arange(0, emb_size, 2) * math.log(10000) / emb_size)
        pos = torch.arange(0, maxlen).reshape(maxlen, 1)
        self.pos_embedding_layer = nn.Embedding(maxlen, emb_size)
        #self.pos_embedding = self.pos_embedding_layer.weight
        #self.pos_embedding = self.pos_embedding.unsqueeze(-2)

        self.dropout = nn.Dropout(dropout)

    def forward(self, token_embedding: Tensor):
        pos_embedding = self.pos_embedding_layer.weight.unsqueeze(-2)
        return self.dropout(token_embedding + pos_embedding[:token_embedding.size(0),:])

class TokenEmbedding(nn.Module):
    def __init__(self, vocab_size: int, emb_size):
        super(TokenEmbedding, self).__init__()
        self.embedding = nn.Embedding(vocab_size, emb_size)
        self.emb_size = emb_size
    def forward(self, tokens: Tensor):
        return self.embedding(tokens.long())# * math.sqrt(self.emb_size)

class Transformer(BasicModel):

    def __init__(self, dropout=0.5):
        super(Transformer, self).__init__()
        self.vocab_size = cfg.MODEL.VOCAB_SIZE + 1

        if (cfg.MODEL.TASK != 'IMAGE_CAPTIONING'):
            self.att_embed = lambda x:x
        else:
            sequential = []
            sequential.append(nn.Linear(cfg.MODEL.ATT_FEATS_DIM, cfg.MODEL.ATT_FEATS_EMBED_DIM))
            sequential.append(utils.activation(cfg.MODEL.ATT_FEATS_EMBED_ACT))
            if cfg.MODEL.ATT_FEATS_NORM == True:
                sequential.append(nn.LayerNorm(cfg.MODEL.ATT_FEATS_EMBED_DIM))
            if cfg.MODEL.DROPOUT_ATT_EMBED > 0:
                sequential.append(nn.Dropout(cfg.MODEL.DROPOUT_ATT_EMBED))
            self.att_embed = nn.Sequential(*sequential) if len(sequential) > 0 else None

        ntoken = self.vocab_size
        ninp = cfg.MODEL.BILINEAR.DIM
        nhead = cfg.MODEL.BILINEAR.HEAD
        nhid = 2048
        nlayers_encoder = cfg.MODEL.BILINEAR.ENCODE_LAYERS
        nlayers_decoder = cfg.MODEL.BILINEAR.DECODE_LAYERS

        encoder_layer = TransformerEncoderLayer(d_model=ninp, nhead=nhead,
                                                dim_feedforward=nhid)
        self.transformer_encoder = TransformerEncoder(encoder_layer, num_layers=nlayers_encoder)
        decoder_layer = TransformerDecoderLayer(d_model=ninp, nhead=nhead,
                                                dim_feedforward=nhid)
        self.transformer_decoder = TransformerDecoder(decoder_layer, num_layers=nlayers_decoder)

        self.model_type = 'Transformer'

        self.embedder = TokenEmbedding(ntoken, ninp)
        self.positional_encoding = PositionalEncodingTrainable(ninp, dropout=cfg.MODEL.DROPOUT_WORD_EMBED)
        self.ninp = ninp

        self.generator = nn.Linear(ninp, ntoken)

        self.encoder_norm_word = lambda x:x#torch.nn.LayerNorm(ninp)
        self.decoder_norm_word = lambda x:x#torch.nn.LayerNorm(ninp)
#         self.encoder_norm_word = torch.nn.LayerNorm(ninp)
#         self.decoder_norm_word = torch.nn.LayerNorm(ninp)

        self.init_weights()
        self.kl_weight = 0.0

    def init_weights(self):
        for p in self.parameters():
            if p.dim() > 1:
                nn.init.xavier_uniform_(p)

#     def forward(self, src, src_mask):
#         src = self.encoder(src) * math.sqrt(self.ninp)
#         src = self.pos_encoder(src)
#         output = self.transformer_encoder(src, src_mask)
#         output = self.decoder(output)
#         return output

    def preprocess_for_paraphrase_task(self, **kwargs):
        source_seq = kwargs[cfg.PARAM.SOURCE_SEQ]
        source_seq = source_seq.transpose(0, 1)
        att_feats = self.positional_encoding(self.embedder(source_seq))
        att_feats = self.encoder_norm_word(att_feats)

        src_mask, src_padding_mask = create_src_mask(source_seq)

        return att_feats, src_mask, src_padding_mask

    def get_src_tensors_for_decoder(self, **kwargs):
        if (cfg.MODEL.TASK != 'IMAGE_CAPTIONING'):
            att_feats, src_mask, src_padding_mask = self.preprocess_for_paraphrase_task(**kwargs)
        else:
            att_feats = kwargs[cfg.PARAM.ATT_FEATS]
            att_mask = kwargs[cfg.PARAM.ATT_FEATS_MASK]
            att_feats = att_feats.transpose(0, 1)
            src_padding_mask = ~(att_mask.to(torch.bool))
            src_seq_len = att_feats.shape[0]
            src_mask = torch.zeros((src_seq_len, src_seq_len), device=DEVICE).type(torch.bool)
            att_feats = self.att_embed(att_feats)
        #print(att_feats.shape, src_mask.shape, src_padding_mask.shape)

        att_feats = self.transformer_encoder(att_feats, src_mask, src_padding_mask)
        return att_feats, src_padding_mask

    def get_tgt_tensors_for_decoder(self, **kwargs):
        tgt_input = kwargs[cfg.PARAM.INPUT_SENT]
        tgt_input = tgt_input.transpose(0, 1)
        tgt_mask, tgt_padding_mask = create_tgt_mask(tgt_input)
        return tgt_input, tgt_mask, tgt_padding_mask

    def forward(self, **kwargs):
        att_feats, src_padding_mask = self.get_src_tensors_for_decoder(**kwargs)
        src_padding_mask = utils.expand_tensor(src_padding_mask, cfg.DATA_LOADER.SEQ_PER_IMG)
        att_feats = utils.expand_tensor(att_feats, cfg.DATA_LOADER.SEQ_PER_IMG, dim=2)

        tgt_input, tgt_mask, tgt_padding_mask = self.get_tgt_tensors_for_decoder(**kwargs)

        tgt_emb = self.positional_encoding(self.embedder(tgt_input))
        tgt_emb = self.decoder_norm_word(tgt_emb)
        #print(tgt_emb.shape, att_feats.shape, tgt_mask.shape, tgt_padding_mask.shape, src_padding_mask.shape)
        decoder_outputs = self.transformer_decoder(tgt_emb, att_feats, tgt_mask, None,
                                        tgt_padding_mask, src_padding_mask)
        decoder_outputs = decoder_outputs.transpose(0, 1)
        out = self.generator(decoder_outputs)

        return out

    def get_logprobs_state(self, att_feats, tgt_mask, src_padding_mask, **kwargs):
        wt = kwargs[cfg.PARAM.WT]
        state = kwargs[cfg.PARAM.STATE]

        if state is None:
            ys = wt.unsqueeze(0)
        else:
            ys = torch.cat([state[0][0], wt.unsqueeze(0)], dim=0)

        tgt_input = ys
        tgt_emb = self.positional_encoding(self.embedder(tgt_input))
        tgt_emb = self.decoder_norm_word(tgt_emb)

        currentL = tgt_input.size(0)
        step_tgt_mask = tgt_mask[:currentL, :currentL]
        step_tgt_padding_mask = create_tgt_padding_mask(tgt_input)
        decoder_outputs = self.transformer_decoder(tgt_emb, att_feats, step_tgt_mask, None,
                                        step_tgt_padding_mask, src_padding_mask)
        #decoder_outputs = decoder_outputs.transpose(0, 1)
        out = self.generator(decoder_outputs[-1,:,:])

        logprobs = F.log_softmax(out, dim=-1)

        return logprobs, [ys.unsqueeze(0)]

    # the beam search code is inspired by https://github.com/aimagelab/meshed-memory-transformer
    def decode_beam(self, **kwargs):
        att_feats, src_padding_mask = self.get_src_tensors_for_decoder(**kwargs)

        beam_size = kwargs['BEAM_SIZE']
        batch_size = att_feats.size(1)
        seq_logprob = torch.zeros((batch_size, 1, 1)).cuda()
        log_probs = []
        selected_words = None
        seq_mask = torch.ones((batch_size, beam_size, 1)).cuda()

        state = None
        wt = Variable(torch.zeros(batch_size, dtype=torch.long).cuda())
        outputs = []

        tgt_mask = generate_square_subsequent_mask(cfg.MODEL.SEQ_LEN)

        for t in range(cfg.MODEL.SEQ_LEN):
            cur_beam_size = 1 if t == 0 else beam_size

            kwargs[cfg.PARAM.WT] = wt
            kwargs[cfg.PARAM.STATE] = state
            word_logprob, state = self.get_logprobs_state(att_feats, tgt_mask, src_padding_mask, **kwargs)
            word_logprob = word_logprob.view(batch_size, cur_beam_size, -1)
            candidate_logprob = seq_logprob + word_logprob

            # Mask sequence if it reaches EOS
            if t > 0:
                mask = (selected_words.view(batch_size, cur_beam_size) != 0).float().unsqueeze(-1)
                seq_mask = seq_mask * mask
                word_logprob = word_logprob * seq_mask.expand_as(word_logprob)
                old_seq_logprob = seq_logprob.expand_as(candidate_logprob).contiguous()
                old_seq_logprob[:, :, 1:] = -999
                candidate_logprob = seq_mask * candidate_logprob + old_seq_logprob * (1 - seq_mask)

            selected_idx, selected_logprob = self.select(batch_size, beam_size, t, candidate_logprob)
            #print("selected_idx",selected_idx[0])
            selected_beam = selected_idx / candidate_logprob.shape[-1]
            selected_beam = selected_beam.to(torch.long)
            #print("selected_beam",selected_beam[0])
            selected_words = selected_idx - selected_beam * candidate_logprob.shape[-1]
            #print("selected_words",selected_words[0])

            seq_logprob = selected_logprob.unsqueeze(-1)

            #print(t, seq_logprob.shape, seq_mask.shape, selected_beam.shape)

            seq_mask = torch.gather(seq_mask, 1, selected_beam.unsqueeze(-1))
            outputs = list(torch.gather(o, 1, selected_beam.unsqueeze(-1)) for o in outputs)
            outputs.append(selected_words.unsqueeze(-1))

            this_word_logprob = torch.gather(word_logprob, 1,
                selected_beam.unsqueeze(-1).expand(batch_size, beam_size, word_logprob.shape[-1]))
            this_word_logprob = torch.gather(this_word_logprob, 2, selected_words.unsqueeze(-1))
            log_probs = list(
                torch.gather(o, 1, selected_beam.unsqueeze(-1).expand(batch_size, beam_size, 1)) for o in log_probs)
            log_probs.append(this_word_logprob)
            selected_words = selected_words.view(-1, 1)
            wt = selected_words.squeeze(-1)

            if t == 0:
                att_feats = utils.expand_tensor(att_feats, beam_size, dim=2)
                src_padding_mask = utils.expand_tensor(src_padding_mask, beam_size)
                state[0] = state[0].squeeze(0)
                state[0] = utils.expand_tensor(state[0], beam_size, dim=2)
                state[0] = state[0].unsqueeze(0)

        seq_logprob, sort_idxs = torch.sort(seq_logprob, 1, descending=True)
        outputs = torch.cat(outputs, -1)
        outputs = torch.gather(outputs, 1, sort_idxs.expand(batch_size, beam_size, cfg.MODEL.SEQ_LEN))
        log_probs = torch.cat(log_probs, -1)
        log_probs = torch.gather(log_probs, 1, sort_idxs.expand(batch_size, beam_size, cfg.MODEL.SEQ_LEN))

        outputs = outputs.contiguous()[:, 0]
        log_probs = log_probs.contiguous()[:, 0]

        return outputs, log_probs

    def decode(self, **kwargs):
        beam_size = kwargs['BEAM_SIZE']
        greedy_decode = kwargs['GREEDY_DECODE']
        if (cfg.MODEL.TASK != 'IMAGE_CAPTIONING'):
            att_feats, att_mask = self.preprocess_for_paraphrase_task(**kwargs)
        else:
            att_feats = kwargs[cfg.PARAM.ATT_FEATS]
            att_mask = kwargs[cfg.PARAM.ATT_FEATS_MASK]

        batch_size = att_feats.size(0)
        att_feats = self.att_embed(att_feats)

        att_mask = att_mask.unsqueeze(-2)
        encoder_out = self.encoder(att_feats, att_mask)

        self.decoder.init_buffer(batch_size)

        state = None
        sents = Variable(torch.zeros((batch_size, cfg.MODEL.SEQ_LEN), dtype=torch.long).cuda())
        logprobs = Variable(torch.zeros(batch_size, cfg.MODEL.SEQ_LEN).cuda())
        wt = Variable(torch.zeros(batch_size, dtype=torch.long).cuda())
        unfinished = wt.eq(wt)
        kwargs[cfg.PARAM.ATT_FEATS] = encoder_out
        kwargs[cfg.PARAM.ATT_FEATS_MASK] = att_mask
        for t in range(cfg.MODEL.SEQ_LEN):
            kwargs[cfg.PARAM.WT] = wt
            kwargs[cfg.PARAM.STATE] = state
            logprobs_t, state = self.get_logprobs_state(**kwargs)

            if greedy_decode:
                logP_t, wt = torch.max(logprobs_t, 1)
            else:
                probs_t = torch.exp(logprobs_t)
                wt = torch.multinomial(probs_t, 1)
                logP_t = logprobs_t.gather(1, wt)
            wt = wt.view(-1).long()
            unfinished = unfinished * (wt > 0)
            wt = wt * unfinished.type_as(wt)
            sents[:,t] = wt
            logprobs[:,t] = logP_t.view(-1)

            if unfinished.sum() == 0:
                break
        self.decoder.clear_buffer()
        return sents, logprobs


import wx
from Message import Message
from datetime import datetime
import EClass

class Forum:
   def __init__(self):
      self.messagesList = []
      self.view = None

      # infinite recurion without this CallLater
      # EClass.GetInstance() -> Forum() ->EClass.GetInstance() ...
      # just wait a sec, then get the instance. Does anyone have a better
      # solution?
      def listen():
         EClass.EClass.GetInstance().connection.registerMessageListener(
            'new message', self.onMessage
         )
         EClass.EClass.GetInstance().registerOnSaveListener(
            'forum', self.onSaveListener
         )
      wx.CallLater(1, listen)

   # Add message gets called by the view only when a new message is
   # created in a text control
   def AddMessage(self, message):
      message = Message(EClass.EClass.GetInstance().user.username, datetime.now(), message)
      if EClass.EClass.GetInstance().user.isPresenter():
         # if the user who typed the message is a presenter, broadcast his
         # message to all students and add his message to his forum view.
         EClass.EClass.GetInstance().connection.send('new message', message)
         self.messagesList.append(message)
         self.view and self.view.Refresh()
      else:
         # if the user who typed the message is a student, don't add the message
         # to the forum yet.. send the message to the presenter who will then
         # broadcast the message back to everyone.
         EClass.EClass.GetInstance().connection.send('new message', message)

   def onMessage(self, message, student):
      print('message received', message, student)
      if EClass.EClass.GetInstance().user.isPresenter():
         # if the presenter is receiving a message the message sender should
         # never be the presenter (send the message to himself). the message
         # sender should always be a student.
         assert(student != False)
         # don't trust what the student set as the 'name' and 'time' fields..
         message = Message(student, datetime.now(), message.text)
         # broadcast the message out to all students and add the message
         # to the presenter's view
         self.messagesList.append(message)
         self.view and self.view.Refresh()
         EClass.EClass.GetInstance().connection.send('new message', message)
      else:
         # If a student is receiving a message, the message sender should always
         # be the presenter (student messages are forwarded through the presenter)
         # student is the username of the student who sent the message,
         # or false if the presenter sent the message
         assert(student == False)
         # the message is good to go, we trust the data that the presenter sent
         # us
         self.messagesList.append(message)
         self.view and self.view.Refresh()

   def onSaveListener(self, eventType, identifier):
      assert identifier == 'forum'
      if eventType == 'save initial data for student':
         messages = map(Message.toDict, self.messagesList)
         return {'messages': messages}

   def loadInitialData(self, data):
      assert len(self.messagesList) == 0, \
      "there's a message in the forum before initial data was received"

      # data =  same object returned from onSaveListener
      messages = map(Message.fromDict, data['messages'])
      self.messagesList.extend(messages)
      self.view and self.view.Refresh()


from django.shortcuts import render
from .forms import SubscriberForm

from products.models import *

def landing(request):
    name = "CodingMedved"
    current_day = "03.01.2017"
    form = SubscriberForm(request.POST or None)
    if request.method =='POST' and form.is_valid():

        data = form.cleaned_data
        new_form = form.save()

    return render(request, 'landing.html', locals())

def home(request):
    products_images = ProductImage.objects.filter(is_active=True, is_main=False, product__is_active=True)
    products_images_second = products_images.filter(product__category__id=1)
    products_images_third = products_images.filter(product__category__id=2)
    return render(request, 'home.html', locals())


from pygame import *
from math import *

init()
screen = display.set_mode((500, 500))

class Ball:
  def __init__(self, pos, vel=[0, 0], radius=30,
               color=Color(255, 0, 0), mass=1):
    self.pos = pos
    self.vel = vel
    self.radius = radius
    self.color = color
    self.mass = mass

  def move(self):
    for i in [0, 1]:
      self.pos[i] += self.vel[i]*dt

  def apply_gravity(self):
    self.vel[1] += gravity*dt

  def bump_from_wall(self, i, c, s):
    c -= s*self.radius
    if s*self.pos[i]>s*c:
      self.vel[i] *= (-1)
      self.pos[i] -= 2*(self.pos[i]-c)

  def predict_position(self, t):
    x = self.pos[0] + t*self.vel[0]
    y = self.pos[1] + t*self.vel[1] + 0.5*(t**2)*gravity
    return [x, y]

# calculate time of next collision,
# return None if no collision is found
def find_collision(b1, b2):
  p = [b1.pos[i]-b2.pos[i] for i in [0, 1]]
  v = [b1.vel[i]-b2.vel[i] for i in [0, 1]]
  r = b1.radius + b2.radius
  a = v[0]**2 + v[1]**2
  b = 2*(p[0]*v[0] + p[1]*v[1])
  c = p[0]**2 + p[1]**2 - r**2
  ts = solve_quadratic_equation(a, b, c)
  ts = [t for t in ts if t>0]
  if len(ts)>0:
    return min(ts)
  else:
    return None

# return list of solutions of a*x**2 + b*x + c = 0
def solve_quadratic_equation(a, b, c):
  d = b**2 - 4*a*c
  if d<0:
    return []
  elif d==0:
    return [-b/(2*a)]
  else:
    sd = sqrt(d)
    return [(-b+sd)/(2*a), (-b-sd)/(2*a)]

# (imprecise) collisions of balls
def handle_collision(b1, b2):
  bs = [b1, b2]
  d = [b2.pos[i]-b1.pos[i] for i in [0,1]]
  d_norm = sqrt(sum([c**2 for c in d]))
  if d_norm > b1.radius+b2.radius or d_norm < 0.001:
    return
  # normalized difference vector
  dn = [c/d_norm for c in d]
  # flip velocities outwards
  mass = sum([b.mass for b in bs])
  vel = [sum([b.vel[i]*b.mass for b in bs])/mass for i in [0, 1]]
  for b in [b1, b2]:
    s = sum([(b.vel[i]-vel[i])*dn[i] for i in [0, 1]])
    for i in [0, 1]:
      b.vel[i] -= 2*s*dn[i]
  # flip positions outward (preserving center of mass)
  overlap = b1.radius+b2.radius - d_norm
  for (b, sign) in [(b1, -1), (b2, 1)]:
    for i in [0, 1]:
      b.pos[i] += sign*dn[i]*overlap*b.mass/mass

balls = [ Ball([100, 200], vel=[5, 5], mass=4, radius=60)
        , Ball([100, 50], vel=[2.5, 0], color=Color(0, 255, 255))]
gravity = 0.2

dt = 1

quit = False
while not quit:
  for e in event.get():
    if e.type == QUIT:
      quit = True

  for b in balls:
    b.apply_gravity()
    b.move()
    b.bump_from_wall(1, 500, 1)
    b.bump_from_wall(1, 0, -1)
    b.bump_from_wall(0, 500, 1)
    b.bump_from_wall(0, 0, -1)
  handle_collision(balls[0], balls[1])

  screen.fill(Color(0, 0, 0))
  for b in balls:
    draw.circle(screen, b.color, [int(c) for c in b.pos], b.radius)
  collision = find_collision(balls[0], balls[1])
  if not collision==None:
    t = collision
    for b in balls[:2]:
      p = b.predict_position(t)
      draw.circle(screen, b.color, [int(c) for c in p], b.radius, 1)

  display.flip()
  if not key.get_pressed()[K_f]:
    time.wait(20)


# Run the HTTP monitor with parameters from parameters.cfg

from log_handler import LogHandler
import configparser

config = configparser.ConfigParser()
config.read("parameters.cfg")

logPath = str(config.get("Monitor", "logPath"))
refreshPeriod = float(config.get("Monitor", "refreshPeriod"))
treshold = float(config.get("Monitor", "treshold"))
monitorDuration = float(config.get("Monitor", "monitorDuration"))
logHandler = LogHandler(logPath, refreshPeriod, treshold, monitorDuration)
logHandler.start()
# Wait for the logHandler to finish to end the program
logHandler.join()