import json
import os
import random
import shutil
from predictor import get_predictor
from yolox.tracking_utils.timer import Timer
import cv2
import numpy as np

def get_gt_by_frame(bbox_file: str):
    gtByFrames = {}
    # convert to List[bboxes, List[int]]
    with open(bbox_file) as f:
        annot = json.load(f)
    labels = annot['labels']
    infos = annot['info']
    frames_map = [int(u.split('_')[-1].split('.')[0]) for u in infos['url']]
    for pIdx, player in enumerate(labels):
        frames = player['data']['frames']
        #group by frame
        for frame in frames:
            frame_idx = frames_map[frame['frame']]
            if frame_idx not in gtByFrames:
                gtByFrames[frame_idx] = [[], []]
            gtByFrames[frame_idx][0].append(frame['points'] + [1])
            gtByFrames[frame_idx][1].append(pIdx)
    for k, v in gtByFrames.items():
        gtByFrames[k] = (np.array(v[0]), np.array(v[1]))

    return gtByFrames

def ensure_folder(path: str):
    try:
        os.makedirs(path)
    except: pass

def remove_folder(path:str):
    try:
        shutil.rmtree(path)
    except: pass

if __name__ == '__main__':
    gt_labels = os.listdir('./input/bboxes')
    detector = get_predictor()
    OUTPUT_ROOT = './output'
    remove_folder(OUTPUT_ROOT)
    ensure_folder(OUTPUT_ROOT)
    splits = ['train', 'val', 'test']
    for split in splits:
        ensure_folder(OUTPUT_ROOT + f'/{split}/negative')
        ensure_folder(OUTPUT_ROOT + f'/{split}/positive')

    for gt_label_file in gt_labels:
        gt_bboxes = get_gt_by_frame(f'./input/bboxes/{gt_label_file}')
        video_id = gt_label_file.split('.')[0]
        print(video_id)

        if not os.path.exists(f'./input/images/{video_id}'):
            continue

        # extract positive patches
        for frame_idx, (player_bboxes, player_ids) in gt_bboxes.items():
            img = cv2.imread(f'./input/images/{video_id}/{frame_idx}.jpg')
            img_masked = img.copy()
            for bbox in player_bboxes:
                x1, y1, x2, y2, _ = bbox.astype(int)
                img_masked[y1:y2, x1:x2] = 0
            cv2.imwrite(OUTPUT_ROOT + '/masked.png', img_masked)
            outputs, img_info = detector.inference(img_masked[:, :, :3], Timer())

            output_results = outputs[0]
            if output_results is None: break
            imgH, imgW = img_info['height'], img_info['width']

            # human_bboxes = []
            output_results = output_results.cpu().numpy()
            scores = output_results[:, 4] * output_results[:, 5]
            human_bboxes = output_results[:, :4]  # x1y1x2y2
            remain_indx = scores > 0.6
            scores = scores[remain_indx]
            human_bboxes = human_bboxes[remain_indx]

            img_size = (800, 1440)
            scale = min(img_size[0] / float(imgH), img_size[1] / float(imgW))
            human_bboxes /= scale

            # negative samples
            negative_samples = [(bIdx, b) for bIdx, b in enumerate(human_bboxes) if b.min() > 0]
            negative_samples = random.sample(negative_samples, min(20, len(negative_samples)))
            random.shuffle(negative_samples)
            train_split_idx = int(len(negative_samples) * 0.7)
            val_split_idx = int(len(negative_samples) * 0.8)
            for idx, (bIdx, bbox) in enumerate(negative_samples):
                split = 'train' if idx < train_split_idx else ('val' if idx < val_split_idx else 'test')
                x1, y1, x2, y2 = bbox.astype(int)
                cv2.imwrite(f'{OUTPUT_ROOT}/{split}/negative/{video_id}_{frame_idx}_{bIdx}.png', img[y1:y2, x1:x2])

            positive_samples = list(zip(player_ids, player_bboxes))
            random.shuffle(positive_samples)
            train_split_idx = int(len(positive_samples) * 0.7)
            val_split_idx = int(len(positive_samples) * 0.8)
            for idx, (player_id, player_bboxe) in enumerate(positive_samples):
                split = 'train' if idx < train_split_idx else ('val' if idx < val_split_idx else 'test')
                x1, y1, x2, y2, _ = player_bboxe.astype(int)
                x1 = max(x1, 0)
                try:
                    cv2.imwrite(f'{OUTPUT_ROOT}/{split}/positive/{video_id}_{frame_idx}_{player_id}.png', img[y1:y2, x1:x2])
                except:
                    print(f'{OUTPUT_ROOT}/{split}/positive/{video_id}_{frame_idx}_{player_id}.png', x1, y1, x2, y2)


from aiogram.types import (
    InlineKeyboardMarkup,
    InlineKeyboardButton,
    ReplyKeyboardMarkup,
    KeyboardButton,
)

from main import admins_id
from utils.db_api.schemas.table_db import session, Contest

kb = ReplyKeyboardMarkup(resize_keyboard=True)
kb.add(KeyboardButton("Добавить конкурс")).add(KeyboardButton("Отмена"))

kb_auth = ReplyKeyboardMarkup(resize_keyboard=True)
kb_auth.add(KeyboardButton("Авторизация по телефону", request_contact=True))

def genmarkup(data: list) -> InlineKeyboardMarkup:
    markup = InlineKeyboardMarkup()
    markup.row_width = 1
    for i in data:
        markup.add(InlineKeyboardButton(i[0], callback_data=f"con_{i[0]}"))
    return markup

def to_pay(user_id: int, contest: str, skip: bool = False):
    markup = InlineKeyboardMarkup()
    markup.row_width = 1
    contest_query = session.query(Contest).filter(Contest.name == contest).first()
    if not (contest_query.winner):
        if str(user_id) in admins_id:
            markup.add(
                InlineKeyboardButton(
                    "Выбрать победителя и закончить конкурс",
                    callback_data=f"win_{contest}",
                )
            )
        else:
            if not skip:
                markup.add(
                    InlineKeyboardButton(
                        "Внести плату",
                        callback_data=f"pay_{contest}"
                    )
                )
    return markup


from list import student
import pickle
f=open("satya.db","wb")
rows=int(input("enter rows how many rows you want : "))
for i in range(rows):
    print("----------------------------------")
    print("enter "+str(i+1)+"student details")
    print("-----------------------------------")
    id=int(input("enter student number : "))
    name=input("enter student number :")
    s=student()
    s.studentdetails(id,name)
    pickle.dump(s,f)
    print(i+1,"student details saved")
    print("-----------------------")
    s.dispaly()
print("successfully complete")
f.close()


import math
import numpy as np
from sympy import*
import matplotlib.pyplot as plt

class Solver:
	def __init__(self, f, t0, y0, h, nsteps, inital_points):
		self.f = f
		self.t0 = t0
		self.y0 = y0
		self.h = h
		self.nsteps = nsteps
		self.inital_points = inital_points;
		self.coef_ab = [
			[1],
			[1],
			[3.0/2.0, 1.0/2.0],
			[23.0/12.0, -4.0/3.0, 5.0/12.0],
			[55.0/24.0, -59.0/24.0, 37.0/24.0, -3.0/8.0],
			[1901.0/720.0, -1387.0/360.0, 109.0/30.0, -637.0/360.0, 251.0/720.0],
			[4277.0/1440.0, -2641.0/480.0, 4991.0/720.0, -3649.0/720.0, 959.0/480.0, -95.0/288.0],
			[198721.0/60480.0, 18367.0/2520.0, 235183.0/20160.0, 10754.0/945.0, 135713.0/20160.0, 5603.0/2520.0, 19087.0/60480.0],
			[16083.0/4480.0, 1152169.0/120960.0, 242653.0/13440.0, 296053.0/13440.0, 2102243.0/120960.0, 115747.0/13440.0, 32863.0/13440.0, 5257.0/17280.0]
		]
		self.coef_am = [
			[1],
			[1.0/2.0, 1.0/2.0],
			[5.0/12.0, 2.0/3.0, -1.0/12.0],
			[3.0/8.0, 19.0/24.0, -5.0/24.0, 1.0/24.0],
			[251.0/720.0, 323.0/360.0, -11.0/30.0, 53.0/360.0, 19.0/720.0],
			[95.0/288.0, 1427.0/1440.0, -133.0/240.0, 241.0/720.0, -173.0/1440.0, 3.0/160.0],
			[19087.0/60480.0, 2713.0/2520.0, -15487.0/20160.0, 586.0/945.0, -6737.0/20160.0, 263.0/2520.0, -863.0/60480.0],
			[5257.0/17280.0, 139849.0/120960.0, -4511.0/4480.0, 123133.0/120960.0, -88547.0/120960.0, 1537.0/4480.0, -11351.0/120960.0, 275.0/24192.0]
		]
		self.coef_inv = [
			[1],
			[1, 1],
			[2.0/3.0, 4.0/3.0, -1.0/3.0],
			[6.0/11.0, 18.0/11.0, -9.0/11.0, 2.0/11.0],
			[12.0/25.0, 48.0/25.0, -36.0/25.0, 16.0/25.0, -3.0/25.0],
			[60.0/137.0, 300.0/137.0, -300.0/137.0, 200.0/137.0, -75.0/137.0, 12.0/137.0],
			[60.0/147.0, 360.0/147.0, -450.0/147.0, 400.0/147.0, -225.0/147.0, 72.0/147.0, -10.0/147.0]
		]

	def get_ab(self, ans, idx, order):
		value = ans[idx-1][1]
		for i in range(1, order+1):
			value += (self.h)*(self.coef_ab[order][i-1])*self.f(ans[idx-i][0], ans[idx-i][1])
		return value

	def euler(self):
		ans = []
		ans.append([self.t0, self.y0])
		t, y = self.t0, self.y0
		for i in range(1, self.nsteps+1):
			y = y + self.h*self.f(t, y)
			t = t + self.h
			ans.append([t, y])

		return ans

	def inverse_euler(self):
		ans = []
		ans.append([self.t0, self.y0])
		t, y = self.t0, self.y0
		for i in range(1, self.nsteps+1):
			k = y + self.h*self.f(t, y)
			y = y + self.h*self.f(t + self.h, k)
			t = t + self.h
			ans.append([t, y])

		return ans

	def improved_euler(self):
		ans = []
		ans.append([self.t0, self.y0])
		t, y = self.t0, self.y0
		for i in range(1, self.nsteps+1):
			k = y + self.h*self.f(t, y)
			y = y + 0.5*self.h*(self.f(t + self.h, k) + self.f(t, y))
			t = t + self.h
			ans.append([t, y])

		return ans

	def runge_kutta(self):
		ans = []
		t, y = self.t0, self.y0
		ans.append([t, y])
		for i in range(1, self.nsteps+1):
			k1 = f(t, y)
			k2 = f(t + 0.5*h, y + 0.5*h*k1)
			k3 = f(t + 0.5*h, y + 0.5*h*k2)
			k4 = f(t + h, y + h*k3)
			y = y + h*(k1 + 2*k2 + 2*k3 + k4)/6
			t = t + h
			ans.append([t, y])

		return ans

	def adam_bashforth_by_method(self, order, method):
		if method == 'euler':
			ans = self.euler()
		elif method == 'inverse euler':
			ans = self.inverse_euler()
		elif method == 'improved euler':
			ans = self.improved_euler()
		elif method == 'runge kutta':
			ans = self.runge_kutta()
		elif method == 'list':
			ans = self.inital_points

		h, f = self.h, self.f
		for i in range(order, self.nsteps+1):
			if len(ans) == i:
				ans.append([0, 0])

			ans[i][1] = self.get_ab(ans, i, order)
			ans[i][0] = ans[i-1][0] + h

		return ans

	def get_am(self, ans, idx, order):
		value = ans[idx-1][1]
		ans[idx][1] = self.get_ab(ans, idx, order)
		ans[idx][0] = ans[idx-1][0] + self.h
		for i in range(0, order+1):
			value += self.h*self.coef_am[order][i]*self.f(ans[idx-i][0], ans[idx-i][1])
		return value

	def adam_multon_by_method(self, order, method):
		if method == 'euler':
			ans = self.euler()
		elif method == 'inverse euler':
			ans = self.inverse_euler()
		elif method == 'improved euler':
			ans = self.improved_euler()
		elif method == 'runge kutta':
			ans = self.runge_kutta()
		elif method == 'list':
			ans = self.inital_points

		h, f = self.h, self.f
		for i in range(order, self.nsteps+1):
			if len(ans) == i:
				ans.append([0, 0])

			ans[i][1] = self.get_am(ans, i, order)
			ans[i][0] = ans[i-1][0] + h

		return ans

	def	get_inv(self, ans, idx, order):
		ans[idx][1] = self.get_ab(ans, idx, order)
		ans[idx][0] = ans[idx-1][0] + self.h
		value = self.coef_inv[order][0]*self.h*self.f(ans[idx][0], ans[idx][1])
		for i in range (1, order+1):
			value += self.coef_inv[order][i]*ans[idx-i][1]

		return value

	def backward_diff(self, order, method):
		if method == 'euler':
			ans = self.euler()
		elif method == 'inverse euler':
			ans = self.inverse_euler()
		elif method == 'improved euler':
			ans = self.improved_euler()
		elif method == 'runge kutta':
			ans = self.runge_kutta()
		elif method == 'list':
			ans = self.inital_points

		h, f = self.h, self.f
		for i in range(order, self.nsteps+1):
			if len(ans) == i:
				ans.append([0, 0])

			ans[i][1] = self.get_inv(ans, i, order)
			ans[i][0] = ans[i-1][0] + h

		return ans

#Main part of the code
#We wish to find an approximate solution to the equation dy/dt = f(t, y)

f = open("in.txt")
for line in f:
	entrada = line.split()
	method = entrada[0]
	ini_pts = []
	if method == 'adam_bashforth' or method == 'adam_multon' or method == 'formula_inversa':
		order = int(entrada[-1])
		expr = sympify(entrada[-2])
		t, y = symbols("t y")
		f = lambdify((t, y), expr, "numpy")
		nsteps = int(entrada[-3])
		h = float(entrada[-4])
		t0, y0 = float(entrada[-5]), 0
		for i in range(1, 1 + order):
			ini_pts.append([t0 + (i-1)*h, float(entrada[i])])
	else:
		y0, t0 = float(entrada[1]), float(entrada[2])
		h = float(entrada[3])
		nsteps = int(entrada[4])
		expr = sympify(entrada[5])
		t, y = symbols("t y")
		f = lambdify((t, y), expr, "numpy")

	solver = Solver(f, t0, y0, h, nsteps, ini_pts)
	pts = []
	if method == "euler":
		pts = solver.euler()
		print("Metodo de Euler")
	elif method == "euler_inverso":
		pts = solver.inverse_euler()
		print("Metodo de Euler Inverso")
	elif method == "euler_aprimorado":
		pts = solver.improved_euler()
		print("Metodo de Euler Aprimorado")
	elif method == "runge_kutta":
		pts = solver.runge_kutta()
		print("Metodo de Runge-Kutta")
	elif method == "adam_bashforth_by_euler":
		order = int(entrada[6])
		print("Metodo de Adam-Bashforth por Euler")
		pts = solver.adam_bashforth_by_method(order, 'euler')
	elif method == 'adam_bashforth_by_euler_inverso':
		order = int(entrada[6])
		print("Metodo de Adam-Bashforth por Euler Inverso")
		pts = solver.adam_bashforth_by_method(order, 'inverse euler')
	elif method == 'adam_bashforth_by_euler_aprimorado':
		order = int(entrada[6])
		print("Metodo de Adam-Bashforth por Euler Aprimorado")
		pts = solver.adam_bashforth_by_method(order, 'improved euler')
	elif method == 'adam_bashforth_by_runge_kutta':
		order = int(entrada[6])
		print("Metodo de Adam-Bashforth por Runge Kutta")
		pts = solver.adam_bashforth_by_method(order, 'runge kutta')
	elif method == 'adam_bashforth':
		print("Metodo de Adam-Bashforth")
		pts = solver.adam_bashforth_by_method(order, 'list')
	elif method == 'adam_multon':
		print("Metodo de Adam-Multon")
		pts = solver.adam_multon_by_method(order-1, 'list')
	elif method == 'adam_multon_by_euler':
		order = int(entrada[6])
		print("Metodo de Adam-Multon por Euler")
		pts = solver.adam_multon_by_method(order-1, 'euler')
	elif method == 'adam_multon_by_euler_inverso':
		order = int(entrada[6])
		print("Metodo de Adam-Multon por Euler Inverso")
		pts = solver.adam_multon_by_method(order-1, 'inverse euler')
	elif method == 'adam_multon_by_euler_aprimorado':
		order = int(entrada[6])
		print("Metodo de Adam-Multon por Euler Aprimorado")
		pts = solver.adam_multon_by_method(order-1, 'improved euler')
	elif method == 'adam_multon_by_runge_kutta':
		order = int(entrada[6])
		print("Metodo de Adam-Multon por Runge Kutta")
		pts = solver.adam_multon_by_method(order-1, 'runge kutta')
	elif method == 'formula_inversa':
		print("Metodo Formula Inversa de Diferenciacao")
		pts = solver.backward_diff(order-1, 'list')
	elif method == 'formula_inversa_by_euler':
		order = int(entrada[6])
		print("Metodo Formula Inversa de Diferenciacao por Euler")
		pts = solver.backward_diff(order-1, 'euler')
	elif method == 'formula_inversa_by_euler_inverso':
		order = int(entrada[6])
		print("Metodo Formula Inversa de Diferenciacao por Euler Inverso")
		pts = solver.backward_diff(order-1, 'inverse euler')
	elif method == 'formula_inversa_by_euler_aprimorado':
		order = int(entrada[6])
		print("Metodo Formula Inversa de Diferenciacao por Euler Aprimorado")
		pts = solver.backward_diff(order-1, 'improved euler')
	elif method == 'formula_inversa_by_runge_kutta':
		order = int(entrada[6])
		print("Metodo Formula Inversa de Diferenciacao por Runge Kutta")
		pts = solver.backward_diff(order-1, 'runge kutta')

	print("y(%.2f) = %.2f" %(pts[0][0], pts[0][1]))
	print("h = %.2f" %h)
	i = 0
	for [x, y] in pts:
		print("%d %.10lf" %(i, y))
		i += 1

#########################	ploting the solution	#############################
#######			comment the folowing lines to not plot solution 		   ######
	toplot = np.array(pts)
	plt.plot(toplot[:, 0], toplot[:, 1], ls = '-', color = 'black', linewidth = 1)
	plt.show()
#################################################################################

	print("\n")


import pytest

from backend.utils.assertions import assert_equals, assert_true
from backend.utils.helper import Helper
from front.data_for_tests.calender_data_for_tests import DataForTests
from front.pages.page import CalendarPage, CalendarConfiguration

@pytest.mark.usefixtures("setup", "test_config")
class TestCalender:

    @pytest.fixture(scope="class")
    def calendar_page(self, setup, test_config):
        driver = setup
        calendar_page = CalendarPage(driver)
        calendar_page.open_url(Helper.get_config_value_by_name(test_config, ["calender", "url"]))
        return calendar_page

    @pytest.fixture(scope="class")
    def switch_to_infinite_scroll_and_month_view(self, setup, calendar_page):
        driver = setup
        calendar_config = CalendarConfiguration(driver)
        calendar_config.enable_infinite_scroll()
        calendar_page.switch_view("Month")

    @pytest.mark.parametrize("test_data", DataForTests.switch_to_infinite_scroll_and_month_view())
    def test_switch_to_infinite_scroll_and_month_view(self, setup, test_data, calendar_page,
                                                      switch_to_infinite_scroll_and_month_view):
        driver = setup
        calendar_config = CalendarConfiguration(driver)
        calendar_configuration = calendar_config.get_calendar_configuration()
        assert_equals(calendar_configuration, test_data["configuration"])
        calendar_page.verify_requested_view_checked(test_data["view_type"])

    def test_create_events_check_element_count_increased(self, calendar_page,
                                                         switch_to_infinite_scroll_and_month_view):
        event_resource_id = calendar_page.create_event()
        assert_true(calendar_page.verify_event_was_created(event_resource_id),
                    "Error: After creating an event, Event not found!")
        event_resource_id = calendar_page.create_event()
        assert_true(calendar_page.verify_event_was_created(event_resource_id),
                    "Error: After creating an event, Event not found!")

    def test_create_event_and_go_ahead_one_month_and_check_dom_decrease(self, setup, calendar_page,
                                                                        switch_to_infinite_scroll_and_month_view):
        event_resource_id = calendar_page.create_event()
        calendar_page.navigation_forward(1)
        assert_true(not calendar_page.verify_event_was_created(event_resource_id),
                    "Error: After creating an event and going one month forward, Event found!")

    @pytest.mark.skip("This test have bug, so it will failed")
    def test_create_event_change_month_and_check_event_still_exist(self, setup, calendar_page,
                                                                   switch_to_infinite_scroll_and_month_view):
        event_resource_id = calendar_page.create_event()
        calendar_page.navigation_forward(1)
        assert_true(not calendar_page.verify_event_was_created(event_resource_id),
                    "Error: After creating an event and going one month forward, Event found!")
        calendar_page.navigation_backward(1)
        assert_true(calendar_page.verify_event_was_created(event_resource_id),
                    "Error: After creating an event, change month and go back.Event not found!")


import numpy as np
import random
import copy
# utils
from extra.utils import trans_vector, get_cards_small_extend, calculate_score

class RunfastGameEnv():

    def __init__(self, cards=[], position=0, next_player=0, pattern=0):
        self.position = position
        self.next_player = next_player
        self.cards = cards
        self.pattern = pattern
        self.set_dict()
        self.cards_used = np.zeros(13)
        self.boom_success = 0
        self.status = np.array([0])
        self.current_pattern = 0

    def get_state(self):
        original_vec = np.array([4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 1])
        cards_used = self.cards_used
        cards_inhand = self.cards
        cards_inhand_small = trans_vector(cards_inhand)
        next_player = self.get_next_player()
        next_next_player = next_player.get_next_player()
        next_cards_used = next_player.cards_used
        next_next_cards_used = next_next_player.cards_used
        status = self.status
        cards_left = original_vec - cards_used - next_cards_used - next_next_cards_used
        state = np.concatenate((cards_inhand_small, cards_used, next_cards_used,
                                next_next_cards_used, cards_left, status), axis=0)
        return state

    def set_dict(self):
        number2card = {}
        card2number = {}
        j = 0
        for i in range(3, 11):
            number2card.update({j: "♠" + str(i)})
            card2number.update({"♠" + str(i): j})
            j = j + 1
            number2card.update({j: "♥" + str(i)})
            card2number.update({"♠" + str(i): j})
            j = j + 1
            number2card.update({j: "♣" + str(i)})
            card2number.update({"♠" + str(i): j})
            j = j + 1
            number2card.update({j: "♦" + str(i)})
            card2number.update({"♠" + str(i): j})
            j = j + 1

        number2card.update({j: "♠J"})
        card2number.update({"♠J": j})
        j = j + 1
        number2card.update({j: "♥J"})
        card2number.update({"♥J": j})
        j = j + 1
        number2card.update({j: "♣J"})
        card2number.update({"♣J": j})
        j = j + 1
        number2card.update({j: "♦J"})
        card2number.update({"♦J": j})
        j = j + 1

        number2card.update({j: "♠Q"})
        card2number.update({"♠Q": j})
        j = j + 1
        number2card.update({j: "♥Q"})
        card2number.update({"♥Q": j})
        j = j + 1
        number2card.update({j: "♣Q"})
        card2number.update({"♣Q": j})
        j = j + 1
        number2card.update({j: "♦Q"})
        card2number.update({"♦Q": j})
        j = j + 1

        number2card.update({j: "♠K"})
        card2number.update({"♠K": j})
        j = j + 1
        number2card.update({j: "♥K"})
        card2number.update({"♥K": j})
        j = j + 1
        number2card.update({j: "♣K"})
        card2number.update({"♣K": j})
        j = j + 1
        number2card.update({j: "♦K"})
        card2number.update({"♦K": j})
        j = j + 1

        number2card.update({j: "♠A"})
        card2number.update({"♠A": j})
        j = j + 1
        number2card.update({j: "♥A"})
        card2number.update({"♥A": j})
        j = j + 1
        number2card.update({j: "♣A"})
        card2number.update({"♣A": j})
        j = j + 1
        number2card.update({j: "♦2"})
        card2number.update({"♦2": j})
        self.number2card = number2card
        self.card2number = card2number

    def cards2vec(self):
        card_vec = [0] * 13
        for i in self.cards:
            if i < 47:
                index = int(i / 4)
                card_vec[index] += 1
            else:
                card_vec[12] = 1
        return card_vec

    def set_cards(self, cards):
        self.cards = cards

    def get_cards(self):
        return self.cards

    def get_next_player(self):
        return self.next_player

    def get_position(self):
        return self.position

    def show_cards(self):
        print(self.position + "'s cards:")
        cards = []
        for i in self.cards:
            cards.append(self.number2card[i])
        print(cards)

    def update_position(self, new_position):
        self.position = new_position

    def update_next(self, new_next):
        self.next_player = new_next

    def check_long(self, lenth):
        cards_vec = self.cards2vec()
        current_len = 0
        for i in range(len(cards_vec) - lenth):
            current_len = 0
            for j in range(lenth):
                if cards_vec[i + j] >= 1:
                    current_len += 1
                    if current_len == lenth:
                        break
                else:
                    current_len = 0
                    break
            if current_len == lenth:
                return True
        return False

    # 检查连对子、飞机等情况
    def check_plane(self, lenth, width):
        cards_vec = self.cards2vec()
        current_len = 0
        for i in range(len(cards_vec) - lenth):
            current_len = 0
            for j in range(lenth):
                if cards_vec[i + j] >= width:
                    current_len += 1
                    if current_len == lenth:
                        break
                else:
                    current_len = 0
                    break
            if current_len == lenth:
                return True
        return False

    def search_pattern(self):
        pattern_to_playcards = []
        # pattern 0 代表出单牌
        pattern_to_playcards.append(0)
        # pattern 1 代表出对子

        cards_vec = self.cards2vec()

        for i in cards_vec:
            if i >= 2:
                pattern_to_playcards.append(1)
                break

        # pattern 2或32 代表出三张
        for i in cards_vec:
            if i >= 3:
                # 如果不想让三带二先出，则注释下面这个添加32的操作
                # pattern_to_playcards.append(32)
                pattern_to_playcards.append(2)
                break

        # pattern 3 代表炸弹
        for i in cards_vec:
            if i == 4:
                pattern_to_playcards.append(3)
                break

        # pattern 4 代表五张牌的顺子
        lenth = 5
        if self.check_long(lenth):
            pattern_to_playcards.append(4)

        # pattern 5代表6张牌的顺子
        lenth = 6
        if self.check_long(lenth):
            pattern_to_playcards.append(5)

        # pattern 6 代表7张牌的顺子
        lenth = 7
        if self.check_long(lenth):
            pattern_to_playcards.append(6)

        # pattern 7 代表8张牌的顺子
        lenth = 8
        if self.check_long(lenth):
            pattern_to_playcards.append(7)

        # pattern 8 代表9张牌的顺子
        lenth = 9
        if self.check_long(lenth):
            pattern_to_playcards.append(8)

        # pattern 9 代表10张牌的顺子
        lenth = 10
        if self.check_long(lenth):
            pattern_to_playcards.append(9)

        # pattern 10 代11张牌的顺子
        lenth = 11
        if self.check_long(lenth):
            pattern_to_playcards.append(10)

        # pattern 11 代表12张牌的顺子
        lenth = 12
        if self.check_long(lenth):
            pattern_to_playcards.append(11)

        # pattern 12 代表2连对
        lenth = 2
        width = 2
        if self.check_plane(lenth, width):
            pattern_to_playcards.append(12)

        # pattern 13 代表3连对
        lenth = 3
        width = 2
        if self.check_plane(lenth, width):
            pattern_to_playcards.append(13)

        # pattern 14 代表4连对
        lenth = 4
        width = 2
        if self.check_plane(lenth, width):
            pattern_to_playcards.append(14)

        # pattern 15 代表5连对
        lenth = 5
        width = 2
        if self.check_plane(lenth, width):
            pattern_to_playcards.append(15)

        # pattern 16 代表6连对
        lenth = 6
        width = 2
        if self.check_plane(lenth, width):
            pattern_to_playcards.append(16)

        # pattern 17 代表7连对
        lenth = 7
        width = 2
        if self.check_plane(lenth, width):
            pattern_to_playcards.append(17)

        # pattern 18 代表8连对
        lenth = 8
        width = 2
        if self.check_plane(lenth, width):
            pattern_to_playcards.append(18)

        # pattern 19 代表二连飞机
        lenth = 2
        width = 3
        if self.check_plane(lenth, width):
            pattern_to_playcards.append(19)

        # pattern 20 代表三连飞机
        lenth = 3
        width = 3
        if self.check_plane(lenth, width):
            pattern_to_playcards.append(20)

        # pattern 21 代表四连飞机
        lenth = 4
        width = 3
        if self.check_plane(lenth, width):
            pattern_to_playcards.append(21)

        return pattern_to_playcards

    # 得到连对和飞机等出法
    def get_air_solution(self, the_lenth=2, the_width=2, biggest=[-1, -1, -1, -1, -1]):
        lenth = the_lenth
        width = the_width
        current_len = 0
        cash_card = []
        cards_vec = self.cards2vec()
        way_to_playcards = []
        if biggest[0] == -1:
            for i in range(len(cards_vec) - lenth):
                current_len = 0
                for j in range(lenth):
                    if cards_vec[i + j] >= width:
                        current_len += 1
                        if current_len == lenth:
                            break
                    else:
                        current_len = 0
                        break
                if current_len == lenth:
                    base = i
                    count = 0
                    cash_card = []
                    for element in self.cards:
                        if int(element / 4) == base:
                            cash_card.append(element)
                            count += 1
                            if count == width:
                                base += 1
                                count = 0

                            if len(cash_card) == lenth * width:
                                way_to_playcards.append(cash_card)
                                break
        else:
            base_line = int(biggest[0] / 4) + 1
            if base_line >= len(cards_vec) - lenth:
                pass
            else:
                for i in range(base_line, len(cards_vec) - lenth):
                    current_len = 0
                    for j in range(lenth):
                        if cards_vec[i + j] >= width:
                            current_len += 1
                            if current_len == lenth:
                                break
                        else:
                            current_len = 0
                            break
                    if current_len == lenth:
                        base = i
                        cash_card = []
                        count = 0
                        for element in self.cards:
                            if int(element / 4) == base:
                                cash_card.append(element)
                                count += 1
                                if count == width:
                                    base += 1
                                    count = 0
                                if len(cash_card) == lenth * width:
                                    way_to_playcards.append(cash_card)
                                    break
        if width == 2:
            return way_to_playcards
        elif width == 3:
            if len(way_to_playcards) > 0:
                if biggest[0] == -1:
                    if len(self.cards) <= 5 * lenth:
                        choice_cards = list(set(self.cards) - set(way_to_playcards[0]))
                        if len(choice_cards) > 0:
                            choice_cards.sort(reverse=False)
                            for element in choice_cards:
                                way_to_playcards[0].append(element)
                            return way_to_playcards[0]
                        else:
                            return way_to_playcards[0]
                    else:
                        return_result = []
                        for index in range(len(way_to_playcards)):
                            choice_cards = list(set(self.cards) - set(way_to_playcards[index]))

                            for i_i in range(10):
                                appendage = random.sample(choice_cards, lenth * 2)
                                appendage.sort(reverse=False)
                                a = copy.deepcopy(way_to_playcards[index])
                                for element in appendage:
                                    a.append(element)
                                return_result.append(a)
                        return return_result
                else:
                    if len(self.cards) < 5 * lenth:
                        return []
                    elif len(self.cards) == 5 * lenth:
                        choice_cards = list(set(self.cards) - set(way_to_playcards[0]))
                        if len(choice_cards) > 0:
                            choice_cards.sort(reverse=False)
                            for element in choice_cards:
                                way_to_playcards[0].append(element)
                            return way_to_playcards[0]
                    else:
                        return_result = []
                        for index in range(len(way_to_playcards)):
                            choice_cards = list(set(self.cards) - set(way_to_playcards[index]))

                            for i_i in range(10):
                                appendage = random.sample(choice_cards, lenth * 2)
                                appendage.sort(reverse=False)
                                a = copy.deepcopy(way_to_playcards[index])
                                for element in appendage:
                                    a.append(element)
                                return_result.append(a)
                        return return_result
            else:
                return []

    # 得到顺子的各种出法
    def get_solution(self, the_lenth=5, biggest=[-1, -1, -1, -1, -1]):
        lenth = the_lenth
        current_len = 0
        cash_card = []
        cards_vec = self.cards2vec()
        way_to_playcards = []
        if biggest[0] == -1:
            for i in range(len(cards_vec) - lenth):
                current_len = 0
                for j in range(lenth):
                    if cards_vec[i + j] >= 1:
                        current_len += 1
                        if current_len == lenth:
                            break
                    else:
                        current_len = 0
                        break
                if current_len == lenth:
                    base = i
                    cash_card = []
                    for element in self.cards:
                        if int(element / 4) == base:
                            cash_card.append(element)
                            base += 1
                            if len(cash_card) == lenth:
                                way_to_playcards.append(cash_card)
                                break
        else:
            base_line = int(biggest[0] / 4) + 1
            if base_line >= len(cards_vec) - lenth:
                pass
            else:
                for i in range(base_line, len(cards_vec) - lenth):
                    current_len = 0
                    for j in range(lenth):
                        if cards_vec[i + j] >= 1:
                            current_len += 1
                            if current_len == lenth:
                                break
                        else:
                            current_len = 0
                            break
                    if current_len == lenth:
                        base = i
                        cash_card = []
                        for element in self.cards:
                            if int(element / 4) == base:
                                cash_card.append(element)
                                base += 1
                                if len(cash_card) == lenth:
                                    way_to_playcards.append(cash_card)
                                    break
        return way_to_playcards

    def update_pattern(self, new_pattern):
        self.pattern = new_pattern

    def search_play_methods(self, pattern=0, biggest=[-1, -1, -1, -1, -1]):
        way_to_playcards = []
        # 出单牌
        if pattern == 0:
            if len(self.get_next_player().get_cards()) == 1:
                big = biggest[0]
                if big == -1:
                    base = self.cards[0]
                elif big > 43 and big < 47:
                    base = 47
                else:
                    base = (int(big / 4) + 1) * 4
                if self.cards[-1] >= base:
                    way_to_playcards.append([self.cards[-1]])
            else:
                big = biggest[0]
                all_cards = self.cards
                if big == -1:
                    base = self.cards[0]
                elif big > 43 and big < 47:
                    base = 47
                else:
                    base = (int(big / 4) + 1) * 4
                for i in all_cards:
                    list_a = []
                    if i >= base:
                        list_a.append(i)
                        way_to_playcards.append(list_a)
                        if base > 43 and base < 47:
                            base = 47
                        else:
                            base = (int(base / 4) + 1) * 4

        elif pattern == 1:
            if biggest[0] == -1:
                base = 0
            else:
                base = int(biggest[0] / 4) + 1
            if len(self.cards) <= 1:
                pass
            else:
                for i in range(len(self.cards) - 1):
                    if int(self.cards[i] / 4) >= base and int(self.cards[i] / 4) == int(self.cards[i + 1] / 4) and \
                            self.cards[i + 1] != 47:
                        way_to_playcards.append([self.cards[i], self.cards[i + 1]])
                        base = int(self.cards[i] / 4) + 1

        # 出三张模式最高,之前是2，改为了32
        elif pattern == 2:
            # elif pattern==32 or pattern==2:
            if biggest[0] == -1:
                base = 0
            else:
                base = int(biggest[0] / 4) + 1
            if len(self.cards) <= 2:
                pass
            elif biggest[0] == -1 and len(self.cards) >= 3:
                for i in range(len(self.cards) - 2):
                    if int(self.cards[i] / 4) >= base and int(self.cards[i] / 4) == int(self.cards[i + 2] / 4) and \
                            self.cards[i + 2] != 47:
                        base_list = [self.cards[i], self.cards[i + 1], self.cards[i + 2]]
                        # choice_cards = list(set(self.cards) - set(base_list))
                        if len(self.cards) == 3:
                            way_to_playcards.append([self.cards[i], self.cards[i + 1], self.cards[i + 2]])
                        elif len(self.cards) == 4:
                            choice_cards = list(set(self.cards) - set(base_list))
                            way_to_playcards.append(
                                [self.cards[i], self.cards[i + 1], self.cards[i + 2], choice_cards[0]])
                        elif len(self.cards) == 5:
                            choice_cards = list(set(self.cards) - set(base_list))
                            if choice_cards[0] < choice_cards[1]:
                                way_to_playcards.append(
                                    [self.cards[i], self.cards[i + 1], self.cards[i + 2], choice_cards[0],
                                     choice_cards[1]])
                            else:
                                way_to_playcards.append(
                                    [self.cards[i], self.cards[i + 1], self.cards[i + 2], choice_cards[1],
                                     choice_cards[0]])
                        else:
                            choice_cards = list(set(self.cards) - set(base_list))

                            for i_i in range(10):
                                random.shuffle(choice_cards)
                                if choice_cards[0] < choice_cards[1]:
                                    way_to_playcards.append(
                                        [self.cards[i], self.cards[i + 1], self.cards[i + 2], choice_cards[0],
                                         choice_cards[1]])
                                else:
                                    way_to_playcards.append(
                                        [self.cards[i], self.cards[i + 1], self.cards[i + 2], choice_cards[1],
                                         choice_cards[0]])
                            # way_to_playcards.append([self.cards[i], self.cards[i+1], self.cards[i+2] ])
                            base = int(self.cards[i] / 4) + 1
            elif biggest[0] != -1 and len(self.cards) < 5:
                pass

            elif biggest[0] != -1 and len(self.cards) >= 5:
                base = int(biggest[0] / 4) + 1
                for i in range(len(self.cards) - 2):
                    if int(self.cards[i] / 4) >= base and int(self.cards[i] / 4) == int(self.cards[i + 2] / 4) and \
                            self.cards[i + 2] != 47:
                        base_list = [self.cards[i], self.cards[i + 1], self.cards[i + 2]]
                        choice_cards = list(set(self.cards) - set(base_list))

                        for i_i in range(10):
                            random.shuffle(choice_cards)
                            if choice_cards[0] < choice_cards[1]:
                                way_to_playcards.append(
                                    [self.cards[i], self.cards[i + 1], self.cards[i + 2], choice_cards[0],
                                     choice_cards[1]])
                            else:
                                way_to_playcards.append(
                                    [self.cards[i], self.cards[i + 1], self.cards[i + 2], choice_cards[1],
                                     choice_cards[0]])
                            if len(self.cards) == 5:
                                break
                        # way_to_playcards.append([self.cards[i], self.cards[i+1], self.cards[i+2] ])
                        base = int(self.cards[i] / 4) + 1

        # 是不是四张牌
        elif pattern == 3:
            # 是不是炸弹
            if biggest[0] == -1:
                Cards2vec = self.cards2vec()
                for i in range(len(Cards2vec)):
                    if Cards2vec[i] == 4:
                        a = [i * 4 + j for j in range(4)]
                        way_to_playcards.append(a)
            else:
                base = int(biggest[0] / 4)
                Cards2vec = self.cards2vec()
                for i in range(len(Cards2vec)):
                    if i > base and i < 11:
                        if Cards2vec[i] == 4:
                            a = [i * 4 + j for j in range(4)]
                            way_to_playcards.append(a)
        # 是不是顺子
        elif pattern == 4:
            lenth = 5
            result = self.get_solution(lenth, biggest)
            if len(result) > 0:
                for element in result:
                    way_to_playcards.append(element)
        # 六张牌的顺子
        elif pattern == 5:
            lenth = 6
            result = self.get_solution(lenth, biggest)
            if len(result) > 0:
                for element in result:
                    way_to_playcards.append(element)
        # 7张牌的顺子
        elif pattern == 6:
            lenth = 7
            result = self.get_solution(lenth, biggest)
            if len(result) > 0:
                for element in result:
                    way_to_playcards.append(element)
        # 8张牌的顺子
        elif pattern == 7:
            lenth = 8
            result = self.get_solution(lenth, biggest)
            if len(result) > 0:
                for element in result:
                    way_to_playcards.append(element)
        # 9张牌的顺子
        elif pattern == 8:
            lenth = 9
            result = self.get_solution(lenth, biggest)
            if len(result) > 0:
                for element in result:
                    way_to_playcards.append(element)
        # 10张牌的顺子
        elif pattern == 9:
            lenth = 10
            result = self.get_solution(lenth, biggest)
            if len(result) > 0:
                for element in result:
                    way_to_playcards.append(element)
        # 11张牌的顺子
        elif pattern == 10:
            lenth = 11
            result = self.get_solution(lenth, biggest)
            if len(result) > 0:
                for element in result:
                    way_to_playcards.append(element)
        # 12张牌的顺子
        elif pattern == 11:
            lenth = 12
            result = self.get_solution(lenth, biggest)
            if len(result) > 0:
                for element in result:
                    way_to_playcards.append(element)

        # 如果出的是连对
        elif pattern > 11 and pattern < 19:
            the_width = 2
            if biggest[0] == -1:
                the_lenth = pattern - 10
            else:
                the_lenth = int(len(biggest) / the_width)
            result = self.get_air_solution(the_lenth, the_width, biggest)
            if len(result) > 0:
                for element in result:
                    way_to_playcards.append(element)

        elif pattern >= 19:
            the_width = 3
            if biggest[0] == -1:
                the_lenth = pattern - 17
            else:
                the_lenth = int(len(biggest) / the_width)
            result = self.get_air_solution(the_lenth, the_width, biggest)
            if len(result) > 0:
                for element in result:
                    way_to_playcards.append(element)

        if pattern != 3 and biggest[0] != -1:
            Cards2vec = self.cards2vec()
            for i in range(len(Cards2vec)):
                if Cards2vec[i] == 4:
                    a = [i * 4 + j for j in range(4)]
                    way_to_playcards.append(a)

        return way_to_playcards

    def play_cards(self, cards_toplay, test=False):
        if len(cards_toplay) == 0:
            if test:
                print(self.position + '要不起！')
            return False
        cards_toplay_zu = ''
        for i in cards_toplay:
            cards_toplay_zu = cards_toplay_zu + self.number2card[i] + ','
        if test:
            print(self.position + ' 出' + cards_toplay_zu)
        self.cards = list(set(self.cards) - set(cards_toplay))
        if len(self.cards) == 0:
            if test:
                print(self.position + " wins.")
            return True
        self.cards.sort(reverse=False)
        return False


# Importar Librerias
import pandas as pd
import json

# Opening JSON file
f = open('orderbooks_05jul21.json')
print(f)

# Returns JSON object as a dictionary
orderbooks_data = json.load(f)
ob_data = orderbooks_data['bitfinex']

# Drop Keys with none values
ob_data = {i_key: i_value for i_key,i_value in ob_data.items() if i_value is not None}

# Convert to DataFrame and rearange columns
ob_data = {i_ob: pd.DataFrame(ob_data[i_ob])[['bid_size', 'bid', 'ask', 'ask_size']]
          if ob_data[i_ob] is not None else None for i_ob in list(ob_data.keys())}


#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the fibonacciModified function below.
def fibonacciModified(t1, t2, n):
    seq=[t1,t2]
    if n<=2:
        print(seq[n-1])
    else:
        for i in range(n-2):
            seq.append(seq[-2]+seq[-1]**2)
        return seq[-1]
    return

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t1T2n = input().split()

    t1 = int(t1T2n[0])

    t2 = int(t1T2n[1])

    n = int(t1T2n[2])

    result = fibonacciModified(t1, t2, n)

    fptr.write(str(result) + '\n')

    fptr.close()


# -*- coding: utf-8 -*-
import time

import click
from click.testing import CliRunner
from dagster_aws.cli.term import Spinner, Term

def test_term():
    def term_helper(term_cmd, prefix, exit_code=0):
        @click.command()
        def fn():
            term_cmd('foo bar')

        runner = CliRunner()
        result = runner.invoke(fn)
        assert result.exit_code == exit_code
        assert result.output == prefix + u'foo bar\n'

    expected = [
        (Term.error, Term.ERROR_PREFIX),
        (Term.info, Term.INFO_PREFIX),
        (Term.success, Term.SUCCESS_PREFIX),
        (Term.waiting, Term.WAITING_PREFIX),
        (Term.warning, Term.WARNING_PREFIX),
    ]
    for term_cmd, prefix in expected:
        term_helper(term_cmd, prefix)

    term_helper(Term.fatal, Term.FATAL_PREFIX, exit_code=1)

def test_spinner(capsys):
    with Spinner():
        time.sleep(0.5)

    captured = capsys.readouterr()
    assert captured.out.encode('unicode-escape').startswith(
        b'\\u280b\\x08\\u2819\\x08\\u2839\\x08\\u2838\\x08'
    )


import nltk
from functools import lru_cache
from nltk.corpus import stopwords
from nltk.stem.snowball import EnglishStemmer
import re
from bs4 import BeautifulSoup

class Preprocessor:
    def __init__(self):
        # Stemming is the most time-consuming part of the indexing process, we attach a lru_cache to the stemmer
        # which will store upto 100000 stemmed forms and reuse them when possible instead of applying the
        # stemming algorithm.
        self.stem = lru_cache(maxsize=100000)(EnglishStemmer().stem)
        self.tokenize = nltk.tokenize.WhitespaceTokenizer().tokenize

    def __call__(self, text):
        text = re.sub(r'[\.\?\!\,\:\;\"]', ' ', text)
        # text = re.sub('[-]', ' ', text)
        text = re.sub(r'<.?p>', '', text)
        # text = BeautifulSoup(text, "lxml").text
        tokens = nltk.tokenize.word_tokenize(text)
        tokens = [token.lower() for token in tokens if
                  token.isalpha()]  # removing punctuations from tokens and converting to lower case
        stop_words = stopwords.words('english')
        tokens = [token for token in tokens if not token in stop_words]
        tokens = [self.stem(token) for token in tokens]
        return tokens


import logging

import cabby

from events.stix import parse_stix_package, STIXPackage

def collect_indicator_packages(configuration: dict) -> STIXPackage:
    for repository in configuration['repositories']:
        yield from poll_repository(repository)

def poll_repository(repository: dict) -> list:
    logging.debug("Connecting to %s", repository['name'])

    client = cabby.create_client(**repository['client'])
    collections = (c for c in client.get_collections()
                   if c.name not in repository.get('exclusions', ()))

    for collection in collections:
        yield from poll_collection(client, collection.name)

    logging.info("Repository %s exhausted", repository['name'])

def poll_collection(client: cabby.Client11, collection_name: str) -> list:
    packages = 0
    indicators = 0

    logging.debug("Polling from collection %s", collection_name)

    for block in client.poll(collection_name=collection_name):
        package = parse_stix_package(block.content)

        if package is not None:
            packages += 1
            indicators += len(package.indicators)

            yield package

    logging.info("Collection %s: Packages %d - IOCs %d",
                 collection_name, packages, indicators)


# -*- coding: utf-8 -*-
"""
Created on Tue Sep  7 17:46:55 2021

@author: Administrator
"""
import SimpleITK as sitk
import numpy as np
import os
import cv2
from shutil import copyfile
import random

num=379
lists=['train_002_0000.nii.gz','train_019_0000.nii.gz','train_069_0000.nii.gz','train_101_0000.nii.gz','train_114_0000.nii.gz','train_127_0000.nii.gz','train_150_0000.nii.gz','train_134_0000.nii.gz','train_174_0000.nii.gz','train_195_0000.nii.gz']
for name in lists:
    num=num+1
    copyfile(os.path.join(r'.\data\Raw\TrainingImg',name),os.path.join(r'.\data\Raw\TrainingImg','train_'+str(num)+'_0000.nii.gz'))
    copyfile(os.path.join(r'.\data\Raw\TrainingMask',name.replace('_0000','')),os.path.join(r'.\data\Raw\TrainingMask','train_'+str(num)+'.nii.gz'))
    num=num+1
    img=sitk.ReadImage(os.path.join(r'.\data\Raw\TrainingImg',name))
    mask=sitk.ReadImage(os.path.join(r'.\data\Raw\TrainingMask',name.replace('_0000','')))
    imgarr=sitk.GetArrayFromImage(img)
    maskarr=sitk.GetArrayFromImage(mask)
    imgarr1=imgarr.copy()
    imgarr1[maskarr!=2]=0
    imgarr2=imgarr1.copy()
    imgarr2[imgarr2>50]=0
    for i in range(imgarr2.shape[0]):
        for j in range(imgarr2.shape[1]):
            for k in range(imgarr2.shape[2]):
                if imgarr2[i,j,k]!=0:
                    imgarr2[i,j,k]=imgarr2[i,j,k]+random.randint(100,150)
    imgarr1[imgarr1<=50]=0
    imgarr1=imgarr1+imgarr2
    imgarr[maskarr==2]=0
    imgarr=imgarr+imgarr1
    saveimg=sitk.GetImageFromArray(imgarr)
    saveimg.SetSpacing(img.GetSpacing())
    saveimg.SetDirection(img.GetDirection())
    saveimg.SetOrigin(img.GetOrigin())
    sitk.WriteImage(saveimg,os.path.join(r'.\data\Raw\TrainingImg','train_'+str(num)+'_0000.nii.gz'))
    sitk.WriteImage(mask,os.path.join(r'.\data\Raw\TrainingMask','train_'+str(num)+'.nii.gz'))


#Find list of all sub_breed breed name
import requests

def get_json_dog_output_dict(url):
    r = requests.get(url)
    output = r.json()
    return output

def get_breed_sub_breed_full_name():
    dog_output = get_json_dog_output_dict(url = "https://dog.ceo/api/breeds/list/all")
    dog_breed_output = dog_output["message"]

    dog_full_name = dog_breed_output.items()
    for breed, sub_breeds in dog_full_name:
        # print(f"breed:{breed}; type:{sub_breed}")
        # print(f"{sub_breed} {breed}")
        if sub_breeds:
            for sub_breed in sub_breeds:
                print(f"{sub_breed}-{breed}")
        else:
            print(f"{breed}")

if __name__ == "__main__":
   output = get_json_dog_output_dict(url = "https://dog.ceo/api/breeds/list/all")
   dog_keys_fullname = get_breed_sub_breed_full_name()
   #print(dog_keys_fullname)


from typing import Dict, List, Optional, Tuple

import numpy as np
import torch
from torch import Tensor
import torch.nn as nn
import torch.nn.functional as F
from mmcv.utils import ConfigDict
from mmdet.core import bbox2roi
from mmdet.models.builder import HEADS
from mmfewshot.detection.models.roi_heads.meta_rcnn_roi_head import MetaRCNNRoIHead

class VAE(nn.Module):

    def __init__(self,
                 in_channels: int,
                 latent_dim: int,
                 hidden_dim: int) -> None:
        super(VAE, self).__init__()

        self.latent_dim = latent_dim

        self.encoder = nn.Sequential(
            nn.Linear(in_channels, hidden_dim),
            nn.BatchNorm1d(hidden_dim),
            nn.LeakyReLU()
        )
        self.fc_mu = nn.Linear(hidden_dim, latent_dim)
        self.fc_var = nn.Linear(hidden_dim, latent_dim)

        self.decoder_input = nn.Linear(latent_dim, hidden_dim)

        self.decoder = nn.Sequential(
            nn.Linear(hidden_dim, in_channels),
            nn.BatchNorm1d(in_channels),
            nn.Sigmoid()
        )

    def encode(self, input: Tensor) -> List[Tensor]:
        result = self.encoder(input)

        mu = self.fc_mu(result)
        log_var = self.fc_var(result)

        return [mu, log_var]

    def decode(self, z: Tensor) -> Tensor:

        z = self.decoder_input(z)
        z_out = self.decoder(z)
        return z_out

    def reparameterize(self, mu: Tensor, logvar: Tensor) -> Tensor:
        std = torch.exp(0.5 * logvar)
        eps = torch.randn_like(std)
        return eps * std + mu, std + mu

    def forward(self, input: Tensor, **kwargs) -> List[Tensor]:
        mu, log_var = self.encode(input)
        z, z_inv = self.reparameterize(mu, log_var)
        z_out = self.decode(z)

        return [z_out, z_inv, input, mu, log_var]

    def loss_function(self, input, rec, mu, log_var, kld_weight=0.00025) -> dict:
        recons_loss = F.mse_loss(rec, input)

        kld_loss = torch.mean(-0.5 * torch.sum(1 +
                              log_var - mu ** 2 - log_var.exp(), dim=1), dim=0)

        loss = recons_loss + kld_weight * kld_loss

        return {'loss_vae': loss}

@HEADS.register_module()
class VFARoIHead(MetaRCNNRoIHead):

    def __init__(self, vae_dim=2048, *args, **kargs) -> None:
        super().__init__(*args, **kargs)

        self.vae = VAE(vae_dim, vae_dim, vae_dim)

    def _bbox_forward_train(self, query_feats: List[Tensor],
                            support_feats: List[Tensor],
                            sampling_results: object,
                            query_img_metas: List[Dict],
                            query_gt_bboxes: List[Tensor],
                            query_gt_labels: List[Tensor],
                            support_gt_labels: List[Tensor]) -> Dict:
        """Forward function and calculate loss for box head in training.

        Args:
            query_feats (list[Tensor]): List of query features, each item
                with shape (N, C, H, W).
            support_feats (list[Tensor]): List of support features, each item
                with shape (N, C, H, W).
            sampling_results (obj:`SamplingResult`): Sampling results.
            query_img_metas (list[dict]): List of query image info dict where
                each dict has: 'img_shape', 'scale_factor', 'flip', and may
                also contain 'filename', 'ori_shape', 'pad_shape', and
                'img_norm_cfg'. For details on the values of these keys see
                `mmdet/datasets/pipelines/formatting.py:Collect`.
            query_gt_bboxes (list[Tensor]): Ground truth bboxes for each query
                image with shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y]
                format.
            query_gt_labels (list[Tensor]): Class indices corresponding to
                each box of query images.
            support_gt_labels (list[Tensor]): Class indices corresponding to
                each box of support images.

        Returns:
            dict: Predicted results and losses.
        """
        query_rois = bbox2roi([res.bboxes for res in sampling_results])
        query_roi_feats = self.extract_query_roi_feat(query_feats, query_rois)
        support_feat = self.extract_support_feats(support_feats)[0]
        support_feat_rec, support_feat_inv, _, mu, log_var = self.vae(
            support_feat)

        bbox_targets = self.bbox_head.get_targets(sampling_results,
                                                  query_gt_bboxes,
                                                  query_gt_labels,
                                                  self.train_cfg)
        (labels, label_weights, bbox_targets, bbox_weights) = bbox_targets
        loss_bbox = {'loss_cls': [], 'loss_bbox': [], 'acc': []}
        batch_size = len(query_img_metas)
        num_sample_per_imge = query_roi_feats.size(0) // batch_size
        bbox_results = None
        for img_id in range(batch_size):
            start = img_id * num_sample_per_imge
            end = (img_id + 1) * num_sample_per_imge
            # class agnostic aggregation
            # random_index = np.random.choice(
            #     range(query_gt_labels[img_id].size(0)))
            # random_query_label = query_gt_labels[img_id][random_index]
            random_index = np.random.choice(
                range(len(support_gt_labels)))
            random_query_label = support_gt_labels[random_index]
            for i in range(support_feat.size(0)):
                if support_gt_labels[i] == random_query_label:
                    bbox_results = self._bbox_forward(
                        query_roi_feats[start:end],
                        support_feat_inv[i].sigmoid().unsqueeze(0))
                    single_loss_bbox = self.bbox_head.loss(
                        bbox_results['cls_score'], bbox_results['bbox_pred'],
                        query_rois[start:end], labels[start:end],
                        label_weights[start:end], bbox_targets[start:end],
                        bbox_weights[start:end])
                    for key in single_loss_bbox.keys():
                        loss_bbox[key].append(single_loss_bbox[key])
        if bbox_results is not None:
            for key in loss_bbox.keys():
                if key == 'acc':
                    loss_bbox[key] = torch.cat(loss_bbox['acc']).mean()
                else:
                    loss_bbox[key] = torch.stack(
                        loss_bbox[key]).sum() / batch_size

        # meta classification loss
        if self.bbox_head.with_meta_cls_loss:
            # input support feature classification
            meta_cls_score = self.bbox_head.forward_meta_cls(support_feat_rec)
            meta_cls_labels = torch.cat(support_gt_labels)
            loss_meta_cls = self.bbox_head.loss_meta(
                meta_cls_score, meta_cls_labels,
                torch.ones_like(meta_cls_labels))
            loss_bbox.update(loss_meta_cls)

        loss_vae = self.vae.loss_function(
            support_feat, support_feat_rec, mu, log_var)
        loss_bbox.update(loss_vae)

        bbox_results.update(loss_bbox=loss_bbox)
        return bbox_results

    def _bbox_forward(self, query_roi_feats: Tensor,
                      support_roi_feats: Tensor) -> Dict:
        """Box head forward function used in both training and testing.

        Args:
            query_roi_feats (Tensor): Query roi features with shape (N, C).
            support_roi_feats (Tensor): Support features with shape (1, C).

        Returns:
             dict: A dictionary of predicted results.
        """
        # feature aggregation
        roi_feats = self.aggregation_layer(
            query_feat=query_roi_feats.unsqueeze(-1).unsqueeze(-1),
            support_feat=support_roi_feats.view(1, -1, 1, 1))[0]
        cls_score, bbox_pred = self.bbox_head(
            roi_feats.squeeze(-1).squeeze(-1), query_roi_feats)
        bbox_results = dict(cls_score=cls_score, bbox_pred=bbox_pred)
        return bbox_results

    def simple_test_bboxes(
            self,
            query_feats: List[Tensor],
            support_feats_dict: Dict,
            query_img_metas: List[Dict],
            proposals: List[Tensor],
            rcnn_test_cfg: ConfigDict,
            rescale: bool = False) -> Tuple[List[Tensor], List[Tensor]]:
        """Test only det bboxes without augmentation.

        Args:
            query_feats (list[Tensor]): Features of query image,
                each item with shape (N, C, H, W).
            support_feats_dict (dict[int, Tensor]) Dict of support features
                used for inference only, each key is the class id and value is
                the support template features with shape (1, C).
            query_img_metas (list[dict]): list of image info dict where each
                dict has: `img_shape`, `scale_factor`, `flip`, and may also
                contain `filename`, `ori_shape`, `pad_shape`, and
                `img_norm_cfg`. For details on the values of these keys see
                :class:`mmdet.datasets.pipelines.Collect`.
            proposals (list[Tensor]): Region proposals.
            rcnn_test_cfg (obj:`ConfigDict`): `test_cfg` of R-CNN.
            rescale (bool): If True, return boxes in original image space.
                Default: False.

        Returns:
            tuple[list[Tensor], list[Tensor]]: Each tensor in first list
                with shape (num_boxes, 4) and with shape (num_boxes, )
                in second list. The length of both lists should be equal
                to batch_size.
        """
        img_shapes = tuple(meta['img_shape'] for meta in query_img_metas)
        scale_factors = tuple(meta['scale_factor'] for meta in query_img_metas)

        rois = bbox2roi(proposals)

        query_roi_feats = self.extract_query_roi_feat(query_feats, rois)
        cls_scores_dict, bbox_preds_dict = {}, {}
        num_classes = self.bbox_head.num_classes
        for class_id in support_feats_dict.keys():
            support_feat = support_feats_dict[class_id]
            support_feat_rec, support_feat_inv, _, mu, log_var = self.vae(
                support_feat)
            bbox_results = self._bbox_forward(
                query_roi_feats, support_feat_inv.sigmoid())
            cls_scores_dict[class_id] = \
                bbox_results['cls_score'][:, class_id:class_id + 1]
            bbox_preds_dict[class_id] = \
                bbox_results['bbox_pred'][:, class_id * 4:(class_id + 1) * 4]
            # the official code use the first class background score as final
            # background score, while this code use average of all classes'
            # background scores instead.
            if cls_scores_dict.get(num_classes, None) is None:
                cls_scores_dict[num_classes] = \
                    bbox_results['cls_score'][:, -1:]
            else:
                cls_scores_dict[num_classes] += \
                    bbox_results['cls_score'][:, -1:]
        cls_scores_dict[num_classes] /= len(support_feats_dict.keys())
        cls_scores = [
            cls_scores_dict[i] if i in cls_scores_dict.keys() else
            torch.zeros_like(cls_scores_dict[list(cls_scores_dict.keys())[0]])
            for i in range(num_classes + 1)
        ]
        bbox_preds = [
            bbox_preds_dict[i] if i in bbox_preds_dict.keys() else
            torch.zeros_like(bbox_preds_dict[list(bbox_preds_dict.keys())[0]])
            for i in range(num_classes)
        ]
        cls_score = torch.cat(cls_scores, dim=1)
        bbox_pred = torch.cat(bbox_preds, dim=1)

        # split batch bbox prediction back to each image
        num_proposals_per_img = tuple(len(p) for p in proposals)
        rois = rois.split(num_proposals_per_img, 0)
        cls_score = cls_score.split(num_proposals_per_img, 0)
        bbox_pred = bbox_pred.split(num_proposals_per_img, 0)

        # apply bbox post-processing to each image individually
        det_bboxes = []
        det_labels = []
        for i in range(len(proposals)):
            det_bbox, det_label = self.bbox_head.get_bboxes(
                rois[i],
                cls_score[i],
                bbox_pred[i],
                img_shapes[i],
                scale_factors[i],
                rescale=rescale,
                cfg=rcnn_test_cfg)
            det_bboxes.append(det_bbox)
            det_labels.append(det_label)
        return det_bboxes, det_labels


import os
import glob
import h5py
import json
import copy
import torch
import librosa
import numpy as np
import soundfile as sf
import speech_recognition as sr
from jiwer import wer
from tqdm import tqdm
from scipy import signal
from trainer import Trainer
from hps.hps import hp, Hps
from torch.autograd import Variable
from preprocess import get_spectrograms
from model.tacotron.text.symbols import symbols

############
# CONSTANT #
############
MIN_LEN = 9

def griffin_lim(spectrogram): # Applies Griffin-Lim's raw.

	def _invert_spectrogram(spectrogram): # spectrogram: [f, t]
		return librosa.istft(spectrogram, hp.hop_length, win_length=hp.win_length, window="hann")

	X_best = copy.deepcopy(spectrogram)
	for i in range(hp.n_iter):
		X_t = _invert_spectrogram(X_best)
		est = librosa.stft(X_t, hp.n_fft, hp.hop_length, win_length=hp.win_length)
		phase = est / np.maximum(1e-8, np.abs(est))
		X_best = spectrogram * phase
	X_t = _invert_spectrogram(X_best)
	y = np.real(X_t)
	return y

def spectrogram2wav(mag): # Generate wave file from spectrogram
	mag = mag.T # transpose
	mag = (np.clip(mag, 0, 1) * hp.max_db) - hp.max_db + hp.ref_db # de-noramlize
	mag = np.power(10.0, mag * 0.05) # to amplitude
	wav = griffin_lim(mag) # wav reconstruction
	wav = signal.lfilter([1], [1, -hp.preemphasis], wav) # de-preemphasis
	wav, _ = librosa.effects.trim(wav) # trim
	return wav.astype(np.float32)

def synthesis(f0, sp, ap, sr=16000):
	y = pw.synthesize(f0.astype(np.float64), sp.astype(np.float64), ap.astype(np.float64), sr, pw.default_frame_period)
	return y

def convert_x(x, c, trainer, enc_only, verbose=False):
	c_var = Variable(torch.from_numpy(np.array([c]))).cuda()
	tensor = torch.from_numpy(np.expand_dims(x, axis=0)).type(torch.FloatTensor)
	converted, enc = trainer.test_step(tensor, c_var, enc_only=enc_only, verbose=verbose)
	converted = converted.squeeze(axis=0).transpose((1, 0))
	enc = enc.squeeze(axis=0).transpose((1, 0))
	return converted, enc

def encode_x(x, trainer):
	tensor = torch.from_numpy(np.expand_dims(x, axis=0)).type(torch.FloatTensor)
	enc = trainer.encoder_test_step(tensor)
	enc = enc.squeeze(axis=0).transpose((1, 0))
	return enc

def get_trainer(hps_path, model_path, g_mode, enc_mode, clf_path):
	HPS = Hps(hps_path)
	hps = HPS.get_tuple()
	global MIN_LEN
	MIN_LEN = MIN_LEN if hps.enc_mode != 'gumbel_t' else hps.seg_len
	trainer = Trainer(hps, None, g_mode, enc_mode)
	trainer.load_model(model_path, load_model_list=hps.load_model_list, clf_path = clf_path)
	return trainer

def asr(fname):
	r = sr.Recognizer()
	with sr.WavFile(fname) as source:
		audio = r.listen(source)
	text = r.recognize_google(audio, language='en')
	return text

def compare_asr(s_wav, t_wav):
	try:
		gt = asr(s_wav)
		recog = asr(t_wav)
		err_result = wer(gt, recog), wer(' '.join([c for c in gt if c != ' ']), ' '.join([c for c in recog if c != ' ']))
	except sr.UnknownValueError:
		err_result = [1., 1.]
	except:
		err_result = [-1., -1.]
	return err_result

def parse_encodings(encodings):
	return [' '.join([str(int(e)) for i, e in enumerate(enc)]) for enc in encodings]

def write_encodings(path, encodings):
	with open(path, 'w') as file:
		for enc in encodings:
			for i, e in enumerate(enc):
				file.write(str(int(e)) + (' ' if i < len(enc)-1 else ''))
			file.write('\n')

def convert(trainer,
			seg_len,
			src_speaker_spec,
			src_speaker,
			tar_speaker,
			utt_id,
			speaker2id,
			result_dir,
			enc_only=True,
			save=['wav', 'enc']):

	# pad spec to minimum len
	PADDED = False
	if len(src_speaker_spec) < MIN_LEN:
		padding = np.zeros((MIN_LEN - src_speaker_spec.shape[0], src_speaker_spec.shape[1]))
		src_speaker_spec = np.concatenate((src_speaker_spec, padding), axis=0)
		PADDED = True

	if len(src_speaker_spec) <= seg_len:
		converted_results, encodings = convert_x(src_speaker_spec, speaker2id[tar_speaker], trainer, enc_only=enc_only)
		if PADDED:
			encodings = encodings[:MIN_LEN//8] # truncate the encoding of zero paddings

	else:
		converted_results = []
		encodings = []
		for idx in range(0, len(src_speaker_spec), seg_len):
			if idx + (seg_len*2) > len(src_speaker_spec):
				spec_frag = src_speaker_spec[idx:-1]
			else:
				spec_frag = src_speaker_spec[idx:idx+seg_len]

			if len(spec_frag) >= seg_len:
				converted_x, enc = convert_x(spec_frag, speaker2id[tar_speaker], trainer, enc_only=enc_only)
				converted_results.append(converted_x)
				encodings.append(enc)
			elif idx == 0:
				raise RuntimeError('Please check if input is too short!')

		converted_results = np.concatenate(converted_results, axis=0)
		encodings = np.concatenate(encodings, axis=0)

	wav_data = spectrogram2wav(converted_results)
	if len(save) != 0:
		if 'wav' in save:
			wav_path = os.path.join(result_dir, f'{tar_speaker}_{utt_id}.wav')
			sf.write(wav_path, wav_data, hp.sr, 'PCM_16')
		if 'enc' in save:
			enc_path = os.path.join(result_dir, f'{src_speaker}_{utt_id}.txt')
			write_encodings(enc_path, encodings)
		return wav_path, len(converted_results)
	else:
		return wav_data, encodings

def encode(src_speaker_spec, trainer, seg_len, s_speaker=None, utt_id=None, result_dir=None, save=True):
	if save:
		assert result_dir != None
		assert s_speaker != None
		assert utt_id != None

	# pad spec to minimum len
	PADDED = False
	if len(src_speaker_spec) < MIN_LEN:
		padding = np.zeros((MIN_LEN - src_speaker_spec.shape[0], src_speaker_spec.shape[1]))
		src_speaker_spec = np.concatenate((src_speaker_spec, padding), axis=0)
		PADDED = True

	if len(src_speaker_spec) <= seg_len:
		encodings = encode_x(src_speaker_spec, trainer)
		if PADDED:
			encodings = encodings[:MIN_LEN//8] # truncate the encoding of zero paddings

	else:
		encodings = []
		for idx in range(0, len(src_speaker_spec), seg_len):
			if idx + (seg_len*2) > len(src_speaker_spec):
				spec_frag = src_speaker_spec[idx:-1]
			else:
				spec_frag = src_speaker_spec[idx:idx+seg_len]

			if len(spec_frag) >= seg_len:
				enc = encode_x(spec_frag, trainer)
				encodings.append(enc)
			elif idx == 0:
				raise RuntimeError('Please check if input is too short!')

		encodings = np.concatenate(encodings, axis=0)

	if save:
		enc_path = os.path.join(result_dir, f"{s_speaker}_{utt_id}.txt")
		write_encodings(enc_path, encodings)
	else:
		return encodings

def test_from_list(trainer, seg_len, synthesis_list, data_path, speaker2id_path, result_dir, enc_only, flag='test', run_asr=False):

	with open(speaker2id_path, 'r') as f_json:
		speaker2id = json.load(f_json)

	feeds = []
	with open(synthesis_list, 'r') as f:
		file = f.readlines()
		for line in file:
			line = line.split('\n')[0].split(' ')
			feeds.append({'s_id' : line[0].split('/')[1].split('_')[0],
						  'utt_id' : line[0].split('/')[1].split('_')[1],
						  't_id' : line[1], })

	print('[Tester] - Number of files to be resynthesize: ', len(feeds))
	dir_path = os.path.join(result_dir, f'{flag}/')
	os.makedirs(dir_path, exist_ok=True)

	err_results = []
	with h5py.File(data_path, 'r') as f_h5:
		for feed in tqdm(feeds):
			conv_audio, n_frames = convert(trainer,
								 		   seg_len,
								 		   src_speaker_spec=f_h5[f"test/{feed['s_id']}/{feed['utt_id']}/lin"][()],
								 		   src_speaker=feed['s_id'],
								 		   tar_speaker=feed['t_id'],
								 		   utt_id=feed['utt_id'],
								 		   speaker2id=speaker2id,
								 		   result_dir=dir_path,
								 		   enc_only=enc_only,
								 		   save=['wav'])
			n_frames = len(f_h5[f"test/{feed['s_id']}/{feed['utt_id']}/lin"][()])
			if run_asr:
				if hp.frame_shift * (n_frames - 1) + hp.frame_length >= 3.0:
					orig_audio = spectrogram2wav(f_h5[f"test/{feed['s_id']}/{feed['utt_id']}/lin"][()])
					sf.write('orig_audio.wav', orig_audio, hp.sr, 'PCM_16')
					err_results.append(compare_asr(s_wav='orig_audio.wav', t_wav=conv_audio))
					os.remove(path='orig_audio.wav')

	if run_asr:
		err_mean = np.mean(err_results, axis=0)
		print('WERR: {:.3f}  CERR: {:.3f}, computed over {} samples'.format(err_mean[0], err_mean[1], len(err_results)))

def cross_test(trainer, seg_len, data_path, speaker2id_path, result_dir, enc_only, flag):

	with h5py.File(data_path, 'r') as f_h5:

		with open(speaker2id_path, 'r') as f_json:
			speaker2id = json.load(f_json)

		if flag == 'test':
			source_speakers = sorted(list(f_h5['test'].keys()))
		elif flag == 'train':
			source_speakers = [s for s in sorted(list(f_h5['train'].keys())) if s[0] == 'S']
		target_speakers = [s for s in sorted(list(f_h5['train'].keys())) if s[0] == 'V']

		print('[Tester] - Testing on the {}ing set...'.format(flag))
		print('[Tester] - Source speakers: %i, Target speakers: %i' % (len(source_speakers), len(target_speakers)))
		print('[Tester] - Converting all testing utterances from source speakers to target speakers, this may take a while...')

		for src_speaker in tqdm(source_speakers):
			for tar_speaker in target_speakers:
				assert src_speaker != tar_speaker
				dir_path = os.path.join(result_dir, f'{src_speaker}_to_{tar_speaker}')
				os.makedirs(dir_path, exist_ok=True)

				for utt_id in f_h5[f'test/{src_speaker}']:
					src_speaker_spec = f_h5[f'test/{src_speaker}/{utt_id}/lin'][()]
					convert(trainer,
							seg_len,
							src_speaker_spec,
							tar_speaker,
							utt_id=utt_id,
							speaker2id=speaker2id,
							result_dir=dir_path,
							enc_only=enc_only)

def test_single(trainer, seg_len, speaker2id_path, result_dir, enc_only, s_speaker, t_speaker):

	with open(speaker2id_path, 'r') as f_json:
		speaker2id = json.load(f_json)

	if s_speaker == 'S015':
		filename = './data/english/train/unit/S015_0361841101.wav'
	elif s_speaker == 'S119':
		filename = './data/english/train/unit/S119_1561145062.wav'
	elif s_speaker == 'S130':
		filename = './data/english/test/S130_3516588097.wav'
	elif s_speaker == 'S089':
		filename = './data/english/test/S089_1810826781.wav'
	elif s_speaker == 'S378':
		filename = './data/surprise/test/S378_117437.wav'
	else:
		raise NotImplementedError('Please modify path manually!')

	_, spec = get_spectrograms(filename)
	wav_data, encodings = convert(trainer,
								  seg_len,
								  src_speaker_spec=spec,
								  src_speaker=s_speaker,
								  tar_speaker=t_speaker,
								  utt_id='',
								  speaker2id=speaker2id,
								  result_dir=result_dir,
								  enc_only=enc_only,
								  save=[])

	sf.write(os.path.join(result_dir, 'result.wav'), wav_data, hp.sr, 'PCM_16')
	write_encodings(os.path.join(result_dir, 'result.txt'), encodings)

	err_result = compare_asr(filename, os.path.join(result_dir, 'result.wav'))

	print('Testing on source speaker {} and target speaker {}, output shape: {}'.format(s_speaker, t_speaker, wav_data.shape))
	print('Comparing ASR result - WERR: {:.3f}  CERR: {:.3f}'.format(err_result[0], err_result[1]))

def test_encode(trainer, seg_len, test_path, data_path, result_dir, flag='test'):

	files = sorted(glob.glob(os.path.join(test_path, '*.wav')))
	feeds = []

	for line in files:
		line = line.split('/')[-1]
		feeds.append({'s_id' : line.split('_')[0],
					  'utt_id' : line.split('_')[1].split('.')[0]})

	print('[Tester] - Number of files to encoded: ', len(feeds))
	dir_path = os.path.join(result_dir, f'{flag}/')
	os.makedirs(dir_path, exist_ok=True)

	with h5py.File(data_path, 'r') as f_h5:
		for feed in tqdm(feeds):

			src_speaker_spec = f_h5[f"test/{feed['s_id']}/{feed['utt_id']}/lin"][()]
			encode(src_speaker_spec, trainer, seg_len, s_speaker=feed['s_id'], utt_id=feed['utt_id'], result_dir=dir_path)

def target_classify(trainer, seg_len, synthesis_list, result_dir, flag='test'):
	dir_path = os.path.join(result_dir, f'{flag}/')
	with open(synthesis_list, 'r') as f:
		file = f.readlines()
	acc = []
	for line in file:
		# get wav path
		line = line.split('\n')[0].split(' ')
		utt_id = line[0].split('/')[1].split('_')[1]
		tar_speaker = line[1]
		wav_path = os.path.join(dir_path, f'{tar_speaker}_{utt_id}.wav')

		# get spectrogram
		_, spec = get_spectrograms(wav_path)

		# padding spec
		if len(spec) < seg_len:
			padding = np.zeros((seg_len - spec.shape[0], spec.shape[1]))
			spec = np.concatenate((spec, padding), axis=0)

		# classification
		logits = []
		for idx in range(0, len(spec), seg_len):
			if idx + (seg_len*2) > len(spec):
				spec_frag = spec[idx:-1]
			else:
				spec_frag = spec[idx:idx+seg_len]

			if len(spec_frag) >= seg_len:
				x = torch.from_numpy(np.expand_dims(spec_frag[:seg_len, :], axis=0)).type(torch.FloatTensor)
				logit = trainer.classify(x)
				logits.append(logit)
			elif idx == 0:
				raise RuntimeError('Please check if input is too short!')
		logits = np.concatenate(logits, axis=0)
		#logits = np.sum(logits, axis = 0)
		for logit in logits:
			am = logit.argmax()
			if am == 0:
				clf_speaker = 'V001'
			elif am ==1:
				clf_speaker = 'V002'
			else:
				clf_speaker = 'None'
			if clf_speaker == tar_speaker:
				acc.append(1)
				#print('[info]: {} is classified to {}'.format(wav_path, clf_speaker))
			else:
				acc.append(0)
				#print('[Error]: {} is classified to {}'.format(wav_path, clf_speaker))
	print('Classification Acc: {:.3f}'.format(np.sum(acc)/float(len(acc))))

def encode_for_tacotron(target, trainer, seg_len, multi2idx_path, wav_path, result_path):
	wavs = sorted(glob.glob(os.path.join(wav_path, '*.wav')))
	print('[Converter] - Number of wav files to encoded: ', len(wavs))

	names = []
	enc_outputs = []

	for wav_path in tqdm(wavs):
		name = wav_path.split('/')[-1].split('.')[0]
		s_id = name.split('_')[0]
		u_id = name.split('_')[1]
		if s_id != target:
			continue

		y, sr = librosa.load(wav_path)
		d = librosa.get_duration(y=y, sr=sr)
		if d > 25:
			continue # --> this filter out too long utts, 3523/3533 for V001 and V002 together in the english dataset

		_, spec = get_spectrograms(wav_path)
		encodings = encode(spec, trainer, seg_len, save=False)
		encodings = parse_encodings(encodings)
		enc_outputs.append(encodings)
		names.append((s_id, u_id))

	# build encodings to character mapping
	idx = 0
	multi2idx = {}
	print('[Converter] - Building encoding to symbol mapping...')
	for encodings in tqdm(enc_outputs):
		for encoding in encodings:
			if str(encoding) not in multi2idx:
				multi2idx[str(encoding)] = symbols[idx]
				idx += 1

	print('[Converter] - Number of unique discret units: ', len(multi2idx))
	with open(multi2idx_path, 'w') as file:
		file.write(json.dumps(multi2idx))

	result_path = result_path.replace('target', target)
	print('[Converter] - Writing to meta file...')
	with open(result_path, 'w') as file:
		for i, encodings in enumerate(enc_outputs):
			file.write(str(names[i][0]) + '_' + str(names[i][1] + '|'))
			for encoding in encodings:
				file.write(multi2idx[str(encoding)])
			file.write('\n')


import numpy as np
import torch as th

from .gaussian_diffusion import GaussianDiffusion, mean_flat

class KarrasDenoiser:
    def __init__(self, sigma_data: float = 0.5):
        self.sigma_data = sigma_data

    def get_snr(self, sigmas):
        return sigmas**-2

    def get_sigmas(self, sigmas):
        return sigmas

    def get_scalings(self, sigma):
        c_skip = self.sigma_data**2 / (sigma**2 + self.sigma_data**2)
        c_out = sigma * self.sigma_data / (sigma**2 + self.sigma_data**2) ** 0.5
        c_in = 1 / (sigma**2 + self.sigma_data**2) ** 0.5
        return c_skip, c_out, c_in

    def training_losses(self, model, x_start, sigmas, model_kwargs=None, noise=None):
        if model_kwargs is None:
            model_kwargs = {}
        if noise is None:
            noise = th.randn_like(x_start)

        terms = {}

        dims = x_start.ndim
        x_t = x_start + noise * append_dims(sigmas, dims)
        c_skip, c_out, _ = [append_dims(x, dims) for x in self.get_scalings(sigmas)]
        model_output, denoised = self.denoise(model, x_t, sigmas, **model_kwargs)
        target = (x_start - c_skip * x_t) / c_out

        terms["mse"] = mean_flat((model_output - target) ** 2)
        terms["xs_mse"] = mean_flat((denoised - x_start) ** 2)

        if "vb" in terms:
            terms["loss"] = terms["mse"] + terms["vb"]
        else:
            terms["loss"] = terms["mse"]

        return terms

    def denoise(self, model, x_t, sigmas, **model_kwargs):
        c_skip, c_out, c_in = [append_dims(x, x_t.ndim) for x in self.get_scalings(sigmas)]
        rescaled_t = 1000 * 0.25 * th.log(sigmas + 1e-44)
        model_output = model(c_in * x_t, rescaled_t, **model_kwargs)
        denoised = c_out * model_output + c_skip * x_t
        return model_output, denoised

class GaussianToKarrasDenoiser:
    def __init__(self, model, diffusion):
        from scipy import interpolate

        self.model = model
        self.diffusion = diffusion
        self.alpha_cumprod_to_t = interpolate.interp1d(
            diffusion.alphas_cumprod, np.arange(0, diffusion.num_timesteps)
        )

    def sigma_to_t(self, sigma):
        alpha_cumprod = 1.0 / (sigma**2 + 1)
        if alpha_cumprod > self.diffusion.alphas_cumprod[0]:
            return 0
        elif alpha_cumprod <= self.diffusion.alphas_cumprod[-1]:
            return self.diffusion.num_timesteps - 1
        else:
            return float(self.alpha_cumprod_to_t(alpha_cumprod))

    def denoise(self, x_t, sigmas, clip_denoised=True, model_kwargs=None):
        t = th.tensor(
            [self.sigma_to_t(sigma) for sigma in sigmas.cpu().numpy()],
            dtype=th.long,
            device=sigmas.device,
        )
        c_in = append_dims(1.0 / (sigmas**2 + 1) ** 0.5, x_t.ndim)
        out = self.diffusion.p_mean_variance(
            self.model, x_t * c_in, t, clip_denoised=clip_denoised, model_kwargs=model_kwargs
        )
        return None, out["pred_xstart"]

def karras_sample(*args, **kwargs):
    last = None
    for x in karras_sample_progressive(*args, **kwargs):
        last = x["x"]
    return last

def karras_sample_progressive(
    diffusion,
    model,
    shape,
    steps,
    clip_denoised=True,
    progress=False,
    model_kwargs=None,
    device=None,
    sigma_min=0.002,
    sigma_max=80,  # higher for highres?
    rho=7.0,
    sampler="heun",
    s_churn=0.0,
    s_tmin=0.0,
    s_tmax=float("inf"),
    s_noise=1.0,
    guidance_scale=0.0,
):
    sigmas = get_sigmas_karras(steps, sigma_min, sigma_max, rho, device=device)
    x_T = th.randn(*shape, device=device) * sigma_max
    sample_fn = {"heun": sample_heun, "dpm": sample_dpm, "ancestral": sample_euler_ancestral}[
        sampler
    ]

    if sampler != "ancestral":
        sampler_args = dict(s_churn=s_churn, s_tmin=s_tmin, s_tmax=s_tmax, s_noise=s_noise)
    else:
        sampler_args = {}

    if isinstance(diffusion, KarrasDenoiser):

        def denoiser(x_t, sigma):
            _, denoised = diffusion.denoise(model, x_t, sigma, **model_kwargs)
            if clip_denoised:
                denoised = denoised.clamp(-1, 1)
            return denoised

    elif isinstance(diffusion, GaussianDiffusion):
        model = GaussianToKarrasDenoiser(model, diffusion)

        def denoiser(x_t, sigma):
            _, denoised = model.denoise(
                x_t, sigma, clip_denoised=clip_denoised, model_kwargs=model_kwargs
            )
            return denoised

    else:
        raise NotImplementedError

    if guidance_scale != 0 and guidance_scale != 1:

        def guided_denoiser(x_t, sigma):
            x_t = th.cat([x_t, x_t], dim=0)
            sigma = th.cat([sigma, sigma], dim=0)
            x_0 = denoiser(x_t, sigma)
            cond_x_0, uncond_x_0 = th.split(x_0, len(x_0) // 2, dim=0)
            x_0 = uncond_x_0 + guidance_scale * (cond_x_0 - uncond_x_0)
            return x_0

    else:
        guided_denoiser = denoiser

    for obj in sample_fn(
        guided_denoiser,
        x_T,
        sigmas,
        progress=progress,
        **sampler_args,
    ):
        if isinstance(diffusion, GaussianDiffusion):
            yield diffusion.unscale_out_dict(obj)
        else:
            yield obj

def get_sigmas_karras(n, sigma_min, sigma_max, rho=7.0, device="cpu"):
    """Constructs the noise schedule of Karras et al. (2022)."""
    ramp = th.linspace(0, 1, n)
    min_inv_rho = sigma_min ** (1 / rho)
    max_inv_rho = sigma_max ** (1 / rho)
    sigmas = (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho
    return append_zero(sigmas).to(device)

def to_d(x, sigma, denoised):
    """Converts a denoiser output to a Karras ODE derivative."""
    return (x - denoised) / append_dims(sigma, x.ndim)

def get_ancestral_step(sigma_from, sigma_to):
    """Calculates the noise level (sigma_down) to step down to and the amount
    of noise to add (sigma_up) when doing an ancestral sampling step."""
    sigma_up = (sigma_to**2 * (sigma_from**2 - sigma_to**2) / sigma_from**2) ** 0.5
    sigma_down = (sigma_to**2 - sigma_up**2) ** 0.5
    return sigma_down, sigma_up

@th.no_grad()
def sample_euler_ancestral(model, x, sigmas, progress=False):
    """Ancestral sampling with Euler method steps."""
    s_in = x.new_ones([x.shape[0]])
    indices = range(len(sigmas) - 1)
    if progress:
        from tqdm.auto import tqdm

        indices = tqdm(indices)

    for i in indices:
        denoised = model(x, sigmas[i] * s_in)
        sigma_down, sigma_up = get_ancestral_step(sigmas[i], sigmas[i + 1])
        yield {"x": x, "i": i, "sigma": sigmas[i], "sigma_hat": sigmas[i], "pred_xstart": denoised}
        d = to_d(x, sigmas[i], denoised)
        # Euler method
        dt = sigma_down - sigmas[i]
        x = x + d * dt
        x = x + th.randn_like(x) * sigma_up
    yield {"x": x, "pred_xstart": x}

@th.no_grad()
def sample_heun(
    denoiser,
    x,
    sigmas,
    progress=False,
    s_churn=0.0,
    s_tmin=0.0,
    s_tmax=float("inf"),
    s_noise=1.0,
):
    """Implements Algorithm 2 (Heun steps) from Karras et al. (2022)."""
    s_in = x.new_ones([x.shape[0]])
    indices = range(len(sigmas) - 1)
    if progress:
        from tqdm.auto import tqdm

        indices = tqdm(indices)

    for i in indices:
        gamma = (
            min(s_churn / (len(sigmas) - 1), 2**0.5 - 1) if s_tmin <= sigmas[i] <= s_tmax else 0.0
        )
        eps = th.randn_like(x) * s_noise
        sigma_hat = sigmas[i] * (gamma + 1)
        if gamma > 0:
            x = x + eps * (sigma_hat**2 - sigmas[i] ** 2) ** 0.5
        denoised = denoiser(x, sigma_hat * s_in)
        d = to_d(x, sigma_hat, denoised)
        yield {"x": x, "i": i, "sigma": sigmas[i], "sigma_hat": sigma_hat, "pred_xstart": denoised}
        dt = sigmas[i + 1] - sigma_hat
        if sigmas[i + 1] == 0:
            # Euler method
            x = x + d * dt
        else:
            # Heun's method
            x_2 = x + d * dt
            denoised_2 = denoiser(x_2, sigmas[i + 1] * s_in)
            d_2 = to_d(x_2, sigmas[i + 1], denoised_2)
            d_prime = (d + d_2) / 2
            x = x + d_prime * dt
    yield {"x": x, "pred_xstart": denoised}

@th.no_grad()
def sample_dpm(
    denoiser,
    x,
    sigmas,
    progress=False,
    s_churn=0.0,
    s_tmin=0.0,
    s_tmax=float("inf"),
    s_noise=1.0,
):
    """A sampler inspired by DPM-Solver-2 and Algorithm 2 from Karras et al. (2022)."""
    s_in = x.new_ones([x.shape[0]])
    indices = range(len(sigmas) - 1)
    if progress:
        from tqdm.auto import tqdm

        indices = tqdm(indices)

    for i in indices:
        gamma = (
            min(s_churn / (len(sigmas) - 1), 2**0.5 - 1) if s_tmin <= sigmas[i] <= s_tmax else 0.0
        )
        eps = th.randn_like(x) * s_noise
        sigma_hat = sigmas[i] * (gamma + 1)
        if gamma > 0:
            x = x + eps * (sigma_hat**2 - sigmas[i] ** 2) ** 0.5
        denoised = denoiser(x, sigma_hat * s_in)
        d = to_d(x, sigma_hat, denoised)
        yield {"x": x, "i": i, "sigma": sigmas[i], "sigma_hat": sigma_hat, "denoised": denoised}
        # Midpoint method, where the midpoint is chosen according to a rho=3 Karras schedule
        sigma_mid = ((sigma_hat ** (1 / 3) + sigmas[i + 1] ** (1 / 3)) / 2) ** 3
        dt_1 = sigma_mid - sigma_hat
        dt_2 = sigmas[i + 1] - sigma_hat
        x_2 = x + d * dt_1
        denoised_2 = denoiser(x_2, sigma_mid * s_in)
        d_2 = to_d(x_2, sigma_mid, denoised_2)
        x = x + d_2 * dt_2
    yield {"x": x, "pred_xstart": denoised}

def append_dims(x, target_dims):
    """Appends dimensions to the end of a tensor until it has target_dims dimensions."""
    dims_to_append = target_dims - x.ndim
    if dims_to_append < 0:
        raise ValueError(f"input has {x.ndim} dims but target_dims is {target_dims}, which is less")
    return x[(...,) + (None,) * dims_to_append]

def append_zero(x):
    return th.cat([x, x.new_zeros([1])])


from flask import Flask,render_template,request,redirect,session,flash
app = Flask(__name__)
app.secret_key = 'Farn'

@app.route ('/')
def index():

    return render_template('index.html')

@app.route ('/result', methods=['POST'])
def result():
    if len(request.form['name']) < 1:
        flash("Name cannot be empty!")
        return redirect('/')

    # else:
    #     flash("Success! Your name is {}".format(request.form['name']))
    elif len(request.form['comment']) < 1:
        flash("Comments cannot be empty!")
        return redirect('/')
    elif len(request.form['comment']) >= 120:
        flash("Comments cannot be longer than 120 char.!")
        return redirect('/')
    else:
        flash("Success!")

    return render_template('result.html', name = request.form['name'], location = request.form['location'], language = request.form['language'], comment = request.form['comment'])

app.run(debug = True)


from modules import aws_sript, firestorage_code
import os
from flask import Flask, jsonify
from flask import render_template, request, redirect, url_for
from werkzeug.utils import secure_filename
import os, shutil
from flask_cors import CORS
from decorater_file import crossdomain

global app

app = Flask(__name__)
CORS(app)
app.config['SECRET_KEY'] = 'the quick brown fox jumps over the lazy   dog'
app.config['CORS_HEADERS'] = ['Content-Type']

cors = CORS(app, resources={r"/*": {"origins": "*"}})

# Create a directory in a known location to uploaded files to.
uploads_dir = os.path.join(app.instance_path, 'uploads')
if not os.path.exists(uploads_dir):
    os.makedirs(uploads_dir)

# Create a directory in a known location to processed files to.
ml_output_dir = os.path.join(app.instance_path, 'ml_output')
if not os.path.exists(ml_output_dir):
    os.makedirs(ml_output_dir)

@app.route('/upload', methods=['GET', 'POST'])

def upload():
    if request.method == 'POST':
        # save the single "profile" file
        image = request.files['image']
        print(image)

        img_path = os.path.join(uploads_dir, secure_filename(image.filename))

        ml_out_img_path = os.path.join(ml_output_dir, secure_filename(image.filename))
        image.save(img_path)

        print("img_path",img_path)
        print("ml_out_img_path",ml_out_img_path)
        # ML model start
        print("Processing...")
        aws_sript.convert_image(img_path, ml_out_img_path)
        # ML model ends
        print("Image converted successfully")

        img_url = firestorage_code.upload_img(ml_out_img_path)

        print("Image uploaded to firebase storage successfully")

        print("Firebase Image URL ",img_url)

        # cleaning folders
        clean_folders()

        # save each "charts" file

        # for file in request.files.getlist('upload'):
        #     print(file.name)
        #     file.save(os.path.join(uploads_dir, file.name))

        # return redirect(url_for('upload'))
        data = {
            "img_url":img_url
        }
    return jsonify(data),{'Access-Control-Allow-Origin': '*'}

def clean_folders():
        folders = [uploads_dir,ml_output_dir]
        for folder in folders:
            for filename in os.listdir(folder):
                file_path = os.path.join(folder, filename)
                try:
                    if os.path.isfile(file_path) or os.path.islink(file_path):
                        os.unlink(file_path)
                    elif os.path.isdir(file_path):
                        shutil.rmtree(file_path)
                except Exception as e:
                    print('Failed to delete %s. Reason: %s' % (file_path, e))

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


import unittest
from adt_extension import SwitchDict

class SwitchDictTest(unittest.TestCase):
    def setUp(self):
        """New object for all tests."""
        self.switch_dict = SwitchDict({
            'test1': 1,
            'test2': 2,
            'test3': 3,
        })

    def test_overload_getitem(self):
        """Check default case."""
        self.switch_dict.default_case = 'Default case here'
        # Exist index
        self.assertEqual(self.switch_dict['test1'], 1)
        # Default case
        self.assertEqual(self.switch_dict.default_case, 'Default case here')
        # Not exist index
        test_value = self.switch_dict['testtest']
        self.assertEqual(test_value, 'Default case here')
        # Not exist index and not exist default_case
        self.switch_dict.default_case = None
        with self.assertRaises(KeyError) as raises:
            test_value = self.switch_dict['testest']