import torch
import sys
# absolute path to utilities
sys.path.insert(0, 'C:\\Users\\ahmed.saidani\\Desktop\\FMLB\\server\\fml\\utils')
import torch.nn as nn
from models import CifarCnn, MnistCnn
from data_loaders import CifarDataLoader, MnistDataLoader
from config import Config
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import torch.optim as optim
import syft as sy  # <-- NEW: import the Pysyft library
import os, psutil
import time
from sklearn.metrics import precision_score, recall_score, f1_score
import GPUtil

# declare the config, model, dataset, optimizer, and criterion
config = Config(sys.argv)

# supports both mnist and cifar datasets
if config.dataset =="CIFAR":
    model = CifarCnn()
else:
    model = MnistCnn()

if config.dataset =="CIFAR":
    dataloader = CifarDataLoader(sys.argv)
else:
    dataloader = MnistDataLoader(sys.argv)

optimizer = optim.SGD(model.parameters(), lr=config.lr)
criterion = nn.CrossEntropyLoss()

# declare clients
hook = sy.TorchHook(torch)
client_one = sy.VirtualWorker(hook, id="client_one")
client_two = sy.VirtualWorker(hook, id="client_two")

# workers and gpu config
use_cuda = config.gpu and torch.cuda.is_available()
torch.manual_seed(1)
device = torch.device("cuda" if use_cuda else "cpu")
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}

# load federated dataset
federated_train_loader = sy.FederatedDataLoader(dataloader.train_dataset.federate((client_one, client_two)), batch_size=config.batch_size, shuffle=True, **kwargs)

# define training
def train():
    for i in range(config.epochs):
        model.train()
        for batch_idx, (data, target) in enumerate(federated_train_loader):
            model.send(data.location)
            data, target = data.to(device), target.to(device)
            optimizer.zero_grad()
            output = model(data)
            loss = criterion(output, target)
            loss.backward()
            optimizer.step()
            model.get()

# define testing
def test():
    model.eval()
    total_loss = 0
    correct = 0
    total = 0
    y_true = []
    y_pred = []
    with torch.no_grad():
        for data, target in dataloader.test_loader:
            data, target = data.to(device), target.to(device)
            output = model(data)
            y_true.extend(target.numpy())
            _, predicted = torch.max(output.data, 1)
            y_pred.extend(predicted.cpu().numpy())
            total_loss += criterion(output, target).item()
            pred = output.argmax(1, keepdim=True)
            correct += pred.eq(target.view_as(pred)).sum().item()
            total += target.size(0)
    # get accuracy metrics
    accuracy = str(100 * correct/total)
    total_loss = str(total_loss/total)
    precision=str(100 * sum(precision_score(y_true, y_pred, average=None))/len(dataloader.train_dataset.classes))
    recall=str(100 * sum(recall_score(y_true, y_pred, average=None))/len(dataloader.train_dataset.classes))
    fone=str(100 * sum(f1_score(y_true, y_pred, average=None))/len(dataloader.train_dataset.classes))
    return accuracy, total_loss, precision, recall, fone

# define benchmarking function
def benchmark():
  # declare start time, cpu and network use at the begining
  start_time = time.time()
  old_network = psutil.net_io_counters().bytes_recv + psutil.net_io_counters().bytes_sent
  old_cpu = psutil.cpu_percent(interval=None)
  # scrape metrics
  train()
  accuracy, total_loss, precision, recall, fone = test()
  execution_time = time.time() - start_time
  cpu = str(psutil.cpu_percent(interval=None))
  execution_time = str(execution_time)
  new_network = psutil.net_io_counters().bytes_recv + psutil.net_io_counters().bytes_sent
  network = str(new_network - old_network)
  memory = str(psutil.Process(os.getpid()).memory_info().rss / 1024 ** 2)
  if config.gpu == True:
    GPUs = GPUtil.getGPUs()
    gpu = str(GPUs[0].load * 100)
  else:
    gpu = "0"
  # log metrics
  data = '{ library: pysyft; \n accuracy: ' + accuracy + '; \n loss: ' + total_loss + '; \n recall: ' + recall + '; \n precision: ' + precision + '; \n f1: ' + fone + '; \n time: ' + execution_time + '; \n network: ' + network  + '; \n memory: ' +  memory + '; \n cpu: ' +  cpu + '; \n gpu: ' +  gpu +"; \n }"
  print(data)
  sys.stdout.flush()

# run
benchmark()


import csv
import sys

class ConvertCSV:
    def __init__(self,fileName):
        self.fileName = fileName

    def fileOpen(self):
        """
        引数に指定されているファイルを読み込み
        """
        try:
            with open(self.fileName, 'r') as file:
                reader = csv.reader(file)
                self.readerData = list(reader)  # CSVファイル全体を変数に格納
        except Exception as e:
            print("ファイルを開く際にエラーが発生しました。")
            print("実行時引数にcsvファイルを指定しているか確認してください。")
            sys.exit()

        csv_count = 0
        for row in self.readerData:
            csv_count += 1
            if csv_count == 1001:
                print("csvファイルが1000行を超えています。")
                sys.exit()

    def convert(self):
        """
        変換を行う
        """
        for row in self.readerData:
            print(row)

def main():
    args = sys.argv
    execution = ConvertCSV(args[1])
    execution.fileOpen()
    execution.convert()

if __name__ == '__main__':
    main()


#Import file along filepath
import os
import csv

csvpath = os.path.join('Resources', 'election_data.csv')

#Read the CSV File
with open(csvpath, encoding='utf') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=",")

    ## Assing preloop values
    win = object
    cVoteC = 0
    cVoteD = 0
    cVoteR = 0
    #cVoteO = 0 (( I added this to ensure there weren't any odd entries))

    ## Count 3 unique occurances of each name per ballot/row
    for row in csv_reader:
        if row [2] == "Charles Casper Stockham":
            cVoteC = cVoteC + 1
        elif row [2]=="Diana DeGette":
            cVoteD = cVoteD + 1
        elif row [2] == "Raymon Anthony Doane":
            cVoteR = cVoteR + 1
        #else:
            #cVoteO = CVoteO + 1

## Total votes
    tVotes = (cVoteC) + (cVoteD) + (cVoteR)# + (cVoteO)

## Divide the 3 occurance counts by the total
    pVoteC = (cVoteC) / (tVotes)
    pVoteD = (cVoteD) / (tVotes)
    pVoteR = (cVoteR) / (tVotes)

## Format percentages
    fpVoteC = "{:.3%}".format(pVoteC)
    fpVoteD = "{:.3%}".format(pVoteD)
    fpVoteR = "{:.3%}".format(pVoteR)

## Find the largest total
    if (cVoteC) > (cVoteD) and (cVoteC) > (cVoteR):
        win = "Charles.Casper.Stockham"
    elif (cVoteD) > (cVoteC) and (cVoteD) > (cVoteR):
        win = "Diana.DeGette"
    else:
        win = "Raymon.Anthony.Doane"

print(" ")
print("Election Results")
print("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _")
print(" ")
print(f"Total votes: {tVotes}")
print("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _")
print(" ")
print(f"Charles Casper Stockham: {fpVoteC} ({cVoteC})")
print(f"Diana DeGette: {fpVoteD} ({cVoteD})")
print(f"Raymon Anthony Doane: {fpVoteR} ({cVoteR})")
print("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _")
print(" ")
print(f"Winner: {win}")
print("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _")
print(" ")

# Set variable for output file
output_file = os.path.join("PyPoll")

#  Open the output file
with open(output_file, "w") as txtfile:
    writer = csv.writer(txtfile, delimiter=' ', quoting=csv.QUOTE_NONE)
    writer.writerow(".")
    writer.writerow("Election.Results")
    writer.writerow("_._._._._._._._._._._._._._._._._._._")
    writer.writerow(".")
    writer.writerow(f"Total.votes:.{tVotes}")
    writer.writerow("_._._._._._._._._._._._._._._._._._._")
    writer.writerow(".")
    writer.writerow(f"Charles.Casper.Stockham:.{fpVoteC}.({cVoteC})")
    writer.writerow(f"Diana.DeGette:.{fpVoteD}.({cVoteD})")
    writer.writerow(f"Raymon.Anthony.Doane:.{fpVoteR}.({cVoteR})")
    writer.writerow("_._._._._._._._._._._._._._._._._._._")
    writer.writerow(".")
    writer.writerow(f"Winner:.{win}")
    writer.writerow("_._._._._._._._._._._._._._._._._._._")
    writer.writerow(".")


import logging, requests, tqdm, random, boto3, instances, json
from typing import List

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s - %(message)s')

# Get the AWS instance base url
INSTANCE = instances.retreive_instance()
INSTANCE.load()
DNS_NAME = INSTANCE.public_dns_name
BASE_URL = 'http://' + DNS_NAME

def benchmark(tech_1: str, tech_2: str, dataset: List[str], repeat: int):
    '''
    Runs MapReduce tasks for provided technologies.

        Parameters:
            tech_1 (str):       1st technology to compare ('hadoop', 'spark' or 'linux')
            tech_2 (str):       2nd technology to compare ('hadoop', 'spark' or 'linux')
            tech_2 (List[str]): List of input filenames
            repeat (int):       Number of times to repeat each comparison

        Returns:
            results (dict): The comparison results
    '''
    # Fetch result n number of times, for each technologies, over all input files
    results = {}
    with tqdm.tqdm(total=len(dataset)*2*repeat) as pbar:
        for data in dataset:
            results[data] = {tech_1: [], tech_2: []}
            for i in range(repeat):
                for t in (tech_1, tech_2):
                    resp = requests.get(BASE_URL + '/' + t + '/wordcount/' + data)
                    results[data][t].append(resp.json()['time'])
                    pbar.update(1)

    # Parse the results
    parsed_results = {
        'per_file': {},
        'total': {}
    }
    totals = {tech_1: 0.0, tech_2: 0.0}
    for file in results:
        totals_file = {tech_1: 0.0, tech_2: 0.0}
        for tech in results[file]:
            for test in results[file][tech]:
                totals[tech]      += test['elapsed']
                totals_file[tech] += test['elapsed']
        parsed_results['per_file'][file] = {
            tech_1: { 'average_time': totals_file[tech_1] / repeat },
            tech_2: { 'average_time': totals_file[tech_2]  / repeat }
        }
    parsed_results['total'] = {
        tech_1: { 'average_time': totals[tech_1] / ( repeat * len(dataset) ) },
        tech_2: { 'average_time': totals[tech_2]  / ( repeat * len(dataset) ) }
    }
    return parsed_results

def main():
    '''
    The main function of the bechmarking script.

    This function will execute two comparisons and output rhe results in files.
    The first comparison is Hadoop vs Linux, which runs 10 times with
    'pg4300.txt' file as input, which means that a total of 20 MapReduce tasks
    are done for thsi comparison.

    The second comparison is Hadoop vs Spark, which runs 3 times for every file
    provided in the dataset (total of 9 files), which means that a total of
    54 MapReduce task are done for this comparison.

    Files generated by this functions are:
    - hadoop_vs_linux.json
    - hadoop_vs_spark.json
    '''

    # Run Hadoop vs Linux benchmark
    logging.info('HADOOP vs LINUX')
    hadoop_vs_linux = benchmark(
        'hadoop',
        'linux',
        [
            'pg4300.txt'
        ],
        10
    )
    with open('hadoop_vs_linux.json', 'w') as file:
        file.write(json.dumps(hadoop_vs_linux, indent=4))
    logging.info('Results written to hadoop_vs_linux.json.')

    # Run Hadoop vs Spark benchmark
    logging.info('HADOOP vs SPARK')
    hadoop_vs_spark = benchmark(
        'hadoop',
        'spark',
        [
            'buchanj-midwinter-00-t.txt',
            'carman-farhorizons-00-t.txt',
            'charlesworth-scene-00-t.txt',
            'cheyneyp-darkbahama-00-t.txt',
            'colby-champlain-00-t.txt',
            'delamare-bumps-00-t.txt',
            'delamare-lucy-00-t.txt',
            'delamare-myfanwy-00-t.txt',
            'delamare-penny-00-t.txt'
        ],
        3
    )
    with open('hadoop_vs_spark.json', 'w') as file:
        file.write(json.dumps(hadoop_vs_spark, indent=4))
    logging.info('Results written to hadoop_vs_spark.json.')

if __name__ == "__main__":
    main()


# учимся работать со списком списков. Финал
def TankRush(H1, W1, S1, H2, W2, S2):
    # проверяем подходит ли количество строк и столбцов
    if H2 > H1 or W2 > W1:
        result = False
        return result

    # разбиваем строку на части
    S1_split = S1.split(' ')
    S2_split = S2.split(' ')

    #   !!! определяем все варианты номера начальной строки и столбца, записываем их в список списков
    start_variants = []
    i_column_1 = -1
    for i_1, val_1 in enumerate(S1_split):
        while 1:
            i_column_1 = val_1.find(S2_split[0], i_column_1 + 1)
            if i_column_1 == -1:
                break
            start_variants += [[i_1, i_column_1]]

    # цикл по вариантам начального номера строки и столбца
    for start in start_variants:
        i_string_1 = start[0]
        i_column_1 = start[1]

        # основной цикл
        i_2 = 0
        general_match = 0

        for i_1, val_1 in enumerate(S1_split):
            try:
                if i_1 < i_string_1:
                    pass

                elif i_1 >= i_string_1 \
                        and (val_1[i_column_1:(i_column_1 + len(S2_split[i_2]))] == S2_split[i_2]):
                    i_2 += 1
                    general_match += 1

                else:
                    break

            except:
                break

        # финальная проверка по всем совпадениям
        if general_match == len(S2_split):
            result = True
            break
        else:
            result = False

    return result


import random

from colour import Color

from manim import *

class SweepingLine(Scene):
    def construct(self):
        growing_circle = Circle(radius=0.001)

        moving_line = Line(start=[-7, 6, 0], end=[-6, -6, 0])
        moving_line.normal_vector = (
            moving_line.copy().rotate(90 * DEGREES).get_vector()
        )

        def opacity_updater(obj: Mobject):
            if sum(  # check whether dot is inside circle
                (growing_circle.points[0] - growing_circle.get_center()) ** 2
            ) >= sum((obj.get_center() - growing_circle.get_center()) ** 2):
                obj.set_fill(BLUE, opacity=1)
                obj.clear_updaters()
                obj.add_updater(color_updater)
                # self.add_sound()

        def color_updater(obj: Mobject):
            if np.dot(  # check whether point is *right* of the line
                obj.get_center(), moving_line.normal_vector
            ) < np.dot(moving_line.get_start(), moving_line.normal_vector):
                if obj.color != Color(BLUE):
                    obj.set_color(BLUE)
                    # self.add_sound()
            else:
                if obj.color != Color(YELLOW):
                    obj.set_color(YELLOW)

        self.add(growing_circle)

        for _ in range(30):
            p = Dot(fill_opacity=0.6)
            p.move_to([random.uniform(-6, 6), random.uniform(-4, 4), 0])
            p.add_updater(opacity_updater)
            self.add(p)

        self.play(
            growing_circle.animate.scale_to_fit_width(
                1.5 * config.frame_width
            ),
            run_time=5,
        )

        self.play(Create(moving_line))
        self.play(moving_line.animate.shift(14 * RIGHT), run_time=5)
        self.play(moving_line.animate.shift(14 * LEFT), run_time=5)


class BST:
	class Node:
		def __init__(self, data):
			# Intializes the note with the right data
			self.data = data
			self.left = None
			self.right = None

	def __init__(self):
		# Creates an empty BST
		self.root = None

	def __contains__(self, data):
		# This tells the program to start at the root
		return self._contains(data, self.root)

	def _contains(self, data, node):
		# This is the base class
		if node.data == data:
			print('True')
			return True

		else:
			# check left
			if data < node.data:
				if node.left != None:
					self._contains(data, node.left)
			# check right
			elif data > data.data:
				if node.right != None:
					self._contains(data, node.right)
			else:
				return False

# This is what will check if certain numbers are in the tree:
tree = BST()
print(3 in tree) # True
print(2 in tree) # False
print(7 in tree) # True
print(6 in tree) # True
print(9 in tree) # False


"""
129. Sum Root to Leaf Numbers
Medium

You are given the root of a binary tree containing digits from 0 to 9 only.
Each root-to-leaf path in the tree represents a number.
For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123.
Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer. A leaf node is a node with no children.

Example 1:
Input: root = [1,2,3]
Output: 25
Explanation:
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Therefore, sum = 12 + 13 = 25.

Example 2:
Input: root = [4,9,0,5,1]
Output: 1026
Explanation:
The root-to-leaf path 4->9->5 represents the number 495.
The root-to-leaf path 4->9->1 represents the number 491.
The root-to-leaf path 4->0 represents the number 40.
Therefore, sum = 495 + 491 + 40 = 1026.
"""

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

class Solution:
    def sumNumbers(self, root: Optional[TreeNode]) -> int:
        """
        BFS for level order traverse
        Record (node, pre) when traversing the tree
        res += pre when reaching leaf node
        """
        res = 0
        queue = collections.deque([(root, 0)])
        while queue:
            node, pre = queue.popleft()
            pre = 10 * pre + node.val
            if not node.left and not node.right:
                res += pre
            if node.left:
                queue.append((node.left, pre))
            if node.right:
                queue.append((node.right, pre))
        return res

class Solution:
    def sumNumbers(self, root: TreeNode, pre=0) -> int:
        """Return sum of root-to-leaf numbers"""
        if not root:
            return 0

        pre = 10 * pre + root.val
        if not root.left and not root.right:
            return pre

        left = self.sumNumbers(root.left, pre)
        right = self.sumNumbers(root.right, pre)
        return left + right


from .data import Data

from presentation.view import FileServer, SyncServer

from .urls import *
from .utils import *
from .views import *

import logging

logging.basicConfig(filename="application/logs/p2p.log",
                            filemode='a',
                            format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
                            datefmt='%H:%M:%S',
                            level=logging.DEBUG)

class Manager:

	@staticmethod
	def start():

		file_server = FileServer(host='0.0.0.0')
		sync_server = SyncServer(host='0.0.0.0')

		port1 = file_server.start()
		port2 = sync_server.start()
		with open(Data.node_config, 'w') as outfile:
			json.dump({"nodes":{"0.0.0.0":{"ip":"0.0.0.0","port":port1,"active":False}}},outfile)

		print("listening on 0.0.0.0:%s TCP"%port1)
		print("listening on 0.0.0.0:%s UDP"%port2)
		logging.info("listening on 0.0.0.0:%s TCP"%port1)
		logging.info("listening on 0.0.0.0:%s UDP"%port2)
		while True:
			print(">",end="")
			command = input()
			if "-l" in command:
				Data.cluster_list = command[command.index("-l")+1]
			if "-d" in command:
				Data.directory = command[command.index("-d")+1]
			if command == "list":
				print(get_cluster())

			elif len(command.split()) > 1 and command.split()[0] == "get":
				request_to_all(command.split()[1])


import os
import numpy as np
import gym

from wgcsl.common import logger
from wgcsl.algo.wgcsl import WGCSL
from wgcsl.algo.supervised_sampler import make_sample_transitions, make_random_sample
from wgcsl.common.monitor import Monitor
from wgcsl.envs.multi_world_wrapper import PointGoalWrapper, SawyerGoalWrapper, ReacherGoalWrapper, FetchGoalWrapper

# offline parameters
DEFAULT_ENV_PARAMS = {
    'Point2DLargeEnv-v1':{
         'n_cycles':5,
         'n_batches': 1,
         'baw_delta': 0.15,
    },
    'Point2D-FourRoom-v1':{
        'n_cycles':5,
        'n_batches': 1,
        'baw_delta': 0.15,
    },
    'SawyerReachXYZEnv-v1':{
        'n_cycles':5,
        'n_batches': 5,
        'baw_delta': 0.15,
        'num_epoch':100,
    },
    'FetchReach-v1': {
        'n_cycles': 5,
        'n_batches': 5,
        'baw_delta': 0.15,
        'num_epoch':100,
    },
    'Reacher-v2': {
        'n_cycles': 10,
        'n_batches': 10,
        'baw_delta': 0.15,
        'num_epoch':200,
    },
    'SawyerDoor-v0':{
        'n_cycles': 10,
        'n_batches': 10,
        'baw_delta': 0.15,
        'num_epoch':200,
        },
    'FetchPush-v1':{
        'batch_size': 512,
        'n_cycles': 20,
        'n_batches': 20,
        'baw_delta': 0.01,
        'num_epoch':100,
        },
    'FetchSlide-v1':{
        'batch_size': 512,
        'n_cycles': 20,
        'n_batches': 20,
        'baw_delta': 0.01,
        'num_epoch':100,
        },
    'FetchPickAndPlace-v1':{
        'batch_size': 512,
        'n_cycles': 20,
        'n_batches': 20,
        'baw_delta': 0.01,
        'num_epoch':100,
        },
    'HandReach-v0':{
        'batch_size': 512,
        'n_cycles': 20,
        'n_batches': 20,
        'baw_delta': 0.01,
        'num_epoch':100,
        }
}

DEFAULT_PARAMS = {
    # env
    'max_u': 1.,  # max absolute value of actions on each coordinate
    'layers': 3,  # number of layers in the critic/actor networks
    'hidden': 256,  # number of neurons in each hidden layers
    'network_class': 'wgcsl.algo.actor_critic:ActorCritic',
    'Q_lr': 5e-4,  # critic learning rate
    'pi_lr': 5e-4,  # actor learning rate
    'buffer_size': int(1E6),  # for experience replay
    'polyak': 0.9,  #polyak averaging coefficient
    'action_l2': 1.0,  # quadratic penalty on actions (before rescaling by max_u)
    'clip_obs': 200.,
    'scope': 'wgcsl',
    'relative_goals': False,
    # training
    'num_epoch':50,
    'n_cycles': 10,  # per epoch
    'rollout_batch_size': 1,  # per mpi thread
    'n_batches': 4,  # training batches per cycle
    'batch_size': 128,  # per mpi thread, measured in transitions and reduced to even multiple of chunk_length.
    'n_test_rollouts': 100,  # number of test rollouts per epoch, each consists of rollout_batch_size rollouts
    'test_with_polyak': False,  # run test episodes with the target network
    # exploration, not used in the offline setting
    'random_eps': 0.3,  # percentage of time a random action is taken
    'noise_eps': 0.2,  # std of gaussian noise added to not-completely-random actions as a percentage of max_u
    # random init episode, not used int the offline setting
    'random_init':20,

    # goal relabeling
    'replay_strategy': 'future',
    'replay_k': 4,  # number of additional goals used for replay
    # normalization
    'norm_eps': 1e-4,  # epsilon used for observation normalization
    'norm_clip': 5,  # normalized observations are cropped to this values
    # use supervised
    'use_supervised': False,
    # best-advantage weight
    'baw_delta': 0.1,
    'baw_max': 80,

    # if do not use her
    'no_relabel':False    # used for no relabel
}

CACHED_ENVS = {}

def cached_make_env(make_env):
    """
    Only creates a new environment from the provided function if one has not yet already been
    created. This is useful here because we need to infer certain properties of the env, e.g.
    its observation and action spaces, without any intend of actually using it.
    """
    if make_env not in CACHED_ENVS:
        env = make_env()
        CACHED_ENVS[make_env] = env
    return CACHED_ENVS[make_env]

def prepare_mode(kwargs):
    if 'mode' in kwargs.keys():
        mode = kwargs['mode']
        if mode == 'supervised':
            kwargs['use_supervised'] = True
        else:
            kwargs['use_supervised'] = False
    else:
        kwargs['use_supervised'] = False
    return kwargs

def prepare_params(kwargs):
    # default max episode steps
    kwargs = prepare_mode(kwargs)
    default_max_episode_steps = 50
    # WGCSL params
    wgcsl_params = dict()
    env_name = kwargs['env_name']
    def make_env(subrank=None):
        try:
            env = gym.make(env_name, rewrad_type='sparse')
        except:
            logger.log('Can not make sparse reward environment')
            env = gym.make(env_name)
        # add wrapper for multiworld environment
        if env_name.startswith('Fetch'):
            env._max_episode_steps = 50
            env = FetchGoalWrapper(env)
        elif env_name.startswith('HandManipulate'):
            env._max_episode_steps = 100
        elif env_name.startswith('Point'):
            env = PointGoalWrapper(env)
            env.env._max_episode_steps = 50
        elif env_name.startswith('Sawyer'):
            env = SawyerGoalWrapper(env)
        elif env_name.startswith('Reacher'):
            env = ReacherGoalWrapper(env)

        if (subrank is not None and logger.get_dir() is not None):
            try:
                from mpi4py import MPI
                mpi_rank = MPI.COMM_WORLD.Get_rank()
            except ImportError:
                MPI = None
                mpi_rank = 0
                logger.warn('Running with a single MPI process. This should work, but the results may differ from the ones publshed in Plappert et al.')

            if hasattr(env, '_max_episode_steps'):
                max_episode_steps = env._max_episode_steps
            else:
                max_episode_steps = default_max_episode_steps # otherwise use defaulit max episode steps
            env =  Monitor(env,
                           os.path.join(logger.get_dir(), str(mpi_rank) + '.' + str(subrank)),
                           allow_early_resets=True)
            # hack to re-expose _max_episode_steps (ideally should replace reliance on it downstream)
            env = gym.wrappers.TimeLimit(env, max_episode_steps=max_episode_steps)
        return env

    kwargs['make_env'] = make_env
    tmp_env = cached_make_env(kwargs['make_env'])
    if hasattr(tmp_env, '_max_episode_steps'):
        kwargs['T'] = tmp_env._max_episode_steps
    else:
        kwargs['T'] = default_max_episode_steps

    kwargs['max_u'] = np.array(kwargs['max_u']) if isinstance(kwargs['max_u'], list) else kwargs['max_u']
    kwargs['gamma'] = 1. - 1. / kwargs['T']
    if 'lr' in kwargs:
        kwargs['pi_lr'] = kwargs['lr']
        kwargs['Q_lr'] = kwargs['lr']
        del kwargs['lr']
    for name in ['buffer_size', 'hidden', 'layers','network_class','polyak','batch_size',
                 'Q_lr', 'pi_lr', 'norm_eps', 'norm_clip', 'max_u','action_l2', 'clip_obs',
                 'scope', 'relative_goals', 'use_supervised']:
        wgcsl_params[name] = kwargs[name]
        kwargs['_' + name] = kwargs[name]
        del kwargs[name]

    kwargs['wgcsl_params'] = wgcsl_params
    return kwargs

def log_params(params, logger=logger):
    for key in sorted(params.keys()):
        logger.info('{}: {}'.format(key, params[key]))

def configure_her(params):
    env = cached_make_env(params['make_env'])
    env.reset()

    def reward_fun(ag_2, g, info):  # vectorized
        return env.compute_reward(achieved_goal=ag_2, desired_goal=g, info=info)

    # Prepare configuration for HER.
    her_params = {
        'reward_fun': reward_fun,
        'no_relabel': params['no_relabel']
    }
    for name in ['replay_strategy', 'replay_k']:
        her_params[name] = params[name]
        params['_' + name] = her_params[name]
        del params[name]

    sample_supervised, her_sampler = make_sample_transitions(**her_params)
    random_sampler = make_random_sample(her_params['reward_fun'])
    samplers = {
        'random': random_sampler,
        'her': her_sampler,
        'supervised':sample_supervised
    }
    return samplers, reward_fun

def simple_goal_subtract(a, b):
    assert a.shape == b.shape
    return a - b

def configure_wgcsl(dims, params, reuse=False, use_mpi=True, clip_return=True, offline_train=False):
    samplers, reward_fun = configure_her(params)
    # Extract relevant parameters.
    rollout_batch_size = params['rollout_batch_size']
    wgcsl_params = params['wgcsl_params']

    input_dims = dims.copy()
    # WGCSL agent
    env = cached_make_env(params['make_env'])
    env.reset()
    wgcsl_params.update({'input_dims': input_dims,  # agent takes an input observations
                        'T': params['T'],
                        'clip_pos_returns': True,  # clip positive returns
                        'clip_return': (1. / (1. - params['gamma'])) if clip_return else np.inf,  # max abs of return
                        'rollout_batch_size': rollout_batch_size,
                        'subtract_goals': simple_goal_subtract,
                        'sample_transitions': samplers['her'],
                        'random_sampler':samplers['random'],
                        'supervised_sampler':samplers['supervised'],
                        'gamma': params['gamma'],
                        'su_method': params['su_method'],
                        'baw_delta': params['baw_delta'],
                        'baw_max': params['baw_max'],
                        })
    wgcsl_params['info'] = {
        'env_name': params['env_name'],
        'reward_fun':reward_fun
    }
    policy = WGCSL(reuse=reuse, **wgcsl_params, use_mpi=use_mpi, offline_train=offline_train)
    return policy

def configure_dims(params):
    env = cached_make_env(params['make_env'])
    env.reset()
    obs, _, _, info = env.step(env.action_space.sample())
    dims = {
        'o': obs['observation'].shape[0],
        'u': env.action_space.shape[0],
        'g': obs['desired_goal'].shape[0],
    }
    return dims


from flask import Flask, render_template, session, redirect, url_for, flash
from flask_wtf import FlaskForm
from wtforms import (StringField, BooleanField, validators,
                     RadioField, TextAreaField, SubmitField)

app = Flask(__name__)

app.config["SECRET_KEY"] = "mykey"

class Form(FlaskForm):
    name = StringField("Enter Name", [validators.DataRequired()])

    submit = SubmitField("submit")

@app.route("/", methods=["POST", "GET"])
def home():
    form = Form()
    if form.validate_on_submit():
        session["name"] = form.name.data
        flash("thank you! {}".format(session["name"]))

        return redirect(url_for("home"))
    return render_template("home.html", form=form)

@app.route("/thankyou", methods=["POST", "GET"])
def thankyou():
    return render_template("thankyou.html")

app.run(debug=True)


import numpy as np

########################### EULER ANGLES #############################################################
# Define basic rotations:
# We use the conventions r = R*r'
# i.e. a unit rotation defines a rotation from the body fixed coordinates to the world-frame coordinates

def fRx(theta):

    return np.array([[1, 0       , 0           ],
                      [0, np.cos(theta),-np.sin(theta)],
                      [0, np.sin(theta), np.cos(theta)]
                     ])
def fRy(theta):
    return np.array([[np.cos(theta), 0 , np.sin(theta)],
                      [0            , 1 , 0 ],
                      [-np.sin(theta), 0 , np.cos(theta)]
                     ])

def fRz(theta):
    return np.array([[np.cos(theta), -np.sin(theta)  , 0],
                      [np.sin(theta), np.cos(theta) , 0],
                      [0            , 0           , 1]
                    ])

################################### QUATERNIONS

def skew(q):
    return np.array([ [ 0   ,-q[2], q[1] ],
                      [ q[2], 0   ,-q[0] ],
                      [-q[1], q[0], 0    ] ])

class Quaternion(object):
    def __init__(self,q0,q):
        '''
        a quaterion consist of a scalar q0 and a three dimensional vector q
        '''
        if (len(q) == 3):
            self.q0 = float(q0)
            self.q  = q
        else:
            print('.q.shape {0}'.format(q.shape))
            raise TypeError

    def __neg__(self):
        return Quaternion(-self.q0,-self.q)

    def __add__(self,other):
        '''
        q = self
        p = other
        q + p = (q0 + p0, qvec + pvec)
        '''

        if type(other) is Quaternion:
            return Quaternion(self.q0 + other.q0,self.q+other.q)
        elif type(other) is list:
            qlist = []
            for _,q in enumerate(other):
                qlist.append(self+q)
            return qlist

    def __sub__(self,other):
        '''
        q = self
        p = other
        q - p = (q0 - p0, qvec - pvec)
        '''
        if type(other) is Quaternion:
            return Quaternion(self.q0 - other.q0, self.q - other.q)
        elif type(other) is list:
            qlist = []
            for _,q in enumerate(other):
                qlist.append(self-q)
            return qlist

    def __mul__(self,other):
        ''' quaternion product (non-commutative):
            q = self
            p = other
            'x' indicates the cross product
            q*p = (q0*p0 - qvec*pvec, q0*pvec + p0*qvec + qvec x pvec)

        '''

        if type(other) is Quaternion:
            v0 = self.q0*other.q0 - self.q.dot(other.q)
            v  = self.q0*other.q + other.q0*self.q + np.cross(self.q,other.q)
            return Quaternion(v0,v)
        elif type(other) is list:
            qlist = []
            for _,q in enumerate(other):
                qlist.append(self*q)
            return qlist

    def adj(self):
        '''
        The adjoint of the quaternion (q0, -qvec)
        '''
        return Quaternion(self.q0,-self.q)

    def norm(self):
        #return np.sqrt( (self.adjoint() * self).q0 )
        return np.sqrt(self.q0**2 + self.q.dot(self.q))

    def Q(self):
        '''
        Quaternion matrix
        '''
        Q1   = np.hstack(( self.q0, -self.q ) )
        Q234 = np.hstack( (self.q[:,None] , self.q0*np.eye(3) + skew(self.q) ))
        Q = np.vstack((Q1,Q234))

        return Q

    @staticmethod
    def to_nparray_st(data):
        if type(data) is Quaternion:
            return data.to_nparray()
        else:
            qarray = np.zeros( (len(data), 4) )
            for i,q in enumerate(data):
                qarray[i,:] = q.to_nparray()
            return qarray

    def to_nparray(self):
            return np.hstack( ([self.q0],self.q))

    @staticmethod
    def from_R(R):
        '''Return (list of) Quaternion from (list of) Rotation Matrix'''
        if type(R) is list:
            res = []
            for Rtmp in R:
                res.append(Quaternion.from_R(Rtmp))
            return res
        else:
            return get_q_from_R(R)

    @staticmethod
    def from_nparray(qarray):
        ''' Return list of Quaternions from an np array
        qarray: n_data x 4 numpy array in which each column is [q0, q1, q2, q3]
        '''
        if qarray.ndim==1:
            # Single sample:
            return Quaternion(qarray[0], qarray[1:])
        else:
            qlist = []
            for i in range(qarray.shape[0]):
                qlist.append(Quaternion(qarray[i,0], qarray[i,1:]))
            return qlist

    def adjQ(self):
        '''
        Quaternion matrix
        '''
        Q1   = np.hstack(( self.q0, -self.q ) )
        Q234 = np.hstack( (self.q[:,None] , self.q0*np.eye(3) - skew(self.q) ))
        Q = np.vstack((Q1,Q234))

        return Q

    def normalized(self):
        norm = self.norm()
        return Quaternion(self.q0/norm, self.q/norm)

    def i(self):
        ''' Reciprocal (inverse) of a Quaternion'''
        qbar = self.adj()
        norm2 = self.norm()**2
        return Quaternion(qbar.q0/norm2, qbar.q/norm2)

    def R(self):
        ''' From Peter Corke's Matlab robotics toolbox'''
        s = self.q0
        x = self.q[0]
        y = self.q[1]
        z = self.q[2]

        R = np.array([
            [1-2*(y**2+z**2), 2*(x*y-s*z)    , 2*(x*z+s*y)],
            [2*(x*y+s*z)    , 1-2*(x**2+z**2), 2*(y*z-s*x)],
            [2*(x*z-s*y)    , 2*(y*z+s*x)    , 1-2*(x**2+y**2)]
            ])
        return R

    def axis_angle(self):
        return get_axisangle(self.to_nparray())

    def __str__(self):
        return "Q({0:.2f}, {1})".format(self.q0,self.q)

    def __repr__(self):
        return self.__str__()

############## Rotational Conversions:

def quatToEulerXYZ(q):
    q = q.normalized()
    R = q.R();

    # NASA paper:
    th1 = np.arctan2(-R[1,2],R[2,2])
    th2 = np.arctan2(R[0,2],np.sqrt(1-R[0,2]**2))
    th3 = np.arctan2(-R[0,1],R[0,0])

    return np.array([th1,th2,th3])

def get_q_from_R(R):
    ''' From Peter Corke's Robotics toolbox'''

    qs = min(np.sqrt( np.trace(R) +1)/2.0,1.0)
    kx = R[2,1] - R[1,2]   # Oz - Ay
    ky = R[0,2] - R[2,0]   # Ax - Nz
    kz = R[1,0] - R[0,1]   # Ny - Ox

    if (R[0,0] >= R[1,1]) and (R[0,0] >= R[2,2]) :
        kx1 = R[0,0] - R[1,1] - R[2,2] + 1 # Nx - Oy - Az + 1
        ky1 = R[1,0] + R[0,1]              # Ny + Ox
        kz1 = R[2,0] + R[0,2]              # Nz + Ax
        add = (kx >= 0)
    elif (R[1,1] >= R[2,2]):
        kx1 = R[1,0] + R[0,1]              # Ny + Ox
        ky1 = R[1,1] - R[0,0] - R[2,2] + 1 # Oy - Nx - Az + 1
        kz1 = R[2,1] + R[1,2]              # Oz + Ay
        add = (ky >= 0)
    else:
        kx1 = R[2,0] + R[0,2]              # Nz + Ax
        ky1 = R[2,1] + R[1,2]              # Oz + Ay
        kz1 = R[2,2] - R[0,0] - R[1,1] + 1 # Az - Nx - Oy + 1
        add = (kz >= 0)

    if add:
        kx = kx + kx1
        ky = ky + ky1
        kz = kz + kz1
    else:
        kx = kx - kx1
        ky = ky - ky1
        kz = kz - kz1

    nm = np.linalg.norm(np.array([kx, ky, kz]))
    if nm == 0:
        q = Quaternion(1, np.zeros(3))
    else:
        s = np.sqrt(1 - qs**2) / nm
        qv = s*np.array([kx, ky, kz])
        q = Quaternion(qs, qv)

    return q

#### Axis-Angle Representation:
def get_axisangle(d, e=None, reg=1e-6):
    ''' Compute axis angle of axis de w.r.t e
        d: Vector [1d ndarray]
        e: Vector [1d ndarray]

        Computation:
        ax    = (e x d) / (|| e x d ||)
        angle = arccos( d * e )

        * is in product
        x is the cross product

        if no e is provided, we adopt: e = [0, 0, 1]^T

    '''

    if e is None:
        e = np.array([0,0,1])
        norm = np.sqrt(d[0]**2 + d[1]**2)
        if norm < reg:
            return (e,0)
        else:
            vec = np.array( [-d[1], d[0], 0])
            return vec/norm, np.arccos(d[2])
    else:
        # Compute cross product between identity and d
        exd = skew(e).dot(d)
        norm = np.linalg.norm(exd)

        # Check norm:
        if norm < reg:
            # smaller than reguralization, assume no rotation:
            ax = e
            angle = 0
        else:
            # Rotation is present:
            ax = exd/norm
            angle = np.arccos( (d*e).sum( axis=(d.ndim-1) ) )
        return (ax, angle)

def R_from_axis_angle(ax, angle):
    ''' Get Rotation matrix from axis angle representation using Rodriguez formula
        ax   : The unit axis defining the axis of rotation [ 1d ndarray]
        angle: Angle of rotation [float]

        Return:
        R(ax, angle) = I + sin(angle) x ax + (1 - cos(angle) ) x ax^2

        where x is the cross product
    '''

    utilde = skew(ax)
    return np.eye(3) + np.sin(angle)*utilde + (1 - np.cos(angle))*utilde.dot(utilde)


'ModuleNotFoundError'
def i_e(num1):
    try:
        import mat
        total=mat.sqrt(num1)
        print(total)
    except ModuleNotFoundError:
        print('El modulo que importaste no pudo ser importado o tal vez no existe')
    except:
        print('El programa sufre de otro ')
    else:
        print(total)
i_e(2)


#!/Users/maxiaoyu/Desktop/python/evns /py3/bin/python3
#encoding:utf-8
'''
@File    :   10.py
@Time    :   2020/03/12 15:34:48
@Author  :   DesignerA
@Version :   1.0
@Contact :   DesignerA@qq.com
@WebSite :   www.cnblogs.com/DesignerA
'''
# Start typing your code from here

#给定一段英文文本，统计每个单词出现的次数；打印输出，按照词频从高到低输出：
#提示：可以用字典来统计：key 是单词，value 是单词出现次数
#先创建一个字典，然后遍历刚刚取出的单词列表，接着做一个判断： 如果字典中 key 已经出现了这个单词
#那么它对应的value，也就是出现次数就+1；如果这个单词没出现过，就直接插入这个单词及value为1到字典中
list1=list(input('请输入英文文本:').split(' '))
print(list1)
dict1={}
length=len(list1)
print(length)
i=0
while(i<length):
    str1=list1[i]
    if(str1 not in dict1.keys()):
        dict1[str1]=1
    elif(str1 in dict1.keys()):
        dict1[str1]+=1
    i+=1
print(dict1)
print(sorted(dict1.items(),key = lambda x:x[1],reverse = True))


import random
import numpy as np

random.seed(1345)

cov_file = './vcv_betweenAreas.csv'
spps_names = np.loadtxt('./spps_names.csv', dtype=str).tolist()

data = np.loadtxt('./betwee_areas_Xy.csv', delimiter=',')
# n,p = data.shape
# data = np.c_[np.ones(n),data]
X0,y0 = data[:,:-1], data[:,-1]

n,p = X0.shape
num_test = round(0.4*n)

def P_mat(spps, cov_file):

    raw_data = []
    row_spps = []
    with open(cov_file, 'r') as f:
        for i in f.readlines()[1:]:
            line = i.strip().split(',')
            row_spps.append(line[0].replace('"', ''))
            raw_data.append( line[1:] )

    vcv = np.array(raw_data).astype(float)
    idxs = [row_spps.index(i) for i in spps]

    row_aug = vcv[idxs,:]
    vcv_aug = row_aug[:,idxs]

    Q,L,Qt = np.linalg.svd(vcv_aug)
    # inverse of the
    # square root of lambda
    LH = np.diag( np.sqrt( 1/abs(L) ) )

    return Q.dot(LH).dot(Qt)

def rmse(K, alpha, Y):
    return np.sqrt( np.mean( (K.dot(alpha) - Y)**2 ) )

def distance_matrix(a, b):
    """
    l2 norm squared matrix
    """
    return np.linalg.norm(a[:, None, :] - b[None, :, :], axis=-1)**2

def RBF_kernel(a, b, gamma):
    """
    Radial Basis Function
    """
    # import sklearn.metrics
    tmp_rbf = -gamma*distance_matrix(a, b)
    np.exp(tmp_rbf, tmp_rbf) # RBF kernel. Inplace exponentiation
    return tmp_rbf
    # return sklearn.metrics.pairwise.rbf_kernel(a, b, gamma=gamma)

def linear_kernel(a, b):
    """
    Linear Kernel
    """
    return a.dot(b.T)

def opt_alpha(K, y, reg_lam = None):
    # Y = y
    # X = X
    # m = None
    K_Idx = K + reg_lam * np.diag( np.ones( K.shape[0] ) )
    return np.linalg.inv( K_Idx ).dot( y )
    # return np.linalg.pinv( K_Idx ).dot( y )

def gls_error(X, y, pseudo_inv = False):
    if pseudo_inv:
        B_gls = np.linalg.pinv(X.T.dot(X)).dot(X.T.dot(y))
    else:
        B_gls = np.linalg.inv(X.T.dot(X)).dot(X.T.dot(y))

    return rmse(X, B_gls, y)

def standard_scale(u):
    return (u - np.mean(u, axis=0))/np.std( u )

P = P_mat(spps_names, cov_file)
X = standard_scale( P.dot(X0) )
y = standard_scale( P.dot(y0) )

reg_lam = 0.00005
reg_gamma = 1e-5

K_linear = linear_kernel( X, X )
alpha_linear = opt_alpha(K_linear, y, reg_lam)

K_rbf = RBF_kernel(X, X, gamma = reg_gamma)
alpha_rbf = opt_alpha(K_rbf, y, reg_lam)

error_rbf = rmse(K_rbf, alpha_rbf, y)
error_lk = rmse(K_linear, alpha_linear, y)
error_gls = gls_error(X, y)

print('RBF kernel = %s' % round(error_rbf, 5))
print('linear kernel = %s' % round(error_lk, 5))
print('GLS = %s' % round(error_gls, 5))

def cv_hype(X, y, max_iter,
              reg_lambdas,
              kernel_type = 'linear',
              gamma = None):

    n,p = X.shape

    if not gamma:
        gamma = 1/2*p

    # kernel_func = linear_kernel
    # max_iter = 15
    # reg_lambdas = np.linspace( 0.001, 0.025, 100, dtype=float )

    training_errors = []
    testing_errors = []

    for reg_lam in reg_lambdas:

        tmp_train = []
        tmp_test = []

        for _ in range(max_iter):

            test_idx  = random.sample(range(n), k = num_test)
            train_idx = list( set(range(n)) - set(test_idx) )

            X_train, X_test = X[train_idx,:], X[test_idx,:]
            y_train, y_test = y[train_idx]  , y[test_idx]

            if kernel_type == 'rbf':
                K_train = RBF_kernel( X_train, X_train, gamma )
                K_test = RBF_kernel(  X_test,  X_train, gamma )

            elif kernel_type == 'linear':
                K_train = linear_kernel( X_train, X_train)
                K_test  = linear_kernel( X_test, X_train )

            else:
                try:
                    gls_err_train = gls_error(X_train, y_train)
                    gls_err_test  = gls_error(X_test, y_test)

                except np.linalg.LinAlgError:
                    gls_err_train = gls_error(X_train, y_train, pseudo_inv=True)
                    gls_err_test  = gls_error(X_test, y_test, pseudo_inv=True)

                tmp_train.append( gls_err_train )
                tmp_test.append( gls_err_test )
                continue

            alpha_trained = opt_alpha(K_train, y_train, reg_lam)

            tmp_train.append( rmse(K_train, alpha_trained, y_train) )
            tmp_test.append( rmse(K_test , alpha_trained, y_test) )

        training_errors.append( np.mean(tmp_train) )
        testing_errors.append( np.mean(tmp_test) )

    return training_errors, testing_errors

def cv_errors(X, y, max_iter,
              reg_lambda,
              kernel_type = 'linear',
              gamma = None):

    n,p = X.shape

    if not gamma:
        gamma = 1/2*p

    # kernel_func = linear_kernel
    # max_iter = 15
    # reg_lambdas = np.linspace( 0.001, 0.025, 100, dtype=float )

    training_errors = []
    testing_errors = []

    for _ in range(max_iter):

        test_idx  = random.sample(range(n), k = num_test)
        train_idx = list( set(range(n)) - set(test_idx) )

        X_train, X_test = X[train_idx,:], X[test_idx,:]
        y_train, y_test = y[train_idx]  , y[test_idx]

        if kernel_type == 'rbf':
            K_train = RBF_kernel( X_train, X_train, gamma )
            K_test = RBF_kernel(  X_test,  X_train, gamma )

        elif kernel_type == 'linear':
            K_train = linear_kernel( X_train, X_train)
            K_test  = linear_kernel( X_test, X_train )

        else:
            try:
                gls_err_train = gls_error(X_train, y_train)
                gls_err_test  = gls_error(X_test, y_test)

            except np.linalg.LinAlgError:
                gls_err_train = gls_error(X_train, y_train, pseudo_inv=True)
                gls_err_test  = gls_error(X_test, y_test, pseudo_inv=True)

            training_errors.append( gls_err_train )
            testing_errors.append( gls_err_test )
            continue

        alpha_trained = opt_alpha(K_train, y_train, reg_lam)

        training_errors.append( rmse(K_train, alpha_trained, y_train) )
        testing_errors.append( rmse(K_test , alpha_trained, y_test) )

    return training_errors, testing_errors

max_iter = 20
reg_lambdas_linear = np.linspace( 1e-10, 1e-6, 100, dtype = float )
# reg_lambdas_linear = np.linspace( 5e-13, 1e-10, 100, dtype = float )
(train_error_lk,
 test_error_lk ) = cv_hype(X, y, max_iter = max_iter ,
                            reg_lambdas = reg_lambdas_linear,
                            kernel_type = 'linear',
                            gamma = None)

reg_gamma = 1e-5
# reg_lambdas_rbf = np.linspace( 1e-5, 1e-4, 100, dtype = float )
reg_lambdas_rbf = np.linspace( 1e-5, 5e-3, 100, dtype = float )
(train_error_rbf,
  test_error_rbf ) = cv_hype(X, y, max_iter = max_iter ,
                            reg_lambdas = reg_lambdas_rbf,
                            kernel_type = 'rbf',
                            gamma = reg_gamma)

(train_error_gls,
  test_error_gls ) = cv_hype(X, y, max_iter = max_iter ,
                            reg_lambdas = reg_lambdas_rbf,
                            kernel_type = 'gls',
                            gamma = None)


# Count the micro:bit's uptime dans display it under binary format
# Press A to switch between binary and decimal display
# Press B to add 1 to the counter
from microbit import *

BINARY = True
DECIMAL = False

def decimal_to_binary_img(n):
    """
    Take a number `n` and convert it into a "micro:bit ready" binary representation.
    """
    # Convert the time from base 10 to base 2 (binary) on 25 positions
    binary_n = '{0:025b}'.format(n)

    # Revert the string to start displaying in the upper left corner
    reverted_binary_n = ''.join(reversed(binary_n))

    # Convert 1 to 9 (led's the brightest luminosity)
    reverted_binary_n = reverted_binary_n.replace('1', '9')

    # Build the image
    return Image(':'.join([reverted_binary_n[i:i+5] for i in range(0, len(reverted_binary_n), 5)]))

display_format = BINARY
while True:
    button_pressed = False
    seconds = running_time() // 1000

    if button_b.was_pressed():
        button_pressed = True
        presses = button_b.get_presses()
        seconds += button_b.get_presses()
        display.scroll('+%s' % presses)

    if button_a.was_pressed():
        button_pressed = True
        display_format = not display_format

    if display_format == BINARY:
        display.show(decimal_to_binary_img(seconds))
    elif display_format == DECIMAL:
        display.scroll(str(seconds))

    if button_pressed:
        # Directly retart the loop so the change appears faster
        continue
    else:
        # Wait for a second
        sleep(1000)


# _*_ coding:utf-8 _*_
__author__ = 'geda'
__date__ = '2020/2/4 20:22'
import re

from django import forms
from operation.models import UserAsk

class UserAskForm(forms.ModelForm):
	class Meta:
		model = UserAsk
		fields = ['name', 'mobile', 'course_name']

	def clean_mobile(self):
		mobile = self.cleaned_data['mobile']
		p = re.compile('^0\d{2,3}\d{7,8}$|^1[358]\d{9}$|^147\d{8}')
		if p.match(mobile):
			return mobile
		raise forms.ValidationError('手机号码格式不正确', code='mobile_inval')


from collections import deque

from rosshm import log
from rosshm.db.sql import insert as mod_insert
from rosshm.db.sql import select as mod_select
from rosshm.db.sql import table

__all__ = ['setLang', 'createTable', 'insert', 'select']

# lang specifics manager
lang = None

def setLang(manager):
	global lang
	log.debug(f"set lang {manager.name}")
	lang = manager
	table.lang = manager
	mod_insert.lang = manager
	mod_select.lang = manager

createTable = table.create
insert = mod_insert.insert
select = mod_select.select


#!/usr/bin/env python3

from typing import no_type_check, Optional, Tuple

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import Tensor

"""
@no_type_check annotation is applied to type-hinted models to avoid errors
related to mismatch with parent (nn.Module) signature. # type_ignore is not
possible here, since it causes errors in JIT scripting code which parses
the relevant type hints.
"""

class BasicLinearReLULinear(nn.Module):
    def __init__(self, in_features, out_features=5, bias=False) -> None:
        super().__init__()
        self.fc1 = nn.Linear(in_features, out_features, bias=bias)
        self.relu1 = nn.ReLU()
        self.fc2 = nn.Linear(out_features, 1, bias=bias)

    def forward(self, x):
        x = self.fc1(x)
        x = self.relu1(x)
        x = self.fc2(x)
        return x

class MixedKwargsAndArgsModule(nn.Module):
    def __init__(self) -> None:
        super().__init__()

    def forward(self, x, y=None):
        if y is not None:
            return x + y
        return x

class BasicModel(nn.Module):
    def __init__(self) -> None:
        super().__init__()

    def forward(self, input):
        input = 1 - F.relu(1 - input)
        return input

class BasicModel2(nn.Module):
    """
    Example model one from the paper
    https://arxiv.org/pdf/1703.01365.pdf

    f(x1, x2) = RELU(ReLU(x1) - 1 - ReLU(x2))
    """

    def __init__(self) -> None:
        super().__init__()

    def forward(self, input1, input2):
        relu_out1 = F.relu(input1)
        relu_out2 = F.relu(input2)
        return F.relu(relu_out1 - 1 - relu_out2)

class BasicModel3(nn.Module):
    """
    Example model two from the paper
    https://arxiv.org/pdf/1703.01365.pdf

    f(x1, x2) = RELU(ReLU(x1 - 1) - ReLU(x2))
    """

    def __init__(self) -> None:
        super().__init__()

    def forward(self, input1, input2):
        relu_out1 = F.relu(input1 - 1)
        relu_out2 = F.relu(input2)
        return F.relu(relu_out1 - relu_out2)

class BasicModel4_MultiArgs(nn.Module):
    """
    Slightly modified example model from the paper
    https://arxiv.org/pdf/1703.01365.pdf
    f(x1, x2) = RELU(ReLU(x1 - 1) - ReLU(x2) / x3)
    """

    def __init__(self) -> None:
        super().__init__()

    def forward(self, input1, input2, additional_input1, additional_input2=0):
        relu_out1 = F.relu(input1 - 1)
        relu_out2 = F.relu(input2)
        relu_out2 = relu_out2.div(additional_input1)
        return F.relu(relu_out1 - relu_out2)[:, additional_input2]

class BasicModel5_MultiArgs(nn.Module):
    """
    Slightly modified example model from the paper
    https://arxiv.org/pdf/1703.01365.pdf
    f(x1, x2) = RELU(ReLU(x1 - 1) * x3[0] - ReLU(x2) * x3[1])
    """

    def __init__(self) -> None:
        super().__init__()

    def forward(self, input1, input2, additional_input1, additional_input2=0):
        relu_out1 = F.relu(input1 - 1) * additional_input1[0]
        relu_out2 = F.relu(input2)
        relu_out2 = relu_out2 * additional_input1[1]
        return F.relu(relu_out1 - relu_out2)[:, additional_input2]

class BasicModel6_MultiTensor(nn.Module):
    def __init__(self) -> None:
        super().__init__()

    def forward(self, input1, input2):
        input = input1 + input2
        return 1 - F.relu(1 - input)[:, 1]

class BasicLinearModel(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.linear = nn.Linear(7, 1)

    def forward(self, x1, x2):
        return self.linear(torch.cat((x1, x2), dim=-1))

class BasicLinearModel2(nn.Module):
    def __init__(self, in_features, out_features) -> None:
        super().__init__()
        self.linear = nn.Linear(in_features, out_features, bias=False)

    def forward(self, input):
        return self.linear(input)

class BasicLinearModel_Multilayer(nn.Module):
    def __init__(self, in_features, hidden_nodes, out_features) -> None:
        super().__init__()
        self.linear1 = nn.Linear(in_features, hidden_nodes, bias=False)
        self.linear2 = nn.Linear(hidden_nodes, out_features, bias=False)

    def forward(self, input):
        x = self.linear1(input)
        return self.linear2(x)

class ReLUDeepLiftModel(nn.Module):
    r"""
    https://www.youtube.com/watch?v=f_iAM0NPwnM
    """

    def __init__(self) -> None:
        super().__init__()
        self.relu1 = nn.ReLU()
        self.relu2 = nn.ReLU()

    def forward(self, x1, x2, x3=2):
        return 2 * self.relu1(x1) + x3 * self.relu2(x2 - 1.5)

class LinearMaxPoolLinearModel(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        # kernel size -> 4
        self.lin1 = nn.Linear(4, 4, bias=False)
        self.lin1.weight = nn.Parameter(torch.eye(4, 4))
        self.pool1 = nn.MaxPool1d(4)
        self.lin2 = nn.Linear(1, 1, bias=False)
        self.lin2.weight = nn.Parameter(torch.ones(1, 1))

    def forward(self, x):
        x = x.unsqueeze(1)
        return self.lin2(self.pool1(self.lin1(x))[:, 0, :])

class BasicModelWithReusedModules(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.lin1 = nn.Linear(3, 2)
        self.relu = nn.ReLU()
        self.lin2 = nn.Linear(2, 2)

    def forward(self, inputs):
        return self.relu(self.lin2(self.relu(self.lin1(inputs))))

class BasicModelWithReusedLinear(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.lin1 = nn.Linear(3, 3)
        self.relu = nn.ReLU()

    def forward(self, inputs):
        return self.relu(self.lin1(self.relu(self.lin1(inputs))))

class BasicModelWithSparseInputs(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.lin1 = nn.Linear(3, 1)
        self.lin1.weight = nn.Parameter(torch.tensor([[3.0, 1.0, 2.0]]))
        self.lin1.bias = nn.Parameter(torch.zeros(1))

    def forward(self, inputs, sparse_list):
        return (
            self.lin1(inputs) + (sparse_list[0] if torch.numel(sparse_list) > 0 else 0)
        ).sum()

class BasicModel_MaxPool_ReLU(nn.Module):
    def __init__(self, inplace=False) -> None:
        super().__init__()
        self.maxpool = nn.MaxPool1d(3)
        self.relu = nn.ReLU(inplace=inplace)

    def forward(self, x):
        return self.relu(self.maxpool(x)).sum(dim=1)

class TanhDeepLiftModel(nn.Module):
    r"""
    Same as the ReLUDeepLiftModel, but with activations
    that can have negative outputs
    """

    def __init__(self) -> None:
        super().__init__()
        self.tanh1 = nn.Tanh()
        self.tanh2 = nn.Tanh()

    def forward(self, x1, x2):
        return 2 * self.tanh1(x1) + 2 * self.tanh2(x2 - 1.5)

class ReLULinearModel(nn.Module):
    r"""
    Simple architecture similar to:
    https://github.com/marcoancona/DeepExplain/blob/master/deepexplain/tests/test_tensorflow.py#L65
    """

    def __init__(self, inplace: bool = False) -> None:
        super().__init__()
        self.l1 = nn.Linear(3, 1, bias=False)
        self.l2 = nn.Linear(3, 1, bias=False)
        self.l1.weight = nn.Parameter(torch.tensor([[3.0, 1.0, 0.0]]))  # type: ignore
        self.l2.weight = nn.Parameter(torch.tensor([[2.0, 3.0, 0.0]]))  # type: ignore
        self.relu = nn.ReLU(inplace=inplace)
        self.l3 = nn.Linear(2, 1, bias=False)
        self.l3.weight = nn.Parameter(torch.tensor([[1.0, 1.0]]))  # type: ignore

    @no_type_check
    def forward(self, x1: Tensor, x2: Tensor, x3: int = 1) -> Tensor:
        return self.l3(self.relu(torch.cat([self.l1(x1), x3 * self.l2(x2)], dim=1)))

class SimpleLRPModel(nn.Module):
    def __init__(self, inplace) -> None:
        super().__init__()
        self.linear = nn.Linear(3, 3, bias=False)
        self.linear.weight.data.fill_(2.0)
        self.relu = torch.nn.ReLU(inplace=inplace)
        self.linear2 = nn.Linear(3, 1, bias=False)
        self.linear2.weight.data.fill_(3.0)
        self.dropout = torch.nn.Dropout(p=0.01)

    def forward(self, x):
        return self.dropout(self.linear2(self.relu(self.linear(x))))

class Conv1dSeqModel(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.seq = nn.Sequential(nn.Conv1d(4, 2, 1), nn.ReLU(), nn.Linear(1000, 1))

    def forward(self, inputs):
        return self.seq(inputs)

class TextModule(nn.Module):
    r"""Basic model that has inner embedding layer. This layer can be pluged
    into a larger network such as `BasicEmbeddingModel` and help us to test
    nested embedding layers
    """

    def __init__(self, num_embeddings, embedding_dim, second_embedding=False) -> None:
        super().__init__()
        self.inner_embedding = nn.Embedding(num_embeddings, embedding_dim)
        self.second_embedding = second_embedding
        if self.second_embedding:
            self.inner_embedding2 = nn.Embedding(num_embeddings, embedding_dim)

    def forward(self, input=None, another_input=None):
        assert input is not None, "The inputs to embedding module must be specified"
        embedding = self.inner_embedding(input)
        if self.second_embedding:
            another_embedding = self.inner_embedding2(
                input if another_input is None else another_input
            )
        return embedding if another_input is None else embedding + another_embedding

class BasicEmbeddingModel(nn.Module):
    r"""
    Implements basic model with nn.Embedding layer. This simple model
    will help us to test nested InterpretableEmbedding layers
    The model has the following structure:
    BasicEmbeddingModel(
      (embedding1): Embedding(30, 100)
      (embedding2): TextModule(
        (inner_embedding): Embedding(30, 100)
      )
      (linear1): Linear(in_features=100, out_features=256, bias=True)
      (relu): ReLU()
      (linear2): Linear(in_features=256, out_features=1, bias=True)
    )
    """

    def __init__(
        self,
        num_embeddings=30,
        embedding_dim=100,
        hidden_dim=256,
        output_dim=1,
        nested_second_embedding=False,
    ) -> None:
        super().__init__()
        self.embedding1 = nn.Embedding(num_embeddings, embedding_dim)
        self.embedding2 = TextModule(
            num_embeddings, embedding_dim, nested_second_embedding
        )
        self.linear1 = nn.Linear(embedding_dim, hidden_dim, bias=False)
        self.linear1.weight = nn.Parameter(torch.ones(hidden_dim, embedding_dim))
        self.relu = nn.ReLU()
        self.linear2 = nn.Linear(hidden_dim, output_dim)
        self.linear2.weight = nn.Parameter(torch.ones(output_dim, hidden_dim))

    def forward(self, input1, input2, input3=None):
        embedding1 = self.embedding1(input1)
        embedding2 = self.embedding2(input2, input3)
        embeddings = embedding1 + embedding2
        return self.linear2(self.relu(self.linear1(embeddings))).sum(1)

class MultiRelu(nn.Module):
    def __init__(self, inplace: bool = False) -> None:
        super().__init__()
        self.relu1 = nn.ReLU(inplace=inplace)
        self.relu2 = nn.ReLU(inplace=inplace)

    @no_type_check
    def forward(self, arg1: Tensor, arg2: Tensor) -> Tuple[Tensor, Tensor]:
        return (self.relu1(arg1), self.relu2(arg2))

class BasicModel_MultiLayer(nn.Module):
    def __init__(self, inplace=False, multi_input_module=False) -> None:
        super().__init__()
        # Linear 0 is simply identity transform
        self.multi_input_module = multi_input_module
        self.linear0 = nn.Linear(3, 3)
        self.linear0.weight = nn.Parameter(torch.eye(3))
        self.linear0.bias = nn.Parameter(torch.zeros(3))
        self.linear1 = nn.Linear(3, 4)
        self.linear1.weight = nn.Parameter(torch.ones(4, 3))
        self.linear1.bias = nn.Parameter(torch.tensor([-10.0, 1.0, 1.0, 1.0]))

        self.linear1_alt = nn.Linear(3, 4)
        self.linear1_alt.weight = nn.Parameter(torch.ones(4, 3))
        self.linear1_alt.bias = nn.Parameter(torch.tensor([-10.0, 1.0, 1.0, 1.0]))
        self.multi_relu = MultiRelu(inplace=inplace)
        self.relu = nn.ReLU(inplace=inplace)

        self.linear2 = nn.Linear(4, 2)
        self.linear2.weight = nn.Parameter(torch.ones(2, 4))
        self.linear2.bias = nn.Parameter(torch.tensor([-1.0, 1.0]))

    @no_type_check
    def forward(
        self,
        x: Tensor,
        add_input: Optional[Tensor] = None,
        multidim_output: bool = False,
    ):
        input = x if add_input is None else x + add_input
        lin0_out = self.linear0(input)
        lin1_out = self.linear1(lin0_out)
        if self.multi_input_module:
            relu_out1, relu_out2 = self.multi_relu(lin1_out, self.linear1_alt(input))
            relu_out = relu_out1 + relu_out2
        else:
            relu_out = self.relu(lin1_out)
        lin2_out = self.linear2(relu_out)
        if multidim_output:
            stack_mid = torch.stack((lin2_out, 2 * lin2_out), dim=2)
            return torch.stack((stack_mid, 4 * stack_mid), dim=3)
        else:
            return lin2_out

class BasicModelBoolInput(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.mod = BasicModel_MultiLayer()

    def forward(
        self,
        x: Tensor,
        add_input: Optional[Tensor] = None,
        mult: float = 10.0,
    ):
        assert x.dtype is torch.bool, "Input must be boolean"
        return self.mod(x.float() * mult, add_input)

class BasicModel_MultiLayer_MultiInput(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.model = BasicModel_MultiLayer()

    @no_type_check
    def forward(self, x1: Tensor, x2: Tensor, x3: Tensor, scale: int):
        return self.model(scale * (x1 + x2 + x3))

class BasicModel_MultiLayer_TrueMultiInput(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.m1 = BasicModel_MultiLayer()
        self.m234 = BasicModel_MultiLayer_MultiInput()

    @no_type_check
    def forward(
        self, x1: Tensor, x2: Tensor, x3: Tensor, x4: Optional[Tensor] = None
    ) -> Tensor:
        a = self.m1(x1)
        if x4 is None:
            b = self.m234(x2, x3, x1, scale=1)
        else:
            b = self.m234(x2, x3, x4, scale=1)
        return a + b

class BasicModel_ConvNet_One_Conv(nn.Module):
    def __init__(self, inplace: bool = False) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 2, 3, 1)
        self.relu1 = nn.ReLU(inplace=inplace)
        self.fc1 = nn.Linear(8, 4)
        self.conv1.weight = nn.Parameter(torch.ones(2, 1, 3, 3))  # type: ignore
        self.conv1.bias = nn.Parameter(torch.tensor([-50.0, -75.0]))  # type: ignore
        self.fc1.weight = nn.Parameter(  # type: ignore
            torch.cat([torch.ones(4, 5), -1 * torch.ones(4, 3)], dim=1)
        )
        self.fc1.bias = nn.Parameter(torch.zeros(4))  # type: ignore
        self.relu2 = nn.ReLU(inplace=inplace)

    @no_type_check
    def forward(self, x: Tensor, x2: Optional[Tensor] = None):
        if x2 is not None:
            x = x + x2
        x = self.relu1(self.conv1(x))
        x = x.view(-1, 8)
        return self.relu2(self.fc1(x))

class BasicModel_ConvNetWithPaddingDilation(nn.Module):
    def __init__(self, inplace: bool = False) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 2, 3, padding=3, stride=2, dilation=2)
        self.relu1 = nn.ReLU(inplace=inplace)
        self.fc1 = nn.Linear(16, 4)

    @no_type_check
    def forward(self, x: Tensor):
        bsz = x.shape[0]
        x = self.relu1(self.conv1(x))
        x = x.reshape(bsz, 2, -1)
        return self.fc1(x).reshape(bsz, -1)

class BasicModel_ConvNet(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 2, 3, 1)
        self.relu1 = nn.ReLU()
        self.pool1 = nn.MaxPool2d(2)
        self.conv2 = nn.Conv2d(2, 4, 3, 1)
        self.relu2 = nn.ReLU()
        self.pool2 = nn.MaxPool2d(2)
        self.fc1 = nn.Linear(4, 8)
        self.relu3 = nn.ReLU()
        self.fc2 = nn.Linear(8, 10)
        self.softmax = nn.Softmax(dim=1)

        self.fc1.weight = nn.Parameter(torch.ones(8, 4))
        self.fc2.weight = nn.Parameter(torch.ones(10, 8))

    @no_type_check
    def forward(self, x: Tensor) -> Tensor:
        x = self.relu1(self.conv1(x))
        x = self.pool1(x)
        x = self.relu2(self.conv2(x))
        x = self.pool2(x)
        x = x.view(-1, 4)
        x = self.relu3(self.fc1(x))
        x = self.fc2(x)
        return self.softmax(x)

class BasicModel_ConvNet_MaxPool1d(nn.Module):
    """Same as above, but with the MaxPool2d replaced
    with a MaxPool1d. This is useful because the MaxPool modules
    behave differently to other modules from the perspective
    of the DeepLift Attributions
    """

    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv1d(1, 2, 3)
        self.relu1 = nn.ReLU()
        self.pool1 = nn.MaxPool1d(2)
        self.conv2 = nn.Conv1d(2, 4, 3)
        self.relu2 = nn.ReLU()
        self.pool2 = nn.MaxPool1d(2)
        self.fc1 = nn.Linear(4, 8)
        self.relu3 = nn.ReLU()
        self.fc2 = nn.Linear(8, 10)
        self.softmax = nn.Softmax(dim=1)

        self.fc1.weight = nn.Parameter(torch.ones(8, 4))
        self.fc2.weight = nn.Parameter(torch.ones(10, 8))

    @no_type_check
    def forward(self, x: Tensor) -> Tensor:
        x = self.relu1(self.conv1(x))
        x = self.pool1(x)
        x = self.relu2(self.conv2(x))
        x = self.pool2(x)
        x = x.view(-1, 4)
        x = self.relu3(self.fc1(x))
        x = self.fc2(x)
        return self.softmax(x)

class BasicModel_ConvNet_MaxPool3d(nn.Module):
    """Same as above, but with the MaxPool1d replaced
    with a MaxPool3d. This is useful because the MaxPool modules
    behave differently to other modules from the perspective
    of the DeepLift Attributions
    """

    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv3d(1, 2, 3)
        self.relu1 = nn.ReLU()
        self.pool1 = nn.MaxPool3d(2)
        self.conv2 = nn.Conv3d(2, 4, 3)
        self.relu2 = nn.ReLU()
        self.pool2 = nn.MaxPool3d(2)
        self.fc1 = nn.Linear(4, 8)
        self.relu3 = nn.ReLU()
        self.fc2 = nn.Linear(8, 10)
        self.softmax = nn.Softmax(dim=1)

        self.fc1.weight = nn.Parameter(torch.ones(8, 4))
        self.fc2.weight = nn.Parameter(torch.ones(10, 8))

    def forward(self, x):
        x = self.relu1(self.conv1(x))
        x = self.pool1(x)
        x = self.relu2(self.conv2(x))
        x = self.pool2(x)
        x = x.view(-1, 4)
        x = self.relu3(self.fc1(x))
        x = self.fc2(x)
        return self.softmax(x)


import matplotlib.pyplot as plot
import pandas as pd
import numpy as np
import seaborn as sb

from sklearn.decomposition import PCA
from sklearn.decomposition import FactorAnalysis
from sklearn.cross_decomposition import CCA
from sklearn.cluster import AgglomerativeClustering
import scipy.cluster.hierarchy as hiclu

# pandas
data_frame = pd.read_csv("./resources/example_data.csv", index_col=0)
data_frame.sort_values("globalRank")
# print(data_frame.values, data_frame.index)

# numpy
def standardise(ndarray):
    means = np.mean(ndarray)
    stddev = np.std(ndarray)
    ndarray = (ndarray - means) / stddev
    return ndarray

def replace_na(ndarray):
    return ndarray.fillna(np.nanmean(ndarray))

replaced = replace_na(data_frame)
standardised = standardise(replaced)

r = np.corrcoef(standardised, rowvar=False)
correlation_data_frame = pd.DataFrame(r)
eigenvalues, eigenvectors = np.linalg.eigh(correlation_data_frame)
reversed_eigenvalues = [i for i in reversed(np.argsort(eigenvalues))]
a = eigenvectors[reversed_eigenvalues]
alpha = eigenvectors[:,reversed_eigenvalues]

# plot.plot(alpha, correlation_data_frame.values, "r+")
# sb.heatmap(np.corrcoef(alpha), cmap='bwr', vmin=0, vmax=1, annot=True)
# plot.show()

# graphics
# draw a heatmap
x_series = [[21,1,1],
            [1,2,1],
            [2,1,1]]

y_series = [[10,1,6],
            [10,3,0],
            [1,0,0]]

heatmap_data = np.corrcoef(x_series, y_series)
max = np.max(heatmap_data)
min = np.min(heatmap_data)
# sb.heatmap(np.round(heatmap_data, 2), cmap='bwr', vmin=min, vmax=max, annot=True)

# draw a circle
data_x = np.random.uniform(-1, 1, (20))
data_y = np.random.uniform(-1, 1, (20))
plot.figure("Circle", figsize=(6, 6))
c = [x for x in np.arange(0, np.math.pi * 2, 0.01)]
x = [np.sin(x) for x in c]
y = [np.cos(x) for x in c]
plot.plot(x, y)
plot.plot(data_x, data_y, 'r+')
# plot.show()

# draw a line graph
data_xi = [x for x in np.arange(0, 1, 0.1)]
data_yi = [x * np.random.uniform(1, -1) for x in data_xi]
plot.figure("Line chart", figsize=(3, 6))
plot.plot(data_xi, data_yi)
# plot.show()

# pca
components = PCA().fit(data_frame).explained_variance_ratio_
plot.plot([k*100 for k in components], data_frame.columns)
plot.show()

#efa
factors = FactorAnalysis().fit(data_frame).components_
plot.plot(data_frame.columns, factors)
plot.show()

#cca
cca = CCA(n_components=4).fit(data_frame[data_frame.columns[:5]], data_frame[data_frame.columns[5:]])
print(cca.x_scores_, cca.y_scores_, cca.x_loadings_, cca.y_loadings_)

#hca
clusters = AgglomerativeClustering(n_clusters=6, affinity='manhattan', linkage='average').fit(data_frame)
print(clusters.labels_)
hiclu.dendrogram(np.ndarray(clusters.labels_))


import logging

import pandas as pd
from path import Path

lg = logging.getLogger(__name__)

def save(dbdir, **tables):
    dbdir = Path(dbdir).expanduser()
    dbdir.makedirs_p()
    lg.info("saving to db dir: %s" % dbdir)
    assert dbdir.isdir()
    for name, table in tables.items():
        lg.info("saving %s (%d records)", name, len(table))
        table.to_pickle(dbdir / '%s.pickle' % (name))
        table.describe().to_csv(
            dbdir / '%s.describe.tsv' % (name), sep="\t")

def load(db, name):
    dbdir = Path(db).expanduser()
    return pd.read_pickle(dbdir / ('%s.pickle' % name))


import numpy as np
import os
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
from torch.autograd import Variable
from ptb_common import load_model, init_device, ptb_raw_data, ModelInfo, Batch, ptb_iterator, repackage_hidden
from models import RNN, GRU
from models import make_model as TRANSFORMER
from sklearn.preprocessing import minmax_scale

def compute_grad_per_timestep(model, device, data, loss_fn):
    """
    One epoch of training/validation (depending on flag is_train).
    """
    model.eval()

    # LOOP THROUGH MINIBATCHES
    for step, (x, y) in enumerate(ptb_iterator(data, model.batch_size, model.seq_len)):

        hidden = model.init_hidden()
        hidden = hidden.to(device)

        inputs = torch.from_numpy(x.astype(np.int64)).transpose(0, 1).contiguous().to(device)
        model.zero_grad()
        hidden = repackage_hidden(hidden)
        outputs, hidden = model(inputs, hidden)

        targets = torch.from_numpy(y.astype(np.int64)).transpose(0, 1).contiguous().to(device)
        # LOSS COMPUTATION
        last_loss = loss_fn(outputs[-1], targets[-1])

        # Compute gradient with respect to last loss. Average gradients in batch
        grads = torch.empty(0).to(device)
        for layer_states in model.state_history:
            batch_grads = torch.autograd.grad(last_loss, layer_states, retain_graph=True)
            avg_grads = [torch.mean(bg, 0) for bg in batch_grads]
            # Gradients for each layer concatenated into a single vector
            grads = torch.cat((grads, torch.stack(avg_grads)), dim=1)

        return [s.norm() for s in grads]

def plot_loss_per_step(model_infos, grads_by_model):

    results_folder = './5_results'
    x = np.arange(35) + 1

    for model_info, grads in zip(model_infos, grads_by_model):
        grads = minmax_scale(grads)
        plt.plot(x, grads, '-o', label=model_info.model)
    plt.title("$\\nabla h_t L_T$ Gradient Normal Vs. Timestep")
    plt.ylabel("Gradient Norm (Scaled)")
    plt.xlabel("Timestep")
    plt.legend()
    file_name = 'grads_per_timestep.png'
    plt.savefig(os.path.join(results_folder, file_name), bbox_inches='tight', pad_inches=0.2)
    plt.clf()
    plt.close()

def compute_grad_per_timestep_by_model():
    device = init_device()
    train_data, valid_data, test_data, word_to_id, id_2_word = ptb_raw_data(data_path='data')
    vocab_size = len(word_to_id)
    loss_fn = nn.CrossEntropyLoss()
    # # Models from 4_1
    model_infos = [ModelInfo('RNN', 'ADAM', 0.0001, 20, 35, 1500, 2, 0.35),
                   ModelInfo('GRU', 'SGD_LR_SCHEDULE', 10, 20, 35, 1500, 2, 0.35)]

    grads_by_model = []
    for model_info in model_infos:
        model = load_model(model_info, device, vocab_size)
        model.track_state_history = True
        grads_per_step = compute_grad_per_timestep(model, device, valid_data, loss_fn)
        grads_by_model.append(grads_per_step)

    # np.save('grads_by_model.npy', grads_by_model)
    # grads_by_model = np.load('grads_by_model.npy')
    plot_loss_per_step(model_infos, grads_by_model)

if __name__ == '__main__':
    print('5.2 - Gradient per timestep')
    compute_grad_per_timestep_by_model()


import plyplus
import os

GRAMMAR_DIR = os.getcwd() + '/raws_parser/dictionaries'

def parse_file(pack_name, file_name):
    grammar = plyplus.Grammar(plyplus.grammars.open(
                              GRAMMAR_DIR + '/base.g'),
                              auto_filter_tokens=False)

    file = os.getcwd() + '/packs/' + pack_name + '/raw/objects/' + file_name\
           + ".txt"
    try:
        result = grammar.parse(open(file).read())
    except plyplus.ParseError as e:
        result = {'error': e}

    return result


# -*- coding: utf-8 -*-
"""
Created on Tue Jul  4 22:39:40 2017

@author: 74297
"""

import numpy as np
import os
from subprocess import Popen, PIPE, STDOUT
from io import BytesIO
aadic={
        'A':1,
        'B':0,
        'C':2,
        'D':3,
        'E':4,

        'F':5,
        'G':6,
        'H':7,
        'I':8,
        'J':0,

        'K':9,
        'L':10,
        'M':11,
        'N':12,
        'O':0,

        'P':13,
        'Q':14,
        'R':15,
        'S':16,
        'T':17,
        'U':0,

        'V':18,
        'W':19,
        'X':0,
        'Y':20,
        'Z':0,
        '-':0,
        '*':0,
        }

def read_msa(file_path):
    lines=open(file_path).readlines()
    lines=[line.strip() for line in lines]
    n=len(lines)
    d=len(lines[0]) #CR AND LF
    msa=np.zeros([n,d],dtype=int)
    for i in  range(n):
        aline=lines[i]
        for j in range(d):
            msa[i,j]=aadic[aline[j]]
    return msa

def cal_large_matrix1(msa,weight):
    #output:21*l*21*l
    ALPHA=21
    pseudoc=1
    M=msa.shape[0]
    N=msa.shape[1]
    pab=np.zeros((ALPHA,ALPHA))
    pa=np.zeros((N,ALPHA))
    cov=np.zeros([N*ALPHA,N*ALPHA ])
    for i in range(N):
        for aa in range(ALPHA):
            pa[i,aa] = pseudoc
        neff=0.0
        for k in range(M):
            pa[i,msa[k,i]]+=weight[k]
            neff+=weight[k]
        for aa in range(ALPHA):
            pa[i,aa] /=pseudoc * ALPHA * 1.0 + neff
    #print(pab)
    for i in range(N):
        for j in range(i,N):
            for a in range(ALPHA):
                for b in range(ALPHA):
                    if i ==j :
                        if a==b :
                            pab[a,b]=pa[i,a]
                        else:
                            pab[a,b]=0.0
                    else:
                        pab[a,b] = pseudoc *1.0 /ALPHA
            if(i!=j):
                neff2=0;
                for k in range(M):
                    a=msa[k,i]
                    b=msa[k,j]
                    tmp=weight[k]
                    pab[a,b]+=tmp
                    neff2+=tmp
                for a in range(ALPHA):
                    for b in range(ALPHA):
                        pab[a,b] /= pseudoc*ALPHA*1.0 +neff2
            for a in range(ALPHA):
                for b in range(ALPHA):
                    if(i!=j or a==b):
                        if (pab[a][b] > 0.0):
                            cov[i*21+a][j*21+b]=pab[a][b] - pa[i][a] * pa[j][b]
                            cov[j*21+b][i*21+a]=cov[i*21+a][j*21+b]
                            #cov[a*ALPHA+b,i,j,]=pab[a][b] - pa[i][a] * pa[j][b]
                            #cov[a*ALPHA+b,j,i,]=cov[a*ALPHA+b,i,j]
            #cov1=cal_cov(pab,pa[<unsigned int>i],pa[<unsigned int>j])
            #cov[<unsigned int>i,<unsigned int>j]=cov[<unsigned int>j,<unsigned int>i]=cov1
    #print(pab)
    #print(pa)
    return cov


import json
from typing import Callable
from typing import Dict
from typing import List
from typing import Union
from json.decoder import JSONDecodeError
from http import HTTPStatus

from httpx import HTTPStatusError
import httpx

from austrakka.utils.exceptions import FailedResponseException
from austrakka.utils.exceptions import UnknownResponseException
from austrakka.utils.output import log_response
from austrakka.utils.context import CxtKey
from austrakka.utils.context import get_ctx_value
from austrakka import __version__

CONTENT_TYPE_JSON = 'application/json'
CONTENT_TYPE_MULTIPART = 'multipart/form-data; charset=utf-8; boundary=+++'

MODE_SKIP = 'skip'
MODE_OVERWRITE = 'overwrite'
MODE = 'mode'

def _get_default_headers(
        content_type: str = CONTENT_TYPE_JSON,
) -> Dict:
    default_headers = {
        'Content-Type': content_type,
        'Authorization': f'Bearer {get_ctx_value(CxtKey.CTX_TOKEN)}',
        'User-Agent': f'austrakka/{__version__}',
    }
    return default_headers

def _check_response(response: httpx.Response):
    # pylint: disable=raise-missing-from
    try:
        parsed_resp = response.json()
        if 'data' not in parsed_resp or 'messages' not in parsed_resp:
            raise UnknownResponseException(
                f'{response.status_code}: {parsed_resp}'
            )
        try:
            response.raise_for_status()
        except HTTPStatusError:
            raise FailedResponseException(parsed_resp)
    except JSONDecodeError:
        raise UnknownResponseException(
            f'{response.status_code}: {response.text}'
        )

def _get_data(body: Union[Dict, List] = None) -> str:
    return json.dumps(body) if body is not None else None

def _get_url(path: str):
    return f'{get_ctx_value(CxtKey.CTX_URI)}/api/{path}'

def get_response(response: httpx.Response, log_resp: bool = False) -> Dict:
    _check_response(response)
    parsed_resp = {} \
        if response.status_code == HTTPStatus.NO_CONTENT else response.json()
    if log_resp:
        log_response(parsed_resp)
    return parsed_resp

def _get_client(
        content_type: str = CONTENT_TYPE_JSON
):
    return httpx.Client(
        headers=_get_default_headers(content_type),
        verify=get_ctx_value(CxtKey.CTX_VERIFY_CERT),
        timeout=300,
        http2=get_ctx_value(CxtKey.CTX_USE_HTTP2),
    )

def _use_http_client(
        content_type: str = CONTENT_TYPE_JSON,
        log_resp: bool = False,
        parse_resp: bool = True
):
    def decorator(func):
        def inner_func(*args, **kwargs):
            with _get_client(content_type) as client:
                response = func(*args, **kwargs, client=client)
                if parse_resp:
                    return get_response(response, log_resp)
                return response
        return inner_func
    return decorator

@_use_http_client()
def api_get(
        path: str,
        params: Dict = None,
        client: httpx.Client = None,
):
    return client.get(
        _get_url(path),
        params=params,
    )

def api_get_stream(
        path: str,
        func: Callable[[httpx.Response], None],
):
    """
    Throws httpx.HTTPStatusError with status is not 2xx.
    """
    resp: httpx.Response
    with _get_client().stream("GET", _get_url(path)) as resp:
        resp.raise_for_status()
        func(resp)

@_use_http_client(content_type=CONTENT_TYPE_MULTIPART, log_resp=True)
def api_post_multipart(
        path: str,
        files,
        params: Dict = None,
        data: Union[Dict, List] = None,
        custom_headers: Dict = None,
        client: httpx.Client = None,
):
    custom_headers = {} if custom_headers is None else custom_headers
    return client.post(
        _get_url(path),
        data=data,
        params=params,
        files=files,
        headers=dict(client.headers) | custom_headers
    )

@_use_http_client(content_type=CONTENT_TYPE_MULTIPART,
                  log_resp=True, parse_resp=False)
def api_post_multipart_raw(
        path: str,
        files,
        params: Dict = None,
        data: Union[Dict, List] = None,
        custom_headers: Dict = None,
        client: httpx.Client = None,
):
    custom_headers = {} if custom_headers is None else custom_headers
    return client.post(
        _get_url(path),
        data=data,
        params=params,
        files=files,
        headers=dict(client.headers) | custom_headers
    )

@_use_http_client(log_resp=True)
def api_post(
        path: str,
        params: Dict = None,
        data: Union[Dict, List] = None,
        client: httpx.Client = None,
):
    return client.post(
        _get_url(path),
        data=json.dumps(data),
        params=params,
    )

@_use_http_client(log_resp=True)
def api_put(
        path: str,
        params: Dict = None,
        data: Union[Dict, List] = None,
        client: httpx.Client = None,
):
    return client.put(
        _get_url(path),
        data=json.dumps(data),
        params=params,
    )

@_use_http_client(log_resp=True)
def api_patch(
        path: str,
        params: Dict = None,
        data: Union[Dict, List] = None,
        client: httpx.Client = None,
):
    return client.patch(
        _get_url(path),
        data=json.dumps(data),
        params=params,
    )

@_use_http_client(log_resp=True)
def api_delete(
        path: str,
        params: Dict = None,
        custom_headers: Dict = None,
        client: httpx.Client = None,
):
    return client.delete(
        _get_url(path),
        params=params,
        headers=dict(client.headers) | custom_headers
    )

def set_mode_header(custom_headers, force, skip):
    if skip:
        custom_headers[MODE] = MODE_SKIP
    if force:
        custom_headers[MODE] = MODE_OVERWRITE


import itertools
from _operator import mul

def transpose(data):
    return list(zip(*data))

res = transpose([[1,2],[3,-1]])
print(res)

def transpose(it):
    return itertools.zip_longest(it[0], it[1])

def scalar_product(a, b):
    a1 = []
    a2 = []
    for i in a:
        if type(i) == int or type(i) == float:
            a1.append(i)
        elif type(i) == str:
            if i.isdigit():
                a1.append(float(i))
            else:
                return None
        else:
            return None
    for i in b:
        if type(i) == int or type(i) == float:
            a2.append(i)
        elif type(i) == str:
            if i.isdigit():
                a2.append(float(i))
            else:
                return None
        else:
            return None
    return sum(itertools.starmap(mul, itertools.zip_longest(a1, a2)))


#!/usr/bin/env python
# -*- coding: UTF-8 -*-
##############################################################################
#
# PDFgui            by DANSE Diffraction group
#                   Simon J. L. Billinge
#                   (c) 2006 trustees of the Michigan State University.
#                   All rights reserved.
#
# File coded by:    Chris Farrow
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE.txt for license information.
#
##############################################################################

# generated by wxGlade 0.9.3 on Fri Jul 19 16:01:22 2019

from __future__ import print_function

import wx
from diffpy.pdfgui.gui.pdfpanel import PDFPanel
from diffpy.pdfgui.gui import tooltips

class DataSetResultsPanel(wx.Panel, PDFPanel):
    def __init__(self, *args, **kwds):
        # begin wxGlade: DataSetResultsPanel.__init__
        kwds["style"] = kwds.get("style", 0) | wx.TAB_TRAVERSAL
        wx.Panel.__init__(self, *args, **kwds)

        sizer_7 = wx.BoxSizer(wx.VERTICAL)

        sizer_panelname = wx.StaticBoxSizer(wx.StaticBox(self, wx.ID_ANY, ""), wx.HORIZONTAL)
        sizer_7.Add(sizer_panelname, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 5)

        self.panelNameLabel = wx.StaticText(self, wx.ID_ANY, "Data Set Results")
        self.panelNameLabel.SetFont(wx.Font(18, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, 0, ""))
        sizer_panelname.Add(self.panelNameLabel, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT, 5)

        grid_sizer_1 = wx.FlexGridSizer(3, 2, 5, 10)
        sizer_7.Add(grid_sizer_1, 0, wx.ALL | wx.EXPAND, 5)

        self.labelScaleFactor = wx.StaticText(self, wx.ID_ANY, "Scale Factor")
        grid_sizer_1.Add(self.labelScaleFactor, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT | wx.LEFT, 5)

        self.textCtrlScaleFactor = wx.TextCtrl(self, wx.ID_ANY, "", style=wx.TE_READONLY)
        grid_sizer_1.Add(self.textCtrlScaleFactor, 0, wx.ALIGN_CENTER_VERTICAL, 20)

        self.labelQdamp = wx.StaticText(self, wx.ID_ANY, "Qdamp")
        grid_sizer_1.Add(self.labelQdamp, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT | wx.LEFT, 5)

        self.textCtrlQdamp = wx.TextCtrl(self, wx.ID_ANY, "", style=wx.TE_READONLY)
        grid_sizer_1.Add(self.textCtrlQdamp, 0, wx.ALIGN_CENTER_VERTICAL, 20)

        self.labelQbroad = wx.StaticText(self, wx.ID_ANY, "Qbroad")
        grid_sizer_1.Add(self.labelQbroad, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT | wx.LEFT, 5)

        self.textCtrlQbroad = wx.TextCtrl(self, wx.ID_ANY, "", style=wx.TE_READONLY)
        grid_sizer_1.Add(self.textCtrlQbroad, 0, wx.ALIGN_CENTER_VERTICAL, 20)

        self.buttonExport = wx.Button(self, wx.ID_OPEN, "Export PDF")
        self.buttonExport.Hide()
        sizer_7.Add(self.buttonExport, 0, wx.ALIGN_RIGHT | wx.ALL, 5)

        self.SetSizer(sizer_7)
        sizer_7.Fit(self)

        self.Layout()

        self.Bind(wx.EVT_BUTTON, self.onExport, self.buttonExport)
        # end wxGlade
        self.__customProperties()

    # USER CONFIGURATION CODE #################################################

    def __customProperties(self):
        self.results = {}
        self.ctrlMap = {'dscale'        :   'textCtrlScaleFactor',
                        'qdamp'         :   'textCtrlQdamp',
                        'qbroad'        :   'textCtrlQbroad',
                        }
        # Define tooltips.
        self.setToolTips(tooltips.datasetresultspanel)
        return

    def setResultsData(self):
        """Set the values in the results panel.

        The values are taken from the results member dictionary.
        dscale      --  float
        qdamp       --  float
        qbroad      --  float
        """
        for name in self.ctrlMap:
            value = self.results.get(name, None)
            ctrlName = self.ctrlMap[name]
            textCtrl = getattr(self, ctrlName)
            if value is not None:
                textCtrl.SetValue(str(value))
            else:
                textCtrl.SetValue('')
        return

    # EVENT CODE #############################################################

    def onExport(self, event): # wxGlade: DataSetResultsPanel.<event_handler>
        print("Event handler `onExport' not implemented")
        event.Skip()

    # Methods overloaded from PDFPanel
    def refresh(self):
        """Refresh the panel."""
        # Set the results data
        self.setResultsData()
        return

# end of class DataSetResultsPanel


import logging
import socket
from wsgiref.util import request_uri

from pyramid.threadlocal import get_current_request

__all__ = ('ContextFilter', 'EnvironFilter')

class ContextFilter(logging.Filter):
    """Provides context specific filter values.
    See also https://docs.python.org/3/library/logging.html#filter-objects
    """
    hostname = socket.gethostname()

    def filter(self, record):
        record.hostname = ContextFilter.hostname
        return True

class EnvironFilter(logging.Filter):
    """Exposes the request ``environ`` to the logger."""

    def _get_defaults(self, environ):
        if environ.get('HTTP_X_FORWARDED_FOR'):
            remote_addr = environ['HTTP_X_FORWARDED_FOR']
        elif environ.get('REMOTE_ADDR'):
            remote_addr = environ['REMOTE_ADDR']
        uri = environ.get('REQUEST_URI', None)
        if uri is None:
            uri = request_uri(environ)
        defaults = {
            'REMOTE_ADDR': remote_addr,
            'REMOTE_USER': environ.get('REMOTE_USER') or '-',
            'REQUEST_URI': uri,
            'HTTP_VERSION': environ.get('SERVER_PROTOCOL'),
            'HTTP_REFERER': environ.get('HTTP_REFERER', '-'),
            'HTTP_USER_AGENT': environ.get('HTTP_USER_AGENT', '-'),
            }
        return defaults

    def filter(self, record):
        request = get_current_request()
        # Apply a set of default values to the record.
        for key, value in self._get_defaults(request.environ).items():
            setattr(record, key, value)
        # Apply actual values to the record.
        for key, value in request.environ.items():
            setattr(record, key, value)

        # Set the status and byte length to the record.
        response = request.response
        bytes = '-'
        for name, value in response.headers.items():
            if name.lower() == 'content-length':
                bytes = value
                break
        record.bytes = bytes
        record.status = response.status

        return True


class Settings():
	"""Screen settings, for now"""
	def __init__(self):
		self.screen_width = 1200
		self.screen_height = 800
		self.bg_color = (0, 30, 60)

		# Bullet Settings
		self.bullet_width = 500
		self.bullet_height = 15
		self.bullet_color = (0, 250, 0)
		self.bullets_allowed = 3

		# Kim Jong Un Settings
		self.kim_drop_factor = 30

		# Stat Settings
		self.life_limit = 2

		# Level-Up Settings for Game Speed
		self.speedup_factor = 1.5

		self.initialize_dynamic_settings()

	def initialize_dynamic_settings(self):
		"""Settings that change throughout the game"""
		self.doodle_speed_factor = 8
		self.bullet_speed_factor = 15
		self.kim_speed_factor = 5

		# Direction factor: 1 is right, -1 is left
		self.squad_direction = 1

	def speed_up(self):
		"""Speed up the game when level-up"""
		self.doodle_speed_factor *= self.speedup_factor
		self.bullet_speed_factor *= self.speedup_factor
		self.kim_speed_factor *= self.speedup_factor


import sys

input = sys.stdin.readline

N, K = map(int, input().split())

answer = 1

for k in range(K):
    answer *= N
    answer /= k+1
    N = N-1

print(int(answer)*10007)


from flask import render_template, session, redirect, url_for, flash, jsonify, request, Response
from datetime import datetime, timedelta, date
from . import main
from .. import db
from ..models import Update
from .utils import *

@main.route('/')
def index():
    return render_template("index.html")

@main.route("/receiver", methods=["GET", "POST"])
def receiver():
    data = request.get_json(force = True)
    td = getdate()
    savedate = date(day=td.day, month=td.month, year=td.year)
    update = Update(user = data['user'], count = data['count'], date= savedate)
    db.session.add(update)
    db.session.commit()
    response = Response(status=200)
    return response

@main.route("/api/getuserdata/<user>")
def getuserdata(user):
    data = Update.query.filter_by(user=user).all()
    data = [i.json() for i in data]
    return jsonify(data)

@main.route("/api/getusers")
def getusers():
    data = Update.query.with_entities(Update.user).distinct().all()
    data = [dat.user for dat in data]
    return jsonify(data)

@main.route("/api/getdates")
def getdates():
    data = Update.query.with_entities(Update.date).distinct().all()
    data = [str(dat.date) for dat in data]
    return jsonify(data)

@main.route("/api/getdata")
def getdata():
    data = Update.query.with_entities(Update.user).distinct().all()
    resp = {}
    for dat in data :
        user = dat.user
        content = Update.query.filter_by(user=user).all()
        resp[user] = [{'x':str(c.date),'y':c.count} for c in content]
    return jsonify(resp)

def getdate():
    time = timedelta(hours=5, minutes=30)
    date = datetime.utcnow()+time
    return date